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