GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 5:12 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Pass GdkRGBA out as boxed value
PostPosted: Sat Apr 14, 2012 6:38 pm 
Offline
Familiar Face

Joined: Thu Jan 12, 2012 7:44 pm
Posts: 7
Hello,

I use following code to pass a GdkRGBA out inside the *_get_property function:
Code:
case PROP_BG_COLOR:
   {
      GdkRGBA rgba;
      fp_gui_plotter_copy_rgba(&rgba, priv->bg_color);
      g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue); /* #1 */
      g_value_set_boxed(value, &rgba);
   }
   break;

Usage:
Code:
   GdkRGBA rgba2;
   g_object_get(gui_plotter, "bg-color", &rgba2, NULL);
   g_print("%lf-%lf-%lf\n", rgba2.red, rgba2.green, rgba2.blue); /* #2 */

#1 output: 0,500000-0,200000-0,100000
#2 output: 0,000000-0,000000-0,000000

Both outputs should be the same - what's my mistake in this code?
I think my problem is that rgba inside the _get_property function only exists in the function's stack and not in the heap.

In gtkcolorsel.c, a deprecated widget, following code is used:
Code:
case PROP_CURRENT_RGBA:
       {
        GdkRGBA rgba;

        gtk_color_selection_get_current_rgba (colorsel, &rgba);
        g_value_set_boxed (value, &rgba);
      }

In this code, rgba also only exists in the functions stack, doesn't it?
Why does this code work but mine not?
Have I got another mistake inside my code?

So long


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Sun Apr 15, 2012 5:21 am 
Offline
Familiar Face

Joined: Thu Dec 15, 2011 6:23 pm
Posts: 36
Location: Hrvatska
Quote:
I think my problem is that rgba inside the _get_property function only exists in the function's stack and not in the heap.

that is correct, it is a problem.
in another sample there is only part of code, so it is hard to tell what is going on.
btw. where did that fp_gui_plotter_copy_rgba() function came from ? is it your private function ?


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Sun Apr 15, 2012 6:39 pm 
Offline
Familiar Face

Joined: Thu Jan 12, 2012 7:44 pm
Posts: 7
Hello,

thanks for your reply.

Sorry, I forgot the fp_gui_plotter_copy_rgba() function. It just copies the red / green / blue / alpha property from the second GdkRGBA-struct to the first:
Code:
static void fp_gui_plotter_copy_rgba(GdkRGBA *dst, GdkRGBA *src) {
   dst->red = src->red;
   dst->green = src->green;
   dst->blue = src->blue;
   dst->alpha = src->alpha;
}


I tried this code:
Code:
   gui_plotter = g_object_new(FP_GUI_TYPE_PLOTTER, NULL);
   GdkRGBA rgba;
   rgba.red = 0.5;
   rgba.green = 0.2;
   rgba.blue = 0.1;
   rgba.alpha = 0.0;
   g_object_set(gui_plotter, "bg-color", &rgba, NULL);
   rgba.red = 1;
   rgba.green = 1;
   rgba.blue = 1;
   g_object_get(gui_plotter, "bg-color", &rgba, NULL);
   g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue);

First g_print gives this output: 0,500000-0,200000-0,100000, the second this: 1,000000-1,000000-1,000000.
It seems like g_object_get() doesn't even touch my pointer's memory.

I also tested this with the GtkColorSelection widget (the example of gtk I posted in my first post belongs to this widget). This is my test code:
Code:
   colsel = g_object_new(GTK_TYPE_COLOR_SELECTION, NULL);
   GdkRGBA rgba;
   rgba.red = 0.1;
   rgba.green = 0.2;
   rgba.blue = 0.3;
   rgba.alpha = 0.4;
   g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue); /* output: 0,100000-0,200000-0,300000 */
   
   g_object_set(colsel, "current-rgba", &rgba, NULL);
   
   rgba.red = 1;
   rgba.green = 1;
   rgba.blue = 1;
   
   g_object_get(colsel, "current-rgba", &rgba, NULL);
   g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue); /* output: 1,000000-1,000000-1,000000 - should be same the output as above*/

It seems like it's the same result, doesn't it? So maybe I'm just using it wrong?


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Sun Apr 15, 2012 10:43 pm 
Offline
Familiar Face

