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