]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dfb/wrapdfb.h
remove old (and unused) MSW-only wxFontEnumerator header
[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 WakeUp()
372 {
373 return Check(m_ptr->WakeUp(m_ptr));
374 }
375
376 bool HasEvent()
377 {
378 // returns DFB_OK if there is >=1 event, DFB_BUFFEREMPTY otherwise
379 DFBResult r = m_ptr->HasEvent(m_ptr);
380
381 // NB: Check() also returns true for DFB_BUFFEREMPTY, so we can't just
382 // return it's return value:
383 Check(r);
384 return (r == DFB_OK);
385 }
386
387 bool WaitForEventWithTimeout(unsigned secs, unsigned millisecs)
388 {
389 DFBResult r = m_ptr->WaitForEventWithTimeout(m_ptr, secs, millisecs);
390
391 // DFB_TIMEOUT is not an error in this function:
392 if ( r == DFB_TIMEOUT )
393 {
394 m_lastResult = DFB_TIMEOUT;
395 return true;
396 }
397
398 return Check(r);
399 }
400
401 bool GetEvent(wxDFBEvent& event)
402 {
403 return Check(m_ptr->GetEvent(m_ptr, &event));
404 }
405 };
406
407
408 //-----------------------------------------------------------------------------
409 // wxIDirectFBWindow
410 //-----------------------------------------------------------------------------
411
412 struct wxIDirectFBWindow : public wxDfbWrapper<IDirectFBWindow>
413 {
414 wxIDirectFBWindow(IDirectFBWindow *s) { Init(s); }
415
416 bool GetID(DFBWindowID *id)
417 { return Check(m_ptr->GetID(m_ptr, id)); }
418
419 bool GetPosition(int *x, int *y)
420 { return Check(m_ptr->GetPosition(m_ptr, x, y)); }
421
422 bool GetSize(int *w, int *h)
423 { return Check(m_ptr->GetSize(m_ptr, w, h)); }
424
425 bool MoveTo(int x, int y)
426 { return Check(m_ptr->MoveTo(m_ptr, x, y)); }
427
428 bool Resize(int w, int h)
429 { return Check(m_ptr->Resize(m_ptr, w, h)); }
430
431 bool SetOpacity(u8 opacity)
432 { return Check(m_ptr->SetOpacity(m_ptr, opacity)); }
433
434 bool SetStackingClass(DFBWindowStackingClass klass)
435 { return Check(m_ptr->SetStackingClass(m_ptr, klass)); }
436
437 bool RaiseToTop()
438 { return Check(m_ptr->RaiseToTop(m_ptr)); }
439
440 bool LowerToBottom()
441 { return Check(m_ptr->LowerToBottom(m_ptr)); }
442
443 wxIDirectFBSurfacePtr GetSurface()
444 {
445 IDirectFBSurface *s;
446 if ( Check(m_ptr->GetSurface(m_ptr, &s)) )
447 return new wxIDirectFBSurface(s);
448 else
449 return NULL;
450 }
451
452 bool AttachEventBuffer(const wxIDirectFBEventBufferPtr& buffer)
453 { return Check(m_ptr->AttachEventBuffer(m_ptr, buffer->GetRaw())); }
454
455 bool RequestFocus()
456 { return Check(m_ptr->RequestFocus(m_ptr)); }
457
458 bool Destroy()
459 { return Check(m_ptr->Destroy(m_ptr)); }
460 };
461
462
463 //-----------------------------------------------------------------------------
464 // wxIDirectFBDisplayLayer
465 //-----------------------------------------------------------------------------
466
467 struct wxIDirectFBDisplayLayer : public wxDfbWrapper<IDirectFBDisplayLayer>
468 {
469 wxIDirectFBDisplayLayer(IDirectFBDisplayLayer *s) { Init(s); }
470
471 wxIDirectFBWindowPtr CreateWindow(const DFBWindowDescription *desc)
472 {
473 IDirectFBWindow *w;
474 if ( Check(m_ptr->CreateWindow(m_ptr, desc, &w)) )
475 return new wxIDirectFBWindow(w);
476 else
477 return NULL;
478 }
479
480 bool GetConfiguration(DFBDisplayLayerConfig *config)
481 { return Check(m_ptr->GetConfiguration(m_ptr, config)); }
482
483 wxVideoMode GetVideoMode();
484
485 bool GetCursorPosition(int *x, int *y)
486 { return Check(m_ptr->GetCursorPosition(m_ptr, x, y)); }
487
488 bool WarpCursor(int x, int y)
489 { return Check(m_ptr->WarpCursor(m_ptr, x, y)); }
490 };
491
492
493 //-----------------------------------------------------------------------------
494 // wxIDirectFB
495 //-----------------------------------------------------------------------------
496
497 struct wxIDirectFB : public wxDfbWrapper<IDirectFB>
498 {
499 /**
500 Returns pointer to DirectFB singleton object, it never returns NULL
501 after wxApp was initialized. The object is cached, so calling this
502 method is cheap.
503 */
504 static wxIDirectFBPtr Get()
505 {
506 if ( !ms_ptr ) CreateDirectFB();
507 return ms_ptr;
508 }
509
510 bool SetVideoMode(int w, int h, int bpp)
511 { return Check(m_ptr->SetVideoMode(m_ptr, w, h, bpp)); }
512
513 wxIDirectFBSurfacePtr CreateSurface(const DFBSurfaceDescription *desc)
514 {
515 IDirectFBSurface *s;
516 if ( Check(m_ptr->CreateSurface(m_ptr, desc, &s)) )
517 return new wxIDirectFBSurface(s);
518 else
519 return NULL;
520 }
521
522 wxIDirectFBEventBufferPtr CreateEventBuffer()
523 {
524 IDirectFBEventBuffer *b;
525 if ( Check(m_ptr->CreateEventBuffer(m_ptr, &b)) )
526 return new wxIDirectFBEventBuffer(b);
527 else
528 return NULL;
529 }
530
531 wxIDirectFBFontPtr CreateFont(const char *filename,
532 const DFBFontDescription *desc)
533 {
534 IDirectFBFont *f;
535 if ( Check(m_ptr->CreateFont(m_ptr, filename, desc, &f)) )
536 return new wxIDirectFBFont(f);
537 else
538 return NULL;
539 }
540
541 wxIDirectFBDisplayLayerPtr
542 GetDisplayLayer(DFBDisplayLayerID id = DLID_PRIMARY)
543 {
544 IDirectFBDisplayLayer *l;
545 if ( Check(m_ptr->GetDisplayLayer(m_ptr, id, &l)) )
546 return new wxIDirectFBDisplayLayer(l);
547 else
548 return NULL;
549 }
550
551 /// Returns primary surface
552 wxIDirectFBSurfacePtr GetPrimarySurface();
553
554 private:
555 wxIDirectFB(IDirectFB *ptr) { Init(ptr); }
556
557 // creates ms_ptr instance
558 static void CreateDirectFB();
559
560 static void CleanUp();
561 friend class wxApp; // calls CleanUp
562
563 // pointer to the singleton IDirectFB object
564 static wxIDirectFBPtr ms_ptr;
565 };
566
567 #endif // _WX_DFB_WRAPDFB_H_