]> saetta.ns0.it Git - libgtkcalendarex/commitdiff
View type switching.
authorAndrea Zagli <azagli@libero.it>
Mon, 1 Nov 2010 11:49:53 +0000 (12:49 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 1 Nov 2010 11:49:53 +0000 (12:49 +0100)
src/gtkcalex.c
src/gtkcalex.h
src/gtkcalexviewday.c
src/gtkcalexviewday.h
tests/test.c
tests/test.ui

index 67a18a477f068d684db860b641d6fe4c48a5ae9e..b9933fc1862b4a53b9b68c40e0fc1298f3dbde20 100644 (file)
@@ -23,6 +23,9 @@
 #include <goocanvas.h>
 
 #include "gtkcalex.h"
+#include "gtkcalexviewday.h"
+#include "gtkcalexviewweek.h"
+#include "gtkcalexviewmonth.h"
 
 enum
 {
@@ -59,7 +62,7 @@ static void gtk_calex_size_request (GtkWidget *widget,
 static void gtk_calex_size_allocate (GtkWidget *widget,
                                      GtkAllocation *allocation);
 
-static void gtk_calex_draw_day (GtkCalEx *gtk_calex);
+static void gtk_calex_clean_canvas (GtkCalEx *gtk_calex);
 
 
 #define GTK_CALEX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_CALEX, GtkCalExPrivate))
@@ -75,6 +78,8 @@ struct _GtkCalExPrivate
 
                GtkCalExViewType view_type;
                GDate *date;
+
+               GObject *view;
        };
 
 G_DEFINE_TYPE (GtkCalEx, gtk_calex, GTK_TYPE_BIN)
@@ -112,6 +117,8 @@ gtk_calex_init (GtkCalEx *gtk_calex)
        GTK_WIDGET_UNSET_FLAGS (gtk_calex, GTK_NO_WINDOW);
 
        priv->scrolledw = gtk_scrolled_window_new (NULL, NULL);
+       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolledw),
+                                       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
        gtk_container_add (GTK_CONTAINER (gtk_calex), priv->scrolledw);
 
        priv->goo_canvas = goo_canvas_new ();
@@ -121,13 +128,13 @@ gtk_calex_init (GtkCalEx *gtk_calex)
 
        priv->goo_canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->goo_canvas));
 
-       priv->view_type = GTK_CALEX_VIEW_TYPE_DAY;
-
        priv->date = g_date_new ();
        g_get_current_time (&tval);
        g_date_set_time_val (priv->date, &tval);
 
-       gtk_calex_draw_day (gtk_calex);
+       priv->view = NULL;
+       priv->view_type = -1;
+       gtk_calex_set_view_type (gtk_calex, GTK_CALEX_VIEW_TYPE_DAY);
 }
 
 static void
@@ -202,7 +209,68 @@ gtk_calex_set_view_type (GtkCalEx *calex, GtkCalExViewType type)
 
        GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (calex);
 
+       if (priv->view_type == type) return;
+
+       gtk_calex_clean_canvas (calex);
+
        priv->view_type = type;
+
+       switch (priv->view_type)
+               {
+                       case GTK_CALEX_VIEW_TYPE_DAY:
+                               if (priv->view != NULL)
+                                       {
+                                               g_object_unref (priv->view);
+                                               priv->view = NULL;
+                                       }
+                               GtkCalExViewDay *vd = gtk_calex_view_day_new (priv->goo_canvas_root, priv->date);
+                               priv->view = G_OBJECT (vd);
+                               break;
+
+                       case GTK_CALEX_VIEW_TYPE_WORK_WEEK:
+                               if (priv->view != NULL)
+                                       {
+                                               g_object_unref (priv->view);
+                                               priv->view = NULL;
+                                       }
+                               GtkCalExViewDay *vww = gtk_calex_view_day_new (priv->goo_canvas_root, priv->date);
+                               priv->view = G_OBJECT (vww);
+                               break;
+
+                       case GTK_CALEX_VIEW_TYPE_WEEK:
+                               if (priv->view != NULL)
+                                       {
+                                               g_object_unref (priv->view);
+                                               priv->view = NULL;
+                                       }
+                               GtkCalExViewDay *vw = gtk_calex_view_day_new (priv->goo_canvas_root, priv->date);
+                               priv->view = G_OBJECT (vw);
+                               break;
+
+                       case GTK_CALEX_VIEW_TYPE_MONTH:
+                               if (priv->view != NULL)
+                                       {
+                                               g_object_unref (priv->view);
+                                               priv->view = NULL;
+                                       }
+                               GtkCalExViewDay *vm = gtk_calex_view_day_new (priv->goo_canvas_root, priv->date);
+                               priv->view = G_OBJECT (vm);
+                               break;
+
+                       case GTK_CALEX_VIEW_TYPE_LIST:
+                               if (priv->view != NULL)
+                                       {
+                                               g_object_unref (priv->view);
+                                               priv->view = NULL;
+                                       }
+                               GtkCalExViewDay *vl = gtk_calex_view_day_new (priv->goo_canvas_root, priv->date);
+                               priv->view = G_OBJECT (vl);
+                               break;
+
+                       default:
+                               g_warning ("View type «%d» not available.", priv->view_type);
+                               break;
+               }
 }
 
 GDate
