diff src/gtkutil.c @ 74990:de706755c4d7

(xg_tool_bar_button_cb): Save last modifier on widget. (xg_tool_bar_callback): Remove check for button. (update_frame_tool_bar): Put an event box in the tool bar and a button in the event box. Attach enter/leave-notify-event to the event box.
author Jan Djärv <jan.h.d@swipnet.se>
date Sat, 30 Dec 2006 19:57:52 +0000
parents 42ad3938ee1a
children 6ec19c8cca02
line wrap: on
line diff
--- a/src/gtkutil.c	Sat Dec 30 18:53:17 2006 +0000
+++ b/src/gtkutil.c	Sat Dec 30 19:57:52 2006 +0000
@@ -3351,11 +3351,6 @@
 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
 
 
-/* Callback function invoked when a tool bar item is pressed.
-   W is the button widget in the tool bar that got pressed,
-   CLIENT_DATA is an integer that is the index of the button in the
-   tool bar.  0 is the first button.  */
-
 static gboolean
 xg_tool_bar_button_cb (widget, event, user_data)
     GtkWidget      *widget;
@@ -3364,11 +3359,16 @@
 {
   /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
   gpointer ptr = (gpointer) (EMACS_INT) event->state;
-  g_object_set_data (G_OBJECT (user_data), XG_TOOL_BAR_LAST_MODIFIER, ptr);
+  g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
   return FALSE;
 }
 
 
+/* Callback function invoked when a tool bar item is pressed.
+   W is the button widget in the tool bar that got pressed,
+   CLIENT_DATA is an integer that is the index of the button in the
+   tool bar.  0 is the first button.  */
+
 static void
 xg_tool_bar_callback (w, client_data)
      GtkWidget *w;
@@ -3491,9 +3491,6 @@
   FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
   Lisp_Object help, frame;
 
-  if (! GTK_IS_BUTTON (w))
-    return FALSE;
-
   if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
     return FALSE;
 
@@ -3631,10 +3628,9 @@
 {
   int i;
   GtkRequisition old_req, new_req;
-  GList *icon_list;
-  GList *iter;
   struct x_output *x = f->output_data.x;
   int hmargin, vmargin;
+  GtkToolItem *ti;
 
   if (! FRAME_GTK_WIDGET (f))
     return;
@@ -3670,9 +3666,6 @@
 
   gtk_widget_size_request (x->toolbar_widget, &old_req);
 
-  icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
-  iter = icon_list;
-
   for (i = 0; i < f->n_tool_bar_items; ++i)
     {
 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
@@ -3683,12 +3676,16 @@
       int img_id;
       struct image *img;
       Lisp_Object image;
-      GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
-      GtkToolItem *ti = NULL;
-      GtkWidget *wvbox;
-      GList *chlist;
-
-      if (iter) iter = g_list_next (iter);
+      GtkWidget *wbutton;
+      GtkWidget *weventbox;
+
+      ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
+
+      if (ti)
+        {
+          weventbox = gtk_bin_get_child (GTK_BIN (ti));
+          wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
+        }
 
       /* If image is a vector, choose the image according to the
 	 button state.  */
@@ -3713,7 +3710,7 @@
       /* Ignore invalid image specifications.  */
       if (!valid_image_p (image))
         {
-          if (wicon) gtk_widget_hide (wicon);
+          if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
           continue;
         }
 
@@ -3723,57 +3720,49 @@
 
       if (img->load_failed_p || img->pixmap == None)
         {
-          if (wicon)
-	    gtk_widget_hide (wicon);
+          if (ti)
+	    gtk_widget_hide_all (GTK_WIDGET (ti));
 	  else
-            /* Insert an empty (non-image) button */
-	    gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget),
-				gtk_tool_button_new (NULL, ""),
-				i);
+            {
+              /* Insert an empty (non-image) button */
+              weventbox = gtk_event_box_new ();
+              wbutton = gtk_button_new ();
+              gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
+              gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
+              ti = gtk_tool_item_new ();
+              gtk_container_add (GTK_CONTAINER (ti), weventbox);
+              gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, i);
+            }
           continue;
         }
 
