Changeset 117:8cdaac0dc743

Show
Ignore:
Timestamp:
07/12/08 14:09:06 (5 months ago)
Author:
Frank Benoit <benoit@tionex.de>
branch:
default
Message:

Added more snippets from TomD

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • snippets/busyindicator/Snippet130.d

    r113 r117  
    88 * Contributors: 
    99 *     IBM Corporation - initial API and implementation 
     10 * D Port: 
     11 *     Thomas Demmer <t_demmer AT web DOT de> 
    1012 *******************************************************************************/ 
    11  
     13module busyindicator.Snippet130; 
    1214/* 
    1315 * BusyIndicator example snippet: display busy cursor during long running task 
     
    1618 * http://www.eclipse.org/swt/snippets/ 
    1719 */ 
    18 module busyindicator.Snippet130; 
    19  
    2020import dwt.DWT; 
    2121import dwt.events.SelectionAdapter; 
     
    3737 
    3838import tango.core.Thread; 
     39import tango.io.Stdout; 
    3940import tango.util.Convert; 
    4041import tango.util.log.Trace; 
     42 
    4143 
    4244void main(String[] args){ 
     
    5254        Text text = new Text(shell, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL); 
    5355        text.setLayoutData(new GridData(GridData.FILL_BOTH)); 
    54         int[1] nextId
     56        int[] nextId = new int[1]
    5557        Button b = new Button(shell, DWT.PUSH); 
    5658        b.setText("invoke long running job"); 
     
    6163                    bool done = false; 
    6264                    int id; 
    63                     private void runThread(){ 
    64                         id = nextId[0]++; 
    65                         display.syncExec( dgRunnable( &printStart, text, id )); 
    66                         for (int i = 0; i < 6; i++) { 
     65                    public void run() { 
     66                        Thread thread = new Thread({ 
     67                            id = nextId[0]++; 
     68                            display.syncExec(new class() Runnable { 
     69                                public void run() { 
     70                                if (text.isDisposed()) return; 
     71                                text.append("\nStart long running task "~to!(char[])(id)); 
     72                                } 
     73                                }); // display.syncExec 
     74                            /* 
     75                             * This crashes when more than 1 thread gets created. THD 
     76                             for (int i = 0; i < 100000; i++) { 
     77                             if (display.isDisposed()) return; 
     78                             Stdout.formatln("do task that takes a long time in a separate thread {}", id); 
     79                             } 
     80                             */ 
     81                            // This runs fine 
     82                            for (int i = 0; i < 6; i++) { 
     83                                if (display.isDisposed()) return; 
     84                                Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i); 
     85                                Thread.sleep(0.500); 
     86                            } 
     87 
    6788                            if (display.isDisposed()) return; 
    68                             Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i); 
    69                             Thread.sleep(0.500); 
    70                         } 
    71                         if (display.isDisposed()) return; 
    72                         display.syncExec( dgRunnable( &printEnd , text, id )); // display.syncExec 
    73                         done = true; 
    74                         display.wake(); 
    75                     } 
    76                     public void run() { 
    77                         Thread thread = new Thread( &runThread ); 
     89                            display.syncExec(new class() Runnable { 
     90                                public void run() { 
     91                                    if (text.isDisposed()) return; 
     92                                    text.append("\nCompleted long running task "~to!(char[])(id)); 
     93                                } 
     94                            }); // display.syncExec 
     95                            done = true; 
     96                            display.wake(); 
     97                        }); // thread = ... 
    7898                        thread.start(); 
    7999 
     
    108128    } 
    109129} 
    110  
    111  
  • snippets/button/Snippet108.d

    r114 r117  
    3333 
    3434void main(String[] args){ 
    35   Snippet108.main(args); 
     35    Snippet108.main(args); 
    3636} 
    3737 
    3838public class Snippet108 { 
    3939 
    40   public static void main (String [] args) { 
    41     Display display = new Display (); 
    42     Shell shell = new Shell (display); 
    43     Label label = new Label (shell, DWT.NONE); 
    44     label.setText ("Enter your name:"); 
    45     Text text = new Text (shell, DWT.BORDER); 
    46     text.setLayoutData (new RowData (100, DWT.DEFAULT)); 
    47     Button ok = new Button (shell, DWT.PUSH); 
    48     ok.setText ("OK"); 
    49     ok.addSelectionListener(new class() SelectionAdapter { 
    50         public void widgetSelected(SelectionEvent e) { 
    51       Stdout.formatln("OK"); 
    52     } 
    53                 }); 
    54     Button cancel = new Button (shell, DWT.PUSH); 
    55     cancel.setText ("Cancel"); 
    56     cancel.addSelectionListener(new class() SelectionAdapter { 
    57         public void widgetSelected(SelectionEvent e) { 
    58       Stdout.formatln("Cancel"); 
    59     } 
    60                 }); 
    61     shell.setDefaultButton (cancel); 
    62     shell.setLayout (new RowLayout ()); 
    63     shell.pack (); 
    64     shell.open (); 
    65     while (!shell.isDisposed ()) { 
    66       if (!display.readAndDispatch ()) display.sleep (); 
     40    public static void main (String [] args) { 
     41        Display display = new Display (); 
     42        Shell shell = new Shell (display); 
     43        Label label = new Label (shell, DWT.NONE); 
     44        label.setText ("Enter your name:"); 
     45        Text text = new Text (shell, DWT.BORDER); 
     46        text.setLayoutData (new RowData (100, DWT.DEFAULT)); 
     47        Button ok = new Button (shell, DWT.PUSH); 
     48        ok.setText ("OK"); 
     49        ok.addSelectionListener(new class() SelectionAdapter { 
     50            public void widgetSelected(SelectionEvent e) { 
     51                Stdout.formatln("OK"); 
     52            } 
     53        }); 
     54        Button cancel = new Button (shell, DWT.PUSH); 
     55        cancel.setText ("Cancel"); 
     56        cancel.addSelectionListener(new class() SelectionAdapter { 
     57            public void widgetSelected(SelectionEvent e) { 
     58                Stdout.formatln("Cancel"); 
     59            } 
     60        }); 
     61        shell.setDefaultButton (cancel); 
     62        shell.setLayout (new RowLayout ()); 
     63        shell.pack (); 
     64        shell.open (); 
     65        while (!shell.isDisposed ()) { 
     66            if (!display.readAndDispatch ()) display.sleep (); 
     67        } 
     68        display.dispose (); 
    6769    } 
    68     display.dispose (); 
    69   } 
    7070} 
     71 
  • snippets/button/Snippet169.d

    r114 r117  
    3535 
    3636void main(String[] args){ 
    37   Snippet169.main(args); 
     37    Snippet169.main(args); 
    3838} 
    3939 
    4040 
    4141public class Snippet169 { 
    42   public static void main (String [] args) { 
    43     Display display = new Display (); 
    44     final Shell shell = new Shell (display); 
    45     shell.setLayout (new FillLayout ()); 
    46     Listener listener = new class() Listener { 
    47       public void handleEvent (Event e) { 
    48     Control [] children = shell.getChildren (); 
    49     for (int i=0; i<children.length; i++) { 
    50       Control child = children [i]; 
    51       if (e.widget !is child && cast(Button)child !is null && (child.getStyle () & DWT.TOGGLE) != 0) { 
    52         (cast(Button) child).setSelection (false); 
    53       } 
    54     } 
    55     (cast(Button) e.widget).setSelection (true); 
    56       } 
    57     }; 
    58     for (int i=0; i<20; i++) { 
    59       Button button = new Button (shell, DWT.TOGGLE); 
    60       button.setText ("B" ~to!(char[])(i)); 
    61       button.addListener (DWT.Selection, listener); 
    62       if (i == 0) button.setSelection (true); 
     42    public static void main (String [] args) { 
     43        Display display = new Display (); 
     44        final Shell shell = new Shell (display); 
     45        shell.setLayout (new FillLayout ()); 
     46        Listener listener = new class() Listener { 
     47            public void handleEvent (Event e) { 
     48                Control [] children = shell.getChildren (); 
     49                for (int i=0; i<children.length; i++) { 
     50                    Control child = children [i]; 
     51                    if (e.widget !is child && cast(Button)child !is null && (child.getStyle () & DWT.TOGGLE) != 0) { 
     52                        (cast(Button) child).setSelection (false); 
     53                    } 
     54                } 
     55                (cast(Button) e.widget).setSelection (true); 
     56            } 
     57        }; 
     58        for (int i=0; i<20; i++) { 
     59            Button button = new Button (shell, DWT.TOGGLE); 
     60            button.setText ("B" ~to!(char[])(i)); 
     61            button.addListener (DWT.Selection, listener); 
     62            if (i == 0) button.setSelection (true); 
     63        } 
     64        shell.pack (); 
     65        shell.open (); 
     66        while (!shell.isDisposed ()) { 
     67            if (!display.readAndDispatch ()) display.sleep (); 
     68        } 
     69        display.dispose (); 
    6370    } 
    64     shell.pack (); 
    65     shell.open (); 
    66     while (!shell.isDisposed ()) { 
    67       if (!display.readAndDispatch ()) display.sleep (); 
    68     } 
    69     display.dispose (); 
    70   } 
    7171} 
  • snippets/button/Snippet206.d

    r114 r117  
    2121 * @since 3.2 
    2222 */ 
    23 /* Port OK */ 
    2423 
    2524import dwt.DWT; 
     
    3332 
    3433void main(String[] args){ 
    35   Snippet206.main(args); 
     34    Snippet206.main(args); 
    3635} 
    3736 
    3837public class Snippet206 { 
    39   public static void main(String[] args) { 
    40     Display display = new Display(); 
    41     Image image = display.getSystemImage(DWT.ICON_QUESTION); 
    42     Shell shell = new Shell(display); 
    43     shell.setLayout (new GridLayout()); 
    44     Button button = new Button(shell, DWT.PUSH); 
    45     button.setImage(image); 
    46     button.setText("Button"); 
    47     shell.setSize(300, 300); 
    48     shell.open(); 
    49     while (!shell.isDisposed ()) { 
    50       if (!display.readAndDispatch ()) display.sleep (); 
     38    public static void main(String[] args) { 
     39        Display display = new Display(); 
     40        Image image = display.getSystemImage(DWT.ICON_QUESTION); 
     41        Shell shell = new Shell(display); 
     42        shell.setLayout (new GridLayout()); 
     43        Button button = new Button(shell, DWT.PUSH); 
     44        button.setImage(image); 
     45        button.setText("Button"); 
     46        shell.setSize(300, 300); 
     47        shell.open(); 
     48        while (!shell.isDisposed ()) { 
     49            if (!display.readAndDispatch ()) display.sleep (); 
     50        } 
     51        display.dispose (); 
    5152    } 
    52     display.dispose (); 
    53   } 
    5453} 
  • snippets/button/Snippet224.d

    r114 r117  
    3535 
    3636void main(String[] args){ 
    37   Snippet224.main(args); 
     37    Snippet224.main(args); 
    3838} 
    3939 
    4040 
    4141public class Snippet224 { 
    42   public static void main (String [] args) { 
    43     Display display = new Display (); 
    44     Shell shell = new Shell (display); 
    45     shell.setLayout (new RowLayout (DWT.VERTICAL)); 
    46     for (int i=0; i<8; i++) { 
    47       Button button = new Button (shell, DWT.RADIO); 
    48       button.setText ("B" ~ to!(char[])(i)); 
    49       if (i == 0) button.setSelection (true); 
     42    public static void main (String [] args) { 
     43        Display display = new Display (); 
     44        Shell shell = new Shell (display); 
     45        shell.setLayout (new RowLayout (DWT.VERTICAL)); 
     46        for (int i=0; i<8; i++) { 
     47            Button button = new Button (shell, DWT.RADIO); 
     48            button.setText ("B" ~ to!(char[])(i)); 
     49            if (i == 0) button.setSelection (true); 
     50        } 
     51        Button button = new Button (shell, DWT.PUSH); 
     52        button.setText ("Set Selection to B4"); 
     53        button.addListener (DWT.Selection, new class() Listener{ 
     54            public void handleEvent (Event event) { 
     55                Control [] children = shell.getChildren (); 
     56                Button newButton = cast(Button) children [4]; 
     57                for (int i=0; i<children.length; i++) { 
     58                    Control child = children [i]; 
     59                    if ( cast(Button)child !is null && (child.getStyle () & DWT.RADIO) != 0) { 
     60                        (cast(Button)child).setSelection (false); 
     61                    } 
     62                } 
     63                newButton.setSelection (true); 
     64            } 
     65        }); 
     66        shell.pack (); 
     67        shell.open (); 
     68        while (!shell.isDisposed ()) { 
     69            if (!display.readAndDispatch ()) display.sleep (); 
     70        } 
     71        display.dispose (); 
    5072    } 
    51     Button button = new Button (shell, DWT.PUSH); 
    52     button.setText ("Set Selection to B4"); 
    53     button.addListener (DWT.Selection, new class() Listener{ 
    54         public void handleEvent (Event event) { 
    55       Control [] children = shell.getChildren (); 
    56       Button newButton = cast(Button) children [4]; 
    57       for (int i=0; i<children.length; i++) { 
    58         Control child = children [i]; 
    59         if ( cast(Button)child !is null && (child.getStyle () & DWT.RADIO) != 0) { 
    60           (cast(Button)child).setSelection (false); 
    61         } 
    62       } 
    63       newButton.setSelection (true); 
    64     } 
    65     }); 
    66     shell.pack (); 
    67     shell.open (); 
    68     while (!shell.isDisposed ()) { 
    69       if (!display.readAndDispatch ()) display.sleep (); 
    70     } 
    71     display.dispose (); 
    72   } 
    7373} 
  • snippets/button/Snippet293.d

    r78 r117  
    1111 *     Jesse Phillips <Jesse.K.Phillips+D> gmail.com 
    1212 *******************************************************************************/ 
    13 module snippets.button.Snippet293; 
     13module button.Snippet293; 
    1414 
    1515/* 
     
    2626 
    2727void main() { 
    28     auto display = new Display(); 
    29     auto shell = new Shell(display); 
    30     shell.setLayout(new GridLayout()); 
    31      
    32     auto b1 = new Button (shell, DWT.CHECK); 
    33     b1.setText("State 1"); 
    34     b1.setSelection(true); 
    35      
    36     auto b2 = new Button (shell, DWT.CHECK); 
    37     b2.setText("State 2"); 
    38     b2.setSelection(false); 
    39      
    40     auto b3 = new Button (shell, DWT.CHECK); 
    41     b3.setText("State 3"); 
    42     b3.setSelection(true); 
     28    auto display = new Display(); 
     29    auto shell = new Shell(display); 
     30    shell.setLayout(new GridLayout()); 
    4331 
    44     // This function does not appear in the api for swt 3.3 
    45     //  b3.setGrayed(true); 
    46      
    47     shell.pack(); 
    48     shell.open(); 
    49     while (!shell.isDisposed()) { 
    50         if (!display.readAndDispatch()) 
    51             display.sleep(); 
    52     } 
    53     display.dispose(); 
     32    auto b1 = new Button (shell, DWT.CHECK); 
     33    b1.setText("State 1"); 
     34    b1.setSelection(true); 
     35 
     36    auto b2 = new Button (shell, DWT.CHECK); 
     37    b2.setText("State 2"); 
     38    b2.setSelection(false); 
     39 
     40    auto b3 = new Button (shell, DWT.CHECK); 
     41    b3.setText("State 3"); 
     42    b3.setSelection(true); 
     43 
     44    // This function does not appear in the api for swt 3.3 
     45    //  b3.setGrayed(true); 
     46 
     47    shell.pack(); 
     48    shell.open(); 
     49    while (!shell.isDisposed()) { 
     50        if (!display.readAndDispatch()) 
     51            display.sleep(); 
     52    } 
     53    display.dispose(); 
    5454} 
  • snippets/button/Snippet294.d

    r114 r117  
    2828 
    2929void main(String[] args){ 
    30   Snippet294.main(args); 
     30    Snippet294.main(args); 
    3131} 
    3232 
     
    4242public class Snippet294 { 
    4343 
    44   static int[] circle(int r, int offsetX, int offsetY) { 
    45     int[] polygon = new int[8 * r + 4]; 
    46     // x^2 + y^2 = r^2 
    47     for (int i = 0; i < 2 * r + 1; i++) { 
    48       int x = i - r; 
    49       int y = cast(int)Math.sqrt(cast(real)(r*r - x*x)); 
    50       polygon[2*i] = offsetX + x; 
    51       polygon[2*i+1] = offsetY + y; 
    52       polygon[8*r - 2*i - 2] = offsetX + x; 
    53       polygon[8*r - 2*i - 1] = offsetY - y; 
     44    static int[] circle(int r, int offsetX, int offsetY) { 
     45        int[] polygon = new int[8 * r + 4]; 
     46        // x^2 + y^2 = r^2 
     47        for (int i = 0; i < 2 * r + 1; i++) { 
     48            int x = i - r; 
     49            int y = cast(int)Math.sqrt(cast(real)(r*r - x*x)); 
     50            polygon[2*i] = offsetX + x; 
     51            polygon[2*i+1] = offsetY + y; 
     52            polygon[8*r - 2*i - 2] = offsetX + x; 
     53            polygon[8*r - 2*i - 1] = offsetY - y; 
     54        } 
     55        return polygon; 
    5456    } 
    55     return polygon; 
    56   } 
    5757 
    58   public static void main(String[] args) { 
    59     Display display = new Display(); 
    60     Shell shell = new Shell(display); 
    61     shell.setText("Regions on a Control"); 
    62     shell.setLayout(new FillLayout()); 
    63     shell.setBackground(display.getSystemColor(DWT.COLOR_DARK_RED)); 
     58    public static void main(String[] args) { 
     59        Display display = new Display(); 
     60        Shell shell = new Shell(display); 
     61        shell.setText("Regions on a Control"); 
     62        shell.setLayout(new FillLayout()); 
     63        shell.setBackground(display.getSystemColor(DWT.COLOR_DARK_RED)); 
    6464 
    65     Button b2 = new Button(shell, DWT.PUSH); 
    66     b2.setText("Button with Regions"); 
     65        Button b2 = new Button(shell, DWT.PUSH); 
     66        b2.setText("Button with Regions"); 
    6767 
    68     // define a region that looks like a circle with two holes in ot 
    69     Region region = new Region(); 
    70     region.add(circle(67, 87, 77)); 
    71     region.subtract(circle(20, 87, 47)); 
    72     region.subtract(circle(20, 87, 113)); 
     68        // define a region that looks like a circle with two holes in ot 
     69        Region region = new Region(); 
     70        region.add(circle(67, 87, 77)); 
     71        region.subtract(circle(20, 87, 47)); 
     72        region.subtract(circle(20, 87, 113)); 
    7373 
    74     // define the shape of the button using setRegion 
    75     b2.setRegion(region); 
    76     b2.setLocation(100,50); 
     74        // define the shape of the button using setRegion 
     75        b2.setRegion(region); 
     76        b2.setLocation(100,50); 
    7777 
    78     b2.addListener(DWT.Selection, new class() Listener { 
    79         public void handleEvent(Event e) { 
    80      shell.close(); 
    81    
    82           }); 
     78        b2.addListener(DWT.Selection, new class() Listener { 
     79                public void handleEvent(Event e) { 
     80                shell.close(); 
     81               
     82                }); 
    8383 
    84     shell.setSize(200,200); 
    85     shell.open(); 
     84        shell.setSize(200,200); 
     85        shell.open(); 
    8686 
    87     while (!shell.isDisposed()) { 
    88       if (!display.readAndDispatch()) 
    89     display.sleep(); 
     87        while (!shell.isDisposed()) { 
     88            if (!display.readAndDispatch()) 
     89                display.sleep(); 
     90        } 
     91        region.dispose(); 
     92        display.dispose(); 
    9093    } 
    91     region.dispose(); 
    92     display.dispose(); 
    93   } 
    9494} 
  • snippets/dsss.conf

    r114 r117  
    1818 
    1919[busyindicator/Snippet130.d] 
     20[busyindicator/Snippet130a.d] 
    2021[button/Snippet108.d] 
     22[button/Snippet162.d] 
    2123[button/Snippet169.d] 
    2224[button/Snippet206.d] 
     
    2931[canvas/Snippet275.d] 
    3032[canvas/Snippet290.d] 
     33[caret/Snippet43.d] 
     34[caret/Snippet74.d] 
     35[ccombo/Snippet39.d] 
     36[clipboard/Snippet94.d] 
     37[clipboard/Snippet122.d] 
     38[clipboard/Snippet282.d] 
     39[color/Snippet208.d] 
    3140[control/Snippet25.d] 
    3241[control/Snippet62.d]