2
* Copyright (c) 2011 Red Hat, Inc.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Author: Cosimo Cecchi <cosimoc@redhat.com>
22
#include "gd-main-icon-view.h"
23
#include "gd-main-view-generic.h"
24
#include "gd-toggle-pixbuf-renderer.h"
25
#include "gd-two-lines-renderer.h"
28
#include <glib/gi18n.h>
30
#define VIEW_ITEM_WIDTH 140
31
#define VIEW_ITEM_WRAP_WIDTH 128
32
#define VIEW_COLUMN_SPACING 20
33
#define VIEW_MARGIN 16
35
struct _GdMainIconViewPrivate {
36
GtkCellRenderer *pixbuf_cell;
37
gboolean selection_mode;
40
static void gd_main_view_generic_iface_init (GdMainViewGenericIface *iface);
41
G_DEFINE_TYPE_WITH_CODE (GdMainIconView, gd_main_icon_view, GTK_TYPE_ICON_VIEW,
42
G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_VIEW_GENERIC,
43
gd_main_view_generic_iface_init))
46
get_source_row (GdkDragContext *context)
48
GtkTreeRowReference *ref;
50
ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
53
return gtk_tree_row_reference_get_path (ref);
59
gd_main_icon_view_drag_data_get (GtkWidget *widget,
60
GdkDragContext *drag_context,
61
GtkSelectionData *data,
65
GdMainIconView *self = GD_MAIN_ICON_VIEW (widget);
66
GtkTreeModel *model = gtk_icon_view_get_model (GTK_ICON_VIEW (self));
71
_gd_main_view_generic_dnd_common (model, self->priv->selection_mode,
72
get_source_row (drag_context), data);
74
GTK_WIDGET_CLASS (gd_main_icon_view_parent_class)->drag_data_get (widget, drag_context,
79
gd_main_icon_view_constructed (GObject *obj)
81
GdMainIconView *self = GD_MAIN_ICON_VIEW (obj);
82
GtkCellRenderer *cell;
83
const GtkTargetEntry targets[] = {
84
{ (char *) "text/uri-list", GTK_TARGET_OTHER_APP, 0 }
87
G_OBJECT_CLASS (gd_main_icon_view_parent_class)->constructed (obj);
89
gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
90
gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
91
gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (self), GTK_SELECTION_NONE);
94
"column-spacing", VIEW_COLUMN_SPACING,
95
"margin", VIEW_MARGIN,
98
self->priv->pixbuf_cell = cell = gd_toggle_pixbuf_renderer_new ();
104
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
105
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell,
106
"active", GD_MAIN_COLUMN_SELECTED);
107
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell,
108
"pixbuf", GD_MAIN_COLUMN_ICON);
110
cell = gd_two_lines_renderer_new ();
113
"alignment", PANGO_ALIGN_CENTER,
114
"wrap-mode", PANGO_WRAP_WORD_CHAR,
115
"wrap-width", VIEW_ITEM_WRAP_WIDTH,
118
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
119
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell,
120
"text", GD_MAIN_COLUMN_PRIMARY_TEXT);
121
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell,
122
"line-two", GD_MAIN_COLUMN_SECONDARY_TEXT);
124
gtk_icon_view_enable_model_drag_source (GTK_ICON_VIEW (self),
131
path_from_line_rects (cairo_t *cr,
135
int start_line, end_line;
139
/* Join rows vertically by extending to the middle */
140
for (i = 0; i < n_lines - 1; i++)
142
GdkRectangle *r1 = &lines[i];
143
GdkRectangle *r2 = &lines[i+1];
144
int gap = r2->y - (r1->y + r1->height);
147
r1->height += gap / 2;
149
r2->y = r1->y + r1->height;
150
r2->height += old_y - r2->y;
158
for (i = start_line; i < n_lines; i++)
162
cairo_move_to (cr, r->x + r->width, r->y);
164
cairo_line_to (cr, r->x + r->width, r->y);
165
cairo_line_to (cr, r->x + r->width, r->y + r->height);
167
if (i < n_lines - 1 &&
168
(r->x + r->width < lines[i+1].x ||
169
r->x > lines[i+1].x + lines[i+1].width))
176
for (i = end_line - 1; i >= start_line; i--)
179
cairo_line_to (cr, r->x, r->y + r->height);
180
cairo_line_to (cr, r->x, r->y);
182
cairo_close_path (cr);
183
start_line = end_line;
185
while (end_line < n_lines);
189
gd_main_icon_view_draw (GtkWidget *widget,
192
GdMainIconView *self = GD_MAIN_ICON_VIEW (widget);
193
GtkAllocation allocation;
194
GtkStyleContext *context;
195
GdkRectangle line_rect;
199
GtkTreePath *rubberband_start, *rubberband_end;
201
GTK_WIDGET_CLASS (gd_main_icon_view_parent_class)->draw (widget, cr);
203
_gd_main_view_generic_get_rubberband_range (GD_MAIN_VIEW_GENERIC (self),
204
&rubberband_start, &rubberband_end);
206
if (rubberband_start)
210
context = gtk_widget_get_style_context (widget);
212
gtk_style_context_save (context);
213
gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
215
path = gtk_tree_path_copy (rubberband_start);
218
lines = g_array_new (FALSE, FALSE, sizeof (GdkRectangle));
220
while (gtk_tree_path_compare (path, rubberband_end) <= 0)
222
if (gtk_icon_view_get_cell_rect (GTK_ICON_VIEW (widget),
226
if (line_rect.width == 0)
230
if (rect.y == line_rect.y)
231
gdk_rectangle_union (&rect, &line_rect, &line_rect);
234
g_array_append_val (lines, line_rect);
239
gtk_tree_path_next (path);
242
if (line_rect.width != 0)
243
g_array_append_val (lines, line_rect);
250
GdkRGBA border_color;
252
path_from_line_rects (cr, (GdkRectangle *)lines->data, lines->len);
254
/* For some reason we need to copy and reapply the path, or it gets
255
eaten by gtk_render_background() */
256
path = cairo_copy_path (cr);
260
gtk_widget_get_allocation (widget, &allocation);
261
gtk_render_background (context, cr,
263
allocation.width, allocation.height);
266
cairo_append_path (cr, path);
267
cairo_path_destroy (path);
269
state = gtk_widget_get_state_flags (widget);
270
gtk_style_context_get_border_color (context,
273
gtk_style_context_get_border (context, state,
276
cairo_set_line_width (cr, border.left);
277
gdk_cairo_set_source_rgba (cr, &border_color);
280
g_array_free (lines, TRUE);
282
gtk_tree_path_free (path);
284
gtk_style_context_restore (context);
292
gd_main_icon_view_class_init (GdMainIconViewClass *klass)
294
GObjectClass *oclass = G_OBJECT_CLASS (klass);
295
GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
296
GtkBindingSet *binding_set;
297
GdkModifierType activate_modifiers[] = { GDK_SHIFT_MASK, GDK_CONTROL_MASK, GDK_SHIFT_MASK | GDK_CONTROL_MASK };
300
binding_set = gtk_binding_set_by_class (klass);
302
oclass->constructed = gd_main_icon_view_constructed;
303
wclass->drag_data_get = gd_main_icon_view_drag_data_get;
304
wclass->draw = gd_main_icon_view_draw;
306
gtk_widget_class_install_style_property (wclass,
307
g_param_spec_int ("check-icon-size",
313
g_type_class_add_private (klass, sizeof (GdMainIconViewPrivate));
316
for (i = 0; i < G_N_ELEMENTS (activate_modifiers); i++)
318
gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, activate_modifiers[i],
319
"activate-cursor-item", 0);
320
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, activate_modifiers[i],
321
"activate-cursor-item", 0);
322
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, activate_modifiers[i],
323
"activate-cursor-item", 0);
324
gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, activate_modifiers[i],
325
"activate-cursor-item", 0);
326
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, activate_modifiers[i],
327
"activate-cursor-item", 0);
332
gd_main_icon_view_init (GdMainIconView *self)
334
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GD_TYPE_MAIN_ICON_VIEW, GdMainIconViewPrivate);
338
gd_main_icon_view_get_path_at_pos (GdMainViewGeneric *mv,
342
return gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (mv), x, y);
346
gd_main_icon_view_set_selection_mode (GdMainViewGeneric *mv,
347
gboolean selection_mode)
349
GdMainIconView *self = GD_MAIN_ICON_VIEW (mv);
351
self->priv->selection_mode = selection_mode;
353
g_object_set (self->priv->pixbuf_cell,
354
"toggle-visible", selection_mode,
356
gtk_widget_queue_draw (GTK_WIDGET (self));
360
gd_main_icon_view_scroll_to_path (GdMainViewGeneric *mv,
363
gtk_icon_view_scroll_to_path (GTK_ICON_VIEW (mv), path, TRUE, 0.5, 0.5);
367
gd_main_icon_view_set_model (GdMainViewGeneric *mv,
370
gtk_icon_view_set_model (GTK_ICON_VIEW (mv), model);
373
static GtkTreeModel *
374
gd_main_icon_view_get_model (GdMainViewGeneric *mv)
376
return gtk_icon_view_get_model (GTK_ICON_VIEW (mv));
380
gd_main_view_generic_iface_init (GdMainViewGenericIface *iface)
382
iface->set_model = gd_main_icon_view_set_model;
383
iface->get_model = gd_main_icon_view_get_model;
384
iface->get_path_at_pos = gd_main_icon_view_get_path_at_pos;
385
iface->scroll_to_path = gd_main_icon_view_scroll_to_path;
386
iface->set_selection_mode = gd_main_icon_view_set_selection_mode;
390
gd_main_icon_view_new (void)
392
return g_object_new (GD_TYPE_MAIN_ICON_VIEW, NULL);