*** 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 25#include <string.h>
aa477689 26#include <signal.h>
e8794476 27#include <stdio.h>
f392e843
JD
28#include "lisp.h"
29#include "xterm.h"
30#include "blockinput.h"
aa477689 31#include "syssignal.h"
f392e843
JD
32#include "window.h"
33#include "atimer.h"
34#include "gtkutil.h"
35#include "termhooks.h"
5b07197a
DL
36#include "keyboard.h"
37#include "charset.h"
38#include "coding.h"
f392e843
JD
39#include <gdk/gdkkeysyms.h>
40
810f2256 41
f392e843 42#define FRAME_TOTAL_PIXEL_HEIGHT(f) \
be786000 43 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
cea9be54 44
810f2256
JD
45\f
46/***********************************************************************
47 Display handling functions
48 ***********************************************************************/
49
50#ifdef HAVE_GTK_MULTIDISPLAY
51
52/* Return the GdkDisplay that corresponds to the X display DPY. */
71bacd48 53
810f2256
JD
54static GdkDisplay *
55xg_get_gdk_display (dpy)
56 Display *dpy;
57{
58 return gdk_x11_lookup_xdisplay (dpy);
59}
60
61/* When the GTK widget W is to be created on a display for F that
62 is not the default display, set the display for W.
63 W can be a GtkMenu or a GtkWindow widget. */
71bacd48 64
810f2256
JD
65static void
66xg_set_screen (w, f)
67 GtkWidget *w;
68 FRAME_PTR f;
69{
70 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
71 {
72 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
73 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
74
75 if (GTK_IS_MENU (w))
76 gtk_menu_set_screen (GTK_MENU (w), gscreen);
77 else
78 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
79 }
80}
81
82
83#else /* not HAVE_GTK_MULTIDISPLAY */
84
85/* Make some defines so we can use the GTK 2.2 functions when
86 compiling with GTK 2.0. */
71bacd48 87
810f2256
JD
88#define xg_set_screen(w, f)
89#define gdk_xid_table_lookup_for_display(dpy, w) gdk_xid_table_lookup (w)
90#define gdk_pixmap_foreign_new_for_display(dpy, p) gdk_pixmap_foreign_new (p)
91#define gdk_cursor_new_for_display(dpy, c) gdk_cursor_new (c)
92#define gdk_x11_lookup_xdisplay(dpy) 0
93#define GdkDisplay void
94
95#endif /* not HAVE_GTK_MULTIDISPLAY */
96
97/* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
98 *DPY is set to NULL if the display can't be opened.
99
100 Returns non-zero if display could be opened, zero if display could not
101 be opened, and less than zero if the GTK version doesn't support
102 multipe displays. */
71bacd48 103
810f2256
JD
104int
105xg_display_open (display_name, dpy)
106 char *display_name;
107 Display **dpy;
108{
109#ifdef HAVE_GTK_MULTIDISPLAY
110 GdkDisplay *gdpy;
111
112 gdpy = gdk_display_open (display_name);
113 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
114
115 return gdpy != NULL;
116
117#else /* not HAVE_GTK_MULTIDISPLAY */
118
119 return -1;
120#endif /* not HAVE_GTK_MULTIDISPLAY */
121}
122
123
71bacd48
JD
124/* Close display DPY. */
125
810f2256
JD
126void
127xg_display_close (Display *dpy)
128{
129#ifdef HAVE_GTK_MULTIDISPLAY
130 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
131
132 /* GTK 2.2 has a bug that makes gdk_display_close crash (bug
133 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way
134 we can continue running, but there will be memory leaks. */
135
136#if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 4
137
138 /* If this is the default display, we must change it before calling
139 dispose, otherwise it will crash. */
140 if (gdk_display_get_default () == gdpy)
141 {
142 struct x_display_info *dpyinfo;
143 Display *new_dpy = 0;
144 GdkDisplay *gdpy_new;
145
146 /* Find another display. */
147 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
148 if (dpyinfo->display != dpy)
149 {
150 new_dpy = dpyinfo->display;
151 break;
152 }
153
154 if (! new_dpy) return; /* Emacs will exit anyway. */
155
156 gdpy_new = gdk_x11_lookup_xdisplay (new_dpy);
157 gdk_display_manager_set_default_display (gdk_display_manager_get (),
158 gdpy_new);
159 }
160
161 g_object_run_dispose (G_OBJECT (gdpy));
162
163#else
164 /* I hope this will be fixed in GTK 2.4. It is what bug 85715 says. */
165 gdk_display_close (gdpy);
166#endif
167#endif /* HAVE_GTK_MULTIDISPLAY */
168}
cea9be54 169
f392e843
JD
170\f
171/***********************************************************************
172 Utility functions
173 ***********************************************************************/
174/* The timer for scroll bar repetition and menu bar timeouts.
175 NULL if no timer is started. */
176static struct atimer *xg_timer;
177
f392e843
JD
178
179/* The next two variables and functions are taken from lwlib. */
180static widget_value *widget_value_free_list;
181static int malloc_cpt;
182
183/* Allocate a widget_value structure, either by taking one from the
184 widget_value_free_list or by malloc:ing a new one.
185
186 Return a pointer to the allocated structure. */
71bacd48 187
f392e843
JD
188widget_value *
189malloc_widget_value ()
190{
191 widget_value *wv;
192 if (widget_value_free_list)
193 {
194 wv = widget_value_free_list;
195 widget_value_free_list = wv->free_list;
196 wv->free_list = 0;
197 }
198 else
199 {
200 wv = (widget_value *) malloc (sizeof (widget_value));
201 malloc_cpt++;
202 }
203 memset (wv, 0, sizeof (widget_value));
204 return wv;
205}
206
207/* This is analogous to free. It frees only what was allocated
208 by malloc_widget_value, and no substructures. */
71bacd48 209
f392e843
JD
210void
211free_widget_value (wv)
212 widget_value *wv;
213{
214 if (wv->free_list)
215 abort ();
216
217 if (malloc_cpt > 25)
218 {
219 /* When the number of already allocated cells is too big,
220 We free it. */
221 free (wv);
222 malloc_cpt--;
223 }
224 else
225 {
226 wv->free_list = widget_value_free_list;
227 widget_value_free_list = wv;
228 }
229}
230
f392e843 231
810f2256
JD
232/* Create and return the cursor to be used for popup menus and
233 scroll bars on display DPY. */
71bacd48 234
810f2256
JD
235GdkCursor *
236xg_create_default_cursor (dpy)
237 Display *dpy;
238{
239 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
240 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
241}
242
5b166323
JD
243/* For the image defined in IMG, make and return a GtkImage. For displays with
244 8 planes or less we must make a GdkPixbuf and apply the mask manually.
245 Otherwise the highlightning and dimming the tool bar code in GTK does
246 will look bad. For display with more than 8 planes we just use the
247 pixmap and mask directly. For monochrome displays, GTK doesn't seem
248 able to use external pixmaps, it looks bad whatever we do.
249 The image is defined on the display where frame F is.
250 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
251 If OLD_WIDGET is NULL, a new widget is constructed and returned.
252 If OLD_WIDGET is not NULL, that widget is modified. */
71bacd48 253
5b166323
JD
254static GtkWidget *
255xg_get_image_for_pixmap (f, img, widget, old_widget)
810f2256
JD
256 FRAME_PTR f;
257 struct image *img;
5b166323
JD
258 GtkWidget *widget;
259 GtkImage *old_widget;
810f2256 260{
5b166323
JD
261 GdkPixmap *gpix;
262 GdkPixmap *gmask;
03ecb80f
JD
263 GdkDisplay *gdpy;
264
265 /* If we are on a one bit display, let GTK do all the image handling.
266 This seems to be the only way to make insensitive and activated icons
267 look good. */
268 if (x_screen_planes (f) == 1)
269 {
270 Lisp_Object specified_file = Qnil;
271 Lisp_Object tail;
272 extern Lisp_Object QCfile;
273
274 for (tail = XCDR (img->spec);
275 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
276 tail = XCDR (XCDR (tail)))
277 if (EQ (XCAR (tail), QCfile))
278 specified_file = XCAR (XCDR (tail));
279
280 if (STRINGP (specified_file))
281 {
282
283 Lisp_Object file = Qnil;
284 struct gcpro gcpro1;
285 GCPRO1 (file);
286
287 file = x_find_image_file (specified_file);
288 /* We already loaded the image once before calling this
289 function, so this should not fail. */
290 xassert (STRINGP (file) != 0);
291
292 if (! old_widget)
293 old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
294 else
295 gtk_image_set_from_file (old_widget, SDATA (file));
296
297 UNGCPRO;
298 return GTK_WIDGET (old_widget);
299 }
300 }
810f2256 301
03ecb80f 302 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
5b166323
JD
303 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
304 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
305
306 if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
307 {
308 if (! old_widget)
309 old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
310 else
311 gtk_image_set_from_pixmap (old_widget, gpix, gmask);
312 }
313 else
314 {
03ecb80f
JD
315 /* This is a workaround to make icons look good on pseudo color
316 displays. Apparently GTK expects the images to have an alpha
317 channel. If they don't, insensitive and activated icons will
318 look bad. This workaround does not work on monochrome displays,
319 and is not needed on true color/static color displays (i.e.
320 16 bits and higher). */
5b166323
JD
321 int x, y, width, height, rowstride, mask_rowstride;
322 GdkPixbuf *icon_buf, *tmp_buf;
323 guchar *pixels;
324 guchar *mask_pixels;
325
326 gdk_drawable_get_size (gpix, &width, &height);
327 tmp_buf = gdk_pixbuf_get_from_drawable (NULL,
328 gpix,
329 gtk_widget_get_colormap (widget),
330 0, 0, 0, 0, width, height);
331 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
332 g_object_unref (G_OBJECT (tmp_buf));
333
334 if (gmask)
335 {
336 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
337 gmask,
338 NULL,
339 0, 0, 0, 0,
340 width, height);
341 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
342 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
343 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
344 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
345 int y;
346
347 for (y = 0; y < height; ++y)
348 {
349 guchar *iconptr, *maskptr;
350 int x;
351
352 iconptr = pixels + y * rowstride;
353 maskptr = mask_pixels + y * mask_rowstride;
354
355 for (x = 0; x < width; ++x)
356 {
357 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
358 just R is sufficient. */
359 if (maskptr[0] == 0)
360 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
361
362 iconptr += rowstride/width;
363 maskptr += mask_rowstride/width;
364 }
365 }
366
5b166323
JD
367 g_object_unref (G_OBJECT (mask_buf));
368 }
369
5b166323
JD
370 if (! old_widget)
371 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
372 else
373 gtk_image_set_from_pixbuf (old_widget, icon_buf);
374
375 g_object_unref (G_OBJECT (icon_buf));
376 }
377
74cdfe05
JD
378 g_object_unref (G_OBJECT (gpix));
379 if (gmask) g_object_unref (G_OBJECT (gmask));
380
5b166323 381 return GTK_WIDGET (old_widget);
810f2256
JD
382}
383
384
385/* Set CURSOR on W and all widgets W contain. We must do like this
386 for scroll bars and menu because they create widgets internally,
387 and it is those widgets that are visible. */
71bacd48 388
810f2256 389static void
f392e843
JD
390xg_set_cursor (w, cursor)
391 GtkWidget *w;
810f2256 392 GdkCursor *cursor;
f392e843
JD
393{
394 GList *children = gdk_window_peek_children (w->window);
395
810f2256 396 gdk_window_set_cursor (w->window, cursor);
f392e843
JD
397
398 /* The scroll bar widget has more than one GDK window (had to look at
399 the source to figure this out), and there is no way to set cursor
400 on widgets in GTK. So we must set the cursor for all GDK windows.
401 Ditto for menus. */
402
403 for ( ; children; children = g_list_next (children))
810f2256 404 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
f392e843
JD
405}
406
407/* Timer function called when a timeout occurs for xg_timer.
408 This function processes all GTK events in a recursive event loop.
409 This is done because GTK timer events are not seen by Emacs event
410 detection, Emacs only looks for X events. When a scroll bar has the
411 pointer (detected by button press/release events below) an Emacs
412 timer is started, and this function can then check if the GTK timer
413 has expired by calling the GTK event loop.
414 Also, when a menu is active, it has a small timeout before it
415 pops down the sub menu under it. */
71bacd48 416
f392e843
JD
417static void
418xg_process_timeouts (timer)
419 struct atimer *timer;
420{
421 BLOCK_INPUT;
422 /* Ideally we would like to just handle timer events, like the Xt version
423 of this does in xterm.c, but there is no such feature in GTK. */
424 while (gtk_events_pending ())
425 gtk_main_iteration ();
426 UNBLOCK_INPUT;
427}
428
429/* Start the xg_timer with an interval of 0.1 seconds, if not already started.
430 xg_process_timeouts is called when the timer expires. The timer
7863d625 431 started is continuous, i.e. runs until xg_stop_timer is called. */
71bacd48 432
f392e843
JD
433static void
434xg_start_timer ()
435{
436 if (! xg_timer)
437 {
438 EMACS_TIME interval;
439 EMACS_SET_SECS_USECS (interval, 0, 100000);
440 xg_timer = start_atimer (ATIMER_CONTINUOUS,
441 interval,
442 xg_process_timeouts,
443 0);
444 }
445}
446
447/* Stop the xg_timer if started. */
71bacd48 448
f392e843
JD
449static void
450xg_stop_timer ()
451{
452 if (xg_timer)
453 {
454 cancel_atimer (xg_timer);
455 xg_timer = 0;
456 }
457}
458
459/* Insert NODE into linked LIST. */
71bacd48 460
f392e843
JD
461static void
462xg_list_insert (xg_list_node *list, xg_list_node *node)
463{
464 xg_list_node *list_start = list->next;
177c0ea7 465
f392e843
JD
466 if (list_start) list_start->prev = node;
467 node->next = list_start;
468 node->prev = 0;
469 list->next = node;
470}
471
472/* Remove NODE from linked LIST. */
71bacd48 473
f392e843
JD
474static void
475xg_list_remove (xg_list_node *list, xg_list_node *node)
476{
477 xg_list_node *list_start = list->next;
478 if (node == list_start)
479 {
480 list->next = node->next;
481 if (list->next) list->next->prev = 0;
482 }
483 else
484 {
485 node->prev->next = node->next;
486 if (node->next) node->next->prev = node->prev;
487 }
488}
489
490/* Allocate and return a utf8 version of STR. If STR is already
491 utf8 or NULL, just return STR.
492 If not, a new string is allocated and the caller must free the result
493 with g_free. */
71bacd48 494
f392e843
JD
495static char *
496get_utf8_string (str)
497 char *str;
498{
499 char *utf8_str = str;
177c0ea7 500
f392e843
JD
501 /* If not UTF-8, try current locale. */
502 if (str && !g_utf8_validate (str, -1, NULL))
503 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
504
505 return utf8_str;
506}
507
508
509\f
510/***********************************************************************
511 General functions for creating widgets, resizing, events, e.t.c.
512 ***********************************************************************/
513
514/* Make a geometry string and pass that to GTK. It seems this is the
515 only way to get geometry position right if the user explicitly
516 asked for a position when starting Emacs.
517 F is the frame we shall set geometry for. */
71bacd48 518
f392e843
JD
519static void
520xg_set_geometry (f)
521 FRAME_PTR f;
522{
be786000 523 if (f->size_hint_flags & USPosition)
f392e843 524 {
be786000
KS
525 int left = f->left_pos;
526 int xneg = f->size_hint_flags & XNegative;
527 int top = f->top_pos;
528 int yneg = f->size_hint_flags & YNegative;
f392e843 529 char geom_str[32];
177c0ea7 530
f392e843
JD
531 if (xneg)
532 left = -left;
533 if (yneg)
534 top = -top;
535
536 sprintf (geom_str, "=%dx%d%c%d%c%d",
be786000 537 FRAME_PIXEL_WIDTH (f),
f392e843
JD
538 FRAME_TOTAL_PIXEL_HEIGHT (f),
539 (xneg ? '-' : '+'), left,
540 (yneg ? '-' : '+'), top);
541
542 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
543 geom_str))
544 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
545 }
546}
547
177c0ea7 548
f392e843
JD
549/* Resize the outer window of frame F after chainging the height.
550 This happend when the menu bar or the tool bar is added or removed.
551 COLUMNS/ROWS is the size the edit area shall have after the resize. */
71bacd48 552
f392e843
JD
553static void
554xg_resize_outer_widget (f, columns, rows)
555 FRAME_PTR f;
556 int columns;
557 int rows;
558{
559 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
be786000 560 FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
f392e843
JD
561
562 /* base_height is now changed. */
563 x_wm_set_size_hint (f, 0, 0);
564
565 /* If we are not mapped yet, set geometry once again, as window
566 height now have changed. */
567 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
568 xg_set_geometry (f);
569
570 xg_frame_set_char_size (f, columns, rows);
571 gdk_window_process_all_updates ();
572}
573
574/* Function to handle resize of our widgets. Since Emacs has some layouts
575 that does not fit well with GTK standard containers, we do most layout
576 manually.
577 F is the frame to resize.
578 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
71bacd48 579
f392e843
JD
580void
581xg_resize_widgets (f, pixelwidth, pixelheight)
582 FRAME_PTR f;
583 int pixelwidth, pixelheight;
584{
585 int mbheight = FRAME_MENUBAR_HEIGHT (f);
586 int tbheight = FRAME_TOOLBAR_HEIGHT (f);
be786000
KS
587 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight
588 - mbheight - tbheight));
589 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
177c0ea7 590
f392e843 591 if (FRAME_GTK_WIDGET (f)
1755a397
JD
592 && (columns != FRAME_COLS (f)
593 || rows != FRAME_LINES (f)
594 || pixelwidth != FRAME_PIXEL_WIDTH (f)
595 || pixelheight != FRAME_PIXEL_HEIGHT (f)))
f392e843
JD
596 {
597 struct x_output *x = f->output_data.x;
598 GtkAllocation all;
599
600 all.y = mbheight + tbheight;
601 all.x = 0;
602
603 all.width = pixelwidth;
604 all.height = pixelheight - mbheight - tbheight;
605
606 gtk_widget_size_allocate (x->edit_widget, &all);
cea9be54 607
f392e843
JD
608 change_frame_size (f, rows, columns, 0, 1, 0);
609 SET_FRAME_GARBAGED (f);
610 cancel_mouse_face (f);
611 }
612}
613
614
615/* Update our widget size to be COLS/ROWS characters for frame F. */
71bacd48 616
f392e843
JD
617void
618xg_frame_set_char_size (f, cols, rows)
619 FRAME_PTR f;
620 int cols;
621 int rows;
622{
be786000 623 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
f392e843 624 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
5fd6f727 625 int pixelwidth;
177c0ea7 626
f392e843
JD
627 /* Take into account the size of the scroll bar. Always use the
628 number of columns occupied by the scroll bar here otherwise we
629 might end up with a frame width that is not a multiple of the
630 frame's character width which is bad for vertically split
631 windows. */
be786000
KS
632 f->scroll_bar_actual_width
633 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
f392e843 634
055d3c98 635 compute_fringe_widths (f, 0);
f392e843 636
be786000 637 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
5fd6f727 638 after calculating that value. */
be786000 639 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5fd6f727 640
f392e843
JD
641 /* Must resize our top level widget. Font size may have changed,
642 but not rows/cols. */
643 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
644 pixelwidth, pixelheight);
645 xg_resize_widgets (f, pixelwidth, pixelheight);
f26fab36 646 x_wm_set_size_hint (f, 0, 0);
5fd6f727
JD
647 SET_FRAME_GARBAGED (f);
648 cancel_mouse_face (f);
f392e843
JD
649}
650
810f2256 651/* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
f392e843
JD
652 Must be done like this, because GtkWidget:s can have "hidden"
653 X Window that aren't accessible.
654
655 Return 0 if no widget match WDESC. */
71bacd48 656
f392e843 657GtkWidget *
810f2256
JD
658xg_win_to_widget (dpy, wdesc)
659 Display *dpy;
f392e843
JD
660 Window wdesc;
661{
662 gpointer gdkwin;
663 GtkWidget *gwdesc = 0;
664
665 BLOCK_INPUT;
810f2256
JD
666
667 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
668 wdesc);
f392e843
JD
669 if (gdkwin)
670 {
671 GdkEvent event;
672 event.any.window = gdkwin;
673 gwdesc = gtk_get_event_widget (&event);
674 }
177c0ea7 675
f392e843
JD
676 UNBLOCK_INPUT;
677 return gwdesc;
678}
679
680/* Fill in the GdkColor C so that it represents PIXEL.
681 W is the widget that color will be used for. Used to find colormap. */
71bacd48 682
f392e843
JD
683static void
684xg_pix_to_gcolor (w, pixel, c)
685 GtkWidget *w;
686 unsigned long pixel;
687 GdkColor *c;
688{
689 GdkColormap *map = gtk_widget_get_colormap (w);
690 gdk_colormap_query_color (map, pixel, c);
691}
692
693/* Create and set up the GTK widgets for frame F.
694 Return 0 if creation failed, non-zero otherwise. */
71bacd48 695
f392e843
JD
696int
697xg_create_frame_widgets (f)
698 FRAME_PTR f;
699{
700 GtkWidget *wtop;
701 GtkWidget *wvbox;
702 GtkWidget *wfixed;
703 GdkColor bg;
704 GtkRcStyle *style;
705 int i;
706 char *title = 0;
177c0ea7 707
f392e843
JD
708 BLOCK_INPUT;
709
710 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
810f2256
JD
711 xg_set_screen (wtop, f);
712
f392e843
JD
713 wvbox = gtk_vbox_new (FALSE, 0);
714 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
177c0ea7 715
f392e843
JD
716 if (! wtop || ! wvbox || ! wfixed)
717 {
718 if (wtop) gtk_widget_destroy (wtop);
719 if (wvbox) gtk_widget_destroy (wvbox);
720 if (wfixed) gtk_widget_destroy (wfixed);
721
722 return 0;
723 }
724
725 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
726 gtk_widget_set_name (wtop, EMACS_CLASS);
727 gtk_widget_set_name (wvbox, "pane");
728 gtk_widget_set_name (wfixed, SDATA (Vx_resource_name));
729
730 /* If this frame has a title or name, set it in the title bar. */
5b07197a
DL
731 if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title));
732 else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name));
f392e843
JD
733
734 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
177c0ea7 735
f392e843
JD
736 FRAME_GTK_OUTER_WIDGET (f) = wtop;
737 FRAME_GTK_WIDGET (f) = wfixed;
738 f->output_data.x->vbox_widget = wvbox;
739
740 gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);
741
810f2256
JD
742 gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f),
743 FRAME_PIXEL_HEIGHT (f));
177c0ea7 744
f392e843
JD
745 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
746 gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);
747
748 if (FRAME_EXTERNAL_TOOL_BAR (f))
749 update_frame_tool_bar (f);
750
751 /* The tool bar is created but first there are no items in it.
752 This causes it to be zero height. Later items are added, but then
753 the frame is already mapped, so there is a "jumping" resize.
754 This makes geometry handling difficult, for example -0-0 will end
755 up in the wrong place as tool bar height has not been taken into account.
756 So we cheat a bit by setting a height that is what it will have
757 later on when tool bar items are added. */
5500be54 758 if (FRAME_EXTERNAL_TOOL_BAR (f) && f->n_tool_bar_items == 0)
e0910857 759 FRAME_TOOLBAR_HEIGHT (f) = 38;
177c0ea7 760
7863d625
JD
761
762 /* We don't want this widget double buffered, because we draw on it
763 with regular X drawing primitives, so from a GTK/GDK point of
764 view, the widget is totally blank. When an expose comes, this
765 will make the widget blank, and then Emacs redraws it. This flickers
766 a lot, so we turn off double buffering. */
f392e843 767 gtk_widget_set_double_buffered (wfixed, FALSE);
7863d625 768
f392e843
JD
769 /* GTK documents says use gtk_window_set_resizable. But then a user
770 can't shrink the window from its starting size. */
771 gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE);
772 gtk_window_set_wmclass (GTK_WINDOW (wtop),
773 SDATA (Vx_resource_name),
774 SDATA (Vx_resource_class));
775
776 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
777 GTK is to destroy the widget. We want Emacs to do that instead. */
778 g_signal_connect (G_OBJECT (wtop), "delete-event",
779 G_CALLBACK (gtk_true), 0);
177c0ea7 780
f392e843
JD
781 /* Convert our geometry parameters into a geometry string
782 and specify it.
783 GTK will itself handle calculating the real position this way. */
784 xg_set_geometry (f);
785
786 gtk_widget_add_events (wfixed,
787 GDK_POINTER_MOTION_MASK
788 | GDK_EXPOSURE_MASK
789 | GDK_BUTTON_PRESS_MASK
790 | GDK_BUTTON_RELEASE_MASK
791 | GDK_KEY_PRESS_MASK
792 | GDK_ENTER_NOTIFY_MASK
793 | GDK_LEAVE_NOTIFY_MASK
794 | GDK_FOCUS_CHANGE_MASK
795 | GDK_STRUCTURE_MASK
796 | GDK_VISIBILITY_NOTIFY_MASK);
797
798 /* Must realize the windows so the X window gets created. It is used
799 by callers of this function. */
800 gtk_widget_realize (wfixed);
801 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
802
803 /* Since GTK clears its window by filling with the background color,
804 we must keep X and GTK background in sync. */
805 xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg);
806 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
807
808 /* Also, do not let any background pixmap to be set, this looks very
809 bad as Emacs overwrites the background pixmap with its own idea
810 of background color. */
811 style = gtk_widget_get_modifier_style (wfixed);
812
813 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
814 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
815 gtk_widget_modify_style (wfixed, style);
177c0ea7 816
f392e843 817 /* GTK does not set any border, and they look bad with GTK. */
be786000
KS
818 f->border_width = 0;
819 f->internal_border_width = 0;
f392e843
JD
820
821 UNBLOCK_INPUT;
822
823 return 1;
824}
825
826/* Set the normal size hints for the window manager, for frame F.
827 FLAGS is the flags word to use--or 0 meaning preserve the flags
828 that the window now has.
829 If USER_POSITION is nonzero, we set the User Position
830 flag (this is useful when FLAGS is 0). */
71bacd48 831
f392e843
JD
832void
833x_wm_set_size_hint (f, flags, user_position)
834 FRAME_PTR f;
835 long flags;
836 int user_position;
837{
838 if (FRAME_GTK_OUTER_WIDGET (f))
839 {
840 /* Must use GTK routines here, otherwise GTK resets the size hints
841 to its own defaults. */
842 GdkGeometry size_hints;
843 gint hint_flags = 0;
844 int base_width, base_height;
845 int min_rows = 0, min_cols = 0;
be786000 846 int win_gravity = f->win_gravity;
177c0ea7 847
f392e843
JD
848 if (flags)
849 {
850 memset (&size_hints, 0, sizeof (size_hints));
851 f->output_data.x->size_hints = size_hints;
852 f->output_data.x->hint_flags = hint_flags;
853 }
854 else
be786000 855 flags = f->size_hint_flags;
177c0ea7 856
f392e843
JD
857 size_hints = f->output_data.x->size_hints;
858 hint_flags = f->output_data.x->hint_flags;
859
860 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
be786000
KS
861 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
862 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
f392e843
JD
863
864 hint_flags |= GDK_HINT_BASE_SIZE;
be786000
KS
865 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
866 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
f392e843
JD
867 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
868
869 check_frame_size (f, &min_rows, &min_cols);
870
871 size_hints.base_width = base_width;
872 size_hints.base_height = base_height;
873 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
874 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
875
177c0ea7 876
f392e843
JD
877 /* These currently have a one to one mapping with the X values, but I
878 don't think we should rely on that. */
879 hint_flags |= GDK_HINT_WIN_GRAVITY;
880 size_hints.win_gravity = 0;
881 if (win_gravity == NorthWestGravity)
882 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
883 else if (win_gravity == NorthGravity)
884 size_hints.win_gravity = GDK_GRAVITY_NORTH;
885 else if (win_gravity == NorthEastGravity)
886 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
887 else if (win_gravity == WestGravity)
888 size_hints.win_gravity = GDK_GRAVITY_WEST;
889 else if (win_gravity == CenterGravity)
890 size_hints.win_gravity = GDK_GRAVITY_CENTER;
891 else if (win_gravity == EastGravity)
892 size_hints.win_gravity = GDK_GRAVITY_EAST;
893 else if (win_gravity == SouthWestGravity)
894 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
895 else if (win_gravity == SouthGravity)
896 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
897 else if (win_gravity == SouthEastGravity)
898 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
899 else if (win_gravity == StaticGravity)
900 size_hints.win_gravity = GDK_GRAVITY_STATIC;
901
902 if (flags & PPosition) hint_flags |= GDK_HINT_POS;
903 if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
904 if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;
905
906 if (user_position)
907 {
908 hint_flags &= ~GDK_HINT_POS;
909 hint_flags |= GDK_HINT_USER_POS;
910 }
911
912 BLOCK_INPUT;
913
914 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
915 FRAME_GTK_OUTER_WIDGET (f),
916 &size_hints,
917 hint_flags);
918
919 f->output_data.x->size_hints = size_hints;
920 f->output_data.x->hint_flags = hint_flags;
921 UNBLOCK_INPUT;
922 }
923}
924
925/* Change background color of a frame.
926 Since GTK uses the background colour to clear the window, we must
927 keep the GTK and X colors in sync.
928 F is the frame to change,
929 BG is the pixel value to change to. */
71bacd48 930
f392e843
JD
931void
932xg_set_background_color (f, bg)
933 FRAME_PTR f;
934 unsigned long bg;
935{
936 if (FRAME_GTK_WIDGET (f))
937 {
938 GdkColor gdk_bg;
939
940 BLOCK_INPUT;
941 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
942 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
943 UNBLOCK_INPUT;
944 }
945}
946
947
948\f
949/***********************************************************************
950 Dialog functions
951 ***********************************************************************/
952/* Return the dialog title to use for a dialog of type KEY.
953 This is the encoding used by lwlib. We use the same for GTK. */
71bacd48 954
f392e843
JD
955static char *
956get_dialog_title (char key)
957{
958 char *title = "";
177c0ea7 959
f392e843
JD
960 switch (key) {
961 case 'E': case 'e':
962 title = "Error";
963 break;
964
965 case 'I': case 'i':
966 title = "Information";
967 break;
968
969 case 'L': case 'l':
970 title = "Prompt";
971 break;
972
973 case 'P': case 'p':
974 title = "Prompt";
975 break;
976
977 case 'Q': case 'q':
978 title = "Question";
979 break;
980 }
981
982 return title;
983}
984
985/* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
986 the dialog, but return TRUE so the event does not propagate further
987 in GTK. This prevents GTK from destroying the dialog widget automatically
988 and we can always destrou the widget manually, regardles of how
989 it was popped down (button press or WM_DELETE_WINDOW).
990 W is the dialog widget.
991 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
992 user_data is NULL (not used).
993
994 Returns TRUE to end propagation of event. */
71bacd48 995
f392e843
JD
996static gboolean
997dialog_delete_callback (w, event, user_data)
998 GtkWidget *w;
999 GdkEvent *event;
1000 gpointer user_data;
1001{
1002 gtk_widget_unmap (w);
1003 return TRUE;
1004}
1005
1006/* Create a popup dialog window. See also xg_create_widget below.
1007 WV is a widget_value describing the dialog.
1008 SELECT_CB is the callback to use when a button has been pressed.
1009 DEACTIVATE_CB is the callback to use when the dialog pops down.
1010
1011 Returns the GTK dialog widget. */
71bacd48 1012
f392e843
JD
1013static GtkWidget *
1014create_dialog (wv, select_cb, deactivate_cb)
1015 widget_value *wv;
1016 GCallback select_cb;
1017 GCallback deactivate_cb;
1018{
1019 char *title = get_dialog_title (wv->name[0]);
1020 int total_buttons = wv->name[1] - '0';
1021 int right_buttons = wv->name[4] - '0';
1022 int left_buttons;
1023 int button_nr = 0;
1024 int button_spacing = 10;
1025 GtkWidget *wdialog = gtk_dialog_new ();
1026 widget_value *item;
1027 GtkBox *cur_box;
1028 GtkWidget *wvbox;
1029 GtkWidget *whbox_up;
1030 GtkWidget *whbox_down;
1031
1032 /* If the number of buttons is greater than 4, make two rows of buttons
1033 instead. This looks better. */
1034 int make_two_rows = total_buttons > 4;
1035
1036 if (right_buttons == 0) right_buttons = total_buttons/2;
1037 left_buttons = total_buttons - right_buttons;
1038
1039 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1040 gtk_widget_set_name (wdialog, "emacs-dialog");
1041
1042 cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);
1043
1044 if (make_two_rows)
1045 {
1046 wvbox = gtk_vbox_new (TRUE, button_spacing);
1047 whbox_up = gtk_hbox_new (FALSE, 0);
1048 whbox_down = gtk_hbox_new (FALSE, 0);
1049
1050 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1051 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1052 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1053
1054 cur_box = GTK_BOX (whbox_up);
1055 }
1056
1057 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1058 G_CALLBACK (dialog_delete_callback), 0);
177c0ea7 1059
f392e843
JD
1060 if (deactivate_cb)
1061 {
1062 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1063 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1064 }
1065
1066 for (item = wv->contents; item; item = item->next)
1067 {
1068 char *utf8_label = get_utf8_string (item->value);
1069 GtkWidget *w;
1070 GtkRequisition req;
1071
0a1d6de0 1072 if (item->name && strcmp (item->name, "message") == 0)
f392e843
JD
1073 {
1074 /* This is the text part of the dialog. */
1075 w = gtk_label_new (utf8_label);
1076 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1077 gtk_label_new (""),
1078 FALSE, FALSE, 0);
1079 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
1080 TRUE, TRUE, 0);
1081 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1082
1083 /* Try to make dialog look better. Must realize first so
1084 the widget can calculate the size it needs. */
1085 gtk_widget_realize (w);
1086 gtk_widget_size_request (w, &req);
1087 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
1088 req.height);
0a1d6de0 1089 if (item->value && strlen (item->value) > 0)
f392e843
JD
1090 button_spacing = 2*req.width/strlen (item->value);
1091 }
1092 else
1093 {
1094 /* This is one button to add to the dialog. */
4b1b4443 1095 w = gtk_button_new_with_label (utf8_label);
f392e843
JD
1096 if (! item->enabled)
1097 gtk_widget_set_sensitive (w, FALSE);
1098 if (select_cb)
1099 g_signal_connect (G_OBJECT (w), "clicked",
1100 select_cb, item->call_data);
1101
1102 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1103 if (++button_nr == left_buttons)
1104 {
1105 if (make_two_rows)
1106 cur_box = GTK_BOX (whbox_down);
1107 else
1108 gtk_box_pack_start (cur_box,
1109 gtk_label_new (""),
1110 TRUE, TRUE,
1111 button_spacing);
1112 }
1113 }
1114
1115 if (utf8_label && utf8_label != item->value)
1116 g_free (utf8_label);
1117 }
1118
1119 return wdialog;
1120}
1121
1122
f9d64bb3
JD
1123\f
1124/***********************************************************************
1125 File dialog functions
1126 ***********************************************************************/
457a8155
JD
1127/* Function that is called when the file dialog pops down.
1128 W is the dialog widget, RESPONSE is the response code.
1129 USER_DATA is what we passed in to g_signal_connect (pointer to int). */
1130
1131static void
1132xg_file_response_cb (w,
1133 response,
1134 user_data)
1135 GtkDialog *w;
1136 gint response;
1137 gpointer user_data;
1138{
1139 int *ptr = (int *) user_data;
1140 *ptr = response;
1141}
1142
1143
1144/* Destroy the dialog. This makes it pop down. */
1145
1146static Lisp_Object
1147pop_down_file_dialog (arg)
1148 Lisp_Object arg;
1149{
1150 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
cd2531db 1151 BLOCK_INPUT;
457a8155 1152 gtk_widget_destroy (GTK_WIDGET (p->pointer));
cd2531db 1153 UNBLOCK_INPUT;
457a8155
JD
1154 return Qnil;
1155}
1156
1157typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
f9d64bb3
JD
1158
1159#ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
457a8155
JD
1160
1161/* Return the selected file for file chooser dialog W.
1162 The returned string must be free:d. */
1163
1164static char *
1165xg_get_file_name_from_chooser (w)
1166 GtkWidget *w;
1167{
1168 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1169}
1170
f9d64bb3
JD
1171/* Read a file name from the user using a file chooser dialog.
1172 F is the current frame.
1173 PROMPT is a prompt to show to the user. May not be NULL.
1174 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1175 If MUSTMATCH_P is non-zero, the returned file name must be an existing
457a8155
JD
1176 file. *FUNC is set to a function that can be used to retrieve the
1177 selected file name from the returned widget.
f9d64bb3 1178
457a8155 1179 Returns the created widget. */
f9d64bb3 1180
457a8155
JD
1181static GtkWidget *
1182xg_get_file_with_chooser (f, prompt, default_filename,
1183 mustmatch_p, only_dir_p, func)
f9d64bb3
JD
1184 FRAME_PTR f;
1185 char *prompt;
1186 char *default_filename;
1187 int mustmatch_p, only_dir_p;
457a8155 1188 xg_get_file_func *func;
f9d64bb3
JD
1189{
1190 GtkWidget *filewin;
1191 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
f9d64bb3
JD
1192 GtkFileChooserAction action = (mustmatch_p ?
1193 GTK_FILE_CHOOSER_ACTION_OPEN :
1194 GTK_FILE_CHOOSER_ACTION_SAVE);
1195
1196 if (only_dir_p)
1197 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1198
1199 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1200 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1201 (mustmatch_p || only_dir_p ?
8cfd0f36 1202 GTK_STOCK_OPEN : GTK_STOCK_OK),
f9d64bb3
JD
1203 GTK_RESPONSE_OK,
1204 NULL);
ded997c1 1205 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
f9d64bb3 1206
f9d64bb3 1207 if (default_filename)
a872928c
JD
1208 {
1209 Lisp_Object file;
1210 struct gcpro gcpro1;
1211 GCPRO1 (file);
1212
ded997c1
JD
1213 file = build_string (default_filename);
1214
a872928c
JD
1215 /* File chooser does not understand ~/... in the file name. It must be
1216 an absolute name starting with /. */
1217 if (default_filename[0] != '/')
ded997c1 1218 file = Fexpand_file_name (file, Qnil);
a872928c 1219
ded997c1
JD
1220 default_filename = SDATA (file);
1221 if (Ffile_directory_p (file))
1222 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1223 default_filename);
1224 else
1225 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1226 default_filename);
a872928c
JD
1227
1228 UNGCPRO;
1229 }
f9d64bb3 1230
457a8155
JD
1231 *func = xg_get_file_name_from_chooser;
1232 return filewin;
f9d64bb3
JD
1233}
1234#endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */
1235
1236#ifdef HAVE_GTK_FILE_SELECTION_NEW
f392e843 1237
457a8155
JD
1238/* Return the selected file for file selector dialog W.
1239 The returned string must be free:d. */
71bacd48 1240
457a8155
JD
1241static char *
1242xg_get_file_name_from_selector (w)
f392e843 1243 GtkWidget *w;
f392e843 1244{
457a8155
JD
1245 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1246 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
f392e843
JD
1247}
1248
457a8155 1249/* Create a file selection dialog.
f392e843
JD
1250 F is the current frame.
1251 PROMPT is a prompt to show to the user. May not be NULL.
1252 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1253 If MUSTMATCH_P is non-zero, the returned file name must be an existing
457a8155
JD
1254 file. *FUNC is set to a function that can be used to retrieve the
1255 selected file name from the returned widget.
f392e843 1256
457a8155 1257 Returns the created widget. */
71bacd48 1258
457a8155 1259static GtkWidget *
f9d64bb3 1260xg_get_file_with_selection (f, prompt, default_filename,
457a8155 1261 mustmatch_p, only_dir_p, func)
f392e843
JD
1262 FRAME_PTR f;
1263 char *prompt;
1264 char *default_filename;
f9d64bb3 1265 int mustmatch_p, only_dir_p;
457a8155 1266 xg_get_file_func *func;
f392e843
JD
1267{
1268 GtkWidget *filewin;
1269 GtkFileSelection *filesel;
177c0ea7 1270
f392e843
JD
1271 filewin = gtk_file_selection_new (prompt);
1272 filesel = GTK_FILE_SELECTION (filewin);
1273
f392e843
JD
1274 if (default_filename)
1275 gtk_file_selection_set_filename (filesel, default_filename);
1276
1277 if (mustmatch_p)
1278 {
1279 /* The selection_entry part of filesel is not documented. */
1280 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1281 gtk_file_selection_hide_fileop_buttons (filesel);
1282 }
1283
457a8155 1284 *func = xg_get_file_name_from_selector;
177c0ea7 1285
457a8155 1286 return filewin;
f392e843 1287}
f9d64bb3
JD
1288#endif /* HAVE_GTK_FILE_SELECTION_NEW */
1289
1290/* Read a file name from the user using a file dialog, either the old
1291 file selection dialog, or the new file chooser dialog. Which to use
1292 depends on what the GTK version used has, and what the value of
1293 gtk-use-old-file-dialog.
1294 F is the current frame.
1295 PROMPT is a prompt to show to the user. May not be NULL.
1296 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1297 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1298 file.
1299
1300 Returns a file name or NULL if no file was selected.
1301 The returned string must be freed by the caller. */
1302
1303char *
1304xg_get_file_name (f, prompt, default_filename, mustmatch_p, only_dir_p)
1305 FRAME_PTR f;
1306 char *prompt;
1307 char *default_filename;
1308 int mustmatch_p, only_dir_p;
1309{
724cde0d 1310 GtkWidget *w = 0;
457a8155
JD
1311 int count = SPECPDL_INDEX ();
1312 char *fn = 0;
1313 int filesel_done = 0;
1314 xg_get_file_func func;
eee141ce 1315 extern int x_use_old_gtk_file_dialog;
457a8155 1316
aa477689
JD
1317#if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1318 /* I really don't know why this is needed, but without this the GLIBC add on
1319 library linuxthreads hangs when the Gnome file chooser backend creates
1320 threads. */
1321 sigblock (sigmask (__SIGRTMIN));
1322#endif /* HAVE_GTK_AND_PTHREAD */
1323
f9d64bb3 1324#ifdef HAVE_GTK_FILE_BOTH
255e4140
JD
1325
1326 if (x_use_old_gtk_file_dialog)
457a8155
JD
1327 w = xg_get_file_with_selection (f, prompt, default_filename,
1328 mustmatch_p, only_dir_p, &func);
1329 else
1330 w = xg_get_file_with_chooser (f, prompt, default_filename,
1331 mustmatch_p, only_dir_p, &func);
f9d64bb3
JD
1332
1333#else /* not HAVE_GTK_FILE_BOTH */
1334
724cde0d 1335#ifdef HAVE_GTK_FILE_SELECTION_NEW
457a8155
JD
1336 w = xg_get_file_with_selection (f, prompt, default_filename,
1337 mustmatch_p, only_dir_p, &func);
f9d64bb3
JD
1338#endif
1339#ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
457a8155
JD
1340 w = xg_get_file_with_chooser (f, prompt, default_filename,
1341 mustmatch_p, only_dir_p, &func);
f9d64bb3
JD
1342#endif
1343
1344#endif /* HAVE_GTK_FILE_BOTH */
457a8155
JD
1345
1346 xg_set_screen (w, f);
1347 gtk_widget_set_name (w, "emacs-filedialog");
1348 gtk_window_set_transient_for (GTK_WINDOW (w),
1349 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1350 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1351 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1352
1353 g_signal_connect (G_OBJECT (w),
1354 "response",
1355 G_CALLBACK (xg_file_response_cb),
1356 &filesel_done);
1357
1358 /* Don't destroy the widget if closed by the window manager close button. */
1359 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1360
1361 gtk_widget_show (w);
1362
1363 record_unwind_protect (pop_down_file_dialog, make_save_value (w, 0));
1364 while (! filesel_done)
1365 {
1366 x_menu_wait_for_event (0);
1367 gtk_main_iteration ();
1368 }
1369
aa477689
JD
1370#if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1371 sigunblock (sigmask (__SIGRTMIN));
1372#endif
1373
457a8155
JD
1374 if (filesel_done == GTK_RESPONSE_OK)
1375 fn = (*func) (w);
1376
1377 unbind_to (count, Qnil);
1378
1379 return fn;
f9d64bb3 1380}
f392e843
JD
1381
1382\f
1383/***********************************************************************
1384 Menu functions.
1385 ***********************************************************************/
1386
1387/* The name of menu items that can be used for citomization. Since GTK
1388 RC files are very crude and primitive, we have to set this on all
1389 menu item names so a user can easily cutomize menu items. */
1390
1391#define MENU_ITEM_NAME "emacs-menuitem"
1392
1393
1394/* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1395 during GC. The next member points to the items. */
1396static xg_list_node xg_menu_cb_list;
1397
1398/* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1399 during GC. The next member points to the items. */
1400static xg_list_node xg_menu_item_cb_list;
1401
1402/* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1403 F is the frame CL_DATA will be initialized for.
1404 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1405
1406 The menu bar and all sub menus under the menu bar in a frame
1407 share the same structure, hence the reference count.
177c0ea7 1408
f392e843
JD
1409 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1410 allocated xg_menu_cb_data if CL_DATA is NULL. */
71bacd48 1411
f392e843
JD
1412static xg_menu_cb_data *
1413make_cl_data (cl_data, f, highlight_cb)
1414 xg_menu_cb_data *cl_data;
1415 FRAME_PTR f;
1416 GCallback highlight_cb;
1417{
1418 if (! cl_data)
1419 {
1420 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1421 cl_data->f = f;
1422 cl_data->menu_bar_vector = f->menu_bar_vector;
1423 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1424 cl_data->highlight_cb = highlight_cb;
1425 cl_data->ref_count = 0;
1426
1427 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1428 }
1429
1430 cl_data->ref_count++;
1431
1432 return cl_data;
1433}
1434
1435/* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1436 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1437
1438 When the menu bar is updated, menu items may have been added and/or
1439 removed, so menu_bar_vector and menu_bar_items_used change. We must
1440 then update CL_DATA since it is used to determine which menu
1441 item that is invoked in the menu.
1442 HIGHLIGHT_CB could change, there is no check that the same
1443 function is given when modifying a menu bar as was given when
1444 creating the menu bar. */
71bacd48 1445
f392e843
JD
1446static void
1447update_cl_data (cl_data, f, highlight_cb)
1448 xg_menu_cb_data *cl_data;
1449 FRAME_PTR f;
1450 GCallback highlight_cb;
1451{
1452 if (cl_data)
1453 {
1454 cl_data->f = f;
1455 cl_data->menu_bar_vector = f->menu_bar_vector;
1456 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1457 cl_data->highlight_cb = highlight_cb;
1458 }
1459}
1460
1461/* Decrease reference count for CL_DATA.
1462 If reference count is zero, free CL_DATA. */
71bacd48 1463
f392e843
JD
1464static void
1465unref_cl_data (cl_data)
1466 xg_menu_cb_data *cl_data;
1467{
1468 if (cl_data && cl_data->ref_count > 0)
1469 {
1470 cl_data->ref_count--;
1471 if (cl_data->ref_count == 0)
1472 {
1473 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1474 xfree (cl_data);
1475 }
1476 }
1477}
1478
1479/* Function that marks all lisp data during GC. */
71bacd48 1480
f392e843
JD
1481void
1482xg_mark_data ()
1483{
1484 xg_list_node *iter;
1485
1486 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
631f2082 1487 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
f392e843
JD
1488
1489 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1490 {
1491 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1492
1493 if (! NILP (cb_data->help))
631f2082 1494 mark_object (cb_data->help);
f392e843
JD
1495 }
1496}
1497
1498
1499/* Callback called when a menu item is destroyed. Used to free data.
1500 W is the widget that is being destroyed (not used).
1501 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
71bacd48 1502
f392e843
JD
1503static void
1504menuitem_destroy_callback (w, client_data)
1505 GtkWidget *w;
1506 gpointer client_data;
1507{
1508 if (client_data)
1509 {
1510 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1511 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1512 xfree (data);
1513 }
1514}
1515
1516/* Callback called when the pointer enters/leaves a menu item.
1517 W is the menu item.
1518 EVENT is either an enter event or leave event.
1519 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1520
1521 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 1522
f392e843
JD
1523static gboolean
1524menuitem_highlight_callback (w, event, client_data)
1525 GtkWidget *w;
1526 GdkEventCrossing *event;
1527 gpointer client_data;
1528{
1529 if (client_data)
1530 {
1531 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1532 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data;
177c0ea7 1533
f392e843
JD
1534 if (! NILP (data->help) && data->cl_data->highlight_cb)
1535 {
1536 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1537 (*func) (w, call_data);
1538 }
1539 }
1540
1541 return FALSE;
1542}
1543
1544/* Callback called when a menu is destroyed. Used to free data.
1545 W is the widget that is being destroyed (not used).
1546 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
71bacd48 1547
f392e843
JD
1548static void
1549menu_destroy_callback (w, client_data)
1550 GtkWidget *w;
1551 gpointer client_data;
1552{
1553 unref_cl_data ((xg_menu_cb_data*) client_data);
1554}
1555
1556/* Callback called when a menu does a grab or ungrab. That means the
1557 menu has been activated or deactivated.
1558 Used to start a timer so the small timeout the menus in GTK uses before
1559 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1560 W is the widget that does the grab (not used).
1561 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1562 CLIENT_DATA is NULL (not used). */
71bacd48 1563
f392e843
JD
1564static void
1565menu_grab_callback (GtkWidget *widget,
1566 gboolean ungrab_p,
1567 gpointer client_data)
1568{
1569 /* Keep track of total number of grabs. */
1570 static int cnt;
1571
1572 if (ungrab_p) cnt--;
1573 else cnt++;
1574
1575 if (cnt > 0 && ! xg_timer) xg_start_timer ();
1576 else if (cnt == 0 && xg_timer) xg_stop_timer ();
1577}
1578
1579/* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1580 must be non-NULL) and can be inserted into a menu item.
1581
1582 Returns the GtkHBox. */
71bacd48 1583
f392e843
JD
1584static GtkWidget *
1585make_widget_for_menu_item (utf8_label, utf8_key)
1586 char *utf8_label;
1587 char *utf8_key;
1588{
1589 GtkWidget *wlbl;
1590 GtkWidget *wkey;
1591 GtkWidget *wbox;
177c0ea7 1592
f392e843 1593 wbox = gtk_hbox_new (FALSE, 0);
4b1b4443 1594 wlbl = gtk_label_new (utf8_label);
f392e843
JD
1595 wkey = gtk_label_new (utf8_key);
1596
1597 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1598 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
177c0ea7 1599
f392e843
JD
1600 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1601 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1602
1603 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1604 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
7863d625 1605 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
f392e843
JD
1606
1607 return wbox;
1608}
1609
1610/* Make and return a menu item widget with the key to the right.
1611 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1612 UTF8_KEY is the text representing the key binding.
1613 ITEM is the widget_value describing the menu item.
177c0ea7 1614
f392e843
JD
1615 GROUP is an in/out parameter. If the menu item to be created is not
1616 part of any radio menu group, *GROUP contains NULL on entry and exit.
1617 If the menu item to be created is part of a radio menu group, on entry
1618 *GROUP contains the group to use, or NULL if this is the first item
1619 in the group. On exit, *GROUP contains the radio item group.
1620
1621 Unfortunately, keys don't line up as nicely as in Motif,
1622 but the MacOS X version doesn't either, so I guess that is OK. */
71bacd48 1623
f392e843
JD
1624static GtkWidget *
1625make_menu_item (utf8_label, utf8_key, item, group)
1626 char *utf8_label;
1627 char *utf8_key;
1628 widget_value *item;
1629 GSList **group;
1630{
1631 GtkWidget *w;
1632 GtkWidget *wtoadd = 0;
177c0ea7 1633
adcb132c
JD
1634 /* It has been observed that some menu items have a NULL name field.
1635 This will lead to this function being called with a NULL utf8_label.
1636 GTK crashes on that so we set a blank label. Why there is a NULL
1637 name remains to be investigated. */
1638 if (! utf8_label) utf8_label = " ";
1639
f392e843
JD
1640 if (utf8_key)
1641 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
177c0ea7 1642
f392e843
JD
1643 if (item->button_type == BUTTON_TYPE_TOGGLE)
1644 {
1645 *group = NULL;
1646 if (utf8_key) w = gtk_check_menu_item_new ();
4b1b4443 1647 else w = gtk_check_menu_item_new_with_label (utf8_label);
f392e843
JD
1648 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1649 }
1650 else if (item->button_type == BUTTON_TYPE_RADIO)
1651 {
1652 if (utf8_key) w = gtk_radio_menu_item_new (*group);
4b1b4443 1653 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
f392e843
JD
1654 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1655 if (item->selected)
1656 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1657 }
1658 else
1659 {
1660 *group = NULL;
1661 if (utf8_key) w = gtk_menu_item_new ();
4b1b4443 1662 else w = gtk_menu_item_new_with_label (utf8_label);
f392e843 1663 }
177c0ea7 1664
f392e843
JD
1665 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1666 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1667
1668 return w;
1669}
1670
5fd6f727 1671/* Return non-zero if LABEL specifies a separator (GTK only has one
f392e843 1672 separator type) */
71bacd48 1673
f392e843 1674static int
5fd6f727
JD
1675xg_separator_p (char *label)
1676{
1677 if (! label) return 0;
1678 else if (strlen (label) > 3
1679 && strncmp (label, "--", 2) == 0
1680 && label[2] != '-')
1681 {
1682 static char* separator_names[] = {
1683 "space",
1684 "no-line",
1685 "single-line",
1686 "double-line",
1687 "single-dashed-line",
1688 "double-dashed-line",
1689 "shadow-etched-in",
1690 "shadow-etched-out",
1691 "shadow-etched-in-dash",
1692 "shadow-etched-out-dash",
1693 "shadow-double-etched-in",
1694 "shadow-double-etched-out",
1695 "shadow-double-etched-in-dash",
1696 "shadow-double-etched-out-dash",
1697 0,
1698 };
1699
1700 int i;
1701
1702 label += 2;
1703 for (i = 0; separator_names[i]; ++i)
1704 if (strcmp (label, separator_names[i]) == 0)
1705 return 1;
1706 }
1707 else
1708 {
1709 /* Old-style separator, maybe. It's a separator if it contains
1710 only dashes. */
1711 while (*label == '-')
1712 ++label;
1713 if (*label == 0) return 1;
1714 }
0a1d6de0 1715
5fd6f727 1716 return 0;
f392e843
JD
1717}
1718
da18b5ac
JD
1719static int xg_detached_menus;
1720
1721/* Returns non-zero if there are detached menus. */
71bacd48 1722
da18b5ac
JD
1723int
1724xg_have_tear_offs ()
1725{
1726 return xg_detached_menus > 0;
1727}
f392e843
JD
1728
1729/* Callback invoked when a detached menu window is removed. Here we
da18b5ac 1730 decrease the xg_detached_menus count.
f392e843 1731 WIDGET is the top level window that is removed (the parent of the menu).
da18b5ac 1732 CLIENT_DATA is not used. */
71bacd48 1733
da18b5ac
JD
1734static void
1735tearoff_remove (widget, client_data)
f392e843 1736 GtkWidget *widget;
f392e843
JD
1737 gpointer client_data;
1738{
da18b5ac 1739 if (xg_detached_menus > 0) --xg_detached_menus;
f392e843
JD
1740}
1741
da18b5ac
JD
1742/* Callback invoked when a menu is detached. It increases the
1743 xg_detached_menus count.
f392e843 1744 WIDGET is the GtkTearoffMenuItem.
177c0ea7 1745 CLIENT_DATA is not used. */
71bacd48 1746
f392e843
JD
1747static void
1748tearoff_activate (widget, client_data)
1749 GtkWidget *widget;
1750 gpointer client_data;
1751{
1752 GtkWidget *menu = gtk_widget_get_parent (widget);
da18b5ac
JD
1753 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1754 {
1755 ++xg_detached_menus;
1756 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1757 "destroy",
1758 G_CALLBACK (tearoff_remove), 0);
1759 }
f392e843
JD
1760}
1761
f392e843 1762
f392e843
JD
1763/* Create a menu item widget, and connect the callbacks.
1764 ITEM decribes the menu item.
1765 F is the frame the created menu belongs to.
1766 SELECT_CB is the callback to use when a menu item is selected.
1767 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1768 CL_DATA points to the callback data to be used for this menu.
1769 GROUP is an in/out parameter. If the menu item to be created is not
1770 part of any radio menu group, *GROUP contains NULL on entry and exit.
1771 If the menu item to be created is part of a radio menu group, on entry
1772 *GROUP contains the group to use, or NULL if this is the first item
1773 in the group. On exit, *GROUP contains the radio item group.
1774
1775 Returns the created GtkWidget. */
71bacd48 1776
f392e843
JD
1777static GtkWidget *
1778xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group)
1779 widget_value *item;
1780 FRAME_PTR f;
1781 GCallback select_cb;
1782 GCallback highlight_cb;
1783 xg_menu_cb_data *cl_data;
1784 GSList **group;
1785{
1786 char *utf8_label;
1787 char *utf8_key;
1788 GtkWidget *w;
1789 xg_menu_item_cb_data *cb_data;
1790
1791 utf8_label = get_utf8_string (item->name);
1792 utf8_key = get_utf8_string (item->key);
1793
1794 w = make_menu_item (utf8_label, utf8_key, item, group);
1795
1796 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1797 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1798
1799 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1800
1801 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1802
1803 cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0;
1804 cb_data->help = item->help;
1805 cb_data->cl_data = cl_data;
1806 cb_data->call_data = item->call_data;
177c0ea7 1807
f392e843
JD
1808 g_signal_connect (G_OBJECT (w),
1809 "destroy",
1810 G_CALLBACK (menuitem_destroy_callback),
1811 cb_data);
1812
1813 /* Put cb_data in widget, so we can get at it when modifying menubar */
1814 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
1815
1816 /* final item, not a submenu */
1817 if (item->call_data && ! item->contents)
1818 {
1819 if (select_cb)
1820 cb_data->select_id
1821 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
1822 }
1823
1824 if (! NILP (item->help) && highlight_cb)
1825 {
1826 /* We use enter/leave notify instead of select/deselect because
1827 select/deselect doesn't go well with detached menus. */
1828 cb_data->highlight_id
1829 = g_signal_connect (G_OBJECT (w),
1830 "enter-notify-event",
1831 G_CALLBACK (menuitem_highlight_callback),
1832 cb_data);
1833 cb_data->unhighlight_id
1834 = g_signal_connect (G_OBJECT (w),
1835 "leave-notify-event",
1836 G_CALLBACK (menuitem_highlight_callback),
1837 cb_data);
1838 }
1839
1840 return w;
1841}
1842
9d6194d6
AS
1843static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback,
1844 GCallback, GCallback, int, int, int,
1845 GtkWidget *, xg_menu_cb_data *, char *));
1846
f392e843
JD
1847/* Create a full menu tree specified by DATA.
1848 F is the frame the created menu belongs to.
1849 SELECT_CB is the callback to use when a menu item is selected.
1850 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1851 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1852 POP_UP_P is non-zero if we shall create a popup menu.
1853 MENU_BAR_P is non-zero if we shall create a menu bar.
1854 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1855 if MENU_BAR_P is non-zero.
1856 TOPMENU is the topmost GtkWidget that others shall be placed under.
1857 It may be NULL, in that case we create the appropriate widget
1858 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1859 CL_DATA is the callback data we shall use for this menu, or NULL
1860 if we haven't set the first callback yet.
1861 NAME is the name to give to the top level menu if this function
1862 creates it. May be NULL to not set any name.
1863
1864 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1865 not NULL.
1866
1867 This function calls itself to create submenus. */
1868
1869static GtkWidget *
1870create_menus (data, f, select_cb, deactivate_cb, highlight_cb,
1871 pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name)
1872 widget_value *data;
1873 FRAME_PTR f;
1874 GCallback select_cb;
1875 GCallback deactivate_cb;
1876 GCallback highlight_cb;
1877 int pop_up_p;
1878 int menu_bar_p;
1879 int add_tearoff_p;
1880 GtkWidget *topmenu;
1881 xg_menu_cb_data *cl_data;
1882 char *name;
1883{
1884 widget_value *item;
1885 GtkWidget *wmenu = topmenu;
1886 GSList *group = NULL;
1887
1888 if (! topmenu)
1889 {
810f2256
JD
1890 if (! menu_bar_p)
1891 {
1892 wmenu = gtk_menu_new ();
1893 xg_set_screen (wmenu, f);
1894 }
f392e843
JD
1895 else wmenu = gtk_menu_bar_new ();
1896
1897 /* Put cl_data on the top menu for easier access. */
1898 cl_data = make_cl_data (cl_data, f, highlight_cb);
1899 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
1900 g_signal_connect (G_OBJECT (wmenu), "destroy",
1901 G_CALLBACK (menu_destroy_callback), cl_data);
177c0ea7 1902
f392e843
JD
1903 if (name)
1904 gtk_widget_set_name (wmenu, name);
1905
1906 if (deactivate_cb)
1907 g_signal_connect (G_OBJECT (wmenu),
c8934d9d 1908 "selection-done", deactivate_cb, 0);
f392e843
JD
1909
1910 g_signal_connect (G_OBJECT (wmenu),
1911 "grab-notify", G_CALLBACK (menu_grab_callback), 0);
1912 }
177c0ea7 1913
f392e843
JD
1914 if (! menu_bar_p && add_tearoff_p)
1915 {
1916 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1917 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
1918
1919 g_signal_connect (G_OBJECT (tearoff), "activate",
1920 G_CALLBACK (tearoff_activate), 0);
1921 }
1922
1923 for (item = data; item; item = item->next)
1924 {
1925 GtkWidget *w;
177c0ea7 1926
f392e843
JD
1927 if (pop_up_p && !item->contents && !item->call_data
1928 && !xg_separator_p (item->name))
1929 {
1930 char *utf8_label;
1931 /* A title for a popup. We do the same as GTK does when
1932 creating titles, but it does not look good. */
1933 group = NULL;
1934 utf8_label = get_utf8_string (item->name);
1935
1936 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
4b1b4443 1937 w = gtk_menu_item_new_with_label (utf8_label);
f392e843
JD
1938 gtk_widget_set_sensitive (w, FALSE);
1939 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1940 }
1941 else if (xg_separator_p (item->name))
1942 {
1943 group = NULL;
1944 /* GTK only have one separator type. */
1945 w = gtk_separator_menu_item_new ();
1946 }
1947 else
1948 {
1949 w = xg_create_one_menuitem (item,
1950 f,
1951 item->contents ? 0 : select_cb,
1952 highlight_cb,
1953 cl_data,
1954 &group);
1955
1956 if (item->contents)
1957 {
1958 GtkWidget *submenu = create_menus (item->contents,
1959 f,
1960 select_cb,
1961 deactivate_cb,
1962 highlight_cb,
1963 0,
1964 0,
da18b5ac 1965 add_tearoff_p,
f392e843
JD
1966 0,
1967 cl_data,
1968 0);
1969 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
1970 }
f392e843
JD
1971 }
1972
1973 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
1974 gtk_widget_set_name (w, MENU_ITEM_NAME);
1975 }
1976
1977 return wmenu;
1978}
1979
1980/* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1981 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1982 with some text and buttons.
1983 F is the frame the created item belongs to.
1984 NAME is the name to use for the top widget.
1985 VAL is a widget_value structure describing items to be created.
1986 SELECT_CB is the callback to use when a menu item is selected or
1987 a dialog button is pressed.
1988 DEACTIVATE_CB is the callback to use when an item is deactivated.
1989 For a menu, when a sub menu is not shown anymore, for a dialog it is
1990 called when the dialog is popped down.
1991 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1992
1993 Returns the widget created. */
71bacd48 1994
f392e843
JD
1995GtkWidget *
1996xg_create_widget (type, name, f, val,
1997 select_cb, deactivate_cb, highlight_cb)
1998 char *type;
1999 char *name;
2000 FRAME_PTR f;
2001 widget_value *val;
2002 GCallback select_cb;
2003 GCallback deactivate_cb;
2004 GCallback highlight_cb;
2005{
2006 GtkWidget *w = 0;
da18b5ac
JD
2007 int menu_bar_p = strcmp (type, "menubar") == 0;
2008 int pop_up_p = strcmp (type, "popup") == 0;
2009
f392e843
JD
2010 if (strcmp (type, "dialog") == 0)
2011 {
2012 w = create_dialog (val, select_cb, deactivate_cb);
810f2256 2013 xg_set_screen (w, f);
f392e843
JD
2014 gtk_window_set_transient_for (GTK_WINDOW (w),
2015 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2016 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
810f2256 2017 gtk_widget_set_name (w, "emacs-dialog");
457a8155 2018 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
f392e843 2019 }
da18b5ac 2020 else if (menu_bar_p || pop_up_p)
f392e843
JD
2021 {
2022 w = create_menus (val->contents,
2023 f,
2024 select_cb,
2025 deactivate_cb,
2026 highlight_cb,
da18b5ac
JD
2027 pop_up_p,
2028 menu_bar_p,
2029 menu_bar_p,
f392e843
JD
2030 0,
2031 0,
2032 name);
2033
2034 /* Set the cursor to an arrow for popup menus when they are mapped.
2035 This is done by default for menu bar menus. */
da18b5ac 2036 if (pop_up_p)
f392e843
JD
2037 {
2038 /* Must realize so the GdkWindow inside the widget is created. */
2039 gtk_widget_realize (w);
810f2256 2040 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
f392e843
JD
2041 }
2042 }
2043 else
2044 {
2045 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2046 type);
2047 }
2048
2049 return w;
2050}
2051
0a1d6de0 2052/* Return the label for menu item WITEM. */
71bacd48 2053
f392e843
JD
2054static const char *
2055xg_get_menu_item_label (witem)
2056 GtkMenuItem *witem;
2057{
2058 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2059 return gtk_label_get_label (wlabel);
2060}
2061
0a1d6de0 2062/* Return non-zero if the menu item WITEM has the text LABEL. */
71bacd48 2063
f392e843
JD
2064static int
2065xg_item_label_same_p (witem, label)
2066 GtkMenuItem *witem;
2067 char *label;
2068{
0a1d6de0 2069 int is_same = 0;
f392e843 2070 char *utf8_label = get_utf8_string (label);
0a1d6de0
JD
2071 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2072
2073 if (! old_label && ! utf8_label)
2074 is_same = 1;
2075 else if (old_label && utf8_label)
2076 is_same = strcmp (utf8_label, old_label) == 0;
2077
2078 if (utf8_label && utf8_label != label) g_free (utf8_label);
f392e843
JD
2079
2080 return is_same;
2081}
2082
71bacd48
JD
2083/* Destroy widgets in LIST. */
2084
f392e843 2085static void
71bacd48 2086xg_destroy_widgets (list)
f392e843
JD
2087 GList *list;
2088{
f392e843
JD
2089 GList *iter;
2090
49853a4d 2091 for (iter = list; iter; iter = g_list_next (iter))
f392e843
JD
2092 {
2093 GtkWidget *w = GTK_WIDGET (iter->data);
2094
71bacd48 2095 /* Destroying the widget will remove it from the container it is in. */
f392e843
JD
2096 gtk_widget_destroy (w);
2097 }
f392e843
JD
2098}
2099
2100/* Update the top level names in MENUBAR (i.e. not submenus).
2101 F is the frame the menu bar belongs to.
49853a4d
JD
2102 *LIST is a list with the current menu bar names (menu item widgets).
2103 ITER is the item within *LIST that shall be updated.
2104 POS is the numerical position, starting at 0, of ITER in *LIST.
f392e843
JD
2105 VAL describes what the menu bar shall look like after the update.
2106 SELECT_CB is the callback to use when a menu item is selected.
2107 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
49853a4d 2108 CL_DATA points to the callback data to be used for this menu bar.
f392e843
JD
2109
2110 This function calls itself to walk through the menu bar names. */
71bacd48 2111
f392e843 2112static void
49853a4d
JD
2113xg_update_menubar (menubar, f, list, iter, pos, val,
2114 select_cb, highlight_cb, cl_data)
f392e843
JD
2115 GtkWidget *menubar;
2116 FRAME_PTR f;
49853a4d
JD
2117 GList **list;
2118 GList *iter;
2119 int pos;
f392e843
JD
2120 widget_value *val;
2121 GCallback select_cb;
2122 GCallback highlight_cb;
2123 xg_menu_cb_data *cl_data;
2124{
49853a4d 2125 if (! iter && ! val)
f392e843 2126 return;
49853a4d 2127 else if (iter && ! val)
f392e843 2128 {
49853a4d 2129 /* Item(s) have been removed. Remove all remaining items. */
71bacd48 2130 xg_destroy_widgets (iter);
f392e843
JD
2131
2132 /* All updated. */
2133 val = 0;
49853a4d 2134 iter = 0;
f392e843 2135 }
49853a4d 2136 else if (! iter && val)
f392e843
JD
2137 {
2138 /* Item(s) added. Add all new items in one call. */
2139 create_menus (val, f, select_cb, 0, highlight_cb,
2140 0, 1, 0, menubar, cl_data, 0);
2141
2142 /* All updated. */
2143 val = 0;
49853a4d 2144 iter = 0;
f392e843 2145 }
49853a4d
JD
2146 /* Below this neither iter or val is NULL */
2147 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
f392e843
JD
2148 {
2149 /* This item is still the same, check next item. */
2150 val = val->next;
49853a4d
JD
2151 iter = g_list_next (iter);
2152 ++pos;
f392e843
JD
2153 }
2154 else /* This item is changed. */
2155 {
49853a4d 2156 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
f392e843 2157 GtkMenuItem *witem2 = 0;
f392e843 2158 int val_in_menubar = 0;
49853a4d
JD
2159 int iter_in_new_menubar = 0;
2160 GList *iter2;
f392e843
JD
2161 widget_value *cur;
2162
f392e843 2163 /* See if the changed entry (val) is present later in the menu bar */
49853a4d
JD
2164 for (iter2 = iter;
2165 iter2 && ! val_in_menubar;
2166 iter2 = g_list_next (iter2))
f392e843 2167 {
49853a4d 2168 witem2 = GTK_MENU_ITEM (iter2->data);
f392e843
JD
2169 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2170 }
2171
49853a4d 2172 /* See if the current entry (iter) is present later in the
f392e843 2173 specification for the new menu bar. */
49853a4d
JD
2174 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2175 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
f392e843 2176
49853a4d 2177 if (val_in_menubar && ! iter_in_new_menubar)
f392e843 2178 {
49853a4d
JD
2179 int nr = pos;
2180
f392e843
JD
2181 /* This corresponds to:
2182 Current: A B C
2183 New: A C
2184 Remove B. */
177c0ea7 2185
f392e843
JD
2186 gtk_widget_ref (GTK_WIDGET (witem));
2187 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2188 gtk_widget_destroy (GTK_WIDGET (witem));
2189
2190 /* Must get new list since the old changed. */
49853a4d
JD
2191 g_list_free (*list);
2192 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2193 while (nr-- > 0) iter = g_list_next (iter);
f392e843 2194 }
49853a4d 2195 else if (! val_in_menubar && ! iter_in_new_menubar)
f392e843
JD
2196 {
2197 /* This corresponds to:
2198 Current: A B C
2199 New: A X C
2200 Rename B to X. This might seem to be a strange thing to do,
2201 since if there is a menu under B it will be totally wrong for X.
2202 But consider editing a C file. Then there is a C-mode menu
2203 (corresponds to B above).
2204 If then doing C-x C-f the minibuf menu (X above) replaces the
2205 C-mode menu. When returning from the minibuffer, we get
2206 back the C-mode menu. Thus we do:
2207 Rename B to X (C-mode to minibuf menu)
2208 Rename X to B (minibuf to C-mode menu).
2209 If the X menu hasn't been invoked, the menu under B
2210 is up to date when leaving the minibuffer. */
2211 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2212 char *utf8_label = get_utf8_string (val->name);
da18b5ac 2213 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
177c0ea7 2214
4b1b4443 2215 gtk_label_set_text (wlabel, utf8_label);
f392e843 2216
da18b5ac
JD
2217 /* If this item has a submenu that has been detached, change
2218 the title in the WM decorations also. */
2219 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2220 /* Set the title of the detached window. */
2221 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2222
49853a4d 2223 iter = g_list_next (iter);
f392e843 2224 val = val->next;
49853a4d 2225 ++pos;
f392e843 2226 }
49853a4d 2227 else if (! val_in_menubar && iter_in_new_menubar)
f392e843
JD
2228 {
2229 /* This corresponds to:
2230 Current: A B C
2231 New: A X B C
2232 Insert X. */
2233
49853a4d 2234 int nr = pos;
f392e843
JD
2235 GList *group = 0;
2236 GtkWidget *w = xg_create_one_menuitem (val,
2237 f,
2238 select_cb,
2239 highlight_cb,
2240 cl_data,
2241 &group);
2242
2243 gtk_widget_set_name (w, MENU_ITEM_NAME);
2244 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2245
49853a4d
JD
2246 g_list_free (*list);
2247 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2248 while (nr-- > 0) iter = g_list_next (iter);
2249 iter = g_list_next (iter);
f392e843 2250 val = val->next;
49853a4d 2251 ++pos;
f392e843 2252 }
49853a4d 2253 else /* if (val_in_menubar && iter_in_new_menubar) */
f392e843 2254 {
49853a4d 2255 int nr = pos;
f392e843
JD
2256 /* This corresponds to:
2257 Current: A B C
2258 New: A C B
2259 Move C before B */
2260
2261 gtk_widget_ref (GTK_WIDGET (witem2));
2262 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2263 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2264 GTK_WIDGET (witem2), pos);
2265 gtk_widget_unref (GTK_WIDGET (witem2));
2266
49853a4d
JD
2267 g_list_free (*list);
2268 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2269 while (nr-- > 0) iter = g_list_next (iter);
f392e843 2270 val = val->next;
49853a4d 2271 ++pos;
f392e843 2272 }
f392e843
JD
2273 }
2274
2275 /* Update the rest of the menu bar. */
49853a4d
JD
2276 xg_update_menubar (menubar, f, list, iter, pos, val,
2277 select_cb, highlight_cb, cl_data);
f392e843
JD
2278}
2279
2280/* Update the menu item W so it corresponds to VAL.
2281 SELECT_CB is the callback to use when a menu item is selected.
2282 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2283 CL_DATA is the data to set in the widget for menu invokation. */
71bacd48 2284
f392e843
JD
2285static void
2286xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data)
2287 widget_value *val;
2288 GtkWidget *w;
2289 GCallback select_cb;
2290 GCallback highlight_cb;
2291 xg_menu_cb_data *cl_data;
2292{
2293 GtkWidget *wchild;
2294 GtkLabel *wlbl = 0;
2295 GtkLabel *wkey = 0;
2296 char *utf8_label;
2297 char *utf8_key;
0a1d6de0
JD
2298 const char *old_label = 0;
2299 const char *old_key = 0;
f392e843 2300 xg_menu_item_cb_data *cb_data;
177c0ea7
JB
2301
2302 wchild = gtk_bin_get_child (GTK_BIN (w));
f392e843
JD
2303 utf8_label = get_utf8_string (val->name);
2304 utf8_key = get_utf8_string (val->key);
2305
2306 /* See if W is a menu item with a key. See make_menu_item above. */
2307 if (GTK_IS_HBOX (wchild))
2308 {
2309 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2310
2311 wlbl = GTK_LABEL (list->data);
2312 wkey = GTK_LABEL (list->next->data);
49853a4d
JD
2313 g_list_free (list);
2314
f392e843
JD
2315 if (! utf8_key)
2316 {
2317 /* Remove the key and keep just the label. */
2318 gtk_widget_ref (GTK_WIDGET (wlbl));
2319 gtk_container_remove (GTK_CONTAINER (w), wchild);
2320 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2321 wkey = 0;
2322 }
49853a4d 2323
f392e843
JD
2324 }
2325 else /* Just a label. */
2326 {
2327 wlbl = GTK_LABEL (wchild);
2328
2329 /* Check if there is now a key. */
2330 if (utf8_key)
2331 {
2332 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2333 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
49853a4d 2334
f392e843
JD
2335 wlbl = GTK_LABEL (list->data);
2336 wkey = GTK_LABEL (list->next->data);
49853a4d 2337 g_list_free (list);
f392e843
JD
2338
2339 gtk_container_remove (GTK_CONTAINER (w), wchild);
2340 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2341 }
2342 }
2343
177c0ea7 2344
0a1d6de0
JD
2345 if (wkey) old_key = gtk_label_get_label (wkey);
2346 if (wlbl) old_label = gtk_label_get_label (wlbl);
177c0ea7 2347
0a1d6de0 2348 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
f392e843
JD
2349 gtk_label_set_text (wkey, utf8_key);
2350
0a1d6de0 2351 if (! old_label || strcmp (utf8_label, old_label) != 0)
4b1b4443 2352 gtk_label_set_text (wlbl, utf8_label);
f392e843 2353
0a1d6de0
JD
2354 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2355 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
177c0ea7 2356
f392e843
JD
2357 if (! val->enabled && GTK_WIDGET_SENSITIVE (w))
2358 gtk_widget_set_sensitive (w, FALSE);
2359 else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w))
2360 gtk_widget_set_sensitive (w, TRUE);
2361
2362 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2363 XG_ITEM_DATA);
2364 if (cb_data)
2365 {
2366 cb_data->call_data = val->call_data;
2367 cb_data->help = val->help;
2368 cb_data->cl_data = cl_data;
177c0ea7 2369
f392e843
JD
2370 /* We assume the callback functions don't change. */
2371 if (val->call_data && ! val->contents)
2372 {
2373 /* This item shall have a select callback. */
2374 if (! cb_data->select_id)
2375 cb_data->select_id
2376 = g_signal_connect (G_OBJECT (w), "activate",
2377 select_cb, cb_data);
2378 }
2379 else if (cb_data->select_id)
2380 {
2381 g_signal_handler_disconnect (w, cb_data->select_id);
2382 cb_data->select_id = 0;
2383 }
2384
2385 if (NILP (cb_data->help))
2386 {
2387 /* Shall not have help. Remove if any existed previously. */
2388 if (cb_data->highlight_id)
2389 {
2390 g_signal_handler_disconnect (G_OBJECT (w),
2391 cb_data->highlight_id);
2392 cb_data->highlight_id = 0;
2393 }
2394 if (cb_data->unhighlight_id)
2395 {
2396 g_signal_handler_disconnect (G_OBJECT (w),
2397 cb_data->unhighlight_id);
2398 cb_data->unhighlight_id = 0;
2399 }
2400 }
2401 else if (! cb_data->highlight_id && highlight_cb)
2402 {
2403 /* Have help now, but didn't previously. Add callback. */
2404 cb_data->highlight_id
2405 = g_signal_connect (G_OBJECT (w),
2406 "enter-notify-event",
2407 G_CALLBACK (menuitem_highlight_callback),
2408 cb_data);
2409 cb_data->unhighlight_id
2410 = g_signal_connect (G_OBJECT (w),
2411 "leave-notify-event",
2412 G_CALLBACK (menuitem_highlight_callback),
2413 cb_data);
2414 }
2415 }
2416}
2417
2418/* Update the toggle menu item W so it corresponds to VAL. */
71bacd48 2419
f392e843
JD
2420static void
2421xg_update_toggle_item (val, w)
2422 widget_value *val;
2423 GtkWidget *w;
2424{
2425 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2426}
2427
2428/* Update the radio menu item W so it corresponds to VAL. */
71bacd48 2429
f392e843
JD
2430static void
2431xg_update_radio_item (val, w)
2432 widget_value *val;
2433 GtkWidget *w;
2434{
2435 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2436}
2437
2438/* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2439 SUBMENU may be NULL, in that case a new menu is created.
2440 F is the frame the menu bar belongs to.
2441 VAL describes the contents of the menu bar.
2442 SELECT_CB is the callback to use when a menu item is selected.
2443 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2444 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2445 CL_DATA is the call back data to use for any newly created items.
2446
2447 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2448 was NULL. */
2449
2450static GtkWidget *
2451xg_update_submenu (submenu, f, val,
2452 select_cb, deactivate_cb, highlight_cb, cl_data)
2453 GtkWidget *submenu;
2454 FRAME_PTR f;
2455 widget_value *val;
2456 GCallback select_cb;
2457 GCallback deactivate_cb;
2458 GCallback highlight_cb;
2459 xg_menu_cb_data *cl_data;
2460{
2461 GtkWidget *newsub = submenu;
2462 GList *list = 0;
2463 GList *iter;
2464 widget_value *cur;
2465 int has_tearoff_p = 0;
2466 GList *first_radio = 0;
177c0ea7 2467
f392e843
JD
2468 if (submenu)
2469 list = gtk_container_get_children (GTK_CONTAINER (submenu));
177c0ea7 2470
f392e843
JD
2471 for (cur = val, iter = list;
2472 cur && iter;
2473 iter = g_list_next (iter), cur = cur->next)
2474 {
2475 GtkWidget *w = GTK_WIDGET (iter->data);
2476
2477 /* Skip tearoff items, they have no counterpart in val. */
2478 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2479 {
2480 has_tearoff_p = 1;
2481 iter = g_list_next (iter);
2482 if (iter) w = GTK_WIDGET (iter->data);
2483 else break;
2484 }
2485
2486 /* Remember first radio button in a group. If we get a mismatch in
2487 a radio group we must rebuild the whole group so that the connections
2488 in GTK becomes correct. */
2489 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2490 first_radio = iter;
2491 else if (cur->button_type != BUTTON_TYPE_RADIO
2492 && ! GTK_IS_RADIO_MENU_ITEM (w))
2493 first_radio = 0;
2494
2495 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2496 {
2497 if (! xg_separator_p (cur->name))
2498 break;
2499 }
2500 else if (GTK_IS_CHECK_MENU_ITEM (w))
2501 {
2502 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2503 break;
2504 xg_update_toggle_item (cur, w);
2505 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2506 }
2507 else if (GTK_IS_RADIO_MENU_ITEM (w))
2508 {
2509 if (cur->button_type != BUTTON_TYPE_RADIO)
2510 break;
2511 xg_update_radio_item (cur, w);
2512 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2513 }
2514 else if (GTK_IS_MENU_ITEM (w))
2515 {
2516 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2517 GtkWidget *sub;
2518
2519 if (cur->button_type != BUTTON_TYPE_NONE ||
2520 xg_separator_p (cur->name))
2521 break;
2522
2523 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2524
2525 sub = gtk_menu_item_get_submenu (witem);
2526 if (sub && ! cur->contents)
2527 {
2528 /* Not a submenu anymore. */
2529 gtk_widget_ref (sub);
2530 gtk_menu_item_remove_submenu (witem);
2531 gtk_widget_destroy (sub);
2532 }
2533 else if (cur->contents)
2534 {
2535 GtkWidget *nsub;
2536
2537 nsub = xg_update_submenu (sub, f, cur->contents,
2538 select_cb, deactivate_cb,
2539 highlight_cb, cl_data);
2540
2541 /* If this item just became a submenu, we must set it. */
2542 if (nsub != sub)
2543 gtk_menu_item_set_submenu (witem, nsub);
2544 }
2545 }
2546 else
2547 {
2548 /* Structural difference. Remove everything from here and down
2549 in SUBMENU. */
2550 break;
2551 }
2552 }
2553
2554 /* Remove widgets from first structual change. */
2555 if (iter)
2556 {
2557 /* If we are adding new menu items below, we must remove from
2558 first radio button so that radio groups become correct. */
71bacd48
JD
2559 if (cur && first_radio) xg_destroy_widgets (first_radio);
2560 else xg_destroy_widgets (iter);
f392e843 2561 }
177c0ea7 2562
f392e843
JD
2563 if (cur)
2564 {
2565 /* More items added. Create them. */
2566 newsub = create_menus (cur,
2567 f,
2568 select_cb,
2569 deactivate_cb,
2570 highlight_cb,
2571 0,
2572 0,
2573 ! has_tearoff_p,
2574 submenu,
2575 cl_data,
2576 0);
2577 }
177c0ea7 2578
49853a4d
JD
2579 if (list) g_list_free (list);
2580
f392e843
JD
2581 return newsub;
2582}
2583
2584/* Update the MENUBAR.
2585 F is the frame the menu bar belongs to.
2586 VAL describes the contents of the menu bar.
2587 If DEEP_P is non-zero, rebuild all but the top level menu names in
2588 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2589 SELECT_CB is the callback to use when a menu item is selected.
2590 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2591 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
71bacd48 2592
f392e843
JD
2593void
2594xg_modify_menubar_widgets (menubar, f, val, deep_p,
2595 select_cb, deactivate_cb, highlight_cb)
2596 GtkWidget *menubar;
2597 FRAME_PTR f;
2598 widget_value *val;
2599 int deep_p;
2600 GCallback select_cb;
2601 GCallback deactivate_cb;
2602 GCallback highlight_cb;
2603{
2604 xg_menu_cb_data *cl_data;
2605 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
f392e843
JD
2606
2607 if (! list) return;
177c0ea7 2608
f392e843
JD
2609 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2610 XG_FRAME_DATA);
2611
da18b5ac
JD
2612 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2613 select_cb, highlight_cb, cl_data);
2614
2615 if (deep_p);
f392e843
JD
2616 {
2617 widget_value *cur;
2618
2619 /* Update all sub menus.
da18b5ac 2620 We must keep the submenus (GTK menu item widgets) since the
f392e843
JD
2621 X Window in the XEvent that activates the menu are those widgets. */
2622
2623 /* Update cl_data, menu_item things in F may have changed. */
2624 update_cl_data (cl_data, f, highlight_cb);
2625
2626 for (cur = val->contents; cur; cur = cur->next)
2627 {
49853a4d 2628 GList *iter;
f392e843
JD
2629 GtkWidget *sub = 0;
2630 GtkWidget *newsub;
2631 GtkMenuItem *witem;
2632
2633 /* Find sub menu that corresponds to val and update it. */
2634 for (iter = list ; iter; iter = g_list_next (iter))
2635 {
2636 witem = GTK_MENU_ITEM (iter->data);
2637 if (xg_item_label_same_p (witem, cur->name))
2638 {
2639 sub = gtk_menu_item_get_submenu (witem);
2640 break;
2641 }
2642 }
177c0ea7 2643
f392e843
JD
2644 newsub = xg_update_submenu (sub,
2645 f,
2646 cur->contents,
2647 select_cb,
2648 deactivate_cb,
2649 highlight_cb,
2650 cl_data);
2651 /* sub may still be NULL. If we just updated non deep and added
2652 a new menu bar item, it has no sub menu yet. So we set the
2653 newly created sub menu under witem. */
2654 if (newsub != sub)
810f2256
JD
2655 {
2656 xg_set_screen (newsub, f);
2657 gtk_menu_item_set_submenu (witem, newsub);
2658 }
f392e843
JD
2659 }
2660 }
2661
49853a4d 2662 g_list_free (list);
f392e843
JD
2663 gtk_widget_show_all (menubar);
2664}
2665
2666/* Recompute all the widgets of frame F, when the menu bar has been
2667 changed. Value is non-zero if widgets were updated. */
2668
2669int
2670xg_update_frame_menubar (f)
2671 FRAME_PTR f;
2672{
2673 struct x_output *x = f->output_data.x;
2674 GtkRequisition req;
177c0ea7 2675
f392e843
JD
2676 if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget))
2677 return 0;
2678
2679 BLOCK_INPUT;
2680
2681 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2682 FALSE, FALSE, 0);
2683 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2684
2685 gtk_widget_show_all (x->menubar_widget);
2686 gtk_widget_size_request (x->menubar_widget, &req);
2687
2688 FRAME_MENUBAR_HEIGHT (f) = req.height;
2689
2690 /* The height has changed, resize outer widget and set columns
2691 rows to what we had before adding the menu bar. */
be786000 2692 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
177c0ea7 2693
f392e843
JD
2694 SET_FRAME_GARBAGED (f);
2695 UNBLOCK_INPUT;
a8303f92
AS
2696
2697 return 1;
f392e843
JD
2698}
2699
2700/* Get rid of the menu bar of frame F, and free its storage.
2701 This is used when deleting a frame, and when turning off the menu bar. */
2702
2703void
2704free_frame_menubar (f)
2705 FRAME_PTR f;
2706{
2707 struct x_output *x = f->output_data.x;
2708
2709 if (x->menubar_widget)
2710 {
2711 BLOCK_INPUT;
2712
2713 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2714 /* The menubar and its children shall be deleted when removed from
2715 the container. */
2716 x->menubar_widget = 0;
2717 FRAME_MENUBAR_HEIGHT (f) = 0;
2718
2719 /* The height has changed, resize outer widget and set columns
2720 rows to what we had before removing the menu bar. */
be786000 2721 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
f392e843
JD
2722
2723 SET_FRAME_GARBAGED (f);
2724 UNBLOCK_INPUT;
2725 }
2726}
2727
2728
2729\f
2730/***********************************************************************
2731 Scroll bar functions
2732 ***********************************************************************/
2733
2734
2735/* Setting scroll bar values invokes the callback. Use this variable
2736 to indicate that callback should do nothing. */
71bacd48 2737
f392e843
JD
2738int xg_ignore_gtk_scrollbar;
2739
f392e843
JD
2740/* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2741 32 bits. But we want to store pointers, and they may be larger
2742 than 32 bits. Keep a mapping from integer index to widget pointers
2743 to get around the 32 bit limitation. */
71bacd48 2744
f392e843
JD
2745static struct
2746{
2747 GtkWidget **widgets;
2748 int max_size;
2749 int used;
81e302ef 2750} id_to_widget;
f392e843
JD
2751
2752/* Grow this much every time we need to allocate more */
71bacd48 2753
f392e843
JD
2754#define ID_TO_WIDGET_INCR 32
2755
2756/* Store the widget pointer W in id_to_widget and return the integer index. */
71bacd48 2757
f392e843
JD
2758static int
2759xg_store_widget_in_map (w)
2760 GtkWidget *w;
2761{
2762 int i;
2763
2764 if (id_to_widget.max_size == id_to_widget.used)
2765 {
2766 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2767
2768 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2769 sizeof (GtkWidget *)*new_size);
2770
2771 for (i = id_to_widget.max_size; i < new_size; ++i)
2772 id_to_widget.widgets[i] = 0;
2773 id_to_widget.max_size = new_size;
2774 }
2775
2776 /* Just loop over the array and find a free place. After all,
2777 how many scroll bars are we creating? Should be a small number.
2778 The check above guarantees we will find a free place. */
2779 for (i = 0; i < id_to_widget.max_size; ++i)
2780 {
2781 if (! id_to_widget.widgets[i])
2782 {
2783 id_to_widget.widgets[i] = w;
2784 ++id_to_widget.used;
2785
2786 return i;
2787 }
2788 }
2789
2790 /* Should never end up here */
2791 abort ();
2792}
2793
2794/* Remove pointer at IDX from id_to_widget.
2795 Called when scroll bar is destroyed. */
71bacd48 2796
f392e843
JD
2797static void
2798xg_remove_widget_from_map (idx)
2799 int idx;
2800{
2801 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2802 {
2803 id_to_widget.widgets[idx] = 0;
2804 --id_to_widget.used;
2805 }
2806}
2807
2808/* Get the widget pointer at IDX from id_to_widget. */
71bacd48 2809
f392e843
JD
2810static GtkWidget *
2811xg_get_widget_from_map (idx)
2812 int idx;
2813{
2814 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2815 return id_to_widget.widgets[idx];
2816
2817 return 0;
2818}
2819
810f2256 2820/* Return the scrollbar id for X Window WID on display DPY.
3a8a22fc 2821 Return -1 if WID not in id_to_widget. */
71bacd48 2822
3a8a22fc 2823int
810f2256
JD
2824xg_get_scroll_id_for_window (dpy, wid)
2825 Display *dpy;
3a8a22fc
JD
2826 Window wid;
2827{
2828 int idx;
2829 GtkWidget *w;
2830
810f2256 2831 w = xg_win_to_widget (dpy, wid);
3a8a22fc
JD
2832
2833 if (w)
2834 {
2835 for (idx = 0; idx < id_to_widget.max_size; ++idx)
2836 if (id_to_widget.widgets[idx] == w)
2837 return idx;
2838 }
2839
2840 return -1;
2841}
2842
f392e843
JD
2843/* Callback invoked when scroll bar WIDGET is destroyed.
2844 DATA is the index into id_to_widget for WIDGET.
cea9be54 2845 We free pointer to last scroll bar values here and remove the index. */
71bacd48 2846
f392e843
JD
2847static void
2848xg_gtk_scroll_destroy (widget, data)
2849 GtkWidget *widget;
2850 gpointer data;
2851{
2852 gpointer p;
ff59904a 2853 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
177c0ea7 2854
f392e843
JD
2855 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
2856 if (p) xfree (p);
2857 xg_remove_widget_from_map (id);
2858}
2859
2860/* Callback for button press/release events. Used to start timer so that
2861 the scroll bar repetition timer in GTK gets handeled.
17097258 2862 Also, sets bar->dragging to Qnil when dragging (button release) is done.
f392e843
JD
2863 WIDGET is the scroll bar widget the event is for (not used).
2864 EVENT contains the event.
17097258 2865 USER_DATA points to the struct scrollbar structure.
f392e843
JD
2866
2867 Returns FALSE to tell GTK that it shall continue propagate the event
2868 to widgets. */
71bacd48 2869
f392e843
JD
2870static gboolean
2871scroll_bar_button_cb (widget, event, user_data)
2872 GtkWidget *widget;
2873 GdkEventButton *event;
2874 gpointer user_data;
2875{
2876 if (event->type == GDK_BUTTON_PRESS && ! xg_timer)
2877 xg_start_timer ();
17097258
JD
2878 else if (event->type == GDK_BUTTON_RELEASE)
2879 {
2880 struct scroll_bar *bar = (struct scroll_bar *) user_data;
2881 if (xg_timer) xg_stop_timer ();
2882 bar->dragging = Qnil;
2883 }
2884
f392e843
JD
2885 return FALSE;
2886}
2887
2888/* Create a scroll bar widget for frame F. Store the scroll bar
2889 in BAR.
2890 SCROLL_CALLBACK is the callback to invoke when the value of the
2891 bar changes.
2892 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2893 to set resources for the widget. */
71bacd48 2894
f392e843
JD
2895void
2896xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name)
2897 FRAME_PTR f;
2898 struct scroll_bar *bar;
2899 GCallback scroll_callback;
2900 char *scroll_bar_name;
2901{
2902 GtkWidget *wscroll;
1755a397 2903 GtkWidget *webox;
f392e843
JD
2904 GtkObject *vadj;
2905 int scroll_id;
177c0ea7 2906
f392e843
JD
2907 /* Page, step increment values are not so important here, they
2908 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2909 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
2910 0.1, 0.1, 0.1);
2911
2912 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
1755a397 2913 webox = gtk_event_box_new ();
f392e843
JD
2914 gtk_widget_set_name (wscroll, scroll_bar_name);
2915 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
177c0ea7 2916
f392e843 2917 scroll_id = xg_store_widget_in_map (wscroll);
177c0ea7 2918
f979dc05 2919 g_signal_connect (G_OBJECT (wscroll),
f392e843
JD
2920 "value-changed",
2921 scroll_callback,
f979dc05 2922 (gpointer) bar);
ff59904a 2923 /* The EMACS_INT cast avoids a warning. */
f392e843
JD
2924 g_signal_connect (G_OBJECT (wscroll),
2925 "destroy",
2926 G_CALLBACK (xg_gtk_scroll_destroy),
ff59904a 2927 (gpointer) (EMACS_INT) scroll_id);
f392e843
JD
2928
2929 /* Connect to button press and button release to detect if any scroll bar
2930 has the pointer. */
2931 g_signal_connect (G_OBJECT (wscroll),
2932 "button-press-event",
2933 G_CALLBACK (scroll_bar_button_cb),
f979dc05 2934 (gpointer) bar);
f392e843
JD
2935 g_signal_connect (G_OBJECT (wscroll),
2936 "button-release-event",
2937 G_CALLBACK (scroll_bar_button_cb),
f979dc05 2938 (gpointer) bar);
177c0ea7 2939
1755a397
JD
2940 /* The scroll bar widget does not draw on a window of its own. Instead
2941 it draws on the parent window, in this case the edit widget. So
2942 whenever the edit widget is cleared, the scroll bar needs to redraw
2943 also, which causes flicker. Put an event box between the edit widget
2944 and the scroll bar, so the scroll bar instead draws itself on the
2945 event box window. */
2946 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
2947 gtk_container_add (GTK_CONTAINER (webox), wscroll);
2948
f392e843
JD
2949
2950 /* Set the cursor to an arrow. */
1755a397 2951 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
f392e843
JD
2952
2953 SET_SCROLL_BAR_X_WINDOW (bar, scroll_id);
2954}
2955
2956/* Make the scroll bar represented by SCROLLBAR_ID visible. */
71bacd48 2957
f392e843
JD
2958void
2959xg_show_scroll_bar (scrollbar_id)
2960 int scrollbar_id;
2961{
2962 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2963 if (w)
1755a397 2964 gtk_widget_show_all (gtk_widget_get_parent (w));
f392e843
JD
2965}
2966
2967/* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
71bacd48 2968
f392e843
JD
2969void
2970xg_remove_scroll_bar (f, scrollbar_id)
2971 FRAME_PTR f;
2972 int scrollbar_id;
2973{
2974 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
2975 if (w)
2976 {
1755a397 2977 GtkWidget *wparent = gtk_widget_get_parent (w);
f392e843 2978 gtk_widget_destroy (w);
1755a397 2979 gtk_widget_destroy (wparent);
f392e843
JD
2980 SET_FRAME_GARBAGED (f);
2981 }
2982}
2983
f392e843
JD
2984/* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2985 in frame F.
2986 TOP/LEFT are the new pixel positions where the bar shall appear.
2987 WIDTH, HEIGHT is the size in pixels the bar shall have. */
71bacd48 2988
f392e843 2989void
1755a397 2990xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height)
f392e843
JD
2991 FRAME_PTR f;
2992 int scrollbar_id;
2993 int top;
2994 int left;
2995 int width;
2996 int height;
2997{
f392e843 2998
49853a4d 2999 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
7863d625 3000
49853a4d
JD
3001 if (wscroll)
3002 {
cea9be54 3003 GtkWidget *wfixed = f->output_data.x->edit_widget;
1755a397 3004 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
7863d625 3005
ab2d724b 3006 /* Move and resize to new values. */
ab2d724b 3007 gtk_widget_set_size_request (wscroll, width, height);
1755a397 3008 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
17097258 3009
49853a4d
JD
3010 SET_FRAME_GARBAGED (f);
3011 cancel_mouse_face (f);
3012 }
f392e843
JD
3013}
3014
3015/* Set the thumb size and position of scroll bar BAR. We are currently
3016 displaying PORTION out of a whole WHOLE, and our position POSITION. */
71bacd48 3017
f392e843
JD
3018void
3019xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
3020 struct scroll_bar *bar;
3021 int portion, position, whole;
3022{
3023 GtkWidget *wscroll = xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar));
3024
3025 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3026
17097258 3027 if (wscroll && NILP (bar->dragging))
f392e843
JD
3028 {
3029 GtkAdjustment *adj;
3030 gdouble shown;
3031 gdouble top;
3032 int size, value;
7863d625
JD
3033 int new_step;
3034 int changed = 0;
177c0ea7 3035
f392e843
JD
3036 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3037
17097258
JD
3038 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3039 rather than the real portion value. This makes the thumb less likely
3040 to resize and that looks better. */
be786000 3041 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
17097258
JD
3042 /* When the thumb is at the bottom, position == whole.
3043 So we need to increase `whole' to make space for the thumb. */
3044 whole += portion;
3045
f392e843
JD
3046 if (whole <= 0)
3047 top = 0, shown = 1;
3048 else
3049 {
f392e843 3050 top = (gdouble) position / whole;
7863d625 3051 shown = (gdouble) portion / whole;
f392e843
JD
3052 }
3053
7863d625
JD
3054 size = shown * XG_SB_RANGE;
3055 size = min (size, XG_SB_RANGE);
f392e843
JD
3056 size = max (size, 1);
3057
7863d625
JD
3058 value = top * XG_SB_RANGE;
3059 value = min (value, XG_SB_MAX - size);
f392e843
JD
3060 value = max (value, XG_SB_MIN);
3061
7863d625 3062 /* Assume all lines are of equal size. */
be786000 3063 new_step = size / max (1, FRAME_LINES (f));
2a2071c3
JD
3064
3065 if ((int) adj->page_size != size
2a2071c3
JD
3066 || (int) adj->step_increment != new_step)
3067 {
7863d625
JD
3068 adj->page_size = size;
3069 adj->step_increment = new_step;
2a2071c3 3070 /* Assume a page increment is about 95% of the page size */
7863d625
JD
3071 adj->page_increment = (int) (0.95*adj->page_size);
3072 changed = 1;
2a2071c3 3073 }
17097258 3074
7863d625
JD
3075 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3076 {
3077 GtkWidget *wfixed = f->output_data.x->edit_widget;
17097258 3078
7863d625 3079 BLOCK_INPUT;
cea9be54 3080
7863d625
JD
3081 /* gtk_range_set_value invokes the callback. Set
3082 ignore_gtk_scrollbar to make the callback do nothing */
3083 xg_ignore_gtk_scrollbar = 1;
f392e843 3084
7863d625
JD
3085 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3086 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3087 else if (changed)
3088 gtk_adjustment_changed (adj);
3089
3090 xg_ignore_gtk_scrollbar = 0;
3091
3092 UNBLOCK_INPUT;
3093 }
3094 }
f392e843
JD
3095}
3096
3097\f
3098/***********************************************************************
3099 Tool bar functions
3100 ***********************************************************************/
3101/* The key for the data we put in the GtkImage widgets. The data is
3102 the image used by Emacs. We use this to see if we need to update
3103 the GtkImage with a new image. */
3104#define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3105
3106/* Callback function invoked when a tool bar item is pressed.
3107 W is the button widget in the tool bar that got pressed,
3108 CLIENT_DATA is an integer that is the index of the button in the
3109 tool bar. 0 is the first button. */
71bacd48 3110
f392e843
JD
3111static void
3112xg_tool_bar_callback (w, client_data)
3113 GtkWidget *w;
3114 gpointer client_data;
3115{
ff59904a
JD
3116 /* The EMACS_INT cast avoids a warning. */
3117 int idx = (int) (EMACS_INT) client_data;
f392e843
JD
3118 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3119 Lisp_Object key, frame;
3120 struct input_event event;
aa4ac494 3121 EVENT_INIT (event);
f392e843
JD
3122
3123 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3124 return;
3125
3126 idx *= TOOL_BAR_ITEM_NSLOTS;
177c0ea7 3127
f392e843
JD
3128 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3129 XSETFRAME (frame, f);
3130 event.kind = TOOL_BAR_EVENT;
3131 event.frame_or_window = frame;
3132 event.arg = frame;
3133 kbd_buffer_store_event (&event);
3134
3135 event.kind = TOOL_BAR_EVENT;
3136 event.frame_or_window = frame;
3137 event.arg = key;
3138 event.modifiers = 0; /* These are not available. */
3139 kbd_buffer_store_event (&event);
3140}
3141
3142/* This callback is called when a tool bar is detached. We must set
3143 the height of the tool bar to zero when this happens so frame sizes
3144 are correctly calculated.
3145 WBOX is the handle box widget that enables detach/attach of the tool bar.
3146 W is the tool bar widget.
3147 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 3148
f392e843
JD
3149static void
3150xg_tool_bar_detach_callback (wbox, w, client_data)
3151 GtkHandleBox *wbox;
3152 GtkWidget *w;
3153 gpointer client_data;
3154{
3155 FRAME_PTR f = (FRAME_PTR) client_data;
3156
3157 if (f)
3158 {
e0910857
JD
3159 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3160
f392e843
JD
3161 /* When detaching a tool bar, not everything dissapear. There are
3162 a few pixels left that are used to drop the tool bar back into
3163 place. */
f392e843
JD
3164 FRAME_TOOLBAR_HEIGHT (f) = 2;
3165
3166 /* The height has changed, resize outer widget and set columns
3167 rows to what we had before detaching the tool bar. */
be786000 3168 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
f392e843
JD
3169 }
3170}
3171
3172/* This callback is called when a tool bar is reattached. We must set
3173 the height of the tool bar when this happens so frame sizes
3174 are correctly calculated.
3175 WBOX is the handle box widget that enables detach/attach of the tool bar.
3176 W is the tool bar widget.
3177 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
71bacd48 3178
f392e843
JD
3179static void
3180xg_tool_bar_attach_callback (wbox, w, client_data)
3181 GtkHandleBox *wbox;
3182 GtkWidget *w;
3183 gpointer client_data;
3184{
3185 FRAME_PTR f = (FRAME_PTR) client_data;
3186
3187 if (f)
3188 {
3189 GtkRequisition req;
3190
e0910857
JD
3191 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3192
f392e843
JD
3193 gtk_widget_size_request (w, &req);
3194 FRAME_TOOLBAR_HEIGHT (f) = req.height;
3195
3196 /* The height has changed, resize outer widget and set columns
e0910857 3197 rows to what we had before attaching the tool bar. */
be786000 3198 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
f392e843
JD
3199 }
3200}
3201
3202/* This callback is called when the mouse enters or leaves a tool bar item.
3203 It is used for displaying and hiding the help text.
3204 W is the tool bar item, a button.
3205 EVENT is either an enter event or leave event.
3206 CLIENT_DATA is an integer that is the index of the button in the
3207 tool bar. 0 is the first button.
3208
3209 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 3210
f392e843
JD
3211static gboolean
3212xg_tool_bar_help_callback (w, event, client_data)
3213 GtkWidget *w;
3214 GdkEventCrossing *event;
3215 gpointer client_data;
3216{
ff59904a
JD
3217 /* The EMACS_INT cast avoids a warning. */
3218 int idx = (int) (EMACS_INT) client_data;
f392e843
JD
3219 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3220 Lisp_Object help, frame;
3221
3222 if (! GTK_IS_BUTTON (w))
3223 {
3224 return FALSE;
3225 }
3226
3227 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
d9301435 3228 return FALSE;
f392e843
JD
3229
3230 if (event->type == GDK_ENTER_NOTIFY)
3231 {
3232 idx *= TOOL_BAR_ITEM_NSLOTS;
3233 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3234
3235 if (NILP (help))
3236 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3237 }
3238 else
3239 help = Qnil;
3240
3241 XSETFRAME (frame, f);
3242 kbd_buffer_store_help_event (frame, help);
3243
3244 return FALSE;
3245}
3246
3247
f098b121
JD
3248/* This callback is called when a tool bar item shall be redrawn.
3249 It modifies the expose event so that the GtkImage widget redraws the
3250 whole image. This to overcome a bug that makes GtkImage draw the image
3251 in the wrong place when it tries to redraw just a part of the image.
3252 W is the GtkImage to be redrawn.
3253 EVENT is the expose event for W.
3254 CLIENT_DATA is unused.
3255
3256 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 3257
f098b121 3258static gboolean
3a8a22fc 3259xg_tool_bar_item_expose_callback (w, event, client_data)
f098b121
JD
3260 GtkWidget *w;
3261 GdkEventExpose *event;
3262 gpointer client_data;
3263{
b676f356
JD
3264 gint width, height;
3265
3266 gdk_drawable_get_size (event->window, &width, &height);
3267
3268 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3269 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3270
810f2256
JD
3271 event->area.x = max (0, event->area.x);
3272 event->area.y = max (0, event->area.y);
b676f356
JD
3273
3274 event->area.width = max (width, event->area.width);
3275 event->area.height = max (height, event->area.height);
3276
f098b121
JD
3277 return FALSE;
3278}
3279
3a8a22fc
JD
3280/* This callback is called when a tool bar shall be redrawn.
3281 We need to update the tool bar from here in case the image cache
3282 has deleted the pixmaps used in the tool bar.
3283 W is the GtkToolbar to be redrawn.
3284 EVENT is the expose event for W.
3285 CLIENT_DATA is pointing to the frame for this tool bar.
3286
3287 Returns FALSE to tell GTK to keep processing this event. */
71bacd48 3288
3a8a22fc
JD
3289static gboolean
3290xg_tool_bar_expose_callback (w, event, client_data)
3291 GtkWidget *w;
3292 GdkEventExpose *event;
3293 gpointer client_data;
3294{
810f2256 3295 update_frame_tool_bar ((FRAME_PTR) client_data);
3a8a22fc
JD
3296 return FALSE;
3297}
3298
71bacd48
JD
3299/* Create a tool bar for frame F. */
3300
f392e843
JD
3301static void
3302xg_create_tool_bar (f)
3303 FRAME_PTR f;
3304{
3305 struct x_output *x = f->output_data.x;
3306 GtkRequisition req;
3307 int vbox_pos = x->menubar_widget ? 1 : 0;
3308
3309 x->toolbar_widget = gtk_toolbar_new ();
3310 x->handlebox_widget = gtk_handle_box_new ();
e0910857
JD
3311 x->toolbar_detached = 0;
3312
f392e843
JD
3313 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3314 x->toolbar_widget);
177c0ea7 3315
f392e843
JD
3316 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3317 FALSE, FALSE, 0);
177c0ea7 3318
f392e843
JD
3319 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3320 vbox_pos);
3321
f098b121
JD
3322 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3323
3324 /* We only have icons, so override any user setting. We could
3325 use the caption property of the toolbar item (see update_frame_tool_bar
3326 below), but some of those strings are long, making the toolbar so
3327 long it does not fit on the screen. The GtkToolbar widget makes every
3328 item equal size, so the longest caption determine the size of every
3329 tool bar item. I think the creators of the GtkToolbar widget
3330 counted on 4 or 5 character long strings. */
3331 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3332 gtk_toolbar_set_orientation (GTK_TOOLBAR (x->toolbar_widget),
3333 GTK_ORIENTATION_HORIZONTAL);
3334
f392e843
JD
3335 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3336 G_CALLBACK (xg_tool_bar_detach_callback), f);
3337 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3338 G_CALLBACK (xg_tool_bar_attach_callback), f);
3a8a22fc
JD
3339 g_signal_connect (G_OBJECT (x->toolbar_widget),
3340 "expose-event",
3341 G_CALLBACK (xg_tool_bar_expose_callback),
3342 f);
f392e843
JD
3343
3344 gtk_widget_show_all (x->handlebox_widget);
3345
3346 gtk_widget_size_request (x->toolbar_widget, &req);
3347 FRAME_TOOLBAR_HEIGHT (f) = req.height;
177c0ea7 3348
f392e843
JD
3349 /* The height has changed, resize outer widget and set columns
3350 rows to what we had before adding the tool bar. */
be786000 3351 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
177c0ea7 3352
f392e843
JD
3353 SET_FRAME_GARBAGED (f);
3354}
3355
71bacd48
JD
3356/* Update the tool bar for frame F. Add new buttons and remove old. */
3357
f392e843
JD
3358void
3359update_frame_tool_bar (f)
3360 FRAME_PTR f;
3361{
3362 int i;
3363 GtkRequisition old_req, new_req;
3364 GList *icon_list;
49853a4d 3365 GList *iter;
f392e843 3366 struct x_output *x = f->output_data.x;
133c0116 3367 int hmargin, vmargin;
f392e843
JD
3368
3369 if (! FRAME_GTK_WIDGET (f))
3370 return;
3371
3372 BLOCK_INPUT;
177c0ea7 3373
133c0116
JD
3374 if (INTEGERP (Vtool_bar_button_margin)
3375 && XINT (Vtool_bar_button_margin) > 0)
3376 {
3377 hmargin = XFASTINT (Vtool_bar_button_margin);
3378 vmargin = XFASTINT (Vtool_bar_button_margin);
3379 }
3380 else if (CONSP (Vtool_bar_button_margin))
3381 {
3382 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3383 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
3384 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
3385
3386 if (INTEGERP (XCDR (Vtool_bar_button_margin))
3387 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
3388 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
3389 }
3390
3391 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
3392 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
3393 i.e. zero. This means that margins less than
3394 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
3395 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3396 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
3397
f392e843
JD
3398 if (! x->toolbar_widget)
3399 xg_create_tool_bar (f);
3400
3401 gtk_widget_size_request (x->toolbar_widget, &old_req);
3402
3403 icon_list = gtk_container_get_children (GTK_CONTAINER (x->toolbar_widget));
49853a4d 3404 iter = icon_list;
177c0ea7 3405
f392e843
JD
3406 for (i = 0; i < f->n_tool_bar_items; ++i)
3407 {
3408#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3409
3410 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
3411 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
3412 int idx;
3413 int img_id;
3414 struct image *img;
3415 Lisp_Object image;
49853a4d 3416 GtkWidget *wicon = iter ? GTK_WIDGET (iter->data) : 0;
26b74a0b 3417
49853a4d 3418 if (iter) iter = g_list_next (iter);
f392e843
JD
3419
3420 /* If image is a vector, choose the image according to the
3421 button state. */
3422 image = PROP (TOOL_BAR_ITEM_IMAGES);
3423 if (VECTORP (image))
3424 {
3425 if (enabled_p)
3426 idx = (selected_p
3427 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
3428 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
3429 else
3430 idx = (selected_p
3431 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
3432 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
3433
3434 xassert (ASIZE (image) >= idx);
3435 image = AREF (image, idx);
3436 }
3437 else
3438 idx = -1;
3439
3440 /* Ignore invalid image specifications. */
3441 if (!valid_image_p (image))
3442 {
3443 if (wicon) gtk_widget_hide (wicon);
3444 continue;
3445 }
3446
3447 img_id = lookup_image (f, image);
3448 img = IMAGE_FROM_ID (f, img_id);
1d1885fc 3449 prepare_image_for_display (f, img);
f392e843 3450
1d1885fc
JD
3451 if (img->load_failed_p || img->pixmap == None)
3452 {
3453 if (wicon) gtk_widget_hide (wicon);
3454 continue;
3455 }
177c0ea7 3456
f392e843
JD
3457 if (! wicon)
3458 {
5b166323 3459 GtkWidget *w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
f392e843 3460
133c0116
JD
3461 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
3462
ff59904a 3463 /* The EMACS_INT cast avoids a warning. */
f392e843
JD
3464 gtk_toolbar_append_item (GTK_TOOLBAR (x->toolbar_widget),
3465 0, 0, 0,
3466 w,
3467 GTK_SIGNAL_FUNC (xg_tool_bar_callback),
ff59904a 3468 (gpointer) (EMACS_INT) i);
177c0ea7 3469
f392e843
JD
3470 /* Save the image so we can see if an update is needed when
3471 this function is called again. */
3472 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
26b74a0b 3473 (gpointer)img->pixmap);
f392e843 3474
f098b121 3475 /* Catch expose events to overcome an annoying redraw bug, see
3a8a22fc 3476 comment for xg_tool_bar_item_expose_callback. */
f098b121
JD
3477 g_signal_connect (G_OBJECT (w),
3478 "expose-event",
3a8a22fc 3479 G_CALLBACK (xg_tool_bar_item_expose_callback),
f098b121
JD
3480 0);
3481
f392e843
JD
3482 /* We must set sensitive on the button that is the parent
3483 of the GtkImage parent. Go upwards until we find the button. */
3484 while (! GTK_IS_BUTTON (w))
3485 w = gtk_widget_get_parent (w);
177c0ea7 3486
f392e843
JD
3487 if (w)
3488 {
3489 /* Save the frame in the button so the xg_tool_bar_callback
3490 can get at it. */
3491 g_object_set_data (G_OBJECT (w), XG_FRAME_DATA, (gpointer)f);
3492 gtk_widget_set_sensitive (w, enabled_p);
3493
3494 /* Use enter/leave notify to show help. We use the events
3495 rather than the GtkButton specific signals "enter" and
3496 "leave", so we can have only one callback. The event
3497 will tell us what kind of event it is. */
ff59904a 3498 /* The EMACS_INT cast avoids a warning. */
f392e843
JD
3499 g_signal_connect (G_OBJECT (w),
3500 "enter-notify-event",
3501 G_CALLBACK (xg_tool_bar_help_callback),
ff59904a 3502 (gpointer) (EMACS_INT) i);
f392e843
JD
3503 g_signal_connect (G_OBJECT (w),
3504 "leave-notify-event",
3505 G_CALLBACK (xg_tool_bar_help_callback),
ff59904a 3506 (gpointer) (EMACS_INT) i);
f392e843
JD
3507 }
3508 }
3509 else
3510 {
3511 /* The child of the tool bar is a button. Inside that button
3512 is a vbox. Inside that vbox is the GtkImage. */
3513 GtkWidget *wvbox = gtk_bin_get_child (GTK_BIN (wicon));
49853a4d
JD
3514 GList *chlist = gtk_container_get_children (GTK_CONTAINER (wvbox));
3515 GtkImage *wimage = GTK_IMAGE (chlist->data);
26b74a0b
JD
3516 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
3517 XG_TOOL_BAR_IMAGE_DATA);
49853a4d 3518 g_list_free (chlist);
f392e843 3519
133c0116
JD
3520 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
3521
26b74a0b 3522 if (old_img != img->pixmap)
5b166323 3523 (void) xg_get_image_for_pixmap (f, img, x->widget, wimage);
f392e843
JD
3524
3525 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
26b74a0b 3526 (gpointer)img->pixmap);
f392e843
JD
3527
3528 gtk_widget_set_sensitive (wicon, enabled_p);
3529 gtk_widget_show (wicon);
3530 }
177c0ea7 3531
f392e843
JD
3532#undef PROP
3533 }
3534
3535 /* Remove buttons not longer needed. We just hide them so they
3536 can be reused later on. */
49853a4d 3537 while (iter)
f392e843 3538 {
49853a4d 3539 GtkWidget *w = GTK_WIDGET (iter->data);
f392e843 3540 gtk_widget_hide (w);
49853a4d 3541 iter = g_list_next (iter);
f392e843
JD
3542 }
3543
3544 gtk_widget_size_request (x->toolbar_widget, &new_req);
e0910857
JD
3545 if (old_req.height != new_req.height
3546 && ! FRAME_X_OUTPUT (f)->toolbar_detached)
f392e843
JD
3547 {
3548 FRAME_TOOLBAR_HEIGHT (f) = new_req.height;
be786000 3549 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
f392e843
JD
3550 }
3551
49853a4d
JD
3552 if (icon_list) g_list_free (icon_list);
3553
f392e843
JD
3554 UNBLOCK_INPUT;
3555}
3556
71bacd48
JD
3557/* Deallocate all resources for the tool bar on frame F.
3558 Remove the tool bar. */
3559
f392e843
JD
3560void
3561free_frame_tool_bar (f)
3562 FRAME_PTR f;
3563{
3564 struct x_output *x = f->output_data.x;
3565
3566 if (x->toolbar_widget)
3567 {
f392e843
JD
3568 BLOCK_INPUT;
3569 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
3570 x->handlebox_widget);
3571 x->toolbar_widget = 0;
3572 x->handlebox_widget = 0;
3573 FRAME_TOOLBAR_HEIGHT (f) = 0;
3574
3575 /* The height has changed, resize outer widget and set columns
3576 rows to what we had before removing the tool bar. */
be786000 3577 xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f));
f392e843
JD
3578
3579 SET_FRAME_GARBAGED (f);
3580 UNBLOCK_INPUT;
3581 }
3582}
3583
3584
3585\f
3586/***********************************************************************
3587 Initializing
3588 ***********************************************************************/
3589void
3590xg_initialize ()
3591{
9f6fcdc5
JD
3592 GtkBindingSet *binding_set;
3593
f392e843 3594 xg_ignore_gtk_scrollbar = 0;
da18b5ac 3595 xg_detached_menus = 0;
f392e843
JD
3596 xg_menu_cb_list.prev = xg_menu_cb_list.next =
3597 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
3598
81e302ef
JD
3599 id_to_widget.max_size = id_to_widget.used = 0;
3600 id_to_widget.widgets = 0;
3601
f392e843
JD
3602 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
3603 bindings. It doesn't seem to be any way to remove properties,
3604 so we set it to VoidSymbol which in X means "no key". */
3605 gtk_settings_set_string_property (gtk_settings_get_default (),
3606 "gtk-menu-bar-accel",
3607 "VoidSymbol",
3608 EMACS_CLASS);
81e302ef
JD
3609
3610 /* Make GTK text input widgets use Emacs style keybindings. This is
3611 Emacs after all. */
3612 gtk_settings_set_string_property (gtk_settings_get_default (),
3613 "gtk-key-theme-name",
3614 "Emacs",
3615 EMACS_CLASS);
9f6fcdc5
JD
3616
3617 /* Make dialogs close on C-g. Since file dialog inherits from
3618 dialog, this works for them also. */
3619 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_DIALOG));
3620 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3621 "close", 0);
3622
3623 /* Make menus close on C-g. */
3624 binding_set = gtk_binding_set_by_class (gtk_type_class (GTK_TYPE_MENU_SHELL));
3625 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
3626 "cancel", 0);
f392e843
JD
3627}
3628
3629#endif /* USE_GTK */
ab5796a9
MB
3630
3631/* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
3632 (do not change this comment) */