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