Use const char* instead of char*.
[emacs.git] / src / gtkutil.c
blobb1591b79f9c85f914b87a22cb7954cfd8f57d992
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 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 <string.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <setjmp.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
31 #include "window.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
38 #include "xsettings.h"
40 #ifdef HAVE_XFT
41 #include <X11/Xft/Xft.h>
42 #endif
44 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
45 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
47 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
48 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
50 /* Avoid "differ in sign" warnings */
51 #define SSDATA(x) ((char *) SDATA (x))
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
77 /***********************************************************************
78 Display handling functions
79 ***********************************************************************/
81 /* Keep track of the default display, or NULL if there is none. Emacs
82 may close all its displays. */
84 static GdkDisplay *gdpy_def;
86 /* When the GTK widget W is to be created on a display for F that
87 is not the default display, set the display for W.
88 W can be a GtkMenu or a GtkWindow widget. */
90 static void
91 xg_set_screen (GtkWidget *w, FRAME_PTR f)
93 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
95 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
96 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
98 if (GTK_IS_MENU (w))
99 gtk_menu_set_screen (GTK_MENU (w), gscreen);
100 else
101 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
106 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
107 *DPY is set to NULL if the display can't be opened.
109 Returns non-zero if display could be opened, zero if display could not
110 be opened, and less than zero if the GTK version doesn't support
111 multipe displays. */
113 void
114 xg_display_open (char *display_name, Display **dpy)
116 GdkDisplay *gdpy;
118 gdpy = gdk_display_open (display_name);
119 if (!gdpy_def && gdpy)
121 gdpy_def = gdpy;
122 gdk_display_manager_set_default_display (gdk_display_manager_get (),
123 gdpy);
126 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
130 /* Close display DPY. */
132 void
133 xg_display_close (Display *dpy)
135 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
137 /* If this is the default display, try to change it before closing.
138 If there is no other display to use, gdpy_def is set to NULL, and
139 the next call to xg_display_open resets the default display. */
140 if (gdk_display_get_default () == gdpy)
142 struct x_display_info *dpyinfo;
143 GdkDisplay *gdpy_new = NULL;
145 /* Find another display. */
146 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
147 if (dpyinfo->display != dpy)
149 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
150 gdk_display_manager_set_default_display (gdk_display_manager_get (),
151 gdpy_new);
152 break;
154 gdpy_def = gdpy_new;
157 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
158 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
159 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
160 can continue running, but there will be memory leaks. */
161 g_object_run_dispose (G_OBJECT (gdpy));
162 #else
163 /* This seems to be fixed in GTK 2.10. */
164 gdk_display_close (gdpy);
165 #endif
169 /***********************************************************************
170 Utility functions
171 ***********************************************************************/
172 /* The next two variables and functions are taken from lwlib. */
173 static widget_value *widget_value_free_list;
174 static int malloc_cpt;
176 /* Allocate a widget_value structure, either by taking one from the
177 widget_value_free_list or by malloc:ing a new one.
179 Return a pointer to the allocated structure. */
181 widget_value *
182 malloc_widget_value (void)
184 widget_value *wv;
185 if (widget_value_free_list)
187 wv = widget_value_free_list;
188 widget_value_free_list = wv->free_list;
189 wv->free_list = 0;
191 else
193 wv = (widget_value *) xmalloc (sizeof (widget_value));
194 malloc_cpt++;
196 memset (wv, 0, sizeof (widget_value));
197 return wv;
200 /* This is analogous to free. It frees only what was allocated
201 by malloc_widget_value, and no substructures. */
203 void
204 free_widget_value (widget_value *wv)
206 if (wv->free_list)
207 abort ();
209 if (malloc_cpt > 25)
211 /* When the number of already allocated cells is too big,
212 We free it. */
213 xfree (wv);
214 malloc_cpt--;
216 else
218 wv->free_list = widget_value_free_list;
219 widget_value_free_list = wv;
224 /* Create and return the cursor to be used for popup menus and
225 scroll bars on display DPY. */
227 GdkCursor *
228 xg_create_default_cursor (Display *dpy)
230 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
231 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
234 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
236 static GdkPixbuf *
237 xg_get_pixbuf_from_pix_and_mask (GdkPixmap *gpix,
238 GdkPixmap *gmask,
239 GdkColormap *cmap)
241 int width, height;
242 GdkPixbuf *icon_buf, *tmp_buf;
244 gdk_drawable_get_size (gpix, &width, &height);
245 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
246 0, 0, 0, 0, width, height);
247 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
248 g_object_unref (G_OBJECT (tmp_buf));
250 if (gmask)
252 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
253 gmask,
254 NULL,
255 0, 0, 0, 0,
256 width, height);
257 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
258 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
259 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
260 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
261 int y;
263 for (y = 0; y < height; ++y)
265 guchar *iconptr, *maskptr;
266 int x;
268 iconptr = pixels + y * rowstride;
269 maskptr = mask_pixels + y * mask_rowstride;
271 for (x = 0; x < width; ++x)
273 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
274 just R is sufficient. */
275 if (maskptr[0] == 0)
276 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
278 iconptr += rowstride/width;
279 maskptr += mask_rowstride/width;
283 g_object_unref (G_OBJECT (mask_buf));
286 return icon_buf;
289 static Lisp_Object
290 file_for_image (Lisp_Object image)
292 Lisp_Object specified_file = Qnil;
293 Lisp_Object tail;
295 for (tail = XCDR (image);
296 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
297 tail = XCDR (XCDR (tail)))
298 if (EQ (XCAR (tail), QCfile))
299 specified_file = XCAR (XCDR (tail));
301 return specified_file;
304 /* For the image defined in IMG, make and return a GtkImage. For displays with
305 8 planes or less we must make a GdkPixbuf and apply the mask manually.
306 Otherwise the highlightning and dimming the tool bar code in GTK does
307 will look bad. For display with more than 8 planes we just use the
308 pixmap and mask directly. For monochrome displays, GTK doesn't seem
309 able to use external pixmaps, it looks bad whatever we do.
310 The image is defined on the display where frame F is.
311 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
312 If OLD_WIDGET is NULL, a new widget is constructed and returned.
313 If OLD_WIDGET is not NULL, that widget is modified. */
315 static GtkWidget *
316 xg_get_image_for_pixmap (FRAME_PTR f,
317 struct image *img,
318 GtkWidget *widget,
319 GtkImage *old_widget)
321 GdkPixmap *gpix;
322 GdkPixmap *gmask;
323 GdkDisplay *gdpy;
324 GdkColormap *cmap;
325 GdkPixbuf *icon_buf;
327 /* If we have a file, let GTK do all the image handling.
328 This seems to be the only way to make insensitive and activated icons
329 look good in all cases. */
330 Lisp_Object specified_file = file_for_image (img->spec);
331 Lisp_Object file;
333 /* We already loaded the image once before calling this
334 function, so this only fails if the image file has been removed.
335 In that case, use the pixmap already loaded. */
337 if (STRINGP (specified_file)
338 && STRINGP (file = x_find_image_file (specified_file)))
340 if (! old_widget)
341 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
342 else
343 gtk_image_set_from_file (old_widget, SSDATA (file));
345 return GTK_WIDGET (old_widget);
348 /* No file, do the image handling ourselves. This will look very bad
349 on a monochrome display, and sometimes bad on all displays with
350 certain themes. */
352 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
353 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
354 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
356 /* This is a workaround to make icons look good on pseudo color
357 displays. Apparently GTK expects the images to have an alpha
358 channel. If they don't, insensitive and activated icons will
359 look bad. This workaround does not work on monochrome displays,
360 and is strictly not needed on true color/static color displays (i.e.
361 16 bits and higher). But we do it anyway so we get a pixbuf that is
362 not associated with the img->pixmap. The img->pixmap may be removed
363 by clearing the image cache and then the tool bar redraw fails, since
364 Gtk+ assumes the pixmap is always there. */
365 cmap = gtk_widget_get_colormap (widget);
366 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
368 if (! old_widget)
369 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
370 else
371 gtk_image_set_from_pixbuf (old_widget, icon_buf);
373 g_object_unref (G_OBJECT (icon_buf));
375 g_object_unref (G_OBJECT (gpix));
376 if (gmask) g_object_unref (G_OBJECT (gmask));
378 return GTK_WIDGET (old_widget);
382 /* Set CURSOR on W and all widgets W contain. We must do like this
383 for scroll bars and menu because they create widgets internally,
384 and it is those widgets that are visible. */
386 static void
387 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
389 GdkWindow *window = gtk_widget_get_window(w);
390 GList *children = gdk_window_peek_children (window);
392 gdk_window_set_cursor (window, cursor);
394 /* The scroll bar widget has more than one GDK window (had to look at
395 the source to figure this out), and there is no way to set cursor
396 on widgets in GTK. So we must set the cursor for all GDK windows.
397 Ditto for menus. */
399 for ( ; children; children = g_list_next (children))
400 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
403 /* Insert NODE into linked LIST. */
405 static void
406 xg_list_insert (xg_list_node *list, xg_list_node *node)
408 xg_list_node *list_start = list->next;
410 if (list_start) list_start->prev = node;
411 node->next = list_start;
412 node->prev = 0;
413 list->next = node;
416 /* Remove NODE from linked LIST. */
418 static void
419 xg_list_remove (xg_list_node *list, xg_list_node *node)
421 xg_list_node *list_start = list->next;
422 if (node == list_start)
424 list->next = node->next;
425 if (list->next) list->next->prev = 0;
427 else
429 node->prev->next = node->next;
430 if (node->next) node->next->prev = node->prev;
434 /* Allocate and return a utf8 version of STR. If STR is already
435 utf8 or NULL, just return STR.
436 If not, a new string is allocated and the caller must free the result
437 with g_free. */
439 static char *
440 get_utf8_string (char *str)
442 char *utf8_str = str;
444 if (!str) return NULL;
446 /* If not UTF-8, try current locale. */
447 if (!g_utf8_validate (str, -1, NULL))
448 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
450 if (!utf8_str)
452 /* Probably some control characters in str. Escape them. */
453 size_t nr_bad = 0;
454 gsize bytes_read;
455 gsize bytes_written;
456 unsigned char *p = (unsigned char *)str;
457 char *cp, *up;
458 GError *error = NULL;
460 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
461 &bytes_written, &error))
462 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
464 ++nr_bad;
465 p += bytes_written+1;
466 g_error_free (error);
467 error = NULL;
470 if (error)
472 g_error_free (error);
473 error = NULL;
475 if (cp) g_free (cp);
477 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
478 p = (unsigned char *)str;
480 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
481 &bytes_written, &error))
482 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
484 strncpy (up, (char *)p, bytes_written);
485 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
486 up[bytes_written+4] = '\0';
487 up += bytes_written+4;
488 p += bytes_written+1;
489 g_error_free (error);
490 error = NULL;
493 if (cp)
495 strcat (utf8_str, cp);
496 g_free (cp);
498 if (error)
500 g_error_free (error);
501 error = NULL;
504 return utf8_str;
509 /***********************************************************************
510 Tooltips
511 ***********************************************************************/
512 /* Gtk+ calls this callback when the parent of our tooltip dummy changes.
513 We use that to pop down the tooltip. This happens if Gtk+ for some
514 reason wants to change or hide the tooltip. */
516 #ifdef USE_GTK_TOOLTIP
518 static void
519 hierarchy_ch_cb (GtkWidget *widget,
520 GtkWidget *previous_toplevel,
521 gpointer user_data)
523 FRAME_PTR f = (FRAME_PTR) user_data;
524 struct x_output *x = f->output_data.x;
525 GtkWidget *top = gtk_widget_get_toplevel (x->ttip_lbl);
527 if (! top || ! GTK_IS_WINDOW (top))
528 gtk_widget_hide (previous_toplevel);
531 /* Callback called when Gtk+ thinks a tooltip should be displayed.
532 We use it to get the tooltip window and the tooltip widget so
533 we can manipulate the ourselves.
535 Return FALSE ensures that the tooltip is not shown. */
537 static gboolean
538 qttip_cb (GtkWidget *widget,
539 gint xpos,
540 gint ypos,
541 gboolean keyboard_mode,
542 GtkTooltip *tooltip,
543 gpointer user_data)
545 FRAME_PTR f = (FRAME_PTR) user_data;
546 struct x_output *x = f->output_data.x;
547 if (x->ttip_widget == NULL)
549 g_object_set (G_OBJECT (widget), "has-tooltip", FALSE, NULL);
550 x->ttip_widget = tooltip;
551 g_object_ref (G_OBJECT (tooltip));
552 x->ttip_lbl = gtk_label_new ("");
553 g_object_ref (G_OBJECT (x->ttip_lbl));
554 gtk_tooltip_set_custom (tooltip, x->ttip_lbl);
555 x->ttip_window = GTK_WINDOW (gtk_widget_get_toplevel (x->ttip_lbl));
556 /* Realize so we can safely get screen later on. */
557 gtk_widget_realize (GTK_WIDGET (x->ttip_window));
558 gtk_widget_realize (x->ttip_lbl);
560 g_signal_connect (x->ttip_lbl, "hierarchy-changed",
561 G_CALLBACK (hierarchy_ch_cb), f);
563 return FALSE;
566 #endif /* USE_GTK_TOOLTIP */
568 /* Prepare a tooltip to be shown, i.e. calculate WIDTH and HEIGHT.
569 Return zero if no system tooltip available, non-zero otherwise. */
572 xg_prepare_tooltip (FRAME_PTR f,
573 Lisp_Object string,
574 int *width,
575 int *height)
577 #ifndef USE_GTK_TOOLTIP
578 return 0;
579 #else
580 struct x_output *x = f->output_data.x;
581 GtkWidget *widget;
582 GdkWindow *gwin;
583 GdkScreen *screen;
584 GtkSettings *settings;
585 gboolean tt_enabled = TRUE;
586 GtkRequisition req;
587 Lisp_Object encoded_string;
589 if (!x->ttip_lbl) return 0;
591 BLOCK_INPUT;
592 encoded_string = ENCODE_UTF_8 (string);
593 widget = GTK_WIDGET (x->ttip_lbl);
594 gwin = gtk_widget_get_window (GTK_WIDGET (x->ttip_window));
595 screen = gdk_drawable_get_screen (gwin);
596 settings = gtk_settings_get_for_screen (screen);
597 g_object_get (settings, "gtk-enable-tooltips", &tt_enabled, NULL);
598 if (tt_enabled)
600 g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
601 /* Record that we disabled it so it can be enabled again. */
602 g_object_set_data (G_OBJECT (x->ttip_window), "restore-tt",
603 (gpointer)f);
606 /* Prevent Gtk+ from hiding tooltip on mouse move and such. */
607 g_object_set_data (G_OBJECT
608 (gtk_widget_get_display (GTK_WIDGET (x->ttip_window))),
609 "gdk-display-current-tooltip", NULL);
611 /* Put out dummy widget in so we can get callbacks for unrealize and
612 hierarchy-changed. */
613 gtk_tooltip_set_custom (x->ttip_widget, widget);
615 gtk_tooltip_set_text (x->ttip_widget, SDATA (encoded_string));
616 gtk_widget_size_request (GTK_WIDGET (x->ttip_window), &req);
617 if (width) *width = req.width;
618 if (height) *height = req.height;
620 UNBLOCK_INPUT;
622 return 1;
623 #endif /* USE_GTK_TOOLTIP */
626 /* Show the tooltip at ROOT_X and ROOT_Y.
627 xg_prepare_tooltip must have been called before this function. */
629 void
630 xg_show_tooltip (FRAME_PTR f, int root_x, int root_y)
632 #ifdef USE_GTK_TOOLTIP
633 struct x_output *x = f->output_data.x;
634 if (x->ttip_window)
636 BLOCK_INPUT;
637 gtk_window_move (x->ttip_window, root_x, root_y);
638 gtk_widget_show_all (GTK_WIDGET (x->ttip_window));
639 UNBLOCK_INPUT;
641 #endif
644 /* Hide tooltip if shown. Do nothing if not shown.
645 Return non-zero if tip was hidden, non-ero if not (i.e. not using
646 system tooltips). */
649 xg_hide_tooltip (FRAME_PTR f)
651 int ret = 0;
652 #ifdef USE_GTK_TOOLTIP
653 if (f->output_data.x->ttip_window)
655 GtkWindow *win = f->output_data.x->ttip_window;
656 BLOCK_INPUT;
657 gtk_widget_hide (GTK_WIDGET (win));
659 if (g_object_get_data (G_OBJECT (win), "restore-tt"))
661 GdkWindow *gwin = gtk_widget_get_window (GTK_WIDGET (win));
662 GdkScreen *screen = gdk_drawable_get_screen (gwin);
663 GtkSettings *settings = gtk_settings_get_for_screen (screen);
664 g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
666 UNBLOCK_INPUT;
668 ret = 1;
670 #endif
671 return ret;
675 /***********************************************************************
676 General functions for creating widgets, resizing, events, e.t.c.
677 ***********************************************************************/
679 /* Make a geometry string and pass that to GTK. It seems this is the
680 only way to get geometry position right if the user explicitly
681 asked for a position when starting Emacs.
682 F is the frame we shall set geometry for. */
684 static void
685 xg_set_geometry (FRAME_PTR f)
687 if (f->size_hint_flags & (USPosition | PPosition))
689 int left = f->left_pos;
690 int xneg = f->size_hint_flags & XNegative;
691 int top = f->top_pos;
692 int yneg = f->size_hint_flags & YNegative;
693 char geom_str[32];
695 if (xneg)
696 left = -left;
697 if (yneg)
698 top = -top;
700 sprintf (geom_str, "=%dx%d%c%d%c%d",
701 FRAME_PIXEL_WIDTH (f),
702 FRAME_PIXEL_HEIGHT (f),
703 (xneg ? '-' : '+'), left,
704 (yneg ? '-' : '+'), top);
706 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
707 geom_str))
708 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
712 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
713 and use a GtkFixed widget, this doesn't happen automatically. */
715 static void
716 xg_clear_under_internal_border (FRAME_PTR f)
718 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
720 GtkWidget *wfixed = f->output_data.x->edit_widget;
721 gtk_widget_queue_draw (wfixed);
722 gdk_window_process_all_updates ();
723 x_clear_area (FRAME_X_DISPLAY (f),
724 FRAME_X_WINDOW (f),
725 0, 0,
726 FRAME_PIXEL_WIDTH (f),
727 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
728 x_clear_area (FRAME_X_DISPLAY (f),
729 FRAME_X_WINDOW (f),
730 0, 0,
731 FRAME_INTERNAL_BORDER_WIDTH (f),
732 FRAME_PIXEL_HEIGHT (f), 0);
733 x_clear_area (FRAME_X_DISPLAY (f),
734 FRAME_X_WINDOW (f),
735 0, FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
736 FRAME_PIXEL_WIDTH (f),
737 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
738 x_clear_area (FRAME_X_DISPLAY (f),
739 FRAME_X_WINDOW (f),
740 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
742 FRAME_INTERNAL_BORDER_WIDTH (f),
743 FRAME_PIXEL_HEIGHT (f), 0);
747 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
748 and a Gtk+ menu bar, we get resize events for the edit part of the
749 frame only. We let Gtk+ deal with the Gtk+ parts.
750 F is the frame to resize.
751 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
753 void
754 xg_frame_resized (FRAME_PTR f, int pixelwidth, int pixelheight)
756 int rows, columns;
758 if (pixelwidth == -1 && pixelheight == -1)
760 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
761 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
762 0, 0,
763 &pixelwidth, &pixelheight, 0);
764 else return;
768 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
769 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
771 if (columns != FRAME_COLS (f)
772 || rows != FRAME_LINES (f)
773 || pixelwidth != FRAME_PIXEL_WIDTH (f)
774 || pixelheight != FRAME_PIXEL_HEIGHT (f))
776 FRAME_PIXEL_WIDTH (f) = pixelwidth;
777 FRAME_PIXEL_HEIGHT (f) = pixelheight;
779 xg_clear_under_internal_border (f);
780 change_frame_size (f, rows, columns, 0, 1, 0);
781 SET_FRAME_GARBAGED (f);
782 cancel_mouse_face (f);
786 /* Resize the outer window of frame F after chainging the height.
787 COLUMNS/ROWS is the size the edit area shall have after the resize. */
789 void
790 xg_frame_set_char_size (FRAME_PTR f, int cols, int rows)
792 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
793 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
794 int pixelwidth;
796 if (FRAME_PIXEL_HEIGHT (f) == 0)
797 return;
799 /* Take into account the size of the scroll bar. Always use the
800 number of columns occupied by the scroll bar here otherwise we
801 might end up with a frame width that is not a multiple of the
802 frame's character width which is bad for vertically split
803 windows. */
804 f->scroll_bar_actual_width
805 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
807 compute_fringe_widths (f, 0);
809 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
810 after calculating that value. */
811 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
812 + FRAME_TOOLBAR_WIDTH (f);
815 /* Do this before resize, as we don't know yet if we will be resized. */
816 xg_clear_under_internal_border (f);
818 /* Must resize our top level widget. Font size may have changed,
819 but not rows/cols. */
820 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
821 pixelwidth, pixelheight);
822 x_wm_set_size_hint (f, 0, 0);
824 SET_FRAME_GARBAGED (f);
825 cancel_mouse_face (f);
827 /* We can not call change_frame_size for a mapped frame,
828 we can not set pixel width/height either. The window manager may
829 override our resize request, XMonad does this all the time.
830 The best we can do is try to sync, so lisp code sees the updated
831 size as fast as possible.
832 For unmapped windows, we can set rows/cols. When
833 the frame is mapped again we will (hopefully) get the correct size. */
834 if (f->async_visible)
836 /* Must call this to flush out events */
837 (void)gtk_events_pending ();
838 gdk_flush ();
839 x_wait_for_event (f, ConfigureNotify);
841 else
843 change_frame_size (f, rows, cols, 0, 1, 0);
844 FRAME_PIXEL_WIDTH (f) = pixelwidth;
845 FRAME_PIXEL_HEIGHT (f) = pixelheight;
849 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
850 The policy is to keep the number of editable lines. */
852 static void
853 xg_height_or_width_changed (FRAME_PTR f)
855 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
856 FRAME_TOTAL_PIXEL_WIDTH (f),
857 FRAME_TOTAL_PIXEL_HEIGHT (f));
858 f->output_data.x->hint_flags = 0;
859 x_wm_set_size_hint (f, 0, 0);
862 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
863 Must be done like this, because GtkWidget:s can have "hidden"
864 X Window that aren't accessible.
866 Return 0 if no widget match WDESC. */
868 GtkWidget *
869 xg_win_to_widget (Display *dpy, Window wdesc)
871 gpointer gdkwin;
872 GtkWidget *gwdesc = 0;
874 BLOCK_INPUT;
876 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
877 wdesc);
878 if (gdkwin)
880 GdkEvent event;
881 event.any.window = gdkwin;
882 gwdesc = gtk_get_event_widget (&event);
885 UNBLOCK_INPUT;
886 return gwdesc;
889 /* Fill in the GdkColor C so that it represents PIXEL.
890 W is the widget that color will be used for. Used to find colormap. */
892 static void
893 xg_pix_to_gcolor (GtkWidget *w, long unsigned int pixel, GdkColor *c)
895 GdkColormap *map = gtk_widget_get_colormap (w);
896 gdk_colormap_query_color (map, pixel, c);
899 /* Create and set up the GTK widgets for frame F.
900 Return 0 if creation failed, non-zero otherwise. */
903 xg_create_frame_widgets (FRAME_PTR f)
905 GtkWidget *wtop;
906 GtkWidget *wvbox, *whbox;
907 GtkWidget *wfixed;
908 GdkColor bg;
909 GtkRcStyle *style;
910 char *title = 0;
912 BLOCK_INPUT;
914 if (FRAME_X_EMBEDDED_P (f))
915 wtop = gtk_plug_new (f->output_data.x->parent_desc);
916 else
917 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
919 xg_set_screen (wtop, f);
921 wvbox = gtk_vbox_new (FALSE, 0);
922 whbox = gtk_hbox_new (FALSE, 0);
923 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
925 if (! wtop || ! wvbox || ! whbox || ! wfixed)
927 if (wtop) gtk_widget_destroy (wtop);
928 if (wvbox) gtk_widget_destroy (wvbox);
929 if (whbox) gtk_widget_destroy (whbox);
930 if (wfixed) gtk_widget_destroy (wfixed);
932 UNBLOCK_INPUT;
933 return 0;
936 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
937 gtk_widget_set_name (wtop, EMACS_CLASS);
938 gtk_widget_set_name (wvbox, "pane");
939 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
941 /* If this frame has a title or name, set it in the title bar. */
942 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
943 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
945 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
947 FRAME_GTK_OUTER_WIDGET (f) = wtop;
948 FRAME_GTK_WIDGET (f) = wfixed;
949 f->output_data.x->vbox_widget = wvbox;
950 f->output_data.x->hbox_widget = whbox;
952 gtk_widget_set_has_window (wfixed, TRUE);
954 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
955 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
956 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
958 if (FRAME_EXTERNAL_TOOL_BAR (f))
959 update_frame_tool_bar (f);
961 /* We don't want this widget double buffered, because we draw on it
962 with regular X drawing primitives, so from a GTK/GDK point of
963 view, the widget is totally blank. When an expose comes, this
964 will make the widget blank, and then Emacs redraws it. This flickers
965 a lot, so we turn off double buffering. */
966 gtk_widget_set_double_buffered (wfixed, FALSE);
968 gtk_window_set_wmclass (GTK_WINDOW (wtop),
969 SSDATA (Vx_resource_name),
970 SSDATA (Vx_resource_class));
972 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
973 GTK is to destroy the widget. We want Emacs to do that instead. */
974 g_signal_connect (G_OBJECT (wtop), "delete-event",
975 G_CALLBACK (gtk_true), 0);
977 /* Convert our geometry parameters into a geometry string
978 and specify it.
979 GTK will itself handle calculating the real position this way. */
980 xg_set_geometry (f);
981 f->win_gravity
982 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
984 gtk_widget_add_events (wfixed,
985 GDK_POINTER_MOTION_MASK
986 | GDK_EXPOSURE_MASK
987 | GDK_BUTTON_PRESS_MASK
988 | GDK_BUTTON_RELEASE_MASK
989 | GDK_KEY_PRESS_MASK
990 | GDK_ENTER_NOTIFY_MASK
991 | GDK_LEAVE_NOTIFY_MASK
992 | GDK_FOCUS_CHANGE_MASK
993 | GDK_STRUCTURE_MASK
994 | GDK_VISIBILITY_NOTIFY_MASK);
996 /* Must realize the windows so the X window gets created. It is used
997 by callers of this function. */
998 gtk_widget_realize (wfixed);
999 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
1001 /* Since GTK clears its window by filling with the background color,
1002 we must keep X and GTK background in sync. */
1003 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
1004 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
1006 /* Also, do not let any background pixmap to be set, this looks very
1007 bad as Emacs overwrites the background pixmap with its own idea
1008 of background color. */
1009 style = gtk_widget_get_modifier_style (wfixed);
1011 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
1012 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
1013 gtk_widget_modify_style (wfixed, style);
1015 #ifdef USE_GTK_TOOLTIP
1016 /* Steal a tool tip window we can move ourselves. */
1017 f->output_data.x->ttip_widget = 0;
1018 f->output_data.x->ttip_lbl = 0;
1019 f->output_data.x->ttip_window = 0;
1020 gtk_widget_set_tooltip_text (wtop, "Dummy text");
1021 g_signal_connect (wtop, "query-tooltip", G_CALLBACK (qttip_cb), f);
1022 #endif
1024 UNBLOCK_INPUT;
1026 return 1;
1029 void
1030 xg_free_frame_widgets (FRAME_PTR f)
1032 if (FRAME_GTK_OUTER_WIDGET (f))
1034 struct x_output *x = f->output_data.x;
1035 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
1036 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow in xterm.c */
1037 FRAME_GTK_OUTER_WIDGET (f) = 0;
1038 #ifdef USE_GTK_TOOLTIP
1039 if (x->ttip_lbl)
1040 gtk_widget_destroy (x->ttip_lbl);
1041 if (x->ttip_widget)
1042 g_object_unref (G_OBJECT (x->ttip_widget));
1043 #endif
1047 /* Set the normal size hints for the window manager, for frame F.
1048 FLAGS is the flags word to use--or 0 meaning preserve the flags
1049 that the window now has.
1050 If USER_POSITION is nonzero, we set the User Position
1051 flag (this is useful when FLAGS is 0). */
1053 void
1054 x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
1056 /* Must use GTK routines here, otherwise GTK resets the size hints
1057 to its own defaults. */
1058 GdkGeometry size_hints;
1059 gint hint_flags = 0;
1060 int base_width, base_height;
1061 int min_rows = 0, min_cols = 0;
1062 int win_gravity = f->win_gravity;
1064 /* Don't set size hints during initialization; that apparently leads
1065 to a race condition. See the thread at
1066 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
1067 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
1068 return;
1070 if (flags)
1072 memset (&size_hints, 0, sizeof (size_hints));
1073 f->output_data.x->size_hints = size_hints;
1074 f->output_data.x->hint_flags = hint_flags;
1076 else
1077 flags = f->size_hint_flags;
1079 size_hints = f->output_data.x->size_hints;
1080 hint_flags = f->output_data.x->hint_flags;
1082 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
1083 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
1084 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
1086 hint_flags |= GDK_HINT_BASE_SIZE;
1087 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
1088 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
1089 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
1091 check_frame_size (f, &min_rows, &min_cols);
1093 size_hints.base_width = base_width;
1094 size_hints.base_height = base_height;
1095 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
1096 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
1098 /* These currently have a one to one mapping with the X values, but I
1099 don't think we should rely on that. */
1100 hint_flags |= GDK_HINT_WIN_GRAVITY;
1101 size_hints.win_gravity = 0;
1102 if (win_gravity == NorthWestGravity)
1103 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
1104 else if (win_gravity == NorthGravity)
1105 size_hints.win_gravity = GDK_GRAVITY_NORTH;
1106 else if (win_gravity == NorthEastGravity)
1107 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
1108 else if (win_gravity == WestGravity)
1109 size_hints.win_gravity = GDK_GRAVITY_WEST;
1110 else if (win_gravity == CenterGravity)
1111 size_hints.win_gravity = GDK_GRAVITY_CENTER;
1112 else if (win_gravity == EastGravity)
1113 size_hints.win_gravity = GDK_GRAVITY_EAST;
1114 else if (win_gravity == SouthWestGravity)
1115 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
1116 else if (win_gravity == SouthGravity)
1117 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
1118 else if (win_gravity == SouthEastGravity)
1119 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
1120 else if (win_gravity == StaticGravity)
1121 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1123 if (user_position)
1125 hint_flags &= ~GDK_HINT_POS;
1126 hint_flags |= GDK_HINT_USER_POS;
1129 if (hint_flags != f->output_data.x->hint_flags
1130 || memcmp (&size_hints,
1131 &f->output_data.x->size_hints,
1132 sizeof (size_hints)) != 0)
1134 BLOCK_INPUT;
1135 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1136 NULL, &size_hints, hint_flags);
1137 f->output_data.x->size_hints = size_hints;
1138 f->output_data.x->hint_flags = hint_flags;
1139 UNBLOCK_INPUT;
1143 /* Change background color of a frame.
1144 Since GTK uses the background color to clear the window, we must
1145 keep the GTK and X colors in sync.
1146 F is the frame to change,
1147 BG is the pixel value to change to. */
1149 void
1150 xg_set_background_color (FRAME_PTR f, long unsigned int bg)
1152 if (FRAME_GTK_WIDGET (f))
1154 GdkColor gdk_bg;
1156 BLOCK_INPUT;
1157 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
1158 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
1159 UNBLOCK_INPUT;
1164 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1165 functions so GTK does not overwrite the icon. */
1167 void
1168 xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
1170 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1171 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
1172 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
1173 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
1175 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1180 /***********************************************************************
1181 Dialog functions
1182 ***********************************************************************/
1183 /* Return the dialog title to use for a dialog of type KEY.
1184 This is the encoding used by lwlib. We use the same for GTK. */
1186 static const char *
1187 get_dialog_title (char key)
1189 const char *title = "";
1191 switch (key) {
1192 case 'E': case 'e':
1193 title = "Error";
1194 break;
1196 case 'I': case 'i':
1197 title = "Information";
1198 break;
1200 case 'L': case 'l':
1201 title = "Prompt";
1202 break;
1204 case 'P': case 'p':
1205 title = "Prompt";
1206 break;
1208 case 'Q': case 'q':
1209 title = "Question";
1210 break;
1213 return title;
1216 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1217 the dialog, but return TRUE so the event does not propagate further
1218 in GTK. This prevents GTK from destroying the dialog widget automatically
1219 and we can always destrou the widget manually, regardles of how
1220 it was popped down (button press or WM_DELETE_WINDOW).
1221 W is the dialog widget.
1222 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1223 user_data is NULL (not used).
1225 Returns TRUE to end propagation of event. */
1227 static gboolean
1228 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1230 gtk_widget_unmap (w);
1231 return TRUE;
1234 /* Create a popup dialog window. See also xg_create_widget below.
1235 WV is a widget_value describing the dialog.
1236 SELECT_CB is the callback to use when a button has been pressed.
1237 DEACTIVATE_CB is the callback to use when the dialog pops down.
1239 Returns the GTK dialog widget. */
1241 static GtkWidget *
1242 create_dialog (widget_value *wv,
1243 GCallback select_cb,
1244 GCallback deactivate_cb)
1246 const char *title = get_dialog_title (wv->name[0]);
1247 int total_buttons = wv->name[1] - '0';
1248 int right_buttons = wv->name[4] - '0';
1249 int left_buttons;
1250 int button_nr = 0;
1251 int button_spacing = 10;
1252 GtkWidget *wdialog = gtk_dialog_new ();
1253 GtkDialog *wd = GTK_DIALOG (wdialog);
1254 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1255 widget_value *item;
1256 GtkWidget *wvbox;
1257 GtkWidget *whbox_up;
1258 GtkWidget *whbox_down;
1260 /* If the number of buttons is greater than 4, make two rows of buttons
1261 instead. This looks better. */
1262 int make_two_rows = total_buttons > 4;
1264 if (right_buttons == 0) right_buttons = total_buttons/2;
1265 left_buttons = total_buttons - right_buttons;
1267 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1268 gtk_widget_set_name (wdialog, "emacs-dialog");
1271 if (make_two_rows)
1273 wvbox = gtk_vbox_new (TRUE, button_spacing);
1274 whbox_up = gtk_hbox_new (FALSE, 0);
1275 whbox_down = gtk_hbox_new (FALSE, 0);
1277 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1278 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1279 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1281 cur_box = GTK_BOX (whbox_up);
1284 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1285 G_CALLBACK (dialog_delete_callback), 0);
1287 if (deactivate_cb)
1289 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1290 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1293 for (item = wv->contents; item; item = item->next)
1295 char *utf8_label = get_utf8_string (item->value);
1296 GtkWidget *w;
1297 GtkRequisition req;
1299 if (item->name && strcmp (item->name, "message") == 0)
1301 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1302 /* This is the text part of the dialog. */
1303 w = gtk_label_new (utf8_label);
1304 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1305 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1306 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1308 /* Try to make dialog look better. Must realize first so
1309 the widget can calculate the size it needs. */
1310 gtk_widget_realize (w);
1311 gtk_widget_size_request (w, &req);
1312 gtk_box_set_spacing (wvbox, req.height);
1313 if (item->value && strlen (item->value) > 0)
1314 button_spacing = 2*req.width/strlen (item->value);
1316 else
1318 /* This is one button to add to the dialog. */
1319 w = gtk_button_new_with_label (utf8_label);
1320 if (! item->enabled)
1321 gtk_widget_set_sensitive (w, FALSE);
1322 if (select_cb)
1323 g_signal_connect (G_OBJECT (w), "clicked",
1324 select_cb, item->call_data);
1326 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1327 if (++button_nr == left_buttons)
1329 if (make_two_rows)
1330 cur_box = GTK_BOX (whbox_down);
1331 else
1332 gtk_box_pack_start (cur_box,
1333 gtk_label_new (""),
1334 TRUE, TRUE,
1335 button_spacing);
1339 if (utf8_label && utf8_label != item->value)
1340 g_free (utf8_label);
1343 return wdialog;
1346 struct xg_dialog_data
1348 GMainLoop *loop;
1349 int response;
1350 GtkWidget *w;
1351 guint timerid;
1354 /* Function that is called when the file or font dialogs pop down.
1355 W is the dialog widget, RESPONSE is the response code.
1356 USER_DATA is what we passed in to g_signal_connect. */
1358 static void
1359 xg_dialog_response_cb (GtkDialog *w,
1360 gint response,
1361 gpointer user_data)
1363 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1364 dd->response = response;
1365 g_main_loop_quit (dd->loop);
1369 /* Destroy the dialog. This makes it pop down. */
1371 static Lisp_Object
1372 pop_down_dialog (Lisp_Object arg)
1374 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1375 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1377 BLOCK_INPUT;
1378 if (dd->w) gtk_widget_destroy (dd->w);
1379 if (dd->timerid != 0) g_source_remove (dd->timerid);
1381 g_main_loop_quit (dd->loop);
1382 g_main_loop_unref (dd->loop);
1384 UNBLOCK_INPUT;
1386 return Qnil;
1389 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1390 We pass in DATA as gpointer* so we can use this as a callback. */
1392 static gboolean
1393 xg_maybe_add_timer (gpointer data)
1395 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1396 EMACS_TIME next_time = timer_check (1);
1397 long secs = EMACS_SECS (next_time);
1398 long usecs = EMACS_USECS (next_time);
1400 dd->timerid = 0;
1402 if (secs >= 0 && usecs >= 0 && secs < ((guint)-1)/1000)
1404 dd->timerid = g_timeout_add (secs * 1000 + usecs/1000,
1405 xg_maybe_add_timer,
1406 dd);
1408 return FALSE;
1412 /* Pops up a modal dialog W and waits for response.
1413 We don't use gtk_dialog_run because we want to process emacs timers.
1414 The dialog W is not destroyed when this function returns. */
1416 static int
1417 xg_dialog_run (FRAME_PTR f, GtkWidget *w)
1419 int count = SPECPDL_INDEX ();
1420 struct xg_dialog_data dd;
1422 xg_set_screen (w, f);
1423 gtk_window_set_transient_for (GTK_WINDOW (w),
1424 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1425 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1426 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1428 dd.loop = g_main_loop_new (NULL, FALSE);
1429 dd.response = GTK_RESPONSE_CANCEL;
1430 dd.w = w;
1431 dd.timerid = 0;
1433 g_signal_connect (G_OBJECT (w),
1434 "response",
1435 G_CALLBACK (xg_dialog_response_cb),
1436 &dd);
1437 /* Don't destroy the widget if closed by the window manager close button. */
1438 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1439 gtk_widget_show (w);
1441 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1443 (void) xg_maybe_add_timer (&dd);
1444 g_main_loop_run (dd.loop);
1446 dd.w = 0;
1447 unbind_to (count, Qnil);
1449 return dd.response;
1453 /***********************************************************************
1454 File dialog functions
1455 ***********************************************************************/
1456 /* Return non-zero if the old file selection dialog is being used.
1457 Return zero if not. */
1460 xg_uses_old_file_dialog (void)
1462 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1463 extern int x_gtk_use_old_file_dialog;
1464 return x_gtk_use_old_file_dialog;
1465 #else
1466 return 0;
1467 #endif
1471 typedef char * (*xg_get_file_func) (GtkWidget *);
1473 /* Return the selected file for file chooser dialog W.
1474 The returned string must be free:d. */
1476 static char *
1477 xg_get_file_name_from_chooser (GtkWidget *w)
1479 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1482 /* Callback called when the "Show hidden files" toggle is pressed.
1483 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1485 static void
1486 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1488 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1489 gboolean visible;
1490 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1491 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1495 /* Callback called when a property changes in a file chooser.
1496 GOBJECT is the file chooser dialog, ARG1 describes the property.
1497 USER_DATA is the toggle widget in the file chooser dialog.
1498 We use this to update the "Show hidden files" toggle when the user
1499 changes that property by right clicking in the file list. */
1501 static void
1502 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1504 extern int x_gtk_show_hidden_files;
1506 if (strcmp (arg1->name, "show-hidden") == 0)
1508 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1509 gboolean visible, toggle_on;
1511 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1512 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1514 if (!!visible != !!toggle_on)
1516 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1517 G_CALLBACK (xg_toggle_visibility_cb),
1518 gobject);
1519 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1520 g_signal_handlers_unblock_by_func
1521 (G_OBJECT (wtoggle),
1522 G_CALLBACK (xg_toggle_visibility_cb),
1523 gobject);
1525 x_gtk_show_hidden_files = visible;
1529 /* Read a file name from the user using a file chooser dialog.
1530 F is the current frame.
1531 PROMPT is a prompt to show to the user. May not be NULL.
1532 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1533 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1534 file. *FUNC is set to a function that can be used to retrieve the
1535 selected file name from the returned widget.
1537 Returns the created widget. */
1539 static GtkWidget *
1540 xg_get_file_with_chooser (FRAME_PTR f,
1541 char *prompt,
1542 char *default_filename,
1543 int mustmatch_p, int only_dir_p,
1544 xg_get_file_func *func)
1546 char message[1024];
1548 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1549 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1550 GtkFileChooserAction action = (mustmatch_p ?
1551 GTK_FILE_CHOOSER_ACTION_OPEN :
1552 GTK_FILE_CHOOSER_ACTION_SAVE);
1553 extern int x_gtk_show_hidden_files;
1554 extern int x_gtk_file_dialog_help_text;
1557 if (only_dir_p)
1558 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1560 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1561 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1562 (mustmatch_p || only_dir_p ?
1563 GTK_STOCK_OPEN : GTK_STOCK_OK),
1564 GTK_RESPONSE_OK,
1565 NULL);
1566 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1568 wbox = gtk_vbox_new (FALSE, 0);
1569 gtk_widget_show (wbox);
1570 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1572 if (x_gtk_show_hidden_files)
1574 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1575 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1577 gtk_widget_show (wtoggle);
1578 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1579 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1580 g_signal_connect (G_OBJECT (filewin), "notify",
1581 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1583 if (x_gtk_file_dialog_help_text)
1585 message[0] = '\0';
1586 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1587 Show the C-l help text only for versions < 2.10. */
1588 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1589 strcat (message, "\nType C-l to display a file name text entry box.\n");
1590 strcat (message, "\nIf you don't like this file selector, use the "
1591 "corresponding\nkey binding or customize "
1592 "use-file-dialog to turn it off.");
1594 wmessage = gtk_label_new (message);
1595 gtk_widget_show (wmessage);
1598 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1599 if (x_gtk_file_dialog_help_text)
1600 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1601 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1603 if (default_filename)
1605 Lisp_Object file;
1606 struct gcpro gcpro1;
1607 char *utf8_filename;
1608 GCPRO1 (file);
1610 file = build_string (default_filename);
1612 /* File chooser does not understand ~/... in the file name. It must be
1613 an absolute name starting with /. */
1614 if (default_filename[0] != '/')
1615 file = Fexpand_file_name (file, Qnil);
1617 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1618 if (! NILP (Ffile_directory_p (file)))
1619 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1620 utf8_filename);
1621 else
1623 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1624 utf8_filename);
1625 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1627 char *cp = strrchr (utf8_filename, '/');
1628 if (cp) ++cp;
1629 else cp = utf8_filename;
1630 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1634 UNGCPRO;
1637 *func = xg_get_file_name_from_chooser;
1638 return filewin;
1641 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1643 /* Return the selected file for file selector dialog W.
1644 The returned string must be free:d. */
1646 static char *
1647 xg_get_file_name_from_selector (GtkWidget *w)
1649 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1650 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1653 /* Create a file selection dialog.
1654 F is the current frame.
1655 PROMPT is a prompt to show to the user. May not be NULL.
1656 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1657 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1658 file. *FUNC is set to a function that can be used to retrieve the
1659 selected file name from the returned widget.
1661 Returns the created widget. */
1663 static GtkWidget *
1664 xg_get_file_with_selection (FRAME_PTR f,
1665 char *prompt,
1666 char *default_filename,
1667 int mustmatch_p, int only_dir_p,
1668 xg_get_file_func *func)
1670 GtkWidget *filewin;
1671 GtkFileSelection *filesel;
1673 filewin = gtk_file_selection_new (prompt);
1674 filesel = GTK_FILE_SELECTION (filewin);
1676 if (default_filename)
1677 gtk_file_selection_set_filename (filesel, default_filename);
1679 if (mustmatch_p)
1681 /* The selection_entry part of filesel is not documented. */
1682 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1683 gtk_file_selection_hide_fileop_buttons (filesel);
1686 *func = xg_get_file_name_from_selector;
1688 return filewin;
1690 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1692 /* Read a file name from the user using a file dialog, either the old
1693 file selection dialog, or the new file chooser dialog. Which to use
1694 depends on what the GTK version used has, and what the value of
1695 gtk-use-old-file-dialog.
1696 F is the current frame.
1697 PROMPT is a prompt to show to the user. May not be NULL.
1698 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1699 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1700 file.
1702 Returns a file name or NULL if no file was selected.
1703 The returned string must be freed by the caller. */
1705 char *
1706 xg_get_file_name (FRAME_PTR f,
1707 char *prompt,
1708 char *default_filename,
1709 int mustmatch_p,
1710 int only_dir_p)
1712 GtkWidget *w = 0;
1713 char *fn = 0;
1714 int filesel_done = 0;
1715 xg_get_file_func func;
1717 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1718 /* I really don't know why this is needed, but without this the GLIBC add on
1719 library linuxthreads hangs when the Gnome file chooser backend creates
1720 threads. */
1721 sigblock (sigmask (__SIGRTMIN));
1722 #endif /* HAVE_GTK_AND_PTHREAD */
1724 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1726 if (xg_uses_old_file_dialog ())
1727 w = xg_get_file_with_selection (f, prompt, default_filename,
1728 mustmatch_p, only_dir_p, &func);
1729 else
1730 w = xg_get_file_with_chooser (f, prompt, default_filename,
1731 mustmatch_p, only_dir_p, &func);
1733 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1734 w = xg_get_file_with_chooser (f, prompt, default_filename,
1735 mustmatch_p, only_dir_p, &func);
1736 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1738 gtk_widget_set_name (w, "emacs-filedialog");
1740 filesel_done = xg_dialog_run (f, w);
1742 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1743 sigunblock (sigmask (__SIGRTMIN));
1744 #endif
1746 if (filesel_done == GTK_RESPONSE_OK)
1747 fn = (*func) (w);
1749 gtk_widget_destroy (w);
1750 return fn;
1753 #ifdef HAVE_FREETYPE
1754 /* Pop up a GTK font selector and return the name of the font the user
1755 selects, as a C string. The returned font name follows GTK's own
1756 format:
1758 `FAMILY [VALUE1 VALUE2] SIZE'
1760 This can be parsed using font_parse_fcname in font.c.
1761 DEFAULT_NAME, if non-zero, is the default font name. */
1763 char *
1764 xg_get_font_name (FRAME_PTR f, const char *default_name)
1766 GtkWidget *w;
1767 char *fontname = NULL;
1768 int done = 0;
1770 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1771 sigblock (sigmask (__SIGRTMIN));
1772 #endif /* HAVE_GTK_AND_PTHREAD */
1774 w = gtk_font_selection_dialog_new ("Pick a font");
1775 if (!default_name)
1776 default_name = "Monospace 10";
1777 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1778 default_name);
1780 gtk_widget_set_name (w, "emacs-fontdialog");
1782 done = xg_dialog_run (f, w);
1784 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1785 sigunblock (sigmask (__SIGRTMIN));
1786 #endif
1788 if (done == GTK_RESPONSE_OK)
1789 fontname = gtk_font_selection_dialog_get_font_name
1790 (GTK_FONT_SELECTION_DIALOG (w));
1792 gtk_widget_destroy (w);
1793 return fontname;
1795 #endif /* HAVE_FREETYPE */
1799 /***********************************************************************
1800 Menu functions.
1801 ***********************************************************************/
1803 /* The name of menu items that can be used for customization. Since GTK
1804 RC files are very crude and primitive, we have to set this on all
1805 menu item names so a user can easily customize menu items. */
1807 #define MENU_ITEM_NAME "emacs-menuitem"
1810 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1811 during GC. The next member points to the items. */
1812 static xg_list_node xg_menu_cb_list;
1814 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1815 during GC. The next member points to the items. */
1816 static xg_list_node xg_menu_item_cb_list;
1818 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1819 F is the frame CL_DATA will be initialized for.
1820 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1822 The menu bar and all sub menus under the menu bar in a frame
1823 share the same structure, hence the reference count.
1825 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1826 allocated xg_menu_cb_data if CL_DATA is NULL. */
1828 static xg_menu_cb_data *
1829 make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
1831 if (! cl_data)
1833 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1834 cl_data->f = f;
1835 cl_data->menu_bar_vector = f->menu_bar_vector;
1836 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1837 cl_data->highlight_cb = highlight_cb;
1838 cl_data->ref_count = 0;
1840 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1843 cl_data->ref_count++;
1845 return cl_data;
1848 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1849 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1851 When the menu bar is updated, menu items may have been added and/or
1852 removed, so menu_bar_vector and menu_bar_items_used change. We must
1853 then update CL_DATA since it is used to determine which menu
1854 item that is invoked in the menu.
1855 HIGHLIGHT_CB could change, there is no check that the same
1856 function is given when modifying a menu bar as was given when
1857 creating the menu bar. */
1859 static void
1860 update_cl_data (xg_menu_cb_data *cl_data,
1861 FRAME_PTR f,
1862 GCallback highlight_cb)
1864 if (cl_data)
1866 cl_data->f = f;
1867 cl_data->menu_bar_vector = f->menu_bar_vector;
1868 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1869 cl_data->highlight_cb = highlight_cb;
1873 /* Decrease reference count for CL_DATA.
1874 If reference count is zero, free CL_DATA. */
1876 static void
1877 unref_cl_data (xg_menu_cb_data *cl_data)
1879 if (cl_data && cl_data->ref_count > 0)
1881 cl_data->ref_count--;
1882 if (cl_data->ref_count == 0)
1884 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1885 xfree (cl_data);
1890 /* Function that marks all lisp data during GC. */
1892 void
1893 xg_mark_data (void)
1895 xg_list_node *iter;
1897 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1898 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1900 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1902 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1904 if (! NILP (cb_data->help))
1905 mark_object (cb_data->help);
1910 /* Callback called when a menu item is destroyed. Used to free data.
1911 W is the widget that is being destroyed (not used).
1912 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1914 static void
1915 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
1917 if (client_data)
1919 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1920 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1921 xfree (data);
1925 /* Callback called when the pointer enters/leaves a menu item.
1926 W is the parent of the menu item.
1927 EVENT is either an enter event or leave event.
1928 CLIENT_DATA is not used.
1930 Returns FALSE to tell GTK to keep processing this event. */
1932 static gboolean
1933 menuitem_highlight_callback (GtkWidget *w,
1934 GdkEventCrossing *event,
1935 gpointer client_data)
1937 GdkEvent ev;
1938 GtkWidget *subwidget;
1939 xg_menu_item_cb_data *data;
1941 ev.crossing = *event;
1942 subwidget = gtk_get_event_widget (&ev);
1943 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1944 XG_ITEM_DATA);
1945 if (data)
1947 if (! NILP (data->help) && data->cl_data->highlight_cb)
1949 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1950 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1951 (*func) (subwidget, call_data);
1955 return FALSE;
1958 /* Callback called when a menu is destroyed. Used to free data.
1959 W is the widget that is being destroyed (not used).
1960 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1962 static void
1963 menu_destroy_callback (GtkWidget *w, gpointer client_data)
1965 unref_cl_data ((xg_menu_cb_data*) client_data);
1968 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1969 must be non-NULL) and can be inserted into a menu item.
1971 Returns the GtkHBox. */
1973 static GtkWidget *
1974 make_widget_for_menu_item (const char *utf8_label, const char *utf8_key)
1976 GtkWidget *wlbl;
1977 GtkWidget *wkey;
1978 GtkWidget *wbox;
1980 wbox = gtk_hbox_new (FALSE, 0);
1981 wlbl = gtk_label_new (utf8_label);
1982 wkey = gtk_label_new (utf8_key);
1984 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1985 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1987 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1988 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1990 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1991 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1992 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1994 return wbox;
1997 /* Make and return a menu item widget with the key to the right.
1998 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1999 UTF8_KEY is the text representing the key binding.
2000 ITEM is the widget_value describing the menu item.
2002 GROUP is an in/out parameter. If the menu item to be created is not
2003 part of any radio menu group, *GROUP contains NULL on entry and exit.
2004 If the menu item to be created is part of a radio menu group, on entry
2005 *GROUP contains the group to use, or NULL if this is the first item
2006 in the group. On exit, *GROUP contains the radio item group.
2008 Unfortunately, keys don't line up as nicely as in Motif,
2009 but the MacOS X version doesn't either, so I guess that is OK. */
2011 static GtkWidget *
2012 make_menu_item (const char *utf8_label,
2013 const char *utf8_key,
2014 widget_value *item,
2015 GSList **group)
2017 GtkWidget *w;
2018 GtkWidget *wtoadd = 0;
2020 /* It has been observed that some menu items have a NULL name field.
2021 This will lead to this function being called with a NULL utf8_label.
2022 GTK crashes on that so we set a blank label. Why there is a NULL
2023 name remains to be investigated. */
2024 if (! utf8_label) utf8_label = " ";
2026 if (utf8_key)
2027 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2029 if (item->button_type == BUTTON_TYPE_TOGGLE)
2031 *group = NULL;
2032 if (utf8_key) w = gtk_check_menu_item_new ();
2033 else w = gtk_check_menu_item_new_with_label (utf8_label);
2034 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
2036 else if (item->button_type == BUTTON_TYPE_RADIO)
2038 if (utf8_key) w = gtk_radio_menu_item_new (*group);
2039 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
2040 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
2041 if (item->selected)
2042 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
2044 else
2046 *group = NULL;
2047 if (utf8_key) w = gtk_menu_item_new ();
2048 else w = gtk_menu_item_new_with_label (utf8_label);
2051 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
2052 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
2054 return w;
2057 /* Return non-zero if LABEL specifies a separator (GTK only has one
2058 separator type) */
2060 static const char* separator_names[] = {
2061 "space",
2062 "no-line",
2063 "single-line",
2064 "double-line",
2065 "single-dashed-line",
2066 "double-dashed-line",
2067 "shadow-etched-in",
2068 "shadow-etched-out",
2069 "shadow-etched-in-dash",
2070 "shadow-etched-out-dash",
2071 "shadow-double-etched-in",
2072 "shadow-double-etched-out",
2073 "shadow-double-etched-in-dash",
2074 "shadow-double-etched-out-dash",
2078 static int
2079 xg_separator_p (char *label)
2081 if (! label) return 0;
2082 else if (strlen (label) > 3
2083 && strncmp (label, "--", 2) == 0
2084 && label[2] != '-')
2086 int i;
2088 label += 2;
2089 for (i = 0; separator_names[i]; ++i)
2090 if (strcmp (label, separator_names[i]) == 0)
2091 return 1;
2093 else
2095 /* Old-style separator, maybe. It's a separator if it contains
2096 only dashes. */
2097 while (*label == '-')
2098 ++label;
2099 if (*label == 0) return 1;
2102 return 0;
2105 static int xg_detached_menus;
2107 /* Returns non-zero if there are detached menus. */
2110 xg_have_tear_offs (void)
2112 return xg_detached_menus > 0;
2115 /* Callback invoked when a detached menu window is removed. Here we
2116 decrease the xg_detached_menus count.
2117 WIDGET is the top level window that is removed (the parent of the menu).
2118 CLIENT_DATA is not used. */
2120 static void
2121 tearoff_remove (GtkWidget *widget, gpointer client_data)
2123 if (xg_detached_menus > 0) --xg_detached_menus;
2126 /* Callback invoked when a menu is detached. It increases the
2127 xg_detached_menus count.
2128 WIDGET is the GtkTearoffMenuItem.
2129 CLIENT_DATA is not used. */
2131 static void
2132 tearoff_activate (GtkWidget *widget, gpointer client_data)
2134 GtkWidget *menu = gtk_widget_get_parent (widget);
2135 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2137 ++xg_detached_menus;
2138 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2139 "destroy",
2140 G_CALLBACK (tearoff_remove), 0);
2145 /* Create a menu item widget, and connect the callbacks.
2146 ITEM decribes the menu item.
2147 F is the frame the created menu belongs to.
2148 SELECT_CB is the callback to use when a menu item is selected.
2149 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2150 CL_DATA points to the callback data to be used for this menu.
2151 GROUP is an in/out parameter. If the menu item to be created is not
2152 part of any radio menu group, *GROUP contains NULL on entry and exit.
2153 If the menu item to be created is part of a radio menu group, on entry
2154 *GROUP contains the group to use, or NULL if this is the first item
2155 in the group. On exit, *GROUP contains the radio item group.
2157 Returns the created GtkWidget. */
2159 static GtkWidget *
2160 xg_create_one_menuitem (widget_value *item,
2161 FRAME_PTR f,
2162 GCallback select_cb,
2163 GCallback highlight_cb,
2164 xg_menu_cb_data *cl_data,
2165 GSList **group)
2167 char *utf8_label;
2168 char *utf8_key;
2169 GtkWidget *w;
2170 xg_menu_item_cb_data *cb_data;
2172 utf8_label = get_utf8_string (item->name);
2173 utf8_key = get_utf8_string (item->key);
2175 w = make_menu_item (utf8_label, utf8_key, item, group);
2177 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2178 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
2180 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2182 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2184 cb_data->select_id = 0;
2185 cb_data->help = item->help;
2186 cb_data->cl_data = cl_data;
2187 cb_data->call_data = item->call_data;
2189 g_signal_connect (G_OBJECT (w),
2190 "destroy",
2191 G_CALLBACK (menuitem_destroy_callback),
2192 cb_data);
2194 /* Put cb_data in widget, so we can get at it when modifying menubar */
2195 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2197 /* final item, not a submenu */
2198 if (item->call_data && ! item->contents)
2200 if (select_cb)
2201 cb_data->select_id
2202 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2205 return w;
2208 /* Create a full menu tree specified by DATA.
2209 F is the frame the created menu belongs to.
2210 SELECT_CB is the callback to use when a menu item is selected.
2211 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2212 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2213 POP_UP_P is non-zero if we shall create a popup menu.
2214 MENU_BAR_P is non-zero if we shall create a menu bar.
2215 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2216 if MENU_BAR_P is non-zero.
2217 TOPMENU is the topmost GtkWidget that others shall be placed under.
2218 It may be NULL, in that case we create the appropriate widget
2219 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2220 CL_DATA is the callback data we shall use for this menu, or NULL
2221 if we haven't set the first callback yet.
2222 NAME is the name to give to the top level menu if this function
2223 creates it. May be NULL to not set any name.
2225 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2226 not NULL.
2228 This function calls itself to create submenus. */
2230 static GtkWidget *
2231 create_menus (widget_value *data,
2232 FRAME_PTR f,
2233 GCallback select_cb,
2234 GCallback deactivate_cb,
2235 GCallback highlight_cb,
2236 int pop_up_p,
2237 int menu_bar_p,
2238 int add_tearoff_p,
2239 GtkWidget *topmenu,
2240 xg_menu_cb_data *cl_data,
2241 const char *name)
2243 widget_value *item;
2244 GtkWidget *wmenu = topmenu;
2245 GSList *group = NULL;
2247 if (! topmenu)
2249 if (! menu_bar_p)
2251 wmenu = gtk_menu_new ();
2252 xg_set_screen (wmenu, f);
2253 /* Connect this to the menu instead of items so we get enter/leave for
2254 disabled items also. TODO: Still does not get enter/leave for
2255 disabled items in detached menus. */
2256 g_signal_connect (G_OBJECT (wmenu),
2257 "enter-notify-event",
2258 G_CALLBACK (menuitem_highlight_callback),
2259 NULL);
2260 g_signal_connect (G_OBJECT (wmenu),
2261 "leave-notify-event",
2262 G_CALLBACK (menuitem_highlight_callback),
2263 NULL);
2265 else
2267 wmenu = gtk_menu_bar_new ();
2268 /* Set width of menu bar to a small value so it doesn't enlarge
2269 a small initial frame size. The width will be set to the
2270 width of the frame later on when it is added to a container.
2271 height -1: Natural height. */
2272 gtk_widget_set_size_request (wmenu, 1, -1);
2275 /* Put cl_data on the top menu for easier access. */
2276 cl_data = make_cl_data (cl_data, f, highlight_cb);
2277 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2278 g_signal_connect (G_OBJECT (wmenu), "destroy",
2279 G_CALLBACK (menu_destroy_callback), cl_data);
2281 if (name)
2282 gtk_widget_set_name (wmenu, name);
2284 if (deactivate_cb)
2285 g_signal_connect (G_OBJECT (wmenu),
2286 "selection-done", deactivate_cb, 0);
2289 if (! menu_bar_p && add_tearoff_p)
2291 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2292 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2294 g_signal_connect (G_OBJECT (tearoff), "activate",
2295 G_CALLBACK (tearoff_activate), 0);
2298 for (item = data; item; item = item->next)
2300 GtkWidget *w;
2302 if (pop_up_p && !item->contents && !item->call_data
2303 && !xg_separator_p (item->name))
2305 char *utf8_label;
2306 /* A title for a popup. We do the same as GTK does when
2307 creating titles, but it does not look good. */
2308 group = NULL;
2309 utf8_label = get_utf8_string (item->name);
2311 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2312 w = gtk_menu_item_new_with_label (utf8_label);
2313 gtk_widget_set_sensitive (w, FALSE);
2314 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2316 else if (xg_separator_p (item->name))
2318 group = NULL;
2319 /* GTK only have one separator type. */
2320 w = gtk_separator_menu_item_new ();
2322 else
2324 w = xg_create_one_menuitem (item,
2326 item->contents ? 0 : select_cb,
2327 highlight_cb,
2328 cl_data,
2329 &group);
2331 /* Create a possibly empty submenu for menu bar items, since some
2332 themes don't highlight items correctly without it. */
2333 if (item->contents || menu_bar_p)
2335 GtkWidget *submenu = create_menus (item->contents,
2337 select_cb,
2338 deactivate_cb,
2339 highlight_cb,
2342 add_tearoff_p,
2344 cl_data,
2346 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2350 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2351 gtk_widget_set_name (w, MENU_ITEM_NAME);
2354 return wmenu;
2357 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2358 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2359 with some text and buttons.
2360 F is the frame the created item belongs to.
2361 NAME is the name to use for the top widget.
2362 VAL is a widget_value structure describing items to be created.
2363 SELECT_CB is the callback to use when a menu item is selected or
2364 a dialog button is pressed.
2365 DEACTIVATE_CB is the callback to use when an item is deactivated.
2366 For a menu, when a sub menu is not shown anymore, for a dialog it is
2367 called when the dialog is popped down.
2368 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2370 Returns the widget created. */
2372 GtkWidget *
2373 xg_create_widget (const char *type, const char *name, FRAME_PTR f, widget_value *val,
2374 GCallback select_cb, GCallback deactivate_cb,
2375 GCallback highlight_cb)
2377 GtkWidget *w = 0;
2378 int menu_bar_p = strcmp (type, "menubar") == 0;
2379 int pop_up_p = strcmp (type, "popup") == 0;
2381 if (strcmp (type, "dialog") == 0)
2383 w = create_dialog (val, select_cb, deactivate_cb);
2384 xg_set_screen (w, f);
2385 gtk_window_set_transient_for (GTK_WINDOW (w),
2386 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2387 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2388 gtk_widget_set_name (w, "emacs-dialog");
2389 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2391 else if (menu_bar_p || pop_up_p)
2393 w = create_menus (val->contents,
2395 select_cb,
2396 deactivate_cb,
2397 highlight_cb,
2398 pop_up_p,
2399 menu_bar_p,
2400 menu_bar_p,
2403 name);
2405 /* Set the cursor to an arrow for popup menus when they are mapped.
2406 This is done by default for menu bar menus. */
2407 if (pop_up_p)
2409 /* Must realize so the GdkWindow inside the widget is created. */
2410 gtk_widget_realize (w);
2411 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2414 else
2416 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2417 type);
2420 return w;
2423 /* Return the label for menu item WITEM. */
2425 static const char *
2426 xg_get_menu_item_label (GtkMenuItem *witem)
2428 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2429 return gtk_label_get_label (wlabel);
2432 /* Return non-zero if the menu item WITEM has the text LABEL. */
2434 static int
2435 xg_item_label_same_p (GtkMenuItem *witem, char *label)
2437 int is_same = 0;
2438 char *utf8_label = get_utf8_string (label);
2439 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2441 if (! old_label && ! utf8_label)
2442 is_same = 1;
2443 else if (old_label && utf8_label)
2444 is_same = strcmp (utf8_label, old_label) == 0;
2446 if (utf8_label && utf8_label != label) g_free (utf8_label);
2448 return is_same;
2451 /* Destroy widgets in LIST. */
2453 static void
2454 xg_destroy_widgets (GList *list)
2456 GList *iter;
2458 for (iter = list; iter; iter = g_list_next (iter))
2460 GtkWidget *w = GTK_WIDGET (iter->data);
2462 /* Destroying the widget will remove it from the container it is in. */
2463 gtk_widget_destroy (w);
2467 /* Update the top level names in MENUBAR (i.e. not submenus).
2468 F is the frame the menu bar belongs to.
2469 *LIST is a list with the current menu bar names (menu item widgets).
2470 ITER is the item within *LIST that shall be updated.
2471 POS is the numerical position, starting at 0, of ITER in *LIST.
2472 VAL describes what the menu bar shall look like after the update.
2473 SELECT_CB is the callback to use when a menu item is selected.
2474 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2475 CL_DATA points to the callback data to be used for this menu bar.
2477 This function calls itself to walk through the menu bar names. */
2479 static void
2480 xg_update_menubar (GtkWidget *menubar,
2481 FRAME_PTR f,
2482 GList **list,
2483 GList *iter,
2484 int pos,
2485 widget_value *val,
2486 GCallback select_cb,
2487 GCallback deactivate_cb,
2488 GCallback highlight_cb,
2489 xg_menu_cb_data *cl_data)
2491 if (! iter && ! val)
2492 return;
2493 else if (iter && ! val)
2495 /* Item(s) have been removed. Remove all remaining items. */
2496 xg_destroy_widgets (iter);
2498 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2499 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2500 gtk_menu_item_new_with_label (""),
2502 /* All updated. */
2503 val = 0;
2504 iter = 0;
2506 else if (! iter && val)
2508 /* Item(s) added. Add all new items in one call. */
2509 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2510 0, 1, 0, menubar, cl_data, 0);
2512 /* All updated. */
2513 val = 0;
2514 iter = 0;
2516 /* Below this neither iter or val is NULL */
2517 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2519 /* This item is still the same, check next item. */
2520 val = val->next;
2521 iter = g_list_next (iter);
2522 ++pos;
2524 else /* This item is changed. */
2526 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2527 GtkMenuItem *witem2 = 0;
2528 int val_in_menubar = 0;
2529 int iter_in_new_menubar = 0;
2530 GList *iter2;
2531 widget_value *cur;
2533 /* See if the changed entry (val) is present later in the menu bar */
2534 for (iter2 = iter;
2535 iter2 && ! val_in_menubar;
2536 iter2 = g_list_next (iter2))
2538 witem2 = GTK_MENU_ITEM (iter2->data);
2539 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2542 /* See if the current entry (iter) is present later in the
2543 specification for the new menu bar. */
2544 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2545 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2547 if (val_in_menubar && ! iter_in_new_menubar)
2549 int nr = pos;
2551 /* This corresponds to:
2552 Current: A B C
2553 New: A C
2554 Remove B. */
2556 g_object_ref (G_OBJECT (witem));
2557 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2558 gtk_widget_destroy (GTK_WIDGET (witem));
2560 /* Must get new list since the old changed. */
2561 g_list_free (*list);
2562 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2563 while (nr-- > 0) iter = g_list_next (iter);
2565 else if (! val_in_menubar && ! iter_in_new_menubar)
2567 /* This corresponds to:
2568 Current: A B C
2569 New: A X C
2570 Rename B to X. This might seem to be a strange thing to do,
2571 since if there is a menu under B it will be totally wrong for X.
2572 But consider editing a C file. Then there is a C-mode menu
2573 (corresponds to B above).
2574 If then doing C-x C-f the minibuf menu (X above) replaces the
2575 C-mode menu. When returning from the minibuffer, we get
2576 back the C-mode menu. Thus we do:
2577 Rename B to X (C-mode to minibuf menu)
2578 Rename X to B (minibuf to C-mode menu).
2579 If the X menu hasn't been invoked, the menu under B
2580 is up to date when leaving the minibuffer. */
2581 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2582 char *utf8_label = get_utf8_string (val->name);
2583 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2585 gtk_label_set_text (wlabel, utf8_label);
2587 /* If this item has a submenu that has been detached, change
2588 the title in the WM decorations also. */
2589 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2590 /* Set the title of the detached window. */
2591 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2593 iter = g_list_next (iter);
2594 val = val->next;
2595 ++pos;
2597 else if (! val_in_menubar && iter_in_new_menubar)
2599 /* This corresponds to:
2600 Current: A B C
2601 New: A X B C
2602 Insert X. */
2604 int nr = pos;
2605 GSList *group = 0;
2606 GtkWidget *w = xg_create_one_menuitem (val,
2608 select_cb,
2609 highlight_cb,
2610 cl_data,
2611 &group);
2613 /* Create a possibly empty submenu for menu bar items, since some
2614 themes don't highlight items correctly without it. */
2615 GtkWidget *submenu = create_menus (NULL, f,
2616 select_cb, deactivate_cb,
2617 highlight_cb,
2618 0, 0, 0, 0, cl_data, 0);
2619 gtk_widget_set_name (w, MENU_ITEM_NAME);
2620 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2621 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2623 g_list_free (*list);
2624 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2625 while (nr-- > 0) iter = g_list_next (iter);
2626 iter = g_list_next (iter);
2627 val = val->next;
2628 ++pos;
2630 else /* if (val_in_menubar && iter_in_new_menubar) */
2632 int nr = pos;
2633 /* This corresponds to:
2634 Current: A B C
2635 New: A C B
2636 Move C before B */
2638 g_object_ref (G_OBJECT (witem2));
2639 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2640 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2641 GTK_WIDGET (witem2), pos);
2642 g_object_unref (G_OBJECT (witem2));
2644 g_list_free (*list);
2645 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2646 while (nr-- > 0) iter = g_list_next (iter);
2647 if (iter) iter = g_list_next (iter);
2648 val = val->next;
2649 ++pos;
2653 /* Update the rest of the menu bar. */
2654 xg_update_menubar (menubar, f, list, iter, pos, val,
2655 select_cb, deactivate_cb, highlight_cb, cl_data);
2658 /* Update the menu item W so it corresponds to VAL.
2659 SELECT_CB is the callback to use when a menu item is selected.
2660 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2661 CL_DATA is the data to set in the widget for menu invocation. */
2663 static void
2664 xg_update_menu_item (widget_value *val,
2665 GtkWidget *w,
2666 GCallback select_cb,
2667 GCallback highlight_cb,
2668 xg_menu_cb_data *cl_data)
2670 GtkWidget *wchild;
2671 GtkLabel *wlbl = 0;
2672 GtkLabel *wkey = 0;
2673 char *utf8_label;
2674 char *utf8_key;
2675 const char *old_label = 0;
2676 const char *old_key = 0;
2677 xg_menu_item_cb_data *cb_data;
2679 wchild = gtk_bin_get_child (GTK_BIN (w));
2680 utf8_label = get_utf8_string (val->name);
2681 utf8_key = get_utf8_string (val->key);
2683 /* See if W is a menu item with a key. See make_menu_item above. */
2684 if (GTK_IS_HBOX (wchild))
2686 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2688 wlbl = GTK_LABEL (list->data);
2689 wkey = GTK_LABEL (list->next->data);
2690 g_list_free (list);
2692 if (! utf8_key)
2694 /* Remove the key and keep just the label. */
2695 g_object_ref (G_OBJECT (wlbl));
2696 gtk_container_remove (GTK_CONTAINER (w), wchild);
2697 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2698 g_object_unref (G_OBJECT (wlbl));
2699 wkey = 0;
2703 else /* Just a label. */
2705 wlbl = GTK_LABEL (wchild);
2707 /* Check if there is now a key. */
2708 if (utf8_key)
2710 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2711 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2713 wlbl = GTK_LABEL (list->data);
2714 wkey = GTK_LABEL (list->next->data);
2715 g_list_free (list);
2717 gtk_container_remove (GTK_CONTAINER (w), wchild);
2718 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2723 if (wkey) old_key = gtk_label_get_label (wkey);
2724 if (wlbl) old_label = gtk_label_get_label (wlbl);
2726 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2727 gtk_label_set_text (wkey, utf8_key);
2729 if (! old_label || strcmp (utf8_label, old_label) != 0)
2730 gtk_label_set_text (wlbl, utf8_label);
2732 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2733 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2735 if (! val->enabled && gtk_widget_get_sensitive (w))
2736 gtk_widget_set_sensitive (w, FALSE);
2737 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2738 gtk_widget_set_sensitive (w, TRUE);
2740 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2741 XG_ITEM_DATA);
2742 if (cb_data)
2744 cb_data->call_data = val->call_data;
2745 cb_data->help = val->help;
2746 cb_data->cl_data = cl_data;
2748 /* We assume the callback functions don't change. */
2749 if (val->call_data && ! val->contents)
2751 /* This item shall have a select callback. */
2752 if (! cb_data->select_id)
2753 cb_data->select_id
2754 = g_signal_connect (G_OBJECT (w), "activate",
2755 select_cb, cb_data);
2757 else if (cb_data->select_id)
2759 g_signal_handler_disconnect (w, cb_data->select_id);
2760 cb_data->select_id = 0;
2765 /* Update the toggle menu item W so it corresponds to VAL. */
2767 static void
2768 xg_update_toggle_item (widget_value *val, GtkWidget *w)
2770 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2773 /* Update the radio menu item W so it corresponds to VAL. */
2775 static void
2776 xg_update_radio_item (widget_value *val, GtkWidget *w)
2778 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2781 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2782 SUBMENU may be NULL, in that case a new menu is created.
2783 F is the frame the menu bar belongs to.
2784 VAL describes the contents of the menu bar.
2785 SELECT_CB is the callback to use when a menu item is selected.
2786 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2787 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2788 CL_DATA is the call back data to use for any newly created items.
2790 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2791 was NULL. */
2793 static GtkWidget *
2794 xg_update_submenu (GtkWidget *submenu,
2795 FRAME_PTR f,
2796 widget_value *val,
2797 GCallback select_cb,
2798 GCallback deactivate_cb,
2799 GCallback highlight_cb,
2800 xg_menu_cb_data *cl_data)
2802 GtkWidget *newsub = submenu;
2803 GList *list = 0;
2804 GList *iter;
2805 widget_value *cur;
2806 int has_tearoff_p = 0;
2807 GList *first_radio = 0;
2809 if (submenu)
2810 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2812 for (cur = val, iter = list;
2813 cur && iter;
2814 iter = g_list_next (iter), cur = cur->next)
2816 GtkWidget *w = GTK_WIDGET (iter->data);
2818 /* Skip tearoff items, they have no counterpart in val. */
2819 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2821 has_tearoff_p = 1;
2822 iter = g_list_next (iter);
2823 if (iter) w = GTK_WIDGET (iter->data);
2824 else break;
2827 /* Remember first radio button in a group. If we get a mismatch in
2828 a radio group we must rebuild the whole group so that the connections
2829 in GTK becomes correct. */
2830 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2831 first_radio = iter;
2832 else if (cur->button_type != BUTTON_TYPE_RADIO
2833 && ! GTK_IS_RADIO_MENU_ITEM (w))
2834 first_radio = 0;
2836 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2838 if (! xg_separator_p (cur->name))
2839 break;
2841 else if (GTK_IS_CHECK_MENU_ITEM (w))
2843 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2844 break;
2845 xg_update_toggle_item (cur, w);
2846 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2848 else if (GTK_IS_RADIO_MENU_ITEM (w))
2850 if (cur->button_type != BUTTON_TYPE_RADIO)
2851 break;
2852 xg_update_radio_item (cur, w);
2853 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2855 else if (GTK_IS_MENU_ITEM (w))
2857 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2858 GtkWidget *sub;
2860 if (cur->button_type != BUTTON_TYPE_NONE ||
2861 xg_separator_p (cur->name))
2862 break;
2864 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2866 sub = gtk_menu_item_get_submenu (witem);
2867 if (sub && ! cur->contents)
2869 /* Not a submenu anymore. */
2870 g_object_ref (G_OBJECT (sub));
2871 remove_submenu (witem);
2872 gtk_widget_destroy (sub);
2874 else if (cur->contents)
2876 GtkWidget *nsub;
2878 nsub = xg_update_submenu (sub, f, cur->contents,
2879 select_cb, deactivate_cb,
2880 highlight_cb, cl_data);
2882 /* If this item just became a submenu, we must set it. */
2883 if (nsub != sub)
2884 gtk_menu_item_set_submenu (witem, nsub);
2887 else
2889 /* Structural difference. Remove everything from here and down
2890 in SUBMENU. */
2891 break;
2895 /* Remove widgets from first structual change. */
2896 if (iter)
2898 /* If we are adding new menu items below, we must remove from
2899 first radio button so that radio groups become correct. */
2900 if (cur && first_radio) xg_destroy_widgets (first_radio);
2901 else xg_destroy_widgets (iter);
2904 if (cur)
2906 /* More items added. Create them. */
2907 newsub = create_menus (cur,
2909 select_cb,
2910 deactivate_cb,
2911 highlight_cb,
2914 ! has_tearoff_p,
2915 submenu,
2916 cl_data,
2920 if (list) g_list_free (list);
2922 return newsub;
2925 /* Update the MENUBAR.
2926 F is the frame the menu bar belongs to.
2927 VAL describes the contents of the menu bar.
2928 If DEEP_P is non-zero, rebuild all but the top level menu names in
2929 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2930 SELECT_CB is the callback to use when a menu item is selected.
2931 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2932 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2934 void
2935 xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
2936 int deep_p,
2937 GCallback select_cb, GCallback deactivate_cb,
2938 GCallback highlight_cb)
2940 xg_menu_cb_data *cl_data;
2941 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2943 if (! list) return;
2945 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2946 XG_FRAME_DATA);
2948 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2949 select_cb, deactivate_cb, highlight_cb, cl_data);
2951 if (deep_p)
2953 widget_value *cur;
2955 /* Update all sub menus.
2956 We must keep the submenus (GTK menu item widgets) since the
2957 X Window in the XEvent that activates the menu are those widgets. */
2959 /* Update cl_data, menu_item things in F may have changed. */
2960 update_cl_data (cl_data, f, highlight_cb);
2962 for (cur = val->contents; cur; cur = cur->next)
2964 GList *iter;
2965 GtkWidget *sub = 0;
2966 GtkWidget *newsub;
2967 GtkMenuItem *witem = 0;
2969 /* Find sub menu that corresponds to val and update it. */
2970 for (iter = list ; iter; iter = g_list_next (iter))
2972 witem = GTK_MENU_ITEM (iter->data);
2973 if (xg_item_label_same_p (witem, cur->name))
2975 sub = gtk_menu_item_get_submenu (witem);
2976 break;
2980 newsub = xg_update_submenu (sub,
2982 cur->contents,
2983 select_cb,
2984 deactivate_cb,
2985 highlight_cb,
2986 cl_data);
2987 /* sub may still be NULL. If we just updated non deep and added
2988 a new menu bar item, it has no sub menu yet. So we set the
2989 newly created sub menu under witem. */
2990 if (newsub != sub && witem != 0)
2992 xg_set_screen (newsub, f);
2993 gtk_menu_item_set_submenu (witem, newsub);
2998 g_list_free (list);
2999 gtk_widget_show_all (menubar);
3002 /* Recompute all the widgets of frame F, when the menu bar has been
3003 changed. Value is non-zero if widgets were updated. */
3006 xg_update_frame_menubar (FRAME_PTR f)
3008 struct x_output *x = f->output_data.x;
3009 GtkRequisition req;
3011 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
3012 return 0;
3014 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
3015 return 0; /* Already done this, happens for frames created invisible. */
3017 BLOCK_INPUT;
3019 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
3020 FALSE, FALSE, 0);
3021 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
3023 gtk_widget_show_all (x->menubar_widget);
3024 gtk_widget_size_request (x->menubar_widget, &req);
3025 FRAME_MENUBAR_HEIGHT (f) = req.height;
3026 xg_height_or_width_changed (f);
3027 UNBLOCK_INPUT;
3029 return 1;
3032 /* Get rid of the menu bar of frame F, and free its storage.
3033 This is used when deleting a frame, and when turning off the menu bar. */
3035 void
3036 free_frame_menubar (FRAME_PTR f)
3038 struct x_output *x = f->output_data.x;
3040 if (x->menubar_widget)
3042 BLOCK_INPUT;
3044 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3045 /* The menubar and its children shall be deleted when removed from
3046 the container. */
3047 x->menubar_widget = 0;
3048 FRAME_MENUBAR_HEIGHT (f) = 0;
3049 xg_height_or_width_changed (f);
3050 UNBLOCK_INPUT;
3055 xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
3057 struct x_output *x = f->output_data.x;
3058 GList *iter;
3059 GdkRectangle rec;
3060 GList *list;
3061 GdkDisplay *gdpy;
3062 GdkWindow *gw;
3063 GdkEvent gevent;
3064 GtkWidget *gwdesc;
3066 if (! x->menubar_widget) return 0;
3068 if (! (event->xbutton.x >= 0
3069 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
3070 && event->xbutton.y >= 0
3071 && event->xbutton.y < f->output_data.x->menubar_height
3072 && event->xbutton.same_screen))
3073 return 0;
3075 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3076 gw = gdk_xid_table_lookup_for_display (gdpy, event->xbutton.window);
3077 if (! gw) return 0;
3078 gevent.any.window = gw;
3079 gwdesc = gtk_get_event_widget (&gevent);
3080 if (! gwdesc) return 0;
3081 if (! GTK_IS_MENU_BAR (gwdesc)
3082 && ! GTK_IS_MENU_ITEM (gwdesc)
3083 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
3084 return 0;
3086 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
3087 if (! list) return 0;
3088 rec.x = event->xbutton.x;
3089 rec.y = event->xbutton.y;
3090 rec.width = 1;
3091 rec.height = 1;
3093 for (iter = list ; iter; iter = g_list_next (iter))
3095 GtkWidget *w = GTK_WIDGET (iter->data);
3096 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
3097 break;
3099 g_list_free (list);
3100 return iter == 0 ? 0 : 1;
3105 /***********************************************************************
3106 Scroll bar functions
3107 ***********************************************************************/
3110 /* Setting scroll bar values invokes the callback. Use this variable
3111 to indicate that callback should do nothing. */
3113 int xg_ignore_gtk_scrollbar;
3115 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3116 may be larger than 32 bits. Keep a mapping from integer index to widget
3117 pointers to get around the 32 bit limitation. */
3119 static struct
3121 GtkWidget **widgets;
3122 int max_size;
3123 int used;
3124 } id_to_widget;
3126 /* Grow this much every time we need to allocate more */
3128 #define ID_TO_WIDGET_INCR 32
3130 /* Store the widget pointer W in id_to_widget and return the integer index. */
3132 static int
3133 xg_store_widget_in_map (GtkWidget *w)
3135 int i;
3137 if (id_to_widget.max_size == id_to_widget.used)
3139 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3141 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3142 sizeof (GtkWidget *)*new_size);
3144 for (i = id_to_widget.max_size; i < new_size; ++i)
3145 id_to_widget.widgets[i] = 0;
3146 id_to_widget.max_size = new_size;
3149 /* Just loop over the array and find a free place. After all,
3150 how many scroll bars are we creating? Should be a small number.
3151 The check above guarantees we will find a free place. */
3152 for (i = 0; i < id_to_widget.max_size; ++i)
3154 if (! id_to_widget.widgets[i])
3156 id_to_widget.widgets[i] = w;
3157 ++id_to_widget.used;
3159 return i;
3163 /* Should never end up here */
3164 abort ();
3167 /* Remove pointer at IDX from id_to_widget.
3168 Called when scroll bar is destroyed. */
3170 static void
3171 xg_remove_widget_from_map (int idx)
3173 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3175 id_to_widget.widgets[idx] = 0;
3176 --id_to_widget.used;
3180 /* Get the widget pointer at IDX from id_to_widget. */
3182 static GtkWidget *
3183 xg_get_widget_from_map (int idx)
3185 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3186 return id_to_widget.widgets[idx];
3188 return 0;
3191 /* Return the scrollbar id for X Window WID on display DPY.
3192 Return -1 if WID not in id_to_widget. */
3195 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3197 int idx;
3198 GtkWidget *w;
3200 w = xg_win_to_widget (dpy, wid);
3202 if (w)
3204 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3205 if (id_to_widget.widgets[idx] == w)
3206 return idx;
3209 return -1;
3212 /* Callback invoked when scroll bar WIDGET is destroyed.
3213 DATA is the index into id_to_widget for WIDGET.
3214 We free pointer to last scroll bar values here and remove the index. */
3216 static void
3217 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3219 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3220 xg_remove_widget_from_map (id);
3223 /* Create a scroll bar widget for frame F. Store the scroll bar
3224 in BAR.
3225 SCROLL_CALLBACK is the callback to invoke when the value of the
3226 bar changes.
3227 END_CALLBACK is the callback to invoke when scrolling ends.
3228 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3229 to set resources for the widget. */
3231 void
3232 xg_create_scroll_bar (FRAME_PTR f,
3233 struct scroll_bar *bar,
3234 GCallback scroll_callback,
3235 GCallback end_callback,
3236 const char *scroll_bar_name)
3238 GtkWidget *wscroll;
3239 GtkWidget *webox;
3240 GtkObject *vadj;
3241 int scroll_id;
3243 /* Page, step increment values are not so important here, they
3244 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3245 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3246 0.1, 0.1, 0.1);
3248 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3249 webox = gtk_event_box_new ();
3250 gtk_widget_set_name (wscroll, scroll_bar_name);
3251 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3252 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3254 scroll_id = xg_store_widget_in_map (wscroll);
3256 /* The EMACS_INT cast avoids a warning. */
3257 g_signal_connect (G_OBJECT (wscroll),
3258 "destroy",
3259 G_CALLBACK (xg_gtk_scroll_destroy),
3260 (gpointer) (EMACS_INT) scroll_id);
3261 g_signal_connect (G_OBJECT (wscroll),
3262 "change-value",
3263 scroll_callback,
3264 (gpointer) bar);
3265 g_signal_connect (G_OBJECT (wscroll),
3266 "button-release-event",
3267 end_callback,
3268 (gpointer) bar);
3270 /* The scroll bar widget does not draw on a window of its own. Instead
3271 it draws on the parent window, in this case the edit widget. So
3272 whenever the edit widget is cleared, the scroll bar needs to redraw
3273 also, which causes flicker. Put an event box between the edit widget
3274 and the scroll bar, so the scroll bar instead draws itself on the
3275 event box window. */
3276 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3277 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3280 /* Set the cursor to an arrow. */
3281 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3283 bar->x_window = scroll_id;
3286 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3288 void
3289 xg_remove_scroll_bar (FRAME_PTR f, int scrollbar_id)
3291 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3292 if (w)
3294 GtkWidget *wparent = gtk_widget_get_parent (w);
3295 gtk_widget_destroy (w);
3296 gtk_widget_destroy (wparent);
3297 SET_FRAME_GARBAGED (f);
3301 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3302 in frame F.
3303 TOP/LEFT are the new pixel positions where the bar shall appear.
3304 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3306 void
3307 xg_update_scrollbar_pos (FRAME_PTR f,
3308 int scrollbar_id,
3309 int top,
3310 int left,
3311 int width,
3312 int height)
3315 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3317 if (wscroll)
3319 GtkWidget *wfixed = f->output_data.x->edit_widget;
3320 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3321 gint msl;
3323 /* Clear out old position. */
3324 int oldx = -1, oldy = -1, oldw, oldh;
3325 if (gtk_widget_get_parent (wparent) == wfixed)
3327 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3328 "x", &oldx, "y", &oldy, NULL);
3329 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3332 /* Move and resize to new values. */
3333 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3334 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3335 if (msl > height)
3337 /* No room. Hide scroll bar as some themes output a warning if
3338 the height is less than the min size. */
3339 gtk_widget_hide (wparent);
3340 gtk_widget_hide (wscroll);
3342 else
3344 gtk_widget_show_all (wparent);
3345 gtk_widget_set_size_request (wscroll, width, height);
3347 gtk_widget_queue_draw (wfixed);
3348 gdk_window_process_all_updates ();
3349 if (oldx != -1 && oldw > 0 && oldh > 0)
3351 /* Clear under old scroll bar position. This must be done after
3352 the gtk_widget_queue_draw and gdk_window_process_all_updates
3353 above. */
3354 x_clear_area (FRAME_X_DISPLAY (f),
3355 FRAME_X_WINDOW (f),
3356 oldx, oldy, oldw, oldh, 0);
3359 /* GTK does not redraw until the main loop is entered again, but
3360 if there are no X events pending we will not enter it. So we sync
3361 here to get some events. */
3363 x_sync (f);
3364 SET_FRAME_GARBAGED (f);
3365 cancel_mouse_face (f);
3369 /* Set the thumb size and position of scroll bar BAR. We are currently
3370 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3372 void
3373 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3374 int portion,
3375 int position,
3376 int whole)
3378 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3380 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3382 if (wscroll && NILP (bar->dragging))
3384 GtkAdjustment *adj;
3385 gdouble shown;
3386 gdouble top;
3387 int size, value;
3388 int new_step;
3389 int changed = 0;
3391 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3393 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3394 rather than the real portion value. This makes the thumb less likely
3395 to resize and that looks better. */
3396 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3397 /* When the thumb is at the bottom, position == whole.
3398 So we need to increase `whole' to make space for the thumb. */
3399 whole += portion;
3401 if (whole <= 0)
3402 top = 0, shown = 1;
3403 else
3405 top = (gdouble) position / whole;
3406 shown = (gdouble) portion / whole;
3409 size = shown * XG_SB_RANGE;
3410 size = min (size, XG_SB_RANGE);
3411 size = max (size, 1);
3413 value = top * XG_SB_RANGE;
3414 value = min (value, XG_SB_MAX - size);
3415 value = max (value, XG_SB_MIN);
3417 /* Assume all lines are of equal size. */
3418 new_step = size / max (1, FRAME_LINES (f));
3420 if ((int) gtk_adjustment_get_page_size (adj) != size
3421 || (int) gtk_adjustment_get_step_increment (adj) != new_step)
3423 gtk_adjustment_set_page_size (adj, size);
3424 gtk_adjustment_set_step_increment (adj, new_step);
3425 /* Assume a page increment is about 95% of the page size */
3426 gtk_adjustment_set_page_increment (adj,(int) (0.95*size));
3427 changed = 1;
3430 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3432 BLOCK_INPUT;
3434 /* gtk_range_set_value invokes the callback. Set
3435 ignore_gtk_scrollbar to make the callback do nothing */
3436 xg_ignore_gtk_scrollbar = 1;
3438 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3439 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3440 else if (changed)
3441 gtk_adjustment_changed (adj);
3443 xg_ignore_gtk_scrollbar = 0;
3445 UNBLOCK_INPUT;
3450 /* Return non-zero if EVENT is for a scroll bar in frame F.
3451 When the same X window is used for several Gtk+ widgets, we cannot
3452 say for sure based on the X window alone if an event is for the
3453 frame. This function does additional checks.
3455 Return non-zero if the event is for a scroll bar, zero otherwise. */
3458 xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
3460 int retval = 0;
3462 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3464 /* Check if press occurred outside the edit widget. */
3465 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3466 retval = gdk_display_get_window_at_pointer (gdpy, NULL, NULL)
3467 != gtk_widget_get_window (f->output_data.x->edit_widget);
3469 else if (f
3470 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3471 || event->type == MotionNotify))
3473 /* If we are releasing or moving the scroll bar, it has the grab. */
3474 GtkWidget *w = gtk_grab_get_current ();
3475 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3478 return retval;
3483 /***********************************************************************
3484 Tool bar functions
3485 ***********************************************************************/
3486 /* The key for the data we put in the GtkImage widgets. The data is
3487 the image used by Emacs. We use this to see if we need to update
3488 the GtkImage with a new image. */
3489 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3491 /* The key for storing the latest modifiers so the activate callback can
3492 get them. */
3493 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3495 /* The key for storing the button widget in its proxy menu item. */
3496 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3498 /* The key for the data we put in the GtkImage widgets. The data is
3499 the stock name used by Emacs. We use this to see if we need to update
3500 the GtkImage with a new image. */
3501 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3503 /* As above, but this is used for named theme widgets, as opposed to
3504 stock items. */
3505 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3507 /* Callback function invoked when a tool bar item is pressed.
3508 W is the button widget in the tool bar that got pressed,
3509 CLIENT_DATA is an integer that is the index of the button in the
3510 tool bar. 0 is the first button. */
3512 static gboolean
3513 xg_tool_bar_button_cb (GtkWidget *widget,
3514 GdkEventButton *event,
3515 gpointer user_data)
3517 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3518 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3519 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3520 return FALSE;
3524 /* Callback function invoked when a tool bar item is pressed.
3525 W is the button widget in the tool bar that got pressed,
3526 CLIENT_DATA is an integer that is the index of the button in the
3527 tool bar. 0 is the first button. */
3529 static void
3530 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3532 /* The EMACS_INT cast avoids a warning. */
3533 int idx = (int) (EMACS_INT) client_data;
3534 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3535 XG_TOOL_BAR_LAST_MODIFIER);
3537 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3538 Lisp_Object key, frame;
3539 struct input_event event;
3540 EVENT_INIT (event);
3542 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3543 return;
3545 idx *= TOOL_BAR_ITEM_NSLOTS;
3547 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3548 XSETFRAME (frame, f);
3550 /* We generate two events here. The first one is to set the prefix
3551 to `(tool_bar)', see keyboard.c. */
3552 event.kind = TOOL_BAR_EVENT;
3553 event.frame_or_window = frame;
3554 event.arg = frame;
3555 kbd_buffer_store_event (&event);
3557 event.kind = TOOL_BAR_EVENT;
3558 event.frame_or_window = frame;
3559 event.arg = key;
3560 /* Convert between the modifier bits GDK uses and the modifier bits
3561 Emacs uses. This assumes GDK and X masks are the same, which they are when
3562 this is written. */
3563 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3564 kbd_buffer_store_event (&event);
3566 /* Return focus to the frame after we have clicked on a detached
3567 tool bar button. */
3568 Fx_focus_frame (frame);
3571 /* Callback function invoked when a tool bar item is pressed in a detached
3572 tool bar or the overflow drop down menu.
3573 We just call xg_tool_bar_callback.
3574 W is the menu item widget that got pressed,
3575 CLIENT_DATA is an integer that is the index of the button in the
3576 tool bar. 0 is the first button. */
3578 static void
3579 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3581 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3582 XG_TOOL_BAR_PROXY_BUTTON));
3583 xg_tool_bar_callback (wbutton, client_data);
3587 static gboolean
3588 xg_tool_bar_help_callback (GtkWidget *w,
3589 GdkEventCrossing *event,
3590 gpointer client_data);
3592 /* This callback is called when a help is to be shown for an item in
3593 the detached tool bar when the detached tool bar it is not expanded. */
3595 static gboolean
3596 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3597 GdkEventCrossing *event,
3598 gpointer client_data)
3600 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3601 XG_TOOL_BAR_PROXY_BUTTON));
3603 return xg_tool_bar_help_callback (wbutton, event, client_data);
3606 static GtkWidget *
3607 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3609 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3610 GtkWidget *c1 = (GtkWidget *) clist->data;
3611 GtkWidget *c2 = (GtkWidget *) clist->next->data;
3612 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3613 g_list_free (clist);
3614 return GTK_IS_LABEL (c1) ? c1 : c2;
3618 /* This callback is called when a tool item should create a proxy item,
3619 such as for the overflow menu. Also called when the tool bar is detached.
3620 If we don't create a proxy menu item, the detached tool bar will be
3621 blank. */
3623 static gboolean
3624 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3626 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3627 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3628 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
3629 GtkWidget *c1;
3630 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3631 GtkImage *wimage = GTK_IMAGE (c1);
3632 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3633 (gtk_label_get_text (wlbl));
3634 GtkWidget *wmenuimage;
3637 if (gtk_button_get_use_stock (wbutton))
3638 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3639 GTK_ICON_SIZE_MENU);
3640 else
3642 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3643 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3645 if (store_type == GTK_IMAGE_STOCK)
3647 gchar *stock_id;
3648 gtk_image_get_stock (wimage, &stock_id, NULL);
3649 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3651 else if (store_type == GTK_IMAGE_ICON_SET)
3653 GtkIconSet *icon_set;
3654 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3655 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3656 GTK_ICON_SIZE_MENU);
3658 else if (store_type == GTK_IMAGE_PIXBUF)
3660 gint width, height;
3662 if (settings &&
3663 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3664 &width, &height))
3666 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3668 src_pixbuf = gtk_image_get_pixbuf (wimage);
3669 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3670 GDK_INTERP_BILINEAR);
3672 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3674 else
3676 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3677 abort ();
3680 else if (store_type == GTK_IMAGE_ICON_NAME)
3682 const gchar *icon_name;
3683 GtkIconSize icon_size;
3685 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3686 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3687 GTK_ICON_SIZE_MENU);
3689 else
3691 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3692 abort ();
3695 if (wmenuimage)
3696 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3698 g_signal_connect (G_OBJECT (wmenuitem),
3699 "activate",
3700 G_CALLBACK (xg_tool_bar_proxy_callback),
3701 user_data);
3704 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3705 (gpointer) wbutton);
3706 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3707 gtk_widget_set_sensitive (wmenuitem,
3708 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
3710 /* Use enter/leave notify to show help. We use the events
3711 rather than the GtkButton specific signals "enter" and
3712 "leave", so we can have only one callback. The event
3713 will tell us what kind of event it is. */
3714 g_signal_connect (G_OBJECT (wmenuitem),
3715 "enter-notify-event",
3716 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3717 user_data);
3718 g_signal_connect (G_OBJECT (wmenuitem),
3719 "leave-notify-event",
3720 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3721 user_data);
3723 return TRUE;
3726 /* This callback is called when a tool bar is detached. We must set
3727 the height of the tool bar to zero when this happens so frame sizes
3728 are correctly calculated.
3729 WBOX is the handle box widget that enables detach/attach of the tool bar.
3730 W is the tool bar widget.
3731 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3733 static void
3734 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
3735 GtkWidget *w,
3736 gpointer client_data)
3738 FRAME_PTR f = (FRAME_PTR) client_data;
3739 extern int x_gtk_whole_detached_tool_bar;
3741 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3742 NULL);
3744 if (f)
3746 GtkRequisition req, req2;
3747 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3748 gtk_widget_size_request (GTK_WIDGET (wbox), &req);
3749 gtk_widget_size_request (w, &req2);
3750 req.width -= req2.width;
3751 req.height -= req2.height;
3752 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3753 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3754 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3755 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3756 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3757 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3758 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3759 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3760 xg_height_or_width_changed (f);
3764 /* This callback is called when a tool bar is reattached. We must set
3765 the height of the tool bar when this happens so frame sizes
3766 are correctly calculated.
3767 WBOX is the handle box widget that enables detach/attach of the tool bar.
3768 W is the tool bar widget.
3769 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3771 static void
3772 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
3773 GtkWidget *w,
3774 gpointer client_data)
3776 FRAME_PTR f = (FRAME_PTR) client_data;
3777 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3779 if (f)
3781 GtkRequisition req, req2;
3782 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3783 gtk_widget_size_request (GTK_WIDGET (wbox), &req);
3784 gtk_widget_size_request (w, &req2);
3785 req.width += req2.width;
3786 req.height += req2.height;
3787 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3788 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3789 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3790 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3791 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3792 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3793 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3794 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3795 xg_height_or_width_changed (f);
3799 /* This callback is called when the mouse enters or leaves a tool bar item.
3800 It is used for displaying and hiding the help text.
3801 W is the tool bar item, a button.
3802 EVENT is either an enter event or leave event.
3803 CLIENT_DATA is an integer that is the index of the button in the
3804 tool bar. 0 is the first button.
3806 Returns FALSE to tell GTK to keep processing this event. */
3808 static gboolean
3809 xg_tool_bar_help_callback (GtkWidget *w,
3810 GdkEventCrossing *event,
3811 gpointer client_data)
3813 /* The EMACS_INT cast avoids a warning. */
3814 int idx = (int) (EMACS_INT) client_data;
3815 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3816 Lisp_Object help, frame;
3818 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3819 return FALSE;
3821 if (event->type == GDK_ENTER_NOTIFY)
3823 idx *= TOOL_BAR_ITEM_NSLOTS;
3824 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3826 if (NILP (help))
3827 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3829 else
3830 help = Qnil;
3832 XSETFRAME (frame, f);
3833 kbd_buffer_store_help_event (frame, help);
3835 return FALSE;
3839 /* This callback is called when a tool bar item shall be redrawn.
3840 It modifies the expose event so that the GtkImage widget redraws the
3841 whole image. This to overcome a bug that makes GtkImage draw the image
3842 in the wrong place when it tries to redraw just a part of the image.
3843 W is the GtkImage to be redrawn.
3844 EVENT is the expose event for W.
3845 CLIENT_DATA is unused.
3847 Returns FALSE to tell GTK to keep processing this event. */
3849 static gboolean
3850 xg_tool_bar_item_expose_callback (GtkWidget *w,
3851 GdkEventExpose *event,
3852 gpointer client_data)
3854 gint width, height;
3856 gdk_drawable_get_size (event->window, &width, &height);
3858 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3859 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3861 event->area.x = max (0, event->area.x);
3862 event->area.y = max (0, event->area.y);
3864 event->area.width = max (width, event->area.width);
3865 event->area.height = max (height, event->area.height);
3867 return FALSE;
3870 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
3871 #define toolbar_set_orientation(w, o) \
3872 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
3873 #else
3874 #define toolbar_set_orientation(w, o) \
3875 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
3876 #endif
3878 /* Attach a tool bar to frame F. */
3880 static void
3881 xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
3883 struct x_output *x = f->output_data.x;
3884 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
3886 toolbar_set_orientation (x->toolbar_widget,
3887 into_hbox
3888 ? GTK_ORIENTATION_VERTICAL
3889 : GTK_ORIENTATION_HORIZONTAL);
3890 if (!x->handlebox_widget)
3892 x->handlebox_widget = gtk_handle_box_new ();
3893 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3894 G_CALLBACK (xg_tool_bar_detach_callback), f);
3895 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3896 G_CALLBACK (xg_tool_bar_attach_callback), f);
3897 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3898 x->toolbar_widget);
3901 if (into_hbox)
3903 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
3904 FALSE, FALSE, 0);
3906 if (EQ (pos, Qleft))
3907 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
3908 x->handlebox_widget,
3910 x->toolbar_in_hbox = 1;
3912 else
3914 int vbox_pos = x->menubar_widget ? 1 : 0;
3915 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3916 FALSE, FALSE, 0);
3918 if (EQ (pos, Qtop))
3919 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
3920 x->handlebox_widget,
3921 vbox_pos);
3922 x->toolbar_in_hbox = 0;
3926 /* Create a tool bar for frame F. */
3928 static void
3929 xg_create_tool_bar (FRAME_PTR f)
3931 struct x_output *x = f->output_data.x;
3933 x->toolbar_widget = gtk_toolbar_new ();
3934 x->toolbar_detached = 0;
3936 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3938 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3939 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
3943 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3945 /* Find the right-to-left image named by RTL in the tool bar images for F.
3946 Returns IMAGE if RTL is not found. */
3948 static Lisp_Object
3949 find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
3951 int i;
3952 Lisp_Object file, rtl_name;
3953 struct gcpro gcpro1, gcpro2;
3954 GCPRO2 (file, rtl_name);
3956 rtl_name = Ffile_name_nondirectory (rtl);
3958 for (i = 0; i < f->n_tool_bar_items; ++i)
3960 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3961 if (!NILP (file = file_for_image (rtl_image)))
3963 file = call1 (intern ("file-name-sans-extension"),
3964 Ffile_name_nondirectory (file));
3965 if (EQ (Fequal (file, rtl_name), Qt))
3967 image = rtl_image;
3968 break;
3973 return image;
3976 static GtkToolItem *
3977 xg_make_tool_item (FRAME_PTR f,
3978 GtkWidget *wimage,
3979 GtkWidget **wbutton,
3980 const char *label,
3981 int i)
3983 GtkToolItem *ti = gtk_tool_item_new ();
3984 Lisp_Object style = Ftool_bar_get_system_style ();
3985 int both_horiz = EQ (style, Qboth_horiz);
3986 int text_image = EQ (style, Qtext_image_horiz);
3988 GtkWidget *vb = both_horiz || text_image
3989 ? gtk_hbox_new (FALSE, 0) : gtk_vbox_new (FALSE, 0);
3990 GtkWidget *wb = gtk_button_new ();
3991 GtkWidget *weventbox = gtk_event_box_new ();
3993 if (wimage && ! text_image)
3994 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
3996 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
3998 if (wimage && text_image)
3999 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
4001 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
4002 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
4003 gtk_container_add (GTK_CONTAINER (wb), vb);
4004 gtk_container_add (GTK_CONTAINER (weventbox), wb);
4005 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4007 if (wimage)
4009 /* The EMACS_INT cast avoids a warning. */
4010 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4011 G_CALLBACK (xg_tool_bar_menu_proxy),
4012 (gpointer) (EMACS_INT) i);
4014 g_signal_connect (G_OBJECT (wb), "clicked",
4015 G_CALLBACK (xg_tool_bar_callback),
4016 (gpointer) (EMACS_INT) i);
4018 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4020 /* Catch expose events to overcome an annoying redraw bug, see
4021 comment for xg_tool_bar_item_expose_callback. */
4022 g_signal_connect (G_OBJECT (ti),
4023 "expose-event",
4024 G_CALLBACK (xg_tool_bar_item_expose_callback),
4027 gtk_tool_item_set_homogeneous (ti, FALSE);
4029 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4030 no distinction based on modifiers in the activate callback,
4031 so we have to do it ourselves. */
4032 g_signal_connect (wb, "button-release-event",
4033 G_CALLBACK (xg_tool_bar_button_cb),
4034 NULL);
4036 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
4038 /* Use enter/leave notify to show help. We use the events
4039 rather than the GtkButton specific signals "enter" and
4040 "leave", so we can have only one callback. The event
4041 will tell us what kind of event it is. */
4042 /* The EMACS_INT cast avoids a warning. */
4043 g_signal_connect (G_OBJECT (weventbox),
4044 "enter-notify-event",
4045 G_CALLBACK (xg_tool_bar_help_callback),
4046 (gpointer) (EMACS_INT) i);
4047 g_signal_connect (G_OBJECT (weventbox),
4048 "leave-notify-event",
4049 G_CALLBACK (xg_tool_bar_help_callback),
4050 (gpointer) (EMACS_INT) i);
4053 if (wbutton) *wbutton = wb;
4055 return ti;
4058 static void
4059 xg_show_toolbar_item (GtkToolItem *ti)
4061 Lisp_Object style = Ftool_bar_get_system_style ();
4062 int both_horiz = EQ (style, Qboth_horiz);
4063 int text_image = EQ (style, Qtext_image_horiz);
4065 int horiz = both_horiz || text_image;
4066 int show_label = ! EQ (style, Qimage);
4067 int show_image = ! EQ (style, Qtext);
4069 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (ti));
4070 GtkWidget *wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
4071 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
4072 GtkWidget *wimage;
4073 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4074 GtkWidget *new_box = NULL;
4076 if (GTK_IS_VBOX (vb) && horiz)
4077 new_box = gtk_hbox_new (FALSE, 0);
4078 else if (GTK_IS_HBOX (vb) && !horiz && show_label && show_image)
4079 new_box = gtk_vbox_new (FALSE, 0);
4081 if (!new_box && horiz)
4082 gtk_box_reorder_child (GTK_BOX (vb), wlbl, text_image ? 0 : 1);
4083 else if (new_box)
4085 g_object_ref (G_OBJECT (wimage));
4086 g_object_ref (G_OBJECT (wlbl));
4087 gtk_container_remove (GTK_CONTAINER (vb), wimage);
4088 gtk_container_remove (GTK_CONTAINER (vb), wlbl);
4089 gtk_widget_destroy (GTK_WIDGET (vb));
4090 if (! text_image)
4091 gtk_box_pack_start (GTK_BOX (new_box), wimage, TRUE, TRUE, 0);
4092 gtk_box_pack_start (GTK_BOX (new_box), wlbl, TRUE, TRUE, 0);
4093 if (text_image)
4094 gtk_box_pack_start (GTK_BOX (new_box), wimage, TRUE, TRUE, 0);
4095 gtk_container_add (GTK_CONTAINER (wbutton), new_box);
4096 g_object_unref (G_OBJECT (wimage));
4097 g_object_unref (G_OBJECT (wlbl));
4098 vb = new_box;
4101 if (show_label) gtk_widget_show (wlbl);
4102 else gtk_widget_hide (wlbl);
4103 if (show_image) gtk_widget_show (wimage);
4104 else gtk_widget_hide (wimage);
4105 gtk_widget_show (GTK_WIDGET (weventbox));
4106 gtk_widget_show (GTK_WIDGET (vb));
4107 gtk_widget_show (GTK_WIDGET (wbutton));
4108 gtk_widget_show (GTK_WIDGET (ti));
4111 static int
4112 xg_update_tool_bar_sizes (FRAME_PTR f)
4114 struct x_output *x = f->output_data.x;
4115 GtkRequisition req;
4116 int nl = 0, nr = 0, nt = 0, nb = 0;
4118 gtk_widget_size_request (GTK_WIDGET (x->handlebox_widget), &req);
4119 if (x->toolbar_in_hbox)
4121 int pos;
4122 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
4123 x->handlebox_widget,
4124 "position", &pos, NULL);
4125 if (pos == 0) nl = req.width;
4126 else nr = req.width;
4128 else
4130 int pos;
4131 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
4132 x->handlebox_widget,
4133 "position", &pos, NULL);
4134 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
4135 else nb = req.height;
4138 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
4139 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
4140 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
4141 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
4143 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
4144 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4145 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
4146 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
4147 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
4148 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
4149 return 1;
4152 return 0;
4156 /* Update the tool bar for frame F. Add new buttons and remove old. */
4158 void
4159 update_frame_tool_bar (FRAME_PTR f)
4161 int i;
4162 struct x_output *x = f->output_data.x;
4163 int hmargin = 0, vmargin = 0;
4164 GtkToolbar *wtoolbar;
4165 GtkToolItem *ti;
4166 GtkTextDirection dir;
4167 int pack_tool_bar = x->handlebox_widget == NULL;
4169 if (! FRAME_GTK_WIDGET (f))
4170 return;
4172 BLOCK_INPUT;
4174 if (INTEGERP (Vtool_bar_button_margin)
4175 && XINT (Vtool_bar_button_margin) > 0)
4177 hmargin = XFASTINT (Vtool_bar_button_margin);
4178 vmargin = XFASTINT (Vtool_bar_button_margin);
4180 else if (CONSP (Vtool_bar_button_margin))
4182 if (INTEGERP (XCAR (Vtool_bar_button_margin))
4183 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
4184 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4186 if (INTEGERP (XCDR (Vtool_bar_button_margin))
4187 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4188 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4191 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4192 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4193 i.e. zero. This means that margins less than
4194 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4195 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4196 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4198 if (! x->toolbar_widget)
4199 xg_create_tool_bar (f);
4201 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4202 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4204 for (i = 0; i < f->n_tool_bar_items; ++i)
4206 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4207 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4208 int idx;
4209 int img_id;
4210 int icon_size = 0;
4211 struct image *img = NULL;
4212 Lisp_Object image;
4213 Lisp_Object stock = Qnil;
4214 GtkStockItem stock_item;
4215 char *stock_name = NULL;
4216 char *icon_name = NULL;
4217 Lisp_Object rtl;
4218 GtkWidget *wbutton = NULL;
4219 GtkWidget *weventbox;
4220 Lisp_Object specified_file;
4221 char *label = SSDATA (PROP (TOOL_BAR_ITEM_LABEL));
4223 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), i);
4225 if (ti)
4227 weventbox = gtk_bin_get_child (GTK_BIN (ti));
4228 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
4232 image = PROP (TOOL_BAR_ITEM_IMAGES);
4234 /* Ignore invalid image specifications. */
4235 if (!valid_image_p (image))
4237 if (wbutton) gtk_widget_hide (wbutton);
4238 continue;
4241 specified_file = file_for_image (image);
4242 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4243 stock = call1 (Qx_gtk_map_stock, specified_file);
4245 if (STRINGP (stock))
4247 stock_name = SSDATA (stock);
4248 if (stock_name[0] == 'n' && stock_name[1] == ':')
4250 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4251 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4253 icon_name = stock_name + 2;
4254 stock_name = NULL;
4255 stock = Qnil;
4257 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4258 icon_name = NULL;
4259 else
4260 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4262 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4263 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4264 else
4266 stock = Qnil;
4267 stock_name = NULL;
4271 if (stock_name == NULL && icon_name == NULL)
4273 /* No stock image, or stock item not known. Try regular image. */
4275 /* If image is a vector, choose the image according to the
4276 button state. */
4277 if (dir == GTK_TEXT_DIR_RTL
4278 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4279 && STRINGP (rtl))
4281 image = find_rtl_image (f, image, rtl);
4284 if (VECTORP (image))
4286 if (enabled_p)
4287 idx = (selected_p
4288 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4289 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4290 else
4291 idx = (selected_p
4292 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4293 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4295 xassert (ASIZE (image) >= idx);
4296 image = AREF (image, idx);
4298 else
4299 idx = -1;
4301 img_id = lookup_image (f, image);
4302 img = IMAGE_FROM_ID (f, img_id);
4303 prepare_image_for_display (f, img);
4305 if (img->load_failed_p || img->pixmap == None)
4307 if (ti)
4308 gtk_widget_hide_all (GTK_WIDGET (ti));
4309 else
4311 /* Insert an empty (non-image) button */
4312 ti = xg_make_tool_item (f, NULL, NULL, "", i);
4313 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, -1);
4315 continue;
4319 if (ti == NULL)
4321 GtkWidget *w;
4322 if (stock_name)
4324 w = gtk_image_new_from_stock (stock_name, icon_size);
4325 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4326 (gpointer) xstrdup (stock_name),
4327 (GDestroyNotify) xfree);
4329 else if (icon_name)
4331 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4332 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4333 (gpointer) xstrdup (icon_name),
4334 (GDestroyNotify) xfree);
4336 else
4338 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4339 /* Save the image so we can see if an update is needed when
4340 this function is called again. */
4341 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4342 (gpointer)img->pixmap);
4345 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4346 ti = xg_make_tool_item (f, w, &wbutton, label, i);
4347 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, -1);
4348 gtk_widget_set_sensitive (wbutton, enabled_p);
4350 else
4352 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
4353 GtkWidget *wimage;
4354 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4356 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4357 XG_TOOL_BAR_IMAGE_DATA);
4358 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4359 XG_TOOL_BAR_STOCK_NAME);
4360 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4361 XG_TOOL_BAR_ICON_NAME);
4362 gtk_label_set_text (GTK_LABEL (wlbl), label);
4363 if (stock_name &&
4364 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4366 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4367 stock_name, icon_size);
4368 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4369 (gpointer) xstrdup (stock_name),
4370 (GDestroyNotify) xfree);
4371 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4372 NULL);
4373 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4374 NULL);
4376 else if (icon_name &&
4377 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4379 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4380 icon_name, icon_size);
4381 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4382 (gpointer) xstrdup (icon_name),
4383 (GDestroyNotify) xfree);
4384 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4385 NULL);
4386 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4387 NULL);
4389 else if (img && old_img != img->pixmap)
4391 (void) xg_get_image_for_pixmap (f, img, x->widget,
4392 GTK_IMAGE (wimage));
4393 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4394 (gpointer)img->pixmap);
4396 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4397 NULL);
4398 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4399 NULL);
4402 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4404 gtk_widget_set_sensitive (wbutton, enabled_p);
4406 xg_show_toolbar_item (ti);
4408 #undef PROP
4411 /* Remove buttons not longer needed. We just hide them so they
4412 can be reused later on. */
4415 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), i++);
4416 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4417 } while (ti != NULL);
4419 if (f->n_tool_bar_items != 0)
4421 if (pack_tool_bar)
4422 xg_pack_tool_bar (f, f->tool_bar_position);
4423 gtk_widget_show (x->toolbar_widget);
4424 gtk_widget_show (x->handlebox_widget);
4425 if (xg_update_tool_bar_sizes (f))
4426 xg_height_or_width_changed (f);
4429 UNBLOCK_INPUT;
4432 /* Deallocate all resources for the tool bar on frame F.
4433 Remove the tool bar. */
4435 void
4436 free_frame_tool_bar (FRAME_PTR f)
4438 struct x_output *x = f->output_data.x;
4440 if (x->toolbar_widget)
4442 int is_packed = x->handlebox_widget != 0;
4443 BLOCK_INPUT;
4444 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4445 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4446 if (is_packed)
4448 if (x->toolbar_in_hbox)
4449 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4450 x->handlebox_widget);
4451 else
4452 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4453 x->handlebox_widget);
4455 else
4456 gtk_widget_destroy (x->toolbar_widget);
4458 x->toolbar_widget = 0;
4459 x->handlebox_widget = 0;
4460 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4461 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4463 xg_height_or_width_changed (f);
4465 UNBLOCK_INPUT;
4470 xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4472 struct x_output *x = f->output_data.x;
4474 if (! x->toolbar_widget || ! x->handlebox_widget)
4475 return 1;
4477 BLOCK_INPUT;
4478 g_object_ref (x->handlebox_widget);
4479 if (x->toolbar_in_hbox)
4480 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4481 x->handlebox_widget);
4482 else
4483 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4484 x->handlebox_widget);
4485 xg_pack_tool_bar (f, pos);
4486 g_object_unref (x->handlebox_widget);
4487 if (xg_update_tool_bar_sizes (f))
4488 xg_height_or_width_changed (f);
4490 UNBLOCK_INPUT;
4491 return 1;
4496 /***********************************************************************
4497 Initializing
4498 ***********************************************************************/
4499 void
4500 xg_initialize (void)
4502 GtkBindingSet *binding_set;
4504 #if HAVE_XFT
4505 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4506 we keep it permanently linked in. */
4507 XftInit (0);
4508 #endif
4510 gdpy_def = NULL;
4511 xg_ignore_gtk_scrollbar = 0;
4512 xg_detached_menus = 0;
4513 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4514 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4516 id_to_widget.max_size = id_to_widget.used = 0;
4517 id_to_widget.widgets = 0;
4519 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4520 bindings. It doesn't seem to be any way to remove properties,
4521 so we set it to VoidSymbol which in X means "no key". */
4522 gtk_settings_set_string_property (gtk_settings_get_default (),
4523 "gtk-menu-bar-accel",
4524 "VoidSymbol",
4525 EMACS_CLASS);
4527 /* Make GTK text input widgets use Emacs style keybindings. This is
4528 Emacs after all. */
4529 gtk_settings_set_string_property (gtk_settings_get_default (),
4530 "gtk-key-theme-name",
4531 "Emacs",
4532 EMACS_CLASS);
4534 /* Make dialogs close on C-g. Since file dialog inherits from
4535 dialog, this works for them also. */
4536 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4537 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4538 "close", 0);
4540 /* Make menus close on C-g. */
4541 binding_set = gtk_binding_set_by_class (g_type_class_ref
4542 (GTK_TYPE_MENU_SHELL));
4543 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4544 "cancel", 0);
4547 #endif /* USE_GTK */
4549 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4550 (do not change this comment) */