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