78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module examples.controlexample.StyledTextTab;
|
|
14
|
|
15
|
|
16 import dwt.dwthelper.ByteArrayInputStream;
|
|
17
|
|
18
|
|
19 import dwt.DWT;
|
|
20 import dwt.custom.StyleRange;
|
|
21 import dwt.custom.StyledText;
|
|
22 import dwt.events.ControlAdapter;
|
|
23 import dwt.events.ControlEvent;
|
|
24 import dwt.events.DisposeEvent;
|
|
25 import dwt.events.DisposeListener;
|
|
26 import dwt.events.SelectionAdapter;
|
|
27 import dwt.events.SelectionEvent;
|
|
28 import dwt.events.SelectionListener;
|
|
29 import dwt.graphics.Color;
|
|
30 import dwt.graphics.Image;
|
|
31 import dwt.graphics.ImageData;
|
|
32 import dwt.graphics.Point;
|
|
33 import dwt.layout.GridData;
|
|
34 import dwt.layout.GridLayout;
|
|
35 import dwt.widgets.Button;
|
|
36 import dwt.widgets.Composite;
|
|
37 import dwt.widgets.Display;
|
|
38 import dwt.widgets.Group;
|
|
39 import dwt.widgets.Label;
|
|
40 import dwt.widgets.TabFolder;
|
|
41 import dwt.widgets.Widget;
|
|
42
|
|
43 import examples.controlexample.ScrollableTab;
|
|
44 import examples.controlexample.ControlExample;
|
|
45 import tango.core.Exception;
|
|
46 import tango.io.Stdout;
|
|
47
|
|
48 class StyledTextTab : ScrollableTab {
|
|
49 /* Example widgets and groups that contain them */
|
|
50 StyledText styledText;
|
|
51 Group styledTextGroup, styledTextStyleGroup;
|
|
52
|
|
53 /* Style widgets added to the "Style" group */
|
|
54 Button wrapButton, readOnlyButton, fullSelectionButton;
|
|
55
|
|
56 /* Buttons for adding StyleRanges to StyledText */
|
|
57 Button boldButton, italicButton, redButton, yellowButton, underlineButton, strikeoutButton;
|
|
58 Image boldImage, italicImage, redImage, yellowImage, underlineImage, strikeoutImage;
|
|
59
|
|
60 /* Variables for saving state. */
|
|
61 char[] text;
|
|
62 StyleRange[] styleRanges;
|
|
63
|
|
64 /**
|
|
65 * Creates the Tab within a given instance of ControlExample.
|
|
66 */
|
|
67 this(ControlExample instance) {
|
|
68 super(instance);
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Creates a bitmap image.
|
|
73 */
|
|
74 Image createBitmapImage (Display display, void[] sourceData, void[] maskData ) {
|
|
75 InputStream sourceStream = new ByteArrayInputStream ( cast(byte[])sourceData );
|
|
76 InputStream maskStream = new ByteArrayInputStream ( cast(byte[])maskData );
|
|
77 ImageData source = new ImageData (sourceStream);
|
|
78 ImageData mask = new ImageData (maskStream);
|
|
79 Image result = new Image (display, source, mask);
|
|
80 try {
|
|
81 sourceStream.close ();
|
|
82 maskStream.close ();
|
|
83 } catch (IOException e) {
|
|
84 Stderr.formatln( "Stacktrace: {}", e );
|
|
85 }
|
|
86 return result;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Creates the "Control" widget children.
|
|
91 */
|
|
92 void createControlWidgets () {
|
|
93 super.createControlWidgets ();
|
|
94
|
|
95 /* Add a group for modifying the StyledText widget */
|
|
96 createStyledTextStyleGroup ();
|
|
97 }
|
|
98
|
|
99 /**
|
|
100 * Creates the "Example" group.
|
|
101 */
|
|
102 void createExampleGroup () {
|
|
103 super.createExampleGroup ();
|
|
104
|
|
105 /* Create a group for the styled text widget */
|
|
106 styledTextGroup = new Group (exampleGroup, DWT.NONE);
|
|
107 styledTextGroup.setLayout (new GridLayout ());
|
|
108 styledTextGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true));
|
|
109 styledTextGroup.setText ("StyledText");
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Creates the "Example" widgets.
|
|
114 */
|
|
115 void createExampleWidgets () {
|
|
116
|
|
117 /* Compute the widget style */
|
|
118 int style = getDefaultStyle();
|
|
119 if (singleButton.getSelection ()) style |= DWT.SINGLE;
|
|
120 if (multiButton.getSelection ()) style |= DWT.MULTI;
|
|
121 if (horizontalButton.getSelection ()) style |= DWT.H_SCROLL;
|
|
122 if (verticalButton.getSelection ()) style |= DWT.V_SCROLL;
|
|
123 if (wrapButton.getSelection ()) style |= DWT.WRAP;
|
|
124 if (readOnlyButton.getSelection ()) style |= DWT.READ_ONLY;
|
|
125 if (borderButton.getSelection ()) style |= DWT.BORDER;
|
|
126 if (fullSelectionButton.getSelection ()) style |= DWT.FULL_SELECTION;
|
|
127
|
|
128 /* Create the example widgets */
|
|
129 styledText = new StyledText (styledTextGroup, style);
|
|
130 styledText.setText (ControlExample.getResourceString("Example_string"));
|
|
131 styledText.append ("\n");
|
|
132 styledText.append (ControlExample.getResourceString("One_Two_Three"));
|
|
133
|
|
134 if (text !is null) {
|
|
135 styledText.setText(text);
|
|
136 text = null;
|
|
137 }
|
|
138 if (styleRanges !is null) {
|
|
139 styledText.setStyleRanges(styleRanges);
|
|
140 styleRanges = null;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 /**
|
|
145 * Creates the "Style" group.
|
|
146 */
|
|
147 void createStyleGroup() {
|
|
148 super.createStyleGroup();
|
|
149
|
|
150 /* Create the extra widgets */
|
|
151 wrapButton = new Button (styleGroup, DWT.CHECK);
|
|
152 wrapButton.setText ("DWT.WRAP");
|
|
153 readOnlyButton = new Button (styleGroup, DWT.CHECK);
|
|
154 readOnlyButton.setText ("DWT.READ_ONLY");
|
|
155 fullSelectionButton = new Button (styleGroup, DWT.CHECK);
|
|
156 fullSelectionButton.setText ("DWT.FULL_SELECTION");
|
|
157 }
|
|
158
|
|
159 /**
|
|
160 * Creates the "StyledText Style" group.
|
|
161 */
|
|
162 void createStyledTextStyleGroup () {
|
|
163 styledTextStyleGroup = new Group (controlGroup, DWT.NONE);
|
|
164 styledTextStyleGroup.setText (ControlExample.getResourceString ("StyledText_Styles"));
|
|
165 styledTextStyleGroup.setLayout (new GridLayout(6, false));
|
|
166 GridData data = new GridData (GridData.HORIZONTAL_ALIGN_FILL);
|
|
167 data.horizontalSpan = 2;
|
|
168 styledTextStyleGroup.setLayoutData (data);
|
|
169
|
|
170 /* Get images */
|
80
|
171 boldImage = createBitmapImage (display, import("examples.controlexample.bold.bmp"), import("examples.controlexample.bold_mask.bmp"));
|
|
172 italicImage = createBitmapImage (display, import("examples.controlexample.italic.bmp"), import("examples.controlexample.italic_mask.bmp"));
|
|
173 redImage = createBitmapImage (display, import("examples.controlexample.red.bmp"), import("examples.controlexample.red_mask.bmp"));
|
|
174 yellowImage = createBitmapImage (display, import("examples.controlexample.yellow.bmp"), import("examples.controlexample.yellow_mask.bmp"));
|
|
175 underlineImage = createBitmapImage (display, import("examples.controlexample.underline.bmp"), import("examples.controlexample.underline_mask.bmp"));
|
|
176 strikeoutImage = createBitmapImage (display, import("examples.controlexample.strikeout.bmp"), import("examples.controlexample.strikeout_mask.bmp"));
|
78
|
177
|
|
178 /* Create controls to modify the StyledText */
|
|
179 Label label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
180 label.setText (ControlExample.getResourceString ("StyledText_Style_Instructions"));
|
|
181 label.setLayoutData(new GridData(DWT.FILL, DWT.CENTER, false, false, 6, 1));
|
|
182 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
183 label.setText (ControlExample.getResourceString ("Bold"));
|
|
184 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
185 boldButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
186 boldButton.setImage (boldImage);
|
|
187 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
188 label.setText (ControlExample.getResourceString ("Underline"));
|
|
189 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
190 underlineButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
191 underlineButton.setImage (underlineImage);
|
|
192 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
193 label.setText (ControlExample.getResourceString ("Foreground_Style"));
|
|
194 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
195 redButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
196 redButton.setImage (redImage);
|
|
197 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
198 label.setText (ControlExample.getResourceString ("Italic"));
|
|
199 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
200 italicButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
201 italicButton.setImage (italicImage);
|
|
202 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
203 label.setText (ControlExample.getResourceString ("Strikeout"));
|
|
204 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
205 strikeoutButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
206 strikeoutButton.setImage (strikeoutImage);
|
|
207 label = new Label (styledTextStyleGroup, DWT.NONE);
|
|
208 label.setText (ControlExample.getResourceString ("Background_Style"));
|
|
209 label.setLayoutData(new GridData(DWT.END, DWT.CENTER, true, false));
|
|
210 yellowButton = new Button (styledTextStyleGroup, DWT.PUSH);
|
|
211 yellowButton.setImage (yellowImage);
|
|
212 SelectionListener styleListener = new class() SelectionAdapter {
|
|
213 public void widgetSelected (SelectionEvent e) {
|
|
214 Point sel = styledText.getSelectionRange();
|
|
215 if ((sel is null) || (sel.y is 0)) return;
|
|
216 StyleRange style;
|
|
217 for (int i = sel.x; i<sel.x+sel.y; i++) {
|
|
218 StyleRange range = styledText.getStyleRangeAtOffset(i);
|
|
219 if (range !is null) {
|
|
220 style = cast(StyleRange)range.clone();
|
|
221 style.start = i;
|
|
222 style.length = 1;
|
|
223 } else {
|
|
224 style = new StyleRange(i, 1, null, null, DWT.NORMAL);
|
|
225 }
|
|
226 if (e.widget is boldButton) {
|
|
227 style.fontStyle ^= DWT.BOLD;
|
|
228 } else if (e.widget is italicButton) {
|
|
229 style.fontStyle ^= DWT.ITALIC;
|
|
230 } else if (e.widget is underlineButton) {
|
|
231 style.underline = !style.underline;
|
|
232 } else if (e.widget is strikeoutButton) {
|
|
233 style.strikeout = !style.strikeout;
|
|
234 }
|
|
235 styledText.setStyleRange(style);
|
|
236 }
|
|
237 styledText.setSelectionRange(sel.x + sel.y, 0);
|
|
238 }
|
|
239 };
|
|
240 SelectionListener colorListener = new class() SelectionAdapter {
|
|
241 public void widgetSelected (SelectionEvent e) {
|
|
242 Point sel = styledText.getSelectionRange();
|
|
243 if ((sel is null) || (sel.y is 0)) return;
|
|
244 Color fg = null, bg = null;
|
|
245 if (e.widget is redButton) {
|
|
246 fg = display.getSystemColor (DWT.COLOR_RED);
|
|
247 } else if (e.widget is yellowButton) {
|
|
248 bg = display.getSystemColor (DWT.COLOR_YELLOW);
|
|
249 }
|
|
250 StyleRange style;
|
|
251 for (int i = sel.x; i<sel.x+sel.y; i++) {
|
|
252 StyleRange range = styledText.getStyleRangeAtOffset(i);
|
|
253 if (range !is null) {
|
|
254 style = cast(StyleRange)range.clone();
|
|
255 style.start = i;
|
|
256 style.length = 1;
|
|
257 style.foreground = style.foreground !is null ? null : fg;
|
|
258 style.background = style.background !is null ? null : bg;
|
|
259 } else {
|
|
260 style = new StyleRange (i, 1, fg, bg, DWT.NORMAL);
|
|
261 }
|
|
262 styledText.setStyleRange(style);
|
|
263 }
|
|
264 styledText.setSelectionRange(sel.x + sel.y, 0);
|
|
265 }
|
|
266 };
|
|
267 boldButton.addSelectionListener(styleListener);
|
|
268 italicButton.addSelectionListener(styleListener);
|
|
269 underlineButton.addSelectionListener(styleListener);
|
|
270 strikeoutButton.addSelectionListener(styleListener);
|
|
271 redButton.addSelectionListener(colorListener);
|
|
272 yellowButton.addSelectionListener(colorListener);
|
|
273 yellowButton.addDisposeListener(new class() DisposeListener {
|
|
274 public void widgetDisposed (DisposeEvent e) {
|
|
275 boldImage.dispose();
|
|
276 italicImage.dispose();
|
|
277 redImage.dispose();
|
|
278 yellowImage.dispose();
|
|
279 underlineImage.dispose();
|
|
280 strikeoutImage.dispose();
|
|
281 }
|
|
282 });
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Creates the tab folder page.
|
|
287 *
|
|
288 * @param tabFolder org.eclipse.swt.widgets.TabFolder
|
|
289 * @return the new page for the tab folder
|
|
290 */
|
|
291 Composite createTabFolderPage (TabFolder tabFolder) {
|
|
292 super.createTabFolderPage (tabFolder);
|
|
293
|
|
294 /*
|
|
295 * Add a resize listener to the tabFolderPage so that
|
|
296 * if the user types into the example widget to change
|
|
297 * its preferred size, and then resizes the shell, we
|
|
298 * recalculate the preferred size correctly.
|
|
299 */
|
|
300 tabFolderPage.addControlListener(new class() ControlAdapter {
|
|
301 public void controlResized(ControlEvent e) {
|
|
302 setExampleWidgetSize ();
|
|
303 }
|
|
304 });
|
|
305
|
|
306 return tabFolderPage;
|
|
307 }
|
|
308
|
|
309 /**
|
|
310 * Disposes the "Example" widgets.
|
|
311 */
|
|
312 void disposeExampleWidgets () {
|
|
313 /* store the state of the styledText if applicable */
|
|
314 if (styledText !is null) {
|
|
315 styleRanges = styledText.getStyleRanges();
|
|
316 text = styledText.getText();
|
|
317 }
|
|
318 super.disposeExampleWidgets();
|
|
319 }
|
|
320
|
|
321 /**
|
|
322 * Gets the "Example" widget children.
|
|
323 */
|
|
324 Widget [] getExampleWidgets () {
|
|
325 return [ cast(Widget) styledText];
|
|
326 }
|
|
327
|
|
328 /**
|
|
329 * Returns a list of set/get API method names (without the set/get prefix)
|
|
330 * that can be used to set/get values in the example control(s).
|
|
331 */
|
|
332 char[][] getMethodNames() {
|
|
333 return ["CaretOffset", "DoubleClickEnabled", "Editable", "HorizontalIndex", "HorizontalPixel", "Orientation", "Selection", "Tabs", "Text", "TextLimit", "ToolTipText", "TopIndex", "TopPixel", "WordWrap"];
|
|
334 }
|
|
335
|
|
336
|
|
337 /**
|
|
338 * Gets the text for the tab folder item.
|
|
339 */
|
|
340 char[] getTabText () {
|
|
341 return "StyledText";
|
|
342 }
|
|
343
|
|
344 /**
|
|
345 * Sets the state of the "Example" widgets.
|
|
346 */
|
|
347 void setExampleWidgetState () {
|
|
348 super.setExampleWidgetState ();
|
|
349 wrapButton.setSelection ((styledText.getStyle () & DWT.WRAP) !is 0);
|
|
350 readOnlyButton.setSelection ((styledText.getStyle () & DWT.READ_ONLY) !is 0);
|
|
351 fullSelectionButton.setSelection ((styledText.getStyle () & DWT.FULL_SELECTION) !is 0);
|
|
352 horizontalButton.setEnabled ((styledText.getStyle () & DWT.MULTI) !is 0);
|
|
353 verticalButton.setEnabled ((styledText.getStyle () & DWT.MULTI) !is 0);
|
|
354 wrapButton.setEnabled ((styledText.getStyle () & DWT.MULTI) !is 0);
|
|
355 }
|
|
356 }
|