KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > Group


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  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class provide an etched border
20  * with an optional title.
21  * <p>
22  * Shadow styles are hints and may not be honoured
23  * by the platform. To create a group with the
24  * default shadow style for the platform, do not
25  * specify a shadow style.
26  * <dl>
27  * <dt><b>Styles:</b></dt>
28  * <dd>SHADOW_ETCHED_IN, SHADOW_ETCHED_OUT, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
29  * <dt><b>Events:</b></dt>
30  * <dd>(none)</dd>
31  * </dl>
32  * <p>
33  * Note: Only one of the above styles may be specified.
34  * </p><p>
35  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
36  * </p>
37  */

38
39 public class Group extends Composite {
40     String JavaDoc text = "";
41     static final int CLIENT_INSET = 3;
42     static final int GroupProc;
43     static final TCHAR GroupClass = new TCHAR (0, OS.IsWinCE ? "BUTTON" : "SWT_GROUP", true);
44     static {
45         /*
46         * Feature in Windows. The group box window class
47         * uses the CS_HREDRAW and CS_VREDRAW style bits to
48         * force a full redraw of the control and all children
49         * when resized. This causes flashing. The fix is to
50         * register a new window class without these bits and
51         * implement special code that damages only the control.
52         *
53         * Feature in WinCE. On certain devices, defining
54         * a new window class which looks like BUTTON causes
55         * CreateWindowEx() to crash. The workaround is to use
56         * the class Button directly.
57         */

58         WNDCLASS lpWndClass = new WNDCLASS ();
59         if (OS.IsWinCE) {
60             OS.GetClassInfo (0, GroupClass, lpWndClass);
61             GroupProc = lpWndClass.lpfnWndProc;
62         } else {
63             TCHAR WC_BUTTON = new TCHAR (0, "BUTTON", true);
64             OS.GetClassInfo (0, WC_BUTTON, lpWndClass);
65             GroupProc = lpWndClass.lpfnWndProc;
66             int hInstance = OS.GetModuleHandle (null);
67             if (!OS.GetClassInfo (hInstance, GroupClass, lpWndClass)) {
68                 int hHeap = OS.GetProcessHeap ();
69                 lpWndClass.hInstance = hInstance;
70                 lpWndClass.style &= ~(OS.CS_HREDRAW | OS.CS_VREDRAW);
71                 int byteCount = GroupClass.length () * TCHAR.sizeof;
72                 int lpszClassName = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
73                 OS.MoveMemory (lpszClassName, GroupClass, byteCount);
74                 lpWndClass.lpszClassName = lpszClassName;
75                 OS.RegisterClass (lpWndClass);
76                 OS.HeapFree (hHeap, 0, lpszClassName);
77             }
78         }
79     }
80
81 /**
82  * Constructs a new instance of this class given its parent
83  * and a style value describing its behavior and appearance.
84  * <p>
85  * The style value is either one of the style constants defined in
86  * class <code>SWT</code> which is applicable to instances of this
87  * class, or must be built by <em>bitwise OR</em>'ing together
88  * (that is, using the <code>int</code> "|" operator) two or more
89  * of those <code>SWT</code> style constants. The class description
90  * lists the style constants that are applicable to the class.
91  * Style bits are also inherited from superclasses.
92  * </p>
93  *
94  * @param parent a composite control which will be the parent of the new instance (cannot be null)
95  * @param style the style of control to construct
96  *
97  * @exception IllegalArgumentException <ul>
98  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
99  * </ul>
100  * @exception SWTException <ul>
101  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
102  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
103  * </ul>
104  *
105  * @see SWT#SHADOW_ETCHED_IN
106  * @see SWT#SHADOW_ETCHED_OUT
107  * @see SWT#SHADOW_IN
108  * @see SWT#SHADOW_OUT
109  * @see SWT#SHADOW_NONE
110  * @see Widget#checkSubclass
111  * @see Widget#getStyle
112  */

113 public Group (Composite parent, int style) {
114     super (parent, checkStyle (style));
115 }
116
117 int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
118     if (handle == 0) return 0;
119     /*
120     * Feature in Windows. When the user clicks on the group
121     * box label, the group box takes focus. This is unwanted.
122     * The fix is to avoid calling the group box window proc.
123     */

124     switch (msg) {
125         case OS.WM_LBUTTONDOWN:
126         case OS.WM_LBUTTONDBLCLK:
127             return OS.DefWindowProc (hwnd, msg, wParam, lParam);
128     }
129     return OS.CallWindowProc (GroupProc, hwnd, msg, wParam, lParam);
130 }
131
132 static int checkStyle (int style) {
133     style |= SWT.NO_FOCUS;
134     /*
135     * Even though it is legal to create this widget
136     * with scroll bars, they serve no useful purpose
137     * because they do not automatically scroll the
138     * widget's client area. The fix is to clear
139     * the SWT style.
140     */

141     return style & ~(SWT.H_SCROLL | SWT.V_SCROLL);
142 }
143
144 protected void checkSubclass () {
145     if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
146 }
147
148 public Point computeSize (int wHint, int hHint, boolean changed) {
149     checkWidget ();
150     Point size = super.computeSize (wHint, hHint, changed);
151     int length = text.length ();
152     if (length != 0) {
153         /*
154         * Bug in Windows. When a group control is right-to-left and
155         * is disabled, the first pixel of the text is clipped. The
156         * fix is to add a space to both sides of the text. Note that
157         * the work around must run all the time to stop the preferred
158         * size from changing when a group is enabled and disabled.
159         */

160         String JavaDoc string = text;
161         if ((style & SWT.RIGHT_TO_LEFT) != 0) {
162             if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
163                 string = " " + string + " ";
164             }
165         }
166         /*
167         * If the group has text, and the text is wider than the
168         * client area, pad the width so the text is not clipped.
169         */

170         TCHAR buffer = new TCHAR (getCodePage (), string, true);
171         int newFont, oldFont = 0;
172         int hDC = OS.GetDC (handle);
173         newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
174         if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
175         RECT rect = new RECT ();
176         int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
177         OS.DrawText (hDC, buffer, -1, rect, flags);
178         if (newFont != 0) OS.SelectObject (hDC, oldFont);
179         OS.ReleaseDC (handle, hDC);
180         size.x = Math.max (size.x, rect.right - rect.left + CLIENT_INSET * 6);
181     }
182     return size;
183 }
184
185 public Rectangle computeTrim (int x, int y, int width, int height) {
186     checkWidget ();
187     Rectangle trim = super.computeTrim (x, y, width, height);
188     int newFont, oldFont = 0;
189     int hDC = OS.GetDC (handle);
190     newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
191     if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
192     TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
193     OS.GetTextMetrics (hDC, tm);
194     if (newFont != 0) OS.SelectObject (hDC, oldFont);
195     OS.ReleaseDC (handle, hDC);
196     trim.x -= CLIENT_INSET;
197     trim.y -= tm.tmHeight;
198     trim.width += CLIENT_INSET * 2;
199     trim.height += tm.tmHeight + CLIENT_INSET;
200     return trim;
201 }
202
203 void createHandle () {
204     super.createHandle ();
205     state |= DRAW_BACKGROUND;
206     state &= ~CANVAS;
207 }
208
209 void enableWidget (boolean enabled) {
210     super.enableWidget (enabled);
211     /*
212     * Bug in Windows. When a group control is right-to-left and
213     * is disabled, the first pixel of the text is clipped. The
214     * fix is to add a space to both sides of the text.
215     */

216     if ((style & SWT.RIGHT_TO_LEFT) != 0) {
217         if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
218             String JavaDoc string = enabled || text.length() == 0 ? text : " " + text + " ";
219             TCHAR buffer = new TCHAR (getCodePage (), string, true);
220             OS.SetWindowText (handle, buffer);
221         }
222     }
223 }
224
225 public Rectangle getClientArea () {
226     checkWidget ();
227     forceResize ();
228     RECT rect = new RECT ();
229     OS.GetClientRect (handle, rect);
230     int newFont, oldFont = 0;
231     int hDC = OS.GetDC (handle);
232     newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
233     if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
234     TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
235     OS.GetTextMetrics (hDC, tm);
236     if (newFont != 0) OS.SelectObject (hDC, oldFont);
237     OS.ReleaseDC (handle, hDC);
238     int x = CLIENT_INSET, y = tm.tmHeight;
239     int width = Math.max (0, rect.right - CLIENT_INSET * 2);
240     int height = Math.max (0, rect.bottom - y - CLIENT_INSET);
241     return new Rectangle (x, y, width, height);
242 }
243
244 String JavaDoc getNameText () {
245     return getText ();
246 }
247
248 /**
249  * Returns the receiver's text, which is the string that the
250  * is used as the <em>title</em>. If the text has not previously
251  * been set, returns an empty string.
252  *
253  * @return the text
254  *
255  * @exception SWTException <ul>
256  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
257  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
258  * </ul>
259  */

