[gnome-calendar/wip/pandusonu/week-view: 27/31] week-view: fix the sync between sidebar width



commit 9c97cf556052f766bf59cb69bfb08ac167c7cc5f
Author: pandusonu2 <pandu sonu yahoo com>
Date:   Thu Jul 28 19:49:54 2016 +0530

    week-view: fix the sync between sidebar width
    
    Fix the width of the sidebar sync between the week view and week header.

 data/ui/week-view.ui         |    4 +-
 src/views/gcal-week-grid.c   |   12 ++---
 src/views/gcal-week-header.c |   46 +++++++++++++++++-----
 src/views/gcal-week-view.c   |   89 +++++++++++++++++++++++++++++++----------
 src/views/gcal-week-view.h   |    1 -
 5 files changed, 111 insertions(+), 41 deletions(-)
---
diff --git a/data/ui/week-view.ui b/data/ui/week-view.ui
index 39d48af..801e728 100644
--- a/data/ui/week-view.ui
+++ b/data/ui/week-view.ui
@@ -10,6 +10,7 @@
       <object class="GcalWeekHeader" id="header">
         <property name="visible">True</property>
         <property name="active-date" bind-source="GcalWeekView" bind-property="active-date" 
bind-flags="default" />
+        <property name="sidebar-width-offset" bind-source="GcalWeekView" 
bind-property="sidebar-width-offset" bind-flags="default" />
       </object>
     </child>
     <child>
@@ -23,10 +24,11 @@
                 <property name="visible">True</property>
                 <property name="orientation">vertical</property>
                 <child>
-                  <object class="GtkDrawingArea">
+                  <object class="GtkDrawingArea" id="hours_bar">
                     <property name="visible">True</property>
                     <property name="height-request">2568</property>
                     <signal name="draw" handler="gcal_week_view_draw_hours" object="GcalWeekView" 
swapped="yes" />
+                    <signal name="size-allocate" handler="gcal_week_view_hours_bar_size_allocate" 
object="GcalWeekView" swapped="yes" />
                   </object>
                 </child>
                 <!--<child>
