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