~ubuntu-branches/ubuntu/quantal/gnome-documents/quantal

« back to all changes in this revision

Viewing changes to libgd/libgd/gd-main-list-view.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-08-22 10:01:18 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20120822100118-3837rqfy72e1op72
Tags: 3.5.90-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011 Red Hat, Inc.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 * Author: Cosimo Cecchi <cosimoc@redhat.com>
 
19
 *
 
20
 */
 
21
 
 
22
#include "gd-main-list-view.h"
 
23
#include "gd-main-view-generic.h"
 
24
#include "gd-two-lines-renderer.h"
 
25
 
 
26
#include <glib/gi18n.h>
 
27
 
 
28
struct _GdMainListViewPrivate {
 
29
  GtkTreeViewColumn *tree_col;
 
30
  GtkCellRenderer *selection_cell;
 
31
 
 
32
  gboolean selection_mode;
 
33
};
 
34
 
 
35
static void gd_main_view_generic_iface_init (GdMainViewGenericIface *iface);
 
36
G_DEFINE_TYPE_WITH_CODE (GdMainListView, gd_main_list_view, GTK_TYPE_TREE_VIEW,
 
37
                         G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_VIEW_GENERIC,
 
38
                                                gd_main_view_generic_iface_init))
 
39
 
 
40
static GtkTreePath*
 
41
get_source_row (GdkDragContext *context)
 
42
{
 
43
  GtkTreeRowReference *ref =
 
44
    g_object_get_data (G_OBJECT (context), "gtk-tree-view-source-row");
 
45
 
 
46
  if (ref)
 
47
    return gtk_tree_row_reference_get_path (ref);
 
48
  else
 
49
    return NULL;
 
50
}
 
51
 
 
52
static void
 
53
gd_main_list_view_drag_data_get (GtkWidget *widget,
 
54
                                 GdkDragContext *drag_context,
 
55
                                 GtkSelectionData *data,
 
56
                                 guint info,
 
57
                                 guint time)
 
58
{
 
59
  GdMainListView *self = GD_MAIN_LIST_VIEW (widget);
 
60
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
 
61
 
 
62
  if (info != 0)
 
63
    return;
 
64
 
 
65
  _gd_main_view_generic_dnd_common (model,
 
66
                                    self->priv->selection_mode,
 
67
                                    get_source_row (drag_context), data);
 
68
 
 
69
  GTK_WIDGET_CLASS (gd_main_list_view_parent_class)->drag_data_get (widget, drag_context,
 
70
                                                                    data, info, time);
 
71
}
 
72
 
 
73
static void
 
74
gd_main_list_view_constructed (GObject *obj)
 
75
{
 
76
  GdMainListView *self = GD_MAIN_LIST_VIEW (obj);
 
77
  GtkCellRenderer *cell;
 
78
  GtkTreeSelection *selection;
 
79
  const GtkTargetEntry targets[] = {
 
80
    { "text/uri-list", GTK_TARGET_OTHER_APP, 0 }
 
81
  };
 
82
 
 
83
  G_OBJECT_CLASS (gd_main_list_view_parent_class)->constructed (obj);
 
84
 
 
85
  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
 
86
  gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
 
87
 
 
88
  g_object_set (self,
 
89
                "headers-visible", FALSE,
 
90
                "enable-search", FALSE,
 
91
                NULL);
 
92
 
 
93
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
 
94
  gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
 
95
 
 
96
  self->priv->tree_col = gtk_tree_view_column_new ();
 
97
  gtk_tree_view_append_column (GTK_TREE_VIEW (self), self->priv->tree_col);
 
98
 
 
99
  self->priv->selection_cell = cell = gtk_cell_renderer_toggle_new ();
 
100
  g_object_set (cell, 
 
101
                "visible", FALSE,
 
102
                "xpad", 12,
 
103
                "xalign", 1.0,
 
104
                NULL);
 
105
  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, FALSE);
 
106
  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
 
107
                                      "active", GD_MAIN_COLUMN_SELECTED);
 
108
 
 
109
  cell = gtk_cell_renderer_pixbuf_new ();
 
110
  g_object_set (cell,
 
111
                "xalign", 0.5,
 
112
                "yalign", 0.5,
 
113
                "xpad", 12,
 
114
                "ypad", 2,
 
115
                NULL);
 
116
  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, FALSE);
 
117
  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
 
