Debian Codegraph @

goffice-bits/goffice/gtk/go-combo-box.c

raw symbols SCIP
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * gnm-combo-box.c - a customizable combobox
4 * Copyright 2000, 2001, Ximian, Inc.
5 *
6 * Authors:
7 * Miguel de Icaza ([email protected])
8 * Adrian E Feiguin ([email protected])
9 * Paolo Molnaro ([email protected]).
10 * Jon K Hellan ([email protected])
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License, version 2, as published by the Free Software Foundation.
15 *
16 * This library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24 * USA.
25 */
26
27 #include <goffice/goffice-config.h>
28 #include "go-combo-box.h"
29 #include <goffice/utils/go-marshalers.h>
30 #include <goffice/gtk/goffice-gtk.h>
31
32 #include <gdk/gdkkeysyms.h>
33 #include <glib/gi18n-lib.h>
34
35 #include <gsf/gsf-impl-utils.h>
36
37 enum {
38 POP_DOWN_DONE,
39 LAST_SIGNAL
40 };
41
42 struct _GOComboBoxPrivate {
43 GtkWidget *popdown_container;
44 GtkWidget *popdown_focus; /* Popup's toplevel when not torn off */
45 GtkWidget *display_widget;
46
47 /* Internal widgets used to implement the ComboBox */
48 GtkWidget *frame;
49 GtkWidget *arrow_button;
50
51 GtkWidget *toplevel; /* Popup's toplevel when not torn off */
52 GtkWidget *tearoff_window; /* Popup's toplevel when torn off */
53 gboolean torn_off;
54 gulong tearoff_signal;
55
56 GtkWidget *tearable; /* The tearoff "button" */
57 GtkWidget *popup; /* Popup */
58
59 gboolean updating_buttons;
60 };
61 static GObjectClass *go_combo_box_parent_class;
62 static guint go_combo_box_signals [LAST_SIGNAL] = { 0, };
63
64 static void go_combo_set_tearoff_state (GOComboBox *combo, gboolean torn_off);
65
66 /**
67 * go_combo_popup_reparent
68 * @popup: Popup
69 * @new_parent: New parent
70 * @unrealize: Unrealize popup if TRUE.
71 *
72 * Reparent the popup, taking care of the refcounting
73 *
74 * Compare with gtk_menu_reparent in gtk/gtkmenu.c
75 */
76 static void
77 go_combo_popup_reparent (GtkWidget *popup,
78 GtkWidget *new_parent,
79 gboolean unrealize)
80 {
81 gboolean was_floating = g_object_is_floating (popup);
82 g_object_ref_sink (popup);
83
84 if (unrealize) {
85 g_object_ref (popup);
86 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (popup)), popup);
87 gtk_container_add (GTK_CONTAINER (new_parent), popup);
88 g_object_unref (popup);
89 }
90 else
91 gtk_widget_reparent (GTK_WIDGET (popup), new_parent);
92 gtk_widget_set_size_request (new_parent, -1, -1);
93
94 if (was_floating) {
95 g_object_force_floating (G_OBJECT (popup));
96 } else
97 g_object_unref (popup);
98 }
99
100 static void
101 go_combo_box_finalize (GObject *object)
102 {
103 GOComboBox *combo_box = GO_COMBO_BOX (object);
104
105 g_free (combo_box->priv);
106
107 go_combo_box_parent_class->finalize (object);
108 }
109
110 static void
111 go_combo_box_destroy (GtkWidget *widget)
112 {
113 GtkWidgetClass *klass = (GtkWidgetClass *)go_combo_box_parent_class;
114 GOComboBox *combo_box = GO_COMBO_BOX (widget);
115
116 if (combo_box->priv->toplevel) {
117 gtk_widget_destroy (combo_box->priv->toplevel);
118 g_object_unref (combo_box->priv->toplevel);
119 combo_box->priv->toplevel = NULL;
120 }
121
122 if (combo_box->priv->tearoff_window) {
123 gtk_widget_destroy (combo_box->priv->tearoff_window);
124 g_object_unref (combo_box->priv->tearoff_window);
125 combo_box->priv->tearoff_window = NULL;
126 }
127
128 if (klass->destroy)
129 klass->destroy (widget);
130 }
131
132 /* Cut and paste from gtkwindow.c */
133 static void
134 do_focus_change (GtkWidget *widget, gboolean in)
135 {
136 GdkEventFocus fevent;
137
138 fevent.type = GDK_FOCUS_CHANGE;
139 fevent.window = gtk_widget_get_window (widget);
140 fevent.in = in;
141 gtk_widget_send_focus_change (widget, (GdkEvent*)&fevent);
142 }
143
144 static void
145 set_arrow_state (GOComboBox *combo_box, gboolean state)
146 {
147 GOComboBoxPrivate *priv = combo_box->priv;
148 g_return_if_fail (!combo_box->priv->updating_buttons);
149
150 combo_box->priv->updating_buttons = TRUE;
151 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), state);
152 if (GTK_IS_TOGGLE_BUTTON (priv->display_widget))
153 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->display_widget), state);
154 combo_box->priv->updating_buttons = FALSE;
155 }
156
157 static void
158 go_combo_box_popup_hide_unconditional (GOComboBox *combo_box)
159 {
160 gboolean popup_info_destroyed = FALSE;
161 GObject *pdc;
162
163 g_return_if_fail (combo_box != NULL);
164 g_return_if_fail (GO_IS_COMBO_BOX (combo_box));
165
166 gtk_widget_hide (combo_box->priv->toplevel);
167 gtk_widget_hide (combo_box->priv->popup);
168 if (combo_box->priv->torn_off)
169 go_combo_set_tearoff_state (combo_box, FALSE);
170
171 do_focus_change (combo_box->priv->toplevel, FALSE);
172 gtk_grab_remove (combo_box->priv->toplevel);
173 gdk_device_ungrab (gtk_get_current_event_device (),
174 GDK_CURRENT_TIME);
175
176 pdc = g_object_ref (combo_box->priv->popdown_container);
177 g_signal_emit (combo_box,
178 go_combo_box_signals [POP_DOWN_DONE], 0,
179 pdc, &popup_info_destroyed);
180
181 if (popup_info_destroyed){
182 gtk_container_remove (
183 GTK_CONTAINER (combo_box->priv->frame),
184 combo_box->priv->popdown_container);
185 combo_box->priv->popdown_container = NULL;
186 }
187 g_object_unref (pdc);
188 set_arrow_state (combo_box, FALSE);
189 }
190
191 static gboolean
192 cb_arrow_pressed (GOComboBox *combo_box)
193 {
194 if (!combo_box->priv->updating_buttons) {
195 if (combo_box->priv->toplevel == NULL ||
196 !gtk_widget_get_visible (combo_box->priv->toplevel))
197 go_combo_box_popup_display (combo_box);
198 else
199 go_combo_box_popup_hide_unconditional (combo_box);
200 }
201
202 return TRUE;
203 }
204
205 static gboolean
206 go_combo_box_mnemonic_activate (GtkWidget *w, G_GNUC_UNUSED gboolean group_cycling)
207 {
208 GOComboBox *combo_box = GO_COMBO_BOX (w);
209 cb_arrow_pressed (combo_box);
210 return TRUE;
211 }
212
213 static void
214 go_combo_box_style_set (GtkWidget *widget,
215 G_GNUC_UNUSED GtkStyle *prev_style)
216 {
217 gboolean add_tearoffs;
218 gtk_widget_style_get (widget,
219 "add-tearoffs", &add_tearoffs,
220 NULL);
221 go_combo_box_set_tearable (GO_COMBO_BOX (widget), add_tearoffs);
222 }
223
224 static void
225 go_combo_box_realize (GtkWidget *widget)
226 {
227 GOComboBox *combo = (GOComboBox *)widget;
228 GdkCursor *cursor;
229
230 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
231 GDK_TOP_LEFT_ARROW);
232 gtk_widget_realize (combo->priv->popup);
233 gdk_window_set_cursor (gtk_widget_get_window (combo->priv->popup), cursor);
234 g_object_unref (cursor);
235
236 ((GtkWidgetClass *)go_combo_box_parent_class)->realize (widget);
237 }
238
239 static void
240 go_combo_box_class_init (GObjectClass *object_class)
241 {
242 GtkWidgetClass *widget_class = (GtkWidgetClass *)object_class;
243 go_combo_box_parent_class = g_type_class_peek_parent (object_class);
244
245 object_class->finalize = go_combo_box_finalize;
246 widget_class->mnemonic_activate = go_combo_box_mnemonic_activate;
247 widget_class->realize = go_combo_box_realize;
248 ((GtkWidgetClass *)object_class)->destroy = go_combo_box_destroy;
249
250 gtk_widget_class_install_style_property
251 (widget_class,
252 g_param_spec_boolean ("add-tearoffs",
253 _("Add tearoffs to menus"),
254 _("Whether dropdowns should have a tearoff menu item"),
255 FALSE,
256 GSF_PARAM_STATIC |
257 G_PARAM_READABLE));
258 widget_class->style_set = go_combo_box_style_set;
259
260 go_combo_box_signals [POP_DOWN_DONE] = g_signal_new (
261 "pop_down_done",
262 G_OBJECT_CLASS_TYPE (object_class),
263 G_SIGNAL_RUN_LAST,
264 G_STRUCT_OFFSET (GOComboBoxClass, pop_down_done),
265 NULL, NULL,
266 go__BOOLEAN__OBJECT,
267 G_TYPE_BOOLEAN, 1, G_TYPE_OBJECT);
268 }
269
270 gboolean
271 _go_combo_is_updating (GOComboBox const *combo_box)
272 {
273 return combo_box->priv->updating_buttons;
274 }
275
276 static gint
277 cb_combo_keypress (G_GNUC_UNUSED GtkWidget *widget, GdkEventKey *event,
278 GOComboBox *combo_box)
279 {
280 if (event->keyval == GDK_KEY_Escape) {
281 go_combo_box_popup_hide_unconditional (combo_box);
282 return TRUE;
283 }
284 return FALSE;
285 }
286
287 /**
288 * go_combo_popup_tear_off
289 * @combo: Combo box
290 * @set_position: Set to position of popup shell if true
291 *
292 * Tear off the popup
293 *
294 * FIXME:
295 * Gtk popup menus are toplevel windows, not dialogs. I think this is wrong,
296 * and make the popups dialogs. But may be there should be a way to make
297 * them toplevel. We can do this after creating:
298 * GTK_WINDOW (tearoff)->type = GTK_WINDOW_TOPLEVEL;
299 */
300 static void
301 go_combo_popup_tear_off (GOComboBox *combo, gboolean set_position)
302 {
303 int x, y;
304
305 if (!combo->priv->tearoff_window) {
306 GtkWidget *tearoff;
307 gchar const *title;
308
309 /* FIXME: made this a toplevel, not a dialog ! */
310 tearoff = gtk_window_new (GTK_WINDOW_TOPLEVEL);
311 g_object_ref_sink (tearoff);
312 combo->priv->tearoff_window = tearoff;
313 gtk_widget_set_app_paintable (tearoff, TRUE);
314 g_signal_connect (tearoff, "key_press_event",
315 G_CALLBACK (cb_combo_keypress),
316 combo);
317 gtk_widget_realize (tearoff);
318 title = go_combo_box_get_title (combo);
319 if (title)
320 gdk_window_set_title (gtk_widget_get_window (tearoff), title);
321 gtk_window_set_transient_for
322 (GTK_WINDOW (tearoff),
323 GTK_WINDOW (gtk_widget_get_toplevel
324 GTK_WIDGET (combo)));
325 }
326
327 if (gtk_widget_get_visible (combo->priv->popup)) {
328 gtk_widget_hide (combo->priv->toplevel);
329
330 gtk_grab_remove (combo->priv->toplevel);
331 gdk_device_ungrab (gtk_get_current_event_device (),
332 GDK_CURRENT_TIME);
333 }
334
335 go_combo_popup_reparent (combo->priv->popup,
336 combo->priv->tearoff_window, FALSE);
337
338 /* It may have got confused about size */
339 gtk_widget_queue_resize (GTK_WIDGET (combo->priv->popup));
340
341 if (set_position) {
342 go_combo_box_get_pos (combo, &x, &y);
343 gtk_window_move (GTK_WINDOW (combo->priv->tearoff_window), x, y);
344 }
345 gtk_widget_show (GTK_WIDGET (combo->priv->popup));
346 gtk_widget_show (combo->priv->tearoff_window);
347
348 }
349
350 /**
351 * go_combo_box_popup_hide:
352 * @combo: a #GOComboBox
353 *
354 * Hides popup, but not when it is torn off.
355 * This is the external interface - for subclasses and apps which expect a
356 * regular combo which doesn't do tearoffs.
357 */
358 /* protected */ void
359 go_combo_box_popup_hide (GOComboBox *combo_box)
360 {
361 if (!combo_box->priv->torn_off)
362 go_combo_box_popup_hide_unconditional (combo_box);
363 else if (gtk_widget_get_visible(combo_box->priv->toplevel)) {
364 /* Both popup and tearoff window present. Get rid of just
365 the popup shell. */
366 go_combo_popup_tear_off (combo_box, FALSE);
367 set_arrow_state (combo_box, FALSE);
368 }
369 }
370
371 /*
372 * Find best location for displaying
373 */
374 /* protected */ void
375 go_combo_box_get_pos (GOComboBox *combo_box, int *x, int *y)
376 {
377 GtkWidget *wcombo = GTK_WIDGET (combo_box);
378 GdkScreen *screen = gtk_widget_get_screen (wcombo);
379 int ph, pw;
380 GtkAllocation allocation;
381
382 gdk_window_get_origin (gtk_widget_get_window (wcombo), x, y);
383 gtk_widget_get_allocation (wcombo, &allocation);
384 *y += allocation.height + allocation.y;
385 *x += allocation.x;
386
387 gtk_widget_get_allocation (combo_box->priv->popup, &allocation);
388 ph = allocation.height;
389 pw = allocation.width;
390
391 if ((*y + ph) > gdk_screen_get_height (screen))
392 *y = gdk_screen_get_height (screen) - ph;
393
394 if ((*x + pw) > gdk_screen_get_width (screen))
395 *x = gdk_screen_get_width (screen) - pw;
396 }
397
398 /**
399 * go_combo_tearoff_bg_copy
400 * @combo_box: Combo box
401 *
402 * Copy popup window image to the tearoff window.
403 */
404 static void
405 go_combo_tearoff_bg_copy (G_GNUC_UNUSED GOComboBox *combo)
406 {
407 #if 0
408 /* FIXME: is this function really needed? seems things work without it */
409 GdkPixmap *pixmap;
410 GdkGC *gc;
411 GdkGCValues gc_values;
412 GtkAllocation allocation;
413
414 GtkWidget *widget = combo->priv->popup;
415
416 if (combo->priv->torn_off) {
417 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
418 gc = gdk_gc_new_with_values (gtk_widget_get_window (widget),
419 &gc_values, GDK_GC_SUBWINDOW);
420
421 gtk_widget_get_allocation (widget, &allocation);
422 pixmap = gdk_pixmap_new (gtk_widget_get_window (widget),
423 allocation.width,
424 allocation.height,
425 -1);
426
427 gdk_draw_drawable (pixmap, gc,
428 gtk_widget_get_window (widget),
429 0, 0, 0, 0, -1, -1);
430 g_object_unref (gc);
431
432 gtk_widget_set_size_request (combo->priv->tearoff_window,
433 allocation.width,
434 allocation.height);
435
436 gdk_window_set_back_pixmap
437 (gtk_widget_get_window (combo->priv->tearoff_window), pixmap, FALSE);
438 g_object_unref (pixmap);
439 }
440 #endif
441 }
442
443 /* protected */ void
444 go_combo_box_popup_display (GOComboBox *combo_box)
445 {
446 int x, y;
447
448 g_return_if_fail (GO_COMBO_BOX (combo_box) != NULL);
449 g_return_if_fail (combo_box->priv->popdown_container != NULL);
450
451 if (combo_box->priv->torn_off) {
452 /* To give the illusion that tearoff still displays the
453 * popup, we copy the image in the popup window to the
454 * background. Thus, it won't be blank after reparenting */
455 go_combo_tearoff_bg_copy (combo_box);
456
457 /* We force an unrealize here so that we don't trigger
458 * redrawing/ clearing code - we just want to reveal our
459 * backing pixmap.
460 */
461 go_combo_popup_reparent (combo_box->priv->popup,
462 combo_box->priv->toplevel, TRUE);
463 }
464
465 go_combo_box_get_pos (combo_box, &x, &y);
466
467 gtk_window_move (GTK_WINDOW (combo_box->priv->toplevel), x, y);
468 gtk_widget_realize (combo_box->priv->popup);
469 gtk_widget_show (combo_box->priv->popup);
470 gtk_widget_realize (combo_box->priv->toplevel);
471 gtk_widget_show (combo_box->priv->toplevel);
472
473 gtk_widget_grab_focus (combo_box->priv->toplevel);
474 do_focus_change (combo_box->priv->toplevel, TRUE);
475
476 gtk_grab_add (combo_box->priv->toplevel);
477 gdk_device_grab (gtk_get_current_event_device (),
478 gtk_widget_get_window (combo_box->priv->toplevel),
479 GDK_OWNERSHIP_APPLICATION, TRUE,
480 GDK_BUTTON_PRESS_MASK |
481 GDK_BUTTON_RELEASE_MASK |
482 GDK_POINTER_MOTION_MASK,
483 NULL, GDK_CURRENT_TIME);
484 set_arrow_state (combo_box, TRUE);
485 }
486
487 static gint
488 go_combo_box_button_press (GtkWidget *widget, GdkEventButton *event, GOComboBox *combo_box)
489 {
490 GtkWidget *child = gtk_get_event_widget ((GdkEvent *) event);
491 if (child != widget){
492 while (child){
493 if (child == widget)
494 return FALSE;
495 child = gtk_widget_get_parent (child);
496 }
497 }
498
499 go_combo_box_popup_hide (combo_box);
500 return TRUE;
501 }
502
503 static void
504 cb_state_change (GtkWidget *widget, G_GNUC_UNUSED GtkStateType old_state, GOComboBox *combo_box)
505 {
506 GtkStateType const new_state = gtk_widget_get_state (widget);
507 if (combo_box->priv->display_widget)
508 gtk_widget_set_state (combo_box->priv->display_widget,
509 new_state);
510 }
511
512 static void
513 go_combo_box_init (GOComboBox *combo_box)
514 {
515 GtkWidget *arrow;
516
517 combo_box->priv = g_new0 (GOComboBoxPrivate, 1);
518 combo_box->priv->updating_buttons = FALSE;
519
520 combo_box->priv->arrow_button = gtk_toggle_button_new ();
521 gtk_button_set_relief (GTK_BUTTON (combo_box->priv->arrow_button), GTK_RELIEF_NONE);
522 gtk_widget_set_can_focus (combo_box->priv->arrow_button, FALSE);
523
524 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
525 gtk_container_add (GTK_CONTAINER (combo_box->priv->arrow_button), arrow);
526 gtk_box_pack_end (GTK_BOX (combo_box), combo_box->priv->arrow_button, FALSE, FALSE, 0);
527 g_signal_connect_swapped (combo_box->priv->arrow_button,
528 "button-press-event",
529 G_CALLBACK (cb_arrow_pressed), combo_box);
530 gtk_widget_show_all (combo_box->priv->arrow_button);
531
532 /*
533 * prelight the display widget when mousing over the arrow.
534 */
535 g_signal_connect (combo_box->priv->arrow_button, "state-changed",
536 G_CALLBACK (cb_state_change), combo_box);
537
538 /*
539 * The pop-down container
540 */
541
542 combo_box->priv->toplevel = gtk_window_new (GTK_WINDOW_POPUP);
543 g_object_ref (combo_box->priv->toplevel);
544 g_object_set (G_OBJECT (combo_box->priv->toplevel),
545 "type-hint", GDK_WINDOW_TYPE_HINT_COMBO,
546 NULL);
547
548 combo_box->priv->popup = gtk_event_box_new ();
549 gtk_container_add (GTK_CONTAINER (combo_box->priv->toplevel),
550 combo_box->priv->popup);
551 gtk_widget_show (combo_box->priv->popup);
552
553 combo_box->priv->torn_off = FALSE;
554 combo_box->priv->tearoff_window = NULL;
555
556 combo_box->priv->frame = gtk_frame_new (NULL);
557 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup),
558 combo_box->priv->frame);
559 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->frame), GTK_SHADOW_OUT);
560
561 g_signal_connect (combo_box->priv->toplevel, "button_press_event",
562 G_CALLBACK (go_combo_box_button_press), combo_box);
563 g_signal_connect (combo_box->priv->toplevel, "key_press_event",
564 G_CALLBACK (cb_combo_keypress), combo_box);
565 }
566
567 GSF_CLASS (GOComboBox, go_combo_box,
568 go_combo_box_class_init, go_combo_box_init,
569 GTK_TYPE_HBOX)
570
571 /**
572 * go_combo_box_set_display:
573 * @combo: the #GOComboBox to modify
574 * @display_widget: The widget to be displayed
575
576 * Sets the displayed widget for the @combo_box to be @display_widget
577 **/
578 /* protected */ void
579 go_combo_box_set_display (GOComboBox *combo_box, GtkWidget *display_widget)
580 {
581 g_return_if_fail (GO_IS_COMBO_BOX (combo_box));
582 g_return_if_fail (!display_widget || GTK_IS_WIDGET (display_widget));
583
584 if (display_widget == combo_box->priv->display_widget)
585 return;
586
587 if (combo_box->priv->display_widget)
588 gtk_container_remove (GTK_CONTAINER (combo_box),
589 combo_box->priv->display_widget);
590 combo_box->priv->display_widget = display_widget;
591
592 if (display_widget)
593 gtk_box_pack_start (GTK_BOX (combo_box), display_widget,
594 TRUE, TRUE, 0);
595 }
596
597 static gboolean
598 cb_tearable_enter_leave (GtkWidget *w, G_GNUC_UNUSED GdkEventCrossing *event, gpointer data)
599 {
600 gboolean const flag = GPOINTER_TO_INT(data);
601 gtk_widget_set_state (w, flag ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
602 return FALSE;
603 }
604
605 /**
606 * go_combo_set_tearoff_state
607 * @combo_box: Combo box
608 * @torn_off: TRUE: Tear off. FALSE: Pop down and reattach
609 *
610 * Set the tearoff state of the popup
611 *
612 * Compare with gtk_menu_set_tearoff_state in gtk/gtkmenu.c
613 */
614 static void
615 go_combo_set_tearoff_state (GOComboBox *combo,
616 gboolean torn_off)
617 {
618 g_return_if_fail (combo != NULL);
619 g_return_if_fail (GO_IS_COMBO_BOX (combo));
620
621 if (combo->priv->torn_off != torn_off) {
622 combo->priv->torn_off = torn_off;
623
624 if (combo->priv->torn_off) {
625 go_combo_popup_tear_off (combo, TRUE);
626 set_arrow_state (combo, FALSE);
627 } else {
628 gtk_widget_hide (combo->priv->tearoff_window);
629 go_combo_popup_reparent (combo->priv->popup,
630 combo->priv->toplevel,
631 FALSE);
632 }
633 }
634 }
635
636 static gboolean
637 cb_popup_delete (GOComboBox *combo)
638 {
639 go_combo_box_popup_hide_unconditional (combo);
640 return TRUE;
641 }
642
643 static gboolean
644 cb_tearable_button_release (GtkWidget *w, G_GNUC_UNUSED GdkEventButton *event,
645 GOComboBox *combo)
646 {
647 g_return_val_if_fail (w != NULL, FALSE);
648 g_return_val_if_fail (GTK_IS_TEAROFF_MENU_ITEM (w), FALSE);
649
650 /* FIXME: should we notify the parent menu? */
651
652 if (!combo->priv->torn_off) {
653 gboolean need_connect;
654
655 need_connect = (!combo->priv->tearoff_window);
656 go_combo_set_tearoff_state (combo, TRUE);
657 if (need_connect)
658 g_signal_connect_swapped (combo->priv->tearoff_window,
659 "delete_event",
660 G_CALLBACK (cb_popup_delete), combo);
661 } else
662 go_combo_box_popup_hide_unconditional (combo);
663
664 return TRUE;
665 }
666
667 static void
668 cb_tearoff_state_changed (GtkMenu *menu, G_GNUC_UNUSED GParamSpec *pspec, GOComboBox *combo)
669 {
670 combo->priv->torn_off = gtk_menu_get_tearoff_state (menu);
671 }
672
673 static void
674 cb_tearable_parent_changed (GtkWidget *tearable_menu, GtkWidget *old_parent, GOComboBox *combo)
675 {
676 GtkWidget *new_parent = gtk_widget_get_parent (tearable_menu);
677
678 if (old_parent && combo->priv->tearoff_signal) {
679 g_signal_handler_disconnect (old_parent, combo->priv->tearoff_signal);
680 combo->priv->tearoff_signal = 0;
681 }
682 if (GTK_IS_MENU (new_parent)) {
683 combo->priv->torn_off = gtk_menu_get_tearoff_state (GTK_MENU (new_parent));
684 combo->priv->tearoff_signal = g_signal_connect (new_parent, "notify::tearoff-state",
685 G_CALLBACK (cb_tearoff_state_changed),
686 combo);
687 }
688 }
689
690 void
691 go_combo_box_construct (GOComboBox *combo,
692 GtkWidget *display_widget,
693 GtkWidget *popdown_container,
694 G_GNUC_UNUSED GtkWidget *popdown_focus)
695 {
696 GtkWidget *tearable;
697 GtkWidget *vbox;
698
699 g_return_if_fail (GO_IS_COMBO_BOX (combo));
700
701 gtk_box_set_spacing (GTK_BOX (combo), 0);
702 gtk_box_set_homogeneous (GTK_BOX (combo), FALSE);
703
704 combo->priv->popdown_container = popdown_container;
705 combo->priv->display_widget = NULL;
706
707 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
708 tearable = gtk_tearoff_menu_item_new ();
709 g_signal_connect (tearable, "enter-notify-event",
710 G_CALLBACK (cb_tearable_enter_leave),
711 GINT_TO_POINTER (TRUE));
712 g_signal_connect (tearable, "leave-notify-event",
713 G_CALLBACK (cb_tearable_enter_leave),
714 GINT_TO_POINTER (FALSE));
715 g_signal_connect (tearable, "button-release-event",
716 G_CALLBACK (cb_tearable_button_release),
717 (gpointer) combo);
718 g_signal_connect (tearable, "parent-set",
719 G_CALLBACK (cb_tearable_parent_changed),
720 (gpointer) combo);
721 gtk_box_pack_start (GTK_BOX (vbox), tearable, FALSE, FALSE, 0);
722 gtk_box_pack_start (GTK_BOX (vbox), popdown_container, TRUE, TRUE, 0);
723 combo->priv->tearable = tearable;
724 g_object_set (tearable, "no-show-all", TRUE, NULL);
725
726 go_combo_box_set_tearable (combo, FALSE);
727
728 go_combo_box_set_relief (combo, GTK_RELIEF_NORMAL);
729
730 go_combo_box_set_display (combo, display_widget);
731 gtk_container_add (GTK_CONTAINER (combo->priv->frame), vbox);
732 gtk_widget_show_all (combo->priv->frame);
733 }
734
735 /**
736 * go_combo_box_set_title
737 * @combo: Combo box
738 * @title: Title
739 *
740 * Set a title to display over the tearoff window.
741 *
742 * FIXME:
743 *
744 * This should really change the title even when the popup is already torn off.
745 * I guess the tearoff window could attach a listener to title change or
746 * something. But I don't think we need the functionality, so I didn't bother
747 * to investigate.
748 *
749 * MW: Just make it a property.
750 */
751 void
752 go_combo_box_set_title (GOComboBox *combo, char const *title)
753 {
754 GOComboBoxClass *klass = G_TYPE_INSTANCE_GET_CLASS (combo,
755 GO_TYPE_COMBO_BOX, GOComboBoxClass);
756
757 g_return_if_fail (klass != NULL);
758
759 g_object_set_data_full (G_OBJECT (combo), "go-combo-title",
760 g_strdup (title), (GDestroyNotify) g_free);
761
762 if (klass->set_title)
763 (klass->set_title) (combo, title);
764 }
765
766 char const *
767 go_combo_box_get_title (GOComboBox *combo)
768 {
769 return g_object_get_data (G_OBJECT (combo), "go-combo-title");
770 }
771
772 /**
773 * go_combo_box_set_tearable:
774 * @combo: Combo box
775 * @tearable: whether to allow the @combo to be tearable
776 *
777 * controls whether the combo box's pop up widget can be torn off.
778 */
779 void
780 go_combo_box_set_tearable (GOComboBox *combo, gboolean tearable)
781 {
782 g_return_if_fail (GO_IS_COMBO_BOX (combo));
783
784 if (tearable){
785 gtk_widget_show (combo->priv->tearable);
786 } else {
787 go_combo_set_tearoff_state (combo, FALSE);
788 gtk_widget_hide (combo->priv->tearable);
789 }
790 }
791
792 void
793 go_combo_box_set_relief (GOComboBox *combo, GtkReliefStyle relief)
794 {
795 g_return_if_fail (GO_IS_COMBO_BOX (combo));
796
797 gtk_button_set_relief (GTK_BUTTON (combo->priv->arrow_button), relief);
798 if (GTK_IS_BUTTON (combo->priv->display_widget))
799 gtk_button_set_relief (GTK_BUTTON (combo->priv->display_widget), relief);
800 }
801
802 void
803 go_combo_box_set_tooltip (GOComboBox *c, G_GNUC_UNUSED void *tips,
804 char const *text,
805 G_GNUC_UNUSED char const *priv_text)
806 {
807 /* FIXME FIXME FIXME this is ugly the tip moves as we jump from preview to arrow */
808 gtk_widget_set_tooltip_text (c->priv->display_widget, text);
809 gtk_widget_set_tooltip_text (c->priv->arrow_button, text);
810 }