[gnome-calendar/wip/pandusonu/week-view: 7/31] week-view: make it a template class



commit aec43bf19fc36bae7630d14c2cbf964f3ad1f08e
Author: pandusonu2 <pandu sonu yahoo com>
Date:   Tue Jun 7 03:21:11 2016 +0530

    week-view: make it a template class
    
    Made changes to make the week-view into a template class.

 data/Makefile.am             |    1 +
 data/calendar.gresource.xml  |    1 +
 data/ui/week-view.ui         |   28 ++++++++++
 src/gcal-window.c            |    1 +
 src/views/gcal-week-header.c |   21 +++++++-
 src/views/gcal-week-view.c   |  114 ++++++++++++++++++++++--------------------
 src/views/gcal-week-view.h   |   24 ++-------
 7 files changed, 115 insertions(+), 75 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index dfe7210..888b3a1 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -62,6 +62,7 @@ EXTRA_DIST=                     \
   ui/source-dialog.ui           \
   ui/time-selector.ui           \
   ui/week-header.ui             \
+  ui/week-view.ui               \
   ui/window.ui                  \
   ui/year-view.ui               \
   theme/gtk-styles.css          \
diff --git a/data/calendar.gresource.xml b/data/calendar.gresource.xml
index 1542e44..6b5397e 100644
--- a/data/calendar.gresource.xml
+++ b/data/calendar.gresource.xml
@@ -13,6 +13,7 @@
     <file alias="source-dialog.ui" compressed="true" preprocess="xml-stripblanks">ui/source-dialog.ui</file>
     <file alias="time-selector.ui" compressed="true" preprocess="xml-stripblanks">ui/time-selector.ui</file>
     <file alias="week-header.ui" compressed="true" preprocess="xml-stripblanks">ui/week-header.ui</file>
+    <file alias="week-view.ui" compressed="true" preprocess="xml-stripblanks">ui/week-view.ui</file>
     <file alias="window.ui" compressed="true" preprocess="xml-stripblanks">ui/window.ui</file>
     <file alias="year-view.ui" compressed="true" preprocess="xml-stripblanks">ui/year-view.ui</file>
     <file alias="gtk-styles.css" compressed="true">theme/gtk-styles.css</file>
diff --git a/data/ui/week-view.ui b/data/ui/week-view.ui
new file mode 100644
index 0000000..83a5357
--- /dev/null
+++ b/data/ui/week-view.ui
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<interface>
+  <template class="GcalWeekView" parent="GtkBox">
+    <property name="visible">True</property>
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GcalWeekHeader" id="header">
+        <property name="visible">True</property>
+        <property name="active-date" bind-source="GcalWeekView" bind-property="active-date" 
bind-flags="default|sync-create" />
+      </object>
+    </child>
+    <child>
+      <object class="GtkScrolledWindow">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkViewport">
+            <property name="visible">True</property>
+            <child>
+              <object class="GcalWeekGrid">
+                <property name="visible">True</property>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
\ No newline at end of file
diff --git a/src/gcal-window.c b/src/gcal-window.c
index 73e8f64..76ca5f2 100644
--- a/src/gcal-window.c
+++ b/src/gcal-window.c
@@ -437,6 +437,7 @@ update_current_date (GcalWindow *window)
   *(window->current_date) = icaltime_current_time_with_zone (gcal_manager_get_system_timezone 
(window->manager));
   *(window->current_date) = icaltime_set_timezone (window->current_date, gcal_manager_get_system_timezone 
(window->manager));
 
+  gcal_week_view_set_current_date (GCAL_WEEK_VIEW (window->week_view), window->current_date);
   gcal_month_view_set_current_date (GCAL_MONTH_VIEW (window->month_view), window->current_date);
   gcal_year_view_set_current_date (GCAL_YEAR_VIEW (window->year_view), window->current_date);
 
diff --git a/src/views/gcal-week-header.c b/src/views/gcal-week-header.c
index aa68706..6bc266a 100644
--- a/src/views/gcal-week-header.c
+++ b/src/views/gcal-week-header.c
@@ -41,6 +41,7 @@ struct _GcalWeekHeader
 
   gboolean          use_24h_format;
 
+  icaltimetype     *active_date;
   icaltimetype     *current_date;
 };
 
@@ -60,6 +61,12 @@ static gboolean       gcal_week_header_draw                 (GcalWeekHeader *sel
                                                              cairo_t        *cr,
                                                              GtkWidget      *widget);
 
+enum
+{
+  PROP_0,
+  PROP_ACTIVE_DATE
+};
+
 G_DEFINE_TYPE (GcalWeekHeader, gcal_week_header, GTK_TYPE_SCROLLED_WINDOW);
 
 static void
