#include #include #include /* * Simple, menu-only, action */ #define RECENT_TYPE_ACTION (recent_action_get_type ()) #define RECENT_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_TYPE_ACTION, RecentAction)) #define RECENT_IS_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RECENT_TYPE_ACTION)) typedef struct _RecentAction RecentAction; typedef struct _RecentActionClass RecentActionClass; struct _RecentAction { GtkAction parent_instance; GtkWidget *menu; }; struct _RecentActionClass { GtkActionClass parent_class; void (*item_activated) (RecentAction *action, GtkRecentInfo *info); }; static guint item_activated = 0; G_DEFINE_TYPE (RecentAction, recent_action, GTK_TYPE_ACTION); static void recent_chooser_item_activated_cb (GtkRecentChooser *chooser, GtkAction *action) { GtkRecentInfo *current; current = gtk_recent_chooser_get_current_item (chooser); if (!current) return; g_signal_emit (action, item_activated, 0, current); gtk_recent_info_unref (current); } static GtkWidget * recent_action_create_menu_item (GtkAction *action) { GtkWidget *menuitem; GtkWidget *chooser_menu; chooser_menu = gtk_recent_chooser_menu_new (); g_signal_connect (chooser_menu, "item-activated", G_CALLBACK (recent_chooser_item_activated_cb), action); menuitem = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL); gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), chooser_menu); return menuitem; } static void recent_action_class_init (RecentActionClass *klass) { GObjectClass *gobject_class; GtkActionClass *action_class; gobject_class = G_OBJECT_CLASS (klass); action_class = GTK_ACTION_CLASS (klass); action_class->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM; action_class->create_menu_item = recent_action_create_menu_item; item_activated = g_signal_new ("item-activated", G_OBJECT_CLASS_TYPE (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, G_STRUCT_OFFSET (RecentActionClass, item_activated), NULL, NULL, g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, GTK_TYPE_RECENT_INFO); } static void recent_action_init (RecentAction *action) { } static GtkAction * recent_action_new (const gchar *name) { return g_object_new (RECENT_TYPE_ACTION, "name", name, "label", "Open _Recent", "tooltip", NULL, "stock-id", NULL, NULL); } /* * Real application code */ static void recent_item_activated_cb (RecentAction *action, GtkRecentInfo *info, gpointer user_data) { g_message ("*** recent item activated: %s ***", gtk_recent_info_get_display_name (info)); } static void activate_cb (GtkAction *action, gpointer user_data) { g_message ("*** action activated: %s ***", gtk_action_get_name (action)); } static GtkActionEntry entries[] = { { "FileMenu", NULL, "_File" }, { "New", GTK_STOCK_NEW, "_New", "N", NULL, G_CALLBACK (activate_cb) }, { "Open", GTK_STOCK_OPEN, "_Open", "O", NULL, G_CALLBACK (activate_cb) }, { "OpenRecent", NULL, "Open _Recent" }, { "Quit", GTK_STOCK_QUIT, "_Quit", "Q", NULL, G_CALLBACK (gtk_main_quit) } }; static const gchar *ui_info = "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""; int main (int argc, char *argv[]) { GtkWidget *window; GtkUIManager *manager; GtkAction *recent; GtkActionGroup *actions; GtkWidget *box; GError *err; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (window), 400, 300); g_signal_connect (window, "delete-event", G_CALLBACK (gtk_widget_destroy), NULL); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); recent = recent_action_new ("OpenRecent"); g_signal_connect (recent, "item-activated", G_CALLBACK (recent_item_activated_cb), NULL); actions = gtk_action_group_new ("Actions"); gtk_action_group_add_actions (actions, entries, G_N_ELEMENTS (entries), NULL); gtk_action_group_add_action (actions, recent); manager = gtk_ui_manager_new (); gtk_ui_manager_insert_action_group (manager, actions, 0); gtk_window_add_accel_group (GTK_WINDOW (window), gtk_ui_manager_get_accel_group (manager)); err = NULL; if (!gtk_ui_manager_add_ui_from_string (manager, ui_info, -1, &err)) { g_warning ("Unable to create menus: %s", err->message); g_error_free (err); } box = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), box); gtk_box_pack_start (GTK_BOX (box), gtk_ui_manager_get_widget (manager, "/MenuBar"), FALSE, FALSE, 0); gtk_widget_show_all (box); gtk_widget_show (window); gtk_main (); return EXIT_SUCCESS; }