]>
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 | { | |
20671963 VS |
44 | wxDfbPaintRequest(const wxRect& rect) : m_rect(rect) {} |
45 | wxDfbPaintRequest(const wxDfbPaintRequest& r) : m_rect(r.m_rect) {} | |
b3c86150 VS |
46 | |
47 | wxRect m_rect; | |
b3c86150 VS |
48 | }; |
49 | ||
14ac4e3a VS |
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 | |
20671963 VS |
59 | void Add(const wxRect& rect) |
60 | { m_queue.push_back(new wxDfbPaintRequest(rect)); } | |
14ac4e3a VS |
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 | }; | |
b3c86150 VS |
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; | |
20671963 | 91 | m_isPainting = false; |
b3c86150 VS |
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: | |
a5b31f4e | 123 | wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer()); |
b3c86150 VS |
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; | |
52c8d32a VS |
135 | m_dfbwin = layer->CreateWindow(&desc); |
136 | if ( !layer ) | |
b3c86150 VS |
137 | return false; |
138 | ||
139 | // add the new TLW to DFBWindowID->wxTLW map: | |
140 | DFBWindowID winid; | |
52c8d32a | 141 | if ( !m_dfbwin->GetID(&winid) ) |
b3c86150 VS |
142 | return false; |
143 | gs_dfbWindowsMap[winid] = this; | |
144 | ||
145 | // TLWs are created initially hidden: | |
52c8d32a | 146 | if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) ) |
b3c86150 VS |
147 | return false; |
148 | ||
20671963 VS |
149 | if ( !wxWindow::Create(NULL, id, pos, size, style, name) ) |
150 | return false; | |
b3c86150 VS |
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 | { | |
52c8d32a | 161 | m_dfbwin->SetStackingClass(DWSC_UPPER); |
b3c86150 VS |
162 | } |
163 | ||
164 | // direct events in this window to the global event buffer: | |
52c8d32a | 165 | m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer()); |
b3c86150 VS |
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 | ||
b3c86150 VS |
184 | wxDELETE(m_toPaint); |
185 | ||
186 | // remove the TLW from DFBWindowID->wxTLW map: | |
187 | DFBWindowID winid; | |
52c8d32a | 188 | if ( m_dfbwin->GetID(&winid) ) |
b3c86150 VS |
189 | gs_dfbWindowsMap.erase(winid); |
190 | } | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // window size & position | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const | |
197 | { | |
52c8d32a | 198 | m_dfbwin->GetPosition(x, y); |
b3c86150 VS |
199 | } |
200 | ||
201 | void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const | |
202 | { | |
52c8d32a | 203 | m_dfbwin->GetSize(width, height); |
b3c86150 VS |
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 | { | |
52c8d32a | 211 | m_dfbwin->MoveTo(x, y); |
b3c86150 VS |
212 | } |
213 | ||
214 | wxSize cursize = GetSize(); | |
215 | if ( cursize.x != width || cursize.y != height ) | |
216 | { | |
7807a2b4 VS |
217 | // changing window's size changes its surface: |
218 | InvalidateDfbSurface(); | |
219 | ||
52c8d32a | 220 | m_dfbwin->Resize(width, height); |
7807a2b4 | 221 | |
b3c86150 | 222 | // we must repaint the window after it changed size: |
20671963 VS |
223 | if ( IsShown() ) |
224 | DoRefreshWindow(); | |
b3c86150 VS |
225 | } |
226 | } | |
227 | ||
228 | // ---------------------------------------------------------------------------- | |
229 | // showing and hiding | |
230 | // ---------------------------------------------------------------------------- | |
231 | ||
232 | #warning "FIXME: the rest of this file is almost same as for MGL, merge it" | |
233 | bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style) | |
234 | { | |
235 | if (show == m_fsIsShowing) return false; // return what? | |
236 | ||
237 | m_fsIsShowing = show; | |
238 | ||
239 | if (show) | |
240 | { | |
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); | |
245 | ||
246 | if ( style & wxFULLSCREEN_NOCAPTION ) | |
247 | m_windowStyle &= ~wxCAPTION; | |
248 | if ( style & wxFULLSCREEN_NOBORDER ) | |
249 | m_windowStyle = wxSIMPLE_BORDER; | |
250 | ||
251 | int x, y; | |
252 | wxDisplaySize(&x, &y); | |
253 | SetSize(0, 0, x, y); | |
254 | } | |
255 | else | |
256 | { | |
257 | m_windowStyle = m_fsSaveStyle; | |
258 | SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y, | |
259 | m_fsSaveFrame.width, m_fsSaveFrame.height); | |
260 | } | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
265 | bool wxTopLevelWindowDFB::Show(bool show) | |
266 | { | |
267 | if ( !wxTopLevelWindowBase::Show(show) ) | |
268 | return false; | |
269 | ||
270 | // hide/show the window by setting its opacity to 0/full: | |
52c8d32a | 271 | m_dfbwin->SetOpacity(show ? m_opacity : 0); |
b3c86150 VS |
272 | |
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) | |
275 | if ( !m_sizeSet ) | |
276 | { | |
277 | m_sizeSet = true; | |
278 | wxSizeEvent event(GetSize(), GetId()); | |
279 | event.SetEventObject(this); | |
280 | GetEventHandler()->ProcessEvent(event); | |
281 | } | |
282 | ||
4ff28c37 VS |
283 | if ( show ) |
284 | { | |
285 | wxWindow *focused = wxWindow::FindFocus(); | |
286 | if ( focused && focused->GetTLW() == this ) | |
287 | { | |
288 | SetDfbFocus(); | |
289 | } | |
290 | else if ( AcceptsFocus() ) | |
291 | { | |
292 | // FIXME: we should probably always call SetDfbFocus instead | |
293 | // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS | |
294 | // handler | |
295 | SetFocus(); | |
296 | } | |
297 | } | |
b3c86150 VS |
298 | |
299 | return true; | |
300 | } | |
301 | ||
302 | bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha) | |
303 | { | |
304 | if ( IsShown() ) | |
305 | { | |
52c8d32a | 306 | if ( !m_dfbwin->SetOpacity(alpha) ) |
b3c86150 VS |
307 | return false; |
308 | } | |
309 | ||
310 | m_opacity = alpha; | |
311 | return true; | |
312 | } | |
313 | ||
314 | // ---------------------------------------------------------------------------- | |
315 | // maximize, minimize etc. | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | void wxTopLevelWindowDFB::Maximize(bool maximize) | |
319 | { | |
320 | int x, y, w, h; | |
321 | wxClientDisplayRect(&x, &y, &w, &h); | |
322 | ||
323 | if ( maximize && !m_isMaximized ) | |
324 | { | |
325 | m_isMaximized = true; | |
326 | ||
327 | GetPosition(&m_savedFrame.x, &m_savedFrame.y); | |
328 | GetSize(&m_savedFrame.width, &m_savedFrame.height); | |
329 | ||
330 | SetSize(x, y, w, h); | |
331 | } | |
332 | else if ( !maximize && m_isMaximized ) | |
333 | { | |
334 | m_isMaximized = false; | |
335 | SetSize(m_savedFrame.x, m_savedFrame.y, | |
336 | m_savedFrame.width, m_savedFrame.height); | |
337 | } | |
338 | } | |
339 | ||
340 | bool wxTopLevelWindowDFB::IsMaximized() const | |
341 | { | |
342 | return m_isMaximized; | |
343 | } | |
344 | ||
345 | void wxTopLevelWindowDFB::Restore() | |
346 | { | |
347 | if ( IsMaximized() ) | |
348 | { | |
349 | Maximize(false); | |
350 | } | |
351 | } | |
352 | ||
353 | void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize)) | |
354 | { | |
355 | wxFAIL_MSG(wxT("Iconize not supported under wxDFB")); | |
356 | } | |
357 | ||
358 | bool wxTopLevelWindowDFB::IsIconized() const | |
359 | { | |
360 | return false; | |
361 | } | |
362 | ||
363 | ||
364 | // ---------------------------------------------------------------------------- | |
365 | // surfaces and painting | |
366 | // ---------------------------------------------------------------------------- | |
367 | ||
52c8d32a | 368 | wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const |
b3c86150 | 369 | { |
52c8d32a | 370 | return m_dfbwin->GetSurface(); |
b3c86150 VS |
371 | } |
372 | ||
373 | void wxTopLevelWindowDFB::HandleQueuedPaintRequests() | |
374 | { | |
14ac4e3a | 375 | if ( m_toPaint->IsEmpty() ) |
b3c86150 VS |
376 | return; // nothing to do |
377 | ||
14ac4e3a VS |
378 | if ( IsFrozen() || !IsShown() ) |
379 | { | |
380 | // nothing to do if the window is frozen or hidden; clear the queue | |
381 | // and return (note that it's OK to clear the queue even if the window | |
382 | // is frozen, because Thaw() calls Refresh()): | |
383 | m_toPaint->Clear(); | |
384 | return; | |
385 | } | |
386 | ||
387 | const wxDfbQueuedPaintRequestsList& requests = m_toPaint->GetRequests(); | |
388 | ||
b3c86150 VS |
389 | // process queued paint requests: |
390 | wxRect winRect(wxPoint(0, 0), GetSize()); | |
391 | wxRect paintedRect; | |
392 | ||
20671963 VS |
393 | // important note: all DCs created from now until m_isPainting is reset to |
394 | // false will not update the front buffer as this flag indicates that we'll | |
395 | // blit the entire back buffer to front soon | |
396 | m_isPainting = true; | |
397 | ||
14ac4e3a | 398 | size_t cnt = requests.size(); |
20671963 VS |
399 | wxLogTrace(TRACE_PAINT, _T("%p ('%s'): processing %i paint requests"), |
400 | this, GetName().c_str(), cnt); | |
401 | ||
b3c86150 VS |
402 | for ( size_t i = 0; i < cnt; ++i ) |
403 | { | |
14ac4e3a | 404 | const wxDfbPaintRequest& request = *requests[i]; |
b3c86150 VS |
405 | |
406 | wxRect clipped(request.m_rect); | |
b3c86150 VS |
407 | clipped.Intersect(winRect); |
408 | if ( clipped.IsEmpty() ) | |
409 | continue; // nothing to refresh | |
410 | ||
20671963 VS |
411 | wxLogTrace(TRACE_PAINT, |
412 | _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"), | |
413 | this, GetName().c_str(), | |
414 | clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom()); | |
415 | ||
416 | PaintWindow(clipped); | |
b3c86150 VS |
417 | |
418 | // remember rectangle covering all repainted areas: | |
419 | if ( paintedRect.IsEmpty() ) | |
420 | paintedRect = clipped; | |
421 | else | |
422 | paintedRect.Union(clipped); | |
423 | } | |
424 | ||
20671963 VS |
425 | m_isPainting = false; |
426 | ||
14ac4e3a | 427 | m_toPaint->Clear(); |
b3c86150 VS |
428 | |
429 | if ( paintedRect.IsEmpty() ) | |
430 | return; // no painting occurred, no need to flip | |
431 | ||
20671963 VS |
432 | // Flip the surface to make the changes visible. Note that the rectangle we |
433 | // flip is *superset* of the union of repainted rectangles (created as | |
434 | // "rectangles union" by wxRect::Union) and so some parts of the back | |
435 | // buffer that we didn't touch in this HandleQueuedPaintRequests call will | |
436 | // be copied to the front buffer as well. This is safe/correct thing to do | |
437 | // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so | |
438 | // the back and front buffers contain the same data. | |
439 | // | |
440 | // Note that we do _not_ split m_toPaint into disjoint rectangles and | |
441 | // do FlipToFront() for each of them, because that could result in visible | |
442 | // updating of the screen; instead, we prefer to flip everything at once. | |
443 | ||
b3c86150 VS |
444 | DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(), |
445 | paintedRect.GetRight(), paintedRect.GetBottom()}; | |
446 | DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r; | |
447 | ||
20671963 VS |
448 | GetDfbSurface()->FlipToFront(rptr); |
449 | ||
450 | wxLogTrace(TRACE_PAINT, | |
451 | _T("%p ('%s'): flipped surface: [%i,%i,%i,%i]"), | |
452 | this, GetName().c_str(), | |
453 | paintedRect.x, paintedRect.y, | |
454 | paintedRect.GetRight(), paintedRect.GetBottom()); | |
b3c86150 VS |
455 | } |
456 | ||
20671963 | 457 | void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect) |
b3c86150 | 458 | { |
bb78b682 VS |
459 | // don't overlap outside of the window (NB: 'rect' is in window coords): |
460 | wxRect r(rect); | |
461 | r.Intersect(wxRect(GetSize())); | |
462 | if ( r.IsEmpty() ) | |
463 | return; | |
20671963 VS |
464 | |
465 | wxLogTrace(TRACE_PAINT, | |
466 | _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"), | |
467 | this, GetName().c_str(), | |
468 | rect.x, rect.y, rect.GetRight(), rect.GetBottom()); | |
469 | ||
14ac4e3a | 470 | // defer painting until idle time or until Update() is called: |
20671963 | 471 | m_toPaint->Add(rect); |
b3c86150 VS |
472 | } |
473 | ||
474 | void wxTopLevelWindowDFB::Update() | |
475 | { | |
476 | HandleQueuedPaintRequests(); | |
477 | } | |
478 | ||
479 | // --------------------------------------------------------------------------- | |
480 | // events handling | |
481 | // --------------------------------------------------------------------------- | |
482 | ||
4ff28c37 VS |
483 | void wxTopLevelWindowDFB::SetDfbFocus() |
484 | { | |
485 | wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") ); | |
486 | wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this, | |
487 | _T("setting DirectFB focus to unexpected window") ); | |
488 | ||
489 | GetDirectFBWindow()->RequestFocus(); | |
490 | } | |
491 | ||
b3c86150 VS |
492 | /* static */ |
493 | void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_) | |
494 | { | |
495 | const DFBWindowEvent& event = event_; | |
496 | ||
497 | if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() ) | |
498 | { | |
499 | wxLogTrace(TRACE_EVENTS, | |
500 | _T("received event for unknown DirectFB window, ignoring")); | |
501 | return; | |
502 | } | |
503 | ||
504 | wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id]; | |
505 | wxWindow *recipient = NULL; | |
506 | void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL; | |
507 | ||
508 | switch ( event.type ) | |
509 | { | |
510 | case DWET_KEYDOWN: | |
511 | case DWET_KEYUP: | |
512 | { | |
513 | recipient = wxWindow::FindFocus(); | |
514 | handlerFunc = &wxWindowDFB::HandleKeyEvent; | |
515 | break; | |
516 | } | |
517 | ||
518 | case DWET_NONE: | |
519 | case DWET_ALL: | |
520 | { | |
521 | wxFAIL_MSG( _T("invalid event type") ); | |
522 | break; | |
523 | } | |
524 | } | |
525 | ||
526 | if ( !recipient ) | |
527 | { | |
528 | wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window")); | |
529 | return; | |
530 | } | |
531 | ||
532 | wxCHECK_RET( recipient && recipient->GetTLW() == tlw, | |
533 | _T("event recipient not in TLW which received the event") ); | |
534 | ||
535 | // process the event: | |
536 | (recipient->*handlerFunc)(event_); | |
537 | } | |
538 | ||
539 | // --------------------------------------------------------------------------- | |
540 | // idle events processing | |
541 | // --------------------------------------------------------------------------- | |
542 | ||
543 | void wxTopLevelWindowDFB::OnInternalIdle() | |
544 | { | |
545 | wxTopLevelWindowBase::OnInternalIdle(); | |
546 | HandleQueuedPaintRequests(); | |
547 | } |