]> code.delx.au - gnu-emacs/blobdiff - src/gtkutil.c
(setup_coding_system): If eol-type is not yet decided
[gnu-emacs] / src / gtkutil.c
index 22919230a65ff7310f4da341ff89a18dc73b8745..b8d37df22144ad16c4ee364900218e4327b36f48 100644 (file)
@@ -1,6 +1,5 @@
 /* Functions for creating and updating GTK widgets.
-   Copyright (C) 2003
-   Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -16,8 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 #include "config.h"
 
@@ -42,6 +41,9 @@ Boston, MA 02111-1307, USA.  */
 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
   (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
 
+/* Avoid "differ in sign" warnings */
+#define SSDATA(x)  ((char *) SDATA (x))
+
 \f
 /***********************************************************************
                       Display handling functions
@@ -197,7 +199,7 @@ malloc_widget_value ()
     }
   else
     {
-      wv = (widget_value *) malloc (sizeof (widget_value));
+      wv = (widget_value *) xmalloc (sizeof (widget_value));
       malloc_cpt++;
     }
   memset (wv, 0, sizeof (widget_value));
@@ -240,6 +242,64 @@ xg_create_default_cursor (dpy)
   return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
 }
 
+/* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel.  */
+
+static GdkPixbuf *
+xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
+     GdkPixmap *gpix;
+     GdkPixmap *gmask;
+     GdkColormap *cmap;
+{
+  int x, y, width, height, rowstride, mask_rowstride;
+  GdkPixbuf *icon_buf, *tmp_buf;
+  guchar *pixels;
+  guchar *mask_pixels;
+
+  gdk_drawable_get_size (gpix, &width, &height);
+  tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
+                                          0, 0, 0, 0, width, height);
+  icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
+  g_object_unref (G_OBJECT (tmp_buf));
+
+  if (gmask)
+    {
+      GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
+                                                          gmask,
+                                                          NULL,
+                                                          0, 0, 0, 0,
+                                                          width, height);
+      guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
+      guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
+      int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
+      int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
+      int y;
+
+      for (y = 0; y < height; ++y)
+        {
+          guchar *iconptr, *maskptr;
+          int x;
+
+          iconptr = pixels + y * rowstride;
+          maskptr = mask_pixels + y * mask_rowstride;
+
+          for (x = 0; x < width; ++x)
+            {
+              /* In a bitmap, RGB is either 255/255/255 or 0/0/0.  Checking
+                 just R is sufficient.  */
+              if (maskptr[0] == 0)
+                iconptr[3] = 0; /* 0, 1, 2 is R, G, B.  3 is alpha.  */
+
+              iconptr += rowstride/width;
+              maskptr += mask_rowstride/width;
+            }
+        }
+
+      g_object_unref (G_OBJECT (mask_buf));
+    }
+
+  return icon_buf;
+}
+
 /* For the image defined in IMG, make and return a GtkImage.  For displays with
    8 planes or less we must make a GdkPixbuf and apply the mask manually.
    Otherwise the highlightning and dimming the tool bar code in GTK does
@@ -262,43 +322,39 @@ xg_get_image_for_pixmap (f, img, widget, old_widget)
   GdkPixmap *gmask;
   GdkDisplay *gdpy;
 
-  /* If we are on a one bit display, let GTK do all the image handling.
+  /* If we have a file, let GTK do all the image handling.
      This seems to be the only way to make insensitive and activated icons
-     look good.  */
-  if (x_screen_planes (f) == 1)
-    {
-      Lisp_Object specified_file = Qnil;
-      Lisp_Object tail;
-      extern Lisp_Object QCfile;
-
-      for (tail = XCDR (img->spec);
-          NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
-          tail = XCDR (XCDR (tail)))
-       if (EQ (XCAR (tail), QCfile))
-         specified_file = XCAR (XCDR (tail));
+     look good in all cases.  */
+  Lisp_Object specified_file = Qnil;
+  Lisp_Object tail;
+  Lisp_Object file;
+  extern Lisp_Object QCfile;
 
-       if (STRINGP (specified_file))
-         {
+  for (tail = XCDR (img->spec);
+       NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
+       tail = XCDR (XCDR (tail)))
+    if (EQ (XCAR (tail), QCfile))
+      specified_file = XCAR (XCDR (tail));
 
-           Lisp_Object file = Qnil;
-           struct gcpro gcpro1;
-           GCPRO1 (file);
+  /* We already loaded the image once before calling this
+     function, so this only fails if the image file has been removed.
+     In that case, use the pixmap already loaded.  */
 
-           file = x_find_image_file (specified_file);
-           /* We already loaded the image once before calling this
-              function, so this should not fail.  */
-           xassert (STRINGP (file) != 0);
-
-           if (! old_widget)
-             old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
-           else
-             gtk_image_set_from_file (old_widget, SDATA (file));
+  if (STRINGP (specified_file)
+      && STRINGP (file = x_find_image_file (specified_file)))
+    {
+      if (! old_widget)
+        old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
+      else
+        gtk_image_set_from_file (old_widget, SSDATA (file));
 
-           UNGCPRO;
-           return GTK_WIDGET (old_widget);
-         }
+      return GTK_WIDGET (old_widget);
     }
 
+  /* No file, do the image handling ourselves.  This will look very bad
+     on a monochrome display, and sometimes bad on all displays with
+     certain themes.  */
+
   gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
   gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
   gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
@@ -312,60 +368,15 @@ xg_get_image_for_pixmap (f, img, widget, old_widget)
     }
   else
     {
+
       /* This is a workaround to make icons look good on pseudo color
          displays.  Apparently GTK expects the images to have an alpha
          channel.  If they don't, insensitive and activated icons will
          look bad.  This workaround does not work on monochrome displays,
          and is not needed on true color/static color displays (i.e.
          16 bits and higher).  */
-      int x, y, width, height, rowstride, mask_rowstride;
-      GdkPixbuf *icon_buf, *tmp_buf;
-      guchar *pixels;
-      guchar *mask_pixels;
-
-      gdk_drawable_get_size (gpix, &width, &height);
-      tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
-                                              gpix,
-                                             gtk_widget_get_colormap (widget),
-                                              0, 0, 0, 0, width, height);
-      icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
-      g_object_unref (G_OBJECT (tmp_buf));
-
-      if (gmask)
-        {
-          GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
-                                                              gmask,
-                                                              NULL,
-                                                              0, 0, 0, 0,
-                                                              width, height);
-          guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
-          guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
-          int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
-          int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
-          int y;
-
-          for (y = 0; y < height; ++y)
-            {
-              guchar *iconptr, *maskptr;
-              int x;
-
-              iconptr = pixels + y * rowstride;
-              maskptr = mask_pixels + y * mask_rowstride;
-
-              for (x = 0; x < width; ++x)
-                {
-                  /* In a bitmap, RGB is either 255/255/255 or 0/0/0.  Checking
-                     just R is sufficient.  */
-                  if (maskptr[0] == 0)
-                    iconptr[3] = 0; /* 0, 1, 2 is R, G, B.  3 is alpha.  */
-
-                  iconptr += rowstride/width;
-                  maskptr += mask_rowstride/width;
-                }
-            }
-
-          g_object_unref (G_OBJECT (mask_buf));
-        }
+      GdkColormap *cmap = gtk_widget_get_colormap (widget);
+      GdkPixbuf *icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
 
       if (! old_widget)
         old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
