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 | ||
232 | /* For the image defined in IMG, make and return a GdkPixmap for | |
233 | the pixmap in *GPIX, and a GdkBitmap for the mask in *GMASK. | |
234 | If IMG has no mask, *GMASK is set to NULL. | |
235 | The image is defined on the display where frame F is. */ | |
236 | static void | |
237 | xg_get_gdk_pixmap_and_mask (f, img, gpix, gmask) | |
238 | FRAME_PTR f; | |
239 | struct image *img; | |
240 | GdkPixmap **gpix; | |
241 | GdkBitmap **gmask; | |
242 | { | |
243 | GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f)); | |
244 | ||
245 | *gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap); | |
246 | *gmask = img->mask ? | |
247 | (GdkBitmap*) gdk_pixmap_foreign_new_for_display (gdpy, img->mask) | |
248 | : 0; | |
249 | } | |
250 | ||
251 | ||
252 | /* Set CURSOR on W and all widgets W contain. We must do like this | |
253 | for scroll bars and menu because they create widgets internally, | |
254 | and it is those widgets that are visible. */ | |
255 | static void | |
f392e843 JD |
256 | xg_set_cursor (w, cursor) |
257 | GtkWidget *w; | |
810f2256 | 258 | GdkCursor *cursor; |
f392e843 JD |
259 | { |
260 | GList *children = gdk_window_peek_children (w->window); | |
261 | ||
810f2256 | 262 | gdk_window_set_cursor (w->window, cursor); |
f392e843 JD |
263 | |
264 | /* The scroll bar widget has more than one GDK window (had to look at | |
265 | the source to figure this out), and there is no way to set cursor | |
266 | on widgets in GTK. So we must set the cursor for all GDK windows. | |
267 | Ditto for menus. */ | |
268 | ||
269 | for ( ; children; children = g_list_next (children)) | |
810f2256 | 270 | gdk_window_set_cursor (GDK_WINDOW (children->data), cursor); |
f392e843 JD |
271 | } |
272 | ||
273 | /* Timer function called when a timeout occurs for xg_timer. | |
274 | This function processes all GTK events in a recursive event loop. | |
275 | This is done because GTK timer events are not seen by Emacs event | |
276 | detection, Emacs only looks for X events. When a scroll bar has the | |
277 | pointer (detected by button press/release events below) an Emacs | |
278 | timer is started, and this function can then check if the GTK timer | |
279 | has expired by calling the GTK event loop. | |
280 | Also, when a menu is active, it has a small timeout before it | |
281 | pops down the sub menu under it. */ | |
282 | static void | |
283 | xg_process_timeouts (timer) | |
284 | struct atimer *timer; | |
285 | { | |
286 | BLOCK_INPUT; | |
287 | /* Ideally we would like to just handle timer events, like the Xt version | |
288 | of this does in xterm.c, but there is no such feature in GTK. */ | |
289 | while (gtk_events_pending ()) | |
290 | gtk_main_iteration (); | |
291 | UNBLOCK_INPUT; | |
292 | } | |
293 | ||
294 | /* Start the xg_timer with an interval of 0.1 seconds, if not already started. | |
295 | xg_process_timeouts is called when the timer expires. The timer | |
7863d625 | 296 | started is continuous, i.e. runs until xg_stop_timer is called. */ |
f392e843 JD |
297 | static void |
298 | xg_start_timer () | |
299 | { | |
300 | if (! xg_timer) | |
301 | { | |
302 | EMACS_TIME interval; | |
303 | EMACS_SET_SECS_USECS (interval, 0, 100000); | |
304 | xg_timer = start_atimer (ATIMER_CONTINUOUS, | |
305 | interval, | |
306 | xg_process_timeouts, | |
307 | 0); | |
308 | } | |
309 | } | |
310 | ||
311 | /* Stop the xg_timer if started. */ | |
312 | static void | |
313 | xg_stop_timer () | |
314 | { | |
315 | if (xg_timer) | |
316 | { | |
317 | cancel_atimer (xg_timer); | |
318 | xg_timer = 0; | |
319 | } | |
320 | } | |
321 | ||
322 | /* Insert NODE into linked LIST. */ | |
323 | static void | |
324 | xg_list_insert (xg_list_node *list, xg_list_node *node) | |
325 | { | |
326 | xg_list_node *list_start = list->next; | |
177c0ea7 | 327 | |
f392e843 JD |
328 | if (list_start) list_start->prev = node; |
329 | node->next = list_start; | |
330 | node->prev = 0; | |
331 | list->next = node; | |
332 | } | |
333 | ||
334 | /* Remove NODE from linked LIST. */ | |
335 | static void | |
336 | xg_list_remove (xg_list_node *list, xg_list_node *node) | |
337 | { | |
338 | xg_list_node *list_start = list->next; | |
339 | if (node == list_start) | |
340 | { | |
341 | list->next = node->next; | |
342 | if (list->next) list->next->prev = 0; | |
343 | } | |
344 | else | |
345 | { | |
346 | node->prev->next = node->next; | |
347 | if (node->next) node->next->prev = node->prev; | |
348 | } | |
349 | } | |
350 | ||
351 | /* Allocate and return a utf8 version of STR. If STR is already | |
352 | utf8 or NULL, just return STR. | |
353 | If not, a new string is allocated and the caller must free the result | |
354 | with g_free. */ | |
355 | static char * | |
356 | get_utf8_string (str) | |
357 | char *str; | |
358 | { | |
359 | char *utf8_str = str; | |
177c0ea7 | 360 | |
f392e843 JD |
361 | /* If not UTF-8, try current locale. */ |
362 | if (str && !g_utf8_validate (str, -1, NULL)) | |
363 | utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0); | |
364 | ||
365 | return utf8_str; | |
366 | } | |
367 | ||
368 | ||
369 | \f | |
370 | /*********************************************************************** | |
371 | General functions for creating widgets, resizing, events, e.t.c. | |
372 | ***********************************************************************/ | |
373 | ||
374 | /* Make a geometry string and pass that to GTK. It seems this is the | |
375 | only way to get geometry position right if the user explicitly | |
376 | asked for a position when starting Emacs. | |
377 | F is the frame we shall set geometry for. */ | |
378 | static void | |
379 | xg_set_geometry (f) | |
380 | FRAME_PTR f; | |
381 | { | |
be786000 | 382 | if (f->size_hint_flags & USPosition) |
f392e843 | 383 | { |
be786000 KS |
384 | int left = f->left_pos; |
385 | int xneg = f->size_hint_flags & XNegative; | |
386 | int top = f->top_pos; | |
387 | int yneg = f->size_hint_flags & YNegative; | |
f392e843 | 388 | char geom_str[32]; |
177c0ea7 | 389 | |
f392e843 JD |
390 | if (xneg) |
391 | left = -left; | |
392 | if (yneg) | |
393 | top = -top; | |
394 | ||
395 | sprintf (geom_str, "=%dx%d%c%d%c%d", | |
be786000 | 396 | FRAME_PIXEL_WIDTH (f), |
f392e843 JD |
397 | FRAME_TOTAL_PIXEL_HEIGHT (f), |
398 | (xneg ? '-' : '+'), left, | |
399 | (yneg ? '-' : '+'), top); | |
400 | ||
401 | if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), | |
402 | geom_str)) | |
403 | fprintf (stderr, "Failed to parse: '%s'\n", geom_str); | |
404 | } | |
405 | } | |
406 | ||
177c0ea7 | 407 | |
f392e843 JD |
408 | /* Resize the outer window of frame F after chainging the height. |
409 | This happend when the menu bar or the tool bar is added or removed. | |
410 | COLUMNS/ROWS is the size the edit area shall have after the resize. */ | |
411 | static void | |
412 | xg_resize_outer_widget (f, columns, rows) | |
413 | FRAME_PTR f; | |
414 | int columns; | |
415 | int rows; | |
416 | { | |
417 | gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), | |
be786000 | 418 | FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f)); |
f392e843 JD |
419 | |
420 | /* base_height is now changed. */ | |
421 | x_wm_set_size_hint (f, 0, 0); | |
422 | ||
423 | /* If we are not mapped yet, set geometry once again, as window | |
424 | height now have changed. */ | |
425 | if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f))) | |
426 | xg_set_geometry (f); | |
427 | ||
428 | xg_frame_set_char_size (f, columns, rows); | |
429 | gdk_window_process_all_updates (); | |
430 | } | |
431 | ||
0cb35f4e JD |
432 | /* This gets called after the frame F has been cleared. Since that is |
433 | done with X calls, we need to redraw GTK widget (scroll bars). */ | |
434 | void | |
435 | xg_frame_cleared (f) | |
436 | FRAME_PTR f; | |
437 | { | |
de38ae5a | 438 | GtkWidget *w = f->output_data.x->widget; |
0cb35f4e | 439 | |
de38ae5a | 440 | if (w) |
0cb35f4e | 441 | { |
de38ae5a JD |
442 | gtk_container_set_reallocate_redraws (GTK_CONTAINER (w), TRUE); |
443 | gtk_container_foreach (GTK_CONTAINER (w), | |
444 | (GtkCallback) gtk_widget_queue_draw, | |
445 | 0); | |
0cb35f4e JD |
446 | gdk_window_process_all_updates (); |
447 | } | |
448 | } | |
449 | ||
f392e843 JD |
450 | /* Function to handle resize of our widgets. Since Emacs has some layouts |
451 | that does not fit well with GTK standard containers, we do most layout | |
452 | manually. | |
453 | F is the frame to resize. | |
454 | PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */ | |
455 | void | |
456 | xg_resize_widgets (f, pixelwidth, pixelheight) | |
457 | FRAME_PTR f; | |
458 | int pixelwidth, pixelheight; | |
459 | { | |
460 | int mbheight = FRAME_MENUBAR_HEIGHT (f); | |
461 | int tbheight = FRAME_TOOLBAR_HEIGHT (f); | |
be786000 KS |
462 | int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, (pixelheight |
463 | - mbheight - tbheight)); | |
464 | int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth); | |
177c0ea7 | 465 | |
f392e843 | 466 | if (FRAME_GTK_WIDGET (f) |
be786000 KS |
467 | && (columns != FRAME_COLS (f) || rows != FRAME_LINES (f) |
468 | || pixelwidth != FRAME_PIXEL_WIDTH (f) || pixelheight != FRAME_PIXEL_HEIGHT (f))) | |
f392e843 JD |
469 | { |
470 | struct x_output *x = f->output_data.x; | |
471 | GtkAllocation all; | |
472 | ||
473 | all.y = mbheight + tbheight; | |
474 | all.x = 0; | |
475 | ||
476 | all.width = pixelwidth; | |
477 | all.height = pixelheight - mbheight - tbheight; | |
478 | ||
479 | gtk_widget_size_allocate (x->edit_widget, &all); | |
cea9be54 | 480 | |
f392e843 JD |
481 | change_frame_size (f, rows, columns, 0, 1, 0); |
482 | SET_FRAME_GARBAGED (f); | |
483 | cancel_mouse_face (f); | |
484 | } | |
485 | } | |
486 | ||
487 | ||
488 | /* Update our widget size to be COLS/ROWS characters for frame F. */ | |
489 | void | |
490 | xg_frame_set_char_size (f, cols, rows) | |
491 | FRAME_PTR f; | |
492 | int cols; | |
493 | int rows; | |
494 | { | |
be786000 | 495 | int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows) |
f392e843 | 496 | + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f); |
5fd6f727 | 497 | int pixelwidth; |
177c0ea7 | 498 | |
f392e843 JD |
499 | /* Take into account the size of the scroll bar. Always use the |
500 | number of columns occupied by the scroll bar here otherwise we | |
501 | might end up with a frame width that is not a multiple of the | |
502 | frame's character width which is bad for vertically split | |
503 | windows. */ | |
be786000 KS |
504 | f->scroll_bar_actual_width |
505 | = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f); | |
f392e843 | 506 | |
055d3c98 | 507 | compute_fringe_widths (f, 0); |
f392e843 | 508 | |
be786000 | 509 | /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it |
5fd6f727 | 510 | after calculating that value. */ |
be786000 | 511 | pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols); |
5fd6f727 | 512 | |
f392e843 JD |
513 | /* Must resize our top level widget. Font size may have changed, |
514 | but not rows/cols. */ | |
515 | gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), | |
516 | pixelwidth, pixelheight); | |
517 | xg_resize_widgets (f, pixelwidth, pixelheight); | |
f26fab36 | 518 | x_wm_set_size_hint (f, 0, 0); |
5fd6f727 JD |
519 | SET_FRAME_GARBAGED (f); |
520 | cancel_mouse_face (f); | |
f392e843 JD |
521 | } |
522 | ||
810f2256 | 523 | /* Convert an X Window WSESC on display DPY to its corresponding GtkWidget. |
f392e843 JD |
524 | Must be done like this, because GtkWidget:s can have "hidden" |
525 | X Window that aren't accessible. | |
526 | ||
527 | Return 0 if no widget match WDESC. */ | |
528 | GtkWidget * | |
810f2256 JD |
529 | xg_win_to_widget (dpy, wdesc) |
530 | Display *dpy; | |
f392e843 JD |
531 | Window wdesc; |
532 | { | |
533 | gpointer gdkwin; | |
534 | GtkWidget *gwdesc = 0; | |
535 | ||
536 | BLOCK_INPUT; | |
810f2256 JD |
537 | |
538 | gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy), | |
539 | wdesc); | |
f392e843 JD |
540 | if (gdkwin) |
541 | { | |
542 | GdkEvent event; | |
543 | event.any.window = gdkwin; | |
544 | gwdesc = gtk_get_event_widget (&event); | |
545 | } | |
177c0ea7 | 546 | |
f392e843 JD |
547 | UNBLOCK_INPUT; |
548 | return gwdesc; | |
549 | } | |
550 | ||
551 | /* Fill in the GdkColor C so that it represents PIXEL. | |
552 | W is the widget that color will be used for. Used to find colormap. */ | |
553 | static void | |
554 | xg_pix_to_gcolor (w, pixel, c) | |
555 | GtkWidget *w; | |
556 | unsigned long pixel; | |
557 | GdkColor *c; | |
558 | { | |
559 | GdkColormap *map = gtk_widget_get_colormap (w); | |
560 | gdk_colormap_query_color (map, pixel, c); | |
561 | } | |
562 | ||
7863d625 JD |
563 | /* Turning off double buffering for our GtkFixed widget has the side |
564 | effect of turning it off also for its children (scroll bars). | |
565 | But we want those to be double buffered to not flicker so handle | |
566 | expose manually here. | |
567 | WIDGET is the GtkFixed widget that gets exposed. | |
568 | EVENT is the expose event. | |
569 | USER_DATA is unused. | |
570 | ||
571 | Return TRUE to tell GTK that this expose event has been fully handeled | |
572 | and that GTK shall do nothing more with it. */ | |
573 | static gboolean | |
810f2256 JD |
574 | xg_fixed_handle_expose (GtkWidget *widget, |
575 | GdkEventExpose *event, | |
576 | gpointer user_data) | |
7863d625 JD |
577 | { |
578 | GList *iter; | |
5fd6f727 | 579 | |
7863d625 JD |
580 | for (iter = GTK_FIXED (widget)->children; iter; iter = g_list_next (iter)) |
581 | { | |
582 | GtkFixedChild *child_data = (GtkFixedChild *) iter->data; | |
583 | GtkWidget *child = child_data->widget; | |
584 | GdkWindow *window = child->window; | |
585 | GdkRegion *region = gtk_widget_region_intersect (child, event->region); | |
586 | ||
587 | if (! gdk_region_empty (region)) | |
588 | { | |
589 | GdkEvent child_event; | |
590 | child_event.expose = *event; | |
591 | child_event.expose.region = region; | |
592 | ||
593 | /* Turn on double buffering, i.e. draw to an off screen area. */ | |
594 | gdk_window_begin_paint_region (window, region); | |
595 | ||
596 | /* Tell child to redraw itself. */ | |
597 | gdk_region_get_clipbox (region, &child_event.expose.area); | |
598 | gtk_widget_send_expose (child, &child_event); | |
599 | gdk_window_process_updates (window, TRUE); | |
600 | ||
601 | /* Copy off screen area to the window. */ | |
602 | gdk_window_end_paint (window); | |
603 | } | |
604 | ||
605 | gdk_region_destroy (region); | |
606 | } | |
607 | ||
608 | return TRUE; | |
609 | } | |
610 | ||
f392e843 JD |
611 | /* Create and set up the GTK widgets for frame F. |
612 | Return 0 if creation failed, non-zero otherwise. */ | |
613 | int | |
614 | xg_create_frame_widgets (f) | |
615 | FRAME_PTR f; | |
616 | { | |
617 | GtkWidget *wtop; | |
618 | GtkWidget *wvbox; | |
619 | GtkWidget *wfixed; | |
620 | GdkColor bg; | |
621 | GtkRcStyle *style; | |
622 | int i; | |
623 | char *title = 0; | |
177c0ea7 | 624 | |
f392e843 JD |
625 | BLOCK_INPUT; |
626 | ||
627 | wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL); | |
810f2256 JD |
628 | xg_set_screen (wtop, f); |
629 | ||
f392e843 JD |
630 | wvbox = gtk_vbox_new (FALSE, 0); |
631 | wfixed = gtk_fixed_new (); /* Must have this to place scroll bars */ | |
177c0ea7 | 632 | |
f392e843 JD |
633 | if (! wtop || ! wvbox || ! wfixed) |
634 | { | |
635 | if (wtop) gtk_widget_destroy (wtop); | |
636 | if (wvbox) gtk_widget_destroy (wvbox); | |
637 | if (wfixed) gtk_widget_destroy (wfixed); | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */ | |
643 | gtk_widget_set_name (wtop, EMACS_CLASS); | |
644 | gtk_widget_set_name (wvbox, "pane"); | |
645 | gtk_widget_set_name (wfixed, SDATA (Vx_resource_name)); | |
646 | ||
647 | /* If this frame has a title or name, set it in the title bar. */ | |
5b07197a DL |
648 | if (! NILP (f->title)) title = SDATA (ENCODE_UTF_8 (f->title)); |
649 | else if (! NILP (f->name)) title = SDATA (ENCODE_UTF_8 (f->name)); | |
f392e843 JD |
650 | |
651 | if (title) gtk_window_set_title (GTK_WINDOW (wtop), title); | |
177c0ea7 | 652 | |
f392e843 JD |
653 | FRAME_GTK_OUTER_WIDGET (f) = wtop; |
654 | FRAME_GTK_WIDGET (f) = wfixed; | |
655 | f->output_data.x->vbox_widget = wvbox; | |
656 | ||
657 | gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE); | |
658 | ||
810f2256 JD |
659 | gtk_widget_set_size_request (wfixed, FRAME_PIXEL_WIDTH (f), |
660 | FRAME_PIXEL_HEIGHT (f)); | |
177c0ea7 | 661 | |
f392e843 JD |
662 | gtk_container_add (GTK_CONTAINER (wtop), wvbox); |
663 | gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0); | |
664 | ||
665 | if (FRAME_EXTERNAL_TOOL_BAR (f)) | |
666 | update_frame_tool_bar (f); | |
667 | ||
668 | /* The tool bar is created but first there are no items in it. | |
669 | This causes it to be zero height. Later items are added, but then | |
670 | the frame is already mapped, so there is a "jumping" resize. | |
671 | This makes geometry handling difficult, for example -0-0 will end | |
672 | up in the wrong place as tool bar height has not been taken into account. | |
673 | So we cheat a bit by setting a height that is what it will have | |
674 | later on when tool bar items are added. */ | |
b73e73bf | 675 | if (FRAME_EXTERNAL_TOOL_BAR (f) && FRAME_TOOLBAR_HEIGHT (f) == 0) |
f392e843 | 676 | FRAME_TOOLBAR_HEIGHT (f) = 34; |
177c0ea7 | 677 | |
7863d625 JD |
678 | |
679 | /* We don't want this widget double buffered, because we draw on it | |
680 | with regular X drawing primitives, so from a GTK/GDK point of | |
681 | view, the widget is totally blank. When an expose comes, this | |
682 | will make the widget blank, and then Emacs redraws it. This flickers | |
683 | a lot, so we turn off double buffering. */ | |
f392e843 | 684 | gtk_widget_set_double_buffered (wfixed, FALSE); |
7863d625 JD |
685 | |
686 | /* Turning off double buffering above has the side effect of turning | |
687 | it off also for its children (scroll bars). But we want those | |
688 | to be double buffered to not flicker so handle expose manually. */ | |
689 | g_signal_connect (G_OBJECT (wfixed), "expose-event", | |
690 | G_CALLBACK (xg_fixed_handle_expose), 0); | |
177c0ea7 | 691 | |
f392e843 JD |
692 | /* GTK documents says use gtk_window_set_resizable. But then a user |
693 | can't shrink the window from its starting size. */ | |
694 | gtk_window_set_policy (GTK_WINDOW (wtop), TRUE, TRUE, TRUE); | |
695 | gtk_window_set_wmclass (GTK_WINDOW (wtop), | |
696 | SDATA (Vx_resource_name), | |
697 | SDATA (Vx_resource_class)); | |
698 | ||
699 | /* Add callback to do nothing on WM_DELETE_WINDOW. The default in | |
700 | GTK is to destroy the widget. We want Emacs to do that instead. */ | |
701 | g_signal_connect (G_OBJECT (wtop), "delete-event", | |
702 | G_CALLBACK (gtk_true), 0); | |
177c0ea7 | 703 | |
f392e843 JD |
704 | /* Convert our geometry parameters into a geometry string |
705 | and specify it. | |
706 | GTK will itself handle calculating the real position this way. */ | |
707 | xg_set_geometry (f); | |
708 | ||
709 | gtk_widget_add_events (wfixed, | |
710 | GDK_POINTER_MOTION_MASK | |
711 | | GDK_EXPOSURE_MASK | |
712 | | GDK_BUTTON_PRESS_MASK | |
713 | | GDK_BUTTON_RELEASE_MASK | |
714 | | GDK_KEY_PRESS_MASK | |
715 | | GDK_ENTER_NOTIFY_MASK | |
716 | | GDK_LEAVE_NOTIFY_MASK | |
717 | | GDK_FOCUS_CHANGE_MASK | |
718 | | GDK_STRUCTURE_MASK | |
719 | | GDK_VISIBILITY_NOTIFY_MASK); | |
720 | ||
721 | /* Must realize the windows so the X window gets created. It is used | |
722 | by callers of this function. */ | |
723 | gtk_widget_realize (wfixed); | |
724 | FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed); | |
725 | ||
726 | /* Since GTK clears its window by filling with the background color, | |
727 | we must keep X and GTK background in sync. */ | |
728 | xg_pix_to_gcolor (wfixed, f->output_data.x->background_pixel, &bg); | |
729 | gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg); | |
730 | ||
731 | /* Also, do not let any background pixmap to be set, this looks very | |
732 | bad as Emacs overwrites the background pixmap with its own idea | |
733 | of background color. */ | |
734 | style = gtk_widget_get_modifier_style (wfixed); | |
735 | ||
736 | /* Must use g_strdup because gtk_widget_modify_style does g_free. */ | |
737 | style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>"); | |
738 | gtk_widget_modify_style (wfixed, style); | |
177c0ea7 | 739 | |
f392e843 | 740 | /* GTK does not set any border, and they look bad with GTK. */ |
be786000 KS |
741 | f->border_width = 0; |
742 | f->internal_border_width = 0; | |
f392e843 JD |
743 | |
744 | UNBLOCK_INPUT; | |
745 | ||
746 | return 1; | |
747 | } | |
748 | ||
749 | /* Set the normal size hints for the window manager, for frame F. | |
750 | FLAGS is the flags word to use--or 0 meaning preserve the flags | |
751 | that the window now has. | |
752 | If USER_POSITION is nonzero, we set the User Position | |
753 | flag (this is useful when FLAGS is 0). */ | |
754 | void | |
755 | x_wm_set_size_hint (f, flags, user_position) | |
756 | FRAME_PTR f; | |
757 | long flags; | |
758 | int user_position; | |
759 | { | |
760 | if (FRAME_GTK_OUTER_WIDGET (f)) | |
761 | { | |
762 | /* Must use GTK routines here, otherwise GTK resets the size hints | |
763 | to its own defaults. */ | |
764 | GdkGeometry size_hints; | |
765 | gint hint_flags = 0; | |
766 | int base_width, base_height; | |
767 | int min_rows = 0, min_cols = 0; | |
be786000 | 768 | int win_gravity = f->win_gravity; |
177c0ea7 | 769 | |
f392e843 JD |
770 | if (flags) |
771 | { | |
772 | memset (&size_hints, 0, sizeof (size_hints)); | |
773 | f->output_data.x->size_hints = size_hints; | |
774 | f->output_data.x->hint_flags = hint_flags; | |
775 | } | |
776 | else | |
be786000 | 777 | flags = f->size_hint_flags; |
177c0ea7 | 778 | |
f392e843 JD |
779 | size_hints = f->output_data.x->size_hints; |
780 | hint_flags = f->output_data.x->hint_flags; | |
781 | ||
782 | hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE; | |
be786000 KS |
783 | size_hints.width_inc = FRAME_COLUMN_WIDTH (f); |
784 | size_hints.height_inc = FRAME_LINE_HEIGHT (f); | |
f392e843 JD |
785 | |
786 | hint_flags |= GDK_HINT_BASE_SIZE; | |
be786000 KS |
787 | base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0); |
788 | base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0) | |
f392e843 JD |
789 | + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f); |
790 | ||
791 | check_frame_size (f, &min_rows, &min_cols); | |
792 | ||
793 | size_hints.base_width = base_width; | |
794 | size_hints.base_height = base_height; | |
795 | size_hints.min_width = base_width + min_cols * size_hints.width_inc; | |
796 | size_hints.min_height = base_height + min_rows * size_hints.height_inc; | |
797 | ||
177c0ea7 | 798 | |
f392e843 JD |
799 | /* These currently have a one to one mapping with the X values, but I |
800 | don't think we should rely on that. */ | |
801 | hint_flags |= GDK_HINT_WIN_GRAVITY; | |
802 | size_hints.win_gravity = 0; | |
803 | if (win_gravity == NorthWestGravity) | |
804 | size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST; | |
805 | else if (win_gravity == NorthGravity) | |
806 | size_hints.win_gravity = GDK_GRAVITY_NORTH; | |
807 | else if (win_gravity == NorthEastGravity) | |
808 | size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST; | |
809 | else if (win_gravity == WestGravity) | |
810 | size_hints.win_gravity = GDK_GRAVITY_WEST; | |
811 | else if (win_gravity == CenterGravity) | |
812 | size_hints.win_gravity = GDK_GRAVITY_CENTER; | |
813 | else if (win_gravity == EastGravity) | |
814 | size_hints.win_gravity = GDK_GRAVITY_EAST; | |
815 | else if (win_gravity == SouthWestGravity) | |
816 | size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST; | |
817 | else if (win_gravity == SouthGravity) | |
818 | size_hints.win_gravity = GDK_GRAVITY_SOUTH; | |
819 | else if (win_gravity == SouthEastGravity) | |
820 | size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST; | |
821 | else if (win_gravity == StaticGravity) | |
822 | size_hints.win_gravity = GDK_GRAVITY_STATIC; | |
823 | ||
824 | if (flags & PPosition) hint_flags |= GDK_HINT_POS; | |
825 | if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS; | |
826 | if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE; | |
827 | ||
828 | if (user_position) | |
829 | { | |
830 | hint_flags &= ~GDK_HINT_POS; | |
831 | hint_flags |= GDK_HINT_USER_POS; | |
832 | } | |
833 | ||
834 | BLOCK_INPUT; | |
835 | ||
836 | gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), | |
837 | FRAME_GTK_OUTER_WIDGET (f), | |
838 | &size_hints, | |
839 | hint_flags); | |
840 | ||
841 | f->output_data.x->size_hints = size_hints; | |
842 | f->output_data.x->hint_flags = hint_flags; | |
843 | UNBLOCK_INPUT; | |
844 | } | |
845 | } | |
846 | ||
847 | /* Change background color of a frame. | |
848 | Since GTK uses the background colour to clear the window, we must | |
849 | keep the GTK and X colors in sync. | |
850 | F is the frame to change, | |
851 | BG is the pixel value to change to. */ | |
852 | void | |
853 | xg_set_background_color (f, bg) | |
854 | FRAME_PTR f; | |
855 | unsigned long bg; | |
856 | { | |
857 | if (FRAME_GTK_WIDGET (f)) | |
858 | { | |
859 | GdkColor gdk_bg; | |
860 | ||
861 | BLOCK_INPUT; | |
862 | xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg); | |
863 | gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg); | |
864 | UNBLOCK_INPUT; | |
865 | } | |
866 | } | |
867 | ||
868 | ||
869 | \f | |
870 | /*********************************************************************** | |
871 | Dialog functions | |
872 | ***********************************************************************/ | |
873 | /* Return the dialog title to use for a dialog of type KEY. | |
874 | This is the encoding used by lwlib. We use the same for GTK. */ | |
875 | static char * | |
876 | get_dialog_title (char key) | |
877 | { | |
878 | char *title = ""; | |
177c0ea7 | 879 | |
f392e843 JD |
880 | switch (key) { |
881 | case 'E': case 'e': | |
882 | title = "Error"; | |
883 | break; | |
884 | ||
885 | case 'I': case 'i': | |
886 | title = "Information"; | |
887 | break; | |
888 | ||
889 | case 'L': case 'l': | |
890 | title = "Prompt"; | |
891 | break; | |
892 | ||
893 | case 'P': case 'p': | |
894 | title = "Prompt"; | |
895 | break; | |
896 | ||
897 | case 'Q': case 'q': | |
898 | title = "Question"; | |
899 | break; | |
900 | } | |
901 | ||
902 | return title; | |
903 | } | |
904 | ||
905 | /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down | |
906 | the dialog, but return TRUE so the event does not propagate further | |
907 | in GTK. This prevents GTK from destroying the dialog widget automatically | |
908 | and we can always destrou the widget manually, regardles of how | |
909 | it was popped down (button press or WM_DELETE_WINDOW). | |
910 | W is the dialog widget. | |
911 | EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used). | |
912 | user_data is NULL (not used). | |
913 | ||
914 | Returns TRUE to end propagation of event. */ | |
915 | static gboolean | |
916 | dialog_delete_callback (w, event, user_data) | |
917 | GtkWidget *w; | |
918 | GdkEvent *event; | |
919 | gpointer user_data; | |
920 | { | |
921 | gtk_widget_unmap (w); | |
922 | return TRUE; | |
923 | } | |
924 | ||
925 | /* Create a popup dialog window. See also xg_create_widget below. | |
926 | WV is a widget_value describing the dialog. | |
927 | SELECT_CB is the callback to use when a button has been pressed. | |
928 | DEACTIVATE_CB is the callback to use when the dialog pops down. | |
929 | ||
930 | Returns the GTK dialog widget. */ | |
931 | static GtkWidget * | |
932 | create_dialog (wv, select_cb, deactivate_cb) | |
933 | widget_value *wv; | |
934 | GCallback select_cb; | |
935 | GCallback deactivate_cb; | |
936 | { | |
937 | char *title = get_dialog_title (wv->name[0]); | |
938 | int total_buttons = wv->name[1] - '0'; | |
939 | int right_buttons = wv->name[4] - '0'; | |
940 | int left_buttons; | |
941 | int button_nr = 0; | |
942 | int button_spacing = 10; | |
943 | GtkWidget *wdialog = gtk_dialog_new (); | |
944 | widget_value *item; | |
945 | GtkBox *cur_box; | |
946 | GtkWidget *wvbox; | |
947 | GtkWidget *whbox_up; | |
948 | GtkWidget *whbox_down; | |
949 | ||
950 | /* If the number of buttons is greater than 4, make two rows of buttons | |
951 | instead. This looks better. */ | |
952 | int make_two_rows = total_buttons > 4; | |
953 | ||
954 | if (right_buttons == 0) right_buttons = total_buttons/2; | |
955 | left_buttons = total_buttons - right_buttons; | |
956 | ||
957 | gtk_window_set_title (GTK_WINDOW (wdialog), title); | |
958 | gtk_widget_set_name (wdialog, "emacs-dialog"); | |
959 | ||
960 | cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area); | |
961 | ||
962 | if (make_two_rows) | |
963 | { | |
964 | wvbox = gtk_vbox_new (TRUE, button_spacing); | |
965 | whbox_up = gtk_hbox_new (FALSE, 0); | |
966 | whbox_down = gtk_hbox_new (FALSE, 0); | |
967 | ||
968 | gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0); | |
969 | gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0); | |
970 | gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0); | |
971 | ||
972 | cur_box = GTK_BOX (whbox_up); | |
973 | } | |
974 | ||
975 | g_signal_connect (G_OBJECT (wdialog), "delete-event", | |
976 | G_CALLBACK (dialog_delete_callback), 0); | |
177c0ea7 | 977 | |
f392e843 JD |
978 | if (deactivate_cb) |
979 | { | |
980 | g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0); | |
981 | g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0); | |
982 | } | |
983 | ||
984 | for (item = wv->contents; item; item = item->next) | |
985 | { | |
986 | char *utf8_label = get_utf8_string (item->value); | |
987 | GtkWidget *w; | |
988 | GtkRequisition req; | |
989 | ||
0a1d6de0 | 990 | if (item->name && strcmp (item->name, "message") == 0) |
f392e843 JD |
991 | { |
992 | /* This is the text part of the dialog. */ | |
993 | w = gtk_label_new (utf8_label); | |
994 | gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), | |
995 | gtk_label_new (""), | |
996 | FALSE, FALSE, 0); | |
997 | gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w, | |
998 | TRUE, TRUE, 0); | |
999 | gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5); | |
1000 | ||
1001 | /* Try to make dialog look better. Must realize first so | |
1002 | the widget can calculate the size it needs. */ | |
1003 | gtk_widget_realize (w); | |
1004 | gtk_widget_size_request (w, &req); | |
1005 | gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox), | |
1006 | req.height); | |
0a1d6de0 | 1007 | if (item->value && strlen (item->value) > 0) |
f392e843 JD |
1008 | button_spacing = 2*req.width/strlen (item->value); |
1009 | } | |
1010 | else | |
1011 | { | |
1012 | /* This is one button to add to the dialog. */ | |
4b1b4443 | 1013 | w = gtk_button_new_with_label (utf8_label); |
f392e843 JD |
1014 | if (! item->enabled) |
1015 | gtk_widget_set_sensitive (w, FALSE); | |
1016 | if (select_cb) | |
1017 | g_signal_connect (G_OBJECT (w), "clicked", | |
1018 | select_cb, item->call_data); | |
1019 | ||
1020 | gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing); | |
1021 | if (++button_nr == left_buttons) | |
1022 | { | |
1023 | if (make_two_rows) | |
1024 | cur_box = GTK_BOX (whbox_down); | |
1025 | else | |
1026 | gtk_box_pack_start (cur_box, | |
1027 | gtk_label_new (""), | |
1028 | TRUE, TRUE, | |
1029 | button_spacing); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | if (utf8_label && utf8_label != item->value) | |
1034 | g_free (utf8_label); | |
1035 | } | |
1036 | ||
1037 | return wdialog; | |
1038 | } | |
1039 | ||
1040 | ||
1041 | enum | |
1042 | { | |
1043 | XG_FILE_NOT_DONE, | |
1044 | XG_FILE_OK, | |
1045 | XG_FILE_CANCEL, | |
1046 | XG_FILE_DESTROYED, | |
1047 | }; | |
1048 | ||
1049 | /* Callback function invoked when the Ok button is pressed in | |
1050 | a file dialog. | |
1051 | W is the file dialog widget, | |
1052 | ARG points to an integer where we record what has happend. */ | |
1053 | static void | |
1054 | xg_file_sel_ok (w, arg) | |
1055 | GtkWidget *w; | |
1056 | gpointer arg; | |
1057 | { | |
1058 | *(int*)arg = XG_FILE_OK; | |
1059 | } | |
1060 | ||
1061 | /* Callback function invoked when the Cancel button is pressed in | |
1062 | a file dialog. | |
1063 | W is the file dialog widget, | |
1064 | ARG points to an integer where we record what has happend. */ | |
1065 | static void | |
1066 | xg_file_sel_cancel (w, arg) | |
1067 | GtkWidget *w; | |
1068 | gpointer arg; | |
1069 | { | |
1070 | *(int*)arg = XG_FILE_CANCEL; | |
1071 | } | |
1072 | ||
1073 | /* Callback function invoked when the file dialog is destroyed (i.e. | |
1074 | popped down). We must keep track of this, because if this | |
1075 | happens, GTK destroys the widget. But if for example, Ok is pressed, | |
1076 | the dialog is popped down, but the dialog widget is not destroyed. | |
1077 | W is the file dialog widget, | |
1078 | ARG points to an integer where we record what has happend. */ | |
1079 | static void | |
1080 | xg_file_sel_destroy (w, arg) | |
1081 | GtkWidget *w; | |
1082 | gpointer arg; | |
1083 | { | |
1084 | *(int*)arg = XG_FILE_DESTROYED; | |
1085 | } | |
1086 | ||
1087 | /* Read a file name from the user using a file dialog. | |
1088 | F is the current frame. | |
1089 | PROMPT is a prompt to show to the user. May not be NULL. | |
1090 | DEFAULT_FILENAME is a default selection to be displayed. May be NULL. | |
1091 | If MUSTMATCH_P is non-zero, the returned file name must be an existing | |
1092 | file. | |
1093 | ||
1094 | Returns a file name or NULL if no file was selected. | |
1095 | The returned string must be freed by the caller. */ | |
1096 | char * | |
1097 | xg_get_file_name (f, prompt, default_filename, mustmatch_p) | |
1098 | FRAME_PTR f; | |
1099 | char *prompt; | |
1100 | char *default_filename; | |
1101 | int mustmatch_p; | |
1102 | { | |
1103 | GtkWidget *filewin; | |
1104 | GtkFileSelection *filesel; | |
1105 | int filesel_done = XG_FILE_NOT_DONE; | |
1106 | char *fn = 0; | |
177c0ea7 | 1107 | |
f392e843 JD |
1108 | filewin = gtk_file_selection_new (prompt); |
1109 | filesel = GTK_FILE_SELECTION (filewin); | |
1110 | ||
810f2256 JD |
1111 | xg_set_screen (filewin, f); |
1112 | ||
f392e843 JD |
1113 | gtk_widget_set_name (filewin, "emacs-filedialog"); |
1114 | ||
1115 | gtk_window_set_transient_for (GTK_WINDOW (filewin), | |
1116 | GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f))); | |
1117 | gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin), TRUE); | |
1118 | ||
1119 | g_signal_connect (G_OBJECT (filesel->ok_button), | |
1120 | "clicked", | |
1121 | G_CALLBACK (xg_file_sel_ok), | |
1122 | &filesel_done); | |
1123 | g_signal_connect (G_OBJECT (filesel->cancel_button), | |
1124 | "clicked", | |
1125 | G_CALLBACK (xg_file_sel_cancel), | |
1126 | &filesel_done); | |
1127 | g_signal_connect (G_OBJECT (filesel), | |
1128 | "destroy", | |
1129 | G_CALLBACK (xg_file_sel_destroy), | |
1130 | &filesel_done); | |
1131 | ||
1132 | if (default_filename) | |
1133 | gtk_file_selection_set_filename (filesel, default_filename); | |
1134 | ||
1135 | if (mustmatch_p) | |
1136 | { | |
1137 | /* The selection_entry part of filesel is not documented. */ | |
1138 | gtk_widget_set_sensitive (filesel->selection_entry, FALSE); | |
1139 | gtk_file_selection_hide_fileop_buttons (filesel); | |
1140 | } | |
1141 | ||
177c0ea7 | 1142 | |
f392e843 | 1143 | gtk_widget_show_all (filewin); |
177c0ea7 | 1144 | |
f392e843 JD |
1145 | while (filesel_done == XG_FILE_NOT_DONE) |
1146 | gtk_main_iteration (); | |
1147 | ||
1148 | if (filesel_done == XG_FILE_OK) | |
1149 | fn = xstrdup ((char*) gtk_file_selection_get_filename (filesel)); | |
1150 | ||
1151 | if (filesel_done != XG_FILE_DESTROYED) | |
1152 | gtk_widget_destroy (filewin); | |
1153 | ||
1154 | return fn; | |
1155 | } | |
1156 | ||
1157 | \f | |
1158 | /*********************************************************************** | |
1159 | Menu functions. | |
1160 | ***********************************************************************/ | |
1161 | ||
1162 | /* The name of menu items that can be used for citomization. Since GTK | |
1163 | RC files are very crude and primitive, we have to set this on all | |
1164 | menu item names so a user can easily cutomize menu items. */ | |
1165 | ||
1166 | #define MENU_ITEM_NAME "emacs-menuitem" | |
1167 | ||
1168 | ||
1169 | /* Linked list of all allocated struct xg_menu_cb_data. Used for marking | |
1170 | during GC. The next member points to the items. */ | |
1171 | static xg_list_node xg_menu_cb_list; | |
1172 | ||
1173 | /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking | |
1174 | during GC. The next member points to the items. */ | |
1175 | static xg_list_node xg_menu_item_cb_list; | |
1176 | ||
1177 | /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count. | |
1178 | F is the frame CL_DATA will be initialized for. | |
1179 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
1180 | ||
1181 | The menu bar and all sub menus under the menu bar in a frame | |
1182 | share the same structure, hence the reference count. | |
177c0ea7 | 1183 | |
f392e843 JD |
1184 | Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly |
1185 | allocated xg_menu_cb_data if CL_DATA is NULL. */ | |
1186 | static xg_menu_cb_data * | |
1187 | make_cl_data (cl_data, f, highlight_cb) | |
1188 | xg_menu_cb_data *cl_data; | |
1189 | FRAME_PTR f; | |
1190 | GCallback highlight_cb; | |
1191 | { | |
1192 | if (! cl_data) | |
1193 | { | |
1194 | cl_data = (xg_menu_cb_data*) xmalloc (sizeof (*cl_data)); | |
1195 | cl_data->f = f; | |
1196 | cl_data->menu_bar_vector = f->menu_bar_vector; | |
1197 | cl_data->menu_bar_items_used = f->menu_bar_items_used; | |
1198 | cl_data->highlight_cb = highlight_cb; | |
1199 | cl_data->ref_count = 0; | |
1200 | ||
1201 | xg_list_insert (&xg_menu_cb_list, &cl_data->ptrs); | |
1202 | } | |
1203 | ||
1204 | cl_data->ref_count++; | |
1205 | ||
1206 | return cl_data; | |
1207 | } | |
1208 | ||
1209 | /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB. | |
1210 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
1211 | ||
1212 | When the menu bar is updated, menu items may have been added and/or | |
1213 | removed, so menu_bar_vector and menu_bar_items_used change. We must | |
1214 | then update CL_DATA since it is used to determine which menu | |
1215 | item that is invoked in the menu. | |
1216 | HIGHLIGHT_CB could change, there is no check that the same | |
1217 | function is given when modifying a menu bar as was given when | |
1218 | creating the menu bar. */ | |
1219 | static void | |
1220 | update_cl_data (cl_data, f, highlight_cb) | |
1221 | xg_menu_cb_data *cl_data; | |
1222 | FRAME_PTR f; | |
1223 | GCallback highlight_cb; | |
1224 | { | |
1225 | if (cl_data) | |
1226 | { | |
1227 | cl_data->f = f; | |
1228 | cl_data->menu_bar_vector = f->menu_bar_vector; | |
1229 | cl_data->menu_bar_items_used = f->menu_bar_items_used; | |
1230 | cl_data->highlight_cb = highlight_cb; | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | /* Decrease reference count for CL_DATA. | |
1235 | If reference count is zero, free CL_DATA. */ | |
1236 | static void | |
1237 | unref_cl_data (cl_data) | |
1238 | xg_menu_cb_data *cl_data; | |
1239 | { | |
1240 | if (cl_data && cl_data->ref_count > 0) | |
1241 | { | |
1242 | cl_data->ref_count--; | |
1243 | if (cl_data->ref_count == 0) | |
1244 | { | |
1245 | xg_list_remove (&xg_menu_cb_list, &cl_data->ptrs); | |
1246 | xfree (cl_data); | |
1247 | } | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | /* Function that marks all lisp data during GC. */ | |
1252 | void | |
1253 | xg_mark_data () | |
1254 | { | |
1255 | xg_list_node *iter; | |
1256 | ||
1257 | for (iter = xg_menu_cb_list.next; iter; iter = iter->next) | |
631f2082 | 1258 | mark_object (((xg_menu_cb_data *) iter)->menu_bar_vector); |
f392e843 JD |
1259 | |
1260 | for (iter = xg_menu_item_cb_list.next; iter; iter = iter->next) | |
1261 | { | |
1262 | xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data *) iter; | |
1263 | ||
1264 | if (! NILP (cb_data->help)) | |
631f2082 | 1265 | mark_object (cb_data->help); |
f392e843 JD |
1266 | } |
1267 | } | |
1268 | ||
1269 | ||
1270 | /* Callback called when a menu item is destroyed. Used to free data. | |
1271 | W is the widget that is being destroyed (not used). | |
1272 | CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */ | |
1273 | static void | |
1274 | menuitem_destroy_callback (w, client_data) | |
1275 | GtkWidget *w; | |
1276 | gpointer client_data; | |
1277 | { | |
1278 | if (client_data) | |
1279 | { | |
1280 | xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data; | |
1281 | xg_list_remove (&xg_menu_item_cb_list, &data->ptrs); | |
1282 | xfree (data); | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* Callback called when the pointer enters/leaves a menu item. | |
1287 | W is the menu item. | |
1288 | EVENT is either an enter event or leave event. | |
1289 | CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. | |
1290 | ||
1291 | Returns FALSE to tell GTK to keep processing this event. */ | |
1292 | static gboolean | |
1293 | menuitem_highlight_callback (w, event, client_data) | |
1294 | GtkWidget *w; | |
1295 | GdkEventCrossing *event; | |
1296 | gpointer client_data; | |
1297 | { | |
1298 | if (client_data) | |
1299 | { | |
1300 | xg_menu_item_cb_data *data = (xg_menu_item_cb_data*) client_data; | |
1301 | gpointer call_data = event->type == GDK_LEAVE_NOTIFY ? 0 : client_data; | |
177c0ea7 | 1302 | |
f392e843 JD |
1303 | if (! NILP (data->help) && data->cl_data->highlight_cb) |
1304 | { | |
1305 | GtkCallback func = (GtkCallback) data->cl_data->highlight_cb; | |
1306 | (*func) (w, call_data); | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | return FALSE; | |
1311 | } | |
1312 | ||
1313 | /* Callback called when a menu is destroyed. Used to free data. | |
1314 | W is the widget that is being destroyed (not used). | |
1315 | CLIENT_DATA points to the xg_menu_cb_data associated with W. */ | |
1316 | static void | |
1317 | menu_destroy_callback (w, client_data) | |
1318 | GtkWidget *w; | |
1319 | gpointer client_data; | |
1320 | { | |
1321 | unref_cl_data ((xg_menu_cb_data*) client_data); | |
1322 | } | |
1323 | ||
1324 | /* Callback called when a menu does a grab or ungrab. That means the | |
1325 | menu has been activated or deactivated. | |
1326 | Used to start a timer so the small timeout the menus in GTK uses before | |
1327 | popping down a menu is seen by Emacs (see xg_process_timeouts above). | |
1328 | W is the widget that does the grab (not used). | |
1329 | UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab. | |
1330 | CLIENT_DATA is NULL (not used). */ | |
1331 | static void | |
1332 | menu_grab_callback (GtkWidget *widget, | |
1333 | gboolean ungrab_p, | |
1334 | gpointer client_data) | |
1335 | { | |
1336 | /* Keep track of total number of grabs. */ | |
1337 | static int cnt; | |
1338 | ||
1339 | if (ungrab_p) cnt--; | |
1340 | else cnt++; | |
1341 | ||
1342 | if (cnt > 0 && ! xg_timer) xg_start_timer (); | |
1343 | else if (cnt == 0 && xg_timer) xg_stop_timer (); | |
1344 | } | |
1345 | ||
1346 | /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both | |
1347 | must be non-NULL) and can be inserted into a menu item. | |
1348 | ||
1349 | Returns the GtkHBox. */ | |
1350 | static GtkWidget * | |
1351 | make_widget_for_menu_item (utf8_label, utf8_key) | |
1352 | char *utf8_label; | |
1353 | char *utf8_key; | |
1354 | { | |
1355 | GtkWidget *wlbl; | |
1356 | GtkWidget *wkey; | |
1357 | GtkWidget *wbox; | |
177c0ea7 | 1358 | |
f392e843 | 1359 | wbox = gtk_hbox_new (FALSE, 0); |
4b1b4443 | 1360 | wlbl = gtk_label_new (utf8_label); |
f392e843 JD |
1361 | wkey = gtk_label_new (utf8_key); |
1362 | ||
1363 | gtk_misc_set_alignment (GTK_MISC (wlbl), 0.0, 0.5); | |
1364 | gtk_misc_set_alignment (GTK_MISC (wkey), 0.0, 0.5); | |
177c0ea7 | 1365 | |
f392e843 JD |
1366 | gtk_box_pack_start (GTK_BOX (wbox), wlbl, TRUE, TRUE, 0); |
1367 | gtk_box_pack_start (GTK_BOX (wbox), wkey, FALSE, FALSE, 0); | |
1368 | ||
1369 | gtk_widget_set_name (wlbl, MENU_ITEM_NAME); | |
1370 | gtk_widget_set_name (wkey, MENU_ITEM_NAME); | |
7863d625 | 1371 | gtk_widget_set_name (wbox, MENU_ITEM_NAME); |
f392e843 JD |
1372 | |
1373 | return wbox; | |
1374 | } | |
1375 | ||
1376 | /* Make and return a menu item widget with the key to the right. | |
1377 | UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally). | |
1378 | UTF8_KEY is the text representing the key binding. | |
1379 | ITEM is the widget_value describing the menu item. | |
177c0ea7 | 1380 | |
f392e843 JD |
1381 | GROUP is an in/out parameter. If the menu item to be created is not |
1382 | part of any radio menu group, *GROUP contains NULL on entry and exit. | |
1383 | If the menu item to be created is part of a radio menu group, on entry | |
1384 | *GROUP contains the group to use, or NULL if this is the first item | |
1385 | in the group. On exit, *GROUP contains the radio item group. | |
1386 | ||
1387 | Unfortunately, keys don't line up as nicely as in Motif, | |
1388 | but the MacOS X version doesn't either, so I guess that is OK. */ | |
1389 | static GtkWidget * | |
1390 | make_menu_item (utf8_label, utf8_key, item, group) | |
1391 | char *utf8_label; | |
1392 | char *utf8_key; | |
1393 | widget_value *item; | |
1394 | GSList **group; | |
1395 | { | |
1396 | GtkWidget *w; | |
1397 | GtkWidget *wtoadd = 0; | |
177c0ea7 | 1398 | |
adcb132c JD |
1399 | /* It has been observed that some menu items have a NULL name field. |
1400 | This will lead to this function being called with a NULL utf8_label. | |
1401 | GTK crashes on that so we set a blank label. Why there is a NULL | |
1402 | name remains to be investigated. */ | |
1403 | if (! utf8_label) utf8_label = " "; | |
1404 | ||
f392e843 JD |
1405 | if (utf8_key) |
1406 | wtoadd = make_widget_for_menu_item (utf8_label, utf8_key); | |
177c0ea7 | 1407 | |
f392e843 JD |
1408 | if (item->button_type == BUTTON_TYPE_TOGGLE) |
1409 | { | |
1410 | *group = NULL; | |
1411 | if (utf8_key) w = gtk_check_menu_item_new (); | |
4b1b4443 | 1412 | else w = gtk_check_menu_item_new_with_label (utf8_label); |
f392e843 JD |
1413 | gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), item->selected); |
1414 | } | |
1415 | else if (item->button_type == BUTTON_TYPE_RADIO) | |
1416 | { | |
1417 | if (utf8_key) w = gtk_radio_menu_item_new (*group); | |
4b1b4443 | 1418 | else w = gtk_radio_menu_item_new_with_label (*group, utf8_label); |
f392e843 JD |
1419 | *group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w)); |
1420 | if (item->selected) | |
1421 | gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE); | |
1422 | } | |
1423 | else | |
1424 | { | |
1425 | *group = NULL; | |
1426 | if (utf8_key) w = gtk_menu_item_new (); | |
4b1b4443 | 1427 | else w = gtk_menu_item_new_with_label (utf8_label); |
f392e843 | 1428 | } |
177c0ea7 | 1429 | |
f392e843 JD |
1430 | if (wtoadd) gtk_container_add (GTK_CONTAINER (w), wtoadd); |
1431 | if (! item->enabled) gtk_widget_set_sensitive (w, FALSE); | |
1432 | ||
1433 | return w; | |
1434 | } | |
1435 | ||
5fd6f727 | 1436 | /* Return non-zero if LABEL specifies a separator (GTK only has one |
f392e843 JD |
1437 | separator type) */ |
1438 | static int | |
5fd6f727 JD |
1439 | xg_separator_p (char *label) |
1440 | { | |
1441 | if (! label) return 0; | |
1442 | else if (strlen (label) > 3 | |
1443 | && strncmp (label, "--", 2) == 0 | |
1444 | && label[2] != '-') | |
1445 | { | |
1446 | static char* separator_names[] = { | |
1447 | "space", | |
1448 | "no-line", | |
1449 | "single-line", | |
1450 | "double-line", | |
1451 | "single-dashed-line", | |
1452 | "double-dashed-line", | |
1453 | "shadow-etched-in", | |
1454 | "shadow-etched-out", | |
1455 | "shadow-etched-in-dash", | |
1456 | "shadow-etched-out-dash", | |
1457 | "shadow-double-etched-in", | |
1458 | "shadow-double-etched-out", | |
1459 | "shadow-double-etched-in-dash", | |
1460 | "shadow-double-etched-out-dash", | |
1461 | 0, | |
1462 | }; | |
1463 | ||
1464 | int i; | |
1465 | ||
1466 | label += 2; | |
1467 | for (i = 0; separator_names[i]; ++i) | |
1468 | if (strcmp (label, separator_names[i]) == 0) | |
1469 | return 1; | |
1470 | } | |
1471 | else | |
1472 | { | |
1473 | /* Old-style separator, maybe. It's a separator if it contains | |
1474 | only dashes. */ | |
1475 | while (*label == '-') | |
1476 | ++label; | |
1477 | if (*label == 0) return 1; | |
1478 | } | |
0a1d6de0 | 1479 | |
5fd6f727 | 1480 | return 0; |
f392e843 JD |
1481 | } |
1482 | ||
da18b5ac JD |
1483 | static int xg_detached_menus; |
1484 | ||
1485 | /* Returns non-zero if there are detached menus. */ | |
1486 | int | |
1487 | xg_have_tear_offs () | |
1488 | { | |
1489 | return xg_detached_menus > 0; | |
1490 | } | |
f392e843 JD |
1491 | |
1492 | /* Callback invoked when a detached menu window is removed. Here we | |
da18b5ac | 1493 | decrease the xg_detached_menus count. |
f392e843 | 1494 | WIDGET is the top level window that is removed (the parent of the menu). |
da18b5ac JD |
1495 | CLIENT_DATA is not used. */ |
1496 | static void | |
1497 | tearoff_remove (widget, client_data) | |
f392e843 | 1498 | GtkWidget *widget; |
f392e843 JD |
1499 | gpointer client_data; |
1500 | { | |
da18b5ac | 1501 | if (xg_detached_menus > 0) --xg_detached_menus; |
f392e843 JD |
1502 | } |
1503 | ||
da18b5ac JD |
1504 | /* Callback invoked when a menu is detached. It increases the |
1505 | xg_detached_menus count. | |
f392e843 | 1506 | WIDGET is the GtkTearoffMenuItem. |
177c0ea7 | 1507 | CLIENT_DATA is not used. */ |
f392e843 JD |
1508 | static void |
1509 | tearoff_activate (widget, client_data) | |
1510 | GtkWidget *widget; | |
1511 | gpointer client_data; | |
1512 | { | |
1513 | GtkWidget *menu = gtk_widget_get_parent (widget); | |
da18b5ac JD |
1514 | if (gtk_menu_get_tearoff_state (GTK_MENU (menu))) |
1515 | { | |
1516 | ++xg_detached_menus; | |
1517 | g_signal_connect (G_OBJECT (gtk_widget_get_toplevel (widget)), | |
1518 | "destroy", | |
1519 | G_CALLBACK (tearoff_remove), 0); | |
1520 | } | |
f392e843 JD |
1521 | } |
1522 | ||
f392e843 | 1523 | |
f392e843 JD |
1524 | /* Create a menu item widget, and connect the callbacks. |
1525 | ITEM decribes the menu item. | |
1526 | F is the frame the created menu belongs to. | |
1527 | SELECT_CB is the callback to use when a menu item is selected. | |
1528 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
1529 | CL_DATA points to the callback data to be used for this menu. | |
1530 | GROUP is an in/out parameter. If the menu item to be created is not | |
1531 | part of any radio menu group, *GROUP contains NULL on entry and exit. | |
1532 | If the menu item to be created is part of a radio menu group, on entry | |
1533 | *GROUP contains the group to use, or NULL if this is the first item | |
1534 | in the group. On exit, *GROUP contains the radio item group. | |
1535 | ||
1536 | Returns the created GtkWidget. */ | |
1537 | static GtkWidget * | |
1538 | xg_create_one_menuitem (item, f, select_cb, highlight_cb, cl_data, group) | |
1539 | widget_value *item; | |
1540 | FRAME_PTR f; | |
1541 | GCallback select_cb; | |
1542 | GCallback highlight_cb; | |
1543 | xg_menu_cb_data *cl_data; | |
1544 | GSList **group; | |
1545 | { | |
1546 | char *utf8_label; | |
1547 | char *utf8_key; | |
1548 | GtkWidget *w; | |
1549 | xg_menu_item_cb_data *cb_data; | |
1550 | ||
1551 | utf8_label = get_utf8_string (item->name); | |
1552 | utf8_key = get_utf8_string (item->key); | |
1553 | ||
1554 | w = make_menu_item (utf8_label, utf8_key, item, group); | |
1555 | ||
1556 | if (utf8_label && utf8_label != item->name) g_free (utf8_label); | |
1557 | if (utf8_key && utf8_key != item->key) g_free (utf8_key); | |
1558 | ||
1559 | cb_data = xmalloc (sizeof (xg_menu_item_cb_data)); | |
1560 | ||
1561 | xg_list_insert (&xg_menu_item_cb_list, &cb_data->ptrs); | |
1562 | ||
1563 | cb_data->unhighlight_id = cb_data->highlight_id = cb_data->select_id = 0; | |
1564 | cb_data->help = item->help; | |
1565 | cb_data->cl_data = cl_data; | |
1566 | cb_data->call_data = item->call_data; | |
177c0ea7 | 1567 | |
f392e843 JD |
1568 | g_signal_connect (G_OBJECT (w), |
1569 | "destroy", | |
1570 | G_CALLBACK (menuitem_destroy_callback), | |
1571 | cb_data); | |
1572 | ||
1573 | /* Put cb_data in widget, so we can get at it when modifying menubar */ | |
1574 | g_object_set_data (G_OBJECT (w), XG_ITEM_DATA, cb_data); | |
1575 | ||
1576 | /* final item, not a submenu */ | |
1577 | if (item->call_data && ! item->contents) | |
1578 | { | |
1579 | if (select_cb) | |
1580 | cb_data->select_id | |
1581 | = g_signal_connect (G_OBJECT (w), "activate", select_cb, cb_data); | |
1582 | } | |
1583 | ||
1584 | if (! NILP (item->help) && highlight_cb) | |
1585 | { | |
1586 | /* We use enter/leave notify instead of select/deselect because | |
1587 | select/deselect doesn't go well with detached menus. */ | |
1588 | cb_data->highlight_id | |
1589 | = g_signal_connect (G_OBJECT (w), | |
1590 | "enter-notify-event", | |
1591 | G_CALLBACK (menuitem_highlight_callback), | |
1592 | cb_data); | |
1593 | cb_data->unhighlight_id | |
1594 | = g_signal_connect (G_OBJECT (w), | |
1595 | "leave-notify-event", | |
1596 | G_CALLBACK (menuitem_highlight_callback), | |
1597 | cb_data); | |
1598 | } | |
1599 | ||
1600 | return w; | |
1601 | } | |
1602 | ||
9d6194d6 AS |
1603 | static GtkWidget *create_menus P_ ((widget_value *, FRAME_PTR, GCallback, |
1604 | GCallback, GCallback, int, int, int, | |
1605 | GtkWidget *, xg_menu_cb_data *, char *)); | |
1606 | ||
f392e843 JD |
1607 | /* Create a full menu tree specified by DATA. |
1608 | F is the frame the created menu belongs to. | |
1609 | SELECT_CB is the callback to use when a menu item is selected. | |
1610 | DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore. | |
1611 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
1612 | POP_UP_P is non-zero if we shall create a popup menu. | |
1613 | MENU_BAR_P is non-zero if we shall create a menu bar. | |
1614 | ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored | |
1615 | if MENU_BAR_P is non-zero. | |
1616 | TOPMENU is the topmost GtkWidget that others shall be placed under. | |
1617 | It may be NULL, in that case we create the appropriate widget | |
1618 | (menu bar or menu item depending on POP_UP_P and MENU_BAR_P) | |
1619 | CL_DATA is the callback data we shall use for this menu, or NULL | |
1620 | if we haven't set the first callback yet. | |
1621 | NAME is the name to give to the top level menu if this function | |
1622 | creates it. May be NULL to not set any name. | |
1623 | ||
1624 | Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is | |
1625 | not NULL. | |
1626 | ||
1627 | This function calls itself to create submenus. */ | |
1628 | ||
1629 | static GtkWidget * | |
1630 | create_menus (data, f, select_cb, deactivate_cb, highlight_cb, | |
1631 | pop_up_p, menu_bar_p, add_tearoff_p, topmenu, cl_data, name) | |
1632 | widget_value *data; | |
1633 | FRAME_PTR f; | |
1634 | GCallback select_cb; | |
1635 | GCallback deactivate_cb; | |
1636 | GCallback highlight_cb; | |
1637 | int pop_up_p; | |
1638 | int menu_bar_p; | |
1639 | int add_tearoff_p; | |
1640 | GtkWidget *topmenu; | |
1641 | xg_menu_cb_data *cl_data; | |
1642 | char *name; | |
1643 | { | |
1644 | widget_value *item; | |
1645 | GtkWidget *wmenu = topmenu; | |
1646 | GSList *group = NULL; | |
1647 | ||
1648 | if (! topmenu) | |
1649 | { | |
810f2256 JD |
1650 | if (! menu_bar_p) |
1651 | { | |
1652 | wmenu = gtk_menu_new (); | |
1653 | xg_set_screen (wmenu, f); | |
1654 | } | |
f392e843 JD |
1655 | else wmenu = gtk_menu_bar_new (); |
1656 | ||
1657 | /* Put cl_data on the top menu for easier access. */ | |
1658 | cl_data = make_cl_data (cl_data, f, highlight_cb); | |
1659 | g_object_set_data (G_OBJECT (wmenu), XG_FRAME_DATA, (gpointer)cl_data); | |
1660 | g_signal_connect (G_OBJECT (wmenu), "destroy", | |
1661 | G_CALLBACK (menu_destroy_callback), cl_data); | |
177c0ea7 | 1662 | |
f392e843 JD |
1663 | if (name) |
1664 | gtk_widget_set_name (wmenu, name); | |
1665 | ||
1666 | if (deactivate_cb) | |
1667 | g_signal_connect (G_OBJECT (wmenu), | |
1668 | "deactivate", deactivate_cb, 0); | |
1669 | ||
1670 | g_signal_connect (G_OBJECT (wmenu), | |
1671 | "grab-notify", G_CALLBACK (menu_grab_callback), 0); | |
1672 | } | |
177c0ea7 | 1673 | |
f392e843 JD |
1674 | if (! menu_bar_p && add_tearoff_p) |
1675 | { | |
1676 | GtkWidget *tearoff = gtk_tearoff_menu_item_new (); | |
1677 | gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), tearoff); | |
1678 | ||
1679 | g_signal_connect (G_OBJECT (tearoff), "activate", | |
1680 | G_CALLBACK (tearoff_activate), 0); | |
1681 | } | |
1682 | ||
1683 | for (item = data; item; item = item->next) | |
1684 | { | |
1685 | GtkWidget *w; | |
177c0ea7 | 1686 | |
f392e843 JD |
1687 | if (pop_up_p && !item->contents && !item->call_data |
1688 | && !xg_separator_p (item->name)) | |
1689 | { | |
1690 | char *utf8_label; | |
1691 | /* A title for a popup. We do the same as GTK does when | |
1692 | creating titles, but it does not look good. */ | |
1693 | group = NULL; | |
1694 | utf8_label = get_utf8_string (item->name); | |
1695 | ||
1696 | gtk_menu_set_title (GTK_MENU (wmenu), utf8_label); | |
4b1b4443 | 1697 | w = gtk_menu_item_new_with_label (utf8_label); |
f392e843 JD |
1698 | gtk_widget_set_sensitive (w, FALSE); |
1699 | if (utf8_label && utf8_label != item->name) g_free (utf8_label); | |
1700 | } | |
1701 | else if (xg_separator_p (item->name)) | |
1702 | { | |
1703 | group = NULL; | |
1704 | /* GTK only have one separator type. */ | |
1705 | w = gtk_separator_menu_item_new (); | |
1706 | } | |
1707 | else | |
1708 | { | |
1709 | w = xg_create_one_menuitem (item, | |
1710 | f, | |
1711 | item->contents ? 0 : select_cb, | |
1712 | highlight_cb, | |
1713 | cl_data, | |
1714 | &group); | |
1715 | ||
1716 | if (item->contents) | |
1717 | { | |
1718 | GtkWidget *submenu = create_menus (item->contents, | |
1719 | f, | |
1720 | select_cb, | |
1721 | deactivate_cb, | |
1722 | highlight_cb, | |
1723 | 0, | |
1724 | 0, | |
da18b5ac | 1725 | add_tearoff_p, |
f392e843 JD |
1726 | 0, |
1727 | cl_data, | |
1728 | 0); | |
1729 | gtk_menu_item_set_submenu (GTK_MENU_ITEM (w), submenu); | |
1730 | } | |
f392e843 JD |
1731 | } |
1732 | ||
1733 | gtk_menu_shell_append (GTK_MENU_SHELL (wmenu), w); | |
1734 | gtk_widget_set_name (w, MENU_ITEM_NAME); | |
1735 | } | |
1736 | ||
1737 | return wmenu; | |
1738 | } | |
1739 | ||
1740 | /* Create a menubar, popup menu or dialog, depending on the TYPE argument. | |
1741 | TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog | |
1742 | with some text and buttons. | |
1743 | F is the frame the created item belongs to. | |
1744 | NAME is the name to use for the top widget. | |
1745 | VAL is a widget_value structure describing items to be created. | |
1746 | SELECT_CB is the callback to use when a menu item is selected or | |
1747 | a dialog button is pressed. | |
1748 | DEACTIVATE_CB is the callback to use when an item is deactivated. | |
1749 | For a menu, when a sub menu is not shown anymore, for a dialog it is | |
1750 | called when the dialog is popped down. | |
1751 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
1752 | ||
1753 | Returns the widget created. */ | |
1754 | GtkWidget * | |
1755 | xg_create_widget (type, name, f, val, | |
1756 | select_cb, deactivate_cb, highlight_cb) | |
1757 | char *type; | |
1758 | char *name; | |
1759 | FRAME_PTR f; | |
1760 | widget_value *val; | |
1761 | GCallback select_cb; | |
1762 | GCallback deactivate_cb; | |
1763 | GCallback highlight_cb; | |
1764 | { | |
1765 | GtkWidget *w = 0; | |
da18b5ac JD |
1766 | int menu_bar_p = strcmp (type, "menubar") == 0; |
1767 | int pop_up_p = strcmp (type, "popup") == 0; | |
1768 | ||
f392e843 JD |
1769 | if (strcmp (type, "dialog") == 0) |
1770 | { | |
1771 | w = create_dialog (val, select_cb, deactivate_cb); | |
810f2256 | 1772 | xg_set_screen (w, f); |
f392e843 JD |
1773 | gtk_window_set_transient_for (GTK_WINDOW (w), |
1774 | GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f))); | |
1775 | gtk_window_set_destroy_with_parent (GTK_WINDOW (w), TRUE); | |
810f2256 | 1776 | gtk_widget_set_name (w, "emacs-dialog"); |
f392e843 | 1777 | } |
da18b5ac | 1778 | else if (menu_bar_p || pop_up_p) |
f392e843 JD |
1779 | { |
1780 | w = create_menus (val->contents, | |
1781 | f, | |
1782 | select_cb, | |
1783 | deactivate_cb, | |
1784 | highlight_cb, | |
da18b5ac JD |
1785 | pop_up_p, |
1786 | menu_bar_p, | |
1787 | menu_bar_p, | |
f392e843 JD |
1788 | 0, |
1789 | 0, | |
1790 | name); | |
1791 | ||
1792 | /* Set the cursor to an arrow for popup menus when they are mapped. | |
1793 | This is done by default for menu bar menus. */ | |
da18b5ac | 1794 | if (pop_up_p) |
f392e843 JD |
1795 | { |
1796 | /* Must realize so the GdkWindow inside the widget is created. */ | |
1797 | gtk_widget_realize (w); | |
810f2256 | 1798 | xg_set_cursor (w, FRAME_X_DISPLAY_INFO (f)->xg_cursor); |
f392e843 JD |
1799 | } |
1800 | } | |
1801 | else | |
1802 | { | |
1803 | fprintf (stderr, "bad type in xg_create_widget: %s, doing nothing\n", | |
1804 | type); | |
1805 | } | |
1806 | ||
1807 | return w; | |
1808 | } | |
1809 | ||
0a1d6de0 | 1810 | /* Return the label for menu item WITEM. */ |
f392e843 JD |
1811 | static const char * |
1812 | xg_get_menu_item_label (witem) | |
1813 | GtkMenuItem *witem; | |
1814 | { | |
1815 | GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem))); | |
1816 | return gtk_label_get_label (wlabel); | |
1817 | } | |
1818 | ||
0a1d6de0 | 1819 | /* Return non-zero if the menu item WITEM has the text LABEL. */ |
f392e843 JD |
1820 | static int |
1821 | xg_item_label_same_p (witem, label) | |
1822 | GtkMenuItem *witem; | |
1823 | char *label; | |
1824 | { | |
0a1d6de0 | 1825 | int is_same = 0; |
f392e843 | 1826 | char *utf8_label = get_utf8_string (label); |
0a1d6de0 JD |
1827 | const char *old_label = witem ? xg_get_menu_item_label (witem) : 0; |
1828 | ||
1829 | if (! old_label && ! utf8_label) | |
1830 | is_same = 1; | |
1831 | else if (old_label && utf8_label) | |
1832 | is_same = strcmp (utf8_label, old_label) == 0; | |
1833 | ||
1834 | if (utf8_label && utf8_label != label) g_free (utf8_label); | |
f392e843 JD |
1835 | |
1836 | return is_same; | |
1837 | } | |
1838 | ||
1839 | /* Remove widgets in LIST from container WCONT. */ | |
1840 | static void | |
1841 | remove_from_container (wcont, list) | |
1842 | GtkWidget *wcont; | |
1843 | GList *list; | |
1844 | { | |
f392e843 JD |
1845 | GList *iter; |
1846 | ||
49853a4d | 1847 | for (iter = list; iter; iter = g_list_next (iter)) |
f392e843 JD |
1848 | { |
1849 | GtkWidget *w = GTK_WIDGET (iter->data); | |
1850 | ||
1851 | /* Add a ref to w so we can explicitly destroy it later. */ | |
1852 | gtk_widget_ref (w); | |
1853 | gtk_container_remove (GTK_CONTAINER (wcont), w); | |
177c0ea7 | 1854 | |
f392e843 JD |
1855 | /* If there is a menu under this widget that has been detached, |
1856 | there is a reference to it, and just removing w from the | |
1857 | container does not destroy the submenu. By explicitly | |
1858 | destroying w we make sure the submenu is destroyed, thus | |
1859 | removing the detached window also if there was one. */ | |
1860 | gtk_widget_destroy (w); | |
1861 | } | |
f392e843 JD |
1862 | } |
1863 | ||
1864 | /* Update the top level names in MENUBAR (i.e. not submenus). | |
1865 | F is the frame the menu bar belongs to. | |
49853a4d JD |
1866 | *LIST is a list with the current menu bar names (menu item widgets). |
1867 | ITER is the item within *LIST that shall be updated. | |
1868 | POS is the numerical position, starting at 0, of ITER in *LIST. | |
f392e843 JD |
1869 | VAL describes what the menu bar shall look like after the update. |
1870 | SELECT_CB is the callback to use when a menu item is selected. | |
1871 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
49853a4d | 1872 | CL_DATA points to the callback data to be used for this menu bar. |
f392e843 JD |
1873 | |
1874 | This function calls itself to walk through the menu bar names. */ | |
1875 | static void | |
49853a4d JD |
1876 | xg_update_menubar (menubar, f, list, iter, pos, val, |
1877 | select_cb, highlight_cb, cl_data) | |
f392e843 JD |
1878 | GtkWidget *menubar; |
1879 | FRAME_PTR f; | |
49853a4d JD |
1880 | GList **list; |
1881 | GList *iter; | |
1882 | int pos; | |
f392e843 JD |
1883 | widget_value *val; |
1884 | GCallback select_cb; | |
1885 | GCallback highlight_cb; | |
1886 | xg_menu_cb_data *cl_data; | |
1887 | { | |
49853a4d | 1888 | if (! iter && ! val) |
f392e843 | 1889 | return; |
49853a4d | 1890 | else if (iter && ! val) |
f392e843 | 1891 | { |
49853a4d JD |
1892 | /* Item(s) have been removed. Remove all remaining items. */ |
1893 | remove_from_container (menubar, iter); | |
f392e843 JD |
1894 | |
1895 | /* All updated. */ | |
1896 | val = 0; | |
49853a4d | 1897 | iter = 0; |
f392e843 | 1898 | } |
49853a4d | 1899 | else if (! iter && val) |
f392e843 JD |
1900 | { |
1901 | /* Item(s) added. Add all new items in one call. */ | |
1902 | create_menus (val, f, select_cb, 0, highlight_cb, | |
1903 | 0, 1, 0, menubar, cl_data, 0); | |
1904 | ||
1905 | /* All updated. */ | |
1906 | val = 0; | |
49853a4d | 1907 | iter = 0; |
f392e843 | 1908 | } |
49853a4d JD |
1909 | /* Below this neither iter or val is NULL */ |
1910 | else if (xg_item_label_same_p (GTK_MENU_ITEM (iter->data), val->name)) | |
f392e843 JD |
1911 | { |
1912 | /* This item is still the same, check next item. */ | |
1913 | val = val->next; | |
49853a4d JD |
1914 | iter = g_list_next (iter); |
1915 | ++pos; | |
f392e843 JD |
1916 | } |
1917 | else /* This item is changed. */ | |
1918 | { | |
49853a4d | 1919 | GtkMenuItem *witem = GTK_MENU_ITEM (iter->data); |
f392e843 | 1920 | GtkMenuItem *witem2 = 0; |
f392e843 | 1921 | int val_in_menubar = 0; |
49853a4d JD |
1922 | int iter_in_new_menubar = 0; |
1923 | GList *iter2; | |
f392e843 JD |
1924 | widget_value *cur; |
1925 | ||
f392e843 | 1926 | /* See if the changed entry (val) is present later in the menu bar */ |
49853a4d JD |
1927 | for (iter2 = iter; |
1928 | iter2 && ! val_in_menubar; | |
1929 | iter2 = g_list_next (iter2)) | |
f392e843 | 1930 | { |
49853a4d | 1931 | witem2 = GTK_MENU_ITEM (iter2->data); |
f392e843 JD |
1932 | val_in_menubar = xg_item_label_same_p (witem2, val->name); |
1933 | } | |
1934 | ||
49853a4d | 1935 | /* See if the current entry (iter) is present later in the |
f392e843 | 1936 | specification for the new menu bar. */ |
49853a4d JD |
1937 | for (cur = val; cur && ! iter_in_new_menubar; cur = cur->next) |
1938 | iter_in_new_menubar = xg_item_label_same_p (witem, cur->name); | |
f392e843 | 1939 | |
49853a4d | 1940 | if (val_in_menubar && ! iter_in_new_menubar) |
f392e843 | 1941 | { |
49853a4d JD |
1942 | int nr = pos; |
1943 | ||
f392e843 JD |
1944 | /* This corresponds to: |
1945 | Current: A B C | |
1946 | New: A C | |
1947 | Remove B. */ | |
177c0ea7 | 1948 | |
f392e843 JD |
1949 | gtk_widget_ref (GTK_WIDGET (witem)); |
1950 | gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem)); | |
1951 | gtk_widget_destroy (GTK_WIDGET (witem)); | |
1952 | ||
1953 | /* Must get new list since the old changed. */ | |
49853a4d JD |
1954 | g_list_free (*list); |
1955 | *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar)); | |
1956 | while (nr-- > 0) iter = g_list_next (iter); | |
f392e843 | 1957 | } |
49853a4d | 1958 | else if (! val_in_menubar && ! iter_in_new_menubar) |
f392e843 JD |
1959 | { |
1960 | /* This corresponds to: | |
1961 | Current: A B C | |
1962 | New: A X C | |
1963 | Rename B to X. This might seem to be a strange thing to do, | |
1964 | since if there is a menu under B it will be totally wrong for X. | |
1965 | But consider editing a C file. Then there is a C-mode menu | |
1966 | (corresponds to B above). | |
1967 | If then doing C-x C-f the minibuf menu (X above) replaces the | |
1968 | C-mode menu. When returning from the minibuffer, we get | |
1969 | back the C-mode menu. Thus we do: | |
1970 | Rename B to X (C-mode to minibuf menu) | |
1971 | Rename X to B (minibuf to C-mode menu). | |
1972 | If the X menu hasn't been invoked, the menu under B | |
1973 | is up to date when leaving the minibuffer. */ | |
1974 | GtkLabel *wlabel = GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem))); | |
1975 | char *utf8_label = get_utf8_string (val->name); | |
da18b5ac | 1976 | GtkWidget *submenu = gtk_menu_item_get_submenu (witem); |
177c0ea7 | 1977 | |
4b1b4443 | 1978 | gtk_label_set_text (wlabel, utf8_label); |
f392e843 | 1979 | |
da18b5ac JD |
1980 | /* If this item has a submenu that has been detached, change |
1981 | the title in the WM decorations also. */ | |
1982 | if (submenu && gtk_menu_get_tearoff_state (GTK_MENU (submenu))) | |
1983 | /* Set the title of the detached window. */ | |
1984 | gtk_menu_set_title (GTK_MENU (submenu), utf8_label); | |
1985 | ||
49853a4d | 1986 | iter = g_list_next (iter); |
f392e843 | 1987 | val = val->next; |
49853a4d | 1988 | ++pos; |
f392e843 | 1989 | } |
49853a4d | 1990 | else if (! val_in_menubar && iter_in_new_menubar) |
f392e843 JD |
1991 | { |
1992 | /* This corresponds to: | |
1993 | Current: A B C | |
1994 | New: A X B C | |
1995 | Insert X. */ | |
1996 | ||
49853a4d | 1997 | int nr = pos; |
f392e843 JD |
1998 | GList *group = 0; |
1999 | GtkWidget *w = xg_create_one_menuitem (val, | |
2000 | f, | |
2001 | select_cb, | |
2002 | highlight_cb, | |
2003 | cl_data, | |
2004 | &group); | |
2005 | ||
2006 | gtk_widget_set_name (w, MENU_ITEM_NAME); | |
2007 | gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), w, pos); | |
2008 | ||
49853a4d JD |
2009 | g_list_free (*list); |
2010 | *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar)); | |
2011 | while (nr-- > 0) iter = g_list_next (iter); | |
2012 | iter = g_list_next (iter); | |
f392e843 | 2013 | val = val->next; |
49853a4d | 2014 | ++pos; |
f392e843 | 2015 | } |
49853a4d | 2016 | else /* if (val_in_menubar && iter_in_new_menubar) */ |
f392e843 | 2017 | { |
49853a4d | 2018 | int nr = pos; |
f392e843 JD |
2019 | /* This corresponds to: |
2020 | Current: A B C | |
2021 | New: A C B | |
2022 | Move C before B */ | |
2023 | ||
2024 | gtk_widget_ref (GTK_WIDGET (witem2)); | |
2025 | gtk_container_remove (GTK_CONTAINER (menubar), GTK_WIDGET (witem2)); | |
2026 | gtk_menu_shell_insert (GTK_MENU_SHELL (menubar), | |
2027 | GTK_WIDGET (witem2), pos); | |
2028 | gtk_widget_unref (GTK_WIDGET (witem2)); | |
2029 | ||
49853a4d JD |
2030 | g_list_free (*list); |
2031 | *list = iter = gtk_container_get_children (GTK_CONTAINER (menubar)); | |
2032 | while (nr-- > 0) iter = g_list_next (iter); | |
f392e843 | 2033 | val = val->next; |
49853a4d | 2034 | ++pos; |
f392e843 | 2035 | } |
f392e843 JD |
2036 | } |
2037 | ||
2038 | /* Update the rest of the menu bar. */ | |
49853a4d JD |
2039 | xg_update_menubar (menubar, f, list, iter, pos, val, |
2040 | select_cb, highlight_cb, cl_data); | |
f392e843 JD |
2041 | } |
2042 | ||
2043 | /* Update the menu item W so it corresponds to VAL. | |
2044 | SELECT_CB is the callback to use when a menu item is selected. | |
2045 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
2046 | CL_DATA is the data to set in the widget for menu invokation. */ | |
2047 | static void | |
2048 | xg_update_menu_item (val, w, select_cb, highlight_cb, cl_data) | |
2049 | widget_value *val; | |
2050 | GtkWidget *w; | |
2051 | GCallback select_cb; | |
2052 | GCallback highlight_cb; | |
2053 | xg_menu_cb_data *cl_data; | |
2054 | { | |
2055 | GtkWidget *wchild; | |
2056 | GtkLabel *wlbl = 0; | |
2057 | GtkLabel *wkey = 0; | |
2058 | char *utf8_label; | |
2059 | char *utf8_key; | |
0a1d6de0 JD |
2060 | const char *old_label = 0; |
2061 | const char *old_key = 0; | |
f392e843 | 2062 | xg_menu_item_cb_data *cb_data; |
177c0ea7 JB |
2063 | |
2064 | wchild = gtk_bin_get_child (GTK_BIN (w)); | |
f392e843 JD |
2065 | utf8_label = get_utf8_string (val->name); |
2066 | utf8_key = get_utf8_string (val->key); | |
2067 | ||
2068 | /* See if W is a menu item with a key. See make_menu_item above. */ | |
2069 | if (GTK_IS_HBOX (wchild)) | |
2070 | { | |
2071 | GList *list = gtk_container_get_children (GTK_CONTAINER (wchild)); | |
2072 | ||
2073 | wlbl = GTK_LABEL (list->data); | |
2074 | wkey = GTK_LABEL (list->next->data); | |
49853a4d JD |
2075 | g_list_free (list); |
2076 | ||
f392e843 JD |
2077 | if (! utf8_key) |
2078 | { | |
2079 | /* Remove the key and keep just the label. */ | |
2080 | gtk_widget_ref (GTK_WIDGET (wlbl)); | |
2081 | gtk_container_remove (GTK_CONTAINER (w), wchild); | |
2082 | gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (wlbl)); | |
2083 | wkey = 0; | |
2084 | } | |
49853a4d | 2085 | |
f392e843 JD |
2086 | } |
2087 | else /* Just a label. */ | |
2088 | { | |
2089 | wlbl = GTK_LABEL (wchild); | |
2090 | ||
2091 | /* Check if there is now a key. */ | |
2092 | if (utf8_key) | |
2093 | { | |
2094 | GtkWidget *wtoadd = make_widget_for_menu_item (utf8_label, utf8_key); | |
2095 | GList *list = gtk_container_get_children (GTK_CONTAINER (wtoadd)); | |
49853a4d | 2096 | |
f392e843 JD |
2097 | wlbl = GTK_LABEL (list->data); |
2098 | wkey = GTK_LABEL (list->next->data); | |
49853a4d | 2099 | g_list_free (list); |
f392e843 JD |
2100 | |
2101 | gtk_container_remove (GTK_CONTAINER (w), wchild); | |
2102 | gtk_container_add (GTK_CONTAINER (w), wtoadd); | |
2103 | } | |
2104 | } | |
2105 | ||
177c0ea7 | 2106 | |
0a1d6de0 JD |
2107 | if (wkey) old_key = gtk_label_get_label (wkey); |
2108 | if (wlbl) old_label = gtk_label_get_label (wlbl); | |
177c0ea7 | 2109 | |
0a1d6de0 | 2110 | if (wkey && utf8_key && (! old_key || strcmp (utf8_key, old_key) != 0)) |
f392e843 JD |
2111 | gtk_label_set_text (wkey, utf8_key); |
2112 | ||
0a1d6de0 | 2113 | if (! old_label || strcmp (utf8_label, old_label) != 0) |
4b1b4443 | 2114 | gtk_label_set_text (wlbl, utf8_label); |
f392e843 | 2115 | |
0a1d6de0 JD |
2116 | if (utf8_key && utf8_key != val->key) g_free (utf8_key); |
2117 | if (utf8_label && utf8_label != val->name) g_free (utf8_label); | |
177c0ea7 | 2118 | |
f392e843 JD |
2119 | if (! val->enabled && GTK_WIDGET_SENSITIVE (w)) |
2120 | gtk_widget_set_sensitive (w, FALSE); | |
2121 | else if (val->enabled && ! GTK_WIDGET_SENSITIVE (w)) | |
2122 | gtk_widget_set_sensitive (w, TRUE); | |
2123 | ||
2124 | cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (w), | |
2125 | XG_ITEM_DATA); | |
2126 | if (cb_data) | |
2127 | { | |
2128 | cb_data->call_data = val->call_data; | |
2129 | cb_data->help = val->help; | |
2130 | cb_data->cl_data = cl_data; | |
177c0ea7 | 2131 | |
f392e843 JD |
2132 | /* We assume the callback functions don't change. */ |
2133 | if (val->call_data && ! val->contents) | |
2134 | { | |
2135 | /* This item shall have a select callback. */ | |
2136 | if (! cb_data->select_id) | |
2137 | cb_data->select_id | |
2138 | = g_signal_connect (G_OBJECT (w), "activate", | |
2139 | select_cb, cb_data); | |
2140 | } | |
2141 | else if (cb_data->select_id) | |
2142 | { | |
2143 | g_signal_handler_disconnect (w, cb_data->select_id); | |
2144 | cb_data->select_id = 0; | |
2145 | } | |
2146 | ||
2147 | if (NILP (cb_data->help)) | |
2148 | { | |
2149 | /* Shall not have help. Remove if any existed previously. */ | |
2150 | if (cb_data->highlight_id) | |
2151 | { | |
2152 | g_signal_handler_disconnect (G_OBJECT (w), | |
2153 | cb_data->highlight_id); | |
2154 | cb_data->highlight_id = 0; | |
2155 | } | |
2156 | if (cb_data->unhighlight_id) | |
2157 | { | |
2158 | g_signal_handler_disconnect (G_OBJECT (w), | |
2159 | cb_data->unhighlight_id); | |
2160 | cb_data->unhighlight_id = 0; | |
2161 | } | |
2162 | } | |
2163 | else if (! cb_data->highlight_id && highlight_cb) | |
2164 | { | |
2165 | /* Have help now, but didn't previously. Add callback. */ | |
2166 | cb_data->highlight_id | |
2167 | = g_signal_connect (G_OBJECT (w), | |
2168 | "enter-notify-event", | |
2169 | G_CALLBACK (menuitem_highlight_callback), | |
2170 | cb_data); | |
2171 | cb_data->unhighlight_id | |
2172 | = g_signal_connect (G_OBJECT (w), | |
2173 | "leave-notify-event", | |
2174 | G_CALLBACK (menuitem_highlight_callback), | |
2175 | cb_data); | |
2176 | } | |
2177 | } | |
2178 | } | |
2179 | ||
2180 | /* Update the toggle menu item W so it corresponds to VAL. */ | |
2181 | static void | |
2182 | xg_update_toggle_item (val, w) | |
2183 | widget_value *val; | |
2184 | GtkWidget *w; | |
2185 | { | |
2186 | gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected); | |
2187 | } | |
2188 | ||
2189 | /* Update the radio menu item W so it corresponds to VAL. */ | |
2190 | static void | |
2191 | xg_update_radio_item (val, w) | |
2192 | widget_value *val; | |
2193 | GtkWidget *w; | |
2194 | { | |
2195 | gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), val->selected); | |
2196 | } | |
2197 | ||
2198 | /* Update the sub menu SUBMENU and all its children so it corresponds to VAL. | |
2199 | SUBMENU may be NULL, in that case a new menu is created. | |
2200 | F is the frame the menu bar belongs to. | |
2201 | VAL describes the contents of the menu bar. | |
2202 | SELECT_CB is the callback to use when a menu item is selected. | |
2203 | DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore. | |
2204 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. | |
2205 | CL_DATA is the call back data to use for any newly created items. | |
2206 | ||
2207 | Returns the updated submenu widget, that is SUBMENU unless SUBMENU | |
2208 | was NULL. */ | |
2209 | ||
2210 | static GtkWidget * | |
2211 | xg_update_submenu (submenu, f, val, | |
2212 | select_cb, deactivate_cb, highlight_cb, cl_data) | |
2213 | GtkWidget *submenu; | |
2214 | FRAME_PTR f; | |
2215 | widget_value *val; | |
2216 | GCallback select_cb; | |
2217 | GCallback deactivate_cb; | |
2218 | GCallback highlight_cb; | |
2219 | xg_menu_cb_data *cl_data; | |
2220 | { | |
2221 | GtkWidget *newsub = submenu; | |
2222 | GList *list = 0; | |
2223 | GList *iter; | |
2224 | widget_value *cur; | |
2225 | int has_tearoff_p = 0; | |
2226 | GList *first_radio = 0; | |
177c0ea7 | 2227 | |
f392e843 JD |
2228 | if (submenu) |
2229 | list = gtk_container_get_children (GTK_CONTAINER (submenu)); | |
177c0ea7 | 2230 | |
f392e843 JD |
2231 | for (cur = val, iter = list; |
2232 | cur && iter; | |
2233 | iter = g_list_next (iter), cur = cur->next) | |
2234 | { | |
2235 | GtkWidget *w = GTK_WIDGET (iter->data); | |
2236 | ||
2237 | /* Skip tearoff items, they have no counterpart in val. */ | |
2238 | if (GTK_IS_TEAROFF_MENU_ITEM (w)) | |
2239 | { | |
2240 | has_tearoff_p = 1; | |
2241 | iter = g_list_next (iter); | |
2242 | if (iter) w = GTK_WIDGET (iter->data); | |
2243 | else break; | |
2244 | } | |
2245 | ||
2246 | /* Remember first radio button in a group. If we get a mismatch in | |
2247 | a radio group we must rebuild the whole group so that the connections | |
2248 | in GTK becomes correct. */ | |
2249 | if (cur->button_type == BUTTON_TYPE_RADIO && ! first_radio) | |
2250 | first_radio = iter; | |
2251 | else if (cur->button_type != BUTTON_TYPE_RADIO | |
2252 | && ! GTK_IS_RADIO_MENU_ITEM (w)) | |
2253 | first_radio = 0; | |
2254 | ||
2255 | if (GTK_IS_SEPARATOR_MENU_ITEM (w)) | |
2256 | { | |
2257 | if (! xg_separator_p (cur->name)) | |
2258 | break; | |
2259 | } | |
2260 | else if (GTK_IS_CHECK_MENU_ITEM (w)) | |
2261 | { | |
2262 | if (cur->button_type != BUTTON_TYPE_TOGGLE) | |
2263 | break; | |
2264 | xg_update_toggle_item (cur, w); | |
2265 | xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data); | |
2266 | } | |
2267 | else if (GTK_IS_RADIO_MENU_ITEM (w)) | |
2268 | { | |
2269 | if (cur->button_type != BUTTON_TYPE_RADIO) | |
2270 | break; | |
2271 | xg_update_radio_item (cur, w); | |
2272 | xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data); | |
2273 | } | |
2274 | else if (GTK_IS_MENU_ITEM (w)) | |
2275 | { | |
2276 | GtkMenuItem *witem = GTK_MENU_ITEM (w); | |
2277 | GtkWidget *sub; | |
2278 | ||
2279 | if (cur->button_type != BUTTON_TYPE_NONE || | |
2280 | xg_separator_p (cur->name)) | |
2281 | break; | |
2282 | ||
2283 | xg_update_menu_item (cur, w, select_cb, highlight_cb, cl_data); | |
2284 | ||
2285 | sub = gtk_menu_item_get_submenu (witem); | |
2286 | if (sub && ! cur->contents) | |
2287 | { | |
2288 | /* Not a submenu anymore. */ | |
2289 | gtk_widget_ref (sub); | |
2290 | gtk_menu_item_remove_submenu (witem); | |
2291 | gtk_widget_destroy (sub); | |
2292 | } | |
2293 | else if (cur->contents) | |
2294 | { | |
2295 | GtkWidget *nsub; | |
2296 | ||
2297 | nsub = xg_update_submenu (sub, f, cur->contents, | |
2298 | select_cb, deactivate_cb, | |
2299 | highlight_cb, cl_data); | |
2300 | ||
2301 | /* If this item just became a submenu, we must set it. */ | |
2302 | if (nsub != sub) | |
2303 | gtk_menu_item_set_submenu (witem, nsub); | |
2304 | } | |
2305 | } | |
2306 | else | |
2307 | { | |
2308 | /* Structural difference. Remove everything from here and down | |
2309 | in SUBMENU. */ | |
2310 | break; | |
2311 | } | |
2312 | } | |
2313 | ||
2314 | /* Remove widgets from first structual change. */ | |
2315 | if (iter) | |
2316 | { | |
2317 | /* If we are adding new menu items below, we must remove from | |
2318 | first radio button so that radio groups become correct. */ | |
2319 | if (cur && first_radio) remove_from_container (submenu, first_radio); | |
2320 | else remove_from_container (submenu, iter); | |
2321 | } | |
177c0ea7 | 2322 | |
f392e843 JD |
2323 | if (cur) |
2324 | { | |
2325 | /* More items added. Create them. */ | |
2326 | newsub = create_menus (cur, | |
2327 | f, | |
2328 | select_cb, | |
2329 | deactivate_cb, | |
2330 | highlight_cb, | |
2331 | 0, | |
2332 | 0, | |
2333 | ! has_tearoff_p, | |
2334 | submenu, | |
2335 | cl_data, | |
2336 | 0); | |
2337 | } | |
177c0ea7 | 2338 | |
49853a4d JD |
2339 | if (list) g_list_free (list); |
2340 | ||
f392e843 JD |
2341 | return newsub; |
2342 | } | |
2343 | ||
2344 | /* Update the MENUBAR. | |
2345 | F is the frame the menu bar belongs to. | |
2346 | VAL describes the contents of the menu bar. | |
2347 | If DEEP_P is non-zero, rebuild all but the top level menu names in | |
2348 | the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar. | |
2349 | SELECT_CB is the callback to use when a menu item is selected. | |
2350 | DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore. | |
2351 | HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */ | |
2352 | void | |
2353 | xg_modify_menubar_widgets (menubar, f, val, deep_p, | |
2354 | select_cb, deactivate_cb, highlight_cb) | |
2355 | GtkWidget *menubar; | |
2356 | FRAME_PTR f; | |
2357 | widget_value *val; | |
2358 | int deep_p; | |
2359 | GCallback select_cb; | |
2360 | GCallback deactivate_cb; | |
2361 | GCallback highlight_cb; | |
2362 | { | |
2363 | xg_menu_cb_data *cl_data; | |
2364 | GList *list = gtk_container_get_children (GTK_CONTAINER (menubar)); | |
f392e843 JD |
2365 | |
2366 | if (! list) return; | |
177c0ea7 | 2367 | |
f392e843 JD |
2368 | cl_data = (xg_menu_cb_data*) g_object_get_data (G_OBJECT (menubar), |
2369 | XG_FRAME_DATA); | |
2370 | ||
da18b5ac JD |
2371 | xg_update_menubar (menubar, f, &list, list, 0, val->contents, |
2372 | select_cb, highlight_cb, cl_data); | |
2373 | ||
2374 | if (deep_p); | |
f392e843 JD |
2375 | { |
2376 | widget_value *cur; | |
2377 | ||
2378 | /* Update all sub menus. | |
da18b5ac | 2379 | We must keep the submenus (GTK menu item widgets) since the |
f392e843 JD |
2380 | X Window in the XEvent that activates the menu are those widgets. */ |
2381 | ||
2382 | /* Update cl_data, menu_item things in F may have changed. */ | |
2383 | update_cl_data (cl_data, f, highlight_cb); | |
2384 | ||
2385 | for (cur = val->contents; cur; cur = cur->next) | |
2386 | { | |
49853a4d | 2387 | GList *iter; |
f392e843 JD |
2388 | GtkWidget *sub = 0; |
2389 | GtkWidget *newsub; | |
2390 | GtkMenuItem *witem; | |
2391 | ||
2392 | /* Find sub menu that corresponds to val and update it. */ | |
2393 | for (iter = list ; iter; iter = g_list_next (iter)) | |
2394 | { | |
2395 | witem = GTK_MENU_ITEM (iter->data); | |
2396 | if (xg_item_label_same_p (witem, cur->name)) | |
2397 | { | |
2398 | sub = gtk_menu_item_get_submenu (witem); | |
2399 | break; | |
2400 | } | |
2401 | } | |
177c0ea7 | 2402 | |
f392e843 JD |
2403 | newsub = xg_update_submenu (sub, |
2404 | f, | |
2405 | cur->contents, | |
2406 | select_cb, | |
2407 | deactivate_cb, | |
2408 | highlight_cb, | |
2409 | cl_data); | |
2410 | /* sub may still be NULL. If we just updated non deep and added | |
2411 | a new menu bar item, it has no sub menu yet. So we set the | |
2412 | newly created sub menu under witem. */ | |
2413 | if (newsub != sub) | |
810f2256 JD |
2414 | { |
2415 | xg_set_screen (newsub, f); | |
2416 | gtk_menu_item_set_submenu (witem, newsub); | |
2417 | } | |
f392e843 JD |
2418 | } |
2419 | } | |
2420 | ||
49853a4d | 2421 | g_list_free (list); |
f392e843 JD |
2422 | gtk_widget_show_all (menubar); |
2423 | } | |
2424 | ||
2425 | /* Recompute all the widgets of frame F, when the menu bar has been | |
2426 | changed. Value is non-zero if widgets were updated. */ | |
2427 | ||
2428 | int | |
2429 | xg_update_frame_menubar (f) | |
2430 | FRAME_PTR f; | |
2431 | { | |
2432 | struct x_output *x = f->output_data.x; | |
2433 | GtkRequisition req; | |
177c0ea7 | 2434 | |
f392e843 JD |
2435 | if (!x->menubar_widget || GTK_WIDGET_MAPPED (x->menubar_widget)) |
2436 | return 0; | |
2437 | ||
2438 | BLOCK_INPUT; | |
2439 | ||
2440 | gtk_box_pack_start (GTK_BOX (x->vbox_widget), x->menubar_widget, | |
2441 | FALSE, FALSE, 0); | |
2442 | gtk_box_reorder_child (GTK_BOX (x->vbox_widget), x->menubar_widget, 0); | |
2443 | ||
2444 | gtk_widget_show_all (x->menubar_widget); | |
2445 | gtk_widget_size_request (x->menubar_widget, &req); | |
2446 | ||
2447 | FRAME_MENUBAR_HEIGHT (f) = req.height; | |
2448 | ||
2449 | /* The height has changed, resize outer widget and set columns | |
2450 | rows to what we had before adding the menu bar. */ | |
be786000 | 2451 | xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f)); |
177c0ea7 | 2452 | |
f392e843 JD |
2453 | SET_FRAME_GARBAGED (f); |
2454 | UNBLOCK_INPUT; | |
a8303f92 AS |
2455 | |
2456 | return 1; | |
f392e843 JD |
2457 | } |
2458 | ||
2459 | /* Get rid of the menu bar of frame F, and free its storage. | |
2460 | This is used when deleting a frame, and when turning off the menu bar. */ | |
2461 | ||
2462 | void | |
2463 | free_frame_menubar (f) | |
2464 | FRAME_PTR f; | |
2465 | { | |
2466 | struct x_output *x = f->output_data.x; | |
2467 | ||
2468 | if (x->menubar_widget) | |
2469 | { | |
2470 | BLOCK_INPUT; | |
2471 | ||
2472 | gtk_container_remove (GTK_CONTAINER (x->vbox_widget), x->menubar_widget); | |
2473 | /* The menubar and its children shall be deleted when removed from | |
2474 | the container. */ | |
2475 | x->menubar_widget = 0; | |
2476 | FRAME_MENUBAR_HEIGHT (f) = 0; | |
2477 | ||
2478 | /* The height has changed, resize outer widget and set columns | |
2479 | rows to what we had before removing the menu bar. */ | |
be786000 | 2480 | xg_resize_outer_widget (f, FRAME_COLS (f), FRAME_LINES (f)); |
f392e843 JD |
2481 | |
2482 | SET_FRAME_GARBAGED (f); | |
2483 | UNBLOCK_INPUT; | |
2484 | } | |
2485 | } | |
2486 | ||
2487 | ||
2488 | \f | |
2489 | /*********************************************************************** | |
2490 | Scroll bar functions | |
2491 | ***********************************************************************/ | |
2492 | ||
2493 | ||
2494 | /* Setting scroll bar values invokes the callback. Use this variable | |
2495 | to indicate that callback should do nothing. */ | |
2496 | int xg_ignore_gtk_scrollbar; | |
2497 | ||
f392e843 JD |
2498 | /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in |
2499 | 32 bits. But we want to store pointers, and they may be larger | |
2500 | than 32 bits. Keep a mapping from integer index to widget pointers | |
2501 | to get around the 32 bit limitation. */ | |
2502 | static struct | |
2503 | { | |
2504 | GtkWidget **widgets; | |
2505 | int max_size; | |
2506 | int used; | |
81e302ef | 2507 | } id_to_widget; |
f392e843 JD |
2508 | |
2509 | /* Grow this much every time we need to allocate more */ | |
2510 | #define ID_TO_WIDGET_INCR 32 | |
2511 | ||
2512 | /* Store the widget pointer W in id_to_widget and return the integer index. */ | |
2513 | static int | |
2514 | xg_store_widget_in_map (w) | |
2515 | GtkWidget *w; | |
2516 | { | |
2517 | int i; | |
2518 | ||
2519 | if (id_to_widget.max_size == id_to_widget.used) | |
2520 | { | |
2521 | int new_size = id_to_widget.max_size + ID_TO_WIDGET_INCR; | |
2522 | ||
2523 | id_to_widget.widgets = xrealloc (id_to_widget.widgets, | |
2524 | sizeof (GtkWidget *)*new_size); | |
2525 | ||
2526 | for (i = id_to_widget.max_size; i < new_size; ++i) | |
2527 | id_to_widget.widgets[i] = 0; | |
2528 | id_to_widget.max_size = new_size; | |
2529 | } | |
2530 | ||
2531 | /* Just loop over the array and find a free place. After all, | |
2532 | how many scroll bars are we creating? Should be a small number. | |
2533 | The check above guarantees we will find a free place. */ | |
2534 | for (i = 0; i < id_to_widget.max_size; ++i) | |
2535 | { | |
2536 | if (! id_to_widget.widgets[i]) | |
2537 | { | |
2538 | id_to_widget.widgets[i] = w; | |
2539 | ++id_to_widget.used; | |
2540 | ||
2541 | return i; | |
2542 | } | |
2543 | } | |
2544 | ||
2545 | /* Should never end up here */ | |
2546 | abort (); | |
2547 | } | |
2548 | ||
2549 | /* Remove pointer at IDX from id_to_widget. | |
2550 | Called when scroll bar is destroyed. */ | |
2551 | static void | |
2552 | xg_remove_widget_from_map (idx) | |
2553 | int idx; | |
2554 | { | |
2555 | if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0) | |
2556 | { | |
2557 | id_to_widget.widgets[idx] = 0; | |
2558 | --id_to_widget.used; | |
2559 | } | |
2560 | } | |
2561 | ||
2562 | /* Get the widget pointer at IDX from id_to_widget. */ | |
2563 | static GtkWidget * | |
2564 | xg_get_widget_from_map (idx) | |
2565 | int idx; | |
2566 | { | |
2567 | if (idx < id_to_widget.max_size && id_to_widget.widgets[idx] != 0) | |
2568 | return id_to_widget.widgets[idx]; | |
2569 | ||
2570 | return 0; | |
2571 | } | |
2572 | ||
810f2256 | 2573 | /* Return the scrollbar id for X Window WID on display DPY. |
3a8a22fc JD |
2574 | Return -1 if WID not in id_to_widget. */ |
2575 | int | |
810f2256 JD |
2576 | xg_get_scroll_id_for_window (dpy, wid) |
2577 | Display *dpy; | |
3a8a22fc JD |
2578 | Window wid; |
2579 | { | |
2580 | int idx; | |
2581 | GtkWidget *w; | |
2582 | ||
810f2256 | 2583 | w = xg_win_to_widget (dpy, wid); |
3a8a22fc JD |
2584 | |
2585 | if (w) | |
2586 | { | |
2587 | for (idx = 0; idx < id_to_widget.max_size; ++idx) | |
2588 | if (id_to_widget.widgets[idx] == w) | |
2589 | return idx; | |
2590 | } | |
2591 | ||
2592 | return -1; | |
2593 | } | |
2594 | ||
f392e843 JD |
2595 | /* Callback invoked when scroll bar WIDGET is destroyed. |
2596 | DATA is the index into id_to_widget for WIDGET. | |
cea9be54 | 2597 | We free pointer to last scroll bar values here and remove the index. */ |
f392e843 JD |
2598 | static void |
2599 | xg_gtk_scroll_destroy (widget, data) | |
2600 | GtkWidget *widget; | |
2601 | gpointer data; | |
2602 | { | |
2603 | gpointer p; | |
2604 | int id = (int)data; | |
177c0ea7 | 2605 | |
f392e843 JD |
2606 | p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA); |
2607 | if (p) xfree (p); | |
2608 | xg_remove_widget_from_map (id); | |
2609 | } | |
2610 | ||
2611 | /* Callback for button press/release events. Used to start timer so that | |
2612 | the scroll bar repetition timer in GTK gets handeled. | |
17097258 | 2613 | Also, sets bar->dragging to Qnil when dragging (button release) is done. |
f392e843 JD |
2614 | WIDGET is the scroll bar widget the event is for (not used). |
2615 | EVENT contains the event. | |
17097258 | 2616 | USER_DATA points to the struct scrollbar structure. |
f392e843 JD |
2617 | |
2618 | Returns FALSE to tell GTK that it shall continue propagate the event | |
2619 | to widgets. */ | |
2620 | static gboolean | |
2621 | scroll_bar_button_cb (widget, event, user_data) | |
2622 | GtkWidget *widget; | |
2623 | GdkEventButton *event; | |
2624 | gpointer user_data; | |
2625 | { | |
2626 | if (event->type == GDK_BUTTON_PRESS && ! xg_timer) | |
2627 | xg_start_timer (); | |
17097258 JD |
2628 | else if (event->type == GDK_BUTTON_RELEASE) |
2629 | { | |
2630 | struct scroll_bar *bar = (struct scroll_bar *) user_data; | |
2631 | if (xg_timer) xg_stop_timer (); | |
2632 | bar->dragging = Qnil; | |
2633 | } | |
2634 | ||
f392e843 JD |
2635 | return FALSE; |
2636 | } | |
2637 | ||
2638 | /* Create a scroll bar widget for frame F. Store the scroll bar | |
2639 | in BAR. | |
2640 | SCROLL_CALLBACK is the callback to invoke when the value of the | |
2641 | bar changes. | |
2642 | SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used | |
2643 | to set resources for the widget. */ | |
2644 | void | |
2645 | xg_create_scroll_bar (f, bar, scroll_callback, scroll_bar_name) | |
2646 | FRAME_PTR f; | |
2647 | struct scroll_bar *bar; | |
2648 | GCallback scroll_callback; | |
2649 | char *scroll_bar_name; | |
2650 | { | |
2651 | GtkWidget *wscroll; | |
2652 | GtkObject *vadj; | |
2653 | int scroll_id; | |
177c0ea7 | 2654 | |
f392e843 JD |
2655 | /* Page, step increment values are not so important here, they |
2656 | will be corrected in x_set_toolkit_scroll_bar_thumb. */ | |
2657 | vadj = gtk_adjustment_new (XG_SB_MIN, XG_SB_MIN, XG_SB_MAX, | |
2658 | 0.1, 0.1, 0.1); | |
2659 | ||
2660 | wscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj)); | |
2661 | gtk_widget_set_name (wscroll, scroll_bar_name); | |
2662 | gtk_range_set_update_policy (GTK_RANGE (wscroll), GTK_UPDATE_CONTINUOUS); | |
177c0ea7 | 2663 | |
f392e843 | 2664 | scroll_id = xg_store_widget_in_map (wscroll); |
177c0ea7 | 2665 | |
f979dc05 | 2666 | g_signal_connect (G_OBJECT (wscroll), |
f392e843 JD |
2667 | "value-changed", |
2668 | scroll_callback, | |
f979dc05 | 2669 | (gpointer) bar); |
f392e843 JD |
2670 | g_signal_connect (G_OBJECT (wscroll), |
2671 | "destroy", | |
2672 | G_CALLBACK (xg_gtk_scroll_destroy), | |
f979dc05 | 2673 | (gpointer) scroll_id); |
f392e843 JD |
2674 | |
2675 | /* Connect to button press and button release to detect if any scroll bar | |
2676 | has the pointer. */ | |
2677 | g_signal_connect (G_OBJECT (wscroll), | |
2678 | "button-press-event", | |
2679 | G_CALLBACK (scroll_bar_button_cb), | |
f979dc05 | 2680 | (gpointer) bar); |
f392e843 JD |
2681 | g_signal_connect (G_OBJECT (wscroll), |
2682 | "button-release-event", | |
2683 | G_CALLBACK (scroll_bar_button_cb), | |
f979dc05 | 2684 | (gpointer) bar); |
177c0ea7 | 2685 | |
f392e843 | 2686 | gtk_fixed_put (GTK_FIXED (f->output_data.x->edit_widget), |
17097258 | 2687 | wscroll, -1, -1); |
f392e843 JD |
2688 | |
2689 | /* Set the cursor to an arrow. */ | |
810f2256 | 2690 | xg_set_cursor (wscroll, FRAME_X_DISPLAY_INFO (f)->xg_cursor); |
f392e843 JD |
2691 | |
2692 | SET_SCROLL_BAR_X_WINDOW (bar, scroll_id); | |
2693 | } | |
2694 | ||
2695 | /* Make the scroll bar represented by SCROLLBAR_ID visible. */ | |
2696 | void | |
2697 | xg_show_scroll_bar (scrollbar_id) | |
2698 | int scrollbar_id; | |
2699 | { | |
2700 | GtkWidget *w = xg_get_widget_from_map (scrollbar_id); | |
2701 | if (w) | |
2702 | gtk_widget_show (w); | |
2703 | } | |
2704 | ||
2705 | /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */ | |
2706 | void | |
2707 | xg_remove_scroll_bar (f, scrollbar_id) | |
2708 | FRAME_PTR f; | |
2709 | int scrollbar_id; | |
2710 | { | |
2711 | GtkWidget *w = xg_get_widget_from_map (scrollbar_id); | |
2712 | if (w) | |
2713 | { | |
2714 | gtk_widget_destroy (w); | |
2715 | SET_FRAME_GARBAGED (f); | |
2716 | } | |
2717 | } | |
2718 | ||
17097258 JD |
2719 | /* Find left/top for widget W in GtkFixed widget WFIXED. */ |
2720 | static void | |
2721 | xg_find_top_left_in_fixed (w, wfixed, left, top) | |
2722 | GtkWidget *w, *wfixed; | |
2723 | int *left, *top; | |
2724 | { | |
2725 | GList *iter; | |
2726 | ||
2727 | for (iter = GTK_FIXED (wfixed)->children; iter; iter = g_list_next (iter)) | |
2728 | { | |
2729 | GtkFixedChild *child = (GtkFixedChild *) iter->data; | |
2730 | ||
2731 | if (child->widget == w) | |
2732 | { | |
2733 | *left = child->x; | |
2734 | *top = child->y; | |
2735 | return; | |
2736 | } | |
2737 | } | |
2738 | ||
2739 | /* Shall never end up here. */ | |
2740 | abort (); | |
2741 | } | |
f392e843 JD |
2742 | |
2743 | /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID | |
2744 | in frame F. | |
2745 | TOP/LEFT are the new pixel positions where the bar shall appear. | |
2746 | WIDTH, HEIGHT is the size in pixels the bar shall have. */ | |
2747 | void | |
7863d625 JD |
2748 | xg_update_scrollbar_pos (f, scrollbar_id, top, left, width, height, |
2749 | real_left, canon_width) | |
f392e843 JD |
2750 | FRAME_PTR f; |
2751 | int scrollbar_id; | |
2752 | int top; | |
2753 | int left; | |
2754 | int width; | |
2755 | int height; | |
2756 | { | |
f392e843 | 2757 | |
49853a4d | 2758 | GtkWidget *wscroll = xg_get_widget_from_map (scrollbar_id); |
7863d625 | 2759 | |
49853a4d JD |
2760 | if (wscroll) |
2761 | { | |
cea9be54 | 2762 | GtkWidget *wfixed = f->output_data.x->edit_widget; |
f392e843 | 2763 | |
ab2d724b | 2764 | gtk_container_set_reallocate_redraws (GTK_CONTAINER (wfixed), TRUE); |
7863d625 | 2765 | |
ab2d724b JD |
2766 | /* Move and resize to new values. */ |
2767 | gtk_fixed_move (GTK_FIXED (wfixed), wscroll, left, top); | |
2768 | gtk_widget_set_size_request (wscroll, width, height); | |
17097258 | 2769 | |
ab2d724b JD |
2770 | /* Must force out update so changed scroll bars gets redrawn. */ |
2771 | gdk_window_process_all_updates (); | |
2772 | ||
17097258 JD |
2773 | /* Scroll bars in GTK has a fixed width, so if we say width 16, it |
2774 | will only be its fixed width (14 is default) anyway, the rest is | |
2775 | blank. We are drawing the mode line across scroll bars when | |
2776 | the frame is split: | |
2777 | |bar| |fringe| | |
2778 | ---------------- | |
2779 | mode line | |
2780 | ---------------- | |
2781 | |bar| |fringe| | |
2782 | ||
2783 | When we "unsplit" the frame: | |
2784 | ||
2785 | |bar| |fringe| | |
2786 | -| |-| | | |
2787 |