Skip to content

Commit

Permalink
Refactor for view-font-name GSetting
Browse files Browse the repository at this point in the history
Move _set_font() from MousepadDocument to MousepadView. Add glue to
bind the setting change to update the view's font.
  • Loading branch information
codebrainz committed Jul 7, 2014
1 parent aa8a283 commit 8b224a3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
24 changes: 1 addition & 23 deletions mousepad/mousepad-document.c
Expand Up @@ -177,7 +177,7 @@ mousepad_document_init (MousepadDocument *document)
{
GtkTargetList *target_list;
gboolean word_wrap, insert_spaces;
gchar *font_name, *color_scheme;
gchar *color_scheme;
gint tab_size;
GtkSourceStyleScheme *scheme = NULL;

Expand Down Expand Up @@ -218,14 +218,12 @@ mousepad_document_init (MousepadDocument *document)

/* read all the default settings */
word_wrap = mousepad_settings_get_boolean ("view-word-wrap");
font_name = mousepad_settings_get_string ("view-font-name");
tab_size = mousepad_settings_get_int ("view-tab-size");
insert_spaces = mousepad_settings_get_boolean ("view-insert-spaces");
color_scheme = mousepad_settings_get_string ("view-color-scheme");

/* set all the settings */
mousepad_document_set_word_wrap (document, word_wrap);
mousepad_document_set_font (document, font_name);
mousepad_view_set_tab_size (document->textview, tab_size);
mousepad_view_set_insert_spaces (document->textview, insert_spaces);

Expand All @@ -235,7 +233,6 @@ mousepad_document_init (MousepadDocument *document)
gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (document->buffer), scheme);

/* cleanup */
g_free (font_name);
g_free (color_scheme);

/* attach signals to the text view and buffer */
Expand Down Expand Up @@ -486,25 +483,6 @@ mousepad_document_set_word_wrap (MousepadDocument *document,



void
mousepad_document_set_font (MousepadDocument *document,
const gchar *font_name)
{
PangoFontDescription *font_desc;

mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));

if (G_LIKELY (font_name))
{
/* set the widget font */
font_desc = pango_font_description_from_string (font_name);
gtk_widget_modify_font (GTK_WIDGET (document->textview), font_desc);
pango_font_description_free (font_desc);
}
}



void
mousepad_document_focus_textview (MousepadDocument *document)
{
Expand Down
3 changes: 0 additions & 3 deletions mousepad/mousepad-document.h
Expand Up @@ -60,9 +60,6 @@ GType mousepad_document_get_type (void) G_GNUC_CONST;

MousepadDocument *mousepad_document_new (void);

void mousepad_document_set_font (MousepadDocument *document,
const gchar *font_name);

void mousepad_document_set_overwrite (MousepadDocument *document,
gboolean overwrite);

Expand Down
54 changes: 54 additions & 0 deletions mousepad/mousepad-view.c
Expand Up @@ -139,9 +139,52 @@ mousepad_view_class_init (MousepadViewClass *klass)



void
mousepad_view_set_font_name (MousepadView *view,
const gchar *font_name)
{
PangoFontDescription *font_desc;

g_return_if_fail (MOUSEPAD_IS_VIEW (view));

if (font_name == NULL)
font_name = "Monospace";

font_desc = pango_font_description_from_string (font_name);

if (G_LIKELY (font_desc != NULL))
{
#if GTK_CHECK_VERSION(3, 0, 0)
gtk_widget_override_font (GTK_WIDGET (view), font_desc);
#else
gtk_widget_modify_font (GTK_WIDGET (view), font_desc);
#endif
pango_font_description_free (font_desc);
}
else
g_critical ("Invalid font-name given: %s", font_name);
}



/* when the view-font-name setting changes, update the view to use that font */
static void
mousepad_view_font_name_setting_changed (MousepadView *view,
gchar *key,
MousepadSettings *settings)
{
gchar *font_name = mousepad_settings_get_string ("view-font-name");
mousepad_view_set_font_name (view, font_name);
g_free (font_name);
}



static void
mousepad_view_init (MousepadView *view)
{
gchar *font_name;

/* initialize selection variables */
view->selection_timeout_id = 0;
view->selection_tag = NULL;
Expand All @@ -159,6 +202,17 @@ mousepad_view_init (MousepadView *view)
/* bind Gsettings */
mousepad_settings_bind ("view-line-numbers", view, "show-line-numbers", G_SETTINGS_BIND_DEFAULT);
mousepad_settings_bind ("view-auto-indent", view, "auto-indent", G_SETTINGS_BIND_DEFAULT);

/* Set the initial font-name */
font_name = mousepad_settings_get_string ("view-font-name");
mousepad_view_set_font_name (view, font_name);
g_free (font_name);

/* connect to Gsettings change notifications */
g_signal_connect_swapped (MOUSEPAD_GSETTINGS,
"changed::view-font-name",
G_CALLBACK (mousepad_view_font_name_setting_changed),
view);
}


Expand Down
2 changes: 2 additions & 0 deletions mousepad/mousepad-view.h
Expand Up @@ -107,6 +107,8 @@ gint mousepad_view_get_tab_size (MousepadView *view

gboolean mousepad_view_get_insert_spaces (MousepadView *view);

void mousepad_view_set_font_name (MousepadView *view,
const gchar *font_name);

G_END_DECLS

Expand Down
18 changes: 0 additions & 18 deletions mousepad/mousepad-window.c
Expand Up @@ -4401,9 +4401,7 @@ mousepad_window_action_select_font (GtkAction *action,
MousepadWindow *window)
{
GtkWidget *dialog;
MousepadDocument *document;
gchar *font_name;
gint i;

mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
Expand All @@ -4428,22 +4426,6 @@ mousepad_window_action_select_font (GtkAction *action,
/* store the font in the preferences */
mousepad_settings_set_string ("view-font-name", font_name);

/* apply the font in all documents in this window */
for (i = 0; i < gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook)); i++)
{
/* get the document */
document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i));

/* debug check */
mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));

/* set the font */
mousepad_document_set_font (document, font_name);

/* update the tab array */
mousepad_view_set_tab_size (document->textview, mousepad_view_get_tab_size (document->textview));
}

/* cleanup */
g_free (font_name);
}
Expand Down

0 comments on commit 8b224a3

Please sign in to comment.