Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Use GResource for menu XML descriptions
Browse files Browse the repository at this point in the history
Instead of a hardcoded ugly long C string, use GResource support to build
the XML in the binary, with the added niceties of using resources (e.g.
gtk_builder_new_from_resource(), resource:// URIs, etc).
  • Loading branch information
aperezdc committed Apr 30, 2014
1 parent bcbe925 commit 38a576c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
dwt
*.sw[op]
*.py[co]
*.gresources.[hc]
12 changes: 10 additions & 2 deletions Makefile
Expand Up @@ -17,16 +17,24 @@ all: dwt dwt.1 dwt.desktop

dwt: CFLAGS += $(PKG_CFLAGS)
dwt: LDLIBS += $(PKG_LDLIBS)
dwt: dwt.o
dwt: dwt.o dwt.gresources.o

%: %.o
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

dwt.1: dwt.rst
rst2man $< $@

dwt.gresources.o: menus.xml

%.gresources.c: %.gresources.xml
glib-compile-resources --generate-source --target=$@ $<

%.gresources.h: %.gresources.xml
glib-compile-resources --generate-header --target=$@ $<

clean:
$(RM) dwt dwt.o
$(RM) dwt dwt.gresources.o dwt.gresources.h dwt.gresources.c dwt.o

install: all
install -m 755 -d $(DESTDIR)$(PREFIX)/bin
Expand Down
53 changes: 4 additions & 49 deletions dwt.c
Expand Up @@ -27,6 +27,8 @@
#define DWT_USE_HEADER_BAR FALSE
#endif /* !DWT_USE_HEADER_BAR */

#define DWT_GRESOURCE(name) ("/org/perezdecastro/dwt/" name)

#include <vte/vte.h>
#include <stdlib.h>
#include <sys/types.h>
Expand Down Expand Up @@ -62,7 +64,6 @@ typedef struct {
/* Forward declarations. */
static GtkWidget*
create_new_window (GtkApplication *application, const gchar *command);
static const gchar ui_menus_xml[];


static const GOptionEntry option_entries[] =
Expand Down Expand Up @@ -305,8 +306,7 @@ setup_popover (VteTerminal *vtterm)
g_signal_connect (G_OBJECT (popover), "closed",
G_CALLBACK (popover_closed), vtterm);

GtkBuilder *builder = gtk_builder_new ();
gtk_builder_add_from_string (builder, ui_menus_xml, -1, NULL);
GtkBuilder *builder = gtk_builder_new_from_resource (DWT_GRESOURCE ("menus.xml"));
gtk_popover_bind_model (GTK_POPOVER (popover),
G_MENU_MODEL (gtk_builder_get_object (builder, "popover-menu")),
NULL);
Expand Down Expand Up @@ -593,50 +593,6 @@ add_application_accels (GtkApplication *application,
}


static const gchar ui_menus_xml[] =
"<interface>"
" <menu id='app-menu'>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>New _Terminal</attribute>"
" <attribute name='action'>app.new-terminal</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_About</attribute>"
" <attribute name='action'>app.about</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Quit</attribute>"
" <attribute name='action'>app.quit</attribute>"
" </item>"
" </section>"
" </menu>"
" <menu id='popover-menu'>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Copy</attribute>"
" <attribute name='action'>win.copy</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Paste</attribute>"
" <attribute name='action'>win.paste</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>Copy _URL</attribute>"
" <attribute name='action'>win.copy-url</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Open URL…</attribute>"
" <attribute name='action'>win.open-url</attribute>"
" </item>"
" </section>"
" </menu>"
"</interface>";

static const ActionReg win_actions[] = {
{ "win.font-reset", "<Super>0", font_reset_action_activated },
{ "win.font-bigger", "<Super>plus", font_bigger_action_activated },
Expand Down Expand Up @@ -755,8 +711,7 @@ app_started (GApplication *application)
add_actions (G_ACTION_MAP (application),
app_actions, G_N_ELEMENTS (app_actions));

GtkBuilder *builder = gtk_builder_new ();
gtk_builder_add_from_string (builder, ui_menus_xml, -1, NULL);
GtkBuilder *builder = gtk_builder_new_from_resource (DWT_GRESOURCE ("menus.xml"));
gtk_application_set_app_menu (GTK_APPLICATION (application),
G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
g_object_unref (builder);
Expand Down
8 changes: 8 additions & 0 deletions dwt.gresources.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<gresources>
<gresource prefix="/org/perezdecastro/dwt">
<file preprocess="xml-stripblanks" compressed="true">menus.xml</file>
</gresource>
</gresources>
<!-- vim:fenc=utf-8
-->
43 changes: 43 additions & 0 deletions menus.xml
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<interface>
<menu id='app-menu'>
<section>
<item>
<attribute name='label' translatable='yes'>New _Terminal</attribute>
<attribute name='action'>app.new-terminal</attribute>
</item>
</section>
<section>
<item>
<attribute name='label' translatable='yes'>_About</attribute>
<attribute name='action'>app.about</attribute>
</item>
</section>
<section>
<item>
<attribute name='label' translatable='yes'>_Quit</attribute>
<attribute name='action'>app.quit</attribute>
</item>
</section>
</menu>
<menu id='popover-menu'>
<section>
<item>
<attribute name='label' translatable='yes'>_Copy</attribute>
<attribute name='action'>win.copy</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>_Paste</attribute>
<attribute name='action'>win.paste</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>Copy _URL</attribute>
<attribute name='action'>win.copy-url</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>_Open URL…</attribute>
<attribute name='action'>win.open-url</attribute>
</item>
</section>
</menu>
</interface>

0 comments on commit 38a576c

Please sign in to comment.