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