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