118
                                      "pixbuf", GD_MAIN_COLUMN_ICON);
 
119
 
 
120
  cell = gd_two_lines_renderer_new ();
 
121
  g_object_set (cell,
 
122
                "xalign", 0.0,
 
123
                "wrap-mode", PANGO_WRAP_WORD_CHAR,
 
124
                "xpad", 12,
 
125
                "text-lines", 2,
 
126
                NULL);
 
127
  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, TRUE);
 
128
  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
 
129
                                      "text", GD_MAIN_COLUMN_PRIMARY_TEXT);
 
130
  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
 
131
                                      "line-two", GD_MAIN_COLUMN_SECONDARY_TEXT);
 
132
 
 
133
  gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (self),
 
134
                                          GDK_BUTTON1_MASK,
 
135
                                          targets, 1,
 
136
                                          GDK_ACTION_COPY);
 
137
}
 
138
 
 
139
static void
 
140
gd_main_list_view_class_init (GdMainListViewClass *klass)
 
141
{
 
142
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
143
  GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
 
144
 
 
145
  oclass->constructed = gd_main_list_view_constructed;
 
146
  wclass->drag_data_get = gd_main_list_view_drag_data_get;
 
147
 
 
148
  g_type_class_add_private (klass, sizeof (GdMainListViewPrivate));
 
149
}
 
150
 
 
151
static void
 
152
gd_main_list_view_init (GdMainListView *self)
 
153
{
 
154
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GD_TYPE_MAIN_LIST_VIEW, GdMainListViewPrivate);
 
155
}
 
156
 
 
157
static GtkTreePath *
 
158
gd_main_list_view_get_path_at_pos (GdMainViewGeneric *mv,
 
159
                                   gint x,
 
160
                                   gint y)
 
161
{
 
162
  GtkTreePath *path = NULL;
 
163
 
 
164
  gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (mv), x, y, &path,
 
165
                                 NULL, NULL, NULL);
 
166
 
 
167
  return path;
 
168
}
 
169
 
 
170
static void
 
171
gd_main_list_view_set_selection_mode (GdMainViewGeneric *mv,
 
172
                                      gboolean selection_mode)
 
173
{
 
174
  GdMainListView *self = GD_MAIN_LIST_VIEW (mv);
 
175
 
 
176
  self->priv->selection_mode = selection_mode;
 
177
 
 
178
  g_object_set (self->priv->selection_cell,
 
179
                "visible", selection_mode,
 
180
                NULL);
 
181
  gtk_tree_view_column_queue_resize (self->priv->tree_col);
 
182
}
 
183
 
 
184
static void
 
185
gd_main_list_view_scroll_to_path (GdMainViewGeneric *mv,
 
186
                                  GtkTreePath *path)
 
187
{
 
188
  gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (mv), path, NULL, TRUE, 0.5, 0.5);
 
189
}
 
190
 
 
191
static void
 
192
gd_main_list_view_set_model (GdMainViewGeneric *mv,
 
193
                             GtkTreeModel *model)
 
194
{
 
195
  gtk_tree_view_set_model (GTK_TREE_VIEW (mv), model);
 
196
}
 
197
 
 
198
static void
 
199
gd_main_view_generic_iface_init (GdMainViewGenericIface *iface)
 
200
{
 
201
  iface->set_model = gd_main_list_view_set_model;
 
202
  iface->get_path_at_pos = gd_main_list_view_get_path_at_pos;
 
203
  iface->scroll_to_path = gd_main_list_view_scroll_to_path;
 
204
  iface->set_selection_mode = gd_main_list_view_set_selection_mode;
 
205
}
 
206
 
 
207
void
 
208
gd_main_list_view_add_renderer (GdMainListView *self,
 
209
                                GtkCellRenderer *renderer,
 
210
                                GtkTreeCellDataFunc func,
 
211
                                gpointer user_data,
 
212
                                GDestroyNotify destroy)
 
213
{
 
214
  gtk_tree_view_column_pack_start (self->priv->tree_col, renderer, FALSE);
 
215
  gtk_tree_view_column_set_cell_data_func (self->priv->tree_col, renderer,
 
216
                                           func, user_data, destroy);
 
217
}
 
218
 
 
219
GtkWidget *
 
220
gd_main_list_view_new (void)
 
221
{
 
222
  return g_object_new (GD_TYPE_MAIN_LIST_VIEW, NULL);
 
223
}