]> code.delx.au - gnu-emacs/blob - src/gtkutil.c
Merge from origin/emacs-24
[gnu-emacs] / src / gtkutil.c
1 /* Functions for creating and updating GTK widgets.
2
3 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21
22 #ifdef USE_GTK
23 #include <float.h>
24 #include <stdio.h>
25
26 #include <c-ctype.h>
27
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "buffer.h"
34 #include "gtkutil.h"
35 #include "termhooks.h"
36 #include "keyboard.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include "font.h"
40
41 #include <gdk/gdkkeysyms.h>
42 #include "xsettings.h"
43
44 #ifdef HAVE_XFT
45 #include <X11/Xft/Xft.h>
46 #endif
47
48 #ifdef HAVE_GTK3
49 #include <gtk/gtkx.h>
50 #include "emacsgtkfixed.h"
51 #endif
52
53 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
54 #define gtk_widget_set_has_window(w, b) \
55 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
56 #endif
57 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
58 #define gtk_dialog_get_action_area(w) ((w)->action_area)
59 #define gtk_dialog_get_content_area(w) ((w)->vbox)
60 #endif
61 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
62 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
63 #endif
64 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
65 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
66 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
67 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
68 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
69 #endif
70 #if GTK_CHECK_VERSION (2, 12, 0)
71 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
72 #else
73 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
74 #endif
75
76 #if ! GTK_CHECK_VERSION (2, 14, 0)
77 #define gtk_adjustment_configure(adj, xvalue, xlower, \
78 xupper, xstep_increment, \
79 xpage_increment, xpagesize) \
80 do { \
81 adj->lower = xlower; \
82 adj->upper = xupper; \
83 adj->page_size = xpagesize; \
84 gtk_adjustment_set_value (adj, xvalue); \
85 adj->page_increment = xpage_increment; \
86 adj->step_increment = xstep_increment; \
87 } while (0)
88 #endif /* < Gtk+ 2.14 */
89
90 #ifdef HAVE_FREETYPE
91 #if GTK_CHECK_VERSION (3, 2, 0)
92 #define USE_NEW_GTK_FONT_CHOOSER 1
93 #else
94 #define USE_NEW_GTK_FONT_CHOOSER 0
95 #define gtk_font_chooser_dialog_new(x, y) \
96 gtk_font_selection_dialog_new (x)
97 #undef GTK_FONT_CHOOSER
98 #define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
99 #define gtk_font_chooser_set_font(x, y) \
100 gtk_font_selection_dialog_set_font_name (x, y)
101 #endif
102 #endif /* HAVE_FREETYPE */
103
104 #if GTK_CHECK_VERSION (3, 10, 0)
105 #define XG_TEXT_CANCEL "Cancel"
106 #define XG_TEXT_OK "OK"
107 #define XG_TEXT_OPEN "Open"
108 #else
109 #define XG_TEXT_CANCEL GTK_STOCK_CANCEL
110 #define XG_TEXT_OK GTK_STOCK_OK
111 #define XG_TEXT_OPEN GTK_STOCK_OPEN
112 #endif
113
114 #ifndef HAVE_GTK3
115 #ifdef USE_GTK_TOOLTIP
116 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
117 #endif
118 #define gdk_window_get_geometry(w, a, b, c, d) \
119 gdk_window_get_geometry (w, a, b, c, d, 0)
120 #define gdk_x11_window_lookup_for_display(d, w) \
121 gdk_xid_table_lookup_for_display (d, w)
122 #define gtk_box_new(ori, spacing) \
123 ((ori) == GTK_ORIENTATION_HORIZONTAL \
124 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
125 #define gtk_scrollbar_new(ori, spacing) \
126 ((ori) == GTK_ORIENTATION_HORIZONTAL \
127 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
128 #ifndef GDK_KEY_g
129 #define GDK_KEY_g GDK_g
130 #endif
131 #endif /* HAVE_GTK3 */
132
133 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
134
135 static void update_theme_scrollbar_width (void);
136 static void update_theme_scrollbar_height (void);
137
138 #define TB_INFO_KEY "xg_frame_tb_info"
139 struct xg_frame_tb_info
140 {
141 Lisp_Object last_tool_bar;
142 Lisp_Object style;
143 int n_last_items;
144 int hmargin, vmargin;
145 GtkTextDirection dir;
146 };
147
148 \f
149 /***********************************************************************
150 Display handling functions
151 ***********************************************************************/
152
153 /* Keep track of the default display, or NULL if there is none. Emacs
154 may close all its displays. */
155
156 static GdkDisplay *gdpy_def;
157
158 /* When the GTK widget W is to be created on a display for F that
159 is not the default display, set the display for W.
160 W can be a GtkMenu or a GtkWindow widget. */
161
162 static void
163 xg_set_screen (GtkWidget *w, struct frame *f)
164 {
165 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
166 {
167 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
168 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
169
170 if (GTK_IS_MENU (w))
171 gtk_menu_set_screen (GTK_MENU (w), gscreen);
172 else
173 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
174 }
175 }
176
177
178 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
179 *DPY is set to NULL if the display can't be opened.
180
181 Returns non-zero if display could be opened, zero if display could not
182 be opened, and less than zero if the GTK version doesn't support
183 multiple displays. */
184
185 void
186 xg_display_open (char *display_name, Display **dpy)
187 {
188 GdkDisplay *gdpy;
189
190 gdpy = gdk_display_open (display_name);
191 if (!gdpy_def && gdpy)
192 {
193 gdpy_def = gdpy;
194 gdk_display_manager_set_default_display (gdk_display_manager_get (),
195 gdpy);
196 }
197
198 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
199 }
200
201
202 /* Close display DPY. */
203
204 void
205 xg_display_close (Display *dpy)
206 {
207 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
208
209 /* If this is the default display, try to change it before closing.
210 If there is no other display to use, gdpy_def is set to NULL, and
211 the next call to xg_display_open resets the default display. */
212 if (gdk_display_get_default () == gdpy)
213 {
214 struct x_display_info *dpyinfo;
215 GdkDisplay *gdpy_new = NULL;
216
217 /* Find another display. */
218 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
219 if (dpyinfo->display != dpy)
220 {
221 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
222 gdk_display_manager_set_default_display (gdk_display_manager_get (),
223 gdpy_new);
224 break;
225 }
226 gdpy_def = gdpy_new;
227 }
228
229 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
230 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
231 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
232 can continue running, but there will be memory leaks. */
233 g_object_run_dispose (G_OBJECT (gdpy));
234 #else
235 /* This seems to be fixed in GTK 2.10. */
236 gdk_display_close (gdpy);
237 #endif
238 }
239
240 \f
241 /***********************************************************************
242 Utility functions
243 ***********************************************************************/
244
245 /* Create and return the cursor to be used for popup menus and
246 scroll bars on display DPY. */
247
248 GdkCursor *
249 xg_create_default_cursor (Display *dpy)
250 {
251 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
252 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
253 }
254
255 static GdkPixbuf *
256 xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
257 {
258 int iunused;
259 GdkPixbuf *tmp_buf;
260 Window wunused;
261 unsigned int width, height, uunused;
262 XImage *xim;
263
264 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
265 &width, &height, &uunused, &uunused);
266
267 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
268 ~0, XYPixmap);
269 if (!xim) return 0;
270
271 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
272 GDK_COLORSPACE_RGB,
273 FALSE,
274 xim->bitmap_unit,
275 width,
276 height,
277 xim->bytes_per_line,
278 NULL,
279 NULL);
280 XDestroyImage (xim);
281 return tmp_buf;
282 }
283
284 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
285
286 static GdkPixbuf *
287 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
288 Pixmap pix,
289 Pixmap mask)
290 {
291 int width, height;
292 GdkPixbuf *icon_buf, *tmp_buf;
293
294 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
295 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
296 g_object_unref (G_OBJECT (tmp_buf));
297
298 width = gdk_pixbuf_get_width (icon_buf);
299 height = gdk_pixbuf_get_height (icon_buf);
300
301 if (mask)
302 {
303 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
304 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
305 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
306 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
307 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
308 int y;
309
310 for (y = 0; y < height; ++y)
311 {
312 guchar *iconptr, *maskptr;
313 int x;
314
315 iconptr = pixels + y * rowstride;
316 maskptr = mask_pixels + y * mask_rowstride;
317
318 for (x = 0; x < width; ++x)
319 {
320 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
321 just R is sufficient. */
322 if (maskptr[0] == 0)
323 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
324
325 iconptr += rowstride/width;
326 maskptr += mask_rowstride/width;
327 }
328 }
329
330 g_object_unref (G_OBJECT (mask_buf));
331 }
332
333 return icon_buf;
334 }
335
336 static Lisp_Object
337 file_for_image (Lisp_Object image)
338 {
339 Lisp_Object specified_file = Qnil;
340 Lisp_Object tail;
341
342 for (tail = XCDR (image);
343 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
344 tail = XCDR (XCDR (tail)))
345 if (EQ (XCAR (tail), QCfile))
346 specified_file = XCAR (XCDR (tail));
347
348 return specified_file;
349 }
350
351 /* For the image defined in IMG, make and return a GtkImage. For displays with
352 8 planes or less we must make a GdkPixbuf and apply the mask manually.
353 Otherwise the highlighting and dimming the tool bar code in GTK does
354 will look bad. For display with more than 8 planes we just use the
355 pixmap and mask directly. For monochrome displays, GTK doesn't seem
356 able to use external pixmaps, it looks bad whatever we do.
357 The image is defined on the display where frame F is.
358 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
359 If OLD_WIDGET is NULL, a new widget is constructed and returned.
360 If OLD_WIDGET is not NULL, that widget is modified. */
361
362 static GtkWidget *
363 xg_get_image_for_pixmap (struct frame *f,
364 struct image *img,
365 GtkWidget *widget,
366 GtkImage *old_widget)
367 {
368 GdkPixbuf *icon_buf;
369
370 /* If we have a file, let GTK do all the image handling.
371 This seems to be the only way to make insensitive and activated icons
372 look good in all cases. */
373 Lisp_Object specified_file = file_for_image (img->spec);
374 Lisp_Object file;
375
376 /* We already loaded the image once before calling this
377 function, so this only fails if the image file has been removed.
378 In that case, use the pixmap already loaded. */
379
380 if (STRINGP (specified_file)
381 && STRINGP (file = x_find_image_file (specified_file)))
382 {
383 if (! old_widget)
384 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
385 else
386 gtk_image_set_from_file (old_widget, SSDATA (file));
387
388 return GTK_WIDGET (old_widget);
389 }
390
391 /* No file, do the image handling ourselves. This will look very bad
392 on a monochrome display, and sometimes bad on all displays with
393 certain themes. */
394
395 /* This is a workaround to make icons look good on pseudo color
396 displays. Apparently GTK expects the images to have an alpha
397 channel. If they don't, insensitive and activated icons will
398 look bad. This workaround does not work on monochrome displays,
399 and is strictly not needed on true color/static color displays (i.e.
400 16 bits and higher). But we do it anyway so we get a pixbuf that is
401 not associated with the img->pixmap. The img->pixmap may be removed
402 by clearing the image cache and then the tool bar redraw fails, since
403 Gtk+ assumes the pixmap is always there. */
404 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
405
406 if (icon_buf)
407 {
408 if (! old_widget)
409 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
410 else
411 gtk_image_set_from_pixbuf (old_widget, icon_buf);
412
413 g_object_unref (G_OBJECT (icon_buf));
414 }
415
416 return GTK_WIDGET (old_widget);
417 }
418
419
420 /* Set CURSOR on W and all widgets W contain. We must do like this
421 for scroll bars and menu because they create widgets internally,
422 and it is those widgets that are visible. */
423
424 static void
425 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
426 {
427 GdkWindow *window = gtk_widget_get_window (w);
428 GList *children = gdk_window_peek_children (window);
429
430 gdk_window_set_cursor (window, cursor);
431
432 /* The scroll bar widget has more than one GDK window (had to look at
433 the source to figure this out), and there is no way to set cursor
434 on widgets in GTK. So we must set the cursor for all GDK windows.
435 Ditto for menus. */
436
437 for ( ; children; children = g_list_next (children))
438 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
439 }
440
441 /* Insert NODE into linked LIST. */
442
443 static void
444 xg_list_insert (xg_list_node *list, xg_list_node *node)
445 {
446 xg_list_node *list_start = list->next;
447
448 if (list_start) list_start->prev = node;
449 node->next = list_start;
450 node->prev = 0;
451 list->next = node;
452 }
453
454 /* Remove NODE from linked LIST. */
455
456 static void
457 xg_list_remove (xg_list_node *list, xg_list_node *node)
458 {
459 xg_list_node *list_start = list->next;
460 if (node == list_start)
461 {
462 list->next = node->next;
463 if (list->next) list->next->prev = 0;
464 }
465 else
466 {
467 node->prev->next = node->next;
468 if (node->next) node->next->prev = node->prev;
469 }
470 }
471
472 /* Allocate and return a utf8 version of STR. If STR is already
473 utf8 or NULL, just return a copy of STR.
474 A new string is allocated and the caller must free the result
475 with g_free. */
476
477 static char *
478 get_utf8_string (const char *str)
479 {
480 char *utf8_str;
481
482 if (!str) return NULL;
483
484 /* If not UTF-8, try current locale. */
485 if (!g_utf8_validate (str, -1, NULL))
486 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
487 else
488 return g_strdup (str);
489
490 if (!utf8_str)
491 {
492 /* Probably some control characters in str. Escape them. */
493 ptrdiff_t len;
494 ptrdiff_t nr_bad = 0;
495 gsize bytes_read;
496 gsize bytes_written;
497 unsigned char *p = (unsigned char *)str;
498 char *cp, *up;
499 GError *err = NULL;
500
501 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
502 &bytes_written, &err))
503 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
504 {
505 ++nr_bad;
506 p += bytes_written+1;
507 g_error_free (err);
508 err = NULL;
509 }
510
511 if (err)
512 {
513 g_error_free (err);
514 err = NULL;
515 }
516 if (cp) g_free (cp);
517
518 len = strlen (str);
519 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
520 memory_full (SIZE_MAX);
521 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
522 p = (unsigned char *)str;
523
524 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
525 &bytes_written, &err))
526 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
527 {
528 memcpy (up, p, bytes_written);
529 up += bytes_written;
530 up += sprintf (up, "\\%03o", p[bytes_written]);
531 p += bytes_written + 1;
532 g_error_free (err);
533 err = NULL;
534 }
535
536 if (cp)
537 {
538 strcpy (up, cp);
539 g_free (cp);
540 }
541 if (err)
542 {
543 g_error_free (err);
544 err = NULL;
545 }
546 }
547 return utf8_str;
548 }
549
550 /* Check for special colors used in face spec for region face.
551 The colors are fetched from the Gtk+ theme.
552 Return true if color was found, false if not. */
553
554 bool
555 xg_check_special_colors (struct frame *f,
556 const char *color_name,
557 XColor *color)
558 {
559 bool success_p = 0;
560 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
561 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
562
563 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
564 return success_p;
565
566 block_input ();
567 {
568 #ifdef HAVE_GTK3
569 GtkStyleContext *gsty
570 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
571 GdkRGBA col;
572 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
573 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
574 if (get_fg)
575 gtk_style_context_get_color (gsty, state, &col);
576 else
577 gtk_style_context_get_background_color (gsty, state, &col);
578
579 sprintf (buf, "rgb:%04x/%04x/%04x",
580 (int)(col.red * 65535),
581 (int)(col.green * 65535),
582 (int)(col.blue * 65535));
583 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
584 buf, color)
585 != 0);
586 #else
587 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
588 GdkColor *grgb = get_bg
589 ? &gsty->bg[GTK_STATE_SELECTED]
590 : &gsty->fg[GTK_STATE_SELECTED];
591
592 color->red = grgb->red;
593 color->green = grgb->green;
594 color->blue = grgb->blue;
595 color->pixel = grgb->pixel;
596 success_p = 1;
597 #endif
598
599 }
600 unblock_input ();
601 return success_p;
602 }
603
604
605 \f
606 /***********************************************************************
607 Tooltips
608 ***********************************************************************/
609 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
610 We use that to pop down the tooltip. This happens if Gtk+ for some
611 reason wants to change or hide the tooltip. */
612
613 #ifdef USE_GTK_TOOLTIP
614
615 static void
616 hierarchy_ch_cb (GtkWidget *widget,
617 GtkWidget *previous_toplevel,
618 gpointer user_data)
619 {
620 struct frame *f = user_data;
621 struct x_output *x = f->output_data.x;
622 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
623
624 if (! top || ! GTK_IS_WINDOW (top))
625 gtk_widget_hide (previous_toplevel);
626 }
627
628 /* Callback called when Gtk+ thinks a tooltip should be displayed.
629 We use it to get the tooltip window and the tooltip widget so
630 we can manipulate the ourselves.
631
632 Return FALSE ensures that the tooltip is not shown. */
633
634 static gboolean
635 qttip_cb (GtkWidget *widget,
636 gint xpos,
637 gint ypos,
638 gboolean keyboard_mode,
639 GtkTooltip *tooltip,
640 gpointer user_data)
641 {
642 struct frame *f = user_data;
643 struct x_output *x = f->output_data.x;
644 if (x->ttip_widget == NULL)
645 {
646 GtkWidget *p;
647 GList *list, *iter;
648
649 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
650 x->ttip_widget = tooltip;
651 g_object_ref (G_OBJECT (tooltip));
652 x->ttip_lbl = gtk_label_new ("");
653 g_object_ref (G_OBJECT (x->ttip_lbl));
654 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
655 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
656
657 /* Change stupid Gtk+ default line wrapping. */
658 p = gtk_widget_get_parent (x->ttip_lbl);
659 list = gtk_container_get_children (GTK_CONTAINER (p));
660 for (iter = list; iter; iter = g_list_next (iter))
661 {
662 GtkWidget *w = GTK_WIDGET (iter->data);
663 if (GTK_IS_LABEL (w))
664 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
665 }
666 g_list_free (list);
667
668 /* ATK needs an empty title for some reason. */
669 gtk_window_set_title (x->ttip_window, "");
670 /* Realize so we can safely get screen later on. */
671 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
672 gtk_widget_realize (x->ttip_lbl);
673
674 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
675 G_CALLBACK (hierarchy_ch_cb), f);
676 }
677 return FALSE;
678 }
679
680 #endif /* USE_GTK_TOOLTIP */
681
682 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
683 Return true if a system tooltip is available. */
684
685 bool
686 xg_prepare_tooltip (struct frame *f,
687 Lisp_Object string,
688 int *width,
689 int *height)
690 {
691 #ifndef USE_GTK_TOOLTIP
692 return 0;
693 #else
694 struct x_output *x = f->output_data.x;
695 GtkWidget *widget;
696 GdkWindow *gwin;
697 GdkScreen *screen;
698 GtkSettings *settings;
699 gboolean tt_enabled = TRUE;
700 GtkRequisition req;
701 Lisp_Object encoded_string;
702
703 if (!x->ttip_lbl) return 0;
704
705 block_input ();
706 encoded_string = ENCODE_UTF_8 (string);
707 widget = GTK_WIDGET (x->ttip_lbl);
708 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
709 screen = gdk_window_get_screen (gwin);
710 settings = gtk_settings_get_for_screen (screen);
711 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
712 if (tt_enabled)
713 {
714 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
715 /* Record that we disabled it so it can be enabled again. */
716 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
717 (gpointer)f);
718 }
719
720 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
721 g_object_set_data (G_OBJECT
722 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
723 "gdk-display-current-tooltip", NULL);
724
725 /* Put our dummy widget in so we can get callbacks for unrealize and
726 hierarchy-changed. */
727 gtk_tooltip_set_custom (x->ttip_widget, widget);
728 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
729 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
730 if (width) *width = req.width;
731 if (height) *height = req.height;
732
733 unblock_input ();
734
735 return 1;
736 #endif /* USE_GTK_TOOLTIP */
737 }
738
739 /* Show the tooltip at ROOT_X and ROOT_Y.
740 xg_prepare_tooltip must have been called before this function. */
741
742 void
743 xg_show_tooltip (struct frame *f, int root_x, int root_y)
744 {
745 #ifdef USE_GTK_TOOLTIP
746 struct x_output *x = f->output_data.x;
747 if (x->ttip_window)
748 {
749 block_input ();
750 gtk_window_move (x->ttip_window, root_x, root_y);
751 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
752 unblock_input ();
753 }
754 #endif
755 }
756
757 /* Hide tooltip if shown. Do nothing if not shown.
758 Return true if tip was hidden, false if not (i.e. not using
759 system tooltips). */
760
761 bool
762 xg_hide_tooltip (struct frame *f)
763 {
764 bool ret = 0;
765 #ifdef USE_GTK_TOOLTIP
766 if (f->output_data.x->ttip_window)
767 {
768 GtkWindow *win = f->output_data.x->ttip_window;
769 block_input ();
770 gtk_widget_hide (GTK_WIDGET (win));
771
772 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
773 {
774 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
775 GdkScreen *screen = gdk_window_get_screen (gwin);
776 GtkSettings *settings = gtk_settings_get_for_screen (screen);
777 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
778 }
779 unblock_input ();
780
781 ret = 1;
782 }
783 #endif
784 return ret;
785 }
786
787 \f
788 /***********************************************************************
789 General functions for creating widgets, resizing, events, e.t.c.
790 ***********************************************************************/
791
792 static void
793 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
794 const gchar *msg, gpointer user_data)
795 {
796 if (!strstr (msg, "visible children"))
797 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
798 }
799
800 /* Make a geometry string and pass that to GTK. It seems this is the
801 only way to get geometry position right if the user explicitly
802 asked for a position when starting Emacs.
803 F is the frame we shall set geometry for. */
804
805 static void
806 xg_set_geometry (struct frame *f)
807 {
808 if (f->size_hint_flags & (USPosition | PPosition))
809 {
810 int left = f->left_pos;
811 int xneg = f->size_hint_flags & XNegative;
812 int top = f->top_pos;
813 int yneg = f->size_hint_flags & YNegative;
814 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
815 guint id;
816
817 if (xneg)
818 left = -left;
819 if (yneg)
820 top = -top;
821
822 sprintf (geom_str, "=%dx%d%c%d%c%d",
823 FRAME_PIXEL_WIDTH (f),
824 FRAME_PIXEL_HEIGHT (f),
825 (xneg ? '-' : '+'), left,
826 (yneg ? '-' : '+'), top);
827
828 /* Silence warning about visible children. */
829 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
830 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
831
832 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
833 geom_str))
834 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
835
836 g_log_remove_handler ("Gtk", id);
837 }
838 }
839
840 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
841 and use a GtkFixed widget, this doesn't happen automatically. */
842
843 void
844 xg_clear_under_internal_border (struct frame *f)
845 {
846 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
847 {
848 GtkWidget *wfixed = f->output_data.x->edit_widget;
849
850 gtk_widget_queue_draw (wfixed);
851 gdk_window_process_all_updates ();
852
853 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
854 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
855
856 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
857 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
858
859 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
860 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
861 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
862
863 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
864 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
865 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
866 }
867 }
868
869 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
870 and a Gtk+ menu bar, we get resize events for the edit part of the
871 frame only. We let Gtk+ deal with the Gtk+ parts.
872 F is the frame to resize.
873 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
874
875 void
876 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
877 {
878 int width, height;
879
880 if (pixelwidth == -1 && pixelheight == -1)
881 {
882 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
883 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
884 0, 0, &pixelwidth, &pixelheight);
885 else
886 return;
887 }
888
889 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
890 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
891
892 frame_size_history_add
893 (f, Qxg_frame_resized, width, height, Qnil);
894
895 if (width != FRAME_TEXT_WIDTH (f)
896 || height != FRAME_TEXT_HEIGHT (f)
897 || pixelwidth != FRAME_PIXEL_WIDTH (f)
898 || pixelheight != FRAME_PIXEL_HEIGHT (f))
899 {
900 xg_clear_under_internal_border (f);
901 change_frame_size (f, width, height, 0, 1, 0, 1);
902 SET_FRAME_GARBAGED (f);
903 cancel_mouse_face (f);
904
905 do_pending_window_change (0);
906 }
907 }
908
909 /* Resize the outer window of frame F after changing the height.
910 COLUMNS/ROWS is the size the edit area shall have after the resize. */
911
912 void
913 xg_frame_set_char_size (struct frame *f, int width, int height)
914 {
915 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
916 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
917 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
918 gint gwidth, gheight;
919
920 if (FRAME_PIXEL_HEIGHT (f) == 0)
921 return;
922
923 gtk_window_get_size (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
924 &gwidth, &gheight);
925
926 /* Do this before resize, as we don't know yet if we will be resized. */
927 xg_clear_under_internal_border (f);
928
929 /* Resize the top level widget so rows and columns remain constant.
930
931 When the frame is fullheight and we only want to change the width
932 or it is fullwidth and we only want to change the height we should
933 be able to preserve the fullscreen property. However, due to the
934 fact that we have to send a resize request anyway, the window
935 manager will abolish it. At least the respective size should
936 remain unchanged but giving the frame back its normal size will
937 be broken ... */
938 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
939 {
940 frame_size_history_add
941 (f, Qxg_frame_set_char_size_1, width, height,
942 list2 (make_number (gheight),
943 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
944 + FRAME_MENUBAR_HEIGHT (f))));
945
946 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
947 gwidth,
948 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
949 + FRAME_MENUBAR_HEIGHT (f));
950 }
951 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
952 {
953 frame_size_history_add
954 (f, Qxg_frame_set_char_size_2, width, height,
955 list2 (make_number (gwidth),
956 make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f))));
957
958 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
959 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
960 gheight);
961 }
962
963 else
964 {
965 frame_size_history_add
966 (f, Qxg_frame_set_char_size_3, width, height,
967 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
968 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
969 + FRAME_MENUBAR_HEIGHT (f))));
970
971 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
972 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
973 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
974 + FRAME_MENUBAR_HEIGHT (f));
975 fullscreen = Qnil;
976 }
977
978 SET_FRAME_GARBAGED (f);
979 cancel_mouse_face (f);
980
981 x_wm_set_size_hint (f, 0, 0);
982 /* We can not call change_frame_size for a mapped frame,
983 we can not set pixel width/height either. The window manager may
984 override our resize request, XMonad does this all the time.
985 The best we can do is try to sync, so lisp code sees the updated
986 size as fast as possible.
987 For unmapped windows, we can set rows/cols. When
988 the frame is mapped again we will (hopefully) get the correct size. */
989 if (FRAME_VISIBLE_P (f))
990 {
991 /* Must call this to flush out events */
992 (void)gtk_events_pending ();
993 gdk_flush ();
994 x_wait_for_event (f, ConfigureNotify);
995
996 if (!NILP (fullscreen))
997 /* Try to restore fullscreen state. */
998 {
999 store_frame_param (f, Qfullscreen, fullscreen);
1000 x_set_fullscreen (f, fullscreen, fullscreen);
1001 }
1002 }
1003 else
1004 adjust_frame_size (f, width, height, 5, 0, Qxg_frame_set_char_size);
1005
1006 }
1007
1008 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
1009 The policy is to keep the number of editable lines. */
1010
1011 #if 0
1012 static void
1013 xg_height_or_width_changed (struct frame *f)
1014 {
1015 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1016 FRAME_TOTAL_PIXEL_WIDTH (f),
1017 FRAME_TOTAL_PIXEL_HEIGHT (f));
1018 f->output_data.x->hint_flags = 0;
1019 x_wm_set_size_hint (f, 0, 0);
1020 }
1021 #endif
1022
1023 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
1024 Must be done like this, because GtkWidget:s can have "hidden"
1025 X Window that aren't accessible.
1026
1027 Return 0 if no widget match WDESC. */
1028
1029 GtkWidget *
1030 xg_win_to_widget (Display *dpy, Window wdesc)
1031 {
1032 gpointer gdkwin;
1033 GtkWidget *gwdesc = 0;
1034
1035 block_input ();
1036
1037 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
1038 wdesc);
1039 if (gdkwin)
1040 {
1041 GdkEvent event;
1042 event.any.window = gdkwin;
1043 event.any.type = GDK_NOTHING;
1044 gwdesc = gtk_get_event_widget (&event);
1045 }
1046
1047 unblock_input ();
1048 return gwdesc;
1049 }
1050
1051 /* Set the background of widget W to PIXEL. */
1052
1053 static void
1054 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
1055 {
1056 #ifdef HAVE_GTK3
1057 GdkRGBA bg;
1058 XColor xbg;
1059 xbg.pixel = pixel;
1060 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1061 {
1062 bg.red = (double)xbg.red/65535.0;
1063 bg.green = (double)xbg.green/65535.0;
1064 bg.blue = (double)xbg.blue/65535.0;
1065 bg.alpha = 1.0;
1066 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1067 }
1068 #else
1069 GdkColor bg;
1070 GdkColormap *map = gtk_widget_get_colormap (w);
1071 gdk_colormap_query_color (map, pixel, &bg);
1072 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1073 #endif
1074 }
1075
1076 /* Callback called when the gtk theme changes.
1077 We notify lisp code so it can fix faces used for region for example. */
1078
1079 static void
1080 style_changed_cb (GObject *go,
1081 GParamSpec *spec,
1082 gpointer user_data)
1083 {
1084 struct input_event event;
1085 GdkDisplay *gdpy = user_data;
1086 const char *display_name = gdk_display_get_name (gdpy);
1087 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1088
1089 EVENT_INIT (event);
1090 event.kind = CONFIG_CHANGED_EVENT;
1091 event.frame_or_window = build_string (display_name);
1092 /* Theme doesn't change often, so intern is called seldom. */
1093 event.arg = intern ("theme-name");
1094 kbd_buffer_store_event (&event);
1095
1096 update_theme_scrollbar_width ();
1097 update_theme_scrollbar_height ();
1098
1099 /* If scroll bar width changed, we need set the new size on all frames
1100 on this display. */
1101 if (dpy)
1102 {
1103 Lisp_Object rest, frame;
1104 FOR_EACH_FRAME (rest, frame)
1105 {
1106 struct frame *f = XFRAME (frame);
1107 if (FRAME_LIVE_P (f)
1108 && FRAME_X_P (f)
1109 && FRAME_X_DISPLAY (f) == dpy)
1110 {
1111 x_set_scroll_bar_default_width (f);
1112 x_set_scroll_bar_default_height (f);
1113 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1114 }
1115 }
1116 }
1117 }
1118
1119 /* Called when a delete-event occurs on WIDGET. */
1120
1121 static gboolean
1122 delete_cb (GtkWidget *widget,
1123 GdkEvent *event,
1124 gpointer user_data)
1125 {
1126 #ifdef HAVE_GTK3
1127 /* The event doesn't arrive in the normal event loop. Send event
1128 here. */
1129 struct frame *f = user_data;
1130 struct input_event ie;
1131
1132 EVENT_INIT (ie);
1133 ie.kind = DELETE_WINDOW_EVENT;
1134 XSETFRAME (ie.frame_or_window, f);
1135 kbd_buffer_store_event (&ie);
1136 #endif
1137
1138 return TRUE;
1139 }
1140
1141 /* Create and set up the GTK widgets for frame F.
1142 Return true if creation succeeded. */
1143
1144 bool
1145 xg_create_frame_widgets (struct frame *f)
1146 {
1147 GtkWidget *wtop;
1148 GtkWidget *wvbox, *whbox;
1149 GtkWidget *wfixed;
1150 #ifndef HAVE_GTK3
1151 GtkRcStyle *style;
1152 #endif
1153 char *title = 0;
1154
1155 block_input ();
1156
1157 if (FRAME_X_EMBEDDED_P (f))
1158 {
1159 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1160 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1161 }
1162 else
1163 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1164
1165 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1166 has backported it to Gtk+ 2.0 and they add the resize grip for
1167 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1168 forever, so disable the grip. */
1169 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1170 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1171 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1172 #endif
1173
1174 xg_set_screen (wtop, f);
1175
1176 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1177 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1178 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1179 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1180
1181 #ifdef HAVE_GTK3
1182 wfixed = emacs_fixed_new (f);
1183 #else
1184 wfixed = gtk_fixed_new ();
1185 #endif
1186
1187 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1188 {
1189 if (wtop) gtk_widget_destroy (wtop);
1190 if (wvbox) gtk_widget_destroy (wvbox);
1191 if (whbox) gtk_widget_destroy (whbox);
1192 if (wfixed) gtk_widget_destroy (wfixed);
1193
1194 unblock_input ();
1195 return 0;
1196 }
1197
1198 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1199 gtk_widget_set_name (wtop, EMACS_CLASS);
1200 gtk_widget_set_name (wvbox, "pane");
1201 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1202
1203 /* If this frame has a title or name, set it in the title bar. */
1204 if (! NILP (f->title))
1205 title = SSDATA (ENCODE_UTF_8 (f->title));
1206 else if (! NILP (f->name))
1207 title = SSDATA (ENCODE_UTF_8 (f->name));
1208
1209 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1210
1211 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1212 FRAME_GTK_WIDGET (f) = wfixed;
1213 f->output_data.x->vbox_widget = wvbox;
1214 f->output_data.x->hbox_widget = whbox;
1215
1216 gtk_widget_set_has_window (wfixed, TRUE);
1217
1218 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1219 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1220 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1221
1222 if (FRAME_EXTERNAL_TOOL_BAR (f))
1223 update_frame_tool_bar (f);
1224
1225 /* We don't want this widget double buffered, because we draw on it
1226 with regular X drawing primitives, so from a GTK/GDK point of
1227 view, the widget is totally blank. When an expose comes, this
1228 will make the widget blank, and then Emacs redraws it. This flickers
1229 a lot, so we turn off double buffering. */
1230 gtk_widget_set_double_buffered (wfixed, FALSE);
1231
1232 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1233 SSDATA (Vx_resource_name),
1234 SSDATA (Vx_resource_class));
1235
1236 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1237 GTK is to destroy the widget. We want Emacs to do that instead. */
1238 g_signal_connect (G_OBJECT (wtop), "delete-event",
1239 G_CALLBACK (delete_cb), f);
1240
1241 /* Convert our geometry parameters into a geometry string
1242 and specify it.
1243 GTK will itself handle calculating the real position this way. */
1244 xg_set_geometry (f);
1245 f->win_gravity
1246 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1247
1248 gtk_widget_add_events (wfixed,
1249 GDK_POINTER_MOTION_MASK
1250 | GDK_EXPOSURE_MASK
1251 | GDK_BUTTON_PRESS_MASK
1252 | GDK_BUTTON_RELEASE_MASK
1253 | GDK_KEY_PRESS_MASK
1254 | GDK_ENTER_NOTIFY_MASK
1255 | GDK_LEAVE_NOTIFY_MASK
1256 | GDK_FOCUS_CHANGE_MASK
1257 | GDK_STRUCTURE_MASK
1258 | GDK_VISIBILITY_NOTIFY_MASK);
1259
1260 /* Must realize the windows so the X window gets created. It is used
1261 by callers of this function. */
1262 gtk_widget_realize (wfixed);
1263 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1264
1265 /* Since GTK clears its window by filling with the background color,
1266 we must keep X and GTK background in sync. */
1267 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1268
1269 #ifndef HAVE_GTK3
1270 /* Also, do not let any background pixmap to be set, this looks very
1271 bad as Emacs overwrites the background pixmap with its own idea
1272 of background color. */
1273 style = gtk_widget_get_modifier_style (wfixed);
1274
1275 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1276 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1277 gtk_widget_modify_style (wfixed, style);
1278 #else
1279 gtk_widget_set_can_focus (wfixed, TRUE);
1280 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1281 #endif
1282
1283 #ifdef USE_GTK_TOOLTIP
1284 /* Steal a tool tip window we can move ourselves. */
1285 f->output_data.x->ttip_widget = 0;
1286 f->output_data.x->ttip_lbl = 0;
1287 f->output_data.x->ttip_window = 0;
1288 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1289 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1290 #endif
1291
1292 {
1293 GdkScreen *screen = gtk_widget_get_screen (wtop);
1294 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1295 /* Only connect this signal once per screen. */
1296 if (! g_signal_handler_find (G_OBJECT (gs),
1297 G_SIGNAL_MATCH_FUNC,
1298 0, 0, 0,
1299 G_CALLBACK (style_changed_cb),
1300 0))
1301 {
1302 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1303 G_CALLBACK (style_changed_cb),
1304 gdk_screen_get_display (screen));
1305 }
1306 }
1307
1308 unblock_input ();
1309
1310 return 1;
1311 }
1312
1313 void
1314 xg_free_frame_widgets (struct frame *f)
1315 {
1316 if (FRAME_GTK_OUTER_WIDGET (f))
1317 {
1318 #ifdef USE_GTK_TOOLTIP
1319 struct x_output *x = f->output_data.x;
1320 #endif
1321 struct xg_frame_tb_info *tbinfo
1322 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1323 TB_INFO_KEY);
1324 if (tbinfo)
1325 xfree (tbinfo);
1326
1327 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1328 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1329 FRAME_GTK_OUTER_WIDGET (f) = 0;
1330 #ifdef USE_GTK_TOOLTIP
1331 if (x->ttip_lbl)
1332 gtk_widget_destroy (x->ttip_lbl);
1333 if (x->ttip_widget)
1334 g_object_unref (G_OBJECT (x->ttip_widget));
1335 #endif
1336 }
1337 }
1338
1339 /* Set the normal size hints for the window manager, for frame F.
1340 FLAGS is the flags word to use--or 0 meaning preserve the flags
1341 that the window now has.
1342 If USER_POSITION, set the User Position
1343 flag (this is useful when FLAGS is 0). */
1344
1345 void
1346 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1347 {
1348 /* Must use GTK routines here, otherwise GTK resets the size hints
1349 to its own defaults. */
1350 GdkGeometry size_hints;
1351 gint hint_flags = 0;
1352 int base_width, base_height;
1353 int min_rows = 0, min_cols = 0;
1354 int win_gravity = f->win_gravity;
1355 Lisp_Object fs_state, frame;
1356
1357 /* Don't set size hints during initialization; that apparently leads
1358 to a race condition. See the thread at
1359 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1360 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1361 return;
1362
1363 XSETFRAME (frame, f);
1364 fs_state = Fframe_parameter (frame, Qfullscreen);
1365 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1366 {
1367 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1368 Gtk3 don't get along and the frame shrinks (!).
1369 */
1370 return;
1371 }
1372
1373 if (flags)
1374 {
1375 memset (&size_hints, 0, sizeof (size_hints));
1376 f->output_data.x->size_hints = size_hints;
1377 f->output_data.x->hint_flags = hint_flags;
1378 }
1379 else
1380 flags = f->size_hint_flags;
1381
1382 size_hints = f->output_data.x->size_hints;
1383 hint_flags = f->output_data.x->hint_flags;
1384
1385 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1386 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1387 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1388
1389 hint_flags |= GDK_HINT_BASE_SIZE;
1390 /* Use one row/col here so base_height/width does not become zero.
1391 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1392 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1393 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1394 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1395
1396 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1397 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1398
1399 size_hints.base_width = base_width;
1400 size_hints.base_height = base_height;
1401 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
1402 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1403
1404 /* These currently have a one to one mapping with the X values, but I
1405 don't think we should rely on that. */
1406 hint_flags |= GDK_HINT_WIN_GRAVITY;
1407 size_hints.win_gravity = 0;
1408 if (win_gravity == NorthWestGravity)
1409 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1410 else if (win_gravity == NorthGravity)
1411 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1412 else if (win_gravity == NorthEastGravity)
1413 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1414 else if (win_gravity == WestGravity)
1415 size_hints.win_gravity = GDK_GRAVITY_WEST;
1416 else if (win_gravity == CenterGravity)
1417 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1418 else if (win_gravity == EastGravity)
1419 size_hints.win_gravity = GDK_GRAVITY_EAST;
1420 else if (win_gravity == SouthWestGravity)
1421 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1422 else if (win_gravity == SouthGravity)
1423 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1424 else if (win_gravity == SouthEastGravity)
1425 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1426 else if (win_gravity == StaticGravity)
1427 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1428
1429 if (user_position)
1430 {
1431 hint_flags &= ~GDK_HINT_POS;
1432 hint_flags |= GDK_HINT_USER_POS;
1433 }
1434
1435 if (hint_flags != f->output_data.x->hint_flags
1436 || memcmp (&size_hints,
1437 &f->output_data.x->size_hints,
1438 sizeof (size_hints)) != 0)
1439 {
1440 block_input ();
1441 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1442 NULL, &size_hints, hint_flags);
1443 f->output_data.x->size_hints = size_hints;
1444 f->output_data.x->hint_flags = hint_flags;
1445 unblock_input ();
1446 }
1447 }
1448
1449 /* Change background color of a frame.
1450 Since GTK uses the background color to clear the window, we must
1451 keep the GTK and X colors in sync.
1452 F is the frame to change,
1453 BG is the pixel value to change to. */
1454
1455 void
1456 xg_set_background_color (struct frame *f, unsigned long bg)
1457 {
1458 if (FRAME_GTK_WIDGET (f))
1459 {
1460 block_input ();
1461 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1462 unblock_input ();
1463 }
1464 }
1465
1466
1467 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1468 functions so GTK does not overwrite the icon. */
1469
1470 void
1471 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1472 {
1473 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1474 icon_pixmap,
1475 icon_mask);
1476 if (gp)
1477 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1478 }
1479
1480
1481 \f
1482 /***********************************************************************
1483 Dialog functions
1484 ***********************************************************************/
1485 /* Return the dialog title to use for a dialog of type KEY.
1486 This is the encoding used by lwlib. We use the same for GTK. */
1487
1488 static const char *
1489 get_dialog_title (char key)
1490 {
1491 const char *title = "";
1492
1493 switch (key) {
1494 case 'E': case 'e':
1495 title = "Error";
1496 break;
1497
1498 case 'I': case 'i':
1499 title = "Information";
1500 break;
1501
1502 case 'L': case 'l':
1503 title = "Prompt";
1504 break;
1505
1506 case 'P': case 'p':
1507 title = "Prompt";
1508 break;
1509
1510 case 'Q': case 'q':
1511 title = "Question";
1512 break;
1513 }
1514
1515 return title;
1516 }
1517
1518 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1519 the dialog, but return TRUE so the event does not propagate further
1520 in GTK. This prevents GTK from destroying the dialog widget automatically
1521 and we can always destroy the widget manually, regardless of how
1522 it was popped down (button press or WM_DELETE_WINDOW).
1523 W is the dialog widget.
1524 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1525 user_data is NULL (not used).
1526
1527 Returns TRUE to end propagation of event. */
1528
1529 static gboolean
1530 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1531 {
1532 gtk_widget_unmap (w);
1533 return TRUE;
1534 }
1535
1536 /* Create a popup dialog window. See also xg_create_widget below.
1537 WV is a widget_value describing the dialog.
1538 SELECT_CB is the callback to use when a button has been pressed.
1539 DEACTIVATE_CB is the callback to use when the dialog pops down.
1540
1541 Returns the GTK dialog widget. */
1542
1543 static GtkWidget *
1544 create_dialog (widget_value *wv,
1545 GCallback select_cb,
1546 GCallback deactivate_cb)
1547 {
1548 const char *title = get_dialog_title (wv->name[0]);
1549 int total_buttons = wv->name[1] - '0';
1550 int right_buttons = wv->name[4] - '0';
1551 int left_buttons;
1552 int button_nr = 0;
1553 int button_spacing = 10;
1554 GtkWidget *wdialog = gtk_dialog_new ();
1555 GtkDialog *wd = GTK_DIALOG (wdialog);
1556 widget_value *item;
1557 GtkWidget *whbox_down;
1558
1559 /* If the number of buttons is greater than 4, make two rows of buttons
1560 instead. This looks better. */
1561 bool make_two_rows = total_buttons > 4;
1562
1563 #if GTK_CHECK_VERSION (3, 12, 0)
1564 GtkBuilder *gbld = gtk_builder_new ();
1565 GObject *go = gtk_buildable_get_internal_child (GTK_BUILDABLE (wd),
1566 gbld,
1567 "action_area");
1568 GtkBox *cur_box = GTK_BOX (go);
1569 g_object_unref (G_OBJECT (gbld));
1570 #else
1571 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1572 #endif
1573
1574 if (right_buttons == 0) right_buttons = total_buttons/2;
1575 left_buttons = total_buttons - right_buttons;
1576
1577 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1578 gtk_widget_set_name (wdialog, "emacs-dialog");
1579
1580
1581 if (make_two_rows)
1582 {
1583 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1584 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1585 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1586 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1587 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1588 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1589
1590 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1591 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1592 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1593
1594 cur_box = GTK_BOX (whbox_up);
1595 }
1596
1597 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1598 G_CALLBACK (dialog_delete_callback), 0);
1599
1600 if (deactivate_cb)
1601 {
1602 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1603 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1604 }
1605
1606 for (item = wv->contents; item; item = item->next)
1607 {
1608 char *utf8_label = get_utf8_string (item->value);
1609 GtkWidget *w;
1610 GtkRequisition req;
1611
1612 if (item->name && strcmp (item->name, "message") == 0)
1613 {
1614 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1615 /* This is the text part of the dialog. */
1616 w = gtk_label_new (utf8_label);
1617 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1618 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1619 #if GTK_CHECK_VERSION (3, 14, 0)
1620 gtk_widget_set_halign (w, GTK_ALIGN_START);
1621 gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
1622 #else
1623 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1624 #endif
1625 /* Try to make dialog look better. Must realize first so
1626 the widget can calculate the size it needs. */
1627 gtk_widget_realize (w);
1628 gtk_widget_get_preferred_size (w, NULL, &req);
1629 gtk_box_set_spacing (wvbox, req.height);
1630 if (item->value && strlen (item->value) > 0)
1631 button_spacing = 2*req.width/strlen (item->value);
1632 if (button_spacing < 10) button_spacing = 10;
1633 }
1634 else
1635 {
1636 /* This is one button to add to the dialog. */
1637 w = gtk_button_new_with_label (utf8_label);
1638 if (! item->enabled)
1639 gtk_widget_set_sensitive (w, FALSE);
1640 if (select_cb)
1641 g_signal_connect (G_OBJECT (w), "clicked",
1642 select_cb, item->call_data);
1643
1644 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1645 if (++button_nr == left_buttons)
1646 {
1647 if (make_two_rows)
1648 cur_box = GTK_BOX (whbox_down);
1649 }
1650 }
1651
1652 if (utf8_label)
1653 g_free (utf8_label);
1654 }
1655
1656 return wdialog;
1657 }
1658
1659 struct xg_dialog_data
1660 {
1661 GMainLoop *loop;
1662 int response;
1663 GtkWidget *w;
1664 guint timerid;
1665 };
1666
1667 /* Function that is called when the file or font dialogs pop down.
1668 W is the dialog widget, RESPONSE is the response code.
1669 USER_DATA is what we passed in to g_signal_connect. */
1670
1671 static void
1672 xg_dialog_response_cb (GtkDialog *w,
1673 gint response,
1674 gpointer user_data)
1675 {
1676 struct xg_dialog_data *dd = user_data;
1677 dd->response = response;
1678 g_main_loop_quit (dd->loop);
1679 }
1680
1681
1682 /* Destroy the dialog. This makes it pop down. */
1683
1684 static void
1685 pop_down_dialog (void *arg)
1686 {
1687 struct xg_dialog_data *dd = arg;
1688
1689 block_input ();
1690 if (dd->w) gtk_widget_destroy (dd->w);
1691 if (dd->timerid != 0) g_source_remove (dd->timerid);
1692
1693 g_main_loop_quit (dd->loop);
1694 g_main_loop_unref (dd->loop);
1695
1696 unblock_input ();
1697 }
1698
1699 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1700 We pass in DATA as gpointer* so we can use this as a callback. */
1701
1702 static gboolean
1703 xg_maybe_add_timer (gpointer data)
1704 {
1705 struct xg_dialog_data *dd = data;
1706 struct timespec next_time = timer_check ();
1707
1708 dd->timerid = 0;
1709
1710 if (timespec_valid_p (next_time))
1711 {
1712 time_t s = next_time.tv_sec;
1713 int per_ms = TIMESPEC_RESOLUTION / 1000;
1714 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1715 if (s <= ((guint) -1 - ms) / 1000)
1716 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1717 }
1718 return FALSE;
1719 }
1720
1721
1722 /* Pops up a modal dialog W and waits for response.
1723 We don't use gtk_dialog_run because we want to process emacs timers.
1724 The dialog W is not destroyed when this function returns. */
1725
1726 static int
1727 xg_dialog_run (struct frame *f, GtkWidget *w)
1728 {
1729 ptrdiff_t count = SPECPDL_INDEX ();
1730 struct xg_dialog_data dd;
1731
1732 xg_set_screen (w, f);
1733 gtk_window_set_transient_for (GTK_WINDOW (w),
1734 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1735 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1736 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1737
1738 dd.loop = g_main_loop_new (NULL, FALSE);
1739 dd.response = GTK_RESPONSE_CANCEL;
1740 dd.w = w;
1741 dd.timerid = 0;
1742
1743 g_signal_connect (G_OBJECT (w),
1744 "response",
1745 G_CALLBACK (xg_dialog_response_cb),
1746 &dd);
1747 /* Don't destroy the widget if closed by the window manager close button. */
1748 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1749 gtk_widget_show (w);
1750
1751 record_unwind_protect_ptr (pop_down_dialog, &dd);
1752
1753 (void) xg_maybe_add_timer (&dd);
1754 g_main_loop_run (dd.loop);
1755
1756 dd.w = 0;
1757 unbind_to (count, Qnil);
1758
1759 return dd.response;
1760 }
1761
1762 \f
1763 /***********************************************************************
1764 File dialog functions
1765 ***********************************************************************/
1766 /* Return true if the old file selection dialog is being used. */
1767
1768 bool
1769 xg_uses_old_file_dialog (void)
1770 {
1771 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1772 return x_gtk_use_old_file_dialog;
1773 #else
1774 return 0;
1775 #endif
1776 }
1777
1778
1779 typedef char * (*xg_get_file_func) (GtkWidget *);
1780
1781 /* Return the selected file for file chooser dialog W.
1782 The returned string must be free:d. */
1783
1784 static char *
1785 xg_get_file_name_from_chooser (GtkWidget *w)
1786 {
1787 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1788 }
1789
1790 /* Callback called when the "Show hidden files" toggle is pressed.
1791 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1792
1793 static void
1794 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1795 {
1796 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1797 gboolean visible;
1798 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1799 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1800 }
1801
1802
1803 /* Callback called when a property changes in a file chooser.
1804 GOBJECT is the file chooser dialog, ARG1 describes the property.
1805 USER_DATA is the toggle widget in the file chooser dialog.
1806 We use this to update the "Show hidden files" toggle when the user
1807 changes that property by right clicking in the file list. */
1808
1809 static void
1810 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1811 {
1812 if (strcmp (arg1->name, "show-hidden") == 0)
1813 {
1814 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1815 gboolean visible, toggle_on;
1816
1817 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1818 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1819
1820 if (!!visible != !!toggle_on)
1821 {
1822 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1823 G_CALLBACK (xg_toggle_visibility_cb),
1824 gobject);
1825 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1826 g_signal_handlers_unblock_by_func
1827 (G_OBJECT (wtoggle),
1828 G_CALLBACK (xg_toggle_visibility_cb),
1829 gobject);
1830 }
1831 x_gtk_show_hidden_files = visible;
1832 }
1833 }
1834
1835 /* Read a file name from the user using a file chooser dialog.
1836 F is the current frame.
1837 PROMPT is a prompt to show to the user. May not be NULL.
1838 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1839 If MUSTMATCH_P, the returned file name must be an existing
1840 file. (Actually, this only has cosmetic effects, the user can
1841 still enter a non-existing file.) *FUNC is set to a function that
1842 can be used to retrieve the selected file name from the returned widget.
1843
1844 Returns the created widget. */
1845
1846 static GtkWidget *
1847 xg_get_file_with_chooser (struct frame *f,
1848 char *prompt,
1849 char *default_filename,
1850 bool mustmatch_p, bool only_dir_p,
1851 xg_get_file_func *func)
1852 {
1853 char msgbuf[1024];
1854
1855 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1856 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1857 GtkFileChooserAction action = (mustmatch_p ?
1858 GTK_FILE_CHOOSER_ACTION_OPEN :
1859 GTK_FILE_CHOOSER_ACTION_SAVE);
1860
1861 if (only_dir_p)
1862 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1863
1864 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1865 XG_TEXT_CANCEL, GTK_RESPONSE_CANCEL,
1866 (mustmatch_p || only_dir_p ?
1867 XG_TEXT_OPEN : XG_TEXT_OK),
1868 GTK_RESPONSE_OK,
1869 NULL);
1870 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1871
1872 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1873 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1874 gtk_widget_show (wbox);
1875 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1876
1877 if (x_gtk_show_hidden_files)
1878 {
1879 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1880 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1881 }
1882 gtk_widget_show (wtoggle);
1883 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1884 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1885 g_signal_connect (G_OBJECT (filewin), "notify",
1886 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1887
1888 if (x_gtk_file_dialog_help_text)
1889 {
1890 char *z = msgbuf;
1891 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1892 Show the C-l help text only for versions < 2.10. */
1893 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1894 z = stpcpy (z, "\nType C-l to display a file name text entry box.\n");
1895 strcpy (z, "\nIf you don't like this file selector, use the "
1896 "corresponding\nkey binding or customize "
1897 "use-file-dialog to turn it off.");
1898
1899 wmessage = gtk_label_new (msgbuf);
1900 gtk_widget_show (wmessage);
1901 }
1902
1903 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1904 if (x_gtk_file_dialog_help_text)
1905 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1906 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1907
1908 if (default_filename)
1909 {
1910 Lisp_Object file;
1911 struct gcpro gcpro1;
1912 char *utf8_filename;
1913 GCPRO1 (file);
1914
1915 file = build_string (default_filename);
1916
1917 /* File chooser does not understand ~/... in the file name. It must be
1918 an absolute name starting with /. */
1919 if (default_filename[0] != '/')
1920 file = Fexpand_file_name (file, Qnil);
1921
1922 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1923 if (! NILP (Ffile_directory_p (file)))
1924 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1925 utf8_filename);
1926 else
1927 {
1928 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1929 utf8_filename);
1930 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1931 {
1932 char *cp = strrchr (utf8_filename, '/');
1933 if (cp) ++cp;
1934 else cp = utf8_filename;
1935 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1936 }
1937 }
1938
1939 UNGCPRO;
1940 }
1941
1942 *func = xg_get_file_name_from_chooser;
1943 return filewin;
1944 }
1945
1946 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1947
1948 /* Return the selected file for file selector dialog W.
1949 The returned string must be free:d. */
1950
1951 static char *
1952 xg_get_file_name_from_selector (GtkWidget *w)
1953 {
1954 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1955 return xstrdup (gtk_file_selection_get_filename (filesel));
1956 }
1957
1958 /* Create a file selection dialog.
1959 F is the current frame.
1960 PROMPT is a prompt to show to the user. May not be NULL.
1961 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1962 If MUSTMATCH_P, the returned file name must be an existing
1963 file. *FUNC is set to a function that can be used to retrieve the
1964 selected file name from the returned widget.
1965
1966 Returns the created widget. */
1967
1968 static GtkWidget *
1969 xg_get_file_with_selection (struct frame *f,
1970 char *prompt,
1971 char *default_filename,
1972 bool mustmatch_p, bool only_dir_p,
1973 xg_get_file_func *func)
1974 {
1975 GtkWidget *filewin;
1976 GtkFileSelection *filesel;
1977
1978 filewin = gtk_file_selection_new (prompt);
1979 filesel = GTK_FILE_SELECTION (filewin);
1980
1981 if (default_filename)
1982 gtk_file_selection_set_filename (filesel, default_filename);
1983
1984 if (mustmatch_p)
1985 {
1986 /* The selection_entry part of filesel is not documented. */
1987 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1988 gtk_file_selection_hide_fileop_buttons (filesel);
1989 }
1990
1991 *func = xg_get_file_name_from_selector;
1992
1993 return filewin;
1994 }
1995 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1996
1997 /* Read a file name from the user using a file dialog, either the old
1998 file selection dialog, or the new file chooser dialog. Which to use
1999 depends on what the GTK version used has, and what the value of
2000 gtk-use-old-file-dialog.
2001 F is the current frame.
2002 PROMPT is a prompt to show to the user. May not be NULL.
2003 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
2004 If MUSTMATCH_P, the returned file name must be an existing
2005 file.
2006
2007 Returns a file name or NULL if no file was selected.
2008 The returned string must be freed by the caller. */
2009
2010 char *
2011 xg_get_file_name (struct frame *f,
2012 char *prompt,
2013 char *default_filename,
2014 bool mustmatch_p,
2015 bool only_dir_p)
2016 {
2017 GtkWidget *w = 0;
2018 char *fn = 0;
2019 int filesel_done = 0;
2020 xg_get_file_func func;
2021
2022 #ifdef HAVE_GTK_FILE_SELECTION_NEW
2023
2024 if (xg_uses_old_file_dialog ())
2025 w = xg_get_file_with_selection (f, prompt, default_filename,
2026 mustmatch_p, only_dir_p, &func);
2027 else
2028 w = xg_get_file_with_chooser (f, prompt, default_filename,
2029 mustmatch_p, only_dir_p, &func);
2030
2031 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
2032 w = xg_get_file_with_chooser (f, prompt, default_filename,
2033 mustmatch_p, only_dir_p, &func);
2034 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
2035
2036 gtk_widget_set_name (w, "emacs-filedialog");
2037
2038 filesel_done = xg_dialog_run (f, w);
2039 if (filesel_done == GTK_RESPONSE_OK)
2040 fn = (*func) (w);
2041
2042 gtk_widget_destroy (w);
2043 return fn;
2044 }
2045
2046 /***********************************************************************
2047 GTK font chooser
2048 ***********************************************************************/
2049
2050 #ifdef HAVE_FREETYPE
2051
2052 #if USE_NEW_GTK_FONT_CHOOSER
2053
2054 #define XG_WEIGHT_TO_SYMBOL(w) \
2055 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
2056 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
2057 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
2058 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
2059 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
2060 : w <= PANGO_WEIGHT_BOLD ? Qbold \
2061 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
2062 : Qultra_bold)
2063
2064 #define XG_STYLE_TO_SYMBOL(s) \
2065 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
2066 : s == PANGO_STYLE_ITALIC ? Qitalic \
2067 : Qnormal)
2068
2069 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2070
2071
2072 static char *x_last_font_name;
2073
2074 /* Pop up a GTK font selector and return the name of the font the user
2075 selects, as a C string. The returned font name follows GTK's own
2076 format:
2077
2078 `FAMILY [VALUE1 VALUE2] SIZE'
2079
2080 This can be parsed using font_parse_fcname in font.c.
2081 DEFAULT_NAME, if non-zero, is the default font name. */
2082
2083 Lisp_Object
2084 xg_get_font (struct frame *f, const char *default_name)
2085 {
2086 GtkWidget *w;
2087 int done = 0;
2088 Lisp_Object font = Qnil;
2089
2090 w = gtk_font_chooser_dialog_new
2091 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2092
2093 if (default_name)
2094 {
2095 /* Convert fontconfig names to Gtk names, i.e. remove - before
2096 number */
2097 char *p = strrchr (default_name, '-');
2098 if (p)
2099 {
2100 char *ep = p+1;
2101 while (c_isdigit (*ep))
2102 ++ep;
2103 if (*ep == '\0') *p = ' ';
2104 }
2105 }
2106 else if (x_last_font_name)
2107 default_name = x_last_font_name;
2108
2109 if (default_name)
2110 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2111
2112 gtk_widget_set_name (w, "emacs-fontdialog");
2113 done = xg_dialog_run (f, w);
2114 if (done == GTK_RESPONSE_OK)
2115 {
2116 #if USE_NEW_GTK_FONT_CHOOSER
2117 /* Use the GTK3 font chooser. */
2118 PangoFontDescription *desc
2119 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2120
2121 if (desc)
2122 {
2123 const char *name = pango_font_description_get_family (desc);
2124 gint size = pango_font_description_get_size (desc);
2125 PangoWeight weight = pango_font_description_get_weight (desc);
2126 PangoStyle style = pango_font_description_get_style (desc);
2127
2128 font = CALLN (Ffont_spec,
2129 QCname, build_string (name),
2130 QCsize, make_float (pango_units_to_double (size)),
2131 QCweight, XG_WEIGHT_TO_SYMBOL (weight),
2132 QCslant, XG_STYLE_TO_SYMBOL (style),
2133 QCtype, Qxft);
2134
2135 pango_font_description_free (desc);
2136 dupstring (&x_last_font_name, name);
2137 }
2138
2139 #else /* Use old font selector, which just returns the font name. */
2140
2141 char *font_name
2142 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2143
2144 if (font_name)
2145 {
2146 font = build_string (font_name);
2147 g_free (x_last_font_name);
2148 x_last_font_name = font_name;
2149 }
2150 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2151 }
2152
2153 gtk_widget_destroy (w);
2154 return font;
2155 }
2156 #endif /* HAVE_FREETYPE */
2157
2158
2159 \f
2160 /***********************************************************************
2161 Menu functions.
2162 ***********************************************************************/
2163
2164 /* The name of menu items that can be used for customization. Since GTK
2165 RC files are very crude and primitive, we have to set this on all
2166 menu item names so a user can easily customize menu items. */
2167
2168 #define MENU_ITEM_NAME "emacs-menuitem"
2169
2170
2171 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2172 during GC. The next member points to the items. */
2173 static xg_list_node xg_menu_cb_list;
2174
2175 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2176 during GC. The next member points to the items. */
2177 static xg_list_node xg_menu_item_cb_list;
2178
2179 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2180 F is the frame CL_DATA will be initialized for.
2181 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2182
2183 The menu bar and all sub menus under the menu bar in a frame
2184 share the same structure, hence the reference count.
2185
2186 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2187 allocated xg_menu_cb_data if CL_DATA is NULL. */
2188
2189 static xg_menu_cb_data *
2190 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2191 {
2192 if (! cl_data)
2193 {
2194 cl_data = xmalloc (sizeof *cl_data);
2195 cl_data->f = f;
2196 cl_data->menu_bar_vector = f->menu_bar_vector;
2197 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2198 cl_data->highlight_cb = highlight_cb;
2199 cl_data->ref_count = 0;
2200
2201 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2202 }
2203
2204 cl_data->ref_count++;
2205
2206 return cl_data;
2207 }
2208
2209 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2210 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2211
2212 When the menu bar is updated, menu items may have been added and/or
2213 removed, so menu_bar_vector and menu_bar_items_used change. We must
2214 then update CL_DATA since it is used to determine which menu
2215 item that is invoked in the menu.
2216 HIGHLIGHT_CB could change, there is no check that the same
2217 function is given when modifying a menu bar as was given when
2218 creating the menu bar. */
2219
2220 static void
2221 update_cl_data (xg_menu_cb_data *cl_data,
2222 struct frame *f,
2223 GCallback highlight_cb)
2224 {
2225 if (cl_data)
2226 {
2227 cl_data->f = f;
2228 cl_data->menu_bar_vector = f->menu_bar_vector;
2229 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2230 cl_data->highlight_cb = highlight_cb;
2231 }
2232 }
2233
2234 /* Decrease reference count for CL_DATA.
2235 If reference count is zero, free CL_DATA. */
2236
2237 static void
2238 unref_cl_data (xg_menu_cb_data *cl_data)
2239 {
2240 if (cl_data && cl_data->ref_count > 0)
2241 {
2242 cl_data->ref_count--;
2243 if (cl_data->ref_count == 0)
2244 {
2245 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2246 xfree (cl_data);
2247 }
2248 }
2249 }
2250
2251 /* Function that marks all lisp data during GC. */
2252
2253 void
2254 xg_mark_data (void)
2255 {
2256 xg_list_node *iter;
2257 Lisp_Object rest, frame;
2258
2259 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2260 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2261
2262 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2263 {
2264 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2265
2266 if (! NILP (cb_data->help))
2267 mark_object (cb_data->help);
2268 }
2269
2270 FOR_EACH_FRAME (rest, frame)
2271 {
2272 struct frame *f = XFRAME (frame);
2273
2274 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2275 {
2276 struct xg_frame_tb_info *tbinfo
2277 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2278 TB_INFO_KEY);
2279 if (tbinfo)
2280 {
2281 mark_object (tbinfo->last_tool_bar);
2282 mark_object (tbinfo->style);
2283 }
2284 }
2285 }
2286 }
2287
2288
2289 /* Callback called when a menu item is destroyed. Used to free data.
2290 W is the widget that is being destroyed (not used).
2291 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2292
2293 static void
2294 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2295 {
2296 if (client_data)
2297 {
2298 xg_menu_item_cb_data *data = client_data;
2299 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2300 xfree (data);
2301 }
2302 }
2303
2304 /* Callback called when the pointer enters/leaves a menu item.
2305 W is the parent of the menu item.
2306 EVENT is either an enter event or leave event.
2307 CLIENT_DATA is not used.
2308
2309 Returns FALSE to tell GTK to keep processing this event. */
2310
2311 static gboolean
2312 menuitem_highlight_callback (GtkWidget *w,
2313 GdkEventCrossing *event,
2314 gpointer client_data)
2315 {
2316 GdkEvent ev;
2317 GtkWidget *subwidget;
2318 xg_menu_item_cb_data *data;
2319
2320 ev.crossing = *event;
2321 subwidget = gtk_get_event_widget (&ev);
2322 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2323 if (data)
2324 {
2325 if (! NILP (data->help) && data->cl_data->highlight_cb)
2326 {
2327 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2328 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2329 (*func) (subwidget, call_data);
2330 }
2331 }
2332
2333 return FALSE;
2334 }
2335
2336 /* Callback called when a menu is destroyed. Used to free data.
2337 W is the widget that is being destroyed (not used).
2338 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2339
2340 static void
2341 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2342 {
2343 unref_cl_data (client_data);
2344 }
2345
2346 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2347 must be non-NULL) and can be inserted into a menu item.
2348
2349 Returns the GtkHBox. */
2350
2351 static GtkWidget *
2352 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2353 {
2354 GtkWidget *wlbl;
2355 GtkWidget *wkey;
2356 GtkWidget *wbox;
2357
2358 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2359 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2360 wlbl = gtk_label_new (utf8_label);
2361 wkey = gtk_label_new (utf8_key);
2362
2363 #if GTK_CHECK_VERSION (3, 14, 0)
2364 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2365 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2366 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2367 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2368 #else
2369 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2370 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2371 #endif
2372 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2373 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2374
2375 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2376 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2377 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2378
2379 return wbox;
2380 }
2381
2382 /* Make and return a menu item widget with the key to the right.
2383 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2384 UTF8_KEY is the text representing the key binding.
2385 ITEM is the widget_value describing the menu item.
2386
2387 GROUP is an in/out parameter. If the menu item to be created is not
2388 part of any radio menu group, *GROUP contains NULL on entry and exit.
2389 If the menu item to be created is part of a radio menu group, on entry
2390 *GROUP contains the group to use, or NULL if this is the first item
2391 in the group. On exit, *GROUP contains the radio item group.
2392
2393 Unfortunately, keys don't line up as nicely as in Motif,
2394 but the MacOS X version doesn't either, so I guess that is OK. */
2395
2396 static GtkWidget *
2397 make_menu_item (const char *utf8_label,
2398 const char *utf8_key,
2399 widget_value *item,
2400 GSList **group)
2401 {
2402 GtkWidget *w;
2403 GtkWidget *wtoadd = 0;
2404
2405 /* It has been observed that some menu items have a NULL name field.
2406 This will lead to this function being called with a NULL utf8_label.
2407 GTK crashes on that so we set a blank label. Why there is a NULL
2408 name remains to be investigated. */
2409 if (! utf8_label) utf8_label = " ";
2410
2411 if (utf8_key)
2412 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2413
2414 if (item->button_type == BUTTON_TYPE_TOGGLE)
2415 {
2416 *group = NULL;
2417 if (utf8_key) w = gtk_check_menu_item_new ();
2418 else w = gtk_check_menu_item_new_with_label (utf8_label);
2419 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2420 }
2421 else if (item->button_type == BUTTON_TYPE_RADIO)
2422 {
2423 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2424 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2425 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2426 if (item->selected)
2427 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2428 }
2429 else
2430 {
2431 *group = NULL;
2432 if (utf8_key) w = gtk_menu_item_new ();
2433 else w = gtk_menu_item_new_with_label (utf8_label);
2434 }
2435
2436 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2437 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2438
2439 return w;
2440 }
2441
2442 /* Create a menu item widget, and connect the callbacks.
2443 ITEM describes the menu item.
2444 F is the frame the created menu belongs to.
2445 SELECT_CB is the callback to use when a menu item is selected.
2446 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2447 CL_DATA points to the callback data to be used for this menu.
2448 GROUP is an in/out parameter. If the menu item to be created is not
2449 part of any radio menu group, *GROUP contains NULL on entry and exit.
2450 If the menu item to be created is part of a radio menu group, on entry
2451 *GROUP contains the group to use, or NULL if this is the first item
2452 in the group. On exit, *GROUP contains the radio item group.
2453
2454 Returns the created GtkWidget. */
2455
2456 static GtkWidget *
2457 xg_create_one_menuitem (widget_value *item,
2458 struct frame *f,
2459 GCallback select_cb,
2460 GCallback highlight_cb,
2461 xg_menu_cb_data *cl_data,
2462 GSList **group)
2463 {
2464 char *utf8_label;
2465 char *utf8_key;
2466 GtkWidget *w;
2467 xg_menu_item_cb_data *cb_data;
2468
2469 utf8_label = get_utf8_string (item->name);
2470 utf8_key = get_utf8_string (item->key);
2471
2472 w = make_menu_item (utf8_label, utf8_key, item, group);
2473
2474 if (utf8_label) g_free (utf8_label);
2475 if (utf8_key) g_free (utf8_key);
2476
2477 cb_data = xmalloc (sizeof *cb_data);
2478
2479 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2480
2481 cb_data->select_id = 0;
2482 cb_data->help = item->help;
2483 cb_data->cl_data = cl_data;
2484 cb_data->call_data = item->call_data;
2485
2486 g_signal_connect (G_OBJECT (w),
2487 "destroy",
2488 G_CALLBACK (menuitem_destroy_callback),
2489 cb_data);
2490
2491 /* Put cb_data in widget, so we can get at it when modifying menubar */
2492 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2493
2494 /* final item, not a submenu */
2495 if (item->call_data && ! item->contents)
2496 {
2497 if (select_cb)
2498 cb_data->select_id
2499 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2500 }
2501
2502 return w;
2503 }
2504
2505 /* Create a full menu tree specified by DATA.
2506 F is the frame the created menu belongs to.
2507 SELECT_CB is the callback to use when a menu item is selected.
2508 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2509 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2510 If POP_UP_P, create a popup menu.
2511 If MENU_BAR_P, create a menu bar.
2512 TOPMENU is the topmost GtkWidget that others shall be placed under.
2513 It may be NULL, in that case we create the appropriate widget
2514 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2515 CL_DATA is the callback data we shall use for this menu, or NULL
2516 if we haven't set the first callback yet.
2517 NAME is the name to give to the top level menu if this function
2518 creates it. May be NULL to not set any name.
2519
2520 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2521 not NULL.
2522
2523 This function calls itself to create submenus. */
2524
2525 static GtkWidget *
2526 create_menus (widget_value *data,
2527 struct frame *f,
2528 GCallback select_cb,
2529 GCallback deactivate_cb,
2530 GCallback highlight_cb,
2531 bool pop_up_p,
2532 bool menu_bar_p,
2533 GtkWidget *topmenu,
2534 xg_menu_cb_data *cl_data,
2535 const char *name)
2536 {
2537 widget_value *item;
2538 GtkWidget *wmenu = topmenu;
2539 GSList *group = NULL;
2540
2541 if (! topmenu)
2542 {
2543 if (! menu_bar_p)
2544 {
2545 wmenu = gtk_menu_new ();
2546 xg_set_screen (wmenu, f);
2547 /* Connect this to the menu instead of items so we get enter/leave for
2548 disabled items also. TODO: Still does not get enter/leave for
2549 disabled items in detached menus. */
2550 g_signal_connect (G_OBJECT (wmenu),
2551 "enter-notify-event",
2552 G_CALLBACK (menuitem_highlight_callback),
2553 NULL);
2554 g_signal_connect (G_OBJECT (wmenu),
2555 "leave-notify-event",
2556 G_CALLBACK (menuitem_highlight_callback),
2557 NULL);
2558 }
2559 else
2560 {
2561 wmenu = gtk_menu_bar_new ();
2562 /* Set width of menu bar to a small value so it doesn't enlarge
2563 a small initial frame size. The width will be set to the
2564 width of the frame later on when it is added to a container.
2565 height -1: Natural height. */
2566 gtk_widget_set_size_request (wmenu, 1, -1);
2567 }
2568
2569 /* Put cl_data on the top menu for easier access. */
2570 cl_data = make_cl_data (cl_data, f, highlight_cb);
2571 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2572 g_signal_connect (G_OBJECT (wmenu), "destroy",
2573 G_CALLBACK (menu_destroy_callback), cl_data);
2574
2575 if (name)
2576 gtk_widget_set_name (wmenu, name);
2577
2578 if (deactivate_cb)
2579 g_signal_connect (G_OBJECT (wmenu),
2580 "selection-done", deactivate_cb, 0);
2581 }
2582
2583 for (item = data; item; item = item->next)
2584 {
2585 GtkWidget *w;
2586
2587 if (pop_up_p && !item->contents && !item->call_data
2588 && !menu_separator_name_p (item->name))
2589 {
2590 char *utf8_label;
2591 /* A title for a popup. We do the same as GTK does when
2592 creating titles, but it does not look good. */
2593 group = NULL;
2594 utf8_label = get_utf8_string (item->name);
2595
2596 w = gtk_menu_item_new_with_label (utf8_label);
2597 gtk_widget_set_sensitive (w, FALSE);
2598 if (utf8_label) g_free (utf8_label);
2599 }
2600 else if (menu_separator_name_p (item->name))
2601 {
2602 group = NULL;
2603 /* GTK only have one separator type. */
2604 w = gtk_separator_menu_item_new ();
2605 }
2606 else
2607 {
2608 w = xg_create_one_menuitem (item,
2609 f,
2610 item->contents ? 0 : select_cb,
2611 highlight_cb,
2612 cl_data,
2613 &group);
2614
2615 /* Create a possibly empty submenu for menu bar items, since some
2616 themes don't highlight items correctly without it. */
2617 if (item->contents || menu_bar_p)
2618 {
2619 GtkWidget *submenu = create_menus (item->contents,
2620 f,
2621 select_cb,
2622 deactivate_cb,
2623 highlight_cb,
2624 0,
2625 0,
2626 0,
2627 cl_data,
2628 0);
2629 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2630 }
2631 }
2632
2633 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2634 gtk_widget_set_name (w, MENU_ITEM_NAME);
2635 }
2636
2637 return wmenu;
2638 }
2639
2640 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2641 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2642 with some text and buttons.
2643 F is the frame the created item belongs to.
2644 NAME is the name to use for the top widget.
2645 VAL is a widget_value structure describing items to be created.
2646 SELECT_CB is the callback to use when a menu item is selected or
2647 a dialog button is pressed.
2648 DEACTIVATE_CB is the callback to use when an item is deactivated.
2649 For a menu, when a sub menu is not shown anymore, for a dialog it is
2650 called when the dialog is popped down.
2651 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2652
2653 Returns the widget created. */
2654
2655 GtkWidget *
2656 xg_create_widget (const char *type, const char *name, struct frame *f,
2657 widget_value *val, GCallback select_cb,
2658 GCallback deactivate_cb, GCallback highlight_cb)
2659 {
2660 GtkWidget *w = 0;
2661 bool menu_bar_p = strcmp (type, "menubar") == 0;
2662 bool pop_up_p = strcmp (type, "popup") == 0;
2663
2664 if (strcmp (type, "dialog") == 0)
2665 {
2666 w = create_dialog (val, select_cb, deactivate_cb);
2667 xg_set_screen (w, f);
2668 gtk_window_set_transient_for (GTK_WINDOW (w),
2669 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2670 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2671 gtk_widget_set_name (w, "emacs-dialog");
2672 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2673 }
2674 else if (menu_bar_p || pop_up_p)
2675 {
2676 w = create_menus (val->contents,
2677 f,
2678 select_cb,
2679 deactivate_cb,
2680 highlight_cb,
2681 pop_up_p,
2682 menu_bar_p,
2683 0,
2684 0,
2685 name);
2686
2687 /* Set the cursor to an arrow for popup menus when they are mapped.
2688 This is done by default for menu bar menus. */
2689 if (pop_up_p)
2690 {
2691 /* Must realize so the GdkWindow inside the widget is created. */
2692 gtk_widget_realize (w);
2693 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2694 }
2695 }
2696 else
2697 {
2698 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2699 type);
2700 }
2701
2702 return w;
2703 }
2704
2705 /* Return the label for menu item WITEM. */
2706
2707 static const char *
2708 xg_get_menu_item_label (GtkMenuItem *witem)
2709 {
2710 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2711 return gtk_label_get_label (wlabel);
2712 }
2713
2714 /* Return true if the menu item WITEM has the text LABEL. */
2715
2716 static bool
2717 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2718 {
2719 bool is_same = 0;
2720 char *utf8_label = get_utf8_string (label);
2721 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2722
2723 if (! old_label && ! utf8_label)
2724 is_same = 1;
2725 else if (old_label && utf8_label)
2726 is_same = strcmp (utf8_label, old_label) == 0;
2727
2728 if (utf8_label) g_free (utf8_label);
2729
2730 return is_same;
2731 }
2732
2733 /* Destroy widgets in LIST. */
2734
2735 static void
2736 xg_destroy_widgets (GList *list)
2737 {
2738 GList *iter;
2739
2740 for (iter = list; iter; iter = g_list_next (iter))
2741 {
2742 GtkWidget *w = GTK_WIDGET (iter->data);
2743
2744 /* Destroying the widget will remove it from the container it is in. */
2745 gtk_widget_destroy (w);
2746 }
2747 }
2748
2749 /* Update the top level names in MENUBAR (i.e. not submenus).
2750 F is the frame the menu bar belongs to.
2751 *LIST is a list with the current menu bar names (menu item widgets).
2752 ITER is the item within *LIST that shall be updated.
2753 POS is the numerical position, starting at 0, of ITER in *LIST.
2754 VAL describes what the menu bar shall look like after the update.
2755 SELECT_CB is the callback to use when a menu item is selected.
2756 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2757 CL_DATA points to the callback data to be used for this menu bar.
2758
2759 This function calls itself to walk through the menu bar names. */
2760
2761 static void
2762 xg_update_menubar (GtkWidget *menubar,
2763 struct frame *f,
2764 GList **list,
2765 GList *iter,
2766 int pos,
2767 widget_value *val,
2768 GCallback select_cb,
2769 GCallback deactivate_cb,
2770 GCallback highlight_cb,
2771 xg_menu_cb_data *cl_data)
2772 {
2773 if (! iter && ! val)
2774 return;
2775 else if (iter && ! val)
2776 {
2777 /* Item(s) have been removed. Remove all remaining items. */
2778 xg_destroy_widgets (iter);
2779
2780 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2781 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2782 gtk_menu_item_new_with_label (""),
2783 0);
2784 /* All updated. */
2785 val = 0;
2786 iter = 0;
2787 }
2788 else if (! iter && val)
2789 {
2790 /* Item(s) added. Add all new items in one call. */
2791 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2792 0, 1, menubar, cl_data, 0);
2793
2794 /* All updated. */
2795 val = 0;
2796 iter = 0;
2797 }
2798 /* Below this neither iter or val is NULL */
2799 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2800 {
2801 /* This item is still the same, check next item. */
2802 val = val->next;
2803 iter = g_list_next (iter);
2804 ++pos;
2805 }
2806 else /* This item is changed. */
2807 {
2808 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2809 GtkMenuItem *witem2 = 0;
2810 bool val_in_menubar = 0;
2811 bool iter_in_new_menubar = 0;
2812 GList *iter2;
2813 widget_value *cur;
2814
2815 /* See if the changed entry (val) is present later in the menu bar */
2816 for (iter2 = iter;
2817 iter2 && ! val_in_menubar;
2818 iter2 = g_list_next (iter2))
2819 {
2820 witem2 = GTK_MENU_ITEM (iter2->data);
2821 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2822 }
2823
2824 /* See if the current entry (iter) is present later in the
2825 specification for the new menu bar. */
2826 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2827 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2828
2829 if (val_in_menubar && ! iter_in_new_menubar)
2830 {
2831 int nr = pos;
2832
2833 /* This corresponds to:
2834 Current: A B C
2835 New: A C
2836 Remove B. */
2837
2838 g_object_ref (G_OBJECT (witem));
2839 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2840 gtk_widget_destroy (GTK_WIDGET (witem));
2841
2842 /* Must get new list since the old changed. */
2843 g_list_free (*list);
2844 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2845 while (nr-- > 0) iter = g_list_next (iter);
2846 }
2847 else if (! val_in_menubar && ! iter_in_new_menubar)
2848 {
2849 /* This corresponds to:
2850 Current: A B C
2851 New: A X C
2852 Rename B to X. This might seem to be a strange thing to do,
2853 since if there is a menu under B it will be totally wrong for X.
2854 But consider editing a C file. Then there is a C-mode menu
2855 (corresponds to B above).
2856 If then doing C-x C-f the minibuf menu (X above) replaces the
2857 C-mode menu. When returning from the minibuffer, we get
2858 back the C-mode menu. Thus we do:
2859 Rename B to X (C-mode to minibuf menu)
2860 Rename X to B (minibuf to C-mode menu).
2861 If the X menu hasn't been invoked, the menu under B
2862 is up to date when leaving the minibuffer. */
2863 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2864 char *utf8_label = get_utf8_string (val->name);
2865
2866 /* GTK menu items don't notice when their labels have been
2867 changed from underneath them, so we have to explicitly
2868 use g_object_notify to tell listeners (e.g., a GMenuModel
2869 bridge that might be loaded) that the item's label has
2870 changed. */
2871 gtk_label_set_text (wlabel, utf8_label);
2872 #if GTK_CHECK_VERSION (2, 16, 0)
2873 g_object_notify (G_OBJECT (witem), "label");
2874 #endif
2875 if (utf8_label) g_free (utf8_label);
2876 iter = g_list_next (iter);
2877 val = val->next;
2878 ++pos;
2879 }
2880 else if (! val_in_menubar && iter_in_new_menubar)
2881 {
2882 /* This corresponds to:
2883 Current: A B C
2884 New: A X B C
2885 Insert X. */
2886
2887 int nr = pos;
2888 GSList *group = 0;
2889 GtkWidget *w = xg_create_one_menuitem (val,
2890 f,
2891 select_cb,
2892 highlight_cb,
2893 cl_data,
2894 &group);
2895
2896 /* Create a possibly empty submenu for menu bar items, since some
2897 themes don't highlight items correctly without it. */
2898 GtkWidget *submenu = create_menus (NULL, f,
2899 select_cb, deactivate_cb,
2900 highlight_cb,
2901 0, 0, 0, cl_data, 0);
2902
2903 gtk_widget_set_name (w, MENU_ITEM_NAME);
2904 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2905 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2906
2907 g_list_free (*list);
2908 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2909 while (nr-- > 0) iter = g_list_next (iter);
2910 iter = g_list_next (iter);
2911 val = val->next;
2912 ++pos;
2913 }
2914 else /* if (val_in_menubar && iter_in_new_menubar) */
2915 {
2916 int nr = pos;
2917 /* This corresponds to:
2918 Current: A B C
2919 New: A C B
2920 Move C before B */
2921
2922 g_object_ref (G_OBJECT (witem2));
2923 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2924 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2925 GTK_WIDGET (witem2), pos);
2926 g_object_unref (G_OBJECT (witem2));
2927
2928 g_list_free (*list);
2929 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2930 while (nr-- > 0) iter = g_list_next (iter);
2931 if (iter) iter = g_list_next (iter);
2932 val = val->next;
2933 ++pos;
2934 }
2935 }
2936
2937 /* Update the rest of the menu bar. */
2938 xg_update_menubar (menubar, f, list, iter, pos, val,
2939 select_cb, deactivate_cb, highlight_cb, cl_data);
2940 }
2941
2942 /* Update the menu item W so it corresponds to VAL.
2943 SELECT_CB is the callback to use when a menu item is selected.
2944 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2945 CL_DATA is the data to set in the widget for menu invocation. */
2946
2947 static void
2948 xg_update_menu_item (widget_value *val,
2949 GtkWidget *w,
2950 GCallback select_cb,
2951 GCallback highlight_cb,
2952 xg_menu_cb_data *cl_data)
2953 {
2954 GtkWidget *wchild;
2955 GtkLabel *wlbl = 0;
2956 GtkLabel *wkey = 0;
2957 char *utf8_label;
2958 char *utf8_key;
2959 const char *old_label = 0;
2960 const char *old_key = 0;
2961 xg_menu_item_cb_data *cb_data;
2962 bool label_changed = false;
2963
2964 wchild = XG_BIN_CHILD (w);
2965 utf8_label = get_utf8_string (val->name);
2966 utf8_key = get_utf8_string (val->key);
2967
2968 /* See if W is a menu item with a key. See make_menu_item above. */
2969 if (GTK_IS_BOX (wchild))
2970 {
2971 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2972
2973 wlbl = GTK_LABEL (list->data);
2974 wkey = GTK_LABEL (list->next->data);
2975 g_list_free (list);
2976
2977 if (! utf8_key)
2978 {
2979 /* Remove the key and keep just the label. */
2980 g_object_ref (G_OBJECT (wlbl));
2981 gtk_container_remove (GTK_CONTAINER (w), wchild);
2982 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2983 g_object_unref (G_OBJECT (wlbl));
2984 wkey = 0;
2985 }
2986
2987 }
2988 else /* Just a label. */
2989 {
2990 wlbl = GTK_LABEL (wchild);
2991
2992 /* Check if there is now a key. */
2993 if (utf8_key)
2994 {
2995 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2996 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2997
2998 wlbl = GTK_LABEL (list->data);
2999 wkey = GTK_LABEL (list->next->data);
3000 g_list_free (list);
3001
3002 gtk_container_remove (GTK_CONTAINER (w), wchild);
3003 gtk_container_add (GTK_CONTAINER (w), wtoadd);
3004 }
3005 }
3006
3007 if (wkey) old_key = gtk_label_get_label (wkey);
3008 if (wlbl) old_label = gtk_label_get_label (wlbl);
3009
3010 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
3011 {
3012 label_changed = true;
3013 gtk_label_set_text (wkey, utf8_key);
3014 }
3015
3016 if (! old_label || strcmp (utf8_label, old_label) != 0)
3017 {
3018 label_changed = true;
3019 gtk_label_set_text (wlbl, utf8_label);
3020 }
3021
3022 if (utf8_key) g_free (utf8_key);
3023 if (utf8_label) g_free (utf8_label);
3024
3025 if (! val->enabled && gtk_widget_get_sensitive (w))
3026 gtk_widget_set_sensitive (w, FALSE);
3027 else if (val->enabled && ! gtk_widget_get_sensitive (w))
3028 gtk_widget_set_sensitive (w, TRUE);
3029
3030 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
3031 if (cb_data)
3032 {
3033 cb_data->call_data = val->call_data;
3034 cb_data->help = val->help;
3035 cb_data->cl_data = cl_data;
3036
3037 /* We assume the callback functions don't change. */
3038 if (val->call_data && ! val->contents)
3039 {
3040 /* This item shall have a select callback. */
3041 if (! cb_data->select_id)
3042 cb_data->select_id
3043 = g_signal_connect (G_OBJECT (w), "activate",
3044 select_cb, cb_data);
3045 }
3046 else if (cb_data->select_id)
3047 {
3048 g_signal_handler_disconnect (w, cb_data->select_id);
3049 cb_data->select_id = 0;
3050 }
3051 }
3052
3053 #if GTK_CHECK_VERSION (2, 16, 0)
3054 if (label_changed) /* See comment in xg_update_menubar. */
3055 g_object_notify (G_OBJECT (w), "label");
3056 #endif
3057 }
3058
3059 /* Update the toggle menu item W so it corresponds to VAL. */
3060
3061 static void
3062 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3063 {
3064 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3065 }
3066
3067 /* Update the radio menu item W so it corresponds to VAL. */
3068
3069 static void
3070 xg_update_radio_item (widget_value *val, GtkWidget *w)
3071 {
3072 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3073 }
3074
3075 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3076 SUBMENU may be NULL, in that case a new menu is created.
3077 F is the frame the menu bar belongs to.
3078 VAL describes the contents of the menu bar.
3079 SELECT_CB is the callback to use when a menu item is selected.
3080 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3081 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3082 CL_DATA is the call back data to use for any newly created items.
3083
3084 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3085 was NULL. */
3086
3087 static GtkWidget *
3088 xg_update_submenu (GtkWidget *submenu,
3089 struct frame *f,
3090 widget_value *val,
3091 GCallback select_cb,
3092 GCallback deactivate_cb,
3093 GCallback highlight_cb,
3094 xg_menu_cb_data *cl_data)
3095 {
3096 GtkWidget *newsub = submenu;
3097 GList *list = 0;
3098 GList *iter;
3099 widget_value *cur;
3100 GList *first_radio = 0;
3101
3102 if (submenu)
3103 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3104
3105 for (cur = val, iter = list;
3106 cur && iter;
3107 iter = g_list_next (iter), cur = cur->next)
3108 {
3109 GtkWidget *w = GTK_WIDGET (iter->data);
3110
3111 /* Remember first radio button in a group. If we get a mismatch in
3112 a radio group we must rebuild the whole group so that the connections
3113 in GTK becomes correct. */
3114 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3115 first_radio = iter;
3116 else if (cur->button_type != BUTTON_TYPE_RADIO
3117 && ! GTK_IS_RADIO_MENU_ITEM (w))
3118 first_radio = 0;
3119
3120 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3121 {
3122 if (! menu_separator_name_p (cur->name))
3123 break;
3124 }
3125 else if (GTK_IS_CHECK_MENU_ITEM (w))
3126 {
3127 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3128 break;
3129 xg_update_toggle_item (cur, w);
3130 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3131 }
3132 else if (GTK_IS_RADIO_MENU_ITEM (w))
3133 {
3134 if (cur->button_type != BUTTON_TYPE_RADIO)
3135 break;
3136 xg_update_radio_item (cur, w);
3137 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3138 }
3139 else if (GTK_IS_MENU_ITEM (w))
3140 {
3141 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3142 GtkWidget *sub;
3143
3144 if (cur->button_type != BUTTON_TYPE_NONE ||
3145 menu_separator_name_p (cur->name))
3146 break;
3147
3148 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3149
3150 sub = gtk_menu_item_get_submenu (witem);
3151 if (sub && ! cur->contents)
3152 {
3153 /* Not a submenu anymore. */
3154 g_object_ref (G_OBJECT (sub));
3155 remove_submenu (witem);
3156 gtk_widget_destroy (sub);
3157 }
3158 else if (cur->contents)
3159 {
3160 GtkWidget *nsub;
3161
3162 nsub = xg_update_submenu (sub, f, cur->contents,
3163 select_cb, deactivate_cb,
3164 highlight_cb, cl_data);
3165
3166 /* If this item just became a submenu, we must set it. */
3167 if (nsub != sub)
3168 gtk_menu_item_set_submenu (witem, nsub);
3169 }
3170 }
3171 else
3172 {
3173 /* Structural difference. Remove everything from here and down
3174 in SUBMENU. */
3175 break;
3176 }
3177 }
3178
3179 /* Remove widgets from first structural change. */
3180 if (iter)
3181 {
3182 /* If we are adding new menu items below, we must remove from
3183 first radio button so that radio groups become correct. */
3184 if (cur && first_radio) xg_destroy_widgets (first_radio);
3185 else xg_destroy_widgets (iter);
3186 }
3187
3188 if (cur)
3189 {
3190 /* More items added. Create them. */
3191 newsub = create_menus (cur,
3192 f,
3193 select_cb,
3194 deactivate_cb,
3195 highlight_cb,
3196 0,
3197 0,
3198 submenu,
3199 cl_data,
3200 0);
3201 }
3202
3203 if (list) g_list_free (list);
3204
3205 return newsub;
3206 }
3207
3208 /* Update the MENUBAR.
3209 F is the frame the menu bar belongs to.
3210 VAL describes the contents of the menu bar.
3211 If DEEP_P, rebuild all but the top level menu names in
3212 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3213 SELECT_CB is the callback to use when a menu item is selected.
3214 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3215 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3216
3217 void
3218 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3219 widget_value *val, bool deep_p,
3220 GCallback select_cb, GCallback deactivate_cb,
3221 GCallback highlight_cb)
3222 {
3223 xg_menu_cb_data *cl_data;
3224 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3225
3226 if (! list) return;
3227
3228 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3229
3230 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3231 select_cb, deactivate_cb, highlight_cb, cl_data);
3232
3233 if (deep_p)
3234 {
3235 widget_value *cur;
3236
3237 /* Update all sub menus.
3238 We must keep the submenus (GTK menu item widgets) since the
3239 X Window in the XEvent that activates the menu are those widgets. */
3240
3241 /* Update cl_data, menu_item things in F may have changed. */
3242 update_cl_data (cl_data, f, highlight_cb);
3243
3244 for (cur = val->contents; cur; cur = cur->next)
3245 {
3246 GList *iter;
3247 GtkWidget *sub = 0;
3248 GtkWidget *newsub;
3249 GtkMenuItem *witem = 0;
3250
3251 /* Find sub menu that corresponds to val and update it. */
3252 for (iter = list ; iter; iter = g_list_next (iter))
3253 {
3254 witem = GTK_MENU_ITEM (iter->data);
3255 if (xg_item_label_same_p (witem, cur->name))
3256 {
3257 sub = gtk_menu_item_get_submenu (witem);
3258 break;
3259 }
3260 }
3261
3262 newsub = xg_update_submenu (sub,
3263 f,
3264 cur->contents,
3265 select_cb,
3266 deactivate_cb,
3267 highlight_cb,
3268 cl_data);
3269 /* sub may still be NULL. If we just updated non deep and added
3270 a new menu bar item, it has no sub menu yet. So we set the
3271 newly created sub menu under witem. */
3272 if (newsub != sub && witem != 0)
3273 {
3274 xg_set_screen (newsub, f);
3275 gtk_menu_item_set_submenu (witem, newsub);
3276 }
3277 }
3278 }
3279
3280 g_list_free (list);
3281 gtk_widget_show_all (menubar);
3282 }
3283
3284 /* Callback called when the menu bar W is mapped.
3285 Used to find the height of the menu bar if we didn't get it
3286 after showing the widget. */
3287
3288 static void
3289 menubar_map_cb (GtkWidget *w, gpointer user_data)
3290 {
3291 GtkRequisition req;
3292 struct frame *f = user_data;
3293 gtk_widget_get_preferred_size (w, NULL, &req);
3294 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3295 {
3296 FRAME_MENUBAR_HEIGHT (f) = req.height;
3297 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3298 }
3299 }
3300
3301 /* Recompute all the widgets of frame F, when the menu bar has been
3302 changed. */
3303
3304 void
3305 xg_update_frame_menubar (struct frame *f)
3306 {
3307 struct x_output *x = f->output_data.x;
3308 GtkRequisition req;
3309
3310 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3311 return;
3312
3313 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3314 return; /* Already done this, happens for frames created invisible. */
3315
3316 block_input ();
3317
3318 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3319 FALSE, FALSE, 0);
3320 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3321
3322 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3323 gtk_widget_show_all (x->menubar_widget);
3324 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3325
3326 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3327 {
3328 FRAME_MENUBAR_HEIGHT (f) = req.height;
3329 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3330 }
3331 unblock_input ();
3332 }
3333
3334 /* Get rid of the menu bar of frame F, and free its storage.
3335 This is used when deleting a frame, and when turning off the menu bar. */
3336
3337 void
3338 free_frame_menubar (struct frame *f)
3339 {
3340 struct x_output *x = f->output_data.x;
3341
3342 if (x->menubar_widget)
3343 {
3344 block_input ();
3345
3346 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3347 /* The menubar and its children shall be deleted when removed from
3348 the container. */
3349 x->menubar_widget = 0;
3350 FRAME_MENUBAR_HEIGHT (f) = 0;
3351 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3352 unblock_input ();
3353 }
3354 }
3355
3356 bool
3357 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3358 {
3359 struct x_output *x = f->output_data.x;
3360 GList *iter;
3361 GdkRectangle rec;
3362 GList *list;
3363 GdkDisplay *gdpy;
3364 GdkWindow *gw;
3365 GdkEvent gevent;
3366 GtkWidget *gwdesc;
3367
3368 if (! x->menubar_widget) return 0;
3369
3370 if (! (event->xbutton.x >= 0
3371 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3372 && event->xbutton.y >= 0
3373 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3374 && event->xbutton.same_screen))
3375 return 0;
3376
3377 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3378 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3379 if (! gw) return 0;
3380 gevent.any.window = gw;
3381 gevent.any.type = GDK_NOTHING;
3382 gwdesc = gtk_get_event_widget (&gevent);
3383 if (! gwdesc) return 0;
3384 if (! GTK_IS_MENU_BAR (gwdesc)
3385 && ! GTK_IS_MENU_ITEM (gwdesc)
3386 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3387 return 0;
3388
3389 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3390 if (! list) return 0;
3391 rec.x = event->xbutton.x;
3392 rec.y = event->xbutton.y;
3393 rec.width = 1;
3394 rec.height = 1;
3395
3396 for (iter = list ; iter; iter = g_list_next (iter))
3397 {
3398 GtkWidget *w = GTK_WIDGET (iter->data);
3399 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3400 break;
3401 }
3402 g_list_free (list);
3403 return iter != 0;
3404 }
3405
3406
3407 \f
3408 /***********************************************************************
3409 Scroll bar functions
3410 ***********************************************************************/
3411
3412
3413 /* Setting scroll bar values invokes the callback. Use this variable
3414 to indicate that callback should do nothing. */
3415
3416 bool xg_ignore_gtk_scrollbar;
3417
3418 /* Width and height of scroll bars for the current theme. */
3419 static int scroll_bar_width_for_theme;
3420 static int scroll_bar_height_for_theme;
3421
3422 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3423 may be larger than 32 bits. Keep a mapping from integer index to widget
3424 pointers to get around the 32 bit limitation. */
3425
3426 static struct
3427 {
3428 GtkWidget **widgets;
3429 ptrdiff_t max_size;
3430 ptrdiff_t used;
3431 } id_to_widget;
3432
3433 /* Grow this much every time we need to allocate more */
3434
3435 #define ID_TO_WIDGET_INCR 32
3436
3437 /* Store the widget pointer W in id_to_widget and return the integer index. */
3438
3439 static ptrdiff_t
3440 xg_store_widget_in_map (GtkWidget *w)
3441 {
3442 ptrdiff_t i;
3443
3444 if (id_to_widget.max_size == id_to_widget.used)
3445 {
3446 ptrdiff_t new_size;
3447 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3448 memory_full (SIZE_MAX);
3449
3450 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3451 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3452 new_size, sizeof (GtkWidget *));
3453
3454 for (i = id_to_widget.max_size; i < new_size; ++i)
3455 id_to_widget.widgets[i] = 0;
3456 id_to_widget.max_size = new_size;
3457 }
3458
3459 /* Just loop over the array and find a free place. After all,
3460 how many scroll bars are we creating? Should be a small number.
3461 The check above guarantees we will find a free place. */
3462 for (i = 0; i < id_to_widget.max_size; ++i)
3463 {
3464 if (! id_to_widget.widgets[i])
3465 {
3466 id_to_widget.widgets[i] = w;
3467 ++id_to_widget.used;
3468
3469 return i;
3470 }
3471 }
3472
3473 /* Should never end up here */
3474 emacs_abort ();
3475 }
3476
3477 /* Remove pointer at IDX from id_to_widget.
3478 Called when scroll bar is destroyed. */
3479
3480 static void
3481 xg_remove_widget_from_map (ptrdiff_t idx)
3482 {
3483 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3484 {
3485 id_to_widget.widgets[idx] = 0;
3486 --id_to_widget.used;
3487 }
3488 }
3489
3490 /* Get the widget pointer at IDX from id_to_widget. */
3491
3492 static GtkWidget *
3493 xg_get_widget_from_map (ptrdiff_t idx)
3494 {
3495 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3496 return id_to_widget.widgets[idx];
3497
3498 return 0;
3499 }
3500
3501 static void
3502 update_theme_scrollbar_width (void)
3503 {
3504 #ifdef HAVE_GTK3
3505 GtkAdjustment *vadj;
3506 #else
3507 GtkObject *vadj;
3508 #endif
3509 GtkWidget *wscroll;
3510 int w = 0, b = 0;
3511
3512 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3513 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3514 g_object_ref_sink (G_OBJECT (wscroll));
3515 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3516 gtk_widget_destroy (wscroll);
3517 g_object_unref (G_OBJECT (wscroll));
3518 w += 2*b;
3519 if (w < 16) w = 16;
3520 scroll_bar_width_for_theme = w;
3521 }
3522
3523 static void
3524 update_theme_scrollbar_height (void)
3525 {
3526 #ifdef HAVE_GTK3
3527 GtkAdjustment *hadj;
3528 #else
3529 GtkObject *hadj;
3530 #endif
3531 GtkWidget *wscroll;
3532 int w = 0, b = 0;
3533
3534 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3535 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3536 g_object_ref_sink (G_OBJECT (wscroll));
3537 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3538 gtk_widget_destroy (wscroll);
3539 g_object_unref (G_OBJECT (wscroll));
3540 w += 2*b;
3541 if (w < 12) w = 12;
3542 scroll_bar_height_for_theme = w;
3543 }
3544
3545 int
3546 xg_get_default_scrollbar_width (void)
3547 {
3548 return scroll_bar_width_for_theme;
3549 }
3550
3551 int
3552 xg_get_default_scrollbar_height (void)
3553 {
3554 /* Apparently there's no default height for themes. */
3555 return scroll_bar_width_for_theme;
3556 }
3557
3558 /* Return the scrollbar id for X Window WID on display DPY.
3559 Return -1 if WID not in id_to_widget. */
3560
3561 ptrdiff_t
3562 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3563 {
3564 ptrdiff_t idx;
3565 GtkWidget *w;
3566
3567 w = xg_win_to_widget (dpy, wid);
3568
3569 if (w)
3570 {
3571 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3572 if (id_to_widget.widgets[idx] == w)
3573 return idx;
3574 }
3575
3576 return -1;
3577 }
3578
3579 /* Callback invoked when scroll bar WIDGET is destroyed.
3580 DATA is the index into id_to_widget for WIDGET.
3581 We free pointer to last scroll bar values here and remove the index. */
3582
3583 static void
3584 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3585 {
3586 intptr_t id = (intptr_t) data;
3587 xg_remove_widget_from_map (id);
3588 }
3589
3590 /* Create a scroll bar widget for frame F. Store the scroll bar
3591 in BAR.
3592 SCROLL_CALLBACK is the callback to invoke when the value of the
3593 bar changes.
3594 END_CALLBACK is the callback to invoke when scrolling ends.
3595 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3596 to set resources for the widget. */
3597
3598 void
3599 xg_create_scroll_bar (struct frame *f,
3600 struct scroll_bar *bar,
3601 GCallback scroll_callback,
3602 GCallback end_callback,
3603 const char *scroll_bar_name)
3604 {
3605 GtkWidget *wscroll;
3606 GtkWidget *webox;
3607 intptr_t scroll_id;
3608 #ifdef HAVE_GTK3
3609 GtkAdjustment *vadj;
3610 #else
3611 GtkObject *vadj;
3612 #endif
3613
3614 /* Page, step increment values are not so important here, they
3615 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3616 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3617 0.1, 0.1, 0.1);
3618
3619 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3620 webox = gtk_event_box_new ();
3621 gtk_widget_set_name (wscroll, scroll_bar_name);
3622 #ifndef HAVE_GTK3
3623 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3624 #endif
3625 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3626
3627 scroll_id = xg_store_widget_in_map (wscroll);
3628
3629 g_signal_connect (G_OBJECT (wscroll),
3630 "destroy",
3631 G_CALLBACK (xg_gtk_scroll_destroy),
3632 (gpointer) scroll_id);
3633 g_signal_connect (G_OBJECT (wscroll),
3634 "change-value",
3635 scroll_callback,
3636 (gpointer) bar);
3637 g_signal_connect (G_OBJECT (wscroll),
3638 "button-release-event",
3639 end_callback,
3640 (gpointer) bar);
3641
3642 /* The scroll bar widget does not draw on a window of its own. Instead
3643 it draws on the parent window, in this case the edit widget. So
3644 whenever the edit widget is cleared, the scroll bar needs to redraw
3645 also, which causes flicker. Put an event box between the edit widget
3646 and the scroll bar, so the scroll bar instead draws itself on the
3647 event box window. */
3648 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3649 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3650
3651
3652 /* Set the cursor to an arrow. */
3653 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3654
3655 bar->x_window = scroll_id;
3656 bar->horizontal = 0;
3657 }
3658
3659 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3660 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3661 of the bar changes. END_CALLBACK is the callback to invoke when
3662 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3663 bar. Can be used to set resources for the widget. */
3664
3665 void
3666 xg_create_horizontal_scroll_bar (struct frame *f,
3667 struct scroll_bar *bar,
3668 GCallback scroll_callback,
3669 GCallback end_callback,
3670 const char *scroll_bar_name)
3671 {
3672 GtkWidget *wscroll;
3673 GtkWidget *webox;
3674 intptr_t scroll_id;
3675 #ifdef HAVE_GTK3
3676 GtkAdjustment *hadj;
3677 #else
3678 GtkObject *hadj;
3679 #endif
3680
3681 /* Page, step increment values are not so important here, they
3682 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3683 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3684 0.1, 0.1, 0.1);
3685
3686 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3687 webox = gtk_event_box_new ();
3688 gtk_widget_set_name (wscroll, scroll_bar_name);
3689 #ifndef HAVE_GTK3
3690 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3691 #endif
3692 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3693
3694 scroll_id = xg_store_widget_in_map (wscroll);
3695
3696 g_signal_connect (G_OBJECT (wscroll),
3697 "destroy",
3698 G_CALLBACK (xg_gtk_scroll_destroy),
3699 (gpointer) scroll_id);
3700 g_signal_connect (G_OBJECT (wscroll),
3701 "change-value",
3702 scroll_callback,
3703 (gpointer) bar);
3704 g_signal_connect (G_OBJECT (wscroll),
3705 "button-release-event",
3706 end_callback,
3707 (gpointer) bar);
3708
3709 /* The scroll bar widget does not draw on a window of its own. Instead
3710 it draws on the parent window, in this case the edit widget. So
3711 whenever the edit widget is cleared, the scroll bar needs to redraw
3712 also, which causes flicker. Put an event box between the edit widget
3713 and the scroll bar, so the scroll bar instead draws itself on the
3714 event box window. */
3715 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3716 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3717
3718
3719 /* Set the cursor to an arrow. */
3720 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3721
3722 bar->x_window = scroll_id;
3723 bar->horizontal = 1;
3724 }
3725
3726 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3727
3728 void
3729 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3730 {
3731 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3732 if (w)
3733 {
3734 GtkWidget *wparent = gtk_widget_get_parent (w);
3735 gtk_widget_destroy (w);
3736 gtk_widget_destroy (wparent);
3737 SET_FRAME_GARBAGED (f);
3738 }
3739 }
3740
3741 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3742 in frame F.
3743 TOP/LEFT are the new pixel positions where the bar shall appear.
3744 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3745
3746 void
3747 xg_update_scrollbar_pos (struct frame *f,
3748 ptrdiff_t scrollbar_id,
3749 int top,
3750 int left,
3751 int width,
3752 int height)
3753 {
3754
3755 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3756
3757 if (wscroll)
3758 {
3759 GtkWidget *wfixed = f->output_data.x->edit_widget;
3760 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3761 gint msl;
3762
3763 /* Clear out old position. */
3764 int oldx = -1, oldy = -1, oldw, oldh;
3765 if (gtk_widget_get_parent (wparent) == wfixed)
3766 {
3767 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3768 "x", &oldx, "y", &oldy, NULL);
3769 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3770 }
3771
3772 /* Move and resize to new values. */
3773 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3774 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3775 if (msl > height)
3776 {
3777 /* No room. Hide scroll bar as some themes output a warning if
3778 the height is less than the min size. */
3779 gtk_widget_hide (wparent);
3780 gtk_widget_hide (wscroll);
3781 }
3782 else
3783 {
3784 gtk_widget_show_all (wparent);
3785 gtk_widget_set_size_request (wscroll, width, height);
3786 }
3787 gtk_widget_queue_draw (wfixed);
3788 gdk_window_process_all_updates ();
3789 if (oldx != -1 && oldw > 0 && oldh > 0)
3790 /* Clear under old scroll bar position. This must be done after
3791 the gtk_widget_queue_draw and gdk_window_process_all_updates
3792 above. */
3793 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3794 oldx, oldy, oldw, oldh);
3795
3796 /* GTK does not redraw until the main loop is entered again, but
3797 if there are no X events pending we will not enter it. So we sync
3798 here to get some events. */
3799
3800 x_sync (f);
3801 SET_FRAME_GARBAGED (f);
3802 cancel_mouse_face (f);
3803 }
3804 }
3805
3806
3807 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3808 in frame F.
3809 TOP/LEFT are the new pixel positions where the bar shall appear.
3810 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3811
3812 void
3813 xg_update_horizontal_scrollbar_pos (struct frame *f,
3814 ptrdiff_t scrollbar_id,
3815 int top,
3816 int left,
3817 int width,
3818 int height)
3819 {
3820
3821 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3822
3823 if (wscroll)
3824 {
3825 GtkWidget *wfixed = f->output_data.x->edit_widget;
3826 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3827 gint msl;
3828
3829 /* Clear out old position. */
3830 int oldx = -1, oldy = -1, oldw, oldh;
3831 if (gtk_widget_get_parent (wparent) == wfixed)
3832 {
3833 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3834 "x", &oldx, "y", &oldy, NULL);
3835 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3836 }
3837
3838 /* Move and resize to new values. */
3839 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3840 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3841 if (msl > width)
3842 {
3843 /* No room. Hide scroll bar as some themes output a warning if
3844 the width is less than the min size. */
3845 gtk_widget_hide (wparent);
3846 gtk_widget_hide (wscroll);
3847 }
3848 else
3849 {
3850 gtk_widget_show_all (wparent);
3851 gtk_widget_set_size_request (wscroll, width, height);
3852 }
3853 gtk_widget_queue_draw (wfixed);
3854 gdk_window_process_all_updates ();
3855 if (oldx != -1 && oldw > 0 && oldh > 0)
3856 /* Clear under old scroll bar position. This must be done after
3857 the gtk_widget_queue_draw and gdk_window_process_all_updates
3858 above. */
3859 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3860 oldx, oldy, oldw, oldh);
3861
3862 /* GTK does not redraw until the main loop is entered again, but
3863 if there are no X events pending we will not enter it. So we sync
3864 here to get some events. */
3865
3866 x_sync (f);
3867 SET_FRAME_GARBAGED (f);
3868 cancel_mouse_face (f);
3869 }
3870 }
3871
3872
3873 /* Get the current value of the range, truncated to an integer. */
3874
3875 static int
3876 int_gtk_range_get_value (GtkRange *range)
3877 {
3878 return gtk_range_get_value (range);
3879 }
3880
3881
3882 /* Set the thumb size and position of scroll bar BAR. We are currently
3883 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3884
3885 void
3886 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3887 int portion,
3888 int position,
3889 int whole)
3890 {
3891 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3892
3893 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3894
3895 if (wscroll && bar->dragging == -1)
3896 {
3897 GtkAdjustment *adj;
3898 gdouble shown;
3899 gdouble top;
3900 int size, value;
3901 int old_size;
3902 int new_step;
3903 bool changed = 0;
3904
3905 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3906
3907 if (scroll_bar_adjust_thumb_portion_p)
3908 {
3909 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3910 line rather than the real portion value. This makes the
3911 thumb less likely to resize and that looks better. */
3912 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3913
3914 /* When the thumb is at the bottom, position == whole.
3915 So we need to increase `whole' to make space for the thumb. */
3916 whole += portion;
3917 }
3918
3919 if (whole <= 0)
3920 top = 0, shown = 1;
3921 else
3922 {
3923 top = (gdouble) position / whole;
3924 shown = (gdouble) portion / whole;
3925 }
3926
3927 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3928 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3929
3930 /* Assume all lines are of equal size. */
3931 new_step = size / max (1, FRAME_LINES (f));
3932
3933 old_size = gtk_adjustment_get_page_size (adj);
3934 if (old_size != size)
3935 {
3936 int old_step = gtk_adjustment_get_step_increment (adj);
3937 if (old_step != new_step)
3938 {
3939 gtk_adjustment_set_page_size (adj, size);
3940 gtk_adjustment_set_step_increment (adj, new_step);
3941 /* Assume a page increment is about 95% of the page size */
3942 gtk_adjustment_set_page_increment (adj, size - size / 20);
3943 changed = 1;
3944 }
3945 }
3946
3947 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3948 {
3949 block_input ();
3950
3951 /* gtk_range_set_value invokes the callback. Set
3952 ignore_gtk_scrollbar to make the callback do nothing */
3953 xg_ignore_gtk_scrollbar = 1;
3954
3955 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3956 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3957 else if (changed)
3958 gtk_adjustment_changed (adj);
3959
3960 xg_ignore_gtk_scrollbar = 0;
3961
3962 unblock_input ();
3963 }
3964 }
3965 }
3966
3967 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3968 currently displaying PORTION out of a whole WHOLE, and our position
3969 POSITION. */
3970 void
3971 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3972 int portion,
3973 int position,
3974 int whole)
3975 {
3976 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3977
3978 if (wscroll && bar->dragging == -1)
3979 {
3980 GtkAdjustment *adj;
3981 int lower = 0;
3982 int upper = max (whole - 1, 0);
3983 int pagesize = min (upper, max (portion, 0));
3984 int value = max (0, min (position, upper - pagesize));
3985 /* These should be set to something more <portion, whole>
3986 related. */
3987 int page_increment = 4;
3988 int step_increment = 1;
3989
3990 block_input ();
3991 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3992 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
3993 (gdouble) upper, (gdouble) step_increment,
3994 (gdouble) page_increment, (gdouble) pagesize);
3995 gtk_adjustment_changed (adj);
3996 unblock_input ();
3997 }
3998 }
3999
4000 /* Return true if EVENT is for a scroll bar in frame F.
4001 When the same X window is used for several Gtk+ widgets, we cannot
4002 say for sure based on the X window alone if an event is for the
4003 frame. This function does additional checks. */
4004
4005 bool
4006 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
4007 {
4008 bool retval = 0;
4009
4010 if (f && event->type == ButtonPress && event->xbutton.button < 4)
4011 {
4012 /* Check if press occurred outside the edit widget. */
4013 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
4014 GdkWindow *gwin;
4015 #ifdef HAVE_GTK3
4016 GdkDevice *gdev = gdk_device_manager_get_client_pointer
4017 (gdk_display_get_device_manager (gdpy));
4018 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
4019 #else
4020 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
4021 #endif
4022 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
4023 }
4024 else if (f
4025 && ((event->type == ButtonRelease && event->xbutton.button < 4)
4026 || event->type == MotionNotify))
4027 {
4028 /* If we are releasing or moving the scroll bar, it has the grab. */
4029 GtkWidget *w = gtk_grab_get_current ();
4030 retval = w != 0 && GTK_IS_SCROLLBAR (w);
4031 }
4032
4033 return retval;
4034 }
4035
4036
4037 \f
4038 /***********************************************************************
4039 Tool bar functions
4040 ***********************************************************************/
4041 /* The key for the data we put in the GtkImage widgets. The data is
4042 the image used by Emacs. We use this to see if we need to update
4043 the GtkImage with a new image. */
4044 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
4045
4046 /* The key for storing the latest modifiers so the activate callback can
4047 get them. */
4048 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4049
4050 /* The key for the data we put in the GtkImage widgets. The data is
4051 the stock name used by Emacs. We use this to see if we need to update
4052 the GtkImage with a new image. */
4053 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4054
4055 /* As above, but this is used for named theme widgets, as opposed to
4056 stock items. */
4057 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4058
4059 /* Callback function invoked when a tool bar item is pressed.
4060 W is the button widget in the tool bar that got pressed,
4061 CLIENT_DATA is an integer that is the index of the button in the
4062 tool bar. 0 is the first button. */
4063
4064 static gboolean
4065 xg_tool_bar_button_cb (GtkWidget *widget,
4066 GdkEventButton *event,
4067 gpointer user_data)
4068 {
4069 intptr_t state = event->state;
4070 gpointer ptr = (gpointer) state;
4071 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4072 return FALSE;
4073 }
4074
4075
4076 /* Callback function invoked when a tool bar item is pressed.
4077 W is the button widget in the tool bar that got pressed,
4078 CLIENT_DATA is an integer that is the index of the button in the
4079 tool bar. 0 is the first button. */
4080
4081 static void
4082 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4083 {
4084 intptr_t idx = (intptr_t) client_data;
4085 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4086 intptr_t mod = (intptr_t) gmod;
4087
4088 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4089 Lisp_Object key, frame;
4090 struct input_event event;
4091 EVENT_INIT (event);
4092
4093 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4094 return;
4095
4096 idx *= TOOL_BAR_ITEM_NSLOTS;
4097
4098 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4099 XSETFRAME (frame, f);
4100
4101 /* We generate two events here. The first one is to set the prefix
4102 to `(tool_bar)', see keyboard.c. */
4103 event.kind = TOOL_BAR_EVENT;
4104 event.frame_or_window = frame;
4105 event.arg = frame;
4106 kbd_buffer_store_event (&event);
4107
4108 event.kind = TOOL_BAR_EVENT;
4109 event.frame_or_window = frame;
4110 event.arg = key;
4111 /* Convert between the modifier bits GDK uses and the modifier bits
4112 Emacs uses. This assumes GDK and X masks are the same, which they are when
4113 this is written. */
4114 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4115 kbd_buffer_store_event (&event);
4116
4117 /* Return focus to the frame after we have clicked on a detached
4118 tool bar button. */
4119 x_focus_frame (f);
4120 }
4121
4122 static GtkWidget *
4123 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4124 {
4125 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4126 GtkWidget *c1 = clist->data;
4127 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4128
4129 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4130 g_list_free (clist);
4131 return GTK_IS_LABEL (c1) ? c1 : c2;
4132 }
4133
4134
4135 /* This callback is called when the mouse enters or leaves a tool bar item.
4136 It is used for displaying and hiding the help text.
4137 W is the tool bar item, a button.
4138 EVENT is either an enter event or leave event.
4139 CLIENT_DATA is an integer that is the index of the button in the
4140 tool bar. 0 is the first button.
4141
4142 Returns FALSE to tell GTK to keep processing this event. */
4143
4144 static gboolean
4145 xg_tool_bar_help_callback (GtkWidget *w,
4146 GdkEventCrossing *event,
4147 gpointer client_data)
4148 {
4149 intptr_t idx = (intptr_t) client_data;
4150 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4151 Lisp_Object help, frame;
4152
4153 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4154 return FALSE;
4155
4156 if (event->type == GDK_ENTER_NOTIFY)
4157 {
4158 idx *= TOOL_BAR_ITEM_NSLOTS;
4159 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4160
4161 if (NILP (help))
4162 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4163 }
4164 else
4165 help = Qnil;
4166
4167 XSETFRAME (frame, f);
4168 kbd_buffer_store_help_event (frame, help);
4169
4170 return FALSE;
4171 }
4172
4173
4174 /* This callback is called when a tool bar item shall be redrawn.
4175 It modifies the expose event so that the GtkImage widget redraws the
4176 whole image. This to overcome a bug that makes GtkImage draw the image
4177 in the wrong place when it tries to redraw just a part of the image.
4178 W is the GtkImage to be redrawn.
4179 EVENT is the expose event for W.
4180 CLIENT_DATA is unused.
4181
4182 Returns FALSE to tell GTK to keep processing this event. */
4183
4184 #ifndef HAVE_GTK3
4185 static gboolean
4186 xg_tool_bar_item_expose_callback (GtkWidget *w,
4187 GdkEventExpose *event,
4188 gpointer client_data)
4189 {
4190 gint width, height;
4191
4192 gdk_drawable_get_size (event->window, &width, &height);
4193 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4194 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4195
4196 event->area.x = max (0, event->area.x);
4197 event->area.y = max (0, event->area.y);
4198
4199 event->area.width = max (width, event->area.width);
4200 event->area.height = max (height, event->area.height);
4201
4202 return FALSE;
4203 }
4204 #endif
4205
4206 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4207 #define toolbar_set_orientation(w, o) \
4208 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4209 #else
4210 #define toolbar_set_orientation(w, o) \
4211 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4212 #endif
4213
4214 /* Attach a tool bar to frame F. */
4215
4216 static void
4217 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4218 {
4219 struct x_output *x = f->output_data.x;
4220 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4221 GtkWidget *top_widget = x->toolbar_widget;
4222
4223 toolbar_set_orientation (x->toolbar_widget,
4224 into_hbox
4225 ? GTK_ORIENTATION_VERTICAL
4226 : GTK_ORIENTATION_HORIZONTAL);
4227
4228 if (into_hbox)
4229 {
4230 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4231 FALSE, FALSE, 0);
4232
4233 if (EQ (pos, Qleft))
4234 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4235 top_widget,
4236 0);
4237 x->toolbar_in_hbox = true;
4238 }
4239 else
4240 {
4241 bool vbox_pos = x->menubar_widget != 0;
4242 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4243 FALSE, FALSE, 0);
4244
4245 if (EQ (pos, Qtop))
4246 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4247 top_widget,
4248 vbox_pos);
4249 x->toolbar_in_hbox = false;
4250 }
4251 x->toolbar_is_packed = true;
4252 }
4253
4254 static bool xg_update_tool_bar_sizes (struct frame *f);
4255
4256 static void
4257 tb_size_cb (GtkWidget *widget,
4258 GdkRectangle *allocation,
4259 gpointer user_data)
4260 {
4261 /* When tool bar is created it has one preferred size. But when size is
4262 allocated between widgets, it may get another. So we must update
4263 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4264 struct frame *f = user_data;
4265
4266 if (xg_update_tool_bar_sizes (f))
4267 {
4268 frame_size_history_add (f, Qtb_size_cb, 0, 0, Qnil);
4269 adjust_frame_size (f, -1, -1, 5, 0, Qtool_bar_lines);
4270 }
4271 }
4272
4273 /* Create a tool bar for frame F. */
4274
4275 static void
4276 xg_create_tool_bar (struct frame *f)
4277 {
4278 struct x_output *x = f->output_data.x;
4279 #if GTK_CHECK_VERSION (3, 3, 6)
4280 GtkStyleContext *gsty;
4281 #endif
4282 struct xg_frame_tb_info *tbinfo
4283 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4284 TB_INFO_KEY);
4285 if (! tbinfo)
4286 {
4287 tbinfo = xmalloc (sizeof (*tbinfo));
4288 tbinfo->last_tool_bar = Qnil;
4289 tbinfo->style = Qnil;
4290 tbinfo->hmargin = tbinfo->vmargin = 0;
4291 tbinfo->dir = GTK_TEXT_DIR_NONE;
4292 tbinfo->n_last_items = 0;
4293 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4294 TB_INFO_KEY,
4295 tbinfo);
4296 }
4297
4298 x->toolbar_widget = gtk_toolbar_new ();
4299
4300 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4301
4302 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4303 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4304 g_signal_connect (x->toolbar_widget, "size-allocate",
4305 G_CALLBACK (tb_size_cb), f);
4306 #if GTK_CHECK_VERSION (3, 3, 6)
4307 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4308 gtk_style_context_add_class (gsty, "primary-toolbar");
4309 #endif
4310 }
4311
4312
4313 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4314
4315 /* Find the right-to-left image named by RTL in the tool bar images for F.
4316 Returns IMAGE if RTL is not found. */
4317
4318 static Lisp_Object
4319 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4320 {
4321 int i;
4322 Lisp_Object file, rtl_name;
4323 struct gcpro gcpro1, gcpro2;
4324 GCPRO2 (file, rtl_name);
4325
4326 rtl_name = Ffile_name_nondirectory (rtl);
4327
4328 for (i = 0; i < f->n_tool_bar_items; ++i)
4329 {
4330 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4331 if (!NILP (file = file_for_image (rtl_image)))
4332 {
4333 file = call1 (intern ("file-name-sans-extension"),
4334 Ffile_name_nondirectory (file));
4335 if (! NILP (Fequal (file, rtl_name)))
4336 {
4337 image = rtl_image;
4338 break;
4339 }
4340 }
4341 }
4342
4343 return image;
4344 }
4345
4346 static GtkToolItem *
4347 xg_make_tool_item (struct frame *f,
4348 GtkWidget *wimage,
4349 GtkWidget **wbutton,
4350 const char *label,
4351 int i, bool horiz, bool text_image)
4352 {
4353 GtkToolItem *ti = gtk_tool_item_new ();
4354 GtkWidget *vb = gtk_box_new (horiz
4355 ? GTK_ORIENTATION_HORIZONTAL
4356 : GTK_ORIENTATION_VERTICAL,
4357 0);
4358 GtkWidget *wb = gtk_button_new ();
4359 /* The eventbox is here so we can have tooltips on disabled items. */
4360 GtkWidget *weventbox = gtk_event_box_new ();
4361 #if GTK_CHECK_VERSION (3, 3, 6)
4362 GtkCssProvider *css_prov = gtk_css_provider_new ();
4363 GtkStyleContext *gsty;
4364
4365 gtk_css_provider_load_from_data (css_prov,
4366 "GtkEventBox {"
4367 " background-color: transparent;"
4368 "}",
4369 -1, NULL);
4370
4371 gsty = gtk_widget_get_style_context (weventbox);
4372 gtk_style_context_add_provider (gsty,
4373 GTK_STYLE_PROVIDER (css_prov),
4374 GTK_STYLE_PROVIDER_PRIORITY_USER);
4375 g_object_unref (css_prov);
4376 #endif
4377
4378 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4379
4380 if (wimage && !text_image)
4381 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4382 if (label)
4383 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4384 if (wimage && text_image)
4385 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4386
4387 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4388 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4389 gtk_container_add (GTK_CONTAINER (wb), vb);
4390 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4391 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4392
4393 if (wimage || label)
4394 {
4395 intptr_t ii = i;
4396 gpointer gi = (gpointer) ii;
4397
4398 g_signal_connect (G_OBJECT (wb), "clicked",
4399 G_CALLBACK (xg_tool_bar_callback),
4400 gi);
4401
4402 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4403
4404 #ifndef HAVE_GTK3
4405 /* Catch expose events to overcome an annoying redraw bug, see
4406 comment for xg_tool_bar_item_expose_callback. */
4407 g_signal_connect (G_OBJECT (ti),
4408 "expose-event",
4409 G_CALLBACK (xg_tool_bar_item_expose_callback),
4410 0);
4411 #endif
4412 gtk_tool_item_set_homogeneous (ti, FALSE);
4413
4414 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4415 no distinction based on modifiers in the activate callback,
4416 so we have to do it ourselves. */
4417 g_signal_connect (wb, "button-release-event",
4418 G_CALLBACK (xg_tool_bar_button_cb),
4419 NULL);
4420
4421 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4422
4423 /* Use enter/leave notify to show help. We use the events
4424 rather than the GtkButton specific signals "enter" and
4425 "leave", so we can have only one callback. The event
4426 will tell us what kind of event it is. */
4427 g_signal_connect (G_OBJECT (weventbox),
4428 "enter-notify-event",
4429 G_CALLBACK (xg_tool_bar_help_callback),
4430 gi);
4431 g_signal_connect (G_OBJECT (weventbox),
4432 "leave-notify-event",
4433 G_CALLBACK (xg_tool_bar_help_callback),
4434 gi);
4435 }
4436
4437 if (wbutton) *wbutton = wb;
4438
4439 return ti;
4440 }
4441
4442 static bool
4443 is_box_type (GtkWidget *vb, bool is_horizontal)
4444 {
4445 #ifdef HAVE_GTK3
4446 bool ret = 0;
4447 if (GTK_IS_BOX (vb))
4448 {
4449 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4450 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4451 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4452 }
4453 return ret;
4454 #else
4455 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4456 #endif
4457 }
4458
4459
4460 static bool
4461 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4462 const char *icon_name, const struct image *img,
4463 const char *label, bool horiz)
4464 {
4465 gpointer old;
4466 GtkWidget *wimage;
4467 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4468 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4469
4470 /* Check if the tool icon matches. */
4471 if (stock_name && wimage)
4472 {
4473 old = g_object_get_data (G_OBJECT (wimage),
4474 XG_TOOL_BAR_STOCK_NAME);
4475 if (!old || strcmp (old, stock_name))
4476 return 1;
4477 }
4478 else if (icon_name && wimage)
4479 {
4480 old = g_object_get_data (G_OBJECT (wimage),
4481 XG_TOOL_BAR_ICON_NAME);
4482 if (!old || strcmp (old, icon_name))
4483 return 1;
4484 }
4485 else if (wimage)
4486 {
4487 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4488 XG_TOOL_BAR_IMAGE_DATA);
4489 Pixmap old_img = (Pixmap) gold_img;
4490 if (old_img != img->pixmap)
4491 return 1;
4492 }
4493
4494 /* Check button configuration and label. */
4495 if (is_box_type (vb, horiz)
4496 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4497 return 1;
4498
4499 /* Ensure label is correct. */
4500 if (label && wlbl)
4501 gtk_label_set_text (GTK_LABEL (wlbl), label);
4502 return 0;
4503 }
4504
4505 static bool
4506 xg_update_tool_bar_sizes (struct frame *f)
4507 {
4508 struct x_output *x = f->output_data.x;
4509 GtkRequisition req;
4510 int nl = 0, nr = 0, nt = 0, nb = 0;
4511 GtkWidget *top_widget = x->toolbar_widget;
4512
4513 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4514 if (x->toolbar_in_hbox)
4515 {
4516 int pos;
4517 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4518 top_widget,
4519 "position", &pos, NULL);
4520 if (pos == 0) nl = req.width;
4521 else nr = req.width;
4522 }
4523 else
4524 {
4525 int pos;
4526 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4527 top_widget,
4528 "position", &pos, NULL);
4529 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4530 else nb = req.height;
4531 }
4532
4533 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4534 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4535 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4536 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4537 {
4538 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4539 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4540 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4541 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4542 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4543 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4544
4545 return true;
4546 }
4547 else
4548 return false;
4549 }
4550
4551 static char *
4552 find_icon_from_name (char *name,
4553 GtkIconTheme *icon_theme,
4554 char **icon_name)
4555 {
4556 #if ! GTK_CHECK_VERSION (3, 10, 0)
4557 GtkStockItem stock_item;
4558 #endif
4559
4560 if (name[0] == 'n' && name[1] == ':')
4561 {
4562 *icon_name = name + 2;
4563 name = NULL;
4564
4565 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4566 *icon_name = NULL;
4567 }
4568
4569 #if ! GTK_CHECK_VERSION (3, 10, 0)
4570 else if (gtk_stock_lookup (name, &stock_item))
4571 *icon_name = NULL;
4572 #endif
4573 else if (gtk_icon_theme_has_icon (icon_theme, name))
4574 {
4575 *icon_name = name;
4576 name = NULL;
4577 }
4578 else
4579 {
4580 name = NULL;
4581 *icon_name = NULL;
4582 }
4583
4584 return name;
4585 }
4586
4587
4588 /* Update the tool bar for frame F. Add new buttons and remove old. */
4589
4590 void
4591 update_frame_tool_bar (struct frame *f)
4592 {
4593 int i, j;
4594 struct x_output *x = f->output_data.x;
4595 int hmargin = 0, vmargin = 0;
4596 GtkToolbar *wtoolbar;
4597 GtkToolItem *ti;
4598 GtkTextDirection dir;
4599 Lisp_Object style;
4600 bool text_image, horiz;
4601 struct xg_frame_tb_info *tbinfo;
4602 GdkScreen *screen;
4603 GtkIconTheme *icon_theme;
4604
4605
4606 if (! FRAME_GTK_WIDGET (f))
4607 return;
4608
4609 block_input ();
4610
4611 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4612 {
4613 hmargin = XFASTINT (Vtool_bar_button_margin);
4614 vmargin = XFASTINT (Vtool_bar_button_margin);
4615 }
4616 else if (CONSP (Vtool_bar_button_margin))
4617 {
4618 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4619 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4620
4621 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4622 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4623 }
4624
4625 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4626 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4627 i.e. zero. This means that margins less than
4628 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4629 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4630 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4631
4632 if (! x->toolbar_widget)
4633 xg_create_tool_bar (f);
4634
4635 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4636 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4637
4638 style = Ftool_bar_get_system_style ();
4639 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4640 icon_theme = gtk_icon_theme_get_for_screen (screen);
4641
4642 /* Are we up to date? */
4643 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4644 TB_INFO_KEY);
4645
4646 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4647 && tbinfo->n_last_items == f->n_tool_bar_items
4648 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4649 && tbinfo->dir == dir
4650 && ! NILP (Fequal (tbinfo->style, style))
4651 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4652 {
4653 unblock_input ();
4654 return;
4655 }
4656
4657 tbinfo->last_tool_bar = f->tool_bar_items;
4658 tbinfo->n_last_items = f->n_tool_bar_items;
4659 tbinfo->style = style;
4660 tbinfo->hmargin = hmargin;
4661 tbinfo->vmargin = vmargin;
4662 tbinfo->dir = dir;
4663
4664 text_image = EQ (style, Qtext_image_horiz);
4665 horiz = EQ (style, Qboth_horiz) || text_image;
4666
4667 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4668 {
4669 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4670 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4671 int idx;
4672 ptrdiff_t img_id;
4673 int icon_size = 0;
4674 struct image *img = NULL;
4675 Lisp_Object image;
4676 Lisp_Object stock = Qnil;
4677 char *stock_name = NULL;
4678 char *icon_name = NULL;
4679 Lisp_Object rtl;
4680 GtkWidget *wbutton = NULL;
4681 Lisp_Object specified_file;
4682 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4683 const char *label
4684 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4685 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4686 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4687 : "";
4688
4689 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4690
4691 /* If this is a separator, use a gtk separator item. */
4692 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4693 {
4694 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4695 {
4696 if (ti)
4697 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4698 GTK_WIDGET (ti));
4699 ti = gtk_separator_tool_item_new ();
4700 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4701 }
4702 j++;
4703 continue;
4704 }
4705
4706 /* Otherwise, the tool-bar item is an ordinary button. */
4707
4708 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4709 {
4710 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4711 ti = NULL;
4712 }
4713
4714 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4715
4716 /* Ignore invalid image specifications. */
4717 image = PROP (TOOL_BAR_ITEM_IMAGES);
4718 if (!valid_image_p (image))
4719 {
4720 if (ti)
4721 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4722 GTK_WIDGET (ti));
4723 continue;
4724 }
4725
4726 specified_file = file_for_image (image);
4727 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4728 stock = call1 (Qx_gtk_map_stock, specified_file);
4729
4730 if (CONSP (stock))
4731 {
4732 Lisp_Object tem;
4733 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4734 if (! NILP (tem) && STRINGP (XCAR (tem)))
4735 {
4736 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4737 icon_theme,
4738 &icon_name);
4739 if (stock_name || icon_name) break;
4740 }
4741 }
4742 else if (STRINGP (stock))
4743 {
4744 stock_name = find_icon_from_name (SSDATA (stock),
4745 icon_theme,
4746 &icon_name);
4747 }
4748
4749 if (stock_name || icon_name)
4750 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4751
4752 if (stock_name == NULL && icon_name == NULL)
4753 {
4754 /* No stock image, or stock item not known. Try regular
4755 image. If image is a vector, choose it according to the
4756 button state. */
4757 if (dir == GTK_TEXT_DIR_RTL
4758 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4759 && STRINGP (rtl))
4760 image = find_rtl_image (f, image, rtl);
4761
4762 if (VECTORP (image))
4763 {
4764 if (enabled_p)
4765 idx = (selected_p
4766 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4767 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4768 else
4769 idx = (selected_p
4770 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4771 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4772
4773 eassert (ASIZE (image) >= idx);
4774 image = AREF (image, idx);
4775 }
4776 else
4777 idx = -1;
4778
4779 img_id = lookup_image (f, image);
4780 img = IMAGE_FROM_ID (f, img_id);
4781 prepare_image_for_display (f, img);
4782
4783 if (img->load_failed_p || img->pixmap == None)
4784 {
4785 if (ti)
4786 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4787 GTK_WIDGET (ti));
4788 continue;
4789 }
4790 }
4791
4792 /* If there is an existing widget, check if it's stale; if so,
4793 remove it and make a new tool item from scratch. */
4794 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4795 img, label, horiz))
4796 {
4797 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4798 GTK_WIDGET (ti));
4799 ti = NULL;
4800 }
4801
4802 if (ti == NULL)
4803 {
4804 GtkWidget *w;
4805
4806 /* Save the image so we can see if an update is needed the
4807 next time we call xg_tool_item_match_p. */
4808 if (EQ (style, Qtext))
4809 w = NULL;
4810 else if (stock_name)
4811 {
4812
4813 #if GTK_CHECK_VERSION (3, 10, 0)
4814 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4815 #else
4816 w = gtk_image_new_from_stock (stock_name, icon_size);
4817 #endif
4818 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4819 (gpointer) xstrdup (stock_name),
4820 (GDestroyNotify) xfree);
4821 }
4822 else if (icon_name)
4823 {
4824 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4825 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4826 (gpointer) xstrdup (icon_name),
4827 (GDestroyNotify) xfree);
4828 }
4829 else
4830 {
4831 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4832 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4833 (gpointer)img->pixmap);
4834 }
4835
4836 #if GTK_CHECK_VERSION (3, 14, 0)
4837 if (w)
4838 {
4839 gtk_widget_set_margin_start (w, hmargin);
4840 gtk_widget_set_margin_end (w, hmargin);
4841 gtk_widget_set_margin_top (w, vmargin);
4842 gtk_widget_set_margin_bottom (w, vmargin);
4843 }
4844 #else
4845 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4846 #endif
4847 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4848 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4849 }
4850
4851 #undef PROP
4852
4853 gtk_widget_set_sensitive (wbutton, enabled_p);
4854 j++;
4855 }
4856
4857 /* Remove buttons not longer needed. */
4858 do
4859 {
4860 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4861 if (ti)
4862 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4863 } while (ti != NULL);
4864
4865 if (f->n_tool_bar_items != 0)
4866 {
4867 if (! x->toolbar_is_packed)
4868 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4869 gtk_widget_show_all (x->toolbar_widget);
4870 if (xg_update_tool_bar_sizes (f))
4871 {
4872 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
4873 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4874 }
4875 }
4876
4877 unblock_input ();
4878 }
4879
4880 /* Deallocate all resources for the tool bar on frame F.
4881 Remove the tool bar. */
4882
4883 void
4884 free_frame_tool_bar (struct frame *f)
4885 {
4886 struct x_output *x = f->output_data.x;
4887
4888 if (x->toolbar_widget)
4889 {
4890 struct xg_frame_tb_info *tbinfo;
4891 GtkWidget *top_widget = x->toolbar_widget;
4892
4893 block_input ();
4894 if (x->toolbar_is_packed)
4895 {
4896 if (x->toolbar_in_hbox)
4897 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4898 top_widget);
4899 else
4900 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4901 top_widget);
4902 }
4903 else
4904 gtk_widget_destroy (x->toolbar_widget);
4905
4906 x->toolbar_widget = 0;
4907 x->toolbar_widget = 0;
4908 x->toolbar_is_packed = false;
4909 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4910 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4911
4912 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4913 TB_INFO_KEY);
4914 if (tbinfo)
4915 {
4916 xfree (tbinfo);
4917 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4918 TB_INFO_KEY,
4919 NULL);
4920 }
4921
4922 frame_size_history_add (f, Qfree_frame_tool_bar, 0, 0, Qnil);
4923 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4924
4925 unblock_input ();
4926 }
4927 }
4928
4929 void
4930 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4931 {
4932 struct x_output *x = f->output_data.x;
4933 GtkWidget *top_widget = x->toolbar_widget;
4934
4935 if (! x->toolbar_widget || ! top_widget)
4936 return;
4937
4938 block_input ();
4939 g_object_ref (top_widget);
4940 if (x->toolbar_is_packed)
4941 {
4942 if (x->toolbar_in_hbox)
4943 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4944 top_widget);
4945 else
4946 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4947 top_widget);
4948 }
4949
4950 xg_pack_tool_bar (f, pos);
4951 g_object_unref (top_widget);
4952
4953 if (xg_update_tool_bar_sizes (f))
4954 {
4955 frame_size_history_add (f, Qxg_change_toolbar_position, 0, 0, Qnil);
4956 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4957 }
4958
4959
4960 unblock_input ();
4961 }
4962
4963
4964 \f
4965 /***********************************************************************
4966 Initializing
4967 ***********************************************************************/
4968 void
4969 xg_initialize (void)
4970 {
4971 GtkBindingSet *binding_set;
4972 GtkSettings *settings;
4973
4974 #if HAVE_XFT
4975 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4976 we keep it permanently linked in. */
4977 XftInit (0);
4978 #endif
4979
4980 gdpy_def = NULL;
4981 xg_ignore_gtk_scrollbar = 0;
4982 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4983 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4984
4985 id_to_widget.max_size = id_to_widget.used = 0;
4986 id_to_widget.widgets = 0;
4987
4988 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4989 (gdk_display_get_default ()));
4990 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4991 bindings. It doesn't seem to be any way to remove properties,
4992 so we set it to "" which in means "no key". */
4993 gtk_settings_set_string_property (settings,
4994 "gtk-menu-bar-accel",
4995 "",
4996 EMACS_CLASS);
4997
4998 /* Make GTK text input widgets use Emacs style keybindings. This is
4999 Emacs after all. */
5000 gtk_settings_set_string_property (settings,
5001 "gtk-key-theme-name",
5002 "Emacs",
5003 EMACS_CLASS);
5004
5005 /* Make dialogs close on C-g. Since file dialog inherits from
5006 dialog, this works for them also. */
5007 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
5008 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5009 "close", 0);
5010
5011 /* Make menus close on C-g. */
5012 binding_set = gtk_binding_set_by_class (g_type_class_ref
5013 (GTK_TYPE_MENU_SHELL));
5014 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5015 "cancel", 0);
5016 update_theme_scrollbar_width ();
5017 update_theme_scrollbar_height ();
5018
5019 #ifdef HAVE_FREETYPE
5020 x_last_font_name = NULL;
5021 #endif
5022 }
5023
5024 #endif /* USE_GTK */