(query-replace-read-from): Fix 2005-02-19 change.
[emacs.git] / src / gtkutil.c
blobc291b44f5a2a25f189f7b24d7e7bf69f1a820c06
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
24 #ifdef USE_GTK
25 #include <string.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "blockinput.h"
31 #include "syssignal.h"
32 #include "window.h"
33 #include "atimer.h"
34 #include "gtkutil.h"
35 #include "termhooks.h"
36 #include "keyboard.h"
37 #include "charset.h"
38 #include "coding.h"
39 #include <gdk/gdkkeysyms.h>
42 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
43 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
46 /***********************************************************************
47 Display handling functions
48 ***********************************************************************/
50 #ifdef HAVE_GTK_MULTIDISPLAY
52 /* Return the GdkDisplay that corresponds to the X display DPY. */
54 static GdkDisplay *
55 xg_get_gdk_display (dpy)
56 Display *dpy;
58 return gdk_x11_lookup_xdisplay (dpy);
61 /* When the GTK widget W is to be created on a display for F that
62 is not the default display, set the display for W.
63 W can be a GtkMenu or a GtkWindow widget. */
65 static void
66 xg_set_screen (w, f)
67 GtkWidget *w;
68 FRAME_PTR f;
70 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
72 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
73 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
75 if (GTK_IS_MENU (w))
76 gtk_menu_set_screen (GTK_MENU (w), gscreen);
77 else
78 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
83 #else /* not HAVE_GTK_MULTIDISPLAY */
85 /* Make some defines so we can use the GTK 2.2 functions when
86 compiling with GTK 2.0. */
88 #define xg_set_screen(w, f)
89 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
90 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
91 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
92 #define gdk_x11_lookup_xdisplay(dpy) 0
93 #define GdkDisplay void
95 #endif /* not HAVE_GTK_MULTIDISPLAY */
97 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
98 *DPY is set to NULL if the display can't be opened.
100 Returns non-zero if display could be opened, zero if display could not
101 be opened, and less than zero if the GTK version doesn't support
102 multipe displays. */
105 xg_display_open (display_name, dpy)
106 char *display_name;
107 Display **dpy;
109 #ifdef HAVE_GTK_MULTIDISPLAY
110 GdkDisplay *gdpy;
112 gdpy = gdk_display_open (display_name);
113 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
115 return gdpy != NULL;
117 #else /* not HAVE_GTK_MULTIDISPLAY */
119 return -1;
120 #endif /* not HAVE_GTK_MULTIDISPLAY */
124 /* Close display DPY. */
126 void
127 xg_display_close (Display *dpy)
129 #ifdef HAVE_GTK_MULTIDISPLAY
130 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
132 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
133 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
134 we can continue running, but there will be memory leaks. */
136 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
138 /* If this is the default display, we must change it before calling
139 dispose, otherwise it will crash. */
140 if (gdk_display_get_default () == gdpy)
142 struct x_display_info *dpyinfo;
143 Display *new_dpy = 0;
144 GdkDisplay *gdpy_new;
146 /* Find another display. */
147 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
148 if (dpyinfo->display != dpy)
150 new_dpy = dpyinfo->display;
151 break;
154 if (! new_dpy) return; /* Emacs will exit anyway. */
156 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
157 gdk_display_manager_set_default_display (gdk_display_manager_get (),
158 gdpy_new);
161 g_object_run_dispose (G_OBJECT (gdpy));
163 #else
164 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
165 gdk_display_close (gdpy);
166 #endif
167 #endif /* HAVE_GTK_MULTIDISPLAY */
171 /***********************************************************************
172 Utility functions
173 ***********************************************************************/
174 /* The timer for scroll bar repetition and menu bar timeouts.
175 NULL if no timer is started. */
176 static struct atimer *xg_timer;
179 /* The next two variables and functions are taken from lwlib. */
180 static widget_value *widget_value_free_list;
181 static int malloc_cpt;
183 /* Allocate a widget_value structure, either by taking one from the
184 widget_value_free_list or by malloc:ing a new one.
186 Return a pointer to the allocated structure. */
188 widget_value *
189 malloc_widget_value ()
191 widget_value *wv;
192 if (widget_value_free_list)
194 wv = widget_value_free_list;
195 widget_value_free_list = wv->free_list;
196 wv->free_list = 0;
198 else
200 wv = (widget_value *) malloc (sizeof (widget_value));
201 malloc_cpt++;
203 memset (wv, 0, sizeof (widget_value));
204 return wv;
207 /* This is analogous to free. It frees only what was allocated
208 by malloc_widget_value, and no substructures. */
210 void
211 free_widget_value (wv)
212 widget_value *wv;
214 if (wv->free_list)
215 abort ();
217 if (malloc_cpt > 25)
219 /* When the number of already allocated cells is too big,
220 We free it. */
221 free (wv);
222 malloc_cpt--;
224 else
226 wv->free_list = widget_value_free_list;
227 widget_value_free_list = wv;
232 /* Create and return the cursor to be used for popup menus and
233 scroll bars on display DPY. */
235 GdkCursor *
236 xg_create_default_cursor (dpy)
237 Display *dpy;
239 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
240 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
243 /* For the image defined in IMG, make and return a GtkImage. For displays with
244 8 planes or less we must make a GdkPixbuf and apply the mask manually.
245 Otherwise the highlightning and dimming the tool bar code in GTK does
246 will look bad. For display with more than 8 planes we just use the
247 pixmap and mask directly. For monochrome displays, GTK doesn't seem
248 able to use external pixmaps, it looks bad whatever we do.
249 The image is defined on the display where frame F is.
250 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
251 If OLD_WIDGET is NULL, a new widget is constructed and returned.
252 If OLD_WIDGET is not NULL, that widget is modified. */
254 static GtkWidget *
255 xg_get_image_for_pixmap (f, img, widget, old_widget)
256 FRAME_PTR f;
257 struct image *img;
258 GtkWidget *widget;
259 GtkImage *old_widget;
261 GdkPixmap *gpix;
262 GdkPixmap *gmask;
263 GdkDisplay *gdpy;
265 /* If we are on a one bit display, let GTK do all the image handling.
266 This seems to be the only way to make insensitive and activated icons
267 look good. */
268 if (x_screen_planes (f) == 1)
270 Lisp_Object specified_file = Qnil;
271 Lisp_Object tail;
272 extern Lisp_Object QCfile;
274 for (tail = XCDR (img->spec);
275 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
276 tail = XCDR (XCDR (tail)))
277 if (EQ (XCAR (tail), QCfile))
278 specified_file = XCAR (XCDR (tail));
280 if (STRINGP (specified_file))
283 Lisp_Object file = Qnil;
284 struct gcpro gcpro1;
285 GCPRO1 (file);
287 file = x_find_image_file (specified_file);
288 /* We already loaded the image once before calling this
289 function, so this should not fail. */
290 xassert (STRINGP (file) != 0);
292 if (! old_widget)
293 old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
294 else
295 gtk_image_set_from_file (old_widget, SDATA (file));
297 UNGCPRO;
298 return GTK_WIDGET (old_widget);
302 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
303 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
304 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
306 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
308 if (! old_widget)
309 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
310 else
311 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
313 else
315 /* This is a workaround to make icons look good on pseudo color
316 displays. Apparently GTK expects the images to have an alpha
317 channel. If they don't, insensitive and activated icons will
318 look bad. This workaround does not work on monochrome displays,
319 and is not needed on true color/static color displays (i.e.
320 16 bits and higher). */
321 int x, y, width, height, rowstride, mask_rowstride;
322 GdkPixbuf *icon_buf, *tmp_buf;
323 guchar *pixels;
324 guchar *mask_pixels;
326 gdk_drawable_get_size (gpix, &width, &height);
327 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
328 gpix,
329 gtk_widget_get_colormap (widget),
330 0, 0, 0, 0, width, height);
331 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
332 g_object_unref (G_OBJECT (tmp_buf));
334 if (gmask)
336 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
337 gmask,
338 NULL,
339 0, 0, 0, 0,
340 width, height);
341 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
342 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
343 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
344 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
345 int y;
347 for (y = 0; y < height; ++y)
349 guchar *iconptr, *maskptr;
350 int x;
352 iconptr = pixels + y * rowstride;
353 maskptr = mask_pixels + y * mask_rowstride;
355 for (x = 0; x < width; ++x)
357 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
358 just R is sufficient. */
359 if (maskptr[0] == 0)
360 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
362 iconptr += rowstride/width;
363 maskptr += mask_rowstride/width;
367 g_object_unref (G_OBJECT (mask_buf));
370 if (! old_widget)
371 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
372 else
373 gtk_image_set_from_pixbuf (old_widget, icon_buf);
375 g_object_unref (G_OBJECT (icon_buf));
378 g_object_unref (G_OBJECT (gpix));
379 if (gmask) g_object_unref (G_OBJECT (gmask));
381 return GTK_WIDGET (old_widget);
385 /* Set CURSOR on W and all widgets W contain. We must do like this
386 for scroll bars and menu because they create widgets internally,
387 and it is those widgets that are visible. */
389 static void
390 xg_set_cursor (w, cursor)
391 GtkWidget *w;
392 GdkCursor *cursor;
394 GList *children = gdk_window_peek_children (w->window);
396 gdk_window_set_cursor (w->window, cursor);
398 /* The scroll bar widget has more than one GDK window (had to look at
399 the source to figure this out), and there is no way to set cursor
400 on widgets in GTK. So we must set the cursor for all GDK windows.
401 Ditto for menus. */
403 for ( ; children; children = g_list_next (children))
404 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
407 /* Timer function called when a timeout occurs for xg_timer.
408 This function processes all GTK events in a recursive event loop.
409 This is done because GTK timer events are not seen by Emacs event
410 detection, Emacs only looks for X events. When a scroll bar has the
411 pointer (detected by button press/release events below) an Emacs
412 timer is started, and this function can then check if the GTK timer
413 has expired by calling the GTK event loop.
414 Also, when a menu is active, it has a small timeout before it
415 pops down the sub menu under it. */
417 static void
418 xg_process_timeouts (timer)
419 struct atimer *timer;
421 BLOCK_INPUT;
422 /* Ideally we would like to just handle timer events, like the Xt version
423 of this does in xterm.c, but there is no such feature in GTK. */
424 while (gtk_events_pending ())
425 gtk_main_iteration ();
426 UNBLOCK_INPUT;
429 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
430 xg_process_timeouts is called when the timer expires. The timer
431 started is continuous, i.e. runs until xg_stop_timer is called. */
433 static void
434 xg_start_timer ()
436 if (! xg_timer)
438 EMACS_TIME interval;
439 EMACS_SET_SECS_USECS (interval, 0, 100000);
440 xg_timer = start_atimer (ATIMER_CONTINUOUS,
441 interval,
442 xg_process_timeouts,
447 /* Stop the xg_timer if started. */
449 static void
450 xg_stop_timer ()
452 if (xg_timer)
454 cancel_atimer (xg_timer);
455 xg_timer = 0;
459 /* Insert NODE into linked LIST. */
461 static void
462 xg_list_insert (xg_list_node *list, xg_list_node *node)
464 xg_list_node *list_start = list->next;
466 if (list_start) list_start->prev = node;
467 node->next = list_start;
468 node->prev = 0;
469 list->next = node;
472 /* Remove NODE from linked LIST. */
474 static void
475 xg_list_remove (xg_list_node *list, xg_list_node *node)
477 xg_list_node *list_start = list->next;
478 if (node == list_start)
480 list->next = node->next;
481 if (list->next) list->next->prev = 0;
483 else
485 node->prev->next = node->next;
486 if (node->next) node->next->prev = node->prev;
490 /* Allocate and return a utf8 version of STR. If STR is already
491 utf8 or NULL, just return STR.
492 If not, a new string is allocated and the caller must free the result
493 with g_free. */
495 static char *
496 get_utf8_string (str)
497 char *str;
499 char *utf8_str = str;
501 /* If not UTF-8, try current locale. */
502 if (str && !g_utf8_validate (str, -1, NULL))
503 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
505 return utf8_str;
510 /***********************************************************************
511 General functions for creating widgets, resizing, events, e.t.c.
512 ***********************************************************************/
514 /* Make a geometry string and pass that to GTK. It seems this is the
515 only way to get geometry position right if the user explicitly
516 asked for a position when starting Emacs.
517 F is the frame we shall set geometry for. */
519 static void
520 xg_set_geometry (f)
521 FRAME_PTR f;
523 if (f->size_hint_flags & USPosition)
525 int left = f->left_pos;
526 int xneg = f->size_hint_flags & XNegative;
527 int top = f->top_pos;
528 int yneg = f->size_hint_flags & YNegative;
529 char geom_str[32];
531 if (xneg)
532 left = -left;
533 if (yneg)
534 top = -top;
536 sprintf (geom_str, "=%dx%d%c%d%c%d",
537 FRAME_PIXEL_WIDTH (f),
538 FRAME_TOTAL_PIXEL_HEIGHT (f),
539 (xneg ? '-' : '+'), left,
540 (yneg ? '-' : '+'), top);
542 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
543 geom_str))
544 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
549 /* Resize the outer window of frame F after chainging the height.
550 This happend when the menu bar or the tool bar is added or removed.
551 COLUMNS/ROWS is the size the edit area shall have after the resize. */
553 static void
554 xg_resize_outer_widget (f, columns, rows)
555 FRAME_PTR f;
556 int columns;
557 int rows;
559 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
560 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
562 /* base_height is now changed. */
563 x_wm_set_size_hint (f, 0, 0);
565 /* If we are not mapped yet, set geometry once again, as window
566 height now have changed. */
567 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
568 xg_set_geometry (f);
570 xg_frame_set_char_size (f, columns, rows);
571 gdk_window_process_all_updates ();
574 /* Function to handle resize of our widgets. Since Emacs has some layouts
575 that does not fit well with GTK standard containers, we do most layout
576 manually.
577 F is the frame to resize.
578 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
580 void
581 xg_resize_widgets (f, pixelwidth, pixelheight)
582 FRAME_PTR f;
583 int pixelwidth, pixelheight;
585 int mbheight = FRAME_MENUBAR_HEIGHT (f);
586 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
587 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
588 - mbheight - tbheight));
589 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
591 if (FRAME_GTK_WIDGET (f)
592 && (columns != FRAME_COLS (f)
593 || rows != FRAME_LINES (f)
594 || pixelwidth != FRAME_PIXEL_WIDTH (f)
595 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
597 struct x_output *x = f->output_data.x;
598 GtkAllocation all;
600 all.y = mbheight + tbheight;
601 all.x = 0;
603 all.width = pixelwidth;
604 all.height = pixelheight - mbheight - tbheight;
606 gtk_widget_size_allocate (x->edit_widget, &all);
608 change_frame_size (f, rows, columns, 0, 1, 0);
609 SET_FRAME_GARBAGED (f);
610 cancel_mouse_face (f);
615 /* Update our widget size to be COLS/ROWS characters for frame F. */
617 void
618 xg_frame_set_char_size (f, cols, rows)
619 FRAME_PTR f;
620 int cols;
621 int rows;
623 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
624 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
625 int pixelwidth;
627 /* Take into account the size of the scroll bar. Always use the
628 number of columns occupied by the scroll bar here otherwise we
629 might end up with a frame width that is not a multiple of the
630 frame's character width which is bad for vertically split
631 windows. */
632 f->scroll_bar_actual_width
633 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
635 compute_fringe_widths (f, 0);
637 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
638 after calculating that value. */
639 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
641 /* Must resize our top level widget. Font size may have changed,
642 but not rows/cols. */
643 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
644 pixelwidth, pixelheight);
645 xg_resize_widgets (f, pixelwidth, pixelheight);
646 x_wm_set_size_hint (f, 0, 0);
647 SET_FRAME_GARBAGED (f);
648 cancel_mouse_face (f);
651 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
652 Must be done like this, because GtkWidget:s can have "hidden"
653 X Window that aren't accessible.
655 Return 0 if no widget match WDESC. */
657 GtkWidget *
658 xg_win_to_widget (dpy, wdesc)
659 Display *dpy;
660 Window wdesc;
662 gpointer gdkwin;
663 GtkWidget *gwdesc = 0;
665 BLOCK_INPUT;
667 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
668 wdesc);
669 if (gdkwin)
671 GdkEvent event;
672 event.any.window = gdkwin;
673 gwdesc = gtk_get_event_widget (&event);
676 UNBLOCK_INPUT;
677 return gwdesc;
680 /* Fill in the GdkColor C so that it represents PIXEL.
681 W is the widget that color will be used for. Used to find colormap. */
683 static void
684 xg_pix_to_gcolor (w, pixel, c)
685 GtkWidget *w;
686 unsigned long pixel;
687 GdkColor *c;
689 GdkColormap *map = gtk_widget_get_colormap (w);
690 gdk_colormap_query_color (map, pixel, c);
693 /* Create and set up the GTK widgets for frame F.
694 Return 0 if creation failed, non-zero otherwise. */
697 xg_create_frame_widgets (f)
698 FRAME_PTR f;
700 GtkWidget *wtop;
701 GtkWidget *wvbox;
702 GtkWidget *wfixed;
703 GdkColor bg;
704 GtkRcStyle *style;
705 int i;
706 char *title = 0;
708 BLOCK_INPUT;
710 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
711 xg_set_screen (wtop, f);
713 wvbox = gtk_vbox_new (FALSE, 0);
714 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
716 if (! wtop || ! wvbox || ! wfixed)
718 if (wtop) gtk_widget_destroy (wtop);
719 if (wvbox) gtk_widget_destroy (wvbox);
720 if (wfixed) gtk_widget_destroy (wfixed);
722 UNBLOCK_INPUT;
723 return 0;
726 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
727 gtk_widget_set_name (wtop, EMACS_CLASS);
728 gtk_widget_set_name (wvbox, "pane");
729 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
731 /* If this frame has a title or name, set it in the title bar. */
732 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
733 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
735 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
737 FRAME_GTK_OUTER_WIDGET (f) = wtop;
738 FRAME_GTK_WIDGET (f) = wfixed;
739 f->output_data.x->vbox_widget = wvbox;
741 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
743 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
744 FRAME_PIXEL_HEIGHT (f));
746 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
747 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
749 if (FRAME_EXTERNAL_TOOL_BAR (f))
750 update_frame_tool_bar (f);
752 /* The tool bar is created but first there are no items in it.
753 This causes it to be zero height. Later items are added, but then
754 the frame is already mapped, so there is a "jumping" resize.
755 This makes geometry handling difficult, for example -0-0 will end
756 up in the wrong place as tool bar height has not been taken into account.
757 So we cheat a bit by setting a height that is what it will have
758 later on when tool bar items are added. */
759 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
760 FRAME_TOOLBAR_HEIGHT (f) = 38;
763 /* We don't want this widget double buffered, because we draw on it
764 with regular X drawing primitives, so from a GTK/GDK point of
765 view, the widget is totally blank. When an expose comes, this
766 will make the widget blank, and then Emacs redraws it. This flickers
767 a lot, so we turn off double buffering. */
768 gtk_widget_set_double_buffered (wfixed, FALSE);
770 /* GTK documents says use gtk_window_set_resizable. But then a user
771 can't shrink the window from its starting size. */
772 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
773 gtk_window_set_wmclass (GTK_WINDOW (wtop),
774 SDATA (Vx_resource_name),
775 SDATA (Vx_resource_class));
777 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
778 GTK is to destroy the widget. We want Emacs to do that instead. */
779 g_signal_connect (G_OBJECT (wtop), "delete-event",
780 G_CALLBACK (gtk_true), 0);
782 /* Convert our geometry parameters into a geometry string
783 and specify it.
784 GTK will itself handle calculating the real position this way. */
785 xg_set_geometry (f);
787 gtk_widget_add_events (wfixed,
788 GDK_POINTER_MOTION_MASK
789 | GDK_EXPOSURE_MASK
790 | GDK_BUTTON_PRESS_MASK
791 | GDK_BUTTON_RELEASE_MASK
792 | GDK_KEY_PRESS_MASK
793 | GDK_ENTER_NOTIFY_MASK
794 | GDK_LEAVE_NOTIFY_MASK
795 | GDK_FOCUS_CHANGE_MASK
796 | GDK_STRUCTURE_MASK
797 | GDK_VISIBILITY_NOTIFY_MASK);
799 /* Must realize the windows so the X window gets created. It is used
800 by callers of this function. */
801 gtk_widget_realize (wfixed);
802 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
804 /* Since GTK clears its window by filling with the background color,
805 we must keep X and GTK background in sync. */
806 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
807 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
809 /* Also, do not let any background pixmap to be set, this looks very
810 bad as Emacs overwrites the background pixmap with its own idea
811 of background color. */
812 style = gtk_widget_get_modifier_style (wfixed);
814 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
815 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
816 gtk_widget_modify_style (wfixed, style);
818 /* GTK does not set any border, and they look bad with GTK. */
819 f->border_width = 0;
820 f->internal_border_width = 0;
822 UNBLOCK_INPUT;
824 return 1;
827 /* Set the normal size hints for the window manager, for frame F.
828 FLAGS is the flags word to use--or 0 meaning preserve the flags
829 that the window now has.
830 If USER_POSITION is nonzero, we set the User Position
831 flag (this is useful when FLAGS is 0). */
833 void
834 x_wm_set_size_hint (f, flags, user_position)
835 FRAME_PTR f;
836 long flags;
837 int user_position;
839 if (FRAME_GTK_OUTER_WIDGET (f))
841 /* Must use GTK routines here, otherwise GTK resets the size hints
842 to its own defaults. */
843 GdkGeometry size_hints;
844 gint hint_flags = 0;
845 int base_width, base_height;
846 int min_rows = 0, min_cols = 0;
847 int win_gravity = f->win_gravity;
849 if (flags)
851 memset (&size_hints, 0, sizeof (size_hints));
852 f->output_data.x->size_hints = size_hints;
853 f->output_data.x->hint_flags = hint_flags;
855 else
856 flags = f->size_hint_flags;
858 size_hints = f->output_data.x->size_hints;
859 hint_flags = f->output_data.x->hint_flags;
861 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
862 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
863 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
865 hint_flags |= GDK_HINT_BASE_SIZE;
866 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
867 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
868 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
870 check_frame_size (f, &min_rows, &min_cols);
872 size_hints.base_width = base_width;
873 size_hints.base_height = base_height;
874 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
875 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
878 /* These currently have a one to one mapping with the X values, but I
879 don't think we should rely on that. */
880 hint_flags |= GDK_HINT_WIN_GRAVITY;
881 size_hints.win_gravity = 0;
882 if (win_gravity == NorthWestGravity)
883 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
884 else if (win_gravity == NorthGravity)
885 size_hints.win_gravity = GDK_GRAVITY_NORTH;
886 else if (win_gravity == NorthEastGravity)
887 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
888 else if (win_gravity == WestGravity)
889 size_hints.win_gravity = GDK_GRAVITY_WEST;
890 else if (win_gravity == CenterGravity)
891 size_hints.win_gravity = GDK_GRAVITY_CENTER;
892 else if (win_gravity == EastGravity)
893 size_hints.win_gravity = GDK_GRAVITY_EAST;
894 else if (win_gravity == SouthWestGravity)
895 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
896 else if (win_gravity == SouthGravity)
897 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
898 else if (win_gravity == SouthEastGravity)
899 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
900 else if (win_gravity == StaticGravity)
901 size_hints.win_gravity = GDK_GRAVITY_STATIC;
903 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
904 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
905 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
907 if (user_position)
909 hint_flags &= ~GDK_HINT_POS;
910 hint_flags |= GDK_HINT_USER_POS;
913 BLOCK_INPUT;
915 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
916 FRAME_GTK_OUTER_WIDGET (f),
917 &size_hints,
918 hint_flags);
920 f->output_data.x->size_hints = size_hints;
921 f->output_data.x->hint_flags = hint_flags;
922 UNBLOCK_INPUT;
926 /* Change background color of a frame.
927 Since GTK uses the background colour to clear the window, we must
928 keep the GTK and X colors in sync.
929 F is the frame to change,
930 BG is the pixel value to change to. */
932 void
933 xg_set_background_color (f, bg)
934 FRAME_PTR f;
935 unsigned long bg;
937 if (FRAME_GTK_WIDGET (f))
939 GdkColor gdk_bg;
941 BLOCK_INPUT;
942 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
943 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
944 UNBLOCK_INPUT;
950 /***********************************************************************
951 Dialog functions
952 ***********************************************************************/
953 /* Return the dialog title to use for a dialog of type KEY.
954 This is the encoding used by lwlib. We use the same for GTK. */
956 static char *
957 get_dialog_title (char key)
959 char *title = "";
961 switch (key) {
962 case 'E': case 'e':
963 title = "Error";
964 break;
966 case 'I': case 'i':
967 title = "Information";
968 break;
970 case 'L': case 'l':
971 title = "Prompt";
972 break;
974 case 'P': case 'p':
975 title = "Prompt";
976 break;
978 case 'Q': case 'q':
979 title = "Question";
980 break;
983 return title;
986 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
987 the dialog, but return TRUE so the event does not propagate further
988 in GTK. This prevents GTK from destroying the dialog widget automatically
989 and we can always destrou the widget manually, regardles of how
990 it was popped down (button press or WM_DELETE_WINDOW).
991 W is the dialog widget.
992 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
993 user_data is NULL (not used).
995 Returns TRUE to end propagation of event. */
997 static gboolean
998 dialog_delete_callback (w, event, user_data)
999 GtkWidget *w;
1000 GdkEvent *event;
1001 gpointer user_data;
1003 gtk_widget_unmap (w);
1004 return TRUE;
1007 /* Create a popup dialog window. See also xg_create_widget below.
1008 WV is a widget_value describing the dialog.
1009 SELECT_CB is the callback to use when a button has been pressed.
1010 DEACTIVATE_CB is the callback to use when the dialog pops down.
1012 Returns the GTK dialog widget. */
1014 static GtkWidget *
1015 create_dialog (wv, select_cb, deactivate_cb)
1016 widget_value *wv;
1017 GCallback select_cb;
1018 GCallback deactivate_cb;
1020 char *title = get_dialog_title (wv->name[0]);
1021 int total_buttons = wv->name[1] - '0';
1022 int right_buttons = wv->name[4] - '0';
1023 int left_buttons;
1024 int button_nr = 0;
1025 int button_spacing = 10;
1026 GtkWidget *wdialog = gtk_dialog_new ();
1027 widget_value *item;
1028 GtkBox *cur_box;
1029 GtkWidget *wvbox;
1030 GtkWidget *whbox_up;
1031 GtkWidget *whbox_down;
1033 /* If the number of buttons is greater than 4, make two rows of buttons
1034 instead. This looks better. */
1035 int make_two_rows = total_buttons > 4;
1037 if (right_buttons == 0) right_buttons = total_buttons/2;
1038 left_buttons = total_buttons - right_buttons;
1040 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1041 gtk_widget_set_name (wdialog, "emacs-dialog");
1043 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1045 if (make_two_rows)
1047 wvbox = gtk_vbox_new (TRUE, button_spacing);
1048 whbox_up = gtk_hbox_new (FALSE, 0);
1049 whbox_down = gtk_hbox_new (FALSE, 0);
1051 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1052 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1053 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1055 cur_box = GTK_BOX (whbox_up);
1058 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1059 G_CALLBACK (dialog_delete_callback), 0);
1061 if (deactivate_cb)
1063 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1064 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1067 for (item = wv->contents; item; item = item->next)
1069 char *utf8_label = get_utf8_string (item->value);
1070 GtkWidget *w;
1071 GtkRequisition req;
1073 if (item->name && strcmp (item->name, "message") == 0)
1075 /* This is the text part of the dialog. */
1076 w = gtk_label_new (utf8_label);
1077 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1078 gtk_label_new (""),
1079 FALSE, FALSE, 0);
1080 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1081 TRUE, TRUE, 0);
1082 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1084 /* Try to make dialog look better. Must realize first so
1085 the widget can calculate the size it needs. */
1086 gtk_widget_realize (w);
1087 gtk_widget_size_request (w, &req);
1088 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1089 req.height);
1090 if (item->value && strlen (item->value) > 0)
1091 button_spacing = 2*req.width/strlen (item->value);
1093 else
1095 /* This is one button to add to the dialog. */
1096 w = gtk_button_new_with_label (utf8_label);
1097 if (! item->enabled)
1098 gtk_widget_set_sensitive (w, FALSE);
1099 if (select_cb)
1100 g_signal_connect (G_OBJECT (w), "clicked",
1101 select_cb, item->call_data);
1103 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1104 if (++button_nr == left_buttons)
1106 if (make_two_rows)
1107 cur_box = GTK_BOX (whbox_down);
1108 else
1109 gtk_box_pack_start (cur_box,
1110 gtk_label_new (""),
1111 TRUE, TRUE,
1112 button_spacing);
1116 if (utf8_label && utf8_label != item->value)
1117 g_free (utf8_label);
1120 return wdialog;
1125 /***********************************************************************
1126 File dialog functions
1127 ***********************************************************************/
1128 /* Function that is called when the file dialog pops down.
1129 W is the dialog widget, RESPONSE is the response code.
1130 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1132 static void
1133 xg_file_response_cb (w,
1134 response,
1135 user_data)
1136 GtkDialog *w;
1137 gint response;
1138 gpointer user_data;
1140 int *ptr = (int *) user_data;
1141 *ptr = response;
1145 /* Destroy the dialog. This makes it pop down. */
1147 static Lisp_Object
1148 pop_down_file_dialog (arg)
1149 Lisp_Object arg;
1151 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1152 BLOCK_INPUT;
1153 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1154 UNBLOCK_INPUT;
1155 return Qnil;
1158 typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1160 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1162 /* Return the selected file for file chooser dialog W.
1163 The returned string must be free:d. */
1165 static char *
1166 xg_get_file_name_from_chooser (w)
1167 GtkWidget *w;
1169 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1172 /* Read a file name from the user using a file chooser dialog.
1173 F is the current frame.
1174 PROMPT is a prompt to show to the user. May not be NULL.
1175 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1176 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1177 file. *FUNC is set to a function that can be used to retrieve the
1178 selected file name from the returned widget.
1180 Returns the created widget. */
1182 static GtkWidget *
1183 xg_get_file_with_chooser (f, prompt, default_filename,
1184 mustmatch_p, only_dir_p, func)
1185 FRAME_PTR f;
1186 char *prompt;
1187 char *default_filename;
1188 int mustmatch_p, only_dir_p;
1189 xg_get_file_func *func;
1191 GtkWidget *filewin;
1192 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1193 GtkFileChooserAction action = (mustmatch_p ?
1194 GTK_FILE_CHOOSER_ACTION_OPEN :
1195 GTK_FILE_CHOOSER_ACTION_SAVE);
1197 if (only_dir_p)
1198 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1200 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1201 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1202 (mustmatch_p || only_dir_p ?
1203 GTK_STOCK_OPEN : GTK_STOCK_OK),
1204 GTK_RESPONSE_OK,
1205 NULL);
1206 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1208 if (default_filename)
1210 Lisp_Object file;
1211 struct gcpro gcpro1;
1212 GCPRO1 (file);
1214 file = build_string (default_filename);
1216 /* File chooser does not understand ~/... in the file name. It must be
1217 an absolute name starting with /. */
1218 if (default_filename[0] != '/')
1219 file = Fexpand_file_name (file, Qnil);
1221 default_filename = SDATA (file);
1222 if (Ffile_directory_p (file))
1223 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1224 default_filename);
1225 else
1226 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1227 default_filename);
1229 UNGCPRO;
1232 *func = xg_get_file_name_from_chooser;
1233 return filewin;
1235 #endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1237 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1239 /* Return the selected file for file selector dialog W.
1240 The returned string must be free:d. */
1242 static char *
1243 xg_get_file_name_from_selector (w)
1244 GtkWidget *w;
1246 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1247 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1250 /* Create a file selection dialog.
1251 F is the current frame.
1252 PROMPT is a prompt to show to the user. May not be NULL.
1253 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1254 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1255 file. *FUNC is set to a function that can be used to retrieve the
1256 selected file name from the returned widget.
1258 Returns the created widget. */
1260 static GtkWidget *
1261 xg_get_file_with_selection (f, prompt, default_filename,
1262 mustmatch_p, only_dir_p, func)
1263 FRAME_PTR f;
1264 char *prompt;
1265 char *default_filename;
1266 int mustmatch_p, only_dir_p;
1267 xg_get_file_func *func;
1269 GtkWidget *filewin;
1270 GtkFileSelection *filesel;
1272 filewin = gtk_file_selection_new (prompt);
1273 filesel = GTK_FILE_SELECTION (filewin);
1275 if (default_filename)
1276 gtk_file_selection_set_filename (filesel, default_filename);
1278 if (mustmatch_p)
1280 /* The selection_entry part of filesel is not documented. */
1281 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1282 gtk_file_selection_hide_fileop_buttons (filesel);
1285 *func = xg_get_file_name_from_selector;
1287 return filewin;
1289 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1291 /* Read a file name from the user using a file dialog, either the old
1292 file selection dialog, or the new file chooser dialog. Which to use
1293 depends on what the GTK version used has, and what the value of
1294 gtk-use-old-file-dialog.
1295 F is the current frame.
1296 PROMPT is a prompt to show to the user. May not be NULL.
1297 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1298 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1299 file.
1301 Returns a file name or NULL if no file was selected.
1302 The returned string must be freed by the caller. */
1304 char *
1305 xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1306 FRAME_PTR f;
1307 char *prompt;
1308 char *default_filename;
1309 int mustmatch_p, only_dir_p;
1311 GtkWidget *w = 0;
1312 int count = SPECPDL_INDEX ();
1313 char *fn = 0;
1314 int filesel_done = 0;
1315 xg_get_file_func func;
1316 extern int x_use_old_gtk_file_dialog;
1318 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1319 /* I really don't know why this is needed, but without this the GLIBC add on
1320 library linuxthreads hangs when the Gnome file chooser backend creates
1321 threads. */
1322 sigblock (sigmask (__SIGRTMIN));
1323 #endif /* HAVE_GTK_AND_PTHREAD */
1325 #ifdef HAVE_GTK_FILE_BOTH
1327 if (x_use_old_gtk_file_dialog)
1328 w = xg_get_file_with_selection (f, prompt, default_filename,
1329 mustmatch_p, only_dir_p, &func);
1330 else
1331 w = xg_get_file_with_chooser (f, prompt, default_filename,
1332 mustmatch_p, only_dir_p, &func);
1334 #else /* not HAVE_GTK_FILE_BOTH */
1336 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1337 w = xg_get_file_with_selection (f, prompt, default_filename,
1338 mustmatch_p, only_dir_p, &func);
1339 #endif
1340 #ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1341 w = xg_get_file_with_chooser (f, prompt, default_filename,
1342 mustmatch_p, only_dir_p, &func);
1343 #endif
1345 #endif /* HAVE_GTK_FILE_BOTH */
1347 xg_set_screen (w, f);
1348 gtk_widget_set_name (w, "emacs-filedialog");
1349 gtk_window_set_transient_for (GTK_WINDOW (w),
1350 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1351 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1352 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1354 g_signal_connect (G_OBJECT (w),
1355 "response",
1356 G_CALLBACK (xg_file_response_cb),
1357 &filesel_done);
1359 /* Don't destroy the widget if closed by the window manager close button. */
1360 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1362 gtk_widget_show (w);
1364 record_unwind_protect (pop_down_file_dialog, make_save_value (w, 0));
1365 while (! filesel_done)
1367 x_menu_wait_for_event (0);
1368 gtk_main_iteration ();
1371 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1372 sigunblock (sigmask (__SIGRTMIN));
1373 #endif
1375 if (filesel_done == GTK_RESPONSE_OK)
1376 fn = (*func) (w);
1378 unbind_to (count, Qnil);
1380 return fn;
1384 /***********************************************************************
1385 Menu functions.
1386 ***********************************************************************/
1388 /* The name of menu items that can be used for citomization. Since GTK
1389 RC files are very crude and primitive, we have to set this on all
1390 menu item names so a user can easily cutomize menu items. */
1392 #define MENU_ITEM_NAME "emacs-menuitem"
1395 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1396 during GC. The next member points to the items. */
1397 static xg_list_node xg_menu_cb_list;
1399 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1400 during GC. The next member points to the items. */
1401 static xg_list_node xg_menu_item_cb_list;
1403 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1404 F is the frame CL_DATA will be initialized for.
1405 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1407 The menu bar and all sub menus under the menu bar in a frame
1408 share the same structure, hence the reference count.
1410 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1411 allocated xg_menu_cb_data if CL_DATA is NULL. */
1413 static xg_menu_cb_data *
1414 make_cl_data (cl_data, f, highlight_cb)
1415 xg_menu_cb_data *cl_data;
1416 FRAME_PTR f;
1417 GCallback highlight_cb;
1419 if (! cl_data)
1421 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1422 cl_data->f = f;
1423 cl_data->menu_bar_vector = f->menu_bar_vector;
1424 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1425 cl_data->highlight_cb = highlight_cb;
1426 cl_data->ref_count = 0;
1428 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1431 cl_data->ref_count++;
1433 return cl_data;
1436 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1437 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1439 When the menu bar is updated, menu items may have been added and/or
1440 removed, so menu_bar_vector and menu_bar_items_used change. We must
1441 then update CL_DATA since it is used to determine which menu
1442 item that is invoked in the menu.
1443 HIGHLIGHT_CB could change, there is no check that the same
1444 function is given when modifying a menu bar as was given when
1445 creating the menu bar. */
1447 static void
1448 update_cl_data (cl_data, f, highlight_cb)
1449 xg_menu_cb_data *cl_data;
1450 FRAME_PTR f;
1451 GCallback highlight_cb;
1453 if (cl_data)
1455 cl_data->f = f;
1456 cl_data->menu_bar_vector = f->menu_bar_vector;
1457 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1458 cl_data->highlight_cb = highlight_cb;
1462 /* Decrease reference count for CL_DATA.
1463 If reference count is zero, free CL_DATA. */
1465 static void
1466 unref_cl_data (cl_data)
1467 xg_menu_cb_data *cl_data;
1469 if (cl_data && cl_data->ref_count > 0)
1471 cl_data->ref_count--;
1472 if (cl_data->ref_count == 0)
1474 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1475 xfree (cl_data);
1480 /* Function that marks all lisp data during GC. */
1482 void
1483 xg_mark_data ()
1485 xg_list_node *iter;
1487 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1488 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1490 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1492 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1494 if (! NILP (cb_data->help))
1495 mark_object (cb_data->help);
1500 /* Callback called when a menu item is destroyed. Used to free data.
1501 W is the widget that is being destroyed (not used).
1502 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1504 static void
1505 menuitem_destroy_callback (w, client_data)
1506 GtkWidget *w;
1507 gpointer client_data;
1509 if (client_data)
1511 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1512 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1513 xfree (data);
1517 /* Callback called when the pointer enters/leaves a menu item.
1518 W is the menu item.
1519 EVENT is either an enter event or leave event.
1520 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1522 Returns FALSE to tell GTK to keep processing this event. */
1524 static gboolean
1525 menuitem_highlight_callback (w, event, client_data)
1526 GtkWidget *w;
1527 GdkEventCrossing *event;
1528 gpointer client_data;
1530 if (client_data)
1532 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1533 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1535 if (! NILP (data->help) && data->cl_data->highlight_cb)
1537 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1538 (*func) (w, call_data);
1542 return FALSE;
1545 /* Callback called when a menu is destroyed. Used to free data.
1546 W is the widget that is being destroyed (not used).
1547 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1549 static void
1550 menu_destroy_callback (w, client_data)
1551 GtkWidget *w;
1552 gpointer client_data;
1554 unref_cl_data ((xg_menu_cb_data*) client_data);
1557 /* Callback called when a menu does a grab or ungrab. That means the
1558 menu has been activated or deactivated.
1559 Used to start a timer so the small timeout the menus in GTK uses before
1560 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1561 W is the widget that does the grab (not used).
1562 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1563 CLIENT_DATA is NULL (not used). */
1565 static void
1566 menu_grab_callback (GtkWidget *widget,
1567 gboolean ungrab_p,
1568 gpointer client_data)
1570 /* Keep track of total number of grabs. */
1571 static int cnt;
1573 if (ungrab_p) cnt--;
1574 else cnt++;
1576 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1577 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1580 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1581 must be non-NULL) and can be inserted into a menu item.
1583 Returns the GtkHBox. */
1585 static GtkWidget *
1586 make_widget_for_menu_item (utf8_label, utf8_key)
1587 char *utf8_label;
1588 char *utf8_key;
1590 GtkWidget *wlbl;
1591 GtkWidget *wkey;
1592 GtkWidget *wbox;
1594 wbox = gtk_hbox_new (FALSE, 0);
1595 wlbl = gtk_label_new (utf8_label);
1596 wkey = gtk_label_new (utf8_key);
1598 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1599 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1601 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1602 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1604 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1605 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1606 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1608 return wbox;
1611 /* Make and return a menu item widget with the key to the right.
1612 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1613 UTF8_KEY is the text representing the key binding.
1614 ITEM is the widget_value describing the menu item.
1616 GROUP is an in/out parameter. If the menu item to be created is not
1617 part of any radio menu group, *GROUP contains NULL on entry and exit.
1618 If the menu item to be created is part of a radio menu group, on entry
1619 *GROUP contains the group to use, or NULL if this is the first item
1620 in the group. On exit, *GROUP contains the radio item group.
1622 Unfortunately, keys don't line up as nicely as in Motif,
1623 but the MacOS X version doesn't either, so I guess that is OK. */
1625 static GtkWidget *
1626 make_menu_item (utf8_label, utf8_key, item, group)
1627 char *utf8_label;
1628 char *utf8_key;
1629 widget_value *item;
1630 GSList **group;
1632 GtkWidget *w;
1633 GtkWidget *wtoadd = 0;
1635 /* It has been observed that some menu items have a NULL name field.
1636 This will lead to this function being called with a NULL utf8_label.
1637 GTK crashes on that so we set a blank label. Why there is a NULL
1638 name remains to be investigated. */
1639 if (! utf8_label) utf8_label = " ";
1641 if (utf8_key)
1642 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1644 if (item->button_type == BUTTON_TYPE_TOGGLE)
1646 *group = NULL;
1647 if (utf8_key) w = gtk_check_menu_item_new ();
1648 else w = gtk_check_menu_item_new_with_label (utf8_label);
1649 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1651 else if (item->button_type == BUTTON_TYPE_RADIO)
1653 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1654 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1655 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1656 if (item->selected)
1657 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1659 else
1661 *group = NULL;
1662 if (utf8_key) w = gtk_menu_item_new ();
1663 else w = gtk_menu_item_new_with_label (utf8_label);
1666 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1667 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1669 return w;
1672 /* Return non-zero if LABEL specifies a separator (GTK only has one
1673 separator type) */
1675 static int
1676 xg_separator_p (char *label)
1678 if (! label) return 0;
1679 else if (strlen (label) > 3
1680 && strncmp (label, "--", 2) == 0
1681 && label[2] != '-')
1683 static char* separator_names[] = {
1684 "space",
1685 "no-line",
1686 "single-line",
1687 "double-line",
1688 "single-dashed-line",
1689 "double-dashed-line",
1690 "shadow-etched-in",
1691 "shadow-etched-out",
1692 "shadow-etched-in-dash",
1693 "shadow-etched-out-dash",
1694 "shadow-double-etched-in",
1695 "shadow-double-etched-out",
1696 "shadow-double-etched-in-dash",
1697 "shadow-double-etched-out-dash",
1701 int i;
1703 label += 2;
1704 for (i = 0; separator_names[i]; ++i)
1705 if (strcmp (label, separator_names[i]) == 0)
1706 return 1;
1708 else
1710 /* Old-style separator, maybe. It's a separator if it contains
1711 only dashes. */
1712 while (*label == '-')
1713 ++label;
1714 if (*label == 0) return 1;
1717 return 0;
1720 static int xg_detached_menus;
1722 /* Returns non-zero if there are detached menus. */
1725 xg_have_tear_offs ()
1727 return xg_detached_menus > 0;
1730 /* Callback invoked when a detached menu window is removed. Here we
1731 decrease the xg_detached_menus count.
1732 WIDGET is the top level window that is removed (the parent of the menu).
1733 CLIENT_DATA is not used. */
1735 static void
1736 tearoff_remove (widget, client_data)
1737 GtkWidget *widget;
1738 gpointer client_data;
1740 if (xg_detached_menus > 0) --xg_detached_menus;
1743 /* Callback invoked when a menu is detached. It increases the
1744 xg_detached_menus count.
1745 WIDGET is the GtkTearoffMenuItem.
1746 CLIENT_DATA is not used. */
1748 static void
1749 tearoff_activate (widget, client_data)
1750 GtkWidget *widget;
1751 gpointer client_data;
1753 GtkWidget *menu = gtk_widget_get_parent (widget);
1754 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1756 ++xg_detached_menus;
1757 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1758 "destroy",
1759 G_CALLBACK (tearoff_remove), 0);
1764 /* Create a menu item widget, and connect the callbacks.
1765 ITEM decribes the menu item.
1766 F is the frame the created menu belongs to.
1767 SELECT_CB is the callback to use when a menu item is selected.
1768 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1769 CL_DATA points to the callback data to be used for this menu.
1770 GROUP is an in/out parameter. If the menu item to be created is not
1771 part of any radio menu group, *GROUP contains NULL on entry and exit.
1772 If the menu item to be created is part of a radio menu group, on entry
1773 *GROUP contains the group to use, or NULL if this is the first item
1774 in the group. On exit, *GROUP contains the radio item group.
1776 Returns the created GtkWidget. */
1778 static GtkWidget *
1779 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1780 widget_value *item;
1781 FRAME_PTR f;
1782 GCallback select_cb;
1783 GCallback highlight_cb;
1784 xg_menu_cb_data *cl_data;
1785 GSList **group;
1787 char *utf8_label;
1788 char *utf8_key;
1789 GtkWidget *w;
1790 xg_menu_item_cb_data *cb_data;
1792 utf8_label = get_utf8_string (item->name);
1793 utf8_key = get_utf8_string (item->key);
1795 w = make_menu_item (utf8_label, utf8_key, item, group);
1797 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1798 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1800 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1802 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1804 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1805 cb_data->help = item->help;
1806 cb_data->cl_data = cl_data;
1807 cb_data->call_data = item->call_data;
1809 g_signal_connect (G_OBJECT (w),
1810 "destroy",
1811 G_CALLBACK (menuitem_destroy_callback),
1812 cb_data);
1814 /* Put cb_data in widget, so we can get at it when modifying menubar */
1815 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1817 /* final item, not a submenu */
1818 if (item->call_data && ! item->contents)
1820 if (select_cb)
1821 cb_data->select_id
1822 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1825 if (! NILP (item->help) && highlight_cb)
1827 /* We use enter/leave notify instead of select/deselect because
1828 select/deselect doesn't go well with detached menus. */
1829 cb_data->highlight_id
1830 = g_signal_connect (G_OBJECT (w),
1831 "enter-notify-event",
1832 G_CALLBACK (menuitem_highlight_callback),
1833 cb_data);
1834 cb_data->unhighlight_id
1835 = g_signal_connect (G_OBJECT (w),
1836 "leave-notify-event",
1837 G_CALLBACK (menuitem_highlight_callback),
1838 cb_data);
1841 return w;
1844 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1845 GCallback, GCallback, int, int, int,
1846 GtkWidget *, xg_menu_cb_data *, char *));
1848 /* Create a full menu tree specified by DATA.
1849 F is the frame the created menu belongs to.
1850 SELECT_CB is the callback to use when a menu item is selected.
1851 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1852 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1853 POP_UP_P is non-zero if we shall create a popup menu.
1854 MENU_BAR_P is non-zero if we shall create a menu bar.
1855 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1856 if MENU_BAR_P is non-zero.
1857 TOPMENU is the topmost GtkWidget that others shall be placed under.
1858 It may be NULL, in that case we create the appropriate widget
1859 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1860 CL_DATA is the callback data we shall use for this menu, or NULL
1861 if we haven't set the first callback yet.
1862 NAME is the name to give to the top level menu if this function
1863 creates it. May be NULL to not set any name.
1865 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1866 not NULL.
1868 This function calls itself to create submenus. */
1870 static GtkWidget *
1871 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1872 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1873 widget_value *data;
1874 FRAME_PTR f;
1875 GCallback select_cb;
1876 GCallback deactivate_cb;
1877 GCallback highlight_cb;
1878 int pop_up_p;
1879 int menu_bar_p;
1880 int add_tearoff_p;
1881 GtkWidget *topmenu;
1882 xg_menu_cb_data *cl_data;
1883 char *name;
1885 widget_value *item;
1886 GtkWidget *wmenu = topmenu;
1887 GSList *group = NULL;
1889 if (! topmenu)
1891 if (! menu_bar_p)
1893 wmenu = gtk_menu_new ();
1894 xg_set_screen (wmenu, f);
1896 else wmenu = gtk_menu_bar_new ();
1898 /* Put cl_data on the top menu for easier access. */
1899 cl_data = make_cl_data (cl_data, f, highlight_cb);
1900 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1901 g_signal_connect (G_OBJECT (wmenu), "destroy",
1902 G_CALLBACK (menu_destroy_callback), cl_data);
1904 if (name)
1905 gtk_widget_set_name (wmenu, name);
1907 if (deactivate_cb)
1908 g_signal_connect (G_OBJECT (wmenu),
1909 "selection-done", deactivate_cb, 0);
1911 g_signal_connect (G_OBJECT (wmenu),
1912 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1915 if (! menu_bar_p && add_tearoff_p)
1917 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1918 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1920 g_signal_connect (G_OBJECT (tearoff), "activate",
1921 G_CALLBACK (tearoff_activate), 0);
1924 for (item = data; item; item = item->next)
1926 GtkWidget *w;
1928 if (pop_up_p && !item->contents && !item->call_data
1929 && !xg_separator_p (item->name))
1931 char *utf8_label;
1932 /* A title for a popup. We do the same as GTK does when
1933 creating titles, but it does not look good. */
1934 group = NULL;
1935 utf8_label = get_utf8_string (item->name);
1937 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1938 w = gtk_menu_item_new_with_label (utf8_label);
1939 gtk_widget_set_sensitive (w, FALSE);
1940 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1942 else if (xg_separator_p (item->name))
1944 group = NULL;
1945 /* GTK only have one separator type. */
1946 w = gtk_separator_menu_item_new ();
1948 else
1950 w = xg_create_one_menuitem (item,
1952 item->contents ? 0 : select_cb,
1953 highlight_cb,
1954 cl_data,
1955 &group);
1957 if (item->contents)
1959 GtkWidget *submenu = create_menus (item->contents,
1961 select_cb,
1962 deactivate_cb,
1963 highlight_cb,
1966 add_tearoff_p,
1968 cl_data,
1970 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1974 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1975 gtk_widget_set_name (w, MENU_ITEM_NAME);
1978 return wmenu;
1981 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1982 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1983 with some text and buttons.
1984 F is the frame the created item belongs to.
1985 NAME is the name to use for the top widget.
1986 VAL is a widget_value structure describing items to be created.
1987 SELECT_CB is the callback to use when a menu item is selected or
1988 a dialog button is pressed.
1989 DEACTIVATE_CB is the callback to use when an item is deactivated.
1990 For a menu, when a sub menu is not shown anymore, for a dialog it is
1991 called when the dialog is popped down.
1992 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1994 Returns the widget created. */
1996 GtkWidget *
1997 xg_create_widget (type, name, f, val,
1998 select_cb, deactivate_cb, highlight_cb)
1999 char *type;
2000 char *name;
2001 FRAME_PTR f;
2002 widget_value *val;
2003 GCallback select_cb;
2004 GCallback deactivate_cb;
2005 GCallback highlight_cb;
2007 GtkWidget *w = 0;
2008 int menu_bar_p = strcmp (type, "menubar") == 0;
2009 int pop_up_p = strcmp (type, "popup") == 0;
2011 if (strcmp (type, "dialog") == 0)
2013 w = create_dialog (val, select_cb, deactivate_cb);
2014 xg_set_screen (w, f);
2015 gtk_window_set_transient_for (GTK_WINDOW (w),
2016 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2017 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2018 gtk_widget_set_name (w, "emacs-dialog");
2019 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2021 else if (menu_bar_p || pop_up_p)
2023 w = create_menus (val->contents,
2025 select_cb,
2026 deactivate_cb,
2027 highlight_cb,
2028 pop_up_p,
2029 menu_bar_p,
2030 menu_bar_p,
2033 name);
2035 /* Set the cursor to an arrow for popup menus when they are mapped.
2036 This is done by default for menu bar menus. */
2037 if (pop_up_p)
2039 /* Must realize so the GdkWindow inside the widget is created. */
2040 gtk_widget_realize (w);
2041 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2044 else
2046 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2047 type);
2050 return w;
2053 /* Return the label for menu item WITEM. */
2055 static const char *
2056 xg_get_menu_item_label (witem)
2057 GtkMenuItem *witem;
2059 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2060 return gtk_label_get_label (wlabel);
2063 /* Return non-zero if the menu item WITEM has the text LABEL. */
2065 static int
2066 xg_item_label_same_p (witem, label)
2067 GtkMenuItem *witem;
2068 char *label;
2070 int is_same = 0;
2071 char *utf8_label = get_utf8_string (label);
2072 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2074 if (! old_label && ! utf8_label)
2075 is_same = 1;
2076 else if (old_label && utf8_label)
2077 is_same = strcmp (utf8_label, old_label) == 0;
2079 if (utf8_label && utf8_label != label) g_free (utf8_label);
2081 return is_same;
2084 /* Destroy widgets in LIST. */
2086 static void
2087 xg_destroy_widgets (list)
2088 GList *list;
2090 GList *iter;
2092 for (iter = list; iter; iter = g_list_next (iter))
2094 GtkWidget *w = GTK_WIDGET (iter->data);
2096 /* Destroying the widget will remove it from the container it is in. */
2097 gtk_widget_destroy (w);
2101 /* Update the top level names in MENUBAR (i.e. not submenus).
2102 F is the frame the menu bar belongs to.
2103 *LIST is a list with the current menu bar names (menu item widgets).
2104 ITER is the item within *LIST that shall be updated.
2105 POS is the numerical position, starting at 0, of ITER in *LIST.
2106 VAL describes what the menu bar shall look like after the update.
2107 SELECT_CB is the callback to use when a menu item is selected.
2108 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2109 CL_DATA points to the callback data to be used for this menu bar.
2111 This function calls itself to walk through the menu bar names. */
2113 static void
2114 xg_update_menubar (menubar, f, list, iter, pos, val,
2115 select_cb, highlight_cb, cl_data)
2116 GtkWidget *menubar;
2117 FRAME_PTR f;
2118 GList **list;
2119 GList *iter;
2120 int pos;
2121 widget_value *val;
2122 GCallback select_cb;
2123 GCallback highlight_cb;
2124 xg_menu_cb_data *cl_data;
2126 if (! iter && ! val)
2127 return;
2128 else if (iter && ! val)
2130 /* Item(s) have been removed. Remove all remaining items. */
2131 xg_destroy_widgets (iter);
2133 /* All updated. */
2134 val = 0;
2135 iter = 0;
2137 else if (! iter && val)
2139 /* Item(s) added. Add all new items in one call. */
2140 create_menus (val, f, select_cb, 0, highlight_cb,
2141 0, 1, 0, menubar, cl_data, 0);
2143 /* All updated. */
2144 val = 0;
2145 iter = 0;
2147 /* Below this neither iter or val is NULL */
2148 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2150 /* This item is still the same, check next item. */
2151 val = val->next;
2152 iter = g_list_next (iter);
2153 ++pos;
2155 else /* This item is changed. */
2157 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2158 GtkMenuItem *witem2 = 0;
2159 int val_in_menubar = 0;
2160 int iter_in_new_menubar = 0;
2161 GList *iter2;
2162 widget_value *cur;
2164 /* See if the changed entry (val) is present later in the menu bar */
2165 for (iter2 = iter;
2166 iter2 && ! val_in_menubar;
2167 iter2 = g_list_next (iter2))
2169 witem2 = GTK_MENU_ITEM (iter2->data);
2170 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2173 /* See if the current entry (iter) is present later in the
2174 specification for the new menu bar. */
2175 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2176 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2178 if (val_in_menubar && ! iter_in_new_menubar)
2180 int nr = pos;
2182 /* This corresponds to:
2183 Current: A B C
2184 New: A C
2185 Remove B. */
2187 gtk_widget_ref (GTK_WIDGET (witem));
2188 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2189 gtk_widget_destroy (GTK_WIDGET (witem));
2191 /* Must get new list since the old changed. */
2192 g_list_free (*list);
2193 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2194 while (nr-- > 0) iter = g_list_next (iter);
2196 else if (! val_in_menubar && ! iter_in_new_menubar)
2198 /* This corresponds to:
2199 Current: A B C
2200 New: A X C
2201 Rename B to X. This might seem to be a strange thing to do,
2202 since if there is a menu under B it will be totally wrong for X.
2203 But consider editing a C file. Then there is a C-mode menu
2204 (corresponds to B above).
2205 If then doing C-x C-f the minibuf menu (X above) replaces the
2206 C-mode menu. When returning from the minibuffer, we get
2207 back the C-mode menu. Thus we do:
2208 Rename B to X (C-mode to minibuf menu)
2209 Rename X to B (minibuf to C-mode menu).
2210 If the X menu hasn't been invoked, the menu under B
2211 is up to date when leaving the minibuffer. */
2212 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2213 char *utf8_label = get_utf8_string (val->name);
2214 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2216 gtk_label_set_text (wlabel, utf8_label);
2218 /* If this item has a submenu that has been detached, change
2219 the title in the WM decorations also. */
2220 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2221 /* Set the title of the detached window. */
2222 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2224 iter = g_list_next (iter);
2225 val = val->next;
2226 ++pos;
2228 else if (! val_in_menubar && iter_in_new_menubar)
2230 /* This corresponds to:
2231 Current: A B C
2232 New: A X B C
2233 Insert X. */
2235 int nr = pos;
2236 GList *group = 0;
2237 GtkWidget *w = xg_create_one_menuitem (val,
2239 select_cb,
2240 highlight_cb,
2241 cl_data,
2242 &group);
2244 gtk_widget_set_name (w, MENU_ITEM_NAME);
2245 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2247 g_list_free (*list);
2248 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2249 while (nr-- > 0) iter = g_list_next (iter);
2250 iter = g_list_next (iter);
2251 val = val->next;
2252 ++pos;
2254 else /* if (val_in_menubar && iter_in_new_menubar) */
2256 int nr = pos;
2257 /* This corresponds to:
2258 Current: A B C
2259 New: A C B
2260 Move C before B */
2262 gtk_widget_ref (GTK_WIDGET (witem2));
2263 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2264 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2265 GTK_WIDGET (witem2), pos);
2266 gtk_widget_unref (GTK_WIDGET (witem2));
2268 g_list_free (*list);
2269 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2270 while (nr-- > 0) iter = g_list_next (iter);
2271 val = val->next;
2272 ++pos;
2276 /* Update the rest of the menu bar. */
2277 xg_update_menubar (menubar, f, list, iter, pos, val,
2278 select_cb, highlight_cb, cl_data);
2281 /* Update the menu item W so it corresponds to VAL.
2282 SELECT_CB is the callback to use when a menu item is selected.
2283 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2284 CL_DATA is the data to set in the widget for menu invokation. */
2286 static void
2287 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2288 widget_value *val;
2289 GtkWidget *w;
2290 GCallback select_cb;
2291 GCallback highlight_cb;
2292 xg_menu_cb_data *cl_data;
2294 GtkWidget *wchild;
2295 GtkLabel *wlbl = 0;
2296 GtkLabel *wkey = 0;
2297 char *utf8_label;
2298 char *utf8_key;
2299 const char *old_label = 0;
2300 const char *old_key = 0;
2301 xg_menu_item_cb_data *cb_data;
2303 wchild = gtk_bin_get_child (GTK_BIN (w));
2304 utf8_label = get_utf8_string (val->name);
2305 utf8_key = get_utf8_string (val->key);
2307 /* See if W is a menu item with a key. See make_menu_item above. */
2308 if (GTK_IS_HBOX (wchild))
2310 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2312 wlbl = GTK_LABEL (list->data);
2313 wkey = GTK_LABEL (list->next->data);
2314 g_list_free (list);
2316 if (! utf8_key)
2318 /* Remove the key and keep just the label. */
2319 gtk_widget_ref (GTK_WIDGET (wlbl));
2320 gtk_container_remove (GTK_CONTAINER (w), wchild);
2321 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2322 wkey = 0;
2326 else /* Just a label. */
2328 wlbl = GTK_LABEL (wchild);
2330 /* Check if there is now a key. */
2331 if (utf8_key)
2333 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2334 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2336 wlbl = GTK_LABEL (list->data);
2337 wkey = GTK_LABEL (list->next->data);
2338 g_list_free (list);
2340 gtk_container_remove (GTK_CONTAINER (w), wchild);
2341 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2346 if (wkey) old_key = gtk_label_get_label (wkey);
2347 if (wlbl) old_label = gtk_label_get_label (wlbl);
2349 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2350 gtk_label_set_text (wkey, utf8_key);
2352 if (! old_label || strcmp (utf8_label, old_label) != 0)
2353 gtk_label_set_text (wlbl, utf8_label);
2355 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2356 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2358 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2359 gtk_widget_set_sensitive (w, FALSE);
2360 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2361 gtk_widget_set_sensitive (w, TRUE);
2363 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2364 XG_ITEM_DATA);
2365 if (cb_data)
2367 cb_data->call_data = val->call_data;
2368 cb_data->help = val->help;
2369 cb_data->cl_data = cl_data;
2371 /* We assume the callback functions don't change. */
2372 if (val->call_data && ! val->contents)
2374 /* This item shall have a select callback. */
2375 if (! cb_data->select_id)
2376 cb_data->select_id
2377 = g_signal_connect (G_OBJECT (w), "activate",
2378 select_cb, cb_data);
2380 else if (cb_data->select_id)
2382 g_signal_handler_disconnect (w, cb_data->select_id);
2383 cb_data->select_id = 0;
2386 if (NILP (cb_data->help))
2388 /* Shall not have help. Remove if any existed previously. */
2389 if (cb_data->highlight_id)
2391 g_signal_handler_disconnect (G_OBJECT (w),
2392 cb_data->highlight_id);
2393 cb_data->highlight_id = 0;
2395 if (cb_data->unhighlight_id)
2397 g_signal_handler_disconnect (G_OBJECT (w),
2398 cb_data->unhighlight_id);
2399 cb_data->unhighlight_id = 0;
2402 else if (! cb_data->highlight_id && highlight_cb)
2404 /* Have help now, but didn't previously. Add callback. */
2405 cb_data->highlight_id
2406 = g_signal_connect (G_OBJECT (w),
2407 "enter-notify-event",
2408 G_CALLBACK (menuitem_highlight_callback),
2409 cb_data);
2410 cb_data->unhighlight_id
2411 = g_signal_connect (G_OBJECT (w),
2412 "leave-notify-event",
2413 G_CALLBACK (menuitem_highlight_callback),
2414 cb_data);
2419 /* Update the toggle menu item W so it corresponds to VAL. */
2421 static void
2422 xg_update_toggle_item (val, w)
2423 widget_value *val;
2424 GtkWidget *w;
2426 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2429 /* Update the radio menu item W so it corresponds to VAL. */
2431 static void
2432 xg_update_radio_item (val, w)
2433 widget_value *val;
2434 GtkWidget *w;
2436 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2439 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2440 SUBMENU may be NULL, in that case a new menu is created.
2441 F is the frame the menu bar belongs to.
2442 VAL describes the contents of the menu bar.
2443 SELECT_CB is the callback to use when a menu item is selected.
2444 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2445 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2446 CL_DATA is the call back data to use for any newly created items.
2448 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2449 was NULL. */
2451 static GtkWidget *
2452 xg_update_submenu (submenu, f, val,
2453 select_cb, deactivate_cb, highlight_cb, cl_data)
2454 GtkWidget *submenu;
2455 FRAME_PTR f;
2456 widget_value *val;
2457 GCallback select_cb;
2458 GCallback deactivate_cb;
2459 GCallback highlight_cb;
2460 xg_menu_cb_data *cl_data;
2462 GtkWidget *newsub = submenu;
2463 GList *list = 0;
2464 GList *iter;
2465 widget_value *cur;
2466 int has_tearoff_p = 0;
2467 GList *first_radio = 0;
2469 if (submenu)
2470 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2472 for (cur = val, iter = list;
2473 cur && iter;
2474 iter = g_list_next (iter), cur = cur->next)
2476 GtkWidget *w = GTK_WIDGET (iter->data);
2478 /* Skip tearoff items, they have no counterpart in val. */
2479 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2481 has_tearoff_p = 1;
2482 iter = g_list_next (iter);
2483 if (iter) w = GTK_WIDGET (iter->data);
2484 else break;
2487 /* Remember first radio button in a group. If we get a mismatch in
2488 a radio group we must rebuild the whole group so that the connections
2489 in GTK becomes correct. */
2490 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2491 first_radio = iter;
2492 else if (cur->button_type != BUTTON_TYPE_RADIO
2493 && ! GTK_IS_RADIO_MENU_ITEM (w))
2494 first_radio = 0;
2496 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2498 if (! xg_separator_p (cur->name))
2499 break;
2501 else if (GTK_IS_CHECK_MENU_ITEM (w))
2503 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2504 break;
2505 xg_update_toggle_item (cur, w);
2506 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2508 else if (GTK_IS_RADIO_MENU_ITEM (w))
2510 if (cur->button_type != BUTTON_TYPE_RADIO)
2511 break;
2512 xg_update_radio_item (cur, w);
2513 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2515 else if (GTK_IS_MENU_ITEM (w))
2517 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2518 GtkWidget *sub;
2520 if (cur->button_type != BUTTON_TYPE_NONE ||
2521 xg_separator_p (cur->name))
2522 break;
2524 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2526 sub = gtk_menu_item_get_submenu (witem);
2527 if (sub && ! cur->contents)
2529 /* Not a submenu anymore. */
2530 gtk_widget_ref (sub);
2531 gtk_menu_item_remove_submenu (witem);
2532 gtk_widget_destroy (sub);
2534 else if (cur->contents)
2536 GtkWidget *nsub;
2538 nsub = xg_update_submenu (sub, f, cur->contents,
2539 select_cb, deactivate_cb,
2540 highlight_cb, cl_data);
2542 /* If this item just became a submenu, we must set it. */
2543 if (nsub != sub)
2544 gtk_menu_item_set_submenu (witem, nsub);
2547 else
2549 /* Structural difference. Remove everything from here and down
2550 in SUBMENU. */
2551 break;
2555 /* Remove widgets from first structual change. */
2556 if (iter)
2558 /* If we are adding new menu items below, we must remove from
2559 first radio button so that radio groups become correct. */
2560 if (cur && first_radio) xg_destroy_widgets (first_radio);
2561 else xg_destroy_widgets (iter);
2564 if (cur)
2566 /* More items added. Create them. */
2567 newsub = create_menus (cur,
2569 select_cb,
2570 deactivate_cb,
2571 highlight_cb,
2574 ! has_tearoff_p,
2575 submenu,
2576 cl_data,
2580 if (list) g_list_free (list);
2582 return newsub;
2585 /* Update the MENUBAR.
2586 F is the frame the menu bar belongs to.
2587 VAL describes the contents of the menu bar.
2588 If DEEP_P is non-zero, rebuild all but the top level menu names in
2589 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2590 SELECT_CB is the callback to use when a menu item is selected.
2591 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2592 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2594 void
2595 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2596 select_cb, deactivate_cb, highlight_cb)
2597 GtkWidget *menubar;
2598 FRAME_PTR f;
2599 widget_value *val;
2600 int deep_p;
2601 GCallback select_cb;
2602 GCallback deactivate_cb;
2603 GCallback highlight_cb;
2605 xg_menu_cb_data *cl_data;
2606 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2608 if (! list) return;
2610 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2611 XG_FRAME_DATA);
2613 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2614 select_cb, highlight_cb, cl_data);
2616 if (deep_p);
2618 widget_value *cur;
2620 /* Update all sub menus.
2621 We must keep the submenus (GTK menu item widgets) since the
2622 X Window in the XEvent that activates the menu are those widgets. */
2624 /* Update cl_data, menu_item things in F may have changed. */
2625 update_cl_data (cl_data, f, highlight_cb);
2627 for (cur = val->contents; cur; cur = cur->next)
2629 GList *iter;
2630 GtkWidget *sub = 0;
2631 GtkWidget *newsub;
2632 GtkMenuItem *witem;
2634 /* Find sub menu that corresponds to val and update it. */
2635 for (iter = list ; iter; iter = g_list_next (iter))
2637 witem = GTK_MENU_ITEM (iter->data);
2638 if (xg_item_label_same_p (witem, cur->name))
2640 sub = gtk_menu_item_get_submenu (witem);
2641 break;
2645 newsub = xg_update_submenu (sub,
2647 cur->contents,
2648 select_cb,
2649 deactivate_cb,
2650 highlight_cb,
2651 cl_data);
2652 /* sub may still be NULL. If we just updated non deep and added
2653 a new menu bar item, it has no sub menu yet. So we set the
2654 newly created sub menu under witem. */
2655 if (newsub != sub)
2657 xg_set_screen (newsub, f);
2658 gtk_menu_item_set_submenu (witem, newsub);
2663 g_list_free (list);
2664 gtk_widget_show_all (menubar);
2667 /* Recompute all the widgets of frame F, when the menu bar has been
2668 changed. Value is non-zero if widgets were updated. */
2671 xg_update_frame_menubar (f)
2672 FRAME_PTR f;
2674 struct x_output *x = f->output_data.x;
2675 GtkRequisition req;
2677 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2678 return 0;
2680 BLOCK_INPUT;
2682 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2683 FALSE, FALSE, 0);
2684 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2686 gtk_widget_show_all (x->menubar_widget);
2687 gtk_widget_size_request (x->menubar_widget, &req);
2689 FRAME_MENUBAR_HEIGHT (f) = req.height;
2691 /* The height has changed, resize outer widget and set columns
2692 rows to what we had before adding the menu bar. */
2693 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2695 SET_FRAME_GARBAGED (f);
2696 UNBLOCK_INPUT;
2698 return 1;
2701 /* Get rid of the menu bar of frame F, and free its storage.
2702 This is used when deleting a frame, and when turning off the menu bar. */
2704 void
2705 free_frame_menubar (f)
2706 FRAME_PTR f;
2708 struct x_output *x = f->output_data.x;
2710 if (x->menubar_widget)
2712 BLOCK_INPUT;
2714 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2715 /* The menubar and its children shall be deleted when removed from
2716 the container. */
2717 x->menubar_widget = 0;
2718 FRAME_MENUBAR_HEIGHT (f) = 0;
2720 /* The height has changed, resize outer widget and set columns
2721 rows to what we had before removing the menu bar. */
2722 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2724 SET_FRAME_GARBAGED (f);
2725 UNBLOCK_INPUT;
2731 /***********************************************************************
2732 Scroll bar functions
2733 ***********************************************************************/
2736 /* Setting scroll bar values invokes the callback. Use this variable
2737 to indicate that callback should do nothing. */
2739 int xg_ignore_gtk_scrollbar;
2741 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2742 32 bits. But we want to store pointers, and they may be larger
2743 than 32 bits. Keep a mapping from integer index to widget pointers
2744 to get around the 32 bit limitation. */
2746 static struct
2748 GtkWidget **widgets;
2749 int max_size;
2750 int used;
2751 } id_to_widget;
2753 /* Grow this much every time we need to allocate more */
2755 #define ID_TO_WIDGET_INCR 32
2757 /* Store the widget pointer W in id_to_widget and return the integer index. */
2759 static int
2760 xg_store_widget_in_map (w)
2761 GtkWidget *w;
2763 int i;
2765 if (id_to_widget.max_size == id_to_widget.used)
2767 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2769 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2770 sizeof (GtkWidget *)*new_size);
2772 for (i = id_to_widget.max_size; i < new_size; ++i)
2773 id_to_widget.widgets[i] = 0;
2774 id_to_widget.max_size = new_size;
2777 /* Just loop over the array and find a free place. After all,
2778 how many scroll bars are we creating? Should be a small number.
2779 The check above guarantees we will find a free place. */
2780 for (i = 0; i < id_to_widget.max_size; ++i)
2782 if (! id_to_widget.widgets[i])
2784 id_to_widget.widgets[i] = w;
2785 ++id_to_widget.used;
2787 return i;
2791 /* Should never end up here */
2792 abort ();
2795 /* Remove pointer at IDX from id_to_widget.
2796 Called when scroll bar is destroyed. */
2798 static void
2799 xg_remove_widget_from_map (idx)
2800 int idx;
2802 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2804 id_to_widget.widgets[idx] = 0;
2805 --id_to_widget.used;
2809 /* Get the widget pointer at IDX from id_to_widget. */
2811 static GtkWidget *
2812 xg_get_widget_from_map (idx)
2813 int idx;
2815 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2816 return id_to_widget.widgets[idx];
2818 return 0;
2821 /* Return the scrollbar id for X Window WID on display DPY.
2822 Return -1 if WID not in id_to_widget. */
2825 xg_get_scroll_id_for_window (dpy, wid)
2826 Display *dpy;
2827 Window wid;
2829 int idx;
2830 GtkWidget *w;
2832 w = xg_win_to_widget (dpy, wid);
2834 if (w)
2836 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2837 if (id_to_widget.widgets[idx] == w)
2838 return idx;
2841 return -1;
2844 /* Callback invoked when scroll bar WIDGET is destroyed.
2845 DATA is the index into id_to_widget for WIDGET.
2846 We free pointer to last scroll bar values here and remove the index. */
2848 static void
2849 xg_gtk_scroll_destroy (widget, data)
2850 GtkWidget *widget;
2851 gpointer data;
2853 gpointer p;
2854 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
2856 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2857 if (p) xfree (p);
2858 xg_remove_widget_from_map (id);
2861 /* Callback for button press/release events. Used to start timer so that
2862 the scroll bar repetition timer in GTK gets handeled.
2863 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2864 WIDGET is the scroll bar widget the event is for (not used).
2865 EVENT contains the event.
2866 USER_DATA points to the struct scrollbar structure.
2868 Returns FALSE to tell GTK that it shall continue propagate the event
2869 to widgets. */
2871 static gboolean
2872 scroll_bar_button_cb (widget, event, user_data)
2873 GtkWidget *widget;
2874 GdkEventButton *event;
2875 gpointer user_data;
2877 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2878 xg_start_timer ();
2879 else if (event->type == GDK_BUTTON_RELEASE)
2881 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2882 if (xg_timer) xg_stop_timer ();
2883 bar->dragging = Qnil;
2886 return FALSE;
2889 /* Create a scroll bar widget for frame F. Store the scroll bar
2890 in BAR.
2891 SCROLL_CALLBACK is the callback to invoke when the value of the
2892 bar changes.
2893 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2894 to set resources for the widget. */
2896 void
2897 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2898 FRAME_PTR f;
2899 struct scroll_bar *bar;
2900 GCallback scroll_callback;
2901 char *scroll_bar_name;
2903 GtkWidget *wscroll;
2904 GtkWidget *webox;
2905 GtkObject *vadj;
2906 int scroll_id;
2908 /* Page, step increment values are not so important here, they
2909 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2910 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2911 0.1, 0.1, 0.1);
2913 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2914 webox = gtk_event_box_new ();
2915 gtk_widget_set_name (wscroll, scroll_bar_name);
2916 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2918 scroll_id = xg_store_widget_in_map (wscroll);
2920 g_signal_connect (G_OBJECT (wscroll),
2921 "value-changed",
2922 scroll_callback,
2923 (gpointer) bar);
2924 /* The EMACS_INT cast avoids a warning. */
2925 g_signal_connect (G_OBJECT (wscroll),
2926 "destroy",
2927 G_CALLBACK (xg_gtk_scroll_destroy),
2928 (gpointer) (EMACS_INT) scroll_id);
2930 /* Connect to button press and button release to detect if any scroll bar
2931 has the pointer. */
2932 g_signal_connect (G_OBJECT (wscroll),
2933 "button-press-event",
2934 G_CALLBACK (scroll_bar_button_cb),
2935 (gpointer) bar);
2936 g_signal_connect (G_OBJECT (wscroll),
2937 "button-release-event",
2938 G_CALLBACK (scroll_bar_button_cb),
2939 (gpointer) bar);
2941 /* The scroll bar widget does not draw on a window of its own. Instead
2942 it draws on the parent window, in this case the edit widget. So
2943 whenever the edit widget is cleared, the scroll bar needs to redraw
2944 also, which causes flicker. Put an event box between the edit widget
2945 and the scroll bar, so the scroll bar instead draws itself on the
2946 event box window. */
2947 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
2948 gtk_container_add (GTK_CONTAINER (webox), wscroll);
2951 /* Set the cursor to an arrow. */
2952 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2954 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2957 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2959 void
2960 xg_show_scroll_bar (scrollbar_id)
2961 int scrollbar_id;
2963 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2964 if (w)
2965 gtk_widget_show_all (gtk_widget_get_parent (w));
2968 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2970 void
2971 xg_remove_scroll_bar (f, scrollbar_id)
2972 FRAME_PTR f;
2973 int scrollbar_id;
2975 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2976 if (w)
2978 GtkWidget *wparent = gtk_widget_get_parent (w);
2979 gtk_widget_destroy (w);
2980 gtk_widget_destroy (wparent);
2981 SET_FRAME_GARBAGED (f);
2985 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2986 in frame F.
2987 TOP/LEFT are the new pixel positions where the bar shall appear.
2988 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2990 void
2991 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
2992 FRAME_PTR f;
2993 int scrollbar_id;
2994 int top;
2995 int left;
2996 int width;
2997 int height;
3000 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3002 if (wscroll)
3004 GtkWidget *wfixed = f->output_data.x->edit_widget;
3005 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3007 /* Move and resize to new values. */
3008 gtk_widget_set_size_request (wscroll, width, height);
3009 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3011 SET_FRAME_GARBAGED (f);
3012 cancel_mouse_face (f);
3016 /* Set the thumb size and position of scroll bar BAR. We are currently
3017 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3019 void
3020 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3021 struct scroll_bar *bar;
3022 int portion, position, whole;
3024 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
3026 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3028 if (wscroll && NILP (bar->dragging))
3030 GtkAdjustment *adj;
3031 gdouble shown;
3032 gdouble top;
3033 int size, value;
3034 int new_step;
3035 int changed = 0;
3037 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3039 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3040 rather than the real portion value. This makes the thumb less likely
3041 to resize and that looks better. */
3042 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3043 /* When the thumb is at the bottom, position == whole.
3044 So we need to increase `whole' to make space for the thumb. */
3045 whole += portion;
3047 if (whole <= 0)
3048 top = 0, shown = 1;
3049 else
3051 top = (gdouble) position / whole;
3052 shown = (gdouble) portion / whole;
3055 size = shown * XG_SB_RANGE;
3056 size = min (size, XG_SB_RANGE);
3057 size = max (size, 1);
3059 value = top * XG_SB_RANGE;
3060 value = min (value, XG_SB_MAX - size);
3061 value = max (value, XG_SB_MIN);
3063 /* Assume all lines are of equal size. */
3064 new_step = size / max (1, FRAME_LINES (f));
3066 if ((int) adj->page_size != size
3067 || (int) adj->step_increment != new_step)
3069 adj->page_size = size;
3070 adj->step_increment = new_step;
3071 /* Assume a page increment is about 95% of the page size */
3072 adj->page_increment = (int) (0.95*adj->page_size);
3073 changed = 1;
3076 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3078 GtkWidget *wfixed = f->output_data.x->edit_widget;
3080 BLOCK_INPUT;
3082 /* gtk_range_set_value invokes the callback. Set
3083 ignore_gtk_scrollbar to make the callback do nothing */
3084 xg_ignore_gtk_scrollbar = 1;
3086 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3087 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3088 else if (changed)
3089 gtk_adjustment_changed (adj);
3091 xg_ignore_gtk_scrollbar = 0;
3093 UNBLOCK_INPUT;
3099 /***********************************************************************
3100 Tool bar functions
3101 ***********************************************************************/
3102 /* The key for the data we put in the GtkImage widgets. The data is
3103 the image used by Emacs. We use this to see if we need to update
3104 the GtkImage with a new image. */
3105 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3107 /* Callback function invoked when a tool bar item is pressed.
3108 W is the button widget in the tool bar that got pressed,
3109 CLIENT_DATA is an integer that is the index of the button in the
3110 tool bar. 0 is the first button. */
3112 static void
3113 xg_tool_bar_callback (w, client_data)
3114 GtkWidget *w;
3115 gpointer client_data;
3117 /* The EMACS_INT cast avoids a warning. */
3118 int idx = (int) (EMACS_INT) client_data;
3119 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3120 Lisp_Object key, frame;
3121 struct input_event event;
3122 EVENT_INIT (event);
3124 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3125 return;
3127 idx *= TOOL_BAR_ITEM_NSLOTS;
3129 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3130 XSETFRAME (frame, f);
3131 event.kind = TOOL_BAR_EVENT;
3132 event.frame_or_window = frame;
3133 event.arg = frame;
3134 kbd_buffer_store_event (&event);
3136 event.kind = TOOL_BAR_EVENT;
3137 event.frame_or_window = frame;
3138 event.arg = key;
3139 event.modifiers = 0; /* These are not available. */
3140 kbd_buffer_store_event (&event);
3143 /* This callback is called when a tool bar is detached. We must set
3144 the height of the tool bar to zero when this happens so frame sizes
3145 are correctly calculated.
3146 WBOX is the handle box widget that enables detach/attach of the tool bar.
3147 W is the tool bar widget.
3148 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3150 static void
3151 xg_tool_bar_detach_callback (wbox, w, client_data)
3152 GtkHandleBox *wbox;
3153 GtkWidget *w;
3154 gpointer client_data;
3156 FRAME_PTR f = (FRAME_PTR) client_data;
3158 if (f)
3160 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3162 /* When detaching a tool bar, not everything dissapear. There are
3163 a few pixels left that are used to drop the tool bar back into
3164 place. */
3165 FRAME_TOOLBAR_HEIGHT (f) = 2;
3167 /* The height has changed, resize outer widget and set columns
3168 rows to what we had before detaching the tool bar. */
3169 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3173 /* This callback is called when a tool bar is reattached. We must set
3174 the height of the tool bar when this happens so frame sizes
3175 are correctly calculated.
3176 WBOX is the handle box widget that enables detach/attach of the tool bar.
3177 W is the tool bar widget.
3178 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3180 static void
3181 xg_tool_bar_attach_callback (wbox, w, client_data)
3182 GtkHandleBox *wbox;
3183 GtkWidget *w;
3184 gpointer client_data;
3186 FRAME_PTR f = (FRAME_PTR) client_data;
3188 if (f)
3190 GtkRequisition req;
3192 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3194 gtk_widget_size_request (w, &req);
3195 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3197 /* The height has changed, resize outer widget and set columns
3198 rows to what we had before attaching the tool bar. */
3199 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3203 /* This callback is called when the mouse enters or leaves a tool bar item.
3204 It is used for displaying and hiding the help text.
3205 W is the tool bar item, a button.
3206 EVENT is either an enter event or leave event.
3207 CLIENT_DATA is an integer that is the index of the button in the
3208 tool bar. 0 is the first button.
3210 Returns FALSE to tell GTK to keep processing this event. */
3212 static gboolean
3213 xg_tool_bar_help_callback (w, event, client_data)
3214 GtkWidget *w;
3215 GdkEventCrossing *event;
3216 gpointer client_data;
3218 /* The EMACS_INT cast avoids a warning. */
3219 int idx = (int) (EMACS_INT) client_data;
3220 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3221 Lisp_Object help, frame;
3223 if (! GTK_IS_BUTTON (w))
3225 return FALSE;
3228 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3229 return FALSE;
3231 if (event->type == GDK_ENTER_NOTIFY)
3233 idx *= TOOL_BAR_ITEM_NSLOTS;
3234 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3236 if (NILP (help))
3237 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3239 else
3240 help = Qnil;
3242 XSETFRAME (frame, f);
3243 kbd_buffer_store_help_event (frame, help);
3245 return FALSE;
3249 /* This callback is called when a tool bar item shall be redrawn.
3250 It modifies the expose event so that the GtkImage widget redraws the
3251 whole image. This to overcome a bug that makes GtkImage draw the image
3252 in the wrong place when it tries to redraw just a part of the image.
3253 W is the GtkImage to be redrawn.
3254 EVENT is the expose event for W.
3255 CLIENT_DATA is unused.
3257 Returns FALSE to tell GTK to keep processing this event. */
3259 static gboolean
3260 xg_tool_bar_item_expose_callback (w, event, client_data)
3261 GtkWidget *w;
3262 GdkEventExpose *event;
3263 gpointer client_data;
3265 gint width, height;
3267 gdk_drawable_get_size (event->window, &width, &height);
3269 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3270 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3272 event->area.x = max (0, event->area.x);
3273 event->area.y = max (0, event->area.y);
3275 event->area.width = max (width, event->area.width);
3276 event->area.height = max (height, event->area.height);
3278 return FALSE;
3281 /* This callback is called when a tool bar shall be redrawn.
3282 We need to update the tool bar from here in case the image cache
3283 has deleted the pixmaps used in the tool bar.
3284 W is the GtkToolbar to be redrawn.
3285 EVENT is the expose event for W.
3286 CLIENT_DATA is pointing to the frame for this tool bar.
3288 Returns FALSE to tell GTK to keep processing this event. */
3290 static gboolean
3291 xg_tool_bar_expose_callback (w, event, client_data)
3292 GtkWidget *w;
3293 GdkEventExpose *event;
3294 gpointer client_data;
3296 update_frame_tool_bar ((FRAME_PTR) client_data);
3297 return FALSE;
3300 /* Create a tool bar for frame F. */
3302 static void
3303 xg_create_tool_bar (f)
3304 FRAME_PTR f;
3306 struct x_output *x = f->output_data.x;
3307 GtkRequisition req;
3308 int vbox_pos = x->menubar_widget ? 1 : 0;
3310 x->toolbar_widget = gtk_toolbar_new ();
3311 x->handlebox_widget = gtk_handle_box_new ();
3312 x->toolbar_detached = 0;
3314 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3315 x->toolbar_widget);
3317 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3318 FALSE, FALSE, 0);
3320 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3321 vbox_pos);
3323 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3325 /* We only have icons, so override any user setting. We could
3326 use the caption property of the toolbar item (see update_frame_tool_bar
3327 below), but some of those strings are long, making the toolbar so
3328 long it does not fit on the screen. The GtkToolbar widget makes every
3329 item equal size, so the longest caption determine the size of every
3330 tool bar item. I think the creators of the GtkToolbar widget
3331 counted on 4 or 5 character long strings. */
3332 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3333 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3334 GTK_ORIENTATION_HORIZONTAL);
3336 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3337 G_CALLBACK (xg_tool_bar_detach_callback), f);
3338 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3339 G_CALLBACK (xg_tool_bar_attach_callback), f);
3340 g_signal_connect (G_OBJECT (x->toolbar_widget),
3341 "expose-event",
3342 G_CALLBACK (xg_tool_bar_expose_callback),
3345 gtk_widget_show_all (x->handlebox_widget);
3347 gtk_widget_size_request (x->toolbar_widget, &req);
3348 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3350 /* The height has changed, resize outer widget and set columns
3351 rows to what we had before adding the tool bar. */
3352 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3354 SET_FRAME_GARBAGED (f);
3357 /* Update the tool bar for frame F. Add new buttons and remove old. */
3359 void
3360 update_frame_tool_bar (f)
3361 FRAME_PTR f;
3363 int i;
3364 GtkRequisition old_req, new_req;
3365 GList *icon_list;
3366 GList *iter;
3367 struct x_output *x = f->output_data.x;
3368 int hmargin, vmargin;
3370 if (! FRAME_GTK_WIDGET (f))
3371 return;
3373 BLOCK_INPUT;
3375 if (INTEGERP (Vtool_bar_button_margin)
3376 && XINT (Vtool_bar_button_margin) > 0)
3378 hmargin = XFASTINT (Vtool_bar_button_margin);
3379 vmargin = XFASTINT (Vtool_bar_button_margin);
3381 else if (CONSP (Vtool_bar_button_margin))
3383 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3384 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3385 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3387 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3388 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3389 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3392 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3393 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3394 i.e. zero. This means that margins less than
3395 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3396 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3397 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3399 if (! x->toolbar_widget)
3400 xg_create_tool_bar (f);
3402 gtk_widget_size_request (x->toolbar_widget, &old_req);
3404 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3405 iter = icon_list;
3407 for (i = 0; i < f->n_tool_bar_items; ++i)
3409 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3411 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3412 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3413 int idx;
3414 int img_id;
3415 struct image *img;
3416 Lisp_Object image;
3417 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3419 if (iter) iter = g_list_next (iter);
3421 /* If image is a vector, choose the image according to the
3422 button state. */
3423 image = PROP (TOOL_BAR_ITEM_IMAGES);
3424 if (VECTORP (image))
3426 if (enabled_p)
3427 idx = (selected_p
3428 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3429 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3430 else
3431 idx = (selected_p
3432 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3433 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3435 xassert (ASIZE (image) >= idx);
3436 image = AREF (image, idx);
3438 else
3439 idx = -1;
3441 /* Ignore invalid image specifications. */
3442 if (!valid_image_p (image))
3444 if (wicon) gtk_widget_hide (wicon);
3445 continue;
3448 img_id = lookup_image (f, image);
3449 img = IMAGE_FROM_ID (f, img_id);
3450 prepare_image_for_display (f, img);
3452 if (img->load_failed_p || img->pixmap == None)
3454 if (wicon) gtk_widget_hide (wicon);
3455 continue;
3458 if (! wicon)
3460 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3462 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3464 /* The EMACS_INT cast avoids a warning. */
3465 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3466 0, 0, 0,
3468 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3469 (gpointer) (EMACS_INT) i);
3471 /* Save the image so we can see if an update is needed when
3472 this function is called again. */
3473 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3474 (gpointer)img->pixmap);
3476 /* Catch expose events to overcome an annoying redraw bug, see
3477 comment for xg_tool_bar_item_expose_callback. */
3478 g_signal_connect (G_OBJECT (w),
3479 "expose-event",
3480 G_CALLBACK (xg_tool_bar_item_expose_callback),
3483 /* We must set sensitive on the button that is the parent
3484 of the GtkImage parent. Go upwards until we find the button. */
3485 while (! GTK_IS_BUTTON (w))
3486 w = gtk_widget_get_parent (w);
3488 if (w)
3490 /* Save the frame in the button so the xg_tool_bar_callback
3491 can get at it. */
3492 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3493 gtk_widget_set_sensitive (w, enabled_p);
3495 /* Use enter/leave notify to show help. We use the events
3496 rather than the GtkButton specific signals "enter" and
3497 "leave", so we can have only one callback. The event
3498 will tell us what kind of event it is. */
3499 /* The EMACS_INT cast avoids a warning. */
3500 g_signal_connect (G_OBJECT (w),
3501 "enter-notify-event",
3502 G_CALLBACK (xg_tool_bar_help_callback),
3503 (gpointer) (EMACS_INT) i);
3504 g_signal_connect (G_OBJECT (w),
3505 "leave-notify-event",
3506 G_CALLBACK (xg_tool_bar_help_callback),
3507 (gpointer) (EMACS_INT) i);
3510 else
3512 /* The child of the tool bar is a button. Inside that button
3513 is a vbox. Inside that vbox is the GtkImage. */
3514 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3515 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3516 GtkImage *wimage = GTK_IMAGE (chlist->data);
3517 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3518 XG_TOOL_BAR_IMAGE_DATA);
3519 g_list_free (chlist);
3521 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
3523 if (old_img != img->pixmap)
3524 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
3526 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3527 (gpointer)img->pixmap);
3529 gtk_widget_set_sensitive (wicon, enabled_p);
3530 gtk_widget_show (wicon);
3533 #undef PROP
3536 /* Remove buttons not longer needed. We just hide them so they
3537 can be reused later on. */
3538 while (iter)
3540 GtkWidget *w = GTK_WIDGET (iter->data);
3541 gtk_widget_hide (w);
3542 iter = g_list_next (iter);
3545 gtk_widget_size_request (x->toolbar_widget, &new_req);
3546 if (old_req.height != new_req.height
3547 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
3549 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3550 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3553 if (icon_list) g_list_free (icon_list);
3555 UNBLOCK_INPUT;
3558 /* Deallocate all resources for the tool bar on frame F.
3559 Remove the tool bar. */
3561 void
3562 free_frame_tool_bar (f)
3563 FRAME_PTR f;
3565 struct x_output *x = f->output_data.x;
3567 if (x->toolbar_widget)
3569 BLOCK_INPUT;
3570 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3571 x->handlebox_widget);
3572 x->toolbar_widget = 0;
3573 x->handlebox_widget = 0;
3574 FRAME_TOOLBAR_HEIGHT (f) = 0;
3576 /* The height has changed, resize outer widget and set columns
3577 rows to what we had before removing the tool bar. */
3578 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3580 SET_FRAME_GARBAGED (f);
3581 UNBLOCK_INPUT;
3587 /***********************************************************************
3588 Initializing
3589 ***********************************************************************/
3590 void
3591 xg_initialize ()
3593 GtkBindingSet *binding_set;
3595 xg_ignore_gtk_scrollbar = 0;
3596 xg_detached_menus = 0;
3597 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3598 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3600 id_to_widget.max_size = id_to_widget.used = 0;
3601 id_to_widget.widgets = 0;
3603 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3604 bindings. It doesn't seem to be any way to remove properties,
3605 so we set it to VoidSymbol which in X means "no key". */
3606 gtk_settings_set_string_property (gtk_settings_get_default (),
3607 "gtk-menu-bar-accel",
3608 "VoidSymbol",
3609 EMACS_CLASS);
3611 /* Make GTK text input widgets use Emacs style keybindings. This is
3612 Emacs after all. */
3613 gtk_settings_set_string_property (gtk_settings_get_default (),
3614 "gtk-key-theme-name",
3615 "Emacs",
3616 EMACS_CLASS);
3618 /* Make dialogs close on C-g. Since file dialog inherits from
3619 dialog, this works for them also. */
3620 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
3621 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3622 "close", 0);
3624 /* Make menus close on C-g. */
3625 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
3626 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3627 "cancel", 0);
3630 #endif /* USE_GTK */
3632 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3633 (do not change this comment) */