[gnome-calendar/gnome-3-20] year-view: Multiple Columns



commit 25b77142539d99825d7d053de58ca0bb46aec74e
Author: Isaque Galdino <igaldino gmail com>
Date:   Mon Apr 4 23:12:43 2016 -0300

    year-view: Multiple Columns
    
    Make year view more flexible being able to handle different number of
    columns.
    
    View was limited to only show months in a 4x3 grid which was causing
    issues when users were trying to use in different screens sizes.
    
    It was added code to implement a dynamic layout, so year view will
    adapt the number of months shown per row, depending on the view width.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753533

 data/ui/year-view.ui |   34 +++++++++++++-----
 src/gcal-year-view.c |   94 +++++++++++++++++++++++++------------------------
 2 files changed, 72 insertions(+), 56 deletions(-)
---
diff --git a/data/ui/year-view.ui b/data/ui/year-view.ui
index 25b3ff0..9206279 100644
--- a/data/ui/year-view.ui
+++ b/data/ui/year-view.ui
@@ -6,17 +6,31 @@
       <class name="year-view"/>
     </style>
     <child>
-      <object class="GtkDrawingArea" id="navigator">
+      <object class="GtkScrolledWindow">
         <property name="visible">True</property>
-        <property name="hexpand">True</property>
-        <property 
name="events">GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK|GDK_STRUCTURE_MASK</property>
-        <signal name="draw" handler="draw_navigator" object="GcalYearView" swapped="yes"/>
-        <signal name="button-press-event" handler="navigator_button_press_cb" object="GcalYearView" 
swapped="yes"/>
-        <signal name="button-release-event" handler="navigator_button_release_cb" object="GcalYearView" 
swapped="yes"/>
-        <signal name="motion-notify-event" handler="navigator_motion_notify_cb" object="GcalYearView" 
swapped="yes"/>
-        <style>
-          <class name="year-navigator"/>
-        </style>
+        <property name="shadow_type">none</property>
+        <property name="vexpand">True</property>
+        <child>
+          <object class="GtkViewport">
+            <property name="visible">True</property>
+            <property name="shadow_type">none</property>
+            <property name="vexpand">True</property>
+            <child>
+              <object class="GtkDrawingArea" id="navigator">
+                <property name="visible">True</property>
+                <property name="hexpand">True</property>
+                <property 
name="events">GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK|GDK_STRUCTURE_MASK</property>
+                <signal name="draw" handler="draw_navigator" object="GcalYearView" swapped="yes"/>
+                <signal name="button-press-event" handler="navigator_button_press_cb" object="GcalYearView" 
swapped="yes"/>
+                <signal name="button-release-event" handler="navigator_button_release_cb" 
object="GcalYearView" swapped="yes"/>
+                <signal name="motion-notify-event" handler="navigator_motion_notify_cb" 
object="GcalYearView" swapped="yes"/>
+                <style>
+                  <class name="year-navigator"/>
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
       </object>
       <packing>
         <property name="position">0</property>
diff --git a/src/gcal-year-view.c b/src/gcal-year-view.c
index 60b061c..69d8c8e 100644
--- a/src/gcal-year-view.c
+++ b/src/gcal-year-view.c
@@ -27,6 +27,7 @@
 
 #define NAVIGATOR_CELL_WIDTH (210 + 15)
 #define NAVIGATOR_CELL_HEIGHT 210
+#define NAVIGATOR_HEADER_HEIGHT 52
 #define SIDEBAR_PREFERRED_WIDTH 200
 #define VISUAL_CLUES_SIDE 3.0
 #define WEEK_NUMBER_MARGIN 3.0
@@ -67,6 +68,9 @@ struct _GcalYearView
 
   /* geometry info */
   GridData     *navigator_grid;
+  guint         number_of_columns;
+  guint         number_of_rows;
+
 
   /* state flags */
   gboolean      popover_mode;
