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