Copyedit.
[emacs.git] / src / gtkutil.c
blob2952e322200b489c151d5e8b16c780dad42aff73
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 /* Callback called when keyboard traversal (started by x-menu-bar-open) ends.
2042 WMENU is the menu for which traversal has been done. DATA points to the
2043 frame for WMENU. We must release grabs, some bad interaction between GTK
2044 and Emacs makes the menus keep the grabs. */
2046 static void
2047 menu_nav_ended (wmenu, data)
2048 GtkMenuShell *wmenu;
2049 gpointer data;
2051 FRAME_PTR f = (FRAME_PTR) data;
2053 if (FRAME_X_OUTPUT (f)->menubar_widget)
2055 GtkMenuShell *w = GTK_MENU_SHELL (FRAME_X_OUTPUT (f)->menubar_widget);
2056 Display *dpy = FRAME_X_DISPLAY (f);
2058 BLOCK_INPUT;
2059 gtk_menu_shell_deactivate (w);
2060 gtk_menu_shell_deselect (w);
2062 XUngrabKeyboard (dpy, CurrentTime);
2063 XUngrabPointer (dpy, CurrentTime);
2064 UNBLOCK_INPUT;
2069 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
2070 GCallback, GCallback, int, int, int,
2071 GtkWidget *, xg_menu_cb_data *, char *));
2073 /* Create a full menu tree specified by DATA.
2074 F is the frame the created menu belongs to.
2075 SELECT_CB is the callback to use when a menu item is selected.
2076 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2077 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2078 POP_UP_P is non-zero if we shall create a popup menu.
2079 MENU_BAR_P is non-zero if we shall create a menu bar.
2080 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2081 if MENU_BAR_P is non-zero.
2082 TOPMENU is the topmost GtkWidget that others shall be placed under.
2083 It may be NULL, in that case we create the appropriate widget
2084 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2085 CL_DATA is the callback data we shall use for this menu, or NULL
2086 if we haven't set the first callback yet.
2087 NAME is the name to give to the top level menu if this function
2088 creates it. May be NULL to not set any name.
2090 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2091 not NULL.
2093 This function calls itself to create submenus. */
2095 static GtkWidget *
2096 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
2097 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
2098 widget_value *data;
2099 FRAME_PTR f;
2100 GCallback select_cb;
2101 GCallback deactivate_cb;
2102 GCallback highlight_cb;
2103 int pop_up_p;
2104 int menu_bar_p;
2105 int add_tearoff_p;
2106 GtkWidget *topmenu;
2107 xg_menu_cb_data *cl_data;
2108 char *name;
2110 widget_value *item;
2111 GtkWidget *wmenu = topmenu;
2112 GSList *group = NULL;
2114 if (! topmenu)
2116 if (! menu_bar_p)
2118 wmenu = gtk_menu_new ();
2119 xg_set_screen (wmenu, f);
2120 /* Connect this to the menu instead of items so we get enter/leave for
2121 disabled items also. TODO: Still does not get enter/leave for
2122 disabled items in detached menus. */
2123 g_signal_connect (G_OBJECT (wmenu),
2124 "enter-notify-event",
2125 G_CALLBACK (menuitem_highlight_callback),
2126 NULL);
2127 g_signal_connect (G_OBJECT (wmenu),
2128 "leave-notify-event",
2129 G_CALLBACK (menuitem_highlight_callback),
2130 NULL);
2132 else wmenu = gtk_menu_bar_new ();
2134 /* Fix up grabs after keyboard traversal ends. */
2135 g_signal_connect (G_OBJECT (wmenu),
2136 "selection-done",
2137 G_CALLBACK (menu_nav_ended),
2140 /* Put cl_data on the top menu for easier access. */
2141 cl_data = make_cl_data (cl_data, f, highlight_cb);
2142 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2143 g_signal_connect (G_OBJECT (wmenu), "destroy",
2144 G_CALLBACK (menu_destroy_callback), cl_data);
2146 if (name)
2147 gtk_widget_set_name (wmenu, name);
2149 if (deactivate_cb)
2150 g_signal_connect (G_OBJECT (wmenu),
2151 "selection-done", deactivate_cb, 0);
2153 g_signal_connect (G_OBJECT (wmenu),
2154 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
2157 if (! menu_bar_p && add_tearoff_p)
2159 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2160 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2162 g_signal_connect (G_OBJECT (tearoff), "activate",
2163 G_CALLBACK (tearoff_activate), 0);
2166 for (item = data; item; item = item->next)
2168 GtkWidget *w;
2170 if (pop_up_p && !item->contents && !item->call_data
2171 && !xg_separator_p (item->name))
2173 char *utf8_label;
2174 /* A title for a popup. We do the same as GTK does when
2175 creating titles, but it does not look good. */
2176 group = NULL;
2177 utf8_label = get_utf8_string (item->name);
2179 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2180 w = gtk_menu_item_new_with_label (utf8_label);
2181 gtk_widget_set_sensitive (w, FALSE);
2182 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2184 else if (xg_separator_p (item->name))
2186 group = NULL;
2187 /* GTK only have one separator type. */
2188 w = gtk_separator_menu_item_new ();
2190 else
2192 w = xg_create_one_menuitem (item,
2194 item->contents ? 0 : select_cb,
2195 highlight_cb,
2196 cl_data,
2197 &group);
2199 /* Create a possibly empty submenu for menu bar items, since some
2200 themes don't highlight items correctly without it. */
2201 if (item->contents || menu_bar_p)
2203 GtkWidget *submenu = create_menus (item->contents,
2205 select_cb,
2206 deactivate_cb,
2207 highlight_cb,
2210 add_tearoff_p,
2212 cl_data,
2214 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2218 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2219 gtk_widget_set_name (w, MENU_ITEM_NAME);
2222 return wmenu;
2225 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2226 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2227 with some text and buttons.
2228 F is the frame the created item belongs to.
2229 NAME is the name to use for the top widget.
2230 VAL is a widget_value structure describing items to be created.
2231 SELECT_CB is the callback to use when a menu item is selected or
2232 a dialog button is pressed.
2233 DEACTIVATE_CB is the callback to use when an item is deactivated.
2234 For a menu, when a sub menu is not shown anymore, for a dialog it is
2235 called when the dialog is popped down.
2236 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2238 Returns the widget created. */
2240 GtkWidget *
2241 xg_create_widget (type, name, f, val,
2242 select_cb, deactivate_cb, highlight_cb)
2243 char *type;
2244 char *name;
2245 FRAME_PTR f;
2246 widget_value *val;
2247 GCallback select_cb;
2248 GCallback deactivate_cb;
2249 GCallback highlight_cb;
2251 GtkWidget *w = 0;
2252 int menu_bar_p = strcmp (type, "menubar") == 0;
2253 int pop_up_p = strcmp (type, "popup") == 0;
2255 if (strcmp (type, "dialog") == 0)
2257 w = create_dialog (val, select_cb, deactivate_cb);
2258 xg_set_screen (w, f);
2259 gtk_window_set_transient_for (GTK_WINDOW (w),
2260 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2261 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2262 gtk_widget_set_name (w, "emacs-dialog");
2263 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2265 else if (menu_bar_p || pop_up_p)
2267 w = create_menus (val->contents,
2269 select_cb,
2270 deactivate_cb,
2271 highlight_cb,
2272 pop_up_p,
2273 menu_bar_p,
2274 menu_bar_p,
2277 name);
2279 /* Set the cursor to an arrow for popup menus when they are mapped.
2280 This is done by default for menu bar menus. */
2281 if (pop_up_p)
2283 /* Must realize so the GdkWindow inside the widget is created. */
2284 gtk_widget_realize (w);
2285 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2288 else
2290 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2291 type);
2294 return w;
2297 /* Return the label for menu item WITEM. */
2299 static const char *
2300 xg_get_menu_item_label (witem)
2301 GtkMenuItem *witem;
2303 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2304 return gtk_label_get_label (wlabel);
2307 /* Return non-zero if the menu item WITEM has the text LABEL. */
2309 static int
2310 xg_item_label_same_p (witem, label)
2311 GtkMenuItem *witem;
2312 char *label;
2314 int is_same = 0;
2315 char *utf8_label = get_utf8_string (label);
2316 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2318 if (! old_label && ! utf8_label)
2319 is_same = 1;
2320 else if (old_label && utf8_label)
2321 is_same = strcmp (utf8_label, old_label) == 0;
2323 if (utf8_label && utf8_label != label) g_free (utf8_label);
2325 return is_same;
2328 /* Destroy widgets in LIST. */
2330 static void
2331 xg_destroy_widgets (list)
2332 GList *list;
2334 GList *iter;
2336 for (iter = list; iter; iter = g_list_next (iter))
2338 GtkWidget *w = GTK_WIDGET (iter->data);
2340 /* Destroying the widget will remove it from the container it is in. */
2341 gtk_widget_destroy (w);
2345 /* Update the top level names in MENUBAR (i.e. not submenus).
2346 F is the frame the menu bar belongs to.
2347 *LIST is a list with the current menu bar names (menu item widgets).
2348 ITER is the item within *LIST that shall be updated.
2349 POS is the numerical position, starting at 0, of ITER in *LIST.
2350 VAL describes what the menu bar shall look like after the update.
2351 SELECT_CB is the callback to use when a menu item is selected.
2352 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2353 CL_DATA points to the callback data to be used for this menu bar.
2355 This function calls itself to walk through the menu bar names. */
2357 static void
2358 xg_update_menubar (menubar, f, list, iter, pos, val,
2359 select_cb, highlight_cb, cl_data)
2360 GtkWidget *menubar;
2361 FRAME_PTR f;
2362 GList **list;
2363 GList *iter;
2364 int pos;
2365 widget_value *val;
2366 GCallback select_cb;
2367 GCallback highlight_cb;
2368 xg_menu_cb_data *cl_data;
2370 if (! iter && ! val)
2371 return;
2372 else if (iter && ! val)
2374 /* Item(s) have been removed. Remove all remaining items. */
2375 xg_destroy_widgets (iter);
2377 /* All updated. */
2378 val = 0;
2379 iter = 0;
2381 else if (! iter && val)
2383 /* Item(s) added. Add all new items in one call. */
2384 create_menus (val, f, select_cb, 0, highlight_cb,
2385 0, 1, 0, menubar, cl_data, 0);
2387 /* All updated. */
2388 val = 0;
2389 iter = 0;
2391 /* Below this neither iter or val is NULL */
2392 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2394 /* This item is still the same, check next item. */
2395 val = val->next;
2396 iter = g_list_next (iter);
2397 ++pos;
2399 else /* This item is changed. */
2401 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2402 GtkMenuItem *witem2 = 0;
2403 int val_in_menubar = 0;
2404 int iter_in_new_menubar = 0;
2405 GList *iter2;
2406 widget_value *cur;
2408 /* See if the changed entry (val) is present later in the menu bar */
2409 for (iter2 = iter;
2410 iter2 && ! val_in_menubar;
2411 iter2 = g_list_next (iter2))
2413 witem2 = GTK_MENU_ITEM (iter2->data);
2414 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2417 /* See if the current entry (iter) is present later in the
2418 specification for the new menu bar. */
2419 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2420 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2422 if (val_in_menubar && ! iter_in_new_menubar)
2424 int nr = pos;
2426 /* This corresponds to:
2427 Current: A B C
2428 New: A C
2429 Remove B. */
2431 gtk_widget_ref (GTK_WIDGET (witem));
2432 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2433 gtk_widget_destroy (GTK_WIDGET (witem));
2435 /* Must get new list since the old changed. */
2436 g_list_free (*list);
2437 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2438 while (nr-- > 0) iter = g_list_next (iter);
2440 else if (! val_in_menubar && ! iter_in_new_menubar)
2442 /* This corresponds to:
2443 Current: A B C
2444 New: A X C
2445 Rename B to X. This might seem to be a strange thing to do,
2446 since if there is a menu under B it will be totally wrong for X.
2447 But consider editing a C file. Then there is a C-mode menu
2448 (corresponds to B above).
2449 If then doing C-x C-f the minibuf menu (X above) replaces the
2450 C-mode menu. When returning from the minibuffer, we get
2451 back the C-mode menu. Thus we do:
2452 Rename B to X (C-mode to minibuf menu)
2453 Rename X to B (minibuf to C-mode menu).
2454 If the X menu hasn't been invoked, the menu under B
2455 is up to date when leaving the minibuffer. */
2456 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2457 char *utf8_label = get_utf8_string (val->name);
2458 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2460 gtk_label_set_text (wlabel, utf8_label);
2462 /* If this item has a submenu that has been detached, change
2463 the title in the WM decorations also. */
2464 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2465 /* Set the title of the detached window. */
2466 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2468 iter = g_list_next (iter);
2469 val = val->next;
2470 ++pos;
2472 else if (! val_in_menubar && iter_in_new_menubar)
2474 /* This corresponds to:
2475 Current: A B C
2476 New: A X B C
2477 Insert X. */
2479 int nr = pos;
2480 GList *group = 0;
2481 GtkWidget *w = xg_create_one_menuitem (val,
2483 select_cb,
2484 highlight_cb,
2485 cl_data,
2486 &group);
2488 /* Create a possibly empty submenu for menu bar items, since some
2489 themes don't highlight items correctly without it. */
2490 GtkWidget *submenu = create_menus (NULL, f,
2491 select_cb, NULL, highlight_cb,
2492 0, 0, 0, 0, cl_data, 0);
2493 gtk_widget_set_name (w, MENU_ITEM_NAME);
2494 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2495 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2497 g_list_free (*list);
2498 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2499 while (nr-- > 0) iter = g_list_next (iter);
2500 iter = g_list_next (iter);
2501 val = val->next;
2502 ++pos;
2504 else /* if (val_in_menubar && iter_in_new_menubar) */
2506 int nr = pos;
2507 /* This corresponds to:
2508 Current: A B C
2509 New: A C B
2510 Move C before B */
2512 gtk_widget_ref (GTK_WIDGET (witem2));
2513 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2514 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2515 GTK_WIDGET (witem2), pos);
2516 gtk_widget_unref (GTK_WIDGET (witem2));
2518 g_list_free (*list);
2519 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2520 while (nr-- > 0) iter = g_list_next (iter);
2521 if (iter) iter = g_list_next (iter);
2522 val = val->next;
2523 ++pos;
2527 /* Update the rest of the menu bar. */
2528 xg_update_menubar (menubar, f, list, iter, pos, val,
2529 select_cb, highlight_cb, cl_data);
2532 /* Update the menu item W so it corresponds to VAL.
2533 SELECT_CB is the callback to use when a menu item is selected.
2534 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2535 CL_DATA is the data to set in the widget for menu invokation. */
2537 static void
2538 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2539 widget_value *val;
2540 GtkWidget *w;
2541 GCallback select_cb;
2542 GCallback highlight_cb;
2543 xg_menu_cb_data *cl_data;
2545 GtkWidget *wchild;
2546 GtkLabel *wlbl = 0;
2547 GtkLabel *wkey = 0;
2548 char *utf8_label;
2549 char *utf8_key;
2550 const char *old_label = 0;
2551 const char *old_key = 0;
2552 xg_menu_item_cb_data *cb_data;
2554 wchild = gtk_bin_get_child (GTK_BIN (w));
2555 utf8_label = get_utf8_string (val->name);
2556 utf8_key = get_utf8_string (val->key);
2558 /* See if W is a menu item with a key. See make_menu_item above. */
2559 if (GTK_IS_HBOX (wchild))
2561 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2563 wlbl = GTK_LABEL (list->data);
2564 wkey = GTK_LABEL (list->next->data);
2565 g_list_free (list);
2567 if (! utf8_key)
2569 /* Remove the key and keep just the label. */
2570 gtk_widget_ref (GTK_WIDGET (wlbl));
2571 gtk_container_remove (GTK_CONTAINER (w), wchild);
2572 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2573 wkey = 0;
2577 else /* Just a label. */
2579 wlbl = GTK_LABEL (wchild);
2581 /* Check if there is now a key. */
2582 if (utf8_key)
2584 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2585 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2587 wlbl = GTK_LABEL (list->data);
2588 wkey = GTK_LABEL (list->next->data);
2589 g_list_free (list);
2591 gtk_container_remove (GTK_CONTAINER (w), wchild);
2592 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2597 if (wkey) old_key = gtk_label_get_label (wkey);
2598 if (wlbl) old_label = gtk_label_get_label (wlbl);
2600 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2601 gtk_label_set_text (wkey, utf8_key);
2603 if (! old_label || strcmp (utf8_label, old_label) != 0)
2604 gtk_label_set_text (wlbl, utf8_label);
2606 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2607 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2609 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2610 gtk_widget_set_sensitive (w, FALSE);
2611 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2612 gtk_widget_set_sensitive (w, TRUE);
2614 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2615 XG_ITEM_DATA);
2616 if (cb_data)
2618 cb_data->call_data = val->call_data;
2619 cb_data->help = val->help;
2620 cb_data->cl_data = cl_data;
2622 /* We assume the callback functions don't change. */
2623 if (val->call_data && ! val->contents)
2625 /* This item shall have a select callback. */
2626 if (! cb_data->select_id)
2627 cb_data->select_id
2628 = g_signal_connect (G_OBJECT (w), "activate",
2629 select_cb, cb_data);
2631 else if (cb_data->select_id)
2633 g_signal_handler_disconnect (w, cb_data->select_id);
2634 cb_data->select_id = 0;
2639 /* Update the toggle menu item W so it corresponds to VAL. */
2641 static void
2642 xg_update_toggle_item (val, w)
2643 widget_value *val;
2644 GtkWidget *w;
2646 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2649 /* Update the radio menu item W so it corresponds to VAL. */
2651 static void
2652 xg_update_radio_item (val, w)
2653 widget_value *val;
2654 GtkWidget *w;
2656 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2659 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2660 SUBMENU may be NULL, in that case a new menu is created.
2661 F is the frame the menu bar belongs to.
2662 VAL describes the contents of the menu bar.
2663 SELECT_CB is the callback to use when a menu item is selected.
2664 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2665 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2666 CL_DATA is the call back data to use for any newly created items.
2668 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2669 was NULL. */
2671 static GtkWidget *
2672 xg_update_submenu (submenu, f, val,
2673 select_cb, deactivate_cb, highlight_cb, cl_data)
2674 GtkWidget *submenu;
2675 FRAME_PTR f;
2676 widget_value *val;
2677 GCallback select_cb;
2678 GCallback deactivate_cb;
2679 GCallback highlight_cb;
2680 xg_menu_cb_data *cl_data;
2682 GtkWidget *newsub = submenu;
2683 GList *list = 0;
2684 GList *iter;
2685 widget_value *cur;
2686 int has_tearoff_p = 0;
2687 GList *first_radio = 0;
2689 if (submenu)
2690 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2692 for (cur = val, iter = list;
2693 cur && iter;
2694 iter = g_list_next (iter), cur = cur->next)
2696 GtkWidget *w = GTK_WIDGET (iter->data);
2698 /* Skip tearoff items, they have no counterpart in val. */
2699 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2701 has_tearoff_p = 1;
2702 iter = g_list_next (iter);
2703 if (iter) w = GTK_WIDGET (iter->data);
2704 else break;
2707 /* Remember first radio button in a group. If we get a mismatch in
2708 a radio group we must rebuild the whole group so that the connections
2709 in GTK becomes correct. */
2710 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2711 first_radio = iter;
2712 else if (cur->button_type != BUTTON_TYPE_RADIO
2713 && ! GTK_IS_RADIO_MENU_ITEM (w))
2714 first_radio = 0;
2716 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2718 if (! xg_separator_p (cur->name))
2719 break;
2721 else if (GTK_IS_CHECK_MENU_ITEM (w))
2723 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2724 break;
2725 xg_update_toggle_item (cur, w);
2726 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2728 else if (GTK_IS_RADIO_MENU_ITEM (w))
2730 if (cur->button_type != BUTTON_TYPE_RADIO)
2731 break;
2732 xg_update_radio_item (cur, w);
2733 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2735 else if (GTK_IS_MENU_ITEM (w))
2737 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2738 GtkWidget *sub;
2740 if (cur->button_type != BUTTON_TYPE_NONE ||
2741 xg_separator_p (cur->name))
2742 break;
2744 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2746 sub = gtk_menu_item_get_submenu (witem);
2747 if (sub && ! cur->contents)
2749 /* Not a submenu anymore. */
2750 gtk_widget_ref (sub);
2751 gtk_menu_item_remove_submenu (witem);
2752 gtk_widget_destroy (sub);
2754 else if (cur->contents)
2756 GtkWidget *nsub;
2758 nsub = xg_update_submenu (sub, f, cur->contents,
2759 select_cb, deactivate_cb,
2760 highlight_cb, cl_data);
2762 /* If this item just became a submenu, we must set it. */
2763 if (nsub != sub)
2764 gtk_menu_item_set_submenu (witem, nsub);
2767 else
2769 /* Structural difference. Remove everything from here and down
2770 in SUBMENU. */
2771 break;
2775 /* Remove widgets from first structual change. */
2776 if (iter)
2778 /* If we are adding new menu items below, we must remove from
2779 first radio button so that radio groups become correct. */
2780 if (cur && first_radio) xg_destroy_widgets (first_radio);
2781 else xg_destroy_widgets (iter);
2784 if (cur)
2786 /* More items added. Create them. */
2787 newsub = create_menus (cur,
2789 select_cb,
2790 deactivate_cb,
2791 highlight_cb,
2794 ! has_tearoff_p,
2795 submenu,
2796 cl_data,
2800 if (list) g_list_free (list);
2802 return newsub;
2805 /* Update the MENUBAR.
2806 F is the frame the menu bar belongs to.
2807 VAL describes the contents of the menu bar.
2808 If DEEP_P is non-zero, rebuild all but the top level menu names in
2809 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2810 SELECT_CB is the callback to use when a menu item is selected.
2811 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2812 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2814 void
2815 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2816 select_cb, deactivate_cb, highlight_cb)
2817 GtkWidget *menubar;
2818 FRAME_PTR f;
2819 widget_value *val;
2820 int deep_p;
2821 GCallback select_cb;
2822 GCallback deactivate_cb;
2823 GCallback highlight_cb;
2825 xg_menu_cb_data *cl_data;
2826 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2828 if (! list) return;
2830 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2831 XG_FRAME_DATA);
2833 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2834 select_cb, highlight_cb, cl_data);
2836 if (deep_p)
2838 widget_value *cur;
2840 /* Update all sub menus.
2841 We must keep the submenus (GTK menu item widgets) since the
2842 X Window in the XEvent that activates the menu are those widgets. */
2844 /* Update cl_data, menu_item things in F may have changed. */
2845 update_cl_data (cl_data, f, highlight_cb);
2847 for (cur = val->contents; cur; cur = cur->next)
2849 GList *iter;
2850 GtkWidget *sub = 0;
2851 GtkWidget *newsub;
2852 GtkMenuItem *witem;
2854 /* Find sub menu that corresponds to val and update it. */
2855 for (iter = list ; iter; iter = g_list_next (iter))
2857 witem = GTK_MENU_ITEM (iter->data);
2858 if (xg_item_label_same_p (witem, cur->name))
2860 sub = gtk_menu_item_get_submenu (witem);
2861 break;
2865 newsub = xg_update_submenu (sub,
2867 cur->contents,
2868 select_cb,
2869 deactivate_cb,
2870 highlight_cb,
2871 cl_data);
2872 /* sub may still be NULL. If we just updated non deep and added
2873 a new menu bar item, it has no sub menu yet. So we set the
2874 newly created sub menu under witem. */
2875 if (newsub != sub)
2877 xg_set_screen (newsub, f);
2878 gtk_menu_item_set_submenu (witem, newsub);
2883 g_list_free (list);
2884 gtk_widget_show_all (menubar);
2887 /* Recompute all the widgets of frame F, when the menu bar has been
2888 changed. Value is non-zero if widgets were updated. */
2891 xg_update_frame_menubar (f)
2892 FRAME_PTR f;
2894 struct x_output *x = f->output_data.x;
2895 GtkRequisition req;
2897 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2898 return 0;
2900 BLOCK_INPUT;
2902 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2903 FALSE, FALSE, 0);
2904 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2906 gtk_widget_show_all (x->menubar_widget);
2907 gtk_widget_size_request (x->menubar_widget, &req);
2909 FRAME_MENUBAR_HEIGHT (f) = req.height;
2911 /* The height has changed, resize outer widget and set columns
2912 rows to what we had before adding the menu bar. */
2913 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2915 SET_FRAME_GARBAGED (f);
2916 UNBLOCK_INPUT;
2918 return 1;
2921 /* Get rid of the menu bar of frame F, and free its storage.
2922 This is used when deleting a frame, and when turning off the menu bar. */
2924 void
2925 free_frame_menubar (f)
2926 FRAME_PTR f;
2928 struct x_output *x = f->output_data.x;
2930 if (x->menubar_widget)
2932 BLOCK_INPUT;
2934 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2935 /* The menubar and its children shall be deleted when removed from
2936 the container. */
2937 x->menubar_widget = 0;
2938 FRAME_MENUBAR_HEIGHT (f) = 0;
2940 /* The height has changed, resize outer widget and set columns
2941 rows to what we had before removing the menu bar. */
2942 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2944 SET_FRAME_GARBAGED (f);
2945 UNBLOCK_INPUT;
2951 /***********************************************************************
2952 Scroll bar functions
2953 ***********************************************************************/
2956 /* Setting scroll bar values invokes the callback. Use this variable
2957 to indicate that callback should do nothing. */
2959 int xg_ignore_gtk_scrollbar;
2961 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2962 32 bits. But we want to store pointers, and they may be larger
2963 than 32 bits. Keep a mapping from integer index to widget pointers
2964 to get around the 32 bit limitation. */
2966 static struct
2968 GtkWidget **widgets;
2969 int max_size;
2970 int used;
2971 } id_to_widget;
2973 /* Grow this much every time we need to allocate more */
2975 #define ID_TO_WIDGET_INCR 32
2977 /* Store the widget pointer W in id_to_widget and return the integer index. */
2979 static int
2980 xg_store_widget_in_map (w)
2981 GtkWidget *w;
2983 int i;
2985 if (id_to_widget.max_size == id_to_widget.used)
2987 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2989 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2990 sizeof (GtkWidget *)*new_size);
2992 for (i = id_to_widget.max_size; i < new_size; ++i)
2993 id_to_widget.widgets[i] = 0;
2994 id_to_widget.max_size = new_size;
2997 /* Just loop over the array and find a free place. After all,
2998 how many scroll bars are we creating? Should be a small number.
2999 The check above guarantees we will find a free place. */
3000 for (i = 0; i < id_to_widget.max_size; ++i)
3002 if (! id_to_widget.widgets[i])
3004 id_to_widget.widgets[i] = w;
3005 ++id_to_widget.used;
3007 return i;
3011 /* Should never end up here */
3012 abort ();
3015 /* Remove pointer at IDX from id_to_widget.
3016 Called when scroll bar is destroyed. */
3018 static void
3019 xg_remove_widget_from_map (idx)
3020 int idx;
3022 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3024 id_to_widget.widgets[idx] = 0;
3025 --id_to_widget.used;
3029 /* Get the widget pointer at IDX from id_to_widget. */
3031 static GtkWidget *
3032 xg_get_widget_from_map (idx)
3033 int idx;
3035 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3036 return id_to_widget.widgets[idx];
3038 return 0;
3041 /* Return the scrollbar id for X Window WID on display DPY.
3042 Return -1 if WID not in id_to_widget. */
3045 xg_get_scroll_id_for_window (dpy, wid)
3046 Display *dpy;
3047 Window wid;
3049 int idx;
3050 GtkWidget *w;
3052 w = xg_win_to_widget (dpy, wid);
3054 if (w)
3056 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3057 if (id_to_widget.widgets[idx] == w)
3058 return idx;
3061 return -1;
3064 /* Callback invoked when scroll bar WIDGET is destroyed.
3065 DATA is the index into id_to_widget for WIDGET.
3066 We free pointer to last scroll bar values here and remove the index. */
3068 static void
3069 xg_gtk_scroll_destroy (widget, data)
3070 GtkWidget *widget;
3071 gpointer data;
3073 gpointer p;
3074 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3076 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
3077 if (p) xfree (p);
3078 xg_remove_widget_from_map (id);
3081 /* Callback for button press/release events. Used to start timer so that
3082 the scroll bar repetition timer in GTK gets handeled.
3083 Also, sets bar->dragging to Qnil when dragging (button release) is done.
3084 WIDGET is the scroll bar widget the event is for (not used).
3085 EVENT contains the event.
3086 USER_DATA points to the struct scrollbar structure.
3088 Returns FALSE to tell GTK that it shall continue propagate the event
3089 to widgets. */
3091 static gboolean
3092 scroll_bar_button_cb (widget, event, user_data)
3093 GtkWidget *widget;
3094 GdkEventButton *event;
3095 gpointer user_data;
3097 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
3098 xg_start_timer ();
3099 else if (event->type == GDK_BUTTON_RELEASE)
3101 struct scroll_bar *bar = (struct scroll_bar *) user_data;
3102 if (xg_timer) xg_stop_timer ();
3103 bar->dragging = Qnil;
3106 return FALSE;
3109 /* Create a scroll bar widget for frame F. Store the scroll bar
3110 in BAR.
3111 SCROLL_CALLBACK is the callback to invoke when the value of the
3112 bar changes.
3113 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3114 to set resources for the widget. */
3116 void
3117 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
3118 FRAME_PTR f;
3119 struct scroll_bar *bar;
3120 GCallback scroll_callback;
3121 char *scroll_bar_name;
3123 GtkWidget *wscroll;
3124 GtkWidget *webox;
3125 GtkObject *vadj;
3126 int scroll_id;
3128 /* Page, step increment values are not so important here, they
3129 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3130 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3131 0.1, 0.1, 0.1);
3133 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3134 webox = gtk_event_box_new ();
3135 gtk_widget_set_name (wscroll, scroll_bar_name);
3136 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3138 scroll_id = xg_store_widget_in_map (wscroll);
3140 g_signal_connect (G_OBJECT (wscroll),
3141 "value-changed",
3142 scroll_callback,
3143 (gpointer) bar);
3144 /* The EMACS_INT cast avoids a warning. */
3145 g_signal_connect (G_OBJECT (wscroll),
3146 "destroy",
3147 G_CALLBACK (xg_gtk_scroll_destroy),
3148 (gpointer) (EMACS_INT) scroll_id);
3150 /* Connect to button press and button release to detect if any scroll bar
3151 has the pointer. */
3152 g_signal_connect (G_OBJECT (wscroll),
3153 "button-press-event",
3154 G_CALLBACK (scroll_bar_button_cb),
3155 (gpointer) bar);
3156 g_signal_connect (G_OBJECT (wscroll),
3157 "button-release-event",
3158 G_CALLBACK (scroll_bar_button_cb),
3159 (gpointer) bar);
3161 /* The scroll bar widget does not draw on a window of its own. Instead
3162 it draws on the parent window, in this case the edit widget. So
3163 whenever the edit widget is cleared, the scroll bar needs to redraw
3164 also, which causes flicker. Put an event box between the edit widget
3165 and the scroll bar, so the scroll bar instead draws itself on the
3166 event box window. */
3167 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3168 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3171 /* Set the cursor to an arrow. */
3172 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3174 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
3177 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3179 void
3180 xg_show_scroll_bar (scrollbar_id)
3181 int scrollbar_id;
3183 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3184 if (w)
3185 gtk_widget_show_all (gtk_widget_get_parent (w));
3188 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3190 void
3191 xg_remove_scroll_bar (f, scrollbar_id)
3192 FRAME_PTR f;
3193 int scrollbar_id;
3195 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3196 if (w)
3198 GtkWidget *wparent = gtk_widget_get_parent (w);
3199 gtk_widget_destroy (w);
3200 gtk_widget_destroy (wparent);
3201 SET_FRAME_GARBAGED (f);
3205 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3206 in frame F.
3207 TOP/LEFT are the new pixel positions where the bar shall appear.
3208 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3210 void
3211 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3212 FRAME_PTR f;
3213 int scrollbar_id;
3214 int top;
3215 int left;
3216 int width;
3217 int height;
3220 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3222 if (wscroll)
3224 GtkWidget *wfixed = f->output_data.x->edit_widget;
3225 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3227 /* Move and resize to new values. */
3228 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3229 gtk_widget_set_size_request (wscroll, width, height);
3230 gtk_widget_queue_draw (wparent);
3231 gdk_window_process_all_updates ();
3232 /* GTK does not redraw until the main loop is entered again, but
3233 if there are no X events pending we will not enter it. So we sync
3234 here to get some events. */
3235 x_sync (f);
3236 SET_FRAME_GARBAGED (f);
3237 cancel_mouse_face (f);
3241 /* Set the thumb size and position of scroll bar BAR. We are currently
3242 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3244 void
3245 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3246 struct scroll_bar *bar;
3247 int portion, position, whole;
3249 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
3251 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3253 if (wscroll && NILP (bar->dragging))
3255 GtkAdjustment *adj;
3256 gdouble shown;
3257 gdouble top;
3258 int size, value;
3259 int new_step;
3260 int changed = 0;
3262 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3264 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3265 rather than the real portion value. This makes the thumb less likely
3266 to resize and that looks better. */
3267 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3268 /* When the thumb is at the bottom, position == whole.
3269 So we need to increase `whole' to make space for the thumb. */
3270 whole += portion;
3272 if (whole <= 0)
3273 top = 0, shown = 1;
3274 else
3276 top = (gdouble) position / whole;
3277 shown = (gdouble) portion / whole;
3280 size = shown * XG_SB_RANGE;
3281 size = min (size, XG_SB_RANGE);
3282 size = max (size, 1);
3284 value = top * XG_SB_RANGE;
3285 value = min (value, XG_SB_MAX - size);
3286 value = max (value, XG_SB_MIN);
3288 /* Assume all lines are of equal size. */
3289 new_step = size / max (1, FRAME_LINES (f));
3291 if ((int) adj->page_size != size
3292 || (int) adj->step_increment != new_step)
3294 adj->page_size = size;
3295 adj->step_increment = new_step;
3296 /* Assume a page increment is about 95% of the page size */
3297 adj->page_increment = (int) (0.95*adj->page_size);
3298 changed = 1;
3301 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3303 GtkWidget *wfixed = f->output_data.x->edit_widget;
3305 BLOCK_INPUT;
3307 /* gtk_range_set_value invokes the callback. Set
3308 ignore_gtk_scrollbar to make the callback do nothing */
3309 xg_ignore_gtk_scrollbar = 1;
3311 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3312 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3313 else if (changed)
3314 gtk_adjustment_changed (adj);
3316 xg_ignore_gtk_scrollbar = 0;
3318 UNBLOCK_INPUT;
3324 /***********************************************************************
3325 Tool bar functions
3326 ***********************************************************************/
3327 /* The key for the data we put in the GtkImage widgets. The data is
3328 the image used by Emacs. We use this to see if we need to update
3329 the GtkImage with a new image. */
3330 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3332 /* The key for storing the latest modifiers so the activate callback can
3333 get them. */
3334 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3336 /* The key for storing the button widget in its proxy menu item. */
3337 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3339 /* The key for the data we put in the GtkImage widgets. The data is
3340 the stock name used by Emacs. We use this to see if we need to update
3341 the GtkImage with a new image. */
3342 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3344 /* As above, but this is used for named theme widgets, as opposed to
3345 stock items. */
3346 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3348 /* Callback function invoked when a tool bar item is pressed.
3349 W is the button widget in the tool bar that got pressed,
3350 CLIENT_DATA is an integer that is the index of the button in the
3351 tool bar. 0 is the first button. */
3353 static gboolean
3354 xg_tool_bar_button_cb (widget, event, user_data)
3355 GtkWidget *widget;
3356 GdkEventButton *event;
3357 gpointer user_data;
3359 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3360 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3361 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3362 return FALSE;
3366 /* Callback function invoked when a tool bar item is pressed.
3367 W is the button widget in the tool bar that got pressed,
3368 CLIENT_DATA is an integer that is the index of the button in the
3369 tool bar. 0 is the first button. */
3371 static void
3372 xg_tool_bar_callback (w, client_data)
3373 GtkWidget *w;
3374 gpointer client_data;
3376 /* The EMACS_INT cast avoids a warning. */
3377 int idx = (int) (EMACS_INT) client_data;
3378 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3379 XG_TOOL_BAR_LAST_MODIFIER);
3381 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3382 Lisp_Object key, frame;
3383 struct input_event event;
3384 EVENT_INIT (event);
3386 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3387 return;
3389 idx *= TOOL_BAR_ITEM_NSLOTS;
3391 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3392 XSETFRAME (frame, f);
3394 /* We generate two events here. The first one is to set the prefix
3395 to `(tool_bar)', see keyboard.c. */
3396 event.kind = TOOL_BAR_EVENT;
3397 event.frame_or_window = frame;
3398 event.arg = frame;
3399 kbd_buffer_store_event (&event);
3401 event.kind = TOOL_BAR_EVENT;
3402 event.frame_or_window = frame;
3403 event.arg = key;
3404 /* Convert between the modifier bits GDK uses and the modifier bits
3405 Emacs uses. This assumes GDK an X masks are the same, which they are when
3406 this is written. */
3407 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3408 kbd_buffer_store_event (&event);
3411 /* Callback function invoked when a tool bar item is pressed in a detached
3412 tool bar or the overflow drop down menu.
3413 We just call xg_tool_bar_callback.
3414 W is the menu item widget that got pressed,
3415 CLIENT_DATA is an integer that is the index of the button in the
3416 tool bar. 0 is the first button. */
3418 static void
3419 xg_tool_bar_proxy_callback (w, client_data)
3420 GtkWidget *w;
3421 gpointer client_data;
3423 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3424 XG_TOOL_BAR_PROXY_BUTTON));
3425 xg_tool_bar_callback (wbutton, client_data);
3428 /* This callback is called when a tool item should create a proxy item,
3429 such as for the overflow menu. Also called when the tool bar is detached.
3430 If we don't create a proxy menu item, the detached tool bar will be
3431 blank. */
3433 static gboolean
3434 xg_tool_bar_menu_proxy (toolitem, user_data)
3435 GtkToolItem *toolitem;
3436 gpointer user_data;
3438 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3439 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3440 GtkWidget *wmenuitem = gtk_image_menu_item_new ();
3441 GtkWidget *wmenuimage;
3443 if (gtk_button_get_use_stock (wbutton))
3444 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3445 GTK_ICON_SIZE_MENU);
3446 else
3448 GtkImage *wimage = GTK_IMAGE (gtk_bin_get_child (GTK_BIN (wbutton)));
3449 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3450 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3451 if (store_type == GTK_IMAGE_STOCK)
3453 gchar *stock_id;
3454 gtk_image_get_stock (wimage, &stock_id, NULL);
3455 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3457 else if (store_type == GTK_IMAGE_ICON_SET)
3459 GtkIconSet *icon_set;
3460 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3461 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3462 GTK_ICON_SIZE_MENU);
3464 else if (store_type == GTK_IMAGE_PIXBUF)
3466 gint width, height;
3468 if (settings &&
3469 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3470 &width, &height))
3472 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3474 src_pixbuf = gtk_image_get_pixbuf (wimage);
3475 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3476 GDK_INTERP_BILINEAR);
3478 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3480 else
3482 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3483 abort ();
3486 else if (store_type == GTK_IMAGE_ICON_NAME)
3488 const gchar *icon_name;
3489 GtkIconSize icon_size;
3491 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3492 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3493 GTK_ICON_SIZE_MENU);
3495 else
3497 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3498 abort ();
3501 if (wmenuimage)
3502 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3504 g_signal_connect (G_OBJECT (wmenuitem),
3505 "activate",
3506 GTK_SIGNAL_FUNC (xg_tool_bar_proxy_callback),
3507 user_data);
3509 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3510 (gpointer) wbutton);
3511 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3513 return TRUE;
3516 /* This callback is called when a tool bar is detached. We must set
3517 the height of the tool bar to zero when this happens so frame sizes
3518 are correctly calculated.
3519 WBOX is the handle box widget that enables detach/attach of the tool bar.
3520 W is the tool bar widget.
3521 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3523 static void
3524 xg_tool_bar_detach_callback (wbox, w, client_data)
3525 GtkHandleBox *wbox;
3526 GtkWidget *w;
3527 gpointer client_data;
3529 FRAME_PTR f = (FRAME_PTR) client_data;
3530 extern int x_gtk_whole_detached_tool_bar;
3532 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3533 NULL);
3535 if (f)
3537 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3539 /* When detaching a tool bar, not everything dissapear. There are
3540 a few pixels left that are used to drop the tool bar back into
3541 place. */
3542 FRAME_TOOLBAR_HEIGHT (f) = 2;
3544 /* The height has changed, resize outer widget and set columns
3545 rows to what we had before detaching the tool bar. */
3546 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3550 /* This callback is called when a tool bar is reattached. We must set
3551 the height of the tool bar when this happens so frame sizes
3552 are correctly calculated.
3553 WBOX is the handle box widget that enables detach/attach of the tool bar.
3554 W is the tool bar widget.
3555 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3557 static void
3558 xg_tool_bar_attach_callback (wbox, w, client_data)
3559 GtkHandleBox *wbox;
3560 GtkWidget *w;
3561 gpointer client_data;
3563 FRAME_PTR f = (FRAME_PTR) client_data;
3564 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3566 if (f)
3568 GtkRequisition req;
3570 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3572 gtk_widget_size_request (w, &req);
3573 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3575 /* The height has changed, resize outer widget and set columns
3576 rows to what we had before attaching the tool bar. */
3577 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3581 /* This callback is called when the mouse enters or leaves a tool bar item.
3582 It is used for displaying and hiding the help text.
3583 W is the tool bar item, a button.
3584 EVENT is either an enter event or leave event.
3585 CLIENT_DATA is an integer that is the index of the button in the
3586 tool bar. 0 is the first button.
3588 Returns FALSE to tell GTK to keep processing this event. */
3590 static gboolean
3591 xg_tool_bar_help_callback (w, event, client_data)
3592 GtkWidget *w;
3593 GdkEventCrossing *event;
3594 gpointer client_data;
3596 /* The EMACS_INT cast avoids a warning. */
3597 int idx = (int) (EMACS_INT) client_data;
3598 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3599 Lisp_Object help, frame;
3601 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3602 return FALSE;
3604 if (event->type == GDK_ENTER_NOTIFY)
3606 idx *= TOOL_BAR_ITEM_NSLOTS;
3607 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3609 if (NILP (help))
3610 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3612 else
3613 help = Qnil;
3615 XSETFRAME (frame, f);
3616 kbd_buffer_store_help_event (frame, help);
3618 return FALSE;
3622 /* This callback is called when a tool bar item shall be redrawn.
3623 It modifies the expose event so that the GtkImage widget redraws the
3624 whole image. This to overcome a bug that makes GtkImage draw the image
3625 in the wrong place when it tries to redraw just a part of the image.
3626 W is the GtkImage to be redrawn.
3627 EVENT is the expose event for W.
3628 CLIENT_DATA is unused.
3630 Returns FALSE to tell GTK to keep processing this event. */
3632 static gboolean
3633 xg_tool_bar_item_expose_callback (w, event, client_data)
3634 GtkWidget *w;
3635 GdkEventExpose *event;
3636 gpointer client_data;
3638 gint width, height;
3640 gdk_drawable_get_size (event->window, &width, &height);
3642 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3643 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3645 event->area.x = max (0, event->area.x);
3646 event->area.y = max (0, event->area.y);
3648 event->area.width = max (width, event->area.width);
3649 event->area.height = max (height, event->area.height);
3651 return FALSE;
3654 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3657 /* Create a tool bar for frame F. */
3659 static void
3660 xg_create_tool_bar (f)
3661 FRAME_PTR f;
3663 struct x_output *x = f->output_data.x;
3664 GtkRequisition req;
3665 int vbox_pos = x->menubar_widget ? 1 : 0;
3667 x->toolbar_widget = gtk_toolbar_new ();
3668 x->handlebox_widget = gtk_handle_box_new ();
3669 x->toolbar_detached = 0;
3671 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3672 x->toolbar_widget);
3674 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3675 FALSE, FALSE, 0);
3677 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3678 vbox_pos);
3680 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3682 /* We only have icons, so override any user setting. We could
3683 use the caption property of the toolbar item (see update_frame_tool_bar
3684 below), but some of those strings are long, making the toolbar so
3685 long it does not fit on the screen. The GtkToolbar widget makes every
3686 item equal size, so the longest caption determine the size of every
3687 tool bar item. I think the creators of the GtkToolbar widget
3688 counted on 4 or 5 character long strings. */
3689 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3690 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3691 GTK_ORIENTATION_HORIZONTAL);
3693 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3694 G_CALLBACK (xg_tool_bar_detach_callback), f);
3695 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3696 G_CALLBACK (xg_tool_bar_attach_callback), f);
3698 gtk_widget_show_all (x->handlebox_widget);
3700 gtk_widget_size_request (x->toolbar_widget, &req);
3701 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3703 /* The height has changed, resize outer widget and set columns
3704 rows to what we had before adding the tool bar. */
3705 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3707 SET_FRAME_GARBAGED (f);
3710 /* Find the right-to-left image named by RTL in the tool bar images for F.
3711 Returns IMAGE if RTL is not found. */
3713 static Lisp_Object
3714 find_rtl_image (f, image, rtl)
3715 FRAME_PTR f;
3716 Lisp_Object image;
3717 Lisp_Object rtl;
3719 int i;
3720 Lisp_Object file, rtl_name;
3721 struct gcpro gcpro1, gcpro2;
3722 GCPRO2 (file, rtl_name);
3724 rtl_name = Ffile_name_nondirectory (rtl);
3726 for (i = 0; i < f->n_tool_bar_items; ++i)
3728 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3729 if (!NILP (file = file_for_image (rtl_image)))
3731 file = call1 (intern ("file-name-sans-extension"),
3732 Ffile_name_nondirectory (file));
3733 if (EQ (Fequal (file, rtl_name), Qt))
3735 image = rtl_image;
3736 break;
3741 return image;
3744 /* Update the tool bar for frame F. Add new buttons and remove old. */
3746 void
3747 update_frame_tool_bar (f)
3748 FRAME_PTR f;
3750 int i;
3751 GtkRequisition old_req, new_req;
3752 struct x_output *x = f->output_data.x;
3753 int hmargin = 0, vmargin = 0;
3754 GtkToolbar *wtoolbar;
3755 GtkToolItem *ti;
3756 GtkTextDirection dir;
3758 if (! FRAME_GTK_WIDGET (f))
3759 return;
3761 BLOCK_INPUT;
3763 if (INTEGERP (Vtool_bar_button_margin)
3764 && XINT (Vtool_bar_button_margin) > 0)
3766 hmargin = XFASTINT (Vtool_bar_button_margin);
3767 vmargin = XFASTINT (Vtool_bar_button_margin);
3769 else if (CONSP (Vtool_bar_button_margin))
3771 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3772 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3773 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3775 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3776 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3777 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3780 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3781 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3782 i.e. zero. This means that margins less than
3783 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3784 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3785 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3787 if (! x->toolbar_widget)
3788 xg_create_tool_bar (f);
3790 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
3791 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &old_req);
3792 dir = gtk_widget_get_direction (x->toolbar_widget);
3794 for (i = 0; i < f->n_tool_bar_items; ++i)
3797 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3798 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3799 int idx;
3800 int img_id;
3801 int icon_size = 0;
3802 struct image *img = NULL;
3803 Lisp_Object image;
3804 Lisp_Object stock;
3805 GtkStockItem stock_item;
3806 char *stock_name = NULL;
3807 char *icon_name = NULL;
3808 Lisp_Object rtl;
3809 GtkWidget *wbutton = NULL;
3810 GtkWidget *weventbox;
3811 Lisp_Object func = intern ("x-gtk-map-stock");
3812 Lisp_Object specified_file;
3814 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
3816 if (ti)
3818 weventbox = gtk_bin_get_child (GTK_BIN (ti));
3819 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3822 image = PROP (TOOL_BAR_ITEM_IMAGES);
3824 /* Ignore invalid image specifications. */
3825 if (!valid_image_p (image))
3827 if (wbutton) gtk_widget_hide (wbutton);
3828 continue;
3831 specified_file = file_for_image (image);
3832 if (!NILP (specified_file) && EQ (Qt, Ffboundp (func)))
3833 stock = call1 (func, specified_file);
3835 if (! NILP (stock) && STRINGP (stock))
3837 stock_name = SSDATA (stock);
3838 if (stock_name[0] == 'n' && stock_name[1] == ':')
3840 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
3841 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
3843 icon_name = stock_name + 2;
3844 stock_name = NULL;
3845 stock = Qnil;
3847 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
3848 icon_name = NULL;
3849 else
3850 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3852 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
3853 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3854 else
3856 stock = Qnil;
3857 stock_name = NULL;
3861 if (stock_name == NULL && icon_name == NULL)
3863 /* No stock image, or stock item not known. Try regular image. */
3865 /* If image is a vector, choose the image according to the
3866 button state. */
3867 if (dir == GTK_TEXT_DIR_RTL
3868 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
3869 && STRINGP (rtl))
3871 image = find_rtl_image (f, image, rtl);
3874 if (VECTORP (image))
3876 if (enabled_p)
3877 idx = (selected_p
3878 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3879 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3880 else
3881 idx = (selected_p
3882 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3883 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3885 xassert (ASIZE (image) >= idx);
3886 image = AREF (image, idx);
3888 else
3889 idx = -1;
3891 img_id = lookup_image (f, image);
3892 img = IMAGE_FROM_ID (f, img_id);
3893 prepare_image_for_display (f, img);
3895 if (img->load_failed_p || img->pixmap == None)
3897 if (ti)
3898 gtk_widget_hide_all (GTK_WIDGET (ti));
3899 else
3901 /* Insert an empty (non-image) button */
3902 weventbox = gtk_event_box_new ();
3903 wbutton = gtk_button_new ();
3904 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3905 gtk_button_set_relief (GTK_BUTTON (wbutton),
3906 GTK_RELIEF_NONE);
3907 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3908 ti = gtk_tool_item_new ();
3909 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3910 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3912 continue;
3916 if (ti == NULL)
3918 GtkWidget *w;
3919 if (stock_name)
3921 w = gtk_image_new_from_stock (stock_name, icon_size);
3922 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
3923 (gpointer) xstrdup (stock_name),
3924 (GDestroyNotify) xfree);
3926 else if (icon_name)
3928 w = gtk_image_new_from_icon_name (icon_name, icon_size);
3929 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
3930 (gpointer) xstrdup (icon_name),
3931 (GDestroyNotify) xfree);
3933 else
3935 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3936 /* Save the image so we can see if an update is needed when
3937 this function is called again. */
3938 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3939 (gpointer)img->pixmap);
3942 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3943 wbutton = gtk_button_new ();
3944 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3945 gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
3946 gtk_container_add (GTK_CONTAINER (wbutton), w);
3947 weventbox = gtk_event_box_new ();
3948 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3949 ti = gtk_tool_item_new ();
3950 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3951 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3954 /* The EMACS_INT cast avoids a warning. */
3955 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
3956 GTK_SIGNAL_FUNC (xg_tool_bar_menu_proxy),
3957 (gpointer) (EMACS_INT) i);
3959 g_signal_connect (G_OBJECT (wbutton), "clicked",
3960 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3961 (gpointer) (EMACS_INT) i);
3963 gtk_widget_show_all (GTK_WIDGET (ti));
3966 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
3968 /* Catch expose events to overcome an annoying redraw bug, see
3969 comment for xg_tool_bar_item_expose_callback. */
3970 g_signal_connect (G_OBJECT (ti),
3971 "expose-event",
3972 G_CALLBACK (xg_tool_bar_item_expose_callback),
3975 gtk_widget_set_sensitive (wbutton, enabled_p);
3976 gtk_tool_item_set_homogeneous (ti, FALSE);
3978 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
3979 no distinction based on modifiers in the activate callback,
3980 so we have to do it ourselves. */
3981 g_signal_connect (wbutton, "button-release-event",
3982 GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
3983 NULL);
3985 g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
3987 /* Use enter/leave notify to show help. We use the events
3988 rather than the GtkButton specific signals "enter" and
3989 "leave", so we can have only one callback. The event
3990 will tell us what kind of event it is. */
3991 /* The EMACS_INT cast avoids a warning. */
3992 g_signal_connect (G_OBJECT (weventbox),
3993 "enter-notify-event",
3994 G_CALLBACK (xg_tool_bar_help_callback),
3995 (gpointer) (EMACS_INT) i);
3996 g_signal_connect (G_OBJECT (weventbox),
3997 "leave-notify-event",
3998 G_CALLBACK (xg_tool_bar_help_callback),
3999 (gpointer) (EMACS_INT) i);
4001 else
4003 GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
4004 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4005 XG_TOOL_BAR_IMAGE_DATA);
4006 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4007 XG_TOOL_BAR_STOCK_NAME);
4008 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4009 XG_TOOL_BAR_ICON_NAME);
4010 if (stock_name &&
4011 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4013 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4014 stock_name, icon_size);
4015 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4016 (gpointer) xstrdup (stock_name),
4017 (GDestroyNotify) xfree);
4018 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4019 NULL);
4020 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4022 else if (icon_name &&
4023 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4025 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4026 icon_name, icon_size);
4027 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4028 (gpointer) xstrdup (icon_name),
4029 (GDestroyNotify) xfree);
4030 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4031 NULL);
4032 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4033 NULL);
4035 else if (img && old_img != img->pixmap)
4037 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
4038 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4039 (gpointer)img->pixmap);
4041 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4042 NULL);
4043 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4046 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4048 gtk_widget_set_sensitive (wbutton, enabled_p);
4049 gtk_widget_show_all (GTK_WIDGET (ti));
4052 #undef PROP
4055 /* Remove buttons not longer needed. We just hide them so they
4056 can be reused later on. */
4059 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
4060 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4061 } while (ti != NULL);
4063 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &new_req);
4064 if (old_req.height != new_req.height
4065 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
4067 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
4068 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
4071 UNBLOCK_INPUT;
4074 /* Deallocate all resources for the tool bar on frame F.
4075 Remove the tool bar. */
4077 void
4078 free_frame_tool_bar (f)
4079 FRAME_PTR f;
4081 struct x_output *x = f->output_data.x;
4083 if (x->toolbar_widget)
4085 BLOCK_INPUT;
4086 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4087 x->handlebox_widget);
4088 x->toolbar_widget = 0;
4089 x->handlebox_widget = 0;
4090 FRAME_TOOLBAR_HEIGHT (f) = 0;
4092 /* The height has changed, resize outer widget and set columns
4093 rows to what we had before removing the tool bar. */
4094 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
4096 SET_FRAME_GARBAGED (f);
4097 UNBLOCK_INPUT;
4103 /***********************************************************************
4104 Initializing
4105 ***********************************************************************/
4106 void
4107 xg_initialize ()
4109 GtkBindingSet *binding_set;
4111 #if HAVE_XFT
4112 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4113 we keep it permanently linked in. */
4114 XftInit (0);
4115 #endif
4116 xg_ignore_gtk_scrollbar = 0;
4117 xg_detached_menus = 0;
4118 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4119 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4121 id_to_widget.max_size = id_to_widget.used = 0;
4122 id_to_widget.widgets = 0;
4124 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4125 bindings. It doesn't seem to be any way to remove properties,
4126 so we set it to VoidSymbol which in X means "no key". */
4127 gtk_settings_set_string_property (gtk_settings_get_default (),
4128 "gtk-menu-bar-accel",
4129 "VoidSymbol",
4130 EMACS_CLASS);
4132 /* Make GTK text input widgets use Emacs style keybindings. This is
4133 Emacs after all. */
4134 gtk_settings_set_string_property (gtk_settings_get_default (),
4135 "gtk-key-theme-name",
4136 "Emacs",
4137 EMACS_CLASS);
4139 /* Make dialogs close on C-g. Since file dialog inherits from
4140 dialog, this works for them also. */
4141 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
4142 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4143 "close", 0);
4145 /* Make menus close on C-g. */
4146 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
4147 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4148 "cancel", 0);
4151 #endif /* USE_GTK */
4153 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4154 (do not change this comment) */