[gnome-notes/136-implement-the-list-row-pattern-in-main-view: 10/13] list-view: added new widgets



commit fcf0df0ac20b75f65ab42658ae1816fd2c9d670a
Author: Isaque Galdino <igaldino gmail com>
Date:   Fri Feb 14 13:34:32 2020 -0300

    list-view: added new widgets

 data/bjb.gresource.xml          |  12 +--
 data/meson.build                |   5 +-
 data/resources/list-view-row.ui |  86 ++++++++++++++++++++++
 data/resources/list-view.ui     |  25 +++++++
 src/bjb-list-view-row.c         |  84 +++++++++++++++++++++
 src/bjb-list-view-row.h         |  38 ++++++++++
 src/bjb-list-view.c             | 159 ++++++++++++++++++++++++++++++++++++++++
 src/bjb-list-view.h             |  40 ++++++++++
 src/meson.build                 |   2 +
 9 files changed, 442 insertions(+), 9 deletions(-)
---
diff --git a/data/bjb.gresource.xml b/data/bjb.gresource.xml
index d02f824..c4f13a3 100644
--- a/data/bjb.gresource.xml
+++ b/data/bjb.gresource.xml
@@ -14,11 +14,13 @@
   </gresource>
     
   <gresource prefix="/org/gnome/Notes/ui">
-    <file alias="main-toolbar.ui" preprocess="xml-stripblanks">resources/main-toolbar.ui</file>
-    <file alias="organize-dialog.ui" preprocess="xml-stripblanks">resources/organize-dialog.ui</file>
-    <file alias="settings-dialog.ui">resources/settings-dialog.ui</file>
-    <file alias="empty-results-box.ui">resources/empty-results-box.ui</file>
-    <file alias="import-dialog.ui" preprocess="xml-stripblanks">resources/import-dialog.ui</file>
+    <file alias="empty-results-box.ui" preprocess="xml-stripblanks">resources/empty-results-box.ui</file>
+    <file alias="import-dialog.ui"     preprocess="xml-stripblanks">resources/import-dialog.ui</file>
+    <file alias="list-view.ui"         preprocess="xml-stripblanks">resources/list-view.ui</file>
+    <file alias="list-view-row.ui"     preprocess="xml-stripblanks">resources/list-view-row.ui</file>
+    <file alias="main-toolbar.ui"      preprocess="xml-stripblanks">resources/main-toolbar.ui</file>
+    <file alias="organize-dialog.ui"   preprocess="xml-stripblanks">resources/organize-dialog.ui</file>
     <file alias="selection-toolbar.ui" preprocess="xml-stripblanks">resources/selection-toolbar.ui</file>
+    <file alias="settings-dialog.ui"   preprocess="xml-stripblanks">resources/settings-dialog.ui</file>
   </gresource>
 </gresources>
