Drop WGET and revert read access to Lisp_Objects slots of struct window.
[emacs.git] / src / gtkutil.c
blob4879479201a1e91e14a5f982788e6e278e5db4ba
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <float.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <setjmp.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
31 #include "window.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
38 #include "xsettings.h"
40 #ifdef HAVE_XFT
41 #include <X11/Xft/Xft.h>
42 #endif
44 #ifdef HAVE_GTK3
45 #include <gtk/gtkx.h>
46 #include "emacsgtkfixed.h"
47 #endif
49 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
50 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
52 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
53 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
55 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
56 #define gtk_widget_set_has_window(w, b) \
57 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
58 #endif
59 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
60 #define gtk_dialog_get_action_area(w) ((w)->action_area)
61 #define gtk_dialog_get_content_area(w) ((w)->vbox)
62 #endif
63 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
64 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
65 #endif
66 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
67 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
68 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
69 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
70 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
71 #endif
72 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
73 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
74 #else
75 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
76 #endif
78 #ifndef HAVE_GTK3
79 #ifdef USE_GTK_TOOLTIP
80 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
81 #endif
82 #define gdk_window_get_geometry(w, a, b, c, d) \
83 gdk_window_get_geometry (w, a, b, c, d, 0)
84 #define gdk_x11_window_lookup_for_display(d, w) \
85 gdk_xid_table_lookup_for_display (d, w)
86 #define gtk_box_new(ori, spacing) \
87 ((ori) == GTK_ORIENTATION_HORIZONTAL \
88 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
89 #define gtk_scrollbar_new(ori, spacing) \
90 ((ori) == GTK_ORIENTATION_HORIZONTAL \
91 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
92 #ifndef GDK_KEY_g
93 #define GDK_KEY_g GDK_g
94 #endif
95 #endif /* HAVE_GTK3 */
97 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
99 static void update_theme_scrollbar_width (void);
102 /***********************************************************************
103 Display handling functions
104 ***********************************************************************/
106 /* Keep track of the default display, or NULL if there is none. Emacs
107 may close all its displays. */
109 static GdkDisplay *gdpy_def;
111 /* When the GTK widget W is to be created on a display for F that
112 is not the default display, set the display for W.
113 W can be a GtkMenu or a GtkWindow widget. */
115 static void
116 xg_set_screen (GtkWidget *w, FRAME_PTR f)
118 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
120 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
121 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
123 if (GTK_IS_MENU (w))
124 gtk_menu_set_screen (GTK_MENU (w), gscreen);
125 else
126 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
131 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
132 *DPY is set to NULL if the display can't be opened.
134 Returns non-zero if display could be opened, zero if display could not
135 be opened, and less than zero if the GTK version doesn't support
136 multiple displays. */
138 void
139 xg_display_open (char *display_name, Display **dpy)
141 GdkDisplay *gdpy;
143 gdpy = gdk_display_open (display_name);
144 if (!gdpy_def && gdpy)
146 gdpy_def = gdpy;
147 gdk_display_manager_set_default_display (gdk_display_manager_get (),
148 gdpy);
151 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
155 /* Close display DPY. */
157 void
158 xg_display_close (Display *dpy)
160 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
162 /* If this is the default display, try to change it before closing.
163 If there is no other display to use, gdpy_def is set to NULL, and
164 the next call to xg_display_open resets the default display. */
165 if (gdk_display_get_default () == gdpy)
167 struct x_display_info *dpyinfo;
168 GdkDisplay *gdpy_new = NULL;
170 /* Find another display. */
171 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
172 if (dpyinfo->display != dpy)
174 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
175 gdk_display_manager_set_default_display (gdk_display_manager_get (),
176 gdpy_new);
177 break;
179 gdpy_def = gdpy_new;
182 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
183 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
184 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
185 can continue running, but there will be memory leaks. */
186 g_object_run_dispose (G_OBJECT (gdpy));
187 #else
188 /* This seems to be fixed in GTK 2.10. */
189 gdk_display_close (gdpy);
190 #endif
194 /***********************************************************************
195 Utility functions
196 ***********************************************************************/
197 /* The next two variables and functions are taken from lwlib. */
198 static widget_value *widget_value_free_list;
199 static int malloc_cpt;
201 /* Allocate a widget_value structure, either by taking one from the
202 widget_value_free_list or by malloc:ing a new one.
204 Return a pointer to the allocated structure. */
206 widget_value *
207 malloc_widget_value (void)
209 widget_value *wv;
210 if (widget_value_free_list)
212 wv = widget_value_free_list;
213 widget_value_free_list = wv->free_list;
214 wv->free_list = 0;
216 else
218 wv = xmalloc (sizeof *wv);
219 malloc_cpt++;
221 memset (wv, 0, sizeof (widget_value));
222 return wv;
225 /* This is analogous to free. It frees only what was allocated
226 by malloc_widget_value, and no substructures. */
228 void
229 free_widget_value (widget_value *wv)
231 if (wv->free_list)
232 abort ();
234 if (malloc_cpt > 25)
236 /* When the number of already allocated cells is too big,
237 We free it. */
238 xfree (wv);
239 malloc_cpt--;
241 else
243 wv->free_list = widget_value_free_list;
244 widget_value_free_list = wv;
249 /* Create and return the cursor to be used for popup menus and
250 scroll bars on display DPY. */
252 GdkCursor *
253 xg_create_default_cursor (Display *dpy)
255 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
256 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
259 static GdkPixbuf *
260 xg_get_pixbuf_from_pixmap (FRAME_PTR f, Pixmap pix)
262 int iunused;
263 GdkPixbuf *tmp_buf;
264 Window wunused;
265 unsigned int width, height, uunused;
266 XImage *xim;
268 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
269 &width, &height, &uunused, &uunused);
271 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
272 ~0, XYPixmap);
273 if (!xim) return 0;
275 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
276 GDK_COLORSPACE_RGB,
277 FALSE,
278 xim->bitmap_unit,
279 width,
280 height,
281 xim->bytes_per_line,
282 NULL,
283 NULL);
284 XDestroyImage (xim);
285 return tmp_buf;
288 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
290 static GdkPixbuf *
291 xg_get_pixbuf_from_pix_and_mask (FRAME_PTR f,
292 Pixmap pix,
293 Pixmap mask)
295 int width, height;
296 GdkPixbuf *icon_buf, *tmp_buf;
298 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
299 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
300 g_object_unref (G_OBJECT (tmp_buf));
302 width = gdk_pixbuf_get_width (icon_buf);
303 height = gdk_pixbuf_get_height (icon_buf);
305 if (mask)
307 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
308 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
309 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
310 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
311 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
312 int y;
314 for (y = 0; y < height; ++y)
316 guchar *iconptr, *maskptr;
317 int x;
319 iconptr = pixels + y * rowstride;
320 maskptr = mask_pixels + y * mask_rowstride;
322 for (x = 0; x < width; ++x)
324 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
325 just R is sufficient. */
326 if (maskptr[0] == 0)
327 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
329 iconptr += rowstride/width;
330 maskptr += mask_rowstride/width;
334 g_object_unref (G_OBJECT (mask_buf));
337 return icon_buf;
340 static Lisp_Object
341 file_for_image (Lisp_Object image)
343 Lisp_Object specified_file = Qnil;
344 Lisp_Object tail;
346 for (tail = XCDR (image);
347 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
348 tail = XCDR (XCDR (tail)))
349 if (EQ (XCAR (tail), QCfile))
350 specified_file = XCAR (XCDR (tail));
352 return specified_file;
355 /* For the image defined in IMG, make and return a GtkImage. For displays with
356 8 planes or less we must make a GdkPixbuf and apply the mask manually.
357 Otherwise the highlighting and dimming the tool bar code in GTK does
358 will look bad. For display with more than 8 planes we just use the
359 pixmap and mask directly. For monochrome displays, GTK doesn't seem
360 able to use external pixmaps, it looks bad whatever we do.
361 The image is defined on the display where frame F is.
362 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
363 If OLD_WIDGET is NULL, a new widget is constructed and returned.
364 If OLD_WIDGET is not NULL, that widget is modified. */
366 static GtkWidget *
367 xg_get_image_for_pixmap (FRAME_PTR f,
368 struct image *img,
369 GtkWidget *widget,
370 GtkImage *old_widget)
372 GdkPixbuf *icon_buf;
374 /* If we have a file, let GTK do all the image handling.
375 This seems to be the only way to make insensitive and activated icons
376 look good in all cases. */
377 Lisp_Object specified_file = file_for_image (img->spec);
378 Lisp_Object file;
380 /* We already loaded the image once before calling this
381 function, so this only fails if the image file has been removed.
382 In that case, use the pixmap already loaded. */
384 if (STRINGP (specified_file)
385 && STRINGP (file = x_find_image_file (specified_file)))
387 if (! old_widget)
388 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
389 else
390 gtk_image_set_from_file (old_widget, SSDATA (file));
392 return GTK_WIDGET (old_widget);
395 /* No file, do the image handling ourselves. This will look very bad
396 on a monochrome display, and sometimes bad on all displays with
397 certain themes. */
399 /* This is a workaround to make icons look good on pseudo color
400 displays. Apparently GTK expects the images to have an alpha
401 channel. If they don't, insensitive and activated icons will
402 look bad. This workaround does not work on monochrome displays,
403 and is strictly not needed on true color/static color displays (i.e.
404 16 bits and higher). But we do it anyway so we get a pixbuf that is
405 not associated with the img->pixmap. The img->pixmap may be removed
406 by clearing the image cache and then the tool bar redraw fails, since
407 Gtk+ assumes the pixmap is always there. */
408 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
410 if (icon_buf)
412 if (! old_widget)
413 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
414 else
415 gtk_image_set_from_pixbuf (old_widget, icon_buf);
417 g_object_unref (G_OBJECT (icon_buf));
420 return GTK_WIDGET (old_widget);
424 /* Set CURSOR on W and all widgets W contain. We must do like this
425 for scroll bars and menu because they create widgets internally,
426 and it is those widgets that are visible. */
428 static void
429 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
431 GdkWindow *window = gtk_widget_get_window (w);
432 GList *children = gdk_window_peek_children (window);
434 gdk_window_set_cursor (window, cursor);
436 /* The scroll bar widget has more than one GDK window (had to look at
437 the source to figure this out), and there is no way to set cursor
438 on widgets in GTK. So we must set the cursor for all GDK windows.
439 Ditto for menus. */
441 for ( ; children; children = g_list_next (children))
442 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
445 /* Insert NODE into linked LIST. */
447 static void
448 xg_list_insert (xg_list_node *list, xg_list_node *node)
450 xg_list_node *list_start = list->next;
452 if (list_start) list_start->prev = node;
453 node->next = list_start;
454 node->prev = 0;
455 list->next = node;
458 /* Remove NODE from linked LIST. */
460 static void
461 xg_list_remove (xg_list_node *list, xg_list_node *node)
463 xg_list_node *list_start = list->next;
464 if (node == list_start)
466 list->next = node->next;
467 if (list->next) list->next->prev = 0;
469 else
471 node->prev->next = node->next;
472 if (node->next) node->next->prev = node->prev;
476 /* Allocate and return a utf8 version of STR. If STR is already
477 utf8 or NULL, just return a copy of STR.
478 A new string is allocated and the caller must free the result
479 with g_free. */
481 static char *
482 get_utf8_string (const char *str)
484 char *utf8_str;
486 if (!str) return NULL;
488 /* If not UTF-8, try current locale. */
489 if (!g_utf8_validate (str, -1, NULL))
490 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
491 else
492 return g_strdup (str);
494 if (!utf8_str)
496 /* Probably some control characters in str. Escape them. */
497 ptrdiff_t len;
498 ptrdiff_t nr_bad = 0;
499 gsize bytes_read;
500 gsize bytes_written;
501 unsigned char *p = (unsigned char *)str;
502 char *cp, *up;
503 GError *err = NULL;
505 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
506 &bytes_written, &err))
507 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
509 ++nr_bad;
510 p += bytes_written+1;
511 g_error_free (err);
512 err = NULL;
515 if (err)
517 g_error_free (err);
518 err = NULL;
520 if (cp) g_free (cp);
522 len = strlen (str);
523 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
524 memory_full (SIZE_MAX);
525 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
526 p = (unsigned char *)str;
528 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
529 &bytes_written, &err))
530 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
532 memcpy (up, p, bytes_written);
533 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
534 up += bytes_written+4;
535 p += bytes_written+1;
536 g_error_free (err);
537 err = NULL;
540 if (cp)
542 strcat (utf8_str, cp);
543 g_free (cp);
545 if (err)
547 g_error_free (err);
548 err = NULL;
551 return utf8_str;
554 /* Check for special colors used in face spec for region face.
555 The colors are fetched from the Gtk+ theme.
556 Return 1 if color was found, 0 if not. */
559 xg_check_special_colors (struct frame *f,
560 const char *color_name,
561 XColor *color)
563 int success_p = 0;
564 int get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
565 int get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
567 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
568 return success_p;
570 BLOCK_INPUT;
572 #ifdef HAVE_GTK3
573 GtkStyleContext *gsty
574 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
575 GdkRGBA col;
576 char buf[sizeof "rgbi://" + 3 * (DBL_MAX_10_EXP + sizeof "-1.000000" - 1)];
577 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
578 if (get_fg)
579 gtk_style_context_get_color (gsty, state, &col);
580 else
581 gtk_style_context_get_background_color (gsty, state, &col);
583 sprintf (buf, "rgbi:%lf/%lf/%lf", col.red, col.green, col.blue);
584 success_p = XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
585 buf, color);
586 #else
587 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
588 GdkColor *grgb = get_bg
589 ? &gsty->bg[GTK_STATE_SELECTED]
590 : &gsty->fg[GTK_STATE_SELECTED];
592 color->red = grgb->red;
593 color->green = grgb->green;
594 color->blue = grgb->blue;
595 color->pixel = grgb->pixel;
596 success_p = 1;
597 #endif
600 UNBLOCK_INPUT;
601 return success_p;
606 /***********************************************************************
607 Tooltips
608 ***********************************************************************/
609 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
610 We use that to pop down the tooltip. This happens if Gtk+ for some
611 reason wants to change or hide the tooltip. */
613 #ifdef USE_GTK_TOOLTIP
615 static void
616 hierarchy_ch_cb (GtkWidget *widget,
617 GtkWidget *previous_toplevel,
618 gpointer user_data)
620 FRAME_PTR f = (FRAME_PTR) user_data;
621 struct x_output *x = f->output_data.x;
622 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
624 if (! top || ! GTK_IS_WINDOW (top))
625 gtk_widget_hide (previous_toplevel);
628 /* Callback called when Gtk+ thinks a tooltip should be displayed.
629 We use it to get the tooltip window and the tooltip widget so
630 we can manipulate the ourselves.
632 Return FALSE ensures that the tooltip is not shown. */
634 static gboolean
635 qttip_cb (GtkWidget *widget,
636 gint xpos,
637 gint ypos,
638 gboolean keyboard_mode,
639 GtkTooltip *tooltip,
640 gpointer user_data)
642 FRAME_PTR f = (FRAME_PTR) user_data;
643 struct x_output *x = f->output_data.x;
644 if (x->ttip_widget == NULL)
646 GtkWidget *p;
647 GList *list, *iter;
649 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
650 x->ttip_widget = tooltip;
651 g_object_ref (G_OBJECT (tooltip));
652 x->ttip_lbl = gtk_label_new ("");
653 g_object_ref (G_OBJECT (x->ttip_lbl));
654 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
655 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
657 /* Change stupid Gtk+ default line wrapping. */
658 p = gtk_widget_get_parent (x->ttip_lbl);
659 list = gtk_container_get_children (GTK_CONTAINER (p));
660 for (iter = list; iter; iter = g_list_next (iter))
662 GtkWidget *w = GTK_WIDGET (iter->data);
663 if (GTK_IS_LABEL (w))
664 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
666 g_list_free (list);
668 /* ATK needs an empty title for some reason. */
669 gtk_window_set_title (x->ttip_window, "");
670 /* Realize so we can safely get screen later on. */
671 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
672 gtk_widget_realize (x->ttip_lbl);
674 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
675 G_CALLBACK (hierarchy_ch_cb), f);
677 return FALSE;
680 #endif /* USE_GTK_TOOLTIP */
682 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
683 Return zero if no system tooltip available, non-zero otherwise. */
686 xg_prepare_tooltip (FRAME_PTR f,
687 Lisp_Object string,
688 int *width,
689 int *height)
691 #ifndef USE_GTK_TOOLTIP
692 return 0;
693 #else
694 struct x_output *x = f->output_data.x;
695 GtkWidget *widget;
696 GdkWindow *gwin;
697 GdkScreen *screen;
698 GtkSettings *settings;
699 gboolean tt_enabled = TRUE;
700 GtkRequisition req;
701 Lisp_Object encoded_string;
703 if (!x->ttip_lbl) return 0;
705 BLOCK_INPUT;
706 encoded_string = ENCODE_UTF_8 (string);
707 widget = GTK_WIDGET (x->ttip_lbl);
708 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
709 screen = gdk_window_get_screen (gwin);
710 settings = gtk_settings_get_for_screen (screen);
711 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
712 if (tt_enabled)
714 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
715 /* Record that we disabled it so it can be enabled again. */
716 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
717 (gpointer)f);
720 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
721 g_object_set_data (G_OBJECT
722 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
723 "gdk-display-current-tooltip", NULL);
725 /* Put our dummy widget in so we can get callbacks for unrealize and
726 hierarchy-changed. */
727 gtk_tooltip_set_custom (x->ttip_widget, widget);
728 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
729 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
730 if (width) *width = req.width;
731 if (height) *height = req.height;
733 UNBLOCK_INPUT;
735 return 1;
736 #endif /* USE_GTK_TOOLTIP */
739 /* Show the tooltip at ROOT_X and ROOT_Y.
740 xg_prepare_tooltip must have been called before this function. */
742 void
743 xg_show_tooltip (FRAME_PTR f, int root_x, int root_y)
745 #ifdef USE_GTK_TOOLTIP
746 struct x_output *x = f->output_data.x;
747 if (x->ttip_window)
749 BLOCK_INPUT;
750 gtk_window_move (x->ttip_window, root_x, root_y);
751 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
752 UNBLOCK_INPUT;
754 #endif
757 /* Hide tooltip if shown. Do nothing if not shown.
758 Return non-zero if tip was hidden, non-zero if not (i.e. not using
759 system tooltips). */
762 xg_hide_tooltip (FRAME_PTR f)
764 int ret = 0;
765 #ifdef USE_GTK_TOOLTIP
766 if (f->output_data.x->ttip_window)
768 GtkWindow *win = f->output_data.x->ttip_window;
769 BLOCK_INPUT;
770 gtk_widget_hide (GTK_WIDGET (win));
772 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
774 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
775 GdkScreen *screen = gdk_window_get_screen (gwin);
776 GtkSettings *settings = gtk_settings_get_for_screen (screen);
777 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
779 UNBLOCK_INPUT;
781 ret = 1;
783 #endif
784 return ret;
788 /***********************************************************************
789 General functions for creating widgets, resizing, events, e.t.c.
790 ***********************************************************************/
792 /* Make a geometry string and pass that to GTK. It seems this is the
793 only way to get geometry position right if the user explicitly
794 asked for a position when starting Emacs.
795 F is the frame we shall set geometry for. */
797 static void
798 xg_set_geometry (FRAME_PTR f)
800 if (f->size_hint_flags & (USPosition | PPosition))
802 int left = f->left_pos;
803 int xneg = f->size_hint_flags & XNegative;
804 int top = f->top_pos;
805 int yneg = f->size_hint_flags & YNegative;
806 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
808 if (xneg)
809 left = -left;
810 if (yneg)
811 top = -top;
813 sprintf (geom_str, "=%dx%d%c%d%c%d",
814 FRAME_PIXEL_WIDTH (f),
815 FRAME_PIXEL_HEIGHT (f),
816 (xneg ? '-' : '+'), left,
817 (yneg ? '-' : '+'), top);
819 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
820 geom_str))
821 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
825 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
826 and use a GtkFixed widget, this doesn't happen automatically. */
828 static void
829 xg_clear_under_internal_border (FRAME_PTR f)
831 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
833 GtkWidget *wfixed = f->output_data.x->edit_widget;
834 gtk_widget_queue_draw (wfixed);
835 gdk_window_process_all_updates ();
836 x_clear_area (FRAME_X_DISPLAY (f),
837 FRAME_X_WINDOW (f),
838 0, 0,
839 FRAME_PIXEL_WIDTH (f),
840 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
841 x_clear_area (FRAME_X_DISPLAY (f),
842 FRAME_X_WINDOW (f),
843 0, 0,
844 FRAME_INTERNAL_BORDER_WIDTH (f),
845 FRAME_PIXEL_HEIGHT (f), 0);
846 x_clear_area (FRAME_X_DISPLAY (f),
847 FRAME_X_WINDOW (f),
848 0, FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
849 FRAME_PIXEL_WIDTH (f),
850 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
851 x_clear_area (FRAME_X_DISPLAY (f),
852 FRAME_X_WINDOW (f),
853 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
855 FRAME_INTERNAL_BORDER_WIDTH (f),
856 FRAME_PIXEL_HEIGHT (f), 0);
860 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
861 and a Gtk+ menu bar, we get resize events for the edit part of the
862 frame only. We let Gtk+ deal with the Gtk+ parts.
863 F is the frame to resize.
864 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
866 void
867 xg_frame_resized (FRAME_PTR f, int pixelwidth, int pixelheight)
869 int rows, columns;
871 if (pixelwidth == -1 && pixelheight == -1)
873 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
874 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
875 0, 0,
876 &pixelwidth, &pixelheight);
877 else return;
881 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
882 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
884 if (columns != FRAME_COLS (f)
885 || rows != FRAME_LINES (f)
886 || pixelwidth != FRAME_PIXEL_WIDTH (f)
887 || pixelheight != FRAME_PIXEL_HEIGHT (f))
889 FRAME_PIXEL_WIDTH (f) = pixelwidth;
890 FRAME_PIXEL_HEIGHT (f) = pixelheight;
892 xg_clear_under_internal_border (f);
893 change_frame_size (f, rows, columns, 0, 1, 0);
894 SET_FRAME_GARBAGED (f);
895 cancel_mouse_face (f);
899 /* Resize the outer window of frame F after changing the height.
900 COLUMNS/ROWS is the size the edit area shall have after the resize. */
902 void
903 xg_frame_set_char_size (FRAME_PTR f, int cols, int rows)
905 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
906 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
907 int pixelwidth;
909 if (FRAME_PIXEL_HEIGHT (f) == 0)
910 return;
912 /* Take into account the size of the scroll bar. Always use the
913 number of columns occupied by the scroll bar here otherwise we
914 might end up with a frame width that is not a multiple of the
915 frame's character width which is bad for vertically split
916 windows. */
917 f->scroll_bar_actual_width
918 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
920 compute_fringe_widths (f, 0);
922 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
923 after calculating that value. */
924 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
925 + FRAME_TOOLBAR_WIDTH (f);
928 /* Do this before resize, as we don't know yet if we will be resized. */
929 xg_clear_under_internal_border (f);
931 /* Must resize our top level widget. Font size may have changed,
932 but not rows/cols. */
933 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
934 pixelwidth, pixelheight);
935 x_wm_set_size_hint (f, 0, 0);
937 SET_FRAME_GARBAGED (f);
938 cancel_mouse_face (f);
940 /* We can not call change_frame_size for a mapped frame,
941 we can not set pixel width/height either. The window manager may
942 override our resize request, XMonad does this all the time.
943 The best we can do is try to sync, so lisp code sees the updated
944 size as fast as possible.
945 For unmapped windows, we can set rows/cols. When
946 the frame is mapped again we will (hopefully) get the correct size. */
947 if (f->async_visible)
949 /* Must call this to flush out events */
950 (void)gtk_events_pending ();
951 gdk_flush ();
952 x_wait_for_event (f, ConfigureNotify);
954 else
956 change_frame_size (f, rows, cols, 0, 1, 0);
957 FRAME_PIXEL_WIDTH (f) = pixelwidth;
958 FRAME_PIXEL_HEIGHT (f) = pixelheight;
962 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
963 The policy is to keep the number of editable lines. */
965 static void
966 xg_height_or_width_changed (FRAME_PTR f)
968 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
969 FRAME_TOTAL_PIXEL_WIDTH (f),
970 FRAME_TOTAL_PIXEL_HEIGHT (f));
971 f->output_data.x->hint_flags = 0;
972 x_wm_set_size_hint (f, 0, 0);
975 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
976 Must be done like this, because GtkWidget:s can have "hidden"
977 X Window that aren't accessible.
979 Return 0 if no widget match WDESC. */
981 GtkWidget *
982 xg_win_to_widget (Display *dpy, Window wdesc)
984 gpointer gdkwin;
985 GtkWidget *gwdesc = 0;
987 BLOCK_INPUT;
989 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
990 wdesc);
991 if (gdkwin)
993 GdkEvent event;
994 event.any.window = gdkwin;
995 event.any.type = GDK_NOTHING;
996 gwdesc = gtk_get_event_widget (&event);
999 UNBLOCK_INPUT;
1000 return gwdesc;
1003 /* Set the background of widget W to PIXEL. */
1005 static void
1006 xg_set_widget_bg (FRAME_PTR f, GtkWidget *w, long unsigned int pixel)
1008 #ifdef HAVE_GTK3
1009 GdkRGBA bg;
1010 XColor xbg;
1011 xbg.pixel = pixel;
1012 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1014 bg.red = (double)xbg.red/65536.0;
1015 bg.green = (double)xbg.green/65536.0;
1016 bg.blue = (double)xbg.blue/65536.0;
1017 bg.alpha = 1.0;
1018 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1020 #else
1021 GdkColor bg;
1022 GdkColormap *map = gtk_widget_get_colormap (w);
1023 gdk_colormap_query_color (map, pixel, &bg);
1024 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1025 #endif
1028 /* Callback called when the gtk theme changes.
1029 We notify lisp code so it can fix faces used for region for example. */
1031 static void
1032 style_changed_cb (GObject *go,
1033 GParamSpec *spec,
1034 gpointer user_data)
1036 struct input_event event;
1037 GdkDisplay *gdpy = (GdkDisplay *) user_data;
1038 const char *display_name = gdk_display_get_name (gdpy);
1039 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1041 EVENT_INIT (event);
1042 event.kind = CONFIG_CHANGED_EVENT;
1043 event.frame_or_window = build_string (display_name);
1044 /* Theme doesn't change often, so intern is called seldom. */
1045 event.arg = intern ("theme-name");
1046 kbd_buffer_store_event (&event);
1048 update_theme_scrollbar_width ();
1050 /* If scroll bar width changed, we need set the new size on all frames
1051 on this display. */
1052 if (dpy)
1054 Lisp_Object rest, frame;
1055 FOR_EACH_FRAME (rest, frame)
1057 FRAME_PTR f = XFRAME (frame);
1058 if (FRAME_X_DISPLAY (f) == dpy)
1060 x_set_scroll_bar_default_width (f);
1061 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
1067 /* Called when a delete-event occurs on WIDGET. */
1069 static gboolean
1070 delete_cb (GtkWidget *widget,
1071 GdkEvent *event,
1072 gpointer user_data)
1074 #ifdef HAVE_GTK3
1075 /* The event doesn't arrive in the normal event loop. Send event
1076 here. */
1077 FRAME_PTR f = (FRAME_PTR) user_data;
1078 struct input_event ie;
1080 EVENT_INIT (ie);
1081 ie.kind = DELETE_WINDOW_EVENT;
1082 XSETFRAME (ie.frame_or_window, f);
1083 kbd_buffer_store_event (&ie);
1084 #endif
1086 return TRUE;
1089 /* Create and set up the GTK widgets for frame F.
1090 Return 0 if creation failed, non-zero otherwise. */
1093 xg_create_frame_widgets (FRAME_PTR f)
1095 GtkWidget *wtop;
1096 GtkWidget *wvbox, *whbox;
1097 GtkWidget *wfixed;
1098 #ifndef HAVE_GTK3
1099 GtkRcStyle *style;
1100 #endif
1101 char *title = 0;
1103 BLOCK_INPUT;
1105 if (FRAME_X_EMBEDDED_P (f))
1107 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1108 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1110 else
1111 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1113 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1114 has backported it to Gtk+ 2.0 and they add the resize grip for
1115 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1116 forever, so disable the grip. */
1117 #if GTK_MAJOR_VERSION < 3 && defined (HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1118 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1119 #endif
1121 xg_set_screen (wtop, f);
1123 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1124 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1125 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1126 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1128 #ifdef HAVE_GTK3
1129 wfixed = emacs_fixed_new (f);
1130 #else
1131 wfixed = gtk_fixed_new ();
1132 #endif
1134 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1136 if (wtop) gtk_widget_destroy (wtop);
1137 if (wvbox) gtk_widget_destroy (wvbox);
1138 if (whbox) gtk_widget_destroy (whbox);
1139 if (wfixed) gtk_widget_destroy (wfixed);
1141 UNBLOCK_INPUT;
1142 return 0;
1145 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1146 gtk_widget_set_name (wtop, EMACS_CLASS);
1147 gtk_widget_set_name (wvbox, "pane");
1148 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1150 /* If this frame has a title or name, set it in the title bar. */
1151 if (! NILP (f->title))
1152 title = SSDATA (ENCODE_UTF_8 (f->title));
1153 else if (! NILP (f->name))
1154 title = SSDATA (ENCODE_UTF_8 (f->name));
1156 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1158 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1159 FRAME_GTK_WIDGET (f) = wfixed;
1160 f->output_data.x->vbox_widget = wvbox;
1161 f->output_data.x->hbox_widget = whbox;
1163 gtk_widget_set_has_window (wfixed, TRUE);
1165 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1166 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1167 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1169 if (FRAME_EXTERNAL_TOOL_BAR (f))
1170 update_frame_tool_bar (f);
1172 /* We don't want this widget double buffered, because we draw on it
1173 with regular X drawing primitives, so from a GTK/GDK point of
1174 view, the widget is totally blank. When an expose comes, this
1175 will make the widget blank, and then Emacs redraws it. This flickers
1176 a lot, so we turn off double buffering. */
1177 gtk_widget_set_double_buffered (wfixed, FALSE);
1179 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1180 SSDATA (Vx_resource_name),
1181 SSDATA (Vx_resource_class));
1183 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1184 GTK is to destroy the widget. We want Emacs to do that instead. */
1185 g_signal_connect (G_OBJECT (wtop), "delete-event",
1186 G_CALLBACK (delete_cb), f);
1188 /* Convert our geometry parameters into a geometry string
1189 and specify it.
1190 GTK will itself handle calculating the real position this way. */
1191 xg_set_geometry (f);
1192 f->win_gravity
1193 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1195 gtk_widget_add_events (wfixed,
1196 GDK_POINTER_MOTION_MASK
1197 | GDK_EXPOSURE_MASK
1198 | GDK_BUTTON_PRESS_MASK
1199 | GDK_BUTTON_RELEASE_MASK
1200 | GDK_KEY_PRESS_MASK
1201 | GDK_ENTER_NOTIFY_MASK
1202 | GDK_LEAVE_NOTIFY_MASK
1203 | GDK_FOCUS_CHANGE_MASK
1204 | GDK_STRUCTURE_MASK
1205 | GDK_VISIBILITY_NOTIFY_MASK);
1207 /* Must realize the windows so the X window gets created. It is used
1208 by callers of this function. */
1209 gtk_widget_realize (wfixed);
1210 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1212 /* Since GTK clears its window by filling with the background color,
1213 we must keep X and GTK background in sync. */
1214 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1216 #ifndef HAVE_GTK3
1217 /* Also, do not let any background pixmap to be set, this looks very
1218 bad as Emacs overwrites the background pixmap with its own idea
1219 of background color. */
1220 style = gtk_widget_get_modifier_style (wfixed);
1222 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1223 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1224 gtk_widget_modify_style (wfixed, style);
1225 #else
1226 gtk_widget_set_can_focus (wfixed, TRUE);
1227 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1228 #endif
1230 #ifdef USE_GTK_TOOLTIP
1231 /* Steal a tool tip window we can move ourselves. */
1232 f->output_data.x->ttip_widget = 0;
1233 f->output_data.x->ttip_lbl = 0;
1234 f->output_data.x->ttip_window = 0;
1235 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1236 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1237 #endif
1240 GdkScreen *screen = gtk_widget_get_screen (wtop);
1241 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1242 /* Only connect this signal once per screen. */
1243 if (! g_signal_handler_find (G_OBJECT (gs),
1244 G_SIGNAL_MATCH_FUNC,
1245 0, 0, 0,
1246 G_CALLBACK (style_changed_cb),
1249 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1250 G_CALLBACK (style_changed_cb),
1251 gdk_screen_get_display (screen));
1255 UNBLOCK_INPUT;
1257 return 1;
1260 void
1261 xg_free_frame_widgets (FRAME_PTR f)
1263 if (FRAME_GTK_OUTER_WIDGET (f))
1265 #ifdef USE_GTK_TOOLTIP
1266 struct x_output *x = f->output_data.x;
1267 #endif
1268 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1269 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1270 FRAME_GTK_OUTER_WIDGET (f) = 0;
1271 #ifdef USE_GTK_TOOLTIP
1272 if (x->ttip_lbl)
1273 gtk_widget_destroy (x->ttip_lbl);
1274 if (x->ttip_widget)
1275 g_object_unref (G_OBJECT (x->ttip_widget));
1276 #endif
1280 /* Set the normal size hints for the window manager, for frame F.
1281 FLAGS is the flags word to use--or 0 meaning preserve the flags
1282 that the window now has.
1283 If USER_POSITION is nonzero, we set the User Position
1284 flag (this is useful when FLAGS is 0). */
1286 void
1287 x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
1289 /* Must use GTK routines here, otherwise GTK resets the size hints
1290 to its own defaults. */
1291 GdkGeometry size_hints;
1292 gint hint_flags = 0;
1293 int base_width, base_height;
1294 int min_rows = 0, min_cols = 0;
1295 int win_gravity = f->win_gravity;
1297 /* Don't set size hints during initialization; that apparently leads
1298 to a race condition. See the thread at
1299 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1300 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1301 return;
1303 if (flags)
1305 memset (&size_hints, 0, sizeof (size_hints));
1306 f->output_data.x->size_hints = size_hints;
1307 f->output_data.x->hint_flags = hint_flags;
1309 else
1310 flags = f->size_hint_flags;
1312 size_hints = f->output_data.x->size_hints;
1313 hint_flags = f->output_data.x->hint_flags;
1315 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1316 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
1317 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
1319 hint_flags |= GDK_HINT_BASE_SIZE;
1320 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
1321 /* Use one row here so base_height does not become zero.
1322 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1323 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1324 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1326 check_frame_size (f, &min_rows, &min_cols);
1327 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1329 size_hints.base_width = base_width;
1330 size_hints.base_height = base_height;
1331 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
1332 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
1334 /* These currently have a one to one mapping with the X values, but I
1335 don't think we should rely on that. */
1336 hint_flags |= GDK_HINT_WIN_GRAVITY;
1337 size_hints.win_gravity = 0;
1338 if (win_gravity == NorthWestGravity)
1339 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1340 else if (win_gravity == NorthGravity)
1341 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1342 else if (win_gravity == NorthEastGravity)
1343 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1344 else if (win_gravity == WestGravity)
1345 size_hints.win_gravity = GDK_GRAVITY_WEST;
1346 else if (win_gravity == CenterGravity)
1347 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1348 else if (win_gravity == EastGravity)
1349 size_hints.win_gravity = GDK_GRAVITY_EAST;
1350 else if (win_gravity == SouthWestGravity)
1351 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1352 else if (win_gravity == SouthGravity)
1353 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1354 else if (win_gravity == SouthEastGravity)
1355 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1356 else if (win_gravity == StaticGravity)
1357 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1359 if (user_position)
1361 hint_flags &= ~GDK_HINT_POS;
1362 hint_flags |= GDK_HINT_USER_POS;
1365 if (hint_flags != f->output_data.x->hint_flags
1366 || memcmp (&size_hints,
1367 &f->output_data.x->size_hints,
1368 sizeof (size_hints)) != 0)
1370 BLOCK_INPUT;
1371 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1372 NULL, &size_hints, hint_flags);
1373 f->output_data.x->size_hints = size_hints;
1374 f->output_data.x->hint_flags = hint_flags;
1375 UNBLOCK_INPUT;
1379 /* Change background color of a frame.
1380 Since GTK uses the background color to clear the window, we must
1381 keep the GTK and X colors in sync.
1382 F is the frame to change,
1383 BG is the pixel value to change to. */
1385 void
1386 xg_set_background_color (FRAME_PTR f, long unsigned int bg)
1388 if (FRAME_GTK_WIDGET (f))
1390 BLOCK_INPUT;
1391 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1392 UNBLOCK_INPUT;
1397 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1398 functions so GTK does not overwrite the icon. */
1400 void
1401 xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
1403 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1404 icon_pixmap,
1405 icon_mask);
1406 if (gp)
1407 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1412 /***********************************************************************
1413 Dialog functions
1414 ***********************************************************************/
1415 /* Return the dialog title to use for a dialog of type KEY.
1416 This is the encoding used by lwlib. We use the same for GTK. */
1418 static const char *
1419 get_dialog_title (char key)
1421 const char *title = "";
1423 switch (key) {
1424 case 'E': case 'e':
1425 title = "Error";
1426 break;
1428 case 'I': case 'i':
1429 title = "Information";
1430 break;
1432 case 'L': case 'l':
1433 title = "Prompt";
1434 break;
1436 case 'P': case 'p':
1437 title = "Prompt";
1438 break;
1440 case 'Q': case 'q':
1441 title = "Question";
1442 break;
1445 return title;
1448 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1449 the dialog, but return TRUE so the event does not propagate further
1450 in GTK. This prevents GTK from destroying the dialog widget automatically
1451 and we can always destroy the widget manually, regardless of how
1452 it was popped down (button press or WM_DELETE_WINDOW).
1453 W is the dialog widget.
1454 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1455 user_data is NULL (not used).
1457 Returns TRUE to end propagation of event. */
1459 static gboolean
1460 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1462 gtk_widget_unmap (w);
1463 return TRUE;
1466 /* Create a popup dialog window. See also xg_create_widget below.
1467 WV is a widget_value describing the dialog.
1468 SELECT_CB is the callback to use when a button has been pressed.
1469 DEACTIVATE_CB is the callback to use when the dialog pops down.
1471 Returns the GTK dialog widget. */
1473 static GtkWidget *
1474 create_dialog (widget_value *wv,
1475 GCallback select_cb,
1476 GCallback deactivate_cb)
1478 const char *title = get_dialog_title (wv->name[0]);
1479 int total_buttons = wv->name[1] - '0';
1480 int right_buttons = wv->name[4] - '0';
1481 int left_buttons;
1482 int button_nr = 0;
1483 int button_spacing = 10;
1484 GtkWidget *wdialog = gtk_dialog_new ();
1485 GtkDialog *wd = GTK_DIALOG (wdialog);
1486 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1487 widget_value *item;
1488 GtkWidget *whbox_down;
1490 /* If the number of buttons is greater than 4, make two rows of buttons
1491 instead. This looks better. */
1492 int make_two_rows = total_buttons > 4;
1494 if (right_buttons == 0) right_buttons = total_buttons/2;
1495 left_buttons = total_buttons - right_buttons;
1497 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1498 gtk_widget_set_name (wdialog, "emacs-dialog");
1501 if (make_two_rows)
1503 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1504 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1505 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1506 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1507 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1508 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1510 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1511 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1512 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1514 cur_box = GTK_BOX (whbox_up);
1517 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1518 G_CALLBACK (dialog_delete_callback), 0);
1520 if (deactivate_cb)
1522 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1523 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1526 for (item = wv->contents; item; item = item->next)
1528 char *utf8_label = get_utf8_string (item->value);
1529 GtkWidget *w;
1530 GtkRequisition req;
1532 if (item->name && strcmp (item->name, "message") == 0)
1534 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1535 /* This is the text part of the dialog. */
1536 w = gtk_label_new (utf8_label);
1537 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1538 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1539 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1541 /* Try to make dialog look better. Must realize first so
1542 the widget can calculate the size it needs. */
1543 gtk_widget_realize (w);
1544 gtk_widget_get_preferred_size (w, NULL, &req);
1545 gtk_box_set_spacing (wvbox, req.height);
1546 if (item->value && strlen (item->value) > 0)
1547 button_spacing = 2*req.width/strlen (item->value);
1549 else
1551 /* This is one button to add to the dialog. */
1552 w = gtk_button_new_with_label (utf8_label);
1553 if (! item->enabled)
1554 gtk_widget_set_sensitive (w, FALSE);
1555 if (select_cb)
1556 g_signal_connect (G_OBJECT (w), "clicked",
1557 select_cb, item->call_data);
1559 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1560 if (++button_nr == left_buttons)
1562 if (make_two_rows)
1563 cur_box = GTK_BOX (whbox_down);
1564 else
1565 gtk_box_pack_start (cur_box,
1566 gtk_label_new (""),
1567 TRUE, TRUE,
1568 button_spacing);
1572 if (utf8_label)
1573 g_free (utf8_label);
1576 return wdialog;
1579 struct xg_dialog_data
1581 GMainLoop *loop;
1582 int response;
1583 GtkWidget *w;
1584 guint timerid;
1587 /* Function that is called when the file or font dialogs pop down.
1588 W is the dialog widget, RESPONSE is the response code.
1589 USER_DATA is what we passed in to g_signal_connect. */
1591 static void
1592 xg_dialog_response_cb (GtkDialog *w,
1593 gint response,
1594 gpointer user_data)
1596 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1597 dd->response = response;
1598 g_main_loop_quit (dd->loop);
1602 /* Destroy the dialog. This makes it pop down. */
1604 static Lisp_Object
1605 pop_down_dialog (Lisp_Object arg)
1607 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1608 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1610 BLOCK_INPUT;
1611 if (dd->w) gtk_widget_destroy (dd->w);
1612 if (dd->timerid != 0) g_source_remove (dd->timerid);
1614 g_main_loop_quit (dd->loop);
1615 g_main_loop_unref (dd->loop);
1617 UNBLOCK_INPUT;
1619 return Qnil;
1622 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1623 We pass in DATA as gpointer* so we can use this as a callback. */
1625 static gboolean
1626 xg_maybe_add_timer (gpointer data)
1628 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1629 EMACS_TIME next_time = timer_check ();
1631 dd->timerid = 0;
1633 if (EMACS_TIME_VALID_P (next_time))
1635 time_t s = EMACS_SECS (next_time);
1636 int per_ms = EMACS_TIME_RESOLUTION / 1000;
1637 int ms = (EMACS_NSECS (next_time) + per_ms - 1) / per_ms;
1638 if (s <= ((guint) -1 - ms) / 1000)
1639 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1641 return FALSE;
1645 /* Pops up a modal dialog W and waits for response.
1646 We don't use gtk_dialog_run because we want to process emacs timers.
1647 The dialog W is not destroyed when this function returns. */
1649 static int
1650 xg_dialog_run (FRAME_PTR f, GtkWidget *w)
1652 ptrdiff_t count = SPECPDL_INDEX ();
1653 struct xg_dialog_data dd;
1655 xg_set_screen (w, f);
1656 gtk_window_set_transient_for (GTK_WINDOW (w),
1657 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1658 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1659 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1661 dd.loop = g_main_loop_new (NULL, FALSE);
1662 dd.response = GTK_RESPONSE_CANCEL;
1663 dd.w = w;
1664 dd.timerid = 0;
1666 g_signal_connect (G_OBJECT (w),
1667 "response",
1668 G_CALLBACK (xg_dialog_response_cb),
1669 &dd);
1670 /* Don't destroy the widget if closed by the window manager close button. */
1671 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1672 gtk_widget_show (w);
1674 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1676 (void) xg_maybe_add_timer (&dd);
1677 g_main_loop_run (dd.loop);
1679 dd.w = 0;
1680 unbind_to (count, Qnil);
1682 return dd.response;
1686 /***********************************************************************
1687 File dialog functions
1688 ***********************************************************************/
1689 /* Return non-zero if the old file selection dialog is being used.
1690 Return zero if not. */
1693 xg_uses_old_file_dialog (void)
1695 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1696 return x_gtk_use_old_file_dialog;
1697 #else
1698 return 0;
1699 #endif
1703 typedef char * (*xg_get_file_func) (GtkWidget *);
1705 /* Return the selected file for file chooser dialog W.
1706 The returned string must be free:d. */
1708 static char *
1709 xg_get_file_name_from_chooser (GtkWidget *w)
1711 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1714 /* Callback called when the "Show hidden files" toggle is pressed.
1715 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1717 static void
1718 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1720 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1721 gboolean visible;
1722 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1723 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1727 /* Callback called when a property changes in a file chooser.
1728 GOBJECT is the file chooser dialog, ARG1 describes the property.
1729 USER_DATA is the toggle widget in the file chooser dialog.
1730 We use this to update the "Show hidden files" toggle when the user
1731 changes that property by right clicking in the file list. */
1733 static void
1734 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1736 if (strcmp (arg1->name, "show-hidden") == 0)
1738 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1739 gboolean visible, toggle_on;
1741 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1742 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1744 if (!!visible != !!toggle_on)
1746 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1747 G_CALLBACK (xg_toggle_visibility_cb),
1748 gobject);
1749 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1750 g_signal_handlers_unblock_by_func
1751 (G_OBJECT (wtoggle),
1752 G_CALLBACK (xg_toggle_visibility_cb),
1753 gobject);
1755 x_gtk_show_hidden_files = visible;
1759 /* Read a file name from the user using a file chooser dialog.
1760 F is the current frame.
1761 PROMPT is a prompt to show to the user. May not be NULL.
1762 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1763 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1764 file. (Actually, this only has cosmetic effects, the user can
1765 still enter a non-existing file.) *FUNC is set to a function that
1766 can be used to retrieve the selected file name from the returned widget.
1768 Returns the created widget. */
1770 static GtkWidget *
1771 xg_get_file_with_chooser (FRAME_PTR f,
1772 char *prompt,
1773 char *default_filename,
1774 int mustmatch_p, int only_dir_p,
1775 xg_get_file_func *func)
1777 char msgbuf[1024];
1779 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1780 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1781 GtkFileChooserAction action = (mustmatch_p ?
1782 GTK_FILE_CHOOSER_ACTION_OPEN :
1783 GTK_FILE_CHOOSER_ACTION_SAVE);
1785 if (only_dir_p)
1786 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1788 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1789 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1790 (mustmatch_p || only_dir_p ?
1791 GTK_STOCK_OPEN : GTK_STOCK_OK),
1792 GTK_RESPONSE_OK,
1793 NULL);
1794 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1796 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1797 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1798 gtk_widget_show (wbox);
1799 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1801 if (x_gtk_show_hidden_files)
1803 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1804 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1806 gtk_widget_show (wtoggle);
1807 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1808 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1809 g_signal_connect (G_OBJECT (filewin), "notify",
1810 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1812 if (x_gtk_file_dialog_help_text)
1814 msgbuf[0] = '\0';
1815 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1816 Show the C-l help text only for versions < 2.10. */
1817 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1818 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1819 strcat (msgbuf, "\nIf you don't like this file selector, use the "
1820 "corresponding\nkey binding or customize "
1821 "use-file-dialog to turn it off.");
1823 wmessage = gtk_label_new (msgbuf);
1824 gtk_widget_show (wmessage);
1827 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1828 if (x_gtk_file_dialog_help_text)
1829 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1830 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1832 if (default_filename)
1834 Lisp_Object file;
1835 struct gcpro gcpro1;
1836 char *utf8_filename;
1837 GCPRO1 (file);
1839 file = build_string (default_filename);
1841 /* File chooser does not understand ~/... in the file name. It must be
1842 an absolute name starting with /. */
1843 if (default_filename[0] != '/')
1844 file = Fexpand_file_name (file, Qnil);
1846 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1847 if (! NILP (Ffile_directory_p (file)))
1848 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1849 utf8_filename);
1850 else
1852 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1853 utf8_filename);
1854 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1856 char *cp = strrchr (utf8_filename, '/');
1857 if (cp) ++cp;
1858 else cp = utf8_filename;
1859 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1863 UNGCPRO;
1866 *func = xg_get_file_name_from_chooser;
1867 return filewin;
1870 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1872 /* Return the selected file for file selector dialog W.
1873 The returned string must be free:d. */
1875 static char *
1876 xg_get_file_name_from_selector (GtkWidget *w)
1878 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1879 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1882 /* Create a file selection dialog.
1883 F is the current frame.
1884 PROMPT is a prompt to show to the user. May not be NULL.
1885 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1886 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1887 file. *FUNC is set to a function that can be used to retrieve the
1888 selected file name from the returned widget.
1890 Returns the created widget. */
1892 static GtkWidget *
1893 xg_get_file_with_selection (FRAME_PTR f,
1894 char *prompt,
1895 char *default_filename,
1896 int mustmatch_p, int only_dir_p,
1897 xg_get_file_func *func)
1899 GtkWidget *filewin;
1900 GtkFileSelection *filesel;
1902 filewin = gtk_file_selection_new (prompt);
1903 filesel = GTK_FILE_SELECTION (filewin);
1905 if (default_filename)
1906 gtk_file_selection_set_filename (filesel, default_filename);
1908 if (mustmatch_p)
1910 /* The selection_entry part of filesel is not documented. */
1911 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1912 gtk_file_selection_hide_fileop_buttons (filesel);
1915 *func = xg_get_file_name_from_selector;
1917 return filewin;
1919 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1921 /* Read a file name from the user using a file dialog, either the old
1922 file selection dialog, or the new file chooser dialog. Which to use
1923 depends on what the GTK version used has, and what the value of
1924 gtk-use-old-file-dialog.
1925 F is the current frame.
1926 PROMPT is a prompt to show to the user. May not be NULL.
1927 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1928 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1929 file.
1931 Returns a file name or NULL if no file was selected.
1932 The returned string must be freed by the caller. */
1934 char *
1935 xg_get_file_name (FRAME_PTR f,
1936 char *prompt,
1937 char *default_filename,
1938 int mustmatch_p,
1939 int only_dir_p)
1941 GtkWidget *w = 0;
1942 char *fn = 0;
1943 int filesel_done = 0;
1944 xg_get_file_func func;
1946 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1947 /* I really don't know why this is needed, but without this the GLIBC add on
1948 library linuxthreads hangs when the Gnome file chooser backend creates
1949 threads. */
1950 sigblock (sigmask (__SIGRTMIN));
1951 #endif /* HAVE_PTHREAD */
1953 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1955 if (xg_uses_old_file_dialog ())
1956 w = xg_get_file_with_selection (f, prompt, default_filename,
1957 mustmatch_p, only_dir_p, &func);
1958 else
1959 w = xg_get_file_with_chooser (f, prompt, default_filename,
1960 mustmatch_p, only_dir_p, &func);
1962 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1963 w = xg_get_file_with_chooser (f, prompt, default_filename,
1964 mustmatch_p, only_dir_p, &func);
1965 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1967 gtk_widget_set_name (w, "emacs-filedialog");
1969 filesel_done = xg_dialog_run (f, w);
1971 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
1972 sigunblock (sigmask (__SIGRTMIN));
1973 #endif
1975 if (filesel_done == GTK_RESPONSE_OK)
1976 fn = (*func) (w);
1978 gtk_widget_destroy (w);
1979 return fn;
1982 #ifdef HAVE_FREETYPE
1983 /* Pop up a GTK font selector and return the name of the font the user
1984 selects, as a C string. The returned font name follows GTK's own
1985 format:
1987 `FAMILY [VALUE1 VALUE2] SIZE'
1989 This can be parsed using font_parse_fcname in font.c.
1990 DEFAULT_NAME, if non-zero, is the default font name. */
1992 char *
1993 xg_get_font_name (FRAME_PTR f, const char *default_name)
1995 GtkWidget *w;
1996 char *fontname = NULL;
1997 int done = 0;
1999 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
2000 sigblock (sigmask (__SIGRTMIN));
2001 #endif /* HAVE_PTHREAD */
2003 w = gtk_font_selection_dialog_new ("Pick a font");
2004 if (!default_name)
2005 default_name = "Monospace 10";
2006 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
2007 default_name);
2009 gtk_widget_set_name (w, "emacs-fontdialog");
2011 done = xg_dialog_run (f, w);
2013 #if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
2014 sigunblock (sigmask (__SIGRTMIN));
2015 #endif
2017 if (done == GTK_RESPONSE_OK)
2018 fontname = gtk_font_selection_dialog_get_font_name
2019 (GTK_FONT_SELECTION_DIALOG (w));
2021 gtk_widget_destroy (w);
2022 return fontname;
2024 #endif /* HAVE_FREETYPE */
2028 /***********************************************************************
2029 Menu functions.
2030 ***********************************************************************/
2032 /* The name of menu items that can be used for customization. Since GTK
2033 RC files are very crude and primitive, we have to set this on all
2034 menu item names so a user can easily customize menu items. */
2036 #define MENU_ITEM_NAME "emacs-menuitem"
2039 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2040 during GC. The next member points to the items. */
2041 static xg_list_node xg_menu_cb_list;
2043 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2044 during GC. The next member points to the items. */
2045 static xg_list_node xg_menu_item_cb_list;
2047 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2048 F is the frame CL_DATA will be initialized for.
2049 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2051 The menu bar and all sub menus under the menu bar in a frame
2052 share the same structure, hence the reference count.
2054 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2055 allocated xg_menu_cb_data if CL_DATA is NULL. */
2057 static xg_menu_cb_data *
2058 make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
2060 if (! cl_data)
2062 cl_data = xmalloc (sizeof *cl_data);
2063 cl_data->f = f;
2064 cl_data->menu_bar_vector = f->menu_bar_vector;
2065 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2066 cl_data->highlight_cb = highlight_cb;
2067 cl_data->ref_count = 0;
2069 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2072 cl_data->ref_count++;
2074 return cl_data;
2077 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2078 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2080 When the menu bar is updated, menu items may have been added and/or
2081 removed, so menu_bar_vector and menu_bar_items_used change. We must
2082 then update CL_DATA since it is used to determine which menu
2083 item that is invoked in the menu.
2084 HIGHLIGHT_CB could change, there is no check that the same
2085 function is given when modifying a menu bar as was given when
2086 creating the menu bar. */
2088 static void
2089 update_cl_data (xg_menu_cb_data *cl_data,
2090 FRAME_PTR f,
2091 GCallback highlight_cb)
2093 if (cl_data)
2095 cl_data->f = f;
2096 cl_data->menu_bar_vector = f->menu_bar_vector;
2097 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2098 cl_data->highlight_cb = highlight_cb;
2102 /* Decrease reference count for CL_DATA.
2103 If reference count is zero, free CL_DATA. */
2105 static void
2106 unref_cl_data (xg_menu_cb_data *cl_data)
2108 if (cl_data && cl_data->ref_count > 0)
2110 cl_data->ref_count--;
2111 if (cl_data->ref_count == 0)
2113 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2114 xfree (cl_data);
2119 /* Function that marks all lisp data during GC. */
2121 void
2122 xg_mark_data (void)
2124 xg_list_node *iter;
2126 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2127 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2129 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2131 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2133 if (! NILP (cb_data->help))
2134 mark_object (cb_data->help);
2139 /* Callback called when a menu item is destroyed. Used to free data.
2140 W is the widget that is being destroyed (not used).
2141 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2143 static void
2144 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2146 if (client_data)
2148 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
2149 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2150 xfree (data);
2154 /* Callback called when the pointer enters/leaves a menu item.
2155 W is the parent of the menu item.
2156 EVENT is either an enter event or leave event.
2157 CLIENT_DATA is not used.
2159 Returns FALSE to tell GTK to keep processing this event. */
2161 static gboolean
2162 menuitem_highlight_callback (GtkWidget *w,
2163 GdkEventCrossing *event,
2164 gpointer client_data)
2166 GdkEvent ev;
2167 GtkWidget *subwidget;
2168 xg_menu_item_cb_data *data;
2170 ev.crossing = *event;
2171 subwidget = gtk_get_event_widget (&ev);
2172 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
2173 XG_ITEM_DATA);
2174 if (data)
2176 if (! NILP (data->help) && data->cl_data->highlight_cb)
2178 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2179 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2180 (*func) (subwidget, call_data);
2184 return FALSE;
2187 /* Callback called when a menu is destroyed. Used to free data.
2188 W is the widget that is being destroyed (not used).
2189 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2191 static void
2192 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2194 unref_cl_data ((xg_menu_cb_data*) client_data);
2197 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2198 must be non-NULL) and can be inserted into a menu item.
2200 Returns the GtkHBox. */
2202 static GtkWidget *
2203 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2205 GtkWidget *wlbl;
2206 GtkWidget *wkey;
2207 GtkWidget *wbox;
2209 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2210 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2211 wlbl = gtk_label_new (utf8_label);
2212 wkey = gtk_label_new (utf8_key);
2214 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2215 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2217 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2218 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2220 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2221 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2222 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2224 return wbox;
2227 /* Make and return a menu item widget with the key to the right.
2228 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2229 UTF8_KEY is the text representing the key binding.
2230 ITEM is the widget_value describing the menu item.
2232 GROUP is an in/out parameter. If the menu item to be created is not
2233 part of any radio menu group, *GROUP contains NULL on entry and exit.
2234 If the menu item to be created is part of a radio menu group, on entry
2235 *GROUP contains the group to use, or NULL if this is the first item
2236 in the group. On exit, *GROUP contains the radio item group.
2238 Unfortunately, keys don't line up as nicely as in Motif,
2239 but the MacOS X version doesn't either, so I guess that is OK. */
2241 static GtkWidget *
2242 make_menu_item (const char *utf8_label,
2243 const char *utf8_key,
2244 widget_value *item,
2245 GSList **group)
2247 GtkWidget *w;
2248 GtkWidget *wtoadd = 0;
2250 /* It has been observed that some menu items have a NULL name field.
2251 This will lead to this function being called with a NULL utf8_label.
2252 GTK crashes on that so we set a blank label. Why there is a NULL
2253 name remains to be investigated. */
2254 if (! utf8_label) utf8_label = " ";
2256 if (utf8_key)
2257 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2259 if (item->button_type == BUTTON_TYPE_TOGGLE)
2261 *group = NULL;
2262 if (utf8_key) w = gtk_check_menu_item_new ();
2263 else w = gtk_check_menu_item_new_with_label (utf8_label);
2264 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2266 else if (item->button_type == BUTTON_TYPE_RADIO)
2268 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2269 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2270 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2271 if (item->selected)
2272 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2274 else
2276 *group = NULL;
2277 if (utf8_key) w = gtk_menu_item_new ();
2278 else w = gtk_menu_item_new_with_label (utf8_label);
2281 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2282 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2284 return w;
2287 static int xg_detached_menus;
2289 /* Returns non-zero if there are detached menus. */
2292 xg_have_tear_offs (void)
2294 return xg_detached_menus > 0;
2297 /* Callback invoked when a detached menu window is removed. Here we
2298 decrease the xg_detached_menus count.
2299 WIDGET is the top level window that is removed (the parent of the menu).
2300 CLIENT_DATA is not used. */
2302 static void
2303 tearoff_remove (GtkWidget *widget, gpointer client_data)
2305 if (xg_detached_menus > 0) --xg_detached_menus;
2308 /* Callback invoked when a menu is detached. It increases the
2309 xg_detached_menus count.
2310 WIDGET is the GtkTearoffMenuItem.
2311 CLIENT_DATA is not used. */
2313 static void
2314 tearoff_activate (GtkWidget *widget, gpointer client_data)
2316 GtkWidget *menu = gtk_widget_get_parent (widget);
2317 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2319 ++xg_detached_menus;
2320 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2321 "destroy",
2322 G_CALLBACK (tearoff_remove), 0);
2327 /* Create a menu item widget, and connect the callbacks.
2328 ITEM describes the menu item.
2329 F is the frame the created menu belongs to.
2330 SELECT_CB is the callback to use when a menu item is selected.
2331 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2332 CL_DATA points to the callback data to be used for this menu.
2333 GROUP is an in/out parameter. If the menu item to be created is not
2334 part of any radio menu group, *GROUP contains NULL on entry and exit.
2335 If the menu item to be created is part of a radio menu group, on entry
2336 *GROUP contains the group to use, or NULL if this is the first item
2337 in the group. On exit, *GROUP contains the radio item group.
2339 Returns the created GtkWidget. */
2341 static GtkWidget *
2342 xg_create_one_menuitem (widget_value *item,
2343 FRAME_PTR f,
2344 GCallback select_cb,
2345 GCallback highlight_cb,
2346 xg_menu_cb_data *cl_data,
2347 GSList **group)
2349 char *utf8_label;
2350 char *utf8_key;
2351 GtkWidget *w;
2352 xg_menu_item_cb_data *cb_data;
2354 utf8_label = get_utf8_string (item->name);
2355 utf8_key = get_utf8_string (item->key);
2357 w = make_menu_item (utf8_label, utf8_key, item, group);
2359 if (utf8_label) g_free (utf8_label);
2360 if (utf8_key) g_free (utf8_key);
2362 cb_data = xmalloc (sizeof *cb_data);
2364 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2366 cb_data->select_id = 0;
2367 cb_data->help = item->help;
2368 cb_data->cl_data = cl_data;
2369 cb_data->call_data = item->call_data;
2371 g_signal_connect (G_OBJECT (w),
2372 "destroy",
2373 G_CALLBACK (menuitem_destroy_callback),
2374 cb_data);
2376 /* Put cb_data in widget, so we can get at it when modifying menubar */
2377 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2379 /* final item, not a submenu */
2380 if (item->call_data && ! item->contents)
2382 if (select_cb)
2383 cb_data->select_id
2384 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2387 return w;
2390 /* Create a full menu tree specified by DATA.
2391 F is the frame the created menu belongs to.
2392 SELECT_CB is the callback to use when a menu item is selected.
2393 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2394 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2395 POP_UP_P is non-zero if we shall create a popup menu.
2396 MENU_BAR_P is non-zero if we shall create a menu bar.
2397 ADD_TEAROFF_P is non-zero if we shall add a tearoff menu item. Ignored
2398 if MENU_BAR_P is non-zero.
2399 TOPMENU is the topmost GtkWidget that others shall be placed under.
2400 It may be NULL, in that case we create the appropriate widget
2401 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2402 CL_DATA is the callback data we shall use for this menu, or NULL
2403 if we haven't set the first callback yet.
2404 NAME is the name to give to the top level menu if this function
2405 creates it. May be NULL to not set any name.
2407 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2408 not NULL.
2410 This function calls itself to create submenus. */
2412 static GtkWidget *
2413 create_menus (widget_value *data,
2414 FRAME_PTR f,
2415 GCallback select_cb,
2416 GCallback deactivate_cb,
2417 GCallback highlight_cb,
2418 int pop_up_p,
2419 int menu_bar_p,
2420 int add_tearoff_p,
2421 GtkWidget *topmenu,
2422 xg_menu_cb_data *cl_data,
2423 const char *name)
2425 widget_value *item;
2426 GtkWidget *wmenu = topmenu;
2427 GSList *group = NULL;
2429 if (! topmenu)
2431 if (! menu_bar_p)
2433 wmenu = gtk_menu_new ();
2434 xg_set_screen (wmenu, f);
2435 /* Connect this to the menu instead of items so we get enter/leave for
2436 disabled items also. TODO: Still does not get enter/leave for
2437 disabled items in detached menus. */
2438 g_signal_connect (G_OBJECT (wmenu),
2439 "enter-notify-event",
2440 G_CALLBACK (menuitem_highlight_callback),
2441 NULL);
2442 g_signal_connect (G_OBJECT (wmenu),
2443 "leave-notify-event",
2444 G_CALLBACK (menuitem_highlight_callback),
2445 NULL);
2447 else
2449 wmenu = gtk_menu_bar_new ();
2450 /* Set width of menu bar to a small value so it doesn't enlarge
2451 a small initial frame size. The width will be set to the
2452 width of the frame later on when it is added to a container.
2453 height -1: Natural height. */
2454 gtk_widget_set_size_request (wmenu, 1, -1);
2457 /* Put cl_data on the top menu for easier access. */
2458 cl_data = make_cl_data (cl_data, f, highlight_cb);
2459 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2460 g_signal_connect (G_OBJECT (wmenu), "destroy",
2461 G_CALLBACK (menu_destroy_callback), cl_data);
2463 if (name)
2464 gtk_widget_set_name (wmenu, name);
2466 if (deactivate_cb)
2467 g_signal_connect (G_OBJECT (wmenu),
2468 "selection-done", deactivate_cb, 0);
2471 if (! menu_bar_p && add_tearoff_p)
2473 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2474 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2476 g_signal_connect (G_OBJECT (tearoff), "activate",
2477 G_CALLBACK (tearoff_activate), 0);
2480 for (item = data; item; item = item->next)
2482 GtkWidget *w;
2484 if (pop_up_p && !item->contents && !item->call_data
2485 && !menu_separator_name_p (item->name))
2487 char *utf8_label;
2488 /* A title for a popup. We do the same as GTK does when
2489 creating titles, but it does not look good. */
2490 group = NULL;
2491 utf8_label = get_utf8_string (item->name);
2493 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2494 w = gtk_menu_item_new_with_label (utf8_label);
2495 gtk_widget_set_sensitive (w, FALSE);
2496 if (utf8_label) g_free (utf8_label);
2498 else if (menu_separator_name_p (item->name))
2500 group = NULL;
2501 /* GTK only have one separator type. */
2502 w = gtk_separator_menu_item_new ();
2504 else
2506 w = xg_create_one_menuitem (item,
2508 item->contents ? 0 : select_cb,
2509 highlight_cb,
2510 cl_data,
2511 &group);
2513 /* Create a possibly empty submenu for menu bar items, since some
2514 themes don't highlight items correctly without it. */
2515 if (item->contents || menu_bar_p)
2517 GtkWidget *submenu = create_menus (item->contents,
2519 select_cb,
2520 deactivate_cb,
2521 highlight_cb,
2524 add_tearoff_p,
2526 cl_data,
2528 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2532 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2533 gtk_widget_set_name (w, MENU_ITEM_NAME);
2536 return wmenu;
2539 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2540 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2541 with some text and buttons.
2542 F is the frame the created item belongs to.
2543 NAME is the name to use for the top widget.
2544 VAL is a widget_value structure describing items to be created.
2545 SELECT_CB is the callback to use when a menu item is selected or
2546 a dialog button is pressed.
2547 DEACTIVATE_CB is the callback to use when an item is deactivated.
2548 For a menu, when a sub menu is not shown anymore, for a dialog it is
2549 called when the dialog is popped down.
2550 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2552 Returns the widget created. */
2554 GtkWidget *
2555 xg_create_widget (const char *type, const char *name, FRAME_PTR f, widget_value *val,
2556 GCallback select_cb, GCallback deactivate_cb,
2557 GCallback highlight_cb)
2559 GtkWidget *w = 0;
2560 int menu_bar_p = strcmp (type, "menubar") == 0;
2561 int pop_up_p = strcmp (type, "popup") == 0;
2563 if (strcmp (type, "dialog") == 0)
2565 w = create_dialog (val, select_cb, deactivate_cb);
2566 xg_set_screen (w, f);
2567 gtk_window_set_transient_for (GTK_WINDOW (w),
2568 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2569 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2570 gtk_widget_set_name (w, "emacs-dialog");
2571 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2573 else if (menu_bar_p || pop_up_p)
2575 w = create_menus (val->contents,
2577 select_cb,
2578 deactivate_cb,
2579 highlight_cb,
2580 pop_up_p,
2581 menu_bar_p,
2582 menu_bar_p,
2585 name);
2587 /* Set the cursor to an arrow for popup menus when they are mapped.
2588 This is done by default for menu bar menus. */
2589 if (pop_up_p)
2591 /* Must realize so the GdkWindow inside the widget is created. */
2592 gtk_widget_realize (w);
2593 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2596 else
2598 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2599 type);
2602 return w;
2605 /* Return the label for menu item WITEM. */
2607 static const char *
2608 xg_get_menu_item_label (GtkMenuItem *witem)
2610 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2611 return gtk_label_get_label (wlabel);
2614 /* Return non-zero if the menu item WITEM has the text LABEL. */
2616 static int
2617 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2619 int is_same = 0;
2620 char *utf8_label = get_utf8_string (label);
2621 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2623 if (! old_label && ! utf8_label)
2624 is_same = 1;
2625 else if (old_label && utf8_label)
2626 is_same = strcmp (utf8_label, old_label) == 0;
2628 if (utf8_label) g_free (utf8_label);
2630 return is_same;
2633 /* Destroy widgets in LIST. */
2635 static void
2636 xg_destroy_widgets (GList *list)
2638 GList *iter;
2640 for (iter = list; iter; iter = g_list_next (iter))
2642 GtkWidget *w = GTK_WIDGET (iter->data);
2644 /* Destroying the widget will remove it from the container it is in. */
2645 gtk_widget_destroy (w);
2649 /* Update the top level names in MENUBAR (i.e. not submenus).
2650 F is the frame the menu bar belongs to.
2651 *LIST is a list with the current menu bar names (menu item widgets).
2652 ITER is the item within *LIST that shall be updated.
2653 POS is the numerical position, starting at 0, of ITER in *LIST.
2654 VAL describes what the menu bar shall look like after the update.
2655 SELECT_CB is the callback to use when a menu item is selected.
2656 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2657 CL_DATA points to the callback data to be used for this menu bar.
2659 This function calls itself to walk through the menu bar names. */
2661 static void
2662 xg_update_menubar (GtkWidget *menubar,
2663 FRAME_PTR f,
2664 GList **list,
2665 GList *iter,
2666 int pos,
2667 widget_value *val,
2668 GCallback select_cb,
2669 GCallback deactivate_cb,
2670 GCallback highlight_cb,
2671 xg_menu_cb_data *cl_data)
2673 if (! iter && ! val)
2674 return;
2675 else if (iter && ! val)
2677 /* Item(s) have been removed. Remove all remaining items. */
2678 xg_destroy_widgets (iter);
2680 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2681 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2682 gtk_menu_item_new_with_label (""),
2684 /* All updated. */
2685 val = 0;
2686 iter = 0;
2688 else if (! iter && val)
2690 /* Item(s) added. Add all new items in one call. */
2691 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2692 0, 1, 0, menubar, cl_data, 0);
2694 /* All updated. */
2695 val = 0;
2696 iter = 0;
2698 /* Below this neither iter or val is NULL */
2699 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2701 /* This item is still the same, check next item. */
2702 val = val->next;
2703 iter = g_list_next (iter);
2704 ++pos;
2706 else /* This item is changed. */
2708 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2709 GtkMenuItem *witem2 = 0;
2710 int val_in_menubar = 0;
2711 int iter_in_new_menubar = 0;
2712 GList *iter2;
2713 widget_value *cur;
2715 /* See if the changed entry (val) is present later in the menu bar */
2716 for (iter2 = iter;
2717 iter2 && ! val_in_menubar;
2718 iter2 = g_list_next (iter2))
2720 witem2 = GTK_MENU_ITEM (iter2->data);
2721 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2724 /* See if the current entry (iter) is present later in the
2725 specification for the new menu bar. */
2726 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2727 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2729 if (val_in_menubar && ! iter_in_new_menubar)
2731 int nr = pos;
2733 /* This corresponds to:
2734 Current: A B C
2735 New: A C
2736 Remove B. */
2738 g_object_ref (G_OBJECT (witem));
2739 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2740 gtk_widget_destroy (GTK_WIDGET (witem));
2742 /* Must get new list since the old changed. */
2743 g_list_free (*list);
2744 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2745 while (nr-- > 0) iter = g_list_next (iter);
2747 else if (! val_in_menubar && ! iter_in_new_menubar)
2749 /* This corresponds to:
2750 Current: A B C
2751 New: A X C
2752 Rename B to X. This might seem to be a strange thing to do,
2753 since if there is a menu under B it will be totally wrong for X.
2754 But consider editing a C file. Then there is a C-mode menu
2755 (corresponds to B above).
2756 If then doing C-x C-f the minibuf menu (X above) replaces the
2757 C-mode menu. When returning from the minibuffer, we get
2758 back the C-mode menu. Thus we do:
2759 Rename B to X (C-mode to minibuf menu)
2760 Rename X to B (minibuf to C-mode menu).
2761 If the X menu hasn't been invoked, the menu under B
2762 is up to date when leaving the minibuffer. */
2763 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2764 char *utf8_label = get_utf8_string (val->name);
2765 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2767 gtk_label_set_text (wlabel, utf8_label);
2769 /* If this item has a submenu that has been detached, change
2770 the title in the WM decorations also. */
2771 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2772 /* Set the title of the detached window. */
2773 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2775 if (utf8_label) g_free (utf8_label);
2776 iter = g_list_next (iter);
2777 val = val->next;
2778 ++pos;
2780 else if (! val_in_menubar && iter_in_new_menubar)
2782 /* This corresponds to:
2783 Current: A B C
2784 New: A X B C
2785 Insert X. */
2787 int nr = pos;
2788 GSList *group = 0;
2789 GtkWidget *w = xg_create_one_menuitem (val,
2791 select_cb,
2792 highlight_cb,
2793 cl_data,
2794 &group);
2796 /* Create a possibly empty submenu for menu bar items, since some
2797 themes don't highlight items correctly without it. */
2798 GtkWidget *submenu = create_menus (NULL, f,
2799 select_cb, deactivate_cb,
2800 highlight_cb,
2801 0, 0, 0, 0, cl_data, 0);
2802 gtk_widget_set_name (w, MENU_ITEM_NAME);
2803 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2804 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2806 g_list_free (*list);
2807 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2808 while (nr-- > 0) iter = g_list_next (iter);
2809 iter = g_list_next (iter);
2810 val = val->next;
2811 ++pos;
2813 else /* if (val_in_menubar && iter_in_new_menubar) */
2815 int nr = pos;
2816 /* This corresponds to:
2817 Current: A B C
2818 New: A C B
2819 Move C before B */
2821 g_object_ref (G_OBJECT (witem2));
2822 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2823 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2824 GTK_WIDGET (witem2), pos);
2825 g_object_unref (G_OBJECT (witem2));
2827 g_list_free (*list);
2828 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2829 while (nr-- > 0) iter = g_list_next (iter);
2830 if (iter) iter = g_list_next (iter);
2831 val = val->next;
2832 ++pos;
2836 /* Update the rest of the menu bar. */
2837 xg_update_menubar (menubar, f, list, iter, pos, val,
2838 select_cb, deactivate_cb, highlight_cb, cl_data);
2841 /* Update the menu item W so it corresponds to VAL.
2842 SELECT_CB is the callback to use when a menu item is selected.
2843 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2844 CL_DATA is the data to set in the widget for menu invocation. */
2846 static void
2847 xg_update_menu_item (widget_value *val,
2848 GtkWidget *w,
2849 GCallback select_cb,
2850 GCallback highlight_cb,
2851 xg_menu_cb_data *cl_data)
2853 GtkWidget *wchild;
2854 GtkLabel *wlbl = 0;
2855 GtkLabel *wkey = 0;
2856 char *utf8_label;
2857 char *utf8_key;
2858 const char *old_label = 0;
2859 const char *old_key = 0;
2860 xg_menu_item_cb_data *cb_data;
2862 wchild = XG_BIN_CHILD (w);
2863 utf8_label = get_utf8_string (val->name);
2864 utf8_key = get_utf8_string (val->key);
2866 /* See if W is a menu item with a key. See make_menu_item above. */
2867 if (GTK_IS_BOX (wchild))
2869 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2871 wlbl = GTK_LABEL (list->data);
2872 wkey = GTK_LABEL (list->next->data);
2873 g_list_free (list);
2875 if (! utf8_key)
2877 /* Remove the key and keep just the label. */
2878 g_object_ref (G_OBJECT (wlbl));
2879 gtk_container_remove (GTK_CONTAINER (w), wchild);
2880 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2881 g_object_unref (G_OBJECT (wlbl));
2882 wkey = 0;
2886 else /* Just a label. */
2888 wlbl = GTK_LABEL (wchild);
2890 /* Check if there is now a key. */
2891 if (utf8_key)
2893 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2894 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2896 wlbl = GTK_LABEL (list->data);
2897 wkey = GTK_LABEL (list->next->data);
2898 g_list_free (list);
2900 gtk_container_remove (GTK_CONTAINER (w), wchild);
2901 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2906 if (wkey) old_key = gtk_label_get_label (wkey);
2907 if (wlbl) old_label = gtk_label_get_label (wlbl);
2909 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2910 gtk_label_set_text (wkey, utf8_key);
2912 if (! old_label || strcmp (utf8_label, old_label) != 0)
2913 gtk_label_set_text (wlbl, utf8_label);
2915 if (utf8_key) g_free (utf8_key);
2916 if (utf8_label) g_free (utf8_label);
2918 if (! val->enabled && gtk_widget_get_sensitive (w))
2919 gtk_widget_set_sensitive (w, FALSE);
2920 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2921 gtk_widget_set_sensitive (w, TRUE);
2923 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2924 XG_ITEM_DATA);
2925 if (cb_data)
2927 cb_data->call_data = val->call_data;
2928 cb_data->help = val->help;
2929 cb_data->cl_data = cl_data;
2931 /* We assume the callback functions don't change. */
2932 if (val->call_data && ! val->contents)
2934 /* This item shall have a select callback. */
2935 if (! cb_data->select_id)
2936 cb_data->select_id
2937 = g_signal_connect (G_OBJECT (w), "activate",
2938 select_cb, cb_data);
2940 else if (cb_data->select_id)
2942 g_signal_handler_disconnect (w, cb_data->select_id);
2943 cb_data->select_id = 0;
2948 /* Update the toggle menu item W so it corresponds to VAL. */
2950 static void
2951 xg_update_toggle_item (widget_value *val, GtkWidget *w)
2953 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2956 /* Update the radio menu item W so it corresponds to VAL. */
2958 static void
2959 xg_update_radio_item (widget_value *val, GtkWidget *w)
2961 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2964 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2965 SUBMENU may be NULL, in that case a new menu is created.
2966 F is the frame the menu bar belongs to.
2967 VAL describes the contents of the menu bar.
2968 SELECT_CB is the callback to use when a menu item is selected.
2969 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2970 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2971 CL_DATA is the call back data to use for any newly created items.
2973 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2974 was NULL. */
2976 static GtkWidget *
2977 xg_update_submenu (GtkWidget *submenu,
2978 FRAME_PTR f,
2979 widget_value *val,
2980 GCallback select_cb,
2981 GCallback deactivate_cb,
2982 GCallback highlight_cb,
2983 xg_menu_cb_data *cl_data)
2985 GtkWidget *newsub = submenu;
2986 GList *list = 0;
2987 GList *iter;
2988 widget_value *cur;
2989 int has_tearoff_p = 0;
2990 GList *first_radio = 0;
2992 if (submenu)
2993 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2995 for (cur = val, iter = list;
2996 cur && iter;
2997 iter = g_list_next (iter), cur = cur->next)
2999 GtkWidget *w = GTK_WIDGET (iter->data);
3001 /* Skip tearoff items, they have no counterpart in val. */
3002 if (GTK_IS_TEAROFF_MENU_ITEM (w))
3004 has_tearoff_p = 1;
3005 iter = g_list_next (iter);
3006 if (iter) w = GTK_WIDGET (iter->data);
3007 else break;
3010 /* Remember first radio button in a group. If we get a mismatch in
3011 a radio group we must rebuild the whole group so that the connections
3012 in GTK becomes correct. */
3013 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3014 first_radio = iter;
3015 else if (cur->button_type != BUTTON_TYPE_RADIO
3016 && ! GTK_IS_RADIO_MENU_ITEM (w))
3017 first_radio = 0;
3019 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3021 if (! menu_separator_name_p (cur->name))
3022 break;
3024 else if (GTK_IS_CHECK_MENU_ITEM (w))
3026 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3027 break;
3028 xg_update_toggle_item (cur, w);
3029 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3031 else if (GTK_IS_RADIO_MENU_ITEM (w))
3033 if (cur->button_type != BUTTON_TYPE_RADIO)
3034 break;
3035 xg_update_radio_item (cur, w);
3036 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3038 else if (GTK_IS_MENU_ITEM (w))
3040 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3041 GtkWidget *sub;
3043 if (cur->button_type != BUTTON_TYPE_NONE ||
3044 menu_separator_name_p (cur->name))
3045 break;
3047 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3049 sub = gtk_menu_item_get_submenu (witem);
3050 if (sub && ! cur->contents)
3052 /* Not a submenu anymore. */
3053 g_object_ref (G_OBJECT (sub));
3054 remove_submenu (witem);
3055 gtk_widget_destroy (sub);
3057 else if (cur->contents)
3059 GtkWidget *nsub;
3061 nsub = xg_update_submenu (sub, f, cur->contents,
3062 select_cb, deactivate_cb,
3063 highlight_cb, cl_data);
3065 /* If this item just became a submenu, we must set it. */
3066 if (nsub != sub)
3067 gtk_menu_item_set_submenu (witem, nsub);
3070 else
3072 /* Structural difference. Remove everything from here and down
3073 in SUBMENU. */
3074 break;
3078 /* Remove widgets from first structural change. */
3079 if (iter)
3081 /* If we are adding new menu items below, we must remove from
3082 first radio button so that radio groups become correct. */
3083 if (cur && first_radio) xg_destroy_widgets (first_radio);
3084 else xg_destroy_widgets (iter);
3087 if (cur)
3089 /* More items added. Create them. */
3090 newsub = create_menus (cur,
3092 select_cb,
3093 deactivate_cb,
3094 highlight_cb,
3097 ! has_tearoff_p,
3098 submenu,
3099 cl_data,
3103 if (list) g_list_free (list);
3105 return newsub;
3108 /* Update the MENUBAR.
3109 F is the frame the menu bar belongs to.
3110 VAL describes the contents of the menu bar.
3111 If DEEP_P is non-zero, rebuild all but the top level menu names in
3112 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3113 SELECT_CB is the callback to use when a menu item is selected.
3114 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3115 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3117 void
3118 xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
3119 int deep_p,
3120 GCallback select_cb, GCallback deactivate_cb,
3121 GCallback highlight_cb)
3123 xg_menu_cb_data *cl_data;
3124 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3126 if (! list) return;
3128 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
3129 XG_FRAME_DATA);
3131 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3132 select_cb, deactivate_cb, highlight_cb, cl_data);
3134 if (deep_p)
3136 widget_value *cur;
3138 /* Update all sub menus.
3139 We must keep the submenus (GTK menu item widgets) since the
3140 X Window in the XEvent that activates the menu are those widgets. */
3142 /* Update cl_data, menu_item things in F may have changed. */
3143 update_cl_data (cl_data, f, highlight_cb);
3145 for (cur = val->contents; cur; cur = cur->next)
3147 GList *iter;
3148 GtkWidget *sub = 0;
3149 GtkWidget *newsub;
3150 GtkMenuItem *witem = 0;
3152 /* Find sub menu that corresponds to val and update it. */
3153 for (iter = list ; iter; iter = g_list_next (iter))
3155 witem = GTK_MENU_ITEM (iter->data);
3156 if (xg_item_label_same_p (witem, cur->name))
3158 sub = gtk_menu_item_get_submenu (witem);
3159 break;
3163 newsub = xg_update_submenu (sub,
3165 cur->contents,
3166 select_cb,
3167 deactivate_cb,
3168 highlight_cb,
3169 cl_data);
3170 /* sub may still be NULL. If we just updated non deep and added
3171 a new menu bar item, it has no sub menu yet. So we set the
3172 newly created sub menu under witem. */
3173 if (newsub != sub && witem != 0)
3175 xg_set_screen (newsub, f);
3176 gtk_menu_item_set_submenu (witem, newsub);
3181 g_list_free (list);
3182 gtk_widget_show_all (menubar);
3185 /* Callback called when the menu bar W is mapped.
3186 Used to find the height of the menu bar if we didn't get it
3187 after showing the widget. */
3189 static void
3190 menubar_map_cb (GtkWidget *w, gpointer user_data)
3192 GtkRequisition req;
3193 FRAME_PTR f = (FRAME_PTR) user_data;
3194 gtk_widget_get_preferred_size (w, NULL, &req);
3195 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3197 FRAME_MENUBAR_HEIGHT (f) = req.height;
3198 xg_height_or_width_changed (f);
3202 /* Recompute all the widgets of frame F, when the menu bar has been
3203 changed. Value is non-zero if widgets were updated. */
3206 xg_update_frame_menubar (FRAME_PTR f)
3208 struct x_output *x = f->output_data.x;
3209 GtkRequisition req;
3211 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3212 return 0;
3214 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3215 return 0; /* Already done this, happens for frames created invisible. */
3217 BLOCK_INPUT;
3219 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3220 FALSE, FALSE, 0);
3221 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3223 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3224 gtk_widget_show_all (x->menubar_widget);
3225 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3227 /* If menu bar doesn't know its height yet, cheat a little so the frame
3228 doesn't jump so much when resized later in menubar_map_cb. */
3229 if (req.height == 0)
3230 req.height = 23;
3232 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3234 FRAME_MENUBAR_HEIGHT (f) = req.height;
3235 xg_height_or_width_changed (f);
3237 UNBLOCK_INPUT;
3239 return 1;
3242 /* Get rid of the menu bar of frame F, and free its storage.
3243 This is used when deleting a frame, and when turning off the menu bar. */
3245 void
3246 free_frame_menubar (FRAME_PTR f)
3248 struct x_output *x = f->output_data.x;
3250 if (x->menubar_widget)
3252 BLOCK_INPUT;
3254 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3255 /* The menubar and its children shall be deleted when removed from
3256 the container. */
3257 x->menubar_widget = 0;
3258 FRAME_MENUBAR_HEIGHT (f) = 0;
3259 xg_height_or_width_changed (f);
3260 UNBLOCK_INPUT;
3265 xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
3267 struct x_output *x = f->output_data.x;
3268 GList *iter;
3269 GdkRectangle rec;
3270 GList *list;
3271 GdkDisplay *gdpy;
3272 GdkWindow *gw;
3273 GdkEvent gevent;
3274 GtkWidget *gwdesc;
3276 if (! x->menubar_widget) return 0;
3278 if (! (event->xbutton.x >= 0
3279 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3280 && event->xbutton.y >= 0
3281 && event->xbutton.y < f->output_data.x->menubar_height
3282 && event->xbutton.same_screen))
3283 return 0;
3285 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3286 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3287 if (! gw) return 0;
3288 gevent.any.window = gw;
3289 gevent.any.type = GDK_NOTHING;
3290 gwdesc = gtk_get_event_widget (&gevent);
3291 if (! gwdesc) return 0;
3292 if (! GTK_IS_MENU_BAR (gwdesc)
3293 && ! GTK_IS_MENU_ITEM (gwdesc)
3294 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3295 return 0;
3297 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3298 if (! list) return 0;
3299 rec.x = event->xbutton.x;
3300 rec.y = event->xbutton.y;
3301 rec.width = 1;
3302 rec.height = 1;
3304 for (iter = list ; iter; iter = g_list_next (iter))
3306 GtkWidget *w = GTK_WIDGET (iter->data);
3307 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3308 break;
3310 g_list_free (list);
3311 return iter == 0 ? 0 : 1;
3316 /***********************************************************************
3317 Scroll bar functions
3318 ***********************************************************************/
3321 /* Setting scroll bar values invokes the callback. Use this variable
3322 to indicate that callback should do nothing. */
3324 int xg_ignore_gtk_scrollbar;
3326 /* The width of the scroll bar for the current theme. */
3328 static int scroll_bar_width_for_theme;
3330 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3331 may be larger than 32 bits. Keep a mapping from integer index to widget
3332 pointers to get around the 32 bit limitation. */
3334 static struct
3336 GtkWidget **widgets;
3337 ptrdiff_t max_size;
3338 ptrdiff_t used;
3339 } id_to_widget;
3341 /* Grow this much every time we need to allocate more */
3343 #define ID_TO_WIDGET_INCR 32
3345 /* Store the widget pointer W in id_to_widget and return the integer index. */
3347 static ptrdiff_t
3348 xg_store_widget_in_map (GtkWidget *w)
3350 ptrdiff_t i;
3352 if (id_to_widget.max_size == id_to_widget.used)
3354 ptrdiff_t new_size;
3355 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3356 memory_full (SIZE_MAX);
3358 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3359 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3360 new_size, sizeof (GtkWidget *));
3362 for (i = id_to_widget.max_size; i < new_size; ++i)
3363 id_to_widget.widgets[i] = 0;
3364 id_to_widget.max_size = new_size;
3367 /* Just loop over the array and find a free place. After all,
3368 how many scroll bars are we creating? Should be a small number.
3369 The check above guarantees we will find a free place. */
3370 for (i = 0; i < id_to_widget.max_size; ++i)
3372 if (! id_to_widget.widgets[i])
3374 id_to_widget.widgets[i] = w;
3375 ++id_to_widget.used;
3377 return i;
3381 /* Should never end up here */
3382 abort ();
3385 /* Remove pointer at IDX from id_to_widget.
3386 Called when scroll bar is destroyed. */
3388 static void
3389 xg_remove_widget_from_map (ptrdiff_t idx)
3391 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3393 id_to_widget.widgets[idx] = 0;
3394 --id_to_widget.used;
3398 /* Get the widget pointer at IDX from id_to_widget. */
3400 static GtkWidget *
3401 xg_get_widget_from_map (ptrdiff_t idx)
3403 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3404 return id_to_widget.widgets[idx];
3406 return 0;
3409 static void
3410 update_theme_scrollbar_width (void)
3412 #ifdef HAVE_GTK3
3413 GtkAdjustment *vadj;
3414 #else
3415 GtkObject *vadj;
3416 #endif
3417 GtkWidget *wscroll;
3418 int w = 0, b = 0;
3420 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3421 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3422 g_object_ref_sink (G_OBJECT (wscroll));
3423 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3424 gtk_widget_destroy (wscroll);
3425 g_object_unref (G_OBJECT (wscroll));
3426 w += 2*b;
3427 if (w < 16) w = 16;
3428 scroll_bar_width_for_theme = w;
3432 xg_get_default_scrollbar_width (void)
3434 return scroll_bar_width_for_theme;
3437 /* Return the scrollbar id for X Window WID on display DPY.
3438 Return -1 if WID not in id_to_widget. */
3440 ptrdiff_t
3441 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3443 ptrdiff_t idx;
3444 GtkWidget *w;
3446 w = xg_win_to_widget (dpy, wid);
3448 if (w)
3450 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3451 if (id_to_widget.widgets[idx] == w)
3452 return idx;
3455 return -1;
3458 /* Callback invoked when scroll bar WIDGET is destroyed.
3459 DATA is the index into id_to_widget for WIDGET.
3460 We free pointer to last scroll bar values here and remove the index. */
3462 static void
3463 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3465 intptr_t id = (intptr_t) data;
3466 xg_remove_widget_from_map (id);
3469 /* Create a scroll bar widget for frame F. Store the scroll bar
3470 in BAR.
3471 SCROLL_CALLBACK is the callback to invoke when the value of the
3472 bar changes.
3473 END_CALLBACK is the callback to invoke when scrolling ends.
3474 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3475 to set resources for the widget. */
3477 void
3478 xg_create_scroll_bar (FRAME_PTR f,
3479 struct scroll_bar *bar,
3480 GCallback scroll_callback,
3481 GCallback end_callback,
3482 const char *scroll_bar_name)
3484 GtkWidget *wscroll;
3485 GtkWidget *webox;
3486 intptr_t scroll_id;
3487 #ifdef HAVE_GTK3
3488 GtkAdjustment *vadj;
3489 #else
3490 GtkObject *vadj;
3491 #endif
3493 /* Page, step increment values are not so important here, they
3494 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3495 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3496 0.1, 0.1, 0.1);
3498 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3499 webox = gtk_event_box_new ();
3500 gtk_widget_set_name (wscroll, scroll_bar_name);
3501 #ifndef HAVE_GTK3
3502 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3503 #endif
3504 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3506 scroll_id = xg_store_widget_in_map (wscroll);
3508 g_signal_connect (G_OBJECT (wscroll),
3509 "destroy",
3510 G_CALLBACK (xg_gtk_scroll_destroy),
3511 (gpointer) scroll_id);
3512 g_signal_connect (G_OBJECT (wscroll),
3513 "change-value",
3514 scroll_callback,
3515 (gpointer) bar);
3516 g_signal_connect (G_OBJECT (wscroll),
3517 "button-release-event",
3518 end_callback,
3519 (gpointer) bar);
3521 /* The scroll bar widget does not draw on a window of its own. Instead
3522 it draws on the parent window, in this case the edit widget. So
3523 whenever the edit widget is cleared, the scroll bar needs to redraw
3524 also, which causes flicker. Put an event box between the edit widget
3525 and the scroll bar, so the scroll bar instead draws itself on the
3526 event box window. */
3527 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3528 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3531 /* Set the cursor to an arrow. */
3532 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3534 bar->x_window = scroll_id;
3537 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3539 void
3540 xg_remove_scroll_bar (FRAME_PTR f, ptrdiff_t scrollbar_id)
3542 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3543 if (w)
3545 GtkWidget *wparent = gtk_widget_get_parent (w);
3546 gtk_widget_destroy (w);
3547 gtk_widget_destroy (wparent);
3548 SET_FRAME_GARBAGED (f);
3552 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3553 in frame F.
3554 TOP/LEFT are the new pixel positions where the bar shall appear.
3555 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3557 void
3558 xg_update_scrollbar_pos (FRAME_PTR f,
3559 ptrdiff_t scrollbar_id,
3560 int top,
3561 int left,
3562 int width,
3563 int height)
3566 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3568 if (wscroll)
3570 GtkWidget *wfixed = f->output_data.x->edit_widget;
3571 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3572 gint msl;
3574 /* Clear out old position. */
3575 int oldx = -1, oldy = -1, oldw, oldh;
3576 if (gtk_widget_get_parent (wparent) == wfixed)
3578 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3579 "x", &oldx, "y", &oldy, NULL);
3580 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3583 /* Move and resize to new values. */
3584 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3585 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3586 if (msl > height)
3588 /* No room. Hide scroll bar as some themes output a warning if
3589 the height is less than the min size. */
3590 gtk_widget_hide (wparent);
3591 gtk_widget_hide (wscroll);
3593 else
3595 gtk_widget_show_all (wparent);
3596 gtk_widget_set_size_request (wscroll, width, height);
3598 gtk_widget_queue_draw (wfixed);
3599 gdk_window_process_all_updates ();
3600 if (oldx != -1 && oldw > 0 && oldh > 0)
3602 /* Clear under old scroll bar position. This must be done after
3603 the gtk_widget_queue_draw and gdk_window_process_all_updates
3604 above. */
3605 x_clear_area (FRAME_X_DISPLAY (f),
3606 FRAME_X_WINDOW (f),
3607 oldx, oldy, oldw, oldh, 0);
3610 /* GTK does not redraw until the main loop is entered again, but
3611 if there are no X events pending we will not enter it. So we sync
3612 here to get some events. */
3614 x_sync (f);
3615 SET_FRAME_GARBAGED (f);
3616 cancel_mouse_face (f);
3620 /* Get the current value of the range, truncated to an integer. */
3622 static int
3623 int_gtk_range_get_value (GtkRange *range)
3625 return gtk_range_get_value (range);
3629 /* Set the thumb size and position of scroll bar BAR. We are currently
3630 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3632 void
3633 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3634 int portion,
3635 int position,
3636 int whole)
3638 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3640 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3642 if (wscroll && NILP (bar->dragging))
3644 GtkAdjustment *adj;
3645 gdouble shown;
3646 gdouble top;
3647 int size, value;
3648 int old_size;
3649 int new_step;
3650 int changed = 0;
3652 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3654 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3655 rather than the real portion value. This makes the thumb less likely
3656 to resize and that looks better. */
3657 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3658 /* When the thumb is at the bottom, position == whole.
3659 So we need to increase `whole' to make space for the thumb. */
3660 whole += portion;
3662 if (whole <= 0)
3663 top = 0, shown = 1;
3664 else
3666 top = (gdouble) position / whole;
3667 shown = (gdouble) portion / whole;
3670 size = shown * XG_SB_RANGE;
3671 size = min (size, XG_SB_RANGE);
3672 size = max (size, 1);
3674 value = top * XG_SB_RANGE;
3675 value = min (value, XG_SB_MAX - size);
3676 value = max (value, XG_SB_MIN);
3678 /* Assume all lines are of equal size. */
3679 new_step = size / max (1, FRAME_LINES (f));
3681 old_size = gtk_adjustment_get_page_size (adj);
3682 if (old_size != size)
3684 int old_step = gtk_adjustment_get_step_increment (adj);
3685 if (old_step != new_step)
3687 gtk_adjustment_set_page_size (adj, size);
3688 gtk_adjustment_set_step_increment (adj, new_step);
3689 /* Assume a page increment is about 95% of the page size */
3690 gtk_adjustment_set_page_increment (adj, size - size / 20);
3691 changed = 1;
3695 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3697 BLOCK_INPUT;
3699 /* gtk_range_set_value invokes the callback. Set
3700 ignore_gtk_scrollbar to make the callback do nothing */
3701 xg_ignore_gtk_scrollbar = 1;
3703 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3704 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3705 else if (changed)
3706 gtk_adjustment_changed (adj);
3708 xg_ignore_gtk_scrollbar = 0;
3710 UNBLOCK_INPUT;
3715 /* Return non-zero if EVENT is for a scroll bar in frame F.
3716 When the same X window is used for several Gtk+ widgets, we cannot
3717 say for sure based on the X window alone if an event is for the
3718 frame. This function does additional checks.
3720 Return non-zero if the event is for a scroll bar, zero otherwise. */
3723 xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
3725 int retval = 0;
3727 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3729 /* Check if press occurred outside the edit widget. */
3730 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3731 GdkWindow *gwin;
3732 #ifdef HAVE_GTK3
3733 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3734 (gdk_display_get_device_manager (gdpy));
3735 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
3736 #else
3737 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3738 #endif
3739 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
3741 else if (f
3742 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3743 || event->type == MotionNotify))
3745 /* If we are releasing or moving the scroll bar, it has the grab. */
3746 GtkWidget *w = gtk_grab_get_current ();
3747 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3750 return retval;
3755 /***********************************************************************
3756 Tool bar functions
3757 ***********************************************************************/
3758 /* The key for the data we put in the GtkImage widgets. The data is
3759 the image used by Emacs. We use this to see if we need to update
3760 the GtkImage with a new image. */
3761 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3763 /* The key for storing the latest modifiers so the activate callback can
3764 get them. */
3765 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3767 /* The key for storing the button widget in its proxy menu item. */
3768 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3770 /* The key for the data we put in the GtkImage widgets. The data is
3771 the stock name used by Emacs. We use this to see if we need to update
3772 the GtkImage with a new image. */
3773 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3775 /* As above, but this is used for named theme widgets, as opposed to
3776 stock items. */
3777 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3779 /* Callback function invoked when a tool bar item is pressed.
3780 W is the button widget in the tool bar that got pressed,
3781 CLIENT_DATA is an integer that is the index of the button in the
3782 tool bar. 0 is the first button. */
3784 static gboolean
3785 xg_tool_bar_button_cb (GtkWidget *widget,
3786 GdkEventButton *event,
3787 gpointer user_data)
3789 intptr_t state = event->state;
3790 gpointer ptr = (gpointer) state;
3791 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3792 return FALSE;
3796 /* Callback function invoked when a tool bar item is pressed.
3797 W is the button widget in the tool bar that got pressed,
3798 CLIENT_DATA is an integer that is the index of the button in the
3799 tool bar. 0 is the first button. */
3801 static void
3802 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3804 intptr_t idx = (intptr_t) client_data;
3805 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
3806 intptr_t mod = (intptr_t) gmod;
3808 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3809 Lisp_Object key, frame;
3810 struct input_event event;
3811 EVENT_INIT (event);
3813 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3814 return;
3816 idx *= TOOL_BAR_ITEM_NSLOTS;
3818 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3819 XSETFRAME (frame, f);
3821 /* We generate two events here. The first one is to set the prefix
3822 to `(tool_bar)', see keyboard.c. */
3823 event.kind = TOOL_BAR_EVENT;
3824 event.frame_or_window = frame;
3825 event.arg = frame;
3826 kbd_buffer_store_event (&event);
3828 event.kind = TOOL_BAR_EVENT;
3829 event.frame_or_window = frame;
3830 event.arg = key;
3831 /* Convert between the modifier bits GDK uses and the modifier bits
3832 Emacs uses. This assumes GDK and X masks are the same, which they are when
3833 this is written. */
3834 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3835 kbd_buffer_store_event (&event);
3837 /* Return focus to the frame after we have clicked on a detached
3838 tool bar button. */
3839 Fx_focus_frame (frame);
3842 /* Callback function invoked when a tool bar item is pressed in a detached
3843 tool bar or the overflow drop down menu.
3844 We just call xg_tool_bar_callback.
3845 W is the menu item widget that got pressed,
3846 CLIENT_DATA is an integer that is the index of the button in the
3847 tool bar. 0 is the first button. */
3849 static void
3850 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3852 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3853 XG_TOOL_BAR_PROXY_BUTTON));
3854 xg_tool_bar_callback (wbutton, client_data);
3858 static gboolean
3859 xg_tool_bar_help_callback (GtkWidget *w,
3860 GdkEventCrossing *event,
3861 gpointer client_data);
3863 /* This callback is called when a help is to be shown for an item in
3864 the detached tool bar when the detached tool bar it is not expanded. */
3866 static gboolean
3867 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3868 GdkEventCrossing *event,
3869 gpointer client_data)
3871 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3872 XG_TOOL_BAR_PROXY_BUTTON));
3874 return xg_tool_bar_help_callback (wbutton, event, client_data);
3877 static GtkWidget *
3878 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3880 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3881 GtkWidget *c1 = (GtkWidget *) clist->data;
3882 GtkWidget *c2 = clist->next ? (GtkWidget *) clist->next->data : NULL;
3884 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3885 g_list_free (clist);
3886 return GTK_IS_LABEL (c1) ? c1 : c2;
3890 /* This callback is called when a tool item should create a proxy item,
3891 such as for the overflow menu. Also called when the tool bar is detached.
3892 If we don't create a proxy menu item, the detached tool bar will be
3893 blank. */
3895 static gboolean
3896 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3898 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3899 GtkWidget *vb = XG_BIN_CHILD (wbutton);
3900 GtkWidget *c1;
3901 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3902 GtkImage *wimage = GTK_IMAGE (c1);
3903 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3904 (wlbl ? gtk_label_get_text (wlbl) : "");
3905 GtkWidget *wmenuimage;
3908 if (gtk_button_get_use_stock (wbutton))
3909 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3910 GTK_ICON_SIZE_MENU);
3911 else
3913 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3914 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3916 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
3918 if (store_type == GTK_IMAGE_STOCK)
3920 gchar *stock_id;
3921 gtk_image_get_stock (wimage, &stock_id, NULL);
3922 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3924 else if (store_type == GTK_IMAGE_ICON_SET)
3926 GtkIconSet *icon_set;
3927 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3928 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3929 GTK_ICON_SIZE_MENU);
3931 else if (store_type == GTK_IMAGE_PIXBUF)
3933 gint width, height;
3935 if (settings &&
3936 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3937 &width, &height))
3939 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3941 src_pixbuf = gtk_image_get_pixbuf (wimage);
3942 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3943 GDK_INTERP_BILINEAR);
3945 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3947 else
3949 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3950 abort ();
3953 else if (store_type == GTK_IMAGE_ICON_NAME)
3955 const gchar *icon_name;
3956 GtkIconSize icon_size;
3958 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3959 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3960 GTK_ICON_SIZE_MENU);
3962 else
3964 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3965 abort ();
3968 if (wmenuimage)
3969 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3971 g_signal_connect (G_OBJECT (wmenuitem),
3972 "activate",
3973 G_CALLBACK (xg_tool_bar_proxy_callback),
3974 user_data);
3977 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3978 (gpointer) wbutton);
3979 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3980 gtk_widget_set_sensitive (wmenuitem,
3981 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
3983 /* Use enter/leave notify to show help. We use the events
3984 rather than the GtkButton specific signals "enter" and
3985 "leave", so we can have only one callback. The event
3986 will tell us what kind of event it is. */
3987 g_signal_connect (G_OBJECT (wmenuitem),
3988 "enter-notify-event",
3989 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3990 user_data);
3991 g_signal_connect (G_OBJECT (wmenuitem),
3992 "leave-notify-event",
3993 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3994 user_data);
3996 return TRUE;
3999 /* This callback is called when a tool bar is detached. We must set
4000 the height of the tool bar to zero when this happens so frame sizes
4001 are correctly calculated.
4002 WBOX is the handle box widget that enables detach/attach of the tool bar.
4003 W is the tool bar widget.
4004 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4006 static void
4007 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
4008 GtkWidget *w,
4009 gpointer client_data)
4011 FRAME_PTR f = (FRAME_PTR) client_data;
4013 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
4014 NULL);
4016 if (f)
4018 GtkRequisition req, req2;
4019 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
4020 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4021 gtk_widget_get_preferred_size (w, NULL, &req2);
4022 req.width -= req2.width;
4023 req.height -= req2.height;
4024 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4025 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4026 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4027 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4028 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4029 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4030 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4031 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4032 xg_height_or_width_changed (f);
4036 /* This callback is called when a tool bar is reattached. We must set
4037 the height of the tool bar when this happens so frame sizes
4038 are correctly calculated.
4039 WBOX is the handle box widget that enables detach/attach of the tool bar.
4040 W is the tool bar widget.
4041 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4043 static void
4044 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
4045 GtkWidget *w,
4046 gpointer client_data)
4048 FRAME_PTR f = (FRAME_PTR) client_data;
4049 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
4051 if (f)
4053 GtkRequisition req, req2;
4054 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
4055 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4056 gtk_widget_get_preferred_size (w, NULL, &req2);
4057 req.width += req2.width;
4058 req.height += req2.height;
4059 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4060 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4061 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4062 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4063 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4064 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4065 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4066 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4067 xg_height_or_width_changed (f);
4071 /* This callback is called when the mouse enters or leaves a tool bar item.
4072 It is used for displaying and hiding the help text.
4073 W is the tool bar item, a button.
4074 EVENT is either an enter event or leave event.
4075 CLIENT_DATA is an integer that is the index of the button in the
4076 tool bar. 0 is the first button.
4078 Returns FALSE to tell GTK to keep processing this event. */
4080 static gboolean
4081 xg_tool_bar_help_callback (GtkWidget *w,
4082 GdkEventCrossing *event,
4083 gpointer client_data)
4085 intptr_t idx = (intptr_t) client_data;
4086 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4087 Lisp_Object help, frame;
4089 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4090 return FALSE;
4092 if (event->type == GDK_ENTER_NOTIFY)
4094 idx *= TOOL_BAR_ITEM_NSLOTS;
4095 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4097 if (NILP (help))
4098 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4100 else
4101 help = Qnil;
4103 XSETFRAME (frame, f);
4104 kbd_buffer_store_help_event (frame, help);
4106 return FALSE;
4110 /* This callback is called when a tool bar item shall be redrawn.
4111 It modifies the expose event so that the GtkImage widget redraws the
4112 whole image. This to overcome a bug that makes GtkImage draw the image
4113 in the wrong place when it tries to redraw just a part of the image.
4114 W is the GtkImage to be redrawn.
4115 EVENT is the expose event for W.
4116 CLIENT_DATA is unused.
4118 Returns FALSE to tell GTK to keep processing this event. */
4120 #ifndef HAVE_GTK3
4121 static gboolean
4122 xg_tool_bar_item_expose_callback (GtkWidget *w,
4123 GdkEventExpose *event,
4124 gpointer client_data)
4126 gint width, height;
4128 gdk_drawable_get_size (event->window, &width, &height);
4129 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4130 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4132 event->area.x = max (0, event->area.x);
4133 event->area.y = max (0, event->area.y);
4135 event->area.width = max (width, event->area.width);
4136 event->area.height = max (height, event->area.height);
4138 return FALSE;
4140 #endif
4142 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4143 #define toolbar_set_orientation(w, o) \
4144 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4145 #else
4146 #define toolbar_set_orientation(w, o) \
4147 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4148 #endif
4150 /* Attach a tool bar to frame F. */
4152 static void
4153 xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
4155 struct x_output *x = f->output_data.x;
4156 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4158 toolbar_set_orientation (x->toolbar_widget,
4159 into_hbox
4160 ? GTK_ORIENTATION_VERTICAL
4161 : GTK_ORIENTATION_HORIZONTAL);
4162 if (!x->handlebox_widget)
4164 x->handlebox_widget = gtk_handle_box_new ();
4165 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4166 G_CALLBACK (xg_tool_bar_detach_callback), f);
4167 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4168 G_CALLBACK (xg_tool_bar_attach_callback), f);
4169 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4170 x->toolbar_widget);
4173 if (into_hbox)
4175 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4176 GTK_POS_TOP);
4177 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
4178 FALSE, FALSE, 0);
4180 if (EQ (pos, Qleft))
4181 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4182 x->handlebox_widget,
4184 x->toolbar_in_hbox = 1;
4186 else
4188 int vbox_pos = x->menubar_widget ? 1 : 0;
4189 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4190 GTK_POS_LEFT);
4191 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
4192 FALSE, FALSE, 0);
4194 if (EQ (pos, Qtop))
4195 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4196 x->handlebox_widget,
4197 vbox_pos);
4198 x->toolbar_in_hbox = 0;
4202 /* Create a tool bar for frame F. */
4204 static void
4205 xg_create_tool_bar (FRAME_PTR f)
4207 struct x_output *x = f->output_data.x;
4208 #if GTK_CHECK_VERSION (3, 3, 6)
4209 GtkStyleContext *gsty;
4210 #endif
4212 x->toolbar_widget = gtk_toolbar_new ();
4213 x->toolbar_detached = 0;
4215 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4217 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4218 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4219 #if GTK_CHECK_VERSION (3, 3, 6)
4220 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4221 gtk_style_context_add_class (gsty, "primary-toolbar");
4222 #endif
4226 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4228 /* Find the right-to-left image named by RTL in the tool bar images for F.
4229 Returns IMAGE if RTL is not found. */
4231 static Lisp_Object
4232 find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
4234 int i;
4235 Lisp_Object file, rtl_name;
4236 struct gcpro gcpro1, gcpro2;
4237 GCPRO2 (file, rtl_name);
4239 rtl_name = Ffile_name_nondirectory (rtl);
4241 for (i = 0; i < f->n_tool_bar_items; ++i)
4243 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4244 if (!NILP (file = file_for_image (rtl_image)))
4246 file = call1 (intern ("file-name-sans-extension"),
4247 Ffile_name_nondirectory (file));
4248 if (EQ (Fequal (file, rtl_name), Qt))
4250 image = rtl_image;
4251 break;
4256 return image;
4259 static GtkToolItem *
4260 xg_make_tool_item (FRAME_PTR f,
4261 GtkWidget *wimage,
4262 GtkWidget **wbutton,
4263 const char *label,
4264 int i, int horiz, int text_image)
4266 GtkToolItem *ti = gtk_tool_item_new ();
4267 GtkWidget *vb = gtk_box_new (horiz
4268 ? GTK_ORIENTATION_HORIZONTAL
4269 : GTK_ORIENTATION_VERTICAL,
4271 GtkWidget *wb = gtk_button_new ();
4272 /* The eventbox is here so we can have tooltips on disabled items. */
4273 GtkWidget *weventbox = gtk_event_box_new ();
4274 #if GTK_CHECK_VERSION (3, 3, 6)
4275 GtkCssProvider *css_prov = gtk_css_provider_new ();
4276 GtkStyleContext *gsty;
4278 gtk_css_provider_load_from_data (css_prov,
4279 "GtkEventBox {"
4280 " background-color: transparent;"
4281 "}",
4282 -1, NULL);
4284 gsty = gtk_widget_get_style_context (weventbox);
4285 gtk_style_context_add_provider (gsty,
4286 GTK_STYLE_PROVIDER (css_prov),
4287 GTK_STYLE_PROVIDER_PRIORITY_USER);
4288 g_object_unref (css_prov);
4289 #endif
4291 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4293 if (wimage && !text_image)
4294 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4295 if (label)
4296 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4297 if (wimage && text_image)
4298 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4300 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4301 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4302 gtk_container_add (GTK_CONTAINER (wb), vb);
4303 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4304 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4306 if (wimage || label)
4308 intptr_t ii = i;
4309 gpointer gi = (gpointer) ii;
4311 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4312 G_CALLBACK (xg_tool_bar_menu_proxy),
4313 gi);
4315 g_signal_connect (G_OBJECT (wb), "clicked",
4316 G_CALLBACK (xg_tool_bar_callback),
4317 gi);
4319 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4321 #ifndef HAVE_GTK3
4322 /* Catch expose events to overcome an annoying redraw bug, see
4323 comment for xg_tool_bar_item_expose_callback. */
4324 g_signal_connect (G_OBJECT (ti),
4325 "expose-event",
4326 G_CALLBACK (xg_tool_bar_item_expose_callback),
4328 #endif
4329 gtk_tool_item_set_homogeneous (ti, FALSE);
4331 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4332 no distinction based on modifiers in the activate callback,
4333 so we have to do it ourselves. */
4334 g_signal_connect (wb, "button-release-event",
4335 G_CALLBACK (xg_tool_bar_button_cb),
4336 NULL);
4338 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4340 /* Use enter/leave notify to show help. We use the events
4341 rather than the GtkButton specific signals "enter" and
4342 "leave", so we can have only one callback. The event
4343 will tell us what kind of event it is. */
4344 g_signal_connect (G_OBJECT (weventbox),
4345 "enter-notify-event",
4346 G_CALLBACK (xg_tool_bar_help_callback),
4347 gi);
4348 g_signal_connect (G_OBJECT (weventbox),
4349 "leave-notify-event",
4350 G_CALLBACK (xg_tool_bar_help_callback),
4351 gi);
4354 if (wbutton) *wbutton = wb;
4356 return ti;
4359 static int
4360 is_box_type (GtkWidget *vb, int is_horizontal)
4362 #ifdef HAVE_GTK3
4363 int ret = 0;
4364 if (GTK_IS_BOX (vb))
4366 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4367 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4368 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4370 return ret;
4371 #else
4372 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4373 #endif
4377 static int
4378 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4379 const char *icon_name, const struct image *img,
4380 const char *label, int horiz)
4382 gpointer old;
4383 GtkWidget *wimage;
4384 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4385 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4387 /* Check if the tool icon matches. */
4388 if (stock_name && wimage)
4390 old = g_object_get_data (G_OBJECT (wimage),
4391 XG_TOOL_BAR_STOCK_NAME);
4392 if (!old || strcmp (old, stock_name))
4393 return 1;
4395 else if (icon_name && wimage)
4397 old = g_object_get_data (G_OBJECT (wimage),
4398 XG_TOOL_BAR_ICON_NAME);
4399 if (!old || strcmp (old, icon_name))
4400 return 1;
4402 else if (wimage)
4404 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4405 XG_TOOL_BAR_IMAGE_DATA);
4406 Pixmap old_img = (Pixmap) gold_img;
4407 if (old_img != img->pixmap)
4408 return 1;
4411 /* Check button configuration and label. */
4412 if (is_box_type (vb, horiz)
4413 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4414 return 1;
4416 /* Ensure label is correct. */
4417 if (label && wlbl)
4418 gtk_label_set_text (GTK_LABEL (wlbl), label);
4419 return 0;
4422 static int
4423 xg_update_tool_bar_sizes (FRAME_PTR f)
4425 struct x_output *x = f->output_data.x;
4426 GtkRequisition req;
4427 int nl = 0, nr = 0, nt = 0, nb = 0;
4429 gtk_widget_get_preferred_size (GTK_WIDGET (x->handlebox_widget), NULL, &req);
4430 if (x->toolbar_in_hbox)
4432 int pos;
4433 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4434 x->handlebox_widget,
4435 "position", &pos, NULL);
4436 if (pos == 0) nl = req.width;
4437 else nr = req.width;
4439 else
4441 int pos;
4442 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4443 x->handlebox_widget,
4444 "position", &pos, NULL);
4445 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4446 else nb = req.height;
4449 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4450 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4451 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4452 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4454 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4455 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4456 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4457 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4458 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4459 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4460 return 1;
4463 return 0;
4467 /* Update the tool bar for frame F. Add new buttons and remove old. */
4469 void
4470 update_frame_tool_bar (FRAME_PTR f)
4472 int i, j;
4473 struct x_output *x = f->output_data.x;
4474 int hmargin = 0, vmargin = 0;
4475 GtkToolbar *wtoolbar;
4476 GtkToolItem *ti;
4477 GtkTextDirection dir;
4478 int pack_tool_bar = x->handlebox_widget == NULL;
4479 Lisp_Object style;
4480 int text_image, horiz;
4482 if (! FRAME_GTK_WIDGET (f))
4483 return;
4485 BLOCK_INPUT;
4487 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4489 hmargin = XFASTINT (Vtool_bar_button_margin);
4490 vmargin = XFASTINT (Vtool_bar_button_margin);
4492 else if (CONSP (Vtool_bar_button_margin))
4494 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4495 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4497 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4498 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4501 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4502 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4503 i.e. zero. This means that margins less than
4504 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4505 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4506 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4508 if (! x->toolbar_widget)
4509 xg_create_tool_bar (f);
4511 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4512 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4514 style = Ftool_bar_get_system_style ();
4515 text_image = EQ (style, Qtext_image_horiz);
4516 horiz = EQ (style, Qboth_horiz) || text_image;
4518 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4520 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4521 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4522 int idx;
4523 ptrdiff_t img_id;
4524 int icon_size = 0;
4525 struct image *img = NULL;
4526 Lisp_Object image;
4527 Lisp_Object stock = Qnil;
4528 GtkStockItem stock_item;
4529 char *stock_name = NULL;
4530 char *icon_name = NULL;
4531 Lisp_Object rtl;
4532 GtkWidget *wbutton = NULL;
4533 Lisp_Object specified_file;
4534 int vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4535 const char *label
4536 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4537 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4538 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4539 : "";
4541 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4543 /* If this is a separator, use a gtk separator item. */
4544 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4546 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4548 if (ti)
4549 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4550 GTK_WIDGET (ti));
4551 ti = gtk_separator_tool_item_new ();
4552 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4554 j++;
4555 continue;
4558 /* Otherwise, the tool-bar item is an ordinary button. */
4560 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4562 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4563 ti = NULL;
4566 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4568 /* Ignore invalid image specifications. */
4569 image = PROP (TOOL_BAR_ITEM_IMAGES);
4570 if (!valid_image_p (image))
4572 if (ti)
4573 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4574 GTK_WIDGET (ti));
4575 continue;
4578 specified_file = file_for_image (image);
4579 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4580 stock = call1 (Qx_gtk_map_stock, specified_file);
4582 if (STRINGP (stock))
4584 stock_name = SSDATA (stock);
4585 if (stock_name[0] == 'n' && stock_name[1] == ':')
4587 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4588 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4590 icon_name = stock_name + 2;
4591 stock_name = NULL;
4592 stock = Qnil;
4594 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4595 icon_name = NULL;
4596 else
4597 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4599 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4600 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4601 else
4603 stock = Qnil;
4604 stock_name = NULL;
4608 if (stock_name == NULL && icon_name == NULL)
4610 /* No stock image, or stock item not known. Try regular
4611 image. If image is a vector, choose it according to the
4612 button state. */
4613 if (dir == GTK_TEXT_DIR_RTL
4614 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4615 && STRINGP (rtl))
4616 image = find_rtl_image (f, image, rtl);
4618 if (VECTORP (image))
4620 if (enabled_p)
4621 idx = (selected_p
4622 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4623 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4624 else
4625 idx = (selected_p
4626 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4627 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4629 eassert (ASIZE (image) >= idx);
4630 image = AREF (image, idx);
4632 else
4633 idx = -1;
4635 img_id = lookup_image (f, image);
4636 img = IMAGE_FROM_ID (f, img_id);
4637 prepare_image_for_display (f, img);
4639 if (img->load_failed_p || img->pixmap == None)
4641 if (ti)
4642 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4643 GTK_WIDGET (ti));
4644 continue;
4648 /* If there is an existing widget, check if it's stale; if so,
4649 remove it and make a new tool item from scratch. */
4650 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4651 img, label, horiz))
4653 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4654 GTK_WIDGET (ti));
4655 ti = NULL;
4658 if (ti == NULL)
4660 GtkWidget *w;
4662 /* Save the image so we can see if an update is needed the
4663 next time we call xg_tool_item_match_p. */
4664 if (EQ (style, Qtext))
4665 w = NULL;
4666 else if (stock_name)
4668 w = gtk_image_new_from_stock (stock_name, icon_size);
4669 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4670 (gpointer) xstrdup (stock_name),
4671 (GDestroyNotify) xfree);
4673 else if (icon_name)
4675 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4676 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4677 (gpointer) xstrdup (icon_name),
4678 (GDestroyNotify) xfree);
4680 else
4682 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4683 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4684 (gpointer)img->pixmap);
4687 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4688 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4689 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4692 #undef PROP
4694 gtk_widget_set_sensitive (wbutton, enabled_p);
4695 j++;
4698 /* Remove buttons not longer needed. */
4701 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4702 if (ti)
4703 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4704 } while (ti != NULL);
4706 if (f->n_tool_bar_items != 0)
4708 if (pack_tool_bar)
4709 xg_pack_tool_bar (f, f->tool_bar_position);
4710 gtk_widget_show_all (GTK_WIDGET (x->handlebox_widget));
4711 if (xg_update_tool_bar_sizes (f))
4712 xg_height_or_width_changed (f);
4715 UNBLOCK_INPUT;
4718 /* Deallocate all resources for the tool bar on frame F.
4719 Remove the tool bar. */
4721 void
4722 free_frame_tool_bar (FRAME_PTR f)
4724 struct x_output *x = f->output_data.x;
4726 if (x->toolbar_widget)
4728 int is_packed = x->handlebox_widget != 0;
4729 BLOCK_INPUT;
4730 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4731 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4732 if (is_packed)
4734 if (x->toolbar_in_hbox)
4735 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4736 x->handlebox_widget);
4737 else
4738 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4739 x->handlebox_widget);
4741 else
4742 gtk_widget_destroy (x->toolbar_widget);
4744 x->toolbar_widget = 0;
4745 x->handlebox_widget = 0;
4746 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4747 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4749 xg_height_or_width_changed (f);
4751 UNBLOCK_INPUT;
4756 xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4758 struct x_output *x = f->output_data.x;
4760 if (! x->toolbar_widget || ! x->handlebox_widget)
4761 return 1;
4763 BLOCK_INPUT;
4764 g_object_ref (x->handlebox_widget);
4765 if (x->toolbar_in_hbox)
4766 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4767 x->handlebox_widget);
4768 else
4769 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4770 x->handlebox_widget);
4771 xg_pack_tool_bar (f, pos);
4772 g_object_unref (x->handlebox_widget);
4773 if (xg_update_tool_bar_sizes (f))
4774 xg_height_or_width_changed (f);
4776 UNBLOCK_INPUT;
4777 return 1;
4782 /***********************************************************************
4783 Initializing
4784 ***********************************************************************/
4785 void
4786 xg_initialize (void)
4788 GtkBindingSet *binding_set;
4789 GtkSettings *settings;
4791 #if HAVE_XFT
4792 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4793 we keep it permanently linked in. */
4794 XftInit (0);
4795 #endif
4797 gdpy_def = NULL;
4798 xg_ignore_gtk_scrollbar = 0;
4799 xg_detached_menus = 0;
4800 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4801 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4803 id_to_widget.max_size = id_to_widget.used = 0;
4804 id_to_widget.widgets = 0;
4806 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4807 (gdk_display_get_default ()));
4808 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4809 bindings. It doesn't seem to be any way to remove properties,
4810 so we set it to VoidSymbol which in X means "no key". */
4811 gtk_settings_set_string_property (settings,
4812 "gtk-menu-bar-accel",
4813 "VoidSymbol",
4814 EMACS_CLASS);
4816 /* Make GTK text input widgets use Emacs style keybindings. This is
4817 Emacs after all. */
4818 gtk_settings_set_string_property (settings,
4819 "gtk-key-theme-name",
4820 "Emacs",
4821 EMACS_CLASS);
4823 /* Make dialogs close on C-g. Since file dialog inherits from
4824 dialog, this works for them also. */
4825 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4826 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4827 "close", 0);
4829 /* Make menus close on C-g. */
4830 binding_set = gtk_binding_set_by_class (g_type_class_ref
4831 (GTK_TYPE_MENU_SHELL));
4832 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4833 "cancel", 0);
4834 update_theme_scrollbar_width ();
4837 #endif /* USE_GTK */