1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/toplevel.cpp
3 // Purpose: Top level window, abstraction of wxFrame and wxDialog
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 _T("events")
25 #define TRACE_PAINT _T("paint")
27 // ============================================================================
29 // ============================================================================
31 // mapping of DirectFB windows to wxTLWs:
32 WX_DECLARE_HASH_MAP(DFBWindowID
, wxTopLevelWindowDFB
*,
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 // ============================================================================
81 // wxTopLevelWindowDFB
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // creation & destruction
86 // ----------------------------------------------------------------------------
88 void wxTopLevelWindowDFB::Init()
91 m_isMaximized
= false;
92 m_fsIsShowing
= false;
95 m_toPaint
= new wxDfbQueuedPaintRequests
;
99 bool wxTopLevelWindowDFB::Create(wxWindow
*parent
,
101 const wxString
& title
,
102 const wxPoint
& posOrig
,
103 const wxSize
& sizeOrig
,
105 const wxString
&name
)
109 // always create a frame of some reasonable, even if arbitrary, size (at
110 // least for MSW compatibility)
111 wxSize
size(sizeOrig
);
112 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
114 wxSize sizeDefault
= GetDefaultSize();
115 if ( size
.x
== wxDefaultCoord
)
116 size
.x
= sizeDefault
.x
;
117 if ( size
.y
== wxDefaultCoord
)
118 size
.y
= sizeDefault
.y
;
121 wxPoint
pos(posOrig
);
122 if ( pos
.x
== wxDefaultCoord
)
124 if ( pos
.y
== wxDefaultCoord
)
127 // create DirectFB window:
128 wxIDirectFBDisplayLayerPtr
layer(wxIDirectFB::Get()->GetDisplayLayer());
129 wxCHECK_MSG( layer
, false, _T("no display layer") );
131 DFBWindowDescription desc
;
132 desc
.flags
= (DFBWindowDescriptionFlags
)
134 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
135 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
139 desc
.height
= size
.y
;
140 m_dfbwin
= layer
->CreateWindow(&desc
);
144 // add the new TLW to DFBWindowID->wxTLW map:
146 if ( !m_dfbwin
->GetID(&winid
) )
148 gs_dfbWindowsMap
[winid
] = this;
150 // TLWs are created initially hidden:
151 if ( !m_dfbwin
->SetOpacity(wxALPHA_TRANSPARENT
) )
154 if ( !wxWindow::Create(NULL
, id
, pos
, size
, style
, name
) )
159 parent
->AddChild(this);
161 wxTopLevelWindows
.Append(this);
164 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
166 m_dfbwin
->SetStackingClass(DWSC_UPPER
);
169 // direct events in this window to the global event buffer:
170 m_dfbwin
->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
175 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
177 m_isBeingDeleted
= true;
179 // destroy all children before we destroy the underlying DirectFB window,
180 // so that if any of them does something with the TLW, it will still work:
183 // it's safe to delete the underlying DirectFB window now:
189 // remove the TLW from DFBWindowID->wxTLW map:
191 if ( m_dfbwin
->GetID(&winid
) )
192 gs_dfbWindowsMap
.erase(winid
);
198 // ----------------------------------------------------------------------------
199 // window size & position
200 // ----------------------------------------------------------------------------
202 void wxTopLevelWindowDFB::DoGetPosition(int *x
, int *y
) const
204 m_dfbwin
->GetPosition(x
, y
);
207 void wxTopLevelWindowDFB::DoGetSize(int *width
, int *height
) const
209 m_dfbwin
->GetSize(width
, height
);
212 void wxTopLevelWindowDFB::DoMoveWindow(int x
, int y
, int width
, int height
)
214 wxPoint curpos
= GetPosition();
215 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
217 m_dfbwin
->MoveTo(x
, y
);
220 wxSize cursize
= GetSize();
221 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
223 // changing window's size changes its surface:
224 InvalidateDfbSurface();
226 m_dfbwin
->Resize(width
, height
);
228 // we must repaint the window after it changed size:
234 // ----------------------------------------------------------------------------
235 // showing and hiding
236 // ----------------------------------------------------------------------------
238 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
239 bool wxTopLevelWindowDFB::ShowFullScreen(bool show
, long style
)
241 if (show
== m_fsIsShowing
) return false; // return what?
243 m_fsIsShowing
= show
;
247 m_fsSaveStyle
= m_windowStyle
;
248 m_fsSaveFlag
= style
;
249 GetPosition(&m_fsSaveFrame
.x
, &m_fsSaveFrame
.y
);
250 GetSize(&m_fsSaveFrame
.width
, &m_fsSaveFrame
.height
);
252 if ( style
& wxFULLSCREEN_NOCAPTION
)
253 m_windowStyle
&= ~wxCAPTION
;
254 if ( style
& wxFULLSCREEN_NOBORDER
)
255 m_windowStyle
= wxSIMPLE_BORDER
;
258 wxDisplaySize(&x
, &y
);
263 m_windowStyle
= m_fsSaveStyle
;
264 SetSize(m_fsSaveFrame
.x
, m_fsSaveFrame
.y
,
265 m_fsSaveFrame
.width
, m_fsSaveFrame
.height
);
271 bool wxTopLevelWindowDFB::Show(bool show
)
273 if ( !wxTopLevelWindowBase::Show(show
) )
276 // hide/show the window by setting its opacity to 0/full:
277 m_dfbwin
->SetOpacity(show
? m_opacity
: 0);
279 // If this is the first time Show was called, send size event,
280 // so that the frame can adjust itself (think auto layout or single child)
284 wxSizeEvent
event(GetSize(), GetId());
285 event
.SetEventObject(this);
286 GetEventHandler()->ProcessEvent(event
);
291 wxWindow
*focused
= wxWindow::FindFocus();
292 if ( focused
&& focused
->GetTLW() == this )
296 else if ( AcceptsFocus() )
298 // FIXME: we should probably always call SetDfbFocus instead
299 // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
308 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha
)
312 if ( !m_dfbwin
->SetOpacity(alpha
) )
320 // ----------------------------------------------------------------------------
321 // maximize, minimize etc.
322 // ----------------------------------------------------------------------------
324 void wxTopLevelWindowDFB::Maximize(bool maximize
)
327 wxClientDisplayRect(&x
, &y
, &w
, &h
);
329 if ( maximize
&& !m_isMaximized
)
331 m_isMaximized
= true;
333 GetPosition(&m_savedFrame
.x
, &m_savedFrame
.y
);
334 GetSize(&m_savedFrame
.width
, &m_savedFrame
.height
);
338 else if ( !maximize
&& m_isMaximized
)
340 m_isMaximized
= false;
341 SetSize(m_savedFrame
.x
, m_savedFrame
.y
,
342 m_savedFrame
.width
, m_savedFrame
.height
);
346 bool wxTopLevelWindowDFB::IsMaximized() const
348 return m_isMaximized
;
351 void wxTopLevelWindowDFB::Restore()
359 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize
))
361 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
364 bool wxTopLevelWindowDFB::IsIconized() const
370 // ----------------------------------------------------------------------------
371 // surfaces and painting
372 // ----------------------------------------------------------------------------
374 wxIDirectFBSurfacePtr
wxTopLevelWindowDFB::ObtainDfbSurface() const
376 return m_dfbwin
->GetSurface();
379 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
381 if ( m_toPaint
->IsEmpty() )
382 return; // nothing to do
384 if ( IsFrozen() || !IsShown() )
386 // nothing to do if the window is frozen or hidden; clear the queue
387 // and return (note that it's OK to clear the queue even if the window
388 // is frozen, because Thaw() calls Refresh()):
393 // process queued paint requests:
394 wxRect
winRect(wxPoint(0, 0), GetSize());
397 // important note: all DCs created from now until m_isPainting is reset to
398 // false will not update the front buffer as this flag indicates that we'll
399 // blit the entire back buffer to front soon
403 int requestsCount
= 0;
407 while ( m_toPaint
->GetNext(request
) )
412 wxRect
clipped(request
);
413 clipped
.Intersect(winRect
);
414 if ( clipped
.IsEmpty() )
415 continue; // nothing to refresh
417 wxLogTrace(TRACE_PAINT
,
418 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
419 this, GetName().c_str(),
420 clipped
.x
, clipped
.y
, clipped
.GetRight(), clipped
.GetBottom());
422 PaintWindow(clipped
);
424 // remember rectangle covering all repainted areas:
425 if ( paintedRect
.IsEmpty() )
426 paintedRect
= clipped
;
428 paintedRect
.Union(clipped
);
431 m_isPainting
= false;
435 if ( paintedRect
.IsEmpty() )
436 return; // no painting occurred, no need to flip
438 // Flip the surface to make the changes visible. Note that the rectangle we
439 // flip is *superset* of the union of repainted rectangles (created as
440 // "rectangles union" by wxRect::Union) and so some parts of the back
441 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
442 // be copied to the front buffer as well. This is safe/correct thing to do
443 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
444 // the back and front buffers contain the same data.
446 // Note that we do _not_ split m_toPaint into disjoint rectangles and
447 // do FlipToFront() for each of them, because that could result in visible
448 // updating of the screen; instead, we prefer to flip everything at once.
450 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
451 paintedRect
.GetRight(), paintedRect
.GetBottom()};
452 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
454 GetDfbSurface()->FlipToFront(rptr
);
456 wxLogTrace(TRACE_PAINT
,
457 _T("%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]"),
458 this, GetName().c_str(),
460 paintedRect
.x
, paintedRect
.y
,
461 paintedRect
.GetRight(), paintedRect
.GetBottom());
464 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect
& rect
)
466 // don't overlap outside of the window (NB: 'rect' is in window coords):
468 r
.Intersect(wxRect(GetSize()));
472 wxLogTrace(TRACE_PAINT
,
473 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
474 this, GetName().c_str(),
475 rect
.x
, rect
.y
, rect
.GetRight(), rect
.GetBottom());
477 // defer painting until idle time or until Update() is called:
478 m_toPaint
->Add(rect
);
481 void wxTopLevelWindowDFB::Update()
483 HandleQueuedPaintRequests();
486 // ---------------------------------------------------------------------------
488 // ---------------------------------------------------------------------------
490 void wxTopLevelWindowDFB::SetDfbFocus()
492 wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") );
493 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
494 _T("setting DirectFB focus to unexpected window") );
496 GetDirectFBWindow()->RequestFocus();
500 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
502 const DFBWindowEvent
& event
= event_
;
504 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
506 wxLogTrace(TRACE_EVENTS
,
507 _T("received event for unknown DirectFB window, ignoring"));
511 wxTopLevelWindowDFB
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
512 wxWindow
*recipient
= NULL
;
513 void (wxWindow::*handlerFunc
)(const wxDFBWindowEvent
&) = NULL
;
515 switch ( event
.type
)
520 recipient
= wxWindow::FindFocus();
521 handlerFunc
= &wxWindowDFB::HandleKeyEvent
;
528 wxFAIL_MSG( _T("invalid event type") );
533 // we're not interested in them here
539 wxLogTrace(TRACE_EVENTS
, _T("ignoring event: no recipient window"));
543 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
544 _T("event recipient not in TLW which received the event") );
546 // process the event:
547 (recipient
->*handlerFunc
)(event_
);
550 // ---------------------------------------------------------------------------
551 // idle events processing
552 // ---------------------------------------------------------------------------
554 void wxTopLevelWindowDFB::OnInternalIdle()
556 wxTopLevelWindowBase::OnInternalIdle();
557 HandleQueuedPaintRequests();