]> git.saurik.com Git - wxWidgets.git/blob - src/osx/nonownedwnd_osx.cpp
Update OpenVMS compile support
[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 #include "wx/log.h"
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
39 wxWindow* 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
50 WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
51
52 static MacWindowMap wxWinMacWindowList;
53
54 wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
55 {
56 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
57
58 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
59 }
60
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
62 void 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
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
72 void 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
85 wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
86 {
87 return wxFindWindowFromWXWindow( win );
88 }
89
90 // ----------------------------------------------------------------------------
91 // wxNonOwnedWindow creation
92 // ----------------------------------------------------------------------------
93
94 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
95
96 wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
97
98 void wxNonOwnedWindow::Init()
99 {
100 m_nowpeer = NULL;
101 }
102
103 bool 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
136 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name );
137 wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
138 m_peer = wxWidgetImpl::CreateContentView(this);
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
153 wxNonOwnedWindow::~wxNonOwnedWindow()
154 {
155 SendDestroyEvent();
156
157 wxRemoveWXWindowAssociation( this ) ;
158
159 DestroyChildren();
160
161 delete m_nowpeer;
162
163 // avoid dangling refs
164 if ( s_macDeactivateWindow == this )
165 s_macDeactivateWindow = NULL;
166 }
167
168 // ----------------------------------------------------------------------------
169 // wxNonOwnedWindow misc
170 // ----------------------------------------------------------------------------
171
172 bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect,
173 unsigned timeout )
174 {
175 if ( !wxWindow::Show(true) )
176 return false;
177
178 // because apps expect a size event to occur at this moment
179 wxSizeEvent event(GetSize() , m_windowId);
180 event.SetEventObject(this);
181 HandleWindowEvent(event);
182
183
184 return m_nowpeer->ShowWithEffect(true, effect, timeout);
185 }
186
187 bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect,
188 unsigned timeout )
189 {
190 if ( !wxWindow::Show(false) )
191 return false;
192
193 return m_nowpeer->ShowWithEffect(false, effect, timeout);
194 }
195
196 wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
197 {
198 int left, top, width, height;
199 m_nowpeer->GetContentArea(left, top, width, height);
200 return wxPoint(left, top);
201 }
202
203 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
204 {
205 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
206 return false ;
207
208 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
209 {
210 if ( m_nowpeer )
211 return m_nowpeer->SetBackgroundColour(c);
212 }
213 return true;
214 }
215
216 // Raise the window to the top of the Z order
217 void wxNonOwnedWindow::Raise()
218 {
219 m_nowpeer->Raise();
220 }
221
222 // Lower the window to the bottom of the Z order
223 void wxNonOwnedWindow::Lower()
224 {
225 m_nowpeer->Lower();
226 }
227
228 void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
229 {
230 MacActivate( (int) (timestampsec * 1000) , didActivate);
231 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
232 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
233 wxevent.SetEventObject(this);
234 HandleWindowEvent(wxevent);
235 }
236
237 void wxNonOwnedWindow::HandleResized( double timestampsec )
238 {
239 wxSizeEvent wxevent( GetSize() , GetId());
240 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
241 wxevent.SetEventObject( this );
242 HandleWindowEvent(wxevent);
243 // we have to inform some controls that have to reset things
244 // relative to the toplevel window (e.g. OpenGL buffers)
245 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
246 }
247
248 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
249 {
250 wxRect r = *rect ;
251
252 // this is a EVT_SIZING not a EVT_SIZE type !
253 wxSizeEvent wxevent( r , GetId() ) ;
254 wxevent.SetEventObject( this ) ;
255 if ( HandleWindowEvent(wxevent) )
256 r = wxevent.GetRect() ;
257
258 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
259 r.SetWidth( GetMaxWidth() ) ;
260 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
261 r.SetHeight( GetMaxHeight() ) ;
262 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
263 r.SetWidth( GetMinWidth() ) ;
264 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
265 r.SetHeight( GetMinHeight() ) ;
266
267 *rect = r;
268 // TODO actuall this is too early, in case the window extents are adjusted
269 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
270 }
271
272 void wxNonOwnedWindow::HandleMoved( double timestampsec )
273 {
274 wxMoveEvent wxevent( GetPosition() , GetId());
275 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
276 wxevent.SetEventObject( this );
277 HandleWindowEvent(wxevent);
278 }
279
280 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
281 {
282 if (s_macDeactivateWindow)
283 {
284 wxLogTrace(TRACE_ACTIVATE,
285 wxT("Doing delayed deactivation of %p"),
286 s_macDeactivateWindow);
287
288 s_macDeactivateWindow->MacActivate(timestamp, false);
289 }
290 }
291
292 void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
293 {
294 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
295
296 if (s_macDeactivateWindow == this)
297 s_macDeactivateWindow = NULL;
298
299 MacDelayedDeactivation(timestamp);
300 }
301
302 bool wxNonOwnedWindow::Show(bool show)
303 {
304 if ( !wxWindow::Show(show) )
305 return false;
306
307 if ( m_nowpeer )
308 m_nowpeer->Show(show);
309
310 if ( show )
311 {
312 // because apps expect a size event to occur at this moment
313 wxSizeEvent event(GetSize() , m_windowId);
314 event.SetEventObject(this);
315 HandleWindowEvent(event);
316 }
317
318 return true ;
319 }
320
321 bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
322 {
323 return m_nowpeer->SetTransparent(alpha);
324 }
325
326
327 bool wxNonOwnedWindow::CanSetTransparent()
328 {
329 return m_nowpeer->CanSetTransparent();
330 }
331
332
333 void wxNonOwnedWindow::SetExtraStyle(long exStyle)
334 {
335 if ( GetExtraStyle() == exStyle )
336 return ;
337
338 wxWindow::SetExtraStyle( exStyle ) ;
339
340 if ( m_nowpeer )
341 m_nowpeer->SetExtraStyle(exStyle);
342 }
343
344 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
345 {
346 if ( !wxWindow::SetBackgroundStyle(style) )
347 return false ;
348
349 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
350 }
351
352 void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
353 {
354 if ( m_nowpeer == NULL )
355 return;
356
357 m_cachedClippedRectValid = false ;
358
359 m_nowpeer->MoveWindow(x, y, width, height);
360 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
361 }
362
363 void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
364 {
365 if ( m_nowpeer == NULL )
366 return;
367
368 int x1,y1 ;
369 m_nowpeer->GetPosition(x1, y1);
370
371 if (x)
372 *x = x1 ;
373 if (y)
374 *y = y1 ;
375 }
376
377 void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
378 {
379 if ( m_nowpeer == NULL )
380 return;
381
382 int w,h;
383
384 m_nowpeer->GetSize(w, h);
385
386 if (width)
387 *width = w ;
388 if (height)
389 *height = h ;
390 }
391
392 void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
393 {
394 if ( m_nowpeer == NULL )
395 return;
396
397 int left, top, w, h;
398 m_nowpeer->GetContentArea(left, top, w, h);
399
400 if (width)
401 *width = w ;
402 if (height)
403 *height = h ;
404 }
405
406
407 void wxNonOwnedWindow::Update()
408 {
409 m_nowpeer->Update();
410 }
411
412 WXWindow wxNonOwnedWindow::GetWXWindow() const
413 {
414 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
415 }
416
417 // ---------------------------------------------------------------------------
418 // Shape implementation
419 // ---------------------------------------------------------------------------
420
421
422 bool wxNonOwnedWindow::DoSetShape(const wxRegion& region)
423 {
424 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
425 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
426
427 // The empty region signifies that the shape
428 // should be removed from the window.
429 if ( region.IsEmpty() )
430 {
431 wxSize sz = GetClientSize();
432 wxRegion rgn(0, 0, sz.x, sz.y);
433 if ( rgn.IsEmpty() )
434 return false ;
435 else
436 return DoSetShape(rgn);
437 }
438
439 return m_nowpeer->SetShape(region);
440 }