*** empty log message ***
[bpt/emacs.git] / src / gtkutil.c
CommitLineData
f392e843
JD
1/* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003
3 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include "config.h"
23
24#ifdef USE_GTK
e8794476
JD
25#include <string.h>
26#include <stdio.h>
f392e843
JD
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"
f392e843
JD
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))
cea9be54
JD
38
39
f392e843
JD
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. */
46static 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. */
50static GdkCursor *xg_left_ptr_cursor;
51
52
53/* The next two variables and functions are taken from lwlib. */
54static widget_value *widget_value_free_list;
55static 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. */
61widget_value *
62malloc_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. */
82void
83free_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. */
109void
110xg_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. */
140static void
141xg_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. */
155static void
156xg_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. */
170static void
171xg_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. */
181static void
182xg_list_insert (xg_list_node *list, xg_list_node *node)
183{
184 xg_list_node *list_start = list->next;
177c0ea7 185
f392e843
JD
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. */
193static void
194xg_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. */
213static char *
214get_utf8_string (str)
215 char *str;
216{
217 char *utf8_str = str;
177c0ea7 218
f392e843
JD
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. */
236static void
237xg_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];
177c0ea7 247
f392e843
JD
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
177c0ea7 265
f392e843
JD
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. */
269static void
270xg_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
0cb35f4e
JD
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). */
292void
293xg_frame_cleared (f)
294 FRAME_PTR f;
295{
296 GtkWidget *wfixed = f->output_data.x->edit_widget;
297
298 if (wfixed)
299 {
0cb35f4e
JD
300 gtk_container_set_reallocate_redraws (GTK_CONTAINER (wfixed), TRUE);
301 gdk_window_process_all_updates ();
302 }
303}
304
f392e843
JD
305/* Function to handle resize of our widgets. Since Emacs has some layouts
306 that does not fit well with GTK standard containers, we do most layout
307 manually.
308 F is the frame to resize.
309 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
310void
311xg_resize_widgets (f, pixelwidth, pixelheight)
312 FRAME_PTR f;
313 int pixelwidth, pixelheight;
314{
315 int mbheight = FRAME_MENUBAR_HEIGHT (f);
316 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
317 int rows = PIXEL_TO_CHAR_HEIGHT (f, pixelheight - mbheight - tbheight);
318 int columns = PIXEL_TO_CHAR_WIDTH (f, pixelwidth);
177c0ea7 319
f392e843
JD
320 if (FRAME_GTK_WIDGET (f)
321 && (columns != FRAME_WIDTH (f) || rows != FRAME_HEIGHT (f)
322 || pixelwidth != PIXEL_WIDTH (f) || pixelheight != PIXEL_HEIGHT (f)))
323 {
324 struct x_output *x = f->output_data.x;
325 GtkAllocation all;
326
327 all.y = mbheight + tbheight;
328 all.x = 0;
329
330 all.width = pixelwidth;
331 all.height = pixelheight - mbheight - tbheight;
332
333 gtk_widget_size_allocate (x->edit_widget, &all);
cea9be54 334
0cb35f4e 335 xg_frame_cleared (f);
f392e843
JD
336
337 change_frame_size (f, rows, columns, 0, 1, 0);
338 SET_FRAME_GARBAGED (f);
339 cancel_mouse_face (f);
340 }
341}
342
343
344/* Update our widget size to be COLS/ROWS characters for frame F. */
345void
346xg_frame_set_char_size (f, cols, rows)
347 FRAME_PTR f;
348 int cols;
349 int rows;
350{
351 int pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows)
352 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
353 int pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
177c0ea7 354
f392e843
JD
355 /* Take into account the size of the scroll bar. Always use the
356 number of columns occupied by the scroll bar here otherwise we
357 might end up with a frame width that is not a multiple of the
358 frame's character width which is bad for vertically split
359 windows. */
360 f->output_data.x->vertical_scroll_bar_extra
361 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
362 ? 0
363 : (FRAME_SCROLL_BAR_COLS (f)
364 * FONT_WIDTH (f->output_data.x->font)));
365
055d3c98 366 compute_fringe_widths (f, 0);
f392e843
JD
367
368 /* Must resize our top level widget. Font size may have changed,
369 but not rows/cols. */
370 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
371 pixelwidth, pixelheight);
372 xg_resize_widgets (f, pixelwidth, pixelheight);
373}
374
375/* Convert an X Window WSESC to its corresponding GtkWidget.
376 Must be done like this, because GtkWidget:s can have "hidden"
377 X Window that aren't accessible.
378
379 Return 0 if no widget match WDESC. */
380GtkWidget *
381xg_win_to_widget (wdesc)
382 Window wdesc;
383{
384 gpointer gdkwin;
385 GtkWidget *gwdesc = 0;
386
387 BLOCK_INPUT;
388 gdkwin = gdk_xid_table_lookup (wdesc);
389 if (gdkwin)
390 {
391 GdkEvent event;
392 event.any.window = gdkwin;
393 gwdesc = gtk_get_event_widget (&event);
394 }
177c0ea7 395
f392e843
JD
396 UNBLOCK_INPUT;
397 return gwdesc;
398}
399
400/* Fill in the GdkColor C so that it represents PIXEL.
401 W is the widget that color will be used for. Used to find colormap. */
402static void
403xg_pix_to_gcolor (w, pixel, c)
404 GtkWidget *w;
405 unsigned long pixel;
406 GdkColor *c;
407{
408 GdkColormap *map = gtk_widget_get_colormap (w);
409 gdk_colormap_query_color (map, pixel, c);
410}
411
412/* Create and set up the GTK widgets for frame F.
413 Return 0 if creation failed, non-zero otherwise. */
414int
415xg_create_frame_widgets (f)
416 FRAME_PTR f;
417{
418 GtkWidget *wtop;
419 GtkWidget *wvbox;
420 GtkWidget *wfixed;
421 GdkColor bg;
422 GtkRcStyle *style;
423 int i;
424 char *title = 0;
177c0ea7 425
f392e843
JD
426 BLOCK_INPUT;
427
428 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
429 wvbox = gtk_vbox_new (FALSE, 0);
430 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
177c0ea7 431
f392e843
JD
432 if (! wtop || ! wvbox || ! wfixed)
433 {
434 if (wtop) gtk_widget_destroy (wtop);
435 if (wvbox) gtk_widget_destroy (wvbox);
436 if (wfixed) gtk_widget_destroy (wfixed);
437
438 return 0;
439 }
440
441 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
442 gtk_widget_set_name (wtop, EMACS_CLASS);
443 gtk_widget_set_name (wvbox, "pane");
444 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
445
446 /* If this frame has a title or name, set it in the title bar. */
447 if (! NILP (f->title)) title = SDATA (f->title);
448 else if (! NILP (f->name)) title = SDATA (f->name);
449
450 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
177c0ea7 451
f392e843
JD
452 FRAME_GTK_OUTER_WIDGET (f) = wtop;
453 FRAME_GTK_WIDGET (f) = wfixed;
454 f->output_data.x->vbox_widget = wvbox;
455
456 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
457
458 gtk_widget_set_size_request (wfixed,
459 PIXEL_WIDTH (f),
460 PIXEL_HEIGHT (f));
177c0ea7 461
f392e843
JD
462 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
463 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
464
465 if (FRAME_EXTERNAL_TOOL_BAR (f))
466 update_frame_tool_bar (f);
467
468 /* The tool bar is created but first there are no items in it.
469 This causes it to be zero height. Later items are added, but then
470 the frame is already mapped, so there is a "jumping" resize.
471 This makes geometry handling difficult, for example -0-0 will end
472 up in the wrong place as tool bar height has not been taken into account.
473 So we cheat a bit by setting a height that is what it will have
474 later on when tool bar items are added. */
b73e73bf 475 if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0)
f392e843 476 FRAME_TOOLBAR_HEIGHT (f) = 34;
177c0ea7 477
f392e843
JD
478 gtk_widget_set_double_buffered (wvbox, FALSE);
479 gtk_widget_set_double_buffered (wfixed, FALSE);
480 gtk_widget_set_double_buffered (wtop, FALSE);
177c0ea7 481
f392e843
JD
482 /* GTK documents says use gtk_window_set_resizable. But then a user
483 can't shrink the window from its starting size. */
484 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
485 gtk_window_set_wmclass (GTK_WINDOW (wtop),
486 SDATA (Vx_resource_name),
487 SDATA (Vx_resource_class));
488
489 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
490 GTK is to destroy the widget. We want Emacs to do that instead. */
491 g_signal_connect (G_OBJECT (wtop), "delete-event",
492 G_CALLBACK (gtk_true), 0);
177c0ea7 493
f392e843
JD
494 /* Convert our geometry parameters into a geometry string
495 and specify it.
496 GTK will itself handle calculating the real position this way. */
497 xg_set_geometry (f);
498
499 gtk_widget_add_events (wfixed,
500 GDK_POINTER_MOTION_MASK
501 | GDK_EXPOSURE_MASK
502 | GDK_BUTTON_PRESS_MASK
503 | GDK_BUTTON_RELEASE_MASK
504 | GDK_KEY_PRESS_MASK
505 | GDK_ENTER_NOTIFY_MASK
506 | GDK_LEAVE_NOTIFY_MASK
507 | GDK_FOCUS_CHANGE_MASK
508 | GDK_STRUCTURE_MASK
509 | GDK_VISIBILITY_NOTIFY_MASK);
510
511 /* Must realize the windows so the X window gets created. It is used
512 by callers of this function. */
513 gtk_widget_realize (wfixed);
514 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
515
516 /* Since GTK clears its window by filling with the background color,
517 we must keep X and GTK background in sync. */
518 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
519 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
520
521 /* Also, do not let any background pixmap to be set, this looks very
522 bad as Emacs overwrites the background pixmap with its own idea
523 of background color. */
524 style = gtk_widget_get_modifier_style (wfixed);
525
526 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
527 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
528 gtk_widget_modify_style (wfixed, style);
177c0ea7 529
f392e843
JD
530 /* GTK does not set any border, and they look bad with GTK. */
531 f->output_data.x->border_width = 0;
532 f->output_data.x->internal_border_width = 0;
533
534 UNBLOCK_INPUT;
535
536 return 1;
537}
538
539/* Set the normal size hints for the window manager, for frame F.
540 FLAGS is the flags word to use--or 0 meaning preserve the flags
541 that the window now has.
542 If USER_POSITION is nonzero, we set the User Position
543 flag (this is useful when FLAGS is 0). */
544void
545x_wm_set_size_hint (f, flags, user_position)
546 FRAME_PTR f;
547 long flags;
548 int user_position;
549{
550 if (FRAME_GTK_OUTER_WIDGET (f))
551 {
552 /* Must use GTK routines here, otherwise GTK resets the size hints
553 to its own defaults. */
554 GdkGeometry size_hints;
555 gint hint_flags = 0;
556 int base_width, base_height;
557 int min_rows = 0, min_cols = 0;
558 int win_gravity = f->output_data.x->win_gravity;
177c0ea7 559
f392e843
JD
560 if (flags)
561 {
562 memset (&size_hints, 0, sizeof (size_hints));
563 f->output_data.x->size_hints = size_hints;
564 f->output_data.x->hint_flags = hint_flags;
565 }
566 else
567 flags = f->output_data.x->size_hint_flags;
177c0ea7 568
f392e843
JD
569 size_hints = f->output_data.x->size_hints;
570 hint_flags = f->output_data.x->hint_flags;
571
572 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
573 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
574 size_hints.height_inc = f->output_data.x->line_height;
575
576 hint_flags |= GDK_HINT_BASE_SIZE;
577 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
578 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0)
579 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
580
581 check_frame_size (f, &min_rows, &min_cols);
582
583 size_hints.base_width = base_width;
584 size_hints.base_height = base_height;
585 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
586 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
587
177c0ea7 588
f392e843
JD
589 /* These currently have a one to one mapping with the X values, but I
590 don't think we should rely on that. */
591 hint_flags |= GDK_HINT_WIN_GRAVITY;
592 size_hints.win_gravity = 0;
593 if (win_gravity == NorthWestGravity)
594 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
595 else if (win_gravity == NorthGravity)
596 size_hints.win_gravity = GDK_GRAVITY_NORTH;
597 else if (win_gravity == NorthEastGravity)
598 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
599 else if (win_gravity == WestGravity)
600 size_hints.win_gravity = GDK_GRAVITY_WEST;
601 else if (win_gravity == CenterGravity)
602 size_hints.win_gravity = GDK_GRAVITY_CENTER;
603 else if (win_gravity == EastGravity)
604 size_hints.win_gravity = GDK_GRAVITY_EAST;
605 else if (win_gravity == SouthWestGravity)
606 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
607 else if (win_gravity == SouthGravity)
608 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
609 else if (win_gravity == SouthEastGravity)
610 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
611 else if (win_gravity == StaticGravity)
612 size_hints.win_gravity = GDK_GRAVITY_STATIC;
613
614 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
615 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
616 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
617
618 if (user_position)
619 {
620 hint_flags &= ~GDK_HINT_POS;
621 hint_flags |= GDK_HINT_USER_POS;
622 }
623
624 BLOCK_INPUT;
625
626 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
627 FRAME_GTK_OUTER_WIDGET (f),
628 &size_hints,
629 hint_flags);
630
631 f->output_data.x->size_hints = size_hints;
632 f->output_data.x->hint_flags = hint_flags;
633 UNBLOCK_INPUT;
634 }
635}
636
637/* Change background color of a frame.
638 Since GTK uses the background colour to clear the window, we must
639 keep the GTK and X colors in sync.
640 F is the frame to change,
641 BG is the pixel value to change to. */
642void
643xg_set_background_color (f, bg)
644 FRAME_PTR f;
645 unsigned long bg;
646{
647 if (FRAME_GTK_WIDGET (f))
648 {
649 GdkColor gdk_bg;
650
651 BLOCK_INPUT;
652 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
653 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
654 UNBLOCK_INPUT;
655 }
656}
657
658
659\f
660/***********************************************************************
661 Dialog functions
662 ***********************************************************************/
663/* Return the dialog title to use for a dialog of type KEY.
664 This is the encoding used by lwlib. We use the same for GTK. */
665static char *
666get_dialog_title (char key)
667{
668 char *title = "";
177c0ea7 669
f392e843
JD
670 switch (key) {
671 case 'E': case 'e':
672 title = "Error";
673 break;
674
675 case 'I': case 'i':
676 title = "Information";
677 break;
678
679 case 'L': case 'l':
680 title = "Prompt";
681 break;
682
683 case 'P': case 'p':
684 title = "Prompt";
685 break;
686
687 case 'Q': case 'q':
688 title = "Question";
689 break;
690 }
691
692 return title;
693}
694
695/* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
696 the dialog, but return TRUE so the event does not propagate further
697 in GTK. This prevents GTK from destroying the dialog widget automatically
698 and we can always destrou the widget manually, regardles of how
699 it was popped down (button press or WM_DELETE_WINDOW).
700 W is the dialog widget.
701 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
702 user_data is NULL (not used).
703
704 Returns TRUE to end propagation of event. */
705static gboolean
706dialog_delete_callback (w, event, user_data)
707 GtkWidget *w;
708 GdkEvent *event;
709 gpointer user_data;
710{
711 gtk_widget_unmap (w);
712 return TRUE;
713}
714
715/* Create a popup dialog window. See also xg_create_widget below.
716 WV is a widget_value describing the dialog.
717 SELECT_CB is the callback to use when a button has been pressed.
718 DEACTIVATE_CB is the callback to use when the dialog pops down.
719
720 Returns the GTK dialog widget. */
721static GtkWidget *
722create_dialog (wv, select_cb, deactivate_cb)
723 widget_value *wv;
724 GCallback select_cb;
725 GCallback deactivate_cb;
726{
727 char *title = get_dialog_title (wv->name[0]);
728 int total_buttons = wv->name[1] - '0';
729 int right_buttons = wv->name[4] - '0';
730 int left_buttons;
731 int button_nr = 0;
732 int button_spacing = 10;
733 GtkWidget *wdialog = gtk_dialog_new ();
734 widget_value *item;
735 GtkBox *cur_box;
736 GtkWidget *wvbox;
737 GtkWidget *whbox_up;
738 GtkWidget *whbox_down;
739
740 /* If the number of buttons is greater than 4, make two rows of buttons
741 instead. This looks better. */
742 int make_two_rows = total_buttons > 4;
743
744 if (right_buttons == 0) right_buttons = total_buttons/2;
745 left_buttons = total_buttons - right_buttons;
746
747 gtk_window_set_title (GTK_WINDOW (wdialog), title);
748 gtk_widget_set_name (wdialog, "emacs-dialog");
749
750 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
751
752 if (make_two_rows)
753 {
754 wvbox = gtk_vbox_new (TRUE, button_spacing);
755 whbox_up = gtk_hbox_new (FALSE, 0);
756 whbox_down = gtk_hbox_new (FALSE, 0);
757
758 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
759 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
760 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
761
762 cur_box = GTK_BOX (whbox_up);
763 }
764
765 g_signal_connect (G_OBJECT (wdialog), "delete-event",
766 G_CALLBACK (dialog_delete_callback), 0);
177c0ea7 767
f392e843
JD
768 if (deactivate_cb)
769 {
770 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
771 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
772 }
773
774 for (item = wv->contents; item; item = item->next)
775 {
776 char *utf8_label = get_utf8_string (item->value);
777 GtkWidget *w;
778 GtkRequisition req;
779
0a1d6de0 780 if (item->name && strcmp (item->name, "message") == 0)
f392e843
JD
781 {
782 /* This is the text part of the dialog. */
783 w = gtk_label_new (utf8_label);
784 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
785 gtk_label_new (""),
786 FALSE, FALSE, 0);
787 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
788 TRUE, TRUE, 0);
789 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
790
791 /* Try to make dialog look better. Must realize first so
792 the widget can calculate the size it needs. */
793 gtk_widget_realize (w);
794 gtk_widget_size_request (w, &req);
795 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
796 req.height);
0a1d6de0 797 if (item->value && strlen (item->value) > 0)
f392e843
JD
798 button_spacing = 2*req.width/strlen (item->value);
799 }
800 else
801 {
802 /* This is one button to add to the dialog. */
803 w = gtk_button_new_with_mnemonic (utf8_label);
804 if (! item->enabled)
805 gtk_widget_set_sensitive (w, FALSE);
806 if (select_cb)
807 g_signal_connect (G_OBJECT (w), "clicked",
808 select_cb, item->call_data);
809
810 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
811 if (++button_nr == left_buttons)
812 {
813 if (make_two_rows)
814 cur_box = GTK_BOX (whbox_down);
815 else
816 gtk_box_pack_start (cur_box,
817 gtk_label_new (""),
818 TRUE, TRUE,
819 button_spacing);
820 }
821 }
822
823 if (utf8_label && utf8_label != item->value)
824 g_free (utf8_label);
825 }
826
827 return wdialog;
828}
829
830
831enum
832{
833 XG_FILE_NOT_DONE,
834 XG_FILE_OK,
835 XG_FILE_CANCEL,
836 XG_FILE_DESTROYED,
837};
838
839/* Callback function invoked when the Ok button is pressed in
840 a file dialog.
841 W is the file dialog widget,
842 ARG points to an integer where we record what has happend. */
843static void
844xg_file_sel_ok (w, arg)
845 GtkWidget *w;
846 gpointer arg;
847{
848 *(int*)arg = XG_FILE_OK;
849}
850
851/* Callback function invoked when the Cancel button is pressed in
852 a file dialog.
853 W is the file dialog widget,
854 ARG points to an integer where we record what has happend. */
855static void
856xg_file_sel_cancel (w, arg)
857 GtkWidget *w;
858 gpointer arg;
859{
860 *(int*)arg = XG_FILE_CANCEL;
861}
862
863/* Callback function invoked when the file dialog is destroyed (i.e.
864 popped down). We must keep track of this, because if this
865 happens, GTK destroys the widget. But if for example, Ok is pressed,
866 the dialog is popped down, but the dialog widget is not destroyed.
867 W is the file dialog widget,
868 ARG points to an integer where we record what has happend. */
869static void
870xg_file_sel_destroy (w, arg)
871 GtkWidget *w;
872 gpointer arg;
873{
874 *(int*)arg = XG_FILE_DESTROYED;
875}
876
877/* Read a file name from the user using a file dialog.
878 F is the current frame.
879 PROMPT is a prompt to show to the user. May not be NULL.
880 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
881 If MUSTMATCH_P is non-zero, the returned file name must be an existing
882 file.
883
884 Returns a file name or NULL if no file was selected.
885 The returned string must be freed by the caller. */
886char *
887xg_get_file_name (f, prompt, default_filename, mustmatch_p)
888 FRAME_PTR f;
889 char *prompt;
890 char *default_filename;
891 int mustmatch_p;
892{
893 GtkWidget *filewin;
894 GtkFileSelection *filesel;
895 int filesel_done = XG_FILE_NOT_DONE;
896 char *fn = 0;
177c0ea7 897
f392e843
JD
898 filewin = gtk_file_selection_new (prompt);
899 filesel = GTK_FILE_SELECTION (filewin);
900
901 gtk_widget_set_name (filewin, "emacs-filedialog");
902
903 gtk_window_set_transient_for (GTK_WINDOW (filewin),
904 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
905 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE);
906
907 g_signal_connect (G_OBJECT (filesel->ok_button),
908 "clicked",
909 G_CALLBACK (xg_file_sel_ok),
910 &filesel_done);
911 g_signal_connect (G_OBJECT (filesel->cancel_button),
912 "clicked",
913 G_CALLBACK (xg_file_sel_cancel),
914 &filesel_done);
915 g_signal_connect (G_OBJECT (filesel),
916 "destroy",
917 G_CALLBACK (xg_file_sel_destroy),
918 &filesel_done);
919
920 if (default_filename)
921 gtk_file_selection_set_filename (filesel, default_filename);
922
923 if (mustmatch_p)
924 {
925 /* The selection_entry part of filesel is not documented. */
926 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
927 gtk_file_selection_hide_fileop_buttons (filesel);
928 }
929
177c0ea7 930
f392e843 931 gtk_widget_show_all (filewin);
177c0ea7 932
f392e843
JD
933 while (filesel_done == XG_FILE_NOT_DONE)
934 gtk_main_iteration ();
935
936 if (filesel_done == XG_FILE_OK)
937 fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel));
938
939 if (filesel_done != XG_FILE_DESTROYED)
940 gtk_widget_destroy (filewin);
941
942 return fn;
943}
944
945\f
946/***********************************************************************
947 Menu functions.
948 ***********************************************************************/
949
950/* The name of menu items that can be used for citomization. Since GTK
951 RC files are very crude and primitive, we have to set this on all
952 menu item names so a user can easily cutomize menu items. */
953
954#define MENU_ITEM_NAME "emacs-menuitem"
955
956
957/* Linked list of all allocated struct xg_menu_cb_data. Used for marking
958 during GC. The next member points to the items. */
959static xg_list_node xg_menu_cb_list;
960
961/* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
962 during GC. The next member points to the items. */
963static xg_list_node xg_menu_item_cb_list;
964
965/* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
966 F is the frame CL_DATA will be initialized for.
967 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
968
969 The menu bar and all sub menus under the menu bar in a frame
970 share the same structure, hence the reference count.
177c0ea7 971
f392e843
JD
972 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
973 allocated xg_menu_cb_data if CL_DATA is NULL. */
974static xg_menu_cb_data *
975make_cl_data (cl_data, f, highlight_cb)
976 xg_menu_cb_data *cl_data;
977 FRAME_PTR f;
978 GCallback highlight_cb;
979{
980 if (! cl_data)
981 {
982 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
983 cl_data->f = f;
984 cl_data->menu_bar_vector = f->menu_bar_vector;
985 cl_data->menu_bar_items_used = f->menu_bar_items_used;
986 cl_data->highlight_cb = highlight_cb;
987 cl_data->ref_count = 0;
988
989 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
990 }
991
992 cl_data->ref_count++;
993
994 return cl_data;
995}
996
997/* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
998 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
999
1000 When the menu bar is updated, menu items may have been added and/or
1001 removed, so menu_bar_vector and menu_bar_items_used change. We must
1002 then update CL_DATA since it is used to determine which menu
1003 item that is invoked in the menu.
1004 HIGHLIGHT_CB could change, there is no check that the same
1005 function is given when modifying a menu bar as was given when
1006 creating the menu bar. */
1007static void
1008update_cl_data (cl_data, f, highlight_cb)
1009 xg_menu_cb_data *cl_data;
1010 FRAME_PTR f;
1011 GCallback highlight_cb;
1012{
1013 if (cl_data)
1014 {
1015 cl_data->f = f;
1016 cl_data->menu_bar_vector = f->menu_bar_vector;
1017 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1018 cl_data->highlight_cb = highlight_cb;
1019 }
1020}
1021
1022/* Decrease reference count for CL_DATA.
1023 If reference count is zero, free CL_DATA. */
1024static void
1025unref_cl_data (cl_data)
1026 xg_menu_cb_data *cl_data;
1027{
1028 if (cl_data && cl_data->ref_count > 0)
1029 {
1030 cl_data->ref_count--;
1031 if (cl_data->ref_count == 0)
1032 {
1033 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1034 xfree (cl_data);
1035 }
1036 }
1037}
1038
1039/* Function that marks all lisp data during GC. */
1040void
1041xg_mark_data ()
1042{
1043 xg_list_node *iter;
1044
1045 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1046 mark_object (&((xg_menu_cb_data *) iter)->menu_bar_vector);
1047
1048 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1049 {
1050 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1051
1052 if (! NILP (cb_data->help))
1053 mark_object (&cb_data->help);
1054 }
1055}
1056
1057
1058/* Callback called when a menu item is destroyed. Used to free data.
1059 W is the widget that is being destroyed (not used).
1060 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1061static void
1062menuitem_destroy_callback (w, client_data)
1063 GtkWidget *w;
1064 gpointer client_data;
1065{
1066 if (client_data)
1067 {
1068 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1069 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1070 xfree (data);
1071 }
1072}
1073
1074/* Callback called when the pointer enters/leaves a menu item.
1075 W is the menu item.
1076 EVENT is either an enter event or leave event.
1077 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1078
1079 Returns FALSE to tell GTK to keep processing this event. */
1080static gboolean
1081menuitem_highlight_callback (w, event, client_data)
1082 GtkWidget *w;
1083 GdkEventCrossing *event;
1084 gpointer client_data;
1085{
1086 if (client_data)
1087 {
1088 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1089 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
177c0ea7 1090
f392e843
JD
1091 if (! NILP (data->help) && data->cl_data->highlight_cb)
1092 {
1093 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1094 (*func) (w, call_data);
1095 }
1096 }
1097
1098 return FALSE;
1099}
1100
1101/* Callback called when a menu is destroyed. Used to free data.
1102 W is the widget that is being destroyed (not used).
1103 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1104static void
1105menu_destroy_callback (w, client_data)
1106 GtkWidget *w;
1107 gpointer client_data;
1108{
1109 unref_cl_data ((xg_menu_cb_data*) client_data);
1110}
1111
1112/* Callback called when a menu does a grab or ungrab. That means the
1113 menu has been activated or deactivated.
1114 Used to start a timer so the small timeout the menus in GTK uses before
1115 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1116 W is the widget that does the grab (not used).
1117 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1118 CLIENT_DATA is NULL (not used). */
1119static void
1120menu_grab_callback (GtkWidget *widget,
1121 gboolean ungrab_p,
1122 gpointer client_data)
1123{
1124 /* Keep track of total number of grabs. */
1125 static int cnt;
1126
1127 if (ungrab_p) cnt--;
1128 else cnt++;
1129
1130 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1131 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1132}
1133
1134/* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1135 must be non-NULL) and can be inserted into a menu item.
1136
1137 Returns the GtkHBox. */
1138static GtkWidget *
1139make_widget_for_menu_item (utf8_label, utf8_key)
1140 char *utf8_label;
1141 char *utf8_key;
1142{
1143 GtkWidget *wlbl;
1144 GtkWidget *wkey;
1145 GtkWidget *wbox;
177c0ea7 1146
f392e843
JD
1147 wbox = gtk_hbox_new (FALSE, 0);
1148 wlbl = gtk_label_new_with_mnemonic (utf8_label);
1149 wkey = gtk_label_new (utf8_key);
1150
1151 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1152 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
177c0ea7 1153
f392e843
JD
1154 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1155 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1156
1157 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1158 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1159
1160 return wbox;
1161}
1162
1163/* Make and return a menu item widget with the key to the right.
1164 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1165 UTF8_KEY is the text representing the key binding.
1166 ITEM is the widget_value describing the menu item.
177c0ea7 1167
f392e843
JD
1168 GROUP is an in/out parameter. If the menu item to be created is not
1169 part of any radio menu group, *GROUP contains NULL on entry and exit.
1170 If the menu item to be created is part of a radio menu group, on entry
1171 *GROUP contains the group to use, or NULL if this is the first item
1172 in the group. On exit, *GROUP contains the radio item group.
1173
1174 Unfortunately, keys don't line up as nicely as in Motif,
1175 but the MacOS X version doesn't either, so I guess that is OK. */
1176static GtkWidget *
1177make_menu_item (utf8_label, utf8_key, item, group)
1178 char *utf8_label;
1179 char *utf8_key;
1180 widget_value *item;
1181 GSList **group;
1182{
1183 GtkWidget *w;
1184 GtkWidget *wtoadd = 0;
177c0ea7 1185
f392e843
JD
1186 if (utf8_key)
1187 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
177c0ea7 1188
f392e843
JD
1189 if (item->button_type == BUTTON_TYPE_TOGGLE)
1190 {
1191 *group = NULL;
1192 if (utf8_key) w = gtk_check_menu_item_new ();
1193 else w = gtk_check_menu_item_new_with_mnemonic (utf8_label);
1194 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1195 }
1196 else if (item->button_type == BUTTON_TYPE_RADIO)
1197 {
1198 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1199 else w = gtk_radio_menu_item_new_with_mnemonic (*group, utf8_label);
1200 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1201 if (item->selected)
1202 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1203 }
1204 else
1205 {
1206 *group = NULL;
1207 if (utf8_key) w = gtk_menu_item_new ();
1208 else w = gtk_menu_item_new_with_mnemonic (utf8_label);
1209 }
177c0ea7 1210
f392e843
JD
1211 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1212 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1213
1214 return w;
1215}
1216
1217/* Return non-zero if NAME specifies a separator (GTK only has one
1218 separator type) */
1219static int
1220xg_separator_p (char *name)
1221{
0a1d6de0
JD
1222 if (! name) return 0;
1223
f392e843 1224 return strcmp (name, "--") == 0
0a1d6de0 1225 || strncmp (name, "--:", 3) == 0
f392e843
JD
1226 || strcmp (name, "---") == 0;
1227}
1228
1229GtkWidget *xg_did_tearoff;
1230
1231/* Callback invoked when a detached menu window is removed. Here we
1232 delete the popup menu.
1233 WIDGET is the top level window that is removed (the parent of the menu).
1234 EVENT is the event that triggers the window removal.
1235 CLIENT_DATA points to the menu that is detached.
1236
1237 Returns TRUE to tell GTK to stop processing this event. */
1238static gboolean
1239tearoff_remove (widget, event, client_data)
1240 GtkWidget *widget;
1241 GdkEvent *event;
1242 gpointer client_data;
1243{
1244 gtk_widget_destroy (GTK_WIDGET (client_data));
1245 return TRUE;
1246}
1247
1248/* Callback invoked when a menu is detached. It sets the xg_did_tearoff
1249 variable.
1250 WIDGET is the GtkTearoffMenuItem.
177c0ea7 1251 CLIENT_DATA is not used. */
f392e843
JD
1252static void
1253tearoff_activate (widget, client_data)
1254 GtkWidget *widget;
1255 gpointer client_data;
1256{
1257 GtkWidget *menu = gtk_widget_get_parent (widget);
1258 if (! gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1259 return;
1260
1261 xg_did_tearoff = menu;
1262}
1263
1264/* If a detach of a popup menu is done, this function should be called
1265 to keep the menu around until the detached window is removed.
1266 MENU is the top level menu for the popup,
1267 SUBMENU is the menu that got detached (that is MENU or a
1268 submenu of MENU), see the xg_did_tearoff variable. */
1269void
1270xg_keep_popup (menu, submenu)
1271 GtkWidget *menu;
1272 GtkWidget *submenu;
1273{
1274 GtkWidget *p;
1275
1276 /* Find the top widget for the detached menu. */
1277 p = gtk_widget_get_toplevel (submenu);
177c0ea7 1278
f392e843
JD
1279 /* Delay destroying the menu until the detached menu is removed. */
1280 g_signal_connect (G_OBJECT (p), "unmap_event",
1281 G_CALLBACK (tearoff_remove), menu);
1282}
1283
1284int xg_debug = 0;
1285
1286/* Create a menu item widget, and connect the callbacks.
1287 ITEM decribes the menu item.
1288 F is the frame the created menu belongs to.
1289 SELECT_CB is the callback to use when a menu item is selected.
1290 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1291 CL_DATA points to the callback data to be used for this menu.
1292 GROUP is an in/out parameter. If the menu item to be created is not
1293 part of any radio menu group, *GROUP contains NULL on entry and exit.
1294 If the menu item to be created is part of a radio menu group, on entry
1295 *GROUP contains the group to use, or NULL if this is the first item
1296 in the group. On exit, *GROUP contains the radio item group.
1297
1298 Returns the created GtkWidget. */
1299static GtkWidget *
1300xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1301 widget_value *item;
1302 FRAME_PTR f;
1303 GCallback select_cb;
1304 GCallback highlight_cb;
1305 xg_menu_cb_data *cl_data;
1306 GSList **group;
1307{
1308 char *utf8_label;
1309 char *utf8_key;
1310 GtkWidget *w;
1311 xg_menu_item_cb_data *cb_data;
1312
1313 utf8_label = get_utf8_string (item->name);
1314 utf8_key = get_utf8_string (item->key);
1315
1316 w = make_menu_item (utf8_label, utf8_key, item, group);
1317
1318 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1319 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1320
1321 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1322
1323 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1324
1325 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1326 cb_data->help = item->help;
1327 cb_data->cl_data = cl_data;
1328 cb_data->call_data = item->call_data;
177c0ea7 1329
f392e843
JD
1330 g_signal_connect (G_OBJECT (w),
1331 "destroy",
1332 G_CALLBACK (menuitem_destroy_callback),
1333 cb_data);
1334
1335 /* Put cb_data in widget, so we can get at it when modifying menubar */
1336 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1337
1338 /* final item, not a submenu */
1339 if (item->call_data && ! item->contents)
1340 {
1341 if (select_cb)
1342 cb_data->select_id
1343 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1344 }
1345
1346 if (! NILP (item->help) && highlight_cb)
1347 {
1348 /* We use enter/leave notify instead of select/deselect because
1349 select/deselect doesn't go well with detached menus. */
1350 cb_data->highlight_id
1351 = g_signal_connect (G_OBJECT (w),
1352 "enter-notify-event",
1353 G_CALLBACK (menuitem_highlight_callback),
1354 cb_data);
1355 cb_data->unhighlight_id
1356 = g_signal_connect (G_OBJECT (w),
1357 "leave-notify-event",
1358 G_CALLBACK (menuitem_highlight_callback),
1359 cb_data);
1360 }
1361
1362 return w;
1363}
1364
9d6194d6
AS
1365static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1366 GCallback, GCallback, int, int, int,
1367 GtkWidget *, xg_menu_cb_data *, char *));
1368
f392e843
JD
1369/* Create a full menu tree specified by DATA.
1370 F is the frame the created menu belongs to.
1371 SELECT_CB is the callback to use when a menu item is selected.
1372 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1373 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1374 POP_UP_P is non-zero if we shall create a popup menu.
1375 MENU_BAR_P is non-zero if we shall create a menu bar.
1376 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1377 if MENU_BAR_P is non-zero.
1378 TOPMENU is the topmost GtkWidget that others shall be placed under.
1379 It may be NULL, in that case we create the appropriate widget
1380 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1381 CL_DATA is the callback data we shall use for this menu, or NULL
1382 if we haven't set the first callback yet.
1383 NAME is the name to give to the top level menu if this function
1384 creates it. May be NULL to not set any name.
1385
1386 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1387 not NULL.
1388
1389 This function calls itself to create submenus. */
1390
1391static GtkWidget *
1392create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1393 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1394 widget_value *data;
1395 FRAME_PTR f;
1396 GCallback select_cb;
1397 GCallback deactivate_cb;
1398 GCallback highlight_cb;
1399 int pop_up_p;
1400 int menu_bar_p;
1401 int add_tearoff_p;
1402 GtkWidget *topmenu;
1403 xg_menu_cb_data *cl_data;
1404 char *name;
1405{
1406 widget_value *item;
1407 GtkWidget *wmenu = topmenu;
1408 GSList *group = NULL;
1409
1410 if (! topmenu)
1411 {
1412 if (! menu_bar_p) wmenu = gtk_menu_new ();
1413 else wmenu = gtk_menu_bar_new ();
1414
1415 /* Put cl_data on the top menu for easier access. */
1416 cl_data = make_cl_data (cl_data, f, highlight_cb);
1417 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1418 g_signal_connect (G_OBJECT (wmenu), "destroy",
1419 G_CALLBACK (menu_destroy_callback), cl_data);
177c0ea7 1420
f392e843
JD
1421 if (name)
1422 gtk_widget_set_name (wmenu, name);
1423
1424 if (deactivate_cb)
1425 g_signal_connect (G_OBJECT (wmenu),
1426 "deactivate", deactivate_cb, 0);
1427
1428 g_signal_connect (G_OBJECT (wmenu),
1429 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1430 }
177c0ea7 1431
f392e843
JD
1432 if (! menu_bar_p && add_tearoff_p)
1433 {
1434 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1435 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1436
1437 g_signal_connect (G_OBJECT (tearoff), "activate",
1438 G_CALLBACK (tearoff_activate), 0);
1439 }
1440
1441 for (item = data; item; item = item->next)
1442 {
1443 GtkWidget *w;
177c0ea7 1444
f392e843
JD
1445 if (pop_up_p && !item->contents && !item->call_data
1446 && !xg_separator_p (item->name))
1447 {
1448 char *utf8_label;
1449 /* A title for a popup. We do the same as GTK does when
1450 creating titles, but it does not look good. */
1451 group = NULL;
1452 utf8_label = get_utf8_string (item->name);
1453
1454 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
1455 w = gtk_menu_item_new_with_mnemonic (utf8_label);
1456 gtk_widget_set_sensitive (w, FALSE);
1457 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1458 }
1459 else if (xg_separator_p (item->name))
1460 {
1461 group = NULL;
1462 /* GTK only have one separator type. */
1463 w = gtk_separator_menu_item_new ();
1464 }
1465 else
1466 {
1467 w = xg_create_one_menuitem (item,
1468 f,
1469 item->contents ? 0 : select_cb,
1470 highlight_cb,
1471 cl_data,
1472 &group);
1473
1474 if (item->contents)
1475 {
1476 GtkWidget *submenu = create_menus (item->contents,
1477 f,
1478 select_cb,
1479 deactivate_cb,
1480 highlight_cb,
1481 0,
1482 0,
1483 1,
1484 0,
1485 cl_data,
1486 0);
1487 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1488 }
f392e843
JD
1489 }
1490
1491 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1492 gtk_widget_set_name (w, MENU_ITEM_NAME);
1493 }
1494
1495 return wmenu;
1496}
1497
1498/* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1499 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1500 with some text and buttons.
1501 F is the frame the created item belongs to.
1502 NAME is the name to use for the top widget.
1503 VAL is a widget_value structure describing items to be created.
1504 SELECT_CB is the callback to use when a menu item is selected or
1505 a dialog button is pressed.
1506 DEACTIVATE_CB is the callback to use when an item is deactivated.
1507 For a menu, when a sub menu is not shown anymore, for a dialog it is
1508 called when the dialog is popped down.
1509 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1510
1511 Returns the widget created. */
1512GtkWidget *
1513xg_create_widget (type, name, f, val,
1514 select_cb, deactivate_cb, highlight_cb)
1515 char *type;
1516 char *name;
1517 FRAME_PTR f;
1518 widget_value *val;
1519 GCallback select_cb;
1520 GCallback deactivate_cb;
1521 GCallback highlight_cb;
1522{
1523 GtkWidget *w = 0;
1524 if (strcmp (type, "dialog") == 0)
1525 {
1526 w = create_dialog (val, select_cb, deactivate_cb);
1527 gtk_window_set_transient_for (GTK_WINDOW (w),
1528 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1529 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1530
1531 if (w)
1532 gtk_widget_set_name (w, "emacs-dialog");
1533 }
1534 else if (strcmp (type, "menubar") == 0 || strcmp (type, "popup") == 0)
1535 {
1536 w = create_menus (val->contents,
1537 f,
1538 select_cb,
1539 deactivate_cb,
1540 highlight_cb,
1541 strcmp (type, "popup") == 0,
1542 strcmp (type, "menubar") == 0,
1543 1,
1544 0,
1545 0,
1546 name);
1547
1548 /* Set the cursor to an arrow for popup menus when they are mapped.
1549 This is done by default for menu bar menus. */
1550 if (strcmp (type, "popup") == 0)
1551 {
1552 /* Must realize so the GdkWindow inside the widget is created. */
1553 gtk_widget_realize (w);
1554 xg_set_cursor (w, &xg_left_ptr_cursor);
1555 }
1556 }
1557 else
1558 {
1559 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
1560 type);
1561 }
1562
1563 return w;
1564}
1565
0a1d6de0 1566/* Return the label for menu item WITEM. */
f392e843
JD
1567static const char *
1568xg_get_menu_item_label (witem)
1569 GtkMenuItem *witem;
1570{
1571 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1572 return gtk_label_get_label (wlabel);
1573}
1574
0a1d6de0 1575/* Return non-zero if the menu item WITEM has the text LABEL. */
f392e843
JD
1576static int
1577xg_item_label_same_p (witem, label)
1578 GtkMenuItem *witem;
1579 char *label;
1580{
0a1d6de0 1581 int is_same = 0;
f392e843 1582 char *utf8_label = get_utf8_string (label);
0a1d6de0
JD
1583 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
1584
1585 if (! old_label && ! utf8_label)
1586 is_same = 1;
1587 else if (old_label && utf8_label)
1588 is_same = strcmp (utf8_label, old_label) == 0;
1589
1590 if (utf8_label && utf8_label != label) g_free (utf8_label);
f392e843
JD
1591
1592 return is_same;
1593}
1594
1595/* Remove widgets in LIST from container WCONT. */
1596static void
1597remove_from_container (wcont, list)
1598 GtkWidget *wcont;
1599 GList *list;
1600{
f392e843
JD
1601 GList *iter;
1602
49853a4d 1603 for (iter = list; iter; iter = g_list_next (iter))
f392e843
JD
1604 {
1605 GtkWidget *w = GTK_WIDGET (iter->data);
1606
1607 /* Add a ref to w so we can explicitly destroy it later. */
1608 gtk_widget_ref (w);
1609 gtk_container_remove (GTK_CONTAINER (wcont), w);
177c0ea7 1610
f392e843
JD
1611 /* If there is a menu under this widget that has been detached,
1612 there is a reference to it, and just removing w from the
1613 container does not destroy the submenu. By explicitly
1614 destroying w we make sure the submenu is destroyed, thus
1615 removing the detached window also if there was one. */
1616 gtk_widget_destroy (w);
1617 }
f392e843
JD
1618}
1619
1620/* Update the top level names in MENUBAR (i.e. not submenus).
1621 F is the frame the menu bar belongs to.
49853a4d
JD
1622 *LIST is a list with the current menu bar names (menu item widgets).
1623 ITER is the item within *LIST that shall be updated.
1624 POS is the numerical position, starting at 0, of ITER in *LIST.
f392e843
JD
1625 VAL describes what the menu bar shall look like after the update.
1626 SELECT_CB is the callback to use when a menu item is selected.
1627 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
49853a4d 1628 CL_DATA points to the callback data to be used for this menu bar.
f392e843
JD
1629
1630 This function calls itself to walk through the menu bar names. */
1631static void
49853a4d
JD
1632xg_update_menubar (menubar, f, list, iter, pos, val,
1633 select_cb, highlight_cb, cl_data)
f392e843
JD
1634 GtkWidget *menubar;
1635 FRAME_PTR f;
49853a4d
JD
1636 GList **list;
1637 GList *iter;
1638 int pos;
f392e843
JD
1639 widget_value *val;
1640 GCallback select_cb;
1641 GCallback highlight_cb;
1642 xg_menu_cb_data *cl_data;
1643{
49853a4d 1644 if (! iter && ! val)
f392e843 1645 return;
49853a4d 1646 else if (iter && ! val)
f392e843 1647 {
49853a4d
JD
1648 /* Item(s) have been removed. Remove all remaining items. */
1649 remove_from_container (menubar, iter);
f392e843
JD
1650
1651 /* All updated. */
1652 val = 0;
49853a4d 1653 iter = 0;
f392e843 1654 }
49853a4d 1655 else if (! iter && val)
f392e843
JD
1656 {
1657 /* Item(s) added. Add all new items in one call. */
1658 create_menus (val, f, select_cb, 0, highlight_cb,
1659 0, 1, 0, menubar, cl_data, 0);
1660
1661 /* All updated. */
1662 val = 0;
49853a4d 1663 iter = 0;
f392e843 1664 }
49853a4d
JD
1665 /* Below this neither iter or val is NULL */
1666 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
f392e843
JD
1667 {
1668 /* This item is still the same, check next item. */
1669 val = val->next;
49853a4d
JD
1670 iter = g_list_next (iter);
1671 ++pos;
f392e843
JD
1672 }
1673 else /* This item is changed. */
1674 {
49853a4d 1675 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
f392e843 1676 GtkMenuItem *witem2 = 0;
f392e843 1677 int val_in_menubar = 0;
49853a4d
JD
1678 int iter_in_new_menubar = 0;
1679 GList *iter2;
f392e843
JD
1680 widget_value *cur;
1681
f392e843 1682 /* See if the changed entry (val) is present later in the menu bar */
49853a4d
JD
1683 for (iter2 = iter;
1684 iter2 && ! val_in_menubar;
1685 iter2 = g_list_next (iter2))
f392e843 1686 {
49853a4d 1687 witem2 = GTK_MENU_ITEM (iter2->data);
f392e843
JD
1688 val_in_menubar = xg_item_label_same_p (witem2, val->name);
1689 }
1690
49853a4d 1691 /* See if the current entry (iter) is present later in the
f392e843 1692 specification for the new menu bar. */
49853a4d
JD
1693 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
1694 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
f392e843 1695
49853a4d 1696 if (val_in_menubar && ! iter_in_new_menubar)
f392e843 1697 {
49853a4d
JD
1698 int nr = pos;
1699
f392e843
JD
1700 /* This corresponds to:
1701 Current: A B C
1702 New: A C
1703 Remove B. */
177c0ea7 1704
f392e843
JD
1705 gtk_widget_ref (GTK_WIDGET (witem));
1706 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
1707 gtk_widget_destroy (GTK_WIDGET (witem));
1708
1709 /* Must get new list since the old changed. */
49853a4d
JD
1710 g_list_free (*list);
1711 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1712 while (nr-- > 0) iter = g_list_next (iter);
f392e843 1713 }
49853a4d 1714 else if (! val_in_menubar && ! iter_in_new_menubar)
f392e843
JD
1715 {
1716 /* This corresponds to:
1717 Current: A B C
1718 New: A X C
1719 Rename B to X. This might seem to be a strange thing to do,
1720 since if there is a menu under B it will be totally wrong for X.
1721 But consider editing a C file. Then there is a C-mode menu
1722 (corresponds to B above).
1723 If then doing C-x C-f the minibuf menu (X above) replaces the
1724 C-mode menu. When returning from the minibuffer, we get
1725 back the C-mode menu. Thus we do:
1726 Rename B to X (C-mode to minibuf menu)
1727 Rename X to B (minibuf to C-mode menu).
1728 If the X menu hasn't been invoked, the menu under B
1729 is up to date when leaving the minibuffer. */
1730 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
1731 char *utf8_label = get_utf8_string (val->name);
177c0ea7 1732
f392e843
JD
1733 gtk_label_set_text_with_mnemonic (wlabel, utf8_label);
1734
49853a4d 1735 iter = g_list_next (iter);
f392e843 1736 val = val->next;
49853a4d 1737 ++pos;
f392e843 1738 }
49853a4d 1739 else if (! val_in_menubar && iter_in_new_menubar)
f392e843
JD
1740 {
1741 /* This corresponds to:
1742 Current: A B C
1743 New: A X B C
1744 Insert X. */
1745
49853a4d 1746 int nr = pos;
f392e843
JD
1747 GList *group = 0;
1748 GtkWidget *w = xg_create_one_menuitem (val,
1749 f,
1750 select_cb,
1751 highlight_cb,
1752 cl_data,
1753 &group);
1754
1755 gtk_widget_set_name (w, MENU_ITEM_NAME);
1756 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
1757
49853a4d
JD
1758 g_list_free (*list);
1759 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1760 while (nr-- > 0) iter = g_list_next (iter);
1761 iter = g_list_next (iter);
f392e843 1762 val = val->next;
49853a4d 1763 ++pos;
f392e843 1764 }
49853a4d 1765 else /* if (val_in_menubar && iter_in_new_menubar) */
f392e843 1766 {
49853a4d 1767 int nr = pos;
f392e843
JD
1768 /* This corresponds to:
1769 Current: A B C
1770 New: A C B
1771 Move C before B */
1772
1773 gtk_widget_ref (GTK_WIDGET (witem2));
1774 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
1775 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
1776 GTK_WIDGET (witem2), pos);
1777 gtk_widget_unref (GTK_WIDGET (witem2));
1778
49853a4d
JD
1779 g_list_free (*list);
1780 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
1781 while (nr-- > 0) iter = g_list_next (iter);
f392e843 1782 val = val->next;
49853a4d 1783 ++pos;
f392e843 1784 }
f392e843
JD
1785 }
1786
1787 /* Update the rest of the menu bar. */
49853a4d
JD
1788 xg_update_menubar (menubar, f, list, iter, pos, val,
1789 select_cb, highlight_cb, cl_data);
f392e843
JD
1790}
1791
1792/* Update the menu item W so it corresponds to VAL.
1793 SELECT_CB is the callback to use when a menu item is selected.
1794 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1795 CL_DATA is the data to set in the widget for menu invokation. */
1796static void
1797xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
1798 widget_value *val;
1799 GtkWidget *w;
1800 GCallback select_cb;
1801 GCallback highlight_cb;
1802 xg_menu_cb_data *cl_data;
1803{
1804 GtkWidget *wchild;
1805 GtkLabel *wlbl = 0;
1806 GtkLabel *wkey = 0;
1807 char *utf8_label;
1808 char *utf8_key;
0a1d6de0
JD
1809 const char *old_label = 0;
1810 const char *old_key = 0;
f392e843 1811 xg_menu_item_cb_data *cb_data;
177c0ea7
JB
1812
1813 wchild = gtk_bin_get_child (GTK_BIN (w));
f392e843
JD
1814 utf8_label = get_utf8_string (val->name);
1815 utf8_key = get_utf8_string (val->key);
1816
1817 /* See if W is a menu item with a key. See make_menu_item above. */
1818 if (GTK_IS_HBOX (wchild))
1819 {
1820 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
1821
1822 wlbl = GTK_LABEL (list->data);
1823 wkey = GTK_LABEL (list->next->data);
49853a4d
JD
1824 g_list_free (list);
1825
f392e843
JD
1826 if (! utf8_key)
1827 {
1828 /* Remove the key and keep just the label. */
1829 gtk_widget_ref (GTK_WIDGET (wlbl));
1830 gtk_container_remove (GTK_CONTAINER (w), wchild);
1831 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
1832 wkey = 0;
1833 }
49853a4d 1834
f392e843
JD
1835 }
1836 else /* Just a label. */
1837 {
1838 wlbl = GTK_LABEL (wchild);
1839
1840 /* Check if there is now a key. */
1841 if (utf8_key)
1842 {
1843 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1844 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
49853a4d 1845
f392e843
JD
1846 wlbl = GTK_LABEL (list->data);
1847 wkey = GTK_LABEL (list->next->data);
49853a4d 1848 g_list_free (list);
f392e843
JD
1849
1850 gtk_container_remove (GTK_CONTAINER (w), wchild);
1851 gtk_container_add (GTK_CONTAINER (w), wtoadd);
1852 }
1853 }
1854
177c0ea7 1855
0a1d6de0
JD
1856 if (wkey) old_key = gtk_label_get_label (wkey);
1857 if (wlbl) old_label = gtk_label_get_label (wlbl);
177c0ea7 1858
0a1d6de0 1859 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
f392e843
JD
1860 gtk_label_set_text (wkey, utf8_key);
1861
0a1d6de0 1862 if (! old_label || strcmp (utf8_label, old_label) != 0)
f392e843
JD
1863 gtk_label_set_text_with_mnemonic (wlbl, utf8_label);
1864
0a1d6de0
JD
1865 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
1866 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
177c0ea7 1867
f392e843
JD
1868 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
1869 gtk_widget_set_sensitive (w, FALSE);
1870 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
1871 gtk_widget_set_sensitive (w, TRUE);
1872
1873 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
1874 XG_ITEM_DATA);
1875 if (cb_data)
1876 {
1877 cb_data->call_data = val->call_data;
1878 cb_data->help = val->help;
1879 cb_data->cl_data = cl_data;
177c0ea7 1880
f392e843
JD
1881 /* We assume the callback functions don't change. */
1882 if (val->call_data && ! val->contents)
1883 {
1884 /* This item shall have a select callback. */
1885 if (! cb_data->select_id)
1886 cb_data->select_id
1887 = g_signal_connect (G_OBJECT (w), "activate",
1888 select_cb, cb_data);
1889 }
1890 else if (cb_data->select_id)
1891 {
1892 g_signal_handler_disconnect (w, cb_data->select_id);
1893 cb_data->select_id = 0;
1894 }
1895
1896 if (NILP (cb_data->help))
1897 {
1898 /* Shall not have help. Remove if any existed previously. */
1899 if (cb_data->highlight_id)
1900 {
1901 g_signal_handler_disconnect (G_OBJECT (w),
1902 cb_data->highlight_id);
1903 cb_data->highlight_id = 0;
1904 }
1905 if (cb_data->unhighlight_id)
1906 {
1907 g_signal_handler_disconnect (G_OBJECT (w),
1908 cb_data->unhighlight_id);
1909 cb_data->unhighlight_id = 0;
1910 }
1911 }
1912 else if (! cb_data->highlight_id && highlight_cb)
1913 {
1914 /* Have help now, but didn't previously. Add callback. */
1915 cb_data->highlight_id
1916 = g_signal_connect (G_OBJECT (w),
1917 "enter-notify-event",
1918 G_CALLBACK (menuitem_highlight_callback),
1919 cb_data);
1920 cb_data->unhighlight_id
1921 = g_signal_connect (G_OBJECT (w),
1922 "leave-notify-event",
1923 G_CALLBACK (menuitem_highlight_callback),
1924 cb_data);
1925 }
1926 }
1927}
1928
1929/* Update the toggle menu item W so it corresponds to VAL. */
1930static void
1931xg_update_toggle_item (val, w)
1932 widget_value *val;
1933 GtkWidget *w;
1934{
1935 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
1936}
1937
1938/* Update the radio menu item W so it corresponds to VAL. */
1939static void
1940xg_update_radio_item (val, w)
1941 widget_value *val;
1942 GtkWidget *w;
1943{
1944 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
1945}
1946
1947/* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
1948 SUBMENU may be NULL, in that case a new menu is created.
1949 F is the frame the menu bar belongs to.
1950 VAL describes the contents of the menu bar.
1951 SELECT_CB is the callback to use when a menu item is selected.
1952 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1953 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1954 CL_DATA is the call back data to use for any newly created items.
1955
1956 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
1957 was NULL. */
1958
1959static GtkWidget *
1960xg_update_submenu (submenu, f, val,
1961 select_cb, deactivate_cb, highlight_cb, cl_data)
1962 GtkWidget *submenu;
1963 FRAME_PTR f;
1964 widget_value *val;
1965 GCallback select_cb;
1966 GCallback deactivate_cb;
1967 GCallback highlight_cb;
1968 xg_menu_cb_data *cl_data;
1969{
1970 GtkWidget *newsub = submenu;
1971 GList *list = 0;
1972 GList *iter;
1973 widget_value *cur;
1974 int has_tearoff_p = 0;
1975 GList *first_radio = 0;
177c0ea7 1976
f392e843
JD
1977 if (submenu)
1978 list = gtk_container_get_children (GTK_CONTAINER (submenu));
177c0ea7 1979
f392e843
JD
1980 for (cur = val, iter = list;
1981 cur && iter;
1982 iter = g_list_next (iter), cur = cur->next)
1983 {
1984 GtkWidget *w = GTK_WIDGET (iter->data);
1985
1986 /* Skip tearoff items, they have no counterpart in val. */
1987 if (GTK_IS_TEAROFF_MENU_ITEM (w))
1988 {
1989 has_tearoff_p = 1;
1990 iter = g_list_next (iter);
1991 if (iter) w = GTK_WIDGET (iter->data);
1992 else break;
1993 }
1994
1995 /* Remember first radio button in a group. If we get a mismatch in
1996 a radio group we must rebuild the whole group so that the connections
1997 in GTK becomes correct. */
1998 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
1999 first_radio = iter;
2000 else if (cur->button_type != BUTTON_TYPE_RADIO
2001 && ! GTK_IS_RADIO_MENU_ITEM (w))
2002 first_radio = 0;
2003
2004 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2005 {
2006 if (! xg_separator_p (cur->name))
2007 break;
2008 }
2009 else if (GTK_IS_CHECK_MENU_ITEM (w))
2010 {
2011 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2012 break;
2013 xg_update_toggle_item (cur, w);
2014 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2015 }
2016 else if (GTK_IS_RADIO_MENU_ITEM (w))
2017 {
2018 if (cur->button_type != BUTTON_TYPE_RADIO)
2019 break;
2020 xg_update_radio_item (cur, w);
2021 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2022 }
2023 else if (GTK_IS_MENU_ITEM (w))
2024 {
2025 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2026 GtkWidget *sub;
2027
2028 if (cur->button_type != BUTTON_TYPE_NONE ||
2029 xg_separator_p (cur->name))
2030 break;
2031
2032 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2033
2034 sub = gtk_menu_item_get_submenu (witem);
2035 if (sub && ! cur->contents)
2036 {
2037 /* Not a submenu anymore. */
2038 gtk_widget_ref (sub);
2039 gtk_menu_item_remove_submenu (witem);
2040 gtk_widget_destroy (sub);
2041 }
2042 else if (cur->contents)
2043 {
2044 GtkWidget *nsub;
2045
2046 nsub = xg_update_submenu (sub, f, cur->contents,
2047 select_cb, deactivate_cb,
2048 highlight_cb, cl_data);
2049
2050 /* If this item just became a submenu, we must set it. */
2051 if (nsub != sub)
2052 gtk_menu_item_set_submenu (witem, nsub);
2053 }
2054 }
2055 else
2056 {
2057 /* Structural difference. Remove everything from here and down
2058 in SUBMENU. */
2059 break;
2060 }
2061 }
2062
2063 /* Remove widgets from first structual change. */
2064 if (iter)
2065 {
2066 /* If we are adding new menu items below, we must remove from
2067 first radio button so that radio groups become correct. */
2068 if (cur && first_radio) remove_from_container (submenu, first_radio);
2069 else remove_from_container (submenu, iter);
2070 }
177c0ea7 2071
f392e843
JD
2072 if (cur)
2073 {
2074 /* More items added. Create them. */
2075 newsub = create_menus (cur,
2076 f,
2077 select_cb,
2078 deactivate_cb,
2079 highlight_cb,
2080 0,
2081 0,
2082 ! has_tearoff_p,
2083 submenu,
2084 cl_data,
2085 0);
2086 }
177c0ea7 2087
49853a4d
JD
2088 if (list) g_list_free (list);
2089
f392e843
JD
2090 return newsub;
2091}
2092
2093/* Update the MENUBAR.
2094 F is the frame the menu bar belongs to.
2095 VAL describes the contents of the menu bar.
2096 If DEEP_P is non-zero, rebuild all but the top level menu names in
2097 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2098 SELECT_CB is the callback to use when a menu item is selected.
2099 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2100 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2101void
2102xg_modify_menubar_widgets (menubar, f, val, deep_p,
2103 select_cb, deactivate_cb, highlight_cb)
2104 GtkWidget *menubar;
2105 FRAME_PTR f;
2106 widget_value *val;
2107 int deep_p;
2108 GCallback select_cb;
2109 GCallback deactivate_cb;
2110 GCallback highlight_cb;
2111{
2112 xg_menu_cb_data *cl_data;
2113 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
f392e843
JD
2114
2115 if (! list) return;
177c0ea7 2116
f392e843
JD
2117 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2118 XG_FRAME_DATA);
2119
2120 if (! deep_p)
2121 {
2122 widget_value *cur = val->contents;
49853a4d 2123 xg_update_menubar (menubar, f, &list, list, 0, cur,
f392e843
JD
2124 select_cb, highlight_cb, cl_data);
2125 }
2126 else
2127 {
2128 widget_value *cur;
2129
2130 /* Update all sub menus.
2131 We must keep the submenu names (GTK menu item widgets) since the
2132 X Window in the XEvent that activates the menu are those widgets. */
2133
2134 /* Update cl_data, menu_item things in F may have changed. */
2135 update_cl_data (cl_data, f, highlight_cb);
2136
2137 for (cur = val->contents; cur; cur = cur->next)
2138 {
49853a4d 2139 GList *iter;
f392e843
JD
2140 GtkWidget *sub = 0;
2141 GtkWidget *newsub;
2142 GtkMenuItem *witem;
2143
2144 /* Find sub menu that corresponds to val and update it. */
2145 for (iter = list ; iter; iter = g_list_next (iter))
2146 {
2147 witem = GTK_MENU_ITEM (iter->data);
2148 if (xg_item_label_same_p (witem, cur->name))
2149 {
2150 sub = gtk_menu_item_get_submenu (witem);
2151 break;
2152 }
2153 }
177c0ea7 2154
f392e843
JD
2155 newsub = xg_update_submenu (sub,
2156 f,
2157 cur->contents,
2158 select_cb,
2159 deactivate_cb,
2160 highlight_cb,
2161 cl_data);
2162 /* sub may still be NULL. If we just updated non deep and added
2163 a new menu bar item, it has no sub menu yet. So we set the
2164 newly created sub menu under witem. */
2165 if (newsub != sub)
2166 gtk_menu_item_set_submenu (witem, newsub);
2167
2168 }
2169 }
2170
49853a4d 2171 g_list_free (list);
f392e843
JD
2172 gtk_widget_show_all (menubar);
2173}
2174
2175/* Recompute all the widgets of frame F, when the menu bar has been
2176 changed. Value is non-zero if widgets were updated. */
2177
2178int
2179xg_update_frame_menubar (f)
2180 FRAME_PTR f;
2181{
2182 struct x_output *x = f->output_data.x;
2183 GtkRequisition req;
177c0ea7 2184
f392e843
JD
2185 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2186 return 0;
2187
2188 BLOCK_INPUT;
2189
2190 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2191 FALSE, FALSE, 0);
2192 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2193
2194 gtk_widget_show_all (x->menubar_widget);
2195 gtk_widget_size_request (x->menubar_widget, &req);
2196
2197 FRAME_MENUBAR_HEIGHT (f) = req.height;
2198
2199 /* The height has changed, resize outer widget and set columns
2200 rows to what we had before adding the menu bar. */
2201 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
177c0ea7 2202
f392e843
JD
2203 SET_FRAME_GARBAGED (f);
2204 UNBLOCK_INPUT;
a8303f92
AS
2205
2206 return 1;
f392e843
JD
2207}
2208
2209/* Get rid of the menu bar of frame F, and free its storage.
2210 This is used when deleting a frame, and when turning off the menu bar. */
2211
2212void
2213free_frame_menubar (f)
2214 FRAME_PTR f;
2215{
2216 struct x_output *x = f->output_data.x;
2217
2218 if (x->menubar_widget)
2219 {
2220 BLOCK_INPUT;
2221
2222 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2223 /* The menubar and its children shall be deleted when removed from
2224 the container. */
2225 x->menubar_widget = 0;
2226 FRAME_MENUBAR_HEIGHT (f) = 0;
2227
2228 /* The height has changed, resize outer widget and set columns
2229 rows to what we had before removing the menu bar. */
2230 xg_resize_outer_widget (f, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2231
2232 SET_FRAME_GARBAGED (f);
2233 UNBLOCK_INPUT;
2234 }
2235}
2236
2237
2238\f
2239/***********************************************************************
2240 Scroll bar functions
2241 ***********************************************************************/
2242
2243
2244/* Setting scroll bar values invokes the callback. Use this variable
2245 to indicate that callback should do nothing. */
2246int xg_ignore_gtk_scrollbar;
2247
f392e843
JD
2248/* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2249 32 bits. But we want to store pointers, and they may be larger
2250 than 32 bits. Keep a mapping from integer index to widget pointers
2251 to get around the 32 bit limitation. */
2252static struct
2253{
2254 GtkWidget **widgets;
2255 int max_size;
2256 int used;
81e302ef 2257} id_to_widget;
f392e843
JD
2258
2259/* Grow this much every time we need to allocate more */
2260#define ID_TO_WIDGET_INCR 32
2261
2262/* Store the widget pointer W in id_to_widget and return the integer index. */
2263static int
2264xg_store_widget_in_map (w)
2265 GtkWidget *w;
2266{
2267 int i;
2268
2269 if (id_to_widget.max_size == id_to_widget.used)
2270 {
2271 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2272
2273 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2274 sizeof (GtkWidget *)*new_size);
2275
2276 for (i = id_to_widget.max_size; i < new_size; ++i)
2277 id_to_widget.widgets[i] = 0;
2278 id_to_widget.max_size = new_size;
2279 }
2280
2281 /* Just loop over the array and find a free place. After all,
2282 how many scroll bars are we creating? Should be a small number.
2283 The check above guarantees we will find a free place. */
2284 for (i = 0; i < id_to_widget.max_size; ++i)
2285 {
2286 if (! id_to_widget.widgets[i])
2287 {
2288 id_to_widget.widgets[i] = w;
2289 ++id_to_widget.used;
2290
2291 return i;
2292 }
2293 }
2294
2295 /* Should never end up here */
2296 abort ();
2297}
2298
2299/* Remove pointer at IDX from id_to_widget.
2300 Called when scroll bar is destroyed. */
2301static void
2302xg_remove_widget_from_map (idx)
2303 int idx;
2304{
2305 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2306 {
2307 id_to_widget.widgets[idx] = 0;
2308 --id_to_widget.used;
2309 }
2310}
2311
2312/* Get the widget pointer at IDX from id_to_widget. */
2313static GtkWidget *
2314xg_get_widget_from_map (idx)
2315 int idx;
2316{
2317 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2318 return id_to_widget.widgets[idx];
2319
2320 return 0;
2321}
2322
3a8a22fc
JD
2323/* Return the scrollbar id for X Window WID.
2324 Return -1 if WID not in id_to_widget. */
2325int
2326xg_get_scroll_id_for_window (wid)
2327 Window wid;
2328{
2329 int idx;
2330 GtkWidget *w;
2331
2332 w = xg_win_to_widget (wid);
2333
2334 if (w)
2335 {
2336 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2337 if (id_to_widget.widgets[idx] == w)
2338 return idx;
2339 }
2340
2341 return -1;
2342}
2343
f392e843
JD
2344/* Callback invoked when scroll bar WIDGET is destroyed.
2345 DATA is the index into id_to_widget for WIDGET.
cea9be54 2346 We free pointer to last scroll bar values here and remove the index. */
f392e843
JD
2347static void
2348xg_gtk_scroll_destroy (widget, data)
2349 GtkWidget *widget;
2350 gpointer data;
2351{
2352 gpointer p;
2353 int id = (int)data;
177c0ea7 2354
f392e843
JD
2355 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2356 if (p) xfree (p);
2357 xg_remove_widget_from_map (id);
2358}
2359
2360/* Callback for button press/release events. Used to start timer so that
2361 the scroll bar repetition timer in GTK gets handeled.
17097258 2362 Also, sets bar->dragging to Qnil when dragging (button release) is done.
f392e843
JD
2363 WIDGET is the scroll bar widget the event is for (not used).
2364 EVENT contains the event.
17097258 2365 USER_DATA points to the struct scrollbar structure.
f392e843
JD
2366
2367 Returns FALSE to tell GTK that it shall continue propagate the event
2368 to widgets. */
2369static gboolean
2370scroll_bar_button_cb (widget, event, user_data)
2371 GtkWidget *widget;
2372 GdkEventButton *event;
2373 gpointer user_data;
2374{
2375 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2376 xg_start_timer ();
17097258
JD
2377 else if (event->type == GDK_BUTTON_RELEASE)
2378 {
2379 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2380 if (xg_timer) xg_stop_timer ();
2381 bar->dragging = Qnil;
2382 }
2383
f392e843
JD
2384 return FALSE;
2385}
2386
2387/* Create a scroll bar widget for frame F. Store the scroll bar
2388 in BAR.
2389 SCROLL_CALLBACK is the callback to invoke when the value of the
2390 bar changes.
2391 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2392 to set resources for the widget. */
2393void
2394xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2395 FRAME_PTR f;
2396 struct scroll_bar *bar;
2397 GCallback scroll_callback;
2398 char *scroll_bar_name;
2399{
2400 GtkWidget *wscroll;
2401 GtkObject *vadj;
2402 int scroll_id;
177c0ea7 2403
f392e843
JD
2404 /* Page, step increment values are not so important here, they
2405 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2406 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2407 0.1, 0.1, 0.1);
2408
2409 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
2410 gtk_widget_set_name (wscroll, scroll_bar_name);
2411 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
177c0ea7 2412
f392e843 2413 scroll_id = xg_store_widget_in_map (wscroll);
177c0ea7 2414
f392e843
JD
2415 g_signal_connect (G_OBJECT (vadj),
2416 "value-changed",
2417 scroll_callback,
2418 (gpointer)bar);
2419 g_signal_connect (G_OBJECT (wscroll),
2420 "destroy",
2421 G_CALLBACK (xg_gtk_scroll_destroy),
2422 (gpointer)scroll_id);
2423
2424 /* Connect to button press and button release to detect if any scroll bar
2425 has the pointer. */
2426 g_signal_connect (G_OBJECT (wscroll),
2427 "button-press-event",
2428 G_CALLBACK (scroll_bar_button_cb),
17097258 2429 (gpointer)bar);
f392e843
JD
2430 g_signal_connect (G_OBJECT (wscroll),
2431 "button-release-event",
2432 G_CALLBACK (scroll_bar_button_cb),
17097258 2433 (gpointer)bar);
177c0ea7 2434
f392e843 2435 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget),
17097258 2436 wscroll, -1, -1);
f392e843
JD
2437
2438 /* Set the cursor to an arrow. */
2439 xg_set_cursor (wscroll, &xg_left_ptr_cursor);
2440
2441 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2442}
2443
2444/* Make the scroll bar represented by SCROLLBAR_ID visible. */
2445void
2446xg_show_scroll_bar (scrollbar_id)
2447 int scrollbar_id;
2448{
2449 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2450 if (w)
2451 gtk_widget_show (w);
2452}
2453
2454/* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2455void
2456xg_remove_scroll_bar (f, scrollbar_id)
2457 FRAME_PTR f;
2458 int scrollbar_id;
2459{
2460 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2461 if (w)
2462 {
2463 gtk_widget_destroy (w);
2464 SET_FRAME_GARBAGED (f);
2465 }
2466}
2467
17097258
JD
2468/* Find left/top for widget W in GtkFixed widget WFIXED. */
2469static void
2470xg_find_top_left_in_fixed (w, wfixed, left, top)
2471 GtkWidget *w, *wfixed;
2472 int *left, *top;
2473{
2474 GList *iter;
2475
2476 for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter))
2477 {
2478 GtkFixedChild *child = (GtkFixedChild *) iter->data;
2479
2480 if (child->widget == w)
2481 {
2482 *left = child->x;
2483 *top = child->y;
2484 return;
2485 }
2486 }
2487
2488 /* Shall never end up here. */
2489 abort ();
2490}
f392e843
JD
2491
2492/* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2493 in frame F.
2494 TOP/LEFT are the new pixel positions where the bar shall appear.
2495 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2496void
177c0ea7 2497xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
f392e843
JD
2498 FRAME_PTR f;
2499 int scrollbar_id;
2500 int top;
2501 int left;
2502 int width;
2503 int height;
2504{
f392e843 2505
49853a4d 2506 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
17097258 2507
49853a4d
JD
2508 if (wscroll)
2509 {
cea9be54 2510 GtkWidget *wfixed = f->output_data.x->edit_widget;
17097258
JD
2511 int gheight = max (height, 1);
2512 int canon_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
2513 int winextra = canon_width > width ? (canon_width - width) / 2 : 0;
2514 int bottom = top + gheight;
f392e843 2515
17097258
JD
2516 gint slider_width;
2517 int oldtop, oldleft, oldbottom;
2518 GtkRequisition req;
2519
2520 /* Get old values. */
2521 xg_find_top_left_in_fixed (wscroll, wfixed, &oldleft, &oldtop);
2522 gtk_widget_size_request (wscroll, &req);
2523 oldbottom = oldtop + req.height;
2524
2525 /* Scroll bars in GTK has a fixed width, so if we say width 16, it
2526 will only be its fixed width (14 is default) anyway, the rest is
2527 blank. We are drawing the mode line across scroll bars when
2528 the frame is split:
2529 |bar| |fringe|
2530 ----------------
2531 mode line
2532 ----------------
2533 |bar| |fringe|
2534
2535 When we "unsplit" the frame:
2536
2537 |bar| |fringe|
2538 -| |-| |
2539