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