Using custom widgets with glade / Gtkbuilder
Asked Answered
Q

3

11

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?

Quean answered 9/6, 2010 at 0:1 Comment(1)
Have you looked at library.gnome.org/devel/gtk/stable/GtkBuilder.html ? Could you clarify exactly what your objective is here?Buy
C
7

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.

Chipmunk answered 10/6, 2010 at 1:44 Comment(5)
That seems good. I've been studying the GNOME sources to get an idea of how they do it, and they do subclass GtkWindow. I'm not sure if they use the technique you outlined, though.Quean
Which applications are you looking at? I sometimes look at Gedit for examples of how to do things, but it is huge.Chipmunk
I usually look at Gedit too. As it turns out, though, Gedit doesn't actually use GtkBuilder -- in fact, I'm pretty sure none of the standard GNOME apps do. Which, I think, explains my confusion -- I expected that anyone in their right mind would want to use GtkBuilder.Quean
GtkBuilder is relatively new, so older applications would have to port legacy code to use it. Also, it's probably more useful for simple applications with a static interface...Ezra
Actually, it's in complicated applications where GtkBuilder is absolutely indispensable. If the application is simple with a static interface, you can just build the GUI in code and forget about it.Chipmunk
D
0

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.

Dermatoid answered 9/6, 2010 at 11:9 Comment(1)
It is possible to do it, and countless GNOME apps have done this, e.g. Todo, Photos, Calender...Rao
F
0

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.

Fictile answered 25/8, 2010 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.