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