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