GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 23, 2014 6:22 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Transparent image?
PostPosted: Sat Jun 14, 2008 10:42 am 
Offline
Familiar Face

Joined: Sun Mar 09, 2008 2:16 pm
Posts: 14
Hello everybody!

I'd like to create a button consisting of an image and it should become transparent on mouse-over.

Right now I use the following code to load the image:

Code:
image_buf = gdk_pixbuf_new_from_file_at_size ("./images/darl.svg", 40, 40, NULL);
image = gtk_image_new_from_pixbuf (image_buf);

Unfortunately I have no idea how to create a transparent image!

Anyone can help me? Or do you have another approach for my problem?

Thank you for helping me!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 12:31 pm 
Offline
Familiar Face

Joined: Wed Jun 04, 2008 10:37 am
Posts: 16
Hi,

Assuming that you are drawing the the image to a drawable widget within a container button you could use something similar to the following code:

Code:
bool drawPng(GtkWidget *pWidget, const std::string file)
{
    cairo_t       *pContext = NULL;
    int           width, height, xOff, yOff;
    GdkDrawable   *pDrawable = pWidget->window;

    gdk_drawable_get_size (pDrawable, &width, &height);
    gdk_window_get_internal_paint_info(pDrawable, NULL, &xOff, &yOff);
    pContext = gdk_cairo_create(pDrawable);

    if (pContext)
    {
        cairo_surface_t *pImage = cairo_image_surface_create_from_png(file.c_str());

        if (pImage)
        {
            double imgWidth  = cairo_image_surface_get_width(pImage);
            double imgHeight = cairo_image_surface_get_height(pImage);
            double xScale = width / imgWidth;
            double yScale = height / imgHeight;
            double xOffset = (width  - (imgWidth  * xScale)) / 2.0;
            double yOffset = (height - (imgHeight * yScale)) / 2.0;

            cairo_translate(pContext, xOffset, yOffset);
            cairo_scale(pContext, xScale, yScale);
            cairo_set_source_surface(pContext, pImage, 0, 0);
            cairo_paint(pContext);
            cairo_surface_destroy(pImage);
        }

        cairo_destroy(pContext);
        return (pImage != NULL);
    }

    return false;
}


In my code:
This function is called from the expose-event signal emitted by the drawable widget. So you'll have to setup this signal.
Once the signal is emitted you can use the widget parameter to pass to pngDraw(). You would alter this for the mouse movement on the same widget as the expose...

You will have to change the png load sequence to the svg load sequence. Also once you can draw the image you could then add your functionality to manipulate the alpha of the image via the cairo API. But that I'll leave you to tinker with ;)

Hope this points you in the right direction
Cheers,

martinpjvr


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 10:44 am 
Offline
Familiar Face

Joined: Sun Mar 09, 2008 2:16 pm
Posts: 14
Hello and thank you for this answer!

I have already found something similar using Cairo and PNG ... unfortunately I believe that Cairo does not support SVG (or at least I couldn't find a way to load an SVG image).

If anyone has another idea or knows how to load SVG images in Cairo, please let me know ... thanks!

Cheers
Zarg


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

All times are UTC


Who is online

Users browsing this forum: Google Adsense [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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group