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