From: fliep Date: Sat, 2 May 2009 17:05:54 +0000 (+0000) Subject: Added properties to the ChimaraGlk widget: "default-font-description" and X-Git-Tag: v0.9~426 X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=cfe519635eddc394e8690a62ca9e47a00384588e;p=projects%2Fchimara%2Fchimara.git Added properties to the ChimaraGlk widget: "default-font-description" and "monospace-font-description". They are the default fonts used in the Glk library. They can either be set directly from PangoFontDescription structs, or by a string such as "Times 12". This is for eventually solving issue #4. --- diff --git a/configure.ac b/configure.ac index ed39840..68ebb78 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,7 @@ PKG_CHECK_MODULES([CHIMARA], [ gtk+-2.0 >= $GTK_REQUIRED_VERSION gthread-2.0 gmodule-2.0 + pango ]) # Libraries needed to build test programs PKG_CHECK_MODULES([TEST], [ diff --git a/docs/reference/chimara-sections.txt b/docs/reference/chimara-sections.txt index 79094a6..15b9828 100644 --- a/docs/reference/chimara-sections.txt +++ b/docs/reference/chimara-sections.txt @@ -7,6 +7,12 @@ chimara_glk_set_interactive chimara_glk_get_interactive chimara_glk_set_protect chimara_glk_get_protect +chimara_glk_set_default_font_description +chimara_glk_set_default_font_string +chimara_glk_get_default_font_description +chimara_glk_set_monospace_font_description +chimara_glk_set_monospace_font_string +chimara_glk_get_monospace_font_description chimara_glk_run chimara_glk_stop chimara_glk_wait diff --git a/src/chimara-glk-private.h b/src/chimara-glk-private.h index 9395b40..8bef3f2 100644 --- a/src/chimara-glk-private.h +++ b/src/chimara-glk-private.h @@ -3,6 +3,7 @@ #include #include +#include #include "glk.h" #include "chimara-glk.h" @@ -17,6 +18,10 @@ struct _ChimaraGlkPrivate { gboolean interactive; /* Whether file operations are allowed */ gboolean protect; + /* Font description of proportional font */ + PangoFontDescription *default_font_desc; + /* Font description of monospace font */ + PangoFontDescription *monospace_font_desc; /* Glk program loaded in widget */ GModule *program; /* Thread in which Glk program is run */ diff --git a/src/chimara-glk.c b/src/chimara-glk.c index 4bc1c15..1a449b5 100644 --- a/src/chimara-glk.c +++ b/src/chimara-glk.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "chimara-glk.h" #include "chimara-glk-private.h" #include "glk.h" @@ -42,7 +43,9 @@ typedef void (* glk_main_t) (void); enum { PROP_0, PROP_INTERACTIVE, - PROP_PROTECT + PROP_PROTECT, + PROP_DEFAULT_FONT_DESCRIPTION, + PROP_MONOSPACE_FONT_DESCRIPTION }; enum { @@ -66,6 +69,8 @@ chimara_glk_init(ChimaraGlk *self) priv->self = self; priv->interactive = TRUE; priv->protect = FALSE; + priv->default_font_desc = pango_font_description_from_string("Sans"); + priv->monospace_font_desc = pango_font_description_from_string("Monospace"); priv->program = NULL; priv->thread = NULL; priv->event_queue = NULL; @@ -89,11 +94,17 @@ chimara_glk_set_property(GObject *object, guint prop_id, const GValue *value, GP switch(prop_id) { case PROP_INTERACTIVE: - chimara_glk_set_interactive(glk, g_value_get_boolean(value)); + chimara_glk_set_interactive( glk, g_value_get_boolean(value) ); break; case PROP_PROTECT: - chimara_glk_set_protect(glk, g_value_get_boolean(value)); + chimara_glk_set_protect( glk, g_value_get_boolean(value) ); break; + case PROP_DEFAULT_FONT_DESCRIPTION: + chimara_glk_set_default_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) ); + break; + case PROP_MONOSPACE_FONT_DESCRIPTION: + chimara_glk_set_monospace_font_description( glk, (PangoFontDescription *)g_value_get_pointer(value) ); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } @@ -112,6 +123,12 @@ chimara_glk_get_property(GObject *object, guint prop_id, GValue *value, GParamSp case PROP_PROTECT: g_value_set_boolean(value, priv->protect); break; + case PROP_DEFAULT_FONT_DESCRIPTION: + g_value_set_pointer(value, priv->default_font_desc); + break; + case PROP_MONOSPACE_FONT_DESCRIPTION: + g_value_set_pointer(value, priv->monospace_font_desc); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } @@ -140,6 +157,10 @@ chimara_glk_finalize(GObject *object) g_mutex_free(priv->abort_lock); priv->abort_lock = NULL; + /* Free private data */ + pango_font_description_free(priv->default_font_desc); + pango_font_description_free(priv->monospace_font_desc); + G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object); } @@ -212,6 +233,11 @@ chimara_glk_started(ChimaraGlk *self) /* TODO: Add default signal handler implementation here */ } +/* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */ +#ifndef G_PARAM_STATIC_STRINGS +#define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB) +#endif + static void chimara_glk_class_init(ChimaraGlkClass *klass) { @@ -259,24 +285,22 @@ chimara_glk_class_init(ChimaraGlkClass *klass) pspec = g_param_spec_boolean("interactive", _("Interactive"), _("Whether user input is expected in the Glk program"), TRUE, - G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION | - G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB); + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS); /** * ChimaraGlk:interactive: * * Sets whether the widget is interactive. A Glk widget is normally * interactive, but in non-interactive mode, keyboard and mouse input are * ignored and the Glk program is controlled by chimara_glk_feed_text(). - * "More" prompts when a lot of text is printed to a text buffer are also - * disabled. This is typically used when you wish to control an interpreter - * program by feeding it a predefined list of commands. + * More prompts when a lot of text is printed to a text + * buffer are also disabled. This is typically used when you wish to control + * an interpreter program by feeding it a predefined list of commands. */ g_object_class_install_property(object_class, PROP_INTERACTIVE, pspec); pspec = g_param_spec_boolean("protect", _("Protected"), _("Whether the Glk program is barred from doing file operations"), FALSE, - G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_LAX_VALIDATION | - G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB); + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS); /** * ChimaraGlk:protect: * @@ -284,7 +308,34 @@ chimara_glk_class_init(ChimaraGlkClass *klass) * mode, all file operations will fail. */ g_object_class_install_property(object_class, PROP_PROTECT, pspec); - + pspec = g_param_spec_pointer("default-font-description", _("Default Font"), + _("Font description of the default proportional font"), + G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS); + /* We can't use G_PARAM_CONSTRUCT on these because then the constructor will + initialize them with NULL */ + /** + * ChimaraGlk:default-font-description: + * + * Pointer to a #PangoFontDescription describing the default proportional + * font, to be used in text buffer windows for example. + * + * Default value: font description created from the string + * Sans + */ + g_object_class_install_property(object_class, PROP_DEFAULT_FONT_DESCRIPTION, pspec); + pspec = g_param_spec_pointer("monospace-font-description", _("Monospace Font"), + _("Font description of the default monospace font"), + G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS); + /** + * ChimaraGlk:monospace-font-description: + * + * Pointer to a #PangoFontDescription describing the default monospace font, + * to be used in text grid windows and #style_Preformatted, for example. + * + * Default value: font description created from the string + * Monospace + */ + g_object_class_install_property(object_class, PROP_MONOSPACE_FONT_DESCRIPTION, pspec); /* Private data */ g_type_class_add_private(klass, sizeof(ChimaraGlkPrivate)); } @@ -383,6 +434,138 @@ chimara_glk_get_protect(ChimaraGlk *glk) return priv->protect; } +/** + * chimara_glk_set_default_font_description: + * @glk: a #ChimaraGlk widget + * @font: a #PangoFontDescription + * + * Sets @glk's default proportional font. See + * #ChimaraGlk:default-font-description. + */ +void +chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font) +{ + g_return_if_fail(glk || CHIMARA_IS_GLK(glk)); + g_return_if_fail(font); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + pango_font_description_free(priv->default_font_desc); + priv->default_font_desc = pango_font_description_copy(font); + + /* TODO: Apply the font description to all the windows and recalculate the sizes */ +} + +/** + * chimara_glk_set_default_font_string: + * @glk: a #ChimaraGlk widget + * @font: string representation of a font description + * + * Sets @glk's default proportional font according to the string @font, which + * must be a string in the form FAMILY-LIST + * [STYLE-OPTIONS] + * [SIZE], such as Charter,Utopia + * Italic 12 or Sans. See + * #ChimaraGlk:default-font-description. + */ +void +chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font) +{ + g_return_if_fail(glk || CHIMARA_IS_GLK(glk)); + g_return_if_fail(font || *font); + + PangoFontDescription *fontdesc = pango_font_description_from_string(font); + g_return_if_fail(fontdesc); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + pango_font_description_free(priv->default_font_desc); + priv->default_font_desc = fontdesc; + + /* TODO: Apply the font description to all the windows and recalculate the sizes */ +} + +/** + * chimara_glk_get_default_font_description: + * + * Returns @glk's default proportional font. + * + * Return value: a newly-allocated #PangoFontDescription which must be freed + * using pango_font_description_free(), or %NULL on error. + */ +PangoFontDescription * +chimara_glk_get_default_font_description(ChimaraGlk *glk) +{ + g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + return pango_font_description_copy(priv->default_font_desc); +} + +/** + * chimara_glk_set_monospace_font_description: + * @glk: a #ChimaraGlk widget + * @font: a #PangoFontDescription + * + * Sets @glk's default monospace font. See + * #ChimaraGlk:monospace-font-description. + */ +void +chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font) +{ + g_return_if_fail(glk || CHIMARA_IS_GLK(glk)); + g_return_if_fail(font); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + pango_font_description_free(priv->monospace_font_desc); + priv->monospace_font_desc = pango_font_description_copy(font); + + /* TODO: Apply the font description to all the windows and recalculate the sizes */ +} + +/** + * chimara_glk_set_monospace_font_string: + * @glk: a #ChimaraGlk widget + * @font: string representation of a font description + * + * Sets @glk's default monospace font according to the string @font, which must + * be a string in the form FAMILY-LIST + * [STYLE-OPTIONS] + * [SIZE], such as Courier + * Bold 12 or Monospace. See + * #ChimaraGlk:monospace-font-description. + */ +void +chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font) +{ + g_return_if_fail(glk || CHIMARA_IS_GLK(glk)); + g_return_if_fail(font || *font); + + PangoFontDescription *fontdesc = pango_font_description_from_string(font); + g_return_if_fail(fontdesc); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + pango_font_description_free(priv->monospace_font_desc); + priv->monospace_font_desc = fontdesc; + + /* TODO: Apply the font description to all the windows and recalculate the sizes */ +} + +/** + * chimara_glk_get_monospace_font_description: + * + * Returns @glk's default monospace font. + * + * Return value: a newly-allocated #PangoFontDescription which must be freed + * using pango_font_description_free(), or %NULL on error. + */ +PangoFontDescription * +chimara_glk_get_monospace_font_description(ChimaraGlk *glk) +{ + g_return_val_if_fail(glk || CHIMARA_IS_GLK(glk), NULL); + + ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(glk); + return pango_font_description_copy(priv->monospace_font_desc); +} + /* glk_enter() is the actual function called in the new thread in which glk_main() runs. */ static gpointer glk_enter(gpointer glk_main) diff --git a/src/chimara-glk.h b/src/chimara-glk.h index 94ee6c5..925e8fc 100644 --- a/src/chimara-glk.h +++ b/src/chimara-glk.h @@ -5,6 +5,7 @@ #include #include +#include G_BEGIN_DECLS @@ -44,6 +45,12 @@ void chimara_glk_set_interactive(ChimaraGlk *glk, gboolean interactive); gboolean chimara_glk_get_interactive(ChimaraGlk *glk); void chimara_glk_set_protect(ChimaraGlk *glk, gboolean protect); gboolean chimara_glk_get_protect(ChimaraGlk *glk); +void chimara_glk_set_default_font_description(ChimaraGlk *glk, PangoFontDescription *font); +void chimara_glk_set_default_font_string(ChimaraGlk *glk, const gchar *font); +PangoFontDescription *chimara_glk_get_default_font_description(ChimaraGlk *glk); +void chimara_glk_set_monospace_font_description(ChimaraGlk *glk, PangoFontDescription *font); +void chimara_glk_set_monospace_font_string(ChimaraGlk *glk, const gchar *font); +PangoFontDescription *chimara_glk_get_monospace_font_description(ChimaraGlk *glk); gboolean chimara_glk_run(ChimaraGlk *glk, gchar *plugin, GError **error); void chimara_glk_stop(ChimaraGlk *glk); void chimara_glk_wait(ChimaraGlk *glk); diff --git a/src/chimara.glade b/src/chimara.glade index 3162368..f15c350 100644 --- a/src/chimara.glade +++ b/src/chimara.glade @@ -1,10 +1,10 @@ - + - 300 - 600 + 400 + 500 Gargoyle GTK @@ -180,7 +180,7 @@ - False + True @@ -190,7 +190,7 @@ gtk-open - False + True diff --git a/src/main.c b/src/main.c index fd2700d..e08e3fb 100644 --- a/src/main.c +++ b/src/main.c @@ -72,6 +72,8 @@ create_window(void) gtk_builder_connect_signals(builder, NULL); glk = chimara_glk_new(); + chimara_glk_set_default_font_string(CHIMARA_GLK(glk), "Utopia 12"); + chimara_glk_set_monospace_font_string(CHIMARA_GLK(glk), "Courier 10"); g_signal_connect(glk, "started", G_CALLBACK(on_started), NULL); g_signal_connect(glk, "stopped", G_CALLBACK(on_stopped), NULL); @@ -115,7 +117,7 @@ main(int argc, char *argv[]) g_object_unref( G_OBJECT(builder) ); - if( !chimara_glk_run(CHIMARA_GLK(glk), ".libs/gridtest.so", &error) ) { + if( !chimara_glk_run(CHIMARA_GLK(glk), ".libs/first.so", &error) ) { error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: "); return 1; } diff --git a/src/window.c b/src/window.c index 364cc14..0cc7efa 100644 --- a/src/window.c +++ b/src/window.c @@ -385,10 +385,7 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, gtk_widget_show_all(scrolledwindow); /* Set the window's font */ - /* TODO: Use Pango to pick out a monospace font on the system */ - PangoFontDescription *font = pango_font_description_from_string("Monospace"); - gtk_widget_modify_font(textview, font); - pango_font_description_free(font); + gtk_widget_modify_font(textview, glk_data->monospace_font_desc); win->widget = textview; win->frame = scrolledwindow; @@ -419,6 +416,9 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype, gtk_container_add( GTK_CONTAINER(scrolledwindow), textview ); gtk_widget_show_all(scrolledwindow); + /* Set the window's font */ + gtk_widget_modify_font(textview, glk_data->default_font_desc); + win->widget = textview; win->frame = scrolledwindow; text_window_get_char_size( textview, &(win->unit_width), &(win->unit_height) );