Keep count of the displayed items to build the menu items mnemonic instead

2007-02-08  Emmanuele Bassi  <ebassi@gnome.org>

	* gtk/gtkrecentchoosermenu.c (idle_populate_func): Keep count
	of the displayed items to build the menu items mnemonic instead
	of the overall item count. (#377164)

	(idle_populate_clean_up): Append a menu item if all the items
	got filtered in the idle populate function. (#405696)

svn path=/trunk/; revision=17277
This commit is contained in:
Emmanuele Bassi 2007-02-08 14:22:20 +00:00 committed by Emmanuele Bassi
parent 8a8202ce13
commit ada1782221
4 changed files with 219 additions and 1 deletions

View File

@ -1,3 +1,16 @@
2007-02-08 Emmanuele Bassi <ebassi@gnome.org>
* gtk/gtkrecentchoosermenu.c (idle_populate_func): Keep count
of the displayed items to build the menu items mnemonic instead
of the overall item count. (#377164)
(idle_populate_clean_up): Append a menu item if all the items
got filtered in the idle populate function. (#405696)
* tests/Makefile.am:
* tests/testrecentchoosermenu.c: Add a test for the
GtkRecentChooserMenu widget.
Thu Feb 8 13:07:08 2007 Tim Janik <timj@imendio.com>
* applied patch from Xan Lopez which adds GTK_BUTTONBOX_CENTER to

View File

@ -1000,6 +1000,7 @@ typedef struct
GList *items;
gint n_items;
gint loaded_items;
gint displayed_items;
GtkRecentChooserMenu *menu;
} MenuPopulateData;
@ -1034,6 +1035,8 @@ idle_populate_func (gpointer data)
gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
gtk_widget_show (item);
pdata->displayed_items = 1;
/* no items: add a placeholder menu */
GDK_THREADS_LEAVE ();
@ -1077,7 +1080,7 @@ idle_populate_func (gpointer data)
item = gtk_recent_chooser_menu_create_item (pdata->menu,
info,
pdata->loaded_items);
pdata->displayed_items);
if (!item)
goto check_and_return;
@ -1091,6 +1094,8 @@ idle_populate_func (gpointer data)
*/
gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
gtk_widget_show (item);
pdata->displayed_items += 1;
/* mark the menu item as one of our own */
g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
@ -1123,6 +1128,25 @@ check_and_return:
static void
idle_populate_clean_up (gpointer data)
{
MenuPopulateData *pdata = data;
if (!pdata->displayed_items)
{
GtkWidget *item;
item = gtk_menu_item_new_with_label (_("No items found"));
gtk_widget_set_sensitive (item, FALSE);
/* we also mark this item, so that it gets removed when rebuilding
* the menu on the next map event
*/
g_object_set_data (G_OBJECT (item), "gtk-recent-menu-mark",
GINT_TO_POINTER (1));
gtk_menu_shell_prepend (GTK_MENU_SHELL (pdata->menu), item);
gtk_widget_show (item);
}
g_slice_free (MenuPopulateData, data);
}
@ -1138,6 +1162,7 @@ gtk_recent_chooser_menu_populate (GtkRecentChooserMenu *menu)
pdata->items = NULL;
pdata->n_items = 0;
pdata->loaded_items = 0;
pdata->displayed_items = 0;
pdata->menu = menu;
menu->priv->icon_size = get_icon_size_for_widget (GTK_WIDGET (menu));

View File

@ -58,6 +58,7 @@ noinst_PROGRAMS = \
testprint \
testrgb \
testrecentchooser \
testrecentchoosermenu \
testrichtext \
testselection \
$(testsocket_programs) \
@ -116,6 +117,7 @@ testnotebookdnd_DEPENDENCIES = $(TEST_DEPS)
testnouiprint_DEPENDENCIES = $(TEST_DEPS)
testprint_DEPENDENCIES = $(TEST_DEPS)
testrecentchooser_DEPENDENCIES = $(TEST_DEPS)
testrecentchoosermenu_DEPENDENCIES = $(TEST_DEPS)
testrgb_DEPENDENCIES = $(TEST_DEPS)
testrichtext_DEPENDENCIES = $(TEST_DEPS)
testselection_DEPENDENCIES = $(TEST_DEPS)
@ -169,6 +171,7 @@ testnotebookdnd_LDADD = $(LDADDS)
testnouiprint_LDADD = $(LDADDS)
testprint_LDADD = $(LDADDS)
testrecentchooser_LDADD = $(LDADDS)
testrecentchoosermenu_LDADD = $(LDADDS)
testrgb_LDADD = $(LDADDS)
testrichtext_LDADD = $(LDADDS)
testselection_LDADD = $(LDADDS)
@ -279,6 +282,9 @@ testgrouping_SOURCES = \
testtoooltips_SOURCES = \
testtooltips.c
testrecentchoosermenu_SOURCES = \
testrecentchoosermenu.c
EXTRA_DIST = \
prop-editor.h \
testgtk.1 \

View File

@ -0,0 +1,174 @@
/* testrecentchoosermenu.c - Test GtkRecentChooserMenu
* Copyright (C) 2007 Emmanuele Bassi <ebassi@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <gtk/gtk.h>
static GtkRecentManager *manager = NULL;
static GtkWidget *window = NULL;
static GtkWidget *label = NULL;
static void
item_activated_cb (GtkRecentChooser *chooser,
gpointer data)
{
GtkRecentInfo *info;
GString *text;
gchar *label_text;
info = gtk_recent_chooser_get_current_item (chooser);
if (!info)
{
g_warning ("Unable to retrieve the current item, aborting...");
return;
}
text = g_string_new ("Selected recent item:\n");
g_string_append_printf (text, " URI: %s\n",
gtk_recent_info_get_uri (info));
g_string_append_printf (text, " MIME Type: %s\n",
gtk_recent_info_get_mime_type (info));
label_text = g_string_free (text, FALSE);
gtk_label_set_text (GTK_LABEL (label), label_text);
gtk_recent_info_unref (info);
g_free (label_text);
}
static GtkWidget *
create_recent_chooser_menu (void)
{
GtkWidget *menu;
menu = gtk_recent_chooser_menu_new_for_manager (manager);
gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (menu), TRUE);
gtk_recent_chooser_set_show_icons (GTK_RECENT_CHOOSER (menu), TRUE);
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu),
GTK_RECENT_SORT_MRU);
gtk_recent_chooser_menu_set_show_numbers (GTK_RECENT_CHOOSER_MENU (menu),
TRUE);
g_signal_connect (menu, "item-activated",
G_CALLBACK (item_activated_cb),
NULL);
gtk_widget_show (menu);
return menu;
}
static GtkWidget *
create_file_menu (GtkAccelGroup *accelgroup)
{
GtkWidget *menu;
GtkWidget *menuitem;
GtkWidget *recentmenu;
menu = gtk_menu_new ();
menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_NEW, accelgroup);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_OPEN, accelgroup);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
menuitem = gtk_menu_item_new_with_mnemonic ("_Open Recent");
recentmenu = create_recent_chooser_menu ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), recentmenu);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
menuitem = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, accelgroup);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
gtk_widget_show (menu);
return menu;
}
int
main (int argc, char *argv[])
{
GtkWidget *box;
GtkWidget *menubar;
GtkWidget *menuitem;
GtkWidget *menu;
GtkWidget *button;
GtkAccelGroup *accel_group;
gtk_init (&argc, &argv);
manager = gtk_recent_manager_get_default ();
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
gtk_window_set_title (GTK_WINDOW (window), "Recent Chooser Menu Test");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
box = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show (box);
menubar = gtk_menu_bar_new ();
gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
gtk_widget_show (menubar);
menu = create_file_menu (accel_group);
menuitem = gtk_menu_item_new_with_mnemonic ("_File");
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
gtk_widget_show (menuitem);
menu = create_recent_chooser_menu ();
menuitem = gtk_menu_item_new_with_mnemonic ("_Recently Used");
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
gtk_widget_show (menuitem);
label = gtk_label_new ("No recent item selected");
gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
gtk_widget_show (label);
button = gtk_button_new_with_label ("Close");
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
gtk_box_pack_end (GTK_BOX (box), button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}