From 619472c30e299b34cd73cd6dfa1c3f4e90c01936 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 1 Nov 2010 10:06:12 +0100 Subject: [PATCH] Showed the widget in the test window. --- src/gtkcalex.c | 279 +++++++++++++++++++++++++++++++++++++--- src/gtkcalex.h | 6 +- src/gtkcalexviewday.c | 6 +- src/gtkcalexviewmonth.c | 6 +- src/gtkcalexviewweek.c | 6 +- tests/test.c | 4 + tests/test.ui | 2 + 7 files changed, 286 insertions(+), 23 deletions(-) diff --git a/src/gtkcalex.c b/src/gtkcalex.c index 5f9ec39..121993f 100644 --- a/src/gtkcalex.c +++ b/src/gtkcalex.c @@ -16,10 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "gtkcalex.h" +#ifdef HAVE_CONFIG_H + #include +#endif #include +#include "gtkcalex.h" + enum { PROP_0, @@ -30,6 +34,14 @@ enum static void gtk_calex_class_init (GtkCalExClass *klass); static void gtk_calex_init (GtkCalEx *gtk_calex); +static void gtk_calex_add (GtkContainer *container, + GtkWidget *widget); + +static void gtk_calex_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data); + static void gtk_calex_set_property (GObject *object, guint property_id, const GValue *value, @@ -39,34 +51,87 @@ static void gtk_calex_get_property (GObject *object, GValue *value, GParamSpec *pspec); +static void gtk_calex_realize (GtkWidget *widget); +static void gtk_calex_unrealize (GtkWidget *widget); +static void gtk_calex_map (GtkWidget *widget); +static void gtk_calex_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void gtk_calex_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); + #define GTK_CALEX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_CALEX, GtkCalExPrivate)) typedef struct _GtkCalExPrivate GtkCalExPrivate; struct _GtkCalExPrivate { + GdkWindow *event_window; + GtkWidget *scrolledw; GtkWidget *goo_canvas; GooCanvasItem *goo_canvas_root; + + GtkCalExViewType view_type; + GDate *date; }; -G_DEFINE_TYPE (GtkCalEx, gtk_calex, G_TYPE_OBJECT) +G_DEFINE_TYPE (GtkCalEx, gtk_calex, GTK_TYPE_BIN) static void gtk_calex_class_init (GtkCalExClass *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 (GtkCalExPrivate)); object_class->set_property = gtk_calex_set_property; object_class->get_property = gtk_calex_get_property; + + widget_class->realize = gtk_calex_realize; + widget_class->unrealize = gtk_calex_unrealize; + widget_class->map = gtk_calex_map; + widget_class->size_request = gtk_calex_size_request; + widget_class->size_allocate = gtk_calex_size_allocate; + + container_class->add = gtk_calex_add; + container_class->forall = gtk_calex_forall; } static void gtk_calex_init (GtkCalEx *gtk_calex) { + GTimeVal tval; + GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (gtk_calex); + + GTK_WIDGET_SET_FLAGS (gtk_calex, GTK_CAN_FOCUS); + GTK_WIDGET_UNSET_FLAGS (gtk_calex, GTK_NO_WINDOW); + + priv->scrolledw = gtk_scrolled_window_new (NULL, NULL); + gtk_container_add (GTK_CONTAINER (gtk_calex), priv->scrolledw); + + priv->goo_canvas = goo_canvas_new (); + gtk_widget_set_size_request (priv->goo_canvas, 600, 450); + 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)); + + goo_canvas_rect_new (priv->goo_canvas_root, 100, 100, 400, 400, + "line-width", 10.0, + "radius-x", 20.0, + "radius-y", 10.0, + "stroke-color", "yellow", + "fill-color", "red", + NULL); + + 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); } static void @@ -116,23 +181,10 @@ gtk_calex_get_property (GObject *object, guint property_id, GValue *value, GPara * * Returns: the newly created #GtkCalEx object. */ -GtkCalEx +GtkWidget *gtk_calex_new () { - GtkWidget *viewport; - - GtkCalEx *gtk_calex = GTK_CALEX (g_object_new (gtk_calex_get_type (), NULL)); - - GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (gtk_calex); - - priv->scrolledw = gtk_scrolled_window_new (NULL, NULL); - - viewport = gtk_viewport_new (NULL, NULL); - gtk_container_add (GTK_CONTAINER (priv->scrolledw), viewport); - - priv->goo_canvas = goo_canvas_new (); - - priv->goo_canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->goo_canvas)); + GtkWidget *gtk_calex = g_object_new (gtk_calex_get_type (), NULL); return gtk_calex; } @@ -140,19 +192,212 @@ GtkCalEx GtkCalExViewType gtk_calex_get_view_type (GtkCalEx *calex) { + g_return_val_if_fail (IS_GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_DAY); + + GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (calex); + + return priv->view_type; } void gtk_calex_set_view_type (GtkCalEx *calex, GtkCalExViewType type) { + g_return_if_fail (IS_GTK_CALEX (calex)); + + GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (calex); + + priv->view_type = type; } GDate *gtk_calex_get_date (GtkCalEx *calex) { + g_return_val_if_fail (IS_GTK_CALEX (calex), GTK_CALEX_VIEW_TYPE_DAY); + + GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (calex); + + return g_memdup (priv->date, sizeof (GDate)); } void gtk_calex_set_date (GtkCalEx *calex, GDate *date) { + g_return_if_fail (IS_GTK_CALEX (calex)); + + GtkCalExPrivate *priv = GTK_CALEX_GET_PRIVATE (calex); + + priv->date = g_memdup (date, sizeof (GDate)); +} + +static void +gtk_calex_add (GtkContainer *container, + GtkWidget *child) +{ + GtkCalEx *gtkcalex; + GtkBin *bin; + + bin = GTK_BIN (container); + g_return_if_fail (bin->child == NULL); + + gtkcalex = GTK_CALEX (container); + + bin->child = child; + gtk_widget_set_parent (child, GTK_WIDGET (bin)); +} + +static void +gtk_calex_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data) +{ + g_return_if_fail (IS_GTK_CALEX (container)); + g_return_if_fail (callback != NULL); + + GTK_CONTAINER_CLASS (gtk_calex_parent_class)->forall (container, + include_internals, + callback, + callback_data); +} + +static void +gtk_calex_realize (GtkWidget *widget) +{ + GdkWindowAttr attributes; + gint attributes_mask; + + GtkCalExPrivate *priv; + + GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); + + priv = GTK_CALEX_GET_PRIVATE (GTK_CALEX (widget)); + + attributes.x = widget->allocation.x; + attributes.y = widget->allocation.y; + attributes.width = widget->allocation.width; + attributes.height = widget->allocation.height; + attributes.window_type = GDK_WINDOW_CHILD; + attributes.wclass = GDK_INPUT_OUTPUT; + attributes.visual = gtk_widget_get_visual (widget); + attributes.colormap = gtk_widget_get_colormap (widget); + attributes.event_mask = gtk_widget_get_events (widget) + | GDK_BUTTON_MOTION_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_BUTTON_RELEASE_MASK + | GDK_POINTER_MOTION_MASK + | GDK_POINTER_MOTION_HINT_MASK; + + attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; + + widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), + &attributes, attributes_mask); + gdk_window_set_user_data (widget->window, widget); + + attributes.wclass = GDK_INPUT_ONLY; + attributes_mask = 0; + + priv->event_window = gdk_window_new (widget->window, + &attributes, attributes_mask); + gdk_window_set_user_data (priv->event_window, widget); + + widget->style = gtk_style_attach (widget->style, widget->window); + + gdk_window_set_back_pixmap (widget->window, NULL, FALSE); +} + +static void +gtk_calex_unrealize (GtkWidget *widget) +{ + GtkCalExPrivate *priv; + + priv = GTK_CALEX_GET_PRIVATE (widget); + + if (priv->event_window != NULL) + { + gdk_window_set_user_data (priv->event_window, NULL); + gdk_window_destroy (priv->event_window); + priv->event_window = NULL; + } + + GTK_WIDGET_CLASS (gtk_calex_parent_class)->unrealize (widget); +} + +static void +gtk_calex_map (GtkWidget *widget) +{ + GtkCalExPrivate *priv; + + g_return_if_fail (IS_GTK_CALEX (widget)); + + priv = GTK_CALEX_GET_PRIVATE (GTK_CALEX (widget)); + + GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED); + + GTK_WIDGET_CLASS (gtk_calex_parent_class)->map (widget); + + gdk_window_show (priv->event_window); + gdk_window_show (widget->window); +} + +static void +gtk_calex_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + GtkCalEx *gtkcalex; + GtkBin *bin; + GtkRequisition child_requisition; + + g_return_if_fail (IS_GTK_CALEX (widget)); + g_return_if_fail (requisition != NULL); + + gtkcalex = GTK_CALEX (widget); + bin = GTK_BIN (gtkcalex); + + 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; + } + + requisition->width += GTK_CONTAINER (widget)->border_width * 2; + requisition->height += GTK_CONTAINER (widget)->border_width * 2; +} + +static void +gtk_calex_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkCalEx *gtkcalex; + GtkCalExPrivate *priv; + GtkBin *bin; + GtkAllocation relative_allocation; + GtkAllocation child_allocation; + + g_return_if_fail (IS_GTK_CALEX (widget)); + g_return_if_fail (allocation != NULL); + + gtkcalex = GTK_CALEX (widget); + bin = GTK_BIN (gtkcalex); + + priv = GTK_CALEX_GET_PRIVATE (gtkcalex); + + widget->allocation = *allocation; + + relative_allocation.x = GTK_CONTAINER (widget)->border_width; + relative_allocation.y = GTK_CONTAINER (widget)->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); + } } diff --git a/src/gtkcalex.h b/src/gtkcalex.h index 498a1ac..b1fcb49 100644 --- a/src/gtkcalex.h +++ b/src/gtkcalex.h @@ -39,18 +39,18 @@ typedef struct _GtkCalExClass GtkCalExClass; struct _GtkCalEx { - GObject parent; + GtkBin parent; }; struct _GtkCalExClass { - GObjectClass parent_class; + GtkBinClass parent_class; }; GType gtk_calex_get_type (void) G_GNUC_CONST; -GtkCalEx *gtk_calex_new (void); +GtkWidget *gtk_calex_new (void); typedef enum { GTK_CALEX_VIEW_TYPE_DAY, diff --git a/src/gtkcalexviewday.c b/src/gtkcalexviewday.c index 6c7dee3..0bb6bd6 100644 --- a/src/gtkcalexviewday.c +++ b/src/gtkcalexviewday.c @@ -16,10 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "gtkcalexviewday.h" +#ifdef HAVE_CONFIG_H + #include +#endif #include +#include "gtkcalexviewday.h" + enum { PROP_0 diff --git a/src/gtkcalexviewmonth.c b/src/gtkcalexviewmonth.c index 7fe290b..35a2db9 100644 --- a/src/gtkcalexviewmonth.c +++ b/src/gtkcalexviewmonth.c @@ -16,10 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "gtkcalexviewmonth.h" +#ifdef HAVE_CONFIG_H + #include +#endif #include +#include "gtkcalexviewmonth.h" + enum { PROP_0 diff --git a/src/gtkcalexviewweek.c b/src/gtkcalexviewweek.c index 67a793a..bf3c4e6 100644 --- a/src/gtkcalexviewweek.c +++ b/src/gtkcalexviewweek.c @@ -16,10 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "gtkcalexviewweek.h" +#ifdef HAVE_CONFIG_H + #include +#endif #include +#include "gtkcalexviewweek.h" + enum { PROP_0 diff --git a/tests/test.c b/tests/test.c index 211f551..2c46ef7 100644 --- a/tests/test.c +++ b/tests/test.c @@ -26,6 +26,7 @@ main (int argc, char **argv) GtkBuilder *gtkbuilder; GError *error; GtkWidget *w; + GtkWidget *calex; gtk_init (&argc, &argv); @@ -39,6 +40,9 @@ main (int argc, char **argv) w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "window1")); + calex = gtk_calex_new (); + gtk_container_add (GTK_CONTAINER (w), calex); + gtk_builder_connect_signals (gtkbuilder, NULL); gtk_widget_show_all (w); diff --git a/tests/test.ui b/tests/test.ui index a6ba3ad..bfa2d20 100644 --- a/tests/test.ui +++ b/tests/test.ui @@ -3,8 +3,10 @@ + center 550 400 + True -- 2.49.0