Question

I'm developing an application with Gtk and Glade. My impression is that it's common practice to create a subclass of GtkWindow for your main window, but I'm stuck on how I would construct my subclass from a GtkBuilder definition. Does anyone know how?

Was it helpful?

Solution

Subclassing GtkWindow is more common in GTK's various language bindings than it is in plain C. You didn't mention which language you were using.

That said, the way I subclass GtkWindow in C is to create the contents of the window in Glade, but not the window itself. In Glade 3 (IIRC) you can right-click on a widget in the palette and choose "Add widget as toplevel" to place a non-toplevel widget without a container.

Then write code for your subclass of GtkWindow, let's call it MyAppWindow. I won't go into that in this answer since there are plenty of examples in the GObject documentation. In the init function (my_app_window_init()) load the Glade file, use gtk_builder_get_object() to get a pointer to the outermost widget in the Glade file, and use gtk_container_add() to add it to the window you are constructing. Then use gtk_builder_connect_signals() as you normally would.

You have to set all the window's properties manually this way, since you can't do it in Glade, but other than that I've found it works quite well.

OTHER TIPS

it is not common practice to subclass GtkWindow.

i don't think it is possible to subclass toplevel window created from gtkbuilder definition.

gtkbuilder needs to know about your subclassed widget before creation.

If you really want to create your own subclass of GtkWindow ptomato describes the basic steps well. It is also possible to create plugins for glade to make your custom widgets available. But this is not very easy, and most likely not what you want to do.

Most applications only use standard widgets without subclassing any of them. Then loading a glade file with gtkbuilder (or libglade) you don't need to have a special class for your GUI (like in some other RAD tools) instead you just get a set of objects. The API lets you look them up by name (and the window is basically just one of them). A common approach is to look up all widgets you are going to interact with and store them in global variables when the program starts up. Or if you need several instances of the window you can create a struct to store them in. Or you can simple lookup the widgets each time you need them. Note that the set of objects you get is completely dynamic. You can for example move the widgets between different windows just as if you created the GUI programmatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top