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