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