@@ -547,7 +551,7 @@ calculate_day_month_for_coord (GcalYearView *year_view,
                                gint         *out_day,
                                gint         *out_month)
 {
-  gint row, column, i, sw, clicked_cell, day, month;
+  gint row, column, i, sw, clicked_cell, day, month, columns_or_rows;
   gdouble box_side;
 
   row = -1;
@@ -555,12 +559,13 @@ calculate_day_month_for_coord (GcalYearView *year_view,
   box_side = year_view->navigator_grid->box_side;
   sw = 1 - 2 * year_view->k;
 
-  /* y selection */
-  for (i = 0; i < 4 && ((row == -1) || (column == -1)); i++)
+  columns_or_rows = year_view->number_of_columns > year_view->number_of_rows ? year_view->number_of_columns 
: year_view->number_of_rows;
+
+  for (i = 0; i < columns_or_rows && ((row == -1) || (column == -1)); i++)
     {
       if (row == -1 &&
-          y > year_view->navigator_grid->coordinates[i * 4].y &&
-          y < year_view->navigator_grid->coordinates[i * 4].y + box_side * 7)
+          y > year_view->navigator_grid->coordinates[i * year_view->number_of_columns].y &&
+          y < year_view->navigator_grid->coordinates[i * year_view->number_of_columns].y + box_side * 7)
         {
           row = i;
         }
@@ -575,7 +580,7 @@ calculate_day_month_for_coord (GcalYearView *year_view,
   if (row == -1 || column == -1)
     return FALSE;
 
-  month = row * 4 + column;
+  month = row * year_view->number_of_columns + column;
   row = (y - (year_view->navigator_grid->coordinates[month].y + box_side)) / box_side;
   column = (x - (year_view->navigator_grid->coordinates[month].x + box_side * (1 - year_view->k))) / 
box_side;
   clicked_cell = row * 7 + column;
@@ -925,23 +930,23 @@ draw_navigator (GcalYearView *year_view,
   header_height = header_padding_top * 2 + layout_height;
   height = gtk_widget_get_allocated_height (widget) - header_height;
 
-  if (((width / 4.0) / 8.0) < ((height / 3.0) / 7.0))
-    box_side = (width / 4.0) / 8.0;
-  else
-    box_side = (height / 3.0) / 7.0;
+  box_side = NAVIGATOR_CELL_WIDTH / 8.0;
 
-  real_padding_left = (width - (8 * 4 * box_side)) / 5.0;
-  real_padding_top = (height - (7 * 3 * box_side)) / 4.0;
+  real_padding_left = (width - NAVIGATOR_CELL_WIDTH * year_view->number_of_columns) / 
(year_view->number_of_columns + 1);
+  real_padding_top = (height - NAVIGATOR_CELL_HEIGHT * year_view->number_of_rows) / 
(year_view->number_of_rows + 1);
+
+  if (real_padding_top < 0)
+    real_padding_top = 0;
 
   year_view->navigator_grid->box_side = box_side;
   weeks_counter = year_view->first_week_of_year;
   for (i = 0; i < 12; i++)
     {
-      gint row = i / 4;
-      gint column = year_view->k * 3 + sw * (i % 4);
+      gint row = i / year_view->number_of_columns;
+      gint column = year_view->k * (year_view->number_of_columns - 1) + sw * (i % 
year_view->number_of_columns);
 
-      year_view->navigator_grid->coordinates[i].x = (column + 1) * real_padding_left + column * box_side * 8;
-      year_view->navigator_grid->coordinates[i].y = (row + 1) * real_padding_top + row * box_side * 7 + 
header_height;
+      year_view->navigator_grid->coordinates[i].x = (column + 1) * real_padding_left + column * 
NAVIGATOR_CELL_WIDTH;
+      year_view->navigator_grid->coordinates[i].y = row * real_padding_top + row * NAVIGATOR_CELL_HEIGHT + 
header_height;
       draw_month_grid (year_view, widget, cr, i, &weeks_counter);
     }
 
@@ -1161,40 +1166,20 @@ gcal_year_view_get_preferred_width (GtkWidget *widget,
 {
   GcalYearView *year_view = GCAL_YEAR_VIEW (widget);
   GtkStyleContext *context;
-  gint padding_left;
+  gint padding_left, padding_right, hpadding;
 
   context = gtk_widget_get_style_context (year_view->navigator);
 
   gtk_style_context_get (context,
                          gtk_style_context_get_state (context),
-                         "padding-left", &padding_left, NULL);
+                         "padding-left", &padding_left,
+                         "padding-right", &padding_right, NULL);
 
+  hpadding = padding_left + padding_right;
   if (minimum != NULL)
-    *minimum = NAVIGATOR_CELL_WIDTH * 4 + padding_left * 8;
+    *minimum = hpadding + NAVIGATOR_CELL_WIDTH;
   if (natural != NULL)
-    *natural = NAVIGATOR_CELL_WIDTH * 4 + padding_left * 8 + SIDEBAR_PREFERRED_WIDTH;
-}
-
-static void
-gcal_year_view_get_preferred_height_for_width (GtkWidget *widget,
-                                               gint       width,
-                                               gint      *minimum,
-                                               gint      *natural)
-{
-  GcalYearView *year_view = GCAL_YEAR_VIEW (widget);
-  GtkStyleContext *context;
-  gint padding_top;
-
-  context = gtk_widget_get_style_context (year_view->navigator);
-
-  gtk_style_context_get (context,
-                         gtk_style_context_get_state (context),
-                         "padding-top", &padding_top, NULL);
-
-  if (minimum != NULL)
-    *minimum = NAVIGATOR_CELL_HEIGHT * 3 + padding_top * 6;
-  if (natural != NULL)
-    *natural = NAVIGATOR_CELL_HEIGHT * 3 + padding_top * 6;
+    *natural = hpadding + NAVIGATOR_CELL_WIDTH + SIDEBAR_PREFERRED_WIDTH;
 }
 
 static void
@@ -1203,15 +1188,29 @@ gcal_year_view_size_allocate (GtkWidget     *widget,
 {
   GcalYearView *year_view = GCAL_YEAR_VIEW (widget);
   GtkStyleContext *context;
-  gint padding_left;
+  gint padding_left, padding_right, padding_top, padding_bottom, hpadding, vpadding;
 
   context = gtk_widget_get_style_context (widget);
   gtk_style_context_save (context);
   gtk_style_context_add_class (context, "year-navigator");
-  gtk_style_context_get (context, gtk_style_context_get_state (context), "padding-left", &padding_left, 
NULL);
+  gtk_style_context_get (context,
+                         gtk_style_context_get_state (context),
+                         "padding-left", &padding_left,
+                         "padding-right", &padding_right,
+                         "padding-top", &padding_top,
+                         "padding-bottom", &padding_bottom, NULL);
   gtk_style_context_restore (context);
 
-  year_view->popover_mode = (alloc->width < NAVIGATOR_CELL_WIDTH * 4 + padding_left * 8 + 
SIDEBAR_PREFERRED_WIDTH);
+  year_view->number_of_columns = (alloc->width - SIDEBAR_PREFERRED_WIDTH) / NAVIGATOR_CELL_WIDTH;
+  year_view->number_of_rows = ceil (12.0 / year_view->number_of_columns);
+
+  hpadding = padding_left + padding_right;
+  vpadding = padding_top + padding_bottom;
+  gtk_widget_set_size_request (year_view->navigator,
+                               hpadding + NAVIGATOR_CELL_WIDTH * year_view->number_of_columns,
+                               vpadding + NAVIGATOR_CELL_HEIGHT * year_view->number_of_rows + 
NAVIGATOR_HEADER_HEIGHT);
+
+  year_view->popover_mode = (alloc->width < padding_left + NAVIGATOR_CELL_WIDTH * 
year_view->number_of_columns + padding_right + SIDEBAR_PREFERRED_WIDTH);
   if (year_view->popover_mode && !gtk_widget_is_ancestor (year_view->events_sidebar, year_view->popover))
     {
       g_object_ref (year_view->sidebar);
@@ -1443,7 +1442,6 @@ gcal_year_view_class_init (GcalYearViewClass *klass)
   object_class->set_property = gcal_year_view_set_property;
 
   widget_class->get_preferred_width = gcal_year_view_get_preferred_width;
-  widget_class->get_preferred_height_for_width = gcal_year_view_get_preferred_height_for_width;
   widget_class->size_allocate = gcal_year_view_size_allocate;
   widget_class->direction_changed = gcal_year_view_direction_changed;
 
@@ -1506,6 +1504,10 @@ gcal_year_view_init (GcalYearView *self)
   g_settings_bind (self->shell_settings, "show-weekdate", self, "show-week-numbers", 
G_SETTINGS_BIND_DEFAULT);
   g_signal_connect_swapped (self->shell_settings, "changed::show-weekdate", G_CALLBACK 
(gtk_widget_queue_draw), self);
 
+  /* default number_of_columns and number_of_rows */
+  self->number_of_columns = 4;
+  self->number_of_rows = 3;
+
   gtk_list_box_set_header_func (GTK_LIST_BOX (self->events_sidebar), update_sidebar_headers, self, NULL);
   gtk_list_box_set_sort_func (GTK_LIST_BOX (self->events_sidebar), sidebar_sort_func, NULL, NULL);
 }


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