Debian Codegraph @

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