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



commit c5fe2a179826984f438e3ee63f3f9581ab40951e
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/Adwaita.css      |  15 +++-
 data/resources/list-view-row.ui | 119 ++++++++++++++++++++++++++
 data/resources/list-view.ui     |  33 ++++++++
 src/bjb-list-view-row.c         | 113 +++++++++++++++++++++++++
 src/bjb-list-view-row.h         |  44 ++++++++++
 src/bjb-list-view.c             | 183 ++++++++++++++++++++++++++++++++++++++++
 src/bjb-list-view.h             |  45 ++++++++++
 src/meson.build                 |   2 +
 10 files changed, 558 insertions(+), 13 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/Adwaita.css b/data/resources/Adwaita.css
index 279cdba..5a54db9 100644
--- a/data/resources/Adwaita.css
+++ b/data/resources/Adwaita.css
@@ -1,5 +1,12 @@
-.biji-notebook-icon {
-    border: 1px solid rgba(0, 0, 0, 0.4);
-    background-color: #d3d7cf;
-    border-radius: 8px;
+.small-font {
+    font-size: smaller;
 }
+
+row {
+       border-bottom: solid 1px rgba(0,0,0, 0.1);
+}
+
+row:last-child {
+       border-bottom: none;
+}
+
diff --git a/data/resources/list-view-row.ui b/data/resources/list-view-row.ui
new file mode 100644
index 0000000..98e4035
--- /dev/null
+++ b/data/resources/list-view-row.ui
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="BjbListViewRow" parent="GtkListBoxRow">
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="orientation">horizontal</property>
+        <property name="border-width">12</property>
+        <property name="spacing">12</property>
+
+        <child>
+          <object class="GtkCheckButton" id="select_button">
+            <property name="visible">False</property>
+            <property name="can-focus">False</property>
+            <property name="valign">start</property>
+            <signal name="toggled" handler="on_toggled_cb" object="BjbListViewRow" swapped="yes" />
+          </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>
+            <property name="spacing">6</property>
+
+            <child>
+              <object class="GtkLabel" id="uuid">
+                <property name="visible">False</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="GtkBox">
+                <property name="visible">True</property>
+                <property name="orientation">horizontal</property>
+                <property name="spacing">12</property>
+
+                <child>
+                  <object class="GtkLabel" id="title">
+                    <property name="visible">True</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="updated_time">
+                    <property name="visible">True</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>
+                    <style>
+                      <class name="dim-label"/>
+                      <class name="small-font"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                  </packing>
+                </child>
+
+              </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="wrap_mode">word-char</property>
+                <property name="ellipsize">end</property>
+                <property name="lines">3</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+                <style>
+                  <class name="small-font"/>
+                </style>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/data/resources/list-view.ui b/data/resources/list-view.ui
new file mode 100644
index 0000000..a4b81ef
--- /dev/null
+++ b/data/resources/list-view.ui
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="BjbListView" parent="GtkScrolledWindow">
+    <property name="visible">True</property>
+    <property name="hscrollbar-policy">never</property>
+    <property name="vscrollbar-policy">automatic</property>
+    <property name="vexpand">True</property>
+    <child>
+      <object class="HdyColumn">
+        <property name="visible">True</property>
+        <property name="maximum_width">600</property>
+        <property name="linear_growth_width">400</property>
+        <property name="margin_top">32</property>
+        <property name="margin_bottom">32</property>
+        <property name="margin_start">12</property>
+        <property name="margin_end">12</property>
+
+        <child>
+          <object class="GtkListBox" id="list_box">
+            <property name="visible">True</property>
+            <property name="selection-mode">none</property>
+            <style>
+              <class name="view"/>
+              <class name="frame"/>
+            </style>
+          </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..ec6be92
--- /dev/null
+++ b/src/bjb-list-view-row.c
@@ -0,0 +1,113 @@
+/*
+ * 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
+{
+  GtkListBoxRow   parent_instance;
+
+  GtkCheckButton *select_button;
+  GtkLabel       *uuid;
+  GtkLabel       *title;
+  GtkLabel       *content;
+  GtkLabel       *updated_time;
+};
+
+G_DEFINE_TYPE (BjbListViewRow, bjb_list_view_row, GTK_TYPE_LIST_BOX_ROW);
+
+static void
+on_toggled_cb (BjbListViewRow *self,
+               gpointer        data)
+{
+  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->select_button)))
+    gtk_list_box_select_row (GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self))), GTK_LIST_BOX_ROW 
(self));
+  else
+    gtk_list_box_unselect_row (GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self))), GTK_LIST_BOX_ROW 
(self));
+
+  g_signal_emit_by_name (G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (self))), "selected-rows-changed", 0);
+
+}
+
+void
+bjb_list_view_row_setup (BjbListViewRow *self,
+                         const char     *uuid,
+                         const char     *title,
+                         const char     *content,
+                         const char     *updated_time)
+{
+  g_auto (GStrv)   lines   = NULL;
+  g_autofree char *preview = NULL;
+
+  if (uuid)
+    gtk_label_set_text (self->uuid, uuid);
+  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);
+}
+
+void
+bjb_list_view_row_show_select_button (BjbListViewRow *self,
+                                      gboolean        show)
+{
+  gtk_widget_set_visible (GTK_WIDGET (self->select_button), show);
+}
+
+const char *
+bjb_list_view_row_get_uuid (BjbListViewRow *self)
+{
+  return gtk_label_get_text (self->uuid);
+}
+
+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, select_button);
+  gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, uuid);
+  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);
+  gtk_widget_class_bind_template_callback (widget_class, on_toggled_cb);
+}
+
+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..656ec57
--- /dev/null
+++ b/src/bjb-list-view-row.h
@@ -0,0 +1,44 @@
+/*
+ * 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, GtkListBoxRow)
+
+BjbListViewRow *bjb_list_view_row_new                (void);
+
+void            bjb_list_view_row_setup              (BjbListViewRow *self,
+                                                      const char     *uuid,
+                                                      const char     *title,
+                                                      const char     *content,
+                                                      const char     *updated_time);
+
+void            bjb_list_view_row_show_select_button (BjbListViewRow *self,
+                                                      gboolean        show);
+
+const char     *bjb_list_view_row_get_uuid           (BjbListViewRow *self);
+
+G_END_DECLS
diff --git a/src/bjb-list-view.c b/src/bjb-list-view.c
new file mode 100644
index 0000000..547511a
--- /dev/null
+++ b/src/bjb-list-view.c
@@ -0,0 +1,183 @@
+/*
+ * 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            *uuid;
+  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_UUID,  &uuid,
+                      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, uuid, 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_show_select_button_cb (GtkWidget *widget,
+                                     gpointer   data)
+{
+  BjbListViewRow *row  = BJB_LIST_VIEW_ROW (widget);
+  gboolean        mode = GPOINTER_TO_INT (data);
+
+  bjb_list_view_row_show_select_button (row, mode);
+}
+
+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);
+}
+
+GtkListBox *
+bjb_list_view_get_list_box (BjbListView *self)
+{
+  return self->list_box;
+}
+
+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);
+}
+
+void
+bjb_list_view_set_selection_mode (BjbListView *self,
+                                  gboolean     mode)
+{
+  gtk_list_box_unselect_all (self->list_box);
+
+  gtk_container_foreach (GTK_CONTAINER (self->list_box),
+                         bjb_list_view_show_select_button_cb,
+                         GINT_TO_POINTER (mode));
+}
diff --git a/src/bjb-list-view.h b/src/bjb-list-view.h
new file mode 100644
index 0000000..5ddcf16
--- /dev/null
+++ b/src/bjb-list-view.h
@@ -0,0 +1,45 @@
+/*
+ * 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);
+
+GtkListBox  *bjb_list_view_get_list_box       (BjbListView   *self);
+
+void         bjb_list_view_set_selection_mode (BjbListView   *self,
+                                               gboolean       mode);
+
+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]