]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
rename g_openDialogs to wxOpenModalDialogsCount and define it in toplevel.cpp to...
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
CommitLineData
a15eb0a5 1///////////////////////////////////////////////////////////////////////////////
fb5246be 2// Name: src/mac/carbon/toplevel.cpp
d66c3960
SC
3// Purpose: implements wxTopLevelWindow for Mac
4// Author: Stefan Csomor
a15eb0a5
SC
5// Modified by:
6// Created: 24.09.01
7// RCS-ID: $Id$
d66c3960 8// Copyright: (c) 2001-2004 Stefan Csomor
65571936 9// License: wxWindows licence
a15eb0a5
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
a15eb0a5
SC
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
1832043f
WS
27#include "wx/toplevel.h"
28
a15eb0a5
SC
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
422644a3 31 #include "wx/frame.h"
a15eb0a5
SC
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
37ec2bd3 35 #include "wx/settings.h"
2d17efa9 36 #include "wx/strconv.h"
179e085f 37 #include "wx/control.h"
a15eb0a5
SC
38#endif //WX_PRECOMP
39
5f0b2f22 40#include "wx/mac/uma.h"
422644a3 41#include "wx/mac/aga.h"
5f0b2f22 42#include "wx/tooltip.h"
a07c1212 43#include "wx/dnd.h"
a979e444 44
1f90939c
SC
45#if wxUSE_SYSTEM_OPTIONS
46 #include "wx/sysopt.h"
47#endif
5f0b2f22 48
3c393c0a 49#ifndef __DARWIN__
7f0c3a63 50#include <ToolUtils.h>
3c393c0a 51#endif
dd49c691 52
a979e444 53// for targeting OSX
eab19a7c
RN
54#include "wx/mac/private.h"
55
f02a4ffa
VZ
56// ----------------------------------------------------------------------------
57// constants
58// ----------------------------------------------------------------------------
59
893727e5
KO
60// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
61#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
62
f02a4ffa
VZ
63// trace mask for activation tracing messages
64static const wxChar *TRACE_ACTIVATE = _T("activation");
65
a15eb0a5
SC
66// ----------------------------------------------------------------------------
67// globals
68// ----------------------------------------------------------------------------
69
70// list of all frames and modeless dialogs
32b5be3d
RR
71wxWindowList wxModelessWindows;
72
6a7e6411
RD
73static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
74
a15eb0a5
SC
75// ============================================================================
76// wxTopLevelWindowMac implementation
77// ============================================================================
78
facd6764
SC
79BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
80END_EVENT_TABLE()
81
851b3a88
SC
82// ---------------------------------------------------------------------------
83// Carbon Events
84// ---------------------------------------------------------------------------
85
1542ea39 86static const EventTypeSpec eventList[] =
851b3a88 87{
a979e444 88 // TODO: remove control related event like key and mouse (except for WindowLeave events)
ffc75ee4 89
e40298d5 90 { kEventClassKeyboard, kEventRawKeyDown } ,
c5c9378c
SC
91 { kEventClassKeyboard, kEventRawKeyRepeat } ,
92 { kEventClassKeyboard, kEventRawKeyUp } ,
93 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
94
ffc75ee4
SC
95 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
96 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
30c23e74 97
16a76184 98 { kEventClassWindow , kEventWindowShown } ,
851b3a88
SC
99 { kEventClassWindow , kEventWindowActivated } ,
100 { kEventClassWindow , kEventWindowDeactivated } ,
101 { kEventClassWindow , kEventWindowBoundsChanging } ,
102 { kEventClassWindow , kEventWindowBoundsChanged } ,
103 { kEventClassWindow , kEventWindowClose } ,
4488a1d3 104 { kEventClassWindow , kEventWindowGetRegion } ,
851b3a88 105
a979e444
DS
106 // we have to catch these events on the toplevel window level,
107 // as controls don't get the raw mouse events anymore
902725ee 108
851b3a88
SC
109 { kEventClassMouse , kEventMouseDown } ,
110 { kEventClassMouse , kEventMouseUp } ,
de127c69 111 { kEventClassMouse , kEventMouseWheelMoved } ,
851b3a88
SC
112 { kEventClassMouse , kEventMouseMoved } ,
113 { kEventClassMouse , kEventMouseDragged } ,
851b3a88
SC
114} ;
115
c5c9378c
SC
116static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
117{
118 OSStatus result = eventNotHandledErr ;
b3bd613a 119 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
a979e444 120 // FindFocus does not return the actual focus window, but the enclosing window
b3bd613a 121 wxWindow* focus = wxWindow::DoFindFocus();
92a7272f 122 if ( focus == NULL )
ccf1ef0f 123 focus = (wxTopLevelWindowMac*) data ;
52c3df99 124
19fc48c6 125 unsigned char charCode ;
f3097709 126 wxChar uniChar[2] ;
670f9935
WS
127 uniChar[0] = 0;
128 uniChar[1] = 0;
129
1542ea39 130 UInt32 keyCode ;
c5c9378c 131 UInt32 modifiers ;
e40298d5
JS
132 Point point ;
133 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
134
2d17efa9 135#if wxUSE_UNICODE
7cd7bc23 136 ByteCount dataSize = 0 ;
a979e444 137 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
2d17efa9
SC
138 {
139 UniChar buf[2] ;
f3097709 140 int numChars = dataSize / sizeof( UniChar) + 1;
902725ee 141
2d17efa9 142 UniChar* charBuf = buf ;
902725ee 143
f3097709
SC
144 if ( numChars * 2 > 4 )
145 charBuf = new UniChar[ numChars ] ;
2d17efa9 146 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
670f9935 147 charBuf[ numChars - 1 ] = 0;
a979e444 148
2d17efa9
SC
149#if SIZEOF_WCHAR_T == 2
150 uniChar = charBuf[0] ;
151#else
d9d488cf 152 wxMBConvUTF16 converter ;
0cc00f4f 153 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
902725ee 154#endif
a979e444 155
f3097709 156 if ( numChars * 2 > 4 )
2d17efa9
SC
157 delete[] charBuf ;
158 }
159#endif
160
a979e444 161 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
52c3df99
DS
162 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
163 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
164 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
902725ee 165
e40298d5 166 UInt32 message = (keyCode << 8) + charCode;
a979e444 167 switch ( GetEventKind( event ) )
e40298d5
JS
168 {
169 case kEventRawKeyRepeat :
170 case kEventRawKeyDown :
e40298d5 171 {
404f1d80
SC
172 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
173 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
174 wxTheApp->MacSetCurrentEvent( event , handler ) ;
902725ee 175 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
f3097709 176 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
404f1d80
SC
177 {
178 result = noErr ;
179 }
180 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
e40298d5
JS
181 }
182 break ;
52c3df99 183
e40298d5 184 case kEventRawKeyUp :
902725ee 185 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
f3097709 186 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
e40298d5
JS
187 {
188 result = noErr ;
189 }
190 break ;
52c3df99 191
e40298d5
JS
192 case kEventRawKeyModifiersChanged :
193 {
194 wxKeyEvent event(wxEVT_KEY_DOWN);
195
196 event.m_shiftDown = modifiers & shiftKey;
197 event.m_controlDown = modifiers & controlKey;
198 event.m_altDown = modifiers & optionKey;
199 event.m_metaDown = modifiers & cmdKey;
a979e444
DS
200 event.m_x = point.h;
201 event.m_y = point.v;
202
2d17efa9 203#if wxUSE_UNICODE
0cc00f4f 204 event.m_uniChar = uniChar[0] ;
2d17efa9 205#endif
a979e444 206
687706f5 207 event.SetTimestamp(when);
e40298d5
JS
208 event.SetEventObject(focus);
209
902725ee 210 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
e40298d5
JS
211 {
212 event.m_keyCode = WXK_CONTROL ;
213 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
214 focus->GetEventHandler()->ProcessEvent( event ) ;
215 }
902725ee 216 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
e40298d5
JS
217 {
218 event.m_keyCode = WXK_SHIFT ;
219 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
220 focus->GetEventHandler()->ProcessEvent( event ) ;
221 }
902725ee 222 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
e40298d5
JS
223 {
224 event.m_keyCode = WXK_ALT ;
225 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
226 focus->GetEventHandler()->ProcessEvent( event ) ;
227 }
902725ee 228 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
b7aec135
SC
229 {
230 event.m_keyCode = WXK_COMMAND ;
231 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
232 focus->GetEventHandler()->ProcessEvent( event ) ;
233 }
a979e444 234
2a1f999f 235 wxApp::s_lastModifiers = modifiers ;
e40298d5 236 }
902725ee 237 break ;
52c3df99
DS
238
239 default:
240 break;
e40298d5 241 }
851b3a88
SC
242
243 return result ;
244}
245
facd6764 246// we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
52c3df99 247// for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
facd6764 248// mouse down at all
52c3df99 249//
facd6764 250// This handler can also be called from app level where data (ie target window) may be null or a non wx window
1542ea39 251
facd6764 252wxWindow* g_MacLastWindow = NULL ;
a3195b73 253
bdf895fd 254EventMouseButton g_lastButton = 0 ;
8736cc48 255bool g_lastButtonWasFakeRight = false ;
86a9144f 256
bdf895fd 257void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
facd6764
SC
258{
259 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
260 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
67245665 261
6239ee05 262 // these parameters are not given for all events
facd6764 263 EventMouseButton button = 0 ;
902725ee 264 UInt32 clickCount = 0 ;
6239ee05
SC
265 UInt32 mouseChord = 0;
266
a979e444
DS
267 cEvent.GetParameter<EventMouseButton>( kEventParamMouseButton, typeMouseButton , &button ) ;
268 cEvent.GetParameter<UInt32>( kEventParamClickCount, typeUInt32 , &clickCount ) ;
6239ee05
SC
269 // the chord is the state of the buttons pressed currently
270 cEvent.GetParameter<UInt32>( kEventParamMouseChord, typeUInt32 , &mouseChord ) ;
facd6764
SC
271
272 wxevent.m_x = screenMouseLocation.h;
273 wxevent.m_y = screenMouseLocation.v;
274 wxevent.m_shiftDown = modifiers & shiftKey;
275 wxevent.m_controlDown = modifiers & controlKey;
276 wxevent.m_altDown = modifiers & optionKey;
277 wxevent.m_metaDown = modifiers & cmdKey;
8866abbb 278 wxevent.m_clickCount = clickCount;
facd6764 279 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
52c3df99 280
8736cc48
SC
281 // a control click is interpreted as a right click
282 bool thisButtonIsFakeRight = false ;
facd6764 283 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
8736cc48 284 {
facd6764 285 button = kEventMouseButtonSecondary ;
8736cc48
SC
286 thisButtonIsFakeRight = true ;
287 }
6239ee05 288
b343fb9f 289 // otherwise we report double clicks by connecting a left click with a ctrl-left click
bdf895fd 290 if ( clickCount > 1 && button != g_lastButton )
b343fb9f 291 clickCount = 1 ;
902725ee 292
86a9144f
SC
293 // we must make sure that our synthetic 'right' button corresponds in
294 // mouse down, moved and mouse up, and does not deliver a right down and left up
902725ee 295
86a9144f 296 if ( cEvent.GetKind() == kEventMouseDown )
8736cc48 297 {
bdf895fd 298 g_lastButton = button ;
8736cc48
SC
299 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
300 }
902725ee 301
4e4e6dce 302 if ( button == 0 )
8736cc48 303 {
bdf895fd 304 g_lastButton = 0 ;
8736cc48
SC
305 g_lastButtonWasFakeRight = false ;
306 }
307 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
bdf895fd 308 button = g_lastButton ;
facd6764 309
6239ee05
SC
310 // Adjust the chord mask to remove the primary button and add the
311 // secondary button. It is possible that the secondary button is
312 // already pressed, e.g. on a mouse connected to a laptop, but this
313 // possibility is ignored here:
314 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
315 mouseChord = ((mouseChord & ~1U) | 2U);
52c3df99 316
6239ee05
SC
317 if(mouseChord & 1U)
318 wxevent.m_leftDown = true ;
319 if(mouseChord & 2U)
facd6764 320 wxevent.m_rightDown = true ;
6239ee05 321 if(mouseChord & 4U)
facd6764 322 wxevent.m_middleDown = true ;
52c3df99 323
90ff87d7
SC
324 // translate into wx types
325 switch ( cEvent.GetKind() )
facd6764 326 {
90ff87d7 327 case kEventMouseDown :
52c3df99 328 switch ( button )
90ff87d7
SC
329 {
330 case kEventMouseButtonPrimary :
a979e444 331 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
90ff87d7 332 break ;
52c3df99 333
90ff87d7
SC
334 case kEventMouseButtonSecondary :
335 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
336 break ;
52c3df99 337
90ff87d7 338 case kEventMouseButtonTertiary :
a979e444 339 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
90ff87d7 340 break ;
52c3df99
DS
341
342 default:
343 break ;
90ff87d7
SC
344 }
345 break ;
52c3df99 346
90ff87d7 347 case kEventMouseUp :
52c3df99 348 switch ( button )
90ff87d7
SC
349 {
350 case kEventMouseButtonPrimary :
351 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
352 break ;
52c3df99 353
90ff87d7
SC
354 case kEventMouseButtonSecondary :
355 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
356 break ;
52c3df99 357
90ff87d7
SC
358 case kEventMouseButtonTertiary :
359 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
360 break ;
52c3df99
DS
361
362 default:
363 break ;
90ff87d7
SC
364 }
365 break ;
52c3df99 366
2cce9b11 367 case kEventMouseWheelMoved :
52c3df99 368 {
a979e444 369 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
facd6764 370
74fa3253 371 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
7cd7bc23 372 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
facd6764
SC
373
374 wxevent.m_wheelRotation = delta;
375 wxevent.m_wheelDelta = 1;
376 wxevent.m_linesPerAction = 1;
74fa3253
SC
377 if ( axis == kEventMouseWheelAxisX )
378 wxevent.m_wheelAxis = 1;
52c3df99
DS
379 }
380 break ;
381
6239ee05
SC
382 case kEventMouseEntered :
383 case kEventMouseExited :
384 case kEventMouseDragged :
385 case kEventMouseMoved :
a979e444 386 wxevent.SetEventType( wxEVT_MOTION ) ;
6239ee05
SC
387 break;
388 default :
90ff87d7 389 break ;
902725ee 390 }
facd6764
SC
391}
392
fbfb8bcc 393ControlRef wxMacFindSubControl( wxTopLevelWindowMac* toplevelWindow, const Point& location , ControlRef superControl , ControlPartCode *outPart )
facd6764
SC
394{
395 if ( superControl )
396 {
397 UInt16 childrenCount = 0 ;
a979e444
DS
398 ControlHandle sibling ;
399 Rect r ;
902725ee 400 OSStatus err = CountSubControls( superControl , &childrenCount ) ;
facd6764
SC
401 if ( err == errControlIsNotEmbedder )
402 return NULL ;
a979e444 403
facd6764 404 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
902725ee 405
facd6764
SC
406 for ( UInt16 i = childrenCount ; i >=1 ; --i )
407 {
facd6764
SC
408 err = GetIndexedSubControl( superControl , i , & sibling ) ;
409 if ( err == errControlIsNotEmbedder )
410 return NULL ;
902725ee 411
facd6764
SC
412 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
413 if ( IsControlVisible( sibling ) )
414 {
d6fd667b 415 UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
facd6764 416 if ( MacPtInRect( location , &r ) )
de127c69 417 {
789ae0cf 418 ControlHandle child = wxMacFindSubControl( toplevelWindow , location , sibling , outPart ) ;
facd6764 419 if ( child )
a979e444 420 {
facd6764 421 return child ;
a979e444 422 }
facd6764 423 else
de127c69 424 {
d6fd667b 425 Point testLocation = location ;
789ae0cf 426
f21449ae 427 if ( toplevelWindow )
789ae0cf
SC
428 {
429 testLocation.h -= r.left ;
430 testLocation.v -= r.top ;
431 }
902725ee 432
d6fd667b 433 *outPart = TestControl( sibling , testLocation ) ;
a979e444 434
facd6764 435 return sibling ;
de127c69
GD
436 }
437 }
facd6764 438 }
e40298d5
JS
439 }
440 }
52c3df99 441
facd6764
SC
442 return NULL ;
443}
1542ea39 444
3c393c0a
SC
445
446#define NEW_CAPTURE_HANDLING 1
447
89954433
VZ
448pascal OSStatus
449wxMacTopLevelMouseEventHandler(EventHandlerCallRef WXUNUSED(handler),
450 EventRef event,
451 void *data)
facd6764 452{
789ae0cf 453 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
902725ee 454
facd6764
SC
455 OSStatus result = eventNotHandledErr ;
456
457 wxMacCarbonEvent cEvent( event ) ;
902725ee 458
facd6764
SC
459 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
460 Point windowMouseLocation = screenMouseLocation ;
461
30c23e74 462 WindowRef window = NULL;
facd6764
SC
463 short windowPart = ::FindWindow(screenMouseLocation, &window);
464
465 wxWindow* currentMouseWindow = NULL ;
d88ffaaa 466 ControlRef control = NULL ;
902725ee 467
3c393c0a
SC
468#if NEW_CAPTURE_HANDLING
469 if ( wxApp::s_captureWindow )
470 {
471 window = (WindowRef) wxApp::s_captureWindow->MacGetTopLevelWindowRef() ;
8fc754bf 472 windowPart = inContent ;
3c393c0a
SC
473 }
474#endif
fb5246be 475
facd6764
SC
476 if ( window )
477 {
7cd7bc23 478 wxMacGlobalToLocal( window, &windowMouseLocation ) ;
facd6764 479
3c393c0a
SC
480 if ( wxApp::s_captureWindow
481#if !NEW_CAPTURE_HANDLING
482 && wxApp::s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent
483#endif
484 )
facd6764 485 {
2a1f999f 486 currentMouseWindow = wxApp::s_captureWindow ;
facd6764
SC
487 }
488 else if ( (IsWindowActive(window) && windowPart == inContent) )
489 {
490 ControlPartCode part ;
6239ee05 491 control = FindControlUnderMouse( windowMouseLocation , window , &part ) ;
d88ffaaa 492 // if there is no control below the mouse position, send the event to the toplevel window itself
b19bf058 493 if ( control == 0 )
a979e444 494 {
b19bf058 495 currentMouseWindow = (wxWindow*) data ;
a979e444 496 }
b19bf058 497 else
b16f4bfa 498 {
6239ee05
SC
499 currentMouseWindow = (wxWindow*) wxFindControlFromMacControl( control ) ;
500#ifndef __WXUNIVERSAL__
8198ae50 501 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
b16f4bfa 502 {
179e085f 503#if wxUSE_TOOLBAR
902725ee
WS
504 // for wxToolBar to function we have to send certaint events to it
505 // instead of its children (wxToolBarTools)
b16f4bfa
SC
506 ControlRef parent ;
507 GetSuperControl(control, &parent );
6239ee05 508 wxWindow *wxParent = (wxWindow*) wxFindControlFromMacControl( parent ) ;
b16f4bfa
SC
509 if ( wxParent && wxParent->IsKindOf( CLASSINFO( wxToolBar ) ) )
510 currentMouseWindow = wxParent ;
179e085f 511#endif
b16f4bfa 512 }
6239ee05 513#endif
b16f4bfa 514 }
f0756835
VZ
515
516 // disabled windows must not get any input messages
517 if ( currentMouseWindow && !currentMouseWindow->MacIsReallyEnabled() )
518 currentMouseWindow = NULL;
902725ee 519 }
facd6764 520 }
902725ee 521
facd6764
SC
522 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
523 SetupMouseEvent( wxevent , cEvent ) ;
524
525 // handle all enter / leave events
902725ee 526
facd6764
SC
527 if ( currentMouseWindow != g_MacLastWindow )
528 {
529 if ( g_MacLastWindow )
530 {
531 wxMouseEvent eventleave(wxevent);
532 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
533 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
534 eventleave.SetEventObject( g_MacLastWindow ) ;
d4bd6c97 535 wxevent.SetId( g_MacLastWindow->GetId() ) ;
a979e444 536
facd6764
SC
537#if wxUSE_TOOLTIPS
538 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
a979e444
DS
539#endif
540
facd6764
SC
541 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
542 }
52c3df99 543
facd6764
SC
544 if ( currentMouseWindow )
545 {
546 wxMouseEvent evententer(wxevent);
547 evententer.SetEventType( wxEVT_ENTER_WINDOW );
548 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
549 evententer.SetEventObject( currentMouseWindow ) ;
d4bd6c97 550 wxevent.SetId( currentMouseWindow->GetId() ) ;
a979e444 551
facd6764 552#if wxUSE_TOOLTIPS
a979e444
DS
553 wxToolTip::RelayEvent( currentMouseWindow , evententer );
554#endif
555
facd6764
SC
556 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
557 }
52c3df99 558
facd6764
SC
559 g_MacLastWindow = currentMouseWindow ;
560 }
902725ee 561
facd6764
SC
562 if ( windowPart == inMenuBar )
563 {
564 // special case menu bar, as we are having a low-level runloop we must do it ourselves
565 if ( cEvent.GetKind() == kEventMouseDown )
566 {
567 ::MenuSelect( screenMouseLocation ) ;
6239ee05 568 ::HiliteMenu(0);
facd6764
SC
569 result = noErr ;
570 }
52c3df99 571 }
facd6764
SC
572 else if ( currentMouseWindow )
573 {
e10e9469 574 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
62ade180
RR
575
576 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
902725ee 577
facd6764 578 wxevent.SetEventObject( currentMouseWindow ) ;
d4bd6c97 579 wxevent.SetId( currentMouseWindow->GetId() ) ;
facd6764 580
facd6764 581 // make tooltips current
902725ee 582
a979e444 583#if wxUSE_TOOLTIPS
f124c908 584 if ( wxevent.GetEventType() == wxEVT_MOTION )
a979e444
DS
585 wxToolTip::RelayEvent( currentMouseWindow , wxevent );
586#endif
587
facd6764 588 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
62ade180 589 {
902725ee 590 if ((currentMouseWindowParent != NULL) &&
62ade180
RR
591 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
592 currentMouseWindow = NULL;
902725ee 593
facd6764 594 result = noErr;
62ade180 595 }
30e0b1c9
SC
596 else
597 {
f90387b4
VZ
598 // if the user code did _not_ handle the event, then perform the
599 // default processing
600 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
601 {
602 // ... that is set focus to this window
ad02525d 603 if (currentMouseWindow->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow)
f90387b4
VZ
604 currentMouseWindow->SetFocus();
605 }
606
30e0b1c9
SC
607 ControlPartCode dummyPart ;
608 // if built-in find control is finding the wrong control (ie static box instead of overlaid
609 // button, we cannot let the standard handler do its job, but must handle manually
610
9d463591 611 if ( cEvent.GetKind() == kEventMouseDown )
30e0b1c9 612 {
902725ee 613 if ( currentMouseWindow->MacIsReallyEnabled() )
7ce1a4e1
SC
614 {
615 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
616 Point clickLocation = windowMouseLocation ;
789ae0cf 617
f21449ae 618 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
789ae0cf 619
7ce1a4e1
SC
620 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
621 modifiers , (ControlActionUPP ) -1 ) ;
902725ee
WS
622
623 if ((currentMouseWindowParent != NULL) &&
62ade180 624 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
a979e444 625 {
e10e9469 626 currentMouseWindow = NULL;
a979e444 627 }
7ce1a4e1 628 }
a979e444 629
30e0b1c9
SC
630 result = noErr ;
631 }
632 }
52c3df99 633
2a1f999f 634 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
facd6764 635 {
2a1f999f 636 wxApp::s_captureWindow = NULL ;
facd6764
SC
637 // update cursor ?
638 }
6d63e2fc
SC
639
640 // update cursor
902725ee 641
6d63e2fc
SC
642 wxWindow* cursorTarget = currentMouseWindow ;
643 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
644
52c3df99 645 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
6d63e2fc
SC
646 {
647 cursorTarget = cursorTarget->GetParent() ;
648 if ( cursorTarget )
e10e9469 649 cursorPoint += cursorTarget->GetPosition();
6d63e2fc
SC
650 }
651
f124c908
VZ
652 }
653 else // currentMouseWindow == NULL
d88ffaaa
SC
654 {
655 // don't mess with controls we don't know about
656 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
657 // so we try sending them the correct control directly
e4b7e0b4 658 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
d88ffaaa
SC
659 {
660 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
661 Point clickLocation = windowMouseLocation ;
f124c908 662#if TARGET_API_MAC_OSX
f21449ae
SC
663 HIPoint hiPoint ;
664 hiPoint.x = clickLocation.h ;
665 hiPoint.y = clickLocation.v ;
666 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
667 clickLocation.h = (int)hiPoint.x ;
668 clickLocation.v = (int)hiPoint.y ;
f124c908 669#endif // TARGET_API_MAC_OSX
52c3df99
DS
670
671 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
d88ffaaa
SC
672 result = noErr ;
673 }
674 }
a979e444 675
facd6764 676 return result ;
851b3a88 677}
facd6764 678
89954433
VZ
679static pascal OSStatus
680wxMacTopLevelWindowEventHandler(EventHandlerCallRef WXUNUSED(handler),
681 EventRef event,
682 void *data)
851b3a88
SC
683{
684 OSStatus result = eventNotHandledErr ;
851b3a88 685
facd6764 686 wxMacCarbonEvent cEvent( event ) ;
902725ee 687
facd6764 688 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
e40298d5 689 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 690
52c3df99 691 switch ( GetEventKind( event ) )
e40298d5 692 {
e40298d5 693 case kEventWindowActivated :
facd6764
SC
694 {
695 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
facd6764
SC
696 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
697 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
698 wxevent.SetEventObject(toplevelWindow);
699 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 700 // we still sending an eventNotHandledErr in order to allow for default processing
facd6764 701 }
52c3df99
DS
702 break ;
703
e40298d5 704 case kEventWindowDeactivated :
facd6764
SC
705 {
706 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
707 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
708 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
709 wxevent.SetEventObject(toplevelWindow);
710 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 711 // we still sending an eventNotHandledErr in order to allow for default processing
facd6764 712 }
52c3df99
DS
713 break ;
714
902725ee 715 case kEventWindowShown :
902725ee
WS
716 toplevelWindow->Refresh() ;
717 result = noErr ;
718 break ;
52c3df99 719
e40298d5 720 case kEventWindowClose :
52c3df99 721 toplevelWindow->Close() ;
e40298d5
JS
722 result = noErr ;
723 break ;
52c3df99 724
e40298d5 725 case kEventWindowBoundsChanged :
facd6764 726 {
52c3df99 727 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
facd6764
SC
728 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
729 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
730 if ( attributes & kWindowBoundsChangeSizeChanged )
e40298d5 731 {
6239ee05 732#ifndef __WXUNIVERSAL__
facd6764
SC
733 // according to the other ports we handle this within the OS level
734 // resize event, not within a wxSizeEvent
735 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
736 if ( frame )
737 {
6f02a879 738 frame->PositionBars();
facd6764 739 }
6239ee05 740#endif
facd6764
SC
741 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
742 event.SetEventObject( toplevelWindow ) ;
851b3a88 743
facd6764 744 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
5e533062 745 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
e40298d5 746 }
52c3df99 747
facd6764
SC
748 if ( attributes & kWindowBoundsChangeOriginChanged )
749 {
750 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
751 event.SetEventObject( toplevelWindow ) ;
752 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
753 }
52c3df99 754
facd6764 755 result = noErr ;
facd6764 756 }
52c3df99
DS
757 break ;
758
f81bfef9 759 case kEventWindowBoundsChanging :
facd6764
SC
760 {
761 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
762 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
83901ec2 763
facd6764
SC
764 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
765 {
29281095
SC
766 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
767 int left , top , right , bottom ;
768 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
52c3df99
DS
769
770 wxRect r(
771 newRect.left - left,
772 newRect.top - top,
773 newRect.right - newRect.left + left + right,
774 newRect.bottom - newRect.top + top + bottom ) ;
775
facd6764
SC
776 // this is a EVT_SIZING not a EVT_SIZE type !
777 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
778 wxevent.SetEventObject( toplevelWindow ) ;
779 wxRect adjustR = r ;
780 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
facd6764 781 adjustR = wxevent.GetRect() ;
29281095 782
facd6764
SC
783 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
784 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
2c25bd55 785 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
facd6764
SC
786 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
787 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
788 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
2c25bd55 789 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
facd6764 790 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
4a5f2351 791 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
facd6764 792 if ( !EqualRect( &newRect , &adjustedRect ) )
1f919f38 793 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
4817190d 794 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
f81bfef9 795 }
facd6764 796
902725ee 797 result = noErr ;
facd6764 798 }
52c3df99
DS
799 break ;
800
4488a1d3
VZ
801 case kEventWindowGetRegion :
802 {
803 if ( toplevelWindow->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
804 {
805 WindowRegionCode windowRegionCode ;
806
807 // Fetch the region code that is being queried
808 GetEventParameter( event,
809 kEventParamWindowRegionCode,
810 typeWindowRegionCode, NULL,
811 sizeof windowRegionCode, NULL,
812 &windowRegionCode ) ;
813
814 // If it is the opaque region code then set the
815 // region to empty and return noErr to stop event
816 // propagation
817 if ( windowRegionCode == kWindowOpaqueRgn ) {
818 RgnHandle region;
819 GetEventParameter( event,
820 kEventParamRgnHandle,
821 typeQDRgnHandle, NULL,
822 sizeof region, NULL,
823 &region) ;
824 SetEmptyRgn(region) ;
825 result = noErr ;
826 }
827 }
828 }
829 break ;
830
e40298d5
JS
831 default :
832 break ;
833 }
52c3df99 834
e40298d5 835 return result ;
851b3a88
SC
836}
837
ffc75ee4
SC
838// mix this in from window.cpp
839pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
840
facd6764 841pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
842{
843 OSStatus result = eventNotHandledErr ;
844
845 switch ( GetEventClass( event ) )
846 {
ffc75ee4
SC
847 case kEventClassTextInput :
848 result = wxMacUnicodeTextEventHandler( handler, event , data ) ;
849 break ;
850
c5c9378c 851 case kEventClassKeyboard :
e40298d5 852 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 853 break ;
52c3df99 854
851b3a88 855 case kEventClassWindow :
facd6764 856 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
e40298d5 857 break ;
52c3df99 858
851b3a88 859 case kEventClassMouse :
facd6764 860 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
e40298d5 861 break ;
52c3df99 862
851b3a88
SC
863 default :
864 break ;
865 }
52c3df99 866
851b3a88
SC
867 return result ;
868}
869
facd6764 870DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
851b3a88 871
5f0b2f22
SC
872// ---------------------------------------------------------------------------
873// wxWindowMac utility functions
874// ---------------------------------------------------------------------------
875
876// Find an item given the Macintosh Window Reference
877
71f2fb52
RN
878WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
879
880static MacWindowMap wxWinMacWindowList;
881
882wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
883{
884 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
885
886 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
887}
888
889void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
890void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
891{
892 // adding NULL WindowRef is (first) surely a result of an error and
893 // nothing else :-)
894 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
895
896 wxWinMacWindowList[inWindowRef] = win;
897}
898
899void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
900void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
901{
902 MacWindowMap::iterator it;
903 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
904 {
905 if ( it->second == win )
906 {
907 wxWinMacWindowList.erase(it);
908 break;
909 }
910 }
911}
5f0b2f22 912
a15eb0a5
SC
913// ----------------------------------------------------------------------------
914// wxTopLevelWindowMac creation
915// ----------------------------------------------------------------------------
916
245f3581 917wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 918
902725ee 919typedef struct
c5985061
SC
920{
921 wxPoint m_position ;
902725ee 922 wxSize m_size ;
f3b2e2d7 923 bool m_wasResizable ;
716d0327 924} FullScreenData ;
c5985061 925
a15eb0a5
SC
926void wxTopLevelWindowMac::Init()
927{
928 m_iconized =
902725ee 929 m_maximizeOnShow = false;
6a17ca35 930 m_macWindow = NULL ;
52c3df99 931
7c091673 932 m_macEventHandler = NULL ;
c5985061 933 m_macFullScreenData = NULL ;
a15eb0a5
SC
934}
935
6239ee05 936wxMacDeferredWindowDeleter::wxMacDeferredWindowDeleter( WindowRef windowRef )
118f012e 937{
6239ee05
SC
938 m_macWindow = windowRef ;
939}
52c3df99 940
6239ee05
SC
941wxMacDeferredWindowDeleter::~wxMacDeferredWindowDeleter()
942{
943 UMADisposeWindow( (WindowRef) m_macWindow ) ;
944}
118f012e 945
a15eb0a5
SC
946bool wxTopLevelWindowMac::Create(wxWindow *parent,
947 wxWindowID id,
948 const wxString& title,
949 const wxPoint& pos,
950 const wxSize& size,
951 long style,
952 const wxString& name)
953{
954 // init our fields
955 Init();
956
957 m_windowStyle = style;
958
52c3df99 959 SetName( name );
a15eb0a5
SC
960
961 m_windowId = id == -1 ? NewControlId() : id;
fb5246be 962 wxWindow::SetLabel( title ) ;
a15eb0a5 963
6239ee05 964 DoMacCreateRealWindow( parent, title, pos , size , style , name ) ;
facd6764 965
94d1d0f4 966 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
902725ee 967
5bd78bbc
JS
968 if (GetExtraStyle() & wxFRAME_EX_METAL)
969 MacSetMetalAppearance(true);
94d1d0f4 970
a15eb0a5
SC
971 wxTopLevelWindows.Append(this);
972
973 if ( parent )
974 parent->AddChild(this);
975
902725ee 976 return true;
a15eb0a5
SC
977}
978
979wxTopLevelWindowMac::~wxTopLevelWindowMac()
980{
6a17ca35
SC
981 if ( m_macWindow )
982 {
c81c6d54 983#if wxUSE_TOOLTIPS
6a17ca35 984 wxToolTip::NotifyWindowDelete(m_macWindow) ;
c81c6d54 985#endif
118f012e 986 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 987 }
1542ea39 988
7c091673
SC
989 if ( m_macEventHandler )
990 {
991 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
992 m_macEventHandler = NULL ;
993 }
851b3a88 994
5f0b2f22
SC
995 wxRemoveMacWindowAssociation( this ) ;
996
a15eb0a5
SC
997 if ( wxModelessWindows.Find(this) )
998 wxModelessWindows.DeleteObject(this);
902725ee 999
c5985061
SC
1000 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1001 delete data ;
1002 m_macFullScreenData = NULL ;
09ac36f8
SC
1003
1004 // avoid dangling refs
1005 if ( s_macDeactivateWindow == this )
1006 s_macDeactivateWindow = NULL;
a15eb0a5
SC
1007}
1008
1009
1010// ----------------------------------------------------------------------------
1011// wxTopLevelWindowMac maximize/minimize
1012// ----------------------------------------------------------------------------
1013
1014void wxTopLevelWindowMac::Maximize(bool maximize)
1015{
6643c354
SC
1016 Point idealSize = { 0 , 0 } ;
1017 if ( maximize )
1018 {
7cd7bc23
SC
1019#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1020 HIRect bounds ;
1021 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay,kHICoordSpace72DPIGlobal,
1022 &bounds);
1023 idealSize.h = bounds.size.width;
1024 idealSize.v = bounds.size.height;
1025#else
6643c354
SC
1026 Rect rect ;
1027 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
1028 idealSize.h = rect.right - rect.left ;
1029 idealSize.v = rect.bottom - rect.top ;
7cd7bc23 1030#endif
6643c354
SC
1031 }
1032 ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
a15eb0a5
SC
1033}
1034
1035bool wxTopLevelWindowMac::IsMaximized() const
1036{
a979e444 1037 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
1038}
1039
1040void wxTopLevelWindowMac::Iconize(bool iconize)
1041{
52c3df99
DS
1042 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1043 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
1044}
1045
1046bool wxTopLevelWindowMac::IsIconized() const
1047{
b7aec135 1048 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
1049}
1050
1051void wxTopLevelWindowMac::Restore()
1052{
902725ee 1053 if ( IsMaximized() )
699b6af4 1054 Maximize(false);
902725ee 1055 else if ( IsIconized() )
699b6af4 1056 Iconize(false);
a15eb0a5
SC
1057}
1058
1059// ----------------------------------------------------------------------------
1060// wxTopLevelWindowMac misc
1061// ----------------------------------------------------------------------------
1062
facd6764
SC
1063wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1064{
52c3df99 1065 return wxPoint(0, 0) ;
facd6764
SC
1066}
1067
6239ee05
SC
1068#ifndef __WXUNIVERSAL__
1069void wxTopLevelWindowMac::SetIcons( const wxIconBundle& icons )
1070{
1071// { SetIcon( icons.GetIcon( -1 ) ); }
1072}
1073#endif
1074
facd6764 1075void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
902725ee 1076{
facd6764
SC
1077 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1078
1079 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1080 {
1081 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1082 }
1083}
1084
6239ee05
SC
1085void wxTopLevelWindowMacInstallTopLevelWindowEventHandler(WindowRef window, EventHandlerRef* handler, void *ref)
1086{
1087 InstallWindowEventHandler(window, GetwxMacTopLevelEventHandlerUPP(),
1088 GetEventTypeCount(eventList), eventList, ref, handler );
1089}
1090
902725ee 1091void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
949cb163
SC
1092{
1093 if ( m_macEventHandler != NULL )
1094 {
1095 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1096 }
6239ee05 1097 wxTopLevelWindowMacInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow),(EventHandlerRef *)&m_macEventHandler,this);
949cb163
SC
1098}
1099
a979e444
DS
1100void wxTopLevelWindowMac::MacCreateRealWindow(
1101 const wxString& title,
1102 const wxPoint& pos,
1103 const wxSize& size,
1104 long style,
1105 const wxString& name )
6239ee05
SC
1106{
1107 DoMacCreateRealWindow( NULL, title, pos, size, style, name );
1108}
1109
1110void wxTopLevelWindowMac::DoMacCreateRealWindow(
1111 wxWindow* parent,
1112 const wxString& title,
1113 const wxPoint& pos,
1114 const wxSize& size,
1115 long style,
1116 const wxString& name )
5f0b2f22 1117{
47ac4f9b 1118 OSStatus err = noErr ;
e40298d5
JS
1119 SetName(name);
1120 m_windowStyle = style;
902725ee 1121 m_isShown = false;
1542ea39 1122
e40298d5 1123 // create frame.
facd6764
SC
1124 int x = (int)pos.x;
1125 int y = (int)pos.y;
902725ee 1126
52c3df99 1127 Rect theBoundsRect;
5a486641
SC
1128 wxRect display = wxGetClientDisplayRect() ;
1129
1130 if ( x == wxDefaultPosition.x )
1131 x = display.x ;
902725ee 1132
5a486641
SC
1133 if ( y == wxDefaultPosition.y )
1134 y = display.y ;
1542ea39 1135
facd6764
SC
1136 int w = WidthDefault(size.x);
1137 int h = HeightDefault(size.y);
1542ea39 1138
facd6764 1139 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 1140
5f0b2f22 1141 // translate the window attributes in the appropriate window class and attributes
5f0b2f22
SC
1142 WindowClass wclass = 0;
1143 WindowAttributes attr = kWindowNoAttributes ;
e353795e 1144 WindowGroupRef group = NULL ;
6239ee05
SC
1145 bool activationScopeSet = false;
1146 WindowActivationScope activationScope = kWindowActivationScopeNone;
1542ea39 1147
f5744893 1148 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 1149 {
1542ea39 1150 if (
f5744893
SC
1151 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1152 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1153 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 1154 )
5f0b2f22 1155 {
6239ee05
SC
1156 if ( HasFlag( wxSTAY_ON_TOP ) )
1157 wclass = kUtilityWindowClass;
1158 else
1159 wclass = kFloatingWindowClass ;
52c3df99 1160
f5744893 1161 if ( HasFlag(wxTINY_CAPTION_VERT) )
f5744893 1162 attr |= kWindowSideTitlebarAttribute ;
f5744893
SC
1163 }
1164 else
1165 {
1166 wclass = kPlainWindowClass ;
6239ee05
SC
1167 activationScopeSet = true;
1168 activationScope = kWindowActivationScopeNone;
5f0b2f22
SC
1169 }
1170 }
a0232aa5
RD
1171 else if ( HasFlag( wxPOPUP_WINDOW ) )
1172 {
1173 // TEMPORARY HACK!
1174 // Until we've got a real wxPopupWindow class on wxMac make it a
1175 // little easier for wxFrame to be used to emulate it and workaround
1176 // the lack of wxPopupWindow.
1177 if ( HasFlag( wxBORDER_NONE ) )
1178 wclass = kHelpWindowClass ; // has no border
1179 else
1180 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1181 //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
89954433 1182 group = GetWindowGroupOfClass( // float above other windows
a0232aa5
RD
1183 kFloatingWindowClass) ;
1184 }
5f0b2f22
SC
1185 else if ( HasFlag( wxCAPTION ) )
1186 {
1542ea39 1187 wclass = kDocumentWindowClass ;
22e3c5bd 1188 attr |= kWindowInWindowMenuAttribute ;
5f0b2f22 1189 }
3e17bc3f
RN
1190 else if ( HasFlag( wxFRAME_DRAWER ) )
1191 {
1192 wclass = kDrawerWindowClass;
3e17bc3f 1193 }
5f0b2f22
SC
1194 else
1195 {
f5744893 1196 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 1197 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
1198 {
1199 wclass = kDocumentWindowClass ;
1200 }
6239ee05
SC
1201 else if ( HasFlag( wxNO_BORDER ) )
1202 {
1203 wclass = kSimpleWindowClass ;
1204 }
f5744893
SC
1205 else
1206 {
1207 wclass = kPlainWindowClass ;
1208 }
5f0b2f22 1209 }
1542ea39 1210
52c3df99 1211 if ( wclass != kPlainWindowClass )
5f0b2f22 1212 {
52c3df99
DS
1213 if ( HasFlag( wxMINIMIZE_BOX ) )
1214 attr |= kWindowCollapseBoxAttribute ;
1215
1216 if ( HasFlag( wxMAXIMIZE_BOX ) )
1217 attr |= kWindowFullZoomAttribute ;
1218
1219 if ( HasFlag( wxRESIZE_BORDER ) )
1220 attr |= kWindowResizableAttribute ;
1221
1222 if ( HasFlag( wxCLOSE_BOX) )
1223 attr |= kWindowCloseBoxAttribute ;
5f0b2f22 1224 }
03561a3c 1225 attr |= kWindowLiveResizeAttribute;
eb630bf1 1226
e353795e 1227 if ( HasFlag(wxSTAY_ON_TOP) )
e353795e 1228 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
6a7e6411 1229
a0232aa5
RD
1230 if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
1231 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
89954433 1232
6239ee05
SC
1233 if ( group == NULL && parent != NULL )
1234 {
1235 WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
1236 if( parenttlw )
1237 group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
1238 }
1239
f21449ae 1240 attr |= kWindowCompositingAttribute;
24fcd264
SC
1241#if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1242 attr |= kWindowFrameworkScaledAttribute;
d66c3960 1243#endif
902725ee 1244
6a7e6411
RD
1245 if ( HasFlag(wxFRAME_SHAPED) )
1246 {
1247 WindowDefSpec customWindowDefSpec;
1248 customWindowDefSpec.defType = kWindowDefProcPtr;
89954433 1249 customWindowDefSpec.u.defProc =
7cd7bc23
SC
1250#ifdef __LP64__
1251 (WindowDefUPP) wxShapedMacWindowDef;
1252#else
1253 NewWindowDefUPP(wxShapedMacWindowDef);
1254#endif
47ac4f9b 1255 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1256 attr, &theBoundsRect,
1257 (WindowRef*) &m_macWindow);
1258 }
1259 else
1260 {
47ac4f9b 1261 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1262 }
1263
e353795e
SC
1264 if ( err == noErr && m_macWindow != NULL && group != NULL )
1265 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1266
47ac4f9b 1267 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761 1268
832f330f 1269 // setup a separate group for each window, so that overlays can be handled easily
6239ee05
SC
1270
1271 WindowGroupRef overlaygroup = NULL;
1272 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
1273 verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
1274 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
1275
1276 if ( activationScopeSet )
1277 {
1278 verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
1279 }
89954433 1280
a979e444
DS
1281 // the create commands are only for content rect,
1282 // so we have to set the size again as structure bounds
f83b6761
SC
1283 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1284
facd6764
SC
1285 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1286 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1f1c8bd4 1287 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
902725ee 1288
f21449ae
SC
1289 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1290 // the content view, so we have to retrieve it explicitly
1291 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1292 m_peer->GetControlRefAddr() ) ;
1293 if ( !m_peer->Ok() )
789ae0cf 1294 {
f21449ae
SC
1295 // compatibility mode fallback
1296 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
789ae0cf 1297 }
52c3df99
DS
1298
1299 // the root control level handler
5ca0d812 1300 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1301
9dc1d180
JS
1302 // Causes the inner part of the window not to be metal
1303 // if the style is used before window creation.
1304#if 0 // TARGET_API_MAC_OSX
8ec0a8fa
SC
1305 if ( m_macUsesCompositing && m_macWindow != NULL )
1306 {
1307 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1308 MacSetMetalAppearance( true ) ;
1309 }
1310#endif
52c3df99 1311
893727e5
KO
1312#if TARGET_API_MAC_OSX
1313 if ( m_macWindow != NULL )
1314 {
1315 MacSetUnifiedAppearance( true ) ;
1316 }
1317#endif
1318
6239ee05
SC
1319 HIViewRef growBoxRef = 0 ;
1320 err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
1321 if ( err == noErr && growBoxRef != 0 )
1322 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
1323
73fe67bd 1324 // the frame window event handler
e40298d5 1325 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1326 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1327
14c96cf2
SC
1328 DoSetWindowVariant( m_windowVariant ) ;
1329
facd6764 1330 m_macFocus = NULL ;
6a7e6411
RD
1331
1332 if ( HasFlag(wxFRAME_SHAPED) )
1333 {
1334 // default shape matches the window size
52c3df99
DS
1335 wxRegion rgn( 0, 0, w, h );
1336 SetShape( rgn );
6a7e6411 1337 }
3dfafdb9
RD
1338
1339 wxWindowCreateEvent event(this);
1340 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1341}
1342
5d2e69e8 1343void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1344{
5d2e69e8 1345 wxWindow::ClearBackground() ;
5f0b2f22
SC
1346}
1347
5f0b2f22
SC
1348// Raise the window to the top of the Z order
1349void wxTopLevelWindowMac::Raise()
1350{
7f3c339c 1351 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1352}
1353
1354// Lower the window to the bottom of the Z order
1355void wxTopLevelWindowMac::Lower()
1356{
76a5e5d2 1357 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1358}
1359
245f3581
DE
1360void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1361{
52c3df99 1362 if (s_macDeactivateWindow)
245f3581 1363 {
f02a4ffa
VZ
1364 wxLogTrace(TRACE_ACTIVATE,
1365 wxT("Doing delayed deactivation of %p"),
1366 s_macDeactivateWindow);
52c3df99 1367
245f3581
DE
1368 s_macDeactivateWindow->MacActivate(timestamp, false);
1369 }
1370}
1371
89954433 1372void wxTopLevelWindowMac::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
5f0b2f22 1373{
f02a4ffa 1374 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
17f8d2a7 1375
52c3df99
DS
1376 if (s_macDeactivateWindow == this)
1377 s_macDeactivateWindow = NULL;
1378
245f3581 1379 MacDelayedDeactivation(timestamp);
8ab50549 1380 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1381}
1382
1383void wxTopLevelWindowMac::SetTitle(const wxString& title)
1384{
fb5246be 1385 wxWindow::SetLabel( title ) ;
16a76184 1386 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1387}
1388
d4bd6c97 1389wxString wxTopLevelWindowMac::GetTitle() const
fb5246be
WS
1390{
1391 return wxWindow::GetLabel();
1392}
1393
5f0b2f22
SC
1394bool wxTopLevelWindowMac::Show(bool show)
1395{
facd6764 1396 if ( !wxTopLevelWindowBase::Show(show) )
902725ee 1397 return false;
5f0b2f22 1398
03561a3c 1399 bool plainTransition = true;
21e77aa1 1400
a979e444 1401#if wxUSE_SYSTEM_OPTIONS
21e77aa1
DS
1402 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1403 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1404#endif
52c3df99 1405
21e77aa1
DS
1406 if (show)
1407 {
8c8f6a4b 1408 if ( plainTransition )
1f90939c 1409 ::ShowWindow( (WindowRef)m_macWindow );
1f90939c 1410 else
52c3df99 1411 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
52c3df99 1412
1f90939c 1413 ::SelectWindow( (WindowRef)m_macWindow ) ;
52c3df99
DS
1414
1415 // because apps expect a size event to occur at this moment
a979e444 1416 wxSizeEvent event(GetSize() , m_windowId);
1f90939c
SC
1417 event.SetEventObject(this);
1418 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1419 }
1420 else
1421 {
8c8f6a4b 1422 if ( plainTransition )
21e77aa1 1423 ::HideWindow( (WindowRef)m_macWindow );
1f90939c 1424 else
21e77aa1 1425 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
5f0b2f22
SC
1426 }
1427
facd6764 1428 MacPropagateVisibilityChanged() ;
5f0b2f22 1429
902725ee 1430 return true ;
5f0b2f22
SC
1431}
1432
c5985061 1433bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
902725ee 1434{
c5985061
SC
1435 if ( show )
1436 {
1437 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1438 delete data ;
1439 data = new FullScreenData() ;
902725ee 1440
c5985061
SC
1441 m_macFullScreenData = data ;
1442 data->m_position = GetPosition() ;
1443 data->m_size = GetSize() ;
f3b2e2d7 1444 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
902725ee 1445
c5985061 1446 if ( style & wxFULLSCREEN_NOMENUBAR )
52c3df99
DS
1447 HideMenuBar() ;
1448
c5985061
SC
1449 wxRect client = wxGetClientDisplayRect() ;
1450
52c3df99 1451 int left , top , right , bottom ;
c5985061 1452 int x, y, w, h ;
902725ee 1453
c5985061
SC
1454 x = client.x ;
1455 y = client.y ;
1456 w = client.width ;
1457 h = client.height ;
902725ee 1458
c5985061
SC
1459 MacGetContentAreaInset( left , top , right , bottom ) ;
1460
1461 if ( style & wxFULLSCREEN_NOCAPTION )
1462 {
1463 y -= top ;
1464 h += top ;
1465 }
52c3df99 1466
c5985061
SC
1467 if ( style & wxFULLSCREEN_NOBORDER )
1468 {
1469 x -= left ;
1470 w += left + right ;
1471 h += bottom ;
1472 }
52c3df99 1473
c5985061
SC
1474 if ( style & wxFULLSCREEN_NOTOOLBAR )
1475 {
3c86150d
SC
1476 // TODO
1477 }
52c3df99 1478
3c86150d
SC
1479 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1480 {
1481 // TODO
c5985061 1482 }
52c3df99 1483
c5985061 1484 SetSize( x , y , w, h ) ;
172da31f 1485 if ( data->m_wasResizable )
f3b2e2d7 1486 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
c5985061
SC
1487 }
1488 else
1489 {
1490 ShowMenuBar() ;
1491 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
172da31f 1492 if ( data->m_wasResizable )
f3b2e2d7 1493 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
c5985061
SC
1494 SetPosition( data->m_position ) ;
1495 SetSize( data->m_size ) ;
52c3df99 1496
c5985061
SC
1497 delete data ;
1498 m_macFullScreenData = NULL ;
1499 }
52c3df99 1500
902725ee 1501 return false;
c5985061
SC
1502}
1503
902725ee
WS
1504bool wxTopLevelWindowMac::IsFullScreen() const
1505{
1506 return m_macFullScreenData != NULL ;
c5985061
SC
1507}
1508
50f3c41d 1509
07880314 1510bool wxTopLevelWindowMac::SetTransparent(wxByte alpha)
50f3c41d 1511{
24fcd264 1512 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, float(alpha)/255.0);
50f3c41d
RD
1513 return result == noErr;
1514}
1515
1516
07880314 1517bool wxTopLevelWindowMac::CanSetTransparent()
50f3c41d
RD
1518{
1519 return true;
1520}
1521
1522
30c23e74 1523void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
8ec0a8fa
SC
1524{
1525 if ( GetExtraStyle() == exStyle )
1526 return ;
30c23e74 1527
8ec0a8fa 1528 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
52c3df99 1529
8ec0a8fa 1530#if TARGET_API_MAC_OSX
f21449ae 1531 if ( m_macWindow != NULL )
8ec0a8fa
SC
1532 {
1533 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
893727e5 1534
8ec0a8fa 1535 if ( MacGetMetalAppearance() != metal )
893727e5
KO
1536 {
1537 if ( MacGetUnifiedAppearance() )
1538 MacSetUnifiedAppearance( !metal ) ;
89954433 1539
8ec0a8fa 1540 MacSetMetalAppearance( metal ) ;
893727e5 1541 }
8ec0a8fa
SC
1542 }
1543#endif
1544}
1545
4488a1d3
VZ
1546bool wxTopLevelWindowMac::SetBackgroundStyle(wxBackgroundStyle style)
1547{
1548 if ( !wxTopLevelWindowBase::SetBackgroundStyle(style) )
1549 return false ;
1550
1551 WindowRef windowRef = HIViewGetWindow( (HIViewRef)GetHandle() );
1552
1553 if ( GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
1554 {
1555 OSStatus err = HIWindowChangeFeatures( windowRef, 0, kWindowIsOpaque );
1556 verify_noerr( err );
1557 err = ReshapeCustomWindow( windowRef );
1558 verify_noerr( err );
1559 }
1560
1561 return true ;
1562}
1563
30c23e74 1564// TODO: switch to structure bounds -
21e77aa1 1565// we are still using coordinates of the content view
21e77aa1 1566//
29281095
SC
1567void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1568{
21e77aa1
DS
1569 Rect content, structure ;
1570
29281095
SC
1571 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1572 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1573
1574 left = content.left - structure.left ;
1575 top = content.top - structure.top ;
1576 right = structure.right - content.right ;
1577 bottom = structure.bottom - content.bottom ;
1578}
1579
5f0b2f22
SC
1580void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1581{
c9698388 1582 m_cachedClippedRectValid = false ;
facd6764
SC
1583 Rect bounds = { y , x , y + height , x + width } ;
1584 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
4817190d 1585 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
5f0b2f22
SC
1586}
1587
facd6764 1588void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1589{
facd6764 1590 Rect bounds ;
52c3df99 1591
facd6764 1592 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1593
1594 if (x)
1595 *x = bounds.left ;
1596 if (y)
1597 *y = bounds.top ;
facd6764 1598}
52c3df99 1599
facd6764
SC
1600void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1601{
1602 Rect bounds ;
52c3df99 1603
facd6764 1604 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1605
1606 if (width)
1607 *width = bounds.right - bounds.left ;
1608 if (height)
1609 *height = bounds.bottom - bounds.top ;
facd6764 1610}
1542ea39 1611
facd6764
SC
1612void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1613{
1614 Rect bounds ;
52c3df99 1615
facd6764 1616 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
52c3df99
DS
1617
1618 if (width)
1619 *width = bounds.right - bounds.left ;
1620 if (height)
1621 *height = bounds.bottom - bounds.top ;
facd6764 1622}
1542ea39 1623
6239ee05
SC
1624void wxTopLevelWindowMac::DoCentre(int dir)
1625{
1626 if ( m_macWindow != 0 )
1627 wxTopLevelWindowBase::DoCentre(dir);
1628}
1629
902725ee 1630void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
facd6764
SC
1631{
1632#if TARGET_API_MAC_OSX
893727e5
KO
1633 if ( MacGetUnifiedAppearance() )
1634 MacSetUnifiedAppearance( false ) ;
89954433 1635
902725ee 1636 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
facd6764
SC
1637 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1638#endif
1639}
1542ea39 1640
902725ee 1641bool wxTopLevelWindowMac::MacGetMetalAppearance() const
6aab345b
SC
1642{
1643#if TARGET_API_MAC_OSX
1644 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1645#else
893727e5 1646 return false;
6aab345b
SC
1647#endif
1648}
1649
893727e5
KO
1650void wxTopLevelWindowMac::MacSetUnifiedAppearance( bool set )
1651{
1652#if TARGET_API_MAC_OSX
1653 if ( UMAGetSystemVersion() >= 0x1040 )
1654 {
1655 if ( MacGetMetalAppearance() )
1656 MacSetMetalAppearance( false ) ;
89954433 1657
893727e5
KO
1658 MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
1659 set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
89954433 1660
893727e5
KO
1661 // For some reason, Tiger uses white as the background color for this appearance,
1662 // while most apps using it use the typical striped background. Restore that behavior
89954433 1663 // for wx.
893727e5
KO
1664 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
1665 // though)
1666 SetBackgroundColour( wxSYS_COLOUR_WINDOW ) ;
1667 }
1668#endif
1669}
1670
1671bool wxTopLevelWindowMac::MacGetUnifiedAppearance() const
1672{
1673#if TARGET_API_MAC_OSX
1674 if ( UMAGetSystemVersion() >= 0x1040 )
1675 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
1676 else
1677#endif
1678 return false;
1679}
1680
902725ee 1681void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
facd6764 1682{
52c3df99 1683 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
facd6764 1684}
1542ea39 1685
902725ee 1686wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
facd6764
SC
1687{
1688 UInt32 attr = 0 ;
52c3df99
DS
1689 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1690
facd6764 1691 return attr ;
5f0b2f22 1692}
a07c1212 1693
902725ee 1694void wxTopLevelWindowMac::MacPerformUpdates()
92346151 1695{
f21449ae
SC
1696 // for composited windows this also triggers a redraw of all
1697 // invalid views in the window
80f3f3be 1698 HIWindowFlush((WindowRef) m_macWindow) ;
92346151
SC
1699}
1700
3feeb1e1
SC
1701// Attracts the users attention to this window if the application is
1702// inactive (should be called when a background event occurs)
1703
1704static pascal void wxMacNMResponse( NMRecPtr ptr )
1705{
1706 NMRemove( ptr ) ;
a979e444 1707 DisposePtr( (Ptr)ptr ) ;
3feeb1e1
SC
1708}
1709
89954433 1710void wxTopLevelWindowMac::RequestUserAttention(int WXUNUSED(flags))
3feeb1e1
SC
1711{
1712 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
52c3df99
DS
1713 static wxMacNMUPP nmupp( wxMacNMResponse );
1714
3feeb1e1
SC
1715 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1716 notificationRequest->qType = nmType ;
1717 notificationRequest->nmMark = 1 ;
1718 notificationRequest->nmIcon = 0 ;
1719 notificationRequest->nmSound = 0 ;
1720 notificationRequest->nmStr = NULL ;
1721 notificationRequest->nmResp = nmupp ;
52c3df99 1722
3feeb1e1
SC
1723 verify_noerr( NMInstall( notificationRequest ) ) ;
1724}
1725
facd6764
SC
1726// ---------------------------------------------------------------------------
1727// Shape implementation
1728// ---------------------------------------------------------------------------
1729
6a7e6411 1730
1542ea39
RD
1731bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1732{
902725ee 1733 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
6a7e6411
RD
1734 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1735
a979e444
DS
1736 // The empty region signifies that the shape
1737 // should be removed from the window.
6a7e6411
RD
1738 if ( region.IsEmpty() )
1739 {
1740 wxSize sz = GetClientSize();
1741 wxRegion rgn(0, 0, sz.x, sz.y);
3c393c0a
SC
1742 if ( rgn.IsEmpty() )
1743 return false ;
1744 else
1745 return SetShape(rgn);
6a7e6411
RD
1746 }
1747
1748 // Make a copy of the region
1749 RgnHandle shapeRegion = NewRgn();
1750 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1751
1752 // Dispose of any shape region we may already have
1753 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1754 if ( oldRgn )
1755 DisposeRgn(oldRgn);
1756
1757 // Save the region so we can use it later
7cd7bc23 1758 SetWRefCon((WindowRef)MacGetWindowRef(), (URefCon)shapeRegion);
6a7e6411 1759
52c3df99 1760 // inform the window manager that the window has changed shape
6a7e6411 1761 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
52c3df99 1762
902725ee 1763 return true;
6a7e6411
RD
1764}
1765
6a7e6411
RD
1766// ---------------------------------------------------------------------------
1767// Support functions for shaped windows, based on Apple's CustomWindow sample at
1768// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1769// ---------------------------------------------------------------------------
1770
6a7e6411
RD
1771static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1772{
1773 GetWindowPortBounds(window, inRect);
7cd7bc23
SC
1774 Point pt = { inRect->top ,inRect->left };
1775 wxMacLocalToGlobal( window, &pt ) ;
1776 inRect->bottom += pt.v - inRect->top;
1777 inRect->right += pt.h - inRect->left;
6a7e6411
RD
1778 inRect->top = pt.v;
1779 inRect->left = pt.h;
6a7e6411
RD
1780}
1781
89954433 1782static SInt32 wxShapedMacWindowGetFeatures(WindowRef WXUNUSED(window), SInt32 param)
6a7e6411
RD
1783{
1784 /*------------------------------------------------------
1785 Define which options your custom window supports.
1786 --------------------------------------------------------*/
1787 //just enable everything for our demo
52c3df99
DS
1788 *(OptionBits*)param =
1789 //kWindowCanGrow |
1790 //kWindowCanZoom |
1791 //kWindowCanCollapse |
1792 //kWindowCanGetWindowRegion |
1793 //kWindowHasTitleBar |
1794 //kWindowSupportsDragHilite |
1795 kWindowCanDrawInCurrentPort |
1796 //kWindowCanMeasureTitle |
1797 kWindowWantsDisposeAtProcessDeath |
1798 kWindowSupportsGetGrowImageRegion |
1799 kWindowDefSupportsColorGrafPort;
1800
6a7e6411 1801 return 1;
1542ea39 1802}
6a7e6411
RD
1803
1804// The content region is left as a rectangle matching the window size, this is
1805// so the origin in the paint event, and etc. still matches what the
1806// programmer expects.
1807static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1808{
1809 SetEmptyRgn(rgn);
1810 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1811 if (win)
1812 {
b19bf058 1813 Rect r ;
52c3df99 1814 wxShapedMacWindowGetPos( window, &r ) ;
b19bf058 1815 RectRgn( rgn , &r ) ;
6a7e6411
RD
1816 }
1817}
1818
1819// The structure region is set to the shape given to the SetShape method.
1820static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1821{
1822 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1823
1824 SetEmptyRgn(rgn);
1825 if (cachedRegion)
1826 {
1827 Rect windowRect;
902725ee
WS
1828 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1829 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
6a7e6411 1830 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
902725ee 1831 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
6a7e6411
RD
1832 }
1833}
1834
6a7e6411
RD
1835static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1836{
52c3df99
DS
1837 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1838
1839 if (rgnRec == NULL)
1840 return paramErr;
6a7e6411 1841
52c3df99 1842 switch (rgnRec->regionCode)
6a7e6411
RD
1843 {
1844 case kWindowStructureRgn:
1845 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1846 break;
52c3df99 1847
6a7e6411
RD
1848 case kWindowContentRgn:
1849 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1850 break;
52c3df99 1851
6a7e6411
RD
1852 default:
1853 SetEmptyRgn(rgnRec->winRgn);
52c3df99
DS
1854 break;
1855 }
6a7e6411
RD
1856
1857 return noErr;
1858}
1859
52c3df99
DS
1860// Determine the region of the window which was hit
1861//
1862static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
6a7e6411 1863{
6a7e6411 1864 Point hitPoint;
52c3df99 1865 static RgnHandle tempRgn = NULL;
6a7e6411 1866
52c3df99
DS
1867 if (tempRgn == NULL)
1868 tempRgn = NewRgn();
6a7e6411 1869
52c3df99
DS
1870 // get the point clicked
1871 SetPt( &hitPoint, LoWord(param), HiWord(param) );
6a7e6411 1872
52c3df99 1873 // Mac OS 8.5 or later
6a7e6411 1874 wxShapedMacWindowStructureRegion(window, tempRgn);
52c3df99 1875 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
6a7e6411
RD
1876 return wInContent;
1877
52c3df99
DS
1878 // no significant area was hit
1879 return wNoHit;
6a7e6411
RD
1880}
1881
89954433 1882static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode), WindowRef window, SInt16 message, SInt32 param)
6a7e6411 1883{
52c3df99 1884 switch (message)
6a7e6411
RD
1885 {
1886 case kWindowMsgHitTest:
52c3df99 1887 return wxShapedMacWindowHitTest(window, param);
6a7e6411
RD
1888
1889 case kWindowMsgGetFeatures:
52c3df99 1890 return wxShapedMacWindowGetFeatures(window, param);
6a7e6411
RD
1891
1892 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1893 case kWindowMsgGetRegion:
52c3df99
DS
1894 return wxShapedMacWindowGetRegion(window, param);
1895
1896 default:
1897 break;
6a7e6411
RD
1898 }
1899
1900 return 0;
1901}