@@ -542,6 +553,9 @@ xg_set_geometry (f)
     if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
                                     geom_str))
       fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
+  } else if (f->size_hint_flags & PPosition) {
+    gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
+                     f->left_pos, f->top_pos);
   }
 }
 
@@ -556,12 +570,6 @@ xg_resize_outer_widget (f, columns, rows)
      int columns;
      int rows;
 {
-  gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
-                     FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
-
-  /* base_height is now changed.  */
-  x_wm_set_size_hint (f, 0, 0);
-
   /* If we are not mapped yet, set geometry once again, as window
      height now have changed.  */
   if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
@@ -584,14 +592,14 @@ xg_resize_widgets (f, pixelwidth, pixelheight)
 {
   int mbheight = FRAME_MENUBAR_HEIGHT (f);
   int tbheight = FRAME_TOOLBAR_HEIGHT (f);
-  int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight 
+  int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
                                                   - mbheight - tbheight));
   int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
 
   if (FRAME_GTK_WIDGET (f)
-      && (columns != FRAME_COLS (f) 
+      && (columns != FRAME_COLS (f)
          || rows != FRAME_LINES (f)
-          || pixelwidth != FRAME_PIXEL_WIDTH (f) 
+          || pixelwidth != FRAME_PIXEL_WIDTH (f)
          || pixelheight != FRAME_PIXEL_HEIGHT (f)))
     {
       struct x_output *x = f->output_data.x;
@@ -719,17 +727,18 @@ xg_create_frame_widgets (f)
       if (wvbox) gtk_widget_destroy (wvbox);
       if (wfixed) gtk_widget_destroy (wfixed);
 
+      UNBLOCK_INPUT;
       return 0;
     }
 
   /* Use same names as the Xt port does.  I.e. Emacs.pane.emacs by default */
   gtk_widget_set_name (wtop, EMACS_CLASS);
   gtk_widget_set_name (wvbox, "pane");
-  gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
+  gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
 
   /* If this frame has a title or name, set it in the title bar.  */
-  if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
-  else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
+  if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
+  else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
 
   if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
 
@@ -756,7 +765,7 @@ xg_create_frame_widgets (f)
      So we cheat a bit by setting a height that is what it will have
      later on when tool bar items are added.  */
   if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
-    FRAME_TOOLBAR_HEIGHT (f) = 34;
+    FRAME_TOOLBAR_HEIGHT (f) = 38;
 
 
   /* We don't want this widget double buffered, because we draw on it
@@ -770,8 +779,8 @@ xg_create_frame_widgets (f)
      can't shrink the window from its starting size.  */
   gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
   gtk_window_set_wmclass (GTK_WINDOW (wtop),
-                          SDATA (Vx_resource_name),
-                          SDATA (Vx_resource_class));
+                          SSDATA (Vx_resource_name),
+                          SSDATA (Vx_resource_class));
 
   /* Add callback to do nothing on WM_DELETE_WINDOW.  The default in
      GTK is to destroy the widget.  We want Emacs to do that instead.  */
