destroy IDirectFBWindow in wxTLW dtor
[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 #endif // WX_PRECOMP
19
20 #include "wx/hashmap.h"
21 #include "wx/evtloop.h"
22 #include "wx/dfb/private.h"
23
24 #define TRACE_EVENTS _T("events")
25 #define TRACE_PAINT _T("paint")
26
27 // ============================================================================
28 // globals
29 // ============================================================================
30
31 // mapping of DirectFB windows to wxTLWs:
32 WX_DECLARE_HASH_MAP(DFBWindowID, wxTopLevelWindowDFB*,
33 wxIntegerHash, wxIntegerEqual,
34 wxDfbWindowsMap);
35 static wxDfbWindowsMap gs_dfbWindowsMap;
36
37 // ============================================================================
38 // helpers
39 // ============================================================================
40
41 // Queue of paint requests
42 class wxDfbQueuedPaintRequests
43 {
44 public:
45 ~wxDfbQueuedPaintRequests() { Clear(); }
46
47 // Adds paint request to the queue
48 void Add(const wxRect& rect)
49 {
50 // We use a simple implementation here for now: all refresh requests
51 // are merged together into single rectangle that is superset of
52 // all the requested rectangles. This wastes some blitting and painting
53 // time, but OTOH, EVT_PAINT handler is called only once per window.
54 m_invalidated.Union(rect);
55 }
56
57 // Is the queue empty?
58 bool IsEmpty() const { return m_invalidated.IsEmpty(); }
59
60 // Empties the queue
61 void Clear() { m_invalidated = wxRect(); }
62
63 // Gets the next request in the queue, returns true if there was one,
64 // false if the queue was empty
65 bool GetNext(wxRect& rect)
66 {
67 if ( m_invalidated.IsEmpty() )
68 return false;
69
70 rect = m_invalidated;
71 Clear(); // there's only one item in the queue
72 return true;
73 }
74
75 private:
76 // currently invalidated region
77 wxRect m_invalidated;
78 };
79
80 // ============================================================================
81 // wxTopLevelWindowDFB
82 // ============================================================================
83
84 // ----------------------------------------------------------------------------
85 // creation & destruction
86 // ----------------------------------------------------------------------------
87
88 void wxTopLevelWindowDFB::Init()
89 {
90 m_isShown = false;
91 m_isMaximized = false;
92 m_fsIsShowing = false;
93 m_sizeSet = false;
94 m_opacity = 255;
95 m_toPaint = new wxDfbQueuedPaintRequests;
96 m_isPainting = false;
97 }
98
99 bool wxTopLevelWindowDFB::Create(wxWindow *parent,
100 wxWindowID id,
101 const wxString& title,
102 const wxPoint& posOrig,
103 const wxSize& sizeOrig,
104 long style,
105 const wxString &name)
106 {
107 m_tlw = this;
108
109 // always create a frame of some reasonable, even if arbitrary, size (at
110 // least for MSW compatibility)
111 wxSize size(sizeOrig);
112 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord )
113 {
114 wxSize sizeDefault = GetDefaultSize();
115 if ( size.x == wxDefaultCoord )
116 size.x = sizeDefault.x;
117 if ( size.y == wxDefaultCoord )
118 size.y = sizeDefault.y;
119 }
120
121 wxPoint pos(posOrig);
122 if ( pos.x == wxDefaultCoord )
123 pos.x = 0;
124 if ( pos.y == wxDefaultCoord )
125 pos.y = 0;
126
127 // create DirectFB window:
128 wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer());
129 wxCHECK_MSG( layer, false, _T("no display layer") );
130
131 DFBWindowDescription desc;
132 desc.flags = (DFBWindowDescriptionFlags)
133 (DWDESC_CAPS |
134 DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY);
135 desc.caps = DWCAPS_DOUBLEBUFFER;
136 desc.posx = pos.x;
137 desc.posy = pos.y;
138 desc.width = size.x;
139 desc.height = size.y;
140 m_dfbwin = layer->CreateWindow(&desc);
141 if ( !m_dfbwin )
142 return false;
143
144 // add the new TLW to DFBWindowID->wxTLW map:
145 DFBWindowID winid;
146 if ( !m_dfbwin->GetID(&winid) )
147 return false;
148 gs_dfbWindowsMap[winid] = this;
149
150 // TLWs are created initially hidden:
151 if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) )
152 return false;
153
154 if ( !wxWindow::Create(NULL, id, pos, size, style, name) )
155 return false;
156
157 SetParent(parent);
158 if ( parent )
159 parent->AddChild(this);
160
161 wxTopLevelWindows.Append(this);
162 m_title = title;
163
164 if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) )
165 {
166 m_dfbwin->SetStackingClass(DWSC_UPPER);
167 }
168
169 // direct events in this window to the global event buffer:
170 m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
171
172 return true;
173 }
174
175 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
176 {
177 m_isBeingDeleted = true;
178
179 wxTopLevelWindows.DeleteObject(this);
180
181 if ( wxTheApp->GetTopWindow() == this )
182 wxTheApp->SetTopWindow(NULL);
183
184 if ( wxTopLevelWindows.empty() && wxTheApp->GetExitOnFrameDelete() )
185 {
186 wxTheApp->ExitMainLoop();
187 }
188
189 wxDELETE(m_toPaint);
190
191 if ( !m_dfbwin )
192 return;
193
194 // remove the TLW from DFBWindowID->wxTLW map:
195 DFBWindowID winid;
196 if ( m_dfbwin->GetID(&winid) )
197 gs_dfbWindowsMap.erase(winid);
198
199 m_dfbwin->Destroy();
200 m_dfbwin.Reset();
201 }
202
203 // ----------------------------------------------------------------------------
204 // window size & position
205 // ----------------------------------------------------------------------------
206
207 void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const
208 {
209 m_dfbwin->GetPosition(x, y);
210 }
211
212 void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const
213 {
214 m_dfbwin->GetSize(width, height);
215 }
216
217 void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height)
218 {
219 wxPoint curpos = GetPosition();
220 if ( curpos.x != x || curpos.y != y )
221 {
222 m_dfbwin->MoveTo(x, y);
223 }
224
225 wxSize cursize = GetSize();
226 if ( cursize.x != width || cursize.y != height )
227 {
228 // changing window's size changes its surface:
229 InvalidateDfbSurface();
230
231 m_dfbwin->Resize(width, height);
232
233 // we must repaint the window after it changed size:
234 if ( IsShown() )
235 DoRefreshWindow();
236 }
237 }
238
239 // ----------------------------------------------------------------------------
240 // showing and hiding
241 // ----------------------------------------------------------------------------
242
243 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
244 bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style)
245 {
246 if (show == m_fsIsShowing) return false; // return what?
247
248 m_fsIsShowing = show;
249
250 if (show)
251 {
252 m_fsSaveStyle = m_windowStyle;
253 m_fsSaveFlag = style;
254 GetPosition(&m_fsSaveFrame.x, &m_fsSaveFrame.y);
255 GetSize(&m_fsSaveFrame.width, &m_fsSaveFrame.height);
256
257 if ( style & wxFULLSCREEN_NOCAPTION )
258 m_windowStyle &= ~wxCAPTION;
259 if ( style & wxFULLSCREEN_NOBORDER )
260 m_windowStyle = wxSIMPLE_BORDER;
261
262 int x, y;
263 wxDisplaySize(&x, &y);
264 SetSize(0, 0, x, y);
265 }
266 else
267 {
268 m_windowStyle = m_fsSaveStyle;
269 SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y,
270 m_fsSaveFrame.width, m_fsSaveFrame.height);
271 }
272
273 return true;
274 }
275
276 bool wxTopLevelWindowDFB::Show(bool show)
277 {
278 if ( !wxTopLevelWindowBase::Show(show) )
279 return false;
280
281 // hide/show the window by setting its opacity to 0/full:
282 m_dfbwin->SetOpacity(show ? m_opacity : 0);
283
284 // If this is the first time Show was called, send size event,
285 // so that the frame can adjust itself (think auto layout or single child)
286 if ( !m_sizeSet )
287 {
288 m_sizeSet = true;
289 wxSizeEvent event(GetSize(), GetId());
290 event.SetEventObject(this);
291 GetEventHandler()->ProcessEvent(event);
292 }
293
294 if ( show )
295 {
296 wxWindow *focused = wxWindow::FindFocus();
297 if ( focused && focused->GetTLW() == this )
298 {
299 SetDfbFocus();
300 }
301 else if ( AcceptsFocus() )
302 {
303 // FIXME: we should probably always call SetDfbFocus instead
304 // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
305 // handler
306 SetFocus();
307 }
308 }
309
310 return true;
311 }
312
313 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha)
314 {
315 if ( IsShown() )
316 {
317 if ( !m_dfbwin->SetOpacity(alpha) )
318 return false;
319 }
320
321 m_opacity = alpha;
322 return true;
323 }
324
325 // ----------------------------------------------------------------------------
326 // maximize, minimize etc.
327 // ----------------------------------------------------------------------------
328
329 void wxTopLevelWindowDFB::Maximize(bool maximize)
330 {
331 int x, y, w, h;
332 wxClientDisplayRect(&x, &y, &w, &h);
333
334 if ( maximize && !m_isMaximized )
335 {
336 m_isMaximized = true;
337
338 GetPosition(&m_savedFrame.x, &m_savedFrame.y);
339 GetSize(&m_savedFrame.width, &m_savedFrame.height);
340
341 SetSize(x, y, w, h);
342 }
343 else if ( !maximize && m_isMaximized )
344 {
345 m_isMaximized = false;
346 SetSize(m_savedFrame.x, m_savedFrame.y,
347 m_savedFrame.width, m_savedFrame.height);
348 }
349 }
350
351 bool wxTopLevelWindowDFB::IsMaximized() const
352 {
353 return m_isMaximized;
354 }
355
356 void wxTopLevelWindowDFB::Restore()
357 {
358 if ( IsMaximized() )
359 {
360 Maximize(false);
361 }
362 }
363
364 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize))
365 {
366 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
367 }
368
369 bool wxTopLevelWindowDFB::IsIconized() const
370 {
371 return false;
372 }
373
374
375 // ----------------------------------------------------------------------------
376 // surfaces and painting
377 // ----------------------------------------------------------------------------
378
379 wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const
380 {
381 return m_dfbwin->GetSurface();
382 }
383
384 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
385 {
386 if ( m_toPaint->IsEmpty() )
387 return; // nothing to do
388
389 if ( IsFrozen() || !IsShown() )
390 {
391 // nothing to do if the window is frozen or hidden; clear the queue
392 // and return (note that it's OK to clear the queue even if the window
393 // is frozen, because Thaw() calls Refresh()):
394 m_toPaint->Clear();
395 return;
396 }
397
398 // process queued paint requests:
399 wxRect winRect(wxPoint(0, 0), GetSize());
400 wxRect paintedRect;
401
402 // important note: all DCs created from now until m_isPainting is reset to
403 // false will not update the front buffer as this flag indicates that we'll
404 // blit the entire back buffer to front soon
405 m_isPainting = true;
406
407 #ifdef __WXDEBUG__
408 int requestsCount = 0;
409 #endif
410
411 wxRect request;
412 while ( m_toPaint->GetNext(request) )
413 {
414 #ifdef __WXDEBUG__
415 requestsCount++;
416 #endif
417 wxRect clipped(request);
418 clipped.Intersect(winRect);
419 if ( clipped.IsEmpty() )
420 continue; // nothing to refresh
421
422 wxLogTrace(TRACE_PAINT,
423 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
424 this, GetName().c_str(),
425 clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom());
426
427 PaintWindow(clipped);
428
429 // remember rectangle covering all repainted areas:
430 if ( paintedRect.IsEmpty() )
431 paintedRect = clipped;
432 else
433 paintedRect.Union(clipped);
434 }
435
436 m_isPainting = false;
437
438 m_toPaint->Clear();
439
440 if ( paintedRect.IsEmpty() )
441 return; // no painting occurred, no need to flip
442
443 // Flip the surface to make the changes visible. Note that the rectangle we
444 // flip is *superset* of the union of repainted rectangles (created as
445 // "rectangles union" by wxRect::Union) and so some parts of the back
446 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
447 // be copied to the front buffer as well. This is safe/correct thing to do
448 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
449 // the back and front buffers contain the same data.
450 //
451 // Note that we do _not_ split m_toPaint into disjoint rectangles and
452 // do FlipToFront() for each of them, because that could result in visible
453 // updating of the screen; instead, we prefer to flip everything at once.
454
455 DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(),
456 paintedRect.GetRight(), paintedRect.GetBottom()};
457 DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r;
458
459 GetDfbSurface()->FlipToFront(rptr);
460
461 wxLogTrace(TRACE_PAINT,
462 _T("%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]"),
463 this, GetName().c_str(),
464 requestsCount,
465 paintedRect.x, paintedRect.y,
466 paintedRect.GetRight(), paintedRect.GetBottom());
467 }
468
469 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect)
470 {
471 // don't overlap outside of the window (NB: 'rect' is in window coords):
472 wxRect r(rect);
473 r.Intersect(wxRect(GetSize()));
474 if ( r.IsEmpty() )
475 return;
476
477 wxLogTrace(TRACE_PAINT,
478 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
479 this, GetName().c_str(),
480 rect.x, rect.y, rect.GetRight(), rect.GetBottom());
481
482 // defer painting until idle time or until Update() is called:
483 m_toPaint->Add(rect);
484 }
485
486 void wxTopLevelWindowDFB::Update()
487 {
488 HandleQueuedPaintRequests();
489 }
490
491 // ---------------------------------------------------------------------------
492 // events handling
493 // ---------------------------------------------------------------------------
494
495 void wxTopLevelWindowDFB::SetDfbFocus()
496 {
497 wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") );
498 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
499 _T("setting DirectFB focus to unexpected window") );
500
501 GetDirectFBWindow()->RequestFocus();
502 }
503
504 /* static */
505 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_)
506 {
507 const DFBWindowEvent& event = event_;
508
509 if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() )
510 {
511 wxLogTrace(TRACE_EVENTS,
512 _T("received event for unknown DirectFB window, ignoring"));
513 return;
514 }
515
516 wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id];
517 wxWindow *recipient = NULL;
518 void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL;
519
520 switch ( event.type )
521 {
522 case DWET_KEYDOWN:
523 case DWET_KEYUP:
524 {
525 recipient = wxWindow::FindFocus();
526 handlerFunc = &wxWindowDFB::HandleKeyEvent;
527 break;
528 }
529
530 case DWET_NONE:
531 case DWET_ALL:
532 {
533 wxFAIL_MSG( _T("invalid event type") );
534 break;
535 }
536
537 default:
538 // we're not interested in them here
539 break;
540 }
541
542 if ( !recipient )
543 {
544 wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window"));
545 return;
546 }
547
548 wxCHECK_RET( recipient && recipient->GetTLW() == tlw,
549 _T("event recipient not in TLW which received the event") );
550
551 // process the event:
552 (recipient->*handlerFunc)(event_);
553 }
554
555 // ---------------------------------------------------------------------------
556 // idle events processing
557 // ---------------------------------------------------------------------------
558
559 void wxTopLevelWindowDFB::OnInternalIdle()
560 {
561 wxTopLevelWindowBase::OnInternalIdle();
562 HandleQueuedPaintRequests();
563 }