Check for invalid GTK+ monitor scales
[emacs.git] / src / gtkutil.c
blob61bc5ee245a5798581dc9ae962582f37076722cb
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2015 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
10 (at 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 "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "buffer.h"
34 #include "gtkutil.h"
35 #include "termhooks.h"
36 #include "keyboard.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include "font.h"
41 #include <gdk/gdkkeysyms.h>
42 #include "xsettings.h"
44 #ifdef HAVE_XFT
45 #include <X11/Xft/Xft.h>
46 #endif
48 #ifdef HAVE_GTK3
49 #include <gtk/gtkx.h>
50 #include "emacsgtkfixed.h"
51 #endif
53 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
54 #define gtk_widget_set_has_window(w, b) \
55 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
56 #endif
57 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
58 #define gtk_dialog_get_action_area(w) ((w)->action_area)
59 #define gtk_dialog_get_content_area(w) ((w)->vbox)
60 #endif
61 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
62 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
63 #endif
64 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
65 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
66 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
67 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
68 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
69 #endif
70 #if GTK_CHECK_VERSION (2, 12, 0)
71 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
72 #else
73 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
74 #endif
76 #if ! GTK_CHECK_VERSION (2, 14, 0)
77 #define gtk_adjustment_configure(adj, xvalue, xlower, \
78 xupper, xstep_increment, \
79 xpage_increment, xpagesize) \
80 do { \
81 adj->lower = xlower; \
82 adj->upper = xupper; \
83 adj->page_size = xpagesize; \
84 gtk_adjustment_set_value (adj, xvalue); \
85 adj->page_increment = xpage_increment; \
86 adj->step_increment = xstep_increment; \
87 } while (0)
88 #endif /* < Gtk+ 2.14 */
90 #ifdef HAVE_FREETYPE
91 #if GTK_CHECK_VERSION (3, 2, 0)
92 #define USE_NEW_GTK_FONT_CHOOSER 1
93 #else
94 #define USE_NEW_GTK_FONT_CHOOSER 0
95 #define gtk_font_chooser_dialog_new(x, y) \
96 gtk_font_selection_dialog_new (x)
97 #undef GTK_FONT_CHOOSER
98 #define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
99 #define gtk_font_chooser_set_font(x, y) \
100 gtk_font_selection_dialog_set_font_name (x, y)
101 #endif
102 #endif /* HAVE_FREETYPE */
104 #if GTK_CHECK_VERSION (3, 10, 0)
105 #define XG_TEXT_CANCEL "Cancel"
106 #define XG_TEXT_OK "OK"
107 #define XG_TEXT_OPEN "Open"
108 #else
109 #define XG_TEXT_CANCEL GTK_STOCK_CANCEL
110 #define XG_TEXT_OK GTK_STOCK_OK
111 #define XG_TEXT_OPEN GTK_STOCK_OPEN
112 #endif
114 #ifndef HAVE_GTK3
115 #ifdef USE_GTK_TOOLTIP
116 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
117 #endif
118 #define gdk_window_get_geometry(w, a, b, c, d) \
119 gdk_window_get_geometry (w, a, b, c, d, 0)
120 #define gdk_x11_window_lookup_for_display(d, w) \
121 gdk_xid_table_lookup_for_display (d, w)
122 #define gtk_box_new(ori, spacing) \
123 ((ori) == GTK_ORIENTATION_HORIZONTAL \
124 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
125 #define gtk_scrollbar_new(ori, spacing) \
126 ((ori) == GTK_ORIENTATION_HORIZONTAL \
127 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
128 #ifndef GDK_KEY_g
129 #define GDK_KEY_g GDK_g
130 #endif
131 #endif /* HAVE_GTK3 */
133 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
135 static void update_theme_scrollbar_width (void);
136 static void update_theme_scrollbar_height (void);
138 #define TB_INFO_KEY "xg_frame_tb_info"
139 struct xg_frame_tb_info
141 Lisp_Object last_tool_bar;
142 Lisp_Object style;
143 int n_last_items;
144 int hmargin, vmargin;
145 GtkTextDirection dir;
149 /***********************************************************************
150 Display handling functions
151 ***********************************************************************/
153 /* Keep track of the default display, or NULL if there is none. Emacs
154 may close all its displays. */
156 static GdkDisplay *gdpy_def;
158 /* When the GTK widget W is to be created on a display for F that
159 is not the default display, set the display for W.
160 W can be a GtkMenu or a GtkWindow widget. */
162 static void
163 xg_set_screen (GtkWidget *w, struct frame *f)
165 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
167 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
168 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
170 if (GTK_IS_MENU (w))
171 gtk_menu_set_screen (GTK_MENU (w), gscreen);
172 else
173 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
178 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
179 *DPY is set to NULL if the display can't be opened.
181 Returns non-zero if display could be opened, zero if display could not
182 be opened, and less than zero if the GTK version doesn't support
183 multiple displays. */
185 void
186 xg_display_open (char *display_name, Display **dpy)
188 GdkDisplay *gdpy;
190 unrequest_sigio (); // See comment in x_display_ok, xterm.c.
191 gdpy = gdk_display_open (display_name);
192 request_sigio ();
193 if (!gdpy_def && gdpy)
195 gdpy_def = gdpy;
196 gdk_display_manager_set_default_display (gdk_display_manager_get (),
197 gdpy);
200 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
204 /* Close display DPY. */
206 void
207 xg_display_close (Display *dpy)
209 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
211 /* If this is the default display, try to change it before closing.
212 If there is no other display to use, gdpy_def is set to NULL, and
213 the next call to xg_display_open resets the default display. */
214 if (gdk_display_get_default () == gdpy)
216 struct x_display_info *dpyinfo;
217 GdkDisplay *gdpy_new = NULL;
219 /* Find another display. */
220 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
221 if (dpyinfo->display != dpy)
223 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
224 gdk_display_manager_set_default_display (gdk_display_manager_get (),
225 gdpy_new);
226 break;
228 gdpy_def = gdpy_new;
231 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
232 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
233 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
234 can continue running, but there will be memory leaks. */
235 g_object_run_dispose (G_OBJECT (gdpy));
236 #else
237 /* This seems to be fixed in GTK 2.10. */
238 gdk_display_close (gdpy);
239 #endif
243 /***********************************************************************
244 Utility functions
245 ***********************************************************************/
247 /* Create and return the cursor to be used for popup menus and
248 scroll bars on display DPY. */
250 GdkCursor *
251 xg_create_default_cursor (Display *dpy)
253 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
254 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
257 static GdkPixbuf *
258 xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
260 int iunused;
261 GdkPixbuf *tmp_buf;
262 Window wunused;
263 unsigned int width, height, uunused;
264 XImage *xim;
266 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
267 &width, &height, &uunused, &uunused);
269 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
270 ~0, XYPixmap);
271 if (!xim) return 0;
273 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
274 GDK_COLORSPACE_RGB,
275 FALSE,
276 xim->bitmap_unit,
277 width,
278 height,
279 xim->bytes_per_line,
280 NULL,
281 NULL);
282 XDestroyImage (xim);
283 return tmp_buf;
286 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
288 static GdkPixbuf *
289 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
290 Pixmap pix,
291 Pixmap mask)
293 int width, height;
294 GdkPixbuf *icon_buf, *tmp_buf;
296 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
297 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
298 g_object_unref (G_OBJECT (tmp_buf));
300 width = gdk_pixbuf_get_width (icon_buf);
301 height = gdk_pixbuf_get_height (icon_buf);
303 if (mask)
305 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
306 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
307 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
308 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
309 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
310 int y;
312 for (y = 0; y < height; ++y)
314 guchar *iconptr, *maskptr;
315 int x;
317 iconptr = pixels + y * rowstride;
318 maskptr = mask_pixels + y * mask_rowstride;
320 for (x = 0; x < width; ++x)
322 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
323 just R is sufficient. */
324 if (maskptr[0] == 0)
325 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
327 iconptr += rowstride/width;
328 maskptr += mask_rowstride/width;
332 g_object_unref (G_OBJECT (mask_buf));
335 return icon_buf;
338 static Lisp_Object
339 file_for_image (Lisp_Object image)
341 Lisp_Object specified_file = Qnil;
342 Lisp_Object tail;
344 for (tail = XCDR (image);
345 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
346 tail = XCDR (XCDR (tail)))
347 if (EQ (XCAR (tail), QCfile))
348 specified_file = XCAR (XCDR (tail));
350 return specified_file;
353 /* For the image defined in IMG, make and return a GtkImage. For displays with
354 8 planes or less we must make a GdkPixbuf and apply the mask manually.
355 Otherwise the highlighting and dimming the tool bar code in GTK does
356 will look bad. For display with more than 8 planes we just use the
357 pixmap and mask directly. For monochrome displays, GTK doesn't seem
358 able to use external pixmaps, it looks bad whatever we do.
359 The image is defined on the display where frame F is.
360 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
361 If OLD_WIDGET is NULL, a new widget is constructed and returned.
362 If OLD_WIDGET is not NULL, that widget is modified. */
364 static GtkWidget *
365 xg_get_image_for_pixmap (struct frame *f,
366 struct image *img,
367 GtkWidget *widget,
368 GtkImage *old_widget)
370 GdkPixbuf *icon_buf;
372 /* If we have a file, let GTK do all the image handling.
373 This seems to be the only way to make insensitive and activated icons
374 look good in all cases. */
375 Lisp_Object specified_file = file_for_image (img->spec);
376 Lisp_Object file;
378 /* We already loaded the image once before calling this
379 function, so this only fails if the image file has been removed.
380 In that case, use the pixmap already loaded. */
382 if (STRINGP (specified_file)
383 && STRINGP (file = x_find_image_file (specified_file)))
385 if (! old_widget)
386 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
387 else
388 gtk_image_set_from_file (old_widget, SSDATA (file));
390 return GTK_WIDGET (old_widget);
393 /* No file, do the image handling ourselves. This will look very bad
394 on a monochrome display, and sometimes bad on all displays with
395 certain themes. */
397 /* This is a workaround to make icons look good on pseudo color
398 displays. Apparently GTK expects the images to have an alpha
399 channel. If they don't, insensitive and activated icons will
400 look bad. This workaround does not work on monochrome displays,
401 and is strictly not needed on true color/static color displays (i.e.
402 16 bits and higher). But we do it anyway so we get a pixbuf that is
403 not associated with the img->pixmap. The img->pixmap may be removed
404 by clearing the image cache and then the tool bar redraw fails, since
405 Gtk+ assumes the pixmap is always there. */
406 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
408 if (icon_buf)
410 if (! old_widget)
411 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
412 else
413 gtk_image_set_from_pixbuf (old_widget, icon_buf);
415 g_object_unref (G_OBJECT (icon_buf));
418 return GTK_WIDGET (old_widget);
422 /* Set CURSOR on W and all widgets W contain. We must do like this
423 for scroll bars and menu because they create widgets internally,
424 and it is those widgets that are visible. */
426 static void
427 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
429 GdkWindow *window = gtk_widget_get_window (w);
430 GList *children = gdk_window_peek_children (window);
432 gdk_window_set_cursor (window, cursor);
434 /* The scroll bar widget has more than one GDK window (had to look at
435 the source to figure this out), and there is no way to set cursor
436 on widgets in GTK. So we must set the cursor for all GDK windows.
437 Ditto for menus. */
439 for ( ; children; children = g_list_next (children))
440 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
443 /* Insert NODE into linked LIST. */
445 static void
446 xg_list_insert (xg_list_node *list, xg_list_node *node)
448 xg_list_node *list_start = list->next;
450 if (list_start) list_start->prev = node;
451 node->next = list_start;
452 node->prev = 0;
453 list->next = node;
456 /* Remove NODE from linked LIST. */
458 static void
459 xg_list_remove (xg_list_node *list, xg_list_node *node)
461 xg_list_node *list_start = list->next;
462 if (node == list_start)
464 list->next = node->next;
465 if (list->next) list->next->prev = 0;
467 else
469 node->prev->next = node->next;
470 if (node->next) node->next->prev = node->prev;
474 /* Allocate and return a utf8 version of STR. If STR is already
475 utf8 or NULL, just return a copy of STR.
476 A new string is allocated and the caller must free the result
477 with g_free. */
479 static char *
480 get_utf8_string (const char *str)
482 char *utf8_str;
484 if (!str) return NULL;
486 /* If not UTF-8, try current locale. */
487 if (!g_utf8_validate (str, -1, NULL))
488 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
489 else
490 return g_strdup (str);
492 if (!utf8_str)
494 /* Probably some control characters in str. Escape them. */
495 ptrdiff_t len;
496 ptrdiff_t nr_bad = 0;
497 gsize bytes_read;
498 gsize bytes_written;
499 unsigned char *p = (unsigned char *)str;
500 char *cp, *up;
501 GError *err = NULL;
503 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
504 &bytes_written, &err))
505 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
507 ++nr_bad;
508 p += bytes_written+1;
509 g_error_free (err);
510 err = NULL;
513 if (err)
515 g_error_free (err);
516 err = NULL;
518 if (cp) g_free (cp);
520 len = strlen (str);
521 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
522 memory_full (SIZE_MAX);
523 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
524 p = (unsigned char *)str;
526 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
527 &bytes_written, &err))
528 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
530 memcpy (up, p, bytes_written);
531 up += bytes_written;
532 up += sprintf (up, "\\%03o", p[bytes_written]);
533 p += bytes_written + 1;
534 g_error_free (err);
535 err = NULL;
538 if (cp)
540 strcpy (up, cp);
541 g_free (cp);
543 if (err)
545 g_error_free (err);
546 err = NULL;
549 return utf8_str;
552 /* Check for special colors used in face spec for region face.
553 The colors are fetched from the Gtk+ theme.
554 Return true if color was found, false if not. */
556 bool
557 xg_check_special_colors (struct frame *f,
558 const char *color_name,
559 XColor *color)
561 bool success_p = 0;
562 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
563 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
565 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
566 return success_p;
568 block_input ();
570 #ifdef HAVE_GTK3
571 GtkStyleContext *gsty
572 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
573 GdkRGBA col;
574 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
575 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
576 if (get_fg)
577 gtk_style_context_get_color (gsty, state, &col);
578 else
579 gtk_style_context_get_background_color (gsty, state, &col);
581 sprintf (buf, "rgb:%04x/%04x/%04x",
582 (unsigned) (col.red * 65535),
583 (unsigned) (col.green * 65535),
584 (unsigned) (col.blue * 65535));
585 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
586 buf, color)
587 != 0);
588 #else
589 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
590 GdkColor *grgb = get_bg
591 ? &gsty->bg[GTK_STATE_SELECTED]
592 : &gsty->fg[GTK_STATE_SELECTED];
594 color->red = grgb->red;
595 color->green = grgb->green;
596 color->blue = grgb->blue;
597 color->pixel = grgb->pixel;
598 success_p = 1;
599 #endif
602 unblock_input ();
603 return success_p;
608 /***********************************************************************
609 Tooltips
610 ***********************************************************************/
611 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
612 We use that to pop down the tooltip. This happens if Gtk+ for some
613 reason wants to change or hide the tooltip. */
615 #ifdef USE_GTK_TOOLTIP
617 static void
618 hierarchy_ch_cb (GtkWidget *widget,
619 GtkWidget *previous_toplevel,
620 gpointer user_data)
622 struct frame *f = user_data;
623 struct x_output *x = f->output_data.x;
624 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
626 if (! top || ! GTK_IS_WINDOW (top))
627 gtk_widget_hide (previous_toplevel);
630 /* Callback called when Gtk+ thinks a tooltip should be displayed.
631 We use it to get the tooltip window and the tooltip widget so
632 we can manipulate the ourselves.
634 Return FALSE ensures that the tooltip is not shown. */
636 static gboolean
637 qttip_cb (GtkWidget *widget,
638 gint xpos,
639 gint ypos,
640 gboolean keyboard_mode,
641 GtkTooltip *tooltip,
642 gpointer user_data)
644 struct frame *f = user_data;
645 struct x_output *x = f->output_data.x;
646 if (x->ttip_widget == NULL)
648 GtkWidget *p;
649 GList *list, *iter;
651 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
652 x->ttip_widget = tooltip;
653 g_object_ref (G_OBJECT (tooltip));
654 x->ttip_lbl = gtk_label_new ("");
655 g_object_ref (G_OBJECT (x->ttip_lbl));
656 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
657 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
659 /* Change stupid Gtk+ default line wrapping. */
660 p = gtk_widget_get_parent (x->ttip_lbl);
661 list = gtk_container_get_children (GTK_CONTAINER (p));
662 for (iter = list; iter; iter = g_list_next (iter))
664 GtkWidget *w = GTK_WIDGET (iter->data);
665 if (GTK_IS_LABEL (w))
666 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
668 g_list_free (list);
670 /* ATK needs an empty title for some reason. */
671 gtk_window_set_title (x->ttip_window, "");
672 /* Realize so we can safely get screen later on. */
673 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
674 gtk_widget_realize (x->ttip_lbl);
676 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
677 G_CALLBACK (hierarchy_ch_cb), f);
679 return FALSE;
682 #endif /* USE_GTK_TOOLTIP */
684 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
685 Return true if a system tooltip is available. */
687 bool
688 xg_prepare_tooltip (struct frame *f,
689 Lisp_Object string,
690 int *width,
691 int *height)
693 #ifndef USE_GTK_TOOLTIP
694 return 0;
695 #else
696 struct x_output *x = f->output_data.x;
697 GtkWidget *widget;
698 GdkWindow *gwin;
699 GdkScreen *screen;
700 GtkSettings *settings;
701 gboolean tt_enabled = TRUE;
702 GtkRequisition req;
703 Lisp_Object encoded_string;
705 if (!x->ttip_lbl) return 0;
707 block_input ();
708 encoded_string = ENCODE_UTF_8 (string);
709 widget = GTK_WIDGET (x->ttip_lbl);
710 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
711 screen = gdk_window_get_screen (gwin);
712 settings = gtk_settings_get_for_screen (screen);
713 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
714 if (tt_enabled)
716 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
717 /* Record that we disabled it so it can be enabled again. */
718 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
719 (gpointer)f);
722 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
723 g_object_set_data (G_OBJECT
724 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
725 "gdk-display-current-tooltip", NULL);
727 /* Put our dummy widget in so we can get callbacks for unrealize and
728 hierarchy-changed. */
729 gtk_tooltip_set_custom (x->ttip_widget, widget);
730 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
731 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
732 if (width) *width = req.width;
733 if (height) *height = req.height;
735 unblock_input ();
737 return 1;
738 #endif /* USE_GTK_TOOLTIP */
741 /* Show the tooltip at ROOT_X and ROOT_Y.
742 xg_prepare_tooltip must have been called before this function. */
744 void
745 xg_show_tooltip (struct frame *f, int root_x, int root_y)
747 #ifdef USE_GTK_TOOLTIP
748 struct x_output *x = f->output_data.x;
749 if (x->ttip_window)
751 block_input ();
752 gtk_window_move (x->ttip_window, root_x, root_y);
753 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
754 unblock_input ();
756 #endif
759 /* Hide tooltip if shown. Do nothing if not shown.
760 Return true if tip was hidden, false if not (i.e. not using
761 system tooltips). */
763 bool
764 xg_hide_tooltip (struct frame *f)
766 bool ret = 0;
767 #ifdef USE_GTK_TOOLTIP
768 if (f->output_data.x->ttip_window)
770 GtkWindow *win = f->output_data.x->ttip_window;
771 block_input ();
772 gtk_widget_hide (GTK_WIDGET (win));
774 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
776 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
777 GdkScreen *screen = gdk_window_get_screen (gwin);
778 GtkSettings *settings = gtk_settings_get_for_screen (screen);
779 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
781 unblock_input ();
783 ret = 1;
785 #endif
786 return ret;
790 /***********************************************************************
791 General functions for creating widgets, resizing, events, e.t.c.
792 ***********************************************************************/
794 static void
795 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
796 const gchar *msg, gpointer user_data)
798 if (!strstr (msg, "visible children"))
799 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
802 /* Make a geometry string and pass that to GTK. It seems this is the
803 only way to get geometry position right if the user explicitly
804 asked for a position when starting Emacs.
805 F is the frame we shall set geometry for. */
807 static void
808 xg_set_geometry (struct frame *f)
810 if (f->size_hint_flags & (USPosition | PPosition))
812 int left = f->left_pos;
813 int xneg = f->size_hint_flags & XNegative;
814 int top = f->top_pos;
815 int yneg = f->size_hint_flags & YNegative;
816 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
817 guint id;
819 if (xneg)
820 left = -left;
821 if (yneg)
822 top = -top;
824 sprintf (geom_str, "=%dx%d%c%d%c%d",
825 FRAME_PIXEL_WIDTH (f),
826 FRAME_PIXEL_HEIGHT (f),
827 (xneg ? '-' : '+'), left,
828 (yneg ? '-' : '+'), top);
830 /* Silence warning about visible children. */
831 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
832 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
834 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
835 geom_str))
836 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
838 g_log_remove_handler ("Gtk", id);
842 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
843 and use a GtkFixed widget, this doesn't happen automatically. */
845 void
846 xg_clear_under_internal_border (struct frame *f)
848 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
850 GtkWidget *wfixed = f->output_data.x->edit_widget;
852 gtk_widget_queue_draw (wfixed);
853 gdk_window_process_all_updates ();
855 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
856 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
858 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
859 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
861 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
862 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
863 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
865 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
866 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
867 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
871 static int
872 xg_get_gdk_scale (void)
874 const char *sscale = getenv ("GDK_SCALE");
876 if (sscale)
878 long scale = atol (sscale);
879 if (0 < scale)
880 return min (scale, INT_MAX);
883 return 1;
886 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
887 and a Gtk+ menu bar, we get resize events for the edit part of the
888 frame only. We let Gtk+ deal with the Gtk+ parts.
889 F is the frame to resize.
890 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
892 void
893 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
895 int width, height;
897 if (pixelwidth == -1 && pixelheight == -1)
899 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
900 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
901 0, 0, &pixelwidth, &pixelheight);
902 else
903 return;
906 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
907 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
909 frame_size_history_add
910 (f, Qxg_frame_resized, width, height, Qnil);
912 if (width != FRAME_TEXT_WIDTH (f)
913 || height != FRAME_TEXT_HEIGHT (f)
914 || pixelwidth != FRAME_PIXEL_WIDTH (f)
915 || pixelheight != FRAME_PIXEL_HEIGHT (f))
917 xg_clear_under_internal_border (f);
918 change_frame_size (f, width, height, 0, 1, 0, 1);
919 SET_FRAME_GARBAGED (f);
920 cancel_mouse_face (f);
922 do_pending_window_change (0);
926 /* Resize the outer window of frame F after changing the height.
927 COLUMNS/ROWS is the size the edit area shall have after the resize. */
929 void
930 xg_frame_set_char_size (struct frame *f, int width, int height)
932 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
933 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
934 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
935 gint gwidth, gheight;
936 int totalheight
937 = pixelheight + FRAME_TOOLBAR_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f);
938 int totalwidth = pixelwidth + FRAME_TOOLBAR_WIDTH (f);
940 if (FRAME_PIXEL_HEIGHT (f) == 0)
941 return;
943 gtk_window_get_size (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
944 &gwidth, &gheight);
946 /* Do this before resize, as we don't know yet if we will be resized. */
947 xg_clear_under_internal_border (f);
949 if (FRAME_VISIBLE_P (f))
951 int scale = xg_get_gdk_scale ();
952 totalheight /= scale;
953 totalwidth /= scale;
956 /* Resize the top level widget so rows and columns remain constant.
958 When the frame is fullheight and we only want to change the width
959 or it is fullwidth and we only want to change the height we should
960 be able to preserve the fullscreen property. However, due to the
961 fact that we have to send a resize request anyway, the window
962 manager will abolish it. At least the respective size should
963 remain unchanged but giving the frame back its normal size will
964 be broken ... */
965 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
967 frame_size_history_add
968 (f, Qxg_frame_set_char_size_1, width, height,
969 list2 (make_number (gheight),
970 make_number (totalheight)));
972 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
973 gwidth,
974 totalheight);
976 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
978 frame_size_history_add
979 (f, Qxg_frame_set_char_size_2, width, height,
980 list2 (make_number (gwidth),
981 make_number (totalwidth)));
983 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
984 totalwidth,
985 gheight);
987 else
989 frame_size_history_add
990 (f, Qxg_frame_set_char_size_3, width, height,
991 list2 (make_number (totalwidth),
992 make_number (totalheight)));
994 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
995 totalwidth,
996 totalheight);
997 fullscreen = Qnil;
1000 SET_FRAME_GARBAGED (f);
1001 cancel_mouse_face (f);
1003 x_wm_set_size_hint (f, 0, 0);
1004 /* We can not call change_frame_size for a mapped frame,
1005 we can not set pixel width/height either. The window manager may
1006 override our resize request, XMonad does this all the time.
1007 The best we can do is try to sync, so lisp code sees the updated
1008 size as fast as possible.
1009 For unmapped windows, we can set rows/cols. When
1010 the frame is mapped again we will (hopefully) get the correct size. */
1011 if (FRAME_VISIBLE_P (f))
1013 /* Must call this to flush out events */
1014 (void)gtk_events_pending ();
1015 gdk_flush ();
1016 x_wait_for_event (f, ConfigureNotify);
1018 if (!NILP (fullscreen))
1019 /* Try to restore fullscreen state. */
1021 store_frame_param (f, Qfullscreen, fullscreen);
1022 x_set_fullscreen (f, fullscreen, fullscreen);
1025 else
1026 adjust_frame_size (f, width, height, 5, 0, Qxg_frame_set_char_size);
1030 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
1031 The policy is to keep the number of editable lines. */
1033 #if 0
1034 static void
1035 xg_height_or_width_changed (struct frame *f)
1037 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1038 FRAME_TOTAL_PIXEL_WIDTH (f),
1039 FRAME_TOTAL_PIXEL_HEIGHT (f));
1040 f->output_data.x->hint_flags = 0;
1041 x_wm_set_size_hint (f, 0, 0);
1043 #endif
1045 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
1046 Must be done like this, because GtkWidget:s can have "hidden"
1047 X Window that aren't accessible.
1049 Return 0 if no widget match WDESC. */
1051 GtkWidget *
1052 xg_win_to_widget (Display *dpy, Window wdesc)
1054 gpointer gdkwin;
1055 GtkWidget *gwdesc = 0;
1057 block_input ();
1059 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
1060 wdesc);
1061 if (gdkwin)
1063 GdkEvent event;
1064 event.any.window = gdkwin;
1065 event.any.type = GDK_NOTHING;
1066 gwdesc = gtk_get_event_widget (&event);
1069 unblock_input ();
1070 return gwdesc;
1073 /* Set the background of widget W to PIXEL. */
1075 static void
1076 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
1078 #ifdef HAVE_GTK3
1079 GdkRGBA bg;
1080 XColor xbg;
1081 xbg.pixel = pixel;
1082 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1084 bg.red = (double)xbg.red/65535.0;
1085 bg.green = (double)xbg.green/65535.0;
1086 bg.blue = (double)xbg.blue/65535.0;
1087 bg.alpha = 1.0;
1088 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1090 #else
1091 GdkColor bg;
1092 GdkColormap *map = gtk_widget_get_colormap (w);
1093 gdk_colormap_query_color (map, pixel, &bg);
1094 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1095 #endif
1098 /* Callback called when the gtk theme changes.
1099 We notify lisp code so it can fix faces used for region for example. */
1101 static void
1102 style_changed_cb (GObject *go,
1103 GParamSpec *spec,
1104 gpointer user_data)
1106 struct input_event event;
1107 GdkDisplay *gdpy = user_data;
1108 const char *display_name = gdk_display_get_name (gdpy);
1109 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1111 EVENT_INIT (event);
1112 event.kind = CONFIG_CHANGED_EVENT;
1113 event.frame_or_window = build_string (display_name);
1114 /* Theme doesn't change often, so intern is called seldom. */
1115 event.arg = intern ("theme-name");
1116 kbd_buffer_store_event (&event);
1118 update_theme_scrollbar_width ();
1119 update_theme_scrollbar_height ();
1121 /* If scroll bar width changed, we need set the new size on all frames
1122 on this display. */
1123 if (dpy)
1125 Lisp_Object rest, frame;
1126 FOR_EACH_FRAME (rest, frame)
1128 struct frame *f = XFRAME (frame);
1129 if (FRAME_LIVE_P (f)
1130 && FRAME_X_P (f)
1131 && FRAME_X_DISPLAY (f) == dpy)
1133 x_set_scroll_bar_default_width (f);
1134 x_set_scroll_bar_default_height (f);
1135 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1141 /* Called when a delete-event occurs on WIDGET. */
1143 static gboolean
1144 delete_cb (GtkWidget *widget,
1145 GdkEvent *event,
1146 gpointer user_data)
1148 return TRUE;
1151 /* Create and set up the GTK widgets for frame F.
1152 Return true if creation succeeded. */
1154 bool
1155 xg_create_frame_widgets (struct frame *f)
1157 GtkWidget *wtop;
1158 GtkWidget *wvbox, *whbox;
1159 GtkWidget *wfixed;
1160 #ifndef HAVE_GTK3
1161 GtkRcStyle *style;
1162 #endif
1163 char *title = 0;
1165 block_input ();
1167 if (FRAME_X_EMBEDDED_P (f))
1169 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1170 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1172 else
1173 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1175 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1176 has backported it to Gtk+ 2.0 and they add the resize grip for
1177 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1178 forever, so disable the grip. */
1179 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1180 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1181 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1182 #endif
1184 xg_set_screen (wtop, f);
1186 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1187 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1188 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1189 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1191 #ifdef HAVE_GTK3
1192 wfixed = emacs_fixed_new (f);
1193 #else
1194 wfixed = gtk_fixed_new ();
1195 #endif
1197 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1199 if (wtop) gtk_widget_destroy (wtop);
1200 if (wvbox) gtk_widget_destroy (wvbox);
1201 if (whbox) gtk_widget_destroy (whbox);
1202 if (wfixed) gtk_widget_destroy (wfixed);
1204 unblock_input ();
1205 return 0;
1208 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1209 gtk_widget_set_name (wtop, EMACS_CLASS);
1210 gtk_widget_set_name (wvbox, "pane");
1211 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1213 /* If this frame has a title or name, set it in the title bar. */
1214 if (! NILP (f->title))
1215 title = SSDATA (ENCODE_UTF_8 (f->title));
1216 else if (! NILP (f->name))
1217 title = SSDATA (ENCODE_UTF_8 (f->name));
1219 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1221 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1222 FRAME_GTK_WIDGET (f) = wfixed;
1223 f->output_data.x->vbox_widget = wvbox;
1224 f->output_data.x->hbox_widget = whbox;
1226 gtk_widget_set_has_window (wfixed, TRUE);
1228 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1229 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1230 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1232 if (FRAME_EXTERNAL_TOOL_BAR (f))
1233 update_frame_tool_bar (f);
1235 /* We don't want this widget double buffered, because we draw on it
1236 with regular X drawing primitives, so from a GTK/GDK point of
1237 view, the widget is totally blank. When an expose comes, this
1238 will make the widget blank, and then Emacs redraws it. This flickers
1239 a lot, so we turn off double buffering. */
1240 gtk_widget_set_double_buffered (wfixed, FALSE);
1242 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1243 SSDATA (Vx_resource_name),
1244 SSDATA (Vx_resource_class));
1246 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1247 GTK is to destroy the widget. We want Emacs to do that instead. */
1248 g_signal_connect (G_OBJECT (wtop), "delete-event",
1249 G_CALLBACK (delete_cb), f);
1251 /* Convert our geometry parameters into a geometry string
1252 and specify it.
1253 GTK will itself handle calculating the real position this way. */
1254 xg_set_geometry (f);
1255 f->win_gravity
1256 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1258 gtk_widget_add_events (wfixed,
1259 GDK_POINTER_MOTION_MASK
1260 | GDK_EXPOSURE_MASK
1261 | GDK_BUTTON_PRESS_MASK
1262 | GDK_BUTTON_RELEASE_MASK
1263 | GDK_KEY_PRESS_MASK
1264 | GDK_ENTER_NOTIFY_MASK
1265 | GDK_LEAVE_NOTIFY_MASK
1266 | GDK_FOCUS_CHANGE_MASK
1267 | GDK_STRUCTURE_MASK
1268 | GDK_VISIBILITY_NOTIFY_MASK);
1270 /* Must realize the windows so the X window gets created. It is used
1271 by callers of this function. */
1272 gtk_widget_realize (wfixed);
1273 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1275 /* Since GTK clears its window by filling with the background color,
1276 we must keep X and GTK background in sync. */
1277 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1279 #ifndef HAVE_GTK3
1280 /* Also, do not let any background pixmap to be set, this looks very
1281 bad as Emacs overwrites the background pixmap with its own idea
1282 of background color. */
1283 style = gtk_widget_get_modifier_style (wfixed);
1285 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1286 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1287 gtk_widget_modify_style (wfixed, style);
1288 #else
1289 gtk_widget_set_can_focus (wfixed, TRUE);
1290 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1291 #endif
1293 #ifdef USE_GTK_TOOLTIP
1294 /* Steal a tool tip window we can move ourselves. */
1295 f->output_data.x->ttip_widget = 0;
1296 f->output_data.x->ttip_lbl = 0;
1297 f->output_data.x->ttip_window = 0;
1298 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1299 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1300 #endif
1303 GdkScreen *screen = gtk_widget_get_screen (wtop);
1304 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1305 /* Only connect this signal once per screen. */
1306 if (! g_signal_handler_find (G_OBJECT (gs),
1307 G_SIGNAL_MATCH_FUNC,
1308 0, 0, 0,
1309 G_CALLBACK (style_changed_cb),
1312 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1313 G_CALLBACK (style_changed_cb),
1314 gdk_screen_get_display (screen));
1318 unblock_input ();
1320 return 1;
1323 void
1324 xg_free_frame_widgets (struct frame *f)
1326 if (FRAME_GTK_OUTER_WIDGET (f))
1328 #ifdef USE_GTK_TOOLTIP
1329 struct x_output *x = f->output_data.x;
1330 #endif
1331 struct xg_frame_tb_info *tbinfo
1332 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1333 TB_INFO_KEY);
1334 if (tbinfo)
1335 xfree (tbinfo);
1337 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1338 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1339 FRAME_GTK_OUTER_WIDGET (f) = 0;
1340 #ifdef USE_GTK_TOOLTIP
1341 if (x->ttip_lbl)
1342 gtk_widget_destroy (x->ttip_lbl);
1343 if (x->ttip_widget)
1344 g_object_unref (G_OBJECT (x->ttip_widget));
1345 #endif
1349 /* Set the normal size hints for the window manager, for frame F.
1350 FLAGS is the flags word to use--or 0 meaning preserve the flags
1351 that the window now has.
1352 If USER_POSITION, set the User Position
1353 flag (this is useful when FLAGS is 0). */
1355 void
1356 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1358 /* Must use GTK routines here, otherwise GTK resets the size hints
1359 to its own defaults. */
1360 GdkGeometry size_hints;
1361 gint hint_flags = 0;
1362 int base_width, base_height;
1363 int min_rows = 0, min_cols = 0;
1364 int win_gravity = f->win_gravity;
1365 Lisp_Object fs_state, frame;
1366 int scale = xg_get_gdk_scale ();
1368 /* Don't set size hints during initialization; that apparently leads
1369 to a race condition. See the thread at
1370 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1371 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1372 return;
1374 XSETFRAME (frame, f);
1375 fs_state = Fframe_parameter (frame, Qfullscreen);
1376 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1378 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1379 Gtk3 don't get along and the frame shrinks (!).
1381 return;
1384 if (flags)
1386 memset (&size_hints, 0, sizeof (size_hints));
1387 f->output_data.x->size_hints = size_hints;
1388 f->output_data.x->hint_flags = hint_flags;
1390 else
1391 flags = f->size_hint_flags;
1393 size_hints = f->output_data.x->size_hints;
1394 hint_flags = f->output_data.x->hint_flags;
1396 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1397 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1398 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1400 hint_flags |= GDK_HINT_BASE_SIZE;
1401 /* Use one row/col here so base_height/width does not become zero.
1402 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1403 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1404 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1405 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1407 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1408 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1410 size_hints.base_width = base_width;
1411 size_hints.base_height = base_height;
1412 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
1413 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1415 /* These currently have a one to one mapping with the X values, but I
1416 don't think we should rely on that. */
1417 hint_flags |= GDK_HINT_WIN_GRAVITY;
1418 size_hints.win_gravity = 0;
1419 if (win_gravity == NorthWestGravity)
1420 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1421 else if (win_gravity == NorthGravity)
1422 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1423 else if (win_gravity == NorthEastGravity)
1424 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1425 else if (win_gravity == WestGravity)
1426 size_hints.win_gravity = GDK_GRAVITY_WEST;
1427 else if (win_gravity == CenterGravity)
1428 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1429 else if (win_gravity == EastGravity)
1430 size_hints.win_gravity = GDK_GRAVITY_EAST;
1431 else if (win_gravity == SouthWestGravity)
1432 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1433 else if (win_gravity == SouthGravity)
1434 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1435 else if (win_gravity == SouthEastGravity)
1436 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1437 else if (win_gravity == StaticGravity)
1438 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1440 if (user_position)
1442 hint_flags &= ~GDK_HINT_POS;
1443 hint_flags |= GDK_HINT_USER_POS;
1446 size_hints.base_width /= scale;
1447 size_hints.base_height /= scale;
1448 size_hints.width_inc /= scale;
1449 size_hints.height_inc /= scale;
1451 if (hint_flags != f->output_data.x->hint_flags
1452 || memcmp (&size_hints,
1453 &f->output_data.x->size_hints,
1454 sizeof (size_hints)) != 0)
1456 block_input ();
1457 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1458 NULL, &size_hints, hint_flags);
1459 f->output_data.x->size_hints = size_hints;
1460 f->output_data.x->hint_flags = hint_flags;
1461 unblock_input ();
1465 /* Change background color of a frame.
1466 Since GTK uses the background color to clear the window, we must
1467 keep the GTK and X colors in sync.
1468 F is the frame to change,
1469 BG is the pixel value to change to. */
1471 void
1472 xg_set_background_color (struct frame *f, unsigned long bg)
1474 if (FRAME_GTK_WIDGET (f))
1476 block_input ();
1477 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1478 unblock_input ();
1483 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1484 functions so GTK does not overwrite the icon. */
1486 void
1487 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1489 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1490 icon_pixmap,
1491 icon_mask);
1492 if (gp)
1493 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1498 /***********************************************************************
1499 Dialog functions
1500 ***********************************************************************/
1501 /* Return the dialog title to use for a dialog of type KEY.
1502 This is the encoding used by lwlib. We use the same for GTK. */
1504 static const char *
1505 get_dialog_title (char key)
1507 const char *title = "";
1509 switch (key) {
1510 case 'E': case 'e':
1511 title = "Error";
1512 break;
1514 case 'I': case 'i':
1515 title = "Information";
1516 break;
1518 case 'L': case 'l':
1519 title = "Prompt";
1520 break;
1522 case 'P': case 'p':
1523 title = "Prompt";
1524 break;
1526 case 'Q': case 'q':
1527 title = "Question";
1528 break;
1531 return title;
1534 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1535 the dialog, but return TRUE so the event does not propagate further
1536 in GTK. This prevents GTK from destroying the dialog widget automatically
1537 and we can always destroy the widget manually, regardless of how
1538 it was popped down (button press or WM_DELETE_WINDOW).
1539 W is the dialog widget.
1540 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1541 user_data is NULL (not used).
1543 Returns TRUE to end propagation of event. */
1545 static gboolean
1546 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1548 gtk_widget_unmap (w);
1549 return TRUE;
1552 /* Create a popup dialog window. See also xg_create_widget below.
1553 WV is a widget_value describing the dialog.
1554 SELECT_CB is the callback to use when a button has been pressed.
1555 DEACTIVATE_CB is the callback to use when the dialog pops down.
1557 Returns the GTK dialog widget. */
1559 static GtkWidget *
1560 create_dialog (widget_value *wv,
1561 GCallback select_cb,
1562 GCallback deactivate_cb)
1564 const char *title = get_dialog_title (wv->name[0]);
1565 int total_buttons = wv->name[1] - '0';
1566 int right_buttons = wv->name[4] - '0';
1567 int left_buttons;
1568 int button_nr = 0;
1569 int button_spacing = 10;
1570 GtkWidget *wdialog = gtk_dialog_new ();
1571 GtkDialog *wd = GTK_DIALOG (wdialog);
1572 widget_value *item;
1573 GtkWidget *whbox_down;
1575 /* If the number of buttons is greater than 4, make two rows of buttons
1576 instead. This looks better. */
1577 bool make_two_rows = total_buttons > 4;
1579 #if GTK_CHECK_VERSION (3, 12, 0)
1580 GtkBuilder *gbld = gtk_builder_new ();
1581 GObject *go = gtk_buildable_get_internal_child (GTK_BUILDABLE (wd),
1582 gbld,
1583 "action_area");
1584 GtkBox *cur_box = GTK_BOX (go);
1585 g_object_unref (G_OBJECT (gbld));
1586 #else
1587 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1588 #endif
1590 if (right_buttons == 0) right_buttons = total_buttons/2;
1591 left_buttons = total_buttons - right_buttons;
1593 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1594 gtk_widget_set_name (wdialog, "emacs-dialog");
1597 if (make_two_rows)
1599 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1600 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1601 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1602 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1603 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1604 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1606 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1607 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1608 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1610 cur_box = GTK_BOX (whbox_up);
1613 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1614 G_CALLBACK (dialog_delete_callback), 0);
1616 if (deactivate_cb)
1618 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1619 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1622 for (item = wv->contents; item; item = item->next)
1624 char *utf8_label = get_utf8_string (item->value);
1625 GtkWidget *w;
1626 GtkRequisition req;
1628 if (item->name && strcmp (item->name, "message") == 0)
1630 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1631 /* This is the text part of the dialog. */
1632 w = gtk_label_new (utf8_label);
1633 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1634 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1635 #if GTK_CHECK_VERSION (3, 14, 0)
1636 gtk_widget_set_halign (w, GTK_ALIGN_START);
1637 gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
1638 #else
1639 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1640 #endif
1641 /* Try to make dialog look better. Must realize first so
1642 the widget can calculate the size it needs. */
1643 gtk_widget_realize (w);
1644 gtk_widget_get_preferred_size (w, NULL, &req);
1645 gtk_box_set_spacing (wvbox, req.height);
1646 if (item->value && strlen (item->value) > 0)
1647 button_spacing = 2*req.width/strlen (item->value);
1648 if (button_spacing < 10) button_spacing = 10;
1650 else
1652 /* This is one button to add to the dialog. */
1653 w = gtk_button_new_with_label (utf8_label);
1654 if (! item->enabled)
1655 gtk_widget_set_sensitive (w, FALSE);
1656 if (select_cb)
1657 g_signal_connect (G_OBJECT (w), "clicked",
1658 select_cb, item->call_data);
1660 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1661 if (++button_nr == left_buttons)
1663 if (make_two_rows)
1664 cur_box = GTK_BOX (whbox_down);
1668 if (utf8_label)
1669 g_free (utf8_label);
1672 return wdialog;
1675 struct xg_dialog_data
1677 GMainLoop *loop;
1678 int response;
1679 GtkWidget *w;
1680 guint timerid;
1683 /* Function that is called when the file or font dialogs pop down.
1684 W is the dialog widget, RESPONSE is the response code.
1685 USER_DATA is what we passed in to g_signal_connect. */
1687 static void
1688 xg_dialog_response_cb (GtkDialog *w,
1689 gint response,
1690 gpointer user_data)
1692 struct xg_dialog_data *dd = user_data;
1693 dd->response = response;
1694 g_main_loop_quit (dd->loop);
1698 /* Destroy the dialog. This makes it pop down. */
1700 static void
1701 pop_down_dialog (void *arg)
1703 struct xg_dialog_data *dd = arg;
1705 block_input ();
1706 if (dd->w) gtk_widget_destroy (dd->w);
1707 if (dd->timerid != 0) g_source_remove (dd->timerid);
1709 g_main_loop_quit (dd->loop);
1710 g_main_loop_unref (dd->loop);
1712 unblock_input ();
1715 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1716 We pass in DATA as gpointer* so we can use this as a callback. */
1718 static gboolean
1719 xg_maybe_add_timer (gpointer data)
1721 struct xg_dialog_data *dd = data;
1722 struct timespec next_time = timer_check ();
1724 dd->timerid = 0;
1726 if (timespec_valid_p (next_time))
1728 time_t s = next_time.tv_sec;
1729 int per_ms = TIMESPEC_RESOLUTION / 1000;
1730 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1731 if (s <= ((guint) -1 - ms) / 1000)
1732 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1734 return FALSE;
1738 /* Pops up a modal dialog W and waits for response.
1739 We don't use gtk_dialog_run because we want to process emacs timers.
1740 The dialog W is not destroyed when this function returns. */
1742 static int
1743 xg_dialog_run (struct frame *f, GtkWidget *w)
1745 ptrdiff_t count = SPECPDL_INDEX ();
1746 struct xg_dialog_data dd;
1748 xg_set_screen (w, f);
1749 gtk_window_set_transient_for (GTK_WINDOW (w),
1750 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1751 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1752 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1754 dd.loop = g_main_loop_new (NULL, FALSE);
1755 dd.response = GTK_RESPONSE_CANCEL;
1756 dd.w = w;
1757 dd.timerid = 0;
1759 g_signal_connect (G_OBJECT (w),
1760 "response",
1761 G_CALLBACK (xg_dialog_response_cb),
1762 &dd);
1763 /* Don't destroy the widget if closed by the window manager close button. */
1764 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1765 gtk_widget_show (w);
1767 record_unwind_protect_ptr (pop_down_dialog, &dd);
1769 (void) xg_maybe_add_timer (&dd);
1770 g_main_loop_run (dd.loop);
1772 dd.w = 0;
1773 unbind_to (count, Qnil);
1775 return dd.response;
1779 /***********************************************************************
1780 File dialog functions
1781 ***********************************************************************/
1782 /* Return true if the old file selection dialog is being used. */
1784 bool
1785 xg_uses_old_file_dialog (void)
1787 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1788 return x_gtk_use_old_file_dialog;
1789 #else
1790 return 0;
1791 #endif
1795 typedef char * (*xg_get_file_func) (GtkWidget *);
1797 /* Return the selected file for file chooser dialog W.
1798 The returned string must be free:d. */
1800 static char *
1801 xg_get_file_name_from_chooser (GtkWidget *w)
1803 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1806 /* Callback called when the "Show hidden files" toggle is pressed.
1807 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1809 static void
1810 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1812 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1813 gboolean visible;
1814 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1815 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1819 /* Callback called when a property changes in a file chooser.
1820 GOBJECT is the file chooser dialog, ARG1 describes the property.
1821 USER_DATA is the toggle widget in the file chooser dialog.
1822 We use this to update the "Show hidden files" toggle when the user
1823 changes that property by right clicking in the file list. */
1825 static void
1826 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1828 if (strcmp (arg1->name, "show-hidden") == 0)
1830 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1831 gboolean visible, toggle_on;
1833 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1834 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1836 if (!!visible != !!toggle_on)
1838 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1839 G_CALLBACK (xg_toggle_visibility_cb),
1840 gobject);
1841 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1842 g_signal_handlers_unblock_by_func
1843 (G_OBJECT (wtoggle),
1844 G_CALLBACK (xg_toggle_visibility_cb),
1845 gobject);
1847 x_gtk_show_hidden_files = visible;
1851 /* Read a file name from the user using a file chooser dialog.
1852 F is the current frame.
1853 PROMPT is a prompt to show to the user. May not be NULL.
1854 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1855 If MUSTMATCH_P, the returned file name must be an existing
1856 file. (Actually, this only has cosmetic effects, the user can
1857 still enter a non-existing file.) *FUNC is set to a function that
1858 can be used to retrieve the selected file name from the returned widget.
1860 Returns the created widget. */
1862 static GtkWidget *
1863 xg_get_file_with_chooser (struct frame *f,
1864 char *prompt,
1865 char *default_filename,
1866 bool mustmatch_p, bool only_dir_p,
1867 xg_get_file_func *func)
1869 char msgbuf[1024];
1871 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1872 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1873 GtkFileChooserAction action = (mustmatch_p ?
1874 GTK_FILE_CHOOSER_ACTION_OPEN :
1875 GTK_FILE_CHOOSER_ACTION_SAVE);
1877 if (only_dir_p)
1878 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1880 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1881 XG_TEXT_CANCEL, GTK_RESPONSE_CANCEL,
1882 (mustmatch_p || only_dir_p ?
1883 XG_TEXT_OPEN : XG_TEXT_OK),
1884 GTK_RESPONSE_OK,
1885 NULL);
1886 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1888 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1889 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1890 gtk_widget_show (wbox);
1891 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1893 if (x_gtk_show_hidden_files)
1895 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1896 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1898 gtk_widget_show (wtoggle);
1899 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1900 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1901 g_signal_connect (G_OBJECT (filewin), "notify",
1902 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1904 if (x_gtk_file_dialog_help_text)
1906 char *z = msgbuf;
1907 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1908 Show the C-l help text only for versions < 2.10. */
1909 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1910 z = stpcpy (z, "\nType C-l to display a file name text entry box.\n");
1911 strcpy (z, "\nIf you don't like this file selector, use the "
1912 "corresponding\nkey binding or customize "
1913 "use-file-dialog to turn it off.");
1915 wmessage = gtk_label_new (msgbuf);
1916 gtk_widget_show (wmessage);
1919 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1920 if (x_gtk_file_dialog_help_text)
1921 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1922 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1924 if (default_filename)
1926 Lisp_Object file;
1927 struct gcpro gcpro1;
1928 char *utf8_filename;
1929 GCPRO1 (file);
1931 file = build_string (default_filename);
1933 /* File chooser does not understand ~/... in the file name. It must be
1934 an absolute name starting with /. */
1935 if (default_filename[0] != '/')
1936 file = Fexpand_file_name (file, Qnil);
1938 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1939 if (! NILP (Ffile_directory_p (file)))
1940 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1941 utf8_filename);
1942 else
1944 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1945 utf8_filename);
1946 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1948 char *cp = strrchr (utf8_filename, '/');
1949 if (cp) ++cp;
1950 else cp = utf8_filename;
1951 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1955 UNGCPRO;
1958 *func = xg_get_file_name_from_chooser;
1959 return filewin;
1962 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1964 /* Return the selected file for file selector dialog W.
1965 The returned string must be free:d. */
1967 static char *
1968 xg_get_file_name_from_selector (GtkWidget *w)
1970 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1971 return xstrdup (gtk_file_selection_get_filename (filesel));
1974 /* Create a file selection 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. *FUNC is set to a function that can be used to retrieve the
1980 selected file name from the returned widget.
1982 Returns the created widget. */
1984 static GtkWidget *
1985 xg_get_file_with_selection (struct frame *f,
1986 char *prompt,
1987 char *default_filename,
1988 bool mustmatch_p, bool only_dir_p,
1989 xg_get_file_func *func)
1991 GtkWidget *filewin;
1992 GtkFileSelection *filesel;
1994 filewin = gtk_file_selection_new (prompt);
1995 filesel = GTK_FILE_SELECTION (filewin);
1997 if (default_filename)
1998 gtk_file_selection_set_filename (filesel, default_filename);
2000 if (mustmatch_p)
2002 /* The selection_entry part of filesel is not documented. */
2003 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
2004 gtk_file_selection_hide_fileop_buttons (filesel);
2007 *func = xg_get_file_name_from_selector;
2009 return filewin;
2011 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
2013 /* Read a file name from the user using a file dialog, either the old
2014 file selection dialog, or the new file chooser dialog. Which to use
2015 depends on what the GTK version used has, and what the value of
2016 gtk-use-old-file-dialog.
2017 F is the current frame.
2018 PROMPT is a prompt to show to the user. May not be NULL.
2019 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
2020 If MUSTMATCH_P, the returned file name must be an existing
2021 file.
2023 Returns a file name or NULL if no file was selected.
2024 The returned string must be freed by the caller. */
2026 char *
2027 xg_get_file_name (struct frame *f,
2028 char *prompt,
2029 char *default_filename,
2030 bool mustmatch_p,
2031 bool only_dir_p)
2033 GtkWidget *w = 0;
2034 char *fn = 0;
2035 int filesel_done = 0;
2036 xg_get_file_func func;
2038 #ifdef HAVE_GTK_FILE_SELECTION_NEW
2040 if (xg_uses_old_file_dialog ())
2041 w = xg_get_file_with_selection (f, prompt, default_filename,
2042 mustmatch_p, only_dir_p, &func);
2043 else
2044 w = xg_get_file_with_chooser (f, prompt, default_filename,
2045 mustmatch_p, only_dir_p, &func);
2047 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
2048 w = xg_get_file_with_chooser (f, prompt, default_filename,
2049 mustmatch_p, only_dir_p, &func);
2050 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
2052 gtk_widget_set_name (w, "emacs-filedialog");
2054 filesel_done = xg_dialog_run (f, w);
2055 if (filesel_done == GTK_RESPONSE_OK)
2056 fn = (*func) (w);
2058 gtk_widget_destroy (w);
2059 return fn;
2062 /***********************************************************************
2063 GTK font chooser
2064 ***********************************************************************/
2066 #ifdef HAVE_FREETYPE
2068 #if USE_NEW_GTK_FONT_CHOOSER
2070 #define XG_WEIGHT_TO_SYMBOL(w) \
2071 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
2072 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
2073 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
2074 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
2075 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
2076 : w <= PANGO_WEIGHT_BOLD ? Qbold \
2077 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
2078 : Qultra_bold)
2080 #define XG_STYLE_TO_SYMBOL(s) \
2081 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
2082 : s == PANGO_STYLE_ITALIC ? Qitalic \
2083 : Qnormal)
2085 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2088 static char *x_last_font_name;
2090 /* Pop up a GTK font selector and return the name of the font the user
2091 selects, as a C string. The returned font name follows GTK's own
2092 format:
2094 `FAMILY [VALUE1 VALUE2] SIZE'
2096 This can be parsed using font_parse_fcname in font.c.
2097 DEFAULT_NAME, if non-zero, is the default font name. */
2099 Lisp_Object
2100 xg_get_font (struct frame *f, const char *default_name)
2102 GtkWidget *w;
2103 int done = 0;
2104 Lisp_Object font = Qnil;
2106 w = gtk_font_chooser_dialog_new
2107 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2109 if (default_name)
2111 /* Convert fontconfig names to Gtk names, i.e. remove - before
2112 number */
2113 char *p = strrchr (default_name, '-');
2114 if (p)
2116 char *ep = p+1;
2117 while (c_isdigit (*ep))
2118 ++ep;
2119 if (*ep == '\0') *p = ' ';
2122 else if (x_last_font_name)
2123 default_name = x_last_font_name;
2125 if (default_name)
2126 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2128 gtk_widget_set_name (w, "emacs-fontdialog");
2129 done = xg_dialog_run (f, w);
2130 if (done == GTK_RESPONSE_OK)
2132 #if USE_NEW_GTK_FONT_CHOOSER
2133 /* Use the GTK3 font chooser. */
2134 PangoFontDescription *desc
2135 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2137 if (desc)
2139 const char *name = pango_font_description_get_family (desc);
2140 gint size = pango_font_description_get_size (desc);
2141 PangoWeight weight = pango_font_description_get_weight (desc);
2142 PangoStyle style = pango_font_description_get_style (desc);
2144 font = CALLN (Ffont_spec,
2145 QCname, build_string (name),
2146 QCsize, make_float (pango_units_to_double (size)),
2147 QCweight, XG_WEIGHT_TO_SYMBOL (weight),
2148 QCslant, XG_STYLE_TO_SYMBOL (style),
2149 QCtype, Qxft);
2151 pango_font_description_free (desc);
2152 dupstring (&x_last_font_name, name);
2155 #else /* Use old font selector, which just returns the font name. */
2157 char *font_name
2158 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2160 if (font_name)
2162 font = build_string (font_name);
2163 g_free (x_last_font_name);
2164 x_last_font_name = font_name;
2166 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2169 gtk_widget_destroy (w);
2170 return font;
2172 #endif /* HAVE_FREETYPE */
2176 /***********************************************************************
2177 Menu functions.
2178 ***********************************************************************/
2180 /* The name of menu items that can be used for customization. Since GTK
2181 RC files are very crude and primitive, we have to set this on all
2182 menu item names so a user can easily customize menu items. */
2184 #define MENU_ITEM_NAME "emacs-menuitem"
2187 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2188 during GC. The next member points to the items. */
2189 static xg_list_node xg_menu_cb_list;
2191 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2192 during GC. The next member points to the items. */
2193 static xg_list_node xg_menu_item_cb_list;
2195 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2196 F is the frame CL_DATA will be initialized for.
2197 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2199 The menu bar and all sub menus under the menu bar in a frame
2200 share the same structure, hence the reference count.
2202 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2203 allocated xg_menu_cb_data if CL_DATA is NULL. */
2205 static xg_menu_cb_data *
2206 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2208 if (! cl_data)
2210 cl_data = xmalloc (sizeof *cl_data);
2211 cl_data->f = f;
2212 cl_data->menu_bar_vector = f->menu_bar_vector;
2213 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2214 cl_data->highlight_cb = highlight_cb;
2215 cl_data->ref_count = 0;
2217 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2220 cl_data->ref_count++;
2222 return cl_data;
2225 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2226 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2228 When the menu bar is updated, menu items may have been added and/or
2229 removed, so menu_bar_vector and menu_bar_items_used change. We must
2230 then update CL_DATA since it is used to determine which menu
2231 item that is invoked in the menu.
2232 HIGHLIGHT_CB could change, there is no check that the same
2233 function is given when modifying a menu bar as was given when
2234 creating the menu bar. */
2236 static void
2237 update_cl_data (xg_menu_cb_data *cl_data,
2238 struct frame *f,
2239 GCallback highlight_cb)
2241 if (cl_data)
2243 cl_data->f = f;
2244 cl_data->menu_bar_vector = f->menu_bar_vector;
2245 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2246 cl_data->highlight_cb = highlight_cb;
2250 /* Decrease reference count for CL_DATA.
2251 If reference count is zero, free CL_DATA. */
2253 static void
2254 unref_cl_data (xg_menu_cb_data *cl_data)
2256 if (cl_data && cl_data->ref_count > 0)
2258 cl_data->ref_count--;
2259 if (cl_data->ref_count == 0)
2261 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2262 xfree (cl_data);
2267 /* Function that marks all lisp data during GC. */
2269 void
2270 xg_mark_data (void)
2272 xg_list_node *iter;
2273 Lisp_Object rest, frame;
2275 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2276 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2278 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2280 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2282 if (! NILP (cb_data->help))
2283 mark_object (cb_data->help);
2286 FOR_EACH_FRAME (rest, frame)
2288 struct frame *f = XFRAME (frame);
2290 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2292 struct xg_frame_tb_info *tbinfo
2293 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2294 TB_INFO_KEY);
2295 if (tbinfo)
2297 mark_object (tbinfo->last_tool_bar);
2298 mark_object (tbinfo->style);
2305 /* Callback called when a menu item is destroyed. Used to free data.
2306 W is the widget that is being destroyed (not used).
2307 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2309 static void
2310 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2312 if (client_data)
2314 xg_menu_item_cb_data *data = client_data;
2315 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2316 xfree (data);
2320 /* Callback called when the pointer enters/leaves a menu item.
2321 W is the parent of the menu item.
2322 EVENT is either an enter event or leave event.
2323 CLIENT_DATA is not used.
2325 Returns FALSE to tell GTK to keep processing this event. */
2327 static gboolean
2328 menuitem_highlight_callback (GtkWidget *w,
2329 GdkEventCrossing *event,
2330 gpointer client_data)
2332 GdkEvent ev;
2333 GtkWidget *subwidget;
2334 xg_menu_item_cb_data *data;
2336 ev.crossing = *event;
2337 subwidget = gtk_get_event_widget (&ev);
2338 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2339 if (data)
2341 if (! NILP (data->help) && data->cl_data->highlight_cb)
2343 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2344 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2345 (*func) (subwidget, call_data);
2349 return FALSE;
2352 /* Callback called when a menu is destroyed. Used to free data.
2353 W is the widget that is being destroyed (not used).
2354 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2356 static void
2357 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2359 unref_cl_data (client_data);
2362 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2363 must be non-NULL) and can be inserted into a menu item.
2365 Returns the GtkHBox. */
2367 static GtkWidget *
2368 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2370 GtkWidget *wlbl;
2371 GtkWidget *wkey;
2372 GtkWidget *wbox;
2374 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2375 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2376 wlbl = gtk_label_new (utf8_label);
2377 wkey = gtk_label_new (utf8_key);
2379 #if GTK_CHECK_VERSION (3, 14, 0)
2380 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2381 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2382 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2383 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2384 #else
2385 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2386 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2387 #endif
2388 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2389 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2391 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2392 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2393 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2395 return wbox;
2398 /* Make and return a menu item widget with the key to the right.
2399 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2400 UTF8_KEY is the text representing the key binding.
2401 ITEM is the widget_value describing the menu item.
2403 GROUP is an in/out parameter. If the menu item to be created is not
2404 part of any radio menu group, *GROUP contains NULL on entry and exit.
2405 If the menu item to be created is part of a radio menu group, on entry
2406 *GROUP contains the group to use, or NULL if this is the first item
2407 in the group. On exit, *GROUP contains the radio item group.
2409 Unfortunately, keys don't line up as nicely as in Motif,
2410 but the MacOS X version doesn't either, so I guess that is OK. */
2412 static GtkWidget *
2413 make_menu_item (const char *utf8_label,
2414 const char *utf8_key,
2415 widget_value *item,
2416 GSList **group)
2418 GtkWidget *w;
2419 GtkWidget *wtoadd = 0;
2421 /* It has been observed that some menu items have a NULL name field.
2422 This will lead to this function being called with a NULL utf8_label.
2423 GTK crashes on that so we set a blank label. Why there is a NULL
2424 name remains to be investigated. */
2425 if (! utf8_label) utf8_label = " ";
2427 if (utf8_key)
2428 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2430 if (item->button_type == BUTTON_TYPE_TOGGLE)
2432 *group = NULL;
2433 if (utf8_key) w = gtk_check_menu_item_new ();
2434 else w = gtk_check_menu_item_new_with_label (utf8_label);
2435 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2437 else if (item->button_type == BUTTON_TYPE_RADIO)
2439 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2440 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2441 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2442 if (item->selected)
2443 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2445 else
2447 *group = NULL;
2448 if (utf8_key) w = gtk_menu_item_new ();
2449 else w = gtk_menu_item_new_with_label (utf8_label);
2452 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2453 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2455 return w;
2458 /* Create a menu item widget, and connect the callbacks.
2459 ITEM describes the menu item.
2460 F is the frame the created menu belongs to.
2461 SELECT_CB is the callback to use when a menu item is selected.
2462 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2463 CL_DATA points to the callback data to be used for this menu.
2464 GROUP is an in/out parameter. If the menu item to be created is not
2465 part of any radio menu group, *GROUP contains NULL on entry and exit.
2466 If the menu item to be created is part of a radio menu group, on entry
2467 *GROUP contains the group to use, or NULL if this is the first item
2468 in the group. On exit, *GROUP contains the radio item group.
2470 Returns the created GtkWidget. */
2472 static GtkWidget *
2473 xg_create_one_menuitem (widget_value *item,
2474 struct frame *f,
2475 GCallback select_cb,
2476 GCallback highlight_cb,
2477 xg_menu_cb_data *cl_data,
2478 GSList **group)
2480 char *utf8_label;
2481 char *utf8_key;
2482 GtkWidget *w;
2483 xg_menu_item_cb_data *cb_data;
2485 utf8_label = get_utf8_string (item->name);
2486 utf8_key = get_utf8_string (item->key);
2488 w = make_menu_item (utf8_label, utf8_key, item, group);
2490 if (utf8_label) g_free (utf8_label);
2491 if (utf8_key) g_free (utf8_key);
2493 cb_data = xmalloc (sizeof *cb_data);
2495 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2497 cb_data->select_id = 0;
2498 cb_data->help = item->help;
2499 cb_data->cl_data = cl_data;
2500 cb_data->call_data = item->call_data;
2502 g_signal_connect (G_OBJECT (w),
2503 "destroy",
2504 G_CALLBACK (menuitem_destroy_callback),
2505 cb_data);
2507 /* Put cb_data in widget, so we can get at it when modifying menubar */
2508 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2510 /* final item, not a submenu */
2511 if (item->call_data && ! item->contents)
2513 if (select_cb)
2514 cb_data->select_id
2515 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2518 return w;
2521 /* Create a full menu tree specified by DATA.
2522 F is the frame the created menu belongs to.
2523 SELECT_CB is the callback to use when a menu item is selected.
2524 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2525 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2526 If POP_UP_P, create a popup menu.
2527 If MENU_BAR_P, create a menu bar.
2528 TOPMENU is the topmost GtkWidget that others shall be placed under.
2529 It may be NULL, in that case we create the appropriate widget
2530 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2531 CL_DATA is the callback data we shall use for this menu, or NULL
2532 if we haven't set the first callback yet.
2533 NAME is the name to give to the top level menu if this function
2534 creates it. May be NULL to not set any name.
2536 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2537 not NULL.
2539 This function calls itself to create submenus. */
2541 static GtkWidget *
2542 create_menus (widget_value *data,
2543 struct frame *f,
2544 GCallback select_cb,
2545 GCallback deactivate_cb,
2546 GCallback highlight_cb,
2547 bool pop_up_p,
2548 bool menu_bar_p,
2549 GtkWidget *topmenu,
2550 xg_menu_cb_data *cl_data,
2551 const char *name)
2553 widget_value *item;
2554 GtkWidget *wmenu = topmenu;
2555 GSList *group = NULL;
2557 if (! topmenu)
2559 if (! menu_bar_p)
2561 wmenu = gtk_menu_new ();
2562 xg_set_screen (wmenu, f);
2563 /* Connect this to the menu instead of items so we get enter/leave for
2564 disabled items also. TODO: Still does not get enter/leave for
2565 disabled items in detached menus. */
2566 g_signal_connect (G_OBJECT (wmenu),
2567 "enter-notify-event",
2568 G_CALLBACK (menuitem_highlight_callback),
2569 NULL);
2570 g_signal_connect (G_OBJECT (wmenu),
2571 "leave-notify-event",
2572 G_CALLBACK (menuitem_highlight_callback),
2573 NULL);
2575 else
2577 wmenu = gtk_menu_bar_new ();
2578 /* Set width of menu bar to a small value so it doesn't enlarge
2579 a small initial frame size. The width will be set to the
2580 width of the frame later on when it is added to a container.
2581 height -1: Natural height. */
2582 gtk_widget_set_size_request (wmenu, 1, -1);
2585 /* Put cl_data on the top menu for easier access. */
2586 cl_data = make_cl_data (cl_data, f, highlight_cb);
2587 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2588 g_signal_connect (G_OBJECT (wmenu), "destroy",
2589 G_CALLBACK (menu_destroy_callback), cl_data);
2591 if (name)
2592 gtk_widget_set_name (wmenu, name);
2594 if (deactivate_cb)
2595 g_signal_connect (G_OBJECT (wmenu),
2596 "selection-done", deactivate_cb, 0);
2599 for (item = data; item; item = item->next)
2601 GtkWidget *w;
2603 if (pop_up_p && !item->contents && !item->call_data
2604 && !menu_separator_name_p (item->name))
2606 char *utf8_label;
2607 /* A title for a popup. We do the same as GTK does when
2608 creating titles, but it does not look good. */
2609 group = NULL;
2610 utf8_label = get_utf8_string (item->name);
2612 w = gtk_menu_item_new_with_label (utf8_label);
2613 gtk_widget_set_sensitive (w, FALSE);
2614 if (utf8_label) g_free (utf8_label);
2616 else if (menu_separator_name_p (item->name))
2618 group = NULL;
2619 /* GTK only have one separator type. */
2620 w = gtk_separator_menu_item_new ();
2622 else
2624 w = xg_create_one_menuitem (item,
2626 item->contents ? 0 : select_cb,
2627 highlight_cb,
2628 cl_data,
2629 &group);
2631 /* Create a possibly empty submenu for menu bar items, since some
2632 themes don't highlight items correctly without it. */
2633 if (item->contents || menu_bar_p)
2635 GtkWidget *submenu = create_menus (item->contents,
2637 select_cb,
2638 deactivate_cb,
2639 highlight_cb,
2643 cl_data,
2645 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2649 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2650 gtk_widget_set_name (w, MENU_ITEM_NAME);
2653 return wmenu;
2656 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2657 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2658 with some text and buttons.
2659 F is the frame the created item belongs to.
2660 NAME is the name to use for the top widget.
2661 VAL is a widget_value structure describing items to be created.
2662 SELECT_CB is the callback to use when a menu item is selected or
2663 a dialog button is pressed.
2664 DEACTIVATE_CB is the callback to use when an item is deactivated.
2665 For a menu, when a sub menu is not shown anymore, for a dialog it is
2666 called when the dialog is popped down.
2667 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2669 Returns the widget created. */
2671 GtkWidget *
2672 xg_create_widget (const char *type, const char *name, struct frame *f,
2673 widget_value *val, GCallback select_cb,
2674 GCallback deactivate_cb, GCallback highlight_cb)
2676 GtkWidget *w = 0;
2677 bool menu_bar_p = strcmp (type, "menubar") == 0;
2678 bool pop_up_p = strcmp (type, "popup") == 0;
2680 if (strcmp (type, "dialog") == 0)
2682 w = create_dialog (val, select_cb, deactivate_cb);
2683 xg_set_screen (w, f);
2684 gtk_window_set_transient_for (GTK_WINDOW (w),
2685 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2686 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2687 gtk_widget_set_name (w, "emacs-dialog");
2688 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2690 else if (menu_bar_p || pop_up_p)
2692 w = create_menus (val->contents,
2694 select_cb,
2695 deactivate_cb,
2696 highlight_cb,
2697 pop_up_p,
2698 menu_bar_p,
2701 name);
2703 /* Set the cursor to an arrow for popup menus when they are mapped.
2704 This is done by default for menu bar menus. */
2705 if (pop_up_p)
2707 /* Must realize so the GdkWindow inside the widget is created. */
2708 gtk_widget_realize (w);
2709 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2712 else
2714 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2715 type);
2718 return w;
2721 /* Return the label for menu item WITEM. */
2723 static const char *
2724 xg_get_menu_item_label (GtkMenuItem *witem)
2726 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2727 return gtk_label_get_label (wlabel);
2730 /* Return true if the menu item WITEM has the text LABEL. */
2732 static bool
2733 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2735 bool is_same = 0;
2736 char *utf8_label = get_utf8_string (label);
2737 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2739 if (! old_label && ! utf8_label)
2740 is_same = 1;
2741 else if (old_label && utf8_label)
2742 is_same = strcmp (utf8_label, old_label) == 0;
2744 if (utf8_label) g_free (utf8_label);
2746 return is_same;
2749 /* Destroy widgets in LIST. */
2751 static void
2752 xg_destroy_widgets (GList *list)
2754 GList *iter;
2756 for (iter = list; iter; iter = g_list_next (iter))
2758 GtkWidget *w = GTK_WIDGET (iter->data);
2760 /* Destroying the widget will remove it from the container it is in. */
2761 gtk_widget_destroy (w);
2765 /* Update the top level names in MENUBAR (i.e. not submenus).
2766 F is the frame the menu bar belongs to.
2767 *LIST is a list with the current menu bar names (menu item widgets).
2768 ITER is the item within *LIST that shall be updated.
2769 POS is the numerical position, starting at 0, of ITER in *LIST.
2770 VAL describes what the menu bar shall look like after the update.
2771 SELECT_CB is the callback to use when a menu item is selected.
2772 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2773 CL_DATA points to the callback data to be used for this menu bar.
2775 This function calls itself to walk through the menu bar names. */
2777 static void
2778 xg_update_menubar (GtkWidget *menubar,
2779 struct frame *f,
2780 GList **list,
2781 GList *iter,
2782 int pos,
2783 widget_value *val,
2784 GCallback select_cb,
2785 GCallback deactivate_cb,
2786 GCallback highlight_cb,
2787 xg_menu_cb_data *cl_data)
2789 if (! iter && ! val)
2790 return;
2791 else if (iter && ! val)
2793 /* Item(s) have been removed. Remove all remaining items. */
2794 xg_destroy_widgets (iter);
2796 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2797 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2798 gtk_menu_item_new_with_label (""),
2800 /* All updated. */
2801 val = 0;
2802 iter = 0;
2804 else if (! iter && val)
2806 /* Item(s) added. Add all new items in one call. */
2807 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2808 0, 1, menubar, cl_data, 0);
2810 /* All updated. */
2811 val = 0;
2812 iter = 0;
2814 /* Below this neither iter or val is NULL */
2815 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2817 /* This item is still the same, check next item. */
2818 val = val->next;
2819 iter = g_list_next (iter);
2820 ++pos;
2822 else /* This item is changed. */
2824 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2825 GtkMenuItem *witem2 = 0;
2826 bool val_in_menubar = 0;
2827 bool iter_in_new_menubar = 0;
2828 GList *iter2;
2829 widget_value *cur;
2831 /* See if the changed entry (val) is present later in the menu bar */
2832 for (iter2 = iter;
2833 iter2 && ! val_in_menubar;
2834 iter2 = g_list_next (iter2))
2836 witem2 = GTK_MENU_ITEM (iter2->data);
2837 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2840 /* See if the current entry (iter) is present later in the
2841 specification for the new menu bar. */
2842 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2843 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2845 if (val_in_menubar && ! iter_in_new_menubar)
2847 int nr = pos;
2849 /* This corresponds to:
2850 Current: A B C
2851 New: A C
2852 Remove B. */
2854 g_object_ref (G_OBJECT (witem));
2855 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2856 gtk_widget_destroy (GTK_WIDGET (witem));
2858 /* Must get new list since the old changed. */
2859 g_list_free (*list);
2860 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2861 while (nr-- > 0) iter = g_list_next (iter);
2863 else if (! val_in_menubar && ! iter_in_new_menubar)
2865 /* This corresponds to:
2866 Current: A B C
2867 New: A X C
2868 Rename B to X. This might seem to be a strange thing to do,
2869 since if there is a menu under B it will be totally wrong for X.
2870 But consider editing a C file. Then there is a C-mode menu
2871 (corresponds to B above).
2872 If then doing C-x C-f the minibuf menu (X above) replaces the
2873 C-mode menu. When returning from the minibuffer, we get
2874 back the C-mode menu. Thus we do:
2875 Rename B to X (C-mode to minibuf menu)
2876 Rename X to B (minibuf to C-mode menu).
2877 If the X menu hasn't been invoked, the menu under B
2878 is up to date when leaving the minibuffer. */
2879 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2880 char *utf8_label = get_utf8_string (val->name);
2882 /* GTK menu items don't notice when their labels have been
2883 changed from underneath them, so we have to explicitly
2884 use g_object_notify to tell listeners (e.g., a GMenuModel
2885 bridge that might be loaded) that the item's label has
2886 changed. */
2887 gtk_label_set_text (wlabel, utf8_label);
2888 #if GTK_CHECK_VERSION (2, 16, 0)
2889 g_object_notify (G_OBJECT (witem), "label");
2890 #endif
2891 if (utf8_label) g_free (utf8_label);
2892 iter = g_list_next (iter);
2893 val = val->next;
2894 ++pos;
2896 else if (! val_in_menubar && iter_in_new_menubar)
2898 /* This corresponds to:
2899 Current: A B C
2900 New: A X B C
2901 Insert X. */
2903 int nr = pos;
2904 GSList *group = 0;
2905 GtkWidget *w = xg_create_one_menuitem (val,
2907 select_cb,
2908 highlight_cb,
2909 cl_data,
2910 &group);
2912 /* Create a possibly empty submenu for menu bar items, since some
2913 themes don't highlight items correctly without it. */
2914 GtkWidget *submenu = create_menus (NULL, f,
2915 select_cb, deactivate_cb,
2916 highlight_cb,
2917 0, 0, 0, cl_data, 0);
2919 gtk_widget_set_name (w, MENU_ITEM_NAME);
2920 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2921 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2923 g_list_free (*list);
2924 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2925 while (nr-- > 0) iter = g_list_next (iter);
2926 iter = g_list_next (iter);
2927 val = val->next;
2928 ++pos;
2930 else /* if (val_in_menubar && iter_in_new_menubar) */
2932 int nr = pos;
2933 /* This corresponds to:
2934 Current: A B C
2935 New: A C B
2936 Move C before B */
2938 g_object_ref (G_OBJECT (witem2));
2939 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2940 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2941 GTK_WIDGET (witem2), pos);
2942 g_object_unref (G_OBJECT (witem2));
2944 g_list_free (*list);
2945 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2946 while (nr-- > 0) iter = g_list_next (iter);
2947 if (iter) iter = g_list_next (iter);
2948 val = val->next;
2949 ++pos;
2953 /* Update the rest of the menu bar. */
2954 xg_update_menubar (menubar, f, list, iter, pos, val,
2955 select_cb, deactivate_cb, highlight_cb, cl_data);
2958 /* Update the menu item W so it corresponds to VAL.
2959 SELECT_CB is the callback to use when a menu item is selected.
2960 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2961 CL_DATA is the data to set in the widget for menu invocation. */
2963 static void
2964 xg_update_menu_item (widget_value *val,
2965 GtkWidget *w,
2966 GCallback select_cb,
2967 GCallback highlight_cb,
2968 xg_menu_cb_data *cl_data)
2970 GtkWidget *wchild;
2971 GtkLabel *wlbl = 0;
2972 GtkLabel *wkey = 0;
2973 char *utf8_label;
2974 char *utf8_key;
2975 const char *old_label = 0;
2976 const char *old_key = 0;
2977 xg_menu_item_cb_data *cb_data;
2978 bool label_changed = false;
2980 wchild = XG_BIN_CHILD (w);
2981 utf8_label = get_utf8_string (val->name);
2982 utf8_key = get_utf8_string (val->key);
2984 /* See if W is a menu item with a key. See make_menu_item above. */
2985 if (GTK_IS_BOX (wchild))
2987 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2989 wlbl = GTK_LABEL (list->data);
2990 wkey = GTK_LABEL (list->next->data);
2991 g_list_free (list);
2993 if (! utf8_key)
2995 /* Remove the key and keep just the label. */
2996 g_object_ref (G_OBJECT (wlbl));
2997 gtk_container_remove (GTK_CONTAINER (w), wchild);
2998 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2999 g_object_unref (G_OBJECT (wlbl));
3000 wkey = 0;
3004 else /* Just a label. */
3006 wlbl = GTK_LABEL (wchild);
3008 /* Check if there is now a key. */
3009 if (utf8_key)
3011 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
3012 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
3014 wlbl = GTK_LABEL (list->data);
3015 wkey = GTK_LABEL (list->next->data);
3016 g_list_free (list);
3018 gtk_container_remove (GTK_CONTAINER (w), wchild);
3019 gtk_container_add (GTK_CONTAINER (w), wtoadd);
3023 if (wkey) old_key = gtk_label_get_label (wkey);
3024 if (wlbl) old_label = gtk_label_get_label (wlbl);
3026 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
3028 label_changed = true;
3029 gtk_label_set_text (wkey, utf8_key);
3032 if (! old_label || strcmp (utf8_label, old_label) != 0)
3034 label_changed = true;
3035 gtk_label_set_text (wlbl, utf8_label);
3038 if (utf8_key) g_free (utf8_key);
3039 if (utf8_label) g_free (utf8_label);
3041 if (! val->enabled && gtk_widget_get_sensitive (w))
3042 gtk_widget_set_sensitive (w, FALSE);
3043 else if (val->enabled && ! gtk_widget_get_sensitive (w))
3044 gtk_widget_set_sensitive (w, TRUE);
3046 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
3047 if (cb_data)
3049 cb_data->call_data = val->call_data;
3050 cb_data->help = val->help;
3051 cb_data->cl_data = cl_data;
3053 /* We assume the callback functions don't change. */
3054 if (val->call_data && ! val->contents)
3056 /* This item shall have a select callback. */
3057 if (! cb_data->select_id)
3058 cb_data->select_id
3059 = g_signal_connect (G_OBJECT (w), "activate",
3060 select_cb, cb_data);
3062 else if (cb_data->select_id)
3064 g_signal_handler_disconnect (w, cb_data->select_id);
3065 cb_data->select_id = 0;
3069 #if GTK_CHECK_VERSION (2, 16, 0)
3070 if (label_changed) /* See comment in xg_update_menubar. */
3071 g_object_notify (G_OBJECT (w), "label");
3072 #endif
3075 /* Update the toggle menu item W so it corresponds to VAL. */
3077 static void
3078 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3080 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3083 /* Update the radio menu item W so it corresponds to VAL. */
3085 static void
3086 xg_update_radio_item (widget_value *val, GtkWidget *w)
3088 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3091 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3092 SUBMENU may be NULL, in that case a new menu is created.
3093 F is the frame the menu bar belongs to.
3094 VAL describes the contents of the menu bar.
3095 SELECT_CB is the callback to use when a menu item is selected.
3096 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3097 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3098 CL_DATA is the call back data to use for any newly created items.
3100 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3101 was NULL. */
3103 static GtkWidget *
3104 xg_update_submenu (GtkWidget *submenu,
3105 struct frame *f,
3106 widget_value *val,
3107 GCallback select_cb,
3108 GCallback deactivate_cb,
3109 GCallback highlight_cb,
3110 xg_menu_cb_data *cl_data)
3112 GtkWidget *newsub = submenu;
3113 GList *list = 0;
3114 GList *iter;
3115 widget_value *cur;
3116 GList *first_radio = 0;
3118 if (submenu)
3119 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3121 for (cur = val, iter = list;
3122 cur && iter;
3123 iter = g_list_next (iter), cur = cur->next)
3125 GtkWidget *w = GTK_WIDGET (iter->data);
3127 /* Remember first radio button in a group. If we get a mismatch in
3128 a radio group we must rebuild the whole group so that the connections
3129 in GTK becomes correct. */
3130 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3131 first_radio = iter;
3132 else if (cur->button_type != BUTTON_TYPE_RADIO
3133 && ! GTK_IS_RADIO_MENU_ITEM (w))
3134 first_radio = 0;
3136 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3138 if (! menu_separator_name_p (cur->name))
3139 break;
3141 else if (GTK_IS_CHECK_MENU_ITEM (w))
3143 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3144 break;
3145 xg_update_toggle_item (cur, w);
3146 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3148 else if (GTK_IS_RADIO_MENU_ITEM (w))
3150 if (cur->button_type != BUTTON_TYPE_RADIO)
3151 break;
3152 xg_update_radio_item (cur, w);
3153 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3155 else if (GTK_IS_MENU_ITEM (w))
3157 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3158 GtkWidget *sub;
3160 if (cur->button_type != BUTTON_TYPE_NONE ||
3161 menu_separator_name_p (cur->name))
3162 break;
3164 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3166 sub = gtk_menu_item_get_submenu (witem);
3167 if (sub && ! cur->contents)
3169 /* Not a submenu anymore. */
3170 g_object_ref (G_OBJECT (sub));
3171 remove_submenu (witem);
3172 gtk_widget_destroy (sub);
3174 else if (cur->contents)
3176 GtkWidget *nsub;
3178 nsub = xg_update_submenu (sub, f, cur->contents,
3179 select_cb, deactivate_cb,
3180 highlight_cb, cl_data);
3182 /* If this item just became a submenu, we must set it. */
3183 if (nsub != sub)
3184 gtk_menu_item_set_submenu (witem, nsub);
3187 else
3189 /* Structural difference. Remove everything from here and down
3190 in SUBMENU. */
3191 break;
3195 /* Remove widgets from first structural change. */
3196 if (iter)
3198 /* If we are adding new menu items below, we must remove from
3199 first radio button so that radio groups become correct. */
3200 if (cur && first_radio) xg_destroy_widgets (first_radio);
3201 else xg_destroy_widgets (iter);
3204 if (cur)
3206 /* More items added. Create them. */
3207 newsub = create_menus (cur,
3209 select_cb,
3210 deactivate_cb,
3211 highlight_cb,
3214 submenu,
3215 cl_data,
3219 if (list) g_list_free (list);
3221 return newsub;
3224 /* Update the MENUBAR.
3225 F is the frame the menu bar belongs to.
3226 VAL describes the contents of the menu bar.
3227 If DEEP_P, rebuild all but the top level menu names in
3228 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3229 SELECT_CB is the callback to use when a menu item is selected.
3230 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3231 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3233 void
3234 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3235 widget_value *val, bool deep_p,
3236 GCallback select_cb, GCallback deactivate_cb,
3237 GCallback highlight_cb)
3239 xg_menu_cb_data *cl_data;
3240 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3242 if (! list) return;
3244 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3246 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3247 select_cb, deactivate_cb, highlight_cb, cl_data);
3249 if (deep_p)
3251 widget_value *cur;
3253 /* Update all sub menus.
3254 We must keep the submenus (GTK menu item widgets) since the
3255 X Window in the XEvent that activates the menu are those widgets. */
3257 /* Update cl_data, menu_item things in F may have changed. */
3258 update_cl_data (cl_data, f, highlight_cb);
3260 for (cur = val->contents; cur; cur = cur->next)
3262 GList *iter;
3263 GtkWidget *sub = 0;
3264 GtkWidget *newsub;
3265 GtkMenuItem *witem = 0;
3267 /* Find sub menu that corresponds to val and update it. */
3268 for (iter = list ; iter; iter = g_list_next (iter))
3270 witem = GTK_MENU_ITEM (iter->data);
3271 if (xg_item_label_same_p (witem, cur->name))
3273 sub = gtk_menu_item_get_submenu (witem);
3274 break;
3278 newsub = xg_update_submenu (sub,
3280 cur->contents,
3281 select_cb,
3282 deactivate_cb,
3283 highlight_cb,
3284 cl_data);
3285 /* sub may still be NULL. If we just updated non deep and added
3286 a new menu bar item, it has no sub menu yet. So we set the
3287 newly created sub menu under witem. */
3288 if (newsub != sub && witem != 0)
3290 xg_set_screen (newsub, f);
3291 gtk_menu_item_set_submenu (witem, newsub);
3296 g_list_free (list);
3297 gtk_widget_show_all (menubar);
3300 /* Callback called when the menu bar W is mapped.
3301 Used to find the height of the menu bar if we didn't get it
3302 after showing the widget. */
3304 static void
3305 menubar_map_cb (GtkWidget *w, gpointer user_data)
3307 GtkRequisition req;
3308 struct frame *f = user_data;
3309 gtk_widget_get_preferred_size (w, NULL, &req);
3310 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3312 FRAME_MENUBAR_HEIGHT (f) = req.height;
3313 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3317 /* Recompute all the widgets of frame F, when the menu bar has been
3318 changed. */
3320 void
3321 xg_update_frame_menubar (struct frame *f)
3323 struct x_output *x = f->output_data.x;
3324 GtkRequisition req;
3326 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3327 return;
3329 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3330 return; /* Already done this, happens for frames created invisible. */
3332 block_input ();
3334 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3335 FALSE, FALSE, 0);
3336 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3338 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3339 gtk_widget_show_all (x->menubar_widget);
3340 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3342 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3344 FRAME_MENUBAR_HEIGHT (f) = req.height;
3345 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3347 unblock_input ();
3350 /* Get rid of the menu bar of frame F, and free its storage.
3351 This is used when deleting a frame, and when turning off the menu bar. */
3353 void
3354 free_frame_menubar (struct frame *f)
3356 struct x_output *x = f->output_data.x;
3358 if (x->menubar_widget)
3360 block_input ();
3362 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3363 /* The menubar and its children shall be deleted when removed from
3364 the container. */
3365 x->menubar_widget = 0;
3366 FRAME_MENUBAR_HEIGHT (f) = 0;
3367 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3368 unblock_input ();
3372 bool
3373 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3375 struct x_output *x = f->output_data.x;
3376 GList *iter;
3377 GdkRectangle rec;
3378 GList *list;
3379 GdkDisplay *gdpy;
3380 GdkWindow *gw;
3381 GdkEvent gevent;
3382 GtkWidget *gwdesc;
3384 if (! x->menubar_widget) return 0;
3386 if (! (event->xbutton.x >= 0
3387 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3388 && event->xbutton.y >= 0
3389 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3390 && event->xbutton.same_screen))
3391 return 0;
3393 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3394 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3395 if (! gw) return 0;
3396 gevent.any.window = gw;
3397 gevent.any.type = GDK_NOTHING;
3398 gwdesc = gtk_get_event_widget (&gevent);
3399 if (! gwdesc) return 0;
3400 if (! GTK_IS_MENU_BAR (gwdesc)
3401 && ! GTK_IS_MENU_ITEM (gwdesc)
3402 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3403 return 0;
3405 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3406 if (! list) return 0;
3407 rec.x = event->xbutton.x;
3408 rec.y = event->xbutton.y;
3409 rec.width = 1;
3410 rec.height = 1;
3412 for (iter = list ; iter; iter = g_list_next (iter))
3414 GtkWidget *w = GTK_WIDGET (iter->data);
3415 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3416 break;
3418 g_list_free (list);
3419 return iter != 0;
3424 /***********************************************************************
3425 Scroll bar functions
3426 ***********************************************************************/
3429 /* Setting scroll bar values invokes the callback. Use this variable
3430 to indicate that callback should do nothing. */
3432 bool xg_ignore_gtk_scrollbar;
3434 /* Width and height of scroll bars for the current theme. */
3435 static int scroll_bar_width_for_theme;
3436 static int scroll_bar_height_for_theme;
3438 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3439 may be larger than 32 bits. Keep a mapping from integer index to widget
3440 pointers to get around the 32 bit limitation. */
3442 static struct
3444 GtkWidget **widgets;
3445 ptrdiff_t max_size;
3446 ptrdiff_t used;
3447 } id_to_widget;
3449 /* Grow this much every time we need to allocate more */
3451 #define ID_TO_WIDGET_INCR 32
3453 /* Store the widget pointer W in id_to_widget and return the integer index. */
3455 static ptrdiff_t
3456 xg_store_widget_in_map (GtkWidget *w)
3458 ptrdiff_t i;
3460 if (id_to_widget.max_size == id_to_widget.used)
3462 ptrdiff_t new_size;
3463 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3464 memory_full (SIZE_MAX);
3466 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3467 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3468 new_size, sizeof (GtkWidget *));
3470 for (i = id_to_widget.max_size; i < new_size; ++i)
3471 id_to_widget.widgets[i] = 0;
3472 id_to_widget.max_size = new_size;
3475 /* Just loop over the array and find a free place. After all,
3476 how many scroll bars are we creating? Should be a small number.
3477 The check above guarantees we will find a free place. */
3478 for (i = 0; i < id_to_widget.max_size; ++i)
3480 if (! id_to_widget.widgets[i])
3482 id_to_widget.widgets[i] = w;
3483 ++id_to_widget.used;
3485 return i;
3489 /* Should never end up here */
3490 emacs_abort ();
3493 /* Remove pointer at IDX from id_to_widget.
3494 Called when scroll bar is destroyed. */
3496 static void
3497 xg_remove_widget_from_map (ptrdiff_t idx)
3499 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3501 id_to_widget.widgets[idx] = 0;
3502 --id_to_widget.used;
3506 /* Get the widget pointer at IDX from id_to_widget. */
3508 static GtkWidget *
3509 xg_get_widget_from_map (ptrdiff_t idx)
3511 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3512 return id_to_widget.widgets[idx];
3514 return 0;
3517 static void
3518 update_theme_scrollbar_width (void)
3520 #ifdef HAVE_GTK3
3521 GtkAdjustment *vadj;
3522 #else
3523 GtkObject *vadj;
3524 #endif
3525 GtkWidget *wscroll;
3526 int w = 0, b = 0;
3528 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3529 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3530 g_object_ref_sink (G_OBJECT (wscroll));
3531 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3532 gtk_widget_destroy (wscroll);
3533 g_object_unref (G_OBJECT (wscroll));
3534 w += 2*b;
3535 #ifndef HAVE_GTK3
3536 if (w < 16) w = 16;
3537 #endif
3538 scroll_bar_width_for_theme = w;
3541 static void
3542 update_theme_scrollbar_height (void)
3544 #ifdef HAVE_GTK3
3545 GtkAdjustment *hadj;
3546 #else
3547 GtkObject *hadj;
3548 #endif
3549 GtkWidget *wscroll;
3550 int w = 0, b = 0;
3552 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3553 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3554 g_object_ref_sink (G_OBJECT (wscroll));
3555 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3556 gtk_widget_destroy (wscroll);
3557 g_object_unref (G_OBJECT (wscroll));
3558 w += 2*b;
3559 if (w < 12) w = 12;
3560 scroll_bar_height_for_theme = w;
3564 xg_get_default_scrollbar_width (void)
3566 return scroll_bar_width_for_theme * xg_get_gdk_scale ();
3570 xg_get_default_scrollbar_height (void)
3572 /* Apparently there's no default height for themes. */
3573 return scroll_bar_width_for_theme * xg_get_gdk_scale ();
3576 /* Return the scrollbar id for X Window WID on display DPY.
3577 Return -1 if WID not in id_to_widget. */
3579 ptrdiff_t
3580 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3582 ptrdiff_t idx;
3583 GtkWidget *w;
3585 w = xg_win_to_widget (dpy, wid);
3587 if (w)
3589 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3590 if (id_to_widget.widgets[idx] == w)
3591 return idx;
3594 return -1;
3597 /* Callback invoked when scroll bar WIDGET is destroyed.
3598 DATA is the index into id_to_widget for WIDGET.
3599 We free pointer to last scroll bar values here and remove the index. */
3601 static void
3602 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3604 intptr_t id = (intptr_t) data;
3605 xg_remove_widget_from_map (id);
3608 /* Create a scroll bar widget for frame F. Store the scroll bar
3609 in BAR.
3610 SCROLL_CALLBACK is the callback to invoke when the value of the
3611 bar changes.
3612 END_CALLBACK is the callback to invoke when scrolling ends.
3613 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3614 to set resources for the widget. */
3616 void
3617 xg_create_scroll_bar (struct frame *f,
3618 struct scroll_bar *bar,
3619 GCallback scroll_callback,
3620 GCallback end_callback,
3621 const char *scroll_bar_name)
3623 GtkWidget *wscroll;
3624 GtkWidget *webox;
3625 intptr_t scroll_id;
3626 #ifdef HAVE_GTK3
3627 GtkAdjustment *vadj;
3628 #else
3629 GtkObject *vadj;
3630 #endif
3632 /* Page, step increment values are not so important here, they
3633 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3634 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3635 0.1, 0.1, 0.1);
3637 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3638 webox = gtk_event_box_new ();
3639 gtk_widget_set_name (wscroll, scroll_bar_name);
3640 #ifndef HAVE_GTK3
3641 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3642 #endif
3643 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3645 scroll_id = xg_store_widget_in_map (wscroll);
3647 g_signal_connect (G_OBJECT (wscroll),
3648 "destroy",
3649 G_CALLBACK (xg_gtk_scroll_destroy),
3650 (gpointer) scroll_id);
3651 g_signal_connect (G_OBJECT (wscroll),
3652 "change-value",
3653 scroll_callback,
3654 (gpointer) bar);
3655 g_signal_connect (G_OBJECT (wscroll),
3656 "button-release-event",
3657 end_callback,
3658 (gpointer) bar);
3660 /* The scroll bar widget does not draw on a window of its own. Instead
3661 it draws on the parent window, in this case the edit widget. So
3662 whenever the edit widget is cleared, the scroll bar needs to redraw
3663 also, which causes flicker. Put an event box between the edit widget
3664 and the scroll bar, so the scroll bar instead draws itself on the
3665 event box window. */
3666 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3667 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3670 /* Set the cursor to an arrow. */
3671 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3673 bar->x_window = scroll_id;
3674 bar->horizontal = 0;
3677 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3678 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3679 of the bar changes. END_CALLBACK is the callback to invoke when
3680 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3681 bar. Can be used to set resources for the widget. */
3683 void
3684 xg_create_horizontal_scroll_bar (struct frame *f,
3685 struct scroll_bar *bar,
3686 GCallback scroll_callback,
3687 GCallback end_callback,
3688 const char *scroll_bar_name)
3690 GtkWidget *wscroll;
3691 GtkWidget *webox;
3692 intptr_t scroll_id;
3693 #ifdef HAVE_GTK3
3694 GtkAdjustment *hadj;
3695 #else
3696 GtkObject *hadj;
3697 #endif
3699 /* Page, step increment values are not so important here, they
3700 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3701 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3702 0.1, 0.1, 0.1);
3704 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3705 webox = gtk_event_box_new ();
3706 gtk_widget_set_name (wscroll, scroll_bar_name);
3707 #ifndef HAVE_GTK3
3708 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3709 #endif
3710 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3712 scroll_id = xg_store_widget_in_map (wscroll);
3714 g_signal_connect (G_OBJECT (wscroll),
3715 "destroy",
3716 G_CALLBACK (xg_gtk_scroll_destroy),
3717 (gpointer) scroll_id);
3718 g_signal_connect (G_OBJECT (wscroll),
3719 "change-value",
3720 scroll_callback,
3721 (gpointer) bar);
3722 g_signal_connect (G_OBJECT (wscroll),
3723 "button-release-event",
3724 end_callback,
3725 (gpointer) bar);
3727 /* The scroll bar widget does not draw on a window of its own. Instead
3728 it draws on the parent window, in this case the edit widget. So
3729 whenever the edit widget is cleared, the scroll bar needs to redraw
3730 also, which causes flicker. Put an event box between the edit widget
3731 and the scroll bar, so the scroll bar instead draws itself on the
3732 event box window. */
3733 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3734 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3737 /* Set the cursor to an arrow. */
3738 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3740 bar->x_window = scroll_id;
3741 bar->horizontal = 1;
3744 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3746 void
3747 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3749 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3750 if (w)
3752 GtkWidget *wparent = gtk_widget_get_parent (w);
3753 gtk_widget_destroy (w);
3754 gtk_widget_destroy (wparent);
3755 SET_FRAME_GARBAGED (f);
3759 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3760 in frame F.
3761 TOP/LEFT are the new pixel positions where the bar shall appear.
3762 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3764 void
3765 xg_update_scrollbar_pos (struct frame *f,
3766 ptrdiff_t scrollbar_id,
3767 int top,
3768 int left,
3769 int width,
3770 int height)
3772 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3773 if (wscroll)
3775 GtkWidget *wfixed = f->output_data.x->edit_widget;
3776 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3777 gint msl;
3778 int scale = xg_get_gdk_scale ();
3780 top /= scale;
3781 left /= scale;
3782 height /= scale;
3783 left -= (scale - 1) * ((width / scale) >> 1);
3785 /* Clear out old position. */
3786 int oldx = -1, oldy = -1, oldw, oldh;
3787 if (gtk_widget_get_parent (wparent) == wfixed)
3789 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3790 "x", &oldx, "y", &oldy, NULL);
3791 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3794 /* Move and resize to new values. */
3795 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3796 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3797 if (msl > height)
3799 /* No room. Hide scroll bar as some themes output a warning if
3800 the height is less than the min size. */
3801 gtk_widget_hide (wparent);
3802 gtk_widget_hide (wscroll);
3804 else
3806 gtk_widget_show_all (wparent);
3807 gtk_widget_set_size_request (wscroll, width, height);
3809 gtk_widget_queue_draw (wfixed);
3810 gdk_window_process_all_updates ();
3811 if (oldx != -1 && oldw > 0 && oldh > 0)
3813 /* Clear under old scroll bar position. This must be done after
3814 the gtk_widget_queue_draw and gdk_window_process_all_updates
3815 above. */
3816 oldw += (scale - 1) * oldw;
3817 oldx -= (scale - 1) * oldw;
3818 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3819 oldx, oldy, oldw, oldh);
3822 /* GTK does not redraw until the main loop is entered again, but
3823 if there are no X events pending we will not enter it. So we sync
3824 here to get some events. */
3826 x_sync (f);
3827 SET_FRAME_GARBAGED (f);
3828 cancel_mouse_face (f);
3833 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3834 in frame F.
3835 TOP/LEFT are the new pixel positions where the bar shall appear.
3836 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3838 void
3839 xg_update_horizontal_scrollbar_pos (struct frame *f,
3840 ptrdiff_t scrollbar_id,
3841 int top,
3842 int left,
3843 int width,
3844 int height)
3847 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3849 if (wscroll)
3851 GtkWidget *wfixed = f->output_data.x->edit_widget;
3852 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3853 gint msl;
3855 /* Clear out old position. */
3856 int oldx = -1, oldy = -1, oldw, oldh;
3857 if (gtk_widget_get_parent (wparent) == wfixed)
3859 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3860 "x", &oldx, "y", &oldy, NULL);
3861 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3864 /* Move and resize to new values. */
3865 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3866 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3867 if (msl > width)
3869 /* No room. Hide scroll bar as some themes output a warning if
3870 the width is less than the min size. */
3871 gtk_widget_hide (wparent);
3872 gtk_widget_hide (wscroll);
3874 else
3876 gtk_widget_show_all (wparent);
3877 gtk_widget_set_size_request (wscroll, width, height);
3879 gtk_widget_queue_draw (wfixed);
3880 gdk_window_process_all_updates ();
3881 if (oldx != -1 && oldw > 0 && oldh > 0)
3882 /* Clear under old scroll bar position. This must be done after
3883 the gtk_widget_queue_draw and gdk_window_process_all_updates
3884 above. */
3885 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3886 oldx, oldy, oldw, oldh);
3888 /* GTK does not redraw until the main loop is entered again, but
3889 if there are no X events pending we will not enter it. So we sync
3890 here to get some events. */
3892 x_sync (f);
3893 SET_FRAME_GARBAGED (f);
3894 cancel_mouse_face (f);
3899 /* Get the current value of the range, truncated to an integer. */
3901 static int
3902 int_gtk_range_get_value (GtkRange *range)
3904 return gtk_range_get_value (range);
3908 /* Set the thumb size and position of scroll bar BAR. We are currently
3909 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3911 void
3912 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3913 int portion,
3914 int position,
3915 int whole)
3917 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3919 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3921 if (wscroll && bar->dragging == -1)
3923 GtkAdjustment *adj;
3924 gdouble shown;
3925 gdouble top;
3926 int size, value;
3927 int old_size;
3928 int new_step;
3929 bool changed = 0;
3931 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3933 if (scroll_bar_adjust_thumb_portion_p)
3935 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3936 line rather than the real portion value. This makes the
3937 thumb less likely to resize and that looks better. */
3938 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3940 /* When the thumb is at the bottom, position == whole.
3941 So we need to increase `whole' to make space for the thumb. */
3942 whole += portion;
3945 if (whole <= 0)
3946 top = 0, shown = 1;
3947 else
3949 top = (gdouble) position / whole;
3950 shown = (gdouble) portion / whole;
3953 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3954 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3956 /* Assume all lines are of equal size. */
3957 new_step = size / max (1, FRAME_LINES (f));
3959 old_size = gtk_adjustment_get_page_size (adj);
3960 if (old_size != size)
3962 int old_step = gtk_adjustment_get_step_increment (adj);
3963 if (old_step != new_step)
3965 gtk_adjustment_set_page_size (adj, size);
3966 gtk_adjustment_set_step_increment (adj, new_step);
3967 /* Assume a page increment is about 95% of the page size */
3968 gtk_adjustment_set_page_increment (adj, size - size / 20);
3969 changed = 1;
3973 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3975 block_input ();
3977 /* gtk_range_set_value invokes the callback. Set
3978 ignore_gtk_scrollbar to make the callback do nothing */
3979 xg_ignore_gtk_scrollbar = 1;
3981 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3982 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3983 else if (changed)
3984 gtk_adjustment_changed (adj);
3986 xg_ignore_gtk_scrollbar = 0;
3988 unblock_input ();
3993 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3994 currently displaying PORTION out of a whole WHOLE, and our position
3995 POSITION. */
3996 void
3997 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3998 int portion,
3999 int position,
4000 int whole)
4002 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
4004 if (wscroll && bar->dragging == -1)
4006 GtkAdjustment *adj;
4007 int lower = 0;
4008 int upper = max (whole - 1, 0);
4009 int pagesize = min (upper, max (portion, 0));
4010 int value = max (0, min (position, upper - pagesize));
4011 /* These should be set to something more <portion, whole>
4012 related. */
4013 int page_increment = 4;
4014 int step_increment = 1;
4016 block_input ();
4017 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
4018 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
4019 (gdouble) upper, (gdouble) step_increment,
4020 (gdouble) page_increment, (gdouble) pagesize);
4021 gtk_adjustment_changed (adj);
4022 unblock_input ();
4026 /* Return true if EVENT is for a scroll bar in frame F.
4027 When the same X window is used for several Gtk+ widgets, we cannot
4028 say for sure based on the X window alone if an event is for the
4029 frame. This function does additional checks. */
4031 bool
4032 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
4034 bool retval = 0;
4036 if (f && event->type == ButtonPress && event->xbutton.button < 4)
4038 /* Check if press occurred outside the edit widget. */
4039 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
4040 GdkWindow *gwin;
4041 #ifdef HAVE_GTK3
4042 GdkDevice *gdev = gdk_device_manager_get_client_pointer
4043 (gdk_display_get_device_manager (gdpy));
4044 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
4045 #else
4046 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
4047 #endif
4048 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
4050 else if (f
4051 && ((event->type == ButtonRelease && event->xbutton.button < 4)
4052 || event->type == MotionNotify))
4054 /* If we are releasing or moving the scroll bar, it has the grab. */
4055 GtkWidget *w = gtk_grab_get_current ();
4056 retval = w != 0 && GTK_IS_SCROLLBAR (w);
4059 return retval;
4064 /***********************************************************************
4065 Tool bar functions
4066 ***********************************************************************/
4067 /* The key for the data we put in the GtkImage widgets. The data is
4068 the image used by Emacs. We use this to see if we need to update
4069 the GtkImage with a new image. */
4070 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
4072 /* The key for storing the latest modifiers so the activate callback can
4073 get them. */
4074 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4076 /* The key for the data we put in the GtkImage widgets. The data is
4077 the stock name used by Emacs. We use this to see if we need to update
4078 the GtkImage with a new image. */
4079 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4081 /* As above, but this is used for named theme widgets, as opposed to
4082 stock items. */
4083 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4085 /* Callback function invoked when a tool bar item is pressed.
4086 W is the button widget in the tool bar that got pressed,
4087 CLIENT_DATA is an integer that is the index of the button in the
4088 tool bar. 0 is the first button. */
4090 static gboolean
4091 xg_tool_bar_button_cb (GtkWidget *widget,
4092 GdkEventButton *event,
4093 gpointer user_data)
4095 intptr_t state = event->state;
4096 gpointer ptr = (gpointer) state;
4097 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4098 return FALSE;
4102 /* Callback function invoked when a tool bar item is pressed.
4103 W is the button widget in the tool bar that got pressed,
4104 CLIENT_DATA is an integer that is the index of the button in the
4105 tool bar. 0 is the first button. */
4107 static void
4108 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4110 intptr_t idx = (intptr_t) client_data;
4111 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4112 intptr_t mod = (intptr_t) gmod;
4114 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4115 Lisp_Object key, frame;
4116 struct input_event event;
4117 EVENT_INIT (event);
4119 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4120 return;
4122 idx *= TOOL_BAR_ITEM_NSLOTS;
4124 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4125 XSETFRAME (frame, f);
4127 /* We generate two events here. The first one is to set the prefix
4128 to `(tool_bar)', see keyboard.c. */
4129 event.kind = TOOL_BAR_EVENT;
4130 event.frame_or_window = frame;
4131 event.arg = frame;
4132 kbd_buffer_store_event (&event);
4134 event.kind = TOOL_BAR_EVENT;
4135 event.frame_or_window = frame;
4136 event.arg = key;
4137 /* Convert between the modifier bits GDK uses and the modifier bits
4138 Emacs uses. This assumes GDK and X masks are the same, which they are when
4139 this is written. */
4140 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4141 kbd_buffer_store_event (&event);
4143 /* Return focus to the frame after we have clicked on a detached
4144 tool bar button. */
4145 x_focus_frame (f);
4148 static GtkWidget *
4149 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4151 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4152 GtkWidget *c1 = clist->data;
4153 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4155 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4156 g_list_free (clist);
4157 return GTK_IS_LABEL (c1) ? c1 : c2;
4161 /* This callback is called when the mouse enters or leaves a tool bar item.
4162 It is used for displaying and hiding the help text.
4163 W is the tool bar item, a button.
4164 EVENT is either an enter event or leave event.
4165 CLIENT_DATA is an integer that is the index of the button in the
4166 tool bar. 0 is the first button.
4168 Returns FALSE to tell GTK to keep processing this event. */
4170 static gboolean
4171 xg_tool_bar_help_callback (GtkWidget *w,
4172 GdkEventCrossing *event,
4173 gpointer client_data)
4175 intptr_t idx = (intptr_t) client_data;
4176 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4177 Lisp_Object help, frame;
4179 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4180 return FALSE;
4182 if (event->type == GDK_ENTER_NOTIFY)
4184 idx *= TOOL_BAR_ITEM_NSLOTS;
4185 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4187 if (NILP (help))
4188 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4190 else
4191 help = Qnil;
4193 XSETFRAME (frame, f);
4194 kbd_buffer_store_help_event (frame, help);
4196 return FALSE;
4200 /* This callback is called when a tool bar item shall be redrawn.
4201 It modifies the expose event so that the GtkImage widget redraws the
4202 whole image. This to overcome a bug that makes GtkImage draw the image
4203 in the wrong place when it tries to redraw just a part of the image.
4204 W is the GtkImage to be redrawn.
4205 EVENT is the expose event for W.
4206 CLIENT_DATA is unused.
4208 Returns FALSE to tell GTK to keep processing this event. */
4210 #ifndef HAVE_GTK3
4211 static gboolean
4212 xg_tool_bar_item_expose_callback (GtkWidget *w,
4213 GdkEventExpose *event,
4214 gpointer client_data)
4216 gint width, height;
4218 gdk_drawable_get_size (event->window, &width, &height);
4219 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4220 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4222 event->area.x = max (0, event->area.x);
4223 event->area.y = max (0, event->area.y);
4225 event->area.width = max (width, event->area.width);
4226 event->area.height = max (height, event->area.height);
4228 return FALSE;
4230 #endif
4232 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4233 #define toolbar_set_orientation(w, o) \
4234 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4235 #else
4236 #define toolbar_set_orientation(w, o) \
4237 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4238 #endif
4240 /* Attach a tool bar to frame F. */
4242 static void
4243 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4245 struct x_output *x = f->output_data.x;
4246 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4247 GtkWidget *top_widget = x->toolbar_widget;
4249 toolbar_set_orientation (x->toolbar_widget,
4250 into_hbox
4251 ? GTK_ORIENTATION_VERTICAL
4252 : GTK_ORIENTATION_HORIZONTAL);
4254 if (into_hbox)
4256 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4257 FALSE, FALSE, 0);
4259 if (EQ (pos, Qleft))
4260 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4261 top_widget,
4263 x->toolbar_in_hbox = true;
4265 else
4267 bool vbox_pos = x->menubar_widget != 0;
4268 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4269 FALSE, FALSE, 0);
4271 if (EQ (pos, Qtop))
4272 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4273 top_widget,
4274 vbox_pos);
4275 x->toolbar_in_hbox = false;
4277 x->toolbar_is_packed = true;
4280 static bool xg_update_tool_bar_sizes (struct frame *f);
4282 static void
4283 tb_size_cb (GtkWidget *widget,
4284 GdkRectangle *allocation,
4285 gpointer user_data)
4287 /* When tool bar is created it has one preferred size. But when size is
4288 allocated between widgets, it may get another. So we must update
4289 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4290 struct frame *f = user_data;
4292 if (xg_update_tool_bar_sizes (f))
4294 frame_size_history_add (f, Qtb_size_cb, 0, 0, Qnil);
4295 adjust_frame_size (f, -1, -1, 5, 0, Qtool_bar_lines);
4299 /* Create a tool bar for frame F. */
4301 static void
4302 xg_create_tool_bar (struct frame *f)
4304 struct x_output *x = f->output_data.x;
4305 #if GTK_CHECK_VERSION (3, 3, 6)
4306 GtkStyleContext *gsty;
4307 #endif
4308 struct xg_frame_tb_info *tbinfo
4309 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4310 TB_INFO_KEY);
4311 if (! tbinfo)
4313 tbinfo = xmalloc (sizeof (*tbinfo));
4314 tbinfo->last_tool_bar = Qnil;
4315 tbinfo->style = Qnil;
4316 tbinfo->hmargin = tbinfo->vmargin = 0;
4317 tbinfo->dir = GTK_TEXT_DIR_NONE;
4318 tbinfo->n_last_items = 0;
4319 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4320 TB_INFO_KEY,
4321 tbinfo);
4324 x->toolbar_widget = gtk_toolbar_new ();
4326 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4328 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4329 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4330 g_signal_connect (x->toolbar_widget, "size-allocate",
4331 G_CALLBACK (tb_size_cb), f);
4332 #if GTK_CHECK_VERSION (3, 3, 6)
4333 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4334 gtk_style_context_add_class (gsty, "primary-toolbar");
4335 #endif
4339 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4341 /* Find the right-to-left image named by RTL in the tool bar images for F.
4342 Returns IMAGE if RTL is not found. */
4344 static Lisp_Object
4345 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4347 int i;
4348 Lisp_Object file, rtl_name;
4349 struct gcpro gcpro1, gcpro2;
4350 GCPRO2 (file, rtl_name);
4352 rtl_name = Ffile_name_nondirectory (rtl);
4354 for (i = 0; i < f->n_tool_bar_items; ++i)
4356 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4357 if (!NILP (file = file_for_image (rtl_image)))
4359 file = call1 (intern ("file-name-sans-extension"),
4360 Ffile_name_nondirectory (file));
4361 if (! NILP (Fequal (file, rtl_name)))
4363 image = rtl_image;
4364 break;
4369 return image;
4372 static GtkToolItem *
4373 xg_make_tool_item (struct frame *f,
4374 GtkWidget *wimage,
4375 GtkWidget **wbutton,
4376 const char *label,
4377 int i, bool horiz, bool text_image)
4379 GtkToolItem *ti = gtk_tool_item_new ();
4380 GtkWidget *vb = gtk_box_new (horiz
4381 ? GTK_ORIENTATION_HORIZONTAL
4382 : GTK_ORIENTATION_VERTICAL,
4384 GtkWidget *wb = gtk_button_new ();
4385 /* The eventbox is here so we can have tooltips on disabled items. */
4386 GtkWidget *weventbox = gtk_event_box_new ();
4387 #if GTK_CHECK_VERSION (3, 3, 6)
4388 GtkCssProvider *css_prov = gtk_css_provider_new ();
4389 GtkStyleContext *gsty;
4391 gtk_css_provider_load_from_data (css_prov,
4392 "GtkEventBox {"
4393 " background-color: transparent;"
4394 "}",
4395 -1, NULL);
4397 gsty = gtk_widget_get_style_context (weventbox);
4398 gtk_style_context_add_provider (gsty,
4399 GTK_STYLE_PROVIDER (css_prov),
4400 GTK_STYLE_PROVIDER_PRIORITY_USER);
4401 g_object_unref (css_prov);
4402 #endif
4404 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4406 if (wimage && !text_image)
4407 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4408 if (label)
4409 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4410 if (wimage && text_image)
4411 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4413 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4414 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4415 gtk_container_add (GTK_CONTAINER (wb), vb);
4416 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4417 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4419 if (wimage || label)
4421 intptr_t ii = i;
4422 gpointer gi = (gpointer) ii;
4424 g_signal_connect (G_OBJECT (wb), "clicked",
4425 G_CALLBACK (xg_tool_bar_callback),
4426 gi);
4428 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4430 #ifndef HAVE_GTK3
4431 /* Catch expose events to overcome an annoying redraw bug, see
4432 comment for xg_tool_bar_item_expose_callback. */
4433 g_signal_connect (G_OBJECT (ti),
4434 "expose-event",
4435 G_CALLBACK (xg_tool_bar_item_expose_callback),
4437 #endif
4438 gtk_tool_item_set_homogeneous (ti, FALSE);
4440 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4441 no distinction based on modifiers in the activate callback,
4442 so we have to do it ourselves. */
4443 g_signal_connect (wb, "button-release-event",
4444 G_CALLBACK (xg_tool_bar_button_cb),
4445 NULL);
4447 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4449 /* Use enter/leave notify to show help. We use the events
4450 rather than the GtkButton specific signals "enter" and
4451 "leave", so we can have only one callback. The event
4452 will tell us what kind of event it is. */
4453 g_signal_connect (G_OBJECT (weventbox),
4454 "enter-notify-event",
4455 G_CALLBACK (xg_tool_bar_help_callback),
4456 gi);
4457 g_signal_connect (G_OBJECT (weventbox),
4458 "leave-notify-event",
4459 G_CALLBACK (xg_tool_bar_help_callback),
4460 gi);
4463 if (wbutton) *wbutton = wb;
4465 return ti;
4468 static bool
4469 is_box_type (GtkWidget *vb, bool is_horizontal)
4471 #ifdef HAVE_GTK3
4472 bool ret = 0;
4473 if (GTK_IS_BOX (vb))
4475 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4476 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4477 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4479 return ret;
4480 #else
4481 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4482 #endif
4486 static bool
4487 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4488 const char *icon_name, const struct image *img,
4489 const char *label, bool horiz)
4491 gpointer old;
4492 GtkWidget *wimage;
4493 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4494 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4496 /* Check if the tool icon matches. */
4497 if (stock_name && wimage)
4499 old = g_object_get_data (G_OBJECT (wimage),
4500 XG_TOOL_BAR_STOCK_NAME);
4501 if (!old || strcmp (old, stock_name))
4502 return 1;
4504 else if (icon_name && wimage)
4506 old = g_object_get_data (G_OBJECT (wimage),
4507 XG_TOOL_BAR_ICON_NAME);
4508 if (!old || strcmp (old, icon_name))
4509 return 1;
4511 else if (wimage)
4513 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4514 XG_TOOL_BAR_IMAGE_DATA);
4515 Pixmap old_img = (Pixmap) gold_img;
4516 if (old_img != img->pixmap)
4517 return 1;
4520 /* Check button configuration and label. */
4521 if (is_box_type (vb, horiz)
4522 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4523 return 1;
4525 /* Ensure label is correct. */
4526 if (label && wlbl)
4527 gtk_label_set_text (GTK_LABEL (wlbl), label);
4528 return 0;
4531 static bool
4532 xg_update_tool_bar_sizes (struct frame *f)
4534 struct x_output *x = f->output_data.x;
4535 GtkRequisition req;
4536 int nl = 0, nr = 0, nt = 0, nb = 0;
4537 GtkWidget *top_widget = x->toolbar_widget;
4539 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4540 if (x->toolbar_in_hbox)
4542 int pos;
4543 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4544 top_widget,
4545 "position", &pos, NULL);
4546 if (pos == 0) nl = req.width;
4547 else nr = req.width;
4549 else
4551 int pos;
4552 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4553 top_widget,
4554 "position", &pos, NULL);
4555 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4556 else nb = req.height;
4559 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4560 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4561 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4562 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4564 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4565 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4566 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4567 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4568 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4569 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4571 return true;
4573 else
4574 return false;
4577 static char *
4578 find_icon_from_name (char *name,
4579 GtkIconTheme *icon_theme,
4580 char **icon_name)
4582 #if ! GTK_CHECK_VERSION (3, 10, 0)
4583 GtkStockItem stock_item;
4584 #endif
4586 if (name[0] == 'n' && name[1] == ':')
4588 *icon_name = name + 2;
4589 name = NULL;
4591 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4592 *icon_name = NULL;
4595 #if ! GTK_CHECK_VERSION (3, 10, 0)
4596 else if (gtk_stock_lookup (name, &stock_item))
4597 *icon_name = NULL;
4598 #endif
4599 else if (gtk_icon_theme_has_icon (icon_theme, name))
4601 *icon_name = name;
4602 name = NULL;
4604 else
4606 name = NULL;
4607 *icon_name = NULL;
4610 return name;
4614 /* Update the tool bar for frame F. Add new buttons and remove old. */
4616 void
4617 update_frame_tool_bar (struct frame *f)
4619 int i, j;
4620 struct x_output *x = f->output_data.x;
4621 int hmargin = 0, vmargin = 0;
4622 GtkToolbar *wtoolbar;
4623 GtkToolItem *ti;
4624 GtkTextDirection dir;
4625 Lisp_Object style;
4626 bool text_image, horiz;
4627 struct xg_frame_tb_info *tbinfo;
4628 GdkScreen *screen;
4629 GtkIconTheme *icon_theme;
4632 if (! FRAME_GTK_WIDGET (f))
4633 return;
4635 block_input ();
4637 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4639 hmargin = XFASTINT (Vtool_bar_button_margin);
4640 vmargin = XFASTINT (Vtool_bar_button_margin);
4642 else if (CONSP (Vtool_bar_button_margin))
4644 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4645 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4647 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4648 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4651 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4652 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4653 i.e. zero. This means that margins less than
4654 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4655 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4656 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4658 if (! x->toolbar_widget)
4659 xg_create_tool_bar (f);
4661 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4662 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4664 style = Ftool_bar_get_system_style ();
4665 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4666 icon_theme = gtk_icon_theme_get_for_screen (screen);
4668 /* Are we up to date? */
4669 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4670 TB_INFO_KEY);
4672 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4673 && tbinfo->n_last_items == f->n_tool_bar_items
4674 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4675 && tbinfo->dir == dir
4676 && ! NILP (Fequal (tbinfo->style, style))
4677 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4679 unblock_input ();
4680 return;
4683 tbinfo->last_tool_bar = f->tool_bar_items;
4684 tbinfo->n_last_items = f->n_tool_bar_items;
4685 tbinfo->style = style;
4686 tbinfo->hmargin = hmargin;
4687 tbinfo->vmargin = vmargin;
4688 tbinfo->dir = dir;
4690 text_image = EQ (style, Qtext_image_horiz);
4691 horiz = EQ (style, Qboth_horiz) || text_image;
4693 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4695 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4696 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4697 int idx;
4698 ptrdiff_t img_id;
4699 int icon_size = 0;
4700 struct image *img = NULL;
4701 Lisp_Object image;
4702 Lisp_Object stock = Qnil;
4703 char *stock_name = NULL;
4704 char *icon_name = NULL;
4705 Lisp_Object rtl;
4706 GtkWidget *wbutton = NULL;
4707 Lisp_Object specified_file;
4708 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4709 const char *label
4710 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4711 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4712 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4713 : "";
4715 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4717 /* If this is a separator, use a gtk separator item. */
4718 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4720 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4722 if (ti)
4723 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4724 GTK_WIDGET (ti));
4725 ti = gtk_separator_tool_item_new ();
4726 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4728 j++;
4729 continue;
4732 /* Otherwise, the tool-bar item is an ordinary button. */
4734 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4736 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4737 ti = NULL;
4740 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4742 /* Ignore invalid image specifications. */
4743 image = PROP (TOOL_BAR_ITEM_IMAGES);
4744 if (!valid_image_p (image))
4746 if (ti)
4747 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4748 GTK_WIDGET (ti));
4749 continue;
4752 specified_file = file_for_image (image);
4753 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4754 stock = call1 (Qx_gtk_map_stock, specified_file);
4756 if (CONSP (stock))
4758 Lisp_Object tem;
4759 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4760 if (! NILP (tem) && STRINGP (XCAR (tem)))
4762 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4763 icon_theme,
4764 &icon_name);
4765 if (stock_name || icon_name) break;
4768 else if (STRINGP (stock))
4770 stock_name = find_icon_from_name (SSDATA (stock),
4771 icon_theme,
4772 &icon_name);
4775 if (stock_name || icon_name)
4776 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4778 if (stock_name == NULL && icon_name == NULL)
4780 /* No stock image, or stock item not known. Try regular
4781 image. If image is a vector, choose it according to the
4782 button state. */
4783 if (dir == GTK_TEXT_DIR_RTL
4784 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4785 && STRINGP (rtl))
4786 image = find_rtl_image (f, image, rtl);
4788 if (VECTORP (image))
4790 if (enabled_p)
4791 idx = (selected_p
4792 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4793 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4794 else
4795 idx = (selected_p
4796 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4797 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4799 eassert (ASIZE (image) >= idx);
4800 image = AREF (image, idx);
4802 else
4803 idx = -1;
4805 img_id = lookup_image (f, image);
4806 img = IMAGE_FROM_ID (f, img_id);
4807 prepare_image_for_display (f, img);
4809 if (img->load_failed_p || img->pixmap == None)
4811 if (ti)
4812 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4813 GTK_WIDGET (ti));
4814 continue;
4818 /* If there is an existing widget, check if it's stale; if so,
4819 remove it and make a new tool item from scratch. */
4820 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4821 img, label, horiz))
4823 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4824 GTK_WIDGET (ti));
4825 ti = NULL;
4828 if (ti == NULL)
4830 GtkWidget *w;
4832 /* Save the image so we can see if an update is needed the
4833 next time we call xg_tool_item_match_p. */
4834 if (EQ (style, Qtext))
4835 w = NULL;
4836 else if (stock_name)
4839 #if GTK_CHECK_VERSION (3, 10, 0)
4840 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4841 #else
4842 w = gtk_image_new_from_stock (stock_name, icon_size);
4843 #endif
4844 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4845 (gpointer) xstrdup (stock_name),
4846 (GDestroyNotify) xfree);
4848 else if (icon_name)
4850 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4851 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4852 (gpointer) xstrdup (icon_name),
4853 (GDestroyNotify) xfree);
4855 else
4857 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4858 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4859 (gpointer)img->pixmap);
4862 #if GTK_CHECK_VERSION (3, 14, 0)
4863 if (w)
4865 gtk_widget_set_margin_start (w, hmargin);
4866 gtk_widget_set_margin_end (w, hmargin);
4867 gtk_widget_set_margin_top (w, vmargin);
4868 gtk_widget_set_margin_bottom (w, vmargin);
4870 #else
4871 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4872 #endif
4873 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4874 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4877 #undef PROP
4879 gtk_widget_set_sensitive (wbutton, enabled_p);
4880 j++;
4883 /* Remove buttons not longer needed. */
4886 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4887 if (ti)
4888 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4889 } while (ti != NULL);
4891 if (f->n_tool_bar_items != 0)
4893 if (! x->toolbar_is_packed)
4894 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4895 gtk_widget_show_all (x->toolbar_widget);
4896 if (xg_update_tool_bar_sizes (f))
4898 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
4899 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4903 unblock_input ();
4906 /* Deallocate all resources for the tool bar on frame F.
4907 Remove the tool bar. */
4909 void
4910 free_frame_tool_bar (struct frame *f)
4912 struct x_output *x = f->output_data.x;
4914 if (x->toolbar_widget)
4916 struct xg_frame_tb_info *tbinfo;
4917 GtkWidget *top_widget = x->toolbar_widget;
4919 block_input ();
4920 if (x->toolbar_is_packed)
4922 if (x->toolbar_in_hbox)
4923 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4924 top_widget);
4925 else
4926 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4927 top_widget);
4929 else
4930 gtk_widget_destroy (x->toolbar_widget);
4932 x->toolbar_widget = 0;
4933 x->toolbar_widget = 0;
4934 x->toolbar_is_packed = false;
4935 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4936 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4938 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4939 TB_INFO_KEY);
4940 if (tbinfo)
4942 xfree (tbinfo);
4943 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4944 TB_INFO_KEY,
4945 NULL);
4948 frame_size_history_add (f, Qfree_frame_tool_bar, 0, 0, Qnil);
4949 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4951 unblock_input ();
4955 void
4956 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4958 struct x_output *x = f->output_data.x;
4959 GtkWidget *top_widget = x->toolbar_widget;
4961 if (! x->toolbar_widget || ! top_widget)
4962 return;
4964 block_input ();
4965 g_object_ref (top_widget);
4966 if (x->toolbar_is_packed)
4968 if (x->toolbar_in_hbox)
4969 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4970 top_widget);
4971 else
4972 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4973 top_widget);
4976 xg_pack_tool_bar (f, pos);
4977 g_object_unref (top_widget);
4979 if (xg_update_tool_bar_sizes (f))
4981 frame_size_history_add (f, Qxg_change_toolbar_position, 0, 0, Qnil);
4982 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4986 unblock_input ();
4991 /***********************************************************************
4992 Initializing
4993 ***********************************************************************/
4994 void
4995 xg_initialize (void)
4997 GtkBindingSet *binding_set;
4998 GtkSettings *settings;
5000 #if HAVE_XFT
5001 /* Work around a bug with corrupted data if libXft gets unloaded. This way
5002 we keep it permanently linked in. */
5003 XftInit (0);
5004 #endif
5006 gdpy_def = NULL;
5007 xg_ignore_gtk_scrollbar = 0;
5008 xg_menu_cb_list.prev = xg_menu_cb_list.next =
5009 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
5011 id_to_widget.max_size = id_to_widget.used = 0;
5012 id_to_widget.widgets = 0;
5014 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
5015 (gdk_display_get_default ()));
5016 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
5017 bindings. It doesn't seem to be any way to remove properties,
5018 so we set it to "" which in means "no key". */
5019 gtk_settings_set_string_property (settings,
5020 "gtk-menu-bar-accel",
5022 EMACS_CLASS);
5024 /* Make GTK text input widgets use Emacs style keybindings. This is
5025 Emacs after all. */
5026 gtk_settings_set_string_property (settings,
5027 "gtk-key-theme-name",
5028 "Emacs",
5029 EMACS_CLASS);
5031 /* Make dialogs close on C-g. Since file dialog inherits from
5032 dialog, this works for them also. */
5033 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
5034 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5035 "close", 0);
5037 /* Make menus close on C-g. */
5038 binding_set = gtk_binding_set_by_class (g_type_class_ref
5039 (GTK_TYPE_MENU_SHELL));
5040 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5041 "cancel", 0);
5042 update_theme_scrollbar_width ();
5043 update_theme_scrollbar_height ();
5045 #ifdef HAVE_FREETYPE
5046 x_last_font_name = NULL;
5047 #endif
5050 #endif /* USE_GTK */