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