#include <goocanvas.h>
 
+#include "gtkcalexview.h"
 #include "gtkcalexviewweek.h"
 
 enum
 static void gtk_calex_view_week_class_init (GtkCalExViewWeekClass *klass);
 static void gtk_calex_view_week_init (GtkCalExViewWeek *gtk_calex_view_week);
 
+static void gtk_calex_view_interface_init (GtkCalExViewInterface *iface);
+
+static void gtk_calex_view_week_size_request (GtkWidget *widget,
+                                    GtkRequisition *requisition);
+static void gtk_calex_view_week_size_allocate (GtkWidget *widget,
+                                     GtkAllocation *allocation);
+
 static void gtk_calex_view_week_set_property (GObject *object,
                                     guint property_id,
                                     const GValue *value,
                                     GValue *value,
                                     GParamSpec *pspec);
 
+static void gtk_calex_view_week_set_date (GtkCalExViewWeek *view_week, GDate *date);
+
+static void gtk_calex_view_week_clean_canvas (GtkCalExViewWeek *view_week);
+
 static void gtk_calex_view_week_draw (GtkCalExViewWeek *gtk_calex_view_week);
 
 
 typedef struct _GtkCalExViewWeekPrivate GtkCalExViewWeekPrivate;
 struct _GtkCalExViewWeekPrivate
        {
-               GtkWidget *canvas;
-               GooCanvasItem *canvas_root;
+               GdkWindow *event_window;
+
+               GtkWidget *scrolledw;
+               GtkWidget *goo_canvas;
+               GooCanvasItem *goo_canvas_root;
+
                GDate *date;
        };
 
-G_DEFINE_TYPE (GtkCalExViewWeek, gtk_calex_view_week, G_TYPE_OBJECT)
+G_DEFINE_TYPE_WITH_CODE (GtkCalExViewWeek, gtk_calex_view_week, GTK_TYPE_BIN,
+                         G_IMPLEMENT_INTERFACE (GTK_CALEX_TYPE_VIEW, gtk_calex_view_interface_init));
 
 static void
 gtk_calex_view_week_class_init (GtkCalExViewWeekClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
 
        g_type_class_add_private (object_class, sizeof (GtkCalExViewWeekPrivate));
 
        object_class->set_property = gtk_calex_view_week_set_property;
        object_class->get_property = gtk_calex_view_week_get_property;
+
+       widget_class->size_request = gtk_calex_view_week_size_request;
+       widget_class->size_allocate = gtk_calex_view_week_size_allocate;
 }
 
 static void
 gtk_calex_view_week_init (GtkCalExViewWeek *gtk_calex_view_week)
 {
        GtkCalExViewWeekPrivate *priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (gtk_calex_view_week);
+
+       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_view_week), priv->scrolledw);
+
+       priv->goo_canvas = goo_canvas_new ();
+       goo_canvas_set_bounds (GOO_CANVAS (priv->goo_canvas), 0, 0, 1000, 1000);
+       gtk_container_add (GTK_CONTAINER (priv->scrolledw), priv->goo_canvas);
+
+       priv->goo_canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->goo_canvas));
+}
+
+static void
+gtk_calex_view_interface_init (GtkCalExViewInterface *iface)
+{
+       iface->set_date = gtk_calex_view_week_set_date;
 }
 
 static void
 
 /**
  * gtk_calex_view_week_new:
- * @canvas:
  * @date:
  *
  * Returns: the newly created #GtkCalExViewWeek object.
  */
 GtkCalExViewWeek
-*gtk_calex_view_week_new (GtkWidget *canvas, GDate *date)
+*gtk_calex_view_week_new (GDate *date)
 {
        GtkCalExViewWeek *gtk_calex_view_week = GTK_CALEX_VIEW_WEEK (g_object_new (gtk_calex_view_week_get_type (), NULL));
 
        GtkCalExViewWeekPrivate *priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (gtk_calex_view_week);
 
-       priv->canvas = canvas;
-       priv->canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->canvas));
        priv->date = date;
 
        gtk_calex_view_week_draw (gtk_calex_view_week);
        return gtk_calex_view_week;
 }
 
