root/dwt/widgets/Group.d

Revision 249:b3dbd786541a, 20.7 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Fix: compile errors

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.widgets.Group;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.graphics.Font;
19 import dwt.graphics.GC;
20 import dwt.graphics.Point;
21 import dwt.graphics.Rectangle;
22 import dwt.internal.win32.OS;
23 import dwt.widgets.Composite;
24 import dwt.widgets.Control;
25
26 import dwt.dwthelper.utils;
27
28 /**
29  * Instances of this class provide an etched border
30  * with an optional title.
31  * <p>
32  * Shadow styles are hints and may not be honoured
33  * by the platform.  To create a group with the
34  * default shadow style for the platform, do not
35  * specify a shadow style.
36  * <dl>
37  * <dt><b>Styles:</b></dt>
38  * <dd>SHADOW_ETCHED_IN, SHADOW_ETCHED_OUT, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
39  * <dt><b>Events:</b></dt>
40  * <dd>(none)</dd>
41  * </dl>
42  * <p>
43  * Note: Only one of the above styles may be specified.
44  * </p><p>
45  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
46  * </p>
47  *
48  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
49  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
50  */
51
52 public class Group : Composite {
53
54     alias Composite.computeSize computeSize;
55     alias Composite.windowProc windowProc;
56
57     String text = "";
58     static const int CLIENT_INSET = 3;
59     private static /+const+/ WNDPROC GroupProc;
60     static if( OS.IsWinCE ){
61         static const TCHAR[] GroupClass = "BUTTON\0";
62     }
63     else{
64         static const TCHAR[] GroupClass = "SWT_GROUP\0";
65     }
66
67     private static bool static_this_completed = false;
68     private static void static_this() {
69         if( static_this_completed ){
70             return;
71         }
72         synchronized {
73             if( static_this_completed ){
74                 return;
75             }
76             /*
77             * Feature in Windows.  The group box window class
78             * uses the CS_HREDRAW and CS_VREDRAW style bits to
79             * force a full redraw of the control and all children
80             * when resized.  This causes flashing.  The fix is to
81             * register a new window class without these bits and
82             * implement special code that damages only the control.
83             *
84             * Feature in WinCE.  On certain devices, defining
85             * a new window class which looks like BUTTON causes
86             * CreateWindowEx() to crash.  The workaround is to use
87             * the class Button directly.
88             */
89             WNDCLASS lpWndClass;
90             static if (OS.IsWinCE) {
91                 OS.GetClassInfo (null, GroupClass.ptr, &lpWndClass);
92                 GroupProc = lpWndClass.lpfnWndProc;
93             } else {
94                 TCHAR[] WC_BUTTON = "BUTTON\0";
95                 OS.GetClassInfo (null, WC_BUTTON.ptr, &lpWndClass);
96                 GroupProc = lpWndClass.lpfnWndProc;
97                 auto hInstance = OS.GetModuleHandle (null);
98                 if (!OS.GetClassInfo (hInstance, GroupClass.ptr, &lpWndClass)) {
99                     auto hHeap = OS.GetProcessHeap ();
100                     lpWndClass.hInstance = hInstance;
101                     lpWndClass.style &= ~(OS.CS_HREDRAW | OS.CS_VREDRAW);
102                     int byteCount = GroupClass.length * TCHAR.sizeof;
103                     auto lpszClassName = cast(TCHAR*)OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
104                     OS.MoveMemory (lpszClassName, GroupClass.ptr, byteCount);
105                     lpWndClass.lpszClassName = lpszClassName;
106                     OS.RegisterClass (&lpWndClass);
107                     OS.HeapFree (hHeap, 0, lpszClassName);
108                 }
109             }
110             static_this_completed = true;
111         }
112     }
113
114 /**
115  * Constructs a new instance of this class given its parent
116  * and a style value describing its behavior and appearance.
117  * <p>
118  * The style value is either one of the style constants defined in
119  * class <code>DWT</code> which is applicable to instances of this
120  * class, or must be built by <em>bitwise OR</em>'ing together
121  * (that is, using the <code>int</code> "|" operator) two or more
122  * of those <code>DWT</code> style constants. The class description
123  * lists the style constants that are applicable to the class.
124  * Style bits are also inherited from superclasses.
125  * </p>
126  *
127  * @param parent a composite control which will be the parent of the new instance (cannot be null)
128  * @param style the style of control to construct
129  *
130  * @exception IllegalArgumentException <ul>
131  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
132  * </ul>
133  * @exception DWTException <ul>
134  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
135  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
136  * </ul>
137  *
138  * @see DWT#SHADOW_ETCHED_IN
139  * @see DWT#SHADOW_ETCHED_OUT
140  * @see DWT#SHADOW_IN
141  * @see DWT#SHADOW_OUT
142  * @see DWT#SHADOW_NONE
143  * @see Widget#checkSubclass
144  * @see Widget#getStyle
145  */
146 public this (Composite parent, int style) {
147     static_this();
148     super (parent, checkStyle (style));
149 }
150
151 override int callWindowProc (HWND hwnd, int msg, int wParam, int lParam) {
152     if (handle is null) return 0;
153     /*
154     * Feature in Windows.  When the user clicks on the group
155     * box label, the group box takes focus.  This is unwanted.
156     * The fix is to avoid calling the group box window proc.
157     */
158     switch (msg) {
159         case OS.WM_LBUTTONDOWN:
160         case OS.WM_LBUTTONDBLCLK:
161             return OS.DefWindowProc (hwnd, msg, wParam, lParam);
162         default:
163     }
164     return OS.CallWindowProc (GroupProc, hwnd, msg, wParam, lParam);
165 }
166
167 static int checkStyle (int style) {
168     style |= DWT.NO_FOCUS;
169     /*
170     * Even though it is legal to create this widget
171     * with scroll bars, they serve no useful purpose
172     * because they do not automatically scroll the
173     * widget's client area.  The fix is to clear
174     * the DWT style.
175     */
176     return style & ~(DWT.H_SCROLL | DWT.V_SCROLL);
177 }
178
179 override protected void checkSubclass () {
180     if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS);
181 }
182
183 override public Point computeSize (int wHint, int hHint, bool changed) {
184     checkWidget ();
185     Point size = super.computeSize (wHint, hHint, changed);
186     int length = text.length;
187     if (length !is 0) {
188         /*
189         * Bug in Windows.  When a group control is right-to-left and
190         * is disabled, the first pixel of the text is clipped.  The
191         * fix is to add a space to both sides of the text.  Note that
192         * the work around must run all the time to stop the preferred
193         * size from changing when a group is enabled and disabled.
194         */
195         String string = text;
196         if ((style & DWT.RIGHT_TO_LEFT) !is 0) {
197             if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
198                 string = " " ~ string ~ " ";
199             }
200         }
201         /*
202         * If the group has text, and the text is wider than the
203         * client area, pad the width so the text is not clipped.
204         */
205         TCHAR* buffer = StrToTCHARz (/+getCodePage (),+/ string);
206         HFONT newFont, oldFont;
207         auto hDC = OS.GetDC (handle);
208         newFont = cast(HFONT) OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
209         if (newFont !is null) oldFont = OS.SelectObject (hDC, newFont);
210         RECT rect;
211         int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
212         OS.DrawText (hDC, buffer, -1, &rect, flags);
213         if (newFont !is null) OS.SelectObject (hDC, oldFont);
214         OS.ReleaseDC (handle, hDC);
215         int offsetY = OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed () ? 0 : 1;
216         size.x = Math.max (size.x, rect.right - rect.left + CLIENT_INSET * 6 + offsetY);
217     }
218     return size;
219 }
220
221 override public Rectangle computeTrim (int x, int y, int width, int height) {
222     checkWidget ();
223     Rectangle trim = super.computeTrim (x, y, width, height);
224     HFONT newFont, oldFont;
225     auto hDC = OS.GetDC (handle);
226     newFont = cast(HFONT) OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
227     if (newFont !is null) oldFont = OS.SelectObject (hDC, newFont);
228     TEXTMETRIC tm;
229     OS.GetTextMetrics (hDC, &tm);
230     if (newFont !is null) OS.SelectObject (hDC, oldFont);
231     OS.ReleaseDC (handle, hDC);
232     int offsetY = OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed () ? 0 : 1;
233     trim.x -= CLIENT_INSET;
234     trim.y -= tm.tmHeight + offsetY;
235     trim.width += CLIENT_INSET * 2;
236     trim.height += tm.tmHeight + CLIENT_INSET;
237     return trim;
238 }
239
240 override void createHandle () {
241     /*
242     * Feature in Windows.  When a button is created,
243     * it clears the UI state for all controls in the
244     * shell by sending WM_CHANGEUISTATE with UIS_SET,
245     * UISF_HIDEACCEL and UISF_HIDEFOCUS to the parent.
246     * This is undocumented and unexpected.  The fix
247     * is to ignore the WM_CHANGEUISTATE, when sent
248     * from CreateWindowEx().
249     */
250     parent.state |= IGNORE_WM_CHANGEUISTATE;
251     super.createHandle ();
252     parent.state &= ~IGNORE_WM_CHANGEUISTATE;
253     state |= DRAW_BACKGROUND;
254     state &= ~CANVAS;
255 }
256
257 override void enableWidget (bool enabled) {
258     super.enableWidget (enabled);
259     /*
260     * Bug in Windows.  When a group control is right-to-left and
261     * is disabled, the first pixel of the text is clipped.  The
262     * fix is to add a space to both sides of the text.
263     */
264     if ((style & DWT.RIGHT_TO_LEFT) !is 0) {
265         if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
266             String string = enabled || text.length is 0 ? text : " " ~ text ~ " ";
267             TCHAR* buffer = StrToTCHARz (/+getCodePage (),+/ string);
268             OS.SetWindowText (handle, buffer);
269         }
270     }
271 }
272
273 override public Rectangle getClientArea () {
274     checkWidget ();
275     forceResize ();
276     RECT rect;
277     OS.GetClientRect (handle, &rect);
278     HFONT newFont, oldFont;
279     auto hDC = OS.GetDC (handle);
280     newFont = cast(HFONT) OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
281     if (newFont !is null) oldFont = OS.SelectObject (hDC, newFont);
282     TEXTMETRIC tm;
283     OS.GetTextMetrics (hDC, &tm);
284     if (newFont !is null) OS.SelectObject (hDC, oldFont);
285     OS.ReleaseDC (handle, hDC);
286     int offsetY = OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed () ? 0 : 1;
287     int x = CLIENT_INSET, y = tm.tmHeight + offsetY;
288     int width = Math.max (0, rect.right - CLIENT_INSET * 2);
289     int height = Math.max (0, rect.bottom - y - CLIENT_INSET);
290     return new Rectangle (x, y, width, height);
291 }
292
293 override String getNameText () {
294     return getText ();
295 }
296
297 /**
298  * Returns the receiver's text, which is the string that the
299  * is used as the <em>title</em>. If the text has not previously
300  * been set, returns an empty string.
301  *
302  * @return the text
303  *
304  * @exception DWTException <ul>
305  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
306  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
307  * </ul>
308  */
309 public String getText () {
310     checkWidget ();
311     return text;
312 }
313
314 override bool mnemonicHit (wchar key) {
315     return setFocus ();
316 }
317
318 override bool mnemonicMatch (wchar key) {
319     wchar mnemonic = findMnemonic (getText ());
320     if (mnemonic is '\0') return false;
321     return CharacterToUpper (key) is CharacterToUpper (mnemonic);
322 }
323
324 void printWidget (HWND hwnd, GC gc) {
325     /*
326     * Bug in Windows.  For some reason, PrintWindow() fails
327     * when it is called on a push button.  The fix is to
328     * detect the failure and use WM_PRINT instead.  Note
329     * that WM_PRINT cannot be used all the time because it
330     * fails for browser controls when the browser has focus.
331     */
332     auto hDC = gc.handle;
333     if (!OS.PrintWindow (hwnd, hDC, 0)) {
334         /*
335         * Bug in Windows.  For some reason, WM_PRINT when called
336         * with PRF_CHILDREN will not draw the tool bar divider
337         * for tool bar children that do not have CCS_NODIVIDER.
338         * The fix is to draw the group box and iterate through
339         * the children, drawing each one.
340         */
341         int flags = OS.PRF_CLIENT | OS.PRF_NONCLIENT | OS.PRF_ERASEBKGND;
342         OS.SendMessage (hwnd, OS.WM_PRINT, hDC, flags);
343         int nSavedDC = OS.SaveDC (hDC);
344         Control [] children = _getChildren ();
345         Rectangle rect = getBounds ();
346         OS.IntersectClipRect (hDC, 0, 0, rect.width, rect.height);
347         for (int i=children.length - 1; i>=0; --i) {
348             Point location = children [i].getLocation ();
349             OS.SetWindowOrgEx (hDC, -location.x, -location.y, null);
350             children [i].print (gc);
351         }
352         OS.RestoreDC (hDC, nSavedDC);
353     }
354 }
355
356 override void releaseWidget () {
357     super.releaseWidget ();
358     text = null;
359 }
360
361 override public void setFont (Font font) {
362     checkWidget ();
363     Rectangle oldRect = getClientArea ();
364     super.setFont (font);
365     Rectangle newRect = getClientArea ();
366     if (oldRect!=newRect) sendResize ();
367 }
368
369 /**
370  * Sets the receiver's text, which is the string that will
371  * be displayed as the receiver's <em>title</em>, to the argument,
372  * which may not be null. The string may include the mnemonic character.
373  * </p>
374  * Mnemonics are indicated by an '&amp;' that causes the next
375  * character to be the mnemonic.  When the user presses a
376  * key sequence that matches the mnemonic, focus is assigned
377  * to the first child of the group. On most platforms, the
378  * mnemonic appears underlined but may be emphasised in a
379  * platform specific manner.  The mnemonic indicator character
380  * '&amp;' can be escaped by doubling it in the string, causing
381  * a single '&amp;' to be displayed.
382  * </p>
383  * @param string the new text
384  *
385  * @exception DWTException <ul>
386  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
387  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
388  * </ul>
389  */
390 public void setText (String string) {
391     checkWidget ();
392     // DWT extension: allow null string
393     //if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
394     text = string;
395     /*
396     * Bug in Windows.  When a group control is right-to-left and
397     * is disabled, the first pixel of the text is clipped.  The
398     * fix is to add a space to both sides of the text.
399     */
400     if ((style & DWT.RIGHT_TO_LEFT) !is 0) {
401         if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
402             if (!OS.IsWindowEnabled (handle)) {
403                 if (string.length !is 0) string = " " ~ string ~ " ";
404             }
405         }
406     }
407     TCHAR* buffer = StrToTCHARz(/+getCodePage (),+/ string);
408     OS.SetWindowText (handle, buffer);
409 }
410
411 override int widgetStyle () {
412     /*
413     * Bug in Windows.  When GetDCEx() is called with DCX_INTERSECTUPDATE,
414     * the HDC that is returned does not include the current update region.
415     * This was confirmed under DEBUG Windows when GetDCEx() complained about
416     * invalid flags.  Therefore, it is not easily possible to get an HDC from
417     * outside of WM_PAINT that includes the current damage and clips children.
418     * Because the receiver has children and draws a frame and label, it is
419     * necessary that the receiver always draw clipped, in the current damaged
420     * area.  The fix is to force the receiver to be fully clipped by including
421     * WS_CLIPCHILDREN and WS_CLIPSIBLINGS in the default style bits.
422     */
423     return super.widgetStyle () | OS.BS_GROUPBOX | OS.WS_CLIPCHILDREN | OS.WS_CLIPSIBLINGS;
424 }
425
426 override String windowClass () {
427     return TCHARsToStr( GroupClass );
428 }
429
430 override int windowProc () {
431     return cast(int) GroupProc;
432 }
433
434 override LRESULT WM_ERASEBKGND (int wParam, int lParam) {
435     LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
436     if (result !is null) return result;
437     /*
438     * Feature in Windows.  Group boxes do not erase
439     * the background before drawing.  The fix is to
440     * fill the background.
441     */
442     drawBackground (cast(HANDLE) wParam);
443     return LRESULT.ONE;
444 }
445
446 override LRESULT WM_NCHITTEST (int wParam, int lParam) {
447     LRESULT result = super.WM_NCHITTEST (wParam, lParam);
448     if (result !is null) return result;
449     /*
450     * Feature in Windows.  The window proc for the group box
451     * returns HTTRANSPARENT indicating that mouse messages
452     * should not be delivered to the receiver and any children.
453     * Normally, group boxes in Windows do not have children and
454     * this is the correct behavior for this case.  Because we
455     * allow children, answer HTCLIENT to allow mouse messages
456     * to be delivered to the children.
457     */
458     int /*long*/ code = callWindowProc (handle, OS.WM_NCHITTEST, wParam, lParam);
459     if (code is OS.HTTRANSPARENT) code = OS.HTCLIENT;
460     return new LRESULT (code);
461 }
462
463 override LRESULT WM_MOUSEMOVE (int wParam, int lParam) {
464     LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
465     if (result !is null) return result;
466     /*
467     * Feature in Windows.  In version 6.00 of COMCTL32.DLL,
468     * every time the mouse moves, the group title redraws.
469     * This only happens when WM_NCHITTEST returns HTCLIENT.
470     * The fix is to avoid calling the group window proc.
471     */
472     return LRESULT.ZERO;
473 }
474
475 override LRESULT WM_PRINTCLIENT (int wParam, int lParam) {
476     LRESULT result = super.WM_PRINTCLIENT (wParam, lParam);
477     if (result !is null) return result;
478     /*
479     * Feature in Windows.  In version 6.00 of COMCTL32.DLL,
480     * when WM_PRINTCLIENT is sent from a child BS_GROUP
481     * control to a parent BS_GROUP, the parent BS_GROUP
482     * clears the font from the HDC.  Normally, group boxes
483     * in Windows do not have children so this behavior is
484     * undefined.  When the parent of a BS_GROUP is not a
485     * BS_GROUP, there is no problem.  The fix is to save
486     * and restore the current font.
487     */
488     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
489         auto nSavedDC = OS.SaveDC (cast(HDC)wParam);
490         int /*long*/ code = callWindowProc (handle, OS.WM_PRINTCLIENT, wParam, lParam);
491         OS.RestoreDC (cast(HDC)wParam, nSavedDC);
492         return new LRESULT (code);
493     }
494     return result;
495 }
496
497 override LRESULT WM_UPDATEUISTATE (int wParam, int lParam) {
498     LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
499     if (result !is null) return result;
500     /*
501     * Feature in Windows.  When WM_UPDATEUISTATE is sent to
502     * a group, it sends WM_CTLCOLORBTN to get the foreground
503     * and background.  If drawing happens in WM_CTLCOLORBTN,
504     * it will overwrite the contents of the control.  The
505     * fix is draw the group without drawing the background
506     * and avoid the group window proc.
507     */
508     bool redraw = findImageControl () !is null;
509     if (!redraw) {
510         if ((state & THEME_BACKGROUND) !is 0) {
511             if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
512                 redraw = findThemeControl () !is null;
513             }
514         }
515         if (!redraw) redraw = findBackgroundControl () !is null;
516     }
517     if (redraw) {
518         OS.InvalidateRect (handle, null, false);
519         int /*long*/ code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
520         return new LRESULT (code);
521     }
522     return result;
523 }
524
525 override LRESULT WM_WINDOWPOSCHANGING (int wParam, int lParam) {
526     LRESULT result = super.WM_WINDOWPOSCHANGING (wParam, lParam);
527     if (result !is null) return result;
528     /*
529     * Invalidate the portion of the group widget that needs to
530     * be redrawn.  Note that for some reason, invalidating the
531     * group from inside WM_SIZE causes pixel corruption for
532     * radio button children.
533     */
534     static if (OS.IsWinCE) return result;
535     if (!OS.IsWindowVisible (handle)) return result;
536     WINDOWPOS* lpwp = cast(WINDOWPOS*)lParam;
537     //OS.MoveMemory (lpwp, lParam, WINDOWPOS.sizeof);
538     if ((lpwp.flags & (OS.SWP_NOSIZE | OS.SWP_NOREDRAW)) !is 0) {
539         return result;
540     }
541     RECT rect;
542     OS.SetRect (&rect, 0, 0, lpwp.cx, lpwp.cy);
543     OS.SendMessage (handle, OS.WM_NCCALCSIZE, 0, &rect);
544     int newWidth = rect.right - rect.left;
545     int newHeight = rect.bottom - rect.top;
546     OS.GetClientRect (handle, &rect);
547     int oldWidth = rect.right - rect.left;
548     int oldHeight = rect.bottom - rect.top;
549     if (newWidth is oldWidth && newHeight is oldHeight) {
550         return result;
551     }
552     if (newWidth !is oldWidth) {
553         int left = oldWidth;
554         if (newWidth < oldWidth) left = newWidth;
555         OS.SetRect (&rect, left - CLIENT_INSET, 0, newWidth, newHeight);
556         OS.InvalidateRect (handle, &rect, true);
557     }
558     if (newHeight !is oldHeight) {
559         int bottom = oldHeight;
560         if (newHeight < oldHeight) bottom = newHeight;
561         if (newWidth < oldWidth) oldWidth -= CLIENT_INSET;
562         OS.SetRect (&rect, 0, bottom - CLIENT_INSET, oldWidth, newHeight);
563         OS.InvalidateRect (handle, &rect, true);
564     }
565     return result;
566 }
567 }
Note: See TracBrowser for help on using the browser.