1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/nonownedwnd.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2006 REA Elektronik GmbH
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/toplevel.h"
19 #include "wx/hashmap.h"
20 #include "wx/evtloop.h"
21 #include "wx/dfb/private.h"
23 #define TRACE_EVENTS "events"
24 #define TRACE_PAINT "paint"
26 // ============================================================================
28 // ============================================================================
30 // mapping of DirectFB windows to wxTLWs:
31 WX_DECLARE_HASH_MAP(DFBWindowID
, wxNonOwnedWindow
*,
32 wxIntegerHash
, wxIntegerEqual
,
34 static wxDfbWindowsMap gs_dfbWindowsMap
;
36 // ============================================================================
38 // ============================================================================
40 // Queue of paint requests
41 class wxDfbQueuedPaintRequests
44 ~wxDfbQueuedPaintRequests() { Clear(); }
46 // Adds paint request to the queue
47 void Add(const wxRect
& rect
)
49 // We use a simple implementation here for now: all refresh requests
50 // are merged together into single rectangle that is superset of
51 // all the requested rectangles. This wastes some blitting and painting
52 // time, but OTOH, EVT_PAINT handler is called only once per window.
53 m_invalidated
.Union(rect
);
56 // Is the queue empty?
57 bool IsEmpty() const { return m_invalidated
.IsEmpty(); }
60 void Clear() { m_invalidated
= wxRect(); }
62 // Gets the next request in the queue, returns true if there was one,
63 // false if the queue was empty
64 bool GetNext(wxRect
& rect
)
66 if ( m_invalidated
.IsEmpty() )
70 Clear(); // there's only one item in the queue
75 // currently invalidated region
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
84 // creation & destruction
85 // ----------------------------------------------------------------------------
87 void wxNonOwnedWindow::Init()
92 m_toPaint
= new wxDfbQueuedPaintRequests
;
96 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
101 const wxString
&name
)
103 wxCHECK_MSG( pos
.x
>= 0 && pos
.y
>= 0, false, "invalid position" );
104 wxCHECK_MSG( size
.x
> 0 && size
.y
> 0, false, "invalid size" );
108 // create DirectFB window:
109 wxIDirectFBDisplayLayerPtr
layer(wxIDirectFB::Get()->GetDisplayLayer());
110 wxCHECK_MSG( layer
, false, "no display layer" );
112 DFBWindowDescription desc
;
113 desc
.flags
= (DFBWindowDescriptionFlags
)
115 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
116 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
120 desc
.height
= size
.y
;
121 m_dfbwin
= layer
->CreateWindow(&desc
);
125 // add the new TLW to DFBWindowID->wxTLW map:
127 if ( !m_dfbwin
->GetID(&winid
) )
129 gs_dfbWindowsMap
[winid
] = this;
131 // TLWs are created initially hidden:
132 if ( !m_dfbwin
->SetOpacity(wxALPHA_TRANSPARENT
) )
135 if ( !wxWindow::Create(NULL
, id
, pos
, size
, style
, name
) )
140 parent
->AddChild(this);
142 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
144 m_dfbwin
->SetStackingClass(DWSC_UPPER
);
147 // direct events in this window to the global event buffer:
148 m_dfbwin
->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
153 wxNonOwnedWindow::~wxNonOwnedWindow()
157 // destroy all children before we destroy the underlying DirectFB window,
158 // so that if any of them does something with the TLW, it will still work:
161 // it's safe to delete the underlying DirectFB window now:
167 // remove the TLW from DFBWindowID->wxTLW map:
169 if ( m_dfbwin
->GetID(&winid
) )
170 gs_dfbWindowsMap
.erase(winid
);
176 // ----------------------------------------------------------------------------
177 // window size & position
178 // ----------------------------------------------------------------------------
180 void wxNonOwnedWindow::DoGetPosition(int *x
, int *y
) const
182 m_dfbwin
->GetPosition(x
, y
);
185 void wxNonOwnedWindow::DoGetSize(int *width
, int *height
) const
187 m_dfbwin
->GetSize(width
, height
);
190 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
192 wxPoint curpos
= GetPosition();
193 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
195 m_dfbwin
->MoveTo(x
, y
);
198 wxSize cursize
= GetSize();
199 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
201 // changing window's size changes its surface:
202 InvalidateDfbSurface();
204 m_dfbwin
->Resize(width
, height
);
206 // we must repaint the window after it changed size:
212 // ----------------------------------------------------------------------------
213 // showing and hiding
214 // ----------------------------------------------------------------------------
216 bool wxNonOwnedWindow::Show(bool show
)
218 // NB: this calls wxWindow::Show() and so ensures DoRefreshWindow() is
219 // called on the window -- we'll need that below
220 if ( !wxWindow::Show(show
) )
223 // If this is the first time Show was called, send size event,
224 // so that the frame can adjust itself (think auto layout or single child)
228 wxSizeEvent
event(GetSize(), GetId());
229 event
.SetEventObject(this);
230 HandleWindowEvent(event
);
233 // make sure the window is fully painted, with all pending updates, before
234 // DFB WM shows it, otherwise it would attempt to show either empty (=
235 // black) window surface (if shown for the first time) or it would show
236 // window with outdated content; note that the window was already refreshed
237 // in the wxWindow::Show() call above:
241 // hide/show the window by setting its opacity to 0/full:
242 m_dfbwin
->SetOpacity(show
? m_opacity
: 0);
246 wxWindow
*focused
= FindFocus();
247 if ( focused
&& focused
->GetTLW() == this )
249 // focus is on this frame or its children, apply it to DirectFB
252 // else: don't do anything, if this is wxFrame or wxDialog that should
253 // get focus when it's shown,
254 // wxTopLevelWindowDFB::HandleFocusEvent() will do it as soon as
255 // the event loop starts
261 void wxNonOwnedWindow::Raise()
263 m_dfbwin
->RaiseToTop();
266 void wxNonOwnedWindow::Lower()
268 m_dfbwin
->LowerToBottom();
271 // ----------------------------------------------------------------------------
272 // surfaces and painting
273 // ----------------------------------------------------------------------------
275 wxIDirectFBSurfacePtr
wxNonOwnedWindow::ObtainDfbSurface() const
277 return m_dfbwin
->GetSurface();
280 void wxNonOwnedWindow::HandleQueuedPaintRequests()
282 if ( m_toPaint
->IsEmpty() )
283 return; // nothing to do
285 if ( IsFrozen() || !IsShown() )
287 // nothing to do if the window is frozen or hidden; clear the queue
288 // and return (note that it's OK to clear the queue even if the window
289 // is frozen, because Thaw() calls Refresh()):
294 // process queued paint requests:
295 wxRect
winRect(wxPoint(0, 0), GetSize());
298 // important note: all DCs created from now until m_isPainting is reset to
299 // false will not update the front buffer as this flag indicates that we'll
300 // blit the entire back buffer to front soon
303 int requestsCount
= 0;
306 while ( m_toPaint
->GetNext(request
) )
309 wxRect
clipped(request
);
310 clipped
.Intersect(winRect
);
311 if ( clipped
.IsEmpty() )
312 continue; // nothing to refresh
314 wxLogTrace(TRACE_PAINT
,
315 "%p ('%s'): processing paint request [%i,%i,%i,%i]",
316 this, GetName().c_str(),
317 clipped
.x
, clipped
.y
, clipped
.GetRight(), clipped
.GetBottom());
319 PaintWindow(clipped
);
321 // remember rectangle covering all repainted areas:
322 if ( paintedRect
.IsEmpty() )
323 paintedRect
= clipped
;
325 paintedRect
.Union(clipped
);
328 m_isPainting
= false;
332 if ( paintedRect
.IsEmpty() )
333 return; // no painting occurred, no need to flip
335 // Flip the surface to make the changes visible. Note that the rectangle we
336 // flip is *superset* of the union of repainted rectangles (created as
337 // "rectangles union" by wxRect::Union) and so some parts of the back
338 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
339 // be copied to the front buffer as well. This is safe/correct thing to do
340 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
341 // the back and front buffers contain the same data.
343 // Note that we do _not_ split m_toPaint into disjoint rectangles and
344 // do FlipToFront() for each of them, because that could result in visible
345 // updating of the screen; instead, we prefer to flip everything at once.
347 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
348 paintedRect
.GetRight(), paintedRect
.GetBottom()};
349 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
351 GetDfbSurface()->FlipToFront(rptr
);
353 wxLogTrace(TRACE_PAINT
,
354 "%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]",
355 this, GetName().c_str(),
357 paintedRect
.x
, paintedRect
.y
,
358 paintedRect
.GetRight(), paintedRect
.GetBottom());
361 void wxNonOwnedWindow::DoRefreshRect(const wxRect
& rect
)
363 // don't overlap outside of the window (NB: 'rect' is in window coords):
365 r
.Intersect(wxRect(GetSize()));
369 wxLogTrace(TRACE_PAINT
,
370 "%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]",
371 this, GetName().c_str(),
372 rect
.x
, rect
.y
, rect
.GetRight(), rect
.GetBottom());
374 // defer painting until idle time or until Update() is called:
375 m_toPaint
->Add(rect
);
378 void wxNonOwnedWindow::Update()
380 HandleQueuedPaintRequests();
383 // ---------------------------------------------------------------------------
385 // ---------------------------------------------------------------------------
390 static wxNonOwnedWindow
*gs_insideDFBFocusHandlerOf
= NULL
;
392 struct InsideDFBFocusHandlerSetter
394 InsideDFBFocusHandlerSetter(wxNonOwnedWindow
*win
)
396 wxASSERT( gs_insideDFBFocusHandlerOf
== NULL
);
397 gs_insideDFBFocusHandlerOf
= win
;
399 ~InsideDFBFocusHandlerSetter()
401 gs_insideDFBFocusHandlerOf
= NULL
;
405 } // anonymous namespace
408 void wxNonOwnedWindow::SetDfbFocus()
410 wxCHECK_RET( IsShown(), "cannot set focus to hidden window" );
411 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
412 "setting DirectFB focus to unexpected window" );
414 // Don't set DirectFB focus if we're called from HandleFocusEvent() on
415 // this window, because we already have the focus in that case. Not only
416 // would it be unnecessary, it would be harmful: RequestFocus() adds
417 // an event to DirectFB event queue and calling it when in
418 // HandleFocusEvent() could result in a window being focused when it
419 // should not be. Consider this example:
421 // tlw1->SetFocus(); // (1)
422 // tlw2->SetFocus(); // (2)
424 // This results in adding these events to DFB queue:
426 // DWET_GOTFOCUS(tlw1)
427 // DWET_LOSTFOCUS(tlw1)
428 // DWET_GOTFOCUS(tlw2)
430 // Note that the events are processed by event loop, i.e. not between
431 // execution of lines (1) and (2) above. So by the time the first
432 // DWET_GOTFOCUS event is handled, tlw2->SetFocus() was already executed.
433 // If we onconditionally called RequestFocus() from here, handling the
434 // first event would result in this change to the event queue:
436 // DWET_LOSTFOCUS(tlw1)
437 // DWET_GOTFOCUS(tlw2) // (3)
438 // DWET_LOSTFOCUS(tlw2)
439 // DWET_GOTFOCUS(tlw1)
441 // And the focus would get back to tlw1 even though that's not what we
444 if ( gs_insideDFBFocusHandlerOf
== this )
447 GetDirectFBWindow()->RequestFocus();
451 void wxNonOwnedWindow::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
453 const DFBWindowEvent
& event
= event_
;
455 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
457 wxLogTrace(TRACE_EVENTS
,
458 "received event for unknown DirectFB window, ignoring");
462 wxNonOwnedWindow
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
464 switch ( event
.type
)
469 wxWindow
*recipient
= wxWindow::FindFocus();
472 wxLogTrace(TRACE_EVENTS
,
473 "ignoring event: no recipient window");
477 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
478 "event recipient not in TLW which received the event" );
480 recipient
->HandleKeyEvent(event_
);
487 InsideDFBFocusHandlerSetter
inside(tlw
);
488 tlw
->HandleFocusEvent(event_
);
494 wxFAIL_MSG( "invalid event type" );
498 // we're not interested in them here
503 // ---------------------------------------------------------------------------
504 // idle events processing
505 // ---------------------------------------------------------------------------
507 void wxNonOwnedWindow::OnInternalIdle()
509 wxWindow::OnInternalIdle();
510 HandleQueuedPaintRequests();