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