*** empty log message ***
[bpt/emacs.git] / src / gtkutil.c
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22 #include "config.h"
23
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>
35
36 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
37 (PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
38
39
40 \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;
47
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;
51
52
53 /* The next two variables and functions are taken from lwlib. */
54 static widget_value *widget_value_free_list;
55 static int malloc_cpt;
56
57 /* Allocate a widget_value structure, either by taking one from the
58 widget_value_free_list or by malloc:ing a new one.
59
60 Return a pointer to the allocated structure. */
61 widget_value *
62 malloc_widget_value ()
63 {
64 widget_value *wv;
65 if (widget_value_free_list)
66 {
67 wv = widget_value_free_list;
68 widget_value_free_list = wv->free_list;
69 wv->free_list = 0;
70 }
71 else
72 {
73 wv = (widget_value *) malloc (sizeof (widget_value));
74 malloc_cpt++;
75 }
76 memset (wv, 0, sizeof (widget_value));
77 return wv;
78 }
79
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;
85 {
86 if (wv->free_list)
87 abort ();
88
89 if (malloc_cpt > 25)
90 {
91 /* When the number of already allocated cells is too big,
92 We free it. */
93 free (wv);
94 malloc_cpt--;
95 }
96 else
97 {
98 wv->free_list = widget_value_free_list;
99 widget_value_free_list = wv;
100 }
101 }
102
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.
106
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;
113 {
114 GList *children = gdk_window_peek_children (w->window);
115
116 /* Create the cursor unless already created. */
117 if (! *cursor)
118 *cursor = gdk_cursor_new (GDK_LEFT_PTR);
119
120 gdk_window_set_cursor (w->window, *cursor);
121
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. */
126
127 for ( ; children; children = g_list_next (children))
128 gdk_window_set_cursor (GDK_WINDOW (children->data), *cursor);
129 }
130
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;
143 {
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;
150 }
151
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 stared is continuous, i.e. runs until xg_stop_timer is called. */
155 static void
156 xg_start_timer ()
157 {
158 if (! xg_timer)
159 {
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,
165 0);
166 }
167 }
168
169 /* Stop the xg_timer if started. */
170 static void
171 xg_stop_timer ()
172 {
173 if (xg_timer)
174 {
175 cancel_atimer (xg_timer);
176 xg_timer = 0;
177 }
178 }
179
180 /* Insert NODE into linked LIST. */
181 static void
182 xg_list_insert (xg_list_node *list, xg_list_node *node)
183 {
184 xg_list_node *list_start = list->next;
185
186 if (list_start) list_start->prev = node;
187 node->next = list_start;
188 node->prev = 0;
189 list->next = node;
190 }
191
192 /* Remove NODE from linked LIST. */
193 static void
194 xg_list_remove (xg_list_node *list, xg_list_node *node)
195 {
196 xg_list_node *list_start = list->next;
197 if (node == list_start)
198 {
199 list->next = node->next;
200 if (list->next) list->next->prev = 0;
201 }
202 else
203 {
204 node->prev->next = node->next;
205 if (node->next) node->next->prev = node->prev;
206 }
207 }
208
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;
216 {
217 char *utf8_str = str;
218
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);
222
223 return utf8_str;
224 }
225
226
227 \f
228 /***********************************************************************
229 General functions for creating widgets, resizing, events, e.t.c.
230 ***********************************************************************/
231
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;
239 {
240 if (f->output_data.x->size_hint_flags & USPosition)
241 {
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];
247
248 if (xneg)
249 left = -left;
250 if (yneg)
251 top = -top;
252
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);
258
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);
262 }
263 }
264
265
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;
274 {
275 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
276 PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
277
278 /* base_height is now changed. */
279 x_wm_set_size_hint (f, 0, 0);
280
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);
285
286 xg_frame_set_char_size (f, columns, rows);
287 gdk_window_process_all_updates ();
288 }
289
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;
295 {
296 GtkWidget *wfixed = f->output_data.x->edit_widget;
297
298 if (wfixed)
299 {
300 gtk_container_set_reallocate_redraws (GTK_CONTAINER (wfixed), TRUE);
301 gdk_window_process_all_updates ();
302 }
303 }
304
305 /* Function to handle resize of our widgets. Since Emacs has some layouts
306 that does not fit well with GTK standard containers, we do most layout
307 manually.
308 F is the frame to resize.
309 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
310 void
311 xg_resize_widgets (f, pixelwidth, pixelheight)
312 FRAME_PTR f;
313 int pixelwidth, pixelheight;
314 {
315 int mbheight = FRAME_MENUBAR_HEIGHT (f);
316 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
317 int rows = PIXEL_TO_CHAR_HEIGHT (f, pixelheight - mbheight - tbheight);
318 int columns = PIXEL_TO_CHAR_WIDTH (f, pixelwidth);
319
320 if (FRAME_GTK_WIDGET (f)
321 && (columns != FRAME_WIDTH (f) || rows != FRAME_HEIGHT (f)
322 || pixelwidth != PIXEL_WIDTH (f) || pixelheight != PIXEL_HEIGHT (f)))
323 {
324 struct x_output *x = f->output_data.x;
325 GtkAllocation all;
326
327 all.y = mbheight + tbheight;
328 all.x = 0;
329
330 all.width = pixelwidth;
331 all.height = pixelheight - mbheight - tbheight;
332
333 gtk_widget_size_allocate (x->edit_widget, &all);
334
335 xg_frame_cleared (f);
336
337 change_frame_size (f, rows, columns, 0, 1, 0);
338 SET_FRAME_GARBAGED (f);
339 cancel_mouse_face (f);
340 }
341 }
342
343
344 /* Update our widget size to be COLS/ROWS characters for frame F. */
345 void
346 xg_frame_set_char_size (f, cols, rows)
347 FRAME_PTR f;
348 int cols;
349 int rows;
350 {
351 int pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows)
352 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
353 int pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
354
355 /* Take into account the size of the scroll bar. Always use the
356 number of columns occupied by the scroll bar here otherwise we
357 might end up with a frame width that is not a multiple of the
358 frame's character width which is bad for vertically split
359 windows. */
360 f->output_data.x->vertical_scroll_bar_extra
361 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
362 ? 0
363 : (FRAME_SCROLL_BAR_COLS (f)
364 * FONT_WIDTH (f->output_data.x->font)));
365
366 compute_fringe_widths (f, 0);
367
368 /* Must resize our top level widget. Font size may have changed,
369 but not rows/cols. */
370 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
371 pixelwidth, pixelheight);
372 xg_resize_widgets (f, pixelwidth, pixelheight);
373 }
374
375 /* Convert an X Window WSESC to its corresponding GtkWidget.
376 Must be done like this, because GtkWidget:s can have "hidden"
377 X Window that aren't accessible.
378
379 Return 0 if no widget match WDESC. */
380 GtkWidget *
381 xg_win_to_widget (wdesc)
382 Window wdesc;
383 {
384 gpointer gdkwin;
385 GtkWidget *gwdesc = 0;
386
387 BLOCK_INPUT;
388 gdkwin = gdk_xid_table_lookup (wdesc);
389 if (gdkwin)
390 {
391 GdkEvent event;
392 event.any.window = gdkwin;
393 gwdesc = gtk_get_event_widget (&event);
394 }
395
396 UNBLOCK_INPUT;
397 return gwdesc;
398 }
399
400 /* Fill in the GdkColor C so that it represents PIXEL.
401 W is the widget that color will be used for. Used to find colormap. */
402 static void
403 xg_pix_to_gcolor (w, pixel, c)
404 GtkWidget *w;
405 unsigned long pixel;
406 GdkColor *c;
407 {
408 GdkColormap *map = gtk_widget_get_colormap (w);
409 gdk_colormap_query_color (map, pixel, c);
410 }
411
412 /* Create and set up the GTK widgets for frame F.
413 Return 0 if creation failed, non-zero otherwise. */
414 int
415 xg_create_frame_widgets (f)
416 FRAME_PTR f;
417 {
418 GtkWidget *wtop;
419 GtkWidget *wvbox;
420 GtkWidget *wfixed;
421 GdkColor bg;
422 GtkRcStyle *style;
423 int i;
424 char *title = 0;
425
426 BLOCK_INPUT;
427
428 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
429 wvbox = gtk_vbox_new (FALSE, 0);
430 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
431
432 if (! wtop || ! wvbox || ! wfixed)
433 {
434 if (wtop) gtk_widget_destroy (wtop);
435 if (wvbox) gtk_widget_destroy (wvbox);
436 if (wfixed) gtk_widget_destroy (wfixed);
437
438 return 0;
439 }
440
441 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
442 gtk_widget_set_name (wtop, EMACS_CLASS);
443 gtk_widget_set_name (wvbox, "pane");
444 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
445
446 /* If this frame has a title or name, set it in the title bar. */
447 if (! NILP (f->title)) title = SDATA (f->title);
448 else if (! NILP (f->name)) title = SDATA (f->name);
449
450 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
451
452 FRAME_GTK_OUTER_WIDGET (f) = wtop;
453 FRAME_GTK_WIDGET (f) = wfixed;
454 f->output_data.x->vbox_widget = wvbox;
455
456 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
457
458 gtk_widget_set_size_request (wfixed,
459 PIXEL_WIDTH (f),
460 PIXEL_HEIGHT (f));
461
462 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
463 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
464
465 if (FRAME_EXTERNAL_TOOL_BAR (f))
466 update_frame_tool_bar (f);
467
468 /* The tool bar is created but first there are no items in it.
469 This causes it to be zero height. Later items are added, but then
470 the frame is already mapped, so there is a "jumping" resize.
471 This makes geometry handling difficult, for example -0-0 will end
472 up in the wrong place as tool bar height has not been taken into account.
473 So we cheat a bit by setting a height that is what it will have
474 later on when tool bar items are added. */
475 if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0)
476 FRAME_TOOLBAR_HEIGHT (f) = 34;
477
478 gtk_widget_set_double_buffered (wvbox, FALSE);
479 gtk_widget_set_double_buffered (wfixed, FALSE);
480 gtk_widget_set_double_buffered (wtop, FALSE);
481
482 /* GTK documents says use gtk_window_set_resizable. But then a user
483 can't shrink the window from its starting size. */
484 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
485 gtk_window_set_wmclass (GTK_WINDOW (wtop),
486 SDATA (Vx_resource_name),
487 SDATA (Vx_resource_class));
488
489 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
490 GTK is to destroy the widget. We want Emacs to do that instead. */
491 g_signal_connect (G_OBJECT (wtop), "delete-event",
492 G_CALLBACK (gtk_true), 0);
493
494 /* Convert our geometry parameters into a geometry string
495 and specify it.
496 GTK will itself handle calculating the real position this way. */
497 xg_set_geometry (f);
498
499 gtk_widget_add_events (wfixed,
500 GDK_POINTER_MOTION_MASK
501 | GDK_EXPOSURE_MASK
502 | GDK_BUTTON_PRESS_MASK
503 | GDK_BUTTON_RELEASE_MASK
504 | GDK_KEY_PRESS_MASK
505 | GDK_ENTER_NOTIFY_MASK
506 | GDK_LEAVE_NOTIFY_MASK
507 | GDK_FOCUS_CHANGE_MASK
508 | GDK_STRUCTURE_MASK
509 | GDK_VISIBILITY_NOTIFY_MASK);
510
511 /* Must realize the windows so the X window gets created. It is used
512 by callers of this function. */
513 gtk_widget_realize (wfixed);
514 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
515
516 /* Since GTK clears its window by filling with the background color,
517 we must keep X and GTK background in sync. */
518 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
519 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
520
521 /* Also, do not let any background pixmap to be set, this looks very
522 bad as Emacs overwrites the background pixmap with its own idea
523 of background color. */
524 style = gtk_widget_get_modifier_style (wfixed);
525
526 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
527 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
528 gtk_widget_modify_style (wfixed, style);
529
530 /* GTK does not set any border, and they look bad with GTK. */
531 f->output_data.x->border_width = 0;
532 f->output_data.x->internal_border_width = 0;
533
534 UNBLOCK_INPUT;
535
536 return 1;
537 }
538
539 /* Set the normal size hints for the window manager, for frame F.
540 FLAGS is the flags word to use--or 0 meaning preserve the flags
541 that the window now has.
542 If USER_POSITION is nonzero, we set the User Position
543 flag (this is useful when FLAGS is 0). */
544 void
545 x_wm_set_size_hint (f, flags, user_position)
546 FRAME_PTR f;
547 long flags;
548 int user_position;
549 {
550 if (FRAME_GTK_OUTER_WIDGET (f))
551 {
552 /* Must use GTK routines here, otherwise GTK resets the size hints
553 to its own defaults. */
554 GdkGeometry size_hints;
555 gint hint_flags = 0;
556 int base_width, base_height;
557 int min_rows = 0, min_cols = 0;
558 int win_gravity = f->output_data.x->win_gravity;
559
560 if (flags)
561 {
562 memset (&size_hints, 0, sizeof (size_hints));
563 f->output_data.x->size_hints = size_hints;
564 f->output_data.x->hint_flags = hint_flags;
565 }
566 else
567 flags = f->output_data.x->size_hint_flags;
568
569 size_hints = f->output_data.x->size_hints;
570 hint_flags = f->output_data.x->hint_flags;
571
572 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
573 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
574 size_hints.height_inc = f->output_data.x->line_height;
575
576 hint_flags |= GDK_HINT_BASE_SIZE;
577 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
578 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0)
579 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
580
581 check_frame_size (f, &min_rows, &min_cols);
582
583 size_hints.base_width = base_width;
584 size_hints.base_height = base_height;
585 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
586 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
587
588
589 /* These currently have a one to one mapping with the X values, but I
590 don't think we should rely on that. */
591 hint_flags |= GDK_HINT_WIN_GRAVITY;
592 size_hints.win_gravity = 0;
593 if (win_gravity == NorthWestGravity)
594 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
595 else if (win_gravity == NorthGravity)
596 size_hints.win_gravity = GDK_GRAVITY_NORTH;
597 else if (win_gravity == NorthEastGravity)
598 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
599 else if (win_gravity == WestGravity)
600 size_hints.win_gravity = GDK_GRAVITY_WEST;
601 else if (win_gravity == CenterGravity)
602 size_hints.win_gravity = GDK_GRAVITY_CENTER;
603 else if (win_gravity == EastGravity)
604 size_hints.win_gravity = GDK_GRAVITY_EAST;
605 else if (win_gravity == SouthWestGravity)
606 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
607 else if (win_gravity == SouthGravity)
608 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
609 else if (win_gravity == SouthEastGravity)
610 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
611 else if (win_gravity == StaticGravity)
612 size_hints.win_gravity = GDK_GRAVITY_STATIC;
613
614 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
615 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
616 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
617
618 if (user_position)
619 {
620 hint_flags &= ~GDK_HINT_POS;
621 hint_flags |= GDK_HINT_USER_POS;
622 }
623
624 BLOCK_INPUT;
625
626 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
627 FRAME_GTK_OUTER_WIDGET (f),
628 &size_hints,
629 hint_flags);
630
631 f->output_data.x->size_hints = size_hints;
632 f->output_data.x->hint_flags = hint_flags;
633 UNBLOCK_INPUT;
634 }
635 }
636
637 /* Change background color of a frame.
638 Since GTK uses the background colour to clear the window, we must
639 keep the GTK and X colors in sync.
640 F is the frame to change,
641 BG is the pixel value to change to. */
642 void
643 xg_set_background_color (f, bg)
644 FRAME_PTR f;
645 unsigned long bg;
646 {
647 if (FRAME_GTK_WIDGET (f))
648 {
649 GdkColor gdk_bg;
650
651 BLOCK_INPUT;
652 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
653 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
654 UNBLOCK_INPUT;
655 }
656 }
657
658
659 \f
660 /***********************************************************************
661 Dialog functions
662 ***********************************************************************/
663 /* Return the dialog title to use for a dialog of type KEY.
664 This is the encoding used by lwlib. We use the same for GTK. */
665 static char *
666 get_dialog_title (char key)
667 {
668 char *title = "";
669
670 switch (key) {
671 case 'E': case 'e':
672 title = "Error";
673 break;
674
675 case 'I': case 'i':
676 title = "Information";
677 break;
678
679 case 'L': case 'l':
680 title = "Prompt";
681 break;
682
683 case 'P': case 'p':
684 title = "Prompt";
685 break;
686
687 case 'Q': case 'q':
688 title = "Question";
689 break;
690 }
691
692 return title;
693 }
694
695 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
696 the dialog, but return TRUE so the event does not propagate further
697 in GTK. This prevents GTK from destroying the dialog widget automatically
698 and we can always destrou the widget manually, regardles of how
699 it was popped down (button press or WM_DELETE_WINDOW).
700 W is the dialog widget.
701 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
702 user_data is NULL (not used).
703
704 Returns TRUE to end propagation of event. */
705 static gboolean
706 dialog_delete_callback (w, event, user_data)
707 GtkWidget *w;
708 GdkEvent *event;
709 gpointer user_data;
710 {
711 gtk_widget_unmap (w);
712 return TRUE;
713 }
714
715 /* Create a popup dialog window. See also xg_create_widget below.
716 WV is a widget_value describing the dialog.
717 SELECT_CB is the callback to use when a button has been pressed.
718 DEACTIVATE_CB is the callback to use when the dialog pops down.
719
720 Returns the GTK dialog widget. */
721 static GtkWidget *
722 create_dialog (wv, select_cb, deactivate_cb)
723 widget_value *wv;
724 GCallback select_cb;
725 GCallback deactivate_cb;
726 {
727 char *title = get_dialog_title (wv->name[0]);
728 int total_buttons = wv->name[1] - '0';
729 int right_buttons = wv->name[4] - '0';
730 int left_buttons;
731 int button_nr = 0;
732 int button_spacing = 10;
733 GtkWidget *wdialog = gtk_dialog_new ();
734 widget_value *item;
735 GtkBox *cur_box;
736 GtkWidget *wvbox;
737 GtkWidget *whbox_up;
738 GtkWidget *whbox_down;
739
740 /* If the number of buttons is greater than 4, make two rows of buttons
741 instead. This looks better. */
742 int make_two_rows = total_buttons > 4;
743
744 if (right_buttons == 0) right_buttons = total_buttons/2;
745 left_buttons = total_buttons - right_buttons;
746
747 gtk_window_set_title (GTK_WINDOW (wdialog), title);
748 gtk_widget_set_name (wdialog, "emacs-dialog");
749
750 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
751
752 if (make_two_rows)
753 {
754 wvbox = gtk_vbox_new (TRUE, button_spacing);
755 whbox_up = gtk_hbox_new (FALSE, 0);
756 whbox_down = gtk_hbox_new (FALSE, 0);
757
758 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
759 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
760 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
761
762 cur_box = GTK_BOX (whbox_up);
763 }
764
765 g_signal_connect (G_OBJECT (wdialog), "delete-event",
766 G_CALLBACK (dialog_delete_callback), 0);
767
768 if (deactivate_cb)
769 {
770 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
771 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
772 }
773
774 for (item = wv->contents; item; item = item->next)
775 {
776 char *utf8_label = get_utf8_string (item->value);
777 GtkWidget *w;
778 GtkRequisition req;
779
780 if (item->name && strcmp (item->name, "message") == 0)
781 {
782 /* This is the text part of the dialog. */
783 w = gtk_label_new (utf8_label);
784 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
785 gtk_label_new (""),
786 FALSE, FALSE, 0);
787 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
788 TRUE, TRUE, 0);
789 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
790
791 /* Try to make dialog look better. Must realize first so
792 the widget can calculate the size it needs. */
793 gtk_widget_realize (w);
794 gtk_widget_size_request (w, &req);
795 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
796 req.height);
797 if (item->value && strlen (item->value) > 0)
798 button_spacing = 2*req.width/strlen (item->value);
799 }
800 else
801 {
802 /* This is one button to add to the dialog. */
803 w = gtk_button_new_with_mnemonic (utf8_label);
804 if (! item->enabled)
805 gtk_widget_set_sensitive (w, FALSE);
806 if (select_cb)
807 g_signal_connect (G_OBJECT (w), "clicked",
808 select_cb, item->call_data);
809
810 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
811 if (++button_nr == left_buttons)
812 {
813 if (make_two_rows)
814 cur_box = GTK_BOX (whbox_down);
815 else
816 gtk_box_pack_start (cur_box,
817 gtk_label_new (""),
818 TRUE, TRUE,
819 button_spacing);
820 }
821 }
822
823 if (utf8_label && utf8_label != item->value)
824 g_free (utf8_label);
825 }
826
827 return wdialog;
828 }
829
830
831 enum
832 {
833 XG_FILE_NOT_DONE,
834 XG_FILE_OK,
835 XG_FILE_CANCEL,
836 XG_FILE_DESTROYED,
837 };
838
839 /* Callback function invoked when the Ok button is pressed in
840 a file dialog.
841 W is the file dialog widget,
842 ARG points to an integer where we record what has happend. */
843 static void
844 xg_file_sel_ok (w, arg)
845 GtkWidget *w;
846 gpointer arg;
847 {
848 *(int*)arg = XG_FILE_OK;
849 }
850
851 /* Callback function invoked when the Cancel button is pressed in
852 a file dialog.
853 W is the file dialog widget,
854 ARG points to an integer where we record what has happend. */
855 static void
856 xg_file_sel_cancel (w, arg)
857 GtkWidget *w;
858 gpointer arg;
859 {
860 *(int*)arg = XG_FILE_CANCEL;
861 }
862
863 /* Callback function invoked when the file dialog is destroyed (i.e.
864 popped down). We must keep track of this, because if this
865 happens, GTK destroys the widget. But if for example, Ok is pressed,
866 the dialog is popped down, but the dialog widget is not destroyed.
867 W is the file dialog widget,
868 ARG points to an integer where we record what has happend. */
869 static void
870 xg_file_sel_destroy (w, arg)
871 GtkWidget *w;
872 gpointer arg;
873 {
874 *(int*)arg = XG_FILE_DESTROYED;
875 }
876
877 /* Read a file name from the user using a file dialog.
878 F is the current frame.
879 PROMPT is a prompt to show to the user. May not be NULL.
880 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
881 If MUSTMATCH_P is non-zero, the returned file name must be an existing
882 file.
883
884 Returns a file name or NULL if no file was selected.
885 The returned string must be freed by the caller. */
886 char *
887 xg_get_file_name (f, prompt, default_filename, mustmatch_p)
888 FRAME_PTR f;
889 char *prompt;
890 char *default_filename;
891 int mustmatch_p;
892 {
893 GtkWidget *filewin;
894 GtkFileSelection *filesel;
895 int filesel_done = XG_FILE_NOT_DONE;
896 char *fn = 0;
897
898 filewin = gtk_file_selection_new (prompt);
899 filesel = GTK_FILE_SELECTION (filewin);
900
901 gtk_widget_set_name (filewin, "emacs-filedialog");
902
903 gtk_window_set_transient_for (GTK_WINDOW (filewin),
904 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
905 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
906
907 g_signal_connect (G_OBJECT (filesel->ok_button),
908 "clicked",
909 G_CALLBACK (xg_file_sel_ok),
910 &filesel_done);
911 g_signal_connect (G_OBJECT (filesel->cancel_button),
912 "clicked",
913 G_CALLBACK (xg_file_sel_cancel),
914 &filesel_done);
915 g_signal_connect (G_OBJECT (filesel),
916 "destroy",
917 G_CALLBACK (xg_file_sel_destroy),
918 &filesel_done);
919
920 if (default_filename)
921 gtk_file_selection_set_filename (filesel, default_filename);
922
923 if (mustmatch_p)
924 {
925 /* The selection_entry part of filesel is not documented. */
926 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
927 gtk_file_selection_hide_fileop_buttons (filesel);
928 }
929
930
931 gtk_widget_show_all (filewin);
932
933 while (filesel_done == XG_FILE_NOT_DONE)
934 gtk_main_iteration ();
935
936 if (filesel_done == XG_FILE_OK)
937 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
938
939 if (filesel_done != XG_FILE_DESTROYED)
940 gtk_widget_destroy (filewin);
941
942 return fn;
943 }
944
945 \f
946 /***********************************************************************
947 Menu functions.
948 ***********************************************************************/
949
950 /* The name of menu items that can be used for citomization. Since GTK
951 RC files are very crude and primitive, we have to set this on all
952 menu item names so a user can easily cutomize menu items. */
953
954 #define MENU_ITEM_NAME "emacs-menuitem"
955
956
957 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
958 during GC. The next member points to the items. */
959 static xg_list_node xg_menu_cb_list;
960
961 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
962 during GC. The next member points to the items. */
963 static xg_list_node xg_menu_item_cb_list;
964
965 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
966 F is the frame CL_DATA will be initialized for.
967 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
968
969 The menu bar and all sub menus under the menu bar in a frame
970 share the same structure, hence the reference count.
971
972 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
973 allocated xg_menu_cb_data if CL_DATA is NULL. */
974 static xg_menu_cb_data *
975 make_cl_data (cl_data, f, highlight_cb)
976 xg_menu_cb_data *cl_data;
977 FRAME_PTR f;
978 GCallback highlight_cb;
979 {
980 if (! cl_data)
981 {
982 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
983 cl_data->f = f;
984 cl_data->menu_bar_vector = f->menu_bar_vector;
985 cl_data->menu_bar_items_used = f->menu_bar_items_used;
986 cl_data->highlight_cb = highlight_cb;
987 cl_data->ref_count = 0;
988
989 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
990 }
991
992 cl_data->ref_count++;
993
994 return cl_data;
995 }
996
997 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
998 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
999
1000 When the menu bar is updated, menu items may have been added and/or
1001 removed, so menu_bar_vector and menu_bar_items_used change. We must
1002 then update CL_DATA since it is used to determine which menu
1003 item that is invoked in the menu.
1004 HIGHLIGHT_CB could change, there is no check that the same
1005 function is given when modifying a menu bar as was given when
1006 creating the menu bar. */
1007 static void
1008 update_cl_data (cl_data, f, highlight_cb)
1009 xg_menu_cb_data *cl_data;
1010 FRAME_PTR f;
1011 GCallback highlight_cb;
1012 {
1013 if (cl_data)
1014 {
1015 cl_data->f = f;
1016 cl_data->menu_bar_vector = f->menu_bar_vector;
1017 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1018 cl_data->highlight_cb = highlight_cb;
1019 }
1020 }
1021
1022 /* Decrease reference count for CL_DATA.
1023 If reference count is zero, free CL_DATA. */
1024 static void
1025 unref_cl_data (cl_data)
1026 xg_menu_cb_data *cl_data;
1027 {
1028 if (cl_data && cl_data->ref_count > 0)
1029 {
1030 cl_data->ref_count--;
1031 if (cl_data->ref_count == 0)
1032 {
1033 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1034 xfree (cl_data);
1035 }
1036 }
1037 }
1038
1039 /* Function that marks all lisp data during GC. */
1040 void
1041 xg_mark_data ()
1042 {
1043 xg_list_node *iter;
1044
1045 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1046 mark_object (&((xg_menu_cb_data *) iter)->menu_bar_vector);
1047
1048 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1049 {
1050 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1051
1052 if (! NILP (cb_data->help))
1053 mark_object (&cb_data->help);
1054 }
1055 }
1056
1057
1058 /* Callback called when a menu item is destroyed. Used to free data.
1059 W is the widget that is being destroyed (not used).
1060 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1061 static void
1062 menuitem_destroy_callback (w, client_data)
1063 GtkWidget *w;
1064 gpointer client_data;
1065 {
1066 if (client_data)
1067 {
1068 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1069 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1070 xfree (data);
1071 }
1072 }
1073
1074 /* Callback called when the pointer enters/leaves a menu item.
1075 W is the menu item.
1076 EVENT is either an enter event or leave event.
1077 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1078
1079 Returns FALSE to tell GTK to keep processing this event. */
1080 static gboolean
1081 menuitem_highlight_callback (w, event, client_data)
1082 GtkWidget *w;
1083 GdkEventCrossing *event;
1084 gpointer client_data;
1085 {
1086 if (client_data)
1087 {
1088 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1089 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
1090
1091 if (! NILP (data->help) && data->cl_data->highlight_cb)
1092 {
1093 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1094 (*func) (w, call_data);
1095 }
1096 }
1097
1098 return FALSE;
1099 }
1100
1101 /* Callback called when a menu is destroyed. Used to free data.
1102 W is the widget that is being destroyed (not used).
1103 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1104 static void
1105 menu_destroy_callback (w, client_data)
1106 GtkWidget *w;
1107 gpointer client_data;
1108 {
1109 unref_cl_data ((xg_menu_cb_data*) client_data);
1110 }
1111
1112 /* Callback called when a menu does a grab or ungrab. That means the
1113 menu has been activated or deactivated.
1114 Used to start a timer so the small timeout the menus in GTK uses before
1115 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1116 W is the widget that does the grab (not used).
1117 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1118 CLIENT_DATA is NULL (not used). */
1119 static void
1120 menu_grab_callback (GtkWidget *widget,
1121 gboolean ungrab_p,
1122 gpointer client_data)
1123 {
1124 /* Keep track of total number of grabs. */
1125 static int cnt;
1126
1127 if (ungrab_p) cnt--;
1128 else cnt++;
1129
1130 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1131 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1132 }
1133
1134 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1135 must be non-NULL) and can be inserted into a menu item.
1136
1137 Returns the GtkHBox. */
1138 static GtkWidget *
1139 make_widget_for_menu_item (utf8_label, utf8_key)
1140 char *utf8_label;
1141 char *utf8_key;
1142 {
1143 GtkWidget *wlbl;
1144 GtkWidget *wkey;
1145 GtkWidget *wbox;
1146
1147 wbox = gtk_hbox_new (FALSE, 0);
1148 wlbl = gtk_label_new_with_mnemonic (utf8_label);
1149 wkey = gtk_label_new (utf8_key);
1150
1151 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1152 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1153
1154 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1155 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1156
1157 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1158 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1159
1160 return wbox;
1161 }
1162
1163 /* Make and return a menu item widget with the key to the right.
1164 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1165 UTF8_KEY is the text representing the key binding.
1166 ITEM is the widget_value describing the menu item.
1167
1168 GROUP is an in/out parameter. If the menu item to be created is not
1169 part of any radio menu group, *GROUP contains NULL on entry and exit.
1170 If the menu item to be created is part of a radio menu group, on entry
1171 *GROUP contains the group to use, or NULL if this is the first item
1172 in the group. On exit, *GROUP contains the radio item group.
1173
1174 Unfortunately, keys don't line up as nicely as in Motif,
1175 but the MacOS X version doesn't either, so I guess that is OK. */
1176 static GtkWidget *
1177 make_menu_item (utf8_label, utf8_key, item, group)
1178 char *utf8_label;
1179 char *utf8_key;
1180 widget_value *item;
1181 GSList **group;
1182 {
1183 GtkWidget *w;
1184 GtkWidget *wtoadd = 0;
1185
1186 if (utf8_key)
1187 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1188
1189 if (item->button_type == BUTTON_TYPE_TOGGLE)
1190 {
1191 *group = NULL;
1192 if (utf8_key) w = gtk_check_menu_item_new ();
1193 else w = gtk_check_menu_item_new_with_mnemonic (utf8_label);
1194 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1195 }
1196 else if (item->button_type == BUTTON_TYPE_RADIO)
1197 {
1198 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1199 else w = gtk_radio_menu_item_new_with_mnemonic (*group, utf8_label);
1200 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1201 if (item->selected)
1202 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1203 }
1204 else
1205 {
1206 *group = NULL;
1207 if (utf8_key) w = gtk_menu_item_new ();
1208 else w = gtk_menu_item_new_with_mnemonic (utf8_label);
1209 }
1210
1211 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1212 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1213
1214 return w;
1215 }
1216
1217 /* Return non-zero if NAME specifies a separator (GTK only has one
1218 separator type) */
1219 static int
1220 xg_separator_p (char *name)
1221 {
1222 if (! name) return 0;
1223
1224 return strcmp (name, "--") == 0
1225 || strncmp (name, "--:", 3) == 0
1226 || strcmp (name, "---") == 0;
1227 }
1228
1229 GtkWidget *xg_did_tearoff;
1230
1231 /* Callback invoked when a detached menu window is removed. Here we
1232 delete the popup menu.
1233 WIDGET is the top level window that is removed (the parent of the menu).
1234 EVENT is the event that triggers the window removal.
1235 CLIENT_DATA points to the menu that is detached.
1236
1237 Returns TRUE to tell GTK to stop processing this event. */
1238 static gboolean
1239 tearoff_remove (widget, event, client_data)
1240 GtkWidget *widget;
1241 GdkEvent *event;
1242 gpointer client_data;
1243 {
1244 gtk_widget_destroy (GTK_WIDGET (client_data));
1245 return TRUE;
1246 }
1247
1248 /* Callback invoked when a menu is detached. It sets the xg_did_tearoff
1249 variable.
1250 WIDGET is the GtkTearoffMenuItem.
1251 CLIENT_DATA is not used. */
1252 static void
1253 tearoff_activate (widget, client_data)
1254 GtkWidget *widget;
1255 gpointer client_data;
1256 {
1257 GtkWidget *menu = gtk_widget_get_parent (widget);
1258 if (! gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1259 return;
1260
1261 xg_did_tearoff = menu;
1262 }
1263
1264 /* If a detach of a popup menu is done, this function should be called
1265 to keep the menu around until the detached window is removed.
1266 MENU is the top level menu for the popup,
1267 SUBMENU is the menu that got detached (that is MENU or a
1268 submenu of MENU), see the xg_did_tearoff variable. */
1269 void
1270 xg_keep_popup (menu, submenu)
1271 GtkWidget *menu;
1272 GtkWidget *submenu;
1273 {
1274 GtkWidget *p;
1275
1276 /* Find the top widget for the detached menu. */
1277 p = gtk_widget_get_toplevel (submenu);
1278
1279 /* Delay destroying the menu until the detached menu is removed. */
1280 g_signal_connect (G_OBJECT (p), "unmap_event",
1281 G_CALLBACK (tearoff_remove), menu);
1282 }
1283
1284 int xg_debug = 0;
1285
1286 /* Create a menu item widget, and connect the callbacks.
1287 ITEM decribes the menu item.
1288 F is the frame the created menu belongs to.
1289 SELECT_CB is the callback to use when a menu item is selected.
1290 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1291 CL_DATA points to the callback data to be used for this menu.
1292 GROUP is an in/out parameter. If the menu item to be created is not
1293 part of any radio menu group, *GROUP contains NULL on entry and exit.
1294 If the menu item to be created is part of a radio menu group, on entry
1295 *GROUP contains the group to use, or NULL if this is the first item
1296 in the group. On exit, *GROUP contains the radio item group.
1297
1298 Returns the created GtkWidget. */
1299 static GtkWidget *
1300 xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1301 widget_value *item;
1302 FRAME_PTR f;
1303 GCallback select_cb;
1304 GCallback highlight_cb;
1305 xg_menu_cb_data *cl_data;
1306 GSList **group;
1307 {
1308 char *utf8_label;
1309 char *utf8_key;
1310 GtkWidget *w;
1311 xg_menu_item_cb_data *cb_data;
1312
1313 utf8_label = get_utf8_string (item->name);
1314 utf8_key = get_utf8_string (item->key);
1315
1316 w = make_menu_item (utf8_label, utf8_key, item, group);
1317
1318 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1319 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1320
1321 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1322
1323 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1324
1325 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1326 cb_data->help = item->help;
1327 cb_data->cl_data = cl_data;
1328 cb_data->call_data = item->call_data;
1329
1330 g_signal_connect (G_OBJECT (w),
1331 "destroy",
1332 G_CALLBACK (menuitem_destroy_callback),
1333 cb_data);
1334
1335 /* Put cb_data in widget, so we can get at it when modifying menubar */
1336 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1337
1338 /* final item, not a submenu */
1339 if (item->call_data && ! item->contents)
1340 {
1341 if (select_cb)
1342 cb_data->select_id
1343 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1344 }
1345
1346 if (! NILP (item->help) && highlight_cb)
1347 {
1348 /* We use enter/leave notify instead of select/deselect because
1349 select/deselect doesn't go well with detached menus. */
1350 cb_data->highlight_id
1351 = g_signal_connect (G_OBJECT (w),
1352 "enter-notify-event",
1353 G_CALLBACK (menuitem_highlight_callback),
1354 cb_data);
1355 cb_data->unhighlight_id
1356 = g_signal_connect (G_OBJECT (w),
1357 "leave-notify-event",
1358 G_CALLBACK (menuitem_highlight_callback),
1359 cb_data);
1360 }
1361
1362 return w;
1363 }
1364
1365 static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1366 GCallback, GCallback, int, int, int,
1367 GtkWidget *, xg_menu_cb_data *, char *));
1368
1369 /* Create a full menu tree specified by DATA.
1370 F is the frame the created menu belongs to.
1371 SELECT_CB is the callback to use when a menu item is selected.
1372 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1373 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1374 POP_UP_P is non-zero if we shall create a popup menu.
1375 MENU_BAR_P is non-zero if we shall create a menu bar.
1376 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1377 if MENU_BAR_P is non-zero.
1378 TOPMENU is the topmost GtkWidget that others shall be placed under.
1379 It may be NULL, in that case we create the appropriate widget
1380 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1381 CL_DATA is the callback data we shall use for this menu, or NULL
1382 if we haven't set the first callback yet.
1383 NAME is the name to give to the top level menu if this function
1384 creates it. May be NULL to not set any name.
1385
1386 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1387 not NULL.
1388
1389 This function calls itself to create submenus. */
1390
1391 static GtkWidget *
1392 create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1393 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1394 widget_value *data;
1395 FRAME_PTR f;
1396 GCallback select_cb;
1397 GCallback deactivate_cb;
1398 GCallback highlight_cb;
1399 int pop_up_p;
1400 int menu_bar_p;
1401 int add_tearoff_p;
1402 GtkWidget *topmenu;
1403 xg_menu_cb_data *cl_data;
1404 char *name;
1405 {
1406 widget_value *item;
1407 GtkWidget *wmenu = topmenu;
1408 GSList *group = NULL;
1409
1410 if (! topmenu)
1411 {
1412 if (! menu_bar_p) wmenu = gtk_menu_new ();
1413 else wmenu = gtk_menu_bar_new ();
1414
1415 /* Put cl_data on the top menu for easier access. */
1416 cl_data = make_cl_data (cl_data, f, highlight_cb);
1417 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1418 g_signal_connect (G_OBJECT (wmenu), "destroy",
1419 G_CALLBACK (menu_destroy_callback), cl_data);
1420
1421 if (name)
1422 gtk_widget_set_name (wmenu, name);
1423
1424 if (deactivate_cb)
1425 g_signal_connect (G_OBJECT (wmenu),
1426 "deactivate", deactivate_cb, 0);
1427
1428 g_signal_connect (G_OBJECT (wmenu),
1429 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1430 }
1431
1432 if (! menu_bar_p && add_tearoff_p)
1433 {
1434 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1435 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1436
1437 g_signal_connect (G_OBJECT (tearoff), "activate",
1438 G_CALLBACK (tearoff_activate), 0);
1439 }
1440
1441 for (item = data; item; item = item->next)
1442 {
1443 GtkWidget *w;
1444
1445 if (pop_up_p && !item->contents && !item->call_data
1446 && !xg_separator_p (item->name))
1447 {
1448 char *utf8_label;
1449 /* A title for a popup. We do the same as GTK does when
1450 creating titles, but it does not look good. */
1451 group = NULL;
1452 utf8_label = get_utf8_string (item->name);
1453
1454 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1455 w = gtk_menu_item_new_with_mnemonic (utf8_label);
1456 gtk_widget_set_sensitive (w, FALSE);
1457 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1458 }
1459 else if (xg_separator_p (item->name))
1460 {
1461 group = NULL;
1462 /* GTK only have one separator type. */
1463 w = gtk_separator_menu_item_new ();
1464 }
1465 else
1466 {
1467 w = xg_create_one_menuitem (item,
1468 f,
1469 item->contents ? 0 : select_cb,
1470 highlight_cb,
1471 cl_data,
1472 &group);
1473
1474 if (item->contents)
1475 {
1476 GtkWidget *submenu = create_menus (item->contents,
1477 f,
1478 select_cb,
1479 deactivate_cb,
1480 highlight_cb,
1481 0,
1482 0,
1483 1,
1484 0,
1485 cl_data,
1486 0);
1487 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1488 }
1489 }
1490
1491 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1492 gtk_widget_set_name (w, MENU_ITEM_NAME);
1493 }
1494
1495 return wmenu;
1496 }
1497
1498 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1499 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1500 with some text and buttons.
1501 F is the frame the created item belongs to.
1502 NAME is the name to use for the top widget.
1503 VAL is a widget_value structure describing items to be created.
1504 SELECT_CB is the callback to use when a menu item is selected or
1505 a dialog button is pressed.
1506 DEACTIVATE_CB is the callback to use when an item is deactivated.
1507 For a menu, when a sub menu is not shown anymore, for a dialog it is
1508 called when the dialog is popped down.
1509 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1510
1511 Returns the widget created. */
1512 GtkWidget *
1513 xg_create_widget (type, name, f, val,
1514 select_cb, deactivate_cb, highlight_cb)
1515 char *type;
1516 char *name;
1517 FRAME_PTR f;
1518 widget_value *val;
1519 GCallback select_cb;
1520 GCallback deactivate_cb;
1521 GCallback highlight_cb;
1522 {
1523 GtkWidget *w = 0;
1524 if (strcmp (type, "dialog") == 0)
1525 {
1526 w = create_dialog (val, select_cb, deactivate_cb);
1527 gtk_window_set_transient_for (GTK_WINDOW (w),
1528 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1529 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1530
1531 if (w)
1532 gtk_widget_set_name (w, "emacs-dialog");
1533 }
1534 else if (strcmp (type, "menubar") == 0 || strcmp (type, "popup") == 0)
1535 {
1536 w = create_menus (val->contents,
1537 f,
1538 select_cb,
1539 deactivate_cb,
1540 highlight_cb,
1541 strcmp (type, "popup") == 0,
1542 strcmp (type, "menubar") == 0,
1543 1,
1544 0,
1545 0,
1546 name);
1547
1548 /* Set the cursor to an arrow for popup menus when they are mapped.
1549 This is done by default for menu bar menus. */
1550 if (strcmp (type, "popup") == 0)
1551 {
1552 /* Must realize so the GdkWindow inside the widget is created. */
1553 gtk_widget_realize (w);
1554 xg_set_cursor (w, &xg_left_ptr_cursor);
1555 }
1556 }
1557 else
1558 {
1559 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1560 type);
1561 }
1562
1563 return w;
1564 }
1565
1566 /* Return the label for menu item WITEM. */
1567 static const char *
1568 xg_get_menu_item_label (witem)
1569 GtkMenuItem *witem;
1570 {
1571 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1572 return gtk_label_get_label (wlabel);
1573 }
1574
1575 /* Return non-zero if the menu item WITEM has the text LABEL. */
1576 static int
1577 xg_item_label_same_p (witem, label)
1578 GtkMenuItem *witem;
1579 char *label;
1580 {
1581 int is_same = 0;
1582 char *utf8_label = get_utf8_string (label);
1583 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1584
1585 if (! old_label && ! utf8_label)
1586 is_same = 1;
1587 else if (old_label && utf8_label)
1588 is_same = strcmp (utf8_label, old_label) == 0;
1589
1590 if (utf8_label && utf8_label != label) g_free (utf8_label);
1591
1592 return is_same;
1593 }
1594
1595 /* Remove widgets in LIST from container WCONT. */
1596 static void
1597 remove_from_container (wcont, list)
1598 GtkWidget *wcont;
1599 GList *list;
1600 {
1601 GList *iter;
1602
1603 for (iter = list; iter; iter = g_list_next (iter))
1604 {
1605 GtkWidget *w = GTK_WIDGET (iter->data);
1606
1607 /* Add a ref to w so we can explicitly destroy it later. */
1608 gtk_widget_ref (w);
1609 gtk_container_remove (GTK_CONTAINER (wcont), w);
1610
1611 /* If there is a menu under this widget that has been detached,
1612 there is a reference to it, and just removing w from the
1613 container does not destroy the submenu. By explicitly
1614 destroying w we make sure the submenu is destroyed, thus
1615 removing the detached window also if there was one. */
1616 gtk_widget_destroy (w);
1617 }
1618 }
1619
1620 /* Update the top level names in MENUBAR (i.e. not submenus).
1621 F is the frame the menu bar belongs to.
1622 *LIST is a list with the current menu bar names (menu item widgets).
1623 ITER is the item within *LIST that shall be updated.
1624 POS is the numerical position, starting at 0, of ITER in *LIST.
1625 VAL describes what the menu bar shall look like after the update.
1626 SELECT_CB is the callback to use when a menu item is selected.
1627 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1628 CL_DATA points to the callback data to be used for this menu bar.
1629
1630 This function calls itself to walk through the menu bar names. */
1631 static void
1632 xg_update_menubar (menubar, f, list, iter, pos, val,
1633 select_cb, highlight_cb, cl_data)
1634 GtkWidget *menubar;
1635 FRAME_PTR f;
1636 GList **list;
1637 GList *iter;
1638 int pos;
1639 widget_value *val;
1640 GCallback select_cb;
1641 GCallback highlight_cb;
1642 xg_menu_cb_data *cl_data;
1643 {
1644 if (! iter && ! val)
1645 return;
1646 else if (iter && ! val)
1647 {
1648 /* Item(s) have been removed. Remove all remaining items. */
1649 remove_from_container (menubar, iter);
1650
1651 /* All updated. */
1652 val = 0;
1653 iter = 0;
1654 }
1655 else if (! iter && val)
1656 {
1657 /* Item(s) added. Add all new items in one call. */
1658 create_menus (val, f, select_cb, 0, highlight_cb,
1659 0, 1, 0, menubar, cl_data, 0);
1660
1661 /* All updated. */
1662 val = 0;
1663 iter = 0;
1664 }
1665 /* Below this neither iter or val is NULL */
1666 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
1667 {
1668 /* This item is still the same, check next item. */
1669 val = val->next;
1670 iter = g_list_next (iter);
1671 ++pos;
1672 }
1673 else /* This item is changed. */
1674 {
1675 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
1676 GtkMenuItem *witem2 = 0;
1677 int val_in_menubar = 0;
1678 int iter_in_new_menubar = 0;
1679 GList *iter2;
1680 widget_value *cur;
1681
1682 /* See if the changed entry (val) is present later in the menu bar */
1683 for (iter2 = iter;
1684 iter2 && ! val_in_menubar;
1685 iter2 = g_list_next (iter2))
1686 {
1687 witem2 = GTK_MENU_ITEM (iter2->data);
1688 val_in_menubar = xg_item_label_same_p (witem2, val->name);
1689 }
1690
1691 /* See if the current entry (iter) is present later in the
1692 specification for the new menu bar. */
1693 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
1694 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
1695
1696 if (val_in_menubar && ! iter_in_new_menubar)
1697 {
1698 int nr = pos;
1699
1700 /* This corresponds to:
1701 Current: A B C
1702 New: A C
1703 Remove B. */
1704
1705 gtk_widget_ref (GTK_WIDGET (witem));
1706 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
1707 gtk_widget_destroy (GTK_WIDGET (witem));
1708
1709 /* Must get new list since the old changed. */
1710 g_list_free (*list);
1711 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1712 while (nr-- > 0) iter = g_list_next (iter);
1713 }
1714 else if (! val_in_menubar && ! iter_in_new_menubar)
1715 {
1716 /* This corresponds to:
1717 Current: A B C
1718 New: A X C
1719 Rename B to X. This might seem to be a strange thing to do,
1720 since if there is a menu under B it will be totally wrong for X.
1721 But consider editing a C file. Then there is a C-mode menu
1722 (corresponds to B above).
1723 If then doing C-x C-f the minibuf menu (X above) replaces the
1724 C-mode menu. When returning from the minibuffer, we get
1725 back the C-mode menu. Thus we do:
1726 Rename B to X (C-mode to minibuf menu)
1727 Rename X to B (minibuf to C-mode menu).
1728 If the X menu hasn't been invoked, the menu under B
1729 is up to date when leaving the minibuffer. */
1730 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1731 char *utf8_label = get_utf8_string (val->name);
1732
1733 gtk_label_set_text_with_mnemonic (wlabel, utf8_label);
1734
1735 iter = g_list_next (iter);
1736 val = val->next;
1737 ++pos;
1738 }
1739 else if (! val_in_menubar && iter_in_new_menubar)
1740 {
1741 /* This corresponds to:
1742 Current: A B C
1743 New: A X B C
1744 Insert X. */
1745
1746 int nr = pos;
1747 GList *group = 0;
1748 GtkWidget *w = xg_create_one_menuitem (val,
1749 f,
1750 select_cb,
1751 highlight_cb,
1752 cl_data,
1753 &group);
1754
1755 gtk_widget_set_name (w, MENU_ITEM_NAME);
1756 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
1757
1758 g_list_free (*list);
1759 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1760 while (nr-- > 0) iter = g_list_next (iter);
1761 iter = g_list_next (iter);
1762 val = val->next;
1763 ++pos;
1764 }
1765 else /* if (val_in_menubar && iter_in_new_menubar) */
1766 {
1767 int nr = pos;
1768 /* This corresponds to:
1769 Current: A B C
1770 New: A C B
1771 Move C before B */
1772
1773 gtk_widget_ref (GTK_WIDGET (witem2));
1774 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
1775 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
1776 GTK_WIDGET (witem2), pos);
1777 gtk_widget_unref (GTK_WIDGET (witem2));
1778
1779 g_list_free (*list);
1780 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1781 while (nr-- > 0) iter = g_list_next (iter);
1782 val = val->next;
1783 ++pos;
1784 }
1785 }
1786
1787 /* Update the rest of the menu bar. */
1788 xg_update_menubar (menubar, f, list, iter, pos, val,
1789 select_cb, highlight_cb, cl_data);
1790 }
1791
1792 /* Update the menu item W so it corresponds to VAL.
1793 SELECT_CB is the callback to use when a menu item is selected.
1794 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1795 CL_DATA is the data to set in the widget for menu invokation. */
1796 static void
1797 xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
1798 widget_value *val;
1799 GtkWidget *w;
1800 GCallback select_cb;
1801 GCallback highlight_cb;
1802 xg_menu_cb_data *cl_data;
1803 {
1804 GtkWidget *wchild;
1805 GtkLabel *wlbl = 0;
1806 GtkLabel *wkey = 0;
1807 char *utf8_label;
1808 char *utf8_key;
1809 const char *old_label = 0;
1810 const char *old_key = 0;
1811 xg_menu_item_cb_data *cb_data;
1812
1813 wchild = gtk_bin_get_child (GTK_BIN (w));
1814 utf8_label = get_utf8_string (val->name);
1815 utf8_key = get_utf8_string (val->key);
1816
1817 /* See if W is a menu item with a key. See make_menu_item above. */
1818 if (GTK_IS_HBOX (wchild))
1819 {
1820 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
1821
1822 wlbl = GTK_LABEL (list->data);
1823 wkey = GTK_LABEL (list->next->data);
1824 g_list_free (list);
1825
1826 if (! utf8_key)
1827 {
1828 /* Remove the key and keep just the label. */
1829 gtk_widget_ref (GTK_WIDGET (wlbl));
1830 gtk_container_remove (GTK_CONTAINER (w), wchild);
1831 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
1832 wkey = 0;
1833 }
1834
1835 }
1836 else /* Just a label. */
1837 {
1838 wlbl = GTK_LABEL (wchild);
1839
1840 /* Check if there is now a key. */
1841 if (utf8_key)
1842 {
1843 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1844 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
1845
1846 wlbl = GTK_LABEL (list->data);
1847 wkey = GTK_LABEL (list->next->data);
1848 g_list_free (list);
1849
1850 gtk_container_remove (GTK_CONTAINER (w), wchild);
1851 gtk_container_add (GTK_CONTAINER (w), wtoadd);
1852 }
1853 }
1854
1855
1856 if (wkey) old_key = gtk_label_get_label (wkey);
1857 if (wlbl) old_label = gtk_label_get_label (wlbl);
1858
1859 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
1860 gtk_label_set_text (wkey, utf8_key);
1861
1862 if (! old_label || strcmp (utf8_label, old_label) != 0)
1863 gtk_label_set_text_with_mnemonic (wlbl, utf8_label);
1864
1865 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
1866 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
1867
1868 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
1869 gtk_widget_set_sensitive (w, FALSE);
1870 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
1871 gtk_widget_set_sensitive (w, TRUE);
1872
1873 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
1874 XG_ITEM_DATA);
1875 if (cb_data)
1876 {
1877 cb_data->call_data = val->call_data;
1878 cb_data->help = val->help;
1879 cb_data->cl_data = cl_data;
1880
1881 /* We assume the callback functions don't change. */
1882 if (val->call_data && ! val->contents)
1883 {
1884 /* This item shall have a select callback. */
1885 if (! cb_data->select_id)
1886 cb_data->select_id
1887 = g_signal_connect (G_OBJECT (w), "activate",
1888 select_cb, cb_data);
1889 }
1890 else if (cb_data->select_id)
1891 {
1892 g_signal_handler_disconnect (w, cb_data->select_id);
1893 cb_data->select_id = 0;
1894 }
1895
1896 if (NILP (cb_data->help))
1897 {
1898 /* Shall not have help. Remove if any existed previously. */
1899 if (cb_data->highlight_id)
1900 {
1901 g_signal_handler_disconnect (G_OBJECT (w),
1902 cb_data->highlight_id);
1903 cb_data->highlight_id = 0;
1904 }
1905 if (cb_data->unhighlight_id)
1906 {
1907 g_signal_handler_disconnect (G_OBJECT (w),
1908 cb_data->unhighlight_id);
1909 cb_data->unhighlight_id = 0;
1910 }
1911 }
1912 else if (! cb_data->highlight_id && highlight_cb)
1913 {
1914 /* Have help now, but didn't previously. Add callback. */
1915 cb_data->highlight_id
1916 = g_signal_connect (G_OBJECT (w),
1917 "enter-notify-event",
1918 G_CALLBACK (menuitem_highlight_callback),
1919 cb_data);
1920 cb_data->unhighlight_id
1921 = g_signal_connect (G_OBJECT (w),
1922 "leave-notify-event",
1923 G_CALLBACK (menuitem_highlight_callback),
1924 cb_data);
1925 }
1926 }
1927 }
1928
1929 /* Update the toggle menu item W so it corresponds to VAL. */
1930 static void
1931 xg_update_toggle_item (val, w)
1932 widget_value *val;
1933 GtkWidget *w;
1934 {
1935 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
1936 }
1937
1938 /* Update the radio menu item W so it corresponds to VAL. */
1939 static void
1940 xg_update_radio_item (val, w)
1941 widget_value *val;
1942 GtkWidget *w;
1943 {
1944 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
1945 }
1946
1947 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
1948 SUBMENU may be NULL, in that case a new menu is created.
1949 F is the frame the menu bar belongs to.
1950 VAL describes the contents of the menu bar.
1951 SELECT_CB is the callback to use when a menu item is selected.
1952 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1953 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1954 CL_DATA is the call back data to use for any newly created items.
1955
1956 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
1957 was NULL. */
1958
1959 static GtkWidget *
1960 xg_update_submenu (submenu, f, val,
1961 select_cb, deactivate_cb, highlight_cb, cl_data)
1962 GtkWidget *submenu;
1963 FRAME_PTR f;
1964 widget_value *val;
1965 GCallback select_cb;
1966 GCallback deactivate_cb;
1967 GCallback highlight_cb;
1968 xg_menu_cb_data *cl_data;
1969 {
1970 GtkWidget *newsub = submenu;
1971 GList *list = 0;
1972 GList *iter;
1973 widget_value *cur;
1974 int has_tearoff_p = 0;
1975 GList *first_radio = 0;
1976
1977 if (submenu)
1978 list = gtk_container_get_children (GTK_CONTAINER (submenu));
1979
1980 for (cur = val, iter = list;
1981 cur && iter;
1982 iter = g_list_next (iter), cur = cur->next)
1983 {
1984 GtkWidget *w = GTK_WIDGET (iter->data);
1985
1986 /* Skip tearoff items, they have no counterpart in val. */
1987 if (GTK_IS_TEAROFF_MENU_ITEM (w))
1988 {
1989 has_tearoff_p = 1;
1990 iter = g_list_next (iter);
1991 if (iter) w = GTK_WIDGET (iter->data);
1992 else break;
1993 }
1994
1995 /* Remember first radio button in a group. If we get a mismatch in
1996 a radio group we must rebuild the whole group so that the connections
1997 in GTK becomes correct. */
1998 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
1999 first_radio = iter;
2000 else if (cur->button_type != BUTTON_TYPE_RADIO
2001 && ! GTK_IS_RADIO_MENU_ITEM (w))
2002 first_radio = 0;
2003
2004 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2005 {
2006 if (! xg_separator_p (cur->name))
2007 break;
2008 }
2009 else if (GTK_IS_CHECK_MENU_ITEM (w))
2010 {
2011 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2012 break;
2013 xg_update_toggle_item (cur, w);
2014 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2015 }
2016 else if (GTK_IS_RADIO_MENU_ITEM (w))
2017 {
2018 if (cur->button_type != BUTTON_TYPE_RADIO)
2019 break;
2020 xg_update_radio_item (cur, w);
2021 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2022 }
2023 else if (GTK_IS_MENU_ITEM (w))
2024 {
2025 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2026 GtkWidget *sub;
2027
2028 if (cur->button_type != BUTTON_TYPE_NONE ||
2029 xg_separator_p (cur->name))
2030 break;
2031
2032 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2033
2034 sub = gtk_menu_item_get_submenu (witem);
2035 if (sub && ! cur->contents)
2036 {
2037 /* Not a submenu anymore. */
2038 gtk_widget_ref (sub);
2039 gtk_menu_item_remove_submenu (witem);
2040 gtk_widget_destroy (sub);
2041 }
2042 else if (cur->contents)
2043 {
2044 GtkWidget *nsub;
2045
2046 nsub = xg_update_submenu (sub, f, cur->contents,
2047 select_cb, deactivate_cb,
2048 highlight_cb, cl_data);
2049
2050 /* If this item just became a submenu, we must set it. */
2051 if (nsub != sub)
2052 gtk_menu_item_set_submenu (witem, nsub);
2053 }
2054 }
2055 else
2056 {
2057 /* Structural difference. Remove everything from here and down
2058 in SUBMENU. */
2059 break;
2060 }
2061 }
2062
2063 /* Remove widgets from first structual change. */
2064 if (iter)
2065 {
2066 /* If we are adding new menu items below, we must remove from
2067 first radio button so that radio groups become correct. */
2068 if (cur && first_radio) remove_from_container (submenu, first_radio);
2069 else remove_from_container (submenu, iter);
2070 }
2071
2072 if (cur)
2073 {
2074 /* More items added. Create them. */
2075 newsub = create_menus (cur,
2076 f,
2077 select_cb,
2078 deactivate_cb,
2079 highlight_cb,
2080 0,
2081 0,
2082 ! has_tearoff_p,
2083 submenu,
2084 cl_data,
2085 0);
2086 }
2087
2088 if (list) g_list_free (list);
2089
2090 return newsub;
2091 }
2092
2093 /* Update the MENUBAR.
2094 F is the frame the menu bar belongs to.
2095 VAL describes the contents of the menu bar.
2096 If DEEP_P is non-zero, rebuild all but the top level menu names in
2097 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2098 SELECT_CB is the callback to use when a menu item is selected.
2099 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2100 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2101 void
2102 xg_modify_menubar_widgets (menubar, f, val, deep_p,
2103 select_cb, deactivate_cb, highlight_cb)
2104 GtkWidget *menubar;
2105 FRAME_PTR f;
2106 widget_value *val;
2107 int deep_p;
2108 GCallback select_cb;
2109 GCallback deactivate_cb;
2110 GCallback highlight_cb;
2111 {
2112 xg_menu_cb_data *cl_data;
2113 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2114
2115 if (! list) return;
2116
2117 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2118 XG_FRAME_DATA);
2119
2120 if (! deep_p)
2121 {
2122 widget_value *cur = val->contents;
2123 xg_update_menubar (menubar, f, &list, list, 0, cur,
2124 select_cb, highlight_cb, cl_data);
2125 }
2126 else
2127 {
2128 widget_value *cur;
2129
2130 /* Update all sub menus.
2131 We must keep the submenu names (GTK menu item widgets) since the
2132 X Window in the XEvent that activates the menu are those widgets. */
2133
2134 /* Update cl_data, menu_item things in F may have changed. */
2135 update_cl_data (cl_data, f, highlight_cb);
2136
2137 for (cur = val->contents; cur; cur = cur->next)
2138 {
2139 GList *iter;
2140 GtkWidget *sub = 0;
2141 GtkWidget *newsub;
2142 GtkMenuItem *witem;
2143
2144 /* Find sub menu that corresponds to val and update it. */
2145 for (iter = list ; iter; iter = g_list_next (iter))
2146 {
2147 witem = GTK_MENU_ITEM (iter->data);
2148 if (xg_item_label_same_p (witem, cur->name))
2149 {
2150 sub = gtk_menu_item_get_submenu (witem);
2151 break;
2152 }
2153 }
2154
2155 newsub = xg_update_submenu (sub,
2156 f,
2157 cur->contents,
2158 select_cb,
2159 deactivate_cb,
2160 highlight_cb,
2161 cl_data);
2162 /* sub may still be NULL. If we just updated non deep and added
2163 a new menu bar item, it has no sub menu yet. So we set the
2164 newly created sub menu under witem. */
2165 if (newsub != sub)
2166 gtk_menu_item_set_submenu (witem, newsub);
2167
2168 }
2169 }
2170
2171 g_list_free (list);
2172 gtk_widget_show_all (menubar);
2173 }
2174
2175 /* Recompute all the widgets of frame F, when the menu bar has been
2176 changed. Value is non-zero if widgets were updated. */
2177
2178 int
2179 xg_update_frame_menubar (f)
2180 FRAME_PTR f;
2181 {
2182 struct x_output *x = f->output_data.x;
2183 GtkRequisition req;
2184
2185 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2186 return 0;
2187
2188 BLOCK_INPUT;
2189
2190 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2191 FALSE, FALSE, 0);
2192 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2193
2194 gtk_widget_show_all (x->menubar_widget);
2195 gtk_widget_size_request (x->menubar_widget, &req);
2196
2197 FRAME_MENUBAR_HEIGHT (f) = req.height;
2198
2199 /* The height has changed, resize outer widget and set columns
2200 rows to what we had before adding the menu bar. */
2201 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2202
2203 SET_FRAME_GARBAGED (f);
2204 UNBLOCK_INPUT;
2205
2206 return 1;
2207 }
2208
2209 /* Get rid of the menu bar of frame F, and free its storage.
2210 This is used when deleting a frame, and when turning off the menu bar. */
2211
2212 void
2213 free_frame_menubar (f)
2214 FRAME_PTR f;
2215 {
2216 struct x_output *x = f->output_data.x;
2217
2218 if (x->menubar_widget)
2219 {
2220 BLOCK_INPUT;
2221
2222 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2223 /* The menubar and its children shall be deleted when removed from
2224 the container. */
2225 x->menubar_widget = 0;
2226 FRAME_MENUBAR_HEIGHT (f) = 0;
2227
2228 /* The height has changed, resize outer widget and set columns
2229 rows to what we had before removing the menu bar. */
2230 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2231
2232 SET_FRAME_GARBAGED (f);
2233 UNBLOCK_INPUT;
2234 }
2235 }
2236
2237
2238 \f
2239 /***********************************************************************
2240 Scroll bar functions
2241 ***********************************************************************/
2242
2243
2244 /* Setting scroll bar values invokes the callback. Use this variable
2245 to indicate that callback should do nothing. */
2246 int xg_ignore_gtk_scrollbar;
2247
2248 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2249 32 bits. But we want to store pointers, and they may be larger
2250 than 32 bits. Keep a mapping from integer index to widget pointers
2251 to get around the 32 bit limitation. */
2252 static struct
2253 {
2254 GtkWidget **widgets;
2255 int max_size;
2256 int used;
2257 } id_to_widget;
2258
2259 /* Grow this much every time we need to allocate more */
2260 #define ID_TO_WIDGET_INCR 32
2261
2262 /* Store the widget pointer W in id_to_widget and return the integer index. */
2263 static int
2264 xg_store_widget_in_map (w)
2265 GtkWidget *w;
2266 {
2267 int i;
2268
2269 if (id_to_widget.max_size == id_to_widget.used)
2270 {
2271 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2272
2273 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2274 sizeof (GtkWidget *)*new_size);
2275
2276 for (i = id_to_widget.max_size; i < new_size; ++i)
2277 id_to_widget.widgets[i] = 0;
2278 id_to_widget.max_size = new_size;
2279 }
2280
2281 /* Just loop over the array and find a free place. After all,
2282 how many scroll bars are we creating? Should be a small number.
2283 The check above guarantees we will find a free place. */
2284 for (i = 0; i < id_to_widget.max_size; ++i)
2285 {
2286 if (! id_to_widget.widgets[i])
2287 {
2288 id_to_widget.widgets[i] = w;
2289 ++id_to_widget.used;
2290
2291 return i;
2292 }
2293 }
2294
2295 /* Should never end up here */
2296 abort ();
2297 }
2298
2299 /* Remove pointer at IDX from id_to_widget.
2300 Called when scroll bar is destroyed. */
2301 static void
2302 xg_remove_widget_from_map (idx)
2303 int idx;
2304 {
2305 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2306 {
2307 id_to_widget.widgets[idx] = 0;
2308 --id_to_widget.used;
2309 }
2310 }
2311
2312 /* Get the widget pointer at IDX from id_to_widget. */
2313 static GtkWidget *
2314 xg_get_widget_from_map (idx)
2315 int idx;
2316 {
2317 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2318 return id_to_widget.widgets[idx];
2319
2320 return 0;
2321 }
2322
2323 /* Return the scrollbar id for X Window WID.
2324 Return -1 if WID not in id_to_widget. */
2325 int
2326 xg_get_scroll_id_for_window (wid)
2327 Window wid;
2328 {
2329 int idx;
2330 GtkWidget *w;
2331
2332 w = xg_win_to_widget (wid);
2333
2334 if (w)
2335 {
2336 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2337 if (id_to_widget.widgets[idx] == w)
2338 return idx;
2339 }
2340
2341 return -1;
2342 }
2343
2344 /* Callback invoked when scroll bar WIDGET is destroyed.
2345 DATA is the index into id_to_widget for WIDGET.
2346 We free pointer to last scroll bar values here and remove the index. */
2347 static void
2348 xg_gtk_scroll_destroy (widget, data)
2349 GtkWidget *widget;
2350 gpointer data;
2351 {
2352 gpointer p;
2353 int id = (int)data;
2354
2355 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2356 if (p) xfree (p);
2357 xg_remove_widget_from_map (id);
2358 }
2359
2360 /* Callback for button press/release events. Used to start timer so that
2361 the scroll bar repetition timer in GTK gets handeled.
2362 Also, sets bar->dragging to Qnil when dragging (button release) is done.
2363 WIDGET is the scroll bar widget the event is for (not used).
2364 EVENT contains the event.
2365 USER_DATA points to the struct scrollbar structure.
2366
2367 Returns FALSE to tell GTK that it shall continue propagate the event
2368 to widgets. */
2369 static gboolean
2370 scroll_bar_button_cb (widget, event, user_data)
2371 GtkWidget *widget;
2372 GdkEventButton *event;
2373 gpointer user_data;
2374 {
2375 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2376 xg_start_timer ();
2377 else if (event->type == GDK_BUTTON_RELEASE)
2378 {
2379 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2380 if (xg_timer) xg_stop_timer ();
2381 bar->dragging = Qnil;
2382 }
2383
2384 return FALSE;
2385 }
2386
2387 /* Create a scroll bar widget for frame F. Store the scroll bar
2388 in BAR.
2389 SCROLL_CALLBACK is the callback to invoke when the value of the
2390 bar changes.
2391 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2392 to set resources for the widget. */
2393 void
2394 xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2395 FRAME_PTR f;
2396 struct scroll_bar *bar;
2397 GCallback scroll_callback;
2398 char *scroll_bar_name;
2399 {
2400 GtkWidget *wscroll;
2401 GtkObject *vadj;
2402 int scroll_id;
2403
2404 /* Page, step increment values are not so important here, they
2405 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2406 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2407 0.1, 0.1, 0.1);
2408
2409 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2410 gtk_widget_set_name (wscroll, scroll_bar_name);
2411 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
2412
2413 scroll_id = xg_store_widget_in_map (wscroll);
2414
2415 g_signal_connect (G_OBJECT (vadj),
2416 "value-changed",
2417 scroll_callback,
2418 (gpointer)bar);
2419 g_signal_connect (G_OBJECT (wscroll),
2420 "destroy",
2421 G_CALLBACK (xg_gtk_scroll_destroy),
2422 (gpointer)scroll_id);
2423
2424 /* Connect to button press and button release to detect if any scroll bar
2425 has the pointer. */
2426 g_signal_connect (G_OBJECT (wscroll),
2427 "button-press-event",
2428 G_CALLBACK (scroll_bar_button_cb),
2429 (gpointer)bar);
2430 g_signal_connect (G_OBJECT (wscroll),
2431 "button-release-event",
2432 G_CALLBACK (scroll_bar_button_cb),
2433 (gpointer)bar);
2434
2435 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget),
2436 wscroll, -1, -1);
2437
2438 /* Set the cursor to an arrow. */
2439 xg_set_cursor (wscroll, &xg_left_ptr_cursor);
2440
2441 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2442 }
2443
2444 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2445 void
2446 xg_show_scroll_bar (scrollbar_id)
2447 int scrollbar_id;
2448 {
2449 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2450 if (w)
2451 gtk_widget_show (w);
2452 }
2453
2454 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2455 void
2456 xg_remove_scroll_bar (f, scrollbar_id)
2457 FRAME_PTR f;
2458 int scrollbar_id;
2459 {
2460 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2461 if (w)
2462 {
2463 gtk_widget_destroy (w);
2464 SET_FRAME_GARBAGED (f);
2465 }
2466 }
2467
2468 /* Find left/top for widget W in GtkFixed widget WFIXED. */
2469 static void
2470 xg_find_top_left_in_fixed (w, wfixed, left, top)
2471 GtkWidget *w, *wfixed;
2472 int *left, *top;
2473 {
2474 GList *iter;
2475
2476 for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter))
2477 {
2478 GtkFixedChild *child = (GtkFixedChild *) iter->data;
2479
2480 if (child->widget == w)
2481 {
2482 *left = child->x;
2483 *top = child->y;
2484 return;
2485 }
2486 }
2487
2488 /* Shall never end up here. */
2489 abort ();
2490 }
2491
2492 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2493 in frame F.
2494 TOP/LEFT are the new pixel positions where the bar shall appear.
2495 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2496 void
2497 xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
2498 FRAME_PTR f;
2499 int scrollbar_id;
2500 int top;
2501 int left;
2502 int width;
2503 int height;
2504 {
2505
2506 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
2507
2508 if (wscroll)
2509 {
2510 GtkWidget *wfixed = f->output_data.x->edit_widget;
2511 int gheight = max (height, 1);
2512 int canon_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
2513 int winextra = canon_width > width ? (canon_width - width) / 2 : 0;
2514 int bottom = top + gheight;
2515
2516 gint slider_width;
2517 int oldtop, oldleft, oldbottom;
2518 GtkRequisition req;
2519
2520 /* Get old values. */
2521 xg_find_top_left_in_fixed (wscroll, wfixed, &oldleft, &oldtop);
2522 gtk_widget_size_request (wscroll, &req);
2523 oldbottom = oldtop + req.height;
2524
2525 /* Scroll bars in GTK has a fixed width, so if we say width 16, it
2526 will only be its fixed width (14 is default) anyway, the rest is
2527 blank. We are drawing the mode line across scroll bars when
2528 the frame is split:
2529 |bar| |fringe|
2530 ----------------
2531 mode line
2532 ----------------
2533 |bar| |fringe|
2534
2535 When we "unsplit" the frame:
2536
2537 |bar| |fringe|
2538 -| |-| |
2539 m¦ |i| |
2540 -| |-| |
2541 | | | |
2542
2543
2544 the remains of the mode line can be seen in these blank spaces.
2545 So we must clear them explicitly.
2546 GTK scroll bars should do that, but they don't.
2547 Also, the scroll bar canonical width may be wider than the width
2548 passed in here. */
2549
2550 if (oldtop != -1 && oldleft != -1)
2551 {
2552 int gtkextra;
2553 int xl, xr, wblank;
2554 int bottomdiff, topdiff;
2555
2556 gtk_widget_style_get (wscroll, "slider_width", &slider_width, NULL);
2557 gtkextra = width > slider_width ? (width - slider_width) / 2 : 0;
2558
2559 xl = left - winextra;
2560 wblank = gtkextra + winextra;
2561 xr = left + gtkextra + slider_width;
2562 bottomdiff = abs (oldbottom - bottom);
2563 topdiff = abs (oldtop - top);
2564
2565 if (oldtop > top)
2566 {
2567 gdk_window_clear_area (wfixed->window, xl, top, wblank, topdiff);
2568 gdk_window_clear_area (wfixed->window, xr, top, wblank, topdiff);
2569 }
2570 else if (oldtop < top)
2571 {
2572 gdk_window_clear_area (wfixed->window, xl, oldtop, wblank,
2573 topdiff);
2574 gdk_window_clear_area (wfixed->window, xr, oldtop, wblank,
2575 topdiff);
2576 }
2577
2578 if (oldbottom > bottom)
2579 {
2580 gdk_window_clear_area (wfixed->window, xl, bottom, wblank,
2581 bottomdiff);
2582 gdk_window_clear_area (wfixed->window, xr, bottom, wblank,
2583 bottomdiff);
2584 }
2585 else if (oldbottom < bottom)
2586 {
2587 gdk_window_clear_area (wfixed->window, xl, oldbottom, wblank,
2588 bottomdiff);
2589 gdk_window_clear_area (wfixed->window, xr, oldbottom, wblank,
2590 bottomdiff);
2591 }
2592 }
2593
2594 /* Move and resize to new values. */
2595 gtk_fixed_move (GTK_FIXED (wfixed), wscroll, left, top);
2596 gtk_widget_set_size_request (wscroll, width, gheight);
2597
2598 gtk_container_set_reallocate_redraws (GTK_CONTAINER (wfixed), TRUE);
2599
2600 /* Make GTK draw the new sizes. We are not using a pure GTK event
2601 loop so we need to do this. */
2602 gdk_window_process_all_updates ();
2603
2604 SET_FRAME_GARBAGED (f);
2605 cancel_mouse_face (f);
2606 }
2607 }
2608
2609 /* Set the thumb size and position of scroll bar BAR. We are currently
2610 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2611 void
2612 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
2613 struct scroll_bar *bar;
2614 int portion, position, whole;
2615 {
2616 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
2617
2618 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
2619
2620 BLOCK_INPUT;
2621
2622 if (wscroll && NILP (bar->dragging))
2623 {
2624 GtkAdjustment *adj;
2625 gdouble shown;
2626 gdouble top;
2627 int size, value;
2628 int new_upper, new_step;
2629
2630 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
2631
2632 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
2633 rather than the real portion value. This makes the thumb less likely
2634 to resize and that looks better. */
2635 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
2636 /* When the thumb is at the bottom, position == whole.
2637 So we need to increase `whole' to make space for the thumb. */
2638 whole += portion;
2639
2640 if (whole <= 0)
2641 top = 0, shown = 1;
2642 else
2643 {
2644 shown = (gdouble) portion / whole;
2645 top = (gdouble) position / whole;
2646 }
2647
2648 size = shown * whole;
2649 size = min (size, whole);
2650 size = max (size, 1);
2651
2652 value = top * whole;
2653 value = min (value, whole - size);
2654 value = max (value, XG_SB_MIN);
2655
2656 /* gtk_range_set_value invokes the callback. Set
2657 ignore_gtk_scrollbar to make the callback do nothing */
2658 xg_ignore_gtk_scrollbar = 1;
2659
2660 new_upper = max (whole, size);
2661 new_step = portion / max (1, FRAME_HEIGHT (f));
2662
2663 if ((int) adj->page_size != size
2664 || (int) adj->upper != new_upper
2665 || (int) adj->step_increment != new_step)
2666 {
2667 adj->page_size = (int) size;
2668
2669 gtk_range_set_range (GTK_RANGE (wscroll), adj->lower,
2670 (gdouble) new_upper);
2671
2672 /* Assume all lines are of equal size. */
2673 /* Assume a page increment is about 95% of the page size */
2674 gtk_range_set_increments (GTK_RANGE (wscroll),
2675 portion / max (1, FRAME_HEIGHT (f)),
2676 (int) (0.95*adj->page_size));
2677
2678 }
2679
2680 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
2681 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
2682
2683 xg_ignore_gtk_scrollbar = 0;
2684
2685 /* Make GTK draw the new thumb. We are not using a pure GTK event
2686 loop so we need to do this. */
2687 gdk_window_process_all_updates ();
2688 }
2689
2690 UNBLOCK_INPUT;
2691 }
2692
2693 \f
2694 /***********************************************************************
2695 Tool bar functions
2696 ***********************************************************************/
2697 /* The key for the data we put in the GtkImage widgets. The data is
2698 the image used by Emacs. We use this to see if we need to update
2699 the GtkImage with a new image. */
2700 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2701
2702 /* Callback function invoked when a tool bar item is pressed.
2703 W is the button widget in the tool bar that got pressed,
2704 CLIENT_DATA is an integer that is the index of the button in the
2705 tool bar. 0 is the first button. */
2706 static void
2707 xg_tool_bar_callback (w, client_data)
2708 GtkWidget *w;
2709 gpointer client_data;
2710 {
2711 int idx = (int)client_data;
2712 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2713 Lisp_Object key, frame;
2714 struct input_event event;
2715
2716 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2717 return;
2718
2719 idx *= TOOL_BAR_ITEM_NSLOTS;
2720
2721 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
2722 XSETFRAME (frame, f);
2723 event.kind = TOOL_BAR_EVENT;
2724 event.frame_or_window = frame;
2725 event.arg = frame;
2726 kbd_buffer_store_event (&event);
2727
2728 event.kind = TOOL_BAR_EVENT;
2729 event.frame_or_window = frame;
2730 event.arg = key;
2731 event.modifiers = 0; /* These are not available. */
2732 kbd_buffer_store_event (&event);
2733 }
2734
2735 /* This callback is called when a tool bar is detached. We must set
2736 the height of the tool bar to zero when this happens so frame sizes
2737 are correctly calculated.
2738 WBOX is the handle box widget that enables detach/attach of the tool bar.
2739 W is the tool bar widget.
2740 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2741 static void
2742 xg_tool_bar_detach_callback (wbox, w, client_data)
2743 GtkHandleBox *wbox;
2744 GtkWidget *w;
2745 gpointer client_data;
2746 {
2747 FRAME_PTR f = (FRAME_PTR) client_data;
2748
2749 if (f)
2750 {
2751 /* When detaching a tool bar, not everything dissapear. There are
2752 a few pixels left that are used to drop the tool bar back into
2753 place. */
2754 int bw = gtk_container_get_border_width (GTK_CONTAINER (wbox));
2755 FRAME_TOOLBAR_HEIGHT (f) = 2;
2756
2757 /* The height has changed, resize outer widget and set columns
2758 rows to what we had before detaching the tool bar. */
2759 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2760 }
2761 }
2762
2763 /* This callback is called when a tool bar is reattached. We must set
2764 the height of the tool bar when this happens so frame sizes
2765 are correctly calculated.
2766 WBOX is the handle box widget that enables detach/attach of the tool bar.
2767 W is the tool bar widget.
2768 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2769 static void
2770 xg_tool_bar_attach_callback (wbox, w, client_data)
2771 GtkHandleBox *wbox;
2772 GtkWidget *w;
2773 gpointer client_data;
2774 {
2775 FRAME_PTR f = (FRAME_PTR) client_data;
2776
2777 if (f)
2778 {
2779 GtkRequisition req;
2780
2781 gtk_widget_size_request (w, &req);
2782 FRAME_TOOLBAR_HEIGHT (f) = req.height;
2783
2784 /* The height has changed, resize outer widget and set columns
2785 rows to what we had before detaching the tool bar. */
2786 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2787 }
2788 }
2789
2790 /* This callback is called when the mouse enters or leaves a tool bar item.
2791 It is used for displaying and hiding the help text.
2792 W is the tool bar item, a button.
2793 EVENT is either an enter event or leave event.
2794 CLIENT_DATA is an integer that is the index of the button in the
2795 tool bar. 0 is the first button.
2796
2797 Returns FALSE to tell GTK to keep processing this event. */
2798 static gboolean
2799 xg_tool_bar_help_callback (w, event, client_data)
2800 GtkWidget *w;
2801 GdkEventCrossing *event;
2802 gpointer client_data;
2803 {
2804 int idx = (int)client_data;
2805 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
2806 Lisp_Object help, frame;
2807
2808 if (! GTK_IS_BUTTON (w))
2809 {
2810 return FALSE;
2811 }
2812
2813 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
2814 return FALSE;
2815
2816 if (event->type == GDK_ENTER_NOTIFY)
2817 {
2818 idx *= TOOL_BAR_ITEM_NSLOTS;
2819 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
2820
2821 if (NILP (help))
2822 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
2823 }
2824 else
2825 help = Qnil;
2826
2827 XSETFRAME (frame, f);
2828 kbd_buffer_store_help_event (frame, help);
2829
2830 return FALSE;
2831 }
2832
2833
2834 /* This callback is called when a tool bar item shall be redrawn.
2835 It modifies the expose event so that the GtkImage widget redraws the
2836 whole image. This to overcome a bug that makes GtkImage draw the image
2837 in the wrong place when it tries to redraw just a part of the image.
2838 W is the GtkImage to be redrawn.
2839 EVENT is the expose event for W.
2840 CLIENT_DATA is unused.
2841
2842 Returns FALSE to tell GTK to keep processing this event. */
2843 static gboolean
2844 xg_tool_bar_item_expose_callback (w, event, client_data)
2845 GtkWidget *w;
2846 GdkEventExpose *event;
2847 gpointer client_data;
2848 {
2849 event->area.x = event->area.y = 0;
2850 event->area.width = event->area.height = 1000;
2851 return FALSE;
2852 }
2853
2854 /* This callback is called when a tool bar shall be redrawn.
2855 We need to update the tool bar from here in case the image cache
2856 has deleted the pixmaps used in the tool bar.
2857 W is the GtkToolbar to be redrawn.
2858 EVENT is the expose event for W.
2859 CLIENT_DATA is pointing to the frame for this tool bar.
2860
2861 Returns FALSE to tell GTK to keep processing this event. */
2862 static gboolean
2863 xg_tool_bar_expose_callback (w, event, client_data)
2864 GtkWidget *w;
2865 GdkEventExpose *event;
2866 gpointer client_data;
2867 {
2868 update_frame_tool_bar((FRAME_PTR)client_data);
2869 return FALSE;
2870 }
2871
2872 static void
2873 xg_create_tool_bar (f)
2874 FRAME_PTR f;
2875 {
2876 struct x_output *x = f->output_data.x;
2877 GtkRequisition req;
2878 int vbox_pos = x->menubar_widget ? 1 : 0;
2879
2880 x->toolbar_widget = gtk_toolbar_new ();
2881 x->handlebox_widget = gtk_handle_box_new ();
2882 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
2883 x->toolbar_widget);
2884
2885 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
2886 FALSE, FALSE, 0);
2887
2888 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
2889 vbox_pos);
2890
2891 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
2892
2893 /* We only have icons, so override any user setting. We could
2894 use the caption property of the toolbar item (see update_frame_tool_bar
2895 below), but some of those strings are long, making the toolbar so
2896 long it does not fit on the screen. The GtkToolbar widget makes every
2897 item equal size, so the longest caption determine the size of every
2898 tool bar item. I think the creators of the GtkToolbar widget
2899 counted on 4 or 5 character long strings. */
2900 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
2901 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
2902 GTK_ORIENTATION_HORIZONTAL);
2903
2904 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
2905 G_CALLBACK (xg_tool_bar_detach_callback), f);
2906 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
2907 G_CALLBACK (xg_tool_bar_attach_callback), f);
2908 g_signal_connect (G_OBJECT (x->toolbar_widget),
2909 "expose-event",
2910 G_CALLBACK (xg_tool_bar_expose_callback),
2911 f);
2912
2913 gtk_widget_show_all (x->handlebox_widget);
2914
2915 gtk_widget_size_request (x->toolbar_widget, &req);
2916 FRAME_TOOLBAR_HEIGHT (f) = req.height;
2917
2918 /* The height has changed, resize outer widget and set columns
2919 rows to what we had before adding the tool bar. */
2920 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2921
2922 SET_FRAME_GARBAGED (f);
2923 }
2924
2925 void
2926 update_frame_tool_bar (f)
2927 FRAME_PTR f;
2928 {
2929 int i;
2930 GtkRequisition old_req, new_req;
2931 GList *icon_list;
2932 GList *iter;
2933 struct x_output *x = f->output_data.x;
2934
2935 if (! FRAME_GTK_WIDGET (f))
2936 return;
2937
2938 BLOCK_INPUT;
2939
2940 if (! x->toolbar_widget)
2941 xg_create_tool_bar (f);
2942
2943 gtk_widget_size_request (x->toolbar_widget, &old_req);
2944
2945 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
2946 iter = icon_list;
2947
2948 for (i = 0; i < f->n_tool_bar_items; ++i)
2949 {
2950 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
2951
2952 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
2953 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
2954 int idx;
2955 int img_id;
2956 struct image *img;
2957 Lisp_Object image;
2958 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
2959
2960 if (iter) iter = g_list_next (iter);
2961
2962 /* If image is a vector, choose the image according to the
2963 button state. */
2964 image = PROP (TOOL_BAR_ITEM_IMAGES);
2965 if (VECTORP (image))
2966 {
2967 if (enabled_p)
2968 idx = (selected_p
2969 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
2970 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
2971 else
2972 idx = (selected_p
2973 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
2974 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2975
2976 xassert (ASIZE (image) >= idx);
2977 image = AREF (image, idx);
2978 }
2979 else
2980 idx = -1;
2981
2982 /* Ignore invalid image specifications. */
2983 if (!valid_image_p (image))
2984 {
2985 if (wicon) gtk_widget_hide (wicon);
2986 continue;
2987 }
2988
2989 img_id = lookup_image (f, image);
2990 img = IMAGE_FROM_ID (f, img_id);
2991 prepare_image_for_display (f, img);
2992
2993 if (img->load_failed_p || img->pixmap == None)
2994 {
2995 if (wicon) gtk_widget_hide (wicon);
2996 continue;
2997 }
2998
2999 if (! wicon)
3000 {
3001 GdkPixmap *gpix = gdk_pixmap_foreign_new (img->pixmap);
3002 GdkBitmap *gmask = img->mask ?
3003 (GdkBitmap*) gdk_pixmap_foreign_new (img->mask) : 0;
3004
3005 GtkWidget *w = gtk_image_new_from_pixmap (gpix, gmask);
3006 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3007 0, 0, 0,
3008 w,
3009 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
3010 (gpointer)i);
3011
3012 /* Save the image so we can see if an update is needed when
3013 this function is called again. */
3014 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
3015 (gpointer)img->pixmap);
3016
3017 /* Catch expose events to overcome an annoying redraw bug, see
3018 comment for xg_tool_bar_item_expose_callback. */
3019 g_signal_connect (G_OBJECT (w),
3020 "expose-event",
3021 G_CALLBACK (xg_tool_bar_item_expose_callback),
3022 0);
3023
3024 /* We must set sensitive on the button that is the parent
3025 of the GtkImage parent. Go upwards until we find the button. */
3026 while (! GTK_IS_BUTTON (w))
3027 w = gtk_widget_get_parent (w);
3028
3029 if (w)
3030 {
3031 /* Save the frame in the button so the xg_tool_bar_callback
3032 can get at it. */
3033 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3034 gtk_widget_set_sensitive (w, enabled_p);
3035
3036 /* Use enter/leave notify to show help. We use the events
3037 rather than the GtkButton specific signals "enter" and
3038 "leave", so we can have only one callback. The event
3039 will tell us what kind of event it is. */
3040 g_signal_connect (G_OBJECT (w),
3041 "enter-notify-event",
3042 G_CALLBACK (xg_tool_bar_help_callback),
3043 (gpointer)i);
3044 g_signal_connect (G_OBJECT (w),
3045 "leave-notify-event",
3046 G_CALLBACK (xg_tool_bar_help_callback),
3047 (gpointer)i);
3048 }
3049 }
3050 else
3051 {
3052 /* The child of the tool bar is a button. Inside that button
3053 is a vbox. Inside that vbox is the GtkImage. */
3054 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
3055 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3056 GtkImage *wimage = GTK_IMAGE (chlist->data);
3057 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3058 XG_TOOL_BAR_IMAGE_DATA);
3059 g_list_free (chlist);
3060
3061 if (old_img != img->pixmap)
3062 {
3063 GdkPixmap *gpix = gdk_pixmap_foreign_new (img->pixmap);
3064 GdkBitmap *gmask = img->mask ?
3065 (GdkBitmap*) gdk_pixmap_foreign_new (img->mask) : 0;
3066
3067 gtk_image_set_from_pixmap (wimage, gpix, gmask);
3068 }
3069
3070 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
3071 (gpointer)img->pixmap);
3072
3073 gtk_widget_set_sensitive (wicon, enabled_p);
3074 gtk_widget_show (wicon);
3075 }
3076
3077 #undef PROP
3078 }
3079
3080 /* Remove buttons not longer needed. We just hide them so they
3081 can be reused later on. */
3082 while (iter)
3083 {
3084 GtkWidget *w = GTK_WIDGET (iter->data);
3085 gtk_widget_hide (w);
3086 iter = g_list_next (iter);
3087 }
3088
3089 gtk_widget_size_request (x->toolbar_widget, &new_req);
3090 if (old_req.height != new_req.height)
3091 {
3092 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
3093 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
3094 }
3095
3096 /* Must force out update so changed images gets redrawn. */
3097 gdk_window_process_all_updates ();
3098
3099 if (icon_list) g_list_free (icon_list);
3100
3101 UNBLOCK_INPUT;
3102 }
3103
3104 void
3105 free_frame_tool_bar (f)
3106 FRAME_PTR f;
3107 {
3108 struct x_output *x = f->output_data.x;
3109
3110 if (x->toolbar_widget)
3111 {
3112 BLOCK_INPUT;
3113 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3114 x->handlebox_widget);
3115 x->toolbar_widget = 0;
3116 x->handlebox_widget = 0;
3117 FRAME_TOOLBAR_HEIGHT (f) = 0;
3118
3119 /* The height has changed, resize outer widget and set columns
3120 rows to what we had before removing the tool bar. */
3121 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
3122
3123 SET_FRAME_GARBAGED (f);
3124 UNBLOCK_INPUT;
3125 }
3126 }
3127
3128
3129 \f
3130 /***********************************************************************
3131 Initializing
3132 ***********************************************************************/
3133 void
3134 xg_initialize ()
3135 {
3136 xg_ignore_gtk_scrollbar = 0;
3137 xg_left_ptr_cursor = 0;
3138 xg_did_tearoff = 0;
3139
3140 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3141 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3142
3143 id_to_widget.max_size = id_to_widget.used = 0;
3144 id_to_widget.widgets = 0;
3145
3146 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3147 bindings. It doesn't seem to be any way to remove properties,
3148 so we set it to VoidSymbol which in X means "no key". */
3149 gtk_settings_set_string_property (gtk_settings_get_default (),
3150 "gtk-menu-bar-accel",
3151 "VoidSymbol",
3152 EMACS_CLASS);
3153
3154 /* Make GTK text input widgets use Emacs style keybindings. This is
3155 Emacs after all. */
3156 gtk_settings_set_string_property (gtk_settings_get_default (),
3157 "gtk-key-theme-name",
3158 "Emacs",
3159 EMACS_CLASS);
3160 }
3161
3162 #endif /* USE_GTK */