Drop FGET and revert read access to Lisp_Objects slots of struct frame.
[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. */
e69b0960
DA
1151 if (! NILP (f->title))
1152 title = SSDATA (ENCODE_UTF_8 (f->title));
1153 else if (! NILP (f->name))
1154 title = SSDATA (ENCODE_UTF_8 (f->name));
f392e843
JD
1155
1156 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
177c0ea7 1157
f392e843
JD
1158 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1159 FRAME_GTK_WIDGET (f) = wfixed;
1160 f->output_data.x->vbox_widget = wvbox;
bfeabdc3 1161 f->output_data.x->hbox_widget = whbox;
f392e843 1162
e547b051 1163 gtk_widget_set_has_window (wfixed, TRUE);
f392e843 1164
f392e843 1165 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
bfeabdc3
JD
1166 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1167 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
f392e843
JD
1168
1169 if (FRAME_EXTERNAL_TOOL_BAR (f))
1170 update_frame_tool_bar (f);
1171
7863d625
JD
1172 /* We don't want this widget double buffered, because we draw on it
1173 with regular X drawing primitives, so from a GTK/GDK point of
1174 view, the widget is totally blank. When an expose comes, this
1175 will make the widget blank, and then Emacs redraws it. This flickers
1176 a lot, so we turn off double buffering. */
f392e843 1177 gtk_widget_set_double_buffered (wfixed, FALSE);
7863d625 1178
f392e843 1179 gtk_window_set_wmclass (GTK_WINDOW (wtop),
93c579e0
JD
1180 SSDATA (Vx_resource_name),
1181 SSDATA (Vx_resource_class));
f392e843
JD
1182
1183 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1184 GTK is to destroy the widget. We want Emacs to do that instead. */
1185 g_signal_connect (G_OBJECT (wtop), "delete-event",
0afb4571 1186 G_CALLBACK (delete_cb), f);
177c0ea7 1187
f392e843
JD
1188 /* Convert our geometry parameters into a geometry string
1189 and specify it.
1190 GTK will itself handle calculating the real position this way. */
1191 xg_set_geometry (f);
32e737d7
JD
1192 f->win_gravity
1193 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
f392e843
JD
1194
1195 gtk_widget_add_events (wfixed,
1196 GDK_POINTER_MOTION_MASK
1197 | GDK_EXPOSURE_MASK
1198 | GDK_BUTTON_PRESS_MASK
1199 | GDK_BUTTON_RELEASE_MASK
1200 | GDK_KEY_PRESS_MASK
1201 | GDK_ENTER_NOTIFY_MASK
1202 | GDK_LEAVE_NOTIFY_MASK
1203 | GDK_FOCUS_CHANGE_MASK
1204 | GDK_STRUCTURE_MASK
1205 | GDK_VISIBILITY_NOTIFY_MASK);
1206
1207 /* Must realize the windows so the X window gets created. It is used
1208 by callers of this function. */
1209 gtk_widget_realize (wfixed);
1210 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1211
1212 /* Since GTK clears its window by filling with the background color,
1213 we must keep X and GTK background in sync. */
0afb4571 1214 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
f392e843 1215
0afb4571 1216#ifndef HAVE_GTK3
f392e843
JD
1217 /* Also, do not let any background pixmap to be set, this looks very
1218 bad as Emacs overwrites the background pixmap with its own idea
1219 of background color. */
1220 style = gtk_widget_get_modifier_style (wfixed);
1221
1222 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1223 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1224 gtk_widget_modify_style (wfixed, style);
0afb4571
J
1225#else
1226 gtk_widget_set_can_focus (wfixed, TRUE);
c195f2de 1227 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
0afb4571 1228#endif
177c0ea7 1229
ac01763e 1230#ifdef USE_GTK_TOOLTIP
aa1859f5
J
1231 /* Steal a tool tip window we can move ourselves. */
1232 f->output_data.x->ttip_widget = 0;
1233 f->output_data.x->ttip_lbl = 0;
1234 f->output_data.x->ttip_window = 0;
088c8c09 1235 gtk_widget_set_tooltip_text (wtop, "Dummy text");
aa1859f5 1236 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
ac01763e 1237#endif
f392e843 1238
3a46642b
J
1239 {
1240 GdkScreen *screen = gtk_widget_get_screen (wtop);
1241 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1242 /* Only connect this signal once per screen. */
1243 if (! g_signal_handler_find (G_OBJECT (gs),
1244 G_SIGNAL_MATCH_FUNC,
1245 0, 0, 0,
1246 G_CALLBACK (style_changed_cb),
1247 0))
1248 {
1249 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1250 G_CALLBACK (style_changed_cb),
1251 gdk_screen_get_display (screen));
1252 }
1253 }
1254
f392e843
JD
1255 UNBLOCK_INPUT;
1256
1257 return 1;
1258}
1259
aa1859f5
J
1260void
1261xg_free_frame_widgets (FRAME_PTR f)
1262{
1263 if (FRAME_GTK_OUTER_WIDGET (f))
1264 {
b0afc268 1265#ifdef USE_GTK_TOOLTIP
aa1859f5 1266 struct x_output *x = f->output_data.x;
b0afc268 1267#endif
aa1859f5
J
1268 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1269 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1270 FRAME_GTK_OUTER_WIDGET (f) = 0;
ac01763e 1271#ifdef USE_GTK_TOOLTIP
aa1859f5
J
1272 if (x->ttip_lbl)
1273 gtk_widget_destroy (x->ttip_lbl);
1274 if (x->ttip_widget)
1275 g_object_unref (G_OBJECT (x->ttip_widget));
ac01763e 1276#endif
aa1859f5
J
1277 }
1278}
1279
f392e843
JD
1280/* Set the normal size hints for the window manager, for frame F.
1281 FLAGS is the flags word to use--or 0 meaning preserve the flags
1282 that the window now has.
1283 If USER_POSITION is nonzero, we set the User Position
1284 flag (this is useful when FLAGS is 0). */
71bacd48 1285
f392e843 1286void
971de7fb 1287x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
f392e843 1288{
3e5fc571
CY
1289 /* Must use GTK routines here, otherwise GTK resets the size hints
1290 to its own defaults. */
1291 GdkGeometry size_hints;
1292 gint hint_flags = 0;
1293 int base_width, base_height;
1294 int min_rows = 0, min_cols = 0;
1295 int win_gravity = f->win_gravity;
1296
32e737d7
JD
1297 /* Don't set size hints during initialization; that apparently leads
1298 to a race condition. See the thread at
1299 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1300 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1301 return;
1302
3e5fc571
CY
1303 if (flags)
1304 {
1305 memset (&size_hints, 0, sizeof (size_hints));
1306 f->output_data.x->size_hints = size_hints;
1307 f->output_data.x->hint_flags = hint_flags;
1308 }
1309 else
1310 flags = f->size_hint_flags;
1311
1312 size_hints = f->output_data.x->size_hints;
1313 hint_flags = f->output_data.x->hint_flags;
1314
1315 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1316 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
1317 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
1318
1319 hint_flags |= GDK_HINT_BASE_SIZE;
bfeabdc3 1320 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
89c94350
JD
1321 /* Use one row here so base_height does not become zero.
1322 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1323 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
3e5fc571
CY
1324 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1325
1326 check_frame_size (f, &min_rows, &min_cols);
89c94350 1327 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
3e5fc571
CY
1328
1329 size_hints.base_width = base_width;
1330 size_hints.base_height = base_height;
1331 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
1332 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
1333
1334 /* These currently have a one to one mapping with the X values, but I
1335 don't think we should rely on that. */
1336 hint_flags |= GDK_HINT_WIN_GRAVITY;
1337 size_hints.win_gravity = 0;
1338 if (win_gravity == NorthWestGravity)
1339 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1340 else if (win_gravity == NorthGravity)
1341 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1342 else if (win_gravity == NorthEastGravity)
1343 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1344 else if (win_gravity == WestGravity)
1345 size_hints.win_gravity = GDK_GRAVITY_WEST;
1346 else if (win_gravity == CenterGravity)
1347 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1348 else if (win_gravity == EastGravity)
1349 size_hints.win_gravity = GDK_GRAVITY_EAST;
1350 else if (win_gravity == SouthWestGravity)
1351 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1352 else if (win_gravity == SouthGravity)
1353 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1354 else if (win_gravity == SouthEastGravity)
1355 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1356 else if (win_gravity == StaticGravity)
1357 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1358
22aa44a8
JD
1359 if (user_position)
1360 {
1361 hint_flags &= ~GDK_HINT_POS;
1362 hint_flags |= GDK_HINT_USER_POS;
1363 }
1364
3e5fc571
CY
1365 if (hint_flags != f->output_data.x->hint_flags
1366 || memcmp (&size_hints,
1367 &f->output_data.x->size_hints,
1368 sizeof (size_hints)) != 0)
1369 {
1370 BLOCK_INPUT;
1371 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
c7e73be5 1372 NULL, &size_hints, hint_flags);
3e5fc571
CY
1373 f->output_data.x->size_hints = size_hints;
1374 f->output_data.x->hint_flags = hint_flags;
1375 UNBLOCK_INPUT;
1376 }
f392e843
JD
1377}
1378
1379/* Change background color of a frame.
6772c8e1 1380 Since GTK uses the background color to clear the window, we must
f392e843
JD
1381 keep the GTK and X colors in sync.
1382 F is the frame to change,
1383 BG is the pixel value to change to. */
71bacd48 1384
f392e843 1385void
971de7fb 1386xg_set_background_color (FRAME_PTR f, long unsigned int bg)
f392e843
JD
1387{
1388 if (FRAME_GTK_WIDGET (f))
1389 {
f392e843 1390 BLOCK_INPUT;
0afb4571 1391 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
f392e843
JD
1392 UNBLOCK_INPUT;
1393 }
1394}
1395
1396
1001243b
JD
1397/* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1398 functions so GTK does not overwrite the icon. */
1399
1400void
971de7fb 1401xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
1001243b 1402{
0afb4571
J
1403 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1404 icon_pixmap,
1405 icon_mask);
1406 if (gp)
1001243b
JD
1407 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1408}
1409
1410
f392e843
JD
1411\f
1412/***********************************************************************
1413 Dialog functions
1414 ***********************************************************************/
1415/* Return the dialog title to use for a dialog of type KEY.
1416 This is the encoding used by lwlib. We use the same for GTK. */
71bacd48 1417
8ea90aa3 1418static const char *
f392e843
JD
1419get_dialog_title (char key)
1420{
8ea90aa3 1421 const char *title = "";
177c0ea7 1422
f392e843
JD
1423 switch (key) {
1424 case 'E': case 'e':
1425 title = "Error";
1426 break;
1427
1428 case 'I': case 'i':
1429 title = "Information";
1430 break;
1431
1432 case 'L': case 'l':
1433 title = "Prompt";
1434 break;
1435
1436 case 'P': case 'p':
1437 title = "Prompt";
1438 break;
1439
1440 case 'Q': case 'q':
1441 title = "Question";
1442 break;
1443 }
1444
1445 return title;
1446}
1447
1448/* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1449 the dialog, but return TRUE so the event does not propagate further
1450 in GTK. This prevents GTK from destroying the dialog widget automatically
4c36be58 1451 and we can always destroy the widget manually, regardless of how
f392e843
JD
1452 it was popped down (button press or WM_DELETE_WINDOW).
1453 W is the dialog widget.
1454 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1455 user_data is NULL (not used).
1456
1457 Returns TRUE to end propagation of event. */
71bacd48 1458
f392e843 1459static gboolean
971de7fb 1460dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
f392e843
JD
1461{
1462 gtk_widget_unmap (w);
1463 return TRUE;
1464}
1465
1466/* Create a popup dialog window. See also xg_create_widget below.
1467 WV is a widget_value describing the dialog.
1468 SELECT_CB is the callback to use when a button has been pressed.
1469 DEACTIVATE_CB is the callback to use when the dialog pops down.
1470
1471 Returns the GTK dialog widget. */
71bacd48 1472
f392e843 1473static GtkWidget *
e4c8d29a
J
1474create_dialog (widget_value *wv,
1475 GCallback select_cb,
1476 GCallback deactivate_cb)
f392e843 1477{
8ea90aa3 1478 const char *title = get_dialog_title (wv->name[0]);
f392e843
JD
1479 int total_buttons = wv->name[1] - '0';
1480 int right_buttons = wv->name[4] - '0';
1481 int left_buttons;
1482 int button_nr = 0;
1483 int button_spacing = 10;
1484 GtkWidget *wdialog = gtk_dialog_new ();
e547b051
J
1485 GtkDialog *wd = GTK_DIALOG (wdialog);
1486 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
f392e843 1487 widget_value *item;
f392e843
JD
1488 GtkWidget *whbox_down;
1489
1490 /* If the number of buttons is greater than 4, make two rows of buttons
1491 instead. This looks better. */
1492 int make_two_rows = total_buttons > 4;
1493
1494 if (right_buttons == 0) right_buttons = total_buttons/2;
1495 left_buttons = total_buttons - right_buttons;
1496
1497 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1498 gtk_widget_set_name (wdialog, "emacs-dialog");
1499
f392e843
JD
1500
1501 if (make_two_rows)
1502 {
383b7c95
JD
1503 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1504 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1505 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1506 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1507 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1508 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
f392e843
JD
1509
1510 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1511 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1512 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1513
1514 cur_box = GTK_BOX (whbox_up);
1515 }
1516
1517 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1518 G_CALLBACK (dialog_delete_callback), 0);
177c0ea7 1519
f392e843
JD
1520 if (deactivate_cb)
1521 {
1522 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1523 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1524 }
1525
1526 for (item = wv->contents; item; item = item->next)
1527 {
1528 char *utf8_label = get_utf8_string (item->value);
1529 GtkWidget *w;
1530 GtkRequisition req;
1531
0a1d6de0 1532 if (item->name && strcmp (item->name, "message") == 0)
f392e843 1533 {
e547b051 1534 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
f392e843
JD
1535 /* This is the text part of the dialog. */
1536 w = gtk_label_new (utf8_label);
e547b051
J
1537 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1538 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
f392e843
JD
1539 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1540
1541 /* Try to make dialog look better. Must realize first so
1542 the widget can calculate the size it needs. */
1543 gtk_widget_realize (w);
0afb4571 1544 gtk_widget_get_preferred_size (w, NULL, &req);
e547b051 1545 gtk_box_set_spacing (wvbox, req.height);
0a1d6de0 1546 if (item->value && strlen (item->value) > 0)
f392e843
JD
1547 button_spacing = 2*req.width/strlen (item->value);
1548 }
1549 else
1550 {
1551 /* This is one button to add to the dialog. */
4b1b4443 1552 w = gtk_button_new_with_label (utf8_label);
f392e843
JD
1553 if (! item->enabled)
1554 gtk_widget_set_sensitive (w, FALSE);
1555 if (select_cb)
1556 g_signal_connect (G_OBJECT (w), "clicked",
1557 select_cb, item->call_data);
1558
1559 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1560 if (++button_nr == left_buttons)
1561 {
1562 if (make_two_rows)
1563 cur_box = GTK_BOX (whbox_down);
1564 else
1565 gtk_box_pack_start (cur_box,
1566 gtk_label_new (""),
1567 TRUE, TRUE,
1568 button_spacing);
1569 }
1570 }
1571
42ca4633 1572 if (utf8_label)
f392e843
JD
1573 g_free (utf8_label);
1574 }
1575
1576 return wdialog;
1577}
1578
e90292a9 1579struct xg_dialog_data
90f2e16b 1580{
e90292a9
JD
1581 GMainLoop *loop;
1582 int response;
1583 GtkWidget *w;
1584 guint timerid;
1585};
90f2e16b 1586
3402c0a6 1587/* Function that is called when the file or font dialogs pop down.
457a8155 1588 W is the dialog widget, RESPONSE is the response code.
e90292a9 1589 USER_DATA is what we passed in to g_signal_connect. */
457a8155
JD
1590
1591static void
dd4c5104
DN
1592xg_dialog_response_cb (GtkDialog *w,
1593 gint response,
1594 gpointer user_data)
457a8155 1595{
e90292a9
JD
1596 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1597 dd->response = response;
1598 g_main_loop_quit (dd->loop);
457a8155
JD
1599}
1600
1601
1602/* Destroy the dialog. This makes it pop down. */
1603
1604static Lisp_Object
971de7fb 1605pop_down_dialog (Lisp_Object arg)
457a8155
JD
1606{
1607 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
e90292a9
JD
1608 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1609
cd2531db 1610 BLOCK_INPUT;
e90292a9
JD
1611 if (dd->w) gtk_widget_destroy (dd->w);
1612 if (dd->timerid != 0) g_source_remove (dd->timerid);
1613
1614 g_main_loop_quit (dd->loop);
1615 g_main_loop_unref (dd->loop);
088c8c09 1616
cd2531db 1617 UNBLOCK_INPUT;
e90292a9 1618
457a8155
JD
1619 return Qnil;
1620}
1621
e90292a9
JD
1622/* If there are any emacs timers pending, add a timeout to main loop in DATA.
1623 We pass in DATA as gpointer* so we can use this as a callback. */
1624
1625static gboolean
971de7fb 1626xg_maybe_add_timer (gpointer data)
e90292a9
JD
1627{
1628 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
f868cd8a 1629 EMACS_TIME next_time = timer_check ();
e90292a9
JD
1630
1631 dd->timerid = 0;
1632
d35af63c 1633 if (EMACS_TIME_VALID_P (next_time))
e90292a9 1634 {
d35af63c
PE
1635 time_t s = EMACS_SECS (next_time);
1636 int per_ms = EMACS_TIME_RESOLUTION / 1000;
1637 int ms = (EMACS_NSECS (next_time) + per_ms - 1) / per_ms;
1638 if (s <= ((guint) -1 - ms) / 1000)
1639 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
e90292a9
JD
1640 }
1641 return FALSE;
1642}
1643
088c8c09 1644
e90292a9
JD
1645/* Pops up a modal dialog W and waits for response.
1646 We don't use gtk_dialog_run because we want to process emacs timers.
1647 The dialog W is not destroyed when this function returns. */
1648
1649static int
971de7fb 1650xg_dialog_run (FRAME_PTR f, GtkWidget *w)
e90292a9 1651{
d311d28c 1652 ptrdiff_t count = SPECPDL_INDEX ();
e90292a9
JD
1653 struct xg_dialog_data dd;
1654
1655 xg_set_screen (w, f);
1656 gtk_window_set_transient_for (GTK_WINDOW (w),
1657 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1658 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1659 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1660
1661 dd.loop = g_main_loop_new (NULL, FALSE);
1662 dd.response = GTK_RESPONSE_CANCEL;
1663 dd.w = w;
1664 dd.timerid = 0;
1665
1666 g_signal_connect (G_OBJECT (w),
1667 "response",
1668 G_CALLBACK (xg_dialog_response_cb),
1669 &dd);
1670 /* Don't destroy the widget if closed by the window manager close button. */
1671 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1672 gtk_widget_show (w);
1673
1674 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1675
1676 (void) xg_maybe_add_timer (&dd);
1677 g_main_loop_run (dd.loop);
088c8c09 1678
e90292a9
JD
1679 dd.w = 0;
1680 unbind_to (count, Qnil);
1681
1682 return dd.response;
1683}
1684
1685\f
1686/***********************************************************************
1687 File dialog functions
1688 ***********************************************************************/
1689/* Return non-zero if the old file selection dialog is being used.
1690 Return zero if not. */
1691
1692int
971de7fb 1693xg_uses_old_file_dialog (void)
e90292a9 1694{
e547b051 1695#ifdef HAVE_GTK_FILE_SELECTION_NEW
e90292a9 1696 return x_gtk_use_old_file_dialog;
e90292a9
JD
1697#else
1698 return 0;
1699#endif
e90292a9
JD
1700}
1701
1702
f57e2426 1703typedef char * (*xg_get_file_func) (GtkWidget *);
f9d64bb3 1704
457a8155
JD
1705/* Return the selected file for file chooser dialog W.
1706 The returned string must be free:d. */
1707
1708static char *
971de7fb 1709xg_get_file_name_from_chooser (GtkWidget *w)
457a8155
JD
1710{
1711 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1712}
1713
4c9ca1a3
JD
1714/* Callback called when the "Show hidden files" toggle is pressed.
1715 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1716
78c55a80 1717static void
971de7fb 1718xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
78c55a80
JD
1719{
1720 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1721 gboolean visible;
78c55a80
JD
1722 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1723 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
4c9ca1a3
JD
1724}
1725
1726
1727/* Callback called when a property changes in a file chooser.
1728 GOBJECT is the file chooser dialog, ARG1 describes the property.
1729 USER_DATA is the toggle widget in the file chooser dialog.
1730 We use this to update the "Show hidden files" toggle when the user
1731 changes that property by right clicking in the file list. */
1732
1733static void
971de7fb 1734xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
4c9ca1a3 1735{
4c9ca1a3
JD
1736 if (strcmp (arg1->name, "show-hidden") == 0)
1737 {
4c9ca1a3
JD
1738 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1739 gboolean visible, toggle_on;
1740
1741 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1742 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1743
1744 if (!!visible != !!toggle_on)
1745 {
1746 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1747 G_CALLBACK (xg_toggle_visibility_cb),
1748 gobject);
1749 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1750 g_signal_handlers_unblock_by_func
1751 (G_OBJECT (wtoggle),
1752 G_CALLBACK (xg_toggle_visibility_cb),
1753 gobject);
1754 }
1755 x_gtk_show_hidden_files = visible;
1756 }
78c55a80
JD
1757}
1758
f9d64bb3
JD
1759/* Read a file name from the user using a file chooser dialog.
1760 F is the current frame.
1761 PROMPT is a prompt to show to the user. May not be NULL.
1762 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1763 If MUSTMATCH_P is non-zero, the returned file name must be an existing
2605051a
GM
1764 file. (Actually, this only has cosmetic effects, the user can
1765 still enter a non-existing file.) *FUNC is set to a function that
1766 can be used to retrieve the selected file name from the returned widget.
f9d64bb3 1767
457a8155 1768 Returns the created widget. */
f9d64bb3 1769
457a8155 1770static GtkWidget *
dd4c5104
DN
1771xg_get_file_with_chooser (FRAME_PTR f,
1772 char *prompt,
1773 char *default_filename,
1774 int mustmatch_p, int only_dir_p,
1775 xg_get_file_func *func)
f9d64bb3 1776{
65dc836c 1777 char msgbuf[1024];
78c55a80 1778
a60e5f68 1779 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
f9d64bb3 1780 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
f9d64bb3
JD
1781 GtkFileChooserAction action = (mustmatch_p ?
1782 GTK_FILE_CHOOSER_ACTION_OPEN :
1783 GTK_FILE_CHOOSER_ACTION_SAVE);
1784
1785 if (only_dir_p)
1786 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1787
1788 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1789 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1790 (mustmatch_p || only_dir_p ?
8cfd0f36 1791 GTK_STOCK_OPEN : GTK_STOCK_OK),
f9d64bb3
JD
1792 GTK_RESPONSE_OK,
1793 NULL);
ded997c1 1794 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
f9d64bb3 1795
383b7c95
JD
1796 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1797 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
78c55a80
JD
1798 gtk_widget_show (wbox);
1799 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
8e5a8840
JB
1800
1801 if (x_gtk_show_hidden_files)
78c55a80
JD
1802 {
1803 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1804 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1805 }
1806 gtk_widget_show (wtoggle);
1807 g_signal_connect (G_OBJECT (wtoggle), "clicked",
4c9ca1a3
JD
1808 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1809 g_signal_connect (G_OBJECT (filewin), "notify",
1810 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
78c55a80 1811
a10fe834 1812 if (x_gtk_file_dialog_help_text)
cf1e295f 1813 {
65dc836c 1814 msgbuf[0] = '\0';
7b7d4a79
JD
1815 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1816 Show the C-l help text only for versions < 2.10. */
7adfb96e 1817 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
65dc836c
PE
1818 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1819 strcat (msgbuf, "\nIf you don't like this file selector, use the "
cf1e295f
JD
1820 "corresponding\nkey binding or customize "
1821 "use-file-dialog to turn it off.");
8e5a8840 1822
65dc836c 1823 wmessage = gtk_label_new (msgbuf);
cf1e295f
JD
1824 gtk_widget_show (wmessage);
1825 }
1826
78c55a80 1827 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
a10fe834 1828 if (x_gtk_file_dialog_help_text)
cf1e295f 1829 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
78c55a80
JD
1830 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1831
f9d64bb3 1832 if (default_filename)
a872928c
JD
1833 {
1834 Lisp_Object file;
1835 struct gcpro gcpro1;
4f3097d8 1836 char *utf8_filename;
45c94881 1837 GCPRO1 (file);
a872928c 1838
ded997c1
JD
1839 file = build_string (default_filename);
1840
a872928c
JD
1841 /* File chooser does not understand ~/... in the file name. It must be
1842 an absolute name starting with /. */
1843 if (default_filename[0] != '/')
ded997c1 1844 file = Fexpand_file_name (file, Qnil);
8e5a8840 1845
4f3097d8
JD
1846 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1847 if (! NILP (Ffile_directory_p (file)))
ded997c1 1848 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
4f3097d8 1849 utf8_filename);
ded997c1 1850 else
4f3097d8 1851 {
4f3097d8
JD
1852 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1853 utf8_filename);
ae6c1c19
JD
1854 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1855 {
1856 char *cp = strrchr (utf8_filename, '/');
1857 if (cp) ++cp;
1858 else cp = utf8_filename;
1859 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1860 }
4f3097d8 1861 }
a872928c
JD
1862
1863 UNGCPRO;
1864 }
f9d64bb3 1865
457a8155
JD
1866 *func = xg_get_file_name_from_chooser;
1867 return filewin;
f9d64bb3 1868}
f9d64bb3
JD
1869
1870#ifdef HAVE_GTK_FILE_SELECTION_NEW
f392e843 1871
457a8155
JD
1872/* Return the selected file for file selector dialog W.
1873 The returned string must be free:d. */
71bacd48 1874
457a8155 1875static char *
971de7fb 1876xg_get_file_name_from_selector (GtkWidget *w)
f392e843 1877{
457a8155
JD
1878 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1879 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
f392e843
JD
1880}
1881
457a8155 1882/* Create a file selection dialog.
f392e843
JD
1883 F is the current frame.
1884 PROMPT is a prompt to show to the user. May not be NULL.
1885 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1886 If MUSTMATCH_P is non-zero, the returned file name must be an existing
457a8155
JD
1887 file. *FUNC is set to a function that can be used to retrieve the
1888 selected file name from the returned widget.
f392e843 1889
457a8155 1890 Returns the created widget. */
71bacd48 1891
457a8155 1892static GtkWidget *
e4c8d29a
J
1893xg_get_file_with_selection (FRAME_PTR f,
1894 char *prompt,
1895 char *default_filename,
1896 int mustmatch_p, int only_dir_p,
1897 xg_get_file_func *func)
f392e843
JD
1898{
1899 GtkWidget *filewin;
1900 GtkFileSelection *filesel;
177c0ea7 1901
f392e843
JD
1902 filewin = gtk_file_selection_new (prompt);
1903 filesel = GTK_FILE_SELECTION (filewin);
1904
f392e843
JD
1905 if (default_filename)
1906 gtk_file_selection_set_filename (filesel, default_filename);
1907
1908 if (mustmatch_p)
1909 {
1910 /* The selection_entry part of filesel is not documented. */
1911 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1912 gtk_file_selection_hide_fileop_buttons (filesel);
1913 }
1914
457a8155 1915 *func = xg_get_file_name_from_selector;
177c0ea7 1916
457a8155 1917 return filewin;
f392e843 1918}
f9d64bb3
JD
1919#endif /* HAVE_GTK_FILE_SELECTION_NEW */
1920
1921/* Read a file name from the user using a file dialog, either the old
1922 file selection dialog, or the new file chooser dialog. Which to use
1923 depends on what the GTK version used has, and what the value of
1924 gtk-use-old-file-dialog.
1925 F is the current frame.
1926 PROMPT is a prompt to show to the user. May not be NULL.
1927 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1928 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1929 file.
1930
1931 Returns a file name or NULL if no file was selected.
1932 The returned string must be freed by the caller. */
1933
1934char *
e4c8d29a
J
1935xg_get_file_name (FRAME_PTR f,
1936 char *prompt,
1937 char *default_filename,
1938 int mustmatch_p,
1939 int only_dir_p)
f9d64bb3 1940{
724cde0d 1941 GtkWidget *w = 0;
457a8155
JD
1942 char *fn = 0;
1943 int filesel_done = 0;
1944 xg_get_file_func func;
1945
ae9e757a 1946#if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
aa477689
JD
1947 /* I really don't know why this is needed, but without this the GLIBC add on
1948 library linuxthreads hangs when the Gnome file chooser backend creates
1949 threads. */
1950 sigblock (sigmask (__SIGRTMIN));
ae9e757a 1951#endif /* HAVE_PTHREAD */
aa477689 1952
e547b051 1953#ifdef HAVE_GTK_FILE_SELECTION_NEW
255e4140 1954
90f2e16b 1955 if (xg_uses_old_file_dialog ())
457a8155
JD
1956 w = xg_get_file_with_selection (f, prompt, default_filename,
1957 mustmatch_p, only_dir_p, &func);
1958 else
1959 w = xg_get_file_with_chooser (f, prompt, default_filename,
1960 mustmatch_p, only_dir_p, &func);
f9d64bb3 1961
e547b051 1962#else /* not HAVE_GTK_FILE_SELECTION_NEW */
457a8155
JD
1963 w = xg_get_file_with_chooser (f, prompt, default_filename,
1964 mustmatch_p, only_dir_p, &func);
e547b051 1965#endif /* not HAVE_GTK_FILE_SELECTION_NEW */
457a8155 1966
457a8155 1967 gtk_widget_set_name (w, "emacs-filedialog");
457a8155 1968
e90292a9 1969 filesel_done = xg_dialog_run (f, w);
457a8155 1970
ae9e757a 1971#if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
aa477689
JD
1972 sigunblock (sigmask (__SIGRTMIN));
1973#endif
1974
457a8155
JD
1975 if (filesel_done == GTK_RESPONSE_OK)
1976 fn = (*func) (w);
1977
e90292a9 1978 gtk_widget_destroy (w);
457a8155 1979 return fn;
f9d64bb3 1980}
f392e843 1981
3402c0a6
CY
1982#ifdef HAVE_FREETYPE
1983/* Pop up a GTK font selector and return the name of the font the user
1984 selects, as a C string. The returned font name follows GTK's own
1985 format:
1986
1987 `FAMILY [VALUE1 VALUE2] SIZE'
1988
1989 This can be parsed using font_parse_fcname in font.c.
1990 DEFAULT_NAME, if non-zero, is the default font name. */
1991
1992char *
8ea90aa3 1993xg_get_font_name (FRAME_PTR f, const char *default_name)
3402c0a6 1994{
e90292a9 1995 GtkWidget *w;
3402c0a6
CY
1996 char *fontname = NULL;
1997 int done = 0;
1998
ae9e757a 1999#if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
3402c0a6 2000 sigblock (sigmask (__SIGRTMIN));
ae9e757a 2001#endif /* HAVE_PTHREAD */
3402c0a6
CY
2002
2003 w = gtk_font_selection_dialog_new ("Pick a font");
5c646d5a
JD
2004 if (!default_name)
2005 default_name = "Monospace 10";
2006 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
2007 default_name);
3402c0a6 2008
3402c0a6 2009 gtk_widget_set_name (w, "emacs-fontdialog");
3402c0a6 2010
e90292a9 2011 done = xg_dialog_run (f, w);
3402c0a6 2012
ae9e757a 2013#if defined (HAVE_PTHREAD) && defined (__SIGRTMIN)
3402c0a6
CY
2014 sigunblock (sigmask (__SIGRTMIN));
2015#endif
2016
2017 if (done == GTK_RESPONSE_OK)
2018 fontname = gtk_font_selection_dialog_get_font_name
e90292a9 2019 (GTK_FONT_SELECTION_DIALOG (w));
3402c0a6 2020
e90292a9 2021 gtk_widget_destroy (w);
3402c0a6
CY
2022 return fontname;
2023}
2024#endif /* HAVE_FREETYPE */
2025
2026
f392e843
JD
2027\f
2028/***********************************************************************
2029 Menu functions.
2030 ***********************************************************************/
2031
d1c38b57 2032/* The name of menu items that can be used for customization. Since GTK
f392e843 2033 RC files are very crude and primitive, we have to set this on all
d1c38b57 2034 menu item names so a user can easily customize menu items. */
f392e843
JD
2035
2036#define MENU_ITEM_NAME "emacs-menuitem"
2037
2038
2039/* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2040 during GC. The next member points to the items. */
2041static xg_list_node xg_menu_cb_list;
2042
2043/* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2044 during GC. The next member points to the items. */
2045static xg_list_node xg_menu_item_cb_list;
2046
2047/* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2048 F is the frame CL_DATA will be initialized for.
2049 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2050
2051 The menu bar and all sub menus under the menu bar in a frame
2052 share the same structure, hence the reference count.
177c0ea7 2053
f392e843
JD
2054 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2055 allocated xg_menu_cb_data if CL_DATA is NULL. */
71bacd48 2056
f392e843 2057static xg_menu_cb_data *
971de7fb 2058make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
f392e843
JD
2059{
2060 if (! cl_data)
2061 {
38182d90 2062 cl_data = xmalloc (sizeof *cl_data);
f392e843 2063 cl_data->f = f;
e69b0960 2064 cl_data->menu_bar_vector = f->menu_bar_vector;
f392e843
JD
2065 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2066 cl_data->highlight_cb = highlight_cb;
2067 cl_data->ref_count = 0;
2068
2069 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2070 }
2071
2072 cl_data->ref_count++;
2073
2074 return cl_data;
2075}
2076
2077/* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2078 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2079
2080 When the menu bar is updated, menu items may have been added and/or
2081 removed, so menu_bar_vector and menu_bar_items_used change. We must
2082 then update CL_DATA since it is used to determine which menu
2083 item that is invoked in the menu.
2084 HIGHLIGHT_CB could change, there is no check that the same
2085 function is given when modifying a menu bar as was given when
2086 creating the menu bar. */
71bacd48 2087
f392e843 2088static void
e4c8d29a
J
2089update_cl_data (xg_menu_cb_data *cl_data,
2090 FRAME_PTR f,
2091 GCallback highlight_cb)
f392e843
JD
2092{
2093 if (cl_data)
2094 {
2095 cl_data->f = f;
e69b0960 2096 cl_data->menu_bar_vector = f->menu_bar_vector;
f392e843
JD
2097 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2098 cl_data->highlight_cb = highlight_cb;
2099 }
2100}
2101
2102/* Decrease reference count for CL_DATA.
2103 If reference count is zero, free CL_DATA. */
71bacd48 2104
f392e843 2105static void
971de7fb 2106unref_cl_data (xg_menu_cb_data *cl_data)
f392e843
JD
2107{
2108 if (cl_data && cl_data->ref_count > 0)
2109 {
2110 cl_data->ref_count--;
2111 if (cl_data->ref_count == 0)
2112 {
2113 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2114 xfree (cl_data);
2115 }
2116 }
2117}
2118
2119/* Function that marks all lisp data during GC. */
71bacd48 2120
f392e843 2121void
971de7fb 2122xg_mark_data (void)
f392e843
JD
2123{
2124 xg_list_node *iter;
2125
2126 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
631f2082 2127 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
f392e843
JD
2128
2129 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2130 {
2131 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2132
2133 if (! NILP (cb_data->help))
631f2082 2134 mark_object (cb_data->help);
f392e843
JD
2135 }
2136}
2137
2138
2139/* Callback called when a menu item is destroyed. Used to free data.
2140 W is the widget that is being destroyed (not used).
2141 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
71bacd48 2142
f392e843 2143static void
971de7fb 2144menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
f392e843
JD
2145{
2146 if (client_data)
2147 {
2148 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
2149 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2150 xfree (data);
2151 }
2152}
2153
2154/* Callback called when the pointer enters/leaves a menu item.
665c8f1c 2155 W is the parent of the menu item.
f392e843 2156 EVENT is either an enter event or leave event.
665c8f1c 2157 CLIENT_DATA is not used.
f392e843
JD
2158
2159 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 2160
f392e843 2161static gboolean
e4c8d29a
J
2162menuitem_highlight_callback (GtkWidget *w,
2163 GdkEventCrossing *event,
2164 gpointer client_data)
f392e843 2165{
665c8f1c
JD
2166 GdkEvent ev;
2167 GtkWidget *subwidget;
2168 xg_menu_item_cb_data *data;
177c0ea7 2169
665c8f1c
JD
2170 ev.crossing = *event;
2171 subwidget = gtk_get_event_widget (&ev);
2172 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
2173 XG_ITEM_DATA);
2174 if (data)
2175 {
f392e843
JD
2176 if (! NILP (data->help) && data->cl_data->highlight_cb)
2177 {
665c8f1c 2178 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
f392e843 2179 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
665c8f1c 2180 (*func) (subwidget, call_data);
f392e843
JD
2181 }
2182 }
2183
2184 return FALSE;
2185}
2186
2187/* Callback called when a menu is destroyed. Used to free data.
2188 W is the widget that is being destroyed (not used).
2189 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
71bacd48 2190
f392e843 2191static void
971de7fb 2192menu_destroy_callback (GtkWidget *w, gpointer client_data)
f392e843
JD
2193{
2194 unref_cl_data ((xg_menu_cb_data*) client_data);
2195}
2196
f392e843
JD
2197/* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2198 must be non-NULL) and can be inserted into a menu item.
2199
2200 Returns the GtkHBox. */
71bacd48 2201
f392e843 2202static GtkWidget *
8ea90aa3 2203make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
f392e843
JD
2204{
2205 GtkWidget *wlbl;
2206 GtkWidget *wkey;
2207 GtkWidget *wbox;
177c0ea7 2208
383b7c95
JD
2209 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2210 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
4b1b4443 2211 wlbl = gtk_label_new (utf8_label);
f392e843
JD
2212 wkey = gtk_label_new (utf8_key);
2213
2214 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2215 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
177c0ea7 2216
f392e843
JD
2217 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2218 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2219
2220 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2221 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
7863d625 2222 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
f392e843
JD
2223
2224 return wbox;
2225}
2226
2227/* Make and return a menu item widget with the key to the right.
2228 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2229 UTF8_KEY is the text representing the key binding.
2230 ITEM is the widget_value describing the menu item.
177c0ea7 2231
f392e843
JD
2232 GROUP is an in/out parameter. If the menu item to be created is not
2233 part of any radio menu group, *GROUP contains NULL on entry and exit.
2234 If the menu item to be created is part of a radio menu group, on entry
2235 *GROUP contains the group to use, or NULL if this is the first item
2236 in the group. On exit, *GROUP contains the radio item group.
2237
2238 Unfortunately, keys don't line up as nicely as in Motif,
2239 but the MacOS X version doesn't either, so I guess that is OK. */
71bacd48 2240
f392e843 2241static GtkWidget *
8ea90aa3
DN
2242make_menu_item (const char *utf8_label,
2243 const char *utf8_key,
e4c8d29a
J
2244 widget_value *item,
2245 GSList **group)
f392e843
JD
2246{
2247 GtkWidget *w;
2248 GtkWidget *wtoadd = 0;
177c0ea7 2249
adcb132c
JD
2250 /* It has been observed that some menu items have a NULL name field.
2251 This will lead to this function being called with a NULL utf8_label.
2252 GTK crashes on that so we set a blank label. Why there is a NULL
2253 name remains to be investigated. */
2254 if (! utf8_label) utf8_label = " ";
2255
f392e843
JD
2256 if (utf8_key)
2257 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
177c0ea7 2258
f392e843
JD
2259 if (item->button_type == BUTTON_TYPE_TOGGLE)
2260 {
2261 *group = NULL;
2262 if (utf8_key) w = gtk_check_menu_item_new ();
4b1b4443 2263 else w = gtk_check_menu_item_new_with_label (utf8_label);
f392e843
JD
2264 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2265 }
2266 else if (item->button_type == BUTTON_TYPE_RADIO)
2267 {
2268 if (utf8_key) w = gtk_radio_menu_item_new (*group);
4b1b4443 2269 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
f392e843
JD
2270 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2271 if (item->selected)
2272 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2273 }
2274 else
2275 {
2276 *group = NULL;
2277 if (utf8_key) w = gtk_menu_item_new ();
4b1b4443 2278 else w = gtk_menu_item_new_with_label (utf8_label);
f392e843 2279 }
177c0ea7 2280
f392e843
JD
2281 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2282 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2283
2284 return w;
2285}
2286
da18b5ac
JD
2287static int xg_detached_menus;
2288
2289/* Returns non-zero if there are detached menus. */
71bacd48 2290
da18b5ac 2291int
971de7fb 2292xg_have_tear_offs (void)
da18b5ac
JD
2293{
2294 return xg_detached_menus > 0;
2295}
f392e843
JD
2296
2297/* Callback invoked when a detached menu window is removed. Here we
da18b5ac 2298 decrease the xg_detached_menus count.
f392e843 2299 WIDGET is the top level window that is removed (the parent of the menu).
da18b5ac 2300 CLIENT_DATA is not used. */
71bacd48 2301
da18b5ac 2302static void
971de7fb 2303tearoff_remove (GtkWidget *widget, gpointer client_data)
f392e843 2304{
da18b5ac 2305 if (xg_detached_menus > 0) --xg_detached_menus;
f392e843
JD
2306}
2307
da18b5ac
JD
2308/* Callback invoked when a menu is detached. It increases the
2309 xg_detached_menus count.
f392e843 2310 WIDGET is the GtkTearoffMenuItem.
177c0ea7 2311 CLIENT_DATA is not used. */
71bacd48 2312
f392e843 2313static void
971de7fb 2314tearoff_activate (GtkWidget *widget, gpointer client_data)
f392e843
JD
2315{
2316 GtkWidget *menu = gtk_widget_get_parent (widget);
da18b5ac
JD
2317 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2318 {
2319 ++xg_detached_menus;
2320 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2321 "destroy",
2322 G_CALLBACK (tearoff_remove), 0);
2323 }
f392e843
JD
2324}
2325
f392e843 2326
f392e843 2327/* Create a menu item widget, and connect the callbacks.
4c36be58 2328 ITEM describes the menu item.
f392e843
JD
2329 F is the frame the created menu belongs to.
2330 SELECT_CB is the callback to use when a menu item is selected.
2331 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2332 CL_DATA points to the callback data to be used for this menu.
2333 GROUP is an in/out parameter. If the menu item to be created is not
2334 part of any radio menu group, *GROUP contains NULL on entry and exit.
2335 If the menu item to be created is part of a radio menu group, on entry
2336 *GROUP contains the group to use, or NULL if this is the first item
2337 in the group. On exit, *GROUP contains the radio item group.
2338
2339 Returns the created GtkWidget. */
71bacd48 2340
f392e843 2341static GtkWidget *
e4c8d29a
J
2342xg_create_one_menuitem (widget_value *item,
2343 FRAME_PTR f,
2344 GCallback select_cb,
2345 GCallback highlight_cb,
2346 xg_menu_cb_data *cl_data,
2347 GSList **group)
f392e843
JD
2348{
2349 char *utf8_label;
2350 char *utf8_key;
2351 GtkWidget *w;
2352 xg_menu_item_cb_data *cb_data;
2353
2354 utf8_label = get_utf8_string (item->name);
2355 utf8_key = get_utf8_string (item->key);
2356
2357 w = make_menu_item (utf8_label, utf8_key, item, group);
2358
42ca4633
J
2359 if (utf8_label) g_free (utf8_label);
2360 if (utf8_key) g_free (utf8_key);
f392e843 2361
38182d90 2362 cb_data = xmalloc (sizeof *cb_data);
f392e843
JD
2363
2364 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2365
665c8f1c 2366 cb_data->select_id = 0;
f392e843
JD
2367 cb_data->help = item->help;
2368 cb_data->cl_data = cl_data;
2369 cb_data->call_data = item->call_data;
177c0ea7 2370
f392e843
JD
2371 g_signal_connect (G_OBJECT (w),
2372 "destroy",
2373 G_CALLBACK (menuitem_destroy_callback),
2374 cb_data);
2375
2376 /* Put cb_data in widget, so we can get at it when modifying menubar */
2377 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2378
2379 /* final item, not a submenu */
2380 if (item->call_data && ! item->contents)
2381 {
2382 if (select_cb)
2383 cb_data->select_id
2384 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2385 }
2386
f392e843
JD
2387 return w;
2388}
2389
2390/* Create a full menu tree specified by DATA.
2391 F is the frame the created menu belongs to.
2392 SELECT_CB is the callback to use when a menu item is selected.
2393 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2394 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2395 POP_UP_P is non-zero if we shall create a popup menu.
2396 MENU_BAR_P is non-zero if we shall create a menu bar.
40ba43b4 2397 ADD_TEAROFF_P is non-zero if we shall add a tearoff menu item. Ignored
f392e843
JD
2398 if MENU_BAR_P is non-zero.
2399 TOPMENU is the topmost GtkWidget that others shall be placed under.
2400 It may be NULL, in that case we create the appropriate widget
2401 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2402 CL_DATA is the callback data we shall use for this menu, or NULL
2403 if we haven't set the first callback yet.
2404 NAME is the name to give to the top level menu if this function
2405 creates it. May be NULL to not set any name.
2406
2407 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2408 not NULL.
2409
2410 This function calls itself to create submenus. */
2411
2412static GtkWidget *
e4c8d29a
J
2413create_menus (widget_value *data,
2414 FRAME_PTR f,
2415 GCallback select_cb,
2416 GCallback deactivate_cb,
2417 GCallback highlight_cb,
2418 int pop_up_p,
2419 int menu_bar_p,
2420 int add_tearoff_p,
2421 GtkWidget *topmenu,
2422 xg_menu_cb_data *cl_data,
8ea90aa3 2423 const char *name)
f392e843
JD
2424{
2425 widget_value *item;
2426 GtkWidget *wmenu = topmenu;
2427 GSList *group = NULL;
2428
2429 if (! topmenu)
2430 {
810f2256
JD
2431 if (! menu_bar_p)
2432 {
2433 wmenu = gtk_menu_new ();
2434 xg_set_screen (wmenu, f);
665c8f1c
JD
2435 /* Connect this to the menu instead of items so we get enter/leave for
2436 disabled items also. TODO: Still does not get enter/leave for
2437 disabled items in detached menus. */
2438 g_signal_connect (G_OBJECT (wmenu),
2439 "enter-notify-event",
2440 G_CALLBACK (menuitem_highlight_callback),
2441 NULL);
2442 g_signal_connect (G_OBJECT (wmenu),
2443 "leave-notify-event",
2444 G_CALLBACK (menuitem_highlight_callback),
2445 NULL);
810f2256 2446 }
5be883cd
JD
2447 else
2448 {
2449 wmenu = gtk_menu_bar_new ();
872870b2
JD
2450 /* Set width of menu bar to a small value so it doesn't enlarge
2451 a small initial frame size. The width will be set to the
2452 width of the frame later on when it is added to a container.
2453 height -1: Natural height. */
5be883cd
JD
2454 gtk_widget_set_size_request (wmenu, 1, -1);
2455 }
f392e843
JD
2456
2457 /* Put cl_data on the top menu for easier access. */
2458 cl_data = make_cl_data (cl_data, f, highlight_cb);
2459 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2460 g_signal_connect (G_OBJECT (wmenu), "destroy",
2461 G_CALLBACK (menu_destroy_callback), cl_data);
177c0ea7 2462
f392e843
JD
2463 if (name)
2464 gtk_widget_set_name (wmenu, name);
2465
2466 if (deactivate_cb)
2467 g_signal_connect (G_OBJECT (wmenu),
c8934d9d 2468 "selection-done", deactivate_cb, 0);
f392e843 2469 }
177c0ea7 2470
f392e843
JD
2471 if (! menu_bar_p && add_tearoff_p)
2472 {
2473 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2474 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2475
2476 g_signal_connect (G_OBJECT (tearoff), "activate",
2477 G_CALLBACK (tearoff_activate), 0);
2478 }
2479
2480 for (item = data; item; item = item->next)
2481 {
2482 GtkWidget *w;
177c0ea7 2483
f392e843 2484 if (pop_up_p && !item->contents && !item->call_data
4039c786 2485 && !menu_separator_name_p (item->name))
f392e843
JD
2486 {
2487 char *utf8_label;
2488 /* A title for a popup. We do the same as GTK does when
2489 creating titles, but it does not look good. */
2490 group = NULL;
2491 utf8_label = get_utf8_string (item->name);
2492
2493 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
4b1b4443 2494 w = gtk_menu_item_new_with_label (utf8_label);
f392e843 2495 gtk_widget_set_sensitive (w, FALSE);
42ca4633 2496 if (utf8_label) g_free (utf8_label);
f392e843 2497 }
4039c786 2498 else if (menu_separator_name_p (item->name))
f392e843
JD
2499 {
2500 group = NULL;
2501 /* GTK only have one separator type. */
2502 w = gtk_separator_menu_item_new ();
2503 }
2504 else
2505 {
2506 w = xg_create_one_menuitem (item,
2507 f,
2508 item->contents ? 0 : select_cb,
2509 highlight_cb,
2510 cl_data,
2511 &group);
2512
f56cff88
JD
2513 /* Create a possibly empty submenu for menu bar items, since some
2514 themes don't highlight items correctly without it. */
2515 if (item->contents || menu_bar_p)
f392e843
JD
2516 {
2517 GtkWidget *submenu = create_menus (item->contents,
2518 f,
2519 select_cb,
2520 deactivate_cb,
2521 highlight_cb,
2522 0,
2523 0,
da18b5ac 2524 add_tearoff_p,
f392e843
JD
2525 0,
2526 cl_data,
2527 0);
2528 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2529 }
f392e843
JD
2530 }
2531
2532 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2533 gtk_widget_set_name (w, MENU_ITEM_NAME);
2534 }
2535
2536 return wmenu;
2537}
2538
2539/* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2540 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2541 with some text and buttons.
2542 F is the frame the created item belongs to.
2543 NAME is the name to use for the top widget.
2544 VAL is a widget_value structure describing items to be created.
2545 SELECT_CB is the callback to use when a menu item is selected or
2546 a dialog button is pressed.
2547 DEACTIVATE_CB is the callback to use when an item is deactivated.
2548 For a menu, when a sub menu is not shown anymore, for a dialog it is
2549 called when the dialog is popped down.
2550 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2551
2552 Returns the widget created. */
71bacd48 2553
f392e843 2554GtkWidget *
675e2c69 2555xg_create_widget (const char *type, const char *name, FRAME_PTR f, widget_value *val,
d5a3eaaf
AS
2556 GCallback select_cb, GCallback deactivate_cb,
2557 GCallback highlight_cb)
f392e843
JD
2558{
2559 GtkWidget *w = 0;
da18b5ac
JD
2560 int menu_bar_p = strcmp (type, "menubar") == 0;
2561 int pop_up_p = strcmp (type, "popup") == 0;
2562
f392e843
JD
2563 if (strcmp (type, "dialog") == 0)
2564 {
2565 w = create_dialog (val, select_cb, deactivate_cb);
810f2256 2566 xg_set_screen (w, f);
f392e843
JD
2567 gtk_window_set_transient_for (GTK_WINDOW (w),
2568 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2569 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
810f2256 2570 gtk_widget_set_name (w, "emacs-dialog");
457a8155 2571 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
f392e843 2572 }
da18b5ac 2573 else if (menu_bar_p || pop_up_p)
f392e843
JD
2574 {
2575 w = create_menus (val->contents,
2576 f,
2577 select_cb,
2578 deactivate_cb,
2579 highlight_cb,
da18b5ac
JD
2580 pop_up_p,
2581 menu_bar_p,
2582 menu_bar_p,
f392e843
JD
2583 0,
2584 0,
2585 name);
2586
2587 /* Set the cursor to an arrow for popup menus when they are mapped.
2588 This is done by default for menu bar menus. */
da18b5ac 2589 if (pop_up_p)
f392e843
JD
2590 {
2591 /* Must realize so the GdkWindow inside the widget is created. */
2592 gtk_widget_realize (w);
810f2256 2593 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
f392e843
JD
2594 }
2595 }
2596 else
2597 {
2598 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2599 type);
2600 }
2601
2602 return w;
2603}
2604
0a1d6de0 2605/* Return the label for menu item WITEM. */
71bacd48 2606
f392e843 2607static const char *
971de7fb 2608xg_get_menu_item_label (GtkMenuItem *witem)
f392e843 2609{
4039c786 2610 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
f392e843
JD
2611 return gtk_label_get_label (wlabel);
2612}
2613
0a1d6de0 2614/* Return non-zero if the menu item WITEM has the text LABEL. */
71bacd48 2615
f392e843 2616static int
42ca4633 2617xg_item_label_same_p (GtkMenuItem *witem, const char *label)
f392e843 2618{
0a1d6de0 2619 int is_same = 0;
f392e843 2620 char *utf8_label = get_utf8_string (label);
0a1d6de0
JD
2621 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2622
2623 if (! old_label && ! utf8_label)
2624 is_same = 1;
2625 else if (old_label && utf8_label)
2626 is_same = strcmp (utf8_label, old_label) == 0;
2627
42ca4633 2628 if (utf8_label) g_free (utf8_label);
f392e843
JD
2629
2630 return is_same;
2631}
2632
71bacd48
JD
2633/* Destroy widgets in LIST. */
2634
f392e843 2635static void
971de7fb 2636xg_destroy_widgets (GList *list)
f392e843 2637{
f392e843
JD
2638 GList *iter;
2639
49853a4d 2640 for (iter = list; iter; iter = g_list_next (iter))
f392e843
JD
2641 {
2642 GtkWidget *w = GTK_WIDGET (iter->data);
2643
71bacd48 2644 /* Destroying the widget will remove it from the container it is in. */
f392e843
JD
2645 gtk_widget_destroy (w);
2646 }
f392e843
JD
2647}
2648
2649/* Update the top level names in MENUBAR (i.e. not submenus).
2650 F is the frame the menu bar belongs to.
49853a4d
JD
2651 *LIST is a list with the current menu bar names (menu item widgets).
2652 ITER is the item within *LIST that shall be updated.
2653 POS is the numerical position, starting at 0, of ITER in *LIST.
f392e843
JD
2654 VAL describes what the menu bar shall look like after the update.
2655 SELECT_CB is the callback to use when a menu item is selected.
2656 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
49853a4d 2657 CL_DATA points to the callback data to be used for this menu bar.
f392e843
JD
2658
2659 This function calls itself to walk through the menu bar names. */
71bacd48 2660
f392e843 2661static void
dd4c5104
DN
2662xg_update_menubar (GtkWidget *menubar,
2663 FRAME_PTR f,
2664 GList **list,
2665 GList *iter,
2666 int pos,
2667 widget_value *val,
2668 GCallback select_cb,
2669 GCallback deactivate_cb,
2670 GCallback highlight_cb,
2671 xg_menu_cb_data *cl_data)
f392e843 2672{
49853a4d 2673 if (! iter && ! val)
f392e843 2674 return;
49853a4d 2675 else if (iter && ! val)
f392e843 2676 {
49853a4d 2677 /* Item(s) have been removed. Remove all remaining items. */
71bacd48 2678 xg_destroy_widgets (iter);
f392e843 2679
4a8e097d
JD
2680 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2681 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2682 gtk_menu_item_new_with_label (""),
2683 0);
f392e843
JD
2684 /* All updated. */
2685 val = 0;
49853a4d 2686 iter = 0;
f392e843 2687 }
49853a4d 2688 else if (! iter && val)
f392e843
JD
2689 {
2690 /* Item(s) added. Add all new items in one call. */
d1c38b57 2691 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
f392e843
JD
2692 0, 1, 0, menubar, cl_data, 0);
2693
2694 /* All updated. */
2695 val = 0;
49853a4d 2696 iter = 0;
f392e843 2697 }
49853a4d
JD
2698 /* Below this neither iter or val is NULL */
2699 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
f392e843
JD
2700 {
2701 /* This item is still the same, check next item. */
2702 val = val->next;
49853a4d
JD
2703 iter = g_list_next (iter);
2704 ++pos;
f392e843
JD
2705 }
2706 else /* This item is changed. */
2707 {
49853a4d 2708 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
f392e843 2709 GtkMenuItem *witem2 = 0;
f392e843 2710 int val_in_menubar = 0;
49853a4d
JD
2711 int iter_in_new_menubar = 0;
2712 GList *iter2;
f392e843
JD
2713 widget_value *cur;
2714
f392e843 2715 /* See if the changed entry (val) is present later in the menu bar */
49853a4d
JD
2716 for (iter2 = iter;
2717 iter2 && ! val_in_menubar;
2718 iter2 = g_list_next (iter2))
f392e843 2719 {
49853a4d 2720 witem2 = GTK_MENU_ITEM (iter2->data);
f392e843
JD
2721 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2722 }
2723
49853a4d 2724 /* See if the current entry (iter) is present later in the
f392e843 2725 specification for the new menu bar. */
49853a4d
JD
2726 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2727 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
f392e843 2728
49853a4d 2729 if (val_in_menubar && ! iter_in_new_menubar)
f392e843 2730 {
49853a4d
JD
2731 int nr = pos;
2732
f392e843
JD
2733 /* This corresponds to:
2734 Current: A B C
2735 New: A C
2736 Remove B. */
177c0ea7 2737
e547b051 2738 g_object_ref (G_OBJECT (witem));
f392e843
JD
2739 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2740 gtk_widget_destroy (GTK_WIDGET (witem));
2741
2742 /* Must get new list since the old changed. */
49853a4d
JD
2743 g_list_free (*list);
2744 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2745 while (nr-- > 0) iter = g_list_next (iter);
f392e843 2746 }
49853a4d 2747 else if (! val_in_menubar && ! iter_in_new_menubar)
f392e843
JD
2748 {
2749 /* This corresponds to:
2750 Current: A B C
2751 New: A X C
2752 Rename B to X. This might seem to be a strange thing to do,
2753 since if there is a menu under B it will be totally wrong for X.
2754 But consider editing a C file. Then there is a C-mode menu
2755 (corresponds to B above).
2756 If then doing C-x C-f the minibuf menu (X above) replaces the
2757 C-mode menu. When returning from the minibuffer, we get
2758 back the C-mode menu. Thus we do:
2759 Rename B to X (C-mode to minibuf menu)
2760 Rename X to B (minibuf to C-mode menu).
2761 If the X menu hasn't been invoked, the menu under B
2762 is up to date when leaving the minibuffer. */
4039c786 2763 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
f392e843 2764 char *utf8_label = get_utf8_string (val->name);
da18b5ac 2765 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
177c0ea7 2766
4b1b4443 2767 gtk_label_set_text (wlabel, utf8_label);
f392e843 2768
da18b5ac
JD
2769 /* If this item has a submenu that has been detached, change
2770 the title in the WM decorations also. */
2771 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2772 /* Set the title of the detached window. */
2773 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2774
42ca4633 2775 if (utf8_label) g_free (utf8_label);
49853a4d 2776 iter = g_list_next (iter);
f392e843 2777 val = val->next;
49853a4d 2778 ++pos;
f392e843 2779 }
49853a4d 2780 else if (! val_in_menubar && iter_in_new_menubar)
f392e843
JD
2781 {
2782 /* This corresponds to:
2783 Current: A B C
2784 New: A X B C
2785 Insert X. */
2786
49853a4d 2787 int nr = pos;
e4c8d29a 2788 GSList *group = 0;
f392e843
JD
2789 GtkWidget *w = xg_create_one_menuitem (val,
2790 f,
2791 select_cb,
2792 highlight_cb,
2793 cl_data,
2794 &group);
2795
f56cff88
JD
2796 /* Create a possibly empty submenu for menu bar items, since some
2797 themes don't highlight items correctly without it. */
2798 GtkWidget *submenu = create_menus (NULL, f,
d1c38b57
JD
2799 select_cb, deactivate_cb,
2800 highlight_cb,
f56cff88 2801 0, 0, 0, 0, cl_data, 0);
f392e843
JD
2802 gtk_widget_set_name (w, MENU_ITEM_NAME);
2803 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
f56cff88 2804 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
f392e843 2805
49853a4d
JD
2806 g_list_free (*list);
2807 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2808 while (nr-- > 0) iter = g_list_next (iter);
2809 iter = g_list_next (iter);
f392e843 2810 val = val->next;
49853a4d 2811 ++pos;
f392e843 2812 }
49853a4d 2813 else /* if (val_in_menubar && iter_in_new_menubar) */
f392e843 2814 {
49853a4d 2815 int nr = pos;
f392e843
JD
2816 /* This corresponds to:
2817 Current: A B C
2818 New: A C B
2819 Move C before B */
2820
e547b051 2821 g_object_ref (G_OBJECT (witem2));
f392e843
JD
2822 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2823 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2824 GTK_WIDGET (witem2), pos);
e547b051 2825 g_object_unref (G_OBJECT (witem2));
f392e843 2826
49853a4d
JD
2827 g_list_free (*list);
2828 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2829 while (nr-- > 0) iter = g_list_next (iter);
0a194c92 2830 if (iter) iter = g_list_next (iter);
f392e843 2831 val = val->next;
49853a4d 2832 ++pos;
f392e843 2833 }
f392e843
JD
2834 }
2835
2836 /* Update the rest of the menu bar. */
49853a4d 2837 xg_update_menubar (menubar, f, list, iter, pos, val,
d1c38b57 2838 select_cb, deactivate_cb, highlight_cb, cl_data);
f392e843
JD
2839}
2840
2841/* Update the menu item W so it corresponds to VAL.
2842 SELECT_CB is the callback to use when a menu item is selected.
2843 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
a8ce3d17 2844 CL_DATA is the data to set in the widget for menu invocation. */
71bacd48 2845
f392e843 2846static void
e4c8d29a
J
2847xg_update_menu_item (widget_value *val,
2848 GtkWidget *w,
2849 GCallback select_cb,
2850 GCallback highlight_cb,
2851 xg_menu_cb_data *cl_data)
f392e843
JD
2852{
2853 GtkWidget *wchild;
2854 GtkLabel *wlbl = 0;
2855 GtkLabel *wkey = 0;
2856 char *utf8_label;
2857 char *utf8_key;
0a1d6de0
JD
2858 const char *old_label = 0;
2859 const char *old_key = 0;
f392e843 2860 xg_menu_item_cb_data *cb_data;
177c0ea7 2861
4039c786 2862 wchild = XG_BIN_CHILD (w);
f392e843
JD
2863 utf8_label = get_utf8_string (val->name);
2864 utf8_key = get_utf8_string (val->key);
2865
2866 /* See if W is a menu item with a key. See make_menu_item above. */
383b7c95 2867 if (GTK_IS_BOX (wchild))
f392e843
JD
2868 {
2869 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2870
2871 wlbl = GTK_LABEL (list->data);
2872 wkey = GTK_LABEL (list->next->data);
49853a4d
JD
2873 g_list_free (list);
2874
f392e843
JD
2875 if (! utf8_key)
2876 {
2877 /* Remove the key and keep just the label. */
e547b051 2878 g_object_ref (G_OBJECT (wlbl));
f392e843
JD
2879 gtk_container_remove (GTK_CONTAINER (w), wchild);
2880 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
e547b051 2881 g_object_unref (G_OBJECT (wlbl));
f392e843
JD
2882 wkey = 0;
2883 }
49853a4d 2884
f392e843
JD
2885 }
2886 else /* Just a label. */
2887 {
2888 wlbl = GTK_LABEL (wchild);
2889
2890 /* Check if there is now a key. */
2891 if (utf8_key)
2892 {
2893 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2894 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
49853a4d 2895
f392e843
JD
2896 wlbl = GTK_LABEL (list->data);
2897 wkey = GTK_LABEL (list->next->data);
49853a4d 2898 g_list_free (list);
f392e843
JD
2899
2900 gtk_container_remove (GTK_CONTAINER (w), wchild);
2901 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2902 }
2903 }
2904
177c0ea7 2905
0a1d6de0
JD
2906 if (wkey) old_key = gtk_label_get_label (wkey);
2907 if (wlbl) old_label = gtk_label_get_label (wlbl);
177c0ea7 2908
0a1d6de0 2909 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
f392e843
JD
2910 gtk_label_set_text (wkey, utf8_key);
2911
0a1d6de0 2912 if (! old_label || strcmp (utf8_label, old_label) != 0)
4b1b4443 2913 gtk_label_set_text (wlbl, utf8_label);
f392e843 2914
42ca4633
J
2915 if (utf8_key) g_free (utf8_key);
2916 if (utf8_label) g_free (utf8_label);
177c0ea7 2917
e547b051 2918 if (! val->enabled && gtk_widget_get_sensitive (w))
f392e843 2919 gtk_widget_set_sensitive (w, FALSE);
e547b051 2920 else if (val->enabled && ! gtk_widget_get_sensitive (w))
f392e843
JD
2921 gtk_widget_set_sensitive (w, TRUE);
2922
2923 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2924 XG_ITEM_DATA);
2925 if (cb_data)
2926 {
2927 cb_data->call_data = val->call_data;
2928 cb_data->help = val->help;
2929 cb_data->cl_data = cl_data;
177c0ea7 2930
f392e843
JD
2931 /* We assume the callback functions don't change. */
2932 if (val->call_data && ! val->contents)
2933 {
2934 /* This item shall have a select callback. */
2935 if (! cb_data->select_id)
2936 cb_data->select_id
2937 = g_signal_connect (G_OBJECT (w), "activate",
2938 select_cb, cb_data);
2939 }
2940 else if (cb_data->select_id)
2941 {
2942 g_signal_handler_disconnect (w, cb_data->select_id);
2943 cb_data->select_id = 0;
2944 }
f392e843
JD
2945 }
2946}
2947
2948/* Update the toggle menu item W so it corresponds to VAL. */
71bacd48 2949
f392e843 2950static void
971de7fb 2951xg_update_toggle_item (widget_value *val, GtkWidget *w)
f392e843
JD
2952{
2953 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2954}
2955
2956/* Update the radio menu item W so it corresponds to VAL. */
71bacd48 2957
f392e843 2958static void
971de7fb 2959xg_update_radio_item (widget_value *val, GtkWidget *w)
f392e843
JD
2960{
2961 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2962}
2963
2964/* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2965 SUBMENU may be NULL, in that case a new menu is created.
2966 F is the frame the menu bar belongs to.
2967 VAL describes the contents of the menu bar.
2968 SELECT_CB is the callback to use when a menu item is selected.
2969 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2970 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2971 CL_DATA is the call back data to use for any newly created items.
2972
2973 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2974 was NULL. */
2975
2976static GtkWidget *
dd4c5104
DN
2977xg_update_submenu (GtkWidget *submenu,
2978 FRAME_PTR f,
2979 widget_value *val,
2980 GCallback select_cb,
2981 GCallback deactivate_cb,
2982 GCallback highlight_cb,
2983 xg_menu_cb_data *cl_data)
f392e843
JD
2984{
2985 GtkWidget *newsub = submenu;
2986 GList *list = 0;
2987 GList *iter;
2988 widget_value *cur;
2989 int has_tearoff_p = 0;
2990 GList *first_radio = 0;
177c0ea7 2991
f392e843
JD
2992 if (submenu)
2993 list = gtk_container_get_children (GTK_CONTAINER (submenu));
177c0ea7 2994
f392e843
JD
2995 for (cur = val, iter = list;
2996 cur && iter;
2997 iter = g_list_next (iter), cur = cur->next)
2998 {
2999 GtkWidget *w = GTK_WIDGET (iter->data);
3000
3001 /* Skip tearoff items, they have no counterpart in val. */
3002 if (GTK_IS_TEAROFF_MENU_ITEM (w))
3003 {
3004 has_tearoff_p = 1;
3005 iter = g_list_next (iter);
3006 if (iter) w = GTK_WIDGET (iter->data);
3007 else break;
3008 }
3009
3010 /* Remember first radio button in a group. If we get a mismatch in
3011 a radio group we must rebuild the whole group so that the connections
3012 in GTK becomes correct. */
3013 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3014 first_radio = iter;
3015 else if (cur->button_type != BUTTON_TYPE_RADIO
3016 && ! GTK_IS_RADIO_MENU_ITEM (w))
3017 first_radio = 0;
3018
3019 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3020 {
4039c786 3021 if (! menu_separator_name_p (cur->name))
f392e843
JD
3022 break;
3023 }
3024 else if (GTK_IS_CHECK_MENU_ITEM (w))
3025 {
3026 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3027 break;
3028 xg_update_toggle_item (cur, w);
3029 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3030 }
3031 else if (GTK_IS_RADIO_MENU_ITEM (w))
3032 {
3033 if (cur->button_type != BUTTON_TYPE_RADIO)
3034 break;
3035 xg_update_radio_item (cur, w);
3036 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3037 }
3038 else if (GTK_IS_MENU_ITEM (w))
3039 {
3040 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3041 GtkWidget *sub;
3042
3043 if (cur->button_type != BUTTON_TYPE_NONE ||
4039c786 3044 menu_separator_name_p (cur->name))
f392e843
JD
3045 break;
3046
3047 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3048
3049 sub = gtk_menu_item_get_submenu (witem);
3050 if (sub && ! cur->contents)
3051 {
3052 /* Not a submenu anymore. */
e547b051
J
3053 g_object_ref (G_OBJECT (sub));
3054 remove_submenu (witem);
f392e843
JD
3055 gtk_widget_destroy (sub);
3056 }
3057 else if (cur->contents)
3058 {
3059 GtkWidget *nsub;
3060
3061 nsub = xg_update_submenu (sub, f, cur->contents,
3062 select_cb, deactivate_cb,
3063 highlight_cb, cl_data);
3064
3065 /* If this item just became a submenu, we must set it. */
3066 if (nsub != sub)
3067 gtk_menu_item_set_submenu (witem, nsub);
3068 }
3069 }
3070 else
3071 {
3072 /* Structural difference. Remove everything from here and down
3073 in SUBMENU. */
3074 break;
3075 }
3076 }
3077
22bcf204 3078 /* Remove widgets from first structural change. */
f392e843
JD
3079 if (iter)
3080 {
3081 /* If we are adding new menu items below, we must remove from
3082 first radio button so that radio groups become correct. */
71bacd48
JD
3083 if (cur && first_radio) xg_destroy_widgets (first_radio);
3084 else xg_destroy_widgets (iter);
f392e843 3085 }
177c0ea7 3086
f392e843
JD
3087 if (cur)
3088 {
3089 /* More items added. Create them. */
3090 newsub = create_menus (cur,
3091 f,
3092 select_cb,
3093 deactivate_cb,
3094 highlight_cb,
3095 0,
3096 0,
3097 ! has_tearoff_p,
3098 submenu,
3099 cl_data,
3100 0);
3101 }
177c0ea7 3102
49853a4d
JD
3103 if (list) g_list_free (list);
3104
f392e843
JD
3105 return newsub;
3106}
3107
3108/* Update the MENUBAR.
3109 F is the frame the menu bar belongs to.
3110 VAL describes the contents of the menu bar.
3111 If DEEP_P is non-zero, rebuild all but the top level menu names in
3112 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3113 SELECT_CB is the callback to use when a menu item is selected.
3114 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3115 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
71bacd48 3116
f392e843 3117void
d5a3eaaf
AS
3118xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
3119 int deep_p,
3120 GCallback select_cb, GCallback deactivate_cb,
3121 GCallback highlight_cb)
f392e843
JD
3122{
3123 xg_menu_cb_data *cl_data;
3124 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
f392e843
JD
3125
3126 if (! list) return;
177c0ea7 3127
f392e843
JD
3128 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
3129 XG_FRAME_DATA);
3130
da18b5ac 3131 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
d1c38b57 3132 select_cb, deactivate_cb, highlight_cb, cl_data);
da18b5ac 3133
aa41b0bf 3134 if (deep_p)
f392e843
JD
3135 {
3136 widget_value *cur;
3137
3138 /* Update all sub menus.
da18b5ac 3139 We must keep the submenus (GTK menu item widgets) since the
f392e843
JD
3140 X Window in the XEvent that activates the menu are those widgets. */
3141
3142 /* Update cl_data, menu_item things in F may have changed. */
3143 update_cl_data (cl_data, f, highlight_cb);
3144
3145 for (cur = val->contents; cur; cur = cur->next)
3146 {
49853a4d 3147 GList *iter;
f392e843
JD
3148 GtkWidget *sub = 0;
3149 GtkWidget *newsub;
e4c8d29a 3150 GtkMenuItem *witem = 0;
f392e843
JD
3151
3152 /* Find sub menu that corresponds to val and update it. */
3153 for (iter = list ; iter; iter = g_list_next (iter))
3154 {
3155 witem = GTK_MENU_ITEM (iter->data);
3156 if (xg_item_label_same_p (witem, cur->name))
3157 {
3158 sub = gtk_menu_item_get_submenu (witem);
3159 break;
3160 }
3161 }
177c0ea7 3162
f392e843
JD
3163 newsub = xg_update_submenu (sub,
3164 f,
3165 cur->contents,
3166 select_cb,
3167 deactivate_cb,
3168 highlight_cb,
3169 cl_data);
3170 /* sub may still be NULL. If we just updated non deep and added
3171 a new menu bar item, it has no sub menu yet. So we set the
3172 newly created sub menu under witem. */
e4c8d29a 3173 if (newsub != sub && witem != 0)
810f2256
JD
3174 {
3175 xg_set_screen (newsub, f);
3176 gtk_menu_item_set_submenu (witem, newsub);
3177 }
f392e843
JD
3178 }
3179 }
3180
49853a4d 3181 g_list_free (list);
f392e843
JD
3182 gtk_widget_show_all (menubar);
3183}
3184
8d7f026f
JD
3185/* Callback called when the menu bar W is mapped.
3186 Used to find the height of the menu bar if we didn't get it
3187 after showing the widget. */
3188
3189static void
3190menubar_map_cb (GtkWidget *w, gpointer user_data)
3191{
3192 GtkRequisition req;
3193 FRAME_PTR f = (FRAME_PTR) user_data;
0afb4571 3194 gtk_widget_get_preferred_size (w, NULL, &req);
088c8c09 3195 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
8d7f026f
JD
3196 {
3197 FRAME_MENUBAR_HEIGHT (f) = req.height;
088c8c09 3198 xg_height_or_width_changed (f);
8d7f026f
JD
3199 }
3200}
3201
f392e843
JD
3202/* Recompute all the widgets of frame F, when the menu bar has been
3203 changed. Value is non-zero if widgets were updated. */
3204
3205int
971de7fb 3206xg_update_frame_menubar (FRAME_PTR f)
f392e843
JD
3207{
3208 struct x_output *x = f->output_data.x;
3209 GtkRequisition req;
177c0ea7 3210
e547b051 3211 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
f392e843
JD
3212 return 0;
3213
473a99b7
JD
3214 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3215 return 0; /* Already done this, happens for frames created invisible. */
3216
f392e843
JD
3217 BLOCK_INPUT;
3218
3219 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3220 FALSE, FALSE, 0);
3221 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3222
8d7f026f 3223 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
f392e843 3224 gtk_widget_show_all (x->menubar_widget);
0afb4571 3225 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
07976ae3 3226
8d7f026f
JD
3227 /* If menu bar doesn't know its height yet, cheat a little so the frame
3228 doesn't jump so much when resized later in menubar_map_cb. */
3229 if (req.height == 0)
3230 req.height = 23;
3231
3232 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3233 {
3234 FRAME_MENUBAR_HEIGHT (f) = req.height;
07976ae3 3235 xg_height_or_width_changed (f);
8d7f026f 3236 }
f392e843 3237 UNBLOCK_INPUT;
a8303f92
AS
3238
3239 return 1;
f392e843
JD
3240}
3241
3242/* Get rid of the menu bar of frame F, and free its storage.
3243 This is used when deleting a frame, and when turning off the menu bar. */
3244
3245void
971de7fb 3246free_frame_menubar (FRAME_PTR f)
f392e843
JD
3247{
3248 struct x_output *x = f->output_data.x;
3249
3250 if (x->menubar_widget)
3251 {
3252 BLOCK_INPUT;
3253
3254 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3255 /* The menubar and its children shall be deleted when removed from
3256 the container. */
3257 x->menubar_widget = 0;
3258 FRAME_MENUBAR_HEIGHT (f) = 0;
bfeabdc3 3259 xg_height_or_width_changed (f);
f392e843
JD
3260 UNBLOCK_INPUT;
3261 }
3262}
3263
b78f9767
J
3264int
3265xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
3266{
3267 struct x_output *x = f->output_data.x;
499322ce
J
3268 GList *iter;
3269 GdkRectangle rec;
3270 GList *list;
3271 GdkDisplay *gdpy;
3272 GdkWindow *gw;
3273 GdkEvent gevent;
3274 GtkWidget *gwdesc;
b78f9767
J
3275
3276 if (! x->menubar_widget) return 0;
3277
3278 if (! (event->xbutton.x >= 0
3279 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3280 && event->xbutton.y >= 0
3281 && event->xbutton.y < f->output_data.x->menubar_height
3282 && event->xbutton.same_screen))
3283 return 0;
3284
499322ce 3285 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
0afb4571 3286 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
499322ce
J
3287 if (! gw) return 0;
3288 gevent.any.window = gw;
3b574623 3289 gevent.any.type = GDK_NOTHING;
499322ce
J
3290 gwdesc = gtk_get_event_widget (&gevent);
3291 if (! gwdesc) return 0;
3292 if (! GTK_IS_MENU_BAR (gwdesc)
3293 && ! GTK_IS_MENU_ITEM (gwdesc)
3294 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3295 return 0;
3296
3297 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
b78f9767 3298 if (! list) return 0;
b78f9767
J
3299 rec.x = event->xbutton.x;
3300 rec.y = event->xbutton.y;
3301 rec.width = 1;
3302 rec.height = 1;
499322ce 3303
b78f9767
J
3304 for (iter = list ; iter; iter = g_list_next (iter))
3305 {
3306 GtkWidget *w = GTK_WIDGET (iter->data);
ce6e9d7f 3307 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
b78f9767
J
3308 break;
3309 }
3310 g_list_free (list);
3311 return iter == 0 ? 0 : 1;
3312}
3313
f392e843
JD
3314
3315\f
3316/***********************************************************************
3317 Scroll bar functions
3318 ***********************************************************************/
3319
3320
3321/* Setting scroll bar values invokes the callback. Use this variable
3322 to indicate that callback should do nothing. */
71bacd48 3323
f392e843
JD
3324int xg_ignore_gtk_scrollbar;
3325
c195f2de
JD
3326/* The width of the scroll bar for the current theme. */
3327
3328static int scroll_bar_width_for_theme;
3329
056ce195
SM
3330/* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3331 may be larger than 32 bits. Keep a mapping from integer index to widget
3332 pointers to get around the 32 bit limitation. */
71bacd48 3333
f392e843
JD
3334static struct
3335{
3336 GtkWidget **widgets;
0eb0f318
PE
3337 ptrdiff_t max_size;
3338 ptrdiff_t used;
81e302ef 3339} id_to_widget;
f392e843
JD
3340
3341/* Grow this much every time we need to allocate more */
71bacd48 3342
f392e843
JD
3343#define ID_TO_WIDGET_INCR 32
3344
3345/* Store the widget pointer W in id_to_widget and return the integer index. */
71bacd48 3346
0eb0f318 3347static ptrdiff_t
971de7fb 3348xg_store_widget_in_map (GtkWidget *w)
f392e843 3349{
0eb0f318 3350 ptrdiff_t i;
f392e843
JD
3351
3352 if (id_to_widget.max_size == id_to_widget.used)
3353 {
0eb0f318 3354 ptrdiff_t new_size;
0065d054 3355 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
0eb0f318 3356 memory_full (SIZE_MAX);
f392e843 3357
0eb0f318 3358 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
0065d054
PE
3359 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3360 new_size, sizeof (GtkWidget *));
f392e843
JD
3361
3362 for (i = id_to_widget.max_size; i < new_size; ++i)
3363 id_to_widget.widgets[i] = 0;
3364 id_to_widget.max_size = new_size;
3365 }
3366
3367 /* Just loop over the array and find a free place. After all,
3368 how many scroll bars are we creating? Should be a small number.
3369 The check above guarantees we will find a free place. */
3370 for (i = 0; i < id_to_widget.max_size; ++i)
3371 {
3372 if (! id_to_widget.widgets[i])
3373 {
3374 id_to_widget.widgets[i] = w;
3375 ++id_to_widget.used;
3376
3377 return i;
3378 }
3379 }
3380
3381 /* Should never end up here */
3382 abort ();
3383}
3384
3385/* Remove pointer at IDX from id_to_widget.
3386 Called when scroll bar is destroyed. */
71bacd48 3387
f392e843 3388static void
0eb0f318 3389xg_remove_widget_from_map (ptrdiff_t idx)
f392e843
JD
3390{
3391 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3392 {
3393 id_to_widget.widgets[idx] = 0;
3394 --id_to_widget.used;
3395 }
3396}
3397
3398/* Get the widget pointer at IDX from id_to_widget. */
71bacd48 3399
f392e843 3400static GtkWidget *
0eb0f318 3401xg_get_widget_from_map (ptrdiff_t idx)
f392e843
JD
3402{
3403 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3404 return id_to_widget.widgets[idx];
3405
3406 return 0;
3407}
3408
c195f2de
JD
3409static void
3410update_theme_scrollbar_width (void)
a059fe24 3411{
43f862f7
AS
3412#ifdef HAVE_GTK3
3413 GtkAdjustment *vadj;
3414#else
3415 GtkObject *vadj;
3416#endif
3417 GtkWidget *wscroll;
a059fe24 3418 int w = 0, b = 0;
c195f2de 3419
43f862f7 3420 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
383b7c95 3421 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
c195f2de 3422 g_object_ref_sink (G_OBJECT (wscroll));
a059fe24
JD
3423 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3424 gtk_widget_destroy (wscroll);
c195f2de 3425 g_object_unref (G_OBJECT (wscroll));
a059fe24
JD
3426 w += 2*b;
3427 if (w < 16) w = 16;
c195f2de
JD
3428 scroll_bar_width_for_theme = w;
3429}
3430
3431int
3432xg_get_default_scrollbar_width (void)
3433{
3434 return scroll_bar_width_for_theme;
a059fe24
JD
3435}
3436
810f2256 3437/* Return the scrollbar id for X Window WID on display DPY.
3a8a22fc 3438 Return -1 if WID not in id_to_widget. */
71bacd48 3439
0eb0f318 3440ptrdiff_t
971de7fb 3441xg_get_scroll_id_for_window (Display *dpy, Window wid)
3a8a22fc 3442{
0eb0f318 3443 ptrdiff_t idx;
3a8a22fc
JD
3444 GtkWidget *w;
3445
810f2256 3446 w = xg_win_to_widget (dpy, wid);
3a8a22fc
JD
3447
3448 if (w)
3449 {
3450 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3451 if (id_to_widget.widgets[idx] == w)
3452 return idx;
3453 }
3454
3455 return -1;
3456}
3457
f392e843
JD
3458/* Callback invoked when scroll bar WIDGET is destroyed.
3459 DATA is the index into id_to_widget for WIDGET.
cea9be54 3460 We free pointer to last scroll bar values here and remove the index. */
71bacd48 3461
f392e843 3462static void
971de7fb 3463xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
f392e843 3464{
0eb0f318 3465 intptr_t id = (intptr_t) data;
f392e843
JD
3466 xg_remove_widget_from_map (id);
3467}
3468
f392e843
JD
3469/* Create a scroll bar widget for frame F. Store the scroll bar
3470 in BAR.
3471 SCROLL_CALLBACK is the callback to invoke when the value of the
3472 bar changes.
e5f0bc9a 3473 END_CALLBACK is the callback to invoke when scrolling ends.
f392e843
JD
3474 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3475 to set resources for the widget. */
71bacd48 3476
f392e843 3477void
e4c8d29a
J
3478xg_create_scroll_bar (FRAME_PTR f,
3479 struct scroll_bar *bar,
3480 GCallback scroll_callback,
3481 GCallback end_callback,
675e2c69 3482 const char *scroll_bar_name)
f392e843
JD
3483{
3484 GtkWidget *wscroll;
1755a397 3485 GtkWidget *webox;
d01a7826 3486 intptr_t scroll_id;
0afb4571
J
3487#ifdef HAVE_GTK3
3488 GtkAdjustment *vadj;
3489#else
3490 GtkObject *vadj;
3491#endif
177c0ea7 3492
f392e843
JD
3493 /* Page, step increment values are not so important here, they
3494 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3495 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3496 0.1, 0.1, 0.1);
3497
383b7c95 3498 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
1755a397 3499 webox = gtk_event_box_new ();
f392e843 3500 gtk_widget_set_name (wscroll, scroll_bar_name);
0afb4571 3501#ifndef HAVE_GTK3
f392e843 3502 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
0afb4571 3503#endif
e5f0bc9a 3504 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
177c0ea7 3505
f392e843 3506 scroll_id = xg_store_widget_in_map (wscroll);
177c0ea7 3507
f392e843
JD
3508 g_signal_connect (G_OBJECT (wscroll),
3509 "destroy",
3510 G_CALLBACK (xg_gtk_scroll_destroy),
8ac068ac 3511 (gpointer) scroll_id);
e5f0bc9a
JD
3512 g_signal_connect (G_OBJECT (wscroll),
3513 "change-value",
3514 scroll_callback,
3515 (gpointer) bar);
e8d7886a
JD
3516 g_signal_connect (G_OBJECT (wscroll),
3517 "button-release-event",
e5f0bc9a 3518 end_callback,
e8d7886a 3519 (gpointer) bar);
088c8c09 3520
1755a397
JD
3521 /* The scroll bar widget does not draw on a window of its own. Instead
3522 it draws on the parent window, in this case the edit widget. So
3523 whenever the edit widget is cleared, the scroll bar needs to redraw
3524 also, which causes flicker. Put an event box between the edit widget
3525 and the scroll bar, so the scroll bar instead draws itself on the
3526 event box window. */
3527 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3528 gtk_container_add (GTK_CONTAINER (webox), wscroll);
c43923ad 3529
f392e843
JD
3530
3531 /* Set the cursor to an arrow. */
1755a397 3532 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
f392e843 3533
056ce195 3534 bar->x_window = scroll_id;
f392e843
JD
3535}
3536
f392e843 3537/* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
71bacd48 3538
f392e843 3539void
0eb0f318 3540xg_remove_scroll_bar (FRAME_PTR f, ptrdiff_t scrollbar_id)
f392e843
JD
3541{
3542 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3543 if (w)
3544 {
1755a397 3545 GtkWidget *wparent = gtk_widget_get_parent (w);
f392e843 3546 gtk_widget_destroy (w);
1755a397 3547 gtk_widget_destroy (wparent);
f392e843
JD
3548 SET_FRAME_GARBAGED (f);
3549 }
3550}
3551
f392e843
JD
3552/* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3553 in frame F.
3554 TOP/LEFT are the new pixel positions where the bar shall appear.
3555 WIDTH, HEIGHT is the size in pixels the bar shall have. */
71bacd48 3556
f392e843 3557void
e4c8d29a 3558xg_update_scrollbar_pos (FRAME_PTR f,
0eb0f318 3559 ptrdiff_t scrollbar_id,
e4c8d29a
J
3560 int top,
3561 int left,
3562 int width,
3563 int height)
f392e843 3564{
f392e843 3565
49853a4d 3566 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
7863d625 3567
49853a4d
JD
3568 if (wscroll)
3569 {
cea9be54 3570 GtkWidget *wfixed = f->output_data.x->edit_widget;
1755a397 3571 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
8c079ebb 3572 gint msl;
7c583cd8
JD
3573
3574 /* Clear out old position. */
7c583cd8 3575 int oldx = -1, oldy = -1, oldw, oldh;
e547b051
J
3576 if (gtk_widget_get_parent (wparent) == wfixed)
3577 {
3578 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3579 "x", &oldx, "y", &oldy, NULL);
3580 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3581 }
7863d625 3582
ab2d724b 3583 /* Move and resize to new values. */
1755a397 3584 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
bc869eca
JD
3585 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3586 if (msl > height)
3587 {
3588 /* No room. Hide scroll bar as some themes output a warning if
3589 the height is less than the min size. */
3590 gtk_widget_hide (wparent);
3591 gtk_widget_hide (wscroll);
3592 }
3593 else
3594 {
3595 gtk_widget_show_all (wparent);
3596 gtk_widget_set_size_request (wscroll, width, height);
3597 }
7c583cd8 3598 gtk_widget_queue_draw (wfixed);
817d354b 3599 gdk_window_process_all_updates ();
bc869eca 3600 if (oldx != -1 && oldw > 0 && oldh > 0)
7c583cd8
JD
3601 {
3602 /* Clear under old scroll bar position. This must be done after
3603 the gtk_widget_queue_draw and gdk_window_process_all_updates
3604 above. */
3605 x_clear_area (FRAME_X_DISPLAY (f),
3606 FRAME_X_WINDOW (f),
3607 oldx, oldy, oldw, oldh, 0);
3608 }
088c8c09 3609
817d354b
JD
3610 /* GTK does not redraw until the main loop is entered again, but
3611 if there are no X events pending we will not enter it. So we sync
3612 here to get some events. */
088c8c09 3613
817d354b 3614 x_sync (f);
49853a4d
JD
3615 SET_FRAME_GARBAGED (f);
3616 cancel_mouse_face (f);
3617 }
f392e843
JD
3618}
3619
c195f2de
JD
3620/* Get the current value of the range, truncated to an integer. */
3621
3622static int
3623int_gtk_range_get_value (GtkRange *range)
3624{
3625 return gtk_range_get_value (range);
3626}
3627
3628
f392e843
JD
3629/* Set the thumb size and position of scroll bar BAR. We are currently
3630 displaying PORTION out of a whole WHOLE, and our position POSITION. */
71bacd48 3631
f392e843 3632void
e4c8d29a
J
3633xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3634 int portion,
3635 int position,
3636 int whole)
f392e843 3637{
056ce195 3638 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
f392e843
JD
3639
3640 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3641
17097258 3642 if (wscroll && NILP (bar->dragging))
f392e843
JD
3643 {
3644 GtkAdjustment *adj;
3645 gdouble shown;
3646 gdouble top;
3647 int size, value;
6048fb2a 3648 int old_size;
7863d625
JD
3649 int new_step;
3650 int changed = 0;
177c0ea7 3651
f392e843
JD
3652 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3653
17097258
JD
3654 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3655 rather than the real portion value. This makes the thumb less likely
3656 to resize and that looks better. */
be786000 3657 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
17097258
JD
3658 /* When the thumb is at the bottom, position == whole.
3659 So we need to increase `whole' to make space for the thumb. */
3660 whole += portion;
3661
f392e843
JD
3662 if (whole <= 0)
3663 top = 0, shown = 1;
3664 else
3665 {
f392e843 3666 top = (gdouble) position / whole;
7863d625 3667 shown = (gdouble) portion / whole;
f392e843
JD
3668 }
3669
7863d625
JD
3670 size = shown * XG_SB_RANGE;
3671 size = min (size, XG_SB_RANGE);
f392e843
JD
3672 size = max (size, 1);
3673
7863d625
JD
3674 value = top * XG_SB_RANGE;
3675 value = min (value, XG_SB_MAX - size);
f392e843
JD
3676 value = max (value, XG_SB_MIN);
3677
7863d625 3678 /* Assume all lines are of equal size. */
be786000 3679 new_step = size / max (1, FRAME_LINES (f));
2a2071c3 3680
6048fb2a
PE
3681 old_size = gtk_adjustment_get_page_size (adj);
3682 if (old_size != size)
3683 {
3684 int old_step = gtk_adjustment_get_step_increment (adj);
3685 if (old_step != new_step)
3686 {
3687 gtk_adjustment_set_page_size (adj, size);
3688 gtk_adjustment_set_step_increment (adj, new_step);
3689 /* Assume a page increment is about 95% of the page size */
5f8f9cc2 3690 gtk_adjustment_set_page_increment (adj, size - size / 20);
6048fb2a
PE
3691 changed = 1;
3692 }
3693 }
17097258 3694
1e5524e7 3695 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
7863d625 3696 {
7863d625 3697 BLOCK_INPUT;
cea9be54 3698
7863d625
JD
3699 /* gtk_range_set_value invokes the callback. Set
3700 ignore_gtk_scrollbar to make the callback do nothing */
3701 xg_ignore_gtk_scrollbar = 1;
f392e843 3702
1e5524e7 3703 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
7863d625
JD
3704 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3705 else if (changed)
3706 gtk_adjustment_changed (adj);
3707
3708 xg_ignore_gtk_scrollbar = 0;
3709
3710 UNBLOCK_INPUT;
3711 }
3712 }
f392e843
JD
3713}
3714
e511451f
JD
3715/* Return non-zero if EVENT is for a scroll bar in frame F.
3716 When the same X window is used for several Gtk+ widgets, we cannot
3717 say for sure based on the X window alone if an event is for the
3718 frame. This function does additional checks.
3719
3720 Return non-zero if the event is for a scroll bar, zero otherwise. */
3721
3722int
971de7fb 3723xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
e511451f
JD
3724{
3725 int retval = 0;
3726
e5f0bc9a 3727 if (f && event->type == ButtonPress && event->xbutton.button < 4)
e511451f
JD
3728 {
3729 /* Check if press occurred outside the edit widget. */
3730 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
383b7c95
JD
3731 GdkWindow *gwin;
3732#ifdef HAVE_GTK3
3733 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3734 (gdk_display_get_device_manager (gdpy));
bdd091e4 3735 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
383b7c95
JD
3736#else
3737 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3738#endif
3739 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
e511451f 3740 }
e5f0bc9a
JD
3741 else if (f
3742 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3743 || event->type == MotionNotify))
e511451f
JD
3744 {
3745 /* If we are releasing or moving the scroll bar, it has the grab. */
3bb49aaf
JD
3746 GtkWidget *w = gtk_grab_get_current ();
3747 retval = w != 0 && GTK_IS_SCROLLBAR (w);
e511451f 3748 }
088c8c09 3749
e511451f
JD
3750 return retval;
3751}
3752
3753
f392e843
JD
3754\f
3755/***********************************************************************
3756 Tool bar functions
3757 ***********************************************************************/
3758/* The key for the data we put in the GtkImage widgets. The data is
3759 the image used by Emacs. We use this to see if we need to update
3760 the GtkImage with a new image. */
3761#define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3762
2334f9e7
JD
3763/* The key for storing the latest modifiers so the activate callback can
3764 get them. */
3765#define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3766
1e0fe298
JD
3767/* The key for storing the button widget in its proxy menu item. */
3768#define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
2334f9e7 3769
98a92193
JD
3770/* The key for the data we put in the GtkImage widgets. The data is
3771 the stock name used by Emacs. We use this to see if we need to update
3772 the GtkImage with a new image. */
3773#define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3774
2154c964
JD
3775/* As above, but this is used for named theme widgets, as opposed to
3776 stock items. */
3777#define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3778
98a92193
JD
3779/* Callback function invoked when a tool bar item is pressed.
3780 W is the button widget in the tool bar that got pressed,
3781 CLIENT_DATA is an integer that is the index of the button in the
3782 tool bar. 0 is the first button. */
3783
2334f9e7 3784static gboolean
e4c8d29a
J
3785xg_tool_bar_button_cb (GtkWidget *widget,
3786 GdkEventButton *event,
3787 gpointer user_data)
2334f9e7 3788{
d01a7826 3789 intptr_t state = event->state;
8ac068ac 3790 gpointer ptr = (gpointer) state;
6e1440e6 3791 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
6d45d2a0 3792 return FALSE;
2334f9e7
JD
3793}
3794
3795
6e1440e6
JD
3796/* Callback function invoked when a tool bar item is pressed.
3797 W is the button widget in the tool bar that got pressed,
3798 CLIENT_DATA is an integer that is the index of the button in the
3799 tool bar. 0 is the first button. */
3800
f392e843 3801static void
971de7fb 3802xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
f392e843 3803{
d01a7826 3804 intptr_t idx = (intptr_t) client_data;
1e5524e7 3805 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
d01a7826 3806 intptr_t mod = (intptr_t) gmod;
2334f9e7 3807
f392e843
JD
3808 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3809 Lisp_Object key, frame;
3810 struct input_event event;
aa4ac494 3811 EVENT_INIT (event);
f392e843 3812
e69b0960 3813 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
f392e843
JD
3814 return;
3815
3816 idx *= TOOL_BAR_ITEM_NSLOTS;
177c0ea7 3817
e69b0960 3818 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
f392e843 3819 XSETFRAME (frame, f);
991bde0d
JD
3820
3821 /* We generate two events here. The first one is to set the prefix
3822 to `(tool_bar)', see keyboard.c. */
f392e843
JD
3823 event.kind = TOOL_BAR_EVENT;
3824 event.frame_or_window = frame;
8e5a8840
JB
3825 event.arg = frame;
3826 kbd_buffer_store_event (&event);
3827
3828 event.kind = TOOL_BAR_EVENT;
991bde0d 3829 event.frame_or_window = frame;
f392e843 3830 event.arg = key;
2334f9e7 3831 /* Convert between the modifier bits GDK uses and the modifier bits
4a02423f 3832 Emacs uses. This assumes GDK and X masks are the same, which they are when
2334f9e7
JD
3833 this is written. */
3834 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
f392e843 3835 kbd_buffer_store_event (&event);
088c8c09 3836
4a02423f
JD
3837 /* Return focus to the frame after we have clicked on a detached
3838 tool bar button. */
3839 Fx_focus_frame (frame);
f392e843
JD
3840}
3841
1e0fe298
JD
3842/* Callback function invoked when a tool bar item is pressed in a detached
3843 tool bar or the overflow drop down menu.
3844 We just call xg_tool_bar_callback.
3845 W is the menu item widget that got pressed,
3846 CLIENT_DATA is an integer that is the index of the button in the
3847 tool bar. 0 is the first button. */
3848
3849static void
971de7fb 3850xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
1e0fe298
JD
3851{
3852 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3853 XG_TOOL_BAR_PROXY_BUTTON));
3854 xg_tool_bar_callback (wbutton, client_data);
3855}
3856
69b16610
JD
3857
3858static gboolean
f57e2426
J
3859xg_tool_bar_help_callback (GtkWidget *w,
3860 GdkEventCrossing *event,
3861 gpointer client_data);
69b16610
JD
3862
3863/* This callback is called when a help is to be shown for an item in
3864 the detached tool bar when the detached tool bar it is not expanded. */
3865
3866static gboolean
e4c8d29a
J
3867xg_tool_bar_proxy_help_callback (GtkWidget *w,
3868 GdkEventCrossing *event,
3869 gpointer client_data)
69b16610
JD
3870{
3871 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3872 XG_TOOL_BAR_PROXY_BUTTON));
088c8c09 3873
eba5eb94 3874 return xg_tool_bar_help_callback (wbutton, event, client_data);
69b16610
JD
3875}
3876
e547b051
J
3877static GtkWidget *
3878xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3879{
3880 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3881 GtkWidget *c1 = (GtkWidget *) clist->data;
3afff00e
CY
3882 GtkWidget *c2 = clist->next ? (GtkWidget *) clist->next->data : NULL;
3883
e547b051
J
3884 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3885 g_list_free (clist);
3886 return GTK_IS_LABEL (c1) ? c1 : c2;
3887}
3888
69b16610 3889
1e0fe298
JD
3890/* This callback is called when a tool item should create a proxy item,
3891 such as for the overflow menu. Also called when the tool bar is detached.
3892 If we don't create a proxy menu item, the detached tool bar will be
3893 blank. */
3894
3895static gboolean
971de7fb 3896xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
1e0fe298 3897{
4039c786
CY
3898 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3899 GtkWidget *vb = XG_BIN_CHILD (wbutton);
e547b051
J
3900 GtkWidget *c1;
3901 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3902 GtkImage *wimage = GTK_IMAGE (c1);
f904c0f9 3903 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
42f60557 3904 (wlbl ? gtk_label_get_text (wlbl) : "");
1e0fe298
JD
3905 GtkWidget *wmenuimage;
3906
e547b051 3907
8e5a8840 3908 if (gtk_button_get_use_stock (wbutton))
1e0fe298
JD
3909 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3910 GTK_ICON_SIZE_MENU);
3911 else
3912 {
1e0fe298
JD
3913 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3914 GtkImageType store_type = gtk_image_get_storage_type (wimage);
e624c3f9 3915
2b5b82db
J
3916 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
3917
1e0fe298
JD
3918 if (store_type == GTK_IMAGE_STOCK)
3919 {
3920 gchar *stock_id;
3921 gtk_image_get_stock (wimage, &stock_id, NULL);
3922 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3923 }
3924 else if (store_type == GTK_IMAGE_ICON_SET)
3925 {
3926 GtkIconSet *icon_set;
3927 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3928 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3929 GTK_ICON_SIZE_MENU);
3930 }
3931 else if (store_type == GTK_IMAGE_PIXBUF)
3932 {
3933 gint width, height;
8e5a8840 3934
1e0fe298
JD
3935 if (settings &&
3936 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3937 &width, &height))
3938 {
3939 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3940
3941 src_pixbuf = gtk_image_get_pixbuf (wimage);
3942 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3943 GDK_INTERP_BILINEAR);
3944
3945 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3946 }
e624c3f9
JD
3947 else
3948 {
3949 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3950 abort ();
3951 }
3952 }
8e5a8840 3953 else if (store_type == GTK_IMAGE_ICON_NAME)
e624c3f9
JD
3954 {
3955 const gchar *icon_name;
3956 GtkIconSize icon_size;
3957
3958 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3959 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3960 GTK_ICON_SIZE_MENU);
3961 }
3962 else
3963 {
3964 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3965 abort ();
1e0fe298
JD
3966 }
3967 }
3968 if (wmenuimage)
3969 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3970
3971 g_signal_connect (G_OBJECT (wmenuitem),
3972 "activate",
dc2933eb 3973 G_CALLBACK (xg_tool_bar_proxy_callback),
1e0fe298
JD
3974 user_data);
3975
088c8c09 3976
1e0fe298
JD
3977 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3978 (gpointer) wbutton);
3979 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
e547b051
J
3980 gtk_widget_set_sensitive (wmenuitem,
3981 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
69b16610
JD
3982
3983 /* Use enter/leave notify to show help. We use the events
3984 rather than the GtkButton specific signals "enter" and
3985 "leave", so we can have only one callback. The event
3986 will tell us what kind of event it is. */
3987 g_signal_connect (G_OBJECT (wmenuitem),
3988 "enter-notify-event",
3989 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3990 user_data);
3991 g_signal_connect (G_OBJECT (wmenuitem),
3992 "leave-notify-event",
3993 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3994 user_data);
1e0fe298
JD
3995
3996 return TRUE;
3997}
3998
f392e843
JD
3999/* This callback is called when a tool bar is detached. We must set
4000 the height of the tool bar to zero when this happens so frame sizes
4001 are correctly calculated.
4002 WBOX is the handle box widget that enables detach/attach of the tool bar.
4003 W is the tool bar widget.
4004 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 4005
f392e843 4006static void
e4c8d29a
J
4007xg_tool_bar_detach_callback (GtkHandleBox *wbox,
4008 GtkWidget *w,
4009 gpointer client_data)
f392e843
JD
4010{
4011 FRAME_PTR f = (FRAME_PTR) client_data;
0f340cab
JD
4012
4013 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
4014 NULL);
f392e843
JD
4015
4016 if (f)
4017 {
bfeabdc3 4018 GtkRequisition req, req2;
e0910857 4019 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
0afb4571
J
4020 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4021 gtk_widget_get_preferred_size (w, NULL, &req2);
bfeabdc3
JD
4022 req.width -= req2.width;
4023 req.height -= req2.height;
4024 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4025 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4026 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4027 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4028 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4029 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4030 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4031 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4032 xg_height_or_width_changed (f);
f392e843
JD
4033 }
4034}
4035
4036/* This callback is called when a tool bar is reattached. We must set
4037 the height of the tool bar when this happens so frame sizes
4038 are correctly calculated.
4039 WBOX is the handle box widget that enables detach/attach of the tool bar.
4040 W is the tool bar widget.
4041 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 4042
f392e843 4043static void
e4c8d29a
J
4044xg_tool_bar_attach_callback (GtkHandleBox *wbox,
4045 GtkWidget *w,
4046 gpointer client_data)
f392e843
JD
4047{
4048 FRAME_PTR f = (FRAME_PTR) client_data;
0f340cab 4049 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
f392e843
JD
4050
4051 if (f)
4052 {
bfeabdc3 4053 GtkRequisition req, req2;
e0910857 4054 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
0afb4571
J
4055 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4056 gtk_widget_get_preferred_size (w, NULL, &req2);
bfeabdc3
JD
4057 req.width += req2.width;
4058 req.height += req2.height;
4059 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4060 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4061 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4062 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4063 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4064 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4065 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4066 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4067 xg_height_or_width_changed (f);
f392e843
JD
4068 }
4069}
4070
4071/* This callback is called when the mouse enters or leaves a tool bar item.
4072 It is used for displaying and hiding the help text.
4073 W is the tool bar item, a button.
4074 EVENT is either an enter event or leave event.
4075 CLIENT_DATA is an integer that is the index of the button in the
4076 tool bar. 0 is the first button.
4077
4078 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 4079
f392e843 4080static gboolean
e4c8d29a
J
4081xg_tool_bar_help_callback (GtkWidget *w,
4082 GdkEventCrossing *event,
4083 gpointer client_data)
f392e843 4084{
d01a7826 4085 intptr_t idx = (intptr_t) client_data;
f392e843
JD
4086 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4087 Lisp_Object help, frame;
4088
e69b0960 4089 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
d9301435 4090 return FALSE;
f392e843
JD
4091
4092 if (event->type == GDK_ENTER_NOTIFY)
4093 {
4094 idx *= TOOL_BAR_ITEM_NSLOTS;
e69b0960 4095 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
f392e843
JD
4096
4097 if (NILP (help))
e69b0960 4098 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
f392e843
JD
4099 }
4100 else
4101 help = Qnil;
4102
4103 XSETFRAME (frame, f);
4104 kbd_buffer_store_help_event (frame, help);
4105
4106 return FALSE;
4107}
4108
4109
f098b121
JD
4110/* This callback is called when a tool bar item shall be redrawn.
4111 It modifies the expose event so that the GtkImage widget redraws the
4112 whole image. This to overcome a bug that makes GtkImage draw the image
4113 in the wrong place when it tries to redraw just a part of the image.
4114 W is the GtkImage to be redrawn.
4115 EVENT is the expose event for W.
4116 CLIENT_DATA is unused.
4117
4118 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 4119
0afb4571 4120#ifndef HAVE_GTK3
f098b121 4121static gboolean
e4c8d29a
J
4122xg_tool_bar_item_expose_callback (GtkWidget *w,
4123 GdkEventExpose *event,
4124 gpointer client_data)
f098b121 4125{
b676f356
JD
4126 gint width, height;
4127
4128 gdk_drawable_get_size (event->window, &width, &height);
b676f356
JD
4129 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4130 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4131
810f2256
JD
4132 event->area.x = max (0, event->area.x);
4133 event->area.y = max (0, event->area.y);
c43923ad 4134
b676f356
JD
4135 event->area.width = max (width, event->area.width);
4136 event->area.height = max (height, event->area.height);
c43923ad 4137
f098b121
JD
4138 return FALSE;
4139}
0afb4571 4140#endif
f098b121 4141
bfeabdc3
JD
4142#ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4143#define toolbar_set_orientation(w, o) \
4144 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4145#else
4146#define toolbar_set_orientation(w, o) \
4147 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4148#endif
4149
1e39cbfb 4150/* Attach a tool bar to frame F. */
71bacd48 4151
f392e843 4152static void
bfeabdc3 4153xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
f392e843
JD
4154{
4155 struct x_output *x = f->output_data.x;
bfeabdc3 4156 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
177c0ea7 4157
bfeabdc3
JD
4158 toolbar_set_orientation (x->toolbar_widget,
4159 into_hbox
4160 ? GTK_ORIENTATION_VERTICAL
4161 : GTK_ORIENTATION_HORIZONTAL);
4162 if (!x->handlebox_widget)
4163 {
4164 x->handlebox_widget = gtk_handle_box_new ();
4165 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4166 G_CALLBACK (xg_tool_bar_detach_callback), f);
4167 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4168 G_CALLBACK (xg_tool_bar_attach_callback), f);
4169 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4170 x->toolbar_widget);
4171 }
4721152c 4172
088c8c09 4173 if (into_hbox)
bfeabdc3 4174 {
318a04c6
J
4175 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4176 GTK_POS_TOP);
bfeabdc3
JD
4177 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
4178 FALSE, FALSE, 0);
4179
4180 if (EQ (pos, Qleft))
4181 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4182 x->handlebox_widget,
4183 0);
4184 x->toolbar_in_hbox = 1;
4185 }
4186 else
4187 {
4188 int vbox_pos = x->menubar_widget ? 1 : 0;
318a04c6
J
4189 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4190 GTK_POS_LEFT);
bfeabdc3
JD
4191 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
4192 FALSE, FALSE, 0);
4193
4194 if (EQ (pos, Qtop))
4195 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4196 x->handlebox_widget,
4197 vbox_pos);
4198 x->toolbar_in_hbox = 0;
4199 }
1e39cbfb
JD
4200}
4201
4202/* Create a tool bar for frame F. */
4203
4204static void
971de7fb 4205xg_create_tool_bar (FRAME_PTR f)
1e39cbfb
JD
4206{
4207 struct x_output *x = f->output_data.x;
c9adfeaa
SF
4208#if GTK_CHECK_VERSION (3, 3, 6)
4209 GtkStyleContext *gsty;
4210#endif
1e39cbfb
JD
4211
4212 x->toolbar_widget = gtk_toolbar_new ();
4213 x->toolbar_detached = 0;
f392e843 4214
f098b121
JD
4215 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4216
f098b121 4217 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
8a52f00a 4218 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
c9adfeaa
SF
4219#if GTK_CHECK_VERSION (3, 3, 6)
4220 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4221 gtk_style_context_add_class (gsty, "primary-toolbar");
4222#endif
1e39cbfb 4223}
f098b121 4224
177c0ea7 4225
e69b0960 4226#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
f392e843 4227
98a92193
JD
4228/* Find the right-to-left image named by RTL in the tool bar images for F.
4229 Returns IMAGE if RTL is not found. */
4230
4231static Lisp_Object
971de7fb 4232find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
98a92193
JD
4233{
4234 int i;
4235 Lisp_Object file, rtl_name;
4236 struct gcpro gcpro1, gcpro2;
4237 GCPRO2 (file, rtl_name);
4238
4239 rtl_name = Ffile_name_nondirectory (rtl);
4240
4241 for (i = 0; i < f->n_tool_bar_items; ++i)
4242 {
4243 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
8e5a8840 4244 if (!NILP (file = file_for_image (rtl_image)))
98a92193
JD
4245 {
4246 file = call1 (intern ("file-name-sans-extension"),
4247 Ffile_name_nondirectory (file));
4248 if (EQ (Fequal (file, rtl_name), Qt))
4249 {
4250 image = rtl_image;
4251 break;
4252 }
4253 }
4254 }
4255
4256 return image;
4257}
4258
f904c0f9
JD
4259static GtkToolItem *
4260xg_make_tool_item (FRAME_PTR f,
4261 GtkWidget *wimage,
4262 GtkWidget **wbutton,
8ea90aa3 4263 const char *label,
3afff00e 4264 int i, int horiz, int text_image)
f904c0f9
JD
4265{
4266 GtkToolItem *ti = gtk_tool_item_new ();
383b7c95
JD
4267 GtkWidget *vb = gtk_box_new (horiz
4268 ? GTK_ORIENTATION_HORIZONTAL
4269 : GTK_ORIENTATION_VERTICAL,
4270 0);
f904c0f9 4271 GtkWidget *wb = gtk_button_new ();
115b96bd 4272 /* The eventbox is here so we can have tooltips on disabled items. */
f904c0f9 4273 GtkWidget *weventbox = gtk_event_box_new ();
c9adfeaa
SF
4274#if GTK_CHECK_VERSION (3, 3, 6)
4275 GtkCssProvider *css_prov = gtk_css_provider_new ();
4276 GtkStyleContext *gsty;
4277
4278 gtk_css_provider_load_from_data (css_prov,
4279 "GtkEventBox {"
4280 " background-color: transparent;"
4281 "}",
4282 -1, NULL);
4283
4284 gsty = gtk_widget_get_style_context (weventbox);
4285 gtk_style_context_add_provider (gsty,
4286 GTK_STYLE_PROVIDER (css_prov),
4287 GTK_STYLE_PROVIDER_PRIORITY_USER);
4288 g_object_unref (css_prov);
4289#endif
f904c0f9 4290
383b7c95
JD
4291 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4292
3afff00e 4293 if (wimage && !text_image)
e547b051 4294 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
3afff00e
CY
4295 if (label)
4296 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
8a52f00a
JD
4297 if (wimage && text_image)
4298 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4299
f904c0f9
JD
4300 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4301 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4302 gtk_container_add (GTK_CONTAINER (wb), vb);
4303 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4304 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4305
fe0b6370 4306 if (wimage || label)
f904c0f9 4307 {
d01a7826 4308 intptr_t ii = i;
8ac068ac
PE
4309 gpointer gi = (gpointer) ii;
4310
f904c0f9
JD
4311 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4312 G_CALLBACK (xg_tool_bar_menu_proxy),
8ac068ac 4313 gi);
f904c0f9
JD
4314
4315 g_signal_connect (G_OBJECT (wb), "clicked",
4316 G_CALLBACK (xg_tool_bar_callback),
8ac068ac 4317 gi);
f904c0f9 4318
f904c0f9
JD
4319 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4320
0afb4571 4321#ifndef HAVE_GTK3
f904c0f9
JD
4322 /* Catch expose events to overcome an annoying redraw bug, see
4323 comment for xg_tool_bar_item_expose_callback. */
4324 g_signal_connect (G_OBJECT (ti),
4325 "expose-event",
4326 G_CALLBACK (xg_tool_bar_item_expose_callback),
4327 0);
0afb4571 4328#endif
f904c0f9
JD
4329 gtk_tool_item_set_homogeneous (ti, FALSE);
4330
e1dbe924 4331 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
f904c0f9
JD
4332 no distinction based on modifiers in the activate callback,
4333 so we have to do it ourselves. */
4334 g_signal_connect (wb, "button-release-event",
4335 G_CALLBACK (xg_tool_bar_button_cb),
4336 NULL);
4337
4338 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
088c8c09 4339
f904c0f9
JD
4340 /* Use enter/leave notify to show help. We use the events
4341 rather than the GtkButton specific signals "enter" and
4342 "leave", so we can have only one callback. The event
4343 will tell us what kind of event it is. */
f904c0f9
JD
4344 g_signal_connect (G_OBJECT (weventbox),
4345 "enter-notify-event",
4346 G_CALLBACK (xg_tool_bar_help_callback),
8ac068ac 4347 gi);
f904c0f9
JD
4348 g_signal_connect (G_OBJECT (weventbox),
4349 "leave-notify-event",
4350 G_CALLBACK (xg_tool_bar_help_callback),
8ac068ac 4351 gi);
f904c0f9 4352 }
088c8c09 4353
f904c0f9
JD
4354 if (wbutton) *wbutton = wb;
4355
4356 return ti;
4357}
4358
383b7c95
JD
4359static int
4360is_box_type (GtkWidget *vb, int is_horizontal)
4361{
4362#ifdef HAVE_GTK3
4363 int ret = 0;
38182d90 4364 if (GTK_IS_BOX (vb))
383b7c95
JD
4365 {
4366 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4367 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4368 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4369 }
4370 return ret;
4371#else
4372 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4373#endif
4374}
4375
4376
3afff00e
CY
4377static int
4378xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4379 const char *icon_name, const struct image *img,
4380 const char *label, int horiz)
f904c0f9 4381{
3afff00e 4382 gpointer old;
e547b051 4383 GtkWidget *wimage;
3afff00e 4384 GtkWidget *vb = XG_BIN_CHILD (wbutton);
e547b051 4385 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
f904c0f9 4386
3afff00e 4387 /* Check if the tool icon matches. */
fe0b6370 4388 if (stock_name && wimage)
3afff00e
CY
4389 {
4390 old = g_object_get_data (G_OBJECT (wimage),
4391 XG_TOOL_BAR_STOCK_NAME);
4392 if (!old || strcmp (old, stock_name))
4393 return 1;
4394 }
fe0b6370 4395 else if (icon_name && wimage)
f904c0f9 4396 {
3afff00e
CY
4397 old = g_object_get_data (G_OBJECT (wimage),
4398 XG_TOOL_BAR_ICON_NAME);
4399 if (!old || strcmp (old, icon_name))
4400 return 1;
f904c0f9 4401 }
fe0b6370 4402 else if (wimage)
3afff00e 4403 {
1e5524e7 4404 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
383b7c95 4405 XG_TOOL_BAR_IMAGE_DATA);
1e5524e7 4406 Pixmap old_img = (Pixmap) gold_img;
3afff00e
CY
4407 if (old_img != img->pixmap)
4408 return 1;
4409 }
4410
4411 /* Check button configuration and label. */
383b7c95 4412 if (is_box_type (vb, horiz)
3afff00e
CY
4413 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4414 return 1;
f904c0f9 4415
3afff00e 4416 /* Ensure label is correct. */
fe0b6370 4417 if (label && wlbl)
3afff00e
CY
4418 gtk_label_set_text (GTK_LABEL (wlbl), label);
4419 return 0;
f904c0f9
JD
4420}
4421
bfeabdc3
JD
4422static int
4423xg_update_tool_bar_sizes (FRAME_PTR f)
4424{
4425 struct x_output *x = f->output_data.x;
4426 GtkRequisition req;
4427 int nl = 0, nr = 0, nt = 0, nb = 0;
4428
0afb4571 4429 gtk_widget_get_preferred_size (GTK_WIDGET (x->handlebox_widget), NULL, &req);
bfeabdc3
JD
4430 if (x->toolbar_in_hbox)
4431 {
4432 int pos;
4433 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4434 x->handlebox_widget,
4435 "position", &pos, NULL);
4436 if (pos == 0) nl = req.width;
4437 else nr = req.width;
4438 }
4439 else
4440 {
4441 int pos;
4442 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4443 x->handlebox_widget,
4444 "position", &pos, NULL);
4445 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4446 else nb = req.height;
4447 }
088c8c09 4448
bfeabdc3
JD
4449 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4450 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4451 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4452 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4453 {
4454 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4455 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4456 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4457 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4458 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4459 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4460 return 1;
4461 }
4462
4463 return 0;
4464}
4465
f904c0f9 4466
71bacd48
JD
4467/* Update the tool bar for frame F. Add new buttons and remove old. */
4468
f392e843 4469void
971de7fb 4470update_frame_tool_bar (FRAME_PTR f)
f392e843 4471{
3afff00e 4472 int i, j;
f392e843 4473 struct x_output *x = f->output_data.x;
bfc2a7d1 4474 int hmargin = 0, vmargin = 0;
98a92193 4475 GtkToolbar *wtoolbar;
6e1440e6 4476 GtkToolItem *ti;
98a92193 4477 GtkTextDirection dir;
1e39cbfb 4478 int pack_tool_bar = x->handlebox_widget == NULL;
3afff00e
CY
4479 Lisp_Object style;
4480 int text_image, horiz;
4481
f392e843
JD
4482 if (! FRAME_GTK_WIDGET (f))
4483 return;
4484
4485 BLOCK_INPUT;
177c0ea7 4486
d311d28c 4487 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
133c0116
JD
4488 {
4489 hmargin = XFASTINT (Vtool_bar_button_margin);
4490 vmargin = XFASTINT (Vtool_bar_button_margin);
4491 }
4492 else if (CONSP (Vtool_bar_button_margin))
4493 {
d311d28c 4494 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
133c0116
JD
4495 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4496
d311d28c 4497 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
133c0116
JD
4498 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4499 }
4500
4501 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4502 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4503 i.e. zero. This means that margins less than
4504 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4505 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4506 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
c43923ad 4507
f392e843
JD
4508 if (! x->toolbar_widget)
4509 xg_create_tool_bar (f);
4510
98a92193 4511 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
f904c0f9 4512 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
088c8c09 4513
3afff00e
CY
4514 style = Ftool_bar_get_system_style ();
4515 text_image = EQ (style, Qtext_image_horiz);
4516 horiz = EQ (style, Qboth_horiz) || text_image;
4517
4518 for (i = j = 0; i < f->n_tool_bar_items; ++i)
f392e843 4519 {
f392e843
JD
4520 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4521 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4522 int idx;
13464394 4523 ptrdiff_t img_id;
98a92193
JD
4524 int icon_size = 0;
4525 struct image *img = NULL;
f392e843 4526 Lisp_Object image;
495effe5 4527 Lisp_Object stock = Qnil;
98a92193
JD
4528 GtkStockItem stock_item;
4529 char *stock_name = NULL;
2154c964 4530 char *icon_name = NULL;
98a92193 4531 Lisp_Object rtl;
b42ff099 4532 GtkWidget *wbutton = NULL;
d4ad8c04 4533 Lisp_Object specified_file;
d2bd5189 4534 int vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
3afff00e
CY
4535 const char *label
4536 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4537 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4538 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4539 : "";
d2bd5189 4540
3afff00e 4541 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
6e1440e6 4542
4039c786
CY
4543 /* If this is a separator, use a gtk separator item. */
4544 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4545 {
4546 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4547 {
4548 if (ti)
4549 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4550 GTK_WIDGET (ti));
4551 ti = gtk_separator_tool_item_new ();
3afff00e 4552 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4039c786 4553 }
3afff00e 4554 j++;
4039c786
CY
4555 continue;
4556 }
4557
4558 /* Otherwise, the tool-bar item is an ordinary button. */
4559
4560 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4561 {
4562 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4563 ti = NULL;
4564 }
f904c0f9 4565
3afff00e 4566 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
f392e843
JD
4567
4568 /* Ignore invalid image specifications. */
4039c786 4569 image = PROP (TOOL_BAR_ITEM_IMAGES);
f392e843
JD
4570 if (!valid_image_p (image))
4571 {
3afff00e
CY
4572 if (ti)
4573 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4574 GTK_WIDGET (ti));
f392e843
JD
4575 continue;
4576 }
4577
d4ad8c04 4578 specified_file = file_for_image (image);
4e6b227d
CY
4579 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4580 stock = call1 (Qx_gtk_map_stock, specified_file);
f392e843 4581
4e6b227d 4582 if (STRINGP (stock))
98a92193
JD
4583 {
4584 stock_name = SSDATA (stock);
2154c964
JD
4585 if (stock_name[0] == 'n' && stock_name[1] == ':')
4586 {
4587 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4588 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4589
4590 icon_name = stock_name + 2;
4591 stock_name = NULL;
4592 stock = Qnil;
4593
4594 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4595 icon_name = NULL;
4596 else
4597 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4598 }
4599 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
f904c0f9 4600 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
8e5a8840 4601 else
2154c964
JD
4602 {
4603 stock = Qnil;
4604 stock_name = NULL;
4605 }
98a92193 4606 }
2154c964
JD
4607
4608 if (stock_name == NULL && icon_name == NULL)
1d1885fc 4609 {
3afff00e
CY
4610 /* No stock image, or stock item not known. Try regular
4611 image. If image is a vector, choose it according to the
98a92193
JD
4612 button state. */
4613 if (dir == GTK_TEXT_DIR_RTL
4614 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4615 && STRINGP (rtl))
3afff00e 4616 image = find_rtl_image (f, image, rtl);
98a92193
JD
4617
4618 if (VECTORP (image))
4619 {
4620 if (enabled_p)
4621 idx = (selected_p
4622 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4623 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4624 else
4625 idx = (selected_p
4626 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4627 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4628
a54e2c05 4629 eassert (ASIZE (image) >= idx);
98a92193
JD
4630 image = AREF (image, idx);
4631 }
4632 else
4633 idx = -1;
4634
4635 img_id = lookup_image (f, image);
4636 img = IMAGE_FROM_ID (f, img_id);
4637 prepare_image_for_display (f, img);
8e5a8840 4638
98a92193
JD
4639 if (img->load_failed_p || img->pixmap == None)
4640 {
f904c0f9 4641 if (ti)
3afff00e
CY
4642 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4643 GTK_WIDGET (ti));
f904c0f9 4644 continue;
6e1440e6 4645 }
1d1885fc 4646 }
177c0ea7 4647
3afff00e
CY
4648 /* If there is an existing widget, check if it's stale; if so,
4649 remove it and make a new tool item from scratch. */
4650 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4651 img, label, horiz))
4652 {
4653 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4654 GTK_WIDGET (ti));
4655 ti = NULL;
4656 }
4657
6e1440e6 4658 if (ti == NULL)
ff24abfe 4659 {
98a92193 4660 GtkWidget *w;
3afff00e
CY
4661
4662 /* Save the image so we can see if an update is needed the
4663 next time we call xg_tool_item_match_p. */
4664 if (EQ (style, Qtext))
4665 w = NULL;
4666 else if (stock_name)
98a92193
JD
4667 {
4668 w = gtk_image_new_from_stock (stock_name, icon_size);
4669 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4670 (gpointer) xstrdup (stock_name),
4671 (GDestroyNotify) xfree);
4672 }
8e5a8840 4673 else if (icon_name)
2154c964
JD
4674 {
4675 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4676 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4677 (gpointer) xstrdup (icon_name),
4678 (GDestroyNotify) xfree);
4679 }
98a92193
JD
4680 else
4681 {
4682 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
98a92193
JD
4683 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4684 (gpointer)img->pixmap);
4685 }
4686
3afff00e
CY
4687 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4688 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4689 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
f904c0f9 4690 }
177c0ea7 4691
f392e843 4692#undef PROP
3afff00e
CY
4693
4694 gtk_widget_set_sensitive (wbutton, enabled_p);
4695 j++;
f392e843
JD
4696 }
4697
3afff00e 4698 /* Remove buttons not longer needed. */
6e1440e6 4699 do
f392e843 4700 {
21a76236 4701 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
3afff00e
CY
4702 if (ti)
4703 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
6e1440e6 4704 } while (ti != NULL);
f392e843 4705
bfeabdc3 4706 if (f->n_tool_bar_items != 0)
f392e843 4707 {
bfeabdc3 4708 if (pack_tool_bar)
e69b0960 4709 xg_pack_tool_bar (f, f->tool_bar_position);
3afff00e 4710 gtk_widget_show_all (GTK_WIDGET (x->handlebox_widget));
bfeabdc3
JD
4711 if (xg_update_tool_bar_sizes (f))
4712 xg_height_or_width_changed (f);
f392e843 4713 }
bfeabdc3 4714
f392e843
JD
4715 UNBLOCK_INPUT;
4716}
4717
71bacd48
JD
4718/* Deallocate all resources for the tool bar on frame F.
4719 Remove the tool bar. */
4720
f392e843 4721void
971de7fb 4722free_frame_tool_bar (FRAME_PTR f)
f392e843
JD
4723{
4724 struct x_output *x = f->output_data.x;
4725
4726 if (x->toolbar_widget)
4727 {
d3b2a6da 4728 int is_packed = x->handlebox_widget != 0;
f392e843 4729 BLOCK_INPUT;
d3b2a6da
JD
4730 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4731 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
088c8c09 4732 if (is_packed)
bfeabdc3
JD
4733 {
4734 if (x->toolbar_in_hbox)
4735 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4736 x->handlebox_widget);
4737 else
4738 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4739 x->handlebox_widget);
4740 }
d3b2a6da
JD
4741 else
4742 gtk_widget_destroy (x->toolbar_widget);
4743
f392e843
JD
4744 x->toolbar_widget = 0;
4745 x->handlebox_widget = 0;
bfeabdc3
JD
4746 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4747 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4748
4749 xg_height_or_width_changed (f);
f392e843 4750
f392e843
JD
4751 UNBLOCK_INPUT;
4752 }
4753}
4754
bfeabdc3
JD
4755int
4756xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4757{
4758 struct x_output *x = f->output_data.x;
4759
4760 if (! x->toolbar_widget || ! x->handlebox_widget)
4761 return 1;
4762
4763 BLOCK_INPUT;
4764 g_object_ref (x->handlebox_widget);
4765 if (x->toolbar_in_hbox)
4766 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4767 x->handlebox_widget);
4768 else
4769 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4770 x->handlebox_widget);
4771 xg_pack_tool_bar (f, pos);
4772 g_object_unref (x->handlebox_widget);
4773 if (xg_update_tool_bar_sizes (f))
4774 xg_height_or_width_changed (f);
4775
4776 UNBLOCK_INPUT;
4777 return 1;
4778}
4779
f392e843
JD
4780
4781\f
4782/***********************************************************************
4783 Initializing
f904c0f9 4784***********************************************************************/
f392e843 4785void
971de7fb 4786xg_initialize (void)
f392e843 4787{
9f6fcdc5 4788 GtkBindingSet *binding_set;
383b7c95 4789 GtkSettings *settings;
9f6fcdc5 4790
430e6c77
JD
4791#if HAVE_XFT
4792 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4793 we keep it permanently linked in. */
4794 XftInit (0);
4795#endif
879ffad9
JD
4796
4797 gdpy_def = NULL;
f392e843 4798 xg_ignore_gtk_scrollbar = 0;
da18b5ac 4799 xg_detached_menus = 0;
f392e843
JD
4800 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4801 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4802
81e302ef
JD
4803 id_to_widget.max_size = id_to_widget.used = 0;
4804 id_to_widget.widgets = 0;
4805
383b7c95
JD
4806 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4807 (gdk_display_get_default ()));
f392e843
JD
4808 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4809 bindings. It doesn't seem to be any way to remove properties,
4810 so we set it to VoidSymbol which in X means "no key". */
383b7c95 4811 gtk_settings_set_string_property (settings,
f392e843
JD
4812 "gtk-menu-bar-accel",
4813 "VoidSymbol",
4814 EMACS_CLASS);
81e302ef
JD
4815
4816 /* Make GTK text input widgets use Emacs style keybindings. This is
4817 Emacs after all. */
383b7c95 4818 gtk_settings_set_string_property (settings,
81e302ef
JD
4819 "gtk-key-theme-name",
4820 "Emacs",
4821 EMACS_CLASS);
9f6fcdc5
JD
4822
4823 /* Make dialogs close on C-g. Since file dialog inherits from
4824 dialog, this works for them also. */
dc2933eb 4825 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
0afb4571 4826 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
9f6fcdc5
JD
4827 "close", 0);
4828
4829 /* Make menus close on C-g. */
dc2933eb
JD
4830 binding_set = gtk_binding_set_by_class (g_type_class_ref
4831 (GTK_TYPE_MENU_SHELL));
0afb4571 4832 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
9f6fcdc5 4833 "cancel", 0);
c195f2de 4834 update_theme_scrollbar_width ();
f392e843
JD
4835}
4836
4837#endif /* USE_GTK */