Various small changes in addition to:
[emacs.git] / src / gtkutil.c
blob3ffba0ba745e1a0b4c848f67da418d841d4843aa
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. */
51 static GdkDisplay *
52 xg_get_gdk_display (dpy)
53 Display *dpy;
55 return gdk_x11_lookup_xdisplay (dpy);
58 /* When the GTK widget W is to be created on a display for F that
59 is not the default display, set the display for W.
60 W can be a GtkMenu or a GtkWindow widget. */
61 static void
62 xg_set_screen (w, f)
63 GtkWidget *w;
64 FRAME_PTR f;
66 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
68 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
69 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
71 if (GTK_IS_MENU (w))
72 gtk_menu_set_screen (GTK_MENU (w), gscreen);
73 else
74 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
79 #else /* not HAVE_GTK_MULTIDISPLAY */
81 /* Make some defines so we can use the GTK 2.2 functions when
82 compiling with GTK 2.0. */
83 #define xg_set_screen(w, f)
84 #define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
85 #define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
86 #define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
87 #define gdk_x11_lookup_xdisplay(dpy) 0
88 #define GdkDisplay void
90 #endif /* not HAVE_GTK_MULTIDISPLAY */
92 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
93 *DPY is set to NULL if the display can't be opened.
95 Returns non-zero if display could be opened, zero if display could not
96 be opened, and less than zero if the GTK version doesn't support
97 multipe displays. */
98 int
99 xg_display_open (display_name, dpy)
100 char *display_name;
101 Display **dpy;
103 #ifdef HAVE_GTK_MULTIDISPLAY
104 GdkDisplay *gdpy;
106 gdpy = gdk_display_open (display_name);
107 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
109 return gdpy != NULL;
111 #else /* not HAVE_GTK_MULTIDISPLAY */
113 return -1;
114 #endif /* not HAVE_GTK_MULTIDISPLAY */
118 void
119 xg_display_close (Display *dpy)
121 #ifdef HAVE_GTK_MULTIDISPLAY
122 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
124 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
125 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
126 we can continue running, but there will be memory leaks. */
128 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
130 /* If this is the default display, we must change it before calling
131 dispose, otherwise it will crash. */
132 if (gdk_display_get_default () == gdpy)
134 struct x_display_info *dpyinfo;
135 Display *new_dpy = 0;
136 GdkDisplay *gdpy_new;
138 /* Find another display. */
139 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
140 if (dpyinfo->display != dpy)
142 new_dpy = dpyinfo->display;
143 break;
146 if (! new_dpy) return; /* Emacs will exit anyway. */
148 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
149 gdk_display_manager_set_default_display (gdk_display_manager_get (),
150 gdpy_new);
153 g_object_run_dispose (G_OBJECT (gdpy));
155 #else
156 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
157 gdk_display_close (gdpy);
158 #endif
159 #endif /* HAVE_GTK_MULTIDISPLAY */
163 /***********************************************************************
164 Utility functions
165 ***********************************************************************/
166 /* The timer for scroll bar repetition and menu bar timeouts.
167 NULL if no timer is started. */
168 static struct atimer *xg_timer;
171 /* The next two variables and functions are taken from lwlib. */
172 static widget_value *widget_value_free_list;
173 static int malloc_cpt;
175 /* Allocate a widget_value structure, either by taking one from the
176 widget_value_free_list or by malloc:ing a new one.
178 Return a pointer to the allocated structure. */
179 widget_value *
180 malloc_widget_value ()
182 widget_value *wv;
183 if (widget_value_free_list)
185 wv = widget_value_free_list;
186 widget_value_free_list = wv->free_list;
187 wv->free_list = 0;
189 else
191 wv = (widget_value *) malloc (sizeof (widget_value));
192 malloc_cpt++;
194 memset (wv, 0, sizeof (widget_value));
195 return wv;
198 /* This is analogous to free. It frees only what was allocated
199 by malloc_widget_value, and no substructures. */
200 void
201 free_widget_value (wv)
202 widget_value *wv;
204 if (wv->free_list)
205 abort ();
207 if (malloc_cpt > 25)
209 /* When the number of already allocated cells is too big,
210 We free it. */
211 free (wv);
212 malloc_cpt--;
214 else
216 wv->free_list = widget_value_free_list;
217 widget_value_free_list = wv;
222 /* Create and return the cursor to be used for popup menus and
223 scroll bars on display DPY. */
224 GdkCursor *
225 xg_create_default_cursor (dpy)
226 Display *dpy;
228 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
229 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
232 /* For the image defined in IMG, make and return a GtkImage. For displays with
233 8 planes or less we must make a GdkPixbuf and apply the mask manually.
234 Otherwise the highlightning and dimming the tool bar code in GTK does
235 will look bad. For display with more than 8 planes we just use the
236 pixmap and mask directly. For monochrome displays, GTK doesn't seem
237 able to use external pixmaps, it looks bad whatever we do.
238 The image is defined on the display where frame F is.
239 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
240 If OLD_WIDGET is NULL, a new widget is constructed and returned.
241 If OLD_WIDGET is not NULL, that widget is modified. */
242 static GtkWidget *
243 xg_get_image_for_pixmap (f, img, widget, old_widget)
244 FRAME_PTR f;
245 struct image *img;
246 GtkWidget *widget;
247 GtkImage *old_widget;
249 GdkPixmap *gpix;
250 GdkPixmap *gmask;
251 GdkDisplay *gdpy;
253 /* If we are on a one bit display, let GTK do all the image handling.
254 This seems to be the only way to make insensitive and activated icons
255 look good. */
256 if (x_screen_planes (f) == 1)
258 Lisp_Object specified_file = Qnil;
259 Lisp_Object tail;
260 extern Lisp_Object QCfile;
262 for (tail = XCDR (img->spec);
263 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
264 tail = XCDR (XCDR (tail)))
265 if (EQ (XCAR (tail), QCfile))
266 specified_file = XCAR (XCDR (tail));
268 if (STRINGP (specified_file))
271 Lisp_Object file = Qnil;
272 struct gcpro gcpro1;
273 GCPRO1 (file);
275 file = x_find_image_file (specified_file);
276 /* We already loaded the image once before calling this
277 function, so this should not fail. */
278 xassert (STRINGP (file) != 0);
280 if (! old_widget)
281 old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
282 else
283 gtk_image_set_from_file (old_widget, SDATA (file));
285 UNGCPRO;
286 return GTK_WIDGET (old_widget);
290 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
291 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
292 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
294 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
296 if (! old_widget)
297 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
298 else
299 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
301 else
303 /* This is a workaround to make icons look good on pseudo color
304 displays. Apparently GTK expects the images to have an alpha
305 channel. If they don't, insensitive and activated icons will
306 look bad. This workaround does not work on monochrome displays,
307 and is not needed on true color/static color displays (i.e.
308 16 bits and higher). */
309 int x, y, width, height, rowstride, mask_rowstride;
310 GdkPixbuf *icon_buf, *tmp_buf;
311 guchar *pixels;
312 guchar *mask_pixels;
314 gdk_drawable_get_size (gpix, &width, &height);
315 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
316 gpix,
317 gtk_widget_get_colormap (widget),
318 0, 0, 0, 0, width, height);
319 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
320 g_object_unref (G_OBJECT (tmp_buf));
322 if (gmask)
324 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
325 gmask,
326 NULL,
327 0, 0, 0, 0,
328 width, height);
329 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
330 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
331 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
332 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
333 int y;
335 for (y = 0; y < height; ++y)
337 guchar *iconptr, *maskptr;
338 int x;
340 iconptr = pixels + y * rowstride;
341 maskptr = mask_pixels + y * mask_rowstride;
343 for (x = 0; x < width; ++x)
345 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
346 just R is sufficient. */
347 if (maskptr[0] == 0)
348 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
350 iconptr += rowstride/width;
351 maskptr += mask_rowstride/width;
355 g_object_unref (G_OBJECT (mask_buf));
358 if (! old_widget)
359 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
360 else
361 gtk_image_set_from_pixbuf (old_widget, icon_buf);
363 g_object_unref (G_OBJECT (icon_buf));
366 g_object_unref (G_OBJECT (gpix));
367 if (gmask) g_object_unref (G_OBJECT (gmask));
369 return GTK_WIDGET (old_widget);
373 /* Set CURSOR on W and all widgets W contain. We must do like this
374 for scroll bars and menu because they create widgets internally,
375 and it is those widgets that are visible. */
376 static void
377 xg_set_cursor (w, cursor)
378 GtkWidget *w;
379 GdkCursor *cursor;
381 GList *children = gdk_window_peek_children (w->window);
383 gdk_window_set_cursor (w->window, cursor);
385 /* The scroll bar widget has more than one GDK window (had to look at
386 the source to figure this out), and there is no way to set cursor
387 on widgets in GTK. So we must set the cursor for all GDK windows.
388 Ditto for menus. */
390 for ( ; children; children = g_list_next (children))
391 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
394 /* Timer function called when a timeout occurs for xg_timer.
395 This function processes all GTK events in a recursive event loop.
396 This is done because GTK timer events are not seen by Emacs event
397 detection, Emacs only looks for X events. When a scroll bar has the
398 pointer (detected by button press/release events below) an Emacs
399 timer is started, and this function can then check if the GTK timer
400 has expired by calling the GTK event loop.
401 Also, when a menu is active, it has a small timeout before it
402 pops down the sub menu under it. */
403 static void
404 xg_process_timeouts (timer)
405 struct atimer *timer;
407 BLOCK_INPUT;
408 /* Ideally we would like to just handle timer events, like the Xt version
409 of this does in xterm.c, but there is no such feature in GTK. */
410 while (gtk_events_pending ())
411 gtk_main_iteration ();
412 UNBLOCK_INPUT;
415 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
416 xg_process_timeouts is called when the timer expires. The timer
417 started is continuous, i.e. runs until xg_stop_timer is called. */
418 static void
419 xg_start_timer ()
421 if (! xg_timer)
423 EMACS_TIME interval;
424 EMACS_SET_SECS_USECS (interval, 0, 100000);
425 xg_timer = start_atimer (ATIMER_CONTINUOUS,
426 interval,
427 xg_process_timeouts,
432 /* Stop the xg_timer if started. */
433 static void
434 xg_stop_timer ()
436 if (xg_timer)
438 cancel_atimer (xg_timer);
439 xg_timer = 0;
443 /* Insert NODE into linked LIST. */
444 static void
445 xg_list_insert (xg_list_node *list, xg_list_node *node)
447 xg_list_node *list_start = list->next;
449 if (list_start) list_start->prev = node;
450 node->next = list_start;
451 node->prev = 0;
452 list->next = node;
455 /* Remove NODE from linked LIST. */
456 static void
457 xg_list_remove (xg_list_node *list, xg_list_node *node)
459 xg_list_node *list_start = list->next;
460 if (node == list_start)
462 list->next = node->next;
463 if (list->next) list->next->prev = 0;
465 else
467 node->prev->next = node->next;
468 if (node->next) node->next->prev = node->prev;
472 /* Allocate and return a utf8 version of STR. If STR is already
473 utf8 or NULL, just return STR.
474 If not, a new string is allocated and the caller must free the result
475 with g_free. */
476 static char *
477 get_utf8_string (str)
478 char *str;
480 char *utf8_str = str;
482 /* If not UTF-8, try current locale. */
483 if (str && !g_utf8_validate (str, -1, NULL))
484 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
486 return utf8_str;
491 /***********************************************************************
492 General functions for creating widgets, resizing, events, e.t.c.
493 ***********************************************************************/
495 /* Make a geometry string and pass that to GTK. It seems this is the
496 only way to get geometry position right if the user explicitly
497 asked for a position when starting Emacs.
498 F is the frame we shall set geometry for. */
499 static void
500 xg_set_geometry (f)
501 FRAME_PTR f;
503 if (f->size_hint_flags & USPosition)
505 int left = f->left_pos;
506 int xneg = f->size_hint_flags & XNegative;
507 int top = f->top_pos;
508 int yneg = f->size_hint_flags & YNegative;
509 char geom_str[32];
511 if (xneg)
512 left = -left;
513 if (yneg)
514 top = -top;
516 sprintf (geom_str, "=%dx%d%c%d%c%d",
517 FRAME_PIXEL_WIDTH (f),
518 FRAME_TOTAL_PIXEL_HEIGHT (f),
519 (xneg ? '-' : '+'), left,
520 (yneg ? '-' : '+'), top);
522 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
523 geom_str))
524 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
529 /* Resize the outer window of frame F after chainging the height.
530 This happend when the menu bar or the tool bar is added or removed.
531 COLUMNS/ROWS is the size the edit area shall have after the resize. */
532 static void
533 xg_resize_outer_widget (f, columns, rows)
534 FRAME_PTR f;
535 int columns;
536 int rows;
538 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
539 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
541 /* base_height is now changed. */
542 x_wm_set_size_hint (f, 0, 0);
544 /* If we are not mapped yet, set geometry once again, as window
545 height now have changed. */
546 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
547 xg_set_geometry (f);
549 xg_frame_set_char_size (f, columns, rows);
550 gdk_window_process_all_updates ();
553 /* This gets called after the frame F has been cleared. Since that is
554 done with X calls, we need to redraw GTK widget (scroll bars). */
555 void
556 xg_frame_cleared (f)
557 FRAME_PTR f;
559 GtkWidget *w = f->output_data.x->widget;
561 if (w)
563 gtk_container_set_reallocate_redraws (GTK_CONTAINER (w), TRUE);
564 gtk_container_foreach (GTK_CONTAINER (w),
565 (GtkCallback) gtk_widget_queue_draw,
567 gdk_window_process_all_updates ();
571 /* Function to handle resize of our widgets. Since Emacs has some layouts
572 that does not fit well with GTK standard containers, we do most layout
573 manually.
574 F is the frame to resize.
575 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
576 void
577 xg_resize_widgets (f, pixelwidth, pixelheight)
578 FRAME_PTR f;
579 int pixelwidth, pixelheight;
581 int mbheight = FRAME_MENUBAR_HEIGHT (f);
582 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
583 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
584 - mbheight - tbheight));
585 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
587 if (FRAME_GTK_WIDGET (f)
588 && (columns != FRAME_COLS (f) || rows != FRAME_LINES (f)
589 || pixelwidth != FRAME_PIXEL_WIDTH (f) || pixelheight != FRAME_PIXEL_HEIGHT (f)))
591 struct x_output *x = f->output_data.x;
592 GtkAllocation all;
594 all.y = mbheight + tbheight;
595 all.x = 0;
597 all.width = pixelwidth;
598 all.height = pixelheight - mbheight - tbheight;
600 gtk_widget_size_allocate (x->edit_widget, &all);
602 change_frame_size (f, rows, columns, 0, 1, 0);
603 SET_FRAME_GARBAGED (f);
604 cancel_mouse_face (f);
609 /* Update our widget size to be COLS/ROWS characters for frame F. */
610 void
611 xg_frame_set_char_size (f, cols, rows)
612 FRAME_PTR f;
613 int cols;
614 int rows;
616 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
617 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
618 int pixelwidth;
620 /* Take into account the size of the scroll bar. Always use the
621 number of columns occupied by the scroll bar here otherwise we
622 might end up with a frame width that is not a multiple of the
623 frame's character width which is bad for vertically split
624 windows. */
625 f->scroll_bar_actual_width
626 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
628 compute_fringe_widths (f, 0);
630 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
631 after calculating that value. */
632 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
634 /* Must resize our top level widget. Font size may have changed,
635 but not rows/cols. */
636 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
637 pixelwidth, pixelheight);
638 xg_resize_widgets (f, pixelwidth, pixelheight);
639 x_wm_set_size_hint (f, 0, 0);
640 SET_FRAME_GARBAGED (f);
641 cancel_mouse_face (f);
644 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
645 Must be done like this, because GtkWidget:s can have "hidden"
646 X Window that aren't accessible.
648 Return 0 if no widget match WDESC. */
649 GtkWidget *
650 xg_win_to_widget (dpy, wdesc)
651 Display *dpy;
652 Window wdesc;
654 gpointer gdkwin;
655 GtkWidget *gwdesc = 0;
657 BLOCK_INPUT;
659 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
660 wdesc);
661 if (gdkwin)
663 GdkEvent event;
664 event.any.window = gdkwin;
665 gwdesc = gtk_get_event_widget (&event);
668 UNBLOCK_INPUT;
669 return gwdesc;
672 /* Fill in the GdkColor C so that it represents PIXEL.
673 W is the widget that color will be used for. Used to find colormap. */
674 static void
675 xg_pix_to_gcolor (w, pixel, c)
676 GtkWidget *w;
677 unsigned long pixel;
678 GdkColor *c;
680 GdkColormap *map = gtk_widget_get_colormap (w);
681 gdk_colormap_query_color (map, pixel, c);
684 /* Turning off double buffering for our GtkFixed widget has the side
685 effect of turning it off also for its children (scroll bars).
686 But we want those to be double buffered to not flicker so handle
687 expose manually here.
688 WIDGET is the GtkFixed widget that gets exposed.
689 EVENT is the expose event.
690 USER_DATA is unused.
692 Return TRUE to tell GTK that this expose event has been fully handeled
693 and that GTK shall do nothing more with it. */
694 static gboolean
695 xg_fixed_handle_expose (GtkWidget *widget,
696 GdkEventExpose *event,
697 gpointer user_data)
699 GList *iter;
701 for (iter = GTK_FIXED (widget)->children; iter; iter = g_list_next (iter))
703 GtkFixedChild *child_data = (GtkFixedChild *) iter->data;
704 GtkWidget *child = child_data->widget;
705 GdkWindow *window = child->window;
706 GdkRegion *region = gtk_widget_region_intersect (child, event->region);
708 if (! gdk_region_empty (region))
710 GdkEvent child_event;
711 child_event.expose = *event;
712 child_event.expose.region = region;
714 /* Turn on double buffering, i.e. draw to an off screen area. */
715 gdk_window_begin_paint_region (window, region);
717 /* Tell child to redraw itself. */
718 gdk_region_get_clipbox (region, &child_event.expose.area);
719 gtk_widget_send_expose (child, &child_event);
720 gdk_window_process_updates (window, TRUE);
722 /* Copy off screen area to the window. */
723 gdk_window_end_paint (window);
726 gdk_region_destroy (region);
729 return TRUE;
732 /* Create and set up the GTK widgets for frame F.
733 Return 0 if creation failed, non-zero otherwise. */
735 xg_create_frame_widgets (f)
736 FRAME_PTR f;
738 GtkWidget *wtop;
739 GtkWidget *wvbox;
740 GtkWidget *wfixed;
741 GdkColor bg;
742 GtkRcStyle *style;
743 int i;
744 char *title = 0;
746 BLOCK_INPUT;
748 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
749 xg_set_screen (wtop, f);
751 wvbox = gtk_vbox_new (FALSE, 0);
752 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
754 if (! wtop || ! wvbox || ! wfixed)
756 if (wtop) gtk_widget_destroy (wtop);
757 if (wvbox) gtk_widget_destroy (wvbox);
758 if (wfixed) gtk_widget_destroy (wfixed);
760 return 0;
763 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
764 gtk_widget_set_name (wtop, EMACS_CLASS);
765 gtk_widget_set_name (wvbox, "pane");
766 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
768 /* If this frame has a title or name, set it in the title bar. */
769 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
770 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
772 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
774 FRAME_GTK_OUTER_WIDGET (f) = wtop;
775 FRAME_GTK_WIDGET (f) = wfixed;
776 f->output_data.x->vbox_widget = wvbox;
778 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
780 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
781 FRAME_PIXEL_HEIGHT (f));
783 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
784 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
786 if (FRAME_EXTERNAL_TOOL_BAR (f))
787 update_frame_tool_bar (f);
789 /* The tool bar is created but first there are no items in it.
790 This causes it to be zero height. Later items are added, but then
791 the frame is already mapped, so there is a "jumping" resize.
792 This makes geometry handling difficult, for example -0-0 will end
793 up in the wrong place as tool bar height has not been taken into account.
794 So we cheat a bit by setting a height that is what it will have
795 later on when tool bar items are added. */
796 if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0)
797 FRAME_TOOLBAR_HEIGHT (f) = 34;
800 /* We don't want this widget double buffered, because we draw on it
801 with regular X drawing primitives, so from a GTK/GDK point of
802 view, the widget is totally blank. When an expose comes, this
803 will make the widget blank, and then Emacs redraws it. This flickers
804 a lot, so we turn off double buffering. */
805 gtk_widget_set_double_buffered (wfixed, FALSE);
807 /* Turning off double buffering above has the side effect of turning
808 it off also for its children (scroll bars). But we want those
809 to be double buffered to not flicker so handle expose manually. */
810 g_signal_connect (G_OBJECT (wfixed), "expose-event",
811 G_CALLBACK (xg_fixed_handle_expose), 0);
813 /* GTK documents says use gtk_window_set_resizable. But then a user
814 can't shrink the window from its starting size. */
815 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
816 gtk_window_set_wmclass (GTK_WINDOW (wtop),
817 SDATA (Vx_resource_name),
818 SDATA (Vx_resource_class));
820 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
821 GTK is to destroy the widget. We want Emacs to do that instead. */
822 g_signal_connect (G_OBJECT (wtop), "delete-event",
823 G_CALLBACK (gtk_true), 0);
825 /* Convert our geometry parameters into a geometry string
826 and specify it.
827 GTK will itself handle calculating the real position this way. */
828 xg_set_geometry (f);
830 gtk_widget_add_events (wfixed,
831 GDK_POINTER_MOTION_MASK
832 | GDK_EXPOSURE_MASK
833 | GDK_BUTTON_PRESS_MASK
834 | GDK_BUTTON_RELEASE_MASK
835 | GDK_KEY_PRESS_MASK
836 | GDK_ENTER_NOTIFY_MASK
837 | GDK_LEAVE_NOTIFY_MASK
838 | GDK_FOCUS_CHANGE_MASK
839 | GDK_STRUCTURE_MASK
840 | GDK_VISIBILITY_NOTIFY_MASK);
842 /* Must realize the windows so the X window gets created. It is used
843 by callers of this function. */
844 gtk_widget_realize (wfixed);
845 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
847 /* Since GTK clears its window by filling with the background color,
848 we must keep X and GTK background in sync. */
849 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
850 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
852 /* Also, do not let any background pixmap to be set, this looks very
853 bad as Emacs overwrites the background pixmap with its own idea
854 of background color. */
855 style = gtk_widget_get_modifier_style (wfixed);
857 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
858 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
859 gtk_widget_modify_style (wfixed, style);
861 /* GTK does not set any border, and they look bad with GTK. */
862 f->border_width = 0;
863 f->internal_border_width = 0;
865 UNBLOCK_INPUT;
867 return 1;
870 /* Set the normal size hints for the window manager, for frame F.
871 FLAGS is the flags word to use--or 0 meaning preserve the flags
872 that the window now has.
873 If USER_POSITION is nonzero, we set the User Position
874 flag (this is useful when FLAGS is 0). */
875 void
876 x_wm_set_size_hint (f, flags, user_position)
877 FRAME_PTR f;
878 long flags;
879 int user_position;
881 if (FRAME_GTK_OUTER_WIDGET (f))
883 /* Must use GTK routines here, otherwise GTK resets the size hints
884 to its own defaults. */
885 GdkGeometry size_hints;
886 gint hint_flags = 0;
887 int base_width, base_height;
888 int min_rows = 0, min_cols = 0;
889 int win_gravity = f->win_gravity;
891 if (flags)
893 memset (&size_hints, 0, sizeof (size_hints));
894 f->output_data.x->size_hints = size_hints;
895 f->output_data.x->hint_flags = hint_flags;
897 else
898 flags = f->size_hint_flags;
900 size_hints = f->output_data.x->size_hints;
901 hint_flags = f->output_data.x->hint_flags;
903 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
904 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
905 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
907 hint_flags |= GDK_HINT_BASE_SIZE;
908 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
909 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
910 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
912 check_frame_size (f, &min_rows, &min_cols);
914 size_hints.base_width = base_width;
915 size_hints.base_height = base_height;
916 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
917 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
920 /* These currently have a one to one mapping with the X values, but I
921 don't think we should rely on that. */
922 hint_flags |= GDK_HINT_WIN_GRAVITY;
923 size_hints.win_gravity = 0;
924 if (win_gravity == NorthWestGravity)
925 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
926 else if (win_gravity == NorthGravity)
927 size_hints.win_gravity = GDK_GRAVITY_NORTH;
928 else if (win_gravity == NorthEastGravity)
929 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
930 else if (win_gravity == WestGravity)
931 size_hints.win_gravity = GDK_GRAVITY_WEST;
932 else if (win_gravity == CenterGravity)
933 size_hints.win_gravity = GDK_GRAVITY_CENTER;
934 else if (win_gravity == EastGravity)
935 size_hints.win_gravity = GDK_GRAVITY_EAST;
936 else if (win_gravity == SouthWestGravity)
937 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
938 else if (win_gravity == SouthGravity)
939 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
940 else if (win_gravity == SouthEastGravity)
941 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
942 else if (win_gravity == StaticGravity)
943 size_hints.win_gravity = GDK_GRAVITY_STATIC;
945 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
946 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
947 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
949 if (user_position)
951 hint_flags &= ~GDK_HINT_POS;
952 hint_flags |= GDK_HINT_USER_POS;
955 BLOCK_INPUT;
957 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
958 FRAME_GTK_OUTER_WIDGET (f),
959 &size_hints,
960 hint_flags);
962 f->output_data.x->size_hints = size_hints;
963 f->output_data.x->hint_flags = hint_flags;
964 UNBLOCK_INPUT;
968 /* Change background color of a frame.
969 Since GTK uses the background colour to clear the window, we must
970 keep the GTK and X colors in sync.
971 F is the frame to change,
972 BG is the pixel value to change to. */
973 void
974 xg_set_background_color (f, bg)
975 FRAME_PTR f;
976 unsigned long bg;
978 if (FRAME_GTK_WIDGET (f))
980 GdkColor gdk_bg;
982 BLOCK_INPUT;
983 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
984 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
985 UNBLOCK_INPUT;
991 /***********************************************************************
992 Dialog functions
993 ***********************************************************************/
994 /* Return the dialog title to use for a dialog of type KEY.
995 This is the encoding used by lwlib. We use the same for GTK. */
996 static char *
997 get_dialog_title (char key)
999 char *title = "";
1001 switch (key) {
1002 case 'E': case 'e':
1003 title = "Error";
1004 break;
1006 case 'I': case 'i':
1007 title = "Information";
1008 break;
1010 case 'L': case 'l':
1011 title = "Prompt";
1012 break;
1014 case 'P': case 'p':
1015 title = "Prompt";
1016 break;
1018 case 'Q': case 'q':
1019 title = "Question";
1020 break;
1023 return title;
1026 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1027 the dialog, but return TRUE so the event does not propagate further
1028 in GTK. This prevents GTK from destroying the dialog widget automatically
1029 and we can always destrou the widget manually, regardles of how
1030 it was popped down (button press or WM_DELETE_WINDOW).
1031 W is the dialog widget.
1032 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1033 user_data is NULL (not used).
1035 Returns TRUE to end propagation of event. */
1036 static gboolean
1037 dialog_delete_callback (w, event, user_data)
1038 GtkWidget *w;
1039 GdkEvent *event;
1040 gpointer user_data;
1042 gtk_widget_unmap (w);
1043 return TRUE;
1046 /* Create a popup dialog window. See also xg_create_widget below.
1047 WV is a widget_value describing the dialog.
1048 SELECT_CB is the callback to use when a button has been pressed.
1049 DEACTIVATE_CB is the callback to use when the dialog pops down.
1051 Returns the GTK dialog widget. */
1052 static GtkWidget *
1053 create_dialog (wv, select_cb, deactivate_cb)
1054 widget_value *wv;
1055 GCallback select_cb;
1056 GCallback deactivate_cb;
1058 char *title = get_dialog_title (wv->name[0]);
1059 int total_buttons = wv->name[1] - '0';
1060 int right_buttons = wv->name[4] - '0';
1061 int left_buttons;
1062 int button_nr = 0;
1063 int button_spacing = 10;
1064 GtkWidget *wdialog = gtk_dialog_new ();
1065 widget_value *item;
1066 GtkBox *cur_box;
1067 GtkWidget *wvbox;
1068 GtkWidget *whbox_up;
1069 GtkWidget *whbox_down;
1071 /* If the number of buttons is greater than 4, make two rows of buttons
1072 instead. This looks better. */
1073 int make_two_rows = total_buttons > 4;
1075 if (right_buttons == 0) right_buttons = total_buttons/2;
1076 left_buttons = total_buttons - right_buttons;
1078 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1079 gtk_widget_set_name (wdialog, "emacs-dialog");
1081 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1083 if (make_two_rows)
1085 wvbox = gtk_vbox_new (TRUE, button_spacing);
1086 whbox_up = gtk_hbox_new (FALSE, 0);
1087 whbox_down = gtk_hbox_new (FALSE, 0);
1089 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1090 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1091 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1093 cur_box = GTK_BOX (whbox_up);
1096 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1097 G_CALLBACK (dialog_delete_callback), 0);
1099 if (deactivate_cb)
1101 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1102 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1105 for (item = wv->contents; item; item = item->next)
1107 char *utf8_label = get_utf8_string (item->value);
1108 GtkWidget *w;
1109 GtkRequisition req;
1111 if (item->name && strcmp (item->name, "message") == 0)
1113 /* This is the text part of the dialog. */
1114 w = gtk_label_new (utf8_label);
1115 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1116 gtk_label_new (""),
1117 FALSE, FALSE, 0);
1118 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1119 TRUE, TRUE, 0);
1120 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1122 /* Try to make dialog look better. Must realize first so
1123 the widget can calculate the size it needs. */
1124 gtk_widget_realize (w);
1125 gtk_widget_size_request (w, &req);
1126 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1127 req.height);
1128 if (item->value && strlen (item->value) > 0)
1129 button_spacing = 2*req.width/strlen (item->value);
1131 else
1133 /* This is one button to add to the dialog. */
1134 w = gtk_button_new_with_label (utf8_label);
1135 if (! item->enabled)
1136 gtk_widget_set_sensitive (w, FALSE);
1137 if (select_cb)
1138 g_signal_connect (G_OBJECT (w), "clicked",
1139 select_cb, item->call_data);
1141 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1142 if (++button_nr == left_buttons)
1144 if (make_two_rows)
1145 cur_box = GTK_BOX (whbox_down);
1146 else
1147 gtk_box_pack_start (cur_box,
1148 gtk_label_new (""),
1149 TRUE, TRUE,
1150 button_spacing);
1154 if (utf8_label && utf8_label != item->value)
1155 g_free (utf8_label);
1158 return wdialog;
1162 enum
1164 XG_FILE_NOT_DONE,
1165 XG_FILE_OK,
1166 XG_FILE_CANCEL,
1167 XG_FILE_DESTROYED,
1170 /* Callback function invoked when the Ok button is pressed in
1171 a file dialog.
1172 W is the file dialog widget,
1173 ARG points to an integer where we record what has happend. */
1174 static void
1175 xg_file_sel_ok (w, arg)
1176 GtkWidget *w;
1177 gpointer arg;
1179 *(int*)arg = XG_FILE_OK;
1182 /* Callback function invoked when the Cancel button is pressed in
1183 a file dialog.
1184 W is the file dialog widget,
1185 ARG points to an integer where we record what has happend. */
1186 static void
1187 xg_file_sel_cancel (w, arg)
1188 GtkWidget *w;
1189 gpointer arg;
1191 *(int*)arg = XG_FILE_CANCEL;
1194 /* Callback function invoked when the file dialog is destroyed (i.e.
1195 popped down). We must keep track of this, because if this
1196 happens, GTK destroys the widget. But if for example, Ok is pressed,
1197 the dialog is popped down, but the dialog widget is not destroyed.
1198 W is the file dialog widget,
1199 ARG points to an integer where we record what has happend. */
1200 static void
1201 xg_file_sel_destroy (w, arg)
1202 GtkWidget *w;
1203 gpointer arg;
1205 *(int*)arg = XG_FILE_DESTROYED;
1208 /* Read a file name from the user using a file dialog.
1209 F is the current frame.
1210 PROMPT is a prompt to show to the user. May not be NULL.
1211 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1212 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1213 file.
1215 Returns a file name or NULL if no file was selected.
1216 The returned string must be freed by the caller. */
1217 char *
1218 xg_get_file_name (f, prompt, default_filename, mustmatch_p)
1219 FRAME_PTR f;
1220 char *prompt;
1221 char *default_filename;
1222 int mustmatch_p;
1224 GtkWidget *filewin;
1225 GtkFileSelection *filesel;
1226 int filesel_done = XG_FILE_NOT_DONE;
1227 char *fn = 0;
1229 filewin = gtk_file_selection_new (prompt);
1230 filesel = GTK_FILE_SELECTION (filewin);
1232 xg_set_screen (filewin, f);
1234 gtk_widget_set_name (filewin, "emacs-filedialog");
1236 gtk_window_set_transient_for (GTK_WINDOW (filewin),
1237 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1238 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
1240 g_signal_connect (G_OBJECT (filesel->ok_button),
1241 "clicked",
1242 G_CALLBACK (xg_file_sel_ok),
1243 &filesel_done);
1244 g_signal_connect (G_OBJECT (filesel->cancel_button),
1245 "clicked",
1246 G_CALLBACK (xg_file_sel_cancel),
1247 &filesel_done);
1248 g_signal_connect (G_OBJECT (filesel),
1249 "destroy",
1250 G_CALLBACK (xg_file_sel_destroy),
1251 &filesel_done);
1253 if (default_filename)
1254 gtk_file_selection_set_filename (filesel, default_filename);
1256 if (mustmatch_p)
1258 /* The selection_entry part of filesel is not documented. */
1259 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1260 gtk_file_selection_hide_fileop_buttons (filesel);
1264 gtk_widget_show_all (filewin);
1266 while (filesel_done == XG_FILE_NOT_DONE)
1267 gtk_main_iteration ();
1269 if (filesel_done == XG_FILE_OK)
1270 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1272 if (filesel_done != XG_FILE_DESTROYED)
1273 gtk_widget_destroy (filewin);
1275 return fn;
1279 /***********************************************************************
1280 Menu functions.
1281 ***********************************************************************/
1283 /* The name of menu items that can be used for citomization. Since GTK
1284 RC files are very crude and primitive, we have to set this on all
1285 menu item names so a user can easily cutomize menu items. */
1287 #define MENU_ITEM_NAME "emacs-menuitem"
1290 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1291 during GC. The next member points to the items. */
1292 static xg_list_node xg_menu_cb_list;
1294 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1295 during GC. The next member points to the items. */
1296 static xg_list_node xg_menu_item_cb_list;
1298 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1299 F is the frame CL_DATA will be initialized for.
1300 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1302 The menu bar and all sub menus under the menu bar in a frame
1303 share the same structure, hence the reference count.
1305 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1306 allocated xg_menu_cb_data if CL_DATA is NULL. */
1307 static xg_menu_cb_data *
1308 make_cl_data (cl_data, f, highlight_cb)
1309 xg_menu_cb_data *cl_data;
1310 FRAME_PTR f;
1311 GCallback highlight_cb;
1313 if (! cl_data)
1315 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1316 cl_data->f = f;
1317 cl_data->menu_bar_vector = f->menu_bar_vector;
1318 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1319 cl_data->highlight_cb = highlight_cb;
1320 cl_data->ref_count = 0;
1322 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1325 cl_data->ref_count++;
1327 return cl_data;
1330 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1331 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1333 When the menu bar is updated, menu items may have been added and/or
1334 removed, so menu_bar_vector and menu_bar_items_used change. We must
1335 then update CL_DATA since it is used to determine which menu
1336 item that is invoked in the menu.
1337 HIGHLIGHT_CB could change, there is no check that the same
1338 function is given when modifying a menu bar as was given when
1339 creating the menu bar. */
1340 static void
1341 update_cl_data (cl_data, f, highlight_cb)
1342 xg_menu_cb_data *cl_data;
1343 FRAME_PTR f;
1344 GCallback highlight_cb;
1346 if (cl_data)
1348 cl_data->f = f;
1349 cl_data->menu_bar_vector = f->menu_bar_vector;
1350 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1351 cl_data->highlight_cb = highlight_cb;
1355 /* Decrease reference count for CL_DATA.
1356 If reference count is zero, free CL_DATA. */
1357 static void
1358 unref_cl_data (cl_data)
1359 xg_menu_cb_data *cl_data;
1361 if (cl_data && cl_data->ref_count > 0)
1363 cl_data->ref_count--;
1364 if (cl_data->ref_count == 0)
1366 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1367 xfree (cl_data);
1372 /* Function that marks all lisp data during GC. */
1373 void
1374 xg_mark_data ()
1376 xg_list_node *iter;
1378 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1379 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1381 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1383 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1385 if (! NILP (cb_data->help))
1386 mark_object (cb_data->help);
1391 /* Callback called when a menu item is destroyed. Used to free data.
1392 W is the widget that is being destroyed (not used).
1393 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1394 static void
1395 menuitem_destroy_callback (w, client_data)
1396 GtkWidget *w;
1397 gpointer client_data;
1399 if (client_data)
1401 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1402 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1403 xfree (data);
1407 /* Callback called when the pointer enters/leaves a menu item.
1408 W is the menu item.
1409 EVENT is either an enter event or leave event.
1410 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1412 Returns FALSE to tell GTK to keep processing this event. */
1413 static gboolean
1414 menuitem_highlight_callback (w, event, client_data)
1415 GtkWidget *w;
1416 GdkEventCrossing *event;
1417 gpointer client_data;
1419 if (client_data)
1421 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1422 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1424 if (! NILP (data->help) && data->cl_data->highlight_cb)
1426 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1427 (*func) (w, call_data);
1431 return FALSE;
1434 /* Callback called when a menu is destroyed. Used to free data.
1435 W is the widget that is being destroyed (not used).
1436 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1437 static void
1438 menu_destroy_callback (w, client_data)
1439 GtkWidget *w;
1440 gpointer client_data;
1442 unref_cl_data ((xg_menu_cb_data*) client_data);
1445 /* Callback called when a menu does a grab or ungrab. That means the
1446 menu has been activated or deactivated.
1447 Used to start a timer so the small timeout the menus in GTK uses before
1448 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1449 W is the widget that does the grab (not used).
1450 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1451 CLIENT_DATA is NULL (not used). */
1452 static void
1453 menu_grab_callback (GtkWidget *widget,
1454 gboolean ungrab_p,
1455 gpointer client_data)
1457 /* Keep track of total number of grabs. */
1458 static int cnt;
1460 if (ungrab_p) cnt--;
1461 else cnt++;
1463 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1464 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1467 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1468 must be non-NULL) and can be inserted into a menu item.
1470 Returns the GtkHBox. */
1471 static GtkWidget *
1472 make_widget_for_menu_item (utf8_label, utf8_key)
1473 char *utf8_label;
1474 char *utf8_key;
1476 GtkWidget *wlbl;
1477 GtkWidget *wkey;
1478 GtkWidget *wbox;
1480 wbox = gtk_hbox_new (FALSE, 0);
1481 wlbl = gtk_label_new (utf8_label);
1482 wkey = gtk_label_new (utf8_key);
1484 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1485 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1487 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1488 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1490 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1491 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1492 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1494 return wbox;
1497 /* Make and return a menu item widget with the key to the right.
1498 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1499 UTF8_KEY is the text representing the key binding.
1500 ITEM is the widget_value describing the menu item.
1502 GROUP is an in/out parameter. If the menu item to be created is not
1503 part of any radio menu group, *GROUP contains NULL on entry and exit.
1504 If the menu item to be created is part of a radio menu group, on entry
1505 *GROUP contains the group to use, or NULL if this is the first item
1506 in the group. On exit, *GROUP contains the radio item group.
1508 Unfortunately, keys don't line up as nicely as in Motif,
1509 but the MacOS X version doesn't either, so I guess that is OK. */
1510 static GtkWidget *
1511 make_menu_item (utf8_label, utf8_key, item, group)
1512 char *utf8_label;
1513 char *utf8_key;
1514 widget_value *item;
1515 GSList **group;
1517 GtkWidget *w;
1518 GtkWidget *wtoadd = 0;
1520 /* It has been observed that some menu items have a NULL name field.
1521 This will lead to this function being called with a NULL utf8_label.
1522 GTK crashes on that so we set a blank label. Why there is a NULL
1523 name remains to be investigated. */
1524 if (! utf8_label) utf8_label = " ";
1526 if (utf8_key)
1527 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1529 if (item->button_type == BUTTON_TYPE_TOGGLE)
1531 *group = NULL;
1532 if (utf8_key) w = gtk_check_menu_item_new ();
1533 else w = gtk_check_menu_item_new_with_label (utf8_label);
1534 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1536 else if (item->button_type == BUTTON_TYPE_RADIO)
1538 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1539 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1540 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1541 if (item->selected)
1542 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1544 else
1546 *group = NULL;
1547 if (utf8_key) w = gtk_menu_item_new ();
1548 else w = gtk_menu_item_new_with_label (utf8_label);
1551 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1552 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1554 return w;
1557 /* Return non-zero if LABEL specifies a separator (GTK only has one
1558 separator type) */
1559 static int
1560 xg_separator_p (char *label)
1562 if (! label) return 0;
1563 else if (strlen (label) > 3
1564 && strncmp (label, "--", 2) == 0
1565 && label[2] != '-')
1567 static char* separator_names[] = {
1568 "space",
1569 "no-line",
1570 "single-line",
1571 "double-line",
1572 "single-dashed-line",
1573 "double-dashed-line",
1574 "shadow-etched-in",
1575 "shadow-etched-out",
1576 "shadow-etched-in-dash",
1577 "shadow-etched-out-dash",
1578 "shadow-double-etched-in",
1579 "shadow-double-etched-out",
1580 "shadow-double-etched-in-dash",
1581 "shadow-double-etched-out-dash",
1585 int i;
1587 label += 2;
1588 for (i = 0; separator_names[i]; ++i)
1589 if (strcmp (label, separator_names[i]) == 0)
1590 return 1;
1592 else
1594 /* Old-style separator, maybe. It's a separator if it contains
1595 only dashes. */
1596 while (*label == '-')
1597 ++label;
1598 if (*label == 0) return 1;
1601 return 0;
1604 static int xg_detached_menus;
1606 /* Returns non-zero if there are detached menus. */
1608 xg_have_tear_offs ()
1610 return xg_detached_menus > 0;
1613 /* Callback invoked when a detached menu window is removed. Here we
1614 decrease the xg_detached_menus count.
1615 WIDGET is the top level window that is removed (the parent of the menu).
1616 CLIENT_DATA is not used. */
1617 static void
1618 tearoff_remove (widget, client_data)
1619 GtkWidget *widget;
1620 gpointer client_data;
1622 if (xg_detached_menus > 0) --xg_detached_menus;
1625 /* Callback invoked when a menu is detached. It increases the
1626 xg_detached_menus count.
1627 WIDGET is the GtkTearoffMenuItem.
1628 CLIENT_DATA is not used. */
1629 static void
1630 tearoff_activate (widget, client_data)
1631 GtkWidget *widget;
1632 gpointer client_data;
1634 GtkWidget *menu = gtk_widget_get_parent (widget);
1635 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1637 ++xg_detached_menus;
1638 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1639 "destroy",
1640 G_CALLBACK (tearoff_remove), 0);
1645 /* Create a menu item widget, and connect the callbacks.
1646 ITEM decribes the menu item.
1647 F is the frame the created menu belongs to.
1648 SELECT_CB is the callback to use when a menu item is selected.
1649 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1650 CL_DATA points to the callback data to be used for this menu.
1651 GROUP is an in/out parameter. If the menu item to be created is not
1652 part of any radio menu group, *GROUP contains NULL on entry and exit.
1653 If the menu item to be created is part of a radio menu group, on entry
1654 *GROUP contains the group to use, or NULL if this is the first item
1655 in the group. On exit, *GROUP contains the radio item group.
1657 Returns the created GtkWidget. */
1658 static GtkWidget *
1659 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1660 widget_value *item;
1661 FRAME_PTR f;
1662 GCallback select_cb;
1663 GCallback highlight_cb;
1664 xg_menu_cb_data *cl_data;
1665 GSList **group;
1667 char *utf8_label;
1668 char *utf8_key;
1669 GtkWidget *w;
1670 xg_menu_item_cb_data *cb_data;
1672 utf8_label = get_utf8_string (item->name);
1673 utf8_key = get_utf8_string (item->key);
1675 w = make_menu_item (utf8_label, utf8_key, item, group);
1677 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1678 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1680 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1682 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1684 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1685 cb_data->help = item->help;
1686 cb_data->cl_data = cl_data;
1687 cb_data->call_data = item->call_data;
1689 g_signal_connect (G_OBJECT (w),
1690 "destroy",
1691 G_CALLBACK (menuitem_destroy_callback),
1692 cb_data);
1694 /* Put cb_data in widget, so we can get at it when modifying menubar */
1695 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1697 /* final item, not a submenu */
1698 if (item->call_data && ! item->contents)
1700 if (select_cb)
1701 cb_data->select_id
1702 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1705 if (! NILP (item->help) && highlight_cb)
1707 /* We use enter/leave notify instead of select/deselect because
1708 select/deselect doesn't go well with detached menus. */
1709 cb_data->highlight_id
1710 = g_signal_connect (G_OBJECT (w),
1711 "enter-notify-event",
1712 G_CALLBACK (menuitem_highlight_callback),
1713 cb_data);
1714 cb_data->unhighlight_id
1715 = g_signal_connect (G_OBJECT (w),
1716 "leave-notify-event",
1717 G_CALLBACK (menuitem_highlight_callback),
1718 cb_data);
1721 return w;
1724 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1725 GCallback, GCallback, int, int, int,
1726 GtkWidget *, xg_menu_cb_data *, char *));
1728 /* Create a full menu tree specified by DATA.
1729 F is the frame the created menu belongs to.
1730 SELECT_CB is the callback to use when a menu item is selected.
1731 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1732 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1733 POP_UP_P is non-zero if we shall create a popup menu.
1734 MENU_BAR_P is non-zero if we shall create a menu bar.
1735 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1736 if MENU_BAR_P is non-zero.
1737 TOPMENU is the topmost GtkWidget that others shall be placed under.
1738 It may be NULL, in that case we create the appropriate widget
1739 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1740 CL_DATA is the callback data we shall use for this menu, or NULL
1741 if we haven't set the first callback yet.
1742 NAME is the name to give to the top level menu if this function
1743 creates it. May be NULL to not set any name.
1745 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1746 not NULL.
1748 This function calls itself to create submenus. */
1750 static GtkWidget *
1751 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1752 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1753 widget_value *data;
1754 FRAME_PTR f;
1755 GCallback select_cb;
1756 GCallback deactivate_cb;
1757 GCallback highlight_cb;
1758 int pop_up_p;
1759 int menu_bar_p;
1760 int add_tearoff_p;
1761 GtkWidget *topmenu;
1762 xg_menu_cb_data *cl_data;
1763 char *name;
1765 widget_value *item;
1766 GtkWidget *wmenu = topmenu;
1767 GSList *group = NULL;
1769 if (! topmenu)
1771 if (! menu_bar_p)
1773 wmenu = gtk_menu_new ();
1774 xg_set_screen (wmenu, f);
1776 else wmenu = gtk_menu_bar_new ();
1778 /* Put cl_data on the top menu for easier access. */
1779 cl_data = make_cl_data (cl_data, f, highlight_cb);
1780 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1781 g_signal_connect (G_OBJECT (wmenu), "destroy",
1782 G_CALLBACK (menu_destroy_callback), cl_data);
1784 if (name)
1785 gtk_widget_set_name (wmenu, name);
1787 if (deactivate_cb)
1788 g_signal_connect (G_OBJECT (wmenu),
1789 "deactivate", deactivate_cb, 0);
1791 g_signal_connect (G_OBJECT (wmenu),
1792 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1795 if (! menu_bar_p && add_tearoff_p)
1797 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1798 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1800 g_signal_connect (G_OBJECT (tearoff), "activate",
1801 G_CALLBACK (tearoff_activate), 0);
1804 for (item = data; item; item = item->next)
1806 GtkWidget *w;
1808 if (pop_up_p && !item->contents && !item->call_data
1809 && !xg_separator_p (item->name))
1811 char *utf8_label;
1812 /* A title for a popup. We do the same as GTK does when
1813 creating titles, but it does not look good. */
1814 group = NULL;
1815 utf8_label = get_utf8_string (item->name);
1817 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1818 w = gtk_menu_item_new_with_label (utf8_label);
1819 gtk_widget_set_sensitive (w, FALSE);
1820 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1822 else if (xg_separator_p (item->name))
1824 group = NULL;
1825 /* GTK only have one separator type. */
1826 w = gtk_separator_menu_item_new ();
1828 else
1830 w = xg_create_one_menuitem (item,
1832 item->contents ? 0 : select_cb,
1833 highlight_cb,
1834 cl_data,
1835 &group);
1837 if (item->contents)
1839 GtkWidget *submenu = create_menus (item->contents,
1841 select_cb,
1842 deactivate_cb,
1843 highlight_cb,
1846 add_tearoff_p,
1848 cl_data,
1850 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1854 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1855 gtk_widget_set_name (w, MENU_ITEM_NAME);
1858 return wmenu;
1861 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1862 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1863 with some text and buttons.
1864 F is the frame the created item belongs to.
1865 NAME is the name to use for the top widget.
1866 VAL is a widget_value structure describing items to be created.
1867 SELECT_CB is the callback to use when a menu item is selected or
1868 a dialog button is pressed.
1869 DEACTIVATE_CB is the callback to use when an item is deactivated.
1870 For a menu, when a sub menu is not shown anymore, for a dialog it is
1871 called when the dialog is popped down.
1872 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1874 Returns the widget created. */
1875 GtkWidget *
1876 xg_create_widget (type, name, f, val,
1877 select_cb, deactivate_cb, highlight_cb)
1878 char *type;
1879 char *name;
1880 FRAME_PTR f;
1881 widget_value *val;
1882 GCallback select_cb;
1883 GCallback deactivate_cb;
1884 GCallback highlight_cb;
1886 GtkWidget *w = 0;
1887 int menu_bar_p = strcmp (type, "menubar") == 0;
1888 int pop_up_p = strcmp (type, "popup") == 0;
1890 if (strcmp (type, "dialog") == 0)
1892 w = create_dialog (val, select_cb, deactivate_cb);
1893 xg_set_screen (w, f);
1894 gtk_window_set_transient_for (GTK_WINDOW (w),
1895 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1896 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1897 gtk_widget_set_name (w, "emacs-dialog");
1899 else if (menu_bar_p || pop_up_p)
1901 w = create_menus (val->contents,
1903 select_cb,
1904 deactivate_cb,
1905 highlight_cb,
1906 pop_up_p,
1907 menu_bar_p,
1908 menu_bar_p,
1911 name);
1913 /* Set the cursor to an arrow for popup menus when they are mapped.
1914 This is done by default for menu bar menus. */
1915 if (pop_up_p)
1917 /* Must realize so the GdkWindow inside the widget is created. */
1918 gtk_widget_realize (w);
1919 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
1922 else
1924 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1925 type);
1928 return w;
1931 /* Return the label for menu item WITEM. */
1932 static const char *
1933 xg_get_menu_item_label (witem)
1934 GtkMenuItem *witem;
1936 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1937 return gtk_label_get_label (wlabel);
1940 /* Return non-zero if the menu item WITEM has the text LABEL. */
1941 static int
1942 xg_item_label_same_p (witem, label)
1943 GtkMenuItem *witem;
1944 char *label;
1946 int is_same = 0;
1947 char *utf8_label = get_utf8_string (label);
1948 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1950 if (! old_label && ! utf8_label)
1951 is_same = 1;
1952 else if (old_label && utf8_label)
1953 is_same = strcmp (utf8_label, old_label) == 0;
1955 if (utf8_label && utf8_label != label) g_free (utf8_label);
1957 return is_same;
1960 /* Remove widgets in LIST from container WCONT. */
1961 static void
1962 remove_from_container (wcont, list)
1963 GtkWidget *wcont;
1964 GList *list;
1966 GList *iter;
1968 for (iter = list; iter; iter = g_list_next (iter))
1970 GtkWidget *w = GTK_WIDGET (iter->data);
1972 /* Add a ref to w so we can explicitly destroy it later. */
1973 gtk_widget_ref (w);
1974 gtk_container_remove (GTK_CONTAINER (wcont), w);
1976 /* If there is a menu under this widget that has been detached,
1977 there is a reference to it, and just removing w from the
1978 container does not destroy the submenu. By explicitly
1979 destroying w we make sure the submenu is destroyed, thus
1980 removing the detached window also if there was one. */
1981 gtk_widget_destroy (w);
1985 /* Update the top level names in MENUBAR (i.e. not submenus).
1986 F is the frame the menu bar belongs to.
1987 *LIST is a list with the current menu bar names (menu item widgets).
1988 ITER is the item within *LIST that shall be updated.
1989 POS is the numerical position, starting at 0, of ITER in *LIST.
1990 VAL describes what the menu bar shall look like after the update.
1991 SELECT_CB is the callback to use when a menu item is selected.
1992 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1993 CL_DATA points to the callback data to be used for this menu bar.
1995 This function calls itself to walk through the menu bar names. */
1996 static void
1997 xg_update_menubar (menubar, f, list, iter, pos, val,
1998 select_cb, highlight_cb, cl_data)
1999 GtkWidget *menubar;
2000 FRAME_PTR f;
2001 GList **list;
2002 GList *iter;
2003 int pos;
2004 widget_value *val;
2005 GCallback select_cb;
2006 GCallback highlight_cb;
2007 xg_menu_cb_data *cl_data;
2009 if (! iter && ! val)
2010 return;
2011 else if (iter && ! val)
2013 /* Item(s) have been removed. Remove all remaining items. */
2014 remove_from_container (menubar, iter);
2016 /* All updated. */
2017 val = 0;
2018 iter = 0;
2020 else if (! iter && val)
2022 /* Item(s) added. Add all new items in one call. */
2023 create_menus (val, f, select_cb, 0, highlight_cb,
2024 0, 1, 0, menubar, cl_data, 0);
2026 /* All updated. */
2027 val = 0;
2028 iter = 0;
2030 /* Below this neither iter or val is NULL */
2031 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2033 /* This item is still the same, check next item. */
2034 val = val->next;
2035 iter = g_list_next (iter);
2036 ++pos;
2038 else /* This item is changed. */
2040 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2041 GtkMenuItem *witem2 = 0;
2042 int val_in_menubar = 0;
2043 int iter_in_new_menubar = 0;
2044 GList *iter2;
2045 widget_value *cur;
2047 /* See if the changed entry (val) is present later in the menu bar */
2048 for (iter2 = iter;
2049 iter2 && ! val_in_menubar;
2050 iter2 = g_list_next (iter2))
2052 witem2 = GTK_MENU_ITEM (iter2->data);
2053 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2056 /* See if the current entry (iter) is present later in the
2057 specification for the new menu bar. */
2058 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2059 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2061 if (val_in_menubar && ! iter_in_new_menubar)
2063 int nr = pos;
2065 /* This corresponds to:
2066 Current: A B C
2067 New: A C
2068 Remove B. */
2070 gtk_widget_ref (GTK_WIDGET (witem));
2071 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2072 gtk_widget_destroy (GTK_WIDGET (witem));
2074 /* Must get new list since the old changed. */
2075 g_list_free (*list);
2076 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2077 while (nr-- > 0) iter = g_list_next (iter);
2079 else if (! val_in_menubar && ! iter_in_new_menubar)
2081 /* This corresponds to:
2082 Current: A B C
2083 New: A X C
2084 Rename B to X. This might seem to be a strange thing to do,
2085 since if there is a menu under B it will be totally wrong for X.
2086 But consider editing a C file. Then there is a C-mode menu
2087 (corresponds to B above).
2088 If then doing C-x C-f the minibuf menu (X above) replaces the
2089 C-mode menu. When returning from the minibuffer, we get
2090 back the C-mode menu. Thus we do:
2091 Rename B to X (C-mode to minibuf menu)
2092 Rename X to B (minibuf to C-mode menu).
2093 If the X menu hasn't been invoked, the menu under B
2094 is up to date when leaving the minibuffer. */
2095 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2096 char *utf8_label = get_utf8_string (val->name);
2097 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2099 gtk_label_set_text (wlabel, utf8_label);
2101 /* If this item has a submenu that has been detached, change
2102 the title in the WM decorations also. */
2103 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2104 /* Set the title of the detached window. */
2105 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2107 iter = g_list_next (iter);
2108 val = val->next;
2109 ++pos;
2111 else if (! val_in_menubar && iter_in_new_menubar)
2113 /* This corresponds to:
2114 Current: A B C
2115 New: A X B C
2116 Insert X. */
2118 int nr = pos;
2119 GList *group = 0;
2120 GtkWidget *w = xg_create_one_menuitem (val,
2122 select_cb,
2123 highlight_cb,
2124 cl_data,
2125 &group);
2127 gtk_widget_set_name (w, MENU_ITEM_NAME);
2128 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2130 g_list_free (*list);
2131 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2132 while (nr-- > 0) iter = g_list_next (iter);
2133 iter = g_list_next (iter);
2134 val = val->next;
2135 ++pos;
2137 else /* if (val_in_menubar && iter_in_new_menubar) */
2139 int nr = pos;
2140 /* This corresponds to:
2141 Current: A B C
2142 New: A C B
2143 Move C before B */
2145 gtk_widget_ref (GTK_WIDGET (witem2));
2146 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2147 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2148 GTK_WIDGET (witem2), pos);
2149 gtk_widget_unref (GTK_WIDGET (witem2));
2151 g_list_free (*list);
2152 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2153 while (nr-- > 0) iter = g_list_next (iter);
2154 val = val->next;
2155 ++pos;
2159 /* Update the rest of the menu bar. */
2160 xg_update_menubar (menubar, f, list, iter, pos, val,
2161 select_cb, highlight_cb, cl_data);
2164 /* Update the menu item W so it corresponds to VAL.
2165 SELECT_CB is the callback to use when a menu item is selected.
2166 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2167 CL_DATA is the data to set in the widget for menu invokation. */
2168 static void
2169 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2170 widget_value *val;
2171 GtkWidget *w;
2172 GCallback select_cb;
2173 GCallback highlight_cb;
2174 xg_menu_cb_data *cl_data;
2176 GtkWidget *wchild;
2177 GtkLabel *wlbl = 0;
2178 GtkLabel *wkey = 0;
2179 char *utf8_label;
2180 char *utf8_key;
2181 const char *old_label = 0;
2182 const char *old_key = 0;
2183 xg_menu_item_cb_data *cb_data;
2185 wchild = gtk_bin_get_child (GTK_BIN (w));
2186 utf8_label = get_utf8_string (val->name);
2187 utf8_key = get_utf8_string (val->key);
2189 /* See if W is a menu item with a key. See make_menu_item above. */
2190 if (GTK_IS_HBOX (wchild))
2192 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2194 wlbl = GTK_LABEL (list->data);
2195 wkey = GTK_LABEL (list->next->data);
2196 g_list_free (list);
2198 if (! utf8_key)
2200 /* Remove the key and keep just the label. */
2201 gtk_widget_ref (GTK_WIDGET (wlbl));
2202 gtk_container_remove (GTK_CONTAINER (w), wchild);
2203 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2204 wkey = 0;
2208 else /* Just a label. */
2210 wlbl = GTK_LABEL (wchild);
2212 /* Check if there is now a key. */
2213 if (utf8_key)
2215 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2216 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2218 wlbl = GTK_LABEL (list->data);
2219 wkey = GTK_LABEL (list->next->data);
2220 g_list_free (list);
2222 gtk_container_remove (GTK_CONTAINER (w), wchild);
2223 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2228 if (wkey) old_key = gtk_label_get_label (wkey);
2229 if (wlbl) old_label = gtk_label_get_label (wlbl);
2231 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2232 gtk_label_set_text (wkey, utf8_key);
2234 if (! old_label || strcmp (utf8_label, old_label) != 0)
2235 gtk_label_set_text (wlbl, utf8_label);
2237 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2238 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2240 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2241 gtk_widget_set_sensitive (w, FALSE);
2242 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2243 gtk_widget_set_sensitive (w, TRUE);
2245 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2246 XG_ITEM_DATA);
2247 if (cb_data)
2249 cb_data->call_data = val->call_data;
2250 cb_data->help = val->help;
2251 cb_data->cl_data = cl_data;
2253 /* We assume the callback functions don't change. */
2254 if (val->call_data && ! val->contents)
2256 /* This item shall have a select callback. */
2257 if (! cb_data->select_id)
2258 cb_data->select_id
2259 = g_signal_connect (G_OBJECT (w), "activate",
2260 select_cb, cb_data);
2262 else if (cb_data->select_id)
2264 g_signal_handler_disconnect (w, cb_data->select_id);
2265 cb_data->select_id = 0;
2268 if (NILP (cb_data->help))
2270 /* Shall not have help. Remove if any existed previously. */
2271 if (cb_data->highlight_id)
2273 g_signal_handler_disconnect (G_OBJECT (w),
2274 cb_data->highlight_id);
2275 cb_data->highlight_id = 0;
2277 if (cb_data->unhighlight_id)
2279 g_signal_handler_disconnect (G_OBJECT (w),
2280 cb_data->unhighlight_id);
2281 cb_data->unhighlight_id = 0;
2284 else if (! cb_data->highlight_id && highlight_cb)
2286 /* Have help now, but didn't previously. Add callback. */
2287 cb_data->highlight_id
2288 = g_signal_connect (G_OBJECT (w),
2289 "enter-notify-event",
2290 G_CALLBACK (menuitem_highlight_callback),
2291 cb_data);
2292 cb_data->unhighlight_id
2293 = g_signal_connect (G_OBJECT (w),
2294 "leave-notify-event",
2295 G_CALLBACK (menuitem_highlight_callback),
2296 cb_data);
2301 /* Update the toggle menu item W so it corresponds to VAL. */
2302 static void
2303 xg_update_toggle_item (val, w)
2304 widget_value *val;
2305 GtkWidget *w;
2307 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2310 /* Update the radio menu item W so it corresponds to VAL. */
2311 static void
2312 xg_update_radio_item (val, w)
2313 widget_value *val;
2314 GtkWidget *w;
2316 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2319 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2320 SUBMENU may be NULL, in that case a new menu is created.
2321 F is the frame the menu bar belongs to.
2322 VAL describes the contents of the menu bar.
2323 SELECT_CB is the callback to use when a menu item is selected.
2324 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2325 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2326 CL_DATA is the call back data to use for any newly created items.
2328 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2329 was NULL. */
2331 static GtkWidget *
2332 xg_update_submenu (submenu, f, val,
2333 select_cb, deactivate_cb, highlight_cb, cl_data)
2334 GtkWidget *submenu;
2335 FRAME_PTR f;
2336 widget_value *val;
2337 GCallback select_cb;
2338 GCallback deactivate_cb;
2339 GCallback highlight_cb;
2340 xg_menu_cb_data *cl_data;
2342 GtkWidget *newsub = submenu;
2343 GList *list = 0;
2344 GList *iter;
2345 widget_value *cur;
2346 int has_tearoff_p = 0;
2347 GList *first_radio = 0;
2349 if (submenu)
2350 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2352 for (cur = val, iter = list;
2353 cur && iter;
2354 iter = g_list_next (iter), cur = cur->next)
2356 GtkWidget *w = GTK_WIDGET (iter->data);
2358 /* Skip tearoff items, they have no counterpart in val. */
2359 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2361 has_tearoff_p = 1;
2362 iter = g_list_next (iter);
2363 if (iter) w = GTK_WIDGET (iter->data);
2364 else break;
2367 /* Remember first radio button in a group. If we get a mismatch in
2368 a radio group we must rebuild the whole group so that the connections
2369 in GTK becomes correct. */
2370 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2371 first_radio = iter;
2372 else if (cur->button_type != BUTTON_TYPE_RADIO
2373 && ! GTK_IS_RADIO_MENU_ITEM (w))
2374 first_radio = 0;
2376 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2378 if (! xg_separator_p (cur->name))
2379 break;
2381 else if (GTK_IS_CHECK_MENU_ITEM (w))
2383 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2384 break;
2385 xg_update_toggle_item (cur, w);
2386 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2388 else if (GTK_IS_RADIO_MENU_ITEM (w))
2390 if (cur->button_type != BUTTON_TYPE_RADIO)
2391 break;
2392 xg_update_radio_item (cur, w);
2393 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2395 else if (GTK_IS_MENU_ITEM (w))
2397 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2398 GtkWidget *sub;
2400 if (cur->button_type != BUTTON_TYPE_NONE ||
2401 xg_separator_p (cur->name))
2402 break;
2404 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2406 sub = gtk_menu_item_get_submenu (witem);
2407 if (sub && ! cur->contents)
2409 /* Not a submenu anymore. */
2410 gtk_widget_ref (sub);
2411 gtk_menu_item_remove_submenu (witem);
2412 gtk_widget_destroy (sub);
2414 else if (cur->contents)
2416 GtkWidget *nsub;
2418 nsub = xg_update_submenu (sub, f, cur->contents,
2419 select_cb, deactivate_cb,
2420 highlight_cb, cl_data);
2422 /* If this item just became a submenu, we must set it. */
2423 if (nsub != sub)
2424 gtk_menu_item_set_submenu (witem, nsub);
2427 else
2429 /* Structural difference. Remove everything from here and down
2430 in SUBMENU. */
2431 break;
2435 /* Remove widgets from first structual change. */
2436 if (iter)
2438 /* If we are adding new menu items below, we must remove from
2439 first radio button so that radio groups become correct. */
2440 if (cur && first_radio) remove_from_container (submenu, first_radio);
2441 else remove_from_container (submenu, iter);
2444 if (cur)
2446 /* More items added. Create them. */
2447 newsub = create_menus (cur,
2449 select_cb,
2450 deactivate_cb,
2451 highlight_cb,
2454 ! has_tearoff_p,
2455 submenu,
2456 cl_data,
2460 if (list) g_list_free (list);
2462 return newsub;
2465 /* Update the MENUBAR.
2466 F is the frame the menu bar belongs to.
2467 VAL describes the contents of the menu bar.
2468 If DEEP_P is non-zero, rebuild all but the top level menu names in
2469 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2470 SELECT_CB is the callback to use when a menu item is selected.
2471 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2472 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2473 void
2474 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2475 select_cb, deactivate_cb, highlight_cb)
2476 GtkWidget *menubar;
2477 FRAME_PTR f;
2478 widget_value *val;
2479 int deep_p;
2480 GCallback select_cb;
2481 GCallback deactivate_cb;
2482 GCallback highlight_cb;
2484 xg_menu_cb_data *cl_data;
2485 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2487 if (! list) return;
2489 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2490 XG_FRAME_DATA);
2492 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2493 select_cb, highlight_cb, cl_data);
2495 if (deep_p);
2497 widget_value *cur;
2499 /* Update all sub menus.
2500 We must keep the submenus (GTK menu item widgets) since the
2501 X Window in the XEvent that activates the menu are those widgets. */
2503 /* Update cl_data, menu_item things in F may have changed. */
2504 update_cl_data (cl_data, f, highlight_cb);
2506 for (cur = val->contents; cur; cur = cur->next)
2508 GList *iter;
2509 GtkWidget *sub = 0;
2510 GtkWidget *newsub;
2511 GtkMenuItem *witem;
2513 /* Find sub menu that corresponds to val and update it. */
2514 for (iter = list ; iter; iter = g_list_next (iter))
2516 witem = GTK_MENU_ITEM (iter->data);
2517 if (xg_item_label_same_p (witem, cur->name))
2519 sub = gtk_menu_item_get_submenu (witem);
2520 break;
2524 newsub = xg_update_submenu (sub,
2526 cur->contents,
2527 select_cb,
2528 deactivate_cb,
2529 highlight_cb,
2530 cl_data);
2531 /* sub may still be NULL. If we just updated non deep and added
2532 a new menu bar item, it has no sub menu yet. So we set the
2533 newly created sub menu under witem. */
2534 if (newsub != sub)
2536 xg_set_screen (newsub, f);
2537 gtk_menu_item_set_submenu (witem, newsub);
2542 g_list_free (list);
2543 gtk_widget_show_all (menubar);
2546 /* Recompute all the widgets of frame F, when the menu bar has been
2547 changed. Value is non-zero if widgets were updated. */
2550 xg_update_frame_menubar (f)
2551 FRAME_PTR f;
2553 struct x_output *x = f->output_data.x;
2554 GtkRequisition req;
2556 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2557 return 0;
2559 BLOCK_INPUT;
2561 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2562 FALSE, FALSE, 0);
2563 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2565 gtk_widget_show_all (x->menubar_widget);
2566 gtk_widget_size_request (x->menubar_widget, &req);
2568 FRAME_MENUBAR_HEIGHT (f) = req.height;
2570 /* The height has changed, resize outer widget and set columns
2571 rows to what we had before adding the menu bar. */
2572 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2574 SET_FRAME_GARBAGED (f);
2575 UNBLOCK_INPUT;
2577 return 1;
2580 /* Get rid of the menu bar of frame F, and free its storage.
2581 This is used when deleting a frame, and when turning off the menu bar. */
2583 void
2584 free_frame_menubar (f)
2585 FRAME_PTR f;
2587 struct x_output *x = f->output_data.x;
2589 if (x->menubar_widget)
2591 BLOCK_INPUT;
2593 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2594 /* The menubar and its children shall be deleted when removed from
2595 the container. */
2596 x->menubar_widget = 0;
2597 FRAME_MENUBAR_HEIGHT (f) = 0;
2599 /* The height has changed, resize outer widget and set columns
2600 rows to what we had before removing the menu bar. */
2601 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2603 SET_FRAME_GARBAGED (f);
2604 UNBLOCK_INPUT;
2610 /***********************************************************************
2611 Scroll bar functions
2612 ***********************************************************************/
2615 /* Setting scroll bar values invokes the callback. Use this variable
2616 to indicate that callback should do nothing. */
2617 int xg_ignore_gtk_scrollbar;
2619 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2620 32 bits. But we want to store pointers, and they may be larger
2621 than 32 bits. Keep a mapping from integer index to widget pointers
2622 to get around the 32 bit limitation. */
2623 static struct
2625 GtkWidget **widgets;
2626 int max_size;
2627 int used;
2628 } id_to_widget;
2630 /* Grow this much every time we need to allocate more */
2631 #define ID_TO_WIDGET_INCR 32
2633 /* Store the widget pointer W in id_to_widget and return the integer index. */
2634 static int
2635 xg_store_widget_in_map (w)
2636 GtkWidget *w;
2638 int i;
2640 if (id_to_widget.max_size == id_to_widget.used)
2642 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2644 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2645 sizeof (GtkWidget *)*new_size);
2647 for (i = id_to_widget.max_size; i < new_size; ++i)
2648 id_to_widget.widgets[i] = 0;
2649 id_to_widget.max_size = new_size;
2652 /* Just loop over the array and find a free place. After all,
2653 how many scroll bars are we creating? Should be a small number.
2654 The check above guarantees we will find a free place. */
2655 for (i = 0; i < id_to_widget.max_size; ++i)
2657 if (! id_to_widget.widgets[i])
2659 id_to_widget.widgets[i] = w;
2660 ++id_to_widget.used;
2662 return i;
2666 /* Should never end up here */
2667 abort ();
2670 /* Remove pointer at IDX from id_to_widget.
2671 Called when scroll bar is destroyed. */
2672 static void
2673 xg_remove_widget_from_map (idx)
2674 int idx;
2676 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2678 id_to_widget.widgets[idx] = 0;
2679 --id_to_widget.used;
2683 /* Get the widget pointer at IDX from id_to_widget. */
2684 static GtkWidget *
2685 xg_get_widget_from_map (idx)
2686 int idx;
2688 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2689 return id_to_widget.widgets[idx];
2691 return 0;
2694 /* Return the scrollbar id for X Window WID on display DPY.
2695 Return -1 if WID not in id_to_widget. */
2697 xg_get_scroll_id_for_window (dpy, wid)
2698 Display *dpy;
2699 Window wid;
2701 int idx;
2702 GtkWidget *w;
2704 w = xg_win_to_widget (dpy, wid);
2706 if (w)
2708 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2709 if (id_to_widget.widgets[idx] == w)
2710 return idx;
2713 return -1;
2716 /* Callback invoked when scroll bar WIDGET is destroyed.
2717 DATA is the index into id_to_widget for WIDGET.
2718 We free pointer to last scroll bar values here and remove the index. */
2719 static void
2720 xg_gtk_scroll_destroy (widget, data)
2721 GtkWidget *widget;
2722 gpointer data;
2724 gpointer p;
2725 int id = (int)data;
2727 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2728 if (p) xfree (p);
2729 xg_remove_widget_from_map (id);
2732 /* Callback for button press/release events. Used to start timer so that
2733 the scroll bar repetition timer in GTK gets handeled.
2734 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2735 WIDGET is the scroll bar widget the event is for (not used).
2736 EVENT contains the event.
2737 USER_DATA points to the struct scrollbar structure.
2739 Returns FALSE to tell GTK that it shall continue propagate the event
2740 to widgets. */
2741 static gboolean
2742 scroll_bar_button_cb (widget, event, user_data)
2743 GtkWidget *widget;
2744 GdkEventButton *event;
2745 gpointer user_data;
2747 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2748 xg_start_timer ();
2749 else if (event->type == GDK_BUTTON_RELEASE)
2751 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2752 if (xg_timer) xg_stop_timer ();
2753 bar->dragging = Qnil;
2756 return FALSE;
2759 /* Create a scroll bar widget for frame F. Store the scroll bar
2760 in BAR.
2761 SCROLL_CALLBACK is the callback to invoke when the value of the
2762 bar changes.
2763 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2764 to set resources for the widget. */
2765 void
2766 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2767 FRAME_PTR f;
2768 struct scroll_bar *bar;
2769 GCallback scroll_callback;
2770 char *scroll_bar_name;
2772 GtkWidget *wscroll;
2773 GtkObject *vadj;
2774 int scroll_id;
2776 /* Page, step increment values are not so important here, they
2777 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2778 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2779 0.1, 0.1, 0.1);
2781 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2782 gtk_widget_set_name (wscroll, scroll_bar_name);
2783 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2785 scroll_id = xg_store_widget_in_map (wscroll);
2787 g_signal_connect (G_OBJECT (wscroll),
2788 "value-changed",
2789 scroll_callback,
2790 (gpointer) bar);
2791 g_signal_connect (G_OBJECT (wscroll),
2792 "destroy",
2793 G_CALLBACK (xg_gtk_scroll_destroy),
2794 (gpointer) scroll_id);
2796 /* Connect to button press and button release to detect if any scroll bar
2797 has the pointer. */
2798 g_signal_connect (G_OBJECT (wscroll),
2799 "button-press-event",
2800 G_CALLBACK (scroll_bar_button_cb),
2801 (gpointer) bar);
2802 g_signal_connect (G_OBJECT (wscroll),
2803 "button-release-event",
2804 G_CALLBACK (scroll_bar_button_cb),
2805 (gpointer) bar);
2807 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget),
2808 wscroll, -1, -1);
2810 /* Set the cursor to an arrow. */
2811 xg_set_cursor (wscroll, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2813 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2816 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2817 void
2818 xg_show_scroll_bar (scrollbar_id)
2819 int scrollbar_id;
2821 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2822 if (w)
2823 gtk_widget_show (w);
2826 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2827 void
2828 xg_remove_scroll_bar (f, scrollbar_id)
2829 FRAME_PTR f;
2830 int scrollbar_id;
2832 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2833 if (w)
2835 gtk_widget_destroy (w);
2836 SET_FRAME_GARBAGED (f);
2840 /* Find left/top for widget W in GtkFixed widget WFIXED. */
2841 static void
2842 xg_find_top_left_in_fixed (w, wfixed, left, top)
2843 GtkWidget *w, *wfixed;
2844 int *left, *top;
2846 GList *iter;
2848 for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter))
2850 GtkFixedChild *child = (GtkFixedChild *) iter->data;
2852 if (child->widget == w)
2854 *left = child->x;
2855 *top = child->y;
2856 return;
2860 /* Shall never end up here. */
2861 abort ();
2864 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2865 in frame F.
2866 TOP/LEFT are the new pixel positions where the bar shall appear.
2867 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2868 void
2869 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height,
2870 real_left, canon_width)
2871 FRAME_PTR f;
2872 int scrollbar_id;
2873 int top;
2874 int left;
2875 int width;
2876 int height;
2879 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2881 if (wscroll)
2883 GtkWidget *wfixed = f->output_data.x->edit_widget;
2885 gtk_container_set_reallocate_redraws (GTK_CONTAINER (wfixed), TRUE);
2887 /* Move and resize to new values. */
2888 gtk_fixed_move (GTK_FIXED (wfixed), wscroll, left, top);
2889 gtk_widget_set_size_request (wscroll, width, height);
2891 /* Must force out update so changed scroll bars gets redrawn. */
2892 gdk_window_process_all_updates ();
2894 /* Scroll bars in GTK has a fixed width, so if we say width 16, it
2895 will only be its fixed width (14 is default) anyway, the rest is
2896 blank. We are drawing the mode line across scroll bars when
2897 the frame is split:
2898 |bar| |fringe|
2899 ----------------
2900 mode line
2901 ----------------
2902 |bar| |fringe|
2904 When we "unsplit" the frame:
2906 |bar| |fringe|
2907 -| |-| |
2908 m¦ |i| |
2909 -| |-| |
2910 | | | |
2913 the remains of the mode line can be seen in these blank spaces.
2914 So we must clear them explicitly.
2915 GTK scroll bars should do that, but they don't.
2916 Also, the canonical width may be wider than the width for the
2917 scroll bar so that there is some space (typically 1 pixel) between
2918 the scroll bar and the edge of the window and between the scroll
2919 bar and the fringe. */
2921 XClearWindow (FRAME_X_DISPLAY (f), GTK_WIDGET_TO_X_WIN (wscroll));
2923 SET_FRAME_GARBAGED (f);
2924 cancel_mouse_face (f);
2928 /* Set the thumb size and position of scroll bar BAR. We are currently
2929 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2930 void
2931 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2932 struct scroll_bar *bar;
2933 int portion, position, whole;
2935 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2937 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2939 if (wscroll && NILP (bar->dragging))
2941 GtkAdjustment *adj;
2942 gdouble shown;
2943 gdouble top;
2944 int size, value;
2945 int new_step;
2946 int changed = 0;
2948 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
2950 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2951 rather than the real portion value. This makes the thumb less likely
2952 to resize and that looks better. */
2953 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
2954 /* When the thumb is at the bottom, position == whole.
2955 So we need to increase `whole' to make space for the thumb. */
2956 whole += portion;
2958 if (whole <= 0)
2959 top = 0, shown = 1;
2960 else
2962 top = (gdouble) position / whole;
2963 shown = (gdouble) portion / whole;
2966 size = shown * XG_SB_RANGE;
2967 size = min (size, XG_SB_RANGE);
2968 size = max (size, 1);
2970 value = top * XG_SB_RANGE;
2971 value = min (value, XG_SB_MAX - size);
2972 value = max (value, XG_SB_MIN);
2974 /* Assume all lines are of equal size. */
2975 new_step = size / max (1, FRAME_LINES (f));
2977 if ((int) adj->page_size != size
2978 || (int) adj->step_increment != new_step)
2980 adj->page_size = size;
2981 adj->step_increment = new_step;
2982 /* Assume a page increment is about 95% of the page size */
2983 adj->page_increment = (int) (0.95*adj->page_size);
2984 changed = 1;
2987 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2989 GtkWidget *wfixed = f->output_data.x->edit_widget;
2991 BLOCK_INPUT;
2993 /* gtk_range_set_value invokes the callback. Set
2994 ignore_gtk_scrollbar to make the callback do nothing */
2995 xg_ignore_gtk_scrollbar = 1;
2997 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2998 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
2999 else if (changed)
3000 gtk_adjustment_changed (adj);
3002 xg_ignore_gtk_scrollbar = 0;
3004 UNBLOCK_INPUT;
3010 /***********************************************************************
3011 Tool bar functions
3012 ***********************************************************************/
3013 /* The key for the data we put in the GtkImage widgets. The data is
3014 the image used by Emacs. We use this to see if we need to update
3015 the GtkImage with a new image. */
3016 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3018 /* Callback function invoked when a tool bar item is pressed.
3019 W is the button widget in the tool bar that got pressed,
3020 CLIENT_DATA is an integer that is the index of the button in the
3021 tool bar. 0 is the first button. */
3022 static void
3023 xg_tool_bar_callback (w, client_data)
3024 GtkWidget *w;
3025 gpointer client_data;
3027 int idx = (int)client_data;
3028 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3029 Lisp_Object key, frame;
3030 struct input_event event;
3031 EVENT_INIT (event);
3033 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3034 return;
3036 idx *= TOOL_BAR_ITEM_NSLOTS;
3038 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3039 XSETFRAME (frame, f);
3040 event.kind = TOOL_BAR_EVENT;
3041 event.frame_or_window = frame;
3042 event.arg = frame;
3043 kbd_buffer_store_event (&event);
3045 event.kind = TOOL_BAR_EVENT;
3046 event.frame_or_window = frame;
3047 event.arg = key;
3048 event.modifiers = 0; /* These are not available. */
3049 kbd_buffer_store_event (&event);
3052 /* This callback is called when a tool bar is detached. We must set
3053 the height of the tool bar to zero when this happens so frame sizes
3054 are correctly calculated.
3055 WBOX is the handle box widget that enables detach/attach of the tool bar.
3056 W is the tool bar widget.
3057 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3058 static void
3059 xg_tool_bar_detach_callback (wbox, w, client_data)
3060 GtkHandleBox *wbox;
3061 GtkWidget *w;
3062 gpointer client_data;
3064 FRAME_PTR f = (FRAME_PTR) client_data;
3066 if (f)
3068 /* When detaching a tool bar, not everything dissapear. There are
3069 a few pixels left that are used to drop the tool bar back into
3070 place. */
3071 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
3072 FRAME_TOOLBAR_HEIGHT (f) = 2;
3074 /* The height has changed, resize outer widget and set columns
3075 rows to what we had before detaching the tool bar. */
3076 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3080 /* This callback is called when a tool bar is reattached. We must set
3081 the height of the tool bar when this happens so frame sizes
3082 are correctly calculated.
3083 WBOX is the handle box widget that enables detach/attach of the tool bar.
3084 W is the tool bar widget.
3085 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3086 static void
3087 xg_tool_bar_attach_callback (wbox, w, client_data)
3088 GtkHandleBox *wbox;
3089 GtkWidget *w;
3090 gpointer client_data;
3092 FRAME_PTR f = (FRAME_PTR) client_data;
3094 if (f)
3096 GtkRequisition req;
3098 gtk_widget_size_request (w, &req);
3099 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3101 /* The height has changed, resize outer widget and set columns
3102 rows to what we had before detaching the tool bar. */
3103 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3107 /* This callback is called when the mouse enters or leaves a tool bar item.
3108 It is used for displaying and hiding the help text.
3109 W is the tool bar item, a button.
3110 EVENT is either an enter event or leave event.
3111 CLIENT_DATA is an integer that is the index of the button in the
3112 tool bar. 0 is the first button.
3114 Returns FALSE to tell GTK to keep processing this event. */
3115 static gboolean
3116 xg_tool_bar_help_callback (w, event, client_data)
3117 GtkWidget *w;
3118 GdkEventCrossing *event;
3119 gpointer client_data;
3121 int idx = (int)client_data;
3122 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3123 Lisp_Object help, frame;
3125 if (! GTK_IS_BUTTON (w))
3127 return FALSE;
3130 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3131 return FALSE;
3133 if (event->type == GDK_ENTER_NOTIFY)
3135 idx *= TOOL_BAR_ITEM_NSLOTS;
3136 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3138 if (NILP (help))
3139 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3141 else
3142 help = Qnil;
3144 XSETFRAME (frame, f);
3145 kbd_buffer_store_help_event (frame, help);
3147 return FALSE;
3151 /* This callback is called when a tool bar item shall be redrawn.
3152 It modifies the expose event so that the GtkImage widget redraws the
3153 whole image. This to overcome a bug that makes GtkImage draw the image
3154 in the wrong place when it tries to redraw just a part of the image.
3155 W is the GtkImage to be redrawn.
3156 EVENT is the expose event for W.
3157 CLIENT_DATA is unused.
3159 Returns FALSE to tell GTK to keep processing this event. */
3160 static gboolean
3161 xg_tool_bar_item_expose_callback (w, event, client_data)
3162 GtkWidget *w;
3163 GdkEventExpose *event;
3164 gpointer client_data;
3166 gint width, height;
3168 gdk_drawable_get_size (event->window, &width, &height);
3170 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3171 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3173 event->area.x = max (0, event->area.x);
3174 event->area.y = max (0, event->area.y);
3176 event->area.width = max (width, event->area.width);
3177 event->area.height = max (height, event->area.height);
3179 return FALSE;
3182 /* This callback is called when a tool bar shall be redrawn.
3183 We need to update the tool bar from here in case the image cache
3184 has deleted the pixmaps used in the tool bar.
3185 W is the GtkToolbar to be redrawn.
3186 EVENT is the expose event for W.
3187 CLIENT_DATA is pointing to the frame for this tool bar.
3189 Returns FALSE to tell GTK to keep processing this event. */
3190 static gboolean
3191 xg_tool_bar_expose_callback (w, event, client_data)
3192 GtkWidget *w;
3193 GdkEventExpose *event;
3194 gpointer client_data;
3196 update_frame_tool_bar ((FRAME_PTR) client_data);
3197 return FALSE;
3200 static void
3201 xg_create_tool_bar (f)
3202 FRAME_PTR f;
3204 struct x_output *x = f->output_data.x;
3205 GtkRequisition req;
3206 int vbox_pos = x->menubar_widget ? 1 : 0;
3208 x->toolbar_widget = gtk_toolbar_new ();
3209 x->handlebox_widget = gtk_handle_box_new ();
3210 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3211 x->toolbar_widget);
3213 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3214 FALSE, FALSE, 0);
3216 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3217 vbox_pos);
3219 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3221 /* We only have icons, so override any user setting. We could
3222 use the caption property of the toolbar item (see update_frame_tool_bar
3223 below), but some of those strings are long, making the toolbar so
3224 long it does not fit on the screen. The GtkToolbar widget makes every
3225 item equal size, so the longest caption determine the size of every
3226 tool bar item. I think the creators of the GtkToolbar widget
3227 counted on 4 or 5 character long strings. */
3228 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3229 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3230 GTK_ORIENTATION_HORIZONTAL);
3232 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3233 G_CALLBACK (xg_tool_bar_detach_callback), f);
3234 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3235 G_CALLBACK (xg_tool_bar_attach_callback), f);
3236 g_signal_connect (G_OBJECT (x->toolbar_widget),
3237 "expose-event",
3238 G_CALLBACK (xg_tool_bar_expose_callback),
3241 gtk_widget_show_all (x->handlebox_widget);
3243 gtk_widget_size_request (x->toolbar_widget, &req);
3244 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3246 /* The height has changed, resize outer widget and set columns
3247 rows to what we had before adding the tool bar. */
3248 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3250 SET_FRAME_GARBAGED (f);
3253 void
3254 update_frame_tool_bar (f)
3255 FRAME_PTR f;
3257 int i;
3258 GtkRequisition old_req, new_req;
3259 GList *icon_list;
3260 GList *iter;
3261 struct x_output *x = f->output_data.x;
3263 if (! FRAME_GTK_WIDGET (f))
3264 return;
3266 BLOCK_INPUT;
3268 if (! x->toolbar_widget)
3269 xg_create_tool_bar (f);
3271 gtk_widget_size_request (x->toolbar_widget, &old_req);
3273 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3274 iter = icon_list;
3276 for (i = 0; i < f->n_tool_bar_items; ++i)
3278 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3280 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3281 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3282 int idx;
3283 int img_id;
3284 struct image *img;
3285 Lisp_Object image;
3286 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3288 if (iter) iter = g_list_next (iter);
3290 /* If image is a vector, choose the image according to the
3291 button state. */
3292 image = PROP (TOOL_BAR_ITEM_IMAGES);
3293 if (VECTORP (image))
3295 if (enabled_p)
3296 idx = (selected_p
3297 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3298 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3299 else
3300 idx = (selected_p
3301 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3302 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3304 xassert (ASIZE (image) >= idx);
3305 image = AREF (image, idx);
3307 else
3308 idx = -1;
3310 /* Ignore invalid image specifications. */
3311 if (!valid_image_p (image))
3313 if (wicon) gtk_widget_hide (wicon);
3314 continue;
3317 img_id = lookup_image (f, image);
3318 img = IMAGE_FROM_ID (f, img_id);
3319 prepare_image_for_display (f, img);
3321 if (img->load_failed_p || img->pixmap == None)
3323 if (wicon) gtk_widget_hide (wicon);
3324 continue;
3327 if (! wicon)
3329 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
3331 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3332 0, 0, 0,
3334 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3335 (gpointer)i);
3337 /* Save the image so we can see if an update is needed when
3338 this function is called again. */
3339 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3340 (gpointer)img->pixmap);
3342 /* Catch expose events to overcome an annoying redraw bug, see
3343 comment for xg_tool_bar_item_expose_callback. */
3344 g_signal_connect (G_OBJECT (w),
3345 "expose-event",
3346 G_CALLBACK (xg_tool_bar_item_expose_callback),
3349 /* We must set sensitive on the button that is the parent
3350 of the GtkImage parent. Go upwards until we find the button. */
3351 while (! GTK_IS_BUTTON (w))
3352 w = gtk_widget_get_parent (w);
3354 if (w)
3356 /* Save the frame in the button so the xg_tool_bar_callback
3357 can get at it. */
3358 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3359 gtk_widget_set_sensitive (w, enabled_p);
3361 /* Use enter/leave notify to show help. We use the events
3362 rather than the GtkButton specific signals "enter" and
3363 "leave", so we can have only one callback. The event
3364 will tell us what kind of event it is. */
3365 g_signal_connect (G_OBJECT (w),
3366 "enter-notify-event",
3367 G_CALLBACK (xg_tool_bar_help_callback),
3368 (gpointer)i);
3369 g_signal_connect (G_OBJECT (w),
3370 "leave-notify-event",
3371 G_CALLBACK (xg_tool_bar_help_callback),
3372 (gpointer)i);
3375 else
3377 /* The child of the tool bar is a button. Inside that button
3378 is a vbox. Inside that vbox is the GtkImage. */
3379 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3380 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3381 GtkImage *wimage = GTK_IMAGE (chlist->data);
3382 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3383 XG_TOOL_BAR_IMAGE_DATA);
3384 g_list_free (chlist);
3386 if (old_img != img->pixmap)
3387 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
3389 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3390 (gpointer)img->pixmap);
3392 gtk_widget_set_sensitive (wicon, enabled_p);
3393 gtk_widget_show (wicon);
3396 #undef PROP
3399 /* Remove buttons not longer needed. We just hide them so they
3400 can be reused later on. */
3401 while (iter)
3403 GtkWidget *w = GTK_WIDGET (iter->data);
3404 gtk_widget_hide (w);
3405 iter = g_list_next (iter);
3408 gtk_widget_size_request (x->toolbar_widget, &new_req);
3409 if (old_req.height != new_req.height)
3411 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3412 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3415 if (icon_list) g_list_free (icon_list);
3417 UNBLOCK_INPUT;
3420 void
3421 free_frame_tool_bar (f)
3422 FRAME_PTR f;
3424 struct x_output *x = f->output_data.x;
3426 if (x->toolbar_widget)
3428 BLOCK_INPUT;
3429 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3430 x->handlebox_widget);
3431 x->toolbar_widget = 0;
3432 x->handlebox_widget = 0;
3433 FRAME_TOOLBAR_HEIGHT (f) = 0;
3435 /* The height has changed, resize outer widget and set columns
3436 rows to what we had before removing the tool bar. */
3437 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3439 SET_FRAME_GARBAGED (f);
3440 UNBLOCK_INPUT;
3446 /***********************************************************************
3447 Initializing
3448 ***********************************************************************/
3449 void
3450 xg_initialize ()
3452 xg_ignore_gtk_scrollbar = 0;
3453 xg_detached_menus = 0;
3454 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3455 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3457 id_to_widget.max_size = id_to_widget.used = 0;
3458 id_to_widget.widgets = 0;
3460 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3461 bindings. It doesn't seem to be any way to remove properties,
3462 so we set it to VoidSymbol which in X means "no key". */
3463 gtk_settings_set_string_property (gtk_settings_get_default (),
3464 "gtk-menu-bar-accel",
3465 "VoidSymbol",
3466 EMACS_CLASS);
3468 /* Make GTK text input widgets use Emacs style keybindings. This is
3469 Emacs after all. */
3470 gtk_settings_set_string_property (gtk_settings_get_default (),
3471 "gtk-key-theme-name",
3472 "Emacs",
3473 EMACS_CLASS);
3476 #endif /* USE_GTK */
3478 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3479 (do not change this comment) */