Merge from trunk.
[emacs.git] / src / gtkutil.c
blob6c00058e7afcde34a949393d6860a40faf561212
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2011 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 <signal.h>
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "blockinput.h"
29 #include "syssignal.h"
30 #include "window.h"
31 #include "gtkutil.h"
32 #include "termhooks.h"
33 #include "keyboard.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include <gdk/gdkkeysyms.h>
37 #include "xsettings.h"
39 #ifdef HAVE_XFT
40 #include <X11/Xft/Xft.h>
41 #endif
43 #ifdef HAVE_GTK3
44 #include <gtk/gtkx.h>
45 #include "emacsgtkfixed.h"
46 #endif
48 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
49 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
51 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
52 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
54 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
55 #define gtk_widget_set_has_window(w, b) \
56 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
57 #endif
58 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
59 #define gtk_dialog_get_action_area(w) ((w)->action_area)
60 #define gtk_dialog_get_content_area(w) ((w)->vbox)
61 #endif
62 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
63 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
64 #endif
65 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
66 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
67 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
68 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
69 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
70 #endif
71 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
72 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
73 #else
74 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
75 #endif
77 #ifndef HAVE_GTK3
78 #ifdef USE_GTK_TOOLTIP
79 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
80 #endif
81 #define gdk_window_get_geometry(w, a, b, c, d) \
82 gdk_window_get_geometry (w, a, b, c, d, 0)
83 #define gdk_x11_window_lookup_for_display(d, w) \
84 gdk_xid_table_lookup_for_display (d, w)
85 #ifndef GDK_KEY_g
86 #define GDK_KEY_g GDK_g
87 #endif
88 #endif
90 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
92 static void update_theme_scrollbar_width (void);
95 /***********************************************************************
96 Display handling functions
97 ***********************************************************************/
99 /* Keep track of the default display, or NULL if there is none. Emacs
100 may close all its displays. */
102 static GdkDisplay *gdpy_def;
104 /* When the GTK widget W is to be created on a display for F that
105 is not the default display, set the display for W.
106 W can be a GtkMenu or a GtkWindow widget. */
108 static void
109 xg_set_screen (GtkWidget *w, FRAME_PTR f)
111 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
113 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
114 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
116 if (GTK_IS_MENU (w))
117 gtk_menu_set_screen (GTK_MENU (w), gscreen);
118 else
119 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
124 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
125 *DPY is set to NULL if the display can't be opened.
127 Returns non-zero if display could be opened, zero if display could not
128 be opened, and less than zero if the GTK version doesn't support
129 multipe displays. */
131 void
132 xg_display_open (char *display_name, Display **dpy)
134 GdkDisplay *gdpy;
136 gdpy = gdk_display_open (display_name);
137 if (!gdpy_def && gdpy)
139 gdpy_def = gdpy;
140 gdk_display_manager_set_default_display (gdk_display_manager_get (),
141 gdpy);
144 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
148 /* Close display DPY. */
150 void
151 xg_display_close (Display *dpy)
153 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
155 /* If this is the default display, try to change it before closing.
156 If there is no other display to use, gdpy_def is set to NULL, and
157 the next call to xg_display_open resets the default display. */
158 if (gdk_display_get_default () == gdpy)
160 struct x_display_info *dpyinfo;
161 GdkDisplay *gdpy_new = NULL;
163 /* Find another display. */
164 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
165 if (dpyinfo->display != dpy)
167 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
168 gdk_display_manager_set_default_display (gdk_display_manager_get (),
169 gdpy_new);
170 break;
172 gdpy_def = gdpy_new;
175 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
176 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
177 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
178 can continue running, but there will be memory leaks. */
179 g_object_run_dispose (G_OBJECT (gdpy));
180 #else
181 /* This seems to be fixed in GTK 2.10. */
182 gdk_display_close (gdpy);
183 #endif
187 /***********************************************************************
188 Utility functions
189 ***********************************************************************/
190 /* The next two variables and functions are taken from lwlib. */
191 static widget_value *widget_value_free_list;
192 static int malloc_cpt;
194 /* Allocate a widget_value structure, either by taking one from the
195 widget_value_free_list or by malloc:ing a new one.
197 Return a pointer to the allocated structure. */
199 widget_value *
200 malloc_widget_value (void)
202 widget_value *wv;
203 if (widget_value_free_list)
205 wv = widget_value_free_list;
206 widget_value_free_list = wv->free_list;
207 wv->free_list = 0;
209 else
211 wv = (widget_value *) xmalloc (sizeof (widget_value));
212 malloc_cpt++;
214 memset (wv, 0, sizeof (widget_value));
215 return wv;
218 /* This is analogous to free. It frees only what was allocated
219 by malloc_widget_value, and no substructures. */
221 void
222 free_widget_value (widget_value *wv)
224 if (wv->free_list)
225 abort ();
227 if (malloc_cpt > 25)
229 /* When the number of already allocated cells is too big,
230 We free it. */
231 xfree (wv);
232 malloc_cpt--;
234 else
236 wv->free_list = widget_value_free_list;
237 widget_value_free_list = wv;
242 /* Create and return the cursor to be used for popup menus and
243 scroll bars on display DPY. */
245 GdkCursor *
246 xg_create_default_cursor (Display *dpy)
248 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
249 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
252 static GdkPixbuf *
253 xg_get_pixbuf_from_pixmap (FRAME_PTR f, Pixmap pix)
255 int iunused;
256 GdkPixbuf *tmp_buf;
257 Window wunused;
258 unsigned int width, height, uunused;
259 XImage *xim;
261 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
262 &width, &height, &uunused, &uunused);
264 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
265 ~0, XYPixmap);
266 if (!xim) return 0;
268 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
269 GDK_COLORSPACE_RGB,
270 FALSE,
271 xim->bitmap_unit,
272 (int) width,
273 (int) height,
274 xim->bytes_per_line,
275 NULL,
276 NULL);
277 XDestroyImage (xim);
278 return tmp_buf;
281 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
283 static GdkPixbuf *
284 xg_get_pixbuf_from_pix_and_mask (FRAME_PTR f,
285 Pixmap pix,
286 Pixmap mask)
288 int width, height;
289 GdkPixbuf *icon_buf, *tmp_buf;
291 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
292 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
293 g_object_unref (G_OBJECT (tmp_buf));
295 width = gdk_pixbuf_get_width (icon_buf);
296 height = gdk_pixbuf_get_height (icon_buf);
298 if (mask)
300 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
301 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
302 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
303 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
304 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
305 int y;
307 for (y = 0; y < height; ++y)
309 guchar *iconptr, *maskptr;
310 int x;
312 iconptr = pixels + y * rowstride;
313 maskptr = mask_pixels + y * mask_rowstride;
315 for (x = 0; x < width; ++x)
317 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
318 just R is sufficient. */
319 if (maskptr[0] == 0)
320 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
322 iconptr += rowstride/width;
323 maskptr += mask_rowstride/width;
327 g_object_unref (G_OBJECT (mask_buf));
330 return icon_buf;
333 static Lisp_Object
334 file_for_image (Lisp_Object image)
336 Lisp_Object specified_file = Qnil;
337 Lisp_Object tail;
339 for (tail = XCDR (image);
340 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
341 tail = XCDR (XCDR (tail)))
342 if (EQ (XCAR (tail), QCfile))
343 specified_file = XCAR (XCDR (tail));
345 return specified_file;
348 /* For the image defined in IMG, make and return a GtkImage. For displays with
349 8 planes or less we must make a GdkPixbuf and apply the mask manually.
350 Otherwise the highlightning and dimming the tool bar code in GTK does
351 will look bad. For display with more than 8 planes we just use the
352 pixmap and mask directly. For monochrome displays, GTK doesn't seem
353 able to use external pixmaps, it looks bad whatever we do.
354 The image is defined on the display where frame F is.
355 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
356 If OLD_WIDGET is NULL, a new widget is constructed and returned.
357 If OLD_WIDGET is not NULL, that widget is modified. */
359 static GtkWidget *
360 xg_get_image_for_pixmap (FRAME_PTR f,
361 struct image *img,
362 GtkWidget *widget,
363 GtkImage *old_widget)
365 GdkPixbuf *icon_buf;
367 /* If we have a file, let GTK do all the image handling.
368 This seems to be the only way to make insensitive and activated icons
369 look good in all cases. */
370 Lisp_Object specified_file = file_for_image (img->spec);
371 Lisp_Object file;
373 /* We already loaded the image once before calling this
374 function, so this only fails if the image file has been removed.
375 In that case, use the pixmap already loaded. */
377 if (STRINGP (specified_file)
378 && STRINGP (file = x_find_image_file (specified_file)))
380 if (! old_widget)
381 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
382 else
383 gtk_image_set_from_file (old_widget, SSDATA (file));
385 return GTK_WIDGET (old_widget);
388 /* No file, do the image handling ourselves. This will look very bad
389 on a monochrome display, and sometimes bad on all displays with
390 certain themes. */
392 /* This is a workaround to make icons look good on pseudo color
393 displays. Apparently GTK expects the images to have an alpha
394 channel. If they don't, insensitive and activated icons will
395 look bad. This workaround does not work on monochrome displays,
396 and is strictly not needed on true color/static color displays (i.e.
397 16 bits and higher). But we do it anyway so we get a pixbuf that is
398 not associated with the img->pixmap. The img->pixmap may be removed
399 by clearing the image cache and then the tool bar redraw fails, since
400 Gtk+ assumes the pixmap is always there. */
401 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
403 if (icon_buf)
405 if (! old_widget)
406 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
407 else
408 gtk_image_set_from_pixbuf (old_widget, icon_buf);
410 g_object_unref (G_OBJECT (icon_buf));
413 return GTK_WIDGET (old_widget);
417 /* Set CURSOR on W and all widgets W contain. We must do like this
418 for scroll bars and menu because they create widgets internally,
419 and it is those widgets that are visible. */
421 static void
422 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
424 GdkWindow *window = gtk_widget_get_window(w);
425 GList *children = gdk_window_peek_children (window);
427 gdk_window_set_cursor (window, cursor);
429 /* The scroll bar widget has more than one GDK window (had to look at
430 the source to figure this out), and there is no way to set cursor
431 on widgets in GTK. So we must set the cursor for all GDK windows.
432 Ditto for menus. */
434 for ( ; children; children = g_list_next (children))
435 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
438 /* Insert NODE into linked LIST. */
440 static void
441 xg_list_insert (xg_list_node *list, xg_list_node *node)
443 xg_list_node *list_start = list->next;
445 if (list_start) list_start->prev = node;
446 node->next = list_start;
447 node->prev = 0;
448 list->next = node;
451 /* Remove NODE from linked LIST. */
453 static void
454 xg_list_remove (xg_list_node *list, xg_list_node *node)
456 xg_list_node *list_start = list->next;
457 if (node == list_start)
459 list->next = node->next;
460 if (list->next) list->next->prev = 0;
462 else
464 node->prev->next = node->next;
465 if (node->next) node->next->prev = node->prev;
469 /* Allocate and return a utf8 version of STR. If STR is already
470 utf8 or NULL, just return a copy of STR.
471 A new string is allocated and the caller must free the result
472 with g_free. */
474 static char *
475 get_utf8_string (const char *str)
477 char *utf8_str;
479 if (!str) return NULL;
481 /* If not UTF-8, try current locale. */
482 if (!g_utf8_validate (str, -1, NULL))
483 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
484 else
485 return g_strdup (str);
487 if (!utf8_str)
489 /* Probably some control characters in str. Escape them. */
490 size_t nr_bad = 0;
491 gsize bytes_read;
492 gsize bytes_written;
493 unsigned char *p = (unsigned char *)str;
494 char *cp, *up;
495 GError *err = NULL;
497 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
498 &bytes_written, &err))
499 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
501 ++nr_bad;
502 p += bytes_written+1;
503 g_error_free (err);
504 err = NULL;
507 if (err)
509 g_error_free (err);
510 err = NULL;
512 if (cp) g_free (cp);
514 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
515 p = (unsigned char *)str;
517 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
518 &bytes_written, &err))
519 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
521 strncpy (up, (char *)p, bytes_written);
522 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
523 up[bytes_written+4] = '\0';
524 up += bytes_written+4;
525 p += bytes_written+1;
526 g_error_free (err);
527 err = NULL;
530 if (cp)
532 strcat (utf8_str, cp);
533 g_free (cp);
535 if (err)
537 g_error_free (err);
538 err = NULL;
541 return utf8_str;
544 /* Check for special colors used in face spec for region face.
545 The colors are fetched from the Gtk+ theme.
546 Return 1 if color was found, 0 if not. */
549 xg_check_special_colors (struct frame *f,
550 const char *color_name,
551 XColor *color)
553 int success_p = 0;
554 int get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
555 int get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
557 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
558 return success_p;
560 BLOCK_INPUT;
562 #ifdef HAVE_GTK3
563 GtkStyleContext *gsty
564 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
565 GdkRGBA col;
566 char buf[64];
567 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
568 if (get_fg)
569 gtk_style_context_get_color (gsty, state, &col);
570 else
571 gtk_style_context_get_background_color (gsty, state, &col);
573 sprintf (buf, "rgbi:%lf/%lf/%lf", col.red, col.green, col.blue);
574 success_p = XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
575 buf, color);
576 #else
577 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
578 GdkColor *grgb = get_bg
579 ? &gsty->bg[GTK_STATE_SELECTED]
580 : &gsty->fg[GTK_STATE_SELECTED];
582 color->red = grgb->red;
583 color->green = grgb->green;
584 color->blue = grgb->blue;
585 color->pixel = grgb->pixel;
586 success_p = 1;
587 #endif
590 UNBLOCK_INPUT;
591 return success_p;
596 /***********************************************************************
597 Tooltips
598 ***********************************************************************/
599 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
600 We use that to pop down the tooltip. This happens if Gtk+ for some
601 reason wants to change or hide the tooltip. */
603 #ifdef USE_GTK_TOOLTIP
605 static void
606 hierarchy_ch_cb (GtkWidget *widget,
607 GtkWidget *previous_toplevel,
608 gpointer user_data)
610 FRAME_PTR f = (FRAME_PTR) user_data;
611 struct x_output *x = f->output_data.x;
612 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
614 if (! top || ! GTK_IS_WINDOW (top))
615 gtk_widget_hide (previous_toplevel);
618 /* Callback called when Gtk+ thinks a tooltip should be displayed.
619 We use it to get the tooltip window and the tooltip widget so
620 we can manipulate the ourselves.
622 Return FALSE ensures that the tooltip is not shown. */
624 static gboolean
625 qttip_cb (GtkWidget *widget,
626 gint xpos,
627 gint ypos,
628 gboolean keyboard_mode,
629 GtkTooltip *tooltip,
630 gpointer user_data)
632 FRAME_PTR f = (FRAME_PTR) user_data;
633 struct x_output *x = f->output_data.x;
634 if (x->ttip_widget == NULL)
636 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
637 x->ttip_widget = tooltip;
638 g_object_ref (G_OBJECT (tooltip));
639 x->ttip_lbl = gtk_label_new ("");
640 g_object_ref (G_OBJECT (x->ttip_lbl));
641 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
642 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
643 /* ATK needs an empty title for some reason. */
644 gtk_window_set_title (x->ttip_window, "");
645 /* Realize so we can safely get screen later on. */
646 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
647 gtk_widget_realize (x->ttip_lbl);
649 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
650 G_CALLBACK (hierarchy_ch_cb), f);
652 return FALSE;
655 #endif /* USE_GTK_TOOLTIP */
657 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
658 Return zero if no system tooltip available, non-zero otherwise. */
661 xg_prepare_tooltip (FRAME_PTR f,
662 Lisp_Object string,
663 int *width,
664 int *height)
666 #ifndef USE_GTK_TOOLTIP
667 return 0;
668 #else
669 struct x_output *x = f->output_data.x;
670 GtkWidget *widget;
671 GdkWindow *gwin;
672 GdkScreen *screen;
673 GtkSettings *settings;
674 gboolean tt_enabled = TRUE;
675 GtkRequisition req;
676 Lisp_Object encoded_string;
678 if (!x->ttip_lbl) return 0;
680 BLOCK_INPUT;
681 encoded_string = ENCODE_UTF_8 (string);
682 widget = GTK_WIDGET (x->ttip_lbl);
683 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
684 screen = gdk_window_get_screen (gwin);
685 settings = gtk_settings_get_for_screen (screen);
686 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
687 if (tt_enabled)
689 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
690 /* Record that we disabled it so it can be enabled again. */
691 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
692 (gpointer)f);
695 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
696 g_object_set_data (G_OBJECT
697 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
698 "gdk-display-current-tooltip", NULL);
700 /* Put out dummy widget in so we can get callbacks for unrealize and
701 hierarchy-changed. */
702 gtk_tooltip_set_custom (x->ttip_widget, widget);
704 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
705 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
706 if (width) *width = req.width;
707 if (height) *height = req.height;
709 UNBLOCK_INPUT;
711 return 1;
712 #endif /* USE_GTK_TOOLTIP */
715 /* Show the tooltip at ROOT_X and ROOT_Y.
716 xg_prepare_tooltip must have been called before this function. */
718 void
719 xg_show_tooltip (FRAME_PTR f, int root_x, int root_y)
721 #ifdef USE_GTK_TOOLTIP
722 struct x_output *x = f->output_data.x;
723 if (x->ttip_window)
725 BLOCK_INPUT;
726 gtk_window_move (x->ttip_window, root_x, root_y);
727 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
728 UNBLOCK_INPUT;
730 #endif
733 /* Hide tooltip if shown. Do nothing if not shown.
734 Return non-zero if tip was hidden, non-ero if not (i.e. not using
735 system tooltips). */
738 xg_hide_tooltip (FRAME_PTR f)
740 int ret = 0;
741 #ifdef USE_GTK_TOOLTIP
742 if (f->output_data.x->ttip_window)
744 GtkWindow *win = f->output_data.x->ttip_window;
745 BLOCK_INPUT;
746 gtk_widget_hide (GTK_WIDGET (win));
748 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
750 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
751 GdkScreen *screen = gdk_window_get_screen (gwin);
752 GtkSettings *settings = gtk_settings_get_for_screen (screen);
753 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
755 UNBLOCK_INPUT;
757 ret = 1;
759 #endif
760 return ret;
764 /***********************************************************************
765 General functions for creating widgets, resizing, events, e.t.c.
766 ***********************************************************************/
768 /* Make a geometry string and pass that to GTK. It seems this is the
769 only way to get geometry position right if the user explicitly
770 asked for a position when starting Emacs.
771 F is the frame we shall set geometry for. */
773 static void
774 xg_set_geometry (FRAME_PTR f)
776 if (f->size_hint_flags & (USPosition | PPosition))
778 int left = f->left_pos;
779 int xneg = f->size_hint_flags & XNegative;
780 int top = f->top_pos;
781 int yneg = f->size_hint_flags & YNegative;
782 char geom_str[32];
784 if (xneg)
785 left = -left;
786 if (yneg)
787 top = -top;
789 sprintf (geom_str, "=%dx%d%c%d%c%d",
790 FRAME_PIXEL_WIDTH (f),
791 FRAME_PIXEL_HEIGHT (f),
792 (xneg ? '-' : '+'), left,
793 (yneg ? '-' : '+'), top);
795 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
796 geom_str))
797 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
801 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
802 and use a GtkFixed widget, this doesn't happen automatically. */
804 static void
805 xg_clear_under_internal_border (FRAME_PTR f)
807 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
809 GtkWidget *wfixed = f->output_data.x->edit_widget;
810 gtk_widget_queue_draw (wfixed);
811 gdk_window_process_all_updates ();
812 x_clear_area (FRAME_X_DISPLAY (f),
813 FRAME_X_WINDOW (f),
814 0, 0,
815 FRAME_PIXEL_WIDTH (f),
816 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
817 x_clear_area (FRAME_X_DISPLAY (f),
818 FRAME_X_WINDOW (f),
819 0, 0,
820 FRAME_INTERNAL_BORDER_WIDTH (f),
821 FRAME_PIXEL_HEIGHT (f), 0);
822 x_clear_area (FRAME_X_DISPLAY (f),
823 FRAME_X_WINDOW (f),
824 0, FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
825 FRAME_PIXEL_WIDTH (f),
826 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
827 x_clear_area (FRAME_X_DISPLAY (f),
828 FRAME_X_WINDOW (f),
829 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
831 FRAME_INTERNAL_BORDER_WIDTH (f),
832 FRAME_PIXEL_HEIGHT (f), 0);
836 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
837 and a Gtk+ menu bar, we get resize events for the edit part of the
838 frame only. We let Gtk+ deal with the Gtk+ parts.
839 F is the frame to resize.
840 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
842 void
843 xg_frame_resized (FRAME_PTR f, int pixelwidth, int pixelheight)
845 int rows, columns;
847 if (pixelwidth == -1 && pixelheight == -1)
849 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
850 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
851 0, 0,
852 &pixelwidth, &pixelheight);
853 else return;
857 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
858 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
860 if (columns != FRAME_COLS (f)
861 || rows != FRAME_LINES (f)
862 || pixelwidth != FRAME_PIXEL_WIDTH (f)
863 || pixelheight != FRAME_PIXEL_HEIGHT (f))
865 FRAME_PIXEL_WIDTH (f) = pixelwidth;
866 FRAME_PIXEL_HEIGHT (f) = pixelheight;
868 xg_clear_under_internal_border (f);
869 change_frame_size (f, rows, columns, 0, 1, 0);
870 SET_FRAME_GARBAGED (f);
871 cancel_mouse_face (f);
875 /* Resize the outer window of frame F after chainging the height.
876 COLUMNS/ROWS is the size the edit area shall have after the resize. */
878 void
879 xg_frame_set_char_size (FRAME_PTR f, int cols, int rows)
881 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
882 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
883 int pixelwidth;
885 if (FRAME_PIXEL_HEIGHT (f) == 0)
886 return;
888 /* Take into account the size of the scroll bar. Always use the
889 number of columns occupied by the scroll bar here otherwise we
890 might end up with a frame width that is not a multiple of the
891 frame's character width which is bad for vertically split
892 windows. */
893 f->scroll_bar_actual_width
894 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
896 compute_fringe_widths (f, 0);
898 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
899 after calculating that value. */
900 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
901 + FRAME_TOOLBAR_WIDTH (f);
904 /* Do this before resize, as we don't know yet if we will be resized. */
905 xg_clear_under_internal_border (f);
907 /* Must resize our top level widget. Font size may have changed,
908 but not rows/cols. */
909 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
910 pixelwidth, pixelheight);
911 x_wm_set_size_hint (f, 0, 0);
913 SET_FRAME_GARBAGED (f);
914 cancel_mouse_face (f);
916 /* We can not call change_frame_size for a mapped frame,
917 we can not set pixel width/height either. The window manager may
918 override our resize request, XMonad does this all the time.
919 The best we can do is try to sync, so lisp code sees the updated
920 size as fast as possible.
921 For unmapped windows, we can set rows/cols. When
922 the frame is mapped again we will (hopefully) get the correct size. */
923 if (f->async_visible)
925 /* Must call this to flush out events */
926 (void)gtk_events_pending ();
927 gdk_flush ();
928 x_wait_for_event (f, ConfigureNotify);
930 else
932 change_frame_size (f, rows, cols, 0, 1, 0);
933 FRAME_PIXEL_WIDTH (f) = pixelwidth;
934 FRAME_PIXEL_HEIGHT (f) = pixelheight;
938 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
939 The policy is to keep the number of editable lines. */
941 static void
942 xg_height_or_width_changed (FRAME_PTR f)
944 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
945 FRAME_TOTAL_PIXEL_WIDTH (f),
946 FRAME_TOTAL_PIXEL_HEIGHT (f));
947 f->output_data.x->hint_flags = 0;
948 x_wm_set_size_hint (f, 0, 0);
951 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
952 Must be done like this, because GtkWidget:s can have "hidden"
953 X Window that aren't accessible.
955 Return 0 if no widget match WDESC. */
957 GtkWidget *
958 xg_win_to_widget (Display *dpy, Window wdesc)
960 gpointer gdkwin;
961 GtkWidget *gwdesc = 0;
963 BLOCK_INPUT;
965 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
966 wdesc);
967 if (gdkwin)
969 GdkEvent event;
970 event.any.window = gdkwin;
971 gwdesc = gtk_get_event_widget (&event);
974 UNBLOCK_INPUT;
975 return gwdesc;
978 /* Set the background of widget W to PIXEL. */
980 static void
981 xg_set_widget_bg (FRAME_PTR f, GtkWidget *w, long unsigned int pixel)
983 #ifdef HAVE_GTK3
984 GdkRGBA bg;
985 XColor xbg;
986 xbg.pixel = pixel;
987 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
989 bg.red = (double)xbg.red/65536.0;
990 bg.green = (double)xbg.green/65536.0;
991 bg.blue = (double)xbg.blue/65536.0;
992 bg.alpha = 1.0;
993 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
995 #else
996 GdkColor bg;
997 GdkColormap *map = gtk_widget_get_colormap (w);
998 gdk_colormap_query_color (map, pixel, &bg);
999 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1000 #endif
1003 /* Callback called when the gtk theme changes.
1004 We notify lisp code so it can fix faces used for region for example. */
1006 static void
1007 style_changed_cb (GObject *go,
1008 GParamSpec *spec,
1009 gpointer user_data)
1011 struct input_event event;
1012 GdkDisplay *gdpy = (GdkDisplay *) user_data;
1013 const char *display_name = gdk_display_get_name (gdpy);
1014 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1016 EVENT_INIT (event);
1017 event.kind = CONFIG_CHANGED_EVENT;
1018 event.frame_or_window = build_string (display_name);
1019 /* Theme doesn't change often, so intern is called seldom. */
1020 event.arg = intern ("theme-name");
1021 kbd_buffer_store_event (&event);
1023 update_theme_scrollbar_width ();
1025 /* If scroll bar width changed, we need set the new size on all frames
1026 on this display. */
1027 if (dpy)
1029 Lisp_Object rest, frame;
1030 FOR_EACH_FRAME (rest, frame)
1032 FRAME_PTR f = XFRAME (frame);
1033 if (FRAME_X_DISPLAY (f) == dpy)
1035 x_set_scroll_bar_default_width (f);
1036 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
1042 /* Called when a delete-event occurs on WIDGET. */
1044 static gboolean
1045 delete_cb (GtkWidget *widget,
1046 GdkEvent *event,
1047 gpointer user_data)
1049 #ifdef HAVE_GTK3
1050 /* The event doesn't arrive in the normal event loop. Send event
1051 here. */
1052 FRAME_PTR f = (FRAME_PTR) user_data;
1053 struct input_event ie;
1055 EVENT_INIT (ie);
1056 ie.kind = DELETE_WINDOW_EVENT;
1057 XSETFRAME (ie.frame_or_window, f);
1058 kbd_buffer_store_event (&ie);
1059 #endif
1061 return TRUE;
1064 /* Create and set up the GTK widgets for frame F.
1065 Return 0 if creation failed, non-zero otherwise. */
1068 xg_create_frame_widgets (FRAME_PTR f)
1070 GtkWidget *wtop;
1071 GtkWidget *wvbox, *whbox;
1072 GtkWidget *wfixed;
1073 GtkRcStyle *style;
1074 char *title = 0;
1076 BLOCK_INPUT;
1078 if (FRAME_X_EMBEDDED_P (f))
1079 wtop = gtk_plug_new (f->output_data.x->parent_desc);
1080 else
1081 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1083 xg_set_screen (wtop, f);
1085 wvbox = gtk_vbox_new (FALSE, 0);
1086 whbox = gtk_hbox_new (FALSE, 0);
1088 #ifdef HAVE_GTK3
1089 wfixed = emacs_fixed_new ();
1090 #else
1091 wfixed = gtk_fixed_new ();
1092 #endif
1094 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1096 if (wtop) gtk_widget_destroy (wtop);
1097 if (wvbox) gtk_widget_destroy (wvbox);
1098 if (whbox) gtk_widget_destroy (whbox);
1099 if (wfixed) gtk_widget_destroy (wfixed);
1101 UNBLOCK_INPUT;
1102 return 0;
1105 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1106 gtk_widget_set_name (wtop, EMACS_CLASS);
1107 gtk_widget_set_name (wvbox, "pane");
1108 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1110 /* If this frame has a title or name, set it in the title bar. */
1111 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
1112 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
1114 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1116 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1117 FRAME_GTK_WIDGET (f) = wfixed;
1118 f->output_data.x->vbox_widget = wvbox;
1119 f->output_data.x->hbox_widget = whbox;
1121 gtk_widget_set_has_window (wfixed, TRUE);
1123 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1124 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1125 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1127 if (FRAME_EXTERNAL_TOOL_BAR (f))
1128 update_frame_tool_bar (f);
1130 /* We don't want this widget double buffered, because we draw on it
1131 with regular X drawing primitives, so from a GTK/GDK point of
1132 view, the widget is totally blank. When an expose comes, this
1133 will make the widget blank, and then Emacs redraws it. This flickers
1134 a lot, so we turn off double buffering. */
1135 gtk_widget_set_double_buffered (wfixed, FALSE);
1137 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1138 SSDATA (Vx_resource_name),
1139 SSDATA (Vx_resource_class));
1141 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1142 GTK is to destroy the widget. We want Emacs to do that instead. */
1143 g_signal_connect (G_OBJECT (wtop), "delete-event",
1144 G_CALLBACK (delete_cb), f);
1146 /* Convert our geometry parameters into a geometry string
1147 and specify it.
1148 GTK will itself handle calculating the real position this way. */
1149 xg_set_geometry (f);
1150 f->win_gravity
1151 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1153 gtk_widget_add_events (wfixed,
1154 GDK_POINTER_MOTION_MASK
1155 | GDK_EXPOSURE_MASK
1156 | GDK_BUTTON_PRESS_MASK
1157 | GDK_BUTTON_RELEASE_MASK
1158 | GDK_KEY_PRESS_MASK
1159 | GDK_ENTER_NOTIFY_MASK
1160 | GDK_LEAVE_NOTIFY_MASK
1161 | GDK_FOCUS_CHANGE_MASK
1162 | GDK_STRUCTURE_MASK
1163 | GDK_VISIBILITY_NOTIFY_MASK);
1165 /* Must realize the windows so the X window gets created. It is used
1166 by callers of this function. */
1167 gtk_widget_realize (wfixed);
1168 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1170 /* Since GTK clears its window by filling with the background color,
1171 we must keep X and GTK background in sync. */
1172 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1174 #ifndef HAVE_GTK3
1175 /* Also, do not let any background pixmap to be set, this looks very
1176 bad as Emacs overwrites the background pixmap with its own idea
1177 of background color. */
1178 style = gtk_widget_get_modifier_style (wfixed);
1180 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1181 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1182 gtk_widget_modify_style (wfixed, style);
1183 #else
1184 gtk_widget_set_can_focus (wfixed, TRUE);
1185 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1186 #endif
1188 #ifdef USE_GTK_TOOLTIP
1189 /* Steal a tool tip window we can move ourselves. */
1190 f->output_data.x->ttip_widget = 0;
1191 f->output_data.x->ttip_lbl = 0;
1192 f->output_data.x->ttip_window = 0;
1193 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1194 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1195 #endif
1198 GdkScreen *screen = gtk_widget_get_screen (wtop);
1199 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1200 /* Only connect this signal once per screen. */
1201 if (! g_signal_handler_find (G_OBJECT (gs),
1202 G_SIGNAL_MATCH_FUNC,
1203 0, 0, 0,
1204 G_CALLBACK (style_changed_cb),
1207 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1208 G_CALLBACK (style_changed_cb),
1209 gdk_screen_get_display (screen));
1213 UNBLOCK_INPUT;
1215 return 1;
1218 void
1219 xg_free_frame_widgets (FRAME_PTR f)
1221 if (FRAME_GTK_OUTER_WIDGET (f))
1223 #ifdef USE_GTK_TOOLTIP
1224 struct x_output *x = f->output_data.x;
1225 #endif
1226 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1227 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1228 FRAME_GTK_OUTER_WIDGET (f) = 0;
1229 #ifdef USE_GTK_TOOLTIP
1230 if (x->ttip_lbl)
1231 gtk_widget_destroy (x->ttip_lbl);
1232 if (x->ttip_widget)
1233 g_object_unref (G_OBJECT (x->ttip_widget));
1234 #endif
1238 /* Set the normal size hints for the window manager, for frame F.
1239 FLAGS is the flags word to use--or 0 meaning preserve the flags
1240 that the window now has.
1241 If USER_POSITION is nonzero, we set the User Position
1242 flag (this is useful when FLAGS is 0). */
1244 void
1245 x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
1247 /* Must use GTK routines here, otherwise GTK resets the size hints
1248 to its own defaults. */
1249 GdkGeometry size_hints;
1250 gint hint_flags = 0;
1251 int base_width, base_height;
1252 int min_rows = 0, min_cols = 0;
1253 int win_gravity = f->win_gravity;
1255 /* Don't set size hints during initialization; that apparently leads
1256 to a race condition. See the thread at
1257 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1258 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1259 return;
1261 if (flags)
1263 memset (&size_hints, 0, sizeof (size_hints));
1264 f->output_data.x->size_hints = size_hints;
1265 f->output_data.x->hint_flags = hint_flags;
1267 else
1268 flags = f->size_hint_flags;
1270 size_hints = f->output_data.x->size_hints;
1271 hint_flags = f->output_data.x->hint_flags;
1273 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1274 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
1275 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
1277 hint_flags |= GDK_HINT_BASE_SIZE;
1278 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
1279 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
1280 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1282 check_frame_size (f, &min_rows, &min_cols);
1284 size_hints.base_width = base_width;
1285 size_hints.base_height = base_height;
1286 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
1287 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
1289 #ifdef HAVE_GTK3
1290 /* Gtk3 ignores min width/height and overwrites them with its own idea
1291 of min width/height. Put out min values to the widget so Gtk
1292 gets the same value we want it to be. Without this, a user can't
1293 shrink an Emacs frame.
1295 if (FRAME_GTK_WIDGET (f))
1296 emacs_fixed_set_min_size (EMACS_FIXED (FRAME_GTK_WIDGET (f)),
1297 size_hints.min_width,
1298 size_hints.min_height);
1299 #endif
1301 /* These currently have a one to one mapping with the X values, but I
1302 don't think we should rely on that. */
1303 hint_flags |= GDK_HINT_WIN_GRAVITY;
1304 size_hints.win_gravity = 0;
1305 if (win_gravity == NorthWestGravity)
1306 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1307 else if (win_gravity == NorthGravity)
1308 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1309 else if (win_gravity == NorthEastGravity)
1310 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1311 else if (win_gravity == WestGravity)
1312 size_hints.win_gravity = GDK_GRAVITY_WEST;
1313 else if (win_gravity == CenterGravity)
1314 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1315 else if (win_gravity == EastGravity)
1316 size_hints.win_gravity = GDK_GRAVITY_EAST;
1317 else if (win_gravity == SouthWestGravity)
1318 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1319 else if (win_gravity == SouthGravity)
1320 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1321 else if (win_gravity == SouthEastGravity)
1322 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1323 else if (win_gravity == StaticGravity)
1324 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1326 if (user_position)
1328 hint_flags &= ~GDK_HINT_POS;
1329 hint_flags |= GDK_HINT_USER_POS;
1332 if (hint_flags != f->output_data.x->hint_flags
1333 || memcmp (&size_hints,
1334 &f->output_data.x->size_hints,
1335 sizeof (size_hints)) != 0)
1337 BLOCK_INPUT;
1338 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1339 NULL, &size_hints, hint_flags);
1340 f->output_data.x->size_hints = size_hints;
1341 f->output_data.x->hint_flags = hint_flags;
1342 UNBLOCK_INPUT;
1346 /* Change background color of a frame.
1347 Since GTK uses the background color to clear the window, we must
1348 keep the GTK and X colors in sync.
1349 F is the frame to change,
1350 BG is the pixel value to change to. */
1352 void
1353 xg_set_background_color (FRAME_PTR f, long unsigned int bg)
1355 if (FRAME_GTK_WIDGET (f))
1357 BLOCK_INPUT;
1358 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1359 UNBLOCK_INPUT;
1364 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1365 functions so GTK does not overwrite the icon. */
1367 void
1368 xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
1370 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1371 icon_pixmap,
1372 icon_mask);
1373 if (gp)
1374 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1379 /***********************************************************************
1380 Dialog functions
1381 ***********************************************************************/
1382 /* Return the dialog title to use for a dialog of type KEY.
1383 This is the encoding used by lwlib. We use the same for GTK. */
1385 static const char *
1386 get_dialog_title (char key)
1388 const char *title = "";
1390 switch (key) {
1391 case 'E': case 'e':
1392 title = "Error";
1393 break;
1395 case 'I': case 'i':
1396 title = "Information";
1397 break;
1399 case 'L': case 'l':
1400 title = "Prompt";
1401 break;
1403 case 'P': case 'p':
1404 title = "Prompt";
1405 break;
1407 case 'Q': case 'q':
1408 title = "Question";
1409 break;
1412 return title;
1415 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1416 the dialog, but return TRUE so the event does not propagate further
1417 in GTK. This prevents GTK from destroying the dialog widget automatically
1418 and we can always destrou the widget manually, regardles of how
1419 it was popped down (button press or WM_DELETE_WINDOW).
1420 W is the dialog widget.
1421 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1422 user_data is NULL (not used).
1424 Returns TRUE to end propagation of event. */
1426 static gboolean
1427 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1429 gtk_widget_unmap (w);
1430 return TRUE;
1433 /* Create a popup dialog window. See also xg_create_widget below.
1434 WV is a widget_value describing the dialog.
1435 SELECT_CB is the callback to use when a button has been pressed.
1436 DEACTIVATE_CB is the callback to use when the dialog pops down.
1438 Returns the GTK dialog widget. */
1440 static GtkWidget *
1441 create_dialog (widget_value *wv,
1442 GCallback select_cb,
1443 GCallback deactivate_cb)
1445 const char *title = get_dialog_title (wv->name[0]);
1446 int total_buttons = wv->name[1] - '0';
1447 int right_buttons = wv->name[4] - '0';
1448 int left_buttons;
1449 int button_nr = 0;
1450 int button_spacing = 10;
1451 GtkWidget *wdialog = gtk_dialog_new ();
1452 GtkDialog *wd = GTK_DIALOG (wdialog);
1453 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1454 widget_value *item;
1455 GtkWidget *whbox_down;
1457 /* If the number of buttons is greater than 4, make two rows of buttons
1458 instead. This looks better. */
1459 int make_two_rows = total_buttons > 4;
1461 if (right_buttons == 0) right_buttons = total_buttons/2;
1462 left_buttons = total_buttons - right_buttons;
1464 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1465 gtk_widget_set_name (wdialog, "emacs-dialog");
1468 if (make_two_rows)
1470 GtkWidget *wvbox = gtk_vbox_new (TRUE, button_spacing);
1471 GtkWidget *whbox_up = gtk_hbox_new (FALSE, 0);
1472 whbox_down = gtk_hbox_new (FALSE, 0);
1474 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1475 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1476 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1478 cur_box = GTK_BOX (whbox_up);
1481 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1482 G_CALLBACK (dialog_delete_callback), 0);
1484 if (deactivate_cb)
1486 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1487 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1490 for (item = wv->contents; item; item = item->next)
1492 char *utf8_label = get_utf8_string (item->value);
1493 GtkWidget *w;
1494 GtkRequisition req;
1496 if (item->name && strcmp (item->name, "message") == 0)
1498 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1499 /* This is the text part of the dialog. */
1500 w = gtk_label_new (utf8_label);
1501 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1502 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1503 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1505 /* Try to make dialog look better. Must realize first so
1506 the widget can calculate the size it needs. */
1507 gtk_widget_realize (w);
1508 gtk_widget_get_preferred_size (w, NULL, &req);
1509 gtk_box_set_spacing (wvbox, req.height);
1510 if (item->value && strlen (item->value) > 0)
1511 button_spacing = 2*req.width/strlen (item->value);
1513 else
1515 /* This is one button to add to the dialog. */
1516 w = gtk_button_new_with_label (utf8_label);
1517 if (! item->enabled)
1518 gtk_widget_set_sensitive (w, FALSE);
1519 if (select_cb)
1520 g_signal_connect (G_OBJECT (w), "clicked",
1521 select_cb, item->call_data);
1523 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1524 if (++button_nr == left_buttons)
1526 if (make_two_rows)
1527 cur_box = GTK_BOX (whbox_down);
1528 else
1529 gtk_box_pack_start (cur_box,
1530 gtk_label_new (""),
1531 TRUE, TRUE,
1532 button_spacing);
1536 if (utf8_label)
1537 g_free (utf8_label);
1540 return wdialog;
1543 struct xg_dialog_data
1545 GMainLoop *loop;
1546 int response;
1547 GtkWidget *w;
1548 guint timerid;
1551 /* Function that is called when the file or font dialogs pop down.
1552 W is the dialog widget, RESPONSE is the response code.
1553 USER_DATA is what we passed in to g_signal_connect. */
1555 static void
1556 xg_dialog_response_cb (GtkDialog *w,
1557 gint response,
1558 gpointer user_data)
1560 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1561 dd->response = response;
1562 g_main_loop_quit (dd->loop);
1566 /* Destroy the dialog. This makes it pop down. */
1568 static Lisp_Object
1569 pop_down_dialog (Lisp_Object arg)
1571 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1572 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1574 BLOCK_INPUT;
1575 if (dd->w) gtk_widget_destroy (dd->w);
1576 if (dd->timerid != 0) g_source_remove (dd->timerid);
1578 g_main_loop_quit (dd->loop);
1579 g_main_loop_unref (dd->loop);
1581 UNBLOCK_INPUT;
1583 return Qnil;
1586 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1587 We pass in DATA as gpointer* so we can use this as a callback. */
1589 static gboolean
1590 xg_maybe_add_timer (gpointer data)
1592 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1593 EMACS_TIME next_time = timer_check ();
1594 long secs = EMACS_SECS (next_time);
1595 long usecs = EMACS_USECS (next_time);
1597 dd->timerid = 0;
1599 if (secs >= 0 && usecs >= 0 && secs < ((guint)-1)/1000)
1601 dd->timerid = g_timeout_add (secs * 1000 + usecs/1000,
1602 xg_maybe_add_timer,
1603 dd);
1605 return FALSE;
1609 /* Pops up a modal dialog W and waits for response.
1610 We don't use gtk_dialog_run because we want to process emacs timers.
1611 The dialog W is not destroyed when this function returns. */
1613 static int
1614 xg_dialog_run (FRAME_PTR f, GtkWidget *w)
1616 int count = SPECPDL_INDEX ();
1617 struct xg_dialog_data dd;
1619 xg_set_screen (w, f);
1620 gtk_window_set_transient_for (GTK_WINDOW (w),
1621 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1622 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1623 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1625 dd.loop = g_main_loop_new (NULL, FALSE);
1626 dd.response = GTK_RESPONSE_CANCEL;
1627 dd.w = w;
1628 dd.timerid = 0;
1630 g_signal_connect (G_OBJECT (w),
1631 "response",
1632 G_CALLBACK (xg_dialog_response_cb),
1633 &dd);
1634 /* Don't destroy the widget if closed by the window manager close button. */
1635 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1636 gtk_widget_show (w);
1638 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1640 (void) xg_maybe_add_timer (&dd);
1641 g_main_loop_run (dd.loop);
1643 dd.w = 0;
1644 unbind_to (count, Qnil);
1646 return dd.response;
1650 /***********************************************************************
1651 File dialog functions
1652 ***********************************************************************/
1653 /* Return non-zero if the old file selection dialog is being used.
1654 Return zero if not. */
1657 xg_uses_old_file_dialog (void)
1659 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1660 return x_gtk_use_old_file_dialog;
1661 #else
1662 return 0;
1663 #endif
1667 typedef char * (*xg_get_file_func) (GtkWidget *);
1669 /* Return the selected file for file chooser dialog W.
1670 The returned string must be free:d. */
1672 static char *
1673 xg_get_file_name_from_chooser (GtkWidget *w)
1675 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1678 /* Callback called when the "Show hidden files" toggle is pressed.
1679 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1681 static void
1682 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1684 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1685 gboolean visible;
1686 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1687 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1691 /* Callback called when a property changes in a file chooser.
1692 GOBJECT is the file chooser dialog, ARG1 describes the property.
1693 USER_DATA is the toggle widget in the file chooser dialog.
1694 We use this to update the "Show hidden files" toggle when the user
1695 changes that property by right clicking in the file list. */
1697 static void
1698 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1700 if (strcmp (arg1->name, "show-hidden") == 0)
1702 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1703 gboolean visible, toggle_on;
1705 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1706 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1708 if (!!visible != !!toggle_on)
1710 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1711 G_CALLBACK (xg_toggle_visibility_cb),
1712 gobject);
1713 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1714 g_signal_handlers_unblock_by_func
1715 (G_OBJECT (wtoggle),
1716 G_CALLBACK (xg_toggle_visibility_cb),
1717 gobject);
1719 x_gtk_show_hidden_files = visible;
1723 /* Read a file name from the user using a file chooser dialog.
1724 F is the current frame.
1725 PROMPT is a prompt to show to the user. May not be NULL.
1726 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1727 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1728 file. *FUNC is set to a function that can be used to retrieve the
1729 selected file name from the returned widget.
1731 Returns the created widget. */
1733 static GtkWidget *
1734 xg_get_file_with_chooser (FRAME_PTR f,
1735 char *prompt,
1736 char *default_filename,
1737 int mustmatch_p, int only_dir_p,
1738 xg_get_file_func *func)
1740 char msgbuf[1024];
1742 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1743 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1744 GtkFileChooserAction action = (mustmatch_p ?
1745 GTK_FILE_CHOOSER_ACTION_OPEN :
1746 GTK_FILE_CHOOSER_ACTION_SAVE);
1748 if (only_dir_p)
1749 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1751 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1752 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1753 (mustmatch_p || only_dir_p ?
1754 GTK_STOCK_OPEN : GTK_STOCK_OK),
1755 GTK_RESPONSE_OK,
1756 NULL);
1757 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1759 wbox = gtk_vbox_new (FALSE, 0);
1760 gtk_widget_show (wbox);
1761 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1763 if (x_gtk_show_hidden_files)
1765 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1766 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1768 gtk_widget_show (wtoggle);
1769 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1770 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1771 g_signal_connect (G_OBJECT (filewin), "notify",
1772 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1774 if (x_gtk_file_dialog_help_text)
1776 msgbuf[0] = '\0';
1777 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1778 Show the C-l help text only for versions < 2.10. */
1779 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1780 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1781 strcat (msgbuf, "\nIf you don't like this file selector, use the "
1782 "corresponding\nkey binding or customize "
1783 "use-file-dialog to turn it off.");
1785 wmessage = gtk_label_new (msgbuf);
1786 gtk_widget_show (wmessage);
1789 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1790 if (x_gtk_file_dialog_help_text)
1791 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1792 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1794 if (default_filename)
1796 Lisp_Object file;
1797 struct gcpro gcpro1;
1798 char *utf8_filename;
1799 GCPRO1 (file);
1801 file = build_string (default_filename);
1803 /* File chooser does not understand ~/... in the file name. It must be
1804 an absolute name starting with /. */
1805 if (default_filename[0] != '/')
1806 file = Fexpand_file_name (file, Qnil);
1808 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1809 if (! NILP (Ffile_directory_p (file)))
1810 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1811 utf8_filename);
1812 else
1814 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1815 utf8_filename);
1816 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1818 char *cp = strrchr (utf8_filename, '/');
1819 if (cp) ++cp;
1820 else cp = utf8_filename;
1821 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1825 UNGCPRO;
1828 *func = xg_get_file_name_from_chooser;
1829 return filewin;
1832 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1834 /* Return the selected file for file selector dialog W.
1835 The returned string must be free:d. */
1837 static char *
1838 xg_get_file_name_from_selector (GtkWidget *w)
1840 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1841 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1844 /* Create a file selection dialog.
1845 F is the current frame.
1846 PROMPT is a prompt to show to the user. May not be NULL.
1847 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1848 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1849 file. *FUNC is set to a function that can be used to retrieve the
1850 selected file name from the returned widget.
1852 Returns the created widget. */
1854 static GtkWidget *
1855 xg_get_file_with_selection (FRAME_PTR f,
1856 char *prompt,
1857 char *default_filename,
1858 int mustmatch_p, int only_dir_p,
1859 xg_get_file_func *func)
1861 GtkWidget *filewin;
1862 GtkFileSelection *filesel;
1864 filewin = gtk_file_selection_new (prompt);
1865 filesel = GTK_FILE_SELECTION (filewin);
1867 if (default_filename)
1868 gtk_file_selection_set_filename (filesel, default_filename);
1870 if (mustmatch_p)
1872 /* The selection_entry part of filesel is not documented. */
1873 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1874 gtk_file_selection_hide_fileop_buttons (filesel);
1877 *func = xg_get_file_name_from_selector;
1879 return filewin;
1881 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1883 /* Read a file name from the user using a file dialog, either the old
1884 file selection dialog, or the new file chooser dialog. Which to use
1885 depends on what the GTK version used has, and what the value of
1886 gtk-use-old-file-dialog.
1887 F is the current frame.
1888 PROMPT is a prompt to show to the user. May not be NULL.
1889 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1890 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1891 file.
1893 Returns a file name or NULL if no file was selected.
1894 The returned string must be freed by the caller. */
1896 char *
1897 xg_get_file_name (FRAME_PTR f,
1898 char *prompt,
1899 char *default_filename,
1900 int mustmatch_p,
1901 int only_dir_p)
1903 GtkWidget *w = 0;
1904 char *fn = 0;
1905 int filesel_done = 0;
1906 xg_get_file_func func;
1908 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1909 /* I really don't know why this is needed, but without this the GLIBC add on
1910 library linuxthreads hangs when the Gnome file chooser backend creates
1911 threads. */
1912 sigblock (sigmask (__SIGRTMIN));
1913 #endif /* HAVE_GTK_AND_PTHREAD */
1915 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1917 if (xg_uses_old_file_dialog ())
1918 w = xg_get_file_with_selection (f, prompt, default_filename,
1919 mustmatch_p, only_dir_p, &func);
1920 else
1921 w = xg_get_file_with_chooser (f, prompt, default_filename,
1922 mustmatch_p, only_dir_p, &func);
1924 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1925 w = xg_get_file_with_chooser (f, prompt, default_filename,
1926 mustmatch_p, only_dir_p, &func);
1927 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1929 gtk_widget_set_name (w, "emacs-filedialog");
1931 filesel_done = xg_dialog_run (f, w);
1933 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1934 sigunblock (sigmask (__SIGRTMIN));
1935 #endif
1937 if (filesel_done == GTK_RESPONSE_OK)
1938 fn = (*func) (w);
1940 gtk_widget_destroy (w);
1941 return fn;
1944 #ifdef HAVE_FREETYPE
1945 /* Pop up a GTK font selector and return the name of the font the user
1946 selects, as a C string. The returned font name follows GTK's own
1947 format:
1949 `FAMILY [VALUE1 VALUE2] SIZE'
1951 This can be parsed using font_parse_fcname in font.c.
1952 DEFAULT_NAME, if non-zero, is the default font name. */
1954 char *
1955 xg_get_font_name (FRAME_PTR f, const char *default_name)
1957 GtkWidget *w;
1958 char *fontname = NULL;
1959 int done = 0;
1961 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1962 sigblock (sigmask (__SIGRTMIN));
1963 #endif /* HAVE_GTK_AND_PTHREAD */
1965 w = gtk_font_selection_dialog_new ("Pick a font");
1966 if (!default_name)
1967 default_name = "Monospace 10";
1968 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1969 default_name);
1971 gtk_widget_set_name (w, "emacs-fontdialog");
1973 done = xg_dialog_run (f, w);
1975 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1976 sigunblock (sigmask (__SIGRTMIN));
1977 #endif
1979 if (done == GTK_RESPONSE_OK)
1980 fontname = gtk_font_selection_dialog_get_font_name
1981 (GTK_FONT_SELECTION_DIALOG (w));
1983 gtk_widget_destroy (w);
1984 return fontname;
1986 #endif /* HAVE_FREETYPE */
1990 /***********************************************************************
1991 Menu functions.
1992 ***********************************************************************/
1994 /* The name of menu items that can be used for customization. Since GTK
1995 RC files are very crude and primitive, we have to set this on all
1996 menu item names so a user can easily customize menu items. */
1998 #define MENU_ITEM_NAME "emacs-menuitem"
2001 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2002 during GC. The next member points to the items. */
2003 static xg_list_node xg_menu_cb_list;
2005 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2006 during GC. The next member points to the items. */
2007 static xg_list_node xg_menu_item_cb_list;
2009 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2010 F is the frame CL_DATA will be initialized for.
2011 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2013 The menu bar and all sub menus under the menu bar in a frame
2014 share the same structure, hence the reference count.
2016 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2017 allocated xg_menu_cb_data if CL_DATA is NULL. */
2019 static xg_menu_cb_data *
2020 make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
2022 if (! cl_data)
2024 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
2025 cl_data->f = f;
2026 cl_data->menu_bar_vector = f->menu_bar_vector;
2027 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2028 cl_data->highlight_cb = highlight_cb;
2029 cl_data->ref_count = 0;
2031 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2034 cl_data->ref_count++;
2036 return cl_data;
2039 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2040 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2042 When the menu bar is updated, menu items may have been added and/or
2043 removed, so menu_bar_vector and menu_bar_items_used change. We must
2044 then update CL_DATA since it is used to determine which menu
2045 item that is invoked in the menu.
2046 HIGHLIGHT_CB could change, there is no check that the same
2047 function is given when modifying a menu bar as was given when
2048 creating the menu bar. */
2050 static void
2051 update_cl_data (xg_menu_cb_data *cl_data,
2052 FRAME_PTR f,
2053 GCallback highlight_cb)
2055 if (cl_data)
2057 cl_data->f = f;
2058 cl_data->menu_bar_vector = f->menu_bar_vector;
2059 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2060 cl_data->highlight_cb = highlight_cb;
2064 /* Decrease reference count for CL_DATA.
2065 If reference count is zero, free CL_DATA. */
2067 static void
2068 unref_cl_data (xg_menu_cb_data *cl_data)
2070 if (cl_data && cl_data->ref_count > 0)
2072 cl_data->ref_count--;
2073 if (cl_data->ref_count == 0)
2075 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2076 xfree (cl_data);
2081 /* Function that marks all lisp data during GC. */
2083 void
2084 xg_mark_data (void)
2086 xg_list_node *iter;
2088 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2089 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2091 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2093 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2095 if (! NILP (cb_data->help))
2096 mark_object (cb_data->help);
2101 /* Callback called when a menu item is destroyed. Used to free data.
2102 W is the widget that is being destroyed (not used).
2103 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2105 static void
2106 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2108 if (client_data)
2110 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
2111 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2112 xfree (data);
2116 /* Callback called when the pointer enters/leaves a menu item.
2117 W is the parent of the menu item.
2118 EVENT is either an enter event or leave event.
2119 CLIENT_DATA is not used.
2121 Returns FALSE to tell GTK to keep processing this event. */
2123 static gboolean
2124 menuitem_highlight_callback (GtkWidget *w,
2125 GdkEventCrossing *event,
2126 gpointer client_data)
2128 GdkEvent ev;
2129 GtkWidget *subwidget;
2130 xg_menu_item_cb_data *data;
2132 ev.crossing = *event;
2133 subwidget = gtk_get_event_widget (&ev);
2134 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
2135 XG_ITEM_DATA);
2136 if (data)
2138 if (! NILP (data->help) && data->cl_data->highlight_cb)
2140 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2141 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2142 (*func) (subwidget, call_data);
2146 return FALSE;
2149 /* Callback called when a menu is destroyed. Used to free data.
2150 W is the widget that is being destroyed (not used).
2151 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2153 static void
2154 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2156 unref_cl_data ((xg_menu_cb_data*) client_data);
2159 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2160 must be non-NULL) and can be inserted into a menu item.
2162 Returns the GtkHBox. */
2164 static GtkWidget *
2165 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2167 GtkWidget *wlbl;
2168 GtkWidget *wkey;
2169 GtkWidget *wbox;
2171 wbox = gtk_hbox_new (FALSE, 0);
2172 wlbl = gtk_label_new (utf8_label);
2173 wkey = gtk_label_new (utf8_key);
2175 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2176 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2178 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2179 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2181 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2182 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2183 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2185 return wbox;
2188 /* Make and return a menu item widget with the key to the right.
2189 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2190 UTF8_KEY is the text representing the key binding.
2191 ITEM is the widget_value describing the menu item.
2193 GROUP is an in/out parameter. If the menu item to be created is not
2194 part of any radio menu group, *GROUP contains NULL on entry and exit.
2195 If the menu item to be created is part of a radio menu group, on entry
2196 *GROUP contains the group to use, or NULL if this is the first item
2197 in the group. On exit, *GROUP contains the radio item group.
2199 Unfortunately, keys don't line up as nicely as in Motif,
2200 but the MacOS X version doesn't either, so I guess that is OK. */
2202 static GtkWidget *
2203 make_menu_item (const char *utf8_label,
2204 const char *utf8_key,
2205 widget_value *item,
2206 GSList **group)
2208 GtkWidget *w;
2209 GtkWidget *wtoadd = 0;
2211 /* It has been observed that some menu items have a NULL name field.
2212 This will lead to this function being called with a NULL utf8_label.
2213 GTK crashes on that so we set a blank label. Why there is a NULL
2214 name remains to be investigated. */
2215 if (! utf8_label) utf8_label = " ";
2217 if (utf8_key)
2218 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2220 if (item->button_type == BUTTON_TYPE_TOGGLE)
2222 *group = NULL;
2223 if (utf8_key) w = gtk_check_menu_item_new ();
2224 else w = gtk_check_menu_item_new_with_label (utf8_label);
2225 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2227 else if (item->button_type == BUTTON_TYPE_RADIO)
2229 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2230 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2231 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2232 if (item->selected)
2233 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2235 else
2237 *group = NULL;
2238 if (utf8_key) w = gtk_menu_item_new ();
2239 else w = gtk_menu_item_new_with_label (utf8_label);
2242 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2243 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2245 return w;
2248 static int xg_detached_menus;
2250 /* Returns non-zero if there are detached menus. */
2253 xg_have_tear_offs (void)
2255 return xg_detached_menus > 0;
2258 /* Callback invoked when a detached menu window is removed. Here we
2259 decrease the xg_detached_menus count.
2260 WIDGET is the top level window that is removed (the parent of the menu).
2261 CLIENT_DATA is not used. */
2263 static void
2264 tearoff_remove (GtkWidget *widget, gpointer client_data)
2266 if (xg_detached_menus > 0) --xg_detached_menus;
2269 /* Callback invoked when a menu is detached. It increases the
2270 xg_detached_menus count.
2271 WIDGET is the GtkTearoffMenuItem.
2272 CLIENT_DATA is not used. */
2274 static void
2275 tearoff_activate (GtkWidget *widget, gpointer client_data)
2277 GtkWidget *menu = gtk_widget_get_parent (widget);
2278 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2280 ++xg_detached_menus;
2281 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2282 "destroy",
2283 G_CALLBACK (tearoff_remove), 0);
2288 /* Create a menu item widget, and connect the callbacks.
2289 ITEM decribes the menu item.
2290 F is the frame the created menu belongs to.
2291 SELECT_CB is the callback to use when a menu item is selected.
2292 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2293 CL_DATA points to the callback data to be used for this menu.
2294 GROUP is an in/out parameter. If the menu item to be created is not
2295 part of any radio menu group, *GROUP contains NULL on entry and exit.
2296 If the menu item to be created is part of a radio menu group, on entry
2297 *GROUP contains the group to use, or NULL if this is the first item
2298 in the group. On exit, *GROUP contains the radio item group.
2300 Returns the created GtkWidget. */
2302 static GtkWidget *
2303 xg_create_one_menuitem (widget_value *item,
2304 FRAME_PTR f,
2305 GCallback select_cb,
2306 GCallback highlight_cb,
2307 xg_menu_cb_data *cl_data,
2308 GSList **group)
2310 char *utf8_label;
2311 char *utf8_key;
2312 GtkWidget *w;
2313 xg_menu_item_cb_data *cb_data;
2315 utf8_label = get_utf8_string (item->name);
2316 utf8_key = get_utf8_string (item->key);
2318 w = make_menu_item (utf8_label, utf8_key, item, group);
2320 if (utf8_label) g_free (utf8_label);
2321 if (utf8_key) g_free (utf8_key);
2323 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2325 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2327 cb_data->select_id = 0;
2328 cb_data->help = item->help;
2329 cb_data->cl_data = cl_data;
2330 cb_data->call_data = item->call_data;
2332 g_signal_connect (G_OBJECT (w),
2333 "destroy",
2334 G_CALLBACK (menuitem_destroy_callback),
2335 cb_data);
2337 /* Put cb_data in widget, so we can get at it when modifying menubar */
2338 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2340 /* final item, not a submenu */
2341 if (item->call_data && ! item->contents)
2343 if (select_cb)
2344 cb_data->select_id
2345 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2348 return w;
2351 /* Create a full menu tree specified by DATA.
2352 F is the frame the created menu belongs to.
2353 SELECT_CB is the callback to use when a menu item is selected.
2354 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2355 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2356 POP_UP_P is non-zero if we shall create a popup menu.
2357 MENU_BAR_P is non-zero if we shall create a menu bar.
2358 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2359 if MENU_BAR_P is non-zero.
2360 TOPMENU is the topmost GtkWidget that others shall be placed under.
2361 It may be NULL, in that case we create the appropriate widget
2362 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2363 CL_DATA is the callback data we shall use for this menu, or NULL
2364 if we haven't set the first callback yet.
2365 NAME is the name to give to the top level menu if this function
2366 creates it. May be NULL to not set any name.
2368 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2369 not NULL.
2371 This function calls itself to create submenus. */
2373 static GtkWidget *
2374 create_menus (widget_value *data,
2375 FRAME_PTR f,
2376 GCallback select_cb,
2377 GCallback deactivate_cb,
2378 GCallback highlight_cb,
2379 int pop_up_p,
2380 int menu_bar_p,
2381 int add_tearoff_p,
2382 GtkWidget *topmenu,
2383 xg_menu_cb_data *cl_data,
2384 const char *name)
2386 widget_value *item;
2387 GtkWidget *wmenu = topmenu;
2388 GSList *group = NULL;
2390 if (! topmenu)
2392 if (! menu_bar_p)
2394 wmenu = gtk_menu_new ();
2395 xg_set_screen (wmenu, f);
2396 /* Connect this to the menu instead of items so we get enter/leave for
2397 disabled items also. TODO: Still does not get enter/leave for
2398 disabled items in detached menus. */
2399 g_signal_connect (G_OBJECT (wmenu),
2400 "enter-notify-event",
2401 G_CALLBACK (menuitem_highlight_callback),
2402 NULL);
2403 g_signal_connect (G_OBJECT (wmenu),
2404 "leave-notify-event",
2405 G_CALLBACK (menuitem_highlight_callback),
2406 NULL);
2408 else
2410 wmenu = gtk_menu_bar_new ();
2411 /* Set width of menu bar to a small value so it doesn't enlarge
2412 a small initial frame size. The width will be set to the
2413 width of the frame later on when it is added to a container.
2414 height -1: Natural height. */
2415 gtk_widget_set_size_request (wmenu, 1, -1);
2418 /* Put cl_data on the top menu for easier access. */
2419 cl_data = make_cl_data (cl_data, f, highlight_cb);
2420 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2421 g_signal_connect (G_OBJECT (wmenu), "destroy",
2422 G_CALLBACK (menu_destroy_callback), cl_data);
2424 if (name)
2425 gtk_widget_set_name (wmenu, name);
2427 if (deactivate_cb)
2428 g_signal_connect (G_OBJECT (wmenu),
2429 "selection-done", deactivate_cb, 0);
2432 if (! menu_bar_p && add_tearoff_p)
2434 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2435 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2437 g_signal_connect (G_OBJECT (tearoff), "activate",
2438 G_CALLBACK (tearoff_activate), 0);
2441 for (item = data; item; item = item->next)
2443 GtkWidget *w;
2445 if (pop_up_p && !item->contents && !item->call_data
2446 && !menu_separator_name_p (item->name))
2448 char *utf8_label;
2449 /* A title for a popup. We do the same as GTK does when
2450 creating titles, but it does not look good. */
2451 group = NULL;
2452 utf8_label = get_utf8_string (item->name);
2454 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2455 w = gtk_menu_item_new_with_label (utf8_label);
2456 gtk_widget_set_sensitive (w, FALSE);
2457 if (utf8_label) g_free (utf8_label);
2459 else if (menu_separator_name_p (item->name))
2461 group = NULL;
2462 /* GTK only have one separator type. */
2463 w = gtk_separator_menu_item_new ();
2465 else
2467 w = xg_create_one_menuitem (item,
2469 item->contents ? 0 : select_cb,
2470 highlight_cb,
2471 cl_data,
2472 &group);
2474 /* Create a possibly empty submenu for menu bar items, since some
2475 themes don't highlight items correctly without it. */
2476 if (item->contents || menu_bar_p)
2478 GtkWidget *submenu = create_menus (item->contents,
2480 select_cb,
2481 deactivate_cb,
2482 highlight_cb,
2485 add_tearoff_p,
2487 cl_data,
2489 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2493 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2494 gtk_widget_set_name (w, MENU_ITEM_NAME);
2497 return wmenu;
2500 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2501 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2502 with some text and buttons.
2503 F is the frame the created item belongs to.
2504 NAME is the name to use for the top widget.
2505 VAL is a widget_value structure describing items to be created.
2506 SELECT_CB is the callback to use when a menu item is selected or
2507 a dialog button is pressed.
2508 DEACTIVATE_CB is the callback to use when an item is deactivated.
2509 For a menu, when a sub menu is not shown anymore, for a dialog it is
2510 called when the dialog is popped down.
2511 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2513 Returns the widget created. */
2515 GtkWidget *
2516 xg_create_widget (const char *type, const char *name, FRAME_PTR f, widget_value *val,
2517 GCallback select_cb, GCallback deactivate_cb,
2518 GCallback highlight_cb)
2520 GtkWidget *w = 0;
2521 int menu_bar_p = strcmp (type, "menubar") == 0;
2522 int pop_up_p = strcmp (type, "popup") == 0;
2524 if (strcmp (type, "dialog") == 0)
2526 w = create_dialog (val, select_cb, deactivate_cb);
2527 xg_set_screen (w, f);
2528 gtk_window_set_transient_for (GTK_WINDOW (w),
2529 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2530 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2531 gtk_widget_set_name (w, "emacs-dialog");
2532 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2534 else if (menu_bar_p || pop_up_p)
2536 w = create_menus (val->contents,
2538 select_cb,
2539 deactivate_cb,
2540 highlight_cb,
2541 pop_up_p,
2542 menu_bar_p,
2543 menu_bar_p,
2546 name);
2548 /* Set the cursor to an arrow for popup menus when they are mapped.
2549 This is done by default for menu bar menus. */
2550 if (pop_up_p)
2552 /* Must realize so the GdkWindow inside the widget is created. */
2553 gtk_widget_realize (w);
2554 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2557 else
2559 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2560 type);
2563 return w;
2566 /* Return the label for menu item WITEM. */
2568 static const char *
2569 xg_get_menu_item_label (GtkMenuItem *witem)
2571 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2572 return gtk_label_get_label (wlabel);
2575 /* Return non-zero if the menu item WITEM has the text LABEL. */
2577 static int
2578 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2580 int is_same = 0;
2581 char *utf8_label = get_utf8_string (label);
2582 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2584 if (! old_label && ! utf8_label)
2585 is_same = 1;
2586 else if (old_label && utf8_label)
2587 is_same = strcmp (utf8_label, old_label) == 0;
2589 if (utf8_label) g_free (utf8_label);
2591 return is_same;
2594 /* Destroy widgets in LIST. */
2596 static void
2597 xg_destroy_widgets (GList *list)
2599 GList *iter;
2601 for (iter = list; iter; iter = g_list_next (iter))
2603 GtkWidget *w = GTK_WIDGET (iter->data);
2605 /* Destroying the widget will remove it from the container it is in. */
2606 gtk_widget_destroy (w);
2610 /* Update the top level names in MENUBAR (i.e. not submenus).
2611 F is the frame the menu bar belongs to.
2612 *LIST is a list with the current menu bar names (menu item widgets).
2613 ITER is the item within *LIST that shall be updated.
2614 POS is the numerical position, starting at 0, of ITER in *LIST.
2615 VAL describes what the menu bar shall look like after the update.
2616 SELECT_CB is the callback to use when a menu item is selected.
2617 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2618 CL_DATA points to the callback data to be used for this menu bar.
2620 This function calls itself to walk through the menu bar names. */
2622 static void
2623 xg_update_menubar (GtkWidget *menubar,
2624 FRAME_PTR f,
2625 GList **list,
2626 GList *iter,
2627 int pos,
2628 widget_value *val,
2629 GCallback select_cb,
2630 GCallback deactivate_cb,
2631 GCallback highlight_cb,
2632 xg_menu_cb_data *cl_data)
2634 if (! iter && ! val)
2635 return;
2636 else if (iter && ! val)
2638 /* Item(s) have been removed. Remove all remaining items. */
2639 xg_destroy_widgets (iter);
2641 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2642 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2643 gtk_menu_item_new_with_label (""),
2645 /* All updated. */
2646 val = 0;
2647 iter = 0;
2649 else if (! iter && val)
2651 /* Item(s) added. Add all new items in one call. */
2652 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2653 0, 1, 0, menubar, cl_data, 0);
2655 /* All updated. */
2656 val = 0;
2657 iter = 0;
2659 /* Below this neither iter or val is NULL */
2660 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2662 /* This item is still the same, check next item. */
2663 val = val->next;
2664 iter = g_list_next (iter);
2665 ++pos;
2667 else /* This item is changed. */
2669 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2670 GtkMenuItem *witem2 = 0;
2671 int val_in_menubar = 0;
2672 int iter_in_new_menubar = 0;
2673 GList *iter2;
2674 widget_value *cur;
2676 /* See if the changed entry (val) is present later in the menu bar */
2677 for (iter2 = iter;
2678 iter2 && ! val_in_menubar;
2679 iter2 = g_list_next (iter2))
2681 witem2 = GTK_MENU_ITEM (iter2->data);
2682 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2685 /* See if the current entry (iter) is present later in the
2686 specification for the new menu bar. */
2687 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2688 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2690 if (val_in_menubar && ! iter_in_new_menubar)
2692 int nr = pos;
2694 /* This corresponds to:
2695 Current: A B C
2696 New: A C
2697 Remove B. */
2699 g_object_ref (G_OBJECT (witem));
2700 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2701 gtk_widget_destroy (GTK_WIDGET (witem));
2703 /* Must get new list since the old changed. */
2704 g_list_free (*list);
2705 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2706 while (nr-- > 0) iter = g_list_next (iter);
2708 else if (! val_in_menubar && ! iter_in_new_menubar)
2710 /* This corresponds to:
2711 Current: A B C
2712 New: A X C
2713 Rename B to X. This might seem to be a strange thing to do,
2714 since if there is a menu under B it will be totally wrong for X.
2715 But consider editing a C file. Then there is a C-mode menu
2716 (corresponds to B above).
2717 If then doing C-x C-f the minibuf menu (X above) replaces the
2718 C-mode menu. When returning from the minibuffer, we get
2719 back the C-mode menu. Thus we do:
2720 Rename B to X (C-mode to minibuf menu)
2721 Rename X to B (minibuf to C-mode menu).
2722 If the X menu hasn't been invoked, the menu under B
2723 is up to date when leaving the minibuffer. */
2724 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2725 char *utf8_label = get_utf8_string (val->name);
2726 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2728 gtk_label_set_text (wlabel, utf8_label);
2730 /* If this item has a submenu that has been detached, change
2731 the title in the WM decorations also. */
2732 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2733 /* Set the title of the detached window. */
2734 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2736 if (utf8_label) g_free (utf8_label);
2737 iter = g_list_next (iter);
2738 val = val->next;
2739 ++pos;
2741 else if (! val_in_menubar && iter_in_new_menubar)
2743 /* This corresponds to:
2744 Current: A B C
2745 New: A X B C
2746 Insert X. */
2748 int nr = pos;
2749 GSList *group = 0;
2750 GtkWidget *w = xg_create_one_menuitem (val,
2752 select_cb,
2753 highlight_cb,
2754 cl_data,
2755 &group);
2757 /* Create a possibly empty submenu for menu bar items, since some
2758 themes don't highlight items correctly without it. */
2759 GtkWidget *submenu = create_menus (NULL, f,
2760 select_cb, deactivate_cb,
2761 highlight_cb,
2762 0, 0, 0, 0, cl_data, 0);
2763 gtk_widget_set_name (w, MENU_ITEM_NAME);
2764 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2765 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2767 g_list_free (*list);
2768 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2769 while (nr-- > 0) iter = g_list_next (iter);
2770 iter = g_list_next (iter);
2771 val = val->next;
2772 ++pos;
2774 else /* if (val_in_menubar && iter_in_new_menubar) */
2776 int nr = pos;
2777 /* This corresponds to:
2778 Current: A B C
2779 New: A C B
2780 Move C before B */
2782 g_object_ref (G_OBJECT (witem2));
2783 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2784 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2785 GTK_WIDGET (witem2), pos);
2786 g_object_unref (G_OBJECT (witem2));
2788 g_list_free (*list);
2789 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2790 while (nr-- > 0) iter = g_list_next (iter);
2791 if (iter) iter = g_list_next (iter);
2792 val = val->next;
2793 ++pos;
2797 /* Update the rest of the menu bar. */
2798 xg_update_menubar (menubar, f, list, iter, pos, val,
2799 select_cb, deactivate_cb, highlight_cb, cl_data);
2802 /* Update the menu item W so it corresponds to VAL.
2803 SELECT_CB is the callback to use when a menu item is selected.
2804 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2805 CL_DATA is the data to set in the widget for menu invocation. */
2807 static void
2808 xg_update_menu_item (widget_value *val,
2809 GtkWidget *w,
2810 GCallback select_cb,
2811 GCallback highlight_cb,
2812 xg_menu_cb_data *cl_data)
2814 GtkWidget *wchild;
2815 GtkLabel *wlbl = 0;
2816 GtkLabel *wkey = 0;
2817 char *utf8_label;
2818 char *utf8_key;
2819 const char *old_label = 0;
2820 const char *old_key = 0;
2821 xg_menu_item_cb_data *cb_data;
2823 wchild = XG_BIN_CHILD (w);
2824 utf8_label = get_utf8_string (val->name);
2825 utf8_key = get_utf8_string (val->key);
2827 /* See if W is a menu item with a key. See make_menu_item above. */
2828 if (GTK_IS_HBOX (wchild))
2830 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2832 wlbl = GTK_LABEL (list->data);
2833 wkey = GTK_LABEL (list->next->data);
2834 g_list_free (list);
2836 if (! utf8_key)
2838 /* Remove the key and keep just the label. */
2839 g_object_ref (G_OBJECT (wlbl));
2840 gtk_container_remove (GTK_CONTAINER (w), wchild);
2841 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2842 g_object_unref (G_OBJECT (wlbl));
2843 wkey = 0;
2847 else /* Just a label. */
2849 wlbl = GTK_LABEL (wchild);
2851 /* Check if there is now a key. */
2852 if (utf8_key)
2854 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2855 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2857 wlbl = GTK_LABEL (list->data);
2858 wkey = GTK_LABEL (list->next->data);
2859 g_list_free (list);
2861 gtk_container_remove (GTK_CONTAINER (w), wchild);
2862 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2867 if (wkey) old_key = gtk_label_get_label (wkey);
2868 if (wlbl) old_label = gtk_label_get_label (wlbl);
2870 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2871 gtk_label_set_text (wkey, utf8_key);
2873 if (! old_label || strcmp (utf8_label, old_label) != 0)
2874 gtk_label_set_text (wlbl, utf8_label);
2876 if (utf8_key) g_free (utf8_key);
2877 if (utf8_label) g_free (utf8_label);
2879 if (! val->enabled && gtk_widget_get_sensitive (w))
2880 gtk_widget_set_sensitive (w, FALSE);
2881 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2882 gtk_widget_set_sensitive (w, TRUE);
2884 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2885 XG_ITEM_DATA);
2886 if (cb_data)
2888 cb_data->call_data = val->call_data;
2889 cb_data->help = val->help;
2890 cb_data->cl_data = cl_data;
2892 /* We assume the callback functions don't change. */
2893 if (val->call_data && ! val->contents)
2895 /* This item shall have a select callback. */
2896 if (! cb_data->select_id)
2897 cb_data->select_id
2898 = g_signal_connect (G_OBJECT (w), "activate",
2899 select_cb, cb_data);
2901 else if (cb_data->select_id)
2903 g_signal_handler_disconnect (w, cb_data->select_id);
2904 cb_data->select_id = 0;
2909 /* Update the toggle menu item W so it corresponds to VAL. */
2911 static void
2912 xg_update_toggle_item (widget_value *val, GtkWidget *w)
2914 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2917 /* Update the radio menu item W so it corresponds to VAL. */
2919 static void
2920 xg_update_radio_item (widget_value *val, GtkWidget *w)
2922 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2925 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2926 SUBMENU may be NULL, in that case a new menu is created.
2927 F is the frame the menu bar belongs to.
2928 VAL describes the contents of the menu bar.
2929 SELECT_CB is the callback to use when a menu item is selected.
2930 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2931 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2932 CL_DATA is the call back data to use for any newly created items.
2934 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2935 was NULL. */
2937 static GtkWidget *
2938 xg_update_submenu (GtkWidget *submenu,
2939 FRAME_PTR f,
2940 widget_value *val,
2941 GCallback select_cb,
2942 GCallback deactivate_cb,
2943 GCallback highlight_cb,
2944 xg_menu_cb_data *cl_data)
2946 GtkWidget *newsub = submenu;
2947 GList *list = 0;
2948 GList *iter;
2949 widget_value *cur;
2950 int has_tearoff_p = 0;
2951 GList *first_radio = 0;
2953 if (submenu)
2954 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2956 for (cur = val, iter = list;
2957 cur && iter;
2958 iter = g_list_next (iter), cur = cur->next)
2960 GtkWidget *w = GTK_WIDGET (iter->data);
2962 /* Skip tearoff items, they have no counterpart in val. */
2963 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2965 has_tearoff_p = 1;
2966 iter = g_list_next (iter);
2967 if (iter) w = GTK_WIDGET (iter->data);
2968 else break;
2971 /* Remember first radio button in a group. If we get a mismatch in
2972 a radio group we must rebuild the whole group so that the connections
2973 in GTK becomes correct. */
2974 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2975 first_radio = iter;
2976 else if (cur->button_type != BUTTON_TYPE_RADIO
2977 && ! GTK_IS_RADIO_MENU_ITEM (w))
2978 first_radio = 0;
2980 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2982 if (! menu_separator_name_p (cur->name))
2983 break;
2985 else if (GTK_IS_CHECK_MENU_ITEM (w))
2987 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2988 break;
2989 xg_update_toggle_item (cur, w);
2990 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2992 else if (GTK_IS_RADIO_MENU_ITEM (w))
2994 if (cur->button_type != BUTTON_TYPE_RADIO)
2995 break;
2996 xg_update_radio_item (cur, w);
2997 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2999 else if (GTK_IS_MENU_ITEM (w))
3001 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3002 GtkWidget *sub;
3004 if (cur->button_type != BUTTON_TYPE_NONE ||
3005 menu_separator_name_p (cur->name))
3006 break;
3008 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3010 sub = gtk_menu_item_get_submenu (witem);
3011 if (sub && ! cur->contents)
3013 /* Not a submenu anymore. */
3014 g_object_ref (G_OBJECT (sub));
3015 remove_submenu (witem);
3016 gtk_widget_destroy (sub);
3018 else if (cur->contents)
3020 GtkWidget *nsub;
3022 nsub = xg_update_submenu (sub, f, cur->contents,
3023 select_cb, deactivate_cb,
3024 highlight_cb, cl_data);
3026 /* If this item just became a submenu, we must set it. */
3027 if (nsub != sub)
3028 gtk_menu_item_set_submenu (witem, nsub);
3031 else
3033 /* Structural difference. Remove everything from here and down
3034 in SUBMENU. */
3035 break;
3039 /* Remove widgets from first structual change. */
3040 if (iter)
3042 /* If we are adding new menu items below, we must remove from
3043 first radio button so that radio groups become correct. */
3044 if (cur && first_radio) xg_destroy_widgets (first_radio);
3045 else xg_destroy_widgets (iter);
3048 if (cur)
3050 /* More items added. Create them. */
3051 newsub = create_menus (cur,
3053 select_cb,
3054 deactivate_cb,
3055 highlight_cb,
3058 ! has_tearoff_p,
3059 submenu,
3060 cl_data,
3064 if (list) g_list_free (list);
3066 return newsub;
3069 /* Update the MENUBAR.
3070 F is the frame the menu bar belongs to.
3071 VAL describes the contents of the menu bar.
3072 If DEEP_P is non-zero, rebuild all but the top level menu names in
3073 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3074 SELECT_CB is the callback to use when a menu item is selected.
3075 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3076 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3078 void
3079 xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
3080 int deep_p,
3081 GCallback select_cb, GCallback deactivate_cb,
3082 GCallback highlight_cb)
3084 xg_menu_cb_data *cl_data;
3085 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3087 if (! list) return;
3089 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
3090 XG_FRAME_DATA);
3092 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3093 select_cb, deactivate_cb, highlight_cb, cl_data);
3095 if (deep_p)
3097 widget_value *cur;
3099 /* Update all sub menus.
3100 We must keep the submenus (GTK menu item widgets) since the
3101 X Window in the XEvent that activates the menu are those widgets. */
3103 /* Update cl_data, menu_item things in F may have changed. */
3104 update_cl_data (cl_data, f, highlight_cb);
3106 for (cur = val->contents; cur; cur = cur->next)
3108 GList *iter;
3109 GtkWidget *sub = 0;
3110 GtkWidget *newsub;
3111 GtkMenuItem *witem = 0;
3113 /* Find sub menu that corresponds to val and update it. */
3114 for (iter = list ; iter; iter = g_list_next (iter))
3116 witem = GTK_MENU_ITEM (iter->data);
3117 if (xg_item_label_same_p (witem, cur->name))
3119 sub = gtk_menu_item_get_submenu (witem);
3120 break;
3124 newsub = xg_update_submenu (sub,
3126 cur->contents,
3127 select_cb,
3128 deactivate_cb,
3129 highlight_cb,
3130 cl_data);
3131 /* sub may still be NULL. If we just updated non deep and added
3132 a new menu bar item, it has no sub menu yet. So we set the
3133 newly created sub menu under witem. */
3134 if (newsub != sub && witem != 0)
3136 xg_set_screen (newsub, f);
3137 gtk_menu_item_set_submenu (witem, newsub);
3142 g_list_free (list);
3143 gtk_widget_show_all (menubar);
3146 /* Callback called when the menu bar W is mapped.
3147 Used to find the height of the menu bar if we didn't get it
3148 after showing the widget. */
3150 static void
3151 menubar_map_cb (GtkWidget *w, gpointer user_data)
3153 GtkRequisition req;
3154 FRAME_PTR f = (FRAME_PTR) user_data;
3155 gtk_widget_get_preferred_size (w, NULL, &req);
3156 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3158 FRAME_MENUBAR_HEIGHT (f) = req.height;
3159 xg_height_or_width_changed (f);
3163 /* Recompute all the widgets of frame F, when the menu bar has been
3164 changed. Value is non-zero if widgets were updated. */
3167 xg_update_frame_menubar (FRAME_PTR f)
3169 struct x_output *x = f->output_data.x;
3170 GtkRequisition req;
3172 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3173 return 0;
3175 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3176 return 0; /* Already done this, happens for frames created invisible. */
3178 BLOCK_INPUT;
3180 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3181 FALSE, FALSE, 0);
3182 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3184 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3185 gtk_widget_show_all (x->menubar_widget);
3186 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3188 /* If menu bar doesn't know its height yet, cheat a little so the frame
3189 doesn't jump so much when resized later in menubar_map_cb. */
3190 if (req.height == 0)
3191 req.height = 23;
3193 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3195 FRAME_MENUBAR_HEIGHT (f) = req.height;
3196 xg_height_or_width_changed (f);
3198 UNBLOCK_INPUT;
3200 return 1;
3203 /* Get rid of the menu bar of frame F, and free its storage.
3204 This is used when deleting a frame, and when turning off the menu bar. */
3206 void
3207 free_frame_menubar (FRAME_PTR f)
3209 struct x_output *x = f->output_data.x;
3211 if (x->menubar_widget)
3213 BLOCK_INPUT;
3215 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3216 /* The menubar and its children shall be deleted when removed from
3217 the container. */
3218 x->menubar_widget = 0;
3219 FRAME_MENUBAR_HEIGHT (f) = 0;
3220 xg_height_or_width_changed (f);
3221 UNBLOCK_INPUT;
3226 xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
3228 struct x_output *x = f->output_data.x;
3229 GList *iter;
3230 GdkRectangle rec;
3231 GList *list;
3232 GdkDisplay *gdpy;
3233 GdkWindow *gw;
3234 GdkEvent gevent;
3235 GtkWidget *gwdesc;
3237 if (! x->menubar_widget) return 0;
3239 if (! (event->xbutton.x >= 0
3240 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3241 && event->xbutton.y >= 0
3242 && event->xbutton.y < f->output_data.x->menubar_height
3243 && event->xbutton.same_screen))
3244 return 0;
3246 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3247 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3248 if (! gw) return 0;
3249 gevent.any.window = gw;
3250 gwdesc = gtk_get_event_widget (&gevent);
3251 if (! gwdesc) return 0;
3252 if (! GTK_IS_MENU_BAR (gwdesc)
3253 && ! GTK_IS_MENU_ITEM (gwdesc)
3254 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3255 return 0;
3257 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3258 if (! list) return 0;
3259 rec.x = event->xbutton.x;
3260 rec.y = event->xbutton.y;
3261 rec.width = 1;
3262 rec.height = 1;
3264 for (iter = list ; iter; iter = g_list_next (iter))
3266 GtkWidget *w = GTK_WIDGET (iter->data);
3267 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3268 break;
3270 g_list_free (list);
3271 return iter == 0 ? 0 : 1;
3276 /***********************************************************************
3277 Scroll bar functions
3278 ***********************************************************************/
3281 /* Setting scroll bar values invokes the callback. Use this variable
3282 to indicate that callback should do nothing. */
3284 int xg_ignore_gtk_scrollbar;
3286 /* The width of the scroll bar for the current theme. */
3288 static int scroll_bar_width_for_theme;
3290 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3291 may be larger than 32 bits. Keep a mapping from integer index to widget
3292 pointers to get around the 32 bit limitation. */
3294 static struct
3296 GtkWidget **widgets;
3297 int max_size;
3298 int used;
3299 } id_to_widget;
3301 /* Grow this much every time we need to allocate more */
3303 #define ID_TO_WIDGET_INCR 32
3305 /* Store the widget pointer W in id_to_widget and return the integer index. */
3307 static int
3308 xg_store_widget_in_map (GtkWidget *w)
3310 int i;
3312 if (id_to_widget.max_size == id_to_widget.used)
3314 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3316 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3317 sizeof (GtkWidget *)*new_size);
3319 for (i = id_to_widget.max_size; i < new_size; ++i)
3320 id_to_widget.widgets[i] = 0;
3321 id_to_widget.max_size = new_size;
3324 /* Just loop over the array and find a free place. After all,
3325 how many scroll bars are we creating? Should be a small number.
3326 The check above guarantees we will find a free place. */
3327 for (i = 0; i < id_to_widget.max_size; ++i)
3329 if (! id_to_widget.widgets[i])
3331 id_to_widget.widgets[i] = w;
3332 ++id_to_widget.used;
3334 return i;
3338 /* Should never end up here */
3339 abort ();
3342 /* Remove pointer at IDX from id_to_widget.
3343 Called when scroll bar is destroyed. */
3345 static void
3346 xg_remove_widget_from_map (int idx)
3348 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3350 id_to_widget.widgets[idx] = 0;
3351 --id_to_widget.used;
3355 /* Get the widget pointer at IDX from id_to_widget. */
3357 static GtkWidget *
3358 xg_get_widget_from_map (int idx)
3360 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3361 return id_to_widget.widgets[idx];
3363 return 0;
3366 static void
3367 update_theme_scrollbar_width (void)
3369 #ifdef HAVE_GTK3
3370 GtkAdjustment *vadj;
3371 #else
3372 GtkObject *vadj;
3373 #endif
3374 GtkWidget *wscroll;
3375 int w = 0, b = 0;
3377 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3378 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3379 g_object_ref_sink (G_OBJECT (wscroll));
3380 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3381 gtk_widget_destroy (wscroll);
3382 g_object_unref (G_OBJECT (wscroll));
3383 w += 2*b;
3384 if (w < 16) w = 16;
3385 scroll_bar_width_for_theme = w;
3389 xg_get_default_scrollbar_width (void)
3391 return scroll_bar_width_for_theme;
3394 /* Return the scrollbar id for X Window WID on display DPY.
3395 Return -1 if WID not in id_to_widget. */
3398 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3400 int idx;
3401 GtkWidget *w;
3403 w = xg_win_to_widget (dpy, wid);
3405 if (w)
3407 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3408 if (id_to_widget.widgets[idx] == w)
3409 return idx;
3412 return -1;
3415 /* Callback invoked when scroll bar WIDGET is destroyed.
3416 DATA is the index into id_to_widget for WIDGET.
3417 We free pointer to last scroll bar values here and remove the index. */
3419 static void
3420 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3422 int id = (intptr_t) data;
3423 xg_remove_widget_from_map (id);
3426 /* Create a scroll bar widget for frame F. Store the scroll bar
3427 in BAR.
3428 SCROLL_CALLBACK is the callback to invoke when the value of the
3429 bar changes.
3430 END_CALLBACK is the callback to invoke when scrolling ends.
3431 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3432 to set resources for the widget. */
3434 void
3435 xg_create_scroll_bar (FRAME_PTR f,
3436 struct scroll_bar *bar,
3437 GCallback scroll_callback,
3438 GCallback end_callback,
3439 const char *scroll_bar_name)
3441 GtkWidget *wscroll;
3442 GtkWidget *webox;
3443 intptr_t scroll_id;
3444 #ifdef HAVE_GTK3
3445 GtkAdjustment *vadj;
3446 #else
3447 GtkObject *vadj;
3448 #endif
3450 /* Page, step increment values are not so important here, they
3451 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3452 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3453 0.1, 0.1, 0.1);
3455 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3456 webox = gtk_event_box_new ();
3457 gtk_widget_set_name (wscroll, scroll_bar_name);
3458 #ifndef HAVE_GTK3
3459 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3460 #endif
3461 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3463 scroll_id = xg_store_widget_in_map (wscroll);
3465 g_signal_connect (G_OBJECT (wscroll),
3466 "destroy",
3467 G_CALLBACK (xg_gtk_scroll_destroy),
3468 (gpointer) scroll_id);
3469 g_signal_connect (G_OBJECT (wscroll),
3470 "change-value",
3471 scroll_callback,
3472 (gpointer) bar);
3473 g_signal_connect (G_OBJECT (wscroll),
3474 "button-release-event",
3475 end_callback,
3476 (gpointer) bar);
3478 /* The scroll bar widget does not draw on a window of its own. Instead
3479 it draws on the parent window, in this case the edit widget. So
3480 whenever the edit widget is cleared, the scroll bar needs to redraw
3481 also, which causes flicker. Put an event box between the edit widget
3482 and the scroll bar, so the scroll bar instead draws itself on the
3483 event box window. */
3484 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3485 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3488 /* Set the cursor to an arrow. */
3489 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3491 bar->x_window = scroll_id;
3494 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3496 void
3497 xg_remove_scroll_bar (FRAME_PTR f, int scrollbar_id)
3499 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3500 if (w)
3502 GtkWidget *wparent = gtk_widget_get_parent (w);
3503 gtk_widget_destroy (w);
3504 gtk_widget_destroy (wparent);
3505 SET_FRAME_GARBAGED (f);
3509 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3510 in frame F.
3511 TOP/LEFT are the new pixel positions where the bar shall appear.
3512 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3514 void
3515 xg_update_scrollbar_pos (FRAME_PTR f,
3516 int scrollbar_id,
3517 int top,
3518 int left,
3519 int width,
3520 int height)
3523 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3525 if (wscroll)
3527 GtkWidget *wfixed = f->output_data.x->edit_widget;
3528 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3529 gint msl;
3531 /* Clear out old position. */
3532 int oldx = -1, oldy = -1, oldw, oldh;
3533 if (gtk_widget_get_parent (wparent) == wfixed)
3535 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3536 "x", &oldx, "y", &oldy, NULL);
3537 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3540 /* Move and resize to new values. */
3541 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3542 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3543 if (msl > height)
3545 /* No room. Hide scroll bar as some themes output a warning if
3546 the height is less than the min size. */
3547 gtk_widget_hide (wparent);
3548 gtk_widget_hide (wscroll);
3550 else
3552 gtk_widget_show_all (wparent);
3553 gtk_widget_set_size_request (wscroll, width, height);
3555 gtk_widget_queue_draw (wfixed);
3556 gdk_window_process_all_updates ();
3557 if (oldx != -1 && oldw > 0 && oldh > 0)
3559 /* Clear under old scroll bar position. This must be done after
3560 the gtk_widget_queue_draw and gdk_window_process_all_updates
3561 above. */
3562 x_clear_area (FRAME_X_DISPLAY (f),
3563 FRAME_X_WINDOW (f),
3564 oldx, oldy, oldw, oldh, 0);
3567 /* GTK does not redraw until the main loop is entered again, but
3568 if there are no X events pending we will not enter it. So we sync
3569 here to get some events. */
3571 x_sync (f);
3572 SET_FRAME_GARBAGED (f);
3573 cancel_mouse_face (f);
3577 /* Get the current value of the range, truncated to an integer. */
3579 static int
3580 int_gtk_range_get_value (GtkRange *range)
3582 return gtk_range_get_value (range);
3586 /* Set the thumb size and position of scroll bar BAR. We are currently
3587 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3589 void
3590 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3591 int portion,
3592 int position,
3593 int whole)
3595 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3597 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3599 if (wscroll && NILP (bar->dragging))
3601 GtkAdjustment *adj;
3602 gdouble shown;
3603 gdouble top;
3604 int size, value;
3605 int old_size;
3606 int new_step;
3607 int changed = 0;
3609 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3611 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3612 rather than the real portion value. This makes the thumb less likely
3613 to resize and that looks better. */
3614 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3615 /* When the thumb is at the bottom, position == whole.
3616 So we need to increase `whole' to make space for the thumb. */
3617 whole += portion;
3619 if (whole <= 0)
3620 top = 0, shown = 1;
3621 else
3623 top = (gdouble) position / whole;
3624 shown = (gdouble) portion / whole;
3627 size = shown * XG_SB_RANGE;
3628 size = min (size, XG_SB_RANGE);
3629 size = max (size, 1);
3631 value = top * XG_SB_RANGE;
3632 value = min (value, XG_SB_MAX - size);
3633 value = max (value, XG_SB_MIN);
3635 /* Assume all lines are of equal size. */
3636 new_step = size / max (1, FRAME_LINES (f));
3638 old_size = gtk_adjustment_get_page_size (adj);
3639 if (old_size != size)
3641 int old_step = gtk_adjustment_get_step_increment (adj);
3642 if (old_step != new_step)
3644 gtk_adjustment_set_page_size (adj, size);
3645 gtk_adjustment_set_step_increment (adj, new_step);
3646 /* Assume a page increment is about 95% of the page size */
3647 gtk_adjustment_set_page_increment (adj,(int) (0.95*size));
3648 changed = 1;
3652 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3654 BLOCK_INPUT;
3656 /* gtk_range_set_value invokes the callback. Set
3657 ignore_gtk_scrollbar to make the callback do nothing */
3658 xg_ignore_gtk_scrollbar = 1;
3660 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3661 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3662 else if (changed)
3663 gtk_adjustment_changed (adj);
3665 xg_ignore_gtk_scrollbar = 0;
3667 UNBLOCK_INPUT;
3672 /* Return non-zero if EVENT is for a scroll bar in frame F.
3673 When the same X window is used for several Gtk+ widgets, we cannot
3674 say for sure based on the X window alone if an event is for the
3675 frame. This function does additional checks.
3677 Return non-zero if the event is for a scroll bar, zero otherwise. */
3680 xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
3682 int retval = 0;
3684 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3686 /* Check if press occurred outside the edit widget. */
3687 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3688 retval = gdk_display_get_window_at_pointer (gdpy, NULL, NULL)
3689 != gtk_widget_get_window (f->output_data.x->edit_widget);
3691 else if (f
3692 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3693 || event->type == MotionNotify))
3695 /* If we are releasing or moving the scroll bar, it has the grab. */
3696 GtkWidget *w = gtk_grab_get_current ();
3697 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3700 return retval;
3705 /***********************************************************************
3706 Tool bar functions
3707 ***********************************************************************/
3708 /* The key for the data we put in the GtkImage widgets. The data is
3709 the image used by Emacs. We use this to see if we need to update
3710 the GtkImage with a new image. */
3711 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3713 /* The key for storing the latest modifiers so the activate callback can
3714 get them. */
3715 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3717 /* The key for storing the button widget in its proxy menu item. */
3718 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3720 /* The key for the data we put in the GtkImage widgets. The data is
3721 the stock name used by Emacs. We use this to see if we need to update
3722 the GtkImage with a new image. */
3723 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3725 /* As above, but this is used for named theme widgets, as opposed to
3726 stock items. */
3727 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3729 /* Callback function invoked when a tool bar item is pressed.
3730 W is the button widget in the tool bar that got pressed,
3731 CLIENT_DATA is an integer that is the index of the button in the
3732 tool bar. 0 is the first button. */
3734 static gboolean
3735 xg_tool_bar_button_cb (GtkWidget *widget,
3736 GdkEventButton *event,
3737 gpointer user_data)
3739 intptr_t state = event->state;
3740 gpointer ptr = (gpointer) state;
3741 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3742 return FALSE;
3746 /* Callback function invoked when a tool bar item is pressed.
3747 W is the button widget in the tool bar that got pressed,
3748 CLIENT_DATA is an integer that is the index of the button in the
3749 tool bar. 0 is the first button. */
3751 static void
3752 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3754 intptr_t idx = (intptr_t) client_data;
3755 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
3756 intptr_t mod = (intptr_t) gmod;
3758 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3759 Lisp_Object key, frame;
3760 struct input_event event;
3761 EVENT_INIT (event);
3763 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3764 return;
3766 idx *= TOOL_BAR_ITEM_NSLOTS;
3768 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3769 XSETFRAME (frame, f);
3771 /* We generate two events here. The first one is to set the prefix
3772 to `(tool_bar)', see keyboard.c. */
3773 event.kind = TOOL_BAR_EVENT;
3774 event.frame_or_window = frame;
3775 event.arg = frame;
3776 kbd_buffer_store_event (&event);
3778 event.kind = TOOL_BAR_EVENT;
3779 event.frame_or_window = frame;
3780 event.arg = key;
3781 /* Convert between the modifier bits GDK uses and the modifier bits
3782 Emacs uses. This assumes GDK and X masks are the same, which they are when
3783 this is written. */
3784 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3785 kbd_buffer_store_event (&event);
3787 /* Return focus to the frame after we have clicked on a detached
3788 tool bar button. */
3789 Fx_focus_frame (frame);
3792 /* Callback function invoked when a tool bar item is pressed in a detached
3793 tool bar or the overflow drop down menu.
3794 We just call xg_tool_bar_callback.
3795 W is the menu item widget that got pressed,
3796 CLIENT_DATA is an integer that is the index of the button in the
3797 tool bar. 0 is the first button. */
3799 static void
3800 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3802 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3803 XG_TOOL_BAR_PROXY_BUTTON));
3804 xg_tool_bar_callback (wbutton, client_data);
3808 static gboolean
3809 xg_tool_bar_help_callback (GtkWidget *w,
3810 GdkEventCrossing *event,
3811 gpointer client_data);
3813 /* This callback is called when a help is to be shown for an item in
3814 the detached tool bar when the detached tool bar it is not expanded. */
3816 static gboolean
3817 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3818 GdkEventCrossing *event,
3819 gpointer client_data)
3821 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3822 XG_TOOL_BAR_PROXY_BUTTON));
3824 return xg_tool_bar_help_callback (wbutton, event, client_data);
3827 static GtkWidget *
3828 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3830 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3831 GtkWidget *c1 = (GtkWidget *) clist->data;
3832 GtkWidget *c2 = clist->next ? (GtkWidget *) clist->next->data : NULL;
3834 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3835 g_list_free (clist);
3836 return GTK_IS_LABEL (c1) ? c1 : c2;
3840 /* This callback is called when a tool item should create a proxy item,
3841 such as for the overflow menu. Also called when the tool bar is detached.
3842 If we don't create a proxy menu item, the detached tool bar will be
3843 blank. */
3845 static gboolean
3846 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3848 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3849 GtkWidget *vb = XG_BIN_CHILD (wbutton);
3850 GtkWidget *c1;
3851 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3852 GtkImage *wimage = GTK_IMAGE (c1);
3853 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3854 (wlbl ? gtk_label_get_text (wlbl) : "");
3855 GtkWidget *wmenuimage;
3858 if (gtk_button_get_use_stock (wbutton))
3859 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3860 GTK_ICON_SIZE_MENU);
3861 else
3863 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3864 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3866 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
3868 if (store_type == GTK_IMAGE_STOCK)
3870 gchar *stock_id;
3871 gtk_image_get_stock (wimage, &stock_id, NULL);
3872 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3874 else if (store_type == GTK_IMAGE_ICON_SET)
3876 GtkIconSet *icon_set;
3877 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3878 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3879 GTK_ICON_SIZE_MENU);
3881 else if (store_type == GTK_IMAGE_PIXBUF)
3883 gint width, height;
3885 if (settings &&
3886 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3887 &width, &height))
3889 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3891 src_pixbuf = gtk_image_get_pixbuf (wimage);
3892 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3893 GDK_INTERP_BILINEAR);
3895 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3897 else
3899 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3900 abort ();
3903 else if (store_type == GTK_IMAGE_ICON_NAME)
3905 const gchar *icon_name;
3906 GtkIconSize icon_size;
3908 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3909 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3910 GTK_ICON_SIZE_MENU);
3912 else
3914 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3915 abort ();
3918 if (wmenuimage)
3919 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3921 g_signal_connect (G_OBJECT (wmenuitem),
3922 "activate",
3923 G_CALLBACK (xg_tool_bar_proxy_callback),
3924 user_data);
3927 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3928 (gpointer) wbutton);
3929 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3930 gtk_widget_set_sensitive (wmenuitem,
3931 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
3933 /* Use enter/leave notify to show help. We use the events
3934 rather than the GtkButton specific signals "enter" and
3935 "leave", so we can have only one callback. The event
3936 will tell us what kind of event it is. */
3937 g_signal_connect (G_OBJECT (wmenuitem),
3938 "enter-notify-event",
3939 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3940 user_data);
3941 g_signal_connect (G_OBJECT (wmenuitem),
3942 "leave-notify-event",
3943 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3944 user_data);
3946 return TRUE;
3949 /* This callback is called when a tool bar is detached. We must set
3950 the height of the tool bar to zero when this happens so frame sizes
3951 are correctly calculated.
3952 WBOX is the handle box widget that enables detach/attach of the tool bar.
3953 W is the tool bar widget.
3954 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3956 static void
3957 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
3958 GtkWidget *w,
3959 gpointer client_data)
3961 FRAME_PTR f = (FRAME_PTR) client_data;
3963 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3964 NULL);
3966 if (f)
3968 GtkRequisition req, req2;
3969 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3970 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
3971 gtk_widget_get_preferred_size (w, NULL, &req2);
3972 req.width -= req2.width;
3973 req.height -= req2.height;
3974 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3975 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3976 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3977 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3978 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3979 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3980 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3981 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3982 xg_height_or_width_changed (f);
3986 /* This callback is called when a tool bar is reattached. We must set
3987 the height of the tool bar when this happens so frame sizes
3988 are correctly calculated.
3989 WBOX is the handle box widget that enables detach/attach of the tool bar.
3990 W is the tool bar widget.
3991 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3993 static void
3994 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
3995 GtkWidget *w,
3996 gpointer client_data)
3998 FRAME_PTR f = (FRAME_PTR) client_data;
3999 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
4001 if (f)
4003 GtkRequisition req, req2;
4004 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
4005 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4006 gtk_widget_get_preferred_size (w, NULL, &req2);
4007 req.width += req2.width;
4008 req.height += req2.height;
4009 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4010 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4011 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4012 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4013 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4014 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4015 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4016 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4017 xg_height_or_width_changed (f);
4021 /* This callback is called when the mouse enters or leaves a tool bar item.
4022 It is used for displaying and hiding the help text.
4023 W is the tool bar item, a button.
4024 EVENT is either an enter event or leave event.
4025 CLIENT_DATA is an integer that is the index of the button in the
4026 tool bar. 0 is the first button.
4028 Returns FALSE to tell GTK to keep processing this event. */
4030 static gboolean
4031 xg_tool_bar_help_callback (GtkWidget *w,
4032 GdkEventCrossing *event,
4033 gpointer client_data)
4035 intptr_t idx = (intptr_t) client_data;
4036 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4037 Lisp_Object help, frame;
4039 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4040 return FALSE;
4042 if (event->type == GDK_ENTER_NOTIFY)
4044 idx *= TOOL_BAR_ITEM_NSLOTS;
4045 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4047 if (NILP (help))
4048 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4050 else
4051 help = Qnil;
4053 XSETFRAME (frame, f);
4054 kbd_buffer_store_help_event (frame, help);
4056 return FALSE;
4060 /* This callback is called when a tool bar item shall be redrawn.
4061 It modifies the expose event so that the GtkImage widget redraws the
4062 whole image. This to overcome a bug that makes GtkImage draw the image
4063 in the wrong place when it tries to redraw just a part of the image.
4064 W is the GtkImage to be redrawn.
4065 EVENT is the expose event for W.
4066 CLIENT_DATA is unused.
4068 Returns FALSE to tell GTK to keep processing this event. */
4070 #ifndef HAVE_GTK3
4071 static gboolean
4072 xg_tool_bar_item_expose_callback (GtkWidget *w,
4073 GdkEventExpose *event,
4074 gpointer client_data)
4076 gint width, height;
4078 gdk_drawable_get_size (event->window, &width, &height);
4079 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4080 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4082 event->area.x = max (0, event->area.x);
4083 event->area.y = max (0, event->area.y);
4085 event->area.width = max (width, event->area.width);
4086 event->area.height = max (height, event->area.height);
4088 return FALSE;
4090 #endif
4092 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4093 #define toolbar_set_orientation(w, o) \
4094 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4095 #else
4096 #define toolbar_set_orientation(w, o) \
4097 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4098 #endif
4100 /* Attach a tool bar to frame F. */
4102 static void
4103 xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
4105 struct x_output *x = f->output_data.x;
4106 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4108 toolbar_set_orientation (x->toolbar_widget,
4109 into_hbox
4110 ? GTK_ORIENTATION_VERTICAL
4111 : GTK_ORIENTATION_HORIZONTAL);
4112 if (!x->handlebox_widget)
4114 x->handlebox_widget = gtk_handle_box_new ();
4115 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4116 G_CALLBACK (xg_tool_bar_detach_callback), f);
4117 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4118 G_CALLBACK (xg_tool_bar_attach_callback), f);
4119 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4120 x->toolbar_widget);
4123 if (into_hbox)
4125 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4126 GTK_POS_TOP);
4127 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
4128 FALSE, FALSE, 0);
4130 if (EQ (pos, Qleft))
4131 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4132 x->handlebox_widget,
4134 x->toolbar_in_hbox = 1;
4136 else
4138 int vbox_pos = x->menubar_widget ? 1 : 0;
4139 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4140 GTK_POS_LEFT);
4141 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
4142 FALSE, FALSE, 0);
4144 if (EQ (pos, Qtop))
4145 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4146 x->handlebox_widget,
4147 vbox_pos);
4148 x->toolbar_in_hbox = 0;
4152 /* Create a tool bar for frame F. */
4154 static void
4155 xg_create_tool_bar (FRAME_PTR f)
4157 struct x_output *x = f->output_data.x;
4159 x->toolbar_widget = gtk_toolbar_new ();
4160 x->toolbar_detached = 0;
4162 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4164 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4165 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4169 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4171 /* Find the right-to-left image named by RTL in the tool bar images for F.
4172 Returns IMAGE if RTL is not found. */
4174 static Lisp_Object
4175 find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
4177 int i;
4178 Lisp_Object file, rtl_name;
4179 struct gcpro gcpro1, gcpro2;
4180 GCPRO2 (file, rtl_name);
4182 rtl_name = Ffile_name_nondirectory (rtl);
4184 for (i = 0; i < f->n_tool_bar_items; ++i)
4186 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4187 if (!NILP (file = file_for_image (rtl_image)))
4189 file = call1 (intern ("file-name-sans-extension"),
4190 Ffile_name_nondirectory (file));
4191 if (EQ (Fequal (file, rtl_name), Qt))
4193 image = rtl_image;
4194 break;
4199 return image;
4202 static GtkToolItem *
4203 xg_make_tool_item (FRAME_PTR f,
4204 GtkWidget *wimage,
4205 GtkWidget **wbutton,
4206 const char *label,
4207 int i, int horiz, int text_image)
4209 GtkToolItem *ti = gtk_tool_item_new ();
4210 GtkWidget *vb = horiz ? gtk_hbox_new (FALSE, 0) : gtk_vbox_new (FALSE, 0);
4211 GtkWidget *wb = gtk_button_new ();
4212 GtkWidget *weventbox = gtk_event_box_new ();
4214 if (wimage && !text_image)
4215 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4216 if (label)
4217 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4218 if (wimage && text_image)
4219 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4221 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4222 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4223 gtk_container_add (GTK_CONTAINER (wb), vb);
4224 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4225 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4227 if (wimage)
4229 intptr_t ii = i;
4230 gpointer gi = (gpointer) ii;
4232 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4233 G_CALLBACK (xg_tool_bar_menu_proxy),
4234 gi);
4236 g_signal_connect (G_OBJECT (wb), "clicked",
4237 G_CALLBACK (xg_tool_bar_callback),
4238 gi);
4240 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4242 #ifndef HAVE_GTK3
4243 /* Catch expose events to overcome an annoying redraw bug, see
4244 comment for xg_tool_bar_item_expose_callback. */
4245 g_signal_connect (G_OBJECT (ti),
4246 "expose-event",
4247 G_CALLBACK (xg_tool_bar_item_expose_callback),
4249 #endif
4250 gtk_tool_item_set_homogeneous (ti, FALSE);
4252 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4253 no distinction based on modifiers in the activate callback,
4254 so we have to do it ourselves. */
4255 g_signal_connect (wb, "button-release-event",
4256 G_CALLBACK (xg_tool_bar_button_cb),
4257 NULL);
4259 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4261 /* Use enter/leave notify to show help. We use the events
4262 rather than the GtkButton specific signals "enter" and
4263 "leave", so we can have only one callback. The event
4264 will tell us what kind of event it is. */
4265 /* The EMACS_INT cast avoids a warning. */
4266 g_signal_connect (G_OBJECT (weventbox),
4267 "enter-notify-event",
4268 G_CALLBACK (xg_tool_bar_help_callback),
4269 gi);
4270 g_signal_connect (G_OBJECT (weventbox),
4271 "leave-notify-event",
4272 G_CALLBACK (xg_tool_bar_help_callback),
4273 gi);
4276 if (wbutton) *wbutton = wb;
4278 return ti;
4281 static int
4282 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4283 const char *icon_name, const struct image *img,
4284 const char *label, int horiz)
4286 gpointer old;
4287 GtkWidget *wimage;
4288 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4289 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4291 /* Check if the tool icon matches. */
4292 if (stock_name)
4294 old = g_object_get_data (G_OBJECT (wimage),
4295 XG_TOOL_BAR_STOCK_NAME);
4296 if (!old || strcmp (old, stock_name))
4297 return 1;
4299 else if (icon_name)
4301 old = g_object_get_data (G_OBJECT (wimage),
4302 XG_TOOL_BAR_ICON_NAME);
4303 if (!old || strcmp (old, icon_name))
4304 return 1;
4306 else
4308 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4309 XG_TOOL_BAR_IMAGE_DATA);
4310 Pixmap old_img = (Pixmap) gold_img;
4311 if (old_img != img->pixmap)
4312 return 1;
4315 /* Check button configuration and label. */
4316 if ((horiz ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb))
4317 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4318 return 1;
4320 /* Ensure label is correct. */
4321 if (label)
4322 gtk_label_set_text (GTK_LABEL (wlbl), label);
4323 return 0;
4326 static int
4327 xg_update_tool_bar_sizes (FRAME_PTR f)
4329 struct x_output *x = f->output_data.x;
4330 GtkRequisition req;
4331 int nl = 0, nr = 0, nt = 0, nb = 0;
4333 gtk_widget_get_preferred_size (GTK_WIDGET (x->handlebox_widget), NULL, &req);
4334 if (x->toolbar_in_hbox)
4336 int pos;
4337 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4338 x->handlebox_widget,
4339 "position", &pos, NULL);
4340 if (pos == 0) nl = req.width;
4341 else nr = req.width;
4343 else
4345 int pos;
4346 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4347 x->handlebox_widget,
4348 "position", &pos, NULL);
4349 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4350 else nb = req.height;
4353 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4354 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4355 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4356 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4358 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4359 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4360 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4361 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4362 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4363 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4364 return 1;
4367 return 0;
4371 /* Update the tool bar for frame F. Add new buttons and remove old. */
4373 void
4374 update_frame_tool_bar (FRAME_PTR f)
4376 int i, j;
4377 struct x_output *x = f->output_data.x;
4378 int hmargin = 0, vmargin = 0;
4379 GtkToolbar *wtoolbar;
4380 GtkToolItem *ti;
4381 GtkTextDirection dir;
4382 int pack_tool_bar = x->handlebox_widget == NULL;
4383 Lisp_Object style;
4384 int text_image, horiz;
4386 if (! FRAME_GTK_WIDGET (f))
4387 return;
4389 BLOCK_INPUT;
4391 if (INTEGERP (Vtool_bar_button_margin)
4392 && XINT (Vtool_bar_button_margin) > 0)
4394 hmargin = XFASTINT (Vtool_bar_button_margin);
4395 vmargin = XFASTINT (Vtool_bar_button_margin);
4397 else if (CONSP (Vtool_bar_button_margin))
4399 if (INTEGERP (XCAR (Vtool_bar_button_margin))
4400 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
4401 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4403 if (INTEGERP (XCDR (Vtool_bar_button_margin))
4404 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4405 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4408 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4409 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4410 i.e. zero. This means that margins less than
4411 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4412 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4413 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4415 if (! x->toolbar_widget)
4416 xg_create_tool_bar (f);
4418 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4419 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4421 style = Ftool_bar_get_system_style ();
4422 text_image = EQ (style, Qtext_image_horiz);
4423 horiz = EQ (style, Qboth_horiz) || text_image;
4425 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4427 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4428 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4429 int idx;
4430 int img_id;
4431 int icon_size = 0;
4432 struct image *img = NULL;
4433 Lisp_Object image;
4434 Lisp_Object stock = Qnil;
4435 GtkStockItem stock_item;
4436 char *stock_name = NULL;
4437 char *icon_name = NULL;
4438 Lisp_Object rtl;
4439 GtkWidget *wbutton = NULL;
4440 Lisp_Object specified_file;
4441 int vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4442 const char *label
4443 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4444 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4445 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4446 : "";
4448 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4450 /* If this is a separator, use a gtk separator item. */
4451 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4453 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4455 if (ti)
4456 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4457 GTK_WIDGET (ti));
4458 ti = gtk_separator_tool_item_new ();
4459 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4461 j++;
4462 continue;
4465 /* Otherwise, the tool-bar item is an ordinary button. */
4467 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4469 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4470 ti = NULL;
4473 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4475 /* Ignore invalid image specifications. */
4476 image = PROP (TOOL_BAR_ITEM_IMAGES);
4477 if (!valid_image_p (image))
4479 if (ti)
4480 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4481 GTK_WIDGET (ti));
4482 continue;
4485 specified_file = file_for_image (image);
4486 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4487 stock = call1 (Qx_gtk_map_stock, specified_file);
4489 if (STRINGP (stock))
4491 stock_name = SSDATA (stock);
4492 if (stock_name[0] == 'n' && stock_name[1] == ':')
4494 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4495 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4497 icon_name = stock_name + 2;
4498 stock_name = NULL;
4499 stock = Qnil;
4501 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4502 icon_name = NULL;
4503 else
4504 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4506 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4507 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4508 else
4510 stock = Qnil;
4511 stock_name = NULL;
4515 if (stock_name == NULL && icon_name == NULL)
4517 /* No stock image, or stock item not known. Try regular
4518 image. If image is a vector, choose it according to the
4519 button state. */
4520 if (dir == GTK_TEXT_DIR_RTL
4521 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4522 && STRINGP (rtl))
4523 image = find_rtl_image (f, image, rtl);
4525 if (VECTORP (image))
4527 if (enabled_p)
4528 idx = (selected_p
4529 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4530 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4531 else
4532 idx = (selected_p
4533 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4534 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4536 xassert (ASIZE (image) >= idx);
4537 image = AREF (image, idx);
4539 else
4540 idx = -1;
4542 img_id = lookup_image (f, image);
4543 img = IMAGE_FROM_ID (f, img_id);
4544 prepare_image_for_display (f, img);
4546 if (img->load_failed_p || img->pixmap == None)
4548 if (ti)
4549 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4550 GTK_WIDGET (ti));
4551 continue;
4555 /* If there is an existing widget, check if it's stale; if so,
4556 remove it and make a new tool item from scratch. */
4557 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4558 img, label, horiz))
4560 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4561 GTK_WIDGET (ti));
4562 ti = NULL;
4565 if (ti == NULL)
4567 GtkWidget *w;
4569 /* Save the image so we can see if an update is needed the
4570 next time we call xg_tool_item_match_p. */
4571 if (EQ (style, Qtext))
4572 w = NULL;
4573 else if (stock_name)
4575 w = gtk_image_new_from_stock (stock_name, icon_size);
4576 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4577 (gpointer) xstrdup (stock_name),
4578 (GDestroyNotify) xfree);
4580 else if (icon_name)
4582 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4583 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4584 (gpointer) xstrdup (icon_name),
4585 (GDestroyNotify) xfree);
4587 else
4589 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4590 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4591 (gpointer)img->pixmap);
4594 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4595 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4596 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4599 #undef PROP
4601 gtk_widget_set_sensitive (wbutton, enabled_p);
4602 j++;
4605 /* Remove buttons not longer needed. */
4608 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4609 if (ti)
4610 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4611 } while (ti != NULL);
4613 if (f->n_tool_bar_items != 0)
4615 if (pack_tool_bar)
4616 xg_pack_tool_bar (f, f->tool_bar_position);
4617 gtk_widget_show_all (GTK_WIDGET (x->handlebox_widget));
4618 if (xg_update_tool_bar_sizes (f))
4619 xg_height_or_width_changed (f);
4622 UNBLOCK_INPUT;
4625 /* Deallocate all resources for the tool bar on frame F.
4626 Remove the tool bar. */
4628 void
4629 free_frame_tool_bar (FRAME_PTR f)
4631 struct x_output *x = f->output_data.x;
4633 if (x->toolbar_widget)
4635 int is_packed = x->handlebox_widget != 0;
4636 BLOCK_INPUT;
4637 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4638 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4639 if (is_packed)
4641 if (x->toolbar_in_hbox)
4642 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4643 x->handlebox_widget);
4644 else
4645 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4646 x->handlebox_widget);
4648 else
4649 gtk_widget_destroy (x->toolbar_widget);
4651 x->toolbar_widget = 0;
4652 x->handlebox_widget = 0;
4653 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4654 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4656 xg_height_or_width_changed (f);
4658 UNBLOCK_INPUT;
4663 xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4665 struct x_output *x = f->output_data.x;
4667 if (! x->toolbar_widget || ! x->handlebox_widget)
4668 return 1;
4670 BLOCK_INPUT;
4671 g_object_ref (x->handlebox_widget);
4672 if (x->toolbar_in_hbox)
4673 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4674 x->handlebox_widget);
4675 else
4676 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4677 x->handlebox_widget);
4678 xg_pack_tool_bar (f, pos);
4679 g_object_unref (x->handlebox_widget);
4680 if (xg_update_tool_bar_sizes (f))
4681 xg_height_or_width_changed (f);
4683 UNBLOCK_INPUT;
4684 return 1;
4689 /***********************************************************************
4690 Initializing
4691 ***********************************************************************/
4692 void
4693 xg_initialize (void)
4695 GtkBindingSet *binding_set;
4697 #if HAVE_XFT
4698 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4699 we keep it permanently linked in. */
4700 XftInit (0);
4701 #endif
4703 gdpy_def = NULL;
4704 xg_ignore_gtk_scrollbar = 0;
4705 xg_detached_menus = 0;
4706 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4707 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4709 id_to_widget.max_size = id_to_widget.used = 0;
4710 id_to_widget.widgets = 0;
4712 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4713 bindings. It doesn't seem to be any way to remove properties,
4714 so we set it to VoidSymbol which in X means "no key". */
4715 gtk_settings_set_string_property (gtk_settings_get_default (),
4716 "gtk-menu-bar-accel",
4717 "VoidSymbol",
4718 EMACS_CLASS);
4720 /* Make GTK text input widgets use Emacs style keybindings. This is
4721 Emacs after all. */
4722 gtk_settings_set_string_property (gtk_settings_get_default (),
4723 "gtk-key-theme-name",
4724 "Emacs",
4725 EMACS_CLASS);
4727 /* Make dialogs close on C-g. Since file dialog inherits from
4728 dialog, this works for them also. */
4729 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4730 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4731 "close", 0);
4733 /* Make menus close on C-g. */
4734 binding_set = gtk_binding_set_by_class (g_type_class_ref
4735 (GTK_TYPE_MENU_SHELL));
4736 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4737 "cancel", 0);
4738 update_theme_scrollbar_width ();
4741 #endif /* USE_GTK */