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