1 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 38 39 public class Group extends Composite { 40 String 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 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 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 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 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 160 String string = text; 161 if ((style & SWT.RIGHT_TO_LEFT) != 0) { 162 if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { 163 string = " " + string + " "; 164 } 165 } 166 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 216 if ((style & SWT.RIGHT_TO_LEFT) != 0) { 217 if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { 218 String 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 getNameText () { 245 return getText (); 246 } 247 248 260 public String 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 312 public void setText (String string) { 313 checkWidget (); 314 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 315 text = string; 316 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 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 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 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 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 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 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 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 |