comparison org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/widgets/Tree.d @ 25:f713da8bc051

Added SWT Linux GTK
author Frank Benoit <benoit@tionex.de>
date Fri, 20 Mar 2009 23:03:58 +0100
parents
children b397a43d66d1
comparison
equal deleted inserted replaced
24:b7a1d02a0e1f 25:f713da8bc051
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.swt.widgets.Tree;
14
15
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.SWTException;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.events.TreeListener;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.internal.gtk.OS;
29 import org.eclipse.swt.widgets.TreeItem;
30 import org.eclipse.swt.widgets.TreeColumn;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.ImageList;
33 import org.eclipse.swt.widgets.Listener;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Decorations;
36 import org.eclipse.swt.widgets.Menu;
37 import org.eclipse.swt.widgets.ScrollBar;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Event;
41 import org.eclipse.swt.widgets.TypedListener;
42 import java.lang.all;
43
44 /**
45 * Instances of this class provide a selectable user interface object
46 * that displays a hierarchy of items and issues notification when an
47 * item in the hierarchy is selected.
48 * <p>
49 * The item children that may be added to instances of this class
50 * must be of type <code>TreeItem</code>.
51 * </p><p>
52 * Style <code>VIRTUAL</code> is used to create a <code>Tree</code> whose
53 * <code>TreeItem</code>s are to be populated by the client on an on-demand basis
54 * instead of up-front. This can provide significant performance improvements for
55 * trees that are very large or for which <code>TreeItem</code> population is
56 * expensive (for example, retrieving values from an external source).
57 * </p><p>
58 * Here is an example of using a <code>Tree</code> with style <code>VIRTUAL</code>:
59 * <code><pre>
60 * final Tree tree = new Tree(parent, SWT.VIRTUAL | SWT.BORDER);
61 * tree.setItemCount(20);
62 * tree.addListener(SWT.SetData, new Listener() {
63 * public void handleEvent(Event event) {
64 * TreeItem item = (TreeItem)event.item;
65 * TreeItem parentItem = item.getParentItem();
66 * String text = null;
67 * if (parentItem is null) {
68 * text = "node " + tree.indexOf(item);
69 * } else {
70 * text = parentItem.getText() + " - " + parentItem.indexOf(item);
71 * }
72 * item.setText(text);
73 * System.out.println(text);
74 * item.setItemCount(10);
75 * }
76 * });
77 * </pre></code>
78 * </p><p>
79 * Note that although this class is a subclass of <code>Composite</code>,
80 * it does not normally make sense to add <code>Control</code> children to
81 * it, or set a layout on it, unless implementing something like a cell
82 * editor.
83 * </p><p>
84 * <dl>
85 * <dt><b>Styles:</b></dt>
86 * <dd>SINGLE, MULTI, CHECK, FULL_SELECTION, VIRTUAL, NO_SCROLL</dd>
87 * <dt><b>Events:</b></dt>
88 * <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem</dd>
89 * </dl>
90 * </p><p>
91 * Note: Only one of the styles SINGLE and MULTI may be specified.
92 * </p><p>
93 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
94 * </p>
95 *
96 * @see <a href="http://www.eclipse.org/swt/snippets/#tree">Tree, TreeItem, TreeColumn snippets</a>
97 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
98 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
99 */
100 public class Tree : Composite {
101
102 alias Composite.computeSize computeSize;
103 alias Composite.createHandle createHandle;
104 alias Composite.dragDetect dragDetect;
105 alias Composite.mnemonicHit mnemonicHit;
106 alias Composite.mnemonicMatch mnemonicMatch;
107 alias Composite.setBackgroundColor setBackgroundColor;
108 alias Composite.setBounds setBounds;
109
110 CallbackData treeSelectionCallbackData;
111 GtkTreeStore* modelHandle;
112 GtkCellRenderer* checkRenderer;
113 int columnCount, sortDirection;
114 GtkWidget* ignoreCell;
115 TreeItem[] items;
116 TreeColumn [] columns;
117 TreeColumn sortColumn;
118 TreeItem currentItem;
119 ImageList imageList, headerImageList;
120 bool firstCustomDraw;
121 bool modelChanged;
122 bool expandAll;
123 int drawState, drawFlags;
124 GdkColor* drawForeground;
125 bool ownerDraw, ignoreSize;
126
127 static const int ID_COLUMN = 0;
128 static const int CHECKED_COLUMN = 1;
129 static const int GRAYED_COLUMN = 2;
130 static const int FOREGROUND_COLUMN = 3;
131 static const int BACKGROUND_COLUMN = 4;
132 static const int FONT_COLUMN = 5;
133 static const int FIRST_COLUMN = FONT_COLUMN + 1;
134 static const int CELL_PIXBUF = 0;
135 static const int CELL_TEXT = 1;
136 static const int CELL_FOREGROUND = 2;
137 static const int CELL_BACKGROUND = 3;
138 static const int CELL_FONT = 4;
139 static const int CELL_TYPES = CELL_FONT + 1;
140
141 /**
142 * Constructs a new instance of this class given its parent
143 * and a style value describing its behavior and appearance.
144 * <p>
145 * The style value is either one of the style constants defined in
146 * class <code>SWT</code> which is applicable to instances of this
147 * class, or must be built by <em>bitwise OR</em>'ing together
148 * (that is, using the <code>int</code> "|" operator) two or more
149 * of those <code>SWT</code> style constants. The class description
150 * lists the style constants that are applicable to the class.
151 * Style bits are also inherited from superclasses.
152 * </p>
153 *
154 * @param parent a composite control which will be the parent of the new instance (cannot be null)
155 * @param style the style of control to construct
156 *
157 * @exception IllegalArgumentException <ul>
158 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
159 * </ul>
160 * @exception SWTException <ul>
161 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
162 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
163 * </ul>
164 *
165 * @see SWT#SINGLE
166 * @see SWT#MULTI
167 * @see SWT#CHECK
168 * @see SWT#FULL_SELECTION
169 * @see SWT#VIRTUAL
170 * @see SWT#NO_SCROLL
171 * @see Widget#checkSubclass
172 * @see Widget#getStyle
173 */
174 public this (Composite parent, int style) {
175 super (parent, checkStyle (style));
176 }
177
178 override void _addListener (int eventType, Listener listener) {
179 super._addListener (eventType, listener);
180 if (!ownerDraw) {
181 switch (eventType) {
182 case SWT.MeasureItem:
183 case SWT.EraseItem:
184 case SWT.PaintItem:
185 ownerDraw = true;
186 recreateRenderers ();
187 break;
188 default:
189 }
190 }
191 }
192
193 TreeItem _getItem (GtkTreeIter* iter) {
194 int id = getId (iter, true);
195 if (items [id] !is null) return items [id];
196 auto path = OS.gtk_tree_model_get_path (modelHandle, iter);
197 int depth = OS.gtk_tree_path_get_depth (path);
198 int [] indices = new int [depth];
199 indices[] = OS.gtk_tree_path_get_indices (path)[ 0 .. depth ];
200 GtkTreeIter* parentIter;
201 GtkTreeIter parentIterInst;
202 if (depth > 1) {
203 OS.gtk_tree_path_up (path);
204 parentIter = &parentIterInst;
205 OS.gtk_tree_model_get_iter (modelHandle, parentIter, path);
206 }
207 items [id] = new TreeItem (this, parentIter, SWT.NONE, indices [indices.length -1], false);
208 OS.gtk_tree_path_free (path);
209 return items [id];
210 }
211
212 TreeItem _getItem (GtkTreeIter* parentIter, int index) {
213 GtkTreeIter iter;
214 OS.gtk_tree_model_iter_nth_child(modelHandle, &iter, parentIter, index);
215 int id = getId (&iter, true);
216 if (items [id] !is null) return items [id];
217 return items [id] = new TreeItem (this, parentIter, SWT.NONE, index, false);
218 }
219
220 int getId (GtkTreeIter* iter, bool queryModel) {
221 if (queryModel) {
222 int value;
223 OS.gtk_tree_model_get1 (modelHandle, iter, ID_COLUMN, cast(void**)&value);
224 if (value !is -1) return value ;
225 }
226 // find next available id
227 int id = 0;
228 while (id < items.length && items [id] !is null) id++;
229 if (id is items.length) {
230 TreeItem [] newItems = new TreeItem [items.length + 4];
231 System.arraycopy (items, 0, newItems, 0, items.length);
232 items = newItems;
233 }
234 OS.gtk_tree_store_set1 (modelHandle, iter, ID_COLUMN, cast(void*)id );
235 return id;
236 }
237
238 static int checkStyle (int style) {
239 /*
240 * Feature in Windows. Even when WS_HSCROLL or
241 * WS_VSCROLL is not specified, Windows creates
242 * trees and tables with scroll bars. The fix
243 * is to set H_SCROLL and V_SCROLL.
244 *
245 * NOTE: This code appears on all platforms so that
246 * applications have consistent scroll bar behavior.
247 */
248 if ((style & SWT.NO_SCROLL) is 0) {
249 style |= SWT.H_SCROLL | SWT.V_SCROLL;
250 }
251 /* GTK is always FULL_SELECTION */
252 style |= SWT.FULL_SELECTION;
253 return checkBits (style, SWT.SINGLE, SWT.MULTI, 0, 0, 0, 0);
254 }
255
256 override void cellDataProc (
257 GtkTreeViewColumn *tree_column,
258 GtkCellRenderer *cell,
259 GtkTreeModel *tree_model,
260 GtkTreeIter *iter,
261 void* data)
262 {
263 if (cell is cast(GtkCellRenderer*)ignoreCell) return;
264 TreeItem item = _getItem (iter);
265 if (item !is null) OS.g_object_set_qdata (cast(GObject*)cell, Display.SWT_OBJECT_INDEX2, item.handle);
266 bool isPixbuf = OS.GTK_IS_CELL_RENDERER_PIXBUF (cell);
267 if (!(isPixbuf || OS.GTK_IS_CELL_RENDERER_TEXT (cell))) return;
268 int modelIndex = -1;
269 bool customDraw = false;
270 if (columnCount is 0) {
271 modelIndex = Tree.FIRST_COLUMN;
272 customDraw = firstCustomDraw;
273 } else {
274 TreeColumn column = cast(TreeColumn) display.getWidget (cast(GtkWidget*)tree_column);
275 if (column !is null) {
276 modelIndex = column.modelIndex;
277 customDraw = column.customDraw;
278 }
279 }
280 if (modelIndex is -1) return 0;
281 bool setData = false;
282 if ((style & SWT.VIRTUAL) !is 0) {
283 /*
284 * Feature in GTK. On GTK before 2.4, fixed_height_mode is not
285 * supported, and the tree asks for the data of all items. The
286 * fix is to only provide the data if the row is visible.
287 */
288 if (OS.GTK_VERSION < OS.buildVERSION (2, 3, 2)) {
289 auto path = OS.gtk_tree_model_get_path (tree_model, iter);
290 OS.gtk_widget_realize (handle);
291 GdkRectangle visible;
292 OS.gtk_tree_view_get_visible_rect (handle, &visible);
293 GdkRectangle area;
294 OS.gtk_tree_view_get_cell_area (handle, path, tree_column, &area);
295 OS.gtk_tree_path_free (path);
296 if (area.y + area.height < 0 || area.y + visible.y > visible.y + visible.height ) {
297 /* Give an image from the image list to make sure the row has
298 * the correct height.
299 */
300 if (imageList !is null && imageList.pixbufs.length > 0) {
301 if (isPixbuf) OS.g_object_set1 (cell, OS.pixbuf.ptr, cast(int)imageList.pixbufs [0]);
302 }
303 return 0;
304 }
305 }
306 if (!item.cached) {
307 //lastIndexOf = index [0];
308 setData = checkData (item);
309 }
310 }
311 void* ptr;
312 if (setData) {
313 if (isPixbuf) {
314 ptr = null;
315 OS.gtk_tree_model_get1 (tree_model, iter, modelIndex + CELL_PIXBUF, &ptr);
316 OS.g_object_set1 (cell, OS.pixbuf.ptr, cast(int)ptr);
317 } else {
318 ptr = null;
319 OS.gtk_tree_model_get1 (tree_model, iter, modelIndex + CELL_TEXT, &ptr);
320 if (ptr !is null) {
321 OS.g_object_set1 (cell, OS.text.ptr, cast(int)ptr);
322 OS.g_free (ptr);
323 }
324 }
325 }
326 if (customDraw) {
327 /*
328 * Bug on GTK. Gtk renders the background on top of the checkbox and pixbuf.
329 * This only happens in version 2.2.1 and earlier. The fix is not to set the background.
330 */
331 if (OS.GTK_VERSION > OS.buildVERSION (2, 2, 1)) {
332 if (!ownerDraw) {
333 ptr = null;
334 OS.gtk_tree_model_get1 (tree_model, iter, modelIndex + CELL_BACKGROUND, &ptr);
335 if (ptr !is null) {
336 OS.g_object_set1 (cell, OS.cell_background_gdk.ptr, cast(int)ptr);
337 }
338 }
339 }
340 if (!isPixbuf) {
341 ptr = null;
342 OS.gtk_tree_model_get1 (tree_model, iter, modelIndex + CELL_FOREGROUND, &ptr);
343 if (ptr !is null) {
344 OS.g_object_set1 (cell, OS.foreground_gdk.ptr, cast(int)ptr);
345 }
346 ptr = null;
347 OS.gtk_tree_model_get1 (tree_model, iter, modelIndex + CELL_FONT, &ptr);
348 if (ptr !is null) {
349 OS.g_object_set1 (cell, OS.font_desc.ptr, cast(int)ptr);
350 }
351 }
352 }
353 if (setData) {
354 ignoreCell = cast(GtkWidget*)cell;
355 setScrollWidth (tree_column, item);
356 ignoreCell = null;
357 }
358 }
359
360 bool checkData (TreeItem item) {
361 if (item.cached) return true;
362 if ((style & SWT.VIRTUAL) !is 0) {
363 item.cached = true;
364 TreeItem parentItem = item.getParentItem ();
365 Event event = new Event ();
366 event.item = item;
367 event.index = parentItem is null ? indexOf (item) : parentItem.indexOf (item);
368 int mask = OS.G_SIGNAL_MATCH_DATA | OS.G_SIGNAL_MATCH_ID;
369 int signal_id = OS.g_signal_lookup (OS.row_changed.ptr, OS.gtk_tree_model_get_type ());
370 OS.g_signal_handlers_block_matched (modelHandle, mask, signal_id, 0, null, null, handle);
371 currentItem = item;
372 sendEvent (SWT.SetData, event);
373 currentItem = null;
374 //widget could be disposed at this point
375 if (isDisposed ()) return false;
376 OS.g_signal_handlers_unblock_matched (modelHandle, mask, signal_id, 0, null, null, handle);
377 if (item.isDisposed ()) return false;
378 }
379 return true;
380 }
381
382 protected override void checkSubclass () {
383 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
384 }
385
386 /**
387 * Adds the listener to the collection of listeners who will
388 * be notified when the user changes the receiver's selection, by sending
389 * it one of the messages defined in the <code>SelectionListener</code>
390 * interface.
391 * <p>
392 * When <code>widgetSelected</code> is called, the item field of the event object is valid.
393 * If the receiver has the <code>SWT.CHECK</code> style and the check selection changes,
394 * the event object detail field contains the value <code>SWT.CHECK</code>.
395 * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked.
396 * The item field of the event object is valid for default selection, but the detail field is not used.
397 * </p>
398 *
399 * @param listener the listener which should be notified when the user changes the receiver's selection
400 *
401 * @exception IllegalArgumentException <ul>
402 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
403 * </ul>
404 * @exception SWTException <ul>
405 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
406 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
407 * </ul>
408 *
409 * @see SelectionListener
410 * @see #removeSelectionListener
411 * @see SelectionEvent
412 */
413 public void addSelectionListener (SelectionListener listener) {
414 checkWidget ();
415 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
416 TypedListener typedListener = new TypedListener (listener);
417 addListener (SWT.Selection, typedListener);
418 addListener (SWT.DefaultSelection, typedListener);
419 }
420
421 /**
422 * Adds the listener to the collection of listeners who will
423 * be notified when an item in the receiver is expanded or collapsed
424 * by sending it one of the messages defined in the <code>TreeListener</code>
425 * interface.
426 *
427 * @param listener the listener which should be notified
428 *
429 * @exception IllegalArgumentException <ul>
430 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
431 * </ul>
432 * @exception SWTException <ul>
433 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
434 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
435 * </ul>
436 *
437 * @see TreeListener
438 * @see #removeTreeListener
439 */
440 public void addTreeListener(TreeListener listener) {
441 checkWidget ();
442 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
443 TypedListener typedListener = new TypedListener (listener);
444 addListener (SWT.Expand, typedListener);
445 addListener (SWT.Collapse, typedListener);
446 }
447
448 int calculateWidth (GtkTreeViewColumn *column, GtkTreeIter* iter, bool recurse) {
449 OS.gtk_tree_view_column_cell_set_cell_data (column, modelHandle, iter, false, false);
450 /*
451 * Bug in GTK. The width calculated by gtk_tree_view_column_cell_get_size()
452 * always grows in size regardless of the text or images in the table.
453 * The fix is to determine the column width from the cell renderers.
454 */
455 // Code intentionally commented
456 //int [] width = new int [1];
457 //OS.gtk_tree_view_column_cell_get_size (column, null, null, null, width, null);
458 //return width [0];
459
460 int width = 0;
461 int w;
462 GtkTreePath* path = null;
463
464 if (OS.gtk_tree_view_get_expander_column (handle) is column) {
465 /* indent */
466 GdkRectangle rect;
467 OS.gtk_widget_realize (handle);
468 path = OS.gtk_tree_model_get_path (modelHandle, iter);
469 OS.gtk_tree_view_get_cell_area (handle, path, column, &rect);
470 width += rect.x;
471 /* expander */
472 OS.gtk_widget_style_get1 (handle, OS.expander_size.ptr, &w);
473 width += w + TreeItem.EXPANDER_EXTRA_PADDING;
474 }
475 OS.gtk_widget_style_get1(handle, OS.focus_line_width.ptr, &w);
476 width += 2 * w ;
477 auto list = OS.gtk_tree_view_column_get_cell_renderers (column);
478 if (list is null) return 0;
479 auto temp = list;
480 while (temp !is null) {
481 auto renderer = OS.g_list_data (temp);
482 if (renderer !is null) {
483 OS.gtk_cell_renderer_get_size (renderer, handle, null, null, null, &w, null);
484 width += w ;
485 }
486 temp = OS.g_list_next (temp);
487 }
488 OS.g_list_free (list);
489
490 if (recurse) {
491 if (path is null) path = OS.gtk_tree_model_get_path (modelHandle, iter);
492 bool expanded = OS.gtk_tree_view_row_expanded (handle, path) !is 0;
493 if (expanded) {
494 GtkTreeIter childIter;
495 bool valid = OS.gtk_tree_model_iter_children (modelHandle, &childIter, iter) !is 0;
496 while (valid) {
497 width = Math.max (width, calculateWidth (column, &childIter, true));
498 valid = OS.gtk_tree_model_iter_next (modelHandle, &childIter) !is 0;
499 }
500 }
501 }
502
503 if (path !is null) OS.gtk_tree_path_free (path);
504 return width;
505 }
506
507 /**
508 * Clears the item at the given zero-relative index in the receiver.
509 * The text, icon and other attributes of the item are set to the default
510 * value. If the tree was created with the <code>SWT.VIRTUAL</code> style,
511 * these attributes are requested again as needed.
512 *
513 * @param index the index of the item to clear
514 * @param all <code>true</code> if all child items of the indexed item should be
515 * cleared recursively, and <code>false</code> otherwise
516 *
517 * @exception IllegalArgumentException <ul>
518 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
519 * </ul>
520 * @exception SWTException <ul>
521 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
522 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
523 * </ul>
524 *
525 * @see SWT#VIRTUAL
526 * @see SWT#SetData
527 *
528 * @since 3.2
529 */
530 public void clear(int index, bool all) {
531 checkWidget ();
532 clear (null, index, all);
533 }
534
535 void clear (GtkTreeIter* parentIter, int index, bool all) {
536 GtkTreeIter iter;
537 OS.gtk_tree_model_iter_nth_child(modelHandle, &iter, parentIter, index);
538 int value;
539 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&value);
540 if (value !is -1) {
541 TreeItem item = items [value ];
542 item.clear ();
543 }
544 if (all) clearAll (all, &iter);
545 }
546
547 /**
548 * Clears all the items in the receiver. The text, icon and other
549 * attributes of the items are set to their default values. If the
550 * tree was created with the <code>SWT.VIRTUAL</code> style, these
551 * attributes are requested again as needed.
552 *
553 * @param all <code>true</code> if all child items should be cleared
554 * recursively, and <code>false</code> otherwise
555 *
556 * @exception SWTException <ul>
557 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
558 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
559 * </ul>
560 *
561 * @see SWT#VIRTUAL
562 * @see SWT#SetData
563 *
564 * @since 3.2
565 */
566 public void clearAll (bool all) {
567 checkWidget ();
568 clearAll (all, null);
569 }
570 void clearAll (bool all, GtkTreeIter* parentIter) {
571 int length = OS.gtk_tree_model_iter_n_children (modelHandle, parentIter);
572 if (length is 0) return;
573 GtkTreeIter iter;
574 bool valid = cast(bool)OS.gtk_tree_model_iter_children (modelHandle, &iter, parentIter);
575 int value;
576 while (valid) {
577 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&value);
578 if (value !is -1) {
579 TreeItem item = items [value ];
580 item.clear ();
581 }
582 if (all) clearAll (all, &iter);
583 valid = cast(bool)OS.gtk_tree_model_iter_next (modelHandle, &iter);
584 }
585 }
586
587 public override Point computeSize (int wHint, int hHint, bool changed) {
588 checkWidget ();
589 if (wHint !is SWT.DEFAULT && wHint < 0) wHint = 0;
590 if (hHint !is SWT.DEFAULT && hHint < 0) hHint = 0;
591 Point size = computeNativeSize (handle, wHint, hHint, changed);
592 Rectangle trim = computeTrim (0, 0, size.x, size.y);
593 size.x = trim.width;
594 size.y = trim.height;
595 return size;
596 }
597
598 void copyModel (void* oldModel, int oldStart, void* newModel, int newStart, uint [] types, void* oldParent, void* newParent, int modelLength) {
599 GtkTreeIter iter;
600 if (OS.gtk_tree_model_iter_children (oldModel, &iter, oldParent)) {
601 GtkTreeIter*[] oldItems = new GtkTreeIter*[]( OS.gtk_tree_model_iter_n_children (oldModel, oldParent));
602 int oldIndex = 0;
603 do {
604 GtkTreeIter* newItem = cast(GtkTreeIter*)OS.g_malloc (GtkTreeIter.sizeof);
605 if (newItem is null) error (SWT.ERROR_NO_HANDLES);
606 OS.gtk_tree_store_append (newModel, newItem, newParent);
607 int index;
608 OS.gtk_tree_model_get1 (oldModel, &iter, ID_COLUMN, cast(void**)&index);
609 TreeItem item = null;
610 if (index !is -1) {
611 item = items [index ];
612 if (item !is null) {
613 auto oldItem = cast(GtkTreeIter*)item.handle;
614 oldItems[oldIndex++] = oldItem;
615 void* ptr;
616 for (int j = 0; j < FIRST_COLUMN; j++) {
617 OS.gtk_tree_model_get1 (oldModel, oldItem, j, &ptr);
618 OS.gtk_tree_store_set1 (newModel, newItem, j, ptr );
619 if (types [j] is OS.G_TYPE_STRING ()) OS.g_free ((ptr ));
620 }
621 for (int j= 0; j<modelLength - FIRST_COLUMN; j++) {
622 OS.gtk_tree_model_get1 (oldModel, oldItem, oldStart + j, &ptr);
623 OS.gtk_tree_store_set1 (newModel, newItem, newStart + j, ptr );
624 if (types [j] is OS.G_TYPE_STRING ()) OS.g_free ((ptr ));
625 }
626 }
627 } else {
628 OS.gtk_tree_store_set1 (newModel, newItem, ID_COLUMN, cast(void*)-1 );
629 }
630 // recurse through children
631 copyModel(oldModel, oldStart, newModel, newStart, types, &iter, newItem, modelLength);
632
633 if (item!is null) {
634 item.handle = cast(GtkWidget*)newItem;
635 } else {
636 OS.g_free (newItem);
637 }
638 } while (OS.gtk_tree_model_iter_next(oldModel, &iter));
639 for (int i = 0; i < oldItems.length; i++) {
640 auto oldItem = oldItems [i];
641 if (oldItem !is null) {
642 OS.gtk_tree_store_remove (oldModel, oldItem);
643 OS.g_free (oldItem);
644 }
645 }
646 }
647 }
648
649 void createColumn (TreeColumn column, int index) {
650 /*
651 * Bug in ATK. For some reason, ATK segments fault if
652 * the GtkTreeView has a column and does not have items.
653 * The fix is to insert the column only when an item is
654 * created.
655 */
656
657 int modelIndex = FIRST_COLUMN;
658 if (columnCount !is 0) {
659 int modelLength = OS.gtk_tree_model_get_n_columns (modelHandle);
660 bool [] usedColumns = new bool [modelLength];
661 for (int i=0; i<columnCount; i++) {
662 int columnIndex = columns [i].modelIndex;
663 for (int j = 0; j < CELL_TYPES; j++) {
664 usedColumns [columnIndex + j] = true;
665 }
666 }
667 while (modelIndex < modelLength) {
668 if (!usedColumns [modelIndex]) break;
669 modelIndex++;
670 }
671 if (modelIndex is modelLength) {
672 auto oldModel = modelHandle;
673 uint[] types = getColumnTypes (columnCount + 4); // grow by 4 rows at a time
674 auto newModel = OS.gtk_tree_store_newv (types.length, types.ptr);
675 if (newModel is null) error (SWT.ERROR_NO_HANDLES);
676 copyModel (oldModel, FIRST_COLUMN, newModel, FIRST_COLUMN, types, null, null, modelLength);
677 OS.gtk_tree_view_set_model (handle, newModel);
678 OS.g_object_unref (oldModel);
679 modelHandle = newModel;
680 }
681 }
682 auto columnHandle = OS.gtk_tree_view_column_new ();
683 if (columnHandle is null) error (SWT.ERROR_NO_HANDLES);
684 if (index is 0 && columnCount > 0) {
685 TreeColumn checkColumn = columns [0];
686 createRenderers (cast(GtkTreeViewColumn *)checkColumn.handle, checkColumn.modelIndex, false, checkColumn.style);
687 }
688 createRenderers (columnHandle, modelIndex, index is 0, column is null ? 0 : column.style);
689 /*
690 * Use GTK_TREE_VIEW_COLUMN_GROW_ONLY on GTK versions < 2.3.2
691 * because fixed_height_mode is not supported.
692 */
693 bool useVirtual = (style & SWT.VIRTUAL) !is 0 && OS.GTK_VERSION >= OS.buildVERSION (2, 3, 2);
694 if (!useVirtual && columnCount is 0) {
695 OS.gtk_tree_view_column_set_sizing (columnHandle, OS.GTK_TREE_VIEW_COLUMN_GROW_ONLY);
696 } else {
697 OS.gtk_tree_view_column_set_sizing (columnHandle, OS.GTK_TREE_VIEW_COLUMN_FIXED);
698 if (columnCount !is 0) OS.gtk_tree_view_column_set_visible (columnHandle, false);
699 }
700 OS.gtk_tree_view_column_set_resizable (columnHandle, true);
701 OS.gtk_tree_view_column_set_clickable (columnHandle, true);
702 OS.gtk_tree_view_column_set_min_width (columnHandle, 0);
703 OS.gtk_tree_view_insert_column (handle, columnHandle, index);
704 if (column !is null) {
705 column.handle = cast(GtkWidget*)columnHandle;
706 column.modelIndex = modelIndex;
707 }
708 /* Disable searching when using VIRTUAL */
709 if ((style & SWT.VIRTUAL) !is 0) {
710 /*
711 * Bug in GTK. Until GTK 2.6.5, calling gtk_tree_view_set_enable_search(FALSE)
712 * would prevent the user from being able to type in text to search the tree.
713 * After 2.6.5, GTK introduced Ctrl+F as being the key binding for interactive
714 * search. This meant that even if FALSE was passed to enable_search, the user
715 * can still bring up the search pop up using the keybinding. GTK also introduced
716 * the notion of passing a -1 to gtk_set_search_column to disable searching
717 * (including the search key binding). The fix is to use the right calls
718 * for the right version.
719 */
720 if (OS.GTK_VERSION >= OS.buildVERSION (2, 6, 5)) {
721 OS.gtk_tree_view_set_search_column (handle, -1);
722 } else {
723 OS.gtk_tree_view_set_enable_search (handle, false);
724 }
725 } else {
726 /* Set the search column whenever the model changes */
727 int firstColumn = columnCount is 0 ? FIRST_COLUMN : columns [0].modelIndex;
728 OS.gtk_tree_view_set_search_column (handle, firstColumn + CELL_TEXT);
729 }
730 }
731
732 override void createHandle (int index) {
733 state |= HANDLE;
734 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null);
735 if (fixedHandle is null) error (SWT.ERROR_NO_HANDLES);
736 OS.gtk_fixed_set_has_window (fixedHandle, true);
737 scrolledHandle = cast(GtkWidget*)OS.gtk_scrolled_window_new (null, null);
738 if (scrolledHandle is null) error (SWT.ERROR_NO_HANDLES);
739 uint[] types = getColumnTypes (1);
740 modelHandle = cast(GtkTreeStore*)OS.gtk_tree_store_newv (types.length, types.ptr);
741 if (modelHandle is null) error (SWT.ERROR_NO_HANDLES);
742 handle = cast(GtkWidget*)OS.gtk_tree_view_new_with_model (modelHandle);
743 if (handle is null) error (SWT.ERROR_NO_HANDLES);
744 if ((style & SWT.CHECK) !is 0) {
745 checkRenderer = cast(GtkCellRenderer*)OS.gtk_cell_renderer_toggle_new ();
746 if (checkRenderer is null) error (SWT.ERROR_NO_HANDLES);
747 OS.g_object_ref (checkRenderer);
748 }
749 createColumn (null, 0);
750 OS.gtk_container_add (fixedHandle, scrolledHandle);
751 OS.gtk_container_add (scrolledHandle, handle);
752
753 int mode = (style & SWT.MULTI) !is 0 ? OS.GTK_SELECTION_MULTIPLE : OS.GTK_SELECTION_BROWSE;
754 auto selectionHandle = OS.gtk_tree_view_get_selection (handle);
755 OS.gtk_tree_selection_set_mode (selectionHandle, mode);
756 OS.gtk_tree_view_set_headers_visible (handle, false);
757 int hsp = (style & SWT.H_SCROLL) !is 0 ? OS.GTK_POLICY_AUTOMATIC : OS.GTK_POLICY_NEVER;
758 int vsp = (style & SWT.V_SCROLL) !is 0 ? OS.GTK_POLICY_AUTOMATIC : OS.GTK_POLICY_NEVER;
759 OS.gtk_scrolled_window_set_policy (scrolledHandle, hsp, vsp);
760 if ((style & SWT.BORDER) !is 0) OS.gtk_scrolled_window_set_shadow_type (scrolledHandle, OS.GTK_SHADOW_ETCHED_IN);
761 /* Disable searching when using VIRTUAL */
762 if ((style & SWT.VIRTUAL) !is 0) {
763 /* The fixed_height_mode property only exists in GTK 2.3.2 and greater */
764 if (OS.GTK_VERSION >= OS.buildVERSION (2, 3, 2)) {
765 OS.g_object_set1 (handle, OS.fixed_height_mode.ptr, true);
766 }
767 /*
768 * Bug in GTK. Until GTK 2.6.5, calling gtk_tree_view_set_enable_search(FALSE)
769 * would prevent the user from being able to type in text to search the tree.
770 * After 2.6.5, GTK introduced Ctrl+F as being the key binding for interactive
771 * search. This meant that even if FALSE was passed to enable_search, the user
772 * can still bring up the search pop up using the keybinding. GTK also introduced
773 * the notion of passing a -1 to gtk_set_search_column to disable searching
774 * (including the search key binding). The fix is to use the right calls
775 * for the right version.
776 */
777 if (OS.GTK_VERSION >= OS.buildVERSION (2, 6, 5)) {
778 OS.gtk_tree_view_set_search_column (handle, -1);
779 } else {
780 OS.gtk_tree_view_set_enable_search (handle, false);
781 };
782 }
783 }
784
785 void createItem (TreeColumn column, int index) {
786 if (!(0 <= index && index <= columnCount)) error (SWT.ERROR_INVALID_RANGE);
787 if (index is 0) {
788 // first column must be left aligned
789 column.style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
790 column.style |= SWT.LEFT;
791 }
792 if (columnCount is 0) {
793 column.handle = cast(GtkWidget*)OS.gtk_tree_view_get_column (handle, 0);
794 OS.gtk_tree_view_column_set_sizing (column.handle, OS.GTK_TREE_VIEW_COLUMN_FIXED);
795 OS.gtk_tree_view_column_set_visible (column.handle, false);
796 column.modelIndex = FIRST_COLUMN;
797 createRenderers (cast(GtkTreeViewColumn *)column.handle, column.modelIndex, true, column.style);
798 column.customDraw = firstCustomDraw;
799 firstCustomDraw = false;
800 } else {
801 createColumn (column, index);
802 }
803 auto boxHandle = OS.gtk_hbox_new (false, 3);
804 if (boxHandle is null) error (SWT.ERROR_NO_HANDLES);
805 auto labelHandle = OS.gtk_label_new_with_mnemonic (null);
806 if (labelHandle is null) error (SWT.ERROR_NO_HANDLES);
807 auto imageHandle = OS.gtk_image_new ();
808 if (imageHandle is null) error (SWT.ERROR_NO_HANDLES);
809 OS.gtk_container_add (boxHandle, imageHandle);
810 OS.gtk_container_add (boxHandle, labelHandle);
811 OS.gtk_widget_show (boxHandle);
812 OS.gtk_widget_show (labelHandle);
813 column.labelHandle = labelHandle;
814 column.imageHandle = imageHandle;
815 OS.gtk_tree_view_column_set_widget (column.handle, boxHandle);
816 auto widget = OS.gtk_widget_get_parent (boxHandle);
817 while (widget !is handle) {
818 if (OS.GTK_IS_BUTTON (widget)) {
819 column.buttonHandle = widget;
820 break;
821 }
822 widget = OS.gtk_widget_get_parent (widget);
823 }
824 if (columnCount is columns.length) {
825 TreeColumn [] newColumns = new TreeColumn [columns.length + 4];
826 System.arraycopy (columns, 0, newColumns, 0, columns.length);
827 columns = newColumns;
828 }
829 System.arraycopy (columns, index, columns, index + 1, columnCount++ - index);
830 columns [index] = column;
831 if ((state & FONT) !is 0) {
832 column.setFontDescription (getFontDescription ());
833 }
834 if (columnCount >= 1) {
835 for (int i=0; i<items.length; i++) {
836 TreeItem item = items [i];
837 if (item !is null) {
838 Font [] cellFont = item.cellFont;
839 if (cellFont !is null) {
840 Font [] temp = new Font [columnCount];
841 System.arraycopy (cellFont, 0, temp, 0, index);
842 System.arraycopy (cellFont, index, temp, index+1, columnCount-index-1);
843 item.cellFont = temp;
844 }
845 }
846 }
847 }
848 }
849
850 void createItem (TreeItem item, GtkTreeIter* parentIter, int index) {
851 int count = OS.gtk_tree_model_iter_n_children (modelHandle, parentIter);
852 if (index is -1) index = count;
853 if (!(0 <= index && index <= count)) error (SWT.ERROR_INVALID_RANGE);
854 item.handle = cast(GtkWidget*)OS.g_malloc (GtkTreeIter.sizeof);
855 if (item.handle is null) error(SWT.ERROR_NO_HANDLES);
856 /*
857 * Feature in GTK. It is much faster to append to a tree store
858 * than to insert at the end using gtk_tree_store_insert().
859 */
860 if (index is count) {
861 OS.gtk_tree_store_append (modelHandle, item.handle, parentIter);
862 } else {
863 OS.gtk_tree_store_insert (modelHandle, item.handle, parentIter, index);
864 }
865 int id = getId (cast(GtkTreeIter*) item.handle, false);
866 items [id] = item;
867 modelChanged = true;
868 }
869
870 void createRenderers (GtkTreeViewColumn* columnHandle, int modelIndex, bool check, int columnStyle) {
871 OS.gtk_tree_view_column_clear (columnHandle);
872 if ((style & SWT.CHECK) !is 0 && check) {
873 OS.gtk_tree_view_column_pack_start (columnHandle, checkRenderer, false);
874 OS.gtk_tree_view_column_add_attribute (columnHandle, checkRenderer, OS.active.ptr, CHECKED_COLUMN);
875 /*
876 * Feature in GTK. The inconsistent property only exists in GTK 2.2.x.
877 */
878 if (OS.GTK_VERSION >= OS.buildVERSION (2, 2, 0)) {
879 OS.gtk_tree_view_column_add_attribute (columnHandle, checkRenderer, OS.inconsistent.ptr, GRAYED_COLUMN);
880 }
881 /*
882 * Bug in GTK. GTK renders the background on top of the checkbox.
883 * This only happens in version 2.2.1 and earlier. The fix is not to set the background.
884 */
885 if (OS.GTK_VERSION > OS.buildVERSION (2, 2, 1)) {
886 if (!ownerDraw) OS.gtk_tree_view_column_add_attribute (columnHandle, checkRenderer, OS.cell_background_gdk.ptr, BACKGROUND_COLUMN);
887 }
888 if (ownerDraw) {
889 display.doCellDataProc( handle, cast(GtkTreeViewColumn*)columnHandle, cast(GtkCellRenderer*)checkRenderer );
890 OS.g_object_set_qdata (cast(GObject*)checkRenderer, Display.SWT_OBJECT_INDEX1, columnHandle);
891 }
892 }
893 auto pixbufRenderer = ownerDraw ? cast(GtkCellRenderer*)OS.g_object_new (display.gtk_cell_renderer_pixbuf_get_type (), null) : OS.gtk_cell_renderer_pixbuf_new ();
894 if (pixbufRenderer is null) error (SWT.ERROR_NO_HANDLES);
895 auto textRenderer = ownerDraw ? cast(GtkCellRenderer*)OS.g_object_new (display.gtk_cell_renderer_text_get_type (), null) : OS.gtk_cell_renderer_text_new ();
896 if (textRenderer is null) error (SWT.ERROR_NO_HANDLES);
897
898 if (ownerDraw) {
899 OS.g_object_set_qdata (cast(GObject*)pixbufRenderer, Display.SWT_OBJECT_INDEX1, columnHandle);
900 OS.g_object_set_qdata (cast(GObject*)textRenderer, Display.SWT_OBJECT_INDEX1, columnHandle);
901 }
902
903 /*
904 * Feature in GTK. When a tree view column contains only one activatable
905 * cell renderer such as a toggle renderer, mouse clicks anywhere in a cell
906 * activate that renderer. The workaround is to set a second cell renderer
907 * to be activatable.
908 */
909 if ((style & SWT.CHECK) !is 0 && check) {
910 OS.g_object_set1 (pixbufRenderer, OS.mode.ptr, OS.GTK_CELL_RENDERER_MODE_ACTIVATABLE);
911 }
912
913 /* Set alignment */
914 if ((columnStyle & SWT.RIGHT) !is 0) {
915 OS.g_object_set1_float(textRenderer, OS.xalign.ptr, 1.0f);
916 OS.gtk_tree_view_column_pack_end (columnHandle, textRenderer, true);
917 OS.gtk_tree_view_column_pack_end (columnHandle, pixbufRenderer, false);
918 OS.gtk_tree_view_column_set_alignment (columnHandle, 1f);
919 } else if ((columnStyle & SWT.CENTER) !is 0) {
920 OS.g_object_set1_float(textRenderer, OS.xalign.ptr, 0.5f);
921 OS.gtk_tree_view_column_pack_start (columnHandle, pixbufRenderer, false);
922 OS.gtk_tree_view_column_pack_end (columnHandle, textRenderer, true);
923 OS.gtk_tree_view_column_set_alignment (columnHandle, 0.5f);
924 } else {
925 OS.gtk_tree_view_column_pack_start (columnHandle, pixbufRenderer, false);
926 OS.gtk_tree_view_column_pack_start (columnHandle, textRenderer, true);
927 OS.gtk_tree_view_column_set_alignment (columnHandle, 0f);
928 }
929
930 /* Add attributes */
931 OS.gtk_tree_view_column_add_attribute (columnHandle, pixbufRenderer, OS.pixbuf.ptr, modelIndex + CELL_PIXBUF);
932 /*
933 * Bug on GTK. Gtk renders the background on top of the pixbuf.
934 * This only happens in version 2.2.1 and earlier. The fix is not to set the background.
935 */
936 if (OS.GTK_VERSION > OS.buildVERSION (2, 2, 1)) {
937 if (!ownerDraw) {
938 OS.gtk_tree_view_column_add_attribute (columnHandle, pixbufRenderer, OS.cell_background_gdk.ptr, BACKGROUND_COLUMN);
939 OS.gtk_tree_view_column_add_attribute (columnHandle, textRenderer, OS.cell_background_gdk.ptr, BACKGROUND_COLUMN);
940 }
941 }
942 OS.gtk_tree_view_column_add_attribute (columnHandle, textRenderer, OS.text.ptr, modelIndex + CELL_TEXT);
943 OS.gtk_tree_view_column_add_attribute (columnHandle, textRenderer, OS.foreground_gdk.ptr, FOREGROUND_COLUMN);
944 OS.gtk_tree_view_column_add_attribute (columnHandle, textRenderer, OS.font_desc.ptr, FONT_COLUMN);
945
946 bool customDraw = firstCustomDraw;
947 if (columnCount !is 0) {
948 for (int i=0; i<columnCount; i++) {
949 if (columns [i].handle is cast(GtkWidget*)columnHandle) {
950 customDraw = columns [i].customDraw;
951 break;
952 }
953 }
954 }
955 if ((style & SWT.VIRTUAL) !is 0 || customDraw || ownerDraw) {
956 display.doCellDataProc( handle, cast(GtkTreeViewColumn*)columnHandle, cast(GtkCellRenderer*)textRenderer );
957 display.doCellDataProc( handle, cast(GtkTreeViewColumn*)columnHandle, cast(GtkCellRenderer*)pixbufRenderer );
958 }
959 }
960
961 void createWidget (int index) {
962 super.createWidget (index);
963 items = new TreeItem [4];
964 columns = new TreeColumn [4];
965 columnCount = 0;
966 }
967
968 GdkColor* defaultBackground () {
969 return display.COLOR_LIST_BACKGROUND;
970 }
971
972 GdkColor* defaultForeground () {
973 return display.COLOR_LIST_FOREGROUND;
974 }
975
976 override void deregister () {
977 super.deregister ();
978 display.removeWidget (cast(GtkWidget*)OS.gtk_tree_view_get_selection (handle));
979 if (checkRenderer !is null) display.removeWidget (cast(GtkWidget*)checkRenderer);
980 }
981
982 /**
983 * Deselects an item in the receiver. If the item was already
984 * deselected, it remains deselected.
985 *
986 * @param item the item to be deselected
987 *
988 * @exception IllegalArgumentException <ul>
989 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
990 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
991 * </ul>
992 * @exception SWTException <ul>
993 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
994 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
995 * </ul>
996 *
997 * @since 3.4
998 */
999 public void deselect (TreeItem item) {
1000 checkWidget ();
1001 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
1002 if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
1003 bool fixColumn = showFirstColumn ();
1004 auto selection = OS.gtk_tree_view_get_selection (handle);
1005 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1006 OS.gtk_tree_selection_unselect_iter (selection, item.handle);
1007 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1008 if (fixColumn) hideFirstColumn ();
1009 }
1010
1011 /**
1012 * Deselects all selected items in the receiver.
1013 *
1014 * @exception SWTException <ul>
1015 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1016 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1017 * </ul>
1018 */
1019 public void deselectAll() {
1020 checkWidget();
1021 bool fixColumn = showFirstColumn ();
1022 auto selection = OS.gtk_tree_view_get_selection (handle);
1023 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1024 OS.gtk_tree_selection_unselect_all (selection);
1025 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1026 if (fixColumn) hideFirstColumn ();
1027 }
1028
1029 void destroyItem (TreeColumn column) {
1030 int index = 0;
1031 while (index < columnCount) {
1032 if (columns [index] is column) break;
1033 index++;
1034 }
1035 if (index is columnCount) return;
1036 auto columnHandle = column.handle;
1037 if (columnCount is 1) {
1038 firstCustomDraw = column.customDraw;
1039 }
1040 System.arraycopy (columns, index + 1, columns, index, --columnCount - index);
1041 columns [columnCount] = null;
1042 OS.gtk_tree_view_remove_column (handle, columnHandle);
1043 if (columnCount is 0) {
1044 auto oldModel = modelHandle;
1045 uint[] types = getColumnTypes (1);
1046 auto newModel = OS.gtk_tree_store_newv (types.length, types.ptr);
1047 if (newModel is null) error (SWT.ERROR_NO_HANDLES);
1048 copyModel(oldModel, column.modelIndex, newModel, FIRST_COLUMN, types, null, null, FIRST_COLUMN + CELL_TYPES);
1049 OS.gtk_tree_view_set_model (handle, newModel);
1050 OS.g_object_unref (oldModel);
1051 modelHandle = newModel;
1052 createColumn (null, 0);
1053
1054 } else {
1055 for (int i=0; i<items.length; i++) {
1056 TreeItem item = items [i];
1057 if (item !is null) {
1058 auto iter = cast(GtkTreeIter*)item.handle;
1059 int modelIndex = column.modelIndex;
1060 OS.gtk_tree_store_set1 (modelHandle, iter, modelIndex + CELL_PIXBUF, null);
1061 OS.gtk_tree_store_set1 (modelHandle, iter, modelIndex + CELL_TEXT, null);
1062 OS.gtk_tree_store_set1 (modelHandle, iter, modelIndex + CELL_FOREGROUND, null);
1063 OS.gtk_tree_store_set1 (modelHandle, iter, modelIndex + CELL_BACKGROUND, null);
1064 OS.gtk_tree_store_set1 (modelHandle, iter, modelIndex + CELL_FONT, null);
1065
1066 Font [] cellFont = item.cellFont;
1067 if (cellFont !is null) {
1068 if (columnCount is 0) {
1069 item.cellFont = null;
1070 } else {
1071 Font [] temp = new Font [columnCount];
1072 System.arraycopy (cellFont, 0, temp, 0, index);
1073 System.arraycopy (cellFont, index + 1, temp, index, columnCount - index);
1074 item.cellFont = temp;
1075 }
1076 }
1077 }
1078 }
1079 if (index is 0) {
1080 // first column must be left aligned and must show check box
1081 TreeColumn firstColumn = columns [0];
1082 firstColumn.style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
1083 firstColumn.style |= SWT.LEFT;
1084 createRenderers (cast(GtkTreeViewColumn*)firstColumn.handle, firstColumn.modelIndex, true, firstColumn.style);
1085 }
1086 }
1087 /* Disable searching when using VIRTUAL */
1088 if ((style & SWT.VIRTUAL) !is 0) {
1089 /*
1090 * Bug in GTK. Until GTK 2.6.5, calling gtk_tree_view_set_enable_search(FALSE)
1091 * would prevent the user from being able to type in text to search the tree.
1092 * After 2.6.5, GTK introduced Ctrl+F as being the key binding for interactive
1093 * search. This meant that even if FALSE was passed to enable_search, the user
1094 * can still bring up the search pop up using the keybinding. GTK also introduced
1095 * the notion of passing a -1 to gtk_set_search_column to disable searching
1096 * (including the search key binding). The fix is to use the right calls
1097 * for the right version.
1098 */
1099 if (OS.GTK_VERSION >= OS.buildVERSION (2, 6, 5)) {
1100 OS.gtk_tree_view_set_search_column (handle, -1);
1101 } else {
1102 OS.gtk_tree_view_set_enable_search (handle, false);
1103 }
1104 } else {
1105 /* Set the search column whenever the model changes */
1106 int firstColumn = columnCount is 0 ? FIRST_COLUMN : columns [0].modelIndex;
1107 OS.gtk_tree_view_set_search_column (handle, firstColumn + CELL_TEXT);
1108 }
1109 }
1110
1111
1112 void destroyItem (TreeItem item) {
1113 /*
1114 * Bug in GTK. GTK segment faults when a root tree item
1115 * is destroyed when the tree is expanded and the last leaf of
1116 * the root is selected. This only happens in versions earlier
1117 * than 2.0.6. The fix is to collapse the tree item being destroyed
1118 * when it is a root, before it is destroyed.
1119 */
1120 if (OS.GTK_VERSION < OS.buildVERSION (2, 0, 6)) {
1121 int length = OS.gtk_tree_model_iter_n_children (modelHandle, null);
1122 if (length > 0) {
1123 GtkTreeIter iter;
1124 bool valid = cast(bool)OS.gtk_tree_model_iter_children (modelHandle, &iter, null);
1125 while (valid) {
1126 //PORTING_TODO: is this condition reasonable?
1127 if (item.handle is cast(GtkWidget*)&iter) {
1128 item.setExpanded (false);
1129 break;
1130 }
1131 valid = cast(bool)OS.gtk_tree_model_iter_next (modelHandle, &iter);
1132 }
1133 }
1134 }
1135 auto selection = OS.gtk_tree_view_get_selection (handle);
1136 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1137 OS.gtk_tree_store_remove (modelHandle, item.handle);
1138 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1139 modelChanged = true;
1140 }
1141
1142 override bool dragDetect (int x, int y, bool filter, bool * consume) {
1143 bool selected = false;
1144 if (filter) {
1145 void* path;
1146 if (OS.gtk_tree_view_get_path_at_pos (handle, x, y, &path, null, null, null)) {
1147 if (path !is null) {
1148 auto selection = OS.gtk_tree_view_get_selection (handle);
1149 if (OS.gtk_tree_selection_path_is_selected (selection, path )) selected = true;
1150 OS.gtk_tree_path_free (path );
1151 }
1152 } else {
1153 return false;
1154 }
1155 }
1156 bool dragDetect = super.dragDetect (x, y, filter, consume);
1157 if (dragDetect && selected && consume !is null) consume [0] = true;
1158 return dragDetect;
1159 }
1160
1161 override GdkDrawable* eventWindow () {
1162 return paintWindow ();
1163 }
1164
1165 override void fixChildren (Shell newShell, Shell oldShell, Decorations newDecorations, Decorations oldDecorations, Menu [] menus) {
1166 super.fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus);
1167 for (int i=0; i<columnCount; i++) {
1168 TreeColumn column = columns [i];
1169 if (column.toolTipText !is null) {
1170 column.setToolTipText(oldShell, null);
1171 column.setToolTipText(newShell, column.toolTipText);
1172 }
1173 }
1174 }
1175
1176 override GdkColor* getBackgroundColor () {
1177 return getBaseColor ();
1178 }
1179
1180 public override Rectangle getClientArea () {
1181 checkWidget ();
1182 forceResize ();
1183 OS.gtk_widget_realize (handle);
1184 auto fixedWindow = OS.GTK_WIDGET_WINDOW (fixedHandle);
1185 auto binWindow = OS.gtk_tree_view_get_bin_window (handle);
1186 int binX, binY;
1187 OS.gdk_window_get_origin (binWindow, &binX, &binY);
1188 int fixedX, fixedY;
1189 OS.gdk_window_get_origin (fixedWindow, &fixedX, &fixedY);
1190 auto clientHandle = clientHandle ();
1191 int width = (state & ZERO_WIDTH) !is 0 ? 0 : OS.GTK_WIDGET_WIDTH (clientHandle);
1192 int height = (state & ZERO_HEIGHT) !is 0 ? 0 : OS.GTK_WIDGET_HEIGHT (clientHandle);
1193 return new Rectangle (fixedX - binX , fixedY - binY , width, height);
1194 }
1195
1196 int getClientWidth () {
1197 int w, h;
1198 OS.gtk_widget_realize (handle);
1199 OS.gdk_drawable_get_size(OS.gtk_tree_view_get_bin_window(handle), &w, &h);
1200 return w;
1201 }
1202
1203 /**
1204 * Returns the column at the given, zero-relative index in the
1205 * receiver. Throws an exception if the index is out of range.
1206 * Columns are returned in the order that they were created.
1207 * If no <code>TreeColumn</code>s were created by the programmer,
1208 * this method will throw <code>ERROR_INVALID_RANGE</code> despite
1209 * the fact that a single column of data may be visible in the tree.
1210 * This occurs when the programmer uses the tree like a list, adding
1211 * items but never creating a column.
1212 *
1213 * @param index the index of the column to return
1214 * @return the column at the given index
1215 *
1216 * @exception IllegalArgumentException <ul>
1217 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1218 * </ul>
1219 * @exception SWTException <ul>
1220 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1221 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1222 * </ul>
1223 *
1224 * @see Tree#getColumnOrder()
1225 * @see Tree#setColumnOrder(int[])
1226 * @see TreeColumn#getMoveable()
1227 * @see TreeColumn#setMoveable(bool)
1228 * @see SWT#Move
1229 *
1230 * @since 3.1
1231 */
1232 public TreeColumn getColumn (int index) {
1233 checkWidget();
1234 if (!(0 <= index && index < columnCount)) error (SWT.ERROR_INVALID_RANGE);
1235 return columns [index];
1236 }
1237
1238 /**
1239 * Returns the number of columns contained in the receiver.
1240 * If no <code>TreeColumn</code>s were created by the programmer,
1241 * this value is zero, despite the fact that visually, one column
1242 * of items may be visible. This occurs when the programmer uses
1243 * the tree like a list, adding items but never creating a column.
1244 *
1245 * @return the number of columns
1246 *
1247 * @exception SWTException <ul>
1248 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1249 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1250 * </ul>
1251 *
1252 * @since 3.1
1253 */
1254 public int getColumnCount () {
1255 checkWidget();
1256 return columnCount;
1257 }
1258
1259 /**
1260 * Returns an array of zero-relative integers that map
1261 * the creation order of the receiver's items to the
1262 * order in which they are currently being displayed.
1263 * <p>
1264 * Specifically, the indices of the returned array represent
1265 * the current visual order of the items, and the contents
1266 * of the array represent the creation order of the items.
1267 * </p><p>
1268 * Note: This is not the actual structure used by the receiver
1269 * to maintain its list of items, so modifying the array will
1270 * not affect the receiver.
1271 * </p>
1272 *
1273 * @return the current visual order of the receiver's items
1274 *
1275 * @exception SWTException <ul>
1276 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1277 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1278 * </ul>
1279 *
1280 * @see Tree#setColumnOrder(int[])
1281 * @see TreeColumn#getMoveable()
1282 * @see TreeColumn#setMoveable(bool)
1283 * @see SWT#Move
1284 *
1285 * @since 3.2
1286 */
1287 public int [] getColumnOrder () {
1288 checkWidget ();
1289 if (columnCount is 0) return null;
1290 auto list = OS.gtk_tree_view_get_columns (handle);
1291 if (list is null) return null;
1292 int i = 0, count = OS.g_list_length (list);
1293 int [] order = new int [count];
1294 auto temp = list;
1295 while (temp !is null) {
1296 auto column = OS.g_list_data (temp);
1297 if (column !is null) {
1298 for (int j=0; j<columnCount; j++) {
1299 if (columns [j].handle is column) {
1300 order [i++] = j;
1301 break;
1302 }
1303 }
1304 }
1305 temp = OS.g_list_next (temp);
1306 }
1307 OS.g_list_free (list);
1308 return order;
1309 }
1310
1311 uint[] getColumnTypes (int columnCount) {
1312 uint[] types = new uint [FIRST_COLUMN + (columnCount * CELL_TYPES)];
1313 // per row data
1314 types [ID_COLUMN] = OS.G_TYPE_INT ();
1315 types [CHECKED_COLUMN] = OS.G_TYPE_BOOLEAN ();
1316 types [GRAYED_COLUMN] = OS.G_TYPE_BOOLEAN ();
1317 types [FOREGROUND_COLUMN] = OS.GDK_TYPE_COLOR ();
1318 types [BACKGROUND_COLUMN] = OS.GDK_TYPE_COLOR ();
1319 types [FONT_COLUMN] = OS.PANGO_TYPE_FONT_DESCRIPTION ();
1320 // per cell data
1321 for (int i=FIRST_COLUMN; i<types.length; i+=CELL_TYPES) {
1322 types [i + CELL_PIXBUF] = OS.GDK_TYPE_PIXBUF ();
1323 types [i + CELL_TEXT] = OS.G_TYPE_STRING ();
1324 types [i + CELL_FOREGROUND] = OS.GDK_TYPE_COLOR ();
1325 types [i + CELL_BACKGROUND] = OS.GDK_TYPE_COLOR ();
1326 types [i + CELL_FONT] = OS.PANGO_TYPE_FONT_DESCRIPTION ();
1327 }
1328 return types;
1329 }
1330
1331 /**
1332 * Returns an array of <code>TreeColumn</code>s which are the
1333 * columns in the receiver. Columns are returned in the order
1334 * that they were created. If no <code>TreeColumn</code>s were
1335 * created by the programmer, the array is empty, despite the fact
1336 * that visually, one column of items may be visible. This occurs
1337 * when the programmer uses the tree like a list, adding items but
1338 * never creating a column.
1339 * <p>
1340 * Note: This is not the actual structure used by the receiver
1341 * to maintain its list of items, so modifying the array will
1342 * not affect the receiver.
1343 * </p>
1344 *
1345 * @return the items in the receiver
1346 *
1347 * @exception SWTException <ul>
1348 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1349 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1350 * </ul>
1351 *
1352 * @see Tree#getColumnOrder()
1353 * @see Tree#setColumnOrder(int[])
1354 * @see TreeColumn#getMoveable()
1355 * @see TreeColumn#setMoveable(bool)
1356 * @see SWT#Move
1357 *
1358 * @since 3.1
1359 */
1360 public TreeColumn [] getColumns () {
1361 checkWidget();
1362 TreeColumn [] result = new TreeColumn [columnCount];
1363 System.arraycopy (columns, 0, result, 0, columnCount);
1364 return result;
1365 }
1366
1367 TreeItem getFocusItem () {
1368 GtkTreePath* path;
1369 OS.gtk_tree_view_get_cursor (handle, &path, null);
1370 if (path is null) return null;
1371 TreeItem item = null;
1372 GtkTreeIter iter;
1373 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, path)) {
1374 int index;
1375 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&index);
1376 if (index !is -1) item = items [index ]; //TODO should we be creating this item when index is -1?
1377 }
1378 OS.gtk_tree_path_free (path );
1379 return item;
1380 }
1381
1382 override GdkColor* getForegroundColor () {
1383 return getTextColor ();
1384 }
1385
1386 /**
1387 * Returns the width in pixels of a grid line.
1388 *
1389 * @return the width of a grid line in pixels
1390 *
1391 * @exception SWTException <ul>
1392 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1393 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1394 * </ul>
1395 *
1396 * @since 3.1
1397 */
1398 public int getGridLineWidth () {
1399 checkWidget();
1400 return 0;
1401 }
1402
1403 /**
1404 * Returns the height of the receiver's header
1405 *
1406 * @return the height of the header or zero if the header is not visible
1407 *
1408 * @exception SWTException <ul>
1409 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1410 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1411 * </ul>
1412 *
1413 * @since 3.1
1414 */
1415 public int getHeaderHeight () {
1416 checkWidget ();
1417 if (!OS.gtk_tree_view_get_headers_visible (handle)) return 0;
1418 if (columnCount > 0) {
1419 GtkRequisition requisition;
1420 int height = 0;
1421 for (int i=0; i<columnCount; i++) {
1422 auto buttonHandle = columns [i].buttonHandle;
1423 if (buttonHandle !is null) {
1424 OS.gtk_widget_size_request (buttonHandle, &requisition);
1425 height = Math.max (height, requisition.height);
1426 }
1427 }
1428 return height;
1429 }
1430 OS.gtk_widget_realize (handle);
1431 auto fixedWindow = OS.GTK_WIDGET_WINDOW (fixedHandle);
1432 auto binWindow = OS.gtk_tree_view_get_bin_window (handle);
1433 int binY ;
1434 OS.gdk_window_get_origin (binWindow, null, &binY);
1435 int fixedY;
1436 OS.gdk_window_get_origin (fixedWindow, null, &fixedY);
1437 return binY - fixedY ;
1438 }
1439
1440 /**
1441 * Returns <code>true</code> if the receiver's header is visible,
1442 * and <code>false</code> otherwise.
1443 * <p>
1444 * If one of the receiver's ancestors is not visible or some
1445 * other condition makes the receiver not visible, this method
1446 * may still indicate that it is considered visible even though
1447 * it may not actually be showing.
1448 * </p>
1449 *
1450 * @return the receiver's header's visibility state
1451 *
1452 * @exception SWTException <ul>
1453 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1454 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1455 * </ul>
1456 *
1457 * @since 3.1
1458 */
1459 public bool getHeaderVisible () {
1460 checkWidget();
1461 return cast(bool)OS.gtk_tree_view_get_headers_visible (handle);
1462 }
1463
1464 /**
1465 * Returns the item at the given, zero-relative index in the
1466 * receiver. Throws an exception if the index is out of range.
1467 *
1468 * @param index the index of the item to return
1469 * @return the item at the given index
1470 *
1471 * @exception IllegalArgumentException <ul>
1472 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1473 * </ul>
1474 * @exception SWTException <ul>
1475 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1476 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1477 * </ul>
1478 *
1479 * @since 3.1
1480 */
1481 public TreeItem getItem (int index) {
1482 checkWidget();
1483 if (!(0 <= index && index < OS.gtk_tree_model_iter_n_children (modelHandle, null))) {
1484 error (SWT.ERROR_INVALID_RANGE);
1485 }
1486 return _getItem (null, index);
1487 }
1488
1489 /**
1490 * Returns the item at the given point in the receiver
1491 * or null if no such item exists. The point is in the
1492 * coordinate system of the receiver.
1493 * <p>
1494 * The item that is returned represents an item that could be selected by the user.
1495 * For example, if selection only occurs in items in the first column, then null is
1496 * returned if the point is outside of the item.
1497 * Note that the SWT.FULL_SELECTION style hint, which specifies the selection policy,
1498 * determines the extent of the selection.
1499 * </p>
1500 *
1501 * @param point the point used to locate the item
1502 * @return the item at the given point, or null if the point is not in a selectable item
1503 *
1504 * @exception IllegalArgumentException <ul>
1505 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
1506 * </ul>
1507 * @exception SWTException <ul>
1508 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1509 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1510 * </ul>
1511 */
1512 public TreeItem getItem (Point point) {
1513 checkWidget ();
1514 if (point is null) error (SWT.ERROR_NULL_ARGUMENT);
1515 void* path;
1516 OS.gtk_widget_realize (handle);
1517 GtkTreeViewColumn* columnHandle;
1518 if (!OS.gtk_tree_view_get_path_at_pos (handle, point.x, point.y, &path, &columnHandle, null, null)) return null;
1519 if (path is null) return null;
1520 TreeItem item = null;
1521 GtkTreeIter iter;
1522 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, path )) {
1523 bool overExpander = false;
1524 if (OS.gtk_tree_view_get_expander_column (handle) is columnHandle ) {
1525 int buffer;
1526 GdkRectangle rect;
1527 OS.gtk_tree_view_get_cell_area (handle, path, columnHandle, &rect);
1528 if (OS.GTK_VERSION < OS.buildVERSION (2, 8, 18)) {
1529 OS.gtk_widget_style_get1 (handle, OS.expander_size.ptr, &buffer);
1530 int expanderSize = buffer + TreeItem.EXPANDER_EXTRA_PADDING;
1531 overExpander = point.x < rect.x + expanderSize;
1532 } else {
1533 overExpander = point.x < rect.x;
1534 }
1535 }
1536 if (!overExpander) {
1537 item = _getItem (&iter);
1538 }
1539 }
1540 OS.gtk_tree_path_free (path );
1541 return item;
1542 }
1543
1544 /**
1545 * Returns the number of items contained in the receiver
1546 * that are direct item children of the receiver. The
1547 * number that is returned is the number of roots in the
1548 * tree.
1549 *
1550 * @return the number of items
1551 *
1552 * @exception SWTException <ul>
1553 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1554 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1555 * </ul>
1556 */
1557 public int getItemCount () {
1558 checkWidget ();
1559 return OS.gtk_tree_model_iter_n_children (modelHandle, null);
1560 }
1561
1562 /**
1563 * Returns the height of the area which would be used to
1564 * display <em>one</em> of the items in the tree.
1565 *
1566 * @return the height of one item
1567 *
1568 * @exception SWTException <ul>
1569 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1570 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1571 * </ul>
1572 */
1573 public int getItemHeight () {
1574 checkWidget ();
1575 int itemCount = OS.gtk_tree_model_iter_n_children (modelHandle, null);
1576 if (itemCount is 0) {
1577 auto column = OS.gtk_tree_view_get_column (handle, 0);
1578 int w, h;
1579 ignoreSize = true;
1580 OS.gtk_tree_view_column_cell_get_size (column, null, null, null, &w, &h);
1581 ignoreSize = false;
1582 return h ;
1583 } else {
1584 int height = 0;
1585 GtkTreeIter iter;
1586 OS.gtk_tree_model_get_iter_first (modelHandle, &iter);
1587 int columnCount = Math.max (1, this.columnCount);
1588 for (int i=0; i<columnCount; i++) {
1589 auto column = OS.gtk_tree_view_get_column (handle, i);
1590 OS.gtk_tree_view_column_cell_set_cell_data (column, modelHandle, &iter, false, false);
1591 int w, h;
1592 OS.gtk_tree_view_column_cell_get_size (column, null, null, null, &w, &h);
1593 height = Math.max (height, h );
1594 }
1595 return height;
1596 }
1597 }
1598
1599 /**
1600 * Returns a (possibly empty) array of items contained in the
1601 * receiver that are direct item children of the receiver. These
1602 * are the roots of the tree.
1603 * <p>
1604 * Note: This is not the actual structure used by the receiver
1605 * to maintain its list of items, so modifying the array will
1606 * not affect the receiver.
1607 * </p>
1608 *
1609 * @return the items
1610 *
1611 * @exception SWTException <ul>
1612 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1613 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1614 * </ul>
1615 */
1616 public TreeItem [] getItems () {
1617 checkWidget();
1618 return getItems (null);
1619 }
1620
1621 TreeItem [] getItems (GtkTreeIter* parent) {
1622 int length = OS.gtk_tree_model_iter_n_children (modelHandle, parent);
1623 TreeItem[] result = new TreeItem [length];
1624 if (length is 0) return result;
1625 if ((style & SWT.VIRTUAL) !is 0) {
1626 for (int i=0; i<length; i++) {
1627 result [i] = _getItem (parent, i);
1628 }
1629 } else {
1630 int i = 0;
1631 int index;
1632 GtkTreeIter iter;
1633 bool valid = cast(bool)OS.gtk_tree_model_iter_children (modelHandle, &iter, parent);
1634 while (valid) {
1635 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&index);
1636 result [i++] = items [index ];
1637 valid = cast(bool)OS.gtk_tree_model_iter_next (modelHandle, &iter);
1638 }
1639 }
1640 return result;
1641 }
1642
1643 /**
1644 * Returns <code>true</code> if the receiver's lines are visible,
1645 * and <code>false</code> otherwise.
1646 * <p>
1647 * If one of the receiver's ancestors is not visible or some
1648 * other condition makes the receiver not visible, this method
1649 * may still indicate that it is considered visible even though
1650 * it may not actually be showing.
1651 * </p>
1652 *
1653 * @return the visibility state of the lines
1654 *
1655 * @exception SWTException <ul>
1656 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1657 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1658 * </ul>
1659 *
1660 * @since 3.1
1661 */
1662 public bool getLinesVisible() {
1663 checkWidget();
1664 return cast(bool)OS.gtk_tree_view_get_rules_hint (handle);
1665 }
1666
1667 /**
1668 * Returns the receiver's parent item, which must be a
1669 * <code>TreeItem</code> or null when the receiver is a
1670 * root.
1671 *
1672 * @return the receiver's parent item
1673 *
1674 * @exception SWTException <ul>
1675 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1676 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1677 * </ul>
1678 */
1679 public TreeItem getParentItem () {
1680 checkWidget ();
1681 return null;
1682 }
1683
1684 GtkCellRendererPixbuf* getPixbufRenderer (GtkTreeViewColumn* column) {
1685 auto list = OS.gtk_tree_view_column_get_cell_renderers (column);
1686 if (list is null) return null;
1687 int count = OS.g_list_length (list);
1688 GtkCellRendererPixbuf* pixbufRenderer;
1689 int i = 0;
1690 while (i < count) {
1691 auto renderer = OS.g_list_nth_data (list, i);
1692 if (OS.GTK_IS_CELL_RENDERER_PIXBUF (renderer)) {
1693 pixbufRenderer = cast(GtkCellRendererPixbuf*)renderer;
1694 break;
1695 }
1696 i++;
1697 }
1698 OS.g_list_free (list);
1699 return pixbufRenderer;
1700 }
1701
1702 /**
1703 * Returns an array of <code>TreeItem</code>s that are currently
1704 * selected in the receiver. The order of the items is unspecified.
1705 * An empty array indicates that no items are selected.
1706 * <p>
1707 * Note: This is not the actual structure used by the receiver
1708 * to maintain its selection, so modifying the array will
1709 * not affect the receiver.
1710 * </p>
1711 * @return an array representing the selection
1712 *
1713 * @exception SWTException <ul>
1714 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1715 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1716 * </ul>
1717 */
1718 public TreeItem[] getSelection () {
1719 checkWidget();
1720 auto selection = OS.gtk_tree_view_get_selection (handle);
1721 if (OS.GTK_VERSION < OS.buildVERSION (2, 2, 0)) {
1722 display.treeSelectionLength = 0;
1723 display.treeSelection = new int [items.length];
1724 display.doTreeSelectionProcConnect( &treeSelectionCallbackData, handle, selection );
1725 TreeItem [] result = new TreeItem [display.treeSelectionLength];
1726 for (int i=0; i<result.length; i++) result [i] = items [display.treeSelection [i]];
1727 return result;
1728 }
1729 /*
1730 * Bug in GTK. gtk_tree_selection_get_selected_rows() segmentation faults
1731 * in versions smaller than 2.2.4 if the model is NULL. The fix is
1732 * to give a valid pointer instead.
1733 */
1734 int dummy;
1735 void* model = OS.GTK_VERSION < OS.buildVERSION (2, 2, 4) ? &dummy : null;
1736 auto list = OS.gtk_tree_selection_get_selected_rows (selection, &model);
1737 if (list !is null) {
1738 int count = OS.g_list_length (list);
1739 TreeItem [] treeSelection = new TreeItem [count];
1740 int length_ = 0;
1741 for (int i=0; i<count; i++) {
1742 auto data = OS.g_list_nth_data (list, i);
1743 GtkTreeIter iter;
1744 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, data)) {
1745 treeSelection [length_] = _getItem (&iter);
1746 length_++;
1747 }
1748 }
1749 OS.g_list_free (list);
1750 if (length_ < count) {
1751 TreeItem [] temp = new TreeItem [length_];
1752 System.arraycopy(treeSelection, 0, temp, 0, length_);
1753 treeSelection = temp;
1754 }
1755 return treeSelection;
1756 }
1757 return null;
1758 }
1759
1760 /**
1761 * Returns the number of selected items contained in the receiver.
1762 *
1763 * @return the number of selected items
1764 *
1765 * @exception SWTException <ul>
1766 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1767 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1768 * </ul>
1769 */
1770 public int getSelectionCount () {
1771 checkWidget();
1772 auto selection = OS.gtk_tree_view_get_selection (handle);
1773 if (OS.GTK_VERSION < OS.buildVERSION (2, 2, 0)) {
1774 display.treeSelectionLength = 0;
1775 display.treeSelection = null;
1776 display.doTreeSelectionProcConnect( &treeSelectionCallbackData, handle, selection );
1777 return display.treeSelectionLength;
1778 }
1779 return OS.gtk_tree_selection_count_selected_rows (selection);
1780 }
1781
1782 /**
1783 * Returns the column which shows the sort indicator for
1784 * the receiver. The value may be null if no column shows
1785 * the sort indicator.
1786 *
1787 * @return the sort indicator
1788 *
1789 * @exception SWTException <ul>
1790 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1791 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1792 * </ul>
1793 *
1794 * @see #setSortColumn(TreeColumn)
1795 *
1796 * @since 3.2
1797 */
1798 public TreeColumn getSortColumn () {
1799 checkWidget ();
1800 return sortColumn;
1801 }
1802
1803 /**
1804 * Returns the direction of the sort indicator for the receiver.
1805 * The value will be one of <code>UP</code>, <code>DOWN</code>
1806 * or <code>NONE</code>.
1807 *
1808 * @return the sort direction
1809 *
1810 * @exception SWTException <ul>
1811 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1812 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1813 * </ul>
1814 *
1815 * @see #setSortDirection(int)
1816 *
1817 * @since 3.2
1818 */
1819 public int getSortDirection () {
1820 checkWidget ();
1821 return sortDirection;
1822 }
1823
1824 GtkCellRendererText* getTextRenderer (GtkTreeViewColumn* column) {
1825 auto list = OS.gtk_tree_view_column_get_cell_renderers (column);
1826 if (list is null) return null;
1827 int count = OS.g_list_length (list);
1828 GtkCellRendererText* textRenderer;
1829 int i = 0;
1830 while (i < count) {
1831 auto renderer = OS.g_list_nth_data (list, i);
1832 if (OS.GTK_IS_CELL_RENDERER_TEXT (renderer)) {
1833 textRenderer = cast(GtkCellRendererText*)renderer;
1834 break;
1835 }
1836 i++;
1837 }
1838 OS.g_list_free (list);
1839 return textRenderer;
1840 }
1841
1842 /**
1843 * Returns the item which is currently at the top of the receiver.
1844 * This item can change when items are expanded, collapsed, scrolled
1845 * or new items are added or removed.
1846 *
1847 * @return the item at the top of the receiver
1848 *
1849 * @exception SWTException <ul>
1850 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1851 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1852 * </ul>
1853 *
1854 * @since 2.1
1855 */
1856 public TreeItem getTopItem () {
1857 checkWidget ();
1858 void* path;
1859 OS.gtk_widget_realize (handle);
1860 if (!OS.gtk_tree_view_get_path_at_pos (handle, 1, 1, &path, null, null, null)) return null;
1861 if (path is null) return null;
1862 TreeItem item = null;
1863 GtkTreeIter iter;
1864 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, path )) {
1865 item = _getItem (&iter);
1866 }
1867 OS.gtk_tree_path_free (path );
1868 return item;
1869 }
1870
1871 override int gtk_button_press_event (GtkWidget* widget, GdkEventButton* gdkEvent) {
1872 if (gdkEvent.window !is OS.gtk_tree_view_get_bin_window (handle)) return 0;
1873 auto result = super.gtk_button_press_event (widget, gdkEvent);
1874 if (result !is 0) return result;
1875 /*
1876 * Feature in GTK. In a multi-select tree view, when multiple items are already
1877 * selected, the selection state of the item is toggled and the previous selection
1878 * is cleared. This is not the desired behaviour when bringing up a popup menu.
1879 * Also, when an item is reselected with the right button, the tree view issues
1880 * an unwanted selection event. The workaround is to detect that case and not
1881 * run the default handler when the item is already part of the current selection.
1882 */
1883 int button = gdkEvent.button;
1884 if (button is 3 && gdkEvent.type is OS.GDK_BUTTON_PRESS) {
1885 void* path;
1886 if (OS.gtk_tree_view_get_path_at_pos (handle, cast(int)gdkEvent.x, cast(int)gdkEvent.y, &path, null, null, null)) {
1887 if (path !is null) {
1888 auto selection = OS.gtk_tree_view_get_selection (handle);
1889 if (OS.gtk_tree_selection_path_is_selected (selection, path )) result = 1;
1890 OS.gtk_tree_path_free (path );
1891 }
1892 }
1893 }
1894
1895 /*
1896 * Feature in GTK. When the user clicks in a single selection GtkTreeView
1897 * and there are no selected items, the first item is selected automatically
1898 * before the click is processed, causing two selection events. The is fix
1899 * is the set the cursor item to be same as the clicked item to stop the
1900 * widget from automatically selecting the first item.
1901 */
1902 if ((style & SWT.SINGLE) !is 0 && getSelectionCount () is 0) {
1903 void* path;
1904 if (OS.gtk_tree_view_get_path_at_pos (handle, cast(int)gdkEvent.x, cast(int)gdkEvent.y, &path, null, null, null)) {
1905 if (path !is null) {
1906 auto selection = OS.gtk_tree_view_get_selection (handle);
1907 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1908 OS.gtk_tree_view_set_cursor (handle, path , null, false);
1909 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
1910 OS.gtk_tree_path_free (path );
1911 }
1912 }
1913 }
1914 /*
1915 * Bug in GTK. GTK segments fault, if the GtkTreeView widget is
1916 * not in focus and all items in the widget are disposed before
1917 * it finishes processing a button press. The fix is to give
1918 * focus to the widget before it starts processing the event.
1919 */
1920 if (!OS.GTK_WIDGET_HAS_FOCUS (handle)) {
1921 OS.gtk_widget_grab_focus (handle);
1922 }
1923 return result;
1924 }
1925
1926 override int gtk_button_release_event (GtkWidget* widget, GdkEventButton* event) {
1927 auto window = OS.GDK_EVENT_WINDOW (event);
1928 if (window !is OS.gtk_tree_view_get_bin_window (handle)) return 0;
1929 return super.gtk_button_release_event (widget, event);
1930 }
1931
1932 override int gtk_changed (GtkWidget* widget) {
1933 TreeItem item = getFocusItem ();
1934 if (item !is null) {
1935 Event event = new Event ();
1936 event.item = item;
1937 postEvent (SWT.Selection, event);
1938 }
1939 return 0;
1940 }
1941
1942 override int gtk_expand_collapse_cursor_row (GtkWidget* widget, int /*long*/ logical, int /*long*/ expand, int /*long*/ open_all) {
1943 // FIXME - this flag is never cleared. It should be cleared when the expand all operation completes.
1944 if (expand !is 0 && open_all !is 0) expandAll = true;
1945 return 0;
1946 }
1947
1948 override int gtk_key_press_event (GtkWidget* widget, GdkEventKey* keyEvent) {
1949 auto result = super.gtk_key_press_event (widget, keyEvent);
1950 if (result !is 0) return result;
1951 if (OS.GTK_VERSION < OS.buildVERSION (2, 2 ,0)) {
1952 /*
1953 * Feature in GTK 2.0.x. When an item is default selected using
1954 * the return key, GTK does not issue notification. The fix is
1955 * to issue this notification when the return key is pressed.
1956 */
1957 int key = keyEvent.keyval;
1958 switch (key) {
1959 case OS.GDK_Return:
1960 case OS.GDK_KP_Enter: {
1961 Event event = new Event ();
1962 event.item = getFocusItem ();
1963 postEvent (SWT.DefaultSelection, event);
1964 break;
1965 }
1966 default:
1967 }
1968 }
1969 return result;
1970 }
1971
1972 override int gtk_motion_notify_event (GtkWidget* widget, GdkEventMotion* event) {
1973 auto window = OS.GDK_EVENT_WINDOW (event);
1974 if (window !is OS.gtk_tree_view_get_bin_window (handle)) return 0;
1975 return super.gtk_motion_notify_event (widget, event);
1976 }
1977
1978 override int gtk_popup_menu (GtkWidget* widget) {
1979 auto result = super.gtk_popup_menu (widget);
1980 /*
1981 * Bug in GTK. The context menu for the typeahead in GtkTreeViewer
1982 * opens in the bottom right corner of the screen when Shift+F10
1983 * is pressed and the typeahead window was not visible. The fix is
1984 * to prevent the context menu from opening by stopping the default
1985 * handler.
1986 *
1987 * NOTE: The bug only happens in GTK 2.6.5 and lower.
1988 */
1989 return OS.GTK_VERSION < OS.buildVERSION (2, 6, 5) ? 1 : result;
1990 }
1991
1992 override void gtk_row_activated (GtkTreeView* tree, GtkTreePath* path, GtkTreeViewColumn* column) {
1993 if (path is null) return 0;
1994 TreeItem item = null;
1995 GtkTreeIter iter;
1996 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, path)) {
1997 int index;
1998 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&index);
1999 item = items [index ];
2000 }
2001 Event event = new Event ();
2002 event.item = item;
2003 postEvent (SWT.DefaultSelection, event);
2004 return 0;
2005 }
2006
2007 override int gtk_test_collapse_row (
2008 GtkTreeView *tree,
2009 GtkTreeIter *iter,
2010 GtkTreePath *path)
2011 {
2012 int index ;
2013 OS.gtk_tree_model_get1 (modelHandle, iter, ID_COLUMN, cast(void**)&index);
2014 TreeItem item = items [index ];
2015 Event event = new Event ();
2016 event.item = item;
2017 bool oldModelChanged = modelChanged;
2018 modelChanged = false;
2019 sendEvent (SWT.Collapse, event);
2020 /*
2021 * Bug in GTK. Collapsing the target row during the test_collapse_row
2022 * handler will cause a segmentation fault if the animation code is allowed
2023 * to run. The fix is to block the animation if the row is already
2024 * collapsed.
2025 */
2026 bool changed = modelChanged || !OS.gtk_tree_view_row_expanded (handle, path);
2027 modelChanged = oldModelChanged;
2028 if (isDisposed () || item.isDisposed ()) return 1;
2029 /*
2030 * Bug in GTK. Expanding or collapsing a row which has no more
2031 * children causes the model state to become invalid, causing
2032 * GTK to give warnings and behave strangely. Other changes to
2033 * the model can cause expansion to fail when using the multiple
2034 * expansion keys (such as *). The fix is to stop the expansion
2035 * if there are model changes.
2036 *
2037 * Note: This callback must return 0 for the collapsing
2038 * animation to occur.
2039 */
2040 if (changed) {
2041 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udTEST_COLLAPSE_ROW);
2042 OS.gtk_tree_view_collapse_row (handle, path);
2043 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udTEST_COLLAPSE_ROW);
2044 return 1;
2045 }
2046 return 0;
2047 }
2048
2049 override int gtk_test_expand_row (
2050 GtkTreeView *tree,
2051 GtkTreeIter *iter,
2052 GtkTreePath *path)
2053 {
2054 int index ;
2055 OS.gtk_tree_model_get1 (modelHandle, iter, ID_COLUMN, cast(void**)&index);
2056 TreeItem item = items [index ];
2057 Event event = new Event ();
2058 event.item = item;
2059 bool oldModelChanged = modelChanged;
2060 modelChanged = false;
2061 sendEvent (SWT.Expand, event);
2062 /*
2063 * Bug in GTK. Expanding the target row during the test_expand_row
2064 * handler will cause a segmentation fault if the animation code is allowed
2065 * to run. The fix is to block the animation if the row is already
2066 * expanded.
2067 */
2068 bool changed = modelChanged || OS.gtk_tree_view_row_expanded (handle, path);
2069 modelChanged = oldModelChanged;
2070 if (isDisposed () || item.isDisposed ()) return 1;
2071 /*
2072 * Bug in GTK. Expanding or collapsing a row which has no more
2073 * children causes the model state to become invalid, causing
2074 * GTK to give warnings and behave strangely. Other changes to
2075 * the model can cause expansion to fail when using the multiple
2076 * expansion keys (such as *). The fix is to stop the expansion
2077 * if there are model changes.
2078 *
2079 * Bug in GTK. test-expand-row does not get called for each row
2080 * in an expand all operation. The fix is to block the initial
2081 * expansion and only expand a single level.
2082 *
2083 * Note: This callback must return 0 for the collapsing
2084 * animation to occur.
2085 */
2086 if (changed || expandAll) {
2087 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udTEST_EXPAND_ROW);
2088 OS.gtk_tree_view_expand_row (handle, path, false);
2089 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udTEST_EXPAND_ROW);
2090 return 1;
2091 }
2092 return 0;
2093 }
2094
2095 override int gtk_toggled (int /*long*/ renderer, char* pathStr) {
2096 auto path = OS.gtk_tree_path_new_from_string (pathStr);
2097 if (path is null) return 0;
2098 TreeItem item = null;
2099 GtkTreeIter iter;
2100 if (OS.gtk_tree_model_get_iter (modelHandle, &iter, path)) {
2101 item = _getItem (&iter);
2102 }
2103 OS.gtk_tree_path_free (path);
2104 if (item !is null) {
2105 item.setChecked (!item.getChecked ());
2106 Event event = new Event ();
2107 event.detail = SWT.CHECK;
2108 event.item = item;
2109 postEvent (SWT.Selection, event);
2110 }
2111 return 0;
2112 }
2113
2114 override void gtk_widget_size_request (GtkWidget* widget, GtkRequisition* requisition) {
2115 /*
2116 * Bug in GTK. For some reason, gtk_widget_size_request() fails
2117 * to include the height of the tree view items when there are
2118 * no columns visible. The fix is to temporarily make one column
2119 * visible.
2120 */
2121 if (columnCount is 0) {
2122 super.gtk_widget_size_request (widget, requisition);
2123 return;
2124 }
2125 auto columns = OS.gtk_tree_view_get_columns (handle), list = columns;
2126 bool fixVisible = columns !is null;
2127 while (list !is null) {
2128 auto column = OS.g_list_data (list);
2129 if (OS.gtk_tree_view_column_get_visible (column)) {
2130 fixVisible = false;
2131 break;
2132 }
2133 list = OS.g_list_next (list);
2134 }
2135 GtkTreeViewColumn* columnHandle;
2136 if (fixVisible) {
2137 columnHandle = cast(GtkTreeViewColumn*)OS.g_list_data (columns);
2138 OS.gtk_tree_view_column_set_visible (columnHandle, true);
2139 }
2140 super.gtk_widget_size_request (widget, requisition);
2141 if (fixVisible) {
2142 OS.gtk_tree_view_column_set_visible (columnHandle, false);
2143 }
2144 if (columns !is null) OS.g_list_free (columns);
2145 }
2146
2147 void hideFirstColumn () {
2148 auto firstColumn = OS.gtk_tree_view_get_column (handle, 0);
2149 OS.gtk_tree_view_column_set_visible (firstColumn, false);
2150 }
2151
2152 override void hookEvents () {
2153 super.hookEvents ();
2154 auto selection = OS.gtk_tree_view_get_selection(handle);
2155 OS.g_signal_connect_closure (selection, OS.changed.ptr, display.closures [CHANGED], false);
2156 OS.g_signal_connect_closure (handle, OS.row_activated.ptr, display.closures [ROW_ACTIVATED], false);
2157 OS.g_signal_connect_closure (handle, OS.test_expand_row.ptr, display.closures [TEST_EXPAND_ROW], false);
2158 OS.g_signal_connect_closure (handle, OS.test_collapse_row.ptr, display.closures [TEST_COLLAPSE_ROW], false);
2159 OS.g_signal_connect_closure (handle, OS.expand_collapse_cursor_row.ptr, display.closures [EXPAND_COLLAPSE_CURSOR_ROW], false);
2160 if (checkRenderer !is null) {
2161 OS.g_signal_connect_closure (checkRenderer, OS.toggled.ptr, display.closures [TOGGLED], false);
2162 }
2163 }
2164
2165 /**
2166 * Searches the receiver's list starting at the first column
2167 * (index 0) until a column is found that is equal to the
2168 * argument, and returns the index of that column. If no column
2169 * is found, returns -1.
2170 *
2171 * @param column the search column
2172 * @return the index of the column
2173 *
2174 * @exception IllegalArgumentException <ul>
2175 * <li>ERROR_NULL_ARGUMENT - if the column is null</li>
2176 * </ul>
2177 * @exception SWTException <ul>
2178 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2179 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2180 * </ul>
2181 *
2182 * @since 3.1
2183 */
2184 public int indexOf (TreeColumn column) {
2185 checkWidget();
2186 if (column is null) error (SWT.ERROR_NULL_ARGUMENT);
2187 for (int i=0; i<columnCount; i++) {
2188 if (columns [i] is column) return i;
2189 }
2190 return -1;
2191 }
2192
2193 /**
2194 * Searches the receiver's list starting at the first item
2195 * (index 0) until an item is found that is equal to the
2196 * argument, and returns the index of that item. If no item
2197 * is found, returns -1.
2198 *
2199 * @param item the search item
2200 * @return the index of the item
2201 *
2202 * @exception IllegalArgumentException <ul>
2203 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
2204 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
2205 * </ul>
2206 * @exception SWTException <ul>
2207 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2208 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2209 * </ul>
2210 *
2211 * @since 3.1
2212 */
2213 public int indexOf (TreeItem item) {
2214 checkWidget();
2215 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
2216 if (item.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
2217 int index = -1;
2218 auto path = OS.gtk_tree_model_get_path (modelHandle, item.handle);
2219 int depth = OS.gtk_tree_path_get_depth (path);
2220 if (depth is 1) {
2221 auto indices = OS.gtk_tree_path_get_indices (path);
2222 if (indices !is null) {
2223 index = indices[0];
2224 }
2225 }
2226 OS.gtk_tree_path_free (path);
2227 return index;
2228 }
2229
2230 override bool mnemonicHit (wchar key) {
2231 for (int i=0; i<columnCount; i++) {
2232 auto labelHandle = columns [i].labelHandle;
2233 if (labelHandle !is null && mnemonicHit (labelHandle, key)) return true;
2234 }
2235 return false;
2236 }
2237
2238 override bool mnemonicMatch (wchar key) {
2239 for (int i=0; i<columnCount; i++) {
2240 auto labelHandle = columns [i].labelHandle;
2241 if (labelHandle !is null && mnemonicMatch (labelHandle, key)) return true;
2242 }
2243 return false;
2244 }
2245
2246 override GdkDrawable* paintWindow () {
2247 OS.gtk_widget_realize (handle);
2248 return OS.gtk_tree_view_get_bin_window (handle);
2249 }
2250
2251 void recreateRenderers () {
2252 if (checkRenderer !is null) {
2253 display.removeWidget (cast(GtkWidget*)checkRenderer);
2254 OS.g_object_unref (checkRenderer);
2255 checkRenderer = ownerDraw ? cast(GtkCellRenderer*)OS.g_object_new (display.gtk_cell_renderer_toggle_get_type(), null) : OS.gtk_cell_renderer_toggle_new ();
2256 if (checkRenderer is null) error (SWT.ERROR_NO_HANDLES);
2257 OS.g_object_ref (checkRenderer);
2258 display.addWidget (cast(GtkWidget*)checkRenderer, this);
2259 OS.g_signal_connect_closure (checkRenderer, OS.toggled.ptr, display.closures [TOGGLED], false);
2260 }
2261 if (columnCount is 0) {
2262 createRenderers (OS.gtk_tree_view_get_column (handle, 0), Tree.FIRST_COLUMN, true, 0);
2263 } else {
2264 for (int i = 0; i < columnCount; i++) {
2265 TreeColumn column = columns [i];
2266 createRenderers (cast(GtkTreeViewColumn*)column.handle, column.modelIndex, i is 0, column.style);
2267 }
2268 }
2269 }
2270
2271 override void redrawBackgroundImage () {
2272 Control control = findBackgroundControl ();
2273 if (control !is null && control.backgroundImage !is null) {
2274 redrawWidget (0, 0, 0, 0, true, false, false);
2275 }
2276 }
2277
2278 override void register () {
2279 super.register ();
2280 display.addWidget (cast(GtkWidget*)OS.gtk_tree_view_get_selection (handle), this);
2281 if (checkRenderer !is null) display.addWidget (cast(GtkWidget*)checkRenderer, this);
2282 }
2283
2284 void releaseItem (TreeItem item, bool release) {
2285 int index;
2286 OS.gtk_tree_model_get1 (modelHandle, item.handle, ID_COLUMN, cast(void**)&index);
2287 if (index is -1) return;
2288 if (release) item.release (false);
2289 items [index ] = null;
2290 }
2291
2292 void releaseItems (GtkTreeIter* parentIter) {
2293 int index;
2294 GtkTreeIter iter;
2295 bool valid = cast(bool)OS.gtk_tree_model_iter_children (modelHandle, &iter, parentIter);
2296 while (valid) {
2297 releaseItems (&iter);
2298 if (!isDisposed ()) {
2299 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&index);
2300 if (index !is -1) {
2301 TreeItem item = items [index ];
2302 if (item !is null) releaseItem (item, true);
2303 }
2304 }
2305 valid = cast(bool)OS.gtk_tree_model_iter_next (modelHandle, &iter);
2306 }
2307 }
2308
2309 override void releaseChildren (bool destroy) {
2310 if (items !is null) {
2311 for (int i=0; i<items.length; i++) {
2312 TreeItem item = items [i];
2313 if (item !is null && !item.isDisposed ()) {
2314 item.release (false);
2315 }
2316 }
2317 items = null;
2318 }
2319 if (columns !is null) {
2320 for (int i=0; i<columnCount; i++) {
2321 TreeColumn column = columns [i];
2322 if (column !is null && !column.isDisposed ()) {
2323 column.release (false);
2324 }
2325 }
2326 columns = null;
2327 }
2328 super.releaseChildren (destroy);
2329 }
2330
2331 override void releaseWidget () {
2332 super.releaseWidget ();
2333 if (modelHandle !is null) OS.g_object_unref (modelHandle);
2334 modelHandle = null;
2335 if (checkRenderer !is null) OS.g_object_unref (checkRenderer);
2336 checkRenderer = null;
2337 if (imageList !is null) imageList.dispose ();
2338 if (headerImageList !is null) headerImageList.dispose ();
2339 imageList = headerImageList = null;
2340 currentItem = null;
2341 }
2342
2343 void remove (GtkTreeIter* parentIter, int start, int end) {
2344 if (start > end) return;
2345 int itemCount = OS.gtk_tree_model_iter_n_children (modelHandle, parentIter);
2346 if (!(0 <= start && start <= end && end < itemCount)) {
2347 error (SWT.ERROR_INVALID_RANGE);
2348 }
2349 auto selection = OS.gtk_tree_view_get_selection (handle);
2350 GtkTreeIter iter;
2351 int index = start;
2352 for (int i = start; i <= end; i++) {
2353 OS.gtk_tree_model_iter_nth_child (modelHandle, &iter, parentIter, index);
2354 int value;
2355 OS.gtk_tree_model_get1 (modelHandle, &iter, ID_COLUMN, cast(void**)&value);
2356 TreeItem item = value !is -1 ? items [value ] : null;
2357 if (item !is null && !item.isDisposed ()) {
2358 item.dispose ();
2359 } else {
2360 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2361 OS.gtk_tree_store_remove (modelHandle, &iter);
2362 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2363 }
2364 }
2365 }
2366
2367 /**
2368 * Removes all of the items from the receiver.
2369 *
2370 * @exception SWTException <ul>
2371 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2372 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2373 * </ul>
2374 */
2375 public void removeAll () {
2376 checkWidget ();
2377 for (int i=0; i<items.length; i++) {
2378 TreeItem item = items [i];
2379 if (item !is null && !item.isDisposed ()) item.release (false);
2380 }
2381 items = new TreeItem[4];
2382 auto selection = OS.gtk_tree_view_get_selection (handle);
2383 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2384 OS.gtk_tree_store_clear (modelHandle);
2385 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2386
2387 /* Disable searching when using VIRTUAL */
2388 if ((style & SWT.VIRTUAL) !is 0) {
2389 /*
2390 * Bug in GTK. Until GTK 2.6.5, calling gtk_tree_view_set_enable_search(FALSE)
2391 * would prevent the user from being able to type in text to search the tree.
2392 * After 2.6.5, GTK introduced Ctrl+F as being the key binding for interactive
2393 * search. This meant that even if FALSE was passed to enable_search, the user
2394 * can still bring up the search pop up using the keybinding. GTK also introduced
2395 * the notion of passing a -1 to gtk_set_search_column to disable searching
2396 * (including the search key binding). The fix is to use the right calls
2397 * for the right version.
2398 */
2399 if (OS.GTK_VERSION >= OS.buildVERSION (2, 6, 5)) {
2400 OS.gtk_tree_view_set_search_column (handle, -1);
2401 } else {
2402 OS.gtk_tree_view_set_enable_search (handle, false);
2403 }
2404 } else {
2405 /* Set the search column whenever the model changes */
2406 int firstColumn = columnCount is 0 ? FIRST_COLUMN : columns [0].modelIndex;
2407 OS.gtk_tree_view_set_search_column (handle, firstColumn + CELL_TEXT);
2408 }
2409 }
2410
2411 /**
2412 * Removes the listener from the collection of listeners who will
2413 * be notified when the user changes the receiver's selection.
2414 *
2415 * @param listener the listener which should no longer be notified
2416 *
2417 * @exception IllegalArgumentException <ul>
2418 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2419 * </ul>
2420 * @exception SWTException <ul>
2421 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2422 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2423 * </ul>
2424 *
2425 * @see SelectionListener
2426 * @see #addSelectionListener
2427 */
2428 public void removeSelectionListener (SelectionListener listener) {
2429 checkWidget ();
2430 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
2431 eventTable.unhook (SWT.Selection, listener);
2432 eventTable.unhook (SWT.DefaultSelection, listener);
2433 }
2434
2435 /**
2436 * Removes the listener from the collection of listeners who will
2437 * be notified when items in the receiver are expanded or collapsed.
2438 *
2439 * @param listener the listener which should no longer be notified
2440 *
2441 * @exception IllegalArgumentException <ul>
2442 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2443 * </ul>
2444 * @exception SWTException <ul>
2445 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2446 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2447 * </ul>
2448 *
2449 * @see TreeListener
2450 * @see #addTreeListener
2451 */
2452 public void removeTreeListener(TreeListener listener) {
2453 checkWidget ();
2454 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
2455 if (eventTable is null) return;
2456 eventTable.unhook (SWT.Expand, listener);
2457 eventTable.unhook (SWT.Collapse, listener);
2458 }
2459
2460 override void rendererGetSizeProc (
2461 GtkCellRenderer *cell,
2462 GtkWidget *widget,
2463 GdkRectangle *cell_area,
2464 int *x_offset,
2465 int *y_offset,
2466 int *width,
2467 int *height)
2468 {
2469 auto g_class = OS.g_type_class_peek_parent (OS.G_OBJECT_GET_CLASS (cell));
2470 GtkCellRendererClass* klass = cast(GtkCellRendererClass*)g_class;
2471 klass.get_size( cell, handle, cell_area, x_offset, y_offset, width, height);
2472 if (!ignoreSize && OS.GTK_IS_CELL_RENDERER_TEXT (cell)) {
2473 auto iter = cast(GtkTreeIter*)OS.g_object_get_qdata (cast(GObject*)cell, Display.SWT_OBJECT_INDEX2);
2474 TreeItem item = null;
2475 if (iter !is null) item = _getItem (iter);
2476 if (item !is null) {
2477 int columnIndex = 0;
2478 if (columnCount > 0) {
2479 auto columnHandle = cast(GtkTreeViewColumn*)OS.g_object_get_qdata (cast(GObject*)cell, Display.SWT_OBJECT_INDEX1);
2480 for (int i = 0; i < columnCount; i++) {
2481 if (columns [i].handle is cast(GtkWidget*)columnHandle) {
2482 columnIndex = i;
2483 break;
2484 }
2485 }
2486 }
2487 if (hooks (SWT.MeasureItem)) {
2488 int contentWidth, contentHeight;
2489 if (width !is null) contentWidth = *width;
2490 if (height !is null) contentHeight = *height;
2491 Image image = item.getImage (columnIndex);
2492 int imageWidth = 0;
2493 if (image !is null) {
2494 Rectangle bounds = image.getBounds ();
2495 imageWidth = bounds.width;
2496 }
2497 contentWidth += imageWidth;
2498 GC gc = new GC (this);
2499 gc.setFont (item.getFont (columnIndex));
2500 Event event = new Event ();
2501 event.item = item;
2502 event.index = columnIndex;
2503 event.gc = gc;
2504 event.width = contentWidth ;
2505 event.height = contentHeight ;
2506 sendEvent (SWT.MeasureItem, event);
2507 gc.dispose ();
2508 contentWidth = event.width - imageWidth;
2509 if (contentHeight < event.height) contentHeight = event.height;
2510 if (width !is null) *width = contentWidth;
2511 if (height !is null) *height = contentHeight;
2512 }
2513 }
2514 }
2515 }
2516
2517 override void rendererRenderProc (
2518 GtkCellRenderer * cell,
2519 GdkDrawable * window,
2520 GtkWidget * widget,
2521 GdkRectangle *background_area,
2522 GdkRectangle *cell_area,
2523 GdkRectangle *expose_area,
2524 int flags)
2525 {
2526 TreeItem item = null;
2527 auto iter = cast(GtkTreeIter*)OS.g_object_get_qdata (cast(GObject*)cell, Display.SWT_OBJECT_INDEX2);
2528 if (iter !is null) item = _getItem (iter);
2529 auto columnHandle = cast(GtkTreeViewColumn*)OS.g_object_get_qdata (cast(GObject*)cell, Display.SWT_OBJECT_INDEX1);
2530 int columnIndex = 0;
2531 if (columnCount > 0) {
2532 for (int i = 0; i < columnCount; i++) {
2533 if (columns [i].handle is cast(GtkWidget*)columnHandle) {
2534 columnIndex = i;
2535 break;
2536 }
2537 }
2538 }
2539 if (item !is null) {
2540 if (OS.GTK_IS_CELL_RENDERER_TOGGLE (cell) || (OS.GTK_IS_CELL_RENDERER_PIXBUF (cell) && (columnIndex !is 0 || (style & SWT.CHECK) is 0))) {
2541 drawFlags = cast(int)/*64*/flags;
2542 drawState = SWT.FOREGROUND;
2543 void* ptr;
2544 OS.gtk_tree_model_get1 (modelHandle, item.handle, Tree.BACKGROUND_COLUMN, &ptr);
2545 if (ptr is null) {
2546 int modelIndex = columnCount is 0 ? Tree.FIRST_COLUMN : columns [columnIndex].modelIndex;
2547 OS.gtk_tree_model_get1 (modelHandle, item.handle, modelIndex + Tree.CELL_BACKGROUND, &ptr);
2548 }
2549 if (ptr !is null) drawState |= SWT.BACKGROUND;
2550 if ((flags & OS.GTK_CELL_RENDERER_SELECTED) !is 0) drawState |= SWT.SELECTED;
2551 if ((flags & OS.GTK_CELL_RENDERER_FOCUSED) !is 0) drawState |= SWT.FOCUSED;
2552
2553 GdkRectangle rect;
2554 auto path = OS.gtk_tree_model_get_path (modelHandle, iter);
2555 OS.gtk_tree_view_get_background_area (handle, path, columnHandle, &rect);
2556 OS.gtk_tree_path_free (path);
2557
2558 if ((drawState & SWT.SELECTED) is 0) {
2559 Control control = findBackgroundControl ();
2560 if (control !is null && control.backgroundImage !is null) {
2561 OS.gdk_window_clear_area (window, rect.x, rect.y, rect.width, rect.height);
2562 }
2563 }
2564
2565 if (hooks (SWT.EraseItem)) {
2566 bool wasSelected = false;
2567 if ((drawState & SWT.SELECTED) !is 0) {
2568 wasSelected = true;
2569 OS.gdk_window_clear_area (window, rect.x, rect.y, rect.width, rect.height);
2570 }
2571 GC gc = new GC (this);
2572 if ((drawState & SWT.SELECTED) !is 0) {
2573 gc.setBackground (display.getSystemColor (SWT.COLOR_LIST_SELECTION));
2574 gc.setForeground (display.getSystemColor (SWT.COLOR_LIST_SELECTION_TEXT));
2575 } else {
2576 gc.setBackground (item.getBackground (columnIndex));
2577 gc.setForeground (item.getForeground (columnIndex));
2578 }
2579 gc.setFont (item.getFont (columnIndex));
2580 if ((style & SWT.MIRRORED) !is 0) rect.x = getClientWidth () - rect.width - rect.x;
2581 gc.setClipping (rect.x, rect.y, rect.width, rect.height);
2582 Event event = new Event ();
2583 event.item = item;
2584 event.index = columnIndex;
2585 event.gc = gc;
2586 event.x = rect.x;
2587 event.y = rect.y;
2588 event.width = rect.width;
2589 event.height = rect.height;
2590 event.detail = drawState;
2591 sendEvent (SWT.EraseItem, event);
2592 drawForeground = null;
2593 drawState = event.doit ? event.detail : 0;
2594 drawFlags &= ~(OS.GTK_CELL_RENDERER_FOCUSED | OS.GTK_CELL_RENDERER_SELECTED);
2595 if ((drawState & SWT.SELECTED) !is 0) drawFlags |= OS.GTK_CELL_RENDERER_SELECTED;
2596 if ((drawState & SWT.FOCUSED) !is 0) drawFlags |= OS.GTK_CELL_RENDERER_FOCUSED;
2597 if ((drawState & SWT.SELECTED) !is 0) {
2598 auto style = OS.gtk_widget_get_style (widget);
2599 //TODO - parity and sorted
2600 OS.gtk_paint_flat_box (style, window, OS.GTK_STATE_SELECTED, OS.GTK_SHADOW_NONE, &rect, widget, "cell_odd".ptr, rect.x, rect.y, rect.width, rect.height);
2601 } else {
2602 if (wasSelected) drawForeground = gc.getForeground ().handle;
2603 }
2604 gc.dispose();
2605 }
2606 }
2607 }
2608 int /*long*/ result = 0;
2609 if ((drawState & SWT.BACKGROUND) !is 0 && (drawState & SWT.SELECTED) is 0) {
2610 GC gc = new GC (this);
2611 gc.setBackground (item.getBackground (columnIndex));
2612 gc.fillRectangle (background_area.x, background_area.y, background_area.width, background_area.height);
2613 gc.dispose ();
2614 }
2615 if ((drawState & SWT.FOREGROUND) !is 0 || OS.GTK_IS_CELL_RENDERER_TOGGLE (cell)) {
2616 auto g_class = OS.g_type_class_peek_parent (OS.G_OBJECT_GET_CLASS (cell));
2617 GtkCellRendererClass* klass = cast(GtkCellRendererClass*)g_class;
2618 if (drawForeground !is null && OS.GTK_IS_CELL_RENDERER_TEXT (cell)) {
2619 OS.g_object_set1 (cell, OS.foreground_gdk.ptr, cast(int)drawForeground);
2620 }
2621 klass.render( cell, window, handle, background_area, cell_area, expose_area, drawFlags);
2622 }
2623 if (item !is null) {
2624 if (OS.GTK_IS_CELL_RENDERER_TEXT (cell)) {
2625 if (hooks (SWT.PaintItem)) {
2626 GdkRectangle rect;
2627 auto path = OS.gtk_tree_model_get_path (modelHandle, iter);
2628 OS.gtk_tree_view_get_cell_area (handle, path, columnHandle, &rect);
2629 OS.gtk_tree_path_free (path);
2630 if (OS.GTK_VERSION < OS.buildVERSION (2, 8, 18) && OS.gtk_tree_view_get_expander_column (handle) is columnHandle) {
2631 int buffer;
2632 OS.gtk_widget_style_get1 (handle, OS.expander_size.ptr, &buffer);
2633 rect.x += buffer + TreeItem.EXPANDER_EXTRA_PADDING;
2634 rect.width -= buffer + TreeItem.EXPANDER_EXTRA_PADDING;
2635 //OS.gtk_widget_style_get (handle, OS.horizontal_separator, buffer, 0);
2636 //rect.x += buffer[0];
2637 //rect.width -= buffer [0]; // TODO Is this required for some versions?
2638 }
2639 ignoreSize = true;
2640 int contentX, contentWidth;
2641 OS.gtk_cell_renderer_get_size (cell, handle, null, null, null, &contentWidth, null);
2642 OS.gtk_tree_view_column_cell_get_position (columnHandle, cell, &contentX, null);
2643 ignoreSize = false;
2644 Image image = item.getImage (columnIndex);
2645 int imageWidth = 0;
2646 if (image !is null) {
2647 Rectangle bounds = image.getBounds ();
2648 imageWidth = bounds.width;
2649 }
2650 contentX -= imageWidth;
2651 contentWidth += imageWidth;
2652 GC gc = new GC (this);
2653 if ((drawState & SWT.SELECTED) !is 0) {
2654 gc.setBackground (display.getSystemColor (SWT.COLOR_LIST_SELECTION));
2655 gc.setForeground (display.getSystemColor (SWT.COLOR_LIST_SELECTION_TEXT));
2656 } else {
2657 gc.setBackground (item.getBackground (columnIndex));
2658 Color foreground = drawForeground !is null ? Color.gtk_new (display, drawForeground) : item.getForeground (columnIndex);
2659 gc.setForeground (foreground);
2660 }
2661 gc.setFont (item.getFont (columnIndex));
2662 if ((style & SWT.MIRRORED) !is 0) rect.x = getClientWidth () - rect.width - rect.x;
2663 gc.setClipping (rect.x, rect.y, rect.width, rect.height);
2664 Event event = new Event ();
2665 event.item = item;
2666 event.index = columnIndex;
2667 event.gc = gc;
2668 event.x = rect.x + contentX;
2669 event.y = rect.y;
2670 event.width = contentWidth;
2671 event.height = rect.height;
2672 event.detail = drawState;
2673 sendEvent (SWT.PaintItem, event);
2674 gc.dispose();
2675 }
2676 }
2677 }
2678 return result;
2679 }
2680
2681 void resetCustomDraw () {
2682 if ((style & SWT.VIRTUAL) !is 0 || ownerDraw) return;
2683 int end = Math.max (1, columnCount);
2684 for (int i=0; i<end; i++) {
2685 bool customDraw = columnCount !is 0 ? columns [i].customDraw : firstCustomDraw;
2686 if (customDraw) {
2687 auto column = OS.gtk_tree_view_get_column (handle, i);
2688 auto textRenderer = getTextRenderer (column);
2689 OS.gtk_tree_view_column_set_cell_data_func (column, textRenderer, null, null, null);
2690 if (columnCount !is 0) columns [i].customDraw = false;
2691 }
2692 }
2693 firstCustomDraw = false;
2694 }
2695
2696 /**
2697 * Display a mark indicating the point at which an item will be inserted.
2698 * The drop insert item has a visual hint to show where a dragged item
2699 * will be inserted when dropped on the tree.
2700 *
2701 * @param item the insert item. Null will clear the insertion mark.
2702 * @param before true places the insert mark above 'item'. false places
2703 * the insert mark below 'item'.
2704 *
2705 * @exception IllegalArgumentException <ul>
2706 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
2707 * </ul>
2708 * @exception SWTException <ul>
2709 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2710 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2711 * </ul>
2712 */
2713 public void setInsertMark (TreeItem item, bool before) {
2714 checkWidget ();
2715 if (item is null) {
2716 OS.gtk_tree_view_set_drag_dest_row(handle, null, OS.GTK_TREE_VIEW_DROP_BEFORE);
2717 return;
2718 }
2719 if (item.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
2720 if (item.parent !is this) return;
2721 Rectangle rect = item.getBounds();
2722 void* path;
2723 OS.gtk_widget_realize (handle);
2724 if (!OS.gtk_tree_view_get_path_at_pos(handle, rect.x, rect.y, &path, null, null, null)) return;
2725 if (path is null) return;
2726 int position = before ? OS.GTK_TREE_VIEW_DROP_BEFORE : OS.GTK_TREE_VIEW_DROP_AFTER;
2727 OS.gtk_tree_view_set_drag_dest_row(handle, path, position);
2728 OS.gtk_tree_path_free (path );
2729 }
2730
2731 void setItemCount (GtkTreeIter* parentIter, int count) {
2732 int itemCount = OS.gtk_tree_model_iter_n_children (modelHandle, parentIter);
2733 if (count is itemCount) return;
2734 bool isVirtual = (style & SWT.VIRTUAL) !is 0;
2735 if (!isVirtual) setRedraw (false);
2736 remove (parentIter, count, itemCount - 1);
2737 if (isVirtual) {
2738 for (int i=itemCount; i<count; i++) {
2739 GtkTreeIter iter;
2740 OS.gtk_tree_store_append (modelHandle, &iter, parentIter);
2741 OS.gtk_tree_store_set1 (modelHandle, &iter, ID_COLUMN, cast(void*)-1);
2742 }
2743 } else {
2744 for (int i=itemCount; i<count; i++) {
2745 new TreeItem (this, parentIter, SWT.NONE, i, true);
2746 }
2747 }
2748 if (!isVirtual) setRedraw (true);
2749 modelChanged = true;
2750 }
2751
2752 /**
2753 * Sets the number of root-level items contained in the receiver.
2754 *
2755 * @param count the number of items
2756 *
2757 * @exception SWTException <ul>
2758 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2759 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2760 * </ul>
2761 *
2762 * @since 3.2
2763 */
2764 public void setItemCount (int count) {
2765 checkWidget ();
2766 count = Math.max (0, count);
2767 setItemCount (null, count);
2768 }
2769
2770 /**
2771 * Selects an item in the receiver. If the item was already
2772 * selected, it remains selected.
2773 *
2774 * @param item the item to be selected
2775 *
2776 * @exception IllegalArgumentException <ul>
2777 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
2778 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
2779 * </ul>
2780 * @exception SWTException <ul>
2781 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2782 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2783 * </ul>
2784 *
2785 * @since 3.4
2786 */
2787 public void select (TreeItem item) {
2788 checkWidget ();
2789 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
2790 if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
2791 bool fixColumn = showFirstColumn ();
2792 auto selection = OS.gtk_tree_view_get_selection (handle);
2793 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2794 OS.gtk_tree_selection_select_iter (selection, item.handle);
2795 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2796 if (fixColumn) hideFirstColumn ();
2797 }
2798
2799 /**
2800 * Selects all of the items in the receiver.
2801 * <p>
2802 * If the receiver is single-select, do nothing.
2803 * </p>
2804 *
2805 * @exception SWTException <ul>
2806 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2807 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2808 * </ul>
2809 */
2810 public void selectAll () {
2811 checkWidget();
2812 if ((style & SWT.SINGLE) !is 0) return;
2813 bool fixColumn = showFirstColumn ();
2814 auto selection = OS.gtk_tree_view_get_selection (handle);
2815 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2816 OS.gtk_tree_selection_select_all (selection);
2817 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
2818 if (fixColumn) hideFirstColumn ();
2819 }
2820
2821 override void setBackgroundColor (GdkColor* color) {
2822 super.setBackgroundColor (color);
2823 OS.gtk_widget_modify_base (handle, 0, color);
2824 }
2825
2826 override void setBackgroundPixmap (GdkPixmap* pixmap) {
2827 super.setBackgroundPixmap (pixmap);
2828 auto window = paintWindow ();
2829 if (window !is null) OS.gdk_window_set_back_pixmap (window, null, true);
2830 }
2831
2832 override int setBounds (int x, int y, int width, int height, bool move, bool resize) {
2833 int result = super.setBounds (x, y, width, height, move, resize);
2834 /*
2835 * Bug on GTK. The tree view sometimes does not get a paint
2836 * event or resizes to a one pixel square when resized in a new
2837 * shell that is not visible after any event loop has been run. The
2838 * problem is intermittent. It doesn't seem to happen the first time
2839 * a new shell is created. The fix is to ensure the tree view is realized
2840 * after it has been resized.
2841 */
2842 OS.gtk_widget_realize (handle);
2843 /*
2844 * Bug in GTK. An empty GtkTreeView fails to repaint the focus rectangle
2845 * correctly when resized on versions before 2.6.0. The fix is to force
2846 * the widget to redraw.
2847 */
2848 if (OS.GTK_VERSION < OS.buildVERSION (2, 6, 0) && OS.gtk_tree_model_iter_n_children (modelHandle, null) is 0) {
2849 redraw (false);
2850 }
2851 return result;
2852 }
2853
2854 /**
2855 * Sets the order that the items in the receiver should
2856 * be displayed in to the given argument which is described
2857 * in terms of the zero-relative ordering of when the items
2858 * were added.
2859 *
2860 * @param order the new order to display the items
2861 *
2862 * @exception SWTException <ul>
2863 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2864 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2865 * </ul>
2866 * @exception IllegalArgumentException <ul>
2867 * <li>ERROR_INVALID_ARGUMENT - if the item order is not the same length as the number of items</li>
2868 * </ul>
2869 *
2870 * @see Tree#getColumnOrder()
2871 * @see TreeColumn#getMoveable()
2872 * @see TreeColumn#setMoveable(bool)
2873 * @see SWT#Move
2874 *
2875 * @since 3.2
2876 */
2877 public void setColumnOrder (int [] order) {
2878 checkWidget ();
2879 // SWT extension: allow null for zero length string
2880 //if (order is null) error (SWT.ERROR_NULL_ARGUMENT);
2881 if (columnCount is 0) {
2882 if (order.length > 0) error (SWT.ERROR_INVALID_ARGUMENT);
2883 return;
2884 }
2885 if (order.length !is columnCount) error (SWT.ERROR_INVALID_ARGUMENT);
2886 bool [] seen = new bool [columnCount];
2887 for (int i = 0; i<order.length; i++) {
2888 int index = order [i];
2889 if (index < 0 || index >= columnCount) error (SWT.ERROR_INVALID_RANGE);
2890 if (seen [index]) error (SWT.ERROR_INVALID_ARGUMENT);
2891 seen [index] = true;
2892 }
2893 void* baseColumn;
2894 for (int i=0; i<order.length; i++) {
2895 auto column = columns [order [i]].handle;
2896 OS.gtk_tree_view_move_column_after (handle, column, baseColumn);
2897 baseColumn = column;
2898 }
2899 }
2900
2901 override void setFontDescription (PangoFontDescription* font) {
2902 super.setFontDescription (font);
2903 TreeColumn[] columns = getColumns ();
2904 for (int i = 0; i < columns.length; i++) {
2905 if (columns[i] !is null) {
2906 columns[i].setFontDescription (font);
2907 }
2908 }
2909 }
2910
2911 /**
2912 * Marks the receiver's header as visible if the argument is <code>true</code>,
2913 * and marks it invisible otherwise.
2914 * <p>
2915 * If one of the receiver's ancestors is not visible or some
2916 * other condition makes the receiver not visible, marking
2917 * it visible may not actually cause it to be displayed.
2918 * </p>
2919 *
2920 * @param show the new visibility state
2921 *
2922 * @exception SWTException <ul>
2923 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2924 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2925 * </ul>
2926 *
2927 * @since 3.1
2928 */
2929 public void setHeaderVisible (bool show) {
2930 checkWidget ();
2931 OS.gtk_tree_view_set_headers_visible (handle, show);
2932 }
2933
2934 /**
2935 * Marks the receiver's lines as visible if the argument is <code>true</code>,
2936 * and marks it invisible otherwise.
2937 * <p>
2938 * If one of the receiver's ancestors is not visible or some
2939 * other condition makes the receiver not visible, marking
2940 * it visible may not actually cause it to be displayed.
2941 * </p>
2942 *
2943 * @param show the new visibility state
2944 *
2945 * @exception SWTException <ul>
2946 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2947 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2948 * </ul>
2949 *
2950 * @since 3.1
2951 */
2952 public void setLinesVisible (bool show) {
2953 checkWidget();
2954 OS.gtk_tree_view_set_rules_hint (handle, show);
2955 }
2956
2957 override void setParentBackground () {
2958 super.setParentBackground ();
2959 auto window = paintWindow ();
2960 if (window !is null) OS.gdk_window_set_back_pixmap (window, null, true);
2961 }
2962
2963 override void setParentWindow (GtkWidget* widget) {
2964 auto window = eventWindow ();
2965 OS.gtk_widget_set_parent_window (widget, window);
2966 }
2967
2968 void setScrollWidth (GtkTreeViewColumn* column, TreeItem item) {
2969 if (columnCount !is 0 || currentItem is item) return;
2970 /*
2971 * Use GTK_TREE_VIEW_COLUMN_GROW_ONLY on GTK versions < 2.3.2
2972 * because fixed_height_mode is not supported.
2973 */
2974 if (((style & SWT.VIRTUAL) !is 0) && OS.GTK_VERSION < OS.buildVERSION (2, 3, 2)) return;
2975 int width = OS.gtk_tree_view_column_get_fixed_width (column);
2976 int itemWidth = calculateWidth (column, cast(GtkTreeIter*)item.handle, true);
2977 if (width < itemWidth) {
2978 OS.gtk_tree_view_column_set_fixed_width (column, itemWidth);
2979 }
2980 }
2981
2982 /**
2983 * Sets the receiver's selection to the given item.
2984 * The current selection is cleared before the new item is selected.
2985 * <p>
2986 * If the item is not in the receiver, then it is ignored.
2987 * </p>
2988 *
2989 * @param item the item to select
2990 *
2991 * @exception IllegalArgumentException <ul>
2992 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
2993 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
2994 * </ul>
2995 * @exception SWTException <ul>
2996 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2997 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2998 * </ul>
2999 *
3000 * @since 3.2
3001 */
3002 public void setSelection (TreeItem item) {
3003 checkWidget ();
3004 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
3005 setSelection ([item]);
3006 }
3007
3008 /**
3009 * Sets the receiver's selection to be the given array of items.
3010 * The current selection is cleared before the new items are selected.
3011 * <p>
3012 * Items that are not in the receiver are ignored.
3013 * If the receiver is single-select and multiple items are specified,
3014 * then all items are ignored.
3015 * </p>
3016 *
3017 * @param items the array of items
3018 *
3019 * @exception IllegalArgumentException <ul>
3020 * <li>ERROR_INVALID_ARGUMENT - if one of the items has been disposed</li>
3021 * </ul>
3022 * @exception SWTException <ul>
3023 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3024 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3025 * </ul>
3026 *
3027 * @see Tree#deselectAll()
3028 */
3029 public void setSelection (TreeItem [] items) {
3030 checkWidget ();
3031 // SWT extension: allow null for zero length string
3032 //if (items is null) error (SWT.ERROR_NULL_ARGUMENT);
3033 deselectAll ();
3034 int length = items.length;
3035 if (length is 0 || ((style & SWT.SINGLE) !is 0 && length > 1)) return;
3036 bool fixColumn = showFirstColumn ();
3037 auto selection = OS.gtk_tree_view_get_selection (handle);
3038 OS.g_signal_handlers_block_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
3039 bool first = true;
3040 for (int i = 0; i < length; i++) {
3041 TreeItem item = items [i];
3042 if (item is null) continue;
3043 if (item.isDisposed ()) break;
3044 if (item.parent !is this) continue;
3045 auto path = OS.gtk_tree_model_get_path (modelHandle, item.handle);
3046 showItem (path, false);
3047 if (first) {
3048 OS.gtk_tree_view_set_cursor (handle, path, null, false);
3049 }
3050 OS.gtk_tree_selection_select_iter (selection, item.handle);
3051 OS.gtk_tree_path_free (path);
3052 first = false;
3053 }
3054 OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
3055 if (fixColumn) hideFirstColumn ();
3056 }
3057
3058 /**
3059 * Sets the column used by the sort indicator for the receiver. A null
3060 * value will clear the sort indicator. The current sort column is cleared
3061 * before the new column is set.
3062 *
3063 * @param column the column used by the sort indicator or <code>null</code>
3064 *
3065 * @exception IllegalArgumentException <ul>
3066 * <li>ERROR_INVALID_ARGUMENT - if the column is disposed</li>
3067 * </ul>
3068 * @exception SWTException <ul>
3069 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3070 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3071 * </ul>
3072 *
3073 * @since 3.2
3074 */
3075 public void setSortColumn (TreeColumn column) {
3076 checkWidget ();
3077 if (column !is null && column.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
3078 if (sortColumn !is null && !sortColumn.isDisposed()) {
3079 OS.gtk_tree_view_column_set_sort_indicator (sortColumn.handle, false);
3080 }
3081 sortColumn = column;
3082 if (sortColumn !is null && sortDirection !is SWT.NONE) {
3083 OS.gtk_tree_view_column_set_sort_indicator (sortColumn.handle, true);
3084 OS.gtk_tree_view_column_set_sort_order (sortColumn.handle, sortDirection is SWT.DOWN ? 0 : 1);
3085 }
3086 }
3087
3088 /**
3089 * Sets the direction of the sort indicator for the receiver. The value
3090 * can be one of <code>UP</code>, <code>DOWN</code> or <code>NONE</code>.
3091 *
3092 * @param direction the direction of the sort indicator
3093 *
3094 * @exception SWTException <ul>
3095 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3096 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3097 * </ul>
3098 *
3099 * @since 3.2
3100 */
3101 public void setSortDirection (int direction) {
3102 checkWidget ();
3103 if (direction !is SWT.UP && direction !is SWT.DOWN && direction !is SWT.NONE) return;
3104 sortDirection = direction;
3105 if (sortColumn is null || sortColumn.isDisposed ()) return;
3106 if (sortDirection is SWT.NONE) {
3107 OS.gtk_tree_view_column_set_sort_indicator (sortColumn.handle, false);
3108 } else {
3109 OS.gtk_tree_view_column_set_sort_indicator (sortColumn.handle, true);
3110 OS.gtk_tree_view_column_set_sort_order (sortColumn.handle, sortDirection is SWT.DOWN ? 0 : 1);
3111 }
3112 }
3113
3114 /**
3115 * Sets the item which is currently at the top of the receiver.
3116 * This item can change when items are expanded, collapsed, scrolled
3117 * or new items are added or removed.
3118 *
3119 * @param item the item to be shown
3120 *
3121 * @exception IllegalArgumentException <ul>
3122 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
3123 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
3124 * </ul>
3125 * @exception SWTException <ul>
3126 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3127 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3128 * </ul>
3129 *
3130 * @see Tree#getTopItem()
3131 *
3132 * @since 2.1
3133 */
3134 public void setTopItem (TreeItem item) {
3135 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
3136 if (item.isDisposed ()) error(SWT.ERROR_INVALID_ARGUMENT);
3137 if (item.parent !is this) return;
3138 auto path = OS.gtk_tree_model_get_path (modelHandle, item.handle);
3139 showItem (path, false);
3140 OS.gtk_tree_view_scroll_to_cell (handle, path, null, true, 0f, 0f);
3141 if (OS.GTK_VERSION < OS.buildVERSION (2, 8, 0)) {
3142 /*
3143 * Bug in GTK. According to the documentation, gtk_tree_view_scroll_to_cell
3144 * should vertically scroll the cell to the top if use_align is true and row_align is 0.
3145 * However, prior to version 2.8 it does not scroll at all. The fix is to determine
3146 * the new location and use gtk_tree_view_scroll_to_point.
3147 * If the widget is a pinhead, calling gtk_tree_view_scroll_to_point
3148 * will have no effect. Therefore, it is still neccessary to call
3149 * gtk_tree_view_scroll_to_cell.
3150 */
3151 OS.gtk_widget_realize (handle);
3152 GdkRectangle cellRect;
3153 OS.gtk_tree_view_get_cell_area (handle, path, null, &cellRect);
3154 int tx, ty;
3155 OS.gtk_tree_view_widget_to_tree_coords(handle, cellRect.x, cellRect.y, &tx, &ty);
3156 OS.gtk_tree_view_scroll_to_point (handle, -1, ty);
3157 }
3158 OS.gtk_tree_path_free (path);
3159 }
3160
3161 /**
3162 * Shows the column. If the column is already showing in the receiver,
3163 * this method simply returns. Otherwise, the columns are scrolled until
3164 * the column is visible.
3165 *
3166 * @param column the column to be shown
3167 *
3168 * @exception IllegalArgumentException <ul>
3169 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
3170 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
3171 * </ul>
3172 * @exception SWTException <ul>
3173 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3174 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3175 * </ul>
3176 *
3177 * @since 3.1
3178 */
3179 public void showColumn (TreeColumn column) {
3180 checkWidget ();
3181 if (column is null) error (SWT.ERROR_NULL_ARGUMENT);
3182 if (column.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
3183 if (column.parent !is this) return;
3184 /*
3185 * This code is intentionally commented. According to the
3186 * documentation, gtk_tree_view_scroll_to_cell should scroll the
3187 * minimum amount to show the column but instead it scrolls strangely.
3188 */
3189 //OS.gtk_tree_view_scroll_to_cell (handle, 0, column.handle, false, 0, 0);
3190 OS.gtk_widget_realize (handle);
3191 GdkRectangle cellRect;
3192 OS.gtk_tree_view_get_cell_area (handle, null, column.handle, &cellRect);
3193 GdkRectangle visibleRect;
3194 OS.gtk_tree_view_get_visible_rect (handle, &visibleRect);
3195 if (cellRect.x < visibleRect.x) {
3196 OS.gtk_tree_view_scroll_to_point (handle, cellRect.x, -1);
3197 } else {
3198 int width = Math.min (visibleRect.width, cellRect.width);
3199 if (cellRect.x + width > visibleRect.x + visibleRect.width) {
3200 int tree_x = cellRect.x + width - visibleRect.width;
3201 OS.gtk_tree_view_scroll_to_point (handle, tree_x, -1);
3202 }
3203 }
3204 }
3205
3206 bool showFirstColumn () {
3207 /*
3208 * Bug in GTK. If no columns are visible, changing the selection
3209 * will fail. The fix is to temporarily make a column visible.
3210 */
3211 int columnCount = Math.max (1, this.columnCount);
3212 for (int i=0; i<columnCount; i++) {
3213 auto column = OS.gtk_tree_view_get_column (handle, i);
3214 if (OS.gtk_tree_view_column_get_visible (column)) return false;
3215 }
3216 auto firstColumn = OS.gtk_tree_view_get_column (handle, 0);
3217 OS.gtk_tree_view_column_set_visible (firstColumn, true);
3218 return true;
3219 }
3220
3221 /**
3222 * Shows the selection. If the selection is already showing in the receiver,
3223 * this method simply returns. Otherwise, the items are scrolled until
3224 * the selection is visible.
3225 *
3226 * @exception SWTException <ul>
3227 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3228 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3229 * </ul>
3230 *
3231 * @see Tree#showItem(TreeItem)
3232 */
3233 public void showSelection () {
3234 checkWidget();
3235 TreeItem [] items = getSelection ();
3236 if (items.length !is 0 && items [0] !is null) showItem (items [0]);
3237 }
3238
3239 void showItem (GtkTreePath* path, bool scroll) {
3240 int depth = OS.gtk_tree_path_get_depth (path);
3241 if (depth > 1) {
3242 auto indicesPtr = OS.gtk_tree_path_get_indices (path);
3243 int [] indices = indicesPtr[ 0 .. depth - 1];
3244 auto tempPath = OS.gtk_tree_path_new ();
3245 for (int i=0; i<indices.length; i++) {
3246 OS.gtk_tree_path_append_index (tempPath, indices [i]);
3247 OS.gtk_tree_view_expand_row (handle, tempPath, false);
3248 }
3249 OS.gtk_tree_path_free (tempPath);
3250 }
3251 if (scroll) {
3252 OS.gtk_widget_realize (handle);
3253 GdkRectangle cellRect;
3254 OS.gtk_tree_view_get_cell_area (handle, path, null, &cellRect);
3255 bool isHidden = cellRect.y is 0 && cellRect.height is 0;
3256 int tx, ty;
3257 OS.gtk_tree_view_widget_to_tree_coords (handle, cellRect.x, cellRect.y, &tx, &ty);
3258 GdkRectangle visibleRect;
3259 OS.gtk_tree_view_get_visible_rect (handle, &visibleRect);
3260 if (!isHidden) {
3261 if (ty < visibleRect.y || ty + cellRect.height > visibleRect.y + visibleRect.height) {
3262 isHidden = true;
3263 }
3264 }
3265 if (isHidden) {
3266 /*
3267 * This code intentionally commented.
3268 * Bug in GTK. According to the documentation, gtk_tree_view_scroll_to_cell
3269 * should scroll the minimum amount to show the cell if use_align is false.
3270 * However, what actually happens is the cell is scrolled to the top.
3271 * The fix is to determine the new location and use gtk_tree_view_scroll_to_point.
3272 * If the widget is a pinhead, calling gtk_tree_view_scroll_to_point
3273 * will have no effect. Therefore, it is still neccessary to
3274 * call gtk_tree_view_scroll_to_cell.
3275 */
3276 // OS.gtk_tree_view_scroll_to_cell (handle, path, 0, depth !is 1, 0.5f, 0.0f);
3277 if (depth !is 1) {
3278 OS.gtk_tree_view_scroll_to_cell (handle, path, null, true, 0.5f, 0.0f);
3279 } else {
3280 if (ty < visibleRect.y ) {
3281 OS.gtk_tree_view_scroll_to_point (handle, -1, ty);
3282 } else {
3283 int height = Math.min (visibleRect.height, cellRect.height);
3284 if (ty + height > visibleRect.y + visibleRect.height) {
3285 OS.gtk_tree_view_scroll_to_point (handle, -1, ty + cellRect.height - visibleRect.height);
3286 }
3287 }
3288 }
3289 }
3290 }
3291 }
3292
3293 /**
3294 * Shows the item. If the item is already showing in the receiver,
3295 * this method simply returns. Otherwise, the items are scrolled
3296 * and expanded until the item is visible.
3297 *
3298 * @param item the item to be shown
3299 *
3300 * @exception IllegalArgumentException <ul>
3301 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
3302 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
3303 * </ul>
3304 * @exception SWTException <ul>
3305 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3306 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3307 * </ul>
3308 *
3309 * @see Tree#showSelection()
3310 */
3311 public void showItem (TreeItem item) {
3312 checkWidget ();
3313 if (item is null) error (SWT.ERROR_NULL_ARGUMENT);
3314 if (item.isDisposed ()) error(SWT.ERROR_INVALID_ARGUMENT);
3315 if (item.parent !is this) return;
3316 auto path = OS.gtk_tree_model_get_path (modelHandle, item.handle);
3317 showItem (path, true);
3318 OS.gtk_tree_path_free (path);
3319 }
3320
3321 override void treeSelectionProc (
3322 GtkTreeModel *model,
3323 GtkTreePath *path,
3324 GtkTreeIter *iter,
3325 int[] selection,
3326 int length_)
3327 {
3328 if (selection !is null) {
3329 int index;
3330 OS.gtk_tree_model_get1 (modelHandle, iter, ID_COLUMN, cast(void**)&index);
3331 selection [length_] = index;
3332 }
3333 return 0;
3334 }
3335
3336 override void updateScrollBarValue (ScrollBar bar) {
3337 super.updateScrollBarValue (bar);
3338 /*
3339 * Bug in GTK. Scrolling changes the XWindow position
3340 * and makes the child widgets appear to scroll even
3341 * though when queried their position is unchanged.
3342 * The fix is to queue a resize event for each child to
3343 * force the position to be corrected.
3344 */
3345 auto parentHandle = parentingHandle ();
3346 auto list = OS.gtk_container_get_children (parentHandle);
3347 if (list is null) return;
3348 auto temp = list;
3349 while (temp !is null) {
3350 auto widget = OS.g_list_data (temp);
3351 if (widget !is null) OS.gtk_widget_queue_resize (widget);
3352 temp = OS.g_list_next (temp);
3353 }
3354 OS.g_list_free (list);
3355 }
3356
3357 }