comparison dwtx/ui/internal/forms/widgets/FormUtil.d @ 75:5d489b9f966c

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