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