[libgd] list-view: add GdMainListView



commit efca9ac230d35caab4f451e6e2eac2d00cef994d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Aug 21 13:40:50 2012 +0200

    list-view: add GdMainListView

 Makefile.am               |   31 ++++++-
 libgd.m4                  |    6 +
 libgd/gd-main-list-view.c |  223 +++++++++++++++++++++++++++++++++++++++++++++
 libgd/gd-main-list-view.h |   80 ++++++++++++++++
 4 files changed, 336 insertions(+), 4 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 616ad89..4ad3c90 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -40,22 +40,45 @@ nodist_libgd_la_SOURCES += $(gtk_hacks_sources)
 EXTRA_DIST += $(gtk_hacks_sources)
 endif
 
+view_common_sources =				\
+	libgd/gd-main-view-generic.c		\
+	libgd/gd-main-view-generic.h		\
+	libgd/gd-two-lines-renderer.c		\
+	libgd/gd-two-lines-renderer.h		\
+	$(NULL)
+
 if LIBGD_MAIN_ICON_VIEW
 main_icon_view_sources =			\
 	libgd/gd-main-icon-view.c		\
 	libgd/gd-main-icon-view.h		\
-	libgd/gd-main-view-generic.c            \
-	libgd/gd-main-view-generic.h            \
 	libgd/gd-toggle-pixbuf-renderer.c	\
 	libgd/gd-toggle-pixbuf-renderer.h	\
-	libgd/gd-two-lines-renderer.c		\
-	libgd/gd-two-lines-renderer.h		\
 	$(NULL)
 
 nodist_libgd_la_SOURCES += $(main_icon_view_sources)
 EXTRA_DIST += $(main_icon_view_sources)
 endif
 
+if LIBGD_MAIN_LIST_VIEW
+main_list_view_sources =			\
+	libgd/gd-main-list-view.c		\
+	libgd/gd-main-list-view.h		\
+	$(NULL)
+
+nodist_libgd_la_SOURCES += $(main_list_view_sources)
+EXTRA_DIST += $(main_list_view_sources)
+endif
+
+if !LIBGD_MAIN_ICON_VIEW
+  if LIBGD_MAIN_LIST_VIEW
+    nodist_libgd_la_SOURCES += $(view_common_sources)
+    EXTRA_DIST += $(view_common_sources)
+  endif
+else
+  nodist_libgd_la_SOURCES += $(view_common_sources)
+  EXTRA_DIST += $(view_common_sources)
+endif
+
 if LIBGD_MAIN_TOOLBAR
 main_toolbar_sources =				\
 	libgd/gd-main-toolbar.c			\
diff --git a/libgd.m4 b/libgd.m4
index e1faa84..5ccc9e5 100644
--- a/libgd.m4
+++ b/libgd.m4
@@ -56,6 +56,12 @@ AC_DEFUN([LIBGD_INIT], [
         AC_DEFINE([LIBGD_MAIN_ICON_VIEW], [1], [Description])
     ])
 
