GTK+ Forums

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

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: capturing mouse on a cairo surface
PostPosted: Sat Jan 28, 2012 9:07 pm 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
Dear all,

i need a suggestion, since i am probably in the wrong way.
I am using a gtk drawingarea to draw several overlapped layers with trasparency, using cairo.
On them i then draw still some round buttons, still using cairo surfaces.
I will now need a way to to get the mouse click over a button.
I can do this simply detecting mouse click inside the rectangular shape (width,height) of the button image, but not with the real circular shape the button have.

I tried alsu using gtkfixed and real gtk buttons but seems i can't display them over the drawing area.

Any help is really appreciated,

thanks
regards
angelo


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Sun Jan 29, 2012 9:53 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
A circular button is arguably easier as you only need one comparison (though a little more arithmetic). lets say you have cursor->x and cursor->y for the mouse position and button->x, button->y and button->r for its centre and radius then the comparison is:
dx=(cursor->x)-(button->x); dy=(cursor->y)-(button->y);
dx*=dx; dy*=dy;
if (dx+dy<((button->r)*(button->r))) {code to execute on button click}


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Sun Jan 29, 2012 9:06 pm 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
hi Paul,

thanks,
unfortunately, i read these buttons image from a skin, they are now circular but they could have a different shape.


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Sun Jan 29, 2012 9:22 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
There will have to be some information in the skin concerning the shape of the button. Regular shapes such as ellipses, rectangles and rounded buttons can be represented parametrically and given an equation to figure out if the mouse is within the region.
Perhaps the button has a cairo_region_t associated with it in which case it can be done simply as cairo_region_contains_point.
Either way I'd have to know more about where you're sourcing your buttons from.


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Mon Jan 30, 2012 7:05 pm 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
Hi Paul,

thanks for the kind support.

Well, i read the buttons images from some .png files, transparency included, as they are part of a skin,
So i do:

Code:
cairo_surface_t * target =
         cairo_image_surface_create_from_png(image.c_str());


Then finally i draw all the buttons from inside the expose event.


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Tue Jan 31, 2012 5:18 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
ok this will go outside the realms of gtk programming and I don't have the background in programming with pngs etc. to get the technical detail out. What I'd go about doing is first determine if the cursor is in a box bounded by the png surface (which is an enclosing rectangle). If no then obviously not on the button; if yes then further testing is needed. this further test would be to index the alpha channel at the cursor position within the surface (= the cursor position - the position of the top-left corner of the image) -- I'll assume that you can define the shape by the alpha channel. How you go about getting the alpha value at such a position will depend on what language you're programming in (most high level languages'll have functions dedicated to do just this). Set a threshold value for the alpha; don't just query whether it's !=0 as sometimes things can go wrong. Something like >0.0625 would be better as less than this is not likely to be visible anyway.


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Tue Jan 31, 2012 2:21 pm 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
Hi Paul,

many thanks
your suggestion definitely works.
I actually arranged a cairo_regiont_t for every image, then, once the click is inside a region, i know what region is, so i check for transparency inside the ARGB32 array to see if i am in the real button.

Many thanks
regards
angelo


Top
 Profile  
 
 Post subject: Re: capturing mouse on a cairo surface
PostPosted: Tue Jan 31, 2012 9:06 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Excellent glad it works. No problem regards the help. Helps clarify and solidify my knowledge too.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 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