]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
Avoid an assert in wxFrame::SetTitle
[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 {
538 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
3a9dc061
SC
539 Point clickLocation = windowMouseLocation ;
540#if TARGET_API_MAC_OSX
541 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
542#endif
543 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
30e0b1c9
SC
544 modifiers , (ControlActionUPP ) -1 ) ;
545 result = noErr ;
546 }
547 }
facd6764
SC
548 if ( cEvent.GetKind() == kEventMouseUp && wxTheApp->s_captureWindow )
549 {
550 wxTheApp->s_captureWindow = NULL ;
551 // update cursor ?
552 }
553 } // else if ( currentMouseWindow )
d88ffaaa
SC
554 else
555 {
556 // don't mess with controls we don't know about
557 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
558 // so we try sending them the correct control directly
559 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
e4b7e0b4 560 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
d88ffaaa
SC
561 {
562 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
563 Point clickLocation = windowMouseLocation ;
564#if TARGET_API_MAC_OSX
565 HIPoint hiPoint ;
566 hiPoint.x = clickLocation.h ;
567 hiPoint.y = clickLocation.v ;
568 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
569 clickLocation.h = (int)hiPoint.x ;
570 clickLocation.v = (int)hiPoint.y ;
571#endif
572 HandleControlClick( control , clickLocation ,
573 modifiers , (ControlActionUPP ) -1 ) ;
574 result = noErr ;
575 }
576 }
facd6764 577 return result ;
851b3a88 578}
facd6764
SC
579
580static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
581{
582 OSStatus result = eventNotHandledErr ;
851b3a88 583
facd6764
SC
584 wxMacCarbonEvent cEvent( event ) ;
585
586 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
e40298d5 587 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 588
e40298d5
JS
589 switch( GetEventKind( event ) )
590 {
e40298d5 591 case kEventWindowActivated :
facd6764
SC
592 {
593 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
facd6764
SC
594 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
595 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
596 wxevent.SetEventObject(toplevelWindow);
597 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 598 // we still sending an eventNotHandledErr in order to allow for default processing
e40298d5 599 break ;
facd6764 600 }
e40298d5 601 case kEventWindowDeactivated :
facd6764
SC
602 {
603 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
604 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
605 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
606 wxevent.SetEventObject(toplevelWindow);
607 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 608 // we still sending an eventNotHandledErr in order to allow for default processing
e40298d5 609 break ;
facd6764 610 }
16a76184
SC
611 case kEventWindowShown :
612 toplevelWindow->Refresh() ;
613 result = noErr ;
614 break ;
e40298d5
JS
615 case kEventWindowClose :
616 toplevelWindow->Close() ;
617 result = noErr ;
618 break ;
619 case kEventWindowBoundsChanged :
facd6764
SC
620 {
621 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
622 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
623 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
624 if ( attributes & kWindowBoundsChangeSizeChanged )
e40298d5 625 {
facd6764
SC
626 // according to the other ports we handle this within the OS level
627 // resize event, not within a wxSizeEvent
628 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
629 if ( frame )
630 {
631 #if wxUSE_STATUSBAR
632 frame->PositionStatusBar();
633 #endif
634 #if wxUSE_TOOLBAR
635 frame->PositionToolBar();
636 #endif
637 }
1542ea39 638
facd6764
SC
639 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
640 event.SetEventObject( toplevelWindow ) ;
851b3a88 641
facd6764 642 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
e40298d5 643 }
facd6764
SC
644 if ( attributes & kWindowBoundsChangeOriginChanged )
645 {
646 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
647 event.SetEventObject( toplevelWindow ) ;
648 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
649 }
650 result = noErr ;
e40298d5 651 break ;
facd6764 652 }
f81bfef9 653 case kEventWindowBoundsChanging :
facd6764
SC
654 {
655 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
656 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
83901ec2 657
facd6764
SC
658 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
659 {
29281095
SC
660 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
661 int left , top , right , bottom ;
662 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
663 wxRect r( newRect.left - left , newRect.top - top ,
664 newRect.right - newRect.left + left + right , newRect.bottom - newRect.top + top + bottom ) ;
facd6764
SC
665 // this is a EVT_SIZING not a EVT_SIZE type !
666 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
667 wxevent.SetEventObject( toplevelWindow ) ;
668 wxRect adjustR = r ;
669 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
facd6764 670 adjustR = wxevent.GetRect() ;
29281095 671
facd6764
SC
672 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
673 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
2c25bd55 674 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
facd6764
SC
675 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
676 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
677 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
2c25bd55 678 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
facd6764 679 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
4a5f2351 680 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
facd6764 681 if ( !EqualRect( &newRect , &adjustedRect ) )
1f919f38 682 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
f81bfef9 683 }
facd6764
SC
684
685 result = noErr ;
f81bfef9 686 break ;
facd6764 687 }
e40298d5
JS
688 default :
689 break ;
690 }
691 return result ;
851b3a88
SC
692}
693
facd6764 694pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
695{
696 OSStatus result = eventNotHandledErr ;
697
698 switch ( GetEventClass( event ) )
699 {
c5c9378c 700 case kEventClassKeyboard :
e40298d5 701 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 702 break ;
851b3a88 703 case kEventClassTextInput :
e40298d5 704 result = TextInputEventHandler( handler, event , data ) ;
851b3a88
SC
705 break ;
706 case kEventClassWindow :
facd6764 707 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
e40298d5 708 break ;
851b3a88 709 case kEventClassMouse :
facd6764 710 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
e40298d5 711 break ;
851b3a88
SC
712 default :
713 break ;
714 }
715 return result ;
716}
717
facd6764 718DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
851b3a88 719
5f0b2f22
SC
720// ---------------------------------------------------------------------------
721// wxWindowMac utility functions
722// ---------------------------------------------------------------------------
723
724// Find an item given the Macintosh Window Reference
725
facd6764
SC
726wxList wxWinMacWindowList(wxKEY_INTEGER);
727wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
5f0b2f22 728{
facd6764 729 wxNode *node = wxWinMacWindowList.Find((long)inWindowRef);
5f0b2f22
SC
730 if (!node)
731 return NULL;
eb22f2a6 732 return (wxTopLevelWindowMac *)node->GetData();
5f0b2f22
SC
733}
734
facd6764
SC
735void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
736void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
5f0b2f22
SC
737{
738 // adding NULL WindowRef is (first) surely a result of an error and
739 // (secondly) breaks menu command processing
427ff662 740 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
5f0b2f22 741
facd6764
SC
742 if ( !wxWinMacWindowList.Find((long)inWindowRef) )
743 wxWinMacWindowList.Append((long)inWindowRef, win);
5f0b2f22
SC
744}
745
facd6764 746void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
5f0b2f22
SC
747void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
748{
facd6764 749 wxWinMacWindowList.DeleteObject(win);
5f0b2f22
SC
750}
751
752
a15eb0a5
SC
753// ----------------------------------------------------------------------------
754// wxTopLevelWindowMac creation
755// ----------------------------------------------------------------------------
756
245f3581 757wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 758
c5985061
SC
759typedef struct
760{
761 wxPoint m_position ;
762 wxSize m_size ;
763} FullScreenData ;
764
a15eb0a5
SC
765void wxTopLevelWindowMac::Init()
766{
767 m_iconized =
768 m_maximizeOnShow = FALSE;
6a17ca35 769 m_macWindow = NULL ;
facd6764
SC
770#if TARGET_API_MAC_OSX
771 m_macUsesCompositing = TRUE;
772#else
773 m_macUsesCompositing = FALSE;
774#endif
7c091673 775 m_macEventHandler = NULL ;
c5985061 776 m_macFullScreenData = NULL ;
a15eb0a5
SC
777}
778
118f012e
SC
779class wxMacDeferredWindowDeleter : public wxObject
780{
781public :
1542ea39
RD
782 wxMacDeferredWindowDeleter( WindowRef windowRef )
783 {
784 m_macWindow = windowRef ;
118f012e 785 }
1542ea39
RD
786 virtual ~wxMacDeferredWindowDeleter()
787 {
788 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e
SC
789 }
790 protected :
791 WindowRef m_macWindow ;
792} ;
793
a15eb0a5
SC
794bool wxTopLevelWindowMac::Create(wxWindow *parent,
795 wxWindowID id,
796 const wxString& title,
797 const wxPoint& pos,
798 const wxSize& size,
799 long style,
800 const wxString& name)
801{
802 // init our fields
803 Init();
804
805 m_windowStyle = style;
806
807 SetName(name);
808
809 m_windowId = id == -1 ? NewControlId() : id;
810
facd6764
SC
811 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
812
94d1d0f4
SC
813 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
814
a15eb0a5
SC
815 wxTopLevelWindows.Append(this);
816
817 if ( parent )
818 parent->AddChild(this);
819
820 return TRUE;
821}
822
823wxTopLevelWindowMac::~wxTopLevelWindowMac()
824{
6a17ca35
SC
825 if ( m_macWindow )
826 {
827 wxToolTip::NotifyWindowDelete(m_macWindow) ;
118f012e 828 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 829 }
1542ea39 830
7c091673
SC
831 if ( m_macEventHandler )
832 {
833 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
834 m_macEventHandler = NULL ;
835 }
851b3a88 836
5f0b2f22
SC
837 wxRemoveMacWindowAssociation( this ) ;
838
a15eb0a5
SC
839 if ( wxModelessWindows.Find(this) )
840 wxModelessWindows.DeleteObject(this);
c5985061
SC
841
842 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
843 delete data ;
844 m_macFullScreenData = NULL ;
a15eb0a5
SC
845}
846
847
848// ----------------------------------------------------------------------------
849// wxTopLevelWindowMac maximize/minimize
850// ----------------------------------------------------------------------------
851
852void wxTopLevelWindowMac::Maximize(bool maximize)
853{
1f90939c
SC
854 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
855 wxMacWindowClipper clip (this);
b7aec135 856 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
a15eb0a5
SC
857}
858
859bool wxTopLevelWindowMac::IsMaximized() const
860{
b7aec135 861 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
862}
863
864void wxTopLevelWindowMac::Iconize(bool iconize)
865{
b7aec135
SC
866 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
867 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
868}
869
870bool wxTopLevelWindowMac::IsIconized() const
871{
b7aec135 872 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
873}
874
875void wxTopLevelWindowMac::Restore()
876{
877 // not available on mac
878}
879
880// ----------------------------------------------------------------------------
881// wxTopLevelWindowMac misc
882// ----------------------------------------------------------------------------
883
facd6764
SC
884wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
885{
886 return wxPoint(0,0) ;
887}
888
a15eb0a5
SC
889void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
890{
891 // this sets m_icon
892 wxTopLevelWindowBase::SetIcon(icon);
893}
5f0b2f22 894
facd6764
SC
895void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
896{
897 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
898
899 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
900 {
901 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
902 }
903}
904
949cb163
SC
905void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
906{
907 if ( m_macEventHandler != NULL )
908 {
909 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
910 }
911 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
912 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
913}
914
5f0b2f22
SC
915void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
916 const wxPoint& pos,
917 const wxSize& size,
918 long style,
1542ea39 919 const wxString& name )
5f0b2f22 920{
47ac4f9b 921 OSStatus err = noErr ;
e40298d5
JS
922 SetName(name);
923 m_windowStyle = style;
924 m_isShown = FALSE;
1542ea39 925
e40298d5 926 // create frame.
1542ea39 927
5f0b2f22 928 Rect theBoundsRect;
1542ea39 929
facd6764
SC
930 int x = (int)pos.x;
931 int y = (int)pos.y;
932 if ( y < 50 )
933 y = 50 ;
934 if ( x < 20 )
935 x = 20 ;
1542ea39 936
facd6764
SC
937 int w = WidthDefault(size.x);
938 int h = HeightDefault(size.y);
1542ea39 939
facd6764 940 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 941
5f0b2f22 942 // translate the window attributes in the appropriate window class and attributes
1542ea39 943
5f0b2f22
SC
944 WindowClass wclass = 0;
945 WindowAttributes attr = kWindowNoAttributes ;
1542ea39 946
f5744893 947 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 948 {
1542ea39 949 if (
f5744893
SC
950 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
951 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
952 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 953 )
5f0b2f22 954 {
f5744893
SC
955 wclass = kFloatingWindowClass ;
956 if ( HasFlag(wxTINY_CAPTION_VERT) )
957 {
958 attr |= kWindowSideTitlebarAttribute ;
959 }
960 }
961 else
962 {
963 wclass = kPlainWindowClass ;
5f0b2f22
SC
964 }
965 }
966 else if ( HasFlag( wxCAPTION ) )
967 {
1542ea39 968 wclass = kDocumentWindowClass ;
5f0b2f22
SC
969 }
970 else
971 {
f5744893 972 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 973 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
974 {
975 wclass = kDocumentWindowClass ;
976 }
977 else
978 {
979 wclass = kPlainWindowClass ;
980 }
5f0b2f22 981 }
1542ea39 982
47ac4f9b 983 if ( HasFlag( wxMINIMIZE_BOX ) )
5f0b2f22 984 {
5f0b2f22
SC
985 attr |= kWindowCollapseBoxAttribute ;
986 }
47ac4f9b
SC
987 if ( HasFlag( wxMAXIMIZE_BOX ) )
988 {
989 attr |= kWindowFullZoomAttribute ;
990 }
5f0b2f22
SC
991 if ( HasFlag( wxRESIZE_BORDER ) )
992 {
993 attr |= kWindowResizableAttribute ;
994 }
47ac4f9b 995 if ( HasFlag( wxCLOSE_BOX) )
5f0b2f22
SC
996 {
997 attr |= kWindowCloseBoxAttribute ;
998 }
1542ea39 999
eb630bf1
DS
1000 if (UMAGetSystemVersion() >= 0x1000)
1001 {
facd6764 1002 // turn on live resizing (OS X only)
eb630bf1
DS
1003 attr |= kWindowLiveResizeAttribute;
1004 }
1005
6a7e6411 1006 if (HasFlag(wxSTAY_ON_TOP))
eb630bf1 1007 wclass = kUtilityWindowClass;
6a7e6411 1008
facd6764
SC
1009#if TARGET_API_MAC_OSX
1010 attr |= kWindowCompositingAttribute;
d66c3960 1011#endif
1f90939c 1012
6a7e6411
RD
1013 if ( HasFlag(wxFRAME_SHAPED) )
1014 {
1015 WindowDefSpec customWindowDefSpec;
1016 customWindowDefSpec.defType = kWindowDefProcPtr;
1017 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1018
47ac4f9b 1019 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1020 attr, &theBoundsRect,
1021 (WindowRef*) &m_macWindow);
1022 }
1023 else
1024 {
47ac4f9b 1025 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1026 }
1027
47ac4f9b 1028 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761
SC
1029
1030 // the create commands are only for content rect, so we have to set the size again as
1031 // structure bounds
1032 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1033
facd6764
SC
1034 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1035 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
21fd5529 1036 m_peer = new wxMacControl() ;
3a9dc061
SC
1037#if TARGET_API_MAC_OSX
1038 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1039 // the content view, so we have to retrieve it explicitely
1040 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
5ca0d812 1041 m_peer->GetControlRefAddr() ) ;
f57d9215
SC
1042 if ( !m_peer->Ok() )
1043 {
1044 // compatibility mode fallback
5ca0d812 1045 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
f57d9215 1046 }
3a9dc061 1047#else
5ca0d812 1048 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
3a9dc061 1049#endif
73fe67bd 1050 // the root control level handleer
5ca0d812 1051 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1052
73fe67bd 1053 // the frame window event handler
e40298d5 1054 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1055 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1056
facd6764 1057 m_macFocus = NULL ;
6a7e6411
RD
1058
1059 if ( HasFlag(wxFRAME_SHAPED) )
1060 {
1061 // default shape matches the window size
facd6764 1062 wxRegion rgn(0, 0, w, h);
6a7e6411
RD
1063 SetShape(rgn);
1064 }
3dfafdb9
RD
1065
1066 wxWindowCreateEvent event(this);
1067 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1068}
1069
5d2e69e8 1070void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1071{
5d2e69e8 1072 wxWindow::ClearBackground() ;
5f0b2f22
SC
1073}
1074
5f0b2f22
SC
1075// Raise the window to the top of the Z order
1076void wxTopLevelWindowMac::Raise()
1077{
7f3c339c 1078 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1079}
1080
1081// Lower the window to the bottom of the Z order
1082void wxTopLevelWindowMac::Lower()
1083{
76a5e5d2 1084 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1085}
1086
851b3a88 1087
245f3581
DE
1088void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1089{
1090 if(s_macDeactivateWindow)
1091 {
365796b1 1092 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
245f3581
DE
1093 s_macDeactivateWindow->MacActivate(timestamp, false);
1094 }
1095}
1096
851b3a88 1097void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 1098{
17f8d2a7
VZ
1099 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1100
245f3581
DE
1101 if(s_macDeactivateWindow==this)
1102 s_macDeactivateWindow=NULL;
1103 MacDelayedDeactivation(timestamp);
8ab50549 1104 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1105}
1106
1107void wxTopLevelWindowMac::SetTitle(const wxString& title)
1108{
1109 wxWindow::SetTitle( title ) ;
16a76184 1110 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1111}
1112
1113bool wxTopLevelWindowMac::Show(bool show)
1114{
facd6764 1115 if ( !wxTopLevelWindowBase::Show(show) )
5f0b2f22
SC
1116 return FALSE;
1117
1118 if (show)
1542ea39 1119 {
1f90939c
SC
1120 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1121 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1122 {
1123 ::ShowWindow( (WindowRef)m_macWindow );
1124 }
1125 else
1126 #endif
1127 {
1128 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
1129 }
1130 ::SelectWindow( (WindowRef)m_macWindow ) ;
facd6764
SC
1131 // as apps expect a size event to occur at this moment
1132 wxSizeEvent event( GetSize() , m_windowId);
1f90939c
SC
1133 event.SetEventObject(this);
1134 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1135 }
1136 else
1137 {
1f90939c
SC
1138 #if wxUSE_SYSTEM_OPTIONS
1139 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1140 {
1141 ::HideWindow((WindowRef) m_macWindow );
1142 }
1143 else
1144 #endif
1145 {
1146 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
1147 }
5f0b2f22
SC
1148 }
1149
facd6764 1150 MacPropagateVisibilityChanged() ;
5f0b2f22 1151
facd6764 1152 return TRUE ;
5f0b2f22
SC
1153}
1154
c5985061
SC
1155bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1156{
1157 if ( show )
1158 {
1159 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1160 delete data ;
1161 data = new FullScreenData() ;
1162
1163 m_macFullScreenData = data ;
1164 data->m_position = GetPosition() ;
1165 data->m_size = GetSize() ;
1166
1167 if ( style & wxFULLSCREEN_NOMENUBAR )
1168 {
1169 HideMenuBar() ;
1170 }
1171 int left , top , right , bottom ;
1172 wxRect client = wxGetClientDisplayRect() ;
1173
1174 int x, y, w, h ;
1175
1176 x = client.x ;
1177 y = client.y ;
1178 w = client.width ;
1179 h = client.height ;
1180
1181 MacGetContentAreaInset( left , top , right , bottom ) ;
1182
1183 if ( style & wxFULLSCREEN_NOCAPTION )
1184 {
1185 y -= top ;
1186 h += top ;
1187 }
1188 if ( style & wxFULLSCREEN_NOBORDER )
1189 {
1190 x -= left ;
1191 w += left + right ;
1192 h += bottom ;
1193 }
1194 if ( style & wxFULLSCREEN_NOTOOLBAR )
1195 {
3c86150d
SC
1196 // TODO
1197 }
1198 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1199 {
1200 // TODO
c5985061
SC
1201 }
1202 SetSize( x , y , w, h ) ;
1203 }
1204 else
1205 {
1206 ShowMenuBar() ;
1207 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1208 SetPosition( data->m_position ) ;
1209 SetSize( data->m_size ) ;
1210 delete data ;
1211 m_macFullScreenData = NULL ;
1212 }
1213 return FALSE;
1214}
1215
1216bool wxTopLevelWindowMac::IsFullScreen() const
1217{
1218 return m_macFullScreenData != NULL ;
1219}
1220
facd6764
SC
1221// we are still using coordinates of the content view, todo switch to structure bounds
1222
29281095
SC
1223void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1224{
1225 Rect content ;
1226 Rect structure ;
1227 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1228 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1229
1230 left = content.left - structure.left ;
1231 top = content.top - structure.top ;
1232 right = structure.right - content.right ;
1233 bottom = structure.bottom - content.bottom ;
1234}
1235
5f0b2f22
SC
1236void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1237{
facd6764
SC
1238 Rect bounds = { y , x , y + height , x + width } ;
1239 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
5f0b2f22
SC
1240}
1241
facd6764 1242void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1243{
facd6764
SC
1244 Rect bounds ;
1245 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1246 if(x) *x = bounds.left ;
1247 if(y) *y = bounds.top ;
1248}
1249void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1250{
1251 Rect bounds ;
1252 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1253 if(width) *width = bounds.right - bounds.left ;
1254 if(height) *height = bounds.bottom - bounds.top ;
1255}
1542ea39 1256
facd6764
SC
1257void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1258{
1259 Rect bounds ;
1260 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1261 if(width) *width = bounds.right - bounds.left ;
1262 if(height) *height = bounds.bottom - bounds.top ;
1263}
1542ea39 1264
facd6764
SC
1265void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1266{
1267#if TARGET_API_MAC_OSX
1268 UInt32 attr = 0 ;
1269 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1270 wxASSERT_MSG( attr & kWindowCompositingAttribute ,
1271 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1272
1273 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1274 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1275#endif
1276}
1542ea39 1277
6aab345b
SC
1278bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1279{
1280#if TARGET_API_MAC_OSX
1281 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1282#else
1283 return false ;
1284#endif
1285}
1286
facd6764
SC
1287void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1288{
1289 ChangeWindowAttributes ( (WindowRef) m_macWindow , attributesToSet, attributesToClear ) ;
1290}
1542ea39 1291
facd6764
SC
1292wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1293{
1294 UInt32 attr = 0 ;
1295 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1296 return attr ;
5f0b2f22 1297}
a07c1212 1298
facd6764
SC
1299// ---------------------------------------------------------------------------
1300// Shape implementation
1301// ---------------------------------------------------------------------------
1302
6a7e6411 1303
1542ea39
RD
1304bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1305{
6a7e6411
RD
1306 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1307 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1308
1309 // The empty region signifies that the shape should be removed from the
1310 // window.
1311 if ( region.IsEmpty() )
1312 {
1313 wxSize sz = GetClientSize();
1314 wxRegion rgn(0, 0, sz.x, sz.y);
1315 return SetShape(rgn);
1316 }
1317
1318 // Make a copy of the region
1319 RgnHandle shapeRegion = NewRgn();
1320 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1321
1322 // Dispose of any shape region we may already have
1323 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1324 if ( oldRgn )
1325 DisposeRgn(oldRgn);
1326
1327 // Save the region so we can use it later
1328 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1329
1330 // Tell the window manager that the window has changed shape
1331 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1332 return TRUE;
1333}
1334
6a7e6411
RD
1335// ---------------------------------------------------------------------------
1336// Support functions for shaped windows, based on Apple's CustomWindow sample at
1337// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1338// ---------------------------------------------------------------------------
1339
6a7e6411
RD
1340static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1341{
1342 GetWindowPortBounds(window, inRect);
1343 Point pt = {inRect->left, inRect->top};
d6fd667b 1344 QDLocalToGlobalPoint( GetWindowPort(window) , &pt ) ;
6a7e6411
RD
1345 inRect->top = pt.v;
1346 inRect->left = pt.h;
1347 inRect->bottom += pt.v;
1348 inRect->right += pt.h;
1349}
1350
1351
1352static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1353{
1354 /*------------------------------------------------------
1355 Define which options your custom window supports.
1356 --------------------------------------------------------*/
1357 //just enable everything for our demo
1358 *(OptionBits*)param=//kWindowCanGrow|
1359 //kWindowCanZoom|
1360 //kWindowCanCollapse|
1361 //kWindowCanGetWindowRegion|
1362 //kWindowHasTitleBar|
1363 //kWindowSupportsDragHilite|
1364 kWindowCanDrawInCurrentPort|
1365 //kWindowCanMeasureTitle|
1366 kWindowWantsDisposeAtProcessDeath|
ae53adb4 1367 kWindowSupportsGetGrowImageRegion|
6a7e6411
RD
1368 kWindowDefSupportsColorGrafPort;
1369 return 1;
1542ea39 1370}
6a7e6411
RD
1371
1372// The content region is left as a rectangle matching the window size, this is
1373// so the origin in the paint event, and etc. still matches what the
1374// programmer expects.
1375static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1376{
1377 SetEmptyRgn(rgn);
1378 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1379 if (win)
1380 {
b19bf058
SC
1381 Rect r ;
1382 wxShapedMacWindowGetPos(window, &r ) ;
1383 RectRgn( rgn , &r ) ;
6a7e6411
RD
1384 }
1385}
1386
1387// The structure region is set to the shape given to the SetShape method.
1388static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1389{
1390 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1391
1392 SetEmptyRgn(rgn);
1393 if (cachedRegion)
1394 {
1395 Rect windowRect;
1396 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1397 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1398 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1399 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1400 }
1401}
1402
1403
1404
1405static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1406{
1407 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1408
1409 switch(rgnRec->regionCode)
1410 {
1411 case kWindowStructureRgn:
1412 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1413 break;
1414 case kWindowContentRgn:
1415 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1416 break;
1417 default:
1418 SetEmptyRgn(rgnRec->winRgn);
1419 } //switch
1420
1421 return noErr;
1422}
1423
1424
1425static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1426{
1427 /*------------------------------------------------------
1428 Determine the region of the window which was hit
1429 --------------------------------------------------------*/
1430 Point hitPoint;
1431 static RgnHandle tempRgn=nil;
1432
1433 if(!tempRgn)
1434 tempRgn=NewRgn();
1435
1436 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1437
1438 //Mac OS 8.5 or later
1439 wxShapedMacWindowStructureRegion(window, tempRgn);
1440 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1441 return wInContent;
1442
1443 return wNoHit;//no significant area was hit.
1444}
1445
1446
1447static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1448{
1449 switch(message)
1450 {
1451 case kWindowMsgHitTest:
1452 return wxShapedMacWindowHitTest(window,param);
1453
1454 case kWindowMsgGetFeatures:
1455 return wxShapedMacWindowGetFeatures(window,param);
1456
1457 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1458 case kWindowMsgGetRegion:
1459 return wxShapedMacWindowGetRegion(window,param);
1460 }
1461
1462 return 0;
1463}
1925be65 1464
6a7e6411 1465// ---------------------------------------------------------------------------
1925be65 1466