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

Re: itemfactory question



> > For the prototype callback, I use always:
> >
> > void menu_callback(gpointer cb_data, guint action, GtkWidget * w)
> > {
> >   switch (action)
> >     {
> >     case ABOUT:
> >       ...
> >       break;
> >     case EXIT:
> >       gtk_main_quit();
> >       break;
> >       ...
> >     }
> > }
> >
> >
> 
> This example implies that there is one call back per menu, but the
> itemFactoryEntry allows a callback to be specified on each item?
> Are you saying I can only specify a call back on the menu?
NO
This function can be used for only one entry.

A little example:

enum
{
  ABOUT,
  EXIT
};

GtkWidget* create_window() {
  GtkWidget *win;
  GtkItemFactory *menu;
  GtkItemFactoryEntry menu_entries[] =
  {
    "/About", NULL, menu_callback1, ABOUT, "<Item>",
    "/Exit", NULL, menu_callback2, EXIT, "<Item>"
  };

  menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", NULL);
  gtk_item_factory_create_items(menu, 2, menu_entries, NULL);

  win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_object_set_data(GTK_OBJECT(win), "menu", menu);
  gtk_widget_ref(win);
  gtk_signal_connect (GTK_OBJECT (win), "destroy", GTK_SIGNAL_FUNC
(gtk_main_quit), NULL);
  gtk_signal_connect (GTK_OBJECT (win), "delete_event", GTK_SIGNAL_FUNC
(gtk_main_quit), NULL);
  gtk_signal_connect (GTK_OBJECT (win), "button_press_event",
		      GTK_SIGNAL_FUNC (popup_menu), NULL);
  return win;
}

void menu_callback1(gpointer cb_data, guint action, GtkWidget * w)
{
        create_about();
}

void menu_callback2(gpointer cb_data, guint action, GtkWidget * w)
{
        gtk_main_quit();
}

static gint popup_menu(GtkWidget *w, GdkEventButton *e) 
{
  GdkModifierType modmask;
  GtkItemFactory *menu;

  switch(e->button) {
  case 1:
    is_motion_ok = TRUE;
    break;
  case 3:
    menu = gtk_object_get_data(GTK_OBJECT(w), "menu");
    gtk_item_factory_popup(menu, (gint)(e->x_root), (gint)(e->y_root),
3, GDK_CURRENT_TIME);
    break;
  }
  return 0;
}


I think that this example is bad because we don't used 
the fourth ItemFactoryItem's attribut (the action)
Here, we need 2 functions whereas we could use only one function
with a 'switch'
Perhaps, I don't understand your problem ???



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