* insdel.c (count_size_as_multibyte): Check for string overflow.
[emacs.git] / src / gtkutil.c
blob6e54006d913c9fa266720e20865160ffb1e0d885
1 /* Functions for creating and updating GTK widgets.
3 Copyright (C) 2003-2011 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 <signal.h>
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "blockinput.h"
29 #include "syssignal.h"
30 #include "window.h"
31 #include "gtkutil.h"
32 #include "termhooks.h"
33 #include "keyboard.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include <gdk/gdkkeysyms.h>
37 #include "xsettings.h"
39 #ifdef HAVE_XFT
40 #include <X11/Xft/Xft.h>
41 #endif
43 #ifdef HAVE_GTK3
44 #include <gtk/gtkx.h>
45 #endif
47 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
48 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
50 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
51 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
53 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
54 #define gtk_widget_set_has_window(w, b) \
55 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
56 #endif
57 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
58 #define gtk_dialog_get_action_area(w) ((w)->action_area)
59 #define gtk_dialog_get_content_area(w) ((w)->vbox)
60 #endif
61 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
62 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
63 #endif
64 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
65 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
66 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
67 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
68 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
69 #endif
70 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
71 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
72 #else
73 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
74 #endif
76 #ifndef HAVE_GTK3
77 #ifdef USE_GTK_TOOLTIP
78 #define gdk_window_get_screen(w) gdk_drawable_get_screen (w)
79 #endif
80 #define gdk_window_get_geometry(w, a, b, c, d) \
81 gdk_window_get_geometry (w, a, b, c, d, 0)
82 #define gdk_x11_window_lookup_for_display(d, w) \
83 gdk_xid_table_lookup_for_display (d, w)
84 #ifndef GDK_KEY_g
85 #define GDK_KEY_g GDK_g
86 #endif
87 #endif
89 #define XG_BIN_CHILD(x) gtk_bin_get_child (GTK_BIN (x))
91 /* Get the current value of the range, truncated to an integer. */
92 static int
93 int_gtk_range_get_value (GtkRange *range)
95 return gtk_range_get_value (range);
99 /***********************************************************************
100 Display handling functions
101 ***********************************************************************/
103 /* Keep track of the default display, or NULL if there is none. Emacs
104 may close all its displays. */
106 static GdkDisplay *gdpy_def;
108 /* When the GTK widget W is to be created on a display for F that
109 is not the default display, set the display for W.
110 W can be a GtkMenu or a GtkWindow widget. */
112 static void
113 xg_set_screen (GtkWidget *w, FRAME_PTR f)
115 if (FRAME_X_DISPLAY (f) != DEFAULT_GDK_DISPLAY ())
117 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
118 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
120 if (GTK_IS_MENU (w))
121 gtk_menu_set_screen (GTK_MENU (w), gscreen);
122 else
123 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
128 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
129 *DPY is set to NULL if the display can't be opened.
131 Returns non-zero if display could be opened, zero if display could not
132 be opened, and less than zero if the GTK version doesn't support
133 multipe displays. */
135 void
136 xg_display_open (char *display_name, Display **dpy)
138 GdkDisplay *gdpy;
140 gdpy = gdk_display_open (display_name);
141 if (!gdpy_def && gdpy)
143 gdpy_def = gdpy;
144 gdk_display_manager_set_default_display (gdk_display_manager_get (),
145 gdpy);
148 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
152 /* Close display DPY. */
154 void
155 xg_display_close (Display *dpy)
157 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
159 /* If this is the default display, try to change it before closing.
160 If there is no other display to use, gdpy_def is set to NULL, and
161 the next call to xg_display_open resets the default display. */
162 if (gdk_display_get_default () == gdpy)
164 struct x_display_info *dpyinfo;
165 GdkDisplay *gdpy_new = NULL;
167 /* Find another display. */
168 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
169 if (dpyinfo->display != dpy)
171 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
172 gdk_display_manager_set_default_display (gdk_display_manager_get (),
173 gdpy_new);
174 break;
176 gdpy_def = gdpy_new;
179 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
180 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
181 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
182 can continue running, but there will be memory leaks. */
183 g_object_run_dispose (G_OBJECT (gdpy));
184 #else
185 /* This seems to be fixed in GTK 2.10. */
186 gdk_display_close (gdpy);
187 #endif
191 /***********************************************************************
192 Utility functions
193 ***********************************************************************/
194 /* The next two variables and functions are taken from lwlib. */
195 static widget_value *widget_value_free_list;
196 static int malloc_cpt;
198 /* Allocate a widget_value structure, either by taking one from the
199 widget_value_free_list or by malloc:ing a new one.
201 Return a pointer to the allocated structure. */
203 widget_value *
204 malloc_widget_value (void)
206 widget_value *wv;
207 if (widget_value_free_list)
209 wv = widget_value_free_list;
210 widget_value_free_list = wv->free_list;
211 wv->free_list = 0;
213 else
215 wv = (widget_value *) xmalloc (sizeof (widget_value));
216 malloc_cpt++;
218 memset (wv, 0, sizeof (widget_value));
219 return wv;
222 /* This is analogous to free. It frees only what was allocated
223 by malloc_widget_value, and no substructures. */
225 void
226 free_widget_value (widget_value *wv)
228 if (wv->free_list)
229 abort ();
231 if (malloc_cpt > 25)
233 /* When the number of already allocated cells is too big,
234 We free it. */
235 xfree (wv);
236 malloc_cpt--;
238 else
240 wv->free_list = widget_value_free_list;
241 widget_value_free_list = wv;
246 /* Create and return the cursor to be used for popup menus and
247 scroll bars on display DPY. */
249 GdkCursor *
250 xg_create_default_cursor (Display *dpy)
252 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
253 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
256 static GdkPixbuf *
257 xg_get_pixbuf_from_pixmap (FRAME_PTR f, Pixmap pix)
259 int iunused;
260 GdkPixbuf *tmp_buf;
261 Window wunused;
262 unsigned int width, height, uunused;
263 XImage *xim;
265 XGetGeometry (FRAME_X_DISPLAY (f), pix, &wunused, &iunused, &iunused,
266 &width, &height, &uunused, &uunused);
268 xim = XGetImage (FRAME_X_DISPLAY (f), pix, 0, 0, width, height,
269 ~0, XYPixmap);
270 if (!xim) return 0;
272 tmp_buf = gdk_pixbuf_new_from_data ((guchar *) xim->data,
273 GDK_COLORSPACE_RGB,
274 FALSE,
275 xim->bitmap_unit,
276 (int) width,
277 (int) height,
278 xim->bytes_per_line,
279 NULL,
280 NULL);
281 XDestroyImage (xim);
282 return tmp_buf;
285 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
287 static GdkPixbuf *
288 xg_get_pixbuf_from_pix_and_mask (FRAME_PTR f,
289 Pixmap pix,
290 Pixmap mask)
292 int width, height;
293 GdkPixbuf *icon_buf, *tmp_buf;
295 tmp_buf = xg_get_pixbuf_from_pixmap (f, pix);
296 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
297 g_object_unref (G_OBJECT (tmp_buf));
299 width = gdk_pixbuf_get_width (icon_buf);
300 height = gdk_pixbuf_get_height (icon_buf);
302 if (mask)
304 GdkPixbuf *mask_buf = xg_get_pixbuf_from_pixmap (f, mask);
305 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
306 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
307 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
308 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
309 int y;
311 for (y = 0; y < height; ++y)
313 guchar *iconptr, *maskptr;
314 int x;
316 iconptr = pixels + y * rowstride;
317 maskptr = mask_pixels + y * mask_rowstride;
319 for (x = 0; x < width; ++x)
321 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
322 just R is sufficient. */
323 if (maskptr[0] == 0)
324 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
326 iconptr += rowstride/width;
327 maskptr += mask_rowstride/width;
331 g_object_unref (G_OBJECT (mask_buf));
334 return icon_buf;
337 static Lisp_Object
338 file_for_image (Lisp_Object image)
340 Lisp_Object specified_file = Qnil;
341 Lisp_Object tail;
343 for (tail = XCDR (image);
344 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
345 tail = XCDR (XCDR (tail)))
346 if (EQ (XCAR (tail), QCfile))
347 specified_file = XCAR (XCDR (tail));
349 return specified_file;
352 /* For the image defined in IMG, make and return a GtkImage. For displays with
353 8 planes or less we must make a GdkPixbuf and apply the mask manually.
354 Otherwise the highlightning and dimming the tool bar code in GTK does
355 will look bad. For display with more than 8 planes we just use the
356 pixmap and mask directly. For monochrome displays, GTK doesn't seem
357 able to use external pixmaps, it looks bad whatever we do.
358 The image is defined on the display where frame F is.
359 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
360 If OLD_WIDGET is NULL, a new widget is constructed and returned.
361 If OLD_WIDGET is not NULL, that widget is modified. */
363 static GtkWidget *
364 xg_get_image_for_pixmap (FRAME_PTR f,
365 struct image *img,
366 GtkWidget *widget,
367 GtkImage *old_widget)
369 GdkPixbuf *icon_buf;
371 /* If we have a file, let GTK do all the image handling.
372 This seems to be the only way to make insensitive and activated icons
373 look good in all cases. */
374 Lisp_Object specified_file = file_for_image (img->spec);
375 Lisp_Object file;
377 /* We already loaded the image once before calling this
378 function, so this only fails if the image file has been removed.
379 In that case, use the pixmap already loaded. */
381 if (STRINGP (specified_file)
382 && STRINGP (file = x_find_image_file (specified_file)))
384 if (! old_widget)
385 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
386 else
387 gtk_image_set_from_file (old_widget, SSDATA (file));
389 return GTK_WIDGET (old_widget);
392 /* No file, do the image handling ourselves. This will look very bad
393 on a monochrome display, and sometimes bad on all displays with
394 certain themes. */
396 /* This is a workaround to make icons look good on pseudo color
397 displays. Apparently GTK expects the images to have an alpha
398 channel. If they don't, insensitive and activated icons will
399 look bad. This workaround does not work on monochrome displays,
400 and is strictly not needed on true color/static color displays (i.e.
401 16 bits and higher). But we do it anyway so we get a pixbuf that is
402 not associated with the img->pixmap. The img->pixmap may be removed
403 by clearing the image cache and then the tool bar redraw fails, since
404 Gtk+ assumes the pixmap is always there. */
405 icon_buf = xg_get_pixbuf_from_pix_and_mask (f, img->pixmap, img->mask);
407 if (icon_buf)
409 if (! old_widget)
410 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
411 else
412 gtk_image_set_from_pixbuf (old_widget, icon_buf);
414 g_object_unref (G_OBJECT (icon_buf));
417 return GTK_WIDGET (old_widget);
421 /* Set CURSOR on W and all widgets W contain. We must do like this
422 for scroll bars and menu because they create widgets internally,
423 and it is those widgets that are visible. */
425 static void
426 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
428 GdkWindow *window = gtk_widget_get_window(w);
429 GList *children = gdk_window_peek_children (window);
431 gdk_window_set_cursor (window, cursor);
433 /* The scroll bar widget has more than one GDK window (had to look at
434 the source to figure this out), and there is no way to set cursor
435 on widgets in GTK. So we must set the cursor for all GDK windows.
436 Ditto for menus. */
438 for ( ; children; children = g_list_next (children))
439 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
442 /* Insert NODE into linked LIST. */
444 static void
445 xg_list_insert (xg_list_node *list, xg_list_node *node)
447 xg_list_node *list_start = list->next;
449 if (list_start) list_start->prev = node;
450 node->next = list_start;
451 node->prev = 0;
452 list->next = node;
455 /* Remove NODE from linked LIST. */
457 static void
458 xg_list_remove (xg_list_node *list, xg_list_node *node)
460 xg_list_node *list_start = list->next;
461 if (node == list_start)
463 list->next = node->next;
464 if (list->next) list->next->prev = 0;
466 else
468 node->prev->next = node->next;
469 if (node->next) node->next->prev = node->prev;
473 /* Allocate and return a utf8 version of STR. If STR is already
474 utf8 or NULL, just return a copy of STR.
475 A new string is allocated and the caller must free the result
476 with g_free. */
478 static char *
479 get_utf8_string (const char *str)
481 char *utf8_str;
483 if (!str) return NULL;
485 /* If not UTF-8, try current locale. */
486 if (!g_utf8_validate (str, -1, NULL))
487 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
488 else
489 return g_strdup (str);
491 if (!utf8_str)
493 /* Probably some control characters in str. Escape them. */
494 size_t nr_bad = 0;
495 gsize bytes_read;
496 gsize bytes_written;
497 unsigned char *p = (unsigned char *)str;
498 char *cp, *up;
499 GError *err = NULL;
501 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
502 &bytes_written, &err))
503 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
505 ++nr_bad;
506 p += bytes_written+1;
507 g_error_free (err);
508 err = NULL;
511 if (err)
513 g_error_free (err);
514 err = NULL;
516 if (cp) g_free (cp);
518 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
519 p = (unsigned char *)str;
521 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
522 &bytes_written, &err))
523 && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
525 strncpy (up, (char *)p, bytes_written);
526 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
527 up[bytes_written+4] = '\0';
528 up += bytes_written+4;
529 p += bytes_written+1;
530 g_error_free (err);
531 err = NULL;
534 if (cp)
536 strcat (utf8_str, cp);
537 g_free (cp);
539 if (err)
541 g_error_free (err);
542 err = NULL;
545 return utf8_str;
548 /* Check for special colors used in face spec for region face.
549 The colors are fetched from the Gtk+ theme.
550 Return 1 if color was found, 0 if not. */
553 xg_check_special_colors (struct frame *f,
554 const char *color_name,
555 XColor *color)
557 int success_p = 0;
558 int get_bg = strcmp ("gtk_selection_bg_color", color_name) == 0;
559 int get_fg = !get_bg && strcmp ("gtk_selection_fg_color", color_name) == 0;
561 if (! FRAME_GTK_WIDGET (f) || ! (get_bg || get_fg))
562 return success_p;
564 BLOCK_INPUT;
566 #ifdef HAVE_GTK3
567 GtkStyleContext *gsty
568 = gtk_widget_get_style_context (FRAME_GTK_OUTER_WIDGET (f));
569 GdkRGBA col;
570 char buf[64];
571 int state = GTK_STATE_FLAG_SELECTED|GTK_STATE_FLAG_FOCUSED;
572 if (get_fg)
573 gtk_style_context_get_color (gsty, state, &col);
574 else
575 gtk_style_context_get_background_color (gsty, state, &col);
577 sprintf (buf, "rgbi:%lf/%lf/%lf", col.red, col.green, col.blue);
578 success_p = XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
579 buf, color);
580 #else
581 GtkStyle *gsty = gtk_widget_get_style (FRAME_GTK_WIDGET (f));
582 GdkColor *grgb = get_bg
583 ? &gsty->bg[GTK_STATE_SELECTED]
584 : &gsty->fg[GTK_STATE_SELECTED];
586 color->red = grgb->red;
587 color->green = grgb->green;
588 color->blue = grgb->blue;
589 color->pixel = grgb->pixel;
590 success_p = 1;
591 #endif
594 UNBLOCK_INPUT;
595 return success_p;
600 /***********************************************************************
601 Tooltips
602 ***********************************************************************/
603 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
604 We use that to pop down the tooltip. This happens if Gtk+ for some
605 reason wants to change or hide the tooltip. */
607 #ifdef USE_GTK_TOOLTIP
609 static void
610 hierarchy_ch_cb (GtkWidget *widget,
611 GtkWidget *previous_toplevel,
612 gpointer user_data)
614 FRAME_PTR f = (FRAME_PTR) user_data;
615 struct x_output *x = f->output_data.x;
616 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
618 if (! top || ! GTK_IS_WINDOW (top))
619 gtk_widget_hide (previous_toplevel);
622 /* Callback called when Gtk+ thinks a tooltip should be displayed.
623 We use it to get the tooltip window and the tooltip widget so
624 we can manipulate the ourselves.
626 Return FALSE ensures that the tooltip is not shown. */
628 static gboolean
629 qttip_cb (GtkWidget *widget,
630 gint xpos,
631 gint ypos,
632 gboolean keyboard_mode,
633 GtkTooltip *tooltip,
634 gpointer user_data)
636 FRAME_PTR f = (FRAME_PTR) user_data;
637 struct x_output *x = f->output_data.x;
638 if (x->ttip_widget == NULL)
640 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
641 x->ttip_widget = tooltip;
642 g_object_ref (G_OBJECT (tooltip));
643 x->ttip_lbl = gtk_label_new ("");
644 g_object_ref (G_OBJECT (x->ttip_lbl));
645 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
646 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
647 /* ATK needs an empty title for some reason. */
648 gtk_window_set_title (x->ttip_window, "");
649 /* Realize so we can safely get screen later on. */
650 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
651 gtk_widget_realize (x->ttip_lbl);
653 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
654 G_CALLBACK (hierarchy_ch_cb), f);
656 return FALSE;
659 #endif /* USE_GTK_TOOLTIP */
661 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
662 Return zero if no system tooltip available, non-zero otherwise. */
665 xg_prepare_tooltip (FRAME_PTR f,
666 Lisp_Object string,
667 int *width,
668 int *height)
670 #ifndef USE_GTK_TOOLTIP
671 return 0;
672 #else
673 struct x_output *x = f->output_data.x;
674 GtkWidget *widget;
675 GdkWindow *gwin;
676 GdkScreen *screen;
677 GtkSettings *settings;
678 gboolean tt_enabled = TRUE;
679 GtkRequisition req;
680 Lisp_Object encoded_string;
682 if (!x->ttip_lbl) return 0;
684 BLOCK_INPUT;
685 encoded_string = ENCODE_UTF_8 (string);
686 widget = GTK_WIDGET (x->ttip_lbl);
687 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
688 screen = gdk_window_get_screen (gwin);
689 settings = gtk_settings_get_for_screen (screen);
690 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
691 if (tt_enabled)
693 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
694 /* Record that we disabled it so it can be enabled again. */
695 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
696 (gpointer)f);
699 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
700 g_object_set_data (G_OBJECT
701 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
702 "gdk-display-current-tooltip", NULL);
704 /* Put out dummy widget in so we can get callbacks for unrealize and
705 hierarchy-changed. */
706 gtk_tooltip_set_custom (x->ttip_widget, widget);
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 (FRAME_PTR 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 non-zero if tip was hidden, non-ero if not (i.e. not using
739 system tooltips). */
742 xg_hide_tooltip (FRAME_PTR f)
744 int 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 /* Make a geometry string and pass that to GTK. It seems this is the
773 only way to get geometry position right if the user explicitly
774 asked for a position when starting Emacs.
775 F is the frame we shall set geometry for. */
777 static void
778 xg_set_geometry (FRAME_PTR f)
780 if (f->size_hint_flags & (USPosition | PPosition))
782 int left = f->left_pos;
783 int xneg = f->size_hint_flags & XNegative;
784 int top = f->top_pos;
785 int yneg = f->size_hint_flags & YNegative;
786 char geom_str[32];
788 if (xneg)
789 left = -left;
790 if (yneg)
791 top = -top;
793 sprintf (geom_str, "=%dx%d%c%d%c%d",
794 FRAME_PIXEL_WIDTH (f),
795 FRAME_PIXEL_HEIGHT (f),
796 (xneg ? '-' : '+'), left,
797 (yneg ? '-' : '+'), top);
799 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
800 geom_str))
801 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
805 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
806 and use a GtkFixed widget, this doesn't happen automatically. */
808 static void
809 xg_clear_under_internal_border (FRAME_PTR f)
811 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
813 GtkWidget *wfixed = f->output_data.x->edit_widget;
814 gtk_widget_queue_draw (wfixed);
815 gdk_window_process_all_updates ();
816 x_clear_area (FRAME_X_DISPLAY (f),
817 FRAME_X_WINDOW (f),
818 0, 0,
819 FRAME_PIXEL_WIDTH (f),
820 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
821 x_clear_area (FRAME_X_DISPLAY (f),
822 FRAME_X_WINDOW (f),
823 0, 0,
824 FRAME_INTERNAL_BORDER_WIDTH (f),
825 FRAME_PIXEL_HEIGHT (f), 0);
826 x_clear_area (FRAME_X_DISPLAY (f),
827 FRAME_X_WINDOW (f),
828 0, FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
829 FRAME_PIXEL_WIDTH (f),
830 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
831 x_clear_area (FRAME_X_DISPLAY (f),
832 FRAME_X_WINDOW (f),
833 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
835 FRAME_INTERNAL_BORDER_WIDTH (f),
836 FRAME_PIXEL_HEIGHT (f), 0);
840 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
841 and a Gtk+ menu bar, we get resize events for the edit part of the
842 frame only. We let Gtk+ deal with the Gtk+ parts.
843 F is the frame to resize.
844 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
846 void
847 xg_frame_resized (FRAME_PTR f, int pixelwidth, int pixelheight)
849 int rows, columns;
851 if (pixelwidth == -1 && pixelheight == -1)
853 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
854 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
855 0, 0,
856 &pixelwidth, &pixelheight);
857 else return;
861 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
862 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
864 if (columns != FRAME_COLS (f)
865 || rows != FRAME_LINES (f)
866 || pixelwidth != FRAME_PIXEL_WIDTH (f)
867 || pixelheight != FRAME_PIXEL_HEIGHT (f))
869 FRAME_PIXEL_WIDTH (f) = pixelwidth;
870 FRAME_PIXEL_HEIGHT (f) = pixelheight;
872 xg_clear_under_internal_border (f);
873 change_frame_size (f, rows, columns, 0, 1, 0);
874 SET_FRAME_GARBAGED (f);
875 cancel_mouse_face (f);
879 /* Resize the outer window of frame F after chainging the height.
880 COLUMNS/ROWS is the size the edit area shall have after the resize. */
882 void
883 xg_frame_set_char_size (FRAME_PTR f, int cols, int rows)
885 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
886 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
887 int pixelwidth;
889 if (FRAME_PIXEL_HEIGHT (f) == 0)
890 return;
892 /* Take into account the size of the scroll bar. Always use the
893 number of columns occupied by the scroll bar here otherwise we
894 might end up with a frame width that is not a multiple of the
895 frame's character width which is bad for vertically split
896 windows. */
897 f->scroll_bar_actual_width
898 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
900 compute_fringe_widths (f, 0);
902 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
903 after calculating that value. */
904 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
905 + FRAME_TOOLBAR_WIDTH (f);
908 /* Do this before resize, as we don't know yet if we will be resized. */
909 xg_clear_under_internal_border (f);
911 /* Must resize our top level widget. Font size may have changed,
912 but not rows/cols. */
913 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
914 pixelwidth, pixelheight);
915 x_wm_set_size_hint (f, 0, 0);
917 SET_FRAME_GARBAGED (f);
918 cancel_mouse_face (f);
920 /* We can not call change_frame_size for a mapped frame,
921 we can not set pixel width/height either. The window manager may
922 override our resize request, XMonad does this all the time.
923 The best we can do is try to sync, so lisp code sees the updated
924 size as fast as possible.
925 For unmapped windows, we can set rows/cols. When
926 the frame is mapped again we will (hopefully) get the correct size. */
927 if (f->async_visible)
929 /* Must call this to flush out events */
930 (void)gtk_events_pending ();
931 gdk_flush ();
932 x_wait_for_event (f, ConfigureNotify);
934 else
936 change_frame_size (f, rows, cols, 0, 1, 0);
937 FRAME_PIXEL_WIDTH (f) = pixelwidth;
938 FRAME_PIXEL_HEIGHT (f) = pixelheight;
942 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
943 The policy is to keep the number of editable lines. */
945 static void
946 xg_height_or_width_changed (FRAME_PTR f)
948 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
949 FRAME_TOTAL_PIXEL_WIDTH (f),
950 FRAME_TOTAL_PIXEL_HEIGHT (f));
951 f->output_data.x->hint_flags = 0;
952 x_wm_set_size_hint (f, 0, 0);
955 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
956 Must be done like this, because GtkWidget:s can have "hidden"
957 X Window that aren't accessible.
959 Return 0 if no widget match WDESC. */
961 GtkWidget *
962 xg_win_to_widget (Display *dpy, Window wdesc)
964 gpointer gdkwin;
965 GtkWidget *gwdesc = 0;
967 BLOCK_INPUT;
969 gdkwin = gdk_x11_window_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
970 wdesc);
971 if (gdkwin)
973 GdkEvent event;
974 event.any.window = gdkwin;
975 gwdesc = gtk_get_event_widget (&event);
978 UNBLOCK_INPUT;
979 return gwdesc;
982 /* Set the background of widget W to PIXEL. */
984 static void
985 xg_set_widget_bg (FRAME_PTR f, GtkWidget *w, long unsigned int pixel)
987 #ifdef HAVE_GTK3
988 GdkRGBA bg;
989 XColor xbg;
990 xbg.pixel = pixel;
991 if (XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &xbg))
993 bg.red = (double)xbg.red/65536.0;
994 bg.green = (double)xbg.green/65536.0;
995 bg.blue = (double)xbg.blue/65536.0;
996 bg.alpha = 1.0;
997 gtk_widget_override_background_color (w, GTK_STATE_FLAG_NORMAL, &bg);
999 #else
1000 GdkColor bg;
1001 GdkColormap *map = gtk_widget_get_colormap (w);
1002 gdk_colormap_query_color (map, pixel, &bg);
1003 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &bg);
1004 #endif
1007 /* Callback called when the gtk theme changes.
1008 We notify lisp code so it can fix faces used for region for example. */
1010 static void
1011 style_changed_cb (GObject *go,
1012 GParamSpec *spec,
1013 gpointer user_data)
1015 struct input_event event;
1016 GdkDisplay *gdpy = (GdkDisplay *) user_data;
1017 const char *display_name = gdk_display_get_name (gdpy);
1019 EVENT_INIT (event);
1020 event.kind = CONFIG_CHANGED_EVENT;
1021 event.frame_or_window = make_string (display_name, strlen (display_name));
1022 /* Theme doesn't change often, so intern is called seldom. */
1023 event.arg = intern ("theme-name");
1024 kbd_buffer_store_event (&event);
1027 /* Called when a delete-event occurs on WIDGET. */
1029 static gboolean
1030 delete_cb (GtkWidget *widget,
1031 GdkEvent *event,
1032 gpointer user_data)
1034 #ifdef HAVE_GTK3
1035 /* The event doesn't arrive in the normal event loop. Send event
1036 here. */
1037 FRAME_PTR f = (FRAME_PTR) user_data;
1038 struct input_event ie;
1040 EVENT_INIT (ie);
1041 ie.kind = DELETE_WINDOW_EVENT;
1042 XSETFRAME (ie.frame_or_window, f);
1043 kbd_buffer_store_event (&ie);
1044 #endif
1046 return TRUE;
1049 /* Create and set up the GTK widgets for frame F.
1050 Return 0 if creation failed, non-zero otherwise. */
1053 xg_create_frame_widgets (FRAME_PTR f)
1055 GtkWidget *wtop;
1056 GtkWidget *wvbox, *whbox;
1057 GtkWidget *wfixed;
1058 GtkRcStyle *style;
1059 char *title = 0;
1061 BLOCK_INPUT;
1063 if (FRAME_X_EMBEDDED_P (f))
1064 wtop = gtk_plug_new (f->output_data.x->parent_desc);
1065 else
1066 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1068 xg_set_screen (wtop, f);
1070 wvbox = gtk_vbox_new (FALSE, 0);
1071 whbox = gtk_hbox_new (FALSE, 0);
1072 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
1074 if (! wtop || ! wvbox || ! whbox || ! wfixed)
1076 if (wtop) gtk_widget_destroy (wtop);
1077 if (wvbox) gtk_widget_destroy (wvbox);
1078 if (whbox) gtk_widget_destroy (whbox);
1079 if (wfixed) gtk_widget_destroy (wfixed);
1081 UNBLOCK_INPUT;
1082 return 0;
1085 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
1086 gtk_widget_set_name (wtop, EMACS_CLASS);
1087 gtk_widget_set_name (wvbox, "pane");
1088 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
1090 /* If this frame has a title or name, set it in the title bar. */
1091 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
1092 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
1094 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
1096 FRAME_GTK_OUTER_WIDGET (f) = wtop;
1097 FRAME_GTK_WIDGET (f) = wfixed;
1098 f->output_data.x->vbox_widget = wvbox;
1099 f->output_data.x->hbox_widget = whbox;
1101 gtk_widget_set_has_window (wfixed, TRUE);
1103 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
1104 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
1105 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
1107 if (FRAME_EXTERNAL_TOOL_BAR (f))
1108 update_frame_tool_bar (f);
1110 /* We don't want this widget double buffered, because we draw on it
1111 with regular X drawing primitives, so from a GTK/GDK point of
1112 view, the widget is totally blank. When an expose comes, this
1113 will make the widget blank, and then Emacs redraws it. This flickers
1114 a lot, so we turn off double buffering. */
1115 gtk_widget_set_double_buffered (wfixed, FALSE);
1117 gtk_window_set_wmclass (GTK_WINDOW (wtop),
1118 SSDATA (Vx_resource_name),
1119 SSDATA (Vx_resource_class));
1121 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
1122 GTK is to destroy the widget. We want Emacs to do that instead. */
1123 g_signal_connect (G_OBJECT (wtop), "delete-event",
1124 G_CALLBACK (delete_cb), f);
1126 /* Convert our geometry parameters into a geometry string
1127 and specify it.
1128 GTK will itself handle calculating the real position this way. */
1129 xg_set_geometry (f);
1130 f->win_gravity
1131 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1133 gtk_widget_add_events (wfixed,
1134 GDK_POINTER_MOTION_MASK
1135 | GDK_EXPOSURE_MASK
1136 | GDK_BUTTON_PRESS_MASK
1137 | GDK_BUTTON_RELEASE_MASK
1138 | GDK_KEY_PRESS_MASK
1139 | GDK_ENTER_NOTIFY_MASK
1140 | GDK_LEAVE_NOTIFY_MASK
1141 | GDK_FOCUS_CHANGE_MASK
1142 | GDK_STRUCTURE_MASK
1143 | GDK_VISIBILITY_NOTIFY_MASK);
1145 /* Must realize the windows so the X window gets created. It is used
1146 by callers of this function. */
1147 gtk_widget_realize (wfixed);
1148 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1150 /* Since GTK clears its window by filling with the background color,
1151 we must keep X and GTK background in sync. */
1152 xg_set_widget_bg (f, wfixed, FRAME_BACKGROUND_PIXEL (f));
1154 #ifndef HAVE_GTK3
1155 /* Also, do not let any background pixmap to be set, this looks very
1156 bad as Emacs overwrites the background pixmap with its own idea
1157 of background color. */
1158 style = gtk_widget_get_modifier_style (wfixed);
1160 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1161 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1162 gtk_widget_modify_style (wfixed, style);
1163 #else
1164 gtk_widget_set_can_focus (wfixed, TRUE);
1165 #endif
1167 #ifdef USE_GTK_TOOLTIP
1168 /* Steal a tool tip window we can move ourselves. */
1169 f->output_data.x->ttip_widget = 0;
1170 f->output_data.x->ttip_lbl = 0;
1171 f->output_data.x->ttip_window = 0;
1172 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1173 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1174 #endif
1177 GdkScreen *screen = gtk_widget_get_screen (wtop);
1178 GtkSettings *gs = gtk_settings_get_for_screen (screen);
1179 /* Only connect this signal once per screen. */
1180 if (! g_signal_handler_find (G_OBJECT (gs),
1181 G_SIGNAL_MATCH_FUNC,
1182 0, 0, 0,
1183 G_CALLBACK (style_changed_cb),
1186 g_signal_connect (G_OBJECT (gs), "notify::gtk-theme-name",
1187 G_CALLBACK (style_changed_cb),
1188 gdk_screen_get_display (screen));
1192 UNBLOCK_INPUT;
1194 return 1;
1197 void
1198 xg_free_frame_widgets (FRAME_PTR f)
1200 if (FRAME_GTK_OUTER_WIDGET (f))
1202 #ifdef USE_GTK_TOOLTIP
1203 struct x_output *x = f->output_data.x;
1204 #endif
1205 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1206 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1207 FRAME_GTK_OUTER_WIDGET (f) = 0;
1208 #ifdef USE_GTK_TOOLTIP
1209 if (x->ttip_lbl)
1210 gtk_widget_destroy (x->ttip_lbl);
1211 if (x->ttip_widget)
1212 g_object_unref (G_OBJECT (x->ttip_widget));
1213 #endif
1217 /* Set the normal size hints for the window manager, for frame F.
1218 FLAGS is the flags word to use--or 0 meaning preserve the flags
1219 that the window now has.
1220 If USER_POSITION is nonzero, we set the User Position
1221 flag (this is useful when FLAGS is 0). */
1223 void
1224 x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
1226 /* Must use GTK routines here, otherwise GTK resets the size hints
1227 to its own defaults. */
1228 GdkGeometry size_hints;
1229 gint hint_flags = 0;
1230 int base_width, base_height;
1231 int min_rows = 0, min_cols = 0;
1232 int win_gravity = f->win_gravity;
1234 /* Don't set size hints during initialization; that apparently leads
1235 to a race condition. See the thread at
1236 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1237 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1238 return;
1240 if (flags)
1242 memset (&size_hints, 0, sizeof (size_hints));
1243 f->output_data.x->size_hints = size_hints;
1244 f->output_data.x->hint_flags = hint_flags;
1246 else
1247 flags = f->size_hint_flags;
1249 size_hints = f->output_data.x->size_hints;
1250 hint_flags = f->output_data.x->hint_flags;
1252 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1253 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
1254 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
1256 hint_flags |= GDK_HINT_BASE_SIZE;
1257 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
1258 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
1259 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1261 check_frame_size (f, &min_rows, &min_cols);
1263 size_hints.base_width = base_width;
1264 size_hints.base_height = base_height;
1265 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
1266 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
1268 /* These currently have a one to one mapping with the X values, but I
1269 don't think we should rely on that. */
1270 hint_flags |= GDK_HINT_WIN_GRAVITY;
1271 size_hints.win_gravity = 0;
1272 if (win_gravity == NorthWestGravity)
1273 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1274 else if (win_gravity == NorthGravity)
1275 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1276 else if (win_gravity == NorthEastGravity)
1277 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1278 else if (win_gravity == WestGravity)
1279 size_hints.win_gravity = GDK_GRAVITY_WEST;
1280 else if (win_gravity == CenterGravity)
1281 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1282 else if (win_gravity == EastGravity)
1283 size_hints.win_gravity = GDK_GRAVITY_EAST;
1284 else if (win_gravity == SouthWestGravity)
1285 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1286 else if (win_gravity == SouthGravity)
1287 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1288 else if (win_gravity == SouthEastGravity)
1289 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1290 else if (win_gravity == StaticGravity)
1291 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1293 if (user_position)
1295 hint_flags &= ~GDK_HINT_POS;
1296 hint_flags |= GDK_HINT_USER_POS;
1299 if (hint_flags != f->output_data.x->hint_flags
1300 || memcmp (&size_hints,
1301 &f->output_data.x->size_hints,
1302 sizeof (size_hints)) != 0)
1304 BLOCK_INPUT;
1305 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1306 NULL, &size_hints, hint_flags);
1307 f->output_data.x->size_hints = size_hints;
1308 f->output_data.x->hint_flags = hint_flags;
1309 UNBLOCK_INPUT;
1313 /* Change background color of a frame.
1314 Since GTK uses the background color to clear the window, we must
1315 keep the GTK and X colors in sync.
1316 F is the frame to change,
1317 BG is the pixel value to change to. */
1319 void
1320 xg_set_background_color (FRAME_PTR f, long unsigned int bg)
1322 if (FRAME_GTK_WIDGET (f))
1324 BLOCK_INPUT;
1325 xg_set_widget_bg (f, FRAME_GTK_WIDGET (f), FRAME_BACKGROUND_PIXEL (f));
1326 UNBLOCK_INPUT;
1331 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1332 functions so GTK does not overwrite the icon. */
1334 void
1335 xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
1337 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (f,
1338 icon_pixmap,
1339 icon_mask);
1340 if (gp)
1341 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1346 /***********************************************************************
1347 Dialog functions
1348 ***********************************************************************/
1349 /* Return the dialog title to use for a dialog of type KEY.
1350 This is the encoding used by lwlib. We use the same for GTK. */
1352 static const char *
1353 get_dialog_title (char key)
1355 const char *title = "";
1357 switch (key) {
1358 case 'E': case 'e':
1359 title = "Error";
1360 break;
1362 case 'I': case 'i':
1363 title = "Information";
1364 break;
1366 case 'L': case 'l':
1367 title = "Prompt";
1368 break;
1370 case 'P': case 'p':
1371 title = "Prompt";
1372 break;
1374 case 'Q': case 'q':
1375 title = "Question";
1376 break;
1379 return title;
1382 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1383 the dialog, but return TRUE so the event does not propagate further
1384 in GTK. This prevents GTK from destroying the dialog widget automatically
1385 and we can always destrou the widget manually, regardles of how
1386 it was popped down (button press or WM_DELETE_WINDOW).
1387 W is the dialog widget.
1388 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1389 user_data is NULL (not used).
1391 Returns TRUE to end propagation of event. */
1393 static gboolean
1394 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1396 gtk_widget_unmap (w);
1397 return TRUE;
1400 /* Create a popup dialog window. See also xg_create_widget below.
1401 WV is a widget_value describing the dialog.
1402 SELECT_CB is the callback to use when a button has been pressed.
1403 DEACTIVATE_CB is the callback to use when the dialog pops down.
1405 Returns the GTK dialog widget. */
1407 static GtkWidget *
1408 create_dialog (widget_value *wv,
1409 GCallback select_cb,
1410 GCallback deactivate_cb)
1412 const char *title = get_dialog_title (wv->name[0]);
1413 int total_buttons = wv->name[1] - '0';
1414 int right_buttons = wv->name[4] - '0';
1415 int left_buttons;
1416 int button_nr = 0;
1417 int button_spacing = 10;
1418 GtkWidget *wdialog = gtk_dialog_new ();
1419 GtkDialog *wd = GTK_DIALOG (wdialog);
1420 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1421 widget_value *item;
1422 GtkWidget *whbox_down;
1424 /* If the number of buttons is greater than 4, make two rows of buttons
1425 instead. This looks better. */
1426 int make_two_rows = total_buttons > 4;
1428 if (right_buttons == 0) right_buttons = total_buttons/2;
1429 left_buttons = total_buttons - right_buttons;
1431 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1432 gtk_widget_set_name (wdialog, "emacs-dialog");
1435 if (make_two_rows)
1437 GtkWidget *wvbox = gtk_vbox_new (TRUE, button_spacing);
1438 GtkWidget *whbox_up = gtk_hbox_new (FALSE, 0);
1439 whbox_down = gtk_hbox_new (FALSE, 0);
1441 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1442 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1443 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1445 cur_box = GTK_BOX (whbox_up);
1448 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1449 G_CALLBACK (dialog_delete_callback), 0);
1451 if (deactivate_cb)
1453 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1454 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1457 for (item = wv->contents; item; item = item->next)
1459 char *utf8_label = get_utf8_string (item->value);
1460 GtkWidget *w;
1461 GtkRequisition req;
1463 if (item->name && strcmp (item->name, "message") == 0)
1465 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1466 /* This is the text part of the dialog. */
1467 w = gtk_label_new (utf8_label);
1468 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1469 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1470 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1472 /* Try to make dialog look better. Must realize first so
1473 the widget can calculate the size it needs. */
1474 gtk_widget_realize (w);
1475 gtk_widget_get_preferred_size (w, NULL, &req);
1476 gtk_box_set_spacing (wvbox, req.height);
1477 if (item->value && strlen (item->value) > 0)
1478 button_spacing = 2*req.width/strlen (item->value);
1480 else
1482 /* This is one button to add to the dialog. */
1483 w = gtk_button_new_with_label (utf8_label);
1484 if (! item->enabled)
1485 gtk_widget_set_sensitive (w, FALSE);
1486 if (select_cb)
1487 g_signal_connect (G_OBJECT (w), "clicked",
1488 select_cb, item->call_data);
1490 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1491 if (++button_nr == left_buttons)
1493 if (make_two_rows)
1494 cur_box = GTK_BOX (whbox_down);
1495 else
1496 gtk_box_pack_start (cur_box,
1497 gtk_label_new (""),
1498 TRUE, TRUE,
1499 button_spacing);
1503 if (utf8_label)
1504 g_free (utf8_label);
1507 return wdialog;
1510 struct xg_dialog_data
1512 GMainLoop *loop;
1513 int response;
1514 GtkWidget *w;
1515 guint timerid;
1518 /* Function that is called when the file or font dialogs pop down.
1519 W is the dialog widget, RESPONSE is the response code.
1520 USER_DATA is what we passed in to g_signal_connect. */
1522 static void
1523 xg_dialog_response_cb (GtkDialog *w,
1524 gint response,
1525 gpointer user_data)
1527 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1528 dd->response = response;
1529 g_main_loop_quit (dd->loop);
1533 /* Destroy the dialog. This makes it pop down. */
1535 static Lisp_Object
1536 pop_down_dialog (Lisp_Object arg)
1538 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1539 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1541 BLOCK_INPUT;
1542 if (dd->w) gtk_widget_destroy (dd->w);
1543 if (dd->timerid != 0) g_source_remove (dd->timerid);
1545 g_main_loop_quit (dd->loop);
1546 g_main_loop_unref (dd->loop);
1548 UNBLOCK_INPUT;
1550 return Qnil;
1553 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1554 We pass in DATA as gpointer* so we can use this as a callback. */
1556 static gboolean
1557 xg_maybe_add_timer (gpointer data)
1559 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1560 EMACS_TIME next_time = timer_check ();
1561 long secs = EMACS_SECS (next_time);
1562 long usecs = EMACS_USECS (next_time);
1564 dd->timerid = 0;
1566 if (secs >= 0 && usecs >= 0 && secs < ((guint)-1)/1000)
1568 dd->timerid = g_timeout_add (secs * 1000 + usecs/1000,
1569 xg_maybe_add_timer,
1570 dd);
1572 return FALSE;
1576 /* Pops up a modal dialog W and waits for response.
1577 We don't use gtk_dialog_run because we want to process emacs timers.
1578 The dialog W is not destroyed when this function returns. */
1580 static int
1581 xg_dialog_run (FRAME_PTR f, GtkWidget *w)
1583 int count = SPECPDL_INDEX ();
1584 struct xg_dialog_data dd;
1586 xg_set_screen (w, f);
1587 gtk_window_set_transient_for (GTK_WINDOW (w),
1588 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1589 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1590 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1592 dd.loop = g_main_loop_new (NULL, FALSE);
1593 dd.response = GTK_RESPONSE_CANCEL;
1594 dd.w = w;
1595 dd.timerid = 0;
1597 g_signal_connect (G_OBJECT (w),
1598 "response",
1599 G_CALLBACK (xg_dialog_response_cb),
1600 &dd);
1601 /* Don't destroy the widget if closed by the window manager close button. */
1602 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1603 gtk_widget_show (w);
1605 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1607 (void) xg_maybe_add_timer (&dd);
1608 g_main_loop_run (dd.loop);
1610 dd.w = 0;
1611 unbind_to (count, Qnil);
1613 return dd.response;
1617 /***********************************************************************
1618 File dialog functions
1619 ***********************************************************************/
1620 /* Return non-zero if the old file selection dialog is being used.
1621 Return zero if not. */
1624 xg_uses_old_file_dialog (void)
1626 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1627 return x_gtk_use_old_file_dialog;
1628 #else
1629 return 0;
1630 #endif
1634 typedef char * (*xg_get_file_func) (GtkWidget *);
1636 /* Return the selected file for file chooser dialog W.
1637 The returned string must be free:d. */
1639 static char *
1640 xg_get_file_name_from_chooser (GtkWidget *w)
1642 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1645 /* Callback called when the "Show hidden files" toggle is pressed.
1646 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1648 static void
1649 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1651 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1652 gboolean visible;
1653 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1654 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1658 /* Callback called when a property changes in a file chooser.
1659 GOBJECT is the file chooser dialog, ARG1 describes the property.
1660 USER_DATA is the toggle widget in the file chooser dialog.
1661 We use this to update the "Show hidden files" toggle when the user
1662 changes that property by right clicking in the file list. */
1664 static void
1665 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1667 if (strcmp (arg1->name, "show-hidden") == 0)
1669 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1670 gboolean visible, toggle_on;
1672 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1673 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1675 if (!!visible != !!toggle_on)
1677 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1678 G_CALLBACK (xg_toggle_visibility_cb),
1679 gobject);
1680 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1681 g_signal_handlers_unblock_by_func
1682 (G_OBJECT (wtoggle),
1683 G_CALLBACK (xg_toggle_visibility_cb),
1684 gobject);
1686 x_gtk_show_hidden_files = visible;
1690 /* Read a file name from the user using a file chooser dialog.
1691 F is the current frame.
1692 PROMPT is a prompt to show to the user. May not be NULL.
1693 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1694 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1695 file. *FUNC is set to a function that can be used to retrieve the
1696 selected file name from the returned widget.
1698 Returns the created widget. */
1700 static GtkWidget *
1701 xg_get_file_with_chooser (FRAME_PTR f,
1702 char *prompt,
1703 char *default_filename,
1704 int mustmatch_p, int only_dir_p,
1705 xg_get_file_func *func)
1707 char msgbuf[1024];
1709 GtkWidget *filewin, *wtoggle, *wbox, *wmessage IF_LINT (= NULL);
1710 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1711 GtkFileChooserAction action = (mustmatch_p ?
1712 GTK_FILE_CHOOSER_ACTION_OPEN :
1713 GTK_FILE_CHOOSER_ACTION_SAVE);
1715 if (only_dir_p)
1716 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1718 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1719 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1720 (mustmatch_p || only_dir_p ?
1721 GTK_STOCK_OPEN : GTK_STOCK_OK),
1722 GTK_RESPONSE_OK,
1723 NULL);
1724 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1726 wbox = gtk_vbox_new (FALSE, 0);
1727 gtk_widget_show (wbox);
1728 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1730 if (x_gtk_show_hidden_files)
1732 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1733 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1735 gtk_widget_show (wtoggle);
1736 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1737 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1738 g_signal_connect (G_OBJECT (filewin), "notify",
1739 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1741 if (x_gtk_file_dialog_help_text)
1743 msgbuf[0] = '\0';
1744 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1745 Show the C-l help text only for versions < 2.10. */
1746 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1747 strcat (msgbuf, "\nType C-l to display a file name text entry box.\n");
1748 strcat (msgbuf, "\nIf you don't like this file selector, use the "
1749 "corresponding\nkey binding or customize "
1750 "use-file-dialog to turn it off.");
1752 wmessage = gtk_label_new (msgbuf);
1753 gtk_widget_show (wmessage);
1756 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1757 if (x_gtk_file_dialog_help_text)
1758 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1759 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1761 if (default_filename)
1763 Lisp_Object file;
1764 struct gcpro gcpro1;
1765 char *utf8_filename;
1766 GCPRO1 (file);
1768 file = build_string (default_filename);
1770 /* File chooser does not understand ~/... in the file name. It must be
1771 an absolute name starting with /. */
1772 if (default_filename[0] != '/')
1773 file = Fexpand_file_name (file, Qnil);
1775 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1776 if (! NILP (Ffile_directory_p (file)))
1777 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1778 utf8_filename);
1779 else
1781 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1782 utf8_filename);
1783 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1785 char *cp = strrchr (utf8_filename, '/');
1786 if (cp) ++cp;
1787 else cp = utf8_filename;
1788 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1792 UNGCPRO;
1795 *func = xg_get_file_name_from_chooser;
1796 return filewin;
1799 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1801 /* Return the selected file for file selector dialog W.
1802 The returned string must be free:d. */
1804 static char *
1805 xg_get_file_name_from_selector (GtkWidget *w)
1807 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1808 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1811 /* Create a file selection dialog.
1812 F is the current frame.
1813 PROMPT is a prompt to show to the user. May not be NULL.
1814 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1815 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1816 file. *FUNC is set to a function that can be used to retrieve the
1817 selected file name from the returned widget.
1819 Returns the created widget. */
1821 static GtkWidget *
1822 xg_get_file_with_selection (FRAME_PTR f,
1823 char *prompt,
1824 char *default_filename,
1825 int mustmatch_p, int only_dir_p,
1826 xg_get_file_func *func)
1828 GtkWidget *filewin;
1829 GtkFileSelection *filesel;
1831 filewin = gtk_file_selection_new (prompt);
1832 filesel = GTK_FILE_SELECTION (filewin);
1834 if (default_filename)
1835 gtk_file_selection_set_filename (filesel, default_filename);
1837 if (mustmatch_p)
1839 /* The selection_entry part of filesel is not documented. */
1840 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1841 gtk_file_selection_hide_fileop_buttons (filesel);
1844 *func = xg_get_file_name_from_selector;
1846 return filewin;
1848 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1850 /* Read a file name from the user using a file dialog, either the old
1851 file selection dialog, or the new file chooser dialog. Which to use
1852 depends on what the GTK version used has, and what the value of
1853 gtk-use-old-file-dialog.
1854 F is the current frame.
1855 PROMPT is a prompt to show to the user. May not be NULL.
1856 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1857 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1858 file.
1860 Returns a file name or NULL if no file was selected.
1861 The returned string must be freed by the caller. */
1863 char *
1864 xg_get_file_name (FRAME_PTR f,
1865 char *prompt,
1866 char *default_filename,
1867 int mustmatch_p,
1868 int only_dir_p)
1870 GtkWidget *w = 0;
1871 char *fn = 0;
1872 int filesel_done = 0;
1873 xg_get_file_func func;
1875 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1876 /* I really don't know why this is needed, but without this the GLIBC add on
1877 library linuxthreads hangs when the Gnome file chooser backend creates
1878 threads. */
1879 sigblock (sigmask (__SIGRTMIN));
1880 #endif /* HAVE_GTK_AND_PTHREAD */
1882 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1884 if (xg_uses_old_file_dialog ())
1885 w = xg_get_file_with_selection (f, prompt, default_filename,
1886 mustmatch_p, only_dir_p, &func);
1887 else
1888 w = xg_get_file_with_chooser (f, prompt, default_filename,
1889 mustmatch_p, only_dir_p, &func);
1891 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1892 w = xg_get_file_with_chooser (f, prompt, default_filename,
1893 mustmatch_p, only_dir_p, &func);
1894 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1896 gtk_widget_set_name (w, "emacs-filedialog");
1898 filesel_done = xg_dialog_run (f, w);
1900 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1901 sigunblock (sigmask (__SIGRTMIN));
1902 #endif
1904 if (filesel_done == GTK_RESPONSE_OK)
1905 fn = (*func) (w);
1907 gtk_widget_destroy (w);
1908 return fn;
1911 #ifdef HAVE_FREETYPE
1912 /* Pop up a GTK font selector and return the name of the font the user
1913 selects, as a C string. The returned font name follows GTK's own
1914 format:
1916 `FAMILY [VALUE1 VALUE2] SIZE'
1918 This can be parsed using font_parse_fcname in font.c.
1919 DEFAULT_NAME, if non-zero, is the default font name. */
1921 char *
1922 xg_get_font_name (FRAME_PTR f, const char *default_name)
1924 GtkWidget *w;
1925 char *fontname = NULL;
1926 int done = 0;
1928 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1929 sigblock (sigmask (__SIGRTMIN));
1930 #endif /* HAVE_GTK_AND_PTHREAD */
1932 w = gtk_font_selection_dialog_new ("Pick a font");
1933 if (!default_name)
1934 default_name = "Monospace 10";
1935 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1936 default_name);
1938 gtk_widget_set_name (w, "emacs-fontdialog");
1940 done = xg_dialog_run (f, w);
1942 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1943 sigunblock (sigmask (__SIGRTMIN));
1944 #endif
1946 if (done == GTK_RESPONSE_OK)
1947 fontname = gtk_font_selection_dialog_get_font_name
1948 (GTK_FONT_SELECTION_DIALOG (w));
1950 gtk_widget_destroy (w);
1951 return fontname;
1953 #endif /* HAVE_FREETYPE */
1957 /***********************************************************************
1958 Menu functions.
1959 ***********************************************************************/
1961 /* The name of menu items that can be used for customization. Since GTK
1962 RC files are very crude and primitive, we have to set this on all
1963 menu item names so a user can easily customize menu items. */
1965 #define MENU_ITEM_NAME "emacs-menuitem"
1968 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1969 during GC. The next member points to the items. */
1970 static xg_list_node xg_menu_cb_list;
1972 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1973 during GC. The next member points to the items. */
1974 static xg_list_node xg_menu_item_cb_list;
1976 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1977 F is the frame CL_DATA will be initialized for.
1978 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1980 The menu bar and all sub menus under the menu bar in a frame
1981 share the same structure, hence the reference count.
1983 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1984 allocated xg_menu_cb_data if CL_DATA is NULL. */
1986 static xg_menu_cb_data *
1987 make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
1989 if (! cl_data)
1991 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1992 cl_data->f = f;
1993 cl_data->menu_bar_vector = f->menu_bar_vector;
1994 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1995 cl_data->highlight_cb = highlight_cb;
1996 cl_data->ref_count = 0;
1998 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
2001 cl_data->ref_count++;
2003 return cl_data;
2006 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
2007 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2009 When the menu bar is updated, menu items may have been added and/or
2010 removed, so menu_bar_vector and menu_bar_items_used change. We must
2011 then update CL_DATA since it is used to determine which menu
2012 item that is invoked in the menu.
2013 HIGHLIGHT_CB could change, there is no check that the same
2014 function is given when modifying a menu bar as was given when
2015 creating the menu bar. */
2017 static void
2018 update_cl_data (xg_menu_cb_data *cl_data,
2019 FRAME_PTR f,
2020 GCallback highlight_cb)
2022 if (cl_data)
2024 cl_data->f = f;
2025 cl_data->menu_bar_vector = f->menu_bar_vector;
2026 cl_data->menu_bar_items_used = f->menu_bar_items_used;
2027 cl_data->highlight_cb = highlight_cb;
2031 /* Decrease reference count for CL_DATA.
2032 If reference count is zero, free CL_DATA. */
2034 static void
2035 unref_cl_data (xg_menu_cb_data *cl_data)
2037 if (cl_data && cl_data->ref_count > 0)
2039 cl_data->ref_count--;
2040 if (cl_data->ref_count == 0)
2042 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
2043 xfree (cl_data);
2048 /* Function that marks all lisp data during GC. */
2050 void
2051 xg_mark_data (void)
2053 xg_list_node *iter;
2055 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
2056 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
2058 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
2060 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
2062 if (! NILP (cb_data->help))
2063 mark_object (cb_data->help);
2068 /* Callback called when a menu item is destroyed. Used to free data.
2069 W is the widget that is being destroyed (not used).
2070 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
2072 static void
2073 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
2075 if (client_data)
2077 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
2078 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
2079 xfree (data);
2083 /* Callback called when the pointer enters/leaves a menu item.
2084 W is the parent of the menu item.
2085 EVENT is either an enter event or leave event.
2086 CLIENT_DATA is not used.
2088 Returns FALSE to tell GTK to keep processing this event. */
2090 static gboolean
2091 menuitem_highlight_callback (GtkWidget *w,
2092 GdkEventCrossing *event,
2093 gpointer client_data)
2095 GdkEvent ev;
2096 GtkWidget *subwidget;
2097 xg_menu_item_cb_data *data;
2099 ev.crossing = *event;
2100 subwidget = gtk_get_event_widget (&ev);
2101 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
2102 XG_ITEM_DATA);
2103 if (data)
2105 if (! NILP (data->help) && data->cl_data->highlight_cb)
2107 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
2108 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
2109 (*func) (subwidget, call_data);
2113 return FALSE;
2116 /* Callback called when a menu is destroyed. Used to free data.
2117 W is the widget that is being destroyed (not used).
2118 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
2120 static void
2121 menu_destroy_callback (GtkWidget *w, gpointer client_data)
2123 unref_cl_data ((xg_menu_cb_data*) client_data);
2126 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
2127 must be non-NULL) and can be inserted into a menu item.
2129 Returns the GtkHBox. */
2131 static GtkWidget *
2132 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
2134 GtkWidget *wlbl;
2135 GtkWidget *wkey;
2136 GtkWidget *wbox;
2138 wbox = gtk_hbox_new (FALSE, 0);
2139 wlbl = gtk_label_new (utf8_label);
2140 wkey = gtk_label_new (utf8_key);
2142 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
2143 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
2145 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
2146 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
2148 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
2149 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
2150 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
2152 return wbox;
2155 /* Make and return a menu item widget with the key to the right.
2156 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
2157 UTF8_KEY is the text representing the key binding.
2158 ITEM is the widget_value describing the menu item.
2160 GROUP is an in/out parameter. If the menu item to be created is not
2161 part of any radio menu group, *GROUP contains NULL on entry and exit.
2162 If the menu item to be created is part of a radio menu group, on entry
2163 *GROUP contains the group to use, or NULL if this is the first item
2164 in the group. On exit, *GROUP contains the radio item group.
2166 Unfortunately, keys don't line up as nicely as in Motif,
2167 but the MacOS X version doesn't either, so I guess that is OK. */
2169 static GtkWidget *
2170 make_menu_item (const char *utf8_label,
2171 const char *utf8_key,
2172 widget_value *item,
2173 GSList **group)
2175 GtkWidget *w;
2176 GtkWidget *wtoadd = 0;
2178 /* It has been observed that some menu items have a NULL name field.
2179 This will lead to this function being called with a NULL utf8_label.
2180 GTK crashes on that so we set a blank label. Why there is a NULL
2181 name remains to be investigated. */
2182 if (! utf8_label) utf8_label = " ";
2184 if (utf8_key)
2185 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2187 if (item->button_type == BUTTON_TYPE_TOGGLE)
2189 *group = NULL;
2190 if (utf8_key) w = gtk_check_menu_item_new ();
2191 else w = gtk_check_menu_item_new_with_label (utf8_label);
2192 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2194 else if (item->button_type == BUTTON_TYPE_RADIO)
2196 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2197 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2198 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2199 if (item->selected)
2200 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2202 else
2204 *group = NULL;
2205 if (utf8_key) w = gtk_menu_item_new ();
2206 else w = gtk_menu_item_new_with_label (utf8_label);
2209 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2210 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2212 return w;
2215 static int xg_detached_menus;
2217 /* Returns non-zero if there are detached menus. */
2220 xg_have_tear_offs (void)
2222 return xg_detached_menus > 0;
2225 /* Callback invoked when a detached menu window is removed. Here we
2226 decrease the xg_detached_menus count.
2227 WIDGET is the top level window that is removed (the parent of the menu).
2228 CLIENT_DATA is not used. */
2230 static void
2231 tearoff_remove (GtkWidget *widget, gpointer client_data)
2233 if (xg_detached_menus > 0) --xg_detached_menus;
2236 /* Callback invoked when a menu is detached. It increases the
2237 xg_detached_menus count.
2238 WIDGET is the GtkTearoffMenuItem.
2239 CLIENT_DATA is not used. */
2241 static void
2242 tearoff_activate (GtkWidget *widget, gpointer client_data)
2244 GtkWidget *menu = gtk_widget_get_parent (widget);
2245 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2247 ++xg_detached_menus;
2248 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2249 "destroy",
2250 G_CALLBACK (tearoff_remove), 0);
2255 /* Create a menu item widget, and connect the callbacks.
2256 ITEM decribes the menu item.
2257 F is the frame the created menu belongs to.
2258 SELECT_CB is the callback to use when a menu item is selected.
2259 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2260 CL_DATA points to the callback data to be used for this menu.
2261 GROUP is an in/out parameter. If the menu item to be created is not
2262 part of any radio menu group, *GROUP contains NULL on entry and exit.
2263 If the menu item to be created is part of a radio menu group, on entry
2264 *GROUP contains the group to use, or NULL if this is the first item
2265 in the group. On exit, *GROUP contains the radio item group.
2267 Returns the created GtkWidget. */
2269 static GtkWidget *
2270 xg_create_one_menuitem (widget_value *item,
2271 FRAME_PTR f,
2272 GCallback select_cb,
2273 GCallback highlight_cb,
2274 xg_menu_cb_data *cl_data,
2275 GSList **group)
2277 char *utf8_label;
2278 char *utf8_key;
2279 GtkWidget *w;
2280 xg_menu_item_cb_data *cb_data;
2282 utf8_label = get_utf8_string (item->name);
2283 utf8_key = get_utf8_string (item->key);
2285 w = make_menu_item (utf8_label, utf8_key, item, group);
2287 if (utf8_label) g_free (utf8_label);
2288 if (utf8_key) g_free (utf8_key);
2290 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2292 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2294 cb_data->select_id = 0;
2295 cb_data->help = item->help;
2296 cb_data->cl_data = cl_data;
2297 cb_data->call_data = item->call_data;
2299 g_signal_connect (G_OBJECT (w),
2300 "destroy",
2301 G_CALLBACK (menuitem_destroy_callback),
2302 cb_data);
2304 /* Put cb_data in widget, so we can get at it when modifying menubar */
2305 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2307 /* final item, not a submenu */
2308 if (item->call_data && ! item->contents)
2310 if (select_cb)
2311 cb_data->select_id
2312 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2315 return w;
2318 /* Create a full menu tree specified by DATA.
2319 F is the frame the created menu belongs to.
2320 SELECT_CB is the callback to use when a menu item is selected.
2321 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2322 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2323 POP_UP_P is non-zero if we shall create a popup menu.
2324 MENU_BAR_P is non-zero if we shall create a menu bar.
2325 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2326 if MENU_BAR_P is non-zero.
2327 TOPMENU is the topmost GtkWidget that others shall be placed under.
2328 It may be NULL, in that case we create the appropriate widget
2329 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2330 CL_DATA is the callback data we shall use for this menu, or NULL
2331 if we haven't set the first callback yet.
2332 NAME is the name to give to the top level menu if this function
2333 creates it. May be NULL to not set any name.
2335 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2336 not NULL.
2338 This function calls itself to create submenus. */
2340 static GtkWidget *
2341 create_menus (widget_value *data,
2342 FRAME_PTR f,
2343 GCallback select_cb,
2344 GCallback deactivate_cb,
2345 GCallback highlight_cb,
2346 int pop_up_p,
2347 int menu_bar_p,
2348 int add_tearoff_p,
2349 GtkWidget *topmenu,
2350 xg_menu_cb_data *cl_data,
2351 const char *name)
2353 widget_value *item;
2354 GtkWidget *wmenu = topmenu;
2355 GSList *group = NULL;
2357 if (! topmenu)
2359 if (! menu_bar_p)
2361 wmenu = gtk_menu_new ();
2362 xg_set_screen (wmenu, f);
2363 /* Connect this to the menu instead of items so we get enter/leave for
2364 disabled items also. TODO: Still does not get enter/leave for
2365 disabled items in detached menus. */
2366 g_signal_connect (G_OBJECT (wmenu),
2367 "enter-notify-event",
2368 G_CALLBACK (menuitem_highlight_callback),
2369 NULL);
2370 g_signal_connect (G_OBJECT (wmenu),
2371 "leave-notify-event",
2372 G_CALLBACK (menuitem_highlight_callback),
2373 NULL);
2375 else
2377 wmenu = gtk_menu_bar_new ();
2378 /* Set width of menu bar to a small value so it doesn't enlarge
2379 a small initial frame size. The width will be set to the
2380 width of the frame later on when it is added to a container.
2381 height -1: Natural height. */
2382 gtk_widget_set_size_request (wmenu, 1, -1);
2385 /* Put cl_data on the top menu for easier access. */
2386 cl_data = make_cl_data (cl_data, f, highlight_cb);
2387 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2388 g_signal_connect (G_OBJECT (wmenu), "destroy",
2389 G_CALLBACK (menu_destroy_callback), cl_data);
2391 if (name)
2392 gtk_widget_set_name (wmenu, name);
2394 if (deactivate_cb)
2395 g_signal_connect (G_OBJECT (wmenu),
2396 "selection-done", deactivate_cb, 0);
2399 if (! menu_bar_p && add_tearoff_p)
2401 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2402 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2404 g_signal_connect (G_OBJECT (tearoff), "activate",
2405 G_CALLBACK (tearoff_activate), 0);
2408 for (item = data; item; item = item->next)
2410 GtkWidget *w;
2412 if (pop_up_p && !item->contents && !item->call_data
2413 && !menu_separator_name_p (item->name))
2415 char *utf8_label;
2416 /* A title for a popup. We do the same as GTK does when
2417 creating titles, but it does not look good. */
2418 group = NULL;
2419 utf8_label = get_utf8_string (item->name);
2421 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2422 w = gtk_menu_item_new_with_label (utf8_label);
2423 gtk_widget_set_sensitive (w, FALSE);
2424 if (utf8_label) g_free (utf8_label);
2426 else if (menu_separator_name_p (item->name))
2428 group = NULL;
2429 /* GTK only have one separator type. */
2430 w = gtk_separator_menu_item_new ();
2432 else
2434 w = xg_create_one_menuitem (item,
2436 item->contents ? 0 : select_cb,
2437 highlight_cb,
2438 cl_data,
2439 &group);
2441 /* Create a possibly empty submenu for menu bar items, since some
2442 themes don't highlight items correctly without it. */
2443 if (item->contents || menu_bar_p)
2445 GtkWidget *submenu = create_menus (item->contents,
2447 select_cb,
2448 deactivate_cb,
2449 highlight_cb,
2452 add_tearoff_p,
2454 cl_data,
2456 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2460 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2461 gtk_widget_set_name (w, MENU_ITEM_NAME);
2464 return wmenu;
2467 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2468 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2469 with some text and buttons.
2470 F is the frame the created item belongs to.
2471 NAME is the name to use for the top widget.
2472 VAL is a widget_value structure describing items to be created.
2473 SELECT_CB is the callback to use when a menu item is selected or
2474 a dialog button is pressed.
2475 DEACTIVATE_CB is the callback to use when an item is deactivated.
2476 For a menu, when a sub menu is not shown anymore, for a dialog it is
2477 called when the dialog is popped down.
2478 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2480 Returns the widget created. */
2482 GtkWidget *
2483 xg_create_widget (const char *type, const char *name, FRAME_PTR f, widget_value *val,
2484 GCallback select_cb, GCallback deactivate_cb,
2485 GCallback highlight_cb)
2487 GtkWidget *w = 0;
2488 int menu_bar_p = strcmp (type, "menubar") == 0;
2489 int pop_up_p = strcmp (type, "popup") == 0;
2491 if (strcmp (type, "dialog") == 0)
2493 w = create_dialog (val, select_cb, deactivate_cb);
2494 xg_set_screen (w, f);
2495 gtk_window_set_transient_for (GTK_WINDOW (w),
2496 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2497 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2498 gtk_widget_set_name (w, "emacs-dialog");
2499 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2501 else if (menu_bar_p || pop_up_p)
2503 w = create_menus (val->contents,
2505 select_cb,
2506 deactivate_cb,
2507 highlight_cb,
2508 pop_up_p,
2509 menu_bar_p,
2510 menu_bar_p,
2513 name);
2515 /* Set the cursor to an arrow for popup menus when they are mapped.
2516 This is done by default for menu bar menus. */
2517 if (pop_up_p)
2519 /* Must realize so the GdkWindow inside the widget is created. */
2520 gtk_widget_realize (w);
2521 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2524 else
2526 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2527 type);
2530 return w;
2533 /* Return the label for menu item WITEM. */
2535 static const char *
2536 xg_get_menu_item_label (GtkMenuItem *witem)
2538 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2539 return gtk_label_get_label (wlabel);
2542 /* Return non-zero if the menu item WITEM has the text LABEL. */
2544 static int
2545 xg_item_label_same_p (GtkMenuItem *witem, const char *label)
2547 int is_same = 0;
2548 char *utf8_label = get_utf8_string (label);
2549 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2551 if (! old_label && ! utf8_label)
2552 is_same = 1;
2553 else if (old_label && utf8_label)
2554 is_same = strcmp (utf8_label, old_label) == 0;
2556 if (utf8_label) g_free (utf8_label);
2558 return is_same;
2561 /* Destroy widgets in LIST. */
2563 static void
2564 xg_destroy_widgets (GList *list)
2566 GList *iter;
2568 for (iter = list; iter; iter = g_list_next (iter))
2570 GtkWidget *w = GTK_WIDGET (iter->data);
2572 /* Destroying the widget will remove it from the container it is in. */
2573 gtk_widget_destroy (w);
2577 /* Update the top level names in MENUBAR (i.e. not submenus).
2578 F is the frame the menu bar belongs to.
2579 *LIST is a list with the current menu bar names (menu item widgets).
2580 ITER is the item within *LIST that shall be updated.
2581 POS is the numerical position, starting at 0, of ITER in *LIST.
2582 VAL describes what the menu bar shall look like after the update.
2583 SELECT_CB is the callback to use when a menu item is selected.
2584 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2585 CL_DATA points to the callback data to be used for this menu bar.
2587 This function calls itself to walk through the menu bar names. */
2589 static void
2590 xg_update_menubar (GtkWidget *menubar,
2591 FRAME_PTR f,
2592 GList **list,
2593 GList *iter,
2594 int pos,
2595 widget_value *val,
2596 GCallback select_cb,
2597 GCallback deactivate_cb,
2598 GCallback highlight_cb,
2599 xg_menu_cb_data *cl_data)
2601 if (! iter && ! val)
2602 return;
2603 else if (iter && ! val)
2605 /* Item(s) have been removed. Remove all remaining items. */
2606 xg_destroy_widgets (iter);
2608 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2609 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2610 gtk_menu_item_new_with_label (""),
2612 /* All updated. */
2613 val = 0;
2614 iter = 0;
2616 else if (! iter && val)
2618 /* Item(s) added. Add all new items in one call. */
2619 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2620 0, 1, 0, menubar, cl_data, 0);
2622 /* All updated. */
2623 val = 0;
2624 iter = 0;
2626 /* Below this neither iter or val is NULL */
2627 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2629 /* This item is still the same, check next item. */
2630 val = val->next;
2631 iter = g_list_next (iter);
2632 ++pos;
2634 else /* This item is changed. */
2636 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2637 GtkMenuItem *witem2 = 0;
2638 int val_in_menubar = 0;
2639 int iter_in_new_menubar = 0;
2640 GList *iter2;
2641 widget_value *cur;
2643 /* See if the changed entry (val) is present later in the menu bar */
2644 for (iter2 = iter;
2645 iter2 && ! val_in_menubar;
2646 iter2 = g_list_next (iter2))
2648 witem2 = GTK_MENU_ITEM (iter2->data);
2649 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2652 /* See if the current entry (iter) is present later in the
2653 specification for the new menu bar. */
2654 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2655 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2657 if (val_in_menubar && ! iter_in_new_menubar)
2659 int nr = pos;
2661 /* This corresponds to:
2662 Current: A B C
2663 New: A C
2664 Remove B. */
2666 g_object_ref (G_OBJECT (witem));
2667 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2668 gtk_widget_destroy (GTK_WIDGET (witem));
2670 /* Must get new list since the old changed. */
2671 g_list_free (*list);
2672 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2673 while (nr-- > 0) iter = g_list_next (iter);
2675 else if (! val_in_menubar && ! iter_in_new_menubar)
2677 /* This corresponds to:
2678 Current: A B C
2679 New: A X C
2680 Rename B to X. This might seem to be a strange thing to do,
2681 since if there is a menu under B it will be totally wrong for X.
2682 But consider editing a C file. Then there is a C-mode menu
2683 (corresponds to B above).
2684 If then doing C-x C-f the minibuf menu (X above) replaces the
2685 C-mode menu. When returning from the minibuffer, we get
2686 back the C-mode menu. Thus we do:
2687 Rename B to X (C-mode to minibuf menu)
2688 Rename X to B (minibuf to C-mode menu).
2689 If the X menu hasn't been invoked, the menu under B
2690 is up to date when leaving the minibuffer. */
2691 GtkLabel *wlabel = GTK_LABEL (XG_BIN_CHILD (witem));
2692 char *utf8_label = get_utf8_string (val->name);
2693 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2695 gtk_label_set_text (wlabel, utf8_label);
2697 /* If this item has a submenu that has been detached, change
2698 the title in the WM decorations also. */
2699 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2700 /* Set the title of the detached window. */
2701 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2703 if (utf8_label) g_free (utf8_label);
2704 iter = g_list_next (iter);
2705 val = val->next;
2706 ++pos;
2708 else if (! val_in_menubar && iter_in_new_menubar)
2710 /* This corresponds to:
2711 Current: A B C
2712 New: A X B C
2713 Insert X. */
2715 int nr = pos;
2716 GSList *group = 0;
2717 GtkWidget *w = xg_create_one_menuitem (val,
2719 select_cb,
2720 highlight_cb,
2721 cl_data,
2722 &group);
2724 /* Create a possibly empty submenu for menu bar items, since some
2725 themes don't highlight items correctly without it. */
2726 GtkWidget *submenu = create_menus (NULL, f,
2727 select_cb, deactivate_cb,
2728 highlight_cb,
2729 0, 0, 0, 0, cl_data, 0);
2730 gtk_widget_set_name (w, MENU_ITEM_NAME);
2731 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2732 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2734 g_list_free (*list);
2735 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2736 while (nr-- > 0) iter = g_list_next (iter);
2737 iter = g_list_next (iter);
2738 val = val->next;
2739 ++pos;
2741 else /* if (val_in_menubar && iter_in_new_menubar) */
2743 int nr = pos;
2744 /* This corresponds to:
2745 Current: A B C
2746 New: A C B
2747 Move C before B */
2749 g_object_ref (G_OBJECT (witem2));
2750 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2751 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2752 GTK_WIDGET (witem2), pos);
2753 g_object_unref (G_OBJECT (witem2));
2755 g_list_free (*list);
2756 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2757 while (nr-- > 0) iter = g_list_next (iter);
2758 if (iter) iter = g_list_next (iter);
2759 val = val->next;
2760 ++pos;
2764 /* Update the rest of the menu bar. */
2765 xg_update_menubar (menubar, f, list, iter, pos, val,
2766 select_cb, deactivate_cb, highlight_cb, cl_data);
2769 /* Update the menu item W so it corresponds to VAL.
2770 SELECT_CB is the callback to use when a menu item is selected.
2771 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2772 CL_DATA is the data to set in the widget for menu invocation. */
2774 static void
2775 xg_update_menu_item (widget_value *val,
2776 GtkWidget *w,
2777 GCallback select_cb,
2778 GCallback highlight_cb,
2779 xg_menu_cb_data *cl_data)
2781 GtkWidget *wchild;
2782 GtkLabel *wlbl = 0;
2783 GtkLabel *wkey = 0;
2784 char *utf8_label;
2785 char *utf8_key;
2786 const char *old_label = 0;
2787 const char *old_key = 0;
2788 xg_menu_item_cb_data *cb_data;
2790 wchild = XG_BIN_CHILD (w);
2791 utf8_label = get_utf8_string (val->name);
2792 utf8_key = get_utf8_string (val->key);
2794 /* See if W is a menu item with a key. See make_menu_item above. */
2795 if (GTK_IS_HBOX (wchild))
2797 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2799 wlbl = GTK_LABEL (list->data);
2800 wkey = GTK_LABEL (list->next->data);
2801 g_list_free (list);
2803 if (! utf8_key)
2805 /* Remove the key and keep just the label. */
2806 g_object_ref (G_OBJECT (wlbl));
2807 gtk_container_remove (GTK_CONTAINER (w), wchild);
2808 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2809 g_object_unref (G_OBJECT (wlbl));
2810 wkey = 0;
2814 else /* Just a label. */
2816 wlbl = GTK_LABEL (wchild);
2818 /* Check if there is now a key. */
2819 if (utf8_key)
2821 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2822 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2824 wlbl = GTK_LABEL (list->data);
2825 wkey = GTK_LABEL (list->next->data);
2826 g_list_free (list);
2828 gtk_container_remove (GTK_CONTAINER (w), wchild);
2829 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2834 if (wkey) old_key = gtk_label_get_label (wkey);
2835 if (wlbl) old_label = gtk_label_get_label (wlbl);
2837 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2838 gtk_label_set_text (wkey, utf8_key);
2840 if (! old_label || strcmp (utf8_label, old_label) != 0)
2841 gtk_label_set_text (wlbl, utf8_label);
2843 if (utf8_key) g_free (utf8_key);
2844 if (utf8_label) g_free (utf8_label);
2846 if (! val->enabled && gtk_widget_get_sensitive (w))
2847 gtk_widget_set_sensitive (w, FALSE);
2848 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2849 gtk_widget_set_sensitive (w, TRUE);
2851 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2852 XG_ITEM_DATA);
2853 if (cb_data)
2855 cb_data->call_data = val->call_data;
2856 cb_data->help = val->help;
2857 cb_data->cl_data = cl_data;
2859 /* We assume the callback functions don't change. */
2860 if (val->call_data && ! val->contents)
2862 /* This item shall have a select callback. */
2863 if (! cb_data->select_id)
2864 cb_data->select_id
2865 = g_signal_connect (G_OBJECT (w), "activate",
2866 select_cb, cb_data);
2868 else if (cb_data->select_id)
2870 g_signal_handler_disconnect (w, cb_data->select_id);
2871 cb_data->select_id = 0;
2876 /* Update the toggle menu item W so it corresponds to VAL. */
2878 static void
2879 xg_update_toggle_item (widget_value *val, GtkWidget *w)
2881 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2884 /* Update the radio menu item W so it corresponds to VAL. */
2886 static void
2887 xg_update_radio_item (widget_value *val, GtkWidget *w)
2889 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2892 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2893 SUBMENU may be NULL, in that case a new menu is created.
2894 F is the frame the menu bar belongs to.
2895 VAL describes the contents of the menu bar.
2896 SELECT_CB is the callback to use when a menu item is selected.
2897 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2898 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2899 CL_DATA is the call back data to use for any newly created items.
2901 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2902 was NULL. */
2904 static GtkWidget *
2905 xg_update_submenu (GtkWidget *submenu,
2906 FRAME_PTR f,
2907 widget_value *val,
2908 GCallback select_cb,
2909 GCallback deactivate_cb,
2910 GCallback highlight_cb,
2911 xg_menu_cb_data *cl_data)
2913 GtkWidget *newsub = submenu;
2914 GList *list = 0;
2915 GList *iter;
2916 widget_value *cur;
2917 int has_tearoff_p = 0;
2918 GList *first_radio = 0;
2920 if (submenu)
2921 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2923 for (cur = val, iter = list;
2924 cur && iter;
2925 iter = g_list_next (iter), cur = cur->next)
2927 GtkWidget *w = GTK_WIDGET (iter->data);
2929 /* Skip tearoff items, they have no counterpart in val. */
2930 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2932 has_tearoff_p = 1;
2933 iter = g_list_next (iter);
2934 if (iter) w = GTK_WIDGET (iter->data);
2935 else break;
2938 /* Remember first radio button in a group. If we get a mismatch in
2939 a radio group we must rebuild the whole group so that the connections
2940 in GTK becomes correct. */
2941 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2942 first_radio = iter;
2943 else if (cur->button_type != BUTTON_TYPE_RADIO
2944 && ! GTK_IS_RADIO_MENU_ITEM (w))
2945 first_radio = 0;
2947 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2949 if (! menu_separator_name_p (cur->name))
2950 break;
2952 else if (GTK_IS_CHECK_MENU_ITEM (w))
2954 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2955 break;
2956 xg_update_toggle_item (cur, w);
2957 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2959 else if (GTK_IS_RADIO_MENU_ITEM (w))
2961 if (cur->button_type != BUTTON_TYPE_RADIO)
2962 break;
2963 xg_update_radio_item (cur, w);
2964 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2966 else if (GTK_IS_MENU_ITEM (w))
2968 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2969 GtkWidget *sub;
2971 if (cur->button_type != BUTTON_TYPE_NONE ||
2972 menu_separator_name_p (cur->name))
2973 break;
2975 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2977 sub = gtk_menu_item_get_submenu (witem);
2978 if (sub && ! cur->contents)
2980 /* Not a submenu anymore. */
2981 g_object_ref (G_OBJECT (sub));
2982 remove_submenu (witem);
2983 gtk_widget_destroy (sub);
2985 else if (cur->contents)
2987 GtkWidget *nsub;
2989 nsub = xg_update_submenu (sub, f, cur->contents,
2990 select_cb, deactivate_cb,
2991 highlight_cb, cl_data);
2993 /* If this item just became a submenu, we must set it. */
2994 if (nsub != sub)
2995 gtk_menu_item_set_submenu (witem, nsub);
2998 else
3000 /* Structural difference. Remove everything from here and down
3001 in SUBMENU. */
3002 break;
3006 /* Remove widgets from first structual change. */
3007 if (iter)
3009 /* If we are adding new menu items below, we must remove from
3010 first radio button so that radio groups become correct. */
3011 if (cur && first_radio) xg_destroy_widgets (first_radio);
3012 else xg_destroy_widgets (iter);
3015 if (cur)
3017 /* More items added. Create them. */
3018 newsub = create_menus (cur,
3020 select_cb,
3021 deactivate_cb,
3022 highlight_cb,
3025 ! has_tearoff_p,
3026 submenu,
3027 cl_data,
3031 if (list) g_list_free (list);
3033 return newsub;
3036 /* Update the MENUBAR.
3037 F is the frame the menu bar belongs to.
3038 VAL describes the contents of the menu bar.
3039 If DEEP_P is non-zero, rebuild all but the top level menu names in
3040 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
3041 SELECT_CB is the callback to use when a menu item is selected.
3042 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
3043 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
3045 void
3046 xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
3047 int deep_p,
3048 GCallback select_cb, GCallback deactivate_cb,
3049 GCallback highlight_cb)
3051 xg_menu_cb_data *cl_data;
3052 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
3054 if (! list) return;
3056 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
3057 XG_FRAME_DATA);
3059 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
3060 select_cb, deactivate_cb, highlight_cb, cl_data);
3062 if (deep_p)
3064 widget_value *cur;
3066 /* Update all sub menus.
3067 We must keep the submenus (GTK menu item widgets) since the
3068 X Window in the XEvent that activates the menu are those widgets. */
3070 /* Update cl_data, menu_item things in F may have changed. */
3071 update_cl_data (cl_data, f, highlight_cb);
3073 for (cur = val->contents; cur; cur = cur->next)
3075 GList *iter;
3076 GtkWidget *sub = 0;
3077 GtkWidget *newsub;
3078 GtkMenuItem *witem = 0;
3080 /* Find sub menu that corresponds to val and update it. */
3081 for (iter = list ; iter; iter = g_list_next (iter))
3083 witem = GTK_MENU_ITEM (iter->data);
3084 if (xg_item_label_same_p (witem, cur->name))
3086 sub = gtk_menu_item_get_submenu (witem);
3087 break;
3091 newsub = xg_update_submenu (sub,
3093 cur->contents,
3094 select_cb,
3095 deactivate_cb,
3096 highlight_cb,
3097 cl_data);
3098 /* sub may still be NULL. If we just updated non deep and added
3099 a new menu bar item, it has no sub menu yet. So we set the
3100 newly created sub menu under witem. */
3101 if (newsub != sub && witem != 0)
3103 xg_set_screen (newsub, f);
3104 gtk_menu_item_set_submenu (witem, newsub);
3109 g_list_free (list);
3110 gtk_widget_show_all (menubar);
3113 /* Callback called when the menu bar W is mapped.
3114 Used to find the height of the menu bar if we didn't get it
3115 after showing the widget. */
3117 static void
3118 menubar_map_cb (GtkWidget *w, gpointer user_data)
3120 GtkRequisition req;
3121 FRAME_PTR f = (FRAME_PTR) user_data;
3122 gtk_widget_get_preferred_size (w, NULL, &req);
3123 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3125 FRAME_MENUBAR_HEIGHT (f) = req.height;
3126 xg_height_or_width_changed (f);
3130 /* Recompute all the widgets of frame F, when the menu bar has been
3131 changed. Value is non-zero if widgets were updated. */
3134 xg_update_frame_menubar (FRAME_PTR f)
3136 struct x_output *x = f->output_data.x;
3137 GtkRequisition req;
3139 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3140 return 0;
3142 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3143 return 0; /* Already done this, happens for frames created invisible. */
3145 BLOCK_INPUT;
3147 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3148 FALSE, FALSE, 0);
3149 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3151 g_signal_connect (x->menubar_widget, "map", G_CALLBACK (menubar_map_cb), f);
3152 gtk_widget_show_all (x->menubar_widget);
3153 gtk_widget_get_preferred_size (x->menubar_widget, NULL, &req);
3155 /* If menu bar doesn't know its height yet, cheat a little so the frame
3156 doesn't jump so much when resized later in menubar_map_cb. */
3157 if (req.height == 0)
3158 req.height = 23;
3160 if (FRAME_MENUBAR_HEIGHT (f) != req.height)
3162 FRAME_MENUBAR_HEIGHT (f) = req.height;
3163 xg_height_or_width_changed (f);
3165 UNBLOCK_INPUT;
3167 return 1;
3170 /* Get rid of the menu bar of frame F, and free its storage.
3171 This is used when deleting a frame, and when turning off the menu bar. */
3173 void
3174 free_frame_menubar (FRAME_PTR f)
3176 struct x_output *x = f->output_data.x;
3178 if (x->menubar_widget)
3180 BLOCK_INPUT;
3182 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3183 /* The menubar and its children shall be deleted when removed from
3184 the container. */
3185 x->menubar_widget = 0;
3186 FRAME_MENUBAR_HEIGHT (f) = 0;
3187 xg_height_or_width_changed (f);
3188 UNBLOCK_INPUT;
3193 xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
3195 struct x_output *x = f->output_data.x;
3196 GList *iter;
3197 GdkRectangle rec;
3198 GList *list;
3199 GdkDisplay *gdpy;
3200 GdkWindow *gw;
3201 GdkEvent gevent;
3202 GtkWidget *gwdesc;
3204 if (! x->menubar_widget) return 0;
3206 if (! (event->xbutton.x >= 0
3207 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3208 && event->xbutton.y >= 0
3209 && event->xbutton.y < f->output_data.x->menubar_height
3210 && event->xbutton.same_screen))
3211 return 0;
3213 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3214 gw = gdk_x11_window_lookup_for_display (gdpy, event->xbutton.window);
3215 if (! gw) return 0;
3216 gevent.any.window = gw;
3217 gwdesc = gtk_get_event_widget (&gevent);
3218 if (! gwdesc) return 0;
3219 if (! GTK_IS_MENU_BAR (gwdesc)
3220 && ! GTK_IS_MENU_ITEM (gwdesc)
3221 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3222 return 0;
3224 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3225 if (! list) return 0;
3226 rec.x = event->xbutton.x;
3227 rec.y = event->xbutton.y;
3228 rec.width = 1;
3229 rec.height = 1;
3231 for (iter = list ; iter; iter = g_list_next (iter))
3233 GtkWidget *w = GTK_WIDGET (iter->data);
3234 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3235 break;
3237 g_list_free (list);
3238 return iter == 0 ? 0 : 1;
3243 /***********************************************************************
3244 Scroll bar functions
3245 ***********************************************************************/
3248 /* Setting scroll bar values invokes the callback. Use this variable
3249 to indicate that callback should do nothing. */
3251 int xg_ignore_gtk_scrollbar;
3253 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3254 may be larger than 32 bits. Keep a mapping from integer index to widget
3255 pointers to get around the 32 bit limitation. */
3257 static struct
3259 GtkWidget **widgets;
3260 int max_size;
3261 int used;
3262 } id_to_widget;
3264 /* Grow this much every time we need to allocate more */
3266 #define ID_TO_WIDGET_INCR 32
3268 /* Store the widget pointer W in id_to_widget and return the integer index. */
3270 static int
3271 xg_store_widget_in_map (GtkWidget *w)
3273 int i;
3275 if (id_to_widget.max_size == id_to_widget.used)
3277 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3279 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3280 sizeof (GtkWidget *)*new_size);
3282 for (i = id_to_widget.max_size; i < new_size; ++i)
3283 id_to_widget.widgets[i] = 0;
3284 id_to_widget.max_size = new_size;
3287 /* Just loop over the array and find a free place. After all,
3288 how many scroll bars are we creating? Should be a small number.
3289 The check above guarantees we will find a free place. */
3290 for (i = 0; i < id_to_widget.max_size; ++i)
3292 if (! id_to_widget.widgets[i])
3294 id_to_widget.widgets[i] = w;
3295 ++id_to_widget.used;
3297 return i;
3301 /* Should never end up here */
3302 abort ();
3305 /* Remove pointer at IDX from id_to_widget.
3306 Called when scroll bar is destroyed. */
3308 static void
3309 xg_remove_widget_from_map (int idx)
3311 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3313 id_to_widget.widgets[idx] = 0;
3314 --id_to_widget.used;
3318 /* Get the widget pointer at IDX from id_to_widget. */
3320 static GtkWidget *
3321 xg_get_widget_from_map (int idx)
3323 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3324 return id_to_widget.widgets[idx];
3326 return 0;
3329 /* Return the scrollbar id for X Window WID on display DPY.
3330 Return -1 if WID not in id_to_widget. */
3333 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3335 int idx;
3336 GtkWidget *w;
3338 w = xg_win_to_widget (dpy, wid);
3340 if (w)
3342 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3343 if (id_to_widget.widgets[idx] == w)
3344 return idx;
3347 return -1;
3350 /* Callback invoked when scroll bar WIDGET is destroyed.
3351 DATA is the index into id_to_widget for WIDGET.
3352 We free pointer to last scroll bar values here and remove the index. */
3354 static void
3355 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3357 int id = (intptr_t) data;
3358 xg_remove_widget_from_map (id);
3361 /* Create a scroll bar widget for frame F. Store the scroll bar
3362 in BAR.
3363 SCROLL_CALLBACK is the callback to invoke when the value of the
3364 bar changes.
3365 END_CALLBACK is the callback to invoke when scrolling ends.
3366 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3367 to set resources for the widget. */
3369 void
3370 xg_create_scroll_bar (FRAME_PTR f,
3371 struct scroll_bar *bar,
3372 GCallback scroll_callback,
3373 GCallback end_callback,
3374 const char *scroll_bar_name)
3376 GtkWidget *wscroll;
3377 GtkWidget *webox;
3378 intptr_t scroll_id;
3379 #ifdef HAVE_GTK3
3380 GtkAdjustment *vadj;
3381 #else
3382 GtkObject *vadj;
3383 #endif
3385 /* Page, step increment values are not so important here, they
3386 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3387 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3388 0.1, 0.1, 0.1);
3390 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3391 webox = gtk_event_box_new ();
3392 gtk_widget_set_name (wscroll, scroll_bar_name);
3393 #ifndef HAVE_GTK3
3394 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3395 #endif
3396 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3398 scroll_id = xg_store_widget_in_map (wscroll);
3400 g_signal_connect (G_OBJECT (wscroll),
3401 "destroy",
3402 G_CALLBACK (xg_gtk_scroll_destroy),
3403 (gpointer) scroll_id);
3404 g_signal_connect (G_OBJECT (wscroll),
3405 "change-value",
3406 scroll_callback,
3407 (gpointer) bar);
3408 g_signal_connect (G_OBJECT (wscroll),
3409 "button-release-event",
3410 end_callback,
3411 (gpointer) bar);
3413 /* The scroll bar widget does not draw on a window of its own. Instead
3414 it draws on the parent window, in this case the edit widget. So
3415 whenever the edit widget is cleared, the scroll bar needs to redraw
3416 also, which causes flicker. Put an event box between the edit widget
3417 and the scroll bar, so the scroll bar instead draws itself on the
3418 event box window. */
3419 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3420 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3423 /* Set the cursor to an arrow. */
3424 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3426 bar->x_window = scroll_id;
3429 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3431 void
3432 xg_remove_scroll_bar (FRAME_PTR f, int scrollbar_id)
3434 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3435 if (w)
3437 GtkWidget *wparent = gtk_widget_get_parent (w);
3438 gtk_widget_destroy (w);
3439 gtk_widget_destroy (wparent);
3440 SET_FRAME_GARBAGED (f);
3444 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3445 in frame F.
3446 TOP/LEFT are the new pixel positions where the bar shall appear.
3447 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3449 void
3450 xg_update_scrollbar_pos (FRAME_PTR f,
3451 int scrollbar_id,
3452 int top,
3453 int left,
3454 int width,
3455 int height)
3458 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3460 if (wscroll)
3462 GtkWidget *wfixed = f->output_data.x->edit_widget;
3463 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3464 gint msl;
3466 /* Clear out old position. */
3467 int oldx = -1, oldy = -1, oldw, oldh;
3468 if (gtk_widget_get_parent (wparent) == wfixed)
3470 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3471 "x", &oldx, "y", &oldy, NULL);
3472 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3475 /* Move and resize to new values. */
3476 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3477 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3478 if (msl > height)
3480 /* No room. Hide scroll bar as some themes output a warning if
3481 the height is less than the min size. */
3482 gtk_widget_hide (wparent);
3483 gtk_widget_hide (wscroll);
3485 else
3487 gtk_widget_show_all (wparent);
3488 gtk_widget_set_size_request (wscroll, width, height);
3490 gtk_widget_queue_draw (wfixed);
3491 gdk_window_process_all_updates ();
3492 if (oldx != -1 && oldw > 0 && oldh > 0)
3494 /* Clear under old scroll bar position. This must be done after
3495 the gtk_widget_queue_draw and gdk_window_process_all_updates
3496 above. */
3497 x_clear_area (FRAME_X_DISPLAY (f),
3498 FRAME_X_WINDOW (f),
3499 oldx, oldy, oldw, oldh, 0);
3502 /* GTK does not redraw until the main loop is entered again, but
3503 if there are no X events pending we will not enter it. So we sync
3504 here to get some events. */
3506 x_sync (f);
3507 SET_FRAME_GARBAGED (f);
3508 cancel_mouse_face (f);
3512 /* Set the thumb size and position of scroll bar BAR. We are currently
3513 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3515 void
3516 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3517 int portion,
3518 int position,
3519 int whole)
3521 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3523 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3525 if (wscroll && NILP (bar->dragging))
3527 GtkAdjustment *adj;
3528 gdouble shown;
3529 gdouble top;
3530 int size, value;
3531 int old_size;
3532 int new_step;
3533 int changed = 0;
3535 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3537 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3538 rather than the real portion value. This makes the thumb less likely
3539 to resize and that looks better. */
3540 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3541 /* When the thumb is at the bottom, position == whole.
3542 So we need to increase `whole' to make space for the thumb. */
3543 whole += portion;
3545 if (whole <= 0)
3546 top = 0, shown = 1;
3547 else
3549 top = (gdouble) position / whole;
3550 shown = (gdouble) portion / whole;
3553 size = shown * XG_SB_RANGE;
3554 size = min (size, XG_SB_RANGE);
3555 size = max (size, 1);
3557 value = top * XG_SB_RANGE;
3558 value = min (value, XG_SB_MAX - size);
3559 value = max (value, XG_SB_MIN);
3561 /* Assume all lines are of equal size. */
3562 new_step = size / max (1, FRAME_LINES (f));
3564 old_size = gtk_adjustment_get_page_size (adj);
3565 if (old_size != size)
3567 int old_step = gtk_adjustment_get_step_increment (adj);
3568 if (old_step != new_step)
3570 gtk_adjustment_set_page_size (adj, size);
3571 gtk_adjustment_set_step_increment (adj, new_step);
3572 /* Assume a page increment is about 95% of the page size */
3573 gtk_adjustment_set_page_increment (adj,(int) (0.95*size));
3574 changed = 1;
3578 if (changed || int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3580 BLOCK_INPUT;
3582 /* gtk_range_set_value invokes the callback. Set
3583 ignore_gtk_scrollbar to make the callback do nothing */
3584 xg_ignore_gtk_scrollbar = 1;
3586 if (int_gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3587 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3588 else if (changed)
3589 gtk_adjustment_changed (adj);
3591 xg_ignore_gtk_scrollbar = 0;
3593 UNBLOCK_INPUT;
3598 /* Return non-zero if EVENT is for a scroll bar in frame F.
3599 When the same X window is used for several Gtk+ widgets, we cannot
3600 say for sure based on the X window alone if an event is for the
3601 frame. This function does additional checks.
3603 Return non-zero if the event is for a scroll bar, zero otherwise. */
3606 xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
3608 int retval = 0;
3610 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3612 /* Check if press occurred outside the edit widget. */
3613 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3614 retval = gdk_display_get_window_at_pointer (gdpy, NULL, NULL)
3615 != gtk_widget_get_window (f->output_data.x->edit_widget);
3617 else if (f
3618 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3619 || event->type == MotionNotify))
3621 /* If we are releasing or moving the scroll bar, it has the grab. */
3622 GtkWidget *w = gtk_grab_get_current ();
3623 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3626 return retval;
3631 /***********************************************************************
3632 Tool bar functions
3633 ***********************************************************************/
3634 /* The key for the data we put in the GtkImage widgets. The data is
3635 the image used by Emacs. We use this to see if we need to update
3636 the GtkImage with a new image. */
3637 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3639 /* The key for storing the latest modifiers so the activate callback can
3640 get them. */
3641 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3643 /* The key for storing the button widget in its proxy menu item. */
3644 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3646 /* The key for the data we put in the GtkImage widgets. The data is
3647 the stock name used by Emacs. We use this to see if we need to update
3648 the GtkImage with a new image. */
3649 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3651 /* As above, but this is used for named theme widgets, as opposed to
3652 stock items. */
3653 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3655 /* Callback function invoked when a tool bar item is pressed.
3656 W is the button widget in the tool bar that got pressed,
3657 CLIENT_DATA is an integer that is the index of the button in the
3658 tool bar. 0 is the first button. */
3660 static gboolean
3661 xg_tool_bar_button_cb (GtkWidget *widget,
3662 GdkEventButton *event,
3663 gpointer user_data)
3665 intptr_t state = event->state;
3666 gpointer ptr = (gpointer) state;
3667 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3668 return FALSE;
3672 /* Callback function invoked when a tool bar item is pressed.
3673 W is the button widget in the tool bar that got pressed,
3674 CLIENT_DATA is an integer that is the index of the button in the
3675 tool bar. 0 is the first button. */
3677 static void
3678 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3680 intptr_t idx = (intptr_t) client_data;
3681 gpointer gmod = g_object_get_data (G_OBJECT (w), XG_TOOL_BAR_LAST_MODIFIER);
3682 intptr_t mod = (intptr_t) gmod;
3684 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3685 Lisp_Object key, frame;
3686 struct input_event event;
3687 EVENT_INIT (event);
3689 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3690 return;
3692 idx *= TOOL_BAR_ITEM_NSLOTS;
3694 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3695 XSETFRAME (frame, f);
3697 /* We generate two events here. The first one is to set the prefix
3698 to `(tool_bar)', see keyboard.c. */
3699 event.kind = TOOL_BAR_EVENT;
3700 event.frame_or_window = frame;
3701 event.arg = frame;
3702 kbd_buffer_store_event (&event);
3704 event.kind = TOOL_BAR_EVENT;
3705 event.frame_or_window = frame;
3706 event.arg = key;
3707 /* Convert between the modifier bits GDK uses and the modifier bits
3708 Emacs uses. This assumes GDK and X masks are the same, which they are when
3709 this is written. */
3710 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3711 kbd_buffer_store_event (&event);
3713 /* Return focus to the frame after we have clicked on a detached
3714 tool bar button. */
3715 Fx_focus_frame (frame);
3718 /* Callback function invoked when a tool bar item is pressed in a detached
3719 tool bar or the overflow drop down menu.
3720 We just call xg_tool_bar_callback.
3721 W is the menu item widget that got pressed,
3722 CLIENT_DATA is an integer that is the index of the button in the
3723 tool bar. 0 is the first button. */
3725 static void
3726 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3728 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3729 XG_TOOL_BAR_PROXY_BUTTON));
3730 xg_tool_bar_callback (wbutton, client_data);
3734 static gboolean
3735 xg_tool_bar_help_callback (GtkWidget *w,
3736 GdkEventCrossing *event,
3737 gpointer client_data);
3739 /* This callback is called when a help is to be shown for an item in
3740 the detached tool bar when the detached tool bar it is not expanded. */
3742 static gboolean
3743 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3744 GdkEventCrossing *event,
3745 gpointer client_data)
3747 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3748 XG_TOOL_BAR_PROXY_BUTTON));
3750 return xg_tool_bar_help_callback (wbutton, event, client_data);
3753 static GtkWidget *
3754 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3756 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3757 GtkWidget *c1 = (GtkWidget *) clist->data;
3758 GtkWidget *c2 = clist->next ? (GtkWidget *) clist->next->data : NULL;
3760 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3761 g_list_free (clist);
3762 return GTK_IS_LABEL (c1) ? c1 : c2;
3766 /* This callback is called when a tool item should create a proxy item,
3767 such as for the overflow menu. Also called when the tool bar is detached.
3768 If we don't create a proxy menu item, the detached tool bar will be
3769 blank. */
3771 static gboolean
3772 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3774 GtkButton *wbutton = GTK_BUTTON (XG_BIN_CHILD (XG_BIN_CHILD (toolitem)));
3775 GtkWidget *vb = XG_BIN_CHILD (wbutton);
3776 GtkWidget *c1;
3777 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3778 GtkImage *wimage = GTK_IMAGE (c1);
3779 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3780 (wlbl ? gtk_label_get_text (wlbl) : "");
3781 GtkWidget *wmenuimage;
3784 if (gtk_button_get_use_stock (wbutton))
3785 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3786 GTK_ICON_SIZE_MENU);
3787 else
3789 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3790 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3792 g_object_set (G_OBJECT (settings), "gtk-menu-images", TRUE, NULL);
3794 if (store_type == GTK_IMAGE_STOCK)
3796 gchar *stock_id;
3797 gtk_image_get_stock (wimage, &stock_id, NULL);
3798 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3800 else if (store_type == GTK_IMAGE_ICON_SET)
3802 GtkIconSet *icon_set;
3803 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3804 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3805 GTK_ICON_SIZE_MENU);
3807 else if (store_type == GTK_IMAGE_PIXBUF)
3809 gint width, height;
3811 if (settings &&
3812 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3813 &width, &height))
3815 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3817 src_pixbuf = gtk_image_get_pixbuf (wimage);
3818 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3819 GDK_INTERP_BILINEAR);
3821 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3823 else
3825 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3826 abort ();
3829 else if (store_type == GTK_IMAGE_ICON_NAME)
3831 const gchar *icon_name;
3832 GtkIconSize icon_size;
3834 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3835 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3836 GTK_ICON_SIZE_MENU);
3838 else
3840 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3841 abort ();
3844 if (wmenuimage)
3845 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3847 g_signal_connect (G_OBJECT (wmenuitem),
3848 "activate",
3849 G_CALLBACK (xg_tool_bar_proxy_callback),
3850 user_data);
3853 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3854 (gpointer) wbutton);
3855 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3856 gtk_widget_set_sensitive (wmenuitem,
3857 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
3859 /* Use enter/leave notify to show help. We use the events
3860 rather than the GtkButton specific signals "enter" and
3861 "leave", so we can have only one callback. The event
3862 will tell us what kind of event it is. */
3863 g_signal_connect (G_OBJECT (wmenuitem),
3864 "enter-notify-event",
3865 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3866 user_data);
3867 g_signal_connect (G_OBJECT (wmenuitem),
3868 "leave-notify-event",
3869 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3870 user_data);
3872 return TRUE;
3875 /* This callback is called when a tool bar is detached. We must set
3876 the height of the tool bar to zero when this happens so frame sizes
3877 are correctly calculated.
3878 WBOX is the handle box widget that enables detach/attach of the tool bar.
3879 W is the tool bar widget.
3880 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3882 static void
3883 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
3884 GtkWidget *w,
3885 gpointer client_data)
3887 FRAME_PTR f = (FRAME_PTR) client_data;
3889 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3890 NULL);
3892 if (f)
3894 GtkRequisition req, req2;
3895 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3896 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
3897 gtk_widget_get_preferred_size (w, NULL, &req2);
3898 req.width -= req2.width;
3899 req.height -= req2.height;
3900 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3901 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3902 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3903 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3904 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3905 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3906 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3907 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3908 xg_height_or_width_changed (f);
3912 /* This callback is called when a tool bar is reattached. We must set
3913 the height of the tool bar when this happens so frame sizes
3914 are correctly calculated.
3915 WBOX is the handle box widget that enables detach/attach of the tool bar.
3916 W is the tool bar widget.
3917 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3919 static void
3920 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
3921 GtkWidget *w,
3922 gpointer client_data)
3924 FRAME_PTR f = (FRAME_PTR) client_data;
3925 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3927 if (f)
3929 GtkRequisition req, req2;
3930 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3931 gtk_widget_get_preferred_size (GTK_WIDGET (wbox), NULL, &req);
3932 gtk_widget_get_preferred_size (w, NULL, &req2);
3933 req.width += req2.width;
3934 req.height += req2.height;
3935 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3936 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3937 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3938 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3939 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3940 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3941 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3942 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3943 xg_height_or_width_changed (f);
3947 /* This callback is called when the mouse enters or leaves a tool bar item.
3948 It is used for displaying and hiding the help text.
3949 W is the tool bar item, a button.
3950 EVENT is either an enter event or leave event.
3951 CLIENT_DATA is an integer that is the index of the button in the
3952 tool bar. 0 is the first button.
3954 Returns FALSE to tell GTK to keep processing this event. */
3956 static gboolean
3957 xg_tool_bar_help_callback (GtkWidget *w,
3958 GdkEventCrossing *event,
3959 gpointer client_data)
3961 intptr_t idx = (intptr_t) client_data;
3962 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3963 Lisp_Object help, frame;
3965 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3966 return FALSE;
3968 if (event->type == GDK_ENTER_NOTIFY)
3970 idx *= TOOL_BAR_ITEM_NSLOTS;
3971 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3973 if (NILP (help))
3974 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3976 else
3977 help = Qnil;
3979 XSETFRAME (frame, f);
3980 kbd_buffer_store_help_event (frame, help);
3982 return FALSE;
3986 /* This callback is called when a tool bar item shall be redrawn.
3987 It modifies the expose event so that the GtkImage widget redraws the
3988 whole image. This to overcome a bug that makes GtkImage draw the image
3989 in the wrong place when it tries to redraw just a part of the image.
3990 W is the GtkImage to be redrawn.
3991 EVENT is the expose event for W.
3992 CLIENT_DATA is unused.
3994 Returns FALSE to tell GTK to keep processing this event. */
3996 #ifndef HAVE_GTK3
3997 static gboolean
3998 xg_tool_bar_item_expose_callback (GtkWidget *w,
3999 GdkEventExpose *event,
4000 gpointer client_data)
4002 gint width, height;
4004 gdk_drawable_get_size (event->window, &width, &height);
4005 event->area.x -= width > event->area.width ? width-event->area.width : 0;
4006 event->area.y -= height > event->area.height ? height-event->area.height : 0;
4008 event->area.x = max (0, event->area.x);
4009 event->area.y = max (0, event->area.y);
4011 event->area.width = max (width, event->area.width);
4012 event->area.height = max (height, event->area.height);
4014 return FALSE;
4016 #endif
4018 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
4019 #define toolbar_set_orientation(w, o) \
4020 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
4021 #else
4022 #define toolbar_set_orientation(w, o) \
4023 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
4024 #endif
4026 /* Attach a tool bar to frame F. */
4028 static void
4029 xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
4031 struct x_output *x = f->output_data.x;
4032 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
4034 toolbar_set_orientation (x->toolbar_widget,
4035 into_hbox
4036 ? GTK_ORIENTATION_VERTICAL
4037 : GTK_ORIENTATION_HORIZONTAL);
4038 if (!x->handlebox_widget)
4040 x->handlebox_widget = gtk_handle_box_new ();
4041 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
4042 G_CALLBACK (xg_tool_bar_detach_callback), f);
4043 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
4044 G_CALLBACK (xg_tool_bar_attach_callback), f);
4045 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
4046 x->toolbar_widget);
4049 if (into_hbox)
4051 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4052 GTK_POS_TOP);
4053 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
4054 FALSE, FALSE, 0);
4056 if (EQ (pos, Qleft))
4057 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
4058 x->handlebox_widget,
4060 x->toolbar_in_hbox = 1;
4062 else
4064 int vbox_pos = x->menubar_widget ? 1 : 0;
4065 gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (x->handlebox_widget),
4066 GTK_POS_LEFT);
4067 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
4068 FALSE, FALSE, 0);
4070 if (EQ (pos, Qtop))
4071 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
4072 x->handlebox_widget,
4073 vbox_pos);
4074 x->toolbar_in_hbox = 0;
4078 /* Create a tool bar for frame F. */
4080 static void
4081 xg_create_tool_bar (FRAME_PTR f)
4083 struct x_output *x = f->output_data.x;
4085 x->toolbar_widget = gtk_toolbar_new ();
4086 x->toolbar_detached = 0;
4088 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
4090 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
4091 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
4095 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
4097 /* Find the right-to-left image named by RTL in the tool bar images for F.
4098 Returns IMAGE if RTL is not found. */
4100 static Lisp_Object
4101 find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
4103 int i;
4104 Lisp_Object file, rtl_name;
4105 struct gcpro gcpro1, gcpro2;
4106 GCPRO2 (file, rtl_name);
4108 rtl_name = Ffile_name_nondirectory (rtl);
4110 for (i = 0; i < f->n_tool_bar_items; ++i)
4112 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
4113 if (!NILP (file = file_for_image (rtl_image)))
4115 file = call1 (intern ("file-name-sans-extension"),
4116 Ffile_name_nondirectory (file));
4117 if (EQ (Fequal (file, rtl_name), Qt))
4119 image = rtl_image;
4120 break;
4125 return image;
4128 static GtkToolItem *
4129 xg_make_tool_item (FRAME_PTR f,
4130 GtkWidget *wimage,
4131 GtkWidget **wbutton,
4132 const char *label,
4133 int i, int horiz, int text_image)
4135 GtkToolItem *ti = gtk_tool_item_new ();
4136 GtkWidget *vb = horiz ? gtk_hbox_new (FALSE, 0) : gtk_vbox_new (FALSE, 0);
4137 GtkWidget *wb = gtk_button_new ();
4138 GtkWidget *weventbox = gtk_event_box_new ();
4140 if (wimage && !text_image)
4141 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4142 if (label)
4143 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
4144 if (wimage && text_image)
4145 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4147 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4148 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4149 gtk_container_add (GTK_CONTAINER (wb), vb);
4150 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4151 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4153 if (wimage)
4155 intptr_t ii = i;
4156 gpointer gi = (gpointer) ii;
4158 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4159 G_CALLBACK (xg_tool_bar_menu_proxy),
4160 gi);
4162 g_signal_connect (G_OBJECT (wb), "clicked",
4163 G_CALLBACK (xg_tool_bar_callback),
4164 gi);
4166 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4168 #ifndef HAVE_GTK3
4169 /* Catch expose events to overcome an annoying redraw bug, see
4170 comment for xg_tool_bar_item_expose_callback. */
4171 g_signal_connect (G_OBJECT (ti),
4172 "expose-event",
4173 G_CALLBACK (xg_tool_bar_item_expose_callback),
4175 #endif
4176 gtk_tool_item_set_homogeneous (ti, FALSE);
4178 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4179 no distinction based on modifiers in the activate callback,
4180 so we have to do it ourselves. */
4181 g_signal_connect (wb, "button-release-event",
4182 G_CALLBACK (xg_tool_bar_button_cb),
4183 NULL);
4185 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4187 /* Use enter/leave notify to show help. We use the events
4188 rather than the GtkButton specific signals "enter" and
4189 "leave", so we can have only one callback. The event
4190 will tell us what kind of event it is. */
4191 /* The EMACS_INT cast avoids a warning. */
4192 g_signal_connect (G_OBJECT (weventbox),
4193 "enter-notify-event",
4194 G_CALLBACK (xg_tool_bar_help_callback),
4195 gi);
4196 g_signal_connect (G_OBJECT (weventbox),
4197 "leave-notify-event",
4198 G_CALLBACK (xg_tool_bar_help_callback),
4199 gi);
4202 if (wbutton) *wbutton = wb;
4204 return ti;
4207 static int
4208 xg_tool_item_stale_p (GtkWidget *wbutton, const char *stock_name,
4209 const char *icon_name, const struct image *img,
4210 const char *label, int horiz)
4212 gpointer old;
4213 GtkWidget *wimage;
4214 GtkWidget *vb = XG_BIN_CHILD (wbutton);
4215 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4217 /* Check if the tool icon matches. */
4218 if (stock_name)
4220 old = g_object_get_data (G_OBJECT (wimage),
4221 XG_TOOL_BAR_STOCK_NAME);
4222 if (!old || strcmp (old, stock_name))
4223 return 1;
4225 else if (icon_name)
4227 old = g_object_get_data (G_OBJECT (wimage),
4228 XG_TOOL_BAR_ICON_NAME);
4229 if (!old || strcmp (old, icon_name))
4230 return 1;
4232 else
4234 gpointer gold_img = g_object_get_data (G_OBJECT (wimage),
4235 XG_TOOL_BAR_IMAGE_DATA);
4236 Pixmap old_img = (Pixmap) gold_img;
4237 if (old_img != img->pixmap)
4238 return 1;
4241 /* Check button configuration and label. */
4242 if ((horiz ? GTK_IS_VBOX (vb) : GTK_IS_HBOX (vb))
4243 || (label ? (wlbl == NULL) : (wlbl != NULL)))
4244 return 1;
4246 /* Ensure label is correct. */
4247 if (label)
4248 gtk_label_set_text (GTK_LABEL (wlbl), label);
4249 return 0;
4252 static int
4253 xg_update_tool_bar_sizes (FRAME_PTR f)
4255 struct x_output *x = f->output_data.x;
4256 GtkRequisition req;
4257 int nl = 0, nr = 0, nt = 0, nb = 0;
4259 gtk_widget_get_preferred_size (GTK_WIDGET (x->handlebox_widget), NULL, &req);
4260 if (x->toolbar_in_hbox)
4262 int pos;
4263 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4264 x->handlebox_widget,
4265 "position", &pos, NULL);
4266 if (pos == 0) nl = req.width;
4267 else nr = req.width;
4269 else
4271 int pos;
4272 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4273 x->handlebox_widget,
4274 "position", &pos, NULL);
4275 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4276 else nb = req.height;
4279 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4280 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4281 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4282 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4284 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4285 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4286 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4287 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4288 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4289 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4290 return 1;
4293 return 0;
4297 /* Update the tool bar for frame F. Add new buttons and remove old. */
4299 void
4300 update_frame_tool_bar (FRAME_PTR f)
4302 int i, j;
4303 struct x_output *x = f->output_data.x;
4304 int hmargin = 0, vmargin = 0;
4305 GtkToolbar *wtoolbar;
4306 GtkToolItem *ti;
4307 GtkTextDirection dir;
4308 int pack_tool_bar = x->handlebox_widget == NULL;
4309 Lisp_Object style;
4310 int text_image, horiz;
4312 if (! FRAME_GTK_WIDGET (f))
4313 return;
4315 BLOCK_INPUT;
4317 if (INTEGERP (Vtool_bar_button_margin)
4318 && XINT (Vtool_bar_button_margin) > 0)
4320 hmargin = XFASTINT (Vtool_bar_button_margin);
4321 vmargin = XFASTINT (Vtool_bar_button_margin);
4323 else if (CONSP (Vtool_bar_button_margin))
4325 if (INTEGERP (XCAR (Vtool_bar_button_margin))
4326 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
4327 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4329 if (INTEGERP (XCDR (Vtool_bar_button_margin))
4330 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4331 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4334 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4335 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4336 i.e. zero. This means that margins less than
4337 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4338 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4339 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4341 if (! x->toolbar_widget)
4342 xg_create_tool_bar (f);
4344 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4345 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4347 style = Ftool_bar_get_system_style ();
4348 text_image = EQ (style, Qtext_image_horiz);
4349 horiz = EQ (style, Qboth_horiz) || text_image;
4351 for (i = j = 0; i < f->n_tool_bar_items; ++i)
4353 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4354 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4355 int idx;
4356 int img_id;
4357 int icon_size = 0;
4358 struct image *img = NULL;
4359 Lisp_Object image;
4360 Lisp_Object stock = Qnil;
4361 GtkStockItem stock_item;
4362 char *stock_name = NULL;
4363 char *icon_name = NULL;
4364 Lisp_Object rtl;
4365 GtkWidget *wbutton = NULL;
4366 Lisp_Object specified_file;
4367 int vert_only = ! NILP (PROP (TOOL_BAR_ITEM_VERT_ONLY));
4368 const char *label
4369 = (EQ (style, Qimage) || (vert_only && horiz)) ? NULL
4370 : STRINGP (PROP (TOOL_BAR_ITEM_LABEL))
4371 ? SSDATA (PROP (TOOL_BAR_ITEM_LABEL))
4372 : "";
4374 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4376 /* If this is a separator, use a gtk separator item. */
4377 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
4379 if (ti == NULL || !GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4381 if (ti)
4382 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4383 GTK_WIDGET (ti));
4384 ti = gtk_separator_tool_item_new ();
4385 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4387 j++;
4388 continue;
4391 /* Otherwise, the tool-bar item is an ordinary button. */
4393 if (ti && GTK_IS_SEPARATOR_TOOL_ITEM (ti))
4395 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4396 ti = NULL;
4399 if (ti) wbutton = XG_BIN_CHILD (XG_BIN_CHILD (ti));
4401 /* Ignore invalid image specifications. */
4402 image = PROP (TOOL_BAR_ITEM_IMAGES);
4403 if (!valid_image_p (image))
4405 if (ti)
4406 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4407 GTK_WIDGET (ti));
4408 continue;
4411 specified_file = file_for_image (image);
4412 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4413 stock = call1 (Qx_gtk_map_stock, specified_file);
4415 if (STRINGP (stock))
4417 stock_name = SSDATA (stock);
4418 if (stock_name[0] == 'n' && stock_name[1] == ':')
4420 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4421 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4423 icon_name = stock_name + 2;
4424 stock_name = NULL;
4425 stock = Qnil;
4427 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4428 icon_name = NULL;
4429 else
4430 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4432 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4433 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4434 else
4436 stock = Qnil;
4437 stock_name = NULL;
4441 if (stock_name == NULL && icon_name == NULL)
4443 /* No stock image, or stock item not known. Try regular
4444 image. If image is a vector, choose it according to the
4445 button state. */
4446 if (dir == GTK_TEXT_DIR_RTL
4447 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4448 && STRINGP (rtl))
4449 image = find_rtl_image (f, image, rtl);
4451 if (VECTORP (image))
4453 if (enabled_p)
4454 idx = (selected_p
4455 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4456 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4457 else
4458 idx = (selected_p
4459 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4460 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4462 xassert (ASIZE (image) >= idx);
4463 image = AREF (image, idx);
4465 else
4466 idx = -1;
4468 img_id = lookup_image (f, image);
4469 img = IMAGE_FROM_ID (f, img_id);
4470 prepare_image_for_display (f, img);
4472 if (img->load_failed_p || img->pixmap == None)
4474 if (ti)
4475 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4476 GTK_WIDGET (ti));
4477 continue;
4481 /* If there is an existing widget, check if it's stale; if so,
4482 remove it and make a new tool item from scratch. */
4483 if (ti && xg_tool_item_stale_p (wbutton, stock_name, icon_name,
4484 img, label, horiz))
4486 gtk_container_remove (GTK_CONTAINER (wtoolbar),
4487 GTK_WIDGET (ti));
4488 ti = NULL;
4491 if (ti == NULL)
4493 GtkWidget *w;
4495 /* Save the image so we can see if an update is needed the
4496 next time we call xg_tool_item_match_p. */
4497 if (EQ (style, Qtext))
4498 w = NULL;
4499 else if (stock_name)
4501 w = gtk_image_new_from_stock (stock_name, icon_size);
4502 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4503 (gpointer) xstrdup (stock_name),
4504 (GDestroyNotify) xfree);
4506 else if (icon_name)
4508 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4509 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4510 (gpointer) xstrdup (icon_name),
4511 (GDestroyNotify) xfree);
4513 else
4515 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4516 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4517 (gpointer)img->pixmap);
4520 if (w) gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4521 ti = xg_make_tool_item (f, w, &wbutton, label, i, horiz, text_image);
4522 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, j);
4525 #undef PROP
4527 gtk_widget_set_sensitive (wbutton, enabled_p);
4528 j++;
4531 /* Remove buttons not longer needed. */
4534 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), j);
4535 if (ti)
4536 gtk_container_remove (GTK_CONTAINER (wtoolbar), GTK_WIDGET (ti));
4537 } while (ti != NULL);
4539 if (f->n_tool_bar_items != 0)
4541 if (pack_tool_bar)
4542 xg_pack_tool_bar (f, f->tool_bar_position);
4543 gtk_widget_show_all (GTK_WIDGET (x->handlebox_widget));
4544 if (xg_update_tool_bar_sizes (f))
4545 xg_height_or_width_changed (f);
4548 UNBLOCK_INPUT;
4551 /* Deallocate all resources for the tool bar on frame F.
4552 Remove the tool bar. */
4554 void
4555 free_frame_tool_bar (FRAME_PTR f)
4557 struct x_output *x = f->output_data.x;
4559 if (x->toolbar_widget)
4561 int is_packed = x->handlebox_widget != 0;
4562 BLOCK_INPUT;
4563 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4564 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4565 if (is_packed)
4567 if (x->toolbar_in_hbox)
4568 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4569 x->handlebox_widget);
4570 else
4571 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4572 x->handlebox_widget);
4574 else
4575 gtk_widget_destroy (x->toolbar_widget);
4577 x->toolbar_widget = 0;
4578 x->handlebox_widget = 0;
4579 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4580 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4582 xg_height_or_width_changed (f);
4584 UNBLOCK_INPUT;
4589 xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4591 struct x_output *x = f->output_data.x;
4593 if (! x->toolbar_widget || ! x->handlebox_widget)
4594 return 1;
4596 BLOCK_INPUT;
4597 g_object_ref (x->handlebox_widget);
4598 if (x->toolbar_in_hbox)
4599 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4600 x->handlebox_widget);
4601 else
4602 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4603 x->handlebox_widget);
4604 xg_pack_tool_bar (f, pos);
4605 g_object_unref (x->handlebox_widget);
4606 if (xg_update_tool_bar_sizes (f))
4607 xg_height_or_width_changed (f);
4609 UNBLOCK_INPUT;
4610 return 1;
4615 /***********************************************************************
4616 Initializing
4617 ***********************************************************************/
4618 void
4619 xg_initialize (void)
4621 GtkBindingSet *binding_set;
4623 #if HAVE_XFT
4624 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4625 we keep it permanently linked in. */
4626 XftInit (0);
4627 #endif
4629 gdpy_def = NULL;
4630 xg_ignore_gtk_scrollbar = 0;
4631 xg_detached_menus = 0;
4632 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4633 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4635 id_to_widget.max_size = id_to_widget.used = 0;
4636 id_to_widget.widgets = 0;
4638 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4639 bindings. It doesn't seem to be any way to remove properties,
4640 so we set it to VoidSymbol which in X means "no key". */
4641 gtk_settings_set_string_property (gtk_settings_get_default (),
4642 "gtk-menu-bar-accel",
4643 "VoidSymbol",
4644 EMACS_CLASS);
4646 /* Make GTK text input widgets use Emacs style keybindings. This is
4647 Emacs after all. */
4648 gtk_settings_set_string_property (gtk_settings_get_default (),
4649 "gtk-key-theme-name",
4650 "Emacs",
4651 EMACS_CLASS);
4653 /* Make dialogs close on C-g. Since file dialog inherits from
4654 dialog, this works for them also. */
4655 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4656 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4657 "close", 0);
4659 /* Make menus close on C-g. */
4660 binding_set = gtk_binding_set_by_class (g_type_class_ref
4661 (GTK_TYPE_MENU_SHELL));
4662 gtk_binding_entry_add_signal (binding_set, GDK_KEY_g, GDK_CONTROL_MASK,
4663 "cancel", 0);
4666 #endif /* USE_GTK */