[HAVE_DIALOGS] (mac_dialog_show): Cast to pointer-size
[emacs.git] / src / gtkutil.c
blobfedfb8fa0df456abb615ef49ba22d00782bf8004
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include <config.h>
24 #ifdef USE_GTK
25 #include <string.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "atimer.h"
34 #include "gtkutil.h"
35 #include "termhooks.h"
36 #include "keyboard.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include <gdk/gdkkeysyms.h>
42 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
43 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
45 /* Avoid "differ in sign" warnings */
46 #define SSDATA(x) ((char *) SDATA (x))
49 /***********************************************************************
50 Display handling functions
51 ***********************************************************************/
53 #ifdef HAVE_GTK_MULTIDISPLAY
55 /* Return the GdkDisplay that corresponds to the X display DPY. */
57 static GdkDisplay *
58 xg_get_gdk_display (dpy)
59 Display *dpy;
61 return gdk_x11_lookup_xdisplay (dpy);
64 /* When the GTK widget W is to be created on a display for F that
65 is not the default display, set the display for W.
66 W can be a GtkMenu or a GtkWindow widget. */
68 static void
69 xg_set_screen (w, f)
70 GtkWidget *w;
71 FRAME_PTR f;
73 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
75 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
76 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
78 if (GTK_IS_MENU (w))
79 gtk_menu_set_screen (GTK_MENU (w), gscreen);
80 else
81 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
86 #else /* not HAVE_GTK_MULTIDISPLAY */
88 /* Make some defines so we can use the GTK 2.2 functions when
89 compiling with GTK 2.0. */
91 #define xg_set_screen(w, f)
92 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
93 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
94 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
95 #define gdk_x11_lookup_xdisplay(dpy) 0
96 #define GdkDisplay void
98 #endif /* not HAVE_GTK_MULTIDISPLAY */
100 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
101 *DPY is set to NULL if the display can't be opened.
103 Returns non-zero if display could be opened, zero if display could not
104 be opened, and less than zero if the GTK version doesn't support
105 multipe displays. */
108 xg_display_open (display_name, dpy)
109 char *display_name;
110 Display **dpy;
112 #ifdef HAVE_GTK_MULTIDISPLAY
113 GdkDisplay *gdpy;
115 gdpy = gdk_display_open (display_name);
116 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
118 return gdpy != NULL;
120 #else /* not HAVE_GTK_MULTIDISPLAY */
122 return -1;
123 #endif /* not HAVE_GTK_MULTIDISPLAY */
127 /* Close display DPY. */
129 void
130 xg_display_close (Display *dpy)
132 #ifdef HAVE_GTK_MULTIDISPLAY
133 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
135 /* If this is the default display, we must change it before calling
136 dispose, otherwise it will crash on some Gtk+ versions. */
137 if (gdk_display_get_default () == gdpy)
139 struct x_display_info *dpyinfo;
140 Display *new_dpy = 0;
141 GdkDisplay *gdpy_new;
143 /* Find another display. */
144 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
145 if (dpyinfo->display != dpy)
147 new_dpy = dpyinfo->display;
148 break;
151 if (! new_dpy) return; /* Emacs will exit anyway. */
153 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
154 gdk_display_manager_set_default_display (gdk_display_manager_get (),
155 gdpy_new);
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
160 we can continue running, but there will be memory leaks. */
162 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
163 g_object_run_dispose (G_OBJECT (gdpy));
164 #else
165 /* This seems to be fixed in GTK 2.10. */
166 gdk_display_close (gdpy);
167 #endif
168 #endif /* HAVE_GTK_MULTIDISPLAY */
172 /***********************************************************************
173 Utility functions
174 ***********************************************************************/
175 /* The timer for scroll bar repetition and menu bar timeouts.
176 NULL if no timer is started. */
177 static struct atimer *xg_timer;
180 /* The next two variables and functions are taken from lwlib. */
181 static widget_value *widget_value_free_list;
182 static int malloc_cpt;
184 /* Allocate a widget_value structure, either by taking one from the
185 widget_value_free_list or by malloc:ing a new one.
187 Return a pointer to the allocated structure. */
189 widget_value *
190 malloc_widget_value ()
192 widget_value *wv;
193 if (widget_value_free_list)
195 wv = widget_value_free_list;
196 widget_value_free_list = wv->free_list;
197 wv->free_list = 0;
199 else
201 wv = (widget_value *) xmalloc (sizeof (widget_value));
202 malloc_cpt++;
204 memset (wv, 0, sizeof (widget_value));
205 return wv;
208 /* This is analogous to free. It frees only what was allocated
209 by malloc_widget_value, and no substructures. */
211 void
212 free_widget_value (wv)
213 widget_value *wv;
215 if (wv->free_list)
216 abort ();
218 if (malloc_cpt > 25)
220 /* When the number of already allocated cells is too big,
221 We free it. */
222 free (wv);
223 malloc_cpt--;
225 else
227 wv->free_list = widget_value_free_list;
228 widget_value_free_list = wv;
233 /* Create and return the cursor to be used for popup menus and
234 scroll bars on display DPY. */
236 GdkCursor *
237 xg_create_default_cursor (dpy)
238 Display *dpy;
240 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
241 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
244 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
246 static GdkPixbuf *
247 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
248 GdkPixmap *gpix;
249 GdkPixmap *gmask;
250 GdkColormap *cmap;
252 int x, y, width, height, rowstride, mask_rowstride;
253 GdkPixbuf *icon_buf, *tmp_buf;
254 guchar *pixels;
255 guchar *mask_pixels;
257 gdk_drawable_get_size (gpix, &width, &height);
258 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
259 0, 0, 0, 0, width, height);
260 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
261 g_object_unref (G_OBJECT (tmp_buf));
263 if (gmask)
265 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
266 gmask,
267 NULL,
268 0, 0, 0, 0,
269 width, height);
270 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
271 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
272 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
273 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
274 int y;
276 for (y = 0; y < height; ++y)
278 guchar *iconptr, *maskptr;
279 int x;
281 iconptr = pixels + y * rowstride;
282 maskptr = mask_pixels + y * mask_rowstride;
284 for (x = 0; x < width; ++x)
286 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
287 just R is sufficient. */
288 if (maskptr[0] == 0)
289 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
291 iconptr += rowstride/width;
292 maskptr += mask_rowstride/width;
296 g_object_unref (G_OBJECT (mask_buf));
299 return icon_buf;
302 static Lisp_Object
303 file_for_image(image)
304 Lisp_Object image;
306 Lisp_Object specified_file = Qnil;
307 Lisp_Object tail;
308 extern Lisp_Object QCfile;
310 for (tail = XCDR (image);
311 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
312 tail = XCDR (XCDR (tail)))
313 if (EQ (XCAR (tail), QCfile))
314 specified_file = XCAR (XCDR (tail));
316 return specified_file;
319 /* For the image defined in IMG, make and return a GtkImage. For displays with
320 8 planes or less we must make a GdkPixbuf and apply the mask manually.
321 Otherwise the highlightning and dimming the tool bar code in GTK does
322 will look bad. For display with more than 8 planes we just use the
323 pixmap and mask directly. For monochrome displays, GTK doesn't seem
324 able to use external pixmaps, it looks bad whatever we do.
325 The image is defined on the display where frame F is.
326 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
327 If OLD_WIDGET is NULL, a new widget is constructed and returned.
328 If OLD_WIDGET is not NULL, that widget is modified. */
330 static GtkWidget *
331 xg_get_image_for_pixmap (f, img, widget, old_widget)
332 FRAME_PTR f;
333 struct image *img;
334 GtkWidget *widget;
335 GtkImage *old_widget;
337 GdkPixmap *gpix;
338 GdkPixmap *gmask;
339 GdkDisplay *gdpy;
340 GdkColormap *cmap;
341 GdkPixbuf *icon_buf;
343 /* If we have a file, let GTK do all the image handling.
344 This seems to be the only way to make insensitive and activated icons
345 look good in all cases. */
346 Lisp_Object specified_file = file_for_image (img->spec);
347 Lisp_Object file;
349 /* We already loaded the image once before calling this
350 function, so this only fails if the image file has been removed.
351 In that case, use the pixmap already loaded. */
353 if (STRINGP (specified_file)
354 && STRINGP (file = x_find_image_file (specified_file)))
356 if (! old_widget)
357 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
358 else
359 gtk_image_set_from_file (old_widget, SSDATA (file));
361 return GTK_WIDGET (old_widget);
364 /* No file, do the image handling ourselves. This will look very bad
365 on a monochrome display, and sometimes bad on all displays with
366 certain themes. */
368 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
369 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
370 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
372 /* This is a workaround to make icons look good on pseudo color
373 displays. Apparently GTK expects the images to have an alpha
374 channel. If they don't, insensitive and activated icons will
375 look bad. This workaround does not work on monochrome displays,
376 and is strictly not needed on true color/static color displays (i.e.
377 16 bits and higher). But we do it anyway so we get a pixbuf that is
378 not associated with the img->pixmap. The img->pixmap may be removed
379 by clearing the image cache and then the tool bar redraw fails, since
380 Gtk+ assumes the pixmap is always there. */
381 cmap = gtk_widget_get_colormap (widget);
382 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
384 if (! old_widget)
385 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
386 else
387 gtk_image_set_from_pixbuf (old_widget, icon_buf);
389 g_object_unref (G_OBJECT (icon_buf));
391 g_object_unref (G_OBJECT (gpix));
392 if (gmask) g_object_unref (G_OBJECT (gmask));
394 return GTK_WIDGET (old_widget);
398 /* Set CURSOR on W and all widgets W contain. We must do like this
399 for scroll bars and menu because they create widgets internally,
400 and it is those widgets that are visible. */
402 static void
403 xg_set_cursor (w, cursor)
404 GtkWidget *w;
405 GdkCursor *cursor;
407 GList *children = gdk_window_peek_children (w->window);
409 gdk_window_set_cursor (w->window, cursor);
411 /* The scroll bar widget has more than one GDK window (had to look at
412 the source to figure this out), and there is no way to set cursor
413 on widgets in GTK. So we must set the cursor for all GDK windows.
414 Ditto for menus. */
416 for ( ; children; children = g_list_next (children))
417 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
420 /* Timer function called when a timeout occurs for xg_timer.
421 This function processes all GTK events in a recursive event loop.
422 This is done because GTK timer events are not seen by Emacs event
423 detection, Emacs only looks for X events. When a scroll bar has the
424 pointer (detected by button press/release events below) an Emacs
425 timer is started, and this function can then check if the GTK timer
426 has expired by calling the GTK event loop.
427 Also, when a menu is active, it has a small timeout before it
428 pops down the sub menu under it. */
430 static void
431 xg_process_timeouts (timer)
432 struct atimer *timer;
434 BLOCK_INPUT;
435 /* Ideally we would like to just handle timer events, like the Xt version
436 of this does in xterm.c, but there is no such feature in GTK. */
437 while (gtk_events_pending ())
438 gtk_main_iteration ();
439 UNBLOCK_INPUT;
442 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
443 xg_process_timeouts is called when the timer expires. The timer
444 started is continuous, i.e. runs until xg_stop_timer is called. */
446 static void
447 xg_start_timer ()
449 if (! xg_timer)
451 EMACS_TIME interval;
452 EMACS_SET_SECS_USECS (interval, 0, 100000);
453 xg_timer = start_atimer (ATIMER_CONTINUOUS,
454 interval,
455 xg_process_timeouts,
460 /* Stop the xg_timer if started. */
462 static void
463 xg_stop_timer ()
465 if (xg_timer)
467 cancel_atimer (xg_timer);
468 xg_timer = 0;
472 /* Insert NODE into linked LIST. */
474 static void
475 xg_list_insert (xg_list_node *list, xg_list_node *node)
477 xg_list_node *list_start = list->next;
479 if (list_start) list_start->prev = node;
480 node->next = list_start;
481 node->prev = 0;
482 list->next = node;
485 /* Remove NODE from linked LIST. */
487 static void
488 xg_list_remove (xg_list_node *list, xg_list_node *node)
490 xg_list_node *list_start = list->next;
491 if (node == list_start)
493 list->next = node->next;
494 if (list->next) list->next->prev = 0;
496 else
498 node->prev->next = node->next;
499 if (node->next) node->next->prev = node->prev;
503 /* Allocate and return a utf8 version of STR. If STR is already
504 utf8 or NULL, just return STR.
505 If not, a new string is allocated and the caller must free the result
506 with g_free. */
508 static char *
509 get_utf8_string (str)
510 char *str;
512 char *utf8_str = str;
514 if (!str) return NULL;
516 /* If not UTF-8, try current locale. */
517 if (!g_utf8_validate (str, -1, NULL))
518 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
520 if (!utf8_str)
522 /* Probably some control characters in str. Escape them. */
523 size_t nr_bad = 0;
524 gsize bytes_read;
525 gsize bytes_written;
526 unsigned char *p = (unsigned char *)str;
527 char *cp, *up;
528 GError *error = NULL;
530 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
531 &bytes_written, &error))
532 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
534 ++nr_bad;
535 p += bytes_written+1;
536 g_error_free (error);
537 error = NULL;
540 if (error)
542 g_error_free (error);
543 error = NULL;
545 if (cp) g_free (cp);
547 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
548 p = (unsigned char *)str;
550 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
551 &bytes_written, &error))
552 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
554 strncpy (up, (char *)p, bytes_written);
555 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
556 up[bytes_written+4] = '\0';
557 up += bytes_written+4;
558 p += bytes_written+1;
559 g_error_free (error);
560 error = NULL;
563 if (cp)
565 strcat (utf8_str, cp);
566 g_free (cp);
568 if (error)
570 g_error_free (error);
571 error = NULL;
574 return utf8_str;
579 /***********************************************************************
580 General functions for creating widgets, resizing, events, e.t.c.
581 ***********************************************************************/
583 /* Make a geometry string and pass that to GTK. It seems this is the
584 only way to get geometry position right if the user explicitly
585 asked for a position when starting Emacs.
586 F is the frame we shall set geometry for. */
588 static void
589 xg_set_geometry (f)
590 FRAME_PTR f;
592 if (f->size_hint_flags & USPosition)
594 int left = f->left_pos;
595 int xneg = f->size_hint_flags & XNegative;
596 int top = f->top_pos;
597 int yneg = f->size_hint_flags & YNegative;
598 char geom_str[32];
600 if (xneg)
601 left = -left;
602 if (yneg)
603 top = -top;
605 sprintf (geom_str, "=%dx%d%c%d%c%d",
606 FRAME_PIXEL_WIDTH (f),
607 FRAME_TOTAL_PIXEL_HEIGHT (f),
608 (xneg ? '-' : '+'), left,
609 (yneg ? '-' : '+'), top);
611 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
612 geom_str))
613 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
614 } else if (f->size_hint_flags & PPosition) {
615 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
616 f->left_pos, f->top_pos);
621 /* Resize the outer window of frame F after chainging the height.
622 This happend when the menu bar or the tool bar is added or removed.
623 COLUMNS/ROWS is the size the edit area shall have after the resize. */
625 static void
626 xg_resize_outer_widget (f, columns, rows)
627 FRAME_PTR f;
628 int columns;
629 int rows;
631 /* If we are not mapped yet, set geometry once again, as window
632 height now have changed. */
633 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
634 xg_set_geometry (f);
636 xg_frame_set_char_size (f, columns, rows);
637 gdk_window_process_all_updates ();
640 /* Function to handle resize of our widgets. Since Emacs has some layouts
641 that does not fit well with GTK standard containers, we do most layout
642 manually.
643 F is the frame to resize.
644 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
646 void
647 xg_resize_widgets (f, pixelwidth, pixelheight)
648 FRAME_PTR f;
649 int pixelwidth, pixelheight;
651 int mbheight = FRAME_MENUBAR_HEIGHT (f);
652 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
653 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
654 - mbheight - tbheight));
655 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
657 if (FRAME_GTK_WIDGET (f)
658 && (columns != FRAME_COLS (f)
659 || rows != FRAME_LINES (f)
660 || pixelwidth != FRAME_PIXEL_WIDTH (f)
661 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
663 struct x_output *x = f->output_data.x;
664 GtkAllocation all;
666 all.y = mbheight + tbheight;
667 all.x = 0;
669 all.width = pixelwidth;
670 all.height = pixelheight - mbheight - tbheight;
672 gtk_widget_size_allocate (x->edit_widget, &all);
674 change_frame_size (f, rows, columns, 0, 1, 0);
675 SET_FRAME_GARBAGED (f);
676 cancel_mouse_face (f);
681 /* Update our widget size to be COLS/ROWS characters for frame F. */
683 void
684 xg_frame_set_char_size (f, cols, rows)
685 FRAME_PTR f;
686 int cols;
687 int rows;
689 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
690 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
691 int pixelwidth;
693 /* Take into account the size of the scroll bar. Always use the
694 number of columns occupied by the scroll bar here otherwise we
695 might end up with a frame width that is not a multiple of the
696 frame's character width which is bad for vertically split
697 windows. */
698 f->scroll_bar_actual_width
699 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
701 compute_fringe_widths (f, 0);
703 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
704 after calculating that value. */
705 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
707 /* Must resize our top level widget. Font size may have changed,
708 but not rows/cols. */
709 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
710 pixelwidth, pixelheight);
711 xg_resize_widgets (f, pixelwidth, pixelheight);
712 x_wm_set_size_hint (f, 0, 0);
713 SET_FRAME_GARBAGED (f);
714 cancel_mouse_face (f);
717 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
718 Must be done like this, because GtkWidget:s can have "hidden"
719 X Window that aren't accessible.
721 Return 0 if no widget match WDESC. */
723 GtkWidget *
724 xg_win_to_widget (dpy, wdesc)
725 Display *dpy;
726 Window wdesc;
728 gpointer gdkwin;
729 GtkWidget *gwdesc = 0;
731 BLOCK_INPUT;
733 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
734 wdesc);
735 if (gdkwin)
737 GdkEvent event;
738 event.any.window = gdkwin;
739 gwdesc = gtk_get_event_widget (&event);
742 UNBLOCK_INPUT;
743 return gwdesc;
746 /* Fill in the GdkColor C so that it represents PIXEL.
747 W is the widget that color will be used for. Used to find colormap. */
749 static void
750 xg_pix_to_gcolor (w, pixel, c)
751 GtkWidget *w;
752 unsigned long pixel;
753 GdkColor *c;
755 GdkColormap *map = gtk_widget_get_colormap (w);
756 gdk_colormap_query_color (map, pixel, c);
759 /* Create and set up the GTK widgets for frame F.
760 Return 0 if creation failed, non-zero otherwise. */
763 xg_create_frame_widgets (f)
764 FRAME_PTR f;
766 GtkWidget *wtop;
767 GtkWidget *wvbox;
768 GtkWidget *wfixed;
769 GdkColor bg;
770 GtkRcStyle *style;
771 int i;
772 char *title = 0;
774 BLOCK_INPUT;
776 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
777 xg_set_screen (wtop, f);
779 wvbox = gtk_vbox_new (FALSE, 0);
780 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
782 if (! wtop || ! wvbox || ! wfixed)
784 if (wtop) gtk_widget_destroy (wtop);
785 if (wvbox) gtk_widget_destroy (wvbox);
786 if (wfixed) gtk_widget_destroy (wfixed);
788 UNBLOCK_INPUT;
789 return 0;
792 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
793 gtk_widget_set_name (wtop, EMACS_CLASS);
794 gtk_widget_set_name (wvbox, "pane");
795 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
797 /* If this frame has a title or name, set it in the title bar. */
798 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
799 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
801 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
803 FRAME_GTK_OUTER_WIDGET (f) = wtop;
804 FRAME_GTK_WIDGET (f) = wfixed;
805 f->output_data.x->vbox_widget = wvbox;
807 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
809 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
810 FRAME_PIXEL_HEIGHT (f));
812 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
813 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
815 if (FRAME_EXTERNAL_TOOL_BAR (f))
816 update_frame_tool_bar (f);
818 /* The tool bar is created but first there are no items in it.
819 This causes it to be zero height. Later items are added, but then
820 the frame is already mapped, so there is a "jumping" resize.
821 This makes geometry handling difficult, for example -0-0 will end
822 up in the wrong place as tool bar height has not been taken into account.
823 So we cheat a bit by setting a height that is what it will have
824 later on when tool bar items are added. */
825 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
826 FRAME_TOOLBAR_HEIGHT (f) = 38;
829 /* We don't want this widget double buffered, because we draw on it
830 with regular X drawing primitives, so from a GTK/GDK point of
831 view, the widget is totally blank. When an expose comes, this
832 will make the widget blank, and then Emacs redraws it. This flickers
833 a lot, so we turn off double buffering. */
834 gtk_widget_set_double_buffered (wfixed, FALSE);
836 /* GTK documents says use gtk_window_set_resizable. But then a user
837 can't shrink the window from its starting size. */
838 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
839 gtk_window_set_wmclass (GTK_WINDOW (wtop),
840 SSDATA (Vx_resource_name),
841 SSDATA (Vx_resource_class));
843 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
844 GTK is to destroy the widget. We want Emacs to do that instead. */
845 g_signal_connect (G_OBJECT (wtop), "delete-event",
846 G_CALLBACK (gtk_true), 0);
848 /* Convert our geometry parameters into a geometry string
849 and specify it.
850 GTK will itself handle calculating the real position this way. */
851 xg_set_geometry (f);
853 gtk_widget_add_events (wfixed,
854 GDK_POINTER_MOTION_MASK
855 | GDK_EXPOSURE_MASK
856 | GDK_BUTTON_PRESS_MASK
857 | GDK_BUTTON_RELEASE_MASK
858 | GDK_KEY_PRESS_MASK
859 | GDK_ENTER_NOTIFY_MASK
860 | GDK_LEAVE_NOTIFY_MASK
861 | GDK_FOCUS_CHANGE_MASK
862 | GDK_STRUCTURE_MASK
863 | GDK_VISIBILITY_NOTIFY_MASK);
865 /* Must realize the windows so the X window gets created. It is used
866 by callers of this function. */
867 gtk_widget_realize (wfixed);
868 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
870 /* Since GTK clears its window by filling with the background color,
871 we must keep X and GTK background in sync. */
872 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
873 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
875 /* Also, do not let any background pixmap to be set, this looks very
876 bad as Emacs overwrites the background pixmap with its own idea
877 of background color. */
878 style = gtk_widget_get_modifier_style (wfixed);
880 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
881 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
882 gtk_widget_modify_style (wfixed, style);
884 /* GTK does not set any border, and they look bad with GTK. */
885 f->border_width = 0;
886 f->internal_border_width = 0;
888 UNBLOCK_INPUT;
890 return 1;
893 /* Set the normal size hints for the window manager, for frame F.
894 FLAGS is the flags word to use--or 0 meaning preserve the flags
895 that the window now has.
896 If USER_POSITION is nonzero, we set the User Position
897 flag (this is useful when FLAGS is 0). */
899 void
900 x_wm_set_size_hint (f, flags, user_position)
901 FRAME_PTR f;
902 long flags;
903 int user_position;
905 if (FRAME_GTK_OUTER_WIDGET (f))
907 /* Must use GTK routines here, otherwise GTK resets the size hints
908 to its own defaults. */
909 GdkGeometry size_hints;
910 gint hint_flags = 0;
911 int base_width, base_height;
912 int min_rows = 0, min_cols = 0;
913 int win_gravity = f->win_gravity;
915 if (flags)
917 memset (&size_hints, 0, sizeof (size_hints));
918 f->output_data.x->size_hints = size_hints;
919 f->output_data.x->hint_flags = hint_flags;
921 else
922 flags = f->size_hint_flags;
924 size_hints = f->output_data.x->size_hints;
925 hint_flags = f->output_data.x->hint_flags;
927 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
928 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
929 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
931 hint_flags |= GDK_HINT_BASE_SIZE;
932 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
933 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
934 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
936 check_frame_size (f, &min_rows, &min_cols);
938 size_hints.base_width = base_width;
939 size_hints.base_height = base_height;
940 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
941 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
944 /* These currently have a one to one mapping with the X values, but I
945 don't think we should rely on that. */
946 hint_flags |= GDK_HINT_WIN_GRAVITY;
947 size_hints.win_gravity = 0;
948 if (win_gravity == NorthWestGravity)
949 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
950 else if (win_gravity == NorthGravity)
951 size_hints.win_gravity = GDK_GRAVITY_NORTH;
952 else if (win_gravity == NorthEastGravity)
953 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
954 else if (win_gravity == WestGravity)
955 size_hints.win_gravity = GDK_GRAVITY_WEST;
956 else if (win_gravity == CenterGravity)
957 size_hints.win_gravity = GDK_GRAVITY_CENTER;
958 else if (win_gravity == EastGravity)
959 size_hints.win_gravity = GDK_GRAVITY_EAST;
960 else if (win_gravity == SouthWestGravity)
961 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
962 else if (win_gravity == SouthGravity)
963 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
964 else if (win_gravity == SouthEastGravity)
965 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
966 else if (win_gravity == StaticGravity)
967 size_hints.win_gravity = GDK_GRAVITY_STATIC;
969 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
970 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
971 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
973 if (user_position)
975 hint_flags &= ~GDK_HINT_POS;
976 hint_flags |= GDK_HINT_USER_POS;
979 BLOCK_INPUT;
981 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
982 FRAME_GTK_OUTER_WIDGET (f),
983 &size_hints,
984 hint_flags);
986 f->output_data.x->size_hints = size_hints;
987 f->output_data.x->hint_flags = hint_flags;
988 UNBLOCK_INPUT;
992 /* Change background color of a frame.
993 Since GTK uses the background colour to clear the window, we must
994 keep the GTK and X colors in sync.
995 F is the frame to change,
996 BG is the pixel value to change to. */
998 void
999 xg_set_background_color (f, bg)
1000 FRAME_PTR f;
1001 unsigned long bg;
1003 if (FRAME_GTK_WIDGET (f))
1005 GdkColor gdk_bg;
1007 BLOCK_INPUT;
1008 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
1009 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
1010 UNBLOCK_INPUT;
1015 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1016 functions so GTK does not overwrite the icon. */
1018 void
1019 xg_set_frame_icon (f, icon_pixmap, icon_mask)
1020 FRAME_PTR f;
1021 Pixmap icon_pixmap;
1022 Pixmap icon_mask;
1024 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1025 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
1026 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
1027 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
1029 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1034 /***********************************************************************
1035 Dialog functions
1036 ***********************************************************************/
1037 /* Return the dialog title to use for a dialog of type KEY.
1038 This is the encoding used by lwlib. We use the same for GTK. */
1040 static char *
1041 get_dialog_title (char key)
1043 char *title = "";
1045 switch (key) {
1046 case 'E': case 'e':
1047 title = "Error";
1048 break;
1050 case 'I': case 'i':
1051 title = "Information";
1052 break;
1054 case 'L': case 'l':
1055 title = "Prompt";
1056 break;
1058 case 'P': case 'p':
1059 title = "Prompt";
1060 break;
1062 case 'Q': case 'q':
1063 title = "Question";
1064 break;
1067 return title;
1070 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1071 the dialog, but return TRUE so the event does not propagate further
1072 in GTK. This prevents GTK from destroying the dialog widget automatically
1073 and we can always destrou the widget manually, regardles of how
1074 it was popped down (button press or WM_DELETE_WINDOW).
1075 W is the dialog widget.
1076 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1077 user_data is NULL (not used).
1079 Returns TRUE to end propagation of event. */
1081 static gboolean
1082 dialog_delete_callback (w, event, user_data)
1083 GtkWidget *w;
1084 GdkEvent *event;
1085 gpointer user_data;
1087 gtk_widget_unmap (w);
1088 return TRUE;
1091 /* Create a popup dialog window. See also xg_create_widget below.
1092 WV is a widget_value describing the dialog.
1093 SELECT_CB is the callback to use when a button has been pressed.
1094 DEACTIVATE_CB is the callback to use when the dialog pops down.
1096 Returns the GTK dialog widget. */
1098 static GtkWidget *
1099 create_dialog (wv, select_cb, deactivate_cb)
1100 widget_value *wv;
1101 GCallback select_cb;
1102 GCallback deactivate_cb;
1104 char *title = get_dialog_title (wv->name[0]);
1105 int total_buttons = wv->name[1] - '0';
1106 int right_buttons = wv->name[4] - '0';
1107 int left_buttons;
1108 int button_nr = 0;
1109 int button_spacing = 10;
1110 GtkWidget *wdialog = gtk_dialog_new ();
1111 widget_value *item;
1112 GtkBox *cur_box;
1113 GtkWidget *wvbox;
1114 GtkWidget *whbox_up;
1115 GtkWidget *whbox_down;
1117 /* If the number of buttons is greater than 4, make two rows of buttons
1118 instead. This looks better. */
1119 int make_two_rows = total_buttons > 4;
1121 if (right_buttons == 0) right_buttons = total_buttons/2;
1122 left_buttons = total_buttons - right_buttons;
1124 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1125 gtk_widget_set_name (wdialog, "emacs-dialog");
1127 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1129 if (make_two_rows)
1131 wvbox = gtk_vbox_new (TRUE, button_spacing);
1132 whbox_up = gtk_hbox_new (FALSE, 0);
1133 whbox_down = gtk_hbox_new (FALSE, 0);
1135 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1136 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1137 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1139 cur_box = GTK_BOX (whbox_up);
1142 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1143 G_CALLBACK (dialog_delete_callback), 0);
1145 if (deactivate_cb)
1147 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1148 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1151 for (item = wv->contents; item; item = item->next)
1153 char *utf8_label = get_utf8_string (item->value);
1154 GtkWidget *w;
1155 GtkRequisition req;
1157 if (item->name && strcmp (item->name, "message") == 0)
1159 /* This is the text part of the dialog. */
1160 w = gtk_label_new (utf8_label);
1161 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1162 gtk_label_new (""),
1163 FALSE, FALSE, 0);
1164 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1165 TRUE, TRUE, 0);
1166 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1168 /* Try to make dialog look better. Must realize first so
1169 the widget can calculate the size it needs. */
1170 gtk_widget_realize (w);
1171 gtk_widget_size_request (w, &req);
1172 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1173 req.height);
1174 if (item->value && strlen (item->value) > 0)
1175 button_spacing = 2*req.width/strlen (item->value);
1177 else
1179 /* This is one button to add to the dialog. */
1180 w = gtk_button_new_with_label (utf8_label);
1181 if (! item->enabled)
1182 gtk_widget_set_sensitive (w, FALSE);
1183 if (select_cb)
1184 g_signal_connect (G_OBJECT (w), "clicked",
1185 select_cb, item->call_data);
1187 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1188 if (++button_nr == left_buttons)
1190 if (make_two_rows)
1191 cur_box = GTK_BOX (whbox_down);
1192 else
1193 gtk_box_pack_start (cur_box,
1194 gtk_label_new (""),
1195 TRUE, TRUE,
1196 button_spacing);
1200 if (utf8_label && utf8_label != item->value)
1201 g_free (utf8_label);
1204 return wdialog;
1209 /***********************************************************************
1210 File dialog functions
1211 ***********************************************************************/
1212 /* Return non-zero if the old file selection dialog is being used.
1213 Return zero if not. */
1216 xg_uses_old_file_dialog ()
1218 #ifdef HAVE_GTK_FILE_BOTH
1219 extern int x_gtk_use_old_file_dialog;
1220 return x_gtk_use_old_file_dialog;
1221 #else /* ! HAVE_GTK_FILE_BOTH */
1223 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1224 return 1;
1225 #else
1226 return 0;
1227 #endif
1229 #endif /* ! HAVE_GTK_FILE_BOTH */
1233 /* Function that is called when the file dialog pops down.
1234 W is the dialog widget, RESPONSE is the response code.
1235 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1237 static void
1238 xg_file_response_cb (w,
1239 response,
1240 user_data)
1241 GtkDialog *w;
1242 gint response;
1243 gpointer user_data;
1245 int *ptr = (int *) user_data;
1246 *ptr = response;
1250 /* Destroy the dialog. This makes it pop down. */
1252 static Lisp_Object
1253 pop_down_file_dialog (arg)
1254 Lisp_Object arg;
1256 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1257 BLOCK_INPUT;
1258 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1259 UNBLOCK_INPUT;
1260 return Qnil;
1263 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1265 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1267 /* Return the selected file for file chooser dialog W.
1268 The returned string must be free:d. */
1270 static char *
1271 xg_get_file_name_from_chooser (w)
1272 GtkWidget *w;
1274 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1277 /* Callback called when the "Show hidden files" toggle is pressed.
1278 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1280 static void
1281 xg_toggle_visibility_cb (widget, data)
1282 GtkWidget *widget;
1283 gpointer data;
1285 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1286 gboolean visible;
1287 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1288 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1292 /* Callback called when a property changes in a file chooser.
1293 GOBJECT is the file chooser dialog, ARG1 describes the property.
1294 USER_DATA is the toggle widget in the file chooser dialog.
1295 We use this to update the "Show hidden files" toggle when the user
1296 changes that property by right clicking in the file list. */
1298 static void
1299 xg_toggle_notify_cb (gobject, arg1, user_data)
1300 GObject *gobject;
1301 GParamSpec *arg1;
1302 gpointer user_data;
1304 extern int x_gtk_show_hidden_files;
1306 if (strcmp (arg1->name, "show-hidden") == 0)
1308 GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
1309 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1310 gboolean visible, toggle_on;
1312 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1313 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1315 if (!!visible != !!toggle_on)
1317 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1318 G_CALLBACK (xg_toggle_visibility_cb),
1319 gobject);
1320 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1321 g_signal_handlers_unblock_by_func
1322 (G_OBJECT (wtoggle),
1323 G_CALLBACK (xg_toggle_visibility_cb),
1324 gobject);
1326 x_gtk_show_hidden_files = visible;
1330 /* Read a file name from the user using a file chooser dialog.
1331 F is the current frame.
1332 PROMPT is a prompt to show to the user. May not be NULL.
1333 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1334 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1335 file. *FUNC is set to a function that can be used to retrieve the
1336 selected file name from the returned widget.
1338 Returns the created widget. */
1340 static GtkWidget *
1341 xg_get_file_with_chooser (f, prompt, default_filename,
1342 mustmatch_p, only_dir_p, func)
1343 FRAME_PTR f;
1344 char *prompt;
1345 char *default_filename;
1346 int mustmatch_p, only_dir_p;
1347 xg_get_file_func *func;
1349 char message[1024];
1351 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1352 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1353 GtkFileChooserAction action = (mustmatch_p ?
1354 GTK_FILE_CHOOSER_ACTION_OPEN :
1355 GTK_FILE_CHOOSER_ACTION_SAVE);
1356 extern int x_gtk_show_hidden_files;
1357 extern int x_gtk_file_dialog_help_text;
1360 if (only_dir_p)
1361 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1363 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1364 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1365 (mustmatch_p || only_dir_p ?
1366 GTK_STOCK_OPEN : GTK_STOCK_OK),
1367 GTK_RESPONSE_OK,
1368 NULL);
1369 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1371 wbox = gtk_vbox_new (FALSE, 0);
1372 gtk_widget_show (wbox);
1373 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1375 if (x_gtk_show_hidden_files)
1377 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1378 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1380 gtk_widget_show (wtoggle);
1381 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1382 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1383 g_signal_connect (G_OBJECT (filewin), "notify",
1384 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1386 if (x_gtk_file_dialog_help_text)
1388 message[0] = '\0';
1389 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1390 Show the C-l help text only for versions < 2.10. */
1391 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1392 strcat (message, "\nType C-l to display a file name text entry box.\n");
1393 strcat (message, "\nIf you don't like this file selector, use the "
1394 "corresponding\nkey binding or customize "
1395 "use-file-dialog to turn it off.");
1397 wmessage = gtk_label_new (message);
1398 gtk_widget_show (wmessage);
1401 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1402 if (x_gtk_file_dialog_help_text)
1403 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1404 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1406 if (default_filename)
1408 Lisp_Object file;
1409 struct gcpro gcpro1;
1410 char *utf8_filename;
1411 GCPRO1 (file);
1413 file = build_string (default_filename);
1415 /* File chooser does not understand ~/... in the file name. It must be
1416 an absolute name starting with /. */
1417 if (default_filename[0] != '/')
1418 file = Fexpand_file_name (file, Qnil);
1420 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1421 if (! NILP (Ffile_directory_p (file)))
1422 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1423 utf8_filename);
1424 else
1426 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1427 utf8_filename);
1428 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1430 char *cp = strrchr (utf8_filename, '/');
1431 if (cp) ++cp;
1432 else cp = utf8_filename;
1433 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1437 UNGCPRO;
1440 *func = xg_get_file_name_from_chooser;
1441 return filewin;
1443 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1445 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1447 /* Return the selected file for file selector dialog W.
1448 The returned string must be free:d. */
1450 static char *
1451 xg_get_file_name_from_selector (w)
1452 GtkWidget *w;
1454 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1455 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1458 /* Create a file selection dialog.
1459 F is the current frame.
1460 PROMPT is a prompt to show to the user. May not be NULL.
1461 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1462 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1463 file. *FUNC is set to a function that can be used to retrieve the
1464 selected file name from the returned widget.
1466 Returns the created widget. */
1468 static GtkWidget *
1469 xg_get_file_with_selection (f, prompt, default_filename,
1470 mustmatch_p, only_dir_p, func)
1471 FRAME_PTR f;
1472 char *prompt;
1473 char *default_filename;
1474 int mustmatch_p, only_dir_p;
1475 xg_get_file_func *func;
1477 GtkWidget *filewin;
1478 GtkFileSelection *filesel;
1480 filewin = gtk_file_selection_new (prompt);
1481 filesel = GTK_FILE_SELECTION (filewin);
1483 if (default_filename)
1484 gtk_file_selection_set_filename (filesel, default_filename);
1486 if (mustmatch_p)
1488 /* The selection_entry part of filesel is not documented. */
1489 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1490 gtk_file_selection_hide_fileop_buttons (filesel);
1493 *func = xg_get_file_name_from_selector;
1495 return filewin;
1497 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1499 /* Read a file name from the user using a file dialog, either the old
1500 file selection dialog, or the new file chooser dialog. Which to use
1501 depends on what the GTK version used has, and what the value of
1502 gtk-use-old-file-dialog.
1503 F is the current frame.
1504 PROMPT is a prompt to show to the user. May not be NULL.
1505 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1506 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1507 file.
1509 Returns a file name or NULL if no file was selected.
1510 The returned string must be freed by the caller. */
1512 char *
1513 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1514 FRAME_PTR f;
1515 char *prompt;
1516 char *default_filename;
1517 int mustmatch_p, only_dir_p;
1519 GtkWidget *w = 0;
1520 int count = SPECPDL_INDEX ();
1521 char *fn = 0;
1522 int filesel_done = 0;
1523 xg_get_file_func func;
1525 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1526 /* I really don't know why this is needed, but without this the GLIBC add on
1527 library linuxthreads hangs when the Gnome file chooser backend creates
1528 threads. */
1529 sigblock (sigmask (__SIGRTMIN));
1530 #endif /* HAVE_GTK_AND_PTHREAD */
1532 #ifdef HAVE_GTK_FILE_BOTH
1534 if (xg_uses_old_file_dialog ())
1535 w = xg_get_file_with_selection (f, prompt, default_filename,
1536 mustmatch_p, only_dir_p, &func);
1537 else
1538 w = xg_get_file_with_chooser (f, prompt, default_filename,
1539 mustmatch_p, only_dir_p, &func);
1541 #else /* not HAVE_GTK_FILE_BOTH */
1543 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1544 w = xg_get_file_with_selection (f, prompt, default_filename,
1545 mustmatch_p, only_dir_p, &func);
1546 #endif
1547 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1548 w = xg_get_file_with_chooser (f, prompt, default_filename,
1549 mustmatch_p, only_dir_p, &func);
1550 #endif
1552 #endif /* HAVE_GTK_FILE_BOTH */
1554 xg_set_screen (w, f);
1555 gtk_widget_set_name (w, "emacs-filedialog");
1556 gtk_window_set_transient_for (GTK_WINDOW (w),
1557 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1558 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1559 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1561 g_signal_connect (G_OBJECT (w),
1562 "response",
1563 G_CALLBACK (xg_file_response_cb),
1564 &filesel_done);
1566 /* Don't destroy the widget if closed by the window manager close button. */
1567 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1569 gtk_widget_show (w);
1571 record_unwind_protect (pop_down_file_dialog, make_save_value (w, 0));
1572 while (! filesel_done)
1574 x_menu_wait_for_event (0);
1575 gtk_main_iteration ();
1578 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1579 sigunblock (sigmask (__SIGRTMIN));
1580 #endif
1582 if (filesel_done == GTK_RESPONSE_OK)
1583 fn = (*func) (w);
1585 unbind_to (count, Qnil);
1587 return fn;
1591 /***********************************************************************
1592 Menu functions.
1593 ***********************************************************************/
1595 /* The name of menu items that can be used for citomization. Since GTK
1596 RC files are very crude and primitive, we have to set this on all
1597 menu item names so a user can easily cutomize menu items. */
1599 #define MENU_ITEM_NAME "emacs-menuitem"
1602 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1603 during GC. The next member points to the items. */
1604 static xg_list_node xg_menu_cb_list;
1606 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1607 during GC. The next member points to the items. */
1608 static xg_list_node xg_menu_item_cb_list;
1610 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1611 F is the frame CL_DATA will be initialized for.
1612 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1614 The menu bar and all sub menus under the menu bar in a frame
1615 share the same structure, hence the reference count.
1617 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1618 allocated xg_menu_cb_data if CL_DATA is NULL. */
1620 static xg_menu_cb_data *
1621 make_cl_data (cl_data, f, highlight_cb)
1622 xg_menu_cb_data *cl_data;
1623 FRAME_PTR f;
1624 GCallback highlight_cb;
1626 if (! cl_data)
1628 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1629 cl_data->f = f;
1630 cl_data->menu_bar_vector = f->menu_bar_vector;
1631 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1632 cl_data->highlight_cb = highlight_cb;
1633 cl_data->ref_count = 0;
1635 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1638 cl_data->ref_count++;
1640 return cl_data;
1643 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1644 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1646 When the menu bar is updated, menu items may have been added and/or
1647 removed, so menu_bar_vector and menu_bar_items_used change. We must
1648 then update CL_DATA since it is used to determine which menu
1649 item that is invoked in the menu.
1650 HIGHLIGHT_CB could change, there is no check that the same
1651 function is given when modifying a menu bar as was given when
1652 creating the menu bar. */
1654 static void
1655 update_cl_data (cl_data, f, highlight_cb)
1656 xg_menu_cb_data *cl_data;
1657 FRAME_PTR f;
1658 GCallback highlight_cb;
1660 if (cl_data)
1662 cl_data->f = f;
1663 cl_data->menu_bar_vector = f->menu_bar_vector;
1664 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1665 cl_data->highlight_cb = highlight_cb;
1669 /* Decrease reference count for CL_DATA.
1670 If reference count is zero, free CL_DATA. */
1672 static void
1673 unref_cl_data (cl_data)
1674 xg_menu_cb_data *cl_data;
1676 if (cl_data && cl_data->ref_count > 0)
1678 cl_data->ref_count--;
1679 if (cl_data->ref_count == 0)
1681 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1682 xfree (cl_data);
1687 /* Function that marks all lisp data during GC. */
1689 void
1690 xg_mark_data ()
1692 xg_list_node *iter;
1694 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1695 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1697 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1699 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1701 if (! NILP (cb_data->help))
1702 mark_object (cb_data->help);
1707 /* Callback called when a menu item is destroyed. Used to free data.
1708 W is the widget that is being destroyed (not used).
1709 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1711 static void
1712 menuitem_destroy_callback (w, client_data)
1713 GtkWidget *w;
1714 gpointer client_data;
1716 if (client_data)
1718 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1719 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1720 xfree (data);
1724 /* Callback called when the pointer enters/leaves a menu item.
1725 W is the parent of the menu item.
1726 EVENT is either an enter event or leave event.
1727 CLIENT_DATA is not used.
1729 Returns FALSE to tell GTK to keep processing this event. */
1731 static gboolean
1732 menuitem_highlight_callback (w, event, client_data)
1733 GtkWidget *w;
1734 GdkEventCrossing *event;
1735 gpointer client_data;
1737 GdkEvent ev;
1738 GtkWidget *subwidget;
1739 xg_menu_item_cb_data *data;
1741 ev.crossing = *event;
1742 subwidget = gtk_get_event_widget (&ev);
1743 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1744 XG_ITEM_DATA);
1745 if (data)
1747 if (! NILP (data->help) && data->cl_data->highlight_cb)
1749 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1750 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1751 (*func) (subwidget, call_data);
1755 return FALSE;
1758 /* Callback called when a menu is destroyed. Used to free data.
1759 W is the widget that is being destroyed (not used).
1760 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1762 static void
1763 menu_destroy_callback (w, client_data)
1764 GtkWidget *w;
1765 gpointer client_data;
1767 unref_cl_data ((xg_menu_cb_data*) client_data);
1770 /* Callback called when a menu does a grab or ungrab. That means the
1771 menu has been activated or deactivated.
1772 Used to start a timer so the small timeout the menus in GTK uses before
1773 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1774 W is the widget that does the grab (not used).
1775 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1776 CLIENT_DATA is NULL (not used). */
1778 /* Keep track of total number of grabs. */
1779 static int menu_grab_callback_cnt;
1781 static void
1782 menu_grab_callback (GtkWidget *widget,
1783 gboolean ungrab_p,
1784 gpointer client_data)
1786 if (ungrab_p) menu_grab_callback_cnt--;
1787 else menu_grab_callback_cnt++;
1789 if (menu_grab_callback_cnt > 0 && ! xg_timer) xg_start_timer ();
1790 else if (menu_grab_callback_cnt == 0 && xg_timer) xg_stop_timer ();
1793 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1794 must be non-NULL) and can be inserted into a menu item.
1796 Returns the GtkHBox. */
1798 static GtkWidget *
1799 make_widget_for_menu_item (utf8_label, utf8_key)
1800 char *utf8_label;
1801 char *utf8_key;
1803 GtkWidget *wlbl;
1804 GtkWidget *wkey;
1805 GtkWidget *wbox;
1807 wbox = gtk_hbox_new (FALSE, 0);
1808 wlbl = gtk_label_new (utf8_label);
1809 wkey = gtk_label_new (utf8_key);
1811 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1812 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1814 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1815 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1817 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1818 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1819 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1821 return wbox;
1824 /* Make and return a menu item widget with the key to the right.
1825 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1826 UTF8_KEY is the text representing the key binding.
1827 ITEM is the widget_value describing the menu item.
1829 GROUP is an in/out parameter. If the menu item to be created is not
1830 part of any radio menu group, *GROUP contains NULL on entry and exit.
1831 If the menu item to be created is part of a radio menu group, on entry
1832 *GROUP contains the group to use, or NULL if this is the first item
1833 in the group. On exit, *GROUP contains the radio item group.
1835 Unfortunately, keys don't line up as nicely as in Motif,
1836 but the MacOS X version doesn't either, so I guess that is OK. */
1838 static GtkWidget *
1839 make_menu_item (utf8_label, utf8_key, item, group)
1840 char *utf8_label;
1841 char *utf8_key;
1842 widget_value *item;
1843 GSList **group;
1845 GtkWidget *w;
1846 GtkWidget *wtoadd = 0;
1848 /* It has been observed that some menu items have a NULL name field.
1849 This will lead to this function being called with a NULL utf8_label.
1850 GTK crashes on that so we set a blank label. Why there is a NULL
1851 name remains to be investigated. */
1852 if (! utf8_label) utf8_label = " ";
1854 if (utf8_key)
1855 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1857 if (item->button_type == BUTTON_TYPE_TOGGLE)
1859 *group = NULL;
1860 if (utf8_key) w = gtk_check_menu_item_new ();
1861 else w = gtk_check_menu_item_new_with_label (utf8_label);
1862 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1864 else if (item->button_type == BUTTON_TYPE_RADIO)
1866 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1867 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1868 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1869 if (item->selected)
1870 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1872 else
1874 *group = NULL;
1875 if (utf8_key) w = gtk_menu_item_new ();
1876 else w = gtk_menu_item_new_with_label (utf8_label);
1879 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1880 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1882 return w;
1885 /* Return non-zero if LABEL specifies a separator (GTK only has one
1886 separator type) */
1888 static char* separator_names[] = {
1889 "space",
1890 "no-line",
1891 "single-line",
1892 "double-line",
1893 "single-dashed-line",
1894 "double-dashed-line",
1895 "shadow-etched-in",
1896 "shadow-etched-out",
1897 "shadow-etched-in-dash",
1898 "shadow-etched-out-dash",
1899 "shadow-double-etched-in",
1900 "shadow-double-etched-out",
1901 "shadow-double-etched-in-dash",
1902 "shadow-double-etched-out-dash",
1906 static int
1907 xg_separator_p (char *label)
1909 if (! label) return 0;
1910 else if (strlen (label) > 3
1911 && strncmp (label, "--", 2) == 0
1912 && label[2] != '-')
1914 int i;
1916 label += 2;
1917 for (i = 0; separator_names[i]; ++i)
1918 if (strcmp (label, separator_names[i]) == 0)
1919 return 1;
1921 else
1923 /* Old-style separator, maybe. It's a separator if it contains
1924 only dashes. */
1925 while (*label == '-')
1926 ++label;
1927 if (*label == 0) return 1;
1930 return 0;
1933 static int xg_detached_menus;
1935 /* Returns non-zero if there are detached menus. */
1938 xg_have_tear_offs ()
1940 return xg_detached_menus > 0;
1943 /* Callback invoked when a detached menu window is removed. Here we
1944 decrease the xg_detached_menus count.
1945 WIDGET is the top level window that is removed (the parent of the menu).
1946 CLIENT_DATA is not used. */
1948 static void
1949 tearoff_remove (widget, client_data)
1950 GtkWidget *widget;
1951 gpointer client_data;
1953 if (xg_detached_menus > 0) --xg_detached_menus;
1956 /* Callback invoked when a menu is detached. It increases the
1957 xg_detached_menus count.
1958 WIDGET is the GtkTearoffMenuItem.
1959 CLIENT_DATA is not used. */
1961 static void
1962 tearoff_activate (widget, client_data)
1963 GtkWidget *widget;
1964 gpointer client_data;
1966 GtkWidget *menu = gtk_widget_get_parent (widget);
1967 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1969 ++xg_detached_menus;
1970 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1971 "destroy",
1972 G_CALLBACK (tearoff_remove), 0);
1977 /* Create a menu item widget, and connect the callbacks.
1978 ITEM decribes the menu item.
1979 F is the frame the created menu belongs to.
1980 SELECT_CB is the callback to use when a menu item is selected.
1981 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1982 CL_DATA points to the callback data to be used for this menu.
1983 GROUP is an in/out parameter. If the menu item to be created is not
1984 part of any radio menu group, *GROUP contains NULL on entry and exit.
1985 If the menu item to be created is part of a radio menu group, on entry
1986 *GROUP contains the group to use, or NULL if this is the first item
1987 in the group. On exit, *GROUP contains the radio item group.
1989 Returns the created GtkWidget. */
1991 static GtkWidget *
1992 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1993 widget_value *item;
1994 FRAME_PTR f;
1995 GCallback select_cb;
1996 GCallback highlight_cb;
1997 xg_menu_cb_data *cl_data;
1998 GSList **group;
2000 char *utf8_label;
2001 char *utf8_key;
2002 GtkWidget *w;
2003 xg_menu_item_cb_data *cb_data;
2005 utf8_label = get_utf8_string (item->name);
2006 utf8_key = get_utf8_string (item->key);
2008 w = make_menu_item (utf8_label, utf8_key, item, group);
2010 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2011 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
2013 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2015 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2017 cb_data->select_id = 0;
2018 cb_data->help = item->help;
2019 cb_data->cl_data = cl_data;
2020 cb_data->call_data = item->call_data;
2022 g_signal_connect (G_OBJECT (w),
2023 "destroy",
2024 G_CALLBACK (menuitem_destroy_callback),
2025 cb_data);
2027 /* Put cb_data in widget, so we can get at it when modifying menubar */
2028 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2030 /* final item, not a submenu */
2031 if (item->call_data && ! item->contents)
2033 if (select_cb)
2034 cb_data->select_id
2035 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2038 return w;
2041 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
2042 GCallback, GCallback, int, int, int,
2043 GtkWidget *, xg_menu_cb_data *, char *));
2045 /* Create a full menu tree specified by DATA.
2046 F is the frame the created menu belongs to.
2047 SELECT_CB is the callback to use when a menu item is selected.
2048 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2049 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2050 POP_UP_P is non-zero if we shall create a popup menu.
2051 MENU_BAR_P is non-zero if we shall create a menu bar.
2052 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2053 if MENU_BAR_P is non-zero.
2054 TOPMENU is the topmost GtkWidget that others shall be placed under.
2055 It may be NULL, in that case we create the appropriate widget
2056 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2057 CL_DATA is the callback data we shall use for this menu, or NULL
2058 if we haven't set the first callback yet.
2059 NAME is the name to give to the top level menu if this function
2060 creates it. May be NULL to not set any name.
2062 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2063 not NULL.
2065 This function calls itself to create submenus. */
2067 static GtkWidget *
2068 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
2069 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
2070 widget_value *data;
2071 FRAME_PTR f;
2072 GCallback select_cb;
2073 GCallback deactivate_cb;
2074 GCallback highlight_cb;
2075 int pop_up_p;
2076 int menu_bar_p;
2077 int add_tearoff_p;
2078 GtkWidget *topmenu;
2079 xg_menu_cb_data *cl_data;
2080 char *name;
2082 widget_value *item;
2083 GtkWidget *wmenu = topmenu;
2084 GSList *group = NULL;
2086 if (! topmenu)
2088 if (! menu_bar_p)
2090 wmenu = gtk_menu_new ();
2091 xg_set_screen (wmenu, f);
2092 /* Connect this to the menu instead of items so we get enter/leave for
2093 disabled items also. TODO: Still does not get enter/leave for
2094 disabled items in detached menus. */
2095 g_signal_connect (G_OBJECT (wmenu),
2096 "enter-notify-event",
2097 G_CALLBACK (menuitem_highlight_callback),
2098 NULL);
2099 g_signal_connect (G_OBJECT (wmenu),
2100 "leave-notify-event",
2101 G_CALLBACK (menuitem_highlight_callback),
2102 NULL);
2104 else wmenu = gtk_menu_bar_new ();
2106 /* Put cl_data on the top menu for easier access. */
2107 cl_data = make_cl_data (cl_data, f, highlight_cb);
2108 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2109 g_signal_connect (G_OBJECT (wmenu), "destroy",
2110 G_CALLBACK (menu_destroy_callback), cl_data);
2112 if (name)
2113 gtk_widget_set_name (wmenu, name);
2115 if (deactivate_cb)
2116 g_signal_connect (G_OBJECT (wmenu),
2117 "selection-done", deactivate_cb, 0);
2119 g_signal_connect (G_OBJECT (wmenu),
2120 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
2123 if (! menu_bar_p && add_tearoff_p)
2125 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2126 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2128 g_signal_connect (G_OBJECT (tearoff), "activate",
2129 G_CALLBACK (tearoff_activate), 0);
2132 for (item = data; item; item = item->next)
2134 GtkWidget *w;
2136 if (pop_up_p && !item->contents && !item->call_data
2137 && !xg_separator_p (item->name))
2139 char *utf8_label;
2140 /* A title for a popup. We do the same as GTK does when
2141 creating titles, but it does not look good. */
2142 group = NULL;
2143 utf8_label = get_utf8_string (item->name);
2145 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2146 w = gtk_menu_item_new_with_label (utf8_label);
2147 gtk_widget_set_sensitive (w, FALSE);
2148 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2150 else if (xg_separator_p (item->name))
2152 group = NULL;
2153 /* GTK only have one separator type. */
2154 w = gtk_separator_menu_item_new ();
2156 else
2158 w = xg_create_one_menuitem (item,
2160 item->contents ? 0 : select_cb,
2161 highlight_cb,
2162 cl_data,
2163 &group);
2165 /* Create a possibly empty submenu for menu bar items, since some
2166 themes don't highlight items correctly without it. */
2167 if (item->contents || menu_bar_p)
2169 GtkWidget *submenu = create_menus (item->contents,
2171 select_cb,
2172 deactivate_cb,
2173 highlight_cb,
2176 add_tearoff_p,
2178 cl_data,
2180 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2184 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2185 gtk_widget_set_name (w, MENU_ITEM_NAME);
2188 return wmenu;
2191 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2192 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2193 with some text and buttons.
2194 F is the frame the created item belongs to.
2195 NAME is the name to use for the top widget.
2196 VAL is a widget_value structure describing items to be created.
2197 SELECT_CB is the callback to use when a menu item is selected or
2198 a dialog button is pressed.
2199 DEACTIVATE_CB is the callback to use when an item is deactivated.
2200 For a menu, when a sub menu is not shown anymore, for a dialog it is
2201 called when the dialog is popped down.
2202 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2204 Returns the widget created. */
2206 GtkWidget *
2207 xg_create_widget (type, name, f, val,
2208 select_cb, deactivate_cb, highlight_cb)
2209 char *type;
2210 char *name;
2211 FRAME_PTR f;
2212 widget_value *val;
2213 GCallback select_cb;
2214 GCallback deactivate_cb;
2215 GCallback highlight_cb;
2217 GtkWidget *w = 0;
2218 int menu_bar_p = strcmp (type, "menubar") == 0;
2219 int pop_up_p = strcmp (type, "popup") == 0;
2221 if (strcmp (type, "dialog") == 0)
2223 w = create_dialog (val, select_cb, deactivate_cb);
2224 xg_set_screen (w, f);
2225 gtk_window_set_transient_for (GTK_WINDOW (w),
2226 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2227 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2228 gtk_widget_set_name (w, "emacs-dialog");
2229 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2231 else if (menu_bar_p || pop_up_p)
2233 w = create_menus (val->contents,
2235 select_cb,
2236 deactivate_cb,
2237 highlight_cb,
2238 pop_up_p,
2239 menu_bar_p,
2240 menu_bar_p,
2243 name);
2245 /* Set the cursor to an arrow for popup menus when they are mapped.
2246 This is done by default for menu bar menus. */
2247 if (pop_up_p)
2249 /* Must realize so the GdkWindow inside the widget is created. */
2250 gtk_widget_realize (w);
2251 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2254 else
2256 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2257 type);
2260 return w;
2263 /* Return the label for menu item WITEM. */
2265 static const char *
2266 xg_get_menu_item_label (witem)
2267 GtkMenuItem *witem;
2269 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2270 return gtk_label_get_label (wlabel);
2273 /* Return non-zero if the menu item WITEM has the text LABEL. */
2275 static int
2276 xg_item_label_same_p (witem, label)
2277 GtkMenuItem *witem;
2278 char *label;
2280 int is_same = 0;
2281 char *utf8_label = get_utf8_string (label);
2282 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2284 if (! old_label && ! utf8_label)
2285 is_same = 1;
2286 else if (old_label && utf8_label)
2287 is_same = strcmp (utf8_label, old_label) == 0;
2289 if (utf8_label && utf8_label != label) g_free (utf8_label);
2291 return is_same;
2294 /* Destroy widgets in LIST. */
2296 static void
2297 xg_destroy_widgets (list)
2298 GList *list;
2300 GList *iter;
2302 for (iter = list; iter; iter = g_list_next (iter))
2304 GtkWidget *w = GTK_WIDGET (iter->data);
2306 /* Destroying the widget will remove it from the container it is in. */
2307 gtk_widget_destroy (w);
2311 /* Update the top level names in MENUBAR (i.e. not submenus).
2312 F is the frame the menu bar belongs to.
2313 *LIST is a list with the current menu bar names (menu item widgets).
2314 ITER is the item within *LIST that shall be updated.
2315 POS is the numerical position, starting at 0, of ITER in *LIST.
2316 VAL describes what the menu bar shall look like after the update.
2317 SELECT_CB is the callback to use when a menu item is selected.
2318 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2319 CL_DATA points to the callback data to be used for this menu bar.
2321 This function calls itself to walk through the menu bar names. */
2323 static void
2324 xg_update_menubar (menubar, f, list, iter, pos, val,
2325 select_cb, deactivate_cb, highlight_cb, cl_data)
2326 GtkWidget *menubar;
2327 FRAME_PTR f;
2328 GList **list;
2329 GList *iter;
2330 int pos;
2331 widget_value *val;
2332 GCallback select_cb;
2333 GCallback deactivate_cb;
2334 GCallback highlight_cb;
2335 xg_menu_cb_data *cl_data;
2337 if (! iter && ! val)
2338 return;
2339 else if (iter && ! val)
2341 /* Item(s) have been removed. Remove all remaining items. */
2342 xg_destroy_widgets (iter);
2344 /* All updated. */
2345 val = 0;
2346 iter = 0;
2348 else if (! iter && val)
2350 /* Item(s) added. Add all new items in one call. */
2351 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2352 0, 1, 0, menubar, cl_data, 0);
2354 /* All updated. */
2355 val = 0;
2356 iter = 0;
2358 /* Below this neither iter or val is NULL */
2359 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2361 /* This item is still the same, check next item. */
2362 val = val->next;
2363 iter = g_list_next (iter);
2364 ++pos;
2366 else /* This item is changed. */
2368 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2369 GtkMenuItem *witem2 = 0;
2370 int val_in_menubar = 0;
2371 int iter_in_new_menubar = 0;
2372 GList *iter2;
2373 widget_value *cur;
2375 /* See if the changed entry (val) is present later in the menu bar */
2376 for (iter2 = iter;
2377 iter2 && ! val_in_menubar;
2378 iter2 = g_list_next (iter2))
2380 witem2 = GTK_MENU_ITEM (iter2->data);
2381 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2384 /* See if the current entry (iter) is present later in the
2385 specification for the new menu bar. */
2386 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2387 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2389 if (val_in_menubar && ! iter_in_new_menubar)
2391 int nr = pos;
2393 /* This corresponds to:
2394 Current: A B C
2395 New: A C
2396 Remove B. */
2398 gtk_widget_ref (GTK_WIDGET (witem));
2399 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2400 gtk_widget_destroy (GTK_WIDGET (witem));
2402 /* Must get new list since the old changed. */
2403 g_list_free (*list);
2404 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2405 while (nr-- > 0) iter = g_list_next (iter);
2407 else if (! val_in_menubar && ! iter_in_new_menubar)
2409 /* This corresponds to:
2410 Current: A B C
2411 New: A X C
2412 Rename B to X. This might seem to be a strange thing to do,
2413 since if there is a menu under B it will be totally wrong for X.
2414 But consider editing a C file. Then there is a C-mode menu
2415 (corresponds to B above).
2416 If then doing C-x C-f the minibuf menu (X above) replaces the
2417 C-mode menu. When returning from the minibuffer, we get
2418 back the C-mode menu. Thus we do:
2419 Rename B to X (C-mode to minibuf menu)
2420 Rename X to B (minibuf to C-mode menu).
2421 If the X menu hasn't been invoked, the menu under B
2422 is up to date when leaving the minibuffer. */
2423 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2424 char *utf8_label = get_utf8_string (val->name);
2425 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2427 gtk_label_set_text (wlabel, utf8_label);
2429 /* If this item has a submenu that has been detached, change
2430 the title in the WM decorations also. */
2431 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2432 /* Set the title of the detached window. */
2433 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2435 iter = g_list_next (iter);
2436 val = val->next;
2437 ++pos;
2439 else if (! val_in_menubar && iter_in_new_menubar)
2441 /* This corresponds to:
2442 Current: A B C
2443 New: A X B C
2444 Insert X. */
2446 int nr = pos;
2447 GList *group = 0;
2448 GtkWidget *w = xg_create_one_menuitem (val,
2450 select_cb,
2451 highlight_cb,
2452 cl_data,
2453 &group);
2455 /* Create a possibly empty submenu for menu bar items, since some
2456 themes don't highlight items correctly without it. */
2457 GtkWidget *submenu = create_menus (NULL, f,
2458 select_cb, deactivate_cb,
2459 highlight_cb,
2460 0, 0, 0, 0, cl_data, 0);
2461 gtk_widget_set_name (w, MENU_ITEM_NAME);
2462 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2463 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2465 g_list_free (*list);
2466 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2467 while (nr-- > 0) iter = g_list_next (iter);
2468 iter = g_list_next (iter);
2469 val = val->next;
2470 ++pos;
2472 else /* if (val_in_menubar && iter_in_new_menubar) */
2474 int nr = pos;
2475 /* This corresponds to:
2476 Current: A B C
2477 New: A C B
2478 Move C before B */
2480 gtk_widget_ref (GTK_WIDGET (witem2));
2481 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2482 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2483 GTK_WIDGET (witem2), pos);
2484 gtk_widget_unref (GTK_WIDGET (witem2));
2486 g_list_free (*list);
2487 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2488 while (nr-- > 0) iter = g_list_next (iter);
2489 if (iter) iter = g_list_next (iter);
2490 val = val->next;
2491 ++pos;
2495 /* Update the rest of the menu bar. */
2496 xg_update_menubar (menubar, f, list, iter, pos, val,
2497 select_cb, deactivate_cb, highlight_cb, cl_data);
2500 /* Update the menu item W so it corresponds to VAL.
2501 SELECT_CB is the callback to use when a menu item is selected.
2502 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2503 CL_DATA is the data to set in the widget for menu invokation. */
2505 static void
2506 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2507 widget_value *val;
2508 GtkWidget *w;
2509 GCallback select_cb;
2510 GCallback highlight_cb;
2511 xg_menu_cb_data *cl_data;
2513 GtkWidget *wchild;
2514 GtkLabel *wlbl = 0;
2515 GtkLabel *wkey = 0;
2516 char *utf8_label;
2517 char *utf8_key;
2518 const char *old_label = 0;
2519 const char *old_key = 0;
2520 xg_menu_item_cb_data *cb_data;
2522 wchild = gtk_bin_get_child (GTK_BIN (w));
2523 utf8_label = get_utf8_string (val->name);
2524 utf8_key = get_utf8_string (val->key);
2526 /* See if W is a menu item with a key. See make_menu_item above. */
2527 if (GTK_IS_HBOX (wchild))
2529 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2531 wlbl = GTK_LABEL (list->data);
2532 wkey = GTK_LABEL (list->next->data);
2533 g_list_free (list);
2535 if (! utf8_key)
2537 /* Remove the key and keep just the label. */
2538 gtk_widget_ref (GTK_WIDGET (wlbl));
2539 gtk_container_remove (GTK_CONTAINER (w), wchild);
2540 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2541 wkey = 0;
2545 else /* Just a label. */
2547 wlbl = GTK_LABEL (wchild);
2549 /* Check if there is now a key. */
2550 if (utf8_key)
2552 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2553 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2555 wlbl = GTK_LABEL (list->data);
2556 wkey = GTK_LABEL (list->next->data);
2557 g_list_free (list);
2559 gtk_container_remove (GTK_CONTAINER (w), wchild);
2560 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2565 if (wkey) old_key = gtk_label_get_label (wkey);
2566 if (wlbl) old_label = gtk_label_get_label (wlbl);
2568 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2569 gtk_label_set_text (wkey, utf8_key);
2571 if (! old_label || strcmp (utf8_label, old_label) != 0)
2572 gtk_label_set_text (wlbl, utf8_label);
2574 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2575 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2577 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2578 gtk_widget_set_sensitive (w, FALSE);
2579 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2580 gtk_widget_set_sensitive (w, TRUE);
2582 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2583 XG_ITEM_DATA);
2584 if (cb_data)
2586 cb_data->call_data = val->call_data;
2587 cb_data->help = val->help;
2588 cb_data->cl_data = cl_data;
2590 /* We assume the callback functions don't change. */
2591 if (val->call_data && ! val->contents)
2593 /* This item shall have a select callback. */
2594 if (! cb_data->select_id)
2595 cb_data->select_id
2596 = g_signal_connect (G_OBJECT (w), "activate",
2597 select_cb, cb_data);
2599 else if (cb_data->select_id)
2601 g_signal_handler_disconnect (w, cb_data->select_id);
2602 cb_data->select_id = 0;
2607 /* Update the toggle menu item W so it corresponds to VAL. */
2609 static void
2610 xg_update_toggle_item (val, w)
2611 widget_value *val;
2612 GtkWidget *w;
2614 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2617 /* Update the radio menu item W so it corresponds to VAL. */
2619 static void
2620 xg_update_radio_item (val, w)
2621 widget_value *val;
2622 GtkWidget *w;
2624 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2627 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2628 SUBMENU may be NULL, in that case a new menu is created.
2629 F is the frame the menu bar belongs to.
2630 VAL describes the contents of the menu bar.
2631 SELECT_CB is the callback to use when a menu item is selected.
2632 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2633 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2634 CL_DATA is the call back data to use for any newly created items.
2636 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2637 was NULL. */
2639 static GtkWidget *
2640 xg_update_submenu (submenu, f, val,
2641 select_cb, deactivate_cb, highlight_cb, cl_data)
2642 GtkWidget *submenu;
2643 FRAME_PTR f;
2644 widget_value *val;
2645 GCallback select_cb;
2646 GCallback deactivate_cb;
2647 GCallback highlight_cb;
2648 xg_menu_cb_data *cl_data;
2650 GtkWidget *newsub = submenu;
2651 GList *list = 0;
2652 GList *iter;
2653 widget_value *cur;
2654 int has_tearoff_p = 0;
2655 GList *first_radio = 0;
2657 if (submenu)
2658 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2660 for (cur = val, iter = list;
2661 cur && iter;
2662 iter = g_list_next (iter), cur = cur->next)
2664 GtkWidget *w = GTK_WIDGET (iter->data);
2666 /* Skip tearoff items, they have no counterpart in val. */
2667 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2669 has_tearoff_p = 1;
2670 iter = g_list_next (iter);
2671 if (iter) w = GTK_WIDGET (iter->data);
2672 else break;
2675 /* Remember first radio button in a group. If we get a mismatch in
2676 a radio group we must rebuild the whole group so that the connections
2677 in GTK becomes correct. */
2678 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2679 first_radio = iter;
2680 else if (cur->button_type != BUTTON_TYPE_RADIO
2681 && ! GTK_IS_RADIO_MENU_ITEM (w))
2682 first_radio = 0;
2684 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2686 if (! xg_separator_p (cur->name))
2687 break;
2689 else if (GTK_IS_CHECK_MENU_ITEM (w))
2691 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2692 break;
2693 xg_update_toggle_item (cur, w);
2694 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2696 else if (GTK_IS_RADIO_MENU_ITEM (w))
2698 if (cur->button_type != BUTTON_TYPE_RADIO)
2699 break;
2700 xg_update_radio_item (cur, w);
2701 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2703 else if (GTK_IS_MENU_ITEM (w))
2705 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2706 GtkWidget *sub;
2708 if (cur->button_type != BUTTON_TYPE_NONE ||
2709 xg_separator_p (cur->name))
2710 break;
2712 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2714 sub = gtk_menu_item_get_submenu (witem);
2715 if (sub && ! cur->contents)
2717 /* Not a submenu anymore. */
2718 gtk_widget_ref (sub);
2719 gtk_menu_item_remove_submenu (witem);
2720 gtk_widget_destroy (sub);
2722 else if (cur->contents)
2724 GtkWidget *nsub;
2726 nsub = xg_update_submenu (sub, f, cur->contents,
2727 select_cb, deactivate_cb,
2728 highlight_cb, cl_data);
2730 /* If this item just became a submenu, we must set it. */
2731 if (nsub != sub)
2732 gtk_menu_item_set_submenu (witem, nsub);
2735 else
2737 /* Structural difference. Remove everything from here and down
2738 in SUBMENU. */
2739 break;
2743 /* Remove widgets from first structual change. */
2744 if (iter)
2746 /* If we are adding new menu items below, we must remove from
2747 first radio button so that radio groups become correct. */
2748 if (cur && first_radio) xg_destroy_widgets (first_radio);
2749 else xg_destroy_widgets (iter);
2752 if (cur)
2754 /* More items added. Create them. */
2755 newsub = create_menus (cur,
2757 select_cb,
2758 deactivate_cb,
2759 highlight_cb,
2762 ! has_tearoff_p,
2763 submenu,
2764 cl_data,
2768 if (list) g_list_free (list);
2770 return newsub;
2773 /* Update the MENUBAR.
2774 F is the frame the menu bar belongs to.
2775 VAL describes the contents of the menu bar.
2776 If DEEP_P is non-zero, rebuild all but the top level menu names in
2777 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2778 SELECT_CB is the callback to use when a menu item is selected.
2779 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2780 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2782 void
2783 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2784 select_cb, deactivate_cb, highlight_cb)
2785 GtkWidget *menubar;
2786 FRAME_PTR f;
2787 widget_value *val;
2788 int deep_p;
2789 GCallback select_cb;
2790 GCallback deactivate_cb;
2791 GCallback highlight_cb;
2793 xg_menu_cb_data *cl_data;
2794 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2796 if (! list) return;
2798 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2799 XG_FRAME_DATA);
2801 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2802 select_cb, deactivate_cb, highlight_cb, cl_data);
2804 if (deep_p)
2806 widget_value *cur;
2808 /* Update all sub menus.
2809 We must keep the submenus (GTK menu item widgets) since the
2810 X Window in the XEvent that activates the menu are those widgets. */
2812 /* Update cl_data, menu_item things in F may have changed. */
2813 update_cl_data (cl_data, f, highlight_cb);
2815 for (cur = val->contents; cur; cur = cur->next)
2817 GList *iter;
2818 GtkWidget *sub = 0;
2819 GtkWidget *newsub;
2820 GtkMenuItem *witem;
2822 /* Find sub menu that corresponds to val and update it. */
2823 for (iter = list ; iter; iter = g_list_next (iter))
2825 witem = GTK_MENU_ITEM (iter->data);
2826 if (xg_item_label_same_p (witem, cur->name))
2828 sub = gtk_menu_item_get_submenu (witem);
2829 break;
2833 newsub = xg_update_submenu (sub,
2835 cur->contents,
2836 select_cb,
2837 deactivate_cb,
2838 highlight_cb,
2839 cl_data);
2840 /* sub may still be NULL. If we just updated non deep and added
2841 a new menu bar item, it has no sub menu yet. So we set the
2842 newly created sub menu under witem. */
2843 if (newsub != sub)
2845 xg_set_screen (newsub, f);
2846 gtk_menu_item_set_submenu (witem, newsub);
2851 g_list_free (list);
2852 gtk_widget_show_all (menubar);
2855 /* Recompute all the widgets of frame F, when the menu bar has been
2856 changed. Value is non-zero if widgets were updated. */
2859 xg_update_frame_menubar (f)
2860 FRAME_PTR f;
2862 struct x_output *x = f->output_data.x;
2863 GtkRequisition req;
2865 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2866 return 0;
2868 BLOCK_INPUT;
2870 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2871 FALSE, FALSE, 0);
2872 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2874 gtk_widget_show_all (x->menubar_widget);
2875 gtk_widget_size_request (x->menubar_widget, &req);
2877 FRAME_MENUBAR_HEIGHT (f) = req.height;
2879 /* The height has changed, resize outer widget and set columns
2880 rows to what we had before adding the menu bar. */
2881 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2883 SET_FRAME_GARBAGED (f);
2884 UNBLOCK_INPUT;
2886 return 1;
2889 /* Get rid of the menu bar of frame F, and free its storage.
2890 This is used when deleting a frame, and when turning off the menu bar. */
2892 void
2893 free_frame_menubar (f)
2894 FRAME_PTR f;
2896 struct x_output *x = f->output_data.x;
2898 if (x->menubar_widget)
2900 BLOCK_INPUT;
2902 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2903 /* The menubar and its children shall be deleted when removed from
2904 the container. */
2905 x->menubar_widget = 0;
2906 FRAME_MENUBAR_HEIGHT (f) = 0;
2908 /* The height has changed, resize outer widget and set columns
2909 rows to what we had before removing the menu bar. */
2910 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2912 SET_FRAME_GARBAGED (f);
2913 UNBLOCK_INPUT;
2919 /***********************************************************************
2920 Scroll bar functions
2921 ***********************************************************************/
2924 /* Setting scroll bar values invokes the callback. Use this variable
2925 to indicate that callback should do nothing. */
2927 int xg_ignore_gtk_scrollbar;
2929 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2930 32 bits. But we want to store pointers, and they may be larger
2931 than 32 bits. Keep a mapping from integer index to widget pointers
2932 to get around the 32 bit limitation. */
2934 static struct
2936 GtkWidget **widgets;
2937 int max_size;
2938 int used;
2939 } id_to_widget;
2941 /* Grow this much every time we need to allocate more */
2943 #define ID_TO_WIDGET_INCR 32
2945 /* Store the widget pointer W in id_to_widget and return the integer index. */
2947 static int
2948 xg_store_widget_in_map (w)
2949 GtkWidget *w;
2951 int i;
2953 if (id_to_widget.max_size == id_to_widget.used)
2955 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2957 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2958 sizeof (GtkWidget *)*new_size);
2960 for (i = id_to_widget.max_size; i < new_size; ++i)
2961 id_to_widget.widgets[i] = 0;
2962 id_to_widget.max_size = new_size;
2965 /* Just loop over the array and find a free place. After all,
2966 how many scroll bars are we creating? Should be a small number.
2967 The check above guarantees we will find a free place. */
2968 for (i = 0; i < id_to_widget.max_size; ++i)
2970 if (! id_to_widget.widgets[i])
2972 id_to_widget.widgets[i] = w;
2973 ++id_to_widget.used;
2975 return i;
2979 /* Should never end up here */
2980 abort ();
2983 /* Remove pointer at IDX from id_to_widget.
2984 Called when scroll bar is destroyed. */
2986 static void
2987 xg_remove_widget_from_map (idx)
2988 int idx;
2990 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2992 id_to_widget.widgets[idx] = 0;
2993 --id_to_widget.used;
2997 /* Get the widget pointer at IDX from id_to_widget. */
2999 static GtkWidget *
3000 xg_get_widget_from_map (idx)
3001 int idx;
3003 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3004 return id_to_widget.widgets[idx];
3006 return 0;
3009 /* Return the scrollbar id for X Window WID on display DPY.
3010 Return -1 if WID not in id_to_widget. */
3013 xg_get_scroll_id_for_window (dpy, wid)
3014 Display *dpy;
3015 Window wid;
3017 int idx;
3018 GtkWidget *w;
3020 w = xg_win_to_widget (dpy, wid);
3022 if (w)
3024 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3025 if (id_to_widget.widgets[idx] == w)
3026 return idx;
3029 return -1;
3032 /* Callback invoked when scroll bar WIDGET is destroyed.
3033 DATA is the index into id_to_widget for WIDGET.
3034 We free pointer to last scroll bar values here and remove the index. */
3036 static void
3037 xg_gtk_scroll_destroy (widget, data)
3038 GtkWidget *widget;
3039 gpointer data;
3041 gpointer p;
3042 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3044 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
3045 if (p) xfree (p);
3046 xg_remove_widget_from_map (id);
3049 /* Callback for button press/release events. Used to start timer so that
3050 the scroll bar repetition timer in GTK gets handeled.
3051 Also, sets bar->dragging to Qnil when dragging (button release) is done.
3052 WIDGET is the scroll bar widget the event is for (not used).
3053 EVENT contains the event.
3054 USER_DATA points to the struct scrollbar structure.
3056 Returns FALSE to tell GTK that it shall continue propagate the event
3057 to widgets. */
3059 static gboolean
3060 scroll_bar_button_cb (widget, event, user_data)
3061 GtkWidget *widget;
3062 GdkEventButton *event;
3063 gpointer user_data;
3065 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
3066 xg_start_timer ();
3067 else if (event->type == GDK_BUTTON_RELEASE)
3069 struct scroll_bar *bar = (struct scroll_bar *) user_data;
3070 if (xg_timer) xg_stop_timer ();
3071 bar->dragging = Qnil;
3074 return FALSE;
3077 /* Create a scroll bar widget for frame F. Store the scroll bar
3078 in BAR.
3079 SCROLL_CALLBACK is the callback to invoke when the value of the
3080 bar changes.
3081 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3082 to set resources for the widget. */
3084 void
3085 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
3086 FRAME_PTR f;
3087 struct scroll_bar *bar;
3088 GCallback scroll_callback;
3089 char *scroll_bar_name;
3091 GtkWidget *wscroll;
3092 GtkWidget *webox;
3093 GtkObject *vadj;
3094 int scroll_id;
3096 /* Page, step increment values are not so important here, they
3097 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3098 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3099 0.1, 0.1, 0.1);
3101 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3102 webox = gtk_event_box_new ();
3103 gtk_widget_set_name (wscroll, scroll_bar_name);
3104 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3106 scroll_id = xg_store_widget_in_map (wscroll);
3108 g_signal_connect (G_OBJECT (wscroll),
3109 "value-changed",
3110 scroll_callback,
3111 (gpointer) bar);
3112 /* The EMACS_INT cast avoids a warning. */
3113 g_signal_connect (G_OBJECT (wscroll),
3114 "destroy",
3115 G_CALLBACK (xg_gtk_scroll_destroy),
3116 (gpointer) (EMACS_INT) scroll_id);
3118 /* Connect to button press and button release to detect if any scroll bar
3119 has the pointer. */
3120 g_signal_connect (G_OBJECT (wscroll),
3121 "button-press-event",
3122 G_CALLBACK (scroll_bar_button_cb),
3123 (gpointer) bar);
3124 g_signal_connect (G_OBJECT (wscroll),
3125 "button-release-event",
3126 G_CALLBACK (scroll_bar_button_cb),
3127 (gpointer) bar);
3129 /* The scroll bar widget does not draw on a window of its own. Instead
3130 it draws on the parent window, in this case the edit widget. So
3131 whenever the edit widget is cleared, the scroll bar needs to redraw
3132 also, which causes flicker. Put an event box between the edit widget
3133 and the scroll bar, so the scroll bar instead draws itself on the
3134 event box window. */
3135 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3136 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3139 /* Set the cursor to an arrow. */
3140 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3142 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
3145 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3147 void
3148 xg_show_scroll_bar (scrollbar_id)
3149 int scrollbar_id;
3151 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3152 if (w)
3153 gtk_widget_show_all (gtk_widget_get_parent (w));
3156 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3158 void
3159 xg_remove_scroll_bar (f, scrollbar_id)
3160 FRAME_PTR f;
3161 int scrollbar_id;
3163 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3164 if (w)
3166 GtkWidget *wparent = gtk_widget_get_parent (w);
3167 gtk_widget_destroy (w);
3168 gtk_widget_destroy (wparent);
3169 SET_FRAME_GARBAGED (f);
3173 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3174 in frame F.
3175 TOP/LEFT are the new pixel positions where the bar shall appear.
3176 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3178 void
3179 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3180 FRAME_PTR f;
3181 int scrollbar_id;
3182 int top;
3183 int left;
3184 int width;
3185 int height;
3188 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3190 if (wscroll)
3192 GtkWidget *wfixed = f->output_data.x->edit_widget;
3193 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3195 /* Move and resize to new values. */
3196 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3197 gtk_widget_set_size_request (wscroll, width, height);
3198 gtk_widget_queue_draw (wparent);
3199 gdk_window_process_all_updates ();
3200 /* GTK does not redraw until the main loop is entered again, but
3201 if there are no X events pending we will not enter it. So we sync
3202 here to get some events. */
3203 x_sync (f);
3204 SET_FRAME_GARBAGED (f);
3205 cancel_mouse_face (f);
3209 /* Set the thumb size and position of scroll bar BAR. We are currently
3210 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3212 void
3213 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3214 struct scroll_bar *bar;
3215 int portion, position, whole;
3217 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
3219 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3221 if (wscroll && NILP (bar->dragging))
3223 GtkAdjustment *adj;
3224 gdouble shown;
3225 gdouble top;
3226 int size, value;
3227 int new_step;
3228 int changed = 0;
3230 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3232 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3233 rather than the real portion value. This makes the thumb less likely
3234 to resize and that looks better. */
3235 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3236 /* When the thumb is at the bottom, position == whole.
3237 So we need to increase `whole' to make space for the thumb. */
3238 whole += portion;
3240 if (whole <= 0)
3241 top = 0, shown = 1;
3242 else
3244 top = (gdouble) position / whole;
3245 shown = (gdouble) portion / whole;
3248 size = shown * XG_SB_RANGE;
3249 size = min (size, XG_SB_RANGE);
3250 size = max (size, 1);
3252 value = top * XG_SB_RANGE;
3253 value = min (value, XG_SB_MAX - size);
3254 value = max (value, XG_SB_MIN);
3256 /* Assume all lines are of equal size. */
3257 new_step = size / max (1, FRAME_LINES (f));
3259 if ((int) adj->page_size != size
3260 || (int) adj->step_increment != new_step)
3262 adj->page_size = size;
3263 adj->step_increment = new_step;
3264 /* Assume a page increment is about 95% of the page size */
3265 adj->page_increment = (int) (0.95*adj->page_size);
3266 changed = 1;
3269 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3271 GtkWidget *wfixed = f->output_data.x->edit_widget;
3273 BLOCK_INPUT;
3275 /* gtk_range_set_value invokes the callback. Set
3276 ignore_gtk_scrollbar to make the callback do nothing */
3277 xg_ignore_gtk_scrollbar = 1;
3279 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3280 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3281 else if (changed)
3282 gtk_adjustment_changed (adj);
3284 xg_ignore_gtk_scrollbar = 0;
3286 UNBLOCK_INPUT;
3292 /***********************************************************************
3293 Tool bar functions
3294 ***********************************************************************/
3295 /* The key for the data we put in the GtkImage widgets. The data is
3296 the image used by Emacs. We use this to see if we need to update
3297 the GtkImage with a new image. */
3298 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3300 /* The key for storing the latest modifiers so the activate callback can
3301 get them. */
3302 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3304 /* The key for storing the button widget in its proxy menu item. */
3305 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3307 /* The key for the data we put in the GtkImage widgets. The data is
3308 the stock name used by Emacs. We use this to see if we need to update
3309 the GtkImage with a new image. */
3310 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3312 /* As above, but this is used for named theme widgets, as opposed to
3313 stock items. */
3314 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3316 /* Callback function invoked when a tool bar item is pressed.
3317 W is the button widget in the tool bar that got pressed,
3318 CLIENT_DATA is an integer that is the index of the button in the
3319 tool bar. 0 is the first button. */
3321 static gboolean
3322 xg_tool_bar_button_cb (widget, event, user_data)
3323 GtkWidget *widget;
3324 GdkEventButton *event;
3325 gpointer user_data;
3327 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3328 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3329 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3330 return FALSE;
3334 /* Callback function invoked when a tool bar item is pressed.
3335 W is the button widget in the tool bar that got pressed,
3336 CLIENT_DATA is an integer that is the index of the button in the
3337 tool bar. 0 is the first button. */
3339 static void
3340 xg_tool_bar_callback (w, client_data)
3341 GtkWidget *w;
3342 gpointer client_data;
3344 /* The EMACS_INT cast avoids a warning. */
3345 int idx = (int) (EMACS_INT) client_data;
3346 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3347 XG_TOOL_BAR_LAST_MODIFIER);
3349 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3350 Lisp_Object key, frame;
3351 struct input_event event;
3352 EVENT_INIT (event);
3354 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3355 return;
3357 idx *= TOOL_BAR_ITEM_NSLOTS;
3359 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3360 XSETFRAME (frame, f);
3362 /* We generate two events here. The first one is to set the prefix
3363 to `(tool_bar)', see keyboard.c. */
3364 event.kind = TOOL_BAR_EVENT;
3365 event.frame_or_window = frame;
3366 event.arg = frame;
3367 kbd_buffer_store_event (&event);
3369 event.kind = TOOL_BAR_EVENT;
3370 event.frame_or_window = frame;
3371 event.arg = key;
3372 /* Convert between the modifier bits GDK uses and the modifier bits
3373 Emacs uses. This assumes GDK an X masks are the same, which they are when
3374 this is written. */
3375 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3376 kbd_buffer_store_event (&event);
3379 /* Callback function invoked when a tool bar item is pressed in a detached
3380 tool bar or the overflow drop down menu.
3381 We just call xg_tool_bar_callback.
3382 W is the menu item widget that got pressed,
3383 CLIENT_DATA is an integer that is the index of the button in the
3384 tool bar. 0 is the first button. */
3386 static void
3387 xg_tool_bar_proxy_callback (w, client_data)
3388 GtkWidget *w;
3389 gpointer client_data;
3391 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3392 XG_TOOL_BAR_PROXY_BUTTON));
3393 xg_tool_bar_callback (wbutton, client_data);
3396 /* This callback is called when a tool item should create a proxy item,
3397 such as for the overflow menu. Also called when the tool bar is detached.
3398 If we don't create a proxy menu item, the detached tool bar will be
3399 blank. */
3401 static gboolean
3402 xg_tool_bar_menu_proxy (toolitem, user_data)
3403 GtkToolItem *toolitem;
3404 gpointer user_data;
3406 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3407 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3408 GtkWidget *wmenuitem = gtk_image_menu_item_new ();
3409 GtkWidget *wmenuimage;
3411 if (gtk_button_get_use_stock (wbutton))
3412 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3413 GTK_ICON_SIZE_MENU);
3414 else
3416 GtkImage *wimage = GTK_IMAGE (gtk_bin_get_child (GTK_BIN (wbutton)));
3417 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3418 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3419 if (store_type == GTK_IMAGE_STOCK)
3421 gchar *stock_id;
3422 gtk_image_get_stock (wimage, &stock_id, NULL);
3423 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3425 else if (store_type == GTK_IMAGE_ICON_SET)
3427 GtkIconSet *icon_set;
3428 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3429 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3430 GTK_ICON_SIZE_MENU);
3432 else if (store_type == GTK_IMAGE_PIXBUF)
3434 gint width, height;
3436 if (settings &&
3437 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3438 &width, &height))
3440 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3442 src_pixbuf = gtk_image_get_pixbuf (wimage);
3443 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3444 GDK_INTERP_BILINEAR);
3446 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3448 else
3450 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3451 abort ();
3454 else if (store_type == GTK_IMAGE_ICON_NAME)
3456 const gchar *icon_name;
3457 GtkIconSize icon_size;
3459 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3460 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3461 GTK_ICON_SIZE_MENU);
3463 else
3465 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3466 abort ();
3469 if (wmenuimage)
3470 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3472 g_signal_connect (G_OBJECT (wmenuitem),
3473 "activate",
3474 GTK_SIGNAL_FUNC (xg_tool_bar_proxy_callback),
3475 user_data);
3477 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3478 (gpointer) wbutton);
3479 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3481 return TRUE;
3484 /* This callback is called when a tool bar is detached. We must set
3485 the height of the tool bar to zero when this happens so frame sizes
3486 are correctly calculated.
3487 WBOX is the handle box widget that enables detach/attach of the tool bar.
3488 W is the tool bar widget.
3489 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3491 static void
3492 xg_tool_bar_detach_callback (wbox, w, client_data)
3493 GtkHandleBox *wbox;
3494 GtkWidget *w;
3495 gpointer client_data;
3497 FRAME_PTR f = (FRAME_PTR) client_data;
3498 extern int x_gtk_whole_detached_tool_bar;
3500 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3501 NULL);
3503 if (f)
3505 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3507 /* When detaching a tool bar, not everything dissapear. There are
3508 a few pixels left that are used to drop the tool bar back into
3509 place. */
3510 FRAME_TOOLBAR_HEIGHT (f) = 2;
3512 /* The height has changed, resize outer widget and set columns
3513 rows to what we had before detaching the tool bar. */
3514 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3518 /* This callback is called when a tool bar is reattached. We must set
3519 the height of the tool bar when this happens so frame sizes
3520 are correctly calculated.
3521 WBOX is the handle box widget that enables detach/attach of the tool bar.
3522 W is the tool bar widget.
3523 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3525 static void
3526 xg_tool_bar_attach_callback (wbox, w, client_data)
3527 GtkHandleBox *wbox;
3528 GtkWidget *w;
3529 gpointer client_data;
3531 FRAME_PTR f = (FRAME_PTR) client_data;
3532 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3534 if (f)
3536 GtkRequisition req;
3538 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3540 gtk_widget_size_request (w, &req);
3541 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3543 /* The height has changed, resize outer widget and set columns
3544 rows to what we had before attaching the tool bar. */
3545 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3549 /* This callback is called when the mouse enters or leaves a tool bar item.
3550 It is used for displaying and hiding the help text.
3551 W is the tool bar item, a button.
3552 EVENT is either an enter event or leave event.
3553 CLIENT_DATA is an integer that is the index of the button in the
3554 tool bar. 0 is the first button.
3556 Returns FALSE to tell GTK to keep processing this event. */
3558 static gboolean
3559 xg_tool_bar_help_callback (w, event, client_data)
3560 GtkWidget *w;
3561 GdkEventCrossing *event;
3562 gpointer client_data;
3564 /* The EMACS_INT cast avoids a warning. */
3565 int idx = (int) (EMACS_INT) client_data;
3566 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3567 Lisp_Object help, frame;
3569 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3570 return FALSE;
3572 if (event->type == GDK_ENTER_NOTIFY)
3574 idx *= TOOL_BAR_ITEM_NSLOTS;
3575 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3577 if (NILP (help))
3578 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3580 else
3581 help = Qnil;
3583 XSETFRAME (frame, f);
3584 kbd_buffer_store_help_event (frame, help);
3586 return FALSE;
3590 /* This callback is called when a tool bar item shall be redrawn.
3591 It modifies the expose event so that the GtkImage widget redraws the
3592 whole image. This to overcome a bug that makes GtkImage draw the image
3593 in the wrong place when it tries to redraw just a part of the image.
3594 W is the GtkImage to be redrawn.
3595 EVENT is the expose event for W.
3596 CLIENT_DATA is unused.
3598 Returns FALSE to tell GTK to keep processing this event. */
3600 static gboolean
3601 xg_tool_bar_item_expose_callback (w, event, client_data)
3602 GtkWidget *w;
3603 GdkEventExpose *event;
3604 gpointer client_data;
3606 gint width, height;
3608 gdk_drawable_get_size (event->window, &width, &height);
3610 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3611 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3613 event->area.x = max (0, event->area.x);
3614 event->area.y = max (0, event->area.y);
3616 event->area.width = max (width, event->area.width);
3617 event->area.height = max (height, event->area.height);
3619 return FALSE;
3622 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3625 /* Create a tool bar for frame F. */
3627 static void
3628 xg_create_tool_bar (f)
3629 FRAME_PTR f;
3631 struct x_output *x = f->output_data.x;
3632 GtkRequisition req;
3633 int vbox_pos = x->menubar_widget ? 1 : 0;
3635 x->toolbar_widget = gtk_toolbar_new ();
3636 x->handlebox_widget = gtk_handle_box_new ();
3637 x->toolbar_detached = 0;
3639 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3640 x->toolbar_widget);
3642 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3643 FALSE, FALSE, 0);
3645 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3646 vbox_pos);
3648 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3650 /* We only have icons, so override any user setting. We could
3651 use the caption property of the toolbar item (see update_frame_tool_bar
3652 below), but some of those strings are long, making the toolbar so
3653 long it does not fit on the screen. The GtkToolbar widget makes every
3654 item equal size, so the longest caption determine the size of every
3655 tool bar item. I think the creators of the GtkToolbar widget
3656 counted on 4 or 5 character long strings. */
3657 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3658 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3659 GTK_ORIENTATION_HORIZONTAL);
3661 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3662 G_CALLBACK (xg_tool_bar_detach_callback), f);
3663 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3664 G_CALLBACK (xg_tool_bar_attach_callback), f);
3666 gtk_widget_show_all (x->handlebox_widget);
3668 gtk_widget_size_request (x->toolbar_widget, &req);
3669 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3671 /* The height has changed, resize outer widget and set columns
3672 rows to what we had before adding the tool bar. */
3673 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3675 SET_FRAME_GARBAGED (f);
3678 /* Find the right-to-left image named by RTL in the tool bar images for F.
3679 Returns IMAGE if RTL is not found. */
3681 static Lisp_Object
3682 find_rtl_image (f, image, rtl)
3683 FRAME_PTR f;
3684 Lisp_Object image;
3685 Lisp_Object rtl;
3687 int i;
3688 Lisp_Object file, rtl_name;
3689 struct gcpro gcpro1, gcpro2;
3690 GCPRO2 (file, rtl_name);
3692 rtl_name = Ffile_name_nondirectory (rtl);
3694 for (i = 0; i < f->n_tool_bar_items; ++i)
3696 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3697 if (!NILP (file = file_for_image (rtl_image)))
3699 file = call1 (intern ("file-name-sans-extension"),
3700 Ffile_name_nondirectory (file));
3701 if (EQ (Fequal (file, rtl_name), Qt))
3703 image = rtl_image;
3704 break;
3709 return image;
3712 /* Update the tool bar for frame F. Add new buttons and remove old. */
3714 void
3715 update_frame_tool_bar (f)
3716 FRAME_PTR f;
3718 int i;
3719 GtkRequisition old_req, new_req;
3720 struct x_output *x = f->output_data.x;
3721 int hmargin = 0, vmargin = 0;
3722 GtkToolbar *wtoolbar;
3723 GtkToolItem *ti;
3724 GtkTextDirection dir;
3726 if (! FRAME_GTK_WIDGET (f))
3727 return;
3729 BLOCK_INPUT;
3731 if (INTEGERP (Vtool_bar_button_margin)
3732 && XINT (Vtool_bar_button_margin) > 0)
3734 hmargin = XFASTINT (Vtool_bar_button_margin);
3735 vmargin = XFASTINT (Vtool_bar_button_margin);
3737 else if (CONSP (Vtool_bar_button_margin))
3739 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3740 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3741 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3743 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3744 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3745 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3748 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3749 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3750 i.e. zero. This means that margins less than
3751 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3752 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3753 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3755 if (! x->toolbar_widget)
3756 xg_create_tool_bar (f);
3758 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
3759 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &old_req);
3760 dir = gtk_widget_get_direction (x->toolbar_widget);
3762 for (i = 0; i < f->n_tool_bar_items; ++i)
3765 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3766 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3767 int idx;
3768 int img_id;
3769 int icon_size = 0;
3770 struct image *img = NULL;
3771 Lisp_Object image;
3772 Lisp_Object stock;
3773 GtkStockItem stock_item;
3774 char *stock_name = NULL;
3775 char *icon_name = NULL;
3776 Lisp_Object rtl;
3777 GtkWidget *wbutton = NULL;
3778 GtkWidget *weventbox;
3779 Lisp_Object func = intern ("x-gtk-map-stock");
3780 Lisp_Object specified_file;
3782 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
3784 if (ti)
3786 weventbox = gtk_bin_get_child (GTK_BIN (ti));
3787 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3790 image = PROP (TOOL_BAR_ITEM_IMAGES);
3792 /* Ignore invalid image specifications. */
3793 if (!valid_image_p (image))
3795 if (wbutton) gtk_widget_hide (wbutton);
3796 continue;
3799 specified_file = file_for_image (image);
3800 if (!NILP (specified_file) && EQ (Qt, Ffboundp (func)))
3801 stock = call1 (func, specified_file);
3803 if (! NILP (stock) && STRINGP (stock))
3805 stock_name = SSDATA (stock);
3806 if (stock_name[0] == 'n' && stock_name[1] == ':')
3808 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
3809 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
3811 icon_name = stock_name + 2;
3812 stock_name = NULL;
3813 stock = Qnil;
3815 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
3816 icon_name = NULL;
3817 else
3818 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3820 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
3821 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3822 else
3824 stock = Qnil;
3825 stock_name = NULL;
3829 if (stock_name == NULL && icon_name == NULL)
3831 /* No stock image, or stock item not known. Try regular image. */
3833 /* If image is a vector, choose the image according to the
3834 button state. */
3835 if (dir == GTK_TEXT_DIR_RTL
3836 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
3837 && STRINGP (rtl))
3839 image = find_rtl_image (f, image, rtl);
3842 if (VECTORP (image))
3844 if (enabled_p)
3845 idx = (selected_p
3846 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3847 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3848 else
3849 idx = (selected_p
3850 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3851 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3853 xassert (ASIZE (image) >= idx);
3854 image = AREF (image, idx);
3856 else
3857 idx = -1;
3859 img_id = lookup_image (f, image);
3860 img = IMAGE_FROM_ID (f, img_id);
3861 prepare_image_for_display (f, img);
3863 if (img->load_failed_p || img->pixmap == None)
3865 if (ti)
3866 gtk_widget_hide_all (GTK_WIDGET (ti));
3867 else
3869 /* Insert an empty (non-image) button */
3870 weventbox = gtk_event_box_new ();
3871 wbutton = gtk_button_new ();
3872 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3873 gtk_button_set_relief (GTK_BUTTON (wbutton),
3874 GTK_RELIEF_NONE);
3875 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3876 ti = gtk_tool_item_new ();
3877 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3878 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3880 continue;
3884 if (ti == NULL)
3886 GtkWidget *w;
3887 if (stock_name)
3889 w = gtk_image_new_from_stock (stock_name, icon_size);
3890 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
3891 (gpointer) xstrdup (stock_name),
3892 (GDestroyNotify) xfree);
3894 else if (icon_name)
3896 w = gtk_image_new_from_icon_name (icon_name, icon_size);
3897 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
3898 (gpointer) xstrdup (icon_name),
3899 (GDestroyNotify) xfree);
3901 else
3903 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3904 /* Save the image so we can see if an update is needed when
3905 this function is called again. */
3906 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3907 (gpointer)img->pixmap);
3910 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3911 wbutton = gtk_button_new ();
3912 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3913 gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
3914 gtk_container_add (GTK_CONTAINER (wbutton), w);
3915 weventbox = gtk_event_box_new ();
3916 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3917 ti = gtk_tool_item_new ();
3918 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3919 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3922 /* The EMACS_INT cast avoids a warning. */
3923 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
3924 GTK_SIGNAL_FUNC (xg_tool_bar_menu_proxy),
3925 (gpointer) (EMACS_INT) i);
3927 g_signal_connect (G_OBJECT (wbutton), "clicked",
3928 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3929 (gpointer) (EMACS_INT) i);
3931 gtk_widget_show_all (GTK_WIDGET (ti));
3934 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
3936 /* Catch expose events to overcome an annoying redraw bug, see
3937 comment for xg_tool_bar_item_expose_callback. */
3938 g_signal_connect (G_OBJECT (ti),
3939 "expose-event",
3940 G_CALLBACK (xg_tool_bar_item_expose_callback),
3943 gtk_widget_set_sensitive (wbutton, enabled_p);
3944 gtk_tool_item_set_homogeneous (ti, FALSE);
3946 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
3947 no distinction based on modifiers in the activate callback,
3948 so we have to do it ourselves. */
3949 g_signal_connect (wbutton, "button-release-event",
3950 GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
3951 NULL);
3953 g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
3955 /* Use enter/leave notify to show help. We use the events
3956 rather than the GtkButton specific signals "enter" and
3957 "leave", so we can have only one callback. The event
3958 will tell us what kind of event it is. */
3959 /* The EMACS_INT cast avoids a warning. */
3960 g_signal_connect (G_OBJECT (weventbox),
3961 "enter-notify-event",
3962 G_CALLBACK (xg_tool_bar_help_callback),
3963 (gpointer) (EMACS_INT) i);
3964 g_signal_connect (G_OBJECT (weventbox),
3965 "leave-notify-event",
3966 G_CALLBACK (xg_tool_bar_help_callback),
3967 (gpointer) (EMACS_INT) i);
3969 else
3971 GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
3972 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3973 XG_TOOL_BAR_IMAGE_DATA);
3974 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
3975 XG_TOOL_BAR_STOCK_NAME);
3976 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
3977 XG_TOOL_BAR_ICON_NAME);
3978 if (stock_name &&
3979 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
3981 gtk_image_set_from_stock (GTK_IMAGE (wimage),
3982 stock_name, icon_size);
3983 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
3984 (gpointer) xstrdup (stock_name),
3985 (GDestroyNotify) xfree);
3986 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3987 NULL);
3988 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
3990 else if (icon_name &&
3991 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
3993 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
3994 icon_name, icon_size);
3995 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
3996 (gpointer) xstrdup (icon_name),
3997 (GDestroyNotify) xfree);
3998 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3999 NULL);
4000 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4001 NULL);
4003 else if (img && old_img != img->pixmap)
4005 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
4006 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4007 (gpointer)img->pixmap);
4009 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4010 NULL);
4011 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4014 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4016 gtk_widget_set_sensitive (wbutton, enabled_p);
4017 gtk_widget_show_all (GTK_WIDGET (ti));
4020 #undef PROP
4023 /* Remove buttons not longer needed. We just hide them so they
4024 can be reused later on. */
4027 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
4028 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4029 } while (ti != NULL);
4031 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &new_req);
4032 if (old_req.height != new_req.height
4033 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
4035 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
4036 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
4039 UNBLOCK_INPUT;
4042 /* Deallocate all resources for the tool bar on frame F.
4043 Remove the tool bar. */
4045 void
4046 free_frame_tool_bar (f)
4047 FRAME_PTR f;
4049 struct x_output *x = f->output_data.x;
4051 if (x->toolbar_widget)
4053 BLOCK_INPUT;
4054 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4055 x->handlebox_widget);
4056 x->toolbar_widget = 0;
4057 x->handlebox_widget = 0;
4058 FRAME_TOOLBAR_HEIGHT (f) = 0;
4060 /* The height has changed, resize outer widget and set columns
4061 rows to what we had before removing the tool bar. */
4062 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
4064 SET_FRAME_GARBAGED (f);
4065 UNBLOCK_INPUT;
4071 /***********************************************************************
4072 Initializing
4073 ***********************************************************************/
4074 void
4075 xg_initialize ()
4077 GtkBindingSet *binding_set;
4079 #if HAVE_XFT
4080 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4081 we keep it permanently linked in. */
4082 XftInit (0);
4083 #endif
4084 xg_ignore_gtk_scrollbar = 0;
4085 xg_detached_menus = 0;
4086 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4087 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4089 id_to_widget.max_size = id_to_widget.used = 0;
4090 id_to_widget.widgets = 0;
4092 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4093 bindings. It doesn't seem to be any way to remove properties,
4094 so we set it to VoidSymbol which in X means "no key". */
4095 gtk_settings_set_string_property (gtk_settings_get_default (),
4096 "gtk-menu-bar-accel",
4097 "VoidSymbol",
4098 EMACS_CLASS);
4100 /* Make GTK text input widgets use Emacs style keybindings. This is
4101 Emacs after all. */
4102 gtk_settings_set_string_property (gtk_settings_get_default (),
4103 "gtk-key-theme-name",
4104 "Emacs",
4105 EMACS_CLASS);
4107 /* Make dialogs close on C-g. Since file dialog inherits from
4108 dialog, this works for them also. */
4109 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
4110 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4111 "close", 0);
4113 /* Make menus close on C-g. */
4114 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
4115 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4116 "cancel", 0);
4119 #endif /* USE_GTK */
4121 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4122 (do not change this comment) */