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