1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/nonownedwnd.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/toplevel.h"
20 #include "wx/hashmap.h"
21 #include "wx/evtloop.h"
22 #include "wx/dfb/private.h"
24 #define TRACE_EVENTS "events"
25 #define TRACE_PAINT "paint"
27 // ============================================================================
29 // ============================================================================
31 // mapping of DirectFB windows to wxTLWs:
32 WX_DECLARE_HASH_MAP(DFBWindowID
, wxNonOwnedWindow
*,
33 wxIntegerHash
, wxIntegerEqual
,
35 static wxDfbWindowsMap gs_dfbWindowsMap
;
37 // ============================================================================
39 // ============================================================================
41 // Queue of paint requests
42 class wxDfbQueuedPaintRequests
45 ~wxDfbQueuedPaintRequests() { Clear(); }
47 // Adds paint request to the queue
48 void Add(const wxRect
& rect
)
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
);
57 // Is the queue empty?
58 bool IsEmpty() const { return m_invalidated
.IsEmpty(); }
61 void Clear() { m_invalidated
= wxRect(); }
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
)
67 if ( m_invalidated
.IsEmpty() )
71 Clear(); // there's only one item in the queue
76 // currently invalidated region
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // creation & destruction
86 // ----------------------------------------------------------------------------
88 void wxNonOwnedWindow::Init()
93 m_toPaint
= new wxDfbQueuedPaintRequests
;
97 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
102 const wxString
&name
)
104 wxCHECK_MSG( pos
.x
>= 0 && pos
.y
>= 0, false, "invalid position" );
105 wxCHECK_MSG( size
.x
> 0 && size
.y
> 0, false, "invalid size" );
109 // create DirectFB window:
110 wxIDirectFBDisplayLayerPtr
layer(wxIDirectFB::Get()->GetDisplayLayer());
111 wxCHECK_MSG( layer
, false, "no display layer" );
113 DFBWindowDescription desc
;
114 desc
.flags
= (DFBWindowDescriptionFlags
)
116 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
117 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
121 desc
.height
= size
.y
;
122 m_dfbwin
= layer
->CreateWindow(&desc
);
126 // add the new TLW to DFBWindowID->wxTLW map:
128 if ( !m_dfbwin
->GetID(&winid
) )
130 gs_dfbWindowsMap
[winid
] = this;
132 // TLWs are created initially hidden:
133 if ( !m_dfbwin
->SetOpacity(wxALPHA_TRANSPARENT
) )
136 if ( !wxWindow::Create(NULL
, id
, pos
, size
, style
, name
) )
141 parent
->AddChild(this);
143 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
145 m_dfbwin
->SetStackingClass(DWSC_UPPER
);
148 // direct events in this window to the global event buffer:
149 m_dfbwin
->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
154 wxNonOwnedWindow::~wxNonOwnedWindow()
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:
162 // it's safe to delete the underlying DirectFB window now:
168 // remove the TLW from DFBWindowID->wxTLW map:
170 if ( m_dfbwin
->GetID(&winid
) )
171 gs_dfbWindowsMap
.erase(winid
);
177 // ----------------------------------------------------------------------------
178 // window size & position
179 // ----------------------------------------------------------------------------
181 void wxNonOwnedWindow::DoGetPosition(int *x
, int *y
) const
183 m_dfbwin
->GetPosition(x
, y
);
186 void wxNonOwnedWindow::DoGetSize(int *width
, int *height
) const
188 m_dfbwin
->GetSize(width
, height
);
191 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
193 wxPoint curpos
= GetPosition();
194 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
196 m_dfbwin
->MoveTo(x
, y
);
199 wxSize cursize
= GetSize();
200 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
202 // changing window's size changes its surface:
203 InvalidateDfbSurface();
205 m_dfbwin
->Resize(width
, height
);
207 // we must repaint the window after it changed size:
213 // ----------------------------------------------------------------------------
214 // showing and hiding
215 // ----------------------------------------------------------------------------
217 bool wxNonOwnedWindow::Show(bool show
)
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
) )
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)
229 wxSizeEvent
event(GetSize(), GetId());
230 event
.SetEventObject(this);
231 HandleWindowEvent(event
);
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:
242 // hide/show the window by setting its opacity to 0/full:
243 m_dfbwin
->SetOpacity(show
? m_opacity
: 0);
247 wxWindow
*focused
= FindFocus();
248 if ( focused
&& focused
->GetTLW() == this )
250 // focus is on this frame or its children, apply it to DirectFB
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
262 void wxNonOwnedWindow::Raise()
264 m_dfbwin
->RaiseToTop();
267 void wxNonOwnedWindow::Lower()
269 m_dfbwin
->LowerToBottom();
272 // ----------------------------------------------------------------------------
273 // surfaces and painting
274 // ----------------------------------------------------------------------------
276 wxIDirectFBSurfacePtr
wxNonOwnedWindow::ObtainDfbSurface() const
278 return m_dfbwin
->GetSurface();
281 void wxNonOwnedWindow::HandleQueuedPaintRequests()
283 if ( m_toPaint
->IsEmpty() )
284 return; // nothing to do
286 if ( IsFrozen() || !IsShown() )
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()):
295 // process queued paint requests:
296 wxRect
winRect(wxPoint(0, 0), GetSize());
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
304 int requestsCount
= 0;
307 while ( m_toPaint
->GetNext(request
) )
310 wxRect
clipped(request
);
311 clipped
.Intersect(winRect
);
312 if ( clipped
.IsEmpty() )
313 continue; // nothing to refresh
315 wxLogTrace(TRACE_PAINT
,
316 "%p ('%s'): processing paint request [%i,%i,%i,%i]",
317 this, GetName().c_str(),
318 clipped
.x
, clipped
.y
, clipped
.GetRight(), clipped
.GetBottom());
320 PaintWindow(clipped
);
322 // remember rectangle covering all repainted areas:
323 if ( paintedRect
.IsEmpty() )
324 paintedRect
= clipped
;
326 paintedRect
.Union(clipped
);
329 m_isPainting
= false;
333 if ( paintedRect
.IsEmpty() )
334 return; // no painting occurred, no need to flip
336 // Flip the surface to make the changes visible. Note that the rectangle we
337 // flip is *superset* of the union of repainted rectangles (created as
338 // "rectangles union" by wxRect::Union) and so some parts of the back
339 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
340 // be copied to the front buffer as well. This is safe/correct thing to do
341 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
342 // the back and front buffers contain the same data.
344 // Note that we do _not_ split m_toPaint into disjoint rectangles and
345 // do FlipToFront() for each of them, because that could result in visible
346 // updating of the screen; instead, we prefer to flip everything at once.
348 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
349 paintedRect
.GetRight(), paintedRect
.GetBottom()};
350 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
352 GetDfbSurface()->FlipToFront(rptr
);
354 wxLogTrace(TRACE_PAINT
,
355 "%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]",
356 this, GetName().c_str(),
358 paintedRect
.x
, paintedRect
.y
,
359 paintedRect
.GetRight(), paintedRect
.GetBottom());
362 void wxNonOwnedWindow::DoRefreshRect(const wxRect
& rect
)
364 // don't overlap outside of the window (NB: 'rect' is in window coords):
366 r
.Intersect(wxRect(GetSize()));
370 wxLogTrace(TRACE_PAINT
,
371 "%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]",
372 this, GetName().c_str(),
373 rect
.x
, rect
.y
, rect
.GetRight(), rect
.GetBottom());
375 // defer painting until idle time or until Update() is called:
376 m_toPaint
->Add(rect
);
379 void wxNonOwnedWindow::Update()
381 HandleQueuedPaintRequests();
384 // ---------------------------------------------------------------------------
386 // ---------------------------------------------------------------------------
391 static wxNonOwnedWindow
*gs_insideDFBFocusHandlerOf
= NULL
;
393 struct InsideDFBFocusHandlerSetter
395 InsideDFBFocusHandlerSetter(wxNonOwnedWindow
*win
)
397 wxASSERT( gs_insideDFBFocusHandlerOf
== NULL
);
398 gs_insideDFBFocusHandlerOf
= win
;
400 ~InsideDFBFocusHandlerSetter()
402 gs_insideDFBFocusHandlerOf
= NULL
;
406 } // anonymous namespace
409 void wxNonOwnedWindow::SetDfbFocus()
411 wxCHECK_RET( IsShown(), "cannot set focus to hidden window" );
412 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
413 "setting DirectFB focus to unexpected window" );
415 // Don't set DirectFB focus if we're called from HandleFocusEvent() on
416 // this window, because we already have the focus in that case. Not only
417 // would it be unnecessary, it would be harmful: RequestFocus() adds
418 // an event to DirectFB event queue and calling it when in
419 // HandleFocusEvent() could result in a window being focused when it
420 // should not be. Consider this example:
422 // tlw1->SetFocus(); // (1)
423 // tlw2->SetFocus(); // (2)
425 // This results in adding these events to DFB queue:
427 // DWET_GOTFOCUS(tlw1)
428 // DWET_LOSTFOCUS(tlw1)
429 // DWET_GOTFOCUS(tlw2)
431 // Note that the events are processed by event loop, i.e. not between
432 // execution of lines (1) and (2) above. So by the time the first
433 // DWET_GOTFOCUS event is handled, tlw2->SetFocus() was already executed.
434 // If we onconditionally called RequestFocus() from here, handling the
435 // first event would result in this change to the event queue:
437 // DWET_LOSTFOCUS(tlw1)
438 // DWET_GOTFOCUS(tlw2) // (3)
439 // DWET_LOSTFOCUS(tlw2)
440 // DWET_GOTFOCUS(tlw1)
442 // And the focus would get back to tlw1 even though that's not what we
445 if ( gs_insideDFBFocusHandlerOf
== this )
448 GetDirectFBWindow()->RequestFocus();
452 void wxNonOwnedWindow::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
454 const DFBWindowEvent
& event
= event_
;
456 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
458 wxLogTrace(TRACE_EVENTS
,
459 "received event for unknown DirectFB window, ignoring");
463 wxNonOwnedWindow
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
465 switch ( event
.type
)
470 wxWindow
*recipient
= wxWindow::FindFocus();
473 wxLogTrace(TRACE_EVENTS
,
474 "ignoring event: no recipient window");
478 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
479 "event recipient not in TLW which received the event" );
481 recipient
->HandleKeyEvent(event_
);
488 InsideDFBFocusHandlerSetter
inside(tlw
);
489 tlw
->HandleFocusEvent(event_
);
495 wxFAIL_MSG( "invalid event type" );
499 // we're not interested in them here
504 // ---------------------------------------------------------------------------
505 // idle events processing
506 // ---------------------------------------------------------------------------
508 void wxNonOwnedWindow::OnInternalIdle()
510 wxWindow::OnInternalIdle();
511 HandleQueuedPaintRequests();