[cairo] [cairo osx] OS X example
Hugo Vincent
hugo.vincent at gmail.com
Wed Jun 20 15:41:14 PDT 2007
Here is a simple example I put together when I went through this
exact painful process a couple of months back. I haven't tested it
for a while, but it seems to still run and might be of use still.
Also, this uses Cairomm, but you should be able to figure it out the
minor changes in API to change it from Cairomm to plain Cairo.
Cheers,
Hugo Vincent.
-----------------------------------------------------------
testview.h:
-----------------------------------------------------------
#import <Cocoa/Cocoa.h>
// Put one of these views into your layout in Interface Builder.
@interface TestView : NSView
{
// nothing needed here
}
@end
-----------------------------------------------------------
testview.m
-----------------------------------------------------------
#include <cairomm/cairomm.h>
#include <cairomm/quartz_surface.h>
#ifndef CAIRO_HAS_QUARTZ_SURFACE
#error Need to build Cairo with Quartz support (version 1.4.0 or higher)
#endif
@implementation TestView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil)
{
// nothing needed here
}
return self;
}
- (BOOL)isOpaque
{
return NO;
}
- (void)drawRect:(NSRect)rect
{
// Get the size of this NSView
NSRect bounds = [self bounds];
int width = bounds.size.width;
int height = bounds.size.height;
// Get CoreGraphcis context reference
CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext]
graphicsPort];
// Make the CGContext coordinate system sane, as expected by Cairo
CGContextTranslateCTM (ctx, 0.0, height);
CGContextScaleCTM (ctx, 1.0, -1.0);
// Create the Cairo surface and context
Cairo::RefPtr<Cairo::QuartzSurface> surface =
Cairo::QuartzSurface::create(ctx, width, height);
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface);
// Cairo-Quartz fallback surfaces don't work properly, so we need to
create a temp. surface like this:
cr->push_group();
//---------- Drawing stuff (put your code in here)
-------------------------------
// Draw a radial gradient (copied and pasted, more or less, from
http://cairographics.org/samples/gradient.html)
cr->scale(width,height);
Cairo::RefPtr<Cairo::RadialGradient> grad2 =
Cairo::RadialGradient::create(0.45, 0.4, 0.1, 0.4, 0.4, 0.5);
grad2->add_color_stop_rgba(0, 1,0,0, 1);
grad2->add_color_stop_rgba(1, 0,1,0, 1);
cr->set_source(grad2);
cairo_arc (cr, 0.5, 0.5, 0.3, 0, 2 * M_PI);
cr->fill();
//---------------------------------------------------------------------
-----------
// Finally, paint the temporary surface we made
cr->pop_group_to_source();
cr->paint();
}
@end
On 21/06/2007, at 7:00 AM, cairo-request at cairographics.org wrote:
> Message: 1
> Date: Wed, 20 Jun 2007 15:52:51 +0200
> From: " Ipacs P?ter " <peter at ipacs.hu>
> Subject: [cairo] [cairo osx] OS X example
> To: cairo at cairographics.org
> Message-ID:
> <6a86db250706200652m24dd6b9di17a881912402da62 at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi there,
>
> I'm quite new to cairo so please excuse me if I ask obvious
> questions:)
>
> I'm trying to build the quartz example from the CVS (
> http://webcvs.cairographics.org/cairo-demo/quartz/) but it fails.
> This is what I get:
> cc -g -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-
> declarations
> -Wredundant-decls `pkg-config --cflags cairo` -c -o main.o main.c
> main.c: In function 'TestWindowEventHandler':
> main.c:65: error: incompatible type for argument 1 of
> 'cairo_quartz_surface_create'
> main.c: In function 'RedrawTimerCallback':
> main.c:188: warning: 'SetRect' is deprecated (declared at
> /System/Library/Frameworks/ApplicationServices.framework/Frameworks/
> QD.framework/Headers/Quickdraw.h:2401)
> main.c: In function 'CreateTestWindow':
> main.c:205: warning: 'SetRect' is deprecated (declared at
> /System/Library/Frameworks/ApplicationServices.framework/Frameworks/
> QD.framework/Headers/Quickdraw.h:2401)
> make: *** [main.o] Error 1
>
> What do you think about it?
>
> Another question:
> Are there any OS X examples around that can be used out-of-the-box?
> I mean
> examples that does not require hundreds of dependencies and can be
> compiled
> by a simple 'make' or something.
>
> Cheers,
> Peter
More information about the cairo
mailing list