-      if (wicon)
-        {
-          /* The child of the tool bar is a button.  Inside that button
-             is a vbox.  Inside that vbox is the GtkImage.  */
-          wvbox = gtk_bin_get_child (GTK_BIN (wicon));
-          chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
-          if (chlist == NULL)
-            /* In this case, we inserted an empty button (above) with no image */
-            ti = GTK_TOOL_ITEM (wicon);
-        }
-
-      if (! wicon || ti != NULL)
+      if (ti == NULL)
         {
           GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
           gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
-
-
-          if (ti == NULL)
-            {
-              ti = gtk_tool_button_new (w, "");
-
-              gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, i);
-            }
-          else
-            gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (ti), w);
+          wbutton = gtk_button_new ();
+          gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
+          gtk_container_add (GTK_CONTAINER (wbutton), w);
+          weventbox = gtk_event_box_new ();
+          gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
+          ti = gtk_tool_item_new ();
+          gtk_container_add (GTK_CONTAINER (ti), weventbox);
+          gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, i);
 
 
           /* The EMACS_INT cast avoids a warning. */
-          g_signal_connect (GTK_WIDGET (ti), "clicked",
+          g_signal_connect (wbutton, "clicked",
                             GTK_SIGNAL_FUNC (xg_tool_bar_callback),
                             (gpointer) (EMACS_INT) i);
 
-          gtk_widget_show (GTK_WIDGET (ti));
-          gtk_widget_show (GTK_WIDGET (w));
+          gtk_widget_show_all (GTK_WIDGET (ti));
 
           /* Save the image so we can see if an update is needed when
              this function is called again.  */
           g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
                              (gpointer)img->pixmap);
 
-          g_object_set_data (G_OBJECT (ti), XG_FRAME_DATA, (gpointer)f);
+          g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
 
           /* Catch expose events to overcome an annoying redraw bug, see
              comment for xg_tool_bar_item_expose_callback.  */
@@ -3782,46 +3771,37 @@
                             G_CALLBACK (xg_tool_bar_item_expose_callback),
                             0);
 
-          gtk_widget_set_sensitive (GTK_WIDGET (ti), enabled_p);
-          gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (ti), FALSE);
+          gtk_widget_set_sensitive (wbutton, enabled_p);
+          gtk_tool_item_set_homogeneous (ti, FALSE);
           
-          while (! GTK_IS_BUTTON (w))
-            w = gtk_widget_get_parent (w);
-
           /* Callback to save modifyer mask (Shift/Control, etc).  GTK makes
              no distinction based on modifiers in the activate callback,
              so we have to do it ourselves.  */
-          g_signal_connect (w, "button-release-event",
+          g_signal_connect (wbutton, "button-release-event",
                             GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
-                            ti);
-
-          g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
+                            NULL);
+
+          g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
 
           /* Use enter/leave notify to show help.  We use the events
              rather than the GtkButton specific signals "enter" and
              "leave", so we can have only one callback.  The event
              will tell us what kind of event it is.  */
           /* The EMACS_INT cast avoids a warning. */
-          g_signal_connect (G_OBJECT (w),
+          g_signal_connect (G_OBJECT (weventbox),
                             "enter-notify-event",
                             G_CALLBACK (xg_tool_bar_help_callback),
                             (gpointer) (EMACS_INT) i);
-          g_signal_connect (G_OBJECT (w),
+          g_signal_connect (G_OBJECT (weventbox),
                             "leave-notify-event",
                             G_CALLBACK (xg_tool_bar_help_callback),
                             (gpointer) (EMACS_INT) i);
         }
       else
         {
-          /* The child of the tool bar is a button.  Inside that button
-             is a vbox.  Inside that vbox is the GtkImage.  */
-          GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
-          GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
-          GtkImage *wimage = GTK_IMAGE (chlist->data);
+          GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
           Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
                                                       XG_TOOL_BAR_IMAGE_DATA);
-          g_list_free (chlist);
-
           gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
 
           if (old_img != img->pixmap)
@@ -3830,21 +3810,20 @@
           g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
                              (gpointer)img->pixmap);
 
-          gtk_widget_set_sensitive (wicon, enabled_p);
-          gtk_widget_show (wicon);
-        }
+          gtk_widget_set_sensitive (wbutton, enabled_p);
+          gtk_widget_show_all (GTK_WIDGET (ti));
+       }
 
 #undef PROP
     }
 
   /* Remove buttons not longer needed.  We just hide them so they
      can be reused later on.  */
-  while (iter)
+  do
     {
-      GtkWidget *w = GTK_WIDGET (iter->data);
-      gtk_widget_hide (w);
-      iter = g_list_next (iter);
-    }
+      ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
+      if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
+    } while (ti != NULL);
 
   gtk_widget_size_request (x->toolbar_widget, &new_req);
   if (old_req.height != new_req.height
@@ -3854,8 +3833,6 @@
       xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
     }
 
-  if (icon_list) g_list_free (icon_list);
-
   UNBLOCK_INPUT;
 }