]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/toplevel.cpp
Compilation fix for really old gcc versions.
[wxWidgets.git] / src / dfb / toplevel.cpp
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 #endif // WX_PRECOMP
19
20 #include "wx/hashmap.h"
21 #include "wx/evtloop.h"
22 #include "wx/dfb/private.h"
23
24 #define TRACE_EVENTS _T("events")
25 #define TRACE_PAINT _T("paint")
26
27 // ============================================================================
28 // globals
29 // ============================================================================
30
31 // mapping of DirectFB windows to wxTLWs:
32 WX_DECLARE_HASH_MAP(DFBWindowID, wxTopLevelWindowDFB*,
33 wxIntegerHash, wxIntegerEqual,
34 wxDfbWindowsMap);
35 static wxDfbWindowsMap gs_dfbWindowsMap;
36
37 // ============================================================================
38 // helpers
39 // ============================================================================
40
41 // Queue of paint requests
42 class wxDfbQueuedPaintRequests
43 {
44 public:
45 ~wxDfbQueuedPaintRequests() { Clear(); }
46
47 // Adds paint request to the queue
48 void Add(const wxRect& rect)
49 {
50 // We use a simple implementation here for now: all refresh requests
51 // are merged together into single rectangle that is superset of
52 // all the requested rectangles. This wastes some blitting and painting
53 // time, but OTOH, EVT_PAINT handler is called only once per window.
54 m_invalidated.Union(rect);
55 }
56
57 // Is the queue empty?
58 bool IsEmpty() const { return m_invalidated.IsEmpty(); }
59
60 // Empties the queue
61 void Clear() { m_invalidated = wxRect(); }
62
63 // Gets the next request in the queue, returns true if there was one,
64 // false if the queue was empty
65 bool GetNext(wxRect& rect)
66 {
67 if ( m_invalidated.IsEmpty() )
68 return false;
69
70 rect = m_invalidated;
71 Clear(); // there's only one item in the queue
72 return true;
73 }
74
75 private:
76 // currently invalidated region
77 wxRect m_invalidated;
78 };
79
80 // ============================================================================
81 // wxTopLevelWindowDFB
82 // ============================================================================
83
84 // ----------------------------------------------------------------------------
85 // creation & destruction
86 // ----------------------------------------------------------------------------
87
88 void wxTopLevelWindowDFB::Init()
89 {
90 m_isShown = false;
91 m_isMaximized = false;
92 m_fsIsShowing = false;
93 m_sizeSet = false;
94 m_opacity = 255;
95 m_toPaint = new wxDfbQueuedPaintRequests;
96 m_isPainting = false;
97 }
98
99 bool wxTopLevelWindowDFB::Create(wxWindow *parent,
100 wxWindowID id,
101 const wxString& title,
102 const wxPoint& posOrig,
103 const wxSize& sizeOrig,
104 long style,
105 const wxString &name)
106 {
107 m_tlw = this;
108
109 // always create a frame of some reasonable, even if arbitrary, size (at
110 // least for MSW compatibility)
111 wxSize size(sizeOrig);
112 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord )
113 {
114 wxSize sizeDefault = GetDefaultSize();
115 if ( size.x == wxDefaultCoord )
116 size.x = sizeDefault.x;
117 if ( size.y == wxDefaultCoord )
118 size.y = sizeDefault.y;
119 }
120
121 wxPoint pos(posOrig);
122 if ( pos.x == wxDefaultCoord )
123 pos.x = 0;
124 if ( pos.y == wxDefaultCoord )
125 pos.y = 0;
126
127 // create DirectFB window:
128 wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer());
129 wxCHECK_MSG( layer, false, _T("no display layer") );
130
131 DFBWindowDescription desc;
132 desc.flags = (DFBWindowDescriptionFlags)
133 (DWDESC_CAPS |
134 DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_POSX | DWDESC_POSY);
135 desc.caps = DWCAPS_DOUBLEBUFFER;
136 desc.posx = pos.x;
137 desc.posy = pos.y;
138 desc.width = size.x;
139 desc.height = size.y;
140 m_dfbwin = layer->CreateWindow(&desc);
141 if ( !m_dfbwin )
142 return false;
143
144 // add the new TLW to DFBWindowID->wxTLW map:
145 DFBWindowID winid;
146 if ( !m_dfbwin->GetID(&winid) )
147 return false;
148 gs_dfbWindowsMap[winid] = this;
149
150 // TLWs are created initially hidden:
151 if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) )
152 return false;
153
154 if ( !wxWindow::Create(NULL, id, pos, size, style, name) )
155 return false;
156
157 SetParent(parent);
158 if ( parent )
159 parent->AddChild(this);
160
161 wxTopLevelWindows.Append(this);
162 m_title = title;
163
164 if ( style & (wxSTAY_ON_TOP | wxPOPUP_WINDOW) )
165 {
166 m_dfbwin->SetStackingClass(DWSC_UPPER);
167 }
168
169 // direct events in this window to the global event buffer:
170 m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
171
172 return true;
173 }
174
175 wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
176 {
177 m_isBeingDeleted = true;
178
179 // destroy all children before we destroy the underlying DirectFB window,
180 // so that if any of them does something with the TLW, it will still work:
181 DestroyChildren();
182
183 // it's safe to delete the underlying DirectFB window now:
184 wxDELETE(m_toPaint);
185
186 if ( !m_dfbwin )
187 return;
188
189 // remove the TLW from DFBWindowID->wxTLW map:
190 DFBWindowID winid;
191 if ( m_dfbwin->GetID(&winid) )
192 gs_dfbWindowsMap.erase(winid);
193
194 m_dfbwin->Destroy();
195 m_dfbwin.Reset();
196 }
197
198 // ----------------------------------------------------------------------------
199 // window size & position
200 // ----------------------------------------------------------------------------
201
202 void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const
203 {
204 m_dfbwin->GetPosition(x, y);
205 }
206
207 void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const
208 {
209 m_dfbwin->GetSize(width, height);
210 }
211
212 void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height)
213 {
214 wxPoint curpos = GetPosition();
215 if ( curpos.x != x || curpos.y != y )
216 {
217 m_dfbwin->MoveTo(x, y);
218 }
219
220 wxSize cursize = GetSize();
221 if ( cursize.x != width || cursize.y != height )
222 {
223 // changing window's size changes its surface:
224 InvalidateDfbSurface();
225
226 m_dfbwin->Resize(width, height);
227
228 // we must repaint the window after it changed size:
229 if ( IsShown() )
230 DoRefreshWindow();
231 }
232 }
233
234 // ----------------------------------------------------------------------------
235 // showing and hiding
236 // ----------------------------------------------------------------------------
237
238 #warning "FIXME: the rest of this file is almost same as for MGL, merge it"
239 bool wxTopLevelWindowDFB::ShowFullScreen(bool show, long style)
240 {
241 if (show == m_fsIsShowing) return false; // return what?
242
243 m_fsIsShowing = show;
244
245 if (show)
246 {
247 m_fsSaveStyle = m_windowStyle;
248 m_fsSaveFlag = style;
249 GetPosition(&m_fsSaveFrame.x, &m_fsSaveFrame.y);
250 GetSize(&m_fsSaveFrame.width, &m_fsSaveFrame.height);
251
252 if ( style & wxFULLSCREEN_NOCAPTION )
253 m_windowStyle &= ~wxCAPTION;
254 if ( style & wxFULLSCREEN_NOBORDER )
255 m_windowStyle = wxSIMPLE_BORDER;
256
257 int x, y;
258 wxDisplaySize(&x, &y);
259 SetSize(0, 0, x, y);
260 }
261 else
262 {
263 m_windowStyle = m_fsSaveStyle;
264 SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y,
265 m_fsSaveFrame.width, m_fsSaveFrame.height);
266 }
267
268 return true;
269 }
270
271 bool wxTopLevelWindowDFB::Show(bool show)
272 {
273 // NB: this calls wxWindow::Show() and so ensures DoRefreshWindow() is
274 // called on the window -- we'll need that below
275 if ( !wxTopLevelWindowBase::Show(show) )
276 return false;
277
278 // If this is the first time Show was called, send size event,
279 // so that the frame can adjust itself (think auto layout or single child)
280 if ( !m_sizeSet )
281 {
282 m_sizeSet = true;
283 wxSizeEvent event(GetSize(), GetId());
284 event.SetEventObject(this);
285 GetEventHandler()->ProcessEvent(event);
286 }
287
288 // make sure the window is fully painted, with all pending updates, before
289 // DFB WM shows it, otherwise it would attempt to show either empty (=
290 // black) window surface (if shown for the first time) or it would show
291 // window with outdated content; note that the window was already refreshed
292 // in the wxTopLevelWindowBase::Show() call above:
293 if ( show )
294 Update();
295
296 // hide/show the window by setting its opacity to 0/full:
297 m_dfbwin->SetOpacity(show ? m_opacity : 0);
298
299 if ( show )
300 {
301 wxWindow *focused = wxWindow::FindFocus();
302 if ( focused && focused->GetTLW() == this )
303 {
304 SetDfbFocus();
305 }
306 else if ( AcceptsFocus() )
307 {
308 // FIXME: we should probably always call SetDfbFocus instead
309 // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
310 // handler
311 SetFocus();
312 }
313 }
314
315 return true;
316 }
317
318 bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha)
319 {
320 if ( IsShown() )
321 {
322 if ( !m_dfbwin->SetOpacity(alpha) )
323 return false;
324 }
325
326 m_opacity = alpha;
327 return true;
328 }
329
330 // ----------------------------------------------------------------------------
331 // maximize, minimize etc.
332 // ----------------------------------------------------------------------------
333
334 void wxTopLevelWindowDFB::Maximize(bool maximize)
335 {
336 int x, y, w, h;
337 wxClientDisplayRect(&x, &y, &w, &h);
338
339 if ( maximize && !m_isMaximized )
340 {
341 m_isMaximized = true;
342
343 GetPosition(&m_savedFrame.x, &m_savedFrame.y);
344 GetSize(&m_savedFrame.width, &m_savedFrame.height);
345
346 SetSize(x, y, w, h);
347 }
348 else if ( !maximize && m_isMaximized )
349 {
350 m_isMaximized = false;
351 SetSize(m_savedFrame.x, m_savedFrame.y,
352 m_savedFrame.width, m_savedFrame.height);
353 }
354 }
355
356 bool wxTopLevelWindowDFB::IsMaximized() const
357 {
358 return m_isMaximized;
359 }
360
361 void wxTopLevelWindowDFB::Restore()
362 {
363 if ( IsMaximized() )
364 {
365 Maximize(false);
366 }
367 }
368
369 void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize))
370 {
371 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
372 }
373
374 bool wxTopLevelWindowDFB::IsIconized() const
375 {
376 return false;
377 }
378
379
380 // ----------------------------------------------------------------------------
381 // surfaces and painting
382 // ----------------------------------------------------------------------------
383
384 wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const
385 {
386 return m_dfbwin->GetSurface();
387 }
388
389 void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
390 {
391 if ( m_toPaint->IsEmpty() )
392 return; // nothing to do
393
394 if ( IsFrozen() || !IsShown() )
395 {
396 // nothing to do if the window is frozen or hidden; clear the queue
397 // and return (note that it's OK to clear the queue even if the window
398 // is frozen, because Thaw() calls Refresh()):
399 m_toPaint->Clear();
400 return;
401 }
402
403 // process queued paint requests:
404 wxRect winRect(wxPoint(0, 0), GetSize());
405 wxRect paintedRect;
406
407 // important note: all DCs created from now until m_isPainting is reset to
408 // false will not update the front buffer as this flag indicates that we'll
409 // blit the entire back buffer to front soon
410 m_isPainting = true;
411
412 #ifdef __WXDEBUG__
413 int requestsCount = 0;
414 #endif
415
416 wxRect request;
417 while ( m_toPaint->GetNext(request) )
418 {
419 #ifdef __WXDEBUG__
420 requestsCount++;
421 #endif
422 wxRect clipped(request);
423 clipped.Intersect(winRect);
424 if ( clipped.IsEmpty() )
425 continue; // nothing to refresh
426
427 wxLogTrace(TRACE_PAINT,
428 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
429 this, GetName().c_str(),
430 clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom());
431
432 PaintWindow(clipped);
433
434 // remember rectangle covering all repainted areas:
435 if ( paintedRect.IsEmpty() )
436 paintedRect = clipped;
437 else
438 paintedRect.Union(clipped);
439 }
440
441 m_isPainting = false;
442
443 m_toPaint->Clear();
444
445 if ( paintedRect.IsEmpty() )
446 return; // no painting occurred, no need to flip
447
448 // Flip the surface to make the changes visible. Note that the rectangle we
449 // flip is *superset* of the union of repainted rectangles (created as
450 // "rectangles union" by wxRect::Union) and so some parts of the back
451 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
452 // be copied to the front buffer as well. This is safe/correct thing to do
453 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
454 // the back and front buffers contain the same data.
455 //
456 // Note that we do _not_ split m_toPaint into disjoint rectangles and
457 // do FlipToFront() for each of them, because that could result in visible
458 // updating of the screen; instead, we prefer to flip everything at once.
459
460 DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(),
461 paintedRect.GetRight(), paintedRect.GetBottom()};
462 DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r;
463
464 GetDfbSurface()->FlipToFront(rptr);
465
466 wxLogTrace(TRACE_PAINT,
467 _T("%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]"),
468 this, GetName().c_str(),
469 requestsCount,
470 paintedRect.x, paintedRect.y,
471 paintedRect.GetRight(), paintedRect.GetBottom());
472 }
473
474 void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect)
475 {
476 // don't overlap outside of the window (NB: 'rect' is in window coords):
477 wxRect r(rect);
478 r.Intersect(wxRect(GetSize()));
479 if ( r.IsEmpty() )
480 return;
481
482 wxLogTrace(TRACE_PAINT,
483 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
484 this, GetName().c_str(),
485 rect.x, rect.y, rect.GetRight(), rect.GetBottom());
486
487 // defer painting until idle time or until Update() is called:
488 m_toPaint->Add(rect);
489 }
490
491 void wxTopLevelWindowDFB::Update()
492 {
493 HandleQueuedPaintRequests();
494 }
495
496 // ---------------------------------------------------------------------------
497 // events handling
498 // ---------------------------------------------------------------------------
499
500 void wxTopLevelWindowDFB::SetDfbFocus()
501 {
502 wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") );
503 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
504 _T("setting DirectFB focus to unexpected window") );
505
506 GetDirectFBWindow()->RequestFocus();
507 }
508
509 /* static */
510 void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_)
511 {
512 const DFBWindowEvent& event = event_;
513
514 if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() )
515 {
516 wxLogTrace(TRACE_EVENTS,
517 _T("received event for unknown DirectFB window, ignoring"));
518 return;
519 }
520
521 wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id];
522 wxWindow *recipient = NULL;
523 void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL;
524
525 switch ( event.type )
526 {
527 case DWET_KEYDOWN:
528 case DWET_KEYUP:
529 {
530 recipient = wxWindow::FindFocus();
531 handlerFunc = &wxWindowDFB::HandleKeyEvent;
532 break;
533 }
534
535 case DWET_NONE:
536 case DWET_ALL:
537 {
538 wxFAIL_MSG( _T("invalid event type") );
539 break;
540 }
541
542 default:
543 // we're not interested in them here
544 break;
545 }
546
547 if ( !recipient )
548 {
549 wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window"));
550 return;
551 }
552
553 wxCHECK_RET( recipient && recipient->GetTLW() == tlw,
554 _T("event recipient not in TLW which received the event") );
555
556 // process the event:
557 (recipient->*handlerFunc)(event_);
558 }
559
560 // ---------------------------------------------------------------------------
561 // idle events processing
562 // ---------------------------------------------------------------------------
563
564 void wxTopLevelWindowDFB::OnInternalIdle()
565 {
566 wxTopLevelWindowBase::OnInternalIdle();
567 HandleQueuedPaintRequests();
568 }