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