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