[rhythmbox] add mnemonics/accelerators for focusing search entries



commit 0857631e65c4cfa2ab8537468e739b0a5e8cf21b
Author: Jonathan Matthew <jonathan d14n org>
Date:   Sat Nov 5 22:41:54 2011 +1000

    add mnemonics/accelerators for focusing search entries
    
    re-enable alt-s, which disappeared with the label for the search,
    (bug #663354) and add ctrl-f (bug #427592).

 plugins/grilo/rb-grilo-source.c |   14 ++++++++++
 widgets/rb-search-entry.c       |   52 +++++++++++++++++++++++++++++++-------
 widgets/rb-search-entry.h       |    2 +
 widgets/rb-source-toolbar.c     |   20 +++++++++++++++
 4 files changed, 78 insertions(+), 10 deletions(-)
---
diff --git a/plugins/grilo/rb-grilo-source.c b/plugins/grilo/rb-grilo-source.c
index 57faba3..3fd6ae8 100644
--- a/plugins/grilo/rb-grilo-source.c
+++ b/plugins/grilo/rb-grilo-source.c
@@ -85,6 +85,7 @@ static void notify_sort_order_cb (GObject *object, GParamSpec *pspec, RBGriloSou
 
 static void impl_delete_thyself (RBDisplayPage *page);
 static void impl_selected (RBDisplayPage *page);
+static void impl_deselected (RBDisplayPage *page);
 
 static RBEntryView *impl_get_entry_view (RBSource *source);
 
@@ -179,6 +180,7 @@ rb_grilo_source_class_init (RBGriloSourceClass *klass)
 
 	page_class->delete_thyself = impl_delete_thyself;
 	page_class->selected = impl_selected;
+	page_class->deselected = impl_deselected;
 
 	source_class->impl_get_entry_view = impl_get_entry_view;
 
@@ -1082,6 +1084,18 @@ impl_selected (RBDisplayPage *page)
 		source->priv->done_initial_browse = TRUE;
 		start_browse (source, NULL, NULL, 0);
 	}
+
+	rb_search_entry_set_mnemonic (source->priv->search_entry, TRUE);
+}
+
+static void
+impl_deselected (RBDisplayPage *page)
+{
+	RBGriloSource *source = RB_GRILO_SOURCE (page);
+
+	RB_DISPLAY_PAGE_CLASS (rb_grilo_source_parent_class)->deselected (page);
+
+	rb_search_entry_set_mnemonic (source->priv->search_entry, FALSE);
 }
 
 static RBEntryView *
diff --git a/widgets/rb-search-entry.c b/widgets/rb-search-entry.c
index a0b5256..719188a 100644
--- a/widgets/rb-search-entry.c
+++ b/widgets/rb-search-entry.c
@@ -57,6 +57,7 @@ static void rb_search_entry_clear_cb (GtkEntry *entry,
 				      GdkEvent *event,
 				      RBSearchEntry *search_entry);
 static void rb_search_entry_update_icons (RBSearchEntry *entry);
+static void rb_search_entry_widget_grab_focus (GtkWidget *widget);
 
 struct RBSearchEntryPrivate
 {
@@ -69,8 +70,6 @@ struct RBSearchEntryPrivate
 	gboolean searching;
 
 	guint timeout;
-
-	gboolean is_a11y_theme;
 };
 
 G_DEFINE_TYPE (RBSearchEntry, rb_search_entry, GTK_TYPE_HBOX)
