* savehist.el (savehist-autosave-interval): Allow setting to nil
[emacs.git] / src / gtkutil.c
blobb1e238af55c7863e1a4701df298942e549850df4
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #ifdef USE_GTK
23 #include <string.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <setjmp.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
31 #include "window.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
39 #ifdef HAVE_XFT
40 #include <X11/Xft/Xft.h>
41 #endif
43 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
44 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
46 /* Avoid "differ in sign" warnings */
47 #define SSDATA(x) ((char *) SDATA (x))
50 /***********************************************************************
51 Display handling functions
52 ***********************************************************************/
54 #ifdef HAVE_GTK_MULTIDISPLAY
56 /* Keep track of the default display, or NULL if there is none. Emacs
57 may close all its displays. */
59 static GdkDisplay *gdpy_def;
61 /* Return the GdkDisplay that corresponds to the X display DPY. */
63 static GdkDisplay *
64 xg_get_gdk_display (dpy)
65 Display *dpy;
67 return gdk_x11_lookup_xdisplay (dpy);
70 /* When the GTK widget W is to be created on a display for F that
71 is not the default display, set the display for W.
72 W can be a GtkMenu or a GtkWindow widget. */
74 static void
75 xg_set_screen (w, f)
76 GtkWidget *w;
77 FRAME_PTR f;
79 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
81 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
82 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
84 if (GTK_IS_MENU (w))
85 gtk_menu_set_screen (GTK_MENU (w), gscreen);
86 else
87 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
92 #else /* not HAVE_GTK_MULTIDISPLAY */
94 /* Make some defines so we can use the GTK 2.2 functions when
95 compiling with GTK 2.0. */
97 #define xg_set_screen(w, f)
98 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
99 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
100 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
101 #define gdk_x11_lookup_xdisplay(dpy) 0
102 #define GdkDisplay void
104 #endif /* not HAVE_GTK_MULTIDISPLAY */
106 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
107 *DPY is set to NULL if the display can't be opened.
109 Returns non-zero if display could be opened, zero if display could not
110 be opened, and less than zero if the GTK version doesn't support
111 multipe displays. */
114 xg_display_open (display_name, dpy)
115 char *display_name;
116 Display **dpy;
118 #ifdef HAVE_GTK_MULTIDISPLAY
119 GdkDisplay *gdpy;
121 gdpy = gdk_display_open (display_name);
122 if (!gdpy_def && gdpy)
124 gdpy_def = gdpy;
125 gdk_display_manager_set_default_display (gdk_display_manager_get (),
126 gdpy);
129 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
130 return gdpy != NULL;
132 #else /* not HAVE_GTK_MULTIDISPLAY */
134 return -1;
135 #endif /* not HAVE_GTK_MULTIDISPLAY */
139 /* Close display DPY. */
141 void
142 xg_display_close (Display *dpy)
144 #ifdef HAVE_GTK_MULTIDISPLAY
145 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
147 /* If this is the default display, try to change it before closing.
148 If there is no other display to use, gdpy_def is set to NULL, and
149 the next call to xg_display_open resets the default display. */
150 if (gdk_display_get_default () == gdpy)
152 struct x_display_info *dpyinfo;
153 GdkDisplay *gdpy_new = NULL;
155 /* Find another display. */
156 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
157 if (dpyinfo->display != dpy)
159 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
160 gdk_display_manager_set_default_display (gdk_display_manager_get (),
161 gdpy_new);
162 break;
164 gdpy_def = gdpy_new;
167 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
168 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
169 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
170 can continue running, but there will be memory leaks. */
171 g_object_run_dispose (G_OBJECT (gdpy));
172 #else
173 /* This seems to be fixed in GTK 2.10. */
174 gdk_display_close (gdpy);
175 #endif
176 #endif /* HAVE_GTK_MULTIDISPLAY */
180 /***********************************************************************
181 Utility functions
182 ***********************************************************************/
183 /* The next two variables and functions are taken from lwlib. */
184 static widget_value *widget_value_free_list;
185 static int malloc_cpt;
187 /* Allocate a widget_value structure, either by taking one from the
188 widget_value_free_list or by malloc:ing a new one.
190 Return a pointer to the allocated structure. */
192 widget_value *
193 malloc_widget_value ()
195 widget_value *wv;
196 if (widget_value_free_list)
198 wv = widget_value_free_list;
199 widget_value_free_list = wv->free_list;
200 wv->free_list = 0;
202 else
204 wv = (widget_value *) xmalloc (sizeof (widget_value));
205 malloc_cpt++;
207 memset (wv, 0, sizeof (widget_value));
208 return wv;
211 /* This is analogous to free. It frees only what was allocated
212 by malloc_widget_value, and no substructures. */
214 void
215 free_widget_value (wv)
216 widget_value *wv;
218 if (wv->free_list)
219 abort ();
221 if (malloc_cpt > 25)
223 /* When the number of already allocated cells is too big,
224 We free it. */
225 xfree (wv);
226 malloc_cpt--;
228 else
230 wv->free_list = widget_value_free_list;
231 widget_value_free_list = wv;
236 /* Create and return the cursor to be used for popup menus and
237 scroll bars on display DPY. */
239 GdkCursor *
240 xg_create_default_cursor (dpy)
241 Display *dpy;
243 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
244 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
247 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
249 static GdkPixbuf *
250 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
251 GdkPixmap *gpix;
252 GdkPixmap *gmask;
253 GdkColormap *cmap;
255 int x, y, width, height, rowstride, mask_rowstride;
256 GdkPixbuf *icon_buf, *tmp_buf;
257 guchar *pixels;
258 guchar *mask_pixels;
260 gdk_drawable_get_size (gpix, &width, &height);
261 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
262 0, 0, 0, 0, width, height);
263 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
264 g_object_unref (G_OBJECT (tmp_buf));
266 if (gmask)
268 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
269 gmask,
270 NULL,
271 0, 0, 0, 0,
272 width, height);
273 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
274 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
275 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
276 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
277 int y;
279 for (y = 0; y < height; ++y)
281 guchar *iconptr, *maskptr;
282 int x;
284 iconptr = pixels + y * rowstride;
285 maskptr = mask_pixels + y * mask_rowstride;
287 for (x = 0; x < width; ++x)
289 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
290 just R is sufficient. */
291 if (maskptr[0] == 0)
292 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
294 iconptr += rowstride/width;
295 maskptr += mask_rowstride/width;
299 g_object_unref (G_OBJECT (mask_buf));
302 return icon_buf;
305 static Lisp_Object
306 file_for_image (image)
307 Lisp_Object image;
309 Lisp_Object specified_file = Qnil;
310 Lisp_Object tail;
311 extern Lisp_Object QCfile;
313 for (tail = XCDR (image);
314 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
315 tail = XCDR (XCDR (tail)))
316 if (EQ (XCAR (tail), QCfile))
317 specified_file = XCAR (XCDR (tail));
319 return specified_file;
322 /* For the image defined in IMG, make and return a GtkImage. For displays with
323 8 planes or less we must make a GdkPixbuf and apply the mask manually.
324 Otherwise the highlightning and dimming the tool bar code in GTK does
325 will look bad. For display with more than 8 planes we just use the
326 pixmap and mask directly. For monochrome displays, GTK doesn't seem
327 able to use external pixmaps, it looks bad whatever we do.
328 The image is defined on the display where frame F is.
329 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
330 If OLD_WIDGET is NULL, a new widget is constructed and returned.
331 If OLD_WIDGET is not NULL, that widget is modified. */
333 static GtkWidget *
334 xg_get_image_for_pixmap (f, img, widget, old_widget)
335 FRAME_PTR f;
336 struct image *img;
337 GtkWidget *widget;
338 GtkImage *old_widget;
340 GdkPixmap *gpix;
341 GdkPixmap *gmask;
342 GdkDisplay *gdpy;
343 GdkColormap *cmap;
344 GdkPixbuf *icon_buf;
346 /* If we have a file, let GTK do all the image handling.
347 This seems to be the only way to make insensitive and activated icons
348 look good in all cases. */
349 Lisp_Object specified_file = file_for_image (img->spec);
350 Lisp_Object file;
352 /* We already loaded the image once before calling this
353 function, so this only fails if the image file has been removed.
354 In that case, use the pixmap already loaded. */
356 if (STRINGP (specified_file)
357 && STRINGP (file = x_find_image_file (specified_file)))
359 if (! old_widget)
360 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
361 else
362 gtk_image_set_from_file (old_widget, SSDATA (file));
364 return GTK_WIDGET (old_widget);
367 /* No file, do the image handling ourselves. This will look very bad
368 on a monochrome display, and sometimes bad on all displays with
369 certain themes. */
371 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
372 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
373 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
375 /* This is a workaround to make icons look good on pseudo color
376 displays. Apparently GTK expects the images to have an alpha
377 channel. If they don't, insensitive and activated icons will
378 look bad. This workaround does not work on monochrome displays,
379 and is strictly not needed on true color/static color displays (i.e.
380 16 bits and higher). But we do it anyway so we get a pixbuf that is
381 not associated with the img->pixmap. The img->pixmap may be removed
382 by clearing the image cache and then the tool bar redraw fails, since
383 Gtk+ assumes the pixmap is always there. */
384 cmap = gtk_widget_get_colormap (widget);
385 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
387 if (! old_widget)
388 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
389 else
390 gtk_image_set_from_pixbuf (old_widget, icon_buf);
392 g_object_unref (G_OBJECT (icon_buf));
394 g_object_unref (G_OBJECT (gpix));
395 if (gmask) g_object_unref (G_OBJECT (gmask));
397 return GTK_WIDGET (old_widget);
401 /* Set CURSOR on W and all widgets W contain. We must do like this
402 for scroll bars and menu because they create widgets internally,
403 and it is those widgets that are visible. */
405 static void
406 xg_set_cursor (w, cursor)
407 GtkWidget *w;
408 GdkCursor *cursor;
410 GList *children = gdk_window_peek_children (w->window);
412 gdk_window_set_cursor (w->window, cursor);
414 /* The scroll bar widget has more than one GDK window (had to look at
415 the source to figure this out), and there is no way to set cursor
416 on widgets in GTK. So we must set the cursor for all GDK windows.
417 Ditto for menus. */
419 for ( ; children; children = g_list_next (children))
420 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
423 /* Insert NODE into linked LIST. */
425 static void
426 xg_list_insert (xg_list_node *list, xg_list_node *node)
428 xg_list_node *list_start = list->next;
430 if (list_start) list_start->prev = node;
431 node->next = list_start;
432 node->prev = 0;
433 list->next = node;
436 /* Remove NODE from linked LIST. */
438 static void
439 xg_list_remove (xg_list_node *list, xg_list_node *node)
441 xg_list_node *list_start = list->next;
442 if (node == list_start)
444 list->next = node->next;
445 if (list->next) list->next->prev = 0;
447 else
449 node->prev->next = node->next;
450 if (node->next) node->next->prev = node->prev;
454 /* Allocate and return a utf8 version of STR. If STR is already
455 utf8 or NULL, just return STR.
456 If not, a new string is allocated and the caller must free the result
457 with g_free. */
459 static char *
460 get_utf8_string (str)
461 char *str;
463 char *utf8_str = str;
465 if (!str) return NULL;
467 /* If not UTF-8, try current locale. */
468 if (!g_utf8_validate (str, -1, NULL))
469 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
471 if (!utf8_str)
473 /* Probably some control characters in str. Escape them. */
474 size_t nr_bad = 0;
475 gsize bytes_read;
476 gsize bytes_written;
477 unsigned char *p = (unsigned char *)str;
478 char *cp, *up;
479 GError *error = NULL;
481 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
482 &bytes_written, &error))
483 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
485 ++nr_bad;
486 p += bytes_written+1;
487 g_error_free (error);
488 error = NULL;
491 if (error)
493 g_error_free (error);
494 error = NULL;
496 if (cp) g_free (cp);
498 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
499 p = (unsigned char *)str;
501 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
502 &bytes_written, &error))
503 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
505 strncpy (up, (char *)p, bytes_written);
506 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
507 up[bytes_written+4] = '\0';
508 up += bytes_written+4;
509 p += bytes_written+1;
510 g_error_free (error);
511 error = NULL;
514 if (cp)
516 strcat (utf8_str, cp);
517 g_free (cp);
519 if (error)
521 g_error_free (error);
522 error = NULL;
525 return utf8_str;
530 /***********************************************************************
531 General functions for creating widgets, resizing, events, e.t.c.
532 ***********************************************************************/
534 /* Make a geometry string and pass that to GTK. It seems this is the
535 only way to get geometry position right if the user explicitly
536 asked for a position when starting Emacs.
537 F is the frame we shall set geometry for. */
539 static void
540 xg_set_geometry (f)
541 FRAME_PTR f;
543 if (f->size_hint_flags & USPosition)
545 int left = f->left_pos;
546 int xneg = f->size_hint_flags & XNegative;
547 int top = f->top_pos;
548 int yneg = f->size_hint_flags & YNegative;
549 char geom_str[32];
551 if (xneg)
552 left = -left;
553 if (yneg)
554 top = -top;
556 sprintf (geom_str, "=%dx%d%c%d%c%d",
557 FRAME_PIXEL_WIDTH (f),
558 FRAME_TOTAL_PIXEL_HEIGHT (f),
559 (xneg ? '-' : '+'), left,
560 (yneg ? '-' : '+'), top);
562 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
563 geom_str))
564 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
566 else if (f->size_hint_flags & PPosition)
567 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
568 f->left_pos, f->top_pos);
571 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
572 and a Gtk+ menu bar, we get resize events for the edit part of the
573 frame only. We let Gtk+ deal with the Gtk+ parts.
574 F is the frame to resize.
575 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
577 void
578 xg_frame_resized (f, pixelwidth, pixelheight)
579 FRAME_PTR f;
580 int pixelwidth, pixelheight;
582 int rows, columns;
584 if (pixelwidth == -1 && pixelheight == -1)
586 if (FRAME_GTK_WIDGET (f) && GTK_WIDGET_MAPPED (FRAME_GTK_WIDGET (f)))
587 gdk_window_get_geometry(FRAME_GTK_WIDGET (f)->window, 0, 0,
588 &pixelwidth, &pixelheight, 0);
589 else return;
593 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
594 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
596 if (columns != FRAME_COLS (f)
597 || rows != FRAME_LINES (f)
598 || pixelwidth != FRAME_PIXEL_WIDTH (f)
599 || pixelheight != FRAME_PIXEL_HEIGHT (f))
601 FRAME_PIXEL_WIDTH (f) = pixelwidth;
602 FRAME_PIXEL_HEIGHT (f) = pixelheight;
604 change_frame_size (f, rows, columns, 0, 1, 0);
605 SET_FRAME_GARBAGED (f);
606 cancel_mouse_face (f);
610 /* Resize the outer window of frame F after chainging the height.
611 COLUMNS/ROWS is the size the edit area shall have after the resize. */
613 void
614 xg_frame_set_char_size (f, cols, rows)
615 FRAME_PTR f;
616 int cols;
617 int rows;
619 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
620 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
621 int pixelwidth;
623 if (FRAME_PIXEL_HEIGHT (f) == 0)
624 return;
626 /* Take into account the size of the scroll bar. Always use the
627 number of columns occupied by the scroll bar here otherwise we
628 might end up with a frame width that is not a multiple of the
629 frame's character width which is bad for vertically split
630 windows. */
631 f->scroll_bar_actual_width
632 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
634 compute_fringe_widths (f, 0);
636 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
637 after calculating that value. */
638 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
640 /* Must resize our top level widget. Font size may have changed,
641 but not rows/cols. */
642 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
643 pixelwidth, pixelheight);
644 x_wm_set_size_hint (f, 0, 0);
646 SET_FRAME_GARBAGED (f);
647 cancel_mouse_face (f);
649 /* We can not call change_frame_size for a mapped frame,
650 we can not set pixel width/height either. The window manager may
651 override our resize request, XMonad does this all the time.
652 The best we can do is try to sync, so lisp code sees the updated
653 size as fast as possible.
654 For unmapped windows, we can set rows/cols. When
655 the frame is mapped again we will (hopefully) get the correct size. */
656 if (f->async_visible)
658 /* Must call this to flush out events */
659 (void)gtk_events_pending ();
660 gdk_flush ();
661 x_wait_for_event (f, ConfigureNotify);
663 else
665 change_frame_size (f, rows, cols, 0, 1, 0);
666 FRAME_PIXEL_WIDTH (f) = pixelwidth;
667 FRAME_PIXEL_HEIGHT (f) = pixelheight;
671 /* Handle height changes (i.e. add/remove menu/toolbar).
672 The policy is to keep the number of editable lines. */
674 static void
675 xg_height_changed (f)
676 FRAME_PTR f;
678 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
679 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
680 f->output_data.x->hint_flags = 0;
681 x_wm_set_size_hint (f, 0, 0);
684 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
685 Must be done like this, because GtkWidget:s can have "hidden"
686 X Window that aren't accessible.
688 Return 0 if no widget match WDESC. */
690 GtkWidget *
691 xg_win_to_widget (dpy, wdesc)
692 Display *dpy;
693 Window wdesc;
695 gpointer gdkwin;
696 GtkWidget *gwdesc = 0;
698 BLOCK_INPUT;
700 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
701 wdesc);
702 if (gdkwin)
704 GdkEvent event;
705 event.any.window = gdkwin;
706 gwdesc = gtk_get_event_widget (&event);
709 UNBLOCK_INPUT;
710 return gwdesc;
713 /* Fill in the GdkColor C so that it represents PIXEL.
714 W is the widget that color will be used for. Used to find colormap. */
716 static void
717 xg_pix_to_gcolor (w, pixel, c)
718 GtkWidget *w;
719 unsigned long pixel;
720 GdkColor *c;
722 GdkColormap *map = gtk_widget_get_colormap (w);
723 gdk_colormap_query_color (map, pixel, c);
726 /* Create and set up the GTK widgets for frame F.
727 Return 0 if creation failed, non-zero otherwise. */
730 xg_create_frame_widgets (f)
731 FRAME_PTR f;
733 GtkWidget *wtop;
734 GtkWidget *wvbox;
735 GtkWidget *wfixed;
736 GdkColor bg;
737 GtkRcStyle *style;
738 int i;
739 char *title = 0;
741 BLOCK_INPUT;
743 if (FRAME_X_EMBEDDED_P (f))
744 wtop = gtk_plug_new (f->output_data.x->parent_desc);
745 else
746 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
748 xg_set_screen (wtop, f);
750 wvbox = gtk_vbox_new (FALSE, 0);
751 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
753 if (! wtop || ! wvbox || ! wfixed)
755 if (wtop) gtk_widget_destroy (wtop);
756 if (wvbox) gtk_widget_destroy (wvbox);
757 if (wfixed) gtk_widget_destroy (wfixed);
759 UNBLOCK_INPUT;
760 return 0;
763 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
764 gtk_widget_set_name (wtop, EMACS_CLASS);
765 gtk_widget_set_name (wvbox, "pane");
766 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
768 /* If this frame has a title or name, set it in the title bar. */
769 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
770 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
772 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
774 FRAME_GTK_OUTER_WIDGET (f) = wtop;
775 FRAME_GTK_WIDGET (f) = wfixed;
776 f->output_data.x->vbox_widget = wvbox;
778 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
780 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
781 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
783 if (FRAME_EXTERNAL_TOOL_BAR (f))
784 update_frame_tool_bar (f);
786 /* We don't want this widget double buffered, because we draw on it
787 with regular X drawing primitives, so from a GTK/GDK point of
788 view, the widget is totally blank. When an expose comes, this
789 will make the widget blank, and then Emacs redraws it. This flickers
790 a lot, so we turn off double buffering. */
791 gtk_widget_set_double_buffered (wfixed, FALSE);
793 gtk_window_set_wmclass (GTK_WINDOW (wtop),
794 SSDATA (Vx_resource_name),
795 SSDATA (Vx_resource_class));
797 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
798 GTK is to destroy the widget. We want Emacs to do that instead. */
799 g_signal_connect (G_OBJECT (wtop), "delete-event",
800 G_CALLBACK (gtk_true), 0);
802 /* Convert our geometry parameters into a geometry string
803 and specify it.
804 GTK will itself handle calculating the real position this way. */
805 xg_set_geometry (f);
806 int grav = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
807 f->win_gravity = grav;
809 gtk_widget_add_events (wfixed,
810 GDK_POINTER_MOTION_MASK
811 | GDK_EXPOSURE_MASK
812 | GDK_BUTTON_PRESS_MASK
813 | GDK_BUTTON_RELEASE_MASK
814 | GDK_KEY_PRESS_MASK
815 | GDK_ENTER_NOTIFY_MASK
816 | GDK_LEAVE_NOTIFY_MASK
817 | GDK_FOCUS_CHANGE_MASK
818 | GDK_STRUCTURE_MASK
819 | GDK_VISIBILITY_NOTIFY_MASK);
821 /* Must realize the windows so the X window gets created. It is used
822 by callers of this function. */
823 gtk_widget_realize (wfixed);
824 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
826 /* Since GTK clears its window by filling with the background color,
827 we must keep X and GTK background in sync. */
828 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
829 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
831 /* Also, do not let any background pixmap to be set, this looks very
832 bad as Emacs overwrites the background pixmap with its own idea
833 of background color. */
834 style = gtk_widget_get_modifier_style (wfixed);
836 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
837 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
838 gtk_widget_modify_style (wfixed, style);
840 /* GTK does not set any border, and they look bad with GTK. */
841 /* That they look bad is no excuse for imposing this here. --Stef
842 It should be done by providing the proper default in Fx_create_Frame.
843 f->border_width = 0;
844 f->internal_border_width = 0; */
846 UNBLOCK_INPUT;
848 return 1;
851 /* Set the normal size hints for the window manager, for frame F.
852 FLAGS is the flags word to use--or 0 meaning preserve the flags
853 that the window now has.
854 If USER_POSITION is nonzero, we set the User Position
855 flag (this is useful when FLAGS is 0). */
857 void
858 x_wm_set_size_hint (f, flags, user_position)
859 FRAME_PTR f;
860 long flags;
861 int user_position;
863 /* Don't set size hints during initialization; that apparently leads
864 to a race condition. See the thread at
865 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
866 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
867 return;
869 /* Must use GTK routines here, otherwise GTK resets the size hints
870 to its own defaults. */
871 GdkGeometry size_hints;
872 gint hint_flags = 0;
873 int base_width, base_height;
874 int min_rows = 0, min_cols = 0;
875 int win_gravity = f->win_gravity;
877 if (flags)
879 memset (&size_hints, 0, sizeof (size_hints));
880 f->output_data.x->size_hints = size_hints;
881 f->output_data.x->hint_flags = hint_flags;
883 else
884 flags = f->size_hint_flags;
886 size_hints = f->output_data.x->size_hints;
887 hint_flags = f->output_data.x->hint_flags;
889 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
890 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
891 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
893 hint_flags |= GDK_HINT_BASE_SIZE;
894 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
895 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
896 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
898 check_frame_size (f, &min_rows, &min_cols);
900 size_hints.base_width = base_width;
901 size_hints.base_height = base_height;
902 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
903 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
905 /* These currently have a one to one mapping with the X values, but I
906 don't think we should rely on that. */
907 hint_flags |= GDK_HINT_WIN_GRAVITY;
908 size_hints.win_gravity = 0;
909 if (win_gravity == NorthWestGravity)
910 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
911 else if (win_gravity == NorthGravity)
912 size_hints.win_gravity = GDK_GRAVITY_NORTH;
913 else if (win_gravity == NorthEastGravity)
914 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
915 else if (win_gravity == WestGravity)
916 size_hints.win_gravity = GDK_GRAVITY_WEST;
917 else if (win_gravity == CenterGravity)
918 size_hints.win_gravity = GDK_GRAVITY_CENTER;
919 else if (win_gravity == EastGravity)
920 size_hints.win_gravity = GDK_GRAVITY_EAST;
921 else if (win_gravity == SouthWestGravity)
922 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
923 else if (win_gravity == SouthGravity)
924 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
925 else if (win_gravity == SouthEastGravity)
926 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
927 else if (win_gravity == StaticGravity)
928 size_hints.win_gravity = GDK_GRAVITY_STATIC;
930 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
931 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
932 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
934 if (user_position)
936 hint_flags &= ~GDK_HINT_POS;
937 hint_flags |= GDK_HINT_USER_POS;
940 if (hint_flags != f->output_data.x->hint_flags
941 || memcmp (&size_hints,
942 &f->output_data.x->size_hints,
943 sizeof (size_hints)) != 0)
945 BLOCK_INPUT;
946 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
947 NULL, &size_hints, hint_flags);
948 f->output_data.x->size_hints = size_hints;
949 f->output_data.x->hint_flags = hint_flags;
950 UNBLOCK_INPUT;
954 /* Change background color of a frame.
955 Since GTK uses the background color to clear the window, we must
956 keep the GTK and X colors in sync.
957 F is the frame to change,
958 BG is the pixel value to change to. */
960 void
961 xg_set_background_color (f, bg)
962 FRAME_PTR f;
963 unsigned long bg;
965 if (FRAME_GTK_WIDGET (f))
967 GdkColor gdk_bg;
969 BLOCK_INPUT;
970 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
971 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
972 UNBLOCK_INPUT;
977 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
978 functions so GTK does not overwrite the icon. */
980 void
981 xg_set_frame_icon (f, icon_pixmap, icon_mask)
982 FRAME_PTR f;
983 Pixmap icon_pixmap;
984 Pixmap icon_mask;
986 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
987 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
988 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
989 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
991 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
996 /***********************************************************************
997 Dialog functions
998 ***********************************************************************/
999 /* Return the dialog title to use for a dialog of type KEY.
1000 This is the encoding used by lwlib. We use the same for GTK. */
1002 static char *
1003 get_dialog_title (char key)
1005 char *title = "";
1007 switch (key) {
1008 case 'E': case 'e':
1009 title = "Error";
1010 break;
1012 case 'I': case 'i':
1013 title = "Information";
1014 break;
1016 case 'L': case 'l':
1017 title = "Prompt";
1018 break;
1020 case 'P': case 'p':
1021 title = "Prompt";
1022 break;
1024 case 'Q': case 'q':
1025 title = "Question";
1026 break;
1029 return title;
1032 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1033 the dialog, but return TRUE so the event does not propagate further
1034 in GTK. This prevents GTK from destroying the dialog widget automatically
1035 and we can always destrou the widget manually, regardles of how
1036 it was popped down (button press or WM_DELETE_WINDOW).
1037 W is the dialog widget.
1038 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1039 user_data is NULL (not used).
1041 Returns TRUE to end propagation of event. */
1043 static gboolean
1044 dialog_delete_callback (w, event, user_data)
1045 GtkWidget *w;
1046 GdkEvent *event;
1047 gpointer user_data;
1049 gtk_widget_unmap (w);
1050 return TRUE;
1053 /* Create a popup dialog window. See also xg_create_widget below.
1054 WV is a widget_value describing the dialog.
1055 SELECT_CB is the callback to use when a button has been pressed.
1056 DEACTIVATE_CB is the callback to use when the dialog pops down.
1058 Returns the GTK dialog widget. */
1060 static GtkWidget *
1061 create_dialog (wv, select_cb, deactivate_cb)
1062 widget_value *wv;
1063 GCallback select_cb;
1064 GCallback deactivate_cb;
1066 char *title = get_dialog_title (wv->name[0]);
1067 int total_buttons = wv->name[1] - '0';
1068 int right_buttons = wv->name[4] - '0';
1069 int left_buttons;
1070 int button_nr = 0;
1071 int button_spacing = 10;
1072 GtkWidget *wdialog = gtk_dialog_new ();
1073 widget_value *item;
1074 GtkBox *cur_box;
1075 GtkWidget *wvbox;
1076 GtkWidget *whbox_up;
1077 GtkWidget *whbox_down;
1079 /* If the number of buttons is greater than 4, make two rows of buttons
1080 instead. This looks better. */
1081 int make_two_rows = total_buttons > 4;
1083 if (right_buttons == 0) right_buttons = total_buttons/2;
1084 left_buttons = total_buttons - right_buttons;
1086 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1087 gtk_widget_set_name (wdialog, "emacs-dialog");
1089 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1091 if (make_two_rows)
1093 wvbox = gtk_vbox_new (TRUE, button_spacing);
1094 whbox_up = gtk_hbox_new (FALSE, 0);
1095 whbox_down = gtk_hbox_new (FALSE, 0);
1097 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1098 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1099 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1101 cur_box = GTK_BOX (whbox_up);
1104 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1105 G_CALLBACK (dialog_delete_callback), 0);
1107 if (deactivate_cb)
1109 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1110 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1113 for (item = wv->contents; item; item = item->next)
1115 char *utf8_label = get_utf8_string (item->value);
1116 GtkWidget *w;
1117 GtkRequisition req;
1119 if (item->name && strcmp (item->name, "message") == 0)
1121 /* This is the text part of the dialog. */
1122 w = gtk_label_new (utf8_label);
1123 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1124 gtk_label_new (""),
1125 FALSE, FALSE, 0);
1126 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1127 TRUE, TRUE, 0);
1128 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1130 /* Try to make dialog look better. Must realize first so
1131 the widget can calculate the size it needs. */
1132 gtk_widget_realize (w);
1133 gtk_widget_size_request (w, &req);
1134 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1135 req.height);
1136 if (item->value && strlen (item->value) > 0)
1137 button_spacing = 2*req.width/strlen (item->value);
1139 else
1141 /* This is one button to add to the dialog. */
1142 w = gtk_button_new_with_label (utf8_label);
1143 if (! item->enabled)
1144 gtk_widget_set_sensitive (w, FALSE);
1145 if (select_cb)
1146 g_signal_connect (G_OBJECT (w), "clicked",
1147 select_cb, item->call_data);
1149 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1150 if (++button_nr == left_buttons)
1152 if (make_two_rows)
1153 cur_box = GTK_BOX (whbox_down);
1154 else
1155 gtk_box_pack_start (cur_box,
1156 gtk_label_new (""),
1157 TRUE, TRUE,
1158 button_spacing);
1162 if (utf8_label && utf8_label != item->value)
1163 g_free (utf8_label);
1166 return wdialog;
1169 struct xg_dialog_data
1171 GMainLoop *loop;
1172 int response;
1173 GtkWidget *w;
1174 guint timerid;
1177 /* Function that is called when the file or font dialogs pop down.
1178 W is the dialog widget, RESPONSE is the response code.
1179 USER_DATA is what we passed in to g_signal_connect. */
1181 static void
1182 xg_dialog_response_cb (w,
1183 response,
1184 user_data)
1185 GtkDialog *w;
1186 gint response;
1187 gpointer user_data;
1189 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1190 dd->response = response;
1191 g_main_loop_quit (dd->loop);
1195 /* Destroy the dialog. This makes it pop down. */
1197 static Lisp_Object
1198 pop_down_dialog (arg)
1199 Lisp_Object arg;
1201 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1202 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1204 BLOCK_INPUT;
1205 if (dd->w) gtk_widget_destroy (dd->w);
1206 if (dd->timerid != 0) g_source_remove (dd->timerid);
1208 g_main_loop_quit (dd->loop);
1209 g_main_loop_unref (dd->loop);
1211 UNBLOCK_INPUT;
1213 return Qnil;
1216 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1217 We pass in DATA as gpointer* so we can use this as a callback. */
1219 static gboolean
1220 xg_maybe_add_timer (data)
1221 gpointer data;
1223 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1224 EMACS_TIME next_time = timer_check (1);
1225 long secs = EMACS_SECS (next_time);
1226 long usecs = EMACS_USECS (next_time);
1228 dd->timerid = 0;
1230 if (secs >= 0 && usecs >= 0 && secs < ((guint)-1)/1000)
1232 dd->timerid = g_timeout_add (secs * 1000 + usecs/1000,
1233 xg_maybe_add_timer,
1234 dd);
1236 return FALSE;
1240 /* Pops up a modal dialog W and waits for response.
1241 We don't use gtk_dialog_run because we want to process emacs timers.
1242 The dialog W is not destroyed when this function returns. */
1244 static int
1245 xg_dialog_run (f, w)
1246 FRAME_PTR f;
1247 GtkWidget *w;
1250 int count = SPECPDL_INDEX ();
1251 struct xg_dialog_data dd;
1253 xg_set_screen (w, f);
1254 gtk_window_set_transient_for (GTK_WINDOW (w),
1255 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1256 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1257 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1259 dd.loop = g_main_loop_new (NULL, FALSE);
1260 dd.response = GTK_RESPONSE_CANCEL;
1261 dd.w = w;
1262 dd.timerid = 0;
1264 g_signal_connect (G_OBJECT (w),
1265 "response",
1266 G_CALLBACK (xg_dialog_response_cb),
1267 &dd);
1268 /* Don't destroy the widget if closed by the window manager close button. */
1269 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1270 gtk_widget_show (w);
1272 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1274 (void) xg_maybe_add_timer (&dd);
1275 g_main_loop_run (dd.loop);
1277 dd.w = 0;
1278 unbind_to (count, Qnil);
1280 return dd.response;
1284 /***********************************************************************
1285 File dialog functions
1286 ***********************************************************************/
1287 /* Return non-zero if the old file selection dialog is being used.
1288 Return zero if not. */
1291 xg_uses_old_file_dialog ()
1293 #ifdef HAVE_GTK_FILE_BOTH
1294 extern int x_gtk_use_old_file_dialog;
1295 return x_gtk_use_old_file_dialog;
1296 #else /* ! HAVE_GTK_FILE_BOTH */
1298 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1299 return 1;
1300 #else
1301 return 0;
1302 #endif
1304 #endif /* ! HAVE_GTK_FILE_BOTH */
1308 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1310 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1312 /* Return the selected file for file chooser dialog W.
1313 The returned string must be free:d. */
1315 static char *
1316 xg_get_file_name_from_chooser (w)
1317 GtkWidget *w;
1319 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1322 /* Callback called when the "Show hidden files" toggle is pressed.
1323 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1325 static void
1326 xg_toggle_visibility_cb (widget, data)
1327 GtkWidget *widget;
1328 gpointer data;
1330 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1331 gboolean visible;
1332 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1333 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1337 /* Callback called when a property changes in a file chooser.
1338 GOBJECT is the file chooser dialog, ARG1 describes the property.
1339 USER_DATA is the toggle widget in the file chooser dialog.
1340 We use this to update the "Show hidden files" toggle when the user
1341 changes that property by right clicking in the file list. */
1343 static void
1344 xg_toggle_notify_cb (gobject, arg1, user_data)
1345 GObject *gobject;
1346 GParamSpec *arg1;
1347 gpointer user_data;
1349 extern int x_gtk_show_hidden_files;
1351 if (strcmp (arg1->name, "show-hidden") == 0)
1353 GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
1354 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1355 gboolean visible, toggle_on;
1357 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1358 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1360 if (!!visible != !!toggle_on)
1362 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1363 G_CALLBACK (xg_toggle_visibility_cb),
1364 gobject);
1365 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1366 g_signal_handlers_unblock_by_func
1367 (G_OBJECT (wtoggle),
1368 G_CALLBACK (xg_toggle_visibility_cb),
1369 gobject);
1371 x_gtk_show_hidden_files = visible;
1375 /* Read a file name from the user using a file chooser dialog.
1376 F is the current frame.
1377 PROMPT is a prompt to show to the user. May not be NULL.
1378 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1379 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1380 file. *FUNC is set to a function that can be used to retrieve the
1381 selected file name from the returned widget.
1383 Returns the created widget. */
1385 static GtkWidget *
1386 xg_get_file_with_chooser (f, prompt, default_filename,
1387 mustmatch_p, only_dir_p, func)
1388 FRAME_PTR f;
1389 char *prompt;
1390 char *default_filename;
1391 int mustmatch_p, only_dir_p;
1392 xg_get_file_func *func;
1394 char message[1024];
1396 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1397 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1398 GtkFileChooserAction action = (mustmatch_p ?
1399 GTK_FILE_CHOOSER_ACTION_OPEN :
1400 GTK_FILE_CHOOSER_ACTION_SAVE);
1401 extern int x_gtk_show_hidden_files;
1402 extern int x_gtk_file_dialog_help_text;
1405 if (only_dir_p)
1406 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1408 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1409 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1410 (mustmatch_p || only_dir_p ?
1411 GTK_STOCK_OPEN : GTK_STOCK_OK),
1412 GTK_RESPONSE_OK,
1413 NULL);
1414 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1416 wbox = gtk_vbox_new (FALSE, 0);
1417 gtk_widget_show (wbox);
1418 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1420 if (x_gtk_show_hidden_files)
1422 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1423 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1425 gtk_widget_show (wtoggle);
1426 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1427 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1428 g_signal_connect (G_OBJECT (filewin), "notify",
1429 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1431 if (x_gtk_file_dialog_help_text)
1433 message[0] = '\0';
1434 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1435 Show the C-l help text only for versions < 2.10. */
1436 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1437 strcat (message, "\nType C-l to display a file name text entry box.\n");
1438 strcat (message, "\nIf you don't like this file selector, use the "
1439 "corresponding\nkey binding or customize "
1440 "use-file-dialog to turn it off.");
1442 wmessage = gtk_label_new (message);
1443 gtk_widget_show (wmessage);
1446 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1447 if (x_gtk_file_dialog_help_text)
1448 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1449 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1451 if (default_filename)
1453 Lisp_Object file;
1454 struct gcpro gcpro1;
1455 char *utf8_filename;
1456 GCPRO1 (file);
1458 file = build_string (default_filename);
1460 /* File chooser does not understand ~/... in the file name. It must be
1461 an absolute name starting with /. */
1462 if (default_filename[0] != '/')
1463 file = Fexpand_file_name (file, Qnil);
1465 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1466 if (! NILP (Ffile_directory_p (file)))
1467 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1468 utf8_filename);
1469 else
1471 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1472 utf8_filename);
1473 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1475 char *cp = strrchr (utf8_filename, '/');
1476 if (cp) ++cp;
1477 else cp = utf8_filename;
1478 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1482 UNGCPRO;
1485 *func = xg_get_file_name_from_chooser;
1486 return filewin;
1488 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1490 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1492 /* Return the selected file for file selector dialog W.
1493 The returned string must be free:d. */
1495 static char *
1496 xg_get_file_name_from_selector (w)
1497 GtkWidget *w;
1499 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1500 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1503 /* Create a file selection dialog.
1504 F is the current frame.
1505 PROMPT is a prompt to show to the user. May not be NULL.
1506 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1507 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1508 file. *FUNC is set to a function that can be used to retrieve the
1509 selected file name from the returned widget.
1511 Returns the created widget. */
1513 static GtkWidget *
1514 xg_get_file_with_selection (f, prompt, default_filename,
1515 mustmatch_p, only_dir_p, func)
1516 FRAME_PTR f;
1517 char *prompt;
1518 char *default_filename;
1519 int mustmatch_p, only_dir_p;
1520 xg_get_file_func *func;
1522 GtkWidget *filewin;
1523 GtkFileSelection *filesel;
1525 filewin = gtk_file_selection_new (prompt);
1526 filesel = GTK_FILE_SELECTION (filewin);
1528 if (default_filename)
1529 gtk_file_selection_set_filename (filesel, default_filename);
1531 if (mustmatch_p)
1533 /* The selection_entry part of filesel is not documented. */
1534 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1535 gtk_file_selection_hide_fileop_buttons (filesel);
1538 *func = xg_get_file_name_from_selector;
1540 return filewin;
1542 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1544 /* Read a file name from the user using a file dialog, either the old
1545 file selection dialog, or the new file chooser dialog. Which to use
1546 depends on what the GTK version used has, and what the value of
1547 gtk-use-old-file-dialog.
1548 F is the current frame.
1549 PROMPT is a prompt to show to the user. May not be NULL.
1550 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1551 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1552 file.
1554 Returns a file name or NULL if no file was selected.
1555 The returned string must be freed by the caller. */
1557 char *
1558 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1559 FRAME_PTR f;
1560 char *prompt;
1561 char *default_filename;
1562 int mustmatch_p, only_dir_p;
1564 GtkWidget *w = 0;
1565 char *fn = 0;
1566 int filesel_done = 0;
1567 xg_get_file_func func;
1569 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1570 /* I really don't know why this is needed, but without this the GLIBC add on
1571 library linuxthreads hangs when the Gnome file chooser backend creates
1572 threads. */
1573 sigblock (sigmask (__SIGRTMIN));
1574 #endif /* HAVE_GTK_AND_PTHREAD */
1576 #ifdef HAVE_GTK_FILE_BOTH
1578 if (xg_uses_old_file_dialog ())
1579 w = xg_get_file_with_selection (f, prompt, default_filename,
1580 mustmatch_p, only_dir_p, &func);
1581 else
1582 w = xg_get_file_with_chooser (f, prompt, default_filename,
1583 mustmatch_p, only_dir_p, &func);
1585 #else /* not HAVE_GTK_FILE_BOTH */
1587 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1588 w = xg_get_file_with_selection (f, prompt, default_filename,
1589 mustmatch_p, only_dir_p, &func);
1590 #endif
1591 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1592 w = xg_get_file_with_chooser (f, prompt, default_filename,
1593 mustmatch_p, only_dir_p, &func);
1594 #endif
1596 #endif /* HAVE_GTK_FILE_BOTH */
1598 gtk_widget_set_name (w, "emacs-filedialog");
1600 filesel_done = xg_dialog_run (f, w);
1602 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1603 sigunblock (sigmask (__SIGRTMIN));
1604 #endif
1606 if (filesel_done == GTK_RESPONSE_OK)
1607 fn = (*func) (w);
1609 gtk_widget_destroy (w);
1610 return fn;
1613 #ifdef HAVE_FREETYPE
1614 /* Pop up a GTK font selector and return the name of the font the user
1615 selects, as a C string. The returned font name follows GTK's own
1616 format:
1618 `FAMILY [VALUE1 VALUE2] SIZE'
1620 This can be parsed using font_parse_fcname in font.c.
1621 DEFAULT_NAME, if non-zero, is the default font name. */
1623 char *
1624 xg_get_font_name (f, default_name)
1625 FRAME_PTR f;
1626 char *default_name;
1628 GtkWidget *w;
1629 char *fontname = NULL;
1630 int done = 0;
1632 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1633 sigblock (sigmask (__SIGRTMIN));
1634 #endif /* HAVE_GTK_AND_PTHREAD */
1636 w = gtk_font_selection_dialog_new ("Pick a font");
1637 if (!default_name)
1638 default_name = "Monospace 10";
1639 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1640 default_name);
1642 gtk_widget_set_name (w, "emacs-fontdialog");
1644 done = xg_dialog_run (f, w);
1646 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1647 sigunblock (sigmask (__SIGRTMIN));
1648 #endif
1650 if (done == GTK_RESPONSE_OK)
1651 fontname = gtk_font_selection_dialog_get_font_name
1652 (GTK_FONT_SELECTION_DIALOG (w));
1654 gtk_widget_destroy (w);
1655 return fontname;
1657 #endif /* HAVE_FREETYPE */
1661 /***********************************************************************
1662 Menu functions.
1663 ***********************************************************************/
1665 /* The name of menu items that can be used for customization. Since GTK
1666 RC files are very crude and primitive, we have to set this on all
1667 menu item names so a user can easily customize menu items. */
1669 #define MENU_ITEM_NAME "emacs-menuitem"
1672 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1673 during GC. The next member points to the items. */
1674 static xg_list_node xg_menu_cb_list;
1676 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1677 during GC. The next member points to the items. */
1678 static xg_list_node xg_menu_item_cb_list;
1680 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1681 F is the frame CL_DATA will be initialized for.
1682 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1684 The menu bar and all sub menus under the menu bar in a frame
1685 share the same structure, hence the reference count.
1687 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1688 allocated xg_menu_cb_data if CL_DATA is NULL. */
1690 static xg_menu_cb_data *
1691 make_cl_data (cl_data, f, highlight_cb)
1692 xg_menu_cb_data *cl_data;
1693 FRAME_PTR f;
1694 GCallback highlight_cb;
1696 if (! cl_data)
1698 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1699 cl_data->f = f;
1700 cl_data->menu_bar_vector = f->menu_bar_vector;
1701 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1702 cl_data->highlight_cb = highlight_cb;
1703 cl_data->ref_count = 0;
1705 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1708 cl_data->ref_count++;
1710 return cl_data;
1713 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1714 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1716 When the menu bar is updated, menu items may have been added and/or
1717 removed, so menu_bar_vector and menu_bar_items_used change. We must
1718 then update CL_DATA since it is used to determine which menu
1719 item that is invoked in the menu.
1720 HIGHLIGHT_CB could change, there is no check that the same
1721 function is given when modifying a menu bar as was given when
1722 creating the menu bar. */
1724 static void
1725 update_cl_data (cl_data, f, highlight_cb)
1726 xg_menu_cb_data *cl_data;
1727 FRAME_PTR f;
1728 GCallback highlight_cb;
1730 if (cl_data)
1732 cl_data->f = f;
1733 cl_data->menu_bar_vector = f->menu_bar_vector;
1734 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1735 cl_data->highlight_cb = highlight_cb;
1739 /* Decrease reference count for CL_DATA.
1740 If reference count is zero, free CL_DATA. */
1742 static void
1743 unref_cl_data (cl_data)
1744 xg_menu_cb_data *cl_data;
1746 if (cl_data && cl_data->ref_count > 0)
1748 cl_data->ref_count--;
1749 if (cl_data->ref_count == 0)
1751 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1752 xfree (cl_data);
1757 /* Function that marks all lisp data during GC. */
1759 void
1760 xg_mark_data ()
1762 xg_list_node *iter;
1764 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1765 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1767 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1769 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1771 if (! NILP (cb_data->help))
1772 mark_object (cb_data->help);
1777 /* Callback called when a menu item is destroyed. Used to free data.
1778 W is the widget that is being destroyed (not used).
1779 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1781 static void
1782 menuitem_destroy_callback (w, client_data)
1783 GtkWidget *w;
1784 gpointer client_data;
1786 if (client_data)
1788 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1789 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1790 xfree (data);
1794 /* Callback called when the pointer enters/leaves a menu item.
1795 W is the parent of the menu item.
1796 EVENT is either an enter event or leave event.
1797 CLIENT_DATA is not used.
1799 Returns FALSE to tell GTK to keep processing this event. */
1801 static gboolean
1802 menuitem_highlight_callback (w, event, client_data)
1803 GtkWidget *w;
1804 GdkEventCrossing *event;
1805 gpointer client_data;
1807 GdkEvent ev;
1808 GtkWidget *subwidget;
1809 xg_menu_item_cb_data *data;
1811 ev.crossing = *event;
1812 subwidget = gtk_get_event_widget (&ev);
1813 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1814 XG_ITEM_DATA);
1815 if (data)
1817 if (! NILP (data->help) && data->cl_data->highlight_cb)
1819 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1820 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1821 (*func) (subwidget, call_data);
1825 return FALSE;
1828 /* Callback called when a menu is destroyed. Used to free data.
1829 W is the widget that is being destroyed (not used).
1830 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1832 static void
1833 menu_destroy_callback (w, client_data)
1834 GtkWidget *w;
1835 gpointer client_data;
1837 unref_cl_data ((xg_menu_cb_data*) client_data);
1840 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1841 must be non-NULL) and can be inserted into a menu item.
1843 Returns the GtkHBox. */
1845 static GtkWidget *
1846 make_widget_for_menu_item (utf8_label, utf8_key)
1847 char *utf8_label;
1848 char *utf8_key;
1850 GtkWidget *wlbl;
1851 GtkWidget *wkey;
1852 GtkWidget *wbox;
1854 wbox = gtk_hbox_new (FALSE, 0);
1855 wlbl = gtk_label_new (utf8_label);
1856 wkey = gtk_label_new (utf8_key);
1858 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1859 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1861 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1862 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1864 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1865 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1866 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1868 return wbox;
1871 /* Make and return a menu item widget with the key to the right.
1872 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1873 UTF8_KEY is the text representing the key binding.
1874 ITEM is the widget_value describing the menu item.
1876 GROUP is an in/out parameter. If the menu item to be created is not
1877 part of any radio menu group, *GROUP contains NULL on entry and exit.
1878 If the menu item to be created is part of a radio menu group, on entry
1879 *GROUP contains the group to use, or NULL if this is the first item
1880 in the group. On exit, *GROUP contains the radio item group.
1882 Unfortunately, keys don't line up as nicely as in Motif,
1883 but the MacOS X version doesn't either, so I guess that is OK. */
1885 static GtkWidget *
1886 make_menu_item (utf8_label, utf8_key, item, group)
1887 char *utf8_label;
1888 char *utf8_key;
1889 widget_value *item;
1890 GSList **group;
1892 GtkWidget *w;
1893 GtkWidget *wtoadd = 0;
1895 /* It has been observed that some menu items have a NULL name field.
1896 This will lead to this function being called with a NULL utf8_label.
1897 GTK crashes on that so we set a blank label. Why there is a NULL
1898 name remains to be investigated. */
1899 if (! utf8_label) utf8_label = " ";
1901 if (utf8_key)
1902 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1904 if (item->button_type == BUTTON_TYPE_TOGGLE)
1906 *group = NULL;
1907 if (utf8_key) w = gtk_check_menu_item_new ();
1908 else w = gtk_check_menu_item_new_with_label (utf8_label);
1909 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1911 else if (item->button_type == BUTTON_TYPE_RADIO)
1913 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1914 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1915 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1916 if (item->selected)
1917 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1919 else
1921 *group = NULL;
1922 if (utf8_key) w = gtk_menu_item_new ();
1923 else w = gtk_menu_item_new_with_label (utf8_label);
1926 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1927 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1929 return w;
1932 /* Return non-zero if LABEL specifies a separator (GTK only has one
1933 separator type) */
1935 static const char* separator_names[] = {
1936 "space",
1937 "no-line",
1938 "single-line",
1939 "double-line",
1940 "single-dashed-line",
1941 "double-dashed-line",
1942 "shadow-etched-in",
1943 "shadow-etched-out",
1944 "shadow-etched-in-dash",
1945 "shadow-etched-out-dash",
1946 "shadow-double-etched-in",
1947 "shadow-double-etched-out",
1948 "shadow-double-etched-in-dash",
1949 "shadow-double-etched-out-dash",
1953 static int
1954 xg_separator_p (char *label)
1956 if (! label) return 0;
1957 else if (strlen (label) > 3
1958 && strncmp (label, "--", 2) == 0
1959 && label[2] != '-')
1961 int i;
1963 label += 2;
1964 for (i = 0; separator_names[i]; ++i)
1965 if (strcmp (label, separator_names[i]) == 0)
1966 return 1;
1968 else
1970 /* Old-style separator, maybe. It's a separator if it contains
1971 only dashes. */
1972 while (*label == '-')
1973 ++label;
1974 if (*label == 0) return 1;
1977 return 0;
1980 static int xg_detached_menus;
1982 /* Returns non-zero if there are detached menus. */
1985 xg_have_tear_offs ()
1987 return xg_detached_menus > 0;
1990 /* Callback invoked when a detached menu window is removed. Here we
1991 decrease the xg_detached_menus count.
1992 WIDGET is the top level window that is removed (the parent of the menu).
1993 CLIENT_DATA is not used. */
1995 static void
1996 tearoff_remove (widget, client_data)
1997 GtkWidget *widget;
1998 gpointer client_data;
2000 if (xg_detached_menus > 0) --xg_detached_menus;
2003 /* Callback invoked when a menu is detached. It increases the
2004 xg_detached_menus count.
2005 WIDGET is the GtkTearoffMenuItem.
2006 CLIENT_DATA is not used. */
2008 static void
2009 tearoff_activate (widget, client_data)
2010 GtkWidget *widget;
2011 gpointer client_data;
2013 GtkWidget *menu = gtk_widget_get_parent (widget);
2014 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2016 ++xg_detached_menus;
2017 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2018 "destroy",
2019 G_CALLBACK (tearoff_remove), 0);
2024 /* Create a menu item widget, and connect the callbacks.
2025 ITEM decribes the menu item.
2026 F is the frame the created menu belongs to.
2027 SELECT_CB is the callback to use when a menu item is selected.
2028 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2029 CL_DATA points to the callback data to be used for this menu.
2030 GROUP is an in/out parameter. If the menu item to be created is not
2031 part of any radio menu group, *GROUP contains NULL on entry and exit.
2032 If the menu item to be created is part of a radio menu group, on entry
2033 *GROUP contains the group to use, or NULL if this is the first item
2034 in the group. On exit, *GROUP contains the radio item group.
2036 Returns the created GtkWidget. */
2038 static GtkWidget *
2039 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
2040 widget_value *item;
2041 FRAME_PTR f;
2042 GCallback select_cb;
2043 GCallback highlight_cb;
2044 xg_menu_cb_data *cl_data;
2045 GSList **group;
2047 char *utf8_label;
2048 char *utf8_key;
2049 GtkWidget *w;
2050 xg_menu_item_cb_data *cb_data;
2052 utf8_label = get_utf8_string (item->name);
2053 utf8_key = get_utf8_string (item->key);
2055 w = make_menu_item (utf8_label, utf8_key, item, group);
2057 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2058 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
2060 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2062 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2064 cb_data->select_id = 0;
2065 cb_data->help = item->help;
2066 cb_data->cl_data = cl_data;
2067 cb_data->call_data = item->call_data;
2069 g_signal_connect (G_OBJECT (w),
2070 "destroy",
2071 G_CALLBACK (menuitem_destroy_callback),
2072 cb_data);
2074 /* Put cb_data in widget, so we can get at it when modifying menubar */
2075 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2077 /* final item, not a submenu */
2078 if (item->call_data && ! item->contents)
2080 if (select_cb)
2081 cb_data->select_id
2082 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2085 return w;
2088 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
2089 GCallback, GCallback, int, int, int,
2090 GtkWidget *, xg_menu_cb_data *, char *));
2092 /* Create a full menu tree specified by DATA.
2093 F is the frame the created menu belongs to.
2094 SELECT_CB is the callback to use when a menu item is selected.
2095 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2096 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2097 POP_UP_P is non-zero if we shall create a popup menu.
2098 MENU_BAR_P is non-zero if we shall create a menu bar.
2099 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2100 if MENU_BAR_P is non-zero.
2101 TOPMENU is the topmost GtkWidget that others shall be placed under.
2102 It may be NULL, in that case we create the appropriate widget
2103 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2104 CL_DATA is the callback data we shall use for this menu, or NULL
2105 if we haven't set the first callback yet.
2106 NAME is the name to give to the top level menu if this function
2107 creates it. May be NULL to not set any name.
2109 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2110 not NULL.
2112 This function calls itself to create submenus. */
2114 static GtkWidget *
2115 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
2116 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
2117 widget_value *data;
2118 FRAME_PTR f;
2119 GCallback select_cb;
2120 GCallback deactivate_cb;
2121 GCallback highlight_cb;
2122 int pop_up_p;
2123 int menu_bar_p;
2124 int add_tearoff_p;
2125 GtkWidget *topmenu;
2126 xg_menu_cb_data *cl_data;
2127 char *name;
2129 widget_value *item;
2130 GtkWidget *wmenu = topmenu;
2131 GSList *group = NULL;
2133 if (! topmenu)
2135 if (! menu_bar_p)
2137 wmenu = gtk_menu_new ();
2138 xg_set_screen (wmenu, f);
2139 /* Connect this to the menu instead of items so we get enter/leave for
2140 disabled items also. TODO: Still does not get enter/leave for
2141 disabled items in detached menus. */
2142 g_signal_connect (G_OBJECT (wmenu),
2143 "enter-notify-event",
2144 G_CALLBACK (menuitem_highlight_callback),
2145 NULL);
2146 g_signal_connect (G_OBJECT (wmenu),
2147 "leave-notify-event",
2148 G_CALLBACK (menuitem_highlight_callback),
2149 NULL);
2151 else
2153 wmenu = gtk_menu_bar_new ();
2154 /* Set width of menu bar to a small value so it doesn't enlarge
2155 a small initial frame size. The width will be set to the
2156 width of the frame later on when it is added to a container.
2157 height -1: Natural height. */
2158 gtk_widget_set_size_request (wmenu, 1, -1);
2161 /* Put cl_data on the top menu for easier access. */
2162 cl_data = make_cl_data (cl_data, f, highlight_cb);
2163 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2164 g_signal_connect (G_OBJECT (wmenu), "destroy",
2165 G_CALLBACK (menu_destroy_callback), cl_data);
2167 if (name)
2168 gtk_widget_set_name (wmenu, name);
2170 if (deactivate_cb)
2171 g_signal_connect (G_OBJECT (wmenu),
2172 "selection-done", deactivate_cb, 0);
2175 if (! menu_bar_p && add_tearoff_p)
2177 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2178 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2180 g_signal_connect (G_OBJECT (tearoff), "activate",
2181 G_CALLBACK (tearoff_activate), 0);
2184 for (item = data; item; item = item->next)
2186 GtkWidget *w;
2188 if (pop_up_p && !item->contents && !item->call_data
2189 && !xg_separator_p (item->name))
2191 char *utf8_label;
2192 /* A title for a popup. We do the same as GTK does when
2193 creating titles, but it does not look good. */
2194 group = NULL;
2195 utf8_label = get_utf8_string (item->name);
2197 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2198 w = gtk_menu_item_new_with_label (utf8_label);
2199 gtk_widget_set_sensitive (w, FALSE);
2200 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2202 else if (xg_separator_p (item->name))
2204 group = NULL;
2205 /* GTK only have one separator type. */
2206 w = gtk_separator_menu_item_new ();
2208 else
2210 w = xg_create_one_menuitem (item,
2212 item->contents ? 0 : select_cb,
2213 highlight_cb,
2214 cl_data,
2215 &group);
2217 /* Create a possibly empty submenu for menu bar items, since some
2218 themes don't highlight items correctly without it. */
2219 if (item->contents || menu_bar_p)
2221 GtkWidget *submenu = create_menus (item->contents,
2223 select_cb,
2224 deactivate_cb,
2225 highlight_cb,
2228 add_tearoff_p,
2230 cl_data,
2232 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2236 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2237 gtk_widget_set_name (w, MENU_ITEM_NAME);
2240 return wmenu;
2243 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2244 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2245 with some text and buttons.
2246 F is the frame the created item belongs to.
2247 NAME is the name to use for the top widget.
2248 VAL is a widget_value structure describing items to be created.
2249 SELECT_CB is the callback to use when a menu item is selected or
2250 a dialog button is pressed.
2251 DEACTIVATE_CB is the callback to use when an item is deactivated.
2252 For a menu, when a sub menu is not shown anymore, for a dialog it is
2253 called when the dialog is popped down.
2254 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2256 Returns the widget created. */
2258 GtkWidget *
2259 xg_create_widget (type, name, f, val,
2260 select_cb, deactivate_cb, highlight_cb)
2261 char *type;
2262 char *name;
2263 FRAME_PTR f;
2264 widget_value *val;
2265 GCallback select_cb;
2266 GCallback deactivate_cb;
2267 GCallback highlight_cb;
2269 GtkWidget *w = 0;
2270 int menu_bar_p = strcmp (type, "menubar") == 0;
2271 int pop_up_p = strcmp (type, "popup") == 0;
2273 if (strcmp (type, "dialog") == 0)
2275 w = create_dialog (val, select_cb, deactivate_cb);
2276 xg_set_screen (w, f);
2277 gtk_window_set_transient_for (GTK_WINDOW (w),
2278 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2279 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2280 gtk_widget_set_name (w, "emacs-dialog");
2281 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2283 else if (menu_bar_p || pop_up_p)
2285 w = create_menus (val->contents,
2287 select_cb,
2288 deactivate_cb,
2289 highlight_cb,
2290 pop_up_p,
2291 menu_bar_p,
2292 menu_bar_p,
2295 name);
2297 /* Set the cursor to an arrow for popup menus when they are mapped.
2298 This is done by default for menu bar menus. */
2299 if (pop_up_p)
2301 /* Must realize so the GdkWindow inside the widget is created. */
2302 gtk_widget_realize (w);
2303 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2306 else
2308 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2309 type);
2312 return w;
2315 /* Return the label for menu item WITEM. */
2317 static const char *
2318 xg_get_menu_item_label (witem)
2319 GtkMenuItem *witem;
2321 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2322 return gtk_label_get_label (wlabel);
2325 /* Return non-zero if the menu item WITEM has the text LABEL. */
2327 static int
2328 xg_item_label_same_p (witem, label)
2329 GtkMenuItem *witem;
2330 char *label;
2332 int is_same = 0;
2333 char *utf8_label = get_utf8_string (label);
2334 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2336 if (! old_label && ! utf8_label)
2337 is_same = 1;
2338 else if (old_label && utf8_label)
2339 is_same = strcmp (utf8_label, old_label) == 0;
2341 if (utf8_label && utf8_label != label) g_free (utf8_label);
2343 return is_same;
2346 /* Destroy widgets in LIST. */
2348 static void
2349 xg_destroy_widgets (list)
2350 GList *list;
2352 GList *iter;
2354 for (iter = list; iter; iter = g_list_next (iter))
2356 GtkWidget *w = GTK_WIDGET (iter->data);
2358 /* Destroying the widget will remove it from the container it is in. */
2359 gtk_widget_destroy (w);
2363 /* Update the top level names in MENUBAR (i.e. not submenus).
2364 F is the frame the menu bar belongs to.
2365 *LIST is a list with the current menu bar names (menu item widgets).
2366 ITER is the item within *LIST that shall be updated.
2367 POS is the numerical position, starting at 0, of ITER in *LIST.
2368 VAL describes what the menu bar shall look like after the update.
2369 SELECT_CB is the callback to use when a menu item is selected.
2370 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2371 CL_DATA points to the callback data to be used for this menu bar.
2373 This function calls itself to walk through the menu bar names. */
2375 static void
2376 xg_update_menubar (menubar, f, list, iter, pos, val,
2377 select_cb, deactivate_cb, highlight_cb, cl_data)
2378 GtkWidget *menubar;
2379 FRAME_PTR f;
2380 GList **list;
2381 GList *iter;
2382 int pos;
2383 widget_value *val;
2384 GCallback select_cb;
2385 GCallback deactivate_cb;
2386 GCallback highlight_cb;
2387 xg_menu_cb_data *cl_data;
2389 if (! iter && ! val)
2390 return;
2391 else if (iter && ! val)
2393 /* Item(s) have been removed. Remove all remaining items. */
2394 xg_destroy_widgets (iter);
2396 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2397 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2398 gtk_menu_item_new_with_label (""),
2400 /* All updated. */
2401 val = 0;
2402 iter = 0;
2404 else if (! iter && val)
2406 /* Item(s) added. Add all new items in one call. */
2407 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2408 0, 1, 0, menubar, cl_data, 0);
2410 /* All updated. */
2411 val = 0;
2412 iter = 0;
2414 /* Below this neither iter or val is NULL */
2415 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2417 /* This item is still the same, check next item. */
2418 val = val->next;
2419 iter = g_list_next (iter);
2420 ++pos;
2422 else /* This item is changed. */
2424 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2425 GtkMenuItem *witem2 = 0;
2426 int val_in_menubar = 0;
2427 int iter_in_new_menubar = 0;
2428 GList *iter2;
2429 widget_value *cur;
2431 /* See if the changed entry (val) is present later in the menu bar */
2432 for (iter2 = iter;
2433 iter2 && ! val_in_menubar;
2434 iter2 = g_list_next (iter2))
2436 witem2 = GTK_MENU_ITEM (iter2->data);
2437 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2440 /* See if the current entry (iter) is present later in the
2441 specification for the new menu bar. */
2442 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2443 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2445 if (val_in_menubar && ! iter_in_new_menubar)
2447 int nr = pos;
2449 /* This corresponds to:
2450 Current: A B C
2451 New: A C
2452 Remove B. */
2454 gtk_widget_ref (GTK_WIDGET (witem));
2455 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2456 gtk_widget_destroy (GTK_WIDGET (witem));
2458 /* Must get new list since the old changed. */
2459 g_list_free (*list);
2460 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2461 while (nr-- > 0) iter = g_list_next (iter);
2463 else if (! val_in_menubar && ! iter_in_new_menubar)
2465 /* This corresponds to:
2466 Current: A B C
2467 New: A X C
2468 Rename B to X. This might seem to be a strange thing to do,
2469 since if there is a menu under B it will be totally wrong for X.
2470 But consider editing a C file. Then there is a C-mode menu
2471 (corresponds to B above).
2472 If then doing C-x C-f the minibuf menu (X above) replaces the
2473 C-mode menu. When returning from the minibuffer, we get
2474 back the C-mode menu. Thus we do:
2475 Rename B to X (C-mode to minibuf menu)
2476 Rename X to B (minibuf to C-mode menu).
2477 If the X menu hasn't been invoked, the menu under B
2478 is up to date when leaving the minibuffer. */
2479 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2480 char *utf8_label = get_utf8_string (val->name);
2481 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2483 gtk_label_set_text (wlabel, utf8_label);
2485 /* If this item has a submenu that has been detached, change
2486 the title in the WM decorations also. */
2487 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2488 /* Set the title of the detached window. */
2489 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2491 iter = g_list_next (iter);
2492 val = val->next;
2493 ++pos;
2495 else if (! val_in_menubar && iter_in_new_menubar)
2497 /* This corresponds to:
2498 Current: A B C
2499 New: A X B C
2500 Insert X. */
2502 int nr = pos;
2503 GList *group = 0;
2504 GtkWidget *w = xg_create_one_menuitem (val,
2506 select_cb,
2507 highlight_cb,
2508 cl_data,
2509 &group);
2511 /* Create a possibly empty submenu for menu bar items, since some
2512 themes don't highlight items correctly without it. */
2513 GtkWidget *submenu = create_menus (NULL, f,
2514 select_cb, deactivate_cb,
2515 highlight_cb,
2516 0, 0, 0, 0, cl_data, 0);
2517 gtk_widget_set_name (w, MENU_ITEM_NAME);
2518 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2519 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2521 g_list_free (*list);
2522 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2523 while (nr-- > 0) iter = g_list_next (iter);
2524 iter = g_list_next (iter);
2525 val = val->next;
2526 ++pos;
2528 else /* if (val_in_menubar && iter_in_new_menubar) */
2530 int nr = pos;
2531 /* This corresponds to:
2532 Current: A B C
2533 New: A C B
2534 Move C before B */
2536 gtk_widget_ref (GTK_WIDGET (witem2));
2537 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2538 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2539 GTK_WIDGET (witem2), pos);
2540 gtk_widget_unref (GTK_WIDGET (witem2));
2542 g_list_free (*list);
2543 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2544 while (nr-- > 0) iter = g_list_next (iter);
2545 if (iter) iter = g_list_next (iter);
2546 val = val->next;
2547 ++pos;
2551 /* Update the rest of the menu bar. */
2552 xg_update_menubar (menubar, f, list, iter, pos, val,
2553 select_cb, deactivate_cb, highlight_cb, cl_data);
2556 /* Update the menu item W so it corresponds to VAL.
2557 SELECT_CB is the callback to use when a menu item is selected.
2558 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2559 CL_DATA is the data to set in the widget for menu invocation. */
2561 static void
2562 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2563 widget_value *val;
2564 GtkWidget *w;
2565 GCallback select_cb;
2566 GCallback highlight_cb;
2567 xg_menu_cb_data *cl_data;
2569 GtkWidget *wchild;
2570 GtkLabel *wlbl = 0;
2571 GtkLabel *wkey = 0;
2572 char *utf8_label;
2573 char *utf8_key;
2574 const char *old_label = 0;
2575 const char *old_key = 0;
2576 xg_menu_item_cb_data *cb_data;
2578 wchild = gtk_bin_get_child (GTK_BIN (w));
2579 utf8_label = get_utf8_string (val->name);
2580 utf8_key = get_utf8_string (val->key);
2582 /* See if W is a menu item with a key. See make_menu_item above. */
2583 if (GTK_IS_HBOX (wchild))
2585 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2587 wlbl = GTK_LABEL (list->data);
2588 wkey = GTK_LABEL (list->next->data);
2589 g_list_free (list);
2591 if (! utf8_key)
2593 /* Remove the key and keep just the label. */
2594 gtk_widget_ref (GTK_WIDGET (wlbl));
2595 gtk_container_remove (GTK_CONTAINER (w), wchild);
2596 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2597 wkey = 0;
2601 else /* Just a label. */
2603 wlbl = GTK_LABEL (wchild);
2605 /* Check if there is now a key. */
2606 if (utf8_key)
2608 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2609 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2611 wlbl = GTK_LABEL (list->data);
2612 wkey = GTK_LABEL (list->next->data);
2613 g_list_free (list);
2615 gtk_container_remove (GTK_CONTAINER (w), wchild);
2616 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2621 if (wkey) old_key = gtk_label_get_label (wkey);
2622 if (wlbl) old_label = gtk_label_get_label (wlbl);
2624 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2625 gtk_label_set_text (wkey, utf8_key);
2627 if (! old_label || strcmp (utf8_label, old_label) != 0)
2628 gtk_label_set_text (wlbl, utf8_label);
2630 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2631 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2633 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2634 gtk_widget_set_sensitive (w, FALSE);
2635 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2636 gtk_widget_set_sensitive (w, TRUE);
2638 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2639 XG_ITEM_DATA);
2640 if (cb_data)
2642 cb_data->call_data = val->call_data;
2643 cb_data->help = val->help;
2644 cb_data->cl_data = cl_data;
2646 /* We assume the callback functions don't change. */
2647 if (val->call_data && ! val->contents)
2649 /* This item shall have a select callback. */
2650 if (! cb_data->select_id)
2651 cb_data->select_id
2652 = g_signal_connect (G_OBJECT (w), "activate",
2653 select_cb, cb_data);
2655 else if (cb_data->select_id)
2657 g_signal_handler_disconnect (w, cb_data->select_id);
2658 cb_data->select_id = 0;
2663 /* Update the toggle menu item W so it corresponds to VAL. */
2665 static void
2666 xg_update_toggle_item (val, w)
2667 widget_value *val;
2668 GtkWidget *w;
2670 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2673 /* Update the radio menu item W so it corresponds to VAL. */
2675 static void
2676 xg_update_radio_item (val, w)
2677 widget_value *val;
2678 GtkWidget *w;
2680 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2683 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2684 SUBMENU may be NULL, in that case a new menu is created.
2685 F is the frame the menu bar belongs to.
2686 VAL describes the contents of the menu bar.
2687 SELECT_CB is the callback to use when a menu item is selected.
2688 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2689 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2690 CL_DATA is the call back data to use for any newly created items.
2692 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2693 was NULL. */
2695 static GtkWidget *
2696 xg_update_submenu (submenu, f, val,
2697 select_cb, deactivate_cb, highlight_cb, cl_data)
2698 GtkWidget *submenu;
2699 FRAME_PTR f;
2700 widget_value *val;
2701 GCallback select_cb;
2702 GCallback deactivate_cb;
2703 GCallback highlight_cb;
2704 xg_menu_cb_data *cl_data;
2706 GtkWidget *newsub = submenu;
2707 GList *list = 0;
2708 GList *iter;
2709 widget_value *cur;
2710 int has_tearoff_p = 0;
2711 GList *first_radio = 0;
2713 if (submenu)
2714 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2716 for (cur = val, iter = list;
2717 cur && iter;
2718 iter = g_list_next (iter), cur = cur->next)
2720 GtkWidget *w = GTK_WIDGET (iter->data);
2722 /* Skip tearoff items, they have no counterpart in val. */
2723 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2725 has_tearoff_p = 1;
2726 iter = g_list_next (iter);
2727 if (iter) w = GTK_WIDGET (iter->data);
2728 else break;
2731 /* Remember first radio button in a group. If we get a mismatch in
2732 a radio group we must rebuild the whole group so that the connections
2733 in GTK becomes correct. */
2734 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2735 first_radio = iter;
2736 else if (cur->button_type != BUTTON_TYPE_RADIO
2737 && ! GTK_IS_RADIO_MENU_ITEM (w))
2738 first_radio = 0;
2740 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2742 if (! xg_separator_p (cur->name))
2743 break;
2745 else if (GTK_IS_CHECK_MENU_ITEM (w))
2747 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2748 break;
2749 xg_update_toggle_item (cur, w);
2750 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2752 else if (GTK_IS_RADIO_MENU_ITEM (w))
2754 if (cur->button_type != BUTTON_TYPE_RADIO)
2755 break;
2756 xg_update_radio_item (cur, w);
2757 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2759 else if (GTK_IS_MENU_ITEM (w))
2761 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2762 GtkWidget *sub;
2764 if (cur->button_type != BUTTON_TYPE_NONE ||
2765 xg_separator_p (cur->name))
2766 break;
2768 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2770 sub = gtk_menu_item_get_submenu (witem);
2771 if (sub && ! cur->contents)
2773 /* Not a submenu anymore. */
2774 gtk_widget_ref (sub);
2775 gtk_menu_item_remove_submenu (witem);
2776 gtk_widget_destroy (sub);
2778 else if (cur->contents)
2780 GtkWidget *nsub;
2782 nsub = xg_update_submenu (sub, f, cur->contents,
2783 select_cb, deactivate_cb,
2784 highlight_cb, cl_data);
2786 /* If this item just became a submenu, we must set it. */
2787 if (nsub != sub)
2788 gtk_menu_item_set_submenu (witem, nsub);
2791 else
2793 /* Structural difference. Remove everything from here and down
2794 in SUBMENU. */
2795 break;
2799 /* Remove widgets from first structual change. */
2800 if (iter)
2802 /* If we are adding new menu items below, we must remove from
2803 first radio button so that radio groups become correct. */
2804 if (cur && first_radio) xg_destroy_widgets (first_radio);
2805 else xg_destroy_widgets (iter);
2808 if (cur)
2810 /* More items added. Create them. */
2811 newsub = create_menus (cur,
2813 select_cb,
2814 deactivate_cb,
2815 highlight_cb,
2818 ! has_tearoff_p,
2819 submenu,
2820 cl_data,
2824 if (list) g_list_free (list);
2826 return newsub;
2829 /* Update the MENUBAR.
2830 F is the frame the menu bar belongs to.
2831 VAL describes the contents of the menu bar.
2832 If DEEP_P is non-zero, rebuild all but the top level menu names in
2833 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2834 SELECT_CB is the callback to use when a menu item is selected.
2835 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2836 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2838 void
2839 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2840 select_cb, deactivate_cb, highlight_cb)
2841 GtkWidget *menubar;
2842 FRAME_PTR f;
2843 widget_value *val;
2844 int deep_p;
2845 GCallback select_cb;
2846 GCallback deactivate_cb;
2847 GCallback highlight_cb;
2849 xg_menu_cb_data *cl_data;
2850 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2852 if (! list) return;
2854 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2855 XG_FRAME_DATA);
2857 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2858 select_cb, deactivate_cb, highlight_cb, cl_data);
2860 if (deep_p)
2862 widget_value *cur;
2864 /* Update all sub menus.
2865 We must keep the submenus (GTK menu item widgets) since the
2866 X Window in the XEvent that activates the menu are those widgets. */
2868 /* Update cl_data, menu_item things in F may have changed. */
2869 update_cl_data (cl_data, f, highlight_cb);
2871 for (cur = val->contents; cur; cur = cur->next)
2873 GList *iter;
2874 GtkWidget *sub = 0;
2875 GtkWidget *newsub;
2876 GtkMenuItem *witem;
2878 /* Find sub menu that corresponds to val and update it. */
2879 for (iter = list ; iter; iter = g_list_next (iter))
2881 witem = GTK_MENU_ITEM (iter->data);
2882 if (xg_item_label_same_p (witem, cur->name))
2884 sub = gtk_menu_item_get_submenu (witem);
2885 break;
2889 newsub = xg_update_submenu (sub,
2891 cur->contents,
2892 select_cb,
2893 deactivate_cb,
2894 highlight_cb,
2895 cl_data);
2896 /* sub may still be NULL. If we just updated non deep and added
2897 a new menu bar item, it has no sub menu yet. So we set the
2898 newly created sub menu under witem. */
2899 if (newsub != sub)
2901 xg_set_screen (newsub, f);
2902 gtk_menu_item_set_submenu (witem, newsub);
2907 g_list_free (list);
2908 gtk_widget_show_all (menubar);
2911 /* Recompute all the widgets of frame F, when the menu bar has been
2912 changed. Value is non-zero if widgets were updated. */
2915 xg_update_frame_menubar (f)
2916 FRAME_PTR f;
2918 struct x_output *x = f->output_data.x;
2919 GtkRequisition req;
2921 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2922 return 0;
2924 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
2925 return 0; /* Already done this, happens for frames created invisible. */
2927 BLOCK_INPUT;
2929 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2930 FALSE, FALSE, 0);
2931 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2933 gtk_widget_show_all (x->menubar_widget);
2934 gtk_widget_size_request (x->menubar_widget, &req);
2935 FRAME_MENUBAR_HEIGHT (f) = req.height;
2936 xg_height_changed (f);
2937 UNBLOCK_INPUT;
2939 return 1;
2942 /* Get rid of the menu bar of frame F, and free its storage.
2943 This is used when deleting a frame, and when turning off the menu bar. */
2945 void
2946 free_frame_menubar (f)
2947 FRAME_PTR f;
2949 struct x_output *x = f->output_data.x;
2951 if (x->menubar_widget)
2953 BLOCK_INPUT;
2955 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2956 /* The menubar and its children shall be deleted when removed from
2957 the container. */
2958 x->menubar_widget = 0;
2959 FRAME_MENUBAR_HEIGHT (f) = 0;
2960 xg_height_changed (f);
2961 UNBLOCK_INPUT;
2967 /***********************************************************************
2968 Scroll bar functions
2969 ***********************************************************************/
2972 /* Setting scroll bar values invokes the callback. Use this variable
2973 to indicate that callback should do nothing. */
2975 int xg_ignore_gtk_scrollbar;
2977 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
2978 may be larger than 32 bits. Keep a mapping from integer index to widget
2979 pointers to get around the 32 bit limitation. */
2981 static struct
2983 GtkWidget **widgets;
2984 int max_size;
2985 int used;
2986 } id_to_widget;
2988 /* Grow this much every time we need to allocate more */
2990 #define ID_TO_WIDGET_INCR 32
2992 /* Store the widget pointer W in id_to_widget and return the integer index. */
2994 static int
2995 xg_store_widget_in_map (w)
2996 GtkWidget *w;
2998 int i;
3000 if (id_to_widget.max_size == id_to_widget.used)
3002 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3004 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3005 sizeof (GtkWidget *)*new_size);
3007 for (i = id_to_widget.max_size; i < new_size; ++i)
3008 id_to_widget.widgets[i] = 0;
3009 id_to_widget.max_size = new_size;
3012 /* Just loop over the array and find a free place. After all,
3013 how many scroll bars are we creating? Should be a small number.
3014 The check above guarantees we will find a free place. */
3015 for (i = 0; i < id_to_widget.max_size; ++i)
3017 if (! id_to_widget.widgets[i])
3019 id_to_widget.widgets[i] = w;
3020 ++id_to_widget.used;
3022 return i;
3026 /* Should never end up here */
3027 abort ();
3030 /* Remove pointer at IDX from id_to_widget.
3031 Called when scroll bar is destroyed. */
3033 static void
3034 xg_remove_widget_from_map (idx)
3035 int idx;
3037 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3039 id_to_widget.widgets[idx] = 0;
3040 --id_to_widget.used;
3044 /* Get the widget pointer at IDX from id_to_widget. */
3046 static GtkWidget *
3047 xg_get_widget_from_map (idx)
3048 int idx;
3050 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3051 return id_to_widget.widgets[idx];
3053 return 0;
3056 /* Return the scrollbar id for X Window WID on display DPY.
3057 Return -1 if WID not in id_to_widget. */
3060 xg_get_scroll_id_for_window (dpy, wid)
3061 Display *dpy;
3062 Window wid;
3064 int idx;
3065 GtkWidget *w;
3067 w = xg_win_to_widget (dpy, wid);
3069 if (w)
3071 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3072 if (id_to_widget.widgets[idx] == w)
3073 return idx;
3076 return -1;
3079 /* Callback invoked when scroll bar WIDGET is destroyed.
3080 DATA is the index into id_to_widget for WIDGET.
3081 We free pointer to last scroll bar values here and remove the index. */
3083 static void
3084 xg_gtk_scroll_destroy (widget, data)
3085 GtkWidget *widget;
3086 gpointer data;
3088 gpointer p;
3089 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3091 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
3092 xfree (p);
3093 xg_remove_widget_from_map (id);
3096 /* Create a scroll bar widget for frame F. Store the scroll bar
3097 in BAR.
3098 SCROLL_CALLBACK is the callback to invoke when the value of the
3099 bar changes.
3100 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3101 to set resources for the widget. */
3103 void
3104 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
3105 FRAME_PTR f;
3106 struct scroll_bar *bar;
3107 GCallback scroll_callback;
3108 char *scroll_bar_name;
3110 GtkWidget *wscroll;
3111 GtkWidget *webox;
3112 GtkObject *vadj;
3113 int scroll_id;
3115 /* Page, step increment values are not so important here, they
3116 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3117 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3118 0.1, 0.1, 0.1);
3120 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3121 webox = gtk_event_box_new ();
3122 gtk_widget_set_name (wscroll, scroll_bar_name);
3123 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3125 scroll_id = xg_store_widget_in_map (wscroll);
3127 g_signal_connect (G_OBJECT (wscroll),
3128 "value-changed",
3129 scroll_callback,
3130 (gpointer) bar);
3131 /* The EMACS_INT cast avoids a warning. */
3132 g_signal_connect (G_OBJECT (wscroll),
3133 "destroy",
3134 G_CALLBACK (xg_gtk_scroll_destroy),
3135 (gpointer) (EMACS_INT) scroll_id);
3137 /* The scroll bar widget does not draw on a window of its own. Instead
3138 it draws on the parent window, in this case the edit widget. So
3139 whenever the edit widget is cleared, the scroll bar needs to redraw
3140 also, which causes flicker. Put an event box between the edit widget
3141 and the scroll bar, so the scroll bar instead draws itself on the
3142 event box window. */
3143 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3144 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3147 /* Set the cursor to an arrow. */
3148 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3150 bar->x_window = scroll_id;
3153 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3155 void
3156 xg_show_scroll_bar (scrollbar_id)
3157 int scrollbar_id;
3159 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3160 if (w)
3161 gtk_widget_show_all (gtk_widget_get_parent (w));
3164 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3166 void
3167 xg_remove_scroll_bar (f, scrollbar_id)
3168 FRAME_PTR f;
3169 int scrollbar_id;
3171 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3172 if (w)
3174 GtkWidget *wparent = gtk_widget_get_parent (w);
3175 gtk_widget_destroy (w);
3176 gtk_widget_destroy (wparent);
3177 SET_FRAME_GARBAGED (f);
3181 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3182 in frame F.
3183 TOP/LEFT are the new pixel positions where the bar shall appear.
3184 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3186 void
3187 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3188 FRAME_PTR f;
3189 int scrollbar_id;
3190 int top;
3191 int left;
3192 int width;
3193 int height;
3196 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3198 if (wscroll)
3200 GtkWidget *wfixed = f->output_data.x->edit_widget;
3201 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3203 /* Move and resize to new values. */
3204 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3205 gtk_widget_set_size_request (wscroll, width, height);
3206 gtk_widget_queue_draw (wparent);
3207 gdk_window_process_all_updates ();
3208 /* GTK does not redraw until the main loop is entered again, but
3209 if there are no X events pending we will not enter it. So we sync
3210 here to get some events. */
3211 x_sync (f);
3212 SET_FRAME_GARBAGED (f);
3213 cancel_mouse_face (f);
3217 /* Set the thumb size and position of scroll bar BAR. We are currently
3218 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3220 void
3221 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3222 struct scroll_bar *bar;
3223 int portion, position, whole;
3225 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3227 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3229 if (wscroll && NILP (bar->dragging))
3231 GtkAdjustment *adj;
3232 gdouble shown;
3233 gdouble top;
3234 int size, value;
3235 int new_step;
3236 int changed = 0;
3238 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3240 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3241 rather than the real portion value. This makes the thumb less likely
3242 to resize and that looks better. */
3243 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3244 /* When the thumb is at the bottom, position == whole.
3245 So we need to increase `whole' to make space for the thumb. */
3246 whole += portion;
3248 if (whole <= 0)
3249 top = 0, shown = 1;
3250 else
3252 top = (gdouble) position / whole;
3253 shown = (gdouble) portion / whole;
3256 size = shown * XG_SB_RANGE;
3257 size = min (size, XG_SB_RANGE);
3258 size = max (size, 1);
3260 value = top * XG_SB_RANGE;
3261 value = min (value, XG_SB_MAX - size);
3262 value = max (value, XG_SB_MIN);
3264 /* Assume all lines are of equal size. */
3265 new_step = size / max (1, FRAME_LINES (f));
3267 if ((int) adj->page_size != size
3268 || (int) adj->step_increment != new_step)
3270 adj->page_size = size;
3271 adj->step_increment = new_step;
3272 /* Assume a page increment is about 95% of the page size */
3273 adj->page_increment = (int) (0.95*adj->page_size);
3274 changed = 1;
3277 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3279 GtkWidget *wfixed = f->output_data.x->edit_widget;
3281 BLOCK_INPUT;
3283 /* gtk_range_set_value invokes the callback. Set
3284 ignore_gtk_scrollbar to make the callback do nothing */
3285 xg_ignore_gtk_scrollbar = 1;
3287 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3288 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3289 else if (changed)
3290 gtk_adjustment_changed (adj);
3292 xg_ignore_gtk_scrollbar = 0;
3294 UNBLOCK_INPUT;
3299 /* Return non-zero if EVENT is for a scroll bar in frame F.
3300 When the same X window is used for several Gtk+ widgets, we cannot
3301 say for sure based on the X window alone if an event is for the
3302 frame. This function does additional checks.
3304 Return non-zero if the event is for a scroll bar, zero otherwise. */
3307 xg_event_is_for_scrollbar (f, event)
3308 FRAME_PTR f;
3309 XEvent *event;
3311 int retval = 0;
3313 if (f && event->type == ButtonPress)
3315 /* Check if press occurred outside the edit widget. */
3316 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3317 retval = gdk_display_get_window_at_pointer (gdpy, NULL, NULL)
3318 != f->output_data.x->edit_widget->window;
3320 else if (f && (event->type != ButtonRelease || event->type != MotionNotify))
3322 /* If we are releasing or moving the scroll bar, it has the grab. */
3323 retval = gtk_grab_get_current () != 0
3324 && gtk_grab_get_current () != f->output_data.x->edit_widget;
3327 return retval;
3332 /***********************************************************************
3333 Tool bar functions
3334 ***********************************************************************/
3335 /* The key for the data we put in the GtkImage widgets. The data is
3336 the image used by Emacs. We use this to see if we need to update
3337 the GtkImage with a new image. */
3338 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3340 /* The key for storing the latest modifiers so the activate callback can
3341 get them. */
3342 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3344 /* The key for storing the button widget in its proxy menu item. */
3345 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3347 /* The key for the data we put in the GtkImage widgets. The data is
3348 the stock name used by Emacs. We use this to see if we need to update
3349 the GtkImage with a new image. */
3350 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3352 /* As above, but this is used for named theme widgets, as opposed to
3353 stock items. */
3354 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3356 /* Callback function invoked when a tool bar item is pressed.
3357 W is the button widget in the tool bar that got pressed,
3358 CLIENT_DATA is an integer that is the index of the button in the
3359 tool bar. 0 is the first button. */
3361 static gboolean
3362 xg_tool_bar_button_cb (widget, event, user_data)
3363 GtkWidget *widget;
3364 GdkEventButton *event;
3365 gpointer user_data;
3367 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3368 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3369 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3370 return FALSE;
3374 /* Callback function invoked when a tool bar item is pressed.
3375 W is the button widget in the tool bar that got pressed,
3376 CLIENT_DATA is an integer that is the index of the button in the
3377 tool bar. 0 is the first button. */
3379 static void
3380 xg_tool_bar_callback (w, client_data)
3381 GtkWidget *w;
3382 gpointer client_data;
3384 /* The EMACS_INT cast avoids a warning. */
3385 int idx = (int) (EMACS_INT) client_data;
3386 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3387 XG_TOOL_BAR_LAST_MODIFIER);
3389 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3390 Lisp_Object key, frame;
3391 struct input_event event;
3392 EVENT_INIT (event);
3394 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3395 return;
3397 idx *= TOOL_BAR_ITEM_NSLOTS;
3399 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3400 XSETFRAME (frame, f);
3402 /* We generate two events here. The first one is to set the prefix
3403 to `(tool_bar)', see keyboard.c. */
3404 event.kind = TOOL_BAR_EVENT;
3405 event.frame_or_window = frame;
3406 event.arg = frame;
3407 kbd_buffer_store_event (&event);
3409 event.kind = TOOL_BAR_EVENT;
3410 event.frame_or_window = frame;
3411 event.arg = key;
3412 /* Convert between the modifier bits GDK uses and the modifier bits
3413 Emacs uses. This assumes GDK and X masks are the same, which they are when
3414 this is written. */
3415 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3416 kbd_buffer_store_event (&event);
3418 /* Return focus to the frame after we have clicked on a detached
3419 tool bar button. */
3420 Fx_focus_frame (frame);
3423 /* Callback function invoked when a tool bar item is pressed in a detached
3424 tool bar or the overflow drop down menu.
3425 We just call xg_tool_bar_callback.
3426 W is the menu item widget that got pressed,
3427 CLIENT_DATA is an integer that is the index of the button in the
3428 tool bar. 0 is the first button. */
3430 static void
3431 xg_tool_bar_proxy_callback (w, client_data)
3432 GtkWidget *w;
3433 gpointer client_data;
3435 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3436 XG_TOOL_BAR_PROXY_BUTTON));
3437 xg_tool_bar_callback (wbutton, client_data);
3441 static gboolean
3442 xg_tool_bar_help_callback P_ ((GtkWidget *w,
3443 GdkEventCrossing *event,
3444 gpointer client_data));
3446 /* This callback is called when a help is to be shown for an item in
3447 the detached tool bar when the detached tool bar it is not expanded. */
3449 static gboolean
3450 xg_tool_bar_proxy_help_callback (w, event, client_data)
3451 GtkWidget *w;
3452 GdkEventCrossing *event;
3453 gpointer client_data;
3455 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3456 XG_TOOL_BAR_PROXY_BUTTON));
3458 xg_tool_bar_help_callback (wbutton, event, client_data);
3462 /* This callback is called when a tool item should create a proxy item,
3463 such as for the overflow menu. Also called when the tool bar is detached.
3464 If we don't create a proxy menu item, the detached tool bar will be
3465 blank. */
3467 static gboolean
3468 xg_tool_bar_menu_proxy (toolitem, user_data)
3469 GtkToolItem *toolitem;
3470 gpointer user_data;
3472 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3473 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3474 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label ("");
3475 GtkWidget *wmenuimage;
3477 if (gtk_button_get_use_stock (wbutton))
3478 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3479 GTK_ICON_SIZE_MENU);
3480 else
3482 GtkImage *wimage = GTK_IMAGE (gtk_bin_get_child (GTK_BIN (wbutton)));
3483 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3484 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3486 if (store_type == GTK_IMAGE_STOCK)
3488 gchar *stock_id;
3489 gtk_image_get_stock (wimage, &stock_id, NULL);
3490 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3492 else if (store_type == GTK_IMAGE_ICON_SET)
3494 GtkIconSet *icon_set;
3495 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3496 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3497 GTK_ICON_SIZE_MENU);
3499 else if (store_type == GTK_IMAGE_PIXBUF)
3501 gint width, height;
3503 if (settings &&
3504 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3505 &width, &height))
3507 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3509 src_pixbuf = gtk_image_get_pixbuf (wimage);
3510 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3511 GDK_INTERP_BILINEAR);
3513 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3515 else
3517 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3518 abort ();
3521 else if (store_type == GTK_IMAGE_ICON_NAME)
3523 const gchar *icon_name;
3524 GtkIconSize icon_size;
3526 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3527 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3528 GTK_ICON_SIZE_MENU);
3530 else
3532 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3533 abort ();
3536 if (wmenuimage)
3537 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3539 g_signal_connect (G_OBJECT (wmenuitem),
3540 "activate",
3541 G_CALLBACK (xg_tool_bar_proxy_callback),
3542 user_data);
3545 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3546 (gpointer) wbutton);
3547 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3548 gtk_widget_set_sensitive (wmenuitem, GTK_WIDGET_SENSITIVE (wbutton));
3550 /* Use enter/leave notify to show help. We use the events
3551 rather than the GtkButton specific signals "enter" and
3552 "leave", so we can have only one callback. The event
3553 will tell us what kind of event it is. */
3554 g_signal_connect (G_OBJECT (wmenuitem),
3555 "enter-notify-event",
3556 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3557 user_data);
3558 g_signal_connect (G_OBJECT (wmenuitem),
3559 "leave-notify-event",
3560 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3561 user_data);
3563 return TRUE;
3566 /* This callback is called when a tool bar is detached. We must set
3567 the height of the tool bar to zero when this happens so frame sizes
3568 are correctly calculated.
3569 WBOX is the handle box widget that enables detach/attach of the tool bar.
3570 W is the tool bar widget.
3571 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3573 static void
3574 xg_tool_bar_detach_callback (wbox, w, client_data)
3575 GtkHandleBox *wbox;
3576 GtkWidget *w;
3577 gpointer client_data;
3579 FRAME_PTR f = (FRAME_PTR) client_data;
3580 extern int x_gtk_whole_detached_tool_bar;
3582 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3583 NULL);
3585 if (f)
3587 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3589 /* When detaching a tool bar, not everything dissapear. There are
3590 a few pixels left that are used to drop the tool bar back into
3591 place. */
3592 FRAME_TOOLBAR_HEIGHT (f) = 4;
3593 xg_height_changed (f);
3597 /* This callback is called when a tool bar is reattached. We must set
3598 the height of the tool bar when this happens so frame sizes
3599 are correctly calculated.
3600 WBOX is the handle box widget that enables detach/attach of the tool bar.
3601 W is the tool bar widget.
3602 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3604 static void
3605 xg_tool_bar_attach_callback (wbox, w, client_data)
3606 GtkHandleBox *wbox;
3607 GtkWidget *w;
3608 gpointer client_data;
3610 FRAME_PTR f = (FRAME_PTR) client_data;
3611 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3613 if (f)
3615 GtkRequisition req;
3617 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3619 gtk_widget_size_request (w, &req);
3620 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3621 xg_height_changed (f);
3625 /* This callback is called when the mouse enters or leaves a tool bar item.
3626 It is used for displaying and hiding the help text.
3627 W is the tool bar item, a button.
3628 EVENT is either an enter event or leave event.
3629 CLIENT_DATA is an integer that is the index of the button in the
3630 tool bar. 0 is the first button.
3632 Returns FALSE to tell GTK to keep processing this event. */
3634 static gboolean
3635 xg_tool_bar_help_callback (w, event, client_data)
3636 GtkWidget *w;
3637 GdkEventCrossing *event;
3638 gpointer client_data;
3640 /* The EMACS_INT cast avoids a warning. */
3641 int idx = (int) (EMACS_INT) client_data;
3642 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3643 Lisp_Object help, frame;
3645 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3646 return FALSE;
3648 if (event->type == GDK_ENTER_NOTIFY)
3650 idx *= TOOL_BAR_ITEM_NSLOTS;
3651 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3653 if (NILP (help))
3654 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3656 else
3657 help = Qnil;
3659 XSETFRAME (frame, f);
3660 kbd_buffer_store_help_event (frame, help);
3662 return FALSE;
3666 /* This callback is called when a tool bar item shall be redrawn.
3667 It modifies the expose event so that the GtkImage widget redraws the
3668 whole image. This to overcome a bug that makes GtkImage draw the image
3669 in the wrong place when it tries to redraw just a part of the image.
3670 W is the GtkImage to be redrawn.
3671 EVENT is the expose event for W.
3672 CLIENT_DATA is unused.
3674 Returns FALSE to tell GTK to keep processing this event. */
3676 static gboolean
3677 xg_tool_bar_item_expose_callback (w, event, client_data)
3678 GtkWidget *w;
3679 GdkEventExpose *event;
3680 gpointer client_data;
3682 gint width, height;
3684 gdk_drawable_get_size (event->window, &width, &height);
3686 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3687 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3689 event->area.x = max (0, event->area.x);
3690 event->area.y = max (0, event->area.y);
3692 event->area.width = max (width, event->area.width);
3693 event->area.height = max (height, event->area.height);
3695 return FALSE;
3698 /* Attach a tool bar to frame F. */
3700 static void
3701 xg_pack_tool_bar (f)
3702 FRAME_PTR f;
3704 struct x_output *x = f->output_data.x;
3705 int vbox_pos = x->menubar_widget ? 1 : 0;
3707 x->handlebox_widget = gtk_handle_box_new ();
3708 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3709 G_CALLBACK (xg_tool_bar_detach_callback), f);
3710 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3711 G_CALLBACK (xg_tool_bar_attach_callback), f);
3713 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3714 x->toolbar_widget);
3716 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3717 FALSE, FALSE, 0);
3719 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3720 vbox_pos);
3721 gtk_widget_show_all (x->handlebox_widget);
3724 /* Create a tool bar for frame F. */
3726 static void
3727 xg_create_tool_bar (f)
3728 FRAME_PTR f;
3730 struct x_output *x = f->output_data.x;
3731 GtkRequisition req;
3733 x->toolbar_widget = gtk_toolbar_new ();
3734 x->toolbar_detached = 0;
3736 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3738 /* We only have icons, so override any user setting. We could
3739 use the caption property of the toolbar item (see update_frame_tool_bar
3740 below), but some of those strings are long, making the toolbar so
3741 long it does not fit on the screen. The GtkToolbar widget makes every
3742 item equal size, so the longest caption determine the size of every
3743 tool bar item. I think the creators of the GtkToolbar widget
3744 counted on 4 or 5 character long strings. */
3745 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3746 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3747 GTK_ORIENTATION_HORIZONTAL);
3751 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3753 /* Find the right-to-left image named by RTL in the tool bar images for F.
3754 Returns IMAGE if RTL is not found. */
3756 static Lisp_Object
3757 find_rtl_image (f, image, rtl)
3758 FRAME_PTR f;
3759 Lisp_Object image;
3760 Lisp_Object rtl;
3762 int i;
3763 Lisp_Object file, rtl_name;
3764 struct gcpro gcpro1, gcpro2;
3765 GCPRO2 (file, rtl_name);
3767 rtl_name = Ffile_name_nondirectory (rtl);
3769 for (i = 0; i < f->n_tool_bar_items; ++i)
3771 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3772 if (!NILP (file = file_for_image (rtl_image)))
3774 file = call1 (intern ("file-name-sans-extension"),
3775 Ffile_name_nondirectory (file));
3776 if (EQ (Fequal (file, rtl_name), Qt))
3778 image = rtl_image;
3779 break;
3784 return image;
3787 /* Update the tool bar for frame F. Add new buttons and remove old. */
3789 extern Lisp_Object Qx_gtk_map_stock;
3791 void
3792 update_frame_tool_bar (f)
3793 FRAME_PTR f;
3795 int i;
3796 GtkRequisition old_req, new_req;
3797 struct x_output *x = f->output_data.x;
3798 int hmargin = 0, vmargin = 0;
3799 GtkToolbar *wtoolbar;
3800 GtkToolItem *ti;
3801 GtkTextDirection dir;
3802 int pack_tool_bar = x->handlebox_widget == NULL;
3804 if (! FRAME_GTK_WIDGET (f))
3805 return;
3807 BLOCK_INPUT;
3809 if (INTEGERP (Vtool_bar_button_margin)
3810 && XINT (Vtool_bar_button_margin) > 0)
3812 hmargin = XFASTINT (Vtool_bar_button_margin);
3813 vmargin = XFASTINT (Vtool_bar_button_margin);
3815 else if (CONSP (Vtool_bar_button_margin))
3817 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3818 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3819 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3821 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3822 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3823 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3826 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3827 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3828 i.e. zero. This means that margins less than
3829 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3830 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3831 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3833 if (! x->toolbar_widget)
3834 xg_create_tool_bar (f);
3836 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
3837 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &old_req);
3838 dir = gtk_widget_get_direction (x->toolbar_widget);
3840 for (i = 0; i < f->n_tool_bar_items; ++i)
3842 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3843 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3844 int idx;
3845 int img_id;
3846 int icon_size = 0;
3847 struct image *img = NULL;
3848 Lisp_Object image;
3849 Lisp_Object stock = Qnil;
3850 GtkStockItem stock_item;
3851 char *stock_name = NULL;
3852 char *icon_name = NULL;
3853 Lisp_Object rtl;
3854 GtkWidget *wbutton = NULL;
3855 GtkWidget *weventbox;
3856 Lisp_Object specified_file;
3858 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
3860 if (ti)
3862 weventbox = gtk_bin_get_child (GTK_BIN (ti));
3863 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3866 image = PROP (TOOL_BAR_ITEM_IMAGES);
3868 /* Ignore invalid image specifications. */
3869 if (!valid_image_p (image))
3871 if (wbutton) gtk_widget_hide (wbutton);
3872 continue;
3875 specified_file = file_for_image (image);
3876 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
3877 stock = call1 (Qx_gtk_map_stock, specified_file);
3879 if (STRINGP (stock))
3881 stock_name = SSDATA (stock);
3882 if (stock_name[0] == 'n' && stock_name[1] == ':')
3884 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
3885 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
3887 icon_name = stock_name + 2;
3888 stock_name = NULL;
3889 stock = Qnil;
3891 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
3892 icon_name = NULL;
3893 else
3894 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3896 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
3897 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3898 else
3900 stock = Qnil;
3901 stock_name = NULL;
3905 if (stock_name == NULL && icon_name == NULL)
3907 /* No stock image, or stock item not known. Try regular image. */
3909 /* If image is a vector, choose the image according to the
3910 button state. */
3911 if (dir == GTK_TEXT_DIR_RTL
3912 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
3913 && STRINGP (rtl))
3915 image = find_rtl_image (f, image, rtl);
3918 if (VECTORP (image))
3920 if (enabled_p)
3921 idx = (selected_p
3922 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3923 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3924 else
3925 idx = (selected_p
3926 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3927 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3929 xassert (ASIZE (image) >= idx);
3930 image = AREF (image, idx);
3932 else
3933 idx = -1;
3935 img_id = lookup_image (f, image);
3936 img = IMAGE_FROM_ID (f, img_id);
3937 prepare_image_for_display (f, img);
3939 if (img->load_failed_p || img->pixmap == None)
3941 if (ti)
3942 gtk_widget_hide_all (GTK_WIDGET (ti));
3943 else
3945 /* Insert an empty (non-image) button */
3946 weventbox = gtk_event_box_new ();
3947 wbutton = gtk_button_new ();
3948 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3949 gtk_button_set_relief (GTK_BUTTON (wbutton),
3950 GTK_RELIEF_NONE);
3951 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3952 ti = gtk_tool_item_new ();
3953 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3954 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3956 continue;
3960 if (ti == NULL)
3962 GtkWidget *w;
3963 if (stock_name)
3965 w = gtk_image_new_from_stock (stock_name, icon_size);
3966 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
3967 (gpointer) xstrdup (stock_name),
3968 (GDestroyNotify) xfree);
3970 else if (icon_name)
3972 w = gtk_image_new_from_icon_name (icon_name, icon_size);
3973 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
3974 (gpointer) xstrdup (icon_name),
3975 (GDestroyNotify) xfree);
3977 else
3979 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3980 /* Save the image so we can see if an update is needed when
3981 this function is called again. */
3982 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3983 (gpointer)img->pixmap);
3986 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3987 wbutton = gtk_button_new ();
3988 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3989 gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
3990 gtk_container_add (GTK_CONTAINER (wbutton), w);
3991 weventbox = gtk_event_box_new ();
3992 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3993 ti = gtk_tool_item_new ();
3994 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3995 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3998 /* The EMACS_INT cast avoids a warning. */
3999 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4000 G_CALLBACK (xg_tool_bar_menu_proxy),
4001 (gpointer) (EMACS_INT) i);
4003 g_signal_connect (G_OBJECT (wbutton), "clicked",
4004 G_CALLBACK (xg_tool_bar_callback),
4005 (gpointer) (EMACS_INT) i);
4007 gtk_widget_show_all (GTK_WIDGET (ti));
4010 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4012 /* Catch expose events to overcome an annoying redraw bug, see
4013 comment for xg_tool_bar_item_expose_callback. */
4014 g_signal_connect (G_OBJECT (ti),
4015 "expose-event",
4016 G_CALLBACK (xg_tool_bar_item_expose_callback),
4019 gtk_widget_set_sensitive (wbutton, enabled_p);
4020 gtk_tool_item_set_homogeneous (ti, FALSE);
4022 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4023 no distinction based on modifiers in the activate callback,
4024 so we have to do it ourselves. */
4025 g_signal_connect (wbutton, "button-release-event",
4026 G_CALLBACK (xg_tool_bar_button_cb),
4027 NULL);
4029 g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
4031 /* Use enter/leave notify to show help. We use the events
4032 rather than the GtkButton specific signals "enter" and
4033 "leave", so we can have only one callback. The event
4034 will tell us what kind of event it is. */
4035 /* The EMACS_INT cast avoids a warning. */
4036 g_signal_connect (G_OBJECT (weventbox),
4037 "enter-notify-event",
4038 G_CALLBACK (xg_tool_bar_help_callback),
4039 (gpointer) (EMACS_INT) i);
4040 g_signal_connect (G_OBJECT (weventbox),
4041 "leave-notify-event",
4042 G_CALLBACK (xg_tool_bar_help_callback),
4043 (gpointer) (EMACS_INT) i);
4045 else
4047 GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
4048 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4049 XG_TOOL_BAR_IMAGE_DATA);
4050 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4051 XG_TOOL_BAR_STOCK_NAME);
4052 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4053 XG_TOOL_BAR_ICON_NAME);
4054 if (stock_name &&
4055 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4057 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4058 stock_name, icon_size);
4059 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4060 (gpointer) xstrdup (stock_name),
4061 (GDestroyNotify) xfree);
4062 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4063 NULL);
4064 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4066 else if (icon_name &&
4067 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4069 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4070 icon_name, icon_size);
4071 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4072 (gpointer) xstrdup (icon_name),
4073 (GDestroyNotify) xfree);
4074 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4075 NULL);
4076 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4077 NULL);
4079 else if (img && old_img != img->pixmap)
4081 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
4082 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4083 (gpointer)img->pixmap);
4085 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4086 NULL);
4087 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4090 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4092 gtk_widget_set_sensitive (wbutton, enabled_p);
4093 gtk_widget_show_all (GTK_WIDGET (ti));
4096 #undef PROP
4099 /* Remove buttons not longer needed. We just hide them so they
4100 can be reused later on. */
4103 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
4104 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4105 } while (ti != NULL);
4107 new_req.height = 0;
4108 if (pack_tool_bar && f->n_tool_bar_items != 0)
4109 xg_pack_tool_bar (f);
4112 gtk_widget_size_request (GTK_WIDGET (x->toolbar_widget), &new_req);
4113 if (old_req.height != new_req.height
4114 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
4116 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
4117 xg_height_changed (f);
4119 UNBLOCK_INPUT;
4122 /* Deallocate all resources for the tool bar on frame F.
4123 Remove the tool bar. */
4125 void
4126 free_frame_tool_bar (f)
4127 FRAME_PTR f;
4129 struct x_output *x = f->output_data.x;
4131 if (x->toolbar_widget)
4133 int is_packed = x->handlebox_widget != 0;
4134 BLOCK_INPUT;
4135 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4136 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4137 if (is_packed)
4138 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4139 x->handlebox_widget);
4140 else
4141 gtk_widget_destroy (x->toolbar_widget);
4143 x->toolbar_widget = 0;
4144 x->handlebox_widget = 0;
4145 FRAME_TOOLBAR_HEIGHT (f) = 0;
4146 xg_height_changed (f);
4148 UNBLOCK_INPUT;
4154 /***********************************************************************
4155 Initializing
4156 ***********************************************************************/
4157 void
4158 xg_initialize ()
4160 GtkBindingSet *binding_set;
4162 #if HAVE_XFT
4163 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4164 we keep it permanently linked in. */
4165 XftInit (0);
4166 #endif
4168 gdpy_def = NULL;
4169 xg_ignore_gtk_scrollbar = 0;
4170 xg_detached_menus = 0;
4171 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4172 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4174 id_to_widget.max_size = id_to_widget.used = 0;
4175 id_to_widget.widgets = 0;
4177 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4178 bindings. It doesn't seem to be any way to remove properties,
4179 so we set it to VoidSymbol which in X means "no key". */
4180 gtk_settings_set_string_property (gtk_settings_get_default (),
4181 "gtk-menu-bar-accel",
4182 "VoidSymbol",
4183 EMACS_CLASS);
4185 /* Make GTK text input widgets use Emacs style keybindings. This is
4186 Emacs after all. */
4187 gtk_settings_set_string_property (gtk_settings_get_default (),
4188 "gtk-key-theme-name",
4189 "Emacs",
4190 EMACS_CLASS);
4192 /* Make dialogs close on C-g. Since file dialog inherits from
4193 dialog, this works for them also. */
4194 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4195 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4196 "close", 0);
4198 /* Make menus close on C-g. */
4199 binding_set = gtk_binding_set_by_class (g_type_class_ref
4200 (GTK_TYPE_MENU_SHELL));
4201 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4202 "cancel", 0);
4205 #endif /* USE_GTK */
4207 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4208 (do not change this comment) */