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