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 import org.eclipse.swt.events.*; 18 19 36 37 public class List extends Scrollable { 38 static final int INSET = 3; 39 static final int ListProc; 40 static final TCHAR ListClass = new TCHAR (0, "LISTBOX", true); 41 static { 42 WNDCLASS lpWndClass = new WNDCLASS (); 43 OS.GetClassInfo (0, ListClass, lpWndClass); 44 ListProc = lpWndClass.lpfnWndProc; 45 } 46 47 76 public List (Composite parent, int style) { 77 super (parent, checkStyle (style)); 78 } 79 94 public void add (String string) { 95 checkWidget (); 96 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 97 TCHAR buffer = new TCHAR (getCodePage (), string, true); 98 int result = OS.SendMessage (handle, OS.LB_ADDSTRING, 0, buffer); 99 if (result == OS.LB_ERR) error (SWT.ERROR_ITEM_NOT_ADDED); 100 if (result == OS.LB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED); 101 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true); 102 } 103 126 public void add (String string, int index) { 127 checkWidget (); 128 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 129 if (index == -1) error (SWT.ERROR_INVALID_RANGE); 130 TCHAR buffer = new TCHAR (getCodePage (), string, true); 131 int result = OS.SendMessage (handle, OS.LB_INSERTSTRING, index, buffer); 132 if (result == OS.LB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED); 133 if (result == OS.LB_ERR) { 134 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 135 if (0 <= index && index <= count) { 136 error (SWT.ERROR_ITEM_NOT_ADDED); 137 } else { 138 error (SWT.ERROR_INVALID_RANGE); 139 } 140 } 141 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, true); 142 } 143 144 168 public void addSelectionListener(SelectionListener listener) { 169 checkWidget (); 170 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 171 TypedListener typedListener = new TypedListener (listener); 172 addListener (SWT.Selection,typedListener); 173 addListener (SWT.DefaultSelection,typedListener); 174 } 175 176 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 177 if (handle == 0) return 0; 178 return OS.CallWindowProc (ListProc, hwnd, msg, wParam, lParam); 179 } 180 181 static int checkStyle (int style) { 182 return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0); 183 } 184 185 public Point computeSize (int wHint, int hHint, boolean changed) { 186 checkWidget (); 187 int width = 0, height = 0; 188 if (wHint == SWT.DEFAULT) { 189 if ((style & SWT.H_SCROLL) != 0) { 190 width = OS.SendMessage (handle, OS.LB_GETHORIZONTALEXTENT, 0, 0); 191 width -= INSET; 192 } else { 193 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 194 int newFont, oldFont = 0; 195 int hDC = OS.GetDC (handle); 196 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 197 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 198 RECT rect = new RECT (); 199 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 200 int cp = getCodePage (); 201 TCHAR buffer = new TCHAR (cp, 64 + 1); 202 for (int i=0; i<count; i++) { 203 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, i, 0); 204 if (length != OS.LB_ERR) { 205 if (length + 1 > buffer.length ()) { 206 buffer = new TCHAR (cp, length + 1); 207 } 208 int result = OS.SendMessage (handle, OS.LB_GETTEXT, i, buffer); 209 if (result != OS.LB_ERR) { 210 OS.DrawText (hDC, buffer, length, rect, flags); 211 width = Math.max (width, rect.right - rect.left); 212 } 213 } 214 } 215 if (newFont != 0) OS.SelectObject (hDC, oldFont); 216 OS.ReleaseDC (handle, hDC); 217 } 218 } 219 if (hHint == SWT.DEFAULT) { 220 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 221 int itemHeight = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0); 222 height = count * itemHeight; 223 } 224 if (width == 0) width = DEFAULT_WIDTH; 225 if (height == 0) height = DEFAULT_HEIGHT; 226 if (wHint != SWT.DEFAULT) width = wHint; 227 if (hHint != SWT.DEFAULT) height = hHint; 228 int border = getBorderWidth (); 229 width += border * 2 + INSET; 230 height += border * 2; 231 if ((style & SWT.V_SCROLL) != 0) { 232 width += OS.GetSystemMetrics (OS.SM_CXVSCROLL); 233 } 234 if ((style & SWT.H_SCROLL) != 0) { 235 height += OS.GetSystemMetrics (OS.SM_CYHSCROLL); 236 } 237 return new Point (width, height); 238 } 239 240 int defaultBackground () { 241 return OS.GetSysColor (OS.COLOR_WINDOW); 242 } 243 244 261 public void deselect (int [] indices) { 262 checkWidget (); 263 if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); 264 if (indices.length == 0) return; 265 if ((style & SWT.SINGLE) != 0) { 266 int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 267 if (oldIndex == OS.LB_ERR) return; 268 for (int i=0; i<indices.length; i++) { 269 if (oldIndex == indices [i]) { 270 OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0); 271 return; 272 } 273 } 274 return; 275 } 276 for (int i=0; i<indices.length; i++) { 277 int index = indices [i]; 278 if (index != -1) { 279 OS.SendMessage (handle, OS.LB_SETSEL, 0, index); 280 } 281 } 282 } 283 284 296 public void deselect (int index) { 297 checkWidget (); 298 if (index == -1) return; 299 if ((style & SWT.SINGLE) != 0) { 300 int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 301 if (oldIndex == OS.LB_ERR) return; 302 if (oldIndex == index) OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0); 303 return; 304 } 305 OS.SendMessage (handle, OS.LB_SETSEL, 0, index); 306 } 307 308 323 public void deselect (int start, int end) { 324 checkWidget (); 325 if (start > end) return; 326 if ((style & SWT.SINGLE) != 0) { 327 int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 328 if (oldIndex == OS.LB_ERR) return; 329 if (start <= oldIndex && oldIndex <= end) { 330 OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0); 331 } 332 return; 333 } 334 340 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 341 if (start < 0 && end < 0) return; 342 if (start >= count && end >= count) return; 343 start = Math.min (count - 1, Math.max (0, start)); 344 end = Math.min (count - 1, Math.max (0, end)); 345 OS.SendMessage (handle, OS.LB_SELITEMRANGEEX, end, start); 346 } 347 348 356 public void deselectAll () { 357 checkWidget (); 358 if ((style & SWT.SINGLE) != 0) { 359 OS.SendMessage (handle, OS.LB_SETCURSEL, -1, 0); 360 } else { 361 OS.SendMessage (handle, OS.LB_SETSEL, 0, -1); 362 } 363 } 364 365 376 public int getFocusIndex () { 377 checkWidget (); 378 int result = OS.SendMessage (handle, OS.LB_GETCARETINDEX, 0, 0); 379 if (result == 0) { 380 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 381 if (count == 0) return -1; 382 } 383 return result; 384 } 385 386 401 public String getItem (int index) { 402 checkWidget (); 403 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, index, 0); 404 if (length != OS.LB_ERR) { 405 TCHAR buffer = new TCHAR (getCodePage (), length + 1); 406 int result = OS.SendMessage (handle, OS.LB_GETTEXT, index, buffer); 407 if (result != OS.LB_ERR) return buffer.toString (0, length); 408 } 409 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 410 if (0 <= index && index < count) error (SWT.ERROR_CANNOT_GET_ITEM); 411 error (SWT.ERROR_INVALID_RANGE); 412 return ""; 413 } 414 415 425 public int getItemCount () { 426 checkWidget (); 427 int result = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 428 if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_COUNT); 429 return result; 430 } 431 432 443 public int getItemHeight () { 444 checkWidget (); 445 int result = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0); 446 if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_ITEM_HEIGHT); 447 return result; 448 } 449 450 466 public String [] getItems () { 467 checkWidget (); 468 int count = getItemCount (); 469 String [] result = new String [count]; 470 for (int i=0; i<count; i++) result [i] = getItem (i); 471 return result; 472 } 473 474 490 public String [] getSelection () { 491 checkWidget (); 492 int [] indices = getSelectionIndices (); 493 String [] result = new String [indices.length]; 494 for (int i=0; i<indices.length; i++) { 495 result [i] = getItem (indices [i]); 496 } 497 return result; 498 } 499 500 510 public int getSelectionCount () { 511 checkWidget (); 512 if ((style & SWT.SINGLE) != 0) { 513 int result = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 514 if (result == OS.LB_ERR) return 0; 515 return 1; 516 } 517 int result = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); 518 if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_COUNT); 519 return result; 520 } 521 522 533 public int getSelectionIndex () { 534 checkWidget (); 535 if ((style & SWT.SINGLE) != 0) { 536 return OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 537 } 538 int count = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); 539 if (count == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); 540 if (count == 0) return -1; 541 int index = OS.SendMessage (handle, OS.LB_GETCARETINDEX, 0, 0); 542 int result = OS.SendMessage (handle, OS.LB_GETSEL, index, 0); 543 if (result == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); 544 if (result != 0) return index; 545 int [] buffer = new int [1]; 546 result = OS.SendMessage (handle, OS.LB_GETSELITEMS, 1, buffer); 547 if (result != 1) error (SWT.ERROR_CANNOT_GET_SELECTION); 548 return buffer [0]; 549 } 550 551 567 public int [] getSelectionIndices () { 568 checkWidget (); 569 if ((style & SWT.SINGLE) != 0) { 570 int result = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 571 if (result == OS.LB_ERR) return new int [0]; 572 return new int [] {result}; 573 } 574 int length = OS.SendMessage (handle, OS.LB_GETSELCOUNT, 0, 0); 575 if (length == OS.LB_ERR) error (SWT.ERROR_CANNOT_GET_SELECTION); 576 int [] indices = new int [length]; 577 int result = OS.SendMessage (handle, OS.LB_GETSELITEMS, length, indices); 578 if (result != length) error (SWT.ERROR_CANNOT_GET_SELECTION); 579 return indices; 580 } 581 582 594 public int getTopIndex () { 595 checkWidget (); 596 return OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 597 } 598 599 618 public int indexOf (String string) { 619 return indexOf (string, 0); 620 } 621 622 641 public int indexOf (String string, int start) { 642 checkWidget (); 643 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 644 645 651 if (string.length () == 0) { 652 int count = getItemCount (); 653 for (int i=start; i<count; i++) { 654 if (string.equals (getItem (i))) return i; 655 } 656 return -1; 657 } 658 659 660 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 661 if (!(0 <= start && start < count)) return -1; 662 int index = start - 1, last; 663 TCHAR buffer = new TCHAR (getCodePage (), string, true); 664 do { 665 index = OS.SendMessage (handle, OS.LB_FINDSTRINGEXACT, last = index, buffer); 666 if (index == OS.LB_ERR || index <= last) return -1; 667 } while (!string.equals (getItem (index))); 668 return index; 669 } 670 671 684 public boolean isSelected (int index) { 685 checkWidget (); 686 int result = OS.SendMessage (handle, OS.LB_GETSEL, index, 0); 687 return (result != 0) && (result != OS.LB_ERR); 688 } 689 690 705 public void remove (int [] indices) { 706 checkWidget (); 707 if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); 708 if (indices.length == 0) return; 709 int [] newIndices = new int [indices.length]; 710 System.arraycopy (indices, 0, newIndices, 0, indices.length); 711 sort (newIndices); 712 int start = newIndices [newIndices.length - 1], end = newIndices [0]; 713 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 714 if (!(0 <= start && start <= end && end < count)) { 715 error (SWT.ERROR_INVALID_RANGE); 716 } 717 int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 718 RECT rect = null; 719 int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; 720 if ((style & SWT.H_SCROLL) != 0) { 721 rect = new RECT (); 722 hDC = OS.GetDC (handle); 723 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 724 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 725 } 726 int cp = getCodePage (); 727 int i = 0, topCount = 0, last = -1; 728 while (i < newIndices.length) { 729 int index = newIndices [i]; 730 if (index != last) { 731 TCHAR buffer = null; 732 if ((style & SWT.H_SCROLL) != 0) { 733 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, index, 0); 734 if (length == OS.LB_ERR) break; 735 buffer = new TCHAR (cp, length + 1); 736 int result = OS.SendMessage (handle, OS.LB_GETTEXT, index, buffer); 737 if (result == OS.LB_ERR) break; 738 } 739 int result = OS.SendMessage (handle, OS.LB_DELETESTRING, index, 0); 740 if (result == OS.LB_ERR) break; 741 if ((style & SWT.H_SCROLL) != 0) { 742 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 743 OS.DrawText (hDC, buffer, -1, rect, flags); 744 newWidth = Math.max (newWidth, rect.right - rect.left); 745 } 746 if (index < topIndex) topCount++; 747 last = index; 748 } 749 i++; 750 } 751 if ((style & SWT.H_SCROLL) != 0) { 752 if (newFont != 0) OS.SelectObject (hDC, oldFont); 753 OS.ReleaseDC (handle, hDC); 754 setScrollWidth (newWidth, false); 755 } 756 if (topCount > 0) { 757 topIndex -= topCount; 758 OS.SendMessage (handle, OS.LB_SETTOPINDEX, topIndex, 0); 759 } 760 if (i < newIndices.length) error (SWT.ERROR_ITEM_NOT_REMOVED); 761 } 762 763 777 public void remove (int index) { 778 checkWidget (); 779 TCHAR buffer = null; 780 if ((style & SWT.H_SCROLL) != 0) { 781 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, index, 0); 782 if (length == OS.LB_ERR) { 783 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 784 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 785 error (SWT.ERROR_INVALID_RANGE); 786 } 787 buffer = new TCHAR (getCodePage (), length + 1); 788 int result = OS.SendMessage (handle, OS.LB_GETTEXT, index, buffer); 789 if (result == OS.LB_ERR) { 790 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 791 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 792 error (SWT.ERROR_INVALID_RANGE); 793 } 794 } 795 int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 796 int result = OS.SendMessage (handle, OS.LB_DELETESTRING, index, 0); 797 if (result == OS.LB_ERR) { 798 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 799 if (0 <= index && index < count) error (SWT.ERROR_ITEM_NOT_REMOVED); 800 error (SWT.ERROR_INVALID_RANGE); 801 } 802 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (buffer, false); 803 if (index < topIndex) { 804 OS.SendMessage (handle, OS.LB_SETTOPINDEX, topIndex - 1, 0); 805 } 806 } 807 808 824 public void remove (int start, int end) { 825 checkWidget (); 826 if (start > end) return; 827 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 828 if (!(0 <= start && start <= end && end < count)) { 829 error (SWT.ERROR_INVALID_RANGE); 830 } 831 if (start == 0 && end == count - 1) { 832 removeAll (); 833 return; 834 } 835 int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 836 RECT rect = null; 837 int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; 838 if ((style & SWT.H_SCROLL) != 0) { 839 rect = new RECT (); 840 hDC = OS.GetDC (handle); 841 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 842 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 843 } 844 int cp = getCodePage (); 845 int index = start; 846 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 847 while (index <= end) { 848 TCHAR buffer = null; 849 if ((style & SWT.H_SCROLL) != 0) { 850 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, start, 0); 851 if (length == OS.LB_ERR) break; 852 buffer = new TCHAR (cp, length + 1); 853 int result = OS.SendMessage (handle, OS.LB_GETTEXT, start, buffer); 854 if (result == OS.LB_ERR) break; 855 } 856 int result = OS.SendMessage (handle, OS.LB_DELETESTRING, start, 0); 857 if (result == OS.LB_ERR) break; 858 if ((style & SWT.H_SCROLL) != 0) { 859 OS.DrawText (hDC, buffer, -1, rect, flags); 860 newWidth = Math.max (newWidth, rect.right - rect.left); 861 } 862 index++; 863 } 864 if ((style & SWT.H_SCROLL) != 0) { 865 if (newFont != 0) OS.SelectObject (hDC, oldFont); 866 OS.ReleaseDC (handle, hDC); 867 setScrollWidth (newWidth, false); 868 } 869 if (end < topIndex) { 870 topIndex -= end - start + 1; 871 OS.SendMessage (handle, OS.LB_SETTOPINDEX, topIndex, 0); 872 } 873 if (index <= end) error (SWT.ERROR_ITEM_NOT_REMOVED); 874 } 875 876 892 public void remove (String string) { 893 checkWidget (); 894 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 895 int index = indexOf (string, 0); 896 if (index == -1) error (SWT.ERROR_INVALID_ARGUMENT); 897 remove (index); 898 } 899 900 908 public void removeAll () { 909 checkWidget (); 910 OS.SendMessage (handle, OS.LB_RESETCONTENT, 0, 0); 911 if ((style & SWT.H_SCROLL) != 0) { 912 OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, 0, 0); 913 } 914 } 915 916 933 public void removeSelectionListener(SelectionListener listener) { 934 checkWidget (); 935 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 936 if (eventTable == null) return; 937 eventTable.unhook (SWT.Selection, listener); 938 eventTable.unhook (SWT.DefaultSelection,listener); 939 } 940 941 963 public void select (int [] indices) { 964 checkWidget (); 965 if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); 966 int length = indices.length; 967 if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; 968 select (indices, false); 969 } 970 971 void select (int [] indices, boolean scroll) { 972 int i = 0; 973 while (i < indices.length) { 974 int index = indices [i]; 975 if (index != -1) { 976 select (index, false); 977 } 978 i++; 979 } 980 if (scroll) showSelection (); 981 } 982 983 995 public void select (int index) { 996 checkWidget (); 997 select (index, false); 998 } 999 1000void select (int index, boolean scroll) { 1001 if (index < 0) return; 1002 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1003 if (index >= count) return; 1004 if (scroll) { 1005 if ((style & SWT.SINGLE) != 0) { 1006 OS.SendMessage (handle, OS.LB_SETCURSEL, index, 0); 1007 } else { 1008 OS.SendMessage (handle, OS.LB_SETSEL, 1, index); 1009 } 1010 return; 1011 } 1012 int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 1013 RECT itemRect = new RECT (), selectedRect = null; 1014 OS.SendMessage (handle, OS.LB_GETITEMRECT, index, itemRect); 1015 boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); 1016 if (redraw) { 1017 OS.UpdateWindow (handle); 1018 OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); 1019 } 1020 int focusIndex = -1; 1021 if ((style & SWT.SINGLE) != 0) { 1022 int oldIndex = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 1023 if (oldIndex != -1) { 1024 selectedRect = new RECT (); 1025 OS.SendMessage (handle, OS.LB_GETITEMRECT, oldIndex, selectedRect); 1026 } 1027 OS.SendMessage (handle, OS.LB_SETCURSEL, index, 0); 1028 } else { 1029 focusIndex = OS.SendMessage (handle, OS.LB_GETCARETINDEX, 0, 0); 1030 OS.SendMessage (handle, OS.LB_SETSEL, 1, index); 1031 } 1032 if ((style & SWT.MULTI) != 0) { 1033 if (focusIndex != -1) { 1034 OS.SendMessage (handle, OS.LB_SETCARETINDEX, focusIndex, 0); 1035 } 1036 } 1037 OS.SendMessage (handle, OS.LB_SETTOPINDEX, topIndex, 0); 1038 if (redraw) { 1039 OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); 1040 OS.ValidateRect (handle, null); 1041 OS.InvalidateRect (handle, itemRect, true); 1042 if (selectedRect != null) { 1043 OS.InvalidateRect (handle, selectedRect, true); 1044 } 1045 } 1046} 1047 1048 1070public void select (int start, int end) { 1071 checkWidget (); 1072 if (end < 0 || start > end || ((style & SWT.SINGLE) != 0 && start != end)) return; 1073 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1074 if (count == 0 || start >= count) return; 1075 start = Math.max (0, start); 1076 end = Math.min (end, count - 1); 1077 if ((style & SWT.SINGLE) != 0) { 1078 select (start, false); 1079 } else { 1080 select (start, end, false); 1081 } 1082} 1083 1084void select (int start, int end, boolean scroll) { 1085 1089 if (start == end) { 1090 select (start, scroll); 1091 return; 1092 } 1093 OS.SendMessage (handle, OS.LB_SELITEMRANGEEX, start, end); 1094 if (scroll) showSelection (); 1095} 1096 1097 1107public void selectAll () { 1108 checkWidget (); 1109 if ((style & SWT.SINGLE) != 0) return; 1110 OS.SendMessage (handle, OS.LB_SETSEL, 1, -1); 1111} 1112 1113void setFocusIndex (int index) { 1114 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1116 if (!(0 <= index && index < count)) return; 1117 OS.SendMessage (handle, OS.LB_SETCARETINDEX, index, 0); 1118} 1119 1120public void setFont (Font font) { 1121 checkWidget (); 1122 super.setFont (font); 1123 if ((style & SWT.H_SCROLL) != 0) setScrollWidth (); 1124} 1125 1126 1142public void setItem (int index, String string) { 1143 checkWidget (); 1144 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 1145 int topIndex = getTopIndex (); 1146 boolean isSelected = isSelected (index); 1147 remove (index); 1148 add (string, index); 1149 if (isSelected) select (index, false); 1150 setTopIndex (topIndex); 1151} 1152 1153 1167public void setItems (String [] items) { 1168 checkWidget (); 1169 if (items == null) error (SWT.ERROR_NULL_ARGUMENT); 1170 for (int i=0; i<items.length; i++) { 1171 if (items [i] == null) error (SWT.ERROR_INVALID_ARGUMENT); 1172 } 1173 int oldProc = OS.GetWindowLong (handle, OS.GWL_WNDPROC); 1174 OS.SetWindowLong (handle, OS.GWL_WNDPROC, ListProc); 1175 boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); 1176 if (redraw) { 1177 OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); 1178 } 1179 RECT rect = null; 1180 int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; 1181 if ((style & SWT.H_SCROLL) != 0) { 1182 rect = new RECT (); 1183 hDC = OS.GetDC (handle); 1184 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1185 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1186 OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, 0, 0); 1187 } 1188 int length = items.length; 1189 OS.SendMessage (handle, OS.LB_RESETCONTENT, 0, 0); 1190 OS.SendMessage (handle, OS.LB_INITSTORAGE, length, length * 32); 1191 int index = 0; 1192 int cp = getCodePage (); 1193 while (index < length) { 1194 String string = items [index]; 1195 TCHAR buffer = new TCHAR (cp, string, true); 1196 int result = OS.SendMessage (handle, OS.LB_ADDSTRING, 0, buffer); 1197 if (result == OS.LB_ERR || result == OS.LB_ERRSPACE) break; 1198 if ((style & SWT.H_SCROLL) != 0) { 1199 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1200 OS.DrawText (hDC, buffer, -1, rect, flags); 1201 newWidth = Math.max (newWidth, rect.right - rect.left); 1202 } 1203 index++; 1204 } 1205 if ((style & SWT.H_SCROLL) != 0) { 1206 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1207 OS.ReleaseDC (handle, hDC); 1208 OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth + INSET, 0); 1209 } 1210 if (redraw) { 1211 OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); 1212 1220 } 1223 OS.SetWindowLong (handle, OS.GWL_WNDPROC, oldProc); 1224 if (index < items.length) error (SWT.ERROR_ITEM_NOT_ADDED); 1225} 1226 1227void setScrollWidth () { 1228 int newWidth = 0; 1229 RECT rect = new RECT (); 1230 int newFont, oldFont = 0; 1231 int hDC = OS.GetDC (handle); 1232 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1233 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1234 int cp = getCodePage (); 1235 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1236 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1237 for (int i=0; i<count; i++) { 1238 int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, i, 0); 1239 if (length != OS.LB_ERR) { 1240 TCHAR buffer = new TCHAR (cp, length + 1); 1241 int result = OS.SendMessage (handle, OS.LB_GETTEXT, i, buffer); 1242 if (result != OS.LB_ERR) { 1243 OS.DrawText (hDC, buffer, -1, rect, flags); 1244 newWidth = Math.max (newWidth, rect.right - rect.left); 1245 } 1246 } 1247 } 1248 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1249 OS.ReleaseDC (handle, hDC); 1250 OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth + INSET, 0); 1251} 1252 1253void setScrollWidth (TCHAR buffer, boolean grow) { 1254 RECT rect = new RECT (); 1255 int newFont, oldFont = 0; 1256 int hDC = OS.GetDC (handle); 1257 newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); 1258 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 1259 int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; 1260 OS.DrawText (hDC, buffer, -1, rect, flags); 1261 if (newFont != 0) OS.SelectObject (hDC, oldFont); 1262 OS.ReleaseDC (handle, hDC); 1263 setScrollWidth (rect.right - rect.left, grow); 1264} 1265 1266void setScrollWidth (int newWidth, boolean grow) { 1267 newWidth += INSET; 1268 int width = OS.SendMessage (handle, OS.LB_GETHORIZONTALEXTENT, 0, 0); 1269 if (grow) { 1270 if (newWidth <= width) return; 1271 OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth, 0); 1272 } else { 1273 if (newWidth < width) return; 1274 setScrollWidth (); 1275 } 1276} 1277 1278 1299public void setSelection(int [] indices) { 1300 checkWidget (); 1301 if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); 1302 deselectAll (); 1303 int length = indices.length; 1304 if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; 1305 select (indices, true); 1306 if ((style & SWT.MULTI) != 0) { 1307 int focusIndex = indices [0]; 1308 if (focusIndex >= 0) setFocusIndex (focusIndex); 1309 } 1310} 1311 1312 1334public void setSelection (String [] items) { 1335 checkWidget (); 1336 if (items == null) error (SWT.ERROR_NULL_ARGUMENT); 1337 deselectAll (); 1338 int length = items.length; 1339 if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; 1340 int focusIndex = -1; 1341 for (int i=length-1; i>=0; --i) { 1342 String string = items [i]; 1343 int index = 0; 1344 if (string != null) { 1345 int localFocus = -1; 1346 while ((index = indexOf (string, index)) != -1) { 1347 if (localFocus == -1) localFocus = index; 1348 select (index, false); 1349 if ((style & SWT.SINGLE) != 0 && isSelected (index)) { 1350 showSelection (); 1351 return; 1352 } 1353 index++; 1354 } 1355 if (localFocus != -1) focusIndex = localFocus; 1356 } 1357 } 1358 if ((style & SWT.MULTI) != 0) { 1359 if (focusIndex >= 0) setFocusIndex (focusIndex); 1360 } 1361} 1362 1363 1378public void setSelection (int index) { 1379 checkWidget (); 1380 deselectAll (); 1381 select (index, true); 1382 if ((style & SWT.MULTI) != 0) { 1383 if (index >= 0) setFocusIndex (index); 1384 } 1385} 1386 1387 1408public void setSelection (int start, int end) { 1409 checkWidget (); 1410 deselectAll (); 1411 if (end < 0 || start > end || ((style & SWT.SINGLE) != 0 && start != end)) return; 1412 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1413 if (count == 0 || start >= count) return; 1414 start = Math.max (0, start); 1415 end = Math.min (end, count - 1); 1416 if ((style & SWT.SINGLE) != 0) { 1417 select (start, true); 1418 } else { 1419 select (start, end, true); 1420 setFocusIndex (start); 1421 } 1422} 1423 1424 1436public void setTopIndex (int index) { 1437 checkWidget (); 1438 int result = OS.SendMessage (handle, OS.LB_SETTOPINDEX, index, 0); 1439 if (result == OS.LB_ERR) { 1440 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1441 index = Math.min (count - 1, Math.max (0, index)); 1442 OS.SendMessage (handle, OS.LB_SETTOPINDEX, index, 0); 1443 } 1444} 1445 1446 1456public void showSelection () { 1457 checkWidget (); 1458 int index; 1459 if ((style & SWT.SINGLE) != 0) { 1460 index = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); 1461 } else { 1462 int [] indices = new int [1]; 1463 int result = OS.SendMessage (handle, OS.LB_GETSELITEMS, 1, indices); 1464 index = indices [0]; 1465 if (result != 1) index = -1; 1466 } 1467 if (index == -1) return; 1468 int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); 1469 if (count == 0) return; 1470 int height = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0); 1471 forceResize (); 1472 RECT rect = new RECT (); 1473 OS.GetClientRect (handle, rect); 1474 int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 1475 int visibleCount = Math.max (rect.bottom / height, 1); 1476 int bottomIndex = Math.min (topIndex + visibleCount, count) - 1; 1477 if (topIndex <= index && index <= bottomIndex) return; 1478 int newTop = Math.min (Math.max (index - (visibleCount / 2), 0), count - 1); 1479 OS.SendMessage (handle, OS.LB_SETTOPINDEX, newTop, 0); 1480} 1481 1482int widgetStyle () { 1483 int bits = super.widgetStyle () | OS.LBS_NOTIFY | OS.LBS_NOINTEGRALHEIGHT; 1484 if ((style & SWT.SINGLE) != 0) return bits; 1485 if ((style & SWT.MULTI) != 0) { 1486 if ((style & SWT.SIMPLE) != 0) return bits | OS.LBS_MULTIPLESEL; 1487 return bits | OS.LBS_EXTENDEDSEL; 1488 } 1489 return bits; 1490} 1491 1492TCHAR windowClass () { 1493 return ListClass; 1494} 1495 1496int windowProc () { 1497 return ListProc; 1498} 1499 1500LRESULT WM_SIZE (int wParam, int lParam) { 1501 1513 int oldIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 1514 LRESULT result = super.WM_SIZE (wParam, lParam); 1515 if (!isDisposed ()) { 1516 SCROLLINFO info = new SCROLLINFO (); 1517 info.cbSize = SCROLLINFO.sizeof; 1518 info.fMask = OS.SIF_POS; 1519 if (OS.GetScrollInfo (handle, OS.SB_HORZ, info)) { 1520 if (info.nPos != 0) OS.InvalidateRect (handle, null, true); 1521 } 1522 int newIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); 1523 if (oldIndex != newIndex) OS.InvalidateRect (handle, null, true); 1524 } 1525 return result; 1526} 1527 1528LRESULT wmCommandChild (int wParam, int lParam) { 1529 int code = wParam >> 16; 1530 switch (code) { 1531 case OS.LBN_SELCHANGE: 1532 postEvent (SWT.Selection); 1533 break; 1534 case OS.LBN_DBLCLK: 1535 postEvent (SWT.DefaultSelection); 1536 break; 1537 } 1538 return super.wmCommandChild (wParam, lParam); 1539} 1540 1541 1542 1543} 1544 | Popular Tags |