Minor tweaks to frame_size_history_add
[emacs.git] / src / gtkutil.c
blob6f1707894c116ecb4d8fe7a1beb4cc946b586f8e
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 gdpy = gdk_display_open (display_name);
191 if (!gdpy_def && gdpy)
193 gdpy_def = gdpy;
194 gdk_display_manager_set_default_display (gdk_display_manager_get (),
195 gdpy);
198 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
202 /* Close display DPY. */
204 void
205 xg_display_close (Display *dpy)
207 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
209 /* If this is the default display, try to change it before closing.
210 If there is no other display to use, gdpy_def is set to NULL, and
211 the next call to xg_display_open resets the default display. */
212 if (gdk_display_get_default () == gdpy)
214 struct x_display_info *dpyinfo;
215 GdkDisplay *gdpy_new = NULL;
217 /* Find another display. */
218 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
219 if (dpyinfo->display != dpy)
221 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
222 gdk_display_manager_set_default_display (gdk_display_manager_get (),
223 gdpy_new);
224 break;
226 gdpy_def = gdpy_new;
229 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
230 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
231 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
232 can continue running, but there will be memory leaks. */
233 g_object_run_dispose (G_OBJECT (gdpy));
234 #else
235 /* This seems to be fixed in GTK 2.10. */
236 gdk_display_close (gdpy);
237 #endif
241 /***********************************************************************
242 Utility functions
243 ***********************************************************************/
245 /* Create and return the cursor to be used for popup menus and
246 scroll bars on display DPY. */
248 GdkCursor *
249 xg_create_default_cursor (Display *dpy)
251 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
252 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
255 static GdkPixbuf *
256 xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
258 int iunused;
259 GdkPixbuf *tmp_buf;
260 Window wunused;
261 unsigned int width, height, uunused;
262 XImage *xim;
264 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
265 &width, &height, &uunused, &uunused);
267 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
268 ~0, XYPixmap);
269 if (!xim) return 0;
271 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
272 GDK_COLORSPACE_RGB,
273 FALSE,
274 xim->bitmap_unit,
275 width,
276 height,
277 xim->bytes_per_line,
278 NULL,
279 NULL);
280 XDestroyImage (xim);
281 return tmp_buf;
284 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
286 static GdkPixbuf *
287 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
288 Pixmap pix,
289 Pixmap mask)
291 int width, height;
292 GdkPixbuf *icon_buf, *tmp_buf;
294 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
295 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
296 g_object_unref (G_OBJECT (tmp_buf));
298 width = gdk_pixbuf_get_width (icon_buf);
299 height = gdk_pixbuf_get_height (icon_buf);
301 if (mask)
303 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
304 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
305 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
306 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
307 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
308 int y;
310 for (y = 0; y < height; ++y)
312 guchar *iconptr, *maskptr;
313 int x;
315 iconptr = pixels + y * rowstride;
316 maskptr = mask_pixels + y * mask_rowstride;
318 for (x = 0; x < width; ++x)
320 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
321 just R is sufficient. */
322 if (maskptr[0] == 0)
323 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
325 iconptr += rowstride/width;
326 maskptr += mask_rowstride/width;
330 g_object_unref (G_OBJECT (mask_buf));
333 return icon_buf;
336 static Lisp_Object
337 file_for_image (Lisp_Object image)
339 Lisp_Object specified_file = Qnil;
340 Lisp_Object tail;
342 for (tail = XCDR (image);
343 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
344 tail = XCDR (XCDR (tail)))
345 if (EQ (XCAR (tail), QCfile))
346 specified_file = XCAR (XCDR (tail));
348 return specified_file;
351 /* For the image defined in IMG, make and return a GtkImage. For displays with
352 8 planes or less we must make a GdkPixbuf and apply the mask manually.
353 Otherwise the highlighting and dimming the tool bar code in GTK does
354 will look bad. For display with more than 8 planes we just use the
355 pixmap and mask directly. For monochrome displays, GTK doesn't seem
356 able to use external pixmaps, it looks bad whatever we do.
357 The image is defined on the display where frame F is.
358 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
359 If OLD_WIDGET is NULL, a new widget is constructed and returned.
360 If OLD_WIDGET is not NULL, that widget is modified. */
362 static GtkWidget *
363 xg_get_image_for_pixmap (struct frame *f,
364 struct image *img,
365 GtkWidget *widget,
366 GtkImage *old_widget)
368 GdkPixbuf *icon_buf;
370 /* If we have a file, let GTK do all the image handling.
371 This seems to be the only way to make insensitive and activated icons
372 look good in all cases. */
373 Lisp_Object specified_file = file_for_image (img->spec);
374 Lisp_Object file;
376 /* We already loaded the image once before calling this
377 function, so this only fails if the image file has been removed.
378 In that case, use the pixmap already loaded. */
380 if (STRINGP (specified_file)
381 && STRINGP (file = x_find_image_file (specified_file)))
383 if (! old_widget)
384 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
385 else
386 gtk_image_set_from_file (old_widget, SSDATA (file));
388 return GTK_WIDGET (old_widget);
391 /* No file, do the image handling ourselves. This will look very bad
392 on a monochrome display, and sometimes bad on all displays with
393 certain themes. */
395 /* This is a workaround to make icons look good on pseudo color
396 displays. Apparently GTK expects the images to have an alpha
397 channel. If they don't, insensitive and activated icons will
398 look bad. This workaround does not work on monochrome displays,
399 and is strictly not needed on true color/static color displays (i.e.
400 16 bits and higher). But we do it anyway so we get a pixbuf that is
401 not associated with the img->pixmap. The img->pixmap may be removed
402 by clearing the image cache and then the tool bar redraw fails, since
403 Gtk+ assumes the pixmap is always there. */
404 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
406 if (icon_buf)
408 if (! old_widget)
409 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
410 else
411 gtk_image_set_from_pixbuf (old_widget, icon_buf);
413 g_object_unref (G_OBJECT (icon_buf));
416 return GTK_WIDGET (old_widget);
420 /* Set CURSOR on W and all widgets W contain. We must do like this
421 for scroll bars and menu because they create widgets internally,
422 and it is those widgets that are visible. */
424 static void
425 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
427 GdkWindow *window = gtk_widget_get_window (w);
428 GList *children = gdk_window_peek_children (window);
430 gdk_window_set_cursor (window, cursor);
432 /* The scroll bar widget has more than one GDK window (had to look at
433 the source to figure this out), and there is no way to set cursor
434 on widgets in GTK. So we must set the cursor for all GDK windows.
435 Ditto for menus. */
437 for ( ; children; children = g_list_next (children))
438 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
441 /* Insert NODE into linked LIST. */
443 static void
444 xg_list_insert (xg_list_node *list, xg_list_node *node)
446 xg_list_node *list_start = list->next;
448 if (list_start) list_start->prev = node;
449 node->next = list_start;
450 node->prev = 0;
451 list->next = node;
454 /* Remove NODE from linked LIST. */
456 static void
457 xg_list_remove (xg_list_node *list, xg_list_node *node)
459 xg_list_node *list_start = list->next;
460 if (node == list_start)
462 list->next = node->next;
463 if (list->next) list->next->prev = 0;
465 else
467 node->prev->next = node->next;
468 if (node->next) node->next->prev = node->prev;
472 /* Allocate and return a utf8 version of STR. If STR is already
473 utf8 or NULL, just return a copy of STR.
474 A new string is allocated and the caller must free the result
475 with g_free. */
477 static char *
478 get_utf8_string (const char *str)
480 char *utf8_str;
482 if (!str) return NULL;
484 /* If not UTF-8, try current locale. */
485 if (!g_utf8_validate (str, -1, NULL))
486 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
487 else
488 return g_strdup (str);
490 if (!utf8_str)
492 /* Probably some control characters in str. Escape them. */
493 ptrdiff_t len;
494 ptrdiff_t nr_bad = 0;
495 gsize bytes_read;
496 gsize bytes_written;
497 unsigned char *p = (unsigned char *)str;
498 char *cp, *up;
499 GError *err = NULL;
501 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
502 &bytes_written, &err))
503 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
505 ++nr_bad;
506 p += bytes_written+1;
507 g_error_free (err);
508 err = NULL;
511 if (err)
513 g_error_free (err);
514 err = NULL;
516 if (cp) g_free (cp);
518 len = strlen (str);
519 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
520 memory_full (SIZE_MAX);
521 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
522 p = (unsigned char *)str;
524 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
525 &bytes_written, &err))
526 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
528 memcpy (up, p, bytes_written);
529 up += bytes_written;
530 up += sprintf (up, "\\%03o", p[bytes_written]);
531 p += bytes_written + 1;
532 g_error_free (err);
533 err = NULL;
536 if (cp)
538 strcpy (up, cp);
539 g_free (cp);
541 if (err)
543 g_error_free (err);
544 err = NULL;
547 return utf8_str;
550 /* Check for special colors used in face spec for region face.
551 The colors are fetched from the Gtk+ theme.
552 Return true if color was found, false if not. */
554 bool
555 xg_check_special_colors (struct frame *f,
556 const char *color_name,
557 XColor *color)
559 bool success_p = 0;
560 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
561 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
563 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
564 return success_p;
566 block_input ();
568 #ifdef HAVE_GTK3
569 GtkStyleContext *gsty
570 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
571 GdkRGBA col;
572 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
573 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
574 if (get_fg)
575 gtk_style_context_get_color (gsty, state, &col);
576 else
577 gtk_style_context_get_background_color (gsty, state, &col);
579 sprintf (buf, "rgb:%04x/%04x/%04x",
580 (int)(col.red * 65535),
581 (int)(col.green * 65535),
582 (int)(col.blue * 65535));
583 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
584 buf, color)
585 != 0);
586 #else
587 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
588 GdkColor *grgb = get_bg
589 ? &gsty->bg[GTK_STATE_SELECTED]
590 : &gsty->fg[GTK_STATE_SELECTED];
592 color->red = grgb->red;
593 color->green = grgb->green;
594 color->blue = grgb->blue;
595 color->pixel = grgb->pixel;
596 success_p = 1;
597 #endif
600 unblock_input ();
601 return success_p;
606 /***********************************************************************
607 Tooltips
608 ***********************************************************************/
609 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
610 We use that to pop down the tooltip. This happens if Gtk+ for some
611 reason wants to change or hide the tooltip. */
613 #ifdef USE_GTK_TOOLTIP
615 static void
616 hierarchy_ch_cb (GtkWidget *widget,
617 GtkWidget *previous_toplevel,
618 gpointer user_data)
620 struct frame *f = user_data;
621 struct x_output *x = f->output_data.x;
622 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
624 if (! top || ! GTK_IS_WINDOW (top))
625 gtk_widget_hide (previous_toplevel);
628 /* Callback called when Gtk+ thinks a tooltip should be displayed.
629 We use it to get the tooltip window and the tooltip widget so
630 we can manipulate the ourselves.
632 Return FALSE ensures that the tooltip is not shown. */
634 static gboolean
635 qttip_cb (GtkWidget *widget,
636 gint xpos,
637 gint ypos,
638 gboolean keyboard_mode,
639 GtkTooltip *tooltip,
640 gpointer user_data)
642 struct frame *f = user_data;
643 struct x_output *x = f->output_data.x;
644 if (x->ttip_widget == NULL)
646 GtkWidget *p;
647 GList *list, *iter;
649 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
650 x->ttip_widget = tooltip;
651 g_object_ref (G_OBJECT (tooltip));
652 x->ttip_lbl = gtk_label_new ("");
653 g_object_ref (G_OBJECT (x->ttip_lbl));
654 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
655 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
657 /* Change stupid Gtk+ default line wrapping. */
658 p = gtk_widget_get_parent (x->ttip_lbl);
659 list = gtk_container_get_children (GTK_CONTAINER (p));
660 for (iter = list; iter; iter = g_list_next (iter))
662 GtkWidget *w = GTK_WIDGET (iter->data);
663 if (GTK_IS_LABEL (w))
664 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
666 g_list_free (list);
668 /* ATK needs an empty title for some reason. */
669 gtk_window_set_title (x->ttip_window, "");
670 /* Realize so we can safely get screen later on. */
671 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
672 gtk_widget_realize (x->ttip_lbl);
674 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
675 G_CALLBACK (hierarchy_ch_cb), f);
677 return FALSE;
680 #endif /* USE_GTK_TOOLTIP */
682 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
683 Return true if a system tooltip is available. */
685 bool
686 xg_prepare_tooltip (struct frame *f,
687 Lisp_Object string,
688 int *width,
689 int *height)
691 #ifndef USE_GTK_TOOLTIP
692 return 0;
693 #else
694 struct x_output *x = f->output_data.x;
695 GtkWidget *widget;
696 GdkWindow *gwin;
697 GdkScreen *screen;
698 GtkSettings *settings;
699 gboolean tt_enabled = TRUE;
700 GtkRequisition req;
701 Lisp_Object encoded_string;
703 if (!x->ttip_lbl) return 0;
705 block_input ();
706 encoded_string = ENCODE_UTF_8 (string);
707 widget = GTK_WIDGET (x->ttip_lbl);
708 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
709 screen = gdk_window_get_screen (gwin);
710 settings = gtk_settings_get_for_screen (screen);
711 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
712 if (tt_enabled)
714 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
715 /* Record that we disabled it so it can be enabled again. */
716 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
717 (gpointer)f);
720 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
721 g_object_set_data (G_OBJECT
722 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
723 "gdk-display-current-tooltip", NULL);
725 /* Put our dummy widget in so we can get callbacks for unrealize and
726 hierarchy-changed. */
727 gtk_tooltip_set_custom (x->ttip_widget, widget);
728 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
729 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
730 if (width) *width = req.width;
731 if (height) *height = req.height;
733 unblock_input ();
735 return 1;
736 #endif /* USE_GTK_TOOLTIP */
739 /* Show the tooltip at ROOT_X and ROOT_Y.
740 xg_prepare_tooltip must have been called before this function. */
742 void
743 xg_show_tooltip (struct frame *f, int root_x, int root_y)
745 #ifdef USE_GTK_TOOLTIP
746 struct x_output *x = f->output_data.x;
747 if (x->ttip_window)
749 block_input ();
750 gtk_window_move (x->ttip_window, root_x, root_y);
751 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
752 unblock_input ();
754 #endif
757 /* Hide tooltip if shown. Do nothing if not shown.
758 Return true if tip was hidden, false if not (i.e. not using
759 system tooltips). */
761 bool
762 xg_hide_tooltip (struct frame *f)
764 bool ret = 0;
765 #ifdef USE_GTK_TOOLTIP
766 if (f->output_data.x->ttip_window)
768 GtkWindow *win = f->output_data.x->ttip_window;
769 block_input ();
770 gtk_widget_hide (GTK_WIDGET (win));
772 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
774 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
775 GdkScreen *screen = gdk_window_get_screen (gwin);
776 GtkSettings *settings = gtk_settings_get_for_screen (screen);
777 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
779 unblock_input ();
781 ret = 1;
783 #endif
784 return ret;
788 /***********************************************************************
789 General functions for creating widgets, resizing, events, e.t.c.
790 ***********************************************************************/
792 static void
793 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
794 const gchar *msg, gpointer user_data)
796 if (!strstr (msg, "visible children"))
797 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
800 /* Make a geometry string and pass that to GTK. It seems this is the
801 only way to get geometry position right if the user explicitly
802 asked for a position when starting Emacs.
803 F is the frame we shall set geometry for. */
805 static void
806 xg_set_geometry (struct frame *f)
808 if (f->size_hint_flags & (USPosition | PPosition))
810 int left = f->left_pos;
811 int xneg = f->size_hint_flags & XNegative;
812 int top = f->top_pos;
813 int yneg = f->size_hint_flags & YNegative;
814 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
815 guint id;
817 if (xneg)
818 left = -left;
819 if (yneg)
820 top = -top;
822 sprintf (geom_str, "=%dx%d%c%d%c%d",
823 FRAME_PIXEL_WIDTH (f),
824 FRAME_PIXEL_HEIGHT (f),
825 (xneg ? '-' : '+'), left,
826 (yneg ? '-' : '+'), top);
828 /* Silence warning about visible children. */
829 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
830 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
832 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
833 geom_str))
834 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
836 g_log_remove_handler ("Gtk", id);
840 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
841 and use a GtkFixed widget, this doesn't happen automatically. */
843 void
844 xg_clear_under_internal_border (struct frame *f)
846 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
848 GtkWidget *wfixed = f->output_data.x->edit_widget;
850 gtk_widget_queue_draw (wfixed);
851 gdk_window_process_all_updates ();
853 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
854 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
856 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
857 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
859 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
860 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
861 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
863 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
864 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
865 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
869 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
870 and a Gtk+ menu bar, we get resize events for the edit part of the
871 frame only. We let Gtk+ deal with the Gtk+ parts.
872 F is the frame to resize.
873 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
875 void
876 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
878 int width, height;
880 if (pixelwidth == -1 && pixelheight == -1)
882 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
883 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
884 0, 0, &pixelwidth, &pixelheight);
885 else
886 return;
889 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
890 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
892 frame_size_history_add
893 (f, Qxg_frame_resized, width, height, Qnil);
895 if (width != FRAME_TEXT_WIDTH (f)
896 || height != FRAME_TEXT_HEIGHT (f)
897 || pixelwidth != FRAME_PIXEL_WIDTH (f)
898 || pixelheight != FRAME_PIXEL_HEIGHT (f))
900 xg_clear_under_internal_border (f);
901 change_frame_size (f, width, height, 0, 1, 0, 1);
902 SET_FRAME_GARBAGED (f);
903 cancel_mouse_face (f);
905 do_pending_window_change (0);
909 /* Resize the outer window of frame F after changing the height.
910 COLUMNS/ROWS is the size the edit area shall have after the resize. */
912 void
913 xg_frame_set_char_size (struct frame *f, int width, int height)
915 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
916 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
917 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
918 gint gwidth, gheight;
920 if (FRAME_PIXEL_HEIGHT (f) == 0)
921 return;
923 gtk_window_get_size (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
924 &gwidth, &gheight);
926 /* Do this before resize, as we don't know yet if we will be resized. */
927 xg_clear_under_internal_border (f);
929 /* Resize the top level widget so rows and columns remain constant.
931 When the frame is fullheight and we only want to change the width
932 or it is fullwidth and we only want to change the height we should
933 be able to preserve the fullscreen property. However, due to the
934 fact that we have to send a resize request anyway, the window
935 manager will abolish it. At least the respective size should
936 remain unchanged but giving the frame back its normal size will
937 be broken ... */
938 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
940 frame_size_history_add
941 (f, Qxg_frame_set_char_size_1, width, height,
942 list2 (make_number (gheight),
943 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
944 + FRAME_MENUBAR_HEIGHT (f))));
946 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
947 gwidth,
948 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
949 + FRAME_MENUBAR_HEIGHT (f));
951 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
953 frame_size_history_add
954 (f, Qxg_frame_set_char_size_2, width, height,
955 list2 (make_number (gwidth),
956 make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f))));
958 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
959 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
960 gheight);
963 else
965 frame_size_history_add
966 (f, Qxg_frame_set_char_size_3, width, height,
967 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
968 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
969 + FRAME_MENUBAR_HEIGHT (f))));
971 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
972 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
973 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
974 + FRAME_MENUBAR_HEIGHT (f));
975 fullscreen = Qnil;
978 SET_FRAME_GARBAGED (f);
979 cancel_mouse_face (f);
981 x_wm_set_size_hint (f, 0, 0);
982 /* We can not call change_frame_size for a mapped frame,
983 we can not set pixel width/height either. The window manager may
984 override our resize request, XMonad does this all the time.
985 The best we can do is try to sync, so lisp code sees the updated
986 size as fast as possible.
987 For unmapped windows, we can set rows/cols. When
988 the frame is mapped again we will (hopefully) get the correct size. */
989 if (FRAME_VISIBLE_P (f))
991 /* Must call this to flush out events */
992 (void)gtk_events_pending ();
993 gdk_flush ();
994 x_wait_for_event (f, ConfigureNotify);
996 if (!NILP (fullscreen))
997 /* Try to restore fullscreen state. */
999 store_frame_param (f, Qfullscreen, fullscreen);
1000 x_set_fullscreen (f, fullscreen, fullscreen);
1003 else
1004 adjust_frame_size (f, width, height, 5, 0, Qxg_frame_set_char_size);
1008 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
1009 The policy is to keep the number of editable lines. */
1011 #if 0
1012 static void
1013 xg_height_or_width_changed (struct frame *f)
1015 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1016 FRAME_TOTAL_PIXEL_WIDTH (f),
1017 FRAME_TOTAL_PIXEL_HEIGHT (f));
1018 f->output_data.x->hint_flags = 0;
1019 x_wm_set_size_hint (f, 0, 0);
1021 #endif
1023 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
1024 Must be done like this, because GtkWidget:s can have "hidden"
1025 X Window that aren't accessible.
1027 Return 0 if no widget match WDESC. */
1029 GtkWidget *
1030 xg_win_to_widget (Display *dpy, Window wdesc)
1032 gpointer gdkwin;
1033 GtkWidget *gwdesc = 0;
1035 block_input ();
1037 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
1038 wdesc);
1039 if (gdkwin)
1041 GdkEvent event;
1042 event.any.window = gdkwin;
1043 event.any.type = GDK_NOTHING;
1044 gwdesc = gtk_get_event_widget (&event);
1047 unblock_input ();
1048 return gwdesc;
1051 /* Set the background of widget W to PIXEL. */
1053 static void
1054 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
1056 #ifdef HAVE_GTK3
1057 GdkRGBA bg;
1058 XColor xbg;
1059 xbg.pixel = pixel;
1060 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1062 bg.red = (double)xbg.red/65535.0;
1063 bg.green = (double)xbg.green/65535.0;
1064 bg.blue = (double)xbg.blue/65535.0;
1065 bg.alpha = 1.0;
1066 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1068 #else
1069 GdkColor bg;
1070 GdkColormap *map = gtk_widget_get_colormap (w);
1071 gdk_colormap_query_color (map, pixel, &bg);
1072 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1073 #endif
1076 /* Callback called when the gtk theme changes.
1077 We notify lisp code so it can fix faces used for region for example. */
1079 static void
1080 style_changed_cb (GObject *go,
1081 GParamSpec *spec,
1082 gpointer user_data)
1084 struct input_event event;
1085 GdkDisplay *gdpy = user_data;
1086 const char *display_name = gdk_display_get_name (gdpy);
1087 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1089 EVENT_INIT (event);
1090 event.kind = CONFIG_CHANGED_EVENT;
1091 event.frame_or_window = build_string (display_name);
1092 /* Theme doesn't change often, so intern is called seldom. */
1093 event.arg = intern ("theme-name");
1094 kbd_buffer_store_event (&event);
1096 update_theme_scrollbar_width ();
1097 update_theme_scrollbar_height ();
1099 /* If scroll bar width changed, we need set the new size on all frames
1100 on this display. */
1101 if (dpy)
1103 Lisp_Object rest, frame;
1104 FOR_EACH_FRAME (rest, frame)
1106 struct frame *f = XFRAME (frame);
1107 if (FRAME_LIVE_P (f)
1108 && FRAME_X_P (f)
1109 && FRAME_X_DISPLAY (f) == dpy)
1111 x_set_scroll_bar_default_width (f);
1112 x_set_scroll_bar_default_height (f);
1113 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1119 /* Called when a delete-event occurs on WIDGET. */
1121 static gboolean
1122 delete_cb (GtkWidget *widget,
1123 GdkEvent *event,
1124 gpointer user_data)
1126 #ifdef HAVE_GTK3
1127 /* The event doesn't arrive in the normal event loop. Send event
1128 here. */
1129 struct frame *f = user_data;
1130 struct input_event ie;
1132 EVENT_INIT (ie);
1133 ie.kind = DELETE_WINDOW_EVENT;
1134 XSETFRAME (ie.frame_or_window, f);
1135 kbd_buffer_store_event (&ie);
1136 #endif
1138 return TRUE;
1141 /* Create and set up the GTK widgets for frame F.
1142 Return true if creation succeeded. */
1144 bool
1145 xg_create_frame_widgets (struct frame *f)
1147 GtkWidget *wtop;
1148 GtkWidget *wvbox, *whbox;
1149 GtkWidget *wfixed;
1150 #ifndef HAVE_GTK3
1151 GtkRcStyle *style;
1152 #endif
1153 char *title = 0;
1155 block_input ();
1157 if (FRAME_X_EMBEDDED_P (f))
1159 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1160 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1162 else
1163 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1165 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1166 has backported it to Gtk+ 2.0 and they add the resize grip for
1167 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1168 forever, so disable the grip. */
1169 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1170 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1171 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1172 #endif
1174 xg_set_screen (wtop, f);
1176 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1177 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1178 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1179 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1181 #ifdef HAVE_GTK3
1182 wfixed = emacs_fixed_new (f);
1183 #else
1184 wfixed = gtk_fixed_new ();
1185 #endif
1187 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1189 if (wtop) gtk_widget_destroy (wtop);
1190 if (wvbox) gtk_widget_destroy (wvbox);
1191 if (whbox) gtk_widget_destroy (whbox);
1192 if (wfixed) gtk_widget_destroy (wfixed);
1194 unblock_input ();
1195 return 0;
1198 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1199 gtk_widget_set_name (wtop, EMACS_CLASS);
1200 gtk_widget_set_name (wvbox, "pane");
1201 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1203 /* If this frame has a title or name, set it in the title bar. */
1204 if (! NILP (f->title))
1205 title = SSDATA (ENCODE_UTF_8 (f->title));
1206 else if (! NILP (f->name))
1207 title = SSDATA (ENCODE_UTF_8 (f->name));
1209 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1211 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1212 FRAME_GTK_WIDGET (f) = wfixed;
1213 f->output_data.x->vbox_widget = wvbox;
1214 f->output_data.x->hbox_widget = whbox;
1216 gtk_widget_set_has_window (wfixed, TRUE);
1218 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1219 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1220 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1222 if (FRAME_EXTERNAL_TOOL_BAR (f))
1223 update_frame_tool_bar (f);
1225 /* We don't want this widget double buffered, because we draw on it
1226 with regular X drawing primitives, so from a GTK/GDK point of
1227 view, the widget is totally blank. When an expose comes, this
1228 will make the widget blank, and then Emacs redraws it. This flickers
1229 a lot, so we turn off double buffering. */
1230 gtk_widget_set_double_buffered (wfixed, FALSE);
1232 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1233 SSDATA (Vx_resource_name),
1234 SSDATA (Vx_resource_class));
1236 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1237 GTK is to destroy the widget. We want Emacs to do that instead. */
1238 g_signal_connect (G_OBJECT (wtop), "delete-event",
1239 G_CALLBACK (delete_cb), f);
1241 /* Convert our geometry parameters into a geometry string
1242 and specify it.
1243 GTK will itself handle calculating the real position this way. */
1244 xg_set_geometry (f);
1245 f->win_gravity
1246 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1248 gtk_widget_add_events (wfixed,
1249 GDK_POINTER_MOTION_MASK
1250 | GDK_EXPOSURE_MASK
1251 | GDK_BUTTON_PRESS_MASK
1252 | GDK_BUTTON_RELEASE_MASK
1253 | GDK_KEY_PRESS_MASK
1254 | GDK_ENTER_NOTIFY_MASK
1255 | GDK_LEAVE_NOTIFY_MASK
1256 | GDK_FOCUS_CHANGE_MASK
1257 | GDK_STRUCTURE_MASK
1258 | GDK_VISIBILITY_NOTIFY_MASK);
1260 /* Must realize the windows so the X window gets created. It is used
1261 by callers of this function. */
1262 gtk_widget_realize (wfixed);
1263 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1265 /* Since GTK clears its window by filling with the background color,
1266 we must keep X and GTK background in sync. */
1267 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1269 #ifndef HAVE_GTK3
1270 /* Also, do not let any background pixmap to be set, this looks very
1271 bad as Emacs overwrites the background pixmap with its own idea
1272 of background color. */
1273 style = gtk_widget_get_modifier_style (wfixed);
1275 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1276 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1277 gtk_widget_modify_style (wfixed, style);
1278 #else
1279 gtk_widget_set_can_focus (wfixed, TRUE);
1280 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1281 #endif
1283 #ifdef USE_GTK_TOOLTIP
1284 /* Steal a tool tip window we can move ourselves. */
1285 f->output_data.x->ttip_widget = 0;
1286 f->output_data.x->ttip_lbl = 0;
1287 f->output_data.x->ttip_window = 0;
1288 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1289 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1290 #endif
1293 GdkScreen *screen = gtk_widget_get_screen (wtop);
1294 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1295 /* Only connect this signal once per screen. */
1296 if (! g_signal_handler_find (G_OBJECT (gs),
1297 G_SIGNAL_MATCH_FUNC,
1298 0, 0, 0,
1299 G_CALLBACK (style_changed_cb),
1302 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1303 G_CALLBACK (style_changed_cb),
1304 gdk_screen_get_display (screen));
1308 unblock_input ();
1310 return 1;
1313 void
1314 xg_free_frame_widgets (struct frame *f)
1316 if (FRAME_GTK_OUTER_WIDGET (f))
1318 #ifdef USE_GTK_TOOLTIP
1319 struct x_output *x = f->output_data.x;
1320 #endif
1321 struct xg_frame_tb_info *tbinfo
1322 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1323 TB_INFO_KEY);
1324 if (tbinfo)
1325 xfree (tbinfo);
1327 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1328 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1329 FRAME_GTK_OUTER_WIDGET (f) = 0;
1330 #ifdef USE_GTK_TOOLTIP
1331 if (x->ttip_lbl)
1332 gtk_widget_destroy (x->ttip_lbl);
1333 if (x->ttip_widget)
1334 g_object_unref (G_OBJECT (x->ttip_widget));
1335 #endif
1339 /* Set the normal size hints for the window manager, for frame F.
1340 FLAGS is the flags word to use--or 0 meaning preserve the flags
1341 that the window now has.
1342 If USER_POSITION, set the User Position
1343 flag (this is useful when FLAGS is 0). */
1345 void
1346 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1348 /* Must use GTK routines here, otherwise GTK resets the size hints
1349 to its own defaults. */
1350 GdkGeometry size_hints;
1351 gint hint_flags = 0;
1352 int base_width, base_height;
1353 int min_rows = 0, min_cols = 0;
1354 int win_gravity = f->win_gravity;
1355 Lisp_Object fs_state, frame;
1357 /* Don't set size hints during initialization; that apparently leads
1358 to a race condition. See the thread at
1359 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1360 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1361 return;
1363 XSETFRAME (frame, f);
1364 fs_state = Fframe_parameter (frame, Qfullscreen);
1365 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1367 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1368 Gtk3 don't get along and the frame shrinks (!).
1370 return;
1373 if (flags)
1375 memset (&size_hints, 0, sizeof (size_hints));
1376 f->output_data.x->size_hints = size_hints;
1377 f->output_data.x->hint_flags = hint_flags;
1379 else
1380 flags = f->size_hint_flags;
1382 size_hints = f->output_data.x->size_hints;
1383 hint_flags = f->output_data.x->hint_flags;
1385 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1386 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1387 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1389 hint_flags |= GDK_HINT_BASE_SIZE;
1390 /* Use one row/col here so base_height/width does not become zero.
1391 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1392 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1393 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1394 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1396 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1397 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1399 size_hints.base_width = base_width;
1400 size_hints.base_height = base_height;
1401 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
1402 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1404 /* These currently have a one to one mapping with the X values, but I
1405 don't think we should rely on that. */
1406 hint_flags |= GDK_HINT_WIN_GRAVITY;
1407 size_hints.win_gravity = 0;
1408 if (win_gravity == NorthWestGravity)
1409 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1410 else if (win_gravity == NorthGravity)
1411 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1412 else if (win_gravity == NorthEastGravity)
1413 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1414 else if (win_gravity == WestGravity)
1415 size_hints.win_gravity = GDK_GRAVITY_WEST;
1416 else if (win_gravity == CenterGravity)
1417 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1418 else if (win_gravity == EastGravity)
1419 size_hints.win_gravity = GDK_GRAVITY_EAST;
1420 else if (win_gravity == SouthWestGravity)
1421 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1422 else if (win_gravity == SouthGravity)
1423 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1424 else if (win_gravity == SouthEastGravity)
1425 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1426 else if (win_gravity == StaticGravity)
1427 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1429 if (user_position)
1431 hint_flags &= ~GDK_HINT_POS;
1432 hint_flags |= GDK_HINT_USER_POS;
1435 if (hint_flags != f->output_data.x->hint_flags
1436 || memcmp (&size_hints,
1437 &f->output_data.x->size_hints,
1438 sizeof (size_hints)) != 0)
1440 block_input ();
1441 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1442 NULL, &size_hints, hint_flags);
1443 f->output_data.x->size_hints = size_hints;
1444 f->output_data.x->hint_flags = hint_flags;
1445 unblock_input ();
1449 /* Change background color of a frame.
1450 Since GTK uses the background color to clear the window, we must
1451 keep the GTK and X colors in sync.
1452 F is the frame to change,
1453 BG is the pixel value to change to. */
1455 void
1456 xg_set_background_color (struct frame *f, unsigned long bg)
1458 if (FRAME_GTK_WIDGET (f))
1460 block_input ();
1461 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1462 unblock_input ();
1467 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1468 functions so GTK does not overwrite the icon. */
1470 void
1471 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1473 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1474 icon_pixmap,
1475 icon_mask);
1476 if (gp)
1477 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1482 /***********************************************************************
1483 Dialog functions
1484 ***********************************************************************/
1485 /* Return the dialog title to use for a dialog of type KEY.
1486 This is the encoding used by lwlib. We use the same for GTK. */
1488 static const char *
1489 get_dialog_title (char key)
1491 const char *title = "";
1493 switch (key) {
1494 case 'E': case 'e':
1495 title = "Error";
1496 break;
1498 case 'I': case 'i':
1499 title = "Information";
1500 break;
1502 case 'L': case 'l':
1503 title = "Prompt";
1504 break;
1506 case 'P': case 'p':
1507 title = "Prompt";
1508 break;
1510 case 'Q': case 'q':
1511 title = "Question";
1512 break;
1515 return title;
1518 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1519 the dialog, but return TRUE so the event does not propagate further
1520 in GTK. This prevents GTK from destroying the dialog widget automatically
1521 and we can always destroy the widget manually, regardless of how
1522 it was popped down (button press or WM_DELETE_WINDOW).
1523 W is the dialog widget.
1524 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1525 user_data is NULL (not used).
1527 Returns TRUE to end propagation of event. */
1529 static gboolean
1530 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1532 gtk_widget_unmap (w);
1533 return TRUE;
1536 /* Create a popup dialog window. See also xg_create_widget below.
1537 WV is a widget_value describing the dialog.
1538 SELECT_CB is the callback to use when a button has been pressed.
1539 DEACTIVATE_CB is the callback to use when the dialog pops down.
1541 Returns the GTK dialog widget. */
1543 static GtkWidget *
1544 create_dialog (widget_value *wv,
1545 GCallback select_cb,
1546 GCallback deactivate_cb)
1548 const char *title = get_dialog_title (wv->name[0]);
1549 int total_buttons = wv->name[1] - '0';
1550 int right_buttons = wv->name[4] - '0';
1551 int left_buttons;
1552 int button_nr = 0;
1553 int button_spacing = 10;
1554 GtkWidget *wdialog = gtk_dialog_new ();
1555 GtkDialog *wd = GTK_DIALOG (wdialog);
1556 widget_value *item;
1557 GtkWidget *whbox_down;
1559 /* If the number of buttons is greater than 4, make two rows of buttons
1560 instead. This looks better. */
1561 bool make_two_rows = total_buttons > 4;
1563 #if GTK_CHECK_VERSION (3, 12, 0)
1564 GtkBuilder *gbld = gtk_builder_new ();
1565 GObject *go = gtk_buildable_get_internal_child (GTK_BUILDABLE (wd),
1566 gbld,
1567 "action_area");
1568 GtkBox *cur_box = GTK_BOX (go);
1569 g_object_unref (G_OBJECT (gbld));
1570 #else
1571 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1572 #endif
1574 if (right_buttons == 0) right_buttons = total_buttons/2;
1575 left_buttons = total_buttons - right_buttons;
1577 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1578 gtk_widget_set_name (wdialog, "emacs-dialog");
1581 if (make_two_rows)
1583 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1584 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1585 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1586 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1587 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1588 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1590 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1591 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1592 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1594 cur_box = GTK_BOX (whbox_up);
1597 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1598 G_CALLBACK (dialog_delete_callback), 0);
1600 if (deactivate_cb)
1602 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1603 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1606 for (item = wv->contents; item; item = item->next)
1608 char *utf8_label = get_utf8_string (item->value);
1609 GtkWidget *w;
1610 GtkRequisition req;
1612 if (item->name && strcmp (item->name, "message") == 0)
1614 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1615 /* This is the text part of the dialog. */
1616 w = gtk_label_new (utf8_label);
1617 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1618 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1619 #if GTK_CHECK_VERSION (3, 14, 0)
1620 gtk_widget_set_halign (w, GTK_ALIGN_START);
1621 gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
1622 #else
1623 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1624 #endif
1625 /* Try to make dialog look better. Must realize first so
1626 the widget can calculate the size it needs. */
1627 gtk_widget_realize (w);
1628 gtk_widget_get_preferred_size (w, NULL, &req);
1629 gtk_box_set_spacing (wvbox, req.height);
1630 if (item->value && strlen (item->value) > 0)
1631 button_spacing = 2*req.width/strlen (item->value);
1632 if (button_spacing < 10) button_spacing = 10;
1634 else
1636 /* This is one button to add to the dialog. */
1637 w = gtk_button_new_with_label (utf8_label);
1638 if (! item->enabled)
1639 gtk_widget_set_sensitive (w, FALSE);
1640 if (select_cb)
1641 g_signal_connect (G_OBJECT (w), "clicked",
1642 select_cb, item->call_data);
1644 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1645 if (++button_nr == left_buttons)
1647 if (make_two_rows)
1648 cur_box = GTK_BOX (whbox_down);
1652 if (utf8_label)
1653 g_free (utf8_label);
1656 return wdialog;
1659 struct xg_dialog_data
1661 GMainLoop *loop;
1662 int response;
1663 GtkWidget *w;
1664 guint timerid;
1667 /* Function that is called when the file or font dialogs pop down.
1668 W is the dialog widget, RESPONSE is the response code.
1669 USER_DATA is what we passed in to g_signal_connect. */
1671 static void
1672 xg_dialog_response_cb (GtkDialog *w,
1673 gint response,
1674 gpointer user_data)
1676 struct xg_dialog_data *dd = user_data;
1677 dd->response = response;
1678 g_main_loop_quit (dd->loop);
1682 /* Destroy the dialog. This makes it pop down. */
1684 static void
1685 pop_down_dialog (void *arg)
1687 struct xg_dialog_data *dd = arg;
1689 block_input ();
1690 if (dd->w) gtk_widget_destroy (dd->w);
1691 if (dd->timerid != 0) g_source_remove (dd->timerid);
1693 g_main_loop_quit (dd->loop);
1694 g_main_loop_unref (dd->loop);
1696 unblock_input ();
1699 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1700 We pass in DATA as gpointer* so we can use this as a callback. */
1702 static gboolean
1703 xg_maybe_add_timer (gpointer data)
1705 struct xg_dialog_data *dd = data;
1706 struct timespec next_time = timer_check ();
1708 dd->timerid = 0;
1710 if (timespec_valid_p (next_time))
1712 time_t s = next_time.tv_sec;
1713 int per_ms = TIMESPEC_RESOLUTION / 1000;
1714 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1715 if (s <= ((guint) -1 - ms) / 1000)
1716 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1718 return FALSE;
1722 /* Pops up a modal dialog W and waits for response.
1723 We don't use gtk_dialog_run because we want to process emacs timers.
1724 The dialog W is not destroyed when this function returns. */
1726 static int
1727 xg_dialog_run (struct frame *f, GtkWidget *w)
1729 ptrdiff_t count = SPECPDL_INDEX ();
1730 struct xg_dialog_data dd;
1732 xg_set_screen (w, f);
1733 gtk_window_set_transient_for (GTK_WINDOW (w),
1734 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1735 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1736 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1738 dd.loop = g_main_loop_new (NULL, FALSE);
1739 dd.response = GTK_RESPONSE_CANCEL;
1740 dd.w = w;
1741 dd.timerid = 0;
1743 g_signal_connect (G_OBJECT (w),
1744 "response",
1745 G_CALLBACK (xg_dialog_response_cb),
1746 &dd);
1747 /* Don't destroy the widget if closed by the window manager close button. */
1748 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1749 gtk_widget_show (w);
1751 record_unwind_protect_ptr (pop_down_dialog, &dd);
1753 (void) xg_maybe_add_timer (&dd);
1754 g_main_loop_run (dd.loop);
1756 dd.w = 0;
1757 unbind_to (count, Qnil);
1759 return dd.response;
1763 /***********************************************************************
1764 File dialog functions
1765 ***********************************************************************/
1766 /* Return true if the old file selection dialog is being used. */
1768 bool
1769 xg_uses_old_file_dialog (void)
1771 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1772 return x_gtk_use_old_file_dialog;
1773 #else
1774 return 0;
1775 #endif
1779 typedef char * (*xg_get_file_func) (GtkWidget *);
1781 /* Return the selected file for file chooser dialog W.
1782 The returned string must be free:d. */
1784 static char *
1785 xg_get_file_name_from_chooser (GtkWidget *w)
1787 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1790 /* Callback called when the "Show hidden files" toggle is pressed.
1791 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1793 static void
1794 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1796 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1797 gboolean visible;
1798 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1799 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1803 /* Callback called when a property changes in a file chooser.
1804 GOBJECT is the file chooser dialog, ARG1 describes the property.
1805 USER_DATA is the toggle widget in the file chooser dialog.
1806 We use this to update the "Show hidden files" toggle when the user
1807 changes that property by right clicking in the file list. */
1809 static void
1810 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1812 if (strcmp (arg1->name, "show-hidden") == 0)
1814 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1815 gboolean visible, toggle_on;
1817 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1818 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1820 if (!!visible != !!toggle_on)
1822 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1823 G_CALLBACK (xg_toggle_visibility_cb),
1824 gobject);
1825 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1826 g_signal_handlers_unblock_by_func
1827 (G_OBJECT (wtoggle),
1828 G_CALLBACK (xg_toggle_visibility_cb),
1829 gobject);
1831 x_gtk_show_hidden_files = visible;
1835 /* Read a file name from the user using a file chooser dialog.
1836 F is the current frame.
1837 PROMPT is a prompt to show to the user. May not be NULL.
1838 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1839 If MUSTMATCH_P, the returned file name must be an existing
1840 file. (Actually, this only has cosmetic effects, the user can
1841 still enter a non-existing file.) *FUNC is set to a function that
1842 can be used to retrieve the selected file name from the returned widget.
1844 Returns the created widget. */
1846 static GtkWidget *
1847 xg_get_file_with_chooser (struct frame *f,
1848 char *prompt,
1849 char *default_filename,
1850 bool mustmatch_p, bool only_dir_p,
1851 xg_get_file_func *func)
1853 char msgbuf[1024];
1855 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1856 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1857 GtkFileChooserAction action = (mustmatch_p ?
1858 GTK_FILE_CHOOSER_ACTION_OPEN :
1859 GTK_FILE_CHOOSER_ACTION_SAVE);
1861 if (only_dir_p)
1862 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1864 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1865 XG_TEXT_CANCEL, GTK_RESPONSE_CANCEL,
1866 (mustmatch_p || only_dir_p ?
1867 XG_TEXT_OPEN : XG_TEXT_OK),
1868 GTK_RESPONSE_OK,
1869 NULL);
1870 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1872 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1873 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1874 gtk_widget_show (wbox);
1875 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1877 if (x_gtk_show_hidden_files)
1879 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1880 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1882 gtk_widget_show (wtoggle);
1883 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1884 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1885 g_signal_connect (G_OBJECT (filewin), "notify",
1886 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1888 if (x_gtk_file_dialog_help_text)
1890 char *z = msgbuf;
1891 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1892 Show the C-l help text only for versions < 2.10. */
1893 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1894 z = stpcpy (z, "\nType C-l to display a file name text entry box.\n");
1895 strcpy (z, "\nIf you don't like this file selector, use the "
1896 "corresponding\nkey binding or customize "
1897 "use-file-dialog to turn it off.");
1899 wmessage = gtk_label_new (msgbuf);
1900 gtk_widget_show (wmessage);
1903 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1904 if (x_gtk_file_dialog_help_text)
1905 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1906 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1908 if (default_filename)
1910 Lisp_Object file;
1911 struct gcpro gcpro1;
1912 char *utf8_filename;
1913 GCPRO1 (file);
1915 file = build_string (default_filename);
1917 /* File chooser does not understand ~/... in the file name. It must be
1918 an absolute name starting with /. */
1919 if (default_filename[0] != '/')
1920 file = Fexpand_file_name (file, Qnil);
1922 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1923 if (! NILP (Ffile_directory_p (file)))
1924 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1925 utf8_filename);
1926 else
1928 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1929 utf8_filename);
1930 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1932 char *cp = strrchr (utf8_filename, '/');
1933 if (cp) ++cp;
1934 else cp = utf8_filename;
1935 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1939 UNGCPRO;
1942 *func = xg_get_file_name_from_chooser;
1943 return filewin;
1946 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1948 /* Return the selected file for file selector dialog W.
1949 The returned string must be free:d. */
1951 static char *
1952 xg_get_file_name_from_selector (GtkWidget *w)
1954 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1955 return xstrdup (gtk_file_selection_get_filename (filesel));
1958 /* Create a file selection dialog.
1959 F is the current frame.
1960 PROMPT is a prompt to show to the user. May not be NULL.
1961 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1962 If MUSTMATCH_P, the returned file name must be an existing
1963 file. *FUNC is set to a function that can be used to retrieve the
1964 selected file name from the returned widget.
1966 Returns the created widget. */
1968 static GtkWidget *
1969 xg_get_file_with_selection (struct frame *f,
1970 char *prompt,
1971 char *default_filename,
1972 bool mustmatch_p, bool only_dir_p,
1973 xg_get_file_func *func)
1975 GtkWidget *filewin;
1976 GtkFileSelection *filesel;
1978 filewin = gtk_file_selection_new (prompt);
1979 filesel = GTK_FILE_SELECTION (filewin);
1981 if (default_filename)
1982 gtk_file_selection_set_filename (filesel, default_filename);
1984 if (mustmatch_p)
1986 /* The selection_entry part of filesel is not documented. */
1987 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1988 gtk_file_selection_hide_fileop_buttons (filesel);
1991 *func = xg_get_file_name_from_selector;
1993 return filewin;
1995 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1997 /* Read a file name from the user using a file dialog, either the old
1998 file selection dialog, or the new file chooser dialog. Which to use
1999 depends on what the GTK version used has, and what the value of
2000 gtk-use-old-file-dialog.
2001 F is the current frame.
2002 PROMPT is a prompt to show to the user. May not be NULL.
2003 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
2004 If MUSTMATCH_P, the returned file name must be an existing
2005 file.
2007 Returns a file name or NULL if no file was selected.
2008 The returned string must be freed by the caller. */
2010 char *
2011 xg_get_file_name (struct frame *f,
2012 char *prompt,
2013 char *default_filename,
2014 bool mustmatch_p,
2015 bool only_dir_p)
2017 GtkWidget *w = 0;
2018 char *fn = 0;
2019 int filesel_done = 0;
2020 xg_get_file_func func;
2022 #ifdef HAVE_GTK_FILE_SELECTION_NEW
2024 if (xg_uses_old_file_dialog ())
2025 w = xg_get_file_with_selection (f, prompt, default_filename,
2026 mustmatch_p, only_dir_p, &func);
2027 else
2028 w = xg_get_file_with_chooser (f, prompt, default_filename,
2029 mustmatch_p, only_dir_p, &func);
2031 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
2032 w = xg_get_file_with_chooser (f, prompt, default_filename,
2033 mustmatch_p, only_dir_p, &func);
2034 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
2036 gtk_widget_set_name (w, "emacs-filedialog");
2038 filesel_done = xg_dialog_run (f, w);
2039 if (filesel_done == GTK_RESPONSE_OK)
2040 fn = (*func) (w);
2042 gtk_widget_destroy (w);
2043 return fn;
2046 /***********************************************************************
2047 GTK font chooser
2048 ***********************************************************************/
2050 #ifdef HAVE_FREETYPE
2052 #if USE_NEW_GTK_FONT_CHOOSER
2054 #define XG_WEIGHT_TO_SYMBOL(w) \
2055 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
2056 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
2057 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
2058 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
2059 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
2060 : w <= PANGO_WEIGHT_BOLD ? Qbold \
2061 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
2062 : Qultra_bold)
2064 #define XG_STYLE_TO_SYMBOL(s) \
2065 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
2066 : s == PANGO_STYLE_ITALIC ? Qitalic \
2067 : Qnormal)
2069 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2072 static char *x_last_font_name;
2074 /* Pop up a GTK font selector and return the name of the font the user
2075 selects, as a C string. The returned font name follows GTK's own
2076 format:
2078 `FAMILY [VALUE1 VALUE2] SIZE'
2080 This can be parsed using font_parse_fcname in font.c.
2081 DEFAULT_NAME, if non-zero, is the default font name. */
2083 Lisp_Object
2084 xg_get_font (struct frame *f, const char *default_name)
2086 GtkWidget *w;
2087 int done = 0;
2088 Lisp_Object font = Qnil;
2090 w = gtk_font_chooser_dialog_new
2091 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2093 if (default_name)
2095 /* Convert fontconfig names to Gtk names, i.e. remove - before
2096 number */
2097 char *p = strrchr (default_name, '-');
2098 if (p)
2100 char *ep = p+1;
2101 while (c_isdigit (*ep))
2102 ++ep;
2103 if (*ep == '\0') *p = ' ';
2106 else if (x_last_font_name)
2107 default_name = x_last_font_name;
2109 if (default_name)
2110 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2112 gtk_widget_set_name (w, "emacs-fontdialog");
2113 done = xg_dialog_run (f, w);
2114 if (done == GTK_RESPONSE_OK)
2116 #if USE_NEW_GTK_FONT_CHOOSER
2117 /* Use the GTK3 font chooser. */
2118 PangoFontDescription *desc
2119 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2121 if (desc)
2123 const char *name = pango_font_description_get_family (desc);
2124 gint size = pango_font_description_get_size (desc);
2125 PangoWeight weight = pango_font_description_get_weight (desc);
2126 PangoStyle style = pango_font_description_get_style (desc);
2128 font = CALLN (Ffont_spec,
2129 QCname, build_string (name),
2130 QCsize, make_float (pango_units_to_double (size)),
2131 QCweight, XG_WEIGHT_TO_SYMBOL (weight),
2132 QCslant, XG_STYLE_TO_SYMBOL (style),
2133 QCtype, Qxft);
2135 pango_font_description_free (desc);
2136 dupstring (&x_last_font_name, name);
2139 #else /* Use old font selector, which just returns the font name. */
2141 char *font_name
2142 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2144 if (font_name)
2146 font = build_string (font_name);
2147 g_free (x_last_font_name);
2148 x_last_font_name = font_name;
2150 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2153 gtk_widget_destroy (w);
2154 return font;
2156 #endif /* HAVE_FREETYPE */
2160 /***********************************************************************
2161 Menu functions.
2162 ***********************************************************************/
2164 /* The name of menu items that can be used for customization. Since GTK
2165 RC files are very crude and primitive, we have to set this on all
2166 menu item names so a user can easily customize menu items. */
2168 #define MENU_ITEM_NAME "emacs-menuitem"
2171 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2172 during GC. The next member points to the items. */
2173 static xg_list_node xg_menu_cb_list;
2175 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2176 during GC. The next member points to the items. */
2177 static xg_list_node xg_menu_item_cb_list;
2179 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2180 F is the frame CL_DATA will be initialized for.
2181 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2183 The menu bar and all sub menus under the menu bar in a frame
2184 share the same structure, hence the reference count.
2186 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2187 allocated xg_menu_cb_data if CL_DATA is NULL. */
2189 static xg_menu_cb_data *
2190 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2192 if (! cl_data)
2194 cl_data = xmalloc (sizeof *cl_data);
2195 cl_data->f = f;
2196 cl_data->menu_bar_vector = f->menu_bar_vector;
2197 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2198 cl_data->highlight_cb = highlight_cb;
2199 cl_data->ref_count = 0;
2201 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2204 cl_data->ref_count++;
2206 return cl_data;
2209 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2210 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2212 When the menu bar is updated, menu items may have been added and/or
2213 removed, so menu_bar_vector and menu_bar_items_used change. We must
2214 then update CL_DATA since it is used to determine which menu
2215 item that is invoked in the menu.
2216 HIGHLIGHT_CB could change, there is no check that the same
2217 function is given when modifying a menu bar as was given when
2218 creating the menu bar. */
2220 static void
2221 update_cl_data (xg_menu_cb_data *cl_data,
2222 struct frame *f,
2223 GCallback highlight_cb)
2225 if (cl_data)
2227 cl_data->f = f;
2228 cl_data->menu_bar_vector = f->menu_bar_vector;
2229 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2230 cl_data->highlight_cb = highlight_cb;
2234 /* Decrease reference count for CL_DATA.
2235 If reference count is zero, free CL_DATA. */
2237 static void
2238 unref_cl_data (xg_menu_cb_data *cl_data)
2240 if (cl_data && cl_data->ref_count > 0)
2242 cl_data->ref_count--;
2243 if (cl_data->ref_count == 0)
2245 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2246 xfree (cl_data);
2251 /* Function that marks all lisp data during GC. */
2253 void
2254 xg_mark_data (void)
2256 xg_list_node *iter;
2257 Lisp_Object rest, frame;
2259 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2260 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2262 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2264 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2266 if (! NILP (cb_data->help))
2267 mark_object (cb_data->help);
2270 FOR_EACH_FRAME (rest, frame)
2272 struct frame *f = XFRAME (frame);
2274 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2276 struct xg_frame_tb_info *tbinfo
2277 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2278 TB_INFO_KEY);
2279 if (tbinfo)
2281 mark_object (tbinfo->last_tool_bar);
2282 mark_object (tbinfo->style);
2289 /* Callback called when a menu item is destroyed. Used to free data.
2290 W is the widget that is being destroyed (not used).
2291 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2293 static void
2294 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2296 if (client_data)
2298 xg_menu_item_cb_data *data = client_data;
2299 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2300 xfree (data);
2304 /* Callback called when the pointer enters/leaves a menu item.
2305 W is the parent of the menu item.
2306 EVENT is either an enter event or leave event.
2307 CLIENT_DATA is not used.
2309 Returns FALSE to tell GTK to keep processing this event. */
2311 static gboolean
2312 menuitem_highlight_callback (GtkWidget *w,
2313 GdkEventCrossing *event,
2314 gpointer client_data)
2316 GdkEvent ev;
2317 GtkWidget *subwidget;
2318 xg_menu_item_cb_data *data;
2320 ev.crossing = *event;
2321 subwidget = gtk_get_event_widget (&ev);
2322 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2323 if (data)
2325 if (! NILP (data->help) && data->cl_data->highlight_cb)
2327 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2328 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2329 (*func) (subwidget, call_data);
2333 return FALSE;
2336 /* Callback called when a menu is destroyed. Used to free data.
2337 W is the widget that is being destroyed (not used).
2338 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2340 static void
2341 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2343 unref_cl_data (client_data);
2346 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2347 must be non-NULL) and can be inserted into a menu item.
2349 Returns the GtkHBox. */
2351 static GtkWidget *
2352 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2354 GtkWidget *wlbl;
2355 GtkWidget *wkey;
2356 GtkWidget *wbox;
2358 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2359 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2360 wlbl = gtk_label_new (utf8_label);
2361 wkey = gtk_label_new (utf8_key);
2363 #if GTK_CHECK_VERSION (3, 14, 0)
2364 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2365 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2366 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2367 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2368 #else
2369 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2370 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2371 #endif
2372 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2373 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2375 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2376 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2377 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2379 return wbox;
2382 /* Make and return a menu item widget with the key to the right.
2383 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2384 UTF8_KEY is the text representing the key binding.
2385 ITEM is the widget_value describing the menu item.
2387 GROUP is an in/out parameter. If the menu item to be created is not
2388 part of any radio menu group, *GROUP contains NULL on entry and exit.
2389 If the menu item to be created is part of a radio menu group, on entry
2390 *GROUP contains the group to use, or NULL if this is the first item
2391 in the group. On exit, *GROUP contains the radio item group.
2393 Unfortunately, keys don't line up as nicely as in Motif,
2394 but the MacOS X version doesn't either, so I guess that is OK. */
2396 static GtkWidget *
2397 make_menu_item (const char *utf8_label,
2398 const char *utf8_key,
2399 widget_value *item,
2400 GSList **group)
2402 GtkWidget *w;
2403 GtkWidget *wtoadd = 0;
2405 /* It has been observed that some menu items have a NULL name field.
2406 This will lead to this function being called with a NULL utf8_label.
2407 GTK crashes on that so we set a blank label. Why there is a NULL
2408 name remains to be investigated. */
2409 if (! utf8_label) utf8_label = " ";
2411 if (utf8_key)
2412 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2414 if (item->button_type == BUTTON_TYPE_TOGGLE)
2416 *group = NULL;
2417 if (utf8_key) w = gtk_check_menu_item_new ();
2418 else w = gtk_check_menu_item_new_with_label (utf8_label);
2419 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2421 else if (item->button_type == BUTTON_TYPE_RADIO)
2423 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2424 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2425 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2426 if (item->selected)
2427 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2429 else
2431 *group = NULL;
2432 if (utf8_key) w = gtk_menu_item_new ();
2433 else w = gtk_menu_item_new_with_label (utf8_label);
2436 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2437 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2439 return w;
2442 /* Create a menu item widget, and connect the callbacks.
2443 ITEM describes the menu item.
2444 F is the frame the created menu belongs to.
2445 SELECT_CB is the callback to use when a menu item is selected.
2446 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2447 CL_DATA points to the callback data to be used for this menu.
2448 GROUP is an in/out parameter. If the menu item to be created is not
2449 part of any radio menu group, *GROUP contains NULL on entry and exit.
2450 If the menu item to be created is part of a radio menu group, on entry
2451 *GROUP contains the group to use, or NULL if this is the first item
2452 in the group. On exit, *GROUP contains the radio item group.
2454 Returns the created GtkWidget. */
2456 static GtkWidget *
2457 xg_create_one_menuitem (widget_value *item,
2458 struct frame *f,
2459 GCallback select_cb,
2460 GCallback highlight_cb,
2461 xg_menu_cb_data *cl_data,
2462 GSList **group)
2464 char *utf8_label;
2465 char *utf8_key;
2466 GtkWidget *w;
2467 xg_menu_item_cb_data *cb_data;
2469 utf8_label = get_utf8_string (item->name);
2470 utf8_key = get_utf8_string (item->key);
2472 w = make_menu_item (utf8_label, utf8_key, item, group);
2474 if (utf8_label) g_free (utf8_label);
2475 if (utf8_key) g_free (utf8_key);
2477 cb_data = xmalloc (sizeof *cb_data);
2479 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2481 cb_data->select_id = 0;
2482 cb_data->help = item->help;
2483 cb_data->cl_data = cl_data;
2484 cb_data->call_data = item->call_data;
2486 g_signal_connect (G_OBJECT (w),
2487 "destroy",
2488 G_CALLBACK (menuitem_destroy_callback),
2489 cb_data);
2491 /* Put cb_data in widget, so we can get at it when modifying menubar */
2492 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2494 /* final item, not a submenu */
2495 if (item->call_data && ! item->contents)
2497 if (select_cb)
2498 cb_data->select_id
2499 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2502 return w;
2505 /* Create a full menu tree specified by DATA.
2506 F is the frame the created menu belongs to.
2507 SELECT_CB is the callback to use when a menu item is selected.
2508 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2509 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2510 If POP_UP_P, create a popup menu.
2511 If MENU_BAR_P, create a menu bar.
2512 TOPMENU is the topmost GtkWidget that others shall be placed under.
2513 It may be NULL, in that case we create the appropriate widget
2514 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2515 CL_DATA is the callback data we shall use for this menu, or NULL
2516 if we haven't set the first callback yet.
2517 NAME is the name to give to the top level menu if this function
2518 creates it. May be NULL to not set any name.
2520 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2521 not NULL.
2523 This function calls itself to create submenus. */
2525 static GtkWidget *
2526 create_menus (widget_value *data,
2527 struct frame *f,
2528 GCallback select_cb,
2529 GCallback deactivate_cb,
2530 GCallback highlight_cb,
2531 bool pop_up_p,
2532 bool menu_bar_p,
2533 GtkWidget *topmenu,
2534 xg_menu_cb_data *cl_data,
2535 const char *name)
2537 widget_value *item;
2538 GtkWidget *wmenu = topmenu;
2539 GSList *group = NULL;
2541 if (! topmenu)
2543 if (! menu_bar_p)
2545 wmenu = gtk_menu_new ();
2546 xg_set_screen (wmenu, f);
2547 /* Connect this to the menu instead of items so we get enter/leave for
2548 disabled items also. TODO: Still does not get enter/leave for
2549 disabled items in detached menus. */
2550 g_signal_connect (G_OBJECT (wmenu),
2551 "enter-notify-event",
2552 G_CALLBACK (menuitem_highlight_callback),
2553 NULL);
2554 g_signal_connect (G_OBJECT (wmenu),
2555 "leave-notify-event",
2556 G_CALLBACK (menuitem_highlight_callback),
2557 NULL);
2559 else
2561 wmenu = gtk_menu_bar_new ();
2562 /* Set width of menu bar to a small value so it doesn't enlarge
2563 a small initial frame size. The width will be set to the
2564 width of the frame later on when it is added to a container.
2565 height -1: Natural height. */
2566 gtk_widget_set_size_request (wmenu, 1, -1);
2569 /* Put cl_data on the top menu for easier access. */
2570 cl_data = make_cl_data (cl_data, f, highlight_cb);
2571 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2572 g_signal_connect (G_OBJECT (wmenu), "destroy",
2573 G_CALLBACK (menu_destroy_callback), cl_data);
2575 if (name)
2576 gtk_widget_set_name (wmenu, name);
2578 if (deactivate_cb)
2579 g_signal_connect (G_OBJECT (wmenu),
2580 "selection-done", deactivate_cb, 0);
2583 for (item = data; item; item = item->next)
2585 GtkWidget *w;
2587 if (pop_up_p && !item->contents && !item->call_data
2588 && !menu_separator_name_p (item->name))
2590 char *utf8_label;
2591 /* A title for a popup. We do the same as GTK does when
2592 creating titles, but it does not look good. */
2593 group = NULL;
2594 utf8_label = get_utf8_string (item->name);
2596 w = gtk_menu_item_new_with_label (utf8_label);
2597 gtk_widget_set_sensitive (w, FALSE);
2598 if (utf8_label) g_free (utf8_label);
2600 else if (menu_separator_name_p (item->name))
2602 group = NULL;
2603 /* GTK only have one separator type. */
2604 w = gtk_separator_menu_item_new ();
2606 else
2608 w = xg_create_one_menuitem (item,
2610 item->contents ? 0 : select_cb,
2611 highlight_cb,
2612 cl_data,
2613 &group);
2615 /* Create a possibly empty submenu for menu bar items, since some
2616 themes don't highlight items correctly without it. */
2617 if (item->contents || menu_bar_p)
2619 GtkWidget *submenu = create_menus (item->contents,
2621 select_cb,
2622 deactivate_cb,
2623 highlight_cb,
2627 cl_data,
2629 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2633 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2634 gtk_widget_set_name (w, MENU_ITEM_NAME);
2637 return wmenu;
2640 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2641 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2642 with some text and buttons.
2643 F is the frame the created item belongs to.
2644 NAME is the name to use for the top widget.
2645 VAL is a widget_value structure describing items to be created.
2646 SELECT_CB is the callback to use when a menu item is selected or
2647 a dialog button is pressed.
2648 DEACTIVATE_CB is the callback to use when an item is deactivated.
2649 For a menu, when a sub menu is not shown anymore, for a dialog it is
2650 called when the dialog is popped down.
2651 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2653 Returns the widget created. */
2655 GtkWidget *
2656 xg_create_widget (const char *type, const char *name, struct frame *f,
2657 widget_value *val, GCallback select_cb,
2658 GCallback deactivate_cb, GCallback highlight_cb)
2660 GtkWidget *w = 0;
2661 bool menu_bar_p = strcmp (type, "menubar") == 0;
2662 bool pop_up_p = strcmp (type, "popup") == 0;
2664 if (strcmp (type, "dialog") == 0)
2666 w = create_dialog (val, select_cb, deactivate_cb);
2667 xg_set_screen (w, f);
2668 gtk_window_set_transient_for (GTK_WINDOW (w),
2669 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2670 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2671 gtk_widget_set_name (w, "emacs-dialog");
2672 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2674 else if (menu_bar_p || pop_up_p)
2676 w = create_menus (val->contents,
2678 select_cb,
2679 deactivate_cb,
2680 highlight_cb,
2681 pop_up_p,
2682 menu_bar_p,
2685 name);
2687 /* Set the cursor to an arrow for popup menus when they are mapped.
2688 This is done by default for menu bar menus. */
2689 if (pop_up_p)
2691 /* Must realize so the GdkWindow inside the widget is created. */
2692 gtk_widget_realize (w);
2693 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2696 else
2698 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2699 type);
2702 return w;
2705 /* Return the label for menu item WITEM. */
2707 static const char *
2708 xg_get_menu_item_label (GtkMenuItem *witem)
2710 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2711 return gtk_label_get_label (wlabel);
2714 /* Return true if the menu item WITEM has the text LABEL. */
2716 static bool
2717 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2719 bool is_same = 0;
2720 char *utf8_label = get_utf8_string (label);
2721 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2723 if (! old_label && ! utf8_label)
2724 is_same = 1;
2725 else if (old_label && utf8_label)
2726 is_same = strcmp (utf8_label, old_label) == 0;
2728 if (utf8_label) g_free (utf8_label);
2730 return is_same;
2733 /* Destroy widgets in LIST. */
2735 static void
2736 xg_destroy_widgets (GList *list)
2738 GList *iter;
2740 for (iter = list; iter; iter = g_list_next (iter))
2742 GtkWidget *w = GTK_WIDGET (iter->data);
2744 /* Destroying the widget will remove it from the container it is in. */
2745 gtk_widget_destroy (w);
2749 /* Update the top level names in MENUBAR (i.e. not submenus).
2750 F is the frame the menu bar belongs to.
2751 *LIST is a list with the current menu bar names (menu item widgets).
2752 ITER is the item within *LIST that shall be updated.
2753 POS is the numerical position, starting at 0, of ITER in *LIST.
2754 VAL describes what the menu bar shall look like after the update.
2755 SELECT_CB is the callback to use when a menu item is selected.
2756 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2757 CL_DATA points to the callback data to be used for this menu bar.
2759 This function calls itself to walk through the menu bar names. */
2761 static void
2762 xg_update_menubar (GtkWidget *menubar,
2763 struct frame *f,
2764 GList **list,
2765 GList *iter,
2766 int pos,
2767 widget_value *val,
2768 GCallback select_cb,
2769 GCallback deactivate_cb,
2770 GCallback highlight_cb,
2771 xg_menu_cb_data *cl_data)
2773 if (! iter && ! val)
2774 return;
2775 else if (iter && ! val)
2777 /* Item(s) have been removed. Remove all remaining items. */
2778 xg_destroy_widgets (iter);
2780 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2781 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2782 gtk_menu_item_new_with_label (""),
2784 /* All updated. */
2785 val = 0;
2786 iter = 0;
2788 else if (! iter && val)
2790 /* Item(s) added. Add all new items in one call. */
2791 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2792 0, 1, menubar, cl_data, 0);
2794 /* All updated. */
2795 val = 0;
2796 iter = 0;
2798 /* Below this neither iter or val is NULL */
2799 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2801 /* This item is still the same, check next item. */
2802 val = val->next;
2803 iter = g_list_next (iter);
2804 ++pos;
2806 else /* This item is changed. */
2808 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2809 GtkMenuItem *witem2 = 0;
2810 bool val_in_menubar = 0;
2811 bool iter_in_new_menubar = 0;
2812 GList *iter2;
2813 widget_value *cur;
2815 /* See if the changed entry (val) is present later in the menu bar */
2816 for (iter2 = iter;
2817 iter2 && ! val_in_menubar;
2818 iter2 = g_list_next (iter2))
2820 witem2 = GTK_MENU_ITEM (iter2->data);
2821 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2824 /* See if the current entry (iter) is present later in the
2825 specification for the new menu bar. */
2826 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2827 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2829 if (val_in_menubar && ! iter_in_new_menubar)
2831 int nr = pos;
2833 /* This corresponds to:
2834 Current: A B C
2835 New: A C
2836 Remove B. */
2838 g_object_ref (G_OBJECT (witem));
2839 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2840 gtk_widget_destroy (GTK_WIDGET (witem));
2842 /* Must get new list since the old changed. */
2843 g_list_free (*list);
2844 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2845 while (nr-- > 0) iter = g_list_next (iter);
2847 else if (! val_in_menubar && ! iter_in_new_menubar)
2849 /* This corresponds to:
2850 Current: A B C
2851 New: A X C
2852 Rename B to X. This might seem to be a strange thing to do,
2853 since if there is a menu under B it will be totally wrong for X.
2854 But consider editing a C file. Then there is a C-mode menu
2855 (corresponds to B above).
2856 If then doing C-x C-f the minibuf menu (X above) replaces the
2857 C-mode menu. When returning from the minibuffer, we get
2858 back the C-mode menu. Thus we do:
2859 Rename B to X (C-mode to minibuf menu)
2860 Rename X to B (minibuf to C-mode menu).
2861 If the X menu hasn't been invoked, the menu under B
2862 is up to date when leaving the minibuffer. */
2863 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2864 char *utf8_label = get_utf8_string (val->name);
2866 /* GTK menu items don't notice when their labels have been
2867 changed from underneath them, so we have to explicitly
2868 use g_object_notify to tell listeners (e.g., a GMenuModel
2869 bridge that might be loaded) that the item's label has
2870 changed. */
2871 gtk_label_set_text (wlabel, utf8_label);
2872 #if GTK_CHECK_VERSION (2, 16, 0)
2873 g_object_notify (G_OBJECT (witem), "label");
2874 #endif
2875 if (utf8_label) g_free (utf8_label);
2876 iter = g_list_next (iter);
2877 val = val->next;
2878 ++pos;
2880 else if (! val_in_menubar && iter_in_new_menubar)
2882 /* This corresponds to:
2883 Current: A B C
2884 New: A X B C
2885 Insert X. */
2887 int nr = pos;
2888 GSList *group = 0;
2889 GtkWidget *w = xg_create_one_menuitem (val,
2891 select_cb,
2892 highlight_cb,
2893 cl_data,
2894 &group);
2896 /* Create a possibly empty submenu for menu bar items, since some
2897 themes don't highlight items correctly without it. */
2898 GtkWidget *submenu = create_menus (NULL, f,
2899 select_cb, deactivate_cb,
2900 highlight_cb,
2901 0, 0, 0, cl_data, 0);
2903 gtk_widget_set_name (w, MENU_ITEM_NAME);
2904 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2905 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2907 g_list_free (*list);
2908 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2909 while (nr-- > 0) iter = g_list_next (iter);
2910 iter = g_list_next (iter);
2911 val = val->next;
2912 ++pos;
2914 else /* if (val_in_menubar && iter_in_new_menubar) */
2916 int nr = pos;
2917 /* This corresponds to:
2918 Current: A B C
2919 New: A C B
2920 Move C before B */
2922 g_object_ref (G_OBJECT (witem2));
2923 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2924 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2925 GTK_WIDGET (witem2), pos);
2926 g_object_unref (G_OBJECT (witem2));
2928 g_list_free (*list);
2929 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2930 while (nr-- > 0) iter = g_list_next (iter);
2931 if (iter) iter = g_list_next (iter);
2932 val = val->next;
2933 ++pos;
2937 /* Update the rest of the menu bar. */
2938 xg_update_menubar (menubar, f, list, iter, pos, val,
2939 select_cb, deactivate_cb, highlight_cb, cl_data);
2942 /* Update the menu item W so it corresponds to VAL.
2943 SELECT_CB is the callback to use when a menu item is selected.
2944 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2945 CL_DATA is the data to set in the widget for menu invocation. */
2947 static void
2948 xg_update_menu_item (widget_value *val,
2949 GtkWidget *w,
2950 GCallback select_cb,
2951 GCallback highlight_cb,
2952 xg_menu_cb_data *cl_data)
2954 GtkWidget *wchild;
2955 GtkLabel *wlbl = 0;
2956 GtkLabel *wkey = 0;
2957 char *utf8_label;
2958 char *utf8_key;
2959 const char *old_label = 0;
2960 const char *old_key = 0;
2961 xg_menu_item_cb_data *cb_data;
2962 bool label_changed = false;
2964 wchild = XG_BIN_CHILD (w);
2965 utf8_label = get_utf8_string (val->name);
2966 utf8_key = get_utf8_string (val->key);
2968 /* See if W is a menu item with a key. See make_menu_item above. */
2969 if (GTK_IS_BOX (wchild))
2971 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2973 wlbl = GTK_LABEL (list->data);
2974 wkey = GTK_LABEL (list->next->data);
2975 g_list_free (list);
2977 if (! utf8_key)
2979 /* Remove the key and keep just the label. */
2980 g_object_ref (G_OBJECT (wlbl));
2981 gtk_container_remove (GTK_CONTAINER (w), wchild);
2982 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2983 g_object_unref (G_OBJECT (wlbl));
2984 wkey = 0;
2988 else /* Just a label. */
2990 wlbl = GTK_LABEL (wchild);
2992 /* Check if there is now a key. */
2993 if (utf8_key)
2995 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2996 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2998 wlbl = GTK_LABEL (list->data);
2999 wkey = GTK_LABEL (list->next->data);
3000 g_list_free (list);
3002 gtk_container_remove (GTK_CONTAINER (w), wchild);
3003 gtk_container_add (GTK_CONTAINER (w), wtoadd);
3007 if (wkey) old_key = gtk_label_get_label (wkey);
3008 if (wlbl) old_label = gtk_label_get_label (wlbl);
3010 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
3012 label_changed = true;
3013 gtk_label_set_text (wkey, utf8_key);
3016 if (! old_label || strcmp (utf8_label, old_label) != 0)
3018 label_changed = true;
3019 gtk_label_set_text (wlbl, utf8_label);
3022 if (utf8_key) g_free (utf8_key);
3023 if (utf8_label) g_free (utf8_label);
3025 if (! val->enabled && gtk_widget_get_sensitive (w))
3026 gtk_widget_set_sensitive (w, FALSE);
3027 else if (val->enabled && ! gtk_widget_get_sensitive (w))
3028 gtk_widget_set_sensitive (w, TRUE);
3030 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
3031 if (cb_data)
3033 cb_data->call_data = val->call_data;
3034 cb_data->help = val->help;
3035 cb_data->cl_data = cl_data;
3037 /* We assume the callback functions don't change. */
3038 if (val->call_data && ! val->contents)
3040 /* This item shall have a select callback. */
3041 if (! cb_data->select_id)
3042 cb_data->select_id
3043 = g_signal_connect (G_OBJECT (w), "activate",
3044 select_cb, cb_data);
3046 else if (cb_data->select_id)
3048 g_signal_handler_disconnect (w, cb_data->select_id);
3049 cb_data->select_id = 0;
3053 #if GTK_CHECK_VERSION (2, 16, 0)
3054 if (label_changed) /* See comment in xg_update_menubar. */
3055 g_object_notify (G_OBJECT (w), "label");
3056 #endif
3059 /* Update the toggle menu item W so it corresponds to VAL. */
3061 static void
3062 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3064 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3067 /* Update the radio menu item W so it corresponds to VAL. */
3069 static void
3070 xg_update_radio_item (widget_value *val, GtkWidget *w)
3072 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3075 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3076 SUBMENU may be NULL, in that case a new menu is created.
3077 F is the frame the menu bar belongs to.
3078 VAL describes the contents of the menu bar.
3079 SELECT_CB is the callback to use when a menu item is selected.
3080 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3081 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3082 CL_DATA is the call back data to use for any newly created items.
3084 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3085 was NULL. */
3087 static GtkWidget *
3088 xg_update_submenu (GtkWidget *submenu,
3089 struct frame *f,
3090 widget_value *val,
3091 GCallback select_cb,
3092 GCallback deactivate_cb,
3093 GCallback highlight_cb,
3094 xg_menu_cb_data *cl_data)
3096 GtkWidget *newsub = submenu;
3097 GList *list = 0;
3098 GList *iter;
3099 widget_value *cur;
3100 GList *first_radio = 0;
3102 if (submenu)
3103 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3105 for (cur = val, iter = list;
3106 cur && iter;
3107 iter = g_list_next (iter), cur = cur->next)
3109 GtkWidget *w = GTK_WIDGET (iter->data);
3111 /* Remember first radio button in a group. If we get a mismatch in
3112 a radio group we must rebuild the whole group so that the connections
3113 in GTK becomes correct. */
3114 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3115 first_radio = iter;
3116 else if (cur->button_type != BUTTON_TYPE_RADIO
3117 && ! GTK_IS_RADIO_MENU_ITEM (w))
3118 first_radio = 0;
3120 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3122 if (! menu_separator_name_p (cur->name))
3123 break;
3125 else if (GTK_IS_CHECK_MENU_ITEM (w))
3127 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3128 break;
3129 xg_update_toggle_item (cur, w);
3130 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3132 else if (GTK_IS_RADIO_MENU_ITEM (w))
3134 if (cur->button_type != BUTTON_TYPE_RADIO)
3135 break;
3136 xg_update_radio_item (cur, w);
3137 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3139 else if (GTK_IS_MENU_ITEM (w))
3141 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3142 GtkWidget *sub;
3144 if (cur->button_type != BUTTON_TYPE_NONE ||
3145 menu_separator_name_p (cur->name))
3146 break;
3148 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3150 sub = gtk_menu_item_get_submenu (witem);
3151 if (sub && ! cur->contents)
3153 /* Not a submenu anymore. */
3154 g_object_ref (G_OBJECT (sub));
3155 remove_submenu (witem);
3156 gtk_widget_destroy (sub);
3158 else if (cur->contents)
3160 GtkWidget *nsub;
3162 nsub = xg_update_submenu (sub, f, cur->contents,
3163 select_cb, deactivate_cb,
3164 highlight_cb, cl_data);
3166 /* If this item just became a submenu, we must set it. */
3167 if (nsub != sub)
3168 gtk_menu_item_set_submenu (witem, nsub);
3171 else
3173 /* Structural difference. Remove everything from here and down
3174 in SUBMENU. */
3175 break;
3179 /* Remove widgets from first structural change. */
3180 if (iter)
3182 /* If we are adding new menu items below, we must remove from
3183 first radio button so that radio groups become correct. */
3184 if (cur && first_radio) xg_destroy_widgets (first_radio);
3185 else xg_destroy_widgets (iter);
3188 if (cur)
3190 /* More items added. Create them. */
3191 newsub = create_menus (cur,
3193 select_cb,
3194 deactivate_cb,
3195 highlight_cb,
3198 submenu,
3199 cl_data,
3203 if (list) g_list_free (list);
3205 return newsub;
3208 /* Update the MENUBAR.
3209 F is the frame the menu bar belongs to.
3210 VAL describes the contents of the menu bar.
3211 If DEEP_P, rebuild all but the top level menu names in
3212 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3213 SELECT_CB is the callback to use when a menu item is selected.
3214 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3215 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3217 void
3218 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3219 widget_value *val, bool deep_p,
3220 GCallback select_cb, GCallback deactivate_cb,
3221 GCallback highlight_cb)
3223 xg_menu_cb_data *cl_data;
3224 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3226 if (! list) return;
3228 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3230 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3231 select_cb, deactivate_cb, highlight_cb, cl_data);
3233 if (deep_p)
3235 widget_value *cur;
3237 /* Update all sub menus.
3238 We must keep the submenus (GTK menu item widgets) since the
3239 X Window in the XEvent that activates the menu are those widgets. */
3241 /* Update cl_data, menu_item things in F may have changed. */
3242 update_cl_data (cl_data, f, highlight_cb);
3244 for (cur = val->contents; cur; cur = cur->next)
3246 GList *iter;
3247 GtkWidget *sub = 0;
3248 GtkWidget *newsub;
3249 GtkMenuItem *witem = 0;
3251 /* Find sub menu that corresponds to val and update it. */
3252 for (iter = list ; iter; iter = g_list_next (iter))
3254 witem = GTK_MENU_ITEM (iter->data);
3255 if (xg_item_label_same_p (witem, cur->name))
3257 sub = gtk_menu_item_get_submenu (witem);
3258 break;
3262 newsub = xg_update_submenu (sub,
3264 cur->contents,
3265 select_cb,
3266 deactivate_cb,
3267 highlight_cb,
3268 cl_data);
3269 /* sub may still be NULL. If we just updated non deep and added
3270 a new menu bar item, it has no sub menu yet. So we set the
3271 newly created sub menu under witem. */
3272 if (newsub != sub && witem != 0)
3274 xg_set_screen (newsub, f);
3275 gtk_menu_item_set_submenu (witem, newsub);
3280 g_list_free (list);
3281 gtk_widget_show_all (menubar);
3284 /* Callback called when the menu bar W is mapped.
3285 Used to find the height of the menu bar if we didn't get it
3286 after showing the widget. */
3288 static void
3289 menubar_map_cb (GtkWidget *w, gpointer user_data)
3291 GtkRequisition req;
3292 struct frame *f = user_data;
3293 gtk_widget_get_preferred_size (w, NULL, &req);
3294 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3296 FRAME_MENUBAR_HEIGHT (f) = req.height;
3297 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3301 /* Recompute all the widgets of frame F, when the menu bar has been
3302 changed. */
3304 void
3305 xg_update_frame_menubar (struct frame *f)
3307 struct x_output *x = f->output_data.x;
3308 GtkRequisition req;
3310 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3311 return;
3313 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3314 return; /* Already done this, happens for frames created invisible. */
3316 block_input ();
3318 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3319 FALSE, FALSE, 0);
3320 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3322 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3323 gtk_widget_show_all (x->menubar_widget);
3324 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3326 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3328 FRAME_MENUBAR_HEIGHT (f) = req.height;
3329 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3331 unblock_input ();
3334 /* Get rid of the menu bar of frame F, and free its storage.
3335 This is used when deleting a frame, and when turning off the menu bar. */
3337 void
3338 free_frame_menubar (struct frame *f)
3340 struct x_output *x = f->output_data.x;
3342 if (x->menubar_widget)
3344 block_input ();
3346 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3347 /* The menubar and its children shall be deleted when removed from
3348 the container. */
3349 x->menubar_widget = 0;
3350 FRAME_MENUBAR_HEIGHT (f) = 0;
3351 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3352 unblock_input ();
3356 bool
3357 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3359 struct x_output *x = f->output_data.x;
3360 GList *iter;
3361 GdkRectangle rec;
3362 GList *list;
3363 GdkDisplay *gdpy;
3364 GdkWindow *gw;
3365 GdkEvent gevent;
3366 GtkWidget *gwdesc;
3368 if (! x->menubar_widget) return 0;
3370 if (! (event->xbutton.x >= 0
3371 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3372 && event->xbutton.y >= 0
3373 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3374 && event->xbutton.same_screen))
3375 return 0;
3377 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3378 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3379 if (! gw) return 0;
3380 gevent.any.window = gw;
3381 gevent.any.type = GDK_NOTHING;
3382 gwdesc = gtk_get_event_widget (&gevent);
3383 if (! gwdesc) return 0;
3384 if (! GTK_IS_MENU_BAR (gwdesc)
3385 && ! GTK_IS_MENU_ITEM (gwdesc)
3386 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3387 return 0;
3389 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3390 if (! list) return 0;
3391 rec.x = event->xbutton.x;
3392 rec.y = event->xbutton.y;
3393 rec.width = 1;
3394 rec.height = 1;
3396 for (iter = list ; iter; iter = g_list_next (iter))
3398 GtkWidget *w = GTK_WIDGET (iter->data);
3399 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3400 break;
3402 g_list_free (list);
3403 return iter != 0;
3408 /***********************************************************************
3409 Scroll bar functions
3410 ***********************************************************************/
3413 /* Setting scroll bar values invokes the callback. Use this variable
3414 to indicate that callback should do nothing. */
3416 bool xg_ignore_gtk_scrollbar;
3418 /* Width and height of scroll bars for the current theme. */
3419 static int scroll_bar_width_for_theme;
3420 static int scroll_bar_height_for_theme;
3422 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3423 may be larger than 32 bits. Keep a mapping from integer index to widget
3424 pointers to get around the 32 bit limitation. */
3426 static struct
3428 GtkWidget **widgets;
3429 ptrdiff_t max_size;
3430 ptrdiff_t used;
3431 } id_to_widget;
3433 /* Grow this much every time we need to allocate more */
3435 #define ID_TO_WIDGET_INCR 32
3437 /* Store the widget pointer W in id_to_widget and return the integer index. */
3439 static ptrdiff_t
3440 xg_store_widget_in_map (GtkWidget *w)
3442 ptrdiff_t i;
3444 if (id_to_widget.max_size == id_to_widget.used)
3446 ptrdiff_t new_size;
3447 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3448 memory_full (SIZE_MAX);
3450 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3451 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3452 new_size, sizeof (GtkWidget *));
3454 for (i = id_to_widget.max_size; i < new_size; ++i)
3455 id_to_widget.widgets[i] = 0;
3456 id_to_widget.max_size = new_size;
3459 /* Just loop over the array and find a free place. After all,
3460 how many scroll bars are we creating? Should be a small number.
3461 The check above guarantees we will find a free place. */
3462 for (i = 0; i < id_to_widget.max_size; ++i)
3464 if (! id_to_widget.widgets[i])
3466 id_to_widget.widgets[i] = w;
3467 ++id_to_widget.used;
3469 return i;
3473 /* Should never end up here */
3474 emacs_abort ();
3477 /* Remove pointer at IDX from id_to_widget.
3478 Called when scroll bar is destroyed. */
3480 static void
3481 xg_remove_widget_from_map (ptrdiff_t idx)
3483 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3485 id_to_widget.widgets[idx] = 0;
3486 --id_to_widget.used;
3490 /* Get the widget pointer at IDX from id_to_widget. */
3492 static GtkWidget *
3493 xg_get_widget_from_map (ptrdiff_t idx)
3495 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3496 return id_to_widget.widgets[idx];
3498 return 0;
3501 static void
3502 update_theme_scrollbar_width (void)
3504 #ifdef HAVE_GTK3
3505 GtkAdjustment *vadj;
3506 #else
3507 GtkObject *vadj;
3508 #endif
3509 GtkWidget *wscroll;
3510 int w = 0, b = 0;
3512 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3513 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3514 g_object_ref_sink (G_OBJECT (wscroll));
3515 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3516 gtk_widget_destroy (wscroll);
3517 g_object_unref (G_OBJECT (wscroll));
3518 w += 2*b;
3519 if (w < 16) w = 16;
3520 scroll_bar_width_for_theme = w;
3523 static void
3524 update_theme_scrollbar_height (void)
3526 #ifdef HAVE_GTK3
3527 GtkAdjustment *hadj;
3528 #else
3529 GtkObject *hadj;
3530 #endif
3531 GtkWidget *wscroll;
3532 int w = 0, b = 0;
3534 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3535 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3536 g_object_ref_sink (G_OBJECT (wscroll));
3537 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3538 gtk_widget_destroy (wscroll);
3539 g_object_unref (G_OBJECT (wscroll));
3540 w += 2*b;
3541 if (w < 12) w = 12;
3542 scroll_bar_height_for_theme = w;
3546 xg_get_default_scrollbar_width (void)
3548 return scroll_bar_width_for_theme;
3552 xg_get_default_scrollbar_height (void)
3554 /* Apparently there's no default height for themes. */
3555 return scroll_bar_width_for_theme;
3558 /* Return the scrollbar id for X Window WID on display DPY.
3559 Return -1 if WID not in id_to_widget. */
3561 ptrdiff_t
3562 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3564 ptrdiff_t idx;
3565 GtkWidget *w;
3567 w = xg_win_to_widget (dpy, wid);
3569 if (w)
3571 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3572 if (id_to_widget.widgets[idx] == w)
3573 return idx;
3576 return -1;
3579 /* Callback invoked when scroll bar WIDGET is destroyed.
3580 DATA is the index into id_to_widget for WIDGET.
3581 We free pointer to last scroll bar values here and remove the index. */
3583 static void
3584 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3586 intptr_t id = (intptr_t) data;
3587 xg_remove_widget_from_map (id);
3590 /* Create a scroll bar widget for frame F. Store the scroll bar
3591 in BAR.
3592 SCROLL_CALLBACK is the callback to invoke when the value of the
3593 bar changes.
3594 END_CALLBACK is the callback to invoke when scrolling ends.
3595 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3596 to set resources for the widget. */
3598 void
3599 xg_create_scroll_bar (struct frame *f,
3600 struct scroll_bar *bar,
3601 GCallback scroll_callback,
3602 GCallback end_callback,
3603 const char *scroll_bar_name)
3605 GtkWidget *wscroll;
3606 GtkWidget *webox;
3607 intptr_t scroll_id;
3608 #ifdef HAVE_GTK3
3609 GtkAdjustment *vadj;
3610 #else
3611 GtkObject *vadj;
3612 #endif
3614 /* Page, step increment values are not so important here, they
3615 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3616 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3617 0.1, 0.1, 0.1);
3619 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3620 webox = gtk_event_box_new ();
3621 gtk_widget_set_name (wscroll, scroll_bar_name);
3622 #ifndef HAVE_GTK3
3623 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3624 #endif
3625 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3627 scroll_id = xg_store_widget_in_map (wscroll);
3629 g_signal_connect (G_OBJECT (wscroll),
3630 "destroy",
3631 G_CALLBACK (xg_gtk_scroll_destroy),
3632 (gpointer) scroll_id);
3633 g_signal_connect (G_OBJECT (wscroll),
3634 "change-value",
3635 scroll_callback,
3636 (gpointer) bar);
3637 g_signal_connect (G_OBJECT (wscroll),
3638 "button-release-event",
3639 end_callback,
3640 (gpointer) bar);
3642 /* The scroll bar widget does not draw on a window of its own. Instead
3643 it draws on the parent window, in this case the edit widget. So
3644 whenever the edit widget is cleared, the scroll bar needs to redraw
3645 also, which causes flicker. Put an event box between the edit widget
3646 and the scroll bar, so the scroll bar instead draws itself on the
3647 event box window. */
3648 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3649 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3652 /* Set the cursor to an arrow. */
3653 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3655 bar->x_window = scroll_id;
3656 bar->horizontal = 0;
3659 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3660 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3661 of the bar changes. END_CALLBACK is the callback to invoke when
3662 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3663 bar. Can be used to set resources for the widget. */
3665 void
3666 xg_create_horizontal_scroll_bar (struct frame *f,
3667 struct scroll_bar *bar,
3668 GCallback scroll_callback,
3669 GCallback end_callback,
3670 const char *scroll_bar_name)
3672 GtkWidget *wscroll;
3673 GtkWidget *webox;
3674 intptr_t scroll_id;
3675 #ifdef HAVE_GTK3
3676 GtkAdjustment *hadj;
3677 #else
3678 GtkObject *hadj;
3679 #endif
3681 /* Page, step increment values are not so important here, they
3682 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3683 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3684 0.1, 0.1, 0.1);
3686 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3687 webox = gtk_event_box_new ();
3688 gtk_widget_set_name (wscroll, scroll_bar_name);
3689 #ifndef HAVE_GTK3
3690 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3691 #endif
3692 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3694 scroll_id = xg_store_widget_in_map (wscroll);
3696 g_signal_connect (G_OBJECT (wscroll),
3697 "destroy",
3698 G_CALLBACK (xg_gtk_scroll_destroy),
3699 (gpointer) scroll_id);
3700 g_signal_connect (G_OBJECT (wscroll),
3701 "change-value",
3702 scroll_callback,
3703 (gpointer) bar);
3704 g_signal_connect (G_OBJECT (wscroll),
3705 "button-release-event",
3706 end_callback,
3707 (gpointer) bar);
3709 /* The scroll bar widget does not draw on a window of its own. Instead
3710 it draws on the parent window, in this case the edit widget. So
3711 whenever the edit widget is cleared, the scroll bar needs to redraw
3712 also, which causes flicker. Put an event box between the edit widget
3713 and the scroll bar, so the scroll bar instead draws itself on the
3714 event box window. */
3715 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3716 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3719 /* Set the cursor to an arrow. */
3720 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3722 bar->x_window = scroll_id;
3723 bar->horizontal = 1;
3726 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3728 void
3729 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3731 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3732 if (w)
3734 GtkWidget *wparent = gtk_widget_get_parent (w);
3735 gtk_widget_destroy (w);
3736 gtk_widget_destroy (wparent);
3737 SET_FRAME_GARBAGED (f);
3741 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3742 in frame F.
3743 TOP/LEFT are the new pixel positions where the bar shall appear.
3744 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3746 void
3747 xg_update_scrollbar_pos (struct frame *f,
3748 ptrdiff_t scrollbar_id,
3749 int top,
3750 int left,
3751 int width,
3752 int height)
3755 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3757 if (wscroll)
3759 GtkWidget *wfixed = f->output_data.x->edit_widget;
3760 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3761 gint msl;
3763 /* Clear out old position. */
3764 int oldx = -1, oldy = -1, oldw, oldh;
3765 if (gtk_widget_get_parent (wparent) == wfixed)
3767 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3768 "x", &oldx, "y", &oldy, NULL);
3769 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3772 /* Move and resize to new values. */
3773 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3774 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3775 if (msl > height)
3777 /* No room. Hide scroll bar as some themes output a warning if
3778 the height is less than the min size. */
3779 gtk_widget_hide (wparent);
3780 gtk_widget_hide (wscroll);
3782 else
3784 gtk_widget_show_all (wparent);
3785 gtk_widget_set_size_request (wscroll, width, height);
3787 gtk_widget_queue_draw (wfixed);
3788 gdk_window_process_all_updates ();
3789 if (oldx != -1 && oldw > 0 && oldh > 0)
3790 /* Clear under old scroll bar position. This must be done after
3791 the gtk_widget_queue_draw and gdk_window_process_all_updates
3792 above. */
3793 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3794 oldx, oldy, oldw, oldh);
3796 /* GTK does not redraw until the main loop is entered again, but
3797 if there are no X events pending we will not enter it. So we sync
3798 here to get some events. */
3800 x_sync (f);
3801 SET_FRAME_GARBAGED (f);
3802 cancel_mouse_face (f);
3807 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3808 in frame F.
3809 TOP/LEFT are the new pixel positions where the bar shall appear.
3810 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3812 void
3813 xg_update_horizontal_scrollbar_pos (struct frame *f,
3814 ptrdiff_t scrollbar_id,
3815 int top,
3816 int left,
3817 int width,
3818 int height)
3821 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3823 if (wscroll)
3825 GtkWidget *wfixed = f->output_data.x->edit_widget;
3826 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3827 gint msl;
3829 /* Clear out old position. */
3830 int oldx = -1, oldy = -1, oldw, oldh;
3831 if (gtk_widget_get_parent (wparent) == wfixed)
3833 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3834 "x", &oldx, "y", &oldy, NULL);
3835 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3838 /* Move and resize to new values. */
3839 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3840 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3841 if (msl > width)
3843 /* No room. Hide scroll bar as some themes output a warning if
3844 the width is less than the min size. */
3845 gtk_widget_hide (wparent);
3846 gtk_widget_hide (wscroll);
3848 else
3850 gtk_widget_show_all (wparent);
3851 gtk_widget_set_size_request (wscroll, width, height);
3853 gtk_widget_queue_draw (wfixed);
3854 gdk_window_process_all_updates ();
3855 if (oldx != -1 && oldw > 0 && oldh > 0)
3856 /* Clear under old scroll bar position. This must be done after
3857 the gtk_widget_queue_draw and gdk_window_process_all_updates
3858 above. */
3859 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3860 oldx, oldy, oldw, oldh);
3862 /* GTK does not redraw until the main loop is entered again, but
3863 if there are no X events pending we will not enter it. So we sync
3864 here to get some events. */
3866 x_sync (f);
3867 SET_FRAME_GARBAGED (f);
3868 cancel_mouse_face (f);
3873 /* Get the current value of the range, truncated to an integer. */
3875 static int
3876 int_gtk_range_get_value (GtkRange *range)
3878 return gtk_range_get_value (range);
3882 /* Set the thumb size and position of scroll bar BAR. We are currently
3883 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3885 void
3886 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3887 int portion,
3888 int position,
3889 int whole)
3891 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3893 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3895 if (wscroll && bar->dragging == -1)
3897 GtkAdjustment *adj;
3898 gdouble shown;
3899 gdouble top;
3900 int size, value;
3901 int old_size;
3902 int new_step;
3903 bool changed = 0;
3905 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3907 if (scroll_bar_adjust_thumb_portion_p)
3909 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3910 line rather than the real portion value. This makes the
3911 thumb less likely to resize and that looks better. */
3912 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3914 /* When the thumb is at the bottom, position == whole.
3915 So we need to increase `whole' to make space for the thumb. */
3916 whole += portion;
3919 if (whole <= 0)
3920 top = 0, shown = 1;
3921 else
3923 top = (gdouble) position / whole;
3924 shown = (gdouble) portion / whole;
3927 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3928 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3930 /* Assume all lines are of equal size. */
3931 new_step = size / max (1, FRAME_LINES (f));
3933 old_size = gtk_adjustment_get_page_size (adj);
3934 if (old_size != size)
3936 int old_step = gtk_adjustment_get_step_increment (adj);
3937 if (old_step != new_step)
3939 gtk_adjustment_set_page_size (adj, size);
3940 gtk_adjustment_set_step_increment (adj, new_step);
3941 /* Assume a page increment is about 95% of the page size */
3942 gtk_adjustment_set_page_increment (adj, size - size / 20);
3943 changed = 1;
3947 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3949 block_input ();
3951 /* gtk_range_set_value invokes the callback. Set
3952 ignore_gtk_scrollbar to make the callback do nothing */
3953 xg_ignore_gtk_scrollbar = 1;
3955 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3956 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3957 else if (changed)
3958 gtk_adjustment_changed (adj);
3960 xg_ignore_gtk_scrollbar = 0;
3962 unblock_input ();
3967 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3968 currently displaying PORTION out of a whole WHOLE, and our position
3969 POSITION. */
3970 void
3971 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3972 int portion,
3973 int position,
3974 int whole)
3976 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3978 if (wscroll && bar->dragging == -1)
3980 GtkAdjustment *adj;
3981 int lower = 0;
3982 int upper = max (whole - 1, 0);
3983 int pagesize = min (upper, max (portion, 0));
3984 int value = max (0, min (position, upper - pagesize));
3985 /* These should be set to something more <portion, whole>
3986 related. */
3987 int page_increment = 4;
3988 int step_increment = 1;
3990 block_input ();
3991 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3992 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
3993 (gdouble) upper, (gdouble) step_increment,
3994 (gdouble) page_increment, (gdouble) pagesize);
3995 gtk_adjustment_changed (adj);
3996 unblock_input ();
4000 /* Return true if EVENT is for a scroll bar in frame F.
4001 When the same X window is used for several Gtk+ widgets, we cannot
4002 say for sure based on the X window alone if an event is for the
4003 frame. This function does additional checks. */
4005 bool
4006 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
4008 bool retval = 0;
4010 if (f && event->type == ButtonPress && event->xbutton.button < 4)
4012 /* Check if press occurred outside the edit widget. */
4013 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
4014 GdkWindow *gwin;
4015 #ifdef HAVE_GTK3
4016 GdkDevice *gdev = gdk_device_manager_get_client_pointer
4017 (gdk_display_get_device_manager (gdpy));
4018 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
4019 #else
4020 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
4021 #endif
4022 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
4024 else if (f
4025 && ((event->type == ButtonRelease && event->xbutton.button < 4)
4026 || event->type == MotionNotify))
4028 /* If we are releasing or moving the scroll bar, it has the grab. */
4029 GtkWidget *w = gtk_grab_get_current ();
4030 retval = w != 0 && GTK_IS_SCROLLBAR (w);
4033 return retval;
4038 /***********************************************************************
4039 Tool bar functions
4040 ***********************************************************************/
4041 /* The key for the data we put in the GtkImage widgets. The data is
4042 the image used by Emacs. We use this to see if we need to update
4043 the GtkImage with a new image. */
4044 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
4046 /* The key for storing the latest modifiers so the activate callback can
4047 get them. */
4048 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4050 /* The key for the data we put in the GtkImage widgets. The data is
4051 the stock name used by Emacs. We use this to see if we need to update
4052 the GtkImage with a new image. */
4053 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4055 /* As above, but this is used for named theme widgets, as opposed to
4056 stock items. */
4057 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4059 /* Callback function invoked when a tool bar item is pressed.
4060 W is the button widget in the tool bar that got pressed,
4061 CLIENT_DATA is an integer that is the index of the button in the
4062 tool bar. 0 is the first button. */
4064 static gboolean
4065 xg_tool_bar_button_cb (GtkWidget *widget,
4066 GdkEventButton *event,
4067 gpointer user_data)
4069 intptr_t state = event->state;
4070 gpointer ptr = (gpointer) state;
4071 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4072 return FALSE;
4076 /* Callback function invoked when a tool bar item is pressed.
4077 W is the button widget in the tool bar that got pressed,
4078 CLIENT_DATA is an integer that is the index of the button in the
4079 tool bar. 0 is the first button. */
4081 static void
4082 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4084 intptr_t idx = (intptr_t) client_data;
4085 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4086 intptr_t mod = (intptr_t) gmod;
4088 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4089 Lisp_Object key, frame;
4090 struct input_event event;
4091 EVENT_INIT (event);
4093 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4094 return;
4096 idx *= TOOL_BAR_ITEM_NSLOTS;
4098 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4099 XSETFRAME (frame, f);
4101 /* We generate two events here. The first one is to set the prefix
4102 to `(tool_bar)', see keyboard.c. */
4103 event.kind = TOOL_BAR_EVENT;
4104 event.frame_or_window = frame;
4105 event.arg = frame;
4106 kbd_buffer_store_event (&event);
4108 event.kind = TOOL_BAR_EVENT;
4109 event.frame_or_window = frame;
4110 event.arg = key;
4111 /* Convert between the modifier bits GDK uses and the modifier bits
4112 Emacs uses. This assumes GDK and X masks are the same, which they are when
4113 this is written. */
4114 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4115 kbd_buffer_store_event (&event);
4117 /* Return focus to the frame after we have clicked on a detached
4118 tool bar button. */
4119 x_focus_frame (f);
4122 static GtkWidget *
4123 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4125 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4126 GtkWidget *c1 = clist->data;
4127 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4129 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4130 g_list_free (clist);
4131 return GTK_IS_LABEL (c1) ? c1 : c2;
4135 /* This callback is called when the mouse enters or leaves a tool bar item.
4136 It is used for displaying and hiding the help text.
4137 W is the tool bar item, a button.
4138 EVENT is either an enter event or leave event.
4139 CLIENT_DATA is an integer that is the index of the button in the
4140 tool bar. 0 is the first button.
4142 Returns FALSE to tell GTK to keep processing this event. */
4144 static gboolean
4145 xg_tool_bar_help_callback (GtkWidget *w,
4146 GdkEventCrossing *event,
4147 gpointer client_data)
4149 intptr_t idx = (intptr_t) client_data;
4150 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4151 Lisp_Object help, frame;
4153 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4154 return FALSE;
4156 if (event->type == GDK_ENTER_NOTIFY)
4158 idx *= TOOL_BAR_ITEM_NSLOTS;
4159 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4161 if (NILP (help))
4162 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4164 else
4165 help = Qnil;
4167 XSETFRAME (frame, f);
4168 kbd_buffer_store_help_event (frame, help);
4170 return FALSE;
4174 /* This callback is called when a tool bar item shall be redrawn.
4175 It modifies the expose event so that the GtkImage widget redraws the
4176 whole image. This to overcome a bug that makes GtkImage draw the image
4177 in the wrong place when it tries to redraw just a part of the image.
4178 W is the GtkImage to be redrawn.
4179 EVENT is the expose event for W.
4180 CLIENT_DATA is unused.
4182 Returns FALSE to tell GTK to keep processing this event. */
4184 #ifndef HAVE_GTK3
4185 static gboolean
4186 xg_tool_bar_item_expose_callback (GtkWidget *w,
4187 GdkEventExpose *event,
4188 gpointer client_data)
4190 gint width, height;
4192 gdk_drawable_get_size (event->window, &width, &height);
4193 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4194 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4196 event->area.x = max (0, event->area.x);
4197 event->area.y = max (0, event->area.y);
4199 event->area.width = max (width, event->area.width);
4200 event->area.height = max (height, event->area.height);
4202 return FALSE;
4204 #endif
4206 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4207 #define toolbar_set_orientation(w, o) \
4208 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4209 #else
4210 #define toolbar_set_orientation(w, o) \
4211 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4212 #endif
4214 /* Attach a tool bar to frame F. */
4216 static void
4217 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4219 struct x_output *x = f->output_data.x;
4220 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4221 GtkWidget *top_widget = x->toolbar_widget;
4223 toolbar_set_orientation (x->toolbar_widget,
4224 into_hbox
4225 ? GTK_ORIENTATION_VERTICAL
4226 : GTK_ORIENTATION_HORIZONTAL);
4228 if (into_hbox)
4230 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4231 FALSE, FALSE, 0);
4233 if (EQ (pos, Qleft))
4234 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4235 top_widget,
4237 x->toolbar_in_hbox = true;
4239 else
4241 bool vbox_pos = x->menubar_widget != 0;
4242 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4243 FALSE, FALSE, 0);
4245 if (EQ (pos, Qtop))
4246 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4247 top_widget,
4248 vbox_pos);
4249 x->toolbar_in_hbox = false;
4251 x->toolbar_is_packed = true;
4254 static bool xg_update_tool_bar_sizes (struct frame *f);
4256 static void
4257 tb_size_cb (GtkWidget *widget,
4258 GdkRectangle *allocation,
4259 gpointer user_data)
4261 /* When tool bar is created it has one preferred size. But when size is
4262 allocated between widgets, it may get another. So we must update
4263 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4264 struct frame *f = user_data;
4266 if (xg_update_tool_bar_sizes (f))
4268 frame_size_history_add (f, Qtb_size_cb, 0, 0, Qnil);
4269 adjust_frame_size (f, -1, -1, 5, 0, Qtool_bar_lines);
4273 /* Create a tool bar for frame F. */
4275 static void
4276 xg_create_tool_bar (struct frame *f)
4278 struct x_output *x = f->output_data.x;
4279 #if GTK_CHECK_VERSION (3, 3, 6)
4280 GtkStyleContext *gsty;
4281 #endif
4282 struct xg_frame_tb_info *tbinfo
4283 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4284 TB_INFO_KEY);
4285 if (! tbinfo)
4287 tbinfo = xmalloc (sizeof (*tbinfo));
4288 tbinfo->last_tool_bar = Qnil;
4289 tbinfo->style = Qnil;
4290 tbinfo->hmargin = tbinfo->vmargin = 0;
4291 tbinfo->dir = GTK_TEXT_DIR_NONE;
4292 tbinfo->n_last_items = 0;
4293 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4294 TB_INFO_KEY,
4295 tbinfo);
4298 x->toolbar_widget = gtk_toolbar_new ();
4300 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4302 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4303 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4304 g_signal_connect (x->toolbar_widget, "size-allocate",
4305 G_CALLBACK (tb_size_cb), f);
4306 #if GTK_CHECK_VERSION (3, 3, 6)
4307 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4308 gtk_style_context_add_class (gsty, "primary-toolbar");
4309 #endif
4313 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4315 /* Find the right-to-left image named by RTL in the tool bar images for F.
4316 Returns IMAGE if RTL is not found. */
4318 static Lisp_Object
4319 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4321 int i;
4322 Lisp_Object file, rtl_name;
4323 struct gcpro gcpro1, gcpro2;
4324 GCPRO2 (file, rtl_name);
4326 rtl_name = Ffile_name_nondirectory (rtl);
4328 for (i = 0; i < f->n_tool_bar_items; ++i)
4330 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4331 if (!NILP (file = file_for_image (rtl_image)))
4333 file = call1 (intern ("file-name-sans-extension"),
4334 Ffile_name_nondirectory (file));
4335 if (! NILP (Fequal (file, rtl_name)))
4337 image = rtl_image;
4338 break;
4343 return image;
4346 static GtkToolItem *
4347 xg_make_tool_item (struct frame *f,
4348 GtkWidget *wimage,
4349 GtkWidget **wbutton,
4350 const char *label,
4351 int i, bool horiz, bool text_image)
4353 GtkToolItem *ti = gtk_tool_item_new ();
4354 GtkWidget *vb = gtk_box_new (horiz
4355 ? GTK_ORIENTATION_HORIZONTAL
4356 : GTK_ORIENTATION_VERTICAL,
4358 GtkWidget *wb = gtk_button_new ();
4359 /* The eventbox is here so we can have tooltips on disabled items. */
4360 GtkWidget *weventbox = gtk_event_box_new ();
4361 #if GTK_CHECK_VERSION (3, 3, 6)
4362 GtkCssProvider *css_prov = gtk_css_provider_new ();
4363 GtkStyleContext *gsty;
4365 gtk_css_provider_load_from_data (css_prov,
4366 "GtkEventBox {"
4367 " background-color: transparent;"
4368 "}",
4369 -1, NULL);
4371 gsty = gtk_widget_get_style_context (weventbox);
4372 gtk_style_context_add_provider (gsty,
4373 GTK_STYLE_PROVIDER (css_prov),
4374 GTK_STYLE_PROVIDER_PRIORITY_USER);
4375 g_object_unref (css_prov);
4376 #endif
4378 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4380 if (wimage && !text_image)
4381 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4382 if (label)
4383 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4384 if (wimage && text_image)
4385 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4387 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4388 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4389 gtk_container_add (GTK_CONTAINER (wb), vb);
4390 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4391 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4393 if (wimage || label)
4395 intptr_t ii = i;
4396 gpointer gi = (gpointer) ii;
4398 g_signal_connect (G_OBJECT (wb), "clicked",
4399 G_CALLBACK (xg_tool_bar_callback),
4400 gi);
4402 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4404 #ifndef HAVE_GTK3
4405 /* Catch expose events to overcome an annoying redraw bug, see
4406 comment for xg_tool_bar_item_expose_callback. */
4407 g_signal_connect (G_OBJECT (ti),
4408 "expose-event",
4409 G_CALLBACK (xg_tool_bar_item_expose_callback),
4411 #endif
4412 gtk_tool_item_set_homogeneous (ti, FALSE);
4414 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4415 no distinction based on modifiers in the activate callback,
4416 so we have to do it ourselves. */
4417 g_signal_connect (wb, "button-release-event",
4418 G_CALLBACK (xg_tool_bar_button_cb),
4419 NULL);
4421 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4423 /* Use enter/leave notify to show help. We use the events
4424 rather than the GtkButton specific signals "enter" and
4425 "leave", so we can have only one callback. The event
4426 will tell us what kind of event it is. */
4427 g_signal_connect (G_OBJECT (weventbox),
4428 "enter-notify-event",
4429 G_CALLBACK (xg_tool_bar_help_callback),
4430 gi);
4431 g_signal_connect (G_OBJECT (weventbox),
4432 "leave-notify-event",
4433 G_CALLBACK (xg_tool_bar_help_callback),
4434 gi);
4437 if (wbutton) *wbutton = wb;
4439 return ti;
4442 static bool
4443 is_box_type (GtkWidget *vb, bool is_horizontal)
4445 #ifdef HAVE_GTK3
4446 bool ret = 0;
4447 if (GTK_IS_BOX (vb))
4449 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4450 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4451 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4453 return ret;
4454 #else
4455 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4456 #endif
4460 static bool
4461 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4462 const char *icon_name, const struct image *img,
4463 const char *label, bool horiz)
4465 gpointer old;
4466 GtkWidget *wimage;
4467 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4468 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4470 /* Check if the tool icon matches. */
4471 if (stock_name && wimage)
4473 old = g_object_get_data (G_OBJECT (wimage),
4474 XG_TOOL_BAR_STOCK_NAME);
4475 if (!old || strcmp (old, stock_name))
4476 return 1;
4478 else if (icon_name && wimage)
4480 old = g_object_get_data (G_OBJECT (wimage),
4481 XG_TOOL_BAR_ICON_NAME);
4482 if (!old || strcmp (old, icon_name))
4483 return 1;
4485 else if (wimage)
4487 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4488 XG_TOOL_BAR_IMAGE_DATA);
4489 Pixmap old_img = (Pixmap) gold_img;
4490 if (old_img != img->pixmap)
4491 return 1;
4494 /* Check button configuration and label. */
4495 if (is_box_type (vb, horiz)
4496 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4497 return 1;
4499 /* Ensure label is correct. */
4500 if (label && wlbl)
4501 gtk_label_set_text (GTK_LABEL (wlbl), label);
4502 return 0;
4505 static bool
4506 xg_update_tool_bar_sizes (struct frame *f)
4508 struct x_output *x = f->output_data.x;
4509 GtkRequisition req;
4510 int nl = 0, nr = 0, nt = 0, nb = 0;
4511 GtkWidget *top_widget = x->toolbar_widget;
4513 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4514 if (x->toolbar_in_hbox)
4516 int pos;
4517 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4518 top_widget,
4519 "position", &pos, NULL);
4520 if (pos == 0) nl = req.width;
4521 else nr = req.width;
4523 else
4525 int pos;
4526 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4527 top_widget,
4528 "position", &pos, NULL);
4529 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4530 else nb = req.height;
4533 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4534 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4535 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4536 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4538 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4539 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4540 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4541 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4542 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4543 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4545 return true;
4547 else
4548 return false;
4551 static char *
4552 find_icon_from_name (char *name,
4553 GtkIconTheme *icon_theme,
4554 char **icon_name)
4556 #if ! GTK_CHECK_VERSION (3, 10, 0)
4557 GtkStockItem stock_item;
4558 #endif
4560 if (name[0] == 'n' && name[1] == ':')
4562 *icon_name = name + 2;
4563 name = NULL;
4565 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4566 *icon_name = NULL;
4569 #if ! GTK_CHECK_VERSION (3, 10, 0)
4570 else if (gtk_stock_lookup (name, &stock_item))
4571 *icon_name = NULL;
4572 #endif
4573 else if (gtk_icon_theme_has_icon (icon_theme, name))
4575 *icon_name = name;
4576 name = NULL;
4578 else
4580 name = NULL;
4581 *icon_name = NULL;
4584 return name;
4588 /* Update the tool bar for frame F. Add new buttons and remove old. */
4590 void
4591 update_frame_tool_bar (struct frame *f)
4593 int i, j;
4594 struct x_output *x = f->output_data.x;
4595 int hmargin = 0, vmargin = 0;
4596 GtkToolbar *wtoolbar;
4597 GtkToolItem *ti;
4598 GtkTextDirection dir;
4599 Lisp_Object style;
4600 bool text_image, horiz;
4601 struct xg_frame_tb_info *tbinfo;
4602 GdkScreen *screen;
4603 GtkIconTheme *icon_theme;
4606 if (! FRAME_GTK_WIDGET (f))
4607 return;
4609 block_input ();
4611 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4613 hmargin = XFASTINT (Vtool_bar_button_margin);
4614 vmargin = XFASTINT (Vtool_bar_button_margin);
4616 else if (CONSP (Vtool_bar_button_margin))
4618 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4619 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4621 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4622 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4625 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4626 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4627 i.e. zero. This means that margins less than
4628 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4629 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4630 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4632 if (! x->toolbar_widget)
4633 xg_create_tool_bar (f);
4635 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4636 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4638 style = Ftool_bar_get_system_style ();
4639 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4640 icon_theme = gtk_icon_theme_get_for_screen (screen);
4642 /* Are we up to date? */
4643 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4644 TB_INFO_KEY);
4646 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4647 && tbinfo->n_last_items == f->n_tool_bar_items
4648 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4649 && tbinfo->dir == dir
4650 && ! NILP (Fequal (tbinfo->style, style))
4651 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4653 unblock_input ();
4654 return;
4657 tbinfo->last_tool_bar = f->tool_bar_items;
4658 tbinfo->n_last_items = f->n_tool_bar_items;
4659 tbinfo->style = style;
4660 tbinfo->hmargin = hmargin;
4661 tbinfo->vmargin = vmargin;
4662 tbinfo->dir = dir;
4664 text_image = EQ (style, Qtext_image_horiz);
4665 horiz = EQ (style, Qboth_horiz) || text_image;
4667 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4669 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4670 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4671 int idx;
4672 ptrdiff_t img_id;
4673 int icon_size = 0;
4674 struct image *img = NULL;
4675 Lisp_Object image;
4676 Lisp_Object stock = Qnil;
4677 char *stock_name = NULL;
4678 char *icon_name = NULL;
4679 Lisp_Object rtl;
4680 GtkWidget *wbutton = NULL;
4681 Lisp_Object specified_file;
4682 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4683 const char *label
4684 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4685 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4686 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4687 : "";
4689 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4691 /* If this is a separator, use a gtk separator item. */
4692 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4694 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4696 if (ti)
4697 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4698 GTK_WIDGET (ti));
4699 ti = gtk_separator_tool_item_new ();
4700 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4702 j++;
4703 continue;
4706 /* Otherwise, the tool-bar item is an ordinary button. */
4708 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4710 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4711 ti = NULL;
4714 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4716 /* Ignore invalid image specifications. */
4717 image = PROP (TOOL_BAR_ITEM_IMAGES);
4718 if (!valid_image_p (image))
4720 if (ti)
4721 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4722 GTK_WIDGET (ti));
4723 continue;
4726 specified_file = file_for_image (image);
4727 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4728 stock = call1 (Qx_gtk_map_stock, specified_file);
4730 if (CONSP (stock))
4732 Lisp_Object tem;
4733 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4734 if (! NILP (tem) && STRINGP (XCAR (tem)))
4736 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4737 icon_theme,
4738 &icon_name);
4739 if (stock_name || icon_name) break;
4742 else if (STRINGP (stock))
4744 stock_name = find_icon_from_name (SSDATA (stock),
4745 icon_theme,
4746 &icon_name);
4749 if (stock_name || icon_name)
4750 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4752 if (stock_name == NULL && icon_name == NULL)
4754 /* No stock image, or stock item not known. Try regular
4755 image. If image is a vector, choose it according to the
4756 button state. */
4757 if (dir == GTK_TEXT_DIR_RTL
4758 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4759 && STRINGP (rtl))
4760 image = find_rtl_image (f, image, rtl);
4762 if (VECTORP (image))
4764 if (enabled_p)
4765 idx = (selected_p
4766 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4767 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4768 else
4769 idx = (selected_p
4770 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4771 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4773 eassert (ASIZE (image) >= idx);
4774 image = AREF (image, idx);
4776 else
4777 idx = -1;
4779 img_id = lookup_image (f, image);
4780 img = IMAGE_FROM_ID (f, img_id);
4781 prepare_image_for_display (f, img);
4783 if (img->load_failed_p || img->pixmap == None)
4785 if (ti)
4786 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4787 GTK_WIDGET (ti));
4788 continue;
4792 /* If there is an existing widget, check if it's stale; if so,
4793 remove it and make a new tool item from scratch. */
4794 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4795 img, label, horiz))
4797 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4798 GTK_WIDGET (ti));
4799 ti = NULL;
4802 if (ti == NULL)
4804 GtkWidget *w;
4806 /* Save the image so we can see if an update is needed the
4807 next time we call xg_tool_item_match_p. */
4808 if (EQ (style, Qtext))
4809 w = NULL;
4810 else if (stock_name)
4813 #if GTK_CHECK_VERSION (3, 10, 0)
4814 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4815 #else
4816 w = gtk_image_new_from_stock (stock_name, icon_size);
4817 #endif
4818 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4819 (gpointer) xstrdup (stock_name),
4820 (GDestroyNotify) xfree);
4822 else if (icon_name)
4824 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4825 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4826 (gpointer) xstrdup (icon_name),
4827 (GDestroyNotify) xfree);
4829 else
4831 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4832 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4833 (gpointer)img->pixmap);
4836 #if GTK_CHECK_VERSION (3, 14, 0)
4837 if (w)
4839 gtk_widget_set_margin_start (w, hmargin);
4840 gtk_widget_set_margin_end (w, hmargin);
4841 gtk_widget_set_margin_top (w, vmargin);
4842 gtk_widget_set_margin_bottom (w, vmargin);
4844 #else
4845 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4846 #endif
4847 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4848 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4851 #undef PROP
4853 gtk_widget_set_sensitive (wbutton, enabled_p);
4854 j++;
4857 /* Remove buttons not longer needed. */
4860 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4861 if (ti)
4862 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4863 } while (ti != NULL);
4865 if (f->n_tool_bar_items != 0)
4867 if (! x->toolbar_is_packed)
4868 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4869 gtk_widget_show_all (x->toolbar_widget);
4870 if (xg_update_tool_bar_sizes (f))
4872 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
4873 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4877 unblock_input ();
4880 /* Deallocate all resources for the tool bar on frame F.
4881 Remove the tool bar. */
4883 void
4884 free_frame_tool_bar (struct frame *f)
4886 struct x_output *x = f->output_data.x;
4888 if (x->toolbar_widget)
4890 struct xg_frame_tb_info *tbinfo;
4891 GtkWidget *top_widget = x->toolbar_widget;
4893 block_input ();
4894 if (x->toolbar_is_packed)
4896 if (x->toolbar_in_hbox)
4897 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4898 top_widget);
4899 else
4900 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4901 top_widget);
4903 else
4904 gtk_widget_destroy (x->toolbar_widget);
4906 x->toolbar_widget = 0;
4907 x->toolbar_widget = 0;
4908 x->toolbar_is_packed = false;
4909 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4910 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4912 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4913 TB_INFO_KEY);
4914 if (tbinfo)
4916 xfree (tbinfo);
4917 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4918 TB_INFO_KEY,
4919 NULL);
4922 frame_size_history_add (f, Qfree_frame_tool_bar, 0, 0, Qnil);
4923 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4925 unblock_input ();
4929 void
4930 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4932 struct x_output *x = f->output_data.x;
4933 GtkWidget *top_widget = x->toolbar_widget;
4935 if (! x->toolbar_widget || ! top_widget)
4936 return;
4938 block_input ();
4939 g_object_ref (top_widget);
4940 if (x->toolbar_is_packed)
4942 if (x->toolbar_in_hbox)
4943 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4944 top_widget);
4945 else
4946 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4947 top_widget);
4950 xg_pack_tool_bar (f, pos);
4951 g_object_unref (top_widget);
4953 if (xg_update_tool_bar_sizes (f))
4955 frame_size_history_add (f, Qxg_change_toolbar_position, 0, 0, Qnil);
4956 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4960 unblock_input ();
4965 /***********************************************************************
4966 Initializing
4967 ***********************************************************************/
4968 void
4969 xg_initialize (void)
4971 GtkBindingSet *binding_set;
4972 GtkSettings *settings;
4974 #if HAVE_XFT
4975 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4976 we keep it permanently linked in. */
4977 XftInit (0);
4978 #endif
4980 gdpy_def = NULL;
4981 xg_ignore_gtk_scrollbar = 0;
4982 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4983 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4985 id_to_widget.max_size = id_to_widget.used = 0;
4986 id_to_widget.widgets = 0;
4988 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4989 (gdk_display_get_default ()));
4990 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4991 bindings. It doesn't seem to be any way to remove properties,
4992 so we set it to "" which in means "no key". */
4993 gtk_settings_set_string_property (settings,
4994 "gtk-menu-bar-accel",
4996 EMACS_CLASS);
4998 /* Make GTK text input widgets use Emacs style keybindings. This is
4999 Emacs after all. */
5000 gtk_settings_set_string_property (settings,
5001 "gtk-key-theme-name",
5002 "Emacs",
5003 EMACS_CLASS);
5005 /* Make dialogs close on C-g. Since file dialog inherits from
5006 dialog, this works for them also. */
5007 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
5008 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5009 "close", 0);
5011 /* Make menus close on C-g. */
5012 binding_set = gtk_binding_set_by_class (g_type_class_ref
5013 (GTK_TYPE_MENU_SHELL));
5014 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5015 "cancel", 0);
5016 update_theme_scrollbar_width ();
5017 update_theme_scrollbar_height ();
5019 #ifdef HAVE_FREETYPE
5020 x_last_font_name = NULL;
5021 #endif
5024 #endif /* USE_GTK */