(cperl-switch-to-doc-buffer): Don't use interactive-p.
[emacs.git] / src / gtkutil.c
blobdc091c1a09b9595221ede72bb39b673c77e003a6
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 <stdio.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "window.h"
31 #include "atimer.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
40 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
41 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
44 /***********************************************************************
45 Display handling functions
46 ***********************************************************************/
48 #ifdef HAVE_GTK_MULTIDISPLAY
50 /* Return the GdkDisplay that corresponds to the X display DPY. */
52 static GdkDisplay *
53 xg_get_gdk_display (dpy)
54 Display *dpy;
56 return gdk_x11_lookup_xdisplay (dpy);
59 /* When the GTK widget W is to be created on a display for F that
60 is not the default display, set the display for W.
61 W can be a GtkMenu or a GtkWindow widget. */
63 static void
64 xg_set_screen (w, f)
65 GtkWidget *w;
66 FRAME_PTR f;
68 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
70 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
71 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
73 if (GTK_IS_MENU (w))
74 gtk_menu_set_screen (GTK_MENU (w), gscreen);
75 else
76 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
81 #else /* not HAVE_GTK_MULTIDISPLAY */
83 /* Make some defines so we can use the GTK 2.2 functions when
84 compiling with GTK 2.0. */
86 #define xg_set_screen(w, f)
87 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
88 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
89 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
90 #define gdk_x11_lookup_xdisplay(dpy) 0
91 #define GdkDisplay void
93 #endif /* not HAVE_GTK_MULTIDISPLAY */
95 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
96 *DPY is set to NULL if the display can't be opened.
98 Returns non-zero if display could be opened, zero if display could not
99 be opened, and less than zero if the GTK version doesn't support
100 multipe displays. */
103 xg_display_open (display_name, dpy)
104 char *display_name;
105 Display **dpy;
107 #ifdef HAVE_GTK_MULTIDISPLAY
108 GdkDisplay *gdpy;
110 gdpy = gdk_display_open (display_name);
111 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
113 return gdpy != NULL;
115 #else /* not HAVE_GTK_MULTIDISPLAY */
117 return -1;
118 #endif /* not HAVE_GTK_MULTIDISPLAY */
122 /* Close display DPY. */
124 void
125 xg_display_close (Display *dpy)
127 #ifdef HAVE_GTK_MULTIDISPLAY
128 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
130 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
131 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
132 we can continue running, but there will be memory leaks. */
134 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
136 /* If this is the default display, we must change it before calling
137 dispose, otherwise it will crash. */
138 if (gdk_display_get_default () == gdpy)
140 struct x_display_info *dpyinfo;
141 Display *new_dpy = 0;
142 GdkDisplay *gdpy_new;
144 /* Find another display. */
145 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
146 if (dpyinfo->display != dpy)
148 new_dpy = dpyinfo->display;
149 break;
152 if (! new_dpy) return; /* Emacs will exit anyway. */
154 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
155 gdk_display_manager_set_default_display (gdk_display_manager_get (),
156 gdpy_new);
159 g_object_run_dispose (G_OBJECT (gdpy));
161 #else
162 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
163 gdk_display_close (gdpy);
164 #endif
165 #endif /* HAVE_GTK_MULTIDISPLAY */
169 /***********************************************************************
170 Utility functions
171 ***********************************************************************/
172 /* The timer for scroll bar repetition and menu bar timeouts.
173 NULL if no timer is started. */
174 static struct atimer *xg_timer;
177 /* The next two variables and functions are taken from lwlib. */
178 static widget_value *widget_value_free_list;
179 static int malloc_cpt;
181 /* Allocate a widget_value structure, either by taking one from the
182 widget_value_free_list or by malloc:ing a new one.
184 Return a pointer to the allocated structure. */
186 widget_value *
187 malloc_widget_value ()
189 widget_value *wv;
190 if (widget_value_free_list)
192 wv = widget_value_free_list;
193 widget_value_free_list = wv->free_list;
194 wv->free_list = 0;
196 else
198 wv = (widget_value *) malloc (sizeof (widget_value));
199 malloc_cpt++;
201 memset (wv, 0, sizeof (widget_value));
202 return wv;
205 /* This is analogous to free. It frees only what was allocated
206 by malloc_widget_value, and no substructures. */
208 void
209 free_widget_value (wv)
210 widget_value *wv;
212 if (wv->free_list)
213 abort ();
215 if (malloc_cpt > 25)
217 /* When the number of already allocated cells is too big,
218 We free it. */
219 free (wv);
220 malloc_cpt--;
222 else
224 wv->free_list = widget_value_free_list;
225 widget_value_free_list = wv;
230 /* Create and return the cursor to be used for popup menus and
231 scroll bars on display DPY. */
233 GdkCursor *
234 xg_create_default_cursor (dpy)
235 Display *dpy;
237 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
238 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
241 /* For the image defined in IMG, make and return a GtkImage. For displays with
242 8 planes or less we must make a GdkPixbuf and apply the mask manually.
243 Otherwise the highlightning and dimming the tool bar code in GTK does
244 will look bad. For display with more than 8 planes we just use the
245 pixmap and mask directly. For monochrome displays, GTK doesn't seem
246 able to use external pixmaps, it looks bad whatever we do.
247 The image is defined on the display where frame F is.
248 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
249 If OLD_WIDGET is NULL, a new widget is constructed and returned.
250 If OLD_WIDGET is not NULL, that widget is modified. */
252 static GtkWidget *
253 xg_get_image_for_pixmap (f, img, widget, old_widget)
254 FRAME_PTR f;
255 struct image *img;
256 GtkWidget *widget;
257 GtkImage *old_widget;
259 GdkPixmap *gpix;
260 GdkPixmap *gmask;
261 GdkDisplay *gdpy;
263 /* If we are on a one bit display, let GTK do all the image handling.
264 This seems to be the only way to make insensitive and activated icons
265 look good. */
266 if (x_screen_planes (f) == 1)
268 Lisp_Object specified_file = Qnil;
269 Lisp_Object tail;
270 extern Lisp_Object QCfile;
272 for (tail = XCDR (img->spec);
273 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
274 tail = XCDR (XCDR (tail)))
275 if (EQ (XCAR (tail), QCfile))
276 specified_file = XCAR (XCDR (tail));
278 if (STRINGP (specified_file))
281 Lisp_Object file = Qnil;
282 struct gcpro gcpro1;
283 GCPRO1 (file);
285 file = x_find_image_file (specified_file);
286 /* We already loaded the image once before calling this
287 function, so this should not fail. */
288 xassert (STRINGP (file) != 0);
290 if (! old_widget)
291 old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
292 else
293 gtk_image_set_from_file (old_widget, SDATA (file));
295 UNGCPRO;
296 return GTK_WIDGET (old_widget);
300 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
301 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
302 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
304 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
306 if (! old_widget)
307 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
308 else
309 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
311 else
313 /* This is a workaround to make icons look good on pseudo color
314 displays. Apparently GTK expects the images to have an alpha
315 channel. If they don't, insensitive and activated icons will
316 look bad. This workaround does not work on monochrome displays,
317 and is not needed on true color/static color displays (i.e.
318 16 bits and higher). */
319 int x, y, width, height, rowstride, mask_rowstride;
320 GdkPixbuf *icon_buf, *tmp_buf;
321 guchar *pixels;
322 guchar *mask_pixels;
324 gdk_drawable_get_size (gpix, &width, &height);
325 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
326 gpix,
327 gtk_widget_get_colormap (widget),
328 0, 0, 0, 0, width, height);
329 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
330 g_object_unref (G_OBJECT (tmp_buf));
332 if (gmask)
334 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
335 gmask,
336 NULL,
337 0, 0, 0, 0,
338 width, height);
339 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
340 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
341 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
342 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
343 int y;
345 for (y = 0; y < height; ++y)
347 guchar *iconptr, *maskptr;
348 int x;
350 iconptr = pixels + y * rowstride;
351 maskptr = mask_pixels + y * mask_rowstride;
353 for (x = 0; x < width; ++x)
355 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
356 just R is sufficient. */
357 if (maskptr[0] == 0)
358 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
360 iconptr += rowstride/width;
361 maskptr += mask_rowstride/width;
365 g_object_unref (G_OBJECT (mask_buf));
368 if (! old_widget)
369 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
370 else
371 gtk_image_set_from_pixbuf (old_widget, icon_buf);
373 g_object_unref (G_OBJECT (icon_buf));
376 g_object_unref (G_OBJECT (gpix));
377 if (gmask) g_object_unref (G_OBJECT (gmask));
379 return GTK_WIDGET (old_widget);
383 /* Set CURSOR on W and all widgets W contain. We must do like this
384 for scroll bars and menu because they create widgets internally,
385 and it is those widgets that are visible. */
387 static void
388 xg_set_cursor (w, cursor)
389 GtkWidget *w;
390 GdkCursor *cursor;
392 GList *children = gdk_window_peek_children (w->window);
394 gdk_window_set_cursor (w->window, cursor);
396 /* The scroll bar widget has more than one GDK window (had to look at
397 the source to figure this out), and there is no way to set cursor
398 on widgets in GTK. So we must set the cursor for all GDK windows.
399 Ditto for menus. */
401 for ( ; children; children = g_list_next (children))
402 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
405 /* Timer function called when a timeout occurs for xg_timer.
406 This function processes all GTK events in a recursive event loop.
407 This is done because GTK timer events are not seen by Emacs event
408 detection, Emacs only looks for X events. When a scroll bar has the
409 pointer (detected by button press/release events below) an Emacs
410 timer is started, and this function can then check if the GTK timer
411 has expired by calling the GTK event loop.
412 Also, when a menu is active, it has a small timeout before it
413 pops down the sub menu under it. */
415 static void
416 xg_process_timeouts (timer)
417 struct atimer *timer;
419 BLOCK_INPUT;
420 /* Ideally we would like to just handle timer events, like the Xt version
421 of this does in xterm.c, but there is no such feature in GTK. */
422 while (gtk_events_pending ())
423 gtk_main_iteration ();
424 UNBLOCK_INPUT;
427 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
428 xg_process_timeouts is called when the timer expires. The timer
429 started is continuous, i.e. runs until xg_stop_timer is called. */
431 static void
432 xg_start_timer ()
434 if (! xg_timer)
436 EMACS_TIME interval;
437 EMACS_SET_SECS_USECS (interval, 0, 100000);
438 xg_timer = start_atimer (ATIMER_CONTINUOUS,
439 interval,
440 xg_process_timeouts,
445 /* Stop the xg_timer if started. */
447 static void
448 xg_stop_timer ()
450 if (xg_timer)
452 cancel_atimer (xg_timer);
453 xg_timer = 0;
457 /* Insert NODE into linked LIST. */
459 static void
460 xg_list_insert (xg_list_node *list, xg_list_node *node)
462 xg_list_node *list_start = list->next;
464 if (list_start) list_start->prev = node;
465 node->next = list_start;
466 node->prev = 0;
467 list->next = node;
470 /* Remove NODE from linked LIST. */
472 static void
473 xg_list_remove (xg_list_node *list, xg_list_node *node)
475 xg_list_node *list_start = list->next;
476 if (node == list_start)
478 list->next = node->next;
479 if (list->next) list->next->prev = 0;
481 else
483 node->prev->next = node->next;
484 if (node->next) node->next->prev = node->prev;
488 /* Allocate and return a utf8 version of STR. If STR is already
489 utf8 or NULL, just return STR.
490 If not, a new string is allocated and the caller must free the result
491 with g_free. */
493 static char *
494 get_utf8_string (str)
495 char *str;
497 char *utf8_str = str;
499 /* If not UTF-8, try current locale. */
500 if (str && !g_utf8_validate (str, -1, NULL))
501 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
503 return utf8_str;
508 /***********************************************************************
509 General functions for creating widgets, resizing, events, e.t.c.
510 ***********************************************************************/
512 /* Make a geometry string and pass that to GTK. It seems this is the
513 only way to get geometry position right if the user explicitly
514 asked for a position when starting Emacs.
515 F is the frame we shall set geometry for. */
517 static void
518 xg_set_geometry (f)
519 FRAME_PTR f;
521 if (f->size_hint_flags & USPosition)
523 int left = f->left_pos;
524 int xneg = f->size_hint_flags & XNegative;
525 int top = f->top_pos;
526 int yneg = f->size_hint_flags & YNegative;
527 char geom_str[32];
529 if (xneg)
530 left = -left;
531 if (yneg)
532 top = -top;
534 sprintf (geom_str, "=%dx%d%c%d%c%d",
535 FRAME_PIXEL_WIDTH (f),
536 FRAME_TOTAL_PIXEL_HEIGHT (f),
537 (xneg ? '-' : '+'), left,
538 (yneg ? '-' : '+'), top);
540 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
541 geom_str))
542 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
547 /* Resize the outer window of frame F after chainging the height.
548 This happend when the menu bar or the tool bar is added or removed.
549 COLUMNS/ROWS is the size the edit area shall have after the resize. */
551 static void
552 xg_resize_outer_widget (f, columns, rows)
553 FRAME_PTR f;
554 int columns;
555 int rows;
557 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
558 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
560 /* base_height is now changed. */
561 x_wm_set_size_hint (f, 0, 0);
563 /* If we are not mapped yet, set geometry once again, as window
564 height now have changed. */
565 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
566 xg_set_geometry (f);
568 xg_frame_set_char_size (f, columns, rows);
569 gdk_window_process_all_updates ();
572 /* Function to handle resize of our widgets. Since Emacs has some layouts
573 that does not fit well with GTK standard containers, we do most layout
574 manually.
575 F is the frame to resize.
576 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
578 void
579 xg_resize_widgets (f, pixelwidth, pixelheight)
580 FRAME_PTR f;
581 int pixelwidth, pixelheight;
583 int mbheight = FRAME_MENUBAR_HEIGHT (f);
584 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
585 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
586 - mbheight - tbheight));
587 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
589 if (FRAME_GTK_WIDGET (f)
590 && (columns != FRAME_COLS (f)
591 || rows != FRAME_LINES (f)
592 || pixelwidth != FRAME_PIXEL_WIDTH (f)
593 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
595 struct x_output *x = f->output_data.x;
596 GtkAllocation all;
598 all.y = mbheight + tbheight;
599 all.x = 0;
601 all.width = pixelwidth;
602 all.height = pixelheight - mbheight - tbheight;
604 gtk_widget_size_allocate (x->edit_widget, &all);
606 change_frame_size (f, rows, columns, 0, 1, 0);
607 SET_FRAME_GARBAGED (f);
608 cancel_mouse_face (f);
613 /* Update our widget size to be COLS/ROWS characters for frame F. */
615 void
616 xg_frame_set_char_size (f, cols, rows)
617 FRAME_PTR f;
618 int cols;
619 int rows;
621 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
622 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
623 int pixelwidth;
625 /* Take into account the size of the scroll bar. Always use the
626 number of columns occupied by the scroll bar here otherwise we
627 might end up with a frame width that is not a multiple of the
628 frame's character width which is bad for vertically split
629 windows. */
630 f->scroll_bar_actual_width
631 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
633 compute_fringe_widths (f, 0);
635 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
636 after calculating that value. */
637 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
639 /* Must resize our top level widget. Font size may have changed,
640 but not rows/cols. */
641 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
642 pixelwidth, pixelheight);
643 xg_resize_widgets (f, pixelwidth, pixelheight);
644 x_wm_set_size_hint (f, 0, 0);
645 SET_FRAME_GARBAGED (f);
646 cancel_mouse_face (f);
649 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
650 Must be done like this, because GtkWidget:s can have "hidden"
651 X Window that aren't accessible.
653 Return 0 if no widget match WDESC. */
655 GtkWidget *
656 xg_win_to_widget (dpy, wdesc)
657 Display *dpy;
658 Window wdesc;
660 gpointer gdkwin;
661 GtkWidget *gwdesc = 0;
663 BLOCK_INPUT;
665 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
666 wdesc);
667 if (gdkwin)
669 GdkEvent event;
670 event.any.window = gdkwin;
671 gwdesc = gtk_get_event_widget (&event);
674 UNBLOCK_INPUT;
675 return gwdesc;
678 /* Fill in the GdkColor C so that it represents PIXEL.
679 W is the widget that color will be used for. Used to find colormap. */
681 static void
682 xg_pix_to_gcolor (w, pixel, c)
683 GtkWidget *w;
684 unsigned long pixel;
685 GdkColor *c;
687 GdkColormap *map = gtk_widget_get_colormap (w);
688 gdk_colormap_query_color (map, pixel, c);
691 /* Create and set up the GTK widgets for frame F.
692 Return 0 if creation failed, non-zero otherwise. */
695 xg_create_frame_widgets (f)
696 FRAME_PTR f;
698 GtkWidget *wtop;
699 GtkWidget *wvbox;
700 GtkWidget *wfixed;
701 GdkColor bg;
702 GtkRcStyle *style;
703 int i;
704 char *title = 0;
706 BLOCK_INPUT;
708 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
709 xg_set_screen (wtop, f);
711 wvbox = gtk_vbox_new (FALSE, 0);
712 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
714 if (! wtop || ! wvbox || ! wfixed)
716 if (wtop) gtk_widget_destroy (wtop);
717 if (wvbox) gtk_widget_destroy (wvbox);
718 if (wfixed) gtk_widget_destroy (wfixed);
720 return 0;
723 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
724 gtk_widget_set_name (wtop, EMACS_CLASS);
725 gtk_widget_set_name (wvbox, "pane");
726 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
728 /* If this frame has a title or name, set it in the title bar. */
729 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
730 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
732 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
734 FRAME_GTK_OUTER_WIDGET (f) = wtop;
735 FRAME_GTK_WIDGET (f) = wfixed;
736 f->output_data.x->vbox_widget = wvbox;
738 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
740 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
741 FRAME_PIXEL_HEIGHT (f));
743 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
744 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
746 if (FRAME_EXTERNAL_TOOL_BAR (f))
747 update_frame_tool_bar (f);
749 /* The tool bar is created but first there are no items in it.
750 This causes it to be zero height. Later items are added, but then
751 the frame is already mapped, so there is a "jumping" resize.
752 This makes geometry handling difficult, for example -0-0 will end
753 up in the wrong place as tool bar height has not been taken into account.
754 So we cheat a bit by setting a height that is what it will have
755 later on when tool bar items are added. */
756 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
757 FRAME_TOOLBAR_HEIGHT (f) = 34;
760 /* We don't want this widget double buffered, because we draw on it
761 with regular X drawing primitives, so from a GTK/GDK point of
762 view, the widget is totally blank. When an expose comes, this
763 will make the widget blank, and then Emacs redraws it. This flickers
764 a lot, so we turn off double buffering. */
765 gtk_widget_set_double_buffered (wfixed, FALSE);
767 /* GTK documents says use gtk_window_set_resizable. But then a user
768 can't shrink the window from its starting size. */
769 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
770 gtk_window_set_wmclass (GTK_WINDOW (wtop),
771 SDATA (Vx_resource_name),
772 SDATA (Vx_resource_class));
774 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
775 GTK is to destroy the widget. We want Emacs to do that instead. */
776 g_signal_connect (G_OBJECT (wtop), "delete-event",
777 G_CALLBACK (gtk_true), 0);
779 /* Convert our geometry parameters into a geometry string
780 and specify it.
781 GTK will itself handle calculating the real position this way. */
782 xg_set_geometry (f);
784 gtk_widget_add_events (wfixed,
785 GDK_POINTER_MOTION_MASK
786 | GDK_EXPOSURE_MASK
787 | GDK_BUTTON_PRESS_MASK
788 | GDK_BUTTON_RELEASE_MASK
789 | GDK_KEY_PRESS_MASK
790 | GDK_ENTER_NOTIFY_MASK
791 | GDK_LEAVE_NOTIFY_MASK
792 | GDK_FOCUS_CHANGE_MASK
793 | GDK_STRUCTURE_MASK
794 | GDK_VISIBILITY_NOTIFY_MASK);
796 /* Must realize the windows so the X window gets created. It is used
797 by callers of this function. */
798 gtk_widget_realize (wfixed);
799 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
801 /* Since GTK clears its window by filling with the background color,
802 we must keep X and GTK background in sync. */
803 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
804 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
806 /* Also, do not let any background pixmap to be set, this looks very
807 bad as Emacs overwrites the background pixmap with its own idea
808 of background color. */
809 style = gtk_widget_get_modifier_style (wfixed);
811 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
812 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
813 gtk_widget_modify_style (wfixed, style);
815 /* GTK does not set any border, and they look bad with GTK. */
816 f->border_width = 0;
817 f->internal_border_width = 0;
819 UNBLOCK_INPUT;
821 return 1;
824 /* Set the normal size hints for the window manager, for frame F.
825 FLAGS is the flags word to use--or 0 meaning preserve the flags
826 that the window now has.
827 If USER_POSITION is nonzero, we set the User Position
828 flag (this is useful when FLAGS is 0). */
830 void
831 x_wm_set_size_hint (f, flags, user_position)
832 FRAME_PTR f;
833 long flags;
834 int user_position;
836 if (FRAME_GTK_OUTER_WIDGET (f))
838 /* Must use GTK routines here, otherwise GTK resets the size hints
839 to its own defaults. */
840 GdkGeometry size_hints;
841 gint hint_flags = 0;
842 int base_width, base_height;
843 int min_rows = 0, min_cols = 0;
844 int win_gravity = f->win_gravity;
846 if (flags)
848 memset (&size_hints, 0, sizeof (size_hints));
849 f->output_data.x->size_hints = size_hints;
850 f->output_data.x->hint_flags = hint_flags;
852 else
853 flags = f->size_hint_flags;
855 size_hints = f->output_data.x->size_hints;
856 hint_flags = f->output_data.x->hint_flags;
858 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
859 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
860 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
862 hint_flags |= GDK_HINT_BASE_SIZE;
863 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
864 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
865 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
867 check_frame_size (f, &min_rows, &min_cols);
869 size_hints.base_width = base_width;
870 size_hints.base_height = base_height;
871 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
872 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
875 /* These currently have a one to one mapping with the X values, but I
876 don't think we should rely on that. */
877 hint_flags |= GDK_HINT_WIN_GRAVITY;
878 size_hints.win_gravity = 0;
879 if (win_gravity == NorthWestGravity)
880 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
881 else if (win_gravity == NorthGravity)
882 size_hints.win_gravity = GDK_GRAVITY_NORTH;
883 else if (win_gravity == NorthEastGravity)
884 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
885 else if (win_gravity == WestGravity)
886 size_hints.win_gravity = GDK_GRAVITY_WEST;
887 else if (win_gravity == CenterGravity)
888 size_hints.win_gravity = GDK_GRAVITY_CENTER;
889 else if (win_gravity == EastGravity)
890 size_hints.win_gravity = GDK_GRAVITY_EAST;
891 else if (win_gravity == SouthWestGravity)
892 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
893 else if (win_gravity == SouthGravity)
894 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
895 else if (win_gravity == SouthEastGravity)
896 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
897 else if (win_gravity == StaticGravity)
898 size_hints.win_gravity = GDK_GRAVITY_STATIC;
900 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
901 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
902 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
904 if (user_position)
906 hint_flags &= ~GDK_HINT_POS;
907 hint_flags |= GDK_HINT_USER_POS;
910 BLOCK_INPUT;
912 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
913 FRAME_GTK_OUTER_WIDGET (f),
914 &size_hints,
915 hint_flags);
917 f->output_data.x->size_hints = size_hints;
918 f->output_data.x->hint_flags = hint_flags;
919 UNBLOCK_INPUT;
923 /* Change background color of a frame.
924 Since GTK uses the background colour to clear the window, we must
925 keep the GTK and X colors in sync.
926 F is the frame to change,
927 BG is the pixel value to change to. */
929 void
930 xg_set_background_color (f, bg)
931 FRAME_PTR f;
932 unsigned long bg;
934 if (FRAME_GTK_WIDGET (f))
936 GdkColor gdk_bg;
938 BLOCK_INPUT;
939 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
940 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
941 UNBLOCK_INPUT;
947 /***********************************************************************
948 Dialog functions
949 ***********************************************************************/
950 /* Return the dialog title to use for a dialog of type KEY.
951 This is the encoding used by lwlib. We use the same for GTK. */
953 static char *
954 get_dialog_title (char key)
956 char *title = "";
958 switch (key) {
959 case 'E': case 'e':
960 title = "Error";
961 break;
963 case 'I': case 'i':
964 title = "Information";
965 break;
967 case 'L': case 'l':
968 title = "Prompt";
969 break;
971 case 'P': case 'p':
972 title = "Prompt";
973 break;
975 case 'Q': case 'q':
976 title = "Question";
977 break;
980 return title;
983 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
984 the dialog, but return TRUE so the event does not propagate further
985 in GTK. This prevents GTK from destroying the dialog widget automatically
986 and we can always destrou the widget manually, regardles of how
987 it was popped down (button press or WM_DELETE_WINDOW).
988 W is the dialog widget.
989 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
990 user_data is NULL (not used).
992 Returns TRUE to end propagation of event. */
994 static gboolean
995 dialog_delete_callback (w, event, user_data)
996 GtkWidget *w;
997 GdkEvent *event;
998 gpointer user_data;
1000 gtk_widget_unmap (w);
1001 return TRUE;
1004 /* Create a popup dialog window. See also xg_create_widget below.
1005 WV is a widget_value describing the dialog.
1006 SELECT_CB is the callback to use when a button has been pressed.
1007 DEACTIVATE_CB is the callback to use when the dialog pops down.
1009 Returns the GTK dialog widget. */
1011 static GtkWidget *
1012 create_dialog (wv, select_cb, deactivate_cb)
1013 widget_value *wv;
1014 GCallback select_cb;
1015 GCallback deactivate_cb;
1017 char *title = get_dialog_title (wv->name[0]);
1018 int total_buttons = wv->name[1] - '0';
1019 int right_buttons = wv->name[4] - '0';
1020 int left_buttons;
1021 int button_nr = 0;
1022 int button_spacing = 10;
1023 GtkWidget *wdialog = gtk_dialog_new ();
1024 widget_value *item;
1025 GtkBox *cur_box;
1026 GtkWidget *wvbox;
1027 GtkWidget *whbox_up;
1028 GtkWidget *whbox_down;
1030 /* If the number of buttons is greater than 4, make two rows of buttons
1031 instead. This looks better. */
1032 int make_two_rows = total_buttons > 4;
1034 if (right_buttons == 0) right_buttons = total_buttons/2;
1035 left_buttons = total_buttons - right_buttons;
1037 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1038 gtk_widget_set_name (wdialog, "emacs-dialog");
1040 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1042 if (make_two_rows)
1044 wvbox = gtk_vbox_new (TRUE, button_spacing);
1045 whbox_up = gtk_hbox_new (FALSE, 0);
1046 whbox_down = gtk_hbox_new (FALSE, 0);
1048 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1049 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1050 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1052 cur_box = GTK_BOX (whbox_up);
1055 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1056 G_CALLBACK (dialog_delete_callback), 0);
1058 if (deactivate_cb)
1060 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1061 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1064 for (item = wv->contents; item; item = item->next)
1066 char *utf8_label = get_utf8_string (item->value);
1067 GtkWidget *w;
1068 GtkRequisition req;
1070 if (item->name && strcmp (item->name, "message") == 0)
1072 /* This is the text part of the dialog. */
1073 w = gtk_label_new (utf8_label);
1074 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1075 gtk_label_new (""),
1076 FALSE, FALSE, 0);
1077 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1078 TRUE, TRUE, 0);
1079 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1081 /* Try to make dialog look better. Must realize first so
1082 the widget can calculate the size it needs. */
1083 gtk_widget_realize (w);
1084 gtk_widget_size_request (w, &req);
1085 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1086 req.height);
1087 if (item->value && strlen (item->value) > 0)
1088 button_spacing = 2*req.width/strlen (item->value);
1090 else
1092 /* This is one button to add to the dialog. */
1093 w = gtk_button_new_with_label (utf8_label);
1094 if (! item->enabled)
1095 gtk_widget_set_sensitive (w, FALSE);
1096 if (select_cb)
1097 g_signal_connect (G_OBJECT (w), "clicked",
1098 select_cb, item->call_data);
1100 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1101 if (++button_nr == left_buttons)
1103 if (make_two_rows)
1104 cur_box = GTK_BOX (whbox_down);
1105 else
1106 gtk_box_pack_start (cur_box,
1107 gtk_label_new (""),
1108 TRUE, TRUE,
1109 button_spacing);
1113 if (utf8_label && utf8_label != item->value)
1114 g_free (utf8_label);
1117 return wdialog;
1121 enum
1123 XG_FILE_NOT_DONE,
1124 XG_FILE_OK,
1125 XG_FILE_CANCEL,
1126 XG_FILE_DESTROYED,
1129 /* Callback function invoked when the Ok button is pressed in
1130 a file dialog.
1131 W is the file dialog widget,
1132 ARG points to an integer where we record what has happend. */
1134 static void
1135 xg_file_sel_ok (w, arg)
1136 GtkWidget *w;
1137 gpointer arg;
1139 *(int*)arg = XG_FILE_OK;
1142 /* Callback function invoked when the Cancel button is pressed in
1143 a file dialog.
1144 W is the file dialog widget,
1145 ARG points to an integer where we record what has happend. */
1147 static void
1148 xg_file_sel_cancel (w, arg)
1149 GtkWidget *w;
1150 gpointer arg;
1152 *(int*)arg = XG_FILE_CANCEL;
1155 /* Callback function invoked when the file dialog is destroyed (i.e.
1156 popped down). We must keep track of this, because if this
1157 happens, GTK destroys the widget. But if for example, Ok is pressed,
1158 the dialog is popped down, but the dialog widget is not destroyed.
1159 W is the file dialog widget,
1160 ARG points to an integer where we record what has happend. */
1162 static void
1163 xg_file_sel_destroy (w, arg)
1164 GtkWidget *w;
1165 gpointer arg;
1167 *(int*)arg = XG_FILE_DESTROYED;
1170 /* Read a file name from the user using a file dialog.
1171 F is the current frame.
1172 PROMPT is a prompt to show to the user. May not be NULL.
1173 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1174 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1175 file.
1177 Returns a file name or NULL if no file was selected.
1178 The returned string must be freed by the caller. */
1180 char *
1181 xg_get_file_name (f, prompt, default_filename, mustmatch_p)
1182 FRAME_PTR f;
1183 char *prompt;
1184 char *default_filename;
1185 int mustmatch_p;
1187 GtkWidget *filewin;
1188 GtkFileSelection *filesel;
1189 int filesel_done = XG_FILE_NOT_DONE;
1190 char *fn = 0;
1192 filewin = gtk_file_selection_new (prompt);
1193 filesel = GTK_FILE_SELECTION (filewin);
1195 xg_set_screen (filewin, f);
1197 gtk_widget_set_name (filewin, "emacs-filedialog");
1199 gtk_window_set_transient_for (GTK_WINDOW (filewin),
1200 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1201 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
1203 g_signal_connect (G_OBJECT (filesel->ok_button),
1204 "clicked",
1205 G_CALLBACK (xg_file_sel_ok),
1206 &filesel_done);
1207 g_signal_connect (G_OBJECT (filesel->cancel_button),
1208 "clicked",
1209 G_CALLBACK (xg_file_sel_cancel),
1210 &filesel_done);
1211 g_signal_connect (G_OBJECT (filesel),
1212 "destroy",
1213 G_CALLBACK (xg_file_sel_destroy),
1214 &filesel_done);
1216 if (default_filename)
1217 gtk_file_selection_set_filename (filesel, default_filename);
1219 if (mustmatch_p)
1221 /* The selection_entry part of filesel is not documented. */
1222 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1223 gtk_file_selection_hide_fileop_buttons (filesel);
1227 gtk_widget_show_all (filewin);
1229 while (filesel_done == XG_FILE_NOT_DONE)
1230 gtk_main_iteration ();
1232 if (filesel_done == XG_FILE_OK)
1233 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1235 if (filesel_done != XG_FILE_DESTROYED)
1236 gtk_widget_destroy (filewin);
1238 return fn;
1242 /***********************************************************************
1243 Menu functions.
1244 ***********************************************************************/
1246 /* The name of menu items that can be used for citomization. Since GTK
1247 RC files are very crude and primitive, we have to set this on all
1248 menu item names so a user can easily cutomize menu items. */
1250 #define MENU_ITEM_NAME "emacs-menuitem"
1253 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1254 during GC. The next member points to the items. */
1255 static xg_list_node xg_menu_cb_list;
1257 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1258 during GC. The next member points to the items. */
1259 static xg_list_node xg_menu_item_cb_list;
1261 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1262 F is the frame CL_DATA will be initialized for.
1263 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1265 The menu bar and all sub menus under the menu bar in a frame
1266 share the same structure, hence the reference count.
1268 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1269 allocated xg_menu_cb_data if CL_DATA is NULL. */
1271 static xg_menu_cb_data *
1272 make_cl_data (cl_data, f, highlight_cb)
1273 xg_menu_cb_data *cl_data;
1274 FRAME_PTR f;
1275 GCallback highlight_cb;
1277 if (! cl_data)
1279 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1280 cl_data->f = f;
1281 cl_data->menu_bar_vector = f->menu_bar_vector;
1282 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1283 cl_data->highlight_cb = highlight_cb;
1284 cl_data->ref_count = 0;
1286 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1289 cl_data->ref_count++;
1291 return cl_data;
1294 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1295 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1297 When the menu bar is updated, menu items may have been added and/or
1298 removed, so menu_bar_vector and menu_bar_items_used change. We must
1299 then update CL_DATA since it is used to determine which menu
1300 item that is invoked in the menu.
1301 HIGHLIGHT_CB could change, there is no check that the same
1302 function is given when modifying a menu bar as was given when
1303 creating the menu bar. */
1305 static void
1306 update_cl_data (cl_data, f, highlight_cb)
1307 xg_menu_cb_data *cl_data;
1308 FRAME_PTR f;
1309 GCallback highlight_cb;
1311 if (cl_data)
1313 cl_data->f = f;
1314 cl_data->menu_bar_vector = f->menu_bar_vector;
1315 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1316 cl_data->highlight_cb = highlight_cb;
1320 /* Decrease reference count for CL_DATA.
1321 If reference count is zero, free CL_DATA. */
1323 static void
1324 unref_cl_data (cl_data)
1325 xg_menu_cb_data *cl_data;
1327 if (cl_data && cl_data->ref_count > 0)
1329 cl_data->ref_count--;
1330 if (cl_data->ref_count == 0)
1332 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1333 xfree (cl_data);
1338 /* Function that marks all lisp data during GC. */
1340 void
1341 xg_mark_data ()
1343 xg_list_node *iter;
1345 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1346 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1348 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1350 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1352 if (! NILP (cb_data->help))
1353 mark_object (cb_data->help);
1358 /* Callback called when a menu item is destroyed. Used to free data.
1359 W is the widget that is being destroyed (not used).
1360 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1362 static void
1363 menuitem_destroy_callback (w, client_data)
1364 GtkWidget *w;
1365 gpointer client_data;
1367 if (client_data)
1369 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1370 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1371 xfree (data);
1375 /* Callback called when the pointer enters/leaves a menu item.
1376 W is the menu item.
1377 EVENT is either an enter event or leave event.
1378 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1380 Returns FALSE to tell GTK to keep processing this event. */
1382 static gboolean
1383 menuitem_highlight_callback (w, event, client_data)
1384 GtkWidget *w;
1385 GdkEventCrossing *event;
1386 gpointer client_data;
1388 if (client_data)
1390 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1391 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1393 if (! NILP (data->help) && data->cl_data->highlight_cb)
1395 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1396 (*func) (w, call_data);
1400 return FALSE;
1403 /* Callback called when a menu is destroyed. Used to free data.
1404 W is the widget that is being destroyed (not used).
1405 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1407 static void
1408 menu_destroy_callback (w, client_data)
1409 GtkWidget *w;
1410 gpointer client_data;
1412 unref_cl_data ((xg_menu_cb_data*) client_data);
1415 /* Callback called when a menu does a grab or ungrab. That means the
1416 menu has been activated or deactivated.
1417 Used to start a timer so the small timeout the menus in GTK uses before
1418 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1419 W is the widget that does the grab (not used).
1420 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1421 CLIENT_DATA is NULL (not used). */
1423 static void
1424 menu_grab_callback (GtkWidget *widget,
1425 gboolean ungrab_p,
1426 gpointer client_data)
1428 /* Keep track of total number of grabs. */
1429 static int cnt;
1431 if (ungrab_p) cnt--;
1432 else cnt++;
1434 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1435 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1438 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1439 must be non-NULL) and can be inserted into a menu item.
1441 Returns the GtkHBox. */
1443 static GtkWidget *
1444 make_widget_for_menu_item (utf8_label, utf8_key)
1445 char *utf8_label;
1446 char *utf8_key;
1448 GtkWidget *wlbl;
1449 GtkWidget *wkey;
1450 GtkWidget *wbox;
1452 wbox = gtk_hbox_new (FALSE, 0);
1453 wlbl = gtk_label_new (utf8_label);
1454 wkey = gtk_label_new (utf8_key);
1456 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1457 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1459 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1460 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1462 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1463 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1464 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1466 return wbox;
1469 /* Make and return a menu item widget with the key to the right.
1470 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1471 UTF8_KEY is the text representing the key binding.
1472 ITEM is the widget_value describing the menu item.
1474 GROUP is an in/out parameter. If the menu item to be created is not
1475 part of any radio menu group, *GROUP contains NULL on entry and exit.
1476 If the menu item to be created is part of a radio menu group, on entry
1477 *GROUP contains the group to use, or NULL if this is the first item
1478 in the group. On exit, *GROUP contains the radio item group.
1480 Unfortunately, keys don't line up as nicely as in Motif,
1481 but the MacOS X version doesn't either, so I guess that is OK. */
1483 static GtkWidget *
1484 make_menu_item (utf8_label, utf8_key, item, group)
1485 char *utf8_label;
1486 char *utf8_key;
1487 widget_value *item;
1488 GSList **group;
1490 GtkWidget *w;
1491 GtkWidget *wtoadd = 0;
1493 /* It has been observed that some menu items have a NULL name field.
1494 This will lead to this function being called with a NULL utf8_label.
1495 GTK crashes on that so we set a blank label. Why there is a NULL
1496 name remains to be investigated. */
1497 if (! utf8_label) utf8_label = " ";
1499 if (utf8_key)
1500 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1502 if (item->button_type == BUTTON_TYPE_TOGGLE)
1504 *group = NULL;
1505 if (utf8_key) w = gtk_check_menu_item_new ();
1506 else w = gtk_check_menu_item_new_with_label (utf8_label);
1507 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1509 else if (item->button_type == BUTTON_TYPE_RADIO)
1511 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1512 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1513 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1514 if (item->selected)
1515 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1517 else
1519 *group = NULL;
1520 if (utf8_key) w = gtk_menu_item_new ();
1521 else w = gtk_menu_item_new_with_label (utf8_label);
1524 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1525 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1527 return w;
1530 /* Return non-zero if LABEL specifies a separator (GTK only has one
1531 separator type) */
1533 static int
1534 xg_separator_p (char *label)
1536 if (! label) return 0;
1537 else if (strlen (label) > 3
1538 && strncmp (label, "--", 2) == 0
1539 && label[2] != '-')
1541 static char* separator_names[] = {
1542 "space",
1543 "no-line",
1544 "single-line",
1545 "double-line",
1546 "single-dashed-line",
1547 "double-dashed-line",
1548 "shadow-etched-in",
1549 "shadow-etched-out",
1550 "shadow-etched-in-dash",
1551 "shadow-etched-out-dash",
1552 "shadow-double-etched-in",
1553 "shadow-double-etched-out",
1554 "shadow-double-etched-in-dash",
1555 "shadow-double-etched-out-dash",
1559 int i;
1561 label += 2;
1562 for (i = 0; separator_names[i]; ++i)
1563 if (strcmp (label, separator_names[i]) == 0)
1564 return 1;
1566 else
1568 /* Old-style separator, maybe. It's a separator if it contains
1569 only dashes. */
1570 while (*label == '-')
1571 ++label;
1572 if (*label == 0) return 1;
1575 return 0;
1578 static int xg_detached_menus;
1580 /* Returns non-zero if there are detached menus. */
1583 xg_have_tear_offs ()
1585 return xg_detached_menus > 0;
1588 /* Callback invoked when a detached menu window is removed. Here we
1589 decrease the xg_detached_menus count.
1590 WIDGET is the top level window that is removed (the parent of the menu).
1591 CLIENT_DATA is not used. */
1593 static void
1594 tearoff_remove (widget, client_data)
1595 GtkWidget *widget;
1596 gpointer client_data;
1598 if (xg_detached_menus > 0) --xg_detached_menus;
1601 /* Callback invoked when a menu is detached. It increases the
1602 xg_detached_menus count.
1603 WIDGET is the GtkTearoffMenuItem.
1604 CLIENT_DATA is not used. */
1606 static void
1607 tearoff_activate (widget, client_data)
1608 GtkWidget *widget;
1609 gpointer client_data;
1611 GtkWidget *menu = gtk_widget_get_parent (widget);
1612 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1614 ++xg_detached_menus;
1615 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1616 "destroy",
1617 G_CALLBACK (tearoff_remove), 0);
1622 /* Create a menu item widget, and connect the callbacks.
1623 ITEM decribes the menu item.
1624 F is the frame the created menu belongs to.
1625 SELECT_CB is the callback to use when a menu item is selected.
1626 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1627 CL_DATA points to the callback data to be used for this menu.
1628 GROUP is an in/out parameter. If the menu item to be created is not
1629 part of any radio menu group, *GROUP contains NULL on entry and exit.
1630 If the menu item to be created is part of a radio menu group, on entry
1631 *GROUP contains the group to use, or NULL if this is the first item
1632 in the group. On exit, *GROUP contains the radio item group.
1634 Returns the created GtkWidget. */
1636 static GtkWidget *
1637 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1638 widget_value *item;
1639 FRAME_PTR f;
1640 GCallback select_cb;
1641 GCallback highlight_cb;
1642 xg_menu_cb_data *cl_data;
1643 GSList **group;
1645 char *utf8_label;
1646 char *utf8_key;
1647 GtkWidget *w;
1648 xg_menu_item_cb_data *cb_data;
1650 utf8_label = get_utf8_string (item->name);
1651 utf8_key = get_utf8_string (item->key);
1653 w = make_menu_item (utf8_label, utf8_key, item, group);
1655 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1656 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1658 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1660 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1662 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1663 cb_data->help = item->help;
1664 cb_data->cl_data = cl_data;
1665 cb_data->call_data = item->call_data;
1667 g_signal_connect (G_OBJECT (w),
1668 "destroy",
1669 G_CALLBACK (menuitem_destroy_callback),
1670 cb_data);
1672 /* Put cb_data in widget, so we can get at it when modifying menubar */
1673 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1675 /* final item, not a submenu */
1676 if (item->call_data && ! item->contents)
1678 if (select_cb)
1679 cb_data->select_id
1680 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1683 if (! NILP (item->help) && highlight_cb)
1685 /* We use enter/leave notify instead of select/deselect because
1686 select/deselect doesn't go well with detached menus. */
1687 cb_data->highlight_id
1688 = g_signal_connect (G_OBJECT (w),
1689 "enter-notify-event",
1690 G_CALLBACK (menuitem_highlight_callback),
1691 cb_data);
1692 cb_data->unhighlight_id
1693 = g_signal_connect (G_OBJECT (w),
1694 "leave-notify-event",
1695 G_CALLBACK (menuitem_highlight_callback),
1696 cb_data);
1699 return w;
1702 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1703 GCallback, GCallback, int, int, int,
1704 GtkWidget *, xg_menu_cb_data *, char *));
1706 /* Create a full menu tree specified by DATA.
1707 F is the frame the created menu belongs to.
1708 SELECT_CB is the callback to use when a menu item is selected.
1709 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1710 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1711 POP_UP_P is non-zero if we shall create a popup menu.
1712 MENU_BAR_P is non-zero if we shall create a menu bar.
1713 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1714 if MENU_BAR_P is non-zero.
1715 TOPMENU is the topmost GtkWidget that others shall be placed under.
1716 It may be NULL, in that case we create the appropriate widget
1717 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1718 CL_DATA is the callback data we shall use for this menu, or NULL
1719 if we haven't set the first callback yet.
1720 NAME is the name to give to the top level menu if this function
1721 creates it. May be NULL to not set any name.
1723 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1724 not NULL.
1726 This function calls itself to create submenus. */
1728 static GtkWidget *
1729 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1730 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1731 widget_value *data;
1732 FRAME_PTR f;
1733 GCallback select_cb;
1734 GCallback deactivate_cb;
1735 GCallback highlight_cb;
1736 int pop_up_p;
1737 int menu_bar_p;
1738 int add_tearoff_p;
1739 GtkWidget *topmenu;
1740 xg_menu_cb_data *cl_data;
1741 char *name;
1743 widget_value *item;
1744 GtkWidget *wmenu = topmenu;
1745 GSList *group = NULL;
1747 if (! topmenu)
1749 if (! menu_bar_p)
1751 wmenu = gtk_menu_new ();
1752 xg_set_screen (wmenu, f);
1754 else wmenu = gtk_menu_bar_new ();
1756 /* Put cl_data on the top menu for easier access. */
1757 cl_data = make_cl_data (cl_data, f, highlight_cb);
1758 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1759 g_signal_connect (G_OBJECT (wmenu), "destroy",
1760 G_CALLBACK (menu_destroy_callback), cl_data);
1762 if (name)
1763 gtk_widget_set_name (wmenu, name);
1765 if (deactivate_cb)
1766 g_signal_connect (G_OBJECT (wmenu),
1767 "deactivate", deactivate_cb, 0);
1769 g_signal_connect (G_OBJECT (wmenu),
1770 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1773 if (! menu_bar_p && add_tearoff_p)
1775 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1776 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1778 g_signal_connect (G_OBJECT (tearoff), "activate",
1779 G_CALLBACK (tearoff_activate), 0);
1782 for (item = data; item; item = item->next)
1784 GtkWidget *w;
1786 if (pop_up_p && !item->contents && !item->call_data
1787 && !xg_separator_p (item->name))
1789 char *utf8_label;
1790 /* A title for a popup. We do the same as GTK does when
1791 creating titles, but it does not look good. */
1792 group = NULL;
1793 utf8_label = get_utf8_string (item->name);
1795 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1796 w = gtk_menu_item_new_with_label (utf8_label);
1797 gtk_widget_set_sensitive (w, FALSE);
1798 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1800 else if (xg_separator_p (item->name))
1802 group = NULL;
1803 /* GTK only have one separator type. */
1804 w = gtk_separator_menu_item_new ();
1806 else
1808 w = xg_create_one_menuitem (item,
1810 item->contents ? 0 : select_cb,
1811 highlight_cb,
1812 cl_data,
1813 &group);
1815 if (item->contents)
1817 GtkWidget *submenu = create_menus (item->contents,
1819 select_cb,
1820 deactivate_cb,
1821 highlight_cb,
1824 add_tearoff_p,
1826 cl_data,
1828 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1832 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1833 gtk_widget_set_name (w, MENU_ITEM_NAME);
1836 return wmenu;
1839 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1840 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1841 with some text and buttons.
1842 F is the frame the created item belongs to.
1843 NAME is the name to use for the top widget.
1844 VAL is a widget_value structure describing items to be created.
1845 SELECT_CB is the callback to use when a menu item is selected or
1846 a dialog button is pressed.
1847 DEACTIVATE_CB is the callback to use when an item is deactivated.
1848 For a menu, when a sub menu is not shown anymore, for a dialog it is
1849 called when the dialog is popped down.
1850 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1852 Returns the widget created. */
1854 GtkWidget *
1855 xg_create_widget (type, name, f, val,
1856 select_cb, deactivate_cb, highlight_cb)
1857 char *type;
1858 char *name;
1859 FRAME_PTR f;
1860 widget_value *val;
1861 GCallback select_cb;
1862 GCallback deactivate_cb;
1863 GCallback highlight_cb;
1865 GtkWidget *w = 0;
1866 int menu_bar_p = strcmp (type, "menubar") == 0;
1867 int pop_up_p = strcmp (type, "popup") == 0;
1869 if (strcmp (type, "dialog") == 0)
1871 w = create_dialog (val, select_cb, deactivate_cb);
1872 xg_set_screen (w, f);
1873 gtk_window_set_transient_for (GTK_WINDOW (w),
1874 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1875 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1876 gtk_widget_set_name (w, "emacs-dialog");
1878 else if (menu_bar_p || pop_up_p)
1880 w = create_menus (val->contents,
1882 select_cb,
1883 deactivate_cb,
1884 highlight_cb,
1885 pop_up_p,
1886 menu_bar_p,
1887 menu_bar_p,
1890 name);
1892 /* Set the cursor to an arrow for popup menus when they are mapped.
1893 This is done by default for menu bar menus. */
1894 if (pop_up_p)
1896 /* Must realize so the GdkWindow inside the widget is created. */
1897 gtk_widget_realize (w);
1898 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
1901 else
1903 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1904 type);
1907 return w;
1910 /* Return the label for menu item WITEM. */
1912 static const char *
1913 xg_get_menu_item_label (witem)
1914 GtkMenuItem *witem;
1916 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1917 return gtk_label_get_label (wlabel);
1920 /* Return non-zero if the menu item WITEM has the text LABEL. */
1922 static int
1923 xg_item_label_same_p (witem, label)
1924 GtkMenuItem *witem;
1925 char *label;
1927 int is_same = 0;
1928 char *utf8_label = get_utf8_string (label);
1929 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1931 if (! old_label && ! utf8_label)
1932 is_same = 1;
1933 else if (old_label && utf8_label)
1934 is_same = strcmp (utf8_label, old_label) == 0;
1936 if (utf8_label && utf8_label != label) g_free (utf8_label);
1938 return is_same;
1941 /* Destroy widgets in LIST. */
1943 static void
1944 xg_destroy_widgets (list)
1945 GList *list;
1947 GList *iter;
1949 for (iter = list; iter; iter = g_list_next (iter))
1951 GtkWidget *w = GTK_WIDGET (iter->data);
1953 /* Destroying the widget will remove it from the container it is in. */
1954 gtk_widget_destroy (w);
1958 /* Update the top level names in MENUBAR (i.e. not submenus).
1959 F is the frame the menu bar belongs to.
1960 *LIST is a list with the current menu bar names (menu item widgets).
1961 ITER is the item within *LIST that shall be updated.
1962 POS is the numerical position, starting at 0, of ITER in *LIST.
1963 VAL describes what the menu bar shall look like after the update.
1964 SELECT_CB is the callback to use when a menu item is selected.
1965 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1966 CL_DATA points to the callback data to be used for this menu bar.
1968 This function calls itself to walk through the menu bar names. */
1970 static void
1971 xg_update_menubar (menubar, f, list, iter, pos, val,
1972 select_cb, highlight_cb, cl_data)
1973 GtkWidget *menubar;
1974 FRAME_PTR f;
1975 GList **list;
1976 GList *iter;
1977 int pos;
1978 widget_value *val;
1979 GCallback select_cb;
1980 GCallback highlight_cb;
1981 xg_menu_cb_data *cl_data;
1983 if (! iter && ! val)
1984 return;
1985 else if (iter && ! val)
1987 /* Item(s) have been removed. Remove all remaining items. */
1988 xg_destroy_widgets (iter);
1990 /* All updated. */
1991 val = 0;
1992 iter = 0;
1994 else if (! iter && val)
1996 /* Item(s) added. Add all new items in one call. */
1997 create_menus (val, f, select_cb, 0, highlight_cb,
1998 0, 1, 0, menubar, cl_data, 0);
2000 /* All updated. */
2001 val = 0;
2002 iter = 0;
2004 /* Below this neither iter or val is NULL */
2005 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2007 /* This item is still the same, check next item. */
2008 val = val->next;
2009 iter = g_list_next (iter);
2010 ++pos;
2012 else /* This item is changed. */
2014 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2015 GtkMenuItem *witem2 = 0;
2016 int val_in_menubar = 0;
2017 int iter_in_new_menubar = 0;
2018 GList *iter2;
2019 widget_value *cur;
2021 /* See if the changed entry (val) is present later in the menu bar */
2022 for (iter2 = iter;
2023 iter2 && ! val_in_menubar;
2024 iter2 = g_list_next (iter2))
2026 witem2 = GTK_MENU_ITEM (iter2->data);
2027 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2030 /* See if the current entry (iter) is present later in the
2031 specification for the new menu bar. */
2032 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2033 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2035 if (val_in_menubar && ! iter_in_new_menubar)
2037 int nr = pos;
2039 /* This corresponds to:
2040 Current: A B C
2041 New: A C
2042 Remove B. */
2044 gtk_widget_ref (GTK_WIDGET (witem));
2045 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2046 gtk_widget_destroy (GTK_WIDGET (witem));
2048 /* Must get new list since the old changed. */
2049 g_list_free (*list);
2050 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2051 while (nr-- > 0) iter = g_list_next (iter);
2053 else if (! val_in_menubar && ! iter_in_new_menubar)
2055 /* This corresponds to:
2056 Current: A B C
2057 New: A X C
2058 Rename B to X. This might seem to be a strange thing to do,
2059 since if there is a menu under B it will be totally wrong for X.
2060 But consider editing a C file. Then there is a C-mode menu
2061 (corresponds to B above).
2062 If then doing C-x C-f the minibuf menu (X above) replaces the
2063 C-mode menu. When returning from the minibuffer, we get
2064 back the C-mode menu. Thus we do:
2065 Rename B to X (C-mode to minibuf menu)
2066 Rename X to B (minibuf to C-mode menu).
2067 If the X menu hasn't been invoked, the menu under B
2068 is up to date when leaving the minibuffer. */
2069 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2070 char *utf8_label = get_utf8_string (val->name);
2071 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2073 gtk_label_set_text (wlabel, utf8_label);
2075 /* If this item has a submenu that has been detached, change
2076 the title in the WM decorations also. */
2077 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2078 /* Set the title of the detached window. */
2079 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2081 iter = g_list_next (iter);
2082 val = val->next;
2083 ++pos;
2085 else if (! val_in_menubar && iter_in_new_menubar)
2087 /* This corresponds to:
2088 Current: A B C
2089 New: A X B C
2090 Insert X. */
2092 int nr = pos;
2093 GList *group = 0;
2094 GtkWidget *w = xg_create_one_menuitem (val,
2096 select_cb,
2097 highlight_cb,
2098 cl_data,
2099 &group);
2101 gtk_widget_set_name (w, MENU_ITEM_NAME);
2102 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2104 g_list_free (*list);
2105 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2106 while (nr-- > 0) iter = g_list_next (iter);
2107 iter = g_list_next (iter);
2108 val = val->next;
2109 ++pos;
2111 else /* if (val_in_menubar && iter_in_new_menubar) */
2113 int nr = pos;
2114 /* This corresponds to:
2115 Current: A B C
2116 New: A C B
2117 Move C before B */
2119 gtk_widget_ref (GTK_WIDGET (witem2));
2120 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2121 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2122 GTK_WIDGET (witem2), pos);
2123 gtk_widget_unref (GTK_WIDGET (witem2));
2125 g_list_free (*list);
2126 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2127 while (nr-- > 0) iter = g_list_next (iter);
2128 val = val->next;
2129 ++pos;
2133 /* Update the rest of the menu bar. */
2134 xg_update_menubar (menubar, f, list, iter, pos, val,
2135 select_cb, highlight_cb, cl_data);
2138 /* Update the menu item W so it corresponds to VAL.
2139 SELECT_CB is the callback to use when a menu item is selected.
2140 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2141 CL_DATA is the data to set in the widget for menu invokation. */
2143 static void
2144 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2145 widget_value *val;
2146 GtkWidget *w;
2147 GCallback select_cb;
2148 GCallback highlight_cb;
2149 xg_menu_cb_data *cl_data;
2151 GtkWidget *wchild;
2152 GtkLabel *wlbl = 0;
2153 GtkLabel *wkey = 0;
2154 char *utf8_label;
2155 char *utf8_key;
2156 const char *old_label = 0;
2157 const char *old_key = 0;
2158 xg_menu_item_cb_data *cb_data;
2160 wchild = gtk_bin_get_child (GTK_BIN (w));
2161 utf8_label = get_utf8_string (val->name);
2162 utf8_key = get_utf8_string (val->key);
2164 /* See if W is a menu item with a key. See make_menu_item above. */
2165 if (GTK_IS_HBOX (wchild))
2167 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2169 wlbl = GTK_LABEL (list->data);
2170 wkey = GTK_LABEL (list->next->data);
2171 g_list_free (list);
2173 if (! utf8_key)
2175 /* Remove the key and keep just the label. */
2176 gtk_widget_ref (GTK_WIDGET (wlbl));
2177 gtk_container_remove (GTK_CONTAINER (w), wchild);
2178 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2179 wkey = 0;
2183 else /* Just a label. */
2185 wlbl = GTK_LABEL (wchild);
2187 /* Check if there is now a key. */
2188 if (utf8_key)
2190 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2191 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2193 wlbl = GTK_LABEL (list->data);
2194 wkey = GTK_LABEL (list->next->data);
2195 g_list_free (list);
2197 gtk_container_remove (GTK_CONTAINER (w), wchild);
2198 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2203 if (wkey) old_key = gtk_label_get_label (wkey);
2204 if (wlbl) old_label = gtk_label_get_label (wlbl);
2206 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2207 gtk_label_set_text (wkey, utf8_key);
2209 if (! old_label || strcmp (utf8_label, old_label) != 0)
2210 gtk_label_set_text (wlbl, utf8_label);
2212 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2213 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2215 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2216 gtk_widget_set_sensitive (w, FALSE);
2217 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2218 gtk_widget_set_sensitive (w, TRUE);
2220 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2221 XG_ITEM_DATA);
2222 if (cb_data)
2224 cb_data->call_data = val->call_data;
2225 cb_data->help = val->help;
2226 cb_data->cl_data = cl_data;
2228 /* We assume the callback functions don't change. */
2229 if (val->call_data && ! val->contents)
2231 /* This item shall have a select callback. */
2232 if (! cb_data->select_id)
2233 cb_data->select_id
2234 = g_signal_connect (G_OBJECT (w), "activate",
2235 select_cb, cb_data);
2237 else if (cb_data->select_id)
2239 g_signal_handler_disconnect (w, cb_data->select_id);
2240 cb_data->select_id = 0;
2243 if (NILP (cb_data->help))
2245 /* Shall not have help. Remove if any existed previously. */
2246 if (cb_data->highlight_id)
2248 g_signal_handler_disconnect (G_OBJECT (w),
2249 cb_data->highlight_id);
2250 cb_data->highlight_id = 0;
2252 if (cb_data->unhighlight_id)
2254 g_signal_handler_disconnect (G_OBJECT (w),
2255 cb_data->unhighlight_id);
2256 cb_data->unhighlight_id = 0;
2259 else if (! cb_data->highlight_id && highlight_cb)
2261 /* Have help now, but didn't previously. Add callback. */
2262 cb_data->highlight_id
2263 = g_signal_connect (G_OBJECT (w),
2264 "enter-notify-event",
2265 G_CALLBACK (menuitem_highlight_callback),
2266 cb_data);
2267 cb_data->unhighlight_id
2268 = g_signal_connect (G_OBJECT (w),
2269 "leave-notify-event",
2270 G_CALLBACK (menuitem_highlight_callback),
2271 cb_data);
2276 /* Update the toggle menu item W so it corresponds to VAL. */
2278 static void
2279 xg_update_toggle_item (val, w)
2280 widget_value *val;
2281 GtkWidget *w;
2283 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2286 /* Update the radio menu item W so it corresponds to VAL. */
2288 static void
2289 xg_update_radio_item (val, w)
2290 widget_value *val;
2291 GtkWidget *w;
2293 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2296 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2297 SUBMENU may be NULL, in that case a new menu is created.
2298 F is the frame the menu bar belongs to.
2299 VAL describes the contents of the menu bar.
2300 SELECT_CB is the callback to use when a menu item is selected.
2301 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2302 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2303 CL_DATA is the call back data to use for any newly created items.
2305 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2306 was NULL. */
2308 static GtkWidget *
2309 xg_update_submenu (submenu, f, val,
2310 select_cb, deactivate_cb, highlight_cb, cl_data)
2311 GtkWidget *submenu;
2312 FRAME_PTR f;
2313 widget_value *val;
2314 GCallback select_cb;
2315 GCallback deactivate_cb;
2316 GCallback highlight_cb;
2317 xg_menu_cb_data *cl_data;
2319 GtkWidget *newsub = submenu;
2320 GList *list = 0;
2321 GList *iter;
2322 widget_value *cur;
2323 int has_tearoff_p = 0;
2324 GList *first_radio = 0;
2326 if (submenu)
2327 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2329 for (cur = val, iter = list;
2330 cur && iter;
2331 iter = g_list_next (iter), cur = cur->next)
2333 GtkWidget *w = GTK_WIDGET (iter->data);
2335 /* Skip tearoff items, they have no counterpart in val. */
2336 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2338 has_tearoff_p = 1;
2339 iter = g_list_next (iter);
2340 if (iter) w = GTK_WIDGET (iter->data);
2341 else break;
2344 /* Remember first radio button in a group. If we get a mismatch in
2345 a radio group we must rebuild the whole group so that the connections
2346 in GTK becomes correct. */
2347 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2348 first_radio = iter;
2349 else if (cur->button_type != BUTTON_TYPE_RADIO
2350 && ! GTK_IS_RADIO_MENU_ITEM (w))
2351 first_radio = 0;
2353 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2355 if (! xg_separator_p (cur->name))
2356 break;
2358 else if (GTK_IS_CHECK_MENU_ITEM (w))
2360 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2361 break;
2362 xg_update_toggle_item (cur, w);
2363 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2365 else if (GTK_IS_RADIO_MENU_ITEM (w))
2367 if (cur->button_type != BUTTON_TYPE_RADIO)
2368 break;
2369 xg_update_radio_item (cur, w);
2370 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2372 else if (GTK_IS_MENU_ITEM (w))
2374 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2375 GtkWidget *sub;
2377 if (cur->button_type != BUTTON_TYPE_NONE ||
2378 xg_separator_p (cur->name))
2379 break;
2381 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2383 sub = gtk_menu_item_get_submenu (witem);
2384 if (sub && ! cur->contents)
2386 /* Not a submenu anymore. */
2387 gtk_widget_ref (sub);
2388 gtk_menu_item_remove_submenu (witem);
2389 gtk_widget_destroy (sub);
2391 else if (cur->contents)
2393 GtkWidget *nsub;
2395 nsub = xg_update_submenu (sub, f, cur->contents,
2396 select_cb, deactivate_cb,
2397 highlight_cb, cl_data);
2399 /* If this item just became a submenu, we must set it. */
2400 if (nsub != sub)
2401 gtk_menu_item_set_submenu (witem, nsub);
2404 else
2406 /* Structural difference. Remove everything from here and down
2407 in SUBMENU. */
2408 break;
2412 /* Remove widgets from first structual change. */
2413 if (iter)
2415 /* If we are adding new menu items below, we must remove from
2416 first radio button so that radio groups become correct. */
2417 if (cur && first_radio) xg_destroy_widgets (first_radio);
2418 else xg_destroy_widgets (iter);
2421 if (cur)
2423 /* More items added. Create them. */
2424 newsub = create_menus (cur,
2426 select_cb,
2427 deactivate_cb,
2428 highlight_cb,
2431 ! has_tearoff_p,
2432 submenu,
2433 cl_data,
2437 if (list) g_list_free (list);
2439 return newsub;
2442 /* Update the MENUBAR.
2443 F is the frame the menu bar belongs to.
2444 VAL describes the contents of the menu bar.
2445 If DEEP_P is non-zero, rebuild all but the top level menu names in
2446 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2447 SELECT_CB is the callback to use when a menu item is selected.
2448 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2449 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2451 void
2452 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2453 select_cb, deactivate_cb, highlight_cb)
2454 GtkWidget *menubar;
2455 FRAME_PTR f;
2456 widget_value *val;
2457 int deep_p;
2458 GCallback select_cb;
2459 GCallback deactivate_cb;
2460 GCallback highlight_cb;
2462 xg_menu_cb_data *cl_data;
2463 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2465 if (! list) return;
2467 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2468 XG_FRAME_DATA);
2470 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2471 select_cb, highlight_cb, cl_data);
2473 if (deep_p);
2475 widget_value *cur;
2477 /* Update all sub menus.
2478 We must keep the submenus (GTK menu item widgets) since the
2479 X Window in the XEvent that activates the menu are those widgets. */
2481 /* Update cl_data, menu_item things in F may have changed. */
2482 update_cl_data (cl_data, f, highlight_cb);
2484 for (cur = val->contents; cur; cur = cur->next)
2486 GList *iter;
2487 GtkWidget *sub = 0;
2488 GtkWidget *newsub;
2489 GtkMenuItem *witem;
2491 /* Find sub menu that corresponds to val and update it. */
2492 for (iter = list ; iter; iter = g_list_next (iter))
2494 witem = GTK_MENU_ITEM (iter->data);
2495 if (xg_item_label_same_p (witem, cur->name))
2497 sub = gtk_menu_item_get_submenu (witem);
2498 break;
2502 newsub = xg_update_submenu (sub,
2504 cur->contents,
2505 select_cb,
2506 deactivate_cb,
2507 highlight_cb,
2508 cl_data);
2509 /* sub may still be NULL. If we just updated non deep and added
2510 a new menu bar item, it has no sub menu yet. So we set the
2511 newly created sub menu under witem. */
2512 if (newsub != sub)
2514 xg_set_screen (newsub, f);
2515 gtk_menu_item_set_submenu (witem, newsub);
2520 g_list_free (list);
2521 gtk_widget_show_all (menubar);
2524 /* Recompute all the widgets of frame F, when the menu bar has been
2525 changed. Value is non-zero if widgets were updated. */
2528 xg_update_frame_menubar (f)
2529 FRAME_PTR f;
2531 struct x_output *x = f->output_data.x;
2532 GtkRequisition req;
2534 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2535 return 0;
2537 BLOCK_INPUT;
2539 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2540 FALSE, FALSE, 0);
2541 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2543 gtk_widget_show_all (x->menubar_widget);
2544 gtk_widget_size_request (x->menubar_widget, &req);
2546 FRAME_MENUBAR_HEIGHT (f) = req.height;
2548 /* The height has changed, resize outer widget and set columns
2549 rows to what we had before adding the menu bar. */
2550 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2552 SET_FRAME_GARBAGED (f);
2553 UNBLOCK_INPUT;
2555 return 1;
2558 /* Get rid of the menu bar of frame F, and free its storage.
2559 This is used when deleting a frame, and when turning off the menu bar. */
2561 void
2562 free_frame_menubar (f)
2563 FRAME_PTR f;
2565 struct x_output *x = f->output_data.x;
2567 if (x->menubar_widget)
2569 BLOCK_INPUT;
2571 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2572 /* The menubar and its children shall be deleted when removed from
2573 the container. */
2574 x->menubar_widget = 0;
2575 FRAME_MENUBAR_HEIGHT (f) = 0;
2577 /* The height has changed, resize outer widget and set columns
2578 rows to what we had before removing the menu bar. */
2579 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2581 SET_FRAME_GARBAGED (f);
2582 UNBLOCK_INPUT;
2588 /***********************************************************************
2589 Scroll bar functions
2590 ***********************************************************************/
2593 /* Setting scroll bar values invokes the callback. Use this variable
2594 to indicate that callback should do nothing. */
2596 int xg_ignore_gtk_scrollbar;
2598 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2599 32 bits. But we want to store pointers, and they may be larger
2600 than 32 bits. Keep a mapping from integer index to widget pointers
2601 to get around the 32 bit limitation. */
2603 static struct
2605 GtkWidget **widgets;
2606 int max_size;
2607 int used;
2608 } id_to_widget;
2610 /* Grow this much every time we need to allocate more */
2612 #define ID_TO_WIDGET_INCR 32
2614 /* Store the widget pointer W in id_to_widget and return the integer index. */
2616 static int
2617 xg_store_widget_in_map (w)
2618 GtkWidget *w;
2620 int i;
2622 if (id_to_widget.max_size == id_to_widget.used)
2624 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2626 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2627 sizeof (GtkWidget *)*new_size);
2629 for (i = id_to_widget.max_size; i < new_size; ++i)
2630 id_to_widget.widgets[i] = 0;
2631 id_to_widget.max_size = new_size;
2634 /* Just loop over the array and find a free place. After all,
2635 how many scroll bars are we creating? Should be a small number.
2636 The check above guarantees we will find a free place. */
2637 for (i = 0; i < id_to_widget.max_size; ++i)
2639 if (! id_to_widget.widgets[i])
2641 id_to_widget.widgets[i] = w;
2642 ++id_to_widget.used;
2644 return i;
2648 /* Should never end up here */
2649 abort ();
2652 /* Remove pointer at IDX from id_to_widget.
2653 Called when scroll bar is destroyed. */
2655 static void
2656 xg_remove_widget_from_map (idx)
2657 int idx;
2659 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2661 id_to_widget.widgets[idx] = 0;
2662 --id_to_widget.used;
2666 /* Get the widget pointer at IDX from id_to_widget. */
2668 static GtkWidget *
2669 xg_get_widget_from_map (idx)
2670 int idx;
2672 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2673 return id_to_widget.widgets[idx];
2675 return 0;
2678 /* Return the scrollbar id for X Window WID on display DPY.
2679 Return -1 if WID not in id_to_widget. */
2682 xg_get_scroll_id_for_window (dpy, wid)
2683 Display *dpy;
2684 Window wid;
2686 int idx;
2687 GtkWidget *w;
2689 w = xg_win_to_widget (dpy, wid);
2691 if (w)
2693 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2694 if (id_to_widget.widgets[idx] == w)
2695 return idx;
2698 return -1;
2701 /* Callback invoked when scroll bar WIDGET is destroyed.
2702 DATA is the index into id_to_widget for WIDGET.
2703 We free pointer to last scroll bar values here and remove the index. */
2705 static void
2706 xg_gtk_scroll_destroy (widget, data)
2707 GtkWidget *widget;
2708 gpointer data;
2710 gpointer p;
2711 int id = (int)data;
2713 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2714 if (p) xfree (p);
2715 xg_remove_widget_from_map (id);
2718 /* Callback for button press/release events. Used to start timer so that
2719 the scroll bar repetition timer in GTK gets handeled.
2720 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2721 WIDGET is the scroll bar widget the event is for (not used).
2722 EVENT contains the event.
2723 USER_DATA points to the struct scrollbar structure.
2725 Returns FALSE to tell GTK that it shall continue propagate the event
2726 to widgets. */
2728 static gboolean
2729 scroll_bar_button_cb (widget, event, user_data)
2730 GtkWidget *widget;
2731 GdkEventButton *event;
2732 gpointer user_data;
2734 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2735 xg_start_timer ();
2736 else if (event->type == GDK_BUTTON_RELEASE)
2738 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2739 if (xg_timer) xg_stop_timer ();
2740 bar->dragging = Qnil;
2743 return FALSE;
2746 /* Create a scroll bar widget for frame F. Store the scroll bar
2747 in BAR.
2748 SCROLL_CALLBACK is the callback to invoke when the value of the
2749 bar changes.
2750 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2751 to set resources for the widget. */
2753 void
2754 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2755 FRAME_PTR f;
2756 struct scroll_bar *bar;
2757 GCallback scroll_callback;
2758 char *scroll_bar_name;
2760 GtkWidget *wscroll;
2761 GtkWidget *webox;
2762 GtkObject *vadj;
2763 int scroll_id;
2765 /* Page, step increment values are not so important here, they
2766 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2767 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2768 0.1, 0.1, 0.1);
2770 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2771 webox = gtk_event_box_new ();
2772 gtk_widget_set_name (wscroll, scroll_bar_name);
2773 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2775 scroll_id = xg_store_widget_in_map (wscroll);
2777 g_signal_connect (G_OBJECT (wscroll),
2778 "value-changed",
2779 scroll_callback,
2780 (gpointer) bar);
2781 g_signal_connect (G_OBJECT (wscroll),
2782 "destroy",
2783 G_CALLBACK (xg_gtk_scroll_destroy),
2784 (gpointer) scroll_id);
2786 /* Connect to button press and button release to detect if any scroll bar
2787 has the pointer. */
2788 g_signal_connect (G_OBJECT (wscroll),
2789 "button-press-event",
2790 G_CALLBACK (scroll_bar_button_cb),
2791 (gpointer) bar);
2792 g_signal_connect (G_OBJECT (wscroll),
2793 "button-release-event",
2794 G_CALLBACK (scroll_bar_button_cb),
2795 (gpointer) bar);
2797 /* The scroll bar widget does not draw on a window of its own. Instead
2798 it draws on the parent window, in this case the edit widget. So
2799 whenever the edit widget is cleared, the scroll bar needs to redraw
2800 also, which causes flicker. Put an event box between the edit widget
2801 and the scroll bar, so the scroll bar instead draws itself on the
2802 event box window. */
2803 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
2804 gtk_container_add (GTK_CONTAINER (webox), wscroll);
2807 /* Set the cursor to an arrow. */
2808 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2810 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2813 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2815 void
2816 xg_show_scroll_bar (scrollbar_id)
2817 int scrollbar_id;
2819 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2820 if (w)
2821 gtk_widget_show_all (gtk_widget_get_parent (w));
2824 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2826 void
2827 xg_remove_scroll_bar (f, scrollbar_id)
2828 FRAME_PTR f;
2829 int scrollbar_id;
2831 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2832 if (w)
2834 GtkWidget *wparent = gtk_widget_get_parent (w);
2835 gtk_widget_destroy (w);
2836 gtk_widget_destroy (wparent);
2837 SET_FRAME_GARBAGED (f);
2841 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2842 in frame F.
2843 TOP/LEFT are the new pixel positions where the bar shall appear.
2844 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2846 void
2847 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
2848 FRAME_PTR f;
2849 int scrollbar_id;
2850 int top;
2851 int left;
2852 int width;
2853 int height;
2856 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2858 if (wscroll)
2860 GtkWidget *wfixed = f->output_data.x->edit_widget;
2861 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
2863 /* Move and resize to new values. */
2864 gtk_widget_set_size_request (wscroll, width, height);
2865 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
2867 SET_FRAME_GARBAGED (f);
2868 cancel_mouse_face (f);
2872 /* Set the thumb size and position of scroll bar BAR. We are currently
2873 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2875 void
2876 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2877 struct scroll_bar *bar;
2878 int portion, position, whole;
2880 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2882 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2884 if (wscroll && NILP (bar->dragging))
2886 GtkAdjustment *adj;
2887 gdouble shown;
2888 gdouble top;
2889 int size, value;
2890 int new_step;
2891 int changed = 0;
2893 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
2895 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2896 rather than the real portion value. This makes the thumb less likely
2897 to resize and that looks better. */
2898 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
2899 /* When the thumb is at the bottom, position == whole.
2900 So we need to increase `whole' to make space for the thumb. */
2901 whole += portion;
2903 if (whole <= 0)
2904 top = 0, shown = 1;
2905 else
2907 top = (gdouble) position / whole;
2908 shown = (gdouble) portion / whole;
2911 size = shown * XG_SB_RANGE;
2912 size = min (size, XG_SB_RANGE);
2913 size = max (size, 1);
2915 value = top * XG_SB_RANGE;
2916 value = min (value, XG_SB_MAX - size);
2917 value = max (value, XG_SB_MIN);
2919 /* Assume all lines are of equal size. */
2920 new_step = size / max (1, FRAME_LINES (f));
2922 if ((int) adj->page_size != size
2923 || (int) adj->step_increment != new_step)
2925 adj->page_size = size;
2926 adj->step_increment = new_step;
2927 /* Assume a page increment is about 95% of the page size */
2928 adj->page_increment = (int) (0.95*adj->page_size);
2929 changed = 1;
2932 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2934 GtkWidget *wfixed = f->output_data.x->edit_widget;
2936 BLOCK_INPUT;
2938 /* gtk_range_set_value invokes the callback. Set
2939 ignore_gtk_scrollbar to make the callback do nothing */
2940 xg_ignore_gtk_scrollbar = 1;
2942 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2943 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
2944 else if (changed)
2945 gtk_adjustment_changed (adj);
2947 xg_ignore_gtk_scrollbar = 0;
2949 UNBLOCK_INPUT;
2955 /***********************************************************************
2956 Tool bar functions
2957 ***********************************************************************/
2958 /* The key for the data we put in the GtkImage widgets. The data is
2959 the image used by Emacs. We use this to see if we need to update
2960 the GtkImage with a new image. */
2961 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2963 /* Callback function invoked when a tool bar item is pressed.
2964 W is the button widget in the tool bar that got pressed,
2965 CLIENT_DATA is an integer that is the index of the button in the
2966 tool bar. 0 is the first button. */
2968 static void
2969 xg_tool_bar_callback (w, client_data)
2970 GtkWidget *w;
2971 gpointer client_data;
2973 int idx = (int)client_data;
2974 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2975 Lisp_Object key, frame;
2976 struct input_event event;
2977 EVENT_INIT (event);
2979 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2980 return;
2982 idx *= TOOL_BAR_ITEM_NSLOTS;
2984 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
2985 XSETFRAME (frame, f);
2986 event.kind = TOOL_BAR_EVENT;
2987 event.frame_or_window = frame;
2988 event.arg = frame;
2989 kbd_buffer_store_event (&event);
2991 event.kind = TOOL_BAR_EVENT;
2992 event.frame_or_window = frame;
2993 event.arg = key;
2994 event.modifiers = 0; /* These are not available. */
2995 kbd_buffer_store_event (&event);
2998 /* This callback is called when a tool bar is detached. We must set
2999 the height of the tool bar to zero when this happens so frame sizes
3000 are correctly calculated.
3001 WBOX is the handle box widget that enables detach/attach of the tool bar.
3002 W is the tool bar widget.
3003 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3005 static void
3006 xg_tool_bar_detach_callback (wbox, w, client_data)
3007 GtkHandleBox *wbox;
3008 GtkWidget *w;
3009 gpointer client_data;
3011 FRAME_PTR f = (FRAME_PTR) client_data;
3013 if (f)
3015 /* When detaching a tool bar, not everything dissapear. There are
3016 a few pixels left that are used to drop the tool bar back into
3017 place. */
3018 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
3019 FRAME_TOOLBAR_HEIGHT (f) = 2;
3021 /* The height has changed, resize outer widget and set columns
3022 rows to what we had before detaching the tool bar. */
3023 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3027 /* This callback is called when a tool bar is reattached. We must set
3028 the height of the tool bar when this happens so frame sizes
3029 are correctly calculated.
3030 WBOX is the handle box widget that enables detach/attach of the tool bar.
3031 W is the tool bar widget.
3032 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3034 static void
3035 xg_tool_bar_attach_callback (wbox, w, client_data)
3036 GtkHandleBox *wbox;
3037 GtkWidget *w;
3038 gpointer client_data;
3040 FRAME_PTR f = (FRAME_PTR) client_data;
3042 if (f)
3044 GtkRequisition req;
3046 gtk_widget_size_request (w, &req);
3047 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3049 /* The height has changed, resize outer widget and set columns
3050 rows to what we had before detaching the tool bar. */
3051 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3055 /* This callback is called when the mouse enters or leaves a tool bar item.
3056 It is used for displaying and hiding the help text.
3057 W is the tool bar item, a button.
3058 EVENT is either an enter event or leave event.
3059 CLIENT_DATA is an integer that is the index of the button in the
3060 tool bar. 0 is the first button.
3062 Returns FALSE to tell GTK to keep processing this event. */
3064 static gboolean
3065 xg_tool_bar_help_callback (w, event, client_data)
3066 GtkWidget *w;
3067 GdkEventCrossing *event;
3068 gpointer client_data;
3070 int idx = (int)client_data;
3071 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3072 Lisp_Object help, frame;
3074 if (! GTK_IS_BUTTON (w))
3076 return FALSE;
3079 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3080 return FALSE;
3082 if (event->type == GDK_ENTER_NOTIFY)
3084 idx *= TOOL_BAR_ITEM_NSLOTS;
3085 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3087 if (NILP (help))
3088 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3090 else
3091 help = Qnil;
3093 XSETFRAME (frame, f);
3094 kbd_buffer_store_help_event (frame, help);
3096 return FALSE;
3100 /* This callback is called when a tool bar item shall be redrawn.
3101 It modifies the expose event so that the GtkImage widget redraws the
3102 whole image. This to overcome a bug that makes GtkImage draw the image
3103 in the wrong place when it tries to redraw just a part of the image.
3104 W is the GtkImage to be redrawn.
3105 EVENT is the expose event for W.
3106 CLIENT_DATA is unused.
3108 Returns FALSE to tell GTK to keep processing this event. */
3110 static gboolean
3111 xg_tool_bar_item_expose_callback (w, event, client_data)
3112 GtkWidget *w;
3113 GdkEventExpose *event;
3114 gpointer client_data;
3116 gint width, height;
3118 gdk_drawable_get_size (event->window, &width, &height);
3120 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3121 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3123 event->area.x = max (0, event->area.x);
3124 event->area.y = max (0, event->area.y);
3126 event->area.width = max (width, event->area.width);
3127 event->area.height = max (height, event->area.height);
3129 return FALSE;
3132 /* This callback is called when a tool bar shall be redrawn.
3133 We need to update the tool bar from here in case the image cache
3134 has deleted the pixmaps used in the tool bar.
3135 W is the GtkToolbar to be redrawn.
3136 EVENT is the expose event for W.
3137 CLIENT_DATA is pointing to the frame for this tool bar.
3139 Returns FALSE to tell GTK to keep processing this event. */
3141 static gboolean
3142 xg_tool_bar_expose_callback (w, event, client_data)
3143 GtkWidget *w;
3144 GdkEventExpose *event;
3145 gpointer client_data;
3147 update_frame_tool_bar ((FRAME_PTR) client_data);
3148 return FALSE;
3151 /* Create a tool bar for frame F. */
3153 static void
3154 xg_create_tool_bar (f)
3155 FRAME_PTR f;
3157 struct x_output *x = f->output_data.x;
3158 GtkRequisition req;
3159 int vbox_pos = x->menubar_widget ? 1 : 0;
3161 x->toolbar_widget = gtk_toolbar_new ();
3162 x->handlebox_widget = gtk_handle_box_new ();
3163 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3164 x->toolbar_widget);
3166 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3167 FALSE, FALSE, 0);
3169 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3170 vbox_pos);
3172 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3174 /* We only have icons, so override any user setting. We could
3175 use the caption property of the toolbar item (see update_frame_tool_bar
3176 below), but some of those strings are long, making the toolbar so
3177 long it does not fit on the screen. The GtkToolbar widget makes every
3178 item equal size, so the longest caption determine the size of every
3179 tool bar item. I think the creators of the GtkToolbar widget
3180 counted on 4 or 5 character long strings. */
3181 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3182 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3183 GTK_ORIENTATION_HORIZONTAL);
3185 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3186 G_CALLBACK (xg_tool_bar_detach_callback), f);
3187 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3188 G_CALLBACK (xg_tool_bar_attach_callback), f);
3189 g_signal_connect (G_OBJECT (x->toolbar_widget),
3190 "expose-event",
3191 G_CALLBACK (xg_tool_bar_expose_callback),
3194 gtk_widget_show_all (x->handlebox_widget);
3196 gtk_widget_size_request (x->toolbar_widget, &req);
3197 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3199 /* The height has changed, resize outer widget and set columns
3200 rows to what we had before adding the tool bar. */
3201 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3203 SET_FRAME_GARBAGED (f);
3206 /* Update the tool bar for frame F. Add new buttons and remove old. */
3208 void
3209 update_frame_tool_bar (f)
3210 FRAME_PTR f;
3212 int i;
3213 GtkRequisition old_req, new_req;
3214 GList *icon_list;
3215 GList *iter;
3216 struct x_output *x = f->output_data.x;
3218 if (! FRAME_GTK_WIDGET (f))
3219 return;
3221 BLOCK_INPUT;
3223 if (! x->toolbar_widget)
3224 xg_create_tool_bar (f);
3226 gtk_widget_size_request (x->toolbar_widget, &old_req);
3228 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3229 iter = icon_list;
3231 for (i = 0; i < f->n_tool_bar_items; ++i)
3233 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3235 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3236 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3237 int idx;
3238 int img_id;
3239 struct image *img;
3240 Lisp_Object image;
3241 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3243 if (iter) iter = g_list_next (iter);
3245 /* If image is a vector, choose the image according to the
3246 button state. */
3247 image = PROP (TOOL_BAR_ITEM_IMAGES);
3248 if (VECTORP (image))
3250 if (enabled_p)
3251 idx = (selected_p
3252 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3253 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3254 else
3255 idx = (selected_p
3256 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3257 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3259 xassert (ASIZE (image) >= idx);
3260 image = AREF (image, idx);
3262 else
3263 idx = -1;
3265 /* Ignore invalid image specifications. */
3266 if (!valid_image_p (image))
3268 if (wicon) gtk_widget_hide (wicon);
3269 continue;
3272 img_id = lookup_image (f, image);
3273 img = IMAGE_FROM_ID (f, img_id);
3274 prepare_image_for_display (f, img);
3276 if (img->load_failed_p || img->pixmap == None)
3278 if (wicon) gtk_widget_hide (wicon);
3279 continue;
3282 if (! wicon)
3284 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3286 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3287 0, 0, 0,
3289 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3290 (gpointer)i);
3292 /* Save the image so we can see if an update is needed when
3293 this function is called again. */
3294 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3295 (gpointer)img->pixmap);
3297 /* Catch expose events to overcome an annoying redraw bug, see
3298 comment for xg_tool_bar_item_expose_callback. */
3299 g_signal_connect (G_OBJECT (w),
3300 "expose-event",
3301 G_CALLBACK (xg_tool_bar_item_expose_callback),
3304 /* We must set sensitive on the button that is the parent
3305 of the GtkImage parent. Go upwards until we find the button. */
3306 while (! GTK_IS_BUTTON (w))
3307 w = gtk_widget_get_parent (w);
3309 if (w)
3311 /* Save the frame in the button so the xg_tool_bar_callback
3312 can get at it. */
3313 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3314 gtk_widget_set_sensitive (w, enabled_p);
3316 /* Use enter/leave notify to show help. We use the events
3317 rather than the GtkButton specific signals "enter" and
3318 "leave", so we can have only one callback. The event
3319 will tell us what kind of event it is. */
3320 g_signal_connect (G_OBJECT (w),
3321 "enter-notify-event",
3322 G_CALLBACK (xg_tool_bar_help_callback),
3323 (gpointer)i);
3324 g_signal_connect (G_OBJECT (w),
3325 "leave-notify-event",
3326 G_CALLBACK (xg_tool_bar_help_callback),
3327 (gpointer)i);
3330 else
3332 /* The child of the tool bar is a button. Inside that button
3333 is a vbox. Inside that vbox is the GtkImage. */
3334 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3335 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3336 GtkImage *wimage = GTK_IMAGE (chlist->data);
3337 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3338 XG_TOOL_BAR_IMAGE_DATA);
3339 g_list_free (chlist);
3341 if (old_img != img->pixmap)
3342 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
3344 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3345 (gpointer)img->pixmap);
3347 gtk_widget_set_sensitive (wicon, enabled_p);
3348 gtk_widget_show (wicon);
3351 #undef PROP
3354 /* Remove buttons not longer needed. We just hide them so they
3355 can be reused later on. */
3356 while (iter)
3358 GtkWidget *w = GTK_WIDGET (iter->data);
3359 gtk_widget_hide (w);
3360 iter = g_list_next (iter);
3363 gtk_widget_size_request (x->toolbar_widget, &new_req);
3364 if (old_req.height != new_req.height)
3366 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3367 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3370 if (icon_list) g_list_free (icon_list);
3372 UNBLOCK_INPUT;
3375 /* Deallocate all resources for the tool bar on frame F.
3376 Remove the tool bar. */
3378 void
3379 free_frame_tool_bar (f)
3380 FRAME_PTR f;
3382 struct x_output *x = f->output_data.x;
3384 if (x->toolbar_widget)
3386 BLOCK_INPUT;
3387 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3388 x->handlebox_widget);
3389 x->toolbar_widget = 0;
3390 x->handlebox_widget = 0;
3391 FRAME_TOOLBAR_HEIGHT (f) = 0;
3393 /* The height has changed, resize outer widget and set columns
3394 rows to what we had before removing the tool bar. */
3395 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3397 SET_FRAME_GARBAGED (f);
3398 UNBLOCK_INPUT;
3404 /***********************************************************************
3405 Initializing
3406 ***********************************************************************/
3407 void
3408 xg_initialize ()
3410 xg_ignore_gtk_scrollbar = 0;
3411 xg_detached_menus = 0;
3412 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3413 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3415 id_to_widget.max_size = id_to_widget.used = 0;
3416 id_to_widget.widgets = 0;
3418 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3419 bindings. It doesn't seem to be any way to remove properties,
3420 so we set it to VoidSymbol which in X means "no key". */
3421 gtk_settings_set_string_property (gtk_settings_get_default (),
3422 "gtk-menu-bar-accel",
3423 "VoidSymbol",
3424 EMACS_CLASS);
3426 /* Make GTK text input widgets use Emacs style keybindings. This is
3427 Emacs after all. */
3428 gtk_settings_set_string_property (gtk_settings_get_default (),
3429 "gtk-key-theme-name",
3430 "Emacs",
3431 EMACS_CLASS);
3434 #endif /* USE_GTK */
3436 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3437 (do not change this comment) */