diff --git a/src/views/gcal-week-grid.c b/src/views/gcal-week-grid.c
index 5327ced..a34c79a 100644
--- a/src/views/gcal-week-grid.c
+++ b/src/views/gcal-week-grid.c
@@ -148,7 +148,6 @@ gcal_week_grid_draw (GtkWidget *widget,
   gint i;
   gint width;
   gint height;
-  gdouble sidebar_width;
   gint current_cell;
 
   PangoLayout *layout;
@@ -167,7 +166,6 @@ gcal_week_grid_draw (GtkWidget *widget,
   pango_layout_set_font_description (layout, font_desc);
   gdk_cairo_set_source_rgba (cr, &color);
 
-  sidebar_width = gcal_week_view_get_sidebar_width (widget);
   width = gtk_widget_get_allocated_width (widget);
   height = gtk_widget_get_allocated_height (widget);
 
@@ -177,9 +175,9 @@ gcal_week_grid_draw (GtkWidget *widget,
   gtk_style_context_save (context);
   gtk_style_context_add_class (context, "current");
   gtk_render_background (context, cr,
-                         ((width - sidebar_width)/ 7.0) * current_cell + sidebar_width,
+                         (width / 7.0) * current_cell,
                          0,
-                         ((width  - sidebar_width)/ 7.0),
+                         (width / 7.0),
                          height);
   gtk_style_context_remove_class (context, "current");
   gtk_style_context_restore (context);
@@ -187,7 +185,7 @@ gcal_week_grid_draw (GtkWidget *widget,
   for (i = 0; i < 7; i++)
     {
       cairo_move_to (cr,
-                     sidebar_width + ((width - sidebar_width) / 7) * i + 0.4,
+                     ((width) / 7) * i + 0.4,
                      0);
       cairo_rel_line_to (cr, 0, height);
     }
@@ -203,8 +201,8 @@ gcal_week_grid_draw (GtkWidget *widget,
   cairo_set_dash (cr, dashed, 2, 0);
   for (i = 0; i < 24; i++)
     {
-      cairo_move_to (cr, sidebar_width, (height / 24) * i + (height / 48) + 0.4);
-      cairo_rel_line_to (cr, width - sidebar_width, 0);
+      cairo_move_to (cr, 0, (height / 24) * i + (height / 48) + 0.4);
+      cairo_rel_line_to (cr, width, 0);
     }
 
   cairo_stroke (cr);
diff --git a/src/views/gcal-week-header.c b/src/views/gcal-week-header.c
index 4038c0e..93bb8e3 100644
--- a/src/views/gcal-week-header.c
+++ b/src/views/gcal-week-header.c
@@ -54,6 +54,8 @@ struct _GcalWeekHeader
 
   gint              first_weekday;
 
+  gint              sidebar_width_offset;
+
   /*
    * Used for checking if the header is in collapsed state or expand state
    * false is collapse state true is expand state
@@ -110,7 +112,8 @@ static gboolean       gcal_week_header_draw                 (GcalWeekHeader *sel
 enum
 {
   PROP_0,
-  PROP_ACTIVE_DATE
+  PROP_ACTIVE_DATE,
+  PROP_SIDEBAR_WIDTH
 };
 
 G_DEFINE_TYPE (GcalWeekHeader, gcal_week_header, GTK_TYPE_GRID);
@@ -527,6 +530,10 @@ gcal_week_header_get_property (GObject    *object,
       g_value_set_boxed (value, self->active_date);
       return;
 
+    case PROP_SIDEBAR_WIDTH:
+      g_value_set_int (value, self->sidebar_width_offset);
+      return;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -554,6 +561,13 @@ gcal_week_header_set_property (GObject      *object,
 
       return;
 
+    case PROP_SIDEBAR_WIDTH:
+      self->sidebar_width_offset = MAX (g_value_get_int (value),
+                                        gtk_widget_get_allocated_width (self->expand_button));
+
+      gtk_widget_queue_draw (self->draw_area);
+      return;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -568,14 +582,19 @@ gcal_week_header_size_allocate (GtkWidget     *widget,
   GtkStateFlags state;
   GtkAllocation draw_alloc;
 
-  gdouble sidebar_width, cell_width, button_width;
+  gint sidebar_width, cell_width;
 
   PangoFontDescription *bold_font;
 
+  if (gtk_widget_get_allocated_width (self->expand_button) > self->sidebar_width_offset)
+    {
+      self->sidebar_width_offset = gtk_widget_get_allocated_width (self->expand_button);
+      g_object_notify (G_OBJECT (self), "sidebar-width-offset");
+    }
+
   context = gtk_widget_get_style_context (self->draw_area);
   state = gtk_widget_get_state_flags (self->draw_area);
-  sidebar_width = gcal_week_view_get_sidebar_width (self->draw_area);
-  button_width = gtk_widget_get_allocated_width (self->expand_button);
+  sidebar_width = self->sidebar_width_offset;
 
   gtk_widget_get_allocation (self->draw_area, &draw_alloc);
 
@@ -585,10 +604,10 @@ gcal_week_header_size_allocate (GtkWidget     *widget,
   pango_font_description_set_weight (bold_font, PANGO_WEIGHT_SEMIBOLD);
 
   gtk_widget_set_margin_start (self->scrolledwindow,
-                               gcal_week_view_get_sidebar_width (self->draw_area) + 1 - button_width);
+                               self->sidebar_width_offset - gtk_widget_get_allocated_width 
(self->expand_button) + 4);
 
   gtk_widget_set_margin_end (self->scrolledwindow,
-                             gtk_widget_get_allocated_width (self->draw_area) - cell_width * 7 - 
sidebar_width + 7);
+                             gtk_widget_get_allocated_width (self->draw_area) - cell_width * 7 - 
sidebar_width);
 
   gtk_widget_set_margin_top (self->scrolledwindow,
                              (4 * pango_font_description_get_size (bold_font)) / PANGO_SCALE);
@@ -637,7 +656,7 @@ gcal_week_header_draw (GcalWeekHeader *self,
   current_cell = icaltime_day_of_week (*(self->active_date)) - 1;
   current_cell = (7 + current_cell - self->first_weekday) % 7;
 
-  sidebar_width = gcal_week_view_get_sidebar_width (widget);
+  sidebar_width = self->sidebar_width_offset;
   cell_width = (alloc.width - sidebar_width) / 7;
   pango_layout_get_pixel_size (layout, NULL, &font_height);
 
@@ -673,7 +692,7 @@ gcal_week_header_draw (GcalWeekHeader *self,
       pango_layout_set_font_description (layout, bold_font);
       pango_layout_set_text (layout, weekday_date, -1);
       cairo_move_to (cr,
-                     padding.left + cell_width * i + sidebar_width,
+                     padding.left + cell_width * i + sidebar_width + 3,
                      font_height + padding.bottom);
       pango_cairo_show_layout (cr,layout);
 
@@ -689,7 +708,7 @@ gcal_week_header_draw (GcalWeekHeader *self,
       pango_layout_set_font_description (layout, bold_font);
       pango_layout_set_text (layout, weekday_abv, -1);
       cairo_move_to (cr,
-                     padding.left + cell_width * i + sidebar_width,
+                     padding.left + cell_width * i + sidebar_width + 3,
                      0);
       pango_cairo_show_layout (cr, layout);
 
@@ -698,7 +717,7 @@ gcal_week_header_draw (GcalWeekHeader *self,
       /* Draws the lines after each day of the week */
       cairo_save (cr);
       cairo_move_to (cr,
-                     cell_width * i + sidebar_width - 3,
+                     cell_width * i + sidebar_width,
                      font_height + padding.bottom + 3);
       cairo_set_line_width (cr, 0.25);
       cairo_rel_line_to (cr, 0.0, gtk_widget_get_allocated_height (self->draw_area));
@@ -759,6 +778,13 @@ gcal_week_header_class_init (GcalWeekHeaderClass *kclass)
                                                        "The active selected date",
                                                        ICAL_TIME_TYPE,
                                                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+  g_object_class_install_property (object_class,
+                                   PROP_SIDEBAR_WIDTH,
+                                   g_param_spec_int ("sidebar-width-offset",
+                                                     "Sidebar Width",
+                                                     "The width of the sidebar",
+                                                     G_MININT, G_MAXINT, 10,
+                                                     G_PARAM_READWRITE));
 
   gtk_widget_class_bind_template_child (widget_class, GcalWeekHeader, grid);
   gtk_widget_class_bind_template_child (widget_class, GcalWeekHeader, month_label);
diff --git a/src/views/gcal-week-view.c b/src/views/gcal-week-view.c
index c6b1e95..7f44966 100644
--- a/src/views/gcal-week-view.c
+++ b/src/views/gcal-week-view.c
@@ -40,7 +40,8 @@ static const double dashed [] =
 enum
 {
   PROP_0,
-  PROP_DATE
+  PROP_DATE,
+  PROP_SIDEBAR_WIDTH
 };
 
 struct _GcalWeekView
@@ -48,6 +49,7 @@ struct _GcalWeekView
   GtkBox          parent;
 
   GtkWidget      *header;
+  GtkWidget      *hours_bar;
 
   /*
    * first day of the week according to user locale, being
@@ -55,6 +57,8 @@ struct _GcalWeekView
    */
   gint            first_weekday;
 
+  gint            sidebar_width_offset;
+
   /*
    * clock format from GNOME desktop settings
    */
@@ -168,45 +172,41 @@ gcal_week_view_get_final_date (GcalView *view)
   return new_date;
 }
 
-gint
+static gint
 gcal_week_view_get_sidebar_width (GtkWidget *widget)
 {
   GtkStyleContext *context;
+  GtkStateFlags state;
   GtkBorder padding;
 
   PangoLayout *layout;
   PangoFontDescription *font_desc;
-  gint mid_width;
-  gint noon_width;
-  gint hours_width;
-  gint sidebar_width;
+
+  gint hours_12_width, hours_24_width, sidebar_width;
 
   context = gtk_widget_get_style_context (widget);
+  state = gtk_style_context_get_state (context);
 
-  gtk_style_context_get_padding (
-      gtk_widget_get_style_context (widget),
-      gtk_widget_get_state_flags (widget),
-      &padding);
+  gtk_style_context_save (context);
+  gtk_style_context_add_class (context, "hours");
 
-  layout = pango_layout_new (gtk_widget_get_pango_context (widget));
-  gtk_style_context_get (context,
-                         gtk_widget_get_state_flags(widget),
+  gtk_style_context_get (context, state,
                          "font", &font_desc,
                          NULL);
+  gtk_style_context_get_padding (context, state, &padding);
+
+  layout = pango_layout_new (gtk_widget_get_pango_context (widget));
   pango_layout_set_font_description (layout, font_desc);
 
-  pango_layout_set_text (layout, _("Midnight"), -1);
-  pango_layout_get_pixel_size (layout, &mid_width, NULL);
+  pango_layout_set_text (layout, _("00 AM"), -1);
+  pango_layout_get_pixel_size (layout, &hours_12_width, NULL);
 
-  pango_layout_set_text (layout, _("Noon"), -1);
-  pango_layout_get_pixel_size (layout, &noon_width, NULL);
+  pango_layout_set_text (layout, _("00:00"), -1);
+  pango_layout_get_pixel_size (layout, &hours_24_width, NULL);
 
-  pango_layout_set_text (layout, _("00:00 PM"), -1);
-  pango_layout_get_pixel_size (layout, &hours_width, NULL);
+  sidebar_width = MAX (hours_12_width, hours_24_width) + padding.left + padding.right;
 
-  sidebar_width = noon_width > mid_width ? noon_width : mid_width;
-  sidebar_width = sidebar_width > hours_width ? 0 : hours_width;
-  sidebar_width += padding.left + padding.right;
+  gtk_style_context_restore (context);
 
   pango_font_description_free (font_desc);
   g_object_unref (layout);
@@ -287,6 +287,25 @@ gcal_week_view_thaw (ECalDataModelSubscriber *subscriber)
 {
 }
 
+static void
+gcal_week_view_hours_bar_size_allocate (GtkWidget     *widget,
+                                        GtkAllocation *alloc)
+{
+  GcalWeekView *self;
+
+  self = GCAL_WEEK_VIEW (widget);
+
+  if (gcal_week_view_get_sidebar_width (GTK_WIDGET (self)) > self->sidebar_width_offset)
+    {
+      self->sidebar_width_offset = gcal_week_view_get_sidebar_width (GTK_WIDGET (self));
+      g_object_notify (G_OBJECT (self), "sidebar-width-offset");
+    }
+
+  gtk_widget_set_size_request (self->hours_bar, self->sidebar_width_offset, 2568);
+
+  GTK_WIDGET_CLASS (gcal_week_view_parent_class)->size_allocate (widget, alloc);
+}
+
 static gboolean
 gcal_week_view_draw_hours (GcalWeekView *self,
                            cairo_t      *cr,
@@ -350,6 +369,9 @@ gcal_week_view_draw_hours (GcalWeekView *self,
 
   cairo_set_line_width (cr, 0.65);
 
+  cairo_move_to (cr, self->sidebar_width_offset, 0);
+  cairo_rel_line_to (cr, 0, height);
+
   /* Draws the horizontal complete lines */
   for (i = 0; i < 24; i++)
     {
@@ -389,12 +411,21 @@ gcal_week_view_class_init (GcalWeekViewClass *klass)
 
   g_object_class_override_property (object_class,
                                     PROP_DATE, "active-date");
+  g_object_class_install_property (object_class,
+                                   PROP_SIDEBAR_WIDTH,
+                                   g_param_spec_int ("sidebar-width-offset",
+                                                     "Sidebar Width",
+                                                     "The width of the sidebar",
+                                                     G_MININT, G_MAXINT, 10,
+                                                     G_PARAM_READWRITE));
 
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/week-view.ui");
 
   gtk_widget_class_bind_template_child (widget_class, GcalWeekView, header);
+  gtk_widget_class_bind_template_child (widget_class, GcalWeekView, hours_bar);
 
   gtk_widget_class_bind_template_callback (widget_class, gcal_week_view_draw_hours);
+  gtk_widget_class_bind_template_callback (widget_class, gcal_week_view_hours_bar_size_allocate);
 
   gtk_widget_class_set_css_name (widget_class, "calendar-view");
 }
@@ -402,6 +433,8 @@ gcal_week_view_class_init (GcalWeekViewClass *klass)
 static void
 gcal_week_view_init (GcalWeekView *self)
 {
+  self->sidebar_width_offset = 10;
+
   gtk_widget_init_template (GTK_WIDGET (self));
 }
 
@@ -488,6 +521,14 @@ gcal_week_view_set_property (GObject       *object,
         break;
       }
 
+    case PROP_SIDEBAR_WIDTH:
+      {
+        self->sidebar_width_offset = g_value_get_int (value);
+
+        gtk_widget_set_size_request (self->hours_bar, self->sidebar_width_offset, 2568);
+        break;
+      }
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -511,6 +552,10 @@ gcal_week_view_get_property (GObject       *object,
       g_value_set_boxed (value, self->date);
       break;
 
+    case PROP_SIDEBAR_WIDTH:
+      g_value_set_int (value, self->sidebar_width_offset);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
diff --git a/src/views/gcal-week-view.h b/src/views/gcal-week-view.h
index 14ef5d5..0e9cc57 100644
--- a/src/views/gcal-week-view.h
+++ b/src/views/gcal-week-view.h
@@ -38,7 +38,6 @@ 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);
 
 G_END_DECLS
 


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