@@ -945,6 +954,24 @@ xg_set_background_color (f, bg)
 }
 
 
+/* Set the frame icon to ICON_PIXMAP/MASK.  This must be done with GTK
+   functions so GTK does not overwrite the icon.  */
+
+void
+xg_set_frame_icon (f, icon_pixmap, icon_mask)
+     FRAME_PTR f;
+     Pixmap icon_pixmap;
+     Pixmap icon_mask;
+{
+    GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
+    GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
+    GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
+    GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
+
+    gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
+}
+
+
 \f
 /***********************************************************************
                       Dialog functions
@@ -1124,6 +1151,27 @@ create_dialog (wv, select_cb, deactivate_cb)
 /***********************************************************************
                       File dialog functions
  ***********************************************************************/
+/* Return non-zero if the old file selection dialog is being used.
+   Return zero if not.  */
+
+int
+xg_uses_old_file_dialog ()
+{
+#ifdef HAVE_GTK_FILE_BOTH
+  extern int x_use_old_gtk_file_dialog;
+  return x_use_old_gtk_file_dialog;
+#else /* ! HAVE_GTK_FILE_BOTH */
+
+#ifdef HAVE_GTK_FILE_SELECTION_NEW
+  return 1;
+#else
+  return 0;
+#endif
+
+#endif /* ! HAVE_GTK_FILE_BOTH */
+}
+
+
 /* Function that is called when the file dialog pops down.
    W is the dialog widget, RESPONSE is the response code.
    USER_DATA is what we passed in to g_signal_connect (pointer to int).  */
@@ -1168,6 +1216,59 @@ xg_get_file_name_from_chooser (w)
   return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
 }
 
