comparison org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormUtil.d @ 12:bc29606a740c

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