Add powerbox hook
[gtk-with-powerbox.git] / tests / testrecentchoosermenu.c
blob94166c6f9bb586ac884bdfb569f3909b3cf45b63
1 /* testrecentchoosermenu.c - Test GtkRecentChooserMenu
2 * Copyright (C) 2007 Emmanuele Bassi <ebassi@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "config.h"
21 #include <gtk/gtk.h>
23 static GtkRecentManager *manager = NULL;
24 static GtkWidget *window = NULL;
25 static GtkWidget *label = NULL;
27 static void
28 item_activated_cb (GtkRecentChooser *chooser,
29 gpointer data)
31 GtkRecentInfo *info;
32 GString *text;
33 gchar *label_text;
35 info = gtk_recent_chooser_get_current_item (chooser);
36 if (!info)
38 g_warning ("Unable to retrieve the current item, aborting...");
39 return;
42 text = g_string_new ("Selected recent item:\n");
43 g_string_append_printf (text, " URI: %s\n",
44 gtk_recent_info_get_uri (info));
45 g_string_append_printf (text, " MIME Type: %s\n",
46 gtk_recent_info_get_mime_type (info));
48 label_text = g_string_free (text, FALSE);
49 gtk_label_set_text (GTK_LABEL (label), label_text);
51 gtk_recent_info_unref (info);
52 g_free (label_text);
55 static GtkWidget *
56 create_recent_chooser_menu (gint limit)
58 GtkWidget *menu;
59 GtkRecentFilter *filter;
60 GtkWidget *menuitem;
62 menu = gtk_recent_chooser_menu_new_for_manager (manager);
64 if (limit > 0)
65 gtk_recent_chooser_set_limit (GTK_RECENT_CHOOSER (menu), limit);
66 gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (menu), TRUE);
67 gtk_recent_chooser_set_show_icons (GTK_RECENT_CHOOSER (menu), TRUE);
68 gtk_recent_chooser_set_show_tips (GTK_RECENT_CHOOSER (menu), TRUE);
69 gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu),
70 GTK_RECENT_SORT_MRU);
71 gtk_recent_chooser_menu_set_show_numbers (GTK_RECENT_CHOOSER_MENU (menu),
72 TRUE);
74 filter = gtk_recent_filter_new ();
75 gtk_recent_filter_set_name (filter, "Gedit files");
76 gtk_recent_filter_add_application (filter, "gedit");
77 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
78 gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (menu), filter);
80 g_signal_connect (menu, "item-activated",
81 G_CALLBACK (item_activated_cb),
82 NULL);
84 menuitem = gtk_separator_menu_item_new ();
85 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), menuitem);
86 gtk_widget_show (menuitem);
88 menuitem = gtk_menu_item_new_with_label ("Test prepend");
89 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), menuitem);
90 gtk_widget_show (menuitem);
92 menuitem = gtk_separator_menu_item_new ();
93 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
94 gtk_widget_show (menuitem);
96 menuitem = gtk_menu_item_new_with_label ("Test append");
97 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
98 gtk_widget_show (menuitem);
100 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
101 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
102 gtk_widget_show (menuitem);
104 gtk_widget_show_all (menu);
106 return menu;
109 static GtkWidget *
110 create_file_menu (GtkAccelGroup *accelgroup)
112 GtkWidget *menu;
113 GtkWidget *menuitem;
114 GtkWidget *recentmenu;
116 menu = gtk_menu_new ();
118 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_NEW, accelgroup);
119 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
120 gtk_widget_show (menuitem);
122 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_OPEN, accelgroup);
123 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
124 gtk_widget_show (menuitem);
126 menuitem = gtk_menu_item_new_with_mnemonic ("_Open Recent");
127 recentmenu = create_recent_chooser_menu (-1);
128 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), recentmenu);
129 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
130 gtk_widget_show (menuitem);
132 menuitem = gtk_separator_menu_item_new ();
133 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
134 gtk_widget_show (menuitem);
136 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, accelgroup);
137 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
138 gtk_widget_show (menuitem);
140 gtk_widget_show (menu);
142 return menu;
146 main (int argc, char *argv[])
148 GtkWidget *box;
149 GtkWidget *menubar;
150 GtkWidget *menuitem;
151 GtkWidget *menu;
152 GtkWidget *button;
153 GtkAccelGroup *accel_group;
155 gtk_init (&argc, &argv);
157 manager = gtk_recent_manager_get_default ();
159 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
160 gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
161 gtk_window_set_title (GTK_WINDOW (window), "Recent Chooser Menu Test");
162 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
164 accel_group = gtk_accel_group_new ();
165 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
167 box = gtk_vbox_new (FALSE, 0);
168 gtk_container_add (GTK_CONTAINER (window), box);
169 gtk_widget_show (box);
171 menubar = gtk_menu_bar_new ();
172 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
173 gtk_widget_show (menubar);
175 menu = create_file_menu (accel_group);
176 menuitem = gtk_menu_item_new_with_mnemonic ("_File");
177 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
178 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
179 gtk_widget_show (menuitem);
181 menu = create_recent_chooser_menu (4);
182 menuitem = gtk_menu_item_new_with_mnemonic ("_Recently Used");
183 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
184 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
185 gtk_widget_show (menuitem);
187 label = gtk_label_new ("No recent item selected");
188 gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
189 gtk_widget_show (label);
191 button = gtk_button_new_with_label ("Close");
192 g_signal_connect_swapped (button, "clicked",
193 G_CALLBACK (gtk_widget_destroy),
194 window);
195 gtk_box_pack_end (GTK_BOX (box), button, TRUE, TRUE, 0);
196 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
197 gtk_widget_grab_default (button);
198 gtk_widget_show (button);
200 gtk_widget_show (window);
202 gtk_main ();
204 return 0;