+static void
+gtk_calex_view_week_set_date (GtkCalExViewWeek *view_week, GDate *date)
+{
+       g_return_if_fail (IS_GTK_CALEX_VIEW_WEEK (view_week));
+
+       GtkCalExViewWeekPrivate *priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (view_week);
+
+       priv->date = g_memdup (date, sizeof (GDate));
+
+       gtk_calex_view_week_draw (view_week);
+}
+
 static void
 gtk_calex_view_week_draw (GtkCalExViewWeek *gtk_calex_view_week)
 {
+       GtkCalExViewWeekPrivate *priv;
+
+       GtkAllocation allocation;
+
+       gdouble width;
+       gdouble height;
+
+       gchar *str_day;
+
+       GDateWeekday wday;
+       GDate *day;
+
+       guint i;
+       gdouble y;
+
        g_return_if_fail (IS_GTK_CALEX_VIEW_WEEK (gtk_calex_view_week));
 
-       GtkCalExViewWeekPrivate *priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (gtk_calex_view_week);
+       priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (gtk_calex_view_week);
+
+       gtk_calex_view_week_clean_canvas (gtk_calex_view_week);
+
+       gtk_widget_get_allocation (priv->goo_canvas, &allocation);
+
+       width = allocation.width / 2;
+       height = allocation.height / 3;
+
+       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                            width, 0, width, allocation.height,
+                            "stroke-color", "black",
+                            NULL);
+
+       str_day = g_malloc0 (100);
+
+       day = g_date_new_dmy ((GDateDay)priv->date->day,
+                             (GDateMonth)priv->date->month,
+                             (GDateYear)priv->date->year);
+       wday = g_date_get_weekday (day);
+       g_date_subtract_days (day, wday);
+
+       y = 0;
+       for (i = 0; i < 5; i++)
+               {
+                       if (i % 2 == 0 && i > 0)
+                               {
+                                       y += height;
+
+                                       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                                                            0, y, allocation.width, y,
+                                                            "stroke-color", "black",
+                                                            NULL);
+                               }
+
+                       g_date_add_days (day, 1);
+                       g_date_strftime (str_day, 100, "%A %d %B", day);
+
+                       goo_canvas_text_new (priv->goo_canvas_root, str_day,
+                                            (i % 2 == 0 ? 0 : width), y, width,
+                                            GTK_ANCHOR_NORTH_WEST,
+                                            "font", "Sans 10",
+                                            NULL);
+                       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                                            (i % 2 == 0 ? 0 : width), y + 20, (i % 2 == 0 ? width : allocation.width), y + 20,
+                                            "stroke-color", "black",
+                                            NULL);
+               }
+
+       /* saturday */
+       g_date_add_days (day, 1);
+       g_date_strftime (str_day, 100, "%A %d %B", day);
+
+       goo_canvas_text_new (priv->goo_canvas_root, str_day,
+                            width, y, width,
+                            GTK_ANCHOR_NORTH_WEST,
+                            "font", "Sans 10",
+                            NULL);
+       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                            width, y + 20, allocation.width, y + 20,
+                            "stroke-color", "black",
+                            NULL);
+
+       /* sunday */
+       y += (height / 2);
+
+       g_date_add_days (day, 1);
+       g_date_strftime (str_day, 100, "%A %d %B", day);
+
+       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                            width, y, allocation.width, y,
+                            "stroke-color", "black",
+                            NULL);
+       goo_canvas_text_new (priv->goo_canvas_root, str_day,
+                            width, y, width,
+                            GTK_ANCHOR_NORTH_WEST,
+                            "font", "Sans 10",
+                            NULL);
+       goo_canvas_polyline_new_line (priv->goo_canvas_root,
+                            width, y + 20, allocation.width, y + 20,
+                            "stroke-color", "black",
+                            NULL);
+
+       goo_canvas_set_bounds (GOO_CANVAS (priv->goo_canvas), 0, 0, allocation.width, allocation.height);
+
+       g_free (str_day);
+}
+
+static void
+gtk_calex_view_week_size_request (GtkWidget *widget,
+                        GtkRequisition *requisition)
+{
+       GtkCalExViewWeek *gtkcalexvw;
+       GtkBin *bin;
+       GtkRequisition child_requisition;
+
+       guint border_width;
+
+       g_return_if_fail (IS_GTK_CALEX_VIEW_WEEK (widget));
+       g_return_if_fail (requisition != NULL);
+
+       gtkcalexvw = GTK_CALEX_VIEW_WEEK (widget);
+       bin = GTK_BIN (gtkcalexvw);
+
+       requisition->width = 0;
+       requisition->height = 0;
+
+       if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
+               {
+                       gtk_widget_size_request (bin->child, &child_requisition);
+                       requisition->width += child_requisition.width;
+                       requisition->height += child_requisition.height;
+               }
+
+       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
+       requisition->width += border_width * 2;
+       requisition->height += border_width * 2;
+}
+
+static void
+gtk_calex_view_week_size_allocate (GtkWidget *widget,
+                         GtkAllocation *allocation)
+{
+       GtkCalExViewWeek *gtkcalexvw;
+       GtkCalExViewWeekPrivate *priv;
+       GtkBin *bin;
+       GtkAllocation relative_allocation;
+       GtkAllocation child_allocation;
+
+       guint border_width;
+
+       g_return_if_fail (IS_GTK_CALEX_VIEW_WEEK (widget));
+       g_return_if_fail (allocation != NULL);
+
+       gtkcalexvw = GTK_CALEX_VIEW_WEEK (widget);
+       bin = GTK_BIN (gtkcalexvw);
+
+       priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (gtkcalexvw);
+
+/*g_message("gtkcalexviewday allocation %d %d %d %d",allocation->x,allocation->y,allocation->width,allocation->height);*/
+
+       gtk_widget_set_allocation (widget, allocation);
+
+       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);
+
+       if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
+               {
+                       child_allocation.x = relative_allocation.x + allocation->x;
+                       child_allocation.y = relative_allocation.y + allocation->y;
+                       child_allocation.width = relative_allocation.width;
+                       child_allocation.height = relative_allocation.height;
+                       gtk_widget_size_allocate (bin->child, &child_allocation);
+               }
+
+       gtk_calex_view_week_draw (gtkcalexvw);
+}
+
+static void
+gtk_calex_view_week_clean_canvas (GtkCalExViewWeek *view_week)
+{
+       gint children;
+       gint i;
+
+       g_return_if_fail (IS_GTK_CALEX_VIEW_WEEK (view_week));
+
+       GtkCalExViewWeekPrivate *priv = GTK_CALEX_VIEW_WEEK_GET_PRIVATE (view_week);
+
+       children = goo_canvas_item_get_n_children (priv->goo_canvas_root);
+       for (i = children - 1; i >= 0; i--)
+               {
+                       goo_canvas_item_remove (goo_canvas_item_get_child (priv->goo_canvas_root, i));
+               }
 }