@@ -337,12 +405,14 @@ gtk_calex_map (GtkWidget *widget)
 
 static void
 gtk_calex_size_request (GtkWidget *widget,
-                      GtkRequisition *requisition)
+                        GtkRequisition *requisition)
 {
        GtkCalEx *gtkcalex;
        GtkBin *bin;
        GtkRequisition child_requisition;
 
+       guint border_width;
+
        g_return_if_fail (IS_GTK_CALEX (widget));
        g_return_if_fail (requisition != NULL);
 
@@ -359,13 +429,14 @@ gtk_calex_size_request (GtkWidget *widget,
                        requisition->height += child_requisition.height;
                }
 
-       requisition->width += GTK_CONTAINER (widget)->border_width * 2;
-       requisition->height += GTK_CONTAINER (widget)->border_width * 2;
+       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
+       requisition->width += border_width * 2;
+       requisition->height += border_width * 2;
 }
 
 static void
 gtk_calex_size_allocate (GtkWidget *widget,
-                       GtkAllocation *allocation)
+                         GtkAllocation *allocation)
 {
        GtkCalEx *gtkcalex;
        GtkCalExPrivate *priv;
@@ -373,6 +444,8 @@ gtk_calex_size_allocate (GtkWidget *widget,
        GtkAllocation relative_allocation;
        GtkAllocation child_allocation;
 
+       guint border_width;
+
        g_return_if_fail (IS_GTK_CALEX (widget));
        g_return_if_fail (allocation != NULL);
 
@@ -381,10 +454,11 @@ gtk_calex_size_allocate (GtkWidget *widget,
 
        priv = GTK_CALEX_GET_PRIVATE (gtkcalex);
 
-       widget->allocation = *allocation;
+       gtk_widget_set_allocation (widget, allocation);
 
-       relative_allocation.x = GTK_CONTAINER (widget)->border_width;
-       relative_allocation.y = GTK_CONTAINER (widget)->border_width;
+       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
+       relative_allocation.x = border_width;
+       relative_allocation.y = border_width;
        relative_allocation.width = MAX (1, (gint)widget->allocation.width - relative_allocation.x * 2);
        relative_allocation.height = MAX (1, (gint)widget->allocation.height - relative_allocation.y * 2);
 
@@ -399,41 +473,18 @@ gtk_calex_size_allocate (GtkWidget *widget,
 }
 
 static void
-gtk_calex_draw_day (GtkCalEx *gtk_calex)
+gtk_calex_clean_canvas (GtkCalEx *gtk_calex)
 {
-       guint hour;
+       guint children;
+       guint i;
 
        g_return_if_fail (IS_GTK_CALEX (gtk_calex));
 
        GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (gtk_calex);
 
-       for (hour = 0; hour < 24; hour++)
+       children = goo_canvas_item_get_n_children (priv->goo_canvas_root);
+       for (i = 0; i < children; i++)
                {
-                       goo_canvas_text_new (priv->goo_canvas_root, g_strdup_printf ("%d", hour),
-                                            0, hour * 40, 50,
-                                            GTK_ANCHOR_NORTH_WEST,
-                                            "font", "Sans 24",
-                                            NULL);
-                       goo_canvas_text_new (priv->goo_canvas_root, "00",
-                                            25, hour * 40, 25,
-                                            GTK_ANCHOR_NORTH_WEST,
-                                            "font", "Sans 12",
-                                            NULL);
-                       goo_canvas_polyline_new_line (priv->goo_canvas_root, 0, hour * 40, 50, hour * 40,
-                                            "stroke-color", "black",
-                                            NULL);
-                       goo_canvas_rect_new (priv->goo_canvas_root, 55, hour * 40, 400, 20,
-                                            "line-width", 1.0,
-                                            "stroke-color", "black",
-                                            "fill-color", "yellow",
-                                            NULL);
-                       goo_canvas_polyline_new_line (priv->goo_canvas_root, 25, hour * 40 + 20, 50, hour * 40 + 20,
-                                            "stroke-color", "black",
-                                            NULL);
-                       goo_canvas_rect_new (priv->goo_canvas_root, 55, hour * 40 + 20, 400, 20,
-                                            "line-width", 1.0,
-                                            "stroke-color", "black",
-                                            "fill-color", "yellow",
-                                            NULL);
+                       goo_canvas_item_remove (goo_canvas_item_get_child (priv->goo_canvas_root, i));
                }
 }
index b1fcb49ac495fd823a6a90369829d4242b4e5ace..5003996960b2bb071993321674e7b107e6a03a0a 100644 (file)
@@ -57,7 +57,7 @@ typedef enum {
        GTK_CALEX_VIEW_TYPE_WORK_WEEK,
        GTK_CALEX_VIEW_TYPE_WEEK,
        GTK_CALEX_VIEW_TYPE_MONTH,
-       GTK_CALEX_VIEW_LIST
+       GTK_CALEX_VIEW_TYPE_LIST
 } GtkCalExViewType;
 
 GtkCalExViewType gtk_calex_get_view_type (GtkCalEx *calex);
index 0bb6bd609ad966e457b42382dfcc4dcf6b110247..1b9b95660b77501059fb8989b0e6e00e46a36767 100644 (file)
@@ -20,8 +20,6 @@
        #include <config.h>
 #endif
 
-#include <goocanvas.h>
-
 #include "gtkcalexviewday.h"
 
 enum
@@ -41,13 +39,16 @@ static void gtk_calex_view_day_get_property (GObject *object,
                                     GValue *value,
                                     GParamSpec *pspec);
 
+static void gtk_calex_view_day_draw (GtkCalExViewDay *gtk_calex_view_day);
+
 
 #define GTK_CALEX_VIEW_DAY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_CALEX_VIEW_DAY, GtkCalExViewDayPrivate))
 
 typedef struct _GtkCalExViewDayPrivate GtkCalExViewDayPrivate;
 struct _GtkCalExViewDayPrivate
        {
-               gpointer foo;
+               GooCanvasItem *goo_canvas_root;
+               GDate *date;
        };
 
 G_DEFINE_TYPE (GtkCalExViewDay, gtk_calex_view_day, G_TYPE_OBJECT)
@@ -105,9 +106,56 @@ gtk_calex_view_day_get_property (GObject *object, guint property_id, GValue *val
  * Returns: the newly created #GtkCalExViewDay object.
  */
 GtkCalExViewDay
-*gtk_calex_view_day_new ()
+*gtk_calex_view_day_new (GooCanvasItem *root, GDate *date)
 {
        GtkCalExViewDay *gtk_calex_view_day = GTK_CALEX_VIEW_DAY (g_object_new (gtk_calex_view_day_get_type (), NULL));
 
+       GtkCalExViewDayPrivate *priv = GTK_CALEX_VIEW_DAY_GET_PRIVATE (gtk_calex_view_day);
+
+       priv->goo_canvas_root = root;
+       priv->date = date;
+
+       gtk_calex_view_day_draw (gtk_calex_view_day);
+
        return gtk_calex_view_day;
 }
+
+static void
+gtk_calex_view_day_draw (GtkCalExViewDay *gtk_calex_view_day)
+{
+       guint hour;
+
+       g_return_if_fail (IS_GTK_CALEX_VIEW_DAY (gtk_calex_view_day));
+
+       GtkCalExViewDayPrivate *priv = GTK_CALEX_VIEW_DAY_GET_PRIVATE (gtk_calex_view_day);
+
+       for (hour = 0; hour < 24; hour++)
+               {
+                       goo_canvas_text_new (priv->goo_canvas_root, g_strdup_printf ("%d", hour),
+                                            0, hour * 40, 50,
+                                            GTK_ANCHOR_NORTH_WEST,
+                                            "font", "Sans 24",
+                                            NULL);
+                       goo_canvas_text_new (priv->goo_canvas_root, "00",
+                                            25, hour * 40, 25,
+                                            GTK_ANCHOR_NORTH_WEST,
+                                            "font", "Sans 12",
+                                            NULL);
+                       goo_canvas_polyline_new_line (priv->goo_canvas_root, 0, hour * 40, 50, hour * 40,
+                                            "stroke-color", "black",
+                                            NULL);
+                       goo_canvas_rect_new (priv->goo_canvas_root, 55, hour * 40, 400, 20,
+                                            "line-width", 1.0,
+                                            "stroke-color", "black",
+                                            "fill-color", "yellow",
+                                            NULL);
+                       goo_canvas_polyline_new_line (priv->goo_canvas_root, 25, hour * 40 + 20, 50, hour * 40 + 20,
+                                            "stroke-color", "black",
+                                            NULL);
+                       goo_canvas_rect_new (priv->goo_canvas_root, 55, hour * 40 + 20, 400, 20,
+                                            "line-width", 1.0,
+                                            "stroke-color", "black",
+                                            "fill-color", "yellow",
+                                            NULL);
+               }
+}
index 2456924091032b32c5c0d8c83f19eaf6ed5d1a2a..579aeb3199eb20bbb4c99b6e1fe1e7a1fad8b687 100644 (file)
@@ -23,6 +23,8 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include <goocanvas.h>
+
 G_BEGIN_DECLS
 
 
@@ -50,7 +52,7 @@ struct _GtkCalExViewDayClass
 GType gtk_calex_view_day_get_type (void) G_GNUC_CONST;
 
 
-GtkCalExViewDay *gtk_calex_view_day_new (void);
+GtkCalExViewDay *gtk_calex_view_day_new (GooCanvasItem *root, GDate *date);
 
 
 G_END_DECLS
index 2c46ef739205f00c694fccd530b1961bb70feab6..8d7986b2ddb5dfe092cfd935c851bb6d92ce4afe 100644 (file)
 
 #include <libgtkcalendarex.h>
 
+GtkWidget *calex;
+
+G_MODULE_EXPORT void
+on_tbtn_view_type_day_toggled (GtkToggleToolButton *toggle_tool_button,
+                           gpointer user_data)
+{
+       gtk_calex_set_view_type (GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_DAY);
+}
+
+G_MODULE_EXPORT void
+on_tbtn_view_type_wweek_toggled (GtkToggleToolButton *toggle_tool_button,
+                           gpointer user_data)
+{
+       gtk_calex_set_view_type (GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_WORK_WEEK);
+}
+
+G_MODULE_EXPORT void
+on_tbtn_view_type_week_toggled (GtkToggleToolButton *toggle_tool_button,
+                           gpointer user_data)
+{
+       gtk_calex_set_view_type (GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_WEEK);
+}
+
+G_MODULE_EXPORT void
+on_tbtn_view_type_month_toggled (GtkToggleToolButton *toggle_tool_button,
+                           gpointer user_data)
+{
+       gtk_calex_set_view_type (GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_MONTH);
+}
+
+G_MODULE_EXPORT void
+on_tbtn_view_type_list_toggled (GtkToggleToolButton *toggle_tool_button,
+                           gpointer user_data)
+{
+       gtk_calex_set_view_type (GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_LIST);
+}
+
 int
 main (int argc, char **argv)
 {
        GtkBuilder *gtkbuilder;
        GError *error;
        GtkWidget *w;
-       GtkWidget *calex;
+       GtkWidget *vbox;
 
        gtk_init (&argc, &argv);
 
@@ -39,9 +76,10 @@ main (int argc, char **argv)
                }
 
        w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "window1"));
+       vbox = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "vbox1"));
 
        calex = gtk_calex_new ();
