1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet128"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * Ported to the D Programming Language:
46  *     by John Reimer
47  *******************************************************************************/
48 module Snippet128;
49 
50 /*
51  * Browser example snippet: bring up a browser
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  *
56  * @since 3.0
57  */
58 
59 import java.lang.all;
60 
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.SWTError;
63 import org.eclipse.swt.SWTException;
64 
65 import org.eclipse.swt.browser.Browser;
66 import org.eclipse.swt.browser.ProgressEvent;
67 import org.eclipse.swt.browser.ProgressListener;
68 import org.eclipse.swt.browser.StatusTextEvent;
69 import org.eclipse.swt.browser.StatusTextListener;
70 import org.eclipse.swt.browser.LocationEvent;
71 import org.eclipse.swt.browser.LocationListener;
72 
73 import org.eclipse.swt.layout.GridLayout;
74 import org.eclipse.swt.layout.GridData;
75 
76 import org.eclipse.swt.widgets.Shell;
77 import org.eclipse.swt.widgets.Display;
78 import org.eclipse.swt.widgets.ToolBar;
79 import org.eclipse.swt.widgets.ToolItem;
80 import org.eclipse.swt.widgets.Label;
81 import org.eclipse.swt.widgets.Text;
82 import org.eclipse.swt.widgets.ProgressBar;
83 import org.eclipse.swt.widgets.Listener;
84 import org.eclipse.swt.widgets.Event;
85 
86 void main() {
87     Display display = new Display();
88     Shell shell = new Shell(display);
89     GridLayout gridLayout = new GridLayout();
90     gridLayout.numColumns = 3;
91     shell.setLayout(gridLayout);
92     ToolBar toolbar = new ToolBar(shell, SWT.NONE);
93     ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
94     itemBack.setText("Back");
95     ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
96     itemForward.setText("Forward");
97     ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
98     itemStop.setText("Stop");
99     ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
100     itemRefresh.setText("Refresh");
101     ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
102     itemGo.setText("Go");
103 
104     GridData data = new GridData();
105     data.horizontalSpan = 3;
106     toolbar.setLayoutData(data);
107 
108     Label labelAddress = new Label(shell, SWT.NONE);
109     labelAddress.setText("Address");
110 
111     Text location = new Text(shell, SWT.BORDER);
112     data = new GridData();
113     data.horizontalAlignment = GridData.FILL;
114     data.horizontalSpan = 2;
115     data.grabExcessHorizontalSpace = true;
116     location.setLayoutData(data);
117 
118     Browser browser;
119     try {
120         browser = new Browser(shell, SWT.NONE);
121     } catch (SWTError e) {
122         getDwtLogger().error ( __FILE__, __LINE__, "Could not instantiate Browser: " ~ e.getMessage());
123         return;
124     }
125     data = new GridData();
126     data.horizontalAlignment = GridData.FILL;
127     data.verticalAlignment = GridData.FILL;
128     data.horizontalSpan = 3;
129     data.grabExcessHorizontalSpace = true;
130     data.grabExcessVerticalSpace = true;
131     browser.setLayoutData(data);
132 
133     Label status = new Label(shell, SWT.NONE);
134     data = new GridData(GridData.FILL_HORIZONTAL);
135     data.horizontalSpan = 2;
136     status.setLayoutData(data);
137 
138     ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
139     data = new GridData();
140     data.horizontalAlignment = GridData.END;
141     progressBar.setLayoutData(data);
142 
143     /* event handling */
144     Listener listener = new class Listener {
145         public void handleEvent(Event event) {
146             ToolItem item = cast(ToolItem)event.widget;
147             String string = item.getText();
148             if (string.equals("Back")) browser.back();
149             else if (string.equals("Forward")) browser.forward();
150             else if (string.equals("Stop")) browser.stop();
151             else if (string.equals("Refresh")) browser.refresh();
152             else if (string.equals("Go")) browser.setUrl(location.getText());
153        }
154     };
155     browser.addProgressListener(new class ProgressListener {
156         public void changed(ProgressEvent event) {
157                 if (event.total == 0) return;
158                 int ratio = event.current * 100 / event.total;
159                 progressBar.setSelection(ratio);
160         }
161         public void completed(ProgressEvent event) {
162             progressBar.setSelection(0);
163         }
164     });
165     browser.addStatusTextListener(new class StatusTextListener {
166         public void changed(StatusTextEvent event) {
167             status.setText(event.text);
168         }
169     });
170     browser.addLocationListener(new class LocationListener {
171         public void changed(LocationEvent event) {
172             if (event.top) location.setText(event.location);
173         }
174         public void changing(LocationEvent event) {
175         }
176     });
177     itemBack.addListener(SWT.Selection, listener);
178     itemForward.addListener(SWT.Selection, listener);
179     itemStop.addListener(SWT.Selection, listener);
180     itemRefresh.addListener(SWT.Selection, listener);
181     itemGo.addListener(SWT.Selection, listener);
182     location.addListener(SWT.DefaultSelection, new class Listener {
183         public void handleEvent(Event e) {
184             browser.setUrl(location.getText());
185         }
186     });
187 
188     shell.open();
189     browser.setUrl("http://eclipse.org");
190 
191     while (!shell.isDisposed()) {
192         if (!display.readAndDispatch())
193             display.sleep();
194     }
195     display.dispose();
196 }
197