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