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