260 public String JavaDoc getText () {
261     checkWidget ();
262     return text;
263 }
264
265 boolean mnemonicHit (char key) {
266     return setFocus ();
267 }
268
269 boolean mnemonicMatch (char key) {
270     char mnemonic = findMnemonic (getText ());
271     if (mnemonic == '\0') return false;
272     return Character.toUpperCase (key) == Character.toUpperCase (mnemonic);
273 }
274
275 void releaseWidget () {
276     super.releaseWidget ();
277     text = null;
278 }
279
280 public void setFont (Font font) {
281     checkWidget ();
282     Rectangle oldRect = getClientArea ();
283     super.setFont (font);
284     Rectangle newRect = getClientArea ();
285     if (!oldRect.equals (newRect)) sendResize ();
286 }
287
288 /**
289  * Sets the receiver's text, which is the string that will
290  * be displayed as the receiver's <em>title</em>, to the argument,
291  * which may not be null. The string may include the mnemonic character.
292  * </p>
293  * Mnemonics are indicated by an '&amp;' that causes the next
294  * character to be the mnemonic. When the user presses a
295  * key sequence that matches the mnemonic, focus is assigned
296  * to the first child of the group. On most platforms, the
297  * mnemonic appears underlined but may be emphasised in a
298  * platform specific manner. The mnemonic indicator character
299  * '&amp;' can be escaped by doubling it in the string, causing
300  * a single '&amp;' to be displayed.
301  * </p>
302  * @param string the new text
303  *
304  * @exception IllegalArgumentException <ul>
305  * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
306  * </ul>
307  * @exception SWTException <ul>
308  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
309  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
310  * </ul>
311  */

