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