(vc-git-status-printer): Synchronize with the default.
[emacs.git] / src / gtkutil.c
blob140796a015ef558383578f94a2a8a4a90c43c69f
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 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 "lisp.h"
27 #include "xterm.h"
28 #include "blockinput.h"
29 #include "syssignal.h"
30 #include "window.h"
31 #include "atimer.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 /* Gtk does not work well without any display open. Emacs may close
57 all its displays. In that case, keep a display around just for
58 the purpose of having one. */
60 static GdkDisplay *gdpy_def;
63 /* Return the GdkDisplay that corresponds to the X display DPY. */
65 static GdkDisplay *
66 xg_get_gdk_display (dpy)
67 Display *dpy;
69 return gdk_x11_lookup_xdisplay (dpy);
72 /* When the GTK widget W is to be created on a display for F that
73 is not the default display, set the display for W.
74 W can be a GtkMenu or a GtkWindow widget. */
76 static void
77 xg_set_screen (w, f)
78 GtkWidget *w;
79 FRAME_PTR f;
81 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
83 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
84 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
86 if (GTK_IS_MENU (w))
87 gtk_menu_set_screen (GTK_MENU (w), gscreen);
88 else
89 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
94 #else /* not HAVE_GTK_MULTIDISPLAY */
96 /* Make some defines so we can use the GTK 2.2 functions when
97 compiling with GTK 2.0. */
99 #define xg_set_screen(w, f)
100 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
101 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
102 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
103 #define gdk_x11_lookup_xdisplay(dpy) 0
104 #define GdkDisplay void
106 #endif /* not HAVE_GTK_MULTIDISPLAY */
108 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
109 *DPY is set to NULL if the display can't be opened.
111 Returns non-zero if display could be opened, zero if display could not
112 be opened, and less than zero if the GTK version doesn't support
113 multipe displays. */
116 xg_display_open (display_name, dpy)
117 char *display_name;
118 Display **dpy;
120 #ifdef HAVE_GTK_MULTIDISPLAY
121 GdkDisplay *gdpy;
123 gdpy = gdk_display_open (display_name);
124 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
126 return gdpy != NULL;
128 #else /* not HAVE_GTK_MULTIDISPLAY */
130 return -1;
131 #endif /* not HAVE_GTK_MULTIDISPLAY */
135 /* Close display DPY. */
137 void
138 xg_display_close (Display *dpy)
140 #ifdef HAVE_GTK_MULTIDISPLAY
141 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
143 /* If this is the default display, we must change it before calling
144 dispose, otherwise it will crash on some Gtk+ versions. */
145 if (gdk_display_get_default () == gdpy)
147 struct x_display_info *dpyinfo;
148 Display *new_dpy = 0;
149 GdkDisplay *gdpy_new;
151 /* Find another display. */
152 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
153 if (dpyinfo->display != dpy)
155 new_dpy = dpyinfo->display;
156 break;
159 if (new_dpy)
160 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
161 else
163 if (!gdpy_def)
164 gdpy_def = gdk_display_open (gdk_display_get_name (gdpy));
165 gdpy_new = gdpy_def;
168 gdk_display_manager_set_default_display (gdk_display_manager_get (),
169 gdpy_new);
172 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
173 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
174 we can continue running, but there will be memory leaks. */
176 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
177 g_object_run_dispose (G_OBJECT (gdpy));
178 #else
179 /* This seems to be fixed in GTK 2.10. */
180 gdk_display_close (gdpy);
181 #endif
182 #endif /* HAVE_GTK_MULTIDISPLAY */
186 /***********************************************************************
187 Utility functions
188 ***********************************************************************/
189 /* The timer for scroll bar repetition and menu bar timeouts.
190 NULL if no timer is started. */
191 static struct atimer *xg_timer;
194 /* The next two variables and functions are taken from lwlib. */
195 static widget_value *widget_value_free_list;
196 static int malloc_cpt;
198 /* Allocate a widget_value structure, either by taking one from the
199 widget_value_free_list or by malloc:ing a new one.
201 Return a pointer to the allocated structure. */
203 widget_value *
204 malloc_widget_value ()
206 widget_value *wv;
207 if (widget_value_free_list)
209 wv = widget_value_free_list;
210 widget_value_free_list = wv->free_list;
211 wv->free_list = 0;
213 else
215 wv = (widget_value *) xmalloc (sizeof (widget_value));
216 malloc_cpt++;
218 memset (wv, 0, sizeof (widget_value));
219 return wv;
222 /* This is analogous to free. It frees only what was allocated
223 by malloc_widget_value, and no substructures. */
225 void
226 free_widget_value (wv)
227 widget_value *wv;
229 if (wv->free_list)
230 abort ();
232 if (malloc_cpt > 25)
234 /* When the number of already allocated cells is too big,
235 We free it. */
236 free (wv);
237 malloc_cpt--;
239 else
241 wv->free_list = widget_value_free_list;
242 widget_value_free_list = wv;
247 /* Create and return the cursor to be used for popup menus and
248 scroll bars on display DPY. */
250 GdkCursor *
251 xg_create_default_cursor (dpy)
252 Display *dpy;
254 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
255 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
258 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
260 static GdkPixbuf *
261 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
262 GdkPixmap *gpix;
263 GdkPixmap *gmask;
264 GdkColormap *cmap;
266 int x, y, width, height, rowstride, mask_rowstride;
267 GdkPixbuf *icon_buf, *tmp_buf;
268 guchar *pixels;
269 guchar *mask_pixels;
271 gdk_drawable_get_size (gpix, &width, &height);
272 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
273 0, 0, 0, 0, width, height);
274 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
275 g_object_unref (G_OBJECT (tmp_buf));
277 if (gmask)
279 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
280 gmask,
281 NULL,
282 0, 0, 0, 0,
283 width, height);
284 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
285 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
286 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
287 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
288 int y;
290 for (y = 0; y < height; ++y)
292 guchar *iconptr, *maskptr;
293 int x;
295 iconptr = pixels + y * rowstride;
296 maskptr = mask_pixels + y * mask_rowstride;
298 for (x = 0; x < width; ++x)
300 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
301 just R is sufficient. */
302 if (maskptr[0] == 0)
303 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
305 iconptr += rowstride/width;
306 maskptr += mask_rowstride/width;
310 g_object_unref (G_OBJECT (mask_buf));
313 return icon_buf;
316 static Lisp_Object
317 file_for_image(image)
318 Lisp_Object image;
320 Lisp_Object specified_file = Qnil;
321 Lisp_Object tail;
322 extern Lisp_Object QCfile;
324 for (tail = XCDR (image);
325 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
326 tail = XCDR (XCDR (tail)))
327 if (EQ (XCAR (tail), QCfile))
328 specified_file = XCAR (XCDR (tail));
330 return specified_file;
333 /* For the image defined in IMG, make and return a GtkImage. For displays with
334 8 planes or less we must make a GdkPixbuf and apply the mask manually.
335 Otherwise the highlightning and dimming the tool bar code in GTK does
336 will look bad. For display with more than 8 planes we just use the
337 pixmap and mask directly. For monochrome displays, GTK doesn't seem
338 able to use external pixmaps, it looks bad whatever we do.
339 The image is defined on the display where frame F is.
340 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
341 If OLD_WIDGET is NULL, a new widget is constructed and returned.
342 If OLD_WIDGET is not NULL, that widget is modified. */
344 static GtkWidget *
345 xg_get_image_for_pixmap (f, img, widget, old_widget)
346 FRAME_PTR f;
347 struct image *img;
348 GtkWidget *widget;
349 GtkImage *old_widget;
351 GdkPixmap *gpix;
352 GdkPixmap *gmask;
353 GdkDisplay *gdpy;
354 GdkColormap *cmap;
355 GdkPixbuf *icon_buf;
357 /* If we have a file, let GTK do all the image handling.
358 This seems to be the only way to make insensitive and activated icons
359 look good in all cases. */
360 Lisp_Object specified_file = file_for_image (img->spec);
361 Lisp_Object file;
363 /* We already loaded the image once before calling this
364 function, so this only fails if the image file has been removed.
365 In that case, use the pixmap already loaded. */
367 if (STRINGP (specified_file)
368 && STRINGP (file = x_find_image_file (specified_file)))
370 if (! old_widget)
371 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
372 else
373 gtk_image_set_from_file (old_widget, SSDATA (file));
375 return GTK_WIDGET (old_widget);
378 /* No file, do the image handling ourselves. This will look very bad
379 on a monochrome display, and sometimes bad on all displays with
380 certain themes. */
382 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
383 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
384 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
386 /* This is a workaround to make icons look good on pseudo color
387 displays. Apparently GTK expects the images to have an alpha
388 channel. If they don't, insensitive and activated icons will
389 look bad. This workaround does not work on monochrome displays,
390 and is strictly not needed on true color/static color displays (i.e.
391 16 bits and higher). But we do it anyway so we get a pixbuf that is
392 not associated with the img->pixmap. The img->pixmap may be removed
393 by clearing the image cache and then the tool bar redraw fails, since
394 Gtk+ assumes the pixmap is always there. */
395 cmap = gtk_widget_get_colormap (widget);
396 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
398 if (! old_widget)
399 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
400 else
401 gtk_image_set_from_pixbuf (old_widget, icon_buf);
403 g_object_unref (G_OBJECT (icon_buf));
405 g_object_unref (G_OBJECT (gpix));
406 if (gmask) g_object_unref (G_OBJECT (gmask));
408 return GTK_WIDGET (old_widget);
412 /* Set CURSOR on W and all widgets W contain. We must do like this
413 for scroll bars and menu because they create widgets internally,
414 and it is those widgets that are visible. */
416 static void
417 xg_set_cursor (w, cursor)
418 GtkWidget *w;
419 GdkCursor *cursor;
421 GList *children = gdk_window_peek_children (w->window);
423 gdk_window_set_cursor (w->window, cursor);
425 /* The scroll bar widget has more than one GDK window (had to look at
426 the source to figure this out), and there is no way to set cursor
427 on widgets in GTK. So we must set the cursor for all GDK windows.
428 Ditto for menus. */
430 for ( ; children; children = g_list_next (children))
431 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
434 /* Timer function called when a timeout occurs for xg_timer.
435 This function processes all GTK events in a recursive event loop.
436 This is done because GTK timer events are not seen by Emacs event
437 detection, Emacs only looks for X events. When a scroll bar has the
438 pointer (detected by button press/release events below) an Emacs
439 timer is started, and this function can then check if the GTK timer
440 has expired by calling the GTK event loop.
441 Also, when a menu is active, it has a small timeout before it
442 pops down the sub menu under it. */
444 static void
445 xg_process_timeouts (timer)
446 struct atimer *timer;
448 BLOCK_INPUT;
449 /* Ideally we would like to just handle timer events, like the Xt version
450 of this does in xterm.c, but there is no such feature in GTK. */
451 while (gtk_events_pending ())
452 gtk_main_iteration ();
453 UNBLOCK_INPUT;
456 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
457 xg_process_timeouts is called when the timer expires. The timer
458 started is continuous, i.e. runs until xg_stop_timer is called. */
460 static void
461 xg_start_timer ()
463 if (! xg_timer)
465 EMACS_TIME interval;
466 EMACS_SET_SECS_USECS (interval, 0, 100000);
467 xg_timer = start_atimer (ATIMER_CONTINUOUS,
468 interval,
469 xg_process_timeouts,
474 /* Stop the xg_timer if started. */
476 static void
477 xg_stop_timer ()
479 if (xg_timer)
481 cancel_atimer (xg_timer);
482 xg_timer = 0;
486 /* Insert NODE into linked LIST. */
488 static void
489 xg_list_insert (xg_list_node *list, xg_list_node *node)
491 xg_list_node *list_start = list->next;
493 if (list_start) list_start->prev = node;
494 node->next = list_start;
495 node->prev = 0;
496 list->next = node;
499 /* Remove NODE from linked LIST. */
501 static void
502 xg_list_remove (xg_list_node *list, xg_list_node *node)
504 xg_list_node *list_start = list->next;
505 if (node == list_start)
507 list->next = node->next;
508 if (list->next) list->next->prev = 0;
510 else
512 node->prev->next = node->next;
513 if (node->next) node->next->prev = node->prev;
517 /* Allocate and return a utf8 version of STR. If STR is already
518 utf8 or NULL, just return STR.
519 If not, a new string is allocated and the caller must free the result
520 with g_free. */
522 static char *
523 get_utf8_string (str)
524 char *str;
526 char *utf8_str = str;
528 if (!str) return NULL;
530 /* If not UTF-8, try current locale. */
531 if (!g_utf8_validate (str, -1, NULL))
532 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
534 if (!utf8_str)
536 /* Probably some control characters in str. Escape them. */
537 size_t nr_bad = 0;
538 gsize bytes_read;
539 gsize bytes_written;
540 unsigned char *p = (unsigned char *)str;
541 char *cp, *up;
542 GError *error = NULL;
544 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
545 &bytes_written, &error))
546 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
548 ++nr_bad;
549 p += bytes_written+1;
550 g_error_free (error);
551 error = NULL;
554 if (error)
556 g_error_free (error);
557 error = NULL;
559 if (cp) g_free (cp);
561 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
562 p = (unsigned char *)str;
564 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
565 &bytes_written, &error))
566 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
568 strncpy (up, (char *)p, bytes_written);
569 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
570 up[bytes_written+4] = '\0';
571 up += bytes_written+4;
572 p += bytes_written+1;
573 g_error_free (error);
574 error = NULL;
577 if (cp)
579 strcat (utf8_str, cp);
580 g_free (cp);
582 if (error)
584 g_error_free (error);
585 error = NULL;
588 return utf8_str;
593 /***********************************************************************
594 General functions for creating widgets, resizing, events, e.t.c.
595 ***********************************************************************/
597 /* Make a geometry string and pass that to GTK. It seems this is the
598 only way to get geometry position right if the user explicitly
599 asked for a position when starting Emacs.
600 F is the frame we shall set geometry for. */
602 static void
603 xg_set_geometry (f)
604 FRAME_PTR f;
606 if (f->size_hint_flags & USPosition)
608 int left = f->left_pos;
609 int xneg = f->size_hint_flags & XNegative;
610 int top = f->top_pos;
611 int yneg = f->size_hint_flags & YNegative;
612 char geom_str[32];
614 if (xneg)
615 left = -left;
616 if (yneg)
617 top = -top;
619 sprintf (geom_str, "=%dx%d%c%d%c%d",
620 FRAME_PIXEL_WIDTH (f),
621 FRAME_TOTAL_PIXEL_HEIGHT (f),
622 (xneg ? '-' : '+'), left,
623 (yneg ? '-' : '+'), top);
625 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
626 geom_str))
627 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
629 else if (f->size_hint_flags & PPosition)
630 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
631 f->left_pos, f->top_pos);
635 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
636 and a Gtk+ menu bar, we get resize events for the edit part of the
637 frame only. We let Gtk+ deal with the Gtk+ parts.
638 F is the frame to resize.
639 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
641 void
642 xg_frame_resized (f, pixelwidth, pixelheight)
643 FRAME_PTR f;
644 int pixelwidth, pixelheight;
646 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
647 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
649 if (FRAME_GTK_WIDGET (f)
650 && (columns != FRAME_COLS (f)
651 || rows != FRAME_LINES (f)
652 || pixelwidth != FRAME_PIXEL_WIDTH (f)
653 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
655 FRAME_PIXEL_WIDTH (f) = pixelwidth;
656 FRAME_PIXEL_HEIGHT (f) = pixelheight;
658 if (rows != FRAME_LINES (f) || columns != FRAME_COLS (f)
659 || (f->new_text_lines != 0 && f->new_text_lines != rows)
660 || (f->new_text_cols != 0 && f->new_text_cols != columns))
662 change_frame_size (f, rows, columns, 0, 1, 0);
663 SET_FRAME_GARBAGED (f);
664 cancel_mouse_face (f);
669 /* Process all pending events on the display for frame F. */
671 static void
672 flush_and_sync (f)
673 FRAME_PTR f;
675 gdk_window_process_all_updates ();
676 x_sync (f);
677 while (gtk_events_pending ())
679 gtk_main_iteration ();
680 gdk_window_process_all_updates ();
681 x_sync (f);
685 /* Turn wm hints for resize off on frame F */
687 static void
688 x_wm_size_hint_off (f)
689 FRAME_PTR f;
691 GdkGeometry size_hints;
692 gint hint_flags = 0;
693 memset (&size_hints, 0, sizeof (size_hints));
694 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
695 size_hints.width_inc = 1;
696 size_hints.height_inc = 1;
697 hint_flags |= GDK_HINT_BASE_SIZE;
698 size_hints.base_width = 1;
699 size_hints.base_height = 1;
700 size_hints.min_width = 1;
701 size_hints.min_height = 1;
702 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
703 NULL,
704 &size_hints,
705 hint_flags);
706 /* Make sure these get set again in next call to x_wm_set_size_hint. */
707 f->output_data.x->hint_flags &= ~hint_flags;
708 flush_and_sync (f);
711 /* Resize the outer window of frame F after chainging the height.
712 This happend when the menu bar or the tool bar is added or removed.
713 COLUMNS/ROWS is the size the edit area shall have after the resize. */
715 void
716 xg_frame_set_char_size (f, cols, rows)
717 FRAME_PTR f;
718 int cols;
719 int rows;
721 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
722 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
723 int pixelwidth;
725 if (FRAME_PIXEL_HEIGHT (f) == 0)
726 return;
728 /* Take into account the size of the scroll bar. Always use the
729 number of columns occupied by the scroll bar here otherwise we
730 might end up with a frame width that is not a multiple of the
731 frame's character width which is bad for vertically split
732 windows. */
733 f->scroll_bar_actual_width
734 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
736 compute_fringe_widths (f, 0);
738 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
739 after calculating that value. */
740 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
742 /* Must resize our top level widget. Font size may have changed,
743 but not rows/cols.
744 Turn wm hints (min/max size and size increments) of temporarly.
745 It interferes too much, when for example adding or removing the
746 menu/tool bar. */
747 x_wm_size_hint_off (f);
748 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
749 pixelwidth, pixelheight);
750 flush_and_sync (f);
751 x_wm_set_size_hint (f, 0, 0);
754 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
755 Must be done like this, because GtkWidget:s can have "hidden"
756 X Window that aren't accessible.
758 Return 0 if no widget match WDESC. */
760 GtkWidget *
761 xg_win_to_widget (dpy, wdesc)
762 Display *dpy;
763 Window wdesc;
765 gpointer gdkwin;
766 GtkWidget *gwdesc = 0;
768 BLOCK_INPUT;
770 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
771 wdesc);
772 if (gdkwin)
774 GdkEvent event;
775 event.any.window = gdkwin;
776 gwdesc = gtk_get_event_widget (&event);
779 UNBLOCK_INPUT;
780 return gwdesc;
783 /* Fill in the GdkColor C so that it represents PIXEL.
784 W is the widget that color will be used for. Used to find colormap. */
786 static void
787 xg_pix_to_gcolor (w, pixel, c)
788 GtkWidget *w;
789 unsigned long pixel;
790 GdkColor *c;
792 GdkColormap *map = gtk_widget_get_colormap (w);
793 gdk_colormap_query_color (map, pixel, c);
796 /* Create and set up the GTK widgets for frame F.
797 Return 0 if creation failed, non-zero otherwise. */
800 xg_create_frame_widgets (f)
801 FRAME_PTR f;
803 GtkWidget *wtop;
804 GtkWidget *wvbox;
805 GtkWidget *wfixed;
806 GdkColor bg;
807 GtkRcStyle *style;
808 int i;
809 char *title = 0;
811 BLOCK_INPUT;
813 if (FRAME_X_EMBEDDED_P (f))
814 wtop = gtk_plug_new (f->output_data.x->parent_desc);
815 else
816 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
818 xg_set_screen (wtop, f);
820 wvbox = gtk_vbox_new (FALSE, 0);
821 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
823 if (! wtop || ! wvbox || ! wfixed)
825 if (wtop) gtk_widget_destroy (wtop);
826 if (wvbox) gtk_widget_destroy (wvbox);
827 if (wfixed) gtk_widget_destroy (wfixed);
829 UNBLOCK_INPUT;
830 return 0;
833 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
834 gtk_widget_set_name (wtop, EMACS_CLASS);
835 gtk_widget_set_name (wvbox, "pane");
836 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
838 /* If this frame has a title or name, set it in the title bar. */
839 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
840 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
842 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
844 FRAME_GTK_OUTER_WIDGET (f) = wtop;
845 FRAME_GTK_WIDGET (f) = wfixed;
846 f->output_data.x->vbox_widget = wvbox;
848 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
850 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
851 FRAME_PIXEL_HEIGHT (f));
853 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
854 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
856 if (FRAME_EXTERNAL_TOOL_BAR (f))
857 update_frame_tool_bar (f);
859 /* We don't want this widget double buffered, because we draw on it
860 with regular X drawing primitives, so from a GTK/GDK point of
861 view, the widget is totally blank. When an expose comes, this
862 will make the widget blank, and then Emacs redraws it. This flickers
863 a lot, so we turn off double buffering. */
864 gtk_widget_set_double_buffered (wfixed, FALSE);
866 /* GTK documents says use gtk_window_set_resizable. But then a user
867 can't shrink the window from its starting size. */
868 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
869 gtk_window_set_wmclass (GTK_WINDOW (wtop),
870 SSDATA (Vx_resource_name),
871 SSDATA (Vx_resource_class));
873 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
874 GTK is to destroy the widget. We want Emacs to do that instead. */
875 g_signal_connect (G_OBJECT (wtop), "delete-event",
876 G_CALLBACK (gtk_true), 0);
878 /* Convert our geometry parameters into a geometry string
879 and specify it.
880 GTK will itself handle calculating the real position this way. */
881 xg_set_geometry (f);
883 gtk_widget_add_events (wfixed,
884 GDK_POINTER_MOTION_MASK
885 | GDK_EXPOSURE_MASK
886 | GDK_BUTTON_PRESS_MASK
887 | GDK_BUTTON_RELEASE_MASK
888 | GDK_KEY_PRESS_MASK
889 | GDK_ENTER_NOTIFY_MASK
890 | GDK_LEAVE_NOTIFY_MASK
891 | GDK_FOCUS_CHANGE_MASK
892 | GDK_STRUCTURE_MASK
893 | GDK_VISIBILITY_NOTIFY_MASK);
895 /* Must realize the windows so the X window gets created. It is used
896 by callers of this function. */
897 gtk_widget_realize (wfixed);
898 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
900 /* Since GTK clears its window by filling with the background color,
901 we must keep X and GTK background in sync. */
902 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
903 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
905 /* Also, do not let any background pixmap to be set, this looks very
906 bad as Emacs overwrites the background pixmap with its own idea
907 of background color. */
908 style = gtk_widget_get_modifier_style (wfixed);
910 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
911 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
912 gtk_widget_modify_style (wfixed, style);
914 /* GTK does not set any border, and they look bad with GTK. */
915 /* That they look bad is no excuse for imposing this here. --Stef
916 It should be done by providing the proper default in Fx_create_Frame.
917 f->border_width = 0;
918 f->internal_border_width = 0; */
920 UNBLOCK_INPUT;
922 return 1;
925 /* Set the normal size hints for the window manager, for frame F.
926 FLAGS is the flags word to use--or 0 meaning preserve the flags
927 that the window now has.
928 If USER_POSITION is nonzero, we set the User Position
929 flag (this is useful when FLAGS is 0). */
931 void
932 x_wm_set_size_hint (f, flags, user_position)
933 FRAME_PTR f;
934 long flags;
935 int user_position;
937 if (FRAME_GTK_OUTER_WIDGET (f))
939 /* Must use GTK routines here, otherwise GTK resets the size hints
940 to its own defaults. */
941 GdkGeometry size_hints;
942 gint hint_flags = 0;
943 int base_width, base_height;
944 int min_rows = 0, min_cols = 0;
945 int win_gravity = f->win_gravity;
947 if (flags)
949 memset (&size_hints, 0, sizeof (size_hints));
950 f->output_data.x->size_hints = size_hints;
951 f->output_data.x->hint_flags = hint_flags;
953 else
954 flags = f->size_hint_flags;
956 size_hints = f->output_data.x->size_hints;
957 hint_flags = f->output_data.x->hint_flags;
959 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
960 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
961 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
963 hint_flags |= GDK_HINT_BASE_SIZE;
964 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
965 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
966 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
968 check_frame_size (f, &min_rows, &min_cols);
970 size_hints.base_width = base_width;
971 size_hints.base_height = base_height;
972 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
973 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
976 /* These currently have a one to one mapping with the X values, but I
977 don't think we should rely on that. */
978 hint_flags |= GDK_HINT_WIN_GRAVITY;
979 size_hints.win_gravity = 0;
980 if (win_gravity == NorthWestGravity)
981 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
982 else if (win_gravity == NorthGravity)
983 size_hints.win_gravity = GDK_GRAVITY_NORTH;
984 else if (win_gravity == NorthEastGravity)
985 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
986 else if (win_gravity == WestGravity)
987 size_hints.win_gravity = GDK_GRAVITY_WEST;
988 else if (win_gravity == CenterGravity)
989 size_hints.win_gravity = GDK_GRAVITY_CENTER;
990 else if (win_gravity == EastGravity)
991 size_hints.win_gravity = GDK_GRAVITY_EAST;
992 else if (win_gravity == SouthWestGravity)
993 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
994 else if (win_gravity == SouthGravity)
995 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
996 else if (win_gravity == SouthEastGravity)
997 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
998 else if (win_gravity == StaticGravity)
999 size_hints.win_gravity = GDK_GRAVITY_STATIC;
1001 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
1002 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
1003 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
1005 if (user_position)
1007 hint_flags &= ~GDK_HINT_POS;
1008 hint_flags |= GDK_HINT_USER_POS;
1011 if (hint_flags != f->output_data.x->hint_flags
1012 || memcmp (&size_hints,
1013 &f->output_data.x->size_hints,
1014 sizeof (size_hints)) != 0)
1016 BLOCK_INPUT;
1018 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1019 NULL,
1020 &size_hints,
1021 hint_flags);
1023 f->output_data.x->size_hints = size_hints;
1024 f->output_data.x->hint_flags = hint_flags;
1025 UNBLOCK_INPUT;
1030 /* Change background color of a frame.
1031 Since GTK uses the background color to clear the window, we must
1032 keep the GTK and X colors in sync.
1033 F is the frame to change,
1034 BG is the pixel value to change to. */
1036 void
1037 xg_set_background_color (f, bg)
1038 FRAME_PTR f;
1039 unsigned long bg;
1041 if (FRAME_GTK_WIDGET (f))
1043 GdkColor gdk_bg;
1045 BLOCK_INPUT;
1046 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
1047 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
1048 UNBLOCK_INPUT;
1053 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
1054 functions so GTK does not overwrite the icon. */
1056 void
1057 xg_set_frame_icon (f, icon_pixmap, icon_mask)
1058 FRAME_PTR f;
1059 Pixmap icon_pixmap;
1060 Pixmap icon_mask;
1062 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
1063 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
1064 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
1065 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
1067 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
1072 /***********************************************************************
1073 Dialog functions
1074 ***********************************************************************/
1075 /* Return the dialog title to use for a dialog of type KEY.
1076 This is the encoding used by lwlib. We use the same for GTK. */
1078 static char *
1079 get_dialog_title (char key)
1081 char *title = "";
1083 switch (key) {
1084 case 'E': case 'e':
1085 title = "Error";
1086 break;
1088 case 'I': case 'i':
1089 title = "Information";
1090 break;
1092 case 'L': case 'l':
1093 title = "Prompt";
1094 break;
1096 case 'P': case 'p':
1097 title = "Prompt";
1098 break;
1100 case 'Q': case 'q':
1101 title = "Question";
1102 break;
1105 return title;
1108 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1109 the dialog, but return TRUE so the event does not propagate further
1110 in GTK. This prevents GTK from destroying the dialog widget automatically
1111 and we can always destrou the widget manually, regardles of how
1112 it was popped down (button press or WM_DELETE_WINDOW).
1113 W is the dialog widget.
1114 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1115 user_data is NULL (not used).
1117 Returns TRUE to end propagation of event. */
1119 static gboolean
1120 dialog_delete_callback (w, event, user_data)
1121 GtkWidget *w;
1122 GdkEvent *event;
1123 gpointer user_data;
1125 gtk_widget_unmap (w);
1126 return TRUE;
1129 /* Create a popup dialog window. See also xg_create_widget below.
1130 WV is a widget_value describing the dialog.
1131 SELECT_CB is the callback to use when a button has been pressed.
1132 DEACTIVATE_CB is the callback to use when the dialog pops down.
1134 Returns the GTK dialog widget. */
1136 static GtkWidget *
1137 create_dialog (wv, select_cb, deactivate_cb)
1138 widget_value *wv;
1139 GCallback select_cb;
1140 GCallback deactivate_cb;
1142 char *title = get_dialog_title (wv->name[0]);
1143 int total_buttons = wv->name[1] - '0';
1144 int right_buttons = wv->name[4] - '0';
1145 int left_buttons;
1146 int button_nr = 0;
1147 int button_spacing = 10;
1148 GtkWidget *wdialog = gtk_dialog_new ();
1149 widget_value *item;
1150 GtkBox *cur_box;
1151 GtkWidget *wvbox;
1152 GtkWidget *whbox_up;
1153 GtkWidget *whbox_down;
1155 /* If the number of buttons is greater than 4, make two rows of buttons
1156 instead. This looks better. */
1157 int make_two_rows = total_buttons > 4;
1159 if (right_buttons == 0) right_buttons = total_buttons/2;
1160 left_buttons = total_buttons - right_buttons;
1162 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1163 gtk_widget_set_name (wdialog, "emacs-dialog");
1165 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1167 if (make_two_rows)
1169 wvbox = gtk_vbox_new (TRUE, button_spacing);
1170 whbox_up = gtk_hbox_new (FALSE, 0);
1171 whbox_down = gtk_hbox_new (FALSE, 0);
1173 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1174 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1175 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1177 cur_box = GTK_BOX (whbox_up);
1180 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1181 G_CALLBACK (dialog_delete_callback), 0);
1183 if (deactivate_cb)
1185 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1186 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1189 for (item = wv->contents; item; item = item->next)
1191 char *utf8_label = get_utf8_string (item->value);
1192 GtkWidget *w;
1193 GtkRequisition req;
1195 if (item->name && strcmp (item->name, "message") == 0)
1197 /* This is the text part of the dialog. */
1198 w = gtk_label_new (utf8_label);
1199 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1200 gtk_label_new (""),
1201 FALSE, FALSE, 0);
1202 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1203 TRUE, TRUE, 0);
1204 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1206 /* Try to make dialog look better. Must realize first so
1207 the widget can calculate the size it needs. */
1208 gtk_widget_realize (w);
1209 gtk_widget_size_request (w, &req);
1210 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1211 req.height);
1212 if (item->value && strlen (item->value) > 0)
1213 button_spacing = 2*req.width/strlen (item->value);
1215 else
1217 /* This is one button to add to the dialog. */
1218 w = gtk_button_new_with_label (utf8_label);
1219 if (! item->enabled)
1220 gtk_widget_set_sensitive (w, FALSE);
1221 if (select_cb)
1222 g_signal_connect (G_OBJECT (w), "clicked",
1223 select_cb, item->call_data);
1225 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1226 if (++button_nr == left_buttons)
1228 if (make_two_rows)
1229 cur_box = GTK_BOX (whbox_down);
1230 else
1231 gtk_box_pack_start (cur_box,
1232 gtk_label_new (""),
1233 TRUE, TRUE,
1234 button_spacing);
1238 if (utf8_label && utf8_label != item->value)
1239 g_free (utf8_label);
1242 return wdialog;
1247 /***********************************************************************
1248 File dialog functions
1249 ***********************************************************************/
1250 /* Return non-zero if the old file selection dialog is being used.
1251 Return zero if not. */
1254 xg_uses_old_file_dialog ()
1256 #ifdef HAVE_GTK_FILE_BOTH
1257 extern int x_gtk_use_old_file_dialog;
1258 return x_gtk_use_old_file_dialog;
1259 #else /* ! HAVE_GTK_FILE_BOTH */
1261 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1262 return 1;
1263 #else
1264 return 0;
1265 #endif
1267 #endif /* ! HAVE_GTK_FILE_BOTH */
1271 /* Function that is called when the file or font dialogs pop down.
1272 W is the dialog widget, RESPONSE is the response code.
1273 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1275 static void
1276 xg_dialog_response_cb (w,
1277 response,
1278 user_data)
1279 GtkDialog *w;
1280 gint response;
1281 gpointer user_data;
1283 int *ptr = (int *) user_data;
1284 *ptr = response;
1288 /* Destroy the dialog. This makes it pop down. */
1290 static Lisp_Object
1291 pop_down_dialog (arg)
1292 Lisp_Object arg;
1294 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1295 BLOCK_INPUT;
1296 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1297 UNBLOCK_INPUT;
1298 return Qnil;
1301 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1303 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1305 /* Return the selected file for file chooser dialog W.
1306 The returned string must be free:d. */
1308 static char *
1309 xg_get_file_name_from_chooser (w)
1310 GtkWidget *w;
1312 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1315 /* Callback called when the "Show hidden files" toggle is pressed.
1316 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1318 static void
1319 xg_toggle_visibility_cb (widget, data)
1320 GtkWidget *widget;
1321 gpointer data;
1323 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1324 gboolean visible;
1325 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1326 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1330 /* Callback called when a property changes in a file chooser.
1331 GOBJECT is the file chooser dialog, ARG1 describes the property.
1332 USER_DATA is the toggle widget in the file chooser dialog.
1333 We use this to update the "Show hidden files" toggle when the user
1334 changes that property by right clicking in the file list. */
1336 static void
1337 xg_toggle_notify_cb (gobject, arg1, user_data)
1338 GObject *gobject;
1339 GParamSpec *arg1;
1340 gpointer user_data;
1342 extern int x_gtk_show_hidden_files;
1344 if (strcmp (arg1->name, "show-hidden") == 0)
1346 GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
1347 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1348 gboolean visible, toggle_on;
1350 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1351 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1353 if (!!visible != !!toggle_on)
1355 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1356 G_CALLBACK (xg_toggle_visibility_cb),
1357 gobject);
1358 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1359 g_signal_handlers_unblock_by_func
1360 (G_OBJECT (wtoggle),
1361 G_CALLBACK (xg_toggle_visibility_cb),
1362 gobject);
1364 x_gtk_show_hidden_files = visible;
1368 /* Read a file name from the user using a file chooser dialog.
1369 F is the current frame.
1370 PROMPT is a prompt to show to the user. May not be NULL.
1371 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1372 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1373 file. *FUNC is set to a function that can be used to retrieve the
1374 selected file name from the returned widget.
1376 Returns the created widget. */
1378 static GtkWidget *
1379 xg_get_file_with_chooser (f, prompt, default_filename,
1380 mustmatch_p, only_dir_p, func)
1381 FRAME_PTR f;
1382 char *prompt;
1383 char *default_filename;
1384 int mustmatch_p, only_dir_p;
1385 xg_get_file_func *func;
1387 char message[1024];
1389 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1390 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1391 GtkFileChooserAction action = (mustmatch_p ?
1392 GTK_FILE_CHOOSER_ACTION_OPEN :
1393 GTK_FILE_CHOOSER_ACTION_SAVE);
1394 extern int x_gtk_show_hidden_files;
1395 extern int x_gtk_file_dialog_help_text;
1398 if (only_dir_p)
1399 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1401 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1402 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1403 (mustmatch_p || only_dir_p ?
1404 GTK_STOCK_OPEN : GTK_STOCK_OK),
1405 GTK_RESPONSE_OK,
1406 NULL);
1407 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1409 wbox = gtk_vbox_new (FALSE, 0);
1410 gtk_widget_show (wbox);
1411 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1413 if (x_gtk_show_hidden_files)
1415 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1416 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1418 gtk_widget_show (wtoggle);
1419 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1420 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1421 g_signal_connect (G_OBJECT (filewin), "notify",
1422 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1424 if (x_gtk_file_dialog_help_text)
1426 message[0] = '\0';
1427 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1428 Show the C-l help text only for versions < 2.10. */
1429 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1430 strcat (message, "\nType C-l to display a file name text entry box.\n");
1431 strcat (message, "\nIf you don't like this file selector, use the "
1432 "corresponding\nkey binding or customize "
1433 "use-file-dialog to turn it off.");
1435 wmessage = gtk_label_new (message);
1436 gtk_widget_show (wmessage);
1439 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1440 if (x_gtk_file_dialog_help_text)
1441 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1442 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1444 if (default_filename)
1446 Lisp_Object file;
1447 struct gcpro gcpro1;
1448 char *utf8_filename;
1449 GCPRO1 (file);
1451 file = build_string (default_filename);
1453 /* File chooser does not understand ~/... in the file name. It must be
1454 an absolute name starting with /. */
1455 if (default_filename[0] != '/')
1456 file = Fexpand_file_name (file, Qnil);
1458 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1459 if (! NILP (Ffile_directory_p (file)))
1460 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1461 utf8_filename);
1462 else
1464 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1465 utf8_filename);
1466 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1468 char *cp = strrchr (utf8_filename, '/');
1469 if (cp) ++cp;
1470 else cp = utf8_filename;
1471 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1475 UNGCPRO;
1478 *func = xg_get_file_name_from_chooser;
1479 return filewin;
1481 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1483 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1485 /* Return the selected file for file selector dialog W.
1486 The returned string must be free:d. */
1488 static char *
1489 xg_get_file_name_from_selector (w)
1490 GtkWidget *w;
1492 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1493 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1496 /* Create a file selection dialog.
1497 F is the current frame.
1498 PROMPT is a prompt to show to the user. May not be NULL.
1499 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1500 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1501 file. *FUNC is set to a function that can be used to retrieve the
1502 selected file name from the returned widget.
1504 Returns the created widget. */
1506 static GtkWidget *
1507 xg_get_file_with_selection (f, prompt, default_filename,
1508 mustmatch_p, only_dir_p, func)
1509 FRAME_PTR f;
1510 char *prompt;
1511 char *default_filename;
1512 int mustmatch_p, only_dir_p;
1513 xg_get_file_func *func;
1515 GtkWidget *filewin;
1516 GtkFileSelection *filesel;
1518 filewin = gtk_file_selection_new (prompt);
1519 filesel = GTK_FILE_SELECTION (filewin);
1521 if (default_filename)
1522 gtk_file_selection_set_filename (filesel, default_filename);
1524 if (mustmatch_p)
1526 /* The selection_entry part of filesel is not documented. */
1527 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1528 gtk_file_selection_hide_fileop_buttons (filesel);
1531 *func = xg_get_file_name_from_selector;
1533 return filewin;
1535 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1537 /* Read a file name from the user using a file dialog, either the old
1538 file selection dialog, or the new file chooser dialog. Which to use
1539 depends on what the GTK version used has, and what the value of
1540 gtk-use-old-file-dialog.
1541 F is the current frame.
1542 PROMPT is a prompt to show to the user. May not be NULL.
1543 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1544 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1545 file.
1547 Returns a file name or NULL if no file was selected.
1548 The returned string must be freed by the caller. */
1550 char *
1551 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1552 FRAME_PTR f;
1553 char *prompt;
1554 char *default_filename;
1555 int mustmatch_p, only_dir_p;
1557 GtkWidget *w = 0;
1558 int count = SPECPDL_INDEX ();
1559 char *fn = 0;
1560 int filesel_done = 0;
1561 xg_get_file_func func;
1563 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1564 /* I really don't know why this is needed, but without this the GLIBC add on
1565 library linuxthreads hangs when the Gnome file chooser backend creates
1566 threads. */
1567 sigblock (sigmask (__SIGRTMIN));
1568 #endif /* HAVE_GTK_AND_PTHREAD */
1570 #ifdef HAVE_GTK_FILE_BOTH
1572 if (xg_uses_old_file_dialog ())
1573 w = xg_get_file_with_selection (f, prompt, default_filename,
1574 mustmatch_p, only_dir_p, &func);
1575 else
1576 w = xg_get_file_with_chooser (f, prompt, default_filename,
1577 mustmatch_p, only_dir_p, &func);
1579 #else /* not HAVE_GTK_FILE_BOTH */
1581 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1582 w = xg_get_file_with_selection (f, prompt, default_filename,
1583 mustmatch_p, only_dir_p, &func);
1584 #endif
1585 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1586 w = xg_get_file_with_chooser (f, prompt, default_filename,
1587 mustmatch_p, only_dir_p, &func);
1588 #endif
1590 #endif /* HAVE_GTK_FILE_BOTH */
1592 xg_set_screen (w, f);
1593 gtk_widget_set_name (w, "emacs-filedialog");
1594 gtk_window_set_transient_for (GTK_WINDOW (w),
1595 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1596 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1597 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1599 g_signal_connect (G_OBJECT (w),
1600 "response",
1601 G_CALLBACK (xg_dialog_response_cb),
1602 &filesel_done);
1604 /* Don't destroy the widget if closed by the window manager close button. */
1605 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1607 gtk_widget_show (w);
1609 record_unwind_protect (pop_down_dialog, make_save_value (w, 0));
1610 while (! filesel_done)
1612 x_menu_wait_for_event (0);
1613 gtk_main_iteration ();
1616 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1617 sigunblock (sigmask (__SIGRTMIN));
1618 #endif
1620 if (filesel_done == GTK_RESPONSE_OK)
1621 fn = (*func) (w);
1623 unbind_to (count, Qnil);
1625 return fn;
1628 #ifdef HAVE_FREETYPE
1629 /* Pop up a GTK font selector and return the name of the font the user
1630 selects, as a C string. The returned font name follows GTK's own
1631 format:
1633 `FAMILY [VALUE1 VALUE2] SIZE'
1635 This can be parsed using font_parse_fcname in font.c.
1636 DEFAULT_NAME, if non-zero, is the default font name. */
1638 char *
1639 xg_get_font_name (f, default_name)
1640 FRAME_PTR f;
1641 char *default_name;
1643 GtkWidget *w = 0;
1644 int count = SPECPDL_INDEX ();
1645 char *fontname = NULL;
1646 int done = 0;
1648 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1649 sigblock (sigmask (__SIGRTMIN));
1650 #endif /* HAVE_GTK_AND_PTHREAD */
1652 w = gtk_font_selection_dialog_new ("Pick a font");
1653 if (default_name)
1654 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1655 default_name);
1657 xg_set_screen (w, f);
1658 gtk_widget_set_name (w, "emacs-fontdialog");
1659 gtk_window_set_transient_for (GTK_WINDOW (w),
1660 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1661 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1662 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1664 g_signal_connect (G_OBJECT (w), "response",
1665 G_CALLBACK (xg_dialog_response_cb), &done);
1667 /* Don't destroy the widget if closed by the window manager close button. */
1668 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1670 gtk_widget_show (w);
1672 record_unwind_protect (pop_down_dialog, make_save_value (w, 0));
1673 while (!done)
1675 x_menu_wait_for_event (0);
1676 gtk_main_iteration ();
1679 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1680 sigunblock (sigmask (__SIGRTMIN));
1681 #endif
1683 if (done == GTK_RESPONSE_OK)
1684 fontname = gtk_font_selection_dialog_get_font_name
1685 ((GtkFontSelectionDialog *) w);
1687 unbind_to (count, Qnil);
1689 return fontname;
1691 #endif /* HAVE_FREETYPE */
1695 /***********************************************************************
1696 Menu functions.
1697 ***********************************************************************/
1699 /* The name of menu items that can be used for customization. Since GTK
1700 RC files are very crude and primitive, we have to set this on all
1701 menu item names so a user can easily customize menu items. */
1703 #define MENU_ITEM_NAME "emacs-menuitem"
1706 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1707 during GC. The next member points to the items. */
1708 static xg_list_node xg_menu_cb_list;
1710 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1711 during GC. The next member points to the items. */
1712 static xg_list_node xg_menu_item_cb_list;
1714 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1715 F is the frame CL_DATA will be initialized for.
1716 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1718 The menu bar and all sub menus under the menu bar in a frame
1719 share the same structure, hence the reference count.
1721 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1722 allocated xg_menu_cb_data if CL_DATA is NULL. */
1724 static xg_menu_cb_data *
1725 make_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 = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1733 cl_data->f = f;
1734 cl_data->menu_bar_vector = f->menu_bar_vector;
1735 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1736 cl_data->highlight_cb = highlight_cb;
1737 cl_data->ref_count = 0;
1739 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1742 cl_data->ref_count++;
1744 return cl_data;
1747 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1748 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1750 When the menu bar is updated, menu items may have been added and/or
1751 removed, so menu_bar_vector and menu_bar_items_used change. We must
1752 then update CL_DATA since it is used to determine which menu
1753 item that is invoked in the menu.
1754 HIGHLIGHT_CB could change, there is no check that the same
1755 function is given when modifying a menu bar as was given when
1756 creating the menu bar. */
1758 static void
1759 update_cl_data (cl_data, f, highlight_cb)
1760 xg_menu_cb_data *cl_data;
1761 FRAME_PTR f;
1762 GCallback highlight_cb;
1764 if (cl_data)
1766 cl_data->f = f;
1767 cl_data->menu_bar_vector = f->menu_bar_vector;
1768 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1769 cl_data->highlight_cb = highlight_cb;
1773 /* Decrease reference count for CL_DATA.
1774 If reference count is zero, free CL_DATA. */
1776 static void
1777 unref_cl_data (cl_data)
1778 xg_menu_cb_data *cl_data;
1780 if (cl_data && cl_data->ref_count > 0)
1782 cl_data->ref_count--;
1783 if (cl_data->ref_count == 0)
1785 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1786 xfree (cl_data);
1791 /* Function that marks all lisp data during GC. */
1793 void
1794 xg_mark_data ()
1796 xg_list_node *iter;
1798 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1799 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1801 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1803 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1805 if (! NILP (cb_data->help))
1806 mark_object (cb_data->help);
1811 /* Callback called when a menu item is destroyed. Used to free data.
1812 W is the widget that is being destroyed (not used).
1813 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1815 static void
1816 menuitem_destroy_callback (w, client_data)
1817 GtkWidget *w;
1818 gpointer client_data;
1820 if (client_data)
1822 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1823 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1824 xfree (data);
1828 /* Callback called when the pointer enters/leaves a menu item.
1829 W is the parent of the menu item.
1830 EVENT is either an enter event or leave event.
1831 CLIENT_DATA is not used.
1833 Returns FALSE to tell GTK to keep processing this event. */
1835 static gboolean
1836 menuitem_highlight_callback (w, event, client_data)
1837 GtkWidget *w;
1838 GdkEventCrossing *event;
1839 gpointer client_data;
1841 GdkEvent ev;
1842 GtkWidget *subwidget;
1843 xg_menu_item_cb_data *data;
1845 ev.crossing = *event;
1846 subwidget = gtk_get_event_widget (&ev);
1847 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1848 XG_ITEM_DATA);
1849 if (data)
1851 if (! NILP (data->help) && data->cl_data->highlight_cb)
1853 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1854 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1855 (*func) (subwidget, call_data);
1859 return FALSE;
1862 /* Callback called when a menu is destroyed. Used to free data.
1863 W is the widget that is being destroyed (not used).
1864 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1866 static void
1867 menu_destroy_callback (w, client_data)
1868 GtkWidget *w;
1869 gpointer client_data;
1871 unref_cl_data ((xg_menu_cb_data*) client_data);
1874 /* Callback called when a menu does a grab or ungrab. That means the
1875 menu has been activated or deactivated.
1876 Used to start a timer so the small timeout the menus in GTK uses before
1877 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1878 W is the widget that does the grab (not used).
1879 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1880 CLIENT_DATA is NULL (not used). */
1882 /* Keep track of total number of grabs. */
1883 static int menu_grab_callback_cnt;
1885 static void
1886 menu_grab_callback (GtkWidget *widget,
1887 gboolean ungrab_p,
1888 gpointer client_data)
1890 if (ungrab_p) menu_grab_callback_cnt--;
1891 else menu_grab_callback_cnt++;
1893 if (menu_grab_callback_cnt > 0 && ! xg_timer) xg_start_timer ();
1894 else if (menu_grab_callback_cnt == 0 && xg_timer) xg_stop_timer ();
1897 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1898 must be non-NULL) and can be inserted into a menu item.
1900 Returns the GtkHBox. */
1902 static GtkWidget *
1903 make_widget_for_menu_item (utf8_label, utf8_key)
1904 char *utf8_label;
1905 char *utf8_key;
1907 GtkWidget *wlbl;
1908 GtkWidget *wkey;
1909 GtkWidget *wbox;
1911 wbox = gtk_hbox_new (FALSE, 0);
1912 wlbl = gtk_label_new (utf8_label);
1913 wkey = gtk_label_new (utf8_key);
1915 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1916 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1918 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1919 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1921 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1922 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1923 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1925 return wbox;
1928 /* Make and return a menu item widget with the key to the right.
1929 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1930 UTF8_KEY is the text representing the key binding.
1931 ITEM is the widget_value describing the menu item.
1933 GROUP is an in/out parameter. If the menu item to be created is not
1934 part of any radio menu group, *GROUP contains NULL on entry and exit.
1935 If the menu item to be created is part of a radio menu group, on entry
1936 *GROUP contains the group to use, or NULL if this is the first item
1937 in the group. On exit, *GROUP contains the radio item group.
1939 Unfortunately, keys don't line up as nicely as in Motif,
1940 but the MacOS X version doesn't either, so I guess that is OK. */
1942 static GtkWidget *
1943 make_menu_item (utf8_label, utf8_key, item, group)
1944 char *utf8_label;
1945 char *utf8_key;
1946 widget_value *item;
1947 GSList **group;
1949 GtkWidget *w;
1950 GtkWidget *wtoadd = 0;
1952 /* It has been observed that some menu items have a NULL name field.
1953 This will lead to this function being called with a NULL utf8_label.
1954 GTK crashes on that so we set a blank label. Why there is a NULL
1955 name remains to be investigated. */
1956 if (! utf8_label) utf8_label = " ";
1958 if (utf8_key)
1959 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1961 if (item->button_type == BUTTON_TYPE_TOGGLE)
1963 *group = NULL;
1964 if (utf8_key) w = gtk_check_menu_item_new ();
1965 else w = gtk_check_menu_item_new_with_label (utf8_label);
1966 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1968 else if (item->button_type == BUTTON_TYPE_RADIO)
1970 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1971 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1972 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1973 if (item->selected)
1974 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1976 else
1978 *group = NULL;
1979 if (utf8_key) w = gtk_menu_item_new ();
1980 else w = gtk_menu_item_new_with_label (utf8_label);
1983 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1984 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1986 return w;
1989 /* Return non-zero if LABEL specifies a separator (GTK only has one
1990 separator type) */
1992 static char* separator_names[] = {
1993 "space",
1994 "no-line",
1995 "single-line",
1996 "double-line",
1997 "single-dashed-line",
1998 "double-dashed-line",
1999 "shadow-etched-in",
2000 "shadow-etched-out",
2001 "shadow-etched-in-dash",
2002 "shadow-etched-out-dash",
2003 "shadow-double-etched-in",
2004 "shadow-double-etched-out",
2005 "shadow-double-etched-in-dash",
2006 "shadow-double-etched-out-dash",
2010 static int
2011 xg_separator_p (char *label)
2013 if (! label) return 0;
2014 else if (strlen (label) > 3
2015 && strncmp (label, "--", 2) == 0
2016 && label[2] != '-')
2018 int i;
2020 label += 2;
2021 for (i = 0; separator_names[i]; ++i)
2022 if (strcmp (label, separator_names[i]) == 0)
2023 return 1;
2025 else
2027 /* Old-style separator, maybe. It's a separator if it contains
2028 only dashes. */
2029 while (*label == '-')
2030 ++label;
2031 if (*label == 0) return 1;
2034 return 0;
2037 static int xg_detached_menus;
2039 /* Returns non-zero if there are detached menus. */
2042 xg_have_tear_offs ()
2044 return xg_detached_menus > 0;
2047 /* Callback invoked when a detached menu window is removed. Here we
2048 decrease the xg_detached_menus count.
2049 WIDGET is the top level window that is removed (the parent of the menu).
2050 CLIENT_DATA is not used. */
2052 static void
2053 tearoff_remove (widget, client_data)
2054 GtkWidget *widget;
2055 gpointer client_data;
2057 if (xg_detached_menus > 0) --xg_detached_menus;
2060 /* Callback invoked when a menu is detached. It increases the
2061 xg_detached_menus count.
2062 WIDGET is the GtkTearoffMenuItem.
2063 CLIENT_DATA is not used. */
2065 static void
2066 tearoff_activate (widget, client_data)
2067 GtkWidget *widget;
2068 gpointer client_data;
2070 GtkWidget *menu = gtk_widget_get_parent (widget);
2071 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
2073 ++xg_detached_menus;
2074 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
2075 "destroy",
2076 G_CALLBACK (tearoff_remove), 0);
2081 /* Create a menu item widget, and connect the callbacks.
2082 ITEM decribes the menu item.
2083 F is the frame the created menu belongs to.
2084 SELECT_CB is the callback to use when a menu item is selected.
2085 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2086 CL_DATA points to the callback data to be used for this menu.
2087 GROUP is an in/out parameter. If the menu item to be created is not
2088 part of any radio menu group, *GROUP contains NULL on entry and exit.
2089 If the menu item to be created is part of a radio menu group, on entry
2090 *GROUP contains the group to use, or NULL if this is the first item
2091 in the group. On exit, *GROUP contains the radio item group.
2093 Returns the created GtkWidget. */
2095 static GtkWidget *
2096 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
2097 widget_value *item;
2098 FRAME_PTR f;
2099 GCallback select_cb;
2100 GCallback highlight_cb;
2101 xg_menu_cb_data *cl_data;
2102 GSList **group;
2104 char *utf8_label;
2105 char *utf8_key;
2106 GtkWidget *w;
2107 xg_menu_item_cb_data *cb_data;
2109 utf8_label = get_utf8_string (item->name);
2110 utf8_key = get_utf8_string (item->key);
2112 w = make_menu_item (utf8_label, utf8_key, item, group);
2114 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2115 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
2117 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
2119 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
2121 cb_data->select_id = 0;
2122 cb_data->help = item->help;
2123 cb_data->cl_data = cl_data;
2124 cb_data->call_data = item->call_data;
2126 g_signal_connect (G_OBJECT (w),
2127 "destroy",
2128 G_CALLBACK (menuitem_destroy_callback),
2129 cb_data);
2131 /* Put cb_data in widget, so we can get at it when modifying menubar */
2132 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2134 /* final item, not a submenu */
2135 if (item->call_data && ! item->contents)
2137 if (select_cb)
2138 cb_data->select_id
2139 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2142 return w;
2145 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
2146 GCallback, GCallback, int, int, int,
2147 GtkWidget *, xg_menu_cb_data *, char *));
2149 /* Create a full menu tree specified by DATA.
2150 F is the frame the created menu belongs to.
2151 SELECT_CB is the callback to use when a menu item is selected.
2152 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2153 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2154 POP_UP_P is non-zero if we shall create a popup menu.
2155 MENU_BAR_P is non-zero if we shall create a menu bar.
2156 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2157 if MENU_BAR_P is non-zero.
2158 TOPMENU is the topmost GtkWidget that others shall be placed under.
2159 It may be NULL, in that case we create the appropriate widget
2160 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2161 CL_DATA is the callback data we shall use for this menu, or NULL
2162 if we haven't set the first callback yet.
2163 NAME is the name to give to the top level menu if this function
2164 creates it. May be NULL to not set any name.
2166 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2167 not NULL.
2169 This function calls itself to create submenus. */
2171 static GtkWidget *
2172 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
2173 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
2174 widget_value *data;
2175 FRAME_PTR f;
2176 GCallback select_cb;
2177 GCallback deactivate_cb;
2178 GCallback highlight_cb;
2179 int pop_up_p;
2180 int menu_bar_p;
2181 int add_tearoff_p;
2182 GtkWidget *topmenu;
2183 xg_menu_cb_data *cl_data;
2184 char *name;
2186 widget_value *item;
2187 GtkWidget *wmenu = topmenu;
2188 GSList *group = NULL;
2190 if (! topmenu)
2192 if (! menu_bar_p)
2194 wmenu = gtk_menu_new ();
2195 xg_set_screen (wmenu, f);
2196 /* Connect this to the menu instead of items so we get enter/leave for
2197 disabled items also. TODO: Still does not get enter/leave for
2198 disabled items in detached menus. */
2199 g_signal_connect (G_OBJECT (wmenu),
2200 "enter-notify-event",
2201 G_CALLBACK (menuitem_highlight_callback),
2202 NULL);
2203 g_signal_connect (G_OBJECT (wmenu),
2204 "leave-notify-event",
2205 G_CALLBACK (menuitem_highlight_callback),
2206 NULL);
2208 else wmenu = gtk_menu_bar_new ();
2210 /* Put cl_data on the top menu for easier access. */
2211 cl_data = make_cl_data (cl_data, f, highlight_cb);
2212 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2213 g_signal_connect (G_OBJECT (wmenu), "destroy",
2214 G_CALLBACK (menu_destroy_callback), cl_data);
2216 if (name)
2217 gtk_widget_set_name (wmenu, name);
2219 if (deactivate_cb)
2220 g_signal_connect (G_OBJECT (wmenu),
2221 "selection-done", deactivate_cb, 0);
2223 g_signal_connect (G_OBJECT (wmenu),
2224 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
2227 if (! menu_bar_p && add_tearoff_p)
2229 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2230 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2232 g_signal_connect (G_OBJECT (tearoff), "activate",
2233 G_CALLBACK (tearoff_activate), 0);
2236 for (item = data; item; item = item->next)
2238 GtkWidget *w;
2240 if (pop_up_p && !item->contents && !item->call_data
2241 && !xg_separator_p (item->name))
2243 char *utf8_label;
2244 /* A title for a popup. We do the same as GTK does when
2245 creating titles, but it does not look good. */
2246 group = NULL;
2247 utf8_label = get_utf8_string (item->name);
2249 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2250 w = gtk_menu_item_new_with_label (utf8_label);
2251 gtk_widget_set_sensitive (w, FALSE);
2252 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2254 else if (xg_separator_p (item->name))
2256 group = NULL;
2257 /* GTK only have one separator type. */
2258 w = gtk_separator_menu_item_new ();
2260 else
2262 w = xg_create_one_menuitem (item,
2264 item->contents ? 0 : select_cb,
2265 highlight_cb,
2266 cl_data,
2267 &group);
2269 /* Create a possibly empty submenu for menu bar items, since some
2270 themes don't highlight items correctly without it. */
2271 if (item->contents || menu_bar_p)
2273 GtkWidget *submenu = create_menus (item->contents,
2275 select_cb,
2276 deactivate_cb,
2277 highlight_cb,
2280 add_tearoff_p,
2282 cl_data,
2284 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2288 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2289 gtk_widget_set_name (w, MENU_ITEM_NAME);
2292 return wmenu;
2295 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2296 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2297 with some text and buttons.
2298 F is the frame the created item belongs to.
2299 NAME is the name to use for the top widget.
2300 VAL is a widget_value structure describing items to be created.
2301 SELECT_CB is the callback to use when a menu item is selected or
2302 a dialog button is pressed.
2303 DEACTIVATE_CB is the callback to use when an item is deactivated.
2304 For a menu, when a sub menu is not shown anymore, for a dialog it is
2305 called when the dialog is popped down.
2306 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2308 Returns the widget created. */
2310 GtkWidget *
2311 xg_create_widget (type, name, f, val,
2312 select_cb, deactivate_cb, highlight_cb)
2313 char *type;
2314 char *name;
2315 FRAME_PTR f;
2316 widget_value *val;
2317 GCallback select_cb;
2318 GCallback deactivate_cb;
2319 GCallback highlight_cb;
2321 GtkWidget *w = 0;
2322 int menu_bar_p = strcmp (type, "menubar") == 0;
2323 int pop_up_p = strcmp (type, "popup") == 0;
2325 if (strcmp (type, "dialog") == 0)
2327 w = create_dialog (val, select_cb, deactivate_cb);
2328 xg_set_screen (w, f);
2329 gtk_window_set_transient_for (GTK_WINDOW (w),
2330 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2331 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2332 gtk_widget_set_name (w, "emacs-dialog");
2333 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2335 else if (menu_bar_p || pop_up_p)
2337 w = create_menus (val->contents,
2339 select_cb,
2340 deactivate_cb,
2341 highlight_cb,
2342 pop_up_p,
2343 menu_bar_p,
2344 menu_bar_p,
2347 name);
2349 /* Set the cursor to an arrow for popup menus when they are mapped.
2350 This is done by default for menu bar menus. */
2351 if (pop_up_p)
2353 /* Must realize so the GdkWindow inside the widget is created. */
2354 gtk_widget_realize (w);
2355 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2358 else
2360 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2361 type);
2364 return w;
2367 /* Return the label for menu item WITEM. */
2369 static const char *
2370 xg_get_menu_item_label (witem)
2371 GtkMenuItem *witem;
2373 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2374 return gtk_label_get_label (wlabel);
2377 /* Return non-zero if the menu item WITEM has the text LABEL. */
2379 static int
2380 xg_item_label_same_p (witem, label)
2381 GtkMenuItem *witem;
2382 char *label;
2384 int is_same = 0;
2385 char *utf8_label = get_utf8_string (label);
2386 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2388 if (! old_label && ! utf8_label)
2389 is_same = 1;
2390 else if (old_label && utf8_label)
2391 is_same = strcmp (utf8_label, old_label) == 0;
2393 if (utf8_label && utf8_label != label) g_free (utf8_label);
2395 return is_same;
2398 /* Destroy widgets in LIST. */
2400 static void
2401 xg_destroy_widgets (list)
2402 GList *list;
2404 GList *iter;
2406 for (iter = list; iter; iter = g_list_next (iter))
2408 GtkWidget *w = GTK_WIDGET (iter->data);
2410 /* Destroying the widget will remove it from the container it is in. */
2411 gtk_widget_destroy (w);
2415 /* Update the top level names in MENUBAR (i.e. not submenus).
2416 F is the frame the menu bar belongs to.
2417 *LIST is a list with the current menu bar names (menu item widgets).
2418 ITER is the item within *LIST that shall be updated.
2419 POS is the numerical position, starting at 0, of ITER in *LIST.
2420 VAL describes what the menu bar shall look like after the update.
2421 SELECT_CB is the callback to use when a menu item is selected.
2422 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2423 CL_DATA points to the callback data to be used for this menu bar.
2425 This function calls itself to walk through the menu bar names. */
2427 static void
2428 xg_update_menubar (menubar, f, list, iter, pos, val,
2429 select_cb, deactivate_cb, highlight_cb, cl_data)
2430 GtkWidget *menubar;
2431 FRAME_PTR f;
2432 GList **list;
2433 GList *iter;
2434 int pos;
2435 widget_value *val;
2436 GCallback select_cb;
2437 GCallback deactivate_cb;
2438 GCallback highlight_cb;
2439 xg_menu_cb_data *cl_data;
2441 if (! iter && ! val)
2442 return;
2443 else if (iter && ! val)
2445 /* Item(s) have been removed. Remove all remaining items. */
2446 xg_destroy_widgets (iter);
2448 /* All updated. */
2449 val = 0;
2450 iter = 0;
2452 else if (! iter && val)
2454 /* Item(s) added. Add all new items in one call. */
2455 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2456 0, 1, 0, menubar, cl_data, 0);
2458 /* All updated. */
2459 val = 0;
2460 iter = 0;
2462 /* Below this neither iter or val is NULL */
2463 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2465 /* This item is still the same, check next item. */
2466 val = val->next;
2467 iter = g_list_next (iter);
2468 ++pos;
2470 else /* This item is changed. */
2472 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2473 GtkMenuItem *witem2 = 0;
2474 int val_in_menubar = 0;
2475 int iter_in_new_menubar = 0;
2476 GList *iter2;
2477 widget_value *cur;
2479 /* See if the changed entry (val) is present later in the menu bar */
2480 for (iter2 = iter;
2481 iter2 && ! val_in_menubar;
2482 iter2 = g_list_next (iter2))
2484 witem2 = GTK_MENU_ITEM (iter2->data);
2485 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2488 /* See if the current entry (iter) is present later in the
2489 specification for the new menu bar. */
2490 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2491 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2493 if (val_in_menubar && ! iter_in_new_menubar)
2495 int nr = pos;
2497 /* This corresponds to:
2498 Current: A B C
2499 New: A C
2500 Remove B. */
2502 gtk_widget_ref (GTK_WIDGET (witem));
2503 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2504 gtk_widget_destroy (GTK_WIDGET (witem));
2506 /* Must get new list since the old changed. */
2507 g_list_free (*list);
2508 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2509 while (nr-- > 0) iter = g_list_next (iter);
2511 else if (! val_in_menubar && ! iter_in_new_menubar)
2513 /* This corresponds to:
2514 Current: A B C
2515 New: A X C
2516 Rename B to X. This might seem to be a strange thing to do,
2517 since if there is a menu under B it will be totally wrong for X.
2518 But consider editing a C file. Then there is a C-mode menu
2519 (corresponds to B above).
2520 If then doing C-x C-f the minibuf menu (X above) replaces the
2521 C-mode menu. When returning from the minibuffer, we get
2522 back the C-mode menu. Thus we do:
2523 Rename B to X (C-mode to minibuf menu)
2524 Rename X to B (minibuf to C-mode menu).
2525 If the X menu hasn't been invoked, the menu under B
2526 is up to date when leaving the minibuffer. */
2527 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2528 char *utf8_label = get_utf8_string (val->name);
2529 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2531 gtk_label_set_text (wlabel, utf8_label);
2533 /* If this item has a submenu that has been detached, change
2534 the title in the WM decorations also. */
2535 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2536 /* Set the title of the detached window. */
2537 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2539 iter = g_list_next (iter);
2540 val = val->next;
2541 ++pos;
2543 else if (! val_in_menubar && iter_in_new_menubar)
2545 /* This corresponds to:
2546 Current: A B C
2547 New: A X B C
2548 Insert X. */
2550 int nr = pos;
2551 GList *group = 0;
2552 GtkWidget *w = xg_create_one_menuitem (val,
2554 select_cb,
2555 highlight_cb,
2556 cl_data,
2557 &group);
2559 /* Create a possibly empty submenu for menu bar items, since some
2560 themes don't highlight items correctly without it. */
2561 GtkWidget *submenu = create_menus (NULL, f,
2562 select_cb, deactivate_cb,
2563 highlight_cb,
2564 0, 0, 0, 0, cl_data, 0);
2565 gtk_widget_set_name (w, MENU_ITEM_NAME);
2566 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2567 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2569 g_list_free (*list);
2570 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2571 while (nr-- > 0) iter = g_list_next (iter);
2572 iter = g_list_next (iter);
2573 val = val->next;
2574 ++pos;
2576 else /* if (val_in_menubar && iter_in_new_menubar) */
2578 int nr = pos;
2579 /* This corresponds to:
2580 Current: A B C
2581 New: A C B
2582 Move C before B */
2584 gtk_widget_ref (GTK_WIDGET (witem2));
2585 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2586 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2587 GTK_WIDGET (witem2), pos);
2588 gtk_widget_unref (GTK_WIDGET (witem2));
2590 g_list_free (*list);
2591 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2592 while (nr-- > 0) iter = g_list_next (iter);
2593 if (iter) iter = g_list_next (iter);
2594 val = val->next;
2595 ++pos;
2599 /* Update the rest of the menu bar. */
2600 xg_update_menubar (menubar, f, list, iter, pos, val,
2601 select_cb, deactivate_cb, highlight_cb, cl_data);
2604 /* Update the menu item W so it corresponds to VAL.
2605 SELECT_CB is the callback to use when a menu item is selected.
2606 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2607 CL_DATA is the data to set in the widget for menu invokation. */
2609 static void
2610 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2611 widget_value *val;
2612 GtkWidget *w;
2613 GCallback select_cb;
2614 GCallback highlight_cb;
2615 xg_menu_cb_data *cl_data;
2617 GtkWidget *wchild;
2618 GtkLabel *wlbl = 0;
2619 GtkLabel *wkey = 0;
2620 char *utf8_label;
2621 char *utf8_key;
2622 const char *old_label = 0;
2623 const char *old_key = 0;
2624 xg_menu_item_cb_data *cb_data;
2626 wchild = gtk_bin_get_child (GTK_BIN (w));
2627 utf8_label = get_utf8_string (val->name);
2628 utf8_key = get_utf8_string (val->key);
2630 /* See if W is a menu item with a key. See make_menu_item above. */
2631 if (GTK_IS_HBOX (wchild))
2633 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2635 wlbl = GTK_LABEL (list->data);
2636 wkey = GTK_LABEL (list->next->data);
2637 g_list_free (list);
2639 if (! utf8_key)
2641 /* Remove the key and keep just the label. */
2642 gtk_widget_ref (GTK_WIDGET (wlbl));
2643 gtk_container_remove (GTK_CONTAINER (w), wchild);
2644 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2645 wkey = 0;
2649 else /* Just a label. */
2651 wlbl = GTK_LABEL (wchild);
2653 /* Check if there is now a key. */
2654 if (utf8_key)
2656 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2657 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2659 wlbl = GTK_LABEL (list->data);
2660 wkey = GTK_LABEL (list->next->data);
2661 g_list_free (list);
2663 gtk_container_remove (GTK_CONTAINER (w), wchild);
2664 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2669 if (wkey) old_key = gtk_label_get_label (wkey);
2670 if (wlbl) old_label = gtk_label_get_label (wlbl);
2672 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2673 gtk_label_set_text (wkey, utf8_key);
2675 if (! old_label || strcmp (utf8_label, old_label) != 0)
2676 gtk_label_set_text (wlbl, utf8_label);
2678 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2679 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2681 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2682 gtk_widget_set_sensitive (w, FALSE);
2683 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2684 gtk_widget_set_sensitive (w, TRUE);
2686 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2687 XG_ITEM_DATA);
2688 if (cb_data)
2690 cb_data->call_data = val->call_data;
2691 cb_data->help = val->help;
2692 cb_data->cl_data = cl_data;
2694 /* We assume the callback functions don't change. */
2695 if (val->call_data && ! val->contents)
2697 /* This item shall have a select callback. */
2698 if (! cb_data->select_id)
2699 cb_data->select_id
2700 = g_signal_connect (G_OBJECT (w), "activate",
2701 select_cb, cb_data);
2703 else if (cb_data->select_id)
2705 g_signal_handler_disconnect (w, cb_data->select_id);
2706 cb_data->select_id = 0;
2711 /* Update the toggle menu item W so it corresponds to VAL. */
2713 static void
2714 xg_update_toggle_item (val, w)
2715 widget_value *val;
2716 GtkWidget *w;
2718 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2721 /* Update the radio menu item W so it corresponds to VAL. */
2723 static void
2724 xg_update_radio_item (val, w)
2725 widget_value *val;
2726 GtkWidget *w;
2728 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2731 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2732 SUBMENU may be NULL, in that case a new menu is created.
2733 F is the frame the menu bar belongs to.
2734 VAL describes the contents of the menu bar.
2735 SELECT_CB is the callback to use when a menu item is selected.
2736 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2737 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2738 CL_DATA is the call back data to use for any newly created items.
2740 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2741 was NULL. */
2743 static GtkWidget *
2744 xg_update_submenu (submenu, f, val,
2745 select_cb, deactivate_cb, highlight_cb, cl_data)
2746 GtkWidget *submenu;
2747 FRAME_PTR f;
2748 widget_value *val;
2749 GCallback select_cb;
2750 GCallback deactivate_cb;
2751 GCallback highlight_cb;
2752 xg_menu_cb_data *cl_data;
2754 GtkWidget *newsub = submenu;
2755 GList *list = 0;
2756 GList *iter;
2757 widget_value *cur;
2758 int has_tearoff_p = 0;
2759 GList *first_radio = 0;
2761 if (submenu)
2762 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2764 for (cur = val, iter = list;
2765 cur && iter;
2766 iter = g_list_next (iter), cur = cur->next)
2768 GtkWidget *w = GTK_WIDGET (iter->data);
2770 /* Skip tearoff items, they have no counterpart in val. */
2771 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2773 has_tearoff_p = 1;
2774 iter = g_list_next (iter);
2775 if (iter) w = GTK_WIDGET (iter->data);
2776 else break;
2779 /* Remember first radio button in a group. If we get a mismatch in
2780 a radio group we must rebuild the whole group so that the connections
2781 in GTK becomes correct. */
2782 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2783 first_radio = iter;
2784 else if (cur->button_type != BUTTON_TYPE_RADIO
2785 && ! GTK_IS_RADIO_MENU_ITEM (w))
2786 first_radio = 0;
2788 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2790 if (! xg_separator_p (cur->name))
2791 break;
2793 else if (GTK_IS_CHECK_MENU_ITEM (w))
2795 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2796 break;
2797 xg_update_toggle_item (cur, w);
2798 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2800 else if (GTK_IS_RADIO_MENU_ITEM (w))
2802 if (cur->button_type != BUTTON_TYPE_RADIO)
2803 break;
2804 xg_update_radio_item (cur, w);
2805 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2807 else if (GTK_IS_MENU_ITEM (w))
2809 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2810 GtkWidget *sub;
2812 if (cur->button_type != BUTTON_TYPE_NONE ||
2813 xg_separator_p (cur->name))
2814 break;
2816 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2818 sub = gtk_menu_item_get_submenu (witem);
2819 if (sub && ! cur->contents)
2821 /* Not a submenu anymore. */
2822 gtk_widget_ref (sub);
2823 gtk_menu_item_remove_submenu (witem);
2824 gtk_widget_destroy (sub);
2826 else if (cur->contents)
2828 GtkWidget *nsub;
2830 nsub = xg_update_submenu (sub, f, cur->contents,
2831 select_cb, deactivate_cb,
2832 highlight_cb, cl_data);
2834 /* If this item just became a submenu, we must set it. */
2835 if (nsub != sub)
2836 gtk_menu_item_set_submenu (witem, nsub);
2839 else
2841 /* Structural difference. Remove everything from here and down
2842 in SUBMENU. */
2843 break;
2847 /* Remove widgets from first structual change. */
2848 if (iter)
2850 /* If we are adding new menu items below, we must remove from
2851 first radio button so that radio groups become correct. */
2852 if (cur && first_radio) xg_destroy_widgets (first_radio);
2853 else xg_destroy_widgets (iter);
2856 if (cur)
2858 /* More items added. Create them. */
2859 newsub = create_menus (cur,
2861 select_cb,
2862 deactivate_cb,
2863 highlight_cb,
2866 ! has_tearoff_p,
2867 submenu,
2868 cl_data,
2872 if (list) g_list_free (list);
2874 return newsub;
2877 /* Update the MENUBAR.
2878 F is the frame the menu bar belongs to.
2879 VAL describes the contents of the menu bar.
2880 If DEEP_P is non-zero, rebuild all but the top level menu names in
2881 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2882 SELECT_CB is the callback to use when a menu item is selected.
2883 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2884 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2886 void
2887 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2888 select_cb, deactivate_cb, highlight_cb)
2889 GtkWidget *menubar;
2890 FRAME_PTR f;
2891 widget_value *val;
2892 int deep_p;
2893 GCallback select_cb;
2894 GCallback deactivate_cb;
2895 GCallback highlight_cb;
2897 xg_menu_cb_data *cl_data;
2898 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2900 if (! list) return;
2902 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2903 XG_FRAME_DATA);
2905 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2906 select_cb, deactivate_cb, highlight_cb, cl_data);
2908 if (deep_p)
2910 widget_value *cur;
2912 /* Update all sub menus.
2913 We must keep the submenus (GTK menu item widgets) since the
2914 X Window in the XEvent that activates the menu are those widgets. */
2916 /* Update cl_data, menu_item things in F may have changed. */
2917 update_cl_data (cl_data, f, highlight_cb);
2919 for (cur = val->contents; cur; cur = cur->next)
2921 GList *iter;
2922 GtkWidget *sub = 0;
2923 GtkWidget *newsub;
2924 GtkMenuItem *witem;
2926 /* Find sub menu that corresponds to val and update it. */
2927 for (iter = list ; iter; iter = g_list_next (iter))
2929 witem = GTK_MENU_ITEM (iter->data);
2930 if (xg_item_label_same_p (witem, cur->name))
2932 sub = gtk_menu_item_get_submenu (witem);
2933 break;
2937 newsub = xg_update_submenu (sub,
2939 cur->contents,
2940 select_cb,
2941 deactivate_cb,
2942 highlight_cb,
2943 cl_data);
2944 /* sub may still be NULL. If we just updated non deep and added
2945 a new menu bar item, it has no sub menu yet. So we set the
2946 newly created sub menu under witem. */
2947 if (newsub != sub)
2949 xg_set_screen (newsub, f);
2950 gtk_menu_item_set_submenu (witem, newsub);
2955 g_list_free (list);
2956 gtk_widget_show_all (menubar);
2959 /* Recompute all the widgets of frame F, when the menu bar has been
2960 changed. Value is non-zero if widgets were updated. */
2963 xg_update_frame_menubar (f)
2964 FRAME_PTR f;
2966 struct x_output *x = f->output_data.x;
2967 GtkRequisition req;
2969 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2970 return 0;
2972 BLOCK_INPUT;
2974 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2975 FALSE, FALSE, 0);
2976 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2978 gtk_widget_show_all (x->menubar_widget);
2979 gtk_widget_size_request (x->menubar_widget, &req);
2981 FRAME_MENUBAR_HEIGHT (f) = req.height;
2983 /* The height has changed, resize outer widget and set columns
2984 rows to what we had before adding the menu bar. */
2985 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
2987 SET_FRAME_GARBAGED (f);
2988 UNBLOCK_INPUT;
2990 return 1;
2993 /* Get rid of the menu bar of frame F, and free its storage.
2994 This is used when deleting a frame, and when turning off the menu bar. */
2996 void
2997 free_frame_menubar (f)
2998 FRAME_PTR f;
3000 struct x_output *x = f->output_data.x;
3002 if (x->menubar_widget)
3004 BLOCK_INPUT;
3006 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
3007 /* The menubar and its children shall be deleted when removed from
3008 the container. */
3009 x->menubar_widget = 0;
3010 FRAME_MENUBAR_HEIGHT (f) = 0;
3012 /* The height has changed, resize outer widget and set columns
3013 rows to what we had before removing the menu bar. */
3014 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3016 SET_FRAME_GARBAGED (f);
3017 UNBLOCK_INPUT;
3023 /***********************************************************************
3024 Scroll bar functions
3025 ***********************************************************************/
3028 /* Setting scroll bar values invokes the callback. Use this variable
3029 to indicate that callback should do nothing. */
3031 int xg_ignore_gtk_scrollbar;
3033 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
3034 may be larger than 32 bits. Keep a mapping from integer index to widget
3035 pointers to get around the 32 bit limitation. */
3037 static struct
3039 GtkWidget **widgets;
3040 int max_size;
3041 int used;
3042 } id_to_widget;
3044 /* Grow this much every time we need to allocate more */
3046 #define ID_TO_WIDGET_INCR 32
3048 /* Store the widget pointer W in id_to_widget and return the integer index. */
3050 static int
3051 xg_store_widget_in_map (w)
3052 GtkWidget *w;
3054 int i;
3056 if (id_to_widget.max_size == id_to_widget.used)
3058 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
3060 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
3061 sizeof (GtkWidget *)*new_size);
3063 for (i = id_to_widget.max_size; i < new_size; ++i)
3064 id_to_widget.widgets[i] = 0;
3065 id_to_widget.max_size = new_size;
3068 /* Just loop over the array and find a free place. After all,
3069 how many scroll bars are we creating? Should be a small number.
3070 The check above guarantees we will find a free place. */
3071 for (i = 0; i < id_to_widget.max_size; ++i)
3073 if (! id_to_widget.widgets[i])
3075 id_to_widget.widgets[i] = w;
3076 ++id_to_widget.used;
3078 return i;
3082 /* Should never end up here */
3083 abort ();
3086 /* Remove pointer at IDX from id_to_widget.
3087 Called when scroll bar is destroyed. */
3089 static void
3090 xg_remove_widget_from_map (idx)
3091 int idx;
3093 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3095 id_to_widget.widgets[idx] = 0;
3096 --id_to_widget.used;
3100 /* Get the widget pointer at IDX from id_to_widget. */
3102 static GtkWidget *
3103 xg_get_widget_from_map (idx)
3104 int idx;
3106 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3107 return id_to_widget.widgets[idx];
3109 return 0;
3112 /* Return the scrollbar id for X Window WID on display DPY.
3113 Return -1 if WID not in id_to_widget. */
3116 xg_get_scroll_id_for_window (dpy, wid)
3117 Display *dpy;
3118 Window wid;
3120 int idx;
3121 GtkWidget *w;
3123 w = xg_win_to_widget (dpy, wid);
3125 if (w)
3127 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3128 if (id_to_widget.widgets[idx] == w)
3129 return idx;
3132 return -1;
3135 /* Callback invoked when scroll bar WIDGET is destroyed.
3136 DATA is the index into id_to_widget for WIDGET.
3137 We free pointer to last scroll bar values here and remove the index. */
3139 static void
3140 xg_gtk_scroll_destroy (widget, data)
3141 GtkWidget *widget;
3142 gpointer data;
3144 gpointer p;
3145 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3147 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
3148 xfree (p);
3149 xg_remove_widget_from_map (id);
3152 /* Callback for button press/release events. Used to start timer so that
3153 the scroll bar repetition timer in GTK gets handeled.
3154 Also, sets bar->dragging to Qnil when dragging (button release) is done.
3155 WIDGET is the scroll bar widget the event is for (not used).
3156 EVENT contains the event.
3157 USER_DATA points to the struct scrollbar structure.
3159 Returns FALSE to tell GTK that it shall continue propagate the event
3160 to widgets. */
3162 static gboolean
3163 scroll_bar_button_cb (widget, event, user_data)
3164 GtkWidget *widget;
3165 GdkEventButton *event;
3166 gpointer user_data;
3168 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
3169 xg_start_timer ();
3170 else if (event->type == GDK_BUTTON_RELEASE)
3172 struct scroll_bar *bar = (struct scroll_bar *) user_data;
3173 if (xg_timer) xg_stop_timer ();
3174 bar->dragging = Qnil;
3177 return FALSE;
3180 /* Create a scroll bar widget for frame F. Store the scroll bar
3181 in BAR.
3182 SCROLL_CALLBACK is the callback to invoke when the value of the
3183 bar changes.
3184 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3185 to set resources for the widget. */
3187 void
3188 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
3189 FRAME_PTR f;
3190 struct scroll_bar *bar;
3191 GCallback scroll_callback;
3192 char *scroll_bar_name;
3194 GtkWidget *wscroll;
3195 GtkWidget *webox;
3196 GtkObject *vadj;
3197 int scroll_id;
3199 /* Page, step increment values are not so important here, they
3200 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3201 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3202 0.1, 0.1, 0.1);
3204 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3205 webox = gtk_event_box_new ();
3206 gtk_widget_set_name (wscroll, scroll_bar_name);
3207 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3209 scroll_id = xg_store_widget_in_map (wscroll);
3211 g_signal_connect (G_OBJECT (wscroll),
3212 "value-changed",
3213 scroll_callback,
3214 (gpointer) bar);
3215 /* The EMACS_INT cast avoids a warning. */
3216 g_signal_connect (G_OBJECT (wscroll),
3217 "destroy",
3218 G_CALLBACK (xg_gtk_scroll_destroy),
3219 (gpointer) (EMACS_INT) scroll_id);
3221 /* Connect to button press and button release to detect if any scroll bar
3222 has the pointer. */
3223 g_signal_connect (G_OBJECT (wscroll),
3224 "button-press-event",
3225 G_CALLBACK (scroll_bar_button_cb),
3226 (gpointer) bar);
3227 g_signal_connect (G_OBJECT (wscroll),
3228 "button-release-event",
3229 G_CALLBACK (scroll_bar_button_cb),
3230 (gpointer) bar);
3232 /* The scroll bar widget does not draw on a window of its own. Instead
3233 it draws on the parent window, in this case the edit widget. So
3234 whenever the edit widget is cleared, the scroll bar needs to redraw
3235 also, which causes flicker. Put an event box between the edit widget
3236 and the scroll bar, so the scroll bar instead draws itself on the
3237 event box window. */
3238 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3239 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3242 /* Set the cursor to an arrow. */
3243 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3245 bar->x_window = scroll_id;
3248 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3250 void
3251 xg_show_scroll_bar (scrollbar_id)
3252 int scrollbar_id;
3254 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3255 if (w)
3256 gtk_widget_show_all (gtk_widget_get_parent (w));
3259 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3261 void
3262 xg_remove_scroll_bar (f, scrollbar_id)
3263 FRAME_PTR f;
3264 int scrollbar_id;
3266 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3267 if (w)
3269 GtkWidget *wparent = gtk_widget_get_parent (w);
3270 gtk_widget_destroy (w);
3271 gtk_widget_destroy (wparent);
3272 SET_FRAME_GARBAGED (f);
3276 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3277 in frame F.
3278 TOP/LEFT are the new pixel positions where the bar shall appear.
3279 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3281 void
3282 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3283 FRAME_PTR f;
3284 int scrollbar_id;
3285 int top;
3286 int left;
3287 int width;
3288 int height;
3291 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3293 if (wscroll)
3295 GtkWidget *wfixed = f->output_data.x->edit_widget;
3296 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3298 /* Move and resize to new values. */
3299 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3300 gtk_widget_set_size_request (wscroll, width, height);
3301 gtk_widget_queue_draw (wparent);
3302 gdk_window_process_all_updates ();
3303 /* GTK does not redraw until the main loop is entered again, but
3304 if there are no X events pending we will not enter it. So we sync
3305 here to get some events. */
3306 x_sync (f);
3307 SET_FRAME_GARBAGED (f);
3308 cancel_mouse_face (f);
3312 /* Set the thumb size and position of scroll bar BAR. We are currently
3313 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3315 void
3316 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3317 struct scroll_bar *bar;
3318 int portion, position, whole;
3320 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3322 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3324 if (wscroll && NILP (bar->dragging))
3326 GtkAdjustment *adj;
3327 gdouble shown;
3328 gdouble top;
3329 int size, value;
3330 int new_step;
3331 int changed = 0;
3333 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3335 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3336 rather than the real portion value. This makes the thumb less likely
3337 to resize and that looks better. */
3338 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3339 /* When the thumb is at the bottom, position == whole.
3340 So we need to increase `whole' to make space for the thumb. */
3341 whole += portion;
3343 if (whole <= 0)
3344 top = 0, shown = 1;
3345 else
3347 top = (gdouble) position / whole;
3348 shown = (gdouble) portion / whole;
3351 size = shown * XG_SB_RANGE;
3352 size = min (size, XG_SB_RANGE);
3353 size = max (size, 1);
3355 value = top * XG_SB_RANGE;
3356 value = min (value, XG_SB_MAX - size);
3357 value = max (value, XG_SB_MIN);
3359 /* Assume all lines are of equal size. */
3360 new_step = size / max (1, FRAME_LINES (f));
3362 if ((int) adj->page_size != size
3363 || (int) adj->step_increment != new_step)
3365 adj->page_size = size;
3366 adj->step_increment = new_step;
3367 /* Assume a page increment is about 95% of the page size */
3368 adj->page_increment = (int) (0.95*adj->page_size);
3369 changed = 1;
3372 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3374 GtkWidget *wfixed = f->output_data.x->edit_widget;
3376 BLOCK_INPUT;
3378 /* gtk_range_set_value invokes the callback. Set
3379 ignore_gtk_scrollbar to make the callback do nothing */
3380 xg_ignore_gtk_scrollbar = 1;
3382 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3383 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3384 else if (changed)
3385 gtk_adjustment_changed (adj);
3387 xg_ignore_gtk_scrollbar = 0;
3389 UNBLOCK_INPUT;
3395 /***********************************************************************
3396 Tool bar functions
3397 ***********************************************************************/
3398 /* The key for the data we put in the GtkImage widgets. The data is
3399 the image used by Emacs. We use this to see if we need to update
3400 the GtkImage with a new image. */
3401 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3403 /* The key for storing the latest modifiers so the activate callback can
3404 get them. */
3405 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3407 /* The key for storing the button widget in its proxy menu item. */
3408 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3410 /* The key for the data we put in the GtkImage widgets. The data is
3411 the stock name used by Emacs. We use this to see if we need to update
3412 the GtkImage with a new image. */
3413 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3415 /* As above, but this is used for named theme widgets, as opposed to
3416 stock items. */
3417 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3419 /* Callback function invoked when a tool bar item is pressed.
3420 W is the button widget in the tool bar that got pressed,
3421 CLIENT_DATA is an integer that is the index of the button in the
3422 tool bar. 0 is the first button. */
3424 static gboolean
3425 xg_tool_bar_button_cb (widget, event, user_data)
3426 GtkWidget *widget;
3427 GdkEventButton *event;
3428 gpointer user_data;
3430 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3431 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3432 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3433 return FALSE;
3437 /* Callback function invoked when a tool bar item is pressed.
3438 W is the button widget in the tool bar that got pressed,
3439 CLIENT_DATA is an integer that is the index of the button in the
3440 tool bar. 0 is the first button. */
3442 static void
3443 xg_tool_bar_callback (w, client_data)
3444 GtkWidget *w;
3445 gpointer client_data;
3447 /* The EMACS_INT cast avoids a warning. */
3448 int idx = (int) (EMACS_INT) client_data;
3449 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3450 XG_TOOL_BAR_LAST_MODIFIER);
3452 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3453 Lisp_Object key, frame;
3454 struct input_event event;
3455 EVENT_INIT (event);
3457 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3458 return;
3460 idx *= TOOL_BAR_ITEM_NSLOTS;
3462 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3463 XSETFRAME (frame, f);
3465 /* We generate two events here. The first one is to set the prefix
3466 to `(tool_bar)', see keyboard.c. */
3467 event.kind = TOOL_BAR_EVENT;
3468 event.frame_or_window = frame;
3469 event.arg = frame;
3470 kbd_buffer_store_event (&event);
3472 event.kind = TOOL_BAR_EVENT;
3473 event.frame_or_window = frame;
3474 event.arg = key;
3475 /* Convert between the modifier bits GDK uses and the modifier bits
3476 Emacs uses. This assumes GDK an X masks are the same, which they are when
3477 this is written. */
3478 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3479 kbd_buffer_store_event (&event);
3482 /* Callback function invoked when a tool bar item is pressed in a detached
3483 tool bar or the overflow drop down menu.
3484 We just call xg_tool_bar_callback.
3485 W is the menu item widget that got pressed,
3486 CLIENT_DATA is an integer that is the index of the button in the
3487 tool bar. 0 is the first button. */
3489 static void
3490 xg_tool_bar_proxy_callback (w, client_data)
3491 GtkWidget *w;
3492 gpointer client_data;
3494 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3495 XG_TOOL_BAR_PROXY_BUTTON));
3496 xg_tool_bar_callback (wbutton, client_data);
3499 /* This callback is called when a tool item should create a proxy item,
3500 such as for the overflow menu. Also called when the tool bar is detached.
3501 If we don't create a proxy menu item, the detached tool bar will be
3502 blank. */
3504 static gboolean
3505 xg_tool_bar_menu_proxy (toolitem, user_data)
3506 GtkToolItem *toolitem;
3507 gpointer user_data;
3509 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3510 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3511 GtkWidget *wmenuitem = gtk_image_menu_item_new ();
3512 GtkWidget *wmenuimage;
3514 if (gtk_button_get_use_stock (wbutton))
3515 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3516 GTK_ICON_SIZE_MENU);
3517 else
3519 GtkImage *wimage = GTK_IMAGE (gtk_bin_get_child (GTK_BIN (wbutton)));
3520 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3521 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3523 if (store_type == GTK_IMAGE_STOCK)
3525 gchar *stock_id;
3526 gtk_image_get_stock (wimage, &stock_id, NULL);
3527 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3529 else if (store_type == GTK_IMAGE_ICON_SET)
3531 GtkIconSet *icon_set;
3532 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3533 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3534 GTK_ICON_SIZE_MENU);
3536 else if (store_type == GTK_IMAGE_PIXBUF)
3538 gint width, height;
3540 if (settings &&
3541 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3542 &width, &height))
3544 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3546 src_pixbuf = gtk_image_get_pixbuf (wimage);
3547 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3548 GDK_INTERP_BILINEAR);
3550 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3552 else
3554 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3555 abort ();
3558 else if (store_type == GTK_IMAGE_ICON_NAME)
3560 const gchar *icon_name;
3561 GtkIconSize icon_size;
3563 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3564 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3565 GTK_ICON_SIZE_MENU);
3567 else
3569 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3570 abort ();
3573 if (wmenuimage)
3574 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3576 g_signal_connect (G_OBJECT (wmenuitem),
3577 "activate",
3578 GTK_SIGNAL_FUNC (xg_tool_bar_proxy_callback),
3579 user_data);
3581 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3582 (gpointer) wbutton);
3583 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3585 return TRUE;
3588 /* This callback is called when a tool bar is detached. We must set
3589 the height of the tool bar to zero when this happens so frame sizes
3590 are correctly calculated.
3591 WBOX is the handle box widget that enables detach/attach of the tool bar.
3592 W is the tool bar widget.
3593 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3595 static void
3596 xg_tool_bar_detach_callback (wbox, w, client_data)
3597 GtkHandleBox *wbox;
3598 GtkWidget *w;
3599 gpointer client_data;
3601 FRAME_PTR f = (FRAME_PTR) client_data;
3602 extern int x_gtk_whole_detached_tool_bar;
3604 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3605 NULL);
3607 if (f)
3609 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3611 /* When detaching a tool bar, not everything dissapear. There are
3612 a few pixels left that are used to drop the tool bar back into
3613 place. */
3614 FRAME_TOOLBAR_HEIGHT (f) = 2;
3616 /* The height has changed, resize outer widget and set columns
3617 rows to what we had before detaching the tool bar. */
3618 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3622 /* This callback is called when a tool bar is reattached. We must set
3623 the height of the tool bar when this happens so frame sizes
3624 are correctly calculated.
3625 WBOX is the handle box widget that enables detach/attach of the tool bar.
3626 W is the tool bar widget.
3627 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3629 static void
3630 xg_tool_bar_attach_callback (wbox, w, client_data)
3631 GtkHandleBox *wbox;
3632 GtkWidget *w;
3633 gpointer client_data;
3635 FRAME_PTR f = (FRAME_PTR) client_data;
3636 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3638 if (f)
3640 GtkRequisition req;
3642 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3644 gtk_widget_size_request (w, &req);
3645 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3647 /* The height has changed, resize outer widget and set columns
3648 rows to what we had before attaching the tool bar. */
3649 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
3653 /* This callback is called when the mouse enters or leaves a tool bar item.
3654 It is used for displaying and hiding the help text.
3655 W is the tool bar item, a button.
3656 EVENT is either an enter event or leave event.
3657 CLIENT_DATA is an integer that is the index of the button in the
3658 tool bar. 0 is the first button.
3660 Returns FALSE to tell GTK to keep processing this event. */
3662 static gboolean
3663 xg_tool_bar_help_callback (w, event, client_data)
3664 GtkWidget *w;
3665 GdkEventCrossing *event;
3666 gpointer client_data;
3668 /* The EMACS_INT cast avoids a warning. */
3669 int idx = (int) (EMACS_INT) client_data;
3670 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3671 Lisp_Object help, frame;
3673 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3674 return FALSE;
3676 if (event->type == GDK_ENTER_NOTIFY)
3678 idx *= TOOL_BAR_ITEM_NSLOTS;
3679 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3681 if (NILP (help))
3682 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3684 else
3685 help = Qnil;
3687 XSETFRAME (frame, f);
3688 kbd_buffer_store_help_event (frame, help);
3690 return FALSE;
3694 /* This callback is called when a tool bar item shall be redrawn.
3695 It modifies the expose event so that the GtkImage widget redraws the
3696 whole image. This to overcome a bug that makes GtkImage draw the image
3697 in the wrong place when it tries to redraw just a part of the image.
3698 W is the GtkImage to be redrawn.
3699 EVENT is the expose event for W.
3700 CLIENT_DATA is unused.
3702 Returns FALSE to tell GTK to keep processing this event. */
3704 static gboolean
3705 xg_tool_bar_item_expose_callback (w, event, client_data)
3706 GtkWidget *w;
3707 GdkEventExpose *event;
3708 gpointer client_data;
3710 gint width, height;
3712 gdk_drawable_get_size (event->window, &width, &height);
3714 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3715 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3717 event->area.x = max (0, event->area.x);
3718 event->area.y = max (0, event->area.y);
3720 event->area.width = max (width, event->area.width);
3721 event->area.height = max (height, event->area.height);
3723 return FALSE;
3726 /* Attach a tool bar to frame F. */
3728 static void
3729 xg_pack_tool_bar (f)
3730 FRAME_PTR f;
3732 struct x_output *x = f->output_data.x;
3733 int vbox_pos = x->menubar_widget ? 1 : 0;
3735 x->handlebox_widget = gtk_handle_box_new ();
3736 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3737 G_CALLBACK (xg_tool_bar_detach_callback), f);
3738 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3739 G_CALLBACK (xg_tool_bar_attach_callback), f);
3741 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3742 x->toolbar_widget);
3744 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3745 FALSE, FALSE, 0);
3747 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3748 vbox_pos);
3749 gtk_widget_show_all (x->handlebox_widget);
3752 /* Create a tool bar for frame F. */
3754 static void
3755 xg_create_tool_bar (f)
3756 FRAME_PTR f;
3758 struct x_output *x = f->output_data.x;
3759 GtkRequisition req;
3761 x->toolbar_widget = gtk_toolbar_new ();
3762 x->toolbar_detached = 0;
3764 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3766 /* We only have icons, so override any user setting. We could
3767 use the caption property of the toolbar item (see update_frame_tool_bar
3768 below), but some of those strings are long, making the toolbar so
3769 long it does not fit on the screen. The GtkToolbar widget makes every
3770 item equal size, so the longest caption determine the size of every
3771 tool bar item. I think the creators of the GtkToolbar widget
3772 counted on 4 or 5 character long strings. */
3773 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3774 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3775 GTK_ORIENTATION_HORIZONTAL);
3779 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3781 /* Find the right-to-left image named by RTL in the tool bar images for F.
3782 Returns IMAGE if RTL is not found. */
3784 static Lisp_Object
3785 find_rtl_image (f, image, rtl)
3786 FRAME_PTR f;
3787 Lisp_Object image;
3788 Lisp_Object rtl;
3790 int i;
3791 Lisp_Object file, rtl_name;
3792 struct gcpro gcpro1, gcpro2;
3793 GCPRO2 (file, rtl_name);
3795 rtl_name = Ffile_name_nondirectory (rtl);
3797 for (i = 0; i < f->n_tool_bar_items; ++i)
3799 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3800 if (!NILP (file = file_for_image (rtl_image)))
3802 file = call1 (intern ("file-name-sans-extension"),
3803 Ffile_name_nondirectory (file));
3804 if (EQ (Fequal (file, rtl_name), Qt))
3806 image = rtl_image;
3807 break;
3812 return image;
3815 /* Update the tool bar for frame F. Add new buttons and remove old. */
3817 void
3818 update_frame_tool_bar (f)
3819 FRAME_PTR f;
3821 int i;
3822 GtkRequisition old_req, new_req;
3823 struct x_output *x = f->output_data.x;
3824 int hmargin = 0, vmargin = 0;
3825 GtkToolbar *wtoolbar;
3826 GtkToolItem *ti;
3827 GtkTextDirection dir;
3828 int pack_tool_bar = x->handlebox_widget == NULL;
3830 if (! FRAME_GTK_WIDGET (f))
3831 return;
3833 BLOCK_INPUT;
3835 if (INTEGERP (Vtool_bar_button_margin)
3836 && XINT (Vtool_bar_button_margin) > 0)
3838 hmargin = XFASTINT (Vtool_bar_button_margin);
3839 vmargin = XFASTINT (Vtool_bar_button_margin);
3841 else if (CONSP (Vtool_bar_button_margin))
3843 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3844 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3845 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3847 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3848 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3849 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3852 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3853 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3854 i.e. zero. This means that margins less than
3855 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3856 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3857 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3859 if (! x->toolbar_widget)
3860 xg_create_tool_bar (f);
3862 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
3863 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &old_req);
3864 dir = gtk_widget_get_direction (x->toolbar_widget);
3866 for (i = 0; i < f->n_tool_bar_items; ++i)
3869 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3870 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3871 int idx;
3872 int img_id;
3873 int icon_size = 0;
3874 struct image *img = NULL;
3875 Lisp_Object image;
3876 Lisp_Object stock;
3877 GtkStockItem stock_item;
3878 char *stock_name = NULL;
3879 char *icon_name = NULL;
3880 Lisp_Object rtl;
3881 GtkWidget *wbutton = NULL;
3882 GtkWidget *weventbox;
3883 Lisp_Object func = intern ("x-gtk-map-stock");
3884 Lisp_Object specified_file;
3886 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i);
3888 if (ti)
3890 weventbox = gtk_bin_get_child (GTK_BIN (ti));
3891 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3894 image = PROP (TOOL_BAR_ITEM_IMAGES);
3896 /* Ignore invalid image specifications. */
3897 if (!valid_image_p (image))
3899 if (wbutton) gtk_widget_hide (wbutton);
3900 continue;
3903 specified_file = file_for_image (image);
3904 if (!NILP (specified_file) && EQ (Qt, Ffboundp (func)))
3905 stock = call1 (func, specified_file);
3907 if (! NILP (stock) && STRINGP (stock))
3909 stock_name = SSDATA (stock);
3910 if (stock_name[0] == 'n' && stock_name[1] == ':')
3912 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
3913 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
3915 icon_name = stock_name + 2;
3916 stock_name = NULL;
3917 stock = Qnil;
3919 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
3920 icon_name = NULL;
3921 else
3922 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3924 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
3925 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
3926 else
3928 stock = Qnil;
3929 stock_name = NULL;
3933 if (stock_name == NULL && icon_name == NULL)
3935 /* No stock image, or stock item not known. Try regular image. */
3937 /* If image is a vector, choose the image according to the
3938 button state. */
3939 if (dir == GTK_TEXT_DIR_RTL
3940 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
3941 && STRINGP (rtl))
3943 image = find_rtl_image (f, image, rtl);
3946 if (VECTORP (image))
3948 if (enabled_p)
3949 idx = (selected_p
3950 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3951 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3952 else
3953 idx = (selected_p
3954 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3955 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3957 xassert (ASIZE (image) >= idx);
3958 image = AREF (image, idx);
3960 else
3961 idx = -1;
3963 img_id = lookup_image (f, image);
3964 img = IMAGE_FROM_ID (f, img_id);
3965 prepare_image_for_display (f, img);
3967 if (img->load_failed_p || img->pixmap == None)
3969 if (ti)
3970 gtk_widget_hide_all (GTK_WIDGET (ti));
3971 else
3973 /* Insert an empty (non-image) button */
3974 weventbox = gtk_event_box_new ();
3975 wbutton = gtk_button_new ();
3976 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
3977 gtk_button_set_relief (GTK_BUTTON (wbutton),
3978 GTK_RELIEF_NONE);
3979 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
3980 ti = gtk_tool_item_new ();
3981 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3982 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
3984 continue;
3988 if (ti == NULL)
3990 GtkWidget *w;
3991 if (stock_name)
3993 w = gtk_image_new_from_stock (stock_name, icon_size);
3994 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
3995 (gpointer) xstrdup (stock_name),
3996 (GDestroyNotify) xfree);
3998 else if (icon_name)
4000 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4001 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4002 (gpointer) xstrdup (icon_name),
4003 (GDestroyNotify) xfree);
4005 else
4007 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4008 /* Save the image so we can see if an update is needed when
4009 this function is called again. */
4010 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4011 (gpointer)img->pixmap);
4014 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4015 wbutton = gtk_button_new ();
4016 gtk_button_set_focus_on_click (GTK_BUTTON (wbutton), FALSE);
4017 gtk_button_set_relief (GTK_BUTTON (wbutton), GTK_RELIEF_NONE);
4018 gtk_container_add (GTK_CONTAINER (wbutton), w);
4019 weventbox = gtk_event_box_new ();
4020 gtk_container_add (GTK_CONTAINER (weventbox), wbutton);
4021 ti = gtk_tool_item_new ();
4022 gtk_container_add (GTK_CONTAINER (ti), weventbox);
4023 gtk_toolbar_insert (GTK_TOOLBAR (x->toolbar_widget), ti, -1);
4026 /* The EMACS_INT cast avoids a warning. */
4027 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
4028 GTK_SIGNAL_FUNC (xg_tool_bar_menu_proxy),
4029 (gpointer) (EMACS_INT) i);
4031 g_signal_connect (G_OBJECT (wbutton), "clicked",
4032 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
4033 (gpointer) (EMACS_INT) i);
4035 gtk_widget_show_all (GTK_WIDGET (ti));
4038 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
4040 /* Catch expose events to overcome an annoying redraw bug, see
4041 comment for xg_tool_bar_item_expose_callback. */
4042 g_signal_connect (G_OBJECT (ti),
4043 "expose-event",
4044 G_CALLBACK (xg_tool_bar_item_expose_callback),
4047 gtk_widget_set_sensitive (wbutton, enabled_p);
4048 gtk_tool_item_set_homogeneous (ti, FALSE);
4050 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
4051 no distinction based on modifiers in the activate callback,
4052 so we have to do it ourselves. */
4053 g_signal_connect (wbutton, "button-release-event",
4054 GTK_SIGNAL_FUNC (xg_tool_bar_button_cb),
4055 NULL);
4057 g_object_set_data (G_OBJECT (wbutton), XG_FRAME_DATA, (gpointer)f);
4059 /* Use enter/leave notify to show help. We use the events
4060 rather than the GtkButton specific signals "enter" and
4061 "leave", so we can have only one callback. The event
4062 will tell us what kind of event it is. */
4063 /* The EMACS_INT cast avoids a warning. */
4064 g_signal_connect (G_OBJECT (weventbox),
4065 "enter-notify-event",
4066 G_CALLBACK (xg_tool_bar_help_callback),
4067 (gpointer) (EMACS_INT) i);
4068 g_signal_connect (G_OBJECT (weventbox),
4069 "leave-notify-event",
4070 G_CALLBACK (xg_tool_bar_help_callback),
4071 (gpointer) (EMACS_INT) i);
4073 else
4075 GtkWidget *wimage = gtk_bin_get_child (GTK_BIN (wbutton));
4076 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4077 XG_TOOL_BAR_IMAGE_DATA);
4078 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4079 XG_TOOL_BAR_STOCK_NAME);
4080 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4081 XG_TOOL_BAR_ICON_NAME);
4082 if (stock_name &&
4083 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4085 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4086 stock_name, icon_size);
4087 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4088 (gpointer) xstrdup (stock_name),
4089 (GDestroyNotify) xfree);
4090 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4091 NULL);
4092 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4094 else if (icon_name &&
4095 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4097 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4098 icon_name, icon_size);
4099 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4100 (gpointer) xstrdup (icon_name),
4101 (GDestroyNotify) xfree);
4102 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4103 NULL);
4104 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4105 NULL);
4107 else if (img && old_img != img->pixmap)
4109 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
4110 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4111 (gpointer)img->pixmap);
4113 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4114 NULL);
4115 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME, NULL);
4118 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4120 gtk_widget_set_sensitive (wbutton, enabled_p);
4121 gtk_widget_show_all (GTK_WIDGET (ti));
4124 #undef PROP
4127 /* Remove buttons not longer needed. We just hide them so they
4128 can be reused later on. */
4131 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (x->toolbar_widget), i++);
4132 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4133 } while (ti != NULL);
4135 new_req.height = 0;
4136 gtk_widget_size_request (GTK_WIDGET (wtoolbar), &new_req);
4138 if (pack_tool_bar && f->n_tool_bar_items != 0)
4139 xg_pack_tool_bar (f);
4141 if (new_req.height != 0
4142 && f->n_tool_bar_items != 0
4143 && old_req.height != new_req.height
4144 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
4146 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
4147 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
4150 UNBLOCK_INPUT;
4153 /* Deallocate all resources for the tool bar on frame F.
4154 Remove the tool bar. */
4156 void
4157 free_frame_tool_bar (f)
4158 FRAME_PTR f;
4160 struct x_output *x = f->output_data.x;
4162 if (x->toolbar_widget)
4164 int is_packed = x->handlebox_widget != 0;
4165 BLOCK_INPUT;
4166 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4167 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4168 if (is_packed)
4169 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4170 x->handlebox_widget);
4171 else
4172 gtk_widget_destroy (x->toolbar_widget);
4174 x->toolbar_widget = 0;
4175 x->handlebox_widget = 0;
4176 FRAME_TOOLBAR_HEIGHT (f) = 0;
4178 /* The height has changed, resize outer widget and set columns
4179 rows to what we had before removing the tool bar. */
4180 xg_frame_set_char_size (f, FRAME_COLS (f), FRAME_LINES (f));
4181 UNBLOCK_INPUT;
4187 /***********************************************************************
4188 Initializing
4189 ***********************************************************************/
4190 void
4191 xg_initialize ()
4193 GtkBindingSet *binding_set;
4195 #if HAVE_XFT
4196 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4197 we keep it permanently linked in. */
4198 XftInit (0);
4199 #endif
4201 gdpy_def = NULL;
4202 xg_ignore_gtk_scrollbar = 0;
4203 xg_detached_menus = 0;
4204 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4205 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4207 id_to_widget.max_size = id_to_widget.used = 0;
4208 id_to_widget.widgets = 0;
4210 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4211 bindings. It doesn't seem to be any way to remove properties,
4212 so we set it to VoidSymbol which in X means "no key". */
4213 gtk_settings_set_string_property (gtk_settings_get_default (),
4214 "gtk-menu-bar-accel",
4215 "VoidSymbol",
4216 EMACS_CLASS);
4218 /* Make GTK text input widgets use Emacs style keybindings. This is
4219 Emacs after all. */
4220 gtk_settings_set_string_property (gtk_settings_get_default (),
4221 "gtk-key-theme-name",
4222 "Emacs",
4223 EMACS_CLASS);
4225 /* Make dialogs close on C-g. Since file dialog inherits from
4226 dialog, this works for them also. */
4227 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
4228 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4229 "close", 0);
4231 /* Make menus close on C-g. */
4232 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
4233 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4234 "cancel", 0);
4237 #endif /* USE_GTK */
4239 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4240 (do not change this comment) */