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