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