]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkfontchooser.c
1 /* GTK - The GIMP Toolkit
2  * gtkfontchooser.c - Abstract interface for font file selectors GUIs
3  *
4  * Copyright (C) 2006, Emmanuele Bassi
5  * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22
23 #include "gtkfontchooser.h"
24 #include "gtkfontchooserprivate.h"
25 #include "gtkintl.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkprivate.h"
28
29 /**
30  * SECTION:gtkfontchooser
31  * @Short_description: Interface implemented by widgets displaying fonts
32  * @Title: GtkFontChooser
33  * @See_also: #GtkFontChooserDialog, #GtkFontChooserWidget, #GtkFontButton
34  *
35  * #GtkFontChooser is an interface that can be implemented by widgets
36  * displaying the list of fonts.  In GTK+, the main objects
37  * that implement this interface are #GtkFontChooserWidget,
38  * #GtkFontChooserDialog and #GtkFontButton.
39  *
40  * Since: 3.2
41  */
42
43 enum
44 {
45   SIGNAL_FONT_ACTIVATED,
46   LAST_SIGNAL
47 };
48
49 static guint chooser_signals[LAST_SIGNAL];
50
51 typedef GtkFontChooserIface GtkFontChooserInterface;
52 G_DEFINE_INTERFACE (GtkFontChooser, gtk_font_chooser, G_TYPE_OBJECT);
53
54 static void
55 gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
56 {
57   /**
58    * GtkFontChooser:font:
59    *
60    * The font description as a string, e.g. "Sans Italic 12".
61    */
62   g_object_interface_install_property
63      (iface,
64       g_param_spec_string ("font",
65                           P_("Font"),
66                            P_("Font description as a string, e.g. \"Sans Italic 12\""),
67                            GTK_FONT_CHOOSER_DEFAULT_FONT_NAME,
68                            GTK_PARAM_READWRITE));
69
70   /**
71    * GtkFontChooser:font-desc:
72    *
73    * The font description as a #PangoFontDescription.
74    */
75   g_object_interface_install_property
76      (iface,
77       g_param_spec_boxed ("font-desc",
78                           P_("Font description"),
79                           P_("Font description as a PangoFontDescription struct"),
80                           PANGO_TYPE_FONT_DESCRIPTION,
81                           GTK_PARAM_READWRITE));
82
83   /**
84    * GtkFontChooser:preview-text:
85    *
86    * The string with which to preview the font.
87    */
88   g_object_interface_install_property
89      (iface,
90       g_param_spec_string ("preview-text",
91                           P_("Preview text"),
92                           P_("The text to display in order to demonstrate the selected font"),
93                           pango_language_get_sample_string (NULL),
94                           GTK_PARAM_READWRITE));
95
96   /**
97    * GtkFontChooser:show-preview-entry:
98    *
99    * Whether to show an entry to change the preview text.
100    */
101   g_object_interface_install_property
102      (iface,
103       g_param_spec_boolean ("show-preview-entry",
104                           P_("Show preview text entry"),
105                           P_("Whether the preview text entry is shown or not"),
106                           TRUE,
107                           GTK_PARAM_READWRITE));
108
109   /**
110    * GtkFontChooser::font-activated:
111    * @self: the object which received the signal
112    * @fontname: the font name
113    *
114    * Emitted when a font is activated.
115    * This usually happens when the user double clicks an item,
116    * or an item is selected and the user presses one of the keys
117    * Space, Shift+Space, Return or Enter.
118     */
119   chooser_signals[SIGNAL_FONT_ACTIVATED] =
120     g_signal_new ("font-activated",
121                   GTK_TYPE_FONT_CHOOSER,
122                   G_SIGNAL_RUN_FIRST,
123                   G_STRUCT_OFFSET (GtkFontChooserIface, font_activated),
124                   NULL, NULL,
125                   NULL,
126                   G_TYPE_NONE,
127                   1, G_TYPE_STRING);
128 }
129
130 /**
131  * gtk_font_chooser_get_font_family:
132  * @fontchooser: a #GtkFontChooser
133  *
134  * Gets the #PangoFontFamily representing the selected font family.
135  * Font families are a collection of font faces.
136  *
137  * If the selected font is not installed, returns %NULL.
138  *
139  * Return value: (transfer none): A #PangoFontFamily representing the
140  *     selected font family, or %NULL. The returned object is owned by @fontchooser
141  *     and must not be modified or freed.
142  *
143  * Since: 3.2
144  */
145 PangoFontFamily *
146 gtk_font_chooser_get_font_family (GtkFontChooser *fontchooser)
147 {
148   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
149
150   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_family (fontchooser);
151 }
152
153 /**
154  * gtk_font_chooser_get_font_face:
155  * @fontchooser: a #GtkFontChooser
156  *
157  * Gets the #PangoFontFace representing the selected font group
158  * details (i.e. family, slant, weight, width, etc).
159  *
160  * If the selected font is not installed, returns %NULL.
161  *
162  * Return value: (transfer none): A #PangoFontFace representing the
163  *     selected font group details, or %NULL. The returned object is owned by
164  *     @fontchooser and must not be modified or freed.
165  *
166  * Since: 3.2
167  */
168 PangoFontFace *
169 gtk_font_chooser_get_font_face (GtkFontChooser *fontchooser)
170 {
171   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
172
173   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_face (fontchooser);
174 }
175
176 /**
177  * gtk_font_chooser_get_font_size:
178  * @fontchooser: a #GtkFontChooser
179  *
180  * The selected font size.
181  *
182  * Return value: A n integer representing the selected font size,
183  *     or -1 if no font size is selected.
184  *
185  * Since: 3.2
186  */
187 gint
188 gtk_font_chooser_get_font_size (GtkFontChooser *fontchooser)
189 {
190   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
191
192   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_size (fontchooser);
193 }
194
195 /**
196  * gtk_font_chooser_get_font:
197  * @fontchooser: a #GtkFontChooser
198  *
199  * Gets the currently-selected font name.
200  *
201  * Note that this can be a different string than what you set with
202  * gtk_font_chooser_set_font(), as the font chooser widget may
203  * normalize font names and thus return a string with a different
204  * structure. For example, "Helvetica Italic Bold 12" could be
205  * normalized to "Helvetica Bold Italic 12".
206  *
207  * Use pango_font_description_equal() if you want to compare two
208  * font descriptions.
209  *
210  * Return value: (transfer full) (allow-none): A string with the name
211  *     of the current font, or %NULL if  no font is selected. You must
212  *     free this string with g_free().
213  *
214  * Since: 3.2
215  */
216 gchar *
217 gtk_font_chooser_get_font (GtkFontChooser *fontchooser)
218 {
219   gchar *fontname;
220
221   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
222
223   g_object_get (fontchooser, "font", &fontname, NULL);
224
225
226   return fontname;
227 }
228
229 /**
230  * gtk_font_chooser_set_font:
231  * @fontchooser: a #GtkFontChooser
232  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
233  *
234  * Sets the currently-selected font.
235  *
236  * Since: 3.2
237  */
238 void
239 gtk_font_chooser_set_font (GtkFontChooser *fontchooser,
240                            const gchar    *fontname)
241 {
242   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
243   g_return_if_fail (fontname != NULL);
244
245   g_object_set (fontchooser, "font", fontname, NULL);
246 }
247
248 /**
249  * gtk_font_chooser_get_font_desc:
250  * @fontchooser: a #GtkFontChooser
251  *
252  * Gets the currently-selected font.
253  *
254  * Note that this can be a different string than what you set with
255  * gtk_font_chooser_set_font(), as the font chooser widget may
256  * normalize font names and thus return a string with a different
257  * structure. For example, "Helvetica Italic Bold 12" could be
258  * normalized to "Helvetica Bold Italic 12".
259  *
260  * Use pango_font_description_equal() if you want to compare two
261  * font descriptions.
262  *
263  * Return value: (transfer full) (allow-none): A #PangoFontDescription for the
264  *     current font, or %NULL if  no font is selected.
265  *
266  * Since: 3.2
267  */
268 PangoFontDescription *
269 gtk_font_chooser_get_font_desc (GtkFontChooser *fontchooser)
270 {
271   PangoFontDescription *font_desc;
272
273   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
274
275   g_object_get (fontchooser, "font-desc", &font_desc, NULL);
276
277   return font_desc;
278 }
279
280 /**
281  * gtk_font_chooser_set_font_desc:
282  * @fontchooser: a #GtkFontChooser
283  * @font_desc: a #PangoFontDescription
284  *
285  * Sets the currently-selected font from @font_desc.
286  *
287  * Since: 3.2
288  */
289 void
290 gtk_font_chooser_set_font_desc (GtkFontChooser             *fontchooser,
291                                 const PangoFontDescription *font_desc)
292 {
293   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
294   g_return_if_fail (font_desc != NULL);
295
296   g_object_set (fontchooser, "font-desc", font_desc, NULL);
297 }
298
299 /**
300  * gtk_font_chooser_get_preview_text:
301  * @fontchooser: a #GtkFontChooser
302  *
303  * Gets the text displayed in the preview area.
304  *
305  * Return value: (transfer full): the text displayed in the
306  *     preview area
307  *
308  * Since: 3.2
309  */
310 gchar *
311 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
312 {
313   char *text;
314
315   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
316
317   g_object_get (fontchooser, "preview-text", &text, NULL);
318
319   return text;
320 }
321
322 /**
323  * gtk_font_chooser_set_preview_text:
324  * @fontchooser: a #GtkFontChooser
325  * @text: (transfer none): the text to display in the preview area
326  *
327  * Sets the text displayed in the preview area.
328  * The @text is used to show how the selected font looks.
329  *
330  * Since: 3.2
331  */
332 void
333 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
334                                    const gchar    *text)
335 {
336   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
337   g_return_if_fail (text != NULL);
338
339   g_object_set (fontchooser, "preview-text", text, NULL);
340 }
341
342 /**
343  * gtk_font_chooser_get_show_preview_entry:
344  * @fontchooser: a #GtkFontChooser
345  *
346  * Returns whether the preview entry is shown or not.
347  *
348  * Return value: %TRUE if the preview entry is shown
349  *     or %FALSE if it is hidden.
350  *
351  * Since: 3.2
352  */
353 gboolean
354 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
355 {
356   gboolean show;
357
358   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
359
360   g_object_get (fontchooser, "show-preview-entry", &show, NULL);
361
362   return show;
363 }
364
365 /**
366  * gtk_font_chooser_set_show_preview_entry:
367  * @fontchooser: a #GtkFontChooser
368  * @show_preview_entry: whether to show the editable preview entry or not
369  *
370  * Shows or hides the editable preview entry.
371  *
372  * Since: 3.2
373  */
374 void
375 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
376                                          gboolean        show_preview_entry)
377 {
378   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
379
380   show_preview_entry = show_preview_entry != FALSE;
381   g_object_set (fontchooser, "show-preview-entry", show_preview_entry, NULL);
382 }
383
384 /**
385  * gtk_font_chooser_set_filter_func:
386  * @fontchooser: a #GtkFontChooser
387  * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
388  * @user_data: data to pass to @filter
389  * @destroy: function to call to free @data when it is no longer needed
390  *
391  * Adds a filter function that decides which fonts to display
392  * in the font chooser.
393  *
394  * Since: 3.2
395  */
396 void
397 gtk_font_chooser_set_filter_func (GtkFontChooser   *fontchooser,
398                                   GtkFontFilterFunc filter,
399                                   gpointer          user_data,
400                                   GDestroyNotify    destroy)
401 {
402   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
403
404   GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_filter_func (fontchooser,
405                                                              filter,
406                                                              user_data,
407                                                              destroy);
408 }
409
410 void
411 _gtk_font_chooser_font_activated (GtkFontChooser *chooser,
412                                   const gchar    *fontname)
413 {
414   g_return_if_fail (GTK_IS_FONT_CHOOSER (chooser));
415
416   g_signal_emit (chooser, chooser_signals[SIGNAL_FONT_ACTIVATED], 0, fontname);
417 }