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