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