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
*, wxDfbQueuedPaintRequests
);
55 // ============================================================================
56 // wxTopLevelWindowDFB
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // creation & destruction
61 // ----------------------------------------------------------------------------
63 void wxTopLevelWindowDFB::Init()
66 m_isMaximized
= false;
67 m_fsIsShowing
= false;
70 m_toPaint
= new wxDfbQueuedPaintRequests
;
73 bool wxTopLevelWindowDFB::Create(wxWindow
*parent
,
75 const wxString
& title
,
76 const wxPoint
& posOrig
,
77 const wxSize
& sizeOrig
,
83 // always create a frame of some reasonable, even if arbitrary, size (at
84 // least for MSW compatibility)
85 wxSize
size(sizeOrig
);
86 if ( size
.x
== wxDefaultCoord
|| size
.y
== wxDefaultCoord
)
88 wxSize sizeDefault
= GetDefaultSize();
89 if ( size
.x
== wxDefaultCoord
)
90 size
.x
= sizeDefault
.x
;
91 if ( size
.y
== wxDefaultCoord
)
92 size
.y
= sizeDefault
.y
;
96 if ( pos
.x
== wxDefaultCoord
)
98 if ( pos
.y
== wxDefaultCoord
)
101 // create DirectFB window:
102 IDirectFBDisplayLayerPtr layer
= wxDfbGetDisplayLayer();
103 wxCHECK_MSG( layer
, false, _T("no display layer") );
105 DFBWindowDescription desc
;
106 desc
.flags
= (DFBWindowDescriptionFlags
)
108 DWDESC_WIDTH
| DWDESC_HEIGHT
| DWDESC_POSX
| DWDESC_POSY
);
109 desc
.caps
= DWCAPS_DOUBLEBUFFER
;
113 desc
.height
= size
.y
;
114 if ( !DFB_CALL( layer
->CreateWindow(layer
, &desc
, &m_dfbwin
) ) )
117 // add the new TLW to DFBWindowID->wxTLW map:
119 if ( !DFB_CALL( m_dfbwin
->GetID(m_dfbwin
, &winid
) ) )
121 gs_dfbWindowsMap
[winid
] = this;
123 // TLWs are created initially hidden:
124 if ( !DFB_CALL( m_dfbwin
->SetOpacity(m_dfbwin
, 0) ) )
127 wxWindow::Create(NULL
, id
, pos
, size
, style
, name
);
131 parent
->AddChild(this);
133 wxTopLevelWindows
.Append(this);
136 if ( style
& (wxSTAY_ON_TOP
| wxPOPUP_WINDOW
) )
138 DFB_CALL( m_dfbwin
->SetStackingClass(m_dfbwin
, DWSC_UPPER
) );
141 // direct events in this window to the global event buffer:
142 DFB_CALL( m_dfbwin
->AttachEventBuffer(
143 m_dfbwin
, wxEventLoop::GetDirectFBEventBuffer()) );
148 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
150 m_isBeingDeleted
= true;
152 wxTopLevelWindows
.DeleteObject(this);
154 if ( wxTheApp
->GetTopWindow() == this )
155 wxTheApp
->SetTopWindow(NULL
);
157 if ( wxTopLevelWindows
.empty() && wxTheApp
->GetExitOnFrameDelete() )
159 wxTheApp
->ExitMainLoop();
162 WX_CLEAR_ARRAY(*m_toPaint
);
165 // remove the TLW from DFBWindowID->wxTLW map:
167 if ( DFB_CALL( m_dfbwin
->GetID(m_dfbwin
, &winid
) ) )
168 gs_dfbWindowsMap
.erase(winid
);
171 // ----------------------------------------------------------------------------
172 // window size & position
173 // ----------------------------------------------------------------------------
175 void wxTopLevelWindowDFB::DoGetPosition(int *x
, int *y
) const
177 DFB_CALL( m_dfbwin
->GetPosition(m_dfbwin
, x
, y
) );
180 void wxTopLevelWindowDFB::DoGetSize(int *width
, int *height
) const
182 DFB_CALL( m_dfbwin
->GetSize(m_dfbwin
, width
, height
) );
185 void wxTopLevelWindowDFB::DoMoveWindow(int x
, int y
, int width
, int height
)
187 wxPoint curpos
= GetPosition();
188 if ( curpos
.x
!= x
|| curpos
.y
!= y
)
190 DFB_CALL( m_dfbwin
->MoveTo(m_dfbwin
, x
, y
) );
193 wxSize cursize
= GetSize();
194 if ( cursize
.x
!= width
|| cursize
.y
!= height
)
196 DFB_CALL( m_dfbwin
->Resize(m_dfbwin
, width
, height
) );
197 // we must repaint the window after it changed size:
202 // ----------------------------------------------------------------------------
203 // showing and hiding
204 // ----------------------------------------------------------------------------
206 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
207 bool wxTopLevelWindowDFB::ShowFullScreen(bool show
, long style
)
209 if (show
== m_fsIsShowing
) return false; // return what?
211 m_fsIsShowing
= show
;
215 m_fsSaveStyle
= m_windowStyle
;
216 m_fsSaveFlag
= style
;
217 GetPosition(&m_fsSaveFrame
.x
, &m_fsSaveFrame
.y
);
218 GetSize(&m_fsSaveFrame
.width
, &m_fsSaveFrame
.height
);
220 if ( style
& wxFULLSCREEN_NOCAPTION
)
221 m_windowStyle
&= ~wxCAPTION
;
222 if ( style
& wxFULLSCREEN_NOBORDER
)
223 m_windowStyle
= wxSIMPLE_BORDER
;
226 wxDisplaySize(&x
, &y
);
231 m_windowStyle
= m_fsSaveStyle
;
232 SetSize(m_fsSaveFrame
.x
, m_fsSaveFrame
.y
,
233 m_fsSaveFrame
.width
, m_fsSaveFrame
.height
);
239 bool wxTopLevelWindowDFB::Show(bool show
)
241 if ( !wxTopLevelWindowBase::Show(show
) )
244 // hide/show the window by setting its opacity to 0/full:
245 DFB_CALL( m_dfbwin
->SetOpacity(m_dfbwin
, show
? m_opacity
: 0) );
247 // If this is the first time Show was called, send size event,
248 // so that the frame can adjust itself (think auto layout or single child)
252 wxSizeEvent
event(GetSize(), GetId());
253 event
.SetEventObject(this);
254 GetEventHandler()->ProcessEvent(event
);
257 // FIXME_DFB: do this at all?
258 if ( show
&& AcceptsFocus() )
260 // FIXME_DFB -- don't do this for popup windows?
265 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha
)
269 if ( !DFB_CALL( m_dfbwin
->SetOpacity(m_dfbwin
, alpha
) ) )
277 // ----------------------------------------------------------------------------
278 // maximize, minimize etc.
279 // ----------------------------------------------------------------------------
281 void wxTopLevelWindowDFB::Maximize(bool maximize
)
284 wxClientDisplayRect(&x
, &y
, &w
, &h
);
286 if ( maximize
&& !m_isMaximized
)
288 m_isMaximized
= true;
290 GetPosition(&m_savedFrame
.x
, &m_savedFrame
.y
);
291 GetSize(&m_savedFrame
.width
, &m_savedFrame
.height
);
295 else if ( !maximize
&& m_isMaximized
)
297 m_isMaximized
= false;
298 SetSize(m_savedFrame
.x
, m_savedFrame
.y
,
299 m_savedFrame
.width
, m_savedFrame
.height
);
303 bool wxTopLevelWindowDFB::IsMaximized() const
305 return m_isMaximized
;
308 void wxTopLevelWindowDFB::Restore()
316 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize
))
318 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
321 bool wxTopLevelWindowDFB::IsIconized() const
327 // ----------------------------------------------------------------------------
328 // surfaces and painting
329 // ----------------------------------------------------------------------------
331 IDirectFBSurfacePtr
wxTopLevelWindowDFB::ObtainDfbSurface() const
333 IDirectFBSurfacePtr surface
;
334 DFB_CALL( m_dfbwin
->GetSurface(m_dfbwin
, &surface
) );
338 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
340 wxDfbQueuedPaintRequests
& toPaint
= *m_toPaint
;
341 if ( toPaint
.empty() )
342 return; // nothing to do
344 // process queued paint requests:
345 wxRect
winRect(wxPoint(0, 0), GetSize());
348 size_t cnt
= toPaint
.size();
349 for ( size_t i
= 0; i
< cnt
; ++i
)
351 const wxDfbPaintRequest
& request
= *toPaint
[i
];
353 wxRect
clipped(request
.m_rect
);
355 wxLogTrace(TRACE_PAINT
,
356 _T("%p ('%s'): processing paint request [x=%i,y=%i,w=%i,h=%i]"),
357 this, GetName().c_str(),
358 clipped
.x
, clipped
.y
, clipped
.width
, clipped
.height
);
360 clipped
.Intersect(winRect
);
361 if ( clipped
.IsEmpty() )
362 continue; // nothing to refresh
364 PaintWindow(clipped
, request
.m_eraseBackground
);
366 // remember rectangle covering all repainted areas:
367 if ( paintedRect
.IsEmpty() )
368 paintedRect
= clipped
;
370 paintedRect
.Union(clipped
);
373 WX_CLEAR_ARRAY(toPaint
);
375 if ( paintedRect
.IsEmpty() )
376 return; // no painting occurred, no need to flip
378 // flip the surface to make the changes visible:
379 DFBRegion r
= {paintedRect
.GetLeft(), paintedRect
.GetTop(),
380 paintedRect
.GetRight(), paintedRect
.GetBottom()};
381 DFBRegion
*rptr
= (winRect
== paintedRect
) ? NULL
: &r
;
383 IDirectFBSurfacePtr
surface(GetDfbSurface());
384 DFB_CALL( surface
->Flip(surface
, rptr
, DSFLIP_NONE
) );
387 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect
& rect
, bool eraseBack
)
389 // defer paiting until idle time or until Update() is called:
390 m_toPaint
->push_back(new wxDfbPaintRequest(rect
, eraseBack
));
393 void wxTopLevelWindowDFB::Update()
395 HandleQueuedPaintRequests();
398 // ---------------------------------------------------------------------------
400 // ---------------------------------------------------------------------------
403 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent
& event_
)
405 const DFBWindowEvent
& event
= event_
;
407 if ( gs_dfbWindowsMap
.find(event
.window_id
) == gs_dfbWindowsMap
.end() )
409 wxLogTrace(TRACE_EVENTS
,
410 _T("received event for unknown DirectFB window, ignoring"));
414 wxTopLevelWindowDFB
*tlw
= gs_dfbWindowsMap
[event
.window_id
];
415 wxWindow
*recipient
= NULL
;
416 void (wxWindow::*handlerFunc
)(const wxDFBWindowEvent
&) = NULL
;
418 switch ( event
.type
)
423 recipient
= wxWindow::FindFocus();
424 handlerFunc
= &wxWindowDFB::HandleKeyEvent
;
431 wxFAIL_MSG( _T("invalid event type") );
438 wxLogTrace(TRACE_EVENTS
, _T("ignoring event: no recipient window"));
442 wxCHECK_RET( recipient
&& recipient
->GetTLW() == tlw
,
443 _T("event recipient not in TLW which received the event") );
445 // process the event:
446 (recipient
->*handlerFunc
)(event_
);
449 // ---------------------------------------------------------------------------
450 // idle events processing
451 // ---------------------------------------------------------------------------
453 void wxTopLevelWindowDFB::OnInternalIdle()
455 wxTopLevelWindowBase::OnInternalIdle();
456 HandleQueuedPaintRequests();