]>
Commit | Line | Data |
---|---|---|
b3c86150 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/dfb/toplevel.cpp | |
3 | // Purpose: Top level window, abstraction of wxFrame and wxDialog | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2006-08-10 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006 REA Elektronik GmbH | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/toplevel.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/app.h" | |
18 | #include "wx/dynarray.h" | |
19 | #endif // WX_PRECOMP | |
20 | ||
21 | #include "wx/hashmap.h" | |
22 | #include "wx/evtloop.h" | |
23 | #include "wx/dfb/private.h" | |
24 | ||
25 | #define TRACE_EVENTS _T("events") | |
26 | #define TRACE_PAINT _T("paint") | |
27 | ||
28 | // ============================================================================ | |
29 | // globals | |
30 | // ============================================================================ | |
31 | ||
32 | // mapping of DirectFB windows to wxTLWs: | |
33 | WX_DECLARE_HASH_MAP(DFBWindowID, wxTopLevelWindowDFB*, | |
34 | wxIntegerHash, wxIntegerEqual, | |
35 | wxDfbWindowsMap); | |
36 | static wxDfbWindowsMap gs_dfbWindowsMap; | |
37 | ||
38 | // ============================================================================ | |
39 | // helpers | |
40 | // ============================================================================ | |
41 | ||
42 | struct wxDfbPaintRequest | |
43 | { | |
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) {} | |
48 | ||
49 | wxRect m_rect; | |
50 | bool m_eraseBackground; | |
51 | }; | |
52 | ||
53 | WX_DEFINE_ARRAY_PTR(wxDfbPaintRequest*, wxDfbQueuedPaintRequests); | |
54 | ||
55 | // ============================================================================ | |
56 | // wxTopLevelWindowDFB | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // creation & destruction | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | void wxTopLevelWindowDFB::Init() | |
64 | { | |
65 | m_isShown = false; | |
66 | m_isMaximized = false; | |
67 | m_fsIsShowing = false; | |
68 | m_sizeSet = false; | |
69 | m_opacity = 255; | |
70 | m_toPaint = new wxDfbQueuedPaintRequests; | |
71 | } | |
72 | ||
73 | bool wxTopLevelWindowDFB::Create(wxWindow *parent, | |
74 | wxWindowID id, | |
75 | const wxString& title, | |
76 | const wxPoint& posOrig, | |
77 | const wxSize& sizeOrig, | |
78 | long style, | |
79 | const wxString &name) | |
80 | { | |
81 | m_tlw = this; | |
82 | ||
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 ) | |
87 | { | |
88 | wxSize sizeDefault = GetDefaultSize(); | |
89 | if ( size.x == wxDefaultCoord ) | |
90 | size.x = sizeDefault.x; | |
91 | if ( size.y == wxDefaultCoord ) | |
92 | size.y = sizeDefault.y; | |
93 | } | |
94 | ||
95 | wxPoint pos(posOrig); | |
96 | if ( pos.x == wxDefaultCoord ) | |
97 | pos.x = 0; | |
98 | if ( pos.y == wxDefaultCoord ) | |
99 | pos.y = 0; | |
100 | ||
101 | // create DirectFB window: | |
102 | IDirectFBDisplayLayerPtr layer = wxDfbGetDisplayLayer(); | |
103 | wxCHECK_MSG( layer, false, _T("no display layer") ); | |
104 | ||
105 | DFBWindowDescription desc; | |
106 | desc.flags = (DFBWindowDescriptionFlags) | |
107 | (DWDESC_CAPS | | |
108 | DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY); | |
109 | desc.caps = DWCAPS_DOUBLEBUFFER; | |
110 | desc.posx = pos.x; | |
111 | desc.posy = pos.y; | |
112 | desc.width = size.x; | |
113 | desc.height = size.y; | |
114 | if ( !DFB_CALL( layer->CreateWindow(layer, &desc, &m_dfbwin) ) ) | |
115 | return false; | |
116 | ||
117 | // add the new TLW to DFBWindowID->wxTLW map: | |
118 | DFBWindowID winid; | |
119 | if ( !DFB_CALL( m_dfbwin->GetID(m_dfbwin, &winid) ) ) | |
120 | return false; | |
121 | gs_dfbWindowsMap[winid] = this; | |
122 | ||
123 | // TLWs are created initially hidden: | |
124 | if ( !DFB_CALL( m_dfbwin->SetOpacity(m_dfbwin, 0) ) ) | |
125 | return false; | |
126 | ||
127 | wxWindow::Create(NULL, id, pos, size, style, name); | |
128 | ||
129 | SetParent(parent); | |
130 | if ( parent ) | |
131 | parent->AddChild(this); | |
132 | ||
133 | wxTopLevelWindows.Append(this); | |
134 | m_title = title; | |
135 | ||
136 | if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) ) | |
137 | { | |
138 | DFB_CALL( m_dfbwin->SetStackingClass(m_dfbwin, DWSC_UPPER) ); | |
139 | } | |
140 | ||
141 | // direct events in this window to the global event buffer: | |
142 | DFB_CALL( m_dfbwin->AttachEventBuffer( | |
143 | m_dfbwin, wxEventLoop::GetDirectFBEventBuffer()) ); | |
144 | ||
145 | return true; | |
146 | } | |
147 | ||
148 | wxTopLevelWindowDFB::~wxTopLevelWindowDFB() | |
149 | { | |
150 | m_isBeingDeleted = true; | |
151 | ||
152 | wxTopLevelWindows.DeleteObject(this); | |
153 | ||
154 | if ( wxTheApp->GetTopWindow() == this ) | |
155 | wxTheApp->SetTopWindow(NULL); | |
156 | ||
157 | if ( wxTopLevelWindows.empty() && wxTheApp->GetExitOnFrameDelete() ) | |
158 | { | |
159 | wxTheApp->ExitMainLoop(); | |
160 | } | |
161 | ||
162 | WX_CLEAR_ARRAY(*m_toPaint); | |
163 | wxDELETE(m_toPaint); | |
164 | ||
165 | // remove the TLW from DFBWindowID->wxTLW map: | |
166 | DFBWindowID winid; | |
167 | if ( DFB_CALL( m_dfbwin->GetID(m_dfbwin, &winid) ) ) | |
168 | gs_dfbWindowsMap.erase(winid); | |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // window size & position | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const | |
176 | { | |
177 | DFB_CALL( m_dfbwin->GetPosition(m_dfbwin, x, y) ); | |
178 | } | |
179 | ||
180 | void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const | |
181 | { | |
182 | DFB_CALL( m_dfbwin->GetSize(m_dfbwin, width, height) ); | |
183 | } | |
184 | ||
185 | void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height) | |
186 | { | |
187 | wxPoint curpos = GetPosition(); | |
188 | if ( curpos.x != x || curpos.y != y ) | |
189 | { | |
190 | DFB_CALL( m_dfbwin->MoveTo(m_dfbwin, x, y) ); | |
191 | } | |
192 | ||
193 | wxSize cursize = GetSize(); | |
194 | if ( cursize.x != width || cursize.y != height ) | |
195 | { | |
196 | DFB_CALL( m_dfbwin->Resize(m_dfbwin, width, height) ); | |
197 | // we must repaint the window after it changed size: | |
198 | Refresh(); | |
199 | } | |
200 | } | |
201 | ||
202 | // ---------------------------------------------------------------------------- | |
203 | // showing and hiding | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
206 | #warning "FIXME: the rest of this file is almost same as for MGL, merge it" | |
207 | bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style) | |
208 | { | |
209 | if (show == m_fsIsShowing) return false; // return what? | |
210 | ||
211 | m_fsIsShowing = show; | |
212 | ||
213 | if (show) | |
214 | { | |
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); | |
219 | ||
220 | if ( style & wxFULLSCREEN_NOCAPTION ) | |
221 | m_windowStyle &= ~wxCAPTION; | |
222 | if ( style & wxFULLSCREEN_NOBORDER ) | |
223 | m_windowStyle = wxSIMPLE_BORDER; | |
224 | ||
225 | int x, y; | |
226 | wxDisplaySize(&x, &y); | |
227 | SetSize(0, 0, x, y); | |
228 | } | |
229 | else | |
230 | { | |
231 | m_windowStyle = m_fsSaveStyle; | |
232 | SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y, | |
233 | m_fsSaveFrame.width, m_fsSaveFrame.height); | |
234 | } | |
235 | ||
236 | return true; | |
237 | } | |
238 | ||
239 | bool wxTopLevelWindowDFB::Show(bool show) | |
240 | { | |
241 | if ( !wxTopLevelWindowBase::Show(show) ) | |
242 | return false; | |
243 | ||
244 | // hide/show the window by setting its opacity to 0/full: | |
245 | DFB_CALL( m_dfbwin->SetOpacity(m_dfbwin, show ? m_opacity : 0) ); | |
246 | ||
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) | |
249 | if ( !m_sizeSet ) | |
250 | { | |
251 | m_sizeSet = true; | |
252 | wxSizeEvent event(GetSize(), GetId()); | |
253 | event.SetEventObject(this); | |
254 | GetEventHandler()->ProcessEvent(event); | |
255 | } | |
256 | ||
257 | // FIXME_DFB: do this at all? | |
258 | if ( show && AcceptsFocus() ) | |
259 | SetFocus(); | |
260 | // FIXME_DFB -- don't do this for popup windows? | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
265 | bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha) | |
266 | { | |
267 | if ( IsShown() ) | |
268 | { | |
269 | if ( !DFB_CALL( m_dfbwin->SetOpacity(m_dfbwin, alpha) ) ) | |
270 | return false; | |
271 | } | |
272 | ||
273 | m_opacity = alpha; | |
274 | return true; | |
275 | } | |
276 | ||
277 | // ---------------------------------------------------------------------------- | |
278 | // maximize, minimize etc. | |
279 | // ---------------------------------------------------------------------------- | |
280 | ||
281 | void wxTopLevelWindowDFB::Maximize(bool maximize) | |
282 | { | |
283 | int x, y, w, h; | |
284 | wxClientDisplayRect(&x, &y, &w, &h); | |
285 | ||
286 | if ( maximize && !m_isMaximized ) | |
287 | { | |
288 | m_isMaximized = true; | |
289 | ||
290 | GetPosition(&m_savedFrame.x, &m_savedFrame.y); | |
291 | GetSize(&m_savedFrame.width, &m_savedFrame.height); | |
292 | ||
293 | SetSize(x, y, w, h); | |
294 | } | |
295 | else if ( !maximize && m_isMaximized ) | |
296 | { | |
297 | m_isMaximized = false; | |
298 | SetSize(m_savedFrame.x, m_savedFrame.y, | |
299 | m_savedFrame.width, m_savedFrame.height); | |
300 | } | |
301 | } | |
302 | ||
303 | bool wxTopLevelWindowDFB::IsMaximized() const | |
304 | { | |
305 | return m_isMaximized; | |
306 | } | |
307 | ||
308 | void wxTopLevelWindowDFB::Restore() | |
309 | { | |
310 | if ( IsMaximized() ) | |
311 | { | |
312 | Maximize(false); | |
313 | } | |
314 | } | |
315 | ||
316 | void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize)) | |
317 | { | |
318 | wxFAIL_MSG(wxT("Iconize not supported under wxDFB")); | |
319 | } | |
320 | ||
321 | bool wxTopLevelWindowDFB::IsIconized() const | |
322 | { | |
323 | return false; | |
324 | } | |
325 | ||
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // surfaces and painting | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
331 | IDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const | |
332 | { | |
333 | IDirectFBSurfacePtr surface; | |
334 | DFB_CALL( m_dfbwin->GetSurface(m_dfbwin, &surface) ); | |
335 | return surface; | |
336 | } | |
337 | ||
338 | void wxTopLevelWindowDFB::HandleQueuedPaintRequests() | |
339 | { | |
340 | wxDfbQueuedPaintRequests& toPaint = *m_toPaint; | |
341 | if ( toPaint.empty() ) | |
342 | return; // nothing to do | |
343 | ||
344 | // process queued paint requests: | |
345 | wxRect winRect(wxPoint(0, 0), GetSize()); | |
346 | wxRect paintedRect; | |
347 | ||
348 | size_t cnt = toPaint.size(); | |
349 | for ( size_t i = 0; i < cnt; ++i ) | |
350 | { | |
351 | const wxDfbPaintRequest& request = *toPaint[i]; | |
352 | ||
353 | wxRect clipped(request.m_rect); | |
354 | ||
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); | |
359 | ||
360 | clipped.Intersect(winRect); | |
361 | if ( clipped.IsEmpty() ) | |
362 | continue; // nothing to refresh | |
363 | ||
364 | PaintWindow(clipped, request.m_eraseBackground); | |
365 | ||
366 | // remember rectangle covering all repainted areas: | |
367 | if ( paintedRect.IsEmpty() ) | |
368 | paintedRect = clipped; | |
369 | else | |
370 | paintedRect.Union(clipped); | |
371 | } | |
372 | ||
373 | WX_CLEAR_ARRAY(toPaint); | |
374 | ||
375 | if ( paintedRect.IsEmpty() ) | |
376 | return; // no painting occurred, no need to flip | |
377 | ||
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; | |
382 | ||
383 | IDirectFBSurfacePtr surface(GetDfbSurface()); | |
384 | DFB_CALL( surface->Flip(surface, rptr, DSFLIP_NONE) ); | |
385 | } | |
386 | ||
387 | void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect, bool eraseBack) | |
388 | { | |
389 | // defer paiting until idle time or until Update() is called: | |
390 | m_toPaint->push_back(new wxDfbPaintRequest(rect, eraseBack)); | |
391 | } | |
392 | ||
393 | void wxTopLevelWindowDFB::Update() | |
394 | { | |
395 | HandleQueuedPaintRequests(); | |
396 | } | |
397 | ||
398 | // --------------------------------------------------------------------------- | |
399 | // events handling | |
400 | // --------------------------------------------------------------------------- | |
401 | ||
402 | /* static */ | |
403 | void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_) | |
404 | { | |
405 | const DFBWindowEvent& event = event_; | |
406 | ||
407 | if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() ) | |
408 | { | |
409 | wxLogTrace(TRACE_EVENTS, | |
410 | _T("received event for unknown DirectFB window, ignoring")); | |
411 | return; | |
412 | } | |
413 | ||
414 | wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id]; | |
415 | wxWindow *recipient = NULL; | |
416 | void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL; | |
417 | ||
418 | switch ( event.type ) | |
419 | { | |
420 | case DWET_KEYDOWN: | |
421 | case DWET_KEYUP: | |
422 | { | |
423 | recipient = wxWindow::FindFocus(); | |
424 | handlerFunc = &wxWindowDFB::HandleKeyEvent; | |
425 | break; | |
426 | } | |
427 | ||
428 | case DWET_NONE: | |
429 | case DWET_ALL: | |
430 | { | |
431 | wxFAIL_MSG( _T("invalid event type") ); | |
432 | break; | |
433 | } | |
434 | } | |
435 | ||
436 | if ( !recipient ) | |
437 | { | |
438 | wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window")); | |
439 | return; | |
440 | } | |
441 | ||
442 | wxCHECK_RET( recipient && recipient->GetTLW() == tlw, | |
443 | _T("event recipient not in TLW which received the event") ); | |
444 | ||
445 | // process the event: | |
446 | (recipient->*handlerFunc)(event_); | |
447 | } | |
448 | ||
449 | // --------------------------------------------------------------------------- | |
450 | // idle events processing | |
451 | // --------------------------------------------------------------------------- | |
452 | ||
453 | void wxTopLevelWindowDFB::OnInternalIdle() | |
454 | { | |
455 | wxTopLevelWindowBase::OnInternalIdle(); | |
456 | HandleQueuedPaintRequests(); | |
457 | } |