[cairo] [cairo osx] OS X example

Baz brian.ewins at gmail.com
Thu Jun 21 14:41:14 PDT 2007

On 21/06/07, Ipacs Péter <peter at ipacs.hu> wrote:
> Now the stupid-beginner question: how can I use the installed cairomm from
> Xcode? It is installed under /usr/... that is unfortunatelly unusable from
> Xcode + I have no experience using 3rd party libraries within Xcode.

Ok I don't use cairomm (I'm a luddite), so I've included a copy of
TestView.m below with 'straight' cairo calls. This works with current
macports cairo, btw (you could have got Hugo's quartz patch working
with macports cairomm by writing a small custom portfile, but that's
more appropriate for the macports list; and I'm sure something like
what I do below will work for cairomm too anyway).

So as usual you need to help the compiler find the headers, libs, and
pass the appropriate flags to the linker. I'm assuming you created a
new 'Cocoa Application' project and added Hugh's view to the nib. You
should have a Target called something like 'testview'. Double click on
it, and the properties dialog appears. Click on the 'Build' tab. See:

Edit 'Header search paths' and add '/opt/local/include'. Tick 'recursive'.
Edit 'Library search paths' and add '/opt/local/lib'. Tick 'recursive'.
Edit 'Zerolink' and turn it off. (so you see link errors earlier)
Edit 'Other linker flags' and add '-lcairo'

Close the properties dialog, click 'Build and Go'. And you should see
a nice red & green circle in your app window. (if the app links & runs
but the window is grey you most likely didn't add the view in
interface builder)


My copy of TestView.m follows.

#include "TestView.h"
#include <cairo/cairo.h>
#include <cairo/cairo-quartz.h>

#error Need to build Cairo with Quartz support (version 1.4.0 or higher)

@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_surface_t *surface =
cairo_quartz_surface_create_for_cg_context (ctx, width, height);
       cairo_t *cr = cairo_create (surface);

       // Cairo-Quartz fallback surfaces don't work properly, so we
need to create a temp. surface like this:

       //---------- Drawing stuff (put your code in here)
       // Draw a radial gradient (copied and pasted, more or less,
from http://cairographics.org/samples/gradient.html)
       cairo_scale(cr, width,height);
       cairo_pattern_t *grad2 = cairo_pattern_create_radial(0.45, 0.4,
0.1, 0.4, 0.4, 0.5);
       cairo_pattern_add_color_stop_rgba(grad2, 0, 1,0,0, 1);
       cairo_pattern_add_color_stop_rgba(grad2, 1, 0,1,0, 1);
       cairo_set_source(cr, grad2);
       cairo_arc (cr, 0.5, 0.5, 0.3, 0, 2 * M_PI);

       // Finally, paint the temporary surface we made
       // clean up
       cairo_destroy (cr);
       cairo_surface_destroy (surface);


More information about the cairo mailing list