]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
further encapsulation of graphics context, wxdc becoming 'generic'
[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
851b3a88
SC
84extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
85
1542ea39 86static const EventTypeSpec eventList[] =
851b3a88 87{
a979e444 88 // TODO: remove control related event like key and mouse (except for WindowLeave events)
ffc75ee4 89
e40298d5 90 { kEventClassKeyboard, kEventRawKeyDown } ,
c5c9378c
SC
91 { kEventClassKeyboard, kEventRawKeyRepeat } ,
92 { kEventClassKeyboard, kEventRawKeyUp } ,
93 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
94
ffc75ee4
SC
95 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
96 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
30c23e74 97
16a76184 98 { kEventClassWindow , kEventWindowShown } ,
851b3a88
SC
99 { kEventClassWindow , kEventWindowActivated } ,
100 { kEventClassWindow , kEventWindowDeactivated } ,
101 { kEventClassWindow , kEventWindowBoundsChanging } ,
102 { kEventClassWindow , kEventWindowBoundsChanged } ,
103 { kEventClassWindow , kEventWindowClose } ,
104
a979e444
DS
105 // we have to catch these events on the toplevel window level,
106 // as controls don't get the raw mouse events anymore
902725ee 107
851b3a88
SC
108 { kEventClassMouse , kEventMouseDown } ,
109 { kEventClassMouse , kEventMouseUp } ,
de127c69 110 { kEventClassMouse , kEventMouseWheelMoved } ,
851b3a88
SC
111 { kEventClassMouse , kEventMouseMoved } ,
112 { kEventClassMouse , kEventMouseDragged } ,
851b3a88
SC
113} ;
114
c5c9378c
SC
115static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
116{
117 OSStatus result = eventNotHandledErr ;
b3bd613a 118 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
a979e444 119 // FindFocus does not return the actual focus window, but the enclosing window
b3bd613a 120 wxWindow* focus = wxWindow::DoFindFocus();
92a7272f 121 if ( focus == NULL )
ccf1ef0f 122 focus = (wxTopLevelWindowMac*) data ;
52c3df99 123
19fc48c6 124 unsigned char charCode ;
f3097709 125 wxChar uniChar[2] ;
670f9935
WS
126 uniChar[0] = 0;
127 uniChar[1] = 0;
128
1542ea39 129 UInt32 keyCode ;
c5c9378c 130 UInt32 modifiers ;
e40298d5
JS
131 Point point ;
132 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
133
2d17efa9
SC
134#if wxUSE_UNICODE
135 UInt32 dataSize = 0 ;
a979e444 136 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
2d17efa9
SC
137 {
138 UniChar buf[2] ;
f3097709 139 int numChars = dataSize / sizeof( UniChar) + 1;
902725ee 140
2d17efa9 141 UniChar* charBuf = buf ;
902725ee 142
f3097709
SC
143 if ( numChars * 2 > 4 )
144 charBuf = new UniChar[ numChars ] ;
2d17efa9 145 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
670f9935 146 charBuf[ numChars - 1 ] = 0;
a979e444 147
2d17efa9
SC
148#if SIZEOF_WCHAR_T == 2
149 uniChar = charBuf[0] ;
150#else
d9d488cf 151 wxMBConvUTF16 converter ;
0cc00f4f 152 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
902725ee 153#endif
a979e444 154
f3097709 155 if ( numChars * 2 > 4 )
2d17efa9
SC
156 delete[] charBuf ;
157 }
158#endif
159
a979e444 160 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
52c3df99
DS
161 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
162 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
163 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
902725ee 164
e40298d5 165 UInt32 message = (keyCode << 8) + charCode;
a979e444 166 switch ( GetEventKind( event ) )
e40298d5
JS
167 {
168 case kEventRawKeyRepeat :
169 case kEventRawKeyDown :
e40298d5 170 {
404f1d80
SC
171 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
172 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
173 wxTheApp->MacSetCurrentEvent( event , handler ) ;
902725ee 174 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
f3097709 175 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
404f1d80
SC
176 {
177 result = noErr ;
178 }
179 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
e40298d5
JS
180 }
181 break ;
52c3df99 182
e40298d5 183 case kEventRawKeyUp :
902725ee 184 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
f3097709 185 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
e40298d5
JS
186 {
187 result = noErr ;
188 }
189 break ;
52c3df99 190
e40298d5
JS
191 case kEventRawKeyModifiersChanged :
192 {
193 wxKeyEvent event(wxEVT_KEY_DOWN);
194
195 event.m_shiftDown = modifiers & shiftKey;
196 event.m_controlDown = modifiers & controlKey;
197 event.m_altDown = modifiers & optionKey;
198 event.m_metaDown = modifiers & cmdKey;
a979e444
DS
199 event.m_x = point.h;
200 event.m_y = point.v;
201
2d17efa9 202#if wxUSE_UNICODE
0cc00f4f 203 event.m_uniChar = uniChar[0] ;
2d17efa9 204#endif
a979e444 205
687706f5 206 event.SetTimestamp(when);
e40298d5
JS
207 event.SetEventObject(focus);
208
902725ee 209 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
e40298d5
JS
210 {
211 event.m_keyCode = WXK_CONTROL ;
212 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
213 focus->GetEventHandler()->ProcessEvent( event ) ;
214 }
902725ee 215 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
e40298d5
JS
216 {
217 event.m_keyCode = WXK_SHIFT ;
218 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
219 focus->GetEventHandler()->ProcessEvent( event ) ;
220 }
902725ee 221 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
e40298d5
JS
222 {
223 event.m_keyCode = WXK_ALT ;
224 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
225 focus->GetEventHandler()->ProcessEvent( event ) ;
226 }
902725ee 227 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
b7aec135
SC
228 {
229 event.m_keyCode = WXK_COMMAND ;
230 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
231 focus->GetEventHandler()->ProcessEvent( event ) ;
232 }
a979e444 233
2a1f999f 234 wxApp::s_lastModifiers = modifiers ;
e40298d5 235 }
902725ee 236 break ;
52c3df99
DS
237
238 default:
239 break;
e40298d5 240 }
851b3a88
SC
241
242 return result ;
243}
244
facd6764 245// we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
52c3df99 246// for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
facd6764 247// mouse down at all
52c3df99 248//
facd6764 249// This handler can also be called from app level where data (ie target window) may be null or a non wx window
1542ea39 250
facd6764 251wxWindow* g_MacLastWindow = NULL ;
a3195b73 252
86a9144f
SC
253static EventMouseButton lastButton = 0 ;
254
facd6764
SC
255static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
256{
257 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
258 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
67245665 259
902725ee 260 // this parameter are not given for all events
facd6764 261 EventMouseButton button = 0 ;
902725ee 262 UInt32 clickCount = 0 ;
a979e444
DS
263 cEvent.GetParameter<EventMouseButton>( kEventParamMouseButton, typeMouseButton , &button ) ;
264 cEvent.GetParameter<UInt32>( kEventParamClickCount, typeUInt32 , &clickCount ) ;
facd6764
SC
265
266 wxevent.m_x = screenMouseLocation.h;
267 wxevent.m_y = screenMouseLocation.v;
268 wxevent.m_shiftDown = modifiers & shiftKey;
269 wxevent.m_controlDown = modifiers & controlKey;
270 wxevent.m_altDown = modifiers & optionKey;
271 wxevent.m_metaDown = modifiers & cmdKey;
272 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
52c3df99 273
902725ee 274 // a control click is interpreted as a right click
facd6764 275 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
facd6764 276 button = kEventMouseButtonSecondary ;
902725ee 277
b343fb9f
SC
278 // otherwise we report double clicks by connecting a left click with a ctrl-left click
279 if ( clickCount > 1 && button != lastButton )
280 clickCount = 1 ;
902725ee 281
86a9144f
SC
282 // we must make sure that our synthetic 'right' button corresponds in
283 // mouse down, moved and mouse up, and does not deliver a right down and left up
902725ee 284
86a9144f
SC
285 if ( cEvent.GetKind() == kEventMouseDown )
286 lastButton = button ;
902725ee 287
4e4e6dce 288 if ( button == 0 )
902725ee 289 lastButton = 0 ;
86a9144f
SC
290 else if ( lastButton )
291 button = lastButton ;
facd6764 292
a979e444
DS
293 // determine the correct down state, wx does not want a 'down' for a mouseUp event,
294 // while mac delivers this button
facd6764
SC
295 if ( button != 0 && cEvent.GetKind() != kEventMouseUp )
296 {
a979e444 297 switch ( button )
e40298d5 298 {
facd6764
SC
299 case kEventMouseButtonPrimary :
300 wxevent.m_leftDown = true ;
e40298d5 301 break ;
52c3df99 302
facd6764
SC
303 case kEventMouseButtonSecondary :
304 wxevent.m_rightDown = true ;
e40298d5 305 break ;
52c3df99 306
facd6764
SC
307 case kEventMouseButtonTertiary :
308 wxevent.m_middleDown = true ;
e40298d5 309 break ;
52c3df99
DS
310
311 default:
312 break ;
facd6764
SC
313 }
314 }
52c3df99 315
90ff87d7
SC
316 // translate into wx types
317 switch ( cEvent.GetKind() )
facd6764 318 {
90ff87d7 319 case kEventMouseDown :
52c3df99 320 switch ( button )
90ff87d7
SC
321 {
322 case kEventMouseButtonPrimary :
a979e444 323 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
90ff87d7 324 break ;
52c3df99 325
90ff87d7
SC
326 case kEventMouseButtonSecondary :
327 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
328 break ;
52c3df99 329
90ff87d7 330 case kEventMouseButtonTertiary :
a979e444 331 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
90ff87d7 332 break ;
52c3df99
DS
333
334 default:
335 break ;
90ff87d7
SC
336 }
337 break ;
52c3df99 338
90ff87d7 339 case kEventMouseUp :
52c3df99 340 switch ( button )
90ff87d7
SC
341 {
342 case kEventMouseButtonPrimary :
343 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
344 break ;
52c3df99 345
90ff87d7
SC
346 case kEventMouseButtonSecondary :
347 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
348 break ;
52c3df99 349
90ff87d7
SC
350 case kEventMouseButtonTertiary :
351 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
352 break ;
52c3df99
DS
353
354 default:
355 break ;
90ff87d7
SC
356 }
357 break ;
52c3df99 358
2cce9b11 359 case kEventMouseWheelMoved :
52c3df99 360 {
a979e444 361 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
facd6764
SC
362
363 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
364 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeLongInteger) ;
365
366 wxevent.m_wheelRotation = delta;
367 wxevent.m_wheelDelta = 1;
368 wxevent.m_linesPerAction = 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 {
d6fd667b 472 QDGlobalToLocalPoint( UMAGetWindowPort(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 ;
52c3df99
DS
887}
888FullScreenData ;
c5985061 889
a15eb0a5
SC
890void wxTopLevelWindowMac::Init()
891{
892 m_iconized =
902725ee 893 m_maximizeOnShow = false;
6a17ca35 894 m_macWindow = NULL ;
52c3df99 895
7c091673 896 m_macEventHandler = NULL ;
c5985061 897 m_macFullScreenData = NULL ;
a15eb0a5
SC
898}
899
118f012e
SC
900class wxMacDeferredWindowDeleter : public wxObject
901{
902public :
1542ea39
RD
903 wxMacDeferredWindowDeleter( WindowRef windowRef )
904 {
905 m_macWindow = windowRef ;
118f012e 906 }
52c3df99 907
1542ea39
RD
908 virtual ~wxMacDeferredWindowDeleter()
909 {
910 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e 911 }
52c3df99
DS
912
913protected :
118f012e
SC
914 WindowRef m_macWindow ;
915} ;
916
a15eb0a5
SC
917bool wxTopLevelWindowMac::Create(wxWindow *parent,
918 wxWindowID id,
919 const wxString& title,
920 const wxPoint& pos,
921 const wxSize& size,
922 long style,
923 const wxString& name)
924{
925 // init our fields
926 Init();
927
928 m_windowStyle = style;
929
52c3df99 930 SetName( name );
a15eb0a5
SC
931
932 m_windowId = id == -1 ? NewControlId() : id;
fb5246be 933 wxWindow::SetLabel( title ) ;
a15eb0a5 934
facd6764
SC
935 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
936
94d1d0f4 937 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
902725ee 938
5bd78bbc
JS
939 if (GetExtraStyle() & wxFRAME_EX_METAL)
940 MacSetMetalAppearance(true);
94d1d0f4 941
a15eb0a5
SC
942 wxTopLevelWindows.Append(this);
943
944 if ( parent )
945 parent->AddChild(this);
946
902725ee 947 return true;
a15eb0a5
SC
948}
949
950wxTopLevelWindowMac::~wxTopLevelWindowMac()
951{
6a17ca35
SC
952 if ( m_macWindow )
953 {
c81c6d54 954#if wxUSE_TOOLTIPS
6a17ca35 955 wxToolTip::NotifyWindowDelete(m_macWindow) ;
c81c6d54 956#endif
118f012e 957 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 958 }
1542ea39 959
7c091673
SC
960 if ( m_macEventHandler )
961 {
962 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
963 m_macEventHandler = NULL ;
964 }
851b3a88 965
5f0b2f22
SC
966 wxRemoveMacWindowAssociation( this ) ;
967
a15eb0a5
SC
968 if ( wxModelessWindows.Find(this) )
969 wxModelessWindows.DeleteObject(this);
902725ee 970
c5985061
SC
971 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
972 delete data ;
973 m_macFullScreenData = NULL ;
a15eb0a5
SC
974}
975
976
977// ----------------------------------------------------------------------------
978// wxTopLevelWindowMac maximize/minimize
979// ----------------------------------------------------------------------------
980
981void wxTopLevelWindowMac::Maximize(bool maximize)
982{
6643c354
SC
983 Point idealSize = { 0 , 0 } ;
984 if ( maximize )
985 {
986 Rect rect ;
987 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
988 idealSize.h = rect.right - rect.left ;
989 idealSize.v = rect.bottom - rect.top ;
990 }
991 ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
a15eb0a5
SC
992}
993
994bool wxTopLevelWindowMac::IsMaximized() const
995{
a979e444 996 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
997}
998
999void wxTopLevelWindowMac::Iconize(bool iconize)
1000{
52c3df99
DS
1001 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1002 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
1003}
1004
1005bool wxTopLevelWindowMac::IsIconized() const
1006{
b7aec135 1007 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
1008}
1009
1010void wxTopLevelWindowMac::Restore()
1011{
902725ee 1012 if ( IsMaximized() )
699b6af4 1013 Maximize(false);
902725ee 1014 else if ( IsIconized() )
699b6af4 1015 Iconize(false);
a15eb0a5
SC
1016}
1017
1018// ----------------------------------------------------------------------------
1019// wxTopLevelWindowMac misc
1020// ----------------------------------------------------------------------------
1021
facd6764
SC
1022wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1023{
52c3df99 1024 return wxPoint(0, 0) ;
facd6764
SC
1025}
1026
a15eb0a5
SC
1027void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1028{
1029 // this sets m_icon
1030 wxTopLevelWindowBase::SetIcon(icon);
1031}
5f0b2f22 1032
facd6764 1033void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
902725ee 1034{
facd6764
SC
1035 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1036
1037 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1038 {
1039 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1040 }
1041}
1042
902725ee 1043void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
949cb163
SC
1044{
1045 if ( m_macEventHandler != NULL )
1046 {
1047 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1048 }
52c3df99
DS
1049
1050 InstallWindowEventHandler(
1051 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1052 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
949cb163
SC
1053}
1054
a979e444
DS
1055void wxTopLevelWindowMac::MacCreateRealWindow(
1056 const wxString& title,
1057 const wxPoint& pos,
1058 const wxSize& size,
1059 long style,
1060 const wxString& name )
5f0b2f22 1061{
47ac4f9b 1062 OSStatus err = noErr ;
e40298d5
JS
1063 SetName(name);
1064 m_windowStyle = style;
902725ee 1065 m_isShown = false;
1542ea39 1066
e40298d5 1067 // create frame.
facd6764
SC
1068 int x = (int)pos.x;
1069 int y = (int)pos.y;
902725ee 1070
52c3df99 1071 Rect theBoundsRect;
5a486641
SC
1072 wxRect display = wxGetClientDisplayRect() ;
1073
1074 if ( x == wxDefaultPosition.x )
1075 x = display.x ;
902725ee 1076
5a486641
SC
1077 if ( y == wxDefaultPosition.y )
1078 y = display.y ;
1542ea39 1079
facd6764
SC
1080 int w = WidthDefault(size.x);
1081 int h = HeightDefault(size.y);
1542ea39 1082
facd6764 1083 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 1084
5f0b2f22 1085 // translate the window attributes in the appropriate window class and attributes
5f0b2f22
SC
1086 WindowClass wclass = 0;
1087 WindowAttributes attr = kWindowNoAttributes ;
e353795e 1088 WindowGroupRef group = NULL ;
1542ea39 1089
f5744893 1090 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 1091 {
1542ea39 1092 if (
f5744893
SC
1093 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1094 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1095 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 1096 )
5f0b2f22 1097 {
f5744893 1098 wclass = kFloatingWindowClass ;
52c3df99 1099
f5744893 1100 if ( HasFlag(wxTINY_CAPTION_VERT) )
f5744893 1101 attr |= kWindowSideTitlebarAttribute ;
f5744893
SC
1102 }
1103 else
1104 {
1105 wclass = kPlainWindowClass ;
5f0b2f22
SC
1106 }
1107 }
1108 else if ( HasFlag( wxCAPTION ) )
1109 {
1542ea39 1110 wclass = kDocumentWindowClass ;
22e3c5bd 1111 attr |= kWindowInWindowMenuAttribute ;
5f0b2f22 1112 }
eab19a7c 1113#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
3e17bc3f
RN
1114 else if ( HasFlag( wxFRAME_DRAWER ) )
1115 {
1116 wclass = kDrawerWindowClass;
3e17bc3f
RN
1117 }
1118#endif //10.2 and up
5f0b2f22
SC
1119 else
1120 {
f5744893 1121 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 1122 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
1123 {
1124 wclass = kDocumentWindowClass ;
1125 }
1126 else
1127 {
1128 wclass = kPlainWindowClass ;
1129 }
5f0b2f22 1130 }
1542ea39 1131
52c3df99 1132 if ( wclass != kPlainWindowClass )
5f0b2f22 1133 {
52c3df99
DS
1134 if ( HasFlag( wxMINIMIZE_BOX ) )
1135 attr |= kWindowCollapseBoxAttribute ;
1136
1137 if ( HasFlag( wxMAXIMIZE_BOX ) )
1138 attr |= kWindowFullZoomAttribute ;
1139
1140 if ( HasFlag( wxRESIZE_BORDER ) )
1141 attr |= kWindowResizableAttribute ;
1142
1143 if ( HasFlag( wxCLOSE_BOX) )
1144 attr |= kWindowCloseBoxAttribute ;
5f0b2f22 1145 }
1542ea39 1146
52c3df99 1147 // turn on live resizing (OS X only)
eb630bf1 1148 if (UMAGetSystemVersion() >= 0x1000)
eb630bf1 1149 attr |= kWindowLiveResizeAttribute;
eb630bf1 1150
e353795e 1151 if ( HasFlag(wxSTAY_ON_TOP) )
e353795e 1152 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
6a7e6411 1153
f21449ae 1154 attr |= kWindowCompositingAttribute;
24fcd264
SC
1155#if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1156 attr |= kWindowFrameworkScaledAttribute;
d66c3960 1157#endif
902725ee 1158
6a7e6411
RD
1159 if ( HasFlag(wxFRAME_SHAPED) )
1160 {
1161 WindowDefSpec customWindowDefSpec;
1162 customWindowDefSpec.defType = kWindowDefProcPtr;
1163 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1164
47ac4f9b 1165 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1166 attr, &theBoundsRect,
1167 (WindowRef*) &m_macWindow);
1168 }
1169 else
1170 {
47ac4f9b 1171 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1172 }
1173
e353795e
SC
1174 if ( err == noErr && m_macWindow != NULL && group != NULL )
1175 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1176
47ac4f9b 1177 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761 1178
a979e444
DS
1179 // the create commands are only for content rect,
1180 // so we have to set the size again as structure bounds
f83b6761
SC
1181 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1182
facd6764
SC
1183 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1184 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1f1c8bd4 1185 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
902725ee 1186
f21449ae
SC
1187 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1188 // the content view, so we have to retrieve it explicitly
1189 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1190 m_peer->GetControlRefAddr() ) ;
1191 if ( !m_peer->Ok() )
789ae0cf 1192 {
f21449ae
SC
1193 // compatibility mode fallback
1194 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
789ae0cf 1195 }
52c3df99
DS
1196
1197 // the root control level handler
5ca0d812 1198 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1199
9dc1d180
JS
1200 // Causes the inner part of the window not to be metal
1201 // if the style is used before window creation.
1202#if 0 // TARGET_API_MAC_OSX
8ec0a8fa
SC
1203 if ( m_macUsesCompositing && m_macWindow != NULL )
1204 {
1205 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1206 MacSetMetalAppearance( true ) ;
1207 }
1208#endif
52c3df99 1209
73fe67bd 1210 // the frame window event handler
e40298d5 1211 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1212 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1213
14c96cf2
SC
1214 DoSetWindowVariant( m_windowVariant ) ;
1215
facd6764 1216 m_macFocus = NULL ;
6a7e6411
RD
1217
1218 if ( HasFlag(wxFRAME_SHAPED) )
1219 {
1220 // default shape matches the window size
52c3df99
DS
1221 wxRegion rgn( 0, 0, w, h );
1222 SetShape( rgn );
6a7e6411 1223 }
3dfafdb9
RD
1224
1225 wxWindowCreateEvent event(this);
1226 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1227}
1228
5d2e69e8 1229void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1230{
5d2e69e8 1231 wxWindow::ClearBackground() ;
5f0b2f22
SC
1232}
1233
5f0b2f22
SC
1234// Raise the window to the top of the Z order
1235void wxTopLevelWindowMac::Raise()
1236{
7f3c339c 1237 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1238}
1239
1240// Lower the window to the bottom of the Z order
1241void wxTopLevelWindowMac::Lower()
1242{
76a5e5d2 1243 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1244}
1245
245f3581
DE
1246void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1247{
52c3df99 1248 if (s_macDeactivateWindow)
245f3581 1249 {
f02a4ffa
VZ
1250 wxLogTrace(TRACE_ACTIVATE,
1251 wxT("Doing delayed deactivation of %p"),
1252 s_macDeactivateWindow);
52c3df99 1253
245f3581
DE
1254 s_macDeactivateWindow->MacActivate(timestamp, false);
1255 }
1256}
1257
851b3a88 1258void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 1259{
f02a4ffa 1260 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
17f8d2a7 1261
52c3df99
DS
1262 if (s_macDeactivateWindow == this)
1263 s_macDeactivateWindow = NULL;
1264
245f3581 1265 MacDelayedDeactivation(timestamp);
8ab50549 1266 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1267}
1268
1269void wxTopLevelWindowMac::SetTitle(const wxString& title)
1270{
fb5246be 1271 wxWindow::SetLabel( title ) ;
16a76184 1272 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1273}
1274
d4bd6c97 1275wxString wxTopLevelWindowMac::GetTitle() const
fb5246be
WS
1276{
1277 return wxWindow::GetLabel();
1278}
1279
5f0b2f22
SC
1280bool wxTopLevelWindowMac::Show(bool show)
1281{
facd6764 1282 if ( !wxTopLevelWindowBase::Show(show) )
902725ee 1283 return false;
5f0b2f22 1284
21e77aa1
DS
1285 bool plainTransition = false;
1286
a979e444 1287#if wxUSE_SYSTEM_OPTIONS
21e77aa1
DS
1288 // code contributed by Ryan Wilcox December 18, 2003
1289 plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1290 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1291 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1292#endif
52c3df99 1293
21e77aa1
DS
1294 if (show)
1295 {
8c8f6a4b 1296 if ( plainTransition )
1f90939c 1297 ::ShowWindow( (WindowRef)m_macWindow );
1f90939c 1298 else
52c3df99 1299 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
52c3df99 1300
1f90939c 1301 ::SelectWindow( (WindowRef)m_macWindow ) ;
52c3df99
DS
1302
1303 // because apps expect a size event to occur at this moment
a979e444 1304 wxSizeEvent event(GetSize() , m_windowId);
1f90939c
SC
1305 event.SetEventObject(this);
1306 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1307 }
1308 else
1309 {
8c8f6a4b 1310 if ( plainTransition )
21e77aa1 1311 ::HideWindow( (WindowRef)m_macWindow );
1f90939c 1312 else
21e77aa1 1313 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
5f0b2f22
SC
1314 }
1315
facd6764 1316 MacPropagateVisibilityChanged() ;
5f0b2f22 1317
902725ee 1318 return true ;
5f0b2f22
SC
1319}
1320
c5985061 1321bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
902725ee 1322{
c5985061
SC
1323 if ( show )
1324 {
1325 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1326 delete data ;
1327 data = new FullScreenData() ;
902725ee 1328
c5985061
SC
1329 m_macFullScreenData = data ;
1330 data->m_position = GetPosition() ;
1331 data->m_size = GetSize() ;
f3b2e2d7 1332 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
902725ee 1333
c5985061 1334 if ( style & wxFULLSCREEN_NOMENUBAR )
52c3df99
DS
1335 HideMenuBar() ;
1336
c5985061
SC
1337 wxRect client = wxGetClientDisplayRect() ;
1338
52c3df99 1339 int left , top , right , bottom ;
c5985061 1340 int x, y, w, h ;
902725ee 1341
c5985061
SC
1342 x = client.x ;
1343 y = client.y ;
1344 w = client.width ;
1345 h = client.height ;
902725ee 1346
c5985061
SC
1347 MacGetContentAreaInset( left , top , right , bottom ) ;
1348
1349 if ( style & wxFULLSCREEN_NOCAPTION )
1350 {
1351 y -= top ;
1352 h += top ;
1353 }
52c3df99 1354
c5985061
SC
1355 if ( style & wxFULLSCREEN_NOBORDER )
1356 {
1357 x -= left ;
1358 w += left + right ;
1359 h += bottom ;
1360 }
52c3df99 1361
c5985061
SC
1362 if ( style & wxFULLSCREEN_NOTOOLBAR )
1363 {
3c86150d
SC
1364 // TODO
1365 }
52c3df99 1366
3c86150d
SC
1367 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1368 {
1369 // TODO
c5985061 1370 }
52c3df99 1371
c5985061 1372 SetSize( x , y , w, h ) ;
172da31f 1373 if ( data->m_wasResizable )
f3b2e2d7 1374 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
c5985061
SC
1375 }
1376 else
1377 {
1378 ShowMenuBar() ;
1379 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
172da31f 1380 if ( data->m_wasResizable )
f3b2e2d7 1381 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
c5985061
SC
1382 SetPosition( data->m_position ) ;
1383 SetSize( data->m_size ) ;
52c3df99 1384
c5985061
SC
1385 delete data ;
1386 m_macFullScreenData = NULL ;
1387 }
52c3df99 1388
902725ee 1389 return false;
c5985061
SC
1390}
1391
902725ee
WS
1392bool wxTopLevelWindowMac::IsFullScreen() const
1393{
1394 return m_macFullScreenData != NULL ;
c5985061
SC
1395}
1396
50f3c41d 1397
07880314 1398bool wxTopLevelWindowMac::SetTransparent(wxByte alpha)
50f3c41d 1399{
24fcd264 1400 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, float(alpha)/255.0);
50f3c41d
RD
1401 return result == noErr;
1402}
1403
1404
07880314 1405bool wxTopLevelWindowMac::CanSetTransparent()
50f3c41d
RD
1406{
1407 return true;
1408}
1409
1410
30c23e74 1411void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
8ec0a8fa
SC
1412{
1413 if ( GetExtraStyle() == exStyle )
1414 return ;
30c23e74 1415
8ec0a8fa 1416 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
52c3df99 1417
8ec0a8fa 1418#if TARGET_API_MAC_OSX
f21449ae 1419 if ( m_macWindow != NULL )
8ec0a8fa
SC
1420 {
1421 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1422 if ( MacGetMetalAppearance() != metal )
1423 MacSetMetalAppearance( metal ) ;
1424 }
1425#endif
1426}
1427
30c23e74 1428// TODO: switch to structure bounds -
21e77aa1 1429// we are still using coordinates of the content view
21e77aa1 1430//
29281095
SC
1431void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1432{
21e77aa1
DS
1433 Rect content, structure ;
1434
29281095
SC
1435 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1436 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1437
1438 left = content.left - structure.left ;
1439 top = content.top - structure.top ;
1440 right = structure.right - content.right ;
1441 bottom = structure.bottom - content.bottom ;
1442}
1443
5f0b2f22
SC
1444void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1445{
c9698388 1446 m_cachedClippedRectValid = false ;
facd6764
SC
1447 Rect bounds = { y , x , y + height , x + width } ;
1448 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
4817190d 1449 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
5f0b2f22
SC
1450}
1451
facd6764 1452void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1453{
facd6764 1454 Rect bounds ;
52c3df99 1455
facd6764 1456 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1457
1458 if (x)
1459 *x = bounds.left ;
1460 if (y)
1461 *y = bounds.top ;
facd6764 1462}
52c3df99 1463
facd6764
SC
1464void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1465{
1466 Rect bounds ;
52c3df99 1467
facd6764 1468 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1469
1470 if (width)
1471 *width = bounds.right - bounds.left ;
1472 if (height)
1473 *height = bounds.bottom - bounds.top ;
facd6764 1474}
1542ea39 1475
facd6764
SC
1476void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1477{
1478 Rect bounds ;
52c3df99 1479
facd6764 1480 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
52c3df99
DS
1481
1482 if (width)
1483 *width = bounds.right - bounds.left ;
1484 if (height)
1485 *height = bounds.bottom - bounds.top ;
facd6764 1486}
1542ea39 1487
902725ee 1488void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
facd6764
SC
1489{
1490#if TARGET_API_MAC_OSX
902725ee 1491 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
facd6764
SC
1492 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1493#endif
1494}
1542ea39 1495
902725ee 1496bool wxTopLevelWindowMac::MacGetMetalAppearance() const
6aab345b
SC
1497{
1498#if TARGET_API_MAC_OSX
1499 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1500#else
1501 return false ;
1502#endif
1503}
1504
902725ee 1505void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
facd6764 1506{
52c3df99 1507 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
facd6764 1508}
1542ea39 1509
902725ee 1510wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
facd6764
SC
1511{
1512 UInt32 attr = 0 ;
52c3df99
DS
1513 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1514
facd6764 1515 return attr ;
5f0b2f22 1516}
a07c1212 1517
902725ee 1518void wxTopLevelWindowMac::MacPerformUpdates()
92346151 1519{
92346151 1520#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
f21449ae
SC
1521 // for composited windows this also triggers a redraw of all
1522 // invalid views in the window
1523 if ( UMAGetSystemVersion() >= 0x1030 )
1524 HIWindowFlush((WindowRef) m_macWindow) ;
92346151
SC
1525 else
1526#endif
1527 {
f21449ae
SC
1528 // the only way to trigger the redrawing on earlier systems is to call
1529 // ReceiveNextEvent
902725ee 1530
f21449ae
SC
1531 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1532 UInt32 currentEventClass = 0 ;
1533 UInt32 currentEventKind = 0 ;
1534 if ( currentEvent != NULL )
902725ee 1535 {
f21449ae
SC
1536 currentEventClass = ::GetEventClass( currentEvent ) ;
1537 currentEventKind = ::GetEventKind( currentEvent ) ;
902725ee 1538 }
52c3df99 1539
f21449ae
SC
1540 if ( currentEventClass != kEventClassMenu )
1541 {
1542 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1543 EventRef theEvent;
1544 OSStatus status = noErr ;
1545 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1546 }
92346151
SC
1547 }
1548}
1549
3feeb1e1
SC
1550// Attracts the users attention to this window if the application is
1551// inactive (should be called when a background event occurs)
1552
1553static pascal void wxMacNMResponse( NMRecPtr ptr )
1554{
1555 NMRemove( ptr ) ;
a979e444 1556 DisposePtr( (Ptr)ptr ) ;
3feeb1e1
SC
1557}
1558
3feeb1e1
SC
1559void wxTopLevelWindowMac::RequestUserAttention(int flags )
1560{
1561 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
52c3df99
DS
1562 static wxMacNMUPP nmupp( wxMacNMResponse );
1563
3feeb1e1
SC
1564 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1565 notificationRequest->qType = nmType ;
1566 notificationRequest->nmMark = 1 ;
1567 notificationRequest->nmIcon = 0 ;
1568 notificationRequest->nmSound = 0 ;
1569 notificationRequest->nmStr = NULL ;
1570 notificationRequest->nmResp = nmupp ;
52c3df99 1571
3feeb1e1
SC
1572 verify_noerr( NMInstall( notificationRequest ) ) ;
1573}
1574
facd6764
SC
1575// ---------------------------------------------------------------------------
1576// Shape implementation
1577// ---------------------------------------------------------------------------
1578
6a7e6411 1579
1542ea39
RD
1580bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1581{
902725ee 1582 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
6a7e6411
RD
1583 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1584
a979e444
DS
1585 // The empty region signifies that the shape
1586 // should be removed from the window.
6a7e6411
RD
1587 if ( region.IsEmpty() )
1588 {
1589 wxSize sz = GetClientSize();
1590 wxRegion rgn(0, 0, sz.x, sz.y);
3c393c0a
SC
1591 if ( rgn.IsEmpty() )
1592 return false ;
1593 else
1594 return SetShape(rgn);
6a7e6411
RD
1595 }
1596
1597 // Make a copy of the region
1598 RgnHandle shapeRegion = NewRgn();
1599 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1600
1601 // Dispose of any shape region we may already have
1602 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1603 if ( oldRgn )
1604 DisposeRgn(oldRgn);
1605
1606 // Save the region so we can use it later
1607 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1608
52c3df99 1609 // inform the window manager that the window has changed shape
6a7e6411 1610 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
52c3df99 1611
902725ee 1612 return true;
6a7e6411
RD
1613}
1614
6a7e6411
RD
1615// ---------------------------------------------------------------------------
1616// Support functions for shaped windows, based on Apple's CustomWindow sample at
1617// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1618// ---------------------------------------------------------------------------
1619
6a7e6411
RD
1620static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1621{
1622 GetWindowPortBounds(window, inRect);
a979e444
DS
1623 Point pt = { inRect->left, inRect->top };
1624
1625 QDLocalToGlobalPoint( GetWindowPort(window), &pt ) ;
6a7e6411
RD
1626 inRect->top = pt.v;
1627 inRect->left = pt.h;
1628 inRect->bottom += pt.v;
1629 inRect->right += pt.h;
1630}
1631
6a7e6411
RD
1632static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1633{
1634 /*------------------------------------------------------
1635 Define which options your custom window supports.
1636 --------------------------------------------------------*/
1637 //just enable everything for our demo
52c3df99
DS
1638 *(OptionBits*)param =
1639 //kWindowCanGrow |
1640 //kWindowCanZoom |
1641 //kWindowCanCollapse |
1642 //kWindowCanGetWindowRegion |
1643 //kWindowHasTitleBar |
1644 //kWindowSupportsDragHilite |
1645 kWindowCanDrawInCurrentPort |
1646 //kWindowCanMeasureTitle |
1647 kWindowWantsDisposeAtProcessDeath |
1648 kWindowSupportsGetGrowImageRegion |
1649 kWindowDefSupportsColorGrafPort;
1650
6a7e6411 1651 return 1;
1542ea39 1652}
6a7e6411
RD
1653
1654// The content region is left as a rectangle matching the window size, this is
1655// so the origin in the paint event, and etc. still matches what the
1656// programmer expects.
1657static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1658{
1659 SetEmptyRgn(rgn);
1660 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1661 if (win)
1662 {
b19bf058 1663 Rect r ;
52c3df99 1664 wxShapedMacWindowGetPos( window, &r ) ;
b19bf058 1665 RectRgn( rgn , &r ) ;
6a7e6411
RD
1666 }
1667}
1668
1669// The structure region is set to the shape given to the SetShape method.
1670static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1671{
1672 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1673
1674 SetEmptyRgn(rgn);
1675 if (cachedRegion)
1676 {
1677 Rect windowRect;
902725ee
WS
1678 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1679 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
6a7e6411 1680 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
902725ee 1681 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
6a7e6411
RD
1682 }
1683}
1684
6a7e6411
RD
1685static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1686{
52c3df99
DS
1687 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1688
1689 if (rgnRec == NULL)
1690 return paramErr;
6a7e6411 1691
52c3df99 1692 switch (rgnRec->regionCode)
6a7e6411
RD
1693 {
1694 case kWindowStructureRgn:
1695 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1696 break;
52c3df99 1697
6a7e6411
RD
1698 case kWindowContentRgn:
1699 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1700 break;
52c3df99 1701
6a7e6411
RD
1702 default:
1703 SetEmptyRgn(rgnRec->winRgn);
52c3df99
DS
1704 break;
1705 }
6a7e6411
RD
1706
1707 return noErr;
1708}
1709
52c3df99
DS
1710// Determine the region of the window which was hit
1711//
1712static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
6a7e6411 1713{
6a7e6411 1714 Point hitPoint;
52c3df99 1715 static RgnHandle tempRgn = NULL;
6a7e6411 1716
52c3df99
DS
1717 if (tempRgn == NULL)
1718 tempRgn = NewRgn();
6a7e6411 1719
52c3df99
DS
1720 // get the point clicked
1721 SetPt( &hitPoint, LoWord(param), HiWord(param) );
6a7e6411 1722
52c3df99 1723 // Mac OS 8.5 or later
6a7e6411 1724 wxShapedMacWindowStructureRegion(window, tempRgn);
52c3df99 1725 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
6a7e6411
RD
1726 return wInContent;
1727
52c3df99
DS
1728 // no significant area was hit
1729 return wNoHit;
6a7e6411
RD
1730}
1731
6a7e6411
RD
1732static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1733{
52c3df99 1734 switch (message)
6a7e6411
RD
1735 {
1736 case kWindowMsgHitTest:
52c3df99 1737 return wxShapedMacWindowHitTest(window, param);
6a7e6411
RD
1738
1739 case kWindowMsgGetFeatures:
52c3df99 1740 return wxShapedMacWindowGetFeatures(window, param);
6a7e6411
RD
1741
1742 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1743 case kWindowMsgGetRegion:
52c3df99
DS
1744 return wxShapedMacWindowGetRegion(window, param);
1745
1746 default:
1747 break;
6a7e6411
RD
1748 }
1749
1750 return 0;
1751}