]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
fixes for wxVsnprintf() in Unicode build (2nd part of patch 1462778)
[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
6643c354 997#if 0
52c3df99 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 }
b7aec135 1006 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
6643c354
SC
1007#else
1008 Point idealSize = { 0 , 0 } ;
1009 if ( maximize )
1010 {
1011 Rect rect ;
1012 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
1013 idealSize.h = rect.right - rect.left ;
1014 idealSize.v = rect.bottom - rect.top ;
1015 }
1016 ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
1017#endif
a15eb0a5
SC
1018}
1019
1020bool wxTopLevelWindowMac::IsMaximized() const
1021{
a979e444 1022 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
1023}
1024
1025void wxTopLevelWindowMac::Iconize(bool iconize)
1026{
52c3df99
DS
1027 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
1028 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
1029}
1030
1031bool wxTopLevelWindowMac::IsIconized() const
1032{
b7aec135 1033 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
1034}
1035
1036void wxTopLevelWindowMac::Restore()
1037{
902725ee 1038 if ( IsMaximized() )
699b6af4 1039 Maximize(false);
902725ee 1040 else if ( IsIconized() )
699b6af4 1041 Iconize(false);
a15eb0a5
SC
1042}
1043
1044// ----------------------------------------------------------------------------
1045// wxTopLevelWindowMac misc
1046// ----------------------------------------------------------------------------
1047
facd6764
SC
1048wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1049{
52c3df99 1050 return wxPoint(0, 0) ;
facd6764
SC
1051}
1052
a15eb0a5
SC
1053void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1054{
1055 // this sets m_icon
1056 wxTopLevelWindowBase::SetIcon(icon);
1057}
5f0b2f22 1058
facd6764 1059void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
902725ee 1060{
facd6764
SC
1061 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1062
1063 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1064 {
1065 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1066 }
1067}
1068
902725ee 1069void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
949cb163
SC
1070{
1071 if ( m_macEventHandler != NULL )
1072 {
1073 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1074 }
52c3df99
DS
1075
1076 InstallWindowEventHandler(
1077 MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1078 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler );
949cb163
SC
1079}
1080
a979e444
DS
1081void wxTopLevelWindowMac::MacCreateRealWindow(
1082 const wxString& title,
1083 const wxPoint& pos,
1084 const wxSize& size,
1085 long style,
1086 const wxString& name )
5f0b2f22 1087{
47ac4f9b 1088 OSStatus err = noErr ;
e40298d5
JS
1089 SetName(name);
1090 m_windowStyle = style;
902725ee 1091 m_isShown = false;
1542ea39 1092
e40298d5 1093 // create frame.
facd6764
SC
1094 int x = (int)pos.x;
1095 int y = (int)pos.y;
902725ee 1096
52c3df99 1097 Rect theBoundsRect;
5a486641
SC
1098 wxRect display = wxGetClientDisplayRect() ;
1099
1100 if ( x == wxDefaultPosition.x )
1101 x = display.x ;
902725ee 1102
5a486641
SC
1103 if ( y == wxDefaultPosition.y )
1104 y = display.y ;
1542ea39 1105
facd6764
SC
1106 int w = WidthDefault(size.x);
1107 int h = HeightDefault(size.y);
1542ea39 1108
facd6764 1109 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 1110
5f0b2f22 1111 // translate the window attributes in the appropriate window class and attributes
5f0b2f22
SC
1112 WindowClass wclass = 0;
1113 WindowAttributes attr = kWindowNoAttributes ;
e353795e 1114 WindowGroupRef group = NULL ;
1542ea39 1115
f5744893 1116 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 1117 {
1542ea39 1118 if (
f5744893
SC
1119 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1120 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1121 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 1122 )
5f0b2f22 1123 {
f5744893 1124 wclass = kFloatingWindowClass ;
52c3df99 1125
f5744893 1126 if ( HasFlag(wxTINY_CAPTION_VERT) )
f5744893 1127 attr |= kWindowSideTitlebarAttribute ;
f5744893
SC
1128 }
1129 else
1130 {
1131 wclass = kPlainWindowClass ;
5f0b2f22
SC
1132 }
1133 }
1134 else if ( HasFlag( wxCAPTION ) )
1135 {
1542ea39 1136 wclass = kDocumentWindowClass ;
22e3c5bd 1137 attr |= kWindowInWindowMenuAttribute ;
5f0b2f22 1138 }
eab19a7c 1139#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
3e17bc3f
RN
1140 else if ( HasFlag( wxFRAME_DRAWER ) )
1141 {
1142 wclass = kDrawerWindowClass;
789ae0cf 1143 // we must force compositing on a drawer
902725ee 1144 m_macUsesCompositing = true ;
3e17bc3f
RN
1145 }
1146#endif //10.2 and up
5f0b2f22
SC
1147 else
1148 {
f5744893 1149 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 1150 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
1151 {
1152 wclass = kDocumentWindowClass ;
1153 }
1154 else
1155 {
1156 wclass = kPlainWindowClass ;
1157 }
5f0b2f22 1158 }
1542ea39 1159
52c3df99 1160 if ( wclass != kPlainWindowClass )
5f0b2f22 1161 {
52c3df99
DS
1162 if ( HasFlag( wxMINIMIZE_BOX ) )
1163 attr |= kWindowCollapseBoxAttribute ;
1164
1165 if ( HasFlag( wxMAXIMIZE_BOX ) )
1166 attr |= kWindowFullZoomAttribute ;
1167
1168 if ( HasFlag( wxRESIZE_BORDER ) )
1169 attr |= kWindowResizableAttribute ;
1170
1171 if ( HasFlag( wxCLOSE_BOX) )
1172 attr |= kWindowCloseBoxAttribute ;
5f0b2f22 1173 }
1542ea39 1174
52c3df99 1175 // turn on live resizing (OS X only)
eb630bf1 1176 if (UMAGetSystemVersion() >= 0x1000)
eb630bf1 1177 attr |= kWindowLiveResizeAttribute;
eb630bf1 1178
e353795e 1179 if ( HasFlag(wxSTAY_ON_TOP) )
e353795e 1180 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
6a7e6411 1181
902725ee 1182#if TARGET_API_MAC_OSX
789ae0cf
SC
1183 if ( m_macUsesCompositing )
1184 attr |= kWindowCompositingAttribute;
d66c3960 1185#endif
902725ee 1186
6a7e6411
RD
1187 if ( HasFlag(wxFRAME_SHAPED) )
1188 {
1189 WindowDefSpec customWindowDefSpec;
1190 customWindowDefSpec.defType = kWindowDefProcPtr;
1191 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1192
47ac4f9b 1193 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1194 attr, &theBoundsRect,
1195 (WindowRef*) &m_macWindow);
1196 }
1197 else
1198 {
47ac4f9b 1199 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1200 }
1201
e353795e
SC
1202 if ( err == noErr && m_macWindow != NULL && group != NULL )
1203 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1204
47ac4f9b 1205 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761 1206
a979e444
DS
1207 // the create commands are only for content rect,
1208 // so we have to set the size again as structure bounds
f83b6761
SC
1209 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1210
facd6764
SC
1211 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1212 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1f1c8bd4 1213 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
902725ee 1214
a979e444 1215#if TARGET_API_MAC_OSX
789ae0cf 1216 if ( m_macUsesCompositing )
f57d9215 1217 {
902725ee 1218 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
3103e8a9 1219 // the content view, so we have to retrieve it explicitly
902725ee 1220 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
789ae0cf
SC
1221 m_peer->GetControlRefAddr() ) ;
1222 if ( !m_peer->Ok() )
1223 {
1224 // compatibility mode fallback
1225 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1226 }
f57d9215 1227 }
3a9dc061 1228#endif
789ae0cf
SC
1229 {
1230 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1231 }
52c3df99
DS
1232
1233 // the root control level handler
5ca0d812 1234 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1235
9dc1d180
JS
1236 // Causes the inner part of the window not to be metal
1237 // if the style is used before window creation.
1238#if 0 // TARGET_API_MAC_OSX
8ec0a8fa
SC
1239 if ( m_macUsesCompositing && m_macWindow != NULL )
1240 {
1241 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1242 MacSetMetalAppearance( true ) ;
1243 }
1244#endif
52c3df99 1245
73fe67bd 1246 // the frame window event handler
e40298d5 1247 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1248 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1249
14c96cf2
SC
1250 DoSetWindowVariant( m_windowVariant ) ;
1251
facd6764 1252 m_macFocus = NULL ;
6a7e6411
RD
1253
1254 if ( HasFlag(wxFRAME_SHAPED) )
1255 {
1256 // default shape matches the window size
52c3df99
DS
1257 wxRegion rgn( 0, 0, w, h );
1258 SetShape( rgn );
6a7e6411 1259 }
3dfafdb9
RD
1260
1261 wxWindowCreateEvent event(this);
1262 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1263}
1264
5d2e69e8 1265void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1266{
5d2e69e8 1267 wxWindow::ClearBackground() ;
5f0b2f22
SC
1268}
1269
5f0b2f22
SC
1270// Raise the window to the top of the Z order
1271void wxTopLevelWindowMac::Raise()
1272{
7f3c339c 1273 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1274}
1275
1276// Lower the window to the bottom of the Z order
1277void wxTopLevelWindowMac::Lower()
1278{
76a5e5d2 1279 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1280}
1281
245f3581
DE
1282void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1283{
52c3df99 1284 if (s_macDeactivateWindow)
245f3581 1285 {
f02a4ffa
VZ
1286 wxLogTrace(TRACE_ACTIVATE,
1287 wxT("Doing delayed deactivation of %p"),
1288 s_macDeactivateWindow);
52c3df99 1289
245f3581
DE
1290 s_macDeactivateWindow->MacActivate(timestamp, false);
1291 }
1292}
1293
851b3a88 1294void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 1295{
f02a4ffa 1296 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
17f8d2a7 1297
52c3df99
DS
1298 if (s_macDeactivateWindow == this)
1299 s_macDeactivateWindow = NULL;
1300
245f3581 1301 MacDelayedDeactivation(timestamp);
8ab50549 1302 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1303}
1304
1305void wxTopLevelWindowMac::SetTitle(const wxString& title)
1306{
fb5246be 1307 wxWindow::SetLabel( title ) ;
16a76184 1308 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1309}
1310
d4bd6c97 1311wxString wxTopLevelWindowMac::GetTitle() const
fb5246be
WS
1312{
1313 return wxWindow::GetLabel();
1314}
1315
5f0b2f22
SC
1316bool wxTopLevelWindowMac::Show(bool show)
1317{
facd6764 1318 if ( !wxTopLevelWindowBase::Show(show) )
902725ee 1319 return false;
5f0b2f22 1320
21e77aa1
DS
1321 bool plainTransition = false;
1322
a979e444 1323#if wxUSE_SYSTEM_OPTIONS
21e77aa1
DS
1324 // code contributed by Ryan Wilcox December 18, 2003
1325 plainTransition = UMAGetSystemVersion() >= 0x1000 ;
1326 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1327 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1328#endif
52c3df99 1329
21e77aa1
DS
1330 if (show)
1331 {
8c8f6a4b 1332 if ( plainTransition )
1f90939c 1333 ::ShowWindow( (WindowRef)m_macWindow );
1f90939c 1334 else
52c3df99 1335 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
52c3df99 1336
1f90939c 1337 ::SelectWindow( (WindowRef)m_macWindow ) ;
52c3df99
DS
1338
1339 // because apps expect a size event to occur at this moment
a979e444 1340 wxSizeEvent event(GetSize() , m_windowId);
1f90939c
SC
1341 event.SetEventObject(this);
1342 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1343 }
1344 else
1345 {
8c8f6a4b 1346 if ( plainTransition )
21e77aa1 1347 ::HideWindow( (WindowRef)m_macWindow );
1f90939c 1348 else
21e77aa1 1349 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
5f0b2f22
SC
1350 }
1351
facd6764 1352 MacPropagateVisibilityChanged() ;
5f0b2f22 1353
902725ee 1354 return true ;
5f0b2f22
SC
1355}
1356
c5985061 1357bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
902725ee 1358{
c5985061
SC
1359 if ( show )
1360 {
1361 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1362 delete data ;
1363 data = new FullScreenData() ;
902725ee 1364
c5985061
SC
1365 m_macFullScreenData = data ;
1366 data->m_position = GetPosition() ;
1367 data->m_size = GetSize() ;
f3b2e2d7 1368 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
902725ee 1369
c5985061 1370 if ( style & wxFULLSCREEN_NOMENUBAR )
52c3df99
DS
1371 HideMenuBar() ;
1372
c5985061
SC
1373 wxRect client = wxGetClientDisplayRect() ;
1374
52c3df99 1375 int left , top , right , bottom ;
c5985061 1376 int x, y, w, h ;
902725ee 1377
c5985061
SC
1378 x = client.x ;
1379 y = client.y ;
1380 w = client.width ;
1381 h = client.height ;
902725ee 1382
c5985061
SC
1383 MacGetContentAreaInset( left , top , right , bottom ) ;
1384
1385 if ( style & wxFULLSCREEN_NOCAPTION )
1386 {
1387 y -= top ;
1388 h += top ;
1389 }
52c3df99 1390
c5985061
SC
1391 if ( style & wxFULLSCREEN_NOBORDER )
1392 {
1393 x -= left ;
1394 w += left + right ;
1395 h += bottom ;
1396 }
52c3df99 1397
c5985061
SC
1398 if ( style & wxFULLSCREEN_NOTOOLBAR )
1399 {
3c86150d
SC
1400 // TODO
1401 }
52c3df99 1402
3c86150d
SC
1403 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1404 {
1405 // TODO
c5985061 1406 }
52c3df99 1407
c5985061 1408 SetSize( x , y , w, h ) ;
172da31f 1409 if ( data->m_wasResizable )
f3b2e2d7 1410 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
c5985061
SC
1411 }
1412 else
1413 {
1414 ShowMenuBar() ;
1415 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
172da31f 1416 if ( data->m_wasResizable )
f3b2e2d7 1417 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
c5985061
SC
1418 SetPosition( data->m_position ) ;
1419 SetSize( data->m_size ) ;
52c3df99 1420
c5985061
SC
1421 delete data ;
1422 m_macFullScreenData = NULL ;
1423 }
52c3df99 1424
902725ee 1425 return false;
c5985061
SC
1426}
1427
902725ee
WS
1428bool wxTopLevelWindowMac::IsFullScreen() const
1429{
1430 return m_macFullScreenData != NULL ;
c5985061
SC
1431}
1432
30c23e74 1433void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
8ec0a8fa
SC
1434{
1435 if ( GetExtraStyle() == exStyle )
1436 return ;
30c23e74 1437
8ec0a8fa 1438 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
52c3df99 1439
8ec0a8fa
SC
1440#if TARGET_API_MAC_OSX
1441 if ( m_macUsesCompositing && m_macWindow != NULL )
1442 {
1443 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1444 if ( MacGetMetalAppearance() != metal )
1445 MacSetMetalAppearance( metal ) ;
1446 }
1447#endif
1448}
1449
30c23e74 1450// TODO: switch to structure bounds -
21e77aa1 1451// we are still using coordinates of the content view
21e77aa1 1452//
29281095
SC
1453void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1454{
21e77aa1
DS
1455 Rect content, structure ;
1456
29281095
SC
1457 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1458 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1459
1460 left = content.left - structure.left ;
1461 top = content.top - structure.top ;
1462 right = structure.right - content.right ;
1463 bottom = structure.bottom - content.bottom ;
1464}
1465
5f0b2f22
SC
1466void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1467{
c9698388 1468 m_cachedClippedRectValid = false ;
facd6764
SC
1469 Rect bounds = { y , x , y + height , x + width } ;
1470 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
4817190d 1471 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
5f0b2f22
SC
1472}
1473
facd6764 1474void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1475{
facd6764 1476 Rect bounds ;
52c3df99 1477
facd6764 1478 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1479
1480 if (x)
1481 *x = bounds.left ;
1482 if (y)
1483 *y = bounds.top ;
facd6764 1484}
52c3df99 1485
facd6764
SC
1486void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1487{
1488 Rect bounds ;
52c3df99 1489
facd6764 1490 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
52c3df99
DS
1491
1492 if (width)
1493 *width = bounds.right - bounds.left ;
1494 if (height)
1495 *height = bounds.bottom - bounds.top ;
facd6764 1496}
1542ea39 1497
facd6764
SC
1498void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1499{
1500 Rect bounds ;
52c3df99 1501
facd6764 1502 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
52c3df99
DS
1503
1504 if (width)
1505 *width = bounds.right - bounds.left ;
1506 if (height)
1507 *height = bounds.bottom - bounds.top ;
facd6764 1508}
1542ea39 1509
902725ee 1510void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
facd6764
SC
1511{
1512#if TARGET_API_MAC_OSX
789ae0cf 1513 wxASSERT_MSG( m_macUsesCompositing ,
facd6764 1514 wxT("Cannot set metal appearance on a non-compositing window") ) ;
902725ee
WS
1515
1516 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
facd6764
SC
1517 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1518#endif
1519}
1542ea39 1520
902725ee 1521bool wxTopLevelWindowMac::MacGetMetalAppearance() const
6aab345b
SC
1522{
1523#if TARGET_API_MAC_OSX
1524 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1525#else
1526 return false ;
1527#endif
1528}
1529
902725ee 1530void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
facd6764 1531{
52c3df99 1532 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
facd6764 1533}
1542ea39 1534
902725ee 1535wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
facd6764
SC
1536{
1537 UInt32 attr = 0 ;
52c3df99
DS
1538 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1539
facd6764 1540 return attr ;
5f0b2f22 1541}
a07c1212 1542
902725ee 1543void wxTopLevelWindowMac::MacPerformUpdates()
92346151
SC
1544{
1545#if TARGET_API_MAC_OSX
902725ee
WS
1546 if ( m_macUsesCompositing )
1547 {
92346151 1548#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
902725ee
WS
1549 // for composited windows this also triggers a redraw of all
1550 // invalid views in the window
52c3df99 1551 if ( UMAGetSystemVersion() >= 0x1030 )
902725ee
WS
1552 HIWindowFlush((WindowRef) m_macWindow) ;
1553 else
92346151 1554#endif
902725ee
WS
1555 {
1556 // the only way to trigger the redrawing on earlier systems is to call
1557 // ReceiveNextEvent
1558
1559 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1560 UInt32 currentEventClass = 0 ;
1561 UInt32 currentEventKind = 0 ;
1562 if ( currentEvent != NULL )
1563 {
1564 currentEventClass = ::GetEventClass( currentEvent ) ;
1565 currentEventKind = ::GetEventKind( currentEvent ) ;
1566 }
52c3df99 1567
902725ee
WS
1568 if ( currentEventClass != kEventClassMenu )
1569 {
1570 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1571 EventRef theEvent;
1572 OSStatus status = noErr ;
1573 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1574 }
1575 }
92346151
SC
1576 }
1577 else
1578#endif
1579 {
902725ee
WS
1580 BeginUpdate( (WindowRef) m_macWindow ) ;
1581
1582 RgnHandle updateRgn = NewRgn();
1583 if ( updateRgn )
1584 {
1585 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
1586 UpdateControls( (WindowRef)m_macWindow , updateRgn ) ;
52c3df99 1587
902725ee
WS
1588 // if ( !EmptyRgn( updateRgn ) )
1589 // MacDoRedraw( updateRgn , 0 , true) ;
52c3df99 1590
902725ee
WS
1591 DisposeRgn( updateRgn );
1592 }
52c3df99 1593
902725ee
WS
1594 EndUpdate( (WindowRef)m_macWindow ) ;
1595 QDFlushPortBuffer( GetWindowPort( (WindowRef)m_macWindow ) , NULL ) ;
92346151
SC
1596 }
1597}
1598
3feeb1e1
SC
1599// Attracts the users attention to this window if the application is
1600// inactive (should be called when a background event occurs)
1601
1602static pascal void wxMacNMResponse( NMRecPtr ptr )
1603{
1604 NMRemove( ptr ) ;
a979e444 1605 DisposePtr( (Ptr)ptr ) ;
3feeb1e1
SC
1606}
1607
3feeb1e1
SC
1608void wxTopLevelWindowMac::RequestUserAttention(int flags )
1609{
1610 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
52c3df99
DS
1611 static wxMacNMUPP nmupp( wxMacNMResponse );
1612
3feeb1e1
SC
1613 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1614 notificationRequest->qType = nmType ;
1615 notificationRequest->nmMark = 1 ;
1616 notificationRequest->nmIcon = 0 ;
1617 notificationRequest->nmSound = 0 ;
1618 notificationRequest->nmStr = NULL ;
1619 notificationRequest->nmResp = nmupp ;
52c3df99 1620
3feeb1e1
SC
1621 verify_noerr( NMInstall( notificationRequest ) ) ;
1622}
1623
facd6764
SC
1624// ---------------------------------------------------------------------------
1625// Shape implementation
1626// ---------------------------------------------------------------------------
1627
6a7e6411 1628
1542ea39
RD
1629bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1630{
902725ee 1631 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
6a7e6411
RD
1632 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1633
a979e444
DS
1634 // The empty region signifies that the shape
1635 // should be removed from the window.
6a7e6411
RD
1636 if ( region.IsEmpty() )
1637 {
1638 wxSize sz = GetClientSize();
1639 wxRegion rgn(0, 0, sz.x, sz.y);
3c393c0a
SC
1640 if ( rgn.IsEmpty() )
1641 return false ;
1642 else
1643 return SetShape(rgn);
6a7e6411
RD
1644 }
1645
1646 // Make a copy of the region
1647 RgnHandle shapeRegion = NewRgn();
1648 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1649
1650 // Dispose of any shape region we may already have
1651 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1652 if ( oldRgn )
1653 DisposeRgn(oldRgn);
1654
1655 // Save the region so we can use it later
1656 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1657
52c3df99 1658 // inform the window manager that the window has changed shape
6a7e6411 1659 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
52c3df99 1660
902725ee 1661 return true;
6a7e6411
RD
1662}
1663
6a7e6411
RD
1664// ---------------------------------------------------------------------------
1665// Support functions for shaped windows, based on Apple's CustomWindow sample at
1666// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1667// ---------------------------------------------------------------------------
1668
6a7e6411
RD
1669static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1670{
1671 GetWindowPortBounds(window, inRect);
a979e444
DS
1672 Point pt = { inRect->left, inRect->top };
1673
1674 QDLocalToGlobalPoint( GetWindowPort(window), &pt ) ;
6a7e6411
RD
1675 inRect->top = pt.v;
1676 inRect->left = pt.h;
1677 inRect->bottom += pt.v;
1678 inRect->right += pt.h;
1679}
1680
6a7e6411
RD
1681static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1682{
1683 /*------------------------------------------------------
1684 Define which options your custom window supports.
1685 --------------------------------------------------------*/
1686 //just enable everything for our demo
52c3df99
DS
1687 *(OptionBits*)param =
1688 //kWindowCanGrow |
1689 //kWindowCanZoom |
1690 //kWindowCanCollapse |
1691 //kWindowCanGetWindowRegion |
1692 //kWindowHasTitleBar |
1693 //kWindowSupportsDragHilite |
1694 kWindowCanDrawInCurrentPort |
1695 //kWindowCanMeasureTitle |
1696 kWindowWantsDisposeAtProcessDeath |
1697 kWindowSupportsGetGrowImageRegion |
1698 kWindowDefSupportsColorGrafPort;
1699
6a7e6411 1700 return 1;
1542ea39 1701}
6a7e6411
RD
1702
1703// The content region is left as a rectangle matching the window size, this is
1704// so the origin in the paint event, and etc. still matches what the
1705// programmer expects.
1706static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1707{
1708 SetEmptyRgn(rgn);
1709 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1710 if (win)
1711 {
b19bf058 1712 Rect r ;
52c3df99 1713 wxShapedMacWindowGetPos( window, &r ) ;
b19bf058 1714 RectRgn( rgn , &r ) ;
6a7e6411
RD
1715 }
1716}
1717
1718// The structure region is set to the shape given to the SetShape method.
1719static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1720{
1721 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1722
1723 SetEmptyRgn(rgn);
1724 if (cachedRegion)
1725 {
1726 Rect windowRect;
902725ee
WS
1727 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1728 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
6a7e6411 1729 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
902725ee 1730 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
6a7e6411
RD
1731 }
1732}
1733
6a7e6411
RD
1734static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1735{
52c3df99
DS
1736 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1737
1738 if (rgnRec == NULL)
1739 return paramErr;
6a7e6411 1740
52c3df99 1741 switch (rgnRec->regionCode)
6a7e6411
RD
1742 {
1743 case kWindowStructureRgn:
1744 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1745 break;
52c3df99 1746
6a7e6411
RD
1747 case kWindowContentRgn:
1748 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1749 break;
52c3df99 1750
6a7e6411
RD
1751 default:
1752 SetEmptyRgn(rgnRec->winRgn);
52c3df99
DS
1753 break;
1754 }
6a7e6411
RD
1755
1756 return noErr;
1757}
1758
52c3df99
DS
1759// Determine the region of the window which was hit
1760//
1761static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
6a7e6411 1762{
6a7e6411 1763 Point hitPoint;
52c3df99 1764 static RgnHandle tempRgn = NULL;
6a7e6411 1765
52c3df99
DS
1766 if (tempRgn == NULL)
1767 tempRgn = NewRgn();
6a7e6411 1768
52c3df99
DS
1769 // get the point clicked
1770 SetPt( &hitPoint, LoWord(param), HiWord(param) );
6a7e6411 1771
52c3df99 1772 // Mac OS 8.5 or later
6a7e6411 1773 wxShapedMacWindowStructureRegion(window, tempRgn);
52c3df99 1774 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
6a7e6411
RD
1775 return wInContent;
1776
52c3df99
DS
1777 // no significant area was hit
1778 return wNoHit;
6a7e6411
RD
1779}
1780
6a7e6411
RD
1781static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1782{
52c3df99 1783 switch (message)
6a7e6411
RD
1784 {
1785 case kWindowMsgHitTest:
52c3df99 1786 return wxShapedMacWindowHitTest(window, param);
6a7e6411
RD
1787
1788 case kWindowMsgGetFeatures:
52c3df99 1789 return wxShapedMacWindowGetFeatures(window, param);
6a7e6411
RD
1790
1791 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1792 case kWindowMsgGetRegion:
52c3df99
DS
1793 return wxShapedMacWindowGetRegion(window, param);
1794
1795 default:
1796 break;
6a7e6411
RD
1797 }
1798
1799 return 0;
1800}