-       gtk_container_add (GTK_CONTAINER (w), calex);
+       gtk_box_pack_start (GTK_BOX (vbox), calex, TRUE, TRUE, 0);
 
        gtk_builder_connect_signals (gtkbuilder, NULL);
 
index bfa2d202f8fd8949aacb0f0d0a12809795a5f5f6..7fca5ff825eeb11d685d07e6909bc5c915e3c024 100644 (file)
@@ -9,7 +9,87 @@
     <property name="destroy_with_parent">True</property>
     <signal name="delete_event" handler="gtk_main_quit"/>
     <child>
-      <placeholder/>
+      <object class="GtkVBox" id="vbox1">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkToolbar" id="toolbar1">
+            <property name="visible">True</property>
+            <child>
+              <object class="GtkRadioToolButton" id="toolbutton1">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Day</property>
+                <property name="use_underline">True</property>
+                <signal name="toggled" handler="on_tbtn_view_type_day_toggled"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkRadioToolButton" id="toolbutton2">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Working week</property>
+                <property name="use_underline">True</property>
+                <property name="group">toolbutton1</property>
+                <signal name="toggled" handler="on_tbtn_view_type_wweek_toggled"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkRadioToolButton" id="toolbutton3">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Week</property>
+                <property name="use_underline">True</property>
+                <property name="group">toolbutton1</property>
+                <signal name="toggled" handler="on_tbtn_view_type_week_toggled"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkRadioToolButton" id="toolbutton4">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Month</property>
+                <property name="use_underline">True</property>
+                <property name="group">toolbutton1</property>
+                <signal name="toggled" handler="on_tbtn_view_type_month_toggled"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkRadioToolButton" id="toolbutton5">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">List</property>
+                <property name="use_underline">True</property>
+                <property name="active">True</property>
+                <property name="group">toolbutton1</property>
+                <signal name="toggled" handler="on_tbtn_view_type_list_toggled"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+      </object>
     </child>
   </object>
 </interface>