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