]> saetta.ns0.it Git - libgtkcalendarex/commitdiff
Implemeted drawing of GtkCalExViewWeek.
authorAndrea Zagli <azagli@libero.it>
Sat, 4 Dec 2010 11:14:59 +0000 (12:14 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 4 Dec 2010 11:14:59 +0000 (12:14 +0100)
src/gtkcalex.c
src/gtkcalexview.c
src/gtkcalexviewday.c
src/gtkcalexviewday.h
src/gtkcalexviewweek.c
src/gtkcalexviewweek.h

index 64d9822f8c65ce749f22079c3c0d2520b139b506..07de2a4198c38cab8a80e910b0e63937f140a6af 100644 (file)
@@ -202,13 +202,12 @@ gtk_calex_set_view_type (GtkCalEx *calex, GtkCalExViewType type)
                                break;
 
                        case GTK_CALEX_VIEW_TYPE_WEEK:
-                               /*if (priv->view != NULL)
+                               if (priv->view != NULL)
                                        {
-                                               g_object_unref (priv->view);
+                                               gtk_widget_destroy (priv->view);
                                                priv->view = NULL;
                                        }
-                               GtkCalExViewWeek *vw = gtk_calex_view_week_new (priv->goo_canvas, priv->date);
-                               priv->view = vw;*/
+                               priv->view = gtk_calex_view_week_new (priv->date);
                                break;
 
                        case GTK_CALEX_VIEW_TYPE_MONTH:
index 48cd0fa6413254616718cfe46a290791452c92ee..c9226769eff8f28a9c4f667281dcb29b433f8255 100644 (file)
@@ -27,6 +27,14 @@ G_DEFINE_INTERFACE (GtkCalExView, gtk_calex_view, G_TYPE_OBJECT)
 static void
 gtk_calex_view_default_init (GtkCalExViewInterface *iface)
 {
+       static gboolean is_initialized = FALSE;
+
+       if (!is_initialized)
+               {
+                       /* add properties and signals to the interface here */
+
+                       is_initialized = TRUE;
+               }
 }
 
 void
index 89e330f96763773195ae095a5b108320dd699e5a..ac473ae97863752ad093bfbf7513f2a5f60eeee0 100644 (file)
@@ -49,6 +49,9 @@ static void gtk_calex_view_day_get_property (GObject *object,
                                     GValue *value,
                                     GParamSpec *pspec);
 
+static void gtk_calex_view_day_set_date (GtkCalExViewDay *view_day, GDate *date);
+static GDate *gtk_calex_view_day_get_date (GtkCalExViewDay *view_day);
+
 static void gtk_calex_view_day_clean_canvas (GtkCalExViewDay *view_day);
 
 static void gtk_calex_view_day_draw (GtkCalExViewDay *gtk_calex_view_day);
@@ -79,7 +82,6 @@ gtk_calex_view_day_class_init (GtkCalExViewDayClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
-       GtkContainerClass *container_class = (GtkContainerClass*) klass;
 
        g_type_class_add_private (object_class, sizeof (GtkCalExViewDayPrivate));
 
@@ -167,7 +169,7 @@ GtkWidget
        return GTK_WIDGET (gtk_calex_view_day);
 }
 
-GDate
+static GDate
 *gtk_calex_view_day_get_date (GtkCalExViewDay *view_day)
 {
        g_return_val_if_fail (IS_GTK_CALEX_VIEW_DAY (view_day), NULL);
@@ -177,7 +179,7 @@ GDate
        return g_memdup (priv->date, sizeof (GDate));
 }
 
-void
+static void
 gtk_calex_view_day_set_date (GtkCalExViewDay *view_day, GDate *date)
 {
        g_return_if_fail (IS_GTK_CALEX_VIEW_DAY (view_day));
index 224a9eaa1e0556a6b5ce3e8ae3df6b301afd84a8..906bbe89c4a43bb874aaadc72f19d71add6cc976 100644 (file)
@@ -52,9 +52,6 @@ GType gtk_calex_view_day_get_type (void) G_GNUC_CONST;
 
 GtkWidget *gtk_calex_view_day_new (GDate *date, gboolean working_week);
 
-GDate *gtk_calex_view_day_get_date (GtkCalExViewDay *view_day);
-void gtk_calex_view_day_set_date (GtkCalExViewDay *view_day, GDate *date);
-
 void gtk_calex_view_day_set_division (GtkCalExViewDay *view_day, guint division);
 guint gtk_calex_view_day_get_division (GtkCalExViewDay *view_day);
 
index 4c4616c5735ed8bd6e3c74e2a9e485e194913a51..7c8791ede7b92fd73b2e2d651ce1ef1ec1dc04ce 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <goocanvas.h>
 
+#include "gtkcalexview.h"
 #include "gtkcalexviewweek.h"
 
 enum
@@ -32,6 +33,13 @@ 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,
@@ -41,6 +49,10 @@ static void gtk_calex_view_week_get_property (GObject *object,
                                     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);
 
 
@@ -49,28 +61,54 @@ 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
@@ -105,20 +143,17 @@ gtk_calex_view_week_get_property (GObject *object, guint property_id, GValue *va
 
 /**
  * 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);
@@ -126,10 +161,212 @@ GtkCalExViewWeek
        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));
+               }
 }
index 4dce464e8d78427cb61a6d34c99324cd311b1725..b5b256ad47db905daccead14c50092f706822bdf 100644 (file)
@@ -20,8 +20,8 @@
 #ifndef __GTK_CALEX_VIEW_WEEK_H__
 #define __GTK_CALEX_VIEW_WEEK_H__
 
-#include <glib.h>
-#include <glib-object.h>
+#include <gtk/gtk.h>
+
 
 G_BEGIN_DECLS
 
@@ -39,18 +39,18 @@ typedef struct _GtkCalExViewWeekClass GtkCalExViewWeekClass;
 
 struct _GtkCalExViewWeek
        {
-               GObject parent;
+               GtkBin parent;
        };
 
 struct _GtkCalExViewWeekClass
        {
-               GObjectClass parent_class;
+               GtkBinClass parent_class;
        };
 
 GType gtk_calex_view_week_get_type (void) G_GNUC_CONST;
 
 
-GtkCalExViewWeek *gtk_calex_view_week_new (GtkWidget *canvas, GDate *date);
+GtkCalExViewWeek *gtk_calex_view_week_new (GDate *date);
 
 
 G_END_DECLS