view snippets/display/Snippet68.d @ 132:104f5ed240fc

More snippets from TomD, thanks!
author Frank Benoit <benoit@tionex.de>
date Tue, 29 Jul 2008 01:50:10 +0200
parents
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * D Port:
 *     Thomas Demmer <t_demmer AT web DOT de>
 *******************************************************************************/
module display.Snippet68;
 
/*
 * Display example snippet: stop a repeating timer when a button is pressed
 *
 * For a list of all DWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import dwt.DWT;
import dwt.graphics.Color;
import dwt.widgets.Button;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Label;
import dwt.widgets.Listener;
import dwt.widgets.Shell;

import dwt.dwthelper.utils;
import dwt.dwthelper.Runnable;

import dwt.layout.RowData;
import dwt.layout.RowLayout;

void main (String [] args) {
    Display display = new Display ();
    final Color red = display.getSystemColor (DWT.COLOR_RED);
    final Color blue = display.getSystemColor (DWT.COLOR_BLUE);
    Shell shell = new Shell (display);
    shell.setLayout (new RowLayout ());
    Button button = new Button (shell, DWT.PUSH);
    button.setText ("Stop Timer");
    final Label label = new Label (shell, DWT.BORDER);
    label.setBackground (red);
    final int time = 500;
    Runnable timer;
    timer = dgRunnable({
        if (label.isDisposed ()) return;
        Color color = label.getBackground ().opEquals (red) ? blue : red;
        label.setBackground (color);
        display.timerExec (time, timer);
    });
    display.timerExec (time, timer);
    button.addListener (DWT.Selection, dgListener( (Event event) {
       display.timerExec (-1, timer);
    }));
    button.pack ();
    label.setLayoutData (new RowData (button.getSize ()));
    shell.pack ();
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}