1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.TreeNode;
20 
21 private import dazzle.Tree;
22 private import dazzle.c.functions;
23 public  import dazzle.c.types;
24 private import gdk.RGBA;
25 private import gio.IconIF;
26 private import glib.ConstructionException;
27 private import glib.Str;
28 private import gobject.ObjectG;
29 private import gtk.Popover;
30 private import gtk.TreeIter;
31 private import gtk.TreePath;
32 
33 
34 /** */
35 public class TreeNode : ObjectG
36 {
37 	/** the main Gtk struct */
38 	protected DzlTreeNode* dzlTreeNode;
39 
40 	/** Get the main Gtk struct */
41 	public DzlTreeNode* getTreeNodeStruct(bool transferOwnership = false)
42 	{
43 		if (transferOwnership)
44 			ownedRef = false;
45 		return dzlTreeNode;
46 	}
47 
48 	/** the main Gtk struct as a void* */
49 	protected override void* getStruct()
50 	{
51 		return cast(void*)dzlTreeNode;
52 	}
53 
54 	/**
55 	 * Sets our main struct and passes it to the parent class.
56 	 */
57 	public this (DzlTreeNode* dzlTreeNode, bool ownedRef = false)
58 	{
59 		this.dzlTreeNode = dzlTreeNode;
60 		super(cast(GObject*)dzlTreeNode, ownedRef);
61 	}
62 
63 
64 	/** */
65 	public static GType getType()
66 	{
67 		return dzl_tree_node_get_type();
68 	}
69 
70 	/**
71 	 * Creates a new #DzlTreeNode instance. This is handy for situations where you
72 	 * do not want to subclass #DzlTreeNode.
73 	 *
74 	 * Returns: A #DzlTreeNode
75 	 *
76 	 * Throws: ConstructionException GTK+ fails to create the object.
77 	 */
78 	public this()
79 	{
80 		auto p = dzl_tree_node_new();
81 
82 		if(p is null)
83 		{
84 			throw new ConstructionException("null returned by new");
85 		}
86 
87 		this(cast(DzlTreeNode*) p, true);
88 	}
89 
90 	/**
91 	 * Adds an emplem to be rendered on top of the node.
92 	 *
93 	 * Use dzl_tree_node_remove_emblem() to remove an emblem.
94 	 *
95 	 * Params:
96 	 *     emblemName = the icon-name of the emblem
97 	 */
98 	public void addEmblem(string emblemName)
99 	{
100 		dzl_tree_node_add_emblem(dzlTreeNode, Str.toStringz(emblemName));
101 	}
102 
103 	/**
104 	 * Appends @child to the list of children owned by @node.
105 	 *
106 	 * Params:
107 	 *     child = A #DzlTreeNode.
108 	 */
109 	public void append(TreeNode child)
110 	{
111 		dzl_tree_node_append(dzlTreeNode, (child is null) ? null : child.getTreeNodeStruct());
112 	}
113 
114 	/**
115 	 * Removes all emblems from @self.
116 	 */
117 	public void clearEmblems()
118 	{
119 		dzl_tree_node_clear_emblems(dzlTreeNode);
120 	}
121 
122 	/** */
123 	public void collapse()
124 	{
125 		dzl_tree_node_collapse(dzlTreeNode);
126 	}
127 
128 	/** */
129 	public bool expand(bool expandAncestors)
130 	{
131 		return dzl_tree_node_expand(dzlTreeNode, expandAncestors) != 0;
132 	}
133 
134 	/** */
135 	public void getArea(GdkRectangle* area)
136 	{
137 		dzl_tree_node_get_area(dzlTreeNode, area);
138 	}
139 
140 	/** */
141 	public bool getChildrenPossible()
142 	{
143 		return dzl_tree_node_get_children_possible(dzlTreeNode) != 0;
144 	}
145 
146 	/** */
147 	public bool getExpanded()
148 	{
149 		return dzl_tree_node_get_expanded(dzlTreeNode) != 0;
150 	}
151 
152 	/**
153 	 * Gets the foreground-rgba to use for row text.
154 	 *
155 	 * If %NULL, the default foreground color should be used.
156 	 *
157 	 * Returns: A #GdkRGBA or %NULL
158 	 *
159 	 * Since: 3.28
160 	 */
161 	public RGBA getForegroundRgba()
162 	{
163 		auto p = dzl_tree_node_get_foreground_rgba(dzlTreeNode);
164 
165 		if(p is null)
166 		{
167 			return null;
168 		}
169 
170 		return ObjectG.getDObject!(RGBA)(cast(GdkRGBA*) p);
171 	}
172 
173 	/**
174 	 * Fetch the GIcon, re-render if necessary
175 	 *
176 	 * Returns: An #GIcon or %NULL.
177 	 */
178 	public IconIF getGicon()
179 	{
180 		auto p = dzl_tree_node_get_gicon(dzlTreeNode);
181 
182 		if(p is null)
183 		{
184 			return null;
185 		}
186 
187 		return ObjectG.getDObject!(IconIF)(cast(GIcon*) p);
188 	}
189 
190 	/**
191 	 * Fetches the icon-name of the icon to display, or NULL for no icon.
192 	 */
193 	public string getIconName()
194 	{
195 		return Str.toString(dzl_tree_node_get_icon_name(dzlTreeNode));
196 	}
197 
198 	/**
199 	 * Gets a #GObject for the node, if one was set.
200 	 *
201 	 * Returns: A #GObject or %NULL.
202 	 */
203 	public ObjectG getItem()
204 	{
205 		auto p = dzl_tree_node_get_item(dzlTreeNode);
206 
207 		if(p is null)
208 		{
209 			return null;
210 		}
211 
212 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p);
213 	}
214 
215 	/** */
216 	public bool getIter(TreeIter iter)
217 	{
218 		return dzl_tree_node_get_iter(dzlTreeNode, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
219 	}
220 
221 	/**
222 	 * Retrieves the parent #DzlTreeNode for @node.
223 	 *
224 	 * Returns: A #DzlTreeNode.
225 	 */
226 	public TreeNode getParent()
227 	{
228 		auto p = dzl_tree_node_get_parent(dzlTreeNode);
229 
230 		if(p is null)
231 		{
232 			return null;
233 		}
234 
235 		return ObjectG.getDObject!(TreeNode)(cast(DzlTreeNode*) p);
236 	}
237 
238 	/**
239 	 * Gets a #GtkTreePath for @node.
240 	 *
241 	 * Returns: A #GtkTreePath if successful; otherwise %NULL.
242 	 */
243 	public TreePath getPath()
244 	{
245 		auto p = dzl_tree_node_get_path(dzlTreeNode);
246 
247 		if(p is null)
248 		{
249 			return null;
250 		}
251 
252 		return ObjectG.getDObject!(TreePath)(cast(GtkTreePath*) p, true);
253 	}
254 
255 	/** */
256 	public bool getResetOnCollapse()
257 	{
258 		return dzl_tree_node_get_reset_on_collapse(dzlTreeNode) != 0;
259 	}
260 
261 	/** */
262 	public string getText()
263 	{
264 		return Str.toString(dzl_tree_node_get_text(dzlTreeNode));
265 	}
266 
267 	/**
268 	 * Fetches the #DzlTree instance that owns the node.
269 	 *
270 	 * Returns: A #DzlTree.
271 	 */
272 	public Tree getTree()
273 	{
274 		auto p = dzl_tree_node_get_tree(dzlTreeNode);
275 
276 		if(p is null)
277 		{
278 			return null;
279 		}
280 
281 		return ObjectG.getDObject!(Tree)(cast(DzlTree*) p);
282 	}
283 
284 	/** */
285 	public bool getUseDimLabel()
286 	{
287 		return dzl_tree_node_get_use_dim_label(dzlTreeNode) != 0;
288 	}
289 
290 	/** */
291 	public bool getUseMarkup()
292 	{
293 		return dzl_tree_node_get_use_markup(dzlTreeNode) != 0;
294 	}
295 
296 	/**
297 	 * Checks to see if @emblem_name has been added to the #DzlTreeNode.
298 	 *
299 	 * Params:
300 	 *     emblemName = a string containing the emblem name
301 	 *
302 	 * Returns: %TRUE if @emblem_name is used by @self
303 	 */
304 	public bool hasEmblem(string emblemName)
305 	{
306 		return dzl_tree_node_has_emblem(dzlTreeNode, Str.toStringz(emblemName)) != 0;
307 	}
308 
309 	/**
310 	 * Inserts @child as a child of @self at @position.
311 	 *
312 	 * Params:
313 	 *     child = a #DzlTreeNode
314 	 *     position = the position for the child
315 	 *
316 	 * Since: 3.28
317 	 */
318 	public void insert(TreeNode child, uint position)
319 	{
320 		dzl_tree_node_insert(dzlTreeNode, (child is null) ? null : child.getTreeNodeStruct(), position);
321 	}
322 
323 	/**
324 	 * Inserts a @child as a child of @node, sorting it among the other children.
325 	 *
326 	 * Params:
327 	 *     child = A #DzlTreeNode.
328 	 *     compareFunc = A compare func to compare nodes.
329 	 *     userData = user data for @compare_func.
330 	 */
331 	public void insertSorted(TreeNode child, DzlTreeNodeCompareFunc compareFunc, void* userData)
332 	{
333 		dzl_tree_node_insert_sorted(dzlTreeNode, (child is null) ? null : child.getTreeNodeStruct(), compareFunc, userData);
334 	}
335 
336 	/** */
337 	public void invalidate()
338 	{
339 		dzl_tree_node_invalidate(dzlTreeNode);
340 	}
341 
342 	/** */
343 	public bool isRoot()
344 	{
345 		return dzl_tree_node_is_root(dzlTreeNode) != 0;
346 	}
347 
348 	/** */
349 	public uint nChildren()
350 	{
351 		return dzl_tree_node_n_children(dzlTreeNode);
352 	}
353 
354 	/**
355 	 * Gets the @nth child of @self or %NULL if it does not exist.
356 	 *
357 	 * Params:
358 	 *     nth = the index of the child
359 	 *
360 	 * Returns: a #DzlTreeNode or %NULL
361 	 */
362 	public TreeNode nthChild(uint nth)
363 	{
364 		auto p = dzl_tree_node_nth_child(dzlTreeNode, nth);
365 
366 		if(p is null)
367 		{
368 			return null;
369 		}
370 
371 		return ObjectG.getDObject!(TreeNode)(cast(DzlTreeNode*) p, true);
372 	}
373 
374 	/**
375 	 * Prepends @child to the list of children owned by @node.
376 	 *
377 	 * Params:
378 	 *     child = A #DzlTreeNode.
379 	 */
380 	public void prepend(TreeNode child)
381 	{
382 		dzl_tree_node_prepend(dzlTreeNode, (child is null) ? null : child.getTreeNodeStruct());
383 	}
384 
385 	/**
386 	 * Rebuilds a node, without invalidating children nodes. If you want to
387 	 * ensure that children are also rebuilt, use dzl_tree_node_invalidate().
388 	 *
389 	 * Since: 3.28
390 	 */
391 	public void rebuild()
392 	{
393 		dzl_tree_node_rebuild(dzlTreeNode);
394 	}
395 
396 	/**
397 	 * Removes @child from the list of children owned by @node.
398 	 *
399 	 * Params:
400 	 *     child = A #DzlTreeNode.
401 	 */
402 	public void remove(TreeNode child)
403 	{
404 		dzl_tree_node_remove(dzlTreeNode, (child is null) ? null : child.getTreeNodeStruct());
405 	}
406 
407 	/** */
408 	public void removeEmblem(string emblemName)
409 	{
410 		dzl_tree_node_remove_emblem(dzlTreeNode, Str.toStringz(emblemName));
411 	}
412 
413 	/** */
414 	public void select()
415 	{
416 		dzl_tree_node_select(dzlTreeNode);
417 	}
418 
419 	/**
420 	 * If the node has not yet been built, setting this to %TRUE will add a
421 	 * dummy child node. This dummy node will be removed when when the node
422 	 * is built by the registered #DzlTreeBuilder instances.
423 	 *
424 	 * Params:
425 	 *     childrenPossible = If the node has children.
426 	 */
427 	public void setChildrenPossible(bool childrenPossible)
428 	{
429 		dzl_tree_node_set_children_possible(dzlTreeNode, childrenPossible);
430 	}
431 
432 	/** */
433 	public void setEmblems(string emblems)
434 	{
435 		dzl_tree_node_set_emblems(dzlTreeNode, Str.toStringz(emblems));
436 	}
437 
438 	/**
439 	 * Sets the foreground-rgba to be used by the row text.
440 	 *
441 	 * If @foreground_rgba is %NULL, the value is reset to the default.
442 	 *
443 	 * Params:
444 	 *     foregroundRgba = A #GdkRGBA or %NULL
445 	 *
446 	 * Since: 3.28
447 	 */
448 	public void setForegroundRgba(RGBA foregroundRgba)
449 	{
450 		dzl_tree_node_set_foreground_rgba(dzlTreeNode, (foregroundRgba is null) ? null : foregroundRgba.getRGBAStruct());
451 	}
452 
453 	/** */
454 	public void setGicon(IconIF icon)
455 	{
456 		dzl_tree_node_set_gicon(dzlTreeNode, (icon is null) ? null : icon.getIconStruct());
457 	}
458 
459 	/**
460 	 * Sets the icon name of the node. This is displayed in the pixbuf
461 	 * cell of the DzlTree.
462 	 *
463 	 * Params:
464 	 *     iconName = The icon name.
465 	 */
466 	public void setIconName(string iconName)
467 	{
468 		dzl_tree_node_set_icon_name(dzlTreeNode, Str.toStringz(iconName));
469 	}
470 
471 	/**
472 	 * An optional object to associate with the node. This is handy to save needing
473 	 * to subclass the #DzlTreeNode class.
474 	 *
475 	 * Params:
476 	 *     item = A #GObject.
477 	 */
478 	public void setItem(ObjectG item)
479 	{
480 		dzl_tree_node_set_item(dzlTreeNode, (item is null) ? null : item.getObjectGStruct());
481 	}
482 
483 	/** */
484 	public void setResetOnCollapse(bool resetOnCollapse)
485 	{
486 		dzl_tree_node_set_reset_on_collapse(dzlTreeNode, resetOnCollapse);
487 	}
488 
489 	/**
490 	 * Sets the text of the node. This is displayed in the text
491 	 * cell of the DzlTree.
492 	 *
493 	 * Params:
494 	 *     text = The node text.
495 	 */
496 	public void setText(string text)
497 	{
498 		dzl_tree_node_set_text(dzlTreeNode, Str.toStringz(text));
499 	}
500 
501 	/** */
502 	public void setUseDimLabel(bool useDimLabel)
503 	{
504 		dzl_tree_node_set_use_dim_label(dzlTreeNode, useDimLabel);
505 	}
506 
507 	/** */
508 	public void setUseMarkup(bool useMarkup)
509 	{
510 		dzl_tree_node_set_use_markup(dzlTreeNode, useMarkup);
511 	}
512 
513 	/** */
514 	public void showPopover(Popover popover)
515 	{
516 		dzl_tree_node_show_popover(dzlTreeNode, (popover is null) ? null : popover.getPopoverStruct());
517 	}
518 }