emacs-lisp/package.el (package-initialize): Populate `package-selected-packages'.
[emacs.git] / src / gtkutil.c
blob21f3cb15e66eb298ecace15b9d45ed0603a34217
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <float.h>
24 #include <stdio.h>
26 #include <c-ctype.h>
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "buffer.h"
34 #include "gtkutil.h"
35 #include "termhooks.h"
36 #include "keyboard.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include "font.h"
41 #include <gdk/gdkkeysyms.h>
42 #include "xsettings.h"
44 #ifdef HAVE_XFT
45 #include <X11/Xft/Xft.h>
46 #endif
48 #ifdef HAVE_GTK3
49 #include <gtk/gtkx.h>
50 #include "emacsgtkfixed.h"
51 #endif
53 /** #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, Qxg_frame_set_char_size);
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 char *z = msgbuf;
1843 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1844 Show the C-l help text only for versions < 2.10. */
1845 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1846 z = stpcpy (z, "\nType C-l to display a file name text entry box.\n");
1847 strcpy (z, "\nIf you don't like this file selector, use the "
1848 "corresponding\nkey binding or customize "
1849 "use-file-dialog to turn it off.");
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 const char *name = pango_font_description_get_family (desc);
2076 gint size = pango_font_description_get_size (desc);
2077 PangoWeight weight = pango_font_description_get_weight (desc);
2078 PangoStyle style = pango_font_description_get_style (desc);
2080 font = CALLN (Ffont_spec,
2081 QCname, build_string (name),
2082 QCsize, make_float (pango_units_to_double (size)),
2083 QCweight, XG_WEIGHT_TO_SYMBOL (weight),
2084 QCslant, XG_STYLE_TO_SYMBOL (style),
2085 QCtype, Qxft);
2087 pango_font_description_free (desc);
2088 dupstring (&x_last_font_name, name);
2091 #else /* Use old font selector, which just returns the font name. */
2093 char *font_name
2094 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2096 if (font_name)
2098 font = build_string (font_name);
2099 g_free (x_last_font_name);
2100 x_last_font_name = font_name;
2102 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2105 gtk_widget_destroy (w);
2106 return font;
2108 #endif /* HAVE_FREETYPE */
2112 /***********************************************************************
2113 Menu functions.
2114 ***********************************************************************/
2116 /* The name of menu items that can be used for customization. Since GTK
2117 RC files are very crude and primitive, we have to set this on all
2118 menu item names so a user can easily customize menu items. */
2120 #define MENU_ITEM_NAME "emacs-menuitem"
2123 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2124 during GC. The next member points to the items. */
2125 static xg_list_node xg_menu_cb_list;
2127 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2128 during GC. The next member points to the items. */
2129 static xg_list_node xg_menu_item_cb_list;
2131 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2132 F is the frame CL_DATA will be initialized for.
2133 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2135 The menu bar and all sub menus under the menu bar in a frame
2136 share the same structure, hence the reference count.
2138 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2139 allocated xg_menu_cb_data if CL_DATA is NULL. */
2141 static xg_menu_cb_data *
2142 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2144 if (! cl_data)
2146 cl_data = xmalloc (sizeof *cl_data);
2147 cl_data->f = f;
2148 cl_data->menu_bar_vector = f->menu_bar_vector;
2149 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2150 cl_data->highlight_cb = highlight_cb;
2151 cl_data->ref_count = 0;
2153 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2156 cl_data->ref_count++;
2158 return cl_data;
2161 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2162 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2164 When the menu bar is updated, menu items may have been added and/or
2165 removed, so menu_bar_vector and menu_bar_items_used change. We must
2166 then update CL_DATA since it is used to determine which menu
2167 item that is invoked in the menu.
2168 HIGHLIGHT_CB could change, there is no check that the same
2169 function is given when modifying a menu bar as was given when
2170 creating the menu bar. */
2172 static void
2173 update_cl_data (xg_menu_cb_data *cl_data,
2174 struct frame *f,
2175 GCallback highlight_cb)
2177 if (cl_data)
2179 cl_data->f = f;
2180 cl_data->menu_bar_vector = f->menu_bar_vector;
2181 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2182 cl_data->highlight_cb = highlight_cb;
2186 /* Decrease reference count for CL_DATA.
2187 If reference count is zero, free CL_DATA. */
2189 static void
2190 unref_cl_data (xg_menu_cb_data *cl_data)
2192 if (cl_data && cl_data->ref_count > 0)
2194 cl_data->ref_count--;
2195 if (cl_data->ref_count == 0)
2197 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2198 xfree (cl_data);
2203 /* Function that marks all lisp data during GC. */
2205 void
2206 xg_mark_data (void)
2208 xg_list_node *iter;
2209 Lisp_Object rest, frame;
2211 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2212 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2214 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2216 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2218 if (! NILP (cb_data->help))
2219 mark_object (cb_data->help);
2222 FOR_EACH_FRAME (rest, frame)
2224 struct frame *f = XFRAME (frame);
2226 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2228 struct xg_frame_tb_info *tbinfo
2229 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2230 TB_INFO_KEY);
2231 if (tbinfo)
2233 mark_object (tbinfo->last_tool_bar);
2234 mark_object (tbinfo->style);
2241 /* Callback called when a menu item is destroyed. Used to free data.
2242 W is the widget that is being destroyed (not used).
2243 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2245 static void
2246 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2248 if (client_data)
2250 xg_menu_item_cb_data *data = client_data;
2251 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2252 xfree (data);
2256 /* Callback called when the pointer enters/leaves a menu item.
2257 W is the parent of the menu item.
2258 EVENT is either an enter event or leave event.
2259 CLIENT_DATA is not used.
2261 Returns FALSE to tell GTK to keep processing this event. */
2263 static gboolean
2264 menuitem_highlight_callback (GtkWidget *w,
2265 GdkEventCrossing *event,
2266 gpointer client_data)
2268 GdkEvent ev;
2269 GtkWidget *subwidget;
2270 xg_menu_item_cb_data *data;
2272 ev.crossing = *event;
2273 subwidget = gtk_get_event_widget (&ev);
2274 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2275 if (data)
2277 if (! NILP (data->help) && data->cl_data->highlight_cb)
2279 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2280 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2281 (*func) (subwidget, call_data);
2285 return FALSE;
2288 /* Callback called when a menu is destroyed. Used to free data.
2289 W is the widget that is being destroyed (not used).
2290 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2292 static void
2293 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2295 unref_cl_data (client_data);
2298 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2299 must be non-NULL) and can be inserted into a menu item.
2301 Returns the GtkHBox. */
2303 static GtkWidget *
2304 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2306 GtkWidget *wlbl;
2307 GtkWidget *wkey;
2308 GtkWidget *wbox;
2310 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2311 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2312 wlbl = gtk_label_new (utf8_label);
2313 wkey = gtk_label_new (utf8_key);
2315 #if GTK_CHECK_VERSION (3, 14, 0)
2316 gtk_widget_set_halign (wlbl, GTK_ALIGN_START);
2317 gtk_widget_set_valign (wlbl, GTK_ALIGN_CENTER);
2318 gtk_widget_set_halign (wkey, GTK_ALIGN_START);
2319 gtk_widget_set_valign (wkey, GTK_ALIGN_CENTER);
2320 #else
2321 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2322 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2323 #endif
2324 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2325 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2327 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2328 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2329 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2331 return wbox;
2334 /* Make and return a menu item widget with the key to the right.
2335 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2336 UTF8_KEY is the text representing the key binding.
2337 ITEM is the widget_value describing the menu item.
2339 GROUP is an in/out parameter. If the menu item to be created is not
2340 part of any radio menu group, *GROUP contains NULL on entry and exit.
2341 If the menu item to be created is part of a radio menu group, on entry
2342 *GROUP contains the group to use, or NULL if this is the first item
2343 in the group. On exit, *GROUP contains the radio item group.
2345 Unfortunately, keys don't line up as nicely as in Motif,
2346 but the MacOS X version doesn't either, so I guess that is OK. */
2348 static GtkWidget *
2349 make_menu_item (const char *utf8_label,
2350 const char *utf8_key,
2351 widget_value *item,
2352 GSList **group)
2354 GtkWidget *w;
2355 GtkWidget *wtoadd = 0;
2357 /* It has been observed that some menu items have a NULL name field.
2358 This will lead to this function being called with a NULL utf8_label.
2359 GTK crashes on that so we set a blank label. Why there is a NULL
2360 name remains to be investigated. */
2361 if (! utf8_label) utf8_label = " ";
2363 if (utf8_key)
2364 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2366 if (item->button_type == BUTTON_TYPE_TOGGLE)
2368 *group = NULL;
2369 if (utf8_key) w = gtk_check_menu_item_new ();
2370 else w = gtk_check_menu_item_new_with_label (utf8_label);
2371 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2373 else if (item->button_type == BUTTON_TYPE_RADIO)
2375 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2376 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2377 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2378 if (item->selected)
2379 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2381 else
2383 *group = NULL;
2384 if (utf8_key) w = gtk_menu_item_new ();
2385 else w = gtk_menu_item_new_with_label (utf8_label);
2388 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2389 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2391 return w;
2394 /* Create a menu item widget, and connect the callbacks.
2395 ITEM describes the menu item.
2396 F is the frame the created menu belongs to.
2397 SELECT_CB is the callback to use when a menu item is selected.
2398 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2399 CL_DATA points to the callback data to be used for this menu.
2400 GROUP is an in/out parameter. If the menu item to be created is not
2401 part of any radio menu group, *GROUP contains NULL on entry and exit.
2402 If the menu item to be created is part of a radio menu group, on entry
2403 *GROUP contains the group to use, or NULL if this is the first item
2404 in the group. On exit, *GROUP contains the radio item group.
2406 Returns the created GtkWidget. */
2408 static GtkWidget *
2409 xg_create_one_menuitem (widget_value *item,
2410 struct frame *f,
2411 GCallback select_cb,
2412 GCallback highlight_cb,
2413 xg_menu_cb_data *cl_data,
2414 GSList **group)
2416 char *utf8_label;
2417 char *utf8_key;
2418 GtkWidget *w;
2419 xg_menu_item_cb_data *cb_data;
2421 utf8_label = get_utf8_string (item->name);
2422 utf8_key = get_utf8_string (item->key);
2424 w = make_menu_item (utf8_label, utf8_key, item, group);
2426 if (utf8_label) g_free (utf8_label);
2427 if (utf8_key) g_free (utf8_key);
2429 cb_data = xmalloc (sizeof *cb_data);
2431 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2433 cb_data->select_id = 0;
2434 cb_data->help = item->help;
2435 cb_data->cl_data = cl_data;
2436 cb_data->call_data = item->call_data;
2438 g_signal_connect (G_OBJECT (w),
2439 "destroy",
2440 G_CALLBACK (menuitem_destroy_callback),
2441 cb_data);
2443 /* Put cb_data in widget, so we can get at it when modifying menubar */
2444 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2446 /* final item, not a submenu */
2447 if (item->call_data && ! item->contents)
2449 if (select_cb)
2450 cb_data->select_id
2451 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2454 return w;
2457 /* Create a full menu tree specified by DATA.
2458 F is the frame the created menu belongs to.
2459 SELECT_CB is the callback to use when a menu item is selected.
2460 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2461 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2462 If POP_UP_P, create a popup menu.
2463 If MENU_BAR_P, create a menu bar.
2464 TOPMENU is the topmost GtkWidget that others shall be placed under.
2465 It may be NULL, in that case we create the appropriate widget
2466 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2467 CL_DATA is the callback data we shall use for this menu, or NULL
2468 if we haven't set the first callback yet.
2469 NAME is the name to give to the top level menu if this function
2470 creates it. May be NULL to not set any name.
2472 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2473 not NULL.
2475 This function calls itself to create submenus. */
2477 static GtkWidget *
2478 create_menus (widget_value *data,
2479 struct frame *f,
2480 GCallback select_cb,
2481 GCallback deactivate_cb,
2482 GCallback highlight_cb,
2483 bool pop_up_p,
2484 bool menu_bar_p,
2485 GtkWidget *topmenu,
2486 xg_menu_cb_data *cl_data,
2487 const char *name)
2489 widget_value *item;
2490 GtkWidget *wmenu = topmenu;
2491 GSList *group = NULL;
2493 if (! topmenu)
2495 if (! menu_bar_p)
2497 wmenu = gtk_menu_new ();
2498 xg_set_screen (wmenu, f);
2499 /* Connect this to the menu instead of items so we get enter/leave for
2500 disabled items also. TODO: Still does not get enter/leave for
2501 disabled items in detached menus. */
2502 g_signal_connect (G_OBJECT (wmenu),
2503 "enter-notify-event",
2504 G_CALLBACK (menuitem_highlight_callback),
2505 NULL);
2506 g_signal_connect (G_OBJECT (wmenu),
2507 "leave-notify-event",
2508 G_CALLBACK (menuitem_highlight_callback),
2509 NULL);
2511 else
2513 wmenu = gtk_menu_bar_new ();
2514 /* Set width of menu bar to a small value so it doesn't enlarge
2515 a small initial frame size. The width will be set to the
2516 width of the frame later on when it is added to a container.
2517 height -1: Natural height. */
2518 gtk_widget_set_size_request (wmenu, 1, -1);
2521 /* Put cl_data on the top menu for easier access. */
2522 cl_data = make_cl_data (cl_data, f, highlight_cb);
2523 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2524 g_signal_connect (G_OBJECT (wmenu), "destroy",
2525 G_CALLBACK (menu_destroy_callback), cl_data);
2527 if (name)
2528 gtk_widget_set_name (wmenu, name);
2530 if (deactivate_cb)
2531 g_signal_connect (G_OBJECT (wmenu),
2532 "selection-done", deactivate_cb, 0);
2535 for (item = data; item; item = item->next)
2537 GtkWidget *w;
2539 if (pop_up_p && !item->contents && !item->call_data
2540 && !menu_separator_name_p (item->name))
2542 char *utf8_label;
2543 /* A title for a popup. We do the same as GTK does when
2544 creating titles, but it does not look good. */
2545 group = NULL;
2546 utf8_label = get_utf8_string (item->name);
2548 w = gtk_menu_item_new_with_label (utf8_label);
2549 gtk_widget_set_sensitive (w, FALSE);
2550 if (utf8_label) g_free (utf8_label);
2552 else if (menu_separator_name_p (item->name))
2554 group = NULL;
2555 /* GTK only have one separator type. */
2556 w = gtk_separator_menu_item_new ();
2558 else
2560 w = xg_create_one_menuitem (item,
2562 item->contents ? 0 : select_cb,
2563 highlight_cb,
2564 cl_data,
2565 &group);
2567 /* Create a possibly empty submenu for menu bar items, since some
2568 themes don't highlight items correctly without it. */
2569 if (item->contents || menu_bar_p)
2571 GtkWidget *submenu = create_menus (item->contents,
2573 select_cb,
2574 deactivate_cb,
2575 highlight_cb,
2579 cl_data,
2581 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2585 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2586 gtk_widget_set_name (w, MENU_ITEM_NAME);
2589 return wmenu;
2592 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2593 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2594 with some text and buttons.
2595 F is the frame the created item belongs to.
2596 NAME is the name to use for the top widget.
2597 VAL is a widget_value structure describing items to be created.
2598 SELECT_CB is the callback to use when a menu item is selected or
2599 a dialog button is pressed.
2600 DEACTIVATE_CB is the callback to use when an item is deactivated.
2601 For a menu, when a sub menu is not shown anymore, for a dialog it is
2602 called when the dialog is popped down.
2603 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2605 Returns the widget created. */
2607 GtkWidget *
2608 xg_create_widget (const char *type, const char *name, struct frame *f,
2609 widget_value *val, GCallback select_cb,
2610 GCallback deactivate_cb, GCallback highlight_cb)
2612 GtkWidget *w = 0;
2613 bool menu_bar_p = strcmp (type, "menubar") == 0;
2614 bool pop_up_p = strcmp (type, "popup") == 0;
2616 if (strcmp (type, "dialog") == 0)
2618 w = create_dialog (val, select_cb, deactivate_cb);
2619 xg_set_screen (w, f);
2620 gtk_window_set_transient_for (GTK_WINDOW (w),
2621 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2622 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2623 gtk_widget_set_name (w, "emacs-dialog");
2624 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2626 else if (menu_bar_p || pop_up_p)
2628 w = create_menus (val->contents,
2630 select_cb,
2631 deactivate_cb,
2632 highlight_cb,
2633 pop_up_p,
2634 menu_bar_p,
2637 name);
2639 /* Set the cursor to an arrow for popup menus when they are mapped.
2640 This is done by default for menu bar menus. */
2641 if (pop_up_p)
2643 /* Must realize so the GdkWindow inside the widget is created. */
2644 gtk_widget_realize (w);
2645 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2648 else
2650 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2651 type);
2654 return w;
2657 /* Return the label for menu item WITEM. */
2659 static const char *
2660 xg_get_menu_item_label (GtkMenuItem *witem)
2662 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2663 return gtk_label_get_label (wlabel);
2666 /* Return true if the menu item WITEM has the text LABEL. */
2668 static bool
2669 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2671 bool is_same = 0;
2672 char *utf8_label = get_utf8_string (label);
2673 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2675 if (! old_label && ! utf8_label)
2676 is_same = 1;
2677 else if (old_label && utf8_label)
2678 is_same = strcmp (utf8_label, old_label) == 0;
2680 if (utf8_label) g_free (utf8_label);
2682 return is_same;
2685 /* Destroy widgets in LIST. */
2687 static void
2688 xg_destroy_widgets (GList *list)
2690 GList *iter;
2692 for (iter = list; iter; iter = g_list_next (iter))
2694 GtkWidget *w = GTK_WIDGET (iter->data);
2696 /* Destroying the widget will remove it from the container it is in. */
2697 gtk_widget_destroy (w);
2701 /* Update the top level names in MENUBAR (i.e. not submenus).
2702 F is the frame the menu bar belongs to.
2703 *LIST is a list with the current menu bar names (menu item widgets).
2704 ITER is the item within *LIST that shall be updated.
2705 POS is the numerical position, starting at 0, of ITER in *LIST.
2706 VAL describes what the menu bar shall look like after the update.
2707 SELECT_CB is the callback to use when a menu item is selected.
2708 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2709 CL_DATA points to the callback data to be used for this menu bar.
2711 This function calls itself to walk through the menu bar names. */
2713 static void
2714 xg_update_menubar (GtkWidget *menubar,
2715 struct frame *f,
2716 GList **list,
2717 GList *iter,
2718 int pos,
2719 widget_value *val,
2720 GCallback select_cb,
2721 GCallback deactivate_cb,
2722 GCallback highlight_cb,
2723 xg_menu_cb_data *cl_data)
2725 if (! iter && ! val)
2726 return;
2727 else if (iter && ! val)
2729 /* Item(s) have been removed. Remove all remaining items. */
2730 xg_destroy_widgets (iter);
2732 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2733 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2734 gtk_menu_item_new_with_label (""),
2736 /* All updated. */
2737 val = 0;
2738 iter = 0;
2740 else if (! iter && val)
2742 /* Item(s) added. Add all new items in one call. */
2743 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2744 0, 1, menubar, cl_data, 0);
2746 /* All updated. */
2747 val = 0;
2748 iter = 0;
2750 /* Below this neither iter or val is NULL */
2751 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2753 /* This item is still the same, check next item. */
2754 val = val->next;
2755 iter = g_list_next (iter);
2756 ++pos;
2758 else /* This item is changed. */
2760 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2761 GtkMenuItem *witem2 = 0;
2762 bool val_in_menubar = 0;
2763 bool iter_in_new_menubar = 0;
2764 GList *iter2;
2765 widget_value *cur;
2767 /* See if the changed entry (val) is present later in the menu bar */
2768 for (iter2 = iter;
2769 iter2 && ! val_in_menubar;
2770 iter2 = g_list_next (iter2))
2772 witem2 = GTK_MENU_ITEM (iter2->data);
2773 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2776 /* See if the current entry (iter) is present later in the
2777 specification for the new menu bar. */
2778 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2779 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2781 if (val_in_menubar && ! iter_in_new_menubar)
2783 int nr = pos;
2785 /* This corresponds to:
2786 Current: A B C
2787 New: A C
2788 Remove B. */
2790 g_object_ref (G_OBJECT (witem));
2791 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2792 gtk_widget_destroy (GTK_WIDGET (witem));
2794 /* Must get new list since the old changed. */
2795 g_list_free (*list);
2796 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2797 while (nr-- > 0) iter = g_list_next (iter);
2799 else if (! val_in_menubar && ! iter_in_new_menubar)
2801 /* This corresponds to:
2802 Current: A B C
2803 New: A X C
2804 Rename B to X. This might seem to be a strange thing to do,
2805 since if there is a menu under B it will be totally wrong for X.
2806 But consider editing a C file. Then there is a C-mode menu
2807 (corresponds to B above).
2808 If then doing C-x C-f the minibuf menu (X above) replaces the
2809 C-mode menu. When returning from the minibuffer, we get
2810 back the C-mode menu. Thus we do:
2811 Rename B to X (C-mode to minibuf menu)
2812 Rename X to B (minibuf to C-mode menu).
2813 If the X menu hasn't been invoked, the menu under B
2814 is up to date when leaving the minibuffer. */
2815 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2816 char *utf8_label = get_utf8_string (val->name);
2818 /* GTK menu items don't notice when their labels have been
2819 changed from underneath them, so we have to explicitly
2820 use g_object_notify to tell listeners (e.g., a GMenuModel
2821 bridge that might be loaded) that the item's label has
2822 changed. */
2823 gtk_label_set_text (wlabel, utf8_label);
2824 #if GTK_CHECK_VERSION (2, 16, 0)
2825 g_object_notify (G_OBJECT (witem), "label");
2826 #endif
2827 if (utf8_label) g_free (utf8_label);
2828 iter = g_list_next (iter);
2829 val = val->next;
2830 ++pos;
2832 else if (! val_in_menubar && iter_in_new_menubar)
2834 /* This corresponds to:
2835 Current: A B C
2836 New: A X B C
2837 Insert X. */
2839 int nr = pos;
2840 GSList *group = 0;
2841 GtkWidget *w = xg_create_one_menuitem (val,
2843 select_cb,
2844 highlight_cb,
2845 cl_data,
2846 &group);
2848 /* Create a possibly empty submenu for menu bar items, since some
2849 themes don't highlight items correctly without it. */
2850 GtkWidget *submenu = create_menus (NULL, f,
2851 select_cb, deactivate_cb,
2852 highlight_cb,
2853 0, 0, 0, cl_data, 0);
2855 gtk_widget_set_name (w, MENU_ITEM_NAME);
2856 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2857 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2859 g_list_free (*list);
2860 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2861 while (nr-- > 0) iter = g_list_next (iter);
2862 iter = g_list_next (iter);
2863 val = val->next;
2864 ++pos;
2866 else /* if (val_in_menubar && iter_in_new_menubar) */
2868 int nr = pos;
2869 /* This corresponds to:
2870 Current: A B C
2871 New: A C B
2872 Move C before B */
2874 g_object_ref (G_OBJECT (witem2));
2875 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2876 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2877 GTK_WIDGET (witem2), pos);
2878 g_object_unref (G_OBJECT (witem2));
2880 g_list_free (*list);
2881 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2882 while (nr-- > 0) iter = g_list_next (iter);
2883 if (iter) iter = g_list_next (iter);
2884 val = val->next;
2885 ++pos;
2889 /* Update the rest of the menu bar. */
2890 xg_update_menubar (menubar, f, list, iter, pos, val,
2891 select_cb, deactivate_cb, highlight_cb, cl_data);
2894 /* Update the menu item W so it corresponds to VAL.
2895 SELECT_CB is the callback to use when a menu item is selected.
2896 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2897 CL_DATA is the data to set in the widget for menu invocation. */
2899 static void
2900 xg_update_menu_item (widget_value *val,
2901 GtkWidget *w,
2902 GCallback select_cb,
2903 GCallback highlight_cb,
2904 xg_menu_cb_data *cl_data)
2906 GtkWidget *wchild;
2907 GtkLabel *wlbl = 0;
2908 GtkLabel *wkey = 0;
2909 char *utf8_label;
2910 char *utf8_key;
2911 const char *old_label = 0;
2912 const char *old_key = 0;
2913 xg_menu_item_cb_data *cb_data;
2914 bool label_changed = false;
2916 wchild = XG_BIN_CHILD (w);
2917 utf8_label = get_utf8_string (val->name);
2918 utf8_key = get_utf8_string (val->key);
2920 /* See if W is a menu item with a key. See make_menu_item above. */
2921 if (GTK_IS_BOX (wchild))
2923 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2925 wlbl = GTK_LABEL (list->data);
2926 wkey = GTK_LABEL (list->next->data);
2927 g_list_free (list);
2929 if (! utf8_key)
2931 /* Remove the key and keep just the label. */
2932 g_object_ref (G_OBJECT (wlbl));
2933 gtk_container_remove (GTK_CONTAINER (w), wchild);
2934 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2935 g_object_unref (G_OBJECT (wlbl));
2936 wkey = 0;
2940 else /* Just a label. */
2942 wlbl = GTK_LABEL (wchild);
2944 /* Check if there is now a key. */
2945 if (utf8_key)
2947 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2948 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2950 wlbl = GTK_LABEL (list->data);
2951 wkey = GTK_LABEL (list->next->data);
2952 g_list_free (list);
2954 gtk_container_remove (GTK_CONTAINER (w), wchild);
2955 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2959 if (wkey) old_key = gtk_label_get_label (wkey);
2960 if (wlbl) old_label = gtk_label_get_label (wlbl);
2962 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2964 label_changed = true;
2965 gtk_label_set_text (wkey, utf8_key);
2968 if (! old_label || strcmp (utf8_label, old_label) != 0)
2970 label_changed = true;
2971 gtk_label_set_text (wlbl, utf8_label);
2974 if (utf8_key) g_free (utf8_key);
2975 if (utf8_label) g_free (utf8_label);
2977 if (! val->enabled && gtk_widget_get_sensitive (w))
2978 gtk_widget_set_sensitive (w, FALSE);
2979 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2980 gtk_widget_set_sensitive (w, TRUE);
2982 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
2983 if (cb_data)
2985 cb_data->call_data = val->call_data;
2986 cb_data->help = val->help;
2987 cb_data->cl_data = cl_data;
2989 /* We assume the callback functions don't change. */
2990 if (val->call_data && ! val->contents)
2992 /* This item shall have a select callback. */
2993 if (! cb_data->select_id)
2994 cb_data->select_id
2995 = g_signal_connect (G_OBJECT (w), "activate",
2996 select_cb, cb_data);
2998 else if (cb_data->select_id)
3000 g_signal_handler_disconnect (w, cb_data->select_id);
3001 cb_data->select_id = 0;
3005 #if GTK_CHECK_VERSION (2, 16, 0)
3006 if (label_changed) /* See comment in xg_update_menubar. */
3007 g_object_notify (G_OBJECT (w), "label");
3008 #endif
3011 /* Update the toggle menu item W so it corresponds to VAL. */
3013 static void
3014 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3016 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3019 /* Update the radio menu item W so it corresponds to VAL. */
3021 static void
3022 xg_update_radio_item (widget_value *val, GtkWidget *w)
3024 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3027 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3028 SUBMENU may be NULL, in that case a new menu is created.
3029 F is the frame the menu bar belongs to.
3030 VAL describes the contents of the menu bar.
3031 SELECT_CB is the callback to use when a menu item is selected.
3032 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3033 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3034 CL_DATA is the call back data to use for any newly created items.
3036 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3037 was NULL. */
3039 static GtkWidget *
3040 xg_update_submenu (GtkWidget *submenu,
3041 struct frame *f,
3042 widget_value *val,
3043 GCallback select_cb,
3044 GCallback deactivate_cb,
3045 GCallback highlight_cb,
3046 xg_menu_cb_data *cl_data)
3048 GtkWidget *newsub = submenu;
3049 GList *list = 0;
3050 GList *iter;
3051 widget_value *cur;
3052 GList *first_radio = 0;
3054 if (submenu)
3055 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3057 for (cur = val, iter = list;
3058 cur && iter;
3059 iter = g_list_next (iter), cur = cur->next)
3061 GtkWidget *w = GTK_WIDGET (iter->data);
3063 /* Remember first radio button in a group. If we get a mismatch in
3064 a radio group we must rebuild the whole group so that the connections
3065 in GTK becomes correct. */
3066 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3067 first_radio = iter;
3068 else if (cur->button_type != BUTTON_TYPE_RADIO
3069 && ! GTK_IS_RADIO_MENU_ITEM (w))
3070 first_radio = 0;
3072 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3074 if (! menu_separator_name_p (cur->name))
3075 break;
3077 else if (GTK_IS_CHECK_MENU_ITEM (w))
3079 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3080 break;
3081 xg_update_toggle_item (cur, w);
3082 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3084 else if (GTK_IS_RADIO_MENU_ITEM (w))
3086 if (cur->button_type != BUTTON_TYPE_RADIO)
3087 break;
3088 xg_update_radio_item (cur, w);
3089 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3091 else if (GTK_IS_MENU_ITEM (w))
3093 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3094 GtkWidget *sub;
3096 if (cur->button_type != BUTTON_TYPE_NONE ||
3097 menu_separator_name_p (cur->name))
3098 break;
3100 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3102 sub = gtk_menu_item_get_submenu (witem);
3103 if (sub && ! cur->contents)
3105 /* Not a submenu anymore. */
3106 g_object_ref (G_OBJECT (sub));
3107 remove_submenu (witem);
3108 gtk_widget_destroy (sub);
3110 else if (cur->contents)
3112 GtkWidget *nsub;
3114 nsub = xg_update_submenu (sub, f, cur->contents,
3115 select_cb, deactivate_cb,
3116 highlight_cb, cl_data);
3118 /* If this item just became a submenu, we must set it. */
3119 if (nsub != sub)
3120 gtk_menu_item_set_submenu (witem, nsub);
3123 else
3125 /* Structural difference. Remove everything from here and down
3126 in SUBMENU. */
3127 break;
3131 /* Remove widgets from first structural change. */
3132 if (iter)
3134 /* If we are adding new menu items below, we must remove from
3135 first radio button so that radio groups become correct. */
3136 if (cur && first_radio) xg_destroy_widgets (first_radio);
3137 else xg_destroy_widgets (iter);
3140 if (cur)
3142 /* More items added. Create them. */
3143 newsub = create_menus (cur,
3145 select_cb,
3146 deactivate_cb,
3147 highlight_cb,
3150 submenu,
3151 cl_data,
3155 if (list) g_list_free (list);
3157 return newsub;
3160 /* Update the MENUBAR.
3161 F is the frame the menu bar belongs to.
3162 VAL describes the contents of the menu bar.
3163 If DEEP_P, rebuild all but the top level menu names in
3164 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3165 SELECT_CB is the callback to use when a menu item is selected.
3166 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3167 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3169 void
3170 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3171 widget_value *val, bool deep_p,
3172 GCallback select_cb, GCallback deactivate_cb,
3173 GCallback highlight_cb)
3175 xg_menu_cb_data *cl_data;
3176 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3178 if (! list) return;
3180 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3182 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3183 select_cb, deactivate_cb, highlight_cb, cl_data);
3185 if (deep_p)
3187 widget_value *cur;
3189 /* Update all sub menus.
3190 We must keep the submenus (GTK menu item widgets) since the
3191 X Window in the XEvent that activates the menu are those widgets. */
3193 /* Update cl_data, menu_item things in F may have changed. */
3194 update_cl_data (cl_data, f, highlight_cb);
3196 for (cur = val->contents; cur; cur = cur->next)
3198 GList *iter;
3199 GtkWidget *sub = 0;
3200 GtkWidget *newsub;
3201 GtkMenuItem *witem = 0;
3203 /* Find sub menu that corresponds to val and update it. */
3204 for (iter = list ; iter; iter = g_list_next (iter))
3206 witem = GTK_MENU_ITEM (iter->data);
3207 if (xg_item_label_same_p (witem, cur->name))
3209 sub = gtk_menu_item_get_submenu (witem);
3210 break;
3214 newsub = xg_update_submenu (sub,
3216 cur->contents,
3217 select_cb,
3218 deactivate_cb,
3219 highlight_cb,
3220 cl_data);
3221 /* sub may still be NULL. If we just updated non deep and added
3222 a new menu bar item, it has no sub menu yet. So we set the
3223 newly created sub menu under witem. */
3224 if (newsub != sub && witem != 0)
3226 xg_set_screen (newsub, f);
3227 gtk_menu_item_set_submenu (witem, newsub);
3232 g_list_free (list);
3233 gtk_widget_show_all (menubar);
3236 /* Callback called when the menu bar W is mapped.
3237 Used to find the height of the menu bar if we didn't get it
3238 after showing the widget. */
3240 static void
3241 menubar_map_cb (GtkWidget *w, gpointer user_data)
3243 GtkRequisition req;
3244 struct frame *f = user_data;
3245 gtk_widget_get_preferred_size (w, NULL, &req);
3246 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3248 FRAME_MENUBAR_HEIGHT (f) = req.height;
3249 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3253 /* Recompute all the widgets of frame F, when the menu bar has been
3254 changed. */
3256 void
3257 xg_update_frame_menubar (struct frame *f)
3259 struct x_output *x = f->output_data.x;
3260 GtkRequisition req;
3262 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3263 return;
3265 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3266 return; /* Already done this, happens for frames created invisible. */
3268 block_input ();
3270 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3271 FALSE, FALSE, 0);
3272 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3274 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3275 gtk_widget_show_all (x->menubar_widget);
3276 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3278 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3280 FRAME_MENUBAR_HEIGHT (f) = req.height;
3281 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3283 unblock_input ();
3286 /* Get rid of the menu bar of frame F, and free its storage.
3287 This is used when deleting a frame, and when turning off the menu bar. */
3289 void
3290 free_frame_menubar (struct frame *f)
3292 struct x_output *x = f->output_data.x;
3294 if (x->menubar_widget)
3296 block_input ();
3298 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3299 /* The menubar and its children shall be deleted when removed from
3300 the container. */
3301 x->menubar_widget = 0;
3302 FRAME_MENUBAR_HEIGHT (f) = 0;
3303 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
3304 unblock_input ();
3308 bool
3309 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3311 struct x_output *x = f->output_data.x;
3312 GList *iter;
3313 GdkRectangle rec;
3314 GList *list;
3315 GdkDisplay *gdpy;
3316 GdkWindow *gw;
3317 GdkEvent gevent;
3318 GtkWidget *gwdesc;
3320 if (! x->menubar_widget) return 0;
3322 if (! (event->xbutton.x >= 0
3323 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3324 && event->xbutton.y >= 0
3325 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
3326 && event->xbutton.same_screen))
3327 return 0;
3329 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3330 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3331 if (! gw) return 0;
3332 gevent.any.window = gw;
3333 gevent.any.type = GDK_NOTHING;
3334 gwdesc = gtk_get_event_widget (&gevent);
3335 if (! gwdesc) return 0;
3336 if (! GTK_IS_MENU_BAR (gwdesc)
3337 && ! GTK_IS_MENU_ITEM (gwdesc)
3338 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3339 return 0;
3341 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3342 if (! list) return 0;
3343 rec.x = event->xbutton.x;
3344 rec.y = event->xbutton.y;
3345 rec.width = 1;
3346 rec.height = 1;
3348 for (iter = list ; iter; iter = g_list_next (iter))
3350 GtkWidget *w = GTK_WIDGET (iter->data);
3351 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3352 break;
3354 g_list_free (list);
3355 return iter != 0;
3360 /***********************************************************************
3361 Scroll bar functions
3362 ***********************************************************************/
3365 /* Setting scroll bar values invokes the callback. Use this variable
3366 to indicate that callback should do nothing. */
3368 bool xg_ignore_gtk_scrollbar;
3370 /* Width and height of scroll bars for the current theme. */
3371 static int scroll_bar_width_for_theme;
3372 static int scroll_bar_height_for_theme;
3374 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3375 may be larger than 32 bits. Keep a mapping from integer index to widget
3376 pointers to get around the 32 bit limitation. */
3378 static struct
3380 GtkWidget **widgets;
3381 ptrdiff_t max_size;
3382 ptrdiff_t used;
3383 } id_to_widget;
3385 /* Grow this much every time we need to allocate more */
3387 #define ID_TO_WIDGET_INCR 32
3389 /* Store the widget pointer W in id_to_widget and return the integer index. */
3391 static ptrdiff_t
3392 xg_store_widget_in_map (GtkWidget *w)
3394 ptrdiff_t i;
3396 if (id_to_widget.max_size == id_to_widget.used)
3398 ptrdiff_t new_size;
3399 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3400 memory_full (SIZE_MAX);
3402 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3403 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3404 new_size, sizeof (GtkWidget *));
3406 for (i = id_to_widget.max_size; i < new_size; ++i)
3407 id_to_widget.widgets[i] = 0;
3408 id_to_widget.max_size = new_size;
3411 /* Just loop over the array and find a free place. After all,
3412 how many scroll bars are we creating? Should be a small number.
3413 The check above guarantees we will find a free place. */
3414 for (i = 0; i < id_to_widget.max_size; ++i)
3416 if (! id_to_widget.widgets[i])
3418 id_to_widget.widgets[i] = w;
3419 ++id_to_widget.used;
3421 return i;
3425 /* Should never end up here */
3426 emacs_abort ();
3429 /* Remove pointer at IDX from id_to_widget.
3430 Called when scroll bar is destroyed. */
3432 static void
3433 xg_remove_widget_from_map (ptrdiff_t idx)
3435 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3437 id_to_widget.widgets[idx] = 0;
3438 --id_to_widget.used;
3442 /* Get the widget pointer at IDX from id_to_widget. */
3444 static GtkWidget *
3445 xg_get_widget_from_map (ptrdiff_t idx)
3447 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3448 return id_to_widget.widgets[idx];
3450 return 0;
3453 static void
3454 update_theme_scrollbar_width (void)
3456 #ifdef HAVE_GTK3
3457 GtkAdjustment *vadj;
3458 #else
3459 GtkObject *vadj;
3460 #endif
3461 GtkWidget *wscroll;
3462 int w = 0, b = 0;
3464 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3465 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3466 g_object_ref_sink (G_OBJECT (wscroll));
3467 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3468 gtk_widget_destroy (wscroll);
3469 g_object_unref (G_OBJECT (wscroll));
3470 w += 2*b;
3471 if (w < 16) w = 16;
3472 scroll_bar_width_for_theme = w;
3475 static void
3476 update_theme_scrollbar_height (void)
3478 #ifdef HAVE_GTK3
3479 GtkAdjustment *hadj;
3480 #else
3481 GtkObject *hadj;
3482 #endif
3483 GtkWidget *wscroll;
3484 int w = 0, b = 0;
3486 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX, 0.1, 0.1, 0.1);
3487 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3488 g_object_ref_sink (G_OBJECT (wscroll));
3489 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3490 gtk_widget_destroy (wscroll);
3491 g_object_unref (G_OBJECT (wscroll));
3492 w += 2*b;
3493 if (w < 12) w = 12;
3494 scroll_bar_height_for_theme = w;
3498 xg_get_default_scrollbar_width (void)
3500 return scroll_bar_width_for_theme;
3504 xg_get_default_scrollbar_height (void)
3506 /* Apparently there's no default height for themes. */
3507 return scroll_bar_width_for_theme;
3510 /* Return the scrollbar id for X Window WID on display DPY.
3511 Return -1 if WID not in id_to_widget. */
3513 ptrdiff_t
3514 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3516 ptrdiff_t idx;
3517 GtkWidget *w;
3519 w = xg_win_to_widget (dpy, wid);
3521 if (w)
3523 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3524 if (id_to_widget.widgets[idx] == w)
3525 return idx;
3528 return -1;
3531 /* Callback invoked when scroll bar WIDGET is destroyed.
3532 DATA is the index into id_to_widget for WIDGET.
3533 We free pointer to last scroll bar values here and remove the index. */
3535 static void
3536 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3538 intptr_t id = (intptr_t) data;
3539 xg_remove_widget_from_map (id);
3542 /* Create a scroll bar widget for frame F. Store the scroll bar
3543 in BAR.
3544 SCROLL_CALLBACK is the callback to invoke when the value of the
3545 bar changes.
3546 END_CALLBACK is the callback to invoke when scrolling ends.
3547 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3548 to set resources for the widget. */
3550 void
3551 xg_create_scroll_bar (struct frame *f,
3552 struct scroll_bar *bar,
3553 GCallback scroll_callback,
3554 GCallback end_callback,
3555 const char *scroll_bar_name)
3557 GtkWidget *wscroll;
3558 GtkWidget *webox;
3559 intptr_t scroll_id;
3560 #ifdef HAVE_GTK3
3561 GtkAdjustment *vadj;
3562 #else
3563 GtkObject *vadj;
3564 #endif
3566 /* Page, step increment values are not so important here, they
3567 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3568 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3569 0.1, 0.1, 0.1);
3571 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3572 webox = gtk_event_box_new ();
3573 gtk_widget_set_name (wscroll, scroll_bar_name);
3574 #ifndef HAVE_GTK3
3575 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3576 #endif
3577 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3579 scroll_id = xg_store_widget_in_map (wscroll);
3581 g_signal_connect (G_OBJECT (wscroll),
3582 "destroy",
3583 G_CALLBACK (xg_gtk_scroll_destroy),
3584 (gpointer) scroll_id);
3585 g_signal_connect (G_OBJECT (wscroll),
3586 "change-value",
3587 scroll_callback,
3588 (gpointer) bar);
3589 g_signal_connect (G_OBJECT (wscroll),
3590 "button-release-event",
3591 end_callback,
3592 (gpointer) bar);
3594 /* The scroll bar widget does not draw on a window of its own. Instead
3595 it draws on the parent window, in this case the edit widget. So
3596 whenever the edit widget is cleared, the scroll bar needs to redraw
3597 also, which causes flicker. Put an event box between the edit widget
3598 and the scroll bar, so the scroll bar instead draws itself on the
3599 event box window. */
3600 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3601 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3604 /* Set the cursor to an arrow. */
3605 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3607 bar->x_window = scroll_id;
3608 bar->horizontal = 0;
3611 /* Create a horizontal scroll bar widget for frame F. Store the scroll
3612 bar in BAR. SCROLL_CALLBACK is the callback to invoke when the value
3613 of the bar changes. END_CALLBACK is the callback to invoke when
3614 scrolling ends. SCROLL_BAR_NAME is the name we use for the scroll
3615 bar. Can be used to set resources for the widget. */
3617 void
3618 xg_create_horizontal_scroll_bar (struct frame *f,
3619 struct scroll_bar *bar,
3620 GCallback scroll_callback,
3621 GCallback end_callback,
3622 const char *scroll_bar_name)
3624 GtkWidget *wscroll;
3625 GtkWidget *webox;
3626 intptr_t scroll_id;
3627 #ifdef HAVE_GTK3
3628 GtkAdjustment *hadj;
3629 #else
3630 GtkObject *hadj;
3631 #endif
3633 /* Page, step increment values are not so important here, they
3634 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3635 hadj = gtk_adjustment_new (YG_SB_MIN, YG_SB_MIN, YG_SB_MAX,
3636 0.1, 0.1, 0.1);
3638 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (hadj));
3639 webox = gtk_event_box_new ();
3640 gtk_widget_set_name (wscroll, scroll_bar_name);
3641 #ifndef HAVE_GTK3
3642 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3643 #endif
3644 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3646 scroll_id = xg_store_widget_in_map (wscroll);
3648 g_signal_connect (G_OBJECT (wscroll),
3649 "destroy",
3650 G_CALLBACK (xg_gtk_scroll_destroy),
3651 (gpointer) scroll_id);
3652 g_signal_connect (G_OBJECT (wscroll),
3653 "change-value",
3654 scroll_callback,
3655 (gpointer) bar);
3656 g_signal_connect (G_OBJECT (wscroll),
3657 "button-release-event",
3658 end_callback,
3659 (gpointer) bar);
3661 /* The scroll bar widget does not draw on a window of its own. Instead
3662 it draws on the parent window, in this case the edit widget. So
3663 whenever the edit widget is cleared, the scroll bar needs to redraw
3664 also, which causes flicker. Put an event box between the edit widget
3665 and the scroll bar, so the scroll bar instead draws itself on the
3666 event box window. */
3667 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3668 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3671 /* Set the cursor to an arrow. */
3672 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3674 bar->x_window = scroll_id;
3675 bar->horizontal = 1;
3678 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3680 void
3681 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3683 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3684 if (w)
3686 GtkWidget *wparent = gtk_widget_get_parent (w);
3687 gtk_widget_destroy (w);
3688 gtk_widget_destroy (wparent);
3689 SET_FRAME_GARBAGED (f);
3693 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3694 in frame F.
3695 TOP/LEFT are the new pixel positions where the bar shall appear.
3696 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3698 void
3699 xg_update_scrollbar_pos (struct frame *f,
3700 ptrdiff_t scrollbar_id,
3701 int top,
3702 int left,
3703 int width,
3704 int height)
3707 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3709 if (wscroll)
3711 GtkWidget *wfixed = f->output_data.x->edit_widget;
3712 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3713 gint msl;
3715 /* Clear out old position. */
3716 int oldx = -1, oldy = -1, oldw, oldh;
3717 if (gtk_widget_get_parent (wparent) == wfixed)
3719 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3720 "x", &oldx, "y", &oldy, NULL);
3721 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3724 /* Move and resize to new values. */
3725 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3726 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3727 if (msl > height)
3729 /* No room. Hide scroll bar as some themes output a warning if
3730 the height is less than the min size. */
3731 gtk_widget_hide (wparent);
3732 gtk_widget_hide (wscroll);
3734 else
3736 gtk_widget_show_all (wparent);
3737 gtk_widget_set_size_request (wscroll, width, height);
3739 gtk_widget_queue_draw (wfixed);
3740 gdk_window_process_all_updates ();
3741 if (oldx != -1 && oldw > 0 && oldh > 0)
3742 /* Clear under old scroll bar position. This must be done after
3743 the gtk_widget_queue_draw and gdk_window_process_all_updates
3744 above. */
3745 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3746 oldx, oldy, oldw, oldh);
3748 /* GTK does not redraw until the main loop is entered again, but
3749 if there are no X events pending we will not enter it. So we sync
3750 here to get some events. */
3752 x_sync (f);
3753 SET_FRAME_GARBAGED (f);
3754 cancel_mouse_face (f);
3759 /* Update the position of the horizontal scroll bar represented by SCROLLBAR_ID
3760 in frame F.
3761 TOP/LEFT are the new pixel positions where the bar shall appear.
3762 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3764 void
3765 xg_update_horizontal_scrollbar_pos (struct frame *f,
3766 ptrdiff_t scrollbar_id,
3767 int top,
3768 int left,
3769 int width,
3770 int height)
3773 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3775 if (wscroll)
3777 GtkWidget *wfixed = f->output_data.x->edit_widget;
3778 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3779 gint msl;
3781 /* Clear out old position. */
3782 int oldx = -1, oldy = -1, oldw, oldh;
3783 if (gtk_widget_get_parent (wparent) == wfixed)
3785 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3786 "x", &oldx, "y", &oldy, NULL);
3787 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3790 /* Move and resize to new values. */
3791 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3792 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3793 if (msl > width)
3795 /* No room. Hide scroll bar as some themes output a warning if
3796 the width is less than the min size. */
3797 gtk_widget_hide (wparent);
3798 gtk_widget_hide (wscroll);
3800 else
3802 gtk_widget_show_all (wparent);
3803 gtk_widget_set_size_request (wscroll, width, height);
3805 gtk_widget_queue_draw (wfixed);
3806 gdk_window_process_all_updates ();
3807 if (oldx != -1 && oldw > 0 && oldh > 0)
3808 /* Clear under old scroll bar position. This must be done after
3809 the gtk_widget_queue_draw and gdk_window_process_all_updates
3810 above. */
3811 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3812 oldx, oldy, oldw, oldh);
3814 /* GTK does not redraw until the main loop is entered again, but
3815 if there are no X events pending we will not enter it. So we sync
3816 here to get some events. */
3818 x_sync (f);
3819 SET_FRAME_GARBAGED (f);
3820 cancel_mouse_face (f);
3825 /* Get the current value of the range, truncated to an integer. */
3827 static int
3828 int_gtk_range_get_value (GtkRange *range)
3830 return gtk_range_get_value (range);
3834 /* Set the thumb size and position of scroll bar BAR. We are currently
3835 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3837 void
3838 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3839 int portion,
3840 int position,
3841 int whole)
3843 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3845 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3847 if (wscroll && bar->dragging == -1)
3849 GtkAdjustment *adj;
3850 gdouble shown;
3851 gdouble top;
3852 int size, value;
3853 int old_size;
3854 int new_step;
3855 bool changed = 0;
3857 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3859 if (scroll_bar_adjust_thumb_portion_p)
3861 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3862 line rather than the real portion value. This makes the
3863 thumb less likely to resize and that looks better. */
3864 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3866 /* When the thumb is at the bottom, position == whole.
3867 So we need to increase `whole' to make space for the thumb. */
3868 whole += portion;
3871 if (whole <= 0)
3872 top = 0, shown = 1;
3873 else
3875 top = (gdouble) position / whole;
3876 shown = (gdouble) portion / whole;
3879 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3880 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3882 /* Assume all lines are of equal size. */
3883 new_step = size / max (1, FRAME_LINES (f));
3885 old_size = gtk_adjustment_get_page_size (adj);
3886 if (old_size != size)
3888 int old_step = gtk_adjustment_get_step_increment (adj);
3889 if (old_step != new_step)
3891 gtk_adjustment_set_page_size (adj, size);
3892 gtk_adjustment_set_step_increment (adj, new_step);
3893 /* Assume a page increment is about 95% of the page size */
3894 gtk_adjustment_set_page_increment (adj, size - size / 20);
3895 changed = 1;
3899 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3901 block_input ();
3903 /* gtk_range_set_value invokes the callback. Set
3904 ignore_gtk_scrollbar to make the callback do nothing */
3905 xg_ignore_gtk_scrollbar = 1;
3907 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3908 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3909 else if (changed)
3910 gtk_adjustment_changed (adj);
3912 xg_ignore_gtk_scrollbar = 0;
3914 unblock_input ();
3919 /* Set the thumb size and position of horizontal scroll bar BAR. We are
3920 currently displaying PORTION out of a whole WHOLE, and our position
3921 POSITION. */
3922 void
3923 xg_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3924 int portion,
3925 int position,
3926 int whole)
3928 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3930 if (wscroll && bar->dragging == -1)
3932 GtkAdjustment *adj;
3933 int lower = 0;
3934 int upper = max (whole - 1, 0);
3935 int pagesize = min (upper, max (portion, 0));
3936 int value = max (0, min (position, upper - pagesize));
3937 /* These should be set to something more <portion, whole>
3938 related. */
3939 int page_increment = 4;
3940 int step_increment = 1;
3942 block_input ();
3943 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3944 gtk_adjustment_configure (adj, (gdouble) value, (gdouble) lower,
3945 (gdouble) upper, (gdouble) step_increment,
3946 (gdouble) page_increment, (gdouble) pagesize);
3947 gtk_adjustment_changed (adj);
3948 unblock_input ();
3952 /* Return true if EVENT is for a scroll bar in frame F.
3953 When the same X window is used for several Gtk+ widgets, we cannot
3954 say for sure based on the X window alone if an event is for the
3955 frame. This function does additional checks. */
3957 bool
3958 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
3960 bool retval = 0;
3962 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3964 /* Check if press occurred outside the edit widget. */
3965 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3966 GdkWindow *gwin;
3967 #ifdef HAVE_GTK3
3968 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3969 (gdk_display_get_device_manager (gdpy));
3970 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
3971 #else
3972 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3973 #endif
3974 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
3976 else if (f
3977 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3978 || event->type == MotionNotify))
3980 /* If we are releasing or moving the scroll bar, it has the grab. */
3981 GtkWidget *w = gtk_grab_get_current ();
3982 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3985 return retval;
3990 /***********************************************************************
3991 Tool bar functions
3992 ***********************************************************************/
3993 /* The key for the data we put in the GtkImage widgets. The data is
3994 the image used by Emacs. We use this to see if we need to update
3995 the GtkImage with a new image. */
3996 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3998 /* The key for storing the latest modifiers so the activate callback can
3999 get them. */
4000 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
4002 /* The key for the data we put in the GtkImage widgets. The data is
4003 the stock name used by Emacs. We use this to see if we need to update
4004 the GtkImage with a new image. */
4005 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
4007 /* As above, but this is used for named theme widgets, as opposed to
4008 stock items. */
4009 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
4011 /* Callback function invoked when a tool bar item is pressed.
4012 W is the button widget in the tool bar that got pressed,
4013 CLIENT_DATA is an integer that is the index of the button in the
4014 tool bar. 0 is the first button. */
4016 static gboolean
4017 xg_tool_bar_button_cb (GtkWidget *widget,
4018 GdkEventButton *event,
4019 gpointer user_data)
4021 intptr_t state = event->state;
4022 gpointer ptr = (gpointer) state;
4023 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
4024 return FALSE;
4028 /* Callback function invoked when a tool bar item is pressed.
4029 W is the button widget in the tool bar that got pressed,
4030 CLIENT_DATA is an integer that is the index of the button in the
4031 tool bar. 0 is the first button. */
4033 static void
4034 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
4036 intptr_t idx = (intptr_t) client_data;
4037 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
4038 intptr_t mod = (intptr_t) gmod;
4040 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4041 Lisp_Object key, frame;
4042 struct input_event event;
4043 EVENT_INIT (event);
4045 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4046 return;
4048 idx *= TOOL_BAR_ITEM_NSLOTS;
4050 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
4051 XSETFRAME (frame, f);
4053 /* We generate two events here. The first one is to set the prefix
4054 to `(tool_bar)', see keyboard.c. */
4055 event.kind = TOOL_BAR_EVENT;
4056 event.frame_or_window = frame;
4057 event.arg = frame;
4058 kbd_buffer_store_event (&event);
4060 event.kind = TOOL_BAR_EVENT;
4061 event.frame_or_window = frame;
4062 event.arg = key;
4063 /* Convert between the modifier bits GDK uses and the modifier bits
4064 Emacs uses. This assumes GDK and X masks are the same, which they are when
4065 this is written. */
4066 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
4067 kbd_buffer_store_event (&event);
4069 /* Return focus to the frame after we have clicked on a detached
4070 tool bar button. */
4071 x_focus_frame (f);
4074 static GtkWidget *
4075 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
4077 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
4078 GtkWidget *c1 = clist->data;
4079 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
4081 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
4082 g_list_free (clist);
4083 return GTK_IS_LABEL (c1) ? c1 : c2;
4087 /* This callback is called when the mouse enters or leaves a tool bar item.
4088 It is used for displaying and hiding the help text.
4089 W is the tool bar item, a button.
4090 EVENT is either an enter event or leave event.
4091 CLIENT_DATA is an integer that is the index of the button in the
4092 tool bar. 0 is the first button.
4094 Returns FALSE to tell GTK to keep processing this event. */
4096 static gboolean
4097 xg_tool_bar_help_callback (GtkWidget *w,
4098 GdkEventCrossing *event,
4099 gpointer client_data)
4101 intptr_t idx = (intptr_t) client_data;
4102 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4103 Lisp_Object help, frame;
4105 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4106 return FALSE;
4108 if (event->type == GDK_ENTER_NOTIFY)
4110 idx *= TOOL_BAR_ITEM_NSLOTS;
4111 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4113 if (NILP (help))
4114 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4116 else
4117 help = Qnil;
4119 XSETFRAME (frame, f);
4120 kbd_buffer_store_help_event (frame, help);
4122 return FALSE;
4126 /* This callback is called when a tool bar item shall be redrawn.
4127 It modifies the expose event so that the GtkImage widget redraws the
4128 whole image. This to overcome a bug that makes GtkImage draw the image
4129 in the wrong place when it tries to redraw just a part of the image.
4130 W is the GtkImage to be redrawn.
4131 EVENT is the expose event for W.
4132 CLIENT_DATA is unused.
4134 Returns FALSE to tell GTK to keep processing this event. */
4136 #ifndef HAVE_GTK3
4137 static gboolean
4138 xg_tool_bar_item_expose_callback (GtkWidget *w,
4139 GdkEventExpose *event,
4140 gpointer client_data)
4142 gint width, height;
4144 gdk_drawable_get_size (event->window, &width, &height);
4145 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4146 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4148 event->area.x = max (0, event->area.x);
4149 event->area.y = max (0, event->area.y);
4151 event->area.width = max (width, event->area.width);
4152 event->area.height = max (height, event->area.height);
4154 return FALSE;
4156 #endif
4158 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4159 #define toolbar_set_orientation(w, o) \
4160 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4161 #else
4162 #define toolbar_set_orientation(w, o) \
4163 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4164 #endif
4166 /* Attach a tool bar to frame F. */
4168 static void
4169 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4171 struct x_output *x = f->output_data.x;
4172 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4173 GtkWidget *top_widget = x->toolbar_widget;
4175 toolbar_set_orientation (x->toolbar_widget,
4176 into_hbox
4177 ? GTK_ORIENTATION_VERTICAL
4178 : GTK_ORIENTATION_HORIZONTAL);
4180 if (into_hbox)
4182 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4183 FALSE, FALSE, 0);
4185 if (EQ (pos, Qleft))
4186 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4187 top_widget,
4189 x->toolbar_in_hbox = true;
4191 else
4193 bool vbox_pos = x->menubar_widget != 0;
4194 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4195 FALSE, FALSE, 0);
4197 if (EQ (pos, Qtop))
4198 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4199 top_widget,
4200 vbox_pos);
4201 x->toolbar_in_hbox = false;
4203 x->toolbar_is_packed = true;
4206 static bool xg_update_tool_bar_sizes (struct frame *f);
4208 static void
4209 tb_size_cb (GtkWidget *widget,
4210 GdkRectangle *allocation,
4211 gpointer user_data)
4213 /* When tool bar is created it has one preferred size. But when size is
4214 allocated between widgets, it may get another. So we must update
4215 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4216 struct frame *f = user_data;
4217 if (xg_update_tool_bar_sizes (f))
4218 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4221 /* Create a tool bar for frame F. */
4223 static void
4224 xg_create_tool_bar (struct frame *f)
4226 struct x_output *x = f->output_data.x;
4227 #if GTK_CHECK_VERSION (3, 3, 6)
4228 GtkStyleContext *gsty;
4229 #endif
4230 struct xg_frame_tb_info *tbinfo
4231 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4232 TB_INFO_KEY);
4233 if (! tbinfo)
4235 tbinfo = xmalloc (sizeof (*tbinfo));
4236 tbinfo->last_tool_bar = Qnil;
4237 tbinfo->style = Qnil;
4238 tbinfo->hmargin = tbinfo->vmargin = 0;
4239 tbinfo->dir = GTK_TEXT_DIR_NONE;
4240 tbinfo->n_last_items = 0;
4241 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4242 TB_INFO_KEY,
4243 tbinfo);
4246 x->toolbar_widget = gtk_toolbar_new ();
4248 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4250 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4251 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4252 g_signal_connect (x->toolbar_widget, "size-allocate",
4253 G_CALLBACK (tb_size_cb), f);
4254 #if GTK_CHECK_VERSION (3, 3, 6)
4255 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4256 gtk_style_context_add_class (gsty, "primary-toolbar");
4257 #endif
4261 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4263 /* Find the right-to-left image named by RTL in the tool bar images for F.
4264 Returns IMAGE if RTL is not found. */
4266 static Lisp_Object
4267 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4269 int i;
4270 Lisp_Object file, rtl_name;
4271 struct gcpro gcpro1, gcpro2;
4272 GCPRO2 (file, rtl_name);
4274 rtl_name = Ffile_name_nondirectory (rtl);
4276 for (i = 0; i < f->n_tool_bar_items; ++i)
4278 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4279 if (!NILP (file = file_for_image (rtl_image)))
4281 file = call1 (intern ("file-name-sans-extension"),
4282 Ffile_name_nondirectory (file));
4283 if (! NILP (Fequal (file, rtl_name)))
4285 image = rtl_image;
4286 break;
4291 return image;
4294 static GtkToolItem *
4295 xg_make_tool_item (struct frame *f,
4296 GtkWidget *wimage,
4297 GtkWidget **wbutton,
4298 const char *label,
4299 int i, bool horiz, bool text_image)
4301 GtkToolItem *ti = gtk_tool_item_new ();
4302 GtkWidget *vb = gtk_box_new (horiz
4303 ? GTK_ORIENTATION_HORIZONTAL
4304 : GTK_ORIENTATION_VERTICAL,
4306 GtkWidget *wb = gtk_button_new ();
4307 /* The eventbox is here so we can have tooltips on disabled items. */
4308 GtkWidget *weventbox = gtk_event_box_new ();
4309 #if GTK_CHECK_VERSION (3, 3, 6)
4310 GtkCssProvider *css_prov = gtk_css_provider_new ();
4311 GtkStyleContext *gsty;
4313 gtk_css_provider_load_from_data (css_prov,
4314 "GtkEventBox {"
4315 " background-color: transparent;"
4316 "}",
4317 -1, NULL);
4319 gsty = gtk_widget_get_style_context (weventbox);
4320 gtk_style_context_add_provider (gsty,
4321 GTK_STYLE_PROVIDER (css_prov),
4322 GTK_STYLE_PROVIDER_PRIORITY_USER);
4323 g_object_unref (css_prov);
4324 #endif
4326 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4328 if (wimage && !text_image)
4329 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4330 if (label)
4331 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4332 if (wimage && text_image)
4333 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4335 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4336 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4337 gtk_container_add (GTK_CONTAINER (wb), vb);
4338 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4339 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4341 if (wimage || label)
4343 intptr_t ii = i;
4344 gpointer gi = (gpointer) ii;
4346 g_signal_connect (G_OBJECT (wb), "clicked",
4347 G_CALLBACK (xg_tool_bar_callback),
4348 gi);
4350 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4352 #ifndef HAVE_GTK3
4353 /* Catch expose events to overcome an annoying redraw bug, see
4354 comment for xg_tool_bar_item_expose_callback. */
4355 g_signal_connect (G_OBJECT (ti),
4356 "expose-event",
4357 G_CALLBACK (xg_tool_bar_item_expose_callback),
4359 #endif
4360 gtk_tool_item_set_homogeneous (ti, FALSE);
4362 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4363 no distinction based on modifiers in the activate callback,
4364 so we have to do it ourselves. */
4365 g_signal_connect (wb, "button-release-event",
4366 G_CALLBACK (xg_tool_bar_button_cb),
4367 NULL);
4369 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4371 /* Use enter/leave notify to show help. We use the events
4372 rather than the GtkButton specific signals "enter" and
4373 "leave", so we can have only one callback. The event
4374 will tell us what kind of event it is. */
4375 g_signal_connect (G_OBJECT (weventbox),
4376 "enter-notify-event",
4377 G_CALLBACK (xg_tool_bar_help_callback),
4378 gi);
4379 g_signal_connect (G_OBJECT (weventbox),
4380 "leave-notify-event",
4381 G_CALLBACK (xg_tool_bar_help_callback),
4382 gi);
4385 if (wbutton) *wbutton = wb;
4387 return ti;
4390 static bool
4391 is_box_type (GtkWidget *vb, bool is_horizontal)
4393 #ifdef HAVE_GTK3
4394 bool ret = 0;
4395 if (GTK_IS_BOX (vb))
4397 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4398 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4399 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4401 return ret;
4402 #else
4403 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4404 #endif
4408 static bool
4409 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4410 const char *icon_name, const struct image *img,
4411 const char *label, bool horiz)
4413 gpointer old;
4414 GtkWidget *wimage;
4415 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4416 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4418 /* Check if the tool icon matches. */
4419 if (stock_name && wimage)
4421 old = g_object_get_data (G_OBJECT (wimage),
4422 XG_TOOL_BAR_STOCK_NAME);
4423 if (!old || strcmp (old, stock_name))
4424 return 1;
4426 else if (icon_name && wimage)
4428 old = g_object_get_data (G_OBJECT (wimage),
4429 XG_TOOL_BAR_ICON_NAME);
4430 if (!old || strcmp (old, icon_name))
4431 return 1;
4433 else if (wimage)
4435 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4436 XG_TOOL_BAR_IMAGE_DATA);
4437 Pixmap old_img = (Pixmap) gold_img;
4438 if (old_img != img->pixmap)
4439 return 1;
4442 /* Check button configuration and label. */
4443 if (is_box_type (vb, horiz)
4444 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4445 return 1;
4447 /* Ensure label is correct. */
4448 if (label && wlbl)
4449 gtk_label_set_text (GTK_LABEL (wlbl), label);
4450 return 0;
4453 static bool
4454 xg_update_tool_bar_sizes (struct frame *f)
4456 struct x_output *x = f->output_data.x;
4457 GtkRequisition req;
4458 int nl = 0, nr = 0, nt = 0, nb = 0;
4459 GtkWidget *top_widget = x->toolbar_widget;
4461 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4462 if (x->toolbar_in_hbox)
4464 int pos;
4465 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4466 top_widget,
4467 "position", &pos, NULL);
4468 if (pos == 0) nl = req.width;
4469 else nr = req.width;
4471 else
4473 int pos;
4474 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4475 top_widget,
4476 "position", &pos, NULL);
4477 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4478 else nb = req.height;
4481 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4482 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4483 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4484 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4486 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4487 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4488 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4489 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4490 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4491 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4492 return 1;
4495 return 0;
4498 static char *
4499 find_icon_from_name (char *name,
4500 GtkIconTheme *icon_theme,
4501 char **icon_name)
4503 #if ! GTK_CHECK_VERSION (3, 10, 0)
4504 GtkStockItem stock_item;
4505 #endif
4507 if (name[0] == 'n' && name[1] == ':')
4509 *icon_name = name + 2;
4510 name = NULL;
4512 if (! gtk_icon_theme_has_icon (icon_theme, *icon_name))
4513 *icon_name = NULL;
4516 #if ! GTK_CHECK_VERSION (3, 10, 0)
4517 else if (gtk_stock_lookup (name, &stock_item))
4518 *icon_name = NULL;
4519 #endif
4520 else if (gtk_icon_theme_has_icon (icon_theme, name))
4522 *icon_name = name;
4523 name = NULL;
4525 else
4527 name = NULL;
4528 *icon_name = NULL;
4531 return name;
4535 /* Update the tool bar for frame F. Add new buttons and remove old. */
4537 void
4538 update_frame_tool_bar (struct frame *f)
4540 int i, j;
4541 struct x_output *x = f->output_data.x;
4542 int hmargin = 0, vmargin = 0;
4543 GtkToolbar *wtoolbar;
4544 GtkToolItem *ti;
4545 GtkTextDirection dir;
4546 Lisp_Object style;
4547 bool text_image, horiz;
4548 struct xg_frame_tb_info *tbinfo;
4549 GdkScreen *screen;
4550 GtkIconTheme *icon_theme;
4553 if (! FRAME_GTK_WIDGET (f))
4554 return;
4556 block_input ();
4558 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4560 hmargin = XFASTINT (Vtool_bar_button_margin);
4561 vmargin = XFASTINT (Vtool_bar_button_margin);
4563 else if (CONSP (Vtool_bar_button_margin))
4565 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4566 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4568 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4569 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4572 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4573 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4574 i.e. zero. This means that margins less than
4575 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4576 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4577 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4579 if (! x->toolbar_widget)
4580 xg_create_tool_bar (f);
4582 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4583 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4585 style = Ftool_bar_get_system_style ();
4586 screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4587 icon_theme = gtk_icon_theme_get_for_screen (screen);
4589 /* Are we up to date? */
4590 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4591 TB_INFO_KEY);
4593 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4594 && tbinfo->n_last_items == f->n_tool_bar_items
4595 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4596 && tbinfo->dir == dir
4597 && ! NILP (Fequal (tbinfo->style, style))
4598 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4600 unblock_input ();
4601 return;
4604 tbinfo->last_tool_bar = f->tool_bar_items;
4605 tbinfo->n_last_items = f->n_tool_bar_items;
4606 tbinfo->style = style;
4607 tbinfo->hmargin = hmargin;
4608 tbinfo->vmargin = vmargin;
4609 tbinfo->dir = dir;
4611 text_image = EQ (style, Qtext_image_horiz);
4612 horiz = EQ (style, Qboth_horiz) || text_image;
4614 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4616 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4617 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4618 int idx;
4619 ptrdiff_t img_id;
4620 int icon_size = 0;
4621 struct image *img = NULL;
4622 Lisp_Object image;
4623 Lisp_Object stock = Qnil;
4624 char *stock_name = NULL;
4625 char *icon_name = NULL;
4626 Lisp_Object rtl;
4627 GtkWidget *wbutton = NULL;
4628 Lisp_Object specified_file;
4629 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4630 const char *label
4631 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4632 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4633 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4634 : "";
4636 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4638 /* If this is a separator, use a gtk separator item. */
4639 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4641 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4643 if (ti)
4644 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4645 GTK_WIDGET (ti));
4646 ti = gtk_separator_tool_item_new ();
4647 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4649 j++;
4650 continue;
4653 /* Otherwise, the tool-bar item is an ordinary button. */
4655 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4657 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4658 ti = NULL;
4661 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4663 /* Ignore invalid image specifications. */
4664 image = PROP (TOOL_BAR_ITEM_IMAGES);
4665 if (!valid_image_p (image))
4667 if (ti)
4668 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4669 GTK_WIDGET (ti));
4670 continue;
4673 specified_file = file_for_image (image);
4674 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4675 stock = call1 (Qx_gtk_map_stock, specified_file);
4677 if (CONSP (stock))
4679 Lisp_Object tem;
4680 for (tem = stock; CONSP (tem); tem = XCDR (tem))
4681 if (! NILP (tem) && STRINGP (XCAR (tem)))
4683 stock_name = find_icon_from_name (SSDATA (XCAR (tem)),
4684 icon_theme,
4685 &icon_name);
4686 if (stock_name || icon_name) break;
4689 else if (STRINGP (stock))
4691 stock_name = find_icon_from_name (SSDATA (stock),
4692 icon_theme,
4693 &icon_name);
4696 if (stock_name || icon_name)
4697 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4699 if (stock_name == NULL && icon_name == NULL)
4701 /* No stock image, or stock item not known. Try regular
4702 image. If image is a vector, choose it according to the
4703 button state. */
4704 if (dir == GTK_TEXT_DIR_RTL
4705 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4706 && STRINGP (rtl))
4707 image = find_rtl_image (f, image, rtl);
4709 if (VECTORP (image))
4711 if (enabled_p)
4712 idx = (selected_p
4713 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4714 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4715 else
4716 idx = (selected_p
4717 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4718 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4720 eassert (ASIZE (image) >= idx);
4721 image = AREF (image, idx);
4723 else
4724 idx = -1;
4726 img_id = lookup_image (f, image);
4727 img = IMAGE_FROM_ID (f, img_id);
4728 prepare_image_for_display (f, img);
4730 if (img->load_failed_p || img->pixmap == None)
4732 if (ti)
4733 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4734 GTK_WIDGET (ti));
4735 continue;
4739 /* If there is an existing widget, check if it's stale; if so,
4740 remove it and make a new tool item from scratch. */
4741 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4742 img, label, horiz))
4744 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4745 GTK_WIDGET (ti));
4746 ti = NULL;
4749 if (ti == NULL)
4751 GtkWidget *w;
4753 /* Save the image so we can see if an update is needed the
4754 next time we call xg_tool_item_match_p. */
4755 if (EQ (style, Qtext))
4756 w = NULL;
4757 else if (stock_name)
4760 #if GTK_CHECK_VERSION (3, 10, 0)
4761 w = gtk_image_new_from_icon_name (stock_name, icon_size);
4762 #else
4763 w = gtk_image_new_from_stock (stock_name, icon_size);
4764 #endif
4765 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4766 (gpointer) xstrdup (stock_name),
4767 (GDestroyNotify) xfree);
4769 else if (icon_name)
4771 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4772 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4773 (gpointer) xstrdup (icon_name),
4774 (GDestroyNotify) xfree);
4776 else
4778 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4779 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4780 (gpointer)img->pixmap);
4783 #if GTK_CHECK_VERSION (3, 14, 0)
4784 if (w)
4786 gtk_widget_set_margin_start (w, hmargin);
4787 gtk_widget_set_margin_end (w, hmargin);
4788 gtk_widget_set_margin_top (w, vmargin);
4789 gtk_widget_set_margin_bottom (w, vmargin);
4791 #else
4792 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4793 #endif
4794 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4795 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4798 #undef PROP
4800 gtk_widget_set_sensitive (wbutton, enabled_p);
4801 j++;
4804 /* Remove buttons not longer needed. */
4807 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4808 if (ti)
4809 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4810 } while (ti != NULL);
4812 if (f->n_tool_bar_items != 0)
4814 if (! x->toolbar_is_packed)
4815 xg_pack_tool_bar (f, FRAME_TOOL_BAR_POSITION (f));
4816 gtk_widget_show_all (x->toolbar_widget);
4817 if (xg_update_tool_bar_sizes (f))
4818 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4821 unblock_input ();
4824 /* Deallocate all resources for the tool bar on frame F.
4825 Remove the tool bar. */
4827 void
4828 free_frame_tool_bar (struct frame *f)
4830 struct x_output *x = f->output_data.x;
4832 if (x->toolbar_widget)
4834 struct xg_frame_tb_info *tbinfo;
4835 GtkWidget *top_widget = x->toolbar_widget;
4837 block_input ();
4838 if (x->toolbar_is_packed)
4840 if (x->toolbar_in_hbox)
4841 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4842 top_widget);
4843 else
4844 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4845 top_widget);
4847 else
4848 gtk_widget_destroy (x->toolbar_widget);
4850 x->toolbar_widget = 0;
4851 x->toolbar_widget = 0;
4852 x->toolbar_is_packed = false;
4853 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4854 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4856 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4857 TB_INFO_KEY);
4858 if (tbinfo)
4860 xfree (tbinfo);
4861 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4862 TB_INFO_KEY,
4863 NULL);
4866 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4868 unblock_input ();
4872 void
4873 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4875 struct x_output *x = f->output_data.x;
4876 GtkWidget *top_widget = x->toolbar_widget;
4878 if (! x->toolbar_widget || ! top_widget)
4879 return;
4881 block_input ();
4882 g_object_ref (top_widget);
4883 if (x->toolbar_is_packed)
4885 if (x->toolbar_in_hbox)
4886 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4887 top_widget);
4888 else
4889 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4890 top_widget);
4893 xg_pack_tool_bar (f, pos);
4894 g_object_unref (top_widget);
4895 if (xg_update_tool_bar_sizes (f))
4896 adjust_frame_size (f, -1, -1, 2, 0, Qtool_bar_lines);
4898 unblock_input ();
4903 /***********************************************************************
4904 Initializing
4905 ***********************************************************************/
4906 void
4907 xg_initialize (void)
4909 GtkBindingSet *binding_set;
4910 GtkSettings *settings;
4912 #if HAVE_XFT
4913 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4914 we keep it permanently linked in. */
4915 XftInit (0);
4916 #endif
4918 gdpy_def = NULL;
4919 xg_ignore_gtk_scrollbar = 0;
4920 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4921 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4923 id_to_widget.max_size = id_to_widget.used = 0;
4924 id_to_widget.widgets = 0;
4926 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4927 (gdk_display_get_default ()));
4928 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4929 bindings. It doesn't seem to be any way to remove properties,
4930 so we set it to "" which in means "no key". */
4931 gtk_settings_set_string_property (settings,
4932 "gtk-menu-bar-accel",
4934 EMACS_CLASS);
4936 /* Make GTK text input widgets use Emacs style keybindings. This is
4937 Emacs after all. */
4938 gtk_settings_set_string_property (settings,
4939 "gtk-key-theme-name",
4940 "Emacs",
4941 EMACS_CLASS);
4943 /* Make dialogs close on C-g. Since file dialog inherits from
4944 dialog, this works for them also. */
4945 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4946 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4947 "close", 0);
4949 /* Make menus close on C-g. */
4950 binding_set = gtk_binding_set_by_class (g_type_class_ref
4951 (GTK_TYPE_MENU_SHELL));
4952 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4953 "cancel", 0);
4954 update_theme_scrollbar_width ();
4955 update_theme_scrollbar_height ();
4957 #ifdef HAVE_FREETYPE
4958 x_last_font_name = NULL;
4959 #endif
4962 #endif /* USE_GTK */