Add ability to put Gtk+ tool bar on the left/right/bottom or top. Default top.
[bpt/emacs.git] / src / gtkutil.c
1 /* Functions for creating and updating GTK widgets.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21
22 #ifdef USE_GTK
23 #include <string.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <setjmp.h>
27 #include "lisp.h"
28 #include "xterm.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
31 #include "window.h"
32 #include "gtkutil.h"
33 #include "termhooks.h"
34 #include "keyboard.h"
35 #include "charset.h"
36 #include "coding.h"
37 #include <gdk/gdkkeysyms.h>
38 #include "xsettings.h"
39
40 #ifdef HAVE_XFT
41 #include <X11/Xft/Xft.h>
42 #endif
43
44 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
45 (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
46
47 #define FRAME_TOTAL_PIXEL_WIDTH(f) \
48 (FRAME_PIXEL_WIDTH (f) + FRAME_TOOLBAR_WIDTH (f))
49
50 /* Avoid "differ in sign" warnings */
51 #define SSDATA(x) ((char *) SDATA (x))
52
53 #ifndef HAVE_GTK_WIDGET_SET_HAS_WINDOW
54 #define gtk_widget_set_has_window(w, b) \
55 (gtk_fixed_set_has_window (GTK_FIXED (w), b))
56 #endif
57 #ifndef HAVE_GTK_DIALOG_GET_ACTION_AREA
58 #define gtk_dialog_get_action_area(w) ((w)->action_area)
59 #define gtk_dialog_get_content_area(w) ((w)->vbox)
60 #endif
61 #ifndef HAVE_GTK_WIDGET_GET_SENSITIVE
62 #define gtk_widget_get_sensitive(w) (GTK_WIDGET_SENSITIVE (w))
63 #endif
64 #ifndef HAVE_GTK_ADJUSTMENT_GET_PAGE_SIZE
65 #define gtk_adjustment_set_page_size(w, s) ((w)->page_size = (s))
66 #define gtk_adjustment_set_page_increment(w, s) ((w)->page_increment = (s))
67 #define gtk_adjustment_get_step_increment(w) ((w)->step_increment)
68 #define gtk_adjustment_set_step_increment(w, s) ((w)->step_increment = (s))
69 #endif
70 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
71 #define remove_submenu(w) gtk_menu_item_set_submenu ((w), NULL)
72 #else
73 #define remove_submenu(w) gtk_menu_item_remove_submenu ((w))
74 #endif
75
76 \f
77 /***********************************************************************
78 Display handling functions
79 ***********************************************************************/
80
81 /* Keep track of the default display, or NULL if there is none. Emacs
82 may close all its displays. */
83
84 static GdkDisplay *gdpy_def;
85
86 /* When the GTK widget W is to be created on a display for F that
87 is not the default display, set the display for W.
88 W can be a GtkMenu or a GtkWindow widget. */
89
90 static void
91 xg_set_screen (GtkWidget *w, FRAME_PTR f)
92 {
93 if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
94 {
95 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
96 GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);
97
98 if (GTK_IS_MENU (w))
99 gtk_menu_set_screen (GTK_MENU (w), gscreen);
100 else
101 gtk_window_set_screen (GTK_WINDOW (w), gscreen);
102 }
103 }
104
105
106 /* Open a display named by DISPLAY_NAME. The display is returned in *DPY.
107 *DPY is set to NULL if the display can't be opened.
108
109 Returns non-zero if display could be opened, zero if display could not
110 be opened, and less than zero if the GTK version doesn't support
111 multipe displays. */
112
113 void
114 xg_display_open (char *display_name, Display **dpy)
115 {
116 GdkDisplay *gdpy;
117
118 gdpy = gdk_display_open (display_name);
119 if (!gdpy_def && gdpy)
120 {
121 gdpy_def = gdpy;
122 gdk_display_manager_set_default_display (gdk_display_manager_get (),
123 gdpy);
124 }
125
126 *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
127 }
128
129
130 /* Close display DPY. */
131
132 void
133 xg_display_close (Display *dpy)
134 {
135 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
136
137 /* If this is the default display, try to change it before closing.
138 If there is no other display to use, gdpy_def is set to NULL, and
139 the next call to xg_display_open resets the default display. */
140 if (gdk_display_get_default () == gdpy)
141 {
142 struct x_display_info *dpyinfo;
143 GdkDisplay *gdpy_new = NULL;
144
145 /* Find another display. */
146 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
147 if (dpyinfo->display != dpy)
148 {
149 gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
150 gdk_display_manager_set_default_display (gdk_display_manager_get (),
151 gdpy_new);
152 break;
153 }
154 gdpy_def = gdpy_new;
155 }
156
157 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
158 /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
159 http://bugzilla.gnome.org/show_bug.cgi?id=85715). This way we
160 can continue running, but there will be memory leaks. */
161 g_object_run_dispose (G_OBJECT (gdpy));
162 #else
163 /* This seems to be fixed in GTK 2.10. */
164 gdk_display_close (gdpy);
165 #endif
166 }
167
168 \f
169 /***********************************************************************
170 Utility functions
171 ***********************************************************************/
172 /* The next two variables and functions are taken from lwlib. */
173 static widget_value *widget_value_free_list;
174 static int malloc_cpt;
175
176 /* Allocate a widget_value structure, either by taking one from the
177 widget_value_free_list or by malloc:ing a new one.
178
179 Return a pointer to the allocated structure. */
180
181 widget_value *
182 malloc_widget_value (void)
183 {
184 widget_value *wv;
185 if (widget_value_free_list)
186 {
187 wv = widget_value_free_list;
188 widget_value_free_list = wv->free_list;
189 wv->free_list = 0;
190 }
191 else
192 {
193 wv = (widget_value *) xmalloc (sizeof (widget_value));
194 malloc_cpt++;
195 }
196 memset (wv, 0, sizeof (widget_value));
197 return wv;
198 }
199
200 /* This is analogous to free. It frees only what was allocated
201 by malloc_widget_value, and no substructures. */
202
203 void
204 free_widget_value (widget_value *wv)
205 {
206 if (wv->free_list)
207 abort ();
208
209 if (malloc_cpt > 25)
210 {
211 /* When the number of already allocated cells is too big,
212 We free it. */
213 xfree (wv);
214 malloc_cpt--;
215 }
216 else
217 {
218 wv->free_list = widget_value_free_list;
219 widget_value_free_list = wv;
220 }
221 }
222
223
224 /* Create and return the cursor to be used for popup menus and
225 scroll bars on display DPY. */
226
227 GdkCursor *
228 xg_create_default_cursor (Display *dpy)
229 {
230 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
231 return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
232 }
233
234 /* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
235
236 static GdkPixbuf *
237 xg_get_pixbuf_from_pix_and_mask (GdkPixmap *gpix,
238 GdkPixmap *gmask,
239 GdkColormap *cmap)
240 {
241 int width, height;
242 GdkPixbuf *icon_buf, *tmp_buf;
243
244 gdk_drawable_get_size (gpix, &width, &height);
245 tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
246 0, 0, 0, 0, width, height);
247 icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
248 g_object_unref (G_OBJECT (tmp_buf));
249
250 if (gmask)
251 {
252 GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
253 gmask,
254 NULL,
255 0, 0, 0, 0,
256 width, height);
257 guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
258 guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
259 int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
260 int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
261 int y;
262
263 for (y = 0; y < height; ++y)
264 {
265 guchar *iconptr, *maskptr;
266 int x;
267
268 iconptr = pixels + y * rowstride;
269 maskptr = mask_pixels + y * mask_rowstride;
270
271 for (x = 0; x < width; ++x)
272 {
273 /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
274 just R is sufficient. */
275 if (maskptr[0] == 0)
276 iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
277
278 iconptr += rowstride/width;
279 maskptr += mask_rowstride/width;
280 }
281 }
282
283 g_object_unref (G_OBJECT (mask_buf));
284 }
285
286 return icon_buf;
287 }
288
289 static Lisp_Object
290 file_for_image (Lisp_Object image)
291 {
292 Lisp_Object specified_file = Qnil;
293 Lisp_Object tail;
294 extern Lisp_Object QCfile;
295
296 for (tail = XCDR (image);
297 NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
298 tail = XCDR (XCDR (tail)))
299 if (EQ (XCAR (tail), QCfile))
300 specified_file = XCAR (XCDR (tail));
301
302 return specified_file;
303 }
304
305 /* For the image defined in IMG, make and return a GtkImage. For displays with
306 8 planes or less we must make a GdkPixbuf and apply the mask manually.
307 Otherwise the highlightning and dimming the tool bar code in GTK does
308 will look bad. For display with more than 8 planes we just use the
309 pixmap and mask directly. For monochrome displays, GTK doesn't seem
310 able to use external pixmaps, it looks bad whatever we do.
311 The image is defined on the display where frame F is.
312 WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
313 If OLD_WIDGET is NULL, a new widget is constructed and returned.
314 If OLD_WIDGET is not NULL, that widget is modified. */
315
316 static GtkWidget *
317 xg_get_image_for_pixmap (FRAME_PTR f,
318 struct image *img,
319 GtkWidget *widget,
320 GtkImage *old_widget)
321 {
322 GdkPixmap *gpix;
323 GdkPixmap *gmask;
324 GdkDisplay *gdpy;
325 GdkColormap *cmap;
326 GdkPixbuf *icon_buf;
327
328 /* If we have a file, let GTK do all the image handling.
329 This seems to be the only way to make insensitive and activated icons
330 look good in all cases. */
331 Lisp_Object specified_file = file_for_image (img->spec);
332 Lisp_Object file;
333
334 /* We already loaded the image once before calling this
335 function, so this only fails if the image file has been removed.
336 In that case, use the pixmap already loaded. */
337
338 if (STRINGP (specified_file)
339 && STRINGP (file = x_find_image_file (specified_file)))
340 {
341 if (! old_widget)
342 old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
343 else
344 gtk_image_set_from_file (old_widget, SSDATA (file));
345
346 return GTK_WIDGET (old_widget);
347 }
348
349 /* No file, do the image handling ourselves. This will look very bad
350 on a monochrome display, and sometimes bad on all displays with
351 certain themes. */
352
353 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
354 gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
355 gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
356
357 /* This is a workaround to make icons look good on pseudo color
358 displays. Apparently GTK expects the images to have an alpha
359 channel. If they don't, insensitive and activated icons will
360 look bad. This workaround does not work on monochrome displays,
361 and is strictly not needed on true color/static color displays (i.e.
362 16 bits and higher). But we do it anyway so we get a pixbuf that is
363 not associated with the img->pixmap. The img->pixmap may be removed
364 by clearing the image cache and then the tool bar redraw fails, since
365 Gtk+ assumes the pixmap is always there. */
366 cmap = gtk_widget_get_colormap (widget);
367 icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
368
369 if (! old_widget)
370 old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
371 else
372 gtk_image_set_from_pixbuf (old_widget, icon_buf);
373
374 g_object_unref (G_OBJECT (icon_buf));
375
376 g_object_unref (G_OBJECT (gpix));
377 if (gmask) g_object_unref (G_OBJECT (gmask));
378
379 return GTK_WIDGET (old_widget);
380 }
381
382
383 /* Set CURSOR on W and all widgets W contain. We must do like this
384 for scroll bars and menu because they create widgets internally,
385 and it is those widgets that are visible. */
386
387 static void
388 xg_set_cursor (GtkWidget *w, GdkCursor *cursor)
389 {
390 GdkWindow *window = gtk_widget_get_window(w);
391 GList *children = gdk_window_peek_children (window);
392
393 gdk_window_set_cursor (window, cursor);
394
395 /* The scroll bar widget has more than one GDK window (had to look at
396 the source to figure this out), and there is no way to set cursor
397 on widgets in GTK. So we must set the cursor for all GDK windows.
398 Ditto for menus. */
399
400 for ( ; children; children = g_list_next (children))
401 gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
402 }
403
404 /* Insert NODE into linked LIST. */
405
406 static void
407 xg_list_insert (xg_list_node *list, xg_list_node *node)
408 {
409 xg_list_node *list_start = list->next;
410
411 if (list_start) list_start->prev = node;
412 node->next = list_start;
413 node->prev = 0;
414 list->next = node;
415 }
416
417 /* Remove NODE from linked LIST. */
418
419 static void
420 xg_list_remove (xg_list_node *list, xg_list_node *node)
421 {
422 xg_list_node *list_start = list->next;
423 if (node == list_start)
424 {
425 list->next = node->next;
426 if (list->next) list->next->prev = 0;
427 }
428 else
429 {
430 node->prev->next = node->next;
431 if (node->next) node->next->prev = node->prev;
432 }
433 }
434
435 /* Allocate and return a utf8 version of STR. If STR is already
436 utf8 or NULL, just return STR.
437 If not, a new string is allocated and the caller must free the result
438 with g_free. */
439
440 static char *
441 get_utf8_string (char *str)
442 {
443 char *utf8_str = str;
444
445 if (!str) return NULL;
446
447 /* If not UTF-8, try current locale. */
448 if (!g_utf8_validate (str, -1, NULL))
449 utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);
450
451 if (!utf8_str)
452 {
453 /* Probably some control characters in str. Escape them. */
454 size_t nr_bad = 0;
455 gsize bytes_read;
456 gsize bytes_written;
457 unsigned char *p = (unsigned char *)str;
458 char *cp, *up;
459 GError *error = NULL;
460
461 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
462 &bytes_written, &error))
463 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
464 {
465 ++nr_bad;
466 p += bytes_written+1;
467 g_error_free (error);
468 error = NULL;
469 }
470
471 if (error)
472 {
473 g_error_free (error);
474 error = NULL;
475 }
476 if (cp) g_free (cp);
477
478 up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
479 p = (unsigned char *)str;
480
481 while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
482 &bytes_written, &error))
483 && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
484 {
485 strncpy (up, (char *)p, bytes_written);
486 sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
487 up[bytes_written+4] = '\0';
488 up += bytes_written+4;
489 p += bytes_written+1;
490 g_error_free (error);
491 error = NULL;
492 }
493
494 if (cp)
495 {
496 strcat (utf8_str, cp);
497 g_free (cp);
498 }
499 if (error)
500 {
501 g_error_free (error);
502 error = NULL;
503 }
504 }
505 return utf8_str;
506 }
507
508
509 \f
510 /***********************************************************************
511 General functions for creating widgets, resizing, events, e.t.c.
512 ***********************************************************************/
513
514 /* Make a geometry string and pass that to GTK. It seems this is the
515 only way to get geometry position right if the user explicitly
516 asked for a position when starting Emacs.
517 F is the frame we shall set geometry for. */
518
519 static void
520 xg_set_geometry (FRAME_PTR f)
521 {
522 if (f->size_hint_flags & (USPosition | PPosition))
523 {
524 int left = f->left_pos;
525 int xneg = f->size_hint_flags & XNegative;
526 int top = f->top_pos;
527 int yneg = f->size_hint_flags & YNegative;
528 char geom_str[32];
529
530 if (xneg)
531 left = -left;
532 if (yneg)
533 top = -top;
534
535 sprintf (geom_str, "=%dx%d%c%d%c%d",
536 FRAME_PIXEL_WIDTH (f),
537 FRAME_PIXEL_HEIGHT (f),
538 (xneg ? '-' : '+'), left,
539 (yneg ? '-' : '+'), top);
540
541 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
542 geom_str))
543 fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
544 }
545 }
546
547 /* Clear under internal border if any. As we use a mix of Gtk+ and X calls
548 and use a GtkFixed widget, this doesn't happen automatically. */
549
550 static void
551 xg_clear_under_internal_border (FRAME_PTR f)
552 {
553 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
554 {
555 GtkWidget *wfixed = f->output_data.x->edit_widget;
556 gtk_widget_queue_draw (wfixed);
557 gdk_window_process_all_updates ();
558 x_clear_area (FRAME_X_DISPLAY (f),
559 FRAME_X_WINDOW (f),
560 0, 0,
561 FRAME_PIXEL_WIDTH (f),
562 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
563 x_clear_area (FRAME_X_DISPLAY (f),
564 FRAME_X_WINDOW (f),
565 0, 0,
566 FRAME_INTERNAL_BORDER_WIDTH (f),
567 FRAME_PIXEL_HEIGHT (f), 0);
568 x_clear_area (FRAME_X_DISPLAY (f),
569 FRAME_X_WINDOW (f),
570 0, FRAME_PIXEL_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
571 FRAME_PIXEL_WIDTH (f),
572 FRAME_INTERNAL_BORDER_WIDTH (f), 0);
573 x_clear_area (FRAME_X_DISPLAY (f),
574 FRAME_X_WINDOW (f),
575 FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f),
576 0,
577 FRAME_INTERNAL_BORDER_WIDTH (f),
578 FRAME_PIXEL_HEIGHT (f), 0);
579 }
580 }
581
582 /* Function to handle resize of our frame. As we have a Gtk+ tool bar
583 and a Gtk+ menu bar, we get resize events for the edit part of the
584 frame only. We let Gtk+ deal with the Gtk+ parts.
585 F is the frame to resize.
586 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
587
588 void
589 xg_frame_resized (FRAME_PTR f, int pixelwidth, int pixelheight)
590 {
591 int rows, columns;
592
593 if (pixelwidth == -1 && pixelheight == -1)
594 {
595 if (FRAME_GTK_WIDGET (f) && gtk_widget_get_mapped (FRAME_GTK_WIDGET (f)))
596 gdk_window_get_geometry (gtk_widget_get_window (FRAME_GTK_WIDGET (f)),
597 0, 0,
598 &pixelwidth, &pixelheight, 0);
599 else return;
600 }
601
602
603 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
604 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
605
606 if (columns != FRAME_COLS (f)
607 || rows != FRAME_LINES (f)
608 || pixelwidth != FRAME_PIXEL_WIDTH (f)
609 || pixelheight != FRAME_PIXEL_HEIGHT (f))
610 {
611 FRAME_PIXEL_WIDTH (f) = pixelwidth;
612 FRAME_PIXEL_HEIGHT (f) = pixelheight;
613
614 xg_clear_under_internal_border (f);
615 change_frame_size (f, rows, columns, 0, 1, 0);
616 SET_FRAME_GARBAGED (f);
617 cancel_mouse_face (f);
618 }
619 }
620
621 /* Resize the outer window of frame F after chainging the height.
622 COLUMNS/ROWS is the size the edit area shall have after the resize. */
623
624 void
625 xg_frame_set_char_size (FRAME_PTR f, int cols, int rows)
626 {
627 int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
628 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
629 int pixelwidth;
630
631 if (FRAME_PIXEL_HEIGHT (f) == 0)
632 return;
633
634 /* Take into account the size of the scroll bar. Always use the
635 number of columns occupied by the scroll bar here otherwise we
636 might end up with a frame width that is not a multiple of the
637 frame's character width which is bad for vertically split
638 windows. */
639 f->scroll_bar_actual_width
640 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
641
642 compute_fringe_widths (f, 0);
643
644 /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
645 after calculating that value. */
646 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
647 + FRAME_TOOLBAR_WIDTH (f);
648
649
650 /* Do this before resize, as we don't know yet if we will be resized. */
651 xg_clear_under_internal_border (f);
652
653 /* Must resize our top level widget. Font size may have changed,
654 but not rows/cols. */
655 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
656 pixelwidth, pixelheight);
657 x_wm_set_size_hint (f, 0, 0);
658
659 SET_FRAME_GARBAGED (f);
660 cancel_mouse_face (f);
661
662 /* We can not call change_frame_size for a mapped frame,
663 we can not set pixel width/height either. The window manager may
664 override our resize request, XMonad does this all the time.
665 The best we can do is try to sync, so lisp code sees the updated
666 size as fast as possible.
667 For unmapped windows, we can set rows/cols. When
668 the frame is mapped again we will (hopefully) get the correct size. */
669 if (f->async_visible)
670 {
671 /* Must call this to flush out events */
672 (void)gtk_events_pending ();
673 gdk_flush ();
674 x_wait_for_event (f, ConfigureNotify);
675 }
676 else
677 {
678 change_frame_size (f, rows, cols, 0, 1, 0);
679 FRAME_PIXEL_WIDTH (f) = pixelwidth;
680 FRAME_PIXEL_HEIGHT (f) = pixelheight;
681 }
682 }
683
684 /* Handle height/width changes (i.e. add/remove/move menu/toolbar).
685 The policy is to keep the number of editable lines. */
686
687 static void
688 xg_height_or_width_changed (FRAME_PTR f)
689 {
690 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
691 FRAME_TOTAL_PIXEL_WIDTH (f),
692 FRAME_TOTAL_PIXEL_HEIGHT (f));
693 f->output_data.x->hint_flags = 0;
694 x_wm_set_size_hint (f, 0, 0);
695 }
696
697 /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
698 Must be done like this, because GtkWidget:s can have "hidden"
699 X Window that aren't accessible.
700
701 Return 0 if no widget match WDESC. */
702
703 GtkWidget *
704 xg_win_to_widget (Display *dpy, Window wdesc)
705 {
706 gpointer gdkwin;
707 GtkWidget *gwdesc = 0;
708
709 BLOCK_INPUT;
710
711 gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
712 wdesc);
713 if (gdkwin)
714 {
715 GdkEvent event;
716 event.any.window = gdkwin;
717 gwdesc = gtk_get_event_widget (&event);
718 }
719
720 UNBLOCK_INPUT;
721 return gwdesc;
722 }
723
724 /* Fill in the GdkColor C so that it represents PIXEL.
725 W is the widget that color will be used for. Used to find colormap. */
726
727 static void
728 xg_pix_to_gcolor (GtkWidget *w, long unsigned int pixel, GdkColor *c)
729 {
730 GdkColormap *map = gtk_widget_get_colormap (w);
731 gdk_colormap_query_color (map, pixel, c);
732 }
733
734 /* Create and set up the GTK widgets for frame F.
735 Return 0 if creation failed, non-zero otherwise. */
736
737 int
738 xg_create_frame_widgets (FRAME_PTR f)
739 {
740 GtkWidget *wtop;
741 GtkWidget *wvbox, *whbox;
742 GtkWidget *wfixed;
743 GdkColor bg;
744 GtkRcStyle *style;
745 char *title = 0;
746
747 BLOCK_INPUT;
748
749 if (FRAME_X_EMBEDDED_P (f))
750 wtop = gtk_plug_new (f->output_data.x->parent_desc);
751 else
752 wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);
753
754 xg_set_screen (wtop, f);
755
756 wvbox = gtk_vbox_new (FALSE, 0);
757 whbox = gtk_hbox_new (FALSE, 0);
758 wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */
759
760 if (! wtop || ! wvbox || ! whbox || ! wfixed)
761 {
762 if (wtop) gtk_widget_destroy (wtop);
763 if (wvbox) gtk_widget_destroy (wvbox);
764 if (whbox) gtk_widget_destroy (whbox);
765 if (wfixed) gtk_widget_destroy (wfixed);
766
767 UNBLOCK_INPUT;
768 return 0;
769 }
770
771 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
772 gtk_widget_set_name (wtop, EMACS_CLASS);
773 gtk_widget_set_name (wvbox, "pane");
774 gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
775
776 /* If this frame has a title or name, set it in the title bar. */
777 if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
778 else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
779
780 if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
781
782 FRAME_GTK_OUTER_WIDGET (f) = wtop;
783 FRAME_GTK_WIDGET (f) = wfixed;
784 f->output_data.x->vbox_widget = wvbox;
785 f->output_data.x->hbox_widget = whbox;
786
787 gtk_widget_set_has_window (wfixed, TRUE);
788
789 gtk_container_add (GTK_CONTAINER (wtop), wvbox);
790 gtk_box_pack_start (GTK_BOX (wvbox), whbox, TRUE, TRUE, 0);
791 gtk_box_pack_start (GTK_BOX (whbox), wfixed, TRUE, TRUE, 0);
792
793 if (FRAME_EXTERNAL_TOOL_BAR (f))
794 update_frame_tool_bar (f);
795
796 /* We don't want this widget double buffered, because we draw on it
797 with regular X drawing primitives, so from a GTK/GDK point of
798 view, the widget is totally blank. When an expose comes, this
799 will make the widget blank, and then Emacs redraws it. This flickers
800 a lot, so we turn off double buffering. */
801 gtk_widget_set_double_buffered (wfixed, FALSE);
802
803 gtk_window_set_wmclass (GTK_WINDOW (wtop),
804 SSDATA (Vx_resource_name),
805 SSDATA (Vx_resource_class));
806
807 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
808 GTK is to destroy the widget. We want Emacs to do that instead. */
809 g_signal_connect (G_OBJECT (wtop), "delete-event",
810 G_CALLBACK (gtk_true), 0);
811
812 /* Convert our geometry parameters into a geometry string
813 and specify it.
814 GTK will itself handle calculating the real position this way. */
815 xg_set_geometry (f);
816 f->win_gravity
817 = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
818
819 gtk_widget_add_events (wfixed,
820 GDK_POINTER_MOTION_MASK
821 | GDK_EXPOSURE_MASK
822 | GDK_BUTTON_PRESS_MASK
823 | GDK_BUTTON_RELEASE_MASK
824 | GDK_KEY_PRESS_MASK
825 | GDK_ENTER_NOTIFY_MASK
826 | GDK_LEAVE_NOTIFY_MASK
827 | GDK_FOCUS_CHANGE_MASK
828 | GDK_STRUCTURE_MASK
829 | GDK_VISIBILITY_NOTIFY_MASK);
830
831 /* Must realize the windows so the X window gets created. It is used
832 by callers of this function. */
833 gtk_widget_realize (wfixed);
834 FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);
835
836 /* Since GTK clears its window by filling with the background color,
837 we must keep X and GTK background in sync. */
838 xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
839 gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);
840
841 /* Also, do not let any background pixmap to be set, this looks very
842 bad as Emacs overwrites the background pixmap with its own idea
843 of background color. */
844 style = gtk_widget_get_modifier_style (wfixed);
845
846 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
847 style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
848 gtk_widget_modify_style (wfixed, style);
849
850 /* GTK does not set any border, and they look bad with GTK. */
851 /* That they look bad is no excuse for imposing this here. --Stef
852 It should be done by providing the proper default in Fx_create_Frame.
853 f->border_width = 0;
854 f->internal_border_width = 0; */
855
856 UNBLOCK_INPUT;
857
858 return 1;
859 }
860
861 /* Set the normal size hints for the window manager, for frame F.
862 FLAGS is the flags word to use--or 0 meaning preserve the flags
863 that the window now has.
864 If USER_POSITION is nonzero, we set the User Position
865 flag (this is useful when FLAGS is 0). */
866
867 void
868 x_wm_set_size_hint (FRAME_PTR f, long int flags, int user_position)
869 {
870 /* Must use GTK routines here, otherwise GTK resets the size hints
871 to its own defaults. */
872 GdkGeometry size_hints;
873 gint hint_flags = 0;
874 int base_width, base_height;
875 int min_rows = 0, min_cols = 0;
876 int win_gravity = f->win_gravity;
877
878 /* Don't set size hints during initialization; that apparently leads
879 to a race condition. See the thread at
880 http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html */
881 if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
882 return;
883
884 if (flags)
885 {
886 memset (&size_hints, 0, sizeof (size_hints));
887 f->output_data.x->size_hints = size_hints;
888 f->output_data.x->hint_flags = hint_flags;
889 }
890 else
891 flags = f->size_hint_flags;
892
893 size_hints = f->output_data.x->size_hints;
894 hint_flags = f->output_data.x->hint_flags;
895
896 hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
897 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
898 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
899
900 hint_flags |= GDK_HINT_BASE_SIZE;
901 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0) + FRAME_TOOLBAR_WIDTH (f);
902 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
903 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
904
905 check_frame_size (f, &min_rows, &min_cols);
906
907 size_hints.base_width = base_width;
908 size_hints.base_height = base_height;
909 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
910 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
911
912 /* These currently have a one to one mapping with the X values, but I
913 don't think we should rely on that. */
914 hint_flags |= GDK_HINT_WIN_GRAVITY;
915 size_hints.win_gravity = 0;
916 if (win_gravity == NorthWestGravity)
917 size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
918 else if (win_gravity == NorthGravity)
919 size_hints.win_gravity = GDK_GRAVITY_NORTH;
920 else if (win_gravity == NorthEastGravity)
921 size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
922 else if (win_gravity == WestGravity)
923 size_hints.win_gravity = GDK_GRAVITY_WEST;
924 else if (win_gravity == CenterGravity)
925 size_hints.win_gravity = GDK_GRAVITY_CENTER;
926 else if (win_gravity == EastGravity)
927 size_hints.win_gravity = GDK_GRAVITY_EAST;
928 else if (win_gravity == SouthWestGravity)
929 size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
930 else if (win_gravity == SouthGravity)
931 size_hints.win_gravity = GDK_GRAVITY_SOUTH;
932 else if (win_gravity == SouthEastGravity)
933 size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
934 else if (win_gravity == StaticGravity)
935 size_hints.win_gravity = GDK_GRAVITY_STATIC;
936
937 if (user_position)
938 {
939 hint_flags &= ~GDK_HINT_POS;
940 hint_flags |= GDK_HINT_USER_POS;
941 }
942
943 if (hint_flags != f->output_data.x->hint_flags
944 || memcmp (&size_hints,
945 &f->output_data.x->size_hints,
946 sizeof (size_hints)) != 0)
947 {
948 BLOCK_INPUT;
949 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
950 NULL, &size_hints, hint_flags);
951 f->output_data.x->size_hints = size_hints;
952 f->output_data.x->hint_flags = hint_flags;
953 UNBLOCK_INPUT;
954 }
955 }
956
957 /* Change background color of a frame.
958 Since GTK uses the background color to clear the window, we must
959 keep the GTK and X colors in sync.
960 F is the frame to change,
961 BG is the pixel value to change to. */
962
963 void
964 xg_set_background_color (FRAME_PTR f, long unsigned int bg)
965 {
966 if (FRAME_GTK_WIDGET (f))
967 {
968 GdkColor gdk_bg;
969
970 BLOCK_INPUT;
971 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
972 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
973 UNBLOCK_INPUT;
974 }
975 }
976
977
978 /* Set the frame icon to ICON_PIXMAP/MASK. This must be done with GTK
979 functions so GTK does not overwrite the icon. */
980
981 void
982 xg_set_frame_icon (FRAME_PTR f, Pixmap icon_pixmap, Pixmap icon_mask)
983 {
984 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
985 GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
986 GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
987 GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);
988
989 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
990 }
991
992
993 \f
994 /***********************************************************************
995 Dialog functions
996 ***********************************************************************/
997 /* Return the dialog title to use for a dialog of type KEY.
998 This is the encoding used by lwlib. We use the same for GTK. */
999
1000 static char *
1001 get_dialog_title (char key)
1002 {
1003 char *title = "";
1004
1005 switch (key) {
1006 case 'E': case 'e':
1007 title = "Error";
1008 break;
1009
1010 case 'I': case 'i':
1011 title = "Information";
1012 break;
1013
1014 case 'L': case 'l':
1015 title = "Prompt";
1016 break;
1017
1018 case 'P': case 'p':
1019 title = "Prompt";
1020 break;
1021
1022 case 'Q': case 'q':
1023 title = "Question";
1024 break;
1025 }
1026
1027 return title;
1028 }
1029
1030 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
1031 the dialog, but return TRUE so the event does not propagate further
1032 in GTK. This prevents GTK from destroying the dialog widget automatically
1033 and we can always destrou the widget manually, regardles of how
1034 it was popped down (button press or WM_DELETE_WINDOW).
1035 W is the dialog widget.
1036 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
1037 user_data is NULL (not used).
1038
1039 Returns TRUE to end propagation of event. */
1040
1041 static gboolean
1042 dialog_delete_callback (GtkWidget *w, GdkEvent *event, gpointer user_data)
1043 {
1044 gtk_widget_unmap (w);
1045 return TRUE;
1046 }
1047
1048 /* Create a popup dialog window. See also xg_create_widget below.
1049 WV is a widget_value describing the dialog.
1050 SELECT_CB is the callback to use when a button has been pressed.
1051 DEACTIVATE_CB is the callback to use when the dialog pops down.
1052
1053 Returns the GTK dialog widget. */
1054
1055 static GtkWidget *
1056 create_dialog (widget_value *wv,
1057 GCallback select_cb,
1058 GCallback deactivate_cb)
1059 {
1060 char *title = get_dialog_title (wv->name[0]);
1061 int total_buttons = wv->name[1] - '0';
1062 int right_buttons = wv->name[4] - '0';
1063 int left_buttons;
1064 int button_nr = 0;
1065 int button_spacing = 10;
1066 GtkWidget *wdialog = gtk_dialog_new ();
1067 GtkDialog *wd = GTK_DIALOG (wdialog);
1068 GtkBox *cur_box = GTK_BOX (gtk_dialog_get_action_area (wd));
1069 widget_value *item;
1070 GtkWidget *wvbox;
1071 GtkWidget *whbox_up;
1072 GtkWidget *whbox_down;
1073
1074 /* If the number of buttons is greater than 4, make two rows of buttons
1075 instead. This looks better. */
1076 int make_two_rows = total_buttons > 4;
1077
1078 if (right_buttons == 0) right_buttons = total_buttons/2;
1079 left_buttons = total_buttons - right_buttons;
1080
1081 gtk_window_set_title (GTK_WINDOW (wdialog), title);
1082 gtk_widget_set_name (wdialog, "emacs-dialog");
1083
1084
1085 if (make_two_rows)
1086 {
1087 wvbox = gtk_vbox_new (TRUE, button_spacing);
1088 whbox_up = gtk_hbox_new (FALSE, 0);
1089 whbox_down = gtk_hbox_new (FALSE, 0);
1090
1091 gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
1092 gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
1093 gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);
1094
1095 cur_box = GTK_BOX (whbox_up);
1096 }
1097
1098 g_signal_connect (G_OBJECT (wdialog), "delete-event",
1099 G_CALLBACK (dialog_delete_callback), 0);
1100
1101 if (deactivate_cb)
1102 {
1103 g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
1104 g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
1105 }
1106
1107 for (item = wv->contents; item; item = item->next)
1108 {
1109 char *utf8_label = get_utf8_string (item->value);
1110 GtkWidget *w;
1111 GtkRequisition req;
1112
1113 if (item->name && strcmp (item->name, "message") == 0)
1114 {
1115 GtkBox *wvbox = GTK_BOX (gtk_dialog_get_content_area (wd));
1116 /* This is the text part of the dialog. */
1117 w = gtk_label_new (utf8_label);
1118 gtk_box_pack_start (wvbox, gtk_label_new (""), FALSE, FALSE, 0);
1119 gtk_box_pack_start (wvbox, w, TRUE, TRUE, 0);
1120 gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);
1121
1122 /* Try to make dialog look better. Must realize first so
1123 the widget can calculate the size it needs. */
1124 gtk_widget_realize (w);
1125 gtk_widget_size_request (w, &req);
1126 gtk_box_set_spacing (wvbox, req.height);
1127 if (item->value && strlen (item->value) > 0)
1128 button_spacing = 2*req.width/strlen (item->value);
1129 }
1130 else
1131 {
1132 /* This is one button to add to the dialog. */
1133 w = gtk_button_new_with_label (utf8_label);
1134 if (! item->enabled)
1135 gtk_widget_set_sensitive (w, FALSE);
1136 if (select_cb)
1137 g_signal_connect (G_OBJECT (w), "clicked",
1138 select_cb, item->call_data);
1139
1140 gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
1141 if (++button_nr == left_buttons)
1142 {
1143 if (make_two_rows)
1144 cur_box = GTK_BOX (whbox_down);
1145 else
1146 gtk_box_pack_start (cur_box,
1147 gtk_label_new (""),
1148 TRUE, TRUE,
1149 button_spacing);
1150 }
1151 }
1152
1153 if (utf8_label && utf8_label != item->value)
1154 g_free (utf8_label);
1155 }
1156
1157 return wdialog;
1158 }
1159
1160 struct xg_dialog_data
1161 {
1162 GMainLoop *loop;
1163 int response;
1164 GtkWidget *w;
1165 guint timerid;
1166 };
1167
1168 /* Function that is called when the file or font dialogs pop down.
1169 W is the dialog widget, RESPONSE is the response code.
1170 USER_DATA is what we passed in to g_signal_connect. */
1171
1172 static void
1173 xg_dialog_response_cb (GtkDialog *w,
1174 gint response,
1175 gpointer user_data)
1176 {
1177 struct xg_dialog_data *dd = (struct xg_dialog_data *)user_data;
1178 dd->response = response;
1179 g_main_loop_quit (dd->loop);
1180 }
1181
1182
1183 /* Destroy the dialog. This makes it pop down. */
1184
1185 static Lisp_Object
1186 pop_down_dialog (Lisp_Object arg)
1187 {
1188 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1189 struct xg_dialog_data *dd = (struct xg_dialog_data *) p->pointer;
1190
1191 BLOCK_INPUT;
1192 if (dd->w) gtk_widget_destroy (dd->w);
1193 if (dd->timerid != 0) g_source_remove (dd->timerid);
1194
1195 g_main_loop_quit (dd->loop);
1196 g_main_loop_unref (dd->loop);
1197
1198 UNBLOCK_INPUT;
1199
1200 return Qnil;
1201 }
1202
1203 /* If there are any emacs timers pending, add a timeout to main loop in DATA.
1204 We pass in DATA as gpointer* so we can use this as a callback. */
1205
1206 static gboolean
1207 xg_maybe_add_timer (gpointer data)
1208 {
1209 struct xg_dialog_data *dd = (struct xg_dialog_data *) data;
1210 EMACS_TIME next_time = timer_check (1);
1211 long secs = EMACS_SECS (next_time);
1212 long usecs = EMACS_USECS (next_time);
1213
1214 dd->timerid = 0;
1215
1216 if (secs >= 0 && usecs >= 0 && secs < ((guint)-1)/1000)
1217 {
1218 dd->timerid = g_timeout_add (secs * 1000 + usecs/1000,
1219 xg_maybe_add_timer,
1220 dd);
1221 }
1222 return FALSE;
1223 }
1224
1225
1226 /* Pops up a modal dialog W and waits for response.
1227 We don't use gtk_dialog_run because we want to process emacs timers.
1228 The dialog W is not destroyed when this function returns. */
1229
1230 static int
1231 xg_dialog_run (FRAME_PTR f, GtkWidget *w)
1232 {
1233 int count = SPECPDL_INDEX ();
1234 struct xg_dialog_data dd;
1235
1236 xg_set_screen (w, f);
1237 gtk_window_set_transient_for (GTK_WINDOW (w),
1238 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
1239 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
1240 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
1241
1242 dd.loop = g_main_loop_new (NULL, FALSE);
1243 dd.response = GTK_RESPONSE_CANCEL;
1244 dd.w = w;
1245 dd.timerid = 0;
1246
1247 g_signal_connect (G_OBJECT (w),
1248 "response",
1249 G_CALLBACK (xg_dialog_response_cb),
1250 &dd);
1251 /* Don't destroy the widget if closed by the window manager close button. */
1252 g_signal_connect (G_OBJECT (w), "delete-event", G_CALLBACK (gtk_true), NULL);
1253 gtk_widget_show (w);
1254
1255 record_unwind_protect (pop_down_dialog, make_save_value (&dd, 0));
1256
1257 (void) xg_maybe_add_timer (&dd);
1258 g_main_loop_run (dd.loop);
1259
1260 dd.w = 0;
1261 unbind_to (count, Qnil);
1262
1263 return dd.response;
1264 }
1265
1266 \f
1267 /***********************************************************************
1268 File dialog functions
1269 ***********************************************************************/
1270 /* Return non-zero if the old file selection dialog is being used.
1271 Return zero if not. */
1272
1273 int
1274 xg_uses_old_file_dialog (void)
1275 {
1276 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1277 extern int x_gtk_use_old_file_dialog;
1278 return x_gtk_use_old_file_dialog;
1279 #else
1280 return 0;
1281 #endif
1282 }
1283
1284
1285 typedef char * (*xg_get_file_func) (GtkWidget *);
1286
1287 /* Return the selected file for file chooser dialog W.
1288 The returned string must be free:d. */
1289
1290 static char *
1291 xg_get_file_name_from_chooser (GtkWidget *w)
1292 {
1293 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
1294 }
1295
1296 /* Callback called when the "Show hidden files" toggle is pressed.
1297 WIDGET is the toggle widget, DATA is the file chooser dialog. */
1298
1299 static void
1300 xg_toggle_visibility_cb (GtkWidget *widget, gpointer data)
1301 {
1302 GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
1303 gboolean visible;
1304 g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
1305 g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1306 }
1307
1308
1309 /* Callback called when a property changes in a file chooser.
1310 GOBJECT is the file chooser dialog, ARG1 describes the property.
1311 USER_DATA is the toggle widget in the file chooser dialog.
1312 We use this to update the "Show hidden files" toggle when the user
1313 changes that property by right clicking in the file list. */
1314
1315 static void
1316 xg_toggle_notify_cb (GObject *gobject, GParamSpec *arg1, gpointer user_data)
1317 {
1318 extern int x_gtk_show_hidden_files;
1319
1320 if (strcmp (arg1->name, "show-hidden") == 0)
1321 {
1322 GtkWidget *wtoggle = GTK_WIDGET (user_data);
1323 gboolean visible, toggle_on;
1324
1325 g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
1326 toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));
1327
1328 if (!!visible != !!toggle_on)
1329 {
1330 g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
1331 G_CALLBACK (xg_toggle_visibility_cb),
1332 gobject);
1333 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
1334 g_signal_handlers_unblock_by_func
1335 (G_OBJECT (wtoggle),
1336 G_CALLBACK (xg_toggle_visibility_cb),
1337 gobject);
1338 }
1339 x_gtk_show_hidden_files = visible;
1340 }
1341 }
1342
1343 /* Read a file name from the user using a file chooser dialog.
1344 F is the current frame.
1345 PROMPT is a prompt to show to the user. May not be NULL.
1346 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1347 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1348 file. *FUNC is set to a function that can be used to retrieve the
1349 selected file name from the returned widget.
1350
1351 Returns the created widget. */
1352
1353 static GtkWidget *
1354 xg_get_file_with_chooser (FRAME_PTR f,
1355 char *prompt,
1356 char *default_filename,
1357 int mustmatch_p, int only_dir_p,
1358 xg_get_file_func *func)
1359 {
1360 char message[1024];
1361
1362 GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1363 GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
1364 GtkFileChooserAction action = (mustmatch_p ?
1365 GTK_FILE_CHOOSER_ACTION_OPEN :
1366 GTK_FILE_CHOOSER_ACTION_SAVE);
1367 extern int x_gtk_show_hidden_files;
1368 extern int x_gtk_file_dialog_help_text;
1369
1370
1371 if (only_dir_p)
1372 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
1373
1374 filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
1375 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1376 (mustmatch_p || only_dir_p ?
1377 GTK_STOCK_OPEN : GTK_STOCK_OK),
1378 GTK_RESPONSE_OK,
1379 NULL);
1380 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1381
1382 wbox = gtk_vbox_new (FALSE, 0);
1383 gtk_widget_show (wbox);
1384 wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
1385
1386 if (x_gtk_show_hidden_files)
1387 {
1388 g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
1389 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
1390 }
1391 gtk_widget_show (wtoggle);
1392 g_signal_connect (G_OBJECT (wtoggle), "clicked",
1393 G_CALLBACK (xg_toggle_visibility_cb), filewin);
1394 g_signal_connect (G_OBJECT (filewin), "notify",
1395 G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1396
1397 if (x_gtk_file_dialog_help_text)
1398 {
1399 message[0] = '\0';
1400 /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
1401 Show the C-l help text only for versions < 2.10. */
1402 if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1403 strcat (message, "\nType C-l to display a file name text entry box.\n");
1404 strcat (message, "\nIf you don't like this file selector, use the "
1405 "corresponding\nkey binding or customize "
1406 "use-file-dialog to turn it off.");
1407
1408 wmessage = gtk_label_new (message);
1409 gtk_widget_show (wmessage);
1410 }
1411
1412 gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1413 if (x_gtk_file_dialog_help_text)
1414 gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1415 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);
1416
1417 if (default_filename)
1418 {
1419 Lisp_Object file;
1420 struct gcpro gcpro1;
1421 char *utf8_filename;
1422 GCPRO1 (file);
1423
1424 file = build_string (default_filename);
1425
1426 /* File chooser does not understand ~/... in the file name. It must be
1427 an absolute name starting with /. */
1428 if (default_filename[0] != '/')
1429 file = Fexpand_file_name (file, Qnil);
1430
1431 utf8_filename = SSDATA (ENCODE_UTF_8 (file));
1432 if (! NILP (Ffile_directory_p (file)))
1433 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1434 utf8_filename);
1435 else
1436 {
1437 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
1438 utf8_filename);
1439 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
1440 {
1441 char *cp = strrchr (utf8_filename, '/');
1442 if (cp) ++cp;
1443 else cp = utf8_filename;
1444 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
1445 }
1446 }
1447
1448 UNGCPRO;
1449 }
1450
1451 *func = xg_get_file_name_from_chooser;
1452 return filewin;
1453 }
1454
1455 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1456
1457 /* Return the selected file for file selector dialog W.
1458 The returned string must be free:d. */
1459
1460 static char *
1461 xg_get_file_name_from_selector (GtkWidget *w)
1462 {
1463 GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
1464 return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
1465 }
1466
1467 /* Create a file selection dialog.
1468 F is the current frame.
1469 PROMPT is a prompt to show to the user. May not be NULL.
1470 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1471 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1472 file. *FUNC is set to a function that can be used to retrieve the
1473 selected file name from the returned widget.
1474
1475 Returns the created widget. */
1476
1477 static GtkWidget *
1478 xg_get_file_with_selection (FRAME_PTR f,
1479 char *prompt,
1480 char *default_filename,
1481 int mustmatch_p, int only_dir_p,
1482 xg_get_file_func *func)
1483 {
1484 GtkWidget *filewin;
1485 GtkFileSelection *filesel;
1486
1487 filewin = gtk_file_selection_new (prompt);
1488 filesel = GTK_FILE_SELECTION (filewin);
1489
1490 if (default_filename)
1491 gtk_file_selection_set_filename (filesel, default_filename);
1492
1493 if (mustmatch_p)
1494 {
1495 /* The selection_entry part of filesel is not documented. */
1496 gtk_widget_set_sensitive (filesel->selection_entry, FALSE);
1497 gtk_file_selection_hide_fileop_buttons (filesel);
1498 }
1499
1500 *func = xg_get_file_name_from_selector;
1501
1502 return filewin;
1503 }
1504 #endif /* HAVE_GTK_FILE_SELECTION_NEW */
1505
1506 /* Read a file name from the user using a file dialog, either the old
1507 file selection dialog, or the new file chooser dialog. Which to use
1508 depends on what the GTK version used has, and what the value of
1509 gtk-use-old-file-dialog.
1510 F is the current frame.
1511 PROMPT is a prompt to show to the user. May not be NULL.
1512 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
1513 If MUSTMATCH_P is non-zero, the returned file name must be an existing
1514 file.
1515
1516 Returns a file name or NULL if no file was selected.
1517 The returned string must be freed by the caller. */
1518
1519 char *
1520 xg_get_file_name (FRAME_PTR f,
1521 char *prompt,
1522 char *default_filename,
1523 int mustmatch_p,
1524 int only_dir_p)
1525 {
1526 GtkWidget *w = 0;
1527 char *fn = 0;
1528 int filesel_done = 0;
1529 xg_get_file_func func;
1530
1531 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1532 /* I really don't know why this is needed, but without this the GLIBC add on
1533 library linuxthreads hangs when the Gnome file chooser backend creates
1534 threads. */
1535 sigblock (sigmask (__SIGRTMIN));
1536 #endif /* HAVE_GTK_AND_PTHREAD */
1537
1538 #ifdef HAVE_GTK_FILE_SELECTION_NEW
1539
1540 if (xg_uses_old_file_dialog ())
1541 w = xg_get_file_with_selection (f, prompt, default_filename,
1542 mustmatch_p, only_dir_p, &func);
1543 else
1544 w = xg_get_file_with_chooser (f, prompt, default_filename,
1545 mustmatch_p, only_dir_p, &func);
1546
1547 #else /* not HAVE_GTK_FILE_SELECTION_NEW */
1548 w = xg_get_file_with_chooser (f, prompt, default_filename,
1549 mustmatch_p, only_dir_p, &func);
1550 #endif /* not HAVE_GTK_FILE_SELECTION_NEW */
1551
1552 gtk_widget_set_name (w, "emacs-filedialog");
1553
1554 filesel_done = xg_dialog_run (f, w);
1555
1556 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1557 sigunblock (sigmask (__SIGRTMIN));
1558 #endif
1559
1560 if (filesel_done == GTK_RESPONSE_OK)
1561 fn = (*func) (w);
1562
1563 gtk_widget_destroy (w);
1564 return fn;
1565 }
1566
1567 #ifdef HAVE_FREETYPE
1568 /* Pop up a GTK font selector and return the name of the font the user
1569 selects, as a C string. The returned font name follows GTK's own
1570 format:
1571
1572 `FAMILY [VALUE1 VALUE2] SIZE'
1573
1574 This can be parsed using font_parse_fcname in font.c.
1575 DEFAULT_NAME, if non-zero, is the default font name. */
1576
1577 char *
1578 xg_get_font_name (FRAME_PTR f, char *default_name)
1579 {
1580 GtkWidget *w;
1581 char *fontname = NULL;
1582 int done = 0;
1583
1584 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1585 sigblock (sigmask (__SIGRTMIN));
1586 #endif /* HAVE_GTK_AND_PTHREAD */
1587
1588 w = gtk_font_selection_dialog_new ("Pick a font");
1589 if (!default_name)
1590 default_name = "Monospace 10";
1591 gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (w),
1592 default_name);
1593
1594 gtk_widget_set_name (w, "emacs-fontdialog");
1595
1596 done = xg_dialog_run (f, w);
1597
1598 #if defined (HAVE_GTK_AND_PTHREAD) && defined (__SIGRTMIN)
1599 sigunblock (sigmask (__SIGRTMIN));
1600 #endif
1601
1602 if (done == GTK_RESPONSE_OK)
1603 fontname = gtk_font_selection_dialog_get_font_name
1604 (GTK_FONT_SELECTION_DIALOG (w));
1605
1606 gtk_widget_destroy (w);
1607 return fontname;
1608 }
1609 #endif /* HAVE_FREETYPE */
1610
1611
1612 \f
1613 /***********************************************************************
1614 Menu functions.
1615 ***********************************************************************/
1616
1617 /* The name of menu items that can be used for customization. Since GTK
1618 RC files are very crude and primitive, we have to set this on all
1619 menu item names so a user can easily customize menu items. */
1620
1621 #define MENU_ITEM_NAME "emacs-menuitem"
1622
1623
1624 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
1625 during GC. The next member points to the items. */
1626 static xg_list_node xg_menu_cb_list;
1627
1628 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
1629 during GC. The next member points to the items. */
1630 static xg_list_node xg_menu_item_cb_list;
1631
1632 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
1633 F is the frame CL_DATA will be initialized for.
1634 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1635
1636 The menu bar and all sub menus under the menu bar in a frame
1637 share the same structure, hence the reference count.
1638
1639 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
1640 allocated xg_menu_cb_data if CL_DATA is NULL. */
1641
1642 static xg_menu_cb_data *
1643 make_cl_data (xg_menu_cb_data *cl_data, FRAME_PTR f, GCallback highlight_cb)
1644 {
1645 if (! cl_data)
1646 {
1647 cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data));
1648 cl_data->f = f;
1649 cl_data->menu_bar_vector = f->menu_bar_vector;
1650 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1651 cl_data->highlight_cb = highlight_cb;
1652 cl_data->ref_count = 0;
1653
1654 xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs);
1655 }
1656
1657 cl_data->ref_count++;
1658
1659 return cl_data;
1660 }
1661
1662 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
1663 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1664
1665 When the menu bar is updated, menu items may have been added and/or
1666 removed, so menu_bar_vector and menu_bar_items_used change. We must
1667 then update CL_DATA since it is used to determine which menu
1668 item that is invoked in the menu.
1669 HIGHLIGHT_CB could change, there is no check that the same
1670 function is given when modifying a menu bar as was given when
1671 creating the menu bar. */
1672
1673 static void
1674 update_cl_data (xg_menu_cb_data *cl_data,
1675 FRAME_PTR f,
1676 GCallback highlight_cb)
1677 {
1678 if (cl_data)
1679 {
1680 cl_data->f = f;
1681 cl_data->menu_bar_vector = f->menu_bar_vector;
1682 cl_data->menu_bar_items_used = f->menu_bar_items_used;
1683 cl_data->highlight_cb = highlight_cb;
1684 }
1685 }
1686
1687 /* Decrease reference count for CL_DATA.
1688 If reference count is zero, free CL_DATA. */
1689
1690 static void
1691 unref_cl_data (xg_menu_cb_data *cl_data)
1692 {
1693 if (cl_data && cl_data->ref_count > 0)
1694 {
1695 cl_data->ref_count--;
1696 if (cl_data->ref_count == 0)
1697 {
1698 xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs);
1699 xfree (cl_data);
1700 }
1701 }
1702 }
1703
1704 /* Function that marks all lisp data during GC. */
1705
1706 void
1707 xg_mark_data (void)
1708 {
1709 xg_list_node *iter;
1710
1711 for (iter = xg_menu_cb_list.next; iter; iter = iter->next)
1712 mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector);
1713
1714 for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next)
1715 {
1716 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter;
1717
1718 if (! NILP (cb_data->help))
1719 mark_object (cb_data->help);
1720 }
1721 }
1722
1723
1724 /* Callback called when a menu item is destroyed. Used to free data.
1725 W is the widget that is being destroyed (not used).
1726 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1727
1728 static void
1729 menuitem_destroy_callback (GtkWidget *w, gpointer client_data)
1730 {
1731 if (client_data)
1732 {
1733 xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data;
1734 xg_list_remove (&xg_menu_item_cb_list, &data->ptrs);
1735 xfree (data);
1736 }
1737 }
1738
1739 /* Callback called when the pointer enters/leaves a menu item.
1740 W is the parent of the menu item.
1741 EVENT is either an enter event or leave event.
1742 CLIENT_DATA is not used.
1743
1744 Returns FALSE to tell GTK to keep processing this event. */
1745
1746 static gboolean
1747 menuitem_highlight_callback (GtkWidget *w,
1748 GdkEventCrossing *event,
1749 gpointer client_data)
1750 {
1751 GdkEvent ev;
1752 GtkWidget *subwidget;
1753 xg_menu_item_cb_data *data;
1754
1755 ev.crossing = *event;
1756 subwidget = gtk_get_event_widget (&ev);
1757 data = (xg_menu_item_cb_data *) g_object_get_data (G_OBJECT (subwidget),
1758 XG_ITEM_DATA);
1759 if (data)
1760 {
1761 if (! NILP (data->help) && data->cl_data->highlight_cb)
1762 {
1763 gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : data;
1764 GtkCallback func = (GtkCallback) data->cl_data->highlight_cb;
1765 (*func) (subwidget, call_data);
1766 }
1767 }
1768
1769 return FALSE;
1770 }
1771
1772 /* Callback called when a menu is destroyed. Used to free data.
1773 W is the widget that is being destroyed (not used).
1774 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1775
1776 static void
1777 menu_destroy_callback (GtkWidget *w, gpointer client_data)
1778 {
1779 unref_cl_data ((xg_menu_cb_data*) client_data);
1780 }
1781
1782 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1783 must be non-NULL) and can be inserted into a menu item.
1784
1785 Returns the GtkHBox. */
1786
1787 static GtkWidget *
1788 make_widget_for_menu_item (char *utf8_label, char *utf8_key)
1789 {
1790 GtkWidget *wlbl;
1791 GtkWidget *wkey;
1792 GtkWidget *wbox;
1793
1794 wbox = gtk_hbox_new (FALSE, 0);
1795 wlbl = gtk_label_new (utf8_label);
1796 wkey = gtk_label_new (utf8_key);
1797
1798 gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5);
1799 gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5);
1800
1801 gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0);
1802 gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0);
1803
1804 gtk_widget_set_name (wlbl, MENU_ITEM_NAME);
1805 gtk_widget_set_name (wkey, MENU_ITEM_NAME);
1806 gtk_widget_set_name (wbox, MENU_ITEM_NAME);
1807
1808 return wbox;
1809 }
1810
1811 /* Make and return a menu item widget with the key to the right.
1812 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1813 UTF8_KEY is the text representing the key binding.
1814 ITEM is the widget_value describing the menu item.
1815
1816 GROUP is an in/out parameter. If the menu item to be created is not
1817 part of any radio menu group, *GROUP contains NULL on entry and exit.
1818 If the menu item to be created is part of a radio menu group, on entry
1819 *GROUP contains the group to use, or NULL if this is the first item
1820 in the group. On exit, *GROUP contains the radio item group.
1821
1822 Unfortunately, keys don't line up as nicely as in Motif,
1823 but the MacOS X version doesn't either, so I guess that is OK. */
1824
1825 static GtkWidget *
1826 make_menu_item (char *utf8_label,
1827 char *utf8_key,
1828 widget_value *item,
1829 GSList **group)
1830 {
1831 GtkWidget *w;
1832 GtkWidget *wtoadd = 0;
1833
1834 /* It has been observed that some menu items have a NULL name field.
1835 This will lead to this function being called with a NULL utf8_label.
1836 GTK crashes on that so we set a blank label. Why there is a NULL
1837 name remains to be investigated. */
1838 if (! utf8_label) utf8_label = " ";
1839
1840 if (utf8_key)
1841 wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
1842
1843 if (item->button_type == BUTTON_TYPE_TOGGLE)
1844 {
1845 *group = NULL;
1846 if (utf8_key) w = gtk_check_menu_item_new ();
1847 else w = gtk_check_menu_item_new_with_label (utf8_label);
1848 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected);
1849 }
1850 else if (item->button_type == BUTTON_TYPE_RADIO)
1851 {
1852 if (utf8_key) w = gtk_radio_menu_item_new (*group);
1853 else w = gtk_radio_menu_item_new_with_label (*group, utf8_label);
1854 *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
1855 if (item->selected)
1856 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
1857 }
1858 else
1859 {
1860 *group = NULL;
1861 if (utf8_key) w = gtk_menu_item_new ();
1862 else w = gtk_menu_item_new_with_label (utf8_label);
1863 }
1864
1865 if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd);
1866 if (! item->enabled) gtk_widget_set_sensitive (w, FALSE);
1867
1868 return w;
1869 }
1870
1871 /* Return non-zero if LABEL specifies a separator (GTK only has one
1872 separator type) */
1873
1874 static const char* separator_names[] = {
1875 "space",
1876 "no-line",
1877 "single-line",
1878 "double-line",
1879 "single-dashed-line",
1880 "double-dashed-line",
1881 "shadow-etched-in",
1882 "shadow-etched-out",
1883 "shadow-etched-in-dash",
1884 "shadow-etched-out-dash",
1885 "shadow-double-etched-in",
1886 "shadow-double-etched-out",
1887 "shadow-double-etched-in-dash",
1888 "shadow-double-etched-out-dash",
1889 0,
1890 };
1891
1892 static int
1893 xg_separator_p (char *label)
1894 {
1895 if (! label) return 0;
1896 else if (strlen (label) > 3
1897 && strncmp (label, "--", 2) == 0
1898 && label[2] != '-')
1899 {
1900 int i;
1901
1902 label += 2;
1903 for (i = 0; separator_names[i]; ++i)
1904 if (strcmp (label, separator_names[i]) == 0)
1905 return 1;
1906 }
1907 else
1908 {
1909 /* Old-style separator, maybe. It's a separator if it contains
1910 only dashes. */
1911 while (*label == '-')
1912 ++label;
1913 if (*label == 0) return 1;
1914 }
1915
1916 return 0;
1917 }
1918
1919 static int xg_detached_menus;
1920
1921 /* Returns non-zero if there are detached menus. */
1922
1923 int
1924 xg_have_tear_offs (void)
1925 {
1926 return xg_detached_menus > 0;
1927 }
1928
1929 /* Callback invoked when a detached menu window is removed. Here we
1930 decrease the xg_detached_menus count.
1931 WIDGET is the top level window that is removed (the parent of the menu).
1932 CLIENT_DATA is not used. */
1933
1934 static void
1935 tearoff_remove (GtkWidget *widget, gpointer client_data)
1936 {
1937 if (xg_detached_menus > 0) --xg_detached_menus;
1938 }
1939
1940 /* Callback invoked when a menu is detached. It increases the
1941 xg_detached_menus count.
1942 WIDGET is the GtkTearoffMenuItem.
1943 CLIENT_DATA is not used. */
1944
1945 static void
1946 tearoff_activate (GtkWidget *widget, gpointer client_data)
1947 {
1948 GtkWidget *menu = gtk_widget_get_parent (widget);
1949 if (gtk_menu_get_tearoff_state (GTK_MENU (menu)))
1950 {
1951 ++xg_detached_menus;
1952 g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)),
1953 "destroy",
1954 G_CALLBACK (tearoff_remove), 0);
1955 }
1956 }
1957
1958
1959 /* Create a menu item widget, and connect the callbacks.
1960 ITEM decribes the menu item.
1961 F is the frame the created menu belongs to.
1962 SELECT_CB is the callback to use when a menu item is selected.
1963 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1964 CL_DATA points to the callback data to be used for this menu.
1965 GROUP is an in/out parameter. If the menu item to be created is not
1966 part of any radio menu group, *GROUP contains NULL on entry and exit.
1967 If the menu item to be created is part of a radio menu group, on entry
1968 *GROUP contains the group to use, or NULL if this is the first item
1969 in the group. On exit, *GROUP contains the radio item group.
1970
1971 Returns the created GtkWidget. */
1972
1973 static GtkWidget *
1974 xg_create_one_menuitem (widget_value *item,
1975 FRAME_PTR f,
1976 GCallback select_cb,
1977 GCallback highlight_cb,
1978 xg_menu_cb_data *cl_data,
1979 GSList **group)
1980 {
1981 char *utf8_label;
1982 char *utf8_key;
1983 GtkWidget *w;
1984 xg_menu_item_cb_data *cb_data;
1985
1986 utf8_label = get_utf8_string (item->name);
1987 utf8_key = get_utf8_string (item->key);
1988
1989 w = make_menu_item (utf8_label, utf8_key, item, group);
1990
1991 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
1992 if (utf8_key && utf8_key != item->key) g_free (utf8_key);
1993
1994 cb_data = xmalloc (sizeof (xg_menu_item_cb_data));
1995
1996 xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs);
1997
1998 cb_data->select_id = 0;
1999 cb_data->help = item->help;
2000 cb_data->cl_data = cl_data;
2001 cb_data->call_data = item->call_data;
2002
2003 g_signal_connect (G_OBJECT (w),
2004 "destroy",
2005 G_CALLBACK (menuitem_destroy_callback),
2006 cb_data);
2007
2008 /* Put cb_data in widget, so we can get at it when modifying menubar */
2009 g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data);
2010
2011 /* final item, not a submenu */
2012 if (item->call_data && ! item->contents)
2013 {
2014 if (select_cb)
2015 cb_data->select_id
2016 = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data);
2017 }
2018
2019 return w;
2020 }
2021
2022 /* Create a full menu tree specified by DATA.
2023 F is the frame the created menu belongs to.
2024 SELECT_CB is the callback to use when a menu item is selected.
2025 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2026 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2027 POP_UP_P is non-zero if we shall create a popup menu.
2028 MENU_BAR_P is non-zero if we shall create a menu bar.
2029 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
2030 if MENU_BAR_P is non-zero.
2031 TOPMENU is the topmost GtkWidget that others shall be placed under.
2032 It may be NULL, in that case we create the appropriate widget
2033 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
2034 CL_DATA is the callback data we shall use for this menu, or NULL
2035 if we haven't set the first callback yet.
2036 NAME is the name to give to the top level menu if this function
2037 creates it. May be NULL to not set any name.
2038
2039 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
2040 not NULL.
2041
2042 This function calls itself to create submenus. */
2043
2044 static GtkWidget *
2045 create_menus (widget_value *data,
2046 FRAME_PTR f,
2047 GCallback select_cb,
2048 GCallback deactivate_cb,
2049 GCallback highlight_cb,
2050 int pop_up_p,
2051 int menu_bar_p,
2052 int add_tearoff_p,
2053 GtkWidget *topmenu,
2054 xg_menu_cb_data *cl_data,
2055 char *name)
2056 {
2057 widget_value *item;
2058 GtkWidget *wmenu = topmenu;
2059 GSList *group = NULL;
2060
2061 if (! topmenu)
2062 {
2063 if (! menu_bar_p)
2064 {
2065 wmenu = gtk_menu_new ();
2066 xg_set_screen (wmenu, f);
2067 /* Connect this to the menu instead of items so we get enter/leave for
2068 disabled items also. TODO: Still does not get enter/leave for
2069 disabled items in detached menus. */
2070 g_signal_connect (G_OBJECT (wmenu),
2071 "enter-notify-event",
2072 G_CALLBACK (menuitem_highlight_callback),
2073 NULL);
2074 g_signal_connect (G_OBJECT (wmenu),
2075 "leave-notify-event",
2076 G_CALLBACK (menuitem_highlight_callback),
2077 NULL);
2078 }
2079 else
2080 {
2081 wmenu = gtk_menu_bar_new ();
2082 /* Set width of menu bar to a small value so it doesn't enlarge
2083 a small initial frame size. The width will be set to the
2084 width of the frame later on when it is added to a container.
2085 height -1: Natural height. */
2086 gtk_widget_set_size_request (wmenu, 1, -1);
2087 }
2088
2089 /* Put cl_data on the top menu for easier access. */
2090 cl_data = make_cl_data (cl_data, f, highlight_cb);
2091 g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data);
2092 g_signal_connect (G_OBJECT (wmenu), "destroy",
2093 G_CALLBACK (menu_destroy_callback), cl_data);
2094
2095 if (name)
2096 gtk_widget_set_name (wmenu, name);
2097
2098 if (deactivate_cb)
2099 g_signal_connect (G_OBJECT (wmenu),
2100 "selection-done", deactivate_cb, 0);
2101 }
2102
2103 if (! menu_bar_p && add_tearoff_p)
2104 {
2105 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2106 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff);
2107
2108 g_signal_connect (G_OBJECT (tearoff), "activate",
2109 G_CALLBACK (tearoff_activate), 0);
2110 }
2111
2112 for (item = data; item; item = item->next)
2113 {
2114 GtkWidget *w;
2115
2116 if (pop_up_p && !item->contents && !item->call_data
2117 && !xg_separator_p (item->name))
2118 {
2119 char *utf8_label;
2120 /* A title for a popup. We do the same as GTK does when
2121 creating titles, but it does not look good. */
2122 group = NULL;
2123 utf8_label = get_utf8_string (item->name);
2124
2125 gtk_menu_set_title (GTK_MENU (wmenu), utf8_label);
2126 w = gtk_menu_item_new_with_label (utf8_label);
2127 gtk_widget_set_sensitive (w, FALSE);
2128 if (utf8_label && utf8_label != item->name) g_free (utf8_label);
2129 }
2130 else if (xg_separator_p (item->name))
2131 {
2132 group = NULL;
2133 /* GTK only have one separator type. */
2134 w = gtk_separator_menu_item_new ();
2135 }
2136 else
2137 {
2138 w = xg_create_one_menuitem (item,
2139 f,
2140 item->contents ? 0 : select_cb,
2141 highlight_cb,
2142 cl_data,
2143 &group);
2144
2145 /* Create a possibly empty submenu for menu bar items, since some
2146 themes don't highlight items correctly without it. */
2147 if (item->contents || menu_bar_p)
2148 {
2149 GtkWidget *submenu = create_menus (item->contents,
2150 f,
2151 select_cb,
2152 deactivate_cb,
2153 highlight_cb,
2154 0,
2155 0,
2156 add_tearoff_p,
2157 0,
2158 cl_data,
2159 0);
2160 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2161 }
2162 }
2163
2164 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w);
2165 gtk_widget_set_name (w, MENU_ITEM_NAME);
2166 }
2167
2168 return wmenu;
2169 }
2170
2171 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
2172 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
2173 with some text and buttons.
2174 F is the frame the created item belongs to.
2175 NAME is the name to use for the top widget.
2176 VAL is a widget_value structure describing items to be created.
2177 SELECT_CB is the callback to use when a menu item is selected or
2178 a dialog button is pressed.
2179 DEACTIVATE_CB is the callback to use when an item is deactivated.
2180 For a menu, when a sub menu is not shown anymore, for a dialog it is
2181 called when the dialog is popped down.
2182 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2183
2184 Returns the widget created. */
2185
2186 GtkWidget *
2187 xg_create_widget (char *type, char *name, FRAME_PTR f, widget_value *val,
2188 GCallback select_cb, GCallback deactivate_cb,
2189 GCallback highlight_cb)
2190 {
2191 GtkWidget *w = 0;
2192 int menu_bar_p = strcmp (type, "menubar") == 0;
2193 int pop_up_p = strcmp (type, "popup") == 0;
2194
2195 if (strcmp (type, "dialog") == 0)
2196 {
2197 w = create_dialog (val, select_cb, deactivate_cb);
2198 xg_set_screen (w, f);
2199 gtk_window_set_transient_for (GTK_WINDOW (w),
2200 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
2201 gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE);
2202 gtk_widget_set_name (w, "emacs-dialog");
2203 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
2204 }
2205 else if (menu_bar_p || pop_up_p)
2206 {
2207 w = create_menus (val->contents,
2208 f,
2209 select_cb,
2210 deactivate_cb,
2211 highlight_cb,
2212 pop_up_p,
2213 menu_bar_p,
2214 menu_bar_p,
2215 0,
2216 0,
2217 name);
2218
2219 /* Set the cursor to an arrow for popup menus when they are mapped.
2220 This is done by default for menu bar menus. */
2221 if (pop_up_p)
2222 {
2223 /* Must realize so the GdkWindow inside the widget is created. */
2224 gtk_widget_realize (w);
2225 xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
2226 }
2227 }
2228 else
2229 {
2230 fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n",
2231 type);
2232 }
2233
2234 return w;
2235 }
2236
2237 /* Return the label for menu item WITEM. */
2238
2239 static const char *
2240 xg_get_menu_item_label (GtkMenuItem *witem)
2241 {
2242 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2243 return gtk_label_get_label (wlabel);
2244 }
2245
2246 /* Return non-zero if the menu item WITEM has the text LABEL. */
2247
2248 static int
2249 xg_item_label_same_p (GtkMenuItem *witem, char *label)
2250 {
2251 int is_same = 0;
2252 char *utf8_label = get_utf8_string (label);
2253 const char *old_label = witem ? xg_get_menu_item_label (witem) : 0;
2254
2255 if (! old_label && ! utf8_label)
2256 is_same = 1;
2257 else if (old_label && utf8_label)
2258 is_same = strcmp (utf8_label, old_label) == 0;
2259
2260 if (utf8_label && utf8_label != label) g_free (utf8_label);
2261
2262 return is_same;
2263 }
2264
2265 /* Destroy widgets in LIST. */
2266
2267 static void
2268 xg_destroy_widgets (GList *list)
2269 {
2270 GList *iter;
2271
2272 for (iter = list; iter; iter = g_list_next (iter))
2273 {
2274 GtkWidget *w = GTK_WIDGET (iter->data);
2275
2276 /* Destroying the widget will remove it from the container it is in. */
2277 gtk_widget_destroy (w);
2278 }
2279 }
2280
2281 /* Update the top level names in MENUBAR (i.e. not submenus).
2282 F is the frame the menu bar belongs to.
2283 *LIST is a list with the current menu bar names (menu item widgets).
2284 ITER is the item within *LIST that shall be updated.
2285 POS is the numerical position, starting at 0, of ITER in *LIST.
2286 VAL describes what the menu bar shall look like after the update.
2287 SELECT_CB is the callback to use when a menu item is selected.
2288 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2289 CL_DATA points to the callback data to be used for this menu bar.
2290
2291 This function calls itself to walk through the menu bar names. */
2292
2293 static void
2294 xg_update_menubar (GtkWidget *menubar,
2295 FRAME_PTR f,
2296 GList **list,
2297 GList *iter,
2298 int pos,
2299 widget_value *val,
2300 GCallback select_cb,
2301 GCallback deactivate_cb,
2302 GCallback highlight_cb,
2303 xg_menu_cb_data *cl_data)
2304 {
2305 if (! iter && ! val)
2306 return;
2307 else if (iter && ! val)
2308 {
2309 /* Item(s) have been removed. Remove all remaining items. */
2310 xg_destroy_widgets (iter);
2311
2312 /* Add a blank entry so the menubar doesn't collapse to nothing. */
2313 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2314 gtk_menu_item_new_with_label (""),
2315 0);
2316 /* All updated. */
2317 val = 0;
2318 iter = 0;
2319 }
2320 else if (! iter && val)
2321 {
2322 /* Item(s) added. Add all new items in one call. */
2323 create_menus (val, f, select_cb, deactivate_cb, highlight_cb,
2324 0, 1, 0, menubar, cl_data, 0);
2325
2326 /* All updated. */
2327 val = 0;
2328 iter = 0;
2329 }
2330 /* Below this neither iter or val is NULL */
2331 else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name))
2332 {
2333 /* This item is still the same, check next item. */
2334 val = val->next;
2335 iter = g_list_next (iter);
2336 ++pos;
2337 }
2338 else /* This item is changed. */
2339 {
2340 GtkMenuItem *witem = GTK_MENU_ITEM (iter->data);
2341 GtkMenuItem *witem2 = 0;
2342 int val_in_menubar = 0;
2343 int iter_in_new_menubar = 0;
2344 GList *iter2;
2345 widget_value *cur;
2346
2347 /* See if the changed entry (val) is present later in the menu bar */
2348 for (iter2 = iter;
2349 iter2 && ! val_in_menubar;
2350 iter2 = g_list_next (iter2))
2351 {
2352 witem2 = GTK_MENU_ITEM (iter2->data);
2353 val_in_menubar = xg_item_label_same_p (witem2, val->name);
2354 }
2355
2356 /* See if the current entry (iter) is present later in the
2357 specification for the new menu bar. */
2358 for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next)
2359 iter_in_new_menubar = xg_item_label_same_p (witem, cur->name);
2360
2361 if (val_in_menubar && ! iter_in_new_menubar)
2362 {
2363 int nr = pos;
2364
2365 /* This corresponds to:
2366 Current: A B C
2367 New: A C
2368 Remove B. */
2369
2370 g_object_ref (G_OBJECT (witem));
2371 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem));
2372 gtk_widget_destroy (GTK_WIDGET (witem));
2373
2374 /* Must get new list since the old changed. */
2375 g_list_free (*list);
2376 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2377 while (nr-- > 0) iter = g_list_next (iter);
2378 }
2379 else if (! val_in_menubar && ! iter_in_new_menubar)
2380 {
2381 /* This corresponds to:
2382 Current: A B C
2383 New: A X C
2384 Rename B to X. This might seem to be a strange thing to do,
2385 since if there is a menu under B it will be totally wrong for X.
2386 But consider editing a C file. Then there is a C-mode menu
2387 (corresponds to B above).
2388 If then doing C-x C-f the minibuf menu (X above) replaces the
2389 C-mode menu. When returning from the minibuffer, we get
2390 back the C-mode menu. Thus we do:
2391 Rename B to X (C-mode to minibuf menu)
2392 Rename X to B (minibuf to C-mode menu).
2393 If the X menu hasn't been invoked, the menu under B
2394 is up to date when leaving the minibuffer. */
2395 GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem)));
2396 char *utf8_label = get_utf8_string (val->name);
2397 GtkWidget *submenu = gtk_menu_item_get_submenu (witem);
2398
2399 gtk_label_set_text (wlabel, utf8_label);
2400
2401 /* If this item has a submenu that has been detached, change
2402 the title in the WM decorations also. */
2403 if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu)))
2404 /* Set the title of the detached window. */
2405 gtk_menu_set_title (GTK_MENU (submenu), utf8_label);
2406
2407 iter = g_list_next (iter);
2408 val = val->next;
2409 ++pos;
2410 }
2411 else if (! val_in_menubar && iter_in_new_menubar)
2412 {
2413 /* This corresponds to:
2414 Current: A B C
2415 New: A X B C
2416 Insert X. */
2417
2418 int nr = pos;
2419 GSList *group = 0;
2420 GtkWidget *w = xg_create_one_menuitem (val,
2421 f,
2422 select_cb,
2423 highlight_cb,
2424 cl_data,
2425 &group);
2426
2427 /* Create a possibly empty submenu for menu bar items, since some
2428 themes don't highlight items correctly without it. */
2429 GtkWidget *submenu = create_menus (NULL, f,
2430 select_cb, deactivate_cb,
2431 highlight_cb,
2432 0, 0, 0, 0, cl_data, 0);
2433 gtk_widget_set_name (w, MENU_ITEM_NAME);
2434 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos);
2435 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu);
2436
2437 g_list_free (*list);
2438 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2439 while (nr-- > 0) iter = g_list_next (iter);
2440 iter = g_list_next (iter);
2441 val = val->next;
2442 ++pos;
2443 }
2444 else /* if (val_in_menubar && iter_in_new_menubar) */
2445 {
2446 int nr = pos;
2447 /* This corresponds to:
2448 Current: A B C
2449 New: A C B
2450 Move C before B */
2451
2452 g_object_ref (G_OBJECT (witem2));
2453 gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2));
2454 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar),
2455 GTK_WIDGET (witem2), pos);
2456 g_object_unref (G_OBJECT (witem2));
2457
2458 g_list_free (*list);
2459 *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar));
2460 while (nr-- > 0) iter = g_list_next (iter);
2461 if (iter) iter = g_list_next (iter);
2462 val = val->next;
2463 ++pos;
2464 }
2465 }
2466
2467 /* Update the rest of the menu bar. */
2468 xg_update_menubar (menubar, f, list, iter, pos, val,
2469 select_cb, deactivate_cb, highlight_cb, cl_data);
2470 }
2471
2472 /* Update the menu item W so it corresponds to VAL.
2473 SELECT_CB is the callback to use when a menu item is selected.
2474 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2475 CL_DATA is the data to set in the widget for menu invocation. */
2476
2477 static void
2478 xg_update_menu_item (widget_value *val,
2479 GtkWidget *w,
2480 GCallback select_cb,
2481 GCallback highlight_cb,
2482 xg_menu_cb_data *cl_data)
2483 {
2484 GtkWidget *wchild;
2485 GtkLabel *wlbl = 0;
2486 GtkLabel *wkey = 0;
2487 char *utf8_label;
2488 char *utf8_key;
2489 const char *old_label = 0;
2490 const char *old_key = 0;
2491 xg_menu_item_cb_data *cb_data;
2492
2493 wchild = gtk_bin_get_child (GTK_BIN (w));
2494 utf8_label = get_utf8_string (val->name);
2495 utf8_key = get_utf8_string (val->key);
2496
2497 /* See if W is a menu item with a key. See make_menu_item above. */
2498 if (GTK_IS_HBOX (wchild))
2499 {
2500 GList *list = gtk_container_get_children (GTK_CONTAINER (wchild));
2501
2502 wlbl = GTK_LABEL (list->data);
2503 wkey = GTK_LABEL (list->next->data);
2504 g_list_free (list);
2505
2506 if (! utf8_key)
2507 {
2508 /* Remove the key and keep just the label. */
2509 g_object_ref (G_OBJECT (wlbl));
2510 gtk_container_remove (GTK_CONTAINER (w), wchild);
2511 gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl));
2512 g_object_unref (G_OBJECT (wlbl));
2513 wkey = 0;
2514 }
2515
2516 }
2517 else /* Just a label. */
2518 {
2519 wlbl = GTK_LABEL (wchild);
2520
2521 /* Check if there is now a key. */
2522 if (utf8_key)
2523 {
2524 GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key);
2525 GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd));
2526
2527 wlbl = GTK_LABEL (list->data);
2528 wkey = GTK_LABEL (list->next->data);
2529 g_list_free (list);
2530
2531 gtk_container_remove (GTK_CONTAINER (w), wchild);
2532 gtk_container_add (GTK_CONTAINER (w), wtoadd);
2533 }
2534 }
2535
2536
2537 if (wkey) old_key = gtk_label_get_label (wkey);
2538 if (wlbl) old_label = gtk_label_get_label (wlbl);
2539
2540 if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0))
2541 gtk_label_set_text (wkey, utf8_key);
2542
2543 if (! old_label || strcmp (utf8_label, old_label) != 0)
2544 gtk_label_set_text (wlbl, utf8_label);
2545
2546 if (utf8_key && utf8_key != val->key) g_free (utf8_key);
2547 if (utf8_label && utf8_label != val->name) g_free (utf8_label);
2548
2549 if (! val->enabled && gtk_widget_get_sensitive (w))
2550 gtk_widget_set_sensitive (w, FALSE);
2551 else if (val->enabled && ! gtk_widget_get_sensitive (w))
2552 gtk_widget_set_sensitive (w, TRUE);
2553
2554 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w),
2555 XG_ITEM_DATA);
2556 if (cb_data)
2557 {
2558 cb_data->call_data = val->call_data;
2559 cb_data->help = val->help;
2560 cb_data->cl_data = cl_data;
2561
2562 /* We assume the callback functions don't change. */
2563 if (val->call_data && ! val->contents)
2564 {
2565 /* This item shall have a select callback. */
2566 if (! cb_data->select_id)
2567 cb_data->select_id
2568 = g_signal_connect (G_OBJECT (w), "activate",
2569 select_cb, cb_data);
2570 }
2571 else if (cb_data->select_id)
2572 {
2573 g_signal_handler_disconnect (w, cb_data->select_id);
2574 cb_data->select_id = 0;
2575 }
2576 }
2577 }
2578
2579 /* Update the toggle menu item W so it corresponds to VAL. */
2580
2581 static void
2582 xg_update_toggle_item (widget_value *val, GtkWidget *w)
2583 {
2584 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2585 }
2586
2587 /* Update the radio menu item W so it corresponds to VAL. */
2588
2589 static void
2590 xg_update_radio_item (widget_value *val, GtkWidget *w)
2591 {
2592 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected);
2593 }
2594
2595 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
2596 SUBMENU may be NULL, in that case a new menu is created.
2597 F is the frame the menu bar belongs to.
2598 VAL describes the contents of the menu bar.
2599 SELECT_CB is the callback to use when a menu item is selected.
2600 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2601 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
2602 CL_DATA is the call back data to use for any newly created items.
2603
2604 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
2605 was NULL. */
2606
2607 static GtkWidget *
2608 xg_update_submenu (GtkWidget *submenu,
2609 FRAME_PTR f,
2610 widget_value *val,
2611 GCallback select_cb,
2612 GCallback deactivate_cb,
2613 GCallback highlight_cb,
2614 xg_menu_cb_data *cl_data)
2615 {
2616 GtkWidget *newsub = submenu;
2617 GList *list = 0;
2618 GList *iter;
2619 widget_value *cur;
2620 int has_tearoff_p = 0;
2621 GList *first_radio = 0;
2622
2623 if (submenu)
2624 list = gtk_container_get_children (GTK_CONTAINER (submenu));
2625
2626 for (cur = val, iter = list;
2627 cur && iter;
2628 iter = g_list_next (iter), cur = cur->next)
2629 {
2630 GtkWidget *w = GTK_WIDGET (iter->data);
2631
2632 /* Skip tearoff items, they have no counterpart in val. */
2633 if (GTK_IS_TEAROFF_MENU_ITEM (w))
2634 {
2635 has_tearoff_p = 1;
2636 iter = g_list_next (iter);
2637 if (iter) w = GTK_WIDGET (iter->data);
2638 else break;
2639 }
2640
2641 /* Remember first radio button in a group. If we get a mismatch in
2642 a radio group we must rebuild the whole group so that the connections
2643 in GTK becomes correct. */
2644 if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio)
2645 first_radio = iter;
2646 else if (cur->button_type != BUTTON_TYPE_RADIO
2647 && ! GTK_IS_RADIO_MENU_ITEM (w))
2648 first_radio = 0;
2649
2650 if (GTK_IS_SEPARATOR_MENU_ITEM (w))
2651 {
2652 if (! xg_separator_p (cur->name))
2653 break;
2654 }
2655 else if (GTK_IS_CHECK_MENU_ITEM (w))
2656 {
2657 if (cur->button_type != BUTTON_TYPE_TOGGLE)
2658 break;
2659 xg_update_toggle_item (cur, w);
2660 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2661 }
2662 else if (GTK_IS_RADIO_MENU_ITEM (w))
2663 {
2664 if (cur->button_type != BUTTON_TYPE_RADIO)
2665 break;
2666 xg_update_radio_item (cur, w);
2667 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2668 }
2669 else if (GTK_IS_MENU_ITEM (w))
2670 {
2671 GtkMenuItem *witem = GTK_MENU_ITEM (w);
2672 GtkWidget *sub;
2673
2674 if (cur->button_type != BUTTON_TYPE_NONE ||
2675 xg_separator_p (cur->name))
2676 break;
2677
2678 xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data);
2679
2680 sub = gtk_menu_item_get_submenu (witem);
2681 if (sub && ! cur->contents)
2682 {
2683 /* Not a submenu anymore. */
2684 g_object_ref (G_OBJECT (sub));
2685 remove_submenu (witem);
2686 gtk_widget_destroy (sub);
2687 }
2688 else if (cur->contents)
2689 {
2690 GtkWidget *nsub;
2691
2692 nsub = xg_update_submenu (sub, f, cur->contents,
2693 select_cb, deactivate_cb,
2694 highlight_cb, cl_data);
2695
2696 /* If this item just became a submenu, we must set it. */
2697 if (nsub != sub)
2698 gtk_menu_item_set_submenu (witem, nsub);
2699 }
2700 }
2701 else
2702 {
2703 /* Structural difference. Remove everything from here and down
2704 in SUBMENU. */
2705 break;
2706 }
2707 }
2708
2709 /* Remove widgets from first structual change. */
2710 if (iter)
2711 {
2712 /* If we are adding new menu items below, we must remove from
2713 first radio button so that radio groups become correct. */
2714 if (cur && first_radio) xg_destroy_widgets (first_radio);
2715 else xg_destroy_widgets (iter);
2716 }
2717
2718 if (cur)
2719 {
2720 /* More items added. Create them. */
2721 newsub = create_menus (cur,
2722 f,
2723 select_cb,
2724 deactivate_cb,
2725 highlight_cb,
2726 0,
2727 0,
2728 ! has_tearoff_p,
2729 submenu,
2730 cl_data,
2731 0);
2732 }
2733
2734 if (list) g_list_free (list);
2735
2736 return newsub;
2737 }
2738
2739 /* Update the MENUBAR.
2740 F is the frame the menu bar belongs to.
2741 VAL describes the contents of the menu bar.
2742 If DEEP_P is non-zero, rebuild all but the top level menu names in
2743 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2744 SELECT_CB is the callback to use when a menu item is selected.
2745 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2746 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2747
2748 void
2749 xg_modify_menubar_widgets (GtkWidget *menubar, FRAME_PTR f, widget_value *val,
2750 int deep_p,
2751 GCallback select_cb, GCallback deactivate_cb,
2752 GCallback highlight_cb)
2753 {
2754 xg_menu_cb_data *cl_data;
2755 GList *list = gtk_container_get_children (GTK_CONTAINER (menubar));
2756
2757 if (! list) return;
2758
2759 cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar),
2760 XG_FRAME_DATA);
2761
2762 xg_update_menubar (menubar, f, &list, list, 0, val->contents,
2763 select_cb, deactivate_cb, highlight_cb, cl_data);
2764
2765 if (deep_p)
2766 {
2767 widget_value *cur;
2768
2769 /* Update all sub menus.
2770 We must keep the submenus (GTK menu item widgets) since the
2771 X Window in the XEvent that activates the menu are those widgets. */
2772
2773 /* Update cl_data, menu_item things in F may have changed. */
2774 update_cl_data (cl_data, f, highlight_cb);
2775
2776 for (cur = val->contents; cur; cur = cur->next)
2777 {
2778 GList *iter;
2779 GtkWidget *sub = 0;
2780 GtkWidget *newsub;
2781 GtkMenuItem *witem = 0;
2782
2783 /* Find sub menu that corresponds to val and update it. */
2784 for (iter = list ; iter; iter = g_list_next (iter))
2785 {
2786 witem = GTK_MENU_ITEM (iter->data);
2787 if (xg_item_label_same_p (witem, cur->name))
2788 {
2789 sub = gtk_menu_item_get_submenu (witem);
2790 break;
2791 }
2792 }
2793
2794 newsub = xg_update_submenu (sub,
2795 f,
2796 cur->contents,
2797 select_cb,
2798 deactivate_cb,
2799 highlight_cb,
2800 cl_data);
2801 /* sub may still be NULL. If we just updated non deep and added
2802 a new menu bar item, it has no sub menu yet. So we set the
2803 newly created sub menu under witem. */
2804 if (newsub != sub && witem != 0)
2805 {
2806 xg_set_screen (newsub, f);
2807 gtk_menu_item_set_submenu (witem, newsub);
2808 }
2809 }
2810 }
2811
2812 g_list_free (list);
2813 gtk_widget_show_all (menubar);
2814 }
2815
2816 /* Recompute all the widgets of frame F, when the menu bar has been
2817 changed. Value is non-zero if widgets were updated. */
2818
2819 int
2820 xg_update_frame_menubar (FRAME_PTR f)
2821 {
2822 struct x_output *x = f->output_data.x;
2823 GtkRequisition req;
2824
2825 if (!x->menubar_widget || gtk_widget_get_mapped (x->menubar_widget))
2826 return 0;
2827
2828 if (x->menubar_widget && gtk_widget_get_parent (x->menubar_widget))
2829 return 0; /* Already done this, happens for frames created invisible. */
2830
2831 BLOCK_INPUT;
2832
2833 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget,
2834 FALSE, FALSE, 0);
2835 gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0);
2836
2837 gtk_widget_show_all (x->menubar_widget);
2838 gtk_widget_size_request (x->menubar_widget, &req);
2839 FRAME_MENUBAR_HEIGHT (f) = req.height;
2840 xg_height_or_width_changed (f);
2841 UNBLOCK_INPUT;
2842
2843 return 1;
2844 }
2845
2846 /* Get rid of the menu bar of frame F, and free its storage.
2847 This is used when deleting a frame, and when turning off the menu bar. */
2848
2849 void
2850 free_frame_menubar (FRAME_PTR f)
2851 {
2852 struct x_output *x = f->output_data.x;
2853
2854 if (x->menubar_widget)
2855 {
2856 BLOCK_INPUT;
2857
2858 gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget);
2859 /* The menubar and its children shall be deleted when removed from
2860 the container. */
2861 x->menubar_widget = 0;
2862 FRAME_MENUBAR_HEIGHT (f) = 0;
2863 xg_height_or_width_changed (f);
2864 UNBLOCK_INPUT;
2865 }
2866 }
2867
2868 int
2869 xg_event_is_for_menubar (FRAME_PTR f, XEvent *event)
2870 {
2871 struct x_output *x = f->output_data.x;
2872 GList *iter;
2873 GdkRectangle rec;
2874 GList *list;
2875 GdkDisplay *gdpy;
2876 GdkWindow *gw;
2877 GdkEvent gevent;
2878 GtkWidget *gwdesc;
2879
2880 if (! x->menubar_widget) return 0;
2881
2882 if (! (event->xbutton.x >= 0
2883 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
2884 && event->xbutton.y >= 0
2885 && event->xbutton.y < f->output_data.x->menubar_height
2886 && event->xbutton.same_screen))
2887 return 0;
2888
2889 gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
2890 gw = gdk_xid_table_lookup_for_display (gdpy, event->xbutton.window);
2891 if (! gw) return 0;
2892 gevent.any.window = gw;
2893 gwdesc = gtk_get_event_widget (&gevent);
2894 if (! gwdesc) return 0;
2895 if (! GTK_IS_MENU_BAR (gwdesc)
2896 && ! GTK_IS_MENU_ITEM (gwdesc)
2897 && ! gtk_widget_is_ancestor (x->menubar_widget, gwdesc))
2898 return 0;
2899
2900 list = gtk_container_get_children (GTK_CONTAINER (x->menubar_widget));
2901 if (! list) return 0;
2902 rec.x = event->xbutton.x;
2903 rec.y = event->xbutton.y;
2904 rec.width = 1;
2905 rec.height = 1;
2906
2907 for (iter = list ; iter; iter = g_list_next (iter))
2908 {
2909 GtkWidget *w = GTK_WIDGET (iter->data);
2910 if (gtk_widget_get_mapped (w) && gtk_widget_intersect (w, &rec, NULL))
2911 break;
2912 }
2913 g_list_free (list);
2914 return iter == 0 ? 0 : 1;
2915 }
2916
2917
2918 \f
2919 /***********************************************************************
2920 Scroll bar functions
2921 ***********************************************************************/
2922
2923
2924 /* Setting scroll bar values invokes the callback. Use this variable
2925 to indicate that callback should do nothing. */
2926
2927 int xg_ignore_gtk_scrollbar;
2928
2929 /* Xlib's `Window' fits in 32 bits. But we want to store pointers, and they
2930 may be larger than 32 bits. Keep a mapping from integer index to widget
2931 pointers to get around the 32 bit limitation. */
2932
2933 static struct
2934 {
2935 GtkWidget **widgets;
2936 int max_size;
2937 int used;
2938 } id_to_widget;
2939
2940 /* Grow this much every time we need to allocate more */
2941
2942 #define ID_TO_WIDGET_INCR 32
2943
2944 /* Store the widget pointer W in id_to_widget and return the integer index. */
2945
2946 static int
2947 xg_store_widget_in_map (GtkWidget *w)
2948 {
2949 int i;
2950
2951 if (id_to_widget.max_size == id_to_widget.used)
2952 {
2953 int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR;
2954
2955 id_to_widget.widgets = xrealloc (id_to_widget.widgets,
2956 sizeof (GtkWidget *)*new_size);
2957
2958 for (i = id_to_widget.max_size; i < new_size; ++i)
2959 id_to_widget.widgets[i] = 0;
2960 id_to_widget.max_size = new_size;
2961 }
2962
2963 /* Just loop over the array and find a free place. After all,
2964 how many scroll bars are we creating? Should be a small number.
2965 The check above guarantees we will find a free place. */
2966 for (i = 0; i < id_to_widget.max_size; ++i)
2967 {
2968 if (! id_to_widget.widgets[i])
2969 {
2970 id_to_widget.widgets[i] = w;
2971 ++id_to_widget.used;
2972
2973 return i;
2974 }
2975 }
2976
2977 /* Should never end up here */
2978 abort ();
2979 }
2980
2981 /* Remove pointer at IDX from id_to_widget.
2982 Called when scroll bar is destroyed. */
2983
2984 static void
2985 xg_remove_widget_from_map (int idx)
2986 {
2987 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
2988 {
2989 id_to_widget.widgets[idx] = 0;
2990 --id_to_widget.used;
2991 }
2992 }
2993
2994 /* Get the widget pointer at IDX from id_to_widget. */
2995
2996 static GtkWidget *
2997 xg_get_widget_from_map (int idx)
2998 {
2999 if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0)
3000 return id_to_widget.widgets[idx];
3001
3002 return 0;
3003 }
3004
3005 /* Return the scrollbar id for X Window WID on display DPY.
3006 Return -1 if WID not in id_to_widget. */
3007
3008 int
3009 xg_get_scroll_id_for_window (Display *dpy, Window wid)
3010 {
3011 int idx;
3012 GtkWidget *w;
3013
3014 w = xg_win_to_widget (dpy, wid);
3015
3016 if (w)
3017 {
3018 for (idx = 0; idx < id_to_widget.max_size; ++idx)
3019 if (id_to_widget.widgets[idx] == w)
3020 return idx;
3021 }
3022
3023 return -1;
3024 }
3025
3026 /* Callback invoked when scroll bar WIDGET is destroyed.
3027 DATA is the index into id_to_widget for WIDGET.
3028 We free pointer to last scroll bar values here and remove the index. */
3029
3030 static void
3031 xg_gtk_scroll_destroy (GtkWidget *widget, gpointer data)
3032 {
3033 int id = (int) (EMACS_INT) data; /* The EMACS_INT cast avoids a warning. */
3034 xg_remove_widget_from_map (id);
3035 }
3036
3037 /* Create a scroll bar widget for frame F. Store the scroll bar
3038 in BAR.
3039 SCROLL_CALLBACK is the callback to invoke when the value of the
3040 bar changes.
3041 END_CALLBACK is the callback to invoke when scrolling ends.
3042 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
3043 to set resources for the widget. */
3044
3045 void
3046 xg_create_scroll_bar (FRAME_PTR f,
3047 struct scroll_bar *bar,
3048 GCallback scroll_callback,
3049 GCallback end_callback,
3050 char *scroll_bar_name)
3051 {
3052 GtkWidget *wscroll;
3053 GtkWidget *webox;
3054 GtkObject *vadj;
3055 int scroll_id;
3056
3057 /* Page, step increment values are not so important here, they
3058 will be corrected in x_set_toolkit_scroll_bar_thumb. */
3059 vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX,
3060 0.1, 0.1, 0.1);
3061
3062 wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj));
3063 webox = gtk_event_box_new ();
3064 gtk_widget_set_name (wscroll, scroll_bar_name);
3065 gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS);
3066 g_object_set_data (G_OBJECT (wscroll), XG_FRAME_DATA, (gpointer)f);
3067
3068 scroll_id = xg_store_widget_in_map (wscroll);
3069
3070 /* The EMACS_INT cast avoids a warning. */
3071 g_signal_connect (G_OBJECT (wscroll),
3072 "destroy",
3073 G_CALLBACK (xg_gtk_scroll_destroy),
3074 (gpointer) (EMACS_INT) scroll_id);
3075 g_signal_connect (G_OBJECT (wscroll),
3076 "change-value",
3077 scroll_callback,
3078 (gpointer) bar);
3079 g_signal_connect (G_OBJECT (wscroll),
3080 "button-release-event",
3081 end_callback,
3082 (gpointer) bar);
3083
3084 /* The scroll bar widget does not draw on a window of its own. Instead
3085 it draws on the parent window, in this case the edit widget. So
3086 whenever the edit widget is cleared, the scroll bar needs to redraw
3087 also, which causes flicker. Put an event box between the edit widget
3088 and the scroll bar, so the scroll bar instead draws itself on the
3089 event box window. */
3090 gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), webox, -1, -1);
3091 gtk_container_add (GTK_CONTAINER (webox), wscroll);
3092
3093
3094 /* Set the cursor to an arrow. */
3095 xg_set_cursor (webox, FRAME_X_DISPLAY_INFO (f)->xg_cursor);
3096
3097 bar->x_window = scroll_id;
3098 }
3099
3100 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
3101
3102 void
3103 xg_remove_scroll_bar (FRAME_PTR f, int scrollbar_id)
3104 {
3105 GtkWidget *w = xg_get_widget_from_map (scrollbar_id);
3106 if (w)
3107 {
3108 GtkWidget *wparent = gtk_widget_get_parent (w);
3109 gtk_widget_destroy (w);
3110 gtk_widget_destroy (wparent);
3111 SET_FRAME_GARBAGED (f);
3112 }
3113 }
3114
3115 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
3116 in frame F.
3117 TOP/LEFT are the new pixel positions where the bar shall appear.
3118 WIDTH, HEIGHT is the size in pixels the bar shall have. */
3119
3120 void
3121 xg_update_scrollbar_pos (FRAME_PTR f,
3122 int scrollbar_id,
3123 int top,
3124 int left,
3125 int width,
3126 int height)
3127 {
3128
3129 GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id);
3130
3131 if (wscroll)
3132 {
3133 GtkWidget *wfixed = f->output_data.x->edit_widget;
3134 GtkWidget *wparent = gtk_widget_get_parent (wscroll);
3135 gint msl;
3136
3137 /* Clear out old position. */
3138 int oldx = -1, oldy = -1, oldw, oldh;
3139 if (gtk_widget_get_parent (wparent) == wfixed)
3140 {
3141 gtk_container_child_get (GTK_CONTAINER (wfixed), wparent,
3142 "x", &oldx, "y", &oldy, NULL);
3143 gtk_widget_get_size_request (wscroll, &oldw, &oldh);
3144 }
3145
3146 /* Move and resize to new values. */
3147 gtk_fixed_move (GTK_FIXED (wfixed), wparent, left, top);
3148 gtk_widget_style_get (wscroll, "min-slider-length", &msl, NULL);
3149 if (msl > height)
3150 {
3151 /* No room. Hide scroll bar as some themes output a warning if
3152 the height is less than the min size. */
3153 gtk_widget_hide (wparent);
3154 gtk_widget_hide (wscroll);
3155 }
3156 else
3157 {
3158 gtk_widget_show_all (wparent);
3159 gtk_widget_set_size_request (wscroll, width, height);
3160 }
3161 gtk_widget_queue_draw (wfixed);
3162 gdk_window_process_all_updates ();
3163 if (oldx != -1 && oldw > 0 && oldh > 0)
3164 {
3165 /* Clear under old scroll bar position. This must be done after
3166 the gtk_widget_queue_draw and gdk_window_process_all_updates
3167 above. */
3168 x_clear_area (FRAME_X_DISPLAY (f),
3169 FRAME_X_WINDOW (f),
3170 oldx, oldy, oldw, oldh, 0);
3171 }
3172
3173 /* GTK does not redraw until the main loop is entered again, but
3174 if there are no X events pending we will not enter it. So we sync
3175 here to get some events. */
3176
3177 x_sync (f);
3178 SET_FRAME_GARBAGED (f);
3179 cancel_mouse_face (f);
3180 }
3181 }
3182
3183 /* Set the thumb size and position of scroll bar BAR. We are currently
3184 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3185
3186 void
3187 xg_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar,
3188 int portion,
3189 int position,
3190 int whole)
3191 {
3192 GtkWidget *wscroll = xg_get_widget_from_map (bar->x_window);
3193
3194 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3195
3196 if (wscroll && NILP (bar->dragging))
3197 {
3198 GtkAdjustment *adj;
3199 gdouble shown;
3200 gdouble top;
3201 int size, value;
3202 int new_step;
3203 int changed = 0;
3204
3205 adj = gtk_range_get_adjustment (GTK_RANGE (wscroll));
3206
3207 /* We do the same as for MOTIF in xterm.c, assume 30 chars per line
3208 rather than the real portion value. This makes the thumb less likely
3209 to resize and that looks better. */
3210 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
3211 /* When the thumb is at the bottom, position == whole.
3212 So we need to increase `whole' to make space for the thumb. */
3213 whole += portion;
3214
3215 if (whole <= 0)
3216 top = 0, shown = 1;
3217 else
3218 {
3219 top = (gdouble) position / whole;
3220 shown = (gdouble) portion / whole;
3221 }
3222
3223 size = shown * XG_SB_RANGE;
3224 size = min (size, XG_SB_RANGE);
3225 size = max (size, 1);
3226
3227 value = top * XG_SB_RANGE;
3228 value = min (value, XG_SB_MAX - size);
3229 value = max (value, XG_SB_MIN);
3230
3231 /* Assume all lines are of equal size. */
3232 new_step = size / max (1, FRAME_LINES (f));
3233
3234 if ((int) gtk_adjustment_get_page_size (adj) != size
3235 || (int) gtk_adjustment_get_step_increment (adj) != new_step)
3236 {
3237 gtk_adjustment_set_page_size (adj, size);
3238 gtk_adjustment_set_step_increment (adj, new_step);
3239 /* Assume a page increment is about 95% of the page size */
3240 gtk_adjustment_set_page_increment (adj,(int) (0.95*size));
3241 changed = 1;
3242 }
3243
3244 if (changed || (int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3245 {
3246 BLOCK_INPUT;
3247
3248 /* gtk_range_set_value invokes the callback. Set
3249 ignore_gtk_scrollbar to make the callback do nothing */
3250 xg_ignore_gtk_scrollbar = 1;
3251
3252 if ((int) gtk_range_get_value (GTK_RANGE (wscroll)) != value)
3253 gtk_range_set_value (GTK_RANGE (wscroll), (gdouble)value);
3254 else if (changed)
3255 gtk_adjustment_changed (adj);
3256
3257 xg_ignore_gtk_scrollbar = 0;
3258
3259 UNBLOCK_INPUT;
3260 }
3261 }
3262 }
3263
3264 /* Return non-zero if EVENT is for a scroll bar in frame F.
3265 When the same X window is used for several Gtk+ widgets, we cannot
3266 say for sure based on the X window alone if an event is for the
3267 frame. This function does additional checks.
3268
3269 Return non-zero if the event is for a scroll bar, zero otherwise. */
3270
3271 int
3272 xg_event_is_for_scrollbar (FRAME_PTR f, XEvent *event)
3273 {
3274 int retval = 0;
3275
3276 if (f && event->type == ButtonPress && event->xbutton.button < 4)
3277 {
3278 /* Check if press occurred outside the edit widget. */
3279 GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
3280 retval = gdk_display_get_window_at_pointer (gdpy, NULL, NULL)
3281 != gtk_widget_get_window (f->output_data.x->edit_widget);
3282 }
3283 else if (f
3284 && ((event->type == ButtonRelease && event->xbutton.button < 4)
3285 || event->type == MotionNotify))
3286 {
3287 /* If we are releasing or moving the scroll bar, it has the grab. */
3288 GtkWidget *w = gtk_grab_get_current ();
3289 retval = w != 0 && GTK_IS_SCROLLBAR (w);
3290 }
3291
3292 return retval;
3293 }
3294
3295
3296 \f
3297 /***********************************************************************
3298 Tool bar functions
3299 ***********************************************************************/
3300 /* The key for the data we put in the GtkImage widgets. The data is
3301 the image used by Emacs. We use this to see if we need to update
3302 the GtkImage with a new image. */
3303 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
3304
3305 /* The key for storing the latest modifiers so the activate callback can
3306 get them. */
3307 #define XG_TOOL_BAR_LAST_MODIFIER "emacs-tool-bar-modifier"
3308
3309 /* The key for storing the button widget in its proxy menu item. */
3310 #define XG_TOOL_BAR_PROXY_BUTTON "emacs-tool-bar-proxy-button"
3311
3312 /* The key for the data we put in the GtkImage widgets. The data is
3313 the stock name used by Emacs. We use this to see if we need to update
3314 the GtkImage with a new image. */
3315 #define XG_TOOL_BAR_STOCK_NAME "emacs-tool-bar-stock-name"
3316
3317 /* As above, but this is used for named theme widgets, as opposed to
3318 stock items. */
3319 #define XG_TOOL_BAR_ICON_NAME "emacs-tool-bar-icon-name"
3320
3321 /* Callback function invoked when a tool bar item is pressed.
3322 W is the button widget in the tool bar that got pressed,
3323 CLIENT_DATA is an integer that is the index of the button in the
3324 tool bar. 0 is the first button. */
3325
3326 static gboolean
3327 xg_tool_bar_button_cb (GtkWidget *widget,
3328 GdkEventButton *event,
3329 gpointer user_data)
3330 {
3331 /* Casts to avoid warnings when gpointer is 64 bits and int is 32 bits */
3332 gpointer ptr = (gpointer) (EMACS_INT) event->state;
3333 g_object_set_data (G_OBJECT (widget), XG_TOOL_BAR_LAST_MODIFIER, ptr);
3334 return FALSE;
3335 }
3336
3337
3338 /* Callback function invoked when a tool bar item is pressed.
3339 W is the button widget in the tool bar that got pressed,
3340 CLIENT_DATA is an integer that is the index of the button in the
3341 tool bar. 0 is the first button. */
3342
3343 static void
3344 xg_tool_bar_callback (GtkWidget *w, gpointer client_data)
3345 {
3346 /* The EMACS_INT cast avoids a warning. */
3347 int idx = (int) (EMACS_INT) client_data;
3348 int mod = (int) (EMACS_INT) g_object_get_data (G_OBJECT (w),
3349 XG_TOOL_BAR_LAST_MODIFIER);
3350
3351 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3352 Lisp_Object key, frame;
3353 struct input_event event;
3354 EVENT_INIT (event);
3355
3356 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3357 return;
3358
3359 idx *= TOOL_BAR_ITEM_NSLOTS;
3360
3361 key = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_KEY);
3362 XSETFRAME (frame, f);
3363
3364 /* We generate two events here. The first one is to set the prefix
3365 to `(tool_bar)', see keyboard.c. */
3366 event.kind = TOOL_BAR_EVENT;
3367 event.frame_or_window = frame;
3368 event.arg = frame;
3369 kbd_buffer_store_event (&event);
3370
3371 event.kind = TOOL_BAR_EVENT;
3372 event.frame_or_window = frame;
3373 event.arg = key;
3374 /* Convert between the modifier bits GDK uses and the modifier bits
3375 Emacs uses. This assumes GDK and X masks are the same, which they are when
3376 this is written. */
3377 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), mod);
3378 kbd_buffer_store_event (&event);
3379
3380 /* Return focus to the frame after we have clicked on a detached
3381 tool bar button. */
3382 Fx_focus_frame (frame);
3383 }
3384
3385 /* Callback function invoked when a tool bar item is pressed in a detached
3386 tool bar or the overflow drop down menu.
3387 We just call xg_tool_bar_callback.
3388 W is the menu item widget that got pressed,
3389 CLIENT_DATA is an integer that is the index of the button in the
3390 tool bar. 0 is the first button. */
3391
3392 static void
3393 xg_tool_bar_proxy_callback (GtkWidget *w, gpointer client_data)
3394 {
3395 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3396 XG_TOOL_BAR_PROXY_BUTTON));
3397 xg_tool_bar_callback (wbutton, client_data);
3398 }
3399
3400
3401 static gboolean
3402 xg_tool_bar_help_callback (GtkWidget *w,
3403 GdkEventCrossing *event,
3404 gpointer client_data);
3405
3406 /* This callback is called when a help is to be shown for an item in
3407 the detached tool bar when the detached tool bar it is not expanded. */
3408
3409 static gboolean
3410 xg_tool_bar_proxy_help_callback (GtkWidget *w,
3411 GdkEventCrossing *event,
3412 gpointer client_data)
3413 {
3414 GtkWidget *wbutton = GTK_WIDGET (g_object_get_data (G_OBJECT (w),
3415 XG_TOOL_BAR_PROXY_BUTTON));
3416
3417 return xg_tool_bar_help_callback (wbutton, event, client_data);
3418 }
3419
3420 static GtkWidget *
3421 xg_get_tool_bar_widgets (GtkWidget *vb, GtkWidget **wimage)
3422 {
3423 GList *clist = gtk_container_get_children (GTK_CONTAINER (vb));
3424 GtkWidget *c1 = (GtkWidget *) clist->data;
3425 GtkWidget *c2 = (GtkWidget *) clist->next->data;
3426 *wimage = GTK_IS_IMAGE (c1) ? c1 : c2;
3427 g_list_free (clist);
3428 return GTK_IS_LABEL (c1) ? c1 : c2;
3429 }
3430
3431
3432 /* This callback is called when a tool item should create a proxy item,
3433 such as for the overflow menu. Also called when the tool bar is detached.
3434 If we don't create a proxy menu item, the detached tool bar will be
3435 blank. */
3436
3437 static gboolean
3438 xg_tool_bar_menu_proxy (GtkToolItem *toolitem, gpointer user_data)
3439 {
3440 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (toolitem));
3441 GtkButton *wbutton = GTK_BUTTON (gtk_bin_get_child (GTK_BIN (weventbox)));
3442 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
3443 GtkWidget *c1;
3444 GtkLabel *wlbl = GTK_LABEL (xg_get_tool_bar_widgets (vb, &c1));
3445 GtkImage *wimage = GTK_IMAGE (c1);
3446 GtkWidget *wmenuitem = gtk_image_menu_item_new_with_label
3447 (gtk_label_get_text (wlbl));
3448 GtkWidget *wmenuimage;
3449
3450
3451 if (gtk_button_get_use_stock (wbutton))
3452 wmenuimage = gtk_image_new_from_stock (gtk_button_get_label (wbutton),
3453 GTK_ICON_SIZE_MENU);
3454 else
3455 {
3456 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (wbutton));
3457 GtkImageType store_type = gtk_image_get_storage_type (wimage);
3458
3459 if (store_type == GTK_IMAGE_STOCK)
3460 {
3461 gchar *stock_id;
3462 gtk_image_get_stock (wimage, &stock_id, NULL);
3463 wmenuimage = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
3464 }
3465 else if (store_type == GTK_IMAGE_ICON_SET)
3466 {
3467 GtkIconSet *icon_set;
3468 gtk_image_get_icon_set (wimage, &icon_set, NULL);
3469 wmenuimage = gtk_image_new_from_icon_set (icon_set,
3470 GTK_ICON_SIZE_MENU);
3471 }
3472 else if (store_type == GTK_IMAGE_PIXBUF)
3473 {
3474 gint width, height;
3475
3476 if (settings &&
3477 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
3478 &width, &height))
3479 {
3480 GdkPixbuf *src_pixbuf, *dest_pixbuf;
3481
3482 src_pixbuf = gtk_image_get_pixbuf (wimage);
3483 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
3484 GDK_INTERP_BILINEAR);
3485
3486 wmenuimage = gtk_image_new_from_pixbuf (dest_pixbuf);
3487 }
3488 else
3489 {
3490 fprintf (stderr, "internal error: GTK_IMAGE_PIXBUF failed\n");
3491 abort ();
3492 }
3493 }
3494 else if (store_type == GTK_IMAGE_ICON_NAME)
3495 {
3496 const gchar *icon_name;
3497 GtkIconSize icon_size;
3498
3499 gtk_image_get_icon_name (wimage, &icon_name, &icon_size);
3500 wmenuimage = gtk_image_new_from_icon_name (icon_name,
3501 GTK_ICON_SIZE_MENU);
3502 }
3503 else
3504 {
3505 fprintf (stderr, "internal error: store_type is %d\n", store_type);
3506 abort ();
3507 }
3508 }
3509 if (wmenuimage)
3510 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (wmenuitem), wmenuimage);
3511
3512 g_signal_connect (G_OBJECT (wmenuitem),
3513 "activate",
3514 G_CALLBACK (xg_tool_bar_proxy_callback),
3515 user_data);
3516
3517
3518 g_object_set_data (G_OBJECT (wmenuitem), XG_TOOL_BAR_PROXY_BUTTON,
3519 (gpointer) wbutton);
3520 gtk_tool_item_set_proxy_menu_item (toolitem, "Emacs toolbar item", wmenuitem);
3521 gtk_widget_set_sensitive (wmenuitem,
3522 gtk_widget_get_sensitive (GTK_WIDGET (wbutton)));
3523
3524 /* Use enter/leave notify to show help. We use the events
3525 rather than the GtkButton specific signals "enter" and
3526 "leave", so we can have only one callback. The event
3527 will tell us what kind of event it is. */
3528 g_signal_connect (G_OBJECT (wmenuitem),
3529 "enter-notify-event",
3530 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3531 user_data);
3532 g_signal_connect (G_OBJECT (wmenuitem),
3533 "leave-notify-event",
3534 G_CALLBACK (xg_tool_bar_proxy_help_callback),
3535 user_data);
3536
3537 return TRUE;
3538 }
3539
3540 /* This callback is called when a tool bar is detached. We must set
3541 the height of the tool bar to zero when this happens so frame sizes
3542 are correctly calculated.
3543 WBOX is the handle box widget that enables detach/attach of the tool bar.
3544 W is the tool bar widget.
3545 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3546
3547 static void
3548 xg_tool_bar_detach_callback (GtkHandleBox *wbox,
3549 GtkWidget *w,
3550 gpointer client_data)
3551 {
3552 FRAME_PTR f = (FRAME_PTR) client_data;
3553 extern int x_gtk_whole_detached_tool_bar;
3554
3555 g_object_set (G_OBJECT (w), "show-arrow", !x_gtk_whole_detached_tool_bar,
3556 NULL);
3557
3558 if (f)
3559 {
3560 GtkRequisition req, req2;
3561 FRAME_X_OUTPUT (f)->toolbar_detached = 1;
3562 gtk_widget_size_request (GTK_WIDGET (wbox), &req);
3563 gtk_widget_size_request (w, &req2);
3564 req.width -= req2.width;
3565 req.height -= req2.height;
3566 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3567 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3568 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3569 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3570 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3571 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3572 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3573 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3574 xg_height_or_width_changed (f);
3575 }
3576 }
3577
3578 /* This callback is called when a tool bar is reattached. We must set
3579 the height of the tool bar when this happens so frame sizes
3580 are correctly calculated.
3581 WBOX is the handle box widget that enables detach/attach of the tool bar.
3582 W is the tool bar widget.
3583 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
3584
3585 static void
3586 xg_tool_bar_attach_callback (GtkHandleBox *wbox,
3587 GtkWidget *w,
3588 gpointer client_data)
3589 {
3590 FRAME_PTR f = (FRAME_PTR) client_data;
3591 g_object_set (G_OBJECT (w), "show-arrow", TRUE, NULL);
3592
3593 if (f)
3594 {
3595 GtkRequisition req, req2;
3596 FRAME_X_OUTPUT (f)->toolbar_detached = 0;
3597 gtk_widget_size_request (GTK_WIDGET (wbox), &req);
3598 gtk_widget_size_request (w, &req2);
3599 req.width += req2.width;
3600 req.height += req2.height;
3601 if (FRAME_TOOLBAR_TOP_HEIGHT (f) != 0)
3602 FRAME_TOOLBAR_TOP_HEIGHT (f) = req.height;
3603 else if (FRAME_TOOLBAR_BOTTOM_HEIGHT (f) != 0)
3604 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = req.height;
3605 else if (FRAME_TOOLBAR_RIGHT_WIDTH (f) != 0)
3606 FRAME_TOOLBAR_RIGHT_WIDTH (f) = req.width;
3607 else if (FRAME_TOOLBAR_LEFT_WIDTH (f) != 0)
3608 FRAME_TOOLBAR_LEFT_WIDTH (f) = req.width;
3609 xg_height_or_width_changed (f);
3610 }
3611 }
3612
3613 /* This callback is called when the mouse enters or leaves a tool bar item.
3614 It is used for displaying and hiding the help text.
3615 W is the tool bar item, a button.
3616 EVENT is either an enter event or leave event.
3617 CLIENT_DATA is an integer that is the index of the button in the
3618 tool bar. 0 is the first button.
3619
3620 Returns FALSE to tell GTK to keep processing this event. */
3621
3622 static gboolean
3623 xg_tool_bar_help_callback (GtkWidget *w,
3624 GdkEventCrossing *event,
3625 gpointer client_data)
3626 {
3627 /* The EMACS_INT cast avoids a warning. */
3628 int idx = (int) (EMACS_INT) client_data;
3629 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (w), XG_FRAME_DATA);
3630 Lisp_Object help, frame;
3631
3632 if (! f || ! f->n_tool_bar_items || NILP (f->tool_bar_items))
3633 return FALSE;
3634
3635 if (event->type == GDK_ENTER_NOTIFY)
3636 {
3637 idx *= TOOL_BAR_ITEM_NSLOTS;
3638 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_HELP);
3639
3640 if (NILP (help))
3641 help = AREF (f->tool_bar_items, idx + TOOL_BAR_ITEM_CAPTION);
3642 }
3643 else
3644 help = Qnil;
3645
3646 XSETFRAME (frame, f);
3647 kbd_buffer_store_help_event (frame, help);
3648
3649 return FALSE;
3650 }
3651
3652
3653 /* This callback is called when a tool bar item shall be redrawn.
3654 It modifies the expose event so that the GtkImage widget redraws the
3655 whole image. This to overcome a bug that makes GtkImage draw the image
3656 in the wrong place when it tries to redraw just a part of the image.
3657 W is the GtkImage to be redrawn.
3658 EVENT is the expose event for W.
3659 CLIENT_DATA is unused.
3660
3661 Returns FALSE to tell GTK to keep processing this event. */
3662
3663 static gboolean
3664 xg_tool_bar_item_expose_callback (GtkWidget *w,
3665 GdkEventExpose *event,
3666 gpointer client_data)
3667 {
3668 gint width, height;
3669
3670 gdk_drawable_get_size (event->window, &width, &height);
3671
3672 event->area.x -= width > event->area.width ? width-event->area.width : 0;
3673 event->area.y -= height > event->area.height ? height-event->area.height : 0;
3674
3675 event->area.x = max (0, event->area.x);
3676 event->area.y = max (0, event->area.y);
3677
3678 event->area.width = max (width, event->area.width);
3679 event->area.height = max (height, event->area.height);
3680
3681 return FALSE;
3682 }
3683
3684 #ifdef HAVE_GTK_ORIENTABLE_SET_ORIENTATION
3685 #define toolbar_set_orientation(w, o) \
3686 gtk_orientable_set_orientation (GTK_ORIENTABLE (w), o)
3687 #else
3688 #define toolbar_set_orientation(w, o) \
3689 gtk_toolbar_set_orientation (GTK_TOOLBAR (w), o)
3690 #endif
3691
3692 /* Attach a tool bar to frame F. */
3693
3694 static void
3695 xg_pack_tool_bar (FRAME_PTR f, Lisp_Object pos)
3696 {
3697 struct x_output *x = f->output_data.x;
3698 int into_hbox = EQ (pos, Qleft) || EQ (pos, Qright);
3699
3700 toolbar_set_orientation (x->toolbar_widget,
3701 into_hbox
3702 ? GTK_ORIENTATION_VERTICAL
3703 : GTK_ORIENTATION_HORIZONTAL);
3704 if (!x->handlebox_widget)
3705 {
3706 x->handlebox_widget = gtk_handle_box_new ();
3707 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-detached",
3708 G_CALLBACK (xg_tool_bar_detach_callback), f);
3709 g_signal_connect (G_OBJECT (x->handlebox_widget), "child-attached",
3710 G_CALLBACK (xg_tool_bar_attach_callback), f);
3711 gtk_container_add (GTK_CONTAINER (x->handlebox_widget),
3712 x->toolbar_widget);
3713 }
3714
3715 if (into_hbox)
3716 {
3717 gtk_box_pack_start (GTK_BOX (x->hbox_widget), x->handlebox_widget,
3718 FALSE, FALSE, 0);
3719
3720 if (EQ (pos, Qleft))
3721 gtk_box_reorder_child (GTK_BOX (x->hbox_widget),
3722 x->handlebox_widget,
3723 0);
3724 x->toolbar_in_hbox = 1;
3725 }
3726 else
3727 {
3728 int vbox_pos = x->menubar_widget ? 1 : 0;
3729 gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->handlebox_widget,
3730 FALSE, FALSE, 0);
3731
3732 if (EQ (pos, Qtop))
3733 gtk_box_reorder_child (GTK_BOX (x->vbox_widget),
3734 x->handlebox_widget,
3735 vbox_pos);
3736 x->toolbar_in_hbox = 0;
3737 }
3738 }
3739
3740 /* Create a tool bar for frame F. */
3741
3742 static void
3743 xg_create_tool_bar (FRAME_PTR f)
3744 {
3745 struct x_output *x = f->output_data.x;
3746
3747 x->toolbar_widget = gtk_toolbar_new ();
3748 x->toolbar_detached = 0;
3749
3750 gtk_widget_set_name (x->toolbar_widget, "emacs-toolbar");
3751
3752 gtk_toolbar_set_style (GTK_TOOLBAR (x->toolbar_widget), GTK_TOOLBAR_ICONS);
3753 toolbar_set_orientation (x->toolbar_widget, GTK_ORIENTATION_HORIZONTAL);
3754 }
3755
3756
3757 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
3758
3759 /* Find the right-to-left image named by RTL in the tool bar images for F.
3760 Returns IMAGE if RTL is not found. */
3761
3762 static Lisp_Object
3763 find_rtl_image (FRAME_PTR f, Lisp_Object image, Lisp_Object rtl)
3764 {
3765 int i;
3766 Lisp_Object file, rtl_name;
3767 struct gcpro gcpro1, gcpro2;
3768 GCPRO2 (file, rtl_name);
3769
3770 rtl_name = Ffile_name_nondirectory (rtl);
3771
3772 for (i = 0; i < f->n_tool_bar_items; ++i)
3773 {
3774 Lisp_Object rtl_image = PROP (TOOL_BAR_ITEM_IMAGES);
3775 if (!NILP (file = file_for_image (rtl_image)))
3776 {
3777 file = call1 (intern ("file-name-sans-extension"),
3778 Ffile_name_nondirectory (file));
3779 if (EQ (Fequal (file, rtl_name), Qt))
3780 {
3781 image = rtl_image;
3782 break;
3783 }
3784 }
3785 }
3786
3787 return image;
3788 }
3789
3790 static GtkToolItem *
3791 xg_make_tool_item (FRAME_PTR f,
3792 GtkWidget *wimage,
3793 GtkWidget **wbutton,
3794 char *label,
3795 int i)
3796 {
3797 GtkToolItem *ti = gtk_tool_item_new ();
3798 Lisp_Object style = Ftool_bar_get_system_style ();
3799 int both_horiz = EQ (style, Qboth_horiz);
3800 int text_image = EQ (style, Qtext_image_horiz);
3801
3802 GtkWidget *vb = both_horiz || text_image
3803 ? gtk_hbox_new (FALSE, 0) : gtk_vbox_new (FALSE, 0);
3804 GtkWidget *wb = gtk_button_new ();
3805 GtkWidget *weventbox = gtk_event_box_new ();
3806
3807 if (wimage && ! text_image)
3808 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
3809
3810 gtk_box_pack_start (GTK_BOX (vb), gtk_label_new (label), TRUE, TRUE, 0);
3811
3812 if (wimage && text_image)
3813 gtk_box_pack_start (GTK_BOX (vb), wimage, TRUE, TRUE, 0);
3814
3815 gtk_button_set_focus_on_click (GTK_BUTTON (wb), FALSE);
3816 gtk_button_set_relief (GTK_BUTTON (wb), GTK_RELIEF_NONE);
3817 gtk_container_add (GTK_CONTAINER (wb), vb);
3818 gtk_container_add (GTK_CONTAINER (weventbox), wb);
3819 gtk_container_add (GTK_CONTAINER (ti), weventbox);
3820
3821 if (wimage)
3822 {
3823 /* The EMACS_INT cast avoids a warning. */
3824 g_signal_connect (G_OBJECT (ti), "create-menu-proxy",
3825 G_CALLBACK (xg_tool_bar_menu_proxy),
3826 (gpointer) (EMACS_INT) i);
3827
3828 g_signal_connect (G_OBJECT (wb), "clicked",
3829 G_CALLBACK (xg_tool_bar_callback),
3830 (gpointer) (EMACS_INT) i);
3831
3832 g_object_set_data (G_OBJECT (weventbox), XG_FRAME_DATA, (gpointer)f);
3833
3834 /* Catch expose events to overcome an annoying redraw bug, see
3835 comment for xg_tool_bar_item_expose_callback. */
3836 g_signal_connect (G_OBJECT (ti),
3837 "expose-event",
3838 G_CALLBACK (xg_tool_bar_item_expose_callback),
3839 0);
3840
3841 gtk_tool_item_set_homogeneous (ti, FALSE);
3842
3843 /* Callback to save modifyer mask (Shift/Control, etc). GTK makes
3844 no distinction based on modifiers in the activate callback,
3845 so we have to do it ourselves. */
3846 g_signal_connect (wb, "button-release-event",
3847 G_CALLBACK (xg_tool_bar_button_cb),
3848 NULL);
3849
3850 g_object_set_data (G_OBJECT (wb), XG_FRAME_DATA, (gpointer)f);
3851
3852 /* Use enter/leave notify to show help. We use the events
3853 rather than the GtkButton specific signals "enter" and
3854 "leave", so we can have only one callback. The event
3855 will tell us what kind of event it is. */
3856 /* The EMACS_INT cast avoids a warning. */
3857 g_signal_connect (G_OBJECT (weventbox),
3858 "enter-notify-event",
3859 G_CALLBACK (xg_tool_bar_help_callback),
3860 (gpointer) (EMACS_INT) i);
3861 g_signal_connect (G_OBJECT (weventbox),
3862 "leave-notify-event",
3863 G_CALLBACK (xg_tool_bar_help_callback),
3864 (gpointer) (EMACS_INT) i);
3865 }
3866
3867 if (wbutton) *wbutton = wb;
3868
3869 return ti;
3870 }
3871
3872 static void
3873 xg_show_toolbar_item (GtkToolItem *ti)
3874 {
3875 Lisp_Object style = Ftool_bar_get_system_style ();
3876 int both_horiz = EQ (style, Qboth_horiz);
3877 int text_image = EQ (style, Qtext_image_horiz);
3878
3879 int horiz = both_horiz || text_image;
3880 int show_label = ! EQ (style, Qimage);
3881 int show_image = ! EQ (style, Qtext);
3882
3883 GtkWidget *weventbox = gtk_bin_get_child (GTK_BIN (ti));
3884 GtkWidget *wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
3885 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
3886 GtkWidget *wimage;
3887 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
3888 GtkWidget *new_box = NULL;
3889
3890 if (GTK_IS_VBOX (vb) && horiz)
3891 new_box = gtk_hbox_new (FALSE, 0);
3892 else if (GTK_IS_HBOX (vb) && !horiz && show_label && show_image)
3893 new_box = gtk_vbox_new (FALSE, 0);
3894
3895 if (!new_box && horiz)
3896 gtk_box_reorder_child (GTK_BOX (vb), wlbl, text_image ? 0 : 1);
3897 else if (new_box)
3898 {
3899 g_object_ref (G_OBJECT (wimage));
3900 g_object_ref (G_OBJECT (wlbl));
3901 gtk_container_remove (GTK_CONTAINER (vb), wimage);
3902 gtk_container_remove (GTK_CONTAINER (vb), wlbl);
3903 gtk_widget_destroy (GTK_WIDGET (vb));
3904 if (! text_image)
3905 gtk_box_pack_start (GTK_BOX (new_box), wimage, TRUE, TRUE, 0);
3906 gtk_box_pack_start (GTK_BOX (new_box), wlbl, TRUE, TRUE, 0);
3907 if (text_image)
3908 gtk_box_pack_start (GTK_BOX (new_box), wimage, TRUE, TRUE, 0);
3909 gtk_container_add (GTK_CONTAINER (wbutton), new_box);
3910 g_object_unref (G_OBJECT (wimage));
3911 g_object_unref (G_OBJECT (wlbl));
3912 vb = new_box;
3913 }
3914
3915 if (show_label) gtk_widget_show (wlbl);
3916 else gtk_widget_hide (wlbl);
3917 if (show_image) gtk_widget_show (wimage);
3918 else gtk_widget_hide (wimage);
3919 gtk_widget_show (GTK_WIDGET (weventbox));
3920 gtk_widget_show (GTK_WIDGET (vb));
3921 gtk_widget_show (GTK_WIDGET (wbutton));
3922 gtk_widget_show (GTK_WIDGET (ti));
3923 }
3924
3925 static int
3926 xg_update_tool_bar_sizes (FRAME_PTR f)
3927 {
3928 struct x_output *x = f->output_data.x;
3929 GtkRequisition req;
3930 int nl = 0, nr = 0, nt = 0, nb = 0;
3931
3932 gtk_widget_size_request (GTK_WIDGET (x->handlebox_widget), &req);
3933 if (x->toolbar_in_hbox)
3934 {
3935 int pos;
3936 gtk_container_child_get (GTK_CONTAINER (x->hbox_widget),
3937 x->handlebox_widget,
3938 "position", &pos, NULL);
3939 if (pos == 0) nl = req.width;
3940 else nr = req.width;
3941 }
3942 else
3943 {
3944 int pos;
3945 gtk_container_child_get (GTK_CONTAINER (x->vbox_widget),
3946 x->handlebox_widget,
3947 "position", &pos, NULL);
3948 if (pos == 0 || (pos == 1 && x->menubar_widget)) nt = req.height;
3949 else nb = req.height;
3950 }
3951
3952 if (nl != FRAME_TOOLBAR_LEFT_WIDTH (f)
3953 || nr != FRAME_TOOLBAR_RIGHT_WIDTH (f)
3954 || nt != FRAME_TOOLBAR_TOP_HEIGHT (f)
3955 || nb != FRAME_TOOLBAR_BOTTOM_HEIGHT (f))
3956 {
3957 FRAME_TOOLBAR_RIGHT_WIDTH (f) = FRAME_TOOLBAR_LEFT_WIDTH (f)
3958 = FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
3959 FRAME_TOOLBAR_LEFT_WIDTH (f) = nl;
3960 FRAME_TOOLBAR_RIGHT_WIDTH (f) = nr;
3961 FRAME_TOOLBAR_TOP_HEIGHT (f) = nt;
3962 FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = nb;
3963 return 1;
3964 }
3965
3966 return 0;
3967 }
3968
3969
3970 /* Update the tool bar for frame F. Add new buttons and remove old. */
3971
3972 extern Lisp_Object Qx_gtk_map_stock;
3973
3974 void
3975 update_frame_tool_bar (FRAME_PTR f)
3976 {
3977 int i;
3978 struct x_output *x = f->output_data.x;
3979 int hmargin = 0, vmargin = 0;
3980 GtkToolbar *wtoolbar;
3981 GtkToolItem *ti;
3982 GtkTextDirection dir;
3983 int pack_tool_bar = x->handlebox_widget == NULL;
3984
3985 if (! FRAME_GTK_WIDGET (f))
3986 return;
3987
3988 BLOCK_INPUT;
3989
3990 if (INTEGERP (Vtool_bar_button_margin)
3991 && XINT (Vtool_bar_button_margin) > 0)
3992 {
3993 hmargin = XFASTINT (Vtool_bar_button_margin);
3994 vmargin = XFASTINT (Vtool_bar_button_margin);
3995 }
3996 else if (CONSP (Vtool_bar_button_margin))
3997 {
3998 if (INTEGERP (XCAR (Vtool_bar_button_margin))
3999 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
4000 hmargin = XFASTINT (XCAR (Vtool_bar_button_margin));
4001
4002 if (INTEGERP (XCDR (Vtool_bar_button_margin))
4003 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4004 vmargin = XFASTINT (XCDR (Vtool_bar_button_margin));
4005 }
4006
4007 /* The natural size (i.e. when GTK uses 0 as margin) looks best,
4008 so take DEFAULT_TOOL_BAR_BUTTON_MARGIN to mean "default for GTK",
4009 i.e. zero. This means that margins less than
4010 DEFAULT_TOOL_BAR_BUTTON_MARGIN has no effect. */
4011 hmargin = max (0, hmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4012 vmargin = max (0, vmargin - DEFAULT_TOOL_BAR_BUTTON_MARGIN);
4013
4014 if (! x->toolbar_widget)
4015 xg_create_tool_bar (f);
4016
4017 wtoolbar = GTK_TOOLBAR (x->toolbar_widget);
4018 dir = gtk_widget_get_direction (GTK_WIDGET (wtoolbar));
4019
4020 for (i = 0; i < f->n_tool_bar_items; ++i)
4021 {
4022 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
4023 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
4024 int idx;
4025 int img_id;
4026 int icon_size = 0;
4027 struct image *img = NULL;
4028 Lisp_Object image;
4029 Lisp_Object stock = Qnil;
4030 GtkStockItem stock_item;
4031 char *stock_name = NULL;
4032 char *icon_name = NULL;
4033 Lisp_Object rtl;
4034 GtkWidget *wbutton = NULL;
4035 GtkWidget *weventbox;
4036 Lisp_Object specified_file;
4037 char *label = SSDATA (PROP (TOOL_BAR_ITEM_LABEL));
4038
4039 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), i);
4040
4041 if (ti)
4042 {
4043 weventbox = gtk_bin_get_child (GTK_BIN (ti));
4044 wbutton = gtk_bin_get_child (GTK_BIN (weventbox));
4045 }
4046
4047
4048 image = PROP (TOOL_BAR_ITEM_IMAGES);
4049
4050 /* Ignore invalid image specifications. */
4051 if (!valid_image_p (image))
4052 {
4053 if (wbutton) gtk_widget_hide (wbutton);
4054 continue;
4055 }
4056
4057 specified_file = file_for_image (image);
4058 if (!NILP (specified_file) && !NILP (Ffboundp (Qx_gtk_map_stock)))
4059 stock = call1 (Qx_gtk_map_stock, specified_file);
4060
4061 if (STRINGP (stock))
4062 {
4063 stock_name = SSDATA (stock);
4064 if (stock_name[0] == 'n' && stock_name[1] == ':')
4065 {
4066 GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (wtoolbar));
4067 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (screen);
4068
4069 icon_name = stock_name + 2;
4070 stock_name = NULL;
4071 stock = Qnil;
4072
4073 if (! gtk_icon_theme_has_icon (icon_theme, icon_name))
4074 icon_name = NULL;
4075 else
4076 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4077 }
4078 else if (gtk_stock_lookup (SSDATA (stock), &stock_item))
4079 icon_size = gtk_toolbar_get_icon_size (wtoolbar);
4080 else
4081 {
4082 stock = Qnil;
4083 stock_name = NULL;
4084 }
4085 }
4086
4087 if (stock_name == NULL && icon_name == NULL)
4088 {
4089 /* No stock image, or stock item not known. Try regular image. */
4090
4091 /* If image is a vector, choose the image according to the
4092 button state. */
4093 if (dir == GTK_TEXT_DIR_RTL
4094 && !NILP (rtl = PROP (TOOL_BAR_ITEM_RTL_IMAGE))
4095 && STRINGP (rtl))
4096 {
4097 image = find_rtl_image (f, image, rtl);
4098 }
4099
4100 if (VECTORP (image))
4101 {
4102 if (enabled_p)
4103 idx = (selected_p
4104 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
4105 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
4106 else
4107 idx = (selected_p
4108 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
4109 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
4110
4111 xassert (ASIZE (image) >= idx);
4112 image = AREF (image, idx);
4113 }
4114 else
4115 idx = -1;
4116
4117 img_id = lookup_image (f, image);
4118 img = IMAGE_FROM_ID (f, img_id);
4119 prepare_image_for_display (f, img);
4120
4121 if (img->load_failed_p || img->pixmap == None)
4122 {
4123 if (ti)
4124 gtk_widget_hide_all (GTK_WIDGET (ti));
4125 else
4126 {
4127 /* Insert an empty (non-image) button */
4128 ti = xg_make_tool_item (f, NULL, NULL, "", i);
4129 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, -1);
4130 }
4131 continue;
4132 }
4133 }
4134
4135 if (ti == NULL)
4136 {
4137 GtkWidget *w;
4138 if (stock_name)
4139 {
4140 w = gtk_image_new_from_stock (stock_name, icon_size);
4141 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_STOCK_NAME,
4142 (gpointer) xstrdup (stock_name),
4143 (GDestroyNotify) xfree);
4144 }
4145 else if (icon_name)
4146 {
4147 w = gtk_image_new_from_icon_name (icon_name, icon_size);
4148 g_object_set_data_full (G_OBJECT (w), XG_TOOL_BAR_ICON_NAME,
4149 (gpointer) xstrdup (icon_name),
4150 (GDestroyNotify) xfree);
4151 }
4152 else
4153 {
4154 w = xg_get_image_for_pixmap (f, img, x->widget, NULL);
4155 /* Save the image so we can see if an update is needed when
4156 this function is called again. */
4157 g_object_set_data (G_OBJECT (w), XG_TOOL_BAR_IMAGE_DATA,
4158 (gpointer)img->pixmap);
4159 }
4160
4161 gtk_misc_set_padding (GTK_MISC (w), hmargin, vmargin);
4162 ti = xg_make_tool_item (f, w, &wbutton, label, i);
4163 gtk_toolbar_insert (GTK_TOOLBAR (wtoolbar), ti, -1);
4164 gtk_widget_set_sensitive (wbutton, enabled_p);
4165 }
4166 else
4167 {
4168 GtkWidget *vb = gtk_bin_get_child (GTK_BIN (wbutton));
4169 GtkWidget *wimage;
4170 GtkWidget *wlbl = xg_get_tool_bar_widgets (vb, &wimage);
4171
4172 Pixmap old_img = (Pixmap)g_object_get_data (G_OBJECT (wimage),
4173 XG_TOOL_BAR_IMAGE_DATA);
4174 gpointer old_stock_name = g_object_get_data (G_OBJECT (wimage),
4175 XG_TOOL_BAR_STOCK_NAME);
4176 gpointer old_icon_name = g_object_get_data (G_OBJECT (wimage),
4177 XG_TOOL_BAR_ICON_NAME);
4178 gtk_label_set_text (GTK_LABEL (wlbl), label);
4179 if (stock_name &&
4180 (! old_stock_name || strcmp (old_stock_name, stock_name) != 0))
4181 {
4182 gtk_image_set_from_stock (GTK_IMAGE (wimage),
4183 stock_name, icon_size);
4184 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4185 (gpointer) xstrdup (stock_name),
4186 (GDestroyNotify) xfree);
4187 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4188 NULL);
4189 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4190 NULL);
4191 }
4192 else if (icon_name &&
4193 (! old_icon_name || strcmp (old_icon_name, icon_name) != 0))
4194 {
4195 gtk_image_set_from_icon_name (GTK_IMAGE (wimage),
4196 icon_name, icon_size);
4197 g_object_set_data_full (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4198 (gpointer) xstrdup (icon_name),
4199 (GDestroyNotify) xfree);
4200 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4201 NULL);
4202 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4203 NULL);
4204 }
4205 else if (img && old_img != img->pixmap)
4206 {
4207 (void) xg_get_image_for_pixmap (f, img, x->widget,
4208 GTK_IMAGE (wimage));
4209 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_IMAGE_DATA,
4210 (gpointer)img->pixmap);
4211
4212 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_STOCK_NAME,
4213 NULL);
4214 g_object_set_data (G_OBJECT (wimage), XG_TOOL_BAR_ICON_NAME,
4215 NULL);
4216 }
4217
4218 gtk_misc_set_padding (GTK_MISC (wimage), hmargin, vmargin);
4219
4220 gtk_widget_set_sensitive (wbutton, enabled_p);
4221 }
4222 xg_show_toolbar_item (ti);
4223
4224 #undef PROP
4225 }
4226
4227 /* Remove buttons not longer needed. We just hide them so they
4228 can be reused later on. */
4229 do
4230 {
4231 ti = gtk_toolbar_get_nth_item (GTK_TOOLBAR (wtoolbar), i++);
4232 if (ti) gtk_widget_hide_all (GTK_WIDGET (ti));
4233 } while (ti != NULL);
4234
4235 if (f->n_tool_bar_items != 0)
4236 {
4237 if (pack_tool_bar)
4238 xg_pack_tool_bar (f, f->tool_bar_position);
4239 gtk_widget_show (x->toolbar_widget);
4240 gtk_widget_show (x->handlebox_widget);
4241 if (xg_update_tool_bar_sizes (f))
4242 xg_height_or_width_changed (f);
4243 }
4244
4245 UNBLOCK_INPUT;
4246 }
4247
4248 /* Deallocate all resources for the tool bar on frame F.
4249 Remove the tool bar. */
4250
4251 void
4252 free_frame_tool_bar (FRAME_PTR f)
4253 {
4254 struct x_output *x = f->output_data.x;
4255
4256 if (x->toolbar_widget)
4257 {
4258 int is_packed = x->handlebox_widget != 0;
4259 BLOCK_INPUT;
4260 /* We may have created the toolbar_widget in xg_create_tool_bar, but
4261 not the x->handlebox_widget which is created in xg_pack_tool_bar. */
4262 if (is_packed)
4263 {
4264 if (x->toolbar_in_hbox)
4265 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4266 x->handlebox_widget);
4267 else
4268 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4269 x->handlebox_widget);
4270 }
4271 else
4272 gtk_widget_destroy (x->toolbar_widget);
4273
4274 x->toolbar_widget = 0;
4275 x->handlebox_widget = 0;
4276 FRAME_TOOLBAR_TOP_HEIGHT (f) = FRAME_TOOLBAR_BOTTOM_HEIGHT (f) = 0;
4277 FRAME_TOOLBAR_LEFT_WIDTH (f) = FRAME_TOOLBAR_RIGHT_WIDTH (f) = 0;
4278
4279 xg_height_or_width_changed (f);
4280
4281 UNBLOCK_INPUT;
4282 }
4283 }
4284
4285 int
4286 xg_change_toolbar_position (FRAME_PTR f, Lisp_Object pos)
4287 {
4288 struct x_output *x = f->output_data.x;
4289
4290 if (! x->toolbar_widget || ! x->handlebox_widget)
4291 return 1;
4292
4293 BLOCK_INPUT;
4294 g_object_ref (x->handlebox_widget);
4295 if (x->toolbar_in_hbox)
4296 gtk_container_remove (GTK_CONTAINER (x->hbox_widget),
4297 x->handlebox_widget);
4298 else
4299 gtk_container_remove (GTK_CONTAINER (x->vbox_widget),
4300 x->handlebox_widget);
4301 xg_pack_tool_bar (f, pos);
4302 g_object_unref (x->handlebox_widget);
4303 if (xg_update_tool_bar_sizes (f))
4304 xg_height_or_width_changed (f);
4305
4306 UNBLOCK_INPUT;
4307 return 1;
4308 }
4309
4310
4311 \f
4312 /***********************************************************************
4313 Initializing
4314 ***********************************************************************/
4315 void
4316 xg_initialize (void)
4317 {
4318 GtkBindingSet *binding_set;
4319
4320 #if HAVE_XFT
4321 /* Work around a bug with corrupted data if libXft gets unloaded. This way
4322 we keep it permanently linked in. */
4323 XftInit (0);
4324 #endif
4325
4326 gdpy_def = NULL;
4327 xg_ignore_gtk_scrollbar = 0;
4328 xg_detached_menus = 0;
4329 xg_menu_cb_list.prev = xg_menu_cb_list.next =
4330 xg_menu_item_cb_list.prev = xg_menu_item_cb_list.next = 0;
4331
4332 id_to_widget.max_size = id_to_widget.used = 0;
4333 id_to_widget.widgets = 0;
4334
4335 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
4336 bindings. It doesn't seem to be any way to remove properties,
4337 so we set it to VoidSymbol which in X means "no key". */
4338 gtk_settings_set_string_property (gtk_settings_get_default (),
4339 "gtk-menu-bar-accel",
4340 "VoidSymbol",
4341 EMACS_CLASS);
4342
4343 /* Make GTK text input widgets use Emacs style keybindings. This is
4344 Emacs after all. */
4345 gtk_settings_set_string_property (gtk_settings_get_default (),
4346 "gtk-key-theme-name",
4347 "Emacs",
4348 EMACS_CLASS);
4349
4350 /* Make dialogs close on C-g. Since file dialog inherits from
4351 dialog, this works for them also. */
4352 binding_set = gtk_binding_set_by_class (g_type_class_ref (GTK_TYPE_DIALOG));
4353 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4354 "close", 0);
4355
4356 /* Make menus close on C-g. */
4357 binding_set = gtk_binding_set_by_class (g_type_class_ref
4358 (GTK_TYPE_MENU_SHELL));
4359 gtk_binding_entry_add_signal (binding_set, GDK_g, GDK_CONTROL_MASK,
4360 "cancel", 0);
4361 }
4362
4363 #endif /* USE_GTK */
4364
4365 /* arch-tag: fe7104da-bc1e-4aba-9bd1-f349c528f7e3
4366 (do not change this comment) */