(f90-join-lines): Make it behave more like the standard function
[emacs.git] / src / gtkutil.c
blobfee560c20b4fa5818d8512df30cac2b486495494
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 <gdk/gdkkeysyms.h>
36 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
37 (PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
41 /***********************************************************************
42 Utility functions
43 ***********************************************************************/
44 /* The timer for scroll bar repetition and menu bar timeouts.
45 NULL if no timer is started. */
46 static struct atimer *xg_timer;
48 /* The cursor used for scroll bars and popup menus.
49 We only have one cursor for all scroll bars and all popup menus. */
50 static GdkCursor *xg_left_ptr_cursor;
53 /* The next two variables and functions are taken from lwlib. */
54 static widget_value *widget_value_free_list;
55 static int malloc_cpt;
57 /* Allocate a widget_value structure, either by taking one from the
58 widget_value_free_list or by malloc:ing a new one.
60 Return a pointer to the allocated structure. */
61 widget_value *
62 malloc_widget_value ()
64 widget_value *wv;
65 if (widget_value_free_list)
67 wv = widget_value_free_list;
68 widget_value_free_list = wv->free_list;
69 wv->free_list = 0;
71 else
73 wv = (widget_value *) malloc (sizeof (widget_value));
74 malloc_cpt++;
76 memset (wv, 0, sizeof (widget_value));
77 return wv;
80 /* This is analogous to free. It frees only what was allocated
81 by malloc_widget_value, and no substructures. */
82 void
83 free_widget_value (wv)
84 widget_value *wv;
86 if (wv->free_list)
87 abort ();
89 if (malloc_cpt > 25)
91 /* When the number of already allocated cells is too big,
92 We free it. */
93 free (wv);
94 malloc_cpt--;
96 else
98 wv->free_list = widget_value_free_list;
99 widget_value_free_list = wv;
103 /* Set *CURSOR on W and all widgets W contain. We must do like this
104 for scroll bars and menu because they create widgets internally,
105 and it is those widgets that are visible.
107 If *CURSOR is NULL, create a GDK_LEFT_PTR cursor and set *CURSOR to
108 the created cursor. */
109 void
110 xg_set_cursor (w, cursor)
111 GtkWidget *w;
112 GdkCursor **cursor;
114 GList *children = gdk_window_peek_children (w->window);
116 /* Create the cursor unless already created. */
117 if (! *cursor)
118 *cursor = gdk_cursor_new (GDK_LEFT_PTR);
120 gdk_window_set_cursor (w->window, *cursor);
122 /* The scroll bar widget has more than one GDK window (had to look at
123 the source to figure this out), and there is no way to set cursor
124 on widgets in GTK. So we must set the cursor for all GDK windows.
125 Ditto for menus. */
127 for ( ; children; children = g_list_next (children))
128 gdk_window_set_cursor (GDK_WINDOW (children->data), *cursor);
131 /* Timer function called when a timeout occurs for xg_timer.
132 This function processes all GTK events in a recursive event loop.
133 This is done because GTK timer events are not seen by Emacs event
134 detection, Emacs only looks for X events. When a scroll bar has the
135 pointer (detected by button press/release events below) an Emacs
136 timer is started, and this function can then check if the GTK timer
137 has expired by calling the GTK event loop.
138 Also, when a menu is active, it has a small timeout before it
139 pops down the sub menu under it. */
140 static void
141 xg_process_timeouts (timer)
142 struct atimer *timer;
144 BLOCK_INPUT;
145 /* Ideally we would like to just handle timer events, like the Xt version
146 of this does in xterm.c, but there is no such feature in GTK. */
147 while (gtk_events_pending ())
148 gtk_main_iteration ();
149 UNBLOCK_INPUT;
152 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
153 xg_process_timeouts is called when the timer expires. The timer
154 started is continuous, i.e. runs until xg_stop_timer is called. */
155 static void
156 xg_start_timer ()
158 if (! xg_timer)
160 EMACS_TIME interval;
161 EMACS_SET_SECS_USECS (interval, 0, 100000);
162 xg_timer = start_atimer (ATIMER_CONTINUOUS,
163 interval,
164 xg_process_timeouts,
169 /* Stop the xg_timer if started. */
170 static void
171 xg_stop_timer ()
173 if (xg_timer)
175 cancel_atimer (xg_timer);
176 xg_timer = 0;
180 /* Insert NODE into linked LIST. */
181 static void
182 xg_list_insert (xg_list_node *list, xg_list_node *node)
184 xg_list_node *list_start = list->next;
186 if (list_start) list_start->prev = node;
187 node->next = list_start;
188 node->prev = 0;
189 list->next = node;
192 /* Remove NODE from linked LIST. */
193 static void
194 xg_list_remove (xg_list_node *list, xg_list_node *node)
196 xg_list_node *list_start = list->next;
197 if (node == list_start)
199 list->next = node->next;
200 if (list->next) list->next->prev = 0;
202 else
204 node->prev->next = node->next;
205 if (node->next) node->next->prev = node->prev;
209 /* Allocate and return a utf8 version of STR. If STR is already
210 utf8 or NULL, just return STR.
211 If not, a new string is allocated and the caller must free the result
212 with g_free. */
213 static char *
214 get_utf8_string (str)
215 char *str;
217 char *utf8_str = str;
219 /* If not UTF-8, try current locale. */
220 if (str && !g_utf8_validate (str, -1, NULL))
221 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
223 return utf8_str;
228 /***********************************************************************
229 General functions for creating widgets, resizing, events, e.t.c.
230 ***********************************************************************/
232 /* Make a geometry string and pass that to GTK. It seems this is the
233 only way to get geometry position right if the user explicitly
234 asked for a position when starting Emacs.
235 F is the frame we shall set geometry for. */
236 static void
237 xg_set_geometry (f)
238 FRAME_PTR f;
240 if (f->output_data.x->size_hint_flags & USPosition)
242 int left = f->output_data.x->left_pos;
243 int xneg = f->output_data.x->size_hint_flags & XNegative;
244 int top = f->output_data.x->top_pos;
245 int yneg = f->output_data.x->size_hint_flags & YNegative;
246 char geom_str[32];
248 if (xneg)
249 left = -left;
250 if (yneg)
251 top = -top;
253 sprintf (geom_str, "=%dx%d%c%d%c%d",
254 PIXEL_WIDTH (f),
255 FRAME_TOTAL_PIXEL_HEIGHT (f),
256 (xneg ? '-' : '+'), left,
257 (yneg ? '-' : '+'), top);
259 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
260 geom_str))
261 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
266 /* Resize the outer window of frame F after chainging the height.
267 This happend when the menu bar or the tool bar is added or removed.
268 COLUMNS/ROWS is the size the edit area shall have after the resize. */
269 static void
270 xg_resize_outer_widget (f, columns, rows)
271 FRAME_PTR f;
272 int columns;
273 int rows;
275 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
276 PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
278 /* base_height is now changed. */
279 x_wm_set_size_hint (f, 0, 0);
281 /* If we are not mapped yet, set geometry once again, as window
282 height now have changed. */
283 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
284 xg_set_geometry (f);
286 xg_frame_set_char_size (f, columns, rows);
287 gdk_window_process_all_updates ();
290 /* This gets called after the frame F has been cleared. Since that is
291 done with X calls, we need to redraw GTK widget (scroll bars). */
292 void
293 xg_frame_cleared (f)
294 FRAME_PTR f;
296 GtkWidget *w = f->output_data.x->widget;
298 if (w)
300 gtk_container_set_reallocate_redraws (GTK_CONTAINER (w), TRUE);
301 gtk_container_foreach (GTK_CONTAINER (w),
302 (GtkCallback) gtk_widget_queue_draw,
304 gdk_window_process_all_updates ();
308 /* Function to handle resize of our widgets. Since Emacs has some layouts
309 that does not fit well with GTK standard containers, we do most layout
310 manually.
311 F is the frame to resize.
312 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
313 void
314 xg_resize_widgets (f, pixelwidth, pixelheight)
315 FRAME_PTR f;
316 int pixelwidth, pixelheight;
318 int mbheight = FRAME_MENUBAR_HEIGHT (f);
319 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
320 int rows = PIXEL_TO_CHAR_HEIGHT (f, pixelheight - mbheight - tbheight);
321 int columns = PIXEL_TO_CHAR_WIDTH (f, pixelwidth);
323 if (FRAME_GTK_WIDGET (f)
324 && (columns != FRAME_WIDTH (f) || rows != FRAME_HEIGHT (f)
325 || pixelwidth != PIXEL_WIDTH (f) || pixelheight != PIXEL_HEIGHT (f)))
327 struct x_output *x = f->output_data.x;
328 GtkAllocation all;
330 all.y = mbheight + tbheight;
331 all.x = 0;
333 all.width = pixelwidth;
334 all.height = pixelheight - mbheight - tbheight;
336 gtk_widget_size_allocate (x->edit_widget, &all);
338 change_frame_size (f, rows, columns, 0, 1, 0);
339 SET_FRAME_GARBAGED (f);
340 cancel_mouse_face (f);
345 /* Update our widget size to be COLS/ROWS characters for frame F. */
346 void
347 xg_frame_set_char_size (f, cols, rows)
348 FRAME_PTR f;
349 int cols;
350 int rows;
352 int pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows)
353 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
354 int pixelwidth;
356 /* Take into account the size of the scroll bar. Always use the
357 number of columns occupied by the scroll bar here otherwise we
358 might end up with a frame width that is not a multiple of the
359 frame's character width which is bad for vertically split
360 windows. */
361 f->output_data.x->vertical_scroll_bar_extra
362 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
364 : (FRAME_SCROLL_BAR_COLS (f)
365 * FONT_WIDTH (f->output_data.x->font)));
367 compute_fringe_widths (f, 0);
369 /* CHAR_TO_PIXEL_WIDTH uses vertical_scroll_bar_extra, so call it
370 after calculating that value. */
371 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
373 /* Must resize our top level widget. Font size may have changed,
374 but not rows/cols. */
375 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
376 pixelwidth, pixelheight);
377 xg_resize_widgets (f, pixelwidth, pixelheight);
379 SET_FRAME_GARBAGED (f);
380 cancel_mouse_face (f);
383 /* Convert an X Window WSESC to its corresponding GtkWidget.
384 Must be done like this, because GtkWidget:s can have "hidden"
385 X Window that aren't accessible.
387 Return 0 if no widget match WDESC. */
388 GtkWidget *
389 xg_win_to_widget (wdesc)
390 Window wdesc;
392 gpointer gdkwin;
393 GtkWidget *gwdesc = 0;
395 BLOCK_INPUT;
396 gdkwin = gdk_xid_table_lookup (wdesc);
397 if (gdkwin)
399 GdkEvent event;
400 event.any.window = gdkwin;
401 gwdesc = gtk_get_event_widget (&event);
404 UNBLOCK_INPUT;
405 return gwdesc;
408 /* Fill in the GdkColor C so that it represents PIXEL.
409 W is the widget that color will be used for. Used to find colormap. */
410 static void
411 xg_pix_to_gcolor (w, pixel, c)
412 GtkWidget *w;
413 unsigned long pixel;
414 GdkColor *c;
416 GdkColormap *map = gtk_widget_get_colormap (w);
417 gdk_colormap_query_color (map, pixel, c);
420 /* Turning off double buffering for our GtkFixed widget has the side
421 effect of turning it off also for its children (scroll bars).
422 But we want those to be double buffered to not flicker so handle
423 expose manually here.
424 WIDGET is the GtkFixed widget that gets exposed.
425 EVENT is the expose event.
426 USER_DATA is unused.
428 Return TRUE to tell GTK that this expose event has been fully handeled
429 and that GTK shall do nothing more with it. */
430 static gboolean
431 xg_fixed_handle_expose(GtkWidget *widget,
432 GdkEventExpose *event,
433 gpointer user_data)
435 GList *iter;
437 for (iter = GTK_FIXED (widget)->children; iter; iter = g_list_next (iter))
439 GtkFixedChild *child_data = (GtkFixedChild *) iter->data;
440 GtkWidget *child = child_data->widget;
441 GdkWindow *window = child->window;
442 GdkRegion *region = gtk_widget_region_intersect (child, event->region);
444 if (! gdk_region_empty (region))
446 GdkEvent child_event;
447 child_event.expose = *event;
448 child_event.expose.region = region;
450 /* Turn on double buffering, i.e. draw to an off screen area. */
451 gdk_window_begin_paint_region (window, region);
453 /* Tell child to redraw itself. */
454 gdk_region_get_clipbox (region, &child_event.expose.area);
455 gtk_widget_send_expose (child, &child_event);
456 gdk_window_process_updates (window, TRUE);
458 /* Copy off screen area to the window. */
459 gdk_window_end_paint (window);
462 gdk_region_destroy (region);
465 return TRUE;
468 /* Create and set up the GTK widgets for frame F.
469 Return 0 if creation failed, non-zero otherwise. */
471 xg_create_frame_widgets (f)
472 FRAME_PTR f;
474 GtkWidget *wtop;
475 GtkWidget *wvbox;
476 GtkWidget *wfixed;
477 GdkColor bg;
478 GtkRcStyle *style;
479 int i;
480 char *title = 0;
482 BLOCK_INPUT;
484 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
485 wvbox = gtk_vbox_new (FALSE, 0);
486 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
488 if (! wtop || ! wvbox || ! wfixed)
490 if (wtop) gtk_widget_destroy (wtop);
491 if (wvbox) gtk_widget_destroy (wvbox);
492 if (wfixed) gtk_widget_destroy (wfixed);
494 return 0;
497 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
498 gtk_widget_set_name (wtop, EMACS_CLASS);
499 gtk_widget_set_name (wvbox, "pane");
500 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
502 /* If this frame has a title or name, set it in the title bar. */
503 if (! NILP (f->title)) title = SDATA (f->title);
504 else if (! NILP (f->name)) title = SDATA (f->name);
506 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
508 FRAME_GTK_OUTER_WIDGET (f) = wtop;
509 FRAME_GTK_WIDGET (f) = wfixed;
510 f->output_data.x->vbox_widget = wvbox;
512 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
514 gtk_widget_set_size_request (wfixed,
515 PIXEL_WIDTH (f),
516 PIXEL_HEIGHT (f));
518 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
519 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
521 if (FRAME_EXTERNAL_TOOL_BAR (f))
522 update_frame_tool_bar (f);
524 /* The tool bar is created but first there are no items in it.
525 This causes it to be zero height. Later items are added, but then
526 the frame is already mapped, so there is a "jumping" resize.
527 This makes geometry handling difficult, for example -0-0 will end
528 up in the wrong place as tool bar height has not been taken into account.
529 So we cheat a bit by setting a height that is what it will have
530 later on when tool bar items are added. */
531 if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0)
532 FRAME_TOOLBAR_HEIGHT (f) = 34;
535 /* We don't want this widget double buffered, because we draw on it
536 with regular X drawing primitives, so from a GTK/GDK point of
537 view, the widget is totally blank. When an expose comes, this
538 will make the widget blank, and then Emacs redraws it. This flickers
539 a lot, so we turn off double buffering. */
540 gtk_widget_set_double_buffered (wfixed, FALSE);
542 /* Turning off double buffering above has the side effect of turning
543 it off also for its children (scroll bars). But we want those
544 to be double buffered to not flicker so handle expose manually. */
545 g_signal_connect (G_OBJECT (wfixed), "expose-event",
546 G_CALLBACK (xg_fixed_handle_expose), 0);
548 /* GTK documents says use gtk_window_set_resizable. But then a user
549 can't shrink the window from its starting size. */
550 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
551 gtk_window_set_wmclass (GTK_WINDOW (wtop),
552 SDATA (Vx_resource_name),
553 SDATA (Vx_resource_class));
555 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
556 GTK is to destroy the widget. We want Emacs to do that instead. */
557 g_signal_connect (G_OBJECT (wtop), "delete-event",
558 G_CALLBACK (gtk_true), 0);
560 /* Convert our geometry parameters into a geometry string
561 and specify it.
562 GTK will itself handle calculating the real position this way. */
563 xg_set_geometry (f);
565 gtk_widget_add_events (wfixed,
566 GDK_POINTER_MOTION_MASK
567 | GDK_EXPOSURE_MASK
568 | GDK_BUTTON_PRESS_MASK
569 | GDK_BUTTON_RELEASE_MASK
570 | GDK_KEY_PRESS_MASK
571 | GDK_ENTER_NOTIFY_MASK
572 | GDK_LEAVE_NOTIFY_MASK
573 | GDK_FOCUS_CHANGE_MASK
574 | GDK_STRUCTURE_MASK
575 | GDK_VISIBILITY_NOTIFY_MASK);
577 /* Must realize the windows so the X window gets created. It is used
578 by callers of this function. */
579 gtk_widget_realize (wfixed);
580 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
582 /* Since GTK clears its window by filling with the background color,
583 we must keep X and GTK background in sync. */
584 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
585 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
587 /* Also, do not let any background pixmap to be set, this looks very
588 bad as Emacs overwrites the background pixmap with its own idea
589 of background color. */
590 style = gtk_widget_get_modifier_style (wfixed);
592 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
593 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
594 gtk_widget_modify_style (wfixed, style);
596 /* GTK does not set any border, and they look bad with GTK. */
597 f->output_data.x->border_width = 0;
598 f->output_data.x->internal_border_width = 0;
600 UNBLOCK_INPUT;
602 return 1;
605 /* Set the normal size hints for the window manager, for frame F.
606 FLAGS is the flags word to use--or 0 meaning preserve the flags
607 that the window now has.
608 If USER_POSITION is nonzero, we set the User Position
609 flag (this is useful when FLAGS is 0). */
610 void
611 x_wm_set_size_hint (f, flags, user_position)
612 FRAME_PTR f;
613 long flags;
614 int user_position;
616 if (FRAME_GTK_OUTER_WIDGET (f))
618 /* Must use GTK routines here, otherwise GTK resets the size hints
619 to its own defaults. */
620 GdkGeometry size_hints;
621 gint hint_flags = 0;
622 int base_width, base_height;
623 int min_rows = 0, min_cols = 0;
624 int win_gravity = f->output_data.x->win_gravity;
626 if (flags)
628 memset (&size_hints, 0, sizeof (size_hints));
629 f->output_data.x->size_hints = size_hints;
630 f->output_data.x->hint_flags = hint_flags;
632 else
633 flags = f->output_data.x->size_hint_flags;
635 size_hints = f->output_data.x->size_hints;
636 hint_flags = f->output_data.x->hint_flags;
638 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
639 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
640 size_hints.height_inc = f->output_data.x->line_height;
642 hint_flags |= GDK_HINT_BASE_SIZE;
643 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
644 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0)
645 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
647 check_frame_size (f, &min_rows, &min_cols);
649 size_hints.base_width = base_width;
650 size_hints.base_height = base_height;
651 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
652 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
655 /* These currently have a one to one mapping with the X values, but I
656 don't think we should rely on that. */
657 hint_flags |= GDK_HINT_WIN_GRAVITY;
658 size_hints.win_gravity = 0;
659 if (win_gravity == NorthWestGravity)
660 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
661 else if (win_gravity == NorthGravity)
662 size_hints.win_gravity = GDK_GRAVITY_NORTH;
663 else if (win_gravity == NorthEastGravity)
664 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
665 else if (win_gravity == WestGravity)
666 size_hints.win_gravity = GDK_GRAVITY_WEST;
667 else if (win_gravity == CenterGravity)
668 size_hints.win_gravity = GDK_GRAVITY_CENTER;
669 else if (win_gravity == EastGravity)
670 size_hints.win_gravity = GDK_GRAVITY_EAST;
671 else if (win_gravity == SouthWestGravity)
672 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
673 else if (win_gravity == SouthGravity)
674 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
675 else if (win_gravity == SouthEastGravity)
676 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
677 else if (win_gravity == StaticGravity)
678 size_hints.win_gravity = GDK_GRAVITY_STATIC;
680 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
681 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
682 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
684 if (user_position)
686 hint_flags &= ~GDK_HINT_POS;
687 hint_flags |= GDK_HINT_USER_POS;
690 BLOCK_INPUT;
692 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
693 FRAME_GTK_OUTER_WIDGET (f),
694 &size_hints,
695 hint_flags);
697 f->output_data.x->size_hints = size_hints;
698 f->output_data.x->hint_flags = hint_flags;
699 UNBLOCK_INPUT;
703 /* Change background color of a frame.
704 Since GTK uses the background colour to clear the window, we must
705 keep the GTK and X colors in sync.
706 F is the frame to change,
707 BG is the pixel value to change to. */
708 void
709 xg_set_background_color (f, bg)
710 FRAME_PTR f;
711 unsigned long bg;
713 if (FRAME_GTK_WIDGET (f))
715 GdkColor gdk_bg;
717 BLOCK_INPUT;
718 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
719 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
720 UNBLOCK_INPUT;
726 /***********************************************************************
727 Dialog functions
728 ***********************************************************************/
729 /* Return the dialog title to use for a dialog of type KEY.
730 This is the encoding used by lwlib. We use the same for GTK. */
731 static char *
732 get_dialog_title (char key)
734 char *title = "";
736 switch (key) {
737 case 'E': case 'e':
738 title = "Error";
739 break;
741 case 'I': case 'i':
742 title = "Information";
743 break;
745 case 'L': case 'l':
746 title = "Prompt";
747 break;
749 case 'P': case 'p':
750 title = "Prompt";
751 break;
753 case 'Q': case 'q':
754 title = "Question";
755 break;
758 return title;
761 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
762 the dialog, but return TRUE so the event does not propagate further
763 in GTK. This prevents GTK from destroying the dialog widget automatically
764 and we can always destrou the widget manually, regardles of how
765 it was popped down (button press or WM_DELETE_WINDOW).
766 W is the dialog widget.
767 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
768 user_data is NULL (not used).
770 Returns TRUE to end propagation of event. */
771 static gboolean
772 dialog_delete_callback (w, event, user_data)
773 GtkWidget *w;
774 GdkEvent *event;
775 gpointer user_data;
777 gtk_widget_unmap (w);
778 return TRUE;
781 /* Create a popup dialog window. See also xg_create_widget below.
782 WV is a widget_value describing the dialog.
783 SELECT_CB is the callback to use when a button has been pressed.
784 DEACTIVATE_CB is the callback to use when the dialog pops down.
786 Returns the GTK dialog widget. */
787 static GtkWidget *
788 create_dialog (wv, select_cb, deactivate_cb)
789 widget_value *wv;
790 GCallback select_cb;
791 GCallback deactivate_cb;
793 char *title = get_dialog_title (wv->name[0]);
794 int total_buttons = wv->name[1] - '0';
795 int right_buttons = wv->name[4] - '0';
796 int left_buttons;
797 int button_nr = 0;
798 int button_spacing = 10;
799 GtkWidget *wdialog = gtk_dialog_new ();
800 widget_value *item;
801 GtkBox *cur_box;
802 GtkWidget *wvbox;
803 GtkWidget *whbox_up;
804 GtkWidget *whbox_down;
806 /* If the number of buttons is greater than 4, make two rows of buttons
807 instead. This looks better. */
808 int make_two_rows = total_buttons > 4;
810 if (right_buttons == 0) right_buttons = total_buttons/2;
811 left_buttons = total_buttons - right_buttons;
813 gtk_window_set_title (GTK_WINDOW (wdialog), title);
814 gtk_widget_set_name (wdialog, "emacs-dialog");
816 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
818 if (make_two_rows)
820 wvbox = gtk_vbox_new (TRUE, button_spacing);
821 whbox_up = gtk_hbox_new (FALSE, 0);
822 whbox_down = gtk_hbox_new (FALSE, 0);
824 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
825 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
826 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
828 cur_box = GTK_BOX (whbox_up);
831 g_signal_connect (G_OBJECT (wdialog), "delete-event",
832 G_CALLBACK (dialog_delete_callback), 0);
834 if (deactivate_cb)
836 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
837 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
840 for (item = wv->contents; item; item = item->next)
842 char *utf8_label = get_utf8_string (item->value);
843 GtkWidget *w;
844 GtkRequisition req;
846 if (item->name && strcmp (item->name, "message") == 0)
848 /* This is the text part of the dialog. */
849 w = gtk_label_new (utf8_label);
850 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
851 gtk_label_new (""),
852 FALSE, FALSE, 0);
853 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
854 TRUE, TRUE, 0);
855 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
857 /* Try to make dialog look better. Must realize first so
858 the widget can calculate the size it needs. */
859 gtk_widget_realize (w);
860 gtk_widget_size_request (w, &req);
861 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
862 req.height);
863 if (item->value && strlen (item->value) > 0)
864 button_spacing = 2*req.width/strlen (item->value);
866 else
868 /* This is one button to add to the dialog. */
869 w = gtk_button_new_with_mnemonic (utf8_label);
870 if (! item->enabled)
871 gtk_widget_set_sensitive (w, FALSE);
872 if (select_cb)
873 g_signal_connect (G_OBJECT (w), "clicked",
874 select_cb, item->call_data);
876 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
877 if (++button_nr == left_buttons)
879 if (make_two_rows)
880 cur_box = GTK_BOX (whbox_down);
881 else
882 gtk_box_pack_start (cur_box,
883 gtk_label_new (""),
884 TRUE, TRUE,
885 button_spacing);
889 if (utf8_label && utf8_label != item->value)
890 g_free (utf8_label);
893 return wdialog;
897 enum
899 XG_FILE_NOT_DONE,
900 XG_FILE_OK,
901 XG_FILE_CANCEL,
902 XG_FILE_DESTROYED,
905 /* Callback function invoked when the Ok button is pressed in
906 a file dialog.
907 W is the file dialog widget,
908 ARG points to an integer where we record what has happend. */
909 static void
910 xg_file_sel_ok (w, arg)
911 GtkWidget *w;
912 gpointer arg;
914 *(int*)arg = XG_FILE_OK;
917 /* Callback function invoked when the Cancel button is pressed in
918 a file dialog.
919 W is the file dialog widget,
920 ARG points to an integer where we record what has happend. */
921 static void
922 xg_file_sel_cancel (w, arg)
923 GtkWidget *w;
924 gpointer arg;
926 *(int*)arg = XG_FILE_CANCEL;
929 /* Callback function invoked when the file dialog is destroyed (i.e.
930 popped down). We must keep track of this, because if this
931 happens, GTK destroys the widget. But if for example, Ok is pressed,
932 the dialog is popped down, but the dialog widget is not destroyed.
933 W is the file dialog widget,
934 ARG points to an integer where we record what has happend. */
935 static void
936 xg_file_sel_destroy (w, arg)
937 GtkWidget *w;
938 gpointer arg;
940 *(int*)arg = XG_FILE_DESTROYED;
943 /* Read a file name from the user using a file dialog.
944 F is the current frame.
945 PROMPT is a prompt to show to the user. May not be NULL.
946 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
947 If MUSTMATCH_P is non-zero, the returned file name must be an existing
948 file.
950 Returns a file name or NULL if no file was selected.
951 The returned string must be freed by the caller. */
952 char *
953 xg_get_file_name (f, prompt, default_filename, mustmatch_p)
954 FRAME_PTR f;
955 char *prompt;
956 char *default_filename;
957 int mustmatch_p;
959 GtkWidget *filewin;
960 GtkFileSelection *filesel;
961 int filesel_done = XG_FILE_NOT_DONE;
962 char *fn = 0;
964 filewin = gtk_file_selection_new (prompt);
965 filesel = GTK_FILE_SELECTION (filewin);
967 gtk_widget_set_name (filewin, "emacs-filedialog");
969 gtk_window_set_transient_for (GTK_WINDOW (filewin),
970 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
971 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
973 g_signal_connect (G_OBJECT (filesel->ok_button),
974 "clicked",
975 G_CALLBACK (xg_file_sel_ok),
976 &filesel_done);
977 g_signal_connect (G_OBJECT (filesel->cancel_button),
978 "clicked",
979 G_CALLBACK (xg_file_sel_cancel),
980 &filesel_done);
981 g_signal_connect (G_OBJECT (filesel),
982 "destroy",
983 G_CALLBACK (xg_file_sel_destroy),
984 &filesel_done);
986 if (default_filename)
987 gtk_file_selection_set_filename (filesel, default_filename);
989 if (mustmatch_p)
991 /* The selection_entry part of filesel is not documented. */
992 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
993 gtk_file_selection_hide_fileop_buttons (filesel);
997 gtk_widget_show_all (filewin);
999 while (filesel_done == XG_FILE_NOT_DONE)
1000 gtk_main_iteration ();
1002 if (filesel_done == XG_FILE_OK)
1003 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1005 if (filesel_done != XG_FILE_DESTROYED)
1006 gtk_widget_destroy (filewin);
1008 return fn;
1012 /***********************************************************************
1013 Menu functions.
1014 ***********************************************************************/
1016 /* The name of menu items that can be used for citomization. Since GTK
1017 RC files are very crude and primitive, we have to set this on all
1018 menu item names so a user can easily cutomize menu items. */
1020 #define MENU_ITEM_NAME "emacs-menuitem"
1023 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1024 during GC. The next member points to the items. */
1025 static xg_list_node xg_menu_cb_list;
1027 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1028 during GC. The next member points to the items. */
1029 static xg_list_node xg_menu_item_cb_list;
1031 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1032 F is the frame CL_DATA will be initialized for.
1033 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1035 The menu bar and all sub menus under the menu bar in a frame
1036 share the same structure, hence the reference count.
1038 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1039 allocated xg_menu_cb_data if CL_DATA is NULL. */
1040 static xg_menu_cb_data *
1041 make_cl_data (cl_data, f, highlight_cb)
1042 xg_menu_cb_data *cl_data;
1043 FRAME_PTR f;
1044 GCallback highlight_cb;
1046 if (! cl_data)
1048 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1049 cl_data->f = f;
1050 cl_data->menu_bar_vector = f->menu_bar_vector;
1051 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1052 cl_data->highlight_cb = highlight_cb;
1053 cl_data->ref_count = 0;
1055 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1058 cl_data->ref_count++;
1060 return cl_data;
1063 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1064 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1066 When the menu bar is updated, menu items may have been added and/or
1067 removed, so menu_bar_vector and menu_bar_items_used change. We must
1068 then update CL_DATA since it is used to determine which menu
1069 item that is invoked in the menu.
1070 HIGHLIGHT_CB could change, there is no check that the same
1071 function is given when modifying a menu bar as was given when
1072 creating the menu bar. */
1073 static void
1074 update_cl_data (cl_data, f, highlight_cb)
1075 xg_menu_cb_data *cl_data;
1076 FRAME_PTR f;
1077 GCallback highlight_cb;
1079 if (cl_data)
1081 cl_data->f = f;
1082 cl_data->menu_bar_vector = f->menu_bar_vector;
1083 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1084 cl_data->highlight_cb = highlight_cb;
1088 /* Decrease reference count for CL_DATA.
1089 If reference count is zero, free CL_DATA. */
1090 static void
1091 unref_cl_data (cl_data)
1092 xg_menu_cb_data *cl_data;
1094 if (cl_data && cl_data->ref_count > 0)
1096 cl_data->ref_count--;
1097 if (cl_data->ref_count == 0)
1099 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1100 xfree (cl_data);
1105 /* Function that marks all lisp data during GC. */
1106 void
1107 xg_mark_data ()
1109 xg_list_node *iter;
1111 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1112 mark_object (&((xg_menu_cb_data *) iter)->menu_bar_vector);
1114 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1116 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1118 if (! NILP (cb_data->help))
1119 mark_object (&cb_data->help);
1124 /* Callback called when a menu item is destroyed. Used to free data.
1125 W is the widget that is being destroyed (not used).
1126 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1127 static void
1128 menuitem_destroy_callback (w, client_data)
1129 GtkWidget *w;
1130 gpointer client_data;
1132 if (client_data)
1134 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1135 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1136 xfree (data);
1140 /* Callback called when the pointer enters/leaves a menu item.
1141 W is the menu item.
1142 EVENT is either an enter event or leave event.
1143 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1145 Returns FALSE to tell GTK to keep processing this event. */
1146 static gboolean
1147 menuitem_highlight_callback (w, event, client_data)
1148 GtkWidget *w;
1149 GdkEventCrossing *event;
1150 gpointer client_data;
1152 if (client_data)
1154 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1155 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1157 if (! NILP (data->help) && data->cl_data->highlight_cb)
1159 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1160 (*func) (w, call_data);
1164 return FALSE;
1167 /* Callback called when a menu is destroyed. Used to free data.
1168 W is the widget that is being destroyed (not used).
1169 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1170 static void
1171 menu_destroy_callback (w, client_data)
1172 GtkWidget *w;
1173 gpointer client_data;
1175 unref_cl_data ((xg_menu_cb_data*) client_data);
1178 /* Callback called when a menu does a grab or ungrab. That means the
1179 menu has been activated or deactivated.
1180 Used to start a timer so the small timeout the menus in GTK uses before
1181 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1182 W is the widget that does the grab (not used).
1183 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1184 CLIENT_DATA is NULL (not used). */
1185 static void
1186 menu_grab_callback (GtkWidget *widget,
1187 gboolean ungrab_p,
1188 gpointer client_data)
1190 /* Keep track of total number of grabs. */
1191 static int cnt;
1193 if (ungrab_p) cnt--;
1194 else cnt++;
1196 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1197 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1200 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1201 must be non-NULL) and can be inserted into a menu item.
1203 Returns the GtkHBox. */
1204 static GtkWidget *
1205 make_widget_for_menu_item (utf8_label, utf8_key)
1206 char *utf8_label;
1207 char *utf8_key;
1209 GtkWidget *wlbl;
1210 GtkWidget *wkey;
1211 GtkWidget *wbox;
1213 wbox = gtk_hbox_new (FALSE, 0);
1214 wlbl = gtk_label_new_with_mnemonic (utf8_label);
1215 wkey = gtk_label_new (utf8_key);
1217 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1218 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1220 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1221 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1223 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1224 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1225 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1227 return wbox;
1230 /* Make and return a menu item widget with the key to the right.
1231 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1232 UTF8_KEY is the text representing the key binding.
1233 ITEM is the widget_value describing the menu item.
1235 GROUP is an in/out parameter. If the menu item to be created is not
1236 part of any radio menu group, *GROUP contains NULL on entry and exit.
1237 If the menu item to be created is part of a radio menu group, on entry
1238 *GROUP contains the group to use, or NULL if this is the first item
1239 in the group. On exit, *GROUP contains the radio item group.
1241 Unfortunately, keys don't line up as nicely as in Motif,
1242 but the MacOS X version doesn't either, so I guess that is OK. */
1243 static GtkWidget *
1244 make_menu_item (utf8_label, utf8_key, item, group)
1245 char *utf8_label;
1246 char *utf8_key;
1247 widget_value *item;
1248 GSList **group;
1250 GtkWidget *w;
1251 GtkWidget *wtoadd = 0;
1253 if (utf8_key)
1254 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1256 if (item->button_type == BUTTON_TYPE_TOGGLE)
1258 *group = NULL;
1259 if (utf8_key) w = gtk_check_menu_item_new ();
1260 else w = gtk_check_menu_item_new_with_mnemonic (utf8_label);
1261 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1263 else if (item->button_type == BUTTON_TYPE_RADIO)
1265 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1266 else w = gtk_radio_menu_item_new_with_mnemonic (*group, utf8_label);
1267 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1268 if (item->selected)
1269 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1271 else
1273 *group = NULL;
1274 if (utf8_key) w = gtk_menu_item_new ();
1275 else w = gtk_menu_item_new_with_mnemonic (utf8_label);
1278 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1279 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1281 return w;
1284 /* Return non-zero if LABEL specifies a separator (GTK only has one
1285 separator type) */
1286 static int
1287 xg_separator_p (char *label)
1289 if (! label) return 0;
1290 else if (strlen (label) > 3
1291 && strncmp (label, "--", 2) == 0
1292 && label[2] != '-')
1294 static char* separator_names[] = {
1295 "space",
1296 "no-line",
1297 "single-line",
1298 "double-line",
1299 "single-dashed-line",
1300 "double-dashed-line",
1301 "shadow-etched-in",
1302 "shadow-etched-out",
1303 "shadow-etched-in-dash",
1304 "shadow-etched-out-dash",
1305 "shadow-double-etched-in",
1306 "shadow-double-etched-out",
1307 "shadow-double-etched-in-dash",
1308 "shadow-double-etched-out-dash",
1312 int i;
1314 label += 2;
1315 for (i = 0; separator_names[i]; ++i)
1316 if (strcmp (label, separator_names[i]) == 0)
1317 return 1;
1319 else
1321 /* Old-style separator, maybe. It's a separator if it contains
1322 only dashes. */
1323 while (*label == '-')
1324 ++label;
1325 if (*label == 0) return 1;
1328 return 0;
1331 GtkWidget *xg_did_tearoff;
1333 /* Callback invoked when a detached menu window is removed. Here we
1334 delete the popup menu.
1335 WIDGET is the top level window that is removed (the parent of the menu).
1336 EVENT is the event that triggers the window removal.
1337 CLIENT_DATA points to the menu that is detached.
1339 Returns TRUE to tell GTK to stop processing this event. */
1340 static gboolean
1341 tearoff_remove (widget, event, client_data)
1342 GtkWidget *widget;
1343 GdkEvent *event;
1344 gpointer client_data;
1346 gtk_widget_destroy (GTK_WIDGET (client_data));
1347 return TRUE;
1350 /* Callback invoked when a menu is detached. It sets the xg_did_tearoff
1351 variable.
1352 WIDGET is the GtkTearoffMenuItem.
1353 CLIENT_DATA is not used. */
1354 static void
1355 tearoff_activate (widget, client_data)
1356 GtkWidget *widget;
1357 gpointer client_data;
1359 GtkWidget *menu = gtk_widget_get_parent (widget);
1360 if (! gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1361 return;
1363 xg_did_tearoff = menu;
1366 /* If a detach of a popup menu is done, this function should be called
1367 to keep the menu around until the detached window is removed.
1368 MENU is the top level menu for the popup,
1369 SUBMENU is the menu that got detached (that is MENU or a
1370 submenu of MENU), see the xg_did_tearoff variable. */
1371 void
1372 xg_keep_popup (menu, submenu)
1373 GtkWidget *menu;
1374 GtkWidget *submenu;
1376 GtkWidget *p;
1378 /* Find the top widget for the detached menu. */
1379 p = gtk_widget_get_toplevel (submenu);
1381 /* Delay destroying the menu until the detached menu is removed. */
1382 g_signal_connect (G_OBJECT (p), "unmap_event",
1383 G_CALLBACK (tearoff_remove), menu);
1386 /* Create a menu item widget, and connect the callbacks.
1387 ITEM decribes the menu item.
1388 F is the frame the created menu belongs to.
1389 SELECT_CB is the callback to use when a menu item is selected.
1390 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1391 CL_DATA points to the callback data to be used for this menu.
1392 GROUP is an in/out parameter. If the menu item to be created is not
1393 part of any radio menu group, *GROUP contains NULL on entry and exit.
1394 If the menu item to be created is part of a radio menu group, on entry
1395 *GROUP contains the group to use, or NULL if this is the first item
1396 in the group. On exit, *GROUP contains the radio item group.
1398 Returns the created GtkWidget. */
1399 static GtkWidget *
1400 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1401 widget_value *item;
1402 FRAME_PTR f;
1403 GCallback select_cb;
1404 GCallback highlight_cb;
1405 xg_menu_cb_data *cl_data;
1406 GSList **group;
1408 char *utf8_label;
1409 char *utf8_key;
1410 GtkWidget *w;
1411 xg_menu_item_cb_data *cb_data;
1413 utf8_label = get_utf8_string (item->name);
1414 utf8_key = get_utf8_string (item->key);
1416 w = make_menu_item (utf8_label, utf8_key, item, group);
1418 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1419 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1421 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1423 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1425 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1426 cb_data->help = item->help;
1427 cb_data->cl_data = cl_data;
1428 cb_data->call_data = item->call_data;
1430 g_signal_connect (G_OBJECT (w),
1431 "destroy",
1432 G_CALLBACK (menuitem_destroy_callback),
1433 cb_data);
1435 /* Put cb_data in widget, so we can get at it when modifying menubar */
1436 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1438 /* final item, not a submenu */
1439 if (item->call_data && ! item->contents)
1441 if (select_cb)
1442 cb_data->select_id
1443 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1446 if (! NILP (item->help) && highlight_cb)
1448 /* We use enter/leave notify instead of select/deselect because
1449 select/deselect doesn't go well with detached menus. */
1450 cb_data->highlight_id
1451 = g_signal_connect (G_OBJECT (w),
1452 "enter-notify-event",
1453 G_CALLBACK (menuitem_highlight_callback),
1454 cb_data);
1455 cb_data->unhighlight_id
1456 = g_signal_connect (G_OBJECT (w),
1457 "leave-notify-event",
1458 G_CALLBACK (menuitem_highlight_callback),
1459 cb_data);
1462 return w;
1465 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1466 GCallback, GCallback, int, int, int,
1467 GtkWidget *, xg_menu_cb_data *, char *));
1469 /* Create a full menu tree specified by DATA.
1470 F is the frame the created menu belongs to.
1471 SELECT_CB is the callback to use when a menu item is selected.
1472 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1473 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1474 POP_UP_P is non-zero if we shall create a popup menu.
1475 MENU_BAR_P is non-zero if we shall create a menu bar.
1476 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1477 if MENU_BAR_P is non-zero.
1478 TOPMENU is the topmost GtkWidget that others shall be placed under.
1479 It may be NULL, in that case we create the appropriate widget
1480 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1481 CL_DATA is the callback data we shall use for this menu, or NULL
1482 if we haven't set the first callback yet.
1483 NAME is the name to give to the top level menu if this function
1484 creates it. May be NULL to not set any name.
1486 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1487 not NULL.
1489 This function calls itself to create submenus. */
1491 static GtkWidget *
1492 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1493 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1494 widget_value *data;
1495 FRAME_PTR f;
1496 GCallback select_cb;
1497 GCallback deactivate_cb;
1498 GCallback highlight_cb;
1499 int pop_up_p;
1500 int menu_bar_p;
1501 int add_tearoff_p;
1502 GtkWidget *topmenu;
1503 xg_menu_cb_data *cl_data;
1504 char *name;
1506 widget_value *item;
1507 GtkWidget *wmenu = topmenu;
1508 GSList *group = NULL;
1510 if (! topmenu)
1512 if (! menu_bar_p) wmenu = gtk_menu_new ();
1513 else wmenu = gtk_menu_bar_new ();
1515 /* Put cl_data on the top menu for easier access. */
1516 cl_data = make_cl_data (cl_data, f, highlight_cb);
1517 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1518 g_signal_connect (G_OBJECT (wmenu), "destroy",
1519 G_CALLBACK (menu_destroy_callback), cl_data);
1521 if (name)
1522 gtk_widget_set_name (wmenu, name);
1524 if (deactivate_cb)
1525 g_signal_connect (G_OBJECT (wmenu),
1526 "deactivate", deactivate_cb, 0);
1528 g_signal_connect (G_OBJECT (wmenu),
1529 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1532 if (! menu_bar_p && add_tearoff_p)
1534 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1535 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1537 g_signal_connect (G_OBJECT (tearoff), "activate",
1538 G_CALLBACK (tearoff_activate), 0);
1541 for (item = data; item; item = item->next)
1543 GtkWidget *w;
1545 if (pop_up_p && !item->contents && !item->call_data
1546 && !xg_separator_p (item->name))
1548 char *utf8_label;
1549 /* A title for a popup. We do the same as GTK does when
1550 creating titles, but it does not look good. */
1551 group = NULL;
1552 utf8_label = get_utf8_string (item->name);
1554 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1555 w = gtk_menu_item_new_with_mnemonic (utf8_label);
1556 gtk_widget_set_sensitive (w, FALSE);
1557 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1559 else if (xg_separator_p (item->name))
1561 group = NULL;
1562 /* GTK only have one separator type. */
1563 w = gtk_separator_menu_item_new ();
1565 else
1567 w = xg_create_one_menuitem (item,
1569 item->contents ? 0 : select_cb,
1570 highlight_cb,
1571 cl_data,
1572 &group);
1574 if (item->contents)
1576 GtkWidget *submenu = create_menus (item->contents,
1578 select_cb,
1579 deactivate_cb,
1580 highlight_cb,
1585 cl_data,
1587 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1591 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1592 gtk_widget_set_name (w, MENU_ITEM_NAME);
1595 return wmenu;
1598 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1599 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1600 with some text and buttons.
1601 F is the frame the created item belongs to.
1602 NAME is the name to use for the top widget.
1603 VAL is a widget_value structure describing items to be created.
1604 SELECT_CB is the callback to use when a menu item is selected or
1605 a dialog button is pressed.
1606 DEACTIVATE_CB is the callback to use when an item is deactivated.
1607 For a menu, when a sub menu is not shown anymore, for a dialog it is
1608 called when the dialog is popped down.
1609 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1611 Returns the widget created. */
1612 GtkWidget *
1613 xg_create_widget (type, name, f, val,
1614 select_cb, deactivate_cb, highlight_cb)
1615 char *type;
1616 char *name;
1617 FRAME_PTR f;
1618 widget_value *val;
1619 GCallback select_cb;
1620 GCallback deactivate_cb;
1621 GCallback highlight_cb;
1623 GtkWidget *w = 0;
1624 if (strcmp (type, "dialog") == 0)
1626 w = create_dialog (val, select_cb, deactivate_cb);
1627 gtk_window_set_transient_for (GTK_WINDOW (w),
1628 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1629 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1631 if (w)
1632 gtk_widget_set_name (w, "emacs-dialog");
1634 else if (strcmp (type, "menubar") == 0 || strcmp (type, "popup") == 0)
1636 w = create_menus (val->contents,
1638 select_cb,
1639 deactivate_cb,
1640 highlight_cb,
1641 strcmp (type, "popup") == 0,
1642 strcmp (type, "menubar") == 0,
1646 name);
1648 /* Set the cursor to an arrow for popup menus when they are mapped.
1649 This is done by default for menu bar menus. */
1650 if (strcmp (type, "popup") == 0)
1652 /* Must realize so the GdkWindow inside the widget is created. */
1653 gtk_widget_realize (w);
1654 xg_set_cursor (w, &xg_left_ptr_cursor);
1657 else
1659 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1660 type);
1663 return w;
1666 /* Return the label for menu item WITEM. */
1667 static const char *
1668 xg_get_menu_item_label (witem)
1669 GtkMenuItem *witem;
1671 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1672 return gtk_label_get_label (wlabel);
1675 /* Return non-zero if the menu item WITEM has the text LABEL. */
1676 static int
1677 xg_item_label_same_p (witem, label)
1678 GtkMenuItem *witem;
1679 char *label;
1681 int is_same = 0;
1682 char *utf8_label = get_utf8_string (label);
1683 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1685 if (! old_label && ! utf8_label)
1686 is_same = 1;
1687 else if (old_label && utf8_label)
1688 is_same = strcmp (utf8_label, old_label) == 0;
1690 if (utf8_label && utf8_label != label) g_free (utf8_label);
1692 return is_same;
1695 /* Remove widgets in LIST from container WCONT. */
1696 static void
1697 remove_from_container (wcont, list)
1698 GtkWidget *wcont;
1699 GList *list;
1701 GList *iter;
1703 for (iter = list; iter; iter = g_list_next (iter))
1705 GtkWidget *w = GTK_WIDGET (iter->data);
1707 /* Add a ref to w so we can explicitly destroy it later. */
1708 gtk_widget_ref (w);
1709 gtk_container_remove (GTK_CONTAINER (wcont), w);
1711 /* If there is a menu under this widget that has been detached,
1712 there is a reference to it, and just removing w from the
1713 container does not destroy the submenu. By explicitly
1714 destroying w we make sure the submenu is destroyed, thus
1715 removing the detached window also if there was one. */
1716 gtk_widget_destroy (w);
1720 /* Update the top level names in MENUBAR (i.e. not submenus).
1721 F is the frame the menu bar belongs to.
1722 *LIST is a list with the current menu bar names (menu item widgets).
1723 ITER is the item within *LIST that shall be updated.
1724 POS is the numerical position, starting at 0, of ITER in *LIST.
1725 VAL describes what the menu bar shall look like after the update.
1726 SELECT_CB is the callback to use when a menu item is selected.
1727 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1728 CL_DATA points to the callback data to be used for this menu bar.
1730 This function calls itself to walk through the menu bar names. */
1731 static void
1732 xg_update_menubar (menubar, f, list, iter, pos, val,
1733 select_cb, highlight_cb, cl_data)
1734 GtkWidget *menubar;
1735 FRAME_PTR f;
1736 GList **list;
1737 GList *iter;
1738 int pos;
1739 widget_value *val;
1740 GCallback select_cb;
1741 GCallback highlight_cb;
1742 xg_menu_cb_data *cl_data;
1744 if (! iter && ! val)
1745 return;
1746 else if (iter && ! val)
1748 /* Item(s) have been removed. Remove all remaining items. */
1749 remove_from_container (menubar, iter);
1751 /* All updated. */
1752 val = 0;
1753 iter = 0;
1755 else if (! iter && val)
1757 /* Item(s) added. Add all new items in one call. */
1758 create_menus (val, f, select_cb, 0, highlight_cb,
1759 0, 1, 0, menubar, cl_data, 0);
1761 /* All updated. */
1762 val = 0;
1763 iter = 0;
1765 /* Below this neither iter or val is NULL */
1766 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
1768 /* This item is still the same, check next item. */
1769 val = val->next;
1770 iter = g_list_next (iter);
1771 ++pos;
1773 else /* This item is changed. */
1775 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
1776 GtkMenuItem *witem2 = 0;
1777 int val_in_menubar = 0;
1778 int iter_in_new_menubar = 0;
1779 GList *iter2;
1780 widget_value *cur;
1782 /* See if the changed entry (val) is present later in the menu bar */
1783 for (iter2 = iter;
1784 iter2 && ! val_in_menubar;
1785 iter2 = g_list_next (iter2))
1787 witem2 = GTK_MENU_ITEM (iter2->data);
1788 val_in_menubar = xg_item_label_same_p (witem2, val->name);
1791 /* See if the current entry (iter) is present later in the
1792 specification for the new menu bar. */
1793 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
1794 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
1796 if (val_in_menubar && ! iter_in_new_menubar)
1798 int nr = pos;
1800 /* This corresponds to:
1801 Current: A B C
1802 New: A C
1803 Remove B. */
1805 gtk_widget_ref (GTK_WIDGET (witem));
1806 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
1807 gtk_widget_destroy (GTK_WIDGET (witem));
1809 /* Must get new list since the old changed. */
1810 g_list_free (*list);
1811 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1812 while (nr-- > 0) iter = g_list_next (iter);
1814 else if (! val_in_menubar && ! iter_in_new_menubar)
1816 /* This corresponds to:
1817 Current: A B C
1818 New: A X C
1819 Rename B to X. This might seem to be a strange thing to do,
1820 since if there is a menu under B it will be totally wrong for X.
1821 But consider editing a C file. Then there is a C-mode menu
1822 (corresponds to B above).
1823 If then doing C-x C-f the minibuf menu (X above) replaces the
1824 C-mode menu. When returning from the minibuffer, we get
1825 back the C-mode menu. Thus we do:
1826 Rename B to X (C-mode to minibuf menu)
1827 Rename X to B (minibuf to C-mode menu).
1828 If the X menu hasn't been invoked, the menu under B
1829 is up to date when leaving the minibuffer. */
1830 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1831 char *utf8_label = get_utf8_string (val->name);
1833 gtk_label_set_text_with_mnemonic (wlabel, utf8_label);
1835 iter = g_list_next (iter);
1836 val = val->next;
1837 ++pos;
1839 else if (! val_in_menubar && iter_in_new_menubar)
1841 /* This corresponds to:
1842 Current: A B C
1843 New: A X B C
1844 Insert X. */
1846 int nr = pos;
1847 GList *group = 0;
1848 GtkWidget *w = xg_create_one_menuitem (val,
1850 select_cb,
1851 highlight_cb,
1852 cl_data,
1853 &group);
1855 gtk_widget_set_name (w, MENU_ITEM_NAME);
1856 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
1858 g_list_free (*list);
1859 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1860 while (nr-- > 0) iter = g_list_next (iter);
1861 iter = g_list_next (iter);
1862 val = val->next;
1863 ++pos;
1865 else /* if (val_in_menubar && iter_in_new_menubar) */
1867 int nr = pos;
1868 /* This corresponds to:
1869 Current: A B C
1870 New: A C B
1871 Move C before B */
1873 gtk_widget_ref (GTK_WIDGET (witem2));
1874 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
1875 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
1876 GTK_WIDGET (witem2), pos);
1877 gtk_widget_unref (GTK_WIDGET (witem2));
1879 g_list_free (*list);
1880 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1881 while (nr-- > 0) iter = g_list_next (iter);
1882 val = val->next;
1883 ++pos;
1887 /* Update the rest of the menu bar. */
1888 xg_update_menubar (menubar, f, list, iter, pos, val,
1889 select_cb, highlight_cb, cl_data);
1892 /* Update the menu item W so it corresponds to VAL.
1893 SELECT_CB is the callback to use when a menu item is selected.
1894 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1895 CL_DATA is the data to set in the widget for menu invokation. */
1896 static void
1897 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
1898 widget_value *val;
1899 GtkWidget *w;
1900 GCallback select_cb;
1901 GCallback highlight_cb;
1902 xg_menu_cb_data *cl_data;
1904 GtkWidget *wchild;
1905 GtkLabel *wlbl = 0;
1906 GtkLabel *wkey = 0;
1907 char *utf8_label;
1908 char *utf8_key;
1909 const char *old_label = 0;
1910 const char *old_key = 0;
1911 xg_menu_item_cb_data *cb_data;
1913 wchild = gtk_bin_get_child (GTK_BIN (w));
1914 utf8_label = get_utf8_string (val->name);
1915 utf8_key = get_utf8_string (val->key);
1917 /* See if W is a menu item with a key. See make_menu_item above. */
1918 if (GTK_IS_HBOX (wchild))
1920 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
1922 wlbl = GTK_LABEL (list->data);
1923 wkey = GTK_LABEL (list->next->data);
1924 g_list_free (list);
1926 if (! utf8_key)
1928 /* Remove the key and keep just the label. */
1929 gtk_widget_ref (GTK_WIDGET (wlbl));
1930 gtk_container_remove (GTK_CONTAINER (w), wchild);
1931 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
1932 wkey = 0;
1936 else /* Just a label. */
1938 wlbl = GTK_LABEL (wchild);
1940 /* Check if there is now a key. */
1941 if (utf8_key)
1943 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1944 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
1946 wlbl = GTK_LABEL (list->data);
1947 wkey = GTK_LABEL (list->next->data);
1948 g_list_free (list);
1950 gtk_container_remove (GTK_CONTAINER (w), wchild);
1951 gtk_container_add (GTK_CONTAINER (w), wtoadd);
1956 if (wkey) old_key = gtk_label_get_label (wkey);
1957 if (wlbl) old_label = gtk_label_get_label (wlbl);
1959 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
1960 gtk_label_set_text (wkey, utf8_key);
1962 if (! old_label || strcmp (utf8_label, old_label) != 0)
1963 gtk_label_set_text_with_mnemonic (wlbl, utf8_label);
1965 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
1966 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
1968 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
1969 gtk_widget_set_sensitive (w, FALSE);
1970 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
1971 gtk_widget_set_sensitive (w, TRUE);
1973 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
1974 XG_ITEM_DATA);
1975 if (cb_data)
1977 cb_data->call_data = val->call_data;
1978 cb_data->help = val->help;
1979 cb_data->cl_data = cl_data;
1981 /* We assume the callback functions don't change. */
1982 if (val->call_data && ! val->contents)
1984 /* This item shall have a select callback. */
1985 if (! cb_data->select_id)
1986 cb_data->select_id
1987 = g_signal_connect (G_OBJECT (w), "activate",
1988 select_cb, cb_data);
1990 else if (cb_data->select_id)
1992 g_signal_handler_disconnect (w, cb_data->select_id);
1993 cb_data->select_id = 0;
1996 if (NILP (cb_data->help))
1998 /* Shall not have help. Remove if any existed previously. */
1999 if (cb_data->highlight_id)
2001 g_signal_handler_disconnect (G_OBJECT (w),
2002 cb_data->highlight_id);
2003 cb_data->highlight_id = 0;
2005 if (cb_data->unhighlight_id)
2007 g_signal_handler_disconnect (G_OBJECT (w),
2008 cb_data->unhighlight_id);
2009 cb_data->unhighlight_id = 0;
2012 else if (! cb_data->highlight_id && highlight_cb)
2014 /* Have help now, but didn't previously. Add callback. */
2015 cb_data->highlight_id
2016 = g_signal_connect (G_OBJECT (w),
2017 "enter-notify-event",
2018 G_CALLBACK (menuitem_highlight_callback),
2019 cb_data);
2020 cb_data->unhighlight_id
2021 = g_signal_connect (G_OBJECT (w),
2022 "leave-notify-event",
2023 G_CALLBACK (menuitem_highlight_callback),
2024 cb_data);
2029 /* Update the toggle menu item W so it corresponds to VAL. */
2030 static void
2031 xg_update_toggle_item (val, w)
2032 widget_value *val;
2033 GtkWidget *w;
2035 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2038 /* Update the radio menu item W so it corresponds to VAL. */
2039 static void
2040 xg_update_radio_item (val, w)
2041 widget_value *val;
2042 GtkWidget *w;
2044 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2047 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2048 SUBMENU may be NULL, in that case a new menu is created.
2049 F is the frame the menu bar belongs to.
2050 VAL describes the contents of the menu bar.
2051 SELECT_CB is the callback to use when a menu item is selected.
2052 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2053 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2054 CL_DATA is the call back data to use for any newly created items.
2056 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2057 was NULL. */
2059 static GtkWidget *
2060 xg_update_submenu (submenu, f, val,
2061 select_cb, deactivate_cb, highlight_cb, cl_data)
2062 GtkWidget *submenu;
2063 FRAME_PTR f;
2064 widget_value *val;
2065 GCallback select_cb;
2066 GCallback deactivate_cb;
2067 GCallback highlight_cb;
2068 xg_menu_cb_data *cl_data;
2070 GtkWidget *newsub = submenu;
2071 GList *list = 0;
2072 GList *iter;
2073 widget_value *cur;
2074 int has_tearoff_p = 0;
2075 GList *first_radio = 0;
2077 if (submenu)
2078 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2080 for (cur = val, iter = list;
2081 cur && iter;
2082 iter = g_list_next (iter), cur = cur->next)
2084 GtkWidget *w = GTK_WIDGET (iter->data);
2086 /* Skip tearoff items, they have no counterpart in val. */
2087 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2089 has_tearoff_p = 1;
2090 iter = g_list_next (iter);
2091 if (iter) w = GTK_WIDGET (iter->data);
2092 else break;
2095 /* Remember first radio button in a group. If we get a mismatch in
2096 a radio group we must rebuild the whole group so that the connections
2097 in GTK becomes correct. */
2098 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2099 first_radio = iter;
2100 else if (cur->button_type != BUTTON_TYPE_RADIO
2101 && ! GTK_IS_RADIO_MENU_ITEM (w))
2102 first_radio = 0;
2104 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2106 if (! xg_separator_p (cur->name))
2107 break;
2109 else if (GTK_IS_CHECK_MENU_ITEM (w))
2111 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2112 break;
2113 xg_update_toggle_item (cur, w);
2114 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2116 else if (GTK_IS_RADIO_MENU_ITEM (w))
2118 if (cur->button_type != BUTTON_TYPE_RADIO)
2119 break;
2120 xg_update_radio_item (cur, w);
2121 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2123 else if (GTK_IS_MENU_ITEM (w))
2125 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2126 GtkWidget *sub;
2128 if (cur->button_type != BUTTON_TYPE_NONE ||
2129 xg_separator_p (cur->name))
2130 break;
2132 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2134 sub = gtk_menu_item_get_submenu (witem);
2135 if (sub && ! cur->contents)
2137 /* Not a submenu anymore. */
2138 gtk_widget_ref (sub);
2139 gtk_menu_item_remove_submenu (witem);
2140 gtk_widget_destroy (sub);
2142 else if (cur->contents)
2144 GtkWidget *nsub;
2146 nsub = xg_update_submenu (sub, f, cur->contents,
2147 select_cb, deactivate_cb,
2148 highlight_cb, cl_data);
2150 /* If this item just became a submenu, we must set it. */
2151 if (nsub != sub)
2152 gtk_menu_item_set_submenu (witem, nsub);
2155 else
2157 /* Structural difference. Remove everything from here and down
2158 in SUBMENU. */
2159 break;
2163 /* Remove widgets from first structual change. */
2164 if (iter)
2166 /* If we are adding new menu items below, we must remove from
2167 first radio button so that radio groups become correct. */
2168 if (cur && first_radio) remove_from_container (submenu, first_radio);
2169 else remove_from_container (submenu, iter);
2172 if (cur)
2174 /* More items added. Create them. */
2175 newsub = create_menus (cur,
2177 select_cb,
2178 deactivate_cb,
2179 highlight_cb,
2182 ! has_tearoff_p,
2183 submenu,
2184 cl_data,
2188 if (list) g_list_free (list);
2190 return newsub;
2193 /* Update the MENUBAR.
2194 F is the frame the menu bar belongs to.
2195 VAL describes the contents of the menu bar.
2196 If DEEP_P is non-zero, rebuild all but the top level menu names in
2197 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2198 SELECT_CB is the callback to use when a menu item is selected.
2199 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2200 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2201 void
2202 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2203 select_cb, deactivate_cb, highlight_cb)
2204 GtkWidget *menubar;
2205 FRAME_PTR f;
2206 widget_value *val;
2207 int deep_p;
2208 GCallback select_cb;
2209 GCallback deactivate_cb;
2210 GCallback highlight_cb;
2212 xg_menu_cb_data *cl_data;
2213 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2215 if (! list) return;
2217 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2218 XG_FRAME_DATA);
2220 if (! deep_p)
2222 widget_value *cur = val->contents;
2223 xg_update_menubar (menubar, f, &list, list, 0, cur,
2224 select_cb, highlight_cb, cl_data);
2226 else
2228 widget_value *cur;
2230 /* Update all sub menus.
2231 We must keep the submenu names (GTK menu item widgets) since the
2232 X Window in the XEvent that activates the menu are those widgets. */
2234 /* Update cl_data, menu_item things in F may have changed. */
2235 update_cl_data (cl_data, f, highlight_cb);
2237 for (cur = val->contents; cur; cur = cur->next)
2239 GList *iter;
2240 GtkWidget *sub = 0;
2241 GtkWidget *newsub;
2242 GtkMenuItem *witem;
2244 /* Find sub menu that corresponds to val and update it. */
2245 for (iter = list ; iter; iter = g_list_next (iter))
2247 witem = GTK_MENU_ITEM (iter->data);
2248 if (xg_item_label_same_p (witem, cur->name))
2250 sub = gtk_menu_item_get_submenu (witem);
2251 break;
2255 newsub = xg_update_submenu (sub,
2257 cur->contents,
2258 select_cb,
2259 deactivate_cb,
2260 highlight_cb,
2261 cl_data);
2262 /* sub may still be NULL. If we just updated non deep and added
2263 a new menu bar item, it has no sub menu yet. So we set the
2264 newly created sub menu under witem. */
2265 if (newsub != sub)
2266 gtk_menu_item_set_submenu (witem, newsub);
2271 g_list_free (list);
2272 gtk_widget_show_all (menubar);
2275 /* Recompute all the widgets of frame F, when the menu bar has been
2276 changed. Value is non-zero if widgets were updated. */
2279 xg_update_frame_menubar (f)
2280 FRAME_PTR f;
2282 struct x_output *x = f->output_data.x;
2283 GtkRequisition req;
2285 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2286 return 0;
2288 BLOCK_INPUT;
2290 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2291 FALSE, FALSE, 0);
2292 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2294 gtk_widget_show_all (x->menubar_widget);
2295 gtk_widget_size_request (x->menubar_widget, &req);
2297 FRAME_MENUBAR_HEIGHT (f) = req.height;
2299 /* The height has changed, resize outer widget and set columns
2300 rows to what we had before adding the menu bar. */
2301 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2303 SET_FRAME_GARBAGED (f);
2304 UNBLOCK_INPUT;
2306 return 1;
2309 /* Get rid of the menu bar of frame F, and free its storage.
2310 This is used when deleting a frame, and when turning off the menu bar. */
2312 void
2313 free_frame_menubar (f)
2314 FRAME_PTR f;
2316 struct x_output *x = f->output_data.x;
2318 if (x->menubar_widget)
2320 BLOCK_INPUT;
2322 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2323 /* The menubar and its children shall be deleted when removed from
2324 the container. */
2325 x->menubar_widget = 0;
2326 FRAME_MENUBAR_HEIGHT (f) = 0;
2328 /* The height has changed, resize outer widget and set columns
2329 rows to what we had before removing the menu bar. */
2330 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2332 SET_FRAME_GARBAGED (f);
2333 UNBLOCK_INPUT;
2339 /***********************************************************************
2340 Scroll bar functions
2341 ***********************************************************************/
2344 /* Setting scroll bar values invokes the callback. Use this variable
2345 to indicate that callback should do nothing. */
2346 int xg_ignore_gtk_scrollbar;
2348 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2349 32 bits. But we want to store pointers, and they may be larger
2350 than 32 bits. Keep a mapping from integer index to widget pointers
2351 to get around the 32 bit limitation. */
2352 static struct
2354 GtkWidget **widgets;
2355 int max_size;
2356 int used;
2357 } id_to_widget;
2359 /* Grow this much every time we need to allocate more */
2360 #define ID_TO_WIDGET_INCR 32
2362 /* Store the widget pointer W in id_to_widget and return the integer index. */
2363 static int
2364 xg_store_widget_in_map (w)
2365 GtkWidget *w;
2367 int i;
2369 if (id_to_widget.max_size == id_to_widget.used)
2371 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2373 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2374 sizeof (GtkWidget *)*new_size);
2376 for (i = id_to_widget.max_size; i < new_size; ++i)
2377 id_to_widget.widgets[i] = 0;
2378 id_to_widget.max_size = new_size;
2381 /* Just loop over the array and find a free place. After all,
2382 how many scroll bars are we creating? Should be a small number.
2383 The check above guarantees we will find a free place. */
2384 for (i = 0; i < id_to_widget.max_size; ++i)
2386 if (! id_to_widget.widgets[i])
2388 id_to_widget.widgets[i] = w;
2389 ++id_to_widget.used;
2391 return i;
2395 /* Should never end up here */
2396 abort ();
2399 /* Remove pointer at IDX from id_to_widget.
2400 Called when scroll bar is destroyed. */
2401 static void
2402 xg_remove_widget_from_map (idx)
2403 int idx;
2405 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2407 id_to_widget.widgets[idx] = 0;
2408 --id_to_widget.used;
2412 /* Get the widget pointer at IDX from id_to_widget. */
2413 static GtkWidget *
2414 xg_get_widget_from_map (idx)
2415 int idx;
2417 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2418 return id_to_widget.widgets[idx];
2420 return 0;
2423 /* Return the scrollbar id for X Window WID.
2424 Return -1 if WID not in id_to_widget. */
2426 xg_get_scroll_id_for_window (wid)
2427 Window wid;
2429 int idx;
2430 GtkWidget *w;
2432 w = xg_win_to_widget (wid);
2434 if (w)
2436 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2437 if (id_to_widget.widgets[idx] == w)
2438 return idx;
2441 return -1;
2444 /* Callback invoked when scroll bar WIDGET is destroyed.
2445 DATA is the index into id_to_widget for WIDGET.
2446 We free pointer to last scroll bar values here and remove the index. */
2447 static void
2448 xg_gtk_scroll_destroy (widget, data)
2449 GtkWidget *widget;
2450 gpointer data;
2452 gpointer p;
2453 int id = (int)data;
2455 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2456 if (p) xfree (p);
2457 xg_remove_widget_from_map (id);
2460 /* Callback for button press/release events. Used to start timer so that
2461 the scroll bar repetition timer in GTK gets handeled.
2462 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2463 WIDGET is the scroll bar widget the event is for (not used).
2464 EVENT contains the event.
2465 USER_DATA points to the struct scrollbar structure.
2467 Returns FALSE to tell GTK that it shall continue propagate the event
2468 to widgets. */
2469 static gboolean
2470 scroll_bar_button_cb (widget, event, user_data)
2471 GtkWidget *widget;
2472 GdkEventButton *event;
2473 gpointer user_data;
2475 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2476 xg_start_timer ();
2477 else if (event->type == GDK_BUTTON_RELEASE)
2479 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2480 if (xg_timer) xg_stop_timer ();
2481 bar->dragging = Qnil;
2484 return FALSE;
2487 /* Create a scroll bar widget for frame F. Store the scroll bar
2488 in BAR.
2489 SCROLL_CALLBACK is the callback to invoke when the value of the
2490 bar changes.
2491 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2492 to set resources for the widget. */
2493 void
2494 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2495 FRAME_PTR f;
2496 struct scroll_bar *bar;
2497 GCallback scroll_callback;
2498 char *scroll_bar_name;
2500 GtkWidget *wscroll;
2501 GtkObject *vadj;
2502 int scroll_id;
2504 /* Page, step increment values are not so important here, they
2505 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2506 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2507 0.1, 0.1, 0.1);
2509 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2510 gtk_widget_set_name (wscroll, scroll_bar_name);
2511 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2513 scroll_id = xg_store_widget_in_map (wscroll);
2515 g_signal_connect (G_OBJECT (wscroll),
2516 "value-changed",
2517 scroll_callback,
2518 (gpointer) bar);
2519 g_signal_connect (G_OBJECT (wscroll),
2520 "destroy",
2521 G_CALLBACK (xg_gtk_scroll_destroy),
2522 (gpointer) scroll_id);
2524 /* Connect to button press and button release to detect if any scroll bar
2525 has the pointer. */
2526 g_signal_connect (G_OBJECT (wscroll),
2527 "button-press-event",
2528 G_CALLBACK (scroll_bar_button_cb),
2529 (gpointer) bar);
2530 g_signal_connect (G_OBJECT (wscroll),
2531 "button-release-event",
2532 G_CALLBACK (scroll_bar_button_cb),
2533 (gpointer) bar);
2535 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget),
2536 wscroll, -1, -1);
2538 /* Set the cursor to an arrow. */
2539 xg_set_cursor (wscroll, &xg_left_ptr_cursor);
2541 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2544 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2545 void
2546 xg_show_scroll_bar (scrollbar_id)
2547 int scrollbar_id;
2549 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2550 if (w)
2551 gtk_widget_show (w);
2554 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2555 void
2556 xg_remove_scroll_bar (f, scrollbar_id)
2557 FRAME_PTR f;
2558 int scrollbar_id;
2560 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2561 if (w)
2563 gtk_widget_destroy (w);
2564 SET_FRAME_GARBAGED (f);
2568 /* Find left/top for widget W in GtkFixed widget WFIXED. */
2569 static void
2570 xg_find_top_left_in_fixed (w, wfixed, left, top)
2571 GtkWidget *w, *wfixed;
2572 int *left, *top;
2574 GList *iter;
2576 for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter))
2578 GtkFixedChild *child = (GtkFixedChild *) iter->data;
2580 if (child->widget == w)
2582 *left = child->x;
2583 *top = child->y;
2584 return;
2588 /* Shall never end up here. */
2589 abort ();
2592 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2593 in frame F.
2594 TOP/LEFT are the new pixel positions where the bar shall appear.
2595 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2596 void
2597 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height,
2598 real_left, canon_width)
2599 FRAME_PTR f;
2600 int scrollbar_id;
2601 int top;
2602 int left;
2603 int width;
2604 int height;
2607 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2609 if (wscroll)
2611 GtkWidget *wfixed = f->output_data.x->edit_widget;
2612 int winextra = canon_width > width ? (canon_width - width) / 2 : 0;
2613 int bottom = top + height;
2615 gint slider_width;
2616 int oldtop, oldleft, oldbottom;
2617 GtkRequisition req;
2619 /* Get old values. */
2620 xg_find_top_left_in_fixed (wscroll, wfixed, &oldleft, &oldtop);
2621 gtk_widget_size_request (wscroll, &req);
2622 oldbottom = oldtop + req.height;
2624 /* Scroll bars in GTK has a fixed width, so if we say width 16, it
2625 will only be its fixed width (14 is default) anyway, the rest is
2626 blank. We are drawing the mode line across scroll bars when
2627 the frame is split:
2628 |bar| |fringe|
2629 ----------------
2630 mode line
2631 ----------------
2632 |bar| |fringe|
2634 When we "unsplit" the frame:
2636 |bar| |fringe|
2637 -| |-| |
2638 m¦ |i| |
2639 -| |-| |
2640 | | | |
2643 the remains of the mode line can be seen in these blank spaces.
2644 So we must clear them explicitly.
2645 GTK scroll bars should do that, but they don't.
2646 Also, the canonical width may be wider than the width for the
2647 scroll bar so that there is some space (typically 1 pixel) between
2648 the scroll bar and the edge of the window and between the scroll
2649 bar and the fringe. */
2651 if (oldtop != -1 && oldleft != -1)
2653 int gtkextral, gtkextrah;
2654 int xl, xr, wbl, wbr;
2655 int bottomdiff, topdiff;
2657 gtk_widget_style_get (wscroll, "slider_width", &slider_width, NULL);
2658 gtkextral = width > slider_width ? (width - slider_width) / 2 : 0;
2659 gtkextrah = gtkextral ? (width - slider_width - gtkextral) : 0;
2661 xl = real_left;
2662 wbl = gtkextral + winextra;
2663 wbr = gtkextrah + winextra;
2664 xr = left + gtkextral + slider_width;
2665 bottomdiff = abs (oldbottom - bottom);
2666 topdiff = abs (oldtop - top);
2668 if (oldleft != left)
2670 gdk_window_clear_area (wfixed->window, xl, top, wbl, height);
2671 gdk_window_clear_area (wfixed->window, xr, top, wbr, height);
2674 if (oldtop > top)
2676 gdk_window_clear_area (wfixed->window, xl, top, wbl, topdiff);
2677 gdk_window_clear_area (wfixed->window, xr, top, wbr, topdiff);
2679 else if (oldtop < top)
2681 gdk_window_clear_area (wfixed->window, xl, oldtop, wbl, topdiff);
2682 gdk_window_clear_area (wfixed->window, xr, oldtop, wbr, topdiff);
2685 if (oldbottom > bottom)
2687 gdk_window_clear_area (wfixed->window, xl, bottom, wbl,
2688 bottomdiff);
2689 gdk_window_clear_area (wfixed->window, xr, bottom, wbr,
2690 bottomdiff);
2692 else if (oldbottom < bottom)
2694 gdk_window_clear_area (wfixed->window, xl, oldbottom, wbl,
2695 bottomdiff);
2696 gdk_window_clear_area (wfixed->window, xr, oldbottom, wbr,
2697 bottomdiff);
2701 /* Move and resize to new values. */
2702 gtk_fixed_move (GTK_FIXED (wfixed), wscroll, left, top);
2703 gtk_widget_set_size_request (wscroll, width, height);
2705 /* Must force out update so changed scroll bars gets redrawn. */
2706 gdk_window_process_all_updates ();
2708 SET_FRAME_GARBAGED (f);
2709 cancel_mouse_face (f);
2713 /* Set the thumb size and position of scroll bar BAR. We are currently
2714 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2715 void
2716 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2717 struct scroll_bar *bar;
2718 int portion, position, whole;
2720 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2722 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2724 if (wscroll && NILP (bar->dragging))
2726 GtkAdjustment *adj;
2727 gdouble shown;
2728 gdouble top;
2729 int size, value;
2730 int new_step;
2731 int changed = 0;
2733 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
2735 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2736 rather than the real portion value. This makes the thumb less likely
2737 to resize and that looks better. */
2738 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
2739 /* When the thumb is at the bottom, position == whole.
2740 So we need to increase `whole' to make space for the thumb. */
2741 whole += portion;
2743 if (whole <= 0)
2744 top = 0, shown = 1;
2745 else
2747 top = (gdouble) position / whole;
2748 shown = (gdouble) portion / whole;
2751 size = shown * XG_SB_RANGE;
2752 size = min (size, XG_SB_RANGE);
2753 size = max (size, 1);
2755 value = top * XG_SB_RANGE;
2756 value = min (value, XG_SB_MAX - size);
2757 value = max (value, XG_SB_MIN);
2759 /* Assume all lines are of equal size. */
2760 new_step = size / max (1, FRAME_HEIGHT (f));
2762 if ((int) adj->page_size != size
2763 || (int) adj->step_increment != new_step)
2765 adj->page_size = size;
2766 adj->step_increment = new_step;
2767 /* Assume a page increment is about 95% of the page size */
2768 adj->page_increment = (int) (0.95*adj->page_size);
2769 changed = 1;
2772 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2774 GtkWidget *wfixed = f->output_data.x->edit_widget;
2776 BLOCK_INPUT;
2778 /* gtk_range_set_value invokes the callback. Set
2779 ignore_gtk_scrollbar to make the callback do nothing */
2780 xg_ignore_gtk_scrollbar = 1;
2782 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2783 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
2784 else if (changed)
2785 gtk_adjustment_changed (adj);
2787 xg_ignore_gtk_scrollbar = 0;
2789 UNBLOCK_INPUT;
2795 /***********************************************************************
2796 Tool bar functions
2797 ***********************************************************************/
2798 /* The key for the data we put in the GtkImage widgets. The data is
2799 the image used by Emacs. We use this to see if we need to update
2800 the GtkImage with a new image. */
2801 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2803 /* Callback function invoked when a tool bar item is pressed.
2804 W is the button widget in the tool bar that got pressed,
2805 CLIENT_DATA is an integer that is the index of the button in the
2806 tool bar. 0 is the first button. */
2807 static void
2808 xg_tool_bar_callback (w, client_data)
2809 GtkWidget *w;
2810 gpointer client_data;
2812 int idx = (int)client_data;
2813 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2814 Lisp_Object key, frame;
2815 struct input_event event;
2817 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2818 return;
2820 idx *= TOOL_BAR_ITEM_NSLOTS;
2822 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
2823 XSETFRAME (frame, f);
2824 event.kind = TOOL_BAR_EVENT;
2825 event.frame_or_window = frame;
2826 event.arg = frame;
2827 kbd_buffer_store_event (&event);
2829 event.kind = TOOL_BAR_EVENT;
2830 event.frame_or_window = frame;
2831 event.arg = key;
2832 event.modifiers = 0; /* These are not available. */
2833 kbd_buffer_store_event (&event);
2836 /* This callback is called when a tool bar is detached. We must set
2837 the height of the tool bar to zero when this happens so frame sizes
2838 are correctly calculated.
2839 WBOX is the handle box widget that enables detach/attach of the tool bar.
2840 W is the tool bar widget.
2841 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2842 static void
2843 xg_tool_bar_detach_callback (wbox, w, client_data)
2844 GtkHandleBox *wbox;
2845 GtkWidget *w;
2846 gpointer client_data;
2848 FRAME_PTR f = (FRAME_PTR) client_data;
2850 if (f)
2852 /* When detaching a tool bar, not everything dissapear. There are
2853 a few pixels left that are used to drop the tool bar back into
2854 place. */
2855 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
2856 FRAME_TOOLBAR_HEIGHT (f) = 2;
2858 /* The height has changed, resize outer widget and set columns
2859 rows to what we had before detaching the tool bar. */
2860 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2864 /* This callback is called when a tool bar is reattached. We must set
2865 the height of the tool bar when this happens so frame sizes
2866 are correctly calculated.
2867 WBOX is the handle box widget that enables detach/attach of the tool bar.
2868 W is the tool bar widget.
2869 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2870 static void
2871 xg_tool_bar_attach_callback (wbox, w, client_data)
2872 GtkHandleBox *wbox;
2873 GtkWidget *w;
2874 gpointer client_data;
2876 FRAME_PTR f = (FRAME_PTR) client_data;
2878 if (f)
2880 GtkRequisition req;
2882 gtk_widget_size_request (w, &req);
2883 FRAME_TOOLBAR_HEIGHT (f) = req.height;
2885 /* The height has changed, resize outer widget and set columns
2886 rows to what we had before detaching the tool bar. */
2887 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2891 /* This callback is called when the mouse enters or leaves a tool bar item.
2892 It is used for displaying and hiding the help text.
2893 W is the tool bar item, a button.
2894 EVENT is either an enter event or leave event.
2895 CLIENT_DATA is an integer that is the index of the button in the
2896 tool bar. 0 is the first button.
2898 Returns FALSE to tell GTK to keep processing this event. */
2899 static gboolean
2900 xg_tool_bar_help_callback (w, event, client_data)
2901 GtkWidget *w;
2902 GdkEventCrossing *event;
2903 gpointer client_data;
2905 int idx = (int)client_data;
2906 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2907 Lisp_Object help, frame;
2909 if (! GTK_IS_BUTTON (w))
2911 return FALSE;
2914 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2915 return FALSE;
2917 if (event->type == GDK_ENTER_NOTIFY)
2919 idx *= TOOL_BAR_ITEM_NSLOTS;
2920 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
2922 if (NILP (help))
2923 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
2925 else
2926 help = Qnil;
2928 XSETFRAME (frame, f);
2929 kbd_buffer_store_help_event (frame, help);
2931 return FALSE;
2935 /* This callback is called when a tool bar item shall be redrawn.
2936 It modifies the expose event so that the GtkImage widget redraws the
2937 whole image. This to overcome a bug that makes GtkImage draw the image
2938 in the wrong place when it tries to redraw just a part of the image.
2939 W is the GtkImage to be redrawn.
2940 EVENT is the expose event for W.
2941 CLIENT_DATA is unused.
2943 Returns FALSE to tell GTK to keep processing this event. */
2944 static gboolean
2945 xg_tool_bar_item_expose_callback (w, event, client_data)
2946 GtkWidget *w;
2947 GdkEventExpose *event;
2948 gpointer client_data;
2950 gint width, height;
2952 gdk_drawable_get_size (event->window, &width, &height);
2954 event->area.x -= width > event->area.width ? width-event->area.width : 0;
2955 event->area.y -= height > event->area.height ? height-event->area.height : 0;
2957 event->area.x = max(0, event->area.x);
2958 event->area.y = max(0, event->area.y);
2960 event->area.width = max (width, event->area.width);
2961 event->area.height = max (height, event->area.height);
2963 return FALSE;
2966 /* This callback is called when a tool bar shall be redrawn.
2967 We need to update the tool bar from here in case the image cache
2968 has deleted the pixmaps used in the tool bar.
2969 W is the GtkToolbar to be redrawn.
2970 EVENT is the expose event for W.
2971 CLIENT_DATA is pointing to the frame for this tool bar.
2973 Returns FALSE to tell GTK to keep processing this event. */
2974 static gboolean
2975 xg_tool_bar_expose_callback (w, event, client_data)
2976 GtkWidget *w;
2977 GdkEventExpose *event;
2978 gpointer client_data;
2980 update_frame_tool_bar((FRAME_PTR)client_data);
2981 return FALSE;
2984 static void
2985 xg_create_tool_bar (f)
2986 FRAME_PTR f;
2988 struct x_output *x = f->output_data.x;
2989 GtkRequisition req;
2990 int vbox_pos = x->menubar_widget ? 1 : 0;
2992 x->toolbar_widget = gtk_toolbar_new ();
2993 x->handlebox_widget = gtk_handle_box_new ();
2994 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
2995 x->toolbar_widget);
2997 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
2998 FALSE, FALSE, 0);
3000 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3001 vbox_pos);
3003 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3005 /* We only have icons, so override any user setting. We could
3006 use the caption property of the toolbar item (see update_frame_tool_bar
3007 below), but some of those strings are long, making the toolbar so
3008 long it does not fit on the screen. The GtkToolbar widget makes every
3009 item equal size, so the longest caption determine the size of every
3010 tool bar item. I think the creators of the GtkToolbar widget
3011 counted on 4 or 5 character long strings. */
3012 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3013 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3014 GTK_ORIENTATION_HORIZONTAL);
3016 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3017 G_CALLBACK (xg_tool_bar_detach_callback), f);
3018 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3019 G_CALLBACK (xg_tool_bar_attach_callback), f);
3020 g_signal_connect (G_OBJECT (x->toolbar_widget),
3021 "expose-event",
3022 G_CALLBACK (xg_tool_bar_expose_callback),
3025 gtk_widget_show_all (x->handlebox_widget);
3027 gtk_widget_size_request (x->toolbar_widget, &req);
3028 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3030 /* The height has changed, resize outer widget and set columns
3031 rows to what we had before adding the tool bar. */
3032 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
3034 SET_FRAME_GARBAGED (f);
3037 void
3038 update_frame_tool_bar (f)
3039 FRAME_PTR f;
3041 int i;
3042 GtkRequisition old_req, new_req;
3043 GList *icon_list;
3044 GList *iter;
3045 struct x_output *x = f->output_data.x;
3047 if (! FRAME_GTK_WIDGET (f))
3048 return;
3050 BLOCK_INPUT;
3052 if (! x->toolbar_widget)
3053 xg_create_tool_bar (f);
3055 gtk_widget_size_request (x->toolbar_widget, &old_req);
3057 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
3058 iter = icon_list;
3060 for (i = 0; i < f->n_tool_bar_items; ++i)
3062 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3064 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3065 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3066 int idx;
3067 int img_id;
3068 struct image *img;
3069 Lisp_Object image;
3070 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
3072 if (iter) iter = g_list_next (iter);
3074 /* If image is a vector, choose the image according to the
3075 button state. */
3076 image = PROP (TOOL_BAR_ITEM_IMAGES);
3077 if (VECTORP (image))
3079 if (enabled_p)
3080 idx = (selected_p
3081 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3082 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3083 else
3084 idx = (selected_p
3085 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3086 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3088 xassert (ASIZE (image) >= idx);
3089 image = AREF (image, idx);
3091 else
3092 idx = -1;
3094 /* Ignore invalid image specifications. */
3095 if (!valid_image_p (image))
3097 if (wicon) gtk_widget_hide (wicon);
3098 continue;
3101 img_id = lookup_image (f, image);
3102 img = IMAGE_FROM_ID (f, img_id);
3103 prepare_image_for_display (f, img);
3105 if (img->load_failed_p || img->pixmap == None)
3107 if (wicon) gtk_widget_hide (wicon);
3108 continue;
3111 if (! wicon)
3113 GdkPixmap *gpix = gdk_pixmap_foreign_new (img->pixmap);
3114 GdkBitmap *gmask = img->mask ?
3115 (GdkBitmap*) gdk_pixmap_foreign_new (img->mask) : 0;
3117 GtkWidget *w = gtk_image_new_from_pixmap (gpix, gmask);
3118 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3119 0, 0, 0,
3121 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3122 (gpointer)i);
3124 /* Save the image so we can see if an update is needed when
3125 this function is called again. */
3126 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3127 (gpointer)img->pixmap);
3129 /* Catch expose events to overcome an annoying redraw bug, see
3130 comment for xg_tool_bar_item_expose_callback. */
3131 g_signal_connect (G_OBJECT (w),
3132 "expose-event",
3133 G_CALLBACK (xg_tool_bar_item_expose_callback),
3136 /* We must set sensitive on the button that is the parent
3137 of the GtkImage parent. Go upwards until we find the button. */
3138 while (! GTK_IS_BUTTON (w))
3139 w = gtk_widget_get_parent (w);
3141 if (w)
3143 /* Save the frame in the button so the xg_tool_bar_callback
3144 can get at it. */
3145 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3146 gtk_widget_set_sensitive (w, enabled_p);
3148 /* Use enter/leave notify to show help. We use the events
3149 rather than the GtkButton specific signals "enter" and
3150 "leave", so we can have only one callback. The event
3151 will tell us what kind of event it is. */
3152 g_signal_connect (G_OBJECT (w),
3153 "enter-notify-event",
3154 G_CALLBACK (xg_tool_bar_help_callback),
3155 (gpointer)i);
3156 g_signal_connect (G_OBJECT (w),
3157 "leave-notify-event",
3158 G_CALLBACK (xg_tool_bar_help_callback),
3159 (gpointer)i);
3162 else
3164 /* The child of the tool bar is a button. Inside that button
3165 is a vbox. Inside that vbox is the GtkImage. */
3166 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3167 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3168 GtkImage *wimage = GTK_IMAGE (chlist->data);
3169 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3170 XG_TOOL_BAR_IMAGE_DATA);
3171 g_list_free (chlist);
3173 if (old_img != img->pixmap)
3175 GdkPixmap *gpix = gdk_pixmap_foreign_new (img->pixmap);
3176 GdkBitmap *gmask = img->mask ?
3177 (GdkBitmap*) gdk_pixmap_foreign_new (img->mask) : 0;
3179 gtk_image_set_from_pixmap (wimage, gpix, gmask);
3182 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3183 (gpointer)img->pixmap);
3185 gtk_widget_set_sensitive (wicon, enabled_p);
3186 gtk_widget_show (wicon);
3189 #undef PROP
3192 /* Remove buttons not longer needed. We just hide them so they
3193 can be reused later on. */
3194 while (iter)
3196 GtkWidget *w = GTK_WIDGET (iter->data);
3197 gtk_widget_hide (w);
3198 iter = g_list_next (iter);
3201 gtk_widget_size_request (x->toolbar_widget, &new_req);
3202 if (old_req.height != new_req.height)
3204 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3205 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
3208 if (icon_list) g_list_free (icon_list);
3210 UNBLOCK_INPUT;
3213 void
3214 free_frame_tool_bar (f)
3215 FRAME_PTR f;
3217 struct x_output *x = f->output_data.x;
3219 if (x->toolbar_widget)
3221 BLOCK_INPUT;
3222 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3223 x->handlebox_widget);
3224 x->toolbar_widget = 0;
3225 x->handlebox_widget = 0;
3226 FRAME_TOOLBAR_HEIGHT (f) = 0;
3228 /* The height has changed, resize outer widget and set columns
3229 rows to what we had before removing the tool bar. */
3230 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
3232 SET_FRAME_GARBAGED (f);
3233 UNBLOCK_INPUT;
3239 /***********************************************************************
3240 Initializing
3241 ***********************************************************************/
3242 void
3243 xg_initialize ()
3245 xg_ignore_gtk_scrollbar = 0;
3246 xg_left_ptr_cursor = 0;
3247 xg_did_tearoff = 0;
3249 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3250 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3252 id_to_widget.max_size = id_to_widget.used = 0;
3253 id_to_widget.widgets = 0;
3255 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3256 bindings. It doesn't seem to be any way to remove properties,
3257 so we set it to VoidSymbol which in X means "no key". */
3258 gtk_settings_set_string_property (gtk_settings_get_default (),
3259 "gtk-menu-bar-accel",
3260 "VoidSymbol",
3261 EMACS_CLASS);
3263 /* Make GTK text input widgets use Emacs style keybindings. This is
3264 Emacs after all. */
3265 gtk_settings_set_string_property (gtk_settings_get_default (),
3266 "gtk-key-theme-name",
3267 "Emacs",
3268 EMACS_CLASS);
3271 #endif /* USE_GTK */