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