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