]>
Commit | Line | Data |
---|---|---|
42b0d8b9 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/dfb/nonownedwnd.cpp | |
fd2be5df | 3 | // Purpose: implementation of wxNonOwnedWindow |
42b0d8b9 VS |
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 | ||
a5001e93 VS |
24 | #define TRACE_EVENTS "events" |
25 | #define TRACE_PAINT "paint" | |
42b0d8b9 VS |
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 | { | |
a5001e93 VS |
104 | wxCHECK_MSG( pos.x >= 0 && pos.y >= 0, false, "invalid position" ); |
105 | wxCHECK_MSG( size.x > 0 && size.y > 0, false, "invalid size" ); | |
42b0d8b9 VS |
106 | |
107 | m_tlw = this; | |
108 | ||
109 | // create DirectFB window: | |
110 | wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer()); | |
a5001e93 | 111 | wxCHECK_MSG( layer, false, "no display layer" ); |
42b0d8b9 VS |
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); | |
937013e0 | 231 | HandleWindowEvent(event); |
42b0d8b9 VS |
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 ) | |
fd2be5df VS |
246 | { |
247 | wxWindow *focused = FindFocus(); | |
248 | if ( focused && focused->GetTLW() == this ) | |
249 | { | |
250 | // focus is on this frame or its children, apply it to DirectFB | |
251 | SetDfbFocus(); | |
252 | } | |
253 | // else: don't do anything, if this is wxFrame or wxDialog that should | |
254 | // get focus when it's shown, | |
255 | // wxTopLevelWindowDFB::HandleFocusEvent() will do it as soon as | |
256 | // the event loop starts | |
257 | } | |
42b0d8b9 VS |
258 | |
259 | return true; | |
260 | } | |
261 | ||
84e45580 VS |
262 | void wxNonOwnedWindow::Raise() |
263 | { | |
264 | m_dfbwin->RaiseToTop(); | |
265 | } | |
266 | ||
267 | void wxNonOwnedWindow::Lower() | |
268 | { | |
269 | m_dfbwin->LowerToBottom(); | |
270 | } | |
271 | ||
42b0d8b9 VS |
272 | // ---------------------------------------------------------------------------- |
273 | // surfaces and painting | |
274 | // ---------------------------------------------------------------------------- | |
275 | ||
276 | wxIDirectFBSurfacePtr wxNonOwnedWindow::ObtainDfbSurface() const | |
277 | { | |
278 | return m_dfbwin->GetSurface(); | |
279 | } | |
280 | ||
281 | void wxNonOwnedWindow::HandleQueuedPaintRequests() | |
282 | { | |
283 | if ( m_toPaint->IsEmpty() ) | |
284 | return; // nothing to do | |
285 | ||
286 | if ( IsFrozen() || !IsShown() ) | |
287 | { | |
288 | // nothing to do if the window is frozen or hidden; clear the queue | |
289 | // and return (note that it's OK to clear the queue even if the window | |
290 | // is frozen, because Thaw() calls Refresh()): | |
291 | m_toPaint->Clear(); | |
292 | return; | |
293 | } | |
294 | ||
295 | // process queued paint requests: | |
296 | wxRect winRect(wxPoint(0, 0), GetSize()); | |
297 | wxRect paintedRect; | |
298 | ||
299 | // important note: all DCs created from now until m_isPainting is reset to | |
300 | // false will not update the front buffer as this flag indicates that we'll | |
301 | // blit the entire back buffer to front soon | |
302 | m_isPainting = true; | |
303 | ||
304 | #ifdef __WXDEBUG__ | |
305 | int requestsCount = 0; | |
306 | #endif | |
307 | ||
308 | wxRect request; | |
309 | while ( m_toPaint->GetNext(request) ) | |
310 | { | |
311 | #ifdef __WXDEBUG__ | |
312 | requestsCount++; | |
313 | #endif | |
314 | wxRect clipped(request); | |
315 | clipped.Intersect(winRect); | |
316 | if ( clipped.IsEmpty() ) | |
317 | continue; // nothing to refresh | |
318 | ||
319 | wxLogTrace(TRACE_PAINT, | |
a5001e93 | 320 | "%p ('%s'): processing paint request [%i,%i,%i,%i]", |
42b0d8b9 VS |
321 | this, GetName().c_str(), |
322 | clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom()); | |
323 | ||
324 | PaintWindow(clipped); | |
325 | ||
326 | // remember rectangle covering all repainted areas: | |
327 | if ( paintedRect.IsEmpty() ) | |
328 | paintedRect = clipped; | |
329 | else | |
330 | paintedRect.Union(clipped); | |
331 | } | |
332 | ||
333 | m_isPainting = false; | |
334 | ||
335 | m_toPaint->Clear(); | |
336 | ||
337 | if ( paintedRect.IsEmpty() ) | |
338 | return; // no painting occurred, no need to flip | |
339 | ||
340 | // Flip the surface to make the changes visible. Note that the rectangle we | |
341 | // flip is *superset* of the union of repainted rectangles (created as | |
342 | // "rectangles union" by wxRect::Union) and so some parts of the back | |
343 | // buffer that we didn't touch in this HandleQueuedPaintRequests call will | |
344 | // be copied to the front buffer as well. This is safe/correct thing to do | |
345 | // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so | |
346 | // the back and front buffers contain the same data. | |
347 | // | |
348 | // Note that we do _not_ split m_toPaint into disjoint rectangles and | |
349 | // do FlipToFront() for each of them, because that could result in visible | |
350 | // updating of the screen; instead, we prefer to flip everything at once. | |
351 | ||
352 | DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(), | |
353 | paintedRect.GetRight(), paintedRect.GetBottom()}; | |
354 | DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r; | |
355 | ||
356 | GetDfbSurface()->FlipToFront(rptr); | |
357 | ||
358 | wxLogTrace(TRACE_PAINT, | |
a5001e93 | 359 | "%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]", |
42b0d8b9 VS |
360 | this, GetName().c_str(), |
361 | requestsCount, | |
362 | paintedRect.x, paintedRect.y, | |
363 | paintedRect.GetRight(), paintedRect.GetBottom()); | |
364 | } | |
365 | ||
366 | void wxNonOwnedWindow::DoRefreshRect(const wxRect& rect) | |
367 | { | |
368 | // don't overlap outside of the window (NB: 'rect' is in window coords): | |
369 | wxRect r(rect); | |
370 | r.Intersect(wxRect(GetSize())); | |
371 | if ( r.IsEmpty() ) | |
372 | return; | |
373 | ||
374 | wxLogTrace(TRACE_PAINT, | |
a5001e93 | 375 | "%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]", |
42b0d8b9 VS |
376 | this, GetName().c_str(), |
377 | rect.x, rect.y, rect.GetRight(), rect.GetBottom()); | |
378 | ||
379 | // defer painting until idle time or until Update() is called: | |
380 | m_toPaint->Add(rect); | |
381 | } | |
382 | ||
383 | void wxNonOwnedWindow::Update() | |
384 | { | |
385 | HandleQueuedPaintRequests(); | |
386 | } | |
387 | ||
388 | // --------------------------------------------------------------------------- | |
389 | // events handling | |
390 | // --------------------------------------------------------------------------- | |
391 | ||
fd2be5df VS |
392 | namespace |
393 | { | |
394 | ||
395 | static wxNonOwnedWindow *gs_insideDFBFocusHandlerOf = NULL; | |
396 | ||
397 | struct InsideDFBFocusHandlerSetter | |
398 | { | |
399 | InsideDFBFocusHandlerSetter(wxNonOwnedWindow *win) | |
400 | { | |
401 | wxASSERT( gs_insideDFBFocusHandlerOf == NULL ); | |
402 | gs_insideDFBFocusHandlerOf = win; | |
403 | } | |
404 | ~InsideDFBFocusHandlerSetter() | |
405 | { | |
406 | gs_insideDFBFocusHandlerOf = NULL; | |
407 | } | |
408 | }; | |
409 | ||
410 | } // anonymous namespace | |
411 | ||
412 | ||
42b0d8b9 VS |
413 | void wxNonOwnedWindow::SetDfbFocus() |
414 | { | |
a5001e93 | 415 | wxCHECK_RET( IsShown(), "cannot set focus to hidden window" ); |
42b0d8b9 | 416 | wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this, |
a5001e93 | 417 | "setting DirectFB focus to unexpected window" ); |
42b0d8b9 | 418 | |
fd2be5df VS |
419 | // Don't set DirectFB focus if we're called from HandleFocusEvent() on |
420 | // this window, because we already have the focus in that case. Not only | |
421 | // would it be unnecessary, it would be harmful: RequestFocus() adds | |
422 | // an event to DirectFB event queue and calling it when in | |
423 | // HandleFocusEvent() could result in a window being focused when it | |
424 | // should not be. Consider this example: | |
425 | // | |
426 | // tlw1->SetFocus(); // (1) | |
427 | // tlw2->SetFocus(); // (2) | |
428 | // | |
429 | // This results in adding these events to DFB queue: | |
430 | // | |
431 | // DWET_GOTFOCUS(tlw1) | |
432 | // DWET_LOSTFOCUS(tlw1) | |
433 | // DWET_GOTFOCUS(tlw2) | |
434 | // | |
435 | // Note that the events are processed by event loop, i.e. not between | |
436 | // execution of lines (1) and (2) above. So by the time the first | |
437 | // DWET_GOTFOCUS event is handled, tlw2->SetFocus() was already executed. | |
438 | // If we onconditionally called RequestFocus() from here, handling the | |
439 | // first event would result in this change to the event queue: | |
440 | // | |
441 | // DWET_LOSTFOCUS(tlw1) | |
442 | // DWET_GOTFOCUS(tlw2) // (3) | |
443 | // DWET_LOSTFOCUS(tlw2) | |
444 | // DWET_GOTFOCUS(tlw1) | |
445 | // | |
446 | // And the focus would get back to tlw1 even though that's not what we | |
447 | // wanted. | |
448 | ||
449 | if ( gs_insideDFBFocusHandlerOf == this ) | |
450 | return; | |
451 | ||
42b0d8b9 VS |
452 | GetDirectFBWindow()->RequestFocus(); |
453 | } | |
454 | ||
455 | /* static */ | |
456 | void wxNonOwnedWindow::HandleDFBWindowEvent(const wxDFBWindowEvent& event_) | |
457 | { | |
458 | const DFBWindowEvent& event = event_; | |
459 | ||
460 | if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() ) | |
461 | { | |
462 | wxLogTrace(TRACE_EVENTS, | |
a5001e93 | 463 | "received event for unknown DirectFB window, ignoring"); |
42b0d8b9 VS |
464 | return; |
465 | } | |
466 | ||
467 | wxNonOwnedWindow *tlw = gs_dfbWindowsMap[event.window_id]; | |
42b0d8b9 VS |
468 | |
469 | switch ( event.type ) | |
470 | { | |
471 | case DWET_KEYDOWN: | |
472 | case DWET_KEYUP: | |
473 | { | |
6954a1e2 VS |
474 | wxWindow *recipient = wxWindow::FindFocus(); |
475 | if ( !recipient ) | |
476 | { | |
477 | wxLogTrace(TRACE_EVENTS, | |
a5001e93 | 478 | "ignoring event: no recipient window"); |
6954a1e2 VS |
479 | return; |
480 | } | |
481 | ||
482 | wxCHECK_RET( recipient && recipient->GetTLW() == tlw, | |
a5001e93 | 483 | "event recipient not in TLW which received the event" ); |
6954a1e2 VS |
484 | |
485 | recipient->HandleKeyEvent(event_); | |
42b0d8b9 VS |
486 | break; |
487 | } | |
488 | ||
6954a1e2 VS |
489 | case DWET_GOTFOCUS: |
490 | case DWET_LOSTFOCUS: | |
fd2be5df VS |
491 | { |
492 | InsideDFBFocusHandlerSetter inside(tlw); | |
493 | tlw->HandleFocusEvent(event_); | |
494 | } | |
6954a1e2 VS |
495 | break; |
496 | ||
42b0d8b9 VS |
497 | case DWET_NONE: |
498 | case DWET_ALL: | |
a5001e93 | 499 | wxFAIL_MSG( "invalid event type" ); |
42b0d8b9 | 500 | break; |
42b0d8b9 VS |
501 | |
502 | default: | |
503 | // we're not interested in them here | |
504 | break; | |
505 | } | |
42b0d8b9 VS |
506 | } |
507 | ||
508 | // --------------------------------------------------------------------------- | |
509 | // idle events processing | |
510 | // --------------------------------------------------------------------------- | |
511 | ||
512 | void wxNonOwnedWindow::OnInternalIdle() | |
513 | { | |
514 | wxWindow::OnInternalIdle(); | |
515 | HandleQueuedPaintRequests(); | |
516 | } |