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