Mercurial > projects > dwt-samples
comparison snippets/browser/Snippet128.d @ 176:219f3c1087d1
Second browser test -- loads eclipse.org url
author | John Reimer <terminal.node@gmail.com> |
---|---|
date | Sun, 02 Nov 2008 08:12:48 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
175:548a657378b8 | 176:219f3c1087d1 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2004 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Ported to the D Programming Language: | |
11 * by John Reimer | |
12 *******************************************************************************/ | |
13 module Snippet128; | |
14 | |
15 /* | |
16 * Browser example snippet: bring up a browser | |
17 * | |
18 * For a list of all DWT example snippets see | |
19 * http://www.eclipse.org/swt/snippets/ | |
20 * | |
21 * @since 3.0 | |
22 */ | |
23 | |
24 version(linux) | |
25 { | |
26 version(build) | |
27 { | |
28 pragma(link,"stdc++"); | |
29 pragma(link,"xpcomglue"); | |
30 } | |
31 } | |
32 | |
33 import tango.io.Console; | |
34 import dwt.dwthelper.utils; | |
35 | |
36 import dwt.DWT; | |
37 import dwt.DWTError; | |
38 import dwt.DWTException; | |
39 | |
40 import dwt.browser.Browser; | |
41 import dwt.browser.ProgressEvent; | |
42 import dwt.browser.ProgressListener; | |
43 import dwt.browser.StatusTextEvent; | |
44 import dwt.browser.StatusTextListener; | |
45 import dwt.browser.LocationEvent; | |
46 import dwt.browser.LocationListener; | |
47 | |
48 import dwt.layout.GridLayout; | |
49 import dwt.layout.GridData; | |
50 | |
51 import dwt.widgets.Shell; | |
52 import dwt.widgets.Display; | |
53 import dwt.widgets.ToolBar; | |
54 import dwt.widgets.ToolItem; | |
55 import dwt.widgets.Label; | |
56 import dwt.widgets.Text; | |
57 import dwt.widgets.ProgressBar; | |
58 import dwt.widgets.Listener; | |
59 import dwt.widgets.Event; | |
60 | |
61 public static void main(String [] args) { | |
62 Display display = new Display(); | |
63 final Shell shell = new Shell(display); | |
64 GridLayout gridLayout = new GridLayout(); | |
65 gridLayout.numColumns = 3; | |
66 shell.setLayout(gridLayout); | |
67 ToolBar toolbar = new ToolBar(shell, DWT.NONE); | |
68 ToolItem itemBack = new ToolItem(toolbar, DWT.PUSH); | |
69 itemBack.setText("Back"); | |
70 ToolItem itemForward = new ToolItem(toolbar, DWT.PUSH); | |
71 itemForward.setText("Forward"); | |
72 ToolItem itemStop = new ToolItem(toolbar, DWT.PUSH); | |
73 itemStop.setText("Stop"); | |
74 ToolItem itemRefresh = new ToolItem(toolbar, DWT.PUSH); | |
75 itemRefresh.setText("Refresh"); | |
76 ToolItem itemGo = new ToolItem(toolbar, DWT.PUSH); | |
77 itemGo.setText("Go"); | |
78 | |
79 GridData data = new GridData(); | |
80 data.horizontalSpan = 3; | |
81 toolbar.setLayoutData(data); | |
82 | |
83 Label labelAddress = new Label(shell, DWT.NONE); | |
84 labelAddress.setText("Address"); | |
85 | |
86 final Text location = new Text(shell, DWT.BORDER); | |
87 data = new GridData(); | |
88 data.horizontalAlignment = GridData.FILL; | |
89 data.horizontalSpan = 2; | |
90 data.grabExcessHorizontalSpace = true; | |
91 location.setLayoutData(data); | |
92 | |
93 Browser browser; | |
94 try { | |
95 browser = new Browser(shell, DWT.NONE); | |
96 } catch (DWTError e) { | |
97 Cout ("Could not instantiate Browser: " ~ e.getMessage()); | |
98 return; | |
99 } | |
100 data = new GridData(); | |
101 data.horizontalAlignment = GridData.FILL; | |
102 data.verticalAlignment = GridData.FILL; | |
103 data.horizontalSpan = 3; | |
104 data.grabExcessHorizontalSpace = true; | |
105 data.grabExcessVerticalSpace = true; | |
106 browser.setLayoutData(data); | |
107 | |
108 Label status = new Label(shell, DWT.NONE); | |
109 data = new GridData(GridData.FILL_HORIZONTAL); | |
110 data.horizontalSpan = 2; | |
111 status.setLayoutData(data); | |
112 | |
113 ProgressBar progressBar = new ProgressBar(shell, DWT.NONE); | |
114 data = new GridData(); | |
115 data.horizontalAlignment = GridData.END; | |
116 progressBar.setLayoutData(data); | |
117 | |
118 /* event handling */ | |
119 Listener listener = new class() Listener { | |
120 public void handleEvent(Event event) { | |
121 ToolItem item = cast(ToolItem)event.widget; | |
122 String string = item.getText(); | |
123 if (string.equals("Back")) browser.back(); | |
124 else if (string.equals("Forward")) browser.forward(); | |
125 else if (string.equals("Stop")) browser.stop(); | |
126 else if (string.equals("Refresh")) browser.refresh(); | |
127 else if (string.equals("Go")) browser.setUrl(location.getText()); | |
128 } | |
129 }; | |
130 browser.addProgressListener(new class() ProgressListener { | |
131 public void changed(ProgressEvent event) { | |
132 if (event.total == 0) return; | |
133 int ratio = event.current * 100 / event.total; | |
134 progressBar.setSelection(ratio); | |
135 } | |
136 public void completed(ProgressEvent event) { | |
137 progressBar.setSelection(0); | |
138 } | |
139 }); | |
140 browser.addStatusTextListener(new class() StatusTextListener { | |
141 public void changed(StatusTextEvent event) { | |
142 status.setText(event.text); | |
143 } | |
144 }); | |
145 browser.addLocationListener(new class() LocationListener { | |
146 public void changed(LocationEvent event) { | |
147 if (event.top) location.setText(event.location); | |
148 } | |
149 public void changing(LocationEvent event) { | |
150 } | |
151 }); | |
152 itemBack.addListener(DWT.Selection, listener); | |
153 itemForward.addListener(DWT.Selection, listener); | |
154 itemStop.addListener(DWT.Selection, listener); | |
155 itemRefresh.addListener(DWT.Selection, listener); | |
156 itemGo.addListener(DWT.Selection, listener); | |
157 location.addListener(DWT.DefaultSelection, new class() Listener { | |
158 public void handleEvent(Event e) { | |
159 browser.setUrl(location.getText()); | |
160 } | |
161 }); | |
162 | |
163 shell.open(); | |
164 browser.setUrl("http://eclipse.org"); | |
165 | |
166 while (!shell.isDisposed()) { | |
167 if (!display.readAndDispatch()) | |
168 display.sleep(); | |
169 } | |
170 display.dispose(); | |
171 } | |
172 |