@@ -109,12 +108,15 @@ static void
 rb_search_entry_class_init (RBSearchEntryClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
 	object_class->constructed = rb_search_entry_constructed;
 	object_class->finalize = rb_search_entry_finalize;
 	object_class->set_property = rb_search_entry_set_property;
 	object_class->get_property = rb_search_entry_get_property;
 
+	widget_class->grab_focus = rb_search_entry_widget_grab_focus;
+
 	/**
 	 * RBSearchEntry::search:
 	 * @entry: the #RBSearchEntry
@@ -208,19 +210,12 @@ static void
 rb_search_entry_constructed (GObject *object)
 {
 	RBSearchEntry *entry;
-	GtkSettings *settings;
-	char *theme;
 
 	RB_CHAIN_GOBJECT_METHOD (rb_search_entry_parent_class, constructed, object);
 
 	entry = RB_SEARCH_ENTRY (object);
 
-	settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (entry)));
-	g_object_get (settings, "gtk-theme-name", &theme, NULL);
-	entry->priv->is_a11y_theme = strncmp (theme, "HighContrast", strlen ("HighContrast")) == 0 ||
-					strncmp (theme, "LowContrast", strlen ("LowContrast")) == 0;
-	g_free (theme);
-
+	gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
 	entry->priv->entry = gtk_entry_new ();
 	g_signal_connect_object (GTK_ENTRY (entry->priv->entry),
 				 "icon-press",
@@ -537,6 +532,12 @@ rb_search_entry_grab_focus (RBSearchEntry *entry)
 }
 
 static void
+rb_search_entry_widget_grab_focus (GtkWidget *widget)
+{
+	rb_search_entry_grab_focus (RB_SEARCH_ENTRY (widget));
+}
+
+static void
 rb_search_entry_clear_cb (GtkEntry *entry,
 			  GtkEntryIconPosition icon_pos,
 			  GdkEvent *event,
@@ -548,3 +549,34 @@ rb_search_entry_clear_cb (GtkEntry *entry,
 		rb_search_entry_set_text (search_entry, "");
 	}
 }
+
+/**
+ * rb_search_entry_set_mnemonic:
+ * @entry: a #RBSearchEntry
+ * @enable: if %TRUE, enable the mnemonic
+ *
+ * Adds or removes a mnemonic allowing the user to focus
+ * the search entry.
+ */
+void
+rb_search_entry_set_mnemonic (RBSearchEntry *entry, gboolean enable)
+{
+	GtkWidget *toplevel;
+	guint keyval;
+	gunichar accel = 0;
+
+	if (pango_parse_markup (_("_Search:"), -1, '_', NULL, NULL, &accel, NULL) && accel != 0) {
+		keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel));
+	} else {
+		keyval = gdk_unicode_to_keyval ('s');
+	}
+
+	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
+	if (gtk_widget_is_toplevel (toplevel)) {
+		if (enable) {
+			gtk_window_add_mnemonic (GTK_WINDOW (toplevel), keyval, entry->priv->entry);
+		} else {
+			gtk_window_remove_mnemonic (GTK_WINDOW (toplevel), keyval, entry->priv->entry);
+		}
+	}
+}
diff --git a/widgets/rb-search-entry.h b/widgets/rb-search-entry.h
index c307f19..2049d29 100644
--- a/widgets/rb-search-entry.h
+++ b/widgets/rb-search-entry.h
@@ -74,6 +74,8 @@ gboolean	rb_search_entry_searching(RBSearchEntry *entry);
 
 void		rb_search_entry_grab_focus (RBSearchEntry *entry);
 
+void		rb_search_entry_set_mnemonic (RBSearchEntry *entry, gboolean enable);
+
 G_END_DECLS
 
 #endif /* __RB_SEARCH_ENTRY_H */
diff --git a/widgets/rb-source-toolbar.c b/widgets/rb-source-toolbar.c
index 6849aa8..92bbf92 100644
--- a/widgets/rb-source-toolbar.c
+++ b/widgets/rb-source-toolbar.c
@@ -125,6 +125,17 @@ source_selected_cb (GObject *object, GParamSpec *pspec, RBSourceToolbar *toolbar
 			gtk_widget_show_all (GTK_WIDGET (toolbar->priv->toolbar));
 		}
 
+		if (toolbar->priv->search_entry != NULL) {
+			rb_search_entry_set_mnemonic (toolbar->priv->search_entry, TRUE);
+
+			gtk_widget_add_accelerator (GTK_WIDGET (toolbar->priv->search_entry),
+						    "grab-focus",
+						    gtk_ui_manager_get_accel_group (toolbar->priv->ui_manager),
+						    gdk_unicode_to_keyval ('f'),
+						    GDK_CONTROL_MASK,
+						    0);
+		}
+
 		if (toolbar->priv->search_group != NULL) {
 			if (toolbar->priv->search_value != -1) {
 				gtk_radio_action_set_current_value (toolbar->priv->search_group,
@@ -158,6 +169,15 @@ source_selected_cb (GObject *object, GParamSpec *pspec, RBSourceToolbar *toolbar
 			gtk_container_remove (GTK_CONTAINER (toolbar), toolbar->priv->toolbar);
 		}
 
+		if (toolbar->priv->search_entry != NULL) {
+			rb_search_entry_set_mnemonic (toolbar->priv->search_entry, FALSE);
+
+			gtk_widget_remove_accelerator (GTK_WIDGET (toolbar->priv->search_entry),
+						       gtk_ui_manager_get_accel_group (toolbar->priv->ui_manager),
+						       gdk_unicode_to_keyval ('f'),
+						       GDK_CONTROL_MASK);
+		}
+
 		if (toolbar->priv->search_group != NULL) {
 			if (toolbar->priv->search_change_cb_id != 0) {
 				g_signal_handler_disconnect (toolbar->priv->search_group,



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