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 40 public class Label extends Control { 41 String text = ""; 42 Image image; 43 static final int MARGIN = 4; 44 static final boolean IMAGE_AND_TEXT = false; 45 static final int LabelProc; 46 static final TCHAR LabelClass = new TCHAR (0, "STATIC", true); 47 static { 48 WNDCLASS lpWndClass = new WNDCLASS (); 49 OS.GetClassInfo (0, LabelClass, lpWndClass); 50 LabelProc = lpWndClass.lpfnWndProc; 51 } 52 53 90 public Label (Composite parent, int style) { 91 super (parent, checkStyle (style)); 92 } 93 94 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 95 if (handle == 0) return 0; 96 return OS.CallWindowProc (LabelProc, hwnd, msg, wParam, lParam); 97 } 98 99 static int checkStyle (int style) { 100 style |= SWT.NO_FOCUS; 101 if ((style & SWT.SEPARATOR) != 0) { 102 style = checkBits (style, SWT.VERTICAL, SWT.HORIZONTAL, 0, 0, 0, 0); 103 return checkBits (style, SWT.SHADOW_OUT, SWT.SHADOW_IN, SWT.SHADOW_NONE, 0, 0, 0); 104 } 105 return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); 106 } 107 108 public Point computeSize (int wHint, int hHint, boolean changed) { 109 checkWidget (); 110 int width = 0, height = 0, border = getBorderWidth (); 111 if ((style & SWT.SEPARATOR) != 0) { 112 int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER); 113 if ((style & SWT.HORIZONTAL) != 0) { 114 width = DEFAULT_WIDTH; height = lineWidth * 2; 115 } else { 116 width = lineWidth * 2; height = DEFAULT_HEIGHT; 117 } 118 if (wHint != SWT.DEFAULT) width = wHint; 119 if (hHint != SWT.DEFAULT) height = hHint; 120 width += border * 2; height += border * 2; 121 return new Point (width, height); 122 } 123 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 124 boolean drawText = true; 125 boolean drawImage = (bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW; 126 if (drawImage) { 127 if (image != null) { 128 Rectangle rect = image.getBounds(); 129 width += rect.width; 130 height += rect.height; 131 if (IMAGE_AND_TEXT) { 132 if (text.length () != 0) width += MARGIN; 133 } else { 134 drawText = false; 135 } 136 } 137 } 138 if (drawText) { 139 int hDC = OS.GetDC (handle); 140 int newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 141 int oldFont = OS.SelectObject (hDC, newFont); 142 int length = OS.GetWindowTextLength (handle); 143 if (length == 0) { 144 TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA (); 145 OS.GetTextMetrics (hDC, tm); 146 height = Math.max (height, tm.tmHeight); 147 } else { 148 RECT rect = new RECT (); 149 int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS; 150 if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) { 151 flags |= OS.DT_WORDBREAK; 152 rect.right = Math.max (0, wHint - width); 153 } 154 TCHAR buffer = new TCHAR (getCodePage (), length + 1); 155 OS.GetWindowText (handle, buffer, length + 1); 156 OS.DrawText (hDC, buffer, length, rect, flags); 157 width += rect.right - rect.left; 158 height = Math.max (height, rect.bottom - rect.top); 159 } 160 if (newFont != 0) OS.SelectObject (hDC, oldFont); 161 OS.ReleaseDC (handle, hDC); 162 } 163 if (wHint != SWT.DEFAULT) width = wHint; 164 if (hHint != SWT.DEFAULT) height = hHint; 165 width += border * 2; 166 height += border * 2; 167 173 if (OS.IsWinCE && !drawImage) width += 2; 174 return new Point (width, height); 175 } 176 177 void createHandle () { 178 super.createHandle (); 179 state |= THEME_BACKGROUND; 180 } 181 182 196 public int getAlignment () { 197 checkWidget (); 198 if ((style & SWT.SEPARATOR) != 0) return 0; 199 if ((style & SWT.LEFT) != 0) return SWT.LEFT; 200 if ((style & SWT.CENTER) != 0) return SWT.CENTER; 201 if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; 202 return SWT.LEFT; 203 } 204 205 216 public Image getImage () { 217 checkWidget (); 218 return image; 219 } 220 221 String getNameText () { 222 return getText (); 223 } 224 225 237 public String getText () { 238 checkWidget (); 239 if ((style & SWT.SEPARATOR) != 0) return ""; 240 return text; 241 } 242 243 boolean mnemonicHit (char key) { 244 Composite control = this.parent; 245 while (control != null) { 246 Control [] children = control._getChildren (); 247 int index = 0; 248 while (index < children.length) { 249 if (children [index] == this) break; 250 index++; 251 } 252 index++; 253 if (index < children.length) { 254 if (children [index].setFocus ()) return true; 255 } 256 control = control.parent; 257 } 258 return false; 259 } 260 261 boolean mnemonicMatch (char key) { 262 char mnemonic = findMnemonic (getText ()); 263 if (mnemonic == '\0') return false; 264 return Character.toUpperCase (key) == Character.toUpperCase (mnemonic); 265 } 266 267 void releaseWidget () { 268 super.releaseWidget (); 269 text = null; 270 image = null; 271 } 272 273 286 public void setAlignment (int alignment) { 287 checkWidget (); 288 if ((style & SWT.SEPARATOR) != 0) return; 289 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return; 290 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); 291 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); 292 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 293 if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) { 294 bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT); 295 if ((style & SWT.LEFT) != 0) { 296 if ((style & SWT.WRAP) != 0) { 297 bits |= OS.SS_LEFT; 298 } else { 299 bits |= OS.SS_LEFTNOWORDWRAP; 300 } 301 } 302 if ((style & SWT.CENTER) != 0) bits |= OS.SS_CENTER; 303 if ((style & SWT.RIGHT) != 0) bits |= OS.SS_RIGHT; 304 OS.SetWindowLong (handle, OS.GWL_STYLE, bits); 305 } 306 OS.InvalidateRect (handle, null, true); 307 } 308 309 323 public void setImage (Image image) { 324 checkWidget (); 325 if ((style & SWT.SEPARATOR) != 0) return; 326 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); 327 this.image = image; 328 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 329 if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) { 330 bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT); 331 bits |= OS.SS_OWNERDRAW; 332 OS.SetWindowLong (handle, OS.GWL_STYLE, bits); 333 } 334 OS.InvalidateRect (handle, null, true); 335 } 336 337 364 public void setText (String string) { 365 checkWidget (); 366 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 367 if ((style & SWT.SEPARATOR) != 0) return; 368 374 if (string.equals (text)) return; 375 text = string; 376 if (image == null || !IMAGE_AND_TEXT) { 377 int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits; 378 newBits &= ~OS.SS_OWNERDRAW; 379 if ((style & SWT.LEFT) != 0) { 380 if ((style & SWT.WRAP) != 0) { 381 newBits |= OS.SS_LEFT; 382 } else { 383 newBits |= OS.SS_LEFTNOWORDWRAP; 384 } 385 } 386 if ((style & SWT.CENTER) != 0) newBits |= OS.SS_CENTER; 387 if ((style & SWT.RIGHT) != 0) newBits |= OS.SS_RIGHT; 388 if (oldBits != newBits) OS.SetWindowLong (handle, OS.GWL_STYLE, newBits); 389 } 390 string = Display.withCrLf (string); 391 TCHAR buffer = new TCHAR (getCodePage (), string, true); 392 OS.SetWindowText (handle, buffer); 393 401 if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { 402 if (findImageControl () != null) OS.InvalidateRect (handle, null, true); 403 } 404 } 405 406 int widgetExtStyle () { 407 int bits = super.widgetExtStyle () & ~OS.WS_EX_CLIENTEDGE; 408 if ((style & SWT.BORDER) != 0) return bits | OS.WS_EX_STATICEDGE; 409 return bits; 410 } 411 412 int widgetStyle () { 413 int bits = super.widgetStyle () | OS.SS_NOTIFY; 414 if ((style & SWT.SEPARATOR) != 0) return bits | OS.SS_OWNERDRAW; 415 if (OS.WIN32_VERSION >= OS.VERSION (5, 0)) { 416 if ((style & SWT.WRAP) != 0) bits |= OS.SS_EDITCONTROL; 417 } 418 if ((style & SWT.CENTER) != 0) return bits | OS.SS_CENTER; 419 if ((style & SWT.RIGHT) != 0) return bits | OS.SS_RIGHT; 420 if ((style & SWT.WRAP) != 0) return bits | OS.SS_LEFT; 421 return bits | OS.SS_LEFTNOWORDWRAP; 422 } 423 424 TCHAR windowClass () { 425 return LabelClass; 426 } 427 428 int windowProc () { 429 return LabelProc; 430 } 431 432 LRESULT WM_ERASEBKGND (int wParam, int lParam) { 433 LRESULT result = super.WM_ERASEBKGND (wParam, lParam); 434 if (result != null) return result; 435 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 436 if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) { 437 return LRESULT.ONE; 438 } 439 447 if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { 448 if (findImageControl () != null) { 449 drawBackground (wParam); 450 return LRESULT.ONE; 451 } 452 } 453 return result; 454 } 455 456 LRESULT WM_SIZE (int wParam, int lParam) { 457 LRESULT result = super.WM_SIZE (wParam, lParam); 458 if (isDisposed ()) return result; 459 if ((style & SWT.SEPARATOR) != 0) { 460 OS.InvalidateRect (handle, null, true); 461 return result; 462 } 463 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 464 if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) { 465 OS.InvalidateRect (handle, null, true); 466 return result; 467 } 468 475 if ((bits & OS.SS_LEFTNOWORDWRAP) != OS.SS_LEFTNOWORDWRAP) { 476 OS.InvalidateRect (handle, null, true); 477 return result; 478 } 479 return result; 480 } 481 482 LRESULT WM_UPDATEUISTATE (int wParam, int lParam) { 483 LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam); 484 492 boolean redraw = findImageControl () != null; 493 if (!redraw) { 494 if ((state & THEME_BACKGROUND) != 0) { 495 if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) { 496 redraw = findThemeControl () != null; 497 } 498 } 499 } 500 if (redraw) { 501 OS.InvalidateRect (handle, null, false); 502 int code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam); 503 return new LRESULT (code); 504 } 505 return result; 506 } 507 508 LRESULT wmColorChild (int wParam, int lParam) { 509 517 LRESULT result = super.wmColorChild (wParam, lParam); 518 if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { 519 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 520 if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) { 521 if (findImageControl () != null) { 522 OS.SetBkMode (wParam, OS.TRANSPARENT); 523 return new LRESULT (OS.GetStockObject (OS.NULL_BRUSH)); 524 } 525 } 526 } 527 return result; 528 } 529 530 LRESULT WM_PAINT (int wParam, int lParam) { 531 if (OS.IsWinCE) { 532 boolean drawImage = image != null; 533 boolean drawSeparator = (style & SWT.SEPARATOR) != 0 && (style & SWT.SHADOW_NONE) == 0; 534 if (drawImage || drawSeparator) { 535 LRESULT result = null; 536 PAINTSTRUCT ps = new PAINTSTRUCT (); 537 GCData data = new GCData (); 538 data.ps = ps; 539 data.hwnd = handle; 540 GC gc = new_GC (data); 541 if (gc != null) { 542 drawBackground (gc.handle); 543 RECT clientRect = new RECT(); 544 OS.GetClientRect (handle, clientRect); 545 if (drawSeparator) { 546 RECT rect = new RECT (); 547 int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER); 548 int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED; 549 if ((style & SWT.HORIZONTAL) != 0) { 550 int bottom = clientRect.top + Math.max (lineWidth * 2, (clientRect.bottom - clientRect.top) / 2); 551 OS.SetRect (rect, clientRect.left, clientRect.top, clientRect.right, bottom); 552 OS.DrawEdge (gc.handle, rect, flags, OS.BF_BOTTOM); 553 } else { 554 int right = clientRect.left + Math.max (lineWidth * 2, (clientRect.right - clientRect.left) / 2); 555 OS.SetRect (rect, clientRect.left, clientRect.top, right, clientRect.bottom); 556 OS.DrawEdge (gc.handle, rect, flags, OS.BF_RIGHT); 557 } 558 result = LRESULT.ONE; 559 } 560 if (drawImage) { 561 Rectangle imageBounds = image.getBounds (); 562 int x = 0; 563 if ((style & SWT.CENTER) != 0) { 564 x = Math.max (0, (clientRect.right - imageBounds.width) / 2); 565 } else { 566 if ((style & SWT.RIGHT) != 0) { 567 x = Math.max (0, (clientRect.right - imageBounds.width)); 568 } 569 } 570 gc.drawImage (image, x, Math.max (0, (clientRect.bottom - imageBounds.height) / 2)); 571 result = LRESULT.ONE; 572 } 573 int width = ps.right - ps.left; 574 int height = ps.bottom - ps.top; 575 if (width != 0 && height != 0) { 576 Event event = new Event (); 577 event.gc = gc; 578 event.x = ps.left; 579 event.y = ps.top; 580 event.width = width; 581 event.height = height; 582 sendEvent (SWT.Paint, event); 583 event.gc = null; 585 } 586 gc.dispose (); 587 } 588 return result; 589 } 590 } 591 return super.WM_PAINT(wParam, lParam); 592 } 593 594 LRESULT wmDrawChild (int wParam, int lParam) { 595 DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT (); 596 OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof); 597 drawBackground (struct.hDC); 598 if ((style & SWT.SEPARATOR) != 0) { 599 if ((style & SWT.SHADOW_NONE) != 0) return null; 600 RECT rect = new RECT (); 601 int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER); 602 int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED; 603 if ((style & SWT.HORIZONTAL) != 0) { 604 int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2); 605 OS.SetRect (rect, struct.left, struct.top, struct.right, bottom); 606 OS.DrawEdge (struct.hDC, rect, flags, OS.BF_BOTTOM); 607 } else { 608 int right = struct.left + Math.max (lineWidth * 2, (struct.right - struct.left) / 2); 609 OS.SetRect (rect, struct.left, struct.top, right, struct.bottom); 610 OS.DrawEdge (struct.hDC, rect, flags, OS.BF_RIGHT); 611 } 612 } else { 613 int width = struct.right - struct.left; 614 int height = struct.bottom - struct.top; 615 if (width != 0 && height != 0) { 616 boolean drawImage = image != null; 617 boolean drawText = IMAGE_AND_TEXT && text.length () != 0; 618 int margin = drawText && drawImage ? MARGIN : 0; 619 int imageWidth = 0, imageHeight = 0; 620 if (drawImage) { 621 Rectangle rect = image.getBounds (); 622 imageWidth = rect.width; 623 imageHeight = rect.height; 624 } 625 RECT rect = null; 626 TCHAR buffer = null; 627 int textWidth = 0, textHeight = 0, flags = 0; 628 if (drawText) { 629 rect = new RECT (); 630 flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS; 631 if ((style & SWT.LEFT) != 0) flags |= OS.DT_LEFT; 632 if ((style & SWT.CENTER) != 0) flags |= OS.DT_CENTER; 633 if ((style & SWT.RIGHT) != 0) flags |= OS.DT_RIGHT; 634 if ((style & SWT.WRAP) != 0) { 635 flags |= OS.DT_WORDBREAK; 636 rect.right = Math.max (0, width - imageWidth - margin); 637 } 638 buffer = new TCHAR (getCodePage (), text, true); 639 OS.DrawText (struct.hDC, buffer, -1, rect, flags); 640 textWidth = rect.right - rect.left; 641 textHeight = rect.bottom - rect.top; 642 } 643 int x = 0; 644 if ((style & SWT.CENTER) != 0) { 645 x = Math.max (0, (width - imageWidth - textWidth - margin) / 2); 646 } else { 647 if ((style & SWT.RIGHT) != 0) { 648 x = width - imageWidth - textWidth - margin; 649 } 650 } 651 if (drawImage) { 652 GCData data = new GCData(); 653 data.device = display; 654 GC gc = GC.win32_new (struct.hDC, data); 655 Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE); 656 gc.drawImage (image, x, Math.max (0, (height - imageHeight) / 2)); 657 if (image != this.image) image.dispose (); 658 gc.dispose (); 659 x += imageWidth + margin; 660 } 661 if (drawText) { 662 flags &= ~OS.DT_CALCRECT; 663 rect.left = x; 664 rect.right += rect.left; 665 rect.top = Math.max (0, (height - textHeight) / 2); 666 rect.bottom += rect.top; 667 OS.DrawText (struct.hDC, buffer, -1, rect, flags); 668 } 669 } 670 } 671 return null; 672 } 673 674 } 675 | Popular Tags |