]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/nonownedwnd.cpp
4fe1ac999d6229480c06b994928c7f4a3094eba2
[wxWidgets.git] / src / dfb / nonownedwnd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/nonownedwnd.cpp
3 // Purpose: implementation of wxNonOwnedWindowow
4 // Author: Vaclav Slavik
5 // Created: 2006-12-24
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, wxNonOwnedWindow*,
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 // wxNonOwnedWindow
82 // ============================================================================
83
84 // ----------------------------------------------------------------------------
85 // creation & destruction
86 // ----------------------------------------------------------------------------
87
88 void wxNonOwnedWindow::Init()
89 {
90 m_isShown = false;
91 m_sizeSet = false;
92 m_opacity = 255;
93 m_toPaint = new wxDfbQueuedPaintRequests;
94 m_isPainting = false;
95 }
96
97 bool wxNonOwnedWindow::Create(wxWindow *parent,
98 wxWindowID id,
99 const wxPoint& pos,
100 const wxSize& size,
101 long style,
102 const wxString &name)
103 {
104 wxCHECK_MSG( pos.x >= 0 && pos.y >= 0, false, _T("invalid position") );
105 wxCHECK_MSG( size.x > 0 && size.y > 0, false, _T("invalid size") );
106
107 m_tlw = this;
108
109 // create DirectFB window:
110 wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer());
111 wxCHECK_MSG( layer, false, _T("no display layer") );
112
113 DFBWindowDescription desc;
114 desc.flags = (DFBWindowDescriptionFlags)
115 (DWDESC_CAPS |
116 DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY);
117 desc.caps = DWCAPS_DOUBLEBUFFER;
118 desc.posx = pos.x;
119 desc.posy = pos.y;
120 desc.width = size.x;
121 desc.height = size.y;
122 m_dfbwin = layer->CreateWindow(&desc);
123 if ( !m_dfbwin )
124 return false;
125
126 // add the new TLW to DFBWindowID->wxTLW map:
127 DFBWindowID winid;
128 if ( !m_dfbwin->GetID(&winid) )
129 return false;
130 gs_dfbWindowsMap[winid] = this;
131
132 // TLWs are created initially hidden:
133 if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) )
134 return false;
135
136 if ( !wxWindow::Create(NULL, id, pos, size, style, name) )
137 return false;
138
139 SetParent(parent);
140 if ( parent )
141 parent->AddChild(this);
142
143 if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) )
144 {
145 m_dfbwin->SetStackingClass(DWSC_UPPER);
146 }
147
148 // direct events in this window to the global event buffer:
149 m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
150
151 return true;
152 }
153
154 wxNonOwnedWindow::~wxNonOwnedWindow()
155 {
156 m_isBeingDeleted = true;
157
158 // destroy all children before we destroy the underlying DirectFB window,
159 // so that if any of them does something with the TLW, it will still work:
160 DestroyChildren();
161
162 // it's safe to delete the underlying DirectFB window now:
163 wxDELETE(m_toPaint);
164
165 if ( !m_dfbwin )
166 return;
167
168 // remove the TLW from DFBWindowID->wxTLW map:
169 DFBWindowID winid;
170 if ( m_dfbwin->GetID(&winid) )
171 gs_dfbWindowsMap.erase(winid);
172
173 m_dfbwin->Destroy();
174 m_dfbwin.Reset();
175 }
176
177 // ----------------------------------------------------------------------------
178 // window size & position
179 // ----------------------------------------------------------------------------
180
181 void wxNonOwnedWindow::DoGetPosition(int *x, int *y) const
182 {
183 m_dfbwin->GetPosition(x, y);
184 }
185
186 void wxNonOwnedWindow::DoGetSize(int *width, int *height) const
187 {
188 m_dfbwin->GetSize(width, height);
189 }
190
191 void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
192 {
193 wxPoint curpos = GetPosition();
194 if ( curpos.x != x || curpos.y != y )
195 {
196 m_dfbwin->MoveTo(x, y);
197 }
198
199 wxSize cursize = GetSize();
200 if ( cursize.x != width || cursize.y != height )
201 {
202 // changing window's size changes its surface:
203 InvalidateDfbSurface();
204
205 m_dfbwin->Resize(width, height);
206
207 // we must repaint the window after it changed size:
208 if ( IsShown() )
209 DoRefreshWindow();
210 }
211 }
212
213 // ----------------------------------------------------------------------------
214 // showing and hiding
215 // ----------------------------------------------------------------------------
216
217 bool wxNonOwnedWindow::Show(bool show)
218 {
219 // NB: this calls wxWindow::Show() and so ensures DoRefreshWindow() is
220 // called on the window -- we'll need that below
221 if ( !wxWindow::Show(show) )
222 return false;
223
224 // If this is the first time Show was called, send size event,
225 // so that the frame can adjust itself (think auto layout or single child)
226 if ( !m_sizeSet )
227 {
228 m_sizeSet = true;
229 wxSizeEvent event(GetSize(), GetId());
230 event.SetEventObject(this);
231 GetEventHandler()->ProcessEvent(event);
232 }
233
234 // make sure the window is fully painted, with all pending updates, before
235 // DFB WM shows it, otherwise it would attempt to show either empty (=
236 // black) window surface (if shown for the first time) or it would show
237 // window with outdated content; note that the window was already refreshed
238 // in the wxWindow::Show() call above:
239 if ( show )
240 Update();
241
242 // hide/show the window by setting its opacity to 0/full:
243 m_dfbwin->SetOpacity(show ? m_opacity : 0);
244
245 if ( show )
246 {
247 wxWindow *focused = wxWindow::FindFocus();
248 if ( focused && focused->GetTLW() == this )
249 {
250 SetDfbFocus();
251 }
252 else if ( AcceptsFocus() )
253 {
254 // FIXME: we should probably always call SetDfbFocus instead
255 // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
256 // handler
257 SetFocus();
258 }
259 }
260
261 return true;
262 }
263
264 // ----------------------------------------------------------------------------
265 // surfaces and painting
266 // ----------------------------------------------------------------------------
267
268 wxIDirectFBSurfacePtr wxNonOwnedWindow::ObtainDfbSurface() const
269 {
270 return m_dfbwin->GetSurface();
271 }
272
273 void wxNonOwnedWindow::HandleQueuedPaintRequests()
274 {
275 if ( m_toPaint->IsEmpty() )
276 return; // nothing to do
277
278 if ( IsFrozen() || !IsShown() )
279 {
280 // nothing to do if the window is frozen or hidden; clear the queue
281 // and return (note that it's OK to clear the queue even if the window
282 // is frozen, because Thaw() calls Refresh()):
283 m_toPaint->Clear();
284 return;
285 }
286
287 // process queued paint requests:
288 wxRect winRect(wxPoint(0, 0), GetSize());
289 wxRect paintedRect;
290
291 // important note: all DCs created from now until m_isPainting is reset to
292 // false will not update the front buffer as this flag indicates that we'll
293 // blit the entire back buffer to front soon
294 m_isPainting = true;
295
296 #ifdef __WXDEBUG__
297 int requestsCount = 0;
298 #endif
299
300 wxRect request;
301 while ( m_toPaint->GetNext(request) )
302 {
303 #ifdef __WXDEBUG__
304 requestsCount++;
305 #endif
306 wxRect clipped(request);
307 clipped.Intersect(winRect);
308 if ( clipped.IsEmpty() )
309 continue; // nothing to refresh
310
311 wxLogTrace(TRACE_PAINT,
312 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
313 this, GetName().c_str(),
314 clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom());
315
316 PaintWindow(clipped);
317
318 // remember rectangle covering all repainted areas:
319 if ( paintedRect.IsEmpty() )
320 paintedRect = clipped;
321 else
322 paintedRect.Union(clipped);
323 }
324
325 m_isPainting = false;
326
327 m_toPaint->Clear();
328
329 if ( paintedRect.IsEmpty() )
330 return; // no painting occurred, no need to flip
331
332 // Flip the surface to make the changes visible. Note that the rectangle we
333 // flip is *superset* of the union of repainted rectangles (created as
334 // "rectangles union" by wxRect::Union) and so some parts of the back
335 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
336 // be copied to the front buffer as well. This is safe/correct thing to do
337 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
338 // the back and front buffers contain the same data.
339 //
340 // Note that we do _not_ split m_toPaint into disjoint rectangles and
341 // do FlipToFront() for each of them, because that could result in visible
342 // updating of the screen; instead, we prefer to flip everything at once.
343
344 DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(),
345 paintedRect.GetRight(), paintedRect.GetBottom()};
346 DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r;
347
348 GetDfbSurface()->FlipToFront(rptr);
349
350 wxLogTrace(TRACE_PAINT,
351 _T("%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]"),
352 this, GetName().c_str(),
353 requestsCount,
354 paintedRect.x, paintedRect.y,
355 paintedRect.GetRight(), paintedRect.GetBottom());
356 }
357
358 void wxNonOwnedWindow::DoRefreshRect(const wxRect& rect)
359 {
360 // don't overlap outside of the window (NB: 'rect' is in window coords):
361 wxRect r(rect);
362 r.Intersect(wxRect(GetSize()));
363 if ( r.IsEmpty() )
364 return;
365
366 wxLogTrace(TRACE_PAINT,
367 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
368 this, GetName().c_str(),
369 rect.x, rect.y, rect.GetRight(), rect.GetBottom());
370
371 // defer painting until idle time or until Update() is called:
372 m_toPaint->Add(rect);
373 }
374
375 void wxNonOwnedWindow::Update()
376 {
377 HandleQueuedPaintRequests();
378 }
379
380 // ---------------------------------------------------------------------------
381 // events handling
382 // ---------------------------------------------------------------------------
383
384 void wxNonOwnedWindow::SetDfbFocus()
385 {
386 wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") );
387 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
388 _T("setting DirectFB focus to unexpected window") );
389
390 GetDirectFBWindow()->RequestFocus();
391 }
392
393 /* static */
394 void wxNonOwnedWindow::HandleDFBWindowEvent(const wxDFBWindowEvent& event_)
395 {
396 const DFBWindowEvent& event = event_;
397
398 if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() )
399 {
400 wxLogTrace(TRACE_EVENTS,
401 _T("received event for unknown DirectFB window, ignoring"));
402 return;
403 }
404
405 wxNonOwnedWindow *tlw = gs_dfbWindowsMap[event.window_id];
406 wxWindow *recipient = NULL;
407 void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL;
408
409 switch ( event.type )
410 {
411 case DWET_KEYDOWN:
412 case DWET_KEYUP:
413 {
414 recipient = wxWindow::FindFocus();
415 handlerFunc = &wxWindowDFB::HandleKeyEvent;
416 break;
417 }
418
419 case DWET_NONE:
420 case DWET_ALL:
421 {
422 wxFAIL_MSG( _T("invalid event type") );
423 break;
424 }
425
426 default:
427 // we're not interested in them here
428 break;
429 }
430
431 if ( !recipient )
432 {
433 wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window"));
434 return;
435 }
436
437 wxCHECK_RET( recipient && recipient->GetTLW() == tlw,
438 _T("event recipient not in TLW which received the event") );
439
440 // process the event:
441 (recipient->*handlerFunc)(event_);
442 }
443
444 // ---------------------------------------------------------------------------
445 // idle events processing
446 // ---------------------------------------------------------------------------
447
448 void wxNonOwnedWindow::OnInternalIdle()
449 {
450 wxWindow::OnInternalIdle();
451 HandleQueuedPaintRequests();
452 }