1 /* Functions for creating and updating GTK widgets.
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include "blockinput.h"
33 #include "termhooks.h"
34 #include <gdk/gdkkeysyms.h>
36 #define FRAME_TOTAL_PIXEL_HEIGHT(f) \
37 (PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
39 /***********************************************************************
41 ***********************************************************************/
42 /* The timer for scroll bar repetition and menu bar timeouts.
43 NULL if no timer is started. */
44 static struct atimer
*xg_timer
;
46 /* The cursor used for scroll bars and popup menus.
47 We only have one cursor for all scroll bars and all popup menus. */
48 static GdkCursor
*xg_left_ptr_cursor
;
51 /* The next two variables and functions are taken from lwlib. */
52 static widget_value
*widget_value_free_list
;
53 static int malloc_cpt
;
55 /* Allocate a widget_value structure, either by taking one from the
56 widget_value_free_list or by malloc:ing a new one.
58 Return a pointer to the allocated structure. */
60 malloc_widget_value ()
63 if (widget_value_free_list
)
65 wv
= widget_value_free_list
;
66 widget_value_free_list
= wv
->free_list
;
71 wv
= (widget_value
*) malloc (sizeof (widget_value
));
74 memset (wv
, 0, sizeof (widget_value
));
78 /* This is analogous to free. It frees only what was allocated
79 by malloc_widget_value, and no substructures. */
81 free_widget_value (wv
)
89 /* When the number of already allocated cells is too big,
96 wv
->free_list
= widget_value_free_list
;
97 widget_value_free_list
= wv
;
101 /* Set *CURSOR on W and all widgets W contain. We must do like this
102 for scroll bars and menu because they create widgets internally,
103 and it is those widgets that are visible.
105 If *CURSOR is NULL, create a GDK_LEFT_PTR cursor and set *CURSOR to
106 the created cursor. */
108 xg_set_cursor (w
, cursor
)
112 GList
*children
= gdk_window_peek_children (w
->window
);
114 /* Create the cursor unless already created. */
116 *cursor
= gdk_cursor_new (GDK_LEFT_PTR
);
118 gdk_window_set_cursor (w
->window
, *cursor
);
120 /* The scroll bar widget has more than one GDK window (had to look at
121 the source to figure this out), and there is no way to set cursor
122 on widgets in GTK. So we must set the cursor for all GDK windows.
125 for ( ; children
; children
= g_list_next (children
))
126 gdk_window_set_cursor (GDK_WINDOW (children
->data
), *cursor
);
129 /* Timer function called when a timeout occurs for xg_timer.
130 This function processes all GTK events in a recursive event loop.
131 This is done because GTK timer events are not seen by Emacs event
132 detection, Emacs only looks for X events. When a scroll bar has the
133 pointer (detected by button press/release events below) an Emacs
134 timer is started, and this function can then check if the GTK timer
135 has expired by calling the GTK event loop.
136 Also, when a menu is active, it has a small timeout before it
137 pops down the sub menu under it. */
139 xg_process_timeouts (timer
)
140 struct atimer
*timer
;
143 /* Ideally we would like to just handle timer events, like the Xt version
144 of this does in xterm.c, but there is no such feature in GTK. */
145 while (gtk_events_pending ())
146 gtk_main_iteration ();
150 /* Start the xg_timer with an interval of 0.1 seconds, if not already started.
151 xg_process_timeouts is called when the timer expires. The timer
152 stared is continuous, i.e. runs until xg_stop_timer is called. */
159 EMACS_SET_SECS_USECS (interval
, 0, 100000);
160 xg_timer
= start_atimer (ATIMER_CONTINUOUS
,
167 /* Stop the xg_timer if started. */
173 cancel_atimer (xg_timer
);
178 /* Insert NODE into linked LIST. */
180 xg_list_insert (xg_list_node
*list
, xg_list_node
*node
)
182 xg_list_node
*list_start
= list
->next
;
184 if (list_start
) list_start
->prev
= node
;
185 node
->next
= list_start
;
190 /* Remove NODE from linked LIST. */
192 xg_list_remove (xg_list_node
*list
, xg_list_node
*node
)
194 xg_list_node
*list_start
= list
->next
;
195 if (node
== list_start
)
197 list
->next
= node
->next
;
198 if (list
->next
) list
->next
->prev
= 0;
202 node
->prev
->next
= node
->next
;
203 if (node
->next
) node
->next
->prev
= node
->prev
;
207 /* Allocate and return a utf8 version of STR. If STR is already
208 utf8 or NULL, just return STR.
209 If not, a new string is allocated and the caller must free the result
212 get_utf8_string (str
)
215 char *utf8_str
= str
;
217 /* If not UTF-8, try current locale. */
218 if (str
&& !g_utf8_validate (str
, -1, NULL
))
219 utf8_str
= g_locale_to_utf8 (str
, -1, 0, 0, 0);
226 /***********************************************************************
227 General functions for creating widgets, resizing, events, e.t.c.
228 ***********************************************************************/
230 /* Make a geometry string and pass that to GTK. It seems this is the
231 only way to get geometry position right if the user explicitly
232 asked for a position when starting Emacs.
233 F is the frame we shall set geometry for. */
238 if (f
->output_data
.x
->size_hint_flags
& USPosition
)
240 int left
= f
->output_data
.x
->left_pos
;
241 int xneg
= f
->output_data
.x
->size_hint_flags
& XNegative
;
242 int top
= f
->output_data
.x
->top_pos
;
243 int yneg
= f
->output_data
.x
->size_hint_flags
& YNegative
;
251 sprintf (geom_str
, "=%dx%d%c%d%c%d",
253 FRAME_TOTAL_PIXEL_HEIGHT (f
),
254 (xneg
? '-' : '+'), left
,
255 (yneg
? '-' : '+'), top
);
257 if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
259 fprintf (stderr
, "Failed to parse: '%s'\n", geom_str
);
264 /* Resize the outer window of frame F after chainging the height.
265 This happend when the menu bar or the tool bar is added or removed.
266 COLUMNS/ROWS is the size the edit area shall have after the resize. */
268 xg_resize_outer_widget (f
, columns
, rows
)
273 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
274 PIXEL_WIDTH (f
), FRAME_TOTAL_PIXEL_HEIGHT (f
));
276 /* base_height is now changed. */
277 x_wm_set_size_hint (f
, 0, 0);
279 /* If we are not mapped yet, set geometry once again, as window
280 height now have changed. */
281 if (! GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f
)))
284 xg_frame_set_char_size (f
, columns
, rows
);
285 gdk_window_process_all_updates ();
288 /* Function to handle resize of our widgets. Since Emacs has some layouts
289 that does not fit well with GTK standard containers, we do most layout
291 F is the frame to resize.
292 PIXELWIDTH, PIXELHEIGHT is the new size in pixels. */
294 xg_resize_widgets (f
, pixelwidth
, pixelheight
)
296 int pixelwidth
, pixelheight
;
298 int mbheight
= FRAME_MENUBAR_HEIGHT (f
);
299 int tbheight
= FRAME_TOOLBAR_HEIGHT (f
);
300 int rows
= PIXEL_TO_CHAR_HEIGHT (f
, pixelheight
- mbheight
- tbheight
);
301 int columns
= PIXEL_TO_CHAR_WIDTH (f
, pixelwidth
);
303 if (FRAME_GTK_WIDGET (f
)
304 && (columns
!= FRAME_WIDTH (f
) || rows
!= FRAME_HEIGHT (f
)
305 || pixelwidth
!= PIXEL_WIDTH (f
) || pixelheight
!= PIXEL_HEIGHT (f
)))
307 struct x_output
*x
= f
->output_data
.x
;
310 all
.y
= mbheight
+ tbheight
;
313 all
.width
= pixelwidth
;
314 all
.height
= pixelheight
- mbheight
- tbheight
;
316 gtk_widget_size_allocate (x
->edit_widget
, &all
);
317 gdk_window_process_all_updates ();
319 change_frame_size (f
, rows
, columns
, 0, 1, 0);
320 SET_FRAME_GARBAGED (f
);
321 cancel_mouse_face (f
);
326 /* Update our widget size to be COLS/ROWS characters for frame F. */
328 xg_frame_set_char_size (f
, cols
, rows
)
333 int pixelheight
= CHAR_TO_PIXEL_HEIGHT (f
, rows
)
334 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
335 int pixelwidth
= CHAR_TO_PIXEL_WIDTH (f
, cols
);
337 /* Take into account the size of the scroll bar. Always use the
338 number of columns occupied by the scroll bar here otherwise we
339 might end up with a frame width that is not a multiple of the
340 frame's character width which is bad for vertically split
342 f
->output_data
.x
->vertical_scroll_bar_extra
343 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f
)
345 : (FRAME_SCROLL_BAR_COLS (f
)
346 * FONT_WIDTH (f
->output_data
.x
->font
)));
348 x_compute_fringe_widths (f
, 0);
350 /* Must resize our top level widget. Font size may have changed,
351 but not rows/cols. */
352 gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
353 pixelwidth
, pixelheight
);
354 xg_resize_widgets (f
, pixelwidth
, pixelheight
);
357 /* Convert an X Window WSESC to its corresponding GtkWidget.
358 Must be done like this, because GtkWidget:s can have "hidden"
359 X Window that aren't accessible.
361 Return 0 if no widget match WDESC. */
363 xg_win_to_widget (wdesc
)
367 GtkWidget
*gwdesc
= 0;
370 gdkwin
= gdk_xid_table_lookup (wdesc
);
374 event
.any
.window
= gdkwin
;
375 gwdesc
= gtk_get_event_widget (&event
);
382 /* Fill in the GdkColor C so that it represents PIXEL.
383 W is the widget that color will be used for. Used to find colormap. */
385 xg_pix_to_gcolor (w
, pixel
, c
)
390 GdkColormap
*map
= gtk_widget_get_colormap (w
);
391 gdk_colormap_query_color (map
, pixel
, c
);
394 /* Create and set up the GTK widgets for frame F.
395 Return 0 if creation failed, non-zero otherwise. */
397 xg_create_frame_widgets (f
)
410 wtop
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
411 wvbox
= gtk_vbox_new (FALSE
, 0);
412 wfixed
= gtk_fixed_new (); /* Must have this to place scroll bars */
414 if (! wtop
|| ! wvbox
|| ! wfixed
)
416 if (wtop
) gtk_widget_destroy (wtop
);
417 if (wvbox
) gtk_widget_destroy (wvbox
);
418 if (wfixed
) gtk_widget_destroy (wfixed
);
423 /* Use same names as the Xt port does. I.e. Emacs.pane.emacs by default */
424 gtk_widget_set_name (wtop
, EMACS_CLASS
);
425 gtk_widget_set_name (wvbox
, "pane");
426 gtk_widget_set_name (wfixed
, SDATA (Vx_resource_name
));
428 /* If this frame has a title or name, set it in the title bar. */
429 if (! NILP (f
->title
)) title
= SDATA (f
->title
);
430 else if (! NILP (f
->name
)) title
= SDATA (f
->name
);
432 if (title
) gtk_window_set_title (GTK_WINDOW (wtop
), title
);
434 FRAME_GTK_OUTER_WIDGET (f
) = wtop
;
435 FRAME_GTK_WIDGET (f
) = wfixed
;
436 f
->output_data
.x
->vbox_widget
= wvbox
;
438 gtk_fixed_set_has_window (GTK_FIXED (wfixed
), TRUE
);
440 gtk_widget_set_size_request (wfixed
,
444 gtk_container_add (GTK_CONTAINER (wtop
), wvbox
);
445 gtk_box_pack_end (GTK_BOX (wvbox
), wfixed
, TRUE
, TRUE
, 0);
447 if (FRAME_EXTERNAL_TOOL_BAR (f
))
448 update_frame_tool_bar (f
);
450 /* The tool bar is created but first there are no items in it.
451 This causes it to be zero height. Later items are added, but then
452 the frame is already mapped, so there is a "jumping" resize.
453 This makes geometry handling difficult, for example -0-0 will end
454 up in the wrong place as tool bar height has not been taken into account.
455 So we cheat a bit by setting a height that is what it will have
456 later on when tool bar items are added. */
457 if (FRAME_EXTERNAL_TOOL_BAR (f
) && FRAME_TOOLBAR_HEIGHT (f
) == 0)
458 FRAME_TOOLBAR_HEIGHT (f
) = 34;
460 gtk_widget_set_double_buffered (wvbox
, FALSE
);
461 gtk_widget_set_double_buffered (wfixed
, FALSE
);
462 gtk_widget_set_double_buffered (wtop
, FALSE
);
464 /* GTK documents says use gtk_window_set_resizable. But then a user
465 can't shrink the window from its starting size. */
466 gtk_window_set_policy (GTK_WINDOW (wtop
), TRUE
, TRUE
, TRUE
);
467 gtk_window_set_wmclass (GTK_WINDOW (wtop
),
468 SDATA (Vx_resource_name
),
469 SDATA (Vx_resource_class
));
471 /* Add callback to do nothing on WM_DELETE_WINDOW. The default in
472 GTK is to destroy the widget. We want Emacs to do that instead. */
473 g_signal_connect (G_OBJECT (wtop
), "delete-event",
474 G_CALLBACK (gtk_true
), 0);
476 /* Convert our geometry parameters into a geometry string
478 GTK will itself handle calculating the real position this way. */
481 gtk_widget_add_events (wfixed
,
482 GDK_POINTER_MOTION_MASK
484 | GDK_BUTTON_PRESS_MASK
485 | GDK_BUTTON_RELEASE_MASK
487 | GDK_ENTER_NOTIFY_MASK
488 | GDK_LEAVE_NOTIFY_MASK
489 | GDK_FOCUS_CHANGE_MASK
491 | GDK_VISIBILITY_NOTIFY_MASK
);
493 /* Must realize the windows so the X window gets created. It is used
494 by callers of this function. */
495 gtk_widget_realize (wfixed
);
496 FRAME_X_WINDOW (f
) = GTK_WIDGET_TO_X_WIN (wfixed
);
498 /* Since GTK clears its window by filling with the background color,
499 we must keep X and GTK background in sync. */
500 xg_pix_to_gcolor (wfixed
, f
->output_data
.x
->background_pixel
, &bg
);
501 gtk_widget_modify_bg (wfixed
, GTK_STATE_NORMAL
, &bg
);
503 /* Also, do not let any background pixmap to be set, this looks very
504 bad as Emacs overwrites the background pixmap with its own idea
505 of background color. */
506 style
= gtk_widget_get_modifier_style (wfixed
);
508 /* Must use g_strdup because gtk_widget_modify_style does g_free. */
509 style
->bg_pixmap_name
[GTK_STATE_NORMAL
] = g_strdup ("<none>");
510 gtk_widget_modify_style (wfixed
, style
);
512 /* GTK does not set any border, and they look bad with GTK. */
513 f
->output_data
.x
->border_width
= 0;
514 f
->output_data
.x
->internal_border_width
= 0;
521 /* Set the normal size hints for the window manager, for frame F.
522 FLAGS is the flags word to use--or 0 meaning preserve the flags
523 that the window now has.
524 If USER_POSITION is nonzero, we set the User Position
525 flag (this is useful when FLAGS is 0). */
527 x_wm_set_size_hint (f
, flags
, user_position
)
532 if (FRAME_GTK_OUTER_WIDGET (f
))
534 /* Must use GTK routines here, otherwise GTK resets the size hints
535 to its own defaults. */
536 GdkGeometry size_hints
;
538 int base_width
, base_height
;
539 int min_rows
= 0, min_cols
= 0;
540 int win_gravity
= f
->output_data
.x
->win_gravity
;
544 memset (&size_hints
, 0, sizeof (size_hints
));
545 f
->output_data
.x
->size_hints
= size_hints
;
546 f
->output_data
.x
->hint_flags
= hint_flags
;
549 flags
= f
->output_data
.x
->size_hint_flags
;
551 size_hints
= f
->output_data
.x
->size_hints
;
552 hint_flags
= f
->output_data
.x
->hint_flags
;
554 hint_flags
|= GDK_HINT_RESIZE_INC
| GDK_HINT_MIN_SIZE
;
555 size_hints
.width_inc
= FONT_WIDTH (f
->output_data
.x
->font
);
556 size_hints
.height_inc
= f
->output_data
.x
->line_height
;
558 hint_flags
|= GDK_HINT_BASE_SIZE
;
559 base_width
= CHAR_TO_PIXEL_WIDTH (f
, 0);
560 base_height
= CHAR_TO_PIXEL_HEIGHT (f
, 0)
561 + FRAME_MENUBAR_HEIGHT (f
) + FRAME_TOOLBAR_HEIGHT (f
);
563 check_frame_size (f
, &min_rows
, &min_cols
);
565 size_hints
.base_width
= base_width
;
566 size_hints
.base_height
= base_height
;
567 size_hints
.min_width
= base_width
+ min_cols
* size_hints
.width_inc
;
568 size_hints
.min_height
= base_height
+ min_rows
* size_hints
.height_inc
;
571 /* These currently have a one to one mapping with the X values, but I
572 don't think we should rely on that. */
573 hint_flags
|= GDK_HINT_WIN_GRAVITY
;
574 size_hints
.win_gravity
= 0;
575 if (win_gravity
== NorthWestGravity
)
576 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_WEST
;
577 else if (win_gravity
== NorthGravity
)
578 size_hints
.win_gravity
= GDK_GRAVITY_NORTH
;
579 else if (win_gravity
== NorthEastGravity
)
580 size_hints
.win_gravity
= GDK_GRAVITY_NORTH_EAST
;
581 else if (win_gravity
== WestGravity
)
582 size_hints
.win_gravity
= GDK_GRAVITY_WEST
;
583 else if (win_gravity
== CenterGravity
)
584 size_hints
.win_gravity
= GDK_GRAVITY_CENTER
;
585 else if (win_gravity
== EastGravity
)
586 size_hints
.win_gravity
= GDK_GRAVITY_EAST
;
587 else if (win_gravity
== SouthWestGravity
)
588 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_WEST
;
589 else if (win_gravity
== SouthGravity
)
590 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH
;
591 else if (win_gravity
== SouthEastGravity
)
592 size_hints
.win_gravity
= GDK_GRAVITY_SOUTH_EAST
;
593 else if (win_gravity
== StaticGravity
)
594 size_hints
.win_gravity
= GDK_GRAVITY_STATIC
;
596 if (flags
& PPosition
) hint_flags
|= GDK_HINT_POS
;
597 if (flags
& USPosition
) hint_flags
|= GDK_HINT_USER_POS
;
598 if (flags
& USSize
) hint_flags
|= GDK_HINT_USER_SIZE
;
602 hint_flags
&= ~GDK_HINT_POS
;
603 hint_flags
|= GDK_HINT_USER_POS
;
608 gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
609 FRAME_GTK_OUTER_WIDGET (f
),
613 f
->output_data
.x
->size_hints
= size_hints
;
614 f
->output_data
.x
->hint_flags
= hint_flags
;
619 /* Change background color of a frame.
620 Since GTK uses the background colour to clear the window, we must
621 keep the GTK and X colors in sync.
622 F is the frame to change,
623 BG is the pixel value to change to. */
625 xg_set_background_color (f
, bg
)
629 if (FRAME_GTK_WIDGET (f
))
634 xg_pix_to_gcolor (FRAME_GTK_WIDGET (f
), bg
, &gdk_bg
);
635 gtk_widget_modify_bg (FRAME_GTK_WIDGET (f
), GTK_STATE_NORMAL
, &gdk_bg
);
642 /***********************************************************************
644 ***********************************************************************/
645 /* Return the dialog title to use for a dialog of type KEY.
646 This is the encoding used by lwlib. We use the same for GTK. */
648 get_dialog_title (char key
)
658 title
= "Information";
677 /* Callback for dialogs that get WM_DELETE_WINDOW. We pop down
678 the dialog, but return TRUE so the event does not propagate further
679 in GTK. This prevents GTK from destroying the dialog widget automatically
680 and we can always destrou the widget manually, regardles of how
681 it was popped down (button press or WM_DELETE_WINDOW).
682 W is the dialog widget.
683 EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
684 user_data is NULL (not used).
686 Returns TRUE to end propagation of event. */
688 dialog_delete_callback (w
, event
, user_data
)
693 gtk_widget_unmap (w
);
697 /* Create a popup dialog window. See also xg_create_widget below.
698 WV is a widget_value describing the dialog.
699 SELECT_CB is the callback to use when a button has been pressed.
700 DEACTIVATE_CB is the callback to use when the dialog pops down.
702 Returns the GTK dialog widget. */
704 create_dialog (wv
, select_cb
, deactivate_cb
)
707 GCallback deactivate_cb
;
709 char *title
= get_dialog_title (wv
->name
[0]);
710 int total_buttons
= wv
->name
[1] - '0';
711 int right_buttons
= wv
->name
[4] - '0';
714 int button_spacing
= 10;
715 GtkWidget
*wdialog
= gtk_dialog_new ();
720 GtkWidget
*whbox_down
;
722 /* If the number of buttons is greater than 4, make two rows of buttons
723 instead. This looks better. */
724 int make_two_rows
= total_buttons
> 4;
726 if (right_buttons
== 0) right_buttons
= total_buttons
/2;
727 left_buttons
= total_buttons
- right_buttons
;
729 gtk_window_set_title (GTK_WINDOW (wdialog
), title
);
730 gtk_widget_set_name (wdialog
, "emacs-dialog");
732 cur_box
= GTK_BOX (GTK_DIALOG (wdialog
)->action_area
);
736 wvbox
= gtk_vbox_new (TRUE
, button_spacing
);
737 whbox_up
= gtk_hbox_new (FALSE
, 0);
738 whbox_down
= gtk_hbox_new (FALSE
, 0);
740 gtk_box_pack_start (cur_box
, wvbox
, FALSE
, FALSE
, 0);
741 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_up
, FALSE
, FALSE
, 0);
742 gtk_box_pack_start (GTK_BOX (wvbox
), whbox_down
, FALSE
, FALSE
, 0);
744 cur_box
= GTK_BOX (whbox_up
);
747 g_signal_connect (G_OBJECT (wdialog
), "delete-event",
748 G_CALLBACK (dialog_delete_callback
), 0);
752 g_signal_connect (G_OBJECT (wdialog
), "close", deactivate_cb
, 0);
753 g_signal_connect (G_OBJECT (wdialog
), "response", deactivate_cb
, 0);
756 for (item
= wv
->contents
; item
; item
= item
->next
)
758 char *utf8_label
= get_utf8_string (item
->value
);
762 if (strcmp (item
->name
, "message") == 0)
764 /* This is the text part of the dialog. */
765 w
= gtk_label_new (utf8_label
);
766 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
),
769 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
), w
,
771 gtk_misc_set_alignment (GTK_MISC (w
), 0.1, 0.5);
773 /* Try to make dialog look better. Must realize first so
774 the widget can calculate the size it needs. */
775 gtk_widget_realize (w
);
776 gtk_widget_size_request (w
, &req
);
777 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog
)->vbox
),
779 if (strlen (item
->value
) > 0)
780 button_spacing
= 2*req
.width
/strlen (item
->value
);
784 /* This is one button to add to the dialog. */
785 w
= gtk_button_new_with_mnemonic (utf8_label
);
787 gtk_widget_set_sensitive (w
, FALSE
);
789 g_signal_connect (G_OBJECT (w
), "clicked",
790 select_cb
, item
->call_data
);
792 gtk_box_pack_start (cur_box
, w
, TRUE
, TRUE
, button_spacing
);
793 if (++button_nr
== left_buttons
)
796 cur_box
= GTK_BOX (whbox_down
);
798 gtk_box_pack_start (cur_box
,
805 if (utf8_label
&& utf8_label
!= item
->value
)
821 /* Callback function invoked when the Ok button is pressed in
823 W is the file dialog widget,
824 ARG points to an integer where we record what has happend. */
826 xg_file_sel_ok (w
, arg
)
830 *(int*)arg
= XG_FILE_OK
;
833 /* Callback function invoked when the Cancel button is pressed in
835 W is the file dialog widget,
836 ARG points to an integer where we record what has happend. */
838 xg_file_sel_cancel (w
, arg
)
842 *(int*)arg
= XG_FILE_CANCEL
;
845 /* Callback function invoked when the file dialog is destroyed (i.e.
846 popped down). We must keep track of this, because if this
847 happens, GTK destroys the widget. But if for example, Ok is pressed,
848 the dialog is popped down, but the dialog widget is not destroyed.
849 W is the file dialog widget,
850 ARG points to an integer where we record what has happend. */
852 xg_file_sel_destroy (w
, arg
)
856 *(int*)arg
= XG_FILE_DESTROYED
;
859 /* Read a file name from the user using a file dialog.
860 F is the current frame.
861 PROMPT is a prompt to show to the user. May not be NULL.
862 DEFAULT_FILENAME is a default selection to be displayed. May be NULL.
863 If MUSTMATCH_P is non-zero, the returned file name must be an existing
866 Returns a file name or NULL if no file was selected.
867 The returned string must be freed by the caller. */
869 xg_get_file_name (f
, prompt
, default_filename
, mustmatch_p
)
872 char *default_filename
;
876 GtkFileSelection
*filesel
;
877 int filesel_done
= XG_FILE_NOT_DONE
;
880 filewin
= gtk_file_selection_new (prompt
);
881 filesel
= GTK_FILE_SELECTION (filewin
);
883 gtk_widget_set_name (filewin
, "emacs-filedialog");
885 gtk_window_set_transient_for (GTK_WINDOW (filewin
),
886 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
887 gtk_window_set_destroy_with_parent (GTK_WINDOW (filewin
), TRUE
);
889 g_signal_connect (G_OBJECT (filesel
->ok_button
),
891 G_CALLBACK (xg_file_sel_ok
),
893 g_signal_connect (G_OBJECT (filesel
->cancel_button
),
895 G_CALLBACK (xg_file_sel_cancel
),
897 g_signal_connect (G_OBJECT (filesel
),
899 G_CALLBACK (xg_file_sel_destroy
),
902 if (default_filename
)
903 gtk_file_selection_set_filename (filesel
, default_filename
);
907 /* The selection_entry part of filesel is not documented. */
908 gtk_widget_set_sensitive (filesel
->selection_entry
, FALSE
);
909 gtk_file_selection_hide_fileop_buttons (filesel
);
913 gtk_widget_show_all (filewin
);
915 while (filesel_done
== XG_FILE_NOT_DONE
)
916 gtk_main_iteration ();
918 if (filesel_done
== XG_FILE_OK
)
919 fn
= xstrdup ((char*) gtk_file_selection_get_filename (filesel
));
921 if (filesel_done
!= XG_FILE_DESTROYED
)
922 gtk_widget_destroy (filewin
);
928 /***********************************************************************
930 ***********************************************************************/
932 /* The name of menu items that can be used for citomization. Since GTK
933 RC files are very crude and primitive, we have to set this on all
934 menu item names so a user can easily cutomize menu items. */
936 #define MENU_ITEM_NAME "emacs-menuitem"
939 /* Linked list of all allocated struct xg_menu_cb_data. Used for marking
940 during GC. The next member points to the items. */
941 static xg_list_node xg_menu_cb_list
;
943 /* Linked list of all allocated struct xg_menu_item_cb_data. Used for marking
944 during GC. The next member points to the items. */
945 static xg_list_node xg_menu_item_cb_list
;
947 /* Allocate and initialize CL_DATA if NULL, otherwise increase ref_count.
948 F is the frame CL_DATA will be initialized for.
949 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
951 The menu bar and all sub menus under the menu bar in a frame
952 share the same structure, hence the reference count.
954 Returns CL_DATA if CL_DATA is not NULL, or a pointer to a newly
955 allocated xg_menu_cb_data if CL_DATA is NULL. */
956 static xg_menu_cb_data
*
957 make_cl_data (cl_data
, f
, highlight_cb
)
958 xg_menu_cb_data
*cl_data
;
960 GCallback highlight_cb
;
964 cl_data
= (xg_menu_cb_data
*) xmalloc (sizeof (*cl_data
));
966 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
967 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
968 cl_data
->highlight_cb
= highlight_cb
;
969 cl_data
->ref_count
= 0;
971 xg_list_insert (&xg_menu_cb_list
, &cl_data
->ptrs
);
974 cl_data
->ref_count
++;
979 /* Update CL_DATA with values from frame F and with HIGHLIGHT_CB.
980 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
982 When the menu bar is updated, menu items may have been added and/or
983 removed, so menu_bar_vector and menu_bar_items_used change. We must
984 then update CL_DATA since it is used to determine which menu
985 item that is invoked in the menu.
986 HIGHLIGHT_CB could change, there is no check that the same
987 function is given when modifying a menu bar as was given when
988 creating the menu bar. */
990 update_cl_data (cl_data
, f
, highlight_cb
)
991 xg_menu_cb_data
*cl_data
;
993 GCallback highlight_cb
;
998 cl_data
->menu_bar_vector
= f
->menu_bar_vector
;
999 cl_data
->menu_bar_items_used
= f
->menu_bar_items_used
;
1000 cl_data
->highlight_cb
= highlight_cb
;
1004 /* Decrease reference count for CL_DATA.
1005 If reference count is zero, free CL_DATA. */
1007 unref_cl_data (cl_data
)
1008 xg_menu_cb_data
*cl_data
;
1010 if (cl_data
&& cl_data
->ref_count
> 0)
1012 cl_data
->ref_count
--;
1013 if (cl_data
->ref_count
== 0)
1015 xg_list_remove (&xg_menu_cb_list
, &cl_data
->ptrs
);
1021 /* Function that marks all lisp data during GC. */
1027 for (iter
= xg_menu_cb_list
.next
; iter
; iter
= iter
->next
)
1028 mark_object (&((xg_menu_cb_data
*) iter
)->menu_bar_vector
);
1030 for (iter
= xg_menu_item_cb_list
.next
; iter
; iter
= iter
->next
)
1032 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) iter
;
1034 if (! NILP (cb_data
->help
))
1035 mark_object (&cb_data
->help
);
1040 /* Callback called when a menu item is destroyed. Used to free data.
1041 W is the widget that is being destroyed (not used).
1042 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W. */
1044 menuitem_destroy_callback (w
, client_data
)
1046 gpointer client_data
;
1050 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
1051 xg_list_remove (&xg_menu_item_cb_list
, &data
->ptrs
);
1056 /* Callback called when the pointer enters/leaves a menu item.
1058 EVENT is either an enter event or leave event.
1059 CLIENT_DATA points to the xg_menu_item_cb_data associated with the W.
1061 Returns FALSE to tell GTK to keep processing this event. */
1063 menuitem_highlight_callback (w
, event
, client_data
)
1065 GdkEventCrossing
*event
;
1066 gpointer client_data
;
1070 xg_menu_item_cb_data
*data
= (xg_menu_item_cb_data
*) client_data
;
1071 gpointer call_data
= event
->type
== GDK_LEAVE_NOTIFY
? 0 : client_data
;
1073 if (! NILP (data
->help
) && data
->cl_data
->highlight_cb
)
1075 GtkCallback func
= (GtkCallback
) data
->cl_data
->highlight_cb
;
1076 (*func
) (w
, call_data
);
1083 /* Callback called when a menu is destroyed. Used to free data.
1084 W is the widget that is being destroyed (not used).
1085 CLIENT_DATA points to the xg_menu_cb_data associated with W. */
1087 menu_destroy_callback (w
, client_data
)
1089 gpointer client_data
;
1091 unref_cl_data ((xg_menu_cb_data
*) client_data
);
1094 /* Callback called when a menu does a grab or ungrab. That means the
1095 menu has been activated or deactivated.
1096 Used to start a timer so the small timeout the menus in GTK uses before
1097 popping down a menu is seen by Emacs (see xg_process_timeouts above).
1098 W is the widget that does the grab (not used).
1099 UNGRAB_P is TRUE if this is an ungrab, FALSE if it is a grab.
1100 CLIENT_DATA is NULL (not used). */
1102 menu_grab_callback (GtkWidget
*widget
,
1104 gpointer client_data
)
1106 /* Keep track of total number of grabs. */
1109 if (ungrab_p
) cnt
--;
1112 if (cnt
> 0 && ! xg_timer
) xg_start_timer ();
1113 else if (cnt
== 0 && xg_timer
) xg_stop_timer ();
1116 /* Make a GTK widget that contains both UTF8_LABEL and UTF8_KEY (both
1117 must be non-NULL) and can be inserted into a menu item.
1119 Returns the GtkHBox. */
1121 make_widget_for_menu_item (utf8_label
, utf8_key
)
1129 wbox
= gtk_hbox_new (FALSE
, 0);
1130 wlbl
= gtk_label_new_with_mnemonic (utf8_label
);
1131 wkey
= gtk_label_new (utf8_key
);
1133 gtk_misc_set_alignment (GTK_MISC (wlbl
), 0.0, 0.5);
1134 gtk_misc_set_alignment (GTK_MISC (wkey
), 0.0, 0.5);
1136 gtk_box_pack_start (GTK_BOX (wbox
), wlbl
, TRUE
, TRUE
, 0);
1137 gtk_box_pack_start (GTK_BOX (wbox
), wkey
, FALSE
, FALSE
, 0);
1139 gtk_widget_set_name (wlbl
, MENU_ITEM_NAME
);
1140 gtk_widget_set_name (wkey
, MENU_ITEM_NAME
);
1145 /* Make and return a menu item widget with the key to the right.
1146 UTF8_LABEL is the text for the menu item (GTK uses UTF8 internally).
1147 UTF8_KEY is the text representing the key binding.
1148 ITEM is the widget_value describing the menu item.
1150 GROUP is an in/out parameter. If the menu item to be created is not
1151 part of any radio menu group, *GROUP contains NULL on entry and exit.
1152 If the menu item to be created is part of a radio menu group, on entry
1153 *GROUP contains the group to use, or NULL if this is the first item
1154 in the group. On exit, *GROUP contains the radio item group.
1156 Unfortunately, keys don't line up as nicely as in Motif,
1157 but the MacOS X version doesn't either, so I guess that is OK. */
1159 make_menu_item (utf8_label
, utf8_key
, item
, group
)
1166 GtkWidget
*wtoadd
= 0;
1169 wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
1171 if (item
->button_type
== BUTTON_TYPE_TOGGLE
)
1174 if (utf8_key
) w
= gtk_check_menu_item_new ();
1175 else w
= gtk_check_menu_item_new_with_mnemonic (utf8_label
);
1176 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), item
->selected
);
1178 else if (item
->button_type
== BUTTON_TYPE_RADIO
)
1180 if (utf8_key
) w
= gtk_radio_menu_item_new (*group
);
1181 else w
= gtk_radio_menu_item_new_with_mnemonic (*group
, utf8_label
);
1182 *group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w
));
1184 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), TRUE
);
1189 if (utf8_key
) w
= gtk_menu_item_new ();
1190 else w
= gtk_menu_item_new_with_mnemonic (utf8_label
);
1193 if (wtoadd
) gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
1194 if (! item
->enabled
) gtk_widget_set_sensitive (w
, FALSE
);
1199 /* Return non-zero if NAME specifies a separator (GTK only has one
1202 xg_separator_p (char *name
)
1204 return strcmp (name
, "--") == 0
1205 || strcmp (name
, "--:") == 0
1206 || strcmp (name
, "---") == 0;
1209 GtkWidget
*xg_did_tearoff
;
1211 /* Callback invoked when a detached menu window is removed. Here we
1212 delete the popup menu.
1213 WIDGET is the top level window that is removed (the parent of the menu).
1214 EVENT is the event that triggers the window removal.
1215 CLIENT_DATA points to the menu that is detached.
1217 Returns TRUE to tell GTK to stop processing this event. */
1219 tearoff_remove (widget
, event
, client_data
)
1222 gpointer client_data
;
1224 gtk_widget_destroy (GTK_WIDGET (client_data
));
1228 /* Callback invoked when a menu is detached. It sets the xg_did_tearoff
1230 WIDGET is the GtkTearoffMenuItem.
1231 CLIENT_DATA is not used. */
1233 tearoff_activate (widget
, client_data
)
1235 gpointer client_data
;
1237 GtkWidget
*menu
= gtk_widget_get_parent (widget
);
1238 if (! gtk_menu_get_tearoff_state (GTK_MENU (menu
)))
1241 xg_did_tearoff
= menu
;
1244 /* If a detach of a popup menu is done, this function should be called
1245 to keep the menu around until the detached window is removed.
1246 MENU is the top level menu for the popup,
1247 SUBMENU is the menu that got detached (that is MENU or a
1248 submenu of MENU), see the xg_did_tearoff variable. */
1250 xg_keep_popup (menu
, submenu
)
1256 /* Find the top widget for the detached menu. */
1257 p
= gtk_widget_get_toplevel (submenu
);
1259 /* Delay destroying the menu until the detached menu is removed. */
1260 g_signal_connect (G_OBJECT (p
), "unmap_event",
1261 G_CALLBACK (tearoff_remove
), menu
);
1266 /* Create a menu item widget, and connect the callbacks.
1267 ITEM decribes the menu item.
1268 F is the frame the created menu belongs to.
1269 SELECT_CB is the callback to use when a menu item is selected.
1270 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1271 CL_DATA points to the callback data to be used for this menu.
1272 GROUP is an in/out parameter. If the menu item to be created is not
1273 part of any radio menu group, *GROUP contains NULL on entry and exit.
1274 If the menu item to be created is part of a radio menu group, on entry
1275 *GROUP contains the group to use, or NULL if this is the first item
1276 in the group. On exit, *GROUP contains the radio item group.
1278 Returns the created GtkWidget. */
1280 xg_create_one_menuitem (item
, f
, select_cb
, highlight_cb
, cl_data
, group
)
1283 GCallback select_cb
;
1284 GCallback highlight_cb
;
1285 xg_menu_cb_data
*cl_data
;
1291 xg_menu_item_cb_data
*cb_data
;
1293 utf8_label
= get_utf8_string (item
->name
);
1294 utf8_key
= get_utf8_string (item
->key
);
1296 w
= make_menu_item (utf8_label
, utf8_key
, item
, group
);
1298 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
1299 if (utf8_key
&& utf8_key
!= item
->key
) g_free (utf8_key
);
1301 cb_data
= xmalloc (sizeof (xg_menu_item_cb_data
));
1303 xg_list_insert (&xg_menu_item_cb_list
, &cb_data
->ptrs
);
1305 cb_data
->unhighlight_id
= cb_data
->highlight_id
= cb_data
->select_id
= 0;
1306 cb_data
->help
= item
->help
;
1307 cb_data
->cl_data
= cl_data
;
1308 cb_data
->call_data
= item
->call_data
;
1310 g_signal_connect (G_OBJECT (w
),
1312 G_CALLBACK (menuitem_destroy_callback
),
1315 /* Put cb_data in widget, so we can get at it when modifying menubar */
1316 g_object_set_data (G_OBJECT (w
), XG_ITEM_DATA
, cb_data
);
1318 /* final item, not a submenu */
1319 if (item
->call_data
&& ! item
->contents
)
1323 = g_signal_connect (G_OBJECT (w
), "activate", select_cb
, cb_data
);
1326 if (! NILP (item
->help
) && highlight_cb
)
1328 /* We use enter/leave notify instead of select/deselect because
1329 select/deselect doesn't go well with detached menus. */
1330 cb_data
->highlight_id
1331 = g_signal_connect (G_OBJECT (w
),
1332 "enter-notify-event",
1333 G_CALLBACK (menuitem_highlight_callback
),
1335 cb_data
->unhighlight_id
1336 = g_signal_connect (G_OBJECT (w
),
1337 "leave-notify-event",
1338 G_CALLBACK (menuitem_highlight_callback
),
1345 /* Create a full menu tree specified by DATA.
1346 F is the frame the created menu belongs to.
1347 SELECT_CB is the callback to use when a menu item is selected.
1348 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1349 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1350 POP_UP_P is non-zero if we shall create a popup menu.
1351 MENU_BAR_P is non-zero if we shall create a menu bar.
1352 ADD_TEAROFF_P is non-zero if we shall add a teroff menu item. Ignored
1353 if MENU_BAR_P is non-zero.
1354 TOPMENU is the topmost GtkWidget that others shall be placed under.
1355 It may be NULL, in that case we create the appropriate widget
1356 (menu bar or menu item depending on POP_UP_P and MENU_BAR_P)
1357 CL_DATA is the callback data we shall use for this menu, or NULL
1358 if we haven't set the first callback yet.
1359 NAME is the name to give to the top level menu if this function
1360 creates it. May be NULL to not set any name.
1362 Returns the top level GtkWidget. This is TOPLEVEL if TOPLEVEL is
1365 This function calls itself to create submenus. */
1368 create_menus (data
, f
, select_cb
, deactivate_cb
, highlight_cb
,
1369 pop_up_p
, menu_bar_p
, add_tearoff_p
, topmenu
, cl_data
, name
)
1372 GCallback select_cb
;
1373 GCallback deactivate_cb
;
1374 GCallback highlight_cb
;
1379 xg_menu_cb_data
*cl_data
;
1383 GtkWidget
*wmenu
= topmenu
;
1384 GSList
*group
= NULL
;
1388 if (! menu_bar_p
) wmenu
= gtk_menu_new ();
1389 else wmenu
= gtk_menu_bar_new ();
1391 /* Put cl_data on the top menu for easier access. */
1392 cl_data
= make_cl_data (cl_data
, f
, highlight_cb
);
1393 g_object_set_data (G_OBJECT (wmenu
), XG_FRAME_DATA
, (gpointer
)cl_data
);
1394 g_signal_connect (G_OBJECT (wmenu
), "destroy",
1395 G_CALLBACK (menu_destroy_callback
), cl_data
);
1398 gtk_widget_set_name (wmenu
, name
);
1401 g_signal_connect (G_OBJECT (wmenu
),
1402 "deactivate", deactivate_cb
, 0);
1404 g_signal_connect (G_OBJECT (wmenu
),
1405 "grab-notify", G_CALLBACK (menu_grab_callback
), 0);
1408 if (! menu_bar_p
&& add_tearoff_p
)
1410 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new ();
1411 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), tearoff
);
1413 g_signal_connect (G_OBJECT (tearoff
), "activate",
1414 G_CALLBACK (tearoff_activate
), 0);
1417 for (item
= data
; item
; item
= item
->next
)
1421 if (pop_up_p
&& !item
->contents
&& !item
->call_data
1422 && !xg_separator_p (item
->name
))
1425 /* A title for a popup. We do the same as GTK does when
1426 creating titles, but it does not look good. */
1428 utf8_label
= get_utf8_string (item
->name
);
1430 gtk_menu_set_title (GTK_MENU (wmenu
), utf8_label
);
1431 w
= gtk_menu_item_new_with_mnemonic (utf8_label
);
1432 gtk_widget_set_sensitive (w
, FALSE
);
1433 if (utf8_label
&& utf8_label
!= item
->name
) g_free (utf8_label
);
1435 else if (xg_separator_p (item
->name
))
1438 /* GTK only have one separator type. */
1439 w
= gtk_separator_menu_item_new ();
1443 w
= xg_create_one_menuitem (item
,
1445 item
->contents
? 0 : select_cb
,
1452 GtkWidget
*submenu
= create_menus (item
->contents
,
1463 gtk_menu_item_set_submenu (GTK_MENU_ITEM (w
), submenu
);
1466 /* Assume "Help" is the last menu in the menubar. */
1467 if (menu_bar_p
&& ! item
->next
)
1468 gtk_menu_item_set_right_justified (GTK_MENU_ITEM (w
), TRUE
);
1471 gtk_menu_shell_append (GTK_MENU_SHELL (wmenu
), w
);
1472 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
1478 /* Create a menubar, popup menu or dialog, depending on the TYPE argument.
1479 TYPE can be "menubar", "popup" for popup menu, or "dialog" for a dialog
1480 with some text and buttons.
1481 F is the frame the created item belongs to.
1482 NAME is the name to use for the top widget.
1483 VAL is a widget_value structure describing items to be created.
1484 SELECT_CB is the callback to use when a menu item is selected or
1485 a dialog button is pressed.
1486 DEACTIVATE_CB is the callback to use when an item is deactivated.
1487 For a menu, when a sub menu is not shown anymore, for a dialog it is
1488 called when the dialog is popped down.
1489 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1491 Returns the widget created. */
1493 xg_create_widget (type
, name
, f
, val
,
1494 select_cb
, deactivate_cb
, highlight_cb
)
1499 GCallback select_cb
;
1500 GCallback deactivate_cb
;
1501 GCallback highlight_cb
;
1504 if (strcmp (type
, "dialog") == 0)
1506 w
= create_dialog (val
, select_cb
, deactivate_cb
);
1507 gtk_window_set_transient_for (GTK_WINDOW (w
),
1508 GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)));
1509 gtk_window_set_destroy_with_parent (GTK_WINDOW (w
), TRUE
);
1512 gtk_widget_set_name (w
, "emacs-dialog");
1514 else if (strcmp (type
, "menubar") == 0 || strcmp (type
, "popup") == 0)
1516 w
= create_menus (val
->contents
,
1521 strcmp (type
, "popup") == 0,
1522 strcmp (type
, "menubar") == 0,
1528 /* Set the cursor to an arrow for popup menus when they are mapped.
1529 This is done by default for menu bar menus. */
1530 if (strcmp (type
, "popup") == 0)
1532 /* Must realize so the GdkWindow inside the widget is created. */
1533 gtk_widget_realize (w
);
1534 xg_set_cursor (w
, &xg_left_ptr_cursor
);
1539 fprintf (stderr
, "bad type in xg_create_widget: %s, doing nothing\n",
1547 xg_get_menu_item_label (witem
)
1550 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
1551 return gtk_label_get_label (wlabel
);
1555 xg_item_label_same_p (witem
, label
)
1560 char *utf8_label
= get_utf8_string (label
);
1562 is_same
= strcmp (utf8_label
, xg_get_menu_item_label (witem
)) == 0;
1563 if (utf8_label
!= label
) g_free (utf8_label
);
1568 /* Remove widgets in LIST from container WCONT. */
1570 remove_from_container (wcont
, list
)
1574 /* We must copy list because gtk_container_remove changes it. */
1575 GList
*clist
= g_list_copy (list
);
1578 for (iter
= clist
; iter
; iter
= g_list_next (iter
))
1580 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
1582 /* Add a ref to w so we can explicitly destroy it later. */
1584 gtk_container_remove (GTK_CONTAINER (wcont
), w
);
1586 /* If there is a menu under this widget that has been detached,
1587 there is a reference to it, and just removing w from the
1588 container does not destroy the submenu. By explicitly
1589 destroying w we make sure the submenu is destroyed, thus
1590 removing the detached window also if there was one. */
1591 gtk_widget_destroy (w
);
1593 g_list_free (clist
);
1596 /* Update the top level names in MENUBAR (i.e. not submenus).
1597 F is the frame the menu bar belongs to.
1598 LIST is a list with the current menu bar names (menu item widgets).
1599 VAL describes what the menu bar shall look like after the update.
1600 SELECT_CB is the callback to use when a menu item is selected.
1601 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1603 This function calls itself to walk through the menu bar names. */
1605 xg_update_menubar (menubar
, f
, list
, val
, select_cb
, highlight_cb
, cl_data
)
1610 GCallback select_cb
;
1611 GCallback highlight_cb
;
1612 xg_menu_cb_data
*cl_data
;
1614 if (! list
&& ! val
)
1616 else if (list
&& ! val
)
1618 /* Item(s) have been removed. Remove all remaining items from list. */
1619 remove_from_container (menubar
, list
);
1625 else if (! list
&& val
)
1627 /* Item(s) added. Add all new items in one call. */
1628 create_menus (val
, f
, select_cb
, 0, highlight_cb
,
1629 0, 1, 0, menubar
, cl_data
, 0);
1635 /* Below this neither list or val is NULL */
1636 else if (xg_item_label_same_p (GTK_MENU_ITEM (list
->data
), val
->name
))
1638 /* This item is still the same, check next item. */
1640 list
= g_list_next (list
);
1642 else /* This item is changed. */
1644 GtkMenuItem
*witem
= GTK_MENU_ITEM (list
->data
);
1645 GtkMenuItem
*witem2
= 0;
1647 int val_in_menubar
= 0;
1648 int list_in_new_menubar
= 0;
1654 /* Get position number for witem. */
1655 list2
= gtk_container_get_children (GTK_CONTAINER (menubar
));
1656 for (iter
= list2
; iter
; iter
= g_list_next (iter
))
1658 if (list
->data
== iter
->data
) break;
1662 /* See if the changed entry (val) is present later in the menu bar */
1663 for (iter
= g_list_next (list
);
1664 iter
&& ! val_in_menubar
;
1665 iter
= g_list_next (iter
))
1667 witem2
= GTK_MENU_ITEM (iter
->data
);
1668 val_in_menubar
= xg_item_label_same_p (witem2
, val
->name
);
1671 /* See if the current entry (list) is present later in the
1672 specification for the new menu bar. */
1673 for (cur
= val
; cur
&& ! list_in_new_menubar
; cur
= cur
->next
)
1674 list_in_new_menubar
= xg_item_label_same_p (witem
, cur
->name
);
1676 if (val_in_menubar
&& ! list_in_new_menubar
)
1678 /* This corresponds to:
1683 gtk_widget_ref (GTK_WIDGET (witem
));
1684 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem
));
1685 gtk_widget_destroy (GTK_WIDGET (witem
));
1687 /* Must get new list since the old changed. */
1688 list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
1689 while (pos
-- > 0) list
= g_list_next (list
);
1691 else if (! val_in_menubar
&& ! list_in_new_menubar
)
1693 /* This corresponds to:
1696 Rename B to X. This might seem to be a strange thing to do,
1697 since if there is a menu under B it will be totally wrong for X.
1698 But consider editing a C file. Then there is a C-mode menu
1699 (corresponds to B above).
1700 If then doing C-x C-f the minibuf menu (X above) replaces the
1701 C-mode menu. When returning from the minibuffer, we get
1702 back the C-mode menu. Thus we do:
1703 Rename B to X (C-mode to minibuf menu)
1704 Rename X to B (minibuf to C-mode menu).
1705 If the X menu hasn't been invoked, the menu under B
1706 is up to date when leaving the minibuffer. */
1707 GtkLabel
*wlabel
= GTK_LABEL (gtk_bin_get_child (GTK_BIN (witem
)));
1708 char *utf8_label
= get_utf8_string (val
->name
);
1710 gtk_label_set_text_with_mnemonic (wlabel
, utf8_label
);
1712 list
= g_list_next (list
);
1715 else if (! val_in_menubar
&& list_in_new_menubar
)
1717 /* This corresponds to:
1723 GtkWidget
*w
= xg_create_one_menuitem (val
,
1730 gtk_widget_set_name (w
, MENU_ITEM_NAME
);
1731 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
), w
, pos
);
1733 list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
1734 while (pos
-- > 0) list
= g_list_next (list
);
1735 list
= g_list_next (list
);
1738 else /* if (val_in_menubar && list_in_new_menubar) */
1740 /* This corresponds to:
1745 gtk_widget_ref (GTK_WIDGET (witem2
));
1746 gtk_container_remove (GTK_CONTAINER (menubar
), GTK_WIDGET (witem2
));
1747 gtk_menu_shell_insert (GTK_MENU_SHELL (menubar
),
1748 GTK_WIDGET (witem2
), pos
);
1749 gtk_widget_unref (GTK_WIDGET (witem2
));
1752 list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
1753 while (pos
-- > 0) list
= g_list_next (list
);
1754 list
= g_list_next (list
);
1759 /* Update the rest of the menu bar. */
1760 xg_update_menubar (menubar
, f
, list
, val
, select_cb
, highlight_cb
, cl_data
);
1763 /* Update the menu item W so it corresponds to VAL.
1764 SELECT_CB is the callback to use when a menu item is selected.
1765 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1766 CL_DATA is the data to set in the widget for menu invokation. */
1768 xg_update_menu_item (val
, w
, select_cb
, highlight_cb
, cl_data
)
1771 GCallback select_cb
;
1772 GCallback highlight_cb
;
1773 xg_menu_cb_data
*cl_data
;
1780 xg_menu_item_cb_data
*cb_data
;
1782 wchild
= gtk_bin_get_child (GTK_BIN (w
));
1783 utf8_label
= get_utf8_string (val
->name
);
1784 utf8_key
= get_utf8_string (val
->key
);
1786 /* See if W is a menu item with a key. See make_menu_item above. */
1787 if (GTK_IS_HBOX (wchild
))
1789 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wchild
));
1791 wlbl
= GTK_LABEL (list
->data
);
1792 wkey
= GTK_LABEL (list
->next
->data
);
1795 /* Remove the key and keep just the label. */
1796 gtk_widget_ref (GTK_WIDGET (wlbl
));
1797 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
1798 gtk_container_add (GTK_CONTAINER (w
), GTK_WIDGET (wlbl
));
1802 else /* Just a label. */
1804 wlbl
= GTK_LABEL (wchild
);
1806 /* Check if there is now a key. */
1809 GtkWidget
*wtoadd
= make_widget_for_menu_item (utf8_label
, utf8_key
);
1810 GList
*list
= gtk_container_get_children (GTK_CONTAINER (wtoadd
));
1811 wlbl
= GTK_LABEL (list
->data
);
1812 wkey
= GTK_LABEL (list
->next
->data
);
1814 gtk_container_remove (GTK_CONTAINER (w
), wchild
);
1815 gtk_container_add (GTK_CONTAINER (w
), wtoadd
);
1819 if (utf8_key
&& strcmp (utf8_key
, gtk_label_get_label (wkey
)) != 0)
1820 gtk_label_set_text (wkey
, utf8_key
);
1822 if (strcmp (utf8_label
, gtk_label_get_label (wlbl
)) != 0)
1823 gtk_label_set_text_with_mnemonic (wlbl
, utf8_label
);
1825 if (utf8_key
!= val
->key
) g_free (utf8_key
);
1826 if (utf8_label
!= val
->name
) g_free (utf8_label
);
1828 if (! val
->enabled
&& GTK_WIDGET_SENSITIVE (w
))
1829 gtk_widget_set_sensitive (w
, FALSE
);
1830 else if (val
->enabled
&& ! GTK_WIDGET_SENSITIVE (w
))
1831 gtk_widget_set_sensitive (w
, TRUE
);
1833 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (w
),
1837 cb_data
->call_data
= val
->call_data
;
1838 cb_data
->help
= val
->help
;
1839 cb_data
->cl_data
= cl_data
;
1841 /* We assume the callback functions don't change. */
1842 if (val
->call_data
&& ! val
->contents
)
1844 /* This item shall have a select callback. */
1845 if (! cb_data
->select_id
)
1847 = g_signal_connect (G_OBJECT (w
), "activate",
1848 select_cb
, cb_data
);
1850 else if (cb_data
->select_id
)
1852 g_signal_handler_disconnect (w
, cb_data
->select_id
);
1853 cb_data
->select_id
= 0;
1856 if (NILP (cb_data
->help
))
1858 /* Shall not have help. Remove if any existed previously. */
1859 if (cb_data
->highlight_id
)
1861 g_signal_handler_disconnect (G_OBJECT (w
),
1862 cb_data
->highlight_id
);
1863 cb_data
->highlight_id
= 0;
1865 if (cb_data
->unhighlight_id
)
1867 g_signal_handler_disconnect (G_OBJECT (w
),
1868 cb_data
->unhighlight_id
);
1869 cb_data
->unhighlight_id
= 0;
1872 else if (! cb_data
->highlight_id
&& highlight_cb
)
1874 /* Have help now, but didn't previously. Add callback. */
1875 cb_data
->highlight_id
1876 = g_signal_connect (G_OBJECT (w
),
1877 "enter-notify-event",
1878 G_CALLBACK (menuitem_highlight_callback
),
1880 cb_data
->unhighlight_id
1881 = g_signal_connect (G_OBJECT (w
),
1882 "leave-notify-event",
1883 G_CALLBACK (menuitem_highlight_callback
),
1889 /* Update the toggle menu item W so it corresponds to VAL. */
1891 xg_update_toggle_item (val
, w
)
1895 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
1898 /* Update the radio menu item W so it corresponds to VAL. */
1900 xg_update_radio_item (val
, w
)
1904 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w
), val
->selected
);
1907 /* Update the sub menu SUBMENU and all its children so it corresponds to VAL.
1908 SUBMENU may be NULL, in that case a new menu is created.
1909 F is the frame the menu bar belongs to.
1910 VAL describes the contents of the menu bar.
1911 SELECT_CB is the callback to use when a menu item is selected.
1912 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
1913 HIGHLIGHT_CB is the callback to call when entering/leaving menu items.
1914 CL_DATA is the call back data to use for any newly created items.
1916 Returns the updated submenu widget, that is SUBMENU unless SUBMENU
1920 xg_update_submenu (submenu
, f
, val
,
1921 select_cb
, deactivate_cb
, highlight_cb
, cl_data
)
1925 GCallback select_cb
;
1926 GCallback deactivate_cb
;
1927 GCallback highlight_cb
;
1928 xg_menu_cb_data
*cl_data
;
1930 GtkWidget
*newsub
= submenu
;
1934 int has_tearoff_p
= 0;
1935 GList
*first_radio
= 0;
1938 list
= gtk_container_get_children (GTK_CONTAINER (submenu
));
1940 for (cur
= val
, iter
= list
;
1942 iter
= g_list_next (iter
), cur
= cur
->next
)
1944 GtkWidget
*w
= GTK_WIDGET (iter
->data
);
1946 /* Skip tearoff items, they have no counterpart in val. */
1947 if (GTK_IS_TEAROFF_MENU_ITEM (w
))
1950 iter
= g_list_next (iter
);
1951 if (iter
) w
= GTK_WIDGET (iter
->data
);
1955 /* Remember first radio button in a group. If we get a mismatch in
1956 a radio group we must rebuild the whole group so that the connections
1957 in GTK becomes correct. */
1958 if (cur
->button_type
== BUTTON_TYPE_RADIO
&& ! first_radio
)
1960 else if (cur
->button_type
!= BUTTON_TYPE_RADIO
1961 && ! GTK_IS_RADIO_MENU_ITEM (w
))
1964 if (GTK_IS_SEPARATOR_MENU_ITEM (w
))
1966 if (! xg_separator_p (cur
->name
))
1969 else if (GTK_IS_CHECK_MENU_ITEM (w
))
1971 if (cur
->button_type
!= BUTTON_TYPE_TOGGLE
)
1973 xg_update_toggle_item (cur
, w
);
1974 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
1976 else if (GTK_IS_RADIO_MENU_ITEM (w
))
1978 if (cur
->button_type
!= BUTTON_TYPE_RADIO
)
1980 xg_update_radio_item (cur
, w
);
1981 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
1983 else if (GTK_IS_MENU_ITEM (w
))
1985 GtkMenuItem
*witem
= GTK_MENU_ITEM (w
);
1988 if (cur
->button_type
!= BUTTON_TYPE_NONE
||
1989 xg_separator_p (cur
->name
))
1992 xg_update_menu_item (cur
, w
, select_cb
, highlight_cb
, cl_data
);
1994 sub
= gtk_menu_item_get_submenu (witem
);
1995 if (sub
&& ! cur
->contents
)
1997 /* Not a submenu anymore. */
1998 gtk_widget_ref (sub
);
1999 gtk_menu_item_remove_submenu (witem
);
2000 gtk_widget_destroy (sub
);
2002 else if (cur
->contents
)
2006 nsub
= xg_update_submenu (sub
, f
, cur
->contents
,
2007 select_cb
, deactivate_cb
,
2008 highlight_cb
, cl_data
);
2010 /* If this item just became a submenu, we must set it. */
2012 gtk_menu_item_set_submenu (witem
, nsub
);
2017 /* Structural difference. Remove everything from here and down
2023 /* Remove widgets from first structual change. */
2026 /* If we are adding new menu items below, we must remove from
2027 first radio button so that radio groups become correct. */
2028 if (cur
&& first_radio
) remove_from_container (submenu
, first_radio
);
2029 else remove_from_container (submenu
, iter
);
2034 /* More items added. Create them. */
2035 newsub
= create_menus (cur
,
2051 /* Update the MENUBAR.
2052 F is the frame the menu bar belongs to.
2053 VAL describes the contents of the menu bar.
2054 If DEEP_P is non-zero, rebuild all but the top level menu names in
2055 the MENUBAR. If DEEP_P is zero, just rebuild the names in the menubar.
2056 SELECT_CB is the callback to use when a menu item is selected.
2057 DEACTIVATE_CB is the callback to use when a sub menu is not shown anymore.
2058 HIGHLIGHT_CB is the callback to call when entering/leaving menu items. */
2060 xg_modify_menubar_widgets (menubar
, f
, val
, deep_p
,
2061 select_cb
, deactivate_cb
, highlight_cb
)
2066 GCallback select_cb
;
2067 GCallback deactivate_cb
;
2068 GCallback highlight_cb
;
2070 xg_menu_cb_data
*cl_data
;
2071 GList
*list
= gtk_container_get_children (GTK_CONTAINER (menubar
));
2076 cl_data
= (xg_menu_cb_data
*) g_object_get_data (G_OBJECT (menubar
),
2081 widget_value
*cur
= val
->contents
;
2082 xg_update_menubar (menubar
, f
, list
, cur
,
2083 select_cb
, highlight_cb
, cl_data
);
2089 /* Update all sub menus.
2090 We must keep the submenu names (GTK menu item widgets) since the
2091 X Window in the XEvent that activates the menu are those widgets. */
2093 /* Update cl_data, menu_item things in F may have changed. */
2094 update_cl_data (cl_data
, f
, highlight_cb
);
2096 for (cur
= val
->contents
; cur
; cur
= cur
->next
)
2102 /* Find sub menu that corresponds to val and update it. */
2103 for (iter
= list
; iter
; iter
= g_list_next (iter
))
2105 witem
= GTK_MENU_ITEM (iter
->data
);
2106 if (xg_item_label_same_p (witem
, cur
->name
))
2108 sub
= gtk_menu_item_get_submenu (witem
);
2113 newsub
= xg_update_submenu (sub
,
2120 /* sub may still be NULL. If we just updated non deep and added
2121 a new menu bar item, it has no sub menu yet. So we set the
2122 newly created sub menu under witem. */
2124 gtk_menu_item_set_submenu (witem
, newsub
);
2129 gtk_widget_show_all (menubar
);
2132 /* Recompute all the widgets of frame F, when the menu bar has been
2133 changed. Value is non-zero if widgets were updated. */
2136 xg_update_frame_menubar (f
)
2139 struct x_output
*x
= f
->output_data
.x
;
2142 if (!x
->menubar_widget
|| GTK_WIDGET_MAPPED (x
->menubar_widget
))
2147 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
,
2149 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->menubar_widget
, 0);
2151 gtk_widget_show_all (x
->menubar_widget
);
2152 gtk_widget_size_request (x
->menubar_widget
, &req
);
2154 FRAME_MENUBAR_HEIGHT (f
) = req
.height
;
2156 /* The height has changed, resize outer widget and set columns
2157 rows to what we had before adding the menu bar. */
2158 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2160 SET_FRAME_GARBAGED (f
);
2164 /* Get rid of the menu bar of frame F, and free its storage.
2165 This is used when deleting a frame, and when turning off the menu bar. */
2168 free_frame_menubar (f
)
2171 struct x_output
*x
= f
->output_data
.x
;
2173 if (x
->menubar_widget
)
2177 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
), x
->menubar_widget
);
2178 /* The menubar and its children shall be deleted when removed from
2180 x
->menubar_widget
= 0;
2181 FRAME_MENUBAR_HEIGHT (f
) = 0;
2183 /* The height has changed, resize outer widget and set columns
2184 rows to what we had before removing the menu bar. */
2185 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2187 SET_FRAME_GARBAGED (f
);
2194 /***********************************************************************
2195 Scroll bar functions
2196 ***********************************************************************/
2199 /* Setting scroll bar values invokes the callback. Use this variable
2200 to indicate that callback should do nothing. */
2201 int xg_ignore_gtk_scrollbar
;
2203 /* After we send a scroll bar event, x_set_toolkit_scroll_bar_thumb will
2204 be called. For some reason that needs to be debugged, it gets called
2205 with bad values. Thus, we set this variable to ignore those calls. */
2206 int xg_ignore_next_thumb
;
2208 /* SET_SCROLL_BAR_X_WINDOW assumes the second argument fits in
2209 32 bits. But we want to store pointers, and they may be larger
2210 than 32 bits. Keep a mapping from integer index to widget pointers
2211 to get around the 32 bit limitation. */
2214 GtkWidget
**widgets
;
2217 } id_to_widget
= { 0, 0, 0 };
2219 /* Grow this much every time we need to allocate more */
2220 #define ID_TO_WIDGET_INCR 32
2222 /* Store the widget pointer W in id_to_widget and return the integer index. */
2224 xg_store_widget_in_map (w
)
2229 if (id_to_widget
.max_size
== id_to_widget
.used
)
2231 int new_size
= id_to_widget
.max_size
+ ID_TO_WIDGET_INCR
;
2233 id_to_widget
.widgets
= xrealloc (id_to_widget
.widgets
,
2234 sizeof (GtkWidget
*)*new_size
);
2236 for (i
= id_to_widget
.max_size
; i
< new_size
; ++i
)
2237 id_to_widget
.widgets
[i
] = 0;
2238 id_to_widget
.max_size
= new_size
;
2241 /* Just loop over the array and find a free place. After all,
2242 how many scroll bars are we creating? Should be a small number.
2243 The check above guarantees we will find a free place. */
2244 for (i
= 0; i
< id_to_widget
.max_size
; ++i
)
2246 if (! id_to_widget
.widgets
[i
])
2248 id_to_widget
.widgets
[i
] = w
;
2249 ++id_to_widget
.used
;
2255 /* Should never end up here */
2259 /* Remove pointer at IDX from id_to_widget.
2260 Called when scroll bar is destroyed. */
2262 xg_remove_widget_from_map (idx
)
2265 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
2267 id_to_widget
.widgets
[idx
] = 0;
2268 --id_to_widget
.used
;
2272 /* Get the widget pointer at IDX from id_to_widget. */
2274 xg_get_widget_from_map (idx
)
2277 if (idx
< id_to_widget
.max_size
&& id_to_widget
.widgets
[idx
] != 0)
2278 return id_to_widget
.widgets
[idx
];
2283 /* Callback invoked when scroll bar WIDGET is destroyed.
2284 DATA is the index into id_to_widget for WIDGET.
2285 We free pointer to last scroll bar value here and remove the index. */
2287 xg_gtk_scroll_destroy (widget
, data
)
2294 p
= g_object_get_data (G_OBJECT (widget
), XG_LAST_SB_DATA
);
2296 xg_remove_widget_from_map (id
);
2299 /* Callback for button press/release events. Used to start timer so that
2300 the scroll bar repetition timer in GTK gets handeled.
2301 WIDGET is the scroll bar widget the event is for (not used).
2302 EVENT contains the event.
2303 USER_DATA is 0 (not used).
2305 Returns FALSE to tell GTK that it shall continue propagate the event
2308 scroll_bar_button_cb (widget
, event
, user_data
)
2310 GdkEventButton
*event
;
2313 if (event
->type
== GDK_BUTTON_PRESS
&& ! xg_timer
)
2315 else if (event
->type
== GDK_BUTTON_RELEASE
&& xg_timer
)
2321 /* Create a scroll bar widget for frame F. Store the scroll bar
2323 SCROLL_CALLBACK is the callback to invoke when the value of the
2325 SCROLL_BAR_NAME is the name we use for the scroll bar. Can be used
2326 to set resources for the widget. */
2328 xg_create_scroll_bar (f
, bar
, scroll_callback
, scroll_bar_name
)
2330 struct scroll_bar
*bar
;
2331 GCallback scroll_callback
;
2332 char *scroll_bar_name
;
2338 /* Page, step increment values are not so important here, they
2339 will be corrected in x_set_toolkit_scroll_bar_thumb. */
2340 vadj
= gtk_adjustment_new (XG_SB_MIN
, XG_SB_MIN
, XG_SB_MAX
,
2343 wscroll
= gtk_vscrollbar_new (GTK_ADJUSTMENT (vadj
));
2344 gtk_widget_set_name (wscroll
, scroll_bar_name
);
2345 gtk_range_set_update_policy (GTK_RANGE (wscroll
), GTK_UPDATE_CONTINUOUS
);
2347 scroll_id
= xg_store_widget_in_map (wscroll
);
2349 g_signal_connect (G_OBJECT (vadj
),
2353 g_signal_connect (G_OBJECT (wscroll
),
2355 G_CALLBACK (xg_gtk_scroll_destroy
),
2356 (gpointer
)scroll_id
);
2358 /* Connect to button press and button release to detect if any scroll bar
2360 g_signal_connect (G_OBJECT (wscroll
),
2361 "button-press-event",
2362 G_CALLBACK (scroll_bar_button_cb
),
2364 g_signal_connect (G_OBJECT (wscroll
),
2365 "button-release-event",
2366 G_CALLBACK (scroll_bar_button_cb
),
2369 gtk_fixed_put (GTK_FIXED (f
->output_data
.x
->edit_widget
),
2372 /* Set the cursor to an arrow. */
2373 xg_set_cursor (wscroll
, &xg_left_ptr_cursor
);
2375 SET_SCROLL_BAR_X_WINDOW (bar
, scroll_id
);
2378 /* Make the scroll bar represented by SCROLLBAR_ID visible. */
2380 xg_show_scroll_bar (scrollbar_id
)
2383 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
2385 gtk_widget_show (w
);
2388 /* Remove the scroll bar represented by SCROLLBAR_ID from the frame F. */
2390 xg_remove_scroll_bar (f
, scrollbar_id
)
2394 GtkWidget
*w
= xg_get_widget_from_map (scrollbar_id
);
2397 gtk_widget_destroy (w
);
2398 SET_FRAME_GARBAGED (f
);
2403 /* Update the position of the vertical scroll bar represented by SCROLLBAR_ID
2405 TOP/LEFT are the new pixel positions where the bar shall appear.
2406 WIDTH, HEIGHT is the size in pixels the bar shall have. */
2408 xg_update_scrollbar_pos (f
, scrollbar_id
, top
, left
, width
, height
)
2416 GtkWidget
*wscroll
= xg_get_widget_from_map (scrollbar_id
);
2420 int gheight
= max (height
, 1);
2422 gtk_fixed_move (GTK_FIXED (f
->output_data
.x
->edit_widget
),
2423 wscroll
, left
, top
);
2425 gtk_widget_set_size_request (wscroll
, width
, gheight
);
2427 /* Must force out update so wscroll gets the resize.
2428 Otherwise, the gdk_window_clear clears the old window size. */
2429 gdk_window_process_all_updates ();
2431 /* The scroll bar doesn't explicitly redraw the whole window
2432 when a resize occurs. Since the scroll bar seems to be fixed
2433 in width it doesn't fill the space reserved, so we must clear
2434 the whole window. */
2435 gdk_window_clear (wscroll
->window
);
2437 /* Since we are not using a pure gtk event loop, we must force out
2438 pending update events with this call. */
2439 gdk_window_process_all_updates ();
2441 SET_FRAME_GARBAGED (f
);
2442 cancel_mouse_face (f
);
2446 /* Set the thumb size and position of scroll bar BAR. We are currently
2447 displaying PORTION out of a whole WHOLE, and our position POSITION. */
2449 xg_set_toolkit_scroll_bar_thumb (bar
, portion
, position
, whole
)
2450 struct scroll_bar
*bar
;
2451 int portion
, position
, whole
;
2453 GtkWidget
*wscroll
= xg_get_widget_from_map (SCROLL_BAR_X_WINDOW (bar
));
2455 FRAME_PTR f
= XFRAME (WINDOW_FRAME (XWINDOW (bar
->window
)));
2458 if (wscroll
&& ! xg_ignore_next_thumb
)
2465 adj
= gtk_range_get_adjustment (GTK_RANGE (wscroll
));
2471 shown
= (gdouble
) portion
/ whole
;
2472 top
= (gdouble
) position
/ whole
;
2475 size
= shown
* whole
;
2476 size
= min (size
, whole
);
2477 size
= max (size
, 1);
2479 value
= top
* whole
;
2480 value
= min (value
, whole
- size
);
2481 value
= max (value
, XG_SB_MIN
);
2483 adj
->upper
= max (whole
, size
);
2484 adj
->page_size
= (int)size
;
2486 /* Assume a page increment is about 95% of the page size */
2487 adj
->page_increment
= (int) (0.95*adj
->page_size
);
2489 /* Assume all lines are equal. */
2490 adj
->step_increment
= portion
/ max (1, FRAME_HEIGHT (f
));
2492 /* gtk_range_set_value invokes the callback. Set
2493 ignore_gtk_scrollbar to make the callback do nothing */
2494 xg_ignore_gtk_scrollbar
= 1;
2495 gtk_range_set_value (GTK_RANGE (wscroll
), (gdouble
)value
);
2496 xg_ignore_gtk_scrollbar
= 0;
2499 /* Make sure the scroll bar is redrawn with new thumb */
2500 gtk_widget_queue_draw (wscroll
);
2501 gdk_window_process_all_updates ();
2502 xg_ignore_next_thumb
= 0;
2507 /***********************************************************************
2509 ***********************************************************************/
2510 /* The key for the data we put in the GtkImage widgets. The data is
2511 the image used by Emacs. We use this to see if we need to update
2512 the GtkImage with a new image. */
2513 #define XG_TOOL_BAR_IMAGE_DATA "emacs-tool-bar-image"
2515 /* Callback function invoked when a tool bar item is pressed.
2516 W is the button widget in the tool bar that got pressed,
2517 CLIENT_DATA is an integer that is the index of the button in the
2518 tool bar. 0 is the first button. */
2520 xg_tool_bar_callback (w
, client_data
)
2522 gpointer client_data
;
2524 int idx
= (int)client_data
;
2525 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
2526 Lisp_Object key
, frame
;
2527 struct input_event event
;
2529 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
2532 idx
*= TOOL_BAR_ITEM_NSLOTS
;
2534 key
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_KEY
);
2535 XSETFRAME (frame
, f
);
2536 event
.kind
= TOOL_BAR_EVENT
;
2537 event
.frame_or_window
= frame
;
2539 kbd_buffer_store_event (&event
);
2541 event
.kind
= TOOL_BAR_EVENT
;
2542 event
.frame_or_window
= frame
;
2544 event
.modifiers
= 0; /* These are not available. */
2545 kbd_buffer_store_event (&event
);
2548 /* This callback is called when a tool bar is detached. We must set
2549 the height of the tool bar to zero when this happens so frame sizes
2550 are correctly calculated.
2551 WBOX is the handle box widget that enables detach/attach of the tool bar.
2552 W is the tool bar widget.
2553 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2555 xg_tool_bar_detach_callback (wbox
, w
, client_data
)
2558 gpointer client_data
;
2560 FRAME_PTR f
= (FRAME_PTR
) client_data
;
2564 /* When detaching a tool bar, not everything dissapear. There are
2565 a few pixels left that are used to drop the tool bar back into
2567 int bw
= gtk_container_get_border_width (GTK_CONTAINER (wbox
));
2568 FRAME_TOOLBAR_HEIGHT (f
) = 2;
2570 /* The height has changed, resize outer widget and set columns
2571 rows to what we had before detaching the tool bar. */
2572 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2576 /* This callback is called when a tool bar is reattached. We must set
2577 the height of the tool bar when this happens so frame sizes
2578 are correctly calculated.
2579 WBOX is the handle box widget that enables detach/attach of the tool bar.
2580 W is the tool bar widget.
2581 CLIENT_DATA is a pointer to the frame the tool bar belongs to. */
2583 xg_tool_bar_attach_callback (wbox
, w
, client_data
)
2586 gpointer client_data
;
2588 FRAME_PTR f
= (FRAME_PTR
) client_data
;
2594 gtk_widget_size_request (w
, &req
);
2595 FRAME_TOOLBAR_HEIGHT (f
) = req
.height
;
2597 /* The height has changed, resize outer widget and set columns
2598 rows to what we had before detaching the tool bar. */
2599 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2603 /* This callback is called when the mouse enters or leaves a tool bar item.
2604 It is used for displaying and hiding the help text.
2605 W is the tool bar item, a button.
2606 EVENT is either an enter event or leave event.
2607 CLIENT_DATA is an integer that is the index of the button in the
2608 tool bar. 0 is the first button.
2610 Returns FALSE to tell GTK to keep processing this event. */
2612 xg_tool_bar_help_callback (w
, event
, client_data
)
2614 GdkEventCrossing
*event
;
2615 gpointer client_data
;
2617 int idx
= (int)client_data
;
2618 FRAME_PTR f
= (FRAME_PTR
) g_object_get_data (G_OBJECT (w
), XG_FRAME_DATA
);
2619 Lisp_Object help
, frame
;
2621 if (! GTK_IS_BUTTON (w
))
2626 if (! f
|| ! f
->n_tool_bar_items
|| NILP (f
->tool_bar_items
))
2629 if (event
->type
== GDK_ENTER_NOTIFY
)
2631 idx
*= TOOL_BAR_ITEM_NSLOTS
;
2632 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_HELP
);
2635 help
= AREF (f
->tool_bar_items
, idx
+ TOOL_BAR_ITEM_CAPTION
);
2640 XSETFRAME (frame
, f
);
2641 kbd_buffer_store_help_event (frame
, help
);
2648 xg_create_tool_bar (f
)
2651 struct x_output
*x
= f
->output_data
.x
;
2653 int vbox_pos
= x
->menubar_widget
? 1 : 0;
2655 x
->toolbar_widget
= gtk_toolbar_new ();
2656 x
->handlebox_widget
= gtk_handle_box_new ();
2657 gtk_container_add (GTK_CONTAINER (x
->handlebox_widget
),
2660 gtk_box_pack_start (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
2663 gtk_box_reorder_child (GTK_BOX (x
->vbox_widget
), x
->handlebox_widget
,
2666 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-detached",
2667 G_CALLBACK (xg_tool_bar_detach_callback
), f
);
2668 g_signal_connect (G_OBJECT (x
->handlebox_widget
), "child-attached",
2669 G_CALLBACK (xg_tool_bar_attach_callback
), f
);
2671 gtk_widget_show_all (x
->handlebox_widget
);
2673 gtk_widget_size_request (x
->toolbar_widget
, &req
);
2674 FRAME_TOOLBAR_HEIGHT (f
) = req
.height
;
2676 /* The height has changed, resize outer widget and set columns
2677 rows to what we had before adding the tool bar. */
2678 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2680 SET_FRAME_GARBAGED (f
);
2684 update_frame_tool_bar (f
)
2688 GtkRequisition old_req
, new_req
;
2690 struct x_output
*x
= f
->output_data
.x
;
2692 if (! FRAME_GTK_WIDGET (f
))
2697 if (! x
->toolbar_widget
)
2698 xg_create_tool_bar (f
);
2700 gtk_widget_size_request (x
->toolbar_widget
, &old_req
);
2702 icon_list
= gtk_container_get_children (GTK_CONTAINER (x
->toolbar_widget
));
2704 for (i
= 0; i
< f
->n_tool_bar_items
; ++i
)
2706 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
2708 int enabled_p
= !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P
));
2709 int selected_p
= !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P
));
2714 GtkWidget
*wicon
= icon_list
? GTK_WIDGET (icon_list
->data
) : 0;
2716 if (icon_list
) icon_list
= g_list_next (icon_list
);
2718 /* If image is a vector, choose the image according to the
2720 image
= PROP (TOOL_BAR_ITEM_IMAGES
);
2721 if (VECTORP (image
))
2725 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
2726 : TOOL_BAR_IMAGE_ENABLED_DESELECTED
);
2729 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
2730 : TOOL_BAR_IMAGE_DISABLED_DESELECTED
);
2732 xassert (ASIZE (image
) >= idx
);
2733 image
= AREF (image
, idx
);
2738 /* Ignore invalid image specifications. */
2739 if (!valid_image_p (image
))
2741 if (wicon
) gtk_widget_hide (wicon
);
2745 img_id
= lookup_image (f
, image
);
2746 img
= IMAGE_FROM_ID (f
, img_id
);
2750 GdkPixmap
*gpix
= gdk_pixmap_foreign_new (img
->pixmap
);
2751 GdkBitmap
*gmask
= img
->mask
?
2752 (GdkBitmap
*) gdk_pixmap_foreign_new (img
->mask
) : 0;
2754 GtkWidget
*w
= gtk_image_new_from_pixmap (gpix
, gmask
);
2755 gtk_toolbar_append_item (GTK_TOOLBAR (x
->toolbar_widget
),
2758 GTK_SIGNAL_FUNC (xg_tool_bar_callback
),
2761 /* Save the image so we can see if an update is needed when
2762 this function is called again. */
2763 g_object_set_data (G_OBJECT (w
), XG_TOOL_BAR_IMAGE_DATA
,
2766 /* We must set sensitive on the button that is the parent
2767 of the GtkImage parent. Go upwards until we find the button. */
2768 while (! GTK_IS_BUTTON (w
))
2769 w
= gtk_widget_get_parent (w
);
2773 /* Save the frame in the button so the xg_tool_bar_callback
2775 g_object_set_data (G_OBJECT (w
), XG_FRAME_DATA
, (gpointer
)f
);
2776 gtk_widget_set_sensitive (w
, enabled_p
);
2778 /* Use enter/leave notify to show help. We use the events
2779 rather than the GtkButton specific signals "enter" and
2780 "leave", so we can have only one callback. The event
2781 will tell us what kind of event it is. */
2782 g_signal_connect (G_OBJECT (w
),
2783 "enter-notify-event",
2784 G_CALLBACK (xg_tool_bar_help_callback
),
2786 g_signal_connect (G_OBJECT (w
),
2787 "leave-notify-event",
2788 G_CALLBACK (xg_tool_bar_help_callback
),
2794 /* The child of the tool bar is a button. Inside that button
2795 is a vbox. Inside that vbox is the GtkImage. */
2796 GtkWidget
*wvbox
= gtk_bin_get_child (GTK_BIN (wicon
));
2797 GList
*ch
= gtk_container_get_children (GTK_CONTAINER (wvbox
));
2798 GtkImage
*wimage
= GTK_IMAGE (ch
->data
);
2799 struct image
*old_img
= g_object_get_data (G_OBJECT (wimage
),
2800 XG_TOOL_BAR_IMAGE_DATA
);
2803 || old_img
->pixmap
!= img
->pixmap
2804 || old_img
->mask
!= img
->mask
)
2806 GdkPixmap
*gpix
= gdk_pixmap_foreign_new (img
->pixmap
);
2807 GdkBitmap
*gmask
= img
->mask
?
2808 (GdkBitmap
*) gdk_pixmap_foreign_new (img
->mask
) : 0;
2810 gtk_image_set_from_pixmap (wimage
, gpix
, gmask
);
2813 g_object_set_data (G_OBJECT (wimage
), XG_TOOL_BAR_IMAGE_DATA
,
2816 gtk_widget_set_sensitive (wicon
, enabled_p
);
2817 gtk_widget_show (wicon
);
2823 /* Remove buttons not longer needed. We just hide them so they
2824 can be reused later on. */
2827 GtkWidget
*w
= GTK_WIDGET (icon_list
->data
);
2828 gtk_widget_hide (w
);
2829 icon_list
= g_list_next (icon_list
);
2832 gtk_widget_size_request (x
->toolbar_widget
, &new_req
);
2833 if (old_req
.height
!= new_req
.height
)
2835 FRAME_TOOLBAR_HEIGHT (f
) = new_req
.height
;
2836 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2839 /* Must force out update so changed images gets redrawn. */
2840 gdk_window_process_all_updates ();
2846 free_frame_tool_bar (f
)
2849 struct x_output
*x
= f
->output_data
.x
;
2851 if (x
->toolbar_widget
)
2853 fprintf(stderr
, "Remove toolbar\n");
2855 gtk_container_remove (GTK_CONTAINER (x
->vbox_widget
),
2856 x
->handlebox_widget
);
2857 x
->toolbar_widget
= 0;
2858 x
->handlebox_widget
= 0;
2859 FRAME_TOOLBAR_HEIGHT (f
) = 0;
2861 /* The height has changed, resize outer widget and set columns
2862 rows to what we had before removing the tool bar. */
2863 xg_resize_outer_widget (f
, FRAME_WIDTH (f
), FRAME_HEIGHT (f
));
2865 SET_FRAME_GARBAGED (f
);
2872 /***********************************************************************
2874 ***********************************************************************/
2878 xg_ignore_gtk_scrollbar
= 0;
2879 xg_ignore_next_thumb
= 0;
2880 xg_left_ptr_cursor
= 0;
2883 xg_menu_cb_list
.prev
= xg_menu_cb_list
.next
=
2884 xg_menu_item_cb_list
.prev
= xg_menu_item_cb_list
.next
= 0;
2886 /* Remove F10 as a menu accelerator, it does not mix well with Emacs key
2887 bindings. It doesn't seem to be any way to remove properties,
2888 so we set it to VoidSymbol which in X means "no key". */
2889 gtk_settings_set_string_property (gtk_settings_get_default (),
2890 "gtk-menu-bar-accel",
2895 #endif /* USE_GTK */