Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GResource to load GtkBuilder XML UI file #1703

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -86,6 +86,7 @@ Makefile.in
#-----------------------------------------------------------------------
/src/geany
/src/geany_private.res
/src/resources.[ch]
/src/signallist.i

#-----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -106,6 +106,7 @@ GEANY_CHECK_PLUGINS
# check for mingw specific settings
GEANY_CHECK_MINGW

GEANY_CHECK_GRESOURCE
GEANY_CHECK_SOCKET
GEANY_CHECK_VTE
GEANY_CHECK_MAC_INTEGRATION
Expand Down
5 changes: 3 additions & 2 deletions data/Makefile.am
Expand Up @@ -110,8 +110,9 @@ nobase_dist_pkgdata_DATA = \
$(templates) \
filetype_extensions.conf \
snippets.conf \
ui_toolbar.xml \
geany.glade
ui_toolbar.xml

EXTRA_DIST = geany.glade

if GTK3
nobase_dist_pkgdata_DATA += \
Expand Down
7 changes: 7 additions & 0 deletions m4/geany-gresource.m4
@@ -0,0 +1,7 @@
AC_DEFUN([GEANY_CHECK_GRESOURCE],
[
AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources], [no])
AS_IF([test "x$GLIB_COMPILE_RESOURCES" = "xno"], [
AC_MSG_ERROR([Unable to find glib-compile-resources, is it installed?])
])
])
27 changes: 26 additions & 1 deletion src/Makefile.am
Expand Up @@ -10,7 +10,8 @@ EXTRA_DIST = \
filetypesprivate.h \
keybindingsprivate.h \
pluginprivate.h \
projectprivate.h
projectprivate.h \
geany.gresource.xml

AM_CPPFLAGS = \
-I$(top_srcdir) \
Expand Down Expand Up @@ -93,6 +94,7 @@ libgeany_la_SOURCES = \
prefs.c prefs.h \
printing.c printing.h \
project.c project.h \
resources.c resources.h \
sciwrappers.c sciwrappers.h \
search.c search.h \
socket.c socket.h \
Expand Down Expand Up @@ -184,6 +186,29 @@ signallist.i: $(glade_file) Makefile

CLEANFILES += signallist.i

# embed files using gresource
resource_dependencies = geany.gresource.xml $(top_srcdir)/data/geany.glade
resources.c: $(resource_dependencies)
$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) \
--target=$@ \
--sourcedir=$(srcdir) \
--generate-source \
--c-name="geany" \
--manual-register \
--internal \
$(srcdir)/geany.gresource.xml
resources.h: $(resource_dependencies)
$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) \
--target=$@ \
--sourcedir=$(srcdir) \
--generate-header \
--c-name="geany" \
--manual-register \
--internal \
$(srcdir)/geany.gresource.xml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, maybe you need to add something like || rm -f $@ (but you need to still return in error) so that if the generation fails for some reason it will be tried again next time. Another common solution is to generate a temporary file and move it over on success, so it's mostly atomic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this tool is too stupid to not properly update the file (or not) depending on the outcome?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but many tools aren't smart enough so I'd at least check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs glib-compile-resources twice. It's usually better to a construct pattern rule, where (GNU) make recognizes that one rule produces multiple output files. That's assuming it's even possible with, I haven't checked.

%.gresource.c %.gresource.h: %.gresource.xml $(top_srcdir)/data/geany.glade
[…]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kugel- It's supposed to run it twice, once for the source file (--generate-source) and once for the header file (--generate-header).

CLEANFILES += resources.c resources.h
BUILT_SOURCES = resources.c resources.h

# install the run script
if MINGW
dist_pkglibexec_SCRIPTS = geany-run-helper.bat
Expand Down
6 changes: 6 additions & 0 deletions src/geany.gresource.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/geany/Geany">
<file alias="geany.glade" compressed="true">../data/geany.glade</file>
</gresource>
</gresources>
20 changes: 20 additions & 0 deletions src/gtkcompat.h
Expand Up @@ -98,6 +98,26 @@ G_BEGIN_DECLS
#endif


#if !GTK_CHECK_VERSION(3, 4, 0)
static inline guint gtkcompat_builder_add_from_resource(GtkBuilder *builder,
const gchar *resource_path, GError **error)
{
GBytes *bytes = g_resources_lookup_data(resource_path,
G_RESOURCE_LOOKUP_FLAGS_NONE, error);
if (bytes == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is worth a a g_error(), as it means Geany wasn't compiled correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be done in the calling code; this function is just a compat wrapper around gtk_builder_add_from_resource and so is meant to match its semantics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, makes sense.

return 0;
gsize data_len = 0;
gconstpointer data = g_bytes_get_data(bytes, &data_len);
g_assert(data_len > 0);
g_assert(data != NULL);
guint id = gtk_builder_add_from_string(builder, data, data_len, error);
g_bytes_unref(bytes);
return id;
}
#define gtk_builder_add_from_resource gtkcompat_builder_add_from_resource
#endif


G_END_DECLS

#endif /* GTK_COMPAT_H */
3 changes: 3 additions & 0 deletions src/libmain.c
Expand Up @@ -49,6 +49,7 @@
#include "plugins.h"
#include "prefs.h"
#include "printing.h"
#include "resources.h"
#include "sidebar.h"
#ifdef HAVE_SOCKET
# include "socket.h"
Expand Down Expand Up @@ -1046,6 +1047,7 @@ gint main_lib(gint argc, gchar **argv)
g_type_init();
#endif

geany_register_resource();
log_handlers_init();

app = g_new0(GeanyApp, 1);
Expand Down Expand Up @@ -1362,6 +1364,7 @@ static void do_main_quit(void)
g_free(app);

ui_finalize_builder();
geany_unregister_resource();

gtk_main_quit();
}
Expand Down
7 changes: 2 additions & 5 deletions src/ui_utils.c
Expand Up @@ -42,6 +42,7 @@
#include "msgwindow.h"
#include "prefs.h"
#include "project.h"
#include "resources.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not needed :) Could you remove this extra include please?

#include "sciwrappers.h"
#include "sidebar.h"
#include "stash.h"
Expand Down Expand Up @@ -2425,7 +2426,6 @@ static GtkWidget *ui_get_top_parent(GtkWidget *widget)

void ui_init_builder(void)
{
gchar *interface_file;
const gchar *name;
GError *error;
GSList *iter, *all_objects;
Expand All @@ -2440,20 +2440,17 @@ void ui_init_builder(void)
gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);

error = NULL;
interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
if (! gtk_builder_add_from_file(builder, interface_file, &error))
if (gtk_builder_add_from_resource(builder, "/org/geany/Geany/geany.glade", &error) == 0)
{
/* Show the user this message so they know WTF happened */
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
_("Geany cannot start!"), error->message);
/* Aborts */
g_error("Cannot create user-interface: %s", error->message);
g_error_free(error);
g_free(interface_file);
g_object_unref(builder);
return;
}
g_free(interface_file);

callbacks_connect(builder);

Expand Down