312 public void setText (String JavaDoc string) {
313     checkWidget ();
314     if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
315     text = string;
316     /*
317     * Bug in Windows. When a group control is right-to-left and
318     * is disabled, the first pixel of the text is clipped. The
319     * fix is to add a space to both sides of the text.
320     */

321     if ((style & SWT.RIGHT_TO_LEFT) != 0) {
322         if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
323             if (!OS.IsWindowEnabled (handle)) {
324                 if (string.length() != 0) string = " " + string + " ";
325             }
326         }
327     }
328     TCHAR buffer = new TCHAR (getCodePage (), string, true);
329     OS.SetWindowText (handle, buffer);
330 }
331
332 int widgetStyle () {
333     /*
334     * Bug in Windows. When GetDCEx() is called with DCX_INTERSECTUPDATE,
335     * the HDC that is returned does not include the current update region.
336     * This was confirmed under DEBUG Windows when GetDCEx() complained about
337     * invalid flags. Therefore, it is not easily possible to get an HDC from
338     * outside of WM_PAINT that includes the current damage and clips children.
339     * Because the receiver has children and draws a frame and label, it is
340     * necessary that the receiver always draw clipped, in the current damaged
341     * area. The fix is to force the receiver to be fully clipped by including
342     * WS_CLIPCHILDREN and WS_CLIPSIBLINGS in the default style bits.
343     */

344     return super.widgetStyle () | OS.BS_GROUPBOX | OS.WS_CLIPCHILDREN | OS.WS_CLIPSIBLINGS;
345 }
346
347 TCHAR windowClass () {
348     return GroupClass;
349 }
350
351 int windowProc () {
352     return GroupProc;
353 }
354
355 LRESULT WM_ERASEBKGND (int wParam, int lParam) {
356     LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
357     if (result != null) return result;
358     /*
359     * Feature in Windows. Group boxes do not erase
360     * the background before drawing. The fix is to
361     * fill the background.
362     */

363     drawBackground (wParam);
364     return LRESULT.ONE;
365 }
366
367 LRESULT WM_NCHITTEST (int wParam, int lParam) {
368     LRESULT result = super.WM_NCHITTEST (wParam, lParam);
369     if (result != null) return result;
370     /*
371     * Feature in Windows. The window proc for the group box
372     * returns HTTRANSPARENT indicating that mouse messages
373     * should not be delivered to the receiver and any children.
374     * Normally, group boxes in Windows do not have children and
375     * this is the correct behavior for this case. Because we
376     * allow children, answer HTCLIENT to allow mouse messages
377     * to be delivered to the children.
378     */

379     int code = callWindowProc (handle, OS.WM_NCHITTEST, wParam, lParam);
380     if (code == OS.HTTRANSPARENT) code = OS.HTCLIENT;
381     return new LRESULT (code);
382 }
383
384 LRESULT WM_MOUSEMOVE (int wParam, int lParam) {
385     LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
386     if (result != null) return result;
387     /*
388     * Feature in Windows. In version 6.00 of COMCTL32.DLL,
389     * every time the mouse moves, the group title redraws.
390     * This only happens when WM_NCHITTEST returns HTCLIENT.
391     * The fix is to avoid calling the group window proc.
392     */

393     return LRESULT.ZERO;
394 }
395
396 LRESULT WM_PRINTCLIENT (int wParam, int lParam) {
397     LRESULT result = super.WM_PRINTCLIENT (wParam, lParam);
398     if (result != null) return result;
399     /*
400     * Feature in Windows. In version 6.00 of COMCTL32.DLL,
401     * when WM_PRINTCLIENT is sent from a child BS_GROUP
402     * control to a parent BS_GROUP, the parent BS_GROUP
403     * clears the font from the HDC. Normally, group boxes
404     * in Windows do not have children so this behavior is
405     * undefined. When the parent of a BS_GROUP is not a
406     * BS_GROUP, there is no problem. The fix is to save
407     * and restore the current font.
408     */

409     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
410         int nSavedDC = OS.SaveDC (wParam);
411         int code = callWindowProc (handle, OS.WM_PRINTCLIENT, wParam, lParam);
412         OS.RestoreDC (wParam, nSavedDC);
413         return new LRESULT (code);
414     }
415     return result;
416 }
417
418 LRESULT WM_UPDATEUISTATE (int wParam, int lParam) {
419     LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
420     if (result != null) return result;
421     /*
422     * Feature in Windows. When WM_UPDATEUISTATE is sent to
423     * a group, it sends WM_CTLCOLORBTN to get the foreground
424     * and background. If drawing happens in WM_CTLCOLORBTN,
425     * it will overwrite the contents of the control. The
426     * fix is draw the group without drawing the background
427     * and avoid the group window proc.
428     */

429     boolean redraw = findImageControl () != null;
430     if (!redraw) {
431         if ((state & THEME_BACKGROUND) != 0) {
432             if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
433                 redraw = findThemeControl () != null;
434             }
435         }
436         if (!redraw) redraw = findBackgroundControl () != null;
437     }
438     if (redraw) {
439         OS.InvalidateRect (handle, null, false);
440         int code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
441         return new LRESULT (code);
442     }
443     return result;
444 }
445
446 LRESULT WM_WINDOWPOSCHANGING (int wParam, int lParam) {
447     LRESULT result = super.WM_WINDOWPOSCHANGING (wParam, lParam);
448     if (result != null) return result;
449     /*
450     * Invalidate the portion of the group widget that needs to
451     * be redrawn. Note that for some reason, invalidating the
452     * group from inside WM_SIZE causes pixel corruption for
453     * radio button children.
454     */

455     if (OS.IsWinCE) return result;
456     if (!OS.IsWindowVisible (handle)) return result;
457     WINDOWPOS lpwp = new WINDOWPOS ();
458     OS.MoveMemory (lpwp, lParam, WINDOWPOS.sizeof);
459     if ((lpwp.flags & (OS.SWP_NOSIZE | OS.SWP_NOREDRAW)) != 0) {
460         return result;
461     }
462     RECT rect = new RECT ();
463     OS.SetRect (rect, 0, 0, lpwp.cx, lpwp.cy);
464     OS.SendMessage (handle, OS.WM_NCCALCSIZE, 0, rect);
465     int newWidth = rect.right - rect.left;
466     int newHeight = rect.bottom - rect.top;
467     OS.GetClientRect (handle, rect);
468     int oldWidth = rect.right - rect.left;
469     int oldHeight = rect.bottom - rect.top;
470     if (newWidth == oldWidth && newHeight == oldHeight) {
471         return result;
472     }
473     if (newWidth != oldWidth) {
474         int left = oldWidth;
475         if (newWidth < oldWidth) left = newWidth;
476         OS.SetRect (rect, left - CLIENT_INSET, 0, newWidth, newHeight);
477         OS.InvalidateRect (handle, rect, true);
478     }
479     if (newHeight != oldHeight) {
480         int bottom = oldHeight;
481         if (newHeight < oldHeight) bottom = newHeight;
482         if (newWidth < oldWidth) oldWidth -= CLIENT_INSET;
483         OS.SetRect (rect, 0, bottom - CLIENT_INSET, oldWidth, newHeight);
484         OS.InvalidateRect (handle, rect, true);
485     }
486     return result;
487 }
488 }
489
Popular Tags