]> git.saurik.com Git - wxWidgets.git/blob - src/common/cairo.cpp
Fix horizontal mouse wheel scrolling in wxGTK.
[wxWidgets.git] / src / common / cairo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cairo.cpp
3 // Purpose: Cairo library
4 // Author: Anthony Betaudeau
5 // Created: 2007-08-25
6 // Copyright: (c) Anthony Bretaudeau
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_CAIRO
19
20 // keep cairo.h from defining dllimport as we're defining the symbols inside
21 // the wx dll in order to load them dynamically.
22 #define cairo_public
23
24 #include <cairo.h>
25 #include "wx/dynlib.h"
26
27 #ifdef __WXMSW__
28 #include "wx/msw/wrapwin.h"
29 #endif
30
31 #ifdef __WXMAC__
32 #include "wx/osx/private.h"
33 #include <cairo-quartz.h>
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/module.h"
38 #include "wx/log.h"
39 #endif
40
41 #define wxCAIRO_METHOD_TYPE(name) \
42 wxCairo##name##_t
43
44 #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \
45 static wxCAIRO_METHOD_TYPE(name) name;
46
47 #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \
48 wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL)
49
50 #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \
51 m( cairo_append_path, \
52 (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \
53 m( cairo_arc, \
54 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
55 m( cairo_arc_negative, \
56 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
57 m( cairo_clip, \
58 (cairo_t *cr), (cr) ) \
59 m( cairo_close_path, \
60 (cairo_t *cr), (cr) ) \
61 m( cairo_curve_to, \
62 (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \
63 m( cairo_destroy, \
64 (cairo_t *cr), (cr) ) \
65 m( cairo_fill, \
66 (cairo_t *cr), (cr) ) \
67 m( cairo_fill_preserve, \
68 (cairo_t *cr), (cr) ) \
69 m( cairo_font_extents, \
70 (cairo_t *cr, cairo_font_extents_t *extents), (cr, extents) ) \
71 m( cairo_font_face_destroy, \
72 (cairo_font_face_t *font_face), (font_face) ) \
73 m( cairo_get_current_point, \
74 (cairo_t *cr, double *x, double *y), (cr, x, y) ) \
75 m( cairo_get_matrix, \
76 (cairo_t *cr, cairo_matrix_t *matrix), (cr, matrix) ) \
77 m( cairo_line_to, \
78 (cairo_t *cr, double x, double y), (cr, x, y) ) \
79 m( cairo_matrix_init, \
80 (cairo_matrix_t *matrix, double xx, double yx, double xy, double yy, double x0, double y0), (matrix, xx, yx, xy, yy, x0, y0) ) \
81 m( cairo_matrix_multiply, \
82 (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b), (result, a, b) ) \
83 m( cairo_matrix_rotate, \
84 (cairo_matrix_t *matrix, double radians), (matrix, radians) ) \
85 m( cairo_matrix_scale, \
86 (cairo_matrix_t *matrix, double sx, double sy), (matrix, sx, sy) ) \
87 m( cairo_matrix_transform_distance, \
88 (const cairo_matrix_t *matrix, double *dx, double *dy), (matrix, dx, dy) ) \
89 m( cairo_matrix_transform_point, \
90 (const cairo_matrix_t *matrix, double *x, double *y), (matrix, x, y) ) \
91 m( cairo_matrix_translate, \
92 (cairo_matrix_t *matrix, double tx, double ty), (matrix, tx, ty) ) \
93 m( cairo_move_to, \
94 (cairo_t *cr, double x, double y), (cr, x, y) ) \
95 m( cairo_new_path, \
96 (cairo_t *cr), (cr) ) \
97 m( cairo_paint, \
98 (cairo_t *cr), (cr) ) \
99 m( cairo_paint_with_alpha, \
100 (cairo_t *cr, double alpha), (cr, alpha) ) \
101 m( cairo_path_destroy, \
102 (cairo_path_t *path), (path) ) \
103 m( cairo_pattern_add_color_stop_rgba, \
104 (cairo_pattern_t *pattern, double offset, double red, double green, double blue, double alpha), (pattern, offset, red, green, blue, alpha) ) \
105 m( cairo_pattern_destroy, \
106 (cairo_pattern_t *pattern), (pattern) ) \
107 m( cairo_pattern_set_extend, \
108 (cairo_pattern_t *pattern, cairo_extend_t extend), (pattern, extend) ) \
109 m( cairo_pattern_set_filter, \
110 (cairo_pattern_t *pattern, cairo_filter_t filter), (pattern, filter) ) \
111 m( cairo_pop_group_to_source, \
112 (cairo_t *cr), (cr) ) \
113 m( cairo_push_group, \
114 (cairo_t *cr), (cr) ) \
115 m( cairo_rectangle, \
116 (cairo_t *cr, double x, double y, double width, double height), (cr, x, y, width, height) ) \
117 m( cairo_reset_clip, \
118 (cairo_t *cr), (cr) ) \
119 m( cairo_restore, \
120 (cairo_t *cr), (cr) ) \
121 m( cairo_rotate, \
122 (cairo_t *cr, double angle), (cr, angle) ) \
123 m( cairo_save, \
124 (cairo_t *cr), (cr) ) \
125 m( cairo_scale, \
126 (cairo_t *cr, double sx, double sy), (cr, sx, sy) ) \
127 m( cairo_select_font_face, \
128 (cairo_t *cr, const char *family, cairo_font_slant_t slant, cairo_font_weight_t weight), (cr, family, slant, weight) ) \
129 m( cairo_set_antialias, \
130 (cairo_t *cr, cairo_antialias_t antialias), (cr, antialias) ) \
131 m( cairo_set_dash, \
132 (cairo_t *cr, const double *dashes, int num_dashes, double offset), (cr, dashes, num_dashes, offset) ) \
133 m( cairo_set_fill_rule, \
134 (cairo_t *cr, cairo_fill_rule_t fill_rule), (cr, fill_rule) ) \
135 m( cairo_set_font_face, \
136 (cairo_t *cr, cairo_font_face_t *font_face), (cr, font_face) ) \
137 m( cairo_set_font_size, \
138 (cairo_t *cr, double size), (cr, size) ) \
139 m( cairo_set_line_cap, \
140 (cairo_t *cr, cairo_line_cap_t line_cap), (cr, line_cap) ) \
141 m( cairo_set_line_join, \
142 (cairo_t *cr, cairo_line_join_t line_join), (cr, line_join) ) \
143 m( cairo_set_line_width, \
144 (cairo_t *cr, double width), (cr, width) ) \
145 m( cairo_set_matrix, \
146 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
147 m( cairo_set_operator, \
148 (cairo_t *cr, cairo_operator_t op), (cr, op) ) \
149 m( cairo_set_source, \
150 (cairo_t *cr, cairo_pattern_t *source), (cr, source) ) \
151 m( cairo_set_source_rgba, \
152 (cairo_t *cr, double red, double green, double blue, double alpha), (cr, red, green, blue, alpha) ) \
153 m( cairo_show_text, \
154 (cairo_t *cr, const char *utf8), (cr, utf8) ) \
155 m( cairo_stroke, \
156 (cairo_t *cr), (cr) ) \
157 m( cairo_stroke_extents, \
158 (cairo_t *cr, double *x1, double *y1, double *x2, double *y2), (cr, x1, y1, x2, y2) ) \
159 m( cairo_stroke_preserve, \
160 (cairo_t *cr), (cr) ) \
161 m( cairo_surface_destroy, \
162 (cairo_surface_t *surface), (surface) ) \
163 m( cairo_text_extents, \
164 (cairo_t *cr, const char *utf8, cairo_text_extents_t *extents), (cr, utf8, extents) ) \
165 m( cairo_transform, \
166 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
167 m( cairo_translate, \
168 (cairo_t *cr, double tx, double ty), (cr, tx, ty) ) \
169
170 #ifdef __WXMAC__
171 #define wxCAIRO_PLATFORM_METHODS(m) \
172 m( cairo_font_face_t*, cairo_quartz_font_face_create_for_cgfont, \
173 (CGFontRef font), (font), NULL ) \
174 m( cairo_surface_t*, cairo_quartz_surface_create_for_cg_context, \
175 (CGContextRef cgContext, unsigned int width, unsigned int height), (cgContext, width, height), NULL )
176 #elif defined(__WXMSW__)
177 #define wxCAIRO_PLATFORM_METHODS(m) \
178 m( cairo_surface_t*, cairo_win32_surface_create, \
179 (HDC hdc), (hdc), NULL ) \
180 m( cairo_surface_t*, cairo_win32_printing_surface_create, \
181 (HDC hdc), (hdc), NULL )
182 #else
183 #define wxCAIRO_PLATFORM_METHODS(m)
184 #endif
185
186 #define wxFOR_ALL_CAIRO_METHODS(m) \
187 m( cairo_path_t*, cairo_copy_path, \
188 (cairo_t *cr), (cr), NULL ) \
189 m( cairo_t*, cairo_create, \
190 (cairo_surface_t *target), (target), NULL) \
191 m( cairo_surface_t*, cairo_get_target, \
192 (cairo_t *cr), (cr), NULL) \
193 m( cairo_surface_t*, cairo_image_surface_create, \
194 (cairo_format_t format, int width, int height), (format, width, height), NULL ) \
195 m( cairo_surface_t*, cairo_image_surface_create_for_data, \
196 (unsigned char *data, cairo_format_t format, int width, int height, int stride), (data, format, width, height, stride), NULL) \
197 m( cairo_bool_t, cairo_in_fill, \
198 (cairo_t *cr, double x, double y), (cr, x, y), false ) \
199 m( cairo_status_t, cairo_matrix_invert, \
200 (cairo_matrix_t *matrix), (matrix), NULL) \
201 m( cairo_pattern_t*, cairo_pattern_create_for_surface, \
202 (cairo_surface_t *surface), (surface), NULL) \
203 m( cairo_pattern_t*, cairo_pattern_create_linear, \
204 (double x0, double y0, double x1, double y1), (x0, y0, x1, y1), NULL) \
205 m( cairo_pattern_t*, cairo_pattern_create_radial, \
206 (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1), (cx0, cy0, radius0, cx1, cy1, radius1), NULL) \
207 m( cairo_status_t, cairo_pattern_status, \
208 (cairo_pattern_t *pattern), (pattern), 4) \
209 m( cairo_t*, cairo_reference, \
210 (cairo_t *cr), (cr), NULL ) \
211 m( cairo_surface_t*, cairo_surface_create_similar, \
212 (cairo_surface_t *other, cairo_content_t content, int width, int height), (other, content, width, height), NULL) \
213 m( int, cairo_format_stride_for_width, \
214 (cairo_format_t format, int width), (format, width), 0) \
215 m( int, cairo_version, \
216 (), (), 0) \
217 m( int, cairo_image_surface_get_stride, \
218 (cairo_surface_t *surface), (surface), 0) \
219 m( unsigned char *, cairo_image_surface_get_data, \
220 (cairo_surface_t *surface), (surface), NULL) \
221 m( cairo_format_t, cairo_image_surface_get_format, \
222 (cairo_surface_t *surface), (surface), CAIRO_FORMAT_INVALID) \
223 m( cairo_surface_type_t, cairo_surface_get_type, \
224 (cairo_surface_t *surface), (surface), -1) \
225 wxCAIRO_PLATFORM_METHODS(m)
226
227
228 #if wxUSE_PANGO
229 #define wxFOR_ALL_PANGO_CAIRO_VOIDMETHODS(m) \
230 m( pango_cairo_update_layout, \
231 (cairo_t *cr, PangoLayout *layout), (cr, layout) ) \
232 m( pango_cairo_show_layout, \
233 (cairo_t *cr, PangoLayout *layout), (cr, layout) )
234 #endif
235
236 #define wxCAIRO_DECLARE_TYPE(rettype, name, args, argnames, defret) \
237 typedef rettype (*wxCAIRO_METHOD_TYPE(name)) args ; \
238 wxCAIRO_METHOD_TYPE(name) wxDL_METHOD_NAME(name);
239
240 #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
241 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
242
243 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE)
244 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE)
245
246
247 class wxCairo
248 {
249 public:
250 static bool Initialize();
251
252 // for internal use only
253 static void CleanUp();
254
255 private:
256 // the single wxCairo instance or NULL
257 static wxCairo *ms_lib;
258
259 wxCairo();
260 ~wxCairo();
261
262 bool IsOk();
263
264 wxDynamicLibrary m_libCairo;
265 wxDynamicLibrary m_libPangoCairo;
266
267 // true if we successfully loaded the libraries and can use them
268 //
269 // note that this field must have this name as it's used by wxDL_XXX macros
270 bool m_ok;
271
272 public:
273
274 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_STATIC_VOIDMETHOD_DEFINE)
275 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_STATIC_METHOD_DEFINE)
276 #if wxUSE_PANGO // untested, uncomment to test compilation.
277 //wxFOR_ALL_PANGO_METHODS(wxDL_STATIC_METHOD_DEFINE)
278 #endif
279
280 wxDECLARE_NO_COPY_CLASS(wxCairo);
281 };
282
283 #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
284 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
285
286 #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
287 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
288
289 wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC)
290 wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC)
291
292 #undef wxINIT_CAIRO_FUNC
293
294 wxCairo *wxCairo::ms_lib = NULL;
295
296 //----------------------------------------------------------------------------
297 // wxCairoLibrary
298 //----------------------------------------------------------------------------
299
300 wxCairo::wxCairo()
301 {
302 wxLogNull log;
303
304 #ifdef __WXMSW__
305 wxString cairoDllStr("libcairo-2.dll");
306 #else
307 wxString cairoDllStr("libcairo.so.2");
308 #endif
309 m_libCairo.Load(cairoDllStr);
310 m_ok = m_libCairo.IsLoaded();
311 if ( !m_ok )
312 return;
313
314 #if wxUSE_PANGO
315 m_libPangoCairo.Load("libpangocairo-1.0.so.0");
316 m_ok = m_libPangoCairo.IsLoaded();
317 if ( !m_ok )
318 {
319 m_libCairo.Unload();
320 return;
321 }
322 #endif
323
324
325 #define wxDO_LOAD_FUNC(name, nameStr) \
326 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
327 if ( !name ) \
328 return;
329
330 #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
331 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
332
333 #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
334 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
335
336 wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC)
337 wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC)
338
339 #undef wxLOAD_CAIRO_FUNC
340
341 m_ok = true;
342 }
343
344 wxCairo::~wxCairo()
345 {
346 }
347
348 /* static */ bool wxCairo::Initialize()
349 {
350 if ( !ms_lib )
351 {
352 ms_lib = new wxCairo();
353 if ( !ms_lib->IsOk() )
354 {
355 delete ms_lib;
356 ms_lib = NULL;
357 }
358 }
359
360 return ms_lib != NULL;
361 }
362
363 /* static */ void wxCairo::CleanUp()
364 {
365 if (ms_lib)
366 {
367 delete ms_lib;
368 ms_lib = NULL;
369 }
370 }
371
372 bool wxCairo::IsOk()
373 {
374 return m_ok;
375 }
376
377 // ============================================================================
378 // implementation of the functions themselves
379 // ============================================================================
380
381 bool wxCairoInit()
382 {
383 return wxCairo::Initialize();
384 }
385
386 extern "C"
387 {
388
389 #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
390 rettype name params \
391 { \
392 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
393 return wxCairo::name args; \
394 }
395
396 #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
397 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
398
399 // we currently link directly to Cairo on GTK since it is usually available there,
400 // so don't use our cairo_xyz wrapper functions until the decision is made to
401 // always load Cairo dynamically there.
402 #ifndef __WXGTK__
403 wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC)
404 wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC)
405 #endif
406
407 } // extern "C"
408
409 //----------------------------------------------------------------------------
410 // wxCairoModule
411 //----------------------------------------------------------------------------
412
413 class wxCairoModule : public wxModule
414 {
415 public:
416 wxCairoModule() { }
417 virtual bool OnInit();
418 virtual void OnExit();
419
420 private:
421 DECLARE_DYNAMIC_CLASS(wxCairoModule)
422 };
423
424 bool wxCairoModule::OnInit()
425 {
426 return true;
427 }
428
429 void wxCairoModule::OnExit()
430 {
431 wxCairo::CleanUp();
432 }
433
434 IMPLEMENT_DYNAMIC_CLASS(wxCairoModule, wxModule)
435
436 #endif // wxUSE_CAIRO