From: Andrea Zagli Date: Sun, 5 Dec 2010 10:56:22 +0000 (+0100) Subject: Implemented drawing of GtkCalExViewMonth. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=HEAD;p=libgtkcalendarex Implemented drawing of GtkCalExViewMonth. --- diff --git a/src/gtkcalex.c b/src/gtkcalex.c index 07de2a4..93ab212 100644 --- a/src/gtkcalex.c +++ b/src/gtkcalex.c @@ -216,7 +216,7 @@ gtk_calex_set_view_type (GtkCalEx *calex, GtkCalExViewType type) gtk_widget_destroy (priv->view); priv->view = NULL; } - priv->view = gtk_calex_view_day_new (priv->date, FALSE); + priv->view = gtk_calex_view_month_new (priv->date); break; case GTK_CALEX_VIEW_TYPE_LIST: diff --git a/src/gtkcalexviewmonth.c b/src/gtkcalexviewmonth.c index 35a2db9..5843fc8 100644 --- a/src/gtkcalexviewmonth.c +++ b/src/gtkcalexviewmonth.c @@ -22,6 +22,7 @@ #include +#include "gtkcalexview.h" #include "gtkcalexviewmonth.h" enum @@ -32,6 +33,13 @@ enum static void gtk_calex_view_month_class_init (GtkCalExViewMonthClass *klass); static void gtk_calex_view_month_init (GtkCalExViewMonth *gtk_calex_view_month); +static void gtk_calex_view_interface_init (GtkCalExViewInterface *iface); + +static void gtk_calex_view_month_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void gtk_calex_view_month_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); + static void gtk_calex_view_month_set_property (GObject *object, guint property_id, const GValue *value, @@ -41,32 +49,67 @@ static void gtk_calex_view_month_get_property (GObject *object, GValue *value, GParamSpec *pspec); +static void gtk_calex_view_month_set_date (GtkCalExViewMonth *view_month, GDate *date); +static GDate *gtk_calex_view_month_get_date (GtkCalExViewMonth *view_month); + +static void gtk_calex_view_month_clean_canvas (GtkCalExViewMonth *view_month); -#define GTK_CALEX_VIEW_MONTH_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_CALEX_VIEW_MONTH, GtkCalExViewMonthPrivate)) +static void gtk_calex_view_month_draw (GtkCalExViewMonth *gtk_calex_view_month); + + +#define GTK_CALEX_VIEW_MONTH_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_CALEX_TYPE_VIEW_MONTH, GtkCalExViewMonthPrivate)) typedef struct _GtkCalExViewMonthPrivate GtkCalExViewMonthPrivate; struct _GtkCalExViewMonthPrivate { - gpointer foo; + GdkWindow *event_window; + + GtkWidget *scrolledw; + GtkWidget *goo_canvas; + GooCanvasItem *goo_canvas_root; + + GDate *date; }; -G_DEFINE_TYPE (GtkCalExViewMonth, gtk_calex_view_month, G_TYPE_OBJECT) +G_DEFINE_TYPE_WITH_CODE (GtkCalExViewMonth, gtk_calex_view_month, GTK_TYPE_BIN, + G_IMPLEMENT_INTERFACE (GTK_CALEX_TYPE_VIEW, gtk_calex_view_interface_init)); static void gtk_calex_view_month_class_init (GtkCalExViewMonthClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = (GtkWidgetClass*) klass; g_type_class_add_private (object_class, sizeof (GtkCalExViewMonthPrivate)); object_class->set_property = gtk_calex_view_month_set_property; object_class->get_property = gtk_calex_view_month_get_property; + + widget_class->size_request = gtk_calex_view_month_size_request; + widget_class->size_allocate = gtk_calex_view_month_size_allocate; } static void gtk_calex_view_month_init (GtkCalExViewMonth *gtk_calex_view_month) { GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (gtk_calex_view_month); + + 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_month), 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_month_set_date; } static void @@ -101,13 +144,233 @@ gtk_calex_view_month_get_property (GObject *object, guint property_id, GValue *v /** * gtk_calex_view_month_new: + * @date: * * Returns: the newly created #GtkCalExViewMonth object. */ -GtkCalExViewMonth -*gtk_calex_view_month_new () +GtkWidget +*gtk_calex_view_month_new (GDate *date) { GtkCalExViewMonth *gtk_calex_view_month = GTK_CALEX_VIEW_MONTH (g_object_new (gtk_calex_view_month_get_type (), NULL)); - return gtk_calex_view_month; + GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (gtk_calex_view_month); + + priv->date = date; + + gtk_calex_view_month_draw (gtk_calex_view_month); + + return GTK_WIDGET (gtk_calex_view_month); +} + +static GDate +*gtk_calex_view_month_get_date (GtkCalExViewMonth *view_month) +{ + g_return_val_if_fail (GTK_CALEX_IS_VIEW_MONTH (view_month), NULL); + + GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (view_month); + + return g_memdup (priv->date, sizeof (GDate)); +} + +static void +gtk_calex_view_month_set_date (GtkCalExViewMonth *view_month, GDate *date) +{ + g_return_if_fail (GTK_CALEX_IS_VIEW_MONTH (view_month)); + + GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (view_month); + + priv->date = g_memdup (date, sizeof (GDate)); + + gtk_calex_view_month_draw (view_month); +} + +static void +gtk_calex_view_month_draw (GtkCalExViewMonth *gtk_calex_view_month) +{ + GtkAllocation allocation; + + GDateWeekday wday; + GDate *day; + GDate *ending_day; + + guint i; + guint days; + guint8 days_month; + + gchar *str_day; + + gdouble width; + gdouble height; + gdouble x; + gdouble y; + + g_return_if_fail (GTK_CALEX_IS_VIEW_MONTH (gtk_calex_view_month)); + + GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (gtk_calex_view_month); + + gtk_calex_view_month_clean_canvas (gtk_calex_view_month); + + gtk_widget_get_allocation (priv->goo_canvas, &allocation); + + width = allocation.width / 6; + + /* starting day */ + day = g_date_new_dmy (1, + (GDateMonth)priv->date->month, + (GDateYear)priv->date->year); + wday = g_date_get_weekday (day); + g_date_subtract_days (day, wday); + + /* ending day */ + days_month = g_date_get_days_in_month ((GDateMonth)priv->date->month, + (GDateYear)priv->date->year); + ending_day = g_date_new_dmy ((GDateDay)days_month, + (GDateMonth)priv->date->month, + (GDateYear)priv->date->year); + wday = g_date_get_weekday (ending_day); + g_date_add_days (ending_day, G_DATE_SUNDAY - wday); + + days = g_date_days_between (day, ending_day); + + height = allocation.height / (days / 7); + + y = -height; + for (i = 0; i < days; i++) + { + g_date_add_days (day, 1); + wday = g_date_get_weekday (day); + + if (wday == G_DATE_MONDAY) + { + x = 0; + if (i == 0) + { + y += height; + } + else + { + y += (height / 2); + } + } + + if (wday == G_DATE_SATURDAY) + { + height /= 2; + } + + goo_canvas_rect_new (priv->goo_canvas_root, + x, y, width, height, + "line-width", 1.0, + "stroke-color", "black", + "fill-color", "yellow", + NULL); + goo_canvas_text_new (priv->goo_canvas_root, g_strdup_printf ("%02d", g_date_get_day (day)), + x, y, width, + GTK_ANCHOR_NORTH_WEST, + "font", "Sans 12", + NULL); + + if (wday != G_DATE_SATURDAY) + { + x += width; + } + else + { + y += height; + } + if (wday == G_DATE_SUNDAY) + { + height *= 2; + } + } + + goo_canvas_set_bounds (GOO_CANVAS (priv->goo_canvas), 0, 0, allocation.width, allocation.height); +} + +static void +gtk_calex_view_month_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + GtkCalExViewMonth *gtkcalexvd; + GtkBin *bin; + GtkRequisition child_requisition; + + guint border_width; + + g_return_if_fail (GTK_CALEX_IS_VIEW_MONTH (widget)); + g_return_if_fail (requisition != NULL); + + gtkcalexvd = GTK_CALEX_VIEW_MONTH (widget); + bin = GTK_BIN (gtkcalexvd); + + 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_month_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkCalExViewMonth *gtkcalexvd; + GtkCalExViewMonthPrivate *priv; + GtkBin *bin; + GtkAllocation relative_allocation; + GtkAllocation child_allocation; + + guint border_width; + + g_return_if_fail (GTK_CALEX_IS_VIEW_MONTH (widget)); + g_return_if_fail (allocation != NULL); + + gtkcalexvd = GTK_CALEX_VIEW_MONTH (widget); + bin = GTK_BIN (gtkcalexvd); + + priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (gtkcalexvd); + + 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_month_draw (gtkcalexvd); +} + +static void +gtk_calex_view_month_clean_canvas (GtkCalExViewMonth *view_month) +{ + gint children; + gint i; + + g_return_if_fail (GTK_CALEX_IS_VIEW_MONTH (view_month)); + + GtkCalExViewMonthPrivate *priv = GTK_CALEX_VIEW_MONTH_GET_PRIVATE (view_month); + + 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/gtkcalexviewmonth.h b/src/gtkcalexviewmonth.h index 999ee04..677f51f 100644 --- a/src/gtkcalexviewmonth.h +++ b/src/gtkcalexviewmonth.h @@ -20,18 +20,18 @@ #ifndef __GTK_CALEX_VIEW_MONTH_H__ #define __GTK_CALEX_VIEW_MONTH_H__ -#include -#include +#include + G_BEGIN_DECLS -#define TYPE_GTK_CALEX_VIEW_MONTH (gtk_calex_view_month_get_type ()) -#define GTK_CALEX_VIEW_MONTH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GTK_CALEX_VIEW_MONTH, GtkCalExViewMonth)) -#define GTK_CALEX_VIEW_MONTH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GTK_CALEX_VIEW_MONTH, GtkCalExViewMonthClass)) -#define IS_GTK_CALEX_VIEW_MONTH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GTK_CALEX_VIEW_MONTH)) -#define IS_GTK_CALEX_VIEW_MONTH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GTK_CALEX_VIEW_MONTH)) -#define GTK_CALEX_VIEW_MONTH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_GTK_CALEX_VIEW_MONTH, GtkCalExViewMonthClass)) +#define GTK_CALEX_TYPE_VIEW_MONTH (gtk_calex_view_month_get_type ()) +#define GTK_CALEX_VIEW_MONTH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_CALEX_TYPE_VIEW_MONTH, GtkCalExViewMonth)) +#define GTK_CALEX_VIEW_MONTH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_CALEX_TYPE_VIEW_MONTH, GtkCalExViewMonthClass)) +#define GTK_CALEX_IS_VIEW_MONTH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_CALEX_TYPE_VIEW_MONTH)) +#define GTK_CALEX_IS_VIEW_MONTH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_CALEX_TYPE_VIEW_MONTH)) +#define GTK_CALEX_VIEW_MONTH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_CALEX_TYPE_VIEW_MONTH, GtkCalExViewMonthClass)) typedef struct _GtkCalExViewMonth GtkCalExViewMonth; @@ -39,18 +39,18 @@ typedef struct _GtkCalExViewMonthClass GtkCalExViewMonthClass; struct _GtkCalExViewMonth { - GObject parent; + GtkBin parent; }; struct _GtkCalExViewMonthClass { - GObjectClass parent_class; + GtkBinClass parent_class; }; GType gtk_calex_view_month_get_type (void) G_GNUC_CONST; -GtkCalExViewMonth *gtk_calex_view_month_new (void); +GtkWidget *gtk_calex_view_month_new (GDate *date); G_END_DECLS