libgc configuration
[bpt/emacs.git] / src / gtkutil.c
CommitLineData
f392e843 1/* Functions for creating and updating GTK widgets.
95df8112 2
ba318903 3Copyright (C) 2003-2014 Free Software Foundation, Inc.
f392e843
JD
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
f392e843 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
f392e843
JD
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f392e843 19
f3bbb3b5 20#include <config.h>
f392e843
JD
21
22#ifdef USE_GTK
6e1a67fb 23#include <float.h>
e8794476 24#include <stdio.h>
620f13b0
PE
25
26#include <c-ctype.h>
27
f392e843
JD
28#include "lisp.h"
29#include "xterm.h"
30#include "blockinput.h"
aa477689 31#include "syssignal.h"
f392e843 32#include "window.h"
f392e843
JD
33#include "gtkutil.h"
34#include "termhooks.h"
5b07197a
DL
35#include "keyboard.h"
36#include "charset.h"
37#include "coding.h"
d350e350
DA
38#include "font.h"
39
f392e843 40#include <gdk/gdkkeysyms.h>
ff5dec5c 41#include "xsettings.h"
f392e843 42
ddaa36e1
AS
43#ifdef HAVE_XFT
44#include <X11/Xft/Xft.h>
45#endif
810f2256 46
0afb4571
J
47#ifdef HAVE_GTK3
48#include <gtk/gtkx.h>
c195f2de 49#include "emacsgtkfixed.h"
0afb4571
J
50#endif
51
f392e843 52#define FRAME_TOTAL_PIXEL_HEIGHT(f) \
be786000 53 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
cea9be54 54
bfeabdc3
JD
55#define FRAME_TOTAL_PIXEL_WIDTH(f) \
56 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
57
e547b051
J
58#ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
59#define gtk_widget_set_has_window(w, b) \
60 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
61#endif
62#ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
63#define gtk_dialog_get_action_area(w) ((w)->action_area)
64#define gtk_dialog_get_content_area(w) ((w)->vbox)
65#endif
66#ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
67#define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
68#endif
69#ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
70#define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
71#define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
72#define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
73#define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
74#endif
7583e2a0 75#if GTK_CHECK_VERSION (2, 12, 0)
e547b051
J
76#define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
77#else
78#define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
79#endif
80
ad8a47b8 81#ifdef HAVE_FREETYPE
7583e2a0 82#if GTK_CHECK_VERSION (3, 2, 0)
f2045622
CY
83#define USE_NEW_GTK_FONT_CHOOSER 1
84#else
85#define USE_NEW_GTK_FONT_CHOOSER 0
32bcadb4
JD
86#define gtk_font_chooser_dialog_new(x, y) \
87 gtk_font_selection_dialog_new (x)
88#undef GTK_FONT_CHOOSER
89#define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
90#define gtk_font_chooser_set_font(x, y) \
91 gtk_font_selection_dialog_set_font_name (x, y)
32bcadb4 92#endif
ad8a47b8 93#endif /* HAVE_FREETYPE */
32bcadb4 94
0afb4571 95#ifndef HAVE_GTK3
01e0b5ad 96#ifdef USE_GTK_TOOLTIP
0afb4571 97#define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
01e0b5ad 98#endif
0afb4571 99#define gdk_window_get_geometry(w, a, b, c, d) \
7c86ee98 100 gdk_window_get_geometry (w, a, b, c, d, 0)
0afb4571
J
101#define gdk_x11_window_lookup_for_display(d, w) \
102 gdk_xid_table_lookup_for_display (d, w)
383b7c95
JD
103#define gtk_box_new(ori, spacing) \
104 ((ori) == GTK_ORIENTATION_HORIZONTAL \
105 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
106#define gtk_scrollbar_new(ori, spacing) \
107 ((ori) == GTK_ORIENTATION_HORIZONTAL \
108 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
6048fb2a 109#ifndef GDK_KEY_g
0afb4571
J
110#define GDK_KEY_g GDK_g
111#endif
383b7c95 112#endif /* HAVE_GTK3 */
0afb4571 113
4039c786
CY
114#define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
115
c195f2de 116static void update_theme_scrollbar_width (void);
1e5524e7 117
ca06f160
JD
118#define TB_INFO_KEY "xg_frame_tb_info"
119struct xg_frame_tb_info
120{
121 Lisp_Object last_tool_bar;
122 Lisp_Object style;
123 int n_last_items;
124 int hmargin, vmargin;
125 GtkTextDirection dir;
126};
127
810f2256
JD
128\f
129/***********************************************************************
130 Display handling functions
131 ***********************************************************************/
132
c1ffddfc
CY
133/* Keep track of the default display, or NULL if there is none. Emacs
134 may close all its displays. */
879ffad9
JD
135
136static GdkDisplay *gdpy_def;
137
810f2256
JD
138/* When the GTK widget W is to be created on a display for F that
139 is not the default display, set the display for W.
140 W can be a GtkMenu or a GtkWindow widget. */
71bacd48 141
810f2256 142static void
a10c8269 143xg_set_screen (GtkWidget *w, struct frame *f)
810f2256 144{
0afb4571 145 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
810f2256
JD
146 {
147 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
148 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
149
150 if (GTK_IS_MENU (w))
151 gtk_menu_set_screen (GTK_MENU (w), gscreen);
152 else
153 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
154 }
155}
156
157
810f2256
JD
158/* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
159 *DPY is set to NULL if the display can't be opened.
160
161 Returns non-zero if display could be opened, zero if display could not
162 be opened, and less than zero if the GTK version doesn't support
e1dbe924 163 multiple displays. */
71bacd48 164
e547b051 165void
971de7fb 166xg_display_open (char *display_name, Display **dpy)
810f2256 167{
810f2256
JD
168 GdkDisplay *gdpy;
169
170 gdpy = gdk_display_open (display_name);
c39ea606
JD
171 if (!gdpy_def && gdpy)
172 {
173 gdpy_def = gdpy;
174 gdk_display_manager_set_default_display (gdk_display_manager_get (),
175 gdpy);
176 }
810f2256 177
c1ffddfc 178 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
810f2256
JD
179}
180
181
71bacd48
JD
182/* Close display DPY. */
183
810f2256
JD
184void
185xg_display_close (Display *dpy)
186{
810f2256
JD
187 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
188
c1ffddfc
CY
189 /* If this is the default display, try to change it before closing.
190 If there is no other display to use, gdpy_def is set to NULL, and
191 the next call to xg_display_open resets the default display. */
810f2256
JD
192 if (gdk_display_get_default () == gdpy)
193 {
194 struct x_display_info *dpyinfo;
c1ffddfc 195 GdkDisplay *gdpy_new = NULL;
810f2256
JD
196
197 /* Find another display. */
198 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
199 if (dpyinfo->display != dpy)
200 {
c1ffddfc
CY
201 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
202 gdk_display_manager_set_default_display (gdk_display_manager_get (),
203 gdpy_new);
810f2256
JD
204 break;
205 }
c1ffddfc 206 gdpy_def = gdpy_new;
810f2256
JD
207 }
208
7583e2a0 209#if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
c1ffddfc
CY
210 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
211 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
212 can continue running, but there will be memory leaks. */
9d7dda8a 213 g_object_run_dispose (G_OBJECT (gdpy));
810f2256 214#else
9d7dda8a 215 /* This seems to be fixed in GTK 2.10. */
810f2256
JD
216 gdk_display_close (gdpy);
217#endif
810f2256 218}
cea9be54 219
f392e843
JD
220\f
221/***********************************************************************
222 Utility functions
223 ***********************************************************************/
f392e843 224
810f2256
JD
225/* Create and return the cursor to be used for popup menus and
226 scroll bars on display DPY. */
71bacd48 227
810f2256 228GdkCursor *
971de7fb 229xg_create_default_cursor (Display *dpy)
810f2256
JD
230{
231 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
232 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
233}
234
0afb4571 235static GdkPixbuf *
a10c8269 236xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
0afb4571
J
237{
238 int iunused;
239 GdkPixbuf *tmp_buf;
240 Window wunused;
241 unsigned int width, height, uunused;
242 XImage *xim;
243
244 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
245 &width, &height, &uunused, &uunused);
246
247 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
248 ~0, XYPixmap);
249 if (!xim) return 0;
250
7c86ee98 251 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
0afb4571
J
252 GDK_COLORSPACE_RGB,
253 FALSE,
254 xim->bitmap_unit,
5f8f9cc2
PE
255 width,
256 height,
0afb4571
J
257 xim->bytes_per_line,
258 NULL,
259 NULL);
260 XDestroyImage (xim);
261 return tmp_buf;
262}
263
5695f1b4
JD
264/* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
265
1001243b 266static GdkPixbuf *
a10c8269 267xg_get_pixbuf_from_pix_and_mask (struct frame *f,
0afb4571
J
268 Pixmap pix,
269 Pixmap mask)
5695f1b4 270{
32e737d7 271 int width, height;
5695f1b4 272 GdkPixbuf *icon_buf, *tmp_buf;
5695f1b4 273
0afb4571 274 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
5695f1b4
JD
275 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
276 g_object_unref (G_OBJECT (tmp_buf));
277
0afb4571
J
278 width = gdk_pixbuf_get_width (icon_buf);
279 height = gdk_pixbuf_get_height (icon_buf);
7c86ee98 280
0afb4571 281 if (mask)
5695f1b4 282 {
0afb4571 283 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
5695f1b4
JD
284 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
285 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
286 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
287 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
288 int y;
289
290 for (y = 0; y < height; ++y)
291 {
292 guchar *iconptr, *maskptr;
293 int x;
294
295 iconptr = pixels + y * rowstride;
296 maskptr = mask_pixels + y * mask_rowstride;
297
298 for (x = 0; x < width; ++x)
299 {
300 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
301 just R is sufficient. */
302 if (maskptr[0] == 0)
303 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
304
305 iconptr += rowstride/width;
306 maskptr += mask_rowstride/width;
307 }
308 }
309
310 g_object_unref (G_OBJECT (mask_buf));
311 }
312
313 return icon_buf;
314}
315
98a92193 316static Lisp_Object
971de7fb 317file_for_image (Lisp_Object image)
98a92193
JD
318{
319 Lisp_Object specified_file = Qnil;
320 Lisp_Object tail;
98a92193
JD
321
322 for (tail = XCDR (image);
323 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
324 tail = XCDR (XCDR (tail)))
325 if (EQ (XCAR (tail), QCfile))
326 specified_file = XCAR (XCDR (tail));
327
328 return specified_file;
329}
330
5b166323
JD
331/* For the image defined in IMG, make and return a GtkImage. For displays with
332 8 planes or less we must make a GdkPixbuf and apply the mask manually.
3ed8598c 333 Otherwise the highlighting and dimming the tool bar code in GTK does
5b166323
JD
334 will look bad. For display with more than 8 planes we just use the
335 pixmap and mask directly. For monochrome displays, GTK doesn't seem
336 able to use external pixmaps, it looks bad whatever we do.
337 The image is defined on the display where frame F is.
338 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
339 If OLD_WIDGET is NULL, a new widget is constructed and returned.
340 If OLD_WIDGET is not NULL, that widget is modified. */
71bacd48 341
5b166323 342static GtkWidget *
a10c8269 343xg_get_image_for_pixmap (struct frame *f,
e4c8d29a
J
344 struct image *img,
345 GtkWidget *widget,
346 GtkImage *old_widget)
810f2256 347{
45c94881 348 GdkPixbuf *icon_buf;
03ecb80f 349
0facd9c3 350 /* If we have a file, let GTK do all the image handling.
03ecb80f 351 This seems to be the only way to make insensitive and activated icons
0facd9c3 352 look good in all cases. */
98a92193 353 Lisp_Object specified_file = file_for_image (img->spec);
a821c035 354 Lisp_Object file;
0facd9c3 355
a821c035
JD
356 /* We already loaded the image once before calling this
357 function, so this only fails if the image file has been removed.
358 In that case, use the pixmap already loaded. */
0facd9c3 359
a821c035
JD
360 if (STRINGP (specified_file)
361 && STRINGP (file = x_find_image_file (specified_file)))
362 {
0facd9c3
JD
363 if (! old_widget)
364 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
365 else
366 gtk_image_set_from_file (old_widget, SSDATA (file));
367
0facd9c3 368 return GTK_WIDGET (old_widget);
03ecb80f 369 }
810f2256 370
0facd9c3
JD
371 /* No file, do the image handling ourselves. This will look very bad
372 on a monochrome display, and sometimes bad on all displays with
373 certain themes. */
374
45c94881
JD
375 /* This is a workaround to make icons look good on pseudo color
376 displays. Apparently GTK expects the images to have an alpha
377 channel. If they don't, insensitive and activated icons will
378 look bad. This workaround does not work on monochrome displays,
379 and is strictly not needed on true color/static color displays (i.e.
380 16 bits and higher). But we do it anyway so we get a pixbuf that is
381 not associated with the img->pixmap. The img->pixmap may be removed
382 by clearing the image cache and then the tool bar redraw fails, since
383 Gtk+ assumes the pixmap is always there. */
0afb4571 384 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
5695f1b4 385
7c86ee98 386 if (icon_buf)
0afb4571
J
387 {
388 if (! old_widget)
389 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
390 else
391 gtk_image_set_from_pixbuf (old_widget, icon_buf);
5b166323 392
0afb4571
J
393 g_object_unref (G_OBJECT (icon_buf));
394 }
74cdfe05 395
5b166323 396 return GTK_WIDGET (old_widget);
810f2256
JD
397}
398
399
400/* Set CURSOR on W and all widgets W contain. We must do like this
401 for scroll bars and menu because they create widgets internally,
402 and it is those widgets that are visible. */
71bacd48 403
810f2256 404static void
971de7fb 405xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
f392e843 406{
5e617bc2 407 GdkWindow *window = gtk_widget_get_window (w);
e547b051 408 GList *children = gdk_window_peek_children (window);
f392e843 409
e547b051 410 gdk_window_set_cursor (window, cursor);
f392e843
JD
411
412 /* The scroll bar widget has more than one GDK window (had to look at
413 the source to figure this out), and there is no way to set cursor
414 on widgets in GTK. So we must set the cursor for all GDK windows.
415 Ditto for menus. */
416
417 for ( ; children; children = g_list_next (children))
810f2256 418 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
f392e843
JD
419}
420
f392e843 421/* Insert NODE into linked LIST. */
71bacd48 422
f392e843
JD
423static void
424xg_list_insert (xg_list_node *list, xg_list_node *node)
425{
426 xg_list_node *list_start = list->next;
177c0ea7 427
f392e843
JD
428 if (list_start) list_start->prev = node;
429 node->next = list_start;
430 node->prev = 0;
431 list->next = node;
432}
433
434/* Remove NODE from linked LIST. */
71bacd48 435
f392e843
JD
436static void
437xg_list_remove (xg_list_node *list, xg_list_node *node)
438{
439 xg_list_node *list_start = list->next;
440 if (node == list_start)
441 {
442 list->next = node->next;
443 if (list->next) list->next->prev = 0;
444 }
445 else
446 {
447 node->prev->next = node->next;
448 if (node->next) node->next->prev = node->prev;
449 }
450}
451
452/* Allocate and return a utf8 version of STR. If STR is already
42ca4633
J
453 utf8 or NULL, just return a copy of STR.
454 A new string is allocated and the caller must free the result
f392e843 455 with g_free. */
71bacd48 456
f392e843 457static char *
42ca4633 458get_utf8_string (const char *str)
f392e843 459{
42ca4633 460 char *utf8_str;
177c0ea7 461
241ad3ca
JD
462 if (!str) return NULL;
463
f392e843 464 /* If not UTF-8, try current locale. */
241ad3ca 465 if (!g_utf8_validate (str, -1, NULL))
f392e843 466 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
42ca4633
J
467 else
468 return g_strdup (str);
f392e843 469
8e5a8840 470 if (!utf8_str)
241ad3ca
JD
471 {
472 /* Probably some control characters in str. Escape them. */
0eb0f318
PE
473 ptrdiff_t len;
474 ptrdiff_t nr_bad = 0;
241ad3ca
JD
475 gsize bytes_read;
476 gsize bytes_written;
477 unsigned char *p = (unsigned char *)str;
478 char *cp, *up;
65dc836c 479 GError *err = NULL;
241ad3ca 480
b43da352 481 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
65dc836c
PE
482 &bytes_written, &err))
483 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
241ad3ca
JD
484 {
485 ++nr_bad;
486 p += bytes_written+1;
65dc836c
PE
487 g_error_free (err);
488 err = NULL;
241ad3ca
JD
489 }
490
65dc836c 491 if (err)
241ad3ca 492 {
65dc836c
PE
493 g_error_free (err);
494 err = NULL;
241ad3ca
JD
495 }
496 if (cp) g_free (cp);
497
0eb0f318 498 len = strlen (str);
7216e43b 499 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
0eb0f318
PE
500 memory_full (SIZE_MAX);
501 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
b43da352 502 p = (unsigned char *)str;
241ad3ca 503
b43da352 504 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
65dc836c
PE
505 &bytes_written, &err))
506 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
241ad3ca 507 {
e99a530f 508 memcpy (up, p, bytes_written);
241ad3ca 509 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
241ad3ca
JD
510 up += bytes_written+4;
511 p += bytes_written+1;
65dc836c
PE
512 g_error_free (err);
513 err = NULL;
241ad3ca
JD
514 }
515
8e5a8840 516 if (cp)
241ad3ca
JD
517 {
518 strcat (utf8_str, cp);
519 g_free (cp);
520 }
65dc836c 521 if (err)
241ad3ca 522 {
65dc836c
PE
523 g_error_free (err);
524 err = NULL;
241ad3ca
JD
525 }
526 }
f392e843
JD
527 return utf8_str;
528}
529
3a46642b
J
530/* Check for special colors used in face spec for region face.
531 The colors are fetched from the Gtk+ theme.
18e27ea8 532 Return true if color was found, false if not. */
3a46642b 533
18e27ea8 534bool
3a46642b
J
535xg_check_special_colors (struct frame *f,
536 const char *color_name,
537 XColor *color)
538{
18e27ea8
PE
539 bool success_p = 0;
540 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
541 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
0afb4571
J
542
543 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
544 return success_p;
545
4d7e6e51 546 block_input ();
0afb4571
J
547 {
548#ifdef HAVE_GTK3
549 GtkStyleContext *gsty
550 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
551 GdkRGBA col;
b24ac90f 552 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
0afb4571
J
553 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
554 if (get_fg)
555 gtk_style_context_get_color (gsty, state, &col);
556 else
557 gtk_style_context_get_background_color (gsty, state, &col);
558
b24ac90f
JD
559 sprintf (buf, "rgb:%04x/%04x/%04x",
560 (int)(col.red * 65535),
561 (int)(col.green * 65535),
562 (int)(col.blue * 65535));
18e27ea8
PE
563 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
564 buf, color)
565 != 0);
0afb4571
J
566#else
567 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
568 GdkColor *grgb = get_bg
569 ? &gsty->bg[GTK_STATE_SELECTED]
570 : &gsty->fg[GTK_STATE_SELECTED];
571
572 color->red = grgb->red;
573 color->green = grgb->green;
574 color->blue = grgb->blue;
575 color->pixel = grgb->pixel;
576 success_p = 1;
577#endif
3a46642b 578
0afb4571 579 }
4d7e6e51 580 unblock_input ();
3a46642b
J
581 return success_p;
582}
583
f392e843
JD
584
585\f
aa1859f5
J
586/***********************************************************************
587 Tooltips
588 ***********************************************************************/
589/* Gtk+ calls this callback when the parent of our tooltip dummy changes.
590 We use that to pop down the tooltip. This happens if Gtk+ for some
591 reason wants to change or hide the tooltip. */
592
ac01763e
JD
593#ifdef USE_GTK_TOOLTIP
594
aa1859f5
J
595static void
596hierarchy_ch_cb (GtkWidget *widget,
597 GtkWidget *previous_toplevel,
598 gpointer user_data)
599{
7d652d97 600 struct frame *f = user_data;
aa1859f5
J
601 struct x_output *x = f->output_data.x;
602 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
088c8c09 603
aa1859f5
J
604 if (! top || ! GTK_IS_WINDOW (top))
605 gtk_widget_hide (previous_toplevel);
606}
607
608/* Callback called when Gtk+ thinks a tooltip should be displayed.
609 We use it to get the tooltip window and the tooltip widget so
610 we can manipulate the ourselves.
611
612 Return FALSE ensures that the tooltip is not shown. */
613
614static gboolean
615qttip_cb (GtkWidget *widget,
616 gint xpos,
617 gint ypos,
618 gboolean keyboard_mode,
619 GtkTooltip *tooltip,
620 gpointer user_data)
621{
7d652d97 622 struct frame *f = user_data;
aa1859f5 623 struct x_output *x = f->output_data.x;
088c8c09 624 if (x->ttip_widget == NULL)
aa1859f5 625 {
1b854618
JD
626 GtkWidget *p;
627 GList *list, *iter;
628
aa1859f5
J
629 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
630 x->ttip_widget = tooltip;
631 g_object_ref (G_OBJECT (tooltip));
632 x->ttip_lbl = gtk_label_new ("");
633 g_object_ref (G_OBJECT (x->ttip_lbl));
634 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
635 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
1b854618
JD
636
637 /* Change stupid Gtk+ default line wrapping. */
638 p = gtk_widget_get_parent (x->ttip_lbl);
639 list = gtk_container_get_children (GTK_CONTAINER (p));
1b854618
JD
640 for (iter = list; iter; iter = g_list_next (iter))
641 {
642 GtkWidget *w = GTK_WIDGET (iter->data);
643 if (GTK_IS_LABEL (w))
644 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
645 }
646 g_list_free (list);
647
8daaeda6
J
648 /* ATK needs an empty title for some reason. */
649 gtk_window_set_title (x->ttip_window, "");
aa1859f5
J
650 /* Realize so we can safely get screen later on. */
651 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
652 gtk_widget_realize (x->ttip_lbl);
653
654 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
655 G_CALLBACK (hierarchy_ch_cb), f);
656 }
657 return FALSE;
658}
659
ac01763e
JD
660#endif /* USE_GTK_TOOLTIP */
661
aa1859f5 662/* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
18e27ea8 663 Return true if a system tooltip is available. */
aa1859f5 664
18e27ea8 665bool
a10c8269 666xg_prepare_tooltip (struct frame *f,
0ce7e563
JD
667 Lisp_Object string,
668 int *width,
aa1859f5
J
669 int *height)
670{
ac01763e
JD
671#ifndef USE_GTK_TOOLTIP
672 return 0;
673#else
aa1859f5
J
674 struct x_output *x = f->output_data.x;
675 GtkWidget *widget;
676 GdkWindow *gwin;
677 GdkScreen *screen;
678 GtkSettings *settings;
679 gboolean tt_enabled = TRUE;
680 GtkRequisition req;
681 Lisp_Object encoded_string;
682
683 if (!x->ttip_lbl) return 0;
684
4d7e6e51 685 block_input ();
aa1859f5
J
686 encoded_string = ENCODE_UTF_8 (string);
687 widget = GTK_WIDGET (x->ttip_lbl);
688 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
0afb4571 689 screen = gdk_window_get_screen (gwin);
aa1859f5
J
690 settings = gtk_settings_get_for_screen (screen);
691 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
088c8c09 692 if (tt_enabled)
aa1859f5
J
693 {
694 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
695 /* Record that we disabled it so it can be enabled again. */
696 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
697 (gpointer)f);
698 }
088c8c09 699
aa1859f5
J
700 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
701 g_object_set_data (G_OBJECT
702 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
703 "gdk-display-current-tooltip", NULL);
704
0ce7e563 705 /* Put our dummy widget in so we can get callbacks for unrealize and
aa1859f5
J
706 hierarchy-changed. */
707 gtk_tooltip_set_custom (x->ttip_widget, widget);
1b854618 708 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
0afb4571 709 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
aa1859f5
J
710 if (width) *width = req.width;
711 if (height) *height = req.height;
088c8c09 712
4d7e6e51 713 unblock_input ();
aa1859f5
J
714
715 return 1;
ac01763e 716#endif /* USE_GTK_TOOLTIP */
aa1859f5
J
717}
718
719/* Show the tooltip at ROOT_X and ROOT_Y.
720 xg_prepare_tooltip must have been called before this function. */
721
722void
a10c8269 723xg_show_tooltip (struct frame *f, int root_x, int root_y)
aa1859f5 724{
ac01763e 725#ifdef USE_GTK_TOOLTIP
aa1859f5
J
726 struct x_output *x = f->output_data.x;
727 if (x->ttip_window)
728 {
4d7e6e51 729 block_input ();
aa1859f5
J
730 gtk_window_move (x->ttip_window, root_x, root_y);
731 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
4d7e6e51 732 unblock_input ();
aa1859f5 733 }
ac01763e 734#endif
aa1859f5
J
735}
736
737/* Hide tooltip if shown. Do nothing if not shown.
18e27ea8 738 Return true if tip was hidden, false if not (i.e. not using
aa1859f5
J
739 system tooltips). */
740
18e27ea8 741bool
a10c8269 742xg_hide_tooltip (struct frame *f)
aa1859f5 743{
18e27ea8 744 bool ret = 0;
ac01763e 745#ifdef USE_GTK_TOOLTIP
aa1859f5
J
746 if (f->output_data.x->ttip_window)
747 {
748 GtkWindow *win = f->output_data.x->ttip_window;
4d7e6e51 749 block_input ();
aa1859f5
J
750 gtk_widget_hide (GTK_WIDGET (win));
751
752 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
753 {
754 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
0afb4571 755 GdkScreen *screen = gdk_window_get_screen (gwin);
aa1859f5
J
756 GtkSettings *settings = gtk_settings_get_for_screen (screen);
757 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
758 }
4d7e6e51 759 unblock_input ();
aa1859f5
J
760
761 ret = 1;
762 }
ac01763e 763#endif
aa1859f5
J
764 return ret;
765}
766
767\f
f392e843
JD
768/***********************************************************************
769 General functions for creating widgets, resizing, events, e.t.c.
770 ***********************************************************************/
771
005c8d13
JD
772static void
773my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
774 const gchar *msg, gpointer user_data)
775{
776 if (!strstr (msg, "visible children"))
777 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
778}
779
f392e843
JD
780/* Make a geometry string and pass that to GTK. It seems this is the
781 only way to get geometry position right if the user explicitly
782 asked for a position when starting Emacs.
783 F is the frame we shall set geometry for. */
71bacd48 784
f392e843 785static void
a10c8269 786xg_set_geometry (struct frame *f)
f392e843 787{
92848133 788 if (f->size_hint_flags & (USPosition | PPosition))
1c9c1270
JD
789 {
790 int left = f->left_pos;
791 int xneg = f->size_hint_flags & XNegative;
792 int top = f->top_pos;
793 int yneg = f->size_hint_flags & YNegative;
84722b3d 794 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
005c8d13 795 guint id;
1c9c1270
JD
796
797 if (xneg)
798 left = -left;
799 if (yneg)
800 top = -top;
801
22aa44a8
JD
802 sprintf (geom_str, "=%dx%d%c%d%c%d",
803 FRAME_PIXEL_WIDTH (f),
804 FRAME_PIXEL_HEIGHT (f),
1c9c1270
JD
805 (xneg ? '-' : '+'), left,
806 (yneg ? '-' : '+'), top);
807
005c8d13
JD
808 /* Silence warning about visible children. */
809 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
810 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
811
1c9c1270
JD
812 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
813 geom_str))
814 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
005c8d13
JD
815
816 g_log_remove_handler ("Gtk", id);
1c9c1270 817 }
f392e843
JD
818}
819
7c583cd8
JD
820/* Clear under internal border if any. As we use a mix of Gtk+ and X calls
821 and use a GtkFixed widget, this doesn't happen automatically. */
822
823static void
a10c8269 824xg_clear_under_internal_border (struct frame *f)
7c583cd8
JD
825{
826 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
827 {
828 GtkWidget *wfixed = f->output_data.x->edit_widget;
1f5cf200 829
7c583cd8
JD
830 gtk_widget_queue_draw (wfixed);
831 gdk_window_process_all_updates ();
1f5cf200
DA
832
833 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
834 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
835
836 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
837 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
838
839 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
840 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
841 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
842
843 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
844 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
845 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
7c583cd8
JD
846 }
847}
848
1e39cbfb
JD
849/* Function to handle resize of our frame. As we have a Gtk+ tool bar
850 and a Gtk+ menu bar, we get resize events for the edit part of the
851 frame only. We let Gtk+ deal with the Gtk+ parts.
f392e843
JD
852 F is the frame to resize.
853 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
71bacd48 854
f392e843 855void
a10c8269 856xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
f392e843 857{
880e6158 858 int width, height;
7b507248
JD
859
860 if (pixelwidth == -1 && pixelheight == -1)
861 {
e547b051
J
862 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
863 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
864 0, 0,
0afb4571 865 &pixelwidth, &pixelheight);
7b507248
JD
866 else return;
867 }
088c8c09 868
7b507248 869
880e6158
MR
870 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
871 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
7b507248 872
880e6158
MR
873 if (width != FRAME_TEXT_WIDTH (f)
874 || height != FRAME_TEXT_HEIGHT (f)
7b507248
JD
875 || pixelwidth != FRAME_PIXEL_WIDTH (f)
876 || pixelheight != FRAME_PIXEL_HEIGHT (f))
f392e843 877 {
1e39cbfb
JD
878 FRAME_PIXEL_WIDTH (f) = pixelwidth;
879 FRAME_PIXEL_HEIGHT (f) = pixelheight;
f392e843 880
7c583cd8 881 xg_clear_under_internal_border (f);
880e6158 882 change_frame_size (f, width, height, 0, 1, 0, 1);
5a130941
JD
883 SET_FRAME_GARBAGED (f);
884 cancel_mouse_face (f);
1e39cbfb
JD
885 }
886}
f392e843 887
da6062e6 888/* Resize the outer window of frame F after changing the height.
1c9c1270 889 COLUMNS/ROWS is the size the edit area shall have after the resize. */
71bacd48 890
f392e843 891void
880e6158 892xg_frame_set_char_size (struct frame *f, int width, int height)
f392e843 893{
7303a0ae 894 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
880e6158 895 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
177c0ea7 896
1e39cbfb
JD
897 if (FRAME_PIXEL_HEIGHT (f) == 0)
898 return;
899
7c583cd8
JD
900 /* Do this before resize, as we don't know yet if we will be resized. */
901 xg_clear_under_internal_border (f);
902
f392e843 903 /* Must resize our top level widget. Font size may have changed,
3f1c6666 904 but not rows/cols. */
f392e843 905 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
0b254a38
MR
906 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
907 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
908 + FRAME_MENUBAR_HEIGHT (f));
3f1c6666 909 x_wm_set_size_hint (f, 0, 0);
47a6002f 910
5c646d5a
JD
911 SET_FRAME_GARBAGED (f);
912 cancel_mouse_face (f);
913
7b507248
JD
914 /* We can not call change_frame_size for a mapped frame,
915 we can not set pixel width/height either. The window manager may
916 override our resize request, XMonad does this all the time.
917 The best we can do is try to sync, so lisp code sees the updated
918 size as fast as possible.
919 For unmapped windows, we can set rows/cols. When
920 the frame is mapped again we will (hopefully) get the correct size. */
edfa7fa0 921 if (FRAME_VISIBLE_P (f))
5c646d5a
JD
922 {
923 /* Must call this to flush out events */
924 (void)gtk_events_pending ();
925 gdk_flush ();
926 x_wait_for_event (f, ConfigureNotify);
927 }
7b507248 928 else
7303a0ae 929 change_frame_size (f, width, height, 0, 1, 0, 1);
3f1c6666
JD
930}
931
bfeabdc3 932/* Handle height/width changes (i.e. add/remove/move menu/toolbar).
3f1c6666
JD
933 The policy is to keep the number of editable lines. */
934
935static void
a10c8269 936xg_height_or_width_changed (struct frame *f)
3f1c6666
JD
937{
938 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
bfeabdc3
JD
939 FRAME_TOTAL_PIXEL_WIDTH (f),
940 FRAME_TOTAL_PIXEL_HEIGHT (f));
3f1c6666 941 f->output_data.x->hint_flags = 0;
f26fab36 942 x_wm_set_size_hint (f, 0, 0);
f392e843
JD
943}
944
810f2256 945/* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
f392e843
JD
946 Must be done like this, because GtkWidget:s can have "hidden"
947 X Window that aren't accessible.
948
949 Return 0 if no widget match WDESC. */
71bacd48 950
f392e843 951GtkWidget *
971de7fb 952xg_win_to_widget (Display *dpy, Window wdesc)
f392e843
JD
953{
954 gpointer gdkwin;
955 GtkWidget *gwdesc = 0;
956
4d7e6e51 957 block_input ();
810f2256 958
0afb4571
J
959 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
960 wdesc);
f392e843
JD
961 if (gdkwin)
962 {
963 GdkEvent event;
964 event.any.window = gdkwin;
3b574623 965 event.any.type = GDK_NOTHING;
f392e843
JD
966 gwdesc = gtk_get_event_widget (&event);
967 }
177c0ea7 968
4d7e6e51 969 unblock_input ();
f392e843
JD
970 return gwdesc;
971}
972
0afb4571 973/* Set the background of widget W to PIXEL. */
71bacd48 974
f392e843 975static void
2d9783e0 976xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
f392e843 977{
0afb4571
J
978#ifdef HAVE_GTK3
979 GdkRGBA bg;
980 XColor xbg;
981 xbg.pixel = pixel;
982 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
983 {
537f336d
JD
984 bg.red = (double)xbg.red/65535.0;
985 bg.green = (double)xbg.green/65535.0;
986 bg.blue = (double)xbg.blue/65535.0;
0afb4571
J
987 bg.alpha = 1.0;
988 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
989 }
990#else
991 GdkColor bg;
f392e843 992 GdkColormap *map = gtk_widget_get_colormap (w);
0afb4571
J
993 gdk_colormap_query_color (map, pixel, &bg);
994 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
995#endif
f392e843
JD
996}
997
3a46642b
J
998/* Callback called when the gtk theme changes.
999 We notify lisp code so it can fix faces used for region for example. */
1000
1001static void
1002style_changed_cb (GObject *go,
1003 GParamSpec *spec,
1004 gpointer user_data)
1005{
1006 struct input_event event;
7d652d97 1007 GdkDisplay *gdpy = user_data;
3a46642b 1008 const char *display_name = gdk_display_get_name (gdpy);
c195f2de 1009 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
3a46642b
J
1010
1011 EVENT_INIT (event);
1012 event.kind = CONFIG_CHANGED_EVENT;
9bda3520 1013 event.frame_or_window = build_string (display_name);
3a46642b
J
1014 /* Theme doesn't change often, so intern is called seldom. */
1015 event.arg = intern ("theme-name");
1016 kbd_buffer_store_event (&event);
c195f2de
JD
1017
1018 update_theme_scrollbar_width ();
1019
1020 /* If scroll bar width changed, we need set the new size on all frames
1021 on this display. */
9bda3520 1022 if (dpy)
c195f2de
JD
1023 {
1024 Lisp_Object rest, frame;
1025 FOR_EACH_FRAME (rest, frame)
1026 {
a10c8269 1027 struct frame *f = XFRAME (frame);
54e95010
JD
1028 if (FRAME_LIVE_P (f)
1029 && FRAME_X_P (f)
1030 && FRAME_X_DISPLAY (f) == dpy)
c195f2de
JD
1031 {
1032 x_set_scroll_bar_default_width (f);
7303a0ae 1033 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
c195f2de
JD
1034 }
1035 }
1036 }
3a46642b
J
1037}
1038
0afb4571
J
1039/* Called when a delete-event occurs on WIDGET. */
1040
1041static gboolean
1042delete_cb (GtkWidget *widget,
1043 GdkEvent *event,
1044 gpointer user_data)
1045{
1046#ifdef HAVE_GTK3
1047 /* The event doesn't arrive in the normal event loop. Send event
1048 here. */
7d652d97 1049 struct frame *f = user_data;
0afb4571
J
1050 struct input_event ie;
1051
1052 EVENT_INIT (ie);
1053 ie.kind = DELETE_WINDOW_EVENT;
1054 XSETFRAME (ie.frame_or_window, f);
1055 kbd_buffer_store_event (&ie);
1056#endif
1057
1058 return TRUE;
1059}
1060
f392e843 1061/* Create and set up the GTK widgets for frame F.
18e27ea8 1062 Return true if creation succeeded. */
71bacd48 1063
18e27ea8 1064bool
a10c8269 1065xg_create_frame_widgets (struct frame *f)
f392e843
JD
1066{
1067 GtkWidget *wtop;
bfeabdc3 1068 GtkWidget *wvbox, *whbox;
f392e843 1069 GtkWidget *wfixed;
1068fe4d 1070#ifndef HAVE_GTK3
f392e843 1071 GtkRcStyle *style;
1068fe4d 1072#endif
f392e843 1073 char *title = 0;
177c0ea7 1074
4d7e6e51 1075 block_input ();
f392e843 1076
0f0d223b 1077 if (FRAME_X_EMBEDDED_P (f))
383b7c95
JD
1078 {
1079 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1080 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1081 }
0f0d223b
JD
1082 else
1083 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1084
54e9e3bf
JD
1085 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1086 has backported it to Gtk+ 2.0 and they add the resize grip for
1087 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1088 forever, so disable the grip. */
7583e2a0
PE
1089#if (! GTK_CHECK_VERSION (3, 0, 0) \
1090 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
54e9e3bf
JD
1091 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1092#endif
1093
810f2256
JD
1094 xg_set_screen (wtop, f);
1095
383b7c95
JD
1096 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1097 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1098 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1099 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
c195f2de
JD
1100
1101#ifdef HAVE_GTK3
c7e73be5 1102 wfixed = emacs_fixed_new (f);
c195f2de
JD
1103#else
1104 wfixed = gtk_fixed_new ();
1105#endif
177c0ea7 1106
bfeabdc3 1107 if (! wtop || ! wvbox || ! whbox || ! wfixed)
f392e843
JD
1108 {
1109 if (wtop) gtk_widget_destroy (wtop);
1110 if (wvbox) gtk_widget_destroy (wvbox);
bfeabdc3 1111 if (whbox) gtk_widget_destroy (whbox);
f392e843
JD
1112 if (wfixed) gtk_widget_destroy (wfixed);
1113
4d7e6e51 1114 unblock_input ();
f392e843
JD
1115 return 0;
1116 }
1117
1118 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1119 gtk_widget_set_name (wtop, EMACS_CLASS);
1120 gtk_widget_set_name (wvbox, "pane");
93c579e0 1121 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
f392e843
JD
1122
1123 /* If this frame has a title or name, set it in the title bar. */
e69b0960
DA
1124 if (! NILP (f->title))
1125 title = SSDATA (ENCODE_UTF_8 (f->title));
1126 else if (! NILP (f->name))
1127 title = SSDATA (ENCODE_UTF_8 (f->name));
f392e843
JD
1128
1129 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
177c0ea7 1130
f392e843
JD
1131 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1132 FRAME_GTK_WIDGET (f) = wfixed;
1133 f->output_data.x->vbox_widget = wvbox;
bfeabdc3 1134 f->output_data.x->hbox_widget = whbox;
f392e843 1135
e547b051 1136 gtk_widget_set_has_window (wfixed, TRUE);
f392e843 1137
f392e843 1138 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
bfeabdc3
JD
1139 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1140 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
f392e843
JD
1141
1142 if (FRAME_EXTERNAL_TOOL_BAR (f))
1143 update_frame_tool_bar (f);
1144
7863d625
JD
1145 /* We don't want this widget double buffered, because we draw on it
1146 with regular X drawing primitives, so from a GTK/GDK point of
1147 view, the widget is totally blank. When an expose comes, this
1148 will make the widget blank, and then Emacs redraws it. This flickers
1149 a lot, so we turn off double buffering. */
f392e843 1150 gtk_widget_set_double_buffered (wfixed, FALSE);
7863d625 1151
f392e843 1152 gtk_window_set_wmclass (GTK_WINDOW (wtop),
93c579e0
JD
1153 SSDATA (Vx_resource_name),
1154 SSDATA (Vx_resource_class));
f392e843
JD
1155
1156 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1157 GTK is to destroy the widget. We want Emacs to do that instead. */
1158 g_signal_connect (G_OBJECT (wtop), "delete-event",
0afb4571 1159 G_CALLBACK (delete_cb), f);
177c0ea7 1160
f392e843
JD
1161 /* Convert our geometry parameters into a geometry string
1162 and specify it.
1163 GTK will itself handle calculating the real position this way. */
1164 xg_set_geometry (f);
32e737d7
JD
1165 f->win_gravity
1166 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
f392e843
JD
1167
1168 gtk_widget_add_events (wfixed,
1169 GDK_POINTER_MOTION_MASK
1170 | GDK_EXPOSURE_MASK
1171 | GDK_BUTTON_PRESS_MASK
1172 | GDK_BUTTON_RELEASE_MASK
1173 | GDK_KEY_PRESS_MASK
1174 | GDK_ENTER_NOTIFY_MASK
1175 | GDK_LEAVE_NOTIFY_MASK
1176 | GDK_FOCUS_CHANGE_MASK
1177 | GDK_STRUCTURE_MASK
1178 | GDK_VISIBILITY_NOTIFY_MASK);
1179
1180 /* Must realize the windows so the X window gets created. It is used
1181 by callers of this function. */
1182 gtk_widget_realize (wfixed);
1183 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1184
1185 /* Since GTK clears its window by filling with the background color,
1186 we must keep X and GTK background in sync. */
0afb4571 1187 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
f392e843 1188
0afb4571 1189#ifndef HAVE_GTK3
f392e843
JD
1190 /* Also, do not let any background pixmap to be set, this looks very
1191 bad as Emacs overwrites the background pixmap with its own idea
1192 of background color. */
1193 style = gtk_widget_get_modifier_style (wfixed);
1194
1195 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1196 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1197 gtk_widget_modify_style (wfixed, style);
0afb4571
J
1198#else
1199 gtk_widget_set_can_focus (wfixed, TRUE);
c195f2de 1200 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
0afb4571 1201#endif
177c0ea7 1202
ac01763e 1203#ifdef USE_GTK_TOOLTIP
aa1859f5
J
1204 /* Steal a tool tip window we can move ourselves. */
1205 f->output_data.x->ttip_widget = 0;
1206 f->output_data.x->ttip_lbl = 0;
1207 f->output_data.x->ttip_window = 0;
088c8c09 1208 gtk_widget_set_tooltip_text (wtop, "Dummy text");
aa1859f5 1209 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
ac01763e 1210#endif
f392e843 1211
3a46642b
J
1212 {
1213 GdkScreen *screen = gtk_widget_get_screen (wtop);
1214 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1215 /* Only connect this signal once per screen. */
1216 if (! g_signal_handler_find (G_OBJECT (gs),
1217 G_SIGNAL_MATCH_FUNC,
1218 0, 0, 0,
1219 G_CALLBACK (style_changed_cb),
1220 0))
1221 {
1222 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1223 G_CALLBACK (style_changed_cb),
1224 gdk_screen_get_display (screen));
1225 }
1226 }
1227
4d7e6e51 1228 unblock_input ();
f392e843
JD
1229
1230 return 1;
1231}
1232
aa1859f5 1233void
a10c8269 1234xg_free_frame_widgets (struct frame *f)
aa1859f5
J
1235{
1236 if (FRAME_GTK_OUTER_WIDGET (f))
1237 {
b0afc268 1238#ifdef USE_GTK_TOOLTIP
aa1859f5 1239 struct x_output *x = f->output_data.x;
b0afc268 1240#endif
ca06f160
JD
1241 struct xg_frame_tb_info *tbinfo
1242 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1243 TB_INFO_KEY);
1244 if (tbinfo)
1245 xfree (tbinfo);
1246
aa1859f5
J
1247 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1248 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1249 FRAME_GTK_OUTER_WIDGET (f) = 0;
ac01763e 1250#ifdef USE_GTK_TOOLTIP
aa1859f5
J
1251 if (x->ttip_lbl)
1252 gtk_widget_destroy (x->ttip_lbl);
1253 if (x->ttip_widget)
1254 g_object_unref (G_OBJECT (x->ttip_widget));
ac01763e 1255#endif
aa1859f5
J
1256 }
1257}
1258
f392e843
JD
1259/* Set the normal size hints for the window manager, for frame F.
1260 FLAGS is the flags word to use--or 0 meaning preserve the flags
1261 that the window now has.
18e27ea8 1262 If USER_POSITION, set the User Position
f392e843 1263 flag (this is useful when FLAGS is 0). */
71bacd48 1264
f392e843 1265void
a10c8269 1266x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
f392e843 1267{
3e5fc571
CY
1268 /* Must use GTK routines here, otherwise GTK resets the size hints
1269 to its own defaults. */
1270 GdkGeometry size_hints;
1271 gint hint_flags = 0;
1272 int base_width, base_height;
1273 int min_rows = 0, min_cols = 0;
1274 int win_gravity = f->win_gravity;
6e1b469e 1275 Lisp_Object fs_state, frame;
3e5fc571 1276
32e737d7
JD
1277 /* Don't set size hints during initialization; that apparently leads
1278 to a race condition. See the thread at
1279 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1280 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1281 return;
1282
6e1b469e
JD
1283 XSETFRAME (frame, f);
1284 fs_state = Fframe_parameter (frame, Qfullscreen);
1285 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1286 {
1287 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1288 Gtk3 don't get along and the frame shrinks (!).
1289 */
1290 return;
1291 }
1292
3e5fc571
CY
1293 if (flags)
1294 {
1295 memset (&size_hints, 0, sizeof (size_hints));
1296 f->output_data.x->size_hints = size_hints;
1297 f->output_data.x->hint_flags = hint_flags;
1298 }
1299 else
1300 flags = f->size_hint_flags;
1301
1302 size_hints = f->output_data.x->size_hints;
1303 hint_flags = f->output_data.x->hint_flags;
1304
1305 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
9b3c0a16
MR
1306 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1307 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
3e5fc571
CY
1308
1309 hint_flags |= GDK_HINT_BASE_SIZE;
b4444c8a 1310 /* Use one row/col here so base_height/width does not become zero.
89c94350 1311 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
b4444c8a 1312 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
89c94350 1313 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
3e5fc571
CY
1314 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1315
880e6158 1316 check_frame_size (f, &min_cols, &min_rows, 0);
b4444c8a 1317 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
89c94350 1318 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
3e5fc571
CY
1319
1320 size_hints.base_width = base_width;
1321 size_hints.base_height = base_height;
9b3c0a16
MR
1322 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);;
1323 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
3e5fc571
CY
1324
1325 /* These currently have a one to one mapping with the X values, but I
1326 don't think we should rely on that. */
1327 hint_flags |= GDK_HINT_WIN_GRAVITY;
1328 size_hints.win_gravity = 0;
1329 if (win_gravity == NorthWestGravity)
1330 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1331 else if (win_gravity == NorthGravity)
1332 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1333 else if (win_gravity == NorthEastGravity)
1334 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1335 else if (win_gravity == WestGravity)
1336 size_hints.win_gravity = GDK_GRAVITY_WEST;
1337 else if (win_gravity == CenterGravity)
1338 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1339 else if (win_gravity == EastGravity)
1340 size_hints.win_gravity = GDK_GRAVITY_EAST;
1341 else if (win_gravity == SouthWestGravity)
1342 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1343 else if (win_gravity == SouthGravity)
1344 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1345 else if (win_gravity == SouthEastGravity)
1346 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1347 else if (win_gravity == StaticGravity)
1348 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1349
22aa44a8
JD
1350 if (user_position)
1351 {
1352 hint_flags &= ~GDK_HINT_POS;
1353 hint_flags |= GDK_HINT_USER_POS;
1354 }
1355
3e5fc571
CY
1356 if (hint_flags != f->output_data.x->hint_flags
1357 || memcmp (&size_hints,
1358 &f->output_data.x->size_hints,
1359 sizeof (size_hints)) != 0)
1360 {
4d7e6e51 1361 block_input ();
3e5fc571 1362 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
c7e73be5 1363 NULL, &size_hints, hint_flags);
3e5fc571
CY
1364 f->output_data.x->size_hints = size_hints;
1365 f->output_data.x->hint_flags = hint_flags;
4d7e6e51 1366 unblock_input ();
3e5fc571 1367 }
f392e843
JD
1368}
1369
1370/* Change background color of a frame.
6772c8e1 1371 Since GTK uses the background color to clear the window, we must
f392e843
JD
1372 keep the GTK and X colors in sync.
1373 F is the frame to change,
1374 BG is the pixel value to change to. */
71bacd48 1375
f392e843 1376void
2d9783e0 1377xg_set_background_color (struct frame *f, unsigned long bg)
f392e843
JD
1378{
1379 if (FRAME_GTK_WIDGET (f))
1380 {
4d7e6e51 1381 block_input ();
0afb4571 1382 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
4d7e6e51 1383 unblock_input ();
f392e843
JD
1384 }
1385}
1386
1387
1001243b
JD
1388/* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1389 functions so GTK does not overwrite the icon. */
1390
1391void
a10c8269 1392xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1001243b 1393{
0afb4571
J
1394 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1395 icon_pixmap,
1396 icon_mask);
1397 if (gp)
1001243b
JD
1398 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1399}
1400
1401
f392e843
JD
1402\f
1403/***********************************************************************
1404 Dialog functions
1405 ***********************************************************************/
1406/* Return the dialog title to use for a dialog of type KEY.
1407 This is the encoding used by lwlib. We use the same for GTK. */
71bacd48 1408
8ea90aa3 1409static const char *
f392e843
JD
1410get_dialog_title (char key)
1411{
8ea90aa3 1412 const char *title = "";
177c0ea7 1413
f392e843
JD
1414 switch (key) {
1415 case 'E': case 'e':
1416 title = "Error";
1417 break;
1418
1419 case 'I': case 'i':
1420 title = "Information";
1421 break;
1422
1423 case 'L': case 'l':
1424 title = "Prompt";
1425 break;
1426
1427 case 'P': case 'p':
1428 title = "Prompt";
1429 break;
1430
1431 case 'Q': case 'q':
1432 title = "Question";
1433 break;
1434 }
1435
1436 return title;
1437}
1438
1439/* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1440 the dialog, but return TRUE so the event does not propagate further
1441 in GTK. This prevents GTK from destroying the dialog widget automatically
4c36be58 1442 and we can always destroy the widget manually, regardless of how
f392e843
JD
1443 it was popped down (button press or WM_DELETE_WINDOW).
1444 W is the dialog widget.
1445 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1446 user_data is NULL (not used).
1447
1448 Returns TRUE to end propagation of event. */
71bacd48 1449
f392e843 1450static gboolean
971de7fb 1451dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
f392e843
JD
1452{
1453 gtk_widget_unmap (w);
1454 return TRUE;
1455}
1456
1457/* Create a popup dialog window. See also xg_create_widget below.
1458 WV is a widget_value describing the dialog.
1459 SELECT_CB is the callback to use when a button has been pressed.
1460 DEACTIVATE_CB is the callback to use when the dialog pops down.
1461
1462 Returns the GTK dialog widget. */
71bacd48 1463
f392e843 1464static GtkWidget *
e4c8d29a
J
1465create_dialog (widget_value *wv,
1466 GCallback select_cb,
1467 GCallback deactivate_cb)
f392e843 1468{
8ea90aa3 1469 const char *title = get_dialog_title (wv->name[0]);
f392e843
JD
1470 int total_buttons = wv->name[1] - '0';
1471 int right_buttons = wv->name[4] - '0';
1472 int left_buttons;
1473 int button_nr = 0;
1474 int button_spacing = 10;
1475 GtkWidget *wdialog = gtk_dialog_new ();
e547b051
J
1476 GtkDialog *wd = GTK_DIALOG (wdialog);
1477 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
f392e843 1478 widget_value *item;
f392e843
JD
1479 GtkWidget *whbox_down;
1480
1481 /* If the number of buttons is greater than 4, make two rows of buttons
1482 instead. This looks better. */
18e27ea8 1483 bool make_two_rows = total_buttons > 4;
f392e843
JD
1484
1485 if (right_buttons == 0) right_buttons = total_buttons/2;
1486 left_buttons = total_buttons - right_buttons;
1487
1488 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1489 gtk_widget_set_name (wdialog, "emacs-dialog");
1490
f392e843
JD
1491
1492 if (make_two_rows)
1493 {
383b7c95
JD
1494 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1495 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1496 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1497 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1498 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1499 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
f392e843
JD
1500
1501 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1502 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1503 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1504
1505 cur_box = GTK_BOX (whbox_up);
1506 }
1507
1508 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1509 G_CALLBACK (dialog_delete_callback), 0);
177c0ea7 1510
f392e843
JD
1511 if (deactivate_cb)
1512 {
1513 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1514 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1515 }
1516
1517 for (item = wv->contents; item; item = item->next)
1518 {
1519 char *utf8_label = get_utf8_string (item->value);
1520 GtkWidget *w;
1521 GtkRequisition req;
1522
0a1d6de0 1523 if (item->name && strcmp (item->name, "message") == 0)
f392e843 1524 {
e547b051 1525 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
f392e843
JD
1526 /* This is the text part of the dialog. */
1527 w = gtk_label_new (utf8_label);
e547b051
J
1528 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1529 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
f392e843
JD
1530 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1531
1532 /* Try to make dialog look better. Must realize first so
1533 the widget can calculate the size it needs. */
1534 gtk_widget_realize (w);
0afb4571 1535 gtk_widget_get_preferred_size (w, NULL, &req);
e547b051 1536 gtk_box_set_spacing (wvbox, req.height);
0a1d6de0 1537 if (item->value && strlen (item->value) > 0)
f392e843
JD
1538 button_spacing = 2*req.width/strlen (item->value);
1539 }
1540 else
1541 {
1542 /* This is one button to add to the dialog. */
4b1b4443 1543 w = gtk_button_new_with_label (utf8_label);
f392e843
JD
1544 if (! item->enabled)
1545 gtk_widget_set_sensitive (w, FALSE);
1546 if (select_cb)
1547 g_signal_connect (G_OBJECT (w), "clicked",
1548 select_cb, item->call_data);
1549
1550 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1551 if (++button_nr == left_buttons)
1552 {
1553 if (make_two_rows)
1554 cur_box = GTK_BOX (whbox_down);
1555 else
1556 gtk_box_pack_start (cur_box,
1557 gtk_label_new (""),
1558 TRUE, TRUE,
1559 button_spacing);
1560 }
1561 }
1562
42ca4633 1563 if (utf8_label)
f392e843
JD
1564 g_free (utf8_label);
1565 }
1566
1567 return wdialog;
1568}
1569
e90292a9 1570struct xg_dialog_data
90f2e16b 1571{
e90292a9
JD
1572 GMainLoop *loop;
1573 int response;
1574 GtkWidget *w;
1575 guint timerid;
1576};
90f2e16b 1577
3402c0a6 1578/* Function that is called when the file or font dialogs pop down.
457a8155 1579 W is the dialog widget, RESPONSE is the response code.
e90292a9 1580 USER_DATA is what we passed in to g_signal_connect. */
457a8155
JD
1581
1582static void
dd4c5104
DN
1583xg_dialog_response_cb (GtkDialog *w,
1584 gint response,
1585 gpointer user_data)
457a8155 1586{
7d652d97 1587 struct xg_dialog_data *dd = user_data;
e90292a9
JD
1588 dd->response = response;
1589 g_main_loop_quit (dd->loop);
457a8155
JD
1590}
1591
1592
1593/* Destroy the dialog. This makes it pop down. */
1594
27e498e6
PE
1595static void
1596pop_down_dialog (void *arg)
457a8155 1597{
27e498e6 1598 struct xg_dialog_data *dd = arg;
e90292a9 1599
4d7e6e51 1600 block_input ();
e90292a9
JD
1601 if (dd->w) gtk_widget_destroy (dd->w);
1602 if (dd->timerid != 0) g_source_remove (dd->timerid);
1603
1604 g_main_loop_quit (dd->loop);
1605 g_main_loop_unref (dd->loop);
088c8c09 1606
4d7e6e51 1607 unblock_input ();
457a8155
JD
1608}
1609
e90292a9
JD
1610/* If there are any emacs timers pending, add a timeout to main loop in DATA.
1611 We pass in DATA as gpointer* so we can use this as a callback. */
1612
1613static gboolean
971de7fb 1614xg_maybe_add_timer (gpointer data)
e90292a9 1615{
7d652d97 1616 struct xg_dialog_data *dd = data;
43aac990 1617 struct timespec next_time = timer_check ();
e90292a9
JD
1618
1619 dd->timerid = 0;
1620
43aac990 1621 if (timespec_valid_p (next_time))
e90292a9 1622 {
43aac990
PE
1623 time_t s = next_time.tv_sec;
1624 int per_ms = TIMESPEC_RESOLUTION / 1000;
1625 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
d35af63c
PE
1626 if (s <= ((guint) -1 - ms) / 1000)
1627 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
e90292a9
JD
1628 }
1629 return FALSE;
1630}
1631
088c8c09 1632
e90292a9
JD
1633/* Pops up a modal dialog W and waits for response.
1634 We don't use gtk_dialog_run because we want to process emacs timers.
1635 The dialog W is not destroyed when this function returns. */
1636
1637static int
a10c8269 1638xg_dialog_run (struct frame *f, GtkWidget *w)
e90292a9 1639{
d311d28c 1640 ptrdiff_t count = SPECPDL_INDEX ();
e90292a9
JD
1641 struct xg_dialog_data dd;
1642
1643 xg_set_screen (w, f);
1644 gtk_window_set_transient_for (GTK_WINDOW (w),
1645 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1646 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1647 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1648
1649 dd.loop = g_main_loop_new (NULL, FALSE);
1650 dd.response = GTK_RESPONSE_CANCEL;
1651 dd.w = w;
1652 dd.timerid = 0;
1653
1654 g_signal_connect (G_OBJECT (w),
1655 "response",
1656 G_CALLBACK (xg_dialog_response_cb),
1657 &dd);
1658 /* Don't destroy the widget if closed by the window manager close button. */
1659 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1660 gtk_widget_show (w);
1661
27e498e6 1662 record_unwind_protect_ptr (pop_down_dialog, &dd);
e90292a9
JD
1663
1664 (void) xg_maybe_add_timer (&dd);
1665 g_main_loop_run (dd.loop);
088c8c09 1666
e90292a9
JD
1667 dd.w = 0;
1668 unbind_to (count, Qnil);
1669
1670 return dd.response;
1671}
1672
1673\f
1674/***********************************************************************
1675 File dialog functions
1676 ***********************************************************************/
18e27ea8 1677/* Return true if the old file selection dialog is being used. */
e90292a9 1678
18e27ea8 1679bool
971de7fb 1680xg_uses_old_file_dialog (void)
e90292a9 1681{
e547b051 1682#ifdef HAVE_GTK_FILE_SELECTION_NEW
e90292a9 1683 return x_gtk_use_old_file_dialog;
e90292a9
JD
1684#else
1685 return 0;
1686#endif
e90292a9
JD
1687}
1688
1689
f57e2426 1690typedef char * (*xg_get_file_func) (GtkWidget *);
f9d64bb3 1691
457a8155
JD
1692/* Return the selected file for file chooser dialog W.
1693 The returned string must be free:d. */
1694
1695static char *
971de7fb 1696xg_get_file_name_from_chooser (GtkWidget *w)
457a8155
JD
1697{
1698 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1699}
1700
4c9ca1a3
JD
1701/* Callback called when the "Show hidden files" toggle is pressed.
1702 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1703
78c55a80 1704static void
971de7fb 1705xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
78c55a80
JD
1706{
1707 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1708 gboolean visible;
78c55a80
JD
1709 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1710 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
4c9ca1a3
JD
1711}
1712
1713
1714/* Callback called when a property changes in a file chooser.
1715 GOBJECT is the file chooser dialog, ARG1 describes the property.
1716 USER_DATA is the toggle widget in the file chooser dialog.
1717 We use this to update the "Show hidden files" toggle when the user
1718 changes that property by right clicking in the file list. */
1719
1720static void
971de7fb 1721xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
4c9ca1a3 1722{
4c9ca1a3
JD
1723 if (strcmp (arg1->name, "show-hidden") == 0)
1724 {
4c9ca1a3
JD
1725 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1726 gboolean visible, toggle_on;
1727
1728 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1729 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1730
1731 if (!!visible != !!toggle_on)
1732 {
1733 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1734 G_CALLBACK (xg_toggle_visibility_cb),
1735 gobject);
1736 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1737 g_signal_handlers_unblock_by_func
1738 (G_OBJECT (wtoggle),
1739 G_CALLBACK (xg_toggle_visibility_cb),
1740 gobject);
1741 }
1742 x_gtk_show_hidden_files = visible;
1743 }
78c55a80
JD
1744}
1745
f9d64bb3
JD
1746/* Read a file name from the user using a file chooser dialog.
1747 F is the current frame.
1748 PROMPT is a prompt to show to the user. May not be NULL.
1749 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
18e27ea8 1750 If MUSTMATCH_P, the returned file name must be an existing
2605051a
GM
1751 file. (Actually, this only has cosmetic effects, the user can
1752 still enter a non-existing file.) *FUNC is set to a function that
1753 can be used to retrieve the selected file name from the returned widget.
f9d64bb3 1754
457a8155 1755 Returns the created widget. */
f9d64bb3 1756
457a8155 1757static GtkWidget *
a10c8269 1758xg_get_file_with_chooser (struct frame *f,
dd4c5104
DN
1759 char *prompt,
1760 char *default_filename,
18e27ea8 1761 bool mustmatch_p, bool only_dir_p,
dd4c5104 1762 xg_get_file_func *func)
f9d64bb3 1763{
65dc836c 1764 char msgbuf[1024];
78c55a80 1765
a60e5f68 1766 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
f9d64bb3 1767 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
f9d64bb3
JD
1768 GtkFileChooserAction action = (mustmatch_p ?
1769 GTK_FILE_CHOOSER_ACTION_OPEN :
1770 GTK_FILE_CHOOSER_ACTION_SAVE);
1771
1772 if (only_dir_p)
1773 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1774
1775 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1776 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1777 (mustmatch_p || only_dir_p ?
8cfd0f36 1778 GTK_STOCK_OPEN : GTK_STOCK_OK),
f9d64bb3
JD
1779 GTK_RESPONSE_OK,
1780 NULL);
ded997c1 1781 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
f9d64bb3 1782
383b7c95
JD
1783 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1784 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
78c55a80
JD
1785 gtk_widget_show (wbox);
1786 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
8e5a8840
JB
1787
1788 if (x_gtk_show_hidden_files)
78c55a80
JD
1789 {
1790 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1791 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1792 }
1793 gtk_widget_show (wtoggle);
1794 g_signal_connect (G_OBJECT (wtoggle), "clicked",
4c9ca1a3
JD
1795 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1796 g_signal_connect (G_OBJECT (filewin), "notify",
1797 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
78c55a80 1798
a10fe834 1799 if (x_gtk_file_dialog_help_text)
cf1e295f 1800 {
65dc836c 1801 msgbuf[0] = '\0';
7b7d4a79
JD
1802 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1803 Show the C-l help text only for versions < 2.10. */
7adfb96e 1804 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
65dc836c
PE
1805 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1806 strcat (msgbuf, "\nIf you don't like this file selector, use the "
cf1e295f
JD
1807 "corresponding\nkey binding or customize "
1808 "use-file-dialog to turn it off.");
8e5a8840 1809
65dc836c 1810 wmessage = gtk_label_new (msgbuf);
cf1e295f
JD
1811 gtk_widget_show (wmessage);
1812 }
1813
78c55a80 1814 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
a10fe834 1815 if (x_gtk_file_dialog_help_text)
cf1e295f 1816 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
78c55a80
JD
1817 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1818
f9d64bb3 1819 if (default_filename)
a872928c
JD
1820 {
1821 Lisp_Object file;
1822 struct gcpro gcpro1;
4f3097d8 1823 char *utf8_filename;
45c94881 1824 GCPRO1 (file);
a872928c 1825
ded997c1
JD
1826 file = build_string (default_filename);
1827
a872928c
JD
1828 /* File chooser does not understand ~/... in the file name. It must be
1829 an absolute name starting with /. */
1830 if (default_filename[0] != '/')
ded997c1 1831 file = Fexpand_file_name (file, Qnil);
8e5a8840 1832
4f3097d8
JD
1833 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1834 if (! NILP (Ffile_directory_p (file)))
ded997c1 1835 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
4f3097d8 1836 utf8_filename);
ded997c1 1837 else
4f3097d8 1838 {
4f3097d8
JD
1839 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1840 utf8_filename);
ae6c1c19
JD
1841 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1842 {
1843 char *cp = strrchr (utf8_filename, '/');
1844 if (cp) ++cp;
1845 else cp = utf8_filename;
1846 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1847 }
4f3097d8 1848 }
a872928c
JD
1849
1850 UNGCPRO;
1851 }
f9d64bb3 1852
457a8155
JD
1853 *func = xg_get_file_name_from_chooser;
1854 return filewin;
f9d64bb3 1855}
f9d64bb3
JD
1856
1857#ifdef HAVE_GTK_FILE_SELECTION_NEW
f392e843 1858
457a8155
JD
1859/* Return the selected file for file selector dialog W.
1860 The returned string must be free:d. */
71bacd48 1861
457a8155 1862static char *
971de7fb 1863xg_get_file_name_from_selector (GtkWidget *w)
f392e843 1864{
457a8155 1865 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
7d652d97 1866 return xstrdup (gtk_file_selection_get_filename (filesel));
f392e843
JD
1867}
1868
457a8155 1869/* Create a file selection dialog.
f392e843
JD
1870 F is the current frame.
1871 PROMPT is a prompt to show to the user. May not be NULL.
1872 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
18e27ea8 1873 If MUSTMATCH_P, the returned file name must be an existing
457a8155
JD
1874 file. *FUNC is set to a function that can be used to retrieve the
1875 selected file name from the returned widget.
f392e843 1876
457a8155 1877 Returns the created widget. */
71bacd48 1878
457a8155 1879static GtkWidget *
a10c8269 1880xg_get_file_with_selection (struct frame *f,
e4c8d29a
J
1881 char *prompt,
1882 char *default_filename,
18e27ea8 1883 bool mustmatch_p, bool only_dir_p,
e4c8d29a 1884 xg_get_file_func *func)
f392e843
JD
1885{
1886 GtkWidget *filewin;
1887 GtkFileSelection *filesel;
177c0ea7 1888
f392e843
JD
1889 filewin = gtk_file_selection_new (prompt);
1890 filesel = GTK_FILE_SELECTION (filewin);
1891
f392e843
JD
1892 if (default_filename)
1893 gtk_file_selection_set_filename (filesel, default_filename);
1894
1895 if (mustmatch_p)
1896 {
1897 /* The selection_entry part of filesel is not documented. */
1898 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1899 gtk_file_selection_hide_fileop_buttons (filesel);
1900 }
1901
457a8155 1902 *func = xg_get_file_name_from_selector;
177c0ea7 1903
457a8155 1904 return filewin;
f392e843 1905}
f9d64bb3
JD
1906#endif /* HAVE_GTK_FILE_SELECTION_NEW */
1907
1908/* Read a file name from the user using a file dialog, either the old
1909 file selection dialog, or the new file chooser dialog. Which to use
1910 depends on what the GTK version used has, and what the value of
1911 gtk-use-old-file-dialog.
1912 F is the current frame.
1913 PROMPT is a prompt to show to the user. May not be NULL.
1914 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
18e27ea8 1915 If MUSTMATCH_P, the returned file name must be an existing
f9d64bb3
JD
1916 file.
1917
1918 Returns a file name or NULL if no file was selected.
1919 The returned string must be freed by the caller. */
1920
1921char *
a10c8269 1922xg_get_file_name (struct frame *f,
e4c8d29a
J
1923 char *prompt,
1924 char *default_filename,
18e27ea8
PE
1925 bool mustmatch_p,
1926 bool only_dir_p)
f9d64bb3 1927{
724cde0d 1928 GtkWidget *w = 0;
457a8155
JD
1929 char *fn = 0;
1930 int filesel_done = 0;
1931 xg_get_file_func func;
1932
e547b051 1933#ifdef HAVE_GTK_FILE_SELECTION_NEW
255e4140 1934
90f2e16b 1935 if (xg_uses_old_file_dialog ())
457a8155
JD
1936 w = xg_get_file_with_selection (f, prompt, default_filename,
1937 mustmatch_p, only_dir_p, &func);
1938 else
1939 w = xg_get_file_with_chooser (f, prompt, default_filename,
1940 mustmatch_p, only_dir_p, &func);
f9d64bb3 1941
e547b051 1942#else /* not HAVE_GTK_FILE_SELECTION_NEW */
457a8155
JD
1943 w = xg_get_file_with_chooser (f, prompt, default_filename,
1944 mustmatch_p, only_dir_p, &func);
e547b051 1945#endif /* not HAVE_GTK_FILE_SELECTION_NEW */
457a8155 1946
457a8155 1947 gtk_widget_set_name (w, "emacs-filedialog");
457a8155 1948
e90292a9 1949 filesel_done = xg_dialog_run (f, w);
457a8155
JD
1950 if (filesel_done == GTK_RESPONSE_OK)
1951 fn = (*func) (w);
1952
e90292a9 1953 gtk_widget_destroy (w);
457a8155 1954 return fn;
f9d64bb3 1955}
f392e843 1956
f2045622
CY
1957/***********************************************************************
1958 GTK font chooser
1959 ***********************************************************************/
1960
3402c0a6 1961#ifdef HAVE_FREETYPE
f2045622
CY
1962
1963#if USE_NEW_GTK_FONT_CHOOSER
1964
f2045622
CY
1965#define XG_WEIGHT_TO_SYMBOL(w) \
1966 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
1967 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
1968 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
1969 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
1970 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
1971 : w <= PANGO_WEIGHT_BOLD ? Qbold \
1972 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
1973 : Qultra_bold)
1974
1975#define XG_STYLE_TO_SYMBOL(s) \
1976 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
1977 : s == PANGO_STYLE_ITALIC ? Qitalic \
1978 : Qnormal)
1979
1980#endif /* USE_NEW_GTK_FONT_CHOOSER */
1981
1982
1983static char *x_last_font_name;
1984
3402c0a6
CY
1985/* Pop up a GTK font selector and return the name of the font the user
1986 selects, as a C string. The returned font name follows GTK's own
1987 format:
1988
1989 `FAMILY [VALUE1 VALUE2] SIZE'
1990
1991 This can be parsed using font_parse_fcname in font.c.
1992 DEFAULT_NAME, if non-zero, is the default font name. */
1993
f2045622 1994Lisp_Object
a10c8269 1995xg_get_font (struct frame *f, const char *default_name)
3402c0a6 1996{
e90292a9 1997 GtkWidget *w;
3402c0a6 1998 int done = 0;
f2045622 1999 Lisp_Object font = Qnil;
3402c0a6 2000
32bcadb4
JD
2001 w = gtk_font_chooser_dialog_new
2002 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2003
f2045622
CY
2004 if (default_name)
2005 {
2006 /* Convert fontconfig names to Gtk names, i.e. remove - before
2007 number */
2008 char *p = strrchr (default_name, '-');
2009 if (p)
2010 {
2011 char *ep = p+1;
620f13b0 2012 while (c_isdigit (*ep))
f2045622
CY
2013 ++ep;
2014 if (*ep == '\0') *p = ' ';
2015 }
2016 }
2017 else if (x_last_font_name)
2018 default_name = x_last_font_name;
3402c0a6 2019
f2045622
CY
2020 if (default_name)
2021 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
3402c0a6 2022
f2045622 2023 gtk_widget_set_name (w, "emacs-fontdialog");
e90292a9 2024 done = xg_dialog_run (f, w);
3402c0a6 2025 if (done == GTK_RESPONSE_OK)
f2045622
CY
2026 {
2027#if USE_NEW_GTK_FONT_CHOOSER
2028 /* Use the GTK3 font chooser. */
2029 PangoFontDescription *desc
2030 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2031
2032 if (desc)
2033 {
a2d19368 2034 Lisp_Object args[10];
f2045622 2035 const char *name = pango_font_description_get_family (desc);
5bf192ca 2036 gint size = pango_font_description_get_size (desc);
f2045622 2037 PangoWeight weight = pango_font_description_get_weight (desc);
5bf192ca 2038 PangoStyle style = pango_font_description_get_style (desc);
f2045622
CY
2039
2040 args[0] = QCname;
2041 args[1] = build_string (name);
2042
2043 args[2] = QCsize;
5bf192ca 2044 args[3] = make_float (pango_units_to_double (size));
f2045622
CY
2045
2046 args[4] = QCweight;
2047 args[5] = XG_WEIGHT_TO_SYMBOL (weight);
2048
2049 args[6] = QCslant;
2050 args[7] = XG_STYLE_TO_SYMBOL (style);
2051
a2d19368
CY
2052 args[8] = QCtype;
2053 args[9] = Qxft;
2054
f2045622
CY
2055 font = Ffont_spec (8, args);
2056
2057 pango_font_description_free (desc);
8268febf 2058 dupstring (&x_last_font_name, name);
f2045622
CY
2059 }
2060
2061#else /* Use old font selector, which just returns the font name. */
2062
2063 char *font_name
2064 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2065
2066 if (font_name)
2067 {
2068 font = build_string (font_name);
2069 g_free (x_last_font_name);
2070 x_last_font_name = font_name;
2071 }
2072#endif /* USE_NEW_GTK_FONT_CHOOSER */
2073 }
3402c0a6 2074
e90292a9 2075 gtk_widget_destroy (w);
f2045622 2076 return font;
3402c0a6
CY
2077}
2078#endif /* HAVE_FREETYPE */
2079
2080
f392e843
JD
2081\f
2082/***********************************************************************
2083 Menu functions.
2084 ***********************************************************************/
2085
d1c38b57 2086/* The name of menu items that can be used for customization. Since GTK
f392e843 2087 RC files are very crude and primitive, we have to set this on all
d1c38b57 2088 menu item names so a user can easily customize menu items. */
f392e843
JD
2089
2090#define MENU_ITEM_NAME "emacs-menuitem"
2091
2092
2093/* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2094 during GC. The next member points to the items. */
2095static xg_list_node xg_menu_cb_list;
2096
2097/* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2098 during GC. The next member points to the items. */
2099static xg_list_node xg_menu_item_cb_list;
2100
2101/* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2102 F is the frame CL_DATA will be initialized for.
2103 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2104
2105 The menu bar and all sub menus under the menu bar in a frame
2106 share the same structure, hence the reference count.
177c0ea7 2107
f392e843
JD
2108 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2109 allocated xg_menu_cb_data if CL_DATA is NULL. */
71bacd48 2110
f392e843 2111static xg_menu_cb_data *
a10c8269 2112make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
f392e843
JD
2113{
2114 if (! cl_data)
2115 {
38182d90 2116 cl_data = xmalloc (sizeof *cl_data);
f392e843 2117 cl_data->f = f;
e69b0960 2118 cl_data->menu_bar_vector = f->menu_bar_vector;
f392e843
JD
2119 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2120 cl_data->highlight_cb = highlight_cb;
2121 cl_data->ref_count = 0;
2122
2123 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2124 }
2125
2126 cl_data->ref_count++;
2127
2128 return cl_data;
2129}
2130
2131/* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2132 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2133
2134 When the menu bar is updated, menu items may have been added and/or
2135 removed, so menu_bar_vector and menu_bar_items_used change. We must
2136 then update CL_DATA since it is used to determine which menu
2137 item that is invoked in the menu.
2138 HIGHLIGHT_CB could change, there is no check that the same
2139 function is given when modifying a menu bar as was given when
2140 creating the menu bar. */
71bacd48 2141
f392e843 2142static void
e4c8d29a 2143update_cl_data (xg_menu_cb_data *cl_data,
a10c8269 2144 struct frame *f,
e4c8d29a 2145 GCallback highlight_cb)
f392e843
JD
2146{
2147 if (cl_data)
2148 {
2149 cl_data->f = f;
e69b0960 2150 cl_data->menu_bar_vector = f->menu_bar_vector;
f392e843
JD
2151 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2152 cl_data->highlight_cb = highlight_cb;
2153 }
2154}
2155
2156/* Decrease reference count for CL_DATA.
2157 If reference count is zero, free CL_DATA. */
71bacd48 2158
f392e843 2159static void
971de7fb 2160unref_cl_data (xg_menu_cb_data *cl_data)
f392e843
JD
2161{
2162 if (cl_data && cl_data->ref_count > 0)
2163 {
2164 cl_data->ref_count--;
2165 if (cl_data->ref_count == 0)
2166 {
2167 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2168 xfree (cl_data);
2169 }
2170 }
2171}
2172
2173/* Function that marks all lisp data during GC. */
71bacd48 2174
f392e843 2175void
971de7fb 2176xg_mark_data (void)
f392e843
JD
2177{
2178 xg_list_node *iter;
5884c324 2179 Lisp_Object rest, frame;
f392e843
JD
2180
2181 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
631f2082 2182 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
f392e843
JD
2183
2184 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2185 {
2186 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2187
2188 if (! NILP (cb_data->help))
631f2082 2189 mark_object (cb_data->help);
f392e843 2190 }
ca06f160 2191
ca06f160
JD
2192 FOR_EACH_FRAME (rest, frame)
2193 {
a10c8269 2194 struct frame *f = XFRAME (frame);
ca06f160 2195
ac4845a6 2196 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
ca06f160
JD
2197 {
2198 struct xg_frame_tb_info *tbinfo
2199 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2200 TB_INFO_KEY);
2201 if (tbinfo)
2202 {
2203 mark_object (tbinfo->last_tool_bar);
2204 mark_object (tbinfo->style);
2205 }
2206 }
2207 }
f392e843
JD
2208}
2209
2210
2211/* Callback called when a menu item is destroyed. Used to free data.
2212 W is the widget that is being destroyed (not used).
2213 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
71bacd48 2214
f392e843 2215static void
971de7fb 2216menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
f392e843
JD
2217{
2218 if (client_data)
2219 {
7d652d97 2220 xg_menu_item_cb_data *data = client_data;
f392e843
JD
2221 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2222 xfree (data);
2223 }
2224}
2225
2226/* Callback called when the pointer enters/leaves a menu item.
665c8f1c 2227 W is the parent of the menu item.
f392e843 2228 EVENT is either an enter event or leave event.
665c8f1c 2229 CLIENT_DATA is not used.
f392e843
JD
2230
2231 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 2232
f392e843 2233static gboolean
e4c8d29a
J
2234menuitem_highlight_callback (GtkWidget *w,
2235 GdkEventCrossing *event,
2236 gpointer client_data)
f392e843 2237{
665c8f1c
JD
2238 GdkEvent ev;
2239 GtkWidget *subwidget;
2240 xg_menu_item_cb_data *data;
177c0ea7 2241
665c8f1c
JD
2242 ev.crossing = *event;
2243 subwidget = gtk_get_event_widget (&ev);
7d652d97 2244 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
665c8f1c
JD
2245 if (data)
2246 {
f392e843
JD
2247 if (! NILP (data->help) && data->cl_data->highlight_cb)
2248 {
665c8f1c 2249 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
f392e843 2250 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
665c8f1c 2251 (*func) (subwidget, call_data);
f392e843
JD
2252 }
2253 }
2254
2255 return FALSE;
2256}
2257
2258/* Callback called when a menu is destroyed. Used to free data.
2259 W is the widget that is being destroyed (not used).
2260 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
71bacd48 2261
f392e843 2262static void
971de7fb 2263menu_destroy_callback (GtkWidget *w, gpointer client_data)
f392e843 2264{
7d652d97 2265 unref_cl_data (client_data);
f392e843
JD
2266}
2267
f392e843
JD
2268/* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2269 must be non-NULL) and can be inserted into a menu item.
2270
2271 Returns the GtkHBox. */
71bacd48 2272
f392e843 2273static GtkWidget *
8ea90aa3 2274make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
f392e843
JD
2275{
2276 GtkWidget *wlbl;
2277 GtkWidget *wkey;
2278 GtkWidget *wbox;
177c0ea7 2279
383b7c95
JD
2280 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2281 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
4b1b4443 2282 wlbl = gtk_label_new (utf8_label);
f392e843
JD
2283 wkey = gtk_label_new (utf8_key);
2284
2285 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2286 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
177c0ea7 2287
f392e843
JD
2288 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2289 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2290
2291 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2292 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
7863d625 2293 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
f392e843
JD
2294
2295 return wbox;
2296}
2297
2298/* Make and return a menu item widget with the key to the right.
2299 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2300 UTF8_KEY is the text representing the key binding.
2301 ITEM is the widget_value describing the menu item.
177c0ea7 2302
f392e843
JD
2303 GROUP is an in/out parameter. If the menu item to be created is not
2304 part of any radio menu group, *GROUP contains NULL on entry and exit.
2305 If the menu item to be created is part of a radio menu group, on entry
2306 *GROUP contains the group to use, or NULL if this is the first item
2307 in the group. On exit, *GROUP contains the radio item group.
2308
2309 Unfortunately, keys don't line up as nicely as in Motif,
2310 but the MacOS X version doesn't either, so I guess that is OK. */
71bacd48 2311
f392e843 2312static GtkWidget *
8ea90aa3
DN
2313make_menu_item (const char *utf8_label,
2314 const char *utf8_key,
e4c8d29a
J
2315 widget_value *item,
2316 GSList **group)
f392e843
JD
2317{
2318 GtkWidget *w;
2319 GtkWidget *wtoadd = 0;
177c0ea7 2320
adcb132c
JD
2321 /* It has been observed that some menu items have a NULL name field.
2322 This will lead to this function being called with a NULL utf8_label.
2323 GTK crashes on that so we set a blank label. Why there is a NULL
2324 name remains to be investigated. */
2325 if (! utf8_label) utf8_label = " ";
2326
f392e843
JD
2327 if (utf8_key)
2328 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
177c0ea7 2329
f392e843
JD
2330 if (item->button_type == BUTTON_TYPE_TOGGLE)
2331 {
2332 *group = NULL;
2333 if (utf8_key) w = gtk_check_menu_item_new ();
4b1b4443 2334 else w = gtk_check_menu_item_new_with_label (utf8_label);
f392e843
JD
2335 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2336 }
2337 else if (item->button_type == BUTTON_TYPE_RADIO)
2338 {
2339 if (utf8_key) w = gtk_radio_menu_item_new (*group);
4b1b4443 2340 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
f392e843
JD
2341 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2342 if (item->selected)
2343 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2344 }
2345 else
2346 {
2347 *group = NULL;
2348 if (utf8_key) w = gtk_menu_item_new ();
4b1b4443 2349 else w = gtk_menu_item_new_with_label (utf8_label);
f392e843 2350 }
177c0ea7 2351
f392e843
JD
2352 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2353 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2354
2355 return w;
2356}
2357
8b745d92
JD
2358#ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
2359
da18b5ac
JD
2360static int xg_detached_menus;
2361
18e27ea8 2362/* Return true if there are detached menus. */
71bacd48 2363
18e27ea8 2364bool
7a1b473c 2365xg_have_tear_offs (struct frame *f)
da18b5ac 2366{
7a1b473c
DC
2367 /* If the frame's menubar height is zero, the menu bar is probably
2368 being redirected outside the window to some kind of global menu;
2369 this situation is the moral equivalent of a tear-off. */
2370 return FRAME_MENUBAR_HEIGHT (f) == 0 || xg_detached_menus > 0;
da18b5ac 2371}
f392e843
JD
2372
2373/* Callback invoked when a detached menu window is removed. Here we
da18b5ac 2374 decrease the xg_detached_menus count.
f392e843 2375 WIDGET is the top level window that is removed (the parent of the menu).
da18b5ac 2376 CLIENT_DATA is not used. */
71bacd48 2377
da18b5ac 2378static void
971de7fb 2379tearoff_remove (GtkWidget *widget, gpointer client_data)
f392e843 2380{
da18b5ac 2381 if (xg_detached_menus > 0) --xg_detached_menus;
f392e843
JD
2382}
2383
da18b5ac
JD
2384/* Callback invoked when a menu is detached. It increases the
2385 xg_detached_menus count.
f392e843 2386 WIDGET is the GtkTearoffMenuItem.
177c0ea7 2387 CLIENT_DATA is not used. */
71bacd48 2388
f392e843 2389static void
971de7fb 2390tearoff_activate (GtkWidget *widget, gpointer client_data)
f392e843
JD
2391{
2392 GtkWidget *menu = gtk_widget_get_parent (widget);
da18b5ac
JD
2393 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2394 {
2395 ++xg_detached_menus;
2396 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2397 "destroy",
2398 G_CALLBACK (tearoff_remove), 0);
2399 }
f392e843 2400}
8b745d92
JD
2401#else /* ! HAVE_GTK_TEAROFF_MENU_ITEM_NEW */
2402bool
7a1b473c 2403xg_have_tear_offs (struct frame *f)
8b745d92 2404{
7a1b473c 2405 return FRAME_MENUBAR_HEIGHT (f) == 0;
8b745d92
JD
2406}
2407#endif /* ! HAVE_GTK_TEAROFF_MENU_ITEM_NEW */
f392e843 2408
f392e843 2409/* Create a menu item widget, and connect the callbacks.
4c36be58 2410 ITEM describes the menu item.
f392e843
JD
2411 F is the frame the created menu belongs to.
2412 SELECT_CB is the callback to use when a menu item is selected.
2413 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2414 CL_DATA points to the callback data to be used for this menu.
2415 GROUP is an in/out parameter. If the menu item to be created is not
2416 part of any radio menu group, *GROUP contains NULL on entry and exit.
2417 If the menu item to be created is part of a radio menu group, on entry
2418 *GROUP contains the group to use, or NULL if this is the first item
2419 in the group. On exit, *GROUP contains the radio item group.
2420
2421 Returns the created GtkWidget. */
71bacd48 2422
f392e843 2423static GtkWidget *
e4c8d29a 2424xg_create_one_menuitem (widget_value *item,
a10c8269 2425 struct frame *f,
e4c8d29a
J
2426 GCallback select_cb,
2427 GCallback highlight_cb,
2428 xg_menu_cb_data *cl_data,
2429 GSList **group)
f392e843
JD
2430{
2431 char *utf8_label;
2432 char *utf8_key;
2433 GtkWidget *w;
2434 xg_menu_item_cb_data *cb_data;
2435
2436 utf8_label = get_utf8_string (item->name);
2437 utf8_key = get_utf8_string (item->key);
2438
2439 w = make_menu_item (utf8_label, utf8_key, item, group);
2440
42ca4633
J
2441 if (utf8_label) g_free (utf8_label);
2442 if (utf8_key) g_free (utf8_key);
f392e843 2443
38182d90 2444 cb_data = xmalloc (sizeof *cb_data);
f392e843
JD
2445
2446 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2447
665c8f1c 2448 cb_data->select_id = 0;
f392e843
JD
2449 cb_data->help = item->help;
2450 cb_data->cl_data = cl_data;
2451 cb_data->call_data = item->call_data;
177c0ea7 2452
f392e843
JD
2453 g_signal_connect (G_OBJECT (w),
2454 "destroy",
2455 G_CALLBACK (menuitem_destroy_callback),
2456 cb_data);
2457
2458 /* Put cb_data in widget, so we can get at it when modifying menubar */
2459 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2460
2461 /* final item, not a submenu */
2462 if (item->call_data && ! item->contents)
2463 {
2464 if (select_cb)
2465 cb_data->select_id
2466 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2467 }
2468
f392e843
JD
2469 return w;
2470}
2471
2472/* Create a full menu tree specified by DATA.
2473 F is the frame the created menu belongs to.
2474 SELECT_CB is the callback to use when a menu item is selected.
2475 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2476 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
18e27ea8
PE
2477 If POP_UP_P, create a popup menu.
2478 If MENU_BAR_P, create a menu bar.
8b745d92
JD
2479 If ADD_TEAROFF_P, add a tearoff menu item. Ignored if MENU_BAR_P or
2480 the Gtk+ version used does not have tearoffs.
f392e843
JD
2481 TOPMENU is the topmost GtkWidget that others shall be placed under.
2482 It may be NULL, in that case we create the appropriate widget
2483 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2484 CL_DATA is the callback data we shall use for this menu, or NULL
2485 if we haven't set the first callback yet.
2486 NAME is the name to give to the top level menu if this function
2487 creates it. May be NULL to not set any name.
2488
2489 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2490 not NULL.
2491
2492 This function calls itself to create submenus. */
2493
2494static GtkWidget *
e4c8d29a 2495create_menus (widget_value *data,
a10c8269 2496 struct frame *f,
e4c8d29a
J
2497 GCallback select_cb,
2498 GCallback deactivate_cb,
2499 GCallback highlight_cb,
18e27ea8
PE
2500 bool pop_up_p,
2501 bool menu_bar_p,
2502 bool add_tearoff_p,
e4c8d29a
J
2503 GtkWidget *topmenu,
2504 xg_menu_cb_data *cl_data,
8ea90aa3 2505 const char *name)
f392e843
JD
2506{
2507 widget_value *item;
2508 GtkWidget *wmenu = topmenu;
2509 GSList *group = NULL;
2510
2511 if (! topmenu)
2512 {
810f2256
JD
2513 if (! menu_bar_p)
2514 {
2515 wmenu = gtk_menu_new ();
2516 xg_set_screen (wmenu, f);
665c8f1c
JD
2517 /* Connect this to the menu instead of items so we get enter/leave for
2518 disabled items also. TODO: Still does not get enter/leave for
2519 disabled items in detached menus. */
2520 g_signal_connect (G_OBJECT (wmenu),
2521 "enter-notify-event",
2522 G_CALLBACK (menuitem_highlight_callback),
2523 NULL);
2524 g_signal_connect (G_OBJECT (wmenu),
2525 "leave-notify-event",
2526 G_CALLBACK (menuitem_highlight_callback),
2527 NULL);
810f2256 2528 }
5be883cd
JD
2529 else
2530 {
2531 wmenu = gtk_menu_bar_new ();
872870b2
JD
2532 /* Set width of menu bar to a small value so it doesn't enlarge
2533 a small initial frame size. The width will be set to the
2534 width of the frame later on when it is added to a container.
2535 height -1: Natural height. */
5be883cd
JD
2536 gtk_widget_set_size_request (wmenu, 1, -1);
2537 }
f392e843
JD
2538
2539 /* Put cl_data on the top menu for easier access. */
2540 cl_data = make_cl_data (cl_data, f, highlight_cb);
2541 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2542 g_signal_connect (G_OBJECT (wmenu), "destroy",
2543 G_CALLBACK (menu_destroy_callback), cl_data);
177c0ea7 2544
f392e843
JD
2545 if (name)
2546 gtk_widget_set_name (wmenu, name);
2547
2548 if (deactivate_cb)
2549 g_signal_connect (G_OBJECT (wmenu),
c8934d9d 2550 "selection-done", deactivate_cb, 0);
f392e843 2551 }
177c0ea7 2552
8b745d92 2553#ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
f392e843
JD
2554 if (! menu_bar_p && add_tearoff_p)
2555 {
2556 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2557 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2558
2559 g_signal_connect (G_OBJECT (tearoff), "activate",
2560 G_CALLBACK (tearoff_activate), 0);
2561 }
8b745d92 2562#endif
f392e843
JD
2563
2564 for (item = data; item; item = item->next)
2565 {
2566 GtkWidget *w;
177c0ea7 2567
f392e843 2568 if (pop_up_p && !item->contents && !item->call_data
4039c786 2569 && !menu_separator_name_p (item->name))
f392e843
JD
2570 {
2571 char *utf8_label;
2572 /* A title for a popup. We do the same as GTK does when
2573 creating titles, but it does not look good. */
2574 group = NULL;
2575 utf8_label = get_utf8_string (item->name);
2576
2577 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
4b1b4443 2578 w = gtk_menu_item_new_with_label (utf8_label);
f392e843 2579 gtk_widget_set_sensitive (w, FALSE);
42ca4633 2580 if (utf8_label) g_free (utf8_label);
f392e843 2581 }
4039c786 2582 else if (menu_separator_name_p (item->name))
f392e843
JD
2583 {
2584 group = NULL;
2585 /* GTK only have one separator type. */
2586 w = gtk_separator_menu_item_new ();
2587 }
2588 else
2589 {
2590 w = xg_create_one_menuitem (item,
2591 f,
2592 item->contents ? 0 : select_cb,
2593 highlight_cb,
2594 cl_data,
2595 &group);
2596
f56cff88
JD
2597 /* Create a possibly empty submenu for menu bar items, since some
2598 themes don't highlight items correctly without it. */
2599 if (item->contents || menu_bar_p)
f392e843
JD
2600 {
2601 GtkWidget *submenu = create_menus (item->contents,
2602 f,
2603 select_cb,
2604 deactivate_cb,
2605 highlight_cb,
2606 0,
2607 0,
da18b5ac 2608 add_tearoff_p,
f392e843
JD
2609 0,
2610 cl_data,
2611 0);
2612 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2613 }
f392e843
JD
2614 }
2615
2616 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2617 gtk_widget_set_name (w, MENU_ITEM_NAME);
2618 }
2619
2620 return wmenu;
2621}
2622
2623/* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2624 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2625 with some text and buttons.
2626 F is the frame the created item belongs to.
2627 NAME is the name to use for the top widget.
2628 VAL is a widget_value structure describing items to be created.
2629 SELECT_CB is the callback to use when a menu item is selected or
2630 a dialog button is pressed.
2631 DEACTIVATE_CB is the callback to use when an item is deactivated.
2632 For a menu, when a sub menu is not shown anymore, for a dialog it is
2633 called when the dialog is popped down.
2634 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2635
2636 Returns the widget created. */
71bacd48 2637
f392e843 2638GtkWidget *
a10c8269
DA
2639xg_create_widget (const char *type, const char *name, struct frame *f,
2640 widget_value *val, GCallback select_cb,
2641 GCallback deactivate_cb, GCallback highlight_cb)
f392e843
JD
2642{
2643 GtkWidget *w = 0;
18e27ea8
PE
2644 bool menu_bar_p = strcmp (type, "menubar") == 0;
2645 bool pop_up_p = strcmp (type, "popup") == 0;
da18b5ac 2646
f392e843
JD
2647 if (strcmp (type, "dialog") == 0)
2648 {
2649 w = create_dialog (val, select_cb, deactivate_cb);
810f2256 2650 xg_set_screen (w, f);
f392e843
JD
2651 gtk_window_set_transient_for (GTK_WINDOW (w),
2652 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2653 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
810f2256 2654 gtk_widget_set_name (w, "emacs-dialog");
457a8155 2655 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
f392e843 2656 }
da18b5ac 2657 else if (menu_bar_p || pop_up_p)
f392e843
JD
2658 {
2659 w = create_menus (val->contents,
2660 f,
2661 select_cb,
2662 deactivate_cb,
2663 highlight_cb,
da18b5ac
JD
2664 pop_up_p,
2665 menu_bar_p,
2666 menu_bar_p,
f392e843
JD
2667 0,
2668 0,
2669 name);
2670
2671 /* Set the cursor to an arrow for popup menus when they are mapped.
2672 This is done by default for menu bar menus. */
da18b5ac 2673 if (pop_up_p)
f392e843
JD
2674 {
2675 /* Must realize so the GdkWindow inside the widget is created. */
2676 gtk_widget_realize (w);
aad3612f 2677 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
f392e843
JD
2678 }
2679 }
2680 else
2681 {
2682 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2683 type);
2684 }
2685
2686 return w;
2687}
2688
0a1d6de0 2689/* Return the label for menu item WITEM. */
71bacd48 2690
f392e843 2691static const char *
971de7fb 2692xg_get_menu_item_label (GtkMenuItem *witem)
f392e843 2693{
4039c786 2694 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
f392e843
JD
2695 return gtk_label_get_label (wlabel);
2696}
2697
18e27ea8 2698/* Return true if the menu item WITEM has the text LABEL. */
71bacd48 2699
18e27ea8 2700static bool
42ca4633 2701xg_item_label_same_p (GtkMenuItem *witem, const char *label)
f392e843 2702{
18e27ea8 2703 bool is_same = 0;
f392e843 2704 char *utf8_label = get_utf8_string (label);
0a1d6de0
JD
2705 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2706
2707 if (! old_label && ! utf8_label)
2708 is_same = 1;
2709 else if (old_label && utf8_label)
2710 is_same = strcmp (utf8_label, old_label) == 0;
2711
42ca4633 2712 if (utf8_label) g_free (utf8_label);
f392e843
JD
2713
2714 return is_same;
2715}
2716
71bacd48
JD
2717/* Destroy widgets in LIST. */
2718
f392e843 2719static void
971de7fb 2720xg_destroy_widgets (GList *list)
f392e843 2721{
f392e843
JD
2722 GList *iter;
2723
49853a4d 2724 for (iter = list; iter; iter = g_list_next (iter))
f392e843
JD
2725 {
2726 GtkWidget *w = GTK_WIDGET (iter->data);
2727
71bacd48 2728 /* Destroying the widget will remove it from the container it is in. */
f392e843
JD
2729 gtk_widget_destroy (w);
2730 }
f392e843
JD
2731}
2732
2733/* Update the top level names in MENUBAR (i.e. not submenus).
2734 F is the frame the menu bar belongs to.
49853a4d
JD
2735 *LIST is a list with the current menu bar names (menu item widgets).
2736 ITER is the item within *LIST that shall be updated.
2737 POS is the numerical position, starting at 0, of ITER in *LIST.
f392e843
JD
2738 VAL describes what the menu bar shall look like after the update.
2739 SELECT_CB is the callback to use when a menu item is selected.
2740 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
49853a4d 2741 CL_DATA points to the callback data to be used for this menu bar.
f392e843
JD
2742
2743 This function calls itself to walk through the menu bar names. */
71bacd48 2744
f392e843 2745static void
dd4c5104 2746xg_update_menubar (GtkWidget *menubar,
a10c8269 2747 struct frame *f,
dd4c5104
DN
2748 GList **list,
2749 GList *iter,
2750 int pos,
2751 widget_value *val,
2752 GCallback select_cb,
2753 GCallback deactivate_cb,
2754 GCallback highlight_cb,
2755 xg_menu_cb_data *cl_data)
f392e843 2756{
49853a4d 2757 if (! iter && ! val)
f392e843 2758 return;
49853a4d 2759 else if (iter && ! val)
f392e843 2760 {
49853a4d 2761 /* Item(s) have been removed. Remove all remaining items. */
71bacd48 2762 xg_destroy_widgets (iter);
f392e843 2763
4a8e097d
JD
2764 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2765 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2766 gtk_menu_item_new_with_label (""),
2767 0);
f392e843
JD
2768 /* All updated. */
2769 val = 0;
49853a4d 2770 iter = 0;
f392e843 2771 }
49853a4d 2772 else if (! iter && val)
f392e843
JD
2773 {
2774 /* Item(s) added. Add all new items in one call. */
d1c38b57 2775 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
f392e843
JD
2776 0, 1, 0, menubar, cl_data, 0);
2777
2778 /* All updated. */
2779 val = 0;
49853a4d 2780 iter = 0;
f392e843 2781 }
49853a4d
JD
2782 /* Below this neither iter or val is NULL */
2783 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
f392e843
JD
2784 {
2785 /* This item is still the same, check next item. */
2786 val = val->next;
49853a4d
JD
2787 iter = g_list_next (iter);
2788 ++pos;
f392e843
JD
2789 }
2790 else /* This item is changed. */
2791 {
49853a4d 2792 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
f392e843 2793 GtkMenuItem *witem2 = 0;
18e27ea8
PE
2794 bool val_in_menubar = 0;
2795 bool iter_in_new_menubar = 0;
49853a4d 2796 GList *iter2;
f392e843
JD
2797 widget_value *cur;
2798
f392e843 2799 /* See if the changed entry (val) is present later in the menu bar */
49853a4d
JD
2800 for (iter2 = iter;
2801 iter2 && ! val_in_menubar;
2802 iter2 = g_list_next (iter2))
f392e843 2803 {
49853a4d 2804 witem2 = GTK_MENU_ITEM (iter2->data);
f392e843
JD
2805 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2806 }
2807
49853a4d 2808 /* See if the current entry (iter) is present later in the
f392e843 2809 specification for the new menu bar. */
49853a4d
JD
2810 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2811 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
f392e843 2812
49853a4d 2813 if (val_in_menubar && ! iter_in_new_menubar)
f392e843 2814 {
49853a4d
JD
2815 int nr = pos;
2816
f392e843
JD
2817 /* This corresponds to:
2818 Current: A B C
2819 New: A C
2820 Remove B. */
177c0ea7 2821
e547b051 2822 g_object_ref (G_OBJECT (witem));
f392e843
JD
2823 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2824 gtk_widget_destroy (GTK_WIDGET (witem));
2825
2826 /* Must get new list since the old changed. */
49853a4d
JD
2827 g_list_free (*list);
2828 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2829 while (nr-- > 0) iter = g_list_next (iter);
f392e843 2830 }
49853a4d 2831 else if (! val_in_menubar && ! iter_in_new_menubar)
f392e843
JD
2832 {
2833 /* This corresponds to:
2834 Current: A B C
2835 New: A X C
2836 Rename B to X. This might seem to be a strange thing to do,
2837 since if there is a menu under B it will be totally wrong for X.
2838 But consider editing a C file. Then there is a C-mode menu
2839 (corresponds to B above).
2840 If then doing C-x C-f the minibuf menu (X above) replaces the
2841 C-mode menu. When returning from the minibuffer, we get
2842 back the C-mode menu. Thus we do:
2843 Rename B to X (C-mode to minibuf menu)
2844 Rename X to B (minibuf to C-mode menu).
2845 If the X menu hasn't been invoked, the menu under B
2846 is up to date when leaving the minibuffer. */
4039c786 2847 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
f392e843 2848 char *utf8_label = get_utf8_string (val->name);
da18b5ac 2849 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
177c0ea7 2850
7a1b473c
DC
2851 /* GTK menu items don't notice when their labels have been
2852 changed from underneath them, so we have to explicitly
2853 use g_object_notify to tell listeners (e.g., a GMenuModel
2854 bridge that might be loaded) that the item's label has
2855 changed. */
4b1b4443 2856 gtk_label_set_text (wlabel, utf8_label);
7a1b473c 2857 g_object_notify (G_OBJECT (witem), "label");
f392e843 2858
8b745d92 2859#ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
da18b5ac
JD
2860 /* If this item has a submenu that has been detached, change
2861 the title in the WM decorations also. */
2862 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2863 /* Set the title of the detached window. */
2864 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
8b745d92 2865#endif
da18b5ac 2866
42ca4633 2867 if (utf8_label) g_free (utf8_label);
49853a4d 2868 iter = g_list_next (iter);
f392e843 2869 val = val->next;
49853a4d 2870 ++pos;
f392e843 2871 }
49853a4d 2872 else if (! val_in_menubar && iter_in_new_menubar)
f392e843
JD
2873 {
2874 /* This corresponds to:
2875 Current: A B C
2876 New: A X B C
2877 Insert X. */
2878
49853a4d 2879 int nr = pos;
e4c8d29a 2880 GSList *group = 0;
f392e843
JD
2881 GtkWidget *w = xg_create_one_menuitem (val,
2882 f,
2883 select_cb,
2884 highlight_cb,
2885 cl_data,
2886 &group);
2887
f56cff88
JD
2888 /* Create a possibly empty submenu for menu bar items, since some
2889 themes don't highlight items correctly without it. */
2890 GtkWidget *submenu = create_menus (NULL, f,
d1c38b57
JD
2891 select_cb, deactivate_cb,
2892 highlight_cb,
f56cff88 2893 0, 0, 0, 0, cl_data, 0);
7a1b473c 2894
f392e843
JD
2895 gtk_widget_set_name (w, MENU_ITEM_NAME);
2896 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
f56cff88 2897 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
f392e843 2898
49853a4d
JD
2899 g_list_free (*list);
2900 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2901 while (nr-- > 0) iter = g_list_next (iter);
2902 iter = g_list_next (iter);
f392e843 2903 val = val->next;
49853a4d 2904 ++pos;
f392e843 2905 }
49853a4d 2906 else /* if (val_in_menubar && iter_in_new_menubar) */
f392e843 2907 {
49853a4d 2908 int nr = pos;
f392e843
JD
2909 /* This corresponds to:
2910 Current: A B C
2911 New: A C B
2912 Move C before B */
2913
e547b051 2914 g_object_ref (G_OBJECT (witem2));
f392e843
JD
2915 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2916 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2917 GTK_WIDGET (witem2), pos);
e547b051 2918 g_object_unref (G_OBJECT (witem2));
f392e843 2919
49853a4d
JD
2920 g_list_free (*list);
2921 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2922 while (nr-- > 0) iter = g_list_next (iter);
0a194c92 2923 if (iter) iter = g_list_next (iter);
f392e843 2924 val = val->next;
49853a4d 2925 ++pos;
f392e843 2926 }
f392e843
JD
2927 }
2928
2929 /* Update the rest of the menu bar. */
49853a4d 2930 xg_update_menubar (menubar, f, list, iter, pos, val,
d1c38b57 2931 select_cb, deactivate_cb, highlight_cb, cl_data);
f392e843
JD
2932}
2933
2934/* Update the menu item W so it corresponds to VAL.
2935 SELECT_CB is the callback to use when a menu item is selected.
2936 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
a8ce3d17 2937 CL_DATA is the data to set in the widget for menu invocation. */
71bacd48 2938
f392e843 2939static void
e4c8d29a
J
2940xg_update_menu_item (widget_value *val,
2941 GtkWidget *w,
2942 GCallback select_cb,
2943 GCallback highlight_cb,
2944 xg_menu_cb_data *cl_data)
f392e843
JD
2945{
2946 GtkWidget *wchild;
2947 GtkLabel *wlbl = 0;
2948 GtkLabel *wkey = 0;
2949 char *utf8_label;
2950 char *utf8_key;
0a1d6de0
JD
2951 const char *old_label = 0;
2952 const char *old_key = 0;
f392e843 2953 xg_menu_item_cb_data *cb_data;
7a1b473c 2954 bool label_changed = false;
177c0ea7 2955
4039c786 2956 wchild = XG_BIN_CHILD (w);
f392e843
JD
2957 utf8_label = get_utf8_string (val->name);
2958 utf8_key = get_utf8_string (val->key);
2959
2960 /* See if W is a menu item with a key. See make_menu_item above. */
383b7c95 2961 if (GTK_IS_BOX (wchild))
f392e843
JD
2962 {
2963 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2964
2965 wlbl = GTK_LABEL (list->data);
2966 wkey = GTK_LABEL (list->next->data);
49853a4d
JD
2967 g_list_free (list);
2968
f392e843
JD
2969 if (! utf8_key)
2970 {
2971 /* Remove the key and keep just the label. */
e547b051 2972 g_object_ref (G_OBJECT (wlbl));
f392e843
JD
2973 gtk_container_remove (GTK_CONTAINER (w), wchild);
2974 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
e547b051 2975 g_object_unref (G_OBJECT (wlbl));
f392e843
JD
2976 wkey = 0;
2977 }
49853a4d 2978
f392e843
JD
2979 }
2980 else /* Just a label. */
2981 {
2982 wlbl = GTK_LABEL (wchild);
2983
2984 /* Check if there is now a key. */
2985 if (utf8_key)
2986 {
2987 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2988 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
49853a4d 2989
f392e843
JD
2990 wlbl = GTK_LABEL (list->data);
2991 wkey = GTK_LABEL (list->next->data);
49853a4d 2992 g_list_free (list);
f392e843
JD
2993
2994 gtk_container_remove (GTK_CONTAINER (w), wchild);
2995 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2996 }
2997 }
2998
0a1d6de0
JD
2999 if (wkey) old_key = gtk_label_get_label (wkey);
3000 if (wlbl) old_label = gtk_label_get_label (wlbl);
177c0ea7 3001
0a1d6de0 3002 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
7a1b473c
DC
3003 {
3004 label_changed = true;
3005 gtk_label_set_text (wkey, utf8_key);
3006 }
f392e843 3007
0a1d6de0 3008 if (! old_label || strcmp (utf8_label, old_label) != 0)
7a1b473c
DC
3009 {
3010 label_changed = true;
3011 gtk_label_set_text (wlbl, utf8_label);
3012 }
f392e843 3013
42ca4633
J
3014 if (utf8_key) g_free (utf8_key);
3015 if (utf8_label) g_free (utf8_label);
177c0ea7 3016
e547b051 3017 if (! val->enabled && gtk_widget_get_sensitive (w))
f392e843 3018 gtk_widget_set_sensitive (w, FALSE);
e547b051 3019 else if (val->enabled && ! gtk_widget_get_sensitive (w))
f392e843
JD
3020 gtk_widget_set_sensitive (w, TRUE);
3021
7d652d97 3022 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
f392e843
JD
3023 if (cb_data)
3024 {
3025 cb_data->call_data = val->call_data;
3026 cb_data->help = val->help;
3027 cb_data->cl_data = cl_data;
177c0ea7 3028
f392e843
JD
3029 /* We assume the callback functions don't change. */
3030 if (val->call_data && ! val->contents)
3031 {
3032 /* This item shall have a select callback. */
3033 if (! cb_data->select_id)
3034 cb_data->select_id
3035 = g_signal_connect (G_OBJECT (w), "activate",
3036 select_cb, cb_data);
3037 }
3038 else if (cb_data->select_id)
3039 {
3040 g_signal_handler_disconnect (w, cb_data->select_id);
3041 cb_data->select_id = 0;
3042 }
f392e843 3043 }
7a1b473c
DC
3044
3045 if (label_changed) /* See comment in xg_update_menubar. */
3046 g_object_notify (G_OBJECT (w), "label");
f392e843
JD
3047}
3048
3049/* Update the toggle menu item W so it corresponds to VAL. */
71bacd48 3050
f392e843 3051static void
971de7fb 3052xg_update_toggle_item (widget_value *val, GtkWidget *w)
f392e843
JD
3053{
3054 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3055}
3056
3057/* Update the radio menu item W so it corresponds to VAL. */
71bacd48 3058
f392e843 3059static void
971de7fb 3060xg_update_radio_item (widget_value *val, GtkWidget *w)
f392e843
JD
3061{
3062 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3063}
3064
3065/* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3066 SUBMENU may be NULL, in that case a new menu is created.
3067 F is the frame the menu bar belongs to.
3068 VAL describes the contents of the menu bar.
3069 SELECT_CB is the callback to use when a menu item is selected.
3070 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3071 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3072 CL_DATA is the call back data to use for any newly created items.
3073
3074 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3075 was NULL. */
3076
3077static GtkWidget *
dd4c5104 3078xg_update_submenu (GtkWidget *submenu,
a10c8269 3079 struct frame *f,
dd4c5104
DN
3080 widget_value *val,
3081 GCallback select_cb,
3082 GCallback deactivate_cb,
3083 GCallback highlight_cb,
3084 xg_menu_cb_data *cl_data)
f392e843
JD
3085{
3086 GtkWidget *newsub = submenu;
3087 GList *list = 0;
3088 GList *iter;
3089 widget_value *cur;
18e27ea8 3090 bool has_tearoff_p = 0;
f392e843 3091 GList *first_radio = 0;
177c0ea7 3092
f392e843
JD
3093 if (submenu)
3094 list = gtk_container_get_children (GTK_CONTAINER (submenu));
177c0ea7 3095
f392e843
JD
3096 for (cur = val, iter = list;
3097 cur && iter;
3098 iter = g_list_next (iter), cur = cur->next)
3099 {
3100 GtkWidget *w = GTK_WIDGET (iter->data);
3101
8b745d92
JD
3102#ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
3103 /* Skip tearoff items, they have no counterpart in val. */
f392e843
JD
3104 if (GTK_IS_TEAROFF_MENU_ITEM (w))
3105 {
3106 has_tearoff_p = 1;
3107 iter = g_list_next (iter);
3108 if (iter) w = GTK_WIDGET (iter->data);
3109 else break;
3110 }
8b745d92 3111#endif
f392e843
JD
3112
3113 /* Remember first radio button in a group. If we get a mismatch in
3114 a radio group we must rebuild the whole group so that the connections
3115 in GTK becomes correct. */
3116 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3117 first_radio = iter;
3118 else if (cur->button_type != BUTTON_TYPE_RADIO
3119 && ! GTK_IS_RADIO_MENU_ITEM (w))
3120 first_radio = 0;
3121
3122 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3123 {
4039c786 3124 if (! menu_separator_name_p (cur->name))
f392e843
JD
3125 break;
3126 }
3127 else if (GTK_IS_CHECK_MENU_ITEM (w))
3128 {
3129 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3130 break;
3131 xg_update_toggle_item (cur, w);
3132 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3133 }
3134 else if (GTK_IS_RADIO_MENU_ITEM (w))
3135 {
3136 if (cur->button_type != BUTTON_TYPE_RADIO)
3137 break;
3138 xg_update_radio_item (cur, w);
3139 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3140 }
3141 else if (GTK_IS_MENU_ITEM (w))
3142 {
3143 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3144 GtkWidget *sub;
3145
3146 if (cur->button_type != BUTTON_TYPE_NONE ||
4039c786 3147 menu_separator_name_p (cur->name))
f392e843
JD
3148 break;
3149
3150 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3151
3152 sub = gtk_menu_item_get_submenu (witem);
3153 if (sub && ! cur->contents)
3154 {
3155 /* Not a submenu anymore. */
e547b051
J
3156 g_object_ref (G_OBJECT (sub));
3157 remove_submenu (witem);
f392e843
JD
3158 gtk_widget_destroy (sub);
3159 }
3160 else if (cur->contents)
3161 {
3162 GtkWidget *nsub;
3163
3164 nsub = xg_update_submenu (sub, f, cur->contents,
3165 select_cb, deactivate_cb,
3166 highlight_cb, cl_data);
3167
3168 /* If this item just became a submenu, we must set it. */
3169 if (nsub != sub)
3170 gtk_menu_item_set_submenu (witem, nsub);
3171 }
3172 }
3173 else
3174 {
3175 /* Structural difference. Remove everything from here and down
3176 in SUBMENU. */
3177 break;
3178 }
3179 }
3180
22bcf204 3181 /* Remove widgets from first structural change. */
f392e843
JD
3182 if (iter)
3183 {
3184 /* If we are adding new menu items below, we must remove from
3185 first radio button so that radio groups become correct. */
71bacd48
JD
3186 if (cur && first_radio) xg_destroy_widgets (first_radio);
3187 else xg_destroy_widgets (iter);
f392e843 3188 }
177c0ea7 3189
f392e843
JD
3190 if (cur)
3191 {
3192 /* More items added. Create them. */
3193 newsub = create_menus (cur,
3194 f,
3195 select_cb,
3196 deactivate_cb,
3197 highlight_cb,
3198 0,
3199 0,
3200 ! has_tearoff_p,
3201 submenu,
3202 cl_data,
3203 0);
3204 }
177c0ea7 3205
49853a4d
JD
3206 if (list) g_list_free (list);
3207
f392e843
JD
3208 return newsub;
3209}
3210
3211/* Update the MENUBAR.
3212 F is the frame the menu bar belongs to.
3213 VAL describes the contents of the menu bar.
18e27ea8 3214 If DEEP_P, rebuild all but the top level menu names in
f392e843
JD
3215 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3216 SELECT_CB is the callback to use when a menu item is selected.
3217 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3218 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
71bacd48 3219
f392e843 3220void
a10c8269
DA
3221xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3222 widget_value *val, bool deep_p,
d5a3eaaf
AS
3223 GCallback select_cb, GCallback deactivate_cb,
3224 GCallback highlight_cb)
f392e843
JD
3225{
3226 xg_menu_cb_data *cl_data;
3227 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
f392e843
JD
3228
3229 if (! list) return;
177c0ea7 3230
7d652d97 3231 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
f392e843 3232
da18b5ac 3233 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
d1c38b57 3234 select_cb, deactivate_cb, highlight_cb, cl_data);
da18b5ac 3235
aa41b0bf 3236 if (deep_p)
f392e843
JD
3237 {
3238 widget_value *cur;
3239
3240 /* Update all sub menus.
da18b5ac 3241 We must keep the submenus (GTK menu item widgets) since the
f392e843
JD
3242 X Window in the XEvent that activates the menu are those widgets. */
3243
3244 /* Update cl_data, menu_item things in F may have changed. */
3245 update_cl_data (cl_data, f, highlight_cb);
3246
3247 for (cur = val->contents; cur; cur = cur->next)
3248 {
49853a4d 3249 GList *iter;
f392e843
JD
3250 GtkWidget *sub = 0;
3251 GtkWidget *newsub;
e4c8d29a 3252 GtkMenuItem *witem = 0;
f392e843
JD
3253
3254 /* Find sub menu that corresponds to val and update it. */
3255 for (iter = list ; iter; iter = g_list_next (iter))
3256 {
3257 witem = GTK_MENU_ITEM (iter->data);
3258 if (xg_item_label_same_p (witem, cur->name))
3259 {
3260 sub = gtk_menu_item_get_submenu (witem);
3261 break;
3262 }
3263 }
177c0ea7 3264
f392e843
JD
3265 newsub = xg_update_submenu (sub,
3266 f,
3267 cur->contents,
3268 select_cb,
3269 deactivate_cb,
3270 highlight_cb,
3271 cl_data);
3272 /* sub may still be NULL. If we just updated non deep and added
3273 a new menu bar item, it has no sub menu yet. So we set the
3274 newly created sub menu under witem. */
e4c8d29a 3275 if (newsub != sub && witem != 0)
810f2256
JD
3276 {
3277 xg_set_screen (newsub, f);
3278 gtk_menu_item_set_submenu (witem, newsub);
3279 }
f392e843
JD
3280 }
3281 }
3282
49853a4d 3283 g_list_free (list);
f392e843
JD
3284 gtk_widget_show_all (menubar);
3285}
3286
8d7f026f
JD
3287/* Callback called when the menu bar W is mapped.
3288 Used to find the height of the menu bar if we didn't get it
3289 after showing the widget. */
3290
3291static void
3292menubar_map_cb (GtkWidget *w, gpointer user_data)
3293{
3294 GtkRequisition req;
7d652d97 3295 struct frame *f = user_data;
0afb4571 3296 gtk_widget_get_preferred_size (w, NULL, &req);
088c8c09 3297 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
8d7f026f
JD
3298 {
3299 FRAME_MENUBAR_HEIGHT (f) = req.height;
088c8c09 3300 xg_height_or_width_changed (f);
8d7f026f
JD
3301 }
3302}
3303
f392e843 3304/* Recompute all the widgets of frame F, when the menu bar has been
18e27ea8 3305 changed. */
f392e843 3306
18e27ea8 3307void
a10c8269 3308xg_update_frame_menubar (struct frame *f)
f392e843
JD
3309{
3310 struct x_output *x = f->output_data.x;
3311 GtkRequisition req;
177c0ea7 3312
e547b051 3313 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
18e27ea8 3314 return;
f392e843 3315
473a99b7 3316 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
18e27ea8 3317 return; /* Already done this, happens for frames created invisible. */
473a99b7 3318
4d7e6e51 3319 block_input ();
f392e843
JD
3320
3321 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3322 FALSE, FALSE, 0);
3323 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3324
8d7f026f 3325 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
f392e843 3326 gtk_widget_show_all (x->menubar_widget);
0afb4571 3327 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
07976ae3 3328
8d7f026f
JD
3329 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3330 {
3331 FRAME_MENUBAR_HEIGHT (f) = req.height;
07976ae3 3332 xg_height_or_width_changed (f);
8d7f026f 3333 }
4d7e6e51 3334 unblock_input ();
f392e843
JD
3335}
3336
3337/* Get rid of the menu bar of frame F, and free its storage.
3338 This is used when deleting a frame, and when turning off the menu bar. */
3339
3340void
a10c8269 3341free_frame_menubar (struct frame *f)
f392e843
JD
3342{
3343 struct x_output *x = f->output_data.x;
3344
3345 if (x->menubar_widget)
3346 {
4d7e6e51 3347 block_input ();
f392e843
JD
3348
3349 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3350 /* The menubar and its children shall be deleted when removed from
3351 the container. */
3352 x->menubar_widget = 0;
3353 FRAME_MENUBAR_HEIGHT (f) = 0;
bfeabdc3 3354 xg_height_or_width_changed (f);
4d7e6e51 3355 unblock_input ();
f392e843
JD
3356 }
3357}
3358
18e27ea8 3359bool
b7ad2f74 3360xg_event_is_for_menubar (struct frame *f, const XEvent *event)
b78f9767
J
3361{
3362 struct x_output *x = f->output_data.x;
499322ce
J
3363 GList *iter;
3364 GdkRectangle rec;
3365 GList *list;
3366 GdkDisplay *gdpy;
3367 GdkWindow *gw;
3368 GdkEvent gevent;
3369 GtkWidget *gwdesc;
b78f9767
J
3370
3371 if (! x->menubar_widget) return 0;
3372
3373 if (! (event->xbutton.x >= 0
3374 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3375 && event->xbutton.y >= 0
3376 && event->xbutton.y < f->output_data.x->menubar_height
3377 && event->xbutton.same_screen))
3378 return 0;
3379
499322ce 3380 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
0afb4571 3381 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
499322ce
J
3382 if (! gw) return 0;
3383 gevent.any.window = gw;
3b574623 3384 gevent.any.type = GDK_NOTHING;
499322ce
J
3385 gwdesc = gtk_get_event_widget (&gevent);
3386 if (! gwdesc) return 0;
3387 if (! GTK_IS_MENU_BAR (gwdesc)
3388 && ! GTK_IS_MENU_ITEM (gwdesc)
3389 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3390 return 0;
3391
3392 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
b78f9767 3393 if (! list) return 0;
b78f9767
J
3394 rec.x = event->xbutton.x;
3395 rec.y = event->xbutton.y;
3396 rec.width = 1;
3397 rec.height = 1;
499322ce 3398
b78f9767
J
3399 for (iter = list ; iter; iter = g_list_next (iter))
3400 {
3401 GtkWidget *w = GTK_WIDGET (iter->data);
ce6e9d7f 3402 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
b78f9767
J
3403 break;
3404 }
3405 g_list_free (list);
18e27ea8 3406 return iter != 0;
b78f9767
J
3407}
3408
f392e843
JD
3409
3410\f
3411/***********************************************************************
3412 Scroll bar functions
3413 ***********************************************************************/
3414
3415
3416/* Setting scroll bar values invokes the callback. Use this variable
3417 to indicate that callback should do nothing. */
71bacd48 3418
18e27ea8 3419bool xg_ignore_gtk_scrollbar;
f392e843 3420
c195f2de
JD
3421/* The width of the scroll bar for the current theme. */
3422
3423static int scroll_bar_width_for_theme;
3424
056ce195
SM
3425/* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3426 may be larger than 32 bits. Keep a mapping from integer index to widget
3427 pointers to get around the 32 bit limitation. */
71bacd48 3428
f392e843
JD
3429static struct
3430{
3431 GtkWidget **widgets;
0eb0f318
PE
3432 ptrdiff_t max_size;
3433 ptrdiff_t used;
81e302ef 3434} id_to_widget;
f392e843
JD
3435
3436/* Grow this much every time we need to allocate more */
71bacd48 3437
f392e843
JD
3438#define ID_TO_WIDGET_INCR 32
3439
3440/* Store the widget pointer W in id_to_widget and return the integer index. */
71bacd48 3441
0eb0f318 3442static ptrdiff_t
971de7fb 3443xg_store_widget_in_map (GtkWidget *w)
f392e843 3444{
0eb0f318 3445 ptrdiff_t i;
f392e843
JD
3446
3447 if (id_to_widget.max_size == id_to_widget.used)
3448 {
0eb0f318 3449 ptrdiff_t new_size;
0065d054 3450 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
0eb0f318 3451 memory_full (SIZE_MAX);
f392e843 3452
0eb0f318 3453 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
0065d054
PE
3454 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3455 new_size, sizeof (GtkWidget *));
f392e843
JD
3456
3457 for (i = id_to_widget.max_size; i < new_size; ++i)
3458 id_to_widget.widgets[i] = 0;
3459 id_to_widget.max_size = new_size;
3460 }
3461
3462 /* Just loop over the array and find a free place. After all,
3463 how many scroll bars are we creating? Should be a small number.
3464 The check above guarantees we will find a free place. */
3465 for (i = 0; i < id_to_widget.max_size; ++i)
3466 {
3467 if (! id_to_widget.widgets[i])
3468 {
3469 id_to_widget.widgets[i] = w;
3470 ++id_to_widget.used;
3471
3472 return i;
3473 }
3474 }
3475
3476 /* Should never end up here */
1088b922 3477 emacs_abort ();
f392e843
JD
3478}
3479
3480/* Remove pointer at IDX from id_to_widget.
3481 Called when scroll bar is destroyed. */
71bacd48 3482
f392e843 3483static void
0eb0f318 3484xg_remove_widget_from_map (ptrdiff_t idx)
f392e843
JD
3485{
3486 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3487 {
3488 id_to_widget.widgets[idx] = 0;
3489 --id_to_widget.used;
3490 }
3491}
3492
3493/* Get the widget pointer at IDX from id_to_widget. */
71bacd48 3494
f392e843 3495static GtkWidget *
0eb0f318 3496xg_get_widget_from_map (ptrdiff_t idx)
f392e843
JD
3497{
3498 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3499 return id_to_widget.widgets[idx];
3500
3501 return 0;
3502}
3503
c195f2de
JD
3504static void
3505update_theme_scrollbar_width (void)
a059fe24 3506{
43f862f7
AS
3507#ifdef HAVE_GTK3
3508 GtkAdjustment *vadj;
3509#else
3510 GtkObject *vadj;
3511#endif
3512 GtkWidget *wscroll;
a059fe24 3513 int w = 0, b = 0;
c195f2de 3514
43f862f7 3515 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
383b7c95 3516 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
c195f2de 3517 g_object_ref_sink (G_OBJECT (wscroll));
a059fe24
JD
3518 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3519 gtk_widget_destroy (wscroll);
c195f2de 3520 g_object_unref (G_OBJECT (wscroll));
a059fe24
JD
3521 w += 2*b;
3522 if (w < 16) w = 16;
c195f2de
JD
3523 scroll_bar_width_for_theme = w;
3524}
3525
3526int
3527xg_get_default_scrollbar_width (void)
3528{
3529 return scroll_bar_width_for_theme;
a059fe24
JD
3530}
3531
810f2256 3532/* Return the scrollbar id for X Window WID on display DPY.
3a8a22fc 3533 Return -1 if WID not in id_to_widget. */
71bacd48 3534
0eb0f318 3535ptrdiff_t
971de7fb 3536xg_get_scroll_id_for_window (Display *dpy, Window wid)
3a8a22fc 3537{
0eb0f318 3538 ptrdiff_t idx;
3a8a22fc
JD
3539 GtkWidget *w;
3540
810f2256 3541 w = xg_win_to_widget (dpy, wid);
3a8a22fc
JD
3542
3543 if (w)
3544 {
3545 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3546 if (id_to_widget.widgets[idx] == w)
3547 return idx;
3548 }
3549
3550 return -1;
3551}
3552
f392e843
JD
3553/* Callback invoked when scroll bar WIDGET is destroyed.
3554 DATA is the index into id_to_widget for WIDGET.
cea9be54 3555 We free pointer to last scroll bar values here and remove the index. */
71bacd48 3556
f392e843 3557static void
971de7fb 3558xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
f392e843 3559{
0eb0f318 3560 intptr_t id = (intptr_t) data;
f392e843
JD
3561 xg_remove_widget_from_map (id);
3562}
3563
f392e843
JD
3564/* Create a scroll bar widget for frame F. Store the scroll bar
3565 in BAR.
3566 SCROLL_CALLBACK is the callback to invoke when the value of the
3567 bar changes.
e5f0bc9a 3568 END_CALLBACK is the callback to invoke when scrolling ends.
f392e843
JD
3569 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3570 to set resources for the widget. */
71bacd48 3571
f392e843 3572void
a10c8269 3573xg_create_scroll_bar (struct frame *f,
e4c8d29a
J
3574 struct scroll_bar *bar,
3575 GCallback scroll_callback,
3576 GCallback end_callback,
675e2c69 3577 const char *scroll_bar_name)
f392e843
JD
3578{
3579 GtkWidget *wscroll;
1755a397 3580 GtkWidget *webox;
d01a7826 3581 intptr_t scroll_id;
0afb4571
J
3582#ifdef HAVE_GTK3
3583 GtkAdjustment *vadj;
3584#else
3585 GtkObject *vadj;
3586#endif
177c0ea7 3587
f392e843
JD
3588 /* Page, step increment values are not so important here, they
3589 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3590 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3591 0.1, 0.1, 0.1);
3592
383b7c95 3593 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
1755a397 3594 webox = gtk_event_box_new ();
f392e843 3595 gtk_widget_set_name (wscroll, scroll_bar_name);
0afb4571 3596#ifndef HAVE_GTK3
f392e843 3597 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
0afb4571 3598#endif
e5f0bc9a 3599 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
177c0ea7 3600
f392e843 3601 scroll_id = xg_store_widget_in_map (wscroll);
177c0ea7 3602
f392e843
JD
3603 g_signal_connect (G_OBJECT (wscroll),
3604 "destroy",
3605 G_CALLBACK (xg_gtk_scroll_destroy),
8ac068ac 3606 (gpointer) scroll_id);
e5f0bc9a
JD
3607 g_signal_connect (G_OBJECT (wscroll),
3608 "change-value",
3609 scroll_callback,
3610 (gpointer) bar);
e8d7886a
JD
3611 g_signal_connect (G_OBJECT (wscroll),
3612 "button-release-event",
e5f0bc9a 3613 end_callback,
e8d7886a 3614 (gpointer) bar);
088c8c09 3615
1755a397
JD
3616 /* The scroll bar widget does not draw on a window of its own. Instead
3617 it draws on the parent window, in this case the edit widget. So
3618 whenever the edit widget is cleared, the scroll bar needs to redraw
3619 also, which causes flicker. Put an event box between the edit widget
3620 and the scroll bar, so the scroll bar instead draws itself on the
3621 event box window. */
3622 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3623 gtk_container_add (GTK_CONTAINER (webox), wscroll);
c43923ad 3624
f392e843
JD
3625
3626 /* Set the cursor to an arrow. */
aad3612f 3627 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
f392e843 3628
056ce195 3629 bar->x_window = scroll_id;
f392e843
JD
3630}
3631
f392e843 3632/* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
71bacd48 3633
f392e843 3634void
a10c8269 3635xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
f392e843
JD
3636{
3637 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3638 if (w)
3639 {
1755a397 3640 GtkWidget *wparent = gtk_widget_get_parent (w);
f392e843 3641 gtk_widget_destroy (w);
1755a397 3642 gtk_widget_destroy (wparent);
f392e843
JD
3643 SET_FRAME_GARBAGED (f);
3644 }
3645}
3646
f392e843
JD
3647/* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3648 in frame F.
3649 TOP/LEFT are the new pixel positions where the bar shall appear.
3650 WIDTH, HEIGHT is the size in pixels the bar shall have. */
71bacd48 3651
f392e843 3652void
a10c8269 3653xg_update_scrollbar_pos (struct frame *f,
0eb0f318 3654 ptrdiff_t scrollbar_id,
e4c8d29a
J
3655 int top,
3656 int left,
3657 int width,
3658 int height)
f392e843 3659{
f392e843 3660
49853a4d 3661 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
7863d625 3662
49853a4d
JD
3663 if (wscroll)
3664 {
cea9be54 3665 GtkWidget *wfixed = f->output_data.x->edit_widget;
1755a397 3666 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
8c079ebb 3667 gint msl;
7c583cd8
JD
3668
3669 /* Clear out old position. */
7c583cd8 3670 int oldx = -1, oldy = -1, oldw, oldh;
e547b051
J
3671 if (gtk_widget_get_parent (wparent) == wfixed)
3672 {
3673 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3674 "x", &oldx, "y", &oldy, NULL);
3675 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3676 }
7863d625 3677
ab2d724b 3678 /* Move and resize to new values. */
1755a397 3679 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
bc869eca
JD
3680 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3681 if (msl > height)
3682 {
3683 /* No room. Hide scroll bar as some themes output a warning if
3684 the height is less than the min size. */
3685 gtk_widget_hide (wparent);
3686 gtk_widget_hide (wscroll);
3687 }
3688 else
3689 {
3690 gtk_widget_show_all (wparent);
3691 gtk_widget_set_size_request (wscroll, width, height);
3692 }
7c583cd8 3693 gtk_widget_queue_draw (wfixed);
817d354b 3694 gdk_window_process_all_updates ();
bc869eca 3695 if (oldx != -1 && oldw > 0 && oldh > 0)
1f5cf200
DA
3696 /* Clear under old scroll bar position. This must be done after
3697 the gtk_widget_queue_draw and gdk_window_process_all_updates
3698 above. */
3699 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3700 oldx, oldy, oldw, oldh);
088c8c09 3701
817d354b
JD
3702 /* GTK does not redraw until the main loop is entered again, but
3703 if there are no X events pending we will not enter it. So we sync
3704 here to get some events. */
088c8c09 3705
817d354b 3706 x_sync (f);
49853a4d
JD
3707 SET_FRAME_GARBAGED (f);
3708 cancel_mouse_face (f);
3709 }
f392e843
JD
3710}
3711
c195f2de
JD
3712/* Get the current value of the range, truncated to an integer. */
3713
3714static int
3715int_gtk_range_get_value (GtkRange *range)
3716{
3717 return gtk_range_get_value (range);
3718}
3719
3720
f392e843
JD
3721/* Set the thumb size and position of scroll bar BAR. We are currently
3722 displaying PORTION out of a whole WHOLE, and our position POSITION. */
71bacd48 3723
f392e843 3724void
e4c8d29a
J
3725xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3726 int portion,
3727 int position,
3728 int whole)
f392e843 3729{
056ce195 3730 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
f392e843 3731
a10c8269 3732 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
f392e843 3733
d1fc6c21 3734 if (wscroll && bar->dragging == -1)
f392e843
JD
3735 {
3736 GtkAdjustment *adj;
3737 gdouble shown;
3738 gdouble top;
3739 int size, value;
6048fb2a 3740 int old_size;
7863d625 3741 int new_step;
18e27ea8 3742 bool changed = 0;
177c0ea7 3743
f392e843
JD
3744 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3745
ec782c5f
JD
3746 if (scroll_bar_adjust_thumb_portion_p)
3747 {
3748 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3749 line rather than the real portion value. This makes the
3750 thumb less likely to resize and that looks better. */
3751 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3752
3753 /* When the thumb is at the bottom, position == whole.
3754 So we need to increase `whole' to make space for the thumb. */
3755 whole += portion;
3756 }
17097258 3757
f392e843
JD
3758 if (whole <= 0)
3759 top = 0, shown = 1;
3760 else
3761 {
f392e843 3762 top = (gdouble) position / whole;
7863d625 3763 shown = (gdouble) portion / whole;
f392e843
JD
3764 }
3765
2dd61a9b
DA
3766 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3767 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
f392e843 3768
7863d625 3769 /* Assume all lines are of equal size. */
be786000 3770 new_step = size / max (1, FRAME_LINES (f));
2a2071c3 3771
6048fb2a
PE
3772 old_size = gtk_adjustment_get_page_size (adj);
3773 if (old_size != size)
3774 {
3775 int old_step = gtk_adjustment_get_step_increment (adj);
3776 if (old_step != new_step)
3777 {
3778 gtk_adjustment_set_page_size (adj, size);
3779 gtk_adjustment_set_step_increment (adj, new_step);
3780 /* Assume a page increment is about 95% of the page size */
5f8f9cc2 3781 gtk_adjustment_set_page_increment (adj, size - size / 20);
6048fb2a
PE
3782 changed = 1;
3783 }
3784 }
17097258 3785
1e5524e7 3786 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
7863d625 3787 {
4d7e6e51 3788 block_input ();
cea9be54 3789
7863d625
JD
3790 /* gtk_range_set_value invokes the callback. Set
3791 ignore_gtk_scrollbar to make the callback do nothing */
3792 xg_ignore_gtk_scrollbar = 1;
f392e843 3793
1e5524e7 3794 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
7863d625
JD
3795 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3796 else if (changed)
3797 gtk_adjustment_changed (adj);
3798
3799 xg_ignore_gtk_scrollbar = 0;
3800
4d7e6e51 3801 unblock_input ();
7863d625
JD
3802 }
3803 }
f392e843
JD
3804}
3805
18e27ea8 3806/* Return true if EVENT is for a scroll bar in frame F.
e511451f
JD
3807 When the same X window is used for several Gtk+ widgets, we cannot
3808 say for sure based on the X window alone if an event is for the
18e27ea8 3809 frame. This function does additional checks. */
e511451f 3810
18e27ea8 3811bool
b7ad2f74 3812xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
e511451f 3813{
18e27ea8 3814 bool retval = 0;
e511451f 3815
e5f0bc9a 3816 if (f && event->type == ButtonPress && event->xbutton.button < 4)
e511451f
JD
3817 {
3818 /* Check if press occurred outside the edit widget. */
3819 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
383b7c95
JD
3820 GdkWindow *gwin;
3821#ifdef HAVE_GTK3
3822 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3823 (gdk_display_get_device_manager (gdpy));
bdd091e4 3824 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
383b7c95
JD
3825#else
3826 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3827#endif
3828 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
e511451f 3829 }
e5f0bc9a
JD
3830 else if (f
3831 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3832 || event->type == MotionNotify))
e511451f
JD
3833 {
3834 /* If we are releasing or moving the scroll bar, it has the grab. */
3bb49aaf
JD
3835 GtkWidget *w = gtk_grab_get_current ();
3836 retval = w != 0 && GTK_IS_SCROLLBAR (w);
e511451f 3837 }
088c8c09 3838
e511451f
JD
3839 return retval;
3840}
3841
3842
f392e843
JD
3843\f
3844/***********************************************************************
3845 Tool bar functions
3846 ***********************************************************************/
3847/* The key for the data we put in the GtkImage widgets. The data is
3848 the image used by Emacs. We use this to see if we need to update
3849 the GtkImage with a new image. */
3850#define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3851
2334f9e7
JD
3852/* The key for storing the latest modifiers so the activate callback can
3853 get them. */
3854#define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3855
1e0fe298
JD
3856/* The key for storing the button widget in its proxy menu item. */
3857#define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
2334f9e7 3858
98a92193
JD
3859/* The key for the data we put in the GtkImage widgets. The data is
3860 the stock name used by Emacs. We use this to see if we need to update
3861 the GtkImage with a new image. */
3862#define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3863
2154c964
JD
3864/* As above, but this is used for named theme widgets, as opposed to
3865 stock items. */
3866#define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3867
98a92193
JD
3868/* Callback function invoked when a tool bar item is pressed.
3869 W is the button widget in the tool bar that got pressed,
3870 CLIENT_DATA is an integer that is the index of the button in the
3871 tool bar. 0 is the first button. */
3872
2334f9e7 3873static gboolean
e4c8d29a
J
3874xg_tool_bar_button_cb (GtkWidget *widget,
3875 GdkEventButton *event,
3876 gpointer user_data)
2334f9e7 3877{
d01a7826 3878 intptr_t state = event->state;
8ac068ac 3879 gpointer ptr = (gpointer) state;
6e1440e6 3880 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
6d45d2a0 3881 return FALSE;
2334f9e7
JD
3882}
3883
3884
6e1440e6
JD
3885/* Callback function invoked when a tool bar item is pressed.
3886 W is the button widget in the tool bar that got pressed,
3887 CLIENT_DATA is an integer that is the index of the button in the
3888 tool bar. 0 is the first button. */
3889
f392e843 3890static void
971de7fb 3891xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
f392e843 3892{
d01a7826 3893 intptr_t idx = (intptr_t) client_data;
1e5524e7 3894 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
d01a7826 3895 intptr_t mod = (intptr_t) gmod;
2334f9e7 3896
7d652d97 3897 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
f392e843
JD
3898 Lisp_Object key, frame;
3899 struct input_event event;
aa4ac494 3900 EVENT_INIT (event);
f392e843 3901
e69b0960 3902 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
f392e843
JD
3903 return;
3904
3905 idx *= TOOL_BAR_ITEM_NSLOTS;
177c0ea7 3906
e69b0960 3907 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
f392e843 3908 XSETFRAME (frame, f);
991bde0d
JD
3909
3910 /* We generate two events here. The first one is to set the prefix
3911 to `(tool_bar)', see keyboard.c. */
f392e843
JD
3912 event.kind = TOOL_BAR_EVENT;
3913 event.frame_or_window = frame;
8e5a8840
JB
3914 event.arg = frame;
3915 kbd_buffer_store_event (&event);
3916
3917 event.kind = TOOL_BAR_EVENT;
991bde0d 3918 event.frame_or_window = frame;
f392e843 3919 event.arg = key;
2334f9e7 3920 /* Convert between the modifier bits GDK uses and the modifier bits
4a02423f 3921 Emacs uses. This assumes GDK and X masks are the same, which they are when
2334f9e7 3922 this is written. */
aad3612f 3923 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
f392e843 3924 kbd_buffer_store_event (&event);
088c8c09 3925
fcd42c11
DA
3926 /* Return focus to the frame after we have clicked on a detached
3927 tool bar button. */
3928 x_focus_frame (f);
f392e843
JD
3929}
3930
1e0fe298
JD
3931/* Callback function invoked when a tool bar item is pressed in a detached
3932 tool bar or the overflow drop down menu.
3933 We just call xg_tool_bar_callback.
3934 W is the menu item widget that got pressed,
3935 CLIENT_DATA is an integer that is the index of the button in the
3936 tool bar. 0 is the first button. */
3937
3938static void
971de7fb 3939xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
1e0fe298
JD
3940{
3941 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3942 XG_TOOL_BAR_PROXY_BUTTON));
3943 xg_tool_bar_callback (wbutton, client_data);
3944}
3945
69b16610
JD
3946
3947static gboolean
f57e2426
J
3948xg_tool_bar_help_callback (GtkWidget *w,
3949 GdkEventCrossing *event,
3950 gpointer client_data);
69b16610
JD
3951
3952/* This callback is called when a help is to be shown for an item in
3953 the detached tool bar when the detached tool bar it is not expanded. */
3954
3955static gboolean
e4c8d29a
J
3956xg_tool_bar_proxy_help_callback (GtkWidget *w,
3957 GdkEventCrossing *event,
3958 gpointer client_data)
69b16610
JD
3959{
3960 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3961 XG_TOOL_BAR_PROXY_BUTTON));
088c8c09 3962
eba5eb94 3963 return xg_tool_bar_help_callback (wbutton, event, client_data);
69b16610
JD
3964}
3965
e547b051
J
3966static GtkWidget *
3967xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3968{
3969 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
7d652d97
PE
3970 GtkWidget *c1 = clist->data;
3971 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
3afff00e 3972
e547b051
J
3973 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3974 g_list_free (clist);
3975 return GTK_IS_LABEL (c1) ? c1 : c2;
3976}
3977
69b16610 3978
1e0fe298
JD
3979/* This callback is called when a tool item should create a proxy item,
3980 such as for the overflow menu. Also called when the tool bar is detached.
3981 If we don't create a proxy menu item, the detached tool bar will be
3982 blank. */
3983
3984static gboolean
971de7fb 3985xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
1e0fe298 3986{
4039c786
CY
3987 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3988 GtkWidget *vb = XG_BIN_CHILD (wbutton);
e547b051
J
3989 GtkWidget *c1;
3990 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3991 GtkImage *wimage = GTK_IMAGE (c1);
f904c0f9 3992 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
42f60557 3993 (wlbl ? gtk_label_get_text (wlbl) : "");
1e0fe298
JD
3994 GtkWidget *wmenuimage;
3995
e547b051 3996
8e5a8840 3997 if (gtk_button_get_use_stock (wbutton))
1e0fe298
JD
3998 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3999 GTK_ICON_SIZE_MENU);
4000 else
4001 {
1e0fe298
JD
4002 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
4003 GtkImageType store_type = gtk_image_get_storage_type (wimage);
e624c3f9 4004
2b5b82db
J
4005 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
4006
1e0fe298
JD
4007 if (store_type == GTK_IMAGE_STOCK)
4008 {
4009 gchar *stock_id;
4010 gtk_image_get_stock (wimage, &stock_id, NULL);
4011 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
4012 }
4013 else if (store_type == GTK_IMAGE_ICON_SET)
4014 {
4015 GtkIconSet *icon_set;
4016 gtk_image_get_icon_set (wimage, &icon_set, NULL);
4017 wmenuimage = gtk_image_new_from_icon_set (icon_set,
4018 GTK_ICON_SIZE_MENU);
4019 }
4020 else if (store_type == GTK_IMAGE_PIXBUF)
4021 {
4022 gint width, height;
8e5a8840 4023
1e0fe298
JD
4024 if (settings &&
4025 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
4026 &width, &height))
4027 {
4028 GdkPixbuf *src_pixbuf, *dest_pixbuf;
4029
4030 src_pixbuf = gtk_image_get_pixbuf (wimage);
4031 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
4032 GDK_INTERP_BILINEAR);
4033
4034 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
4035 }
e624c3f9
JD
4036 else
4037 {
4038 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
1088b922 4039 emacs_abort ();
e624c3f9
JD
4040 }
4041 }
8e5a8840 4042 else if (store_type == GTK_IMAGE_ICON_NAME)
e624c3f9
JD
4043 {
4044 const gchar *icon_name;
4045 GtkIconSize icon_size;
4046
4047 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
4048 wmenuimage = gtk_image_new_from_icon_name (icon_name,
4049 GTK_ICON_SIZE_MENU);
4050 }
4051 else
4052 {
4053 fprintf (stderr, "internal error: store_type is %d\n", store_type);
1088b922 4054 emacs_abort ();
1e0fe298
JD
4055 }
4056 }
4057 if (wmenuimage)
4058 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
4059
4060 g_signal_connect (G_OBJECT (wmenuitem),
4061 "activate",
dc2933eb 4062 G_CALLBACK (xg_tool_bar_proxy_callback),
1e0fe298
JD
4063 user_data);
4064
088c8c09 4065
1e0fe298
JD
4066 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
4067 (gpointer) wbutton);
4068 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
e547b051
J
4069 gtk_widget_set_sensitive (wmenuitem,
4070 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
69b16610
JD
4071
4072 /* Use enter/leave notify to show help. We use the events
4073 rather than the GtkButton specific signals "enter" and
4074 "leave", so we can have only one callback. The event
4075 will tell us what kind of event it is. */
4076 g_signal_connect (G_OBJECT (wmenuitem),
4077 "enter-notify-event",
4078 G_CALLBACK (xg_tool_bar_proxy_help_callback),
4079 user_data);
4080 g_signal_connect (G_OBJECT (wmenuitem),
4081 "leave-notify-event",
4082 G_CALLBACK (xg_tool_bar_proxy_help_callback),
4083 user_data);
1e0fe298
JD
4084
4085 return TRUE;
4086}
4087
f392e843
JD
4088/* This callback is called when a tool bar is detached. We must set
4089 the height of the tool bar to zero when this happens so frame sizes
4090 are correctly calculated.
4091 WBOX is the handle box widget that enables detach/attach of the tool bar.
4092 W is the tool bar widget.
4093 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 4094
f392e843 4095static void
e4c8d29a
J
4096xg_tool_bar_detach_callback (GtkHandleBox *wbox,
4097 GtkWidget *w,
4098 gpointer client_data)
f392e843 4099{
7d652d97 4100 struct frame *f = client_data;
0f340cab
JD
4101
4102 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
4103 NULL);
f392e843
JD
4104
4105 if (f)
4106 {
bfeabdc3 4107 GtkRequisition req, req2;
cd78d9b1 4108
0afb4571
J
4109 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4110 gtk_widget_get_preferred_size (w, NULL, &req2);
bfeabdc3
JD
4111 req.width -= req2.width;
4112 req.height -= req2.height;
4113 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4114 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4115 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4116 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4117 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4118 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4119 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4120 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4121 xg_height_or_width_changed (f);
f392e843
JD
4122 }
4123}
4124
4125/* This callback is called when a tool bar is reattached. We must set
4126 the height of the tool bar when this happens so frame sizes
4127 are correctly calculated.
4128 WBOX is the handle box widget that enables detach/attach of the tool bar.
4129 W is the tool bar widget.
4130 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 4131
f392e843 4132static void
e4c8d29a
J
4133xg_tool_bar_attach_callback (GtkHandleBox *wbox,
4134 GtkWidget *w,
4135 gpointer client_data)
f392e843 4136{
7d652d97 4137 struct frame *f = client_data;
0f340cab 4138 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
f392e843
JD
4139
4140 if (f)
4141 {
bfeabdc3 4142 GtkRequisition req, req2;
cd78d9b1 4143
0afb4571
J
4144 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4145 gtk_widget_get_preferred_size (w, NULL, &req2);
bfeabdc3
JD
4146 req.width += req2.width;
4147 req.height += req2.height;
4148 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4149 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4150 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4151 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4152 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4153 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4154 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4155 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4156 xg_height_or_width_changed (f);
f392e843
JD
4157 }
4158}
4159
4160/* This callback is called when the mouse enters or leaves a tool bar item.
4161 It is used for displaying and hiding the help text.
4162 W is the tool bar item, a button.
4163 EVENT is either an enter event or leave event.
4164 CLIENT_DATA is an integer that is the index of the button in the
4165 tool bar. 0 is the first button.
4166
4167 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 4168
f392e843 4169static gboolean
e4c8d29a
J
4170xg_tool_bar_help_callback (GtkWidget *w,
4171 GdkEventCrossing *event,
4172 gpointer client_data)
f392e843 4173{
d01a7826 4174 intptr_t idx = (intptr_t) client_data;
7d652d97 4175 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
f392e843
JD
4176 Lisp_Object help, frame;
4177
e69b0960 4178 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
d9301435 4179 return FALSE;
f392e843
JD
4180
4181 if (event->type == GDK_ENTER_NOTIFY)
4182 {
4183 idx *= TOOL_BAR_ITEM_NSLOTS;
e69b0960 4184 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
f392e843
JD
4185
4186 if (NILP (help))
e69b0960 4187 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
f392e843
JD
4188 }
4189 else
4190 help = Qnil;
4191
4192 XSETFRAME (frame, f);
4193 kbd_buffer_store_help_event (frame, help);
4194
4195 return FALSE;
4196}
4197
4198
f098b121
JD
4199/* This callback is called when a tool bar item shall be redrawn.
4200 It modifies the expose event so that the GtkImage widget redraws the
4201 whole image. This to overcome a bug that makes GtkImage draw the image
4202 in the wrong place when it tries to redraw just a part of the image.
4203 W is the GtkImage to be redrawn.
4204 EVENT is the expose event for W.
4205 CLIENT_DATA is unused.
4206
4207 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 4208
0afb4571 4209#ifndef HAVE_GTK3
f098b121 4210static gboolean
e4c8d29a
J
4211xg_tool_bar_item_expose_callback (GtkWidget *w,
4212 GdkEventExpose *event,
4213 gpointer client_data)
f098b121 4214{
b676f356
JD
4215 gint width, height;
4216
4217 gdk_drawable_get_size (event->window, &width, &height);
b676f356
JD
4218 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4219 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4220
810f2256
JD
4221 event->area.x = max (0, event->area.x);
4222 event->area.y = max (0, event->area.y);
c43923ad 4223
b676f356
JD
4224 event->area.width = max (width, event->area.width);
4225 event->area.height = max (height, event->area.height);
c43923ad 4226
f098b121
JD
4227 return FALSE;
4228}
0afb4571 4229#endif
f098b121 4230
bfeabdc3
JD
4231#ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4232#define toolbar_set_orientation(w, o) \
4233 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4234#else
4235#define toolbar_set_orientation(w, o) \
4236 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4237#endif
4238
5a1d858b
JD
4239#ifdef HAVE_GTK_HANDLE_BOX_NEW
4240#define TOOLBAR_TOP_WIDGET(x) ((x)->handlebox_widget)
4241#else
4242#define TOOLBAR_TOP_WIDGET(x) ((x)->toolbar_widget)
4243#endif
4244
1e39cbfb 4245/* Attach a tool bar to frame F. */
71bacd48 4246
f392e843 4247static void
a10c8269 4248xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
f392e843
JD
4249{
4250 struct x_output *x = f->output_data.x;
18e27ea8 4251 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
5a1d858b 4252 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
177c0ea7 4253
bfeabdc3
JD
4254 toolbar_set_orientation (x->toolbar_widget,
4255 into_hbox
4256 ? GTK_ORIENTATION_VERTICAL
4257 : GTK_ORIENTATION_HORIZONTAL);
5a1d858b 4258#ifdef HAVE_GTK_HANDLE_BOX_NEW
bfeabdc3
JD
4259 if (!x->handlebox_widget)
4260 {
5a1d858b 4261 top_widget = x->handlebox_widget = gtk_handle_box_new ();
bfeabdc3
JD
4262 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4263 G_CALLBACK (xg_tool_bar_detach_callback), f);
4264 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4265 G_CALLBACK (xg_tool_bar_attach_callback), f);
4266 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4267 x->toolbar_widget);
4268 }
5a1d858b 4269#endif
4721152c 4270
088c8c09 4271 if (into_hbox)
bfeabdc3 4272 {
5a1d858b 4273#ifdef HAVE_GTK_HANDLE_BOX_NEW
318a04c6
J
4274 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4275 GTK_POS_TOP);
5a1d858b
JD
4276#endif
4277 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
bfeabdc3
JD
4278 FALSE, FALSE, 0);
4279
4280 if (EQ (pos, Qleft))
4281 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
5a1d858b 4282 top_widget,
bfeabdc3 4283 0);
5a1d858b 4284 x->toolbar_in_hbox = true;
bfeabdc3
JD
4285 }
4286 else
4287 {
18e27ea8 4288 bool vbox_pos = x->menubar_widget != 0;
5a1d858b 4289#ifdef HAVE_GTK_HANDLE_BOX_NEW
318a04c6
J
4290 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4291 GTK_POS_LEFT);
5a1d858b
JD
4292#endif
4293 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
bfeabdc3
JD
4294 FALSE, FALSE, 0);
4295
4296 if (EQ (pos, Qtop))
4297 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
5a1d858b 4298 top_widget,
bfeabdc3 4299 vbox_pos);
5a1d858b 4300 x->toolbar_in_hbox = false;
bfeabdc3 4301 }
5a1d858b 4302 x->toolbar_is_packed = true;
1e39cbfb
JD
4303}
4304
a10c8269 4305static bool xg_update_tool_bar_sizes (struct frame *f);
51b3a99c
JD
4306
4307static void
4308tb_size_cb (GtkWidget *widget,
4309 GdkRectangle *allocation,
4310 gpointer user_data)
4311{
4312 /* When tool bar is created it has one preferred size. But when size is
4313 allocated between widgets, it may get another. So we must update
4314 size hints if tool bar size changes. Seen on Fedora 18 at least. */
7d652d97 4315 struct frame *f = user_data;
51b3a99c 4316 if (xg_update_tool_bar_sizes (f))
c4142a83 4317 xg_height_or_width_changed (f);
51b3a99c
JD
4318}
4319
1e39cbfb
JD
4320/* Create a tool bar for frame F. */
4321
4322static void
a10c8269 4323xg_create_tool_bar (struct frame *f)
1e39cbfb
JD
4324{
4325 struct x_output *x = f->output_data.x;
c9adfeaa
SF
4326#if GTK_CHECK_VERSION (3, 3, 6)
4327 GtkStyleContext *gsty;
4328#endif
ca06f160
JD
4329 struct xg_frame_tb_info *tbinfo
4330 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4331 TB_INFO_KEY);
4332 if (! tbinfo)
4333 {
4334 tbinfo = xmalloc (sizeof (*tbinfo));
4335 tbinfo->last_tool_bar = Qnil;
4336 tbinfo->style = Qnil;
4337 tbinfo->hmargin = tbinfo->vmargin = 0;
4338 tbinfo->dir = GTK_TEXT_DIR_NONE;
4339 tbinfo->n_last_items = 0;
4340 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4341 TB_INFO_KEY,
4342 tbinfo);
4343 }
1e39cbfb
JD
4344
4345 x->toolbar_widget = gtk_toolbar_new ();
f392e843 4346
f098b121
JD
4347 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4348
f098b121 4349 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
8a52f00a 4350 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
51b3a99c
JD
4351 g_signal_connect (x->toolbar_widget, "size-allocate",
4352 G_CALLBACK (tb_size_cb), f);
c9adfeaa
SF
4353#if GTK_CHECK_VERSION (3, 3, 6)
4354 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4355 gtk_style_context_add_class (gsty, "primary-toolbar");
4356#endif
1e39cbfb 4357}
f098b121 4358
177c0ea7 4359
e69b0960 4360#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
f392e843 4361
98a92193
JD
4362/* Find the right-to-left image named by RTL in the tool bar images for F.
4363 Returns IMAGE if RTL is not found. */
4364
4365static Lisp_Object
a10c8269 4366find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
98a92193
JD
4367{
4368 int i;
4369 Lisp_Object file, rtl_name;
4370 struct gcpro gcpro1, gcpro2;
4371 GCPRO2 (file, rtl_name);
4372
4373 rtl_name = Ffile_name_nondirectory (rtl);
4374
4375 for (i = 0; i < f->n_tool_bar_items; ++i)
4376 {
4377 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
8e5a8840 4378 if (!NILP (file = file_for_image (rtl_image)))
98a92193
JD
4379 {
4380 file = call1 (intern ("file-name-sans-extension"),
4381 Ffile_name_nondirectory (file));
67b77c0b 4382 if (! NILP (Fequal (file, rtl_name)))
98a92193
JD
4383 {
4384 image = rtl_image;
4385 break;
4386 }
4387 }
4388 }
4389
4390 return image;
4391}
4392
f904c0f9 4393static GtkToolItem *
a10c8269 4394xg_make_tool_item (struct frame *f,
f904c0f9
JD
4395 GtkWidget *wimage,
4396 GtkWidget **wbutton,
8ea90aa3 4397 const char *label,
18e27ea8 4398 int i, bool horiz, bool text_image)
f904c0f9
JD
4399{
4400 GtkToolItem *ti = gtk_tool_item_new ();
383b7c95
JD
4401 GtkWidget *vb = gtk_box_new (horiz
4402 ? GTK_ORIENTATION_HORIZONTAL
4403 : GTK_ORIENTATION_VERTICAL,
4404 0);
f904c0f9 4405 GtkWidget *wb = gtk_button_new ();
115b96bd 4406 /* The eventbox is here so we can have tooltips on disabled items. */
f904c0f9 4407 GtkWidget *weventbox = gtk_event_box_new ();
c9adfeaa
SF
4408#if GTK_CHECK_VERSION (3, 3, 6)
4409 GtkCssProvider *css_prov = gtk_css_provider_new ();
4410 GtkStyleContext *gsty;
4411
4412 gtk_css_provider_load_from_data (css_prov,
4413 "GtkEventBox {"
4414 " background-color: transparent;"
4415 "}",
4416 -1, NULL);
4417
4418 gsty = gtk_widget_get_style_context (weventbox);
4419 gtk_style_context_add_provider (gsty,
4420 GTK_STYLE_PROVIDER (css_prov),
4421 GTK_STYLE_PROVIDER_PRIORITY_USER);
4422 g_object_unref (css_prov);
4423#endif
f904c0f9 4424
383b7c95
JD
4425 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4426
3afff00e 4427 if (wimage && !text_image)
e547b051 4428 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
3afff00e
CY
4429 if (label)
4430 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
8a52f00a
JD
4431 if (wimage && text_image)
4432 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4433
f904c0f9
JD
4434 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4435 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4436 gtk_container_add (GTK_CONTAINER (wb), vb);
4437 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4438 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4439
fe0b6370 4440 if (wimage || label)
f904c0f9 4441 {
d01a7826 4442 intptr_t ii = i;
8ac068ac
PE
4443 gpointer gi = (gpointer) ii;
4444
f904c0f9
JD
4445 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4446 G_CALLBACK (xg_tool_bar_menu_proxy),
8ac068ac 4447 gi);
f904c0f9
JD
4448
4449 g_signal_connect (G_OBJECT (wb), "clicked",
4450 G_CALLBACK (xg_tool_bar_callback),
8ac068ac 4451 gi);
f904c0f9 4452
f904c0f9
JD
4453 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4454
0afb4571 4455#ifndef HAVE_GTK3
f904c0f9
JD
4456 /* Catch expose events to overcome an annoying redraw bug, see
4457 comment for xg_tool_bar_item_expose_callback. */
4458 g_signal_connect (G_OBJECT (ti),
4459 "expose-event",
4460 G_CALLBACK (xg_tool_bar_item_expose_callback),
4461 0);
0afb4571 4462#endif
f904c0f9
JD
4463 gtk_tool_item_set_homogeneous (ti, FALSE);
4464
e1dbe924 4465 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
f904c0f9
JD
4466 no distinction based on modifiers in the activate callback,
4467 so we have to do it ourselves. */
4468 g_signal_connect (wb, "button-release-event",
4469 G_CALLBACK (xg_tool_bar_button_cb),
4470 NULL);
4471
4472 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
088c8c09 4473
f904c0f9
JD
4474 /* Use enter/leave notify to show help. We use the events
4475 rather than the GtkButton specific signals "enter" and
4476 "leave", so we can have only one callback. The event
4477 will tell us what kind of event it is. */
f904c0f9
JD
4478 g_signal_connect (G_OBJECT (weventbox),
4479 "enter-notify-event",
4480 G_CALLBACK (xg_tool_bar_help_callback),
8ac068ac 4481 gi);
f904c0f9
JD
4482 g_signal_connect (G_OBJECT (weventbox),
4483 "leave-notify-event",
4484 G_CALLBACK (xg_tool_bar_help_callback),
8ac068ac 4485 gi);
f904c0f9 4486 }
088c8c09 4487
f904c0f9
JD
4488 if (wbutton) *wbutton = wb;
4489
4490 return ti;
4491}
4492
18e27ea8
PE
4493static bool
4494is_box_type (GtkWidget *vb, bool is_horizontal)
383b7c95
JD
4495{
4496#ifdef HAVE_GTK3
18e27ea8 4497 bool ret = 0;
38182d90 4498 if (GTK_IS_BOX (vb))
383b7c95
JD
4499 {
4500 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4501 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4502 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4503 }
4504 return ret;
4505#else
4506 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4507#endif
4508}
4509
4510
18e27ea8 4511static bool
3afff00e
CY
4512xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4513 const char *icon_name, const struct image *img,
18e27ea8 4514 const char *label, bool horiz)
f904c0f9 4515{
3afff00e 4516 gpointer old;
e547b051 4517 GtkWidget *wimage;
3afff00e 4518 GtkWidget *vb = XG_BIN_CHILD (wbutton);
e547b051 4519 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
f904c0f9 4520
3afff00e 4521 /* Check if the tool icon matches. */
fe0b6370 4522 if (stock_name && wimage)
3afff00e
CY
4523 {
4524 old = g_object_get_data (G_OBJECT (wimage),
4525 XG_TOOL_BAR_STOCK_NAME);
4526 if (!old || strcmp (old, stock_name))
4527 return 1;
4528 }
fe0b6370 4529 else if (icon_name && wimage)
f904c0f9 4530 {
3afff00e
CY
4531 old = g_object_get_data (G_OBJECT (wimage),
4532 XG_TOOL_BAR_ICON_NAME);
4533 if (!old || strcmp (old, icon_name))
4534 return 1;
f904c0f9 4535 }
fe0b6370 4536 else if (wimage)
3afff00e 4537 {
1e5524e7 4538 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
383b7c95 4539 XG_TOOL_BAR_IMAGE_DATA);
1e5524e7 4540 Pixmap old_img = (Pixmap) gold_img;
3afff00e
CY
4541 if (old_img != img->pixmap)
4542 return 1;
4543 }
4544
4545 /* Check button configuration and label. */
383b7c95 4546 if (is_box_type (vb, horiz)
3afff00e
CY
4547 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4548 return 1;
f904c0f9 4549
3afff00e 4550 /* Ensure label is correct. */
fe0b6370 4551 if (label && wlbl)
3afff00e
CY
4552 gtk_label_set_text (GTK_LABEL (wlbl), label);
4553 return 0;
f904c0f9
JD
4554}
4555
18e27ea8 4556static bool
a10c8269 4557xg_update_tool_bar_sizes (struct frame *f)
bfeabdc3
JD
4558{
4559 struct x_output *x = f->output_data.x;
4560 GtkRequisition req;
4561 int nl = 0, nr = 0, nt = 0, nb = 0;
5a1d858b 4562 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
bfeabdc3 4563
5a1d858b 4564 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
bfeabdc3
JD
4565 if (x->toolbar_in_hbox)
4566 {
4567 int pos;
4568 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
5a1d858b 4569 top_widget,
bfeabdc3
JD
4570 "position", &pos, NULL);
4571 if (pos == 0) nl = req.width;
4572 else nr = req.width;
4573 }
4574 else
4575 {
4576 int pos;
4577 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
5a1d858b 4578 top_widget,
bfeabdc3
JD
4579 "position", &pos, NULL);
4580 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4581 else nb = req.height;
4582 }
088c8c09 4583
bfeabdc3
JD
4584 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4585 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4586 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4587 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4588 {
4589 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4590 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4591 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4592 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4593 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4594 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4595 return 1;
4596 }
4597
4598 return 0;
4599}
4600
f904c0f9 4601
71bacd48
JD
4602/* Update the tool bar for frame F. Add new buttons and remove old. */
4603
f392e843 4604void
a10c8269 4605update_frame_tool_bar (struct frame *f)
f392e843 4606{
3afff00e 4607 int i, j;
f392e843 4608 struct x_output *x = f->output_data.x;
bfc2a7d1 4609 int hmargin = 0, vmargin = 0;
98a92193 4610 GtkToolbar *wtoolbar;
6e1440e6 4611 GtkToolItem *ti;
98a92193 4612 GtkTextDirection dir;
3afff00e 4613 Lisp_Object style;
18e27ea8 4614 bool text_image, horiz;
ca06f160 4615 struct xg_frame_tb_info *tbinfo;
3afff00e 4616
f392e843
JD
4617 if (! FRAME_GTK_WIDGET (f))
4618 return;
4619
4d7e6e51 4620 block_input ();
177c0ea7 4621
d311d28c 4622 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
133c0116
JD
4623 {
4624 hmargin = XFASTINT (Vtool_bar_button_margin);
4625 vmargin = XFASTINT (Vtool_bar_button_margin);
4626 }
4627 else if (CONSP (Vtool_bar_button_margin))
4628 {
d311d28c 4629 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
133c0116
JD
4630 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4631
d311d28c 4632 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
133c0116
JD
4633 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4634 }
4635
4636 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4637 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4638 i.e. zero. This means that margins less than
4639 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4640 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4641 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
c43923ad 4642
f392e843
JD
4643 if (! x->toolbar_widget)
4644 xg_create_tool_bar (f);
4645
98a92193 4646 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
f904c0f9 4647 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
088c8c09 4648
3afff00e 4649 style = Ftool_bar_get_system_style ();
ca06f160
JD
4650
4651 /* Are we up to date? */
4652 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4653 TB_INFO_KEY);
4654
4655 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4656 && tbinfo->n_last_items == f->n_tool_bar_items
4657 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4658 && tbinfo->dir == dir
67b77c0b
AS
4659 && ! NILP (Fequal (tbinfo->style, style))
4660 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
ca06f160 4661 {
4d7e6e51 4662 unblock_input ();
ca06f160
JD
4663 return;
4664 }
4665
4666 tbinfo->last_tool_bar = f->tool_bar_items;
4667 tbinfo->n_last_items = f->n_tool_bar_items;
4668 tbinfo->style = style;
4669 tbinfo->hmargin = hmargin;
4670 tbinfo->vmargin = vmargin;
4671 tbinfo->dir = dir;
4672
3afff00e
CY
4673 text_image = EQ (style, Qtext_image_horiz);
4674 horiz = EQ (style, Qboth_horiz) || text_image;
4675
4676 for (i = j = 0; i < f->n_tool_bar_items; ++i)
f392e843 4677 {
18e27ea8
PE
4678 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4679 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
f392e843 4680 int idx;
13464394 4681 ptrdiff_t img_id;
98a92193
JD
4682 int icon_size = 0;
4683 struct image *img = NULL;
f392e843 4684 Lisp_Object image;
495effe5 4685 Lisp_Object stock = Qnil;
98a92193
JD
4686 GtkStockItem stock_item;
4687 char *stock_name = NULL;
2154c964 4688 char *icon_name = NULL;
98a92193 4689 Lisp_Object rtl;
b42ff099 4690 GtkWidget *wbutton = NULL;
d4ad8c04 4691 Lisp_Object specified_file;
18e27ea8 4692 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
3afff00e
CY
4693 const char *label
4694 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4695 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4696 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4697 : "";
d2bd5189 4698
3afff00e 4699 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
6e1440e6 4700
4039c786
CY
4701 /* If this is a separator, use a gtk separator item. */
4702 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4703 {
4704 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4705 {
4706 if (ti)
4707 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4708 GTK_WIDGET (ti));
4709 ti = gtk_separator_tool_item_new ();
3afff00e 4710 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4039c786 4711 }
3afff00e 4712 j++;
4039c786
CY
4713 continue;
4714 }
4715
4716 /* Otherwise, the tool-bar item is an ordinary button. */
4717
4718 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4719 {
4720 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4721 ti = NULL;
4722 }
f904c0f9 4723
3afff00e 4724 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
f392e843
JD
4725
4726 /* Ignore invalid image specifications. */
4039c786 4727 image = PROP (TOOL_BAR_ITEM_IMAGES);
f392e843
JD
4728 if (!valid_image_p (image))
4729 {
3afff00e
CY
4730 if (ti)
4731 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4732 GTK_WIDGET (ti));
f392e843
JD
4733 continue;
4734 }
4735
d4ad8c04 4736 specified_file = file_for_image (image);
4e6b227d
CY
4737 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4738 stock = call1 (Qx_gtk_map_stock, specified_file);
f392e843 4739
4e6b227d 4740 if (STRINGP (stock))
98a92193
JD
4741 {
4742 stock_name = SSDATA (stock);
2154c964
JD
4743 if (stock_name[0] == 'n' && stock_name[1] == ':')
4744 {
4745 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4746 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4747
4748 icon_name = stock_name + 2;
4749 stock_name = NULL;
4750 stock = Qnil;
4751
4752 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4753 icon_name = NULL;
4754 else
4755 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4756 }
4757 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
f904c0f9 4758 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
8e5a8840 4759 else
2154c964
JD
4760 {
4761 stock = Qnil;
4762 stock_name = NULL;
4763 }
98a92193 4764 }
2154c964
JD
4765
4766 if (stock_name == NULL && icon_name == NULL)
1d1885fc 4767 {
3afff00e
CY
4768 /* No stock image, or stock item not known. Try regular
4769 image. If image is a vector, choose it according to the
98a92193
JD
4770 button state. */
4771 if (dir == GTK_TEXT_DIR_RTL
4772 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4773 && STRINGP (rtl))
3afff00e 4774 image = find_rtl_image (f, image, rtl);
98a92193
JD
4775
4776 if (VECTORP (image))
4777 {
4778 if (enabled_p)
4779 idx = (selected_p
4780 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4781 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4782 else
4783 idx = (selected_p
4784 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4785 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4786
a54e2c05 4787 eassert (ASIZE (image) >= idx);
98a92193
JD
4788 image = AREF (image, idx);
4789 }
4790 else
4791 idx = -1;
4792
4793 img_id = lookup_image (f, image);
4794 img = IMAGE_FROM_ID (f, img_id);
4795 prepare_image_for_display (f, img);
8e5a8840 4796
98a92193
JD
4797 if (img->load_failed_p || img->pixmap == None)
4798 {
f904c0f9 4799 if (ti)
3afff00e
CY
4800 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4801 GTK_WIDGET (ti));
f904c0f9 4802 continue;
6e1440e6 4803 }
1d1885fc 4804 }
177c0ea7 4805
3afff00e
CY
4806 /* If there is an existing widget, check if it's stale; if so,
4807 remove it and make a new tool item from scratch. */
4808 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4809 img, label, horiz))
4810 {
4811 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4812 GTK_WIDGET (ti));
4813 ti = NULL;
4814 }
4815
6e1440e6 4816 if (ti == NULL)
ff24abfe 4817 {
98a92193 4818 GtkWidget *w;
3afff00e
CY
4819
4820 /* Save the image so we can see if an update is needed the
4821 next time we call xg_tool_item_match_p. */
4822 if (EQ (style, Qtext))
4823 w = NULL;
4824 else if (stock_name)
98a92193
JD
4825 {
4826 w = gtk_image_new_from_stock (stock_name, icon_size);
4827 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4828 (gpointer) xstrdup (stock_name),
4829 (GDestroyNotify) xfree);
4830 }
8e5a8840 4831 else if (icon_name)
2154c964
JD
4832 {
4833 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4834 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4835 (gpointer) xstrdup (icon_name),
4836 (GDestroyNotify) xfree);
4837 }
98a92193
JD
4838 else
4839 {
4840 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
98a92193
JD
4841 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4842 (gpointer)img->pixmap);
4843 }
4844
3afff00e
CY
4845 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4846 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4847 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
f904c0f9 4848 }
177c0ea7 4849
f392e843 4850#undef PROP
3afff00e
CY
4851
4852 gtk_widget_set_sensitive (wbutton, enabled_p);
4853 j++;
f392e843
JD
4854 }
4855
3afff00e 4856 /* Remove buttons not longer needed. */
6e1440e6 4857 do
f392e843 4858 {
21a76236 4859 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
3afff00e
CY
4860 if (ti)
4861 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
6e1440e6 4862 } while (ti != NULL);
f392e843 4863
bfeabdc3 4864 if (f->n_tool_bar_items != 0)
f392e843 4865 {
5a1d858b 4866 if (! x->toolbar_is_packed)
e69b0960 4867 xg_pack_tool_bar (f, f->tool_bar_position);
5a1d858b 4868 gtk_widget_show_all (TOOLBAR_TOP_WIDGET (x));
bfeabdc3
JD
4869 if (xg_update_tool_bar_sizes (f))
4870 xg_height_or_width_changed (f);
f392e843 4871 }
bfeabdc3 4872
4d7e6e51 4873 unblock_input ();
f392e843
JD
4874}
4875
71bacd48
JD
4876/* Deallocate all resources for the tool bar on frame F.
4877 Remove the tool bar. */
4878
f392e843 4879void
a10c8269 4880free_frame_tool_bar (struct frame *f)
f392e843
JD
4881{
4882 struct x_output *x = f->output_data.x;
4883
4884 if (x->toolbar_widget)
4885 {
ca06f160 4886 struct xg_frame_tb_info *tbinfo;
5a1d858b
JD
4887 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
4888
4d7e6e51 4889 block_input ();
d3b2a6da
JD
4890 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4891 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
5a1d858b 4892 if (x->toolbar_is_packed)
bfeabdc3
JD
4893 {
4894 if (x->toolbar_in_hbox)
4895 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
5a1d858b 4896 top_widget);
bfeabdc3
JD
4897 else
4898 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
5a1d858b 4899 top_widget);
bfeabdc3 4900 }
d3b2a6da
JD
4901 else
4902 gtk_widget_destroy (x->toolbar_widget);
4903
f392e843 4904 x->toolbar_widget = 0;
5a1d858b
JD
4905 TOOLBAR_TOP_WIDGET (x) = 0;
4906 x->toolbar_is_packed = false;
bfeabdc3
JD
4907 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4908 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4909
ca06f160
JD
4910 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4911 TB_INFO_KEY);
4912 if (tbinfo)
4913 {
4914 xfree (tbinfo);
4915 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4916 TB_INFO_KEY,
4917 NULL);
4918 }
4919
bfeabdc3 4920 xg_height_or_width_changed (f);
f392e843 4921
4d7e6e51 4922 unblock_input ();
f392e843
JD
4923 }
4924}
4925
18e27ea8 4926void
a10c8269 4927xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
bfeabdc3
JD
4928{
4929 struct x_output *x = f->output_data.x;
5a1d858b 4930 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
bfeabdc3 4931
5a1d858b 4932 if (! x->toolbar_widget || ! top_widget)
18e27ea8 4933 return;
bfeabdc3 4934
4d7e6e51 4935 block_input ();
5a1d858b
JD
4936 g_object_ref (top_widget);
4937 if (x->toolbar_is_packed)
4938 {
4939 if (x->toolbar_in_hbox)
4940 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4941 top_widget);
4942 else
4943 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4944 top_widget);
4945 }
4946
bfeabdc3 4947 xg_pack_tool_bar (f, pos);
5a1d858b 4948 g_object_unref (top_widget);
bfeabdc3
JD
4949 if (xg_update_tool_bar_sizes (f))
4950 xg_height_or_width_changed (f);
4951
4d7e6e51 4952 unblock_input ();
bfeabdc3
JD
4953}
4954
f392e843
JD
4955
4956\f
4957/***********************************************************************
4958 Initializing
f904c0f9 4959***********************************************************************/
f392e843 4960void
971de7fb 4961xg_initialize (void)
f392e843 4962{
9f6fcdc5 4963 GtkBindingSet *binding_set;
383b7c95 4964 GtkSettings *settings;
9f6fcdc5 4965
430e6c77
JD
4966#if HAVE_XFT
4967 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4968 we keep it permanently linked in. */
4969 XftInit (0);
4970#endif
879ffad9
JD
4971
4972 gdpy_def = NULL;
f392e843 4973 xg_ignore_gtk_scrollbar = 0;
8b745d92 4974#ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
da18b5ac 4975 xg_detached_menus = 0;
8b745d92 4976#endif
f392e843
JD
4977 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4978 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4979
81e302ef
JD
4980 id_to_widget.max_size = id_to_widget.used = 0;
4981 id_to_widget.widgets = 0;
4982
383b7c95
JD
4983 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4984 (gdk_display_get_default ()));
f392e843
JD
4985 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4986 bindings. It doesn't seem to be any way to remove properties,
efec3894 4987 so we set it to "" which in means "no key". */
383b7c95 4988 gtk_settings_set_string_property (settings,
f392e843 4989 "gtk-menu-bar-accel",
efec3894 4990 "",
f392e843 4991 EMACS_CLASS);
81e302ef
JD
4992
4993 /* Make GTK text input widgets use Emacs style keybindings. This is
4994 Emacs after all. */
383b7c95 4995 gtk_settings_set_string_property (settings,
81e302ef
JD
4996 "gtk-key-theme-name",
4997 "Emacs",
4998 EMACS_CLASS);
9f6fcdc5
JD
4999
5000 /* Make dialogs close on C-g. Since file dialog inherits from
5001 dialog, this works for them also. */
dc2933eb 5002 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
0afb4571 5003 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
9f6fcdc5
JD
5004 "close", 0);
5005
5006 /* Make menus close on C-g. */
dc2933eb
JD
5007 binding_set = gtk_binding_set_by_class (g_type_class_ref
5008 (GTK_TYPE_MENU_SHELL));
0afb4571 5009 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
9f6fcdc5 5010 "cancel", 0);
c195f2de 5011 update_theme_scrollbar_width ();
f2045622 5012
48660ca5 5013#ifdef HAVE_FREETYPE
f2045622 5014 x_last_font_name = NULL;
48660ca5 5015#endif
f392e843
JD
5016}
5017
5018#endif /* USE_GTK */