]> git.saurik.com Git - wxWidgets.git/blame - src/osx/nonownedwnd_osx.cpp
fixing some warnings, adding proper OnChar support for special keys
[wxWidgets.git] / src / osx / nonownedwnd_osx.cpp
CommitLineData
3ccc5735
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/nonownedwnd_osx.cpp
3// Purpose: implementation of wxNonOwnedWindow
4// Author: Stefan Csomor
5// Created: 2008-03-24
6// RCS-ID: $Id: nonownedwnd.cpp 50329 2007-11-29 17:00:58Z VS $
7// Copyright: (c) Stefan Csomor 2008
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifndef WX_PRECOMP
15 #include "wx/app.h"
e1f833da 16 #include "wx/log.h"
3ccc5735
SC
17#endif // WX_PRECOMP
18
19#include "wx/hashmap.h"
20#include "wx/evtloop.h"
21#include "wx/tooltip.h"
22#include "wx/nonownedwnd.h"
23
24#include "wx/osx/private.h"
25#include "wx/settings.h"
26#include "wx/frame.h"
27
28#if wxUSE_SYSTEM_OPTIONS
29 #include "wx/sysopt.h"
30#endif
31
32// ----------------------------------------------------------------------------
33// constants
34// ----------------------------------------------------------------------------
35
36// trace mask for activation tracing messages
37#define TRACE_ACTIVATE "activation"
38
39wxWindow* g_MacLastWindow = NULL ;
40
41// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
42#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
43
44// ---------------------------------------------------------------------------
45// wxWindowMac utility functions
46// ---------------------------------------------------------------------------
47
48// Find an item given the Macintosh Window Reference
49
50WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
51
52static MacWindowMap wxWinMacWindowList;
53
54wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
55{
56 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
57
58 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
59}
60
61void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
62void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win)
63{
64 // adding NULL WindowRef is (first) surely a result of an error and
65 // nothing else :-)
66 wxCHECK_RET( inWindowRef != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
67
68 wxWinMacWindowList[inWindowRef] = win;
69}
70
71void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
72void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win)
73{
74 MacWindowMap::iterator it;
75 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
76 {
77 if ( it->second == win )
78 {
79 wxWinMacWindowList.erase(it);
80 break;
81 }
82 }
83}
84
85wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
86{
87 return wxFindWindowFromWXWindow( win );
88}
89
90// ----------------------------------------------------------------------------
91// wxNonOwnedWindow creation
92// ----------------------------------------------------------------------------
93
94IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
95
96wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
97
98void wxNonOwnedWindow::Init()
99{
100 m_nowpeer = NULL;
101}
102
103bool wxNonOwnedWindow::Create(wxWindow *parent,
104 wxWindowID id,
105 const wxPoint& pos,
106 const wxSize& size,
107 long style,
108 const wxString& name)
109{
110 // init our fields
111 Init();
112
113 m_windowStyle = style;
114
115 SetName( name );
116
117 m_windowId = id == -1 ? NewControlId() : id;
118 m_windowStyle = style;
119 m_isShown = false;
120
121 // create frame.
122 int x = (int)pos.x;
123 int y = (int)pos.y;
124
125 wxRect display = wxGetClientDisplayRect() ;
126
127 if ( x == wxDefaultPosition.x )
128 x = display.x ;
129
130 if ( y == wxDefaultPosition.y )
131 y = display.y ;
132
133 int w = WidthDefault(size.x);
134 int h = HeightDefault(size.y);
135
524c47aa 136 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name );
3ccc5735 137 wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
3ccc5735 138 m_peer = wxWidgetImpl::CreateContentView(this);
3ccc5735
SC
139
140 DoSetWindowVariant( m_windowVariant ) ;
141
142 wxWindowCreateEvent event(this);
143 HandleWindowEvent(event);
144
145 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
146
147 if ( parent )
148 parent->AddChild(this);
149
150 return true;
151}
152
153wxNonOwnedWindow::~wxNonOwnedWindow()
154{
c6212a0c 155 SendDestroyEvent();
03647350 156
3ccc5735 157 wxRemoveWXWindowAssociation( this ) ;
03647350 158
524c47aa 159 DestroyChildren();
03647350 160
524c47aa 161 delete m_nowpeer;
0aaa6ace 162 m_nowpeer = NULL;
3ccc5735
SC
163
164 // avoid dangling refs
165 if ( s_macDeactivateWindow == this )
166 s_macDeactivateWindow = NULL;
167}
168
e2758e21 169bool wxNonOwnedWindow::Destroy()
0aaa6ace 170{
e2758e21 171 WillBeDestroyed();
0aaa6ace 172
e2758e21
KO
173 return wxWindow::Destroy();
174}
175
176void wxNonOwnedWindow::WillBeDestroyed()
177{
0aaa6ace
SC
178 if ( m_nowpeer )
179 m_nowpeer->WillBeDestroyed();
180}
181
3ccc5735
SC
182// ----------------------------------------------------------------------------
183// wxNonOwnedWindow misc
184// ----------------------------------------------------------------------------
185
ab9a0b84
VZ
186bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
187 wxShowEffect effect,
188 unsigned timeout)
03647350 189{
ab9a0b84
VZ
190 // Cocoa code needs to manage window visibility on its own and so calls
191 // wxWindow::Show() as needed but if we already changed the internal
192 // visibility flag here, Show() would do nothing, so avoid doing it
193#if wxOSX_USE_CARBON
194 if ( !wxWindow::Show(show) )
3ccc5735 195 return false;
ab9a0b84 196#endif // Carbon
3ccc5735 197
ab9a0b84
VZ
198 if ( effect == wxSHOW_EFFECT_NONE ||
199 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
200 return Show(show);
3ccc5735 201
ab9a0b84
VZ
202 if ( show )
203 {
204 // as apps expect a size event to occur when the window is shown,
205 // generate one when it is shown with effect too
206 wxSizeEvent event(GetSize(), m_windowId);
207 event.SetEventObject(this);
208 HandleWindowEvent(event);
209 }
3ccc5735 210
ab9a0b84 211 return true;
3ccc5735
SC
212}
213
214wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
215{
216 int left, top, width, height;
217 m_nowpeer->GetContentArea(left, top, width, height);
218 return wxPoint(left, top);
219}
220
221bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
03647350 222{
3ccc5735
SC
223 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
224 return false ;
03647350 225
3ccc5735
SC
226 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
227 {
524c47aa
SC
228 if ( m_nowpeer )
229 return m_nowpeer->SetBackgroundColour(c);
3ccc5735
SC
230 }
231 return true;
03647350 232}
3ccc5735 233
b6dc21e7
KO
234void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
235{
236 if (flags == GetWindowStyleFlag())
237 return;
238
239 wxWindow::SetWindowStyleFlag(flags);
240
241 if (m_nowpeer)
242 m_nowpeer->SetWindowStyleFlag(flags);
243}
244
3ccc5735
SC
245// Raise the window to the top of the Z order
246void wxNonOwnedWindow::Raise()
247{
248 m_nowpeer->Raise();
249}
250
251// Lower the window to the bottom of the Z order
252void wxNonOwnedWindow::Lower()
253{
254 m_nowpeer->Lower();
255}
256
257void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
258{
259 MacActivate( (int) (timestampsec * 1000) , didActivate);
260 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
261 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
262 wxevent.SetEventObject(this);
263 HandleWindowEvent(wxevent);
264}
265
266void wxNonOwnedWindow::HandleResized( double timestampsec )
267{
268 wxSizeEvent wxevent( GetSize() , GetId());
269 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
270 wxevent.SetEventObject( this );
271 HandleWindowEvent(wxevent);
272 // we have to inform some controls that have to reset things
273 // relative to the toplevel window (e.g. OpenGL buffers)
274 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
275}
276
0faf03bf 277void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
3ccc5735
SC
278{
279 wxRect r = *rect ;
280
281 // this is a EVT_SIZING not a EVT_SIZE type !
282 wxSizeEvent wxevent( r , GetId() ) ;
283 wxevent.SetEventObject( this ) ;
284 if ( HandleWindowEvent(wxevent) )
285 r = wxevent.GetRect() ;
286
287 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
288 r.SetWidth( GetMaxWidth() ) ;
289 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
290 r.SetHeight( GetMaxHeight() ) ;
291 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
292 r.SetWidth( GetMinWidth() ) ;
293 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
294 r.SetHeight( GetMinHeight() ) ;
295
296 *rect = r;
297 // TODO actuall this is too early, in case the window extents are adjusted
298 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
299}
300
301void wxNonOwnedWindow::HandleMoved( double timestampsec )
302{
303 wxMoveEvent wxevent( GetPosition() , GetId());
304 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
305 wxevent.SetEventObject( this );
306 HandleWindowEvent(wxevent);
307}
308
309void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
310{
311 if (s_macDeactivateWindow)
312 {
313 wxLogTrace(TRACE_ACTIVATE,
314 wxT("Doing delayed deactivation of %p"),
315 s_macDeactivateWindow);
316
317 s_macDeactivateWindow->MacActivate(timestamp, false);
318 }
319}
320
321void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
322{
323 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
324
325 if (s_macDeactivateWindow == this)
326 s_macDeactivateWindow = NULL;
327
328 MacDelayedDeactivation(timestamp);
329}
330
331bool wxNonOwnedWindow::Show(bool show)
332{
333 if ( !wxWindow::Show(show) )
334 return false;
335
336 if ( m_nowpeer )
337 m_nowpeer->Show(show);
03647350 338
3ccc5735
SC
339 if ( show )
340 {
341 // because apps expect a size event to occur at this moment
342 wxSizeEvent event(GetSize() , m_windowId);
343 event.SetEventObject(this);
344 HandleWindowEvent(event);
345 }
03647350 346
3ccc5735
SC
347 return true ;
348}
349
350bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
351{
352 return m_nowpeer->SetTransparent(alpha);
353}
354
355
356bool wxNonOwnedWindow::CanSetTransparent()
357{
358 return m_nowpeer->CanSetTransparent();
359}
360
361
362void wxNonOwnedWindow::SetExtraStyle(long exStyle)
363{
364 if ( GetExtraStyle() == exStyle )
365 return ;
366
367 wxWindow::SetExtraStyle( exStyle ) ;
368
369 if ( m_nowpeer )
370 m_nowpeer->SetExtraStyle(exStyle);
371}
372
373bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
374{
375 if ( !wxWindow::SetBackgroundStyle(style) )
376 return false ;
03647350 377
4d949e7f 378 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
3ccc5735
SC
379}
380
381void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
382{
b8afff01
SC
383 if ( m_nowpeer == NULL )
384 return;
385
3ccc5735
SC
386 m_cachedClippedRectValid = false ;
387
388 m_nowpeer->MoveWindow(x, y, width, height);
389 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
390}
391
392void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
393{
b8afff01
SC
394 if ( m_nowpeer == NULL )
395 return;
396
3ccc5735
SC
397 int x1,y1 ;
398 m_nowpeer->GetPosition(x1, y1);
399
400 if (x)
401 *x = x1 ;
402 if (y)
403 *y = y1 ;
404}
405
406void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
407{
b8afff01
SC
408 if ( m_nowpeer == NULL )
409 return;
03647350 410
3ccc5735 411 int w,h;
03647350 412
3ccc5735
SC
413 m_nowpeer->GetSize(w, h);
414
415 if (width)
416 *width = w ;
417 if (height)
418 *height = h ;
419}
420
421void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
422{
b8afff01
SC
423 if ( m_nowpeer == NULL )
424 return;
425
3ccc5735
SC
426 int left, top, w, h;
427 m_nowpeer->GetContentArea(left, top, w, h);
65536c92 428
3ccc5735
SC
429 if (width)
430 *width = w ;
431 if (height)
432 *height = h ;
433}
434
435
436void wxNonOwnedWindow::Update()
437{
438 m_nowpeer->Update();
439}
440
441WXWindow wxNonOwnedWindow::GetWXWindow() const
442{
443 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
444}
445
446// ---------------------------------------------------------------------------
447// Shape implementation
448// ---------------------------------------------------------------------------
449
450
8e88e984 451bool wxNonOwnedWindow::DoSetShape(const wxRegion& region)
3ccc5735
SC
452{
453 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
9a83f860 454 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
3ccc5735
SC
455
456 // The empty region signifies that the shape
457 // should be removed from the window.
458 if ( region.IsEmpty() )
459 {
460 wxSize sz = GetClientSize();
461 wxRegion rgn(0, 0, sz.x, sz.y);
462 if ( rgn.IsEmpty() )
463 return false ;
464 else
8e88e984 465 return DoSetShape(rgn);
3ccc5735
SC
466 }
467
468 return m_nowpeer->SetShape(region);
469}