]>
Commit | Line | Data |
---|---|---|
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) : m_rect(rect) {} | |
45 | wxDfbPaintRequest(const wxDfbPaintRequest& r) : m_rect(r.m_rect) {} | |
46 | ||
47 | wxRect m_rect; | |
48 | }; | |
49 | ||
50 | WX_DEFINE_ARRAY_PTR(wxDfbPaintRequest*, wxDfbQueuedPaintRequestsList); | |
51 | ||
52 | // Queue of paint requests | |
53 | class wxDfbQueuedPaintRequests | |
54 | { | |
55 | public: | |
56 | ~wxDfbQueuedPaintRequests() { Clear(); } | |
57 | ||
58 | // Adds paint request to the queue | |
59 | void Add(const wxRect& rect) | |
60 | { m_queue.push_back(new wxDfbPaintRequest(rect)); } | |
61 | ||
62 | // Is the queue empty? | |
63 | bool IsEmpty() const { return m_queue.empty(); } | |
64 | ||
65 | // Empties the queue | |
66 | void Clear() { WX_CLEAR_ARRAY(m_queue); } | |
67 | ||
68 | // Gets requests in the queue | |
69 | const wxDfbQueuedPaintRequestsList& GetRequests() const { return m_queue; } | |
70 | ||
71 | private: | |
72 | wxDfbQueuedPaintRequestsList m_queue; | |
73 | }; | |
74 | ||
75 | // ============================================================================ | |
76 | // wxTopLevelWindowDFB | |
77 | // ============================================================================ | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // creation & destruction | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | void wxTopLevelWindowDFB::Init() | |
84 | { | |
85 | m_isShown = false; | |
86 | m_isMaximized = false; | |
87 | m_fsIsShowing = false; | |
88 | m_sizeSet = false; | |
89 | m_opacity = 255; | |
90 | m_toPaint = new wxDfbQueuedPaintRequests; | |
91 | m_isPainting = false; | |
92 | } | |
93 | ||
94 | bool wxTopLevelWindowDFB::Create(wxWindow *parent, | |
95 | wxWindowID id, | |
96 | const wxString& title, | |
97 | const wxPoint& posOrig, | |
98 | const wxSize& sizeOrig, | |
99 | long style, | |
100 | const wxString &name) | |
101 | { | |
102 | m_tlw = this; | |
103 | ||
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 ) | |
108 | { | |
109 | wxSize sizeDefault = GetDefaultSize(); | |
110 | if ( size.x == wxDefaultCoord ) | |
111 | size.x = sizeDefault.x; | |
112 | if ( size.y == wxDefaultCoord ) | |
113 | size.y = sizeDefault.y; | |
114 | } | |
115 | ||
116 | wxPoint pos(posOrig); | |
117 | if ( pos.x == wxDefaultCoord ) | |
118 | pos.x = 0; | |
119 | if ( pos.y == wxDefaultCoord ) | |
120 | pos.y = 0; | |
121 | ||
122 | // create DirectFB window: | |
123 | wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer()); | |
124 | wxCHECK_MSG( layer, false, _T("no display layer") ); | |
125 | ||
126 | DFBWindowDescription desc; | |
127 | desc.flags = (DFBWindowDescriptionFlags) | |
128 | (DWDESC_CAPS | | |
129 | DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY); | |
130 | desc.caps = DWCAPS_DOUBLEBUFFER; | |
131 | desc.posx = pos.x; | |
132 | desc.posy = pos.y; | |
133 | desc.width = size.x; | |
134 | desc.height = size.y; | |
135 | m_dfbwin = layer->CreateWindow(&desc); | |
136 | if ( !layer ) | |
137 | return false; | |
138 | ||
139 | // add the new TLW to DFBWindowID->wxTLW map: | |
140 | DFBWindowID winid; | |
141 | if ( !m_dfbwin->GetID(&winid) ) | |
142 | return false; | |
143 | gs_dfbWindowsMap[winid] = this; | |
144 | ||
145 | // TLWs are created initially hidden: | |
146 | if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) ) | |
147 | return false; | |
148 | ||
149 | if ( !wxWindow::Create(NULL, id, pos, size, style, name) ) | |
150 | return false; | |
151 | ||
152 | SetParent(parent); | |
153 | if ( parent ) | |
154 | parent->AddChild(this); | |
155 | ||
156 | wxTopLevelWindows.Append(this); | |
157 | m_title = title; | |
158 | ||
159 | if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) ) | |
160 | { | |
161 | m_dfbwin->SetStackingClass(DWSC_UPPER); | |
162 | } | |
163 | ||
164 | // direct events in this window to the global event buffer: | |
165 | m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer()); | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | wxTopLevelWindowDFB::~wxTopLevelWindowDFB() | |
171 | { | |
172 | m_isBeingDeleted = true; | |
173 | ||
174 | wxTopLevelWindows.DeleteObject(this); | |
175 | ||
176 | if ( wxTheApp->GetTopWindow() == this ) | |
177 | wxTheApp->SetTopWindow(NULL); | |
178 | ||
179 | if ( wxTopLevelWindows.empty() && wxTheApp->GetExitOnFrameDelete() ) | |
180 | { | |
181 | wxTheApp->ExitMainLoop(); | |
182 | } | |
183 | ||
184 | wxDELETE(m_toPaint); | |
185 | ||
186 | // remove the TLW from DFBWindowID->wxTLW map: | |
187 | DFBWindowID winid; | |
188 | if ( m_dfbwin->GetID(&winid) ) | |
189 | gs_dfbWindowsMap.erase(winid); | |
190 | } | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // window size & position | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const | |
197 | { | |
198 | m_dfbwin->GetPosition(x, y); | |
199 | } | |
200 | ||
201 | void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const | |
202 | { | |
203 | m_dfbwin->GetSize(width, height); | |
204 | } | |
205 | ||
206 | void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height) | |
207 | { | |
208 | wxPoint curpos = GetPosition(); | |
209 | if ( curpos.x != x || curpos.y != y ) | |
210 | { | |
211 | m_dfbwin->MoveTo(x, y); | |
212 | } | |
213 | ||
214 | wxSize cursize = GetSize(); | |
215 | if ( cursize.x != width || cursize.y != height ) | |
216 | { | |
217 | m_dfbwin->Resize(width, height); | |
218 | // we must repaint the window after it changed size: | |
219 | if ( IsShown() ) | |
220 | DoRefreshWindow(); | |
221 | } | |
222 | } | |
223 | ||
224 | // ---------------------------------------------------------------------------- | |
225 | // showing and hiding | |
226 | // ---------------------------------------------------------------------------- | |
227 | ||
228 | #warning "FIXME: the rest of this file is almost same as for MGL, merge it" | |
229 | bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style) | |
230 | { | |
231 | if (show == m_fsIsShowing) return false; // return what? | |
232 | ||
233 | m_fsIsShowing = show; | |
234 | ||
235 | if (show) | |
236 | { | |
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); | |
241 | ||
242 | if ( style & wxFULLSCREEN_NOCAPTION ) | |
243 | m_windowStyle &= ~wxCAPTION; | |
244 | if ( style & wxFULLSCREEN_NOBORDER ) | |
245 | m_windowStyle = wxSIMPLE_BORDER; | |
246 | ||
247 | int x, y; | |
248 | wxDisplaySize(&x, &y); | |
249 | SetSize(0, 0, x, y); | |
250 | } | |
251 | else | |
252 | { | |
253 | m_windowStyle = m_fsSaveStyle; | |
254 | SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y, | |
255 | m_fsSaveFrame.width, m_fsSaveFrame.height); | |
256 | } | |
257 | ||
258 | return true; | |
259 | } | |
260 | ||
261 | bool wxTopLevelWindowDFB::Show(bool show) | |
262 | { | |
263 | if ( !wxTopLevelWindowBase::Show(show) ) | |
264 | return false; | |
265 | ||
266 | // hide/show the window by setting its opacity to 0/full: | |
267 | m_dfbwin->SetOpacity(show ? m_opacity : 0); | |
268 | ||
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) | |
271 | if ( !m_sizeSet ) | |
272 | { | |
273 | m_sizeSet = true; | |
274 | wxSizeEvent event(GetSize(), GetId()); | |
275 | event.SetEventObject(this); | |
276 | GetEventHandler()->ProcessEvent(event); | |
277 | } | |
278 | ||
279 | // FIXME_DFB: do this at all? | |
280 | if ( show && AcceptsFocus() ) | |
281 | SetFocus(); | |
282 | // FIXME_DFB -- don't do this for popup windows? | |
283 | ||
284 | return true; | |
285 | } | |
286 | ||
287 | bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha) | |
288 | { | |
289 | if ( IsShown() ) | |
290 | { | |
291 | if ( !m_dfbwin->SetOpacity(alpha) ) | |
292 | return false; | |
293 | } | |
294 | ||
295 | m_opacity = alpha; | |
296 | return true; | |
297 | } | |
298 | ||
299 | // ---------------------------------------------------------------------------- | |
300 | // maximize, minimize etc. | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
303 | void wxTopLevelWindowDFB::Maximize(bool maximize) | |
304 | { | |
305 | int x, y, w, h; | |
306 | wxClientDisplayRect(&x, &y, &w, &h); | |
307 | ||
308 | if ( maximize && !m_isMaximized ) | |
309 | { | |
310 | m_isMaximized = true; | |
311 | ||
312 | GetPosition(&m_savedFrame.x, &m_savedFrame.y); | |
313 | GetSize(&m_savedFrame.width, &m_savedFrame.height); | |
314 | ||
315 | SetSize(x, y, w, h); | |
316 | } | |
317 | else if ( !maximize && m_isMaximized ) | |
318 | { | |
319 | m_isMaximized = false; | |
320 | SetSize(m_savedFrame.x, m_savedFrame.y, | |
321 | m_savedFrame.width, m_savedFrame.height); | |
322 | } | |
323 | } | |
324 | ||
325 | bool wxTopLevelWindowDFB::IsMaximized() const | |
326 | { | |
327 | return m_isMaximized; | |
328 | } | |
329 | ||
330 | void wxTopLevelWindowDFB::Restore() | |
331 | { | |
332 | if ( IsMaximized() ) | |
333 | { | |
334 | Maximize(false); | |
335 | } | |
336 | } | |
337 | ||
338 | void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize)) | |
339 | { | |
340 | wxFAIL_MSG(wxT("Iconize not supported under wxDFB")); | |
341 | } | |
342 | ||
343 | bool wxTopLevelWindowDFB::IsIconized() const | |
344 | { | |
345 | return false; | |
346 | } | |
347 | ||
348 | ||
349 | // ---------------------------------------------------------------------------- | |
350 | // surfaces and painting | |
351 | // ---------------------------------------------------------------------------- | |
352 | ||
353 | wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const | |
354 | { | |
355 | return m_dfbwin->GetSurface(); | |
356 | } | |
357 | ||
358 | void wxTopLevelWindowDFB::HandleQueuedPaintRequests() | |
359 | { | |
360 | if ( m_toPaint->IsEmpty() ) | |
361 | return; // nothing to do | |
362 | ||
363 | if ( IsFrozen() || !IsShown() ) | |
364 | { | |
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()): | |
368 | m_toPaint->Clear(); | |
369 | return; | |
370 | } | |
371 | ||
372 | const wxDfbQueuedPaintRequestsList& requests = m_toPaint->GetRequests(); | |
373 | ||
374 | // process queued paint requests: | |
375 | wxRect winRect(wxPoint(0, 0), GetSize()); | |
376 | wxRect paintedRect; | |
377 | ||
378 | // important note: all DCs created from now until m_isPainting is reset to | |
379 | // false will not update the front buffer as this flag indicates that we'll | |
380 | // blit the entire back buffer to front soon | |
381 | m_isPainting = true; | |
382 | ||
383 | size_t cnt = requests.size(); | |
384 | wxLogTrace(TRACE_PAINT, _T("%p ('%s'): processing %i paint requests"), | |
385 | this, GetName().c_str(), cnt); | |
386 | ||
387 | for ( size_t i = 0; i < cnt; ++i ) | |
388 | { | |
389 | const wxDfbPaintRequest& request = *requests[i]; | |
390 | ||
391 | wxRect clipped(request.m_rect); | |
392 | clipped.Intersect(winRect); | |
393 | if ( clipped.IsEmpty() ) | |
394 | continue; // nothing to refresh | |
395 | ||
396 | wxLogTrace(TRACE_PAINT, | |
397 | _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"), | |
398 | this, GetName().c_str(), | |
399 | clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom()); | |
400 | ||
401 | PaintWindow(clipped); | |
402 | ||
403 | // remember rectangle covering all repainted areas: | |
404 | if ( paintedRect.IsEmpty() ) | |
405 | paintedRect = clipped; | |
406 | else | |
407 | paintedRect.Union(clipped); | |
408 | } | |
409 | ||
410 | m_isPainting = false; | |
411 | ||
412 | m_toPaint->Clear(); | |
413 | ||
414 | if ( paintedRect.IsEmpty() ) | |
415 | return; // no painting occurred, no need to flip | |
416 | ||
417 | // Flip the surface to make the changes visible. Note that the rectangle we | |
418 | // flip is *superset* of the union of repainted rectangles (created as | |
419 | // "rectangles union" by wxRect::Union) and so some parts of the back | |
420 | // buffer that we didn't touch in this HandleQueuedPaintRequests call will | |
421 | // be copied to the front buffer as well. This is safe/correct thing to do | |
422 | // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so | |
423 | // the back and front buffers contain the same data. | |
424 | // | |
425 | // Note that we do _not_ split m_toPaint into disjoint rectangles and | |
426 | // do FlipToFront() for each of them, because that could result in visible | |
427 | // updating of the screen; instead, we prefer to flip everything at once. | |
428 | ||
429 | DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(), | |
430 | paintedRect.GetRight(), paintedRect.GetBottom()}; | |
431 | DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r; | |
432 | ||
433 | GetDfbSurface()->FlipToFront(rptr); | |
434 | ||
435 | wxLogTrace(TRACE_PAINT, | |
436 | _T("%p ('%s'): flipped surface: [%i,%i,%i,%i]"), | |
437 | this, GetName().c_str(), | |
438 | paintedRect.x, paintedRect.y, | |
439 | paintedRect.GetRight(), paintedRect.GetBottom()); | |
440 | } | |
441 | ||
442 | void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect) | |
443 | { | |
444 | // don't overlap outside of the window (NB: 'rect' is in window coords): | |
445 | wxRect r(rect); | |
446 | r.Intersect(wxRect(GetSize())); | |
447 | if ( r.IsEmpty() ) | |
448 | return; | |
449 | ||
450 | wxLogTrace(TRACE_PAINT, | |
451 | _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"), | |
452 | this, GetName().c_str(), | |
453 | rect.x, rect.y, rect.GetRight(), rect.GetBottom()); | |
454 | ||
455 | // defer painting until idle time or until Update() is called: | |
456 | m_toPaint->Add(rect); | |
457 | } | |
458 | ||
459 | void wxTopLevelWindowDFB::Update() | |
460 | { | |
461 | HandleQueuedPaintRequests(); | |
462 | } | |
463 | ||
464 | // --------------------------------------------------------------------------- | |
465 | // events handling | |
466 | // --------------------------------------------------------------------------- | |
467 | ||
468 | /* static */ | |
469 | void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_) | |
470 | { | |
471 | const DFBWindowEvent& event = event_; | |
472 | ||
473 | if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() ) | |
474 | { | |
475 | wxLogTrace(TRACE_EVENTS, | |
476 | _T("received event for unknown DirectFB window, ignoring")); | |
477 | return; | |
478 | } | |
479 | ||
480 | wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id]; | |
481 | wxWindow *recipient = NULL; | |
482 | void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL; | |
483 | ||
484 | switch ( event.type ) | |
485 | { | |
486 | case DWET_KEYDOWN: | |
487 | case DWET_KEYUP: | |
488 | { | |
489 | recipient = wxWindow::FindFocus(); | |
490 | handlerFunc = &wxWindowDFB::HandleKeyEvent; | |
491 | break; | |
492 | } | |
493 | ||
494 | case DWET_NONE: | |
495 | case DWET_ALL: | |
496 | { | |
497 | wxFAIL_MSG( _T("invalid event type") ); | |
498 | break; | |
499 | } | |
500 | } | |
501 | ||
502 | if ( !recipient ) | |
503 | { | |
504 | wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window")); | |
505 | return; | |
506 | } | |
507 | ||
508 | wxCHECK_RET( recipient && recipient->GetTLW() == tlw, | |
509 | _T("event recipient not in TLW which received the event") ); | |
510 | ||
511 | // process the event: | |
512 | (recipient->*handlerFunc)(event_); | |
513 | } | |
514 | ||
515 | // --------------------------------------------------------------------------- | |
516 | // idle events processing | |
517 | // --------------------------------------------------------------------------- | |
518 | ||
519 | void wxTopLevelWindowDFB::OnInternalIdle() | |
520 | { | |
521 | wxTopLevelWindowBase::OnInternalIdle(); | |
522 | HandleQueuedPaintRequests(); | |
523 | } |