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