]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dfb/wrapdfb.h
split wxControl::Ellipsize() in two functions for better readability of the code...
[wxWidgets.git] / include / wx / dfb / wrapdfb.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dfb/wrapdfb.h
3 // Purpose: wx wrappers for DirectFB interfaces
4 // Author: Vaclav Slavik
5 // Created: 2006-08-23
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_DFB_WRAPDFB_H_
12 #define _WX_DFB_WRAPDFB_H_
13
14 #include "wx/dfb/dfbptr.h"
15 #include "wx/gdicmn.h"
16 #include "wx/vidmode.h"
17
18 #include <directfb.h>
19 #include <directfb_version.h>
20
21 // DFB < 1.0 didn't have u8 type, only __u8
22 #if DIRECTFB_MAJOR_VERSION == 0
23 typedef __u8 u8;
24 #endif
25
26
27 wxDFB_DECLARE_INTERFACE(IDirectFB);
28 wxDFB_DECLARE_INTERFACE(IDirectFBDisplayLayer);
29 wxDFB_DECLARE_INTERFACE(IDirectFBFont);
30 wxDFB_DECLARE_INTERFACE(IDirectFBWindow);
31 wxDFB_DECLARE_INTERFACE(IDirectFBSurface);
32 wxDFB_DECLARE_INTERFACE(IDirectFBPalette);
33 wxDFB_DECLARE_INTERFACE(IDirectFBEventBuffer);
34
35
36 /**
37 Checks the @a code of a DirectFB call and returns true if it was
38 successful and false if it failed, logging the errors as appropriate
39 (asserts for programming errors, wxLogError for runtime failures).
40 */
41 bool wxDfbCheckReturn(DFBResult code);
42
43 //-----------------------------------------------------------------------------
44 // wxDfbEvent
45 //-----------------------------------------------------------------------------
46
47 /**
48 The struct defined by this macro is a thin wrapper around DFB*Event type.
49 It is needed because DFB*Event are typedefs and so we can't forward declare
50 them, but we need to pass them to methods declared in public headers where
51 <directfb.h> cannot be included. So this struct just holds the event value,
52 it's sole purpose is that it can be forward declared.
53 */
54 #define WXDFB_DEFINE_EVENT_WRAPPER(T) \
55 struct wx##T \
56 { \
57 wx##T() {} \
58 wx##T(const T& event) : m_event(event) {} \
59 \
60 operator T&() { return m_event; } \
61 operator const T&() const { return m_event; } \
62 T* operator&() { return &m_event; } \
63 \
64 DFBEventClass GetClass() const { return m_event.clazz; } \
65 \
66 private: \
67 T m_event; \
68 };
69
70 WXDFB_DEFINE_EVENT_WRAPPER(DFBEvent)
71 WXDFB_DEFINE_EVENT_WRAPPER(DFBWindowEvent)
72
73
74 //-----------------------------------------------------------------------------
75 // wxDfbWrapper<T>
76 //-----------------------------------------------------------------------------
77
78 /// Base class for wxDfbWrapper<T>
79 class wxDfbWrapperBase
80 {
81 public:
82 /// Increases reference count of the object
83 void AddRef()
84 {
85 m_refCnt++;
86 }
87
88 /// Decreases reference count and if it reaches zero, deletes the object
89 void Release()
90 {
91 if ( --m_refCnt == 0 )
92 delete this;
93 }
94
95 /// Returns result code of the last call
96 DFBResult GetLastResult() const { return m_lastResult; }
97
98 protected:
99 wxDfbWrapperBase() : m_refCnt(1), m_lastResult(DFB_OK) {}
100
101 /// Dtor may only be called from Release()
102 virtual ~wxDfbWrapperBase() {}
103
104 /**
105 Checks the @a result of a DirectFB call and returns true if it was
106 successful and false if it failed. Also stores result of the call
107 so that it can be obtained by calling GetLastResult().
108 */
109 bool Check(DFBResult result)
110 {
111 m_lastResult = result;
112 return wxDfbCheckReturn(result);
113 }
114
115 protected:
116 /// Reference count
117 unsigned m_refCnt;
118
119 /// Result of the last DirectFB call
120 DFBResult m_lastResult;
121 };
122
123 /**
124 This template is base class for friendly C++ wrapper around DirectFB
125 interface T.
126
127 The wrapper provides same API as DirectFB, with a few exceptions:
128 - methods return true/false instead of error code
129 - methods that return or create another interface return pointer to the
130 interface (or NULL on failure) instead of storing it in the last
131 argument
132 - interface arguments use wxFooPtr type instead of raw DirectFB pointer
133 - methods taking flags use int type instead of an enum when the flags
134 can be or-combination of enum elements (this is workaround for
135 C++-unfriendly DirectFB API)
136 */
137 template<typename T>
138 class wxDfbWrapper : public wxDfbWrapperBase
139 {
140 public:
141 /// "Raw" DirectFB interface type
142 typedef T DirectFBIface;
143
144 /// Returns raw DirectFB pointer
145 T *GetRaw() const { return m_ptr; }
146
147 protected:
148 /// To be called from ctor. Takes ownership of raw object.
149 void Init(T *ptr) { m_ptr = ptr; }
150
151 /// Dtor may only be used from Release
152 ~wxDfbWrapper()
153 {
154 if ( m_ptr )
155 m_ptr->Release(m_ptr);
156 }
157
158 protected:
159 // pointer to DirectFB object
160 T *m_ptr;
161 };
162
163
164 //-----------------------------------------------------------------------------
165 // wxIDirectFBFont
166 //-----------------------------------------------------------------------------
167
168 struct wxIDirectFBFont : public wxDfbWrapper<IDirectFBFont>
169 {
170 wxIDirectFBFont(IDirectFBFont *s) { Init(s); }
171
172 bool GetStringWidth(const char *text, int bytes, int *w)
173 { return Check(m_ptr->GetStringWidth(m_ptr, text, bytes, w)); }
174
175 bool GetStringExtents(const char *text, int bytes,
176 DFBRectangle *logicalRect, DFBRectangle *inkRect)
177 {
178 return Check(m_ptr->GetStringExtents(m_ptr, text, bytes,
179 logicalRect, inkRect));
180 }
181
182 bool GetHeight(int *h)
183 { return Check(m_ptr->GetHeight(m_ptr, h)); }
184
185 bool GetDescender(int *descender)
186 { return Check(m_ptr->GetDescender(m_ptr, descender)); }
187 };
188
189
190 //-----------------------------------------------------------------------------
191 // wxIDirectFBPalette
192 //-----------------------------------------------------------------------------
193
194 struct wxIDirectFBPalette : public wxDfbWrapper<IDirectFBPalette>
195 {
196 wxIDirectFBPalette(IDirectFBPalette *s) { Init(s); }
197 };
198
199
200 //-----------------------------------------------------------------------------
201 // wxIDirectFBSurface
202 //-----------------------------------------------------------------------------
203
204 struct wxIDirectFBSurface : public wxDfbWrapper<IDirectFBSurface>
205 {
206 wxIDirectFBSurface(IDirectFBSurface *s) { Init(s); }
207
208 bool GetSize(int *w, int *h)
209 { return Check(m_ptr->GetSize(m_ptr, w, h)); }
210
211 bool GetCapabilities(DFBSurfaceCapabilities *caps)
212 { return Check(m_ptr->GetCapabilities(m_ptr, caps)); }
213
214 bool GetPixelFormat(DFBSurfacePixelFormat *caps)
215 { return Check(m_ptr->GetPixelFormat(m_ptr, caps)); }
216
217 // convenience version of GetPixelFormat, returns DSPF_UNKNOWN if fails
218 DFBSurfacePixelFormat GetPixelFormat();
219
220 bool SetClip(const DFBRegion *clip)
221 { return Check(m_ptr->SetClip(m_ptr, clip)); }
222
223 bool SetColor(u8 r, u8 g, u8 b, u8 a)
224 { return Check(m_ptr->SetColor(m_ptr, r, g, b, a)); }
225
226 bool Clear(u8 r, u8 g, u8 b, u8 a)
227 { return Check(m_ptr->Clear(m_ptr, r, g, b, a)); }
228
229 bool DrawLine(int x1, int y1, int x2, int y2)
230 { return Check(m_ptr->DrawLine(m_ptr, x1, y1, x2, y2)); }
231
232 bool DrawRectangle(int x, int y, int w, int h)
233 { return Check(m_ptr->DrawRectangle(m_ptr, x, y, w, h)); }
234
235 bool FillRectangle(int x, int y, int w, int h)
236 { return Check(m_ptr->FillRectangle(m_ptr, x, y, w, h)); }
237
238 bool SetFont(const wxIDirectFBFontPtr& font)
239 { return Check(m_ptr->SetFont(m_ptr, font->GetRaw())); }
240
241 bool DrawString(const char *text, int bytes, int x, int y, int flags)
242 {
243 return Check(m_ptr->DrawString(m_ptr, text, bytes, x, y,
244 (DFBSurfaceTextFlags)flags));
245 }
246
247 /**
248 Updates the front buffer from the back buffer. If @a region is not
249 NULL, only given rectangle is updated.
250 */
251 bool FlipToFront(const DFBRegion *region = NULL);
252
253 wxIDirectFBSurfacePtr GetSubSurface(const DFBRectangle *rect)
254 {
255 IDirectFBSurface *s;
256 if ( Check(m_ptr->GetSubSurface(m_ptr, rect, &s)) )
257 return new wxIDirectFBSurface(s);
258 else
259 return NULL;
260 }
261
262 wxIDirectFBPalettePtr GetPalette()
263 {
264 IDirectFBPalette *s;
265 if ( Check(m_ptr->GetPalette(m_ptr, &s)) )
266 return new wxIDirectFBPalette(s);
267 else
268 return NULL;
269 }
270
271 bool SetPalette(const wxIDirectFBPalettePtr& pal)
272 { return Check(m_ptr->SetPalette(m_ptr, pal->GetRaw())); }
273
274 bool SetBlittingFlags(int flags)
275 {
276 return Check(
277 m_ptr->SetBlittingFlags(m_ptr, (DFBSurfaceBlittingFlags)flags));
278 }
279
280 bool Blit(const wxIDirectFBSurfacePtr& source,
281 const DFBRectangle *source_rect,
282 int x, int y)
283 { return Blit(source->GetRaw(), source_rect, x, y); }
284
285 bool Blit(IDirectFBSurface *source,
286 const DFBRectangle *source_rect,
287 int x, int y)
288 { return Check(m_ptr->Blit(m_ptr, source, source_rect, x, y)); }
289
290 bool StretchBlit(const wxIDirectFBSurfacePtr& source,
291 const DFBRectangle *source_rect,
292 const DFBRectangle *dest_rect)
293 {
294 return Check(m_ptr->StretchBlit(m_ptr, source->GetRaw(),
295 source_rect, dest_rect));
296 }
297
298 /// Returns bit depth used by the surface or -1 on error
299 int GetDepth();
300
301 /**
302 Creates a new surface by cloning this one. New surface will have same
303 capabilities, pixel format and pixel data as the existing one.
304
305 @see CreateCompatible
306 */
307 wxIDirectFBSurfacePtr Clone();
308
309 /// Flags for CreateCompatible()
310 enum CreateCompatibleFlags
311 {
312 /// Don't create double-buffered surface
313 CreateCompatible_NoBackBuffer = 1
314 };
315
316 /**
317 Creates a surface compatible with this one, i.e. surface with the same
318 capabilities and pixel format, but with different and size.
319
320 @param size Size of the surface to create. If wxDefaultSize, use the
321 size of this surface.
322 @param flags Or-combination of CreateCompatibleFlags values
323 */
324 wxIDirectFBSurfacePtr CreateCompatible(const wxSize& size = wxDefaultSize,
325 int flags = 0);
326
327 bool Lock(DFBSurfaceLockFlags flags, void **ret_ptr, int *ret_pitch)
328 { return Check(m_ptr->Lock(m_ptr, flags, ret_ptr, ret_pitch)); }
329
330 bool Unlock()
331 { return Check(m_ptr->Unlock(m_ptr)); }
332
333 /// Helper struct for safe locking & unlocking of surfaces
334 struct Locked
335 {
336 Locked(const wxIDirectFBSurfacePtr& surface, DFBSurfaceLockFlags flags)
337 : m_surface(surface)
338 {
339 if ( !surface->Lock(flags, &ptr, &pitch) )
340 ptr = NULL;
341 }
342
343 ~Locked()
344 {
345 if ( ptr )
346 m_surface->Unlock();
347 }
348
349 void *ptr;
350 int pitch;
351
352 private:
353 wxIDirectFBSurfacePtr m_surface;
354 };
355
356
357 private:
358 // this is private because we want user code to use FlipToFront()
359 bool Flip(const DFBRegion *region, int flags);
360 };
361
362
363 //-----------------------------------------------------------------------------
364 // wxIDirectFBEventBuffer
365 //-----------------------------------------------------------------------------
366
367 struct wxIDirectFBEventBuffer : public wxDfbWrapper<IDirectFBEventBuffer>
368 {
369 wxIDirectFBEventBuffer(IDirectFBEventBuffer *s) { Init(s); }
370
371 bool CreateFileDescriptor(int *ret_fd)
372 {
373 return Check(m_ptr->CreateFileDescriptor(m_ptr, ret_fd));
374 }
375 };
376
377
378 //-----------------------------------------------------------------------------
379 // wxIDirectFBWindow
380 //-----------------------------------------------------------------------------
381
382 struct wxIDirectFBWindow : public wxDfbWrapper<IDirectFBWindow>
383 {
384 wxIDirectFBWindow(IDirectFBWindow *s) { Init(s); }
385
386 bool GetID(DFBWindowID *id)
387 { return Check(m_ptr->GetID(m_ptr, id)); }
388
389 bool GetPosition(int *x, int *y)
390 { return Check(m_ptr->GetPosition(m_ptr, x, y)); }
391
392 bool GetSize(int *w, int *h)
393 { return Check(m_ptr->GetSize(m_ptr, w, h)); }
394
395 bool MoveTo(int x, int y)
396 { return Check(m_ptr->MoveTo(m_ptr, x, y)); }
397
398 bool Resize(int w, int h)
399 { return Check(m_ptr->Resize(m_ptr, w, h)); }
400
401 bool SetOpacity(u8 opacity)
402 { return Check(m_ptr->SetOpacity(m_ptr, opacity)); }
403
404 bool SetStackingClass(DFBWindowStackingClass klass)
405 { return Check(m_ptr->SetStackingClass(m_ptr, klass)); }
406
407 bool RaiseToTop()
408 { return Check(m_ptr->RaiseToTop(m_ptr)); }
409
410 bool LowerToBottom()
411 { return Check(m_ptr->LowerToBottom(m_ptr)); }
412
413 wxIDirectFBSurfacePtr GetSurface()
414 {
415 IDirectFBSurface *s;
416 if ( Check(m_ptr->GetSurface(m_ptr, &s)) )
417 return new wxIDirectFBSurface(s);
418 else
419 return NULL;
420 }
421
422 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr& buffer)
423 { return Check(m_ptr->AttachEventBuffer(m_ptr, buffer->GetRaw())); }
424
425 bool RequestFocus()
426 { return Check(m_ptr->RequestFocus(m_ptr)); }
427
428 bool Destroy()
429 { return Check(m_ptr->Destroy(m_ptr)); }
430 };
431
432
433 //-----------------------------------------------------------------------------
434 // wxIDirectFBDisplayLayer
435 //-----------------------------------------------------------------------------
436
437 struct wxIDirectFBDisplayLayer : public wxDfbWrapper<IDirectFBDisplayLayer>
438 {
439 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer *s) { Init(s); }
440
441 wxIDirectFBWindowPtr CreateWindow(const DFBWindowDescription *desc)
442 {
443 IDirectFBWindow *w;
444 if ( Check(m_ptr->CreateWindow(m_ptr, desc, &w)) )
445 return new wxIDirectFBWindow(w);
446 else
447 return NULL;
448 }
449
450 bool GetConfiguration(DFBDisplayLayerConfig *config)
451 { return Check(m_ptr->GetConfiguration(m_ptr, config)); }
452
453 wxVideoMode GetVideoMode();
454
455 bool GetCursorPosition(int *x, int *y)
456 { return Check(m_ptr->GetCursorPosition(m_ptr, x, y)); }
457
458 bool WarpCursor(int x, int y)
459 { return Check(m_ptr->WarpCursor(m_ptr, x, y)); }
460 };
461
462
463 //-----------------------------------------------------------------------------
464 // wxIDirectFB
465 //-----------------------------------------------------------------------------
466
467 struct wxIDirectFB : public wxDfbWrapper<IDirectFB>
468 {
469 /**
470 Returns pointer to DirectFB singleton object, it never returns NULL
471 after wxApp was initialized. The object is cached, so calling this
472 method is cheap.
473 */
474 static wxIDirectFBPtr Get()
475 {
476 if ( !ms_ptr ) CreateDirectFB();
477 return ms_ptr;
478 }
479
480 bool SetVideoMode(int w, int h, int bpp)
481 { return Check(m_ptr->SetVideoMode(m_ptr, w, h, bpp)); }
482
483 wxIDirectFBSurfacePtr CreateSurface(const DFBSurfaceDescription *desc)
484 {
485 IDirectFBSurface *s;
486 if ( Check(m_ptr->CreateSurface(m_ptr, desc, &s)) )
487 return new wxIDirectFBSurface(s);
488 else
489 return NULL;
490 }
491
492 wxIDirectFBEventBufferPtr CreateEventBuffer()
493 {
494 IDirectFBEventBuffer *b;
495 if ( Check(m_ptr->CreateEventBuffer(m_ptr, &b)) )
496 return new wxIDirectFBEventBuffer(b);
497 else
498 return NULL;
499 }
500
501 wxIDirectFBFontPtr CreateFont(const char *filename,
502 const DFBFontDescription *desc)
503 {
504 IDirectFBFont *f;
505 if ( Check(m_ptr->CreateFont(m_ptr, filename, desc, &f)) )
506 return new wxIDirectFBFont(f);
507 else
508 return NULL;
509 }
510
511 wxIDirectFBDisplayLayerPtr
512 GetDisplayLayer(DFBDisplayLayerID id = DLID_PRIMARY)
513 {
514 IDirectFBDisplayLayer *l;
515 if ( Check(m_ptr->GetDisplayLayer(m_ptr, id, &l)) )
516 return new wxIDirectFBDisplayLayer(l);
517 else
518 return NULL;
519 }
520
521 /// Returns primary surface
522 wxIDirectFBSurfacePtr GetPrimarySurface();
523
524 private:
525 wxIDirectFB(IDirectFB *ptr) { Init(ptr); }
526
527 // creates ms_ptr instance
528 static void CreateDirectFB();
529
530 static void CleanUp();
531 friend class wxApp; // calls CleanUp
532
533 // pointer to the singleton IDirectFB object
534 static wxIDirectFBPtr ms_ptr;
535 };
536
537 #endif // _WX_DFB_WRAPDFB_H_