1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
23 * Instances of this class provide an etched border
24 * with an optional title.
26 * Shadow styles are hints and may not be honoured
27 * by the platform. To create a group with the
28 * default shadow style for the platform, do not
29 * specify a shadow style.
31 * <dt><b>Styles:</b></dt>
32 * <dd>SHADOW_ETCHED_IN, SHADOW_ETCHED_OUT, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
33 * <dt><b>Events:</b></dt>
37 * Note: Only one of the above styles may be specified.
39 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
42 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
43 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
44 * @noextend This class is not intended to be subclassed by clients.
46 public class Group extends Composite {
48 static final int CLIENT_INSET = 3;
49 static final long GroupProc;
50 static final TCHAR GroupClass = new TCHAR (0, "SWT_GROUP", true);
53 * Feature in Windows. The group box window class
54 * uses the CS_HREDRAW and CS_VREDRAW style bits to
55 * force a full redraw of the control and all children
56 * when resized. This causes flashing. The fix is to
57 * register a new window class without these bits and
58 * implement special code that damages only the control.
60 WNDCLASS lpWndClass = new WNDCLASS ();
61 TCHAR WC_BUTTON = new TCHAR (0, "BUTTON", true);
62 OS.GetClassInfo (0, WC_BUTTON, lpWndClass);
63 GroupProc = lpWndClass.lpfnWndProc;
64 long hInstance = OS.GetModuleHandle (null);
65 if (!OS.GetClassInfo (hInstance, GroupClass, lpWndClass)) {
66 lpWndClass.hInstance = hInstance;
67 lpWndClass.style &= ~(OS.CS_HREDRAW | OS.CS_VREDRAW);
68 OS.RegisterClass (GroupClass, lpWndClass);
73 * Constructs a new instance of this class given its parent
74 * and a style value describing its behavior and appearance.
76 * The style value is either one of the style constants defined in
77 * class <code>SWT</code> which is applicable to instances of this
78 * class, or must be built by <em>bitwise OR</em>'ing together
79 * (that is, using the <code>int</code> "|" operator) two or more
80 * of those <code>SWT</code> style constants. The class description
81 * lists the style constants that are applicable to the class.
82 * Style bits are also inherited from superclasses.
85 * @param parent a composite control which will be the parent of the new instance (cannot be null)
86 * @param style the style of control to construct
88 * @exception IllegalArgumentException <ul>
89 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
91 * @exception SWTException <ul>
92 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
93 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
96 * @see SWT#SHADOW_ETCHED_IN
97 * @see SWT#SHADOW_ETCHED_OUT
100 * @see SWT#SHADOW_NONE
101 * @see Widget#checkSubclass
102 * @see Widget#getStyle
104 public Group (Composite parent, int style) {
105 super (parent, checkStyle (style));
109 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
110 if (handle == 0) return 0;
112 * Feature in Windows. When the user clicks on the group
113 * box label, the group box takes focus. This is unwanted.
114 * The fix is to avoid calling the group box window proc.
117 case OS.WM_LBUTTONDOWN:
118 case OS.WM_LBUTTONDBLCLK:
119 return OS.DefWindowProc (hwnd, msg, wParam, lParam);
121 return OS.CallWindowProc (GroupProc, hwnd, msg, wParam, lParam);
124 static int checkStyle (int style) {
125 style |= SWT.NO_FOCUS;
127 * Even though it is legal to create this widget
128 * with scroll bars, they serve no useful purpose
129 * because they do not automatically scroll the
130 * widget's client area. The fix is to clear
133 return style & ~(SWT.H_SCROLL | SWT.V_SCROLL);
137 protected void checkSubclass () {
138 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
141 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
143 Point size = super.computeSizeInPixels (wHint, hHint, changed);
144 int length = text.length ();
146 String string = fixText (false);
149 * If the group has text, and the text is wider than the
150 * client area, pad the width so the text is not clipped.
152 char [] buffer = (string == null ? text : string).toCharArray ();
153 long newFont, oldFont = 0;
154 long hDC = OS.GetDC (handle);
155 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
156 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
157 RECT rect = new RECT ();
158 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
159 OS.DrawText (hDC, buffer, buffer.length, rect, flags);
160 if (newFont != 0) OS.SelectObject (hDC, oldFont);
161 OS.ReleaseDC (handle, hDC);
162 int offsetY = OS.IsAppThemed () ? 0 : 1;
163 size.x = Math.max (size.x, rect.right - rect.left + CLIENT_INSET * 6 + offsetY);
168 @Override Rectangle computeTrimInPixels (int x, int y, int width, int height) {
170 Rectangle trim = super.computeTrimInPixels (x, y, width, height);
171 long newFont, oldFont = 0;
172 long hDC = OS.GetDC (handle);
173 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
174 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
175 TEXTMETRIC tm = new TEXTMETRIC ();
176 OS.GetTextMetrics (hDC, tm);
177 if (newFont != 0) OS.SelectObject (hDC, oldFont);
178 OS.ReleaseDC (handle, hDC);
179 int offsetY = OS.IsAppThemed () ? 0 : 1;
180 trim.x -= CLIENT_INSET;
181 trim.y -= tm.tmHeight + offsetY;
182 trim.width += CLIENT_INSET * 2;
183 trim.height += tm.tmHeight + CLIENT_INSET + offsetY;
188 void createHandle () {
190 * Feature in Windows. When a button is created,
191 * it clears the UI state for all controls in the
192 * shell by sending WM_CHANGEUISTATE with UIS_SET,
193 * UISF_HIDEACCEL and UISF_HIDEFOCUS to the parent.
194 * This is undocumented and unexpected. The fix
195 * is to ignore the WM_CHANGEUISTATE, when sent
196 * from CreateWindowEx().
198 parent.state |= IGNORE_WM_CHANGEUISTATE;
199 super.createHandle ();
200 parent.state &= ~IGNORE_WM_CHANGEUISTATE;
201 state |= DRAW_BACKGROUND;
206 void enableWidget (boolean enabled) {
207 super.enableWidget (enabled);
209 * Bug in Windows. When a group control is right-to-left and
210 * is disabled, the first pixel of the text is clipped. The
211 * fix is to add a space to both sides of the text.
213 String string = fixText (enabled);
214 if (string != null) {
215 TCHAR buffer = new TCHAR (getCodePage (), string, true);
216 OS.SetWindowText (handle, buffer);
218 if (enabled && hasCustomForeground()) {
219 OS.InvalidateRect (handle, null, true);
223 String fixText (boolean enabled) {
225 * Bug in Windows. When a group control is right-to-left and
226 * is disabled, the first pixel of the text is clipped. The
227 * fix is to add a space to both sides of the text.
229 if (text.length() == 0) return null;
230 if ((style & SWT.RIGHT_TO_LEFT) != 0) {
231 String string = null;
232 if (!enabled && !OS.IsAppThemed ()) {
233 string = " " + text + " ";
235 return (style & SWT.FLIP_TEXT_DIRECTION) == 0 ? string : string != null ? LRE + string : LRE + text;
236 } else if ((style & SWT.FLIP_TEXT_DIRECTION) != 0) {
242 @Override Rectangle getClientAreaInPixels () {
245 RECT rect = new RECT ();
246 OS.GetClientRect (handle, rect);
247 long newFont, oldFont = 0;
248 long hDC = OS.GetDC (handle);
249 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
250 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
251 TEXTMETRIC tm = new TEXTMETRIC ();
252 OS.GetTextMetrics (hDC, tm);
253 if (newFont != 0) OS.SelectObject (hDC, oldFont);
254 OS.ReleaseDC (handle, hDC);
255 int offsetY = OS.IsAppThemed () ? 0 : 1;
256 int x = CLIENT_INSET, y = tm.tmHeight + offsetY;
257 int width = Math.max (0, rect.right - CLIENT_INSET * 2);
258 int height = Math.max (0, rect.bottom - y - CLIENT_INSET);
259 return new Rectangle (x, y, width, height);
263 String getNameText () {
268 * Returns the receiver's text, which is the string that the
269 * is used as the <em>title</em>. If the text has not previously
270 * been set, returns an empty string.
274 * @exception SWTException <ul>
275 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
276 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
279 public String getText () {
285 boolean mnemonicHit (char key) {
290 boolean mnemonicMatch (char key) {
291 char mnemonic = findMnemonic (getText ());
292 if (mnemonic == '\0') return false;
293 return Character.toUpperCase (key) == Character.toUpperCase (mnemonic);
297 void printWidget (long hwnd, long hdc, GC gc) {
299 * Bug in Windows. For some reason, PrintWindow()
300 * returns success but does nothing when it is called
301 * on a printer. The fix is to just go directly to
302 * WM_PRINT in this case.
304 boolean success = false;
305 if (!(OS.GetDeviceCaps(gc.handle, OS.TECHNOLOGY) == OS.DT_RASPRINTER)) {
306 int bits = OS.GetWindowLong (hwnd, OS.GWL_STYLE);
307 if ((bits & OS.WS_VISIBLE) == 0) {
308 OS.ShowWindow (hwnd, OS.SW_SHOW);
310 success = OS.PrintWindow (hwnd, hdc, 0);
311 if ((bits & OS.WS_VISIBLE) == 0) {
312 OS.ShowWindow (hwnd, OS.SW_HIDE);
317 * Bug in Windows. For some reason, PrintWindow() fails
318 * when it is called on a push button. The fix is to
319 * detect the failure and use WM_PRINT instead. Note
320 * that WM_PRINT cannot be used all the time because it
321 * fails for browser controls when the browser has focus.
325 * Bug in Windows. For some reason, WM_PRINT when called
326 * with PRF_CHILDREN will not draw the tool bar divider
327 * for tool bar children that do not have CCS_NODIVIDER.
328 * The fix is to draw the group box and iterate through
329 * the children, drawing each one.
331 int flags = OS.PRF_CLIENT | OS.PRF_NONCLIENT | OS.PRF_ERASEBKGND;
332 OS.SendMessage (hwnd, OS.WM_PRINT, hdc, flags);
333 int nSavedDC = OS.SaveDC (hdc);
334 Control [] children = _getChildren ();
335 Rectangle rect = getBoundsInPixels ();
336 OS.IntersectClipRect (hdc, 0, 0, rect.width, rect.height);
337 for (int i=children.length - 1; i>=0; --i) {
338 Point location = children [i].getLocationInPixels ();
339 int graphicsMode = OS.GetGraphicsMode(hdc);
340 if (graphicsMode == OS.GM_ADVANCED) {
341 float [] lpXform = {1, 0, 0, 1, location.x, location.y};
342 OS.ModifyWorldTransform(hdc, lpXform, OS.MWT_LEFTMULTIPLY);
344 OS.SetWindowOrgEx (hdc, -location.x, -location.y, null);
346 long topHandle = children [i].topHandle();
347 int bits = OS.GetWindowLong (topHandle, OS.GWL_STYLE);
348 if ((bits & OS.WS_VISIBLE) != 0) {
349 children [i].printWidget (topHandle, hdc, gc);
351 if (graphicsMode == OS.GM_ADVANCED) {
352 float [] lpXform = {1, 0, 0, 1, -location.x, -location.y};
353 OS.ModifyWorldTransform(hdc, lpXform, OS.MWT_LEFTMULTIPLY);
356 OS.RestoreDC (hdc, nSavedDC);
361 void releaseWidget () {
362 super.releaseWidget ();
367 int resolveTextDirection () {
368 return BidiUtil.resolveTextDirection (text);
372 public void setFont (Font font) {
374 Rectangle oldRect = getClientAreaInPixels ();
375 super.setFont (font);
376 Rectangle newRect = getClientAreaInPixels ();
377 if (!oldRect.equals (newRect)) sendResize ();
381 * Sets the receiver's text, which is the string that will
382 * be displayed as the receiver's <em>title</em>, to the argument,
383 * which may not be null. The string may include the mnemonic character.
385 * Mnemonics are indicated by an '&' that causes the next
386 * character to be the mnemonic. When the user presses a
387 * key sequence that matches the mnemonic, focus is assigned
388 * to the first child of the group. On most platforms, the
389 * mnemonic appears underlined but may be emphasised in a
390 * platform specific manner. The mnemonic indicator character
391 * '&' can be escaped by doubling it in the string, causing
392 * a single '&' to be displayed.
394 * Note: If control characters like '\n', '\t' etc. are used
395 * in the string, then the behavior is platform dependent.
397 * @param string the new text
399 * @exception IllegalArgumentException <ul>
400 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
402 * @exception SWTException <ul>
403 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
404 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
407 public void setText (String string) {
409 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
411 if ((state & HAS_AUTO_DIRECTION) == 0 || !updateTextDirection (AUTO_TEXT_DIRECTION)) {
412 string = fixText (OS.IsWindowEnabled (handle));
413 TCHAR buffer = new TCHAR (getCodePage (), string == null ? text : string, true);
414 OS.SetWindowText (handle, buffer);
419 boolean updateTextDirection(int textDirection) {
420 if (super.updateTextDirection(textDirection)) {
421 String string = fixText (OS.IsWindowEnabled (handle));
422 TCHAR buffer = new TCHAR (getCodePage (), string == null ? text : string, true);
423 OS.SetWindowText (handle, buffer);
432 * Bug in Windows. When GetDCEx() is called with DCX_INTERSECTUPDATE,
433 * the HDC that is returned does not include the current update region.
434 * This was confirmed under DEBUG Windows when GetDCEx() complained about
435 * invalid flags. Therefore, it is not easily possible to get an HDC from
436 * outside of WM_PAINT that includes the current damage and clips children.
437 * Because the receiver has children and draws a frame and label, it is
438 * necessary that the receiver always draw clipped, in the current damaged
439 * area. The fix is to force the receiver to be fully clipped by including
440 * WS_CLIPCHILDREN and WS_CLIPSIBLINGS in the default style bits.
442 return super.widgetStyle () | OS.BS_GROUPBOX | OS.WS_CLIPCHILDREN | OS.WS_CLIPSIBLINGS;
446 TCHAR windowClass () {
456 LRESULT WM_ERASEBKGND (long wParam, long lParam) {
457 LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
458 if (result != null) return result;
460 * Feature in Windows. Group boxes do not erase
461 * the background before drawing. The fix is to
462 * fill the background.
464 drawBackground (wParam);
469 LRESULT WM_NCHITTEST (long wParam, long lParam) {
470 LRESULT result = super.WM_NCHITTEST (wParam, lParam);
471 if (result != null) return result;
473 * Feature in Windows. The window proc for the group box
474 * returns HTTRANSPARENT indicating that mouse messages
475 * should not be delivered to the receiver and any children.
476 * Normally, group boxes in Windows do not have children and
477 * this is the correct behavior for this case. Because we
478 * allow children, answer HTCLIENT to allow mouse messages
479 * to be delivered to the children.
481 long code = callWindowProc (handle, OS.WM_NCHITTEST, wParam, lParam);
482 if (code == OS.HTTRANSPARENT) code = OS.HTCLIENT;
483 return new LRESULT (code);
487 LRESULT WM_MOUSEMOVE (long wParam, long lParam) {
488 LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
489 if (result != null) return result;
491 * Feature in Windows. In version 6.00 of COMCTL32.DLL,
492 * every time the mouse moves, the group title redraws.
493 * This only happens when WM_NCHITTEST returns HTCLIENT.
494 * The fix is to avoid calling the group window proc.
500 LRESULT WM_PAINT (long wParam, long lParam) {
501 LRESULT result = super.WM_PAINT(wParam, lParam);
503 if (hasCustomForeground() && text.length () != 0) {
504 String string = fixText (false);
505 char [] buffer = (string == null ? text : string).toCharArray ();
507 // We cannot use BeginPaint and EndPaint, because that removes the group border
508 long hDC = OS.GetDC(handle);
509 RECT rect = new RECT ();
510 OS.GetClientRect (handle, rect);
511 rect.left += 3*CLIENT_INSET;
513 long newFont, oldFont = 0;
514 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
515 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
517 OS.DrawText(hDC, buffer, buffer.length, rect, OS.DT_SINGLELINE | OS.DT_LEFT | OS.DT_TOP | OS.DT_CALCRECT);
518 // The calculated rectangle is a little bit too small. Italic fonts would show some small part in the default color.
519 rect.right += CLIENT_INSET;
520 drawBackground(hDC, rect);
521 OS.SetBkMode(hDC, OS.TRANSPARENT);
522 OS.SetTextColor(hDC, getForegroundPixel());
523 OS.DrawText(hDC, buffer, buffer.length, rect, OS.DT_SINGLELINE | OS.DT_LEFT | OS.DT_TOP);
525 if (newFont != 0) OS.SelectObject (hDC, oldFont);
526 OS.ReleaseDC(handle, hDC);
527 // Without validating the drawn area it would be overdrawn by windows
528 OS.ValidateRect(handle, rect);
534 LRESULT WM_PRINTCLIENT (long wParam, long lParam) {
535 LRESULT result = super.WM_PRINTCLIENT (wParam, lParam);
536 if (result != null) return result;
538 * Feature in Windows. In version 6.00 of COMCTL32.DLL,
539 * when WM_PRINTCLIENT is sent from a child BS_GROUP
540 * control to a parent BS_GROUP, the parent BS_GROUP
541 * clears the font from the HDC. Normally, group boxes
542 * in Windows do not have children so this behavior is
543 * undefined. When the parent of a BS_GROUP is not a
544 * BS_GROUP, there is no problem. The fix is to save
545 * and restore the current font.
547 if (OS.IsAppThemed ()) {
548 int nSavedDC = OS.SaveDC (wParam);
549 long code = callWindowProc (handle, OS.WM_PRINTCLIENT, wParam, lParam);
550 OS.RestoreDC (wParam, nSavedDC);
551 return new LRESULT (code);
557 LRESULT WM_UPDATEUISTATE (long wParam, long lParam) {
558 LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
559 if (result != null) return result;
561 * Feature in Windows. When WM_UPDATEUISTATE is sent to
562 * a group, it sends WM_CTLCOLORBTN to get the foreground
563 * and background. If drawing happens in WM_CTLCOLORBTN,
564 * it will overwrite the contents of the control. The
565 * fix is draw the group without drawing the background
566 * and avoid the group window proc.
568 boolean redraw = findImageControl () != null;
570 if ((state & THEME_BACKGROUND) != 0) {
571 if (OS.IsAppThemed ()) {
572 redraw = findThemeControl () != null;
575 if (!redraw) redraw = findBackgroundControl () != null;
578 OS.InvalidateRect (handle, null, false);
579 long code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
580 return new LRESULT (code);
586 LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
587 LRESULT result = super.WM_WINDOWPOSCHANGING (wParam, lParam);
588 if (result != null) return result;
590 * Invalidate the portion of the group widget that needs to
591 * be redrawn. Note that for some reason, invalidating the
592 * group from inside WM_SIZE causes pixel corruption for
593 * radio button children.
595 if (!OS.IsWindowVisible (handle)) return result;
596 WINDOWPOS lpwp = new WINDOWPOS ();
597 OS.MoveMemory (lpwp, lParam, WINDOWPOS.sizeof);
598 if ((lpwp.flags & (OS.SWP_NOSIZE | OS.SWP_NOREDRAW)) != 0) {
601 RECT rect = new RECT ();
602 OS.SetRect (rect, 0, 0, lpwp.cx, lpwp.cy);
603 OS.SendMessage (handle, OS.WM_NCCALCSIZE, 0, rect);
604 int newWidth = rect.right - rect.left;
605 int newHeight = rect.bottom - rect.top;
606 OS.GetClientRect (handle, rect);
607 int oldWidth = rect.right - rect.left;
608 int oldHeight = rect.bottom - rect.top;
609 if (newWidth == oldWidth && newHeight == oldHeight) {
612 if (newWidth != oldWidth) {
614 if (newWidth < oldWidth) left = newWidth;
615 OS.SetRect (rect, left - CLIENT_INSET, 0, newWidth, newHeight);
616 OS.InvalidateRect (handle, rect, true);
618 if (newHeight != oldHeight) {
619 int bottom = oldHeight;
620 if (newHeight < oldHeight) bottom = newHeight;
621 if (newWidth < oldWidth) oldWidth -= CLIENT_INSET;
622 OS.SetRect (rect, 0, bottom - CLIENT_INSET, oldWidth, newHeight);
623 OS.InvalidateRect (handle, rect, true);