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