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