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