]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/toplevel.cpp
Change event names to clarify that they are only fired by button clicks, and note...
[wxWidgets.git] / src / dfb / toplevel.cpp
CommitLineData
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"
b3c86150
VS
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:
32WX_DECLARE_HASH_MAP(DFBWindowID, wxTopLevelWindowDFB*,
33 wxIntegerHash, wxIntegerEqual,
34 wxDfbWindowsMap);
35static wxDfbWindowsMap gs_dfbWindowsMap;
36
37// ============================================================================
38// helpers
39// ============================================================================
40
14ac4e3a
VS
41// Queue of paint requests
42class wxDfbQueuedPaintRequests
43{
44public:
45 ~wxDfbQueuedPaintRequests() { Clear(); }
46
47 // Adds paint request to the queue
20671963 48 void Add(const wxRect& rect)
93259e59
VS
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 }
14ac4e3a
VS
56
57 // Is the queue empty?
93259e59 58 bool IsEmpty() const { return m_invalidated.IsEmpty(); }
14ac4e3a
VS
59
60 // Empties the queue
93259e59 61 void Clear() { m_invalidated = wxRect(); }
14ac4e3a 62
93259e59
VS
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 }
14ac4e3a
VS
74
75private:
93259e59
VS
76 // currently invalidated region
77 wxRect m_invalidated;
14ac4e3a 78};
b3c86150
VS
79
80// ============================================================================
81// wxTopLevelWindowDFB
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// creation & destruction
86// ----------------------------------------------------------------------------
87
88void 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;
20671963 96 m_isPainting = false;
b3c86150
VS
97}
98
99bool 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:
a5b31f4e 128 wxIDirectFBDisplayLayerPtr layer(wxIDirectFB::Get()->GetDisplayLayer());
b3c86150
VS
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;
52c8d32a 140 m_dfbwin = layer->CreateWindow(&desc);
82c74d89 141 if ( !m_dfbwin )
b3c86150
VS
142 return false;
143
144 // add the new TLW to DFBWindowID->wxTLW map:
145 DFBWindowID winid;
52c8d32a 146 if ( !m_dfbwin->GetID(&winid) )
b3c86150
VS
147 return false;
148 gs_dfbWindowsMap[winid] = this;
149
150 // TLWs are created initially hidden:
52c8d32a 151 if ( !m_dfbwin->SetOpacity(wxALPHA_TRANSPARENT) )
b3c86150
VS
152 return false;
153
20671963
VS
154 if ( !wxWindow::Create(NULL, id, pos, size, style, name) )
155 return false;
b3c86150
VS
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 {
52c8d32a 166 m_dfbwin->SetStackingClass(DWSC_UPPER);
b3c86150
VS
167 }
168
169 // direct events in this window to the global event buffer:
52c8d32a 170 m_dfbwin->AttachEventBuffer(wxEventLoop::GetDirectFBEventBuffer());
b3c86150
VS
171
172 return true;
173}
174
175wxTopLevelWindowDFB::~wxTopLevelWindowDFB()
176{
177 m_isBeingDeleted = true;
178
d1bb1bb5
VS
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();
b3c86150 182
d1bb1bb5 183 // it's safe to delete the underlying DirectFB window now:
b3c86150
VS
184 wxDELETE(m_toPaint);
185
82c74d89
VS
186 if ( !m_dfbwin )
187 return;
188
b3c86150
VS
189 // remove the TLW from DFBWindowID->wxTLW map:
190 DFBWindowID winid;
52c8d32a 191 if ( m_dfbwin->GetID(&winid) )
b3c86150 192 gs_dfbWindowsMap.erase(winid);
82c74d89
VS
193
194 m_dfbwin->Destroy();
195 m_dfbwin.Reset();
b3c86150
VS
196}
197
198// ----------------------------------------------------------------------------
199// window size & position
200// ----------------------------------------------------------------------------
201
202void wxTopLevelWindowDFB::DoGetPosition(int *x, int *y) const
203{
52c8d32a 204 m_dfbwin->GetPosition(x, y);
b3c86150
VS
205}
206
207void wxTopLevelWindowDFB::DoGetSize(int *width, int *height) const
208{
52c8d32a 209 m_dfbwin->GetSize(width, height);
b3c86150
VS
210}
211
212void wxTopLevelWindowDFB::DoMoveWindow(int x, int y, int width, int height)
213{
214 wxPoint curpos = GetPosition();
215 if ( curpos.x != x || curpos.y != y )
216 {
52c8d32a 217 m_dfbwin->MoveTo(x, y);
b3c86150
VS
218 }
219
220 wxSize cursize = GetSize();
221 if ( cursize.x != width || cursize.y != height )
222 {
7807a2b4
VS
223 // changing window's size changes its surface:
224 InvalidateDfbSurface();
225
52c8d32a 226 m_dfbwin->Resize(width, height);
7807a2b4 227
b3c86150 228 // we must repaint the window after it changed size:
20671963
VS
229 if ( IsShown() )
230 DoRefreshWindow();
b3c86150
VS
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"
239bool 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
271bool wxTopLevelWindowDFB::Show(bool show)
272{
273 if ( !wxTopLevelWindowBase::Show(show) )
274 return false;
275
276 // hide/show the window by setting its opacity to 0/full:
52c8d32a 277 m_dfbwin->SetOpacity(show ? m_opacity : 0);
b3c86150
VS
278
279 // If this is the first time Show was called, send size event,
280 // so that the frame can adjust itself (think auto layout or single child)
281 if ( !m_sizeSet )
282 {
283 m_sizeSet = true;
284 wxSizeEvent event(GetSize(), GetId());
285 event.SetEventObject(this);
286 GetEventHandler()->ProcessEvent(event);
287 }
288
4ff28c37
VS
289 if ( show )
290 {
291 wxWindow *focused = wxWindow::FindFocus();
292 if ( focused && focused->GetTLW() == this )
293 {
294 SetDfbFocus();
295 }
296 else if ( AcceptsFocus() )
297 {
298 // FIXME: we should probably always call SetDfbFocus instead
299 // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
300 // handler
301 SetFocus();
302 }
303 }
b3c86150
VS
304
305 return true;
306}
307
308bool wxTopLevelWindowDFB::SetTransparent(wxByte alpha)
309{
310 if ( IsShown() )
311 {
52c8d32a 312 if ( !m_dfbwin->SetOpacity(alpha) )
b3c86150
VS
313 return false;
314 }
315
316 m_opacity = alpha;
317 return true;
318}
319
320// ----------------------------------------------------------------------------
321// maximize, minimize etc.
322// ----------------------------------------------------------------------------
323
324void wxTopLevelWindowDFB::Maximize(bool maximize)
325{
326 int x, y, w, h;
327 wxClientDisplayRect(&x, &y, &w, &h);
328
329 if ( maximize && !m_isMaximized )
330 {
331 m_isMaximized = true;
332
333 GetPosition(&m_savedFrame.x, &m_savedFrame.y);
334 GetSize(&m_savedFrame.width, &m_savedFrame.height);
335
336 SetSize(x, y, w, h);
337 }
338 else if ( !maximize && m_isMaximized )
339 {
340 m_isMaximized = false;
341 SetSize(m_savedFrame.x, m_savedFrame.y,
342 m_savedFrame.width, m_savedFrame.height);
343 }
344}
345
346bool wxTopLevelWindowDFB::IsMaximized() const
347{
348 return m_isMaximized;
349}
350
351void wxTopLevelWindowDFB::Restore()
352{
353 if ( IsMaximized() )
354 {
355 Maximize(false);
356 }
357}
358
359void wxTopLevelWindowDFB::Iconize(bool WXUNUSED(iconize))
360{
361 wxFAIL_MSG(wxT("Iconize not supported under wxDFB"));
362}
363
364bool wxTopLevelWindowDFB::IsIconized() const
365{
366 return false;
367}
368
369
370// ----------------------------------------------------------------------------
371// surfaces and painting
372// ----------------------------------------------------------------------------
373
52c8d32a 374wxIDirectFBSurfacePtr wxTopLevelWindowDFB::ObtainDfbSurface() const
b3c86150 375{
52c8d32a 376 return m_dfbwin->GetSurface();
b3c86150
VS
377}
378
379void wxTopLevelWindowDFB::HandleQueuedPaintRequests()
380{
14ac4e3a 381 if ( m_toPaint->IsEmpty() )
b3c86150
VS
382 return; // nothing to do
383
14ac4e3a
VS
384 if ( IsFrozen() || !IsShown() )
385 {
386 // nothing to do if the window is frozen or hidden; clear the queue
387 // and return (note that it's OK to clear the queue even if the window
388 // is frozen, because Thaw() calls Refresh()):
389 m_toPaint->Clear();
390 return;
391 }
392
b3c86150
VS
393 // process queued paint requests:
394 wxRect winRect(wxPoint(0, 0), GetSize());
395 wxRect paintedRect;
396
20671963
VS
397 // important note: all DCs created from now until m_isPainting is reset to
398 // false will not update the front buffer as this flag indicates that we'll
399 // blit the entire back buffer to front soon
400 m_isPainting = true;
401
93259e59
VS
402#ifdef __WXDEBUG__
403 int requestsCount = 0;
404#endif
20671963 405
93259e59
VS
406 wxRect request;
407 while ( m_toPaint->GetNext(request) )
b3c86150 408 {
93259e59
VS
409#ifdef __WXDEBUG__
410 requestsCount++;
411#endif
412 wxRect clipped(request);
b3c86150
VS
413 clipped.Intersect(winRect);
414 if ( clipped.IsEmpty() )
415 continue; // nothing to refresh
416
20671963
VS
417 wxLogTrace(TRACE_PAINT,
418 _T("%p ('%s'): processing paint request [%i,%i,%i,%i]"),
419 this, GetName().c_str(),
420 clipped.x, clipped.y, clipped.GetRight(), clipped.GetBottom());
421
422 PaintWindow(clipped);
b3c86150
VS
423
424 // remember rectangle covering all repainted areas:
425 if ( paintedRect.IsEmpty() )
426 paintedRect = clipped;
427 else
428 paintedRect.Union(clipped);
429 }
430
20671963
VS
431 m_isPainting = false;
432
14ac4e3a 433 m_toPaint->Clear();
b3c86150
VS
434
435 if ( paintedRect.IsEmpty() )
436 return; // no painting occurred, no need to flip
437
20671963
VS
438 // Flip the surface to make the changes visible. Note that the rectangle we
439 // flip is *superset* of the union of repainted rectangles (created as
440 // "rectangles union" by wxRect::Union) and so some parts of the back
441 // buffer that we didn't touch in this HandleQueuedPaintRequests call will
442 // be copied to the front buffer as well. This is safe/correct thing to do
443 // *only* because wx always use wxIDirectFBSurface::FlipToFront() and so
444 // the back and front buffers contain the same data.
445 //
446 // Note that we do _not_ split m_toPaint into disjoint rectangles and
447 // do FlipToFront() for each of them, because that could result in visible
448 // updating of the screen; instead, we prefer to flip everything at once.
449
b3c86150
VS
450 DFBRegion r = {paintedRect.GetLeft(), paintedRect.GetTop(),
451 paintedRect.GetRight(), paintedRect.GetBottom()};
452 DFBRegion *rptr = (winRect == paintedRect) ? NULL : &r;
453
20671963
VS
454 GetDfbSurface()->FlipToFront(rptr);
455
456 wxLogTrace(TRACE_PAINT,
93259e59 457 _T("%p ('%s'): processed %i paint requests, flipped surface: [%i,%i,%i,%i]"),
20671963 458 this, GetName().c_str(),
93259e59 459 requestsCount,
20671963
VS
460 paintedRect.x, paintedRect.y,
461 paintedRect.GetRight(), paintedRect.GetBottom());
b3c86150
VS
462}
463
20671963 464void wxTopLevelWindowDFB::DoRefreshRect(const wxRect& rect)
b3c86150 465{
bb78b682
VS
466 // don't overlap outside of the window (NB: 'rect' is in window coords):
467 wxRect r(rect);
468 r.Intersect(wxRect(GetSize()));
469 if ( r.IsEmpty() )
470 return;
20671963
VS
471
472 wxLogTrace(TRACE_PAINT,
473 _T("%p ('%s'): [TLW] refresh rect [%i,%i,%i,%i]"),
474 this, GetName().c_str(),
475 rect.x, rect.y, rect.GetRight(), rect.GetBottom());
476
14ac4e3a 477 // defer painting until idle time or until Update() is called:
20671963 478 m_toPaint->Add(rect);
b3c86150
VS
479}
480
481void wxTopLevelWindowDFB::Update()
482{
483 HandleQueuedPaintRequests();
484}
485
486// ---------------------------------------------------------------------------
487// events handling
488// ---------------------------------------------------------------------------
489
4ff28c37
VS
490void wxTopLevelWindowDFB::SetDfbFocus()
491{
492 wxCHECK_RET( IsShown(), _T("cannot set focus to hidden window") );
493 wxASSERT_MSG( FindFocus() && FindFocus()->GetTLW() == this,
494 _T("setting DirectFB focus to unexpected window") );
495
496 GetDirectFBWindow()->RequestFocus();
497}
498
b3c86150
VS
499/* static */
500void wxTopLevelWindowDFB::HandleDFBWindowEvent(const wxDFBWindowEvent& event_)
501{
502 const DFBWindowEvent& event = event_;
503
504 if ( gs_dfbWindowsMap.find(event.window_id) == gs_dfbWindowsMap.end() )
505 {
506 wxLogTrace(TRACE_EVENTS,
507 _T("received event for unknown DirectFB window, ignoring"));
508 return;
509 }
510
511 wxTopLevelWindowDFB *tlw = gs_dfbWindowsMap[event.window_id];
512 wxWindow *recipient = NULL;
513 void (wxWindow::*handlerFunc)(const wxDFBWindowEvent&) = NULL;
514
515 switch ( event.type )
516 {
517 case DWET_KEYDOWN:
518 case DWET_KEYUP:
519 {
520 recipient = wxWindow::FindFocus();
521 handlerFunc = &wxWindowDFB::HandleKeyEvent;
522 break;
523 }
524
525 case DWET_NONE:
526 case DWET_ALL:
527 {
528 wxFAIL_MSG( _T("invalid event type") );
529 break;
530 }
a43c3ed0
VZ
531
532 default:
533 // we're not interested in them here
534 break;
b3c86150
VS
535 }
536
537 if ( !recipient )
538 {
539 wxLogTrace(TRACE_EVENTS, _T("ignoring event: no recipient window"));
540 return;
541 }
542
543 wxCHECK_RET( recipient && recipient->GetTLW() == tlw,
544 _T("event recipient not in TLW which received the event") );
545
546 // process the event:
547 (recipient->*handlerFunc)(event_);
548}
549
550// ---------------------------------------------------------------------------
551// idle events processing
552// ---------------------------------------------------------------------------
553
554void wxTopLevelWindowDFB::OnInternalIdle()
555{
556 wxTopLevelWindowBase::OnInternalIdle();
557 HandleQueuedPaintRequests();
558}