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