+/* Callback called when the "Show hidden files" toggle is pressed.
+   WIDGET is the toggle widget, DATA is the file chooser dialog.  */
+
+static void
+xg_toggle_visibility_cb (widget, data)
+     GtkWidget *widget;
+     gpointer data;
+{
+  GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
+  gboolean visible;
+  g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
+  g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
+}
+
+
+/* Callback called when a property changes in a file chooser.
+   GOBJECT is the file chooser dialog, ARG1 describes the property.
+   USER_DATA is the toggle widget in the file chooser dialog.
+   We use this to update the "Show hidden files" toggle when the user
+   changes that property by right clicking in the file list.  */
+
+static void
+xg_toggle_notify_cb (gobject, arg1, user_data)
+     GObject *gobject;
+     GParamSpec *arg1;
+     gpointer user_data;
+{
+  extern int x_gtk_show_hidden_files;
+
+  if (strcmp (arg1->name, "show-hidden") == 0)
+    {
+      GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
+      GtkWidget *wtoggle = GTK_WIDGET (user_data);
+      gboolean visible, toggle_on;
+
+      g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
+      toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
+
+      if (!!visible != !!toggle_on)
+        {
+          g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
+                                           G_CALLBACK (xg_toggle_visibility_cb),
+                                           gobject);
+          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
+          g_signal_handlers_unblock_by_func
+            (G_OBJECT (wtoggle),
+             G_CALLBACK (xg_toggle_visibility_cb),
+             gobject);
+        }
+      x_gtk_show_hidden_files = visible;
+    }
+}
+
 /* Read a file name from the user using a file chooser dialog.
    F is the current frame.
    PROMPT is a prompt to show to the user.  May not be NULL.
@@ -1187,11 +1288,14 @@ xg_get_file_with_chooser (f, prompt, default_filename,
      int mustmatch_p, only_dir_p;
      xg_get_file_func *func;
 {
-  GtkWidget *filewin;
+  char message[1024];
+
+  GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
   GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
   GtkFileChooserAction action = (mustmatch_p ?
                                  GTK_FILE_CHOOSER_ACTION_OPEN :
                                  GTK_FILE_CHOOSER_ACTION_SAVE);
+  extern int x_gtk_show_hidden_files;
 
   if (only_dir_p)
     action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
@@ -1204,6 +1308,33 @@ xg_get_file_with_chooser (f, prompt, default_filename,
                                          NULL);
   gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
 
+  wbox = gtk_vbox_new (FALSE, 0);
+  gtk_widget_show (wbox);
+  wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
+  
+  if (x_gtk_show_hidden_files) 
+    {
+      g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
+    }
+  gtk_widget_show (wtoggle);
+  g_signal_connect (G_OBJECT (wtoggle), "clicked",
+                    G_CALLBACK (xg_toggle_visibility_cb), filewin);
+  g_signal_connect (G_OBJECT (filewin), "notify",
+                    G_CALLBACK (xg_toggle_notify_cb), wtoggle);
+
+  message[0] = '\0';
+  if (action != GTK_FILE_CHOOSER_ACTION_SAVE)
+    strcat (message, "\nType C-l to display a file name text entry box.\n");
+  strcat (message, "\nIf you don't like this file selector, customize "
+          "use-file-dialog\nto turn it off, or type C-x C-f to visit files.");
+
+  wmessage = gtk_label_new (message);
+  gtk_widget_show (wmessage);
+  gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
+  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
+
   if (default_filename)
     {
       Lisp_Object file;
@@ -1217,7 +1348,7 @@ xg_get_file_with_chooser (f, prompt, default_filename,
       if (default_filename[0] != '/')
         file = Fexpand_file_name (file, Qnil);
 
-      default_filename = SDATA (file);
+      default_filename = SSDATA (file);
       if (Ffile_directory_p (file))
         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
                                              default_filename);
@@ -1321,9 +1452,8 @@ xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
 #endif /* HAVE_GTK_AND_PTHREAD */
 
 #ifdef HAVE_GTK_FILE_BOTH
-  extern int x_use_old_gtk_file_dialog;
 
-  if (x_use_old_gtk_file_dialog)
+  if (xg_uses_old_file_dialog ())
     w = xg_get_file_with_selection (f, prompt, default_filename,
                                     mustmatch_p, only_dir_p, &func);
   else
@@ -1905,7 +2035,7 @@ create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
 
       if (deactivate_cb)
         g_signal_connect (G_OBJECT (wmenu),
-                          "deactivate", deactivate_cb, 0);
+                          "selection-done", deactivate_cb, 0);
 
       g_signal_connect (G_OBJECT (wmenu),
                         "grab-notify", G_CALLBACK (menu_grab_callback), 0);
@@ -2612,7 +2742,7 @@ xg_modify_menubar_widgets (menubar, f, val, deep_p,
   xg_update_menubar (menubar, f, &list, list, 0, val->contents,
                      select_cb, highlight_cb, cl_data);
 
-  if (deep_p);
+  if (deep_p)
     {
       widget_value *cur;
 
@@ -2850,7 +2980,7 @@ xg_gtk_scroll_destroy (widget, data)
      gpointer data;
 {
   gpointer p;
-  int id = (int)data;
+  int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
 
   p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
   if (p) xfree (p);
@@ -2881,7 +3011,7 @@ scroll_bar_button_cb (widget, event, user_data)
       if (xg_timer) xg_stop_timer ();
       bar->dragging = Qnil;
     }
-  
+
   return FALSE;
 }
 
@@ -2920,10 +3050,11 @@ xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
                     "value-changed",
                     scroll_callback,
                     (gpointer) bar);
