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