3 * Copyright (C) 2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
23 #include "font-combo-menu.h"
25 #include <glib/gi18n.h>
28 #include "font-combo-menu-item.h"
29 #include "font-util.h"
30 #include "font-history.h"
34 /*===========================================*/
35 /* Private macros and constants. */
36 /*===========================================*/
39 /*===========================================*/
41 /*===========================================*/
43 struct _glFontComboMenuPrivate {
47 GtkWidget *recent_menu_item;
48 GtkWidget *recent_sub_menu;
57 gchar *standard_families[] = { "Sans", "Serif", "Monospace", NULL };
59 /*===========================================*/
61 /*===========================================*/
63 static guint signals[LAST_SIGNAL] = {0};
66 /*===========================================*/
67 /* Local function prototypes */
68 /*===========================================*/
70 static void gl_font_combo_menu_finalize (GObject *object);
72 static void menu_item_activate_cb (glFontComboMenuItem *item,
73 glFontComboMenu *this);
75 static GtkWidget *new_font_sub_menu (glFontComboMenu *this,
78 static void font_history_changed_cb (glFontComboMenu *this);
82 /****************************************************************************/
83 /* Boilerplate Object stuff. */
84 /****************************************************************************/
85 G_DEFINE_TYPE (glFontComboMenu, gl_font_combo_menu, GTK_TYPE_MENU)
88 /*****************************************************************************/
89 /* Class Init Function. */
90 /*****************************************************************************/
92 gl_font_combo_menu_class_init (glFontComboMenuClass *class)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
96 gl_font_combo_menu_parent_class = g_type_class_peek_parent (class);
98 gobject_class->finalize = gl_font_combo_menu_finalize;
100 signals[FONT_CHANGED] =
101 g_signal_new ("font_changed",
102 G_OBJECT_CLASS_TYPE (gobject_class),
104 G_STRUCT_OFFSET (glFontComboMenuClass, font_changed),
106 gl_marshal_VOID__VOID,
111 /*****************************************************************************/
112 /* Object Instance Init Function. */
113 /*****************************************************************************/
115 gl_font_combo_menu_init (glFontComboMenu *this)
118 GtkWidget *menu_item;
122 this->priv = g_new0 (glFontComboMenuPrivate, 1);
125 for ( i = 0; standard_families[i] != NULL; i++ )
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);
133 menu_item = gtk_separator_menu_item_new ();
134 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
137 menu_item = gtk_menu_item_new_with_label (_("Recent fonts"));
138 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
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);
145 this->priv->recent_menu_item = menu_item;
146 this->priv->recent_sub_menu = sub_menu;
148 menu_item = gtk_separator_menu_item_new ();
149 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
152 menu_item = gtk_menu_item_new_with_label (_("Proportional fonts"));
153 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
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);
160 menu_item = gtk_menu_item_new_with_label (_("Fixed-width fonts"));
161 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
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);
168 menu_item = gtk_menu_item_new_with_label (_("All fonts"));
169 gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item);
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);
177 gtk_widget_show_all (GTK_WIDGET (this));
180 g_signal_connect_swapped (gl_font_history, "changed",
181 G_CALLBACK (font_history_changed_cb), this);
186 /*****************************************************************************/
187 /* Finalize Method. */
188 /*****************************************************************************/
190 gl_font_combo_menu_finalize (GObject *object)
192 glFontComboMenu *this = GL_FONT_COMBO_MENU (object);
194 g_return_if_fail (object != NULL);
195 g_return_if_fail (GL_IS_FONT_COMBO_MENU (object));
199 G_OBJECT_CLASS (gl_font_combo_menu_parent_class)->finalize (object);
203 /*****************************************************************************/
204 /** New Object Generator. */
205 /*****************************************************************************/
207 gl_font_combo_menu_new (void)
209 glFontComboMenu *this;
211 this = g_object_new (gl_font_combo_menu_get_type (), NULL);
213 return GTK_WIDGET (this);
217 /*****************************************************************************/
218 /* menu_item activate callback. */
219 /*****************************************************************************/
220 static void menu_item_activate_cb (glFontComboMenuItem *item,
221 glFontComboMenu *this)
223 this->priv->font_family = gl_font_combo_menu_item_get_family (item);
225 g_signal_emit (this, signals[FONT_CHANGED], 0);
227 gtk_widget_hide (GTK_WIDGET (this));
231 /*****************************************************************************/
232 /* Get font family name. */
233 /*****************************************************************************/
235 gl_font_combo_menu_get_family (glFontComboMenu *this)
237 return g_strdup (this->priv->font_family);
241 /*****************************************************************************/
242 /* Create a new font sub menu from font list. */
243 /*****************************************************************************/
245 new_font_sub_menu (glFontComboMenu *this,
249 GtkWidget *menu_item;
252 menu = gtk_menu_new ();
254 for ( p = (GList *)list; p != NULL; p = p->next )
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);
263 gtk_widget_show (menu);
268 /*****************************************************************************/
269 /* Font history changed callback. */
270 /*****************************************************************************/
272 font_history_changed_cb (glFontComboMenu *this)
277 * Remove old sub menu
279 gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item),
285 list = gl_font_history_model_get_family_list (gl_font_history);
286 this->priv->recent_sub_menu = new_font_sub_menu (this, list);
289 * Attach to top-level menu item
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);
295 gl_font_history_model_free_family_list (list);
302 * Local Variables: -- emacs
304 * c-basic-offset: 8 -- emacs
305 * tab-width: 8 -- emacs
306 * indent-tabs-mode: nil -- emacs