1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.*; 17 import org.eclipse.swt.graphics.*; 18 import org.eclipse.swt.events.*; 19 20 55 56 public class Combo extends Composite { 57 boolean noSelection, ignoreDefaultSelection, ignoreCharacter, ignoreModify; 58 int cbtHook, scrollWidth, visibleCount = 5; 59 60 64 public static final int LIMIT; 65 66 71 static { 72 LIMIT = OS.IsWinNT ? 0x7FFFFFFF : 0x7FFF; 73 } 74 75 81 static final int CBID_LIST = 1000; 82 static final int CBID_EDIT = 1001; 83 static int EditProc, ListProc; 84 85 static final int ComboProc; 86 static final TCHAR ComboClass = new TCHAR (0, "COMBOBOX", true); 87 static { 88 WNDCLASS lpWndClass = new WNDCLASS (); 89 OS.GetClassInfo (0, ComboClass, lpWndClass); 90 ComboProc = lpWndClass.lpfnWndProc; 91 } 92 93 123 public Combo (Composite parent, int style) { 124 super (parent, checkStyle (style)); 125 126 this.style |= SWT.H_SCROLL; 128 } 129 130 145 public void add (String string) { 146 checkWidget (); 147 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 148 TCHAR buffer = new TCHAR (getCodePage (), string, true); 149 int result = OS.SendMessage (handle, OS.CB_ADDSTRING, 0, buffer); 150 if (result == OS.CB_ERR) error (SWT.ERROR_ITEM_NOT_ADDED); 151 if (result == OS.CB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED); 152 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true); 153 } 154 155 178 public void add (String string, int index) { 179 checkWidget (); 180 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 181 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 182 if (!(0 <= index && index <= count)) { 183 error (SWT.ERROR_INVALID_RANGE); 184 } 185 TCHAR buffer = new TCHAR (getCodePage (), string, true); 186 int result = OS.SendMessage (handle, OS.CB_INSERTSTRING, index, buffer); 187 if (result == OS.CB_ERRSPACE || result == OS.CB_ERR) { 188 error (SWT.ERROR_ITEM_NOT_ADDED); 189 } 190 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true); 191 } 192 193 212 public void addModifyListener (ModifyListener listener) { 213 checkWidget (); 214 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 215 TypedListener typedListener = new TypedListener (listener); 216 addListener (SWT.Modify, typedListener); 217 } 218 219 243 public void addSelectionListener(SelectionListener listener) { 244 checkWidget (); 245 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 246 TypedListener typedListener = new TypedListener (listener); 247 addListener (SWT.Selection,typedListener); 248 addListener (SWT.DefaultSelection,typedListener); 249 } 250 251 272 public void addVerifyListener (VerifyListener listener) { 273 checkWidget (); 274 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 275 TypedListener typedListener = new TypedListener (listener); 276 addListener (SWT.Verify, typedListener); 277 } 278 279 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 280 if (handle == 0) return 0; 281 if (hwnd == handle) { 282 return OS.CallWindowProc (ComboProc, hwnd, msg, wParam, lParam); 283 } 284 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 285 if (hwnd == hwndText) { 286 return OS.CallWindowProc (EditProc, hwnd, msg, wParam, lParam); 287 } 288 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 289 if (hwnd == hwndList) { 290 return OS.CallWindowProc (ListProc, hwnd, msg, wParam, lParam); 291 } 292 return OS.DefWindowProc (hwnd, msg, wParam, lParam); 293 } 294 295 int CBTProc (int nCode, int wParam, int lParam) { 296 if (nCode == OS.HCBT_CREATEWND) { 297 TCHAR buffer = new TCHAR (0, 128); 298 OS.GetClassName (wParam, buffer, buffer.length ()); 299 String className = buffer.toString (0, buffer.strlen ()); 300 if (className.equals ("Edit") || className.equals ("EDIT")) { int bits = OS.GetWindowLong (wParam, OS.GWL_STYLE); 302 OS.SetWindowLong (wParam, OS.GWL_STYLE, bits & ~OS.ES_NOHIDESEL); 303 } 304 } 305 return OS.CallNextHookEx (cbtHook, nCode, wParam, lParam); 306 } 307 308 boolean checkHandle (int hwnd) { 309 return hwnd == handle || hwnd == OS.GetDlgItem (handle, CBID_EDIT) || hwnd == OS.GetDlgItem (handle, CBID_LIST); 310 } 311 312 protected void checkSubclass () { 313 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 314 } 315 316 static int checkStyle (int style) { 317 329 style &= ~SWT.BORDER; 330 331 338 style &= ~(SWT.H_SCROLL | SWT.V_SCROLL); 339 style = checkBits (style, SWT.DROP_DOWN, SWT.SIMPLE, 0, 0, 0, 0); 340 if ((style & SWT.SIMPLE) != 0) return style & ~SWT.READ_ONLY; 341 return style; 342 } 343 344 361 public void clearSelection () { 362 checkWidget (); 363 OS.SendMessage (handle, OS.CB_SETEDITSEL, 0, -1); 364 } 365 366 public Point computeSize (int wHint, int hHint, boolean changed) { 367 checkWidget (); 368 int width = 0, height = 0; 369 if (wHint == SWT.DEFAULT) { 370 int newFont, oldFont = 0; 371 int hDC = OS.GetDC (handle); 372 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 373 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 374 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 375 RECT rect = new RECT (); 376 int flags = OS.DT_CALCRECT | OS.DT_NOPREFIX; 377 if ((style & SWT.READ_ONLY) == 0) flags |= OS.DT_EDITCONTROL; 378 int length = OS.GetWindowTextLength (handle); 379 int cp = getCodePage (); 380 TCHAR buffer = new TCHAR (cp, length + 1); 381 OS.GetWindowText (handle, buffer, length + 1); 382 OS.DrawText (hDC, buffer, length, rect, flags); 383 width = Math.max (width, rect.right - rect.left); 384 if ((style & SWT.H_SCROLL) != 0) { 385 width = Math.max (width, scrollWidth); 386 } else { 387 for (int i=0; i<count; i++) { 388 length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, i, 0); 389 if (length != OS.CB_ERR) { 390 if (length + 1 > buffer.length ()) buffer = new TCHAR (cp, length + 1); 391 int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, i, buffer); 392 if (result != OS.CB_ERR) { 393 OS.DrawText (hDC, buffer, length, rect, flags); 394 width = Math.max (width, rect.right - rect.left); 395 } 396 } 397 } 398 } 399 if (newFont != 0) OS.SelectObject (hDC, oldFont); 400 OS.ReleaseDC (handle, hDC); 401 } 402 if (hHint == SWT.DEFAULT) { 403 if ((style & SWT.SIMPLE) != 0) { 404 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 405 int itemHeight = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, 0, 0); 406 height = count * itemHeight; 407 } 408 } 409 if (width == 0) width = DEFAULT_WIDTH; 410 if (height == 0) height = DEFAULT_HEIGHT; 411 if (wHint != SWT.DEFAULT) width = wHint; 412 if (hHint != SWT.DEFAULT) height = hHint; 413 if ((style & SWT.READ_ONLY) != 0) { 414 width += 8; 415 } else { 416 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 417 if (hwndText != 0) { 418 int margins = OS.SendMessage (hwndText, OS.EM_GETMARGINS, 0, 0); 419 int marginWidth = (margins & 0xFFFF) + ((margins >> 16) & 0xFFFF); 420 width += marginWidth + 3; 421 } 422 } 423 COMBOBOXINFO pcbi = new COMBOBOXINFO (); 424 pcbi.cbSize = COMBOBOXINFO.sizeof; 425 if (((style & SWT.SIMPLE) == 0) && !OS.IsWinCE && OS.GetComboBoxInfo (handle, pcbi)) { 426 width += pcbi.itemLeft + (pcbi.buttonRight - pcbi.buttonLeft); 427 height = (pcbi.buttonBottom - pcbi.buttonTop) + pcbi.buttonTop * 2; 428 } else { 429 int border = OS.GetSystemMetrics (OS.SM_CXEDGE); 430 width += OS.GetSystemMetrics (OS.SM_CXVSCROLL) + border * 2; 431 int textHeight = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, -1, 0); 432 if ((style & SWT.DROP_DOWN) != 0) { 433 height = textHeight + 6; 434 } else { 435 height += textHeight + 10; 436 } 437 } 438 if ((style & SWT.SIMPLE) != 0 && (style & SWT.H_SCROLL) != 0) { 439 height += OS.GetSystemMetrics (OS.SM_CYHSCROLL); 440 } 441 return new Point (width, height); 442 } 443 444 457 public void copy () { 458 checkWidget (); 459 OS.SendMessage (handle, OS.WM_COPY, 0, 0); 460 } 461 462 void createHandle () { 463 473 if (OS.IsWinCE || (style & (SWT.READ_ONLY | SWT.SIMPLE)) != 0) { 474 super.createHandle (); 475 } else { 476 int threadId = OS.GetCurrentThreadId (); 477 Callback cbtCallback = new Callback (this, "CBTProc", 3); int cbtProc = cbtCallback.getAddress (); 479 if (cbtProc == 0) error (SWT.ERROR_NO_MORE_CALLBACKS); 480 cbtHook = OS.SetWindowsHookEx (OS.WH_CBT, cbtProc, 0, threadId); 481 super.createHandle (); 482 if (cbtHook != 0) OS.UnhookWindowsHookEx (cbtHook); 483 cbtHook = 0; 484 cbtCallback.dispose (); 485 } 486 state &= ~(CANVAS | THEME_BACKGROUND); 487 488 489 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 490 if (hwndText != 0 && EditProc == 0) { 491 EditProc = OS.GetWindowLong (hwndText, OS.GWL_WNDPROC); 492 } 493 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 494 if (hwndList != 0 && ListProc == 0) { 495 ListProc = OS.GetWindowLong (hwndList, OS.GWL_WNDPROC); 496 } 497 498 504 if ((style & SWT.SIMPLE) != 0) { 505 int flags = OS.SWP_NOZORDER | OS.SWP_DRAWFRAME | OS.SWP_NOACTIVATE; 506 SetWindowPos (handle, 0, 0, 0, 0x3FFF, 0x3FFF, flags); 507 SetWindowPos (handle, 0, 0, 0, 0, 0, flags); 508 } 509 } 510 511 525 public void cut () { 526 checkWidget (); 527 if ((style & SWT.READ_ONLY) != 0) return; 528 OS.SendMessage (handle, OS.WM_CUT, 0, 0); 529 } 530 531 int defaultBackground () { 532 return OS.GetSysColor (OS.COLOR_WINDOW); 533 } 534 535 void deregister () { 536 super.deregister (); 537 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 538 if (hwndText != 0) display.removeControl (hwndText); 539 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 540 if (hwndList != 0) display.removeControl (hwndList); 541 } 542 543 555 public void deselect (int index) { 556 checkWidget (); 557 int selection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 558 if (index != selection) return; 559 OS.SendMessage (handle, OS.CB_SETCURSEL, -1, 0); 560 sendEvent (SWT.Modify); 561 } 563 564 578 public void deselectAll () { 579 checkWidget (); 580 OS.SendMessage (handle, OS.CB_SETCURSEL, -1, 0); 581 sendEvent (SWT.Modify); 582 } 584 585 boolean dragDetect (int hwnd, int x, int y, boolean filter, boolean [] detect, boolean [] consume) { 586 if (filter && (style & SWT.READ_ONLY) == 0) { 587 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 588 if (hwndText != 0) { 589 int [] start = new int [1], end = new int [1]; 590 OS.SendMessage (handle, OS.CB_GETEDITSEL, start, end); 591 if (start [0] != end [0]) { 592 int lParam = (x & 0xFFFF) | ((y << 16) & 0xFFFF0000); 593 int position = OS.SendMessage (hwndText, OS.EM_CHARFROMPOS, 0, lParam) & 0xFFFF; 594 if (start [0] <= position && position < end [0]) { 595 if (super.dragDetect (hwnd, x, y, filter, detect, consume)) { 596 if (consume != null) consume [0] = true; 597 return true; 598 } 599 } 600 } 601 return false; 602 } 603 } 604 return super.dragDetect (hwnd, x, y, filter, detect, consume); 605 } 606 607 623 public String getItem (int index) { 624 checkWidget (); 625 int length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, index, 0); 626 if (length != OS.CB_ERR) { 627 TCHAR buffer = new TCHAR (getCodePage (), length + 1); 628 int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, index, buffer); 629 if (result != OS.CB_ERR) return buffer.toString (0, length); 630 } 631 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 632 if (0 <= index && index < count) error (SWT.ERROR_CANNOT_GET_ITEM); 633 error (SWT.ERROR_INVALID_RANGE); 634 return ""; 635 } 636 637 647 public int getItemCount () { 648 checkWidget (); 649 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 650 if (count == OS.CB_ERR) error (SWT.ERROR_CANNOT_GET_COUNT); 651 return count; 652 } 653 654 665 public int getItemHeight () { 666 checkWidget (); 667 int result = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, 0, 0); 668 if (result == OS.CB_ERR) error (SWT.ERROR_CANNOT_GET_ITEM_HEIGHT); 669 return result; 670 } 671 672 688 public String [] getItems () { 689 checkWidget (); 690 int count = getItemCount (); 691 String [] result = new String [count]; 692 for (int i=0; i<count; i++) result [i] = getItem (i); 693 return result; 694 } 695 696 715 boolean getListVisible () { 716 checkWidget (); 717 if ((style & SWT.DROP_DOWN) != 0) { 718 return OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0; 719 } 720 return true; 721 } 722 723 String getNameText () { 724 return getText (); 725 } 726 727 739 public int getOrientation () { 740 checkWidget(); 741 return style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); 742 } 743 744 763 public Point getSelection () { 764 checkWidget (); 765 if ((style & SWT.DROP_DOWN) != 0 && (style & SWT.READ_ONLY) != 0) { 766 return new Point (0, OS.GetWindowTextLength (handle)); 767 } 768 int [] start = new int [1], end = new int [1]; 769 OS.SendMessage (handle, OS.CB_GETEDITSEL, start, end); 770 if (!OS.IsUnicode && OS.IsDBLocale) { 771 start [0] = mbcsToWcsPos (start [0]); 772 end [0] = mbcsToWcsPos (end [0]); 773 } 774 return new Point (start [0], end [0]); 775 } 776 777 788 public int getSelectionIndex () { 789 checkWidget (); 790 if (noSelection) return -1; 791 return OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 792 } 793 794 806 public String getText () { 807 checkWidget (); 808 int length = OS.GetWindowTextLength (handle); 809 if (length == 0) return ""; 810 TCHAR buffer = new TCHAR (getCodePage (), length + 1); 811 OS.GetWindowText (handle, buffer, length + 1); 812 return buffer.toString (0, length); 813 } 814 815 825 public int getTextHeight () { 826 checkWidget (); 827 COMBOBOXINFO pcbi = new COMBOBOXINFO (); 828 pcbi.cbSize = COMBOBOXINFO.sizeof; 829 if (((style & SWT.SIMPLE) == 0) && !OS.IsWinCE && OS.GetComboBoxInfo (handle, pcbi)) { 830 return (pcbi.buttonBottom - pcbi.buttonTop) + pcbi.buttonTop * 2; 831 } 832 int result = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, -1, 0); 833 if (result == OS.CB_ERR) error (SWT.ERROR_CANNOT_GET_ITEM_HEIGHT); 834 return (style & SWT.DROP_DOWN) != 0 ? result + 6 : result + 10; 835 } 836 837 852 public int getTextLimit () { 853 checkWidget (); 854 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 855 if (hwndText == 0) return LIMIT; 856 return OS.SendMessage (hwndText, OS.EM_GETLIMITTEXT, 0, 0) & 0x7FFFFFFF; 857 } 858 859 876 public int getVisibleItemCount () { 877 checkWidget (); 878 return visibleCount; 879 } 880 881 boolean hasFocus () { 882 int hwndFocus = OS.GetFocus (); 883 if (hwndFocus == handle) return true; 884 if (hwndFocus == 0) return false; 885 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 886 if (hwndFocus == hwndText) return true; 887 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 888 if (hwndFocus == hwndList) return true; 889 return false; 890 } 891 892 909 public int indexOf (String string) { 910 return indexOf (string, 0); 911 } 912 913 932 public int indexOf (String string, int start) { 933 checkWidget (); 934 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 935 936 942 if (string.length () == 0) { 943 int count = getItemCount (); 944 for (int i=start; i<count; i++) { 945 if (string.equals (getItem (i))) return i; 946 } 947 return -1; 948 } 949 950 951 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 952 if (!(0 <= start && start < count)) return -1; 953 int index = start - 1, last = 0; 954 TCHAR buffer = new TCHAR (getCodePage (), string, true); 955 do { 956 index = OS.SendMessage (handle, OS.CB_FINDSTRINGEXACT, last = index, buffer); 957 if (index == OS.CB_ERR || index <= last) return -1; 958 } while (!string.equals (getItem (index))); 959 return index; 960 } 961 962 int mbcsToWcsPos (int mbcsPos) { 963 if (mbcsPos <= 0) return 0; 964 if (OS.IsUnicode) return mbcsPos; 965 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 966 if (hwndText == 0) return mbcsPos; 967 int mbcsSize = OS.GetWindowTextLengthA (hwndText); 968 if (mbcsSize == 0) return 0; 969 if (mbcsPos >= mbcsSize) return mbcsSize; 970 byte [] buffer = new byte [mbcsSize + 1]; 971 OS.GetWindowTextA (hwndText, buffer, mbcsSize + 1); 972 return OS.MultiByteToWideChar (getCodePage (), OS.MB_PRECOMPOSED, buffer, mbcsPos, null, 0); 973 } 974 975 989 public void paste () { 990 checkWidget (); 991 if ((style & SWT.READ_ONLY) != 0) return; 992 OS.SendMessage (handle, OS.WM_PASTE, 0, 0); 993 } 994 995 void register () { 996 super.register (); 997 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 998 if (hwndText != 0) display.addControl (hwndText, this); 999 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1000 if (hwndList != 0) display.addControl (hwndList, this); 1001} 1002 1003 1017public void remove (int index) { 1018 checkWidget (); 1019 remove (index, true); 1020} 1021 1022void remove (int index, boolean notify) { 1023 TCHAR buffer = null; 1024 if ((style & SWT.H_SCROLL) != 0) { 1025 int length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, index, 0); 1026 if (length == OS.CB_ERR) { 1027 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1028 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 1029 error (SWT.ERROR_INVALID_RANGE); 1030 } 1031 buffer = new TCHAR (getCodePage (), length + 1); 1032 int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, index, buffer); 1033 if (result == OS.CB_ERR) { 1034 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1035 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 1036 error (SWT.ERROR_INVALID_RANGE); 1037 } 1038 } 1039 int length = OS.GetWindowTextLength (handle); 1040 int code = OS.SendMessage (handle, OS.CB_DELETESTRING, index, 0); 1041 if (code == OS.CB_ERR) { 1042 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1043 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 1044 error (SWT.ERROR_INVALID_RANGE); 1045 } 1046 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true); 1047 if (notify && length != OS.GetWindowTextLength (handle)) { 1048 sendEvent (SWT.Modify); 1049 if (isDisposed ()) return; 1050 } 1051 1058 if ((style & SWT.READ_ONLY) != 0) { 1059 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1060 if (count == 0) OS.InvalidateRect (handle, null, true); 1061 } 1062} 1063 1064 1080public void remove (int start, int end) { 1081 checkWidget (); 1082 if (start > end) return; 1083 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1084 if (!(0 <= start && start <= end && end < count)) { 1085 error (SWT.ERROR_INVALID_RANGE); 1086 } 1087 int textLength = OS.GetWindowTextLength (handle); 1088 RECT rect = null; 1089 int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; 1090 if ((style & SWT.H_SCROLL) != 0) { 1091 rect = new RECT (); 1092 hDC = OS.GetDC (handle); 1093 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1094 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1095 } 1096 int cp = getCodePage (); 1097 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1098 for (int i=start; i<=end; i++) { 1099 TCHAR buffer = null; 1100 if ((style & SWT.H_SCROLL) != 0) { 1101 int length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, start, 0); 1102 if (length == OS.CB_ERR) break; 1103 buffer = new TCHAR (cp, length + 1); 1104 int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, start, buffer); 1105 if (result == OS.CB_ERR) break; 1106 } 1107 int result = OS.SendMessage (handle, OS.CB_DELETESTRING, start, 0); 1108 if (result == OS.CB_ERR) error (SWT.ERROR_ITEM_NOT_REMOVED); 1109 if ((style & SWT.H_SCROLL) != 0) { 1110 OS.DrawText (hDC, buffer, -1, rect, flags); 1111 newWidth = Math.max (newWidth, rect.right - rect.left); 1112 } 1113 } 1114 if ((style & SWT.H_SCROLL) != 0) { 1115 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1116 OS.ReleaseDC (handle, hDC); 1117 setScrollWidth (newWidth, false); 1118 } 1119 if (textLength != OS.GetWindowTextLength (handle)) { 1120 sendEvent (SWT.Modify); 1121 if (isDisposed ()) return; 1122 } 1123 1130 if ((style & SWT.READ_ONLY) != 0) { 1131 count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1132 if (count == 0) OS.InvalidateRect (handle, null, true); 1133 } 1134} 1135 1136 1152public void remove (String string) { 1153 checkWidget (); 1154 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 1155 int index = indexOf (string, 0); 1156 if (index == -1) error (SWT.ERROR_INVALID_ARGUMENT); 1157 remove (index); 1158} 1159 1160 1169public void removeAll () { 1170 checkWidget (); 1171 OS.SendMessage (handle, OS.CB_RESETCONTENT, 0, 0); 1172 sendEvent (SWT.Modify); 1173 if (isDisposed ()) return; 1174 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (0); 1175} 1176 1177 1194public void removeModifyListener (ModifyListener listener) { 1195 checkWidget (); 1196 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 1197 if (eventTable == null) return; 1198 eventTable.unhook (SWT.Modify, listener); 1199} 1200 1201 1218public void removeSelectionListener (SelectionListener listener) { 1219 checkWidget (); 1220 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 1221 if (eventTable == null) return; 1222 eventTable.unhook (SWT.Selection, listener); 1223 eventTable.unhook (SWT.DefaultSelection,listener); 1224} 1225 1226 1245public void removeVerifyListener (VerifyListener listener) { 1246 checkWidget (); 1247 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 1248 if (eventTable == null) return; 1249 eventTable.unhook (SWT.Verify, listener); 1250} 1251 1252boolean sendKeyEvent (int type, int msg, int wParam, int lParam, Event event) { 1253 if (!super.sendKeyEvent (type, msg, wParam, lParam, event)) { 1254 return false; 1255 } 1256 if ((style & SWT.READ_ONLY) != 0) return true; 1257 if (type != SWT.KeyDown) return true; 1258 if (msg != OS.WM_CHAR && msg != OS.WM_KEYDOWN && msg != OS.WM_IME_CHAR) { 1259 return true; 1260 } 1261 if (event.character == 0) return true; 1262 if (!hooks (SWT.Verify) && !filters (SWT.Verify)) return true; 1263 char key = event.character; 1264 int stateMask = event.stateMask; 1265 1266 1271 switch (msg) { 1272 case OS.WM_CHAR: 1273 if (key != 0x08 && key != 0x7F && key != '\r' && key != '\t' && key != '\n') break; 1274 case OS.WM_KEYDOWN: 1276 if ((stateMask & (SWT.ALT | SWT.SHIFT | SWT.CONTROL)) != 0) return false; 1277 break; 1278 } 1279 1280 1283 if (OS.GetKeyState (OS.VK_LBUTTON) < 0) { 1284 return true; 1285 } 1286 1287 1288 String oldText = ""; 1289 int [] start = new int [1], end = new int [1]; 1290 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1291 if (hwndText == 0) return true; 1292 OS.SendMessage (hwndText, OS.EM_GETSEL, start, end); 1293 switch (key) { 1294 case 0x08: 1295 if (start [0] == end [0]) { 1296 if (start [0] == 0) return true; 1297 start [0] = start [0] - 1; 1298 if (!OS.IsUnicode && OS.IsDBLocale) { 1299 int [] newStart = new int [1], newEnd = new int [1]; 1300 OS.SendMessage (hwndText, OS.EM_SETSEL, start [0], end [0]); 1301 OS.SendMessage (hwndText, OS.EM_GETSEL, newStart, newEnd); 1302 if (start [0] != newStart [0]) start [0] = start [0] - 1; 1303 } 1304 start [0] = Math.max (start [0], 0); 1305 } 1306 break; 1307 case 0x7F: 1308 if (start [0] == end [0]) { 1309 int length = OS.GetWindowTextLength (hwndText); 1310 if (start [0] == length) return true; 1311 end [0] = end [0] + 1; 1312 if (!OS.IsUnicode && OS.IsDBLocale) { 1313 int [] newStart = new int [1], newEnd = new int [1]; 1314 OS.SendMessage (hwndText, OS.EM_SETSEL, start [0], end [0]); 1315 OS.SendMessage (hwndText, OS.EM_GETSEL, newStart, newEnd); 1316 if (end [0] != newEnd [0]) end [0] = end [0] + 1; 1317 } 1318 end [0] = Math.min (end [0], length); 1319 } 1320 break; 1321 case '\r': 1322 return true; 1323 default: 1324 if (key != '\t' && key < 0x20) return true; 1325 oldText = new String (new char [] {key}); 1326 break; 1327 } 1328 String newText = verifyText (oldText, start [0], end [0], event); 1329 if (newText == null) return false; 1330 if (newText == oldText) return true; 1331 TCHAR buffer = new TCHAR (getCodePage (), newText, true); 1332 OS.SendMessage (hwndText, OS.EM_SETSEL, start [0], end [0]); 1333 OS.SendMessage (hwndText, OS.EM_REPLACESEL, 0, buffer); 1334 return false; 1335} 1336 1337 1349public void select (int index) { 1350 checkWidget (); 1351 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1352 if (0 <= index && index < count) { 1353 int selection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 1354 int code = OS.SendMessage (handle, OS.CB_SETCURSEL, index, 0); 1355 if (code != OS.CB_ERR && code != selection) { 1356 sendEvent (SWT.Modify); 1357 } 1359 } 1360} 1361 1362void setBackgroundImage (int hBitmap) { 1363 super.setBackgroundImage (hBitmap); 1364 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1365 if (hwndText != 0) OS.InvalidateRect (hwndText, null, true); 1366 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1367 if (hwndList != 0) OS.InvalidateRect (hwndList, null, true); 1368} 1369 1370void setBackgroundPixel (int pixel) { 1371 super.setBackgroundPixel (pixel); 1372 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1373 if (hwndText != 0) OS.InvalidateRect (hwndText, null, true); 1374 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1375 if (hwndList != 0) OS.InvalidateRect (hwndList, null, true); 1376} 1377 1378void setBounds (int x, int y, int width, int height, int flags) { 1379 1396 if ((style & SWT.DROP_DOWN) != 0) { 1397 height = getTextHeight () + (getItemHeight () * visibleCount) + 2; 1398 1411 RECT rect = new RECT (); 1412 OS.GetWindowRect (handle, rect); 1413 if (rect.right - rect.left != 0) { 1414 if (OS.SendMessage (handle, OS.CB_GETDROPPEDCONTROLRECT, 0, rect) != 0) { 1415 int oldWidth = rect.right - rect.left, oldHeight = rect.bottom - rect.top; 1416 if (oldWidth == width && oldHeight == height) flags |= OS.SWP_NOSIZE; 1417 } 1418 } 1419 SetWindowPos (handle, 0, x, y, width, height, flags); 1420 } else { 1421 super.setBounds (x, y, width, height, flags); 1422 } 1423} 1424 1425public void setFont (Font font) { 1426 checkWidget (); 1427 super.setFont (font); 1428 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (); 1429} 1430 1431void setForegroundPixel (int pixel) { 1432 super.setForegroundPixel (pixel); 1433 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1434 if (hwndText != 0) OS.InvalidateRect (hwndText, null, true); 1435 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1436 if (hwndList != 0) OS.InvalidateRect (hwndList, null, true); 1437} 1438 1439 1455public void setItem (int index, String string) { 1456 checkWidget (); 1457 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 1458 int selection = getSelectionIndex (); 1459 remove (index, false); 1460 if (isDisposed ()) return; 1461 add (string, index); 1462 if (selection != -1) select (selection); 1463} 1464 1465 1479public void setItems (String [] items) { 1480 checkWidget (); 1481 if (items == null) error (SWT.ERROR_NULL_ARGUMENT); 1482 for (int i=0; i<items.length; i++) { 1483 if (items [i] == null) error (SWT.ERROR_INVALID_ARGUMENT); 1484 } 1485 RECT rect = null; 1486 int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; 1487 if ((style & SWT.H_SCROLL) != 0) { 1488 rect = new RECT (); 1489 hDC = OS.GetDC (handle); 1490 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1491 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1492 setScrollWidth (0); 1493 } 1494 OS.SendMessage (handle, OS.CB_RESETCONTENT, 0, 0); 1495 int codePage = getCodePage (); 1496 for (int i=0; i<items.length; i++) { 1497 String string = items [i]; 1498 TCHAR buffer = new TCHAR (codePage, string, true); 1499 int code = OS.SendMessage (handle, OS.CB_ADDSTRING, 0, buffer); 1500 if (code == OS.CB_ERR) error (SWT.ERROR_ITEM_NOT_ADDED); 1501 if (code == OS.CB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED); 1502 if ((style & SWT.H_SCROLL) != 0) { 1503 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1504 OS.DrawText (hDC, buffer, -1, rect, flags); 1505 newWidth = Math.max (newWidth, rect.right - rect.left); 1506 } 1507 } 1508 if ((style & SWT.H_SCROLL) != 0) { 1509 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1510 OS.ReleaseDC (handle, hDC); 1511 setScrollWidth (newWidth + 3); 1512 } 1513 sendEvent (SWT.Modify); 1514 } 1516 1517 1535 void setListVisible (boolean visible) { 1536 checkWidget (); 1537 OS.SendMessage (handle, OS.CB_SHOWDROPDOWN, visible ? 1 : 0, 0); 1538} 1539 1540 1554public void setOrientation (int orientation) { 1555 checkWidget(); 1556 if (OS.IsWinCE) return; 1557 if (OS.WIN32_VERSION < OS.VERSION (4, 10)) return; 1558 int flags = SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT; 1559 if ((orientation & flags) == 0 || (orientation & flags) == flags) return; 1560 style &= ~flags; 1561 style |= orientation & flags; 1562 int bits = OS.GetWindowLong (handle, OS.GWL_EXSTYLE); 1563 if ((style & SWT.RIGHT_TO_LEFT) != 0) { 1564 style |= SWT.MIRRORED; 1565 bits |= OS.WS_EX_LAYOUTRTL; 1566 } else { 1567 style &= ~SWT.MIRRORED; 1568 bits &= ~OS.WS_EX_LAYOUTRTL; 1569 } 1570 OS.SetWindowLong (handle, OS.GWL_EXSTYLE, bits); 1571 int hwndText = 0, hwndList = 0; 1572 COMBOBOXINFO pcbi = new COMBOBOXINFO (); 1573 pcbi.cbSize = COMBOBOXINFO.sizeof; 1574 if (OS.GetComboBoxInfo (handle, pcbi)) { 1575 hwndText = pcbi.hwndItem; 1576 hwndList = pcbi.hwndList; 1577 } 1578 if (hwndText != 0) { 1579 int bits1 = OS.GetWindowLong (hwndText, OS.GWL_EXSTYLE); 1580 int bits2 = OS.GetWindowLong (hwndText, OS.GWL_STYLE); 1581 if ((style & SWT.RIGHT_TO_LEFT) != 0) { 1582 bits1 |= OS.WS_EX_RIGHT | OS.WS_EX_RTLREADING; 1583 bits2 |= OS.ES_RIGHT; 1584 } else { 1585 bits1 &= ~(OS.WS_EX_RIGHT | OS.WS_EX_RTLREADING); 1586 bits2 &= ~OS.ES_RIGHT; 1587 } 1588 OS.SetWindowLong (hwndText, OS.GWL_EXSTYLE, bits1); 1589 OS.SetWindowLong (hwndText, OS.GWL_STYLE, bits2); 1590 1591 1597 RECT rect = new RECT (); 1598 OS.GetWindowRect (hwndText, rect); 1599 int width = rect.right - rect.left, height = rect.bottom - rect.top; 1600 OS.GetWindowRect (handle, rect); 1601 int widthCombo = rect.right - rect.left, heightCombo = rect.bottom - rect.top; 1602 int uFlags = OS.SWP_NOMOVE | OS.SWP_NOZORDER | OS.SWP_NOACTIVATE; 1603 SetWindowPos (hwndText, 0, 0, 0, width - 1, height - 1, uFlags); 1604 SetWindowPos (handle, 0, 0, 0, widthCombo - 1, heightCombo - 1, uFlags); 1605 SetWindowPos (hwndText, 0, 0, 0, width, height, uFlags); 1606 SetWindowPos (handle, 0, 0, 0, widthCombo, heightCombo, uFlags); 1607 OS.InvalidateRect (handle, null, true); 1608 } 1609 if (hwndList != 0) { 1610 int bits1 = OS.GetWindowLong (hwndList, OS.GWL_EXSTYLE); 1611 if ((style & SWT.RIGHT_TO_LEFT) != 0) { 1612 bits1 |= OS.WS_EX_LAYOUTRTL; 1613 } else { 1614 bits1 &= ~OS.WS_EX_LAYOUTRTL; 1615 } 1616 OS.SetWindowLong (hwndList, OS.GWL_EXSTYLE, bits1); 1617 } 1618} 1619 1620void setScrollWidth () { 1621 int newWidth = 0; 1622 RECT rect = new RECT (); 1623 int newFont, oldFont = 0; 1624 int hDC = OS.GetDC (handle); 1625 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1626 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1627 int cp = getCodePage (); 1628 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1629 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1630 for (int i=0; i<count; i++) { 1631 int length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, i, 0); 1632 if (length != OS.CB_ERR) { 1633 TCHAR buffer = new TCHAR (cp, length + 1); 1634 int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, i, buffer); 1635 if (result != OS.CB_ERR) { 1636 OS.DrawText (hDC, buffer, -1, rect, flags); 1637 newWidth = Math.max (newWidth, rect.right - rect.left); 1638 } 1639 } 1640 } 1641 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1642 OS.ReleaseDC (handle, hDC); 1643 setScrollWidth (newWidth + 3); 1644} 1645 1646void setScrollWidth (int scrollWidth) { 1647 this.scrollWidth = scrollWidth; 1648 if ((style & SWT.SIMPLE) != 0) { 1649 OS.SendMessage (handle, OS.CB_SETHORIZONTALEXTENT, scrollWidth, 0); 1650 return; 1651 } 1652 boolean scroll = false; 1653 int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0); 1654 if (count > 3) { 1655 int maxWidth = 0; 1656 if (OS.IsWinCE || OS.WIN32_VERSION < OS.VERSION (4, 10)) { 1657 RECT rect = new RECT (); 1658 OS.SystemParametersInfo (OS.SPI_GETWORKAREA, 0, rect, 0); 1659 maxWidth = (rect.right - rect.left) / 4; 1660 } else { 1661 int hmonitor = OS.MonitorFromWindow (handle, OS.MONITOR_DEFAULTTONEAREST); 1662 MONITORINFO lpmi = new MONITORINFO (); 1663 lpmi.cbSize = MONITORINFO.sizeof; 1664 OS.GetMonitorInfo (hmonitor, lpmi); 1665 maxWidth = (lpmi.rcWork_right - lpmi.rcWork_left) / 4; 1666 } 1667 scroll = scrollWidth > maxWidth; 1668 } 1669 if (scroll) { 1670 OS.SendMessage (handle, OS.CB_SETDROPPEDWIDTH, 0, 0); 1671 OS.SendMessage (handle, OS.CB_SETHORIZONTALEXTENT, scrollWidth, 0); 1672 } else { 1673 scrollWidth += OS.GetSystemMetrics (OS.SM_CYHSCROLL); 1674 OS.SendMessage (handle, OS.CB_SETDROPPEDWIDTH, scrollWidth, 0); 1675 OS.SendMessage (handle, OS.CB_SETHORIZONTALEXTENT, 0, 0); 1676 } 1677} 1678 1679void setScrollWidth (TCHAR buffer, boolean grow) { 1680 RECT rect = new RECT (); 1681 int newFont, oldFont = 0; 1682 int hDC = OS.GetDC (handle); 1683 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1684 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1685 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1686 OS.DrawText (hDC, buffer, -1, rect, flags); 1687 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1688 OS.ReleaseDC (handle, hDC); 1689 setScrollWidth (rect.right - rect.left, grow); 1690} 1691 1692void setScrollWidth (int newWidth, boolean grow) { 1693 if (grow) { 1694 if (newWidth <= scrollWidth) return; 1695 setScrollWidth (newWidth + 3); 1696 } else { 1697 if (newWidth < scrollWidth) return; 1698 setScrollWidth (); 1699 } 1700} 1701 1702 1718public void setSelection (Point selection) { 1719 checkWidget (); 1720 if (selection == null) error (SWT.ERROR_NULL_ARGUMENT); 1721 int start = selection.x, end = selection.y; 1722 if (!OS.IsUnicode && OS.IsDBLocale) { 1723 start = wcsToMbcsPos (start); 1724 end = wcsToMbcsPos (end); 1725 } 1726 int bits = (start & 0xFFFF) | ((end << 16) & 0xFFFF0000); 1727 OS.SendMessage (handle, OS.CB_SETEDITSEL, 0, bits); 1728} 1729 1730 1751public void setText (String string) { 1752 checkWidget (); 1753 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 1754 if ((style & SWT.READ_ONLY) != 0) { 1755 int index = indexOf (string); 1756 if (index != -1) select (index); 1757 return; 1758 } 1759 int limit = LIMIT; 1760 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1761 if (hwndText != 0) { 1762 limit = OS.SendMessage (hwndText, OS.EM_GETLIMITTEXT, 0, 0); 1763 } 1764 if (string.length () > limit) string = string.substring (0, limit); 1765 TCHAR buffer = new TCHAR (getCodePage (), string, true); 1766 if (OS.SetWindowText (handle, buffer)) { 1767 sendEvent (SWT.Modify); 1768 } 1770} 1771 1772 1792public void setTextLimit (int limit) { 1793 checkWidget (); 1794 if (limit == 0) error (SWT.ERROR_CANNOT_BE_ZERO); 1795 OS.SendMessage (handle, OS.CB_LIMITTEXT, limit, 0); 1796} 1797 1798void setToolTipText (Shell shell, String string) { 1799 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1800 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1801 if (hwndText != 0) shell.setToolTipText (hwndText, string); 1802 if (hwndList != 0) shell.setToolTipText (hwndList, string); 1803 shell.setToolTipText (handle, string); 1804} 1805 1806 1823public void setVisibleItemCount (int count) { 1824 checkWidget (); 1825 if (count < 0) return; 1826 visibleCount = count; 1827 if ((style & SWT.DROP_DOWN) != 0) { 1828 forceResize (); 1829 RECT rect = new RECT (); 1830 OS.GetWindowRect (handle, rect); 1831 int flags = OS.SWP_NOMOVE | OS.SWP_NOZORDER | OS.SWP_DRAWFRAME | OS.SWP_NOACTIVATE; 1832 setBounds (0, 0, rect.right - rect.left, rect.bottom - rect.top, flags); 1833 } 1834} 1835 1836void subclass () { 1837 super.subclass (); 1838 int newProc = display.windowProc; 1839 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1840 if (hwndText != 0) { 1841 OS.SetWindowLong (hwndText, OS.GWL_WNDPROC, newProc); 1842 } 1843 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1844 if (hwndList != 0) { 1845 OS.SetWindowLong (hwndList, OS.GWL_WNDPROC, newProc); 1846 } 1847} 1848 1849boolean translateTraversal (MSG msg) { 1850 1855 switch (msg.wParam) { 1856 case OS.VK_RETURN: 1857 case OS.VK_ESCAPE: 1858 if ((style & SWT.DROP_DOWN) != 0) { 1859 if (OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0) { 1860 return false; 1861 } 1862 } 1863 } 1864 return super.translateTraversal (msg); 1865} 1866 1867boolean traverseEscape () { 1868 if ((style & SWT.DROP_DOWN) != 0) { 1869 if (OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0) { 1870 OS.SendMessage (handle, OS.CB_SHOWDROPDOWN, 0, 0); 1871 return true; 1872 } 1873 } 1874 return super.traverseEscape (); 1875} 1876 1877boolean traverseReturn () { 1878 if ((style & SWT.DROP_DOWN) != 0) { 1879 if (OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0) { 1880 OS.SendMessage (handle, OS.CB_SHOWDROPDOWN, 0, 0); 1881 return true; 1882 } 1883 } 1884 return super.traverseReturn (); 1885} 1886 1887void unsubclass () { 1888 super.unsubclass (); 1889 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1890 if (hwndText != 0 && EditProc != 0) { 1891 OS.SetWindowLong (hwndText, OS.GWL_WNDPROC, EditProc); 1892 } 1893 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1894 if (hwndList != 0 && ListProc != 0) { 1895 OS.SetWindowLong (hwndList, OS.GWL_WNDPROC, ListProc); 1896 } 1897} 1898 1899String verifyText (String string, int start, int end, Event keyEvent) { 1900 Event event = new Event (); 1901 event.text = string; 1902 event.start = start; 1903 event.end = end; 1904 if (keyEvent != null) { 1905 event.character = keyEvent.character; 1906 event.keyCode = keyEvent.keyCode; 1907 event.stateMask = keyEvent.stateMask; 1908 } 1909 if (!OS.IsUnicode && OS.IsDBLocale) { 1910 event.start = mbcsToWcsPos (start); 1911 event.end = mbcsToWcsPos (end); 1912 } 1913 1919 sendEvent (SWT.Verify, event); 1920 if (!event.doit || isDisposed ()) return null; 1921 return event.text; 1922} 1923 1924int wcsToMbcsPos (int wcsPos) { 1925 if (wcsPos <= 0) return 0; 1926 if (OS.IsUnicode) return wcsPos; 1927 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1928 if (hwndText == 0) return wcsPos; 1929 int mbcsSize = OS.GetWindowTextLengthA (hwndText); 1930 if (mbcsSize == 0) return 0; 1931 byte [] buffer = new byte [mbcsSize + 1]; 1932 OS.GetWindowTextA (hwndText, buffer, mbcsSize + 1); 1933 int mbcsPos = 0, wcsCount = 0; 1934 while (mbcsPos < mbcsSize) { 1935 if (wcsPos == wcsCount) break; 1936 if (OS.IsDBCSLeadByte (buffer [mbcsPos++])) mbcsPos++; 1937 wcsCount++; 1938 } 1939 return mbcsPos; 1940} 1941 1942int widgetExtStyle () { 1943 return super.widgetExtStyle () & ~OS.WS_EX_NOINHERITLAYOUT; 1944} 1945 1946int widgetStyle () { 1947 int bits = super.widgetStyle () | OS.CBS_AUTOHSCROLL | OS.CBS_NOINTEGRALHEIGHT | OS.WS_HSCROLL |OS.WS_VSCROLL; 1948 if ((style & SWT.SIMPLE) != 0) return bits | OS.CBS_SIMPLE; 1949 if ((style & SWT.READ_ONLY) != 0) return bits | OS.CBS_DROPDOWNLIST; 1950 return bits | OS.CBS_DROPDOWN; 1951} 1952 1953TCHAR windowClass () { 1954 return ComboClass; 1955} 1956 1957int windowProc () { 1958 return ComboProc; 1959} 1960 1961int windowProc (int hwnd, int msg, int wParam, int lParam) { 1962 if (handle == 0) return 0; 1963 if (hwnd != handle) { 1964 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 1965 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 1966 if ((hwndText != 0 && hwnd == hwndText) || (hwndList != 0 && hwnd == hwndList)) { 1967 LRESULT result = null; 1968 switch (msg) { 1969 1970 case OS.WM_CHAR: result = wmChar (hwnd, wParam, lParam); break; 1971 case OS.WM_IME_CHAR: result = wmIMEChar (hwnd, wParam, lParam); break; 1972 case OS.WM_KEYDOWN: result = wmKeyDown (hwnd, wParam, lParam); break; 1973 case OS.WM_KEYUP: result = wmKeyUp (hwnd, wParam, lParam); break; 1974 case OS.WM_SYSCHAR: result = wmSysChar (hwnd, wParam, lParam); break; 1975 case OS.WM_SYSKEYDOWN: result = wmSysKeyDown (hwnd, wParam, lParam); break; 1976 case OS.WM_SYSKEYUP: result = wmSysKeyUp (hwnd, wParam, lParam); break; 1977 1978 1979 case OS.WM_CAPTURECHANGED: result = wmCaptureChanged (hwnd, wParam, lParam); break; 1980 case OS.WM_LBUTTONDBLCLK: result = wmLButtonDblClk (hwnd, wParam, lParam); break; 1981 case OS.WM_LBUTTONDOWN: result = wmLButtonDown (hwnd, wParam, lParam); break; 1982 case OS.WM_LBUTTONUP: result = wmLButtonUp (hwnd, wParam, lParam); break; 1983 case OS.WM_MBUTTONDBLCLK: result = wmMButtonDblClk (hwnd, wParam, lParam); break; 1984 case OS.WM_MBUTTONDOWN: result = wmMButtonDown (hwnd, wParam, lParam); break; 1985 case OS.WM_MBUTTONUP: result = wmMButtonUp (hwnd, wParam, lParam); break; 1986 case OS.WM_MOUSEHOVER: result = wmMouseHover (hwnd, wParam, lParam); break; 1987 case OS.WM_MOUSELEAVE: result = wmMouseLeave (hwnd, wParam, lParam); break; 1988 case OS.WM_MOUSEMOVE: result = wmMouseMove (hwnd, wParam, lParam); break; 1989 case OS.WM_RBUTTONDBLCLK: result = wmRButtonDblClk (hwnd, wParam, lParam); break; 1991 case OS.WM_RBUTTONDOWN: result = wmRButtonDown (hwnd, wParam, lParam); break; 1992 case OS.WM_RBUTTONUP: result = wmRButtonUp (hwnd, wParam, lParam); break; 1993 case OS.WM_XBUTTONDBLCLK: result = wmXButtonDblClk (hwnd, wParam, lParam); break; 1994 case OS.WM_XBUTTONDOWN: result = wmXButtonDown (hwnd, wParam, lParam); break; 1995 case OS.WM_XBUTTONUP: result = wmXButtonUp (hwnd, wParam, lParam); break; 1996 1997 1998 case OS.WM_PAINT: result = wmPaint (hwnd, wParam, lParam); break; 1999 2000 2001 case OS.WM_CONTEXTMENU: result = wmContextMenu (hwnd, wParam, lParam); break; 2002 2003 2004 case OS.WM_CLEAR: 2005 case OS.WM_CUT: 2006 case OS.WM_PASTE: 2007 case OS.WM_UNDO: 2008 case OS.EM_UNDO: 2009 case OS.WM_SETTEXT: 2010 if (hwnd == hwndText) { 2011 result = wmClipboard (hwnd, msg, wParam, lParam); 2012 } 2013 break; 2014 } 2015 if (result != null) return result.value; 2016 return callWindowProc (hwnd, msg, wParam, lParam); 2017 } 2018 } 2019 if (msg == OS.CB_SETCURSEL) { 2020 if ((style & SWT.READ_ONLY) != 0) { 2021 if (hooks (SWT.Verify) || filters (SWT.Verify)) { 2022 String oldText = getText (), newText = null; 2023 if (wParam == -1) { 2024 newText = ""; 2025 } else { 2026 if (0 <= wParam && wParam < getItemCount ()) { 2027 newText = getItem (wParam); 2028 } 2029 } 2030 if (newText != null && !newText.equals (oldText)) { 2031 int length = OS.GetWindowTextLength (handle); 2032 oldText = newText; 2033 newText = verifyText (newText, 0, length, null); 2034 if (newText == null) return 0; 2035 if (!newText.equals (oldText)) { 2036 int index = indexOf (newText); 2037 if (index != -1 && index != wParam) { 2038 return callWindowProc (handle, OS.CB_SETCURSEL, index, lParam); 2039 } 2040 } 2041 } 2042 } 2043 } 2044 } 2045 return super.windowProc (hwnd, msg, wParam, lParam); 2046} 2047 2048LRESULT WM_CTLCOLOR (int wParam, int lParam) { 2049 return wmColorChild (wParam, lParam); 2050} 2051 2052LRESULT WM_GETDLGCODE (int wParam, int lParam) { 2053 int code = callWindowProc (handle, OS.WM_GETDLGCODE, wParam, lParam); 2054 return new LRESULT (code | OS.DLGC_WANTARROWS); 2055} 2056 2057LRESULT WM_KILLFOCUS (int wParam, int lParam) { 2058 2066 if ((style & SWT.READ_ONLY) != 0) { 2067 return super.WM_KILLFOCUS (wParam, lParam); 2068 } 2069 2070 2074 return null; 2075} 2076 2077LRESULT WM_LBUTTONDOWN (int wParam, int lParam) { 2078 2085 int oldSelection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2086 LRESULT result = super.WM_LBUTTONDOWN (wParam, lParam); 2087 if (result == LRESULT.ZERO) return result; 2088 if ((style & SWT.READ_ONLY) == 0) { 2089 int newSelection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2090 if (oldSelection != newSelection) { 2091 sendEvent (SWT.Modify); 2092 if (isDisposed ()) return LRESULT.ZERO; 2093 sendEvent (SWT.Selection); 2094 if (isDisposed ()) return LRESULT.ZERO; 2095 } 2096 } 2097 return result; 2098} 2099 2100LRESULT WM_SETFOCUS (int wParam, int lParam) { 2101 2105 return null; 2106} 2107 2108LRESULT WM_SIZE (int wParam, int lParam) { 2109 2115 if ((style & SWT.SIMPLE) != 0) { 2116 LRESULT result = super.WM_SIZE (wParam, lParam); 2117 if (OS.IsWindowVisible (handle)) { 2118 if (OS.IsWinCE) { 2119 int hwndText = OS.GetDlgItem (handle, CBID_EDIT); 2120 if (hwndText != 0) OS.InvalidateRect (hwndText, null, true); 2121 int hwndList = OS.GetDlgItem (handle, CBID_LIST); 2122 if (hwndList != 0) OS.InvalidateRect (hwndList, null, true); 2123 } else { 2124 int uFlags = OS.RDW_ERASE | OS.RDW_INVALIDATE | OS.RDW_ALLCHILDREN; 2125 OS.RedrawWindow (handle, null, 0, uFlags); 2126 } 2127 } 2128 return result; 2129 } 2130 2131 2138 LRESULT result = null; 2139 if ((style & SWT.READ_ONLY) != 0) { 2140 result = super.WM_SIZE (wParam, lParam); 2141 } else { 2142 int index = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2143 boolean redraw = false; 2144 TCHAR buffer = null; 2145 int [] start = null, end = null; 2146 if (index == OS.CB_ERR) { 2147 int length = OS.GetWindowTextLength (handle); 2148 if (length != 0) { 2149 buffer = new TCHAR (getCodePage (), length + 1); 2150 OS.GetWindowText (handle, buffer, length + 1); 2151 start = new int [1]; end = new int [1]; 2152 OS.SendMessage (handle, OS.CB_GETEDITSEL, start, end); 2153 redraw = drawCount == 0 && OS.IsWindowVisible (handle); 2154 if (redraw) setRedraw (false); 2155 } 2156 } 2157 result = super.WM_SIZE (wParam, lParam); 2158 2165 if (isDisposed ()) return result; 2166 if (buffer != null) { 2167 OS.SetWindowText (handle, buffer); 2168 int bits = (start [0] & 0xFFFF) | ((end [0] << 16) & 0xFFFF0000); 2169 OS.SendMessage (handle, OS.CB_SETEDITSEL, 0, bits); 2170 if (redraw) setRedraw (true); 2171 } 2172 } 2173 2179 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (scrollWidth); 2180 return result; 2181} 2182 2183LRESULT wmChar (int hwnd, int wParam, int lParam) { 2184 if (ignoreCharacter) return null; 2185 LRESULT result = super.wmChar (hwnd, wParam, lParam); 2186 if (result != null) return result; 2187 2197 switch (wParam) { 2198 case SWT.TAB: return LRESULT.ZERO; 2199 case SWT.CR: 2200 if (!ignoreDefaultSelection) postEvent (SWT.DefaultSelection); 2201 ignoreDefaultSelection = false; 2202 case SWT.ESC: 2204 if ((style & SWT.DROP_DOWN) != 0) { 2205 if (OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) == 0) { 2206 return LRESULT.ZERO; 2207 } 2208 } 2209 } 2210 return result; 2211} 2212 2213LRESULT wmClipboard (int hwndText, int msg, int wParam, int lParam) { 2214 if ((style & SWT.READ_ONLY) != 0) return null; 2215 if (!hooks (SWT.Verify) && !filters (SWT.Verify)) return null; 2216 boolean call = false; 2217 int [] start = new int [1], end = new int [1]; 2218 String newText = null; 2219 switch (msg) { 2220 case OS.WM_CLEAR: 2221 case OS.WM_CUT: 2222 OS.SendMessage (hwndText, OS.EM_GETSEL, start, end); 2223 if (start [0] != end [0]) { 2224 newText = ""; 2225 call = true; 2226 } 2227 break; 2228 case OS.WM_PASTE: 2229 OS.SendMessage (hwndText, OS.EM_GETSEL, start, end); 2230 newText = getClipboardText (); 2231 break; 2232 case OS.EM_UNDO: 2233 case OS.WM_UNDO: 2234 if (OS.SendMessage (hwndText, OS.EM_CANUNDO, 0, 0) != 0) { 2235 ignoreModify = true; 2236 OS.SendMessage (hwndText, OS.EM_GETSEL, start, end); 2237 OS.CallWindowProc (EditProc, hwndText, msg, wParam, lParam); 2238 int length = OS.GetWindowTextLength (hwndText); 2239 int [] newStart = new int [1], newEnd = new int [1]; 2240 OS.SendMessage (hwndText, OS.EM_GETSEL, newStart, newEnd); 2241 if (length != 0 && newStart [0] != newEnd [0]) { 2242 TCHAR buffer = new TCHAR (getCodePage (), length + 1); 2243 OS.GetWindowText (hwndText, buffer, length + 1); 2244 newText = buffer.toString (newStart [0], newEnd [0] - newStart [0]); 2245 } else { 2246 newText = ""; 2247 } 2248 OS.CallWindowProc (EditProc, hwndText, msg, wParam, lParam); 2249 ignoreModify = false; 2250 } 2251 break; 2252 case OS.WM_SETTEXT: 2253 end [0] = OS.GetWindowTextLength (hwndText); 2254 int length = OS.IsUnicode ? OS.wcslen (lParam) : OS.strlen (lParam); 2255 TCHAR buffer = new TCHAR (getCodePage (), length); 2256 int byteCount = buffer.length () * TCHAR.sizeof; 2257 OS.MoveMemory (buffer, lParam, byteCount); 2258 newText = buffer.toString (0, length); 2259 break; 2260 } 2261 if (newText != null) { 2262 String oldText = newText; 2263 newText = verifyText (newText, start [0], end [0], null); 2264 if (newText == null) return LRESULT.ZERO; 2265 if (!newText.equals (oldText)) { 2266 if (call) { 2267 OS.CallWindowProc (EditProc, hwndText, msg, wParam, lParam); 2268 } 2269 TCHAR buffer = new TCHAR (getCodePage (), newText, true); 2270 if (msg == OS.WM_SETTEXT) { 2271 int hHeap = OS.GetProcessHeap (); 2272 int byteCount = buffer.length () * TCHAR.sizeof; 2273 int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 2274 OS.MoveMemory (pszText, buffer, byteCount); 2275 int code = OS.CallWindowProc (EditProc, hwndText, msg, wParam, pszText); 2276 OS.HeapFree (hHeap, 0, pszText); 2277 return new LRESULT (code); 2278 } else { 2279 OS.SendMessage (hwndText, OS.EM_REPLACESEL, 0, buffer); 2280 return LRESULT.ZERO; 2281 } 2282 } 2283 } 2284 return null; 2285} 2286 2287LRESULT wmCommandChild (int wParam, int lParam) { 2288 int code = wParam >> 16; 2289 switch (code) { 2290 case OS.CBN_EDITCHANGE: 2291 if (ignoreModify) break; 2292 2301 noSelection = true; 2302 sendEvent (SWT.Modify); 2303 if (isDisposed ()) return LRESULT.ZERO; 2304 noSelection = false; 2305 break; 2306 case OS.CBN_SELCHANGE: 2307 2316 int index = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2317 if (index != OS.CB_ERR) { 2318 OS.SendMessage (handle, OS.CB_SETCURSEL, index, 0); 2319 } 2320 2327 sendEvent (SWT.Modify); 2328 if (isDisposed ()) return LRESULT.ZERO; 2329 postEvent (SWT.Selection); 2330 break; 2331 case OS.CBN_SETFOCUS: 2332 sendFocusEvent (SWT.FocusIn); 2333 if (isDisposed ()) return LRESULT.ZERO; 2334 break; 2335 case OS.CBN_KILLFOCUS: 2336 2344 if ((style & SWT.READ_ONLY) != 0) break; 2345 sendFocusEvent (SWT.FocusOut); 2346 if (isDisposed ()) return LRESULT.ZERO; 2347 break; 2348 } 2349 return super.wmCommandChild (wParam, lParam); 2350} 2351 2352LRESULT wmIMEChar (int hwnd, int wParam, int lParam) { 2353 2354 2355 Display display = this.display; 2356 display.lastKey = 0; 2357 display.lastAscii = wParam; 2358 display.lastVirtual = display.lastNull = display.lastDead = false; 2359 if (!sendKeyEvent (SWT.KeyDown, OS.WM_IME_CHAR, wParam, lParam)) { 2360 return LRESULT.ZERO; 2361 } 2362 2363 2370 ignoreCharacter = true; 2371 int result = callWindowProc (hwnd, OS.WM_IME_CHAR, wParam, lParam); 2372 MSG msg = new MSG (); 2373 int flags = OS.PM_REMOVE | OS.PM_NOYIELD | OS.PM_QS_INPUT | OS.PM_QS_POSTMESSAGE; 2374 while (OS.PeekMessage (msg, hwnd, OS.WM_CHAR, OS.WM_CHAR, flags)) { 2375 OS.TranslateMessage (msg); 2376 OS.DispatchMessage (msg); 2377 } 2378 ignoreCharacter = false; 2379 2380 sendKeyEvent (SWT.KeyUp, OS.WM_IME_CHAR, wParam, lParam); 2381 display.lastKey = display.lastAscii = 0; 2383 return new LRESULT (result); 2384} 2385 2386LRESULT wmKeyDown (int hwnd, int wParam, int lParam) { 2387 if (ignoreCharacter) return null; 2388 LRESULT result = super.wmKeyDown (hwnd, wParam, lParam); 2389 if (result != null) return result; 2390 ignoreDefaultSelection = false; 2391 if (wParam == OS.VK_RETURN) { 2392 if ((style & SWT.DROP_DOWN) != 0) { 2393 if (OS.SendMessage (handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0) { 2394 ignoreDefaultSelection = true; 2395 } 2396 } 2397 } 2398 return result; 2399} 2400 2401LRESULT wmSysKeyDown (int hwnd, int wParam, int lParam) { 2402 2409 int oldSelection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2410 LRESULT result = super.wmSysKeyDown (hwnd, wParam, lParam); 2411 if (result != null) return result; 2412 if ((style & SWT.READ_ONLY) == 0) { 2413 if (wParam == OS.VK_DOWN) { 2414 int code = callWindowProc (hwnd, OS.WM_SYSKEYDOWN, wParam, lParam); 2415 int newSelection = OS.SendMessage (handle, OS.CB_GETCURSEL, 0, 0); 2416 if (oldSelection != newSelection) { 2417 sendEvent (SWT.Modify); 2418 if (isDisposed ()) return LRESULT.ZERO; 2419 sendEvent (SWT.Selection); 2420 if (isDisposed ()) return LRESULT.ZERO; 2421 } 2422 return new LRESULT (code); 2423 } 2424 } 2425 return result; 2426} 2427 2428} 2429 | Popular Tags |