use C++ wrappers around DirectFB API for easier use
[wxWidgets.git] / src / dfb / toplevel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/toplevel.cpp
3 // Purpose: Top level window, abstraction of wxFrame and wxDialog
4 // Author: Vaclav Slavik
5 // Created: 2006-08-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/toplevel.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/dynarray.h"
19 #endif // WX_PRECOMP
20
21 #include "wx/hashmap.h"
22 #include "wx/evtloop.h"
23 #include "wx/dfb/private.h"
24
25 #define TRACE_EVENTS _T("events")
26 #define TRACE_PAINT _T("paint")
27
28 // ============================================================================
29 // globals
30 // ============================================================================
31
32 // mapping of DirectFB windows to wxTLWs:
33 WX_DECLARE_HASH_MAP(DFBWindowID, wxTopLevelWindowDFB*,
34 wxIntegerHash, wxIntegerEqual,
35 wxDfbWindowsMap);
36 static wxDfbWindowsMap gs_dfbWindowsMap;
37
38 // ============================================================================
39 // helpers
40 // ============================================================================
41
42 struct wxDfbPaintRequest
43 {
44 wxDfbPaintRequest(const wxRect& rect, bool eraseBackground)
45 : m_rect(rect), m_eraseBackground(eraseBackground) {}
46 wxDfbPaintRequest(const wxDfbPaintRequest& r)
47 : m_rect(r.m_rect), m_eraseBackground(r.m_eraseBackground) {}
48
49 wxRect m_rect;
50 bool m_eraseBackground;
51 };
52
53 WX_DEFINE_ARRAY_PTR(wxDfbPaintRequest*, wxDfbQueuedPaintRequests);
54
55 // ============================================================================
56 // wxTopLevelWindowDFB
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // creation & destruction
61 // ----------------------------------------------------------------------------
62
63 void wxTopLevelWindowDFB::Init()
64 {
65 m_isShown = false;
66 m_isMaximized = false;
67 m_fsIsShowing = false;
68 m_sizeSet = false;
69 m_opacity = 255;
70 m_toPaint = new wxDfbQueuedPaintRequests;
71 }
72
73 bool wxTopLevelWindowDFB::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxString& title,
76 const wxPoint& posOrig,
77 const wxSize& sizeOrig,
78 long style,
79 const wxString &name)
80 {
81 m_tlw = this;
82
83 // always create a frame of some reasonable, even if arbitrary, size (at
84 // least for MSW compatibility)
85 wxSize size(sizeOrig);
86 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord )
87 {
88 wxSize sizeDefault = GetDefaultSize();
89 if ( size.x == wxDefaultCoord )
90 size.x = sizeDefault.x;
91 if ( size.y == wxDefaultCoord )
92 size.y = sizeDefault.y;
93 }
94
95 wxPoint pos(posOrig);
96 if ( pos.x == wxDefaultCoord )
97 pos.x = 0;
98 if ( pos.y == wxDefaultCoord )
99 pos.y = 0;
100
101 // create DirectFB window:
102 wxIDirectFBDisplayLayerPtr layer = wxDfbGetDisplayLayer();
103 wxCHECK_MSG( layer, false, _T("no display layer") );
104
105 DFBWindowDescription desc;
106 desc.flags = (DFBWindowDescriptionFlags)
107 (DWDESC_CAPS |
108 DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY);
109 desc.caps = DWCAPS_DOUBLEBUFFER;
110 desc.posx = pos.x;
111 desc.posy = pos.y;
112 desc.width = size.x;
113 desc.height = size.y;
114 m_dfbwin = layer->CreateWindow(&desc);
115 if ( !layer )
116 return false;
117
118 // add the new TLW to DFBWindowID->wxTLW map:
119 DFBWindowID winid;
120 if ( !m_dfbwin->GetID(&winid) )
121 return false;
122 gs_dfbWindowsMap[winid] = this;
123
124 // TLWs are created initially hidden:
125 if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) )
126 return false;
127
128 wxWindow::Create(NULL, id, pos, size, style, name);
129
130 SetParent(parent);
131 if ( parent )
132 parent->AddChild(this);
133
134 wxTopLevelWindows.Append(this);
135 m_title = title;
136
137 if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) )
138 {
139 m_dfbwin->SetStackingClass(DWSC_UPPER);
140 }
141
142 // direct events in this window to the global event buffer:
143 m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
144
145 return true;
146 }
147
148 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
149 {
150 m_isBeingDeleted = true;
151
152 wxTopLevelWindows.DeleteObject(this);
153
154 if ( wxTheApp->GetTopWindow() == this )
155 wxTheApp->SetTopWindow(NULL);
156
157 if ( wxTopLevelWindows.empty() && wxTheApp->GetExitOnFrameDelete() )
158 {
159 wxTheApp->ExitMainLoop();
160 }
161
162 WX_CLEAR_ARRAY(*m_toPaint);
163 wxDELETE(m_toPaint);
164
165 // remove the TLW from DFBWindowID->wxTLW map:
166 DFBWindowID winid;
167 if ( m_dfbwin->GetID(&winid) )
168 gs_dfbWindowsMap.erase(winid);
169 }
170
171 // ----------------------------------------------------------------------------
172 // window size & position
173 // ----------------------------------------------------------------------------
174
175 void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const
176 {
177 m_dfbwin->GetPosition(x, y);
178 }
179
180 void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const
181 {
182 m_dfbwin->GetSize(width, height);
183 }
184
185 void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height)
186 {
187 wxPoint curpos = GetPosition();
188 if ( curpos.x != x || curpos.y != y )
189 {
190 m_dfbwin->MoveTo(x, y);
191 }
192
193 wxSize cursize = GetSize();
194 if ( cursize.x != width || cursize.y != height )
195 {
196 m_dfbwin->Resize(width, height);
197 // we must repaint the window after it changed size:
198 Refresh();
199 }
200 }
201
202 // ----------------------------------------------------------------------------
203 // showing and hiding
204 // ----------------------------------------------------------------------------
205
206 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
207 bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style)
208 {
209 if (show == m_fsIsShowing) return false; // return what?
210
211 m_fsIsShowing = show;
212
213 if (show)
214 {
215 m_fsSaveStyle = m_windowStyle;
216 m_fsSaveFlag = style;
217 GetPosition(&m_fsSaveFrame.x, &m_fsSaveFrame.y);
218 GetSize(&m_fsSaveFrame.width, &m_fsSaveFrame.height);
219
220 if ( style & wxFULLSCREEN_NOCAPTION )
221 m_windowStyle &= ~wxCAPTION;
222 if ( style & wxFULLSCREEN_NOBORDER )
223 m_windowStyle = wxSIMPLE_BORDER;
224
225 int x, y;
226 wxDisplaySize(&x, &y);
227 SetSize(0, 0, x, y);
228 }
229 else
230 {
231 m_windowStyle = m_fsSaveStyle;
232 SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y,
233 m_fsSaveFrame.width, m_fsSaveFrame.height);
234 }
235
236 return true;
237 }
238
239 bool wxTopLevelWindowDFB::Show(bool show)
240 {
241 if ( !wxTopLevelWindowBase::Show(show) )
242 return false;
243
244 // hide/show the window by setting its opacity to 0/full:
245 m_dfbwin->SetOpacity(show ? m_opacity : 0);
246
247 // If this is the first time Show was called, send size event,
248 // so that the frame can adjust itself (think auto layout or single child)
249 if ( !m_sizeSet )
250 {
251 m_sizeSet = true;
252 wxSizeEvent event(GetSize(), GetId());
253 event.SetEventObject(this);
254 GetEventHandler()->ProcessEvent(event);
255 }
256
257 // FIXME_DFB: do this at all?
258 if ( show && AcceptsFocus() )
259 SetFocus();
260 // FIXME_DFB -- don't do this for popup windows?
261
262 return true;
263 }
264
265 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha)
266 {
267 if ( IsShown() )
268 {
269 if ( !m_dfbwin->SetOpacity(alpha) )
270 return false;
271 }
272
273 m_opacity = alpha;
274 return true;
275 }
276
277 // ----------------------------------------------------------------------------
278 // maximize, minimize etc.
279 // ----------------------------------------------------------------------------
280
281 void wxTopLevelWindowDFB::Maximize(bool maximize)
282 {
283 int x, y, w, h;
284 wxClientDisplayRect(&x, &y, &w, &h);
285
286 if ( maximize && !m_isMaximized )
287 {
288 m_isMaximized = true;
289
290 GetPosition(&m_savedFrame.x, &m_savedFrame.y);
291 GetSize(&m_savedFrame.width, &m_savedFrame.height);
292
293 SetSize(x, y, w, h);
294 }
295 else if ( !maximize && m_isMaximized )
296 {
297 m_isMaximized = false;
298 SetSize(m_savedFrame.x, m_savedFrame.y,
299 m_savedFrame.width, m_savedFrame.height);
300 }
301 }
302
303 bool wxTopLevelWindowDFB::IsMaximized() const
304 {
305 return m_isMaximized;
306 }
307
308 void wxTopLevelWindowDFB::Restore()
309 {
310 if ( IsMaximized() )
311 {
312 Maximize(false);
313 }
314 }
315
316 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize))
317 {
318 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
319 }
320
321 bool wxTopLevelWindowDFB::IsIconized() const
322 {
323 return false;
324 }
325
326
327 // ----------------------------------------------------------------------------
328 // surfaces and painting
329 // ----------------------------------------------------------------------------
330
331 wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const
332 {
333 return m_dfbwin->GetSurface();
334 }
335
336 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
337 {
338 wxDfbQueuedPaintRequests& toPaint = *m_toPaint;
339 if ( toPaint.empty() )
340 return; // nothing to do
341
342 // process queued paint requests:
343 wxRect winRect(wxPoint(0, 0), GetSize());
344 wxRect paintedRect;
345
346 size_t cnt = toPaint.size();
347 for ( size_t i = 0; i < cnt; ++i )
348 {
349 const wxDfbPaintRequest& request = *toPaint[i];
350
351 wxRect clipped(request.m_rect);
352
353 wxLogTrace(TRACE_PAINT,
354 _T("%p ('%s'): processing paint request [x=%i,y=%i,w=%i,h=%i]"),
355 this, GetName().c_str(),
356 clipped.x, clipped.y, clipped.width, clipped.height);
357
358 clipped.Intersect(winRect);
359 if ( clipped.IsEmpty() )
360 continue; // nothing to refresh
361
362 PaintWindow(clipped, request.m_eraseBackground);
363
364 // remember rectangle covering all repainted areas:
365 if ( paintedRect.IsEmpty() )
366 paintedRect = clipped;
367 else
368 paintedRect.Union(clipped);
369 }
370
371 WX_CLEAR_ARRAY(toPaint);
372
373 if ( paintedRect.IsEmpty() )
374 return; // no painting occurred, no need to flip
375
376 // flip the surface to make the changes visible:
377 DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(),
378 paintedRect.GetRight(), paintedRect.GetBottom()};
379 DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r;
380
381 GetDfbSurface()->Flip(rptr, DSFLIP_NONE);
382 }
383
384 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect, bool eraseBack)
385 {
386 // defer paiting until idle time or until Update() is called:
387 m_toPaint->push_back(new wxDfbPaintRequest(rect, eraseBack));
388 }
389
390 void wxTopLevelWindowDFB::Update()
391 {
392 HandleQueuedPaintRequests();
393 }
394
395 // ---------------------------------------------------------------------------
396 // events handling
397 // ---------------------------------------------------------------------------
398
399 /* static */
400 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_)
401 {
402 const DFBWindowEvent& event = event_;
403
404 if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() )
405 {
406 wxLogTrace(TRACE_EVENTS,
407 _T("received event for unknown DirectFB window, ignoring"));
408 return;
409 }
410
411 wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id];
412 wxWindow *recipient = NULL;
413 void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL;
414
415 switch ( event.type )
416 {
417 case DWET_KEYDOWN:
418 case DWET_KEYUP:
419 {
420 recipient = wxWindow::FindFocus();
421 handlerFunc = &wxWindowDFB::HandleKeyEvent;
422 break;
423 }
424
425 case DWET_NONE:
426 case DWET_ALL:
427 {
428 wxFAIL_MSG( _T("invalid event type") );
429 break;
430 }
431 }
432
433 if ( !recipient )
434 {
435 wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window"));
436 return;
437 }
438
439 wxCHECK_RET( recipient && recipient->GetTLW() == tlw,
440 _T("event recipient not in TLW which received the event") );
441
442 // process the event:
443 (recipient->*handlerFunc)(event_);
444 }
445
446 // ---------------------------------------------------------------------------
447 // idle events processing
448 // ---------------------------------------------------------------------------
449
450 void wxTopLevelWindowDFB::OnInternalIdle()
451 {
452 wxTopLevelWindowBase::OnInternalIdle();
453 HandleQueuedPaintRequests();
454 }