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