gtk-demo: modernize the application demo

Subclass GtkApplication and GtkApplicationWindow
and use a template.
This commit is contained in:
Matthias Clasen 2016-02-13 21:46:40 -05:00
parent 7f2d4400ba
commit 7bca66e1ff
2 changed files with 192 additions and 171 deletions

View File

@ -13,6 +13,22 @@
#ifdef STANDALONE
typedef GtkApplication DemoApplication;
typedef GtkApplicationClass DemoApplicationClass;
G_DEFINE_TYPE (DemoApplication, demo_application, GTK_TYPE_APPLICATION)
typedef struct {
GtkApplicationWindow parent_instance;
GtkWidget *message;
GtkWidget *infobar;
GtkTextBuffer *buffer;
} DemoApplicationWindow;
typedef GtkApplicationWindowClass DemoApplicationWindowClass;
G_DEFINE_TYPE (DemoApplicationWindow, demo_application_window, GTK_TYPE_APPLICATION_WINDOW)
static void create_window (GApplication *app, const char *contents);
static void
@ -39,10 +55,9 @@ show_action_dialog (GSimpleAction *action)
static void
show_action_infobar (GSimpleAction *action,
GVariant *parameter,
gpointer window)
gpointer data)
{
GtkWidget *infobar;
GtkWidget *message;
DemoApplicationWindow *window = data;
gchar *text;
const gchar *name;
const gchar *value;
@ -50,12 +65,10 @@ show_action_infobar (GSimpleAction *action,
name = g_action_get_name (G_ACTION (action));
value = g_variant_get_string (parameter, NULL);
message = g_object_get_data (G_OBJECT (window), "message");
infobar = g_object_get_data (G_OBJECT (window), "infobar");
text = g_strdup_printf ("You activated radio action: \"%s\".\n"
"Current value: %s", name, value);
gtk_label_set_text (GTK_LABEL (message), text);
gtk_widget_show (infobar);
gtk_label_set_text (GTK_LABEL (window->message), text);
gtk_widget_show (window->infobar);
g_free (text);
}
@ -332,6 +345,8 @@ startup (GApplication *app)
GMenuModel *appmenu;
GMenuModel *menubar;
G_APPLICATION_CLASS (demo_application_parent_class)->startup (app);
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder, "/application/menus.ui", NULL);
@ -346,66 +361,13 @@ startup (GApplication *app)
static void
create_window (GApplication *app,
const char *content_text)
const char *content)
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *grid;
GtkWidget *contents;
GtkWidget *status;
GtkWidget *message;
GtkWidget *button;
GtkWidget *infobar;
GtkWidget *menutool;
GMenuModel *toolmenu;
GtkTextBuffer *buffer;
DemoApplicationWindow *window;
window = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (window), "Application Class");
gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
g_action_map_add_action_entries (G_ACTION_MAP (window),
win_entries, G_N_ELEMENTS (win_entries),
window);
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder, "/application/application.ui", NULL);
grid = (GtkWidget *)gtk_builder_get_object (builder, "grid");
contents = (GtkWidget *)gtk_builder_get_object (builder, "contents");
status = (GtkWidget *)gtk_builder_get_object (builder, "status");
message = (GtkWidget *)gtk_builder_get_object (builder, "message");
button = (GtkWidget *)gtk_builder_get_object (builder, "button");
infobar = (GtkWidget *)gtk_builder_get_object (builder, "infobar");
menutool = (GtkWidget *)gtk_builder_get_object (builder, "menutool");
toolmenu = (GMenuModel *)gtk_builder_get_object (builder, "toolmenu");
g_object_set_data (G_OBJECT (window), "message", message);
g_object_set_data (G_OBJECT (window), "infobar", infobar);
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (menutool),
gtk_menu_new_from_model (toolmenu));
gtk_widget_grab_focus (contents);
g_signal_connect (button, "clicked", G_CALLBACK (clicked_cb), infobar);
/* Show text widget info in the statusbar */
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
if (content_text)
gtk_text_buffer_set_text (buffer, content_text, -1);
g_signal_connect_object (buffer, "changed",
G_CALLBACK (update_statusbar), status, 0);
g_signal_connect_object (buffer, "mark-set",
G_CALLBACK (mark_set_callback), status, 0);
update_statusbar (buffer, GTK_STATUSBAR (status));
gtk_widget_show_all (window);
g_object_unref (builder);
window = (DemoApplicationWindow *)g_object_new (demo_application_window_get_type (), NULL);
if (content)
gtk_text_buffer_set_text (window->buffer, content, -1);
}
static void
@ -414,16 +376,12 @@ activate (GApplication *app)
create_window (app, NULL);
}
int
main (int argc, char *argv[])
static void
demo_application_init (DemoApplication *app)
{
GtkApplication *app;
GSettings *settings;
GAction *action;
gtk_init (NULL, NULL);
app = gtk_application_new ("org.gtk.Demo2", 0);
settings = g_settings_new ("org.gtk.Demo");
g_action_map_add_action_entries (G_ACTION_MAP (app),
@ -433,13 +391,50 @@ main (int argc, char *argv[])
action = g_settings_create_action (settings, "color");
g_action_map_add_action (G_ACTION_MAP (app), action);
}
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
static void
demo_application_class_init (DemoApplicationClass *class)
{
GApplicationClass *app_class = G_APPLICATION_CLASS (class);
g_application_run (G_APPLICATION (app), 0, NULL);
app_class->startup = startup;
app_class->activate = activate;
}
return 0;
static void
demo_application_window_init (DemoApplicationWindow *win)
{
gtk_widget_init_template (GTK_WIDGET (win));
g_action_map_add_action_entries (G_ACTION_MAP (win),
win_entries, G_N_ELEMENTS (win_entries),
win);
}
static void
demo_application_window_class_init (DemoApplicationWindowClass *class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gtk_widget_class_set_template_from_resource (widget_class, "/application/application.ui");
gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, message);
gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, infobar);
gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, buffer);
gtk_widget_class_bind_template_callback (widget_class, clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, update_statusbar);
gtk_widget_class_bind_template_callback (widget_class, mark_set_callback);
}
int
main (int argc, char *argv[])
{
GtkApplication *app;
app = GTK_APPLICATION (g_object_new (demo_application_get_type (),
"application-id", "org.gtk.Demo2",
NULL));
return g_application_run (G_APPLICATION (app), 0, NULL);
}
#else /* !STANDALONE */

