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"
18 #include "wx/dynarray.h"
21 #include "wx/hashmap.h"
22 #include "wx/evtloop.h"
23 #include "wx/dfb/private.h"
25 #define TRACE_EVENTS _T("events")
26 #define TRACE_PAINT _T("paint")
28 // ============================================================================
30 // ============================================================================
32 // mapping of DirectFB windows to wxTLWs:
33 WX_DECLARE_HASH_MAP(DFBWindowID
, wxTopLevelWindowDFB
*,
34 wxIntegerHash
, wxIntegerEqual
,
36 static wxDfbWindowsMap gs_dfbWindowsMap
;
38 // ============================================================================
40 // ============================================================================
42 struct wxDfbPaintRequest
44 wxDfbPaintRequest(const wxRect
& rect
) : m_rect(rect
) {}
45 wxDfbPaintRequest(const wxDfbPaintRequest
& r
) : m_rect(r
.m_rect
) {}
50 WX_DEFINE_ARRAY_PTR(wxDfbPaintRequest
*, wxDfbQueuedPaintRequestsList
);
52 // Queue of paint requests
53 class wxDfbQueuedPaintRequests
56 ~wxDfbQueuedPaintRequests() { Clear(); }
58 // Adds paint request to the queue
59 void Add(const wxRect
& rect
)
60 { m_queue
.push_back(new wxDfbPaintRequest(rect
)); }
62 // Is the queue empty?
63 bool IsEmpty() const { return m_queue
.empty(); }
66 void Clear() { WX_CLEAR_ARRAY(m_queue
); }
68 // Gets requests in the queue
69 const wxDfbQueuedPaintRequestsList
& GetRequests() const { return m_queue
; }
72 wxDfbQueuedPaintRequestsList m_queue
;
75 // ============================================================================
76 // wxTopLevelWindowDFB
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // creation & destruction
81 // ----------------------------------------------------------------------------
83 void wxTopLevelWindowDFB::Init()
86 m_isMaximized
= false;
87 m_fsIsShowing
= false;
90 m_toPaint
= new wxDfbQueuedPaintRequests
;
94 bool wxTopLevelWindowDFB::Create(wxWindow
*parent
,
96 const wxString
& title
,
97 const wxPoint
& posOrig
,
98 const wxSize
& sizeOrig
,
100 const wxString
&name
)
104 // always create a frame of some reasonable, even if arbitrary, size (at
105 // least for MSW compatibility)
106 wxSize
size(sizeOrig
);
107 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
109 wxSize sizeDefault
= GetDefaultSize();
110 if ( size
.x
== wxDefaultCoord
)
111 size
.x
= sizeDefault
.x
;
112 if ( size
.y
== wxDefaultCoord
)
113 size
.y
= sizeDefault
.y
;
116 wxPoint
pos(posOrig
);
117 if ( pos
.x
== wxDefaultCoord
)
119 if ( pos
.y
== wxDefaultCoord
)
122 // create DirectFB window:
123 wxIDirectFBDisplayLayerPtr
layer(wxIDirectFB::Get()->GetDisplayLayer());
124 wxCHECK_MSG( layer
, false, _T("no display layer") );
126 DFBWindowDescription desc
;
127 desc
.flags
= (DFBWindowDescriptionFlags
)
129 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
130 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
134 desc
.height
= size
.y
;
135 m_dfbwin
= layer
->CreateWindow(&desc
);
139 // add the new TLW to DFBWindowID->wxTLW map:
141 if ( !m_dfbwin
->GetID(&winid
) )
143 gs_dfbWindowsMap
[winid
] = this;
145 // TLWs are created initially hidden:
146 if ( !m_dfbwin
->SetOpacity(wxALPHA_TRANSPARENT
) )
149 if ( !wxWindow::Create(NULL
, id
, pos
, size
, style
, name
) )
154 parent
->AddChild(this);
156 wxTopLevelWindows
.Append(this);
159 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
161 m_dfbwin
->SetStackingClass(DWSC_UPPER
);
164 // direct events in this window to the global event buffer:
165 m_dfbwin
->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
170 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
172 m_isBeingDeleted
= true;
174 wxTopLevelWindows
.DeleteObject(this);
176 if ( wxTheApp
->GetTopWindow() == this )
177 wxTheApp
->SetTopWindow(NULL
);
179 if ( wxTopLevelWindows
.empty() && wxTheApp
->GetExitOnFrameDelete() )
181 wxTheApp
->ExitMainLoop();
186 // remove the TLW from DFBWindowID->wxTLW map:
188 if ( m_dfbwin
->GetID(&winid
) )
189 gs_dfbWindowsMap
.erase(winid
);
192 // ----------------------------------------------------------------------------
193 // window size & position
194 // ----------------------------------------------------------------------------
196 void wxTopLevelWindowDFB::DoGetPosition(int *x
, int *y
) const
198 m_dfbwin
->GetPosition(x
, y
);
201 void wxTopLevelWindowDFB::DoGetSize(int *width
, int *height
) const
203 m_dfbwin
->GetSize(width
, height
);
206 void wxTopLevelWindowDFB::DoMoveWindow(int x
, int y
, int width
, int height
)
208 wxPoint curpos
= GetPosition();
209 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
211 m_dfbwin
->MoveTo(x
, y
);
214 wxSize cursize
= GetSize();
215 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
217 // changing window's size changes its surface:
218 InvalidateDfbSurface();
220 m_dfbwin
->Resize(width
, height
);
222 // we must repaint the window after it changed size:
228 // ----------------------------------------------------------------------------
229 // showing and hiding
230 // ----------------------------------------------------------------------------
232 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
233 bool wxTopLevelWindowDFB::ShowFullScreen(bool show
, long style
)
235 if (show
== m_fsIsShowing
) return false; // return what?
237 m_fsIsShowing
= show
;
241 m_fsSaveStyle
= m_windowStyle
;
242 m_fsSaveFlag
= style
;
243 GetPosition(&m_fsSaveFrame
.x
, &m_fsSaveFrame
.y
);
244 GetSize(&m_fsSaveFrame
.width
, &m_fsSaveFrame
.height
);
246 if ( style
& wxFULLSCREEN_NOCAPTION
)
247 m_windowStyle
&= ~wxCAPTION
;
248 if ( style
& wxFULLSCREEN_NOBORDER
)
249 m_windowStyle
= wxSIMPLE_BORDER
;
252 wxDisplaySize(&x
, &y
);
257 m_windowStyle
= m_fsSaveStyle
;
258 SetSize(m_fsSaveFrame
.x
, m_fsSaveFrame
.y
,
259 m_fsSaveFrame
.width
, m_fsSaveFrame
.height
);
265 bool wxTopLevelWindowDFB::Show(bool show
)
267 if ( !wxTopLevelWindowBase::Show(show
) )
270 // hide/show the window by setting its opacity to 0/full:
271 m_dfbwin
->SetOpacity(show
? m_opacity
: 0);
273 // If this is the first time Show was called, send size event,
274 // so that the frame can adjust itself (think auto layout or single child)
278 wxSizeEvent
event(GetSize(), GetId());
279 event
.SetEventObject(this);
280 GetEventHandler()->ProcessEvent(event
);
283 // FIXME_DFB: do this at all?
284 if ( show
&& AcceptsFocus() )
286 // FIXME_DFB -- don't do this for popup windows?
291 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha
)
295 if ( !m_dfbwin
->SetOpacity(alpha
) )
303 // ----------------------------------------------------------------------------
304 // maximize, minimize etc.
305 // ----------------------------------------------------------------------------
307 void wxTopLevelWindowDFB::Maximize(bool maximize
)
310 wxClientDisplayRect(&x
, &y
, &w
, &h
);
312 if ( maximize
&& !m_isMaximized
)
314 m_isMaximized
= true;
316 GetPosition(&m_savedFrame
.x
, &m_savedFrame
.y
);
317 GetSize(&m_savedFrame
.width
, &m_savedFrame
.height
);
321 else if ( !maximize
&& m_isMaximized
)
323 m_isMaximized
= false;
324 SetSize(m_savedFrame
.x
, m_savedFrame
.y
,
325 m_savedFrame
.width
, m_savedFrame
.height
);
329 bool wxTopLevelWindowDFB::IsMaximized() const
331 return m_isMaximized
;
334 void wxTopLevelWindowDFB::Restore()
342 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize
))
344 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
347 bool wxTopLevelWindowDFB::IsIconized() const
353 // ----------------------------------------------------------------------------
354 // surfaces and painting
355 // ----------------------------------------------------------------------------
357 wxIDirectFBSurfacePtr
wxTopLevelWindowDFB::ObtainDfbSurface() const
359 return m_dfbwin
->GetSurface();
362 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
364 if ( m_toPaint
->IsEmpty() )
365 return; // nothing to do
367 if ( IsFrozen() || !IsShown() )
369 // nothing to do if the window is frozen or hidden; clear the queue
370 // and return (note that it's OK to clear the queue even if the window
371 // is frozen, because Thaw() calls Refresh()):
376 const wxDfbQueuedPaintRequestsList
& requests
= m_toPaint
->GetRequests();
378 // process queued paint requests:
379 wxRect
winRect(wxPoint(0, 0), GetSize());
382 // important note: all DCs created from now until m_isPainting is reset to
383 // false will not update the front buffer as this flag indicates that we'll
384 // blit the entire back buffer to front soon
387 size_t cnt
= requests
.size();
388 wxLogTrace(TRACE_PAINT
, _T("%p ('%s'): processing %i paint requests"),
389 this, GetName().c_str(), cnt
);
391 for ( size_t i
= 0; i
< cnt
; ++i
)
393 const wxDfbPaintRequest
& request
= *requests
[i
];
395 wxRect
clipped(request
.m_rect
);
396 clipped
.Intersect(winRect
);
397 if ( clipped
.IsEmpty() )
398 continue; // nothing to refresh
400 wxLogTrace(TRACE_PAINT
,
401 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
402 this, GetName().c_str(),
403 clipped
.x
, clipped
.y
, clipped
.GetRight(), clipped
.GetBottom());
405 PaintWindow(clipped
);
407 // remember rectangle covering all repainted areas:
408 if ( paintedRect
.IsEmpty() )
409 paintedRect
= clipped
;
411 paintedRect
.Union(clipped
);
414 m_isPainting
= false;
418 if ( paintedRect
.IsEmpty() )
419 return; // no painting occurred, no need to flip
421 // Flip the surface to make the changes visible. Note that the rectangle we
422 // flip is *superset* of the union of repainted rectangles (created as
423 // "rectangles union" by wxRect::Union) and so some parts of the back
424 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
425 // be copied to the front buffer as well. This is safe/correct thing to do
426 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
427 // the back and front buffers contain the same data.
429 // Note that we do _not_ split m_toPaint into disjoint rectangles and
430 // do FlipToFront() for each of them, because that could result in visible
431 // updating of the screen; instead, we prefer to flip everything at once.
433 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
434 paintedRect
.GetRight(), paintedRect
.GetBottom()};
435 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
437 GetDfbSurface()->FlipToFront(rptr
);
439 wxLogTrace(TRACE_PAINT
,
440 _T("%p ('%s'): flipped surface: [%i,%i,%i,%i]"),
441 this, GetName().c_str(),
442 paintedRect
.x
, paintedRect
.y
,
443 paintedRect
.GetRight(), paintedRect
.GetBottom());
446 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect
& rect
)
448 // don't overlap outside of the window (NB: 'rect' is in window coords):
450 r
.Intersect(wxRect(GetSize()));
454 wxLogTrace(TRACE_PAINT
,
455 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
456 this, GetName().c_str(),
457 rect
.x
, rect
.y
, rect
.GetRight(), rect
.GetBottom());
459 // defer painting until idle time or until Update() is called:
460 m_toPaint
->Add(rect
);
463 void wxTopLevelWindowDFB::Update()
465 HandleQueuedPaintRequests();
468 // ---------------------------------------------------------------------------
470 // ---------------------------------------------------------------------------
473 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
475 const DFBWindowEvent
& event
= event_
;
477 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
479 wxLogTrace(TRACE_EVENTS
,
480 _T("received event for unknown DirectFB window, ignoring"));
484 wxTopLevelWindowDFB
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
485 wxWindow
*recipient
= NULL
;
486 void (wxWindow::*handlerFunc
)(const wxDFBWindowEvent
&) = NULL
;
488 switch ( event
.type
)
493 recipient
= wxWindow::FindFocus();
494 handlerFunc
= &wxWindowDFB::HandleKeyEvent
;
501 wxFAIL_MSG( _T("invalid event type") );
508 wxLogTrace(TRACE_EVENTS
, _T("ignoring event: no recipient window"));
512 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
513 _T("event recipient not in TLW which received the event") );
515 // process the event:
516 (recipient
->*handlerFunc
)(event_
);
519 // ---------------------------------------------------------------------------
520 // idle events processing
521 // ---------------------------------------------------------------------------
523 void wxTopLevelWindowDFB::OnInternalIdle()
525 wxTopLevelWindowBase::OnInternalIdle();
526 HandleQueuedPaintRequests();