5

I try to write GUI programs with gnome-builder and try to make the layout with Glade. I made a tutorial on Programmer's Notes.

C

#include <gtk/gtk.h>

int main(int argc, char *argv[]){


    GError          *err = NULL;
    GtkBuilder      *builder;
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "window_main.ui", &err);

    if (err != NULL) {
        fprintf (stderr, "Unable to read file: %s\n", err->message);
        g_error_free(err);
        return 1;
    }

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));

    if (window == NULL || !GTK_IS_WINDOW(window)) {
        fprintf (stderr, "Unable to get window. (window == NULL || window != GtkWindow)\n");
        return 1;
    }

    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);
    gtk_main();

    return 0;
}

void on_window_main_destroy(GtkWidget *widget, gpointer user_data){
    gtk_main_quit();
}

Glade

<?xml version=1.0 encoding="UFT-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
    <requires lib="gtk+" version="3.12"/>
    <object class="GtkWindow" id="window_main">
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">Test Window</property>
        <property name="default_width">640</property>
        <property name="default_height">480</property>
        <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
        <child>
            <placeholder/>
     </child>
    </object>
</interface>

The .glade file is located in the src/ direction like the .c file. When I run the code, following error message returns Unable to read file: Failed to open file “./window_main.ui”: No such file or directory I don't know where I should put the glade file, because when I compiled my self with gcc main.c -Wall 'pkg-config --cflags --libs gtk+-3.0' -export-dynamic and run it with ./a.out the window opens without any error.

  • I don't use Gnome Builder but probably the build output location is not the same and therefore it will not find it. Check the build output in gnomebuilder and try to find the location of the build, maybe in some cache folder or temporary folder somewhere. – José Fonte Aug 26 '17 at 11:04
  • 2
    Ideally you use GResource, not opening a relative path by hand. – TingPing Aug 26 '17 at 22:25

1 Answers1

3

As mentioned by TingPing in comment area, the best way is to include it in GResource. In your case, the resource specification XML would be like

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/main">
    <file>window_main.ui</file>
  </gresource>
</gresources>

And you load the resource with gtk_builder_new_from_resource(), like

GtkBuilder *builder = gtk_builder_new_from_resource("/main/window_main.ui");
Franklin Yu
  • 8,920
  • 6
  • 43
  • 57
  • Could you show a demo to explain how to use GResource in Gnome Builder, I still can not find the file, even after use "glib-compile-resources --generate gresource.xml " – Honghe.Wu Jan 27 '18 at 16:56
  • @Honghe.Wu You can try `--generate-source` and `--generate-header` so you can statically link them like any other C source code. – Franklin Yu Jan 29 '18 at 14:57
  • I have sent the detail problem to you email -- zyu13ATbinghamton with posted the code in Github, Can you check the mail in your free time.Thanks! – Honghe.Wu Jan 30 '18 at 06:53
  • @Honghe.Wu This is a question for C and you sent me an email about Python (I assume that’s [PyGTK](https://en.wikipedia.org/wiki/PyGTK)). Sorry that I’m not familiar with PyGTK. Please make it a separate question with the [tag:pygtk] tag. – Franklin Yu Jan 30 '18 at 15:15