GtkNotebook accelerators.



As discussed earlier on gtk-devel-list and in bugzilla GtkNotebook lacks
an easy way to set keyboard accelerators to switch between pages.

Here is a patch that adds support for setting the accelerator keys and
easy construction of labels using underlined labels.

In order to make stuff work i had to do a small "hack" using the
grab_focus signals. Credits to jamesh for this idea.

Comments?

/ Alex

Index: gtknotebook.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtknotebook.c,v
retrieving revision 1.80
diff -u -p -r1.80 gtknotebook.c
--- gtknotebook.c	2001/02/02 17:53:29	1.80
+++ gtknotebook.c	2001/02/26 16:23:57
@@ -3534,6 +3534,30 @@ gtk_notebook_append_page (GtkNotebook *n
 }

 /**
+ * gtk_notebook_append_page_label:
+ * @notebook: a #GtkNotebook
+ * @child: the #GtkWidget to use as the contents of the page.
+ * @uline_label: the text of the label, use underscore to make
+ *               keyboard accelerators.
+ * @accel_group: the accel group to place the keyboard accelerators
+ *               in. May be null.
+ *
+ * Appends a page to @notebook, with the specified label text.
+ **/
+void
+gtk_notebook_append_page_label (GtkNotebook *notebook,
+				GtkWidget   *child,
+				const gchar *uline_label,
+				GtkAccelGroup *accel_group)
+{
+  g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+  g_return_if_fail (uline_label != NULL);
+
+  gtk_notebook_insert_page_label (notebook, child, uline_label, accel_group, -1);
+}
+
+/**
  * gtk_notebook_append_page_menu:
  * @notebook: a #GtkNotebook
  * @child: the #GtkWidget to use as the contents of the page.
@@ -3570,7 +3594,7 @@ gtk_notebook_append_page_menu (GtkNotebo
  * @tab_label: the #GtkWidget to be used as the label for the page,
  *             or %NULL to use the default label, 'page N'.
  *
- * Prepends a page to @noteobook.
+ * Prepends a page to @notebook.
  **/
 void
 gtk_notebook_prepend_page (GtkNotebook *notebook,
@@ -3585,6 +3609,30 @@ gtk_notebook_prepend_page (GtkNotebook *
 }

 /**
+ * gtk_notebook_prepend_page_label:
+ * @notebook: a #GtkNotebook
+ * @child: the #GtkWidget to use as the contents of the page.
+ * @uline_label: the text of the label, use underscore to make
+ *               keyboard accelerators.
+ * @accel_group: the accel group to place the keyboard accelerators
+ *               in. May be null.
+ *
+ * Prepends a page to @notebook, with the specified label text.
+ **/
+void
+gtk_notebook_prepend_page_label (GtkNotebook *notebook,
+				 GtkWidget   *child,
+				 const gchar *uline_label,
+				 GtkAccelGroup *accel_group)
+{
+  g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+  g_return_if_fail (uline_label != NULL);
+
+  gtk_notebook_insert_page_label (notebook, child, uline_label, accel_group, 0);
+}
+
+/**
  * gtk_notebook_prepend_page_menu:
  * @notebook: a #GtkNotebook
  * @child: the #GtkWidget to use as the contents of the page.
@@ -3639,6 +3687,48 @@ gtk_notebook_insert_page (GtkNotebook *n
 }

 /**
+ * gtk_notebook_insert_page_label:
+ * @notebook: a #GtkNotebook
+ * @child: the #GtkWidget to use as the contents of the page.
+ * @uline_label: the text of the label, use underscore to make
+ *               keyboard accelerators.
+ * @accel_group: the accel group to place the keyboard accelerators
+ *               in. May be null.
+ * @position: the index (starting at 0) at which to insert the page,
+ *            or -1 to append the page after all other pages.
+ *
+ * Inserts a page to @notebook, with the specified label text.
+ **/
+void
+gtk_notebook_insert_page_label (GtkNotebook *notebook,
+				GtkWidget   *child,
+				const gchar *uline_label,
+				GtkAccelGroup *accel_group,
+				gint         position)
+{
+  GtkWidget *tab_label;
+  guint keyval;
+
+  g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+  g_return_if_fail (uline_label != NULL);
+
+  tab_label = gtk_label_new (uline_label);
+  keyval = gtk_label_parse_uline (GTK_LABEL (tab_label),
+				  uline_label);
+
+  gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, position);
+
+  if (accel_group)
+    gtk_notebook_set_tab_accel (notebook,
+				child,
+				keyval,
+				GDK_MOD1_MASK,
+				accel_group);
+
+}
+
+/**
  * gtk_notebook_insert_page_menu:
  * @notebook: a #GtkNotebook
  * @child: the #GtkWidget to use as the contents of the page.
@@ -4380,6 +4470,100 @@ gtk_notebook_set_tab_label_text (GtkNote

   if (tab_text)
     tab_label = gtk_label_new (tab_text);
+  gtk_notebook_set_tab_label (notebook, child, tab_label);
+}
+
+static gboolean
+grab_focus_callback (GtkWidget *child, gpointer data)
+{
+  GtkNotebook *notebook = GTK_NOTEBOOK (data);
+  GtkNotebookPage *page;
+  GList *list;
+
+  list = CHECK_FIND_CHILD (notebook, child);
+  if (!list)
+    return TRUE;
+
+  page = list->data;
+
+  gtk_notebook_switch_page (notebook, page,  -1);
+  return TRUE;
+}
+
+void
+gtk_notebook_set_tab_accel (GtkNotebook   *notebook,
+			    GtkWidget     *child,
+			    guint          keyval,
+			    guint          modifiers,
+			    GtkAccelGroup *accel_group)
+{
+  GList *list;
+  GtkNotebookPage *page;
+
+  g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  g_return_if_fail (child != NULL);
+
+  list = CHECK_FIND_CHILD (notebook, child);
+  if (!list)
+    return;
+
+  page = list->data;
+
+  /* Since we cannot add arguments to the signal emitted
+   * by the accelerator we misuse grab_focus of the child
+   * widget to get the child pointer.
+   */
+
+  gtk_widget_add_accelerator (child,
+			      "grab_focus",
+			      accel_group,
+			      keyval,
+			      modifiers,
+			      GTK_ACCEL_LOCKED);
+
+  gtk_signal_connect (GTK_OBJECT (child),
+		      "grab_focus",
+		      (GtkSignalFunc) grab_focus_callback,
+		      notebook);
+}
+
+/**
+ * gtk_notebook_set_tab_label_text_accel:
+ * @notebook: a #GtkNotebook
+ * @child: the page
+ * @uline_text: the label text, with underline
+ * @accel_group: The acceleration group to set the accelerator in
+ *
+ * Creates a new label and sets it as the tab label for the page
+ * containing @child. The label text is parsed looking for
+ * an underscore, and an accelerator <ALT>key is added that
+ * shows the page.
+ **/
+void
+gtk_notebook_set_tab_label_text_accel (GtkNotebook *notebook,
+				       GtkWidget   *child,
+				       const gchar   *uline_text,
+				       GtkAccelGroup *accel_group)
+{
+  GtkWidget *tab_label = NULL;
+  guint keyval;
+
+  g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+
+  if (uline_text)
+    {
+      tab_label = gtk_label_new (uline_text);
+      keyval = gtk_label_parse_uline (GTK_LABEL (tab_label),
+                                      uline_text);
+
+      if (accel_group)
+	gtk_notebook_set_tab_accel (notebook,
+				    child,
+				    keyval,
+				    GDK_MOD1_MASK,
+				    accel_group);
+
+    }
   gtk_notebook_set_tab_label (notebook, child, tab_label);
 }

