📄 list.java
字号:
/** * Sets the text of the item in the receiver's list at the given * zero-relative index to the string argument. This is equivalent * to <code>remove</code>'ing the old item at the index, and then * <code>add</code>'ing the new item at that index. * * @param index the index for the item * @param string the new text for the item * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li> * <li>ERROR_NULL_ARGUMENT - if the string is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * @exception SWTError <ul> * <li>ERROR_ITEM_NOT_REMOVED - if the remove operation fails because of an operating system failure</li> * <li>ERROR_ITEM_NOT_ADDED - if the add operation fails because of an operating system failure</li> * </ul> */public void setItem (int index, String string) { checkWidget (); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); int topIndex = getTopIndex (); boolean isSelected = isSelected (index); remove (index); add (string, index); if (isSelected) select (index, false); setTopIndex (topIndex);}/** * Sets the receiver's items to be the given array of items. * * @param items the array of items * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the items array is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * @exception SWTError <ul> * <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li> * </ul> */public void setItems (String [] items) { checkWidget (); if (items == null) error (SWT.ERROR_NULL_ARGUMENT); int oldProc = OS.GetWindowLong (handle, OS.GWL_WNDPROC); OS.SetWindowLong (handle, OS.GWL_WNDPROC, ListProc); boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); if (redraw) { OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); } RECT rect = null; int hDC = 0, oldFont = 0, newFont = 0, newWidth = 0; if ((style & SWT.H_SCROLL) != 0) { rect = new RECT (); hDC = OS.GetDC (handle); newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, 0, 0); } int length = items.length; OS.SendMessage (handle, OS.LB_RESETCONTENT, 0, 0); OS.SendMessage (handle, OS.LB_INITSTORAGE, length, length * 32); int index = 0; int cp = getCodePage (); while (index < length) { String string = items [index]; if (string == null) break; TCHAR buffer = new TCHAR (cp, string, true); int result = OS.SendMessage (handle, OS.LB_ADDSTRING, 0, buffer); if (result == OS.LB_ERR || result == OS.LB_ERRSPACE) break; if ((style & SWT.H_SCROLL) != 0) { int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; OS.DrawText (hDC, buffer, buffer.length (), rect, flags); newWidth = Math.max (newWidth, rect.right - rect.left); } index++; } if ((style & SWT.H_SCROLL) != 0) { if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC); OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth + 3, 0); } if (redraw) { OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); /* * This code is intentionally commented. The window proc * for the list box implements WM_SETREDRAW to invalidate * and erase the widget. This is undocumented behavior. * The commented code below shows what is actually happening * and reminds us that we are relying on this undocumented * behavior. */// int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;// OS.RedrawWindow (handle, null, 0, flags); } OS.SetWindowLong (handle, OS.GWL_WNDPROC, oldProc); if (index < items.length) error (SWT.ERROR_ITEM_NOT_ADDED);}void setScrollWidth () { int newWidth = 0; RECT rect = new RECT (); int newFont, oldFont = 0; int hDC = OS.GetDC (handle); newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); int cp = getCodePage (); int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; for (int i=0; i<count; i++) { int length = OS.SendMessage (handle, OS.LB_GETTEXTLEN, i, 0); if (length != OS.LB_ERR) { TCHAR buffer = new TCHAR (cp, length + 1); int result = OS.SendMessage (handle, OS.LB_GETTEXT, i, buffer); if (result != OS.LB_ERR) { OS.DrawText (hDC, buffer, -1, rect, flags); newWidth = Math.max (newWidth, rect.right - rect.left); } } } if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC); OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth + 3, 0);}void setScrollWidth (TCHAR buffer, boolean grow) { RECT rect = new RECT (); int newFont, oldFont = 0; int hDC = OS.GetDC (handle); newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; OS.DrawText (hDC, buffer, -1, rect, flags); if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC); setScrollWidth (rect.right - rect.left, grow);}void setScrollWidth (int newWidth, boolean grow) { int width = OS.SendMessage (handle, OS.LB_GETHORIZONTALEXTENT, 0, 0); if (grow) { if (newWidth <= width) return; OS.SendMessage (handle, OS.LB_SETHORIZONTALEXTENT, newWidth + 3, 0); } else { if (newWidth < width) return; setScrollWidth (); }}/** * Selects the items at the given zero-relative indices in the receiver. * The current selection is cleared before the new items are selected. * <p> * Indices that are out of range and duplicate indices are ignored. * If the receiver is single-select and multiple indices are specified, * then all indices are ignored. * * @param indices the indices of the items to select * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the array of indices is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see List#deselectAll() * @see List#select(int[]) */public void setSelection(int [] indices) { checkWidget (); if (indices == null) error (SWT.ERROR_NULL_ARGUMENT); deselectAll (); int length = indices.length; if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; select (indices, true); if ((style & SWT.MULTI) != 0) { int focusIndex = indices [0]; if (focusIndex >= 0) setFocusIndex (focusIndex); }}/** * Sets the receiver's selection to be the given array of items. * The current selection is cleared before the new items are selected. * <p> * Items that are not in the receiver are ignored. * If the receiver is single-select and multiple items are specified, * then all items are ignored. * * @param items the array of items * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the array of items is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see List#deselectAll() * @see List#select(int[]) * @see List#setSelection(int[]) */public void setSelection (String [] items) { checkWidget (); if (items == null) error (SWT.ERROR_NULL_ARGUMENT); deselectAll (); int length = items.length; if (length == 0 || ((style & SWT.SINGLE) != 0 && length > 1)) return; int focusIndex = -1; for (int i=length-1; i>=0; --i) { String string = items [i]; int index = 0; if (string != null) { int localFocus = -1; while ((index = indexOf (string, index)) != -1) { if (localFocus == -1) localFocus = index; select (index, false); if ((style & SWT.SINGLE) != 0 && isSelected (index)) { showSelection (); return; } index++; } if (localFocus != -1) focusIndex = localFocus; } } if ((style & SWT.MULTI) != 0) { if (focusIndex >= 0) setFocusIndex (focusIndex); }}/** * Selects the item at the given zero-relative index in the receiver. * If the item at the index was already selected, it remains selected. * The current selected is first cleared, then the new items are selected. * Indices that are out of range are ignored. * * @param index the index of the item to select * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * @see List#deselectAll() * @see List#select(int) */public void setSelection (int index) { checkWidget (); if ((style & SWT.MULTI) != 0) deselectAll (); select (index, true); if ((style & SWT.MULTI) != 0) { if (index >= 0) setFocusIndex (index); }}/** * Selects the items in the range specified by the given zero-relative * indices in the receiver. The range of indices is inclusive. * The current selection is cleared before the new items are selected. * <p> * Indices that are out of range are ignored and no items will be selected * if start is greater than end. * If the receiver is single-select and there is more than one item in the * given range, then all indices are ignored. * * @param start the start index of the items to select * @param end the end index of the items to select * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see List#deselectAll() * @see List#select(int,int) */public void setSelection (int start, int end) { checkWidget (); deselectAll (); if (end < 0 || start > end || ((style & SWT.SINGLE) != 0 && start != end)) return; int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (count == 0 || start >= count) return; start = Math.max (0, start); end = Math.min (end, count - 1); if ((style & SWT.SINGLE) != 0) { select (start, true); } else { select (start, end, true); setFocusIndex (start); }}/** * Sets the zero-relative index of the item which is currently * at the top of the receiver. This index can change when items * are scrolled or new items are added and removed. * * @param index the index of the top item * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setTopIndex (int index) { checkWidget (); int result = OS.SendMessage (handle, OS.LB_SETTOPINDEX, index, 0); if (result == OS.LB_ERR) { int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); index = Math.min (count - 1, Math.max (0, index)); OS.SendMessage (handle, OS.LB_SETTOPINDEX, index, 0); }}/** * Shows the selection. If the selection is already showing in the receiver, * this method simply returns. Otherwise, the items are scrolled until * the selection is visible. * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void showSelection () { checkWidget (); int index; if ((style & SWT.SINGLE) != 0) { index = OS.SendMessage (handle, OS.LB_GETCURSEL, 0, 0); } else { int [] indices = new int [1]; int result = OS.SendMessage (handle, OS.LB_GETSELITEMS, 1, indices); index = indices [0]; if (result != 1) index = -1; } if (index == -1) return; int count = OS.SendMessage (handle, OS.LB_GETCOUNT, 0, 0); if (count == 0) return; int height = OS.SendMessage (handle, OS.LB_GETITEMHEIGHT, 0, 0); forceResize (); RECT rect = new RECT (); OS.GetClientRect (handle, rect); int topIndex = OS.SendMessage (handle, OS.LB_GETTOPINDEX, 0, 0); int visibleCount = Math.max (rect.bottom / height, 1); int bottomIndex = Math.min (topIndex + visibleCount + 1, count - 1); if ((topIndex <= index) && (index <= bottomIndex)) return; int newTop = Math.min (Math.max (index - (visibleCount / 2), 0), count - 1); OS.SendMessage (handle, OS.LB_SETTOPINDEX, newTop, 0);}int widgetStyle () { int bits = super.widgetStyle () | OS.LBS_NOTIFY | OS.LBS_NOINTEGRALHEIGHT; if ((style & SWT.SINGLE) != 0) return bits; if ((style & SWT.MULTI) != 0) { if ((style & SWT.SIMPLE) != 0) return bits | OS.LBS_MULTIPLESEL; return bits | OS.LBS_EXTENDEDSEL; } return bits;}TCHAR windowClass () { return ListClass;}int windowProc () { return ListProc;}LRESULT wmCommandChild (int wParam, int lParam) { int code = wParam >> 16; switch (code) { case OS.LBN_SELCHANGE: postEvent (SWT.Selection); break; case OS.LBN_DBLCLK: postEvent (SWT.DefaultSelection); break; } return super.wmCommandChild (wParam, lParam);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -