(Highlight Interactively): Use double space to separate sentences.
[emacs.git] / src / gtkutil.c
blob9da3c5bf45728cbefe1e4ebf53acb266103e9e04
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
23 #ifdef USE_GTK
24 #include <string.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
31 #include "window.h"
32 #include "atimer.h"
33 #include "gtkutil.h"
34 #include "termhooks.h"
35 #include "keyboard.h"
36 #include "charset.h"
37 #include "coding.h"
38 #include <gdk/gdkkeysyms.h>
41 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
42 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
44 /* Avoid "differ in sign" warnings */
45 #define SSDATA(x) ((char *) SDATA (x))
48 /***********************************************************************
49 Display handling functions
50 ***********************************************************************/
52 #ifdef HAVE_GTK_MULTIDISPLAY
54 /* Return the GdkDisplay that corresponds to the X display DPY. */
56 static GdkDisplay *
57 xg_get_gdk_display (dpy)
58 Display *dpy;
60 return gdk_x11_lookup_xdisplay (dpy);
63 /* When the GTK widget W is to be created on a display for F that
64 is not the default display, set the display for W.
65 W can be a GtkMenu or a GtkWindow widget. */
67 static void
68 xg_set_screen (w, f)
69 GtkWidget *w;
70 FRAME_PTR f;
72 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
74 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
75 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
77 if (GTK_IS_MENU (w))
78 gtk_menu_set_screen (GTK_MENU (w), gscreen);
79 else
80 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
85 #else /* not HAVE_GTK_MULTIDISPLAY */
87 /* Make some defines so we can use the GTK 2.2 functions when
88 compiling with GTK 2.0. */
90 #define xg_set_screen(w, f)
91 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
92 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
93 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
94 #define gdk_x11_lookup_xdisplay(dpy) 0
95 #define GdkDisplay void
97 #endif /* not HAVE_GTK_MULTIDISPLAY */
99 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
100 *DPY is set to NULL if the display can't be opened.
102 Returns non-zero if display could be opened, zero if display could not
103 be opened, and less than zero if the GTK version doesn't support
104 multipe displays. */
107 xg_display_open (display_name, dpy)
108 char *display_name;
109 Display **dpy;
111 #ifdef HAVE_GTK_MULTIDISPLAY
112 GdkDisplay *gdpy;
114 gdpy = gdk_display_open (display_name);
115 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
117 return gdpy != NULL;
119 #else /* not HAVE_GTK_MULTIDISPLAY */
121 return -1;
122 #endif /* not HAVE_GTK_MULTIDISPLAY */
126 /* Close display DPY. */
128 void
129 xg_display_close (Display *dpy)
131 #ifdef HAVE_GTK_MULTIDISPLAY
132 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
134 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
135 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
136 we can continue running, but there will be memory leaks. */
138 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
140 /* If this is the default display, we must change it before calling
141 dispose, otherwise it will crash. */
142 if (gdk_display_get_default () == gdpy)
144 struct x_display_info *dpyinfo;
145 Display *new_dpy = 0;
146 GdkDisplay *gdpy_new;
148 /* Find another display. */
149 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
150 if (dpyinfo->display != dpy)
152 new_dpy = dpyinfo->display;
153 break;
156 if (! new_dpy) return; /* Emacs will exit anyway. */
158 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
159 gdk_display_manager_set_default_display (gdk_display_manager_get (),
160 gdpy_new);
163 g_object_run_dispose (G_OBJECT (gdpy));
165 #else
166 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
167 gdk_display_close (gdpy);
168 #endif
169 #endif /* HAVE_GTK_MULTIDISPLAY */
173 /***********************************************************************
174 Utility functions
175 ***********************************************************************/
176 /* The timer for scroll bar repetition and menu bar timeouts.
177 NULL if no timer is started. */
178 static struct atimer *xg_timer;
181 /* The next two variables and functions are taken from lwlib. */
182 static widget_value *widget_value_free_list;
183 static int malloc_cpt;
185 /* Allocate a widget_value structure, either by taking one from the
186 widget_value_free_list or by malloc:ing a new one.
188 Return a pointer to the allocated structure. */
190 widget_value *
191 malloc_widget_value ()
193 widget_value *wv;
194 if (widget_value_free_list)
196 wv = widget_value_free_list;
197 widget_value_free_list = wv->free_list;
198 wv->free_list = 0;
200 else
202 wv = (widget_value *) malloc (sizeof (widget_value));
203 malloc_cpt++;
205 memset (wv, 0, sizeof (widget_value));
206 return wv;
209 /* This is analogous to free. It frees only what was allocated
210 by malloc_widget_value, and no substructures. */
212 void
213 free_widget_value (wv)
214 widget_value *wv;
216 if (wv->free_list)
217 abort ();
219 if (malloc_cpt > 25)
221 /* When the number of already allocated cells is too big,
222 We free it. */
223 free (wv);
224 malloc_cpt--;
226 else
228 wv->free_list = widget_value_free_list;
229 widget_value_free_list = wv;
234 /* Create and return the cursor to be used for popup menus and
235 scroll bars on display DPY. */
237 GdkCursor *
238 xg_create_default_cursor (dpy)
239 Display *dpy;
241 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
242 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
245 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
247 static GdkPixbuf *
248 xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
249 GdkPixmap *gpix;
250 GdkPixmap *gmask;
251 GdkColormap *cmap;
253 int x, y, width, height, rowstride, mask_rowstride;
254 GdkPixbuf *icon_buf, *tmp_buf;
255 guchar *pixels;
256 guchar *mask_pixels;
258 gdk_drawable_get_size (gpix, &width, &height);
259 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
260 0, 0, 0, 0, width, height);
261 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
262 g_object_unref (G_OBJECT (tmp_buf));
264 if (gmask)
266 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
267 gmask,
268 NULL,
269 0, 0, 0, 0,
270 width, height);
271 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
272 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
273 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
274 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
275 int y;
277 for (y = 0; y < height; ++y)
279 guchar *iconptr, *maskptr;
280 int x;
282 iconptr = pixels + y * rowstride;
283 maskptr = mask_pixels + y * mask_rowstride;
285 for (x = 0; x < width; ++x)
287 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
288 just R is sufficient. */
289 if (maskptr[0] == 0)
290 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
292 iconptr += rowstride/width;
293 maskptr += mask_rowstride/width;
297 g_object_unref (G_OBJECT (mask_buf));
300 return icon_buf;
303 /* For the image defined in IMG, make and return a GtkImage. For displays with
304 8 planes or less we must make a GdkPixbuf and apply the mask manually.
305 Otherwise the highlightning and dimming the tool bar code in GTK does
306 will look bad. For display with more than 8 planes we just use the
307 pixmap and mask directly. For monochrome displays, GTK doesn't seem
308 able to use external pixmaps, it looks bad whatever we do.
309 The image is defined on the display where frame F is.
310 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
311 If OLD_WIDGET is NULL, a new widget is constructed and returned.
312 If OLD_WIDGET is not NULL, that widget is modified. */
314 static GtkWidget *
315 xg_get_image_for_pixmap (f, img, widget, old_widget)
316 FRAME_PTR f;
317 struct image *img;
318 GtkWidget *widget;
319 GtkImage *old_widget;
321 GdkPixmap *gpix;
322 GdkPixmap *gmask;
323 GdkDisplay *gdpy;
325 /* If we are on a one bit display, let GTK do all the image handling.
326 This seems to be the only way to make insensitive and activated icons
327 look good. */
328 if (x_screen_planes (f) == 1)
330 Lisp_Object specified_file = Qnil;
331 Lisp_Object tail;
332 extern Lisp_Object QCfile;
334 for (tail = XCDR (img->spec);
335 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
336 tail = XCDR (XCDR (tail)))
337 if (EQ (XCAR (tail), QCfile))
338 specified_file = XCAR (XCDR (tail));
340 if (STRINGP (specified_file))
343 Lisp_Object file = Qnil;
344 struct gcpro gcpro1;
345 GCPRO1 (file);
347 file = x_find_image_file (specified_file);
348 /* We already loaded the image once before calling this
349 function, so this should not fail. */
350 xassert (STRINGP (file) != 0);
352 if (! old_widget)
353 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
354 else
355 gtk_image_set_from_file (old_widget, SSDATA (file));
357 UNGCPRO;
358 return GTK_WIDGET (old_widget);
362 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
363 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
364 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
366 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
368 if (! old_widget)
369 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
370 else
371 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
373 else
376 /* This is a workaround to make icons look good on pseudo color
377 displays. Apparently GTK expects the images to have an alpha
378 channel. If they don't, insensitive and activated icons will
379 look bad. This workaround does not work on monochrome displays,
380 and is not needed on true color/static color displays (i.e.
381 16 bits and higher). */
382 GdkColormap *cmap = gtk_widget_get_colormap (widget);
383 GdkPixbuf *icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
385 if (! old_widget)
386 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
387 else
388 gtk_image_set_from_pixbuf (old_widget, icon_buf);
390 g_object_unref (G_OBJECT (icon_buf));
393 g_object_unref (G_OBJECT (gpix));
394 if (gmask) g_object_unref (G_OBJECT (gmask));
396 return GTK_WIDGET (old_widget);
400 /* Set CURSOR on W and all widgets W contain. We must do like this
401 for scroll bars and menu because they create widgets internally,
402 and it is those widgets that are visible. */
404 static void
405 xg_set_cursor (w, cursor)
406 GtkWidget *w;
407 GdkCursor *cursor;
409 GList *children = gdk_window_peek_children (w->window);
411 gdk_window_set_cursor (w->window, cursor);
413 /* The scroll bar widget has more than one GDK window (had to look at
414 the source to figure this out), and there is no way to set cursor
415 on widgets in GTK. So we must set the cursor for all GDK windows.
416 Ditto for menus. */
418 for ( ; children; children = g_list_next (children))
419 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
422 /* Timer function called when a timeout occurs for xg_timer.
423 This function processes all GTK events in a recursive event loop.
424 This is done because GTK timer events are not seen by Emacs event
425 detection, Emacs only looks for X events. When a scroll bar has the
426 pointer (detected by button press/release events below) an Emacs
427 timer is started, and this function can then check if the GTK timer
428 has expired by calling the GTK event loop.
429 Also, when a menu is active, it has a small timeout before it
430 pops down the sub menu under it. */
432 static void
433 xg_process_timeouts (timer)
434 struct atimer *timer;
436 BLOCK_INPUT;
437 /* Ideally we would like to just handle timer events, like the Xt version
438 of this does in xterm.c, but there is no such feature in GTK. */
439 while (gtk_events_pending ())
440 gtk_main_iteration ();
441 UNBLOCK_INPUT;
444 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
445 xg_process_timeouts is called when the timer expires. The timer
446 started is continuous, i.e. runs until xg_stop_timer is called. */
448 static void
449 xg_start_timer ()
451 if (! xg_timer)
453 EMACS_TIME interval;
454 EMACS_SET_SECS_USECS (interval, 0, 100000);
455 xg_timer = start_atimer (ATIMER_CONTINUOUS,
456 interval,
457 xg_process_timeouts,
462 /* Stop the xg_timer if started. */
464 static void
465 xg_stop_timer ()
467 if (xg_timer)
469 cancel_atimer (xg_timer);
470 xg_timer = 0;
474 /* Insert NODE into linked LIST. */
476 static void
477 xg_list_insert (xg_list_node *list, xg_list_node *node)
479 xg_list_node *list_start = list->next;
481 if (list_start) list_start->prev = node;
482 node->next = list_start;
483 node->prev = 0;
484 list->next = node;
487 /* Remove NODE from linked LIST. */
489 static void
490 xg_list_remove (xg_list_node *list, xg_list_node *node)
492 xg_list_node *list_start = list->next;
493 if (node == list_start)
495 list->next = node->next;
496 if (list->next) list->next->prev = 0;
498 else
500 node->prev->next = node->next;
501 if (node->next) node->next->prev = node->prev;
505 /* Allocate and return a utf8 version of STR. If STR is already
506 utf8 or NULL, just return STR.
507 If not, a new string is allocated and the caller must free the result
508 with g_free. */
510 static char *
511 get_utf8_string (str)
512 char *str;
514 char *utf8_str = str;
516 /* If not UTF-8, try current locale. */
517 if (str && !g_utf8_validate (str, -1, NULL))
518 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
520 return utf8_str;
525 /***********************************************************************
526 General functions for creating widgets, resizing, events, e.t.c.
527 ***********************************************************************/
529 /* Make a geometry string and pass that to GTK. It seems this is the
530 only way to get geometry position right if the user explicitly
531 asked for a position when starting Emacs.
532 F is the frame we shall set geometry for. */
534 static void
535 xg_set_geometry (f)
536 FRAME_PTR f;
538 if (f->size_hint_flags & USPosition)
540 int left = f->left_pos;
541 int xneg = f->size_hint_flags & XNegative;
542 int top = f->top_pos;
543 int yneg = f->size_hint_flags & YNegative;
544 char geom_str[32];
546 if (xneg)
547 left = -left;
548 if (yneg)
549 top = -top;
551 sprintf (geom_str, "=%dx%d%c%d%c%d",
552 FRAME_PIXEL_WIDTH (f),
553 FRAME_TOTAL_PIXEL_HEIGHT (f),
554 (xneg ? '-' : '+'), left,
555 (yneg ? '-' : '+'), top);
557 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
558 geom_str))
559 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
560 } else if (f->size_hint_flags & PPosition) {
561 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
562 f->left_pos, f->top_pos);
567 /* Resize the outer window of frame F after chainging the height.
568 This happend when the menu bar or the tool bar is added or removed.
569 COLUMNS/ROWS is the size the edit area shall have after the resize. */
571 static void
572 xg_resize_outer_widget (f, columns, rows)
573 FRAME_PTR f;
574 int columns;
575 int rows;
577 /* If we are not mapped yet, set geometry once again, as window
578 height now have changed. */
579 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
580 xg_set_geometry (f);
582 xg_frame_set_char_size (f, columns, rows);
583 gdk_window_process_all_updates ();
586 /* Function to handle resize of our widgets. Since Emacs has some layouts
587 that does not fit well with GTK standard containers, we do most layout
588 manually.
589 F is the frame to resize.
590 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
592 void
593 xg_resize_widgets (f, pixelwidth, pixelheight)
594 FRAME_PTR f;
595 int pixelwidth, pixelheight;
597 int mbheight = FRAME_MENUBAR_HEIGHT (f);
598 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
599 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
600 - mbheight - tbheight));
601 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
603 if (FRAME_GTK_WIDGET (f)
604 && (columns != FRAME_COLS (f)
605 || rows != FRAME_LINES (f)
606 || pixelwidth != FRAME_PIXEL_WIDTH (f)
607 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
609 struct x_output *x = f->output_data.x;
610 GtkAllocation all;
612 all.y = mbheight + tbheight;
613 all.x = 0;
615 all.width = pixelwidth;
616 all.height = pixelheight - mbheight - tbheight;
618 gtk_widget_size_allocate (x->edit_widget, &all);
620 change_frame_size (f, rows, columns, 0, 1, 0);
621 SET_FRAME_GARBAGED (f);
622 cancel_mouse_face (f);
627 /* Update our widget size to be COLS/ROWS characters for frame F. */
629 void
630 xg_frame_set_char_size (f, cols, rows)
631 FRAME_PTR f;
632 int cols;
633 int rows;
635 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
636 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
637 int pixelwidth;
639 /* Take into account the size of the scroll bar. Always use the
640 number of columns occupied by the scroll bar here otherwise we
641 might end up with a frame width that is not a multiple of the
642 frame's character width which is bad for vertically split
643 windows. */
644 f->scroll_bar_actual_width
645 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
647 compute_fringe_widths (f, 0);
649 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
650 after calculating that value. */
651 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
653 /* Must resize our top level widget. Font size may have changed,
654 but not rows/cols. */
655 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
656 pixelwidth, pixelheight);
657 xg_resize_widgets (f, pixelwidth, pixelheight);
658 x_wm_set_size_hint (f, 0, 0);
659 SET_FRAME_GARBAGED (f);
660 cancel_mouse_face (f);
663 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
664 Must be done like this, because GtkWidget:s can have "hidden"
665 X Window that aren't accessible.
667 Return 0 if no widget match WDESC. */
669 GtkWidget *
670 xg_win_to_widget (dpy, wdesc)
671 Display *dpy;
672 Window wdesc;
674 gpointer gdkwin;
675 GtkWidget *gwdesc = 0;
677 BLOCK_INPUT;
679 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
680 wdesc);
681 if (gdkwin)
683 GdkEvent event;
684 event.any.window = gdkwin;
685 gwdesc = gtk_get_event_widget (&event);
688 UNBLOCK_INPUT;
689 return gwdesc;
692 /* Fill in the GdkColor C so that it represents PIXEL.
693 W is the widget that color will be used for. Used to find colormap. */
695 static void
696 xg_pix_to_gcolor (w, pixel, c)
697 GtkWidget *w;
698 unsigned long pixel;
699 GdkColor *c;
701 GdkColormap *map = gtk_widget_get_colormap (w);
702 gdk_colormap_query_color (map, pixel, c);
705 /* Create and set up the GTK widgets for frame F.
706 Return 0 if creation failed, non-zero otherwise. */
709 xg_create_frame_widgets (f)
710 FRAME_PTR f;
712 GtkWidget *wtop;
713 GtkWidget *wvbox;
714 GtkWidget *wfixed;
715 GdkColor bg;
716 GtkRcStyle *style;
717 int i;
718 char *title = 0;
720 BLOCK_INPUT;
722 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
723 xg_set_screen (wtop, f);
725 wvbox = gtk_vbox_new (FALSE, 0);
726 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
728 if (! wtop || ! wvbox || ! wfixed)
730 if (wtop) gtk_widget_destroy (wtop);
731 if (wvbox) gtk_widget_destroy (wvbox);
732 if (wfixed) gtk_widget_destroy (wfixed);
734 UNBLOCK_INPUT;
735 return 0;
738 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
739 gtk_widget_set_name (wtop, EMACS_CLASS);
740 gtk_widget_set_name (wvbox, "pane");
741 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
743 /* If this frame has a title or name, set it in the title bar. */
744 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
745 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
747 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
749 FRAME_GTK_OUTER_WIDGET (f) = wtop;
750 FRAME_GTK_WIDGET (f) = wfixed;
751 f->output_data.x->vbox_widget = wvbox;
753 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
755 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
756 FRAME_PIXEL_HEIGHT (f));
758 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
759 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
761 if (FRAME_EXTERNAL_TOOL_BAR (f))
762 update_frame_tool_bar (f);
764 /* The tool bar is created but first there are no items in it.
765 This causes it to be zero height. Later items are added, but then
766 the frame is already mapped, so there is a "jumping" resize.
767 This makes geometry handling difficult, for example -0-0 will end
768 up in the wrong place as tool bar height has not been taken into account.
769 So we cheat a bit by setting a height that is what it will have
770 later on when tool bar items are added. */
771 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
772 FRAME_TOOLBAR_HEIGHT (f) = 38;
775 /* We don't want this widget double buffered, because we draw on it
776 with regular X drawing primitives, so from a GTK/GDK point of
777 view, the widget is totally blank. When an expose comes, this
778 will make the widget blank, and then Emacs redraws it. This flickers
779 a lot, so we turn off double buffering. */
780 gtk_widget_set_double_buffered (wfixed, FALSE);
782 /* GTK documents says use gtk_window_set_resizable. But then a user
783 can't shrink the window from its starting size. */
784 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
785 gtk_window_set_wmclass (GTK_WINDOW (wtop),
786 SSDATA (Vx_resource_name),
787 SSDATA (Vx_resource_class));
789 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
790 GTK is to destroy the widget. We want Emacs to do that instead. */
791 g_signal_connect (G_OBJECT (wtop), "delete-event",
792 G_CALLBACK (gtk_true), 0);
794 /* Convert our geometry parameters into a geometry string
795 and specify it.
796 GTK will itself handle calculating the real position this way. */
797 xg_set_geometry (f);
799 gtk_widget_add_events (wfixed,
800 GDK_POINTER_MOTION_MASK
801 | GDK_EXPOSURE_MASK
802 | GDK_BUTTON_PRESS_MASK
803 | GDK_BUTTON_RELEASE_MASK
804 | GDK_KEY_PRESS_MASK
805 | GDK_ENTER_NOTIFY_MASK
806 | GDK_LEAVE_NOTIFY_MASK
807 | GDK_FOCUS_CHANGE_MASK
808 | GDK_STRUCTURE_MASK
809 | GDK_VISIBILITY_NOTIFY_MASK);
811 /* Must realize the windows so the X window gets created. It is used
812 by callers of this function. */
813 gtk_widget_realize (wfixed);
814 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
816 /* Since GTK clears its window by filling with the background color,
817 we must keep X and GTK background in sync. */
818 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
819 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
821 /* Also, do not let any background pixmap to be set, this looks very
822 bad as Emacs overwrites the background pixmap with its own idea
823 of background color. */
824 style = gtk_widget_get_modifier_style (wfixed);
826 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
827 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
828 gtk_widget_modify_style (wfixed, style);
830 /* GTK does not set any border, and they look bad with GTK. */
831 f->border_width = 0;
832 f->internal_border_width = 0;
834 UNBLOCK_INPUT;
836 return 1;
839 /* Set the normal size hints for the window manager, for frame F.
840 FLAGS is the flags word to use--or 0 meaning preserve the flags
841 that the window now has.
842 If USER_POSITION is nonzero, we set the User Position
843 flag (this is useful when FLAGS is 0). */
845 void
846 x_wm_set_size_hint (f, flags, user_position)
847 FRAME_PTR f;
848 long flags;
849 int user_position;
851 if (FRAME_GTK_OUTER_WIDGET (f))
853 /* Must use GTK routines here, otherwise GTK resets the size hints
854 to its own defaults. */
855 GdkGeometry size_hints;
856 gint hint_flags = 0;
857 int base_width, base_height;
858 int min_rows = 0, min_cols = 0;
859 int win_gravity = f->win_gravity;
861 if (flags)
863 memset (&size_hints, 0, sizeof (size_hints));
864 f->output_data.x->size_hints = size_hints;
865 f->output_data.x->hint_flags = hint_flags;
867 else
868 flags = f->size_hint_flags;
870 size_hints = f->output_data.x->size_hints;
871 hint_flags = f->output_data.x->hint_flags;
873 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
874 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
875 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
877 hint_flags |= GDK_HINT_BASE_SIZE;
878 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
879 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
880 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
882 check_frame_size (f, &min_rows, &min_cols);
884 size_hints.base_width = base_width;
885 size_hints.base_height = base_height;
886 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
887 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
890 /* These currently have a one to one mapping with the X values, but I
891 don't think we should rely on that. */
892 hint_flags |= GDK_HINT_WIN_GRAVITY;
893 size_hints.win_gravity = 0;
894 if (win_gravity == NorthWestGravity)
895 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
896 else if (win_gravity == NorthGravity)
897 size_hints.win_gravity = GDK_GRAVITY_NORTH;
898 else if (win_gravity == NorthEastGravity)
899 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
900 else if (win_gravity == WestGravity)
901 size_hints.win_gravity = GDK_GRAVITY_WEST;
902 else if (win_gravity == CenterGravity)
903 size_hints.win_gravity = GDK_GRAVITY_CENTER;
904 else if (win_gravity == EastGravity)
905 size_hints.win_gravity = GDK_GRAVITY_EAST;
906 else if (win_gravity == SouthWestGravity)
907 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
908 else if (win_gravity == SouthGravity)
909 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
910 else if (win_gravity == SouthEastGravity)
911 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
912 else if (win_gravity == StaticGravity)
913 size_hints.win_gravity = GDK_GRAVITY_STATIC;
915 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
916 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
917 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
919 if (user_position)
921 hint_flags &= ~GDK_HINT_POS;
922 hint_flags |= GDK_HINT_USER_POS;
925 BLOCK_INPUT;
927 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
928 FRAME_GTK_OUTER_WIDGET (f),
929 &size_hints,
930 hint_flags);
932 f->output_data.x->size_hints = size_hints;
933 f->output_data.x->hint_flags = hint_flags;
934 UNBLOCK_INPUT;
938 /* Change background color of a frame.
939 Since GTK uses the background colour to clear the window, we must
940 keep the GTK and X colors in sync.
941 F is the frame to change,
942 BG is the pixel value to change to. */
944 void
945 xg_set_background_color (f, bg)
946 FRAME_PTR f;
947 unsigned long bg;
949 if (FRAME_GTK_WIDGET (f))
951 GdkColor gdk_bg;
953 BLOCK_INPUT;
954 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
955 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
956 UNBLOCK_INPUT;
961 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
962 functions so GTK does not overwrite the icon. */
964 void
965 xg_set_frame_icon (f, icon_pixmap, icon_mask)
966 FRAME_PTR f;
967 Pixmap icon_pixmap;
968 Pixmap icon_mask;
970 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
971 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
972 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
973 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
975 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
980 /***********************************************************************
981 Dialog functions
982 ***********************************************************************/
983 /* Return the dialog title to use for a dialog of type KEY.
984 This is the encoding used by lwlib. We use the same for GTK. */
986 static char *
987 get_dialog_title (char key)
989 char *title = "";
991 switch (key) {
992 case 'E': case 'e':
993 title = "Error";
994 break;
996 case 'I': case 'i':
997 title = "Information";
998 break;
1000 case 'L': case 'l':
1001 title = "Prompt";
1002 break;
1004 case 'P': case 'p':
1005 title = "Prompt";
1006 break;
1008 case 'Q': case 'q':
1009 title = "Question";
1010 break;
1013 return title;
1016 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1017 the dialog, but return TRUE so the event does not propagate further
1018 in GTK. This prevents GTK from destroying the dialog widget automatically
1019 and we can always destrou the widget manually, regardles of how
1020 it was popped down (button press or WM_DELETE_WINDOW).
1021 W is the dialog widget.
1022 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1023 user_data is NULL (not used).
1025 Returns TRUE to end propagation of event. */
1027 static gboolean
1028 dialog_delete_callback (w, event, user_data)
1029 GtkWidget *w;
1030 GdkEvent *event;
1031 gpointer user_data;
1033 gtk_widget_unmap (w);
1034 return TRUE;
1037 /* Create a popup dialog window. See also xg_create_widget below.
1038 WV is a widget_value describing the dialog.
1039 SELECT_CB is the callback to use when a button has been pressed.
1040 DEACTIVATE_CB is the callback to use when the dialog pops down.
1042 Returns the GTK dialog widget. */
1044 static GtkWidget *
1045 create_dialog (wv, select_cb, deactivate_cb)
1046 widget_value *wv;
1047 GCallback select_cb;
1048 GCallback deactivate_cb;
1050 char *title = get_dialog_title (wv->name[0]);
1051 int total_buttons = wv->name[1] - '0';
1052 int right_buttons = wv->name[4] - '0';
1053 int left_buttons;
1054 int button_nr = 0;
1055 int button_spacing = 10;
1056 GtkWidget *wdialog = gtk_dialog_new ();
1057 widget_value *item;
1058 GtkBox *cur_box;
1059 GtkWidget *wvbox;
1060 GtkWidget *whbox_up;
1061 GtkWidget *whbox_down;
1063 /* If the number of buttons is greater than 4, make two rows of buttons
1064 instead. This looks better. */
1065 int make_two_rows = total_buttons > 4;
1067 if (right_buttons == 0) right_buttons = total_buttons/2;
1068 left_buttons = total_buttons - right_buttons;
1070 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1071 gtk_widget_set_name (wdialog, "emacs-dialog");
1073 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1075 if (make_two_rows)
1077 wvbox = gtk_vbox_new (TRUE, button_spacing);
1078 whbox_up = gtk_hbox_new (FALSE, 0);
1079 whbox_down = gtk_hbox_new (FALSE, 0);
1081 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1082 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1083 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1085 cur_box = GTK_BOX (whbox_up);
1088 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1089 G_CALLBACK (dialog_delete_callback), 0);
1091 if (deactivate_cb)
1093 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1094 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1097 for (item = wv->contents; item; item = item->next)
1099 char *utf8_label = get_utf8_string (item->value);
1100 GtkWidget *w;
1101 GtkRequisition req;
1103 if (item->name && strcmp (item->name, "message") == 0)
1105 /* This is the text part of the dialog. */
1106 w = gtk_label_new (utf8_label);
1107 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1108 gtk_label_new (""),
1109 FALSE, FALSE, 0);
1110 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1111 TRUE, TRUE, 0);
1112 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1114 /* Try to make dialog look better. Must realize first so
1115 the widget can calculate the size it needs. */
1116 gtk_widget_realize (w);
1117 gtk_widget_size_request (w, &req);
1118 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1119 req.height);
1120 if (item->value && strlen (item->value) > 0)
1121 button_spacing = 2*req.width/strlen (item->value);
1123 else
1125 /* This is one button to add to the dialog. */
1126 w = gtk_button_new_with_label (utf8_label);
1127 if (! item->enabled)
1128 gtk_widget_set_sensitive (w, FALSE);
1129 if (select_cb)
1130 g_signal_connect (G_OBJECT (w), "clicked",
1131 select_cb, item->call_data);
1133 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1134 if (++button_nr == left_buttons)
1136 if (make_two_rows)
1137 cur_box = GTK_BOX (whbox_down);
1138 else
1139 gtk_box_pack_start (cur_box,
1140 gtk_label_new (""),
1141 TRUE, TRUE,
1142 button_spacing);
1146 if (utf8_label && utf8_label != item->value)
1147 g_free (utf8_label);
1150 return wdialog;
1155 /***********************************************************************
1156 File dialog functions
1157 ***********************************************************************/
1158 /* Return non-zero if the old file selection dialog is being used.
1159 Return zero if not. */
1162 xg_uses_old_file_dialog ()
1164 #ifdef HAVE_GTK_FILE_BOTH
1165 extern int x_use_old_gtk_file_dialog;
1166 return x_use_old_gtk_file_dialog;
1167 #else /* ! HAVE_GTK_FILE_BOTH */
1169 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1170 return 1;
1171 #else
1172 return 0;
1173 #endif
1175 #endif /* ! HAVE_GTK_FILE_BOTH */
1179 /* Function that is called when the file dialog pops down.
1180 W is the dialog widget, RESPONSE is the response code.
1181 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1183 static void
1184 xg_file_response_cb (w,
1185 response,
1186 user_data)
1187 GtkDialog *w;
1188 gint response;
1189 gpointer user_data;
1191 int *ptr = (int *) user_data;
1192 *ptr = response;
1196 /* Destroy the dialog. This makes it pop down. */
1198 static Lisp_Object
1199 pop_down_file_dialog (arg)
1200 Lisp_Object arg;
1202 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1203 BLOCK_INPUT;
1204 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1205 UNBLOCK_INPUT;
1206 return Qnil;
1209 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1211 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1213 /* Return the selected file for file chooser dialog W.
1214 The returned string must be free:d. */
1216 static char *
1217 xg_get_file_name_from_chooser (w)
1218 GtkWidget *w;
1220 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1223 /* Read a file name from the user using a file chooser dialog.
1224 F is the current frame.
1225 PROMPT is a prompt to show to the user. May not be NULL.
1226 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1227 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1228 file. *FUNC is set to a function that can be used to retrieve the
1229 selected file name from the returned widget.
1231 Returns the created widget. */
1233 static GtkWidget *
1234 xg_get_file_with_chooser (f, prompt, default_filename,
1235 mustmatch_p, only_dir_p, func)
1236 FRAME_PTR f;
1237 char *prompt;
1238 char *default_filename;
1239 int mustmatch_p, only_dir_p;
1240 xg_get_file_func *func;
1242 GtkWidget *filewin;
1243 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1244 GtkFileChooserAction action = (mustmatch_p ?
1245 GTK_FILE_CHOOSER_ACTION_OPEN :
1246 GTK_FILE_CHOOSER_ACTION_SAVE);
1248 if (only_dir_p)
1249 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1251 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1252 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1253 (mustmatch_p || only_dir_p ?
1254 GTK_STOCK_OPEN : GTK_STOCK_OK),
1255 GTK_RESPONSE_OK,
1256 NULL);
1257 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1259 if (default_filename)
1261 Lisp_Object file;
1262 struct gcpro gcpro1;
1263 GCPRO1 (file);
1265 file = build_string (default_filename);
1267 /* File chooser does not understand ~/... in the file name. It must be
1268 an absolute name starting with /. */
1269 if (default_filename[0] != '/')
1270 file = Fexpand_file_name (file, Qnil);
1272 default_filename = SSDATA (file);
1273 if (Ffile_directory_p (file))
1274 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1275 default_filename);
1276 else
1277 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1278 default_filename);
1280 UNGCPRO;
1283 *func = xg_get_file_name_from_chooser;
1284 return filewin;
1286 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1288 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1290 /* Return the selected file for file selector dialog W.
1291 The returned string must be free:d. */
1293 static char *
1294 xg_get_file_name_from_selector (w)
1295 GtkWidget *w;
1297 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1298 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1301 /* Create a file selection dialog.
1302 F is the current frame.
1303 PROMPT is a prompt to show to the user. May not be NULL.
1304 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1305 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1306 file. *FUNC is set to a function that can be used to retrieve the
1307 selected file name from the returned widget.
1309 Returns the created widget. */
1311 static GtkWidget *
1312 xg_get_file_with_selection (f, prompt, default_filename,
1313 mustmatch_p, only_dir_p, func)
1314 FRAME_PTR f;
1315 char *prompt;
1316 char *default_filename;
1317 int mustmatch_p, only_dir_p;
1318 xg_get_file_func *func;
1320 GtkWidget *filewin;
1321 GtkFileSelection *filesel;
1323 filewin = gtk_file_selection_new (prompt);
1324 filesel = GTK_FILE_SELECTION (filewin);
1326 if (default_filename)
1327 gtk_file_selection_set_filename (filesel, default_filename);
1329 if (mustmatch_p)
1331 /* The selection_entry part of filesel is not documented. */
1332 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1333 gtk_file_selection_hide_fileop_buttons (filesel);
1336 *func = xg_get_file_name_from_selector;
1338 return filewin;
1340 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1342 /* Read a file name from the user using a file dialog, either the old
1343 file selection dialog, or the new file chooser dialog. Which to use
1344 depends on what the GTK version used has, and what the value of
1345 gtk-use-old-file-dialog.
1346 F is the current frame.
1347 PROMPT is a prompt to show to the user. May not be NULL.
1348 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1349 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1350 file.
1352 Returns a file name or NULL if no file was selected.
1353 The returned string must be freed by the caller. */
1355 char *
1356 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1357 FRAME_PTR f;
1358 char *prompt;
1359 char *default_filename;
1360 int mustmatch_p, only_dir_p;
1362 GtkWidget *w = 0;
1363 int count = SPECPDL_INDEX ();
1364 char *fn = 0;
1365 int filesel_done = 0;
1366 xg_get_file_func func;
1368 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1369 /* I really don't know why this is needed, but without this the GLIBC add on
1370 library linuxthreads hangs when the Gnome file chooser backend creates
1371 threads. */
1372 sigblock (sigmask (__SIGRTMIN));
1373 #endif /* HAVE_GTK_AND_PTHREAD */
1375 #ifdef HAVE_GTK_FILE_BOTH
1377 if (xg_uses_old_file_dialog ())
1378 w = xg_get_file_with_selection (f, prompt, default_filename,
1379 mustmatch_p, only_dir_p, &func);
1380 else
1381 w = xg_get_file_with_chooser (f, prompt, default_filename,
1382 mustmatch_p, only_dir_p, &func);
1384 #else /* not HAVE_GTK_FILE_BOTH */
1386 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1387 w = xg_get_file_with_selection (f, prompt, default_filename,
1388 mustmatch_p, only_dir_p, &func);
1389 #endif
1390 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1391 w = xg_get_file_with_chooser (f, prompt, default_filename,
1392 mustmatch_p, only_dir_p, &func);
1393 #endif
1395 #endif /* HAVE_GTK_FILE_BOTH */
1397 xg_set_screen (w, f);
1398 gtk_widget_set_name (w, "emacs-filedialog");
1399 gtk_window_set_transient_for (GTK_WINDOW (w),
1400 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1401 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1402 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1404 g_signal_connect (G_OBJECT (w),
1405 "response",
1406 G_CALLBACK (xg_file_response_cb),
1407 &filesel_done);
1409 /* Don't destroy the widget if closed by the window manager close button. */
1410 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1412 gtk_widget_show (w);
1414 record_unwind_protect (pop_down_file_dialog, make_save_value (w, 0));
1415 while (! filesel_done)
1417 x_menu_wait_for_event (0);
1418 gtk_main_iteration ();
1421 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1422 sigunblock (sigmask (__SIGRTMIN));
1423 #endif
1425 if (filesel_done == GTK_RESPONSE_OK)
1426 fn = (*func) (w);
1428 unbind_to (count, Qnil);
1430 return fn;
1434 /***********************************************************************
1435 Menu functions.
1436 ***********************************************************************/
1438 /* The name of menu items that can be used for citomization. Since GTK
1439 RC files are very crude and primitive, we have to set this on all
1440 menu item names so a user can easily cutomize menu items. */
1442 #define MENU_ITEM_NAME "emacs-menuitem"
1445 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1446 during GC. The next member points to the items. */
1447 static xg_list_node xg_menu_cb_list;
1449 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1450 during GC. The next member points to the items. */
1451 static xg_list_node xg_menu_item_cb_list;
1453 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1454 F is the frame CL_DATA will be initialized for.
1455 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1457 The menu bar and all sub menus under the menu bar in a frame
1458 share the same structure, hence the reference count.
1460 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1461 allocated xg_menu_cb_data if CL_DATA is NULL. */
1463 static xg_menu_cb_data *
1464 make_cl_data (cl_data, f, highlight_cb)
1465 xg_menu_cb_data *cl_data;
1466 FRAME_PTR f;
1467 GCallback highlight_cb;
1469 if (! cl_data)
1471 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1472 cl_data->f = f;
1473 cl_data->menu_bar_vector = f->menu_bar_vector;
1474 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1475 cl_data->highlight_cb = highlight_cb;
1476 cl_data->ref_count = 0;
1478 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1481 cl_data->ref_count++;
1483 return cl_data;
1486 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1487 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1489 When the menu bar is updated, menu items may have been added and/or
1490 removed, so menu_bar_vector and menu_bar_items_used change. We must
1491 then update CL_DATA since it is used to determine which menu
1492 item that is invoked in the menu.
1493 HIGHLIGHT_CB could change, there is no check that the same
1494 function is given when modifying a menu bar as was given when
1495 creating the menu bar. */
1497 static void
1498 update_cl_data (cl_data, f, highlight_cb)
1499 xg_menu_cb_data *cl_data;
1500 FRAME_PTR f;
1501 GCallback highlight_cb;
1503 if (cl_data)
1505 cl_data->f = f;
1506 cl_data->menu_bar_vector = f->menu_bar_vector;
1507 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1508 cl_data->highlight_cb = highlight_cb;
1512 /* Decrease reference count for CL_DATA.
1513 If reference count is zero, free CL_DATA. */
1515 static void
1516 unref_cl_data (cl_data)
1517 xg_menu_cb_data *cl_data;
1519 if (cl_data && cl_data->ref_count > 0)
1521 cl_data->ref_count--;
1522 if (cl_data->ref_count == 0)
1524 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1525 xfree (cl_data);
1530 /* Function that marks all lisp data during GC. */
1532 void
1533 xg_mark_data ()
1535 xg_list_node *iter;
1537 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1538 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1540 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1542 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1544 if (! NILP (cb_data->help))
1545 mark_object (cb_data->help);
1550 /* Callback called when a menu item is destroyed. Used to free data.
1551 W is the widget that is being destroyed (not used).
1552 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1554 static void
1555 menuitem_destroy_callback (w, client_data)
1556 GtkWidget *w;
1557 gpointer client_data;
1559 if (client_data)
1561 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1562 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1563 xfree (data);
1567 /* Callback called when the pointer enters/leaves a menu item.
1568 W is the menu item.
1569 EVENT is either an enter event or leave event.
1570 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1572 Returns FALSE to tell GTK to keep processing this event. */
1574 static gboolean
1575 menuitem_highlight_callback (w, event, client_data)
1576 GtkWidget *w;
1577 GdkEventCrossing *event;
1578 gpointer client_data;
1580 if (client_data)
1582 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1583 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1585 if (! NILP (data->help) && data->cl_data->highlight_cb)
1587 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1588 (*func) (w, call_data);
1592 return FALSE;
1595 /* Callback called when a menu is destroyed. Used to free data.
1596 W is the widget that is being destroyed (not used).
1597 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1599 static void
1600 menu_destroy_callback (w, client_data)
1601 GtkWidget *w;
1602 gpointer client_data;
1604 unref_cl_data ((xg_menu_cb_data*) client_data);
1607 /* Callback called when a menu does a grab or ungrab. That means the
1608 menu has been activated or deactivated.
1609 Used to start a timer so the small timeout the menus in GTK uses before
1610 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1611 W is the widget that does the grab (not used).
1612 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1613 CLIENT_DATA is NULL (not used). */
1615 static void
1616 menu_grab_callback (GtkWidget *widget,
1617 gboolean ungrab_p,
1618 gpointer client_data)
1620 /* Keep track of total number of grabs. */
1621 static int cnt;
1623 if (ungrab_p) cnt--;
1624 else cnt++;
1626 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1627 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1630 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1631 must be non-NULL) and can be inserted into a menu item.
1633 Returns the GtkHBox. */
1635 static GtkWidget *
1636 make_widget_for_menu_item (utf8_label, utf8_key)
1637 char *utf8_label;
1638 char *utf8_key;
1640 GtkWidget *wlbl;
1641 GtkWidget *wkey;
1642 GtkWidget *wbox;
1644 wbox = gtk_hbox_new (FALSE, 0);
1645 wlbl = gtk_label_new (utf8_label);
1646 wkey = gtk_label_new (utf8_key);
1648 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1649 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1651 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1652 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1654 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1655 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1656 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1658 return wbox;
1661 /* Make and return a menu item widget with the key to the right.
1662 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1663 UTF8_KEY is the text representing the key binding.
1664 ITEM is the widget_value describing the menu item.
1666 GROUP is an in/out parameter. If the menu item to be created is not
1667 part of any radio menu group, *GROUP contains NULL on entry and exit.
1668 If the menu item to be created is part of a radio menu group, on entry
1669 *GROUP contains the group to use, or NULL if this is the first item
1670 in the group. On exit, *GROUP contains the radio item group.
1672 Unfortunately, keys don't line up as nicely as in Motif,
1673 but the MacOS X version doesn't either, so I guess that is OK. */
1675 static GtkWidget *
1676 make_menu_item (utf8_label, utf8_key, item, group)
1677 char *utf8_label;
1678 char *utf8_key;
1679 widget_value *item;
1680 GSList **group;
1682 GtkWidget *w;
1683 GtkWidget *wtoadd = 0;
1685 /* It has been observed that some menu items have a NULL name field.
1686 This will lead to this function being called with a NULL utf8_label.
1687 GTK crashes on that so we set a blank label. Why there is a NULL
1688 name remains to be investigated. */
1689 if (! utf8_label) utf8_label = " ";
1691 if (utf8_key)
1692 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1694 if (item->button_type == BUTTON_TYPE_TOGGLE)
1696 *group = NULL;
1697 if (utf8_key) w = gtk_check_menu_item_new ();
1698 else w = gtk_check_menu_item_new_with_label (utf8_label);
1699 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1701 else if (item->button_type == BUTTON_TYPE_RADIO)
1703 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1704 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1705 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1706 if (item->selected)
1707 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1709 else
1711 *group = NULL;
1712 if (utf8_key) w = gtk_menu_item_new ();
1713 else w = gtk_menu_item_new_with_label (utf8_label);
1716 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1717 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1719 return w;
1722 /* Return non-zero if LABEL specifies a separator (GTK only has one
1723 separator type) */
1725 static int
1726 xg_separator_p (char *label)
1728 if (! label) return 0;
1729 else if (strlen (label) > 3
1730 && strncmp (label, "--", 2) == 0
1731 && label[2] != '-')
1733 static char* separator_names[] = {
1734 "space",
1735 "no-line",
1736 "single-line",
1737 "double-line",
1738 "single-dashed-line",
1739 "double-dashed-line",
1740 "shadow-etched-in",
1741 "shadow-etched-out",
1742 "shadow-etched-in-dash",
1743 "shadow-etched-out-dash",
1744 "shadow-double-etched-in",
1745 "shadow-double-etched-out",
1746 "shadow-double-etched-in-dash",
1747 "shadow-double-etched-out-dash",
1751 int i;
1753 label += 2;
1754 for (i = 0; separator_names[i]; ++i)
1755 if (strcmp (label, separator_names[i]) == 0)
1756 return 1;
1758 else
1760 /* Old-style separator, maybe. It's a separator if it contains
1761 only dashes. */
1762 while (*label == '-')
1763 ++label;
1764 if (*label == 0) return 1;
1767 return 0;
1770 static int xg_detached_menus;
1772 /* Returns non-zero if there are detached menus. */
1775 xg_have_tear_offs ()
1777 return xg_detached_menus > 0;
1780 /* Callback invoked when a detached menu window is removed. Here we
1781 decrease the xg_detached_menus count.
1782 WIDGET is the top level window that is removed (the parent of the menu).
1783 CLIENT_DATA is not used. */
1785 static void
1786 tearoff_remove (widget, client_data)
1787 GtkWidget *widget;
1788 gpointer client_data;
1790 if (xg_detached_menus > 0) --xg_detached_menus;
1793 /* Callback invoked when a menu is detached. It increases the
1794 xg_detached_menus count.
1795 WIDGET is the GtkTearoffMenuItem.
1796 CLIENT_DATA is not used. */
1798 static void
1799 tearoff_activate (widget, client_data)
1800 GtkWidget *widget;
1801 gpointer client_data;
1803 GtkWidget *menu = gtk_widget_get_parent (widget);
1804 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1806 ++xg_detached_menus;
1807 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1808 "destroy",
1809 G_CALLBACK (tearoff_remove), 0);
1814 /* Create a menu item widget, and connect the callbacks.
1815 ITEM decribes the menu item.
1816 F is the frame the created menu belongs to.
1817 SELECT_CB is the callback to use when a menu item is selected.
1818 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1819 CL_DATA points to the callback data to be used for this menu.
1820 GROUP is an in/out parameter. If the menu item to be created is not
1821 part of any radio menu group, *GROUP contains NULL on entry and exit.
1822 If the menu item to be created is part of a radio menu group, on entry
1823 *GROUP contains the group to use, or NULL if this is the first item
1824 in the group. On exit, *GROUP contains the radio item group.
1826 Returns the created GtkWidget. */
1828 static GtkWidget *
1829 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1830 widget_value *item;
1831 FRAME_PTR f;
1832 GCallback select_cb;
1833 GCallback highlight_cb;
1834 xg_menu_cb_data *cl_data;
1835 GSList **group;
1837 char *utf8_label;
1838 char *utf8_key;
1839 GtkWidget *w;
1840 xg_menu_item_cb_data *cb_data;
1842 utf8_label = get_utf8_string (item->name);
1843 utf8_key = get_utf8_string (item->key);
1845 w = make_menu_item (utf8_label, utf8_key, item, group);
1847 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1848 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1850 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1852 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1854 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1855 cb_data->help = item->help;
1856 cb_data->cl_data = cl_data;
1857 cb_data->call_data = item->call_data;
1859 g_signal_connect (G_OBJECT (w),
1860 "destroy",
1861 G_CALLBACK (menuitem_destroy_callback),
1862 cb_data);
1864 /* Put cb_data in widget, so we can get at it when modifying menubar */
1865 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1867 /* final item, not a submenu */
1868 if (item->call_data && ! item->contents)
1870 if (select_cb)
1871 cb_data->select_id
1872 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1875 if (! NILP (item->help) && highlight_cb)
1877 /* We use enter/leave notify instead of select/deselect because
1878 select/deselect doesn't go well with detached menus. */
1879 cb_data->highlight_id
1880 = g_signal_connect (G_OBJECT (w),
1881 "enter-notify-event",
1882 G_CALLBACK (menuitem_highlight_callback),
1883 cb_data);
1884 cb_data->unhighlight_id
1885 = g_signal_connect (G_OBJECT (w),
1886 "leave-notify-event",
1887 G_CALLBACK (menuitem_highlight_callback),
1888 cb_data);
1891 return w;
1894 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1895 GCallback, GCallback, int, int, int,
1896 GtkWidget *, xg_menu_cb_data *, char *));
1898 /* Create a full menu tree specified by DATA.
1899 F is the frame the created menu belongs to.
1900 SELECT_CB is the callback to use when a menu item is selected.
1901 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1902 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1903 POP_UP_P is non-zero if we shall create a popup menu.
1904 MENU_BAR_P is non-zero if we shall create a menu bar.
1905 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1906 if MENU_BAR_P is non-zero.
1907 TOPMENU is the topmost GtkWidget that others shall be placed under.
1908 It may be NULL, in that case we create the appropriate widget
1909 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1910 CL_DATA is the callback data we shall use for this menu, or NULL
1911 if we haven't set the first callback yet.
1912 NAME is the name to give to the top level menu if this function
1913 creates it. May be NULL to not set any name.
1915 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1916 not NULL.
1918 This function calls itself to create submenus. */
1920 static GtkWidget *
1921 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1922 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1923 widget_value *data;
1924 FRAME_PTR f;
1925 GCallback select_cb;
1926 GCallback deactivate_cb;
1927 GCallback highlight_cb;
1928 int pop_up_p;
1929 int menu_bar_p;
1930 int add_tearoff_p;
1931 GtkWidget *topmenu;
1932 xg_menu_cb_data *cl_data;
1933 char *name;
1935 widget_value *item;
1936 GtkWidget *wmenu = topmenu;
1937 GSList *group = NULL;
1939 if (! topmenu)
1941 if (! menu_bar_p)
1943 wmenu = gtk_menu_new ();
1944 xg_set_screen (wmenu, f);
1946 else wmenu = gtk_menu_bar_new ();
1948 /* Put cl_data on the top menu for easier access. */
1949 cl_data = make_cl_data (cl_data, f, highlight_cb);
1950 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1951 g_signal_connect (G_OBJECT (wmenu), "destroy",
1952 G_CALLBACK (menu_destroy_callback), cl_data);
1954 if (name)
1955 gtk_widget_set_name (wmenu, name);
1957 if (deactivate_cb)
1958 g_signal_connect (G_OBJECT (wmenu),
1959 "selection-done", deactivate_cb, 0);
1961 g_signal_connect (G_OBJECT (wmenu),
1962 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1965 if (! menu_bar_p && add_tearoff_p)
1967 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1968 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1970 g_signal_connect (G_OBJECT (tearoff), "activate",
1971 G_CALLBACK (tearoff_activate), 0);
1974 for (item = data; item; item = item->next)
1976 GtkWidget *w;
1978 if (pop_up_p && !item->contents && !item->call_data
1979 && !xg_separator_p (item->name))
1981 char *utf8_label;
1982 /* A title for a popup. We do the same as GTK does when
1983 creating titles, but it does not look good. */
1984 group = NULL;
1985 utf8_label = get_utf8_string (item->name);
1987 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1988 w = gtk_menu_item_new_with_label (utf8_label);
1989 gtk_widget_set_sensitive (w, FALSE);
1990 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1992 else if (xg_separator_p (item->name))
1994 group = NULL;
1995 /* GTK only have one separator type. */
1996 w = gtk_separator_menu_item_new ();
1998 else
2000 w = xg_create_one_menuitem (item,
2002 item->contents ? 0 : select_cb,
2003 highlight_cb,
2004 cl_data,
2005 &group);
2007 if (item->contents)
2009 GtkWidget *submenu = create_menus (item->contents,
2011 select_cb,
2012 deactivate_cb,
2013 highlight_cb,
2016 add_tearoff_p,
2018 cl_data,
2020 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2024 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2025 gtk_widget_set_name (w, MENU_ITEM_NAME);
2028 return wmenu;
2031 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2032 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2033 with some text and buttons.
2034 F is the frame the created item belongs to.
2035 NAME is the name to use for the top widget.
2036 VAL is a widget_value structure describing items to be created.
2037 SELECT_CB is the callback to use when a menu item is selected or
2038 a dialog button is pressed.
2039 DEACTIVATE_CB is the callback to use when an item is deactivated.
2040 For a menu, when a sub menu is not shown anymore, for a dialog it is
2041 called when the dialog is popped down.
2042 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2044 Returns the widget created. */
2046 GtkWidget *
2047 xg_create_widget (type, name, f, val,
2048 select_cb, deactivate_cb, highlight_cb)
2049 char *type;
2050 char *name;
2051 FRAME_PTR f;
2052 widget_value *val;
2053 GCallback select_cb;
2054 GCallback deactivate_cb;
2055 GCallback highlight_cb;
2057 GtkWidget *w = 0;
2058 int menu_bar_p = strcmp (type, "menubar") == 0;
2059 int pop_up_p = strcmp (type, "popup") == 0;
2061 if (strcmp (type, "dialog") == 0)
2063 w = create_dialog (val, select_cb, deactivate_cb);
2064 xg_set_screen (w, f);
2065 gtk_window_set_transient_for (GTK_WINDOW (w),
2066 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2067 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2068 gtk_widget_set_name (w, "emacs-dialog");
2069 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2071 else if (menu_bar_p || pop_up_p)
2073 w = create_menus (val->contents,
2075 select_cb,
2076 deactivate_cb,
2077 highlight_cb,
2078 pop_up_p,
2079 menu_bar_p,
2080 menu_bar_p,
2083 name);
2085 /* Set the cursor to an arrow for popup menus when they are mapped.
2086 This is done by default for menu bar menus. */
2087 if (pop_up_p)
2089 /* Must realize so the GdkWindow inside the widget is created. */
2090 gtk_widget_realize (w);
2091 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2094 else
2096 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2097 type);
2100 return w;
2103 /* Return the label for menu item WITEM. */
2105 static const char *
2106 xg_get_menu_item_label (witem)
2107 GtkMenuItem *witem;
2109 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2110 return gtk_label_get_label (wlabel);
2113 /* Return non-zero if the menu item WITEM has the text LABEL. */
2115 static int
2116 xg_item_label_same_p (witem, label)
2117 GtkMenuItem *witem;
2118 char *label;
2120 int is_same = 0;
2121 char *utf8_label = get_utf8_string (label);
2122 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2124 if (! old_label && ! utf8_label)
2125 is_same = 1;
2126 else if (old_label && utf8_label)
2127 is_same = strcmp (utf8_label, old_label) == 0;
2129 if (utf8_label && utf8_label != label) g_free (utf8_label);
2131 return is_same;
2134 /* Destroy widgets in LIST. */
2136 static void
2137 xg_destroy_widgets (list)
2138 GList *list;
2140 GList *iter;
2142 for (iter = list; iter; iter = g_list_next (iter))
2144 GtkWidget *w = GTK_WIDGET (iter->data);
2146 /* Destroying the widget will remove it from the container it is in. */
2147 gtk_widget_destroy (w);
2151 /* Update the top level names in MENUBAR (i.e. not submenus).
2152 F is the frame the menu bar belongs to.
2153 *LIST is a list with the current menu bar names (menu item widgets).
2154 ITER is the item within *LIST that shall be updated.
2155 POS is the numerical position, starting at 0, of ITER in *LIST.
2156 VAL describes what the menu bar shall look like after the update.
2157 SELECT_CB is the callback to use when a menu item is selected.
2158 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2159 CL_DATA points to the callback data to be used for this menu bar.
2161 This function calls itself to walk through the menu bar names. */
2163 static void
2164 xg_update_menubar (menubar, f, list, iter, pos, val,
2165 select_cb, highlight_cb, cl_data)
2166 GtkWidget *menubar;
2167 FRAME_PTR f;
2168 GList **list;
2169 GList *iter;
2170 int pos;
2171 widget_value *val;
2172 GCallback select_cb;
2173 GCallback highlight_cb;
2174 xg_menu_cb_data *cl_data;
2176 if (! iter && ! val)
2177 return;
2178 else if (iter && ! val)
2180 /* Item(s) have been removed. Remove all remaining items. */
2181 xg_destroy_widgets (iter);
2183 /* All updated. */
2184 val = 0;
2185 iter = 0;
2187 else if (! iter && val)
2189 /* Item(s) added. Add all new items in one call. */
2190 create_menus (val, f, select_cb, 0, highlight_cb,
2191 0, 1, 0, menubar, cl_data, 0);
2193 /* All updated. */
2194 val = 0;
2195 iter = 0;
2197 /* Below this neither iter or val is NULL */
2198 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2200 /* This item is still the same, check next item. */
2201 val = val->next;
2202 iter = g_list_next (iter);
2203 ++pos;
2205 else /* This item is changed. */
2207 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2208 GtkMenuItem *witem2 = 0;
2209 int val_in_menubar = 0;
2210 int iter_in_new_menubar = 0;
2211 GList *iter2;
2212 widget_value *cur;
2214 /* See if the changed entry (val) is present later in the menu bar */
2215 for (iter2 = iter;
2216 iter2 && ! val_in_menubar;
2217 iter2 = g_list_next (iter2))
2219 witem2 = GTK_MENU_ITEM (iter2->data);
2220 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2223 /* See if the current entry (iter) is present later in the
2224 specification for the new menu bar. */
2225 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2226 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2228 if (val_in_menubar && ! iter_in_new_menubar)
2230 int nr = pos;
2232 /* This corresponds to:
2233 Current: A B C
2234 New: A C
2235 Remove B. */
2237 gtk_widget_ref (GTK_WIDGET (witem));
2238 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2239 gtk_widget_destroy (GTK_WIDGET (witem));
2241 /* Must get new list since the old changed. */
2242 g_list_free (*list);
2243 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2244 while (nr-- > 0) iter = g_list_next (iter);
2246 else if (! val_in_menubar && ! iter_in_new_menubar)
2248 /* This corresponds to:
2249 Current: A B C
2250 New: A X C
2251 Rename B to X. This might seem to be a strange thing to do,
2252 since if there is a menu under B it will be totally wrong for X.
2253 But consider editing a C file. Then there is a C-mode menu
2254 (corresponds to B above).
2255 If then doing C-x C-f the minibuf menu (X above) replaces the
2256 C-mode menu. When returning from the minibuffer, we get
2257 back the C-mode menu. Thus we do:
2258 Rename B to X (C-mode to minibuf menu)
2259 Rename X to B (minibuf to C-mode menu).
2260 If the X menu hasn't been invoked, the menu under B
2261 is up to date when leaving the minibuffer. */
2262 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2263 char *utf8_label = get_utf8_string (val->name);
2264 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2266 gtk_label_set_text (wlabel, utf8_label);
2268 /* If this item has a submenu that has been detached, change
2269 the title in the WM decorations also. */
2270 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2271 /* Set the title of the detached window. */
2272 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2274 iter = g_list_next (iter);
2275 val = val->next;
2276 ++pos;
2278 else if (! val_in_menubar && iter_in_new_menubar)
2280 /* This corresponds to:
2281 Current: A B C
2282 New: A X B C
2283 Insert X. */
2285 int nr = pos;
2286 GList *group = 0;
2287 GtkWidget *w = xg_create_one_menuitem (val,
2289 select_cb,
2290 highlight_cb,
2291 cl_data,
2292 &group);
2294 gtk_widget_set_name (w, MENU_ITEM_NAME);
2295 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2297 g_list_free (*list);
2298 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2299 while (nr-- > 0) iter = g_list_next (iter);
2300 iter = g_list_next (iter);
2301 val = val->next;
2302 ++pos;
2304 else /* if (val_in_menubar && iter_in_new_menubar) */
2306 int nr = pos;
2307 /* This corresponds to:
2308 Current: A B C
2309 New: A C B
2310 Move C before B */
2312 gtk_widget_ref (GTK_WIDGET (witem2));
2313 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2314 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2315 GTK_WIDGET (witem2), pos);
2316 gtk_widget_unref (GTK_WIDGET (witem2));
2318 g_list_free (*list);
2319 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2320 while (nr-- > 0) iter = g_list_next (iter);
2321 val = val->next;
2322 ++pos;
2326 /* Update the rest of the menu bar. */
2327 xg_update_menubar (menubar, f, list, iter, pos, val,
2328 select_cb, highlight_cb, cl_data);
2331 /* Update the menu item W so it corresponds to VAL.
2332 SELECT_CB is the callback to use when a menu item is selected.
2333 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2334 CL_DATA is the data to set in the widget for menu invokation. */
2336 static void
2337 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2338 widget_value *val;
2339 GtkWidget *w;
2340 GCallback select_cb;
2341 GCallback highlight_cb;
2342 xg_menu_cb_data *cl_data;
2344 GtkWidget *wchild;
2345 GtkLabel *wlbl = 0;
2346 GtkLabel *wkey = 0;
2347 char *utf8_label;
2348 char *utf8_key;
2349 const char *old_label = 0;
2350 const char *old_key = 0;
2351 xg_menu_item_cb_data *cb_data;
2353 wchild = gtk_bin_get_child (GTK_BIN (w));
2354 utf8_label = get_utf8_string (val->name);
2355 utf8_key = get_utf8_string (val->key);
2357 /* See if W is a menu item with a key. See make_menu_item above. */
2358 if (GTK_IS_HBOX (wchild))
2360 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2362 wlbl = GTK_LABEL (list->data);
2363 wkey = GTK_LABEL (list->next->data);
2364 g_list_free (list);
2366 if (! utf8_key)
2368 /* Remove the key and keep just the label. */
2369 gtk_widget_ref (GTK_WIDGET (wlbl));
2370 gtk_container_remove (GTK_CONTAINER (w), wchild);
2371 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2372 wkey = 0;
2376 else /* Just a label. */
2378 wlbl = GTK_LABEL (wchild);
2380 /* Check if there is now a key. */
2381 if (utf8_key)
2383 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2384 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2386 wlbl = GTK_LABEL (list->data);
2387 wkey = GTK_LABEL (list->next->data);
2388 g_list_free (list);
2390 gtk_container_remove (GTK_CONTAINER (w), wchild);
2391 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2396 if (wkey) old_key = gtk_label_get_label (wkey);
2397 if (wlbl) old_label = gtk_label_get_label (wlbl);
2399 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2400 gtk_label_set_text (wkey, utf8_key);
2402 if (! old_label || strcmp (utf8_label, old_label) != 0)
2403 gtk_label_set_text (wlbl, utf8_label);
2405 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2406 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2408 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2409 gtk_widget_set_sensitive (w, FALSE);
2410 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2411 gtk_widget_set_sensitive (w, TRUE);
2413 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2414 XG_ITEM_DATA);
2415 if (cb_data)
2417 cb_data->call_data = val->call_data;
2418 cb_data->help = val->help;
2419 cb_data->cl_data = cl_data;
2421 /* We assume the callback functions don't change. */
2422 if (val->call_data && ! val->contents)
2424 /* This item shall have a select callback. */
2425 if (! cb_data->select_id)
2426 cb_data->select_id
2427 = g_signal_connect (G_OBJECT (w), "activate",
2428 select_cb, cb_data);
2430 else if (cb_data->select_id)
2432 g_signal_handler_disconnect (w, cb_data->select_id);
2433 cb_data->select_id = 0;
2436 if (NILP (cb_data->help))
2438 /* Shall not have help. Remove if any existed previously. */
2439 if (cb_data->highlight_id)
2441 g_signal_handler_disconnect (G_OBJECT (w),
2442 cb_data->highlight_id);
2443 cb_data->highlight_id = 0;
2445 if (cb_data->unhighlight_id)
2447 g_signal_handler_disconnect (G_OBJECT (w),
2448 cb_data->unhighlight_id);
2449 cb_data->unhighlight_id = 0;
2452 else if (! cb_data->highlight_id && highlight_cb)
2454 /* Have help now, but didn't previously. Add callback. */
2455 cb_data->highlight_id
2456 = g_signal_connect (G_OBJECT (w),
2457 "enter-notify-event",
2458 G_CALLBACK (menuitem_highlight_callback),
2459 cb_data);
2460 cb_data->unhighlight_id
2461 = g_signal_connect (G_OBJECT (w),
2462 "leave-notify-event",
2463 G_CALLBACK (menuitem_highlight_callback),
2464 cb_data);
2469 /* Update the toggle menu item W so it corresponds to VAL. */
2471 static void
2472 xg_update_toggle_item (val, w)
2473 widget_value *val;
2474 GtkWidget *w;
2476 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2479 /* Update the radio menu item W so it corresponds to VAL. */
2481 static void
2482 xg_update_radio_item (val, w)
2483 widget_value *val;
2484 GtkWidget *w;
2486 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2489 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2490 SUBMENU may be NULL, in that case a new menu is created.
2491 F is the frame the menu bar belongs to.
2492 VAL describes the contents of the menu bar.
2493 SELECT_CB is the callback to use when a menu item is selected.
2494 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2495 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2496 CL_DATA is the call back data to use for any newly created items.
2498 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2499 was NULL. */
2501 static GtkWidget *
2502 xg_update_submenu (submenu, f, val,
2503 select_cb, deactivate_cb, highlight_cb, cl_data)
2504 GtkWidget *submenu;
2505 FRAME_PTR f;
2506 widget_value *val;
2507 GCallback select_cb;
2508 GCallback deactivate_cb;
2509 GCallback highlight_cb;
2510 xg_menu_cb_data *cl_data;
2512 GtkWidget *newsub = submenu;
2513 GList *list = 0;
2514 GList *iter;
2515 widget_value *cur;
2516 int has_tearoff_p = 0;
2517 GList *first_radio = 0;
2519 if (submenu)
2520 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2522 for (cur = val, iter = list;
2523 cur && iter;
2524 iter = g_list_next (iter), cur = cur->next)
2526 GtkWidget *w = GTK_WIDGET (iter->data);
2528 /* Skip tearoff items, they have no counterpart in val. */
2529 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2531 has_tearoff_p = 1;
2532 iter = g_list_next (iter);
2533 if (iter) w = GTK_WIDGET (iter->data);
2534 else break;
2537 /* Remember first radio button in a group. If we get a mismatch in
2538 a radio group we must rebuild the whole group so that the connections
2539 in GTK becomes correct. */
2540 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2541 first_radio = iter;
2542 else if (cur->button_type != BUTTON_TYPE_RADIO
2543 && ! GTK_IS_RADIO_MENU_ITEM (w))
2544 first_radio = 0;
2546 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2548 if (! xg_separator_p (cur->name))
2549 break;
2551 else if (GTK_IS_CHECK_MENU_ITEM (w))
2553 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2554 break;
2555 xg_update_toggle_item (cur, w);
2556 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2558 else if (GTK_IS_RADIO_MENU_ITEM (w))
2560 if (cur->button_type != BUTTON_TYPE_RADIO)
2561 break;
2562 xg_update_radio_item (cur, w);
2563 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2565 else if (GTK_IS_MENU_ITEM (w))
2567 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2568 GtkWidget *sub;
2570 if (cur->button_type != BUTTON_TYPE_NONE ||
2571 xg_separator_p (cur->name))
2572 break;
2574 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2576 sub = gtk_menu_item_get_submenu (witem);
2577 if (sub && ! cur->contents)
2579 /* Not a submenu anymore. */
2580 gtk_widget_ref (sub);
2581 gtk_menu_item_remove_submenu (witem);
2582 gtk_widget_destroy (sub);
2584 else if (cur->contents)
2586 GtkWidget *nsub;
2588 nsub = xg_update_submenu (sub, f, cur->contents,
2589 select_cb, deactivate_cb,
2590 highlight_cb, cl_data);
2592 /* If this item just became a submenu, we must set it. */
2593 if (nsub != sub)
2594 gtk_menu_item_set_submenu (witem, nsub);
2597 else
2599 /* Structural difference. Remove everything from here and down
2600 in SUBMENU. */
2601 break;
2605 /* Remove widgets from first structual change. */
2606 if (iter)
2608 /* If we are adding new menu items below, we must remove from
2609 first radio button so that radio groups become correct. */
2610 if (cur && first_radio) xg_destroy_widgets (first_radio);
2611 else xg_destroy_widgets (iter);
2614 if (cur)
2616 /* More items added. Create them. */
2617 newsub = create_menus (cur,
2619 select_cb,
2620 deactivate_cb,
2621 highlight_cb,
2624 ! has_tearoff_p,
2625 submenu,
2626 cl_data,
2630 if (list) g_list_free (list);
2632 return newsub;
2635 /* Update the MENUBAR.
2636 F is the frame the menu bar belongs to.
2637 VAL describes the contents of the menu bar.
2638 If DEEP_P is non-zero, rebuild all but the top level menu names in
2639 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2640 SELECT_CB is the callback to use when a menu item is selected.
2641 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2642 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2644 void
2645 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2646 select_cb, deactivate_cb, highlight_cb)
2647 GtkWidget *menubar;
2648 FRAME_PTR f;
2649 widget_value *val;
2650 int deep_p;
2651 GCallback select_cb;
2652 GCallback deactivate_cb;
2653 GCallback highlight_cb;
2655 xg_menu_cb_data *cl_data;
2656 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2658 if (! list) return;
2660 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2661 XG_FRAME_DATA);
2663 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2664 select_cb, highlight_cb, cl_data);
2666 if (deep_p)
2668 widget_value *cur;
2670 /* Update all sub menus.
2671 We must keep the submenus (GTK menu item widgets) since the
2672 X Window in the XEvent that activates the menu are those widgets. */
2674 /* Update cl_data, menu_item things in F may have changed. */
2675 update_cl_data (cl_data, f, highlight_cb);
2677 for (cur = val->contents; cur; cur = cur->next)
2679 GList *iter;
2680 GtkWidget *sub = 0;
2681 GtkWidget *newsub;
2682 GtkMenuItem *witem;
2684 /* Find sub menu that corresponds to val and update it. */
2685 for (iter = list ; iter; iter = g_list_next (iter))
2687 witem = GTK_MENU_ITEM (iter->data);
2688 if (xg_item_label_same_p (witem, cur->name))
2690 sub = gtk_menu_item_get_submenu (witem);
2691 break;
2695 newsub = xg_update_submenu (sub,
2697 cur->contents,
2698 select_cb,
2699 deactivate_cb,
2700 highlight_cb,
2701 cl_data);
2702 /* sub may still be NULL. If we just updated non deep and added
2703 a new menu bar item, it has no sub menu yet. So we set the
2704 newly created sub menu under witem. */
2705 if (newsub != sub)
2707 xg_set_screen (newsub, f);
2708 gtk_menu_item_set_submenu (witem, newsub);
2713 g_list_free (list);
2714 gtk_widget_show_all (menubar);
2717 /* Recompute all the widgets of frame F, when the menu bar has been
2718 changed. Value is non-zero if widgets were updated. */
2721 xg_update_frame_menubar (f)
2722 FRAME_PTR f;
2724 struct x_output *x = f->output_data.x;
2725 GtkRequisition req;
2727 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2728 return 0;
2730 BLOCK_INPUT;
2732 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2733 FALSE, FALSE, 0);
2734 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2736 gtk_widget_show_all (x->menubar_widget);
2737 gtk_widget_size_request (x->menubar_widget, &req);
2739 FRAME_MENUBAR_HEIGHT (f) = req.height;
2741 /* The height has changed, resize outer widget and set columns
2742 rows to what we had before adding the menu bar. */
2743 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2745 SET_FRAME_GARBAGED (f);
2746 UNBLOCK_INPUT;
2748 return 1;
2751 /* Get rid of the menu bar of frame F, and free its storage.
2752 This is used when deleting a frame, and when turning off the menu bar. */
2754 void
2755 free_frame_menubar (f)
2756 FRAME_PTR f;
2758 struct x_output *x = f->output_data.x;
2760 if (x->menubar_widget)
2762 BLOCK_INPUT;
2764 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2765 /* The menubar and its children shall be deleted when removed from
2766 the container. */
2767 x->menubar_widget = 0;
2768 FRAME_MENUBAR_HEIGHT (f) = 0;
2770 /* The height has changed, resize outer widget and set columns
2771 rows to what we had before removing the menu bar. */
2772 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2774 SET_FRAME_GARBAGED (f);
2775 UNBLOCK_INPUT;
2781 /***********************************************************************
2782 Scroll bar functions
2783 ***********************************************************************/
2786 /* Setting scroll bar values invokes the callback. Use this variable
2787 to indicate that callback should do nothing. */
2789 int xg_ignore_gtk_scrollbar;
2791 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2792 32 bits. But we want to store pointers, and they may be larger
2793 than 32 bits. Keep a mapping from integer index to widget pointers
2794 to get around the 32 bit limitation. */
2796 static struct
2798 GtkWidget **widgets;
2799 int max_size;
2800 int used;
2801 } id_to_widget;
2803 /* Grow this much every time we need to allocate more */
2805 #define ID_TO_WIDGET_INCR 32
2807 /* Store the widget pointer W in id_to_widget and return the integer index. */
2809 static int
2810 xg_store_widget_in_map (w)
2811 GtkWidget *w;
2813 int i;
2815 if (id_to_widget.max_size == id_to_widget.used)
2817 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2819 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2820 sizeof (GtkWidget *)*new_size);
2822 for (i = id_to_widget.max_size; i < new_size; ++i)
2823 id_to_widget.widgets[i] = 0;
2824 id_to_widget.max_size = new_size;
2827 /* Just loop over the array and find a free place. After all,
2828 how many scroll bars are we creating? Should be a small number.
2829 The check above guarantees we will find a free place. */
2830 for (i = 0; i < id_to_widget.max_size; ++i)
2832 if (! id_to_widget.widgets[i])
2834 id_to_widget.widgets[i] = w;
2835 ++id_to_widget.used;
2837 return i;
2841 /* Should never end up here */
2842 abort ();
2845 /* Remove pointer at IDX from id_to_widget.
2846 Called when scroll bar is destroyed. */
2848 static void
2849 xg_remove_widget_from_map (idx)
2850 int idx;
2852 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2854 id_to_widget.widgets[idx] = 0;
2855 --id_to_widget.used;
2859 /* Get the widget pointer at IDX from id_to_widget. */
2861 static GtkWidget *
2862 xg_get_widget_from_map (idx)
2863 int idx;
2865 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2866 return id_to_widget.widgets[idx];
2868 return 0;
2871 /* Return the scrollbar id for X Window WID on display DPY.
2872 Return -1 if WID not in id_to_widget. */
2875 xg_get_scroll_id_for_window (dpy, wid)
2876 Display *dpy;
2877 Window wid;
2879 int idx;
2880 GtkWidget *w;
2882 w = xg_win_to_widget (dpy, wid);
2884 if (w)
2886 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2887 if (id_to_widget.widgets[idx] == w)
2888 return idx;
2891 return -1;
2894 /* Callback invoked when scroll bar WIDGET is destroyed.
2895 DATA is the index into id_to_widget for WIDGET.
2896 We free pointer to last scroll bar values here and remove the index. */
2898 static void
2899 xg_gtk_scroll_destroy (widget, data)
2900 GtkWidget *widget;
2901 gpointer data;
2903 gpointer p;
2904 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
2906 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2907 if (p) xfree (p);
2908 xg_remove_widget_from_map (id);
2911 /* Callback for button press/release events. Used to start timer so that
2912 the scroll bar repetition timer in GTK gets handeled.
2913 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2914 WIDGET is the scroll bar widget the event is for (not used).
2915 EVENT contains the event.
2916 USER_DATA points to the struct scrollbar structure.
2918 Returns FALSE to tell GTK that it shall continue propagate the event
2919 to widgets. */
2921 static gboolean
2922 scroll_bar_button_cb (widget, event, user_data)
2923 GtkWidget *widget;
2924 GdkEventButton *event;
2925 gpointer user_data;
2927 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2928 xg_start_timer ();
2929 else if (event->type == GDK_BUTTON_RELEASE)
2931 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2932 if (xg_timer) xg_stop_timer ();
2933 bar->dragging = Qnil;
2936 return FALSE;
2939 /* Create a scroll bar widget for frame F. Store the scroll bar
2940 in BAR.
2941 SCROLL_CALLBACK is the callback to invoke when the value of the
2942 bar changes.
2943 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2944 to set resources for the widget. */
2946 void
2947 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2948 FRAME_PTR f;
2949 struct scroll_bar *bar;
2950 GCallback scroll_callback;
2951 char *scroll_bar_name;
2953 GtkWidget *wscroll;
2954 GtkWidget *webox;
2955 GtkObject *vadj;
2956 int scroll_id;
2958 /* Page, step increment values are not so important here, they
2959 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2960 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2961 0.1, 0.1, 0.1);
2963 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2964 webox = gtk_event_box_new ();
2965 gtk_widget_set_name (wscroll, scroll_bar_name);
2966 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2968 scroll_id = xg_store_widget_in_map (wscroll);
2970 g_signal_connect (G_OBJECT (wscroll),
2971 "value-changed",
2972 scroll_callback,
2973 (gpointer) bar);
2974 /* The EMACS_INT cast avoids a warning. */
2975 g_signal_connect (G_OBJECT (wscroll),
2976 "destroy",
2977 G_CALLBACK (xg_gtk_scroll_destroy),
2978 (gpointer) (EMACS_INT) scroll_id);
2980 /* Connect to button press and button release to detect if any scroll bar
2981 has the pointer. */
2982 g_signal_connect (G_OBJECT (wscroll),
2983 "button-press-event",
2984 G_CALLBACK (scroll_bar_button_cb),
2985 (gpointer) bar);
2986 g_signal_connect (G_OBJECT (wscroll),
2987 "button-release-event",
2988 G_CALLBACK (scroll_bar_button_cb),
2989 (gpointer) bar);
2991 /* The scroll bar widget does not draw on a window of its own. Instead
2992 it draws on the parent window, in this case the edit widget. So
2993 whenever the edit widget is cleared, the scroll bar needs to redraw
2994 also, which causes flicker. Put an event box between the edit widget
2995 and the scroll bar, so the scroll bar instead draws itself on the
2996 event box window. */
2997 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
2998 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3001 /* Set the cursor to an arrow. */
3002 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3004 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
3007 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
3009 void
3010 xg_show_scroll_bar (scrollbar_id)
3011 int scrollbar_id;
3013 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3014 if (w)
3015 gtk_widget_show_all (gtk_widget_get_parent (w));
3018 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3020 void
3021 xg_remove_scroll_bar (f, scrollbar_id)
3022 FRAME_PTR f;
3023 int scrollbar_id;
3025 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3026 if (w)
3028 GtkWidget *wparent = gtk_widget_get_parent (w);
3029 gtk_widget_destroy (w);
3030 gtk_widget_destroy (wparent);
3031 SET_FRAME_GARBAGED (f);
3035 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3036 in frame F.
3037 TOP/LEFT are the new pixel positions where the bar shall appear.
3038 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3040 void
3041 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
3042 FRAME_PTR f;
3043 int scrollbar_id;
3044 int top;
3045 int left;
3046 int width;
3047 int height;
3050 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3052 if (wscroll)
3054 GtkWidget *wfixed = f->output_data.x->edit_widget;
3055 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3057 /* Move and resize to new values. */
3058 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3059 gtk_widget_set_size_request (wscroll, width, height);
3060 gtk_widget_queue_draw (wparent);
3061 gdk_window_process_all_updates ();
3062 /* GTK does not redraw until the main loop is entered again, but
3063 if there are no X events pending we will not enter it. So we sync
3064 here to get some events. */
3065 x_sync (f);
3066 SET_FRAME_GARBAGED (f);
3067 cancel_mouse_face (f);
3071 /* Set the thumb size and position of scroll bar BAR. We are currently
3072 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3074 void
3075 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3076 struct scroll_bar *bar;
3077 int portion, position, whole;
3079 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
3081 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3083 if (wscroll && NILP (bar->dragging))
3085 GtkAdjustment *adj;
3086 gdouble shown;
3087 gdouble top;
3088 int size, value;
3089 int new_step;
3090 int changed = 0;
3092 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3094 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3095 rather than the real portion value. This makes the thumb less likely
3096 to resize and that looks better. */
3097 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3098 /* When the thumb is at the bottom, position == whole.
3099 So we need to increase `whole' to make space for the thumb. */
3100 whole += portion;
3102 if (whole <= 0)
3103 top = 0, shown = 1;
3104 else
3106 top = (gdouble) position / whole;
3107 shown = (gdouble) portion / whole;
3110 size = shown * XG_SB_RANGE;
3111 size = min (size, XG_SB_RANGE);
3112 size = max (size, 1);
3114 value = top * XG_SB_RANGE;
3115 value = min (value, XG_SB_MAX - size);
3116 value = max (value, XG_SB_MIN);
3118 /* Assume all lines are of equal size. */
3119 new_step = size / max (1, FRAME_LINES (f));
3121 if ((int) adj->page_size != size
3122 || (int) adj->step_increment != new_step)
3124 adj->page_size = size;
3125 adj->step_increment = new_step;
3126 /* Assume a page increment is about 95% of the page size */
3127 adj->page_increment = (int) (0.95*adj->page_size);
3128 changed = 1;
3131 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3133 GtkWidget *wfixed = f->output_data.x->edit_widget;
3135 BLOCK_INPUT;
3137 /* gtk_range_set_value invokes the callback. Set
3138 ignore_gtk_scrollbar to make the callback do nothing */
3139 xg_ignore_gtk_scrollbar = 1;
3141 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3142 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3143 else if (changed)
3144 gtk_adjustment_changed (adj);
3146 xg_ignore_gtk_scrollbar = 0;
3148 UNBLOCK_INPUT;
3154 /***********************************************************************
3155 Tool bar functions
3156 ***********************************************************************/
3157 /* The key for the data we put in the GtkImage widgets. The data is
3158 the image used by Emacs. We use this to see if we need to update
3159 the GtkImage with a new image. */
3160 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3162 /* Callback function invoked when a tool bar item is pressed.
3163 W is the button widget in the tool bar that got pressed,
3164 CLIENT_DATA is an integer that is the index of the button in the
3165 tool bar. 0 is the first button. */
3167 static void
3168 xg_tool_bar_callback (w, client_data)
3169 GtkWidget *w;
3170 gpointer client_data;
3172 /* The EMACS_INT cast avoids a warning. */
3173 int idx = (int) (EMACS_INT) client_data;
3174 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3175 Lisp_Object key, frame;
3176 struct input_event event;
3177 EVENT_INIT (event);
3179 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3180 return;
3182 idx *= TOOL_BAR_ITEM_NSLOTS;
3184 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3185 XSETFRAME (frame, f);
3186 event.kind = TOOL_BAR_EVENT;
3187 event.frame_or_window = frame;
3188 event.arg = frame;
3189 kbd_buffer_store_event (&event);
3191 event.kind = TOOL_BAR_EVENT;
3192 event.frame_or_window = frame;
3193 event.arg = key;
3194 event.modifiers = 0; /* These are not available. */
3195 kbd_buffer_store_event (&event);
3198 /* This callback is called when a tool bar is detached. We must set
3199 the height of the tool bar to zero when this happens so frame sizes
3200 are correctly calculated.
3201 WBOX is the handle box widget that enables detach/attach of the tool bar.
3202 W is the tool bar widget.
3203 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3205 static void
3206 xg_tool_bar_detach_callback (wbox, w, client_data)
3207 GtkHandleBox *wbox;
3208 GtkWidget *w;
3209 gpointer client_data;
3211 FRAME_PTR f = (FRAME_PTR) client_data;
3213 if (f)
3215 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3217 /* When detaching a tool bar, not everything dissapear. There are
3218 a few pixels left that are used to drop the tool bar back into
3219 place. */
3220 FRAME_TOOLBAR_HEIGHT (f) = 2;
3222 /* The height has changed, resize outer widget and set columns
3223 rows to what we had before detaching the tool bar. */
3224 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3228 /* This callback is called when a tool bar is reattached. We must set
3229 the height of the tool bar when this happens so frame sizes
3230 are correctly calculated.
3231 WBOX is the handle box widget that enables detach/attach of the tool bar.
3232 W is the tool bar widget.
3233 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3235 static void
3236 xg_tool_bar_attach_callback (wbox, w, client_data)
3237 GtkHandleBox *wbox;
3238 GtkWidget *w;
3239 gpointer client_data;
3241 FRAME_PTR f = (FRAME_PTR) client_data;
3243 if (f)
3245 GtkRequisition req;
3247 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3249 gtk_widget_size_request (w, &req);
3250 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3252 /* The height has changed, resize outer widget and set columns
3253 rows to what we had before attaching the tool bar. */
3254 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3258 /* This callback is called when the mouse enters or leaves a tool bar item.
3259 It is used for displaying and hiding the help text.
3260 W is the tool bar item, a button.
3261 EVENT is either an enter event or leave event.
3262 CLIENT_DATA is an integer that is the index of the button in the
3263 tool bar. 0 is the first button.
3265 Returns FALSE to tell GTK to keep processing this event. */
3267 static gboolean
3268 xg_tool_bar_help_callback (w, event, client_data)
3269 GtkWidget *w;
3270 GdkEventCrossing *event;
3271 gpointer client_data;
3273 /* The EMACS_INT cast avoids a warning. */
3274 int idx = (int) (EMACS_INT) client_data;
3275 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3276 Lisp_Object help, frame;
3278 if (! GTK_IS_BUTTON (w))
3280 return FALSE;
3283 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3284 return FALSE;
3286 if (event->type == GDK_ENTER_NOTIFY)
3288 idx *= TOOL_BAR_ITEM_NSLOTS;
3289 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3291 if (NILP (help))
3292 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3294 else
3295 help = Qnil;
3297 XSETFRAME (frame, f);
3298 kbd_buffer_store_help_event (frame, help);
3300 return FALSE;
3304 /* This callback is called when a tool bar item shall be redrawn.
3305 It modifies the expose event so that the GtkImage widget redraws the
3306 whole image. This to overcome a bug that makes GtkImage draw the image
3307 in the wrong place when it tries to redraw just a part of the image.
3308 W is the GtkImage to be redrawn.
3309 EVENT is the expose event for W.
3310 CLIENT_DATA is unused.
3312 Returns FALSE to tell GTK to keep processing this event. */
3314 static gboolean
3315 xg_tool_bar_item_expose_callback (w, event, client_data)
3316 GtkWidget *w;
3317 GdkEventExpose *event;
3318 gpointer client_data;
3320 gint width, height;
3322 gdk_drawable_get_size (event->window, &width, &height);
3324 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3325 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3327 event->area.x = max (0, event->area.x);
3328 event->area.y = max (0, event->area.y);
3330 event->area.width = max (width, event->area.width);
3331 event->area.height = max (height, event->area.height);
3333 return FALSE;
3336 /* This callback is called when a tool bar shall be redrawn.
3337 We need to update the tool bar from here in case the image cache
3338 has deleted the pixmaps used in the tool bar.
3339 W is the GtkToolbar to be redrawn.
3340 EVENT is the expose event for W.
3341 CLIENT_DATA is pointing to the frame for this tool bar.
3343 Returns FALSE to tell GTK to keep processing this event. */
3345 static gboolean
3346 xg_tool_bar_expose_callback (w, event, client_data)
3347 GtkWidget *w;
3348 GdkEventExpose *event;
3349 gpointer client_data;
3351 update_frame_tool_bar ((FRAME_PTR) client_data);
3352 return FALSE;
3355 /* Create a tool bar for frame F. */
3357 static void
3358 xg_create_tool_bar (f)
3359 FRAME_PTR f;
3361 struct x_output *x = f->output_data.x;
3362 GtkRequisition req;
3363 int vbox_pos = x->menubar_widget ? 1 : 0;
3365 x->toolbar_widget = gtk_toolbar_new ();
3366 x->handlebox_widget = gtk_handle_box_new ();
3367 x->toolbar_detached = 0;
3369 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3370 x->toolbar_widget);
3372 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3373 FALSE, FALSE, 0);
3375 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3376 vbox_pos);
3378 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3380 /* We only have icons, so override any user setting. We could
3381 use the caption property of the toolbar item (see update_frame_tool_bar
3382 below), but some of those strings are long, making the toolbar so
3383 long it does not fit on the screen. The GtkToolbar widget makes every
3384 item equal size, so the longest caption determine the size of every
3385 tool bar item. I think the creators of the GtkToolbar widget
3386 counted on 4 or 5 character long strings. */
3387 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3388 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3389 GTK_ORIENTATION_HORIZONTAL);
3391 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3392 G_CALLBACK (xg_tool_bar_detach_callback), f);
3393 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3394 G_CALLBACK (xg_tool_bar_attach_callback), f);
3395 g_signal_connect (G_OBJECT (x->toolbar_widget),
3396 "expose-event",
3397 G_CALLBACK (xg_tool_bar_expose_callback),
3400 gtk_widget_show_all (x->handlebox_widget);
3402 gtk_widget_size_request (x->toolbar_widget, &req);
3403 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3405 /* The height has changed, resize outer widget and set columns
3406 rows to what we had before adding the tool bar. */
3407 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3409 SET_FRAME_GARBAGED (f);
3412 /* Update the tool bar for frame F. Add new buttons and remove old. */
3414 void
3415 update_frame_tool_bar (f)
3416 FRAME_PTR f;
3418 int i;
3419 GtkRequisition old_req, new_req;
3420 GList *icon_list;
3421 GList *iter;
3422 struct x_output *x = f->output_data.x;
3423 int hmargin, vmargin;
3425 if (! FRAME_GTK_WIDGET (f))
3426 return;
3428 BLOCK_INPUT;
3430 if (INTEGERP (Vtool_bar_button_margin)
3431 && XINT (Vtool_bar_button_margin) > 0)
3433 hmargin = XFASTINT (Vtool_bar_button_margin);
3434 vmargin = XFASTINT (Vtool_bar_button_margin);
3436 else if (CONSP (Vtool_bar_button_margin))
3438 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3439 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3440 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3442 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3443 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3444 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3447 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3448 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3449 i.e. zero. This means that margins less than
3450 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3451 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3452 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3454 if (! x->toolbar_widget)
3455 xg_create_tool_bar (f);
3457 gtk_widget_size_request (x->toolbar_widget, &old_req);
3459 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3460 iter = icon_list;
3462 for (i = 0; i < f->n_tool_bar_items; ++i)
3464 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3466 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3467 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3468 int idx;
3469 int img_id;
3470 struct image *img;
3471 Lisp_Object image;
3472 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3474 if (iter) iter = g_list_next (iter);
3476 /* If image is a vector, choose the image according to the
3477 button state. */
3478 image = PROP (TOOL_BAR_ITEM_IMAGES);
3479 if (VECTORP (image))
3481 if (enabled_p)
3482 idx = (selected_p
3483 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3484 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3485 else
3486 idx = (selected_p
3487 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3488 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3490 xassert (ASIZE (image) >= idx);
3491 image = AREF (image, idx);
3493 else
3494 idx = -1;
3496 /* Ignore invalid image specifications. */
3497 if (!valid_image_p (image))
3499 if (wicon) gtk_widget_hide (wicon);
3500 continue;
3503 img_id = lookup_image (f, image);
3504 img = IMAGE_FROM_ID (f, img_id);
3505 prepare_image_for_display (f, img);
3507 if (img->load_failed_p || img->pixmap == None)
3509 if (wicon) gtk_widget_hide (wicon);
3510 continue;
3513 if (! wicon)
3515 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3517 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3519 /* The EMACS_INT cast avoids a warning. */
3520 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3521 0, 0, 0,
3523 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3524 (gpointer) (EMACS_INT) i);
3526 /* Save the image so we can see if an update is needed when
3527 this function is called again. */
3528 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3529 (gpointer)img->pixmap);
3531 /* Catch expose events to overcome an annoying redraw bug, see
3532 comment for xg_tool_bar_item_expose_callback. */
3533 g_signal_connect (G_OBJECT (w),
3534 "expose-event",
3535 G_CALLBACK (xg_tool_bar_item_expose_callback),
3538 /* We must set sensitive on the button that is the parent
3539 of the GtkImage parent. Go upwards until we find the button. */
3540 while (! GTK_IS_BUTTON (w))
3541 w = gtk_widget_get_parent (w);
3543 if (w)
3545 /* Save the frame in the button so the xg_tool_bar_callback
3546 can get at it. */
3547 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3548 gtk_widget_set_sensitive (w, enabled_p);
3550 /* Use enter/leave notify to show help. We use the events
3551 rather than the GtkButton specific signals "enter" and
3552 "leave", so we can have only one callback. The event
3553 will tell us what kind of event it is. */
3554 /* The EMACS_INT cast avoids a warning. */
3555 g_signal_connect (G_OBJECT (w),
3556 "enter-notify-event",
3557 G_CALLBACK (xg_tool_bar_help_callback),
3558 (gpointer) (EMACS_INT) i);
3559 g_signal_connect (G_OBJECT (w),
3560 "leave-notify-event",
3561 G_CALLBACK (xg_tool_bar_help_callback),
3562 (gpointer) (EMACS_INT) i);
3565 else
3567 /* The child of the tool bar is a button. Inside that button
3568 is a vbox. Inside that vbox is the GtkImage. */
3569 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3570 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3571 GtkImage *wimage = GTK_IMAGE (chlist->data);
3572 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3573 XG_TOOL_BAR_IMAGE_DATA);
3574 g_list_free (chlist);
3576 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
3578 if (old_img != img->pixmap)
3579 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
3581 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3582 (gpointer)img->pixmap);
3584 gtk_widget_set_sensitive (wicon, enabled_p);
3585 gtk_widget_show (wicon);
3588 #undef PROP
3591 /* Remove buttons not longer needed. We just hide them so they
3592 can be reused later on. */
3593 while (iter)
3595 GtkWidget *w = GTK_WIDGET (iter->data);
3596 gtk_widget_hide (w);
3597 iter = g_list_next (iter);
3600 gtk_widget_size_request (x->toolbar_widget, &new_req);
3601 if (old_req.height != new_req.height
3602 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
3604 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3605 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3608 if (icon_list) g_list_free (icon_list);
3610 UNBLOCK_INPUT;
3613 /* Deallocate all resources for the tool bar on frame F.
3614 Remove the tool bar. */
3616 void
3617 free_frame_tool_bar (f)
3618 FRAME_PTR f;
3620 struct x_output *x = f->output_data.x;
3622 if (x->toolbar_widget)
3624 BLOCK_INPUT;
3625 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3626 x->handlebox_widget);
3627 x->toolbar_widget = 0;
3628 x->handlebox_widget = 0;
3629 FRAME_TOOLBAR_HEIGHT (f) = 0;
3631 /* The height has changed, resize outer widget and set columns
3632 rows to what we had before removing the tool bar. */
3633 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3635 SET_FRAME_GARBAGED (f);
3636 UNBLOCK_INPUT;
3642 /***********************************************************************
3643 Initializing
3644 ***********************************************************************/
3645 void
3646 xg_initialize ()
3648 GtkBindingSet *binding_set;
3650 xg_ignore_gtk_scrollbar = 0;
3651 xg_detached_menus = 0;
3652 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3653 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3655 id_to_widget.max_size = id_to_widget.used = 0;
3656 id_to_widget.widgets = 0;
3658 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3659 bindings. It doesn't seem to be any way to remove properties,
3660 so we set it to VoidSymbol which in X means "no key". */
3661 gtk_settings_set_string_property (gtk_settings_get_default (),
3662 "gtk-menu-bar-accel",
3663 "VoidSymbol",
3664 EMACS_CLASS);
3666 /* Make GTK text input widgets use Emacs style keybindings. This is
3667 Emacs after all. */
3668 gtk_settings_set_string_property (gtk_settings_get_default (),
3669 "gtk-key-theme-name",
3670 "Emacs",
3671 EMACS_CLASS);
3673 /* Make dialogs close on C-g. Since file dialog inherits from
3674 dialog, this works for them also. */
3675 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
3676 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3677 "close", 0);
3679 /* Make menus close on C-g. */
3680 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
3681 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3682 "cancel", 0);
3685 #endif /* USE_GTK */
3687 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3688 (do not change this comment) */