[bijiben/wip/sadiq/rewrite: 6/6] Add main-view class



commit 18524029909ce925f5e2c2e5b3c16d27ee2d9d54
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Sat Mar 10 12:42:56 2018 +0530

    Add main-view class

 src/resources/bijiben.gresource.xml |    1 +
 src/resources/ui/bjb-main-view.ui   |   28 ++++
 src/views/bjb-main-view.c           |  298 +++++++++++++++++++++++++++++++++++
 src/views/bjb-main-view.h           |   46 ++++++
 4 files changed, 373 insertions(+), 0 deletions(-)
---
diff --git a/src/resources/bijiben.gresource.xml b/src/resources/bijiben.gresource.xml
index 475904f..c4903d1 100644
--- a/src/resources/bijiben.gresource.xml
+++ b/src/resources/bijiben.gresource.xml
@@ -3,6 +3,7 @@
   <gresource prefix="/org/gnome/bijiben">
     <file preprocess="xml-stripblanks">gtk/menus.ui</file>
     <file preprocess="xml-stripblanks">ui/bjb-window.ui</file>
+    <file preprocess="xml-stripblanks">ui/bjb-main-view.ui</file>
     <file>css/style.css</file>
   </gresource>
 </gresources>
diff --git a/src/resources/ui/bjb-main-view.ui b/src/resources/ui/bjb-main-view.ui
new file mode 100644
index 0000000..cb1ef4e
--- /dev/null
+++ b/src/resources/ui/bjb-main-view.ui
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk 3.14 -->
+  <template class="BjbMainView" parent="GtkStack">
+    <property name="visible">1</property>
+
+    <!-- <child>
+         <object class="BjbListView" id="list_view">
+         <property name="selection-mode" bind-source="BjbMainView" bind-property="selection-mode"/>
+         <property name="visible">1</property>
+         </object>
+         <packing>
+         <property name="name">list</property>
+         </packing>
+         </child>
+    -->
+    <!-- <child>
+         <object class="BjbGridView" id="grid_view">
+         <property name="selection-mode" bind-source="BjbMainView" bind-property="selection-mode"/>
+         <property name="visible">1</property>
+         </object>
+         <packing>
+         <property name="name">grid</property>
+         </packing>
+         </child> -->
+
+  </template>
+</interface>
diff --git a/src/views/bjb-main-view.c b/src/views/bjb-main-view.c
new file mode 100644
index 0000000..8100cc7
--- /dev/null
+++ b/src/views/bjb-main-view.c
@@ -0,0 +1,298 @@
+/* bjb-main-view.c
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "bjb-main-view"
+
+#include "config.h"
+
+#include "bjb-trace.h"
+
+#include "bjb-main-view.h"
+
+/**
+ * SECTION: bjb-main-view
+ * @title: BjbMainView
+ * @short_description:
+ * @include: "bjb-main-view.h"
+ *
+ *
+ */
+
+struct _BjbMainView
+{
+  GtkStack parent_instance;
+
+  GtkTreeStore *store;
+
+  GtkWidget *grid_view;
+  GtkWidget *list_view;
+
+  gboolean selection_mode;
+};
+
+G_DEFINE_TYPE (BjbMainView, bjb_main_view, GTK_TYPE_STACK)
+
+enum {
+  PROP_0,
+  PROP_SELECTION_MODE,
+  PROP_STORE,
+  N_PROPS
+};
+
+enum {
+  ITEM_ACTIVATED,
+  N_SIGNALS
+};
+
+static GParamSpec *properties[N_PROPS];
+static guint signals[N_SIGNALS];
+
+static void
+bjb_main_view_get_property (GObject    *object,
+                            guint       prop_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+  BjbMainView *self = (BjbMainView *)object;
+
+  switch (prop_id)
+    {
+    case PROP_SELECTION_MODE:
+      g_value_set_boolean (value, self->selection_mode);
+      break;
+
+    case PROP_STORE:
+      g_value_set_object (value, self->store);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+bjb_main_view_set_property (GObject      *object,
+                            guint         prop_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+  BjbMainView *self = (BjbMainView *)object;
+
+  switch (prop_id)
+    {
+    case PROP_SELECTION_MODE:
+      bjb_main_view_set_selection_mode (self, g_value_get_boolean (value));
+      break;
+
+    case PROP_STORE:
+      bjb_main_view_set_store (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+bjb_main_view_dispose (GObject *object)
+{
+  BjbMainView *self = (BjbMainView *)object;
+
+  BJB_ENTRY;
+
+
+
+  G_OBJECT_CLASS (bjb_main_view_parent_class)->dispose (object);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_main_view_finalize (GObject *object)
+{
+  BjbMainView *self = (BjbMainView *)object;
+
+  BJB_ENTRY;
+
+
+
+  G_OBJECT_CLASS (bjb_main_view_parent_class)->finalize (object);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_main_view_class_init (BjbMainViewClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->get_property = bjb_main_view_get_property;
+  object_class->set_property = bjb_main_view_set_property;
+  object_class->dispose = bjb_main_view_dispose;
+  object_class->finalize = bjb_main_view_finalize;
+
+  properties[PROP_SELECTION_MODE] =
+    g_param_spec_boolean ("selection-mode",
+                          "Selection Mode",
+                          "If mode is selection mode or not",
+                          FALSE,
+                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_STORE] =
+    g_param_spec_object ("store",
+                         "Store",
+                         "The GtkTreeStore for the view",
+                         GTK_TYPE_TREE_STORE,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                               "/org/gnome/bijiben/"
+                                               "ui/bjb-main-view.ui");
+
+  /* gtk_widget_class_bind_template_child (widget_class, BjbMainView, menu_button); */
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+bjb_main_view_init (BjbMainView *self)
+{
+  BJB_ENTRY;
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  BJB_EXIT;
+}
+
+BjbMainView *
+bjb_main_view_new (void)
+{
+  return g_object_new (BJB_TYPE_MAIN_VIEW,
+                       NULL);
+}
+
+/**
+ * bjb_main_view_get_selection_mode:
+ * @self: A #BjbMainView
+ *
+ * Get if selection mode is active
+ *
+ * Returns: %TRUE if selection mode is active. %FALSE otherwise
+ */
+gboolean
+bjb_main_view_get_selection_mode (BjbMainView *self)
+{
+  g_return_val_if_fail (BJB_IS_MAIN_VIEW (self), FALSE);
+
+  return self->selection_mode;
+}
+
+/**
+ * bjb_main_view_set_selection_mode:
+ * @self: A #BjbMainView
+ * @selection_mode: a gboolean to set mode
+ *
+ * Set selection mode
+ */
+void
+bjb_main_view_set_selection_mode (BjbMainView *self,
+                                  gboolean     selection_mode)
+{
+  BJB_ENTRY;
+
+  g_return_if_fail (BJB_IS_MAIN_VIEW (self));
+
+  if (self->selection_mode == selection_mode)
+    return;
+
+  self->selection_mode = selection_mode;
+
+  if (selection_mode)
+    {
+      gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (self->grid_view),
+                                       GTK_SELECTION_MULTIPLE);
+      gtk_list_box_set_selection_mode (GTK_LIST_BOX (self->list_view),
+                                       GTK_SELECTION_MULTIPLE);
+    }
+  else
+    {
+      gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (self->grid_view),
+                                       GTK_SELECTION_NONE);
+      gtk_list_box_set_selection_mode (GTK_LIST_BOX (self->list_view),
+                                       GTK_SELECTION_NONE);
+    }
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTION_MODE]);
+
+  BJB_EXIT;
+}
+
+/**
+ * bjb_main_view_set_selection_mode:
+ * @self: A #BjbMainView
+ * @selection_mode: a gboolean to set mode
+ *
+ * Set selection mode
+ */
+void
+bjb_main_view_set_store (BjbMainView  *self,
+                         GtkTreeStore *store)
+{
+  g_return_if_fail (BJB_IS_MAIN_VIEW (self));
+
+  if (g_set_object (&self->store, store))
+    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STORE]);
+}
+
+/**
+ * bjb_main_view_set_view:
+ * @self: A #BjbMainView
+ * @view_type: a #BjbViewType
+ *
+ * Set Current view type.
+ */
+void
+bjb_main_view_set_view (BjbMainView *self,
+                        BjbViewType  view_type)
+{
+  GtkStack *stack;
+  GtkWidget *current_view;
+
+  g_return_if_fail (BJB_IS_MAIN_VIEW (self));
+
+  stack = GTK_STACK (self);
+  current_view = gtk_stack_get_visible_child (stack);
+
+  if (view_type == BJB_VIEW_TYPE_GRID &&
+      current_view == self->grid_view)
+    return;
+
+  if (view_type == BJB_VIEW_TYPE_GRID)
+    {
+      gtk_stack_set_visible_child (stack, self->list_view);
+      /* bjb_main_view_populate_view (self, self->list_view); */
+    }
+  else
+    {
+      gtk_stack_set_visible_child (stack, self->grid_view);
+      /* bjb_main_view_populate_view (self, self->grid_view); */
+    }
+}
diff --git a/src/views/bjb-main-view.h b/src/views/bjb-main-view.h
new file mode 100644
index 0000000..6f82df2
--- /dev/null
+++ b/src/views/bjb-main-view.h
@@ -0,0 +1,46 @@
+/* bjb-main-view.h
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_MAIN_VIEW (bjb_main_view_get_type ())
+
+typedef enum
+{
+ BJB_VIEW_TYPE_GRID,
+ BJB_VIEW_TYPE_LIST
+} BjbViewType;
+
+G_DECLARE_FINAL_TYPE (BjbMainView, bjb_main_view, BJB, MAIN_VIEW, GtkStack)
+
+BjbMainView *bjb_main_view_new                (void);
+gboolean     bjb_main_view_get_selection_mode (BjbMainView *self);
+void         bjb_main_view_set_selection_mode (BjbMainView *self,
+                                               gboolean     selection_mode);
+void         bjb_main_view_set_store          (BjbMainView  *self,
+                                               GtkTreeStore *store);
+void         bjb_main_view_set_view           (BjbMainView *self,
+                                               BjbViewType  view_type);
+
+G_END_DECLS


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