]> git.sur5r.net Git - glabels/blob - src/font-combo-menu.c
Imported Upstream version 3.0.0
[glabels] / src / font-combo-menu.c
1 /*
2  *  font-combo-menu.c
3  *  Copyright (C) 2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels 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
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "font-combo-menu.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "font-combo-menu-item.h"
29 #include "font-util.h"
30 #include "font-history.h"
31 #include "marshal.h"
32
33
34 /*===========================================*/
35 /* Private macros and constants.             */
36 /*===========================================*/
37
38
39 /*===========================================*/
40 /* Private types                             */
41 /*===========================================*/
42
43 struct _glFontComboMenuPrivate {
44
45         gchar     *font_family;
46
47         GtkWidget *recent_menu_item;
48         GtkWidget *recent_sub_menu;
49
50 };
51
52 enum {
53         FONT_CHANGED,
54         LAST_SIGNAL
55 };
56
57 gchar *standard_families[] = { "Sans", "Serif", "Monospace", NULL };
58
59 /*===========================================*/
60 /* Private globals                           */
61 /*===========================================*/
62
63 static guint signals[LAST_SIGNAL] = {0};
64
65
66 /*===========================================*/
67 /* Local function prototypes                 */
68 /*===========================================*/
69
70 static void       gl_font_combo_menu_finalize (GObject             *object);
71
72 static void       menu_item_activate_cb       (glFontComboMenuItem *item,
73                                                glFontComboMenu     *this);
74
75 static GtkWidget *new_font_sub_menu           (glFontComboMenu     *this,
76                                                const GList         *list);
77
78 static void       font_history_changed_cb     (glFontComboMenu     *this);
79
80
81
82 /****************************************************************************/
83 /* Boilerplate Object stuff.                                                */
84 /****************************************************************************/
85 G_DEFINE_TYPE (glFontComboMenu, gl_font_combo_menu, GTK_TYPE_MENU)
86
87
88 /*****************************************************************************/
89 /* Class Init Function.                                                      */
90 /*****************************************************************************/
91 static void
92 gl_font_combo_menu_class_init (glFontComboMenuClass *class)
93 {
94         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
95
96         gl_font_combo_menu_parent_class = g_type_class_peek_parent (class);
97
98         gobject_class->finalize = gl_font_combo_menu_finalize;
99
100         signals[FONT_CHANGED] =
101                 g_signal_new ("font_changed",
102                               G_OBJECT_CLASS_TYPE (gobject_class),
103                               G_SIGNAL_RUN_LAST,
104                               G_STRUCT_OFFSET (glFontComboMenuClass, font_changed),
105                               NULL, NULL,
106                               gl_marshal_VOID__VOID,
107                               G_TYPE_NONE, 0);
108 }
109
110
111 /*****************************************************************************/
112 /* Object Instance Init Function.                                            */
113 /*****************************************************************************/
114 static void
115 gl_font_combo_menu_init (glFontComboMenu *this)
116 {
117         gint          i;
118         GtkWidget    *menu_item;
119         GtkWidget    *sub_menu;
120         const GList  *list;
121
122         this->priv = g_new0 (glFontComboMenuPrivate, 1);
123
124
125         for ( i = 0; standard_families[i] != NULL; i++ )
126         {
127                 menu_item = gl_font_combo_menu_item_new (standard_families[i]);
128                 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
129                 g_signal_connect (menu_item, "activate",
130                                   G_CALLBACK (menu_item_activate_cb), this);
131         }
132
133         menu_item = gtk_separator_menu_item_new ();
134         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
135
136
137         menu_item = gtk_menu_item_new_with_label (_("Recent fonts"));
138         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
139
140         list = gl_font_history_model_get_family_list (gl_font_history);
141         sub_menu = new_font_sub_menu (this, list);
142         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
143         gtk_widget_set_sensitive (menu_item, list != NULL);
144
145         this->priv->recent_menu_item = menu_item;
146         this->priv->recent_sub_menu  = sub_menu;
147
148         menu_item = gtk_separator_menu_item_new ();
149         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
150
151
152         menu_item = gtk_menu_item_new_with_label (_("Proportional fonts"));
153         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
154
155         list = gl_font_util_get_proportional_families ();
156         sub_menu = new_font_sub_menu (this, list);
157         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
158         gtk_widget_set_sensitive (menu_item, list != NULL);
159
160         menu_item = gtk_menu_item_new_with_label (_("Fixed-width fonts"));
161         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
162
163         list = gl_font_util_get_fixed_width_families ();
164         sub_menu = new_font_sub_menu (this, list);
165         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
166         gtk_widget_set_sensitive (menu_item, list != NULL);
167
168         menu_item = gtk_menu_item_new_with_label (_("All fonts"));
169         gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
170
171         list = gl_font_util_get_all_families ();
172         sub_menu = new_font_sub_menu (this, list);
173         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
174         gtk_widget_set_sensitive (menu_item, list != NULL);
175
176
177         gtk_widget_show_all (GTK_WIDGET (this));
178
179
180         g_signal_connect_swapped (gl_font_history, "changed",
181                                   G_CALLBACK (font_history_changed_cb), this);
182
183 }
184
185
186 /*****************************************************************************/
187 /* Finalize Method.                                                          */
188 /*****************************************************************************/
189 static void
190 gl_font_combo_menu_finalize (GObject *object)
191 {
192         glFontComboMenu *this = GL_FONT_COMBO_MENU (object);
193
194         g_return_if_fail (object != NULL);
195         g_return_if_fail (GL_IS_FONT_COMBO_MENU (object));
196
197         g_free (this->priv);
198
199         G_OBJECT_CLASS (gl_font_combo_menu_parent_class)->finalize (object);
200 }
201
202
203 /*****************************************************************************/
204 /** New Object Generator.                                                    */
205 /*****************************************************************************/
206 GtkWidget *
207 gl_font_combo_menu_new (void)
208 {
209         glFontComboMenu *this;
210
211         this = g_object_new (gl_font_combo_menu_get_type (), NULL);
212
213         return GTK_WIDGET (this);
214 }
215
216
217 /*****************************************************************************/
218 /* menu_item activate callback.                                              */
219 /*****************************************************************************/
220 static void menu_item_activate_cb (glFontComboMenuItem *item,
221                                    glFontComboMenu     *this)
222 {
223         this->priv->font_family = gl_font_combo_menu_item_get_family (item);
224
225         g_signal_emit (this, signals[FONT_CHANGED], 0);
226
227         gtk_widget_hide (GTK_WIDGET (this));
228 }
229
230
231 /*****************************************************************************/
232 /* Get font family name.                                                     */
233 /*****************************************************************************/
234 gchar *
235 gl_font_combo_menu_get_family (glFontComboMenu *this)
236 {
237         return g_strdup (this->priv->font_family);
238 }
239
240
241 /*****************************************************************************/
242 /* Create a new font sub menu from font list.                                */
243 /*****************************************************************************/
244 static GtkWidget *
245 new_font_sub_menu (glFontComboMenu *this,
246                    const GList     *list)
247 {
248         GtkWidget   *menu;
249         GtkWidget   *menu_item;
250         GList       *p;
251
252         menu = gtk_menu_new ();
253
254         for ( p = (GList *)list; p != NULL; p = p->next )
255         {
256                 menu_item = gl_font_combo_menu_item_new (p->data);
257                 gtk_widget_show_all (menu_item);
258                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
259                 g_signal_connect (menu_item, "activate",
260                                   G_CALLBACK (menu_item_activate_cb), this);
261         }
262
263         gtk_widget_show (menu);
264         return menu;
265 }
266
267
268 /*****************************************************************************/
269 /* Font history changed callback.                                            */
270 /*****************************************************************************/
271 static void
272 font_history_changed_cb (glFontComboMenu     *this)
273 {
274         GList *list;
275
276         /*
277          * Remove old sub menu
278          */
279         gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item),
280                                    NULL);
281
282         /*
283          * Build new sub menu
284          */
285         list = gl_font_history_model_get_family_list (gl_font_history);
286         this->priv->recent_sub_menu = new_font_sub_menu (this, list);
287
288         /*
289          * Attach to top-level menu item
290          */
291         gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item),
292                                    this->priv->recent_sub_menu);
293         gtk_widget_set_sensitive (this->priv->recent_menu_item, list != NULL);
294
295         gl_font_history_model_free_family_list (list);
296 }
297
298
299
300
301 /*
302  * Local Variables:       -- emacs
303  * mode: C                -- emacs
304  * c-basic-offset: 8      -- emacs
305  * tab-width: 8           -- emacs
306  * indent-tabs-mode: nil  -- emacs
307  * End:                   -- emacs
308  */