From: Jim Evins Date: Fri, 23 Oct 2009 03:00:38 +0000 (-0400) Subject: Added font history X-Git-Tag: glabels-2_3_0~153 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=b0a92a3f0b9855585fb7fe856483d2e8cece64f7;p=glabels Added font history Added font history object. Setting/changing fonts in text objects updates this object. The glFontCombMenu tracks this object to keep its recent fonts submenu up-to-date. --- diff --git a/po/POTFILES.in b/po/POTFILES.in index e0c51d34..a134b2aa 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -52,6 +52,10 @@ src/font-combo-menu.c src/font-combo-menu.h src/font-combo-menu-item.c src/font-combo-menu-item.h +src/font-history.c +src/font-history.h +src/font-history-model.c +src/font-history-model.h src/font-sample.c src/font-sample.h src/font-util.c diff --git a/src/Makefile.am b/src/Makefile.am index 6008dee8..b851e3df 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -205,6 +205,10 @@ glabels_SOURCES = \ font-sample.h \ font-util.c \ font-util.h \ + font-history.c \ + font-history.h \ + font-history-model.c \ + font-history-model.h \ cairo-label-path.c \ cairo-label-path.h \ cairo-markup-path.c \ @@ -282,6 +286,10 @@ glabels_batch_SOURCES = \ prefs-model.h \ font-util.c \ font-util.h \ + font-history.c \ + font-history.h \ + font-history-model.c \ + font-history-model.h \ str-util.c \ str-util.h \ color.c \ diff --git a/src/font-combo-menu.c b/src/font-combo-menu.c index 34c93979..43190032 100644 --- a/src/font-combo-menu.c +++ b/src/font-combo-menu.c @@ -27,6 +27,7 @@ #include "font-combo-menu-item.h" #include "font-util.h" +#include "font-history.h" #include "marshal.h" @@ -41,7 +42,10 @@ struct _glFontComboMenuPrivate { - gchar *font_family; + gchar *font_family; + + GtkWidget *recent_menu_item; + GtkWidget *recent_sub_menu; }; @@ -63,10 +67,16 @@ static guint signals[LAST_SIGNAL] = {0}; /* Local function prototypes */ /*===========================================*/ -static void gl_font_combo_menu_finalize (GObject *object); +static void gl_font_combo_menu_finalize (GObject *object); + +static void menu_item_activate_cb (glFontComboMenuItem *item, + glFontComboMenu *this); + +static GtkWidget *new_font_sub_menu (glFontComboMenu *this, + const GList *list); + +static void font_history_changed_cb (glFontComboMenu *this); -static void menu_item_activate_cb (glFontComboMenuItem *item, - glFontComboMenu *this); /****************************************************************************/ @@ -124,50 +134,52 @@ gl_font_combo_menu_init (glFontComboMenu *this) menu_item = gtk_separator_menu_item_new (); gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); - menu_item = gtk_menu_item_new_with_label (_("Proportional fonts")); + + menu_item = gtk_menu_item_new_with_label (_("Recent fonts")); gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); - sub_menu = gtk_menu_new (); + list = gl_font_history_model_get_family_list (gl_font_history); + sub_menu = new_font_sub_menu (this, list); gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu); + gtk_widget_set_sensitive (menu_item, list != NULL); - list = gl_font_util_get_proportional_families (); - for ( p = (GList *)list; p != NULL; p = p->next ) - { - menu_item = gl_font_combo_menu_item_new (p->data); - gtk_menu_shell_append (GTK_MENU_SHELL (sub_menu), menu_item); - g_signal_connect (menu_item, "activate", - G_CALLBACK (menu_item_activate_cb), this); - } + this->priv->recent_menu_item = menu_item; + this->priv->recent_sub_menu = sub_menu; - menu_item = gtk_menu_item_new_with_label (_("Fixed-width fonts")); + menu_item = gtk_separator_menu_item_new (); + gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); + + + menu_item = gtk_menu_item_new_with_label (_("Proportional fonts")); gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); - sub_menu = gtk_menu_new (); + list = gl_font_util_get_proportional_families (); + sub_menu = new_font_sub_menu (this, list); gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu); + gtk_widget_set_sensitive (menu_item, list != NULL); + + menu_item = gtk_menu_item_new_with_label (_("Fixed-width fonts")); + gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); list = gl_font_util_get_fixed_width_families (); - for ( p = (GList *)list; p != NULL; p = p->next ) - { - menu_item = gl_font_combo_menu_item_new (p->data); - gtk_menu_shell_append (GTK_MENU_SHELL (sub_menu), menu_item); - g_signal_connect (menu_item, "activate", - G_CALLBACK (menu_item_activate_cb), this); - } + sub_menu = new_font_sub_menu (this, list); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu); + gtk_widget_set_sensitive (menu_item, list != NULL); menu_item = gtk_menu_item_new_with_label (_("All fonts")); gtk_menu_shell_append (GTK_MENU_SHELL (this), menu_item); - sub_menu = gtk_menu_new (); + list = gl_font_util_get_all_families (); + sub_menu = new_font_sub_menu (this, list); gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu); + gtk_widget_set_sensitive (menu_item, list != NULL); - list = gl_font_util_get_all_families (); - for ( p = (GList *)list; p != NULL; p = p->next ) - { - menu_item = gl_font_combo_menu_item_new (p->data); - gtk_menu_shell_append (GTK_MENU_SHELL (sub_menu), menu_item); - g_signal_connect (menu_item, "activate", - G_CALLBACK (menu_item_activate_cb), this); - } + + gtk_widget_show_all (GTK_WIDGET (this)); + + + g_signal_connect_swapped (gl_font_history, "changed", + G_CALLBACK (font_history_changed_cb), this); } @@ -227,6 +239,62 @@ gl_font_combo_menu_get_family (glFontComboMenu *this) } +/*****************************************************************************/ +/* Create a new font sub menu from font list. */ +/*****************************************************************************/ +static GtkWidget * +new_font_sub_menu (glFontComboMenu *this, + const GList *list) +{ + GtkWidget *menu; + GtkWidget *menu_item; + GList *p; + + menu = gtk_menu_new (); + + for ( p = (GList *)list; p != NULL; p = p->next ) + { + menu_item = gl_font_combo_menu_item_new (p->data); + gtk_widget_show_all (menu_item); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item); + g_signal_connect (menu_item, "activate", + G_CALLBACK (menu_item_activate_cb), this); + } + + gtk_widget_show (menu); + return menu; +} + + +/*****************************************************************************/ +/* Font history changed callback. */ +/*****************************************************************************/ +static void +font_history_changed_cb (glFontComboMenu *this) +{ + const GList *list; + + /* + * Remove old sub menu + */ + gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item), + NULL); + + /* + * Build new sub menu + */ + list = gl_font_history_model_get_family_list (gl_font_history); + this->priv->recent_sub_menu = new_font_sub_menu (this, list); + + /* + * Attach to top-level menu item + */ + gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item), + this->priv->recent_sub_menu); + gtk_widget_set_sensitive (this->priv->recent_menu_item, list != NULL); +} + + /* diff --git a/src/font-history-model.c b/src/font-history-model.c new file mode 100644 index 00000000..c7390b2f --- /dev/null +++ b/src/font-history-model.c @@ -0,0 +1,204 @@ +/* + * font-history-model.c + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels. If not, see . + */ + +#include + +#include "font-history-model.h" + +#include +#include "marshal.h" + + +/*========================================================*/ +/* Private types. */ +/*========================================================*/ + +/** GL_FONT_HISTORY_MODEL Private fields */ +struct _glFontHistoryModelPrivate { + + guint max_n; + + GList *family_list; +}; + +enum { + CHANGED, + LAST_SIGNAL +}; + + +/*========================================================*/ +/* Private globals. */ +/*========================================================*/ + +static guint signals[LAST_SIGNAL] = {0}; + + +/*========================================================*/ +/* Private function prototypes. */ +/*========================================================*/ + +static void gl_font_history_model_finalize (GObject *object); + + +/*****************************************************************************/ +/* Object infrastructure. */ +/*****************************************************************************/ +G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT); + + +/*****************************************************************************/ +/* Class Init Function. */ +/*****************************************************************************/ +static void +gl_font_history_model_class_init (glFontHistoryModelClass *class) +{ + GObjectClass *gobject_class = (GObjectClass *) class; + + gl_font_history_model_parent_class = g_type_class_peek_parent (class); + + gobject_class->finalize = gl_font_history_model_finalize; + + signals[CHANGED] = + g_signal_new ("changed", + G_OBJECT_CLASS_TYPE (gobject_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (glFontHistoryModelClass, changed), + NULL, NULL, + gl_marshal_VOID__VOID, + G_TYPE_NONE, + 0); +} + + +/*****************************************************************************/ +/* Object Instance Init Function. */ +/*****************************************************************************/ +static void +gl_font_history_model_init (glFontHistoryModel *this) +{ + this->priv = g_new0 (glFontHistoryModelPrivate, 1); +} + + +/*****************************************************************************/ +/* Finalize Method. */ +/*****************************************************************************/ +static void +gl_font_history_model_finalize (GObject *object) +{ + glFontHistoryModel *this; + GList *p; + + g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object)); + this = GL_FONT_HISTORY_MODEL (object); + + for ( p = this->priv->family_list; p; p=p->next ) + { + g_free (p->data); + } + g_list_free (this->priv->family_list); + + g_free (this->priv); + + G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object); +} + + +/*****************************************************************************/ +/** New Object Generator. */ +/*****************************************************************************/ +glFontHistoryModel * +gl_font_history_model_new (guint n) +{ + glFontHistoryModel *this; + + this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL); + + this->priv->max_n = n; + + return this; +} + + +/*****************************************************************************/ +/* Add font to history. */ +/*****************************************************************************/ +void +gl_font_history_model_add_family (glFontHistoryModel *this, + const gchar *family) +{ + GList *p; + + /* + * If already in list, remove that entry. + */ + p = g_list_find_custom (this->priv->family_list, + family, + (GCompareFunc)lgl_str_utf8_casecmp); + if (p) + { + this->priv->family_list = + g_list_remove_link (this->priv->family_list, p); + g_free (p->data); + g_list_free_1 (p); + } + + /* + * Now prepend to list. + */ + this->priv->family_list = + g_list_prepend (this->priv->family_list, g_strdup (family)); + + /* + * Truncate list to maximum size + */ + while (g_list_length (this->priv->family_list) > this->priv->max_n) + { + p = g_list_last (this->priv->family_list); + this->priv->family_list = + g_list_remove_link (this->priv->family_list, p); + g_free (p->data); + g_list_free_1 (p); + } + + g_signal_emit (G_OBJECT(this), signals[CHANGED], 0); +} + + +/*****************************************************************************/ +/* Get font. */ +/*****************************************************************************/ +const GList * +gl_font_history_model_get_family_list (glFontHistoryModel *this) +{ + return this->priv->family_list; +} + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */ diff --git a/src/font-history-model.h b/src/font-history-model.h new file mode 100644 index 00000000..7b2d5237 --- /dev/null +++ b/src/font-history-model.h @@ -0,0 +1,89 @@ +/* + * font-history-model.h + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels. If not, see . + */ + +#ifndef __GL_FONT_HISTORY_MODEL_H__ +#define __GL_FONT_HISTORY_MODEL_H__ + + +#include + + +G_BEGIN_DECLS + +/* Utility Macros */ +#define TYPE_GL_FONT_HISTORY_MODEL (gl_font_history_model_get_type ()) +#define GL_FONT_HISTORY_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GL_FONT_HISTORY_MODEL, glFontHistoryModel)) +#define GL_FONT_HISTORY_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GL_FONT_HISTORY_MODEL, glFontHistoryModelClass)) +#define IS_GL_FONT_HISTORY_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GL_FONT_HISTORY_MODEL)) +#define IS_GL_FONT_HISTORY_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GL_FONT_HISTORY_MODEL)) +#define GL_FONT_HISTORY_MODEL_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), TYPE_GL_FONT_HISTORY_MODEL, glFontHistoryModelClass)) + + +/* Type definitions */ +typedef struct _glFontHistoryModel glFontHistoryModel; +typedef struct _glFontHistoryModelPrivate glFontHistoryModelPrivate; +typedef struct _glFontHistoryModelClass glFontHistoryModelClass; + + +/** GL_FONT_HISTORY_MODEL Object */ +struct _glFontHistoryModel { + GObject parent; + + glFontHistoryModelPrivate *priv; +}; + +/** GL_FONT_HISTORY_MODEL Class */ +struct _glFontHistoryModelClass { + GObjectClass parent_class; + + /* + * Signals + */ + void (*changed) (glFontHistoryModel *this, + gpointer user_data); + + +}; + + +GType gl_font_history_model_get_type (void) G_GNUC_CONST; + +glFontHistoryModel *gl_font_history_model_new (guint n); + +void gl_font_history_model_add_family (glFontHistoryModel *this, + const gchar *family); + +const GList *gl_font_history_model_get_family_list (glFontHistoryModel *this); + + +G_END_DECLS + +#endif /* __GL_FONT_HISTORY_MODEL_H__ */ + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */ diff --git a/src/font-history.c b/src/font-history.c new file mode 100644 index 00000000..ec076fc2 --- /dev/null +++ b/src/font-history.c @@ -0,0 +1,67 @@ +/* + * font-history.c + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels. If not, see . + */ + +#include + +#include "font-history.h" + + +#define MAX_FONTS 10 + + +/*========================================================*/ +/* Public globals. */ +/*========================================================*/ + +glFontHistoryModel *gl_font_history = NULL; + + +/*========================================================*/ +/* Private types. */ +/*========================================================*/ + +/*========================================================*/ +/* Private globals. */ +/*========================================================*/ + +/*========================================================*/ +/* Private function prototypes. */ +/*========================================================*/ + + +/*****************************************************************************/ +/* Initialize font history. */ +/*****************************************************************************/ +void +gl_font_history_init (void) +{ + gl_font_history = gl_font_history_model_new (MAX_FONTS); +} + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */ diff --git a/src/font-history.h b/src/font-history.h new file mode 100644 index 00000000..c877c7b9 --- /dev/null +++ b/src/font-history.h @@ -0,0 +1,50 @@ +/* + * font-history.h + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels. If not, see . + */ + +#ifndef __GL_FONT_HISTORY_H__ +#define __GL_FONT_HISTORY_H__ + + +#include "font-history-model.h" + + +G_BEGIN_DECLS + + +glFontHistoryModel *gl_font_history; + + +void gl_font_history_init (void); + + +G_END_DECLS + +#endif /* __GL_FONT_HISTORY_H__ */ + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */ diff --git a/src/glabels-batch.c b/src/glabels-batch.c index ac574326..b3113cbc 100644 --- a/src/glabels-batch.c +++ b/src/glabels-batch.c @@ -31,6 +31,7 @@ #include "print-op.h" #include "file-util.h" #include "prefs.h" +#include "font-history.h" #include "debug.h" /*============================================*/ @@ -127,6 +128,7 @@ main (int argc, char **argv) gl_merge_init (); lgl_db_init (); gl_prefs_init (); + gl_font_history_init (); /* now print the files */ for (p = file_list; p; p = p->next) { diff --git a/src/glabels.c b/src/glabels.c index 14e300bb..61f4dc88 100644 --- a/src/glabels.c +++ b/src/glabels.c @@ -30,6 +30,7 @@ #include "recent.h" #include "mini-preview-pixbuf-cache.h" #include "prefs.h" +#include "font-history.h" #include "debug.h" #include "window.h" #include "file.h" @@ -115,6 +116,7 @@ main (int argc, char **argv) gl_mini_preview_pixbuf_cache_init (); gl_merge_init (); gl_recent_init (); + gl_font_history_init (); /* Parse args and build the list of files to be loaded at startup */ diff --git a/src/label-text.c b/src/label-text.c index d7497624..ca3e08e9 100644 --- a/src/label-text.c +++ b/src/label-text.c @@ -28,6 +28,7 @@ #include #include "font-util.h" +#include "font-history.h" #include "debug.h" @@ -217,6 +218,8 @@ gl_label_text_init (glLabelText *ltext) ltext->priv->size_changed = TRUE; + gl_font_history_model_add_family (gl_font_history, DEFAULT_FONT_FAMILY); + g_signal_connect (G_OBJECT(ltext->priv->buffer), "changed", G_CALLBACK(buffer_changed_cb), ltext); } @@ -481,6 +484,8 @@ set_font_family (glLabelObject *object, ltext->priv->size_changed = TRUE; + gl_font_history_model_add_family (gl_font_history, ltext->priv->font_family); + gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext)); gl_debug (DEBUG_LABEL, "END");