Mercurial > projects > dwt-addons
annotate dwtx/ui/internal/forms/widgets/FormUtil.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | 4ac9946b9fb5 |
children | 11e8159caf7a |
rev | line source |
---|---|
75 | 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 * Chriss Gross (schtoo@schtoo.com) - fix for 61670 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.ui.internal.forms.widgets.FormUtil; | |
15 | |
16 | |
90 | 17 // import com.ibm.icu.text.BreakIterator; |
18 | |
75 | 19 import dwt.DWT; |
20 import dwt.custom.ScrolledComposite; | |
21 import dwt.events.MouseEvent; | |
22 import dwt.graphics.Device; | |
23 import dwt.graphics.FontMetrics; | |
24 import dwt.graphics.GC; | |
25 import dwt.graphics.Image; | |
26 import dwt.graphics.ImageData; | |
27 import dwt.graphics.Point; | |
28 import dwt.graphics.Rectangle; | |
29 import dwt.layout.GridData; | |
30 import dwt.widgets.Combo; | |
31 import dwt.widgets.Composite; | |
32 import dwt.widgets.Control; | |
33 import dwt.widgets.Label; | |
34 import dwt.widgets.Layout; | |
35 import dwt.widgets.ScrollBar; | |
36 import dwt.widgets.Text; | |
37 import dwtx.ui.forms.widgets.ColumnLayout; | |
38 import dwtx.ui.forms.widgets.Form; | |
39 import dwtx.ui.forms.widgets.FormText; | |
40 import dwtx.ui.forms.widgets.FormToolkit; | |
41 import dwtx.ui.forms.widgets.ILayoutExtension; | |
42 | |
43 import dwt.dwthelper.utils; | |
44 | |
90 | 45 import dwtx.dwtxhelper.BreakIterator; |
46 // import mango.icu.UString; | |
47 // import mango.icu.ULocale; | |
48 // import mango.icu.UBreakIterator; | |
49 | |
75 | 50 public class FormUtil { |
51 | |
52 public static const String PLUGIN_ID = "dwtx.ui.forms"; //$NON-NLS-1$ | |
53 | |
54 static const int H_SCROLL_INCREMENT = 5; | |
55 | |
56 static const int V_SCROLL_INCREMENT = 64; | |
57 | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
58 public static const String DEBUG = PLUGIN_ID ~ "/debug"; //$NON-NLS-1$ |
75 | 59 |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
60 public static const String DEBUG_TEXT = DEBUG ~ "/text"; //$NON-NLS-1$ |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
61 public static const String DEBUG_TEXTSIZE = DEBUG ~ "/textsize"; //$NON-NLS-1$ |
75 | 62 |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
63 public static const String DEBUG_FOCUS = DEBUG ~ "/focus"; //$NON-NLS-1$ |
75 | 64 |
65 public static const String FOCUS_SCROLLING = "focusScrolling"; //$NON-NLS-1$ | |
66 | |
67 public static const String IGNORE_BODY = "__ignore_body__"; //$NON-NLS-1$ | |
68 | |
69 public static Text createText(Composite parent, String label, | |
70 FormToolkit factory) { | |
71 return createText(parent, label, factory, 1); | |
72 } | |
73 | |
74 public static Text createText(Composite parent, String label, | |
75 FormToolkit factory, int span) { | |
76 factory.createLabel(parent, label); | |
77 Text text = factory.createText(parent, ""); //$NON-NLS-1$ | |
78 int hfill = span is 1 ? GridData.FILL_HORIZONTAL | |
79 : GridData.HORIZONTAL_ALIGN_FILL; | |
80 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); | |
81 gd.horizontalSpan = span; | |
82 text.setLayoutData(gd); | |
83 return text; | |
84 } | |
85 | |
86 public static Text createText(Composite parent, String label, | |
87 FormToolkit factory, int span, int style) { | |
88 Label l = factory.createLabel(parent, label); | |
89 if ((style & DWT.MULTI) !is 0) { | |
90 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); | |
91 l.setLayoutData(gd); | |
92 } | |
93 Text text = factory.createText(parent, "", style); //$NON-NLS-1$ | |
94 int hfill = span is 1 ? GridData.FILL_HORIZONTAL | |
95 : GridData.HORIZONTAL_ALIGN_FILL; | |
96 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); | |
97 gd.horizontalSpan = span; | |
98 text.setLayoutData(gd); | |
99 return text; | |
100 } | |
101 | |
102 public static Text createText(Composite parent, FormToolkit factory, | |
103 int span) { | |
104 Text text = factory.createText(parent, ""); //$NON-NLS-1$ | |
105 int hfill = span is 1 ? GridData.FILL_HORIZONTAL | |
106 : GridData.HORIZONTAL_ALIGN_FILL; | |
107 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); | |
108 gd.horizontalSpan = span; | |
109 text.setLayoutData(gd); | |
110 return text; | |
111 } | |
112 | |
113 public static int computeMinimumWidth(GC gc, String text) { | |
90 | 114 auto wb = UBreakIterator.openWordIterator( ULocale.Default, text ); |
115 scope(exit) wb.close(); | |
75 | 116 int last = 0; |
117 int width = 0; | |
118 | |
90 | 119 for (int loc = wb.first(); loc !is UBreakIterator.DONE; loc = wb.next()) { |
75 | 120 String word = text.substring(last, loc); |
121 Point extent = gc.textExtent(word); | |
122 width = Math.max(width, extent.x); | |
123 last = loc; | |
124 } | |
125 String lastWord = text.substring(last); | |
126 Point extent = gc.textExtent(lastWord); | |
127 width = Math.max(width, extent.x); | |
128 return width; | |
129 } | |
130 | |
131 public static Point computeWrapSize(GC gc, String text, int wHint) { | |
90 | 132 auto wb = UBreakIterator.openWordIterator( ULocale.Default, text ); |
133 scope(exit) wb.close(); | |
75 | 134 FontMetrics fm = gc.getFontMetrics(); |
135 int lineHeight = fm.getHeight(); | |
136 | |
137 int saved = 0; | |
138 int last = 0; | |
139 int height = lineHeight; | |
140 int maxWidth = 0; | |
90 | 141 for (int loc = wb.first(); loc !is UBreakIterator.DONE; loc = wb.next()) { |
75 | 142 String word = text.substring(saved, loc); |
143 Point extent = gc.textExtent(word); | |
144 if (extent.x > wHint) { | |
145 // overflow | |
146 saved = last; | |
147 height += extent.y; | |
148 // switch to current word so maxWidth will accommodate very long single words | |
149 word = text.substring(last, loc); | |
150 extent = gc.textExtent(word); | |
151 } | |
152 maxWidth = Math.max(maxWidth, extent.x); | |
153 last = loc; | |
154 } | |
155 /* | |
156 * Correct the height attribute in case it was calculated wrong due to wHint being less than maxWidth. | |
157 * The recursive call proved to be the only thing that worked in all cases. Some attempts can be made | |
158 * to estimate the height, but the algorithm needs to be run again to be sure. | |
159 */ | |
160 if (maxWidth > wHint) | |
161 return computeWrapSize(gc, text, maxWidth); | |
162 return new Point(maxWidth, height); | |
163 } | |
164 | |
165 public static void paintWrapText(GC gc, String text, Rectangle bounds) { | |
166 paintWrapText(gc, text, bounds, false); | |
167 } | |
168 | |
169 public static void paintWrapText(GC gc, String text, Rectangle bounds, | |
170 bool underline) { | |
90 | 171 auto wb = UBreakIterator.openWordIterator( ULocale.Default, text ); |
172 scope(exit) wb.close(); | |
75 | 173 FontMetrics fm = gc.getFontMetrics(); |
174 int lineHeight = fm.getHeight(); | |
175 int descent = fm.getDescent(); | |
176 | |
177 int saved = 0; | |
178 int last = 0; | |
179 int y = bounds.y; | |
180 int width = bounds.width; | |
181 | |
90 | 182 for (int loc = wb.first(); loc !is UBreakIterator.DONE; loc = wb.next()) { |
75 | 183 String line = text.substring(saved, loc); |
184 Point extent = gc.textExtent(line); | |
185 | |
186 if (extent.x > width) { | |
187 // overflow | |
188 String prevLine = text.substring(saved, last); | |
189 gc.drawText(prevLine, bounds.x, y, true); | |
190 if (underline) { | |
191 Point prevExtent = gc.textExtent(prevLine); | |
192 int lineY = y + lineHeight - descent + 1; | |
193 gc | |
194 .drawLine(bounds.x, lineY, bounds.x + prevExtent.x, | |
195 lineY); | |
196 } | |
197 | |
198 saved = last; | |
199 y += lineHeight; | |
200 } | |
201 last = loc; | |
202 } | |
203 // paint the last line | |
204 String lastLine = text.substring(saved, last); | |
205 gc.drawText(lastLine, bounds.x, y, true); | |
206 if (underline) { | |
207 int lineY = y + lineHeight - descent + 1; | |
208 Point lastExtent = gc.textExtent(lastLine); | |
209 gc.drawLine(bounds.x, lineY, bounds.x + lastExtent.x, lineY); | |
210 } | |
211 } | |
212 | |
213 public static ScrolledComposite getScrolledComposite(Control c) { | |
214 Composite parent = c.getParent(); | |
215 | |
216 while (parent !is null) { | |
217 if ( auto sc = cast(ScrolledComposite)parent ) { | |
218 return sc; | |
219 } | |
220 parent = parent.getParent(); | |
221 } | |
222 return null; | |
223 } | |
224 | |
225 public static void ensureVisible(Control c) { | |
226 ScrolledComposite scomp = getScrolledComposite(c); | |
227 if (scomp !is null) { | |
228 Object data = scomp.getData(FOCUS_SCROLLING); | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
229 if (data is null || !data.opEquals(Boolean.FALSE)) |
75 | 230 FormUtil.ensureVisible(scomp, c); |
231 } | |
232 } | |
233 | |
234 public static void ensureVisible(ScrolledComposite scomp, Control control) { | |
235 // if the control is a FormText we do not need to scroll since it will | |
236 // ensure visibility of its segments as necessary | |
237 if ( auto ft = cast(FormText)control ) | |
238 return; | |
239 Point controlSize = control.getSize(); | |
240 Point controlOrigin = getControlLocation(scomp, control); | |
241 ensureVisible(scomp, controlOrigin, controlSize); | |
242 } | |
243 | |
244 public static void ensureVisible(ScrolledComposite scomp, | |
245 Point controlOrigin, Point controlSize) { | |
246 Rectangle area = scomp.getClientArea(); | |
247 Point scompOrigin = scomp.getOrigin(); | |
248 | |
249 int x = scompOrigin.x; | |
250 int y = scompOrigin.y; | |
251 | |
252 // horizontal right, but only if the control is smaller | |
253 // than the client area | |
254 if (controlSize.x < area.width | |
255 && (controlOrigin.x + controlSize.x > scompOrigin.x | |
256 + area.width)) { | |
257 x = controlOrigin.x + controlSize.x - area.width; | |
258 } | |
259 // horizontal left - make sure the left edge of | |
260 // the control is showing | |
261 if (controlOrigin.x < x) { | |
262 if (controlSize.x < area.width) | |
263 x = controlOrigin.x + controlSize.x - area.width; | |
264 else | |
265 x = controlOrigin.x; | |
266 } | |
267 // vertical bottom | |
268 if (controlSize.y < area.height | |
269 && (controlOrigin.y + controlSize.y > scompOrigin.y | |
270 + area.height)) { | |
271 y = controlOrigin.y + controlSize.y - area.height; | |
272 } | |
273 // vertical top - make sure the top of | |
274 // the control is showing | |
275 if (controlOrigin.y < y) { | |
276 if (controlSize.y < area.height) | |
277 y = controlOrigin.y + controlSize.y - area.height; | |
278 else | |
279 y = controlOrigin.y; | |
280 } | |
281 | |
282 if (scompOrigin.x !is x || scompOrigin.y !is y) { | |
283 // scroll to reveal | |
284 scomp.setOrigin(x, y); | |
285 } | |
286 } | |
287 | |
288 public static void ensureVisible(ScrolledComposite scomp, Control control, | |
289 MouseEvent e) { | |
290 Point controlOrigin = getControlLocation(scomp, control); | |
291 int rX = controlOrigin.x + e.x; | |
292 int rY = controlOrigin.y + e.y; | |
293 Rectangle area = scomp.getClientArea(); | |
294 Point scompOrigin = scomp.getOrigin(); | |
295 | |
296 int x = scompOrigin.x; | |
297 int y = scompOrigin.y; | |
298 // System.out.println("Ensure: area="+area+", origin="+scompOrigin+", | |
299 // cloc="+controlOrigin+", csize="+controlSize+", x="+x+", y="+y); | |
300 | |
301 // horizontal right | |
302 if (rX > scompOrigin.x + area.width) { | |
303 x = rX - area.width; | |
304 } | |
305 // horizontal left | |
306 else if (rX < x) { | |
307 x = rX; | |
308 } | |
309 // vertical bottom | |
310 if (rY > scompOrigin.y + area.height) { | |
311 y = rY - area.height; | |
312 } | |
313 // vertical top | |
314 else if (rY < y) { | |
315 y = rY; | |
316 } | |
317 | |
318 if (scompOrigin.x !is x || scompOrigin.y !is y) { | |
319 // scroll to reveal | |
320 scomp.setOrigin(x, y); | |
321 } | |
322 } | |
323 | |
324 public static Point getControlLocation(ScrolledComposite scomp, | |
325 Control control) { | |
326 int x = 0; | |
327 int y = 0; | |
328 Control content = scomp.getContent(); | |
329 Control currentControl = control; | |
330 for (;;) { | |
331 if (currentControl is content) | |
332 break; | |
333 Point location = currentControl.getLocation(); | |
334 // if (location.x > 0) | |
335 // x += location.x; | |
336 // if (location.y > 0) | |
337 // y += location.y; | |
338 x += location.x; | |
339 y += location.y; | |
340 currentControl = currentControl.getParent(); | |
341 } | |
342 return new Point(x, y); | |
343 } | |
344 | |
345 static void scrollVertical(ScrolledComposite scomp, bool up) { | |
346 scroll(scomp, 0, up ? -V_SCROLL_INCREMENT : V_SCROLL_INCREMENT); | |
347 } | |
348 | |
349 static void scrollHorizontal(ScrolledComposite scomp, bool left) { | |
350 scroll(scomp, left ? -H_SCROLL_INCREMENT : H_SCROLL_INCREMENT, 0); | |
351 } | |
352 | |
353 static void scrollPage(ScrolledComposite scomp, bool up) { | |
354 Rectangle clientArea = scomp.getClientArea(); | |
355 int increment = up ? -clientArea.height : clientArea.height; | |
356 scroll(scomp, 0, increment); | |
357 } | |
358 | |
359 static void scroll(ScrolledComposite scomp, int xoffset, int yoffset) { | |
360 Point origin = scomp.getOrigin(); | |
361 Point contentSize = scomp.getContent().getSize(); | |
362 int xorigin = origin.x + xoffset; | |
363 int yorigin = origin.y + yoffset; | |
364 xorigin = Math.max(xorigin, 0); | |
365 xorigin = Math.min(xorigin, contentSize.x - 1); | |
366 yorigin = Math.max(yorigin, 0); | |
367 yorigin = Math.min(yorigin, contentSize.y - 1); | |
368 scomp.setOrigin(xorigin, yorigin); | |
369 } | |
370 | |
371 public static void updatePageIncrement(ScrolledComposite scomp) { | |
372 ScrollBar vbar = scomp.getVerticalBar(); | |
373 if (vbar !is null) { | |
374 Rectangle clientArea = scomp.getClientArea(); | |
375 int increment = clientArea.height - 5; | |
376 vbar.setPageIncrement(increment); | |
377 } | |
378 ScrollBar hbar = scomp.getHorizontalBar(); | |
379 if (hbar !is null) { | |
380 Rectangle clientArea = scomp.getClientArea(); | |
381 int increment = clientArea.width - 5; | |
382 hbar.setPageIncrement(increment); | |
383 } | |
384 } | |
385 | |
386 public static void processKey(int keyCode, Control c) { | |
387 ScrolledComposite scomp = FormUtil.getScrolledComposite(c); | |
388 if (scomp !is null) { | |
389 if (null !is cast(Combo)c ) | |
390 return; | |
391 switch (keyCode) { | |
392 case DWT.ARROW_DOWN: | |
393 if (scomp.getData("novarrows") is null) //$NON-NLS-1$ | |
394 FormUtil.scrollVertical(scomp, false); | |
395 break; | |
396 case DWT.ARROW_UP: | |
397 if (scomp.getData("novarrows") is null) //$NON-NLS-1$ | |
398 FormUtil.scrollVertical(scomp, true); | |
399 break; | |
400 case DWT.ARROW_LEFT: | |
401 FormUtil.scrollHorizontal(scomp, true); | |
402 break; | |
403 case DWT.ARROW_RIGHT: | |
404 FormUtil.scrollHorizontal(scomp, false); | |
405 break; | |
406 case DWT.PAGE_UP: | |
407 FormUtil.scrollPage(scomp, true); | |
408 break; | |
409 case DWT.PAGE_DOWN: | |
410 FormUtil.scrollPage(scomp, false); | |
411 break; | |
78 | 412 default: |
75 | 413 } |
414 } | |
415 } | |
416 | |
417 public static bool isWrapControl(Control c) { | |
418 if ((c.getStyle() & DWT.WRAP) !is 0) | |
419 return true; | |
420 if (auto comp = cast(Composite)c ) { | |
421 return ( null !is cast(ILayoutExtension)( comp.getLayout() )); | |
422 } | |
423 return false; | |
424 } | |
425 | |
426 public static int getWidthHint(int wHint, Control c) { | |
427 bool wrap = isWrapControl(c); | |
428 return wrap ? wHint : DWT.DEFAULT; | |
429 } | |
430 | |
431 public static int getHeightHint(int hHint, Control c) { | |
432 if ( auto comp = cast(Composite)c ) { | |
433 Layout layout = comp.getLayout(); | |
434 if (null !is cast(ColumnLayout)layout ) | |
435 return hHint; | |
436 } | |
437 return DWT.DEFAULT; | |
438 } | |
439 | |
440 public static int computeMinimumWidth(Control c, bool changed) { | |
441 if ( auto comp = cast(Composite)c ) { | |
442 Layout layout = comp.getLayout(); | |
443 if ( auto le = cast(ILayoutExtension)layout ) | |
444 return le.computeMinimumWidth( | |
445 comp, changed); | |
446 } | |
447 return c.computeSize(FormUtil.getWidthHint(5, c), DWT.DEFAULT, changed).x; | |
448 } | |
449 | |
450 public static int computeMaximumWidth(Control c, bool changed) { | |
451 if ( auto comp = cast(Composite)c ) { | |
452 Layout layout = comp.getLayout(); | |
453 if ( auto le = cast(ILayoutExtension)layout ) | |
454 return le.computeMaximumWidth( | |
455 comp, changed); | |
456 } | |
457 return c.computeSize(DWT.DEFAULT, DWT.DEFAULT, changed).x; | |
458 } | |
459 | |
460 public static Form getForm(Control c) { | |
461 Composite parent = c.getParent(); | |
462 while (parent !is null) { | |
463 if ( auto frm = cast(Form)parent ) { | |
464 return frm; | |
465 } | |
466 parent = parent.getParent(); | |
467 } | |
468 return null; | |
469 } | |
470 | |
471 public static Image createAlphaMashImage(Device device, Image srcImage) { | |
472 Rectangle bounds = srcImage.getBounds(); | |
473 int alpha = 0; | |
474 int calpha = 0; | |
475 ImageData data = srcImage.getImageData(); | |
476 // Create a new image with alpha values alternating | |
477 // between fully transparent (0) and fully opaque (255). | |
478 // This image will show the background through the | |
479 // transparent pixels. | |
480 for (int i = 0; i < bounds.height; i++) { | |
481 // scan line | |
482 alpha = calpha; | |
483 for (int j = 0; j < bounds.width; j++) { | |
484 // column | |
485 data.setAlpha(j, i, alpha); | |
486 alpha = alpha is 255 ? 0 : 255; | |
487 } | |
488 calpha = calpha is 255 ? 0 : 255; | |
489 } | |
490 return new Image(device, data); | |
491 } | |
492 | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
493 public static bool mnemonicMatch(String text, dchar key) { |
75 | 494 char mnemonic = findMnemonic(text); |
495 if (mnemonic is '\0') | |
496 return false; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
497 return CharacterToUpper(key) is CharacterToUpper(mnemonic); |
75 | 498 } |
499 | |
500 private static char findMnemonic(String string) { | |
501 int index = 0; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
502 int length = string.length; |
75 | 503 do { |
504 while (index < length && string.charAt(index) !is '&') | |
505 index++; | |
506 if (++index >= length) | |
507 return '\0'; | |
508 if (string.charAt(index) !is '&') | |
509 return string.charAt(index); | |
510 index++; | |
511 } while (index < length); | |
512 return '\0'; | |
513 } | |
514 | |
515 public static void setFocusScrollingEnabled(Control c, bool enabled) { | |
516 ScrolledComposite scomp = null; | |
517 | |
518 if ( auto sc = cast(ScrolledComposite)c ) | |
519 scomp = sc; | |
520 else | |
521 scomp = getScrolledComposite(c); | |
522 if (scomp !is null) | |
523 scomp.setData(FormUtil.FOCUS_SCROLLING, enabled ? null : Boolean.FALSE); | |
524 } | |
525 | |
526 public static void setAntialias(GC gc, int style) { | |
527 if (!gc.getAdvanced()) { | |
528 gc.setAdvanced(true); | |
529 if (!gc.getAdvanced()) | |
530 return; | |
531 } | |
532 gc.setAntialias(style); | |
533 } | |
534 } |