evolution-3.6.4/widgets/misc/e-attachment-icon-view.c

No issues found

  1 /*
  2  * e-attachment-icon-view.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 #ifdef HAVE_CONFIG_H
 23 #include <config.h>
 24 #endif
 25 
 26 #include "e-attachment-icon-view.h"
 27 
 28 #include <glib/gi18n.h>
 29 #include <libebackend/libebackend.h>
 30 
 31 #include "e-attachment.h"
 32 #include "e-attachment-store.h"
 33 #include "e-attachment-view.h"
 34 
 35 #define E_ATTACHMENT_ICON_VIEW_GET_PRIVATE(obj) \
 36 	(G_TYPE_INSTANCE_GET_PRIVATE \
 37 	((obj), E_TYPE_ATTACHMENT_ICON_VIEW, EAttachmentIconViewPrivate))
 38 
 39 struct _EAttachmentIconViewPrivate {
 40 	EAttachmentViewPrivate view_priv;
 41 };
 42 
 43 enum {
 44 	PROP_0,
 45 	PROP_DRAGGING,
 46 	PROP_EDITABLE
 47 };
 48 
 49 static gint icon_size = GTK_ICON_SIZE_DIALOG;
 50 
 51 /* Forward Declarations */
 52 static void	e_attachment_icon_view_interface_init
 53 					(EAttachmentViewInterface *interface);
 54 
 55 G_DEFINE_TYPE_WITH_CODE (
 56 	EAttachmentIconView,
 57 	e_attachment_icon_view,
 58 	GTK_TYPE_ICON_VIEW,
 59 	G_IMPLEMENT_INTERFACE (
 60 		E_TYPE_ATTACHMENT_VIEW,
 61 		e_attachment_icon_view_interface_init)
 62 	G_IMPLEMENT_INTERFACE (
 63 		E_TYPE_EXTENSIBLE, NULL))
 64 
 65 void
 66 e_attachment_icon_view_set_default_icon_size (gint size)
 67 {
 68 	icon_size = size;
 69 }
 70 
 71 static void
 72 attachment_icon_view_set_property (GObject *object,
 73                                    guint property_id,
 74                                    const GValue *value,
 75                                    GParamSpec *pspec)
 76 {
 77 	switch (property_id) {
 78 		case PROP_DRAGGING:
 79 			e_attachment_view_set_dragging (
 80 				E_ATTACHMENT_VIEW (object),
 81 				g_value_get_boolean (value));
 82 			return;
 83 
 84 		case PROP_EDITABLE:
 85 			e_attachment_view_set_editable (
 86 				E_ATTACHMENT_VIEW (object),
 87 				g_value_get_boolean (value));
 88 			return;
 89 	}
 90 
 91 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 92 }
 93 
 94 static void
 95 attachment_icon_view_get_property (GObject *object,
 96                                    guint property_id,
 97                                    GValue *value,
 98                                    GParamSpec *pspec)
 99 {
100 	switch (property_id) {
101 		case PROP_DRAGGING:
102 			g_value_set_boolean (
103 				value, e_attachment_view_get_dragging (
104 				E_ATTACHMENT_VIEW (object)));
105 			return;
106 
107 		case PROP_EDITABLE:
108 			g_value_set_boolean (
109 				value, e_attachment_view_get_editable (
110 				E_ATTACHMENT_VIEW (object)));
111 			return;
112 	}
113 
114 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 }
116 
117 static void
118 attachment_icon_view_dispose (GObject *object)
119 {
120 	e_attachment_view_dispose (E_ATTACHMENT_VIEW (object));
121 
122 	/* Chain up to parent's dispose() method. */
123 	G_OBJECT_CLASS (e_attachment_icon_view_parent_class)->dispose (object);
124 }
125 
126 static void
127 attachment_icon_view_finalize (GObject *object)
128 {
129 	e_attachment_view_finalize (E_ATTACHMENT_VIEW (object));
130 
131 	/* Chain up to parent's finalize() method. */
132 	G_OBJECT_CLASS (e_attachment_icon_view_parent_class)->finalize (object);
133 }
134 
135 static void
136 attachment_icon_view_constructed (GObject *object)
137 {
138 	GtkCellLayout *cell_layout;
139 	GtkCellRenderer *renderer;
140 
141 	cell_layout = GTK_CELL_LAYOUT (object);
142 
143 	/* This needs to happen after constructor properties are set
144 	 * so that GtkCellLayout.get_area() returns something valid. */
145 
146 	renderer = gtk_cell_renderer_pixbuf_new ();
147 	g_object_set (renderer, "stock-size", icon_size, NULL);
148 	gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
149 
150 	gtk_cell_layout_add_attribute (
151 		cell_layout, renderer, "gicon",
152 		E_ATTACHMENT_STORE_COLUMN_ICON);
153 
154 	renderer = gtk_cell_renderer_text_new ();
155 	g_object_set (
156 		renderer, "alignment", PANGO_ALIGN_CENTER,
157 		"wrap-mode", PANGO_WRAP_WORD, "wrap-width", 150,
158 		"yalign", 0.0, NULL);
159 	gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
160 
161 	gtk_cell_layout_add_attribute (
162 		cell_layout, renderer, "text",
163 		E_ATTACHMENT_STORE_COLUMN_CAPTION);
164 
165 	renderer = gtk_cell_renderer_progress_new ();
166 	g_object_set (renderer, "text", _("Loading"), NULL);
167 	gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
168 
169 	gtk_cell_layout_add_attribute (
170 		cell_layout, renderer, "value",
171 		E_ATTACHMENT_STORE_COLUMN_PERCENT);
172 
173 	gtk_cell_layout_add_attribute (
174 		cell_layout, renderer, "visible",
175 		E_ATTACHMENT_STORE_COLUMN_LOADING);
176 
177 	renderer = gtk_cell_renderer_progress_new ();
178 	g_object_set (renderer, "text", _("Saving"), NULL);
179 	gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
180 
181 	gtk_cell_layout_add_attribute (
182 		cell_layout, renderer, "value",
183 		E_ATTACHMENT_STORE_COLUMN_PERCENT);
184 
185 	gtk_cell_layout_add_attribute (
186 		cell_layout, renderer, "visible",
187 		E_ATTACHMENT_STORE_COLUMN_SAVING);
188 
189 	e_extensible_load_extensions (E_EXTENSIBLE (object));
190 }
191 
192 static gboolean
193 attachment_icon_view_button_press_event (GtkWidget *widget,
194                                          GdkEventButton *event)
195 {
196 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
197 
198 	if (e_attachment_view_button_press_event (view, event))
199 		return TRUE;
200 
201 	/* Chain up to parent's button_press_event() method. */
202 	return GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
203 		button_press_event (widget, event);
204 }
205 
206 static gboolean
207 attachment_icon_view_button_release_event (GtkWidget *widget,
208                                            GdkEventButton *event)
209 {
210 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
211 
212 	if (e_attachment_view_button_release_event (view, event))
213 		return TRUE;
214 
215 	/* Chain up to parent's button_release_event() method. */
216 	return GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
217 		button_release_event (widget, event);
218 }
219 
220 static gboolean
221 attachment_icon_view_motion_notify_event (GtkWidget *widget,
222                                           GdkEventMotion *event)
223 {
224 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
225 
226 	if (e_attachment_view_motion_notify_event (view, event))
227 		return TRUE;
228 
229 	/* Chain up to parent's motion_notify_event() method. */
230 	return GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
231 		motion_notify_event (widget, event);
232 }
233 
234 static gboolean
235 attachment_icon_view_key_press_event (GtkWidget *widget,
236                                       GdkEventKey *event)
237 {
238 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
239 
240 	if (e_attachment_view_key_press_event (view, event))
241 		return TRUE;
242 
243 	/* Chain up to parent's key_press_event() method. */
244 	return GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
245 		key_press_event (widget, event);
246 }
247 
248 static void
249 attachment_icon_view_drag_begin (GtkWidget *widget,
250                                  GdkDragContext *context)
251 {
252 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
253 
254 	/* Chain up to parent's drag_begin() method. */
255 	GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
256 		drag_begin (widget, context);
257 
258 	e_attachment_view_drag_begin (view, context);
259 }
260 
261 static void
262 attachment_icon_view_drag_end (GtkWidget *widget,
263                                GdkDragContext *context)
264 {
265 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
266 
267 	/* Chain up to parent's drag_end() method. */
268 	GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
269 		drag_end (widget, context);
270 
271 	e_attachment_view_drag_end (view, context);
272 }
273 
274 static void
275 attachment_icon_view_drag_data_get (GtkWidget *widget,
276                                     GdkDragContext *context,
277                                     GtkSelectionData *selection,
278                                     guint info,
279                                     guint time)
280 {
281 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
282 
283 	e_attachment_view_drag_data_get (
284 		view, context, selection, info, time);
285 }
286 
287 static gboolean
288 attachment_icon_view_drag_motion (GtkWidget *widget,
289                                   GdkDragContext *context,
290                                   gint x,
291                                   gint y,
292                                   guint time)
293 {
294 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
295 
296 	return e_attachment_view_drag_motion (view, context, x, y, time);
297 }
298 
299 static gboolean
300 attachment_icon_view_drag_drop (GtkWidget *widget,
301                                 GdkDragContext *context,
302                                 gint x,
303                                 gint y,
304                                 guint time)
305 {
306 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
307 
308 	if (!e_attachment_view_drag_drop (view, context, x, y, time))
309 		return FALSE;
310 
311 	/* Chain up to parent's drag_drop() method. */
312 	return GTK_WIDGET_CLASS (e_attachment_icon_view_parent_class)->
313 		drag_drop (widget, context, x, y, time);
314 }
315 
316 static void
317 attachment_icon_view_drag_data_received (GtkWidget *widget,
318                                          GdkDragContext *context,
319                                          gint x,
320                                          gint y,
321                                          GtkSelectionData *selection,
322                                          guint info,
323                                          guint time)
324 {
325 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
326 
327 	e_attachment_view_drag_data_received (
328 		view, context, x, y, selection, info, time);
329 }
330 
331 static gboolean
332 attachment_icon_view_popup_menu (GtkWidget *widget)
333 {
334 	EAttachmentView *view = E_ATTACHMENT_VIEW (widget);
335 
336 	e_attachment_view_show_popup_menu (view, NULL, NULL, NULL);
337 
338 	return TRUE;
339 }
340 
341 static void
342 attachment_icon_view_item_activated (GtkIconView *icon_view,
343                                      GtkTreePath *path)
344 {
345 	EAttachmentView *view = E_ATTACHMENT_VIEW (icon_view);
346 
347 	e_attachment_view_open_path (view, path, NULL);
348 }
349 
350 static EAttachmentViewPrivate *
351 attachment_icon_view_get_private (EAttachmentView *view)
352 {
353 	EAttachmentIconViewPrivate *priv;
354 
355 	priv = E_ATTACHMENT_ICON_VIEW_GET_PRIVATE (view);
356 
357 	return &priv->view_priv;
358 }
359 
360 static EAttachmentStore *
361 attachment_icon_view_get_store (EAttachmentView *view)
362 {
363 	GtkIconView *icon_view;
364 	GtkTreeModel *model;
365 
366 	icon_view = GTK_ICON_VIEW (view);
367 	model = gtk_icon_view_get_model (icon_view);
368 
369 	return E_ATTACHMENT_STORE (model);
370 }
371 
372 static GtkTreePath *
373 attachment_icon_view_get_path_at_pos (EAttachmentView *view,
374                                       gint x,
375                                       gint y)
376 {
377 	GtkIconView *icon_view;
378 
379 	icon_view = GTK_ICON_VIEW (view);
380 
381 	return gtk_icon_view_get_path_at_pos (icon_view, x, y);
382 }
383 
384 static GList *
385 attachment_icon_view_get_selected_paths (EAttachmentView *view)
386 {
387 	GtkIconView *icon_view;
388 
389 	icon_view = GTK_ICON_VIEW (view);
390 
391 	return gtk_icon_view_get_selected_items (icon_view);
392 }
393 
394 static gboolean
395 attachment_icon_view_path_is_selected (EAttachmentView *view,
396                                        GtkTreePath *path)
397 {
398 	GtkIconView *icon_view;
399 
400 	icon_view = GTK_ICON_VIEW (view);
401 
402 	return gtk_icon_view_path_is_selected (icon_view, path);
403 }
404 
405 static void
406 attachment_icon_view_select_path (EAttachmentView *view,
407                                   GtkTreePath *path)
408 {
409 	GtkIconView *icon_view;
410 
411 	icon_view = GTK_ICON_VIEW (view);
412 
413 	gtk_icon_view_select_path (icon_view, path);
414 }
415 
416 static void
417 attachment_icon_view_unselect_path (EAttachmentView *view,
418                                     GtkTreePath *path)
419 {
420 	GtkIconView *icon_view;
421 
422 	icon_view = GTK_ICON_VIEW (view);
423 
424 	gtk_icon_view_unselect_path (icon_view, path);
425 }
426 
427 static void
428 attachment_icon_view_select_all (EAttachmentView *view)
429 {
430 	GtkIconView *icon_view;
431 
432 	icon_view = GTK_ICON_VIEW (view);
433 
434 	gtk_icon_view_select_all (icon_view);
435 }
436 
437 static void
438 attachment_icon_view_unselect_all (EAttachmentView *view)
439 {
440 	GtkIconView *icon_view;
441 
442 	icon_view = GTK_ICON_VIEW (view);
443 
444 	gtk_icon_view_unselect_all (icon_view);
445 }
446 
447 static void
448 attachment_icon_view_drag_source_set (EAttachmentView *view,
449                                       GdkModifierType start_button_mask,
450                                       const GtkTargetEntry *targets,
451                                       gint n_targets,
452                                       GdkDragAction actions)
453 {
454 	GtkIconView *icon_view;
455 
456 	icon_view = GTK_ICON_VIEW (view);
457 
458 	gtk_icon_view_enable_model_drag_source (
459 		icon_view, start_button_mask, targets, n_targets, actions);
460 }
461 
462 static void
463 attachment_icon_view_drag_dest_set (EAttachmentView *view,
464                                     const GtkTargetEntry *targets,
465                                     gint n_targets,
466                                     GdkDragAction actions)
467 {
468 	GtkIconView *icon_view;
469 
470 	icon_view = GTK_ICON_VIEW (view);
471 
472 	gtk_icon_view_enable_model_drag_dest (
473 		icon_view, targets, n_targets, actions);
474 }
475 
476 static void
477 attachment_icon_view_drag_source_unset (EAttachmentView *view)
478 {
479 	GtkIconView *icon_view;
480 
481 	icon_view = GTK_ICON_VIEW (view);
482 
483 	gtk_icon_view_unset_model_drag_source (icon_view);
484 }
485 
486 static void
487 attachment_icon_view_drag_dest_unset (EAttachmentView *view)
488 {
489 	GtkIconView *icon_view;
490 
491 	icon_view = GTK_ICON_VIEW (view);
492 
493 	gtk_icon_view_unset_model_drag_dest (icon_view);
494 }
495 
496 static void
497 e_attachment_icon_view_class_init (EAttachmentIconViewClass *class)
498 {
499 	GObjectClass *object_class;
500 	GtkWidgetClass *widget_class;
501 	GtkIconViewClass *icon_view_class;
502 
503 	g_type_class_add_private (class, sizeof (EAttachmentViewPrivate));
504 
505 	object_class = G_OBJECT_CLASS (class);
506 	object_class->set_property = attachment_icon_view_set_property;
507 	object_class->get_property = attachment_icon_view_get_property;
508 	object_class->dispose = attachment_icon_view_dispose;
509 	object_class->finalize = attachment_icon_view_finalize;
510 	object_class->constructed = attachment_icon_view_constructed;
511 
512 	widget_class = GTK_WIDGET_CLASS (class);
513 	widget_class->button_press_event = attachment_icon_view_button_press_event;
514 	widget_class->button_release_event = attachment_icon_view_button_release_event;
515 	widget_class->motion_notify_event = attachment_icon_view_motion_notify_event;
516 	widget_class->key_press_event = attachment_icon_view_key_press_event;
517 	widget_class->drag_begin = attachment_icon_view_drag_begin;
518 	widget_class->drag_end = attachment_icon_view_drag_end;
519 	widget_class->drag_data_get = attachment_icon_view_drag_data_get;
520 	widget_class->drag_motion = attachment_icon_view_drag_motion;
521 	widget_class->drag_drop = attachment_icon_view_drag_drop;
522 	widget_class->drag_data_received = attachment_icon_view_drag_data_received;
523 	widget_class->popup_menu = attachment_icon_view_popup_menu;
524 
525 	icon_view_class = GTK_ICON_VIEW_CLASS (class);
526 	icon_view_class->item_activated = attachment_icon_view_item_activated;
527 
528 	g_object_class_override_property (
529 		object_class, PROP_DRAGGING, "dragging");
530 
531 	g_object_class_override_property (
532 		object_class, PROP_EDITABLE, "editable");
533 }
534 
535 static void
536 e_attachment_icon_view_init (EAttachmentIconView *icon_view)
537 {
538 	icon_view->priv = E_ATTACHMENT_ICON_VIEW_GET_PRIVATE (icon_view);
539 
540 	e_attachment_view_init (E_ATTACHMENT_VIEW (icon_view));
541 
542 	gtk_icon_view_set_selection_mode (
543 		GTK_ICON_VIEW (icon_view), GTK_SELECTION_MULTIPLE);
544 }
545 
546 static void
547 e_attachment_icon_view_interface_init (EAttachmentViewInterface *interface)
548 {
549 	interface->get_private = attachment_icon_view_get_private;
550 	interface->get_store = attachment_icon_view_get_store;
551 
552 	interface->get_path_at_pos = attachment_icon_view_get_path_at_pos;
553 	interface->get_selected_paths = attachment_icon_view_get_selected_paths;
554 	interface->path_is_selected = attachment_icon_view_path_is_selected;
555 	interface->select_path = attachment_icon_view_select_path;
556 	interface->unselect_path = attachment_icon_view_unselect_path;
557 	interface->select_all = attachment_icon_view_select_all;
558 	interface->unselect_all = attachment_icon_view_unselect_all;
559 
560 	interface->drag_source_set = attachment_icon_view_drag_source_set;
561 	interface->drag_dest_set = attachment_icon_view_drag_dest_set;
562 	interface->drag_source_unset = attachment_icon_view_drag_source_unset;
563 	interface->drag_dest_unset = attachment_icon_view_drag_dest_unset;
564 }
565 
566 GtkWidget *
567 e_attachment_icon_view_new (void)
568 {
569 	return g_object_new (E_TYPE_ATTACHMENT_ICON_VIEW, NULL);
570 }