[gnome-todo/wip/gbsneto/plugins: 14/18] task-list-view: add support for custom header functions



commit 973ed9cd518a09a9506ae86b3c30b0bf4635000d
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Jan 16 20:30:32 2016 -0200

    task-list-view: add support for custom header functions
    
    They'll be used later on Scheduled panel to show some
    cool labels according to the task due date (e.g. "Overdue",
    "N days ago", "Yesterday", etc).

 src/gtd-task-list-view.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++
 src/gtd-task-list-view.h |   24 ++++++++++++++-
 2 files changed, 96 insertions(+), 1 deletions(-)
---
diff --git a/src/gtd-task-list-view.c b/src/gtd-task-list-view.c
index 4adfaf0..81c4ced 100644
--- a/src/gtd-task-list-view.c
+++ b/src/gtd-task-list-view.c
@@ -57,6 +57,9 @@ typedef struct
 
   /* action */
   GActionGroup          *action_group;
+
+  GtdTaskListViewHeaderFunc header_func;
+  gpointer                  header_user_data;
 } GtdTaskListViewPrivate;
 
 struct _GtdTaskListView
@@ -145,6 +148,32 @@ undo_remove_task_action (GtdNotification *notification,
 }
 
 static void
+internal_header_func (GtdTaskRow      *row,
+                      GtdTaskRow      *before,
+                      GtdTaskListView *view)
+{
+  GtdTask *row_task;
+  GtdTask *before_task;
+
+  if (!view->priv->header_func || row == view->priv->new_task_row)
+    return;
+
+  row_task = before_task = NULL;
+
+  if (row)
+    row_task = gtd_task_row_get_task (row);
+
+  if (before)
+    before_task = gtd_task_row_get_task (before);
+
+  view->priv->header_func (GTK_LIST_BOX_ROW (row),
+                           row_task,
+                           GTK_LIST_BOX_ROW (before),
+                           before_task,
+                           view->priv->header_user_data);
+}
+
+static void
 update_font_color (GtdTaskListView *view)
 {
   GtdTaskListViewPrivate *priv;
@@ -1203,3 +1232,47 @@ gtd_task_list_view_set_show_completed (GtdTaskListView *view,
       g_object_notify (G_OBJECT (view), "show-completed");
     }
 }
+
+/**
+ * gtd_task_list_view_set_header_func:
+ * @view: a #GtdTaskListView
+ * @func: (closure user_data) (scope call) (nullable): the header function
+ * @user_data: data passed to @func
+ *
+ * Sets @func as the header function of @view. You can safely call
+ * %gtk_list_box_row_set_header from within @func.
+ *
+ * Do not unref nor free any of the passed data.
+ */
+void
+gtd_task_list_view_set_header_func (GtdTaskListView           *view,
+                                    GtdTaskListViewHeaderFunc  func,
+                                    gpointer                   user_data)
+{
+  GtdTaskListViewPrivate *priv;
+
+  g_return_if_fail (GTD_IS_TASK_LIST_VIEW (view));
+
+  priv = view->priv;
+
+  if (func)
+    {
+      priv->header_func = func;
+      priv->header_user_data = user_data;
+
+      gtk_list_box_set_header_func (priv->listbox,
+                                    (GtkListBoxUpdateHeaderFunc) internal_header_func,
+                                    view,
+                                    NULL);
+    }
+  else
+    {
+      priv->header_func = NULL;
+      priv->header_user_data = NULL;
+
+      gtk_list_box_set_header_func (priv->listbox,
+                                    NULL,
+                                    NULL,
+                                    NULL);
+    }
+}
diff --git a/src/gtd-task-list-view.h b/src/gtd-task-list-view.h
index 226c052..4a4b039 100644
--- a/src/gtd-task-list-view.h
+++ b/src/gtd-task-list-view.h
@@ -29,6 +29,23 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtdTaskListView, gtd_task_list_view, GTD, TASK_LIST_VIEW, GtkOverlay)
 
+/**
+ * GtdTaskListViewHeaderFunc:
+ * @row: the current #GtkListBoxRow
+ * @row_task: the #GtdTask that @row represents
+ * @before: the #GtkListBoxRow before @row
+ * @before_task: the #GtdTask that @before represents
+ * @user_data: (closure): user data
+ *
+ * Will be called when the primary or secondary action of @notification
+ * is executed.
+ */
+typedef void (*GtdTaskListViewHeaderFunc) (GtkListBoxRow        *row,
+                                           GtdTask              *row_task,
+                                           GtkListBoxRow        *before,
+                                           GtdTask              *before_task,
+                                           gpointer              user_data);
+
 GtkWidget*                gtd_task_list_view_new                (void);
 
 GList*                    gtd_task_list_view_get_list           (GtdTaskListView        *view);
@@ -53,8 +70,13 @@ void                      gtd_task_list_view_set_show_list_name (GtdTaskListView
 
 gboolean                  gtd_task_list_view_get_show_completed (GtdTaskListView        *view);
 
-void                      gtd_task_list_view_set_show_completed      (GtdTaskListView        *view,
+void                      gtd_task_list_view_set_show_completed (GtdTaskListView        *view,
                                                                  gboolean                show_completed);
+
+void                      gtd_task_list_view_set_header_func    (GtdTaskListView           *view,
+                                                                 GtdTaskListViewHeaderFunc  func,
+                                                                 gpointer                   user_data);
+
 G_END_DECLS
 
 #endif /* GTD_TASK_LIST_VIEW_H */


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