Merge from origin/emacs-25
[emacs.git] / src / gtkutil.c
blob88e6d30bd9a02d9afd5e5e2b9c48aa9bd5db0c0b
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <float.h>
24 #include <stdio.h>
26 #include <c-ctype.h>
28 #include "lisp.h"
29 #include "dispextern.h"
30 #include "frame.h"
31 #include "systime.h"
32 #include "xterm.h"
33 #include "blockinput.h"
34 #include "window.h"
35 #include "gtkutil.h"
36 #include "termhooks.h"
37 #include "keyboard.h"
38 #include "coding.h"
40 #include <gdk/gdkkeysyms.h>
42 #ifdef HAVE_XFT
43 #include <X11/Xft/Xft.h>
44 #endif
46 #ifdef HAVE_GTK3
47 #include <gtk/gtkx.h>
48 #include "emacsgtkfixed.h"
49 #endif
51 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
52 #define gtk_widget_set_has_window(w, b) \
53 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
54 #endif
55 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
56 #define gtk_dialog_get_action_area(w) ((w)->action_area)
57 #define gtk_dialog_get_content_area(w) ((w)->vbox)
58 #endif
59 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
60 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
61 #endif
62 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
63 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
64 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
65 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
66 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
67 #endif
68 #if GTK_CHECK_VERSION (2, 12, 0)
69 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
70 #else
71 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
72 #endif
74 #if ! GTK_CHECK_VERSION (2, 14, 0)
75 #define gtk_adjustment_configure(adj, xvalue, xlower, \
76 xupper, xstep_increment, \
77 xpage_increment, xpagesize) \
78 do { \
79 adj->lower = xlower; \
80 adj->upper = xupper; \
81 adj->page_size = xpagesize; \
82 gtk_adjustment_set_value (adj, xvalue); \
83 adj->page_increment = xpage_increment; \
84 adj->step_increment = xstep_increment; \
85 } while (0)
86 #endif /* < Gtk+ 2.14 */
88 #ifdef HAVE_FREETYPE
89 #if GTK_CHECK_VERSION (3, 2, 0)
90 #define USE_NEW_GTK_FONT_CHOOSER 1
91 #else
92 #define USE_NEW_GTK_FONT_CHOOSER 0
93 #define gtk_font_chooser_dialog_new(x, y) \
94 gtk_font_selection_dialog_new (x)
95 #undef GTK_FONT_CHOOSER
96 #define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
97 #define gtk_font_chooser_set_font(x, y) \
98 gtk_font_selection_dialog_set_font_name (x, y)
99 #endif
100 #endif /* HAVE_FREETYPE */
102 #if GTK_CHECK_VERSION (3, 10, 0)
103 #define XG_TEXT_CANCEL "Cancel"
104 #define XG_TEXT_OK "OK"
105 #define XG_TEXT_OPEN "Open"
106 #else
107 #define XG_TEXT_CANCEL GTK_STOCK_CANCEL
108 #define XG_TEXT_OK GTK_STOCK_OK
109 #define XG_TEXT_OPEN GTK_STOCK_OPEN
110 #endif
112 #ifndef HAVE_GTK3
113 #ifdef USE_GTK_TOOLTIP
114 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
115 #endif
116 #define gdk_window_get_geometry(w, a, b, c, d) \
117 gdk_window_get_geometry (w, a, b, c, d, 0)
118 #define gdk_x11_window_lookup_for_display(d, w) \
119 gdk_xid_table_lookup_for_display (d, w)
120 #define gtk_box_new(ori, spacing) \
121 ((ori) == GTK_ORIENTATION_HORIZONTAL \
122 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
123 #define gtk_scrollbar_new(ori, spacing) \
124 ((ori) == GTK_ORIENTATION_HORIZONTAL \
125 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
126 #ifndef GDK_KEY_g
127 #define GDK_KEY_g GDK_g
128 #endif
129 #endif /* HAVE_GTK3 */
131 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
133 static void update_theme_scrollbar_width (void);
134 static void update_theme_scrollbar_height (void);
136 #define TB_INFO_KEY "xg_frame_tb_info"
137 struct xg_frame_tb_info
139 Lisp_Object last_tool_bar;
140 Lisp_Object style;
141 int n_last_items;
142 int hmargin, vmargin;
143 GtkTextDirection dir;
147 /***********************************************************************
148 Display handling functions
149 ***********************************************************************/
151 /* Keep track of the default display, or NULL if there is none. Emacs
152 may close all its displays. */
154 static GdkDisplay *gdpy_def;
156 /* When the GTK widget W is to be created on a display for F that
157 is not the default display, set the display for W.
158 W can be a GtkMenu or a GtkWindow widget. */
160 static void
161 xg_set_screen (GtkWidget *w, struct frame *f)
163 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
165 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
166 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
168 if (GTK_IS_MENU (w))
169 gtk_menu_set_screen (GTK_MENU (w), gscreen);
170 else
171 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
176 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
177 *DPY is set to NULL if the display can't be opened.
179 Returns non-zero if display could be opened, zero if display could not
180 be opened, and less than zero if the GTK version doesn't support
181 multiple displays. */
183 void
184 xg_display_open (char *display_name, Display **dpy)
186 GdkDisplay *gdpy;
188 unrequest_sigio (); // See comment in x_display_ok, xterm.c.
189 gdpy = gdk_display_open (display_name);
190 request_sigio ();
191 if (!gdpy_def && gdpy)
193 gdpy_def = gdpy;
194 gdk_display_manager_set_default_display (gdk_display_manager_get (),
195 gdpy);
198 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
202 /* Close display DPY. */
204 void
205 xg_display_close (Display *dpy)
207 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
209 /* If this is the default display, try to change it before closing.
210 If there is no other display to use, gdpy_def is set to NULL, and
211 the next call to xg_display_open resets the default display. */
212 if (gdk_display_get_default () == gdpy)
214 struct x_display_info *dpyinfo;
215 GdkDisplay *gdpy_new = NULL;
217 /* Find another display. */
218 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
219 if (dpyinfo->display != dpy)
221 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
222 gdk_display_manager_set_default_display (gdk_display_manager_get (),
223 gdpy_new);
224 break;
226 gdpy_def = gdpy_new;
229 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
230 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
231 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
232 can continue running, but there will be memory leaks. */
233 g_object_run_dispose (G_OBJECT (gdpy));
234 #else
235 /* This seems to be fixed in GTK 2.10. */
236 gdk_display_close (gdpy);
237 #endif
241 /***********************************************************************
242 Utility functions
243 ***********************************************************************/
245 /* Create and return the cursor to be used for popup menus and
246 scroll bars on display DPY. */
248 GdkCursor *
249 xg_create_default_cursor (Display *dpy)
251 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
252 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
255 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
257 static GdkPixbuf *
258 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
259 Pixmap pix,
260 Pixmap mask)
262 GdkPixbuf *icon_buf = 0;
263 int iunused;
264 Window wunused;
265 unsigned int width, height, depth, uunused;
267 if (FRAME_DISPLAY_INFO (f)->red_bits != 8)
268 return 0;
269 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
270 &width, &height, &uunused, &depth);
271 if (depth != 24)
272 return 0;
273 XImage *xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
274 ~0, XYPixmap);
275 if (xim)
277 XImage *xmm = (! mask ? 0
278 : XGetImage (FRAME_X_DISPLAY (f), mask, 0, 0,
279 width, height, ~0, XYPixmap));
280 icon_buf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
281 if (icon_buf)
283 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
284 int rowjunkwidth = gdk_pixbuf_get_rowstride (icon_buf) - width * 4;
285 for (int y = 0; y < height; y++, pixels += rowjunkwidth)
286 for (int x = 0; x < width; x++)
288 unsigned long rgb = XGetPixel (xim, x, y);
289 *pixels++ = (rgb >> 16) & 255;
290 *pixels++ = (rgb >> 8) & 255;
291 *pixels++ = rgb & 255;
292 *pixels++ = xmm && !XGetPixel (xmm, x, y) ? 0 : 255;
296 if (xmm)
297 XDestroyImage (xmm);
298 XDestroyImage (xim);
301 return icon_buf;
304 static Lisp_Object
305 file_for_image (Lisp_Object image)
307 Lisp_Object specified_file = Qnil;
308 Lisp_Object tail;
310 for (tail = XCDR (image);
311 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
312 tail = XCDR (XCDR (tail)))
313 if (EQ (XCAR (tail), QCfile))
314 specified_file = XCAR (XCDR (tail));
316 return specified_file;
319 /* For the image defined in IMG, make and return a GtkImage. For displays with
320 8 planes or less we must make a GdkPixbuf and apply the mask manually.
321 Otherwise the highlighting and dimming the tool bar code in GTK does
322 will look bad. For display with more than 8 planes we just use the
323 pixmap and mask directly. For monochrome displays, GTK doesn't seem
324 able to use external pixmaps, it looks bad whatever we do.
325 The image is defined on the display where frame F is.
326 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
327 If OLD_WIDGET is NULL, a new widget is constructed and returned.
328 If OLD_WIDGET is not NULL, that widget is modified. */
330 static GtkWidget *
331 xg_get_image_for_pixmap (struct frame *f,
332 struct image *img,
333 GtkWidget *widget,
334 GtkImage *old_widget)
336 GdkPixbuf *icon_buf;
338 /* If we have a file, let GTK do all the image handling.
339 This seems to be the only way to make insensitive and activated icons
340 look good in all cases. */
341 Lisp_Object specified_file = file_for_image (img->spec);
342 Lisp_Object file;
344 /* We already loaded the image once before calling this
345 function, so this only fails if the image file has been removed.
346 In that case, use the pixmap already loaded. */
348 if (STRINGP (specified_file)
349 && STRINGP (file = x_find_image_file (specified_file)))
351 char *encoded_file = SSDATA (ENCODE_FILE (file));
352 if (! old_widget)
353 old_widget = GTK_IMAGE (gtk_image_new_from_file (encoded_file));
354 else
355 gtk_image_set_from_file (old_widget, encoded_file);
357 return GTK_WIDGET (old_widget);
360 /* No file, do the image handling ourselves. This will look very bad
361 on a monochrome display, and sometimes bad on all displays with
362 certain themes. */
364 /* This is a workaround to make icons look good on pseudo color
365 displays. Apparently GTK expects the images to have an alpha
366 channel. If they don't, insensitive and activated icons will
367 look bad. This workaround does not work on monochrome displays,
368 and is strictly not needed on true color/static color displays (i.e.
369 16 bits and higher). But we do it anyway so we get a pixbuf that is
370 not associated with the img->pixmap. The img->pixmap may be removed
371 by clearing the image cache and then the tool bar redraw fails, since
372 Gtk+ assumes the pixmap is always there. */
373 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
375 if (icon_buf)
377 if (! old_widget)
378 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
379 else
380 gtk_image_set_from_pixbuf (old_widget, icon_buf);
382 g_object_unref (G_OBJECT (icon_buf));
385 return GTK_WIDGET (old_widget);
389 /* Set CURSOR on W and all widgets W contain. We must do like this
390 for scroll bars and menu because they create widgets internally,
391 and it is those widgets that are visible. */
393 static void
394 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
396 GdkWindow *window = gtk_widget_get_window (w);
397 GList *children = gdk_window_peek_children (window);
399 gdk_window_set_cursor (window, cursor);
401 /* The scroll bar widget has more than one GDK window (had to look at
402 the source to figure this out), and there is no way to set cursor
403 on widgets in GTK. So we must set the cursor for all GDK windows.
404 Ditto for menus. */
406 for ( ; children; children = g_list_next (children))
407 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
410 /* Insert NODE into linked LIST. */
412 static void
413 xg_list_insert (xg_list_node *list, xg_list_node *node)
415 xg_list_node *list_start = list->next;
417 if (list_start) list_start->prev = node;
418 node->next = list_start;
419 node->prev = 0;
420 list->next = node;
423 /* Remove NODE from linked LIST. */
425 static void
426 xg_list_remove (xg_list_node *list, xg_list_node *node)
428 xg_list_node *list_start = list->next;
429 if (node == list_start)
431 list->next = node->next;
432 if (list->next) list->next->prev = 0;
434 else
436 node->prev->next = node->next;
437 if (node->next) node->next->prev = node->prev;
441 /* Allocate and return a utf8 version of STR. If STR is already
442 utf8 or NULL, just return a copy of STR.
443 A new string is allocated and the caller must free the result
444 with g_free. */
446 static char *
447 get_utf8_string (const char *str)
449 char *utf8_str;
451 if (!str) return NULL;
453 /* If not UTF-8, try current locale. */
454 if (!g_utf8_validate (str, -1, NULL))
455 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
456 else
457 return g_strdup (str);
459 if (!utf8_str)
461 /* Probably some control characters in str. Escape them. */
462 ptrdiff_t len;
463 ptrdiff_t nr_bad = 0;
464 gsize bytes_read;
465 gsize bytes_written;
466 unsigned char *p = (unsigned char *)str;
467 char *cp, *up;
468 GError *err = NULL;
470 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
471 &bytes_written, &err))
472 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
474 ++nr_bad;
475 p += bytes_written+1;
476 g_error_free (err);
477 err = NULL;
480 if (err)
482 g_error_free (err);
483 err = NULL;
485 if (cp) g_free (cp);
487 len = strlen (str);
488 ptrdiff_t alloc;
489 if (INT_MULTIPLY_WRAPV (nr_bad, 4, &alloc)
490 || INT_ADD_WRAPV (len + 1, alloc, &alloc)
491 || SIZE_MAX < alloc)
492 memory_full (SIZE_MAX);
493 up = utf8_str = xmalloc (alloc);
494 p = (unsigned char *)str;
496 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
497 &bytes_written, &err))
498 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
500 memcpy (up, p, bytes_written);
501 up += bytes_written;
502 up += sprintf (up, "\\%03o", p[bytes_written]);
503 p += bytes_written + 1;
504 g_error_free (err);
505 err = NULL;
508 if (cp)
510 strcpy (up, cp);
511 g_free (cp);
513 if (err)
515 g_error_free (err);
516 err = NULL;
519 return utf8_str;
522 /* Check for special colors used in face spec for region face.
523 The colors are fetched from the Gtk+ theme.
524 Return true if color was found, false if not. */
526 bool
527 xg_check_special_colors (struct frame *f,
528 const char *color_name,
529 XColor *color)
531 bool success_p = 0;
532 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
533 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
535 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
536 return success_p;
538 block_input ();
540 #ifdef HAVE_GTK3
541 GtkStyleContext *gsty
542 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
543 GdkRGBA col;
544 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
545 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
546 if (get_fg)
547 gtk_style_context_get_color (gsty, state, &col);
548 else
549 gtk_style_context_get_background_color (gsty, state, &col);
551 sprintf (buf, "rgb:%04x/%04x/%04x",
552 (unsigned) (col.red * 65535),
553 (unsigned) (col.green * 65535),
554 (unsigned) (col.blue * 65535));
555 success_p = x_parse_color (f, buf, color) != 0;
556 #else
557 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
558 GdkColor *grgb = get_bg
559 ? &gsty->bg[GTK_STATE_SELECTED]
560 : &gsty->fg[GTK_STATE_SELECTED];
562 color->red = grgb->red;
563 color->green = grgb->green;
564 color->blue = grgb->blue;
565 color->pixel = grgb->pixel;
566 success_p = 1;
567 #endif
570 unblock_input ();
571 return success_p;
576 /***********************************************************************
577 Tooltips
578 ***********************************************************************/
579 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
580 We use that to pop down the tooltip. This happens if Gtk+ for some
581 reason wants to change or hide the tooltip. */
583 #ifdef USE_GTK_TOOLTIP
585 static void
586 hierarchy_ch_cb (GtkWidget *widget,
587 GtkWidget *previous_toplevel,
588 gpointer user_data)
590 struct frame *f = user_data;
591 struct x_output *x = f->output_data.x;
592 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
594 if (! top || ! GTK_IS_WINDOW (top))
595 gtk_widget_hide (previous_toplevel);
598 /* Callback called when Gtk+ thinks a tooltip should be displayed.
599 We use it to get the tooltip window and the tooltip widget so
600 we can manipulate the ourselves.
602 Return FALSE ensures that the tooltip is not shown. */
604 static gboolean
605 qttip_cb (GtkWidget *widget,
606 gint xpos,
607 gint ypos,
608 gboolean keyboard_mode,
609 GtkTooltip *tooltip,
610 gpointer user_data)
612 struct frame *f = user_data;
613 struct x_output *x = f->output_data.x;
614 if (x->ttip_widget == NULL)
616 GtkWidget *p;
617 GList *list, *iter;
619 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
620 x->ttip_widget = tooltip;
621 g_object_ref (G_OBJECT (tooltip));
622 x->ttip_lbl = gtk_label_new ("");
623 g_object_ref (G_OBJECT (x->ttip_lbl));
624 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
625 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
627 /* Change stupid Gtk+ default line wrapping. */
628 p = gtk_widget_get_parent (x->ttip_lbl);
629 list = gtk_container_get_children (GTK_CONTAINER (p));
630 for (iter = list; iter; iter = g_list_next (iter))
632 GtkWidget *w = GTK_WIDGET (iter->data);
633 if (GTK_IS_LABEL (w))
634 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
636 g_list_free (list);
638 /* ATK needs an empty title for some reason. */
639 gtk_window_set_title (x->ttip_window, "");
640 /* Realize so we can safely get screen later on. */
641 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
642 gtk_widget_realize (x->ttip_lbl);
644 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
645 G_CALLBACK (hierarchy_ch_cb), f);
647 return FALSE;
650 #endif /* USE_GTK_TOOLTIP */
652 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
653 Return true if a system tooltip is available. */
655 bool
656 xg_prepare_tooltip (struct frame *f,
657 Lisp_Object string,
658 int *width,
659 int *height)
661 #ifndef USE_GTK_TOOLTIP
662 return 0;
663 #else
664 struct x_output *x = f->output_data.x;
665 GtkWidget *widget;
666 GdkWindow *gwin;
667 GdkScreen *screen;
668 GtkSettings *settings;
669 gboolean tt_enabled = TRUE;
670 GtkRequisition req;
671 Lisp_Object encoded_string;
673 if (!x->ttip_lbl) return 0;
675 block_input ();
676 encoded_string = ENCODE_UTF_8 (string);
677 widget = GTK_WIDGET (x->ttip_lbl);
678 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
679 screen = gdk_window_get_screen (gwin);
680 settings = gtk_settings_get_for_screen (screen);
681 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
682 if (tt_enabled)
684 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
685 /* Record that we disabled it so it can be enabled again. */
686 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
687 (gpointer)f);
690 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
691 g_object_set_data (G_OBJECT
692 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
693 "gdk-display-current-tooltip", NULL);
695 /* Put our dummy widget in so we can get callbacks for unrealize and
696 hierarchy-changed. */
697 gtk_tooltip_set_custom (x->ttip_widget, widget);
698 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
699 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
700 if (width) *width = req.width;
701 if (height) *height = req.height;
703 unblock_input ();
705 return 1;
706 #endif /* USE_GTK_TOOLTIP */
709 /* Show the tooltip at ROOT_X and ROOT_Y.
710 xg_prepare_tooltip must have been called before this function. */
712 void
713 xg_show_tooltip (struct frame *f, int root_x, int root_y)
715 #ifdef USE_GTK_TOOLTIP
716 struct x_output *x = f->output_data.x;
717 if (x->ttip_window)
719 block_input ();
720 gtk_window_move (x->ttip_window, root_x, root_y);
721 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
722 unblock_input ();
724 #endif
727 /* Hide tooltip if shown. Do nothing if not shown.
728 Return true if tip was hidden, false if not (i.e. not using
729 system tooltips). */
731 bool
732 xg_hide_tooltip (struct frame *f)
734 bool ret = 0;
735 #ifdef USE_GTK_TOOLTIP
736 if (f->output_data.x->ttip_window)
738 GtkWindow *win = f->output_data.x->ttip_window;
739 block_input ();
740 gtk_widget_hide (GTK_WIDGET (win));
742 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
744 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
745 GdkScreen *screen = gdk_window_get_screen (gwin);
746 GtkSettings *settings = gtk_settings_get_for_screen (screen);
747 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
749 unblock_input ();
751 ret = 1;
753 #endif
754 return ret;
758 /***********************************************************************
759 General functions for creating widgets, resizing, events, e.t.c.
760 ***********************************************************************/
762 static void
763 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
764 const gchar *msg, gpointer user_data)
766 if (!strstr (msg, "visible children"))
767 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
770 /* Make a geometry string and pass that to GTK. It seems this is the
771 only way to get geometry position right if the user explicitly
772 asked for a position when starting Emacs.
773 F is the frame we shall set geometry for. */
775 static void
776 xg_set_geometry (struct frame *f)
778 if (f->size_hint_flags & (USPosition | PPosition))
780 int left = f->left_pos;
781 int xneg = f->size_hint_flags & XNegative;
782 int top = f->top_pos;
783 int yneg = f->size_hint_flags & YNegative;
784 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
785 guint id;
787 if (xneg)
788 left = -left;
789 if (yneg)
790 top = -top;
792 sprintf (geom_str, "=%dx%d%c%d%c%d",
793 FRAME_PIXEL_WIDTH (f),
794 FRAME_PIXEL_HEIGHT (f),
795 (xneg ? '-' : '+'), left,
796 (yneg ? '-' : '+'), top);
798 /* Silence warning about visible children. */
799 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
800 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
802 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
803 geom_str))
804 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
806 g_log_remove_handler ("Gtk", id);
810 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
811 and use a GtkFixed widget, this doesn't happen automatically. */
813 void
814 xg_clear_under_internal_border (struct frame *f)
816 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
818 #ifndef USE_CAIRO
819 GtkWidget *wfixed = f->output_data.x->edit_widget;
821 gtk_widget_queue_draw (wfixed);
822 gdk_window_process_all_updates ();
823 #endif
824 x_clear_area (f, 0, 0,
825 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
827 x_clear_area (f, 0, 0,
828 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
830 x_clear_area (f, 0,
831 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
832 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
834 x_clear_area (f,
835 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
836 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
840 static int
841 xg_get_gdk_scale (void)
843 const char *sscale = getenv ("GDK_SCALE");
845 if (sscale)
847 long scale = atol (sscale);
848 if (0 < scale)
849 return min (scale, INT_MAX);
852 return 1;
855 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
856 and a Gtk+ menu bar, we get resize events for the edit part of the
857 frame only. We let Gtk+ deal with the Gtk+ parts.
858 F is the frame to resize.
859 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
861 void
862 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
864 int width, height;
866 if (pixelwidth == -1 && pixelheight == -1)
868 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
869 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
870 0, 0, &pixelwidth, &pixelheight);
871 else
872 return;
875 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
876 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
878 frame_size_history_add
879 (f, Qxg_frame_resized, width, height, Qnil);
881 if (width != FRAME_TEXT_WIDTH (f)
882 || height != FRAME_TEXT_HEIGHT (f)
883 || pixelwidth != FRAME_PIXEL_WIDTH (f)
884 || pixelheight != FRAME_PIXEL_HEIGHT (f))
886 xg_clear_under_internal_border (f);
887 change_frame_size (f, width, height, 0, 1, 0, 1);
888 SET_FRAME_GARBAGED (f);
889 cancel_mouse_face (f);
893 /* Resize the outer window of frame F after changing the height.
894 COLUMNS/ROWS is the size the edit area shall have after the resize. */
896 void
897 xg_frame_set_char_size (struct frame *f, int width, int height)
899 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
900 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
901 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
902 gint gwidth, gheight;
903 int totalheight
904 = pixelheight + FRAME_TOOLBAR_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f);
905 int totalwidth = pixelwidth + FRAME_TOOLBAR_WIDTH (f);
907 if (FRAME_PIXEL_HEIGHT (f) == 0)
908 return;
910 gtk_window_get_size (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
911 &gwidth, &gheight);
913 /* Do this before resize, as we don't know yet if we will be resized. */
914 xg_clear_under_internal_border (f);
916 if (FRAME_VISIBLE_P (f))
918 int scale = xg_get_gdk_scale ();
919 totalheight /= scale;
920 totalwidth /= scale;
923 x_wm_set_size_hint (f, 0, 0);
925 /* Resize the top level widget so rows and columns remain constant.
927 When the frame is fullheight and we only want to change the width
928 or it is fullwidth and we only want to change the height we should
929 be able to preserve the fullscreen property. However, due to the
930 fact that we have to send a resize request anyway, the window
931 manager will abolish it. At least the respective size should
932 remain unchanged but giving the frame back its normal size will
933 be broken ... */
934 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
936 frame_size_history_add
937 (f, Qxg_frame_set_char_size_1, width, height,
938 list2 (make_number (gheight), make_number (totalheight)));
940 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
941 gwidth, totalheight);
943 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
945 frame_size_history_add
946 (f, Qxg_frame_set_char_size_2, width, height,
947 list2 (make_number (gwidth), make_number (totalwidth)));
949 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
950 totalwidth, gheight);
952 else
954 frame_size_history_add
955 (f, Qxg_frame_set_char_size_3, width, height,
956 list2 (make_number (totalwidth), make_number (totalheight)));
958 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
959 totalwidth, totalheight);
960 fullscreen = Qnil;
963 SET_FRAME_GARBAGED (f);
964 cancel_mouse_face (f);
966 /* We can not call change_frame_size for a mapped frame,
967 we can not set pixel width/height either. The window manager may
968 override our resize request, XMonad does this all the time.
969 The best we can do is try to sync, so lisp code sees the updated
970 size as fast as possible.
971 For unmapped windows, we can set rows/cols. When
972 the frame is mapped again we will (hopefully) get the correct size. */
973 if (FRAME_VISIBLE_P (f))
975 /* Must call this to flush out events */
976 (void)gtk_events_pending ();
977 gdk_flush ();
978 x_wait_for_event (f, ConfigureNotify);
980 if (!NILP (fullscreen))
981 /* Try to restore fullscreen state. */
983 store_frame_param (f, Qfullscreen, fullscreen);
984 x_set_fullscreen (f, fullscreen, fullscreen);
987 else
988 adjust_frame_size (f, width, height, 5, 0, Qxg_frame_set_char_size);
992 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
993 The policy is to keep the number of editable lines. */
995 #if 0
996 static void
997 xg_height_or_width_changed (struct frame *f)
999 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1000 FRAME_TOTAL_PIXEL_WIDTH (f),
1001 FRAME_TOTAL_PIXEL_HEIGHT (f));
1002 f->output_data.x->hint_flags = 0;
1003 x_wm_set_size_hint (f, 0, 0);
1005 #endif
1007 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
1008 Must be done like this, because GtkWidget:s can have "hidden"
1009 X Window that aren't accessible.
1011 Return 0 if no widget match WDESC. */
1013 GtkWidget *
1014 xg_win_to_widget (Display *dpy, Window wdesc)
1016 gpointer gdkwin;
1017 GtkWidget *gwdesc = 0;
1019 block_input ();
1021 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
1022 wdesc);
1023 if (gdkwin)
1025 GdkEvent event;
1026 event.any.window = gdkwin;
1027 event.any.type = GDK_NOTHING;
1028 gwdesc = gtk_get_event_widget (&event);
1031 unblock_input ();
1032 return gwdesc;
1035 /* Set the background of widget W to PIXEL. */
1037 static void
1038 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
1040 #ifdef HAVE_GTK3
1041 GdkRGBA bg;
1042 XColor xbg;
1043 xbg.pixel = pixel;
1044 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1046 bg.red = (double)xbg.red/65535.0;
1047 bg.green = (double)xbg.green/65535.0;
1048 bg.blue = (double)xbg.blue/65535.0;
1049 bg.alpha = 1.0;
1050 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1052 #else
1053 GdkColor bg;
1054 GdkColormap *map = gtk_widget_get_colormap (w);
1055 gdk_colormap_query_color (map, pixel, &bg);
1056 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1057 #endif
1060 /* Callback called when the gtk theme changes.
1061 We notify lisp code so it can fix faces used for region for example. */
1063 static void
1064 style_changed_cb (GObject *go,
1065 GParamSpec *spec,
1066 gpointer user_data)
1068 struct input_event event;
1069 GdkDisplay *gdpy = user_data;
1070 const char *display_name = gdk_display_get_name (gdpy);
1071 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1073 EVENT_INIT (event);
1074 event.kind = CONFIG_CHANGED_EVENT;
1075 event.frame_or_window = build_string (display_name);
1076 /* Theme doesn't change often, so intern is called seldom. */
1077 event.arg = intern ("theme-name");
1078 kbd_buffer_store_event (&event);
1080 update_theme_scrollbar_width ();
1081 update_theme_scrollbar_height ();
1083 /* If scroll bar width changed, we need set the new size on all frames
1084 on this display. */
1085 if (dpy)
1087 Lisp_Object rest, frame;
1088 FOR_EACH_FRAME (rest, frame)
1090 struct frame *f = XFRAME (frame);
1091 if (FRAME_LIVE_P (f)
1092 && FRAME_X_P (f)
1093 && FRAME_X_DISPLAY (f) == dpy)
1095 x_set_scroll_bar_default_width (f);
1096 x_set_scroll_bar_default_height (f);
1097 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1103 /* Called when a delete-event occurs on WIDGET. */
1105 static gboolean
1106 delete_cb (GtkWidget *widget,
1107 GdkEvent *event,
1108 gpointer user_data)
1110 return TRUE;
1113 /* Create and set up the GTK widgets for frame F.
1114 Return true if creation succeeded. */
1116 bool
1117 xg_create_frame_widgets (struct frame *f)
1119 GtkWidget *wtop;
1120 GtkWidget *wvbox, *whbox;
1121 GtkWidget *wfixed;
1122 #ifndef HAVE_GTK3
1123 GtkRcStyle *style;
1124 #endif
1125 char *title = 0;
1127 block_input ();
1129 if (FRAME_X_EMBEDDED_P (f))
1131 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1132 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1134 else
1135 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1137 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1138 has backported it to Gtk+ 2.0 and they add the resize grip for
1139 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1140 forever, so disable the grip. */
1141 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1142 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1143 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1144 #endif
1146 xg_set_screen (wtop, f);
1148 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1149 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1150 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1151 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1153 #ifdef HAVE_GTK3
1154 wfixed = emacs_fixed_new (f);
1155 #else
1156 wfixed = gtk_fixed_new ();
1157 #endif
1159 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1161 if (wtop) gtk_widget_destroy (wtop);
1162 if (wvbox) gtk_widget_destroy (wvbox);
1163 if (whbox) gtk_widget_destroy (whbox);
1164 if (wfixed) gtk_widget_destroy (wfixed);
1166 unblock_input ();
1167 return 0;
1170 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1171 gtk_widget_set_name (wtop, EMACS_CLASS);
1172 gtk_widget_set_name (wvbox, "pane");
1173 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1175 /* If this frame has a title or name, set it in the title bar. */
1176 if (! NILP (f->title))
1177 title = SSDATA (ENCODE_UTF_8 (f->title));
1178 else if (! NILP (f->name))
1179 title = SSDATA (ENCODE_UTF_8 (f->name));
1181 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1183 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1184 FRAME_GTK_WIDGET (f) = wfixed;
1185 f->output_data.x->vbox_widget = wvbox;
1186 f->output_data.x->hbox_widget = whbox;
1188 gtk_widget_set_has_window (wfixed, TRUE);
1190 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1191 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1192 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1194 if (FRAME_EXTERNAL_TOOL_BAR (f))
1195 update_frame_tool_bar (f);
1197 /* We don't want this widget double buffered, because we draw on it
1198 with regular X drawing primitives, so from a GTK/GDK point of
1199 view, the widget is totally blank. When an expose comes, this
1200 will make the widget blank, and then Emacs redraws it. This flickers
1201 a lot, so we turn off double buffering. */
1202 gtk_widget_set_double_buffered (wfixed, FALSE);
1204 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1205 SSDATA (Vx_resource_name),
1206 SSDATA (Vx_resource_class));
1208 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1209 GTK is to destroy the widget. We want Emacs to do that instead. */
1210 g_signal_connect (G_OBJECT (wtop), "delete-event",
1211 G_CALLBACK (delete_cb), f);
1213 /* Convert our geometry parameters into a geometry string
1214 and specify it.
1215 GTK will itself handle calculating the real position this way. */
1216 xg_set_geometry (f);
1217 f->win_gravity
1218 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1220 gtk_widget_add_events (wfixed,
1221 GDK_POINTER_MOTION_MASK
1222 | GDK_EXPOSURE_MASK
1223 | GDK_BUTTON_PRESS_MASK
1224 | GDK_BUTTON_RELEASE_MASK
1225 | GDK_KEY_PRESS_MASK
1226 | GDK_ENTER_NOTIFY_MASK
1227 | GDK_LEAVE_NOTIFY_MASK
1228 | GDK_FOCUS_CHANGE_MASK
1229 | GDK_STRUCTURE_MASK
1230 | GDK_VISIBILITY_NOTIFY_MASK);
1232 /* Must realize the windows so the X window gets created. It is used
1233 by callers of this function. */
1234 gtk_widget_realize (wfixed);
1235 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1237 /* Since GTK clears its window by filling with the background color,
1238 we must keep X and GTK background in sync. */
1239 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1241 #ifndef HAVE_GTK3
1242 /* Also, do not let any background pixmap to be set, this looks very
1243 bad as Emacs overwrites the background pixmap with its own idea
1244 of background color. */
1245 style = gtk_widget_get_modifier_style (wfixed);
1247 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1248 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1249 gtk_widget_modify_style (wfixed, style);
1250 #else
1251 gtk_widget_set_can_focus (wfixed, TRUE);
1252 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1253 #endif
1255 #ifdef USE_GTK_TOOLTIP
1256 /* Steal a tool tip window we can move ourselves. */
1257 f->output_data.x->ttip_widget = 0;
1258 f->output_data.x->ttip_lbl = 0;
1259 f->output_data.x->ttip_window = 0;
1260 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1261 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1262 #endif
1265 GdkScreen *screen = gtk_widget_get_screen (wtop);
1266 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1267 /* Only connect this signal once per screen. */
1268 if (! g_signal_handler_find (G_OBJECT (gs),
1269 G_SIGNAL_MATCH_FUNC,
1270 0, 0, 0,
1271 (gpointer) G_CALLBACK (style_changed_cb),
1274 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1275 G_CALLBACK (style_changed_cb),
1276 gdk_screen_get_display (screen));
1280 unblock_input ();
1282 return 1;
1285 void
1286 xg_free_frame_widgets (struct frame *f)
1288 if (FRAME_GTK_OUTER_WIDGET (f))
1290 #ifdef USE_GTK_TOOLTIP
1291 struct x_output *x = f->output_data.x;
1292 #endif
1293 struct xg_frame_tb_info *tbinfo
1294 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1295 TB_INFO_KEY);
1296 if (tbinfo)
1297 xfree (tbinfo);
1299 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1300 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1301 FRAME_GTK_OUTER_WIDGET (f) = 0;
1302 #ifdef USE_GTK_TOOLTIP
1303 if (x->ttip_lbl)
1304 gtk_widget_destroy (x->ttip_lbl);
1305 if (x->ttip_widget)
1306 g_object_unref (G_OBJECT (x->ttip_widget));
1307 #endif
1311 /* Set the normal size hints for the window manager, for frame F.
1312 FLAGS is the flags word to use--or 0 meaning preserve the flags
1313 that the window now has.
1314 If USER_POSITION, set the User Position
1315 flag (this is useful when FLAGS is 0). */
1317 void
1318 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1320 /* Must use GTK routines here, otherwise GTK resets the size hints
1321 to its own defaults. */
1322 GdkGeometry size_hints;
1323 gint hint_flags = 0;
1324 int base_width, base_height;
1325 int min_rows = 0, min_cols = 0;
1326 int win_gravity = f->win_gravity;
1327 Lisp_Object fs_state, frame;
1328 int scale = xg_get_gdk_scale ();
1330 /* Don't set size hints during initialization; that apparently leads
1331 to a race condition. See the thread at
1332 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1333 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1334 return;
1336 XSETFRAME (frame, f);
1337 fs_state = Fframe_parameter (frame, Qfullscreen);
1338 if ((EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth)) &&
1339 (x_wm_supports (f, FRAME_DISPLAY_INFO (f)->Xatom_net_wm_state) ||
1340 x_wm_supports (f, FRAME_DISPLAY_INFO (f)->Xatom_net_wm_state_fullscreen)))
1342 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1343 Gtk3 don't get along and the frame shrinks (!).
1345 return;
1348 if (flags)
1350 memset (&size_hints, 0, sizeof (size_hints));
1351 f->output_data.x->size_hints = size_hints;
1352 f->output_data.x->hint_flags = hint_flags;
1354 else
1355 flags = f->size_hint_flags;
1357 size_hints = f->output_data.x->size_hints;
1358 hint_flags = f->output_data.x->hint_flags;
1360 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1361 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1362 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1364 hint_flags |= GDK_HINT_BASE_SIZE;
1365 /* Use one row/col here so base_height/width does not become zero.
1366 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it.
1367 Obviously this makes the row/col value displayed off by 1. */
1368 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1369 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1370 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1372 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1373 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1375 size_hints.base_width = base_width;
1376 size_hints.base_height = base_height;
1377 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
1378 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1380 /* These currently have a one to one mapping with the X values, but I
1381 don't think we should rely on that. */
1382 hint_flags |= GDK_HINT_WIN_GRAVITY;
1383 size_hints.win_gravity = 0;
1384 if (win_gravity == NorthWestGravity)
1385 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1386 else if (win_gravity == NorthGravity)
1387 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1388 else if (win_gravity == NorthEastGravity)
1389 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1390 else if (win_gravity == WestGravity)
1391 size_hints.win_gravity = GDK_GRAVITY_WEST;
1392 else if (win_gravity == CenterGravity)
1393 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1394 else if (win_gravity == EastGravity)
1395 size_hints.win_gravity = GDK_GRAVITY_EAST;
1396 else if (win_gravity == SouthWestGravity)
1397 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1398 else if (win_gravity == SouthGravity)
1399 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1400 else if (win_gravity == SouthEastGravity)
1401 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1402 else if (win_gravity == StaticGravity)
1403 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1405 if (user_position)
1407 hint_flags &= ~GDK_HINT_POS;
1408 hint_flags |= GDK_HINT_USER_POS;
1411 size_hints.base_width /= scale;
1412 size_hints.base_height /= scale;
1413 size_hints.width_inc /= scale;
1414 size_hints.height_inc /= scale;
1416 if (hint_flags != f->output_data.x->hint_flags
1417 || memcmp (&size_hints,
1418 &f->output_data.x->size_hints,
1419 sizeof (size_hints)) != 0)
1421 block_input ();
1422 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1423 NULL, &size_hints, hint_flags);
1424 f->output_data.x->size_hints = size_hints;
1425 f->output_data.x->hint_flags = hint_flags;
1426 unblock_input ();
1430 /* Change background color of a frame.
1431 Since GTK uses the background color to clear the window, we must
1432 keep the GTK and X colors in sync.
1433 F is the frame to change,
1434 BG is the pixel value to change to. */
1436 void
1437 xg_set_background_color (struct frame *f, unsigned long bg)
1439 if (FRAME_GTK_WIDGET (f))
1441 block_input ();
1442 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1443 unblock_input ();
1448 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1449 functions so GTK does not overwrite the icon. */
1451 void
1452 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1454 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1455 icon_pixmap,
1456 icon_mask);
1457 if (gp)
1458 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1463 /***********************************************************************
1464 Dialog functions
1465 ***********************************************************************/
1466 /* Return the dialog title to use for a dialog of type KEY.
1467 This is the encoding used by lwlib. We use the same for GTK. */
1469 static const char *
1470 get_dialog_title (char key)
1472 const char *title = "";
1474 switch (key) {
1475 case 'E': case 'e':
1476 title = "Error";
1477 break;
1479 case 'I': case 'i':
1480 title = "Information";
1481 break;
1483 case 'L': case 'l':
1484 title = "Prompt";
1485 break;
1487 case 'P': case 'p':
1488 title = "Prompt";
1489 break;
1491 case 'Q': case 'q':
1492 title = "Question";
1493 break;
1496 return title;
1499 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1500 the dialog, but return TRUE so the event does not propagate further
1501 in GTK. This prevents GTK from destroying the dialog widget automatically
1502 and we can always destroy the widget manually, regardless of how
1503 it was popped down (button press or WM_DELETE_WINDOW).
1504 W is the dialog widget.
1505 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1506 user_data is NULL (not used).
1508 Returns TRUE to end propagation of event. */
1510 static gboolean
1511 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1513 gtk_widget_unmap (w);
1514 return TRUE;
1517 /* Create a popup dialog window. See also xg_create_widget below.
1518 WV is a widget_value describing the dialog.
1519 SELECT_CB is the callback to use when a button has been pressed.
1520 DEACTIVATE_CB is the callback to use when the dialog pops down.
1522 Returns the GTK dialog widget. */
1524 static GtkWidget *
1525 create_dialog (widget_value *wv,
1526 GCallback select_cb,
1527 GCallback deactivate_cb)
1529 const char *title = get_dialog_title (wv->name[0]);
1530 int total_buttons = wv->name[1] - '0';
1531 int right_buttons = wv->name[4] - '0';
1532 int left_buttons;
1533 int button_nr = 0;
1534 int button_spacing = 10;
1535 GtkWidget *wdialog = gtk_dialog_new ();
1536 GtkDialog *wd = GTK_DIALOG (wdialog);
1537 widget_value *item;
1538 GtkWidget *whbox_down;
1540 /* If the number of buttons is greater than 4, make two rows of buttons
1541 instead. This looks better. */
1542 bool make_two_rows = total_buttons > 4;
1544 #if GTK_CHECK_VERSION (3, 12, 0)
1545 GtkBuilder *gbld = gtk_builder_new ();
1546 GObject *go = gtk_buildable_get_internal_child (GTK_BUILDABLE (wd),
1547 gbld,
1548 "action_area");
1549 GtkBox *cur_box = GTK_BOX (go);
1550 g_object_unref (G_OBJECT (gbld));
1551 #else
1552 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1553 #endif
1555 if (right_buttons == 0) right_buttons = total_buttons/2;
1556 left_buttons = total_buttons - right_buttons;
1558 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1559 gtk_widget_set_name (wdialog, "emacs-dialog");
1562 if (make_two_rows)
1564 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1565 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1566 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1567 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1568 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1569 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1571 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1572 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1573 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1575 cur_box = GTK_BOX (whbox_up);
1578 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1579 G_CALLBACK (dialog_delete_callback), 0);
1581 if (deactivate_cb)
1583 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1584 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1587 for (item = wv->contents; item; item = item->next)
1589 char *utf8_label = get_utf8_string (item->value);
1590 GtkWidget *w;
1591 GtkRequisition req;
1593 if (item->name && strcmp (item->name, "message") == 0)
1595 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1596 /* This is the text part of the dialog. */
1597 w = gtk_label_new (utf8_label);
1598 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1599 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1600 #if GTK_CHECK_VERSION (3, 14, 0)
1601 gtk_widget_set_halign (w, GTK_ALIGN_START);
1602 gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
1603 #else
1604 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1605 #endif
1606 /* Try to make dialog look better. Must realize first so
1607 the widget can calculate the size it needs. */
1608 gtk_widget_realize (w);
1609 gtk_widget_get_preferred_size (w, NULL, &req);
1610 gtk_box_set_spacing (wvbox, req.height);
1611 if (item->value && strlen (item->value) > 0)
1612 button_spacing = 2*req.width/strlen (item->value);
1613 if (button_spacing < 10) button_spacing = 10;
1615 else
1617 /* This is one button to add to the dialog. */
1618 w = gtk_button_new_with_label (utf8_label);
1619 if (! item->enabled)
1620 gtk_widget_set_sensitive (w, FALSE);
1621 if (select_cb)
1622 g_signal_connect (G_OBJECT (w), "clicked",
1623 select_cb, item->call_data);
1625 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1626 if (++button_nr == left_buttons)
1628 if (make_two_rows)
1629 cur_box = GTK_BOX (whbox_down);
1633 if (utf8_label)
1634 g_free (utf8_label);
1637 return wdialog;
1640 struct xg_dialog_data
1642 GMainLoop *loop;
1643 int response;
1644 GtkWidget *w;
1645 guint timerid;
1648 /* Function that is called when the file or font dialogs pop down.
1649 W is the dialog widget, RESPONSE is the response code.
1650 USER_DATA is what we passed in to g_signal_connect. */
1652 static void
1653 xg_dialog_response_cb (GtkDialog *w,
1654 gint response,
1655 gpointer user_data)
1657 struct xg_dialog_data *dd = user_data;
1658 dd->response = response;
1659 g_main_loop_quit (dd->loop);
1663 /* Destroy the dialog. This makes it pop down. */
1665 static void
1666 pop_down_dialog (void *arg)
1668 struct xg_dialog_data *dd = arg;
1670 block_input ();
1671 if (dd->w) gtk_widget_destroy (dd->w);
1672 if (dd->timerid != 0) g_source_remove (dd->timerid);
1674 g_main_loop_quit (dd->loop);
1675 g_main_loop_unref (dd->loop);
1677 unblock_input ();
1680 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1681 We pass in DATA as gpointer* so we can use this as a callback. */
1683 static gboolean
1684 xg_maybe_add_timer (gpointer data)
1686 struct xg_dialog_data *dd = data;
1687 struct timespec next_time = timer_check ();
1689 dd->timerid = 0;
1691 if (timespec_valid_p (next_time))
1693 time_t s = next_time.tv_sec;
1694 int per_ms = TIMESPEC_RESOLUTION / 1000;
1695 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1696 if (s <= ((guint) -1 - ms) / 1000)
1697 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1699 return FALSE;
1703 /* Pops up a modal dialog W and waits for response.
1704 We don't use gtk_dialog_run because we want to process emacs timers.
1705 The dialog W is not destroyed when this function returns. */
1707 static int
1708 xg_dialog_run (struct frame *f, GtkWidget *w)
1710 ptrdiff_t count = SPECPDL_INDEX ();
1711 struct xg_dialog_data dd;
1713 xg_set_screen (w, f);
1714 gtk_window_set_transient_for (GTK_WINDOW (w),
1715 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1716 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1717 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1719 dd.loop = g_main_loop_new (NULL, FALSE);
1720 dd.response = GTK_RESPONSE_CANCEL;
1721 dd.w = w;
1722 dd.timerid = 0;
1724 g_signal_connect (G_OBJECT (w),
1725 "response",
1726 G_CALLBACK (xg_dialog_response_cb),
1727 &dd);
1728 /* Don't destroy the widget if closed by the window manager close button. */
1729 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1730 gtk_widget_show (w);
1732 record_unwind_protect_ptr (pop_down_dialog, &dd);
1734 (void) xg_maybe_add_timer (&dd);
1735 g_main_loop_run (dd.loop);
1737 dd.w = 0;
1738 unbind_to (count, Qnil);
1740 return dd.response;
1744 /***********************************************************************
1745 File dialog functions
1746 ***********************************************************************/
1747 /* Return true if the old file selection dialog is being used. */
1749 bool
1750 xg_uses_old_file_dialog (void)
1752 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1753 return x_gtk_use_old_file_dialog;
1754 #else
1755 return 0;
1756 #endif
1760 typedef char * (*xg_get_file_func) (GtkWidget *);
1762 /* Return the selected file for file chooser dialog W.
1763 The returned string must be free:d. */
1765 static char *
1766 xg_get_file_name_from_chooser (GtkWidget *w)
1768 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1771 /* Callback called when the "Show hidden files" toggle is pressed.
1772 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1774 static void
1775 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1777 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1778 gboolean visible;
1779 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1780 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1784 /* Callback called when a property changes in a file chooser.
1785 GOBJECT is the file chooser dialog, ARG1 describes the property.
1786 USER_DATA is the toggle widget in the file chooser dialog.
1787 We use this to update the "Show hidden files" toggle when the user
1788 changes that property by right clicking in the file list. */
1790 static void
1791 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1793 if (strcmp (arg1->name, "show-hidden") == 0)
1795 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1796 gboolean visible, toggle_on;
1798 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1799 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1801 if (!!visible != !!toggle_on)
1803 gpointer cb = (gpointer) G_CALLBACK (xg_toggle_visibility_cb);
1804 g_signal_handlers_block_by_func (G_OBJECT (wtoggle), cb, gobject);
1805 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1806 g_signal_handlers_unblock_by_func (G_OBJECT (wtoggle), cb, gobject);
1808 x_gtk_show_hidden_files = visible;
1812 /* Read a file name from the user using a file chooser dialog.
1813 F is the current frame.
1814 PROMPT is a prompt to show to the user. May not be NULL.
1815 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1816 If MUSTMATCH_P, the returned file name must be an existing
1817 file. (Actually, this only has cosmetic effects, the user can
1818 still enter a non-existing file.) *FUNC is set to a function that
1819 can be used to retrieve the selected file name from the returned widget.
1821 Returns the created widget. */
1823 static GtkWidget *
1824 xg_get_file_with_chooser (struct frame *f,
1825 char *prompt,
1826 char *default_filename,
1827 bool mustmatch_p, bool only_dir_p,
1828 xg_get_file_func *func)
1830 char msgbuf[1024];
1832 GtkWidget *filewin, *wtoggle, *wbox;
1833 GtkWidget *wmessage UNINIT;
1834 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1835 GtkFileChooserAction action = (mustmatch_p ?
1836 GTK_FILE_CHOOSER_ACTION_OPEN :
1837 GTK_FILE_CHOOSER_ACTION_SAVE);
1839 if (only_dir_p)
1840 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1842 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1843 XG_TEXT_CANCEL, GTK_RESPONSE_CANCEL,
1844 (mustmatch_p || only_dir_p ?
1845 XG_TEXT_OPEN : XG_TEXT_OK),
1846 GTK_RESPONSE_OK,
1847 NULL);
1848 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1850 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1851 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1852 gtk_widget_show (wbox);
1853 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1855 if (x_gtk_show_hidden_files)
1857 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1858 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1860 gtk_widget_show (wtoggle);
1861 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1862 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1863 g_signal_connect (G_OBJECT (filewin), "notify",
1864 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1866 if (x_gtk_file_dialog_help_text)
1868 char *z = msgbuf;
1869 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1870 Show the C-l help text only for versions < 2.10. */
1871 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1872 z = stpcpy (z, "\nType C-l to display a file name text entry box.\n");
1873 strcpy (z, "\nIf you don't like this file selector, use the "
1874 "corresponding\nkey binding or customize "
1875 "use-file-dialog to turn it off.");
1877 wmessage = gtk_label_new (msgbuf);
1878 gtk_widget_show (wmessage);
1881 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1882 if (x_gtk_file_dialog_help_text)
1883 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1884 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1886 if (default_filename)
1888 Lisp_Object file;
1889 char *utf8_filename;
1891 file = build_string (default_filename);
1893 /* File chooser does not understand ~/... in the file name. It must be
1894 an absolute name starting with /. */
1895 if (default_filename[0] != '/')
1896 file = Fexpand_file_name (file, Qnil);
1898 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1899 if (! NILP (Ffile_directory_p (file)))
1900 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1901 utf8_filename);
1902 else
1904 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1905 utf8_filename);
1906 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1908 char *cp = strrchr (utf8_filename, '/');
1909 if (cp) ++cp;
1910 else cp = utf8_filename;
1911 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1916 *func = xg_get_file_name_from_chooser;
1917 return filewin;
1920 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1922 /* Return the selected file for file selector dialog W.
1923 The returned string must be free:d. */
1925 static char *
1926 xg_get_file_name_from_selector (GtkWidget *w)
1928 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1929 return xstrdup (gtk_file_selection_get_filename (filesel));
1932 /* Create a file selection dialog.
1933 F is the current frame.
1934 PROMPT is a prompt to show to the user. May not be NULL.
1935 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1936 If MUSTMATCH_P, the returned file name must be an existing
1937 file. *FUNC is set to a function that can be used to retrieve the
1938 selected file name from the returned widget.
1940 Returns the created widget. */
1942 static GtkWidget *
1943 xg_get_file_with_selection (struct frame *f,
1944 char *prompt,
1945 char *default_filename,
1946 bool mustmatch_p, bool only_dir_p,
1947 xg_get_file_func *func)
1949 GtkWidget *filewin;
1950 GtkFileSelection *filesel;
1952 filewin = gtk_file_selection_new (prompt);
1953 filesel = GTK_FILE_SELECTION (filewin);
1955 if (default_filename)
1956 gtk_file_selection_set_filename (filesel, default_filename);
1958 if (mustmatch_p)
1960 /* The selection_entry part of filesel is not documented. */
1961 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1962 gtk_file_selection_hide_fileop_buttons (filesel);
1965 *func = xg_get_file_name_from_selector;
1967 return filewin;
1969 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1971 /* Read a file name from the user using a file dialog, either the old
1972 file selection dialog, or the new file chooser dialog. Which to use
1973 depends on what the GTK version used has, and what the value of
1974 gtk-use-old-file-dialog.
1975 F is the current frame.
1976 PROMPT is a prompt to show to the user. May not be NULL.
1977 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1978 If MUSTMATCH_P, the returned file name must be an existing
1979 file.
1981 Returns a file name or NULL if no file was selected.
1982 The returned string must be freed by the caller. */
1984 char *
1985 xg_get_file_name (struct frame *f,
1986 char *prompt,
1987 char *default_filename,
1988 bool mustmatch_p,
1989 bool only_dir_p)
1991 GtkWidget *w = 0;
1992 char *fn = 0;
1993 int filesel_done = 0;
1994 xg_get_file_func func;
1996 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1998 if (xg_uses_old_file_dialog ())
1999 w = xg_get_file_with_selection (f, prompt, default_filename,
2000 mustmatch_p, only_dir_p, &func);
2001 else
2002 w = xg_get_file_with_chooser (f, prompt, default_filename,
2003 mustmatch_p, only_dir_p, &func);
2005 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
2006 w = xg_get_file_with_chooser (f, prompt, default_filename,
2007 mustmatch_p, only_dir_p, &func);
2008 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
2010 gtk_widget_set_name (w, "emacs-filedialog");
2012 filesel_done = xg_dialog_run (f, w);
2013 if (filesel_done == GTK_RESPONSE_OK)
2014 fn = (*func) (w);
2016 gtk_widget_destroy (w);
2017 return fn;
2020 /***********************************************************************
2021 GTK font chooser
2022 ***********************************************************************/
2024 #ifdef HAVE_FREETYPE
2026 #if USE_NEW_GTK_FONT_CHOOSER
2028 #define XG_WEIGHT_TO_SYMBOL(w) \
2029 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
2030 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
2031 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
2032 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
2033 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
2034 : w <= PANGO_WEIGHT_BOLD ? Qbold \
2035 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
2036 : Qultra_bold)
2038 #define XG_STYLE_TO_SYMBOL(s) \
2039 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
2040 : s == PANGO_STYLE_ITALIC ? Qitalic \
2041 : Qnormal)
2043 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2046 static char *x_last_font_name;
2048 /* Pop up a GTK font selector and return the name of the font the user
2049 selects, as a C string. The returned font name follows GTK's own
2050 format:
2052 `FAMILY [VALUE1 VALUE2] SIZE'
2054 This can be parsed using font_parse_fcname in font.c.
2055 DEFAULT_NAME, if non-zero, is the default font name. */
2057 Lisp_Object
2058 xg_get_font (struct frame *f, const char *default_name)
2060 GtkWidget *w;
2061 int done = 0;
2062 Lisp_Object font = Qnil;
2064 w = gtk_font_chooser_dialog_new
2065 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2067 if (default_name)
2069 /* Convert fontconfig names to Gtk names, i.e. remove - before
2070 number */
2071 char *p = strrchr (default_name, '-');
2072 if (p)
2074 char *ep = p+1;
2075 while (c_isdigit (*ep))
2076 ++ep;
2077 if (*ep == '\0') *p = ' ';
2080 else if (x_last_font_name)
2081 default_name = x_last_font_name;
2083 if (default_name)
2084 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2086 gtk_widget_set_name (w, "emacs-fontdialog");
2087 done = xg_dialog_run (f, w);
2088 if (done == GTK_RESPONSE_OK)
2090 #if USE_NEW_GTK_FONT_CHOOSER
2091 /* Use the GTK3 font chooser. */
2092 PangoFontDescription *desc
2093 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2095 if (desc)
2097 const char *name = pango_font_description_get_family (desc);
2098 gint size = pango_font_description_get_size (desc);
2099 PangoWeight weight = pango_font_description_get_weight (desc);
2100 PangoStyle style = pango_font_description_get_style (desc);
2102 #ifdef USE_CAIRO
2103 #define FONT_TYPE_WANTED (Qftcr)
2104 #else
2105 #define FONT_TYPE_WANTED (Qxft)
2106 #endif
2107 font = CALLN (Ffont_spec,
2108 QCname, build_string (name),
2109 QCsize, make_float (pango_units_to_double (size)),
2110 QCweight, XG_WEIGHT_TO_SYMBOL (weight),
2111 QCslant, XG_STYLE_TO_SYMBOL (style),
2112 QCtype,
2113 FONT_TYPE_WANTED);
2115 pango_font_description_free (desc);
2116 dupstring (&x_last_font_name, name);
2119 #else /* Use old font selector, which just returns the font name. */
2121 char *font_name
2122 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2124 if (font_name)
2126 font = build_string (font_name);
2127 g_free (x_last_font_name);
2128 x_last_font_name = font_name;
2130 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2133 gtk_widget_destroy (w);
2134 return font;
2136 #endif /* HAVE_FREETYPE */
2140 /***********************************************************************
2141 Menu functions.
2142 ***********************************************************************/
2144 /* The name of menu items that can be used for customization. Since GTK
2145 RC files are very crude and primitive, we have to set this on all
2146 menu item names so a user can easily customize menu items. */
2148 #define MENU_ITEM_NAME "emacs-menuitem"
2151 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2152 during GC. The next member points to the items. */
2153 static xg_list_node xg_menu_cb_list;
2155 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2156 during GC. The next member points to the items. */
2157 static xg_list_node xg_menu_item_cb_list;
2159 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2160 F is the frame CL_DATA will be initialized for.
2161 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2163 The menu bar and all sub menus under the menu bar in a frame
2164 share the same structure, hence the reference count.
2166 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2167 allocated xg_menu_cb_data if CL_DATA is NULL. */
2169 static xg_menu_cb_data *
2170 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2172 if (! cl_data)
2174 cl_data = xmalloc (sizeof *cl_data);
2175 cl_data->f = f;
2176 cl_data->menu_bar_vector = f->menu_bar_vector;
2177 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2178 cl_data->highlight_cb = highlight_cb;
2179 cl_data->ref_count = 0;
2181 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2184 cl_data->ref_count++;
2186 return cl_data;
2189 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2190 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2192 When the menu bar is updated, menu items may have been added and/or
2193 removed, so menu_bar_vector and menu_bar_items_used change. We must
2194 then update CL_DATA since it is used to determine which menu
2195 item that is invoked in the menu.
2196 HIGHLIGHT_CB could change, there is no check that the same
2197 function is given when modifying a menu bar as was given when
2198 creating the menu bar. */
2200 static void
2201 update_cl_data (xg_menu_cb_data *cl_data,
2202 struct frame *f,
2203 GCallback highlight_cb)
2205 if (cl_data)
2207 cl_data->f = f;
2208 cl_data->menu_bar_vector = f->menu_bar_vector;
2209 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2210 cl_data->highlight_cb = highlight_cb;
2214 /* Decrease reference count for CL_DATA.
2215 If reference count is zero, free CL_DATA. */
2217 static void
2218 unref_cl_data (xg_menu_cb_data *cl_data)
2220 if (cl_data && cl_data->ref_count > 0)
2222 cl_data->ref_count--;
2223 if (cl_data->ref_count == 0)
2225 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2226 xfree (cl_data);
2231 /* Function that marks all lisp data during GC. */
2233 void
2234 xg_mark_data (void)
2236 xg_list_node *iter;
2237 Lisp_Object rest, frame;
2239 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2240 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2242 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2244 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2246 if (! NILP (cb_data->help))
2247 mark_object (cb_data->help);
2250 FOR_EACH_FRAME (rest, frame)
2252 struct frame *f = XFRAME (frame);
2254 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2256 struct xg_frame_tb_info *tbinfo
2257 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2258 TB_INFO_KEY);
2259 if (tbinfo)
2261 mark_object (tbinfo->last_tool_bar);
2262 mark_object (tbinfo->style);
2269 /* Callback called when a menu item is destroyed. Used to free data.
2270 W is the widget that is being destroyed (not used).
2271 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2273 static void
2274 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2276 if (client_data)
2278 xg_menu_item_cb_data *data = client_data;
2279 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2280 xfree (data);
2284 /* Callback called when the pointer enters/leaves a menu item.
2285 W is the parent of the menu item.
2286 EVENT is either an enter event or leave event.
2287 CLIENT_DATA is not used.
2289 Returns FALSE to tell GTK to keep processing this event. */
2291 static gboolean
2292 menuitem_highlight_callback (GtkWidget *w,
2293 GdkEventCrossing *event,
2294 gpointer client_data)
2296 GdkEvent ev;
2297 GtkWidget *subwidget;
2298 xg_menu_item_cb_data *data;
2300 ev.crossing = *event;
2301 subwidget = gtk_get_event_widget (&ev);
2302 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2303 if (data)
2305 if (! NILP (data->help) && data->cl_data->highlight_cb)
2307 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2308 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2309 (*func) (subwidget, call_data);
2313 return FALSE;
2316 /* Callback called when a menu is destroyed. Used to free data.
2317 W is the widget that is being destroyed (not used).
2318 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2320 static void
2321 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2323 unref_cl_data (client_data);
2326 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2327 must be non-NULL) and can be inserted into a menu item.
2329 Returns the GtkHBox. */
2331 static GtkWidget *
2332 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2334 GtkWidget *wlbl;
2335 GtkWidget *wkey;
2336 GtkWidget *wbox;
2338 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2339 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2340 wlbl = gtk_label_new (utf8_label);
2341 wkey = gtk_label_new (utf8_key);
2343 #if GTK_CHECK_VERSION (3, 14, 0)
2344 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2345 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2346 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2347 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2348 #else
2349 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2350 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2351 #endif
2352 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2353 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2355 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2356 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2357 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2359 return wbox;
2362 /* Make and return a menu item widget with the key to the right.
2363 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2364 UTF8_KEY is the text representing the key binding.
2365 ITEM is the widget_value describing the menu item.
2367 GROUP is an in/out parameter. If the menu item to be created is not
2368 part of any radio menu group, *GROUP contains NULL on entry and exit.
2369 If the menu item to be created is part of a radio menu group, on entry
2370 *GROUP contains the group to use, or NULL if this is the first item
2371 in the group. On exit, *GROUP contains the radio item group.
2373 Unfortunately, keys don't line up as nicely as in Motif,
2374 but the MacOS X version doesn't either, so I guess that is OK. */
2376 static GtkWidget *
2377 make_menu_item (const char *utf8_label,
2378 const char *utf8_key,
2379 widget_value *item,
2380 GSList **group)
2382 GtkWidget *w;
2383 GtkWidget *wtoadd = 0;
2385 /* It has been observed that some menu items have a NULL name field.
2386 This will lead to this function being called with a NULL utf8_label.
2387 GTK crashes on that so we set a blank label. Why there is a NULL
2388 name remains to be investigated. */
2389 if (! utf8_label) utf8_label = " ";
2391 if (utf8_key)
2392 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2394 if (item->button_type == BUTTON_TYPE_TOGGLE)
2396 *group = NULL;
2397 if (utf8_key) w = gtk_check_menu_item_new ();
2398 else w = gtk_check_menu_item_new_with_label (utf8_label);
2399 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2401 else if (item->button_type == BUTTON_TYPE_RADIO)
2403 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2404 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2405 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2406 if (item->selected)
2407 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2409 else
2411 *group = NULL;
2412 if (utf8_key) w = gtk_menu_item_new ();
2413 else w = gtk_menu_item_new_with_label (utf8_label);
2416 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2417 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2419 return w;
2422 /* Create a menu item widget, and connect the callbacks.
2423 ITEM describes the menu item.
2424 F is the frame the created menu belongs to.
2425 SELECT_CB is the callback to use when a menu item is selected.
2426 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2427 CL_DATA points to the callback data to be used for this menu.
2428 GROUP is an in/out parameter. If the menu item to be created is not
2429 part of any radio menu group, *GROUP contains NULL on entry and exit.
2430 If the menu item to be created is part of a radio menu group, on entry
2431 *GROUP contains the group to use, or NULL if this is the first item
2432 in the group. On exit, *GROUP contains the radio item group.
2434 Returns the created GtkWidget. */
2436 static GtkWidget *
2437 xg_create_one_menuitem (widget_value *item,
2438 struct frame *f,
2439 GCallback select_cb,
2440 GCallback highlight_cb,
2441 xg_menu_cb_data *cl_data,
2442 GSList **group)
2444 char *utf8_label;
2445 char *utf8_key;
2446 GtkWidget *w;
2447 xg_menu_item_cb_data *cb_data;
2449 utf8_label = get_utf8_string (item->name);
2450 utf8_key = get_utf8_string (item->key);
2452 w = make_menu_item (utf8_label, utf8_key, item, group);
2454 if (utf8_label) g_free (utf8_label);
2455 if (utf8_key) g_free (utf8_key);
2457 cb_data = xmalloc (sizeof *cb_data);
2459 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2461 cb_data->select_id = 0;
2462 cb_data->help = item->help;
2463 cb_data->cl_data = cl_data;
2464 cb_data->call_data = item->call_data;
2466 g_signal_connect (G_OBJECT (w),
2467 "destroy",
2468 G_CALLBACK (menuitem_destroy_callback),
2469 cb_data);
2471 /* Put cb_data in widget, so we can get at it when modifying menubar */
2472 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2474 /* final item, not a submenu */
2475 if (item->call_data && ! item->contents)
2477 if (select_cb)
2478 cb_data->select_id
2479 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2482 return w;
2485 /* Create a full menu tree specified by DATA.
2486 F is the frame the created menu belongs to.
2487 SELECT_CB is the callback to use when a menu item is selected.
2488 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2489 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2490 If POP_UP_P, create a popup menu.
2491 If MENU_BAR_P, create a menu bar.
2492 TOPMENU is the topmost GtkWidget that others shall be placed under.
2493 It may be NULL, in that case we create the appropriate widget
2494 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2495 CL_DATA is the callback data we shall use for this menu, or NULL
2496 if we haven't set the first callback yet.
2497 NAME is the name to give to the top level menu if this function
2498 creates it. May be NULL to not set any name.
2500 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2501 not NULL.
2503 This function calls itself to create submenus. */
2505 static GtkWidget *
2506 create_menus (widget_value *data,
2507 struct frame *f,
2508 GCallback select_cb,
2509 GCallback deactivate_cb,
2510 GCallback highlight_cb,
2511 bool pop_up_p,
2512 bool menu_bar_p,
2513 GtkWidget *topmenu,
2514 xg_menu_cb_data *cl_data,
2515 const char *name)
2517 widget_value *item;
2518 GtkWidget *wmenu = topmenu;
2519 GSList *group = NULL;
2521 if (! topmenu)
2523 if (! menu_bar_p)
2525 wmenu = gtk_menu_new ();
2526 xg_set_screen (wmenu, f);
2527 /* Connect this to the menu instead of items so we get enter/leave for
2528 disabled items also. TODO: Still does not get enter/leave for
2529 disabled items in detached menus. */
2530 g_signal_connect (G_OBJECT (wmenu),
2531 "enter-notify-event",
2532 G_CALLBACK (menuitem_highlight_callback),
2533 NULL);
2534 g_signal_connect (G_OBJECT (wmenu),
2535 "leave-notify-event",
2536 G_CALLBACK (menuitem_highlight_callback),
2537 NULL);
2539 else
2541 wmenu = gtk_menu_bar_new ();
2542 /* Set width of menu bar to a small value so it doesn't enlarge
2543 a small initial frame size. The width will be set to the
2544 width of the frame later on when it is added to a container.
2545 height -1: Natural height. */
2546 gtk_widget_set_size_request (wmenu, 1, -1);
2549 /* Put cl_data on the top menu for easier access. */
2550 cl_data = make_cl_data (cl_data, f, highlight_cb);
2551 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2552 g_signal_connect (G_OBJECT (wmenu), "destroy",
2553 G_CALLBACK (menu_destroy_callback), cl_data);
2555 if (name)
2556 gtk_widget_set_name (wmenu, name);
2558 if (deactivate_cb)
2559 g_signal_connect (G_OBJECT (wmenu),
2560 "selection-done", deactivate_cb, 0);
2563 for (item = data; item; item = item->next)
2565 GtkWidget *w;
2567 if (pop_up_p && !item->contents && !item->call_data
2568 && !menu_separator_name_p (item->name))
2570 char *utf8_label;
2571 /* A title for a popup. We do the same as GTK does when
2572 creating titles, but it does not look good. */
2573 group = NULL;
2574 utf8_label = get_utf8_string (item->name);
2576 w = gtk_menu_item_new_with_label (utf8_label);
2577 gtk_widget_set_sensitive (w, FALSE);
2578 if (utf8_label) g_free (utf8_label);
2580 else if (menu_separator_name_p (item->name))
2582 group = NULL;
2583 /* GTK only have one separator type. */
2584 w = gtk_separator_menu_item_new ();
2586 else
2588 w = xg_create_one_menuitem (item,
2590 item->contents ? 0 : select_cb,
2591 highlight_cb,
2592 cl_data,
2593 &group);
2595 /* Create a possibly empty submenu for menu bar items, since some
2596 themes don't highlight items correctly without it. */
2597 if (item->contents || menu_bar_p)
2599 GtkWidget *submenu = create_menus (item->contents,
2601 select_cb,
2602 deactivate_cb,
2603 highlight_cb,
2607 cl_data,
2609 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2613 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2614 gtk_widget_set_name (w, MENU_ITEM_NAME);
2617 return wmenu;
2620 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2621 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2622 with some text and buttons.
2623 F is the frame the created item belongs to.
2624 NAME is the name to use for the top widget.
2625 VAL is a widget_value structure describing items to be created.
2626 SELECT_CB is the callback to use when a menu item is selected or
2627 a dialog button is pressed.
2628 DEACTIVATE_CB is the callback to use when an item is deactivated.
2629 For a menu, when a sub menu is not shown anymore, for a dialog it is
2630 called when the dialog is popped down.
2631 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2633 Returns the widget created. */
2635 GtkWidget *
2636 xg_create_widget (const char *type, const char *name, struct frame *f,
2637 widget_value *val, GCallback select_cb,
2638 GCallback deactivate_cb, GCallback highlight_cb)
2640 GtkWidget *w = 0;
2641 bool menu_bar_p = strcmp (type, "menubar") == 0;
2642 bool pop_up_p = strcmp (type, "popup") == 0;
2644 if (strcmp (type, "dialog") == 0)
2646 w = create_dialog (val, select_cb, deactivate_cb);
2647 xg_set_screen (w, f);
2648 gtk_window_set_transient_for (GTK_WINDOW (w),
2649 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2650 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2651 gtk_widget_set_name (w, "emacs-dialog");
2652 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2654 else if (menu_bar_p || pop_up_p)
2656 w = create_menus (val->contents,
2658 select_cb,
2659 deactivate_cb,
2660 highlight_cb,
2661 pop_up_p,
2662 menu_bar_p,
2665 name);
2667 /* Set the cursor to an arrow for popup menus when they are mapped.
2668 This is done by default for menu bar menus. */
2669 if (pop_up_p)
2671 /* Must realize so the GdkWindow inside the widget is created. */
2672 gtk_widget_realize (w);
2673 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2676 else
2678 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2679 type);
2682 return w;
2685 /* Return the label for menu item WITEM. */
2687 static const char *
2688 xg_get_menu_item_label (GtkMenuItem *witem)
2690 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2691 return gtk_label_get_label (wlabel);
2694 /* Return true if the menu item WITEM has the text LABEL. */
2696 static bool
2697 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2699 bool is_same = 0;
2700 char *utf8_label = get_utf8_string (label);
2701 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2703 if (! old_label && ! utf8_label)
2704 is_same = 1;
2705 else if (old_label && utf8_label)
2706 is_same = strcmp (utf8_label, old_label) == 0;
2708 if (utf8_label) g_free (utf8_label);
2710 return is_same;
2713 /* Destroy widgets in LIST. */
2715 static void
2716 xg_destroy_widgets (GList *list)
2718 GList *iter;
2720 for (iter = list; iter; iter = g_list_next (iter))
2722 GtkWidget *w = GTK_WIDGET (iter->data);
2724 /* Destroying the widget will remove it from the container it is in. */
2725 gtk_widget_destroy (w);
2729 /* Update the top level names in MENUBAR (i.e. not submenus).
2730 F is the frame the menu bar belongs to.
2731 *LIST is a list with the current menu bar names (menu item widgets).
2732 ITER is the item within *LIST that shall be updated.
2733 POS is the numerical position, starting at 0, of ITER in *LIST.
2734 VAL describes what the menu bar shall look like after the update.
2735 SELECT_CB is the callback to use when a menu item is selected.
2736 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2737 CL_DATA points to the callback data to be used for this menu bar.
2739 This function calls itself to walk through the menu bar names. */
2741 static void
2742 xg_update_menubar (GtkWidget *menubar,
2743 struct frame *f,
2744 GList **list,
2745 GList *iter,
2746 int pos,
2747 widget_value *val,
2748 GCallback select_cb,
2749 GCallback deactivate_cb,
2750 GCallback highlight_cb,
2751 xg_menu_cb_data *cl_data)
2753 if (! iter && ! val)
2754 return;
2755 else if (iter && ! val)
2757 /* Item(s) have been removed. Remove all remaining items. */
2758 xg_destroy_widgets (iter);
2760 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2761 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2762 gtk_menu_item_new_with_label (""),
2764 /* All updated. */
2765 val = 0;
2766 iter = 0;
2768 else if (! iter && val)
2770 /* Item(s) added. Add all new items in one call. */
2771 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2772 0, 1, menubar, cl_data, 0);
2774 /* All updated. */
2775 val = 0;
2776 iter = 0;
2778 /* Below this neither iter or val is NULL */
2779 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2781 /* This item is still the same, check next item. */
2782 val = val->next;
2783 iter = g_list_next (iter);
2784 ++pos;
2786 else /* This item is changed. */
2788 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2789 GtkMenuItem *witem2 = 0;
2790 bool val_in_menubar = 0;
2791 bool iter_in_new_menubar = 0;
2792 GList *iter2;
2793 widget_value *cur;
2795 /* See if the changed entry (val) is present later in the menu bar */
2796 for (iter2 = iter;
2797 iter2 && ! val_in_menubar;
2798 iter2 = g_list_next (iter2))
2800 witem2 = GTK_MENU_ITEM (iter2->data);
2801 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2804 /* See if the current entry (iter) is present later in the
2805 specification for the new menu bar. */
2806 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2807 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2809 if (val_in_menubar && ! iter_in_new_menubar)
2811 int nr = pos;
2813 /* This corresponds to:
2814 Current: A B C
2815 New: A C
2816 Remove B. */
2818 g_object_ref (G_OBJECT (witem));
2819 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2820 gtk_widget_destroy (GTK_WIDGET (witem));
2822 /* Must get new list since the old changed. */
2823 g_list_free (*list);
2824 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2825 while (nr-- > 0) iter = g_list_next (iter);
2827 else if (! val_in_menubar && ! iter_in_new_menubar)
2829 /* This corresponds to:
2830 Current: A B C
2831 New: A X C
2832 Rename B to X. This might seem to be a strange thing to do,
2833 since if there is a menu under B it will be totally wrong for X.
2834 But consider editing a C file. Then there is a C-mode menu
2835 (corresponds to B above).
2836 If then doing C-x C-f the minibuf menu (X above) replaces the
2837 C-mode menu. When returning from the minibuffer, we get
2838 back the C-mode menu. Thus we do:
2839 Rename B to X (C-mode to minibuf menu)
2840 Rename X to B (minibuf to C-mode menu).
2841 If the X menu hasn't been invoked, the menu under B
2842 is up to date when leaving the minibuffer. */
2843 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2844 char *utf8_label = get_utf8_string (val->name);
2846 /* GTK menu items don't notice when their labels have been
2847 changed from underneath them, so we have to explicitly
2848 use g_object_notify to tell listeners (e.g., a GMenuModel
2849 bridge that might be loaded) that the item's label has
2850 changed. */
2851 gtk_label_set_text (wlabel, utf8_label);
2852 #if GTK_CHECK_VERSION (2, 16, 0)
2853 g_object_notify (G_OBJECT (witem), "label");
2854 #endif
2855 if (utf8_label) g_free (utf8_label);
2856 iter = g_list_next (iter);
2857 val = val->next;
2858 ++pos;
2860 else if (! val_in_menubar && iter_in_new_menubar)
2862 /* This corresponds to:
2863 Current: A B C
2864 New: A X B C
2865 Insert X. */
2867 int nr = pos;
2868 GSList *group = 0;
2869 GtkWidget *w = xg_create_one_menuitem (val,
2871 select_cb,
2872 highlight_cb,
2873 cl_data,
2874 &group);
2876 /* Create a possibly empty submenu for menu bar items, since some
2877 themes don't highlight items correctly without it. */
2878 GtkWidget *submenu = create_menus (NULL, f,
2879 select_cb, deactivate_cb,
2880 highlight_cb,
2881 0, 0, 0, cl_data, 0);
2883 gtk_widget_set_name (w, MENU_ITEM_NAME);
2884 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2885 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2887 g_list_free (*list);
2888 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2889 while (nr-- > 0) iter = g_list_next (iter);
2890 iter = g_list_next (iter);
2891 val = val->next;
2892 ++pos;
2894 else /* if (val_in_menubar && iter_in_new_menubar) */
2896 int nr = pos;
2897 /* This corresponds to:
2898 Current: A B C
2899 New: A C B
2900 Move C before B */
2902 g_object_ref (G_OBJECT (witem2));
2903 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2904 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2905 GTK_WIDGET (witem2), pos);
2906 g_object_unref (G_OBJECT (witem2));
2908 g_list_free (*list);
2909 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2910 while (nr-- > 0) iter = g_list_next (iter);
2911 if (iter) iter = g_list_next (iter);
2912 val = val->next;
2913 ++pos;
2917 /* Update the rest of the menu bar. */
2918 xg_update_menubar (menubar, f, list, iter, pos, val,
2919 select_cb, deactivate_cb, highlight_cb, cl_data);
2922 /* Update the menu item W so it corresponds to VAL.
2923 SELECT_CB is the callback to use when a menu item is selected.
2924 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2925 CL_DATA is the data to set in the widget for menu invocation. */
2927 static void
2928 xg_update_menu_item (widget_value *val,
2929 GtkWidget *w,
2930 GCallback select_cb,
2931 GCallback highlight_cb,
2932 xg_menu_cb_data *cl_data)
2934 GtkWidget *wchild;
2935 GtkLabel *wlbl = 0;
2936 GtkLabel *wkey = 0;
2937 char *utf8_label;
2938 char *utf8_key;
2939 const char *old_label = 0;
2940 const char *old_key = 0;
2941 xg_menu_item_cb_data *cb_data;
2942 bool label_changed = false;
2944 wchild = XG_BIN_CHILD (w);
2945 utf8_label = get_utf8_string (val->name);
2946 utf8_key = get_utf8_string (val->key);
2948 /* See if W is a menu item with a key. See make_menu_item above. */
2949 if (GTK_IS_BOX (wchild))
2951 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2953 wlbl = GTK_LABEL (list->data);
2954 wkey = GTK_LABEL (list->next->data);
2955 g_list_free (list);
2957 if (! utf8_key)
2959 /* Remove the key and keep just the label. */
2960 g_object_ref (G_OBJECT (wlbl));
2961 gtk_container_remove (GTK_CONTAINER (w), wchild);
2962 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2963 g_object_unref (G_OBJECT (wlbl));
2964 wkey = 0;
2968 else /* Just a label. */
2970 wlbl = GTK_LABEL (wchild);
2972 /* Check if there is now a key. */
2973 if (utf8_key)
2975 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2976 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2978 wlbl = GTK_LABEL (list->data);
2979 wkey = GTK_LABEL (list->next->data);
2980 g_list_free (list);
2982 gtk_container_remove (GTK_CONTAINER (w), wchild);
2983 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2987 if (wkey) old_key = gtk_label_get_label (wkey);
2988 if (wlbl) old_label = gtk_label_get_label (wlbl);
2990 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2992 label_changed = true;
2993 gtk_label_set_text (wkey, utf8_key);
2996 if (! old_label || strcmp (utf8_label, old_label) != 0)
2998 label_changed = true;
2999 gtk_label_set_text (wlbl, utf8_label);
3002 if (utf8_key) g_free (utf8_key);
3003 if (utf8_label) g_free (utf8_label);
3005 if (! val->enabled && gtk_widget_get_sensitive (w))
3006 gtk_widget_set_sensitive (w, FALSE);
3007 else if (val->enabled && ! gtk_widget_get_sensitive (w))
3008 gtk_widget_set_sensitive (w, TRUE);
3010 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
3011 if (cb_data)
3013 cb_data->call_data = val->call_data;
3014 cb_data->help = val->help;
3015 cb_data->cl_data = cl_data;
3017 /* We assume the callback functions don't change. */
3018 if (val->call_data && ! val->contents)
3020 /* This item shall have a select callback. */
3021 if (! cb_data->select_id)
3022 cb_data->select_id
3023 = g_signal_connect (G_OBJECT (w), "activate",
3024 select_cb, cb_data);
3026 else if (cb_data->select_id)
3028 g_signal_handler_disconnect (w, cb_data->select_id);
3029 cb_data->select_id = 0;
3033 #if GTK_CHECK_VERSION (2, 16, 0)
3034 if (label_changed) /* See comment in xg_update_menubar. */
3035 g_object_notify (G_OBJECT (w), "label");
3036 #endif
3039 /* Update the toggle menu item W so it corresponds to VAL. */
3041 static void
3042 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3044 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3047 /* Update the radio menu item W so it corresponds to VAL. */
3049 static void
3050 xg_update_radio_item (widget_value *val, GtkWidget *w)
3052 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3055 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3056 SUBMENU may be NULL, in that case a new menu is created.
3057 F is the frame the menu bar belongs to.
3058 VAL describes the contents of the menu bar.
3059 SELECT_CB is the callback to use when a menu item is selected.
3060 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3061 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3062 CL_DATA is the call back data to use for any newly created items.
3064 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3065 was NULL. */
3067 static GtkWidget *
3068 xg_update_submenu (GtkWidget *submenu,
3069 struct frame *f,
3070 widget_value *val,
3071 GCallback select_cb,
3072 GCallback deactivate_cb,
3073 GCallback highlight_cb,
3074 xg_menu_cb_data *cl_data)
3076 GtkWidget *newsub = submenu;
3077 GList *list = 0;
3078 GList *iter;
3079 widget_value *cur;
3080 GList *first_radio = 0;
3082 if (submenu)
3083 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3085 for (cur = val, iter = list;
3086 cur && iter;
3087 iter = g_list_next (iter), cur = cur->next)
3089 GtkWidget *w = GTK_WIDGET (iter->data);
3091 /* Remember first radio button in a group. If we get a mismatch in
3092 a radio group we must rebuild the whole group so that the connections
3093 in GTK becomes correct. */
3094 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3095 first_radio = iter;
3096 else if (cur->button_type != BUTTON_TYPE_RADIO
3097 && ! GTK_IS_RADIO_MENU_ITEM (w))
3098 first_radio = 0;
3100 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3102 if (! menu_separator_name_p (cur->name))
3103 break;
3105 else if (GTK_IS_CHECK_MENU_ITEM (w))
3107 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3108 break;
3109 xg_update_toggle_item (cur, w);
3110 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3112 else if (GTK_IS_RADIO_MENU_ITEM (w))
3114 if (cur->button_type != BUTTON_TYPE_RADIO)
3115 break;
3116 xg_update_radio_item (cur, w);
3117 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3119 else if (GTK_IS_MENU_ITEM (w))
3121 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3122 GtkWidget *sub;
3124 if (cur->button_type != BUTTON_TYPE_NONE ||
3125 menu_separator_name_p (cur->name))
3126 break;
3128 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3130 sub = gtk_menu_item_get_submenu (witem);
3131 if (sub && ! cur->contents)
3133 /* Not a submenu anymore. */
3134 g_object_ref (G_OBJECT (sub));
3135 remove_submenu (witem);
3136 gtk_widget_destroy (sub);
3138 else if (cur->contents)
3140 GtkWidget *nsub;
3142 nsub = xg_update_submenu (sub, f, cur->contents,
3143 select_cb, deactivate_cb,
3144 highlight_cb, cl_data);
3146 /* If this item just became a submenu, we must set it. */
3147 if (nsub != sub)
3148 gtk_menu_item_set_submenu (witem, nsub);
3151 else
3153 /* Structural difference. Remove everything from here and down
3154 in SUBMENU. */
3155 break;
3159 /* Remove widgets from first structural change. */
3160 if (iter)
3162 /* If we are adding new menu items below, we must remove from
3163 first radio button so that radio groups become correct. */
3164 if (cur && first_radio) xg_destroy_widgets (first_radio);
3165 else xg_destroy_widgets (iter);
3168 if (cur)
3170 /* More items added. Create them. */
3171 newsub = create_menus (cur,
3173 select_cb,
3174 deactivate_cb,
3175 highlight_cb,
3178 submenu,
3179 cl_data,
3183 if (list) g_list_free (list);
3185 return newsub;
3188 /* Update the MENUBAR.
3189 F is the frame the menu bar belongs to.
3190 VAL describes the contents of the menu bar.
3191 If DEEP_P, rebuild all but the top level menu names in
3192 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3193 SELECT_CB is the callback to use when a menu item is selected.
3194 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3195 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3197 void
3198 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3199 widget_value *val, bool deep_p,
3200 GCallback select_cb, GCallback deactivate_cb,
3201 GCallback highlight_cb)
3203 xg_menu_cb_data *cl_data;
3204 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3206 if (! list) return;
3208 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3210 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3211 select_cb, deactivate_cb, highlight_cb, cl_data);
3213 if (deep_p)
3215 widget_value *cur;
3217 /* Update all sub menus.
3218 We must keep the submenus (GTK menu item widgets) since the
3219 X Window in the XEvent that activates the menu are those widgets. */
3221 /* Update cl_data, menu_item things in F may have changed. */
3222 update_cl_data (cl_data, f, highlight_cb);
3224 for (cur = val->contents; cur; cur = cur->next)
3226 GList *iter;
3227 GtkWidget *sub = 0;
3228 GtkWidget *newsub;
3229 GtkMenuItem *witem = 0;
3231 /* Find sub menu that corresponds to val and update it. */
3232 for (iter = list ; iter; iter = g_list_next (iter))
3234 witem = GTK_MENU_ITEM (iter->data);
3235 if (xg_item_label_same_p (witem, cur->name))
3237 sub = gtk_menu_item_get_submenu (witem);
3238 break;
3242 newsub = xg_update_submenu (sub,
3244 cur->contents,
3245 select_cb,
3246 deactivate_cb,
3247 highlight_cb,
3248 cl_data);
3249 /* sub may still be NULL. If we just updated non deep and added
3250 a new menu bar item, it has no sub menu yet. So we set the
3251 newly created sub menu under witem. */
3252 if (newsub != sub && witem != 0)
3254 xg_set_screen (newsub, f);
3255 gtk_menu_item_set_submenu (witem, newsub);
3260 g_list_free (list);
3261 gtk_widget_show_all (menubar);
3264 /* Callback called when the menu bar W is mapped.
3265 Used to find the height of the menu bar if we didn't get it
3266 after showing the widget. */
3268 static void
3269 menubar_map_cb (GtkWidget *w, gpointer user_data)
3271 GtkRequisition req;
3272 struct frame *f = user_data;
3273 gtk_widget_get_preferred_size (w, NULL, &req);
3274 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3276 FRAME_MENUBAR_HEIGHT (f) = req.height;
3277 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3281 /* Recompute all the widgets of frame F, when the menu bar has been
3282 changed. */
3284 void
3285 xg_update_frame_menubar (struct frame *f)
3287 struct x_output *x = f->output_data.x;
3288 GtkRequisition req;
3290 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3291 return;
3293 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3294 return; /* Already done this, happens for frames created invisible. */
3296 block_input ();
3298 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3299 FALSE, FALSE, 0);
3300 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3302 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3303 gtk_widget_show_all (x->menubar_widget);
3304 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3306 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3308 FRAME_MENUBAR_HEIGHT (f) = req.height;
3309 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3311 unblock_input ();
3314 /* Get rid of the menu bar of frame F, and free its storage.
3315 This is used when deleting a frame, and when turning off the menu bar. */
3317 void
3318 free_frame_menubar (struct frame *f)
3320 struct x_output *x = f->output_data.x;
3322 if (x->menubar_widget)
3324 block_input ();
3326 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3327 /* The menubar and its children shall be deleted when removed from
3328 the container. */
3329 x->menubar_widget = 0;
3330 FRAME_MENUBAR_HEIGHT (f) = 0;
3331 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3332 unblock_input ();
3336 bool
3337 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3339 struct x_output *x = f->output_data.x;
3340 GList *iter;
3341 GdkRectangle rec;
3342 GList *list;
3343 GdkDisplay *gdpy;
3344 GdkWindow *gw;
3345 GdkEvent gevent;
3346 GtkWidget *gwdesc;
3348 if (! x->menubar_widget) return 0;
3350 if (! (event->xbutton.x >= 0
3351 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3352 && event->xbutton.y >= 0
3353 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3354 && event->xbutton.same_screen))
3355 return 0;
3357 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3358 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3359 if (! gw) return 0;
3360 gevent.any.window = gw;
3361 gevent.any.type = GDK_NOTHING;
3362 gwdesc = gtk_get_event_widget (&gevent);
3363 if (! gwdesc) return 0;
3364 if (! GTK_IS_MENU_BAR (gwdesc)
3365 && ! GTK_IS_MENU_ITEM (gwdesc)
3366 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3367 return 0;
3369 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3370 if (! list) return 0;
3371 rec.x = event->xbutton.x;
3372 rec.y = event->xbutton.y;
3373 rec.width = 1;
3374 rec.height = 1;
3376 for (iter = list ; iter; iter = g_list_next (iter))
3378 GtkWidget *w = GTK_WIDGET (iter->data);
3379 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3380 break;
3382 g_list_free (list);
3383 return iter != 0;
3388 /***********************************************************************
3389 Scroll bar functions
3390 ***********************************************************************/
3393 /* Setting scroll bar values invokes the callback. Use this variable
3394 to indicate that callback should do nothing. */
3396 bool xg_ignore_gtk_scrollbar;
3398 /* Width and height of scroll bars for the current theme. */
3399 static int scroll_bar_width_for_theme;
3400 static int scroll_bar_height_for_theme;
3402 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3403 may be larger than 32 bits. Keep a mapping from integer index to widget
3404 pointers to get around the 32 bit limitation. */
3406 static struct
3408 GtkWidget **widgets;
3409 ptrdiff_t max_size;
3410 ptrdiff_t used;
3411 } id_to_widget;
3413 /* Grow this much every time we need to allocate more */
3415 #define ID_TO_WIDGET_INCR 32
3417 /* Store the widget pointer W in id_to_widget and return the integer index. */
3419 static ptrdiff_t
3420 xg_store_widget_in_map (GtkWidget *w)
3422 ptrdiff_t i;
3424 if (id_to_widget.max_size == id_to_widget.used)
3426 ptrdiff_t new_size;
3427 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3428 memory_full (SIZE_MAX);
3430 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3431 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3432 new_size, sizeof (GtkWidget *));
3434 for (i = id_to_widget.max_size; i < new_size; ++i)
3435 id_to_widget.widgets[i] = 0;
3436 id_to_widget.max_size = new_size;
3439 /* Just loop over the array and find a free place. After all,
3440 how many scroll bars are we creating? Should be a small number.
3441 The check above guarantees we will find a free place. */
3442 for (i = 0; i < id_to_widget.max_size; ++i)
3444 if (! id_to_widget.widgets[i])
3446 id_to_widget.widgets[i] = w;
3447 ++id_to_widget.used;
3449 return i;
3453 /* Should never end up here */
3454 emacs_abort ();
3457 /* Remove pointer at IDX from id_to_widget.
3458 Called when scroll bar is destroyed. */
3460 static void
3461 xg_remove_widget_from_map (ptrdiff_t idx)
3463 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3465 id_to_widget.widgets[idx] = 0;
3466 --id_to_widget.used;
3470 /* Get the widget pointer at IDX from id_to_widget. */
3472 static GtkWidget *
3473 xg_get_widget_from_map (ptrdiff_t idx)
3475 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3476 return id_to_widget.widgets[idx];
3478 return 0;
3481 static void
3482 update_theme_scrollbar_width (void)
3484 #ifdef HAVE_GTK3
3485 GtkAdjustment *vadj;
3486 #else
3487 GtkObject *vadj;
3488 #endif
3489 GtkWidget *wscroll;
3490 int w = 0, b = 0;
3492 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3493 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3494 g_object_ref_sink (G_OBJECT (wscroll));
3495 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3496 gtk_widget_destroy (wscroll);
3497 g_object_unref (G_OBJECT (wscroll));
3498 w += 2*b;
3499 #ifndef HAVE_GTK3
3500 if (w < 16) w = 16;
3501 #endif
3502 scroll_bar_width_for_theme = w;
3505 static void
3506 update_theme_scrollbar_height (void)
3508 #ifdef HAVE_GTK3
3509 GtkAdjustment *hadj;
3510 #else
3511 GtkObject *hadj;
3512 #endif
3513 GtkWidget *wscroll;
3514 int w = 0, b = 0;
3516 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3517 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3518 g_object_ref_sink (G_OBJECT (wscroll));
3519 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3520 gtk_widget_destroy (wscroll);
3521 g_object_unref (G_OBJECT (wscroll));
3522 w += 2*b;
3523 if (w < 12) w = 12;
3524 scroll_bar_height_for_theme = w;
3528 xg_get_default_scrollbar_width (void)
3530 return scroll_bar_width_for_theme * xg_get_gdk_scale ();
3534 xg_get_default_scrollbar_height (void)
3536 /* Apparently there's no default height for themes. */
3537 return scroll_bar_width_for_theme * xg_get_gdk_scale ();
3540 /* Return the scrollbar id for X Window WID on display DPY.
3541 Return -1 if WID not in id_to_widget. */
3543 ptrdiff_t
3544 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3546 ptrdiff_t idx;
3547 GtkWidget *w;
3549 w = xg_win_to_widget (dpy, wid);
3551 if (w)
3553 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3554 if (id_to_widget.widgets[idx] == w)
3555 return idx;
3558 return -1;
3561 /* Callback invoked when scroll bar WIDGET is destroyed.
3562 DATA is the index into id_to_widget for WIDGET.
3563 We free pointer to last scroll bar values here and remove the index. */
3565 static void
3566 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3568 intptr_t id = (intptr_t) data;
3569 xg_remove_widget_from_map (id);
3572 /* Create a scroll bar widget for frame F. Store the scroll bar
3573 in BAR.
3574 SCROLL_CALLBACK is the callback to invoke when the value of the
3575 bar changes.
3576 END_CALLBACK is the callback to invoke when scrolling ends.
3577 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3578 to set resources for the widget. */
3580 void
3581 xg_create_scroll_bar (struct frame *f,
3582 struct scroll_bar *bar,
3583 GCallback scroll_callback,
3584 GCallback end_callback,
3585 const char *scroll_bar_name)
3587 GtkWidget *wscroll;
3588 GtkWidget *webox;
3589 intptr_t scroll_id;
3590 #ifdef HAVE_GTK3
3591 GtkAdjustment *vadj;
3592 #else
3593 GtkObject *vadj;
3594 #endif
3596 /* Page, step increment values are not so important here, they
3597 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3598 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3599 0.1, 0.1, 0.1);
3601 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3602 webox = gtk_event_box_new ();
3603 gtk_widget_set_name (wscroll, scroll_bar_name);
3604 #ifndef HAVE_GTK3
3605 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3606 #endif
3607 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3609 scroll_id = xg_store_widget_in_map (wscroll);
3611 g_signal_connect (G_OBJECT (wscroll),
3612 "destroy",
3613 G_CALLBACK (xg_gtk_scroll_destroy),
3614 (gpointer) scroll_id);
3615 g_signal_connect (G_OBJECT (wscroll),
3616 "change-value",
3617 scroll_callback,
3618 (gpointer) bar);
3619 g_signal_connect (G_OBJECT (wscroll),
3620 "button-release-event",
3621 end_callback,
3622 (gpointer) bar);
3624 /* The scroll bar widget does not draw on a window of its own. Instead
3625 it draws on the parent window, in this case the edit widget. So
3626 whenever the edit widget is cleared, the scroll bar needs to redraw
3627 also, which causes flicker. Put an event box between the edit widget
3628 and the scroll bar, so the scroll bar instead draws itself on the
3629 event box window. */
3630 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3631 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3634 /* Set the cursor to an arrow. */
3635 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3637 bar->x_window = scroll_id;
3638 bar->horizontal = 0;
3641 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3642 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3643 of the bar changes. END_CALLBACK is the callback to invoke when
3644 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3645 bar. Can be used to set resources for the widget. */
3647 void
3648 xg_create_horizontal_scroll_bar (struct frame *f,
3649 struct scroll_bar *bar,
3650 GCallback scroll_callback,
3651 GCallback end_callback,
3652 const char *scroll_bar_name)
3654 GtkWidget *wscroll;
3655 GtkWidget *webox;
3656 intptr_t scroll_id;
3657 #ifdef HAVE_GTK3
3658 GtkAdjustment *hadj;
3659 #else
3660 GtkObject *hadj;
3661 #endif
3663 /* Page, step increment values are not so important here, they
3664 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3665 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3666 0.1, 0.1, 0.1);
3668 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3669 webox = gtk_event_box_new ();
3670 gtk_widget_set_name (wscroll, scroll_bar_name);
3671 #ifndef HAVE_GTK3
3672 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3673 #endif
3674 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3676 scroll_id = xg_store_widget_in_map (wscroll);
3678 g_signal_connect (G_OBJECT (wscroll),
3679 "destroy",
3680 G_CALLBACK (xg_gtk_scroll_destroy),
3681 (gpointer) scroll_id);
3682 g_signal_connect (G_OBJECT (wscroll),
3683 "change-value",
3684 scroll_callback,
3685 (gpointer) bar);
3686 g_signal_connect (G_OBJECT (wscroll),
3687 "button-release-event",
3688 end_callback,
3689 (gpointer) bar);
3691 /* The scroll bar widget does not draw on a window of its own. Instead
3692 it draws on the parent window, in this case the edit widget. So
3693 whenever the edit widget is cleared, the scroll bar needs to redraw
3694 also, which causes flicker. Put an event box between the edit widget
3695 and the scroll bar, so the scroll bar instead draws itself on the
3696 event box window. */
3697 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3698 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3701 /* Set the cursor to an arrow. */
3702 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3704 bar->x_window = scroll_id;
3705 bar->horizontal = 1;
3708 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3710 void
3711 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3713 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3714 if (w)
3716 GtkWidget *wparent = gtk_widget_get_parent (w);
3717 gtk_widget_destroy (w);
3718 gtk_widget_destroy (wparent);
3719 SET_FRAME_GARBAGED (f);
3723 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3724 in frame F.
3725 TOP/LEFT are the new pixel positions where the bar shall appear.
3726 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3728 void
3729 xg_update_scrollbar_pos (struct frame *f,
3730 ptrdiff_t scrollbar_id,
3731 int top,
3732 int left,
3733 int width,
3734 int height)
3736 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3737 if (wscroll)
3739 GtkWidget *wfixed = f->output_data.x->edit_widget;
3740 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3741 gint msl;
3742 int scale = xg_get_gdk_scale ();
3744 top /= scale;
3745 left /= scale;
3746 height /= scale;
3747 left -= (scale - 1) * ((width / scale) >> 1);
3749 /* Clear out old position. */
3750 int oldx = -1, oldy = -1, oldw, oldh;
3751 if (gtk_widget_get_parent (wparent) == wfixed)
3753 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3754 "x", &oldx, "y", &oldy, NULL);
3755 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3758 /* Move and resize to new values. */
3759 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3760 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3761 if (msl > height)
3763 /* No room. Hide scroll bar as some themes output a warning if
3764 the height is less than the min size. */
3765 gtk_widget_hide (wparent);
3766 gtk_widget_hide (wscroll);
3768 else
3770 gtk_widget_show_all (wparent);
3771 gtk_widget_set_size_request (wscroll, width, height);
3773 #ifndef USE_CAIRO
3774 gtk_widget_queue_draw (wfixed);
3775 gdk_window_process_all_updates ();
3776 #endif
3777 if (oldx != -1 && oldw > 0 && oldh > 0)
3779 /* Clear under old scroll bar position. This must be done after
3780 the gtk_widget_queue_draw and gdk_window_process_all_updates
3781 above. */
3782 oldw += (scale - 1) * oldw;
3783 oldx -= (scale - 1) * oldw;
3784 x_clear_area (f, oldx, oldy, oldw, oldh);
3787 /* GTK does not redraw until the main loop is entered again, but
3788 if there are no X events pending we will not enter it. So we sync
3789 here to get some events. */
3791 x_sync (f);
3792 SET_FRAME_GARBAGED (f);
3793 cancel_mouse_face (f);
3798 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3799 in frame F.
3800 TOP/LEFT are the new pixel positions where the bar shall appear.
3801 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3803 void
3804 xg_update_horizontal_scrollbar_pos (struct frame *f,
3805 ptrdiff_t scrollbar_id,
3806 int top,
3807 int left,
3808 int width,
3809 int height)
3812 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3814 if (wscroll)
3816 GtkWidget *wfixed = f->output_data.x->edit_widget;
3817 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3818 gint msl;
3820 /* Clear out old position. */
3821 int oldx = -1, oldy = -1, oldw, oldh;
3822 if (gtk_widget_get_parent (wparent) == wfixed)
3824 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3825 "x", &oldx, "y", &oldy, NULL);
3826 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3829 /* Move and resize to new values. */
3830 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3831 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3832 if (msl > width)
3834 /* No room. Hide scroll bar as some themes output a warning if
3835 the width is less than the min size. */
3836 gtk_widget_hide (wparent);
3837 gtk_widget_hide (wscroll);
3839 else
3841 gtk_widget_show_all (wparent);
3842 gtk_widget_set_size_request (wscroll, width, height);
3844 gtk_widget_queue_draw (wfixed);
3845 gdk_window_process_all_updates ();
3846 if (oldx != -1 && oldw > 0 && oldh > 0)
3847 /* Clear under old scroll bar position. This must be done after
3848 the gtk_widget_queue_draw and gdk_window_process_all_updates
3849 above. */
3850 x_clear_area (f,
3851 oldx, oldy, oldw, oldh);
3853 /* GTK does not redraw until the main loop is entered again, but
3854 if there are no X events pending we will not enter it. So we sync
3855 here to get some events. */
3857 x_sync (f);
3858 SET_FRAME_GARBAGED (f);
3859 cancel_mouse_face (f);
3864 /* Get the current value of the range, truncated to an integer. */
3866 static int
3867 int_gtk_range_get_value (GtkRange *range)
3869 return gtk_range_get_value (range);
3873 /* Set the thumb size and position of scroll bar BAR. We are currently
3874 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3876 void
3877 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3878 int portion,
3879 int position,
3880 int whole)
3882 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3884 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3886 if (wscroll && bar->dragging == -1)
3888 GtkAdjustment *adj;
3889 gdouble shown;
3890 gdouble top;
3891 int size, value;
3892 int old_size;
3893 int new_step;
3894 bool changed = 0;
3896 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3898 if (scroll_bar_adjust_thumb_portion_p)
3900 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3901 line rather than the real portion value. This makes the
3902 thumb less likely to resize and that looks better. */
3903 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3905 /* When the thumb is at the bottom, position == whole.
3906 So we need to increase `whole' to make space for the thumb. */
3907 whole += portion;
3910 if (whole <= 0)
3911 top = 0, shown = 1;
3912 else
3914 top = (gdouble) position / whole;
3915 shown = (gdouble) portion / whole;
3918 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3919 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3921 /* Assume all lines are of equal size. */
3922 new_step = size / max (1, FRAME_LINES (f));
3924 old_size = gtk_adjustment_get_page_size (adj);
3925 if (old_size != size)
3927 int old_step = gtk_adjustment_get_step_increment (adj);
3928 if (old_step != new_step)
3930 gtk_adjustment_set_page_size (adj, size);
3931 gtk_adjustment_set_step_increment (adj, new_step);
3932 /* Assume a page increment is about 95% of the page size */
3933 gtk_adjustment_set_page_increment (adj, size - size / 20);
3934 changed = 1;
3938 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3940 block_input ();
3942 /* gtk_range_set_value invokes the callback. Set
3943 ignore_gtk_scrollbar to make the callback do nothing */
3944 xg_ignore_gtk_scrollbar = 1;
3946 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3947 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3948 else if (changed)
3949 gtk_adjustment_changed (adj);
3951 xg_ignore_gtk_scrollbar = 0;
3953 unblock_input ();
3958 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3959 currently displaying PORTION out of a whole WHOLE, and our position
3960 POSITION. */
3961 void
3962 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3963 int portion,
3964 int position,
3965 int whole)
3967 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3969 if (wscroll && bar->dragging == -1)
3971 GtkAdjustment *adj;
3972 int lower = 0;
3973 int upper = max (whole - 1, 0);
3974 int pagesize = min (upper, max (portion, 0));
3975 int value = max (0, min (position, upper - pagesize));
3976 /* These should be set to something more <portion, whole>
3977 related. */
3978 int page_increment = 4;
3979 int step_increment = 1;
3981 block_input ();
3982 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3983 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
3984 (gdouble) upper, (gdouble) step_increment,
3985 (gdouble) page_increment, (gdouble) pagesize);
3986 gtk_adjustment_changed (adj);
3987 unblock_input ();
3991 /* Return true if EVENT is for a scroll bar in frame F.
3992 When the same X window is used for several Gtk+ widgets, we cannot
3993 say for sure based on the X window alone if an event is for the
3994 frame. This function does additional checks. */
3996 bool
3997 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
3999 bool retval = 0;
4001 if (f && event->type == ButtonPress && event->xbutton.button < 4)
4003 /* Check if press occurred outside the edit widget. */
4004 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
4005 GdkWindow *gwin;
4006 #ifdef HAVE_GTK3
4007 GdkDevice *gdev = gdk_device_manager_get_client_pointer
4008 (gdk_display_get_device_manager (gdpy));
4009 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
4010 #else
4011 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
4012 #endif
4013 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
4015 else if (f
4016 && ((event->type == ButtonRelease && event->xbutton.button < 4)
4017 || event->type == MotionNotify))
4019 /* If we are releasing or moving the scroll bar, it has the grab. */
4020 GtkWidget *w = gtk_grab_get_current ();
4021 retval = w != 0 && GTK_IS_SCROLLBAR (w);
4024 return retval;
4028 /***********************************************************************
4029 Printing
4030 ***********************************************************************/
4031 #ifdef USE_CAIRO
4032 static GtkPrintSettings *print_settings = NULL;
4033 static GtkPageSetup *page_setup = NULL;
4035 void
4036 xg_page_setup_dialog (void)
4038 GtkPageSetup *new_page_setup = NULL;
4040 if (print_settings == NULL)
4041 print_settings = gtk_print_settings_new ();
4042 new_page_setup = gtk_print_run_page_setup_dialog (NULL, page_setup,
4043 print_settings);
4044 if (page_setup)
4045 g_object_unref (page_setup);
4046 page_setup = new_page_setup;
4049 Lisp_Object
4050 xg_get_page_setup (void)
4052 Lisp_Object orientation_symbol;
4054 if (page_setup == NULL)
4055 page_setup = gtk_page_setup_new ();
4057 switch (gtk_page_setup_get_orientation (page_setup))
4059 case GTK_PAGE_ORIENTATION_PORTRAIT:
4060 orientation_symbol = Qportrait;
4061 break;
4062 case GTK_PAGE_ORIENTATION_LANDSCAPE:
4063 orientation_symbol = Qlandscape;
4064 break;
4065 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
4066 orientation_symbol = Qreverse_portrait;
4067 break;
4068 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
4069 orientation_symbol = Qreverse_landscape;
4070 break;
4071 default:
4072 eassume (false);
4075 return listn (CONSTYPE_HEAP, 7,
4076 Fcons (Qorientation, orientation_symbol),
4077 #define MAKE_FLOAT_PAGE_SETUP(f) make_float (f (page_setup, GTK_UNIT_POINTS))
4078 Fcons (Qwidth,
4079 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_page_width)),
4080 Fcons (Qheight,
4081 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_page_height)),
4082 Fcons (Qleft_margin,
4083 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_left_margin)),
4084 Fcons (Qright_margin,
4085 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_right_margin)),
4086 Fcons (Qtop_margin,
4087 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_top_margin)),
4088 Fcons (Qbottom_margin,
4089 MAKE_FLOAT_PAGE_SETUP (gtk_page_setup_get_bottom_margin))
4090 #undef MAKE_FLOAT_PAGE_SETUP
4094 static void
4095 draw_page (GtkPrintOperation *operation, GtkPrintContext *context,
4096 gint page_nr, gpointer user_data)
4098 Lisp_Object frames = *((Lisp_Object *) user_data);
4099 struct frame *f = XFRAME (Fnth (make_number (page_nr), frames));
4100 cairo_t *cr = gtk_print_context_get_cairo_context (context);
4102 x_cr_draw_frame (cr, f);
4105 void
4106 xg_print_frames_dialog (Lisp_Object frames)
4108 GtkPrintOperation *print;
4109 GtkPrintOperationResult res;
4111 print = gtk_print_operation_new ();
4112 if (print_settings != NULL)
4113 gtk_print_operation_set_print_settings (print, print_settings);
4114 if (page_setup != NULL)
4115 gtk_print_operation_set_default_page_setup (print, page_setup);
4116 gtk_print_operation_set_n_pages (print, XINT (Flength (frames)));
4117 g_signal_connect (print, "draw-page", G_CALLBACK (draw_page), &frames);
4118 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
4119 NULL, NULL);
4120 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
4122 if (print_settings != NULL)
4123 g_object_unref (print_settings);
4124 print_settings =
4125 g_object_ref (gtk_print_operation_get_print_settings (print));
4127 g_object_unref (print);
4130 #endif /* USE_CAIRO */
4134 /***********************************************************************
4135 Tool bar functions
4136 ***********************************************************************/
4137 /* The key for the data we put in the GtkImage widgets. The data is
4138 the image used by Emacs. We use this to see if we need to update
4139 the GtkImage with a new image. */
4140 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
4142 /* The key for storing the latest modifiers so the activate callback can
4143 get them. */
4144 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4146 /* The key for the data we put in the GtkImage widgets. The data is
4147 the stock name used by Emacs. We use this to see if we need to update
4148 the GtkImage with a new image. */
4149 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4151 /* As above, but this is used for named theme widgets, as opposed to
4152 stock items. */
4153 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4155 /* Callback function invoked when a tool bar item is pressed.
4156 W is the button widget in the tool bar that got pressed,
4157 CLIENT_DATA is an integer that is the index of the button in the
4158 tool bar. 0 is the first button. */
4160 static gboolean
4161 xg_tool_bar_button_cb (GtkWidget *widget,
4162 GdkEventButton *event,
4163 gpointer user_data)
4165 intptr_t state = event->state;
4166 gpointer ptr = (gpointer) state;
4167 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4168 return FALSE;
4172 /* Callback function invoked when a tool bar item is pressed.
4173 W is the button widget in the tool bar that got pressed,
4174 CLIENT_DATA is an integer that is the index of the button in the
4175 tool bar. 0 is the first button. */
4177 static void
4178 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4180 intptr_t idx = (intptr_t) client_data;
4181 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4182 intptr_t mod = (intptr_t) gmod;
4184 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4185 Lisp_Object key, frame;
4186 struct input_event event;
4187 EVENT_INIT (event);
4189 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4190 return;
4192 idx *= TOOL_BAR_ITEM_NSLOTS;
4194 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4195 XSETFRAME (frame, f);
4197 /* We generate two events here. The first one is to set the prefix
4198 to `(tool_bar)', see keyboard.c. */
4199 event.kind = TOOL_BAR_EVENT;
4200 event.frame_or_window = frame;
4201 event.arg = frame;
4202 kbd_buffer_store_event (&event);
4204 event.kind = TOOL_BAR_EVENT;
4205 event.frame_or_window = frame;
4206 event.arg = key;
4207 /* Convert between the modifier bits GDK uses and the modifier bits
4208 Emacs uses. This assumes GDK and X masks are the same, which they are when
4209 this is written. */
4210 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4211 kbd_buffer_store_event (&event);
4213 /* Return focus to the frame after we have clicked on a detached
4214 tool bar button. */
4215 x_focus_frame (f);
4218 static GtkWidget *
4219 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4221 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4222 GtkWidget *c1 = clist->data;
4223 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4225 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4226 g_list_free (clist);
4227 return GTK_IS_LABEL (c1) ? c1 : c2;
4231 /* This callback is called when the mouse enters or leaves a tool bar item.
4232 It is used for displaying and hiding the help text.
4233 W is the tool bar item, a button.
4234 EVENT is either an enter event or leave event.
4235 CLIENT_DATA is an integer that is the index of the button in the
4236 tool bar. 0 is the first button.
4238 Returns FALSE to tell GTK to keep processing this event. */
4240 static gboolean
4241 xg_tool_bar_help_callback (GtkWidget *w,
4242 GdkEventCrossing *event,
4243 gpointer client_data)
4245 intptr_t idx = (intptr_t) client_data;
4246 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4247 Lisp_Object help, frame;
4249 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4250 return FALSE;
4252 if (event->type == GDK_ENTER_NOTIFY)
4254 idx *= TOOL_BAR_ITEM_NSLOTS;
4255 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4257 if (NILP (help))
4258 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4260 else
4261 help = Qnil;
4263 XSETFRAME (frame, f);
4264 kbd_buffer_store_help_event (frame, help);
4266 return FALSE;
4270 /* This callback is called when a tool bar item shall be redrawn.
4271 It modifies the expose event so that the GtkImage widget redraws the
4272 whole image. This to overcome a bug that makes GtkImage draw the image
4273 in the wrong place when it tries to redraw just a part of the image.
4274 W is the GtkImage to be redrawn.
4275 EVENT is the expose event for W.
4276 CLIENT_DATA is unused.
4278 Returns FALSE to tell GTK to keep processing this event. */
4280 #ifndef HAVE_GTK3
4281 static gboolean
4282 xg_tool_bar_item_expose_callback (GtkWidget *w,
4283 GdkEventExpose *event,
4284 gpointer client_data)
4286 gint width, height;
4288 gdk_drawable_get_size (event->window, &width, &height);
4289 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4290 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4292 event->area.x = max (0, event->area.x);
4293 event->area.y = max (0, event->area.y);
4295 event->area.width = max (width, event->area.width);
4296 event->area.height = max (height, event->area.height);
4298 return FALSE;
4300 #endif
4302 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4303 #define toolbar_set_orientation(w, o) \
4304 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4305 #else
4306 #define toolbar_set_orientation(w, o) \
4307 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4308 #endif
4310 /* Attach a tool bar to frame F. */
4312 static void
4313 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4315 struct x_output *x = f->output_data.x;
4316 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4317 GtkWidget *top_widget = x->toolbar_widget;
4319 toolbar_set_orientation (x->toolbar_widget,
4320 into_hbox
4321 ? GTK_ORIENTATION_VERTICAL
4322 : GTK_ORIENTATION_HORIZONTAL);
4324 if (into_hbox)
4326 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4327 FALSE, FALSE, 0);
4329 if (EQ (pos, Qleft))
4330 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4331 top_widget,
4333 x->toolbar_in_hbox = true;
4335 else
4337 bool vbox_pos = x->menubar_widget != 0;
4338 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4339 FALSE, FALSE, 0);
4341 if (EQ (pos, Qtop))
4342 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4343 top_widget,
4344 vbox_pos);
4345 x->toolbar_in_hbox = false;
4347 x->toolbar_is_packed = true;
4350 static bool xg_update_tool_bar_sizes (struct frame *f);
4352 static void
4353 tb_size_cb (GtkWidget *widget,
4354 GdkRectangle *allocation,
4355 gpointer user_data)
4357 /* When tool bar is created it has one preferred size. But when size is
4358 allocated between widgets, it may get another. So we must update
4359 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4360 struct frame *f = user_data;
4362 if (xg_update_tool_bar_sizes (f))
4364 frame_size_history_add (f, Qtb_size_cb, 0, 0, Qnil);
4365 adjust_frame_size (f, -1, -1, 5, 0, Qtool_bar_lines);
4369 /* Create a tool bar for frame F. */
4371 static void
4372 xg_create_tool_bar (struct frame *f)
4374 struct x_output *x = f->output_data.x;
4375 #if GTK_CHECK_VERSION (3, 3, 6)
4376 GtkStyleContext *gsty;
4377 #endif
4378 struct xg_frame_tb_info *tbinfo
4379 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4380 TB_INFO_KEY);
4381 if (! tbinfo)
4383 tbinfo = xmalloc (sizeof (*tbinfo));
4384 tbinfo->last_tool_bar = Qnil;
4385 tbinfo->style = Qnil;
4386 tbinfo->hmargin = tbinfo->vmargin = 0;
4387 tbinfo->dir = GTK_TEXT_DIR_NONE;
4388 tbinfo->n_last_items = 0;
4389 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4390 TB_INFO_KEY,
4391 tbinfo);
4394 x->toolbar_widget = gtk_toolbar_new ();
4396 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4398 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4399 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4400 g_signal_connect (x->toolbar_widget, "size-allocate",
4401 G_CALLBACK (tb_size_cb), f);
4402 #if GTK_CHECK_VERSION (3, 3, 6)
4403 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4404 gtk_style_context_add_class (gsty, "primary-toolbar");
4405 #endif
4409 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4411 /* Find the right-to-left image named by RTL in the tool bar images for F.
4412 Returns IMAGE if RTL is not found. */
4414 static Lisp_Object
4415 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4417 int i;
4418 Lisp_Object file, rtl_name;
4420 rtl_name = Ffile_name_nondirectory (rtl);
4422 for (i = 0; i < f->n_tool_bar_items; ++i)
4424 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4425 if (!NILP (file = file_for_image (rtl_image)))
4427 file = call1 (intern ("file-name-sans-extension"),
4428 Ffile_name_nondirectory (file));
4429 if (! NILP (Fequal (file, rtl_name)))
4431 image = rtl_image;
4432 break;
4437 return image;
4440 static GtkToolItem *
4441 xg_make_tool_item (struct frame *f,
4442 GtkWidget *wimage,
4443 GtkWidget **wbutton,
4444 const char *label,
4445 int i, bool horiz, bool text_image)
4447 GtkToolItem *ti = gtk_tool_item_new ();
4448 GtkWidget *vb = gtk_box_new (horiz
4449 ? GTK_ORIENTATION_HORIZONTAL
4450 : GTK_ORIENTATION_VERTICAL,
4452 GtkWidget *wb = gtk_button_new ();
4453 /* The eventbox is here so we can have tooltips on disabled items. */
4454 GtkWidget *weventbox = gtk_event_box_new ();
4455 #if GTK_CHECK_VERSION (3, 3, 6)
4456 GtkCssProvider *css_prov = gtk_css_provider_new ();
4457 GtkStyleContext *gsty;
4459 gtk_css_provider_load_from_data (css_prov,
4460 "GtkEventBox {"
4461 " background-color: transparent;"
4462 "}",
4463 -1, NULL);
4465 gsty = gtk_widget_get_style_context (weventbox);
4466 gtk_style_context_add_provider (gsty,
4467 GTK_STYLE_PROVIDER (css_prov),
4468 GTK_STYLE_PROVIDER_PRIORITY_USER);
4469 g_object_unref (css_prov);
4470 #endif
4472 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4474 if (wimage && !text_image)
4475 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4476 if (label)
4477 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4478 if (wimage && text_image)
4479 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4481 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4482 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4483 gtk_container_add (GTK_CONTAINER (wb), vb);
4484 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4485 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4487 if (wimage || label)
4489 intptr_t ii = i;
4490 gpointer gi = (gpointer) ii;
4492 g_signal_connect (G_OBJECT (wb), "clicked",
4493 G_CALLBACK (xg_tool_bar_callback),
4494 gi);
4496 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4498 #ifndef HAVE_GTK3
4499 /* Catch expose events to overcome an annoying redraw bug, see
4500 comment for xg_tool_bar_item_expose_callback. */
4501 g_signal_connect (G_OBJECT (ti),
4502 "expose-event",
4503 G_CALLBACK (xg_tool_bar_item_expose_callback),
4505 #endif
4506 gtk_tool_item_set_homogeneous (ti, FALSE);
4508 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4509 no distinction based on modifiers in the activate callback,
4510 so we have to do it ourselves. */
4511 g_signal_connect (wb, "button-release-event",
4512 G_CALLBACK (xg_tool_bar_button_cb),
4513 NULL);
4515 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4517 /* Use enter/leave notify to show help. We use the events
4518 rather than the GtkButton specific signals "enter" and
4519 "leave", so we can have only one callback. The event
4520 will tell us what kind of event it is. */
4521 g_signal_connect (G_OBJECT (weventbox),
4522 "enter-notify-event",
4523 G_CALLBACK (xg_tool_bar_help_callback),
4524 gi);
4525 g_signal_connect (G_OBJECT (weventbox),
4526 "leave-notify-event",
4527 G_CALLBACK (xg_tool_bar_help_callback),
4528 gi);
4531 if (wbutton) *wbutton = wb;
4533 return ti;
4536 static bool
4537 is_box_type (GtkWidget *vb, bool is_horizontal)
4539 #ifdef HAVE_GTK3
4540 bool ret = 0;
4541 if (GTK_IS_BOX (vb))
4543 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4544 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4545 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4547 return ret;
4548 #else
4549 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4550 #endif
4554 static bool
4555 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4556 const char *icon_name, const struct image *img,
4557 const char *label, bool horiz)
4559 gpointer old;
4560 GtkWidget *wimage;
4561 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4562 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4564 /* Check if the tool icon matches. */
4565 if (stock_name && wimage)
4567 old = g_object_get_data (G_OBJECT (wimage),
4568 XG_TOOL_BAR_STOCK_NAME);
4569 if (!old || strcmp (old, stock_name))
4570 return 1;
4572 else if (icon_name && wimage)
4574 old = g_object_get_data (G_OBJECT (wimage),
4575 XG_TOOL_BAR_ICON_NAME);
4576 if (!old || strcmp (old, icon_name))
4577 return 1;
4579 else if (wimage)
4581 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4582 XG_TOOL_BAR_IMAGE_DATA);
4583 Pixmap old_img = (Pixmap) gold_img;
4584 if (old_img != img->pixmap)
4585 return 1;
4588 /* Check button configuration and label. */
4589 if (is_box_type (vb, horiz)
4590 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4591 return 1;
4593 /* Ensure label is correct. */
4594 if (label && wlbl)
4595 gtk_label_set_text (GTK_LABEL (wlbl), label);
4596 return 0;
4599 static bool
4600 xg_update_tool_bar_sizes (struct frame *f)
4602 struct x_output *x = f->output_data.x;
4603 GtkRequisition req;
4604 int nl = 0, nr = 0, nt = 0, nb = 0;
4605 GtkWidget *top_widget = x->toolbar_widget;
4607 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4608 if (x->toolbar_in_hbox)
4610 int pos;
4611 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4612 top_widget,
4613 "position", &pos, NULL);
4614 if (pos == 0) nl = req.width;
4615 else nr = req.width;
4617 else
4619 int pos;
4620 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4621 top_widget,
4622 "position", &pos, NULL);
4623 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4624 else nb = req.height;
4627 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4628 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4629 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4630 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4632 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4633 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4634 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4635 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4636 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4637 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4639 return true;
4641 else
4642 return false;
4645 static char *
4646 find_icon_from_name (char *name,
4647 GtkIconTheme *icon_theme,
4648 char **icon_name)
4650 #if ! GTK_CHECK_VERSION (3, 10, 0)
4651 GtkStockItem stock_item;
4652 #endif
4654 if (name[0] == 'n' && name[1] == ':')
4656 *icon_name = name + 2;
4657 name = NULL;
4659 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4660 *icon_name = NULL;
4663 #if ! GTK_CHECK_VERSION (3, 10, 0)
4664 else if (gtk_stock_lookup (name, &stock_item))
4665 *icon_name = NULL;
4666 #endif
4667 else if (gtk_icon_theme_has_icon (icon_theme, name))
4669 *icon_name = name;
4670 name = NULL;
4672 else
4674 name = NULL;
4675 *icon_name = NULL;
4678 return name;
4682 /* Update the tool bar for frame F. Add new buttons and remove old. */
4684 void
4685 update_frame_tool_bar (struct frame *f)
4687 int i, j;
4688 struct x_output *x = f->output_data.x;
4689 int hmargin = 0, vmargin = 0;
4690 GtkToolbar *wtoolbar;
4691 GtkToolItem *ti;
4692 GtkTextDirection dir;
4693 Lisp_Object style;
4694 bool text_image, horiz;
4695 struct xg_frame_tb_info *tbinfo;
4696 GdkScreen *screen;
4697 GtkIconTheme *icon_theme;
4700 if (! FRAME_GTK_WIDGET (f))
4701 return;
4703 block_input ();
4705 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4707 hmargin = XFASTINT (Vtool_bar_button_margin);
4708 vmargin = XFASTINT (Vtool_bar_button_margin);
4710 else if (CONSP (Vtool_bar_button_margin))
4712 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4713 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4715 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4716 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4719 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4720 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4721 i.e. zero. This means that margins less than
4722 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4723 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4724 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4726 if (! x->toolbar_widget)
4727 xg_create_tool_bar (f);
4729 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4730 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4732 style = Ftool_bar_get_system_style ();
4733 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4734 icon_theme = gtk_icon_theme_get_for_screen (screen);
4736 /* Are we up to date? */
4737 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4738 TB_INFO_KEY);
4740 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4741 && tbinfo->n_last_items == f->n_tool_bar_items
4742 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4743 && tbinfo->dir == dir
4744 && ! NILP (Fequal (tbinfo->style, style))
4745 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4747 unblock_input ();
4748 return;
4751 tbinfo->last_tool_bar = f->tool_bar_items;
4752 tbinfo->n_last_items = f->n_tool_bar_items;
4753 tbinfo->style = style;
4754 tbinfo->hmargin = hmargin;
4755 tbinfo->vmargin = vmargin;
4756 tbinfo->dir = dir;
4758 text_image = EQ (style, Qtext_image_horiz);
4759 horiz = EQ (style, Qboth_horiz) || text_image;
4761 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4763 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4764 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4765 int idx;
4766 ptrdiff_t img_id;
4767 int icon_size = 0;
4768 struct image *img = NULL;
4769 Lisp_Object image;
4770 Lisp_Object stock = Qnil;
4771 char *stock_name = NULL;
4772 char *icon_name = NULL;
4773 Lisp_Object rtl;
4774 GtkWidget *wbutton = NULL;
4775 Lisp_Object specified_file;
4776 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4777 const char *label
4778 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4779 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4780 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4781 : "";
4783 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4785 /* If this is a separator, use a gtk separator item. */
4786 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4788 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4790 if (ti)
4791 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4792 GTK_WIDGET (ti));
4793 ti = gtk_separator_tool_item_new ();
4794 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4796 j++;
4797 continue;
4800 /* Otherwise, the tool-bar item is an ordinary button. */
4802 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4804 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4805 ti = NULL;
4808 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4810 /* Ignore invalid image specifications. */
4811 image = PROP (TOOL_BAR_ITEM_IMAGES);
4812 if (!valid_image_p (image))
4814 if (ti)
4815 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4816 GTK_WIDGET (ti));
4817 continue;
4820 specified_file = file_for_image (image);
4821 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4822 stock = call1 (Qx_gtk_map_stock, specified_file);
4824 if (CONSP (stock))
4826 Lisp_Object tem;
4827 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4828 if (! NILP (tem) && STRINGP (XCAR (tem)))
4830 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4831 icon_theme,
4832 &icon_name);
4833 if (stock_name || icon_name) break;
4836 else if (STRINGP (stock))
4838 stock_name = find_icon_from_name (SSDATA (stock),
4839 icon_theme,
4840 &icon_name);
4843 if (stock_name || icon_name)
4844 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4846 if (stock_name == NULL && icon_name == NULL)
4848 /* No stock image, or stock item not known. Try regular
4849 image. If image is a vector, choose it according to the
4850 button state. */
4851 if (dir == GTK_TEXT_DIR_RTL
4852 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4853 && STRINGP (rtl))
4854 image = find_rtl_image (f, image, rtl);
4856 if (VECTORP (image))
4858 if (enabled_p)
4859 idx = (selected_p
4860 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4861 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4862 else
4863 idx = (selected_p
4864 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4865 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4867 eassert (ASIZE (image) >= idx);
4868 image = AREF (image, idx);
4870 else
4871 idx = -1;
4873 img_id = lookup_image (f, image);
4874 img = IMAGE_FROM_ID (f, img_id);
4875 prepare_image_for_display (f, img);
4877 if (img->load_failed_p || img->pixmap == None)
4879 if (ti)
4880 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4881 GTK_WIDGET (ti));
4882 continue;
4886 /* If there is an existing widget, check if it's stale; if so,
4887 remove it and make a new tool item from scratch. */
4888 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4889 img, label, horiz))
4891 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4892 GTK_WIDGET (ti));
4893 ti = NULL;
4896 if (ti == NULL)
4898 GtkWidget *w;
4900 /* Save the image so we can see if an update is needed the
4901 next time we call xg_tool_item_match_p. */
4902 if (EQ (style, Qtext))
4903 w = NULL;
4904 else if (stock_name)
4907 #if GTK_CHECK_VERSION (3, 10, 0)
4908 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4909 #else
4910 w = gtk_image_new_from_stock (stock_name, icon_size);
4911 #endif
4912 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4913 (gpointer) xstrdup (stock_name),
4914 (GDestroyNotify) xfree);
4916 else if (icon_name)
4918 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4919 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4920 (gpointer) xstrdup (icon_name),
4921 (GDestroyNotify) xfree);
4923 else
4925 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4926 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4927 (gpointer)img->pixmap);
4930 #if GTK_CHECK_VERSION (3, 14, 0)
4931 if (w)
4933 gtk_widget_set_margin_start (w, hmargin);
4934 gtk_widget_set_margin_end (w, hmargin);
4935 gtk_widget_set_margin_top (w, vmargin);
4936 gtk_widget_set_margin_bottom (w, vmargin);
4938 #else
4939 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4940 #endif
4941 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4942 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4945 #undef PROP
4947 gtk_widget_set_sensitive (wbutton, enabled_p);
4948 j++;
4951 /* Remove buttons not longer needed. */
4954 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4955 if (ti)
4956 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4957 } while (ti != NULL);
4959 if (f->n_tool_bar_items != 0)
4961 if (! x->toolbar_is_packed)
4962 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4963 gtk_widget_show_all (x->toolbar_widget);
4964 if (xg_update_tool_bar_sizes (f))
4966 int inhibit
4967 = ((f->after_make_frame
4968 && !f->tool_bar_resized
4969 && (EQ (frame_inhibit_implied_resize, Qt)
4970 || (CONSP (frame_inhibit_implied_resize)
4971 && !NILP (Fmemq (Qtool_bar_lines,
4972 frame_inhibit_implied_resize))))
4973 /* This will probably fail to DTRT in the
4974 fullheight/-width cases. */
4975 && NILP (get_frame_param (f, Qfullscreen)))
4977 : 2);
4979 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
4980 adjust_frame_size (f, -1, -1, inhibit, 0, Qtool_bar_lines);
4982 f->tool_bar_resized = f->tool_bar_redisplayed;
4985 unblock_input ();
4988 /* Deallocate all resources for the tool bar on frame F.
4989 Remove the tool bar. */
4991 void
4992 free_frame_tool_bar (struct frame *f)
4994 struct x_output *x = f->output_data.x;
4996 if (x->toolbar_widget)
4998 struct xg_frame_tb_info *tbinfo;
4999 GtkWidget *top_widget = x->toolbar_widget;
5001 block_input ();
5002 if (x->toolbar_is_packed)
5004 if (x->toolbar_in_hbox)
5005 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
5006 top_widget);
5007 else
5008 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
5009 top_widget);
5011 else
5012 gtk_widget_destroy (x->toolbar_widget);
5014 x->toolbar_widget = 0;
5015 x->toolbar_widget = 0;
5016 x->toolbar_is_packed = false;
5017 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
5018 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
5020 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
5021 TB_INFO_KEY);
5022 if (tbinfo)
5024 xfree (tbinfo);
5025 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
5026 TB_INFO_KEY,
5027 NULL);
5030 frame_size_history_add (f, Qfree_frame_tool_bar, 0, 0, Qnil);
5031 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
5033 unblock_input ();
5037 void
5038 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
5040 struct x_output *x = f->output_data.x;
5041 GtkWidget *top_widget = x->toolbar_widget;
5043 if (! x->toolbar_widget || ! top_widget)
5044 return;
5046 block_input ();
5047 g_object_ref (top_widget);
5048 if (x->toolbar_is_packed)
5050 if (x->toolbar_in_hbox)
5051 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
5052 top_widget);
5053 else
5054 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
5055 top_widget);
5058 xg_pack_tool_bar (f, pos);
5059 g_object_unref (top_widget);
5061 if (xg_update_tool_bar_sizes (f))
5063 frame_size_history_add (f, Qxg_change_toolbar_position, 0, 0, Qnil);
5064 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
5068 unblock_input ();
5073 /***********************************************************************
5074 Initializing
5075 ***********************************************************************/
5076 void
5077 xg_initialize (void)
5079 GtkBindingSet *binding_set;
5080 GtkSettings *settings;
5082 #if HAVE_XFT
5083 /* Work around a bug with corrupted data if libXft gets unloaded. This way
5084 we keep it permanently linked in. */
5085 XftInit (0);
5086 #endif
5088 gdpy_def = NULL;
5089 xg_ignore_gtk_scrollbar = 0;
5090 xg_menu_cb_list.prev = xg_menu_cb_list.next =
5091 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
5093 id_to_widget.max_size = id_to_widget.used = 0;
5094 id_to_widget.widgets = 0;
5096 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
5097 (gdk_display_get_default ()));
5098 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
5099 bindings. It doesn't seem to be any way to remove properties,
5100 so we set it to "" which in means "no key". */
5101 gtk_settings_set_string_property (settings,
5102 "gtk-menu-bar-accel",
5104 EMACS_CLASS);
5106 /* Make GTK text input widgets use Emacs style keybindings. This is
5107 Emacs after all. */
5108 gtk_settings_set_string_property (settings,
5109 "gtk-key-theme-name",
5110 "Emacs",
5111 EMACS_CLASS);
5113 /* Make dialogs close on C-g. Since file dialog inherits from
5114 dialog, this works for them also. */
5115 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
5116 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5117 "close", 0);
5119 /* Make menus close on C-g. */
5120 binding_set = gtk_binding_set_by_class (g_type_class_ref
5121 (GTK_TYPE_MENU_SHELL));
5122 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5123 "cancel", 0);
5124 update_theme_scrollbar_width ();
5125 update_theme_scrollbar_height ();
5127 #ifdef HAVE_FREETYPE
5128 x_last_font_name = NULL;
5129 #endif
5132 #endif /* USE_GTK */