Re: [gtk-list] GtkItemFactory -- need example



On 23 Jul 1998, Thomas Mailund Jensen wrote:

> 
> I am trying to deduce how to use gtk item factory, which isn't too
> easy with only the source.  I could really use an example.  Is there
> anywhere I can get such?

the CVS gimp uses GtkItemFactory already, as well, as GLE.

i guess gle/gleted.c gives some good example on the factories use,
since it uses it for a menubar and a popup, but since the code isn't
too obvious, i have assembled a small example below.
note, the "<RadioItem>" type for a factory entry doesn't yet work.
i have yet to get around to implement the radio items linking.
but once that works, you should be able to do

  { "/Choice/One",    "",   NULL, 0, "<RadioItem>" },
  { "/Choice/Two",    "",   NULL, 0, "<RadioItem:/Choice/One>" },
  { "/Choice/Three",  "",   NULL, 0, "<RadioItem:/Choice/Two>" },

so that you got three radio items to select from.




static GtkItemFactoryEntry menubar_entries[] =
{
  { "/File/Test",       "",             my_cb, 1,  "<Item>" },
  { "/File/-----",      "",             NULL,  0,  "<Separator>" },
  { "/File/Close",      "<Ctrl>C",      my_cb, 2,  "<Item>" },
};
static guint n_menubar_entries = sizeof (menubar_entries) / sizeof (menubar_entries[0]);
GtkItemFactory *menubar_factory;


{
[assmuing you have a window and a vbox in that window]

  /* read the menurc */
  gtk_item_factory_parse_rc ("~/.my-app-menurc");

  /* create the menu bar
   */
  menubar_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR,
                                          "ExampleApp",
                                          NULL);
  gtk_window_add_accel_group (GTK_WINDOW (window),
                              menubar_factory->accel_group);
  gtk_item_factory_create_items (menubar_factory,
                                 n_menubar_entries,
                                 menubar_entries,
                                 window);
  gtk_container_add_with_args (GTK_CONTAINER (vbox), menubar_factory->widget,
                               "expand", FALSE,
                               "fill", TRUE,
                               "position", 0,
                               NULL);
  gtk_widget_show (menubar_factory->widget);
  /* don't forget to handle destruction */
  gtk_signal_connect (GTK_OBJECT (window),
                      "destroy",
                      my_shutdown,
                      menubar_factory);

}


void
my_shutdown (GtkWindow      *window,
             GtkItemFactory *menubar_factory)
{
  /* get rid of the factory */
  gtk_object_unref (GTK_OBJECT (menubar_factory));
  
  /* dump our applications menurc */
  gtk_item_factory_dump_rc ("~/.my-app-menurc", NULL, TRUE);
}

void
my_cb (gpointer   callback_data,
       guint      callback_action,
       GtkWidget *widget)
{
  `callback_data' is the window pointer that got passed to
    gtk_item_factory_create_items()
  `callback_action' will be 2 if /File/Close got clicked, and 1 for /File/Test
  `widget' is the associated menu item that triggered invocation of my_cb()
    through its ::activate signal.
}
  

      

> 
> /mailund 
> 

i hope that helps, feel free to ask more questions though ;)



---
ciaoTJ



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