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 Nov 29, 2014 7:13 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: little cairo help
PostPosted: Mon Feb 06, 2012 8:45 am 
Offline
Familiar Face

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

sorry if the post is not properly pure gtk, but i found help here often on this,

I need to draw an horizontal rectangular bar inside a same size rectangular region, but the desired effect is that, increasing the X coordinate (X position) of the bar, the exceeding part must never be drawn outside of the predefined rectangle region limits.

What should be the easier way to go for this with cairo ?

Many thanks,
angleo


Top
 Profile  
 
 Post subject: Re: little cairo help
PostPosted: Mon Feb 06, 2012 10:00 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Use cairo_clip() function to restrict drawing to your "master" rectangle. Something like this should work:

Code:
cairo_save (cr);
cairo_rectangle (cr, x_master, y_master, w_master, h_master);
cairo_clip (cr);
/* Draw your bar here */
cairo_restore (cr);


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: little cairo help
PostPosted: Mon Feb 06, 2012 8:43 pm 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
works perfect, great thanks Tadej

and great forum !


Top
 Profile  
 
 Post subject: Re: little cairo help
PostPosted: Mon Feb 06, 2012 9:10 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
By the way you can never have a rectangle completely inside a same size rectangular region. The reason for this is the finite linewidth will mean your rectangle has an outer size and an inner size. If your rectangular region is from say 0,0 to 10,10 with a line width of 1, and draw a rectangle with the same coordinates it will have an outer border of -1,-1 to 11,11 and an inner border of 1,1 to 9,9 (yes the line width is actually 2 unless you set integer +1/2 coordinates due to rounding). If you clip it, it of course will limit the outer border to just 0,0 to 10,10; however, the linewidth will look different than a rectangle that isn't clipped.
Clipping'll do the job, but if you want to ensure you don't get any variant behaviour try the following:
If you want to limit the region to (a,b) to (c,d) with an actual linewidth l (a-d,l all integers and a<c, b<d) then draw your rectangle as:
(a+(l/2),b+(l/2)) to (c-(l/2),d-(l/2))
Nonetheless clipping is still very useful just to make sure nothing unforeseen goes wrong


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

All times are UTC


Who is online

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