]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
cleanup magic number usage and comments
[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 }
f0756835
VZ
500
501 // disabled windows must not get any input messages
502 if ( currentMouseWindow && !currentMouseWindow->MacIsReallyEnabled() )
503 currentMouseWindow = NULL;
902725ee 504 }
facd6764 505 }
902725ee 506
facd6764
SC
507 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
508 SetupMouseEvent( wxevent , cEvent ) ;
509
510 // handle all enter / leave events
902725ee 511
facd6764
SC
512 if ( currentMouseWindow != g_MacLastWindow )
513 {
514 if ( g_MacLastWindow )
515 {
516 wxMouseEvent eventleave(wxevent);
517 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
518 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
519 eventleave.SetEventObject( g_MacLastWindow ) ;
d4bd6c97 520 wxevent.SetId( g_MacLastWindow->GetId() ) ;
a979e444 521
facd6764
SC
522#if wxUSE_TOOLTIPS
523 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
a979e444
DS
524#endif
525
facd6764
SC
526 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
527 }
52c3df99 528
facd6764
SC
529 if ( currentMouseWindow )
530 {
531 wxMouseEvent evententer(wxevent);
532 evententer.SetEventType( wxEVT_ENTER_WINDOW );
533 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
534 evententer.SetEventObject( currentMouseWindow ) ;
d4bd6c97 535 wxevent.SetId( currentMouseWindow->GetId() ) ;
a979e444 536
facd6764 537#if wxUSE_TOOLTIPS
a979e444
DS
538 wxToolTip::RelayEvent( currentMouseWindow , evententer );
539#endif
540
facd6764
SC
541 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
542 }
52c3df99 543
facd6764
SC
544 g_MacLastWindow = currentMouseWindow ;
545 }
902725ee 546
facd6764
SC
547 if ( windowPart == inMenuBar )
548 {
549 // special case menu bar, as we are having a low-level runloop we must do it ourselves
550 if ( cEvent.GetKind() == kEventMouseDown )
551 {
552 ::MenuSelect( screenMouseLocation ) ;
553 result = noErr ;
554 }
52c3df99 555 }
facd6764
SC
556 else if ( currentMouseWindow )
557 {
e10e9469 558 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
62ade180
RR
559
560 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
902725ee 561
facd6764 562 wxevent.SetEventObject( currentMouseWindow ) ;
d4bd6c97 563 wxevent.SetId( currentMouseWindow->GetId() ) ;
facd6764 564
facd6764 565 // make tooltips current
902725ee 566
a979e444 567#if wxUSE_TOOLTIPS
f124c908 568 if ( wxevent.GetEventType() == wxEVT_MOTION )
a979e444
DS
569 wxToolTip::RelayEvent( currentMouseWindow , wxevent );
570#endif
571
facd6764 572 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
62ade180 573 {
902725ee 574 if ((currentMouseWindowParent != NULL) &&
62ade180
RR
575 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
576 currentMouseWindow = NULL;
902725ee 577
facd6764 578 result = noErr;
62ade180 579 }
30e0b1c9
SC
580 else
581 {
f90387b4
VZ
582 // if the user code did _not_ handle the event, then perform the
583 // default processing
584 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
585 {
586 // ... that is set focus to this window
587 if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
588 currentMouseWindow->SetFocus();
589 }
590
30e0b1c9
SC
591 ControlPartCode dummyPart ;
592 // if built-in find control is finding the wrong control (ie static box instead of overlaid
593 // button, we cannot let the standard handler do its job, but must handle manually
594
902725ee 595 if ( ( cEvent.GetKind() == kEventMouseDown )
23ad132c 596#ifdef __WXMAC_OSX__
902725ee
WS
597 &&
598 (FindControlUnderMouse(windowMouseLocation , window , &dummyPart) !=
599 wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &dummyPart ) )
23ad132c
SC
600#endif
601 )
30e0b1c9 602 {
902725ee 603 if ( currentMouseWindow->MacIsReallyEnabled() )
7ce1a4e1
SC
604 {
605 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
606 Point clickLocation = windowMouseLocation ;
789ae0cf
SC
607
608 if ( toplevelWindow->MacUsesCompositing() )
609 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
610
7ce1a4e1
SC
611 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
612 modifiers , (ControlActionUPP ) -1 ) ;
902725ee
WS
613
614 if ((currentMouseWindowParent != NULL) &&
62ade180 615 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
a979e444 616 {
e10e9469 617 currentMouseWindow = NULL;
a979e444 618 }
7ce1a4e1 619 }
a979e444 620
30e0b1c9
SC
621 result = noErr ;
622 }
623 }
52c3df99 624
2a1f999f 625 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
facd6764 626 {
2a1f999f 627 wxApp::s_captureWindow = NULL ;
facd6764
SC
628 // update cursor ?
629 }
6d63e2fc
SC
630
631 // update cursor
902725ee 632
6d63e2fc
SC
633 wxWindow* cursorTarget = currentMouseWindow ;
634 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
635
52c3df99 636 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
6d63e2fc
SC
637 {
638 cursorTarget = cursorTarget->GetParent() ;
639 if ( cursorTarget )
e10e9469 640 cursorPoint += cursorTarget->GetPosition();
6d63e2fc
SC
641 }
642
f124c908
VZ
643 }
644 else // currentMouseWindow == NULL
d88ffaaa
SC
645 {
646 // don't mess with controls we don't know about
647 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
648 // so we try sending them the correct control directly
e4b7e0b4 649 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
d88ffaaa
SC
650 {
651 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
652 Point clickLocation = windowMouseLocation ;
f124c908 653#if TARGET_API_MAC_OSX
789ae0cf
SC
654 if ( toplevelWindow->MacUsesCompositing() )
655 {
789ae0cf
SC
656 HIPoint hiPoint ;
657 hiPoint.x = clickLocation.h ;
658 hiPoint.y = clickLocation.v ;
659 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
660 clickLocation.h = (int)hiPoint.x ;
661 clickLocation.v = (int)hiPoint.y ;
789ae0cf 662 }
f124c908 663#endif // TARGET_API_MAC_OSX
52c3df99
DS
664
665 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
d88ffaaa
SC
666 result = noErr ;
667 }
668 }
a979e444 669
facd6764 670 return result ;
851b3a88 671}
facd6764
SC
672
673static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
674{
675 OSStatus result = eventNotHandledErr ;
851b3a88 676
facd6764 677 wxMacCarbonEvent cEvent( event ) ;
902725ee 678
facd6764 679 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
e40298d5 680 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 681
52c3df99 682 switch ( GetEventKind( event ) )
e40298d5 683 {
e40298d5 684 case kEventWindowActivated :
facd6764
SC
685 {
686 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
facd6764
SC
687 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
688 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
689 wxevent.SetEventObject(toplevelWindow);
690 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 691 // we still sending an eventNotHandledErr in order to allow for default processing
facd6764 692 }
52c3df99
DS
693 break ;
694
e40298d5 695 case kEventWindowDeactivated :
facd6764
SC
696 {
697 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
698 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
699 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
700 wxevent.SetEventObject(toplevelWindow);
701 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 702 // we still sending an eventNotHandledErr in order to allow for default processing
facd6764 703 }
52c3df99
DS
704 break ;
705
902725ee 706 case kEventWindowShown :
902725ee
WS
707 toplevelWindow->Refresh() ;
708 result = noErr ;
709 break ;
52c3df99 710
e40298d5 711 case kEventWindowClose :
52c3df99 712 toplevelWindow->Close() ;
e40298d5
JS
713 result = noErr ;
714 break ;
52c3df99 715
e40298d5 716 case kEventWindowBoundsChanged :
facd6764 717 {
52c3df99 718 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
facd6764
SC
719 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
720 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
721 if ( attributes & kWindowBoundsChangeSizeChanged )
e40298d5 722 {
facd6764
SC
723 // according to the other ports we handle this within the OS level
724 // resize event, not within a wxSizeEvent
725 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
726 if ( frame )
727 {
6f02a879 728 frame->PositionBars();
facd6764 729 }
1542ea39 730
facd6764
SC
731 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
732 event.SetEventObject( toplevelWindow ) ;
851b3a88 733
facd6764 734 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
5e533062 735 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
e40298d5 736 }
52c3df99 737
facd6764
SC
738 if ( attributes & kWindowBoundsChangeOriginChanged )
739 {
740 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
741 event.SetEventObject( toplevelWindow ) ;
742 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
743 }
52c3df99 744
facd6764 745 result = noErr ;
facd6764 746 }
52c3df99
DS
747 break ;
748
f81bfef9 749 case kEventWindowBoundsChanging :
facd6764
SC
750 {
751 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
752 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
83901ec2 753
facd6764
SC
754 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
755 {
29281095
SC
756 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
757 int left , top , right , bottom ;
758 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
52c3df99
DS
759
760 wxRect r(
761 newRect.left - left,
762 newRect.top - top,
763 newRect.right - newRect.left + left + right,
764 newRect.bottom - newRect.top + top + bottom ) ;
765
facd6764
SC
766 // this is a EVT_SIZING not a EVT_SIZE type !
767 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
768 wxevent.SetEventObject( toplevelWindow ) ;
769 wxRect adjustR = r ;
770 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
facd6764 771 adjustR = wxevent.GetRect() ;
29281095 772
facd6764
SC
773 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
774 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
2c25bd55 775 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
facd6764
SC
776 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
777 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
778 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
2c25bd55 779 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
facd6764 780 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
4a5f2351 781 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
facd6764 782 if ( !EqualRect( &newRect , &adjustedRect ) )
1f919f38 783 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
4817190d 784 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
f81bfef9 785 }
facd6764 786
902725ee 787 result = noErr ;
facd6764 788 }
52c3df99
DS
789 break ;
790
e40298d5
JS
791 default :
792 break ;
793 }
52c3df99 794
e40298d5 795 return result ;
851b3a88
SC
796}
797
facd6764 798pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
799{
800 OSStatus result = eventNotHandledErr ;
801
802 switch ( GetEventClass( event ) )
803 {
c5c9378c 804 case kEventClassKeyboard :
e40298d5 805 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 806 break ;
52c3df99 807
851b3a88 808 case kEventClassWindow :
facd6764 809 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
e40298d5 810 break ;
52c3df99 811
851b3a88 812 case kEventClassMouse :
facd6764 813 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
e40298d5 814 break ;
52c3df99 815
851b3a88
SC
816 default :
817 break ;
818 }
52c3df99 819
851b3a88
SC
820 return result ;
821}
822
facd6764 823DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
851b3a88 824
5f0b2f22
SC
825// ---------------------------------------------------------------------------
826// wxWindowMac utility functions
827// ---------------------------------------------------------------------------
828
829// Find an item given the Macintosh Window Reference
830
71f2fb52
RN
831WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
832
833static MacWindowMap wxWinMacWindowList;
834
835wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
836{
837 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
838
839 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
840}
841
842void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
843void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
844{
845 // adding NULL WindowRef is (first) surely a result of an error and
846 // nothing else :-)
847 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
848
849 wxWinMacWindowList[inWindowRef] = win;
850}
851
852void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
853void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
854{
855 MacWindowMap::iterator it;
856 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
857 {
858 if ( it->second == win )
859 {
860 wxWinMacWindowList.erase(it);
861 break;
862 }
863 }
864}
5f0b2f22 865
a15eb0a5
SC
866// ----------------------------------------------------------------------------
867// wxTopLevelWindowMac creation
868// ----------------------------------------------------------------------------
869
245f3581 870wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 871
902725ee 872typedef struct
c5985061
SC
873{
874 wxPoint m_position ;
902725ee 875 wxSize m_size ;
f3b2e2d7 876 bool m_wasResizable ;
52c3df99
DS
877}
878FullScreenData ;
c5985061 879
a15eb0a5
SC
880void wxTopLevelWindowMac::Init()
881{
882 m_iconized =
902725ee 883 m_maximizeOnShow = false;
6a17ca35 884 m_macWindow = NULL ;
52c3df99 885
902725ee 886#if TARGET_API_MAC_OSX
52c3df99
DS
887 m_macUsesCompositing = ( UMAGetSystemVersion() >= 0x1030 );
888#else
889 m_macUsesCompositing = false;
facd6764 890#endif
52c3df99 891
7c091673 892 m_macEventHandler = NULL ;
c5985061 893 m_macFullScreenData = NULL ;
a15eb0a5
SC
894}
895
118f012e
SC
896class wxMacDeferredWindowDeleter : public wxObject
897{
898public :
1542ea39
RD
899 wxMacDeferredWindowDeleter( WindowRef windowRef )
900 {
901 m_macWindow = windowRef ;
118f012e 902 }
52c3df99 903
1542ea39
RD
904 virtual ~wxMacDeferredWindowDeleter()
905 {
906 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e 907 }
52c3df99
DS
908
909protected :
118f012e
SC
910 WindowRef m_macWindow ;
911} ;
912
a15eb0a5
SC
913bool wxTopLevelWindowMac::Create(wxWindow *parent,
914 wxWindowID id,
915 const wxString& title,
916 const wxPoint& pos,
917 const wxSize& size,
918 long style,
919 const wxString& name)
920{
921 // init our fields
922 Init();
923
924 m_windowStyle = style;
925
52c3df99 926 SetName( name );
a15eb0a5
SC
927
928 m_windowId = id == -1 ? NewControlId() : id;
fb5246be 929 wxWindow::SetLabel( title ) ;
a15eb0a5 930
facd6764
SC
931 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
932
94d1d0f4 933 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
902725ee 934
5bd78bbc
JS
935 if (GetExtraStyle() & wxFRAME_EX_METAL)
936 MacSetMetalAppearance(true);
94d1d0f4 937
a15eb0a5
SC
938 wxTopLevelWindows.Append(this);
939
940 if ( parent )
941 parent->AddChild(this);
942
902725ee 943 return true;
a15eb0a5
SC
944}
945
946wxTopLevelWindowMac::~wxTopLevelWindowMac()
947{
6a17ca35
SC
948 if ( m_macWindow )
949 {
c81c6d54 950#if wxUSE_TOOLTIPS
6a17ca35 951 wxToolTip::NotifyWindowDelete(m_macWindow) ;
c81c6d54 952#endif
118f012e 953 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 954 }
1542ea39 955
7c091673
SC
956 if ( m_macEventHandler )
957 {
958 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
959 m_macEventHandler = NULL ;
960 }
851b3a88 961
5f0b2f22
SC
962 wxRemoveMacWindowAssociation( this ) ;
963
a15eb0a5
SC
964 if ( wxModelessWindows.Find(this) )
965 wxModelessWindows.DeleteObject(this);
902725ee 966
c5985061
SC
967 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
968 delete data ;
969 m_macFullScreenData = NULL ;
a15eb0a5
SC
970}
971
972
973// ----------------------------------------------------------------------------
974// wxTopLevelWindowMac maximize/minimize
975// ----------------------------------------------------------------------------
976
977void wxTopLevelWindowMac::Maximize(bool maximize)
978{
a979e444 979 // TODO: check if this is still necessary
699b6af4 980#if 0
a979e444
DS
981 wxMacPortStateHelper help( (GrafPtr)GetWindowPort( (WindowRef)m_macWindow) ) ;
982 wxMacWindowClipper clip( this );
699b6af4 983#endif
52c3df99
DS
984
985 if ( !IsWindowInStandardState( (WindowRef)m_macWindow, NULL, NULL ) )
699b6af4
SC
986 {
987 Rect rect;
a979e444 988
699b6af4
SC
989 GetWindowBounds((WindowRef)m_macWindow, kWindowGlobalPortRgn, &rect);
990 SetWindowIdealUserState((WindowRef)m_macWindow, &rect);
991 SetWindowUserState((WindowRef)m_macWindow, &rect);
992 }
52c3df99 993
b7aec135 994 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
a15eb0a5
SC
995}
996
997bool wxTopLevelWindowMac::IsMaximized() const
998{
a979e444 999 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
1000}
1001
1002void wxTopLevelWindowMac::Iconize(bool iconize)
1003{
52c3df99
DS
1004 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1005 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
1006}
1007
1008bool wxTopLevelWindowMac::IsIconized() const
1009{
b7aec135 1010 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
1011}
1012
1013void wxTopLevelWindowMac::Restore()
1014{
902725ee 1015 if ( IsMaximized() )
699b6af4 1016 Maximize(false);
902725ee 1017 else if ( IsIconized() )
699b6af4 1018 Iconize(false);
a15eb0a5
SC
1019}
1020
1021// ----------------------------------------------------------------------------
1022// wxTopLevelWindowMac misc
1023// ----------------------------------------------------------------------------
1024
facd6764
SC
1025wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1026{
52c3df99 1027 return wxPoint(0, 0) ;
facd6764
SC
1028}
1029
a15eb0a5
SC
1030void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1031{
1032 // this sets m_icon
1033 wxTopLevelWindowBase::SetIcon(icon);
1034}
5f0b2f22 1035
facd6764 1036void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
902725ee 1037{
facd6764
SC
1038 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1039
1040 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1041 {
1042 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1043 }
1044}
1045
902725ee 1046void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
949cb163
SC
1047{
1048 if ( m_macEventHandler != NULL )
1049 {
1050 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1051 }
52c3df99
DS
1052
1053 InstallWindowEventHandler(
1054 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1055 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
949cb163
SC
1056}
1057
a979e444
DS
1058void wxTopLevelWindowMac::MacCreateRealWindow(
1059 const wxString& title,
1060 const wxPoint& pos,
1061 const wxSize& size,
1062 long style,
1063 const wxString& name )
5f0b2f22 1064{
47ac4f9b 1065 OSStatus err = noErr ;
e40298d5
JS
1066 SetName(name);
1067 m_windowStyle = style;
902725ee 1068 m_isShown = false;
1542ea39 1069
e40298d5 1070 // create frame.
facd6764
SC
1071 int x = (int)pos.x;
1072 int y = (int)pos.y;
902725ee 1073
52c3df99 1074 Rect theBoundsRect;
5a486641
SC
1075 wxRect display = wxGetClientDisplayRect() ;
1076
1077 if ( x == wxDefaultPosition.x )
1078 x = display.x ;
902725ee 1079
5a486641
SC
1080 if ( y == wxDefaultPosition.y )
1081 y = display.y ;
1542ea39 1082
facd6764
SC
1083 int w = WidthDefault(size.x);
1084 int h = HeightDefault(size.y);
1542ea39 1085
facd6764 1086 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 1087
5f0b2f22 1088 // translate the window attributes in the appropriate window class and attributes
5f0b2f22
SC
1089 WindowClass wclass = 0;
1090 WindowAttributes attr = kWindowNoAttributes ;
e353795e 1091 WindowGroupRef group = NULL ;
1542ea39 1092
f5744893 1093 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 1094 {
1542ea39 1095 if (
f5744893
SC
1096 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1097 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1098 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 1099 )
5f0b2f22 1100 {
f5744893 1101 wclass = kFloatingWindowClass ;
52c3df99 1102
f5744893 1103 if ( HasFlag(wxTINY_CAPTION_VERT) )
f5744893 1104 attr |= kWindowSideTitlebarAttribute ;
f5744893
SC
1105 }
1106 else
1107 {
1108 wclass = kPlainWindowClass ;
5f0b2f22
SC
1109 }
1110 }
1111 else if ( HasFlag( wxCAPTION ) )
1112 {
1542ea39 1113 wclass = kDocumentWindowClass ;
22e3c5bd 1114 attr |= kWindowInWindowMenuAttribute ;
5f0b2f22 1115 }
eab19a7c 1116#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
3e17bc3f
RN
1117 else if ( HasFlag( wxFRAME_DRAWER ) )
1118 {
1119 wclass = kDrawerWindowClass;
789ae0cf 1120 // we must force compositing on a drawer
902725ee 1121 m_macUsesCompositing = true ;
3e17bc3f
RN
1122 }
1123#endif //10.2 and up
5f0b2f22
SC
1124 else
1125 {
f5744893 1126 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 1127 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
1128 {
1129 wclass = kDocumentWindowClass ;
1130 }
1131 else
1132 {
1133 wclass = kPlainWindowClass ;
1134 }
5f0b2f22 1135 }
1542ea39 1136
52c3df99 1137 if ( wclass != kPlainWindowClass )
5f0b2f22 1138 {
52c3df99
DS
1139 if ( HasFlag( wxMINIMIZE_BOX ) )
1140 attr |= kWindowCollapseBoxAttribute ;
1141
1142 if ( HasFlag( wxMAXIMIZE_BOX ) )
1143 attr |= kWindowFullZoomAttribute ;
1144
1145 if ( HasFlag( wxRESIZE_BORDER ) )
1146 attr |= kWindowResizableAttribute ;
1147
1148 if ( HasFlag( wxCLOSE_BOX) )
1149 attr |= kWindowCloseBoxAttribute ;
5f0b2f22 1150 }
1542ea39 1151
52c3df99 1152 // turn on live resizing (OS X only)
eb630bf1 1153 if (UMAGetSystemVersion() >= 0x1000)
eb630bf1 1154 attr |= kWindowLiveResizeAttribute;
eb630bf1 1155
e353795e 1156 if ( HasFlag(wxSTAY_ON_TOP) )
e353795e 1157 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
6a7e6411 1158
902725ee 1159#if TARGET_API_MAC_OSX
789ae0cf
SC
1160 if ( m_macUsesCompositing )
1161 attr |= kWindowCompositingAttribute;
d66c3960 1162#endif
902725ee 1163
6a7e6411
RD
1164 if ( HasFlag(wxFRAME_SHAPED) )
1165 {
1166 WindowDefSpec customWindowDefSpec;
1167 customWindowDefSpec.defType = kWindowDefProcPtr;
1168 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1169
47ac4f9b 1170 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1171 attr, &theBoundsRect,
1172 (WindowRef*) &m_macWindow);
1173 }
1174 else
1175 {
47ac4f9b 1176 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1177 }
1178
e353795e
SC
1179 if ( err == noErr && m_macWindow != NULL && group != NULL )
1180 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1181
47ac4f9b 1182 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761 1183
a979e444
DS
1184 // the create commands are only for content rect,
1185 // so we have to set the size again as structure bounds
f83b6761
SC
1186 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1187
facd6764
SC
1188 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1189 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1f1c8bd4 1190 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
902725ee 1191
a979e444 1192#if TARGET_API_MAC_OSX
789ae0cf 1193 if ( m_macUsesCompositing )
f57d9215 1194 {
902725ee 1195 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
3103e8a9 1196 // the content view, so we have to retrieve it explicitly
902725ee 1197 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
789ae0cf
SC
1198 m_peer->GetControlRefAddr() ) ;
1199 if ( !m_peer->Ok() )
1200 {
1201 // compatibility mode fallback
1202 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1203 }
f57d9215 1204 }
3a9dc061 1205#endif
789ae0cf
SC
1206 {
1207 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1208 }
52c3df99
DS
1209
1210 // the root control level handler
5ca0d812 1211 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1212
8ec0a8fa
SC
1213#if TARGET_API_MAC_OSX
1214 if ( m_macUsesCompositing && m_macWindow != NULL )
1215 {
1216 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1217 MacSetMetalAppearance( true ) ;
1218 }
1219#endif
52c3df99 1220
73fe67bd 1221 // the frame window event handler
e40298d5 1222 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1223 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1224
14c96cf2
SC
1225 DoSetWindowVariant( m_windowVariant ) ;
1226
facd6764 1227 m_macFocus = NULL ;
6a7e6411
RD
1228
1229 if ( HasFlag(wxFRAME_SHAPED) )
1230 {
1231 // default shape matches the window size
52c3df99
DS
1232 wxRegion rgn( 0, 0, w, h );
1233 SetShape( rgn );
6a7e6411 1234 }
3dfafdb9
RD
1235
1236 wxWindowCreateEvent event(this);
1237 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1238}
1239
5d2e69e8 1240void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1241{
5d2e69e8 1242 wxWindow::ClearBackground() ;
5f0b2f22
SC
1243}
1244
5f0b2f22
SC
1245// Raise the window to the top of the Z order
1246void wxTopLevelWindowMac::Raise()
1247{
7f3c339c 1248 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1249}
1250
1251// Lower the window to the bottom of the Z order
1252void wxTopLevelWindowMac::Lower()
1253{
76a5e5d2 1254 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1255}
1256
245f3581
DE
1257void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1258{
52c3df99 1259 if (s_macDeactivateWindow)
245f3581 1260 {
f02a4ffa
VZ
1261 wxLogTrace(TRACE_ACTIVATE,
1262 wxT("Doing delayed deactivation of %p"),
1263 s_macDeactivateWindow);
52c3df99 1264
245f3581
DE
1265 s_macDeactivateWindow->MacActivate(timestamp, false);
1266 }
1267}
1268
851b3a88 1269void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 1270{
f02a4ffa 1271 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
17f8d2a7 1272
52c3df99
DS
1273 if (s_macDeactivateWindow == this)
1274 s_macDeactivateWindow = NULL;
1275
245f3581 1276 MacDelayedDeactivation(timestamp);
8ab50549 1277 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1278}
1279
1280void wxTopLevelWindowMac::SetTitle(const wxString& title)
1281{
fb5246be 1282 wxWindow::SetLabel( title ) ;
16a76184 1283 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1284}
1285
d4bd6c97 1286wxString wxTopLevelWindowMac::GetTitle() const
fb5246be
WS
1287{
1288 return wxWindow::GetLabel();
1289}
1290
5f0b2f22
SC
1291bool wxTopLevelWindowMac::Show(bool show)
1292{
facd6764 1293 if ( !wxTopLevelWindowBase::Show(show) )
902725ee 1294 return false;
5f0b2f22 1295
21e77aa1
DS
1296 bool plainTransition = false;
1297
a979e444 1298#if wxUSE_SYSTEM_OPTIONS
21e77aa1
DS
1299 // code contributed by Ryan Wilcox December 18, 2003
1300 plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1301 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1302 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1303#endif
52c3df99 1304
21e77aa1
DS
1305 if (show)
1306 {
8c8f6a4b 1307 if ( plainTransition )
1f90939c 1308 ::ShowWindow( (WindowRef)m_macWindow );
1f90939c 1309 else
52c3df99 1310 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
52c3df99 1311
1f90939c 1312 ::SelectWindow( (WindowRef)m_macWindow ) ;
52c3df99
DS
1313
1314 // because apps expect a size event to occur at this moment
a979e444 1315 wxSizeEvent event(GetSize() , m_windowId);
1f90939c
SC
1316 event.SetEventObject(this);
1317 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1318 }
1319 else
1320 {
8c8f6a4b 1321 if ( plainTransition )
21e77aa1 1322 ::HideWindow( (WindowRef)m_macWindow );
1f90939c 1323 else
21e77aa1 1324 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
5f0b2f22
SC
1325 }
1326
facd6764 1327 MacPropagateVisibilityChanged() ;
5f0b2f22 1328
902725ee 1329 return true ;
5f0b2f22
SC
1330}
1331
c5985061 1332bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
902725ee 1333{
c5985061
SC
1334 if ( show )
1335 {
1336 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1337 delete data ;
1338 data = new FullScreenData() ;
902725ee 1339
c5985061
SC
1340 m_macFullScreenData = data ;
1341 data->m_position = GetPosition() ;
1342 data->m_size = GetSize() ;
f3b2e2d7 1343 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
902725ee 1344
c5985061 1345 if ( style & wxFULLSCREEN_NOMENUBAR )
52c3df99
DS
1346 HideMenuBar() ;
1347
c5985061
SC
1348 wxRect client = wxGetClientDisplayRect() ;
1349
52c3df99 1350 int left , top , right , bottom ;
c5985061 1351 int x, y, w, h ;
902725ee 1352
c5985061
SC
1353 x = client.x ;
1354 y = client.y ;
1355 w = client.width ;
1356 h = client.height ;
902725ee 1357
c5985061
SC
1358 MacGetContentAreaInset( left , top , right , bottom ) ;
1359
1360 if ( style & wxFULLSCREEN_NOCAPTION )
1361 {
1362 y -= top ;
1363 h += top ;
1364 }
52c3df99 1365
c5985061
SC
1366 if ( style & wxFULLSCREEN_NOBORDER )
1367 {
1368 x -= left ;
1369 w += left + right ;
1370 h += bottom ;
1371 }
52c3df99 1372
c5985061
SC
1373 if ( style & wxFULLSCREEN_NOTOOLBAR )
1374 {
3c86150d
SC
1375 // TODO
1376 }
52c3df99 1377
3c86150d
SC
1378 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1379 {
1380 // TODO
c5985061 1381 }
52c3df99 1382
c5985061 1383 SetSize( x , y , w, h ) ;
172da31f 1384 if ( data->m_wasResizable )
f3b2e2d7 1385 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
c5985061
SC
1386 }
1387 else
1388 {
1389 ShowMenuBar() ;
1390 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
172da31f 1391 if ( data->m_wasResizable )
f3b2e2d7 1392 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
c5985061
SC
1393 SetPosition( data->m_position ) ;
1394 SetSize( data->m_size ) ;
52c3df99 1395
c5985061
SC
1396 delete data ;
1397 m_macFullScreenData = NULL ;
1398 }
52c3df99 1399
902725ee 1400 return false;
c5985061
SC
1401}
1402
902725ee
WS
1403bool wxTopLevelWindowMac::IsFullScreen() const
1404{
1405 return m_macFullScreenData != NULL ;
c5985061
SC
1406}
1407
8ec0a8fa
SC
1408void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1409{
1410 if ( GetExtraStyle() == exStyle )
1411 return ;
1412
1413 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
52c3df99 1414
8ec0a8fa
SC
1415#if TARGET_API_MAC_OSX
1416 if ( m_macUsesCompositing && m_macWindow != NULL )
1417 {
1418 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1419 if ( MacGetMetalAppearance() != metal )
1420 MacSetMetalAppearance( metal ) ;
1421 }
1422#endif
1423}
1424
21e77aa1
DS
1425// we are still using coordinates of the content view
1426// TODO: switch to structure bounds
1427//
29281095
SC
1428void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1429{
21e77aa1
DS
1430 Rect content, structure ;
1431
29281095
SC
1432 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1433 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1434
1435 left = content.left - structure.left ;
1436 top = content.top - structure.top ;
1437 right = structure.right - content.right ;
1438 bottom = structure.bottom - content.bottom ;
1439}
1440
5f0b2f22
SC
1441void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1442{
c9698388 1443 m_cachedClippedRectValid = false ;
facd6764
SC
1444 Rect bounds = { y , x , y + height , x + width } ;
1445 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
4817190d 1446 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
5f0b2f22
SC
1447}
1448
facd6764 1449void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1450{
facd6764 1451 Rect bounds ;
52c3df99 1452
facd6764 1453 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1454
1455 if (x)
1456 *x = bounds.left ;
1457 if (y)
1458 *y = bounds.top ;
facd6764 1459}
52c3df99 1460
facd6764
SC
1461void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1462{
1463 Rect bounds ;
52c3df99 1464
facd6764 1465 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1466
1467 if (width)
1468 *width = bounds.right - bounds.left ;
1469 if (height)
1470 *height = bounds.bottom - bounds.top ;
facd6764 1471}
1542ea39 1472
facd6764
SC
1473void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1474{
1475 Rect bounds ;
52c3df99 1476
facd6764 1477 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
52c3df99
DS
1478
1479 if (width)
1480 *width = bounds.right - bounds.left ;
1481 if (height)
1482 *height = bounds.bottom - bounds.top ;
facd6764 1483}
1542ea39 1484
902725ee 1485void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
facd6764
SC
1486{
1487#if TARGET_API_MAC_OSX
789ae0cf 1488 wxASSERT_MSG( m_macUsesCompositing ,
facd6764 1489 wxT("Cannot set metal appearance on a non-compositing window") ) ;
902725ee
WS
1490
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
SC
1519{
1520#if TARGET_API_MAC_OSX
902725ee
WS
1521 if ( m_macUsesCompositing )
1522 {
92346151 1523#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
902725ee
WS
1524 // for composited windows this also triggers a redraw of all
1525 // invalid views in the window
52c3df99 1526 if ( UMAGetSystemVersion() >= 0x1030 )
902725ee
WS
1527 HIWindowFlush((WindowRef) m_macWindow) ;
1528 else
92346151 1529#endif
902725ee
WS
1530 {
1531 // the only way to trigger the redrawing on earlier systems is to call
1532 // ReceiveNextEvent
1533
1534 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1535 UInt32 currentEventClass = 0 ;
1536 UInt32 currentEventKind = 0 ;
1537 if ( currentEvent != NULL )
1538 {
1539 currentEventClass = ::GetEventClass( currentEvent ) ;
1540 currentEventKind = ::GetEventKind( currentEvent ) ;
1541 }
52c3df99 1542
902725ee
WS
1543 if ( currentEventClass != kEventClassMenu )
1544 {
1545 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1546 EventRef theEvent;
1547 OSStatus status = noErr ;
1548 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1549 }
1550 }
92346151
SC
1551 }
1552 else
1553#endif
1554 {
902725ee
WS
1555 BeginUpdate( (WindowRef) m_macWindow ) ;
1556
1557 RgnHandle updateRgn = NewRgn();
1558 if ( updateRgn )
1559 {
1560 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
1561 UpdateControls( (WindowRef)m_macWindow , updateRgn ) ;
52c3df99 1562
902725ee
WS
1563 // if ( !EmptyRgn( updateRgn ) )
1564 // MacDoRedraw( updateRgn , 0 , true) ;
52c3df99 1565
902725ee
WS
1566 DisposeRgn( updateRgn );
1567 }
52c3df99 1568
902725ee
WS
1569 EndUpdate( (WindowRef)m_macWindow ) ;
1570 QDFlushPortBuffer( GetWindowPort( (WindowRef)m_macWindow ) , NULL ) ;
92346151
SC
1571 }
1572}
1573
3feeb1e1
SC
1574// Attracts the users attention to this window if the application is
1575// inactive (should be called when a background event occurs)
1576
1577static pascal void wxMacNMResponse( NMRecPtr ptr )
1578{
1579 NMRemove( ptr ) ;
a979e444 1580 DisposePtr( (Ptr)ptr ) ;
3feeb1e1
SC
1581}
1582
3feeb1e1
SC
1583void wxTopLevelWindowMac::RequestUserAttention(int flags )
1584{
1585 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
52c3df99
DS
1586 static wxMacNMUPP nmupp( wxMacNMResponse );
1587
3feeb1e1
SC
1588 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1589 notificationRequest->qType = nmType ;
1590 notificationRequest->nmMark = 1 ;
1591 notificationRequest->nmIcon = 0 ;
1592 notificationRequest->nmSound = 0 ;
1593 notificationRequest->nmStr = NULL ;
1594 notificationRequest->nmResp = nmupp ;
52c3df99 1595
3feeb1e1
SC
1596 verify_noerr( NMInstall( notificationRequest ) ) ;
1597}
1598
facd6764
SC
1599// ---------------------------------------------------------------------------
1600// Shape implementation
1601// ---------------------------------------------------------------------------
1602
6a7e6411 1603
1542ea39
RD
1604bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1605{
902725ee 1606 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
6a7e6411
RD
1607 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1608
a979e444
DS
1609 // The empty region signifies that the shape
1610 // should be removed from the window.
6a7e6411
RD
1611 if ( region.IsEmpty() )
1612 {
1613 wxSize sz = GetClientSize();
1614 wxRegion rgn(0, 0, sz.x, sz.y);
3c393c0a
SC
1615 if ( rgn.IsEmpty() )
1616 return false ;
1617 else
1618 return SetShape(rgn);
6a7e6411
RD
1619 }
1620
1621 // Make a copy of the region
1622 RgnHandle shapeRegion = NewRgn();
1623 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1624
1625 // Dispose of any shape region we may already have
1626 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1627 if ( oldRgn )
1628 DisposeRgn(oldRgn);
1629
1630 // Save the region so we can use it later
1631 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1632
52c3df99 1633 // inform the window manager that the window has changed shape
6a7e6411 1634 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
52c3df99 1635
902725ee 1636 return true;
6a7e6411
RD
1637}
1638
6a7e6411
RD
1639// ---------------------------------------------------------------------------
1640// Support functions for shaped windows, based on Apple's CustomWindow sample at
1641// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1642// ---------------------------------------------------------------------------
1643
6a7e6411
RD
1644static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1645{
1646 GetWindowPortBounds(window, inRect);
a979e444
DS
1647 Point pt = { inRect->left, inRect->top };
1648
1649 QDLocalToGlobalPoint( GetWindowPort(window), &pt ) ;
6a7e6411
RD
1650 inRect->top = pt.v;
1651 inRect->left = pt.h;
1652 inRect->bottom += pt.v;
1653 inRect->right += pt.h;
1654}
1655
6a7e6411
RD
1656static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1657{
1658 /*------------------------------------------------------
1659 Define which options your custom window supports.
1660 --------------------------------------------------------*/
1661 //just enable everything for our demo
52c3df99
DS
1662 *(OptionBits*)param =
1663 //kWindowCanGrow |
1664 //kWindowCanZoom |
1665 //kWindowCanCollapse |
1666 //kWindowCanGetWindowRegion |
1667 //kWindowHasTitleBar |
1668 //kWindowSupportsDragHilite |
1669 kWindowCanDrawInCurrentPort |
1670 //kWindowCanMeasureTitle |
1671 kWindowWantsDisposeAtProcessDeath |
1672 kWindowSupportsGetGrowImageRegion |
1673 kWindowDefSupportsColorGrafPort;
1674
6a7e6411 1675 return 1;
1542ea39 1676}
6a7e6411
RD
1677
1678// The content region is left as a rectangle matching the window size, this is
1679// so the origin in the paint event, and etc. still matches what the
1680// programmer expects.
1681static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1682{
1683 SetEmptyRgn(rgn);
1684 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1685 if (win)
1686 {
b19bf058 1687 Rect r ;
52c3df99 1688 wxShapedMacWindowGetPos( window, &r ) ;
b19bf058 1689 RectRgn( rgn , &r ) ;
6a7e6411
RD
1690 }
1691}
1692
1693// The structure region is set to the shape given to the SetShape method.
1694static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1695{
1696 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1697
1698 SetEmptyRgn(rgn);
1699 if (cachedRegion)
1700 {
1701 Rect windowRect;
902725ee
WS
1702 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1703 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
6a7e6411 1704 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
902725ee 1705 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
6a7e6411
RD
1706 }
1707}
1708
6a7e6411
RD
1709static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1710{
52c3df99
DS
1711 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1712
1713 if (rgnRec == NULL)
1714 return paramErr;
6a7e6411 1715
52c3df99 1716 switch (rgnRec->regionCode)
6a7e6411
RD
1717 {
1718 case kWindowStructureRgn:
1719 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1720 break;
52c3df99 1721
6a7e6411
RD
1722 case kWindowContentRgn:
1723 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1724 break;
52c3df99 1725
6a7e6411
RD
1726 default:
1727 SetEmptyRgn(rgnRec->winRgn);
52c3df99
DS
1728 break;
1729 }
6a7e6411
RD
1730
1731 return noErr;
1732}
1733
52c3df99
DS
1734// Determine the region of the window which was hit
1735//
1736static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
6a7e6411 1737{
6a7e6411 1738 Point hitPoint;
52c3df99 1739 static RgnHandle tempRgn = NULL;
6a7e6411 1740
52c3df99
DS
1741 if (tempRgn == NULL)
1742 tempRgn = NewRgn();
6a7e6411 1743
52c3df99
DS
1744 // get the point clicked
1745 SetPt( &hitPoint, LoWord(param), HiWord(param) );
6a7e6411 1746
52c3df99 1747 // Mac OS 8.5 or later
6a7e6411 1748 wxShapedMacWindowStructureRegion(window, tempRgn);
52c3df99 1749 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
6a7e6411
RD
1750 return wInContent;
1751
52c3df99
DS
1752 // no significant area was hit
1753 return wNoHit;
6a7e6411
RD
1754}
1755
6a7e6411
RD
1756static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1757{
52c3df99 1758 switch (message)
6a7e6411
RD
1759 {
1760 case kWindowMsgHitTest:
52c3df99 1761 return wxShapedMacWindowHitTest(window, param);
6a7e6411
RD
1762
1763 case kWindowMsgGetFeatures:
52c3df99 1764 return wxShapedMacWindowGetFeatures(window, param);
6a7e6411
RD
1765
1766 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1767 case kWindowMsgGetRegion:
52c3df99
DS
1768 return wxShapedMacWindowGetRegion(window, param);
1769
1770 default:
1771 break;
6a7e6411
RD
1772 }
1773
1774 return 0;
1775}
1925be65 1776