diff --git a/data/meson.build b/data/meson.build
index ac090e6..4867ab8 100644
--- a/data/meson.build
+++ b/data/meson.build
@@ -45,10 +45,7 @@ install_data(
 
 resource_data = files(
   'resources/Adwaita.css',
-  'resources/editor-toolbar.ui',
-  'resources/empty-results-box.ui',
-  'resources/note-symbolic.svg',
-  'resources/settings-dialog.ui'
+  'resources/note-symbolic.svg'
 )
 
 gresource = files('bjb.gresource.xml')
diff --git a/data/resources/list-view-row.ui b/data/resources/list-view-row.ui
new file mode 100644
index 0000000..dd68874
--- /dev/null
+++ b/data/resources/list-view-row.ui
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="BjbListViewRow" parent="GtkBox">
+    <property name="visible">True</property>
+    <property name="margin-start">24</property>
+    <property name="margin-end">24</property>
+    <property name="margin-top">8</property>
+    <property name="margin-bottom">8</property>
+    <property name="orientation">horizontal</property>
+    <property name="spacing">16</property>
+
+    <child>
+      <object class="GtkCheckButton" id="selection_button">
+        <property name="visible">False</property>
+        <property name="can-focus">False</property>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="fill">True</property>
+      </packing>
+    </child>
+
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="valign">center</property>
+
+        <child>
+          <object class="GtkLabel" id="title">
+            <property name="visible">True</property>
+            <property name="width-request">192</property>
+            <property name="max-width-chars">128</property>
+            <property name="justify">left</property>
+            <property name="halign">start</property>
+            <property name="hexpand">True</property>
+            <property name="valign">center</property>
+            <property name="ellipsize">end</property>
+            <property name="xalign">0.0</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+          </packing>
+        </child>
+
+        <child>
+          <object class="GtkLabel" id="content">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="wrap">True</property>
+                    <property name="ellipsize">end</property>
+                    <property name="lines">3</property>
+                    <property name="xalign">0</property>
+                    <property name="yalign">0</property>
+
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+
+    <child>
+      <object class="GtkLabel" id="updated_time">
+        <property name="visible">True</property>
+        <property name="width-request">192</property>
+        <property name="max-width-chars">0</property>
+        <property name="justify">right</property>
+        <property name="halign">end</property>
+        <property name="valign">start</property>
+        <property name="ellipsize">end</property>
+        <property name="xalign">1.0</property>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="fill">True</property>
+      </packing>
+    </child>
+  </template>
+</interface>
diff --git a/data/resources/list-view.ui b/data/resources/list-view.ui
new file mode 100644
index 0000000..6602d30
--- /dev/null
+++ b/data/resources/list-view.ui
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="BjbListView" parent="GtkScrolledWindow">
+    <property name="visible">True</property>
+    <property name="expand">True</property>
+    <property name="hscrollbar-policy">never</property>
+    <property name="vscrollbar-policy">automatic</property>
+    <property name="max-content-width">948</property>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkListBox" id="list_box">
+            <property name="visible">True</property>
+            <property name="margin-start">24</property>
+            <property name="margin-end">24</property>
+            <property name="margin-top">12</property>
+            <property name="margin-bottom">12</property>
+            <property name="valign">start</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/bjb-list-view-row.c b/src/bjb-list-view-row.c
new file mode 100644
index 0000000..2088bef
--- /dev/null
+++ b/src/bjb-list-view-row.c
@@ -0,0 +1,84 @@
+/*
+ * bjb-list-view-row.c
+ * Copyright 2020 Isaque Galdino <igaldino gmail org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <biji-string.h>
+#include "bjb-list-view-row.h"
+
+struct _BjbListViewRow
+{
+  GtkBox    parent_instance;
+
+  GtkLabel *title;
+  GtkLabel *content;
+  GtkLabel *updated_time;
+};
+
+G_DEFINE_TYPE (BjbListViewRow, bjb_list_view_row, GTK_TYPE_BOX);
+
+void
+bjb_list_view_row_setup (BjbListViewRow *self,
+                         const char     *title,
+                         const char     *content,
+                         const char     *updated_time)
+{
+  g_auto (GStrv)   lines   = NULL;
+  g_autofree char *preview = NULL;
+
+  if (title)
+    {
+      gtk_label_set_text (self->title, title);
+    }
+  if (content)
+    {
+      lines = g_strsplit(content, "\n", 4);
+      preview = bjb_strjoinv ("\n", lines, 3);
+      gtk_label_set_text (self->content, preview);
+    }
+  if (updated_time)
+    {
+      gtk_label_set_text (self->updated_time, updated_time);
+    }
+}
+
+static void
+bjb_list_view_row_init (BjbListViewRow *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+static void
+bjb_list_view_row_class_init (BjbListViewRowClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Notes/ui/list-view-row.ui");
+
+  gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, title);
+  gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, content);
+  gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, updated_time);
+}
+
+
+BjbListViewRow *
+bjb_list_view_row_new (void)
+{
+  return g_object_new (BJB_TYPE_LIST_VIEW_ROW, NULL);
+}
+
diff --git a/src/bjb-list-view-row.h b/src/bjb-list-view-row.h
new file mode 100644
index 0000000..407c542
--- /dev/null
+++ b/src/bjb-list-view-row.h
@@ -0,0 +1,38 @@
+/*
+ * bjb-list-view-row.h
+ * Copyright 2020 Isaque Galdino <igaldino gmail com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_LIST_VIEW_ROW (bjb_list_view_row_get_type ())
+
+G_DECLARE_FINAL_TYPE (BjbListViewRow, bjb_list_view_row, BJB, LIST_VIEW_ROW, GtkBox)
+
+BjbListViewRow *bjb_list_view_row_new   (void);
+
+void            bjb_list_view_row_setup (BjbListViewRow *self,
+                                         const char     *title,
+                                         const char     *content,
+                                         const char     *updated_time);
+
+G_END_DECLS
diff --git a/src/bjb-list-view.c b/src/bjb-list-view.c
new file mode 100644
index 0000000..e68df4d
--- /dev/null
+++ b/src/bjb-list-view.c
@@ -0,0 +1,159 @@
+/*
+ * bjb-list-view.c
+ * Copyright 2018 Isaque Galdino <igaldino gmail com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "bjb-list-view.h"
+#include "bjb-list-view-row.h"
+#include "bjb-utils.h"
+
+struct _BjbListView
+{
+  GtkScrolledWindow  parent_instance;
+
+  GtkListBox        *list_box;
+  BjbController     *controller;
+
+  gulong             display_items_changed;
+};
+
+G_DEFINE_TYPE (BjbListView, bjb_list_view, GTK_TYPE_SCROLLED_WINDOW);
+
+static void
+bjb_list_view_destroy_row_cb (GtkWidget *widget,
+                              gpointer   data)
+{
+  gtk_widget_destroy (widget);
+}
+
+static gboolean
+bjb_list_view_create_row_cb (GtkTreeModel *model,
+                             GtkTreePath  *path,
+                             GtkTreeIter  *iter,
+                             gpointer      data)
+{
+  BjbListView     *self = NULL;
+  char            *title;
+  char            *text;
+  gint64           mtime;
+  BjbListViewRow  *row;
+  g_autofree char *updated_time = NULL;
+
+  self = BJB_LIST_VIEW (data);
+
+  gtk_tree_model_get (model,
+                      iter,
+                      BJB_MODEL_COLUMN_TITLE, &title,
+                      BJB_MODEL_COLUMN_TEXT,  &text,
+                      BJB_MODEL_COLUMN_MTIME, &mtime,
+                      -1);
+
+  updated_time = bjb_utils_get_human_time (mtime);
+
+  row = bjb_list_view_row_new ();
+  bjb_list_view_row_setup (row, title, text, updated_time);
+
+  gtk_widget_show (GTK_WIDGET (row));
+
+  gtk_container_add (GTK_CONTAINER (self->list_box), GTK_WIDGET (row));
+
+  return FALSE;
+}
+
+static void
+bjb_list_view_on_display_items_changed (BjbController *controller,
+                                        gboolean       items_to_show,
+                                        gboolean       remaining_items,
+                                        BjbListView   *self)
+{
+  g_return_if_fail (self);
+
+  bjb_list_view_update (self);
+}
+
+static void
+bjb_list_view_finalize (GObject *object)
+{
+  BjbListView *self = BJB_LIST_VIEW (object);
+
+  if (self->display_items_changed != 0)
+    {
+      g_signal_handler_disconnect (self->controller, self->display_items_changed);
+    }
+
+  G_OBJECT_CLASS (bjb_list_view_parent_class)->finalize (object);
+}
+
+void
+bjb_list_view_setup (BjbListView   *self,
+                     BjbController *controller)
+{
+  g_return_if_fail (controller);
+
+  if (self->display_items_changed != 0)
+    {
+      g_signal_handler_disconnect (self->controller, self->display_items_changed);
+    }
+
+  self->controller = controller;
+
+  self->display_items_changed = g_signal_connect (self->controller,
+                                                  "display-items-changed",
+                                                  G_CALLBACK (bjb_list_view_on_display_items_changed),
+                                                  self);
+
+  bjb_list_view_update (self);
+}
+
+void
+bjb_list_view_update (BjbListView *self)
+{
+  gtk_container_foreach (GTK_CONTAINER (self->list_box),
+                         bjb_list_view_destroy_row_cb,
+                         NULL);
+
+  gtk_tree_model_foreach (bjb_controller_get_model (self->controller),
+                          bjb_list_view_create_row_cb,
+                          self);
+}
+
+static void
+bjb_list_view_init (BjbListView *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+static void
+bjb_list_view_class_init (BjbListViewClass *klass)
+{
+  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = bjb_list_view_finalize;
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Notes/ui/list-view.ui");
+
+  gtk_widget_class_bind_template_child (widget_class, BjbListView, list_box);
+}
+
+
+BjbListView *
+bjb_list_view_new (void)
+{
+  return g_object_new (BJB_TYPE_LIST_VIEW, NULL);
+}
diff --git a/src/bjb-list-view.h b/src/bjb-list-view.h
new file mode 100644
index 0000000..d68ae6c
--- /dev/null
+++ b/src/bjb-list-view.h
@@ -0,0 +1,40 @@
+/*
+ * bjb-list-view.h
+ * Copyright 2018 Isaque Galdino <igaldino gmail com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+#include <bjb-controller.h>
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_LIST_VIEW (bjb_list_view_get_type ())
+
+G_DECLARE_FINAL_TYPE (BjbListView, bjb_list_view, BJB, LIST_VIEW, GtkScrolledWindow)
+
+BjbListView *bjb_list_view_new    (void);
+
+void         bjb_list_view_setup  (BjbListView   *self,
+                                   BjbController *controller);
+
+void         bjb_list_view_update (BjbListView   *self);
+
+G_END_DECLS
+
diff --git a/src/meson.build b/src/meson.build
index 27d5b3a..aeb3dba 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -7,6 +7,8 @@ sources = files(
   'bjb-editor-toolbar.c',
   'bjb-empty-results-box.c',
   'bjb-import-dialog.c',
+  'bjb-list-view.c',
+  'bjb-list-view-row.c',
   'bjb-load-more-button.c',
   'bjb-main.c',
   'bjb-main-toolbar.c',


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