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