1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet258"
5     dependency "dwt" path="../../../../../../"
6     stringImportPaths "../../../../../res"
7     libs \
8       "atk-1.0" \
9       "cairo" \
10       "dl" \
11       "fontconfig" \
12       "gdk-x11-2.0" \
13       "gdk_pixbuf-2.0" \
14       "glib-2.0" \
15       "gmodule-2.0" \
16       "gnomeui-2" \
17       "gnomevfs-2" \
18       "gobject-2.0" \
19       "gthread-2.0" \
20       "gtk-x11-2.0" \
21       "pango-1.0" \
22       "pangocairo-1.0" \
23       "X11" \
24       "Xcomposite" \
25       "Xcursor" \
26       "Xdamage" \
27       "Xext" \
28       "Xfixes" \
29       "Xi" \
30       "Xinerama" \
31       "Xrandr" \
32       "Xrender" \
33       "Xtst" \
34       platform="linux"
35 +/
36 
37 /*******************************************************************************
38  * Copyright (c) 2007 IBM Corporation and others.
39  * All rights reserved. This program and the accompanying materials
40  * are made available under the terms of the Eclipse Public License v1.0
41  * which accompanies this distribution, and is available at
42  * http://www.eclipse.org/legal/epl-v10.html
43  *
44  * Contributors:
45  *     IBM Corporation - initial API and implementation
46  * Port to the D programming language:
47  *     Jesse Phillips <Jesse.K.Phillips+D> gmail.com
48  *******************************************************************************/
49 
50 module org.eclipse.swt.snippets.Snippet258;
51 
52 /*
53  * Create a search text control
54  *
55  * For a list of all SWT example snippets see
56  * http://www.eclipse.org/swt/snippets/
57  *
58  * @since 3.3
59  */
60 import org.eclipse.swt.SWT;
61 import java.io.ByteArrayInputStream;
62 import org.eclipse.swt.events.SelectionAdapter;
63 import org.eclipse.swt.events.SelectionEvent;
64 import org.eclipse.swt.graphics.Image;
65 import org.eclipse.swt.graphics.ImageData;
66 import org.eclipse.swt.layout.GridLayout;
67 import org.eclipse.swt.layout.GridData;
68 import org.eclipse.swt.widgets.Display;
69 import org.eclipse.swt.widgets.Shell;
70 import org.eclipse.swt.widgets.Text;
71 import org.eclipse.swt.widgets.ToolBar;
72 import org.eclipse.swt.widgets.ToolItem;
73 
74 version(Tango){
75     import tango.io.Stdout;
76 } else { // Phobos
77     import std.stdio;
78 }
79 
80 void main() {
81     auto display = new Display();
82     auto shell = new Shell(display);
83     shell.setLayout(new GridLayout(2, false));
84 
85     auto text = new Text(shell, SWT.SEARCH | SWT.CANCEL);
86     Image image = null;
87     if ((text.getStyle() & SWT.CANCEL) == 0) {
88         image = new Image (display, new ImageData(new ByteArrayInputStream( cast(byte[]) import("links_obj.gif" ))));
89         auto toolBar = new ToolBar (shell, SWT.FLAT);
90         auto item = new ToolItem (toolBar, SWT.PUSH);
91         item.setImage (image);
92         item.addSelectionListener(new class SelectionAdapter {
93             override
94             public void widgetSelected(SelectionEvent e) {
95                 text.setText("");
96                 version(Tango){
97                     Stdout("Search cancelled").newline;
98                 } else { // Phobos
99                     writeln("Search cancelled");
100                 }
101             }
102         });
103     }
104     text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105     text.setText("Search text");
106     text.addSelectionListener(new class SelectionAdapter {
107         override
108         public void widgetDefaultSelected(SelectionEvent e) {
109             version(Tango){
110                 if (e.detail == SWT.CANCEL) {
111                     Stdout("Search cancelled").newline;
112                 } else {
113                     Stdout("Searching for: ")(text.getText())("...").newline;
114                 }
115             } else { // Phobos
116                 if (e.detail == SWT.CANCEL) {
117                     writeln("Search cancelled");
118                 } else {
119                     writeln("Searching for: " ~ text.getText() ~ "...");
120                 }
121             }
122         }
123     });
124 
125     shell.pack();
126     shell.open();
127     while (!shell.isDisposed()) {
128         if (!display.readAndDispatch()) display.sleep();
129     }
130     if (image !is null) image.dispose();
131     display.dispose();
132 }