Changeset 51:6e4e3a8e4971
- Timestamp:
- 02/02/08 21:43:52
(1 year ago)
- Author:
- Frank Benoit <benoit@tionex.de>
- branch:
- default
- Message:
Label
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r31 |
r51 |
|
| 13 | 13 | module dwt.widgets.Label; |
|---|
| 14 | 14 | |
|---|
| 15 | | import dwt.widgets.Control; |
|---|
| 16 | | import dwt.widgets.Composite; |
|---|
| 17 | | class Label : Control { |
|---|
| 18 | | this (Composite parent, int style) ; |
|---|
| 19 | | } |
|---|
| 20 | | /++ |
|---|
| | 15 | |
|---|
| 21 | 16 | import dwt.DWT; |
|---|
| 22 | 17 | import dwt.DWTException; |
|---|
| … | … | |
| 26 | 21 | import dwt.graphics.Point; |
|---|
| 27 | 22 | import dwt.graphics.Rectangle; |
|---|
| 28 | | import dwt.internal.win32.DRAWITEMSTRUCT; |
|---|
| 29 | | import dwt.internal.win32.LRESULT; |
|---|
| 30 | 23 | import dwt.internal.win32.OS; |
|---|
| 31 | | import dwt.internal.win32.PAINTSTRUCT; |
|---|
| 32 | | import dwt.internal.win32.RECT; |
|---|
| 33 | | import dwt.internal.win32.TCHAR; |
|---|
| 34 | | import dwt.internal.win32.TEXTMETRIC; |
|---|
| 35 | | import dwt.internal.win32.TEXTMETRICA; |
|---|
| 36 | | import dwt.internal.win32.TEXTMETRICW; |
|---|
| 37 | | import dwt.internal.win32.WNDCLASS; |
|---|
| | 24 | |
|---|
| | 25 | import dwt.widgets.Control; |
|---|
| | 26 | import dwt.widgets.Composite; |
|---|
| | 27 | import dwt.widgets.Display; |
|---|
| | 28 | import dwt.widgets.Event; |
|---|
| | 29 | |
|---|
| | 30 | import dwt.dwthelper.utils; |
|---|
| 38 | 31 | |
|---|
| 39 | 32 | /** |
|---|
| … | … | |
| 59 | 52 | * </p> |
|---|
| 60 | 53 | */ |
|---|
| 61 | | public class Label extends Control { |
|---|
| 62 | | String text = ""; |
|---|
| | 54 | public class Label : Control { |
|---|
| | 55 | char[] text = ""; |
|---|
| 63 | 56 | Image image; |
|---|
| 64 | | static final int MARGIN = 4; |
|---|
| 65 | | static final bool IMAGE_AND_TEXT = false; |
|---|
| 66 | | static final int LabelProc; |
|---|
| 67 | | static final TCHAR LabelClass = new TCHAR (0, "STATIC", true); |
|---|
| 68 | | static { |
|---|
| 69 | | WNDCLASS lpWndClass = new WNDCLASS (); |
|---|
| 70 | | OS.GetClassInfo (0, LabelClass, lpWndClass); |
|---|
| | 57 | static const int MARGIN = 4; |
|---|
| | 58 | static const bool IMAGE_AND_TEXT = false; |
|---|
| | 59 | static const WNDPROC LabelProc; |
|---|
| | 60 | static const TCHAR[] LabelClass = "STATIC\0"; |
|---|
| | 61 | static this() { |
|---|
| | 62 | WNDCLASS lpWndClass; |
|---|
| | 63 | OS.GetClassInfo (null, LabelClass.ptr, &lpWndClass); |
|---|
| 71 | 64 | LabelProc = lpWndClass.lpfnWndProc; |
|---|
| 72 | 65 | } |
|---|
| … | … | |
| 109 | 102 | * @see Widget#getStyle |
|---|
| 110 | 103 | */ |
|---|
| 111 | | public Label (Composite parent, int style) { |
|---|
| | 104 | public this (Composite parent, int style) { |
|---|
| 112 | 105 | super (parent, checkStyle (style)); |
|---|
| 113 | 106 | } |
|---|
| 114 | 107 | |
|---|
| 115 | | int callWindowProc (int hwnd, int msg, int wParam, int lParam) { |
|---|
| 116 | | if (handle is 0) return 0; |
|---|
| 117 | | return OS.CallWindowProc (LabelProc, hwnd, msg, wParam, lParam); |
|---|
| | 108 | LRESULT callWindowProc (HWND hwnd, int msg, int wParam, int lParam) { |
|---|
| | 109 | if (handle is null) return LRESULT.NULL; |
|---|
| | 110 | return cast(LRESULT) OS.CallWindowProc (LabelProc, hwnd, msg, wParam, lParam); |
|---|
| 118 | 111 | } |
|---|
| 119 | 112 | |
|---|
| … | … | |
| 151 | 144 | height += rect.height; |
|---|
| 152 | 145 | if (IMAGE_AND_TEXT) { |
|---|
| 153 | | if (text.length () !is 0) width += MARGIN; |
|---|
| | 146 | if (text.length !is 0) width += MARGIN; |
|---|
| 154 | 147 | } else { |
|---|
| 155 | 148 | drawText = false; |
|---|
| … | … | |
| 158 | 151 | } |
|---|
| 159 | 152 | if (drawText) { |
|---|
| 160 | | int hDC = OS.GetDC (handle); |
|---|
| 161 | | int newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); |
|---|
| 162 | | int oldFont = OS.SelectObject (hDC, newFont); |
|---|
| | 153 | auto hDC = OS.GetDC (handle); |
|---|
| | 154 | auto newFont = cast(HFONT) OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); |
|---|
| | 155 | auto oldFont = OS.SelectObject (hDC, newFont); |
|---|
| 163 | 156 | int length = OS.GetWindowTextLength (handle); |
|---|
| 164 | 157 | if (length is 0) { |
|---|
| 165 | | TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA (); |
|---|
| 166 | | OS.GetTextMetrics (hDC, tm); |
|---|
| | 158 | TEXTMETRIC tm; |
|---|
| | 159 | OS.GetTextMetrics (hDC, &tm); |
|---|
| 167 | 160 | height = Math.max (height, tm.tmHeight); |
|---|
| 168 | 161 | } else { |
|---|
| 169 | | RECT rect = new RECT (); |
|---|
| | 162 | RECT rect; |
|---|
| 170 | 163 | int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS; |
|---|
| 171 | 164 | if ((style & DWT.WRAP) !is 0 && wHint !is DWT.DEFAULT) { |
|---|
| … | … | |
| 173 | 166 | rect.right = Math.max (0, wHint - width); |
|---|
| 174 | 167 | } |
|---|
| 175 | | TCHAR buffer = new TCHAR (getCodePage (), length + 1); |
|---|
| 176 | | OS.GetWindowText (handle, buffer, length + 1); |
|---|
| 177 | | OS.DrawText (hDC, buffer, length, rect, flags); |
|---|
| | 168 | TCHAR[] buffer = new TCHAR [/+getCodePage (),+/ length + 1]; |
|---|
| | 169 | OS.GetWindowText (handle, buffer.ptr, length + 1); |
|---|
| | 170 | OS.DrawText (hDC, buffer.ptr, length, &rect, flags); |
|---|
| 178 | 171 | width += rect.right - rect.left; |
|---|
| 179 | 172 | height = Math.max (height, rect.bottom - rect.top); |
|---|
| 180 | 173 | } |
|---|
| 181 | | if (newFont !is 0) OS.SelectObject (hDC, oldFont); |
|---|
| | 174 | if (newFont !is null) OS.SelectObject (hDC, oldFont); |
|---|
| 182 | 175 | OS.ReleaseDC (handle, hDC); |
|---|
| 183 | 176 | } |
|---|
| … | … | |
| 240 | 233 | } |
|---|
| 241 | 234 | |
|---|
| 242 | | String getNameText () { |
|---|
| | 235 | char[] getNameText () { |
|---|
| 243 | 236 | return getText (); |
|---|
| 244 | 237 | } |
|---|
| … | … | |
| 256 | 249 | * </ul> |
|---|
| 257 | 250 | */ |
|---|
| 258 | | public String getText () { |
|---|
| | 251 | public char[] getText () { |
|---|
| 259 | 252 | checkWidget (); |
|---|
| 260 | 253 | if ((style & DWT.SEPARATOR) !is 0) return ""; |
|---|
| … | … | |
| 262 | 255 | } |
|---|
| 263 | 256 | |
|---|
| 264 | | bool mnemonicHit (char key) { |
|---|
| | 257 | bool mnemonicHit (wchar key) { |
|---|
| 265 | 258 | Composite control = this.parent; |
|---|
| 266 | 259 | while (control !is null) { |
|---|
| … | … | |
| 280 | 273 | } |
|---|
| 281 | 274 | |
|---|
| 282 | | bool mnemonicMatch (char key) { |
|---|
| 283 | | char mnemonic = findMnemonic (getText ()); |
|---|
| | 275 | bool mnemonicMatch (wchar key) { |
|---|
| | 276 | wchar mnemonic = findMnemonic (getText ()); |
|---|
| 284 | 277 | if (mnemonic is '\0') return false; |
|---|
| 285 | | return Character.toUpperCase (key) is Character.toUpperCase (mnemonic); |
|---|
| | 278 | return CharacterToUpper (key) is CharacterToUpper (mnemonic); |
|---|
| 286 | 279 | } |
|---|
| 287 | 280 | |
|---|
| … | … | |
| 383 | 376 | * </ul> |
|---|
| 384 | 377 | */ |
|---|
| 385 | | public void setText (String string) { |
|---|
| | 378 | public void setText (char[] string) { |
|---|
| 386 | 379 | checkWidget (); |
|---|
| 387 | 380 | if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
|---|
| … | … | |
| 393 | 386 | * nothing. |
|---|
| 394 | 387 | */ |
|---|
| 395 | | if (string.equals (text)) return; |
|---|
| | 388 | if (string==/*eq*/text) return; |
|---|
| 396 | 389 | text = string; |
|---|
| 397 | 390 | if (image is null || !IMAGE_AND_TEXT) { |
|---|
| … | … | |
| 410 | 403 | } |
|---|
| 411 | 404 | string = Display.withCrLf (string); |
|---|
| 412 | | TCHAR buffer = new TCHAR (getCodePage (), string, true); |
|---|
| | 405 | TCHAR* buffer = StrToTCHARz (/+getCodePage (),+/ string); |
|---|
| 413 | 406 | OS.SetWindowText (handle, buffer); |
|---|
| 414 | 407 | /* |
|---|
| … | … | |
| 443 | 436 | } |
|---|
| 444 | 437 | |
|---|
| 445 | | TCHAR windowClass () { |
|---|
| 446 | | return LabelClass; |
|---|
| | 438 | char[] windowClass () { |
|---|
| | 439 | return TCHARsToStr( LabelClass ); |
|---|
| 447 | 440 | } |
|---|
| 448 | 441 | |
|---|
| 449 | 442 | int windowProc () { |
|---|
| 450 | | return LabelProc; |
|---|
| | 443 | return cast(int) LabelProc; |
|---|
| 451 | 444 | } |
|---|
| 452 | 445 | |
|---|
| 453 | 446 | LRESULT WM_ERASEBKGND (int wParam, int lParam) { |
|---|
| 454 | 447 | LRESULT result = super.WM_ERASEBKGND (wParam, lParam); |
|---|
| 455 | | if (result !is null) return result; |
|---|
| | 448 | if (result !is LRESULT.NULL) return result; |
|---|
| 456 | 449 | int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); |
|---|
| 457 | 450 | if ((bits & OS.SS_OWNERDRAW) is OS.SS_OWNERDRAW) { |
|---|
| … | … | |
| 468 | 461 | if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) { |
|---|
| 469 | 462 | if (findImageControl () !is null) { |
|---|
| 470 | | drawBackground (wParam); |
|---|
| | 463 | drawBackground (cast(HANDLE)wParam); |
|---|
| 471 | 464 | return LRESULT.ONE; |
|---|
| 472 | 465 | } |
|---|
| … | … | |
| 522 | 515 | OS.InvalidateRect (handle, null, false); |
|---|
| 523 | 516 | int code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam); |
|---|
| 524 | | return new LRESULT (code); |
|---|
| | 517 | return cast(LRESULT) (code); |
|---|
| 525 | 518 | } |
|---|
| 526 | 519 | return result; |
|---|
| … | … | |
| 541 | 534 | if ((bits & OS.SS_OWNERDRAW) !is OS.SS_OWNERDRAW) { |
|---|
| 542 | 535 | if (findImageControl () !is null) { |
|---|
| 543 | | OS.SetBkMode (wParam, OS.TRANSPARENT); |
|---|
| 544 | | return new LRESULT (OS.GetStockObject (OS.NULL_BRUSH)); |
|---|
| | 536 | OS.SetBkMode ( cast(HANDLE) wParam, OS.TRANSPARENT); |
|---|
| | 537 | return cast(LRESULT) (OS.GetStockObject (OS.NULL_BRUSH)); |
|---|
| 545 | 538 | } |
|---|
| 546 | 539 | } |
|---|
| … | … | |
| 554 | 547 | bool drawSeparator = (style & DWT.SEPARATOR) !is 0 && (style & DWT.SHADOW_NONE) is 0; |
|---|
| 555 | 548 | if (drawImage || drawSeparator) { |
|---|
| 556 | | LRESULT result = null; |
|---|
| 557 | | PAINTSTRUCT ps = new PAINTSTRUCT (); |
|---|
| | 549 | LRESULT result = LRESULT.NULL; |
|---|
| | 550 | PAINTSTRUCT ps; |
|---|
| 558 | 551 | GCData data = new GCData (); |
|---|
| 559 | | data.ps = ps; |
|---|
| | 552 | data.ps = &ps; |
|---|
| 560 | 553 | data.hwnd = handle; |
|---|
| 561 | 554 | GC gc = new_GC (data); |
|---|
| 562 | 555 | if (gc !is null) { |
|---|
| 563 | 556 | drawBackground (gc.handle); |
|---|
| 564 | | RECT clientRect = new RECT(); |
|---|
| 565 | | OS.GetClientRect (handle, clientRect); |
|---|
| | 557 | RECT clientRect; |
|---|
| | 558 | OS.GetClientRect (handle, &clientRect); |
|---|
| 566 | 559 | if (drawSeparator) { |
|---|
| 567 | | RECT rect = new RECT (); |
|---|
| | 560 | RECT rect; |
|---|
| 568 | 561 | int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER); |
|---|
| 569 | 562 | int flags = (style & DWT.SHADOW_IN) !is 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED; |
|---|
| 570 | 563 | if ((style & DWT.HORIZONTAL) !is 0) { |
|---|
| 571 | 564 | int bottom = clientRect.top + Math.max (lineWidth * 2, (clientRect.bottom - clientRect.top) / 2); |
|---|
| 572 | | OS.SetRect (rect, clientRect.left, clientRect.top, clientRect.right, bottom); |
|---|
| 573 | | OS.DrawEdge (gc.handle, rect, flags, OS.BF_BOTTOM); |
|---|
| | 565 | OS.SetRect (&rect, clientRect.left, clientRect.top, clientRect.right, bottom); |
|---|
| | 566 | OS.DrawEdge (gc.handle, &rect, flags, OS.BF_BOTTOM); |
|---|
| 574 | 567 | } else { |
|---|
| 575 | 568 | int right = clientRect.left + Math.max (lineWidth * 2, (clientRect.right - clientRect.left) / 2); |
|---|
| 576 | | OS.SetRect (rect, clientRect.left, clientRect.top, right, clientRect.bottom); |
|---|
| 577 | | OS.DrawEdge (gc.handle, rect, flags, OS.BF_RIGHT); |
|---|
| | 569 | OS.SetRect (&rect, clientRect.left, clientRect.top, right, clientRect.bottom); |
|---|
| | 570 | OS.DrawEdge (gc.handle, &rect, flags, OS.BF_RIGHT); |
|---|
| 578 | 571 | } |
|---|
| 579 | 572 | result = LRESULT.ONE; |
|---|
| … | … | |
| 592 | 585 | result = LRESULT.ONE; |
|---|
| 593 | 586 | } |
|---|
| 594 | | int width = ps.right - ps.left; |
|---|
| 595 | | int height = ps.bottom - ps.top; |
|---|
| | 587 | int width = ps.rcPaint.right - ps.rcPaint.left; |
|---|
| | 588 | int height = ps.rcPaint.bottom - ps.rcPaint.top; |
|---|
| 596 | 589 | if (width !is 0 && height !is 0) { |
|---|
| 597 | 590 | Event event = new Event (); |
|---|
| 598 | 591 | event.gc = gc; |
|---|
| 599 | | event.x = ps.left; |
|---|
| 600 | | event.y = ps.top; |
|---|
| | 592 | event.x = ps.rcPaint.left; |
|---|
| | 593 | event.y = ps.rcPaint.top; |
|---|
| 601 | 594 | event.width = width; |
|---|
| 602 | 595 | event.height = height; |
|---|
| … | … | |
| 614 | 607 | |
|---|
| 615 | 608 | LRESULT wmDrawChild (int wParam, int lParam) { |
|---|
| 616 | | DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT (); |
|---|
| 617 | | OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof); |
|---|
| 618 | | drawBackground (struct.hDC); |
|---|
| | 609 | DRAWITEMSTRUCT* struct_ = cast(DRAWITEMSTRUCT*)lParam; |
|---|
| | 610 | drawBackground (struct_.hDC); |
|---|
| 619 | 611 | if ((style & DWT.SEPARATOR) !is 0) { |
|---|
| 620 | | if ((style & DWT.SHADOW_NONE) !is 0) return null; |
|---|
| 621 | | RECT rect = new RECT (); |
|---|
| | 612 | if ((style & DWT.SHADOW_NONE) !is 0) return LRESULT.NULL; |
|---|
| | 613 | RECT rect; |
|---|
| 622 | 614 | int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER); |
|---|
| 623 | 615 | int flags = (style & DWT.SHADOW_IN) !is 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED; |
|---|
| 624 | 616 | if ((style & DWT.HORIZONTAL) !is 0) { |
|---|
| 625 | | int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2); |
|---|
| 626 | | OS.SetRect (rect, struct.left, struct.top, struct.right, bottom); |
|---|
| 627 | | OS.DrawEdge (struct.hDC, rect, flags, OS.BF_BOTTOM); |
|---|
| | 617 | int bottom = struct_.rcItem.top + Math.max (lineWidth * 2, (struct_.rcItem.bottom - struct_.rcItem.top) / 2); |
|---|
| | 618 | OS.SetRect (&rect, struct_.rcItem.left, struct_.rcItem.top, struct_.rcItem.right, bottom); |
|---|
| | 619 | OS.DrawEdge (struct_.hDC, &rect, flags, OS.BF_BOTTOM); |
|---|
| 628 | 620 | } else { |
|---|
| 629 | | int right = struct.left + Math.max (lineWidth * 2, (struct.right - struct.left) / 2); |
|---|
| 630 | | OS.SetRect (rect, struct.left, struct.top, right, struct.bottom); |
|---|
| 631 | | OS.DrawEdge (struct.hDC, rect, flags, OS.BF_RIGHT); |
|---|
| | 621 | int right = struct_.rcItem.left + Math.max (lineWidth * 2, (struct_.rcItem.right - struct_.rcItem.left) / 2); |
|---|
| | 622 | OS.SetRect (&rect, struct_.rcItem.left, struct_.rcItem.top, right, struct_.rcItem.bottom); |
|---|
| | 623 | OS.DrawEdge (struct_.hDC, &rect, flags, OS.BF_RIGHT); |
|---|
| 632 | 624 | } |
|---|
| 633 | 625 | } else { |
|---|
| 634 | | int width = struct.right - struct.left; |
|---|
| 635 | | int height = struct.bottom - struct.top; |
|---|
| | 626 | int width = struct_.rcItem.right - struct_.rcItem.left; |
|---|
| | 627 | int height = struct_.rcItem.bottom - struct_.rcItem.top; |
|---|
| 636 | 628 | if (width !is 0 && height !is 0) { |
|---|
| 637 | 629 | bool drawImage = image !is null; |
|---|
| 638 | | bool drawText = IMAGE_AND_TEXT && text.length () !is 0; |
|---|
| | 630 | bool drawText = IMAGE_AND_TEXT && text.length !is 0; |
|---|
| 639 | 631 | int margin = drawText && drawImage ? MARGIN : 0; |
|---|
| 640 | 632 | int imageWidth = 0, imageHeight = 0; |
|---|
| … | … | |
| 644 | 636 | imageHeight = rect.height; |
|---|
| 645 | 637 | } |
|---|
| 646 | | RECT rect = null; |
|---|
| 647 | | TCHAR buffer = null; |
|---|
| | 638 | RECT rect; |
|---|
| | 639 | TCHAR* buffer = null; |
|---|
| 648 | 640 | int textWidth = 0, textHeight = 0, flags = 0; |
|---|
| 649 | 641 | if (drawText) { |
|---|
| 650 | | rect = new RECT (); |
|---|
| | 642 | //rect = new RECT (); |
|---|
| 651 | 643 | flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS; |
|---|
| 652 | 644 | if ((style & DWT.LEFT) !is 0) flags |= OS.DT_LEFT; |
|---|
| … | … | |
| 657 | 649 | rect.right = Math.max (0, width - imageWidth - margin); |
|---|
| 658 | 650 | } |
|---|
| 659 | | buffer = new TCHAR (getCodePage (), text, true); |
|---|
| 660 | | OS.DrawText (struct.hDC, buffer, -1, rect, flags); |
|---|
| | 651 | buffer = StrToTCHARz (/+getCodePage (),+/ text); |
|---|
| | 652 | OS.DrawText (struct_.hDC, buffer, -1, &rect, flags); |
|---|
| 661 | 653 | textWidth = rect.right - rect.left; |
|---|
| 662 | 654 | textHeight = rect.bottom - rect.top; |
|---|
| … | … | |
| 673 | 665 | GCData data = new GCData(); |
|---|
| 674 | 666 | data.device = display; |
|---|
| 675 | | GC gc = GC.win32_new (struct.hDC, data); |
|---|
| | 667 | GC gc = GC.win32_new (struct_.hDC, data); |
|---|
| 676 | 668 | Image image = getEnabled () ? this.image : new Image (display, this.image, DWT.IMAGE_DISABLE); |
|---|
| 677 | 669 | gc.drawImage (image, x, Math.max (0, (height - imageHeight) / 2)); |
|---|
| … | … | |
| 686 | 678 | rect.top = Math.max (0, (height - textHeight) / 2); |
|---|
| 687 | 679 | rect.bottom += rect.top; |
|---|
| 688 | | OS.DrawText (struct.hDC, buffer, -1, rect, flags); |
|---|
| 689 | | } |
|---|
| 690 | | } |
|---|
| 691 | | } |
|---|
| 692 | | return null; |
|---|
| 693 | | } |
|---|
| 694 | | |
|---|
| 695 | | } |
|---|
| 696 | | ++/ |
|---|
| | 680 | OS.DrawText (struct_.hDC, buffer, -1, &rect, flags); |
|---|
| | 681 | } |
|---|
| | 682 | } |
|---|
| | 683 | } |
|---|
| | 684 | return LRESULT.NULL; |
|---|
| | 685 | } |
|---|
| | 686 | |
|---|
| | 687 | } |
|---|
| | 688 | |
|---|