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