Re: gtk_widget_get_toplevel() weirdness.



  
  I've narrowed it down...  gtk_widget_get_toplevel()  considers a popup
menu to be a top-level window.
  
  Is this a bug or a feature?
  
  The program below traces widget parentage up to NULL whenever you click
on file->open and file->close.  
  The popups' parent is always NULL, and its name is GtkWindow.
  
  John
    
/*
   This code ripped off from helloworld.c, in the gtk tutorial.

  compile with:
     gcc -Wall -g menus.c -o menus `gtk-config --cflags`  `gtk-config
--libs` -lm
*/
     #include <gtk/gtk.h>
      
     char *luggage="A string attached to the toplevel window.";
     
     void quitbutton( GtkWidget *widget, gpointer data )
     {
       printf ("Quitting!\n");
     }

     /* Another callback */
     void destroy( GtkWidget *widget,  gpointer   data )
     {
         gtk_main_quit();
     }

void traceparents(GtkWidget *widget)
/* **********************************************************  
*   Print out all of a widget's parentage, up to the top.
********************************************************** */ 
{ 
  GtkWidget *temp=widget;
  printf("starting parent hunt...\n");
  while (temp){
    printf("widget name: %s\n",gtk_widget_get_name(temp));
    printf("widget parent address: %X\n",temp->parent);
    temp=temp->parent;
  }
}/* traceparents */

void on_choice_activate          (GtkMenuItem     *menuitem,
                                  gpointer         user_data)
/**************************************************************
**************************************************************/
{
  char *luggage;
  GtkWidget *top;
  traceparents(GTK_WIDGET(menuitem));
  
  top=gtk_widget_get_toplevel (GTK_WIDGET(menuitem));
  luggage=gtk_object_get_data (GTK_OBJECT (top), "luggage");
  printf("toplevel data luggage is:%s\n",luggage);
}
  
GtkWidget *addmenu()
/* *************************************************** 
*   Create the menu bar and return it.
*************************************************** */
{
  GtkWidget *MainMenu,*FileMenu,*FileMenuSub,*choice;
  
  MainMenu = gtk_menu_bar_new ();
  
  gtk_widget_show (MainMenu);

  FileMenu = gtk_menu_item_new_with_label ("File");
  gtk_widget_show (FileMenu);
  gtk_container_add (GTK_CONTAINER (MainMenu), FileMenu);

  FileMenuSub = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (FileMenu), FileMenuSub);
  
  choice = gtk_menu_item_new_with_label ("Open");
  gtk_widget_show (choice);
  gtk_container_add (GTK_CONTAINER (FileMenuSub), choice);
  gtk_signal_connect (GTK_OBJECT (choice), "activate",
                      GTK_SIGNAL_FUNC (on_choice_activate),
                      NULL);

  choice = gtk_menu_item_new_with_label ("close");
  gtk_widget_show (choice);
  gtk_container_add (GTK_CONTAINER (FileMenuSub), choice);
  gtk_signal_connect (GTK_OBJECT (choice), "activate",
                      GTK_SIGNAL_FUNC (on_choice_activate),
                      NULL);
  return(MainMenu);
}

     int main( int   argc,
               char *argv[] )
     {
         /* GtkWidget is the storage type for widgets */
         GtkWidget *window;
         GtkWidget *vbox,*button;
         char *lugcheck;
         
         /* This is called in all GTK applications. Arguments are parsed
          * from the command line and are returned to the application. */
         gtk_init(&argc, &argv);
         
         /* create a new window */
         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
         gtk_object_set_data (GTK_OBJECT (window), "luggage", luggage);
         
         lugcheck=gtk_object_get_data (GTK_OBJECT (window), "luggage");
         printf("Echo check, toplevel data luggage has been set
as:%s\n",lugcheck);

         
         vbox = gtk_vbox_new (FALSE, 0);
         gtk_widget_show (vbox);
         gtk_container_add (GTK_CONTAINER (window), vbox);
         gtk_box_pack_start (GTK_BOX (vbox), addmenu(), FALSE, FALSE, 0);
                  
         /* Here we connect the "destroy" event to a signal handler.  
          * This event occurs when we call gtk_widget_destroy() on the
window,
          * or if we return 'FALSE' in the "delete_event" callback. */
         gtk_signal_connect (GTK_OBJECT (window), "destroy",
                             GTK_SIGNAL_FUNC (destroy), NULL);
         
         /* Sets the border width of the window. */
         gtk_container_border_width (GTK_CONTAINER (window), 30);
         
         /* Creates a new button with the label "Stop button". */
         button = gtk_button_new_with_label ("Stop button.");
         gtk_signal_connect (GTK_OBJECT (button), "clicked",
                             GTK_SIGNAL_FUNC (quitbutton), NULL);
         
         /* This will cause the window to be destroyed by calling
          * gtk_widget_destroy(window) when "clicked".  Again, the
destroy
          * signal could come from here, or the window manager. */
         gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
                                    GTK_SIGNAL_FUNC (gtk_widget_destroy),
                                    GTK_OBJECT (window));
         
         /* This packs the button into the vbox. */
         gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
         
         /* The final step is to display this newly created widget. */
         gtk_widget_show (button);
         
         /* and the window */
         gtk_widget_show (window);
         
         gtk_main ();
         
         return(0);
     }
     /* example-end */




___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: http://dl.www.juno.com/dynoget/tagj.



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