View File

@ -1,28 +1,43 @@
<?xml version="1.0"?>
<interface>
<template class="DemoApplicationWindow" parent="GtkWindow">
<property name="title" translatable="yes">Application Class</property>
<property name="default-width">200</property>
<property name="default-height">200</property>
<property name="icon-name">document-open</property>
<property name="visible">1</property>
<child>
<object class="GtkGrid" id="grid">
<property name="visible">1</property>
<child>
<object class="GtkToolbar" id="toolbar">
<property name="visible">1</property>
<property name="hexpand">1</property>
<style>
<class name="primary-toolbar"/>
</style>
<child>
<object class="GtkMenuToolButton" id="menutool">
<property name="visible">1</property>
<property name="icon-name">document-open</property>
<property name="menumodel">toolmenu</property>
</object>
</child>
<child>
<object class="GtkToolButton" id="quit">
<property name="visible">1</property>
<property name="icon-name">application-exit</property>
<property name="action-name">app.quit</property>
</object>
</child>
<child>
<object class="GtkSeparatorToolItem" id="sep"/>
<object class="GtkSeparatorToolItem" id="sep">
<property name="visible">1</property>
</object>
</child>
<child>
<object class="GtkToolButton" id="logo">
<property name="visible">1</property>
<property name="icon-name">applications-other</property>
<property name="action-name">win.logo</property>
</object>
@ -35,6 +50,7 @@
</child>
<child>
<object class="GtkInfoBar" id="infobar">
<property name="visible">1</property>
<property name="no-show-all">1</property>
<property name="hexpand">1</property>
<child internal-child="content_area">
@ -55,6 +71,7 @@
<property name="valign">center</property>
<property name="label" translatable="yes">_OK</property>
<property name="use_underline">1</property>
<signal name="clicked" handler="cliced_cb" object="infobar"/>
</object>
</child>
</object>
@ -67,11 +84,14 @@
</child>
<child>
<object class="GtkScrolledWindow" id="sw">
<property name="visible">1</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkTextView" id="contents">
<property name="visible">1</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<property name="buffer">buffer</property>
</object>
</child>
</object>
@ -91,10 +111,16 @@
</packing>
</child>
</object>
</child>
</template>
<menu id="toolmenu">
<item>
<attribute name="label">File1</attribute>
<attribute name="action">win.file1</attribute>
</item>
</menu>
<object class="GtkTextBuffer" id="buffer">
<signal name="changed" handler="update_statusbar" object="status"/>
<signal name="mark-set" handler="mark_set_callback" object="status"/>
</object>
</interface>