| /******************************************************************************* |
| * Copyright (c) 2000, 2005 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.swt.widgets; |
| |
| |
| import org.eclipse.swt.internal.carbon.OS; |
| import org.eclipse.swt.internal.carbon.DataBrowserCallbacks; |
| import org.eclipse.swt.internal.carbon.DataBrowserCustomCallbacks; |
| import org.eclipse.swt.internal.carbon.DataBrowserListViewColumnDesc; |
| import org.eclipse.swt.internal.carbon.DataBrowserListViewHeaderDesc; |
| import org.eclipse.swt.internal.carbon.Rect; |
| |
| import org.eclipse.swt.*; |
| import org.eclipse.swt.events.*; |
| import org.eclipse.swt.graphics.*; |
| |
| /** |
| * Instances of this class provide a selectable user interface object |
| * that displays a hierarchy of items and issue notification when an |
| * item in the hierarchy is selected. |
| * <p> |
| * The item children that may be added to instances of this class |
| * must be of type <code>TreeItem</code>. |
| * </p><p> |
| * Note that although this class is a subclass of <code>Composite</code>, |
| * it does not make sense to add <code>Control</code> children to it, |
| * or set a layout on it. |
| * </p><p> |
| * <dl> |
| * <dt><b>Styles:</b></dt> |
| * <dd>SINGLE, MULTI, CHECK, FULL_SELECTION</dd> |
| * <dt><b>Events:</b></dt> |
| * <dd>Selection, DefaultSelection, Collapse, Expand</dd> |
| * </dl> |
| * <p> |
| * Note: Only one of the styles SINGLE and MULTI may be specified. |
| * </p><p> |
| * IMPORTANT: This class is <em>not</em> intended to be subclassed. |
| * </p> |
| */ |
| public class Tree extends Composite { |
| TreeItem [] items; |
| TreeColumn [] columns; |
| GC paintGC; |
| int clickCount; |
| int columnCount, column_id, idCount, anchorFirst, anchorLast, headerHeight; |
| boolean ignoreRedraw, ignoreSelect, wasSelected, ignoreExpand, wasExpanded; |
| Rectangle imageBounds; |
| TreeItem showItem; |
| int lastHittest; |
| static final int CHECK_COLUMN_ID = 1024; |
| static final int COLUMN_ID = 1025; |
| static final int EXTRA_WIDTH = 30; |
| static final int CHECK_COLUMN_WIDTH = 25; |
| |
| /** |
| * Constructs a new instance of this class given its parent |
| * and a style value describing its behavior and appearance. |
| * <p> |
| * The style value is either one of the style constants defined in |
| * class <code>SWT</code> which is applicable to instances of this |
| * class, or must be built by <em>bitwise OR</em>'ing together |
| * (that is, using the <code>int</code> "|" operator) two or more |
| * of those <code>SWT</code> style constants. The class description |
| * lists the style constants that are applicable to the class. |
| * Style bits are also inherited from superclasses. |
| * </p> |
| * |
| * @param parent a composite control which will be the parent of the new instance (cannot be null) |
| * @param style the style of control to construct |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| * |
| * @see SWT#SINGLE |
| * @see SWT#MULTI |
| * @see SWT#CHECK |
| * @see Widget#checkSubclass |
| * @see Widget#getStyle |
| */ |
| public Tree (Composite parent, int style) { |
| super (parent, checkStyle (style)); |
| } |
| |
| /** |
| * Adds the listener to the collection of listeners who will |
| * be notified when the receiver's selection changes, by sending |
| * it one of the messages defined in the <code>SelectionListener</code> |
| * interface. |
| * <p> |
| * When <code>widgetSelected</code> is called, the item field of the event object is valid. |
| * If the reciever has <code>SWT.CHECK</code> style set and the check selection changes, |
| * the event object detail field contains the value <code>SWT.CHECK</code>. |
| * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked. |
| * The item field of the event object is valid for default selection, but the detail field is not used. |
| * </p> |
| * |
| * @param listener the listener which should be notified |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the listener 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 SelectionListener |
| * @see #removeSelectionListener |
| * @see SelectionEvent |
| */ |
| public void addSelectionListener(SelectionListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| TypedListener typedListener = new TypedListener (listener); |
| addListener (SWT.Selection, typedListener); |
| addListener (SWT.DefaultSelection, typedListener); |
| } |
| |
| /** |
| * Adds the listener to the collection of listeners who will |
| * be notified when an item in the receiver is expanded or collapsed |
| * by sending it one of the messages defined in the <code>TreeListener</code> |
| * interface. |
| * |
| * @param listener the listener which should be notified |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the listener 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 TreeListener |
| * @see #removeTreeListener |
| */ |
| public void addTreeListener(TreeListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| TypedListener typedListener = new TypedListener (listener); |
| addListener (SWT.Expand, typedListener); |
| addListener (SWT.Collapse, typedListener); |
| } |
| |
| int calculateWidth (TreeItem [] items, GC gc) { |
| int width = 0; |
| for (int i = 0; i < items.length; i++) { |
| TreeItem item = items [i]; |
| width = Math.max (width, item.calculateWidth (0, gc)); |
| if (item._getExpanded ()) { |
| width = Math.max (width, calculateWidth (item.getItems (), gc)); |
| } |
| } |
| return width; |
| } |
| |
| int callPaintEventHandler (int control, int damageRgn, int visibleRgn, int theEvent, int nextHandler) { |
| GC currentGC = paintGC; |
| if (currentGC == null) { |
| GCData data = new GCData (); |
| data.paintEvent = theEvent; |
| data.visibleRgn = visibleRgn; |
| paintGC = GC.carbon_new (this, data); |
| } |
| int result = super.callPaintEventHandler (control, damageRgn, visibleRgn, theEvent, nextHandler); |
| if (currentGC == null) { |
| paintGC.dispose (); |
| paintGC = null; |
| } |
| return result; |
| } |
| |
| boolean checkData (TreeItem item, boolean redraw) { |
| if (item.cached) return true; |
| if ((style & SWT.VIRTUAL) != 0) { |
| item.cached = true; |
| Event event = new Event (); |
| event.item = item; |
| ignoreRedraw = true; |
| sendEvent (SWT.SetData, event); |
| //widget could be disposed at this point |
| ignoreRedraw = false; |
| if (isDisposed () || item.isDisposed ()) return false; |
| if (redraw) { |
| if (!setScrollWidth (item)) item.redraw (OS.kDataBrowserNoItem); |
| } |
| } |
| return true; |
| } |
| |
| static int checkStyle (int style) { |
| /* |
| * Feature in Windows. It is not possible to create |
| * a tree that scrolls and does not have scroll bars. |
| * The TVS_NOSCROLL style will remove the scroll bars |
| * but the tree will never scroll. Therefore, no matter |
| * what style bits are specified, set the H_SCROLL and |
| * V_SCROLL bits so that the SWT style will match the |
| * widget that Windows creates. |
| */ |
| style |= SWT.H_SCROLL | SWT.V_SCROLL; |
| style &= ~SWT.VIRTUAL; |
| return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0); |
| } |
| |
| protected void checkSubclass () { |
| if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); |
| } |
| |
| public Point computeSize (int wHint, int hHint, boolean changed) { |
| checkWidget (); |
| int width = 0; |
| if (wHint == SWT.DEFAULT) { |
| TreeItem [] items = getItems (null); |
| GC gc = new GC (this); |
| int columnCount = Math.max (this.columnCount, 1); |
| for (int j=0; j<columnCount; j++) { |
| int columnWidth = this.columnCount != 0 ? columns [j].getWidth () : 0; |
| for (int i=0; i<items.length; i++) { |
| columnWidth = Math.max (columnWidth, items [i].calculateWidth (j, gc)); |
| } |
| width += columnWidth + EXTRA_WIDTH; |
| } |
| gc.dispose (); |
| if ((style & SWT.CHECK) != 0) width += CHECK_COLUMN_WIDTH; |
| } else { |
| width = wHint; |
| } |
| if (width <= 0) width = DEFAULT_WIDTH; |
| int height = 0; |
| if (hHint == SWT.DEFAULT) { |
| height = getItemCount () * getItemHeight () + getHeaderHeight(); |
| } else { |
| height = hHint; |
| } |
| if (height <= 0) height = DEFAULT_HEIGHT; |
| Rectangle rect = computeTrim (0, 0, width, height); |
| return new Point (rect.width, rect.height); |
| } |
| |
| public Rectangle computeTrim (int x, int y, int width, int height) { |
| checkWidget(); |
| int border = 0; |
| int [] outMetric = new int [1]; |
| OS.GetThemeMetric (OS.kThemeMetricFocusRectOutset, outMetric); |
| border += outMetric [0]; |
| OS.GetThemeMetric (OS.kThemeMetricEditTextFrameOutset, outMetric); |
| border += outMetric [0]; |
| Rect rect = new Rect (); |
| OS.GetDataBrowserScrollBarInset (handle, rect); |
| x -= rect.left + border; |
| y -= rect.top + border; |
| width += rect.left + rect.right + border + border; |
| height += rect.top + rect.bottom + border + border; |
| return new Rectangle (x, y, width, height); |
| } |
| |
| void createHandle () { |
| column_id = COLUMN_ID; |
| int [] outControl = new int [1]; |
| int window = OS.GetControlOwner (parent.handle); |
| OS.CreateDataBrowserControl (window, null, OS.kDataBrowserListView, outControl); |
| if (outControl [0] == 0) error (SWT.ERROR_NO_HANDLES); |
| handle = outControl [0]; |
| if (!drawFocusRing ()) { |
| OS.SetControlData (handle, OS.kControlEntireControl, OS.kControlDataBrowserIncludesFrameAndFocusTag, 1, new byte[] {0}); |
| } |
| int selectionFlags = (style & SWT.SINGLE) != 0 ? OS.kDataBrowserSelectOnlyOne | OS.kDataBrowserNeverEmptySelectionSet : OS.kDataBrowserCmdTogglesSelection; |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags); |
| short [] height = new short [1]; |
| OS.GetDataBrowserListViewHeaderBtnHeight (handle, height); |
| headerHeight = height [0]; |
| OS.SetDataBrowserListViewHeaderBtnHeight (handle, (short) 0); |
| OS.SetDataBrowserHasScrollBars (handle, (style & SWT.H_SCROLL) != 0, (style & SWT.V_SCROLL) != 0); |
| int position = 0; |
| if ((style & SWT.CHECK) != 0) { |
| DataBrowserListViewColumnDesc checkColumn = new DataBrowserListViewColumnDesc (); |
| checkColumn.headerBtnDesc_version = OS.kDataBrowserListViewLatestHeaderDesc; |
| checkColumn.propertyDesc_propertyID = CHECK_COLUMN_ID; |
| checkColumn.propertyDesc_propertyType = OS.kDataBrowserCheckboxType; |
| checkColumn.propertyDesc_propertyFlags = OS.kDataBrowserPropertyIsMutable; |
| //TODO - CHECK column size |
| checkColumn.headerBtnDesc_minimumWidth = CHECK_COLUMN_WIDTH; |
| checkColumn.headerBtnDesc_maximumWidth = CHECK_COLUMN_WIDTH; |
| checkColumn.headerBtnDesc_initialOrder = (short) OS.kDataBrowserOrderIncreasing; |
| OS.AddDataBrowserListViewColumn (handle, checkColumn, position++); |
| } |
| DataBrowserListViewColumnDesc column = new DataBrowserListViewColumnDesc (); |
| column.headerBtnDesc_version = OS.kDataBrowserListViewLatestHeaderDesc; |
| column.propertyDesc_propertyID = column_id; |
| column.propertyDesc_propertyType = OS.kDataBrowserCustomType; |
| column.propertyDesc_propertyFlags = OS.kDataBrowserListViewSelectionColumn | OS.kDataBrowserDefaultPropertyFlags | OS.kDataBrowserListViewSortableColumn; |
| column.headerBtnDesc_maximumWidth = 0x7fff; |
| column.headerBtnDesc_initialOrder = (short) OS.kDataBrowserOrderIncreasing; |
| OS.AddDataBrowserListViewColumn (handle, column, position); |
| OS.SetDataBrowserListViewDisclosureColumn (handle, column_id, false); |
| OS.SetDataBrowserTableViewNamedColumnWidth (handle, column_id, (short) 0); |
| |
| /* |
| * Feature in the Macintosh. Scroll bars are not created until |
| * the data browser needs to draw them. The fix is to force the scroll |
| * bars to be created by temporarily giving the widget a size, drawing |
| * it on a offscreen buffer to avoid flashes and then restoring it to |
| * size zero. |
| */ |
| int size = 50; |
| Rect rect = new Rect (); |
| rect.right = rect.bottom = (short) size; |
| OS.SetControlBounds (handle, rect); |
| int bpl = size * 4; |
| int [] gWorld = new int [1]; |
| int data = OS.NewPtr (bpl * size); |
| OS.NewGWorldFromPtr (gWorld, OS.k32ARGBPixelFormat, rect, 0, 0, 0, data, bpl); |
| int [] curPort = new int [1]; |
| int [] curGWorld = new int [1]; |
| OS.GetGWorld (curPort, curGWorld); |
| OS.SetGWorld (gWorld [0], curGWorld [0]); |
| OS.DrawControlInCurrentPort (handle); |
| OS.SetGWorld (curPort [0], curGWorld [0]); |
| OS.DisposeGWorld (gWorld [0]); |
| OS.DisposePtr (data); |
| rect.right = rect.bottom = (short) 0; |
| OS.SetControlBounds (handle, rect); |
| } |
| |
| void createItem (TreeColumn column, int index) { |
| if (!(0 <= index && index <= columnCount)) error (SWT.ERROR_INVALID_RANGE); |
| if (index == 0) { |
| // first column must be left aligned |
| column.style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); |
| column.style |= SWT.LEFT; |
| } |
| column.id = column_id + idCount++; |
| int position = index + ((style & SWT.CHECK) != 0 ? 1 : 0); |
| if (columnCount != 0) { |
| DataBrowserListViewColumnDesc desc = new DataBrowserListViewColumnDesc (); |
| desc.headerBtnDesc_version = OS.kDataBrowserListViewLatestHeaderDesc; |
| desc.propertyDesc_propertyID = column.id; |
| desc.propertyDesc_propertyType = OS.kDataBrowserCustomType; |
| desc.propertyDesc_propertyFlags = OS.kDataBrowserListViewSelectionColumn | OS.kDataBrowserDefaultPropertyFlags | OS.kDataBrowserListViewSortableColumn; |
| desc.headerBtnDesc_maximumWidth = 0x7fff; |
| desc.headerBtnDesc_initialOrder = OS.kDataBrowserOrderIncreasing; |
| desc.headerBtnDesc_btnFontStyle_just = OS.teFlushLeft; |
| if ((style & SWT.CENTER) != 0) desc.headerBtnDesc_btnFontStyle_just = OS.teCenter; |
| if ((style & SWT.RIGHT) != 0) desc.headerBtnDesc_btnFontStyle_just = OS.teFlushRight; |
| desc.headerBtnDesc_btnFontStyle_flags |= OS.kControlUseJustMask; |
| OS.AddDataBrowserListViewColumn (handle, desc, position); |
| OS.SetDataBrowserTableViewNamedColumnWidth (handle, column.id, (short)0); |
| |
| if (index == 0) { |
| int [] disclosure = new int [1]; |
| boolean [] expandableRows = new boolean [1]; |
| OS.GetDataBrowserListViewDisclosureColumn (handle, disclosure, expandableRows); |
| OS.SetDataBrowserListViewDisclosureColumn (handle, column.id, expandableRows [0]); |
| } |
| } |
| if (columnCount == columns.length) { |
| TreeColumn [] newColumns = new TreeColumn [columnCount + 4]; |
| System.arraycopy (columns, 0, newColumns, 0, columns.length); |
| columns = newColumns; |
| } |
| System.arraycopy (columns, index, columns, index + 1, columnCount++ - index); |
| columns [index] = column; |
| if (columnCount >= 1) { |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null) { |
| String [] strings = item.strings; |
| if (strings != null) { |
| String [] temp = new String [columnCount]; |
| System.arraycopy (strings, 0, temp, 0, index); |
| System.arraycopy (strings, index, temp, index+1, columnCount-index-1); |
| temp [index] = ""; |
| item.strings = temp; |
| } |
| if (index == 0) item.text = ""; |
| Image [] images = item.images; |
| if (images != null) { |
| Image [] temp = new Image [columnCount]; |
| System.arraycopy (images, 0, temp, 0, index); |
| System.arraycopy (images, index, temp, index+1, columnCount-index-1); |
| item.images = temp; |
| } |
| if (index == 0) item.image = null; |
| Color [] cellBackground = item.cellBackground; |
| if (cellBackground != null) { |
| Color [] temp = new Color [columnCount]; |
| System.arraycopy (cellBackground, 0, temp, 0, index); |
| System.arraycopy (cellBackground, index, temp, index+1, columnCount-index-1); |
| item.cellBackground = temp; |
| } |
| Color [] cellForeground = item.cellForeground; |
| if (cellForeground != null) { |
| Color [] temp = new Color [columnCount]; |
| System.arraycopy (cellForeground, 0, temp, 0, index); |
| System.arraycopy (cellForeground, index, temp, index+1, columnCount-index-1); |
| item.cellForeground = temp; |
| } |
| Font [] cellFont = item.cellFont; |
| if (cellFont != null) { |
| Font [] temp = new Font [columnCount]; |
| System.arraycopy (cellFont, 0, temp, 0, index); |
| System.arraycopy (cellFont, index, temp, index+1, columnCount-index-1); |
| item.cellFont = temp; |
| } |
| } |
| } |
| } |
| } |
| |
| void createItem (TreeItem item, TreeItem parentItem, int index) { |
| int count = 0; |
| int id = items.length; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] == null) { |
| if (id == items.length) id = i; |
| } else { |
| if (items [i].parentItem == parentItem) count++; |
| } |
| } |
| if (index == -1) index = count; |
| if (!(0 <= index && index <= count)) error (SWT.ERROR_INVALID_RANGE); |
| item.index = index; |
| if (index != count) { |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == parentItem) { |
| if (items [i].index >= item.index) items [i].index++; |
| } |
| } |
| } |
| if (id == items.length) { |
| TreeItem [] newItems = new TreeItem [items.length + 4]; |
| System.arraycopy (items, 0, newItems, 0, items.length); |
| items = newItems; |
| } |
| items [id] = item; |
| item.id = id + 1; |
| int parentID = OS.kDataBrowserNoItem; |
| boolean expanded = true; |
| if (parentItem != null) { |
| parentID = parentItem.id; |
| expanded = parentItem.getExpanded (); |
| } |
| if (expanded) { |
| if (OS.AddDataBrowserItems (handle, parentID, 1, new int[] {item.id}, OS.kDataBrowserItemNoProperty) != OS.noErr) { |
| items [id] = null; |
| error (SWT.ERROR_ITEM_NOT_ADDED); |
| } |
| } else { |
| /* |
| * Bug in the Macintosh. When the first child of a tree item is |
| * added and the parent is not expanded, the parent does not |
| * redraw to show the expander. The fix is to force a redraw. |
| */ |
| if (count == 0 && parentItem != null) parentItem.redraw (OS.kDataBrowserNoItem); |
| } |
| } |
| |
| ScrollBar createScrollBar (int style) { |
| return createStandardBar (style); |
| } |
| |
| void createWidget () { |
| super.createWidget (); |
| items = new TreeItem [4]; |
| columns = new TreeColumn [4]; |
| } |
| |
| Color defaultBackground () { |
| return display.getSystemColor (SWT.COLOR_LIST_BACKGROUND); |
| } |
| |
| Color defaultForeground () { |
| return display.getSystemColor (SWT.COLOR_LIST_FOREGROUND); |
| } |
| |
| int defaultThemeFont () { |
| if (display.smallFonts) return OS.kThemeSmallSystemFont; |
| return OS.kThemeViewsFont; |
| } |
| |
| /** |
| * Deselects all selected items in the receiver. |
| * |
| * @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 deselectAll () { |
| checkWidget (); |
| ignoreSelect = true; |
| /* |
| * Bug in the Macintosh. When the DataBroswer selection flags includes |
| * both kDataBrowserNeverEmptySelectionSet and kDataBrowserSelectOnlyOne, |
| * two items are selected when SetDataBrowserSelectedItems() is called |
| * with kDataBrowserItemsAssign to assign a new seletion despite the fact |
| * that kDataBrowserSelectOnlyOne was specified. The fix is to save and |
| * restore kDataBrowserNeverEmptySelectionSet around each call to |
| * SetDataBrowserSelectedItems(). |
| */ |
| int [] selectionFlags = null; |
| if ((style & SWT.SINGLE) != 0) { |
| selectionFlags = new int [1]; |
| OS.GetDataBrowserSelectionFlags (handle, selectionFlags); |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0] & ~OS.kDataBrowserNeverEmptySelectionSet); |
| } |
| OS.SetDataBrowserSelectedItems (handle, 0, null, OS.kDataBrowserItemsRemove); |
| if ((style & SWT.SINGLE) != 0) { |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0]); |
| } |
| ignoreSelect = false; |
| } |
| |
| void destroyItem (TreeColumn column) { |
| int index = 0; |
| while (index < columnCount) { |
| if (columns [index] == column) break; |
| index++; |
| } |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null) { |
| if (columnCount <= 1) { |
| item.strings = null; |
| item.images = null; |
| item.cellBackground = null; |
| item.cellForeground = null; |
| item.cellFont = null; |
| } else { |
| if (item.strings != null) { |
| String [] strings = item.strings; |
| if (index == 0) { |
| item.text = strings [1] != null ? strings [1] : ""; |
| } |
| String [] temp = new String [columnCount - 1]; |
| System.arraycopy (strings, 0, temp, 0, index); |
| System.arraycopy (strings, index + 1, temp, index, columnCount - 1 - index); |
| item.strings = temp; |
| } else { |
| if (index == 0) item.text = ""; |
| } |
| if (item.images != null) { |
| Image [] images = item.images; |
| if (index == 0) item.image = images [1]; |
| Image [] temp = new Image [columnCount - 1]; |
| System.arraycopy (images, 0, temp, 0, index); |
| System.arraycopy (images, index + 1, temp, index, columnCount - 1 - index); |
| item.images = temp; |
| } else { |
| if (index == 0) item.image = null; |
| } |
| if (item.cellBackground != null) { |
| Color [] cellBackground = item.cellBackground; |
| Color [] temp = new Color [columnCount - 1]; |
| System.arraycopy (cellBackground, 0, temp, 0, index); |
| System.arraycopy (cellBackground, index + 1, temp, index, columnCount - 1 - index); |
| item.cellBackground = temp; |
| } |
| if (item.cellForeground != null) { |
| Color [] cellForeground = item.cellForeground; |
| Color [] temp = new Color [columnCount - 1]; |
| System.arraycopy (cellForeground, 0, temp, 0, index); |
| System.arraycopy (cellForeground, index + 1, temp, index, columnCount - 1 - index); |
| item.cellForeground = temp; |
| } |
| if (item.cellFont != null) { |
| Font [] cellFont = item.cellFont; |
| Font [] temp = new Font [columnCount - 1]; |
| System.arraycopy (cellFont, 0, temp, 0, index); |
| System.arraycopy (cellFont, index + 1, temp, index, columnCount - 1 - index); |
| item.cellFont = temp; |
| } |
| } |
| } |
| } |
| if (columnCount == 1) { |
| column_id = column.id; idCount = 0; |
| DataBrowserListViewHeaderDesc desc = new DataBrowserListViewHeaderDesc (); |
| desc.version = OS.kDataBrowserListViewLatestHeaderDesc; |
| short [] width = new short [1]; |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, column_id, width); |
| desc.minimumWidth = desc.maximumWidth = width [0]; |
| int str = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, null, 0); |
| desc.titleString = str; |
| OS.SetDataBrowserListViewHeaderDesc (handle, column_id, desc); |
| OS.CFRelease (str); |
| } else { |
| int [] disclosure = new int [1]; |
| boolean [] expandableRows = new boolean [1]; |
| OS.GetDataBrowserListViewDisclosureColumn (handle, disclosure, expandableRows); |
| if (disclosure [0] == column.id) { |
| TreeColumn firstColumn = columns [1]; |
| firstColumn.style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); |
| firstColumn.style |= SWT.LEFT; |
| firstColumn.updateHeader(); |
| OS.SetDataBrowserListViewDisclosureColumn (handle, firstColumn.id, expandableRows [0]); |
| } |
| if (OS.RemoveDataBrowserTableViewColumn (handle, column.id) != OS.noErr) { |
| error (SWT.ERROR_ITEM_NOT_REMOVED); |
| } |
| } |
| System.arraycopy (columns, index + 1, columns, index, --columnCount - index); |
| columns [columnCount] = null; |
| for (int i=index; i<columnCount; i++) { |
| columns [i].sendEvent (SWT.Move); |
| } |
| } |
| |
| void destroyItem (TreeItem item) { |
| //checkItems (true); |
| TreeItem parentItem = item.parentItem; |
| if (parentItem == null || parentItem.getExpanded ()) { |
| int parentID = parentItem == null ? OS.kDataBrowserNoItem : item.parentItem.id; |
| ignoreExpand = true; |
| if (OS.RemoveDataBrowserItems (handle, parentID, 1, new int[] {item.id}, 0) != OS.noErr) { |
| error (SWT.ERROR_ITEM_NOT_REMOVED); |
| } |
| ignoreExpand = false; |
| } |
| releaseItems (item.getItems ()); |
| releaseItem (item); |
| boolean hasChild = false; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == parentItem) { |
| if (items [i].index >= item.index) { |
| --items [i].index; |
| hasChild = true; |
| } |
| } |
| } |
| /* |
| * Bug in the Macintosh. When the last child of a tree item is |
| * removed and the parent is not expanded, the parent does not |
| * redraw to remove the expander. The fix is to force a redraw. |
| */ |
| if (hasChild && parentItem != null && !parentItem.getExpanded ()) { |
| parentItem.redraw (OS.kDataBrowserNoItem); |
| } |
| setScrollWidth (); |
| } |
| |
| int drawItemProc (int browser, int id, int property, int itemState, int theRect, int gdDepth, int colorDevice) { |
| int index = id - 1; |
| if (!(0 <= index && index < items.length)) return OS.noErr; |
| int columnIndex = 0; |
| if (columnCount > 0) { |
| for (columnIndex=0; columnIndex<columnCount; columnIndex++) { |
| if (columns [columnIndex].id == property) break; |
| } |
| if (columnIndex == columnCount) return OS.noErr; |
| } |
| Rect controlRect = new Rect (); |
| OS.GetControlBounds (handle, controlRect); |
| TreeItem item = items [index]; |
| if ((style & SWT.VIRTUAL) != 0) { |
| if (!item.cached) { |
| if (!checkData (item, false)) return OS.noErr; |
| if (setScrollWidth (item)) { |
| Rect rect = new Rect(); |
| if (OS.GetDataBrowserItemPartBounds (handle, id, property, OS.kDataBrowserPropertyEnclosingPart, rect) == OS.noErr) { |
| int x = rect.left - controlRect.left; |
| int y = rect.top - controlRect.top; |
| int width = rect.right - rect.left; |
| int height = rect.bottom - rect.top; |
| redrawWidget (handle, x, y, width, height, false); |
| } |
| return OS.noErr; |
| } |
| } |
| } |
| Rect rect = new Rect (); |
| OS.memcpy (rect, theRect, Rect.sizeof); |
| int x = rect.left; |
| int y = rect.top; |
| int width = rect.right - rect.left; |
| int height = rect.bottom - rect.top; |
| boolean selected = (itemState & OS.kDataBrowserItemIsSelected) != 0; |
| x -= controlRect.left; |
| y -= controlRect.top; |
| GC gc = paintGC; |
| if (gc == null) { |
| GCData data = new GCData (); |
| int [] port = new int [1]; |
| OS.GetPort (port); |
| data.port = port [0]; |
| gc = GC.carbon_new (this, data); |
| } |
| int clip = OS.NewRgn (); |
| OS.GetClip (clip); |
| OS.OffsetRgn (clip, (short)-controlRect.left, (short)-controlRect.top); |
| gc.setClipping (Region.carbon_new (display, clip)); |
| Rect itemRect = new Rect(); |
| OS.GetDataBrowserItemPartBounds (handle, id, property, OS.kDataBrowserPropertyEnclosingPart, itemRect); |
| OS.OffsetRect (itemRect, (short) -controlRect.left, (short) -controlRect.top); |
| if (columnCount != 0) { |
| if (selected && ((style & SWT.FULL_SELECTION) != 0 || columnIndex == 0)) { |
| gc.setBackground (display.getSystemColor (SWT.COLOR_LIST_SELECTION)); |
| } else { |
| gc.setBackground (item.getBackground (columnIndex)); |
| } |
| if (columnIndex == 0) { |
| gc.fillRectangle (x - 1, y, itemRect.right - x + 2, itemRect.bottom - y); |
| } else { |
| gc.fillRectangle (itemRect.left, itemRect.top, itemRect.right - itemRect.left, itemRect.bottom - itemRect.top); |
| } |
| } |
| int rectRgn = OS.NewRgn (); |
| OS.RectRgn (rectRgn, rect); |
| OS.OffsetRgn (rectRgn, (short)-controlRect.left, (short)-controlRect.top); |
| OS.SectRgn (rectRgn, clip, clip); |
| OS.DisposeRgn (rectRgn); |
| gc.setClipping (Region.carbon_new (display, clip)); |
| OS.DisposeRgn (clip); |
| Image image = item.getImage (columnIndex); |
| String text = item.getText (columnIndex); |
| gc.setFont (item.getFont (columnIndex)); |
| Point extent = gc.stringExtent (text); |
| int itemWidth = extent.x; |
| Rectangle imageBounds = null; |
| if (image != null) { |
| imageBounds = image.getBounds (); |
| itemWidth += this.imageBounds.width + 2; |
| } |
| if (columnCount != 0) { |
| TreeColumn column = columns [columnIndex]; |
| if ((column.style & SWT.CENTER) != 0) x += (width - itemWidth) / 2; |
| if ((column.style & SWT.RIGHT) != 0) x += width - itemWidth; |
| } |
| if (image != null) { |
| gc.drawImage (image, 0, 0, imageBounds.width, imageBounds.height, x, y + (height - this.imageBounds.height) / 2, this.imageBounds.width, this.imageBounds.height); |
| x += this.imageBounds.width + 2; |
| } |
| if (selected && ((style & SWT.FULL_SELECTION) != 0 || columnIndex == 0)) { |
| gc.setForeground (display.getSystemColor (SWT.COLOR_LIST_SELECTION_TEXT)); |
| if (columnCount == 0) { |
| gc.setBackground (display.getSystemColor (SWT.COLOR_LIST_SELECTION)); |
| gc.fillRectangle (x - 1, y, extent.x + 2, height); |
| } |
| } else { |
| if (columnCount == 0) { |
| gc.setBackground (item.getBackground ()); |
| gc.fillRectangle (x - 1, y, extent.x + 2, height); |
| } |
| Color foreground = item.getForeground (columnIndex); |
| gc.setForeground (foreground); |
| } |
| gc.drawString (text, x, y + (height - extent.y) / 2, true); |
| if (gc != paintGC) gc.dispose (); |
| return OS.noErr; |
| } |
| |
| public Rectangle getClientArea () { |
| checkWidget(); |
| int border = 0; |
| int [] outMetric = new int [1]; |
| OS.GetThemeMetric (OS.kThemeMetricFocusRectOutset, outMetric); |
| border += outMetric [0]; |
| OS.GetThemeMetric (OS.kThemeMetricEditTextFrameOutset, outMetric); |
| border += outMetric [0]; |
| Rect rect = new Rect (), inset = new Rect (); |
| OS.GetControlBounds (handle, rect); |
| OS.GetDataBrowserScrollBarInset (handle, inset); |
| int width = Math.max (0, rect.right - rect.left - inset.right - border - border); |
| int height = Math.max (0, rect.bottom - rect.top - inset.bottom - border - border); |
| return new Rectangle (inset.left, inset.top, width, height); |
| } |
| |
| /** |
| * Returns the column at the given, zero-relative index in the |
| * receiver. Throws an exception if the index is out of range. |
| * If no <code>TreeColumn</code>s were created by the programmer, |
| * this method will throw <code>ERROR_INVALID_RANGE</code> despite |
| * the fact that a single column of data may be visible in the tree. |
| * This occurs when the programmer uses the tree like a list, adding |
| * items but never creating a column. |
| * |
| * @param index the index of the column to return |
| * @return the column at the given index |
| * |
| * @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> |
| * </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> |
| * |
| * @since 3.1 |
| */ |
| public TreeColumn getColumn (int index) { |
| checkWidget (); |
| if (!(0 <=index && index < columnCount)) error (SWT.ERROR_INVALID_RANGE); |
| return columns [index]; |
| } |
| |
| /** |
| * Returns the number of columns contained in the receiver. |
| * If no <code>TreeColumn</code>s were created by the programmer, |
| * this value is zero, despite the fact that visually, one column |
| * of items may be visible. This occurs when the programmer uses |
| * the tree like a list, adding items but never creating a column. |
| * |
| * @return the number of columns |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public int getColumnCount () { |
| checkWidget (); |
| return columnCount; |
| } |
| |
| /** |
| * Returns an array of <code>TreeColumn</code>s which are the |
| * columns in the receiver. If no <code>TreeColumn</code>s were |
| * created by the programmer, the array is empty, despite the fact |
| * that visually, one column of items may be visible. This occurs |
| * when the programmer uses the tree like a list, adding items but |
| * never creating a column. |
| * <p> |
| * Note: This is not the actual structure used by the receiver |
| * to maintain its list of items, so modifying the array will |
| * not affect the receiver. |
| * </p> |
| * |
| * @return the items in the receiver |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public TreeColumn [] getColumns () { |
| checkWidget (); |
| TreeColumn [] result = new TreeColumn [columnCount]; |
| System.arraycopy (columns, 0, result, 0, columnCount); |
| return result; |
| } |
| |
| /** |
| * Returns the width in pixels of a grid line. |
| * |
| * @return the width of a grid line in pixels |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public int getGridLineWidth () { |
| checkWidget (); |
| return 0; |
| } |
| |
| /** |
| * Returns the height of the receiver's header |
| * |
| * @return the height of the header or zero if the header is not 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> |
| * |
| * @since 3.1 |
| */ |
| public int getHeaderHeight () { |
| checkWidget (); |
| short [] height = new short [1]; |
| OS.GetDataBrowserListViewHeaderBtnHeight (handle, height); |
| return height [0]; |
| } |
| |
| /** |
| * Returns <code>true</code> if the receiver's header is visible, |
| * and <code>false</code> otherwise. |
| * <p> |
| * If one of the receiver's ancestors is not visible or some |
| * other condition makes the receiver not visible, this method |
| * may still indicate that it is considered visible even though |
| * it may not actually be showing. |
| * </p> |
| * |
| * @return the receiver's header's visibility state |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public boolean getHeaderVisible () { |
| checkWidget (); |
| short [] height = new short [1]; |
| OS.GetDataBrowserListViewHeaderBtnHeight (handle, height); |
| return height [0] != 0; |
| } |
| |
| /** |
| * Returns the item at the given, zero-relative index in the |
| * receiver. Throws an exception if the index is out of range. |
| * |
| * @param index the index of the item to return |
| * @return the item at the given index |
| * |
| * @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> |
| * </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> |
| * |
| * @since 3.1 |
| */ |
| public TreeItem getItem (int index) { |
| checkWidget (); |
| if (index < 0) error (SWT.ERROR_INVALID_RANGE); |
| int i = 0; |
| TreeItem item = null; |
| while (item == null && i < items.length) { |
| TreeItem next = items [i++]; |
| if (next != null && next.parentItem == null && next.index == index) { |
| item = next; |
| } |
| } |
| if (item == null) error (SWT.ERROR_INVALID_RANGE); |
| return item; |
| } |
| |
| /** |
| * Returns the item at the given point in the receiver |
| * or null if no such item exists. The point is in the |
| * coordinate system of the receiver. |
| * |
| * @param point the point used to locate the item |
| * @return the item at the given point |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the point 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> |
| */ |
| public TreeItem getItem (Point point) { |
| checkWidget (); |
| if (point == null) error (SWT.ERROR_NULL_ARGUMENT); |
| Rect rect = new Rect (); |
| OS.GetControlBounds (handle, rect); |
| org.eclipse.swt.internal.carbon.Point pt = new org.eclipse.swt.internal.carbon.Point (); |
| OS.SetPt (pt, (short) (point.x + rect.left), (short) (point.y + rect.top)); |
| //TODO - optimize |
| int columnId = (columnCount == 0) ? column_id : columns [0].id; |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null) { |
| if (OS.GetDataBrowserItemPartBounds (handle, item.id, columnId, OS.kDataBrowserPropertyEnclosingPart, rect) == OS.noErr) { |
| if ((style & SWT.FULL_SELECTION) != 0) { |
| if (rect.top <= pt.v && pt.v < rect.bottom) return item; |
| } else { |
| if (OS.PtInRect (pt, rect)) return item; |
| } |
| } |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Returns the number of items contained in the receiver |
| * that are direct item children of the receiver. The |
| * number that is returned is the number of roots in the |
| * tree. |
| * |
| * @return the number of items |
| * |
| * @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 int getItemCount () { |
| checkWidget (); |
| return getItemCount (null); |
| } |
| |
| int getItemCount (TreeItem item) { |
| checkWidget (); |
| int count = 0; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) count++; |
| } |
| return count; |
| } |
| |
| /** |
| * Returns the height of the area which would be used to |
| * display <em>one</em> of the items in the tree. |
| * |
| * @return the height of one 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 int getItemHeight () { |
| checkWidget (); |
| short [] height = new short [1]; |
| if (OS.GetDataBrowserTableViewRowHeight (handle, height) != OS.noErr) { |
| error (SWT.ERROR_CANNOT_GET_ITEM_HEIGHT); |
| } |
| return height [0]; |
| } |
| |
| /** |
| * Returns a (possibly empty) array of items contained in the |
| * receiver that are direct item children of the receiver. These |
| * are the roots of the tree. |
| * <p> |
| * Note: This is not the actual structure used by the receiver |
| * to maintain its list of items, so modifying the array will |
| * not affect the receiver. |
| * </p> |
| * |
| * @return the items |
| * |
| * @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 TreeItem [] getItems () { |
| checkWidget (); |
| return getItems (null); |
| } |
| |
| TreeItem [] getItems (TreeItem item) { |
| if (items == null) return new TreeItem [0]; |
| int count = 0; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) count++; |
| } |
| TreeItem [] result = new TreeItem [count]; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) { |
| result [items [i].index] = items [i]; |
| } |
| } |
| return result; |
| } |
| |
| /** |
| * Returns <code>true</code> if the receiver's lines are visible, |
| * and <code>false</code> otherwise. |
| * <p> |
| * If one of the receiver's ancestors is not visible or some |
| * other condition makes the receiver not visible, this method |
| * may still indicate that it is considered visible even though |
| * it may not actually be showing. |
| * </p> |
| * |
| * @return the visibility state of the lines |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public boolean getLinesVisible () { |
| checkWidget (); |
| return false; |
| } |
| |
| /** |
| * Returns the receiver's parent item, which must be a |
| * <code>TreeItem</code> or null when the receiver is a |
| * root. |
| * |
| * @return the receiver's parent 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 TreeItem getParentItem () { |
| checkWidget (); |
| return null; |
| } |
| |
| /** |
| * Returns an array of <code>TreeItem</code>s that are currently |
| * selected in the receiver. The order of the items is unspecified. |
| * An empty array indicates that no items are selected. |
| * <p> |
| * Note: This is not the actual structure used by the receiver |
| * to maintain its selection, so modifying the array will |
| * not affect the receiver. |
| * </p> |
| * @return an array representing the selection |
| * |
| * @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 TreeItem [] getSelection () { |
| checkWidget (); |
| int ptr = OS.NewHandle (0); |
| if (OS.GetDataBrowserItems (handle, OS.kDataBrowserNoItem, true, OS.kDataBrowserItemIsSelected, ptr) != OS.noErr) { |
| error (SWT.ERROR_CANNOT_GET_SELECTION); |
| } |
| int count = OS.GetHandleSize (ptr) / 4; |
| TreeItem [] result = new TreeItem [count]; |
| OS.HLock (ptr); |
| int [] start = new int [1]; |
| OS.memcpy (start, ptr, 4); |
| int [] id = new int [1]; |
| for (int i=0; i<count; i++) { |
| OS.memcpy (id, start [0] + (i * 4), 4); |
| result [i] = items [id [0] - 1]; |
| } |
| OS.HUnlock (ptr); |
| OS.DisposeHandle (ptr); |
| return result; |
| } |
| |
| /** |
| * Returns the number of selected items contained in the receiver. |
| * |
| * @return the number of selected items |
| * |
| * @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 int getSelectionCount () { |
| checkWidget (); |
| int [] count = new int [1]; |
| if (OS.GetDataBrowserItemCount (handle, OS.kDataBrowserNoItem, true, OS.kDataBrowserItemIsSelected, count) != OS.noErr) { |
| error (SWT.ERROR_CANNOT_GET_COUNT); |
| } |
| return count [0]; |
| } |
| |
| /** |
| * Returns the item which is currently at the top of the receiver. |
| * This item can change when items are expanded, collapsed, scrolled |
| * or new items are added or removed. |
| * |
| * @return the item at the top of the receiver |
| * |
| * @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> |
| * |
| * @since 2.1 |
| */ |
| public TreeItem getTopItem () { |
| checkWidget(); |
| //TODO - optimize |
| Rect rect = new Rect (); |
| OS.GetControlBounds (handle, rect); |
| int offset = 0; |
| int [] outMetric = new int [1]; |
| OS.GetThemeMetric (OS.kThemeMetricFocusRectOutset, outMetric); |
| offset += outMetric [0]; |
| OS.GetThemeMetric (OS.kThemeMetricEditTextFrameOutset, outMetric); |
| offset += outMetric [0]; |
| if (getHeaderVisible ()) offset += getHeaderHeight (); |
| int y = rect.top + offset; |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null) { |
| int columnId = (columnCount == 0) ? column_id : columns [0].id; |
| if (OS.GetDataBrowserItemPartBounds (handle, item.id, columnId, OS.kDataBrowserPropertyEnclosingPart, rect) == OS.noErr) { |
| if (rect.top <= y && y <= rect.bottom) return item; |
| } |
| } |
| } |
| return null; |
| } |
| |
| int hitTestProc (int browser, int id, int property, int theRect, int mouseRect) { |
| lastHittest = id; |
| return 1; |
| } |
| |
| void hookEvents () { |
| super.hookEvents (); |
| DataBrowserCallbacks callbacks = new DataBrowserCallbacks (); |
| callbacks.version = OS.kDataBrowserLatestCallbacks; |
| OS.InitDataBrowserCallbacks (callbacks); |
| callbacks.v1_itemCompareCallback = display.itemCompareProc; |
| callbacks.v1_itemDataCallback = display.itemDataProc; |
| callbacks.v1_itemNotificationCallback = display.itemNotificationProc; |
| OS.SetDataBrowserCallbacks (handle, callbacks); |
| DataBrowserCustomCallbacks custom = new DataBrowserCustomCallbacks (); |
| custom.version = OS.kDataBrowserLatestCustomCallbacks; |
| OS.InitDataBrowserCustomCallbacks (custom); |
| custom.v1_drawItemCallback = display.drawItemProc; |
| custom.v1_hitTestCallback = display.hitTestProc; |
| custom.v1_trackingCallback = display.trackingProc; |
| OS.SetDataBrowserCustomCallbacks (handle, custom); |
| } |
| |
| /** |
| * Searches the receiver's list starting at the first column |
| * (index 0) until a column is found that is equal to the |
| * argument, and returns the index of that column. If no column |
| * is found, returns -1. |
| * |
| * @param column the search column |
| * @return the index of the column |
| * |
| * @exception IllegalArgumentException <ul> |
| * <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> |
| * |
| * @since 3.1 |
| */ |
| public int indexOf (TreeColumn column) { |
| checkWidget (); |
| if (column == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (column.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); |
| for (int i=0; i<columnCount; i++) { |
| if (columns [i] == column) return i; |
| } |
| return -1; |
| } |
| |
| /** |
| * Searches the receiver's list starting at the first item |
| * (index 0) until an item is found that is equal to the |
| * argument, and returns the index of that item. If no item |
| * is found, returns -1. |
| * |
| * @param item the search item |
| * @return the index of the item |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the tool item is null</li> |
| * <li>ERROR_INVALID_ARGUMENT - if the tool item has been disposed</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> |
| * |
| * @since 3.1 |
| */ |
| public int indexOf (TreeItem item) { |
| checkWidget (); |
| if (item == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); |
| if (item.parentItem != null) return -1; |
| return item.index; |
| } |
| |
| int itemCompareProc (int browser, int itemOne, int itemTwo, int sortProperty) { |
| int index1 = itemOne - 1; |
| if (!(0 <= index1 && index1 < items.length)) return OS.noErr; |
| int index2 = itemTwo - 1; |
| if (!(0 <= index2 && index2 < items.length)) return OS.noErr; |
| return items [index1].index < items [index2].index ? 1 : 0; |
| } |
| |
| int itemDataProc (int browser, int id, int property, int itemData, int setValue) { |
| int index = id - 1; |
| if (!(0 <= index && index < items.length)) return OS.noErr; |
| TreeItem item = items [index]; |
| switch (property) { |
| case CHECK_COLUMN_ID: { |
| if (setValue != 0) { |
| item.checked = !item.checked; |
| if (item.checked && item.grayed) { |
| OS.SetDataBrowserItemDataButtonValue (itemData, (short) OS.kThemeButtonMixed); |
| } else { |
| int theData = item.checked ? OS.kThemeButtonOn : OS.kThemeButtonOff; |
| OS.SetDataBrowserItemDataButtonValue (itemData, (short) theData); |
| } |
| Event event = new Event (); |
| event.item = item; |
| event.detail = SWT.CHECK; |
| postEvent (SWT.Selection, event); |
| /* |
| * Bug in the Macintosh. When the height of the row is smaller than the |
| * check box, the tail of the check mark draws outside of the item part |
| * bounds. This means it will not be redrawn when the item is unckeched. |
| * The fix is to redraw the area. |
| */ |
| if (!item.checked) item.redraw(Tree.CHECK_COLUMN_ID); |
| } else { |
| int theData = OS.kThemeButtonOff; |
| if (item.checked) theData = item.grayed ? OS.kThemeButtonMixed : OS.kThemeButtonOn; |
| OS.SetDataBrowserItemDataButtonValue (itemData, (short) theData); |
| } |
| break; |
| } |
| case OS.kDataBrowserItemIsContainerProperty: { |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) { |
| OS.SetDataBrowserItemDataBooleanValue (itemData, true); |
| } |
| } |
| break; |
| } |
| } |
| return OS.noErr; |
| } |
| |
| int itemNotificationProc (int browser, int id, int message) { |
| if (message == OS.kDataBrowserUserStateChanged) { |
| short [] width = new short [1]; |
| TreeColumn [] newColumns = getColumns (); |
| for (int i = 0; i < columnCount; i++) { |
| TreeColumn column = newColumns [i]; |
| if (!column.isDisposed ()) { |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, column.id, width); |
| if (width [0] != column.lastWidth) { |
| column.resized (width [0]); |
| return OS.noErr; |
| } |
| } |
| } |
| int [] property = new int [1]; |
| OS.GetDataBrowserSortProperty (handle, property); |
| if (property [0] != 0) { |
| for (int i = 0; i < columnCount; i++) { |
| TreeColumn column = columns [i]; |
| if (property [0] == column.id) { |
| column.postEvent (clickCount == 2 ? SWT.DefaultSelection : SWT.Selection); |
| break; |
| } |
| } |
| OS.SetDataBrowserSortProperty (handle, 0); |
| } |
| return OS.noErr; |
| } |
| int index = id - 1; |
| if (!(0 <= index && index < items.length)) return OS.noErr; |
| TreeItem item = items [index]; |
| switch (message) { |
| case OS.kDataBrowserItemSelected: |
| case OS.kDataBrowserItemDeselected: { |
| wasSelected = true; |
| if (ignoreSelect) break; |
| int [] first = new int [1], last = new int [1]; |
| OS.GetDataBrowserSelectionAnchor (handle, first, last); |
| boolean selected = false; |
| if ((style & SWT.MULTI) != 0) { |
| int modifiers = OS.GetCurrentEventKeyModifiers (); |
| if ((modifiers & OS.shiftKey) != 0) { |
| if (message == OS.kDataBrowserItemSelected) { |
| selected = first [0] == id || last [0] == id; |
| } else { |
| selected = id == anchorFirst || id == anchorLast; |
| } |
| } else { |
| if ((modifiers & OS.cmdKey) != 0) { |
| selected = true; |
| } else { |
| selected = first [0] == last [0]; |
| } |
| } |
| } else { |
| selected = message == OS.kDataBrowserItemSelected; |
| } |
| if (selected) { |
| anchorFirst = first [0]; |
| anchorLast = last [0]; |
| Event event = new Event (); |
| event.item = item; |
| postEvent (SWT.Selection, event); |
| } |
| break; |
| } |
| case OS.kDataBrowserItemDoubleClicked: { |
| wasSelected = true; |
| Event event = new Event (); |
| event.item = item; |
| postEvent (SWT.DefaultSelection, event); |
| break; |
| } |
| case OS.kDataBrowserContainerClosing: { |
| /* |
| * Bug in the Macintosh. For some reason, if the selected sub items of an item |
| * get a kDataBrowserItemDeselected notificaton when the item is collapsed, a |
| * call to GetDataBrowserSelectionAnchor () will cause a segment fault. The |
| * fix is to deselect these items ignoring kDataBrowserItemDeselected and then |
| * issue a selection event. |
| */ |
| int ptr = OS.NewHandle (0); |
| if (OS.GetDataBrowserItems (handle, item.id, true, OS.kDataBrowserItemIsSelected, ptr) == OS.noErr) { |
| int count = OS.GetHandleSize (ptr) / 4; |
| if (count > 0) { |
| int [] ids = new int [count]; |
| OS.HLock (ptr); |
| int [] start = new int [1]; |
| OS.memcpy (start, ptr, 4); |
| OS.memcpy (ids, start [0], count * 4); |
| OS.HUnlock (ptr); |
| ignoreSelect = true; |
| /* |
| * Bug in the Macintosh. When the DataBroswer selection flags includes |
| * both kDataBrowserNeverEmptySelectionSet and kDataBrowserSelectOnlyOne, |
| * two items are selected when SetDataBrowserSelectedItems() is called |
| * with kDataBrowserItemsAssign to assign a new seletion despite the fact |
| * that kDataBrowserSelectOnlyOne was specified. The fix is to save and |
| * restore kDataBrowserNeverEmptySelectionSet around each call to |
| * SetDataBrowserSelectedItems(). |
| */ |
| int [] selectionFlags = null; |
| if ((style & SWT.SINGLE) != 0) { |
| selectionFlags = new int [1]; |
| OS.GetDataBrowserSelectionFlags (handle, selectionFlags); |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0] & ~OS.kDataBrowserNeverEmptySelectionSet); |
| } |
| OS.SetDataBrowserSelectedItems (handle, ids.length, ids, OS.kDataBrowserItemsRemove); |
| if ((style & SWT.SINGLE) != 0) { |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0]); |
| } |
| ignoreSelect = false; |
| Event event = new Event (); |
| event.item = item; |
| sendEvent (SWT.Selection, event); |
| } |
| } |
| OS.DisposeHandle (ptr); |
| break; |
| } |
| case OS.kDataBrowserContainerClosed: { |
| wasExpanded = true; |
| if (!ignoreExpand) { |
| Event event = new Event (); |
| event.item = item; |
| sendEvent (SWT.Collapse, event); |
| } |
| setScrollWidth (); |
| break; |
| } |
| case OS.kDataBrowserContainerOpened: { |
| wasExpanded = true; |
| if (!ignoreExpand) { |
| Event event = new Event (); |
| event.item = item; |
| try { |
| item.state |= EXPANDING; |
| sendEvent (SWT.Expand, event); |
| } finally { |
| item.state &= ~EXPANDING; |
| } |
| } |
| int count = 0; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) count++; |
| } |
| TreeItem [] newItems = new TreeItem [count]; |
| int [] ids = new int [count]; |
| for (int i=0; i<items.length; i++) { |
| if (items [i] != null && items [i].parentItem == item) { |
| ids [items [i].index] = items [i].id; |
| newItems [items [i].index] = items [i]; |
| } |
| } |
| OS.AddDataBrowserItems (handle, id, ids.length, ids, OS.kDataBrowserItemNoProperty); |
| setScrollWidth (newItems, false); |
| break; |
| } |
| } |
| return OS.noErr; |
| } |
| |
| int kEventTextInputUnicodeForKeyEvent (int nextHandler, int theEvent, int userData) { |
| int result = super.kEventTextInputUnicodeForKeyEvent (nextHandler, theEvent, userData); |
| if (result == OS.noErr) return result; |
| int [] keyboardEvent = new int [1]; |
| OS.GetEventParameter (theEvent, OS.kEventParamTextInputSendKeyboardEvent, OS.typeEventRef, null, keyboardEvent.length * 4, null, keyboardEvent); |
| int [] keyCode = new int [1]; |
| OS.GetEventParameter (keyboardEvent [0], OS.kEventParamKeyCode, OS.typeUInt32, null, keyCode.length * 4, null, keyCode); |
| switch (keyCode [0]) { |
| case 36: { /* Return */ |
| postEvent (SWT.DefaultSelection); |
| break; |
| } |
| |
| } |
| return result; |
| } |
| |
| int kEventMouseDown (int nextHandler, int theEvent, int userData) { |
| int [] outData = new int [1]; |
| OS.GetEventParameter (theEvent, OS.kEventParamClickCount, OS.typeUInt32, null, 4, null, outData); |
| clickCount = outData [0]; |
| int result = super.kEventMouseDown (nextHandler, theEvent, userData); |
| if (result == OS.noErr) return result; |
| Shell shell = getShell (); |
| shell.bringToTop (true); |
| /* |
| * Feature in the Macintosh. For some reason, when the user |
| * clicks on the data browser, focus is assigned, then lost |
| * and then reassigned causing kEvenControlSetFocusPart events. |
| * The fix is to ignore kEvenControlSetFocusPart when the user |
| * clicks and send the focus events from kEventMouseDown. |
| */ |
| Control oldFocus = display.getFocusControl (); |
| display.ignoreFocus = true; |
| wasSelected = wasExpanded = false; |
| result = OS.CallNextEventHandler (nextHandler, theEvent); |
| display.ignoreFocus = false; |
| if (oldFocus != this) { |
| if (oldFocus != null && !oldFocus.isDisposed ()) oldFocus.sendFocusEvent (SWT.FocusOut, false); |
| if (!isDisposed () && isEnabled ()) sendFocusEvent (SWT.FocusIn, false); |
| } |
| if (!wasSelected && !wasExpanded) { |
| if (OS.IsDataBrowserItemSelected (handle, lastHittest)) { |
| int index = lastHittest - 1; |
| if (0 <= index && index < items.length) { |
| Event event = new Event (); |
| event.item = items [index]; |
| postEvent (SWT.Selection, event); |
| } |
| } |
| } |
| /* |
| * Feature in the Macintosh. Some controls call TrackControl() or |
| * HandleControlClick() to track the mouse. Unfortunately, mouse move |
| * events and the mouse up events are consumed. The fix is to call the |
| * default handler and send a fake mouse up when tracking is finished. |
| * |
| * NOTE: No mouse move events are sent while tracking. There is no |
| * fix for this at this time. |
| */ |
| if (wasExpanded) { |
| org.eclipse.swt.internal.carbon.Point outPt = new org.eclipse.swt.internal.carbon.Point (); |
| OS.GetGlobalMouse (outPt); |
| Rect rect = new Rect (); |
| int window = OS.GetControlOwner (handle); |
| OS.GetWindowBounds (window, (short) OS.kWindowContentRgn, rect); |
| int x = outPt.h - rect.left; |
| int y = outPt.v - rect.top; |
| OS.GetControlBounds (handle, rect); |
| x -= rect.left; |
| y -= rect.top; |
| short [] button = new short [1]; |
| OS.GetEventParameter (theEvent, OS.kEventParamMouseButton, OS.typeMouseButton, null, 2, null, button); |
| int chord = OS.GetCurrentEventButtonState (); |
| int modifiers = OS.GetCurrentEventKeyModifiers (); |
| sendMouseEvent (SWT.MouseUp, button [0], chord, (short)x, (short)y, modifiers, false); |
| } |
| wasSelected = wasExpanded = false; |
| return result; |
| } |
| |
| boolean releaseItem (TreeItem item) { |
| if (item.isDisposed ()) return false; |
| items [item.id - 1] = null; |
| return true; |
| } |
| |
| void releaseItems (TreeItem [] nodes) { |
| for (int i=0; i<nodes.length; i++) { |
| TreeItem item = nodes [i]; |
| TreeItem [] sons = item.getItems (); |
| if (sons.length != 0) { |
| releaseItems (sons); |
| } |
| if (releaseItem (item)) { |
| item.releaseResources (); |
| } |
| } |
| } |
| |
| void releaseWidget () { |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null && !item.isDisposed ()) { |
| item.releaseResources (); |
| } |
| } |
| items = null; |
| for (int i=0; i<columnCount; i++) { |
| TreeColumn column = columns [i]; |
| if (!column.isDisposed ()) column.releaseResources (); |
| } |
| columns = null; |
| super.releaseWidget (); |
| } |
| |
| /** |
| * Removes all of the items from the receiver. |
| * <p> |
| * @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 removeAll () { |
| checkWidget (); |
| ignoreExpand = true; |
| if (OS.RemoveDataBrowserItems (handle, OS.kDataBrowserNoItem, 0, null, 0) != OS.noErr) { |
| error (SWT.ERROR_ITEM_NOT_REMOVED); |
| } |
| ignoreExpand = false; |
| OS.SetDataBrowserScrollPosition (handle, 0, 0); |
| for (int i=0; i<items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null && !item.isDisposed ()) item.releaseResources (); |
| } |
| items = new TreeItem [4]; |
| anchorFirst = anchorLast = 0; |
| setScrollWidth (); |
| } |
| |
| /** |
| * Removes the listener from the collection of listeners who will |
| * be notified when the receiver's selection changes. |
| * |
| * @param listener the listener which should no longer be notified |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the listener 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 SelectionListener |
| * @see #addSelectionListener |
| */ |
| public void removeSelectionListener (SelectionListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| eventTable.unhook (SWT.Selection, listener); |
| eventTable.unhook (SWT.DefaultSelection, listener); |
| } |
| |
| /** |
| * Removes the listener from the collection of listeners who will |
| * be notified when items in the receiver are expanded or collapsed.. |
| * |
| * @param listener the listener which should no longer be notified |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the listener 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 TreeListener |
| * @see #addTreeListener |
| */ |
| public void removeTreeListener(TreeListener listener) { |
| checkWidget (); |
| if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (eventTable == null) return; |
| eventTable.unhook (SWT.Expand, listener); |
| eventTable.unhook (SWT.Collapse, listener); |
| } |
| |
| void resetVisibleRegion (int control) { |
| super.resetVisibleRegion (control); |
| if (showItem != null && !showItem.isDisposed ()) { |
| showItem (showItem , true); |
| } |
| } |
| |
| /** |
| * Display a mark indicating the point at which an item will be inserted. |
| * The drop insert item has a visual hint to show where a dragged item |
| * will be inserted when dropped on the tree. |
| * |
| * @param item the insert item. Null will clear the insertion mark. |
| * @param before true places the insert mark above 'item'. false places |
| * the insert mark below 'item'. |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</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> |
| */ |
| public void setInsertMark (TreeItem item, boolean before) { |
| checkWidget (); |
| if (item != null) { |
| if (item.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); |
| } |
| } |
| |
| /** |
| * Selects all of the items in the receiver. |
| * <p> |
| * If the receiver is single-select, do nothing. |
| * |
| * @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 selectAll () { |
| checkWidget (); |
| if ((style & SWT.SINGLE) != 0) return; |
| ignoreSelect = true; |
| OS.SetDataBrowserSelectedItems (handle, 0, null, OS.kDataBrowserItemsAssign); |
| ignoreSelect = false; |
| } |
| |
| void setBackground (float [] color) { |
| /* |
| * Bug in the Macintosh. The default background of a window changes when |
| * the background of a data browser is set using SetControlFontStyle(). This |
| * also affects the background of any TNXObject created on that window. The |
| * fix is to avoid calling SetControlFontStyle() which has no effect |
| * in a data browser anyways. |
| */ |
| } |
| |
| int setBounds (int x, int y, int width, int height, boolean move, boolean resize, boolean events) { |
| /* |
| * Ensure that the selection is visible when the tree is resized |
| * from a zero size to a size that can show the selection. |
| */ |
| int result = super.setBounds (x, y, width, height, move, resize, events); |
| if (showItem != null && !showItem.isDisposed ()) { |
| showItem (showItem , true); |
| } |
| return result; |
| } |
| |
| void setFontStyle (Font font) { |
| super.setFontStyle (font); |
| if (items == null) return; |
| for (int i = 0; i < items.length; i++) { |
| TreeItem item = items [i]; |
| if (item != null) item.width = -1; |
| } |
| setScrollWidth (); |
| } |
| |
| /** |
| * Marks the receiver's header as visible if the argument is <code>true</code>, |
| * and marks it invisible otherwise. |
| * <p> |
| * If one of the receiver's ancestors is not visible or some |
| * other condition makes the receiver not visible, marking |
| * it visible may not actually cause it to be displayed. |
| * </p> |
| * |
| * @param show the new visibility state |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public void setHeaderVisible (boolean show) { |
| checkWidget (); |
| short [] height = new short [1]; |
| OS.GetDataBrowserListViewHeaderBtnHeight (handle, height); |
| if ((height [0] != 0) != show) { |
| OS.SetDataBrowserListViewHeaderBtnHeight (handle, (short) (show ? headerHeight : 0)); |
| invalidateVisibleRegion (handle); |
| } |
| } |
| |
| void setItemHeight (Image image) { |
| Rectangle bounds = image != null ? image.getBounds () : imageBounds; |
| if (bounds == null) return; |
| imageBounds = bounds; |
| short [] height = new short [1]; |
| if (OS.GetDataBrowserTableViewRowHeight (handle, height) == OS.noErr) { |
| if (height [0] < bounds.height) { |
| OS.SetDataBrowserTableViewRowHeight (handle, (short) bounds.height); |
| } |
| } |
| } |
| |
| /** |
| * Marks the receiver's lines as visible if the argument is <code>true</code>, |
| * and marks it invisible otherwise. |
| * <p> |
| * If one of the receiver's ancestors is not visible or some |
| * other condition makes the receiver not visible, marking |
| * it visible may not actually cause it to be displayed. |
| * </p> |
| * |
| * @param show the new visibility state |
| * |
| * @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> |
| * |
| * @since 3.1 |
| */ |
| public void setLinesVisible (boolean show) { |
| checkWidget (); |
| } |
| |
| public void setRedraw (boolean redraw) { |
| checkWidget(); |
| super.setRedraw (redraw); |
| if (redraw && drawCount == 0) { |
| setScrollWidth (); |
| } |
| } |
| |
| void setScrollWidth () { |
| setScrollWidth (getItems (null), true); |
| } |
| |
| boolean setScrollWidth (TreeItem item) { |
| if (ignoreRedraw || drawCount != 0) return false; |
| if (columnCount != 0) return false; |
| TreeItem parentItem = item.parentItem; |
| if (parentItem != null && !parentItem._getExpanded ()) return false; |
| GC gc = new GC (this); |
| int newWidth = item.calculateWidth (0, gc); |
| gc.dispose (); |
| newWidth += EXTRA_WIDTH; |
| short [] width = new short [1]; |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, column_id, width); |
| if (width [0] < newWidth) { |
| OS.SetDataBrowserTableViewNamedColumnWidth (handle, column_id, (short) newWidth); |
| return true; |
| } |
| return false; |
| } |
| |
| boolean setScrollWidth (TreeItem [] items, boolean set) { |
| if (ignoreRedraw || drawCount != 0) return false; |
| if (columnCount != 0) return false; |
| GC gc = new GC (this); |
| int newWidth = calculateWidth (items, gc); |
| gc.dispose (); |
| newWidth += EXTRA_WIDTH; |
| if (!set) { |
| short [] width = new short [1]; |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, column_id, width); |
| if (width [0] >= newWidth) return false; |
| } |
| OS.SetDataBrowserTableViewNamedColumnWidth (handle, column_id, (short) newWidth); |
| return true; |
| } |
| |
| /** |
| * 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> |
| * <li>ERROR_INVALID_ARGUMENT - if one of the items has been disposed</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 Tree#deselectAll() |
| */ |
| public void setSelection (TreeItem [] 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[] ids = new int [length]; |
| for (int i=0; i<length; i++) { |
| if (items [i] == null) error (SWT.ERROR_INVALID_ARGUMENT); |
| if (items [i].isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); |
| ids [i] = items [i].id; |
| showItem (items [i], false); |
| } |
| ignoreSelect = true; |
| /* |
| * Bug in the Macintosh. When the DataBroswer selection flags includes |
| * both kDataBrowserNeverEmptySelectionSet and kDataBrowserSelectOnlyOne, |
| * two items are selected when SetDataBrowserSelectedItems() is called |
| * with kDataBrowserItemsAssign to assign a new seletion despite the fact |
| * that kDataBrowserSelectOnlyOne was specified. The fix is to save and |
| * restore kDataBrowserNeverEmptySelectionSet around each call to |
| * SetDataBrowserSelectedItems(). |
| */ |
| int [] selectionFlags = null; |
| if ((style & SWT.SINGLE) != 0) { |
| selectionFlags = new int [1]; |
| OS.GetDataBrowserSelectionFlags (handle, selectionFlags); |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0] & ~OS.kDataBrowserNeverEmptySelectionSet); |
| } |
| OS.SetDataBrowserSelectedItems (handle, ids.length, ids, OS.kDataBrowserItemsAssign); |
| if ((style & SWT.SINGLE) != 0) { |
| OS.SetDataBrowserSelectionFlags (handle, selectionFlags [0]); |
| } |
| ignoreSelect = false; |
| if (length > 0) showItem (items [0], true); |
| } |
| |
| /** |
| * Sets the item which is currently at the top of the receiver. |
| * This item can change when items are expanded, collapsed, scrolled |
| * or new items are added or removed. |
| * |
| * @param item the item to be shown |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the item is null</li> |
| * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</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 Tree#getTopItem() |
| * |
| * @since 2.1 |
| */ |
| public void setTopItem (TreeItem item) { |
| checkWidget(); |
| if (item == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); |
| showItem (item, false); |
| int columnId = (columnCount == 0) ? column_id : columns [0].id; |
| OS.RevealDataBrowserItem (handle, item.id, columnId, (byte) OS.kDataBrowserRevealWithoutSelecting); |
| // Rect rect = new Rect (); |
| // OS.GetControlBounds (handle, rect); |
| // int x = rect.left, y = rect.top; |
| // if (OS.GetDataBrowserItemPartBounds (handle, item.id, column_id, OS.kDataBrowserPropertyEnclosingPart, rect) == OS.noErr) { |
| // OS.SetDataBrowserScrollPosition (handle, rect.top - y - 3, 0); |
| // } |
| } |
| |
| /** |
| * Shows the column. If the column is already showing in the receiver, |
| * this method simply returns. Otherwise, the columns are scrolled until |
| * the column is visible. |
| * |
| * @param column the column to be shown |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the item is null</li> |
| * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</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> |
| * |
| * @since 3.1 |
| */ |
| public void showColumn (TreeColumn column) { |
| checkWidget (); |
| if (column == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (column.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); |
| if (column.parent != this) return; |
| int index = indexOf (column); |
| if (columnCount <= 1 || !(0 <= index && index < columnCount)) return; |
| // Get width and horizontal position of column |
| short [] w = new short [1]; |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, column.id, w); |
| int width = w [0]; |
| int x = 0; |
| for (int i = 0; i < index; i++) { |
| w = new short [1]; |
| OS.GetDataBrowserTableViewNamedColumnWidth (handle, columns[i].id, w); |
| x += w [0]; |
| } |
| // Get current scroll position |
| int [] top = new int [1], left = new int [1]; |
| OS.GetDataBrowserScrollPosition (handle, top, left); |
| // Scroll column into view |
| if (x < left[0]) { |
| OS.SetDataBrowserScrollPosition(handle, top [0], x); |
| } else { |
| Rectangle rect = getClientArea (); |
| int maxWidth = rect.width; |
| width = Math.min(width, maxWidth); |
| if (x + width > left [0] + maxWidth) { |
| left [0] = x + width - maxWidth; |
| OS.SetDataBrowserScrollPosition(handle, top [0], left [0]); |
| } |
| } |
| } |
| |
| /** |
| * Shows the item. If the item is already showing in the receiver, |
| * this method simply returns. Otherwise, the items are scrolled |
| * and expanded until the item is visible. |
| * |
| * @param item the item to be shown |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the item is null</li> |
| * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</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 Tree#showSelection() |
| */ |
| public void showItem (TreeItem item) { |
| checkWidget (); |
| if (item == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); |
| showItem (item, true); |
| } |
| |
| void showItem (TreeItem item, boolean scroll) { |
| int count = 0; |
| TreeItem parentItem = item.parentItem; |
| while (parentItem != null && !parentItem._getExpanded ()) { |
| count++; |
| parentItem = parentItem.parentItem; |
| } |
| int index = 0; |
| parentItem = item.parentItem; |
| TreeItem [] path = new TreeItem [count]; |
| while (parentItem != null && !parentItem._getExpanded ()) { |
| path [index++] = parentItem; |
| parentItem = parentItem.parentItem; |
| } |
| for (int i=path.length-1; i>=0; --i) { |
| path [i].setExpanded (true); |
| } |
| if (scroll) { |
| /* |
| * Bug in the Macintosh. When there is not room to show a |
| * single item in the data browser, RevealDataBrowserItem() |
| * scrolls the item such that it is above the top of the data |
| * browser. The fix is to remember the index and scroll when |
| * the data browser is resized. |
| * |
| * Bug in the Macintosh. When items are added to the data |
| * browser after is has been hidden, RevealDataBrowserItem() |
| * when called before the controls behind the data browser |
| * are repainted causes a redraw. This redraw happens right |
| * away causing pixel corruption. The fix is to remember the |
| * index and scroll when the data browser is shown. |
| */ |
| Rectangle rect = getClientArea (); |
| if (rect.height < getItemHeight () || !OS.IsControlVisible (handle)) { |
| showItem = item; |
| return; |
| } |
| showItem = null; |
| Rectangle itemRect = item.getBounds (); |
| if (!itemRect.isEmpty()) { |
| if (rect.contains (itemRect.x, itemRect.y) |
| && rect.contains (itemRect.x, itemRect.y + itemRect.height)) return; |
| } |
| int [] top = new int [1], left = new int [1]; |
| OS.GetDataBrowserScrollPosition (handle, top, left); |
| int columnId = (columnCount == 0) ? column_id : columns [0].id; |
| OS.RevealDataBrowserItem (handle, item.id, columnId, (byte) OS.kDataBrowserRevealWithoutSelecting); |
| |
| /* |
| * Bug in the Macintosh. For some reason, when the DataBrowser is scrolled |
| * by RevealDataBrowserItem(), the scrollbars are not redrawn. The fix is to |
| * force a redraw. |
| */ |
| int [] newTop = new int [1], newLeft = new int [1]; |
| OS.GetDataBrowserScrollPosition (handle, newTop, newLeft); |
| if (horizontalBar != null && newLeft [0] != left [0]) horizontalBar.redraw (); |
| if (verticalBar != null && newTop [0] != top [0]) verticalBar.redraw (); |
| } |
| } |
| |
| /** |
| * 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> |
| * |
| * @see Tree#showItem(TreeItem) |
| */ |
| public void showSelection () { |
| checkWidget (); |
| //checkItems (false); |
| //TODO - optimize |
| TreeItem [] selection = getSelection (); |
| if (selection.length > 0) showItem (selection [0], true); |
| } |
| |
| int trackingProc (int browser, int id, int property, int theRect, int startPt, int modifiers) { |
| return 1; |
| } |
| |
| } |