]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
some setup.h changes for wxMovieCtrl
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
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"
34 #include "wx/frame.h"
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/settings.h"
39 #endif //WX_PRECOMP
40
41 #include "wx/mac/uma.h"
42 #include "wx/mac/aga.h"
43 #include "wx/app.h"
44 #include "wx/tooltip.h"
45 #include "wx/dnd.h"
46 #if wxUSE_SYSTEM_OPTIONS
47 #include "wx/sysopt.h"
48 #endif
49
50 #include <ToolUtils.h>
51
52 //For targeting OSX
53 #include "wx/mac/private.h"
54
55 // ----------------------------------------------------------------------------
56 // globals
57 // ----------------------------------------------------------------------------
58
59 // list of all frames and modeless dialogs
60 wxWindowList wxModelessWindows;
61
62 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
63
64 // ============================================================================
65 // wxTopLevelWindowMac implementation
66 // ============================================================================
67
68 BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
69 END_EVENT_TABLE()
70
71
72 // ---------------------------------------------------------------------------
73 // Carbon Events
74 // ---------------------------------------------------------------------------
75
76 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
77
78 static const EventTypeSpec eventList[] =
79 {
80 // TODO remove control related event like key and mouse (except for WindowLeave events)
81 #if 1
82 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
83
84 { kEventClassKeyboard, kEventRawKeyDown } ,
85 { kEventClassKeyboard, kEventRawKeyRepeat } ,
86 { kEventClassKeyboard, kEventRawKeyUp } ,
87 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
88 #endif
89
90 { kEventClassWindow , kEventWindowShown } ,
91 { kEventClassWindow , kEventWindowActivated } ,
92 { kEventClassWindow , kEventWindowDeactivated } ,
93 { kEventClassWindow , kEventWindowBoundsChanging } ,
94 { kEventClassWindow , kEventWindowBoundsChanged } ,
95 { kEventClassWindow , kEventWindowClose } ,
96
97 // we have to catch these events on the toplevel window level, as controls don't get the
98 // raw mouse events anymore
99
100 { kEventClassMouse , kEventMouseDown } ,
101 { kEventClassMouse , kEventMouseUp } ,
102 { kEventClassMouse , kEventMouseWheelMoved } ,
103 { kEventClassMouse , kEventMouseMoved } ,
104 { kEventClassMouse , kEventMouseDragged } ,
105 } ;
106
107 static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
108 {
109 OSStatus result = eventNotHandledErr ;
110
111 wxWindow* focus = wxWindow::FindFocus() ;
112 char charCode ;
113 UInt32 keyCode ;
114 UInt32 modifiers ;
115 Point point ;
116
117 EventRef rawEvent ;
118
119 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
120
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
127 switch ( GetEventKind( event ) )
128 {
129 case kEventTextInputUnicodeForKeyEvent :
130 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
131 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
132 wxControl* control = wxDynamicCast( focus , wxControl ) ;
133 if ( control )
134 {
135 ControlRef macControl = (ControlRef) control->GetHandle() ;
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
144 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
145 UInt32 message = (keyCode << 8) + charCode;
146
147 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
148 focus , message , modifiers , when , point.h , point.v ) )
149 {
150 result = noErr ;
151 }
152 */
153 break ;
154 }
155
156 return result ;
157 }
158
159 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
160 {
161 OSStatus result = eventNotHandledErr ;
162
163 wxWindow* focus = wxWindow::FindFocus() ;
164 if ( focus == NULL )
165 return result ;
166
167 char charCode ;
168 UInt32 keyCode ;
169 UInt32 modifiers ;
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 :
184 {
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 ) ;
194 }
195 break ;
196 case kEventRawKeyUp :
197 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
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
218 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
219 {
220 event.m_keyCode = WXK_CONTROL ;
221 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
222 focus->GetEventHandler()->ProcessEvent( event ) ;
223 }
224 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
225 {
226 event.m_keyCode = WXK_SHIFT ;
227 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
228 focus->GetEventHandler()->ProcessEvent( event ) ;
229 }
230 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
231 {
232 event.m_keyCode = WXK_ALT ;
233 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
234 focus->GetEventHandler()->ProcessEvent( event ) ;
235 }
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 }
242 wxTheApp->s_lastModifiers = modifiers ;
243 }
244 break ;
245 }
246
247 return result ;
248 }
249
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
253
254 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
255
256 wxWindow* g_MacLastWindow = NULL ;
257
258 static EventMouseButton lastButton = 0 ;
259
260 static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
261 {
262 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
263 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
264
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) )
280 {
281 button = kEventMouseButtonSecondary ;
282 }
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 ;
289
290 if ( button == 0 )
291 lastButton = 0 ;
292 else if ( lastButton )
293 button = lastButton ;
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 )
300 {
301 case kEventMouseButtonPrimary :
302 wxevent.m_leftDown = true ;
303 break ;
304 case kEventMouseButtonSecondary :
305 wxevent.m_rightDown = true ;
306 break ;
307 case kEventMouseButtonTertiary :
308 wxevent.m_middleDown = true ;
309 break ;
310 }
311 }
312 // translate into wx types
313 switch ( cEvent.GetKind() )
314 {
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 ;
343 case kEventMouseWheelMoved :
344 {
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;
353 break ;
354 }
355 default :
356 wxevent.SetEventType(wxEVT_MOTION ) ;
357 break ;
358 }
359 }
360
361 ControlRef 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 ;
382 UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
383 if ( MacPtInRect( location , &r ) )
384 {
385 ControlHandle child = wxMacFindSubControl( location , sibling , outPart ) ;
386 if ( child )
387 return child ;
388 else
389 {
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 ) ;
396 return sibling ;
397 }
398 }
399 }
400 }
401 }
402 return NULL ;
403 }
404
405 ControlRef wxMacFindControlUnderMouse( Point location , WindowRef window , ControlPartCode *outPart )
406 {
407 #if TARGET_API_MAC_OSX
408 if ( UMAGetSystemVersion() >= 0x1030 )
409 return FindControlUnderMouse( location , window , outPart ) ;
410 #endif
411 ControlRef rootControl = NULL ;
412 verify_noerr( GetRootControl( window , &rootControl ) ) ;
413 return wxMacFindSubControl( location , rootControl , outPart ) ;
414
415 }
416 pascal 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 ;
430 ControlRef control = NULL ;
431
432 if ( window )
433 {
434 QDGlobalToLocalPoint( UMAGetWindowPort(window ) , &windowMouseLocation ) ;
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 ;
443 control = wxMacFindControlUnderMouse( windowMouseLocation , window , &part ) ;
444 // if there is no control below the mouse position, send the event to the toplevel window itself
445 if ( control == 0 )
446 currentMouseWindow = (wxWindow*) data ;
447 else
448 {
449 currentMouseWindow = wxFindControlFromMacControl( control ) ;
450 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
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 }
461 }
462 }
463
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 }
523
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;
534 else
535 {
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
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 {
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 }
563 result = noErr ;
564 }
565 }
566 if ( cEvent.GetKind() == kEventMouseUp && wxTheApp->s_captureWindow )
567 {
568 wxTheApp->s_captureWindow = NULL ;
569 // update cursor ?
570 }
571 } // else if ( currentMouseWindow )
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 ;
578 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
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 }
595 return result ;
596 }
597
598 static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
599 {
600 OSStatus result = eventNotHandledErr ;
601
602 wxMacCarbonEvent cEvent( event ) ;
603
604 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
605 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
606
607 switch( GetEventKind( event ) )
608 {
609 case kEventWindowActivated :
610 {
611 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
612 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
613 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
614 wxevent.SetEventObject(toplevelWindow);
615 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
616 // we still sending an eventNotHandledErr in order to allow for default processing
617 break ;
618 }
619 case kEventWindowDeactivated :
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);
626 // we still sending an eventNotHandledErr in order to allow for default processing
627 break ;
628 }
629 case kEventWindowShown :
630 toplevelWindow->Refresh() ;
631 result = noErr ;
632 break ;
633 case kEventWindowClose :
634 toplevelWindow->Close() ;
635 result = noErr ;
636 break ;
637 case kEventWindowBoundsChanged :
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 )
643 {
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 }
656
657 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
658 event.SetEventObject( toplevelWindow ) ;
659
660 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
661 }
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 ;
669 break ;
670 }
671 case kEventWindowBoundsChanging :
672 {
673 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
674 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
675
676 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
677 {
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 ) ;
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) )
688 adjustR = wxevent.GetRect() ;
689
690 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
691 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
692 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
693 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
694 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
695 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
696 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
697 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
698 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
699 if ( !EqualRect( &newRect , &adjustedRect ) )
700 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
701 }
702
703 result = noErr ;
704 break ;
705 }
706 default :
707 break ;
708 }
709 return result ;
710 }
711
712 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
713 {
714 OSStatus result = eventNotHandledErr ;
715
716 switch ( GetEventClass( event ) )
717 {
718 case kEventClassKeyboard :
719 result = KeyboardEventHandler( handler, event , data ) ;
720 break ;
721 case kEventClassTextInput :
722 result = TextInputEventHandler( handler, event , data ) ;
723 break ;
724 case kEventClassWindow :
725 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
726 break ;
727 case kEventClassMouse :
728 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
729 break ;
730 default :
731 break ;
732 }
733 return result ;
734 }
735
736 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
737
738 // ---------------------------------------------------------------------------
739 // wxWindowMac utility functions
740 // ---------------------------------------------------------------------------
741
742 // Find an item given the Macintosh Window Reference
743
744 wxList wxWinMacWindowList(wxKEY_INTEGER);
745 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
746 {
747 wxNode *node = wxWinMacWindowList.Find((long)inWindowRef);
748 if (!node)
749 return NULL;
750 return (wxTopLevelWindowMac *)node->GetData();
751 }
752
753 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
754 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
755 {
756 // adding NULL WindowRef is (first) surely a result of an error and
757 // (secondly) breaks menu command processing
758 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
759
760 if ( !wxWinMacWindowList.Find((long)inWindowRef) )
761 wxWinMacWindowList.Append((long)inWindowRef, win);
762 }
763
764 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
765 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
766 {
767 wxWinMacWindowList.DeleteObject(win);
768 }
769
770
771 // ----------------------------------------------------------------------------
772 // wxTopLevelWindowMac creation
773 // ----------------------------------------------------------------------------
774
775 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
776
777 typedef struct
778 {
779 wxPoint m_position ;
780 wxSize m_size ;
781 } FullScreenData ;
782
783 void wxTopLevelWindowMac::Init()
784 {
785 m_iconized =
786 m_maximizeOnShow = FALSE;
787 m_macWindow = NULL ;
788 #if TARGET_API_MAC_OSX
789 m_macUsesCompositing = TRUE;
790 #else
791 m_macUsesCompositing = FALSE;
792 #endif
793 m_macEventHandler = NULL ;
794 m_macFullScreenData = NULL ;
795 }
796
797 class wxMacDeferredWindowDeleter : public wxObject
798 {
799 public :
800 wxMacDeferredWindowDeleter( WindowRef windowRef )
801 {
802 m_macWindow = windowRef ;
803 }
804 virtual ~wxMacDeferredWindowDeleter()
805 {
806 UMADisposeWindow( (WindowRef) m_macWindow ) ;
807 }
808 protected :
809 WindowRef m_macWindow ;
810 } ;
811
812 bool wxTopLevelWindowMac::Create(wxWindow *parent,
813 wxWindowID id,
814 const wxString& title,
815 const wxPoint& pos,
816 const wxSize& size,
817 long style,
818 const wxString& name)
819 {
820 // init our fields
821 Init();
822
823 m_windowStyle = style;
824
825 SetName(name);
826
827 m_windowId = id == -1 ? NewControlId() : id;
828 wxWindow::SetTitle( title ) ;
829
830 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
831
832 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
833
834 wxTopLevelWindows.Append(this);
835
836 if ( parent )
837 parent->AddChild(this);
838
839 return TRUE;
840 }
841
842 wxTopLevelWindowMac::~wxTopLevelWindowMac()
843 {
844 if ( m_macWindow )
845 {
846 wxToolTip::NotifyWindowDelete(m_macWindow) ;
847 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
848 }
849
850 if ( m_macEventHandler )
851 {
852 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
853 m_macEventHandler = NULL ;
854 }
855
856 wxRemoveMacWindowAssociation( this ) ;
857
858 if ( wxModelessWindows.Find(this) )
859 wxModelessWindows.DeleteObject(this);
860
861 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
862 delete data ;
863 m_macFullScreenData = NULL ;
864 }
865
866
867 // ----------------------------------------------------------------------------
868 // wxTopLevelWindowMac maximize/minimize
869 // ----------------------------------------------------------------------------
870
871 void wxTopLevelWindowMac::Maximize(bool maximize)
872 {
873 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
874 wxMacWindowClipper clip (this);
875 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
876 }
877
878 bool wxTopLevelWindowMac::IsMaximized() const
879 {
880 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
881 }
882
883 void wxTopLevelWindowMac::Iconize(bool iconize)
884 {
885 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
886 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
887 }
888
889 bool wxTopLevelWindowMac::IsIconized() const
890 {
891 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
892 }
893
894 void wxTopLevelWindowMac::Restore()
895 {
896 // not available on mac
897 }
898
899 // ----------------------------------------------------------------------------
900 // wxTopLevelWindowMac misc
901 // ----------------------------------------------------------------------------
902
903 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
904 {
905 return wxPoint(0,0) ;
906 }
907
908 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
909 {
910 // this sets m_icon
911 wxTopLevelWindowBase::SetIcon(icon);
912 }
913
914 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
915 {
916 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
917
918 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
919 {
920 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
921 }
922 }
923
924 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
925 {
926 if ( m_macEventHandler != NULL )
927 {
928 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
929 }
930 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
931 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
932 }
933
934 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
935 const wxPoint& pos,
936 const wxSize& size,
937 long style,
938 const wxString& name )
939 {
940 OSStatus err = noErr ;
941 SetName(name);
942 m_windowStyle = style;
943 m_isShown = FALSE;
944
945 // create frame.
946
947 Rect theBoundsRect;
948
949 int x = (int)pos.x;
950 int y = (int)pos.y;
951 if ( y < 50 )
952 y = 50 ;
953 if ( x < 20 )
954 x = 20 ;
955
956 int w = WidthDefault(size.x);
957 int h = HeightDefault(size.y);
958
959 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
960
961 // translate the window attributes in the appropriate window class and attributes
962
963 WindowClass wclass = 0;
964 WindowAttributes attr = kWindowNoAttributes ;
965
966 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
967 {
968 if (
969 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
970 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
971 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
972 )
973 {
974 wclass = kFloatingWindowClass ;
975 if ( HasFlag(wxTINY_CAPTION_VERT) )
976 {
977 attr |= kWindowSideTitlebarAttribute ;
978 }
979 }
980 else
981 {
982 wclass = kPlainWindowClass ;
983 }
984 }
985 else if ( HasFlag( wxCAPTION ) )
986 {
987 wclass = kDocumentWindowClass ;
988 }
989 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
990 else if ( HasFlag( wxFRAME_DRAWER ) )
991 {
992 wclass = kDrawerWindowClass;
993 // Should this be left for compositing check below?
994 // CreateNewWindow will fail without it, should wxDrawerWindow turn
995 // on compositing before calling MacCreateRealWindow?
996 attr |= kWindowCompositingAttribute;// | kWindowStandardHandlerAttribute;
997 }
998 #endif //10.2 and up
999 else
1000 {
1001 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1002 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1003 {
1004 wclass = kDocumentWindowClass ;
1005 }
1006 else
1007 {
1008 wclass = kPlainWindowClass ;
1009 }
1010 }
1011
1012 if ( HasFlag( wxMINIMIZE_BOX ) )
1013 {
1014 attr |= kWindowCollapseBoxAttribute ;
1015 }
1016 if ( HasFlag( wxMAXIMIZE_BOX ) )
1017 {
1018 attr |= kWindowFullZoomAttribute ;
1019 }
1020 if ( HasFlag( wxRESIZE_BORDER ) )
1021 {
1022 attr |= kWindowResizableAttribute ;
1023 }
1024 if ( HasFlag( wxCLOSE_BOX) )
1025 {
1026 attr |= kWindowCloseBoxAttribute ;
1027 }
1028
1029 if (UMAGetSystemVersion() >= 0x1000)
1030 {
1031 // turn on live resizing (OS X only)
1032 attr |= kWindowLiveResizeAttribute;
1033 }
1034
1035 if (HasFlag(wxSTAY_ON_TOP))
1036 wclass = kUtilityWindowClass;
1037
1038 #if TARGET_API_MAC_OSX
1039 attr |= kWindowCompositingAttribute;
1040 #endif
1041
1042 if ( HasFlag(wxFRAME_SHAPED) )
1043 {
1044 WindowDefSpec customWindowDefSpec;
1045 customWindowDefSpec.defType = kWindowDefProcPtr;
1046 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1047
1048 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1049 attr, &theBoundsRect,
1050 (WindowRef*) &m_macWindow);
1051 }
1052 else
1053 {
1054 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1055 }
1056
1057 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1058
1059 // the create commands are only for content rect, so we have to set the size again as
1060 // structure bounds
1061 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1062
1063 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1064 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1065 m_peer = new wxMacControl() ;
1066 #if TARGET_API_MAC_OSX
1067 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1068 // the content view, so we have to retrieve it explicitely
1069 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1070 m_peer->GetControlRefAddr() ) ;
1071 if ( !m_peer->Ok() )
1072 {
1073 // compatibility mode fallback
1074 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1075 }
1076 #else
1077 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1078 #endif
1079 // the root control level handleer
1080 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1081
1082 // the frame window event handler
1083 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1084 MacInstallTopLevelWindowEventHandler() ;
1085
1086 m_macFocus = NULL ;
1087
1088 if ( HasFlag(wxFRAME_SHAPED) )
1089 {
1090 // default shape matches the window size
1091 wxRegion rgn(0, 0, w, h);
1092 SetShape(rgn);
1093 }
1094
1095 wxWindowCreateEvent event(this);
1096 GetEventHandler()->ProcessEvent(event);
1097 }
1098
1099 void wxTopLevelWindowMac::ClearBackground()
1100 {
1101 wxWindow::ClearBackground() ;
1102 }
1103
1104 // Raise the window to the top of the Z order
1105 void wxTopLevelWindowMac::Raise()
1106 {
1107 ::SelectWindow( (WindowRef)m_macWindow ) ;
1108 }
1109
1110 // Lower the window to the bottom of the Z order
1111 void wxTopLevelWindowMac::Lower()
1112 {
1113 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1114 }
1115
1116
1117 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1118 {
1119 if(s_macDeactivateWindow)
1120 {
1121 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
1122 s_macDeactivateWindow->MacActivate(timestamp, false);
1123 }
1124 }
1125
1126 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1127 {
1128 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1129
1130 if(s_macDeactivateWindow==this)
1131 s_macDeactivateWindow=NULL;
1132 MacDelayedDeactivation(timestamp);
1133 MacPropagateHiliteChanged() ;
1134 }
1135
1136 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1137 {
1138 wxWindow::SetTitle( title ) ;
1139 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1140 }
1141
1142 bool wxTopLevelWindowMac::Show(bool show)
1143 {
1144 if ( !wxTopLevelWindowBase::Show(show) )
1145 return FALSE;
1146
1147 if (show)
1148 {
1149 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1150 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1151 {
1152 ::ShowWindow( (WindowRef)m_macWindow );
1153 }
1154 else
1155 #endif
1156 {
1157 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
1158 }
1159 ::SelectWindow( (WindowRef)m_macWindow ) ;
1160 // as apps expect a size event to occur at this moment
1161 wxSizeEvent event( GetSize() , m_windowId);
1162 event.SetEventObject(this);
1163 GetEventHandler()->ProcessEvent(event);
1164 }
1165 else
1166 {
1167 #if wxUSE_SYSTEM_OPTIONS
1168 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1169 {
1170 ::HideWindow((WindowRef) m_macWindow );
1171 }
1172 else
1173 #endif
1174 {
1175 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
1176 }
1177 }
1178
1179 MacPropagateVisibilityChanged() ;
1180
1181 return TRUE ;
1182 }
1183
1184 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1185 {
1186 if ( show )
1187 {
1188 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1189 delete data ;
1190 data = new FullScreenData() ;
1191
1192 m_macFullScreenData = data ;
1193 data->m_position = GetPosition() ;
1194 data->m_size = GetSize() ;
1195
1196 if ( style & wxFULLSCREEN_NOMENUBAR )
1197 {
1198 HideMenuBar() ;
1199 }
1200 int left , top , right , bottom ;
1201 wxRect client = wxGetClientDisplayRect() ;
1202
1203 int x, y, w, h ;
1204
1205 x = client.x ;
1206 y = client.y ;
1207 w = client.width ;
1208 h = client.height ;
1209
1210 MacGetContentAreaInset( left , top , right , bottom ) ;
1211
1212 if ( style & wxFULLSCREEN_NOCAPTION )
1213 {
1214 y -= top ;
1215 h += top ;
1216 }
1217 if ( style & wxFULLSCREEN_NOBORDER )
1218 {
1219 x -= left ;
1220 w += left + right ;
1221 h += bottom ;
1222 }
1223 if ( style & wxFULLSCREEN_NOTOOLBAR )
1224 {
1225 // TODO
1226 }
1227 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1228 {
1229 // TODO
1230 }
1231 SetSize( x , y , w, h ) ;
1232 }
1233 else
1234 {
1235 ShowMenuBar() ;
1236 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1237 SetPosition( data->m_position ) ;
1238 SetSize( data->m_size ) ;
1239 delete data ;
1240 m_macFullScreenData = NULL ;
1241 }
1242 return FALSE;
1243 }
1244
1245 bool wxTopLevelWindowMac::IsFullScreen() const
1246 {
1247 return m_macFullScreenData != NULL ;
1248 }
1249
1250 // we are still using coordinates of the content view, todo switch to structure bounds
1251
1252 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1253 {
1254 Rect content ;
1255 Rect structure ;
1256 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1257 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1258
1259 left = content.left - structure.left ;
1260 top = content.top - structure.top ;
1261 right = structure.right - content.right ;
1262 bottom = structure.bottom - content.bottom ;
1263 }
1264
1265 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1266 {
1267 Rect bounds = { y , x , y + height , x + width } ;
1268 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1269 }
1270
1271 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1272 {
1273 Rect bounds ;
1274 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1275 if(x) *x = bounds.left ;
1276 if(y) *y = bounds.top ;
1277 }
1278 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1279 {
1280 Rect bounds ;
1281 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1282 if(width) *width = bounds.right - bounds.left ;
1283 if(height) *height = bounds.bottom - bounds.top ;
1284 }
1285
1286 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1287 {
1288 Rect bounds ;
1289 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1290 if(width) *width = bounds.right - bounds.left ;
1291 if(height) *height = bounds.bottom - bounds.top ;
1292 }
1293
1294 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1295 {
1296 #if TARGET_API_MAC_OSX
1297 UInt32 attr = 0 ;
1298 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1299 wxASSERT_MSG( attr & kWindowCompositingAttribute ,
1300 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1301
1302 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1303 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1304 #endif
1305 }
1306
1307 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1308 {
1309 #if TARGET_API_MAC_OSX
1310 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1311 #else
1312 return false ;
1313 #endif
1314 }
1315
1316 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1317 {
1318 ChangeWindowAttributes ( (WindowRef) m_macWindow , attributesToSet, attributesToClear ) ;
1319 }
1320
1321 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1322 {
1323 UInt32 attr = 0 ;
1324 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1325 return attr ;
1326 }
1327
1328 // ---------------------------------------------------------------------------
1329 // Shape implementation
1330 // ---------------------------------------------------------------------------
1331
1332
1333 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1334 {
1335 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1336 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1337
1338 // The empty region signifies that the shape should be removed from the
1339 // window.
1340 if ( region.IsEmpty() )
1341 {
1342 wxSize sz = GetClientSize();
1343 wxRegion rgn(0, 0, sz.x, sz.y);
1344 return SetShape(rgn);
1345 }
1346
1347 // Make a copy of the region
1348 RgnHandle shapeRegion = NewRgn();
1349 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1350
1351 // Dispose of any shape region we may already have
1352 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1353 if ( oldRgn )
1354 DisposeRgn(oldRgn);
1355
1356 // Save the region so we can use it later
1357 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1358
1359 // Tell the window manager that the window has changed shape
1360 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1361 return TRUE;
1362 }
1363
1364 // ---------------------------------------------------------------------------
1365 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1366 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1367 // ---------------------------------------------------------------------------
1368
1369 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1370 {
1371 GetWindowPortBounds(window, inRect);
1372 Point pt = {inRect->left, inRect->top};
1373 QDLocalToGlobalPoint( GetWindowPort(window) , &pt ) ;
1374 inRect->top = pt.v;
1375 inRect->left = pt.h;
1376 inRect->bottom += pt.v;
1377 inRect->right += pt.h;
1378 }
1379
1380
1381 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1382 {
1383 /*------------------------------------------------------
1384 Define which options your custom window supports.
1385 --------------------------------------------------------*/
1386 //just enable everything for our demo
1387 *(OptionBits*)param=//kWindowCanGrow|
1388 //kWindowCanZoom|
1389 //kWindowCanCollapse|
1390 //kWindowCanGetWindowRegion|
1391 //kWindowHasTitleBar|
1392 //kWindowSupportsDragHilite|
1393 kWindowCanDrawInCurrentPort|
1394 //kWindowCanMeasureTitle|
1395 kWindowWantsDisposeAtProcessDeath|
1396 kWindowSupportsGetGrowImageRegion|
1397 kWindowDefSupportsColorGrafPort;
1398 return 1;
1399 }
1400
1401 // The content region is left as a rectangle matching the window size, this is
1402 // so the origin in the paint event, and etc. still matches what the
1403 // programmer expects.
1404 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1405 {
1406 SetEmptyRgn(rgn);
1407 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1408 if (win)
1409 {
1410 Rect r ;
1411 wxShapedMacWindowGetPos(window, &r ) ;
1412 RectRgn( rgn , &r ) ;
1413 }
1414 }
1415
1416 // The structure region is set to the shape given to the SetShape method.
1417 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1418 {
1419 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1420
1421 SetEmptyRgn(rgn);
1422 if (cachedRegion)
1423 {
1424 Rect windowRect;
1425 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1426 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1427 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1428 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1429 }
1430 }
1431
1432
1433
1434 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1435 {
1436 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1437
1438 switch(rgnRec->regionCode)
1439 {
1440 case kWindowStructureRgn:
1441 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1442 break;
1443 case kWindowContentRgn:
1444 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1445 break;
1446 default:
1447 SetEmptyRgn(rgnRec->winRgn);
1448 } //switch
1449
1450 return noErr;
1451 }
1452
1453
1454 static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1455 {
1456 /*------------------------------------------------------
1457 Determine the region of the window which was hit
1458 --------------------------------------------------------*/
1459 Point hitPoint;
1460 static RgnHandle tempRgn=nil;
1461
1462 if(!tempRgn)
1463 tempRgn=NewRgn();
1464
1465 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1466
1467 //Mac OS 8.5 or later
1468 wxShapedMacWindowStructureRegion(window, tempRgn);
1469 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1470 return wInContent;
1471
1472 return wNoHit;//no significant area was hit.
1473 }
1474
1475
1476 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1477 {
1478 switch(message)
1479 {
1480 case kWindowMsgHitTest:
1481 return wxShapedMacWindowHitTest(window,param);
1482
1483 case kWindowMsgGetFeatures:
1484 return wxShapedMacWindowGetFeatures(window,param);
1485
1486 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1487 case kWindowMsgGetRegion:
1488 return wxShapedMacWindowGetRegion(window,param);
1489 }
1490
1491 return 0;
1492 }
1493
1494 // ---------------------------------------------------------------------------
1495