+  /* The EMACS_INT cast avoids a warning. */
   g_signal_connect (G_OBJECT (wscroll),
                     "destroy",
                     G_CALLBACK (xg_gtk_scroll_destroy),
-                    (gpointer) scroll_id);
+                    (gpointer) (EMACS_INT) scroll_id);
 
   /* Connect to button press and button release to detect if any scroll bar
      has the pointer.  */
@@ -2944,7 +3075,7 @@ xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
      event box window.  */
   gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
   gtk_container_add (GTK_CONTAINER (webox), wscroll);
-  
+
 
   /* Set the cursor to an arrow.  */
   xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
@@ -3003,9 +3134,14 @@ xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
       GtkWidget *wparent = gtk_widget_get_parent (wscroll);
 
       /* Move and resize to new values.  */
-      gtk_widget_set_size_request (wscroll, width, height);
       gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
-
+      gtk_widget_set_size_request (wscroll, width, height);
+      gtk_widget_queue_draw (wparent);
+      gdk_window_process_all_updates ();
+      /* GTK does not redraw until the main loop is entered again, but
+         if there are no X events pending we will not enter it.  So we sync
+         here to get some events.  */
+      x_sync (f);
       SET_FRAME_GARBAGED (f);
       cancel_mouse_face (f);
     }
@@ -3102,17 +3238,37 @@ xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
    the GtkImage with a new image.  */
 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
 
+/* The key for storing the latest modifiers so the activate callback can
+   get them.  */
+#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;
+    GdkEventButton *event;
+    gpointer        user_data;
+{
+  g_object_set_data (G_OBJECT (user_data), XG_TOOL_BAR_LAST_MODIFIER,
+                     (gpointer) event->state);
+  return FALSE;
+}
+
+
 static void
 xg_tool_bar_callback (w, client_data)
      GtkWidget *w;
      gpointer client_data;
 {
-  int idx = (int)client_data;
+  /* The EMACS_INT cast avoids a warning. */
+  int idx = (int) (EMACS_INT) client_data;
+  int mod = (int) g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
+
   FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
   Lisp_Object key, frame;
   struct input_event event;
@@ -3133,7 +3289,10 @@ xg_tool_bar_callback (w, client_data)
   event.kind = TOOL_BAR_EVENT;
   event.frame_or_window = frame;
   event.arg = key;
-  event.modifiers = 0;  /* These are not available.  */
+  /* Convert between the modifier bits GDK uses and the modifier bits
+     Emacs uses.  This assumes GDK an X masks are the same, which they are when
+     this is written.  */
+  event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
   kbd_buffer_store_event (&event);
 }
 
@@ -3151,13 +3310,18 @@ xg_tool_bar_detach_callback (wbox, w, client_data)
      gpointer client_data;
 {
   FRAME_PTR f = (FRAME_PTR) client_data;
+  extern int x_gtk_whole_detached_tool_bar;
+
+  g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
+               NULL);
 
   if (f)
     {
+      FRAME_X_OUTPUT (f)->toolbar_detached = 1;
+
       /* When detaching a tool bar, not everything dissapear.  There are
          a few pixels left that are used to drop the tool bar back into
          place.  */
-      int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
       FRAME_TOOLBAR_HEIGHT (f) = 2;
 
       /* The height has changed, resize outer widget and set columns
@@ -3180,16 +3344,19 @@ xg_tool_bar_attach_callback (wbox, w, client_data)
      gpointer client_data;
 {
   FRAME_PTR f = (FRAME_PTR) client_data;
+  g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
 
   if (f)
     {
       GtkRequisition req;
 
+      FRAME_X_OUTPUT (f)->toolbar_detached = 0;
+
       gtk_widget_size_request (w, &req);
       FRAME_TOOLBAR_HEIGHT (f) = req.height;
 
       /* The height has changed, resize outer widget and set columns
-         rows to what we had before detaching the tool bar.  */
+         rows to what we had before attaching the tool bar.  */
       xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
     }
 }
