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 * Michael Williamson (eclipse-bugs@magnaworks.com) - patch (see Bugzilla #92545)
|
|
11 *
|
|
12 * Port to the D programming language:
|
|
13 * Frank Benoit <benoit@tionex.de>
|
|
14 *******************************************************************************/
|
|
15 module dwtx.ui.forms.widgets.Section;
|
|
16
|
|
17 import dwtx.ui.forms.widgets.ExpandableComposite;
|
|
18 import dwtx.ui.forms.widgets.SharedScrolledComposite;
|
|
19
|
|
20 import dwt.DWT;
|
|
21 import dwt.events.PaintEvent;
|
|
22 import dwt.graphics.Color;
|
|
23 import dwt.graphics.GC;
|
|
24 import dwt.graphics.Image;
|
|
25 import dwt.graphics.Point;
|
|
26 import dwt.graphics.Rectangle;
|
|
27 import dwt.widgets.Composite;
|
|
28 import dwt.widgets.Control;
|
|
29 import dwt.widgets.Event;
|
|
30 import dwt.widgets.Listener;
|
|
31 import dwt.widgets.Text;
|
|
32 import dwtx.core.runtime.Assert;
|
|
33 import dwtx.ui.internal.forms.widgets.FormImages;
|
|
34 import dwtx.ui.internal.forms.widgets.FormUtil;
|
|
35
|
|
36 import dwt.dwthelper.utils;
|
|
37
|
|
38 import tango.util.collection.HashMap;
|
|
39
|
|
40 /**
|
|
41 * A variation of the expandable composite that adds optional description below
|
|
42 * the title. Section is often used as a basic building block in forms because
|
|
43 * it provides for logical grouping of information.
|
|
44 * <p>
|
|
45 * In case of the TITLE_BAR style, Section renders the title bar in a way
|
|
46 * compatible with the rest of the workbench. Since it is a widget, all the
|
|
47 * colors must be supplied directly. When created by the form toolkit, these
|
|
48 * colors are supplied by the toolkit. The toolkit initializes these colors
|
|
49 * based on the system colors. For this reason, it is recommended to create the
|
|
50 * section by the toolkit instead of through its own constructor.
|
|
51 * <p>
|
|
52 * Since 3.1, it is possible to set a control to be used for section
|
|
53 * description. If used, <code>DESCRIPTION</code> style should not be set. A
|
|
54 * typical way to take advantage of the new method is to set an instance of
|
|
55 * <code>FormText</code> to provide for hyperlinks and images in the
|
|
56 * description area.
|
|
57 *
|
|
58 * @since 3.0
|
|
59 */
|
|
60 public class Section : ExpandableComposite {
|
|
61 /**
|
|
62 * Description style. If used, description will be rendered below the title.
|
|
63 */
|
|
64 public static const int DESCRIPTION = 1 << 7;
|
|
65
|
|
66 private Control descriptionControl;
|
|
67
|
|
68 private Control separator;
|
|
69
|
|
70 private HashMap!(String,Object) titleColors;
|
|
71
|
|
72 private static const String COLOR_BG = "bg"; //$NON-NLS-1$
|
|
73
|
|
74 private static const String COLOR_GBG = "gbg"; //$NON-NLS-1$
|
|
75
|
|
76 private static const String COLOR_BORDER = "border"; //$NON-NLS-1$
|
|
77
|
|
78 /**
|
|
79 * Creates a new section instance in the provided parent.
|
|
80 *
|
|
81 * @param parent
|
|
82 * the parent composite
|
|
83 * @param style
|
|
84 * the style to use
|
|
85 */
|
|
86 public this(Composite parent, int style) {
|
|
87 this(parent, DWT.NULL, style);
|
|
88 }
|
|
89
|
|
90 this(Composite parent, int cstyle, int style) {
|
|
91 super(parent, cstyle | getBackgroundStyle(style), style);
|
|
92 int rtl = cstyle & DWT.RIGHT_TO_LEFT;
|
|
93 if ((style & DESCRIPTION) !is 0) {
|
|
94 descriptionControl = new Text(this, DWT.READ_ONLY | DWT.WRAP | rtl);
|
|
95 }
|
|
96 if ((style & TITLE_BAR) !is 0) {
|
|
97 Listener listener = new class Listener{
|
|
98 public void handleEvent(Event e) {
|
|
99 Image image = this.outer.callSuperGetBackgroundImage();
|
|
100 if (image !is null) {
|
|
101 FormImages.getInstance().markFinished(image);
|
|
102 }
|
|
103 this.outer.callSuperSetBackgroundImage(null);
|
|
104 }
|
|
105 };
|
|
106 addListener(DWT.Dispose, listener);
|
|
107 addListener(DWT.Resize, listener);
|
|
108 }
|
|
109 }
|
|
110
|
|
111 private static int getBackgroundStyle(int estyle) {
|
|
112 return ((estyle & TITLE_BAR) !is 0) ? DWT.NO_BACKGROUND : DWT.NULL;
|
|
113 }
|
|
114
|
|
115 private Image callSuperGetBackgroundImage(){
|
|
116 return super.getBackgroundImage();
|
|
117 }
|
|
118 private void callSuperSetBackgroundImage(Image image){
|
|
119 return super.setBackgroundImage(image);
|
|
120 }
|
|
121 protected void internalSetExpanded(bool expanded) {
|
|
122 super.internalSetExpanded(expanded);
|
|
123 if ((getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
124 if (!expanded)
|
|
125 super.setBackgroundImage(null);
|
|
126 }
|
|
127 reflow();
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * Reflows this section and all the parents up the hierarchy until a
|
|
132 * SharedScrolledComposite is reached.
|
|
133 */
|
|
134 protected void reflow() {
|
|
135 Composite c = this;
|
|
136 while (c !is null) {
|
|
137 c.setRedraw(false);
|
|
138 c = c.getParent();
|
|
139 if (null !is cast(SharedScrolledComposite)c ) {
|
|
140 break;
|
|
141 }
|
|
142 }
|
|
143 c = this;
|
|
144 while (c !is null) {
|
|
145 c.layout(true);
|
|
146 c = c.getParent();
|
|
147 if (null !is cast(SharedScrolledComposite)c ) {
|
|
148 (cast(SharedScrolledComposite) c).reflow(true);
|
|
149 break;
|
|
150 }
|
|
151 }
|
|
152 c = this;
|
|
153 while (c !is null) {
|
|
154 c.setRedraw(true);
|
|
155 c = c.getParent();
|
|
156 if (null !is cast(SharedScrolledComposite)c ) {
|
|
157 break;
|
|
158 }
|
|
159 }
|
|
160 }
|
|
161
|
|
162 /**
|
|
163 * Sets the description text. Has no effect if DESCRIPTION style was not
|
|
164 * used to create the control.
|
|
165 *
|
|
166 * @param description
|
|
167 */
|
|
168 public void setDescription(String description) {
|
|
169 if (null !is cast(Text)descriptionControl )
|
|
170 (cast(Text) descriptionControl).setText(description);
|
|
171 }
|
|
172
|
|
173 /**
|
|
174 * Returns the current description text.
|
|
175 *
|
|
176 * @return description text or <code>null</code> if DESCRIPTION style was
|
|
177 * not used to create the control.
|
|
178 */
|
|
179 public String getDescription() {
|
|
180 if (null !is cast(Text)descriptionControl )
|
|
181 return (cast(Text) descriptionControl).getText();
|
|
182 return null;
|
|
183 }
|
|
184
|
|
185 /**
|
|
186 * Sets the separator control of this section. The separator must not be
|
|
187 * <samp>null </samp> and must be a direct child of this container. If
|
|
188 * defined, separator will be placed below the title text and will remain
|
|
189 * visible regardless of the expansion state.
|
|
190 *
|
|
191 * @param separator
|
|
192 * the separator that will be placed below the title text.
|
|
193 */
|
|
194 public void setSeparatorControl(Control separator) {
|
|
195 Assert.isTrue(separator !is null && separator.getParent().opEquals(this));
|
|
196 this.separator = separator;
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Returns the control that is used as a separator betweeen the title and
|
|
201 * the client, or <samp>null </samp> if not set.
|
|
202 *
|
|
203 * @return separator control or <samp>null </samp> if not set.
|
|
204 */
|
|
205 public Control getSeparatorControl() {
|
|
206 return separator;
|
|
207 }
|
|
208
|
|
209 /**
|
|
210 * Sets the background of the section.
|
|
211 *
|
|
212 * @param bg
|
|
213 * the new background
|
|
214 */
|
|
215 public void setBackground(Color bg) {
|
|
216 super.setBackground(bg);
|
|
217 if (descriptionControl !is null
|
|
218 && (getExpansionStyle() & DESCRIPTION) !is 0)
|
|
219 descriptionControl.setBackground(bg);
|
|
220 }
|
|
221
|
|
222 /**
|
|
223 * Sets the foreground of the section.
|
|
224 *
|
|
225 * @param fg
|
|
226 * the new foreground.
|
|
227 */
|
|
228 public void setForeground(Color fg) {
|
|
229 super.setForeground(fg);
|
|
230 if (descriptionControl !is null
|
|
231 && (getExpansionStyle() & DESCRIPTION) !is 0)
|
|
232 descriptionControl.setForeground(fg);
|
|
233 }
|
|
234
|
|
235 /**
|
|
236 * Returns the control used to render the description. In 3.1, this method
|
|
237 * was promoted to public.
|
|
238 *
|
|
239 * @return description control or <code>null</code> if DESCRIPTION style
|
|
240 * was not used to create the control and description control was
|
|
241 * not set by the client.
|
|
242 * @see #setDescriptionControl(dwt.widgets.Control)
|
|
243 */
|
|
244 public Control getDescriptionControl() {
|
|
245 return descriptionControl;
|
|
246 }
|
|
247
|
|
248 /**
|
|
249 * Sets the description control of this section. The control must not be
|
|
250 * <samp>null</samp> and must be a direct child of this container. If
|
|
251 * defined, contol will be placed below the title text and the separator and
|
|
252 * will be hidden int he collapsed state.
|
|
253 * <p>
|
|
254 * This method and <code>DESCRIPTION</code> style are mutually exclusive.
|
|
255 * Use the method only if you want to create the description control
|
|
256 * yourself.
|
|
257 *
|
|
258 * @since 3.1
|
|
259 * @param descriptionControl
|
|
260 * the control that will be placed below the title text.
|
|
261 */
|
|
262 public void setDescriptionControl(Control descriptionControl) {
|
|
263 Assert.isTrue((getExpansionStyle() & DESCRIPTION) is 0);
|
|
264 Assert.isTrue(descriptionControl !is null
|
|
265 && descriptionControl.getParent().opEquals(this));
|
|
266 this.descriptionControl = descriptionControl;
|
|
267 }
|
|
268
|
|
269 /**
|
|
270 * Sets the color of the title bar border when TITLE_BAR style is used.
|
|
271 *
|
|
272 * @param color
|
|
273 * the title bar border color
|
|
274 */
|
|
275 public void setTitleBarBorderColor(Color color) {
|
|
276 putTitleBarColor(COLOR_BORDER, color);
|
|
277 }
|
|
278
|
|
279 /**
|
|
280 * Sets the color of the title bar background when TITLE_BAR style is used.
|
|
281 * This color is used as a starting color for the vertical gradient.
|
|
282 *
|
|
283 * @param color
|
|
284 * the title bar border background
|
|
285 */
|
|
286 public void setTitleBarBackground(Color color) {
|
|
287 putTitleBarColor(COLOR_BG, color);
|
|
288 }
|
|
289
|
|
290 /**
|
|
291 * Sets the color of the title bar gradient background when TITLE_BAR style
|
|
292 * is used. This color is used at the height where title controls end
|
|
293 * (toggle, tool bar).
|
|
294 *
|
|
295 * @param color
|
|
296 * the title bar gradient background
|
|
297 */
|
|
298 public void setTitleBarGradientBackground(Color color) {
|
|
299 putTitleBarColor(COLOR_GBG, color);
|
|
300 }
|
|
301
|
|
302 /**
|
|
303 * Returns the title bar border color when TITLE_BAR style is used.
|
|
304 *
|
|
305 * @return the title bar border color
|
|
306 */
|
|
307 public Color getTitleBarBorderColor() {
|
|
308 if (titleColors is null)
|
|
309 return null;
|
|
310 return cast(Color) titleColors.get(COLOR_BORDER);
|
|
311 }
|
|
312
|
|
313 /**
|
|
314 * Returns the title bar gradient background color when TITLE_BAR style is
|
|
315 * used.
|
|
316 *
|
|
317 * @return the title bar gradient background
|
|
318 */
|
|
319 public Color getTitleBarGradientBackground() {
|
|
320 if (titleColors is null)
|
|
321 return null;
|
|
322 if ((getExpansionStyle() & SHORT_TITLE_BAR) !is 0)
|
|
323 return getBackground();
|
|
324 return cast(Color) titleColors.get(COLOR_GBG);
|
|
325 }
|
|
326
|
|
327 /**
|
|
328 * Returns the title bar background when TITLE_BAR style is used.
|
|
329 *
|
|
330 * @return the title bar background
|
|
331 */
|
|
332 public Color getTitleBarBackground() {
|
|
333 if (titleColors is null)
|
|
334 return null;
|
|
335 return cast(Color) titleColors.get(COLOR_BG);
|
|
336 }
|
|
337
|
|
338 private void putTitleBarColor(String key, Color color) {
|
|
339 if (color is null)
|
|
340 return;
|
|
341 if (titleColors is null)
|
|
342 titleColors = new HashMap!(String,Object);
|
|
343 titleColors.add(key, color);
|
|
344 }
|
|
345
|
|
346 protected void onPaint(PaintEvent e) {
|
|
347 Color bg = null;
|
|
348 Color fg = null;
|
|
349 Color border = null;
|
|
350
|
|
351 GC gc = e.gc;
|
|
352 Image buffer = null;
|
|
353 Rectangle bounds = getClientArea();
|
|
354
|
|
355 if ((getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
356 buffer = new Image(getDisplay(), bounds.width, bounds.height);
|
|
357 buffer.setBackground(getBackground());
|
|
358 gc = new GC(buffer);
|
|
359 }
|
|
360 if (titleColors !is null) {
|
|
361 bg = cast(Color) titleColors.get(COLOR_BG);
|
|
362 fg = getTitleBarForeground();
|
|
363 border = cast(Color) titleColors.get(COLOR_BORDER);
|
|
364 }
|
|
365 if (bg is null)
|
|
366 bg = getBackground();
|
|
367 if (fg is null)
|
|
368 fg = getForeground();
|
|
369 if (border is null)
|
|
370 border = fg;
|
|
371 int theight = 0;
|
|
372 int gradientheight = 0;
|
|
373 int tvmargin = IGAP;
|
|
374 if ((getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
375 Point tsize = null;
|
|
376 Point tcsize = null;
|
|
377 if (toggle !is null)
|
|
378 tsize = toggle.getSize();
|
|
379 int twidth = bounds.width - marginWidth - marginWidth;
|
|
380 if (tsize !is null)
|
|
381 twidth -= tsize.x + IGAP;
|
|
382 if (getTextClient() !is null)
|
|
383 tcsize = getTextClient().getSize();
|
|
384 if (tcsize !is null)
|
|
385 twidth -= tcsize.x + IGAP;
|
|
386 Point size = textLabel.getSize();
|
|
387 if (tsize !is null)
|
|
388 theight += Math.max(theight, tsize.y);
|
|
389 gradientheight = theight;
|
|
390 if (tcsize !is null) {
|
|
391 theight = Math.max(theight, tcsize.y);
|
|
392 }
|
|
393 theight = Math.max(theight, size.y);
|
|
394 gradientheight = Math.max(gradientheight, size.y);
|
|
395 theight += tvmargin + tvmargin;
|
|
396 gradientheight += tvmargin + tvmargin;
|
|
397 } else {
|
|
398 theight = 5;
|
|
399 }
|
|
400 if ((getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
401 if (getBackgroundImage() is null)
|
|
402 updateHeaderImage(bg, bounds, gradientheight, theight);
|
|
403 gc.setBackground(getBackground());
|
|
404 gc.fillRectangle(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
405 drawBackground(gc, bounds.x, bounds.y, bounds.width, theight);
|
|
406 if (marginWidth > 0) {
|
|
407 // fix up margins
|
|
408 gc.setBackground(getBackground());
|
|
409 gc.fillRectangle(0, 0, marginWidth, theight);
|
|
410 gc.fillRectangle(bounds.x + bounds.width - marginWidth, 0,
|
|
411 marginWidth, theight);
|
|
412 }
|
|
413 } else if (isExpanded()) {
|
|
414 gc.setForeground(bg);
|
|
415 gc.setBackground(getBackground());
|
|
416 gc.fillGradientRectangle(marginWidth, marginHeight, bounds.width
|
|
417 - marginWidth - marginWidth, theight, true);
|
|
418 }
|
|
419 gc.setBackground(getBackground());
|
|
420 FormUtil.setAntialias(gc, DWT.ON);
|
|
421 // repair the upper left corner
|
|
422 gc.fillPolygon([ marginWidth, marginHeight, marginWidth,
|
|
423 marginHeight + 2, marginWidth + 2, marginHeight ]);
|
|
424 // repair the upper right corner
|
|
425 gc.fillPolygon([ bounds.width - marginWidth - 3,
|
|
426 marginHeight, bounds.width - marginWidth, marginHeight,
|
|
427 bounds.width - marginWidth, marginHeight + 3 ]);
|
|
428 gc.setForeground(border);
|
|
429 if (isExpanded() || (getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
430 // top left curve
|
|
431 gc.drawLine(marginWidth, marginHeight + 2, marginWidth + 2,
|
|
432 marginHeight);
|
|
433 // top edge
|
|
434 gc.drawLine(marginWidth + 2, marginHeight, bounds.width
|
|
435 - marginWidth - 3, marginHeight);
|
|
436 // top right curve
|
|
437 gc.drawLine(bounds.width - marginWidth - 3, marginHeight,
|
|
438 bounds.width - marginWidth - 1, marginHeight + 2);
|
|
439 } else {
|
|
440 // collapsed short title bar
|
|
441 // top edge
|
|
442 gc.drawLine(marginWidth, marginHeight, bounds.width - 1,
|
|
443 marginHeight);
|
|
444 }
|
|
445 if ((getExpansionStyle() & TITLE_BAR) !is 0 || isExpanded()) {
|
|
446 // left vertical edge gradient
|
|
447 gc.fillGradientRectangle(marginWidth, marginHeight + 2, 1,
|
|
448 gradientheight - 2, true);
|
|
449 // right vertical edge gradient
|
|
450 gc.fillGradientRectangle(bounds.width - marginWidth - 1,
|
|
451 marginHeight + 2, 1, gradientheight - 2, true);
|
|
452 }
|
|
453 if ((getExpansionStyle() & TITLE_BAR) !is 0) {
|
|
454 // New in 3.3 - edge treatmant
|
|
455 gc.setForeground(getDisplay().getSystemColor(DWT.COLOR_WHITE));
|
|
456 gc.drawPolyline([ marginWidth + 1,
|
|
457 marginHeight + gradientheight - 1, marginWidth + 1,
|
|
458 marginHeight + 2, marginWidth + 2, marginHeight + 2,
|
|
459 marginWidth + 2, marginHeight + 1,
|
|
460 bounds.width - marginWidth - 3, marginHeight + 1,
|
|
461 bounds.width - marginWidth - 3, marginHeight + 2,
|
|
462 bounds.width - marginWidth - 2, marginHeight + 2,
|
|
463 bounds.width - marginWidth - 2,
|
|
464 marginHeight + gradientheight - 1 ]);
|
|
465 }
|
|
466 if (buffer !is null) {
|
|
467 gc.dispose();
|
|
468 e.gc.drawImage(buffer, 0, 0);
|
|
469 buffer.dispose();
|
|
470 }
|
|
471 }
|
|
472
|
|
473 private void updateHeaderImage(Color bg, Rectangle bounds, int theight,
|
|
474 int realtheight) {
|
|
475 Image image = FormImages.getInstance().getGradient(getDisplay(), getBackground(), bg, realtheight, theight, marginHeight);
|
|
476 super.setBackgroundImage(image);
|
|
477 }
|
|
478
|
|
479 /**
|
|
480 * Background image is used for the title gradient - does nothing.
|
|
481 */
|
|
482 public final void setBackgroundImage(Image image) {
|
|
483 }
|
|
484 }
|