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