* make-docfile.c: Simplify a bit, to simplify further refactoring.
[emacs.git] / src / gtkutil.c
blob8614fe57cb253c8f69328a48d62a3157f94bdab2
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <float.h>
24 #include <stdio.h>
26 #include <c-ctype.h>
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "gtkutil.h"
34 #include "termhooks.h"
35 #include "keyboard.h"
36 #include "charset.h"
37 #include "coding.h"
38 #include "font.h"
40 #include <gdk/gdkkeysyms.h>
41 #include "xsettings.h"
43 #ifdef HAVE_XFT
44 #include <X11/Xft/Xft.h>
45 #endif
47 #ifdef HAVE_GTK3
48 #include <gtk/gtkx.h>
49 #include "emacsgtkfixed.h"
50 #endif
52 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
53 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
55 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
56 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
58 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
59 #define gtk_widget_set_has_window(w, b) \
60 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
61 #endif
62 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
63 #define gtk_dialog_get_action_area(w) ((w)->action_area)
64 #define gtk_dialog_get_content_area(w) ((w)->vbox)
65 #endif
66 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
67 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
68 #endif
69 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
70 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
71 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
72 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
73 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
74 #endif
75 #if GTK_CHECK_VERSION (2, 12, 0)
76 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
77 #else
78 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
79 #endif
81 #ifdef HAVE_FREETYPE
82 #if GTK_CHECK_VERSION (3, 2, 0)
83 #define USE_NEW_GTK_FONT_CHOOSER 1
84 #else
85 #define USE_NEW_GTK_FONT_CHOOSER 0
86 #define gtk_font_chooser_dialog_new(x, y) \
87 gtk_font_selection_dialog_new (x)
88 #undef GTK_FONT_CHOOSER
89 #define GTK_FONT_CHOOSER(x) GTK_FONT_SELECTION_DIALOG (x)
90 #define gtk_font_chooser_set_font(x, y) \
91 gtk_font_selection_dialog_set_font_name (x, y)
92 #endif
93 #endif /* HAVE_FREETYPE */
95 #ifndef HAVE_GTK3
96 #ifdef USE_GTK_TOOLTIP
97 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
98 #endif
99 #define gdk_window_get_geometry(w, a, b, c, d) \
100 gdk_window_get_geometry (w, a, b, c, d, 0)
101 #define gdk_x11_window_lookup_for_display(d, w) \
102 gdk_xid_table_lookup_for_display (d, w)
103 #define gtk_box_new(ori, spacing) \
104 ((ori) == GTK_ORIENTATION_HORIZONTAL \
105 ? gtk_hbox_new (FALSE, (spacing)) : gtk_vbox_new (FALSE, (spacing)))
106 #define gtk_scrollbar_new(ori, spacing) \
107 ((ori) == GTK_ORIENTATION_HORIZONTAL \
108 ? gtk_hscrollbar_new ((spacing)) : gtk_vscrollbar_new ((spacing)))
109 #ifndef GDK_KEY_g
110 #define GDK_KEY_g GDK_g
111 #endif
112 #endif /* HAVE_GTK3 */
114 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
116 static void update_theme_scrollbar_width (void);
118 #define TB_INFO_KEY "xg_frame_tb_info"
119 struct xg_frame_tb_info
121 Lisp_Object last_tool_bar;
122 Lisp_Object style;
123 int n_last_items;
124 int hmargin, vmargin;
125 GtkTextDirection dir;
129 /***********************************************************************
130 Display handling functions
131 ***********************************************************************/
133 /* Keep track of the default display, or NULL if there is none. Emacs
134 may close all its displays. */
136 static GdkDisplay *gdpy_def;
138 /* When the GTK widget W is to be created on a display for F that
139 is not the default display, set the display for W.
140 W can be a GtkMenu or a GtkWindow widget. */
142 static void
143 xg_set_screen (GtkWidget *w, struct frame *f)
145 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
147 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
148 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
150 if (GTK_IS_MENU (w))
151 gtk_menu_set_screen (GTK_MENU (w), gscreen);
152 else
153 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
158 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
159 *DPY is set to NULL if the display can't be opened.
161 Returns non-zero if display could be opened, zero if display could not
162 be opened, and less than zero if the GTK version doesn't support
163 multiple displays. */
165 void
166 xg_display_open (char *display_name, Display **dpy)
168 GdkDisplay *gdpy;
170 gdpy = gdk_display_open (display_name);
171 if (!gdpy_def && gdpy)
173 gdpy_def = gdpy;
174 gdk_display_manager_set_default_display (gdk_display_manager_get (),
175 gdpy);
178 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
182 /* Close display DPY. */
184 void
185 xg_display_close (Display *dpy)
187 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
189 /* If this is the default display, try to change it before closing.
190 If there is no other display to use, gdpy_def is set to NULL, and
191 the next call to xg_display_open resets the default display. */
192 if (gdk_display_get_default () == gdpy)
194 struct x_display_info *dpyinfo;
195 GdkDisplay *gdpy_new = NULL;
197 /* Find another display. */
198 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
199 if (dpyinfo->display != dpy)
201 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
202 gdk_display_manager_set_default_display (gdk_display_manager_get (),
203 gdpy_new);
204 break;
206 gdpy_def = gdpy_new;
209 #if GTK_CHECK_VERSION (2, 0, 0) && ! GTK_CHECK_VERSION (2, 10, 0)
210 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
211 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
212 can continue running, but there will be memory leaks. */
213 g_object_run_dispose (G_OBJECT (gdpy));
214 #else
215 /* This seems to be fixed in GTK 2.10. */
216 gdk_display_close (gdpy);
217 #endif
221 /***********************************************************************
222 Utility functions
223 ***********************************************************************/
225 /* Create and return the cursor to be used for popup menus and
226 scroll bars on display DPY. */
228 GdkCursor *
229 xg_create_default_cursor (Display *dpy)
231 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
232 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
235 static GdkPixbuf *
236 xg_get_pixbuf_from_pixmap (struct frame *f, Pixmap pix)
238 int iunused;
239 GdkPixbuf *tmp_buf;
240 Window wunused;
241 unsigned int width, height, uunused;
242 XImage *xim;
244 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
245 &width, &height, &uunused, &uunused);
247 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
248 ~0, XYPixmap);
249 if (!xim) return 0;
251 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
252 GDK_COLORSPACE_RGB,
253 FALSE,
254 xim->bitmap_unit,
255 width,
256 height,
257 xim->bytes_per_line,
258 NULL,
259 NULL);
260 XDestroyImage (xim);
261 return tmp_buf;
264 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
266 static GdkPixbuf *
267 xg_get_pixbuf_from_pix_and_mask (struct frame *f,
268 Pixmap pix,
269 Pixmap mask)
271 int width, height;
272 GdkPixbuf *icon_buf, *tmp_buf;
274 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
275 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
276 g_object_unref (G_OBJECT (tmp_buf));
278 width = gdk_pixbuf_get_width (icon_buf);
279 height = gdk_pixbuf_get_height (icon_buf);
281 if (mask)
283 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
284 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
285 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
286 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
287 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
288 int y;
290 for (y = 0; y < height; ++y)
292 guchar *iconptr, *maskptr;
293 int x;
295 iconptr = pixels + y * rowstride;
296 maskptr = mask_pixels + y * mask_rowstride;
298 for (x = 0; x < width; ++x)
300 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
301 just R is sufficient. */
302 if (maskptr[0] == 0)
303 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
305 iconptr += rowstride/width;
306 maskptr += mask_rowstride/width;
310 g_object_unref (G_OBJECT (mask_buf));
313 return icon_buf;
316 static Lisp_Object
317 file_for_image (Lisp_Object image)
319 Lisp_Object specified_file = Qnil;
320 Lisp_Object tail;
322 for (tail = XCDR (image);
323 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
324 tail = XCDR (XCDR (tail)))
325 if (EQ (XCAR (tail), QCfile))
326 specified_file = XCAR (XCDR (tail));
328 return specified_file;
331 /* For the image defined in IMG, make and return a GtkImage. For displays with
332 8 planes or less we must make a GdkPixbuf and apply the mask manually.
333 Otherwise the highlighting and dimming the tool bar code in GTK does
334 will look bad. For display with more than 8 planes we just use the
335 pixmap and mask directly. For monochrome displays, GTK doesn't seem
336 able to use external pixmaps, it looks bad whatever we do.
337 The image is defined on the display where frame F is.
338 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
339 If OLD_WIDGET is NULL, a new widget is constructed and returned.
340 If OLD_WIDGET is not NULL, that widget is modified. */
342 static GtkWidget *
343 xg_get_image_for_pixmap (struct frame *f,
344 struct image *img,
345 GtkWidget *widget,
346 GtkImage *old_widget)
348 GdkPixbuf *icon_buf;
350 /* If we have a file, let GTK do all the image handling.
351 This seems to be the only way to make insensitive and activated icons
352 look good in all cases. */
353 Lisp_Object specified_file = file_for_image (img->spec);
354 Lisp_Object file;
356 /* We already loaded the image once before calling this
357 function, so this only fails if the image file has been removed.
358 In that case, use the pixmap already loaded. */
360 if (STRINGP (specified_file)
361 && STRINGP (file = x_find_image_file (specified_file)))
363 if (! old_widget)
364 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
365 else
366 gtk_image_set_from_file (old_widget, SSDATA (file));
368 return GTK_WIDGET (old_widget);
371 /* No file, do the image handling ourselves. This will look very bad
372 on a monochrome display, and sometimes bad on all displays with
373 certain themes. */
375 /* This is a workaround to make icons look good on pseudo color
376 displays. Apparently GTK expects the images to have an alpha
377 channel. If they don't, insensitive and activated icons will
378 look bad. This workaround does not work on monochrome displays,
379 and is strictly not needed on true color/static color displays (i.e.
380 16 bits and higher). But we do it anyway so we get a pixbuf that is
381 not associated with the img->pixmap. The img->pixmap may be removed
382 by clearing the image cache and then the tool bar redraw fails, since
383 Gtk+ assumes the pixmap is always there. */
384 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
386 if (icon_buf)
388 if (! old_widget)
389 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
390 else
391 gtk_image_set_from_pixbuf (old_widget, icon_buf);
393 g_object_unref (G_OBJECT (icon_buf));
396 return GTK_WIDGET (old_widget);
400 /* Set CURSOR on W and all widgets W contain. We must do like this
401 for scroll bars and menu because they create widgets internally,
402 and it is those widgets that are visible. */
404 static void
405 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
407 GdkWindow *window = gtk_widget_get_window (w);
408 GList *children = gdk_window_peek_children (window);
410 gdk_window_set_cursor (window, cursor);
412 /* The scroll bar widget has more than one GDK window (had to look at
413 the source to figure this out), and there is no way to set cursor
414 on widgets in GTK. So we must set the cursor for all GDK windows.
415 Ditto for menus. */
417 for ( ; children; children = g_list_next (children))
418 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
421 /* Insert NODE into linked LIST. */
423 static void
424 xg_list_insert (xg_list_node *list, xg_list_node *node)
426 xg_list_node *list_start = list->next;
428 if (list_start) list_start->prev = node;
429 node->next = list_start;
430 node->prev = 0;
431 list->next = node;
434 /* Remove NODE from linked LIST. */
436 static void
437 xg_list_remove (xg_list_node *list, xg_list_node *node)
439 xg_list_node *list_start = list->next;
440 if (node == list_start)
442 list->next = node->next;
443 if (list->next) list->next->prev = 0;
445 else
447 node->prev->next = node->next;
448 if (node->next) node->next->prev = node->prev;
452 /* Allocate and return a utf8 version of STR. If STR is already
453 utf8 or NULL, just return a copy of STR.
454 A new string is allocated and the caller must free the result
455 with g_free. */
457 static char *
458 get_utf8_string (const char *str)
460 char *utf8_str;
462 if (!str) return NULL;
464 /* If not UTF-8, try current locale. */
465 if (!g_utf8_validate (str, -1, NULL))
466 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
467 else
468 return g_strdup (str);
470 if (!utf8_str)
472 /* Probably some control characters in str. Escape them. */
473 ptrdiff_t len;
474 ptrdiff_t nr_bad = 0;
475 gsize bytes_read;
476 gsize bytes_written;
477 unsigned char *p = (unsigned char *)str;
478 char *cp, *up;
479 GError *err = NULL;
481 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
482 &bytes_written, &err))
483 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
485 ++nr_bad;
486 p += bytes_written+1;
487 g_error_free (err);
488 err = NULL;
491 if (err)
493 g_error_free (err);
494 err = NULL;
496 if (cp) g_free (cp);
498 len = strlen (str);
499 if ((min (PTRDIFF_MAX, SIZE_MAX) - len - 1) / 4 < nr_bad)
500 memory_full (SIZE_MAX);
501 up = utf8_str = xmalloc (len + nr_bad * 4 + 1);
502 p = (unsigned char *)str;
504 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
505 &bytes_written, &err))
506 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
508 memcpy (up, p, bytes_written);
509 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
510 up += bytes_written+4;
511 p += bytes_written+1;
512 g_error_free (err);
513 err = NULL;
516 if (cp)
518 strcat (utf8_str, cp);
519 g_free (cp);
521 if (err)
523 g_error_free (err);
524 err = NULL;
527 return utf8_str;
530 /* Check for special colors used in face spec for region face.
531 The colors are fetched from the Gtk+ theme.
532 Return true if color was found, false if not. */
534 bool
535 xg_check_special_colors (struct frame *f,
536 const char *color_name,
537 XColor *color)
539 bool success_p = 0;
540 bool get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
541 bool get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
543 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
544 return success_p;
546 block_input ();
548 #ifdef HAVE_GTK3
549 GtkStyleContext *gsty
550 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
551 GdkRGBA col;
552 char buf[sizeof "rgb://rrrr/gggg/bbbb"];
553 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
554 if (get_fg)
555 gtk_style_context_get_color (gsty, state, &col);
556 else
557 gtk_style_context_get_background_color (gsty, state, &col);
559 sprintf (buf, "rgb:%04x/%04x/%04x",
560 (int)(col.red * 65535),
561 (int)(col.green * 65535),
562 (int)(col.blue * 65535));
563 success_p = (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
564 buf, color)
565 != 0);
566 #else
567 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
568 GdkColor *grgb = get_bg
569 ? &gsty->bg[GTK_STATE_SELECTED]
570 : &gsty->fg[GTK_STATE_SELECTED];
572 color->red = grgb->red;
573 color->green = grgb->green;
574 color->blue = grgb->blue;
575 color->pixel = grgb->pixel;
576 success_p = 1;
577 #endif
580 unblock_input ();
581 return success_p;
586 /***********************************************************************
587 Tooltips
588 ***********************************************************************/
589 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
590 We use that to pop down the tooltip. This happens if Gtk+ for some
591 reason wants to change or hide the tooltip. */
593 #ifdef USE_GTK_TOOLTIP
595 static void
596 hierarchy_ch_cb (GtkWidget *widget,
597 GtkWidget *previous_toplevel,
598 gpointer user_data)
600 struct frame *f = user_data;
601 struct x_output *x = f->output_data.x;
602 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
604 if (! top || ! GTK_IS_WINDOW (top))
605 gtk_widget_hide (previous_toplevel);
608 /* Callback called when Gtk+ thinks a tooltip should be displayed.
609 We use it to get the tooltip window and the tooltip widget so
610 we can manipulate the ourselves.
612 Return FALSE ensures that the tooltip is not shown. */
614 static gboolean
615 qttip_cb (GtkWidget *widget,
616 gint xpos,
617 gint ypos,
618 gboolean keyboard_mode,
619 GtkTooltip *tooltip,
620 gpointer user_data)
622 struct frame *f = user_data;
623 struct x_output *x = f->output_data.x;
624 if (x->ttip_widget == NULL)
626 GtkWidget *p;
627 GList *list, *iter;
629 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
630 x->ttip_widget = tooltip;
631 g_object_ref (G_OBJECT (tooltip));
632 x->ttip_lbl = gtk_label_new ("");
633 g_object_ref (G_OBJECT (x->ttip_lbl));
634 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
635 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
637 /* Change stupid Gtk+ default line wrapping. */
638 p = gtk_widget_get_parent (x->ttip_lbl);
639 list = gtk_container_get_children (GTK_CONTAINER (p));
640 for (iter = list; iter; iter = g_list_next (iter))
642 GtkWidget *w = GTK_WIDGET (iter->data);
643 if (GTK_IS_LABEL (w))
644 gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
646 g_list_free (list);
648 /* ATK needs an empty title for some reason. */
649 gtk_window_set_title (x->ttip_window, "");
650 /* Realize so we can safely get screen later on. */
651 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
652 gtk_widget_realize (x->ttip_lbl);
654 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
655 G_CALLBACK (hierarchy_ch_cb), f);
657 return FALSE;
660 #endif /* USE_GTK_TOOLTIP */
662 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
663 Return true if a system tooltip is available. */
665 bool
666 xg_prepare_tooltip (struct frame *f,
667 Lisp_Object string,
668 int *width,
669 int *height)
671 #ifndef USE_GTK_TOOLTIP
672 return 0;
673 #else
674 struct x_output *x = f->output_data.x;
675 GtkWidget *widget;
676 GdkWindow *gwin;
677 GdkScreen *screen;
678 GtkSettings *settings;
679 gboolean tt_enabled = TRUE;
680 GtkRequisition req;
681 Lisp_Object encoded_string;
683 if (!x->ttip_lbl) return 0;
685 block_input ();
686 encoded_string = ENCODE_UTF_8 (string);
687 widget = GTK_WIDGET (x->ttip_lbl);
688 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
689 screen = gdk_window_get_screen (gwin);
690 settings = gtk_settings_get_for_screen (screen);
691 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
692 if (tt_enabled)
694 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
695 /* Record that we disabled it so it can be enabled again. */
696 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
697 (gpointer)f);
700 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
701 g_object_set_data (G_OBJECT
702 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
703 "gdk-display-current-tooltip", NULL);
705 /* Put our dummy widget in so we can get callbacks for unrealize and
706 hierarchy-changed. */
707 gtk_tooltip_set_custom (x->ttip_widget, widget);
708 gtk_tooltip_set_text (x->ttip_widget, SSDATA (encoded_string));
709 gtk_widget_get_preferred_size (GTK_WIDGET (x->ttip_window), NULL, &req);
710 if (width) *width = req.width;
711 if (height) *height = req.height;
713 unblock_input ();
715 return 1;
716 #endif /* USE_GTK_TOOLTIP */
719 /* Show the tooltip at ROOT_X and ROOT_Y.
720 xg_prepare_tooltip must have been called before this function. */
722 void
723 xg_show_tooltip (struct frame *f, int root_x, int root_y)
725 #ifdef USE_GTK_TOOLTIP
726 struct x_output *x = f->output_data.x;
727 if (x->ttip_window)
729 block_input ();
730 gtk_window_move (x->ttip_window, root_x, root_y);
731 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
732 unblock_input ();
734 #endif
737 /* Hide tooltip if shown. Do nothing if not shown.
738 Return true if tip was hidden, false if not (i.e. not using
739 system tooltips). */
741 bool
742 xg_hide_tooltip (struct frame *f)
744 bool ret = 0;
745 #ifdef USE_GTK_TOOLTIP
746 if (f->output_data.x->ttip_window)
748 GtkWindow *win = f->output_data.x->ttip_window;
749 block_input ();
750 gtk_widget_hide (GTK_WIDGET (win));
752 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
754 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
755 GdkScreen *screen = gdk_window_get_screen (gwin);
756 GtkSettings *settings = gtk_settings_get_for_screen (screen);
757 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
759 unblock_input ();
761 ret = 1;
763 #endif
764 return ret;
768 /***********************************************************************
769 General functions for creating widgets, resizing, events, e.t.c.
770 ***********************************************************************/
772 static void
773 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
774 const gchar *msg, gpointer user_data)
776 if (!strstr (msg, "visible children"))
777 fprintf (stderr, "XX %s-WARNING **: %s\n", log_domain, msg);
780 /* Make a geometry string and pass that to GTK. It seems this is the
781 only way to get geometry position right if the user explicitly
782 asked for a position when starting Emacs.
783 F is the frame we shall set geometry for. */
785 static void
786 xg_set_geometry (struct frame *f)
788 if (f->size_hint_flags & (USPosition | PPosition))
790 int left = f->left_pos;
791 int xneg = f->size_hint_flags & XNegative;
792 int top = f->top_pos;
793 int yneg = f->size_hint_flags & YNegative;
794 char geom_str[sizeof "=x--" + 4 * INT_STRLEN_BOUND (int)];
795 guint id;
797 if (xneg)
798 left = -left;
799 if (yneg)
800 top = -top;
802 sprintf (geom_str, "=%dx%d%c%d%c%d",
803 FRAME_PIXEL_WIDTH (f),
804 FRAME_PIXEL_HEIGHT (f),
805 (xneg ? '-' : '+'), left,
806 (yneg ? '-' : '+'), top);
808 /* Silence warning about visible children. */
809 id = g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
810 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
812 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
813 geom_str))
814 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
816 g_log_remove_handler ("Gtk", id);
820 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
821 and use a GtkFixed widget, this doesn't happen automatically. */
823 static void
824 xg_clear_under_internal_border (struct frame *f)
826 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
828 GtkWidget *wfixed = f->output_data.x->edit_widget;
830 gtk_widget_queue_draw (wfixed);
831 gdk_window_process_all_updates ();
833 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
834 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
836 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0, 0,
837 FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
839 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), 0,
840 FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
841 FRAME_PIXEL_WIDTH (f), FRAME_INTERNAL_BORDER_WIDTH (f));
843 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
844 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
845 0, FRAME_INTERNAL_BORDER_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
849 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
850 and a Gtk+ menu bar, we get resize events for the edit part of the
851 frame only. We let Gtk+ deal with the Gtk+ parts.
852 F is the frame to resize.
853 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
855 void
856 xg_frame_resized (struct frame *f, int pixelwidth, int pixelheight)
858 int width, height;
860 if (pixelwidth == -1 && pixelheight == -1)
862 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
863 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
864 0, 0,
865 &pixelwidth, &pixelheight);
866 else return;
870 width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth);
871 height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight);
873 if (width != FRAME_TEXT_WIDTH (f)
874 || height != FRAME_TEXT_HEIGHT (f)
875 || pixelwidth != FRAME_PIXEL_WIDTH (f)
876 || pixelheight != FRAME_PIXEL_HEIGHT (f))
878 FRAME_PIXEL_WIDTH (f) = pixelwidth;
879 FRAME_PIXEL_HEIGHT (f) = pixelheight;
881 xg_clear_under_internal_border (f);
882 change_frame_size (f, width, height, 0, 1, 0, 1);
883 SET_FRAME_GARBAGED (f);
884 cancel_mouse_face (f);
888 /* Resize the outer window of frame F after changing the height.
889 COLUMNS/ROWS is the size the edit area shall have after the resize. */
891 void
892 xg_frame_set_char_size (struct frame *f, int width, int height)
894 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
895 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
897 if (FRAME_PIXEL_HEIGHT (f) == 0)
898 return;
900 /* Do this before resize, as we don't know yet if we will be resized. */
901 xg_clear_under_internal_border (f);
903 /* Must resize our top level widget. Font size may have changed,
904 but not rows/cols. */
905 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
906 pixelwidth + FRAME_TOOLBAR_WIDTH (f),
907 pixelheight + FRAME_TOOLBAR_HEIGHT (f)
908 + FRAME_MENUBAR_HEIGHT (f));
909 x_wm_set_size_hint (f, 0, 0);
911 SET_FRAME_GARBAGED (f);
912 cancel_mouse_face (f);
914 /* We can not call change_frame_size for a mapped frame,
915 we can not set pixel width/height either. The window manager may
916 override our resize request, XMonad does this all the time.
917 The best we can do is try to sync, so lisp code sees the updated
918 size as fast as possible.
919 For unmapped windows, we can set rows/cols. When
920 the frame is mapped again we will (hopefully) get the correct size. */
921 if (FRAME_VISIBLE_P (f))
923 /* Must call this to flush out events */
924 (void)gtk_events_pending ();
925 gdk_flush ();
926 x_wait_for_event (f, ConfigureNotify);
928 else
929 change_frame_size (f, width, height, 0, 1, 0, 1);
932 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
933 The policy is to keep the number of editable lines. */
935 static void
936 xg_height_or_width_changed (struct frame *f)
938 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
939 FRAME_TOTAL_PIXEL_WIDTH (f),
940 FRAME_TOTAL_PIXEL_HEIGHT (f));
941 f->output_data.x->hint_flags = 0;
942 x_wm_set_size_hint (f, 0, 0);
945 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
946 Must be done like this, because GtkWidget:s can have "hidden"
947 X Window that aren't accessible.
949 Return 0 if no widget match WDESC. */
951 GtkWidget *
952 xg_win_to_widget (Display *dpy, Window wdesc)
954 gpointer gdkwin;
955 GtkWidget *gwdesc = 0;
957 block_input ();
959 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
960 wdesc);
961 if (gdkwin)
963 GdkEvent event;
964 event.any.window = gdkwin;
965 event.any.type = GDK_NOTHING;
966 gwdesc = gtk_get_event_widget (&event);
969 unblock_input ();
970 return gwdesc;
973 /* Set the background of widget W to PIXEL. */
975 static void
976 xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
978 #ifdef HAVE_GTK3
979 GdkRGBA bg;
980 XColor xbg;
981 xbg.pixel = pixel;
982 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
984 bg.red = (double)xbg.red/65535.0;
985 bg.green = (double)xbg.green/65535.0;
986 bg.blue = (double)xbg.blue/65535.0;
987 bg.alpha = 1.0;
988 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
990 #else
991 GdkColor bg;
992 GdkColormap *map = gtk_widget_get_colormap (w);
993 gdk_colormap_query_color (map, pixel, &bg);
994 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
995 #endif
998 /* Callback called when the gtk theme changes.
999 We notify lisp code so it can fix faces used for region for example. */
1001 static void
1002 style_changed_cb (GObject *go,
1003 GParamSpec *spec,
1004 gpointer user_data)
1006 struct input_event event;
1007 GdkDisplay *gdpy = user_data;
1008 const char *display_name = gdk_display_get_name (gdpy);
1009 Display *dpy = GDK_DISPLAY_XDISPLAY (gdpy);
1011 EVENT_INIT (event);
1012 event.kind = CONFIG_CHANGED_EVENT;
1013 event.frame_or_window = build_string (display_name);
1014 /* Theme doesn't change often, so intern is called seldom. */
1015 event.arg = intern ("theme-name");
1016 kbd_buffer_store_event (&event);
1018 update_theme_scrollbar_width ();
1020 /* If scroll bar width changed, we need set the new size on all frames
1021 on this display. */
1022 if (dpy)
1024 Lisp_Object rest, frame;
1025 FOR_EACH_FRAME (rest, frame)
1027 struct frame *f = XFRAME (frame);
1028 if (FRAME_LIVE_P (f)
1029 && FRAME_X_P (f)
1030 && FRAME_X_DISPLAY (f) == dpy)
1032 x_set_scroll_bar_default_width (f);
1033 xg_frame_set_char_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f));
1039 /* Called when a delete-event occurs on WIDGET. */
1041 static gboolean
1042 delete_cb (GtkWidget *widget,
1043 GdkEvent *event,
1044 gpointer user_data)
1046 #ifdef HAVE_GTK3
1047 /* The event doesn't arrive in the normal event loop. Send event
1048 here. */
1049 struct frame *f = user_data;
1050 struct input_event ie;
1052 EVENT_INIT (ie);
1053 ie.kind = DELETE_WINDOW_EVENT;
1054 XSETFRAME (ie.frame_or_window, f);
1055 kbd_buffer_store_event (&ie);
1056 #endif
1058 return TRUE;
1061 /* Create and set up the GTK widgets for frame F.
1062 Return true if creation succeeded. */
1064 bool
1065 xg_create_frame_widgets (struct frame *f)
1067 GtkWidget *wtop;
1068 GtkWidget *wvbox, *whbox;
1069 GtkWidget *wfixed;
1070 #ifndef HAVE_GTK3
1071 GtkRcStyle *style;
1072 #endif
1073 char *title = 0;
1075 block_input ();
1077 if (FRAME_X_EMBEDDED_P (f))
1079 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1080 wtop = gtk_plug_new_for_display (gdpy, f->output_data.x->parent_desc);
1082 else
1083 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1085 /* gtk_window_set_has_resize_grip is a Gtk+ 3.0 function but Ubuntu
1086 has backported it to Gtk+ 2.0 and they add the resize grip for
1087 Gtk+ 2.0 applications also. But it has a bug that makes Emacs loop
1088 forever, so disable the grip. */
1089 #if (! GTK_CHECK_VERSION (3, 0, 0) \
1090 && defined HAVE_GTK_WINDOW_SET_HAS_RESIZE_GRIP)
1091 gtk_window_set_has_resize_grip (GTK_WINDOW (wtop), FALSE);
1092 #endif
1094 xg_set_screen (wtop, f);
1096 wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1097 whbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1098 gtk_box_set_homogeneous (GTK_BOX (wvbox), FALSE);
1099 gtk_box_set_homogeneous (GTK_BOX (whbox), FALSE);
1101 #ifdef HAVE_GTK3
1102 wfixed = emacs_fixed_new (f);
1103 #else
1104 wfixed = gtk_fixed_new ();
1105 #endif
1107 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1109 if (wtop) gtk_widget_destroy (wtop);
1110 if (wvbox) gtk_widget_destroy (wvbox);
1111 if (whbox) gtk_widget_destroy (whbox);
1112 if (wfixed) gtk_widget_destroy (wfixed);
1114 unblock_input ();
1115 return 0;
1118 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1119 gtk_widget_set_name (wtop, EMACS_CLASS);
1120 gtk_widget_set_name (wvbox, "pane");
1121 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1123 /* If this frame has a title or name, set it in the title bar. */
1124 if (! NILP (f->title))
1125 title = SSDATA (ENCODE_UTF_8 (f->title));
1126 else if (! NILP (f->name))
1127 title = SSDATA (ENCODE_UTF_8 (f->name));
1129 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1131 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1132 FRAME_GTK_WIDGET (f) = wfixed;
1133 f->output_data.x->vbox_widget = wvbox;
1134 f->output_data.x->hbox_widget = whbox;
1136 gtk_widget_set_has_window (wfixed, TRUE);
1138 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1139 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1140 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1142 if (FRAME_EXTERNAL_TOOL_BAR (f))
1143 update_frame_tool_bar (f);
1145 /* We don't want this widget double buffered, because we draw on it
1146 with regular X drawing primitives, so from a GTK/GDK point of
1147 view, the widget is totally blank. When an expose comes, this
1148 will make the widget blank, and then Emacs redraws it. This flickers
1149 a lot, so we turn off double buffering. */
1150 gtk_widget_set_double_buffered (wfixed, FALSE);
1152 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1153 SSDATA (Vx_resource_name),
1154 SSDATA (Vx_resource_class));
1156 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1157 GTK is to destroy the widget. We want Emacs to do that instead. */
1158 g_signal_connect (G_OBJECT (wtop), "delete-event",
1159 G_CALLBACK (delete_cb), f);
1161 /* Convert our geometry parameters into a geometry string
1162 and specify it.
1163 GTK will itself handle calculating the real position this way. */
1164 xg_set_geometry (f);
1165 f->win_gravity
1166 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1168 gtk_widget_add_events (wfixed,
1169 GDK_POINTER_MOTION_MASK
1170 | GDK_EXPOSURE_MASK
1171 | GDK_BUTTON_PRESS_MASK
1172 | GDK_BUTTON_RELEASE_MASK
1173 | GDK_KEY_PRESS_MASK
1174 | GDK_ENTER_NOTIFY_MASK
1175 | GDK_LEAVE_NOTIFY_MASK
1176 | GDK_FOCUS_CHANGE_MASK
1177 | GDK_STRUCTURE_MASK
1178 | GDK_VISIBILITY_NOTIFY_MASK);
1180 /* Must realize the windows so the X window gets created. It is used
1181 by callers of this function. */
1182 gtk_widget_realize (wfixed);
1183 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1185 /* Since GTK clears its window by filling with the background color,
1186 we must keep X and GTK background in sync. */
1187 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1189 #ifndef HAVE_GTK3
1190 /* Also, do not let any background pixmap to be set, this looks very
1191 bad as Emacs overwrites the background pixmap with its own idea
1192 of background color. */
1193 style = gtk_widget_get_modifier_style (wfixed);
1195 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1196 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1197 gtk_widget_modify_style (wfixed, style);
1198 #else
1199 gtk_widget_set_can_focus (wfixed, TRUE);
1200 gtk_window_set_resizable (GTK_WINDOW (wtop), TRUE);
1201 #endif
1203 #ifdef USE_GTK_TOOLTIP
1204 /* Steal a tool tip window we can move ourselves. */
1205 f->output_data.x->ttip_widget = 0;
1206 f->output_data.x->ttip_lbl = 0;
1207 f->output_data.x->ttip_window = 0;
1208 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1209 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1210 #endif
1213 GdkScreen *screen = gtk_widget_get_screen (wtop);
1214 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1215 /* Only connect this signal once per screen. */
1216 if (! g_signal_handler_find (G_OBJECT (gs),
1217 G_SIGNAL_MATCH_FUNC,
1218 0, 0, 0,
1219 G_CALLBACK (style_changed_cb),
1222 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1223 G_CALLBACK (style_changed_cb),
1224 gdk_screen_get_display (screen));
1228 unblock_input ();
1230 return 1;
1233 void
1234 xg_free_frame_widgets (struct frame *f)
1236 if (FRAME_GTK_OUTER_WIDGET (f))
1238 #ifdef USE_GTK_TOOLTIP
1239 struct x_output *x = f->output_data.x;
1240 #endif
1241 struct xg_frame_tb_info *tbinfo
1242 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
1243 TB_INFO_KEY);
1244 if (tbinfo)
1245 xfree (tbinfo);
1247 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1248 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1249 FRAME_GTK_OUTER_WIDGET (f) = 0;
1250 #ifdef USE_GTK_TOOLTIP
1251 if (x->ttip_lbl)
1252 gtk_widget_destroy (x->ttip_lbl);
1253 if (x->ttip_widget)
1254 g_object_unref (G_OBJECT (x->ttip_widget));
1255 #endif
1259 /* Set the normal size hints for the window manager, for frame F.
1260 FLAGS is the flags word to use--or 0 meaning preserve the flags
1261 that the window now has.
1262 If USER_POSITION, set the User Position
1263 flag (this is useful when FLAGS is 0). */
1265 void
1266 x_wm_set_size_hint (struct frame *f, long int flags, bool user_position)
1268 /* Must use GTK routines here, otherwise GTK resets the size hints
1269 to its own defaults. */
1270 GdkGeometry size_hints;
1271 gint hint_flags = 0;
1272 int base_width, base_height;
1273 int min_rows = 0, min_cols = 0;
1274 int win_gravity = f->win_gravity;
1275 Lisp_Object fs_state, frame;
1277 /* Don't set size hints during initialization; that apparently leads
1278 to a race condition. See the thread at
1279 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1280 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1281 return;
1283 XSETFRAME (frame, f);
1284 fs_state = Fframe_parameter (frame, Qfullscreen);
1285 if (EQ (fs_state, Qmaximized) || EQ (fs_state, Qfullboth))
1287 /* Don't set hints when maximized or fullscreen. Apparently KWin and
1288 Gtk3 don't get along and the frame shrinks (!).
1290 return;
1293 if (flags)
1295 memset (&size_hints, 0, sizeof (size_hints));
1296 f->output_data.x->size_hints = size_hints;
1297 f->output_data.x->hint_flags = hint_flags;
1299 else
1300 flags = f->size_hint_flags;
1302 size_hints = f->output_data.x->size_hints;
1303 hint_flags = f->output_data.x->hint_flags;
1305 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1306 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
1307 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
1309 hint_flags |= GDK_HINT_BASE_SIZE;
1310 /* Use one row/col here so base_height/width does not become zero.
1311 Gtk+ and/or Unity on Ubuntu 12.04 can't handle it. */
1312 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 1) + FRAME_TOOLBAR_WIDTH (f);
1313 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 1)
1314 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1316 check_frame_size (f, &min_cols, &min_rows, 0);
1317 if (min_cols > 0) --min_cols; /* We used one col in base_width = ... 1); */
1318 if (min_rows > 0) --min_rows; /* We used one row in base_height = ... 1); */
1320 size_hints.base_width = base_width;
1321 size_hints.base_height = base_height;
1322 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);;
1323 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
1325 /* These currently have a one to one mapping with the X values, but I
1326 don't think we should rely on that. */
1327 hint_flags |= GDK_HINT_WIN_GRAVITY;
1328 size_hints.win_gravity = 0;
1329 if (win_gravity == NorthWestGravity)
1330 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1331 else if (win_gravity == NorthGravity)
1332 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1333 else if (win_gravity == NorthEastGravity)
1334 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1335 else if (win_gravity == WestGravity)
1336 size_hints.win_gravity = GDK_GRAVITY_WEST;
1337 else if (win_gravity == CenterGravity)
1338 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1339 else if (win_gravity == EastGravity)
1340 size_hints.win_gravity = GDK_GRAVITY_EAST;
1341 else if (win_gravity == SouthWestGravity)
1342 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1343 else if (win_gravity == SouthGravity)
1344 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1345 else if (win_gravity == SouthEastGravity)
1346 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1347 else if (win_gravity == StaticGravity)
1348 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1350 if (user_position)
1352 hint_flags &= ~GDK_HINT_POS;
1353 hint_flags |= GDK_HINT_USER_POS;
1356 if (hint_flags != f->output_data.x->hint_flags
1357 || memcmp (&size_hints,
1358 &f->output_data.x->size_hints,
1359 sizeof (size_hints)) != 0)
1361 block_input ();
1362 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1363 NULL, &size_hints, hint_flags);
1364 f->output_data.x->size_hints = size_hints;
1365 f->output_data.x->hint_flags = hint_flags;
1366 unblock_input ();
1370 /* Change background color of a frame.
1371 Since GTK uses the background color to clear the window, we must
1372 keep the GTK and X colors in sync.
1373 F is the frame to change,
1374 BG is the pixel value to change to. */
1376 void
1377 xg_set_background_color (struct frame *f, unsigned long bg)
1379 if (FRAME_GTK_WIDGET (f))
1381 block_input ();
1382 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1383 unblock_input ();
1388 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1389 functions so GTK does not overwrite the icon. */
1391 void
1392 xg_set_frame_icon (struct frame *f, Pixmap icon_pixmap, Pixmap icon_mask)
1394 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1395 icon_pixmap,
1396 icon_mask);
1397 if (gp)
1398 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1403 /***********************************************************************
1404 Dialog functions
1405 ***********************************************************************/
1406 /* Return the dialog title to use for a dialog of type KEY.
1407 This is the encoding used by lwlib. We use the same for GTK. */
1409 static const char *
1410 get_dialog_title (char key)
1412 const char *title = "";
1414 switch (key) {
1415 case 'E': case 'e':
1416 title = "Error";
1417 break;
1419 case 'I': case 'i':
1420 title = "Information";
1421 break;
1423 case 'L': case 'l':
1424 title = "Prompt";
1425 break;
1427 case 'P': case 'p':
1428 title = "Prompt";
1429 break;
1431 case 'Q': case 'q':
1432 title = "Question";
1433 break;
1436 return title;
1439 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1440 the dialog, but return TRUE so the event does not propagate further
1441 in GTK. This prevents GTK from destroying the dialog widget automatically
1442 and we can always destroy the widget manually, regardless of how
1443 it was popped down (button press or WM_DELETE_WINDOW).
1444 W is the dialog widget.
1445 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1446 user_data is NULL (not used).
1448 Returns TRUE to end propagation of event. */
1450 static gboolean
1451 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1453 gtk_widget_unmap (w);
1454 return TRUE;
1457 /* Create a popup dialog window. See also xg_create_widget below.
1458 WV is a widget_value describing the dialog.
1459 SELECT_CB is the callback to use when a button has been pressed.
1460 DEACTIVATE_CB is the callback to use when the dialog pops down.
1462 Returns the GTK dialog widget. */
1464 static GtkWidget *
1465 create_dialog (widget_value *wv,
1466 GCallback select_cb,
1467 GCallback deactivate_cb)
1469 const char *title = get_dialog_title (wv->name[0]);
1470 int total_buttons = wv->name[1] - '0';
1471 int right_buttons = wv->name[4] - '0';
1472 int left_buttons;
1473 int button_nr = 0;
1474 int button_spacing = 10;
1475 GtkWidget *wdialog = gtk_dialog_new ();
1476 GtkDialog *wd = GTK_DIALOG (wdialog);
1477 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1478 widget_value *item;
1479 GtkWidget *whbox_down;
1481 /* If the number of buttons is greater than 4, make two rows of buttons
1482 instead. This looks better. */
1483 bool make_two_rows = total_buttons > 4;
1485 if (right_buttons == 0) right_buttons = total_buttons/2;
1486 left_buttons = total_buttons - right_buttons;
1488 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1489 gtk_widget_set_name (wdialog, "emacs-dialog");
1492 if (make_two_rows)
1494 GtkWidget *wvbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, button_spacing);
1495 GtkWidget *whbox_up = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1496 gtk_box_set_homogeneous (GTK_BOX (wvbox), TRUE);
1497 gtk_box_set_homogeneous (GTK_BOX (whbox_up), FALSE);
1498 whbox_down = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1499 gtk_box_set_homogeneous (GTK_BOX (whbox_down), FALSE);
1501 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1502 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1503 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1505 cur_box = GTK_BOX (whbox_up);
1508 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1509 G_CALLBACK (dialog_delete_callback), 0);
1511 if (deactivate_cb)
1513 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1514 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1517 for (item = wv->contents; item; item = item->next)
1519 char *utf8_label = get_utf8_string (item->value);
1520 GtkWidget *w;
1521 GtkRequisition req;
1523 if (item->name && strcmp (item->name, "message") == 0)
1525 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1526 /* This is the text part of the dialog. */
1527 w = gtk_label_new (utf8_label);
1528 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1529 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1530 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1532 /* Try to make dialog look better. Must realize first so
1533 the widget can calculate the size it needs. */
1534 gtk_widget_realize (w);
1535 gtk_widget_get_preferred_size (w, NULL, &req);
1536 gtk_box_set_spacing (wvbox, req.height);
1537 if (item->value && strlen (item->value) > 0)
1538 button_spacing = 2*req.width/strlen (item->value);
1540 else
1542 /* This is one button to add to the dialog. */
1543 w = gtk_button_new_with_label (utf8_label);
1544 if (! item->enabled)
1545 gtk_widget_set_sensitive (w, FALSE);
1546 if (select_cb)
1547 g_signal_connect (G_OBJECT (w), "clicked",
1548 select_cb, item->call_data);
1550 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1551 if (++button_nr == left_buttons)
1553 if (make_two_rows)
1554 cur_box = GTK_BOX (whbox_down);
1555 else
1556 gtk_box_pack_start (cur_box,
1557 gtk_label_new (""),
1558 TRUE, TRUE,
1559 button_spacing);
1563 if (utf8_label)
1564 g_free (utf8_label);
1567 return wdialog;
1570 struct xg_dialog_data
1572 GMainLoop *loop;
1573 int response;
1574 GtkWidget *w;
1575 guint timerid;
1578 /* Function that is called when the file or font dialogs pop down.
1579 W is the dialog widget, RESPONSE is the response code.
1580 USER_DATA is what we passed in to g_signal_connect. */
1582 static void
1583 xg_dialog_response_cb (GtkDialog *w,
1584 gint response,
1585 gpointer user_data)
1587 struct xg_dialog_data *dd = user_data;
1588 dd->response = response;
1589 g_main_loop_quit (dd->loop);
1593 /* Destroy the dialog. This makes it pop down. */
1595 static void
1596 pop_down_dialog (void *arg)
1598 struct xg_dialog_data *dd = arg;
1600 block_input ();
1601 if (dd->w) gtk_widget_destroy (dd->w);
1602 if (dd->timerid != 0) g_source_remove (dd->timerid);
1604 g_main_loop_quit (dd->loop);
1605 g_main_loop_unref (dd->loop);
1607 unblock_input ();
1610 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1611 We pass in DATA as gpointer* so we can use this as a callback. */
1613 static gboolean
1614 xg_maybe_add_timer (gpointer data)
1616 struct xg_dialog_data *dd = data;
1617 struct timespec next_time = timer_check ();
1619 dd->timerid = 0;
1621 if (timespec_valid_p (next_time))
1623 time_t s = next_time.tv_sec;
1624 int per_ms = TIMESPEC_RESOLUTION / 1000;
1625 int ms = (next_time.tv_nsec + per_ms - 1) / per_ms;
1626 if (s <= ((guint) -1 - ms) / 1000)
1627 dd->timerid = g_timeout_add (s * 1000 + ms, xg_maybe_add_timer, dd);
1629 return FALSE;
1633 /* Pops up a modal dialog W and waits for response.
1634 We don't use gtk_dialog_run because we want to process emacs timers.
1635 The dialog W is not destroyed when this function returns. */
1637 static int
1638 xg_dialog_run (struct frame *f, GtkWidget *w)
1640 ptrdiff_t count = SPECPDL_INDEX ();
1641 struct xg_dialog_data dd;
1643 xg_set_screen (w, f);
1644 gtk_window_set_transient_for (GTK_WINDOW (w),
1645 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1646 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1647 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1649 dd.loop = g_main_loop_new (NULL, FALSE);
1650 dd.response = GTK_RESPONSE_CANCEL;
1651 dd.w = w;
1652 dd.timerid = 0;
1654 g_signal_connect (G_OBJECT (w),
1655 "response",
1656 G_CALLBACK (xg_dialog_response_cb),
1657 &dd);
1658 /* Don't destroy the widget if closed by the window manager close button. */
1659 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1660 gtk_widget_show (w);
1662 record_unwind_protect_ptr (pop_down_dialog, &dd);
1664 (void) xg_maybe_add_timer (&dd);
1665 g_main_loop_run (dd.loop);
1667 dd.w = 0;
1668 unbind_to (count, Qnil);
1670 return dd.response;
1674 /***********************************************************************
1675 File dialog functions
1676 ***********************************************************************/
1677 /* Return true if the old file selection dialog is being used. */
1679 bool
1680 xg_uses_old_file_dialog (void)
1682 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1683 return x_gtk_use_old_file_dialog;
1684 #else
1685 return 0;
1686 #endif
1690 typedef char * (*xg_get_file_func) (GtkWidget *);
1692 /* Return the selected file for file chooser dialog W.
1693 The returned string must be free:d. */
1695 static char *
1696 xg_get_file_name_from_chooser (GtkWidget *w)
1698 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1701 /* Callback called when the "Show hidden files" toggle is pressed.
1702 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1704 static void
1705 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1707 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1708 gboolean visible;
1709 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1710 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1714 /* Callback called when a property changes in a file chooser.
1715 GOBJECT is the file chooser dialog, ARG1 describes the property.
1716 USER_DATA is the toggle widget in the file chooser dialog.
1717 We use this to update the "Show hidden files" toggle when the user
1718 changes that property by right clicking in the file list. */
1720 static void
1721 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1723 if (strcmp (arg1->name, "show-hidden") == 0)
1725 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1726 gboolean visible, toggle_on;
1728 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1729 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1731 if (!!visible != !!toggle_on)
1733 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1734 G_CALLBACK (xg_toggle_visibility_cb),
1735 gobject);
1736 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1737 g_signal_handlers_unblock_by_func
1738 (G_OBJECT (wtoggle),
1739 G_CALLBACK (xg_toggle_visibility_cb),
1740 gobject);
1742 x_gtk_show_hidden_files = visible;
1746 /* Read a file name from the user using a file chooser dialog.
1747 F is the current frame.
1748 PROMPT is a prompt to show to the user. May not be NULL.
1749 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1750 If MUSTMATCH_P, the returned file name must be an existing
1751 file. (Actually, this only has cosmetic effects, the user can
1752 still enter a non-existing file.) *FUNC is set to a function that
1753 can be used to retrieve the selected file name from the returned widget.
1755 Returns the created widget. */
1757 static GtkWidget *
1758 xg_get_file_with_chooser (struct frame *f,
1759 char *prompt,
1760 char *default_filename,
1761 bool mustmatch_p, bool only_dir_p,
1762 xg_get_file_func *func)
1764 char msgbuf[1024];
1766 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1767 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1768 GtkFileChooserAction action = (mustmatch_p ?
1769 GTK_FILE_CHOOSER_ACTION_OPEN :
1770 GTK_FILE_CHOOSER_ACTION_SAVE);
1772 if (only_dir_p)
1773 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1775 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1776 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1777 (mustmatch_p || only_dir_p ?
1778 GTK_STOCK_OPEN : GTK_STOCK_OK),
1779 GTK_RESPONSE_OK,
1780 NULL);
1781 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1783 wbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1784 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
1785 gtk_widget_show (wbox);
1786 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1788 if (x_gtk_show_hidden_files)
1790 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1791 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1793 gtk_widget_show (wtoggle);
1794 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1795 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1796 g_signal_connect (G_OBJECT (filewin), "notify",
1797 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1799 if (x_gtk_file_dialog_help_text)
1801 msgbuf[0] = '\0';
1802 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1803 Show the C-l help text only for versions < 2.10. */
1804 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1805 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1806 strcat (msgbuf, "\nIf you don't like this file selector, use the "
1807 "corresponding\nkey binding or customize "
1808 "use-file-dialog to turn it off.");
1810 wmessage = gtk_label_new (msgbuf);
1811 gtk_widget_show (wmessage);
1814 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1815 if (x_gtk_file_dialog_help_text)
1816 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1817 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1819 if (default_filename)
1821 Lisp_Object file;
1822 struct gcpro gcpro1;
1823 char *utf8_filename;
1824 GCPRO1 (file);
1826 file = build_string (default_filename);
1828 /* File chooser does not understand ~/... in the file name. It must be
1829 an absolute name starting with /. */
1830 if (default_filename[0] != '/')
1831 file = Fexpand_file_name (file, Qnil);
1833 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1834 if (! NILP (Ffile_directory_p (file)))
1835 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1836 utf8_filename);
1837 else
1839 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1840 utf8_filename);
1841 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1843 char *cp = strrchr (utf8_filename, '/');
1844 if (cp) ++cp;
1845 else cp = utf8_filename;
1846 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1850 UNGCPRO;
1853 *func = xg_get_file_name_from_chooser;
1854 return filewin;
1857 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1859 /* Return the selected file for file selector dialog W.
1860 The returned string must be free:d. */
1862 static char *
1863 xg_get_file_name_from_selector (GtkWidget *w)
1865 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1866 return xstrdup (gtk_file_selection_get_filename (filesel));
1869 /* Create a file selection dialog.
1870 F is the current frame.
1871 PROMPT is a prompt to show to the user. May not be NULL.
1872 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1873 If MUSTMATCH_P, the returned file name must be an existing
1874 file. *FUNC is set to a function that can be used to retrieve the
1875 selected file name from the returned widget.
1877 Returns the created widget. */
1879 static GtkWidget *
1880 xg_get_file_with_selection (struct frame *f,
1881 char *prompt,
1882 char *default_filename,
1883 bool mustmatch_p, bool only_dir_p,
1884 xg_get_file_func *func)
1886 GtkWidget *filewin;
1887 GtkFileSelection *filesel;
1889 filewin = gtk_file_selection_new (prompt);
1890 filesel = GTK_FILE_SELECTION (filewin);
1892 if (default_filename)
1893 gtk_file_selection_set_filename (filesel, default_filename);
1895 if (mustmatch_p)
1897 /* The selection_entry part of filesel is not documented. */
1898 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1899 gtk_file_selection_hide_fileop_buttons (filesel);
1902 *func = xg_get_file_name_from_selector;
1904 return filewin;
1906 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1908 /* Read a file name from the user using a file dialog, either the old
1909 file selection dialog, or the new file chooser dialog. Which to use
1910 depends on what the GTK version used has, and what the value of
1911 gtk-use-old-file-dialog.
1912 F is the current frame.
1913 PROMPT is a prompt to show to the user. May not be NULL.
1914 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1915 If MUSTMATCH_P, the returned file name must be an existing
1916 file.
1918 Returns a file name or NULL if no file was selected.
1919 The returned string must be freed by the caller. */
1921 char *
1922 xg_get_file_name (struct frame *f,
1923 char *prompt,
1924 char *default_filename,
1925 bool mustmatch_p,
1926 bool only_dir_p)
1928 GtkWidget *w = 0;
1929 char *fn = 0;
1930 int filesel_done = 0;
1931 xg_get_file_func func;
1933 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1935 if (xg_uses_old_file_dialog ())
1936 w = xg_get_file_with_selection (f, prompt, default_filename,
1937 mustmatch_p, only_dir_p, &func);
1938 else
1939 w = xg_get_file_with_chooser (f, prompt, default_filename,
1940 mustmatch_p, only_dir_p, &func);
1942 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1943 w = xg_get_file_with_chooser (f, prompt, default_filename,
1944 mustmatch_p, only_dir_p, &func);
1945 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1947 gtk_widget_set_name (w, "emacs-filedialog");
1949 filesel_done = xg_dialog_run (f, w);
1950 if (filesel_done == GTK_RESPONSE_OK)
1951 fn = (*func) (w);
1953 gtk_widget_destroy (w);
1954 return fn;
1957 /***********************************************************************
1958 GTK font chooser
1959 ***********************************************************************/
1961 #ifdef HAVE_FREETYPE
1963 #if USE_NEW_GTK_FONT_CHOOSER
1965 #define XG_WEIGHT_TO_SYMBOL(w) \
1966 (w <= PANGO_WEIGHT_THIN ? Qextra_light \
1967 : w <= PANGO_WEIGHT_ULTRALIGHT ? Qlight \
1968 : w <= PANGO_WEIGHT_LIGHT ? Qsemi_light \
1969 : w < PANGO_WEIGHT_MEDIUM ? Qnormal \
1970 : w <= PANGO_WEIGHT_SEMIBOLD ? Qsemi_bold \
1971 : w <= PANGO_WEIGHT_BOLD ? Qbold \
1972 : w <= PANGO_WEIGHT_HEAVY ? Qextra_bold \
1973 : Qultra_bold)
1975 #define XG_STYLE_TO_SYMBOL(s) \
1976 (s == PANGO_STYLE_OBLIQUE ? Qoblique \
1977 : s == PANGO_STYLE_ITALIC ? Qitalic \
1978 : Qnormal)
1980 #endif /* USE_NEW_GTK_FONT_CHOOSER */
1983 static char *x_last_font_name;
1985 /* Pop up a GTK font selector and return the name of the font the user
1986 selects, as a C string. The returned font name follows GTK's own
1987 format:
1989 `FAMILY [VALUE1 VALUE2] SIZE'
1991 This can be parsed using font_parse_fcname in font.c.
1992 DEFAULT_NAME, if non-zero, is the default font name. */
1994 Lisp_Object
1995 xg_get_font (struct frame *f, const char *default_name)
1997 GtkWidget *w;
1998 int done = 0;
1999 Lisp_Object font = Qnil;
2001 w = gtk_font_chooser_dialog_new
2002 ("Pick a font", GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2004 if (default_name)
2006 /* Convert fontconfig names to Gtk names, i.e. remove - before
2007 number */
2008 char *p = strrchr (default_name, '-');
2009 if (p)
2011 char *ep = p+1;
2012 while (c_isdigit (*ep))
2013 ++ep;
2014 if (*ep == '\0') *p = ' ';
2017 else if (x_last_font_name)
2018 default_name = x_last_font_name;
2020 if (default_name)
2021 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), default_name);
2023 gtk_widget_set_name (w, "emacs-fontdialog");
2024 done = xg_dialog_run (f, w);
2025 if (done == GTK_RESPONSE_OK)
2027 #if USE_NEW_GTK_FONT_CHOOSER
2028 /* Use the GTK3 font chooser. */
2029 PangoFontDescription *desc
2030 = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (w));
2032 if (desc)
2034 Lisp_Object args[10];
2035 const char *name = pango_font_description_get_family (desc);
2036 gint size = pango_font_description_get_size (desc);
2037 PangoWeight weight = pango_font_description_get_weight (desc);
2038 PangoStyle style = pango_font_description_get_style (desc);
2040 args[0] = QCname;
2041 args[1] = build_string (name);
2043 args[2] = QCsize;
2044 args[3] = make_float (pango_units_to_double (size));
2046 args[4] = QCweight;
2047 args[5] = XG_WEIGHT_TO_SYMBOL (weight);
2049 args[6] = QCslant;
2050 args[7] = XG_STYLE_TO_SYMBOL (style);
2052 args[8] = QCtype;
2053 args[9] = Qxft;
2055 font = Ffont_spec (8, args);
2057 pango_font_description_free (desc);
2058 dupstring (&x_last_font_name, name);
2061 #else /* Use old font selector, which just returns the font name. */
2063 char *font_name
2064 = gtk_font_selection_dialog_get_font_name (GTK_FONT_CHOOSER (w));
2066 if (font_name)
2068 font = build_string (font_name);
2069 g_free (x_last_font_name);
2070 x_last_font_name = font_name;
2072 #endif /* USE_NEW_GTK_FONT_CHOOSER */
2075 gtk_widget_destroy (w);
2076 return font;
2078 #endif /* HAVE_FREETYPE */
2082 /***********************************************************************
2083 Menu functions.
2084 ***********************************************************************/
2086 /* The name of menu items that can be used for customization. Since GTK
2087 RC files are very crude and primitive, we have to set this on all
2088 menu item names so a user can easily customize menu items. */
2090 #define MENU_ITEM_NAME "emacs-menuitem"
2093 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
2094 during GC. The next member points to the items. */
2095 static xg_list_node xg_menu_cb_list;
2097 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
2098 during GC. The next member points to the items. */
2099 static xg_list_node xg_menu_item_cb_list;
2101 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
2102 F is the frame CL_DATA will be initialized for.
2103 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2105 The menu bar and all sub menus under the menu bar in a frame
2106 share the same structure, hence the reference count.
2108 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
2109 allocated xg_menu_cb_data if CL_DATA is NULL. */
2111 static xg_menu_cb_data *
2112 make_cl_data (xg_menu_cb_data *cl_data, struct frame *f, GCallback highlight_cb)
2114 if (! cl_data)
2116 cl_data = xmalloc (sizeof *cl_data);
2117 cl_data->f = f;
2118 cl_data->menu_bar_vector = f->menu_bar_vector;
2119 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2120 cl_data->highlight_cb = highlight_cb;
2121 cl_data->ref_count = 0;
2123 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2126 cl_data->ref_count++;
2128 return cl_data;
2131 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2132 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2134 When the menu bar is updated, menu items may have been added and/or
2135 removed, so menu_bar_vector and menu_bar_items_used change. We must
2136 then update CL_DATA since it is used to determine which menu
2137 item that is invoked in the menu.
2138 HIGHLIGHT_CB could change, there is no check that the same
2139 function is given when modifying a menu bar as was given when
2140 creating the menu bar. */
2142 static void
2143 update_cl_data (xg_menu_cb_data *cl_data,
2144 struct frame *f,
2145 GCallback highlight_cb)
2147 if (cl_data)
2149 cl_data->f = f;
2150 cl_data->menu_bar_vector = f->menu_bar_vector;
2151 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2152 cl_data->highlight_cb = highlight_cb;
2156 /* Decrease reference count for CL_DATA.
2157 If reference count is zero, free CL_DATA. */
2159 static void
2160 unref_cl_data (xg_menu_cb_data *cl_data)
2162 if (cl_data && cl_data->ref_count > 0)
2164 cl_data->ref_count--;
2165 if (cl_data->ref_count == 0)
2167 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2168 xfree (cl_data);
2173 /* Function that marks all lisp data during GC. */
2175 void
2176 xg_mark_data (void)
2178 xg_list_node *iter;
2179 Lisp_Object rest, frame;
2181 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2182 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2184 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2186 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2188 if (! NILP (cb_data->help))
2189 mark_object (cb_data->help);
2192 FOR_EACH_FRAME (rest, frame)
2194 struct frame *f = XFRAME (frame);
2196 if (FRAME_X_P (f) && FRAME_GTK_OUTER_WIDGET (f))
2198 struct xg_frame_tb_info *tbinfo
2199 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
2200 TB_INFO_KEY);
2201 if (tbinfo)
2203 mark_object (tbinfo->last_tool_bar);
2204 mark_object (tbinfo->style);
2211 /* Callback called when a menu item is destroyed. Used to free data.
2212 W is the widget that is being destroyed (not used).
2213 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2215 static void
2216 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2218 if (client_data)
2220 xg_menu_item_cb_data *data = client_data;
2221 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2222 xfree (data);
2226 /* Callback called when the pointer enters/leaves a menu item.
2227 W is the parent of the menu item.
2228 EVENT is either an enter event or leave event.
2229 CLIENT_DATA is not used.
2231 Returns FALSE to tell GTK to keep processing this event. */
2233 static gboolean
2234 menuitem_highlight_callback (GtkWidget *w,
2235 GdkEventCrossing *event,
2236 gpointer client_data)
2238 GdkEvent ev;
2239 GtkWidget *subwidget;
2240 xg_menu_item_cb_data *data;
2242 ev.crossing = *event;
2243 subwidget = gtk_get_event_widget (&ev);
2244 data = g_object_get_data (G_OBJECT (subwidget), XG_ITEM_DATA);
2245 if (data)
2247 if (! NILP (data->help) && data->cl_data->highlight_cb)
2249 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2250 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2251 (*func) (subwidget, call_data);
2255 return FALSE;
2258 /* Callback called when a menu is destroyed. Used to free data.
2259 W is the widget that is being destroyed (not used).
2260 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2262 static void
2263 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2265 unref_cl_data (client_data);
2268 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2269 must be non-NULL) and can be inserted into a menu item.
2271 Returns the GtkHBox. */
2273 static GtkWidget *
2274 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2276 GtkWidget *wlbl;
2277 GtkWidget *wkey;
2278 GtkWidget *wbox;
2280 wbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2281 gtk_box_set_homogeneous (GTK_BOX (wbox), FALSE);
2282 wlbl = gtk_label_new (utf8_label);
2283 wkey = gtk_label_new (utf8_key);
2285 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2286 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2288 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2289 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2291 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2292 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2293 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2295 return wbox;
2298 /* Make and return a menu item widget with the key to the right.
2299 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2300 UTF8_KEY is the text representing the key binding.
2301 ITEM is the widget_value describing the menu item.
2303 GROUP is an in/out parameter. If the menu item to be created is not
2304 part of any radio menu group, *GROUP contains NULL on entry and exit.
2305 If the menu item to be created is part of a radio menu group, on entry
2306 *GROUP contains the group to use, or NULL if this is the first item
2307 in the group. On exit, *GROUP contains the radio item group.
2309 Unfortunately, keys don't line up as nicely as in Motif,
2310 but the MacOS X version doesn't either, so I guess that is OK. */
2312 static GtkWidget *
2313 make_menu_item (const char *utf8_label,
2314 const char *utf8_key,
2315 widget_value *item,
2316 GSList **group)
2318 GtkWidget *w;
2319 GtkWidget *wtoadd = 0;
2321 /* It has been observed that some menu items have a NULL name field.
2322 This will lead to this function being called with a NULL utf8_label.
2323 GTK crashes on that so we set a blank label. Why there is a NULL
2324 name remains to be investigated. */
2325 if (! utf8_label) utf8_label = " ";
2327 if (utf8_key)
2328 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2330 if (item->button_type == BUTTON_TYPE_TOGGLE)
2332 *group = NULL;
2333 if (utf8_key) w = gtk_check_menu_item_new ();
2334 else w = gtk_check_menu_item_new_with_label (utf8_label);
2335 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2337 else if (item->button_type == BUTTON_TYPE_RADIO)
2339 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2340 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2341 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2342 if (item->selected)
2343 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2345 else
2347 *group = NULL;
2348 if (utf8_key) w = gtk_menu_item_new ();
2349 else w = gtk_menu_item_new_with_label (utf8_label);
2352 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2353 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2355 return w;
2358 #ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
2360 static int xg_detached_menus;
2362 /* Return true if there are detached menus. */
2364 bool
2365 xg_have_tear_offs (struct frame *f)
2367 /* If the frame's menubar height is zero, the menu bar is probably
2368 being redirected outside the window to some kind of global menu;
2369 this situation is the moral equivalent of a tear-off. */
2370 return FRAME_MENUBAR_HEIGHT (f) == 0 || xg_detached_menus > 0;
2373 /* Callback invoked when a detached menu window is removed. Here we
2374 decrease the xg_detached_menus count.
2375 WIDGET is the top level window that is removed (the parent of the menu).
2376 CLIENT_DATA is not used. */
2378 static void
2379 tearoff_remove (GtkWidget *widget, gpointer client_data)
2381 if (xg_detached_menus > 0) --xg_detached_menus;
2384 /* Callback invoked when a menu is detached. It increases the
2385 xg_detached_menus count.
2386 WIDGET is the GtkTearoffMenuItem.
2387 CLIENT_DATA is not used. */
2389 static void
2390 tearoff_activate (GtkWidget *widget, gpointer client_data)
2392 GtkWidget *menu = gtk_widget_get_parent (widget);
2393 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2395 ++xg_detached_menus;
2396 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2397 "destroy",
2398 G_CALLBACK (tearoff_remove), 0);
2401 #else /* ! HAVE_GTK_TEAROFF_MENU_ITEM_NEW */
2402 bool
2403 xg_have_tear_offs (struct frame *f)
2405 return FRAME_MENUBAR_HEIGHT (f) == 0;
2407 #endif /* ! HAVE_GTK_TEAROFF_MENU_ITEM_NEW */
2409 /* Create a menu item widget, and connect the callbacks.
2410 ITEM describes the menu item.
2411 F is the frame the created menu belongs to.
2412 SELECT_CB is the callback to use when a menu item is selected.
2413 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2414 CL_DATA points to the callback data to be used for this menu.
2415 GROUP is an in/out parameter. If the menu item to be created is not
2416 part of any radio menu group, *GROUP contains NULL on entry and exit.
2417 If the menu item to be created is part of a radio menu group, on entry
2418 *GROUP contains the group to use, or NULL if this is the first item
2419 in the group. On exit, *GROUP contains the radio item group.
2421 Returns the created GtkWidget. */
2423 static GtkWidget *
2424 xg_create_one_menuitem (widget_value *item,
2425 struct frame *f,
2426 GCallback select_cb,
2427 GCallback highlight_cb,
2428 xg_menu_cb_data *cl_data,
2429 GSList **group)
2431 char *utf8_label;
2432 char *utf8_key;
2433 GtkWidget *w;
2434 xg_menu_item_cb_data *cb_data;
2436 utf8_label = get_utf8_string (item->name);
2437 utf8_key = get_utf8_string (item->key);
2439 w = make_menu_item (utf8_label, utf8_key, item, group);
2441 if (utf8_label) g_free (utf8_label);
2442 if (utf8_key) g_free (utf8_key);
2444 cb_data = xmalloc (sizeof *cb_data);
2446 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2448 cb_data->select_id = 0;
2449 cb_data->help = item->help;
2450 cb_data->cl_data = cl_data;
2451 cb_data->call_data = item->call_data;
2453 g_signal_connect (G_OBJECT (w),
2454 "destroy",
2455 G_CALLBACK (menuitem_destroy_callback),
2456 cb_data);
2458 /* Put cb_data in widget, so we can get at it when modifying menubar */
2459 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2461 /* final item, not a submenu */
2462 if (item->call_data && ! item->contents)
2464 if (select_cb)
2465 cb_data->select_id
2466 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2469 return w;
2472 /* Create a full menu tree specified by DATA.
2473 F is the frame the created menu belongs to.
2474 SELECT_CB is the callback to use when a menu item is selected.
2475 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2476 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2477 If POP_UP_P, create a popup menu.
2478 If MENU_BAR_P, create a menu bar.
2479 If ADD_TEAROFF_P, add a tearoff menu item. Ignored if MENU_BAR_P or
2480 the Gtk+ version used does not have tearoffs.
2481 TOPMENU is the topmost GtkWidget that others shall be placed under.
2482 It may be NULL, in that case we create the appropriate widget
2483 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2484 CL_DATA is the callback data we shall use for this menu, or NULL
2485 if we haven't set the first callback yet.
2486 NAME is the name to give to the top level menu if this function
2487 creates it. May be NULL to not set any name.
2489 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2490 not NULL.
2492 This function calls itself to create submenus. */
2494 static GtkWidget *
2495 create_menus (widget_value *data,
2496 struct frame *f,
2497 GCallback select_cb,
2498 GCallback deactivate_cb,
2499 GCallback highlight_cb,
2500 bool pop_up_p,
2501 bool menu_bar_p,
2502 bool add_tearoff_p,
2503 GtkWidget *topmenu,
2504 xg_menu_cb_data *cl_data,
2505 const char *name)
2507 widget_value *item;
2508 GtkWidget *wmenu = topmenu;
2509 GSList *group = NULL;
2511 if (! topmenu)
2513 if (! menu_bar_p)
2515 wmenu = gtk_menu_new ();
2516 xg_set_screen (wmenu, f);
2517 /* Connect this to the menu instead of items so we get enter/leave for
2518 disabled items also. TODO: Still does not get enter/leave for
2519 disabled items in detached menus. */
2520 g_signal_connect (G_OBJECT (wmenu),
2521 "enter-notify-event",
2522 G_CALLBACK (menuitem_highlight_callback),
2523 NULL);
2524 g_signal_connect (G_OBJECT (wmenu),
2525 "leave-notify-event",
2526 G_CALLBACK (menuitem_highlight_callback),
2527 NULL);
2529 else
2531 wmenu = gtk_menu_bar_new ();
2532 /* Set width of menu bar to a small value so it doesn't enlarge
2533 a small initial frame size. The width will be set to the
2534 width of the frame later on when it is added to a container.
2535 height -1: Natural height. */
2536 gtk_widget_set_size_request (wmenu, 1, -1);
2539 /* Put cl_data on the top menu for easier access. */
2540 cl_data = make_cl_data (cl_data, f, highlight_cb);
2541 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2542 g_signal_connect (G_OBJECT (wmenu), "destroy",
2543 G_CALLBACK (menu_destroy_callback), cl_data);
2545 if (name)
2546 gtk_widget_set_name (wmenu, name);
2548 if (deactivate_cb)
2549 g_signal_connect (G_OBJECT (wmenu),
2550 "selection-done", deactivate_cb, 0);
2553 #ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
2554 if (! menu_bar_p && add_tearoff_p)
2556 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2557 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2559 g_signal_connect (G_OBJECT (tearoff), "activate",
2560 G_CALLBACK (tearoff_activate), 0);
2562 #endif
2564 for (item = data; item; item = item->next)
2566 GtkWidget *w;
2568 if (pop_up_p && !item->contents && !item->call_data
2569 && !menu_separator_name_p (item->name))
2571 char *utf8_label;
2572 /* A title for a popup. We do the same as GTK does when
2573 creating titles, but it does not look good. */
2574 group = NULL;
2575 utf8_label = get_utf8_string (item->name);
2577 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2578 w = gtk_menu_item_new_with_label (utf8_label);
2579 gtk_widget_set_sensitive (w, FALSE);
2580 if (utf8_label) g_free (utf8_label);
2582 else if (menu_separator_name_p (item->name))
2584 group = NULL;
2585 /* GTK only have one separator type. */
2586 w = gtk_separator_menu_item_new ();
2588 else
2590 w = xg_create_one_menuitem (item,
2592 item->contents ? 0 : select_cb,
2593 highlight_cb,
2594 cl_data,
2595 &group);
2597 /* Create a possibly empty submenu for menu bar items, since some
2598 themes don't highlight items correctly without it. */
2599 if (item->contents || menu_bar_p)
2601 GtkWidget *submenu = create_menus (item->contents,
2603 select_cb,
2604 deactivate_cb,
2605 highlight_cb,
2608 add_tearoff_p,
2610 cl_data,
2612 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2616 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2617 gtk_widget_set_name (w, MENU_ITEM_NAME);
2620 return wmenu;
2623 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2624 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2625 with some text and buttons.
2626 F is the frame the created item belongs to.
2627 NAME is the name to use for the top widget.
2628 VAL is a widget_value structure describing items to be created.
2629 SELECT_CB is the callback to use when a menu item is selected or
2630 a dialog button is pressed.
2631 DEACTIVATE_CB is the callback to use when an item is deactivated.
2632 For a menu, when a sub menu is not shown anymore, for a dialog it is
2633 called when the dialog is popped down.
2634 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2636 Returns the widget created. */
2638 GtkWidget *
2639 xg_create_widget (const char *type, const char *name, struct frame *f,
2640 widget_value *val, GCallback select_cb,
2641 GCallback deactivate_cb, GCallback highlight_cb)
2643 GtkWidget *w = 0;
2644 bool menu_bar_p = strcmp (type, "menubar") == 0;
2645 bool pop_up_p = strcmp (type, "popup") == 0;
2647 if (strcmp (type, "dialog") == 0)
2649 w = create_dialog (val, select_cb, deactivate_cb);
2650 xg_set_screen (w, f);
2651 gtk_window_set_transient_for (GTK_WINDOW (w),
2652 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2653 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2654 gtk_widget_set_name (w, "emacs-dialog");
2655 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2657 else if (menu_bar_p || pop_up_p)
2659 w = create_menus (val->contents,
2661 select_cb,
2662 deactivate_cb,
2663 highlight_cb,
2664 pop_up_p,
2665 menu_bar_p,
2666 menu_bar_p,
2669 name);
2671 /* Set the cursor to an arrow for popup menus when they are mapped.
2672 This is done by default for menu bar menus. */
2673 if (pop_up_p)
2675 /* Must realize so the GdkWindow inside the widget is created. */
2676 gtk_widget_realize (w);
2677 xg_set_cursor (w, FRAME_DISPLAY_INFO (f)->xg_cursor);
2680 else
2682 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2683 type);
2686 return w;
2689 /* Return the label for menu item WITEM. */
2691 static const char *
2692 xg_get_menu_item_label (GtkMenuItem *witem)
2694 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2695 return gtk_label_get_label (wlabel);
2698 /* Return true if the menu item WITEM has the text LABEL. */
2700 static bool
2701 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2703 bool is_same = 0;
2704 char *utf8_label = get_utf8_string (label);
2705 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2707 if (! old_label && ! utf8_label)
2708 is_same = 1;
2709 else if (old_label && utf8_label)
2710 is_same = strcmp (utf8_label, old_label) == 0;
2712 if (utf8_label) g_free (utf8_label);
2714 return is_same;
2717 /* Destroy widgets in LIST. */
2719 static void
2720 xg_destroy_widgets (GList *list)
2722 GList *iter;
2724 for (iter = list; iter; iter = g_list_next (iter))
2726 GtkWidget *w = GTK_WIDGET (iter->data);
2728 /* Destroying the widget will remove it from the container it is in. */
2729 gtk_widget_destroy (w);
2733 /* Update the top level names in MENUBAR (i.e. not submenus).
2734 F is the frame the menu bar belongs to.
2735 *LIST is a list with the current menu bar names (menu item widgets).
2736 ITER is the item within *LIST that shall be updated.
2737 POS is the numerical position, starting at 0, of ITER in *LIST.
2738 VAL describes what the menu bar shall look like after the update.
2739 SELECT_CB is the callback to use when a menu item is selected.
2740 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2741 CL_DATA points to the callback data to be used for this menu bar.
2743 This function calls itself to walk through the menu bar names. */
2745 static void
2746 xg_update_menubar (GtkWidget *menubar,
2747 struct frame *f,
2748 GList **list,
2749 GList *iter,
2750 int pos,
2751 widget_value *val,
2752 GCallback select_cb,
2753 GCallback deactivate_cb,
2754 GCallback highlight_cb,
2755 xg_menu_cb_data *cl_data)
2757 if (! iter && ! val)
2758 return;
2759 else if (iter && ! val)
2761 /* Item(s) have been removed. Remove all remaining items. */
2762 xg_destroy_widgets (iter);
2764 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2765 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2766 gtk_menu_item_new_with_label (""),
2768 /* All updated. */
2769 val = 0;
2770 iter = 0;
2772 else if (! iter && val)
2774 /* Item(s) added. Add all new items in one call. */
2775 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2776 0, 1, 0, menubar, cl_data, 0);
2778 /* All updated. */
2779 val = 0;
2780 iter = 0;
2782 /* Below this neither iter or val is NULL */
2783 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2785 /* This item is still the same, check next item. */
2786 val = val->next;
2787 iter = g_list_next (iter);
2788 ++pos;
2790 else /* This item is changed. */
2792 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2793 GtkMenuItem *witem2 = 0;
2794 bool val_in_menubar = 0;
2795 bool iter_in_new_menubar = 0;
2796 GList *iter2;
2797 widget_value *cur;
2799 /* See if the changed entry (val) is present later in the menu bar */
2800 for (iter2 = iter;
2801 iter2 && ! val_in_menubar;
2802 iter2 = g_list_next (iter2))
2804 witem2 = GTK_MENU_ITEM (iter2->data);
2805 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2808 /* See if the current entry (iter) is present later in the
2809 specification for the new menu bar. */
2810 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2811 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2813 if (val_in_menubar && ! iter_in_new_menubar)
2815 int nr = pos;
2817 /* This corresponds to:
2818 Current: A B C
2819 New: A C
2820 Remove B. */
2822 g_object_ref (G_OBJECT (witem));
2823 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2824 gtk_widget_destroy (GTK_WIDGET (witem));
2826 /* Must get new list since the old changed. */
2827 g_list_free (*list);
2828 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2829 while (nr-- > 0) iter = g_list_next (iter);
2831 else if (! val_in_menubar && ! iter_in_new_menubar)
2833 /* This corresponds to:
2834 Current: A B C
2835 New: A X C
2836 Rename B to X. This might seem to be a strange thing to do,
2837 since if there is a menu under B it will be totally wrong for X.
2838 But consider editing a C file. Then there is a C-mode menu
2839 (corresponds to B above).
2840 If then doing C-x C-f the minibuf menu (X above) replaces the
2841 C-mode menu. When returning from the minibuffer, we get
2842 back the C-mode menu. Thus we do:
2843 Rename B to X (C-mode to minibuf menu)
2844 Rename X to B (minibuf to C-mode menu).
2845 If the X menu hasn't been invoked, the menu under B
2846 is up to date when leaving the minibuffer. */
2847 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2848 char *utf8_label = get_utf8_string (val->name);
2849 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2851 /* GTK menu items don't notice when their labels have been
2852 changed from underneath them, so we have to explicitly
2853 use g_object_notify to tell listeners (e.g., a GMenuModel
2854 bridge that might be loaded) that the item's label has
2855 changed. */
2856 gtk_label_set_text (wlabel, utf8_label);
2857 g_object_notify (G_OBJECT (witem), "label");
2859 #ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
2860 /* If this item has a submenu that has been detached, change
2861 the title in the WM decorations also. */
2862 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2863 /* Set the title of the detached window. */
2864 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2865 #endif
2867 if (utf8_label) g_free (utf8_label);
2868 iter = g_list_next (iter);
2869 val = val->next;
2870 ++pos;
2872 else if (! val_in_menubar && iter_in_new_menubar)
2874 /* This corresponds to:
2875 Current: A B C
2876 New: A X B C
2877 Insert X. */
2879 int nr = pos;
2880 GSList *group = 0;
2881 GtkWidget *w = xg_create_one_menuitem (val,
2883 select_cb,
2884 highlight_cb,
2885 cl_data,
2886 &group);
2888 /* Create a possibly empty submenu for menu bar items, since some
2889 themes don't highlight items correctly without it. */
2890 GtkWidget *submenu = create_menus (NULL, f,
2891 select_cb, deactivate_cb,
2892 highlight_cb,
2893 0, 0, 0, 0, cl_data, 0);
2895 gtk_widget_set_name (w, MENU_ITEM_NAME);
2896 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2897 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2899 g_list_free (*list);
2900 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2901 while (nr-- > 0) iter = g_list_next (iter);
2902 iter = g_list_next (iter);
2903 val = val->next;
2904 ++pos;
2906 else /* if (val_in_menubar && iter_in_new_menubar) */
2908 int nr = pos;
2909 /* This corresponds to:
2910 Current: A B C
2911 New: A C B
2912 Move C before B */
2914 g_object_ref (G_OBJECT (witem2));
2915 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2916 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2917 GTK_WIDGET (witem2), pos);
2918 g_object_unref (G_OBJECT (witem2));
2920 g_list_free (*list);
2921 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2922 while (nr-- > 0) iter = g_list_next (iter);
2923 if (iter) iter = g_list_next (iter);
2924 val = val->next;
2925 ++pos;
2929 /* Update the rest of the menu bar. */
2930 xg_update_menubar (menubar, f, list, iter, pos, val,
2931 select_cb, deactivate_cb, highlight_cb, cl_data);
2934 /* Update the menu item W so it corresponds to VAL.
2935 SELECT_CB is the callback to use when a menu item is selected.
2936 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2937 CL_DATA is the data to set in the widget for menu invocation. */
2939 static void
2940 xg_update_menu_item (widget_value *val,
2941 GtkWidget *w,
2942 GCallback select_cb,
2943 GCallback highlight_cb,
2944 xg_menu_cb_data *cl_data)
2946 GtkWidget *wchild;
2947 GtkLabel *wlbl = 0;
2948 GtkLabel *wkey = 0;
2949 char *utf8_label;
2950 char *utf8_key;
2951 const char *old_label = 0;
2952 const char *old_key = 0;
2953 xg_menu_item_cb_data *cb_data;
2954 bool label_changed = false;
2956 wchild = XG_BIN_CHILD (w);
2957 utf8_label = get_utf8_string (val->name);
2958 utf8_key = get_utf8_string (val->key);
2960 /* See if W is a menu item with a key. See make_menu_item above. */
2961 if (GTK_IS_BOX (wchild))
2963 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2965 wlbl = GTK_LABEL (list->data);
2966 wkey = GTK_LABEL (list->next->data);
2967 g_list_free (list);
2969 if (! utf8_key)
2971 /* Remove the key and keep just the label. */
2972 g_object_ref (G_OBJECT (wlbl));
2973 gtk_container_remove (GTK_CONTAINER (w), wchild);
2974 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2975 g_object_unref (G_OBJECT (wlbl));
2976 wkey = 0;
2980 else /* Just a label. */
2982 wlbl = GTK_LABEL (wchild);
2984 /* Check if there is now a key. */
2985 if (utf8_key)
2987 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2988 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2990 wlbl = GTK_LABEL (list->data);
2991 wkey = GTK_LABEL (list->next->data);
2992 g_list_free (list);
2994 gtk_container_remove (GTK_CONTAINER (w), wchild);
2995 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2999 if (wkey) old_key = gtk_label_get_label (wkey);
3000 if (wlbl) old_label = gtk_label_get_label (wlbl);
3002 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
3004 label_changed = true;
3005 gtk_label_set_text (wkey, utf8_key);
3008 if (! old_label || strcmp (utf8_label, old_label) != 0)
3010 label_changed = true;
3011 gtk_label_set_text (wlbl, utf8_label);
3014 if (utf8_key) g_free (utf8_key);
3015 if (utf8_label) g_free (utf8_label);
3017 if (! val->enabled && gtk_widget_get_sensitive (w))
3018 gtk_widget_set_sensitive (w, FALSE);
3019 else if (val->enabled && ! gtk_widget_get_sensitive (w))
3020 gtk_widget_set_sensitive (w, TRUE);
3022 cb_data = g_object_get_data (G_OBJECT (w), XG_ITEM_DATA);
3023 if (cb_data)
3025 cb_data->call_data = val->call_data;
3026 cb_data->help = val->help;
3027 cb_data->cl_data = cl_data;
3029 /* We assume the callback functions don't change. */
3030 if (val->call_data && ! val->contents)
3032 /* This item shall have a select callback. */
3033 if (! cb_data->select_id)
3034 cb_data->select_id
3035 = g_signal_connect (G_OBJECT (w), "activate",
3036 select_cb, cb_data);
3038 else if (cb_data->select_id)
3040 g_signal_handler_disconnect (w, cb_data->select_id);
3041 cb_data->select_id = 0;
3045 if (label_changed) /* See comment in xg_update_menubar. */
3046 g_object_notify (G_OBJECT (w), "label");
3049 /* Update the toggle menu item W so it corresponds to VAL. */
3051 static void
3052 xg_update_toggle_item (widget_value *val, GtkWidget *w)
3054 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3057 /* Update the radio menu item W so it corresponds to VAL. */
3059 static void
3060 xg_update_radio_item (widget_value *val, GtkWidget *w)
3062 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
3065 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
3066 SUBMENU may be NULL, in that case a new menu is created.
3067 F is the frame the menu bar belongs to.
3068 VAL describes the contents of the menu bar.
3069 SELECT_CB is the callback to use when a menu item is selected.
3070 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3071 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
3072 CL_DATA is the call back data to use for any newly created items.
3074 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
3075 was NULL. */
3077 static GtkWidget *
3078 xg_update_submenu (GtkWidget *submenu,
3079 struct frame *f,
3080 widget_value *val,
3081 GCallback select_cb,
3082 GCallback deactivate_cb,
3083 GCallback highlight_cb,
3084 xg_menu_cb_data *cl_data)
3086 GtkWidget *newsub = submenu;
3087 GList *list = 0;
3088 GList *iter;
3089 widget_value *cur;
3090 bool has_tearoff_p = 0;
3091 GList *first_radio = 0;
3093 if (submenu)
3094 list = gtk_container_get_children (GTK_CONTAINER (submenu));
3096 for (cur = val, iter = list;
3097 cur && iter;
3098 iter = g_list_next (iter), cur = cur->next)
3100 GtkWidget *w = GTK_WIDGET (iter->data);
3102 #ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
3103 /* Skip tearoff items, they have no counterpart in val. */
3104 if (GTK_IS_TEAROFF_MENU_ITEM (w))
3106 has_tearoff_p = 1;
3107 iter = g_list_next (iter);
3108 if (iter) w = GTK_WIDGET (iter->data);
3109 else break;
3111 #endif
3113 /* Remember first radio button in a group. If we get a mismatch in
3114 a radio group we must rebuild the whole group so that the connections
3115 in GTK becomes correct. */
3116 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
3117 first_radio = iter;
3118 else if (cur->button_type != BUTTON_TYPE_RADIO
3119 && ! GTK_IS_RADIO_MENU_ITEM (w))
3120 first_radio = 0;
3122 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
3124 if (! menu_separator_name_p (cur->name))
3125 break;
3127 else if (GTK_IS_CHECK_MENU_ITEM (w))
3129 if (cur->button_type != BUTTON_TYPE_TOGGLE)
3130 break;
3131 xg_update_toggle_item (cur, w);
3132 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3134 else if (GTK_IS_RADIO_MENU_ITEM (w))
3136 if (cur->button_type != BUTTON_TYPE_RADIO)
3137 break;
3138 xg_update_radio_item (cur, w);
3139 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3141 else if (GTK_IS_MENU_ITEM (w))
3143 GtkMenuItem *witem = GTK_MENU_ITEM (w);
3144 GtkWidget *sub;
3146 if (cur->button_type != BUTTON_TYPE_NONE ||
3147 menu_separator_name_p (cur->name))
3148 break;
3150 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
3152 sub = gtk_menu_item_get_submenu (witem);
3153 if (sub && ! cur->contents)
3155 /* Not a submenu anymore. */
3156 g_object_ref (G_OBJECT (sub));
3157 remove_submenu (witem);
3158 gtk_widget_destroy (sub);
3160 else if (cur->contents)
3162 GtkWidget *nsub;
3164 nsub = xg_update_submenu (sub, f, cur->contents,
3165 select_cb, deactivate_cb,
3166 highlight_cb, cl_data);
3168 /* If this item just became a submenu, we must set it. */
3169 if (nsub != sub)
3170 gtk_menu_item_set_submenu (witem, nsub);
3173 else
3175 /* Structural difference. Remove everything from here and down
3176 in SUBMENU. */
3177 break;
3181 /* Remove widgets from first structural change. */
3182 if (iter)
3184 /* If we are adding new menu items below, we must remove from
3185 first radio button so that radio groups become correct. */
3186 if (cur && first_radio) xg_destroy_widgets (first_radio);
3187 else xg_destroy_widgets (iter);
3190 if (cur)
3192 /* More items added. Create them. */
3193 newsub = create_menus (cur,
3195 select_cb,
3196 deactivate_cb,
3197 highlight_cb,
3200 ! has_tearoff_p,
3201 submenu,
3202 cl_data,
3206 if (list) g_list_free (list);
3208 return newsub;
3211 /* Update the MENUBAR.
3212 F is the frame the menu bar belongs to.
3213 VAL describes the contents of the menu bar.
3214 If DEEP_P, rebuild all but the top level menu names in
3215 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3216 SELECT_CB is the callback to use when a menu item is selected.
3217 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3218 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3220 void
3221 xg_modify_menubar_widgets (GtkWidget *menubar, struct frame *f,
3222 widget_value *val, bool deep_p,
3223 GCallback select_cb, GCallback deactivate_cb,
3224 GCallback highlight_cb)
3226 xg_menu_cb_data *cl_data;
3227 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3229 if (! list) return;
3231 cl_data = g_object_get_data (G_OBJECT (menubar), XG_FRAME_DATA);
3233 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3234 select_cb, deactivate_cb, highlight_cb, cl_data);
3236 if (deep_p)
3238 widget_value *cur;
3240 /* Update all sub menus.
3241 We must keep the submenus (GTK menu item widgets) since the
3242 X Window in the XEvent that activates the menu are those widgets. */
3244 /* Update cl_data, menu_item things in F may have changed. */
3245 update_cl_data (cl_data, f, highlight_cb);
3247 for (cur = val->contents; cur; cur = cur->next)
3249 GList *iter;
3250 GtkWidget *sub = 0;
3251 GtkWidget *newsub;
3252 GtkMenuItem *witem = 0;
3254 /* Find sub menu that corresponds to val and update it. */
3255 for (iter = list ; iter; iter = g_list_next (iter))
3257 witem = GTK_MENU_ITEM (iter->data);
3258 if (xg_item_label_same_p (witem, cur->name))
3260 sub = gtk_menu_item_get_submenu (witem);
3261 break;
3265 newsub = xg_update_submenu (sub,
3267 cur->contents,
3268 select_cb,
3269 deactivate_cb,
3270 highlight_cb,
3271 cl_data);
3272 /* sub may still be NULL. If we just updated non deep and added
3273 a new menu bar item, it has no sub menu yet. So we set the
3274 newly created sub menu under witem. */
3275 if (newsub != sub && witem != 0)
3277 xg_set_screen (newsub, f);
3278 gtk_menu_item_set_submenu (witem, newsub);
3283 g_list_free (list);
3284 gtk_widget_show_all (menubar);
3287 /* Callback called when the menu bar W is mapped.
3288 Used to find the height of the menu bar if we didn't get it
3289 after showing the widget. */
3291 static void
3292 menubar_map_cb (GtkWidget *w, gpointer user_data)
3294 GtkRequisition req;
3295 struct frame *f = user_data;
3296 gtk_widget_get_preferred_size (w, NULL, &req);
3297 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3299 FRAME_MENUBAR_HEIGHT (f) = req.height;
3300 xg_height_or_width_changed (f);
3304 /* Recompute all the widgets of frame F, when the menu bar has been
3305 changed. */
3307 void
3308 xg_update_frame_menubar (struct frame *f)
3310 struct x_output *x = f->output_data.x;
3311 GtkRequisition req;
3313 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3314 return;
3316 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3317 return; /* Already done this, happens for frames created invisible. */
3319 block_input ();
3321 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3322 FALSE, FALSE, 0);
3323 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3325 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3326 gtk_widget_show_all (x->menubar_widget);
3327 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3329 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3331 FRAME_MENUBAR_HEIGHT (f) = req.height;
3332 xg_height_or_width_changed (f);
3334 unblock_input ();
3337 /* Get rid of the menu bar of frame F, and free its storage.
3338 This is used when deleting a frame, and when turning off the menu bar. */
3340 void
3341 free_frame_menubar (struct frame *f)
3343 struct x_output *x = f->output_data.x;
3345 if (x->menubar_widget)
3347 block_input ();
3349 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3350 /* The menubar and its children shall be deleted when removed from
3351 the container. */
3352 x->menubar_widget = 0;
3353 FRAME_MENUBAR_HEIGHT (f) = 0;
3354 xg_height_or_width_changed (f);
3355 unblock_input ();
3359 bool
3360 xg_event_is_for_menubar (struct frame *f, const XEvent *event)
3362 struct x_output *x = f->output_data.x;
3363 GList *iter;
3364 GdkRectangle rec;
3365 GList *list;
3366 GdkDisplay *gdpy;
3367 GdkWindow *gw;
3368 GdkEvent gevent;
3369 GtkWidget *gwdesc;
3371 if (! x->menubar_widget) return 0;
3373 if (! (event->xbutton.x >= 0
3374 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3375 && event->xbutton.y >= 0
3376 && event->xbutton.y < f->output_data.x->menubar_height
3377 && event->xbutton.same_screen))
3378 return 0;
3380 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3381 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3382 if (! gw) return 0;
3383 gevent.any.window = gw;
3384 gevent.any.type = GDK_NOTHING;
3385 gwdesc = gtk_get_event_widget (&gevent);
3386 if (! gwdesc) return 0;
3387 if (! GTK_IS_MENU_BAR (gwdesc)
3388 && ! GTK_IS_MENU_ITEM (gwdesc)
3389 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3390 return 0;
3392 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3393 if (! list) return 0;
3394 rec.x = event->xbutton.x;
3395 rec.y = event->xbutton.y;
3396 rec.width = 1;
3397 rec.height = 1;
3399 for (iter = list ; iter; iter = g_list_next (iter))
3401 GtkWidget *w = GTK_WIDGET (iter->data);
3402 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3403 break;
3405 g_list_free (list);
3406 return iter != 0;
3411 /***********************************************************************
3412 Scroll bar functions
3413 ***********************************************************************/
3416 /* Setting scroll bar values invokes the callback. Use this variable
3417 to indicate that callback should do nothing. */
3419 bool xg_ignore_gtk_scrollbar;
3421 /* The width of the scroll bar for the current theme. */
3423 static int scroll_bar_width_for_theme;
3425 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3426 may be larger than 32 bits. Keep a mapping from integer index to widget
3427 pointers to get around the 32 bit limitation. */
3429 static struct
3431 GtkWidget **widgets;
3432 ptrdiff_t max_size;
3433 ptrdiff_t used;
3434 } id_to_widget;
3436 /* Grow this much every time we need to allocate more */
3438 #define ID_TO_WIDGET_INCR 32
3440 /* Store the widget pointer W in id_to_widget and return the integer index. */
3442 static ptrdiff_t
3443 xg_store_widget_in_map (GtkWidget *w)
3445 ptrdiff_t i;
3447 if (id_to_widget.max_size == id_to_widget.used)
3449 ptrdiff_t new_size;
3450 if (TYPE_MAXIMUM (Window) - ID_TO_WIDGET_INCR < id_to_widget.max_size)
3451 memory_full (SIZE_MAX);
3453 new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3454 id_to_widget.widgets = xnrealloc (id_to_widget.widgets,
3455 new_size, sizeof (GtkWidget *));
3457 for (i = id_to_widget.max_size; i < new_size; ++i)
3458 id_to_widget.widgets[i] = 0;
3459 id_to_widget.max_size = new_size;
3462 /* Just loop over the array and find a free place. After all,
3463 how many scroll bars are we creating? Should be a small number.
3464 The check above guarantees we will find a free place. */
3465 for (i = 0; i < id_to_widget.max_size; ++i)
3467 if (! id_to_widget.widgets[i])
3469 id_to_widget.widgets[i] = w;
3470 ++id_to_widget.used;
3472 return i;
3476 /* Should never end up here */
3477 emacs_abort ();
3480 /* Remove pointer at IDX from id_to_widget.
3481 Called when scroll bar is destroyed. */
3483 static void
3484 xg_remove_widget_from_map (ptrdiff_t idx)
3486 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3488 id_to_widget.widgets[idx] = 0;
3489 --id_to_widget.used;
3493 /* Get the widget pointer at IDX from id_to_widget. */
3495 static GtkWidget *
3496 xg_get_widget_from_map (ptrdiff_t idx)
3498 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3499 return id_to_widget.widgets[idx];
3501 return 0;
3504 static void
3505 update_theme_scrollbar_width (void)
3507 #ifdef HAVE_GTK3
3508 GtkAdjustment *vadj;
3509 #else
3510 GtkObject *vadj;
3511 #endif
3512 GtkWidget *wscroll;
3513 int w = 0, b = 0;
3515 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, 0.1, 0.1, 0.1);
3516 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3517 g_object_ref_sink (G_OBJECT (wscroll));
3518 gtk_widget_style_get (wscroll, "slider-width", &w, "trough-border", &b, NULL);
3519 gtk_widget_destroy (wscroll);
3520 g_object_unref (G_OBJECT (wscroll));
3521 w += 2*b;
3522 if (w < 16) w = 16;
3523 scroll_bar_width_for_theme = w;
3527 xg_get_default_scrollbar_width (void)
3529 return scroll_bar_width_for_theme;
3532 /* Return the scrollbar id for X Window WID on display DPY.
3533 Return -1 if WID not in id_to_widget. */
3535 ptrdiff_t
3536 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3538 ptrdiff_t idx;
3539 GtkWidget *w;
3541 w = xg_win_to_widget (dpy, wid);
3543 if (w)
3545 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3546 if (id_to_widget.widgets[idx] == w)
3547 return idx;
3550 return -1;
3553 /* Callback invoked when scroll bar WIDGET is destroyed.
3554 DATA is the index into id_to_widget for WIDGET.
3555 We free pointer to last scroll bar values here and remove the index. */
3557 static void
3558 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3560 intptr_t id = (intptr_t) data;
3561 xg_remove_widget_from_map (id);
3564 /* Create a scroll bar widget for frame F. Store the scroll bar
3565 in BAR.
3566 SCROLL_CALLBACK is the callback to invoke when the value of the
3567 bar changes.
3568 END_CALLBACK is the callback to invoke when scrolling ends.
3569 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3570 to set resources for the widget. */
3572 void
3573 xg_create_scroll_bar (struct frame *f,
3574 struct scroll_bar *bar,
3575 GCallback scroll_callback,
3576 GCallback end_callback,
3577 const char *scroll_bar_name)
3579 GtkWidget *wscroll;
3580 GtkWidget *webox;
3581 intptr_t scroll_id;
3582 #ifdef HAVE_GTK3
3583 GtkAdjustment *vadj;
3584 #else
3585 GtkObject *vadj;
3586 #endif
3588 /* Page, step increment values are not so important here, they
3589 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3590 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3591 0.1, 0.1, 0.1);
3593 wscroll = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (vadj));
3594 webox = gtk_event_box_new ();
3595 gtk_widget_set_name (wscroll, scroll_bar_name);
3596 #ifndef HAVE_GTK3
3597 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3598 #endif
3599 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3601 scroll_id = xg_store_widget_in_map (wscroll);
3603 g_signal_connect (G_OBJECT (wscroll),
3604 "destroy",
3605 G_CALLBACK (xg_gtk_scroll_destroy),
3606 (gpointer) scroll_id);
3607 g_signal_connect (G_OBJECT (wscroll),
3608 "change-value",
3609 scroll_callback,
3610 (gpointer) bar);
3611 g_signal_connect (G_OBJECT (wscroll),
3612 "button-release-event",
3613 end_callback,
3614 (gpointer) bar);
3616 /* The scroll bar widget does not draw on a window of its own. Instead
3617 it draws on the parent window, in this case the edit widget. So
3618 whenever the edit widget is cleared, the scroll bar needs to redraw
3619 also, which causes flicker. Put an event box between the edit widget
3620 and the scroll bar, so the scroll bar instead draws itself on the
3621 event box window. */
3622 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3623 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3626 /* Set the cursor to an arrow. */
3627 xg_set_cursor (webox, FRAME_DISPLAY_INFO (f)->xg_cursor);
3629 bar->x_window = scroll_id;
3632 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3634 void
3635 xg_remove_scroll_bar (struct frame *f, ptrdiff_t scrollbar_id)
3637 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3638 if (w)
3640 GtkWidget *wparent = gtk_widget_get_parent (w);
3641 gtk_widget_destroy (w);
3642 gtk_widget_destroy (wparent);
3643 SET_FRAME_GARBAGED (f);
3647 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3648 in frame F.
3649 TOP/LEFT are the new pixel positions where the bar shall appear.
3650 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3652 void
3653 xg_update_scrollbar_pos (struct frame *f,
3654 ptrdiff_t scrollbar_id,
3655 int top,
3656 int left,
3657 int width,
3658 int height)
3661 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3663 if (wscroll)
3665 GtkWidget *wfixed = f->output_data.x->edit_widget;
3666 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3667 gint msl;
3669 /* Clear out old position. */
3670 int oldx = -1, oldy = -1, oldw, oldh;
3671 if (gtk_widget_get_parent (wparent) == wfixed)
3673 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3674 "x", &oldx, "y", &oldy, NULL);
3675 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3678 /* Move and resize to new values. */
3679 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3680 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3681 if (msl > height)
3683 /* No room. Hide scroll bar as some themes output a warning if
3684 the height is less than the min size. */
3685 gtk_widget_hide (wparent);
3686 gtk_widget_hide (wscroll);
3688 else
3690 gtk_widget_show_all (wparent);
3691 gtk_widget_set_size_request (wscroll, width, height);
3693 gtk_widget_queue_draw (wfixed);
3694 gdk_window_process_all_updates ();
3695 if (oldx != -1 && oldw > 0 && oldh > 0)
3696 /* Clear under old scroll bar position. This must be done after
3697 the gtk_widget_queue_draw and gdk_window_process_all_updates
3698 above. */
3699 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3700 oldx, oldy, oldw, oldh);
3702 /* GTK does not redraw until the main loop is entered again, but
3703 if there are no X events pending we will not enter it. So we sync
3704 here to get some events. */
3706 x_sync (f);
3707 SET_FRAME_GARBAGED (f);
3708 cancel_mouse_face (f);
3712 /* Get the current value of the range, truncated to an integer. */
3714 static int
3715 int_gtk_range_get_value (GtkRange *range)
3717 return gtk_range_get_value (range);
3721 /* Set the thumb size and position of scroll bar BAR. We are currently
3722 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3724 void
3725 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3726 int portion,
3727 int position,
3728 int whole)
3730 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3732 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3734 if (wscroll && bar->dragging == -1)
3736 GtkAdjustment *adj;
3737 gdouble shown;
3738 gdouble top;
3739 int size, value;
3740 int old_size;
3741 int new_step;
3742 bool changed = 0;
3744 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3746 if (scroll_bar_adjust_thumb_portion_p)
3748 /* We do the same as for MOTIF in xterm.c, use 30 chars per
3749 line rather than the real portion value. This makes the
3750 thumb less likely to resize and that looks better. */
3751 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3753 /* When the thumb is at the bottom, position == whole.
3754 So we need to increase `whole' to make space for the thumb. */
3755 whole += portion;
3758 if (whole <= 0)
3759 top = 0, shown = 1;
3760 else
3762 top = (gdouble) position / whole;
3763 shown = (gdouble) portion / whole;
3766 size = clip_to_bounds (1, shown * XG_SB_RANGE, XG_SB_RANGE);
3767 value = clip_to_bounds (XG_SB_MIN, top * XG_SB_RANGE, XG_SB_MAX - size);
3769 /* Assume all lines are of equal size. */
3770 new_step = size / max (1, FRAME_LINES (f));
3772 old_size = gtk_adjustment_get_page_size (adj);
3773 if (old_size != size)
3775 int old_step = gtk_adjustment_get_step_increment (adj);
3776 if (old_step != new_step)
3778 gtk_adjustment_set_page_size (adj, size);
3779 gtk_adjustment_set_step_increment (adj, new_step);
3780 /* Assume a page increment is about 95% of the page size */
3781 gtk_adjustment_set_page_increment (adj, size - size / 20);
3782 changed = 1;
3786 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3788 block_input ();
3790 /* gtk_range_set_value invokes the callback. Set
3791 ignore_gtk_scrollbar to make the callback do nothing */
3792 xg_ignore_gtk_scrollbar = 1;
3794 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3795 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3796 else if (changed)
3797 gtk_adjustment_changed (adj);
3799 xg_ignore_gtk_scrollbar = 0;
3801 unblock_input ();
3806 /* Return true if EVENT is for a scroll bar in frame F.
3807 When the same X window is used for several Gtk+ widgets, we cannot
3808 say for sure based on the X window alone if an event is for the
3809 frame. This function does additional checks. */
3811 bool
3812 xg_event_is_for_scrollbar (struct frame *f, const XEvent *event)
3814 bool retval = 0;
3816 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3818 /* Check if press occurred outside the edit widget. */
3819 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3820 GdkWindow *gwin;
3821 #ifdef HAVE_GTK3
3822 GdkDevice *gdev = gdk_device_manager_get_client_pointer
3823 (gdk_display_get_device_manager (gdpy));
3824 gwin = gdk_device_get_window_at_position (gdev, NULL, NULL);
3825 #else
3826 gwin = gdk_display_get_window_at_pointer (gdpy, NULL, NULL);
3827 #endif
3828 retval = gwin != gtk_widget_get_window (f->output_data.x->edit_widget);
3830 else if (f
3831 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3832 || event->type == MotionNotify))
3834 /* If we are releasing or moving the scroll bar, it has the grab. */
3835 GtkWidget *w = gtk_grab_get_current ();
3836 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3839 return retval;
3844 /***********************************************************************
3845 Tool bar functions
3846 ***********************************************************************/
3847 /* The key for the data we put in the GtkImage widgets. The data is
3848 the image used by Emacs. We use this to see if we need to update
3849 the GtkImage with a new image. */
3850 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3852 /* The key for storing the latest modifiers so the activate callback can
3853 get them. */
3854 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3856 /* The key for storing the button widget in its proxy menu item. */
3857 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3859 /* The key for the data we put in the GtkImage widgets. The data is
3860 the stock name used by Emacs. We use this to see if we need to update
3861 the GtkImage with a new image. */
3862 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3864 /* As above, but this is used for named theme widgets, as opposed to
3865 stock items. */
3866 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3868 /* Callback function invoked when a tool bar item is pressed.
3869 W is the button widget in the tool bar that got pressed,
3870 CLIENT_DATA is an integer that is the index of the button in the
3871 tool bar. 0 is the first button. */
3873 static gboolean
3874 xg_tool_bar_button_cb (GtkWidget *widget,
3875 GdkEventButton *event,
3876 gpointer user_data)
3878 intptr_t state = event->state;
3879 gpointer ptr = (gpointer) state;
3880 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3881 return FALSE;
3885 /* Callback function invoked when a tool bar item is pressed.
3886 W is the button widget in the tool bar that got pressed,
3887 CLIENT_DATA is an integer that is the index of the button in the
3888 tool bar. 0 is the first button. */
3890 static void
3891 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3893 intptr_t idx = (intptr_t) client_data;
3894 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
3895 intptr_t mod = (intptr_t) gmod;
3897 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3898 Lisp_Object key, frame;
3899 struct input_event event;
3900 EVENT_INIT (event);
3902 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3903 return;
3905 idx *= TOOL_BAR_ITEM_NSLOTS;
3907 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3908 XSETFRAME (frame, f);
3910 /* We generate two events here. The first one is to set the prefix
3911 to `(tool_bar)', see keyboard.c. */
3912 event.kind = TOOL_BAR_EVENT;
3913 event.frame_or_window = frame;
3914 event.arg = frame;
3915 kbd_buffer_store_event (&event);
3917 event.kind = TOOL_BAR_EVENT;
3918 event.frame_or_window = frame;
3919 event.arg = key;
3920 /* Convert between the modifier bits GDK uses and the modifier bits
3921 Emacs uses. This assumes GDK and X masks are the same, which they are when
3922 this is written. */
3923 event.modifiers = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), mod);
3924 kbd_buffer_store_event (&event);
3926 /* Return focus to the frame after we have clicked on a detached
3927 tool bar button. */
3928 x_focus_frame (f);
3931 /* Callback function invoked when a tool bar item is pressed in a detached
3932 tool bar or the overflow drop down menu.
3933 We just call xg_tool_bar_callback.
3934 W is the menu item widget that got pressed,
3935 CLIENT_DATA is an integer that is the index of the button in the
3936 tool bar. 0 is the first button. */
3938 static void
3939 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3941 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3942 XG_TOOL_BAR_PROXY_BUTTON));
3943 xg_tool_bar_callback (wbutton, client_data);
3947 static gboolean
3948 xg_tool_bar_help_callback (GtkWidget *w,
3949 GdkEventCrossing *event,
3950 gpointer client_data);
3952 /* This callback is called when a help is to be shown for an item in
3953 the detached tool bar when the detached tool bar it is not expanded. */
3955 static gboolean
3956 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3957 GdkEventCrossing *event,
3958 gpointer client_data)
3960 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3961 XG_TOOL_BAR_PROXY_BUTTON));
3963 return xg_tool_bar_help_callback (wbutton, event, client_data);
3966 static GtkWidget *
3967 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3969 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3970 GtkWidget *c1 = clist->data;
3971 GtkWidget *c2 = clist->next ? clist->next->data : NULL;
3973 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3974 g_list_free (clist);
3975 return GTK_IS_LABEL (c1) ? c1 : c2;
3979 /* This callback is called when a tool item should create a proxy item,
3980 such as for the overflow menu. Also called when the tool bar is detached.
3981 If we don't create a proxy menu item, the detached tool bar will be
3982 blank. */
3984 static gboolean
3985 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3987 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3988 GtkWidget *vb = XG_BIN_CHILD (wbutton);
3989 GtkWidget *c1;
3990 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3991 GtkImage *wimage = GTK_IMAGE (c1);
3992 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3993 (wlbl ? gtk_label_get_text (wlbl) : "");
3994 GtkWidget *wmenuimage;
3997 if (gtk_button_get_use_stock (wbutton))
3998 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3999 GTK_ICON_SIZE_MENU);
4000 else
4002 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
4003 GtkImageType store_type = gtk_image_get_storage_type (wimage);
4005 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
4007 if (store_type == GTK_IMAGE_STOCK)
4009 gchar *stock_id;
4010 gtk_image_get_stock (wimage, &stock_id, NULL);
4011 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
4013 else if (store_type == GTK_IMAGE_ICON_SET)
4015 GtkIconSet *icon_set;
4016 gtk_image_get_icon_set (wimage, &icon_set, NULL);
4017 wmenuimage = gtk_image_new_from_icon_set (icon_set,
4018 GTK_ICON_SIZE_MENU);
4020 else if (store_type == GTK_IMAGE_PIXBUF)
4022 gint width, height;
4024 if (settings &&
4025 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
4026 &width, &height))
4028 GdkPixbuf *src_pixbuf, *dest_pixbuf;
4030 src_pixbuf = gtk_image_get_pixbuf (wimage);
4031 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
4032 GDK_INTERP_BILINEAR);
4034 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
4036 else
4038 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
4039 emacs_abort ();
4042 else if (store_type == GTK_IMAGE_ICON_NAME)
4044 const gchar *icon_name;
4045 GtkIconSize icon_size;
4047 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
4048 wmenuimage = gtk_image_new_from_icon_name (icon_name,
4049 GTK_ICON_SIZE_MENU);
4051 else
4053 fprintf (stderr, "internal error: store_type is %d\n", store_type);
4054 emacs_abort ();
4057 if (wmenuimage)
4058 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
4060 g_signal_connect (G_OBJECT (wmenuitem),
4061 "activate",
4062 G_CALLBACK (xg_tool_bar_proxy_callback),
4063 user_data);
4066 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
4067 (gpointer) wbutton);
4068 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
4069 gtk_widget_set_sensitive (wmenuitem,
4070 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
4072 /* Use enter/leave notify to show help. We use the events
4073 rather than the GtkButton specific signals "enter" and
4074 "leave", so we can have only one callback. The event
4075 will tell us what kind of event it is. */
4076 g_signal_connect (G_OBJECT (wmenuitem),
4077 "enter-notify-event",
4078 G_CALLBACK (xg_tool_bar_proxy_help_callback),
4079 user_data);
4080 g_signal_connect (G_OBJECT (wmenuitem),
4081 "leave-notify-event",
4082 G_CALLBACK (xg_tool_bar_proxy_help_callback),
4083 user_data);
4085 return TRUE;
4088 /* This callback is called when a tool bar is detached. We must set
4089 the height of the tool bar to zero when this happens so frame sizes
4090 are correctly calculated.
4091 WBOX is the handle box widget that enables detach/attach of the tool bar.
4092 W is the tool bar widget.
4093 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4095 static void
4096 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
4097 GtkWidget *w,
4098 gpointer client_data)
4100 struct frame *f = client_data;
4102 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
4103 NULL);
4105 if (f)
4107 GtkRequisition req, req2;
4109 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4110 gtk_widget_get_preferred_size (w, NULL, &req2);
4111 req.width -= req2.width;
4112 req.height -= req2.height;
4113 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4114 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4115 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4116 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4117 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4118 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4119 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4120 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4121 xg_height_or_width_changed (f);
4125 /* This callback is called when a tool bar is reattached. We must set
4126 the height of the tool bar when this happens so frame sizes
4127 are correctly calculated.
4128 WBOX is the handle box widget that enables detach/attach of the tool bar.
4129 W is the tool bar widget.
4130 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
4132 static void
4133 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
4134 GtkWidget *w,
4135 gpointer client_data)
4137 struct frame *f = client_data;
4138 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
4140 if (f)
4142 GtkRequisition req, req2;
4144 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
4145 gtk_widget_get_preferred_size (w, NULL, &req2);
4146 req.width += req2.width;
4147 req.height += req2.height;
4148 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
4149 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
4150 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
4151 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
4152 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
4153 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
4154 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
4155 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
4156 xg_height_or_width_changed (f);
4160 /* This callback is called when the mouse enters or leaves a tool bar item.
4161 It is used for displaying and hiding the help text.
4162 W is the tool bar item, a button.
4163 EVENT is either an enter event or leave event.
4164 CLIENT_DATA is an integer that is the index of the button in the
4165 tool bar. 0 is the first button.
4167 Returns FALSE to tell GTK to keep processing this event. */
4169 static gboolean
4170 xg_tool_bar_help_callback (GtkWidget *w,
4171 GdkEventCrossing *event,
4172 gpointer client_data)
4174 intptr_t idx = (intptr_t) client_data;
4175 struct frame *f = g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
4176 Lisp_Object help, frame;
4178 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
4179 return FALSE;
4181 if (event->type == GDK_ENTER_NOTIFY)
4183 idx *= TOOL_BAR_ITEM_NSLOTS;
4184 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
4186 if (NILP (help))
4187 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
4189 else
4190 help = Qnil;
4192 XSETFRAME (frame, f);
4193 kbd_buffer_store_help_event (frame, help);
4195 return FALSE;
4199 /* This callback is called when a tool bar item shall be redrawn.
4200 It modifies the expose event so that the GtkImage widget redraws the
4201 whole image. This to overcome a bug that makes GtkImage draw the image
4202 in the wrong place when it tries to redraw just a part of the image.
4203 W is the GtkImage to be redrawn.
4204 EVENT is the expose event for W.
4205 CLIENT_DATA is unused.
4207 Returns FALSE to tell GTK to keep processing this event. */
4209 #ifndef HAVE_GTK3
4210 static gboolean
4211 xg_tool_bar_item_expose_callback (GtkWidget *w,
4212 GdkEventExpose *event,
4213 gpointer client_data)
4215 gint width, height;
4217 gdk_drawable_get_size (event->window, &width, &height);
4218 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4219 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4221 event->area.x = max (0, event->area.x);
4222 event->area.y = max (0, event->area.y);
4224 event->area.width = max (width, event->area.width);
4225 event->area.height = max (height, event->area.height);
4227 return FALSE;
4229 #endif
4231 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4232 #define toolbar_set_orientation(w, o) \
4233 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4234 #else
4235 #define toolbar_set_orientation(w, o) \
4236 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4237 #endif
4239 #ifdef HAVE_GTK_HANDLE_BOX_NEW
4240 #define TOOLBAR_TOP_WIDGET(x) ((x)->handlebox_widget)
4241 #else
4242 #define TOOLBAR_TOP_WIDGET(x) ((x)->toolbar_widget)
4243 #endif
4245 /* Attach a tool bar to frame F. */
4247 static void
4248 xg_pack_tool_bar (struct frame *f, Lisp_Object pos)
4250 struct x_output *x = f->output_data.x;
4251 bool into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4252 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
4254 toolbar_set_orientation (x->toolbar_widget,
4255 into_hbox
4256 ? GTK_ORIENTATION_VERTICAL
4257 : GTK_ORIENTATION_HORIZONTAL);
4258 #ifdef HAVE_GTK_HANDLE_BOX_NEW
4259 if (!x->handlebox_widget)
4261 top_widget = x->handlebox_widget = gtk_handle_box_new ();
4262 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4263 G_CALLBACK (xg_tool_bar_detach_callback), f);
4264 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4265 G_CALLBACK (xg_tool_bar_attach_callback), f);
4266 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4267 x->toolbar_widget);
4269 #endif
4271 if (into_hbox)
4273 #ifdef HAVE_GTK_HANDLE_BOX_NEW
4274 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4275 GTK_POS_TOP);
4276 #endif
4277 gtk_box_pack_start (GTK_BOX (x->hbox_widget), top_widget,
4278 FALSE, FALSE, 0);
4280 if (EQ (pos, Qleft))
4281 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4282 top_widget,
4284 x->toolbar_in_hbox = true;
4286 else
4288 bool vbox_pos = x->menubar_widget != 0;
4289 #ifdef HAVE_GTK_HANDLE_BOX_NEW
4290 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4291 GTK_POS_LEFT);
4292 #endif
4293 gtk_box_pack_start (GTK_BOX (x->vbox_widget), top_widget,
4294 FALSE, FALSE, 0);
4296 if (EQ (pos, Qtop))
4297 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4298 top_widget,
4299 vbox_pos);
4300 x->toolbar_in_hbox = false;
4302 x->toolbar_is_packed = true;
4305 static bool xg_update_tool_bar_sizes (struct frame *f);
4307 static void
4308 tb_size_cb (GtkWidget *widget,
4309 GdkRectangle *allocation,
4310 gpointer user_data)
4312 /* When tool bar is created it has one preferred size. But when size is
4313 allocated between widgets, it may get another. So we must update
4314 size hints if tool bar size changes. Seen on Fedora 18 at least. */
4315 struct frame *f = user_data;
4316 if (xg_update_tool_bar_sizes (f))
4317 xg_height_or_width_changed (f);
4320 /* Create a tool bar for frame F. */
4322 static void
4323 xg_create_tool_bar (struct frame *f)
4325 struct x_output *x = f->output_data.x;
4326 #if GTK_CHECK_VERSION (3, 3, 6)
4327 GtkStyleContext *gsty;
4328 #endif
4329 struct xg_frame_tb_info *tbinfo
4330 = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4331 TB_INFO_KEY);
4332 if (! tbinfo)
4334 tbinfo = xmalloc (sizeof (*tbinfo));
4335 tbinfo->last_tool_bar = Qnil;
4336 tbinfo->style = Qnil;
4337 tbinfo->hmargin = tbinfo->vmargin = 0;
4338 tbinfo->dir = GTK_TEXT_DIR_NONE;
4339 tbinfo->n_last_items = 0;
4340 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4341 TB_INFO_KEY,
4342 tbinfo);
4345 x->toolbar_widget = gtk_toolbar_new ();
4347 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4349 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4350 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4351 g_signal_connect (x->toolbar_widget, "size-allocate",
4352 G_CALLBACK (tb_size_cb), f);
4353 #if GTK_CHECK_VERSION (3, 3, 6)
4354 gsty = gtk_widget_get_style_context (x->toolbar_widget);
4355 gtk_style_context_add_class (gsty, "primary-toolbar");
4356 #endif
4360 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4362 /* Find the right-to-left image named by RTL in the tool bar images for F.
4363 Returns IMAGE if RTL is not found. */
4365 static Lisp_Object
4366 find_rtl_image (struct frame *f, Lisp_Object image, Lisp_Object rtl)
4368 int i;
4369 Lisp_Object file, rtl_name;
4370 struct gcpro gcpro1, gcpro2;
4371 GCPRO2 (file, rtl_name);
4373 rtl_name = Ffile_name_nondirectory (rtl);
4375 for (i = 0; i < f->n_tool_bar_items; ++i)
4377 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4378 if (!NILP (file = file_for_image (rtl_image)))
4380 file = call1 (intern ("file-name-sans-extension"),
4381 Ffile_name_nondirectory (file));
4382 if (! NILP (Fequal (file, rtl_name)))
4384 image = rtl_image;
4385 break;
4390 return image;
4393 static GtkToolItem *
4394 xg_make_tool_item (struct frame *f,
4395 GtkWidget *wimage,
4396 GtkWidget **wbutton,
4397 const char *label,
4398 int i, bool horiz, bool text_image)
4400 GtkToolItem *ti = gtk_tool_item_new ();
4401 GtkWidget *vb = gtk_box_new (horiz
4402 ? GTK_ORIENTATION_HORIZONTAL
4403 : GTK_ORIENTATION_VERTICAL,
4405 GtkWidget *wb = gtk_button_new ();
4406 /* The eventbox is here so we can have tooltips on disabled items. */
4407 GtkWidget *weventbox = gtk_event_box_new ();
4408 #if GTK_CHECK_VERSION (3, 3, 6)
4409 GtkCssProvider *css_prov = gtk_css_provider_new ();
4410 GtkStyleContext *gsty;
4412 gtk_css_provider_load_from_data (css_prov,
4413 "GtkEventBox {"
4414 " background-color: transparent;"
4415 "}",
4416 -1, NULL);
4418 gsty = gtk_widget_get_style_context (weventbox);
4419 gtk_style_context_add_provider (gsty,
4420 GTK_STYLE_PROVIDER (css_prov),
4421 GTK_STYLE_PROVIDER_PRIORITY_USER);
4422 g_object_unref (css_prov);
4423 #endif
4425 gtk_box_set_homogeneous (GTK_BOX (vb), FALSE);
4427 if (wimage && !text_image)
4428 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4429 if (label)
4430 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4431 if (wimage && text_image)
4432 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4434 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4435 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4436 gtk_container_add (GTK_CONTAINER (wb), vb);
4437 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4438 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4440 if (wimage || label)
4442 intptr_t ii = i;
4443 gpointer gi = (gpointer) ii;
4445 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4446 G_CALLBACK (xg_tool_bar_menu_proxy),
4447 gi);
4449 g_signal_connect (G_OBJECT (wb), "clicked",
4450 G_CALLBACK (xg_tool_bar_callback),
4451 gi);
4453 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4455 #ifndef HAVE_GTK3
4456 /* Catch expose events to overcome an annoying redraw bug, see
4457 comment for xg_tool_bar_item_expose_callback. */
4458 g_signal_connect (G_OBJECT (ti),
4459 "expose-event",
4460 G_CALLBACK (xg_tool_bar_item_expose_callback),
4462 #endif
4463 gtk_tool_item_set_homogeneous (ti, FALSE);
4465 /* Callback to save modifier mask (Shift/Control, etc). GTK makes
4466 no distinction based on modifiers in the activate callback,
4467 so we have to do it ourselves. */
4468 g_signal_connect (wb, "button-release-event",
4469 G_CALLBACK (xg_tool_bar_button_cb),
4470 NULL);
4472 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4474 /* Use enter/leave notify to show help. We use the events
4475 rather than the GtkButton specific signals "enter" and
4476 "leave", so we can have only one callback. The event
4477 will tell us what kind of event it is. */
4478 g_signal_connect (G_OBJECT (weventbox),
4479 "enter-notify-event",
4480 G_CALLBACK (xg_tool_bar_help_callback),
4481 gi);
4482 g_signal_connect (G_OBJECT (weventbox),
4483 "leave-notify-event",
4484 G_CALLBACK (xg_tool_bar_help_callback),
4485 gi);
4488 if (wbutton) *wbutton = wb;
4490 return ti;
4493 static bool
4494 is_box_type (GtkWidget *vb, bool is_horizontal)
4496 #ifdef HAVE_GTK3
4497 bool ret = 0;
4498 if (GTK_IS_BOX (vb))
4500 GtkOrientation ori = gtk_orientable_get_orientation (GTK_ORIENTABLE (vb));
4501 ret = (ori == GTK_ORIENTATION_HORIZONTAL && is_horizontal)
4502 || (ori == GTK_ORIENTATION_VERTICAL && ! is_horizontal);
4504 return ret;
4505 #else
4506 return is_horizontal ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb);
4507 #endif
4511 static bool
4512 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4513 const char *icon_name, const struct image *img,
4514 const char *label, bool horiz)
4516 gpointer old;
4517 GtkWidget *wimage;
4518 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4519 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4521 /* Check if the tool icon matches. */
4522 if (stock_name && wimage)
4524 old = g_object_get_data (G_OBJECT (wimage),
4525 XG_TOOL_BAR_STOCK_NAME);
4526 if (!old || strcmp (old, stock_name))
4527 return 1;
4529 else if (icon_name && wimage)
4531 old = g_object_get_data (G_OBJECT (wimage),
4532 XG_TOOL_BAR_ICON_NAME);
4533 if (!old || strcmp (old, icon_name))
4534 return 1;
4536 else if (wimage)
4538 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4539 XG_TOOL_BAR_IMAGE_DATA);
4540 Pixmap old_img = (Pixmap) gold_img;
4541 if (old_img != img->pixmap)
4542 return 1;
4545 /* Check button configuration and label. */
4546 if (is_box_type (vb, horiz)
4547 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4548 return 1;
4550 /* Ensure label is correct. */
4551 if (label && wlbl)
4552 gtk_label_set_text (GTK_LABEL (wlbl), label);
4553 return 0;
4556 static bool
4557 xg_update_tool_bar_sizes (struct frame *f)
4559 struct x_output *x = f->output_data.x;
4560 GtkRequisition req;
4561 int nl = 0, nr = 0, nt = 0, nb = 0;
4562 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
4564 gtk_widget_get_preferred_size (GTK_WIDGET (top_widget), NULL, &req);
4565 if (x->toolbar_in_hbox)
4567 int pos;
4568 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4569 top_widget,
4570 "position", &pos, NULL);
4571 if (pos == 0) nl = req.width;
4572 else nr = req.width;
4574 else
4576 int pos;
4577 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4578 top_widget,
4579 "position", &pos, NULL);
4580 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4581 else nb = req.height;
4584 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4585 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4586 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4587 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4589 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4590 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4591 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4592 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4593 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4594 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4595 return 1;
4598 return 0;
4602 /* Update the tool bar for frame F. Add new buttons and remove old. */
4604 void
4605 update_frame_tool_bar (struct frame *f)
4607 int i, j;
4608 struct x_output *x = f->output_data.x;
4609 int hmargin = 0, vmargin = 0;
4610 GtkToolbar *wtoolbar;
4611 GtkToolItem *ti;
4612 GtkTextDirection dir;
4613 Lisp_Object style;
4614 bool text_image, horiz;
4615 struct xg_frame_tb_info *tbinfo;
4617 if (! FRAME_GTK_WIDGET (f))
4618 return;
4620 block_input ();
4622 if (RANGED_INTEGERP (1, Vtool_bar_button_margin, INT_MAX))
4624 hmargin = XFASTINT (Vtool_bar_button_margin);
4625 vmargin = XFASTINT (Vtool_bar_button_margin);
4627 else if (CONSP (Vtool_bar_button_margin))
4629 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin), INT_MAX))
4630 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4632 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin), INT_MAX))
4633 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4636 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4637 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4638 i.e. zero. This means that margins less than
4639 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4640 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4641 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4643 if (! x->toolbar_widget)
4644 xg_create_tool_bar (f);
4646 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4647 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4649 style = Ftool_bar_get_system_style ();
4651 /* Are we up to date? */
4652 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4653 TB_INFO_KEY);
4655 if (! NILP (tbinfo->last_tool_bar) && ! NILP (f->tool_bar_items)
4656 && tbinfo->n_last_items == f->n_tool_bar_items
4657 && tbinfo->hmargin == hmargin && tbinfo->vmargin == vmargin
4658 && tbinfo->dir == dir
4659 && ! NILP (Fequal (tbinfo->style, style))
4660 && ! NILP (Fequal (tbinfo->last_tool_bar, f->tool_bar_items)))
4662 unblock_input ();
4663 return;
4666 tbinfo->last_tool_bar = f->tool_bar_items;
4667 tbinfo->n_last_items = f->n_tool_bar_items;
4668 tbinfo->style = style;
4669 tbinfo->hmargin = hmargin;
4670 tbinfo->vmargin = vmargin;
4671 tbinfo->dir = dir;
4673 text_image = EQ (style, Qtext_image_horiz);
4674 horiz = EQ (style, Qboth_horiz) || text_image;
4676 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4678 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4679 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4680 int idx;
4681 ptrdiff_t img_id;
4682 int icon_size = 0;
4683 struct image *img = NULL;
4684 Lisp_Object image;
4685 Lisp_Object stock = Qnil;
4686 GtkStockItem stock_item;
4687 char *stock_name = NULL;
4688 char *icon_name = NULL;
4689 Lisp_Object rtl;
4690 GtkWidget *wbutton = NULL;
4691 Lisp_Object specified_file;
4692 bool vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4693 const char *label
4694 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4695 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4696 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4697 : "";
4699 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4701 /* If this is a separator, use a gtk separator item. */
4702 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4704 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4706 if (ti)
4707 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4708 GTK_WIDGET (ti));
4709 ti = gtk_separator_tool_item_new ();
4710 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4712 j++;
4713 continue;
4716 /* Otherwise, the tool-bar item is an ordinary button. */
4718 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4720 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4721 ti = NULL;
4724 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4726 /* Ignore invalid image specifications. */
4727 image = PROP (TOOL_BAR_ITEM_IMAGES);
4728 if (!valid_image_p (image))
4730 if (ti)
4731 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4732 GTK_WIDGET (ti));
4733 continue;
4736 specified_file = file_for_image (image);
4737 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4738 stock = call1 (Qx_gtk_map_stock, specified_file);
4740 if (STRINGP (stock))
4742 stock_name = SSDATA (stock);
4743 if (stock_name[0] == 'n' && stock_name[1] == ':')
4745 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4746 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4748 icon_name = stock_name + 2;
4749 stock_name = NULL;
4750 stock = Qnil;
4752 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4753 icon_name = NULL;
4754 else
4755 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4757 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4758 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4759 else
4761 stock = Qnil;
4762 stock_name = NULL;
4766 if (stock_name == NULL && icon_name == NULL)
4768 /* No stock image, or stock item not known. Try regular
4769 image. If image is a vector, choose it according to the
4770 button state. */
4771 if (dir == GTK_TEXT_DIR_RTL
4772 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4773 && STRINGP (rtl))
4774 image = find_rtl_image (f, image, rtl);
4776 if (VECTORP (image))
4778 if (enabled_p)
4779 idx = (selected_p
4780 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4781 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4782 else
4783 idx = (selected_p
4784 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4785 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4787 eassert (ASIZE (image) >= idx);
4788 image = AREF (image, idx);
4790 else
4791 idx = -1;
4793 img_id = lookup_image (f, image);
4794 img = IMAGE_FROM_ID (f, img_id);
4795 prepare_image_for_display (f, img);
4797 if (img->load_failed_p || img->pixmap == None)
4799 if (ti)
4800 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4801 GTK_WIDGET (ti));
4802 continue;
4806 /* If there is an existing widget, check if it's stale; if so,
4807 remove it and make a new tool item from scratch. */
4808 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4809 img, label, horiz))
4811 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4812 GTK_WIDGET (ti));
4813 ti = NULL;
4816 if (ti == NULL)
4818 GtkWidget *w;
4820 /* Save the image so we can see if an update is needed the
4821 next time we call xg_tool_item_match_p. */
4822 if (EQ (style, Qtext))
4823 w = NULL;
4824 else if (stock_name)
4826 w = gtk_image_new_from_stock (stock_name, icon_size);
4827 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4828 (gpointer) xstrdup (stock_name),
4829 (GDestroyNotify) xfree);
4831 else if (icon_name)
4833 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4834 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4835 (gpointer) xstrdup (icon_name),
4836 (GDestroyNotify) xfree);
4838 else
4840 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4841 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4842 (gpointer)img->pixmap);
4845 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4846 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4847 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4850 #undef PROP
4852 gtk_widget_set_sensitive (wbutton, enabled_p);
4853 j++;
4856 /* Remove buttons not longer needed. */
4859 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4860 if (ti)
4861 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4862 } while (ti != NULL);
4864 if (f->n_tool_bar_items != 0)
4866 if (! x->toolbar_is_packed)
4867 xg_pack_tool_bar (f, f->tool_bar_position);
4868 gtk_widget_show_all (TOOLBAR_TOP_WIDGET (x));
4869 if (xg_update_tool_bar_sizes (f))
4870 xg_height_or_width_changed (f);
4873 unblock_input ();
4876 /* Deallocate all resources for the tool bar on frame F.
4877 Remove the tool bar. */
4879 void
4880 free_frame_tool_bar (struct frame *f)
4882 struct x_output *x = f->output_data.x;
4884 if (x->toolbar_widget)
4886 struct xg_frame_tb_info *tbinfo;
4887 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
4889 block_input ();
4890 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4891 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4892 if (x->toolbar_is_packed)
4894 if (x->toolbar_in_hbox)
4895 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4896 top_widget);
4897 else
4898 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4899 top_widget);
4901 else
4902 gtk_widget_destroy (x->toolbar_widget);
4904 x->toolbar_widget = 0;
4905 TOOLBAR_TOP_WIDGET (x) = 0;
4906 x->toolbar_is_packed = false;
4907 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4908 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4910 tbinfo = g_object_get_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4911 TB_INFO_KEY);
4912 if (tbinfo)
4914 xfree (tbinfo);
4915 g_object_set_data (G_OBJECT (FRAME_GTK_OUTER_WIDGET (f)),
4916 TB_INFO_KEY,
4917 NULL);
4920 xg_height_or_width_changed (f);
4922 unblock_input ();
4926 void
4927 xg_change_toolbar_position (struct frame *f, Lisp_Object pos)
4929 struct x_output *x = f->output_data.x;
4930 GtkWidget *top_widget = TOOLBAR_TOP_WIDGET (x);
4932 if (! x->toolbar_widget || ! top_widget)
4933 return;
4935 block_input ();
4936 g_object_ref (top_widget);
4937 if (x->toolbar_is_packed)
4939 if (x->toolbar_in_hbox)
4940 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4941 top_widget);
4942 else
4943 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4944 top_widget);
4947 xg_pack_tool_bar (f, pos);
4948 g_object_unref (top_widget);
4949 if (xg_update_tool_bar_sizes (f))
4950 xg_height_or_width_changed (f);
4952 unblock_input ();
4957 /***********************************************************************
4958 Initializing
4959 ***********************************************************************/
4960 void
4961 xg_initialize (void)
4963 GtkBindingSet *binding_set;
4964 GtkSettings *settings;
4966 #if HAVE_XFT
4967 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4968 we keep it permanently linked in. */
4969 XftInit (0);
4970 #endif
4972 gdpy_def = NULL;
4973 xg_ignore_gtk_scrollbar = 0;
4974 #ifdef HAVE_GTK_TEAROFF_MENU_ITEM_NEW
4975 xg_detached_menus = 0;
4976 #endif
4977 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4978 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4980 id_to_widget.max_size = id_to_widget.used = 0;
4981 id_to_widget.widgets = 0;
4983 settings = gtk_settings_get_for_screen (gdk_display_get_default_screen
4984 (gdk_display_get_default ()));
4985 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4986 bindings. It doesn't seem to be any way to remove properties,
4987 so we set it to "" which in means "no key". */
4988 gtk_settings_set_string_property (settings,
4989 "gtk-menu-bar-accel",
4991 EMACS_CLASS);
4993 /* Make GTK text input widgets use Emacs style keybindings. This is
4994 Emacs after all. */
4995 gtk_settings_set_string_property (settings,
4996 "gtk-key-theme-name",
4997 "Emacs",
4998 EMACS_CLASS);
5000 /* Make dialogs close on C-g. Since file dialog inherits from
5001 dialog, this works for them also. */
5002 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
5003 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5004 "close", 0);
5006 /* Make menus close on C-g. */
5007 binding_set = gtk_binding_set_by_class (g_type_class_ref
5008 (GTK_TYPE_MENU_SHELL));
5009 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
5010 "cancel", 0);
5011 update_theme_scrollbar_width ();
5013 #ifdef HAVE_FREETYPE
5014 x_last_font_name = NULL;
5015 #endif
5018 #endif /* USE_GTK */