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.TextTagTable;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.BuildableIF;
32 private import gtk.BuildableT;
33 private import gtk.TextTag;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * You may wish to begin by reading the
42  * [text widget conceptual overview][TextWidget]
43  * which gives an overview of all the objects and
44  * data types related to the text widget and how they work together.
45  * 
46  * # GtkTextTagTables as GtkBuildable
47  * 
48  * The GtkTextTagTable implementation of the GtkBuildable interface
49  * supports adding tags by specifying “tag” as the “type” attribute
50  * of a <child> element.
51  * 
52  * An example of a UI definition fragment specifying tags:
53  * |[
54  * <object class="GtkTextTagTable">
55  * <child type="tag">
56  * <object class="GtkTextTag"/>
57  * </child>
58  * </object>
59  * ]|
60  */
61 public class TextTagTable : ObjectG, BuildableIF
62 {
63 	/** the main Gtk struct */
64 	protected GtkTextTagTable* gtkTextTagTable;
65 
66 	/** Get the main Gtk struct */
67 	public GtkTextTagTable* getTextTagTableStruct(bool transferOwnership = false)
68 	{
69 		if (transferOwnership)
70 			ownedRef = false;
71 		return gtkTextTagTable;
72 	}
73 
74 	/** the main Gtk struct as a void* */
75 	protected override void* getStruct()
76 	{
77 		return cast(void*)gtkTextTagTable;
78 	}
79 
80 	protected override void setStruct(GObject* obj)
81 	{
82 		gtkTextTagTable = cast(GtkTextTagTable*)obj;
83 		super.setStruct(obj);
84 	}
85 
86 	/**
87 	 * Sets our main struct and passes it to the parent class.
88 	 */
89 	public this (GtkTextTagTable* gtkTextTagTable, bool ownedRef = false)
90 	{
91 		this.gtkTextTagTable = gtkTextTagTable;
92 		super(cast(GObject*)gtkTextTagTable, ownedRef);
93 	}
94 
95 	// add the Buildable capabilities
96 	mixin BuildableT!(GtkTextTagTable);
97 
98 
99 	/** */
100 	public static GType getType()
101 	{
102 		return gtk_text_tag_table_get_type();
103 	}
104 
105 	/**
106 	 * Creates a new #GtkTextTagTable. The table contains no tags by
107 	 * default.
108 	 *
109 	 * Returns: a new #GtkTextTagTable
110 	 *
111 	 * Throws: ConstructionException GTK+ fails to create the object.
112 	 */
113 	public this()
114 	{
115 		auto p = gtk_text_tag_table_new();
116 
117 		if(p is null)
118 		{
119 			throw new ConstructionException("null returned by new");
120 		}
121 
122 		this(cast(GtkTextTagTable*) p, true);
123 	}
124 
125 	/**
126 	 * Add a tag to the table. The tag is assigned the highest priority
127 	 * in the table.
128 	 *
129 	 * @tag must not be in a tag table already, and may not have
130 	 * the same name as an already-added tag.
131 	 *
132 	 * Params:
133 	 *     tag = a #GtkTextTag
134 	 *
135 	 * Returns: %TRUE on success.
136 	 */
137 	public bool add(TextTag tag)
138 	{
139 		return gtk_text_tag_table_add(gtkTextTagTable, (tag is null) ? null : tag.getTextTagStruct()) != 0;
140 	}
141 
142 	/**
143 	 * Calls @func on each tag in @table, with user data @data.
144 	 * Note that the table may not be modified while iterating
145 	 * over it (you can’t add/remove tags).
146 	 *
147 	 * Params:
148 	 *     func = a function to call on each tag
149 	 *     data = user data
150 	 */
151 	public void foreac(GtkTextTagTableForeach func, void* data)
152 	{
153 		gtk_text_tag_table_foreach(gtkTextTagTable, func, data);
154 	}
155 
156 	/**
157 	 * Returns the size of the table (number of tags)
158 	 *
159 	 * Returns: number of tags in @table
160 	 */
161 	public int getSize()
162 	{
163 		return gtk_text_tag_table_get_size(gtkTextTagTable);
164 	}
165 
166 	/**
167 	 * Look up a named tag.
168 	 *
169 	 * Params:
170 	 *     name = name of a tag
171 	 *
172 	 * Returns: The tag, or %NULL if none by that
173 	 *     name is in the table.
174 	 */
175 	public TextTag lookup(string name)
176 	{
177 		auto p = gtk_text_tag_table_lookup(gtkTextTagTable, Str.toStringz(name));
178 
179 		if(p is null)
180 		{
181 			return null;
182 		}
183 
184 		return ObjectG.getDObject!(TextTag)(cast(GtkTextTag*) p);
185 	}
186 
187 	/**
188 	 * Remove a tag from the table. If a #GtkTextBuffer has @table as its tag table,
189 	 * the tag is removed from the buffer. The table’s reference to the tag is
190 	 * removed, so the tag will end up destroyed if you don’t have a reference to
191 	 * it.
192 	 *
193 	 * Params:
194 	 *     tag = a #GtkTextTag
195 	 */
196 	public void remove(TextTag tag)
197 	{
198 		gtk_text_tag_table_remove(gtkTextTagTable, (tag is null) ? null : tag.getTextTagStruct());
199 	}
200 
201 	protected class OnTagAddedDelegateWrapper
202 	{
203 		void delegate(TextTag, TextTagTable) dlg;
204 		gulong handlerId;
205 
206 		this(void delegate(TextTag, TextTagTable) dlg)
207 		{
208 			this.dlg = dlg;
209 			onTagAddedListeners ~= this;
210 		}
211 
212 		void remove(OnTagAddedDelegateWrapper source)
213 		{
214 			foreach(index, wrapper; onTagAddedListeners)
215 			{
216 				if (wrapper.handlerId == source.handlerId)
217 				{
218 					onTagAddedListeners[index] = null;
219 					onTagAddedListeners = std.algorithm.remove(onTagAddedListeners, index);
220 					break;
221 				}
222 			}
223 		}
224 	}
225 	OnTagAddedDelegateWrapper[] onTagAddedListeners;
226 
227 	/** */
228 	gulong addOnTagAdded(void delegate(TextTag, TextTagTable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
229 	{
230 		auto wrapper = new OnTagAddedDelegateWrapper(dlg);
231 		wrapper.handlerId = Signals.connectData(
232 			this,
233 			"tag-added",
234 			cast(GCallback)&callBackTagAdded,
235 			cast(void*)wrapper,
236 			cast(GClosureNotify)&callBackTagAddedDestroy,
237 			connectFlags);
238 		return wrapper.handlerId;
239 	}
240 
241 	extern(C) static void callBackTagAdded(GtkTextTagTable* texttagtableStruct, GtkTextTag* tag, OnTagAddedDelegateWrapper wrapper)
242 	{
243 		wrapper.dlg(ObjectG.getDObject!(TextTag)(tag), wrapper.outer);
244 	}
245 
246 	extern(C) static void callBackTagAddedDestroy(OnTagAddedDelegateWrapper wrapper, GClosure* closure)
247 	{
248 		wrapper.remove(wrapper);
249 	}
250 
251 	protected class OnTagChangedDelegateWrapper
252 	{
253 		void delegate(TextTag, bool, TextTagTable) dlg;
254 		gulong handlerId;
255 
256 		this(void delegate(TextTag, bool, TextTagTable) dlg)
257 		{
258 			this.dlg = dlg;
259 			onTagChangedListeners ~= this;
260 		}
261 
262 		void remove(OnTagChangedDelegateWrapper source)
263 		{
264 			foreach(index, wrapper; onTagChangedListeners)
265 			{
266 				if (wrapper.handlerId == source.handlerId)
267 				{
268 					onTagChangedListeners[index] = null;
269 					onTagChangedListeners = std.algorithm.remove(onTagChangedListeners, index);
270 					break;
271 				}
272 			}
273 		}
274 	}
275 	OnTagChangedDelegateWrapper[] onTagChangedListeners;
276 
277 	/** */
278 	gulong addOnTagChanged(void delegate(TextTag, bool, TextTagTable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
279 	{
280 		auto wrapper = new OnTagChangedDelegateWrapper(dlg);
281 		wrapper.handlerId = Signals.connectData(
282 			this,
283 			"tag-changed",
284 			cast(GCallback)&callBackTagChanged,
285 			cast(void*)wrapper,
286 			cast(GClosureNotify)&callBackTagChangedDestroy,
287 			connectFlags);
288 		return wrapper.handlerId;
289 	}
290 
291 	extern(C) static void callBackTagChanged(GtkTextTagTable* texttagtableStruct, GtkTextTag* tag, bool sizeChanged, OnTagChangedDelegateWrapper wrapper)
292 	{
293 		wrapper.dlg(ObjectG.getDObject!(TextTag)(tag), sizeChanged, wrapper.outer);
294 	}
295 
296 	extern(C) static void callBackTagChangedDestroy(OnTagChangedDelegateWrapper wrapper, GClosure* closure)
297 	{
298 		wrapper.remove(wrapper);
299 	}
300 
301 	protected class OnTagRemovedDelegateWrapper
302 	{
303 		void delegate(TextTag, TextTagTable) dlg;
304 		gulong handlerId;
305 
306 		this(void delegate(TextTag, TextTagTable) dlg)
307 		{
308 			this.dlg = dlg;
309 			onTagRemovedListeners ~= this;
310 		}
311 
312 		void remove(OnTagRemovedDelegateWrapper source)
313 		{
314 			foreach(index, wrapper; onTagRemovedListeners)
315 			{
316 				if (wrapper.handlerId == source.handlerId)
317 				{
318 					onTagRemovedListeners[index] = null;
319 					onTagRemovedListeners = std.algorithm.remove(onTagRemovedListeners, index);
320 					break;
321 				}
322 			}
323 		}
324 	}
325 	OnTagRemovedDelegateWrapper[] onTagRemovedListeners;
326 
327 	/** */
328 	gulong addOnTagRemoved(void delegate(TextTag, TextTagTable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
329 	{
330 		auto wrapper = new OnTagRemovedDelegateWrapper(dlg);
331 		wrapper.handlerId = Signals.connectData(
332 			this,
333 			"tag-removed",
334 			cast(GCallback)&callBackTagRemoved,
335 			cast(void*)wrapper,
336 			cast(GClosureNotify)&callBackTagRemovedDestroy,
337 			connectFlags);
338 		return wrapper.handlerId;
339 	}
340 
341 	extern(C) static void callBackTagRemoved(GtkTextTagTable* texttagtableStruct, GtkTextTag* tag, OnTagRemovedDelegateWrapper wrapper)
342 	{
343 		wrapper.dlg(ObjectG.getDObject!(TextTag)(tag), wrapper.outer);
344 	}
345 
346 	extern(C) static void callBackTagRemovedDestroy(OnTagRemovedDelegateWrapper wrapper, GClosure* closure)
347 	{
348 		wrapper.remove(wrapper);
349 	}
350 }