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