*** empty log message ***
[emacs.git] / src / gtkutil.c
blob4d0b50573f7f79c0e7e5b4227ef1d80acbacd623
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 GdkPixmap for
233 the pixmap in *GPIX, and a GdkBitmap for the mask in *GMASK.
234 If IMG has no mask, *GMASK is set to NULL.
235 The image is defined on the display where frame F is. */
236 static void
237 xg_get_gdk_pixmap_and_mask (f, img, gpix, gmask)
238 FRAME_PTR f;
239 struct image *img;
240 GdkPixmap **gpix;
241 GdkBitmap **gmask;
243 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
245 *gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
246 *gmask = img->mask ?
247 (GdkBitmap*) gdk_pixmap_foreign_new_for_display (gdpy, img->mask)
248 : 0;
252 /* Set CURSOR on W and all widgets W contain. We must do like this
253 for scroll bars and menu because they create widgets internally,
254 and it is those widgets that are visible. */
255 static void
256 xg_set_cursor (w, cursor)
257 GtkWidget *w;
258 GdkCursor *cursor;
260 GList *children = gdk_window_peek_children (w->window);
262 gdk_window_set_cursor (w->window, cursor);
264 /* The scroll bar widget has more than one GDK window (had to look at
265 the source to figure this out), and there is no way to set cursor
266 on widgets in GTK. So we must set the cursor for all GDK windows.
267 Ditto for menus. */
269 for ( ; children; children = g_list_next (children))
270 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
273 /* Timer function called when a timeout occurs for xg_timer.
274 This function processes all GTK events in a recursive event loop.
275 This is done because GTK timer events are not seen by Emacs event
276 detection, Emacs only looks for X events. When a scroll bar has the
277 pointer (detected by button press/release events below) an Emacs
278 timer is started, and this function can then check if the GTK timer
279 has expired by calling the GTK event loop.
280 Also, when a menu is active, it has a small timeout before it
281 pops down the sub menu under it. */
282 static void
283 xg_process_timeouts (timer)
284 struct atimer *timer;
286 BLOCK_INPUT;
287 /* Ideally we would like to just handle timer events, like the Xt version
288 of this does in xterm.c, but there is no such feature in GTK. */
289 while (gtk_events_pending ())
290 gtk_main_iteration ();
291 UNBLOCK_INPUT;
294 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
295 xg_process_timeouts is called when the timer expires. The timer
296 started is continuous, i.e. runs until xg_stop_timer is called. */
297 static void
298 xg_start_timer ()
300 if (! xg_timer)
302 EMACS_TIME interval;
303 EMACS_SET_SECS_USECS (interval, 0, 100000);
304 xg_timer = start_atimer (ATIMER_CONTINUOUS,
305 interval,
306 xg_process_timeouts,
311 /* Stop the xg_timer if started. */
312 static void
313 xg_stop_timer ()
315 if (xg_timer)
317 cancel_atimer (xg_timer);
318 xg_timer = 0;
322 /* Insert NODE into linked LIST. */
323 static void
324 xg_list_insert (xg_list_node *list, xg_list_node *node)
326 xg_list_node *list_start = list->next;
328 if (list_start) list_start->prev = node;
329 node->next = list_start;
330 node->prev = 0;
331 list->next = node;
334 /* Remove NODE from linked LIST. */
335 static void
336 xg_list_remove (xg_list_node *list, xg_list_node *node)
338 xg_list_node *list_start = list->next;
339 if (node == list_start)
341 list->next = node->next;
342 if (list->next) list->next->prev = 0;
344 else
346 node->prev->next = node->next;
347 if (node->next) node->next->prev = node->prev;
351 /* Allocate and return a utf8 version of STR. If STR is already
352 utf8 or NULL, just return STR.
353 If not, a new string is allocated and the caller must free the result
354 with g_free. */
355 static char *
356 get_utf8_string (str)
357 char *str;
359 char *utf8_str = str;
361 /* If not UTF-8, try current locale. */
362 if (str && !g_utf8_validate (str, -1, NULL))
363 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
365 return utf8_str;
370 /***********************************************************************
371 General functions for creating widgets, resizing, events, e.t.c.
372 ***********************************************************************/
374 /* Make a geometry string and pass that to GTK. It seems this is the
375 only way to get geometry position right if the user explicitly
376 asked for a position when starting Emacs.
377 F is the frame we shall set geometry for. */
378 static void
379 xg_set_geometry (f)
380 FRAME_PTR f;
382 if (f->size_hint_flags & USPosition)
384 int left = f->left_pos;
385 int xneg = f->size_hint_flags & XNegative;
386 int top = f->top_pos;
387 int yneg = f->size_hint_flags & YNegative;
388 char geom_str[32];
390 if (xneg)
391 left = -left;
392 if (yneg)
393 top = -top;
395 sprintf (geom_str, "=%dx%d%c%d%c%d",
396 FRAME_PIXEL_WIDTH (f),
397 FRAME_TOTAL_PIXEL_HEIGHT (f),
398 (xneg ? '-' : '+'), left,
399 (yneg ? '-' : '+'), top);
401 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
402 geom_str))
403 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
408 /* Resize the outer window of frame F after chainging the height.
409 This happend when the menu bar or the tool bar is added or removed.
410 COLUMNS/ROWS is the size the edit area shall have after the resize. */
411 static void
412 xg_resize_outer_widget (f, columns, rows)
413 FRAME_PTR f;
414 int columns;
415 int rows;
417 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
418 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
420 /* base_height is now changed. */
421 x_wm_set_size_hint (f, 0, 0);
423 /* If we are not mapped yet, set geometry once again, as window
424 height now have changed. */
425 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
426 xg_set_geometry (f);
428 xg_frame_set_char_size (f, columns, rows);
429 gdk_window_process_all_updates ();
432 /* This gets called after the frame F has been cleared. Since that is
433 done with X calls, we need to redraw GTK widget (scroll bars). */
434 void
435 xg_frame_cleared (f)
436 FRAME_PTR f;
438 GtkWidget *w = f->output_data.x->widget;
440 if (w)
442 gtk_container_set_reallocate_redraws (GTK_CONTAINER (w), TRUE);
443 gtk_container_foreach (GTK_CONTAINER (w),
444 (GtkCallback) gtk_widget_queue_draw,
446 gdk_window_process_all_updates ();
450 /* Function to handle resize of our widgets. Since Emacs has some layouts
451 that does not fit well with GTK standard containers, we do most layout
452 manually.
453 F is the frame to resize.
454 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
455 void
456 xg_resize_widgets (f, pixelwidth, pixelheight)
457 FRAME_PTR f;
458 int pixelwidth, pixelheight;
460 int mbheight = FRAME_MENUBAR_HEIGHT (f);
461 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
462 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
463 - mbheight - tbheight));
464 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
466 if (FRAME_GTK_WIDGET (f)
467 && (columns != FRAME_COLS (f) || rows != FRAME_LINES (f)
468 || pixelwidth != FRAME_PIXEL_WIDTH (f) || pixelheight != FRAME_PIXEL_HEIGHT (f)))
470 struct x_output *x = f->output_data.x;
471 GtkAllocation all;
473 all.y = mbheight + tbheight;
474 all.x = 0;
476 all.width = pixelwidth;
477 all.height = pixelheight - mbheight - tbheight;
479 gtk_widget_size_allocate (x->edit_widget, &all);
481 change_frame_size (f, rows, columns, 0, 1, 0);
482 SET_FRAME_GARBAGED (f);
483 cancel_mouse_face (f);
488 /* Update our widget size to be COLS/ROWS characters for frame F. */
489 void
490 xg_frame_set_char_size (f, cols, rows)
491 FRAME_PTR f;
492 int cols;
493 int rows;
495 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
496 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
497 int pixelwidth;
499 /* Take into account the size of the scroll bar. Always use the
500 number of columns occupied by the scroll bar here otherwise we
501 might end up with a frame width that is not a multiple of the
502 frame's character width which is bad for vertically split
503 windows. */
504 f->scroll_bar_actual_width
505 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
507 compute_fringe_widths (f, 0);
509 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
510 after calculating that value. */
511 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
513 /* Must resize our top level widget. Font size may have changed,
514 but not rows/cols. */
515 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
516 pixelwidth, pixelheight);
517 xg_resize_widgets (f, pixelwidth, pixelheight);
518 x_wm_set_size_hint (f, 0, 0);
519 SET_FRAME_GARBAGED (f);
520 cancel_mouse_face (f);
523 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
524 Must be done like this, because GtkWidget:s can have "hidden"
525 X Window that aren't accessible.
527 Return 0 if no widget match WDESC. */
528 GtkWidget *
529 xg_win_to_widget (dpy, wdesc)
530 Display *dpy;
531 Window wdesc;
533 gpointer gdkwin;
534 GtkWidget *gwdesc = 0;
536 BLOCK_INPUT;
538 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
539 wdesc);
540 if (gdkwin)
542 GdkEvent event;
543 event.any.window = gdkwin;
544 gwdesc = gtk_get_event_widget (&event);
547 UNBLOCK_INPUT;
548 return gwdesc;
551 /* Fill in the GdkColor C so that it represents PIXEL.
552 W is the widget that color will be used for. Used to find colormap. */
553 static void
554 xg_pix_to_gcolor (w, pixel, c)
555 GtkWidget *w;
556 unsigned long pixel;
557 GdkColor *c;
559 GdkColormap *map = gtk_widget_get_colormap (w);
560 gdk_colormap_query_color (map, pixel, c);
563 /* Turning off double buffering for our GtkFixed widget has the side
564 effect of turning it off also for its children (scroll bars).
565 But we want those to be double buffered to not flicker so handle
566 expose manually here.
567 WIDGET is the GtkFixed widget that gets exposed.
568 EVENT is the expose event.
569 USER_DATA is unused.
571 Return TRUE to tell GTK that this expose event has been fully handeled
572 and that GTK shall do nothing more with it. */
573 static gboolean
574 xg_fixed_handle_expose (GtkWidget *widget,
575 GdkEventExpose *event,
576 gpointer user_data)
578 GList *iter;
580 for (iter = GTK_FIXED (widget)->children; iter; iter = g_list_next (iter))
582 GtkFixedChild *child_data = (GtkFixedChild *) iter->data;
583 GtkWidget *child = child_data->widget;
584 GdkWindow *window = child->window;
585 GdkRegion *region = gtk_widget_region_intersect (child, event->region);
587 if (! gdk_region_empty (region))
589 GdkEvent child_event;
590 child_event.expose = *event;
591 child_event.expose.region = region;
593 /* Turn on double buffering, i.e. draw to an off screen area. */
594 gdk_window_begin_paint_region (window, region);
596 /* Tell child to redraw itself. */
597 gdk_region_get_clipbox (region, &child_event.expose.area);
598 gtk_widget_send_expose (child, &child_event);
599 gdk_window_process_updates (window, TRUE);
601 /* Copy off screen area to the window. */
602 gdk_window_end_paint (window);
605 gdk_region_destroy (region);
608 return TRUE;
611 /* Create and set up the GTK widgets for frame F.
612 Return 0 if creation failed, non-zero otherwise. */
614 xg_create_frame_widgets (f)
615 FRAME_PTR f;
617 GtkWidget *wtop;
618 GtkWidget *wvbox;
619 GtkWidget *wfixed;
620 GdkColor bg;
621 GtkRcStyle *style;
622 int i;
623 char *title = 0;
625 BLOCK_INPUT;
627 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
628 xg_set_screen (wtop, f);
630 wvbox = gtk_vbox_new (FALSE, 0);
631 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
633 if (! wtop || ! wvbox || ! wfixed)
635 if (wtop) gtk_widget_destroy (wtop);
636 if (wvbox) gtk_widget_destroy (wvbox);
637 if (wfixed) gtk_widget_destroy (wfixed);
639 return 0;
642 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
643 gtk_widget_set_name (wtop, EMACS_CLASS);
644 gtk_widget_set_name (wvbox, "pane");
645 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
647 /* If this frame has a title or name, set it in the title bar. */
648 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
649 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
651 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
653 FRAME_GTK_OUTER_WIDGET (f) = wtop;
654 FRAME_GTK_WIDGET (f) = wfixed;
655 f->output_data.x->vbox_widget = wvbox;
657 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
659 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
660 FRAME_PIXEL_HEIGHT (f));
662 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
663 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
665 if (FRAME_EXTERNAL_TOOL_BAR (f))
666 update_frame_tool_bar (f);
668 /* The tool bar is created but first there are no items in it.
669 This causes it to be zero height. Later items are added, but then
670 the frame is already mapped, so there is a "jumping" resize.
671 This makes geometry handling difficult, for example -0-0 will end
672 up in the wrong place as tool bar height has not been taken into account.
673 So we cheat a bit by setting a height that is what it will have
674 later on when tool bar items are added. */
675 if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0)
676 FRAME_TOOLBAR_HEIGHT (f) = 34;
679 /* We don't want this widget double buffered, because we draw on it
680 with regular X drawing primitives, so from a GTK/GDK point of
681 view, the widget is totally blank. When an expose comes, this
682 will make the widget blank, and then Emacs redraws it. This flickers
683 a lot, so we turn off double buffering. */
684 gtk_widget_set_double_buffered (wfixed, FALSE);
686 /* Turning off double buffering above has the side effect of turning
687 it off also for its children (scroll bars). But we want those
688 to be double buffered to not flicker so handle expose manually. */
689 g_signal_connect (G_OBJECT (wfixed), "expose-event",
690 G_CALLBACK (xg_fixed_handle_expose), 0);
692 /* GTK documents says use gtk_window_set_resizable. But then a user
693 can't shrink the window from its starting size. */
694 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
695 gtk_window_set_wmclass (GTK_WINDOW (wtop),
696 SDATA (Vx_resource_name),
697 SDATA (Vx_resource_class));
699 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
700 GTK is to destroy the widget. We want Emacs to do that instead. */
701 g_signal_connect (G_OBJECT (wtop), "delete-event",
702 G_CALLBACK (gtk_true), 0);
704 /* Convert our geometry parameters into a geometry string
705 and specify it.
706 GTK will itself handle calculating the real position this way. */
707 xg_set_geometry (f);
709 gtk_widget_add_events (wfixed,
710 GDK_POINTER_MOTION_MASK
711 | GDK_EXPOSURE_MASK
712 | GDK_BUTTON_PRESS_MASK
713 | GDK_BUTTON_RELEASE_MASK
714 | GDK_KEY_PRESS_MASK
715 | GDK_ENTER_NOTIFY_MASK
716 | GDK_LEAVE_NOTIFY_MASK
717 | GDK_FOCUS_CHANGE_MASK
718 | GDK_STRUCTURE_MASK
719 | GDK_VISIBILITY_NOTIFY_MASK);
721 /* Must realize the windows so the X window gets created. It is used
722 by callers of this function. */
723 gtk_widget_realize (wfixed);
724 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
726 /* Since GTK clears its window by filling with the background color,
727 we must keep X and GTK background in sync. */
728 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
729 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
731 /* Also, do not let any background pixmap to be set, this looks very
732 bad as Emacs overwrites the background pixmap with its own idea
733 of background color. */
734 style = gtk_widget_get_modifier_style (wfixed);
736 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
737 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
738 gtk_widget_modify_style (wfixed, style);
740 /* GTK does not set any border, and they look bad with GTK. */
741 f->border_width = 0;
742 f->internal_border_width = 0;
744 UNBLOCK_INPUT;
746 return 1;
749 /* Set the normal size hints for the window manager, for frame F.
750 FLAGS is the flags word to use--or 0 meaning preserve the flags
751 that the window now has.
752 If USER_POSITION is nonzero, we set the User Position
753 flag (this is useful when FLAGS is 0). */
754 void
755 x_wm_set_size_hint (f, flags, user_position)
756 FRAME_PTR f;
757 long flags;
758 int user_position;
760 if (FRAME_GTK_OUTER_WIDGET (f))
762 /* Must use GTK routines here, otherwise GTK resets the size hints
763 to its own defaults. */
764 GdkGeometry size_hints;
765 gint hint_flags = 0;
766 int base_width, base_height;
767 int min_rows = 0, min_cols = 0;
768 int win_gravity = f->win_gravity;
770 if (flags)
772 memset (&size_hints, 0, sizeof (size_hints));
773 f->output_data.x->size_hints = size_hints;
774 f->output_data.x->hint_flags = hint_flags;
776 else
777 flags = f->size_hint_flags;
779 size_hints = f->output_data.x->size_hints;
780 hint_flags = f->output_data.x->hint_flags;
782 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
783 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
784 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
786 hint_flags |= GDK_HINT_BASE_SIZE;
787 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
788 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
789 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
791 check_frame_size (f, &min_rows, &min_cols);
793 size_hints.base_width = base_width;
794 size_hints.base_height = base_height;
795 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
796 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
799 /* These currently have a one to one mapping with the X values, but I
800 don't think we should rely on that. */
801 hint_flags |= GDK_HINT_WIN_GRAVITY;
802 size_hints.win_gravity = 0;
803 if (win_gravity == NorthWestGravity)
804 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
805 else if (win_gravity == NorthGravity)
806 size_hints.win_gravity = GDK_GRAVITY_NORTH;
807 else if (win_gravity == NorthEastGravity)
808 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
809 else if (win_gravity == WestGravity)
810 size_hints.win_gravity = GDK_GRAVITY_WEST;
811 else if (win_gravity == CenterGravity)
812 size_hints.win_gravity = GDK_GRAVITY_CENTER;
813 else if (win_gravity == EastGravity)
814 size_hints.win_gravity = GDK_GRAVITY_EAST;
815 else if (win_gravity == SouthWestGravity)
816 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
817 else if (win_gravity == SouthGravity)
818 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
819 else if (win_gravity == SouthEastGravity)
820 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
821 else if (win_gravity == StaticGravity)
822 size_hints.win_gravity = GDK_GRAVITY_STATIC;
824 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
825 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
826 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
828 if (user_position)
830 hint_flags &= ~GDK_HINT_POS;
831 hint_flags |= GDK_HINT_USER_POS;
834 BLOCK_INPUT;
836 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
837 FRAME_GTK_OUTER_WIDGET (f),
838 &size_hints,
839 hint_flags);
841 f->output_data.x->size_hints = size_hints;
842 f->output_data.x->hint_flags = hint_flags;
843 UNBLOCK_INPUT;
847 /* Change background color of a frame.
848 Since GTK uses the background colour to clear the window, we must
849 keep the GTK and X colors in sync.
850 F is the frame to change,
851 BG is the pixel value to change to. */
852 void
853 xg_set_background_color (f, bg)
854 FRAME_PTR f;
855 unsigned long bg;
857 if (FRAME_GTK_WIDGET (f))
859 GdkColor gdk_bg;
861 BLOCK_INPUT;
862 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
863 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
864 UNBLOCK_INPUT;
870 /***********************************************************************
871 Dialog functions
872 ***********************************************************************/
873 /* Return the dialog title to use for a dialog of type KEY.
874 This is the encoding used by lwlib. We use the same for GTK. */
875 static char *
876 get_dialog_title (char key)
878 char *title = "";
880 switch (key) {
881 case 'E': case 'e':
882 title = "Error";
883 break;
885 case 'I': case 'i':
886 title = "Information";
887 break;
889 case 'L': case 'l':
890 title = "Prompt";
891 break;
893 case 'P': case 'p':
894 title = "Prompt";
895 break;
897 case 'Q': case 'q':
898 title = "Question";
899 break;
902 return title;
905 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
906 the dialog, but return TRUE so the event does not propagate further
907 in GTK. This prevents GTK from destroying the dialog widget automatically
908 and we can always destrou the widget manually, regardles of how
909 it was popped down (button press or WM_DELETE_WINDOW).
910 W is the dialog widget.
911 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
912 user_data is NULL (not used).
914 Returns TRUE to end propagation of event. */
915 static gboolean
916 dialog_delete_callback (w, event, user_data)
917 GtkWidget *w;
918 GdkEvent *event;
919 gpointer user_data;
921 gtk_widget_unmap (w);
922 return TRUE;
925 /* Create a popup dialog window. See also xg_create_widget below.
926 WV is a widget_value describing the dialog.
927 SELECT_CB is the callback to use when a button has been pressed.
928 DEACTIVATE_CB is the callback to use when the dialog pops down.
930 Returns the GTK dialog widget. */
931 static GtkWidget *
932 create_dialog (wv, select_cb, deactivate_cb)
933 widget_value *wv;
934 GCallback select_cb;
935 GCallback deactivate_cb;
937 char *title = get_dialog_title (wv->name[0]);
938 int total_buttons = wv->name[1] - '0';
939 int right_buttons = wv->name[4] - '0';
940 int left_buttons;
941 int button_nr = 0;
942 int button_spacing = 10;
943 GtkWidget *wdialog = gtk_dialog_new ();
944 widget_value *item;
945 GtkBox *cur_box;
946 GtkWidget *wvbox;
947 GtkWidget *whbox_up;
948 GtkWidget *whbox_down;
950 /* If the number of buttons is greater than 4, make two rows of buttons
951 instead. This looks better. */
952 int make_two_rows = total_buttons > 4;
954 if (right_buttons == 0) right_buttons = total_buttons/2;
955 left_buttons = total_buttons - right_buttons;
957 gtk_window_set_title (GTK_WINDOW (wdialog), title);
958 gtk_widget_set_name (wdialog, "emacs-dialog");
960 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
962 if (make_two_rows)
964 wvbox = gtk_vbox_new (TRUE, button_spacing);
965 whbox_up = gtk_hbox_new (FALSE, 0);
966 whbox_down = gtk_hbox_new (FALSE, 0);
968 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
969 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
970 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
972 cur_box = GTK_BOX (whbox_up);
975 g_signal_connect (G_OBJECT (wdialog), "delete-event",
976 G_CALLBACK (dialog_delete_callback), 0);
978 if (deactivate_cb)
980 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
981 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
984 for (item = wv->contents; item; item = item->next)
986 char *utf8_label = get_utf8_string (item->value);
987 GtkWidget *w;
988 GtkRequisition req;
990 if (item->name && strcmp (item->name, "message") == 0)
992 /* This is the text part of the dialog. */
993 w = gtk_label_new (utf8_label);
994 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
995 gtk_label_new (""),
996 FALSE, FALSE, 0);
997 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
998 TRUE, TRUE, 0);
999 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1001 /* Try to make dialog look better. Must realize first so
1002 the widget can calculate the size it needs. */
1003 gtk_widget_realize (w);
1004 gtk_widget_size_request (w, &req);
1005 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1006 req.height);
1007 if (item->value && strlen (item->value) > 0)
1008 button_spacing = 2*req.width/strlen (item->value);
1010 else
1012 /* This is one button to add to the dialog. */
1013 w = gtk_button_new_with_label (utf8_label);
1014 if (! item->enabled)
1015 gtk_widget_set_sensitive (w, FALSE);
1016 if (select_cb)
1017 g_signal_connect (G_OBJECT (w), "clicked",
1018 select_cb, item->call_data);
1020 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1021 if (++button_nr == left_buttons)
1023 if (make_two_rows)
1024 cur_box = GTK_BOX (whbox_down);
1025 else
1026 gtk_box_pack_start (cur_box,
1027 gtk_label_new (""),
1028 TRUE, TRUE,
1029 button_spacing);
1033 if (utf8_label && utf8_label != item->value)
1034 g_free (utf8_label);
1037 return wdialog;
1041 enum
1043 XG_FILE_NOT_DONE,
1044 XG_FILE_OK,
1045 XG_FILE_CANCEL,
1046 XG_FILE_DESTROYED,
1049 /* Callback function invoked when the Ok button is pressed in
1050 a file dialog.
1051 W is the file dialog widget,
1052 ARG points to an integer where we record what has happend. */
1053 static void
1054 xg_file_sel_ok (w, arg)
1055 GtkWidget *w;
1056 gpointer arg;
1058 *(int*)arg = XG_FILE_OK;
1061 /* Callback function invoked when the Cancel button is pressed in
1062 a file dialog.
1063 W is the file dialog widget,
1064 ARG points to an integer where we record what has happend. */
1065 static void
1066 xg_file_sel_cancel (w, arg)
1067 GtkWidget *w;
1068 gpointer arg;
1070 *(int*)arg = XG_FILE_CANCEL;
1073 /* Callback function invoked when the file dialog is destroyed (i.e.
1074 popped down). We must keep track of this, because if this
1075 happens, GTK destroys the widget. But if for example, Ok is pressed,
1076 the dialog is popped down, but the dialog widget is not destroyed.
1077 W is the file dialog widget,
1078 ARG points to an integer where we record what has happend. */
1079 static void
1080 xg_file_sel_destroy (w, arg)
1081 GtkWidget *w;
1082 gpointer arg;
1084 *(int*)arg = XG_FILE_DESTROYED;
1087 /* Read a file name from the user using a file dialog.
1088 F is the current frame.
1089 PROMPT is a prompt to show to the user. May not be NULL.
1090 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1091 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1092 file.
1094 Returns a file name or NULL if no file was selected.
1095 The returned string must be freed by the caller. */
1096 char *
1097 xg_get_file_name (f, prompt, default_filename, mustmatch_p)
1098 FRAME_PTR f;
1099 char *prompt;
1100 char *default_filename;
1101 int mustmatch_p;
1103 GtkWidget *filewin;
1104 GtkFileSelection *filesel;
1105 int filesel_done = XG_FILE_NOT_DONE;
1106 char *fn = 0;
1108 filewin = gtk_file_selection_new (prompt);
1109 filesel = GTK_FILE_SELECTION (filewin);
1111 xg_set_screen (filewin, f);
1113 gtk_widget_set_name (filewin, "emacs-filedialog");
1115 gtk_window_set_transient_for (GTK_WINDOW (filewin),
1116 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1117 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
1119 g_signal_connect (G_OBJECT (filesel->ok_button),
1120 "clicked",
1121 G_CALLBACK (xg_file_sel_ok),
1122 &filesel_done);
1123 g_signal_connect (G_OBJECT (filesel->cancel_button),
1124 "clicked",
1125 G_CALLBACK (xg_file_sel_cancel),
1126 &filesel_done);
1127 g_signal_connect (G_OBJECT (filesel),
1128 "destroy",
1129 G_CALLBACK (xg_file_sel_destroy),
1130 &filesel_done);
1132 if (default_filename)
1133 gtk_file_selection_set_filename (filesel, default_filename);
1135 if (mustmatch_p)
1137 /* The selection_entry part of filesel is not documented. */
1138 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1139 gtk_file_selection_hide_fileop_buttons (filesel);
1143 gtk_widget_show_all (filewin);
1145 while (filesel_done == XG_FILE_NOT_DONE)
1146 gtk_main_iteration ();
1148 if (filesel_done == XG_FILE_OK)
1149 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1151 if (filesel_done != XG_FILE_DESTROYED)
1152 gtk_widget_destroy (filewin);
1154 return fn;
1158 /***********************************************************************
1159 Menu functions.
1160 ***********************************************************************/
1162 /* The name of menu items that can be used for citomization. Since GTK
1163 RC files are very crude and primitive, we have to set this on all
1164 menu item names so a user can easily cutomize menu items. */
1166 #define MENU_ITEM_NAME "emacs-menuitem"
1169 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1170 during GC. The next member points to the items. */
1171 static xg_list_node xg_menu_cb_list;
1173 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1174 during GC. The next member points to the items. */
1175 static xg_list_node xg_menu_item_cb_list;
1177 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1178 F is the frame CL_DATA will be initialized for.
1179 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1181 The menu bar and all sub menus under the menu bar in a frame
1182 share the same structure, hence the reference count.
1184 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1185 allocated xg_menu_cb_data if CL_DATA is NULL. */
1186 static xg_menu_cb_data *
1187 make_cl_data (cl_data, f, highlight_cb)
1188 xg_menu_cb_data *cl_data;
1189 FRAME_PTR f;
1190 GCallback highlight_cb;
1192 if (! cl_data)
1194 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1195 cl_data->f = f;
1196 cl_data->menu_bar_vector = f->menu_bar_vector;
1197 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1198 cl_data->highlight_cb = highlight_cb;
1199 cl_data->ref_count = 0;
1201 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1204 cl_data->ref_count++;
1206 return cl_data;
1209 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1210 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1212 When the menu bar is updated, menu items may have been added and/or
1213 removed, so menu_bar_vector and menu_bar_items_used change. We must
1214 then update CL_DATA since it is used to determine which menu
1215 item that is invoked in the menu.
1216 HIGHLIGHT_CB could change, there is no check that the same
1217 function is given when modifying a menu bar as was given when
1218 creating the menu bar. */
1219 static void
1220 update_cl_data (cl_data, f, highlight_cb)
1221 xg_menu_cb_data *cl_data;
1222 FRAME_PTR f;
1223 GCallback highlight_cb;
1225 if (cl_data)
1227 cl_data->f = f;
1228 cl_data->menu_bar_vector = f->menu_bar_vector;
1229 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1230 cl_data->highlight_cb = highlight_cb;
1234 /* Decrease reference count for CL_DATA.
1235 If reference count is zero, free CL_DATA. */
1236 static void
1237 unref_cl_data (cl_data)
1238 xg_menu_cb_data *cl_data;
1240 if (cl_data && cl_data->ref_count > 0)
1242 cl_data->ref_count--;
1243 if (cl_data->ref_count == 0)
1245 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1246 xfree (cl_data);
1251 /* Function that marks all lisp data during GC. */
1252 void
1253 xg_mark_data ()
1255 xg_list_node *iter;
1257 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1258 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1260 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1262 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1264 if (! NILP (cb_data->help))
1265 mark_object (cb_data->help);
1270 /* Callback called when a menu item is destroyed. Used to free data.
1271 W is the widget that is being destroyed (not used).
1272 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1273 static void
1274 menuitem_destroy_callback (w, client_data)
1275 GtkWidget *w;
1276 gpointer client_data;
1278 if (client_data)
1280 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1281 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1282 xfree (data);
1286 /* Callback called when the pointer enters/leaves a menu item.
1287 W is the menu item.
1288 EVENT is either an enter event or leave event.
1289 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1291 Returns FALSE to tell GTK to keep processing this event. */
1292 static gboolean
1293 menuitem_highlight_callback (w, event, client_data)
1294 GtkWidget *w;
1295 GdkEventCrossing *event;
1296 gpointer client_data;
1298 if (client_data)
1300 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1301 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1303 if (! NILP (data->help) && data->cl_data->highlight_cb)
1305 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1306 (*func) (w, call_data);
1310 return FALSE;
1313 /* Callback called when a menu is destroyed. Used to free data.
1314 W is the widget that is being destroyed (not used).
1315 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1316 static void
1317 menu_destroy_callback (w, client_data)
1318 GtkWidget *w;
1319 gpointer client_data;
1321 unref_cl_data ((xg_menu_cb_data*) client_data);
1324 /* Callback called when a menu does a grab or ungrab. That means the
1325 menu has been activated or deactivated.
1326 Used to start a timer so the small timeout the menus in GTK uses before
1327 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1328 W is the widget that does the grab (not used).
1329 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1330 CLIENT_DATA is NULL (not used). */
1331 static void
1332 menu_grab_callback (GtkWidget *widget,
1333 gboolean ungrab_p,
1334 gpointer client_data)
1336 /* Keep track of total number of grabs. */
1337 static int cnt;
1339 if (ungrab_p) cnt--;
1340 else cnt++;
1342 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1343 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1346 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1347 must be non-NULL) and can be inserted into a menu item.
1349 Returns the GtkHBox. */
1350 static GtkWidget *
1351 make_widget_for_menu_item (utf8_label, utf8_key)
1352 char *utf8_label;
1353 char *utf8_key;
1355 GtkWidget *wlbl;
1356 GtkWidget *wkey;
1357 GtkWidget *wbox;
1359 wbox = gtk_hbox_new (FALSE, 0);
1360 wlbl = gtk_label_new (utf8_label);
1361 wkey = gtk_label_new (utf8_key);
1363 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1364 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1366 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1367 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1369 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1370 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1371 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1373 return wbox;
1376 /* Make and return a menu item widget with the key to the right.
1377 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1378 UTF8_KEY is the text representing the key binding.
1379 ITEM is the widget_value describing the menu item.
1381 GROUP is an in/out parameter. If the menu item to be created is not
1382 part of any radio menu group, *GROUP contains NULL on entry and exit.
1383 If the menu item to be created is part of a radio menu group, on entry
1384 *GROUP contains the group to use, or NULL if this is the first item
1385 in the group. On exit, *GROUP contains the radio item group.
1387 Unfortunately, keys don't line up as nicely as in Motif,
1388 but the MacOS X version doesn't either, so I guess that is OK. */
1389 static GtkWidget *
1390 make_menu_item (utf8_label, utf8_key, item, group)
1391 char *utf8_label;
1392 char *utf8_key;
1393 widget_value *item;
1394 GSList **group;
1396 GtkWidget *w;
1397 GtkWidget *wtoadd = 0;
1399 /* It has been observed that some menu items have a NULL name field.
1400 This will lead to this function being called with a NULL utf8_label.
1401 GTK crashes on that so we set a blank label. Why there is a NULL
1402 name remains to be investigated. */
1403 if (! utf8_label) utf8_label = " ";
1405 if (utf8_key)
1406 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1408 if (item->button_type == BUTTON_TYPE_TOGGLE)
1410 *group = NULL;
1411 if (utf8_key) w = gtk_check_menu_item_new ();
1412 else w = gtk_check_menu_item_new_with_label (utf8_label);
1413 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1415 else if (item->button_type == BUTTON_TYPE_RADIO)
1417 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1418 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1419 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1420 if (item->selected)
1421 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1423 else
1425 *group = NULL;
1426 if (utf8_key) w = gtk_menu_item_new ();
1427 else w = gtk_menu_item_new_with_label (utf8_label);
1430 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1431 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1433 return w;
1436 /* Return non-zero if LABEL specifies a separator (GTK only has one
1437 separator type) */
1438 static int
1439 xg_separator_p (char *label)
1441 if (! label) return 0;
1442 else if (strlen (label) > 3
1443 && strncmp (label, "--", 2) == 0
1444 && label[2] != '-')
1446 static char* separator_names[] = {
1447 "space",
1448 "no-line",
1449 "single-line",
1450 "double-line",
1451 "single-dashed-line",
1452 "double-dashed-line",
1453 "shadow-etched-in",
1454 "shadow-etched-out",
1455 "shadow-etched-in-dash",
1456 "shadow-etched-out-dash",
1457 "shadow-double-etched-in",
1458 "shadow-double-etched-out",
1459 "shadow-double-etched-in-dash",
1460 "shadow-double-etched-out-dash",
1464 int i;
1466 label += 2;
1467 for (i = 0; separator_names[i]; ++i)
1468 if (strcmp (label, separator_names[i]) == 0)
1469 return 1;
1471 else
1473 /* Old-style separator, maybe. It's a separator if it contains
1474 only dashes. */
1475 while (*label == '-')
1476 ++label;
1477 if (*label == 0) return 1;
1480 return 0;
1483 static int xg_detached_menus;
1485 /* Returns non-zero if there are detached menus. */
1487 xg_have_tear_offs ()
1489 return xg_detached_menus > 0;
1492 /* Callback invoked when a detached menu window is removed. Here we
1493 decrease the xg_detached_menus count.
1494 WIDGET is the top level window that is removed (the parent of the menu).
1495 CLIENT_DATA is not used. */
1496 static void
1497 tearoff_remove (widget, client_data)
1498 GtkWidget *widget;
1499 gpointer client_data;
1501 if (xg_detached_menus > 0) --xg_detached_menus;
1504 /* Callback invoked when a menu is detached. It increases the
1505 xg_detached_menus count.
1506 WIDGET is the GtkTearoffMenuItem.
1507 CLIENT_DATA is not used. */
1508 static void
1509 tearoff_activate (widget, client_data)
1510 GtkWidget *widget;
1511 gpointer client_data;
1513 GtkWidget *menu = gtk_widget_get_parent (widget);
1514 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1516 ++xg_detached_menus;
1517 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1518 "destroy",
1519 G_CALLBACK (tearoff_remove), 0);
1524 /* Create a menu item widget, and connect the callbacks.
1525 ITEM decribes the menu item.
1526 F is the frame the created menu belongs to.
1527 SELECT_CB is the callback to use when a menu item is selected.
1528 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1529 CL_DATA points to the callback data to be used for this menu.
1530 GROUP is an in/out parameter. If the menu item to be created is not
1531 part of any radio menu group, *GROUP contains NULL on entry and exit.
1532 If the menu item to be created is part of a radio menu group, on entry
1533 *GROUP contains the group to use, or NULL if this is the first item
1534 in the group. On exit, *GROUP contains the radio item group.
1536 Returns the created GtkWidget. */
1537 static GtkWidget *
1538 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1539 widget_value *item;
1540 FRAME_PTR f;
1541 GCallback select_cb;
1542 GCallback highlight_cb;
1543 xg_menu_cb_data *cl_data;
1544 GSList **group;
1546 char *utf8_label;
1547 char *utf8_key;
1548 GtkWidget *w;
1549 xg_menu_item_cb_data *cb_data;
1551 utf8_label = get_utf8_string (item->name);
1552 utf8_key = get_utf8_string (item->key);
1554 w = make_menu_item (utf8_label, utf8_key, item, group);
1556 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1557 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1559 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1561 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1563 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1564 cb_data->help = item->help;
1565 cb_data->cl_data = cl_data;
1566 cb_data->call_data = item->call_data;
1568 g_signal_connect (G_OBJECT (w),
1569 "destroy",
1570 G_CALLBACK (menuitem_destroy_callback),
1571 cb_data);
1573 /* Put cb_data in widget, so we can get at it when modifying menubar */
1574 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1576 /* final item, not a submenu */
1577 if (item->call_data && ! item->contents)
1579 if (select_cb)
1580 cb_data->select_id
1581 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1584 if (! NILP (item->help) && highlight_cb)
1586 /* We use enter/leave notify instead of select/deselect because
1587 select/deselect doesn't go well with detached menus. */
1588 cb_data->highlight_id
1589 = g_signal_connect (G_OBJECT (w),
1590 "enter-notify-event",
1591 G_CALLBACK (menuitem_highlight_callback),
1592 cb_data);
1593 cb_data->unhighlight_id
1594 = g_signal_connect (G_OBJECT (w),
1595 "leave-notify-event",
1596 G_CALLBACK (menuitem_highlight_callback),
1597 cb_data);
1600 return w;
1603 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1604 GCallback, GCallback, int, int, int,
1605 GtkWidget *, xg_menu_cb_data *, char *));
1607 /* Create a full menu tree specified by DATA.
1608 F is the frame the created menu belongs to.
1609 SELECT_CB is the callback to use when a menu item is selected.
1610 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1611 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1612 POP_UP_P is non-zero if we shall create a popup menu.
1613 MENU_BAR_P is non-zero if we shall create a menu bar.
1614 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1615 if MENU_BAR_P is non-zero.
1616 TOPMENU is the topmost GtkWidget that others shall be placed under.
1617 It may be NULL, in that case we create the appropriate widget
1618 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1619 CL_DATA is the callback data we shall use for this menu, or NULL
1620 if we haven't set the first callback yet.
1621 NAME is the name to give to the top level menu if this function
1622 creates it. May be NULL to not set any name.
1624 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1625 not NULL.
1627 This function calls itself to create submenus. */
1629 static GtkWidget *
1630 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1631 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1632 widget_value *data;
1633 FRAME_PTR f;
1634 GCallback select_cb;
1635 GCallback deactivate_cb;
1636 GCallback highlight_cb;
1637 int pop_up_p;
1638 int menu_bar_p;
1639 int add_tearoff_p;
1640 GtkWidget *topmenu;
1641 xg_menu_cb_data *cl_data;
1642 char *name;
1644 widget_value *item;
1645 GtkWidget *wmenu = topmenu;
1646 GSList *group = NULL;
1648 if (! topmenu)
1650 if (! menu_bar_p)
1652 wmenu = gtk_menu_new ();
1653 xg_set_screen (wmenu, f);
1655 else wmenu = gtk_menu_bar_new ();
1657 /* Put cl_data on the top menu for easier access. */
1658 cl_data = make_cl_data (cl_data, f, highlight_cb);
1659 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1660 g_signal_connect (G_OBJECT (wmenu), "destroy",
1661 G_CALLBACK (menu_destroy_callback), cl_data);
1663 if (name)
1664 gtk_widget_set_name (wmenu, name);
1666 if (deactivate_cb)
1667 g_signal_connect (G_OBJECT (wmenu),
1668 "deactivate", deactivate_cb, 0);
1670 g_signal_connect (G_OBJECT (wmenu),
1671 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1674 if (! menu_bar_p && add_tearoff_p)
1676 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1677 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1679 g_signal_connect (G_OBJECT (tearoff), "activate",
1680 G_CALLBACK (tearoff_activate), 0);
1683 for (item = data; item; item = item->next)
1685 GtkWidget *w;
1687 if (pop_up_p && !item->contents && !item->call_data
1688 && !xg_separator_p (item->name))
1690 char *utf8_label;
1691 /* A title for a popup. We do the same as GTK does when
1692 creating titles, but it does not look good. */
1693 group = NULL;
1694 utf8_label = get_utf8_string (item->name);
1696 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1697 w = gtk_menu_item_new_with_label (utf8_label);
1698 gtk_widget_set_sensitive (w, FALSE);
1699 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1701 else if (xg_separator_p (item->name))
1703 group = NULL;
1704 /* GTK only have one separator type. */
1705 w = gtk_separator_menu_item_new ();
1707 else
1709 w = xg_create_one_menuitem (item,
1711 item->contents ? 0 : select_cb,
1712 highlight_cb,
1713 cl_data,
1714 &group);
1716 if (item->contents)
1718 GtkWidget *submenu = create_menus (item->contents,
1720 select_cb,
1721 deactivate_cb,
1722 highlight_cb,
1725 add_tearoff_p,
1727 cl_data,
1729 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1733 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1734 gtk_widget_set_name (w, MENU_ITEM_NAME);
1737 return wmenu;
1740 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1741 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1742 with some text and buttons.
1743 F is the frame the created item belongs to.
1744 NAME is the name to use for the top widget.
1745 VAL is a widget_value structure describing items to be created.
1746 SELECT_CB is the callback to use when a menu item is selected or
1747 a dialog button is pressed.
1748 DEACTIVATE_CB is the callback to use when an item is deactivated.
1749 For a menu, when a sub menu is not shown anymore, for a dialog it is
1750 called when the dialog is popped down.
1751 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1753 Returns the widget created. */
1754 GtkWidget *
1755 xg_create_widget (type, name, f, val,
1756 select_cb, deactivate_cb, highlight_cb)
1757 char *type;
1758 char *name;
1759 FRAME_PTR f;
1760 widget_value *val;
1761 GCallback select_cb;
1762 GCallback deactivate_cb;
1763 GCallback highlight_cb;
1765 GtkWidget *w = 0;
1766 int menu_bar_p = strcmp (type, "menubar") == 0;
1767 int pop_up_p = strcmp (type, "popup") == 0;
1769 if (strcmp (type, "dialog") == 0)
1771 w = create_dialog (val, select_cb, deactivate_cb);
1772 xg_set_screen (w, f);
1773 gtk_window_set_transient_for (GTK_WINDOW (w),
1774 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1775 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1776 gtk_widget_set_name (w, "emacs-dialog");
1778 else if (menu_bar_p || pop_up_p)
1780 w = create_menus (val->contents,
1782 select_cb,
1783 deactivate_cb,
1784 highlight_cb,
1785 pop_up_p,
1786 menu_bar_p,
1787 menu_bar_p,
1790 name);
1792 /* Set the cursor to an arrow for popup menus when they are mapped.
1793 This is done by default for menu bar menus. */
1794 if (pop_up_p)
1796 /* Must realize so the GdkWindow inside the widget is created. */
1797 gtk_widget_realize (w);
1798 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
1801 else
1803 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1804 type);
1807 return w;
1810 /* Return the label for menu item WITEM. */
1811 static const char *
1812 xg_get_menu_item_label (witem)
1813 GtkMenuItem *witem;
1815 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1816 return gtk_label_get_label (wlabel);
1819 /* Return non-zero if the menu item WITEM has the text LABEL. */
1820 static int
1821 xg_item_label_same_p (witem, label)
1822 GtkMenuItem *witem;
1823 char *label;
1825 int is_same = 0;
1826 char *utf8_label = get_utf8_string (label);
1827 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1829 if (! old_label && ! utf8_label)
1830 is_same = 1;
1831 else if (old_label && utf8_label)
1832 is_same = strcmp (utf8_label, old_label) == 0;
1834 if (utf8_label && utf8_label != label) g_free (utf8_label);
1836 return is_same;
1839 /* Remove widgets in LIST from container WCONT. */
1840 static void
1841 remove_from_container (wcont, list)
1842 GtkWidget *wcont;
1843 GList *list;
1845 GList *iter;
1847 for (iter = list; iter; iter = g_list_next (iter))
1849 GtkWidget *w = GTK_WIDGET (iter->data);
1851 /* Add a ref to w so we can explicitly destroy it later. */
1852 gtk_widget_ref (w);
1853 gtk_container_remove (GTK_CONTAINER (wcont), w);
1855 /* If there is a menu under this widget that has been detached,
1856 there is a reference to it, and just removing w from the
1857 container does not destroy the submenu. By explicitly
1858 destroying w we make sure the submenu is destroyed, thus
1859 removing the detached window also if there was one. */
1860 gtk_widget_destroy (w);
1864 /* Update the top level names in MENUBAR (i.e. not submenus).
1865 F is the frame the menu bar belongs to.
1866 *LIST is a list with the current menu bar names (menu item widgets).
1867 ITER is the item within *LIST that shall be updated.
1868 POS is the numerical position, starting at 0, of ITER in *LIST.
1869 VAL describes what the menu bar shall look like after the update.
1870 SELECT_CB is the callback to use when a menu item is selected.
1871 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1872 CL_DATA points to the callback data to be used for this menu bar.
1874 This function calls itself to walk through the menu bar names. */
1875 static void
1876 xg_update_menubar (menubar, f, list, iter, pos, val,
1877 select_cb, highlight_cb, cl_data)
1878 GtkWidget *menubar;
1879 FRAME_PTR f;
1880 GList **list;
1881 GList *iter;
1882 int pos;
1883 widget_value *val;
1884 GCallback select_cb;
1885 GCallback highlight_cb;
1886 xg_menu_cb_data *cl_data;
1888 if (! iter && ! val)
1889 return;
1890 else if (iter && ! val)
1892 /* Item(s) have been removed. Remove all remaining items. */
1893 remove_from_container (menubar, iter);
1895 /* All updated. */
1896 val = 0;
1897 iter = 0;
1899 else if (! iter && val)
1901 /* Item(s) added. Add all new items in one call. */
1902 create_menus (val, f, select_cb, 0, highlight_cb,
1903 0, 1, 0, menubar, cl_data, 0);
1905 /* All updated. */
1906 val = 0;
1907 iter = 0;
1909 /* Below this neither iter or val is NULL */
1910 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
1912 /* This item is still the same, check next item. */
1913 val = val->next;
1914 iter = g_list_next (iter);
1915 ++pos;
1917 else /* This item is changed. */
1919 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
1920 GtkMenuItem *witem2 = 0;
1921 int val_in_menubar = 0;
1922 int iter_in_new_menubar = 0;
1923 GList *iter2;
1924 widget_value *cur;
1926 /* See if the changed entry (val) is present later in the menu bar */
1927 for (iter2 = iter;
1928 iter2 && ! val_in_menubar;
1929 iter2 = g_list_next (iter2))
1931 witem2 = GTK_MENU_ITEM (iter2->data);
1932 val_in_menubar = xg_item_label_same_p (witem2, val->name);
1935 /* See if the current entry (iter) is present later in the
1936 specification for the new menu bar. */
1937 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
1938 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
1940 if (val_in_menubar && ! iter_in_new_menubar)
1942 int nr = pos;
1944 /* This corresponds to:
1945 Current: A B C
1946 New: A C
1947 Remove B. */
1949 gtk_widget_ref (GTK_WIDGET (witem));
1950 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
1951 gtk_widget_destroy (GTK_WIDGET (witem));
1953 /* Must get new list since the old changed. */
1954 g_list_free (*list);
1955 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1956 while (nr-- > 0) iter = g_list_next (iter);
1958 else if (! val_in_menubar && ! iter_in_new_menubar)
1960 /* This corresponds to:
1961 Current: A B C
1962 New: A X C
1963 Rename B to X. This might seem to be a strange thing to do,
1964 since if there is a menu under B it will be totally wrong for X.
1965 But consider editing a C file. Then there is a C-mode menu
1966 (corresponds to B above).
1967 If then doing C-x C-f the minibuf menu (X above) replaces the
1968 C-mode menu. When returning from the minibuffer, we get
1969 back the C-mode menu. Thus we do:
1970 Rename B to X (C-mode to minibuf menu)
1971 Rename X to B (minibuf to C-mode menu).
1972 If the X menu hasn't been invoked, the menu under B
1973 is up to date when leaving the minibuffer. */
1974 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1975 char *utf8_label = get_utf8_string (val->name);
1976 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
1978 gtk_label_set_text (wlabel, utf8_label);
1980 /* If this item has a submenu that has been detached, change
1981 the title in the WM decorations also. */
1982 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
1983 /* Set the title of the detached window. */
1984 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
1986 iter = g_list_next (iter);
1987 val = val->next;
1988 ++pos;
1990 else if (! val_in_menubar && iter_in_new_menubar)
1992 /* This corresponds to:
1993 Current: A B C
1994 New: A X B C
1995 Insert X. */
1997 int nr = pos;
1998 GList *group = 0;
1999 GtkWidget *w = xg_create_one_menuitem (val,
2001 select_cb,
2002 highlight_cb,
2003 cl_data,
2004 &group);
2006 gtk_widget_set_name (w, MENU_ITEM_NAME);
2007 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2009 g_list_free (*list);
2010 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2011 while (nr-- > 0) iter = g_list_next (iter);
2012 iter = g_list_next (iter);
2013 val = val->next;
2014 ++pos;
2016 else /* if (val_in_menubar && iter_in_new_menubar) */
2018 int nr = pos;
2019 /* This corresponds to:
2020 Current: A B C
2021 New: A C B
2022 Move C before B */
2024 gtk_widget_ref (GTK_WIDGET (witem2));
2025 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2026 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2027 GTK_WIDGET (witem2), pos);
2028 gtk_widget_unref (GTK_WIDGET (witem2));
2030 g_list_free (*list);
2031 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2032 while (nr-- > 0) iter = g_list_next (iter);
2033 val = val->next;
2034 ++pos;
2038 /* Update the rest of the menu bar. */
2039 xg_update_menubar (menubar, f, list, iter, pos, val,
2040 select_cb, highlight_cb, cl_data);
2043 /* Update the menu item W so it corresponds to VAL.
2044 SELECT_CB is the callback to use when a menu item is selected.
2045 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2046 CL_DATA is the data to set in the widget for menu invokation. */
2047 static void
2048 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2049 widget_value *val;
2050 GtkWidget *w;
2051 GCallback select_cb;
2052 GCallback highlight_cb;
2053 xg_menu_cb_data *cl_data;
2055 GtkWidget *wchild;
2056 GtkLabel *wlbl = 0;
2057 GtkLabel *wkey = 0;
2058 char *utf8_label;
2059 char *utf8_key;
2060 const char *old_label = 0;
2061 const char *old_key = 0;
2062 xg_menu_item_cb_data *cb_data;
2064 wchild = gtk_bin_get_child (GTK_BIN (w));
2065 utf8_label = get_utf8_string (val->name);
2066 utf8_key = get_utf8_string (val->key);
2068 /* See if W is a menu item with a key. See make_menu_item above. */
2069 if (GTK_IS_HBOX (wchild))
2071 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2073 wlbl = GTK_LABEL (list->data);
2074 wkey = GTK_LABEL (list->next->data);
2075 g_list_free (list);
2077 if (! utf8_key)
2079 /* Remove the key and keep just the label. */
2080 gtk_widget_ref (GTK_WIDGET (wlbl));
2081 gtk_container_remove (GTK_CONTAINER (w), wchild);
2082 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2083 wkey = 0;
2087 else /* Just a label. */
2089 wlbl = GTK_LABEL (wchild);
2091 /* Check if there is now a key. */
2092 if (utf8_key)
2094 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2095 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2097 wlbl = GTK_LABEL (list->data);
2098 wkey = GTK_LABEL (list->next->data);
2099 g_list_free (list);
2101 gtk_container_remove (GTK_CONTAINER (w), wchild);
2102 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2107 if (wkey) old_key = gtk_label_get_label (wkey);
2108 if (wlbl) old_label = gtk_label_get_label (wlbl);
2110 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2111 gtk_label_set_text (wkey, utf8_key);
2113 if (! old_label || strcmp (utf8_label, old_label) != 0)
2114 gtk_label_set_text (wlbl, utf8_label);
2116 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2117 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2119 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2120 gtk_widget_set_sensitive (w, FALSE);
2121 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2122 gtk_widget_set_sensitive (w, TRUE);
2124 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2125 XG_ITEM_DATA);
2126 if (cb_data)
2128 cb_data->call_data = val->call_data;
2129 cb_data->help = val->help;
2130 cb_data->cl_data = cl_data;
2132 /* We assume the callback functions don't change. */
2133 if (val->call_data && ! val->contents)
2135 /* This item shall have a select callback. */
2136 if (! cb_data->select_id)
2137 cb_data->select_id
2138 = g_signal_connect (G_OBJECT (w), "activate",
2139 select_cb, cb_data);
2141 else if (cb_data->select_id)
2143 g_signal_handler_disconnect (w, cb_data->select_id);
2144 cb_data->select_id = 0;
2147 if (NILP (cb_data->help))
2149 /* Shall not have help. Remove if any existed previously. */
2150 if (cb_data->highlight_id)
2152 g_signal_handler_disconnect (G_OBJECT (w),
2153 cb_data->highlight_id);
2154 cb_data->highlight_id = 0;
2156 if (cb_data->unhighlight_id)
2158 g_signal_handler_disconnect (G_OBJECT (w),
2159 cb_data->unhighlight_id);
2160 cb_data->unhighlight_id = 0;
2163 else if (! cb_data->highlight_id && highlight_cb)
2165 /* Have help now, but didn't previously. Add callback. */
2166 cb_data->highlight_id
2167 = g_signal_connect (G_OBJECT (w),
2168 "enter-notify-event",
2169 G_CALLBACK (menuitem_highlight_callback),
2170 cb_data);
2171 cb_data->unhighlight_id
2172 = g_signal_connect (G_OBJECT (w),
2173 "leave-notify-event",
2174 G_CALLBACK (menuitem_highlight_callback),
2175 cb_data);
2180 /* Update the toggle menu item W so it corresponds to VAL. */
2181 static void
2182 xg_update_toggle_item (val, w)
2183 widget_value *val;
2184 GtkWidget *w;
2186 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2189 /* Update the radio menu item W so it corresponds to VAL. */
2190 static void
2191 xg_update_radio_item (val, w)
2192 widget_value *val;
2193 GtkWidget *w;
2195 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2198 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2199 SUBMENU may be NULL, in that case a new menu is created.
2200 F is the frame the menu bar belongs to.
2201 VAL describes the contents of the menu bar.
2202 SELECT_CB is the callback to use when a menu item is selected.
2203 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2204 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2205 CL_DATA is the call back data to use for any newly created items.
2207 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2208 was NULL. */
2210 static GtkWidget *
2211 xg_update_submenu (submenu, f, val,
2212 select_cb, deactivate_cb, highlight_cb, cl_data)
2213 GtkWidget *submenu;
2214 FRAME_PTR f;
2215 widget_value *val;
2216 GCallback select_cb;
2217 GCallback deactivate_cb;
2218 GCallback highlight_cb;
2219 xg_menu_cb_data *cl_data;
2221 GtkWidget *newsub = submenu;
2222 GList *list = 0;
2223 GList *iter;
2224 widget_value *cur;
2225 int has_tearoff_p = 0;
2226 GList *first_radio = 0;
2228 if (submenu)
2229 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2231 for (cur = val, iter = list;
2232 cur && iter;
2233 iter = g_list_next (iter), cur = cur->next)
2235 GtkWidget *w = GTK_WIDGET (iter->data);
2237 /* Skip tearoff items, they have no counterpart in val. */
2238 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2240 has_tearoff_p = 1;
2241 iter = g_list_next (iter);
2242 if (iter) w = GTK_WIDGET (iter->data);
2243 else break;
2246 /* Remember first radio button in a group. If we get a mismatch in
2247 a radio group we must rebuild the whole group so that the connections
2248 in GTK becomes correct. */
2249 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2250 first_radio = iter;
2251 else if (cur->button_type != BUTTON_TYPE_RADIO
2252 && ! GTK_IS_RADIO_MENU_ITEM (w))
2253 first_radio = 0;
2255 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2257 if (! xg_separator_p (cur->name))
2258 break;
2260 else if (GTK_IS_CHECK_MENU_ITEM (w))
2262 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2263 break;
2264 xg_update_toggle_item (cur, w);
2265 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2267 else if (GTK_IS_RADIO_MENU_ITEM (w))
2269 if (cur->button_type != BUTTON_TYPE_RADIO)
2270 break;
2271 xg_update_radio_item (cur, w);
2272 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2274 else if (GTK_IS_MENU_ITEM (w))
2276 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2277 GtkWidget *sub;
2279 if (cur->button_type != BUTTON_TYPE_NONE ||
2280 xg_separator_p (cur->name))
2281 break;
2283 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2285 sub = gtk_menu_item_get_submenu (witem);
2286 if (sub && ! cur->contents)
2288 /* Not a submenu anymore. */
2289 gtk_widget_ref (sub);
2290 gtk_menu_item_remove_submenu (witem);
2291 gtk_widget_destroy (sub);
2293 else if (cur->contents)
2295 GtkWidget *nsub;
2297 nsub = xg_update_submenu (sub, f, cur->contents,
2298 select_cb, deactivate_cb,
2299 highlight_cb, cl_data);
2301 /* If this item just became a submenu, we must set it. */
2302 if (nsub != sub)
2303 gtk_menu_item_set_submenu (witem, nsub);
2306 else
2308 /* Structural difference. Remove everything from here and down
2309 in SUBMENU. */
2310 break;
2314 /* Remove widgets from first structual change. */
2315 if (iter)
2317 /* If we are adding new menu items below, we must remove from
2318 first radio button so that radio groups become correct. */
2319 if (cur && first_radio) remove_from_container (submenu, first_radio);
2320 else remove_from_container (submenu, iter);
2323 if (cur)
2325 /* More items added. Create them. */
2326 newsub = create_menus (cur,
2328 select_cb,
2329 deactivate_cb,
2330 highlight_cb,
2333 ! has_tearoff_p,
2334 submenu,
2335 cl_data,
2339 if (list) g_list_free (list);
2341 return newsub;
2344 /* Update the MENUBAR.
2345 F is the frame the menu bar belongs to.
2346 VAL describes the contents of the menu bar.
2347 If DEEP_P is non-zero, rebuild all but the top level menu names in
2348 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2349 SELECT_CB is the callback to use when a menu item is selected.
2350 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2351 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2352 void
2353 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2354 select_cb, deactivate_cb, highlight_cb)
2355 GtkWidget *menubar;
2356 FRAME_PTR f;
2357 widget_value *val;
2358 int deep_p;
2359 GCallback select_cb;
2360 GCallback deactivate_cb;
2361 GCallback highlight_cb;
2363 xg_menu_cb_data *cl_data;
2364 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2366 if (! list) return;
2368 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2369 XG_FRAME_DATA);
2371 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2372 select_cb, highlight_cb, cl_data);
2374 if (deep_p);
2376 widget_value *cur;
2378 /* Update all sub menus.
2379 We must keep the submenus (GTK menu item widgets) since the
2380 X Window in the XEvent that activates the menu are those widgets. */
2382 /* Update cl_data, menu_item things in F may have changed. */
2383 update_cl_data (cl_data, f, highlight_cb);
2385 for (cur = val->contents; cur; cur = cur->next)
2387 GList *iter;
2388 GtkWidget *sub = 0;
2389 GtkWidget *newsub;
2390 GtkMenuItem *witem;
2392 /* Find sub menu that corresponds to val and update it. */
2393 for (iter = list ; iter; iter = g_list_next (iter))
2395 witem = GTK_MENU_ITEM (iter->data);
2396 if (xg_item_label_same_p (witem, cur->name))
2398 sub = gtk_menu_item_get_submenu (witem);
2399 break;
2403 newsub = xg_update_submenu (sub,
2405 cur->contents,
2406 select_cb,
2407 deactivate_cb,
2408 highlight_cb,
2409 cl_data);
2410 /* sub may still be NULL. If we just updated non deep and added
2411 a new menu bar item, it has no sub menu yet. So we set the
2412 newly created sub menu under witem. */
2413 if (newsub != sub)
2415 xg_set_screen (newsub, f);
2416 gtk_menu_item_set_submenu (witem, newsub);
2421 g_list_free (list);
2422 gtk_widget_show_all (menubar);
2425 /* Recompute all the widgets of frame F, when the menu bar has been
2426 changed. Value is non-zero if widgets were updated. */
2429 xg_update_frame_menubar (f)
2430 FRAME_PTR f;
2432 struct x_output *x = f->output_data.x;
2433 GtkRequisition req;
2435 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2436 return 0;
2438 BLOCK_INPUT;
2440 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2441 FALSE, FALSE, 0);
2442 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2444 gtk_widget_show_all (x->menubar_widget);
2445 gtk_widget_size_request (x->menubar_widget, &req);
2447 FRAME_MENUBAR_HEIGHT (f) = req.height;
2449 /* The height has changed, resize outer widget and set columns
2450 rows to what we had before adding the menu bar. */
2451 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2453 SET_FRAME_GARBAGED (f);
2454 UNBLOCK_INPUT;
2456 return 1;
2459 /* Get rid of the menu bar of frame F, and free its storage.
2460 This is used when deleting a frame, and when turning off the menu bar. */
2462 void
2463 free_frame_menubar (f)
2464 FRAME_PTR f;
2466 struct x_output *x = f->output_data.x;
2468 if (x->menubar_widget)
2470 BLOCK_INPUT;
2472 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2473 /* The menubar and its children shall be deleted when removed from
2474 the container. */
2475 x->menubar_widget = 0;
2476 FRAME_MENUBAR_HEIGHT (f) = 0;
2478 /* The height has changed, resize outer widget and set columns
2479 rows to what we had before removing the menu bar. */
2480 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
2482 SET_FRAME_GARBAGED (f);
2483 UNBLOCK_INPUT;
2489 /***********************************************************************
2490 Scroll bar functions
2491 ***********************************************************************/
2494 /* Setting scroll bar values invokes the callback. Use this variable
2495 to indicate that callback should do nothing. */
2496 int xg_ignore_gtk_scrollbar;
2498 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2499 32 bits. But we want to store pointers, and they may be larger
2500 than 32 bits. Keep a mapping from integer index to widget pointers
2501 to get around the 32 bit limitation. */
2502 static struct
2504 GtkWidget **widgets;
2505 int max_size;
2506 int used;
2507 } id_to_widget;
2509 /* Grow this much every time we need to allocate more */
2510 #define ID_TO_WIDGET_INCR 32
2512 /* Store the widget pointer W in id_to_widget and return the integer index. */
2513 static int
2514 xg_store_widget_in_map (w)
2515 GtkWidget *w;
2517 int i;
2519 if (id_to_widget.max_size == id_to_widget.used)
2521 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2523 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2524 sizeof (GtkWidget *)*new_size);
2526 for (i = id_to_widget.max_size; i < new_size; ++i)
2527 id_to_widget.widgets[i] = 0;
2528 id_to_widget.max_size = new_size;
2531 /* Just loop over the array and find a free place. After all,
2532 how many scroll bars are we creating? Should be a small number.
2533 The check above guarantees we will find a free place. */
2534 for (i = 0; i < id_to_widget.max_size; ++i)
2536 if (! id_to_widget.widgets[i])
2538 id_to_widget.widgets[i] = w;
2539 ++id_to_widget.used;
2541 return i;
2545 /* Should never end up here */
2546 abort ();
2549 /* Remove pointer at IDX from id_to_widget.
2550 Called when scroll bar is destroyed. */
2551 static void
2552 xg_remove_widget_from_map (idx)
2553 int idx;
2555 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2557 id_to_widget.widgets[idx] = 0;
2558 --id_to_widget.used;
2562 /* Get the widget pointer at IDX from id_to_widget. */
2563 static GtkWidget *
2564 xg_get_widget_from_map (idx)
2565 int idx;
2567 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2568 return id_to_widget.widgets[idx];
2570 return 0;
2573 /* Return the scrollbar id for X Window WID on display DPY.
2574 Return -1 if WID not in id_to_widget. */
2576 xg_get_scroll_id_for_window (dpy, wid)
2577 Display *dpy;
2578 Window wid;
2580 int idx;
2581 GtkWidget *w;
2583 w = xg_win_to_widget (dpy, wid);
2585 if (w)
2587 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2588 if (id_to_widget.widgets[idx] == w)
2589 return idx;
2592 return -1;
2595 /* Callback invoked when scroll bar WIDGET is destroyed.
2596 DATA is the index into id_to_widget for WIDGET.
2597 We free pointer to last scroll bar values here and remove the index. */
2598 static void
2599 xg_gtk_scroll_destroy (widget, data)
2600 GtkWidget *widget;
2601 gpointer data;
2603 gpointer p;
2604 int id = (int)data;
2606 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2607 if (p) xfree (p);
2608 xg_remove_widget_from_map (id);
2611 /* Callback for button press/release events. Used to start timer so that
2612 the scroll bar repetition timer in GTK gets handeled.
2613 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2614 WIDGET is the scroll bar widget the event is for (not used).
2615 EVENT contains the event.
2616 USER_DATA points to the struct scrollbar structure.
2618 Returns FALSE to tell GTK that it shall continue propagate the event
2619 to widgets. */
2620 static gboolean
2621 scroll_bar_button_cb (widget, event, user_data)
2622 GtkWidget *widget;
2623 GdkEventButton *event;
2624 gpointer user_data;
2626 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2627 xg_start_timer ();
2628 else if (event->type == GDK_BUTTON_RELEASE)
2630 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2631 if (xg_timer) xg_stop_timer ();
2632 bar->dragging = Qnil;
2635 return FALSE;
2638 /* Create a scroll bar widget for frame F. Store the scroll bar
2639 in BAR.
2640 SCROLL_CALLBACK is the callback to invoke when the value of the
2641 bar changes.
2642 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2643 to set resources for the widget. */
2644 void
2645 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2646 FRAME_PTR f;
2647 struct scroll_bar *bar;
2648 GCallback scroll_callback;
2649 char *scroll_bar_name;
2651 GtkWidget *wscroll;
2652 GtkObject *vadj;
2653 int scroll_id;
2655 /* Page, step increment values are not so important here, they
2656 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2657 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2658 0.1, 0.1, 0.1);
2660 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2661 gtk_widget_set_name (wscroll, scroll_bar_name);
2662 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2664 scroll_id = xg_store_widget_in_map (wscroll);
2666 g_signal_connect (G_OBJECT (wscroll),
2667 "value-changed",
2668 scroll_callback,
2669 (gpointer) bar);
2670 g_signal_connect (G_OBJECT (wscroll),
2671 "destroy",
2672 G_CALLBACK (xg_gtk_scroll_destroy),
2673 (gpointer) scroll_id);
2675 /* Connect to button press and button release to detect if any scroll bar
2676 has the pointer. */
2677 g_signal_connect (G_OBJECT (wscroll),
2678 "button-press-event",
2679 G_CALLBACK (scroll_bar_button_cb),
2680 (gpointer) bar);
2681 g_signal_connect (G_OBJECT (wscroll),
2682 "button-release-event",
2683 G_CALLBACK (scroll_bar_button_cb),
2684 (gpointer) bar);
2686 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget),
2687 wscroll, -1, -1);
2689 /* Set the cursor to an arrow. */
2690 xg_set_cursor (wscroll, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2692 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2695 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2696 void
2697 xg_show_scroll_bar (scrollbar_id)
2698 int scrollbar_id;
2700 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2701 if (w)
2702 gtk_widget_show (w);
2705 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2706 void
2707 xg_remove_scroll_bar (f, scrollbar_id)
2708 FRAME_PTR f;
2709 int scrollbar_id;
2711 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2712 if (w)
2714 gtk_widget_destroy (w);
2715 SET_FRAME_GARBAGED (f);
2719 /* Find left/top for widget W in GtkFixed widget WFIXED. */
2720 static void
2721 xg_find_top_left_in_fixed (w, wfixed, left, top)
2722 GtkWidget *w, *wfixed;
2723 int *left, *top;
2725 GList *iter;
2727 for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter))
2729 GtkFixedChild *child = (GtkFixedChild *) iter->data;
2731 if (child->widget == w)
2733 *left = child->x;
2734 *top = child->y;
2735 return;
2739 /* Shall never end up here. */
2740 abort ();
2743 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2744 in frame F.
2745 TOP/LEFT are the new pixel positions where the bar shall appear.
2746 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2747 void
2748 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height,
2749 real_left, canon_width)
2750 FRAME_PTR f;
2751 int scrollbar_id;
2752 int top;
2753 int left;
2754 int width;
2755 int height;
2758 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2760 if (wscroll)
2762 GtkWidget *wfixed = f->output_data.x->edit_widget;
2763 int winextra = canon_width > width ? (canon_width - width) / 2 : 0;
2764 int bottom = top + height;
2766 gint slider_width;
2767 int oldtop, oldleft, oldbottom;
2768 GtkRequisition req;
2770 /* Get old values. */
2771 xg_find_top_left_in_fixed (wscroll, wfixed, &oldleft, &oldtop);
2772 gtk_widget_size_request (wscroll, &req);
2773 oldbottom = oldtop + req.height;
2775 /* Scroll bars in GTK has a fixed width, so if we say width 16, it
2776 will only be its fixed width (14 is default) anyway, the rest is
2777 blank. We are drawing the mode line across scroll bars when
2778 the frame is split:
2779 |bar| |fringe|
2780 ----------------
2781 mode line
2782 ----------------
2783 |bar| |fringe|
2785 When we "unsplit" the frame:
2787 |bar| |fringe|
2788 -| |-| |
2789 m¦ |i| |
2790 -| |-| |
2791 | | | |
2794 the remains of the mode line can be seen in these blank spaces.
2795 So we must clear them explicitly.
2796 GTK scroll bars should do that, but they don't.
2797 Also, the canonical width may be wider than the width for the
2798 scroll bar so that there is some space (typically 1 pixel) between
2799 the scroll bar and the edge of the window and between the scroll
2800 bar and the fringe. */
2802 if (oldtop != -1 && oldleft != -1)
2804 int gtkextral, gtkextrah;
2805 int xl, xr, wbl, wbr;
2806 int bottomdiff, topdiff;
2808 gtk_widget_style_get (wscroll, "slider_width", &slider_width, NULL);
2809 gtkextral = width > slider_width ? (width - slider_width) / 2 : 0;
2810 gtkextrah = gtkextral ? (width - slider_width - gtkextral) : 0;
2812 xl = real_left;
2813 wbl = gtkextral + winextra;
2814 wbr = gtkextrah + winextra;
2815 xr = left + gtkextral + slider_width;
2816 bottomdiff = abs (oldbottom - bottom);
2817 topdiff = abs (oldtop - top);
2819 if (oldleft != left)
2821 gdk_window_clear_area (wfixed->window, xl, top, wbl, height);
2822 gdk_window_clear_area (wfixed->window, xr, top, wbr, height);
2825 if (oldtop > top)
2827 gdk_window_clear_area (wfixed->window, xl, top, wbl, topdiff);
2828 gdk_window_clear_area (wfixed->window, xr, top, wbr, topdiff);
2830 else if (oldtop < top)
2832 gdk_window_clear_area (wfixed->window, xl, oldtop, wbl, topdiff);
2833 gdk_window_clear_area (wfixed->window, xr, oldtop, wbr, topdiff);
2836 if (oldbottom > bottom)
2838 gdk_window_clear_area (wfixed->window, xl, bottom, wbl,
2839 bottomdiff);
2840 gdk_window_clear_area (wfixed->window, xr, bottom, wbr,
2841 bottomdiff);
2843 else if (oldbottom < bottom)
2845 gdk_window_clear_area (wfixed->window, xl, oldbottom, wbl,
2846 bottomdiff);
2847 gdk_window_clear_area (wfixed->window, xr, oldbottom, wbr,
2848 bottomdiff);
2852 /* Move and resize to new values. */
2853 gtk_fixed_move (GTK_FIXED (wfixed), wscroll, left, top);
2854 gtk_widget_set_size_request (wscroll, width, height);
2856 /* Must force out update so changed scroll bars gets redrawn. */
2857 gdk_window_process_all_updates ();
2859 SET_FRAME_GARBAGED (f);
2860 cancel_mouse_face (f);
2864 /* Set the thumb size and position of scroll bar BAR. We are currently
2865 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2866 void
2867 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2868 struct scroll_bar *bar;
2869 int portion, position, whole;
2871 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2873 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2875 if (wscroll && NILP (bar->dragging))
2877 GtkAdjustment *adj;
2878 gdouble shown;
2879 gdouble top;
2880 int size, value;
2881 int new_step;
2882 int changed = 0;
2884 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
2886 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2887 rather than the real portion value. This makes the thumb less likely
2888 to resize and that looks better. */
2889 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
2890 /* When the thumb is at the bottom, position == whole.
2891 So we need to increase `whole' to make space for the thumb. */
2892 whole += portion;
2894 if (whole <= 0)
2895 top = 0, shown = 1;
2896 else
2898 top = (gdouble) position / whole;
2899 shown = (gdouble) portion / whole;
2902 size = shown * XG_SB_RANGE;
2903 size = min (size, XG_SB_RANGE);
2904 size = max (size, 1);
2906 value = top * XG_SB_RANGE;
2907 value = min (value, XG_SB_MAX - size);
2908 value = max (value, XG_SB_MIN);
2910 /* Assume all lines are of equal size. */
2911 new_step = size / max (1, FRAME_LINES (f));
2913 if ((int) adj->page_size != size
2914 || (int) adj->step_increment != new_step)
2916 adj->page_size = size;
2917 adj->step_increment = new_step;
2918 /* Assume a page increment is about 95% of the page size */
2919 adj->page_increment = (int) (0.95*adj->page_size);
2920 changed = 1;
2923 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2925 GtkWidget *wfixed = f->output_data.x->edit_widget;
2927 BLOCK_INPUT;
2929 /* gtk_range_set_value invokes the callback. Set
2930 ignore_gtk_scrollbar to make the callback do nothing */
2931 xg_ignore_gtk_scrollbar = 1;
2933 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2934 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
2935 else if (changed)
2936 gtk_adjustment_changed (adj);
2938 xg_ignore_gtk_scrollbar = 0;
2940 UNBLOCK_INPUT;
2946 /***********************************************************************
2947 Tool bar functions
2948 ***********************************************************************/
2949 /* The key for the data we put in the GtkImage widgets. The data is
2950 the image used by Emacs. We use this to see if we need to update
2951 the GtkImage with a new image. */
2952 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2954 /* Callback function invoked when a tool bar item is pressed.
2955 W is the button widget in the tool bar that got pressed,
2956 CLIENT_DATA is an integer that is the index of the button in the
2957 tool bar. 0 is the first button. */
2958 static void
2959 xg_tool_bar_callback (w, client_data)
2960 GtkWidget *w;
2961 gpointer client_data;
2963 int idx = (int)client_data;
2964 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2965 Lisp_Object key, frame;
2966 struct input_event event;
2967 EVENT_INIT (event);
2969 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2970 return;
2972 idx *= TOOL_BAR_ITEM_NSLOTS;
2974 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
2975 XSETFRAME (frame, f);
2976 event.kind = TOOL_BAR_EVENT;
2977 event.frame_or_window = frame;
2978 event.arg = frame;
2979 kbd_buffer_store_event (&event);
2981 event.kind = TOOL_BAR_EVENT;
2982 event.frame_or_window = frame;
2983 event.arg = key;
2984 event.modifiers = 0; /* These are not available. */
2985 kbd_buffer_store_event (&event);
2988 /* This callback is called when a tool bar is detached. We must set
2989 the height of the tool bar to zero when this happens so frame sizes
2990 are correctly calculated.
2991 WBOX is the handle box widget that enables detach/attach of the tool bar.
2992 W is the tool bar widget.
2993 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2994 static void
2995 xg_tool_bar_detach_callback (wbox, w, client_data)
2996 GtkHandleBox *wbox;
2997 GtkWidget *w;
2998 gpointer client_data;
3000 FRAME_PTR f = (FRAME_PTR) client_data;
3002 if (f)
3004 /* When detaching a tool bar, not everything dissapear. There are
3005 a few pixels left that are used to drop the tool bar back into
3006 place. */
3007 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
3008 FRAME_TOOLBAR_HEIGHT (f) = 2;
3010 /* The height has changed, resize outer widget and set columns
3011 rows to what we had before detaching the tool bar. */
3012 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3016 /* This callback is called when a tool bar is reattached. We must set
3017 the height of the tool bar when this happens so frame sizes
3018 are correctly calculated.
3019 WBOX is the handle box widget that enables detach/attach of the tool bar.
3020 W is the tool bar widget.
3021 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3022 static void
3023 xg_tool_bar_attach_callback (wbox, w, client_data)
3024 GtkHandleBox *wbox;
3025 GtkWidget *w;
3026 gpointer client_data;
3028 FRAME_PTR f = (FRAME_PTR) client_data;
3030 if (f)
3032 GtkRequisition req;
3034 gtk_widget_size_request (w, &req);
3035 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3037 /* The height has changed, resize outer widget and set columns
3038 rows to what we had before detaching the tool bar. */
3039 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3043 /* This callback is called when the mouse enters or leaves a tool bar item.
3044 It is used for displaying and hiding the help text.
3045 W is the tool bar item, a button.
3046 EVENT is either an enter event or leave event.
3047 CLIENT_DATA is an integer that is the index of the button in the
3048 tool bar. 0 is the first button.
3050 Returns FALSE to tell GTK to keep processing this event. */
3051 static gboolean
3052 xg_tool_bar_help_callback (w, event, client_data)
3053 GtkWidget *w;
3054 GdkEventCrossing *event;
3055 gpointer client_data;
3057 int idx = (int)client_data;
3058 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3059 Lisp_Object help, frame;
3061 if (! GTK_IS_BUTTON (w))
3063 return FALSE;
3066 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3067 return FALSE;
3069 if (event->type == GDK_ENTER_NOTIFY)
3071 idx *= TOOL_BAR_ITEM_NSLOTS;
3072 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3074 if (NILP (help))
3075 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3077 else
3078 help = Qnil;
3080 XSETFRAME (frame, f);
3081 kbd_buffer_store_help_event (frame, help);
3083 return FALSE;
3087 /* This callback is called when a tool bar item shall be redrawn.
3088 It modifies the expose event so that the GtkImage widget redraws the
3089 whole image. This to overcome a bug that makes GtkImage draw the image
3090 in the wrong place when it tries to redraw just a part of the image.
3091 W is the GtkImage to be redrawn.
3092 EVENT is the expose event for W.
3093 CLIENT_DATA is unused.
3095 Returns FALSE to tell GTK to keep processing this event. */
3096 static gboolean
3097 xg_tool_bar_item_expose_callback (w, event, client_data)
3098 GtkWidget *w;
3099 GdkEventExpose *event;
3100 gpointer client_data;
3102 gint width, height;
3104 gdk_drawable_get_size (event->window, &width, &height);
3106 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3107 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3109 event->area.x = max (0, event->area.x);
3110 event->area.y = max (0, event->area.y);
3112 event->area.width = max (width, event->area.width);
3113 event->area.height = max (height, event->area.height);
3115 return FALSE;
3118 /* This callback is called when a tool bar shall be redrawn.
3119 We need to update the tool bar from here in case the image cache
3120 has deleted the pixmaps used in the tool bar.
3121 W is the GtkToolbar to be redrawn.
3122 EVENT is the expose event for W.
3123 CLIENT_DATA is pointing to the frame for this tool bar.
3125 Returns FALSE to tell GTK to keep processing this event. */
3126 static gboolean
3127 xg_tool_bar_expose_callback (w, event, client_data)
3128 GtkWidget *w;
3129 GdkEventExpose *event;
3130 gpointer client_data;
3132 update_frame_tool_bar ((FRAME_PTR) client_data);
3133 return FALSE;
3136 static void
3137 xg_create_tool_bar (f)
3138 FRAME_PTR f;
3140 struct x_output *x = f->output_data.x;
3141 GtkRequisition req;
3142 int vbox_pos = x->menubar_widget ? 1 : 0;
3144 x->toolbar_widget = gtk_toolbar_new ();
3145 x->handlebox_widget = gtk_handle_box_new ();
3146 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3147 x->toolbar_widget);
3149 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3150 FALSE, FALSE, 0);
3152 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3153 vbox_pos);
3155 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3157 /* We only have icons, so override any user setting. We could
3158 use the caption property of the toolbar item (see update_frame_tool_bar
3159 below), but some of those strings are long, making the toolbar so
3160 long it does not fit on the screen. The GtkToolbar widget makes every
3161 item equal size, so the longest caption determine the size of every
3162 tool bar item. I think the creators of the GtkToolbar widget
3163 counted on 4 or 5 character long strings. */
3164 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3165 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3166 GTK_ORIENTATION_HORIZONTAL);
3168 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3169 G_CALLBACK (xg_tool_bar_detach_callback), f);
3170 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3171 G_CALLBACK (xg_tool_bar_attach_callback), f);
3172 g_signal_connect (G_OBJECT (x->toolbar_widget),
3173 "expose-event",
3174 G_CALLBACK (xg_tool_bar_expose_callback),
3177 gtk_widget_show_all (x->handlebox_widget);
3179 gtk_widget_size_request (x->toolbar_widget, &req);
3180 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3182 /* The height has changed, resize outer widget and set columns
3183 rows to what we had before adding the tool bar. */
3184 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3186 SET_FRAME_GARBAGED (f);
3189 void
3190 update_frame_tool_bar (f)
3191 FRAME_PTR f;
3193 int i;
3194 GtkRequisition old_req, new_req;
3195 GList *icon_list;
3196 GList *iter;
3197 struct x_output *x = f->output_data.x;
3199 if (! FRAME_GTK_WIDGET (f))
3200 return;
3202 BLOCK_INPUT;
3204 if (! x->toolbar_widget)
3205 xg_create_tool_bar (f);
3207 gtk_widget_size_request (x->toolbar_widget, &old_req);
3209 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3210 iter = icon_list;
3212 for (i = 0; i < f->n_tool_bar_items; ++i)
3214 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3216 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3217 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3218 int idx;
3219 int img_id;
3220 struct image *img;
3221 Lisp_Object image;
3222 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3224 if (iter) iter = g_list_next (iter);
3226 /* If image is a vector, choose the image according to the
3227 button state. */
3228 image = PROP (TOOL_BAR_ITEM_IMAGES);
3229 if (VECTORP (image))
3231 if (enabled_p)
3232 idx = (selected_p
3233 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3234 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3235 else
3236 idx = (selected_p
3237 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3238 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3240 xassert (ASIZE (image) >= idx);
3241 image = AREF (image, idx);
3243 else
3244 idx = -1;
3246 /* Ignore invalid image specifications. */
3247 if (!valid_image_p (image))
3249 if (wicon) gtk_widget_hide (wicon);
3250 continue;
3253 img_id = lookup_image (f, image);
3254 img = IMAGE_FROM_ID (f, img_id);
3255 prepare_image_for_display (f, img);
3257 if (img->load_failed_p || img->pixmap == None)
3259 if (wicon) gtk_widget_hide (wicon);
3260 continue;
3263 if (! wicon)
3265 GdkPixmap *gpix;
3266 GdkBitmap *gmask;
3267 GtkWidget *w;
3269 xg_get_gdk_pixmap_and_mask (f, img, &gpix, &gmask);
3270 w = gtk_image_new_from_pixmap (gpix, gmask);
3271 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3272 0, 0, 0,
3274 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3275 (gpointer)i);
3277 /* Save the image so we can see if an update is needed when
3278 this function is called again. */
3279 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3280 (gpointer)img->pixmap);
3282 /* Catch expose events to overcome an annoying redraw bug, see
3283 comment for xg_tool_bar_item_expose_callback. */
3284 g_signal_connect (G_OBJECT (w),
3285 "expose-event",
3286 G_CALLBACK (xg_tool_bar_item_expose_callback),
3289 /* We must set sensitive on the button that is the parent
3290 of the GtkImage parent. Go upwards until we find the button. */
3291 while (! GTK_IS_BUTTON (w))
3292 w = gtk_widget_get_parent (w);
3294 if (w)
3296 /* Save the frame in the button so the xg_tool_bar_callback
3297 can get at it. */
3298 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3299 gtk_widget_set_sensitive (w, enabled_p);
3301 /* Use enter/leave notify to show help. We use the events
3302 rather than the GtkButton specific signals "enter" and
3303 "leave", so we can have only one callback. The event
3304 will tell us what kind of event it is. */
3305 g_signal_connect (G_OBJECT (w),
3306 "enter-notify-event",
3307 G_CALLBACK (xg_tool_bar_help_callback),
3308 (gpointer)i);
3309 g_signal_connect (G_OBJECT (w),
3310 "leave-notify-event",
3311 G_CALLBACK (xg_tool_bar_help_callback),
3312 (gpointer)i);
3315 else
3317 /* The child of the tool bar is a button. Inside that button
3318 is a vbox. Inside that vbox is the GtkImage. */
3319 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3320 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3321 GtkImage *wimage = GTK_IMAGE (chlist->data);
3322 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3323 XG_TOOL_BAR_IMAGE_DATA);
3324 g_list_free (chlist);
3326 if (old_img != img->pixmap)
3328 GdkPixmap *gpix;
3329 GdkBitmap *gmask;
3331 xg_get_gdk_pixmap_and_mask (f, img, &gpix, &gmask);
3332 gtk_image_set_from_pixmap (wimage, gpix, gmask);
3335 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3336 (gpointer)img->pixmap);
3338 gtk_widget_set_sensitive (wicon, enabled_p);
3339 gtk_widget_show (wicon);
3342 #undef PROP
3345 /* Remove buttons not longer needed. We just hide them so they
3346 can be reused later on. */
3347 while (iter)
3349 GtkWidget *w = GTK_WIDGET (iter->data);
3350 gtk_widget_hide (w);
3351 iter = g_list_next (iter);
3354 gtk_widget_size_request (x->toolbar_widget, &new_req);
3355 if (old_req.height != new_req.height)
3357 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3358 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3361 if (icon_list) g_list_free (icon_list);
3363 UNBLOCK_INPUT;
3366 void
3367 free_frame_tool_bar (f)
3368 FRAME_PTR f;
3370 struct x_output *x = f->output_data.x;
3372 if (x->toolbar_widget)
3374 BLOCK_INPUT;
3375 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3376 x->handlebox_widget);
3377 x->toolbar_widget = 0;
3378 x->handlebox_widget = 0;
3379 FRAME_TOOLBAR_HEIGHT (f) = 0;
3381 /* The height has changed, resize outer widget and set columns
3382 rows to what we had before removing the tool bar. */
3383 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
3385 SET_FRAME_GARBAGED (f);
3386 UNBLOCK_INPUT;
3392 /***********************************************************************
3393 Initializing
3394 ***********************************************************************/
3395 void
3396 xg_initialize ()
3398 xg_ignore_gtk_scrollbar = 0;
3399 xg_detached_menus = 0;
3400 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3401 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3403 id_to_widget.max_size = id_to_widget.used = 0;
3404 id_to_widget.widgets = 0;
3406 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3407 bindings. It doesn't seem to be any way to remove properties,
3408 so we set it to VoidSymbol which in X means "no key". */
3409 gtk_settings_set_string_property (gtk_settings_get_default (),
3410 "gtk-menu-bar-accel",
3411 "VoidSymbol",
3412 EMACS_CLASS);
3414 /* Make GTK text input widgets use Emacs style keybindings. This is
3415 Emacs after all. */
3416 gtk_settings_set_string_property (gtk_settings_get_default (),
3417 "gtk-key-theme-name",
3418 "Emacs",
3419 EMACS_CLASS);
3422 #endif /* USE_GTK */
3424 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3425 (do not change this comment) */