Merge from origin/emacs-24
[emacs.git] / src / gtkutil.c
blob9465d5479dfe9e21b8e2e43899b56a7e4ccaf1b0
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2014 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 /** #define FRAME_TOTAL_PIXEL_HEIGHT(f) \ **/
54 /** (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)) **/
56 /** #define FRAME_TOTAL_PIXEL_WIDTH(f) \ **/
57 /** (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f)) **/
59 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
60 #define gtk_widget_set_has_window(w, b) \
61 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
62 #endif
63 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
64 #define gtk_dialog_get_action_area(w) ((w)->action_area)
65 #define gtk_dialog_get_content_area(w) ((w)->vbox)
66 #endif
67 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
68 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
69 #endif
70 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
71 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
72 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
73 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
74 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
75 #endif
76 #if GTK_CHECK_VERSION (2, 12, 0)
77 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
78 #else
79 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
80 #endif
82 #if ! GTK_CHECK_VERSION (2, 14, 0)
83 #define gtk_adjustment_configure(adj, xvalue, xlower, \
84 xupper, xstep_increment, \
85 xpage_increment, xpagesize) \
86 do { \
87 adj->lower = xlower; \
88 adj->upper = xupper; \
89 adj->page_size = xpagesize; \
90 gtk_adjustment_set_value (adj, xvalue); \
91 adj->page_increment = xpage_increment; \
92 adj->step_increment = xstep_increment; \
93 } while (0)
94 #endif /* < Gtk+ 2.14 */
96 #ifdef HAVE_FREETYPE
97 #if GTK_CHECK_VERSION (3, 2, 0)
98 #define USE_NEW_GTK_FONT_CHOOSER 1
99 #else
100 #define USE_NEW_GTK_FONT_CHOOSER 0
101 #define gtk_font_chooser_dialog_new(x, y) \
102 gtk_font_selection_dialog_new (x)
103 #undef GTK_FONT_CHOOSER
104 #define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
105 #define gtk_font_chooser_set_font(x, y) \
106 gtk_font_selection_dialog_set_font_name (x, y)
107 #endif
108 #endif /* HAVE_FREETYPE */
110 #if GTK_CHECK_VERSION (3, 10, 0)
111 #define XG_TEXT_CANCEL "Cancel"
112 #define XG_TEXT_OK "OK"
113 #define XG_TEXT_OPEN "Open"
114 #else
115 #define XG_TEXT_CANCEL GTK_STOCK_CANCEL
116 #define XG_TEXT_OK GTK_STOCK_OK
117 #define XG_TEXT_OPEN GTK_STOCK_OPEN
118 #endif
120 #ifndef HAVE_GTK3
121 #ifdef USE_GTK_TOOLTIP
122 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
123 #endif
124 #define gdk_window_get_geometry(w, a, b, c, d) \
125 gdk_window_get_geometry (w, a, b, c, d, 0)
126 #define gdk_x11_window_lookup_for_display(d, w) \
127 gdk_xid_table_lookup_for_display (d, w)
128 #define gtk_box_new(ori, spacing) \
129 ((ori) == GTK_ORIENTATION_HORIZONTAL \
130 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
131 #define gtk_scrollbar_new(ori, spacing) \
132 ((ori) == GTK_ORIENTATION_HORIZONTAL \
133 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
134 #ifndef GDK_KEY_g
135 #define GDK_KEY_g GDK_g
136 #endif
137 #endif /* HAVE_GTK3 */
139 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
141 static void update_theme_scrollbar_width (void);
142 static void update_theme_scrollbar_height (void);
144 #define TB_INFO_KEY "xg_frame_tb_info"
145 struct xg_frame_tb_info
147 Lisp_Object last_tool_bar;
148 Lisp_Object style;
149 int n_last_items;
150 int hmargin, vmargin;
151 GtkTextDirection dir;
155 /***********************************************************************
156 Display handling functions
157 ***********************************************************************/
159 /* Keep track of the default display, or NULL if there is none. Emacs
160 may close all its displays. */
162 static GdkDisplay *gdpy_def;
164 /* When the GTK widget W is to be created on a display for F that
165 is not the default display, set the display for W.
166 W can be a GtkMenu or a GtkWindow widget. */
168 static void
169 xg_set_screen (GtkWidget *w, struct frame *f)
171 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
173 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
174 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
176 if (GTK_IS_MENU (w))
177 gtk_menu_set_screen (GTK_MENU (w), gscreen);
178 else
179 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
184 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
185 *DPY is set to NULL if the display can't be opened.
187 Returns non-zero if display could be opened, zero if display could not
188 be opened, and less than zero if the GTK version doesn't support
189 multiple displays. */
191 void
192 xg_display_open (char *display_name, Display **dpy)
194 GdkDisplay *gdpy;
196 gdpy = gdk_display_open (display_name);
197 if (!gdpy_def && gdpy)
199 gdpy_def = gdpy;
200 gdk_display_manager_set_default_display (gdk_display_manager_get (),
201 gdpy);
204 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
208 /* Close display DPY. */
210 void
211 xg_display_close (Display *dpy)
213 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
215 /* If this is the default display, try to change it before closing.
216 If there is no other display to use, gdpy_def is set to NULL, and
217 the next call to xg_display_open resets the default display. */
218 if (gdk_display_get_default () == gdpy)
220 struct x_display_info *dpyinfo;
221 GdkDisplay *gdpy_new = NULL;
223 /* Find another display. */
224 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
225 if (dpyinfo->display != dpy)
227 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
228 gdk_display_manager_set_default_display (gdk_display_manager_get (),
229 gdpy_new);
230 break;
232 gdpy_def = gdpy_new;
235 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
236 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
237 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
238 can continue running, but there will be memory leaks. */
239 g_object_run_dispose (G_OBJECT (gdpy));
240 #else
241 /* This seems to be fixed in GTK 2.10. */
242 gdk_display_close (gdpy);
243 #endif
247 /***********************************************************************
248 Utility functions
249 ***********************************************************************/
251 /* Create and return the cursor to be used for popup menus and
252 scroll bars on display DPY. */
254 GdkCursor *
255 xg_create_default_cursor (Display *dpy)
257 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
258 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
261 static GdkPixbuf *
262 xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
264 int iunused;
265 GdkPixbuf *tmp_buf;
266 Window wunused;
267 unsigned int width, height, uunused;
268 XImage *xim;
270 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
271 &width, &height, &uunused, &uunused);
273 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
274 ~0, XYPixmap);
275 if (!xim) return 0;
277 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
278 GDK_COLORSPACE_RGB,
279 FALSE,
280 xim->bitmap_unit,
281 width,
282 height,
283 xim->bytes_per_line,
284 NULL,
285 NULL);
286 XDestroyImage (xim);
287 return tmp_buf;
290 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
292 static GdkPixbuf *
293 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
294 Pixmap pix,
295 Pixmap mask)
297 int width, height;
298 GdkPixbuf *icon_buf, *tmp_buf;
300 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
301 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
302 g_object_unref (G_OBJECT (tmp_buf));
304 width = gdk_pixbuf_get_width (icon_buf);
305 height = gdk_pixbuf_get_height (icon_buf);
307 if (mask)
309 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
310 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
311 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
312 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
313 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
314 int y;
316 for (y = 0; y < height; ++y)
318 guchar *iconptr, *maskptr;
319 int x;
321 iconptr = pixels + y * rowstride;
322 maskptr = mask_pixels + y * mask_rowstride;
324 for (x = 0; x < width; ++x)
326 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
327 just R is sufficient. */
328 if (maskptr[0] == 0)
329 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
331 iconptr += rowstride/width;
332 maskptr += mask_rowstride/width;
336 g_object_unref (G_OBJECT (mask_buf));
339 return icon_buf;
342 static Lisp_Object
343 file_for_image (Lisp_Object image)
345 Lisp_Object specified_file = Qnil;
346 Lisp_Object tail;
348 for (tail = XCDR (image);
349 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
350 tail = XCDR (XCDR (tail)))
351 if (EQ (XCAR (tail), QCfile))
352 specified_file = XCAR (XCDR (tail));
354 return specified_file;
357 /* For the image defined in IMG, make and return a GtkImage. For displays with
358 8 planes or less we must make a GdkPixbuf and apply the mask manually.
359 Otherwise the highlighting and dimming the tool bar code in GTK does
360 will look bad. For display with more than 8 planes we just use the
361 pixmap and mask directly. For monochrome displays, GTK doesn't seem
362 able to use external pixmaps, it looks bad whatever we do.
363 The image is defined on the display where frame F is.
364 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
365 If OLD_WIDGET is NULL, a new widget is constructed and returned.
366 If OLD_WIDGET is not NULL, that widget is modified. */
368 static GtkWidget *
369 xg_get_image_for_pixmap (struct frame *f,
370 struct image *img,
371 GtkWidget *widget,
372 GtkImage *old_widget)
374 GdkPixbuf *icon_buf;
376 /* If we have a file, let GTK do all the image handling.
377 This seems to be the only way to make insensitive and activated icons
378 look good in all cases. */
379 Lisp_Object specified_file = file_for_image (img->spec);
380 Lisp_Object file;
382 /* We already loaded the image once before calling this
383 function, so this only fails if the image file has been removed.
384 In that case, use the pixmap already loaded. */
386 if (STRINGP (specified_file)
387 && STRINGP (file = x_find_image_file (specified_file)))
389 if (! old_widget)
390 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
391 else
392 gtk_image_set_from_file (old_widget, SSDATA (file));
394 return GTK_WIDGET (old_widget);
397 /* No file, do the image handling ourselves. This will look very bad
398 on a monochrome display, and sometimes bad on all displays with
399 certain themes. */
401 /* This is a workaround to make icons look good on pseudo color
402 displays. Apparently GTK expects the images to have an alpha
403 channel. If they don't, insensitive and activated icons will
404 look bad. This workaround does not work on monochrome displays,
405 and is strictly not needed on true color/static color displays (i.e.
406 16 bits and higher). But we do it anyway so we get a pixbuf that is
407 not associated with the img->pixmap. The img->pixmap may be removed
408 by clearing the image cache and then the tool bar redraw fails, since
409 Gtk+ assumes the pixmap is always there. */
410 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
412 if (icon_buf)
414 if (! old_widget)
415 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
416 else
417 gtk_image_set_from_pixbuf (old_widget, icon_buf);
419 g_object_unref (G_OBJECT (icon_buf));
422 return GTK_WIDGET (old_widget);
426 /* Set CURSOR on W and all widgets W contain. We must do like this
427 for scroll bars and menu because they create widgets internally,
428 and it is those widgets that are visible. */
430 static void
431 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
433 GdkWindow *window = gtk_widget_get_window (w);
434 GList *children = gdk_window_peek_children (window);
436 gdk_window_set_cursor (window, cursor);
438 /* The scroll bar widget has more than one GDK window (had to look at
439 the source to figure this out), and there is no way to set cursor
440 on widgets in GTK. So we must set the cursor for all GDK windows.
441 Ditto for menus. */
443 for ( ; children; children = g_list_next (children))
444 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
447 /* Insert NODE into linked LIST. */
449 static void
450 xg_list_insert (xg_list_node *list, xg_list_node *node)
452 xg_list_node *list_start = list->next;
454 if (list_start) list_start->prev = node;
455 node->next = list_start;
456 node->prev = 0;
457 list->next = node;
460 /* Remove NODE from linked LIST. */
462 static void
463 xg_list_remove (xg_list_node *list, xg_list_node *node)
465 xg_list_node *list_start = list->next;
466 if (node == list_start)
468 list->next = node->next;
469 if (list->next) list->next->prev = 0;
471 else
473 node->prev->next = node->next;
474 if (node->next) node->next->prev = node->prev;
478 /* Allocate and return a utf8 version of STR. If STR is already
479 utf8 or NULL, just return a copy of STR.
480 A new string is allocated and the caller must free the result
481 with g_free. */
483 static char *
484 get_utf8_string (const char *str)
486 char *utf8_str;
488 if (!str) return NULL;
490 /* If not UTF-8, try current locale. */
491 if (!g_utf8_validate (str, -1, NULL))
492 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
493 else
494 return g_strdup (str);
496 if (!utf8_str)
498 /* Probably some control characters in str. Escape them. */
499 ptrdiff_t len;
500 ptrdiff_t nr_bad = 0;
501 gsize bytes_read;
502 gsize bytes_written;
503 unsigned char *p = (unsigned char *)str;
504 char *cp, *up;
505 GError *err = NULL;
507 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
508 &bytes_written, &err))
509 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
511 ++nr_bad;
512 p += bytes_written+1;
513 g_error_free (err);
514 err = NULL;
517 if (err)
519 g_error_free (err);
520 err = NULL;
522 if (cp) g_free (cp);
524 len = strlen (str);
525 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
526 memory_full (SIZE_MAX);
527 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
528 p = (unsigned char *)str;
530 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
531 &bytes_written, &err))
532 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
534 memcpy (up, p, bytes_written);
535 up += bytes_written;
536 up += sprintf (up, "\\%03o", p[bytes_written]);
537 p += bytes_written + 1;
538 g_error_free (err);
539 err = NULL;
542 if (cp)
544 strcpy (up, cp);
545 g_free (cp);
547 if (err)
549 g_error_free (err);
550 err = NULL;
553 return utf8_str;
556 /* Check for special colors used in face spec for region face.
557 The colors are fetched from the Gtk+ theme.
558 Return true if color was found, false if not. */
560 bool
561 xg_check_special_colors (struct frame *f,
562 const char *color_name,
563 XColor *color)
565 bool success_p = 0;
566 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
567 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
569 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
570 return success_p;
572 block_input ();
574 #ifdef HAVE_GTK3
575 GtkStyleContext *gsty
576 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
577 GdkRGBA col;
578 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
579 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
580 if (get_fg)
581 gtk_style_context_get_color (gsty, state, &col);
582 else
583 gtk_style_context_get_background_color (gsty, state, &col);
585 sprintf (buf, "rgb:%04x/%04x/%04x",
586 (int)(col.red * 65535),
587 (int)(col.green * 65535),
588 (int)(col.blue * 65535));
589 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
590 buf, color)
591 != 0);
592 #else
593 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
594 GdkColor *grgb = get_bg
595 ? &gsty->bg[GTK_STATE_SELECTED]
596 : &gsty->fg[GTK_STATE_SELECTED];
598 color->red = grgb->red;
599 color->green = grgb->green;
600 color->blue = grgb->blue;
601 color->pixel = grgb->pixel;
602 success_p = 1;
603 #endif
606 unblock_input ();
607 return success_p;
612 /***********************************************************************
613 Tooltips
614 ***********************************************************************/
615 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
616 We use that to pop down the tooltip. This happens if Gtk+ for some
617 reason wants to change or hide the tooltip. */
619 #ifdef USE_GTK_TOOLTIP
621 static void
622 hierarchy_ch_cb (GtkWidget *widget,
623 GtkWidget *previous_toplevel,
624 gpointer user_data)
626 struct frame *f = user_data;
627 struct x_output *x = f->output_data.x;
628 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
630 if (! top || ! GTK_IS_WINDOW (top))
631 gtk_widget_hide (previous_toplevel);
634 /* Callback called when Gtk+ thinks a tooltip should be displayed.
635 We use it to get the tooltip window and the tooltip widget so
636 we can manipulate the ourselves.
638 Return FALSE ensures that the tooltip is not shown. */
640 static gboolean
641 qttip_cb (GtkWidget *widget,
642 gint xpos,
643 gint ypos,
644 gboolean keyboard_mode,
645 GtkTooltip *tooltip,
646 gpointer user_data)
648 struct frame *f = user_data;
649 struct x_output *x = f->output_data.x;
650 if (x->ttip_widget == NULL)
652 GtkWidget *p;
653 GList *list, *iter;
655 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
656 x->ttip_widget = tooltip;
657 g_object_ref (G_OBJECT (tooltip));
658 x->ttip_lbl = gtk_label_new ("");
659 g_object_ref (G_OBJECT (x->ttip_lbl));
660 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
661 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
663 /* Change stupid Gtk+ default line wrapping. */
664 p = gtk_widget_get_parent (x->ttip_lbl);
665 list = gtk_container_get_children (GTK_CONTAINER (p));
666 for (iter = list; iter; iter = g_list_next (iter))
668 GtkWidget *w = GTK_WIDGET (iter->data);
669 if (GTK_IS_LABEL (w))
670 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
672 g_list_free (list);
674 /* ATK needs an empty title for some reason. */
675 gtk_window_set_title (x->ttip_window, "");
676 /* Realize so we can safely get screen later on. */
677 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
678 gtk_widget_realize (x->ttip_lbl);
680 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
681 G_CALLBACK (hierarchy_ch_cb), f);
683 return FALSE;
686 #endif /* USE_GTK_TOOLTIP */
688 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
689 Return true if a system tooltip is available. */
691 bool
692 xg_prepare_tooltip (struct frame *f,
693 Lisp_Object string,
694 int *width,
695 int *height)
697 #ifndef USE_GTK_TOOLTIP
698 return 0;
699 #else
700 struct x_output *x = f->output_data.x;
701 GtkWidget *widget;
702 GdkWindow *gwin;
703 GdkScreen *screen;
704 GtkSettings *settings;
705 gboolean tt_enabled = TRUE;
706 GtkRequisition req;
707 Lisp_Object encoded_string;
709 if (!x->ttip_lbl) return 0;
711 block_input ();
712 encoded_string = ENCODE_UTF_8 (string);
713 widget = GTK_WIDGET (x->ttip_lbl);
714 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
715 screen = gdk_window_get_screen (gwin);
716 settings = gtk_settings_get_for_screen (screen);
717 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
718 if (tt_enabled)
720 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
721 /* Record that we disabled it so it can be enabled again. */
722 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
723 (gpointer)f);
726 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
727 g_object_set_data (G_OBJECT
728 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
729 "gdk-display-current-tooltip", NULL);
731 /* Put our dummy widget in so we can get callbacks for unrealize and
732 hierarchy-changed. */
733 gtk_tooltip_set_custom (x->ttip_widget, widget);
734 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
735 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
736 if (width) *width = req.width;
737 if (height) *height = req.height;
739 unblock_input ();
741 return 1;
742 #endif /* USE_GTK_TOOLTIP */
745 /* Show the tooltip at ROOT_X and ROOT_Y.
746 xg_prepare_tooltip must have been called before this function. */
748 void
749 xg_show_tooltip (struct frame *f, int root_x, int root_y)
751 #ifdef USE_GTK_TOOLTIP
752 struct x_output *x = f->output_data.x;
753 if (x->ttip_window)
755 block_input ();
756 gtk_window_move (x->ttip_window, root_x, root_y);
757 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
758 unblock_input ();
760 #endif
763 /* Hide tooltip if shown. Do nothing if not shown.
764 Return true if tip was hidden, false if not (i.e. not using
765 system tooltips). */
767 bool
768 xg_hide_tooltip (struct frame *f)
770 bool ret = 0;
771 #ifdef USE_GTK_TOOLTIP
772 if (f->output_data.x->ttip_window)
774 GtkWindow *win = f->output_data.x->ttip_window;
775 block_input ();
776 gtk_widget_hide (GTK_WIDGET (win));
778 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
780 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
781 GdkScreen *screen = gdk_window_get_screen (gwin);
782 GtkSettings *settings = gtk_settings_get_for_screen (screen);
783 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
785 unblock_input ();
787 ret = 1;
789 #endif
790 return ret;
794 /***********************************************************************
795 General functions for creating widgets, resizing, events, e.t.c.
796 ***********************************************************************/
798 static void
799 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
800 const gchar *msg, gpointer user_data)
802 if (!strstr (msg, "visible children"))
803 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
806 /* Make a geometry string and pass that to GTK. It seems this is the
807 only way to get geometry position right if the user explicitly
808 asked for a position when starting Emacs.
809 F is the frame we shall set geometry for. */
811 static void
812 xg_set_geometry (struct frame *f)
814 if (f->size_hint_flags & (USPosition | PPosition))
816 int left = f->left_pos;
817 int xneg = f->size_hint_flags & XNegative;
818 int top = f->top_pos;
819 int yneg = f->size_hint_flags & YNegative;
820 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
821 guint id;
823 if (xneg)
824 left = -left;
825 if (yneg)
826 top = -top;
828 sprintf (geom_str, "=%dx%d%c%d%c%d",
829 FRAME_PIXEL_WIDTH (f),
830 FRAME_PIXEL_HEIGHT (f),
831 (xneg ? '-' : '+'), left,
832 (yneg ? '-' : '+'), top);
834 /* Silence warning about visible children. */
835 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
836 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
838 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
839 geom_str))
840 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
842 g_log_remove_handler ("Gtk", id);
846 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
847 and use a GtkFixed widget, this doesn't happen automatically. */
849 void
850 xg_clear_under_internal_border (struct frame *f)
852 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
854 GtkWidget *wfixed = f->output_data.x->edit_widget;
856 gtk_widget_queue_draw (wfixed);
857 gdk_window_process_all_updates ();
859 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
860 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
862 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
863 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
865 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
866 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
867 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
869 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
870 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
871 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
875 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
876 and a Gtk+ menu bar, we get resize events for the edit part of the
877 frame only. We let Gtk+ deal with the Gtk+ parts.
878 F is the frame to resize.
879 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
881 void
882 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
884 int width, height;
886 if (pixelwidth == -1 && pixelheight == -1)
888 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
889 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
890 0, 0,
891 &pixelwidth, &pixelheight);
892 else return;
896 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
897 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
899 if (width != FRAME_TEXT_WIDTH (f)
900 || height != FRAME_TEXT_HEIGHT (f)
901 || pixelwidth != FRAME_PIXEL_WIDTH (f)
902 || pixelheight != FRAME_PIXEL_HEIGHT (f))
904 FRAME_PIXEL_WIDTH (f) = pixelwidth;
905 FRAME_PIXEL_HEIGHT (f) = pixelheight;
907 xg_clear_under_internal_border (f);
908 change_frame_size (f, width, height, 0, 1, 0, 1);
909 SET_FRAME_GARBAGED (f);
910 cancel_mouse_face (f);
912 do_pending_window_change (0);
916 /* Resize the outer window of frame F after changing the height.
917 COLUMNS/ROWS is the size the edit area shall have after the resize. */
919 void
920 xg_frame_set_char_size (struct frame *f, int width, int height)
922 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
923 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
925 if (FRAME_PIXEL_HEIGHT (f) == 0)
926 return;
928 /* Do this before resize, as we don't know yet if we will be resized. */
929 xg_clear_under_internal_border (f);
931 /* Must resize our top level widget. Font size may have changed,
932 but not rows/cols. */
933 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
934 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
935 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
936 + FRAME_MENUBAR_HEIGHT (f));
937 x_wm_set_size_hint (f, 0, 0);
939 SET_FRAME_GARBAGED (f);
940 cancel_mouse_face (f);
942 /* We can not call change_frame_size for a mapped frame,
943 we can not set pixel width/height either. The window manager may
944 override our resize request, XMonad does this all the time.
945 The best we can do is try to sync, so lisp code sees the updated
946 size as fast as possible.
947 For unmapped windows, we can set rows/cols. When
948 the frame is mapped again we will (hopefully) get the correct size. */
949 if (FRAME_VISIBLE_P (f))
951 /* Must call this to flush out events */
952 (void)gtk_events_pending ();
953 gdk_flush ();
954 x_wait_for_event (f, ConfigureNotify);
956 else
957 adjust_frame_size (f, -1, -1, 5, 0, Qnil);
960 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
961 The policy is to keep the number of editable lines. */
963 #if 0
964 static void
965 xg_height_or_width_changed (struct frame *f)
967 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
968 FRAME_TOTAL_PIXEL_WIDTH (f),
969 FRAME_TOTAL_PIXEL_HEIGHT (f));
970 f->output_data.x->hint_flags = 0;
971 x_wm_set_size_hint (f, 0, 0);
973 #endif
975 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
976 Must be done like this, because GtkWidget:s can have "hidden"
977 X Window that aren't accessible.
979 Return 0 if no widget match WDESC. */
981 GtkWidget *
982 xg_win_to_widget (Display *dpy, Window wdesc)
984 gpointer gdkwin;
985 GtkWidget *gwdesc = 0;
987 block_input ();
989 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
990 wdesc);
991 if (gdkwin)
993 GdkEvent event;
994 event.any.window = gdkwin;
995 event.any.type = GDK_NOTHING;
996 gwdesc = gtk_get_event_widget (&event);
999 unblock_input ();
1000 return gwdesc;
1003 /* Set the background of widget W to PIXEL. */
1005 static void
1006 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
1008 #ifdef HAVE_GTK3
1009 GdkRGBA bg;
1010 XColor xbg;
1011 xbg.pixel = pixel;
1012 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
1014 bg.red = (double)xbg.red/65535.0;
1015 bg.green = (double)xbg.green/65535.0;
1016 bg.blue = (double)xbg.blue/65535.0;
1017 bg.alpha = 1.0;
1018 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
1020 #else
1021 GdkColor bg;
1022 GdkColormap *map = gtk_widget_get_colormap (w);
1023 gdk_colormap_query_color (map, pixel, &bg);
1024 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1025 #endif
1028 /* Callback called when the gtk theme changes.
1029 We notify lisp code so it can fix faces used for region for example. */
1031 static void
1032 style_changed_cb (GObject *go,
1033 GParamSpec *spec,
1034 gpointer user_data)
1036 struct input_event event;
1037 GdkDisplay *gdpy = user_data;
1038 const char *display_name = gdk_display_get_name (gdpy);
1039 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1041 EVENT_INIT (event);
1042 event.kind = CONFIG_CHANGED_EVENT;
1043 event.frame_or_window = build_string (display_name);
1044 /* Theme doesn't change often, so intern is called seldom. */
1045 event.arg = intern ("theme-name");
1046 kbd_buffer_store_event (&event);
1048 update_theme_scrollbar_width ();
1049 update_theme_scrollbar_height ();
1051 /* If scroll bar width changed, we need set the new size on all frames
1052 on this display. */
1053 if (dpy)
1055 Lisp_Object rest, frame;
1056 FOR_EACH_FRAME (rest, frame)
1058 struct frame *f = XFRAME (frame);
1059 if (FRAME_LIVE_P (f)
1060 && FRAME_X_P (f)
1061 && FRAME_X_DISPLAY (f) == dpy)
1063 x_set_scroll_bar_default_width (f);
1064 x_set_scroll_bar_default_height (f);
1065 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1071 /* Called when a delete-event occurs on WIDGET. */
1073 static gboolean
1074 delete_cb (GtkWidget *widget,
1075 GdkEvent *event,
1076 gpointer user_data)
1078 #ifdef HAVE_GTK3
1079 /* The event doesn't arrive in the normal event loop. Send event
1080 here. */
1081 struct frame *f = user_data;
1082 struct input_event ie;
1084 EVENT_INIT (ie);
1085 ie.kind = DELETE_WINDOW_EVENT;
1086 XSETFRAME (ie.frame_or_window, f);
1087 kbd_buffer_store_event (&ie);
1088 #endif
1090 return TRUE;
1093 /* Create and set up the GTK widgets for frame F.
1094 Return true if creation succeeded. */
1096 bool
1097 xg_create_frame_widgets (struct frame *f)
1099 GtkWidget *wtop;
1100 GtkWidget *wvbox, *whbox;
1101 GtkWidget *wfixed;
1102 #ifndef HAVE_GTK3
1103 GtkRcStyle *style;
1104 #endif
1105 char *title = 0;
1107 block_input ();
1109 if (FRAME_X_EMBEDDED_P (f))
1111 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1112 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1114 else
1115 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1117 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1118 has backported it to Gtk+ 2.0 and they add the resize grip for
1119 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1120 forever, so disable the grip. */
1121 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1122 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1123 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1124 #endif
1126 xg_set_screen (wtop, f);
1128 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1129 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1130 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1131 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1133 #ifdef HAVE_GTK3
1134 wfixed = emacs_fixed_new (f);
1135 #else
1136 wfixed = gtk_fixed_new ();
1137 #endif
1139 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1141 if (wtop) gtk_widget_destroy (wtop);
1142 if (wvbox) gtk_widget_destroy (wvbox);
1143 if (whbox) gtk_widget_destroy (whbox);
1144 if (wfixed) gtk_widget_destroy (wfixed);
1146 unblock_input ();
1147 return 0;
1150 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1151 gtk_widget_set_name (wtop, EMACS_CLASS);
1152 gtk_widget_set_name (wvbox, "pane");
1153 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1155 /* If this frame has a title or name, set it in the title bar. */
1156 if (! NILP (f->title))
1157 title = SSDATA (ENCODE_UTF_8 (f->title));
1158 else if (! NILP (f->name))
1159 title = SSDATA (ENCODE_UTF_8 (f->name));
1161 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1163 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1164 FRAME_GTK_WIDGET (f) = wfixed;
1165 f->output_data.x->vbox_widget = wvbox;
1166 f->output_data.x->hbox_widget = whbox;
1168 gtk_widget_set_has_window (wfixed, TRUE);
1170 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1171 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1172 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1174 if (FRAME_EXTERNAL_TOOL_BAR (f))
1175 update_frame_tool_bar (f);
1177 /* We don't want this widget double buffered, because we draw on it
1178 with regular X drawing primitives, so from a GTK/GDK point of
1179 view, the widget is totally blank. When an expose comes, this
1180 will make the widget blank, and then Emacs redraws it. This flickers
1181 a lot, so we turn off double buffering. */
1182 gtk_widget_set_double_buffered (wfixed, FALSE);
1184 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1185 SSDATA (Vx_resource_name),
1186 SSDATA (Vx_resource_class));
1188 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1189 GTK is to destroy the widget. We want Emacs to do that instead. */
1190 g_signal_connect (G_OBJECT (wtop), "delete-event",
1191 G_CALLBACK (delete_cb), f);
1193 /* Convert our geometry parameters into a geometry string
1194 and specify it.
1195 GTK will itself handle calculating the real position this way. */
1196 xg_set_geometry (f);
1197 f->win_gravity
1198 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1200 gtk_widget_add_events (wfixed,
1201 GDK_POINTER_MOTION_MASK
1202 | GDK_EXPOSURE_MASK
1203 | GDK_BUTTON_PRESS_MASK
1204 | GDK_BUTTON_RELEASE_MASK
1205 | GDK_KEY_PRESS_MASK
1206 | GDK_ENTER_NOTIFY_MASK
1207 | GDK_LEAVE_NOTIFY_MASK
1208 | GDK_FOCUS_CHANGE_MASK
1209 | GDK_STRUCTURE_MASK
1210 | GDK_VISIBILITY_NOTIFY_MASK);
1212 /* Must realize the windows so the X window gets created. It is used
1213 by callers of this function. */
1214 gtk_widget_realize (wfixed);
1215 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1217 /* Since GTK clears its window by filling with the background color,
1218 we must keep X and GTK background in sync. */
1219 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1221 #ifndef HAVE_GTK3
1222 /* Also, do not let any background pixmap to be set, this looks very
1223 bad as Emacs overwrites the background pixmap with its own idea
1224 of background color. */
1225 style = gtk_widget_get_modifier_style (wfixed);
1227 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1228 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1229 gtk_widget_modify_style (wfixed, style);
1230 #else
1231 gtk_widget_set_can_focus (wfixed, TRUE);
1232 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1233 #endif
1235 #ifdef USE_GTK_TOOLTIP
1236 /* Steal a tool tip window we can move ourselves. */
1237 f->output_data.x->ttip_widget = 0;
1238 f->output_data.x->ttip_lbl = 0;
1239 f->output_data.x->ttip_window = 0;
1240 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1241 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1242 #endif
1245 GdkScreen *screen = gtk_widget_get_screen (wtop);
1246 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1247 /* Only connect this signal once per screen. */
1248 if (! g_signal_handler_find (G_OBJECT (gs),
1249 G_SIGNAL_MATCH_FUNC,
1250 0, 0, 0,
1251 G_CALLBACK (style_changed_cb),
1254 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1255 G_CALLBACK (style_changed_cb),
1256 gdk_screen_get_display (screen));
1260 unblock_input ();
1262 return 1;
1265 void
1266 xg_free_frame_widgets (struct frame *f)
1268 if (FRAME_GTK_OUTER_WIDGET (f))
1270 #ifdef USE_GTK_TOOLTIP
1271 struct x_output *x = f->output_data.x;
1272 #endif
1273 struct xg_frame_tb_info *tbinfo
1274 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1275 TB_INFO_KEY);
1276 if (tbinfo)
1277 xfree (tbinfo);
1279 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1280 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1281 FRAME_GTK_OUTER_WIDGET (f) = 0;
1282 #ifdef USE_GTK_TOOLTIP
1283 if (x->ttip_lbl)
1284 gtk_widget_destroy (x->ttip_lbl);
1285 if (x->ttip_widget)
1286 g_object_unref (G_OBJECT (x->ttip_widget));
1287 #endif
1291 /* Set the normal size hints for the window manager, for frame F.
1292 FLAGS is the flags word to use--or 0 meaning preserve the flags
1293 that the window now has.
1294 If USER_POSITION, set the User Position
1295 flag (this is useful when FLAGS is 0). */
1297 void
1298 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1300 /* Must use GTK routines here, otherwise GTK resets the size hints
1301 to its own defaults. */
1302 GdkGeometry size_hints;
1303 gint hint_flags = 0;
1304 int base_width, base_height;
1305 int min_rows = 0, min_cols = 0;
1306 int win_gravity = f->win_gravity;
1307 Lisp_Object fs_state, frame;
1309 /* Don't set size hints during initialization; that apparently leads
1310 to a race condition. See the thread at
1311 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1312 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1313 return;
1315 XSETFRAME (frame, f);
1316 fs_state = Fframe_parameter (frame, Qfullscreen);
1317 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1319 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1320 Gtk3 don't get along and the frame shrinks (!).
1322 return;
1325 if (flags)
1327 memset (&size_hints, 0, sizeof (size_hints));
1328 f->output_data.x->size_hints = size_hints;
1329 f->output_data.x->hint_flags = hint_flags;
1331 else
1332 flags = f->size_hint_flags;
1334 size_hints = f->output_data.x->size_hints;
1335 hint_flags = f->output_data.x->hint_flags;
1337 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1338 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1339 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1341 hint_flags |= GDK_HINT_BASE_SIZE;
1342 /* Use one row/col here so base_height/width does not become zero.
1343 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1344 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1345 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1346 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1348 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1349 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1351 size_hints.base_width = base_width;
1352 size_hints.base_height = base_height;
1353 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
1354 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1356 /* These currently have a one to one mapping with the X values, but I
1357 don't think we should rely on that. */
1358 hint_flags |= GDK_HINT_WIN_GRAVITY;
1359 size_hints.win_gravity = 0;
1360 if (win_gravity == NorthWestGravity)
1361 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1362 else if (win_gravity == NorthGravity)
1363 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1364 else if (win_gravity == NorthEastGravity)
1365 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1366 else if (win_gravity == WestGravity)
1367 size_hints.win_gravity = GDK_GRAVITY_WEST;
1368 else if (win_gravity == CenterGravity)
1369 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1370 else if (win_gravity == EastGravity)
1371 size_hints.win_gravity = GDK_GRAVITY_EAST;
1372 else if (win_gravity == SouthWestGravity)
1373 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1374 else if (win_gravity == SouthGravity)
1375 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1376 else if (win_gravity == SouthEastGravity)
1377 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1378 else if (win_gravity == StaticGravity)
1379 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1381 if (user_position)
1383 hint_flags &= ~GDK_HINT_POS;
1384 hint_flags |= GDK_HINT_USER_POS;
1387 if (hint_flags != f->output_data.x->hint_flags
1388 || memcmp (&size_hints,
1389 &f->output_data.x->size_hints,
1390 sizeof (size_hints)) != 0)
1392 block_input ();
1393 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1394 NULL, &size_hints, hint_flags);
1395 f->output_data.x->size_hints = size_hints;
1396 f->output_data.x->hint_flags = hint_flags;
1397 unblock_input ();
1401 /* Change background color of a frame.
1402 Since GTK uses the background color to clear the window, we must
1403 keep the GTK and X colors in sync.
1404 F is the frame to change,
1405 BG is the pixel value to change to. */
1407 void
1408 xg_set_background_color (struct frame *f, unsigned long bg)
1410 if (FRAME_GTK_WIDGET (f))
1412 block_input ();
1413 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1414 unblock_input ();
1419 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1420 functions so GTK does not overwrite the icon. */
1422 void
1423 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1425 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1426 icon_pixmap,
1427 icon_mask);
1428 if (gp)
1429 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1434 /***********************************************************************
1435 Dialog functions
1436 ***********************************************************************/
1437 /* Return the dialog title to use for a dialog of type KEY.
1438 This is the encoding used by lwlib. We use the same for GTK. */
1440 static const char *
1441 get_dialog_title (char key)
1443 const char *title = "";
1445 switch (key) {
1446 case 'E': case 'e':
1447 title = "Error";
1448 break;
1450 case 'I': case 'i':
1451 title = "Information";
1452 break;
1454 case 'L': case 'l':
1455 title = "Prompt";
1456 break;
1458 case 'P': case 'p':
1459 title = "Prompt";
1460 break;
1462 case 'Q': case 'q':
1463 title = "Question";
1464 break;
1467 return title;
1470 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1471 the dialog, but return TRUE so the event does not propagate further
1472 in GTK. This prevents GTK from destroying the dialog widget automatically
1473 and we can always destroy the widget manually, regardless of how
1474 it was popped down (button press or WM_DELETE_WINDOW).
1475 W is the dialog widget.
1476 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1477 user_data is NULL (not used).
1479 Returns TRUE to end propagation of event. */
1481 static gboolean
1482 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1484 gtk_widget_unmap (w);
1485 return TRUE;
1488 /* Create a popup dialog window. See also xg_create_widget below.
1489 WV is a widget_value describing the dialog.
1490 SELECT_CB is the callback to use when a button has been pressed.
1491 DEACTIVATE_CB is the callback to use when the dialog pops down.
1493 Returns the GTK dialog widget. */
1495 static GtkWidget *
1496 create_dialog (widget_value *wv,
1497 GCallback select_cb,
1498 GCallback deactivate_cb)
1500 const char *title = get_dialog_title (wv->name[0]);
1501 int total_buttons = wv->name[1] - '0';
1502 int right_buttons = wv->name[4] - '0';
1503 int left_buttons;
1504 int button_nr = 0;
1505 int button_spacing = 10;
1506 GtkWidget *wdialog = gtk_dialog_new ();
1507 GtkDialog *wd = GTK_DIALOG (wdialog);
1508 widget_value *item;
1509 GtkWidget *whbox_down;
1511 /* If the number of buttons is greater than 4, make two rows of buttons
1512 instead. This looks better. */
1513 bool make_two_rows = total_buttons > 4;
1515 #if GTK_CHECK_VERSION (3, 12, 0)
1516 GtkBuilder *gbld = gtk_builder_new ();
1517 GObject *go = gtk_buildable_get_internal_child (GTK_BUILDABLE (wd),
1518 gbld,
1519 "action_area");
1520 GtkBox *cur_box = GTK_BOX (go);
1521 g_object_unref (G_OBJECT (gbld));
1522 #else
1523 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1524 #endif
1526 if (right_buttons == 0) right_buttons = total_buttons/2;
1527 left_buttons = total_buttons - right_buttons;
1529 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1530 gtk_widget_set_name (wdialog, "emacs-dialog");
1533 if (make_two_rows)
1535 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1536 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1537 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1538 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1539 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1540 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1542 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1543 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1544 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1546 cur_box = GTK_BOX (whbox_up);
1549 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1550 G_CALLBACK (dialog_delete_callback), 0);
1552 if (deactivate_cb)
1554 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1555 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1558 for (item = wv->contents; item; item = item->next)
1560 char *utf8_label = get_utf8_string (item->value);
1561 GtkWidget *w;
1562 GtkRequisition req;
1564 if (item->name && strcmp (item->name, "message") == 0)
1566 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1567 /* This is the text part of the dialog. */
1568 w = gtk_label_new (utf8_label);
1569 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1570 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1571 #if GTK_CHECK_VERSION (3, 14, 0)
1572 gtk_widget_set_halign (w, GTK_ALIGN_START);
1573 gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
1574 #else
1575 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1576 #endif
1577 /* Try to make dialog look better. Must realize first so
1578 the widget can calculate the size it needs. */
1579 gtk_widget_realize (w);
1580 gtk_widget_get_preferred_size (w, NULL, &req);
1581 gtk_box_set_spacing (wvbox, req.height);
1582 if (item->value && strlen (item->value) > 0)
1583 button_spacing = 2*req.width/strlen (item->value);
1584 if (button_spacing < 10) button_spacing = 10;
1586 else
1588 /* This is one button to add to the dialog. */
1589 w = gtk_button_new_with_label (utf8_label);
1590 if (! item->enabled)
1591 gtk_widget_set_sensitive (w, FALSE);
1592 if (select_cb)
1593 g_signal_connect (G_OBJECT (w), "clicked",
1594 select_cb, item->call_data);
1596 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1597 if (++button_nr == left_buttons)
1599 if (make_two_rows)
1600 cur_box = GTK_BOX (whbox_down);
1604 if (utf8_label)
1605 g_free (utf8_label);
1608 return wdialog;
1611 struct xg_dialog_data
1613 GMainLoop *loop;
1614 int response;
1615 GtkWidget *w;
1616 guint timerid;
1619 /* Function that is called when the file or font dialogs pop down.
1620 W is the dialog widget, RESPONSE is the response code.
1621 USER_DATA is what we passed in to g_signal_connect. */
1623 static void
1624 xg_dialog_response_cb (GtkDialog *w,
1625 gint response,
1626 gpointer user_data)
1628 struct xg_dialog_data *dd = user_data;
1629 dd->response = response;
1630 g_main_loop_quit (dd->loop);
1634 /* Destroy the dialog. This makes it pop down. */
1636 static void
1637 pop_down_dialog (void *arg)
1639 struct xg_dialog_data *dd = arg;
1641 block_input ();
1642 if (dd->w) gtk_widget_destroy (dd->w);
1643 if (dd->timerid != 0) g_source_remove (dd->timerid);
1645 g_main_loop_quit (dd->loop);
1646 g_main_loop_unref (dd->loop);
1648 unblock_input ();
1651 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1652 We pass in DATA as gpointer* so we can use this as a callback. */
1654 static gboolean
1655 xg_maybe_add_timer (gpointer data)
1657 struct xg_dialog_data *dd = data;
1658 struct timespec next_time = timer_check ();
1660 dd->timerid = 0;
1662 if (timespec_valid_p (next_time))
1664 time_t s = next_time.tv_sec;
1665 int per_ms = TIMESPEC_RESOLUTION / 1000;
1666 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1667 if (s <= ((guint) -1 - ms) / 1000)
1668 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1670 return FALSE;
1674 /* Pops up a modal dialog W and waits for response.
1675 We don't use gtk_dialog_run because we want to process emacs timers.
1676 The dialog W is not destroyed when this function returns. */
1678 static int
1679 xg_dialog_run (struct frame *f, GtkWidget *w)
1681 ptrdiff_t count = SPECPDL_INDEX ();
1682 struct xg_dialog_data dd;
1684 xg_set_screen (w, f);
1685 gtk_window_set_transient_for (GTK_WINDOW (w),
1686 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1687 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1688 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1690 dd.loop = g_main_loop_new (NULL, FALSE);
1691 dd.response = GTK_RESPONSE_CANCEL;
1692 dd.w = w;
1693 dd.timerid = 0;
1695 g_signal_connect (G_OBJECT (w),
1696 "response",
1697 G_CALLBACK (xg_dialog_response_cb),
1698 &dd);
1699 /* Don't destroy the widget if closed by the window manager close button. */
1700 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1701 gtk_widget_show (w);
1703 record_unwind_protect_ptr (pop_down_dialog, &dd);
1705 (void) xg_maybe_add_timer (&dd);
1706 g_main_loop_run (dd.loop);
1708 dd.w = 0;
1709 unbind_to (count, Qnil);
1711 return dd.response;
1715 /***********************************************************************
1716 File dialog functions
1717 ***********************************************************************/
1718 /* Return true if the old file selection dialog is being used. */
1720 bool
1721 xg_uses_old_file_dialog (void)
1723 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1724 return x_gtk_use_old_file_dialog;
1725 #else
1726 return 0;
1727 #endif
1731 typedef char * (*xg_get_file_func) (GtkWidget *);
1733 /* Return the selected file for file chooser dialog W.
1734 The returned string must be free:d. */
1736 static char *
1737 xg_get_file_name_from_chooser (GtkWidget *w)
1739 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1742 /* Callback called when the "Show hidden files" toggle is pressed.
1743 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1745 static void
1746 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1748 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1749 gboolean visible;
1750 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1751 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1755 /* Callback called when a property changes in a file chooser.
1756 GOBJECT is the file chooser dialog, ARG1 describes the property.
1757 USER_DATA is the toggle widget in the file chooser dialog.
1758 We use this to update the "Show hidden files" toggle when the user
1759 changes that property by right clicking in the file list. */
1761 static void
1762 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1764 if (strcmp (arg1->name, "show-hidden") == 0)
1766 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1767 gboolean visible, toggle_on;
1769 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1770 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1772 if (!!visible != !!toggle_on)
1774 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1775 G_CALLBACK (xg_toggle_visibility_cb),
1776 gobject);
1777 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1778 g_signal_handlers_unblock_by_func
1779 (G_OBJECT (wtoggle),
1780 G_CALLBACK (xg_toggle_visibility_cb),
1781 gobject);
1783 x_gtk_show_hidden_files = visible;
1787 /* Read a file name from the user using a file chooser dialog.
1788 F is the current frame.
1789 PROMPT is a prompt to show to the user. May not be NULL.
1790 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1791 If MUSTMATCH_P, the returned file name must be an existing
1792 file. (Actually, this only has cosmetic effects, the user can
1793 still enter a non-existing file.) *FUNC is set to a function that
1794 can be used to retrieve the selected file name from the returned widget.
1796 Returns the created widget. */
1798 static GtkWidget *
1799 xg_get_file_with_chooser (struct frame *f,
1800 char *prompt,
1801 char *default_filename,
1802 bool mustmatch_p, bool only_dir_p,
1803 xg_get_file_func *func)
1805 char msgbuf[1024];
1807 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1808 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1809 GtkFileChooserAction action = (mustmatch_p ?
1810 GTK_FILE_CHOOSER_ACTION_OPEN :
1811 GTK_FILE_CHOOSER_ACTION_SAVE);
1813 if (only_dir_p)
1814 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1816 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1817 XG_TEXT_CANCEL, GTK_RESPONSE_CANCEL,
1818 (mustmatch_p || only_dir_p ?
1819 XG_TEXT_OPEN : XG_TEXT_OK),
1820 GTK_RESPONSE_OK,
1821 NULL);
1822 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1824 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1825 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1826 gtk_widget_show (wbox);
1827 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1829 if (x_gtk_show_hidden_files)
1831 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1832 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1834 gtk_widget_show (wtoggle);
1835 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1836 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1837 g_signal_connect (G_OBJECT (filewin), "notify",
1838 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1840 if (x_gtk_file_dialog_help_text)
1842 msgbuf[0] = '\0';
1843 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1844 Show the C-l help text only for versions < 2.10. */
1845 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1846 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1847 strcat (msgbuf, "\nIf you don't like this file selector, use the "
1848 "corresponding\nkey binding or customize "
1849 "use-file-dialog to turn it off.");
1851 wmessage = gtk_label_new (msgbuf);
1852 gtk_widget_show (wmessage);
1855 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1856 if (x_gtk_file_dialog_help_text)
1857 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1858 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1860 if (default_filename)
1862 Lisp_Object file;
1863 struct gcpro gcpro1;
1864 char *utf8_filename;
1865 GCPRO1 (file);
1867 file = build_string (default_filename);
1869 /* File chooser does not understand ~/... in the file name. It must be
1870 an absolute name starting with /. */
1871 if (default_filename[0] != '/')
1872 file = Fexpand_file_name (file, Qnil);
1874 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1875 if (! NILP (Ffile_directory_p (file)))
1876 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1877 utf8_filename);
1878 else
1880 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1881 utf8_filename);
1882 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1884 char *cp = strrchr (utf8_filename, '/');
1885 if (cp) ++cp;
1886 else cp = utf8_filename;
1887 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1891 UNGCPRO;
1894 *func = xg_get_file_name_from_chooser;
1895 return filewin;
1898 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1900 /* Return the selected file for file selector dialog W.
1901 The returned string must be free:d. */
1903 static char *
1904 xg_get_file_name_from_selector (GtkWidget *w)
1906 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1907 return xstrdup (gtk_file_selection_get_filename (filesel));
1910 /* Create a file selection dialog.
1911 F is the current frame.
1912 PROMPT is a prompt to show to the user. May not be NULL.
1913 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1914 If MUSTMATCH_P, the returned file name must be an existing
1915 file. *FUNC is set to a function that can be used to retrieve the
1916 selected file name from the returned widget.
1918 Returns the created widget. */
1920 static GtkWidget *
1921 xg_get_file_with_selection (struct frame *f,
1922 char *prompt,
1923 char *default_filename,
1924 bool mustmatch_p, bool only_dir_p,
1925 xg_get_file_func *func)
1927 GtkWidget *filewin;
1928 GtkFileSelection *filesel;
1930 filewin = gtk_file_selection_new (prompt);
1931 filesel = GTK_FILE_SELECTION (filewin);
1933 if (default_filename)
1934 gtk_file_selection_set_filename (filesel, default_filename);
1936 if (mustmatch_p)
1938 /* The selection_entry part of filesel is not documented. */
1939 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1940 gtk_file_selection_hide_fileop_buttons (filesel);
1943 *func = xg_get_file_name_from_selector;
1945 return filewin;
1947 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1949 /* Read a file name from the user using a file dialog, either the old
1950 file selection dialog, or the new file chooser dialog. Which to use
1951 depends on what the GTK version used has, and what the value of
1952 gtk-use-old-file-dialog.
1953 F is the current frame.
1954 PROMPT is a prompt to show to the user. May not be NULL.
1955 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1956 If MUSTMATCH_P, the returned file name must be an existing
1957 file.
1959 Returns a file name or NULL if no file was selected.
1960 The returned string must be freed by the caller. */
1962 char *
1963 xg_get_file_name (struct frame *f,
1964 char *prompt,
1965 char *default_filename,
1966 bool mustmatch_p,
1967 bool only_dir_p)
1969 GtkWidget *w = 0;
1970 char *fn = 0;
1971 int filesel_done = 0;
1972 xg_get_file_func func;
1974 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1976 if (xg_uses_old_file_dialog ())
1977 w = xg_get_file_with_selection (f, prompt, default_filename,
1978 mustmatch_p, only_dir_p, &func);
1979 else
1980 w = xg_get_file_with_chooser (f, prompt, default_filename,
1981 mustmatch_p, only_dir_p, &func);
1983 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1984 w = xg_get_file_with_chooser (f, prompt, default_filename,
1985 mustmatch_p, only_dir_p, &func);
1986 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1988 gtk_widget_set_name (w, "emacs-filedialog");
1990 filesel_done = xg_dialog_run (f, w);
1991 if (filesel_done == GTK_RESPONSE_OK)
1992 fn = (*func) (w);
1994 gtk_widget_destroy (w);
1995 return fn;
1998 /***********************************************************************
1999 GTK font chooser
2000 ***********************************************************************/
2002 #ifdef HAVE_FREETYPE
2004 #if USE_NEW_GTK_FONT_CHOOSER
2006 #define XG_WEIGHT_TO_SYMBOL(w) \
2007 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
2008 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
2009 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
2010 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
2011 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
2012 : w <= PANGO_WEIGHT_BOLD ? Qbold \
2013 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
2014 : Qultra_bold)
2016 #define XG_STYLE_TO_SYMBOL(s) \
2017 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
2018 : s == PANGO_STYLE_ITALIC ? Qitalic \
2019 : Qnormal)
2021 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2024 static char *x_last_font_name;
2026 /* Pop up a GTK font selector and return the name of the font the user
2027 selects, as a C string. The returned font name follows GTK's own
2028 format:
2030 `FAMILY [VALUE1 VALUE2] SIZE'
2032 This can be parsed using font_parse_fcname in font.c.
2033 DEFAULT_NAME, if non-zero, is the default font name. */
2035 Lisp_Object
2036 xg_get_font (struct frame *f, const char *default_name)
2038 GtkWidget *w;
2039 int done = 0;
2040 Lisp_Object font = Qnil;
2042 w = gtk_font_chooser_dialog_new
2043 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2045 if (default_name)
2047 /* Convert fontconfig names to Gtk names, i.e. remove - before
2048 number */
2049 char *p = strrchr (default_name, '-');
2050 if (p)
2052 char *ep = p+1;
2053 while (c_isdigit (*ep))
2054 ++ep;
2055 if (*ep == '\0') *p = ' ';
2058 else if (x_last_font_name)
2059 default_name = x_last_font_name;
2061 if (default_name)
2062 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2064 gtk_widget_set_name (w, "emacs-fontdialog");
2065 done = xg_dialog_run (f, w);
2066 if (done == GTK_RESPONSE_OK)
2068 #if USE_NEW_GTK_FONT_CHOOSER
2069 /* Use the GTK3 font chooser. */
2070 PangoFontDescription *desc
2071 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2073 if (desc)
2075 Lisp_Object args[10];
2076 const char *name = pango_font_description_get_family (desc);
2077 gint size = pango_font_description_get_size (desc);
2078 PangoWeight weight = pango_font_description_get_weight (desc);
2079 PangoStyle style = pango_font_description_get_style (desc);
2081 args[0] = QCname;
2082 args[1] = build_string (name);
2084 args[2] = QCsize;
2085 args[3] = make_float (pango_units_to_double (size));
2087 args[4] = QCweight;
2088 args[5] = XG_WEIGHT_TO_SYMBOL (weight);
2090 args[6] = QCslant;
2091 args[7] = XG_STYLE_TO_SYMBOL (style);
2093 args[8] = QCtype;
2094 args[9] = Qxft;
2096 font = Ffont_spec (8, args);
2098 pango_font_description_free (desc);
2099 dupstring (&x_last_font_name, name);
2102 #else /* Use old font selector, which just returns the font name. */
2104 char *font_name
2105 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2107 if (font_name)
2109 font = build_string (font_name);
2110 g_free (x_last_font_name);
2111 x_last_font_name = font_name;
2113 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2116 gtk_widget_destroy (w);
2117 return font;
2119 #endif /* HAVE_FREETYPE */
2123 /***********************************************************************
2124 Menu functions.
2125 ***********************************************************************/
2127 /* The name of menu items that can be used for customization. Since GTK
2128 RC files are very crude and primitive, we have to set this on all
2129 menu item names so a user can easily customize menu items. */
2131 #define MENU_ITEM_NAME "emacs-menuitem"
2134 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2135 during GC. The next member points to the items. */
2136 static xg_list_node xg_menu_cb_list;
2138 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2139 during GC. The next member points to the items. */
2140 static xg_list_node xg_menu_item_cb_list;
2142 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2143 F is the frame CL_DATA will be initialized for.
2144 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2146 The menu bar and all sub menus under the menu bar in a frame
2147 share the same structure, hence the reference count.
2149 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2150 allocated xg_menu_cb_data if CL_DATA is NULL. */
2152 static xg_menu_cb_data *
2153 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2155 if (! cl_data)
2157 cl_data = xmalloc (sizeof *cl_data);
2158 cl_data->f = f;
2159 cl_data->menu_bar_vector = f->menu_bar_vector;
2160 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2161 cl_data->highlight_cb = highlight_cb;
2162 cl_data->ref_count = 0;
2164 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2167 cl_data->ref_count++;
2169 return cl_data;
2172 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2173 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2175 When the menu bar is updated, menu items may have been added and/or
2176 removed, so menu_bar_vector and menu_bar_items_used change. We must
2177 then update CL_DATA since it is used to determine which menu
2178 item that is invoked in the menu.
2179 HIGHLIGHT_CB could change, there is no check that the same
2180 function is given when modifying a menu bar as was given when
2181 creating the menu bar. */
2183 static void
2184 update_cl_data (xg_menu_cb_data *cl_data,
2185 struct frame *f,
2186 GCallback highlight_cb)
2188 if (cl_data)
2190 cl_data->f = f;
2191 cl_data->menu_bar_vector = f->menu_bar_vector;
2192 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2193 cl_data->highlight_cb = highlight_cb;
2197 /* Decrease reference count for CL_DATA.
2198 If reference count is zero, free CL_DATA. */
2200 static void
2201 unref_cl_data (xg_menu_cb_data *cl_data)
2203 if (cl_data && cl_data->ref_count > 0)
2205 cl_data->ref_count--;
2206 if (cl_data->ref_count == 0)
2208 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2209 xfree (cl_data);
2214 /* Function that marks all lisp data during GC. */
2216 void
2217 xg_mark_data (void)
2219 xg_list_node *iter;
2220 Lisp_Object rest, frame;
2222 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2223 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2225 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2227 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2229 if (! NILP (cb_data->help))
2230 mark_object (cb_data->help);
2233 FOR_EACH_FRAME (rest, frame)
2235 struct frame *f = XFRAME (frame);
2237 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2239 struct xg_frame_tb_info *tbinfo
2240 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2241 TB_INFO_KEY);
2242 if (tbinfo)
2244 mark_object (tbinfo->last_tool_bar);
2245 mark_object (tbinfo->style);
2252 /* Callback called when a menu item is destroyed. Used to free data.
2253 W is the widget that is being destroyed (not used).
2254 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2256 static void
2257 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2259 if (client_data)
2261 xg_menu_item_cb_data *data = client_data;
2262 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2263 xfree (data);
2267 /* Callback called when the pointer enters/leaves a menu item.
2268 W is the parent of the menu item.
2269 EVENT is either an enter event or leave event.
2270 CLIENT_DATA is not used.
2272 Returns FALSE to tell GTK to keep processing this event. */
2274 static gboolean
2275 menuitem_highlight_callback (GtkWidget *w,
2276 GdkEventCrossing *event,
2277 gpointer client_data)
2279 GdkEvent ev;
2280 GtkWidget *subwidget;
2281 xg_menu_item_cb_data *data;
2283 ev.crossing = *event;
2284 subwidget = gtk_get_event_widget (&ev);
2285 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2286 if (data)
2288 if (! NILP (data->help) && data->cl_data->highlight_cb)
2290 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2291 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2292 (*func) (subwidget, call_data);
2296 return FALSE;
2299 /* Callback called when a menu is destroyed. Used to free data.
2300 W is the widget that is being destroyed (not used).
2301 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2303 static void
2304 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2306 unref_cl_data (client_data);
2309 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2310 must be non-NULL) and can be inserted into a menu item.
2312 Returns the GtkHBox. */
2314 static GtkWidget *
2315 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2317 GtkWidget *wlbl;
2318 GtkWidget *wkey;
2319 GtkWidget *wbox;
2321 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2322 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2323 wlbl = gtk_label_new (utf8_label);
2324 wkey = gtk_label_new (utf8_key);
2326 #if GTK_CHECK_VERSION (3, 14, 0)
2327 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2328 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2329 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2330 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2331 #else
2332 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2333 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2334 #endif
2335 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2336 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2338 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2339 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2340 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2342 return wbox;
2345 /* Make and return a menu item widget with the key to the right.
2346 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2347 UTF8_KEY is the text representing the key binding.
2348 ITEM is the widget_value describing the menu item.
2350 GROUP is an in/out parameter. If the menu item to be created is not
2351 part of any radio menu group, *GROUP contains NULL on entry and exit.
2352 If the menu item to be created is part of a radio menu group, on entry
2353 *GROUP contains the group to use, or NULL if this is the first item
2354 in the group. On exit, *GROUP contains the radio item group.
2356 Unfortunately, keys don't line up as nicely as in Motif,
2357 but the MacOS X version doesn't either, so I guess that is OK. */
2359 static GtkWidget *
2360 make_menu_item (const char *utf8_label,
2361 const char *utf8_key,
2362 widget_value *item,
2363 GSList **group)
2365 GtkWidget *w;
2366 GtkWidget *wtoadd = 0;
2368 /* It has been observed that some menu items have a NULL name field.
2369 This will lead to this function being called with a NULL utf8_label.
2370 GTK crashes on that so we set a blank label. Why there is a NULL
2371 name remains to be investigated. */
2372 if (! utf8_label) utf8_label = " ";
2374 if (utf8_key)
2375 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2377 if (item->button_type == BUTTON_TYPE_TOGGLE)
2379 *group = NULL;
2380 if (utf8_key) w = gtk_check_menu_item_new ();
2381 else w = gtk_check_menu_item_new_with_label (utf8_label);
2382 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2384 else if (item->button_type == BUTTON_TYPE_RADIO)
2386 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2387 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2388 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2389 if (item->selected)
2390 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2392 else
2394 *group = NULL;
2395 if (utf8_key) w = gtk_menu_item_new ();
2396 else w = gtk_menu_item_new_with_label (utf8_label);
2399 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2400 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2402 return w;
2405 /* Create a menu item widget, and connect the callbacks.
2406 ITEM describes the menu item.
2407 F is the frame the created menu belongs to.
2408 SELECT_CB is the callback to use when a menu item is selected.
2409 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2410 CL_DATA points to the callback data to be used for this menu.
2411 GROUP is an in/out parameter. If the menu item to be created is not
2412 part of any radio menu group, *GROUP contains NULL on entry and exit.
2413 If the menu item to be created is part of a radio menu group, on entry
2414 *GROUP contains the group to use, or NULL if this is the first item
2415 in the group. On exit, *GROUP contains the radio item group.
2417 Returns the created GtkWidget. */
2419 static GtkWidget *
2420 xg_create_one_menuitem (widget_value *item,
2421 struct frame *f,
2422 GCallback select_cb,
2423 GCallback highlight_cb,
2424 xg_menu_cb_data *cl_data,
2425 GSList **group)
2427 char *utf8_label;
2428 char *utf8_key;
2429 GtkWidget *w;
2430 xg_menu_item_cb_data *cb_data;
2432 utf8_label = get_utf8_string (item->name);
2433 utf8_key = get_utf8_string (item->key);
2435 w = make_menu_item (utf8_label, utf8_key, item, group);
2437 if (utf8_label) g_free (utf8_label);
2438 if (utf8_key) g_free (utf8_key);
2440 cb_data = xmalloc (sizeof *cb_data);
2442 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2444 cb_data->select_id = 0;
2445 cb_data->help = item->help;
2446 cb_data->cl_data = cl_data;
2447 cb_data->call_data = item->call_data;
2449 g_signal_connect (G_OBJECT (w),
2450 "destroy",
2451 G_CALLBACK (menuitem_destroy_callback),
2452 cb_data);
2454 /* Put cb_data in widget, so we can get at it when modifying menubar */
2455 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2457 /* final item, not a submenu */
2458 if (item->call_data && ! item->contents)
2460 if (select_cb)
2461 cb_data->select_id
2462 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2465 return w;
2468 /* Create a full menu tree specified by DATA.
2469 F is the frame the created menu belongs to.
2470 SELECT_CB is the callback to use when a menu item is selected.
2471 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2472 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2473 If POP_UP_P, create a popup menu.
2474 If MENU_BAR_P, create a menu bar.
2475 TOPMENU is the topmost GtkWidget that others shall be placed under.
2476 It may be NULL, in that case we create the appropriate widget
2477 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2478 CL_DATA is the callback data we shall use for this menu, or NULL
2479 if we haven't set the first callback yet.
2480 NAME is the name to give to the top level menu if this function
2481 creates it. May be NULL to not set any name.
2483 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2484 not NULL.
2486 This function calls itself to create submenus. */
2488 static GtkWidget *
2489 create_menus (widget_value *data,
2490 struct frame *f,
2491 GCallback select_cb,
2492 GCallback deactivate_cb,
2493 GCallback highlight_cb,
2494 bool pop_up_p,
2495 bool menu_bar_p,
2496 GtkWidget *topmenu,
2497 xg_menu_cb_data *cl_data,
2498 const char *name)
2500 widget_value *item;
2501 GtkWidget *wmenu = topmenu;
2502 GSList *group = NULL;
2504 if (! topmenu)
2506 if (! menu_bar_p)
2508 wmenu = gtk_menu_new ();
2509 xg_set_screen (wmenu, f);
2510 /* Connect this to the menu instead of items so we get enter/leave for
2511 disabled items also. TODO: Still does not get enter/leave for
2512 disabled items in detached menus. */
2513 g_signal_connect (G_OBJECT (wmenu),
2514 "enter-notify-event",
2515 G_CALLBACK (menuitem_highlight_callback),
2516 NULL);
2517 g_signal_connect (G_OBJECT (wmenu),
2518 "leave-notify-event",
2519 G_CALLBACK (menuitem_highlight_callback),
2520 NULL);
2522 else
2524 wmenu = gtk_menu_bar_new ();
2525 /* Set width of menu bar to a small value so it doesn't enlarge
2526 a small initial frame size. The width will be set to the
2527 width of the frame later on when it is added to a container.
2528 height -1: Natural height. */
2529 gtk_widget_set_size_request (wmenu, 1, -1);
2532 /* Put cl_data on the top menu for easier access. */
2533 cl_data = make_cl_data (cl_data, f, highlight_cb);
2534 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2535 g_signal_connect (G_OBJECT (wmenu), "destroy",
2536 G_CALLBACK (menu_destroy_callback), cl_data);
2538 if (name)
2539 gtk_widget_set_name (wmenu, name);
2541 if (deactivate_cb)
2542 g_signal_connect (G_OBJECT (wmenu),
2543 "selection-done", deactivate_cb, 0);
2546 for (item = data; item; item = item->next)
2548 GtkWidget *w;
2550 if (pop_up_p && !item->contents && !item->call_data
2551 && !menu_separator_name_p (item->name))
2553 char *utf8_label;
2554 /* A title for a popup. We do the same as GTK does when
2555 creating titles, but it does not look good. */
2556 group = NULL;
2557 utf8_label = get_utf8_string (item->name);
2559 w = gtk_menu_item_new_with_label (utf8_label);
2560 gtk_widget_set_sensitive (w, FALSE);
2561 if (utf8_label) g_free (utf8_label);
2563 else if (menu_separator_name_p (item->name))
2565 group = NULL;
2566 /* GTK only have one separator type. */
2567 w = gtk_separator_menu_item_new ();
2569 else
2571 w = xg_create_one_menuitem (item,
2573 item->contents ? 0 : select_cb,
2574 highlight_cb,
2575 cl_data,
2576 &group);
2578 /* Create a possibly empty submenu for menu bar items, since some
2579 themes don't highlight items correctly without it. */
2580 if (item->contents || menu_bar_p)
2582 GtkWidget *submenu = create_menus (item->contents,
2584 select_cb,
2585 deactivate_cb,
2586 highlight_cb,
2590 cl_data,
2592 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2596 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2597 gtk_widget_set_name (w, MENU_ITEM_NAME);
2600 return wmenu;
2603 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2604 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2605 with some text and buttons.
2606 F is the frame the created item belongs to.
2607 NAME is the name to use for the top widget.
2608 VAL is a widget_value structure describing items to be created.
2609 SELECT_CB is the callback to use when a menu item is selected or
2610 a dialog button is pressed.
2611 DEACTIVATE_CB is the callback to use when an item is deactivated.
2612 For a menu, when a sub menu is not shown anymore, for a dialog it is
2613 called when the dialog is popped down.
2614 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2616 Returns the widget created. */
2618 GtkWidget *
2619 xg_create_widget (const char *type, const char *name, struct frame *f,
2620 widget_value *val, GCallback select_cb,
2621 GCallback deactivate_cb, GCallback highlight_cb)
2623 GtkWidget *w = 0;
2624 bool menu_bar_p = strcmp (type, "menubar") == 0;
2625 bool pop_up_p = strcmp (type, "popup") == 0;
2627 if (strcmp (type, "dialog") == 0)
2629 w = create_dialog (val, select_cb, deactivate_cb);
2630 xg_set_screen (w, f);
2631 gtk_window_set_transient_for (GTK_WINDOW (w),
2632 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2633 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2634 gtk_widget_set_name (w, "emacs-dialog");
2635 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2637 else if (menu_bar_p || pop_up_p)
2639 w = create_menus (val->contents,
2641 select_cb,
2642 deactivate_cb,
2643 highlight_cb,
2644 pop_up_p,
2645 menu_bar_p,
2648 name);
2650 /* Set the cursor to an arrow for popup menus when they are mapped.
2651 This is done by default for menu bar menus. */
2652 if (pop_up_p)
2654 /* Must realize so the GdkWindow inside the widget is created. */
2655 gtk_widget_realize (w);
2656 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2659 else
2661 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2662 type);
2665 return w;
2668 /* Return the label for menu item WITEM. */
2670 static const char *
2671 xg_get_menu_item_label (GtkMenuItem *witem)
2673 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2674 return gtk_label_get_label (wlabel);
2677 /* Return true if the menu item WITEM has the text LABEL. */
2679 static bool
2680 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2682 bool is_same = 0;
2683 char *utf8_label = get_utf8_string (label);
2684 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2686 if (! old_label && ! utf8_label)
2687 is_same = 1;
2688 else if (old_label && utf8_label)
2689 is_same = strcmp (utf8_label, old_label) == 0;
2691 if (utf8_label) g_free (utf8_label);
2693 return is_same;
2696 /* Destroy widgets in LIST. */
2698 static void
2699 xg_destroy_widgets (GList *list)
2701 GList *iter;
2703 for (iter = list; iter; iter = g_list_next (iter))
2705 GtkWidget *w = GTK_WIDGET (iter->data);
2707 /* Destroying the widget will remove it from the container it is in. */
2708 gtk_widget_destroy (w);
2712 /* Update the top level names in MENUBAR (i.e. not submenus).
2713 F is the frame the menu bar belongs to.
2714 *LIST is a list with the current menu bar names (menu item widgets).
2715 ITER is the item within *LIST that shall be updated.
2716 POS is the numerical position, starting at 0, of ITER in *LIST.
2717 VAL describes what the menu bar shall look like after the update.
2718 SELECT_CB is the callback to use when a menu item is selected.
2719 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2720 CL_DATA points to the callback data to be used for this menu bar.
2722 This function calls itself to walk through the menu bar names. */
2724 static void
2725 xg_update_menubar (GtkWidget *menubar,
2726 struct frame *f,
2727 GList **list,
2728 GList *iter,
2729 int pos,
2730 widget_value *val,
2731 GCallback select_cb,
2732 GCallback deactivate_cb,
2733 GCallback highlight_cb,
2734 xg_menu_cb_data *cl_data)
2736 if (! iter && ! val)
2737 return;
2738 else if (iter && ! val)
2740 /* Item(s) have been removed. Remove all remaining items. */
2741 xg_destroy_widgets (iter);
2743 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2744 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2745 gtk_menu_item_new_with_label (""),
2747 /* All updated. */
2748 val = 0;
2749 iter = 0;
2751 else if (! iter && val)
2753 /* Item(s) added. Add all new items in one call. */
2754 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2755 0, 1, menubar, cl_data, 0);
2757 /* All updated. */
2758 val = 0;
2759 iter = 0;
2761 /* Below this neither iter or val is NULL */
2762 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2764 /* This item is still the same, check next item. */
2765 val = val->next;
2766 iter = g_list_next (iter);
2767 ++pos;
2769 else /* This item is changed. */
2771 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2772 GtkMenuItem *witem2 = 0;
2773 bool val_in_menubar = 0;
2774 bool iter_in_new_menubar = 0;
2775 GList *iter2;
2776 widget_value *cur;
2778 /* See if the changed entry (val) is present later in the menu bar */
2779 for (iter2 = iter;
2780 iter2 && ! val_in_menubar;
2781 iter2 = g_list_next (iter2))
2783 witem2 = GTK_MENU_ITEM (iter2->data);
2784 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2787 /* See if the current entry (iter) is present later in the
2788 specification for the new menu bar. */
2789 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2790 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2792 if (val_in_menubar && ! iter_in_new_menubar)
2794 int nr = pos;
2796 /* This corresponds to:
2797 Current: A B C
2798 New: A C
2799 Remove B. */
2801 g_object_ref (G_OBJECT (witem));
2802 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2803 gtk_widget_destroy (GTK_WIDGET (witem));
2805 /* Must get new list since the old changed. */
2806 g_list_free (*list);
2807 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2808 while (nr-- > 0) iter = g_list_next (iter);
2810 else if (! val_in_menubar && ! iter_in_new_menubar)
2812 /* This corresponds to:
2813 Current: A B C
2814 New: A X C
2815 Rename B to X. This might seem to be a strange thing to do,
2816 since if there is a menu under B it will be totally wrong for X.
2817 But consider editing a C file. Then there is a C-mode menu
2818 (corresponds to B above).
2819 If then doing C-x C-f the minibuf menu (X above) replaces the
2820 C-mode menu. When returning from the minibuffer, we get
2821 back the C-mode menu. Thus we do:
2822 Rename B to X (C-mode to minibuf menu)
2823 Rename X to B (minibuf to C-mode menu).
2824 If the X menu hasn't been invoked, the menu under B
2825 is up to date when leaving the minibuffer. */
2826 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2827 char *utf8_label = get_utf8_string (val->name);
2829 /* GTK menu items don't notice when their labels have been
2830 changed from underneath them, so we have to explicitly
2831 use g_object_notify to tell listeners (e.g., a GMenuModel
2832 bridge that might be loaded) that the item's label has
2833 changed. */
2834 gtk_label_set_text (wlabel, utf8_label);
2835 #if GTK_CHECK_VERSION (2, 16, 0)
2836 g_object_notify (G_OBJECT (witem), "label");
2837 #endif
2838 if (utf8_label) g_free (utf8_label);
2839 iter = g_list_next (iter);
2840 val = val->next;
2841 ++pos;
2843 else if (! val_in_menubar && iter_in_new_menubar)
2845 /* This corresponds to:
2846 Current: A B C
2847 New: A X B C
2848 Insert X. */
2850 int nr = pos;
2851 GSList *group = 0;
2852 GtkWidget *w = xg_create_one_menuitem (val,
2854 select_cb,
2855 highlight_cb,
2856 cl_data,
2857 &group);
2859 /* Create a possibly empty submenu for menu bar items, since some
2860 themes don't highlight items correctly without it. */
2861 GtkWidget *submenu = create_menus (NULL, f,
2862 select_cb, deactivate_cb,
2863 highlight_cb,
2864 0, 0, 0, cl_data, 0);
2866 gtk_widget_set_name (w, MENU_ITEM_NAME);
2867 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2868 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2870 g_list_free (*list);
2871 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2872 while (nr-- > 0) iter = g_list_next (iter);
2873 iter = g_list_next (iter);
2874 val = val->next;
2875 ++pos;
2877 else /* if (val_in_menubar && iter_in_new_menubar) */
2879 int nr = pos;
2880 /* This corresponds to:
2881 Current: A B C
2882 New: A C B
2883 Move C before B */
2885 g_object_ref (G_OBJECT (witem2));
2886 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2887 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2888 GTK_WIDGET (witem2), pos);
2889 g_object_unref (G_OBJECT (witem2));
2891 g_list_free (*list);
2892 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2893 while (nr-- > 0) iter = g_list_next (iter);
2894 if (iter) iter = g_list_next (iter);
2895 val = val->next;
2896 ++pos;
2900 /* Update the rest of the menu bar. */
2901 xg_update_menubar (menubar, f, list, iter, pos, val,
2902 select_cb, deactivate_cb, highlight_cb, cl_data);
2905 /* Update the menu item W so it corresponds to VAL.
2906 SELECT_CB is the callback to use when a menu item is selected.
2907 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2908 CL_DATA is the data to set in the widget for menu invocation. */
2910 static void
2911 xg_update_menu_item (widget_value *val,
2912 GtkWidget *w,
2913 GCallback select_cb,
2914 GCallback highlight_cb,
2915 xg_menu_cb_data *cl_data)
2917 GtkWidget *wchild;
2918 GtkLabel *wlbl = 0;
2919 GtkLabel *wkey = 0;
2920 char *utf8_label;
2921 char *utf8_key;
2922 const char *old_label = 0;
2923 const char *old_key = 0;
2924 xg_menu_item_cb_data *cb_data;
2925 bool label_changed = false;
2927 wchild = XG_BIN_CHILD (w);
2928 utf8_label = get_utf8_string (val->name);
2929 utf8_key = get_utf8_string (val->key);
2931 /* See if W is a menu item with a key. See make_menu_item above. */
2932 if (GTK_IS_BOX (wchild))
2934 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2936 wlbl = GTK_LABEL (list->data);
2937 wkey = GTK_LABEL (list->next->data);
2938 g_list_free (list);
2940 if (! utf8_key)
2942 /* Remove the key and keep just the label. */
2943 g_object_ref (G_OBJECT (wlbl));
2944 gtk_container_remove (GTK_CONTAINER (w), wchild);
2945 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2946 g_object_unref (G_OBJECT (wlbl));
2947 wkey = 0;
2951 else /* Just a label. */
2953 wlbl = GTK_LABEL (wchild);
2955 /* Check if there is now a key. */
2956 if (utf8_key)
2958 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2959 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2961 wlbl = GTK_LABEL (list->data);
2962 wkey = GTK_LABEL (list->next->data);
2963 g_list_free (list);
2965 gtk_container_remove (GTK_CONTAINER (w), wchild);
2966 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2970 if (wkey) old_key = gtk_label_get_label (wkey);
2971 if (wlbl) old_label = gtk_label_get_label (wlbl);
2973 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2975 label_changed = true;
2976 gtk_label_set_text (wkey, utf8_key);
2979 if (! old_label || strcmp (utf8_label, old_label) != 0)
2981 label_changed = true;
2982 gtk_label_set_text (wlbl, utf8_label);
2985 if (utf8_key) g_free (utf8_key);
2986 if (utf8_label) g_free (utf8_label);
2988 if (! val->enabled && gtk_widget_get_sensitive (w))
2989 gtk_widget_set_sensitive (w, FALSE);
2990 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2991 gtk_widget_set_sensitive (w, TRUE);
2993 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
2994 if (cb_data)
2996 cb_data->call_data = val->call_data;
2997 cb_data->help = val->help;
2998 cb_data->cl_data = cl_data;
3000 /* We assume the callback functions don't change. */
3001 if (val->call_data && ! val->contents)
3003 /* This item shall have a select callback. */
3004 if (! cb_data->select_id)
3005 cb_data->select_id
3006 = g_signal_connect (G_OBJECT (w), "activate",
3007 select_cb, cb_data);
3009 else if (cb_data->select_id)
3011 g_signal_handler_disconnect (w, cb_data->select_id);
3012 cb_data->select_id = 0;
3016 #if GTK_CHECK_VERSION (2, 16, 0)
3017 if (label_changed) /* See comment in xg_update_menubar. */
3018 g_object_notify (G_OBJECT (w), "label");
3019 #endif
3022 /* Update the toggle menu item W so it corresponds to VAL. */
3024 static void
3025 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3027 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3030 /* Update the radio menu item W so it corresponds to VAL. */
3032 static void
3033 xg_update_radio_item (widget_value *val, GtkWidget *w)
3035 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3038 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3039 SUBMENU may be NULL, in that case a new menu is created.
3040 F is the frame the menu bar belongs to.
3041 VAL describes the contents of the menu bar.
3042 SELECT_CB is the callback to use when a menu item is selected.
3043 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3044 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3045 CL_DATA is the call back data to use for any newly created items.
3047 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3048 was NULL. */
3050 static GtkWidget *
3051 xg_update_submenu (GtkWidget *submenu,
3052 struct frame *f,
3053 widget_value *val,
3054 GCallback select_cb,
3055 GCallback deactivate_cb,
3056 GCallback highlight_cb,
3057 xg_menu_cb_data *cl_data)
3059 GtkWidget *newsub = submenu;
3060 GList *list = 0;
3061 GList *iter;
3062 widget_value *cur;
3063 GList *first_radio = 0;
3065 if (submenu)
3066 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3068 for (cur = val, iter = list;
3069 cur && iter;
3070 iter = g_list_next (iter), cur = cur->next)
3072 GtkWidget *w = GTK_WIDGET (iter->data);
3074 /* Remember first radio button in a group. If we get a mismatch in
3075 a radio group we must rebuild the whole group so that the connections
3076 in GTK becomes correct. */
3077 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3078 first_radio = iter;
3079 else if (cur->button_type != BUTTON_TYPE_RADIO
3080 && ! GTK_IS_RADIO_MENU_ITEM (w))
3081 first_radio = 0;
3083 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3085 if (! menu_separator_name_p (cur->name))
3086 break;
3088 else if (GTK_IS_CHECK_MENU_ITEM (w))
3090 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3091 break;
3092 xg_update_toggle_item (cur, w);
3093 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3095 else if (GTK_IS_RADIO_MENU_ITEM (w))
3097 if (cur->button_type != BUTTON_TYPE_RADIO)
3098 break;
3099 xg_update_radio_item (cur, w);
3100 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3102 else if (GTK_IS_MENU_ITEM (w))
3104 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3105 GtkWidget *sub;
3107 if (cur->button_type != BUTTON_TYPE_NONE ||
3108 menu_separator_name_p (cur->name))
3109 break;
3111 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3113 sub = gtk_menu_item_get_submenu (witem);
3114 if (sub && ! cur->contents)
3116 /* Not a submenu anymore. */
3117 g_object_ref (G_OBJECT (sub));
3118 remove_submenu (witem);
3119 gtk_widget_destroy (sub);
3121 else if (cur->contents)
3123 GtkWidget *nsub;
3125 nsub = xg_update_submenu (sub, f, cur->contents,
3126 select_cb, deactivate_cb,
3127 highlight_cb, cl_data);
3129 /* If this item just became a submenu, we must set it. */
3130 if (nsub != sub)
3131 gtk_menu_item_set_submenu (witem, nsub);
3134 else
3136 /* Structural difference. Remove everything from here and down
3137 in SUBMENU. */
3138 break;
3142 /* Remove widgets from first structural change. */
3143 if (iter)
3145 /* If we are adding new menu items below, we must remove from
3146 first radio button so that radio groups become correct. */
3147 if (cur && first_radio) xg_destroy_widgets (first_radio);
3148 else xg_destroy_widgets (iter);
3151 if (cur)
3153 /* More items added. Create them. */
3154 newsub = create_menus (cur,
3156 select_cb,
3157 deactivate_cb,
3158 highlight_cb,
3161 submenu,
3162 cl_data,
3166 if (list) g_list_free (list);
3168 return newsub;
3171 /* Update the MENUBAR.
3172 F is the frame the menu bar belongs to.
3173 VAL describes the contents of the menu bar.
3174 If DEEP_P, rebuild all but the top level menu names in
3175 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3176 SELECT_CB is the callback to use when a menu item is selected.
3177 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3178 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3180 void
3181 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3182 widget_value *val, bool deep_p,
3183 GCallback select_cb, GCallback deactivate_cb,
3184 GCallback highlight_cb)
3186 xg_menu_cb_data *cl_data;
3187 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3189 if (! list) return;
3191 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3193 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3194 select_cb, deactivate_cb, highlight_cb, cl_data);
3196 if (deep_p)
3198 widget_value *cur;
3200 /* Update all sub menus.
3201 We must keep the submenus (GTK menu item widgets) since the
3202 X Window in the XEvent that activates the menu are those widgets. */
3204 /* Update cl_data, menu_item things in F may have changed. */
3205 update_cl_data (cl_data, f, highlight_cb);
3207 for (cur = val->contents; cur; cur = cur->next)
3209 GList *iter;
3210 GtkWidget *sub = 0;
3211 GtkWidget *newsub;
3212 GtkMenuItem *witem = 0;
3214 /* Find sub menu that corresponds to val and update it. */
3215 for (iter = list ; iter; iter = g_list_next (iter))
3217 witem = GTK_MENU_ITEM (iter->data);
3218 if (xg_item_label_same_p (witem, cur->name))
3220 sub = gtk_menu_item_get_submenu (witem);
3221 break;
3225 newsub = xg_update_submenu (sub,
3227 cur->contents,
3228 select_cb,
3229 deactivate_cb,
3230 highlight_cb,
3231 cl_data);
3232 /* sub may still be NULL. If we just updated non deep and added
3233 a new menu bar item, it has no sub menu yet. So we set the
3234 newly created sub menu under witem. */
3235 if (newsub != sub && witem != 0)
3237 xg_set_screen (newsub, f);
3238 gtk_menu_item_set_submenu (witem, newsub);
3243 g_list_free (list);
3244 gtk_widget_show_all (menubar);
3247 /* Callback called when the menu bar W is mapped.
3248 Used to find the height of the menu bar if we didn't get it
3249 after showing the widget. */
3251 static void
3252 menubar_map_cb (GtkWidget *w, gpointer user_data)
3254 GtkRequisition req;
3255 struct frame *f = user_data;
3256 gtk_widget_get_preferred_size (w, NULL, &req);
3257 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3259 FRAME_MENUBAR_HEIGHT (f) = req.height;
3260 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3264 /* Recompute all the widgets of frame F, when the menu bar has been
3265 changed. */
3267 void
3268 xg_update_frame_menubar (struct frame *f)
3270 struct x_output *x = f->output_data.x;
3271 GtkRequisition req;
3273 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3274 return;
3276 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3277 return; /* Already done this, happens for frames created invisible. */
3279 block_input ();
3281 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3282 FALSE, FALSE, 0);
3283 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3285 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3286 gtk_widget_show_all (x->menubar_widget);
3287 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3289 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3291 FRAME_MENUBAR_HEIGHT (f) = req.height;
3292 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3294 unblock_input ();
3297 /* Get rid of the menu bar of frame F, and free its storage.
3298 This is used when deleting a frame, and when turning off the menu bar. */
3300 void
3301 free_frame_menubar (struct frame *f)
3303 struct x_output *x = f->output_data.x;
3305 if (x->menubar_widget)
3307 block_input ();
3309 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3310 /* The menubar and its children shall be deleted when removed from
3311 the container. */
3312 x->menubar_widget = 0;
3313 FRAME_MENUBAR_HEIGHT (f) = 0;
3314 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3315 unblock_input ();
3319 bool
3320 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3322 struct x_output *x = f->output_data.x;
3323 GList *iter;
3324 GdkRectangle rec;
3325 GList *list;
3326 GdkDisplay *gdpy;
3327 GdkWindow *gw;
3328 GdkEvent gevent;
3329 GtkWidget *gwdesc;
3331 if (! x->menubar_widget) return 0;
3333 if (! (event->xbutton.x >= 0
3334 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3335 && event->xbutton.y >= 0
3336 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3337 && event->xbutton.same_screen))
3338 return 0;
3340 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3341 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3342 if (! gw) return 0;
3343 gevent.any.window = gw;
3344 gevent.any.type = GDK_NOTHING;
3345 gwdesc = gtk_get_event_widget (&gevent);
3346 if (! gwdesc) return 0;
3347 if (! GTK_IS_MENU_BAR (gwdesc)
3348 && ! GTK_IS_MENU_ITEM (gwdesc)
3349 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3350 return 0;
3352 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3353 if (! list) return 0;
3354 rec.x = event->xbutton.x;
3355 rec.y = event->xbutton.y;
3356 rec.width = 1;
3357 rec.height = 1;
3359 for (iter = list ; iter; iter = g_list_next (iter))
3361 GtkWidget *w = GTK_WIDGET (iter->data);
3362 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3363 break;
3365 g_list_free (list);
3366 return iter != 0;
3371 /***********************************************************************
3372 Scroll bar functions
3373 ***********************************************************************/
3376 /* Setting scroll bar values invokes the callback. Use this variable
3377 to indicate that callback should do nothing. */
3379 bool xg_ignore_gtk_scrollbar;
3381 /* Width and height of scroll bars for the current theme. */
3382 static int scroll_bar_width_for_theme;
3383 static int scroll_bar_height_for_theme;
3385 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3386 may be larger than 32 bits. Keep a mapping from integer index to widget
3387 pointers to get around the 32 bit limitation. */
3389 static struct
3391 GtkWidget **widgets;
3392 ptrdiff_t max_size;
3393 ptrdiff_t used;
3394 } id_to_widget;
3396 /* Grow this much every time we need to allocate more */
3398 #define ID_TO_WIDGET_INCR 32
3400 /* Store the widget pointer W in id_to_widget and return the integer index. */
3402 static ptrdiff_t
3403 xg_store_widget_in_map (GtkWidget *w)
3405 ptrdiff_t i;
3407 if (id_to_widget.max_size == id_to_widget.used)
3409 ptrdiff_t new_size;
3410 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3411 memory_full (SIZE_MAX);
3413 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3414 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3415 new_size, sizeof (GtkWidget *));
3417 for (i = id_to_widget.max_size; i < new_size; ++i)
3418 id_to_widget.widgets[i] = 0;
3419 id_to_widget.max_size = new_size;
3422 /* Just loop over the array and find a free place. After all,
3423 how many scroll bars are we creating? Should be a small number.
3424 The check above guarantees we will find a free place. */
3425 for (i = 0; i < id_to_widget.max_size; ++i)
3427 if (! id_to_widget.widgets[i])
3429 id_to_widget.widgets[i] = w;
3430 ++id_to_widget.used;
3432 return i;
3436 /* Should never end up here */
3437 emacs_abort ();
3440 /* Remove pointer at IDX from id_to_widget.
3441 Called when scroll bar is destroyed. */
3443 static void
3444 xg_remove_widget_from_map (ptrdiff_t idx)
3446 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3448 id_to_widget.widgets[idx] = 0;
3449 --id_to_widget.used;
3453 /* Get the widget pointer at IDX from id_to_widget. */
3455 static GtkWidget *
3456 xg_get_widget_from_map (ptrdiff_t idx)
3458 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3459 return id_to_widget.widgets[idx];
3461 return 0;
3464 static void
3465 update_theme_scrollbar_width (void)
3467 #ifdef HAVE_GTK3
3468 GtkAdjustment *vadj;
3469 #else
3470 GtkObject *vadj;
3471 #endif
3472 GtkWidget *wscroll;
3473 int w = 0, b = 0;
3475 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3476 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3477 g_object_ref_sink (G_OBJECT (wscroll));
3478 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3479 gtk_widget_destroy (wscroll);
3480 g_object_unref (G_OBJECT (wscroll));
3481 w += 2*b;
3482 if (w < 16) w = 16;
3483 scroll_bar_width_for_theme = w;
3486 static void
3487 update_theme_scrollbar_height (void)
3489 #ifdef HAVE_GTK3
3490 GtkAdjustment *hadj;
3491 #else
3492 GtkObject *hadj;
3493 #endif
3494 GtkWidget *wscroll;
3495 int w = 0, b = 0;
3497 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3498 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3499 g_object_ref_sink (G_OBJECT (wscroll));
3500 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3501 gtk_widget_destroy (wscroll);
3502 g_object_unref (G_OBJECT (wscroll));
3503 w += 2*b;
3504 if (w < 12) w = 12;
3505 scroll_bar_height_for_theme = w;
3509 xg_get_default_scrollbar_width (void)
3511 return scroll_bar_width_for_theme;
3515 xg_get_default_scrollbar_height (void)
3517 /* Apparently there's no default height for themes. */
3518 return scroll_bar_width_for_theme;
3521 /* Return the scrollbar id for X Window WID on display DPY.
3522 Return -1 if WID not in id_to_widget. */
3524 ptrdiff_t
3525 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3527 ptrdiff_t idx;
3528 GtkWidget *w;
3530 w = xg_win_to_widget (dpy, wid);
3532 if (w)
3534 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3535 if (id_to_widget.widgets[idx] == w)
3536 return idx;
3539 return -1;
3542 /* Callback invoked when scroll bar WIDGET is destroyed.
3543 DATA is the index into id_to_widget for WIDGET.
3544 We free pointer to last scroll bar values here and remove the index. */
3546 static void
3547 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3549 intptr_t id = (intptr_t) data;
3550 xg_remove_widget_from_map (id);
3553 /* Create a scroll bar widget for frame F. Store the scroll bar
3554 in BAR.
3555 SCROLL_CALLBACK is the callback to invoke when the value of the
3556 bar changes.
3557 END_CALLBACK is the callback to invoke when scrolling ends.
3558 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3559 to set resources for the widget. */
3561 void
3562 xg_create_scroll_bar (struct frame *f,
3563 struct scroll_bar *bar,
3564 GCallback scroll_callback,
3565 GCallback end_callback,
3566 const char *scroll_bar_name)
3568 GtkWidget *wscroll;
3569 GtkWidget *webox;
3570 intptr_t scroll_id;
3571 #ifdef HAVE_GTK3
3572 GtkAdjustment *vadj;
3573 #else
3574 GtkObject *vadj;
3575 #endif
3577 /* Page, step increment values are not so important here, they
3578 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3579 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3580 0.1, 0.1, 0.1);
3582 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3583 webox = gtk_event_box_new ();
3584 gtk_widget_set_name (wscroll, scroll_bar_name);
3585 #ifndef HAVE_GTK3
3586 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3587 #endif
3588 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3590 scroll_id = xg_store_widget_in_map (wscroll);
3592 g_signal_connect (G_OBJECT (wscroll),
3593 "destroy",
3594 G_CALLBACK (xg_gtk_scroll_destroy),
3595 (gpointer) scroll_id);
3596 g_signal_connect (G_OBJECT (wscroll),
3597 "change-value",
3598 scroll_callback,
3599 (gpointer) bar);
3600 g_signal_connect (G_OBJECT (wscroll),
3601 "button-release-event",
3602 end_callback,
3603 (gpointer) bar);
3605 /* The scroll bar widget does not draw on a window of its own. Instead
3606 it draws on the parent window, in this case the edit widget. So
3607 whenever the edit widget is cleared, the scroll bar needs to redraw
3608 also, which causes flicker. Put an event box between the edit widget
3609 and the scroll bar, so the scroll bar instead draws itself on the
3610 event box window. */
3611 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3612 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3615 /* Set the cursor to an arrow. */
3616 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3618 bar->x_window = scroll_id;
3619 bar->horizontal = 0;
3622 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3623 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3624 of the bar changes. END_CALLBACK is the callback to invoke when
3625 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3626 bar. Can be used to set resources for the widget. */
3628 void
3629 xg_create_horizontal_scroll_bar (struct frame *f,
3630 struct scroll_bar *bar,
3631 GCallback scroll_callback,
3632 GCallback end_callback,
3633 const char *scroll_bar_name)
3635 GtkWidget *wscroll;
3636 GtkWidget *webox;
3637 intptr_t scroll_id;
3638 #ifdef HAVE_GTK3
3639 GtkAdjustment *hadj;
3640 #else
3641 GtkObject *hadj;
3642 #endif
3644 /* Page, step increment values are not so important here, they
3645 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3646 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3647 0.1, 0.1, 0.1);
3649 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3650 webox = gtk_event_box_new ();
3651 gtk_widget_set_name (wscroll, scroll_bar_name);
3652 #ifndef HAVE_GTK3
3653 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3654 #endif
3655 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3657 scroll_id = xg_store_widget_in_map (wscroll);
3659 g_signal_connect (G_OBJECT (wscroll),
3660 "destroy",
3661 G_CALLBACK (xg_gtk_scroll_destroy),
3662 (gpointer) scroll_id);
3663 g_signal_connect (G_OBJECT (wscroll),
3664 "change-value",
3665 scroll_callback,
3666 (gpointer) bar);
3667 g_signal_connect (G_OBJECT (wscroll),
3668 "button-release-event",
3669 end_callback,
3670 (gpointer) bar);
3672 /* The scroll bar widget does not draw on a window of its own. Instead
3673 it draws on the parent window, in this case the edit widget. So
3674 whenever the edit widget is cleared, the scroll bar needs to redraw
3675 also, which causes flicker. Put an event box between the edit widget
3676 and the scroll bar, so the scroll bar instead draws itself on the
3677 event box window. */
3678 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3679 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3682 /* Set the cursor to an arrow. */
3683 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3685 bar->x_window = scroll_id;
3686 bar->horizontal = 1;
3689 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3691 void
3692 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3694 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3695 if (w)
3697 GtkWidget *wparent = gtk_widget_get_parent (w);
3698 gtk_widget_destroy (w);
3699 gtk_widget_destroy (wparent);
3700 SET_FRAME_GARBAGED (f);
3704 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3705 in frame F.
3706 TOP/LEFT are the new pixel positions where the bar shall appear.
3707 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3709 void
3710 xg_update_scrollbar_pos (struct frame *f,
3711 ptrdiff_t scrollbar_id,
3712 int top,
3713 int left,
3714 int width,
3715 int height)
3718 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3720 if (wscroll)
3722 GtkWidget *wfixed = f->output_data.x->edit_widget;
3723 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3724 gint msl;
3726 /* Clear out old position. */
3727 int oldx = -1, oldy = -1, oldw, oldh;
3728 if (gtk_widget_get_parent (wparent) == wfixed)
3730 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3731 "x", &oldx, "y", &oldy, NULL);
3732 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3735 /* Move and resize to new values. */
3736 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3737 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3738 if (msl > height)
3740 /* No room. Hide scroll bar as some themes output a warning if
3741 the height is less than the min size. */
3742 gtk_widget_hide (wparent);
3743 gtk_widget_hide (wscroll);
3745 else
3747 gtk_widget_show_all (wparent);
3748 gtk_widget_set_size_request (wscroll, width, height);
3750 gtk_widget_queue_draw (wfixed);
3751 gdk_window_process_all_updates ();
3752 if (oldx != -1 && oldw > 0 && oldh > 0)
3753 /* Clear under old scroll bar position. This must be done after
3754 the gtk_widget_queue_draw and gdk_window_process_all_updates
3755 above. */
3756 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3757 oldx, oldy, oldw, oldh);
3759 /* GTK does not redraw until the main loop is entered again, but
3760 if there are no X events pending we will not enter it. So we sync
3761 here to get some events. */
3763 x_sync (f);
3764 SET_FRAME_GARBAGED (f);
3765 cancel_mouse_face (f);
3770 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3771 in frame F.
3772 TOP/LEFT are the new pixel positions where the bar shall appear.
3773 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3775 void
3776 xg_update_horizontal_scrollbar_pos (struct frame *f,
3777 ptrdiff_t scrollbar_id,
3778 int top,
3779 int left,
3780 int width,
3781 int height)
3784 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3786 if (wscroll)
3788 GtkWidget *wfixed = f->output_data.x->edit_widget;
3789 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3790 gint msl;
3792 /* Clear out old position. */
3793 int oldx = -1, oldy = -1, oldw, oldh;
3794 if (gtk_widget_get_parent (wparent) == wfixed)
3796 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3797 "x", &oldx, "y", &oldy, NULL);
3798 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3801 /* Move and resize to new values. */
3802 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3803 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3804 if (msl > width)
3806 /* No room. Hide scroll bar as some themes output a warning if
3807 the width is less than the min size. */
3808 gtk_widget_hide (wparent);
3809 gtk_widget_hide (wscroll);
3811 else
3813 gtk_widget_show_all (wparent);
3814 gtk_widget_set_size_request (wscroll, width, height);
3816 gtk_widget_queue_draw (wfixed);
3817 gdk_window_process_all_updates ();
3818 if (oldx != -1 && oldw > 0 && oldh > 0)
3819 /* Clear under old scroll bar position. This must be done after
3820 the gtk_widget_queue_draw and gdk_window_process_all_updates
3821 above. */
3822 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3823 oldx, oldy, oldw, oldh);
3825 /* GTK does not redraw until the main loop is entered again, but
3826 if there are no X events pending we will not enter it. So we sync
3827 here to get some events. */
3829 x_sync (f);
3830 SET_FRAME_GARBAGED (f);
3831 cancel_mouse_face (f);
3836 /* Get the current value of the range, truncated to an integer. */
3838 static int
3839 int_gtk_range_get_value (GtkRange *range)
3841 return gtk_range_get_value (range);
3845 /* Set the thumb size and position of scroll bar BAR. We are currently
3846 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3848 void
3849 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3850 int portion,
3851 int position,
3852 int whole)
3854 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3856 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3858 if (wscroll && bar->dragging == -1)
3860 GtkAdjustment *adj;
3861 gdouble shown;
3862 gdouble top;
3863 int size, value;
3864 int old_size;
3865 int new_step;
3866 bool changed = 0;
3868 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3870 if (scroll_bar_adjust_thumb_portion_p)
3872 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3873 line rather than the real portion value. This makes the
3874 thumb less likely to resize and that looks better. */
3875 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3877 /* When the thumb is at the bottom, position == whole.
3878 So we need to increase `whole' to make space for the thumb. */
3879 whole += portion;
3882 if (whole <= 0)
3883 top = 0, shown = 1;
3884 else
3886 top = (gdouble) position / whole;
3887 shown = (gdouble) portion / whole;
3890 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3891 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3893 /* Assume all lines are of equal size. */
3894 new_step = size / max (1, FRAME_LINES (f));
3896 old_size = gtk_adjustment_get_page_size (adj);
3897 if (old_size != size)
3899 int old_step = gtk_adjustment_get_step_increment (adj);
3900 if (old_step != new_step)
3902 gtk_adjustment_set_page_size (adj, size);
3903 gtk_adjustment_set_step_increment (adj, new_step);
3904 /* Assume a page increment is about 95% of the page size */
3905 gtk_adjustment_set_page_increment (adj, size - size / 20);
3906 changed = 1;
3910 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3912 block_input ();
3914 /* gtk_range_set_value invokes the callback. Set
3915 ignore_gtk_scrollbar to make the callback do nothing */
3916 xg_ignore_gtk_scrollbar = 1;
3918 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3919 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3920 else if (changed)
3921 gtk_adjustment_changed (adj);
3923 xg_ignore_gtk_scrollbar = 0;
3925 unblock_input ();
3930 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3931 currently displaying PORTION out of a whole WHOLE, and our position
3932 POSITION. */
3933 void
3934 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3935 int portion,
3936 int position,
3937 int whole)
3939 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3941 if (wscroll && bar->dragging == -1)
3943 GtkAdjustment *adj;
3944 int lower = 0;
3945 int upper = max (whole - 1, 0);
3946 int pagesize = min (upper, max (portion, 0));
3947 int value = max (0, min (position, upper - pagesize));
3948 /* These should be set to something more <portion, whole>
3949 related. */
3950 int page_increment = 4;
3951 int step_increment = 1;
3953 block_input ();
3954 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3955 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
3956 (gdouble) upper, (gdouble) step_increment,
3957 (gdouble) page_increment, (gdouble) pagesize);
3958 gtk_adjustment_changed (adj);
3959 unblock_input ();
3963 /* Return true if EVENT is for a scroll bar in frame F.
3964 When the same X window is used for several Gtk+ widgets, we cannot
3965 say for sure based on the X window alone if an event is for the
3966 frame. This function does additional checks. */
3968 bool
3969 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
3971 bool retval = 0;
3973 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3975 /* Check if press occurred outside the edit widget. */
3976 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3977 GdkWindow *gwin;
3978 #ifdef HAVE_GTK3
3979 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3980 (gdk_display_get_device_manager (gdpy));
3981 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
3982 #else
3983 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3984 #endif
3985 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
3987 else if (f
3988 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3989 || event->type == MotionNotify))
3991 /* If we are releasing or moving the scroll bar, it has the grab. */
3992 GtkWidget *w = gtk_grab_get_current ();
3993 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3996 return retval;
4001 /***********************************************************************
4002 Tool bar functions
4003 ***********************************************************************/
4004 /* The key for the data we put in the GtkImage widgets. The data is
4005 the image used by Emacs. We use this to see if we need to update
4006 the GtkImage with a new image. */
4007 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
4009 /* The key for storing the latest modifiers so the activate callback can
4010 get them. */
4011 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4013 /* The key for the data we put in the GtkImage widgets. The data is
4014 the stock name used by Emacs. We use this to see if we need to update
4015 the GtkImage with a new image. */
4016 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4018 /* As above, but this is used for named theme widgets, as opposed to
4019 stock items. */
4020 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4022 /* Callback function invoked when a tool bar item is pressed.
4023 W is the button widget in the tool bar that got pressed,
4024 CLIENT_DATA is an integer that is the index of the button in the
4025 tool bar. 0 is the first button. */
4027 static gboolean
4028 xg_tool_bar_button_cb (GtkWidget *widget,
4029 GdkEventButton *event,
4030 gpointer user_data)
4032 intptr_t state = event->state;
4033 gpointer ptr = (gpointer) state;
4034 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4035 return FALSE;
4039 /* Callback function invoked when a tool bar item is pressed.
4040 W is the button widget in the tool bar that got pressed,
4041 CLIENT_DATA is an integer that is the index of the button in the
4042 tool bar. 0 is the first button. */
4044 static void
4045 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4047 intptr_t idx = (intptr_t) client_data;
4048 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4049 intptr_t mod = (intptr_t) gmod;
4051 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4052 Lisp_Object key, frame;
4053 struct input_event event;
4054 EVENT_INIT (event);
4056 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4057 return;
4059 idx *= TOOL_BAR_ITEM_NSLOTS;
4061 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4062 XSETFRAME (frame, f);
4064 /* We generate two events here. The first one is to set the prefix
4065 to `(tool_bar)', see keyboard.c. */
4066 event.kind = TOOL_BAR_EVENT;
4067 event.frame_or_window = frame;
4068 event.arg = frame;
4069 kbd_buffer_store_event (&event);
4071 event.kind = TOOL_BAR_EVENT;
4072 event.frame_or_window = frame;
4073 event.arg = key;
4074 /* Convert between the modifier bits GDK uses and the modifier bits
4075 Emacs uses. This assumes GDK and X masks are the same, which they are when
4076 this is written. */
4077 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4078 kbd_buffer_store_event (&event);
4080 /* Return focus to the frame after we have clicked on a detached
4081 tool bar button. */
4082 x_focus_frame (f);
4085 static GtkWidget *
4086 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4088 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4089 GtkWidget *c1 = clist->data;
4090 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4092 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4093 g_list_free (clist);
4094 return GTK_IS_LABEL (c1) ? c1 : c2;
4098 /* This callback is called when the mouse enters or leaves a tool bar item.
4099 It is used for displaying and hiding the help text.
4100 W is the tool bar item, a button.
4101 EVENT is either an enter event or leave event.
4102 CLIENT_DATA is an integer that is the index of the button in the
4103 tool bar. 0 is the first button.
4105 Returns FALSE to tell GTK to keep processing this event. */
4107 static gboolean
4108 xg_tool_bar_help_callback (GtkWidget *w,
4109 GdkEventCrossing *event,
4110 gpointer client_data)
4112 intptr_t idx = (intptr_t) client_data;
4113 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4114 Lisp_Object help, frame;
4116 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4117 return FALSE;
4119 if (event->type == GDK_ENTER_NOTIFY)
4121 idx *= TOOL_BAR_ITEM_NSLOTS;
4122 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4124 if (NILP (help))
4125 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4127 else
4128 help = Qnil;
4130 XSETFRAME (frame, f);
4131 kbd_buffer_store_help_event (frame, help);
4133 return FALSE;
4137 /* This callback is called when a tool bar item shall be redrawn.
4138 It modifies the expose event so that the GtkImage widget redraws the
4139 whole image. This to overcome a bug that makes GtkImage draw the image
4140 in the wrong place when it tries to redraw just a part of the image.
4141 W is the GtkImage to be redrawn.
4142 EVENT is the expose event for W.
4143 CLIENT_DATA is unused.
4145 Returns FALSE to tell GTK to keep processing this event. */
4147 #ifndef HAVE_GTK3
4148 static gboolean
4149 xg_tool_bar_item_expose_callback (GtkWidget *w,
4150 GdkEventExpose *event,
4151 gpointer client_data)
4153 gint width, height;
4155 gdk_drawable_get_size (event->window, &width, &height);
4156 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4157 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4159 event->area.x = max (0, event->area.x);
4160 event->area.y = max (0, event->area.y);
4162 event->area.width = max (width, event->area.width);
4163 event->area.height = max (height, event->area.height);
4165 return FALSE;
4167 #endif
4169 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4170 #define toolbar_set_orientation(w, o) \
4171 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4172 #else
4173 #define toolbar_set_orientation(w, o) \
4174 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4175 #endif
4177 /* Attach a tool bar to frame F. */
4179 static void
4180 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4182 struct x_output *x = f->output_data.x;
4183 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4184 GtkWidget *top_widget = x->toolbar_widget;
4186 toolbar_set_orientation (x->toolbar_widget,
4187 into_hbox
4188 ? GTK_ORIENTATION_VERTICAL
4189 : GTK_ORIENTATION_HORIZONTAL);
4191 if (into_hbox)
4193 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4194 FALSE, FALSE, 0);
4196 if (EQ (pos, Qleft))
4197 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4198 top_widget,
4200 x->toolbar_in_hbox = true;
4202 else
4204 bool vbox_pos = x->menubar_widget != 0;
4205 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4206 FALSE, FALSE, 0);
4208 if (EQ (pos, Qtop))
4209 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4210 top_widget,
4211 vbox_pos);
4212 x->toolbar_in_hbox = false;
4214 x->toolbar_is_packed = true;
4217 static bool xg_update_tool_bar_sizes (struct frame *f);
4219 static void
4220 tb_size_cb (GtkWidget *widget,
4221 GdkRectangle *allocation,
4222 gpointer user_data)
4224 /* When tool bar is created it has one preferred size. But when size is
4225 allocated between widgets, it may get another. So we must update
4226 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4227 struct frame *f = user_data;
4228 if (xg_update_tool_bar_sizes (f))
4229 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4232 /* Create a tool bar for frame F. */
4234 static void
4235 xg_create_tool_bar (struct frame *f)
4237 struct x_output *x = f->output_data.x;
4238 #if GTK_CHECK_VERSION (3, 3, 6)
4239 GtkStyleContext *gsty;
4240 #endif
4241 struct xg_frame_tb_info *tbinfo
4242 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4243 TB_INFO_KEY);
4244 if (! tbinfo)
4246 tbinfo = xmalloc (sizeof (*tbinfo));
4247 tbinfo->last_tool_bar = Qnil;
4248 tbinfo->style = Qnil;
4249 tbinfo->hmargin = tbinfo->vmargin = 0;
4250 tbinfo->dir = GTK_TEXT_DIR_NONE;
4251 tbinfo->n_last_items = 0;
4252 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4253 TB_INFO_KEY,
4254 tbinfo);
4257 x->toolbar_widget = gtk_toolbar_new ();
4259 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4261 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4262 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4263 g_signal_connect (x->toolbar_widget, "size-allocate",
4264 G_CALLBACK (tb_size_cb), f);
4265 #if GTK_CHECK_VERSION (3, 3, 6)
4266 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4267 gtk_style_context_add_class (gsty, "primary-toolbar");
4268 #endif
4272 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4274 /* Find the right-to-left image named by RTL in the tool bar images for F.
4275 Returns IMAGE if RTL is not found. */
4277 static Lisp_Object
4278 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4280 int i;
4281 Lisp_Object file, rtl_name;
4282 struct gcpro gcpro1, gcpro2;
4283 GCPRO2 (file, rtl_name);
4285 rtl_name = Ffile_name_nondirectory (rtl);
4287 for (i = 0; i < f->n_tool_bar_items; ++i)
4289 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4290 if (!NILP (file = file_for_image (rtl_image)))
4292 file = call1 (intern ("file-name-sans-extension"),
4293 Ffile_name_nondirectory (file));
4294 if (! NILP (Fequal (file, rtl_name)))
4296 image = rtl_image;
4297 break;
4302 return image;
4305 static GtkToolItem *
4306 xg_make_tool_item (struct frame *f,
4307 GtkWidget *wimage,
4308 GtkWidget **wbutton,
4309 const char *label,
4310 int i, bool horiz, bool text_image)
4312 GtkToolItem *ti = gtk_tool_item_new ();
4313 GtkWidget *vb = gtk_box_new (horiz
4314 ? GTK_ORIENTATION_HORIZONTAL
4315 : GTK_ORIENTATION_VERTICAL,
4317 GtkWidget *wb = gtk_button_new ();
4318 /* The eventbox is here so we can have tooltips on disabled items. */
4319 GtkWidget *weventbox = gtk_event_box_new ();
4320 #if GTK_CHECK_VERSION (3, 3, 6)
4321 GtkCssProvider *css_prov = gtk_css_provider_new ();
4322 GtkStyleContext *gsty;
4324 gtk_css_provider_load_from_data (css_prov,
4325 "GtkEventBox {"
4326 " background-color: transparent;"
4327 "}",
4328 -1, NULL);
4330 gsty = gtk_widget_get_style_context (weventbox);
4331 gtk_style_context_add_provider (gsty,
4332 GTK_STYLE_PROVIDER (css_prov),
4333 GTK_STYLE_PROVIDER_PRIORITY_USER);
4334 g_object_unref (css_prov);
4335 #endif
4337 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4339 if (wimage && !text_image)
4340 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4341 if (label)
4342 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4343 if (wimage && text_image)
4344 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4346 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4347 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4348 gtk_container_add (GTK_CONTAINER (wb), vb);
4349 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4350 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4352 if (wimage || label)
4354 intptr_t ii = i;
4355 gpointer gi = (gpointer) ii;
4357 g_signal_connect (G_OBJECT (wb), "clicked",
4358 G_CALLBACK (xg_tool_bar_callback),
4359 gi);
4361 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4363 #ifndef HAVE_GTK3
4364 /* Catch expose events to overcome an annoying redraw bug, see
4365 comment for xg_tool_bar_item_expose_callback. */
4366 g_signal_connect (G_OBJECT (ti),
4367 "expose-event",
4368 G_CALLBACK (xg_tool_bar_item_expose_callback),
4370 #endif
4371 gtk_tool_item_set_homogeneous (ti, FALSE);
4373 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4374 no distinction based on modifiers in the activate callback,
4375 so we have to do it ourselves. */
4376 g_signal_connect (wb, "button-release-event",
4377 G_CALLBACK (xg_tool_bar_button_cb),
4378 NULL);
4380 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4382 /* Use enter/leave notify to show help. We use the events
4383 rather than the GtkButton specific signals "enter" and
4384 "leave", so we can have only one callback. The event
4385 will tell us what kind of event it is. */
4386 g_signal_connect (G_OBJECT (weventbox),
4387 "enter-notify-event",
4388 G_CALLBACK (xg_tool_bar_help_callback),
4389 gi);
4390 g_signal_connect (G_OBJECT (weventbox),
4391 "leave-notify-event",
4392 G_CALLBACK (xg_tool_bar_help_callback),
4393 gi);
4396 if (wbutton) *wbutton = wb;
4398 return ti;
4401 static bool
4402 is_box_type (GtkWidget *vb, bool is_horizontal)
4404 #ifdef HAVE_GTK3
4405 bool ret = 0;
4406 if (GTK_IS_BOX (vb))
4408 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4409 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4410 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4412 return ret;
4413 #else
4414 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4415 #endif
4419 static bool
4420 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4421 const char *icon_name, const struct image *img,
4422 const char *label, bool horiz)
4424 gpointer old;
4425 GtkWidget *wimage;
4426 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4427 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4429 /* Check if the tool icon matches. */
4430 if (stock_name && wimage)
4432 old = g_object_get_data (G_OBJECT (wimage),
4433 XG_TOOL_BAR_STOCK_NAME);
4434 if (!old || strcmp (old, stock_name))
4435 return 1;
4437 else if (icon_name && wimage)
4439 old = g_object_get_data (G_OBJECT (wimage),
4440 XG_TOOL_BAR_ICON_NAME);
4441 if (!old || strcmp (old, icon_name))
4442 return 1;
4444 else if (wimage)
4446 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4447 XG_TOOL_BAR_IMAGE_DATA);
4448 Pixmap old_img = (Pixmap) gold_img;
4449 if (old_img != img->pixmap)
4450 return 1;
4453 /* Check button configuration and label. */
4454 if (is_box_type (vb, horiz)
4455 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4456 return 1;
4458 /* Ensure label is correct. */
4459 if (label && wlbl)
4460 gtk_label_set_text (GTK_LABEL (wlbl), label);
4461 return 0;
4464 static bool
4465 xg_update_tool_bar_sizes (struct frame *f)
4467 struct x_output *x = f->output_data.x;
4468 GtkRequisition req;
4469 int nl = 0, nr = 0, nt = 0, nb = 0;
4470 GtkWidget *top_widget = x->toolbar_widget;
4472 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4473 if (x->toolbar_in_hbox)
4475 int pos;
4476 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4477 top_widget,
4478 "position", &pos, NULL);
4479 if (pos == 0) nl = req.width;
4480 else nr = req.width;
4482 else
4484 int pos;
4485 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4486 top_widget,
4487 "position", &pos, NULL);
4488 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4489 else nb = req.height;
4492 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4493 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4494 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4495 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4497 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4498 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4499 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4500 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4501 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4502 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4503 return 1;
4506 return 0;
4509 static char *
4510 find_icon_from_name (char *name,
4511 GtkIconTheme *icon_theme,
4512 char **icon_name)
4514 #if ! GTK_CHECK_VERSION (3, 10, 0)
4515 GtkStockItem stock_item;
4516 #endif
4518 if (name[0] == 'n' && name[1] == ':')
4520 *icon_name = name + 2;
4521 name = NULL;
4523 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4524 *icon_name = NULL;
4527 #if ! GTK_CHECK_VERSION (3, 10, 0)
4528 else if (gtk_stock_lookup (name, &stock_item))
4529 *icon_name = NULL;
4530 #endif
4531 else if (gtk_icon_theme_has_icon (icon_theme, name))
4533 *icon_name = name;
4534 name = NULL;
4536 else
4538 name = NULL;
4539 *icon_name = NULL;
4542 return name;
4546 /* Update the tool bar for frame F. Add new buttons and remove old. */
4548 void
4549 update_frame_tool_bar (struct frame *f)
4551 int i, j;
4552 struct x_output *x = f->output_data.x;
4553 int hmargin = 0, vmargin = 0;
4554 GtkToolbar *wtoolbar;
4555 GtkToolItem *ti;
4556 GtkTextDirection dir;
4557 Lisp_Object style;
4558 bool text_image, horiz;
4559 struct xg_frame_tb_info *tbinfo;
4560 GdkScreen *screen;
4561 GtkIconTheme *icon_theme;
4564 if (! FRAME_GTK_WIDGET (f))
4565 return;
4567 block_input ();
4569 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4571 hmargin = XFASTINT (Vtool_bar_button_margin);
4572 vmargin = XFASTINT (Vtool_bar_button_margin);
4574 else if (CONSP (Vtool_bar_button_margin))
4576 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4577 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4579 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4580 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4583 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4584 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4585 i.e. zero. This means that margins less than
4586 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4587 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4588 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4590 if (! x->toolbar_widget)
4591 xg_create_tool_bar (f);
4593 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4594 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4596 style = Ftool_bar_get_system_style ();
4597 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4598 icon_theme = gtk_icon_theme_get_for_screen (screen);
4600 /* Are we up to date? */
4601 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4602 TB_INFO_KEY);
4604 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4605 && tbinfo->n_last_items == f->n_tool_bar_items
4606 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4607 && tbinfo->dir == dir
4608 && ! NILP (Fequal (tbinfo->style, style))
4609 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4611 unblock_input ();
4612 return;
4615 tbinfo->last_tool_bar = f->tool_bar_items;
4616 tbinfo->n_last_items = f->n_tool_bar_items;
4617 tbinfo->style = style;
4618 tbinfo->hmargin = hmargin;
4619 tbinfo->vmargin = vmargin;
4620 tbinfo->dir = dir;
4622 text_image = EQ (style, Qtext_image_horiz);
4623 horiz = EQ (style, Qboth_horiz) || text_image;
4625 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4627 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4628 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4629 int idx;
4630 ptrdiff_t img_id;
4631 int icon_size = 0;
4632 struct image *img = NULL;
4633 Lisp_Object image;
4634 Lisp_Object stock = Qnil;
4635 char *stock_name = NULL;
4636 char *icon_name = NULL;
4637 Lisp_Object rtl;
4638 GtkWidget *wbutton = NULL;
4639 Lisp_Object specified_file;
4640 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4641 const char *label
4642 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4643 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4644 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4645 : "";
4647 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4649 /* If this is a separator, use a gtk separator item. */
4650 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4652 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4654 if (ti)
4655 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4656 GTK_WIDGET (ti));
4657 ti = gtk_separator_tool_item_new ();
4658 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4660 j++;
4661 continue;
4664 /* Otherwise, the tool-bar item is an ordinary button. */
4666 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4668 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4669 ti = NULL;
4672 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4674 /* Ignore invalid image specifications. */
4675 image = PROP (TOOL_BAR_ITEM_IMAGES);
4676 if (!valid_image_p (image))
4678 if (ti)
4679 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4680 GTK_WIDGET (ti));
4681 continue;
4684 specified_file = file_for_image (image);
4685 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4686 stock = call1 (Qx_gtk_map_stock, specified_file);
4688 if (CONSP (stock))
4690 Lisp_Object tem;
4691 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4692 if (! NILP (tem) && STRINGP (XCAR (tem)))
4694 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4695 icon_theme,
4696 &icon_name);
4697 if (stock_name || icon_name) break;
4700 else if (STRINGP (stock))
4702 stock_name = find_icon_from_name (SSDATA (stock),
4703 icon_theme,
4704 &icon_name);
4707 if (stock_name || icon_name)
4708 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4710 if (stock_name == NULL && icon_name == NULL)
4712 /* No stock image, or stock item not known. Try regular
4713 image. If image is a vector, choose it according to the
4714 button state. */
4715 if (dir == GTK_TEXT_DIR_RTL
4716 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4717 && STRINGP (rtl))
4718 image = find_rtl_image (f, image, rtl);
4720 if (VECTORP (image))
4722 if (enabled_p)
4723 idx = (selected_p
4724 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4725 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4726 else
4727 idx = (selected_p
4728 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4729 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4731 eassert (ASIZE (image) >= idx);
4732 image = AREF (image, idx);
4734 else
4735 idx = -1;
4737 img_id = lookup_image (f, image);
4738 img = IMAGE_FROM_ID (f, img_id);
4739 prepare_image_for_display (f, img);
4741 if (img->load_failed_p || img->pixmap == None)
4743 if (ti)
4744 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4745 GTK_WIDGET (ti));
4746 continue;
4750 /* If there is an existing widget, check if it's stale; if so,
4751 remove it and make a new tool item from scratch. */
4752 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4753 img, label, horiz))
4755 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4756 GTK_WIDGET (ti));
4757 ti = NULL;
4760 if (ti == NULL)
4762 GtkWidget *w;
4764 /* Save the image so we can see if an update is needed the
4765 next time we call xg_tool_item_match_p. */
4766 if (EQ (style, Qtext))
4767 w = NULL;
4768 else if (stock_name)
4771 #if GTK_CHECK_VERSION (3, 10, 0)
4772 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4773 #else
4774 w = gtk_image_new_from_stock (stock_name, icon_size);
4775 #endif
4776 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4777 (gpointer) xstrdup (stock_name),
4778 (GDestroyNotify) xfree);
4780 else if (icon_name)
4782 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4783 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4784 (gpointer) xstrdup (icon_name),
4785 (GDestroyNotify) xfree);
4787 else
4789 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4790 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4791 (gpointer)img->pixmap);
4794 #if GTK_CHECK_VERSION (3, 14, 0)
4795 if (w)
4797 gtk_widget_set_margin_start (w, hmargin);
4798 gtk_widget_set_margin_end (w, hmargin);
4799 gtk_widget_set_margin_top (w, vmargin);
4800 gtk_widget_set_margin_bottom (w, vmargin);
4802 #else
4803 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4804 #endif
4805 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4806 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4809 #undef PROP
4811 gtk_widget_set_sensitive (wbutton, enabled_p);
4812 j++;
4815 /* Remove buttons not longer needed. */
4818 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4819 if (ti)
4820 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4821 } while (ti != NULL);
4823 if (f->n_tool_bar_items != 0)
4825 if (! x->toolbar_is_packed)
4826 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4827 gtk_widget_show_all (x->toolbar_widget);
4828 if (xg_update_tool_bar_sizes (f))
4829 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4832 unblock_input ();
4835 /* Deallocate all resources for the tool bar on frame F.
4836 Remove the tool bar. */
4838 void
4839 free_frame_tool_bar (struct frame *f)
4841 struct x_output *x = f->output_data.x;
4843 if (x->toolbar_widget)
4845 struct xg_frame_tb_info *tbinfo;
4846 GtkWidget *top_widget = x->toolbar_widget;
4848 block_input ();
4849 if (x->toolbar_is_packed)
4851 if (x->toolbar_in_hbox)
4852 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4853 top_widget);
4854 else
4855 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4856 top_widget);
4858 else
4859 gtk_widget_destroy (x->toolbar_widget);
4861 x->toolbar_widget = 0;
4862 x->toolbar_widget = 0;
4863 x->toolbar_is_packed = false;
4864 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4865 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4867 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4868 TB_INFO_KEY);
4869 if (tbinfo)
4871 xfree (tbinfo);
4872 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4873 TB_INFO_KEY,
4874 NULL);
4877 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4879 unblock_input ();
4883 void
4884 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4886 struct x_output *x = f->output_data.x;
4887 GtkWidget *top_widget = x->toolbar_widget;
4889 if (! x->toolbar_widget || ! top_widget)
4890 return;
4892 block_input ();
4893 g_object_ref (top_widget);
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);
4904 xg_pack_tool_bar (f, pos);
4905 g_object_unref (top_widget);
4906 if (xg_update_tool_bar_sizes (f))
4907 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4909 unblock_input ();
4914 /***********************************************************************
4915 Initializing
4916 ***********************************************************************/
4917 void
4918 xg_initialize (void)
4920 GtkBindingSet *binding_set;
4921 GtkSettings *settings;
4923 #if HAVE_XFT
4924 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4925 we keep it permanently linked in. */
4926 XftInit (0);
4927 #endif
4929 gdpy_def = NULL;
4930 xg_ignore_gtk_scrollbar = 0;
4931 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4932 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4934 id_to_widget.max_size = id_to_widget.used = 0;
4935 id_to_widget.widgets = 0;
4937 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4938 (gdk_display_get_default ()));
4939 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4940 bindings. It doesn't seem to be any way to remove properties,
4941 so we set it to "" which in means "no key". */
4942 gtk_settings_set_string_property (settings,
4943 "gtk-menu-bar-accel",
4945 EMACS_CLASS);
4947 /* Make GTK text input widgets use Emacs style keybindings. This is
4948 Emacs after all. */
4949 gtk_settings_set_string_property (settings,
4950 "gtk-key-theme-name",
4951 "Emacs",
4952 EMACS_CLASS);
4954 /* Make dialogs close on C-g. Since file dialog inherits from
4955 dialog, this works for them also. */
4956 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4957 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4958 "close", 0);
4960 /* Make menus close on C-g. */
4961 binding_set = gtk_binding_set_by_class (g_type_class_ref
4962 (GTK_TYPE_MENU_SHELL));
4963 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4964 "cancel", 0);
4965 update_theme_scrollbar_width ();
4966 update_theme_scrollbar_height ();
4968 #ifdef HAVE_FREETYPE
4969 x_last_font_name = NULL;
4970 #endif
4973 #endif /* USE_GTK */