evolution-3.6.4/widgets/misc/e-attachment-button.c

No issues found

  1 /*
  2  * e-attachment-button.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 19  *
 20  */
 21 
 22 /* Much of the popup menu logic here was ripped from GtkMenuToolButton. */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include "e-attachment-button.h"
 29 
 30 #define E_ATTACHMENT_BUTTON_GET_PRIVATE(obj) \
 31 	(G_TYPE_INSTANCE_GET_PRIVATE \
 32 	((obj), E_TYPE_ATTACHMENT_BUTTON, EAttachmentButtonPrivate))
 33 
 34 struct _EAttachmentButtonPrivate {
 35 
 36 	EAttachmentView *view;
 37 	EAttachment *attachment;
 38 	gulong reference_handler_id;
 39 
 40 	GBinding *can_show_binding;
 41 	GBinding *shown_binding;
 42 
 43 	GtkWidget *expand_button;
 44 	GtkWidget *toggle_button;
 45 	GtkWidget *cell_view;
 46 	GtkWidget *popup_menu;
 47 
 48 	guint expandable : 1;
 49 	guint expanded   : 1;
 50 };
 51 
 52 enum {
 53 	PROP_0,
 54 	PROP_ATTACHMENT,
 55 	PROP_EXPANDABLE,
 56 	PROP_EXPANDED,
 57 	PROP_VIEW
 58 };
 59 
 60 G_DEFINE_TYPE (
 61 	EAttachmentButton,
 62 	e_attachment_button,
 63 	GTK_TYPE_HBOX)
 64 
 65 static void
 66 attachment_button_menu_deactivate_cb (EAttachmentButton *button)
 67 {
 68 	EAttachmentView *view;
 69 	GtkActionGroup *action_group;
 70 	GtkToggleButton *toggle_button;
 71 
 72 	view = e_attachment_button_get_view (button);
 73 	action_group = e_attachment_view_get_action_group (view, "inline");
 74 	toggle_button = GTK_TOGGLE_BUTTON (button->priv->toggle_button);
 75 
 76 	gtk_toggle_button_set_active (toggle_button, FALSE);
 77 
 78 	gtk_action_group_set_visible (action_group, FALSE);
 79 }
 80 
 81 static void
 82 attachment_button_menu_position (GtkMenu *menu,
 83                                  gint *x,
 84                                  gint *y,
 85                                  gboolean *push_in,
 86                                  EAttachmentButton *button)
 87 {
 88 	GtkRequisition menu_requisition;
 89 	GtkTextDirection direction;
 90 	GtkAllocation allocation;
 91 	GdkRectangle monitor;
 92 	GdkScreen *screen;
 93 	GdkWindow *window;
 94 	GtkWidget *widget;
 95 	GtkWidget *toggle_button;
 96 	gint monitor_num;
 97 
 98 	widget = GTK_WIDGET (button);
 99 	toggle_button = button->priv->toggle_button;
100 	gtk_widget_get_preferred_size (GTK_WIDGET (menu), &menu_requisition, NULL);
101 
102 	window = gtk_widget_get_parent_window (widget);
103 	screen = gtk_widget_get_screen (GTK_WIDGET (menu));
104 	monitor_num = gdk_screen_get_monitor_at_window (screen, window);
105 	if (monitor_num < 0)
106 		monitor_num = 0;
107 	gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
108 
109 	gtk_widget_get_allocation (widget, &allocation);
110 
111 	gdk_window_get_origin (window, x, y);
112 	*x += allocation.x;
113 	*y += allocation.y;
114 
115 	direction = gtk_widget_get_direction (widget);
116 	if (direction == GTK_TEXT_DIR_LTR)
117 		*x += MAX (allocation.width - menu_requisition.width, 0);
118 	else if (menu_requisition.width > allocation.width)
119 		*x -= menu_requisition.width - allocation.width;
120 
121 	gtk_widget_get_allocation (toggle_button, &allocation);
122 
123 	if ((*y + allocation.height +
124 		menu_requisition.height) <= monitor.y + monitor.height)
125 		*y += allocation.height;
126 	else if ((*y - menu_requisition.height) >= monitor.y)
127 		*y -= menu_requisition.height;
128 	else if (monitor.y + monitor.height -
129 		(*y + allocation.height) > *y)
130 		*y += allocation.height;
131 	else
132 		*y -= menu_requisition.height;
133 
134 	*push_in = FALSE;
135 }
136 
137 static void
138 attachment_button_select_path (EAttachmentButton *button)
139 {
140 	EAttachmentView *view;
141 	EAttachment *attachment;
142 	GtkTreeRowReference *reference;
143 	GtkTreePath *path;
144 
145 	attachment = e_attachment_button_get_attachment (button);
146 	g_return_if_fail (E_IS_ATTACHMENT (attachment));
147 
148 	reference = e_attachment_get_reference (attachment);
149 	g_return_if_fail (gtk_tree_row_reference_valid (reference));
150 
151 	view = e_attachment_button_get_view (button);
152 	path = gtk_tree_row_reference_get_path (reference);
153 
154 	e_attachment_view_unselect_all (view);
155 	e_attachment_view_select_path (view, path);
156 
157 	gtk_tree_path_free (path);
158 }
159 
160 static void
161 attachment_button_show_popup_menu (EAttachmentButton *button,
162                                    GdkEventButton *event)
163 {
164 	EAttachmentView *view;
165 	GtkActionGroup *action_group;
166 	GtkToggleButton *toggle_button;
167 
168 	view = e_attachment_button_get_view (button);
169 	action_group = e_attachment_view_get_action_group (view, "inline");
170 	toggle_button = GTK_TOGGLE_BUTTON (button->priv->toggle_button);
171 
172 	attachment_button_select_path (button);
173 	gtk_toggle_button_set_active (toggle_button, TRUE);
174 
175 	e_attachment_view_show_popup_menu (
176 		view, event, (GtkMenuPositionFunc)
177 		attachment_button_menu_position, button);
178 
179 	gtk_action_group_set_visible (action_group, TRUE);
180 }
181 
182 static void
183 attachment_button_update_cell_view (EAttachmentButton *button)
184 {
185 	GtkCellView *cell_view;
186 	EAttachment *attachment;
187 	GtkTreeRowReference *reference;
188 	GtkTreeModel *model = NULL;
189 	GtkTreePath *path = NULL;
190 
191 	cell_view = GTK_CELL_VIEW (button->priv->cell_view);
192 
193 	attachment = e_attachment_button_get_attachment (button);
194 	if (attachment == NULL)
195 		goto exit;
196 
197 	reference = e_attachment_get_reference (attachment);
198 	if (reference == NULL)
199 		goto exit;
200 
201 	model = gtk_tree_row_reference_get_model (reference);
202 	path = gtk_tree_row_reference_get_path (reference);
203 
204 exit:
205 	gtk_cell_view_set_model (cell_view, model);
206 	gtk_cell_view_set_displayed_row (cell_view, path);
207 
208 	if (path != NULL)
209 		gtk_tree_path_free (path);
210 }
211 
212 static void
213 attachment_button_update_pixbufs (EAttachmentButton *button)
214 {
215 	GtkCellLayout *cell_layout;
216 	GtkCellRenderer *renderer;
217 	GdkPixbuf *pixbuf_expander_open;
218 	GdkPixbuf *pixbuf_expander_closed;
219 	GList *list;
220 
221 	/* Grab the first cell renderer. */
222 	cell_layout = GTK_CELL_LAYOUT (button->priv->cell_view);
223 	list = gtk_cell_layout_get_cells (cell_layout);
224 	renderer = GTK_CELL_RENDERER (list->data);
225 	g_list_free (list);
226 
227 	pixbuf_expander_open = gtk_widget_render_icon (
228 		GTK_WIDGET (button), GTK_STOCK_GO_DOWN,
229 		GTK_ICON_SIZE_BUTTON, NULL);
230 
231 	pixbuf_expander_closed = gtk_widget_render_icon (
232 		GTK_WIDGET (button), GTK_STOCK_GO_FORWARD,
233 		GTK_ICON_SIZE_BUTTON, NULL);
234 
235 	g_object_set (
236 		renderer,
237 		"pixbuf-expander-open", pixbuf_expander_open,
238 		"pixbuf-expander-closed", pixbuf_expander_closed,
239 		NULL);
240 
241 	g_object_unref (pixbuf_expander_open);
242 	g_object_unref (pixbuf_expander_closed);
243 }
244 
245 static void
246 attachment_button_expand_clicked_cb (EAttachmentButton *button)
247 {
248 	gboolean expanded;
249 
250 	expanded = e_attachment_button_get_expanded (button);
251 	e_attachment_button_set_expanded (button, !expanded);
252 }
253 
254 static void
255 attachment_button_expand_drag_begin_cb (EAttachmentButton *button,
256                                         GdkDragContext *context)
257 {
258 	EAttachmentView *view;
259 
260 	view = e_attachment_button_get_view (button);
261 
262 	attachment_button_select_path (button);
263 	e_attachment_view_drag_begin (view, context);
264 }
265 
266 static void
267 attachment_button_expand_drag_data_get_cb (EAttachmentButton *button,
268                                            GdkDragContext *context,
269                                            GtkSelectionData *selection,
270                                            guint info,
271                                            guint time)
272 {
273 	EAttachmentView *view;
274 
275 	if (button->priv->attachment) {
276 		gchar *mime_type;
277 
278 		mime_type = e_attachment_get_mime_type (
279 			button->priv->attachment);
280 
281 		if (mime_type) {
282 			gboolean processed = FALSE;
283 			GdkAtom atom;
284 			gchar *atom_name;
285 
286 			atom = gtk_selection_data_get_target (selection);
287 			atom_name = gdk_atom_name (atom);
288 
289 			if (g_strcmp0 (atom_name, mime_type) == 0) {
290 				CamelMimePart *mime_part;
291 
292 				mime_part = e_attachment_get_mime_part (
293 					button->priv->attachment);
294 
295 				if (CAMEL_IS_MIME_PART (mime_part)) {
296 					CamelDataWrapper *wrapper;
297 					CamelStream *stream;
298 					GByteArray *buffer;
299 
300 					buffer = g_byte_array_new ();
301 					stream = camel_stream_mem_new ();
302 					camel_stream_mem_set_byte_array (
303 						CAMEL_STREAM_MEM (stream),
304 						buffer);
305 					wrapper = camel_medium_get_content (
306 						CAMEL_MEDIUM (mime_part));
307 					camel_data_wrapper_decode_to_stream_sync (
308 						wrapper, stream, NULL, NULL);
309 					g_object_unref (stream);
310 
311 					gtk_selection_data_set (
312 						selection, atom, 8,
313 						buffer->data, buffer->len);
314 					processed = TRUE;
315 
316 					g_byte_array_free (buffer, TRUE);
317 				}
318 			}
319 
320 			g_free (atom_name);
321 			g_free (mime_type);
322 
323 			if (processed)
324 				return;
325 		}
326 	}
327 
328 	view = e_attachment_button_get_view (button);
329 
330 	e_attachment_view_drag_data_get (
331 		view, context, selection, info, time);
332 }
333 
334 static void
335 attachment_button_expand_drag_end_cb (EAttachmentButton *button,
336                                       GdkDragContext *context)
337 {
338 	EAttachmentView *view;
339 
340 	view = e_attachment_button_get_view (button);
341 
342 	e_attachment_view_drag_end (view, context);
343 }
344 
345 static gboolean
346 attachment_button_toggle_button_press_event_cb (EAttachmentButton *button,
347                                                 GdkEventButton *event)
348 {
349 	if (event->button == 1) {
350 		attachment_button_show_popup_menu (button, event);
351 		return TRUE;
352 	}
353 
354 	return FALSE;
355 }
356 
357 static void
358 attachment_button_set_property (GObject *object,
359                                 guint property_id,
360                                 const GValue *value,
361                                 GParamSpec *pspec)
362 {
363 	switch (property_id) {
364 		case PROP_ATTACHMENT:
365 			e_attachment_button_set_attachment (
366 				E_ATTACHMENT_BUTTON (object),
367 				g_value_get_object (value));
368 			return;
369 
370 		case PROP_EXPANDABLE:
371 			e_attachment_button_set_expandable (
372 				E_ATTACHMENT_BUTTON (object),
373 				g_value_get_boolean (value));
374 			return;
375 
376 		case PROP_EXPANDED:
377 			e_attachment_button_set_expanded (
378 				E_ATTACHMENT_BUTTON (object),
379 				g_value_get_boolean (value));
380 			return;
381 
382 		case PROP_VIEW:
383 			e_attachment_button_set_view (
384 				E_ATTACHMENT_BUTTON (object),
385 				g_value_get_object (value));
386 			return;
387 	}
388 
389 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390 }
391 
392 static void
393 attachment_button_get_property (GObject *object,
394                                 guint property_id,
395                                 GValue *value,
396                                 GParamSpec *pspec)
397 {
398 	switch (property_id) {
399 		case PROP_ATTACHMENT:
400 			g_value_set_object (
401 				value,
402 				e_attachment_button_get_attachment (
403 				E_ATTACHMENT_BUTTON (object)));
404 			return;
405 
406 		case PROP_EXPANDABLE:
407 			g_value_set_boolean (
408 				value,
409 				e_attachment_button_get_expandable (
410 				E_ATTACHMENT_BUTTON (object)));
411 			return;
412 
413 		case PROP_EXPANDED:
414 			g_value_set_boolean (
415 				value,
416 				e_attachment_button_get_expanded (
417 				E_ATTACHMENT_BUTTON (object)));
418 			return;
419 
420 		case PROP_VIEW:
421 			g_value_set_object (
422 				value,
423 				e_attachment_button_get_view (
424 				E_ATTACHMENT_BUTTON (object)));
425 			return;
426 	}
427 
428 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
429 }
430 
431 static void
432 attachment_button_dispose (GObject *object)
433 {
434 	EAttachmentButtonPrivate *priv;
435 
436 	priv = E_ATTACHMENT_BUTTON_GET_PRIVATE (object);
437 
438 	if (priv->view != NULL) {
439 		g_object_unref (priv->view);
440 		priv->view = NULL;
441 	}
442 
443 	if (priv->attachment != NULL) {
444 		g_signal_handler_disconnect (
445 			priv->attachment,
446 			priv->reference_handler_id);
447 		g_object_unref (priv->attachment);
448 		priv->attachment = NULL;
449 	}
450 
451 	if (priv->expand_button != NULL) {
452 		g_object_unref (priv->expand_button);
453 		priv->expand_button = NULL;
454 	}
455 
456 	if (priv->toggle_button != NULL) {
457 		g_object_unref (priv->toggle_button);
458 		priv->toggle_button = NULL;
459 	}
460 
461 	if (priv->cell_view != NULL) {
462 		g_object_unref (priv->cell_view);
463 		priv->cell_view = NULL;
464 	}
465 
466 	if (priv->popup_menu != NULL) {
467 		g_signal_handlers_disconnect_matched (
468 			priv->popup_menu, G_SIGNAL_MATCH_DATA,
469 			0, 0, NULL, NULL, object);
470 		g_object_unref (priv->popup_menu);
471 		priv->popup_menu = NULL;
472 	}
473 
474 	/* Chain up to parent's dispose() method. */
475 	G_OBJECT_CLASS (e_attachment_button_parent_class)->dispose (object);
476 }
477 
478 static void
479 attachment_button_style_set (GtkWidget *widget,
480                              GtkStyle *previous_style)
481 {
482 	EAttachmentButton *button;
483 
484 	/* Chain up to parent's style_set() method. */
485 	GTK_WIDGET_CLASS (e_attachment_button_parent_class)->
486 		style_set (widget, previous_style);
487 
488 	button = E_ATTACHMENT_BUTTON (widget);
489 	attachment_button_update_pixbufs (button);
490 }
491 
492 static void
493 e_attachment_button_class_init (EAttachmentButtonClass *class)
494 {
495 	GObjectClass *object_class;
496 	GtkWidgetClass *widget_class;
497 
498 	g_type_class_add_private (class, sizeof (EAttachmentButtonPrivate));
499 
500 	object_class = G_OBJECT_CLASS (class);
501 	object_class->set_property = attachment_button_set_property;
502 	object_class->get_property = attachment_button_get_property;
503 	object_class->dispose = attachment_button_dispose;
504 
505 	widget_class = GTK_WIDGET_CLASS (class);
506 	widget_class->style_set = attachment_button_style_set;
507 
508 	g_object_class_install_property (
509 		object_class,
510 		PROP_ATTACHMENT,
511 		g_param_spec_object (
512 			"attachment",
513 			"Attachment",
514 			NULL,
515 			E_TYPE_ATTACHMENT,
516 			G_PARAM_READWRITE));
517 
518 	g_object_class_install_property (
519 		object_class,
520 		PROP_EXPANDABLE,
521 		g_param_spec_boolean (
522 			"expandable",
523 			"Expandable",
524 			NULL,
525 			TRUE,
526 			G_PARAM_READWRITE |
527 			G_PARAM_CONSTRUCT));
528 
529 	g_object_class_install_property (
530 		object_class,
531 		PROP_EXPANDED,
532 		g_param_spec_boolean (
533 			"expanded",
534 			"Expanded",
535 			NULL,
536 			FALSE,
537 			G_PARAM_READWRITE |
538 			G_PARAM_CONSTRUCT));
539 
540 	g_object_class_install_property (
541 		object_class,
542 		PROP_VIEW,
543 		g_param_spec_object (
544 			"view",
545 			"View",
546 			NULL,
547 			E_TYPE_ATTACHMENT_VIEW,
548 			G_PARAM_READWRITE));
549 }
550 
551 static void
552 e_attachment_button_init (EAttachmentButton *button)
553 {
554 	GtkCellRenderer *renderer;
555 	GtkCellLayout *cell_layout;
556 	GtkTargetEntry *targets;
557 	GtkTargetList *list;
558 	GtkWidget *container;
559 	GtkWidget *widget;
560 	GtkStyleContext *context;
561 	gint n_targets;
562 
563 	button->priv = E_ATTACHMENT_BUTTON_GET_PRIVATE (button);
564 
565 	/* Configure Widgets */
566 
567 	container = GTK_WIDGET (button);
568 	context = gtk_widget_get_style_context (container);
569 	gtk_style_context_add_class (context, "linked");
570 
571 	widget = gtk_button_new ();
572 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
573 	button->priv->expand_button = g_object_ref (widget);
574 	gtk_widget_show (widget);
575 
576 	g_object_bind_property (
577 		button, "expandable",
578 		widget, "sensitive",
579 		G_BINDING_BIDIRECTIONAL |
580 		G_BINDING_SYNC_CREATE);
581 
582 	widget = gtk_toggle_button_new ();
583 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
584 	button->priv->toggle_button = g_object_ref (widget);
585 	gtk_widget_show (widget);
586 
587 	container = button->priv->expand_button;
588 
589 	widget = gtk_cell_view_new ();
590 	gtk_container_add (GTK_CONTAINER (container), widget);
591 	button->priv->cell_view = g_object_ref (widget);
592 	gtk_widget_show (widget);
593 
594 	container = button->priv->toggle_button;
595 
596 	widget = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
597 	gtk_container_add (GTK_CONTAINER (container), widget);
598 	gtk_widget_show (widget);
599 
600 	/* Configure Renderers */
601 
602 	cell_layout = GTK_CELL_LAYOUT (button->priv->cell_view);
603 
604 	renderer = gtk_cell_renderer_pixbuf_new ();
605 	g_object_set (renderer, "is-expander", TRUE, NULL);
606 	gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
607 
608 	g_object_bind_property (
609 		button, "expanded",
610 		renderer, "is-expanded",
611 		G_BINDING_BIDIRECTIONAL |
612 		G_BINDING_SYNC_CREATE);
613 
614 	renderer = gtk_cell_renderer_pixbuf_new ();
615 	g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
616 	gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
617 
618 	gtk_cell_layout_add_attribute (
619 		cell_layout, renderer, "gicon",
620 		E_ATTACHMENT_STORE_COLUMN_ICON);
621 
622 	/* Configure Drag and Drop */
623 
624 	list = gtk_target_list_new (NULL, 0);
625 	gtk_target_list_add_uri_targets (list, 0);
626 	targets = gtk_target_table_new_from_list (list, &n_targets);
627 
628 	gtk_drag_source_set (
629 		button->priv->expand_button, GDK_BUTTON1_MASK,
630 		targets, n_targets, GDK_ACTION_COPY);
631 
632 	gtk_drag_source_set (
633 		button->priv->toggle_button, GDK_BUTTON1_MASK,
634 		targets, n_targets, GDK_ACTION_COPY);
635 
636 	gtk_target_table_free (targets, n_targets);
637 	gtk_target_list_unref (list);
638 
639 	/* Configure Signal Handlers */
640 
641 	g_signal_connect_swapped (
642 		button->priv->expand_button, "clicked",
643 		G_CALLBACK (attachment_button_expand_clicked_cb), button);
644 
645 	g_signal_connect_swapped (
646 		button->priv->expand_button, "drag-begin",
647 		G_CALLBACK (attachment_button_expand_drag_begin_cb),
648 		button);
649 
650 	g_signal_connect_swapped (
651 		button->priv->expand_button, "drag-data-get",
652 		G_CALLBACK (attachment_button_expand_drag_data_get_cb),
653 		button);
654 
655 	g_signal_connect_swapped (
656 		button->priv->expand_button, "drag-end",
657 		G_CALLBACK (attachment_button_expand_drag_end_cb),
658 		button);
659 
660 	g_signal_connect_swapped (
661 		button->priv->toggle_button, "button-press-event",
662 		G_CALLBACK (attachment_button_toggle_button_press_event_cb),
663 		button);
664 
665 	g_signal_connect_swapped (
666 		button->priv->toggle_button, "drag-begin",
667 		G_CALLBACK (attachment_button_expand_drag_begin_cb),
668 		button);
669 
670 	g_signal_connect_swapped (
671 		button->priv->toggle_button, "drag-data-get",
672 		G_CALLBACK (attachment_button_expand_drag_data_get_cb),
673 		button);
674 
675 	g_signal_connect_swapped (
676 		button->priv->toggle_button, "drag-end",
677 		G_CALLBACK (attachment_button_expand_drag_end_cb),
678 		button);
679 }
680 
681 GtkWidget *
682 e_attachment_button_new ()
683 {
684 	return g_object_new (
685 		E_TYPE_ATTACHMENT_BUTTON, NULL);
686 }
687 
688 EAttachmentView *
689 e_attachment_button_get_view (EAttachmentButton *button)
690 {
691 	g_return_val_if_fail (E_IS_ATTACHMENT_BUTTON (button), NULL);
692 
693 	return button->priv->view;
694 }
695 
696 void
697 e_attachment_button_set_view (EAttachmentButton *button,
698                               EAttachmentView *view)
699 {
700 	GtkWidget *popup_menu;
701 
702 	g_return_if_fail (button->priv->view == NULL);
703 
704 	g_object_ref (view);
705 	if (button->priv->view)
706 		g_object_unref (button->priv->view);
707 	button->priv->view = view;
708 
709 	popup_menu = e_attachment_view_get_popup_menu (view);
710 
711 	g_signal_connect_swapped (
712 		popup_menu, "deactivate",
713 		G_CALLBACK (attachment_button_menu_deactivate_cb), button);
714 
715 	/* Keep a reference to the popup menu so we can
716 	 * disconnect the signal handler in dispose(). */
717 	if (button->priv->popup_menu)
718 		g_object_unref (button->priv->popup_menu);
719 	button->priv->popup_menu = g_object_ref (popup_menu);
720 }
721 
722 EAttachment *
723 e_attachment_button_get_attachment (EAttachmentButton *button)
724 {
725 	g_return_val_if_fail (E_IS_ATTACHMENT_BUTTON (button), NULL);
726 
727 	return button->priv->attachment;
728 }
729 
730 void
731 e_attachment_button_set_attachment (EAttachmentButton *button,
732                                     EAttachment *attachment)
733 {
734 	GtkTargetEntry *targets;
735 	GtkTargetList *list;
736 	gint n_targets;
737 
738 	g_return_if_fail (E_IS_ATTACHMENT_BUTTON (button));
739 
740 	if (attachment != NULL) {
741 		g_return_if_fail (E_IS_ATTACHMENT (attachment));
742 		g_object_ref (attachment);
743 	}
744 
745 	if (button->priv->attachment != NULL) {
746 		g_object_unref (button->priv->can_show_binding);
747 		button->priv->can_show_binding = NULL;
748 		g_object_unref (button->priv->shown_binding);
749 		button->priv->shown_binding = NULL;
750 		g_signal_handler_disconnect (
751 			button->priv->attachment,
752 			button->priv->reference_handler_id);
753 		g_object_unref (button->priv->attachment);
754 	}
755 
756 	button->priv->attachment = attachment;
757 
758 	if (attachment != NULL) {
759 		GBinding *binding;
760 		gulong handler_id;
761 
762 		binding = g_object_bind_property (
763 			attachment, "can-show",
764 			button, "expandable",
765 			G_BINDING_BIDIRECTIONAL |
766 			G_BINDING_SYNC_CREATE);
767 		button->priv->can_show_binding = binding;
768 
769 		binding = g_object_bind_property (
770 			attachment, "shown",
771 			button, "expanded",
772 			G_BINDING_BIDIRECTIONAL |
773 			G_BINDING_SYNC_CREATE);
774 		button->priv->shown_binding = binding;
775 
776 		handler_id = g_signal_connect_swapped (
777 			attachment, "notify::reference",
778 			G_CALLBACK (attachment_button_update_cell_view),
779 			button);
780 		button->priv->reference_handler_id = handler_id;
781 
782 		attachment_button_update_cell_view (button);
783 		attachment_button_update_pixbufs (button);
784 	}
785 
786 	/* update drag sources */
787 	list = gtk_target_list_new (NULL, 0);
788 	gtk_target_list_add_uri_targets (list, 0);
789 
790 	if (attachment) {
791 		gchar *simple_type;
792 
793 		simple_type = e_attachment_get_mime_type (attachment);
794 		if (simple_type) {
795 			GtkTargetEntry attach_entry[] = { { NULL, 0, 2 } };
796 
797 			attach_entry[0].target = simple_type;
798 
799 			gtk_target_list_add_table (
800 				list, attach_entry,
801 				G_N_ELEMENTS (attach_entry));
802 
803 			g_free (simple_type);
804 		}
805 	}
806 
807 	targets = gtk_target_table_new_from_list (list, &n_targets);
808 
809 	gtk_drag_source_set (
810 		button->priv->expand_button, GDK_BUTTON1_MASK,
811 		targets, n_targets, GDK_ACTION_COPY);
812 
813 	gtk_drag_source_set (
814 		button->priv->toggle_button, GDK_BUTTON1_MASK,
815 		targets, n_targets, GDK_ACTION_COPY);
816 
817 	gtk_target_table_free (targets, n_targets);
818 	gtk_target_list_unref (list);
819 
820 	g_object_notify (G_OBJECT (button), "attachment");
821 }
822 
823 gboolean
824 e_attachment_button_get_expandable (EAttachmentButton *button)
825 {
826 	g_return_val_if_fail (E_IS_ATTACHMENT_BUTTON (button), FALSE);
827 
828 	return button->priv->expandable;
829 }
830 
831 void
832 e_attachment_button_set_expandable (EAttachmentButton *button,
833                                     gboolean expandable)
834 {
835 	g_return_if_fail (E_IS_ATTACHMENT_BUTTON (button));
836 
837 	if (button->priv->expandable == expandable)
838 		return;
839 
840 	button->priv->expandable = expandable;
841 
842 	if (!expandable)
843 		e_attachment_button_set_expanded (button, FALSE);
844 
845 	g_object_notify (G_OBJECT (button), "expandable");
846 }
847 
848 gboolean
849 e_attachment_button_get_expanded (EAttachmentButton *button)
850 {
851 	g_return_val_if_fail (E_IS_ATTACHMENT_BUTTON (button), FALSE);
852 
853 	return button->priv->expanded;
854 }
855 
856 void
857 e_attachment_button_set_expanded (EAttachmentButton *button,
858                                   gboolean expanded)
859 {
860 	g_return_if_fail (E_IS_ATTACHMENT_BUTTON (button));
861 
862 	if (button->priv->expanded == expanded)
863 		return;
864 
865 	button->priv->expanded = expanded;
866 
867 	g_object_notify (G_OBJECT (button), "expanded");
868 }