Index: gtknotebook.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtknotebook.h,v
retrieving revision 1.23
diff -u -p -r1.23 gtknotebook.h
--- gtknotebook.h	2000/12/11 17:47:24	1.23
+++ gtknotebook.h	2001/02/26 16:23:57
@@ -92,34 +92,48 @@ struct _GtkNotebookClass
  *           Creation, insertion, deletion                 *
  ***********************************************************/

-GtkType gtk_notebook_get_type       (void) G_GNUC_CONST;
-GtkWidget * gtk_notebook_new        (void);
-void gtk_notebook_append_page       (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label);
-void gtk_notebook_append_page_menu  (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label,
-				     GtkWidget   *menu_label);
-void gtk_notebook_prepend_page      (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label);
-void gtk_notebook_prepend_page_menu (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label,
-				     GtkWidget   *menu_label);
-void gtk_notebook_insert_page       (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label,
-				     gint         position);
-void gtk_notebook_insert_page_menu  (GtkNotebook *notebook,
-				     GtkWidget   *child,
-				     GtkWidget   *tab_label,
-				     GtkWidget   *menu_label,
-				     gint         position);
-void gtk_notebook_remove_page       (GtkNotebook *notebook,
-				     gint         page_num);
+GtkType    gtk_notebook_get_type           (void) G_GNUC_CONST;
+GtkWidget *gtk_notebook_new                (void);
+void       gtk_notebook_append_page        (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label);
+void       gtk_notebook_append_page_label  (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    const gchar   *uline_label,
+					    GtkAccelGroup *accel_group);
+void       gtk_notebook_append_page_menu   (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label,
+					    GtkWidget     *menu_label);
+void       gtk_notebook_prepend_page       (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label);
+void       gtk_notebook_prepend_page_label (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    const gchar   *uline_label,
+					    GtkAccelGroup *accel_group);
+void       gtk_notebook_prepend_page_menu  (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label,
+					    GtkWidget     *menu_label);
+void       gtk_notebook_insert_page        (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label,
+					    gint           position);
+void       gtk_notebook_insert_page_label  (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    const gchar   *uline_label,
+					    GtkAccelGroup *accel_group,
+					    gint           position);
+void       gtk_notebook_insert_page_menu   (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    GtkWidget     *tab_label,
+					    GtkWidget     *menu_label,
+					    gint           position);
+void       gtk_notebook_remove_page        (GtkNotebook   *notebook,
+					    gint           page_num);

+
 /***********************************************************
  *            query, set current NoteebookPage             *
  ***********************************************************/
@@ -174,6 +188,15 @@ void gtk_notebook_set_tab_label
 void gtk_notebook_set_tab_label_text      (GtkNotebook *notebook,
 					   GtkWidget   *child,
 					   const gchar *tab_text);
+void gtk_notebook_set_tab_label_text_accel (GtkNotebook   *notebook,
+					    GtkWidget     *child,
+					    const gchar   *uline_text,
+					    GtkAccelGroup *accel_group);
+void gtk_notebook_set_tab_accel           (GtkNotebook   *notebook,
+					   GtkWidget     *child,
+					   guint          keyval,
+					   guint          modifiers,
+					   GtkAccelGroup *accel_group);
 GtkWidget * gtk_notebook_get_menu_label   (GtkNotebook *notebook,
 					   GtkWidget   *child);
 void gtk_notebook_set_menu_label          (GtkNotebook *notebook,





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