quarta-feira, 13 de abril de 2011

Can you get the parent GTK window from a widget? - Stack Overflow

Can you get the parent GTK window from a widget? - Stack Overflow: "

I have a custom widget and it needs to launch a MessageDialog and in order for me to put that message dialog on top of the window my widget is in then I need access to the parent gtk.window. Is there a way to get the parent GTK window? Thanks

link|edit|flag


2 Answers

The GTK docs suggest:

   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);    if (gtk_widget_is_toplevel (toplevel))      {        /* Perform action on toplevel. */      } 

get_toplevel will return the topmost widget you're inside, whether or not it's a window, thus the is_toplevel check. Yeah something is mis-named since the code above does a "get_toplevel()" then an immediate "is_toplevel()" (most likely, get_toplevel() should be called something else).

link|edit|flag


Though gtk_widget_get_toplevel should work, you may also give a try to the code below. It should get the parent gtk window for the given widget and print it's title.

GdkWindow *gtk_window = gtk_widget_get_parent_window(widget); GtkWindow *parent = NULL; gdk_window_get_user_data(gtk_window, (gpointer *)&parent); g_print("%s\n", gtk_window_get_title(parent)); 

hope this helps, regards

link|edit|flag


This takes a needless detour through low-level GDK gunge - better to stay with the GTK layer. Here you require the widget to be realized, and will likely have issues in GTK 3.Havoc P Feb 24 at 2:49

Nenhum comentário: