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
, bool eraseBackground
)
45 : m_rect(rect
), m_eraseBackground(eraseBackground
) {}
46 wxDfbPaintRequest(const wxDfbPaintRequest
& r
)
47 : m_rect(r
.m_rect
), m_eraseBackground(r
.m_eraseBackground
) {}
50 bool m_eraseBackground
;
53 WX_DEFINE_ARRAY_PTR(wxDfbPaintRequest
*, wxDfbQueuedPaintRequestsList
);
55 // Queue of paint requests
56 class wxDfbQueuedPaintRequests
59 ~wxDfbQueuedPaintRequests() { Clear(); }
61 // Adds paint request to the queue
62 void Add(const wxRect
& rect
, bool eraseBack
)
63 { m_queue
.push_back(new wxDfbPaintRequest(rect
, eraseBack
)); }
65 // Is the queue empty?
66 bool IsEmpty() const { return m_queue
.empty(); }
69 void Clear() { WX_CLEAR_ARRAY(m_queue
); }
71 // Gets requests in the queue
72 const wxDfbQueuedPaintRequestsList
& GetRequests() const { return m_queue
; }
75 wxDfbQueuedPaintRequestsList m_queue
;
78 // ============================================================================
79 // wxTopLevelWindowDFB
80 // ============================================================================
82 // ----------------------------------------------------------------------------
83 // creation & destruction
84 // ----------------------------------------------------------------------------
86 void wxTopLevelWindowDFB::Init()
89 m_isMaximized
= false;
90 m_fsIsShowing
= false;
93 m_toPaint
= new wxDfbQueuedPaintRequests
;
96 bool wxTopLevelWindowDFB::Create(wxWindow
*parent
,
98 const wxString
& title
,
99 const wxPoint
& posOrig
,
100 const wxSize
& sizeOrig
,
102 const wxString
&name
)
106 // always create a frame of some reasonable, even if arbitrary, size (at
107 // least for MSW compatibility)
108 wxSize
size(sizeOrig
);
109 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
111 wxSize sizeDefault
= GetDefaultSize();
112 if ( size
.x
== wxDefaultCoord
)
113 size
.x
= sizeDefault
.x
;
114 if ( size
.y
== wxDefaultCoord
)
115 size
.y
= sizeDefault
.y
;
118 wxPoint
pos(posOrig
);
119 if ( pos
.x
== wxDefaultCoord
)
121 if ( pos
.y
== wxDefaultCoord
)
124 // create DirectFB window:
125 wxIDirectFBDisplayLayerPtr layer
= wxDfbGetDisplayLayer();
126 wxCHECK_MSG( layer
, false, _T("no display layer") );
128 DFBWindowDescription desc
;
129 desc
.flags
= (DFBWindowDescriptionFlags
)
131 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
132 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
136 desc
.height
= size
.y
;
137 m_dfbwin
= layer
->CreateWindow(&desc
);
141 // add the new TLW to DFBWindowID->wxTLW map:
143 if ( !m_dfbwin
->GetID(&winid
) )
145 gs_dfbWindowsMap
[winid
] = this;
147 // TLWs are created initially hidden:
148 if ( !m_dfbwin
->SetOpacity(wxALPHA_TRANSPARENT
) )
151 wxWindow::Create(NULL
, id
, pos
, size
, style
, name
);
155 parent
->AddChild(this);
157 wxTopLevelWindows
.Append(this);
160 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
162 m_dfbwin
->SetStackingClass(DWSC_UPPER
);
165 // direct events in this window to the global event buffer:
166 m_dfbwin
->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
171 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
173 m_isBeingDeleted
= true;
175 wxTopLevelWindows
.DeleteObject(this);
177 if ( wxTheApp
->GetTopWindow() == this )
178 wxTheApp
->SetTopWindow(NULL
);
180 if ( wxTopLevelWindows
.empty() && wxTheApp
->GetExitOnFrameDelete() )
182 wxTheApp
->ExitMainLoop();
187 // remove the TLW from DFBWindowID->wxTLW map:
189 if ( m_dfbwin
->GetID(&winid
) )
190 gs_dfbWindowsMap
.erase(winid
);
193 // ----------------------------------------------------------------------------
194 // window size & position
195 // ----------------------------------------------------------------------------
197 void wxTopLevelWindowDFB::DoGetPosition(int *x
, int *y
) const
199 m_dfbwin
->GetPosition(x
, y
);
202 void wxTopLevelWindowDFB::DoGetSize(int *width
, int *height
) const
204 m_dfbwin
->GetSize(width
, height
);
207 void wxTopLevelWindowDFB::DoMoveWindow(int x
, int y
, int width
, int height
)
209 wxPoint curpos
= GetPosition();
210 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
212 m_dfbwin
->MoveTo(x
, y
);
215 wxSize cursize
= GetSize();
216 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
218 m_dfbwin
->Resize(width
, height
);
219 // we must repaint the window after it changed size:
224 // ----------------------------------------------------------------------------
225 // showing and hiding
226 // ----------------------------------------------------------------------------
228 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
229 bool wxTopLevelWindowDFB::ShowFullScreen(bool show
, long style
)
231 if (show
== m_fsIsShowing
) return false; // return what?
233 m_fsIsShowing
= show
;
237 m_fsSaveStyle
= m_windowStyle
;
238 m_fsSaveFlag
= style
;
239 GetPosition(&m_fsSaveFrame
.x
, &m_fsSaveFrame
.y
);
240 GetSize(&m_fsSaveFrame
.width
, &m_fsSaveFrame
.height
);
242 if ( style
& wxFULLSCREEN_NOCAPTION
)
243 m_windowStyle
&= ~wxCAPTION
;
244 if ( style
& wxFULLSCREEN_NOBORDER
)
245 m_windowStyle
= wxSIMPLE_BORDER
;
248 wxDisplaySize(&x
, &y
);
253 m_windowStyle
= m_fsSaveStyle
;
254 SetSize(m_fsSaveFrame
.x
, m_fsSaveFrame
.y
,
255 m_fsSaveFrame
.width
, m_fsSaveFrame
.height
);
261 bool wxTopLevelWindowDFB::Show(bool show
)
263 if ( !wxTopLevelWindowBase::Show(show
) )
266 // hide/show the window by setting its opacity to 0/full:
267 m_dfbwin
->SetOpacity(show
? m_opacity
: 0);
269 // If this is the first time Show was called, send size event,
270 // so that the frame can adjust itself (think auto layout or single child)
274 wxSizeEvent
event(GetSize(), GetId());
275 event
.SetEventObject(this);
276 GetEventHandler()->ProcessEvent(event
);
279 // FIXME_DFB: do this at all?
280 if ( show
&& AcceptsFocus() )
282 // FIXME_DFB -- don't do this for popup windows?
287 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha
)
291 if ( !m_dfbwin
->SetOpacity(alpha
) )
299 // ----------------------------------------------------------------------------
300 // maximize, minimize etc.
301 // ----------------------------------------------------------------------------
303 void wxTopLevelWindowDFB::Maximize(bool maximize
)
306 wxClientDisplayRect(&x
, &y
, &w
, &h
);
308 if ( maximize
&& !m_isMaximized
)
310 m_isMaximized
= true;
312 GetPosition(&m_savedFrame
.x
, &m_savedFrame
.y
);
313 GetSize(&m_savedFrame
.width
, &m_savedFrame
.height
);
317 else if ( !maximize
&& m_isMaximized
)
319 m_isMaximized
= false;
320 SetSize(m_savedFrame
.x
, m_savedFrame
.y
,
321 m_savedFrame
.width
, m_savedFrame
.height
);
325 bool wxTopLevelWindowDFB::IsMaximized() const
327 return m_isMaximized
;
330 void wxTopLevelWindowDFB::Restore()
338 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize
))
340 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
343 bool wxTopLevelWindowDFB::IsIconized() const
349 // ----------------------------------------------------------------------------
350 // surfaces and painting
351 // ----------------------------------------------------------------------------
353 wxIDirectFBSurfacePtr
wxTopLevelWindowDFB::ObtainDfbSurface() const
355 return m_dfbwin
->GetSurface();
358 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
360 if ( m_toPaint
->IsEmpty() )
361 return; // nothing to do
363 if ( IsFrozen() || !IsShown() )
365 // nothing to do if the window is frozen or hidden; clear the queue
366 // and return (note that it's OK to clear the queue even if the window
367 // is frozen, because Thaw() calls Refresh()):
372 const wxDfbQueuedPaintRequestsList
& requests
= m_toPaint
->GetRequests();
374 // process queued paint requests:
375 wxRect
winRect(wxPoint(0, 0), GetSize());
378 size_t cnt
= requests
.size();
379 for ( size_t i
= 0; i
< cnt
; ++i
)
381 const wxDfbPaintRequest
& request
= *requests
[i
];
383 wxRect
clipped(request
.m_rect
);
385 wxLogTrace(TRACE_PAINT
,
386 _T("%p ('%s'): processing paint request [x=%i,y=%i,w=%i,h=%i]"),
387 this, GetName().c_str(),
388 clipped
.x
, clipped
.y
, clipped
.width
, clipped
.height
);
390 clipped
.Intersect(winRect
);
391 if ( clipped
.IsEmpty() )
392 continue; // nothing to refresh
394 PaintWindow(clipped
, request
.m_eraseBackground
);
396 // remember rectangle covering all repainted areas:
397 if ( paintedRect
.IsEmpty() )
398 paintedRect
= clipped
;
400 paintedRect
.Union(clipped
);
405 if ( paintedRect
.IsEmpty() )
406 return; // no painting occurred, no need to flip
408 // flip the surface to make the changes visible:
409 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
410 paintedRect
.GetRight(), paintedRect
.GetBottom()};
411 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
413 GetDfbSurface()->Flip(rptr
, DSFLIP_NONE
);
416 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect
& rect
, bool eraseBack
)
418 // defer painting until idle time or until Update() is called:
419 m_toPaint
->Add(rect
, eraseBack
);
422 void wxTopLevelWindowDFB::Update()
424 HandleQueuedPaintRequests();
427 // ---------------------------------------------------------------------------
429 // ---------------------------------------------------------------------------
432 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
434 const DFBWindowEvent
& event
= event_
;
436 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
438 wxLogTrace(TRACE_EVENTS
,
439 _T("received event for unknown DirectFB window, ignoring"));
443 wxTopLevelWindowDFB
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
444 wxWindow
*recipient
= NULL
;
445 void (wxWindow::*handlerFunc
)(const wxDFBWindowEvent
&) = NULL
;
447 switch ( event
.type
)
452 recipient
= wxWindow::FindFocus();
453 handlerFunc
= &wxWindowDFB::HandleKeyEvent
;
460 wxFAIL_MSG( _T("invalid event type") );
467 wxLogTrace(TRACE_EVENTS
, _T("ignoring event: no recipient window"));
471 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
472 _T("event recipient not in TLW which received the event") );
474 // process the event:
475 (recipient
->*handlerFunc
)(event_
);
478 // ---------------------------------------------------------------------------
479 // idle events processing
480 // ---------------------------------------------------------------------------
482 void wxTopLevelWindowDFB::OnInternalIdle()
484 wxTopLevelWindowBase::OnInternalIdle();
485 HandleQueuedPaintRequests();