+    # main-list-view:
+    AM_CONDITIONAL([LIBGD_MAIN_LIST_VIEW],[_LIBGD_IF_OPTION_SET([main-list-view],[true],[false])])
+    _LIBGD_IF_OPTION_SET([main-list-view],[
+        AC_DEFINE([LIBGD_MAIN_LIST_VIEW], [1], [Description])
+    ])
+
     # main-toolbar:
     AM_CONDITIONAL([LIBGD_MAIN_TOOLBAR],[_LIBGD_IF_OPTION_SET([main-toolbar],[true],[false])])
     _LIBGD_IF_OPTION_SET([main-toolbar],[
diff --git a/libgd/gd-main-list-view.c b/libgd/gd-main-list-view.c
new file mode 100644
index 0000000..12eec1b
--- /dev/null
+++ b/libgd/gd-main-list-view.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2011 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by 
+ * the Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License 
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Cosimo Cecchi <cosimoc redhat com>
+ *
+ */
+
+#include "gd-main-list-view.h"
+#include "gd-main-view-generic.h"
+#include "gd-two-lines-renderer.h"
+
+#include <glib/gi18n.h>
+
+struct _GdMainListViewPrivate {
+  GtkTreeViewColumn *tree_col;
+  GtkCellRenderer *selection_cell;
+
+  gboolean selection_mode;
+};
+
+static void gd_main_view_generic_iface_init (GdMainViewGenericIface *iface);
+G_DEFINE_TYPE_WITH_CODE (GdMainListView, gd_main_list_view, GTK_TYPE_TREE_VIEW,
+                         G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_VIEW_GENERIC,
+                                                gd_main_view_generic_iface_init))
+
+static GtkTreePath*
+get_source_row (GdkDragContext *context)
+{
+  GtkTreeRowReference *ref =
+    g_object_get_data (G_OBJECT (context), "gtk-tree-view-source-row");
+
+  if (ref)
+    return gtk_tree_row_reference_get_path (ref);
+  else
+    return NULL;
+}
+
+static void
+gd_main_list_view_drag_data_get (GtkWidget *widget,
+                                 GdkDragContext *drag_context,
+                                 GtkSelectionData *data,
+                                 guint info,
+                                 guint time)
+{
+  GdMainListView *self = GD_MAIN_LIST_VIEW (widget);
+  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+
+  if (info != 0)
+    return;
+
+  _gd_main_view_generic_dnd_common (model,
+                                    self->priv->selection_mode,
+                                    get_source_row (drag_context), data);
+
+  GTK_WIDGET_CLASS (gd_main_list_view_parent_class)->drag_data_get (widget, drag_context,
+                                                                    data, info, time);
+}
+
+static void
+gd_main_list_view_constructed (GObject *obj)
+{
+  GdMainListView *self = GD_MAIN_LIST_VIEW (obj);
+  GtkCellRenderer *cell;
+  GtkTreeSelection *selection;
+  const GtkTargetEntry targets[] = {
+    { "text/uri-list", GTK_TARGET_OTHER_APP, 0 }
+  };
+
+  G_OBJECT_CLASS (gd_main_list_view_parent_class)->constructed (obj);
+
+  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
+  gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
+
+  g_object_set (self,
+                "headers-visible", FALSE,
+                "enable-search", FALSE,
+                NULL);
+
+  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+  gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
+
+  self->priv->tree_col = gtk_tree_view_column_new ();
+  gtk_tree_view_append_column (GTK_TREE_VIEW (self), self->priv->tree_col);
+
+  self->priv->selection_cell = cell = gtk_cell_renderer_toggle_new ();
+  g_object_set (cell, 
+                "visible", FALSE,
+                "xpad", 12,
+                "xalign", 1.0,
+                NULL);
+  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, FALSE);
+  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
+                                      "active", GD_MAIN_COLUMN_SELECTED);
+
+  cell = gtk_cell_renderer_pixbuf_new ();
+  g_object_set (cell,
+                "xalign", 0.5,
+                "yalign", 0.5,
+                "xpad", 12,
+                "ypad", 2,
+                NULL);
+  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, FALSE);
+  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
+                                      "pixbuf", GD_MAIN_COLUMN_ICON);
+
+  cell = gd_two_lines_renderer_new ();
+  g_object_set (cell,
+                "xalign", 0.0,
+                "wrap-mode", PANGO_WRAP_WORD_CHAR,
+                "xpad", 12,
+                "text-lines", 2,
+                NULL);
+  gtk_tree_view_column_pack_start (self->priv->tree_col, cell, TRUE);
+  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
+                                      "text", GD_MAIN_COLUMN_PRIMARY_TEXT);
+  gtk_tree_view_column_add_attribute (self->priv->tree_col, cell,
+                                      "line-two", GD_MAIN_COLUMN_SECONDARY_TEXT);
+
+  gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (self),
+                                          GDK_BUTTON1_MASK,
+                                          targets, 1,
+                                          GDK_ACTION_COPY);
+}
+
+static void
+gd_main_list_view_class_init (GdMainListViewClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
+
+  oclass->constructed = gd_main_list_view_constructed;
+  wclass->drag_data_get = gd_main_list_view_drag_data_get;
+
+  g_type_class_add_private (klass, sizeof (GdMainListViewPrivate));
+}
+
+static void
+gd_main_list_view_init (GdMainListView *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GD_TYPE_MAIN_LIST_VIEW, GdMainListViewPrivate);
+}
+
+static GtkTreePath *
+gd_main_list_view_get_path_at_pos (GdMainViewGeneric *mv,
+                                   gint x,
+                                   gint y)
+{
+  GtkTreePath *path = NULL;
+
+  gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (mv), x, y, &path,
+                                 NULL, NULL, NULL);
+
+  return path;
+}
+
+static void
+gd_main_list_view_set_selection_mode (GdMainViewGeneric *mv,
+                                      gboolean selection_mode)
+{
+  GdMainListView *self = GD_MAIN_LIST_VIEW (mv);
+
+  self->priv->selection_mode = selection_mode;
+
+  g_object_set (self->priv->selection_cell,
+                "visible", selection_mode,
+                NULL);
+  gtk_tree_view_column_queue_resize (self->priv->tree_col);
+}
+
+static void
+gd_main_list_view_scroll_to_path (GdMainViewGeneric *mv,
+                                  GtkTreePath *path)
+{
+  gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (mv), path, NULL, TRUE, 0.5, 0.5);
+}
+
+static void
+gd_main_list_view_set_model (GdMainViewGeneric *mv,
+                             GtkTreeModel *model)
+{
+  gtk_tree_view_set_model (GTK_TREE_VIEW (mv), model);
+}
+
+static void
+gd_main_view_generic_iface_init (GdMainViewGenericIface *iface)
+{
+  iface->set_model = gd_main_list_view_set_model;
+  iface->get_path_at_pos = gd_main_list_view_get_path_at_pos;
+  iface->scroll_to_path = gd_main_list_view_scroll_to_path;
+  iface->set_selection_mode = gd_main_list_view_set_selection_mode;
+}
+
+void
+gd_main_list_view_add_renderer (GdMainListView *self,
+                                GtkCellRenderer *renderer,
+                                GtkTreeCellDataFunc func,
+                                gpointer user_data,
+                                GDestroyNotify destroy)
+{
+  gtk_tree_view_column_pack_start (self->priv->tree_col, renderer, FALSE);
+  gtk_tree_view_column_set_cell_data_func (self->priv->tree_col, renderer,
+                                           func, user_data, destroy);
+}
+
+GtkWidget *
+gd_main_list_view_new (void)
+{
+  return g_object_new (GD_TYPE_MAIN_LIST_VIEW, NULL);
+}
diff --git a/libgd/gd-main-list-view.h b/libgd/gd-main-list-view.h
new file mode 100644
index 0000000..317e9c4
--- /dev/null
+++ b/libgd/gd-main-list-view.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2011 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by 
+ * the Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License 
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Cosimo Cecchi <cosimoc redhat com>
+ *
+ */
+
+#ifndef __GD_MAIN_LIST_VIEW_H__
+#define __GD_MAIN_LIST_VIEW_H__
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GD_TYPE_MAIN_LIST_VIEW gd_main_list_view_get_type()
+
+#define GD_MAIN_LIST_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   GD_TYPE_MAIN_LIST_VIEW, GdMainListView))
+
+#define GD_MAIN_LIST_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   GD_TYPE_MAIN_LIST_VIEW, GdMainListViewClass))
+
+#define GD_IS_MAIN_LIST_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   GD_TYPE_MAIN_LIST_VIEW))
+
+#define GD_IS_MAIN_LIST_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   GD_TYPE_MAIN_LIST_VIEW))
+
+#define GD_MAIN_LIST_VIEW_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   GD_TYPE_MAIN_LIST_VIEW, GdMainListViewClass))
+
+typedef struct _GdMainListView GdMainListView;
+typedef struct _GdMainListViewClass GdMainListViewClass;
+typedef struct _GdMainListViewPrivate GdMainListViewPrivate;
+
+struct _GdMainListView
+{
+  GtkTreeView parent;
+
+  GdMainListViewPrivate *priv;
+};
+
+struct _GdMainListViewClass
+{
+  GtkTreeViewClass parent_class;
+};
+
+GType gd_main_list_view_get_type (void) G_GNUC_CONST;
+
+GtkWidget * gd_main_list_view_new (void);
+
+void gd_main_list_view_add_renderer (GdMainListView *self,
+                                     GtkCellRenderer *renderer,
+                                     GtkTreeCellDataFunc func,
+                                     gpointer user_data,
+                                     GDestroyNotify destroy);
+
+G_END_DECLS
+
+#endif /* __GD_MAIN_LIST_VIEW_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]