Re: getting a pointer to a GtkWindow

Jeremy Wilkins wrote:

I'm trying to learn the basics of gtk. I've created a simple app with
the layout done in glade. I can use lookup widget to get a pointer to
the current window, but it doesn't find other windows in the app.

Note that lookup_widget () isn't a GTK function. It's available only when
the GUI was built using Glade. It's a Glade-specific support function.

I need to find a way to open a new window (when the user clicks a
button). I also need to figure how to stop the second window from
showing automatically when the program loads up, the visible property
for the window in glade doesn't seem to change anything.

Have a look at your main.c file, which should be generated by Glade. The
main () function in it contains the code to create and show the windows.
If your window is named i.e. "MyWindow1" then look for the two lines:

MyWindow1 = (GtkWindow *) create_MyWindow1 ();
gtk_widget_show ((GtkWidget *) MyWindow1);

For every window which is automatically shown on startup, lines similar to
these should appear. Just remove (or comment out) them for the windows you
don't want to be automatically opened on program startup. You can safely
edit the main.c file, it won't be changed later by Glade.

This is a generic way to open windows. You just need:
1. a handle variable to reference the window ("GtkWindow *MyWindow1")
2. call the creator function (provided by Glade) ("create_MyWindow1 ();")
3. to show the newly created window, using gtk_widget_show ().


Note that the GtkWindow "class" is a "subclass" of GtkWidget. This means
that all functions meant to deal with GtkWidgets can be applied on
GtkWindows as well. The use of gtk_widget_show () is an example of this.

(Classes and subclasses are actually not part of C but GTK does a great
job of consistently emulating their handling in the same way they're known
from other object oriented languages like C++ or Java.)

I recommend to make the window's handle variable globally accessable. The
Glade code provides it as a local variable for the main () function only.
Just make it a global variable to have it accessable from anywhere in your

To open a particular window only when a button (or menu entry or key) is
pressed you simply need to put those two lines of code into an appropriate
signal handler for it. For buttons this is most likely the "clicked"-


Note that when using Glade to build the GUI you should tell Glade at its
"signals" page of its widget property window to generate a declaration and
skeleton (an empty handler function) for your signal (instead of writing
the handler completely on your own). They'll be provided in the
callbacks.h / callbacks.c files. You can write your desired actions into
the signal handler then.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]