@@ -95,6 +102,10 @@ gcal_week_header_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_ACTIVE_DATE:
+      self->active_date = g_value_dup_boxed (value);
+      return;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -155,7 +166,7 @@ gcal_week_header_draw (GcalWeekHeader *self,
   pango_font_description_set_weight (bold_font, PANGO_WEIGHT_SEMIBOLD);
   pango_layout_set_font_description (layout, bold_font);
 
-  start_of_week = gcal_week_view_get_initial_date (GCAL_VIEW (widget));
+  start_of_week = self->active_date;
   current_cell = icaltime_day_of_week (*(self->current_date)) - 1;
   current_cell = (7 + current_cell - self->first_weekday) % 7;
 
@@ -240,6 +251,14 @@ gcal_week_header_class_init (GcalWeekHeaderClass *kclass)
 
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/week-header.ui");
 
+  g_object_class_install_property (object_class,
+                                   PROP_ACTIVE_DATE,
+                                   g_param_spec_boxed ("active-date",
+                                                       "Date",
+                                                       "The active selected date",
+                                                       ICAL_TIME_TYPE,
+                                                       G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
   gtk_widget_class_bind_template_child (widget_class, GcalWeekHeader, grid);
 
   gtk_widget_class_bind_template_callback (widget_class, gcal_week_header_draw);
diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c
index 93a06d6..4fe8316 100644
--- a/src/views/gcal-week-view.c
+++ b/src/views/gcal-week-view.c
@@ -54,7 +54,7 @@ struct _GcalWeekViewChild
 
 typedef struct _GcalWeekViewChild GcalWeekViewChild;
 
-typedef struct
+struct _GcalWeekView
 {
   GtkBox          parent;
 
@@ -71,10 +71,11 @@ typedef struct
 
   /* property */
   icaltimetype   *date;
+  icaltimetype   *current_date;
   GcalManager    *manager; /* weak referenced */
 
   gint            clicked_cell;
-} GcalWeekViewPrivate;
+};
 
 static void           gcal_week_view_constructed           (GObject        *object);
 
@@ -94,8 +95,7 @@ static icaltimetype*  gcal_week_view_get_final_date        (GcalView       *view
 
 static void           gcal_view_interface_init             (GcalViewInterface *iface);
 
-G_DEFINE_TYPE_WITH_CODE (GcalWeekView, gcal_week_view, GCAL_TYPE_SUBSCRIBER_VIEW,
-                         G_ADD_PRIVATE (GcalWeekView)
+G_DEFINE_TYPE_WITH_CODE (GcalWeekView, gcal_week_view, GTK_TYPE_BOX,
                          G_IMPLEMENT_INTERFACE (GCAL_TYPE_VIEW, gcal_view_interface_init));
 
 /*
@@ -195,12 +195,9 @@ gcal_week_view_get_sidebar_width (GtkWidget *widget)
 static void
 gcal_week_view_class_init (GcalWeekViewClass *klass)
 {
-  GtkWidgetClass *widget_class;
-  GObjectClass *object_class;
-
-  widget_class = GTK_WIDGET_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
-  object_class = G_OBJECT_CLASS (klass);
   object_class->constructed = gcal_week_view_constructed;
   object_class->finalize = gcal_week_view_finalize;
   object_class->set_property = gcal_week_view_set_property;
@@ -208,13 +205,16 @@ gcal_week_view_class_init (GcalWeekViewClass *klass)
 
   g_object_class_override_property (object_class,
                                     PROP_DATE, "active-date");
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/week-view.ui");
+
+  gtk_widget_class_set_css_name (widget_class, "calendar-view");
 }
 
 static void
 gcal_week_view_init (GcalWeekView *self)
 {
-  gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
-  gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (self)), "calendar-view");
+  gtk_widget_init_template (GTK_WIDGET (self));
 }
 
 static void
@@ -229,10 +229,10 @@ gcal_view_interface_init (GcalViewInterface *iface)
 static void
 gcal_week_view_constructed (GObject *object)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
 
   g_return_if_fail (GCAL_IS_WEEK_VIEW (object));
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW (object));
+  self = GCAL_WEEK_VIEW (object);
 
   if (G_OBJECT_CLASS (gcal_week_view_parent_class)->constructed != NULL)
       G_OBJECT_CLASS (gcal_week_view_parent_class)->constructed (object);
@@ -241,12 +241,11 @@ gcal_week_view_constructed (GObject *object)
 static void
 gcal_week_view_finalize (GObject       *object)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
 
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW (object));
+  self = GCAL_WEEK_VIEW (object);
 
-  if (priv->date != NULL)
-    g_free (priv->date);
+  g_clear_pointer (&self->date, g_free);
 
   /* Chain up to parent's finalize() method. */
   G_OBJECT_CLASS (gcal_week_view_parent_class)->finalize (object);
@@ -258,9 +257,9 @@ gcal_week_view_set_property (GObject       *object,
                              const GValue  *value,
                              GParamSpec    *pspec)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
 
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW (object));
+  self = GCAL_WEEK_VIEW (object);
 
   switch (property_id)
     {
@@ -270,13 +269,11 @@ gcal_week_view_set_property (GObject       *object,
         icaltimetype *date;
         icaltimezone* default_zone;
 
-        if (priv->date != NULL)
-          g_free (priv->date);
+        g_clear_pointer (&self->date, g_free);
 
-        priv->date = g_value_dup_boxed (value);
+        self->date = g_value_dup_boxed (value);
 
-        default_zone =
-          gcal_manager_get_system_timezone (priv->manager);
+        default_zone = gcal_manager_get_system_timezone (self->manager);
         date = gcal_view_get_initial_date (GCAL_VIEW (object));
         range_start = icaltime_as_timet_with_zone (*date,
                                                    default_zone);
@@ -285,13 +282,14 @@ gcal_week_view_set_property (GObject       *object,
         range_end = icaltime_as_timet_with_zone (*date,
                                                  default_zone);
         g_free (date);
-        gcal_manager_set_subscriber (priv->manager,
+        gcal_manager_set_subscriber (self->manager,
                                      E_CAL_DATA_MODEL_SUBSCRIBER (object),
                                      range_start,
                                      range_end);
         gtk_widget_queue_draw (GTK_WIDGET (object));
         break;
       }
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -300,20 +298,21 @@ gcal_week_view_set_property (GObject       *object,
 
 static void
 gcal_week_view_get_property (GObject       *object,
-                              guint          property_id,
-                              GValue        *value,
-                              GParamSpec    *pspec)
+                             guint          property_id,
+                             GValue        *value,
+                             GParamSpec    *pspec)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
 
   g_return_if_fail (GCAL_IS_WEEK_VIEW (object));
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW (object));
+  self = GCAL_WEEK_VIEW (object);
 
   switch (property_id)
     {
     case PROP_DATE:
-      g_value_set_boxed (value, priv->date);
+      g_value_set_boxed (value, self->date);
       break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -331,20 +330,20 @@ gcal_week_view_get_property (GObject       *object,
 icaltimetype*
 gcal_week_view_get_initial_date (GcalView *view)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
   icaltimetype *new_date;
 
   g_return_val_if_fail (GCAL_IS_WEEK_VIEW (view), NULL);
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW(view));
+  self = GCAL_WEEK_VIEW(view);
   new_date = g_new0 (icaltimetype, 1);
-  *new_date = icaltime_from_day_of_year (
-                  icaltime_start_doy_week (*(priv->date), priv->first_weekday + 1),
-                  priv->date->year);
+  *new_date = icaltime_from_day_of_year (icaltime_start_doy_week (*(self->date),
+                                                                  self->first_weekday + 1),
+                                         self->date->year);
   new_date->is_date = 0;
   new_date->hour = 0;
   new_date->minute = 0;
   new_date->second = 0;
-  *new_date = icaltime_set_timezone (new_date, priv->date->zone);
+  *new_date = icaltime_set_timezone (new_date, self->date->zone);
   return new_date;
 }
 
@@ -358,20 +357,20 @@ gcal_week_view_get_initial_date (GcalView *view)
 static icaltimetype*
 gcal_week_view_get_final_date (GcalView *view)
 {
-  GcalWeekViewPrivate *priv;
+  GcalWeekView *self;
   icaltimetype *new_date;
 
   g_return_val_if_fail (GCAL_IS_WEEK_VIEW (view), NULL);
-  priv = gcal_week_view_get_instance_private (GCAL_WEEK_VIEW(view));
+  self = GCAL_WEEK_VIEW(view);
   new_date = g_new0 (icaltimetype, 1);
-  *new_date = icaltime_from_day_of_year (
-                  icaltime_start_doy_week (*(priv->date), priv->first_weekday + 1) + 6,
-                  priv->date->year);
+  *new_date = icaltime_from_day_of_year (icaltime_start_doy_week (*(self->date),
+                                                                  self->first_weekday + 1) + 6,
+                                         self->date->year);
   new_date->is_date = 0;
   new_date->hour = 23;
   new_date->minute = 59;
   new_date->second = 0;
-  *new_date = icaltime_set_timezone (new_date, priv->date->zone);
+  *new_date = icaltime_set_timezone (new_date, self->date->zone);
   return new_date;
 }
 
@@ -390,12 +389,12 @@ gcal_week_view_new (void)
 }
 
 void
-gcal_week_view_set_manager (GcalWeekView *week_view,
+gcal_week_view_set_manager (GcalWeekView *self,
                             GcalManager  *manager)
 {
-  GcalWeekViewPrivate *priv = gcal_week_view_get_instance_private (week_view);
+  g_return_if_fail (GCAL_IS_WEEK_VIEW (self));
 
-  priv->manager = manager;
+  self->manager = manager;
 }
 
 /**
@@ -407,13 +406,12 @@ gcal_week_view_set_manager (GcalWeekView *week_view,
  * 0 for Sunday, 1 for Monday and so on.
  **/
 void
-gcal_week_view_set_first_weekday (GcalWeekView *view,
+gcal_week_view_set_first_weekday (GcalWeekView *self,
                                   gint          day_nr)
 {
-  GcalWeekViewPrivate *priv;
+  g_return_if_fail (GCAL_IS_WEEK_VIEW (self));
 
-  priv = gcal_week_view_get_instance_private (view);
-  priv->first_weekday = day_nr;
+  self->first_weekday = day_nr;
 }
 
 /**
@@ -424,11 +422,19 @@ gcal_week_view_set_first_weekday (GcalWeekView *view,
  * Whether the view will show time using 24h or 12h format
  **/
 void
-gcal_week_view_set_use_24h_format (GcalWeekView *view,
+gcal_week_view_set_use_24h_format (GcalWeekView *self,
                                    gboolean      use_24h)
 {
-  GcalWeekViewPrivate *priv;
+  g_return_if_fail (GCAL_IS_WEEK_VIEW (self));
 
-  priv = gcal_week_view_get_instance_private (view);
-  priv->use_24h_format = use_24h;
+  self->use_24h_format = use_24h;
 }
+
+void
+gcal_week_view_set_current_date (GcalWeekView *self,
+                                 icaltimetype *current_date)
+{
+  g_return_if_fail (GCAL_IS_WEEK_VIEW (self));
+
+  self->current_date = current_date;
+}
\ No newline at end of file
diff --git a/src/views/gcal-week-view.h b/src/views/gcal-week-view.h
index 01bcf61..a5384f9 100644
--- a/src/views/gcal-week-view.h
+++ b/src/views/gcal-week-view.h
@@ -22,31 +22,13 @@
 
 #include "gcal-manager.h"
 #include "gcal-view.h"
-#include "gcal-subscriber-view.h"
 
 G_BEGIN_DECLS
 
-#define GCAL_TYPE_WEEK_VIEW                       (gcal_week_view_get_type ())
-#define GCAL_WEEK_VIEW(obj)                       (G_TYPE_CHECK_INSTANCE_CAST((obj), GCAL_TYPE_WEEK_VIEW, 
GcalWeekView))
-#define GCAL_WEEK_VIEW_CLASS(klass)               (G_TYPE_CHECK_CLASS_CAST((klass), GCAL_TYPE_WEEK_VIEW, 
GcalWeekViewClass))
-#define GCAL_IS_WEEK_VIEW(obj)                    (G_TYPE_CHECK_INSTANCE_TYPE((obj), GCAL_TYPE_WEEK_VIEW))
-#define GCAL_IS_WEEK_VIEW_CLASS(klass)            (G_TYPE_CHECK_CLASS_TYPE((klass), GCAL_TYPE_WEEK_VIEW))
-#define GCAL_WEEK_VIEW_GET_CLASS(obj)             (G_TYPE_INSTANCE_GET_CLASS((obj), GCAL_TYPE_WEEK_VIEW, 
GcalWeekViewClass))
+#define GCAL_TYPE_WEEK_VIEW (gcal_week_view_get_type ())
 
-typedef struct _GcalWeekView                       GcalWeekView;
-typedef struct _GcalWeekViewClass                  GcalWeekViewClass;
+G_DECLARE_FINAL_TYPE (GcalWeekView, gcal_week_view, GCAL, WEEK_VIEW, GtkBox)
 
-struct _GcalWeekView
-{
-  GcalSubscriberView parent;
-};
-
-struct _GcalWeekViewClass
-{
-  GcalSubscriberViewClass parent_class;
-};
-
-GType          gcal_week_view_get_type           (void);
 GtkWidget*     gcal_week_view_new                (void);
 void           gcal_week_view_set_manager        (GcalWeekView *view,
                                                   GcalManager  *manager);
@@ -54,6 +36,8 @@ void           gcal_week_view_set_first_weekday  (GcalWeekView *view,
                                                   gint          day_nr);
 void           gcal_week_view_set_use_24h_format (GcalWeekView *view,
                                                   gboolean      use_24h);
+void           gcal_week_view_set_current_date   (GcalWeekView *week_header,
+                                                  icaltimetype *current_date);
 gint           gcal_week_view_get_sidebar_width  (GtkWidget *widget);
 icaltimetype*  gcal_week_view_get_initial_date   (GcalView *view);
 gint           gcal_week_view_get_start_grid_y   (GtkWidget *widget);


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