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.FontChooserT;
26 
27 public  import glib.Str;
28 public  import gobject.ObjectG;
29 public  import gobject.Signals;
30 public  import gtk.c.functions;
31 public  import gtk.c.types;
32 public  import gtkc.gtktypes;
33 public  import pango.PgFontDescription;
34 public  import pango.PgFontFace;
35 public  import pango.PgFontFamily;
36 public  import pango.PgFontMap;
37 public  import std.algorithm;
38 
39 
40 /**
41  * #GtkFontChooser is an interface that can be implemented by widgets
42  * displaying the list of fonts. In GTK+, the main objects
43  * that implement this interface are #GtkFontChooserWidget,
44  * #GtkFontChooserDialog and #GtkFontButton. The GtkFontChooser interface
45  * has been introducted in GTK+ 3.2.
46  */
47 public template FontChooserT(TStruct)
48 {
49 	/** Get the main Gtk struct */
50 	public GtkFontChooser* getFontChooserStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return cast(GtkFontChooser*)getStruct();
55 	}
56 
57 
58 	/**
59 	 * Gets the currently-selected font name.
60 	 *
61 	 * Note that this can be a different string than what you set with
62 	 * gtk_font_chooser_set_font(), as the font chooser widget may
63 	 * normalize font names and thus return a string with a different
64 	 * structure. For example, “Helvetica Italic Bold 12” could be
65 	 * normalized to “Helvetica Bold Italic 12”.
66 	 *
67 	 * Use pango_font_description_equal() if you want to compare two
68 	 * font descriptions.
69 	 *
70 	 * Returns: A string with the name
71 	 *     of the current font, or %NULL if  no font is selected. You must
72 	 *     free this string with g_free().
73 	 *
74 	 * Since: 3.2
75 	 */
76 	public string getFont()
77 	{
78 		auto retStr = gtk_font_chooser_get_font(getFontChooserStruct());
79 
80 		scope(exit) Str.freeString(retStr);
81 		return Str.toString(retStr);
82 	}
83 
84 	/**
85 	 * Gets the currently-selected font.
86 	 *
87 	 * Note that this can be a different string than what you set with
88 	 * gtk_font_chooser_set_font(), as the font chooser widget may
89 	 * normalize font names and thus return a string with a different
90 	 * structure. For example, “Helvetica Italic Bold 12” could be
91 	 * normalized to “Helvetica Bold Italic 12”.
92 	 *
93 	 * Use pango_font_description_equal() if you want to compare two
94 	 * font descriptions.
95 	 *
96 	 * Returns: A #PangoFontDescription for the
97 	 *     current font, or %NULL if  no font is selected.
98 	 *
99 	 * Since: 3.2
100 	 */
101 	public PgFontDescription getFontDesc()
102 	{
103 		auto p = gtk_font_chooser_get_font_desc(getFontChooserStruct());
104 
105 		if(p is null)
106 		{
107 			return null;
108 		}
109 
110 		return ObjectG.getDObject!(PgFontDescription)(cast(PangoFontDescription*) p, true);
111 	}
112 
113 	/**
114 	 * Gets the #PangoFontFace representing the selected font group
115 	 * details (i.e. family, slant, weight, width, etc).
116 	 *
117 	 * If the selected font is not installed, returns %NULL.
118 	 *
119 	 * Returns: A #PangoFontFace representing the
120 	 *     selected font group details, or %NULL. The returned object is owned by
121 	 *     @fontchooser and must not be modified or freed.
122 	 *
123 	 * Since: 3.2
124 	 */
125 	public PgFontFace getFontFace()
126 	{
127 		auto p = gtk_font_chooser_get_font_face(getFontChooserStruct());
128 
129 		if(p is null)
130 		{
131 			return null;
132 		}
133 
134 		return ObjectG.getDObject!(PgFontFace)(cast(PangoFontFace*) p);
135 	}
136 
137 	/**
138 	 * Gets the #PangoFontFamily representing the selected font family.
139 	 * Font families are a collection of font faces.
140 	 *
141 	 * If the selected font is not installed, returns %NULL.
142 	 *
143 	 * Returns: A #PangoFontFamily representing the
144 	 *     selected font family, or %NULL. The returned object is owned by @fontchooser
145 	 *     and must not be modified or freed.
146 	 *
147 	 * Since: 3.2
148 	 */
149 	public PgFontFamily getFontFamily()
150 	{
151 		auto p = gtk_font_chooser_get_font_family(getFontChooserStruct());
152 
153 		if(p is null)
154 		{
155 			return null;
156 		}
157 
158 		return ObjectG.getDObject!(PgFontFamily)(cast(PangoFontFamily*) p);
159 	}
160 
161 	/**
162 	 * Gets the custom font map of this font chooser widget,
163 	 * or %NULL if it does not have one.
164 	 *
165 	 * Returns: a #PangoFontMap, or %NULL
166 	 *
167 	 * Since: 3.18
168 	 */
169 	public override PgFontMap getFontMap()
170 	{
171 		auto p = gtk_font_chooser_get_font_map(getFontChooserStruct());
172 
173 		if(p is null)
174 		{
175 			return null;
176 		}
177 
178 		return ObjectG.getDObject!(PgFontMap)(cast(PangoFontMap*) p, true);
179 	}
180 
181 	/**
182 	 * The selected font size.
183 	 *
184 	 * Returns: A n integer representing the selected font size,
185 	 *     or -1 if no font size is selected.
186 	 *
187 	 * Since: 3.2
188 	 */
189 	public int getFontSize()
190 	{
191 		return gtk_font_chooser_get_font_size(getFontChooserStruct());
192 	}
193 
194 	/**
195 	 * Gets the text displayed in the preview area.
196 	 *
197 	 * Returns: the text displayed in the
198 	 *     preview area
199 	 *
200 	 * Since: 3.2
201 	 */
202 	public string getPreviewText()
203 	{
204 		auto retStr = gtk_font_chooser_get_preview_text(getFontChooserStruct());
205 
206 		scope(exit) Str.freeString(retStr);
207 		return Str.toString(retStr);
208 	}
209 
210 	/**
211 	 * Returns whether the preview entry is shown or not.
212 	 *
213 	 * Returns: %TRUE if the preview entry is shown
214 	 *     or %FALSE if it is hidden.
215 	 *
216 	 * Since: 3.2
217 	 */
218 	public bool getShowPreviewEntry()
219 	{
220 		return gtk_font_chooser_get_show_preview_entry(getFontChooserStruct()) != 0;
221 	}
222 
223 	/**
224 	 * Adds a filter function that decides which fonts to display
225 	 * in the font chooser.
226 	 *
227 	 * Params:
228 	 *     filter = a #GtkFontFilterFunc, or %NULL
229 	 *     userData = data to pass to @filter
230 	 *     destroy = function to call to free @data when it is no longer needed
231 	 *
232 	 * Since: 3.2
233 	 */
234 	public void setFilterFunc(GtkFontFilterFunc filter, void* userData, GDestroyNotify destroy)
235 	{
236 		gtk_font_chooser_set_filter_func(getFontChooserStruct(), filter, userData, destroy);
237 	}
238 
239 	/**
240 	 * Sets the currently-selected font.
241 	 *
242 	 * Params:
243 	 *     fontname = a font name like “Helvetica 12” or “Times Bold 18”
244 	 *
245 	 * Since: 3.2
246 	 */
247 	public void setFont(string fontname)
248 	{
249 		gtk_font_chooser_set_font(getFontChooserStruct(), Str.toStringz(fontname));
250 	}
251 
252 	/**
253 	 * Sets the currently-selected font from @font_desc.
254 	 *
255 	 * Params:
256 	 *     fontDesc = a #PangoFontDescription
257 	 *
258 	 * Since: 3.2
259 	 */
260 	public void setFontDesc(PgFontDescription fontDesc)
261 	{
262 		gtk_font_chooser_set_font_desc(getFontChooserStruct(), (fontDesc is null) ? null : fontDesc.getPgFontDescriptionStruct());
263 	}
264 
265 	/**
266 	 * Sets a custom font map to use for this font chooser widget.
267 	 * A custom font map can be used to present application-specific
268 	 * fonts instead of or in addition to the normal system fonts.
269 	 *
270 	 * |[<!-- language="C" -->
271 	 * FcConfig *config;
272 	 * PangoFontMap *fontmap;
273 	 *
274 	 * config = FcInitLoadConfigAndFonts ();
275 	 * FcConfigAppFontAddFile (config, my_app_font_file);
276 	 *
277 	 * fontmap = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
278 	 * pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (fontmap), config);
279 	 *
280 	 * gtk_font_chooser_set_font_map (font_chooser, fontmap);
281 	 * ]|
282 	 *
283 	 * Note that other GTK+ widgets will only be able to use the application-specific
284 	 * font if it is present in the font map they use:
285 	 *
286 	 * |[
287 	 * context = gtk_widget_get_pango_context (label);
288 	 * pango_context_set_font_map (context, fontmap);
289 	 * ]|
290 	 *
291 	 * Params:
292 	 *     fontmap = a #PangoFontMap
293 	 *
294 	 * Since: 3.18
295 	 */
296 	public override void setFontMap(PgFontMap fontmap)
297 	{
298 		gtk_font_chooser_set_font_map(getFontChooserStruct(), (fontmap is null) ? null : fontmap.getPgFontMapStruct());
299 	}
300 
301 	/**
302 	 * Sets the text displayed in the preview area.
303 	 * The @text is used to show how the selected font looks.
304 	 *
305 	 * Params:
306 	 *     text = the text to display in the preview area
307 	 *
308 	 * Since: 3.2
309 	 */
310 	public void setPreviewText(string text)
311 	{
312 		gtk_font_chooser_set_preview_text(getFontChooserStruct(), Str.toStringz(text));
313 	}
314 
315 	/**
316 	 * Shows or hides the editable preview entry.
317 	 *
318 	 * Params:
319 	 *     showPreviewEntry = whether to show the editable preview entry or not
320 	 *
321 	 * Since: 3.2
322 	 */
323 	public void setShowPreviewEntry(bool showPreviewEntry)
324 	{
325 		gtk_font_chooser_set_show_preview_entry(getFontChooserStruct(), showPreviewEntry);
326 	}
327 
328 	protected class OnFontActivatedDelegateWrapper
329 	{
330 		void delegate(string, FontChooserIF) dlg;
331 		gulong handlerId;
332 
333 		this(void delegate(string, FontChooserIF) dlg)
334 		{
335 			this.dlg = dlg;
336 			onFontActivatedListeners ~= this;
337 		}
338 
339 		void remove(OnFontActivatedDelegateWrapper source)
340 		{
341 			foreach(index, wrapper; onFontActivatedListeners)
342 			{
343 				if (wrapper.handlerId == source.handlerId)
344 				{
345 					onFontActivatedListeners[index] = null;
346 					onFontActivatedListeners = std.algorithm.remove(onFontActivatedListeners, index);
347 					break;
348 				}
349 			}
350 		}
351 	}
352 	OnFontActivatedDelegateWrapper[] onFontActivatedListeners;
353 
354 	/**
355 	 * Emitted when a font is activated.
356 	 * This usually happens when the user double clicks an item,
357 	 * or an item is selected and the user presses one of the keys
358 	 * Space, Shift+Space, Return or Enter.
359 	 *
360 	 * Params:
361 	 *     fontname = the font name
362 	 */
363 	gulong addOnFontActivated(void delegate(string, FontChooserIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
364 	{
365 		auto wrapper = new OnFontActivatedDelegateWrapper(dlg);
366 		wrapper.handlerId = Signals.connectData(
367 			this,
368 			"font-activated",
369 			cast(GCallback)&callBackFontActivated,
370 			cast(void*)wrapper,
371 			cast(GClosureNotify)&callBackFontActivatedDestroy,
372 			connectFlags);
373 		return wrapper.handlerId;
374 	}
375 
376 	extern(C) static void callBackFontActivated(GtkFontChooser* fontchooserStruct, char* fontname, OnFontActivatedDelegateWrapper wrapper)
377 	{
378 		wrapper.dlg(Str.toString(fontname), wrapper.outer);
379 	}
380 
381 	extern(C) static void callBackFontActivatedDestroy(OnFontActivatedDelegateWrapper wrapper, GClosure* closure)
382 	{
383 		wrapper.remove(wrapper);
384 	}
385 }