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