[cairo] need help making background on custom cairo widget transparent

Michael L. Gualtieri mikeg at cs.pitt.edu
Sat Jul 15 09:15:04 PDT 2006


Thanks for your suggestions.

It looks like you may be right, and I am trying to do things that GTK
really doesn't support yet.

For the rgba channel I accidently reversed the meaning of 1.0 and 0.0, but
we were talking about the same thing.  I tried the snippit of code you put
below, however the 0.5 alpha comes up as a murky red (actually half red
and half black).  0.0 aplha comes up as all black, so I guess that the
alpha channel isn't being respected.

It looks like if I used the new X server and some of its fancy extensions
I could do this, however I have a need for this to be low in resources,
and I think that the new Xorg features may not work well on many video
cards.

I think I'll take a quick look at the GtkLabel class, as it paints on its
parent window instead of its own, however I think figuring out how that
works may take more time than I have.  Another option I have is to try and
handle all the GUI components in one cairo window...  this could get a
little messy, but as long as it looks good and runs well the end user is
none the wiser :-)

I'll report back if I figure anything out.


--
On Sat, 15 Jul 2006, Carl Worth wrote:

> On Fri, 14 Jul 2006 18:51:34 -0400 (EDT), "Michael L. Gualtieri" wrote:
> >
> > After many hours trying to get this working I broke down and decided to
> > join the mailing list here :-)
>
> Hi Michael,
>
> I hope you're having fun with cairo so far. And I'm glad you decided
> to come talk with us rather than just struggling alone.
>
> > My plans are to have one cairo widget be the background image of the
> > application (let's call it "background widget") and another widget that
> > rests on top of the "background widget" (let's call it "image
> > widget").
>
> That sounds like a fine plan, and something very desirable. However, I
> think GTK+ itself may need some code changes to support what you
> want. You see, GTK+ is involved with the creation, drawing and
> combination of widgets in ways other than the cairo-based drawing of
> the widgets that you are doing yourself.
>
> For example, GTK+ creates the cairo surface you are drawing to and
> will need to create one with alpha content in order for you to get the
> result you want. It likely also clears to an original background
> color, (which you could still change), but also probably copies things
> around, and may or may not do that in a way that respects the alpha
> channel you are carefully trying to put into your widgets.
>
> I'm not sure what the status of that might be in GTK+. Perhaps someone
> here on the cairo list knows. If not, then the right place to ask is
> probably the gtk-devel-list.
>
> > The problem is that the "image widget" appears in an ugly grey box.  I
> > would like the background of the image widget to show what is under it
> > (namely the "background widget" or whatever else I decide to put it on),
> > leaving only the image inside the "image widget" visible.  I have tried
> > countless ways to make this happen but have had no luck.
> >
> > I have been able to change the color of the widget, but when I use
> > the rgba channel, 100% alpha just shows up as a black background.  I have
> > also tried to set the GTK_NO_WINDOW flag for the "image widget", but this
> > makes it disappear completely.
>
> I'm not sure what you mean exactly by "use the rgba channel". I should
> point out that while we usually think of alpha as allowing
> translucence, the alpha value itself is a measure of opacity. So if
> you talk about 100% alpha (or an alpha value of 1.0) then you're
> talking about something entirely opaque. An alpha value of 0.0 would
> be entirely transparent.
>
> Also, the easiest ways of using alpha in cairo involve setting up a
> source that has a desired alpha value in it. For example, 50%
> translucent red can be achieved with:
>
> 	cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); /* 50% red */
>
> When you draw with this, (with the default OVER operator), the red
> source does blend nicely with the background. However, that background
> won't start acquiring any translucence if it started out as opaque
> already.
>
> So shoving a non-opaque alpha value into your destination does take a
> bit more work. One way is to use the SOURCE operator to directly copy
> all color and alpha channels from the source to the destination.
>
> So here's the test I would recommend you try. Do the following drawing
> operation (and no other drawing) in the widget you want to appear with
> translucence over another widget:
>
> 	cairo_save (cr);
> 	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
> 	cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); /* 50% red */
> 	cairo_paint (cr);
> 	cairo_restore (cr);
>
> That will set the entire widget to red with 50% alpha. If that doesn't
> blend at all then you've identified an enhancement that you can
> request in GTK+.
>
> If that does work, then you probably just need to start your drawing
> out by clearing to translucent with something like:
>
> 	cairo_save (cr);
> 	cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
> 	cairo_paint (cr);
> 	cairo_restore (cr);
>
> and then doing your drawing from there.
>
> Good luck, and have fun with cairo.
>
> -Carl
>



More information about the cairo mailing list