gtk-demo: Improve the GtkApplication demo

Following suggestions in bug 667968
This commit is contained in:
Matthias Clasen 2012-01-16 17:07:12 -05:00
parent 7328cc73aa
commit 2a6a354aa8
5 changed files with 67 additions and 62 deletions

View File

@ -75,6 +75,7 @@ EXTRA_DIST += \
demo.ui \
application.gresource.xml \
application.ui \
menus.ui \
gtk-logo-24.png \
gtk-logo-48.png
@ -97,8 +98,8 @@ gtk3_demo_application_SOURCES = \
gtk3_demo_application_LDADD = $(LDADDS)
application_resources.c: application.gresource.xml gtk-logo-24.png gtk-logo-48.png application.ui
glib-compile-resources --target=$@ --sourcedir=$(srcdir) --generate-source $(srcdir)/application.gresource.xml
application_resources.c: application.gresource.xml gtk-logo-24.png gtk-logo-48.png application.ui menus.ui
$(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate-source $(srcdir)/application.gresource.xml
IMAGEFILES= alphatest.png \
apple-red.png \

View File

@ -293,6 +293,25 @@ clicked_cb (GtkWidget *widget, GtkWidget *info)
gtk_widget_hide (info);
}
static void
startup (GApplication *app)
{
GtkBuilder *builder;
GMenuModel *appmenu;
GMenuModel *menubar;
builder = gtk_builder_new ();
gtk_builder_add_from_resource (builder, "/ui/menus.ui", NULL);
appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
menubar = (GMenuModel *)gtk_builder_get_object (builder, "menubar");
gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu);
gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
g_object_unref (builder);
}
static void
activate (GApplication *app)
{
@ -305,10 +324,6 @@ activate (GApplication *app)
GtkWidget *button;
GtkWidget *infobar;
GtkTextBuffer *buffer;
GMenuModel *appmenu, *menubar;
GBytes *bytes;
const gchar *data;
gsize size;
window = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (window), "Application Class");
@ -320,15 +335,9 @@ activate (GApplication *app)
window);
builder = gtk_builder_new ();
bytes = g_resources_lookup_data ("/ui/application.ui", 0, NULL);
data = g_bytes_get_data (bytes, &size);
gtk_builder_add_from_string (builder, data, size, NULL);
g_bytes_unref (bytes);
gtk_builder_add_from_resource (builder, "/ui/application.ui", NULL);
grid = (GtkWidget *)gtk_builder_get_object (builder, "grid");
appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
menubar = (GMenuModel *)gtk_builder_get_object (builder, "menubar");
contents = (GtkWidget *)gtk_builder_get_object (builder, "contents");
status = (GtkWidget *)gtk_builder_get_object (builder, "status");
message = (GtkWidget *)gtk_builder_get_object (builder, "message");
@ -339,8 +348,6 @@ activate (GApplication *app)
g_object_set_data (G_OBJECT (window), "infobar", infobar);
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu);
gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
gtk_widget_grab_focus (contents);
g_signal_connect (button, "clicked", G_CALLBACK (clicked_cb), infobar);
@ -355,6 +362,8 @@ activate (GApplication *app)
update_statusbar (buffer, GTK_STATUSBAR (status));
gtk_widget_show_all (window);
g_object_unref (builder);
}
int
@ -372,6 +381,7 @@ main (int argc, char *argv[])
app_entries, G_N_ELEMENTS (app_entries),
app);
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_application_run (G_APPLICATION (app), 0, NULL);

View File

@ -6,5 +6,6 @@
</gresource>
<gresource prefix="/ui">
<file>application.ui</file>
<file>menus.ui</file>
</gresource>
</gresources>

View File

@ -106,45 +106,4 @@
</packing>
</child>
</object>
<menu id="appmenu">
<section>
<item label="_New" action="app.new" accel="<Primary>n"/>
<item label="_Open" action="app.open"/>
<item label="_Save" action="app.save" accel="<Primary>s"/>
<item label="Save _As..." action="app.save-as" accel="<Primary>s"/>
</section>
<section>
<item label="_Quit" action="app.quit" accel="<Primary>q"/>
</section>
</menu>
<menu id="menubar">
<section>
<submenu label="_Preferences">
<section>
<item label="_Prefer Dark Theme" action="app.dark"/>
<item label="_Hide Titlebar when maximized" action="win.titlebar"/>
<submenu label="_Color">
<section>
<item label="_Red" action="win.color" target="red" accel="<Primary>r"/>
<item label="_Green" action="win.color" target="green" accel="<Primary>g"/>
<item label="_Blue" action="win.color" target="blue" accel="<Primary>b"/>
</section>
</submenu>
<submenu label="_Shape">
<section>
<item label="_Square" action="win.shape" target="square" accel="<Primary>s"/>
<item label="_Rectangle" action="win.shape" target="rectangle" accel="<Primary>r"/>
<item label="_Oval" action="win.shape" target="oval" accel="<Primary>o"/>
</section>
</submenu>
<item label="_Bold" action="win.bold" accel="<Primary>b"/>
</section>
</submenu>
<submenu label="_Help">
<section>
<item label="_About" action="win.about" accel="<Primary>a"/>
</section>
</submenu>
</section>
</menu>
</interface>

34
demos/gtk-demo/menus.ui Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0"?>
<interface>
<menu id="appmenu">
<section>
<item label="_New" action="app.new" accel="&lt;Primary&gt;n"/>
<item label="_Open" action="app.open"/>
<item label="_Save" action="app.save" accel="&lt;Primary&gt;s"/>
<item label="Save _As..." action="app.save-as" accel="&lt;Primary&gt;s"/>
</section>
<section>
<item label="_Quit" action="app.quit" accel="&lt;Primary&gt;q"/>
</section>
</menu>
<menu id="menubar">
<submenu label="_Preferences">
<item label="_Prefer Dark Theme" action="app.dark"/>
<item label="_Hide Titlebar when maximized" action="win.titlebar"/>
<submenu label="_Color">
<item label="_Red" action="win.color" target="red" accel="&lt;Primary&gt;r"/>
<item label="_Green" action="win.color" target="green" accel="&lt;Primary&gt;g"/>
<item label="_Blue" action="win.color" target="blue" accel="&lt;Primary&gt;b"/>
</submenu>
<submenu label="_Shape">
<item label="_Square" action="win.shape" target="square" accel="&lt;Primary&gt;s"/>
<item label="_Rectangle" action="win.shape" target="rectangle" accel="&lt;Primary&gt;r"/>
<item label="_Oval" action="win.shape" target="oval" accel="&lt;Primary&gt;o"/>
</submenu>
<item label="_Bold" action="win.bold" accel="&lt;Primary&gt;b"/>
</submenu>
<submenu label="_Help">
<item label="_About" action="win.about" accel="&lt;Primary&gt;a"/>
</submenu>
</menu>
</interface>