From: Pierre-Luc Beaudoin Date: Sat, 31 Jan 2009 11:56:48 +0000 (+0200) Subject: Fix bug 557532: champlain_view_embed_new contains initialization code X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f0dabd2a0e1a9764fee6d70a9e980d8a5741a51;p=libchamplain Fix bug 557532: champlain_view_embed_new contains initialization code It also makes it now possible to change the embedded ChamplainView at any moment. --- diff --git a/champlain-gtk/champlainviewembed.c b/champlain-gtk/champlainviewembed.c index 1d884bd..7d3bd1a 100644 --- a/champlain-gtk/champlainviewembed.c +++ b/champlain-gtk/champlainviewembed.c @@ -28,10 +28,15 @@ enum { /* normal signals */ - SIGNAL_TBD, LAST_SIGNAL }; +enum +{ + /* normal signals */ + PROP_0, + PROP_VIEW +}; //static guint champlain_view_embed_embed_signals[LAST_SIGNAL] = { 0, }; @@ -47,36 +52,67 @@ struct _ChamplainViewEmbedPrivate }; -static void champlain_view_embed_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); -static void champlain_view_embed_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static void champlain_view_embed_get_property(GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec); +static void champlain_view_embed_set_property(GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec); static void champlain_view_embed_finalize (GObject *object); -static void champlain_view_embed_class_init (ChamplainViewEmbedClass *champlainViewEmbedClass); +static void champlain_view_embed_class_init (ChamplainViewEmbedClass *klass); static void champlain_view_embed_init (ChamplainViewEmbed *view); -static void view_size_allocated_cb (GtkWidget *widget, GtkAllocation *allocation, ChamplainViewEmbed *view); +static void view_size_allocated_cb (GtkWidget *widget, + GtkAllocation *allocation, ChamplainViewEmbed *view); +static gboolean mouse_button_cb (GtkWidget *widget, GdkEventButton *event, + ChamplainViewEmbed *view); +static void view_size_allocated_cb (GtkWidget *widget, + GtkAllocation *allocation, ChamplainViewEmbed *view); G_DEFINE_TYPE (ChamplainViewEmbed, champlain_view_embed, GTK_TYPE_ALIGNMENT); static void -champlain_view_embed_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +champlain_view_embed_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) { - //ChamplainViewEmbed *view = CHAMPLAIN_VIEW_EMBED(object); - //ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); + ChamplainViewEmbed *embed = CHAMPLAIN_VIEW_EMBED(object); + ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (embed); switch(prop_id) { + case PROP_VIEW: + g_value_set_object (value, priv->view); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } } static void -champlain_view_embed_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +champlain_view_embed_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) { - //ChamplainViewEmbed *view = CHAMPLAIN_VIEW_EMBED(object); - //ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); + ChamplainViewEmbed *embed = CHAMPLAIN_VIEW_EMBED(object); + ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (embed); switch(prop_id) { + case PROP_VIEW: + { + ClutterActor *stage; + stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutter_embed)); + + if (priv->view != NULL) + { + g_object_unref (priv->view); + clutter_container_remove_actor (CLUTTER_CONTAINER (stage), CLUTTER_ACTOR (priv->view)); + } + + priv->view = g_value_dup_object (value); + clutter_container_add_actor (CLUTTER_CONTAINER (stage), CLUTTER_ACTOR (priv->view)); + break; + } default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } @@ -85,48 +121,93 @@ champlain_view_embed_set_property(GObject *object, guint prop_id, const GValue * static void champlain_view_embed_finalize (GObject *object) { - //ChamplainViewEmbed *view = CHAMPLAIN_VIEW_EMBED (object); - //ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); + ChamplainViewEmbed *embed = CHAMPLAIN_VIEW_EMBED (object); + ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (embed); + g_object_unref (priv->view); G_OBJECT_CLASS (champlain_view_embed_parent_class)->finalize (object); } static void -champlain_view_embed_class_init (ChamplainViewEmbedClass *champlainViewEmbedClass) +champlain_view_embed_class_init (ChamplainViewEmbedClass *klass) { - g_type_class_add_private (champlainViewEmbedClass, sizeof (ChamplainViewEmbedPrivate)); + g_type_class_add_private (klass, sizeof (ChamplainViewEmbedPrivate)); - GObjectClass *object_class = G_OBJECT_CLASS (champlainViewEmbedClass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->finalize = champlain_view_embed_finalize; object_class->get_property = champlain_view_embed_get_property; object_class->set_property = champlain_view_embed_set_property; + /** + * ChamplainViewEmbed:champlain-view: + * + * The #ChamplainView to embed in the Gtk+ widget. + * + * Since: 0.4 + */ + g_object_class_install_property (object_class, + PROP_VIEW, + g_param_spec_object ("champlain-view", + "Champlain view", + "The ChamplainView to embed into the Gtk+ widget", + CHAMPLAIN_TYPE_VIEW, + CHAMPLAIN_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void -champlain_view_embed_init (ChamplainViewEmbed *view) +champlain_view_embed_init (ChamplainViewEmbed *embed) { - ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); + ClutterColor stage_color = { 0x34, 0x39, 0x39, 0xff }; + ClutterActor *stage; + + ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (embed); priv->view = NULL; + priv->clutter_embed = gtk_clutter_embed_new (); + + g_signal_connect (priv->clutter_embed, + "size-allocate", + G_CALLBACK (view_size_allocated_cb), + embed); + g_signal_connect (priv->clutter_embed, + "button-press-event", + G_CALLBACK (mouse_button_cb), + embed); + g_signal_connect (priv->clutter_embed, + "button-release-event", + G_CALLBACK (mouse_button_cb), + embed); + // Setup cursors + priv->cursor_hand_open = gdk_cursor_new(GDK_HAND1); + priv->cursor_hand_closed = gdk_cursor_new(GDK_FLEUR); + + // Setup stage + stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutter_embed)); + clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); + + gtk_container_add (GTK_CONTAINER (embed), priv->clutter_embed); } static void -view_size_allocated_cb (GtkWidget *widget, GtkAllocation *allocation, ChamplainViewEmbed *view) +view_size_allocated_cb (GtkWidget *widget, + GtkAllocation *allocation, + ChamplainViewEmbed *view) { ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); champlain_view_set_size(priv->view, allocation->width, allocation->height); - + // Setup mouse cursor to a hand gdk_window_set_cursor( priv->clutter_embed->window, priv->cursor_hand_open); } -static gboolean -mouse_button_cb (GtkWidget *widget, GdkEventButton *event, ChamplainViewEmbed *view) +static gboolean +mouse_button_cb (GtkWidget *widget, + GdkEventButton *event, + ChamplainViewEmbed *view) { ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (view); - + if (event->type == GDK_BUTTON_PRESS) gdk_window_set_cursor( priv->clutter_embed->window, priv->cursor_hand_closed); else @@ -145,41 +226,9 @@ mouse_button_cb (GtkWidget *widget, GdkEventButton *event, ChamplainViewEmbed *v GtkWidget * champlain_view_embed_new (ChamplainView *view) { - g_return_val_if_fail(CLUTTER_IS_ACTOR(view), NULL); //FIXME should be CHAMPLAIN_IS_VIEW but doesn't compile - - ClutterColor stage_color = { 0x34, 0x39, 0x39, 0xff }; - ChamplainViewEmbed *embed; - ClutterActor *stage; - - embed = CHAMPLAIN_VIEW_EMBED (g_object_new (CHAMPLAIN_TYPE_VIEW_EMBED, NULL)); - ChamplainViewEmbedPrivate *priv = CHAMPLAIN_VIEW_EMBED_GET_PRIVATE (embed); - - priv->view = view; - priv->clutter_embed = gtk_clutter_embed_new (); - g_signal_connect (priv->clutter_embed, - "size-allocate", - G_CALLBACK (view_size_allocated_cb), - embed); - g_signal_connect (priv->clutter_embed, - "button-press-event", - G_CALLBACK (mouse_button_cb), - embed); - g_signal_connect (priv->clutter_embed, - "button-release-event", - G_CALLBACK (mouse_button_cb), - embed); - // Setup cursors - priv->cursor_hand_open = gdk_cursor_new(GDK_HAND1); - priv->cursor_hand_closed = gdk_cursor_new(GDK_FLEUR); - - // Setup stage - stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutter_embed)); - clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); - clutter_container_add_actor (CLUTTER_CONTAINER (stage), CLUTTER_ACTOR(view)); - - gtk_container_add (GTK_CONTAINER (embed), priv->clutter_embed); + g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), NULL); - return GTK_WIDGET (embed); + return g_object_new (CHAMPLAIN_TYPE_VIEW_EMBED, "champlain-view", view, NULL); } ChamplainView *