1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD 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  * gtkD 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 gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.TreeSortableT;
26 
27 public  import gobject.Signals;
28 public  import gtkc.gdktypes;
29 public  import gtkc.gtk;
30 public  import gtkc.gtktypes;
31 public  import std.algorithm;
32 
33 
34 /**
35  * #GtkTreeSortable is an interface to be implemented by tree models which
36  * support sorting. The #GtkTreeView uses the methods provided by this interface
37  * to sort the model.
38  */
39 public template TreeSortableT(TStruct)
40 {
41 	/** Get the main Gtk struct */
42 	public GtkTreeSortable* getTreeSortableStruct()
43 	{
44 		return cast(GtkTreeSortable*)getStruct();
45 	}
46 
47 
48 	/**
49 	 * Fills in @sort_column_id and @order with the current sort column and the
50 	 * order. It returns %TRUE unless the @sort_column_id is
51 	 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID or
52 	 * %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID.
53 	 *
54 	 * Params:
55 	 *     sortColumnId = The sort column id to be filled in
56 	 *     order = The #GtkSortType to be filled in
57 	 *
58 	 * Return: %TRUE if the sort column is not one of the special sort
59 	 *     column ids.
60 	 */
61 	public bool getSortColumnId(out int sortColumnId, out GtkSortType order)
62 	{
63 		return gtk_tree_sortable_get_sort_column_id(getTreeSortableStruct(), &sortColumnId, &order) != 0;
64 	}
65 
66 	/**
67 	 * Returns %TRUE if the model has a default sort function. This is used
68 	 * primarily by GtkTreeViewColumns in order to determine if a model can
69 	 * go back to the default state, or not.
70 	 *
71 	 * Return: %TRUE, if the model has a default sort function
72 	 */
73 	public bool hasDefaultSortFunc()
74 	{
75 		return gtk_tree_sortable_has_default_sort_func(getTreeSortableStruct()) != 0;
76 	}
77 
78 	/**
79 	 * Sets the default comparison function used when sorting to be @sort_func.
80 	 * If the current sort column id of @sortable is
81 	 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using
82 	 * this function.
83 	 *
84 	 * If @sort_func is %NULL, then there will be no default comparison function.
85 	 * This means that once the model  has been sorted, it can’t go back to the
86 	 * default state. In this case, when the current sort column id of @sortable
87 	 * is %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
88 	 *
89 	 * Params:
90 	 *     sortFunc = The comparison function
91 	 *     userData = User data to pass to @sort_func, or %NULL
92 	 *     destroy = Destroy notifier of @user_data, or %NULL
93 	 */
94 	public void setDefaultSortFunc(GtkTreeIterCompareFunc sortFunc, void* userData, GDestroyNotify destroy)
95 	{
96 		gtk_tree_sortable_set_default_sort_func(getTreeSortableStruct(), sortFunc, userData, destroy);
97 	}
98 
99 	/**
100 	 * Sets the current sort column to be @sort_column_id. The @sortable will
101 	 * resort itself to reflect this change, after emitting a
102 	 * #GtkTreeSortable::sort-column-changed signal. @sort_column_id may either be
103 	 * a regular column id, or one of the following special values:
104 	 *
105 	 * - %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID: the default sort function
106 	 * will be used, if it is set
107 	 *
108 	 * - %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID: no sorting will occur
109 	 *
110 	 * Params:
111 	 *     sortColumnId = the sort column id to set
112 	 *     order = The sort order of the column
113 	 */
114 	public void setSortColumnId(int sortColumnId, GtkSortType order)
115 	{
116 		gtk_tree_sortable_set_sort_column_id(getTreeSortableStruct(), sortColumnId, order);
117 	}
118 
119 	/**
120 	 * Sets the comparison function used when sorting to be @sort_func. If the
121 	 * current sort column id of @sortable is the same as @sort_column_id, then
122 	 * the model will sort using this function.
123 	 *
124 	 * Params:
125 	 *     sortColumnId = the sort column id to set the function for
126 	 *     sortFunc = The comparison function
127 	 *     userData = User data to pass to @sort_func, or %NULL
128 	 *     destroy = Destroy notifier of @user_data, or %NULL
129 	 */
130 	public void setSortFunc(int sortColumnId, GtkTreeIterCompareFunc sortFunc, void* userData, GDestroyNotify destroy)
131 	{
132 		gtk_tree_sortable_set_sort_func(getTreeSortableStruct(), sortColumnId, sortFunc, userData, destroy);
133 	}
134 
135 	/**
136 	 * Emits a #GtkTreeSortable::sort-column-changed signal on @sortable.
137 	 */
138 	public void sortColumnChanged()
139 	{
140 		gtk_tree_sortable_sort_column_changed(getTreeSortableStruct());
141 	}
142 
143 	protected class OnSortColumnChangedDelegateWrapper
144 	{
145 		void delegate(TreeSortableIF) dlg;
146 		gulong handlerId;
147 		ConnectFlags flags;
148 		this(void delegate(TreeSortableIF) dlg, gulong handlerId, ConnectFlags flags)
149 		{
150 			this.dlg = dlg;
151 			this.handlerId = handlerId;
152 			this.flags = flags;
153 		}
154 	}
155 	protected OnSortColumnChangedDelegateWrapper[] onSortColumnChangedListeners;
156 
157 	/**
158 	 * The ::sort-column-changed signal is emitted when the sort column
159 	 * or sort order of @sortable is changed. The signal is emitted before
160 	 * the contents of @sortable are resorted.
161 	 */
162 	gulong addOnSortColumnChanged(void delegate(TreeSortableIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
163 	{
164 		onSortColumnChangedListeners ~= new OnSortColumnChangedDelegateWrapper(dlg, 0, connectFlags);
165 		onSortColumnChangedListeners[onSortColumnChangedListeners.length - 1].handlerId = Signals.connectData(
166 			this,
167 			"sort-column-changed",
168 			cast(GCallback)&callBackSortColumnChanged,
169 			cast(void*)onSortColumnChangedListeners[onSortColumnChangedListeners.length - 1],
170 			cast(GClosureNotify)&callBackSortColumnChangedDestroy,
171 			connectFlags);
172 		return onSortColumnChangedListeners[onSortColumnChangedListeners.length - 1].handlerId;
173 	}
174 	
175 	extern(C) static void callBackSortColumnChanged(GtkTreeSortable* treesortableStruct,OnSortColumnChangedDelegateWrapper wrapper)
176 	{
177 		wrapper.dlg(wrapper.outer);
178 	}
179 	
180 	extern(C) static void callBackSortColumnChangedDestroy(OnSortColumnChangedDelegateWrapper wrapper, GClosure* closure)
181 	{
182 		wrapper.outer.internalRemoveOnSortColumnChanged(wrapper);
183 	}
184 
185 	protected void internalRemoveOnSortColumnChanged(OnSortColumnChangedDelegateWrapper source)
186 	{
187 		foreach(index, wrapper; onSortColumnChangedListeners)
188 		{
189 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
190 			{
191 				onSortColumnChangedListeners[index] = null;
192 				onSortColumnChangedListeners = std.algorithm.remove(onSortColumnChangedListeners, index);
193 				break;
194 			}
195 		}
196 	}
197 	
198 }