From: Andrea Zagli Date: Sat, 4 Dec 2010 11:14:59 +0000 (+0100) Subject: Implemeted drawing of GtkCalExViewWeek. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=cbea77bab48e4feb928f47736c957fdf875e705e;p=libgtkcalendarex Implemeted drawing of GtkCalExViewWeek. --- diff --git a/src/gtkcalex.c b/src/gtkcalex.c index 64d9822..07de2a4 100644 --- a/src/gtkcalex.c +++ b/src/gtkcalex.c @@ -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: diff --git a/src/gtkcalexview.c b/src/gtkcalexview.c index 48cd0fa..c922676 100644 --- a/src/gtkcalexview.c +++ b/src/gtkcalexview.c @@ -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 diff --git a/src/gtkcalexviewday.c b/src/gtkcalexviewday.c index 89e330f..ac473ae 100644 --- a/src/gtkcalexviewday.c +++ b/src/gtkcalexviewday.c @@ -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)); diff --git a/src/gtkcalexviewday.h b/src/gtkcalexviewday.h index 224a9ea..906bbe8 100644 --- a/src/gtkcalexviewday.h +++ b/src/gtkcalexviewday.h @@ -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); diff --git a/src/gtkcalexviewweek.c b/src/gtkcalexviewweek.c index 4c4616c..7c8791e 100644 --- a/src/gtkcalexviewweek.c +++ b/src/gtkcalexviewweek.c @@ -22,6 +22,7 @@ #include +#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)); + } } diff --git a/src/gtkcalexviewweek.h b/src/gtkcalexviewweek.h index 4dce464..b5b256a 100644 --- a/src/gtkcalexviewweek.h +++ b/src/gtkcalexviewweek.h @@ -20,8 +20,8 @@ #ifndef __GTK_CALEX_VIEW_WEEK_H__ #define __GTK_CALEX_VIEW_WEEK_H__ -#include -#include +#include + 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