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