@@ -3209,14 +3376,13 @@ xg_tool_bar_help_callback (w, event, client_data)
      GdkEventCrossing *event;
      gpointer client_data;
 {
-  int idx = (int)client_data;
+  /* The EMACS_INT cast avoids a warning. */
+  int idx = (int) (EMACS_INT) client_data;
   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;
-    }
+    return FALSE;
 
   if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
     return FALSE;
@@ -3264,10 +3430,10 @@ xg_tool_bar_item_expose_callback (w, event, client_data)
 
   event->area.x = max (0, event->area.x);
   event->area.y = max (0, event->area.y);
-  
+
   event->area.width = max (width, event->area.width);
   event->area.height = max (height, event->area.height);
-  
+
   return FALSE;
 }
 
@@ -3302,6 +3468,8 @@ xg_create_tool_bar (f)
 
   x->toolbar_widget = gtk_toolbar_new ();
   x->handlebox_widget = gtk_handle_box_new ();
+  x->toolbar_detached = 0;
+
   gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
                      x->toolbar_widget);
 
@@ -3386,7 +3554,7 @@ update_frame_tool_bar (f)
      DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect.  */
   hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
   vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
-  
+
   if (! x->toolbar_widget)
     xg_create_tool_bar (f);
 
@@ -3449,52 +3617,63 @@ update_frame_tool_bar (f)
       if (! wicon)
         {
           GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
+          GtkToolItem *ti = gtk_tool_button_new (w, "");
 
           gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
 
-          gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
-                                   0, 0, 0,
-                                   w,
-                                   GTK_SIGNAL_FUNC (xg_tool_bar_callback),
-                                   (gpointer)i);
+          gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget),
+                              ti,
+                              i);
+          /* The EMACS_INT cast avoids a warning. */
+          g_signal_connect (GTK_WIDGET (ti), "clicked",
+                            GTK_SIGNAL_FUNC (xg_tool_bar_callback),
+                            (gpointer) (EMACS_INT) i);
+
+          gtk_widget_show (GTK_WIDGET (ti));
+          gtk_widget_show (GTK_WIDGET (w));
 
           /* 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);
+
           /* Catch expose events to overcome an annoying redraw bug, see
              comment for xg_tool_bar_item_expose_callback.  */
-          g_signal_connect (G_OBJECT (w),
+          g_signal_connect (G_OBJECT (ti),
                             "expose-event",
                             G_CALLBACK (xg_tool_bar_item_expose_callback),
                             0);
 
-          /* We must set sensitive on the button that is the parent
-             of the GtkImage parent.  Go upwards until we find the button.  */
+          gtk_widget_set_sensitive (GTK_WIDGET (ti), enabled_p);
+          gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (ti), FALSE);
+          
           while (! GTK_IS_BUTTON (w))
             w = gtk_widget_get_parent (w);
 
-          if (w)
-            {
-              /* Save the frame in the button so the xg_tool_bar_callback
-                 can get at it.  */
-              g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
-              gtk_widget_set_sensitive (w, enabled_p);
-
-              /* 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.  */
-              g_signal_connect (G_OBJECT (w),
-                                "enter-notify-event",
-                                G_CALLBACK (xg_tool_bar_help_callback),
-                                (gpointer)i);
-              g_signal_connect (G_OBJECT (w),
-                                "leave-notify-event",
-                                G_CALLBACK (xg_tool_bar_help_callback),
-                                (gpointer)i);
-            }
+          /* 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",
+                            GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
+                            ti);
+
+          g_object_set_data (G_OBJECT (w), 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),
+                            "enter-notify-event",
+                            G_CALLBACK (xg_tool_bar_help_callback),
+                            (gpointer) (EMACS_INT) i);
+          g_signal_connect (G_OBJECT (w),
+                            "leave-notify-event",
+                            G_CALLBACK (xg_tool_bar_help_callback),
+                            (gpointer) (EMACS_INT) i);
         }
       else
         {
@@ -3532,7 +3711,8 @@ update_frame_tool_bar (f)
     }
 
   gtk_widget_size_request (x->toolbar_widget, &new_req);
-  if (old_req.height != new_req.height)
+  if (old_req.height != new_req.height
+      && ! FRAME_X_OUTPUT (f)->toolbar_detached)
     {
       FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
       xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));