Joined: Thu Dec 15, 2011 6:23 pm
Posts: 36
Location: Hrvatska
I rewrite your sample in this way:
Code:
       GtkWidget *colsel = gtk_color_selection_new ();
       // colsel = g_object_new(GTK_TYPE_COLOR_SELECTION, NULL);
       GdkRGBA rgba;
       rgba.red = 0.1;
       rgba.green = 0.2;
       rgba.blue = 0.3;
       rgba.alpha = 0.4;

       g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue);

       gtk_color_selection_set_current_rgba (GTK_COLOR_SELECTION(colsel), &rgba);
       // g_object_set(colsel, "current-rgba", &rgba, NULL);

       rgba.red = 1;
       rgba.green = 1;
       rgba.blue = 1;

       // g_object_get(colsel, "current-rgba", &rgba, NULL);
       gtk_color_selection_get_current_rgba (GTK_COLOR_SELECTION(colsel), &rgba);
       g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue);

it works OK.
why g_object_set / g_object_get did not work ?
I don't know.
it is a strange indeed.
hope someone will help ...


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Tue Apr 17, 2012 5:39 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
No it won't be the same.
You are passing a pointer to g_object_set. The colour selection stores this pointer directly as its current colour (rather than setting up a duplicate pointer with the same contents). When you then modify rgba you are modifying the current colour. In the documentation you'll see "const GdkColor *color". This is what it means and you'll have similar issues with anything else that has the const.


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Wed Apr 18, 2012 2:25 pm 
Offline
Familiar Face

Joined: Thu Jan 12, 2012 7:44 pm
Posts: 7
Hello,

thanks for your reply.

Quote:
You are passing a pointer to g_object_set. The colour selection stores this pointer directly as its current colour (rather than setting up a duplicate pointer with the same contents).

Are you sure about this? In gtk+-3.4.0/gtk/deprecated/gtkcolorsel.c (just to make sure we're talking about the same widget), I found this code in gtk_color_selection_set_current_rgba:
Code:
  priv->color[COLORSEL_RED] = CLAMP (rgba->red, 0, 1);
  priv->color[COLORSEL_GREEN] = CLAMP (rgba->green, 0, 1);
  priv->color[COLORSEL_BLUE] = CLAMP (rgba->blue, 0, 1);
  priv->color[COLORSEL_OPACITY] = CLAMP (rgba->alpha, 0, 1);

It's the only time where the function uses rgba - it clearly doesn't save the pointer. Also, assuming that the function would save the pointer, this wouldn't work (because rgba only exists on the stack, so it will get lost if the function ends), would it?
Code:
GtkColorSelection some_function(void) {
  GdkRGBA rgba;
  /* fill rgba */
  return g_object_new(GTK_TYPE_COLOR_SELECTION, "current-rgba", &rgba, NULL);
}

At least, this example also doesn't work (it's the same as in my post above, just using another GdkRGBA, rgba2, for the get part - the second output is still 1,00000-1,000000-1,00000).
Code:
   colsel = g_object_new(GTK_TYPE_COLOR_SELECTION, NULL);
   GdkRGBA rgba;
   rgba.red = 0.1;
   rgba.green = 0.2;
   rgba.blue = 0.3;
   rgba.alpha = 0.4;
   g_print("%lf-%lf-%lf\n", rgba.red, rgba.green, rgba.blue);
   
   g_object_set(colsel, "current-rgba", &rgba, NULL);

   GdkRGBA rgba2;
   rgba2.red = 1;
   rgba2.green = 1;
   rgba2.blue = 1;
   
   g_object_get(colsel, "current-rgba", &rgba2, NULL);
   g_print("%lf-%lf-%lf\n", rgba2.red, rgba2.green, rgba2.blue);


Top
 Profile  
 
 Post subject: Re: Pass GdkRGBA out as boxed value
PostPosted: Thu Apr 19, 2012 11:24 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Sorry I only started reading from your second post when the missing details were added (why don't you use gdk_rgba_copy?). The examples gven there had the obvious mistake of overwriting the contents of the pointer.
I remember having a lot of trouble getting the hang of the colour selection when I started out. It was a lot worse in gtk2 as it took guint16 values which had to be converted to use in cairo and you also had to allocate colourmaps.
Maybe the issue is you're not transferring ownership and should use something like g_value_take_boxed() instead (http://blogs.gnome.org/danni/2011/11/16/mistakes-with-g_value_set_boxed/)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group