]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
porting forward scrolling fix
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/toplevel.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/frame.h"
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/settings.h"
36 #include "wx/strconv.h"
37 #include "wx/control.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/mac/uma.h"
41 #include "wx/mac/aga.h"
42 #include "wx/tooltip.h"
43 #include "wx/dnd.h"
44
45 #if wxUSE_SYSTEM_OPTIONS
46 #include "wx/sysopt.h"
47 #endif
48
49 #ifndef __DARWIN__
50 #include <ToolUtils.h>
51 #endif
52
53 // for targeting OSX
54 #include "wx/mac/private.h"
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
61 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
62
63 // trace mask for activation tracing messages
64 static const wxChar *TRACE_ACTIVATE = _T("activation");
65
66 // ----------------------------------------------------------------------------
67 // globals
68 // ----------------------------------------------------------------------------
69
70 // list of all frames and modeless dialogs
71 wxWindowList wxModelessWindows;
72
73 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
74
75 // ============================================================================
76 // wxTopLevelWindowMac implementation
77 // ============================================================================
78
79 BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
80 END_EVENT_TABLE()
81
82 // ---------------------------------------------------------------------------
83 // Carbon Events
84 // ---------------------------------------------------------------------------
85
86 static const EventTypeSpec eventList[] =
87 {
88 // TODO: remove control related event like key and mouse (except for WindowLeave events)
89
90 { kEventClassKeyboard, kEventRawKeyDown } ,
91 { kEventClassKeyboard, kEventRawKeyRepeat } ,
92 { kEventClassKeyboard, kEventRawKeyUp } ,
93 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
94
95 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
96 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
97
98 { kEventClassWindow , kEventWindowShown } ,
99 { kEventClassWindow , kEventWindowActivated } ,
100 { kEventClassWindow , kEventWindowDeactivated } ,
101 { kEventClassWindow , kEventWindowBoundsChanging } ,
102 { kEventClassWindow , kEventWindowBoundsChanged } ,
103 { kEventClassWindow , kEventWindowClose } ,
104 { kEventClassWindow , kEventWindowGetRegion } ,
105
106 // we have to catch these events on the toplevel window level,
107 // as controls don't get the raw mouse events anymore
108
109 { kEventClassMouse , kEventMouseDown } ,
110 { kEventClassMouse , kEventMouseUp } ,
111 { kEventClassMouse , kEventMouseWheelMoved } ,
112 { kEventClassMouse , kEventMouseMoved } ,
113 { kEventClassMouse , kEventMouseDragged } ,
114 } ;
115
116 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
117 {
118 OSStatus result = eventNotHandledErr ;
119 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
120 // FindFocus does not return the actual focus window, but the enclosing window
121 wxWindow* focus = wxWindow::DoFindFocus();
122 if ( focus == NULL )
123 focus = (wxTopLevelWindowMac*) data ;
124
125 unsigned char charCode ;
126 wxChar uniChar[2] ;
127 uniChar[0] = 0;
128 uniChar[1] = 0;
129
130 UInt32 keyCode ;
131 UInt32 modifiers ;
132 Point point ;
133 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
134
135 #if wxUSE_UNICODE
136 ByteCount dataSize = 0 ;
137 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
138 {
139 UniChar buf[2] ;
140 int numChars = dataSize / sizeof( UniChar) + 1;
141
142 UniChar* charBuf = buf ;
143
144 if ( numChars * 2 > 4 )
145 charBuf = new UniChar[ numChars ] ;
146 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
147 charBuf[ numChars - 1 ] = 0;
148
149 #if SIZEOF_WCHAR_T == 2
150 uniChar = charBuf[0] ;
151 #else
152 wxMBConvUTF16 converter ;
153 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
154 #endif
155
156 if ( numChars * 2 > 4 )
157 delete[] charBuf ;
158 }
159 #endif
160
161 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
162 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
163 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
164 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
165
166 UInt32 message = (keyCode << 8) + charCode;
167 switch ( GetEventKind( event ) )
168 {
169 case kEventRawKeyRepeat :
170 case kEventRawKeyDown :
171 {
172 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
173 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
174 wxTheApp->MacSetCurrentEvent( event , handler ) ;
175 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
176 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
177 {
178 result = noErr ;
179 }
180 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
181 }
182 break ;
183
184 case kEventRawKeyUp :
185 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
186 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
187 {
188 result = noErr ;
189 }
190 break ;
191
192 case kEventRawKeyModifiersChanged :
193 {
194 wxKeyEvent event(wxEVT_KEY_DOWN);
195
196 event.m_shiftDown = modifiers & shiftKey;
197 event.m_controlDown = modifiers & controlKey;
198 event.m_altDown = modifiers & optionKey;
199 event.m_metaDown = modifiers & cmdKey;
200 event.m_x = point.h;
201 event.m_y = point.v;
202
203 #if wxUSE_UNICODE
204 event.m_uniChar = uniChar[0] ;
205 #endif
206
207 event.SetTimestamp(when);
208 event.SetEventObject(focus);
209
210 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
211 {
212 event.m_keyCode = WXK_CONTROL ;
213 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
214 focus->GetEventHandler()->ProcessEvent( event ) ;
215 }
216 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
217 {
218 event.m_keyCode = WXK_SHIFT ;
219 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
220 focus->GetEventHandler()->ProcessEvent( event ) ;
221 }
222 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
223 {
224 event.m_keyCode = WXK_ALT ;
225 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
226 focus->GetEventHandler()->ProcessEvent( event ) ;
227 }
228 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
229 {
230 event.m_keyCode = WXK_COMMAND ;
231 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
232 focus->GetEventHandler()->ProcessEvent( event ) ;
233 }
234
235 wxApp::s_lastModifiers = modifiers ;
236 }
237 break ;
238
239 default:
240 break;
241 }
242
243 return result ;
244 }
245
246 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
247 // for windows that we didn't create (like eg Scrollbars in a databrowser), or for controls where we did not handle the
248 // mouse down at all
249 //
250 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
251
252 wxWindow* g_MacLastWindow = NULL ;
253
254 EventMouseButton g_lastButton = 0 ;
255 bool g_lastButtonWasFakeRight = false ;
256
257 void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
258 {
259 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
260 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
261
262 // these parameters are not given for all events
263 EventMouseButton button = 0 ;
264 UInt32 clickCount = 0 ;
265 UInt32 mouseChord = 0;
266
267 cEvent.GetParameter<EventMouseButton>( kEventParamMouseButton, typeMouseButton , &button ) ;
268 cEvent.GetParameter<UInt32>( kEventParamClickCount, typeUInt32 , &clickCount ) ;
269 // the chord is the state of the buttons pressed currently
270 cEvent.GetParameter<UInt32>( kEventParamMouseChord, typeUInt32 , &mouseChord ) ;
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.m_clickCount = clickCount;
279 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
280
281 // a control click is interpreted as a right click
282 bool thisButtonIsFakeRight = false ;
283 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
284 {
285 button = kEventMouseButtonSecondary ;
286 thisButtonIsFakeRight = true ;
287 }
288
289 // otherwise we report double clicks by connecting a left click with a ctrl-left click
290 if ( clickCount > 1 && button != g_lastButton )
291 clickCount = 1 ;
292
293 // we must make sure that our synthetic 'right' button corresponds in
294 // mouse down, moved and mouse up, and does not deliver a right down and left up
295
296 if ( cEvent.GetKind() == kEventMouseDown )
297 {
298 g_lastButton = button ;
299 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
300 }
301
302 if ( button == 0 )
303 {
304 g_lastButton = 0 ;
305 g_lastButtonWasFakeRight = false ;
306 }
307 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
308 button = g_lastButton ;
309
310 // Adjust the chord mask to remove the primary button and add the
311 // secondary button. It is possible that the secondary button is
312 // already pressed, e.g. on a mouse connected to a laptop, but this
313 // possibility is ignored here:
314 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
315 mouseChord = ((mouseChord & ~1U) | 2U);
316
317 if(mouseChord & 1U)
318 wxevent.m_leftDown = true ;
319 if(mouseChord & 2U)
320 wxevent.m_rightDown = true ;
321 if(mouseChord & 4U)
322 wxevent.m_middleDown = true ;
323
324 // translate into wx types
325 switch ( cEvent.GetKind() )
326 {
327 case kEventMouseDown :
328 switch ( button )
329 {
330 case kEventMouseButtonPrimary :
331 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
332 break ;
333
334 case kEventMouseButtonSecondary :
335 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
336 break ;
337
338 case kEventMouseButtonTertiary :
339 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
340 break ;
341
342 default:
343 break ;
344 }
345 break ;
346
347 case kEventMouseUp :
348 switch ( button )
349 {
350 case kEventMouseButtonPrimary :
351 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
352 break ;
353
354 case kEventMouseButtonSecondary :
355 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
356 break ;
357
358 case kEventMouseButtonTertiary :
359 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
360 break ;
361
362 default:
363 break ;
364 }
365 break ;
366
367 case kEventMouseWheelMoved :
368 {
369 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
370
371 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
372 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
373
374 wxevent.m_wheelRotation = delta;
375 wxevent.m_wheelDelta = 1;
376 wxevent.m_linesPerAction = 1;
377 if ( axis == kEventMouseWheelAxisX )
378 wxevent.m_wheelAxis = 1;
379 }
380 break ;
381
382 case kEventMouseEntered :
383 case kEventMouseExited :
384 case kEventMouseDragged :
385 case kEventMouseMoved :
386 wxevent.SetEventType( wxEVT_MOTION ) ;
387 break;
388 default :
389 break ;
390 }
391 }
392
393 #define NEW_CAPTURE_HANDLING 1
394
395 pascal OSStatus
396 wxMacTopLevelMouseEventHandler(EventHandlerCallRef WXUNUSED(handler),
397 EventRef event,
398 void *data)
399 {
400 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
401
402 OSStatus result = eventNotHandledErr ;
403
404 wxMacCarbonEvent cEvent( event ) ;
405
406 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
407 Point windowMouseLocation = screenMouseLocation ;
408
409 WindowRef window = NULL;
410 short windowPart = ::FindWindow(screenMouseLocation, &window);
411
412 wxWindow* currentMouseWindow = NULL ;
413 ControlRef control = NULL ;
414
415 #if NEW_CAPTURE_HANDLING
416 if ( wxApp::s_captureWindow )
417 {
418 window = (WindowRef) wxApp::s_captureWindow->MacGetTopLevelWindowRef() ;
419 windowPart = inContent ;
420 }
421 #endif
422
423 if ( window )
424 {
425 wxMacGlobalToLocal( window, &windowMouseLocation ) ;
426
427 if ( wxApp::s_captureWindow
428 #if !NEW_CAPTURE_HANDLING
429 && wxApp::s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent
430 #endif
431 )
432 {
433 currentMouseWindow = wxApp::s_captureWindow ;
434 }
435 else if ( (IsWindowActive(window) && windowPart == inContent) )
436 {
437 ControlPartCode part ;
438 control = FindControlUnderMouse( windowMouseLocation , window , &part ) ;
439 // if there is no control below the mouse position, send the event to the toplevel window itself
440 if ( control == 0 )
441 {
442 currentMouseWindow = (wxWindow*) data ;
443 }
444 else
445 {
446 currentMouseWindow = (wxWindow*) wxFindControlFromMacControl( control ) ;
447 #ifndef __WXUNIVERSAL__
448 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
449 {
450 #if wxUSE_TOOLBAR
451 // for wxToolBar to function we have to send certaint events to it
452 // instead of its children (wxToolBarTools)
453 ControlRef parent ;
454 GetSuperControl(control, &parent );
455 wxWindow *wxParent = (wxWindow*) wxFindControlFromMacControl( parent ) ;
456 if ( wxParent && wxParent->IsKindOf( CLASSINFO( wxToolBar ) ) )
457 currentMouseWindow = wxParent ;
458 #endif
459 }
460 #endif
461 }
462
463 // disabled windows must not get any input messages
464 if ( currentMouseWindow && !currentMouseWindow->MacIsReallyEnabled() )
465 currentMouseWindow = NULL;
466 }
467 }
468
469 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
470 SetupMouseEvent( wxevent , cEvent ) ;
471
472 // handle all enter / leave events
473
474 if ( currentMouseWindow != g_MacLastWindow )
475 {
476 if ( g_MacLastWindow )
477 {
478 wxMouseEvent eventleave(wxevent);
479 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
480 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
481 eventleave.SetEventObject( g_MacLastWindow ) ;
482 wxevent.SetId( g_MacLastWindow->GetId() ) ;
483
484 #if wxUSE_TOOLTIPS
485 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
486 #endif
487
488 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
489 }
490
491 if ( currentMouseWindow )
492 {
493 wxMouseEvent evententer(wxevent);
494 evententer.SetEventType( wxEVT_ENTER_WINDOW );
495 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
496 evententer.SetEventObject( currentMouseWindow ) ;
497 wxevent.SetId( currentMouseWindow->GetId() ) ;
498
499 #if wxUSE_TOOLTIPS
500 wxToolTip::RelayEvent( currentMouseWindow , evententer );
501 #endif
502
503 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
504 }
505
506 g_MacLastWindow = currentMouseWindow ;
507 }
508
509 if ( windowPart == inMenuBar )
510 {
511 // special case menu bar, as we are having a low-level runloop we must do it ourselves
512 if ( cEvent.GetKind() == kEventMouseDown )
513 {
514 ::MenuSelect( screenMouseLocation ) ;
515 ::HiliteMenu(0);
516 result = noErr ;
517 }
518 }
519 else if ( currentMouseWindow )
520 {
521 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
522
523 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
524
525 wxevent.SetEventObject( currentMouseWindow ) ;
526 wxevent.SetId( currentMouseWindow->GetId() ) ;
527
528 // make tooltips current
529
530 #if wxUSE_TOOLTIPS
531 if ( wxevent.GetEventType() == wxEVT_MOTION )
532 wxToolTip::RelayEvent( currentMouseWindow , wxevent );
533 #endif
534
535 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
536 {
537 if ((currentMouseWindowParent != NULL) &&
538 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
539 currentMouseWindow = NULL;
540
541 result = noErr;
542 }
543 else
544 {
545 // if the user code did _not_ handle the event, then perform the
546 // default processing
547 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
548 {
549 // ... that is set focus to this window
550 if (currentMouseWindow->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow)
551 currentMouseWindow->SetFocus();
552 }
553
554 // if built-in find control is finding the wrong control (ie static box instead of overlaid
555 // button, we cannot let the standard handler do its job, but must handle manually
556
557 if ( cEvent.GetKind() == kEventMouseDown )
558 {
559 if ( currentMouseWindow->MacIsReallyEnabled() )
560 {
561 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
562 Point clickLocation = windowMouseLocation ;
563
564 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
565
566 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
567 modifiers , (ControlActionUPP ) -1 ) ;
568
569 if ((currentMouseWindowParent != NULL) &&
570 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
571 {
572 currentMouseWindow = NULL;
573 }
574 }
575
576 result = noErr ;
577 }
578 }
579
580 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
581 {
582 wxApp::s_captureWindow = NULL ;
583 // update cursor ?
584 }
585
586 // update cursor
587
588 wxWindow* cursorTarget = currentMouseWindow ;
589 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
590
591 while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
592 {
593 cursorTarget = cursorTarget->GetParent() ;
594 if ( cursorTarget )
595 cursorPoint += cursorTarget->GetPosition();
596 }
597
598 }
599 else // currentMouseWindow == NULL
600 {
601 // don't mess with controls we don't know about
602 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
603 // so we try sending them the correct control directly
604 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
605 {
606 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
607 Point clickLocation = windowMouseLocation ;
608
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
616 HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
617 result = noErr ;
618 }
619 }
620
621 return result ;
622 }
623
624 static pascal OSStatus
625 wxMacTopLevelWindowEventHandler(EventHandlerCallRef WXUNUSED(handler),
626 EventRef event,
627 void *data)
628 {
629 OSStatus result = eventNotHandledErr ;
630
631 wxMacCarbonEvent cEvent( event ) ;
632
633 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
634 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
635
636 switch ( GetEventKind( event ) )
637 {
638 case kEventWindowActivated :
639 {
640 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
641 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
642 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
643 wxevent.SetEventObject(toplevelWindow);
644 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
645 // we still sending an eventNotHandledErr in order to allow for default processing
646 }
647 break ;
648
649 case kEventWindowDeactivated :
650 {
651 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
652 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
653 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
654 wxevent.SetEventObject(toplevelWindow);
655 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
656 // we still sending an eventNotHandledErr in order to allow for default processing
657 }
658 break ;
659
660 case kEventWindowShown :
661 toplevelWindow->Refresh() ;
662 result = noErr ;
663 break ;
664
665 case kEventWindowClose :
666 toplevelWindow->Close() ;
667 result = noErr ;
668 break ;
669
670 case kEventWindowBoundsChanged :
671 {
672 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
673 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
674 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
675 if ( attributes & kWindowBoundsChangeSizeChanged )
676 {
677 #ifndef __WXUNIVERSAL__
678 // according to the other ports we handle this within the OS level
679 // resize event, not within a wxSizeEvent
680 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
681 if ( frame )
682 {
683 frame->PositionBars();
684 }
685 #endif
686 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
687 event.SetEventObject( toplevelWindow ) ;
688
689 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
690 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
691 }
692
693 if ( attributes & kWindowBoundsChangeOriginChanged )
694 {
695 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
696 event.SetEventObject( toplevelWindow ) ;
697 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
698 }
699
700 result = noErr ;
701 }
702 break ;
703
704 case kEventWindowBoundsChanging :
705 {
706 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
707 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
708
709 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
710 {
711 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
712 int left , top , right , bottom ;
713 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
714
715 wxRect r(
716 newRect.left - left,
717 newRect.top - top,
718 newRect.right - newRect.left + left + right,
719 newRect.bottom - newRect.top + top + bottom ) ;
720
721 // this is a EVT_SIZING not a EVT_SIZE type !
722 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
723 wxevent.SetEventObject( toplevelWindow ) ;
724 wxRect adjustR = r ;
725 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
726 adjustR = wxevent.GetRect() ;
727
728 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
729 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
730 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
731 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
732 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
733 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
734 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
735 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
736 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
737 if ( !EqualRect( &newRect , &adjustedRect ) )
738 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
739 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
740 }
741
742 result = noErr ;
743 }
744 break ;
745
746 case kEventWindowGetRegion :
747 {
748 if ( toplevelWindow->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
749 {
750 WindowRegionCode windowRegionCode ;
751
752 // Fetch the region code that is being queried
753 GetEventParameter( event,
754 kEventParamWindowRegionCode,
755 typeWindowRegionCode, NULL,
756 sizeof windowRegionCode, NULL,
757 &windowRegionCode ) ;
758
759 // If it is the opaque region code then set the
760 // region to empty and return noErr to stop event
761 // propagation
762 if ( windowRegionCode == kWindowOpaqueRgn ) {
763 RgnHandle region;
764 GetEventParameter( event,
765 kEventParamRgnHandle,
766 typeQDRgnHandle, NULL,
767 sizeof region, NULL,
768 &region) ;
769 SetEmptyRgn(region) ;
770 result = noErr ;
771 }
772 }
773 }
774 break ;
775
776 default :
777 break ;
778 }
779
780 return result ;
781 }
782
783 // mix this in from window.cpp
784 pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
785
786 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
787 {
788 OSStatus result = eventNotHandledErr ;
789
790 switch ( GetEventClass( event ) )
791 {
792 case kEventClassTextInput :
793 result = wxMacUnicodeTextEventHandler( handler, event , data ) ;
794 break ;
795
796 case kEventClassKeyboard :
797 result = KeyboardEventHandler( handler, event , data ) ;
798 break ;
799
800 case kEventClassWindow :
801 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
802 break ;
803
804 case kEventClassMouse :
805 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
806 break ;
807
808 default :
809 break ;
810 }
811
812 return result ;
813 }
814
815 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
816
817 // ---------------------------------------------------------------------------
818 // wxWindowMac utility functions
819 // ---------------------------------------------------------------------------
820
821 // Find an item given the Macintosh Window Reference
822
823 WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
824
825 static MacWindowMap wxWinMacWindowList;
826
827 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
828 {
829 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
830
831 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
832 }
833
834 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
835 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
836 {
837 // adding NULL WindowRef is (first) surely a result of an error and
838 // nothing else :-)
839 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
840
841 wxWinMacWindowList[inWindowRef] = win;
842 }
843
844 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
845 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
846 {
847 MacWindowMap::iterator it;
848 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
849 {
850 if ( it->second == win )
851 {
852 wxWinMacWindowList.erase(it);
853 break;
854 }
855 }
856 }
857
858 // ----------------------------------------------------------------------------
859 // wxTopLevelWindowMac creation
860 // ----------------------------------------------------------------------------
861
862 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
863
864 typedef struct
865 {
866 wxPoint m_position ;
867 wxSize m_size ;
868 bool m_wasResizable ;
869 } FullScreenData ;
870
871 void wxTopLevelWindowMac::Init()
872 {
873 m_iconized =
874 m_maximizeOnShow = false;
875 m_macWindow = NULL ;
876
877 m_macEventHandler = NULL ;
878 m_macFullScreenData = NULL ;
879 }
880
881 wxMacDeferredWindowDeleter::wxMacDeferredWindowDeleter( WindowRef windowRef )
882 {
883 m_macWindow = windowRef ;
884 }
885
886 wxMacDeferredWindowDeleter::~wxMacDeferredWindowDeleter()
887 {
888 DisposeWindow( (WindowRef) m_macWindow ) ;
889 }
890
891 bool wxTopLevelWindowMac::Create(wxWindow *parent,
892 wxWindowID id,
893 const wxString& title,
894 const wxPoint& pos,
895 const wxSize& size,
896 long style,
897 const wxString& name)
898 {
899 // init our fields
900 Init();
901
902 m_windowStyle = style;
903
904 SetName( name );
905
906 m_windowId = id == -1 ? NewControlId() : id;
907 wxWindow::SetLabel( title ) ;
908
909 DoMacCreateRealWindow( parent, title, pos , size , style , name ) ;
910
911 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
912
913 if (GetExtraStyle() & wxFRAME_EX_METAL)
914 MacSetMetalAppearance(true);
915
916 wxTopLevelWindows.Append(this);
917
918 if ( parent )
919 parent->AddChild(this);
920
921 return true;
922 }
923
924 wxTopLevelWindowMac::~wxTopLevelWindowMac()
925 {
926 if ( m_macWindow )
927 {
928 #if wxUSE_TOOLTIPS
929 wxToolTip::NotifyWindowDelete(m_macWindow) ;
930 #endif
931 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
932 }
933
934 if ( m_macEventHandler )
935 {
936 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
937 m_macEventHandler = NULL ;
938 }
939
940 wxRemoveMacWindowAssociation( this ) ;
941
942 if ( wxModelessWindows.Find(this) )
943 wxModelessWindows.DeleteObject(this);
944
945 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
946 delete data ;
947 m_macFullScreenData = NULL ;
948
949 // avoid dangling refs
950 if ( s_macDeactivateWindow == this )
951 s_macDeactivateWindow = NULL;
952 }
953
954
955 // ----------------------------------------------------------------------------
956 // wxTopLevelWindowMac maximize/minimize
957 // ----------------------------------------------------------------------------
958
959 void wxTopLevelWindowMac::Maximize(bool maximize)
960 {
961 Point idealSize = { 0 , 0 } ;
962 if ( maximize )
963 {
964 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
965 HIRect bounds ;
966 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay,kHICoordSpace72DPIGlobal,
967 &bounds);
968 idealSize.h = bounds.size.width;
969 idealSize.v = bounds.size.height;
970 #else
971 Rect rect ;
972 GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
973 idealSize.h = rect.right - rect.left ;
974 idealSize.v = rect.bottom - rect.top ;
975 #endif
976 }
977 ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
978 }
979
980 bool wxTopLevelWindowMac::IsMaximized() const
981 {
982 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
983 }
984
985 void wxTopLevelWindowMac::Iconize(bool iconize)
986 {
987 if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
988 CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
989 }
990
991 bool wxTopLevelWindowMac::IsIconized() const
992 {
993 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
994 }
995
996 void wxTopLevelWindowMac::Restore()
997 {
998 if ( IsMaximized() )
999 Maximize(false);
1000 else if ( IsIconized() )
1001 Iconize(false);
1002 }
1003
1004 // ----------------------------------------------------------------------------
1005 // wxTopLevelWindowMac misc
1006 // ----------------------------------------------------------------------------
1007
1008 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1009 {
1010 return wxPoint(0, 0) ;
1011 }
1012
1013 bool wxTopLevelWindowMac::SetBackgroundColour(const wxColour& col )
1014 {
1015 if ( !wxTopLevelWindowBase::SetBackgroundColour(col) && m_hasBgCol )
1016 return false ;
1017
1018 if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground)) )
1019 {
1020 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDocumentWindowBackground, false ) ;
1021 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1022 }
1023 else if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
1024 {
1025 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDialogBackgroundActive, false ) ;
1026 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1027 }
1028 return true;
1029 }
1030
1031 void wxTopLevelWindowMacInstallTopLevelWindowEventHandler(WindowRef window, EventHandlerRef* handler, void *ref)
1032 {
1033 InstallWindowEventHandler(window, GetwxMacTopLevelEventHandlerUPP(),
1034 GetEventTypeCount(eventList), eventList, ref, handler );
1035 }
1036
1037 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
1038 {
1039 if ( m_macEventHandler != NULL )
1040 {
1041 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1042 }
1043 wxTopLevelWindowMacInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow),(EventHandlerRef *)&m_macEventHandler,this);
1044 }
1045
1046 void wxTopLevelWindowMac::MacCreateRealWindow(
1047 const wxString& title,
1048 const wxPoint& pos,
1049 const wxSize& size,
1050 long style,
1051 const wxString& name )
1052 {
1053 DoMacCreateRealWindow( NULL, title, pos, size, style, name );
1054 }
1055
1056 void wxTopLevelWindowMac::DoMacCreateRealWindow(
1057 wxWindow* parent,
1058 const wxString& title,
1059 const wxPoint& pos,
1060 const wxSize& size,
1061 long style,
1062 const wxString& name )
1063 {
1064 OSStatus err = noErr ;
1065 SetName(name);
1066 m_windowStyle = style;
1067 m_isShown = false;
1068
1069 // create frame.
1070 int x = (int)pos.x;
1071 int y = (int)pos.y;
1072
1073 Rect theBoundsRect;
1074 wxRect display = wxGetClientDisplayRect() ;
1075
1076 if ( x == wxDefaultPosition.x )
1077 x = display.x ;
1078
1079 if ( y == wxDefaultPosition.y )
1080 y = display.y ;
1081
1082 int w = WidthDefault(size.x);
1083 int h = HeightDefault(size.y);
1084
1085 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1086
1087 // translate the window attributes in the appropriate window class and attributes
1088 WindowClass wclass = 0;
1089 WindowAttributes attr = kWindowNoAttributes ;
1090 WindowGroupRef group = NULL ;
1091 bool activationScopeSet = false;
1092 WindowActivationScope activationScope = kWindowActivationScopeNone;
1093
1094 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
1095 {
1096 if (
1097 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1098 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1099 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
1100 )
1101 {
1102 if ( HasFlag( wxSTAY_ON_TOP ) )
1103 wclass = kUtilityWindowClass;
1104 else
1105 wclass = kFloatingWindowClass ;
1106
1107 if ( HasFlag(wxTINY_CAPTION_VERT) )
1108 attr |= kWindowSideTitlebarAttribute ;
1109 }
1110 else
1111 {
1112 wclass = kPlainWindowClass ;
1113 activationScopeSet = true;
1114 activationScope = kWindowActivationScopeNone;
1115 }
1116 }
1117 else if ( HasFlag( wxPOPUP_WINDOW ) )
1118 {
1119 // TEMPORARY HACK!
1120 // Until we've got a real wxPopupWindow class on wxMac make it a
1121 // little easier for wxFrame to be used to emulate it and workaround
1122 // the lack of wxPopupWindow.
1123 if ( HasFlag( wxBORDER_NONE ) )
1124 wclass = kHelpWindowClass ; // has no border
1125 else
1126 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1127 //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
1128 group = GetWindowGroupOfClass( // float above other windows
1129 kFloatingWindowClass) ;
1130 }
1131 else if ( HasFlag( wxCAPTION ) )
1132 {
1133 wclass = kDocumentWindowClass ;
1134 attr |= kWindowInWindowMenuAttribute ;
1135 }
1136 else if ( HasFlag( wxFRAME_DRAWER ) )
1137 {
1138 wclass = kDrawerWindowClass;
1139 }
1140 else
1141 {
1142 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1143 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1144 {
1145 wclass = kDocumentWindowClass ;
1146 }
1147 else if ( HasFlag( wxNO_BORDER ) )
1148 {
1149 wclass = kSimpleWindowClass ;
1150 }
1151 else
1152 {
1153 wclass = kPlainWindowClass ;
1154 }
1155 }
1156
1157 if ( wclass != kPlainWindowClass )
1158 {
1159 if ( HasFlag( wxMINIMIZE_BOX ) )
1160 attr |= kWindowCollapseBoxAttribute ;
1161
1162 if ( HasFlag( wxMAXIMIZE_BOX ) )
1163 attr |= kWindowFullZoomAttribute ;
1164
1165 if ( HasFlag( wxRESIZE_BORDER ) )
1166 attr |= kWindowResizableAttribute ;
1167
1168 if ( HasFlag( wxCLOSE_BOX) )
1169 attr |= kWindowCloseBoxAttribute ;
1170 }
1171 attr |= kWindowLiveResizeAttribute;
1172
1173 if ( HasFlag(wxSTAY_ON_TOP) )
1174 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1175
1176 if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
1177 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1178
1179 if ( group == NULL && parent != NULL )
1180 {
1181 WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
1182 if( parenttlw )
1183 group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
1184 }
1185
1186 attr |= kWindowCompositingAttribute;
1187 #if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1188 attr |= kWindowFrameworkScaledAttribute;
1189 #endif
1190
1191 if ( HasFlag(wxFRAME_SHAPED) )
1192 {
1193 WindowDefSpec customWindowDefSpec;
1194 customWindowDefSpec.defType = kWindowDefProcPtr;
1195 customWindowDefSpec.u.defProc =
1196 #ifdef __LP64__
1197 (WindowDefUPP) wxShapedMacWindowDef;
1198 #else
1199 NewWindowDefUPP(wxShapedMacWindowDef);
1200 #endif
1201 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1202 attr, &theBoundsRect,
1203 (WindowRef*) &m_macWindow);
1204 }
1205 else
1206 {
1207 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1208 }
1209
1210 if ( err == noErr && m_macWindow != NULL && group != NULL )
1211 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1212
1213 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1214
1215 // setup a separate group for each window, so that overlays can be handled easily
1216
1217 WindowGroupRef overlaygroup = NULL;
1218 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
1219 verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
1220 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
1221
1222 if ( activationScopeSet )
1223 {
1224 verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
1225 }
1226
1227 // the create commands are only for content rect,
1228 // so we have to set the size again as structure bounds
1229 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1230
1231 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1232 SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxMacCFStringHolder( title , m_font.GetEncoding() ) );
1233 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1234
1235 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1236 // the content view, so we have to retrieve it explicitly
1237 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1238 m_peer->GetControlRefAddr() ) ;
1239 if ( !m_peer->Ok() )
1240 {
1241 // compatibility mode fallback
1242 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1243 }
1244
1245 // the root control level handler
1246 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1247
1248 // Causes the inner part of the window not to be metal
1249 // if the style is used before window creation.
1250 #if 0 // TARGET_API_MAC_OSX
1251 if ( m_macUsesCompositing && m_macWindow != NULL )
1252 {
1253 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1254 MacSetMetalAppearance( true ) ;
1255 }
1256 #endif
1257
1258 if ( m_macWindow != NULL )
1259 {
1260 MacSetUnifiedAppearance( true ) ;
1261 }
1262
1263 HIViewRef growBoxRef = 0 ;
1264 err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
1265 if ( err == noErr && growBoxRef != 0 )
1266 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
1267
1268 // the frame window event handler
1269 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1270 MacInstallTopLevelWindowEventHandler() ;
1271
1272 DoSetWindowVariant( m_windowVariant ) ;
1273
1274 m_macFocus = NULL ;
1275
1276 if ( HasFlag(wxFRAME_SHAPED) )
1277 {
1278 // default shape matches the window size
1279 wxRegion rgn( 0, 0, w, h );
1280 SetShape( rgn );
1281 }
1282
1283 wxWindowCreateEvent event(this);
1284 GetEventHandler()->ProcessEvent(event);
1285 }
1286
1287 void wxTopLevelWindowMac::ClearBackground()
1288 {
1289 wxWindow::ClearBackground() ;
1290 }
1291
1292 // Raise the window to the top of the Z order
1293 void wxTopLevelWindowMac::Raise()
1294 {
1295 ::SelectWindow( (WindowRef)m_macWindow ) ;
1296 }
1297
1298 // Lower the window to the bottom of the Z order
1299 void wxTopLevelWindowMac::Lower()
1300 {
1301 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1302 }
1303
1304 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1305 {
1306 if (s_macDeactivateWindow)
1307 {
1308 wxLogTrace(TRACE_ACTIVATE,
1309 wxT("Doing delayed deactivation of %p"),
1310 s_macDeactivateWindow);
1311
1312 s_macDeactivateWindow->MacActivate(timestamp, false);
1313 }
1314 }
1315
1316 void wxTopLevelWindowMac::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
1317 {
1318 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1319
1320 if (s_macDeactivateWindow == this)
1321 s_macDeactivateWindow = NULL;
1322
1323 MacDelayedDeactivation(timestamp);
1324 }
1325
1326 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1327 {
1328 wxWindow::SetLabel( title ) ;
1329 SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxMacCFStringHolder( title , m_font.GetEncoding() ) ) ;
1330 }
1331
1332 wxString wxTopLevelWindowMac::GetTitle() const
1333 {
1334 return wxWindow::GetLabel();
1335 }
1336
1337 bool wxTopLevelWindowMac::Show(bool show)
1338 {
1339 if ( !wxTopLevelWindowBase::Show(show) )
1340 return false;
1341
1342 bool plainTransition = true;
1343
1344 #if wxUSE_SYSTEM_OPTIONS
1345 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1346 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1347 #endif
1348
1349 if (show)
1350 {
1351 if ( plainTransition )
1352 ::ShowWindow( (WindowRef)m_macWindow );
1353 else
1354 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1355
1356 ::SelectWindow( (WindowRef)m_macWindow ) ;
1357
1358 // because apps expect a size event to occur at this moment
1359 wxSizeEvent event(GetSize() , m_windowId);
1360 event.SetEventObject(this);
1361 GetEventHandler()->ProcessEvent(event);
1362 }
1363 else
1364 {
1365 if ( plainTransition )
1366 ::HideWindow( (WindowRef)m_macWindow );
1367 else
1368 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1369 }
1370
1371 return true ;
1372 }
1373
1374 bool wxTopLevelWindowMac::ShowWithEffect(wxShowEffect effect,
1375 unsigned timeout,
1376 wxDirection dir)
1377 {
1378 // TODO factor common code
1379 if ( !wxTopLevelWindowBase::Show(true) )
1380 return false;
1381
1382 WindowTransitionEffect transition = 0 ;
1383 switch( effect )
1384 {
1385 case wxSHOW_EFFECT_ROLL :
1386 case wxSHOW_EFFECT_SLIDE :
1387 transition = kWindowGenieTransitionEffect;
1388 break;
1389 case wxSHOW_EFFECT_BLEND :
1390 transition = kWindowFadeTransitionEffect;
1391 break;
1392 case wxSHOW_EFFECT_EXPAND :
1393 default :
1394 // having sheets would be fine, but this might lead to a repositioning
1395 #if 0
1396 if ( GetParent() )
1397 transition = kWindowSheetTransitionEffect;
1398 else
1399 #endif
1400 transition = kWindowZoomTransitionEffect;
1401 break;
1402 }
1403
1404 TransitionWindowOptions options;
1405 options.version = 0;
1406 options.duration = timeout / 1000.0;
1407 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1408 options.userData = 0;
1409
1410 wxSize size = wxGetDisplaySize();
1411 Rect bounds;
1412 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1413 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1414
1415 if ( dir & wxRIGHT )
1416 {
1417 hiBounds.origin.x = size.x;
1418 hiBounds.size.width = 0;
1419 }
1420 if ( dir & wxUP )
1421 {
1422 hiBounds.origin.y = 0;
1423 hiBounds.size.height = 0;
1424 }
1425 if ( dir & wxDOWN )
1426 {
1427 hiBounds.origin.y = size.y;
1428 hiBounds.size.height = 0;
1429 }
1430 if ( dir & wxLEFT )
1431 {
1432 hiBounds.origin.x = 0;
1433 hiBounds.size.width = 0;
1434 }
1435
1436 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowShowTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1437 false, &options );
1438
1439 ::SelectWindow( (WindowRef)m_macWindow ) ;
1440
1441 // because apps expect a size event to occur at this moment
1442 wxSizeEvent event(GetSize() , m_windowId);
1443 event.SetEventObject(this);
1444 GetEventHandler()->ProcessEvent(event);
1445
1446 return true;
1447 }
1448
1449 bool wxTopLevelWindowMac::HideWithEffect(wxShowEffect effect,
1450 unsigned timeout ,
1451 wxDirection dir )
1452 {
1453 if ( !wxTopLevelWindowBase::Show(false) )
1454 return false;
1455
1456 WindowTransitionEffect transition = 0 ;
1457 switch( effect )
1458 {
1459 case wxSHOW_EFFECT_ROLL :
1460 case wxSHOW_EFFECT_SLIDE :
1461 transition = kWindowGenieTransitionEffect;
1462 break;
1463 case wxSHOW_EFFECT_BLEND :
1464 transition = kWindowFadeTransitionEffect;
1465 break;
1466 case wxSHOW_EFFECT_EXPAND :
1467 default:
1468 #if 0
1469 if ( GetParent() )
1470 transition = kWindowSheetTransitionEffect;
1471 else
1472 #endif
1473 transition = kWindowZoomTransitionEffect;
1474 break;
1475 }
1476 TransitionWindowOptions options;
1477 options.version = 0;
1478 options.duration = timeout / 1000.0;
1479 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1480 options.userData = 0;
1481
1482 wxSize size = wxGetDisplaySize();
1483 Rect bounds;
1484 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1485 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1486
1487 if ( dir & wxRIGHT )
1488 {
1489 hiBounds.origin.x = size.x;
1490 hiBounds.size.width = 0;
1491 }
1492 if ( dir & wxUP )
1493 {
1494 hiBounds.origin.y = 0;
1495 hiBounds.size.height = 0;
1496 }
1497 if ( dir & wxDOWN )
1498 {
1499 hiBounds.origin.y = size.y;
1500 hiBounds.size.height = 0;
1501 }
1502 if ( dir & wxLEFT )
1503 {
1504 hiBounds.origin.x = 0;
1505 hiBounds.size.width = 0;
1506 }
1507 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowHideTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1508 false, &options );
1509
1510 return true;
1511 }
1512
1513 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1514 {
1515 if ( show )
1516 {
1517 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1518 delete data ;
1519 data = new FullScreenData() ;
1520
1521 m_macFullScreenData = data ;
1522 data->m_position = GetPosition() ;
1523 data->m_size = GetSize() ;
1524 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
1525
1526 if ( style & wxFULLSCREEN_NOMENUBAR )
1527 HideMenuBar() ;
1528
1529 wxRect client = wxGetClientDisplayRect() ;
1530
1531 int left , top , right , bottom ;
1532 int x, y, w, h ;
1533
1534 x = client.x ;
1535 y = client.y ;
1536 w = client.width ;
1537 h = client.height ;
1538
1539 MacGetContentAreaInset( left , top , right , bottom ) ;
1540
1541 if ( style & wxFULLSCREEN_NOCAPTION )
1542 {
1543 y -= top ;
1544 h += top ;
1545 }
1546
1547 if ( style & wxFULLSCREEN_NOBORDER )
1548 {
1549 x -= left ;
1550 w += left + right ;
1551 h += bottom ;
1552 }
1553
1554 if ( style & wxFULLSCREEN_NOTOOLBAR )
1555 {
1556 // TODO
1557 }
1558
1559 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1560 {
1561 // TODO
1562 }
1563
1564 SetSize( x , y , w, h ) ;
1565 if ( data->m_wasResizable )
1566 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
1567 }
1568 else
1569 {
1570 ShowMenuBar() ;
1571 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1572 if ( data->m_wasResizable )
1573 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
1574 SetPosition( data->m_position ) ;
1575 SetSize( data->m_size ) ;
1576
1577 delete data ;
1578 m_macFullScreenData = NULL ;
1579 }
1580
1581 return false;
1582 }
1583
1584 bool wxTopLevelWindowMac::IsFullScreen() const
1585 {
1586 return m_macFullScreenData != NULL ;
1587 }
1588
1589
1590 bool wxTopLevelWindowMac::SetTransparent(wxByte alpha)
1591 {
1592 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, float(alpha)/255.0);
1593 return result == noErr;
1594 }
1595
1596
1597 bool wxTopLevelWindowMac::CanSetTransparent()
1598 {
1599 return true;
1600 }
1601
1602
1603 void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1604 {
1605 if ( GetExtraStyle() == exStyle )
1606 return ;
1607
1608 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
1609
1610 if ( m_macWindow != NULL )
1611 {
1612 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1613
1614 if ( MacGetMetalAppearance() != metal )
1615 {
1616 if ( MacGetUnifiedAppearance() )
1617 MacSetUnifiedAppearance( !metal ) ;
1618
1619 MacSetMetalAppearance( metal ) ;
1620 }
1621 }
1622 }
1623
1624 bool wxTopLevelWindowMac::SetBackgroundStyle(wxBackgroundStyle style)
1625 {
1626 if ( !wxTopLevelWindowBase::SetBackgroundStyle(style) )
1627 return false ;
1628
1629 WindowRef windowRef = HIViewGetWindow( (HIViewRef)GetHandle() );
1630
1631 if ( GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
1632 {
1633 OSStatus err = HIWindowChangeFeatures( windowRef, 0, kWindowIsOpaque );
1634 verify_noerr( err );
1635 err = ReshapeCustomWindow( windowRef );
1636 verify_noerr( err );
1637 }
1638
1639 return true ;
1640 }
1641
1642 // TODO: switch to structure bounds -
1643 // we are still using coordinates of the content view
1644 //
1645 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1646 {
1647 Rect content, structure ;
1648
1649 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1650 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1651
1652 left = content.left - structure.left ;
1653 top = content.top - structure.top ;
1654 right = structure.right - content.right ;
1655 bottom = structure.bottom - content.bottom ;
1656 }
1657
1658 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1659 {
1660 m_cachedClippedRectValid = false ;
1661 Rect bounds = { y , x , y + height , x + width } ;
1662 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1663 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1664 }
1665
1666 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1667 {
1668 Rect bounds ;
1669
1670 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1671
1672 if (x)
1673 *x = bounds.left ;
1674 if (y)
1675 *y = bounds.top ;
1676 }
1677
1678 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1679 {
1680 Rect bounds ;
1681
1682 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1683
1684 if (width)
1685 *width = bounds.right - bounds.left ;
1686 if (height)
1687 *height = bounds.bottom - bounds.top ;
1688 }
1689
1690 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1691 {
1692 Rect bounds ;
1693
1694 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1695
1696 if (width)
1697 *width = bounds.right - bounds.left ;
1698 if (height)
1699 *height = bounds.bottom - bounds.top ;
1700 }
1701
1702 void wxTopLevelWindowMac::DoCentre(int dir)
1703 {
1704 if ( m_macWindow != 0 )
1705 wxTopLevelWindowBase::DoCentre(dir);
1706 }
1707
1708 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1709 {
1710 if ( MacGetUnifiedAppearance() )
1711 MacSetUnifiedAppearance( false ) ;
1712
1713 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1714 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1715 }
1716
1717 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1718 {
1719 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1720 }
1721
1722 void wxTopLevelWindowMac::MacSetUnifiedAppearance( bool set )
1723 {
1724 if ( MacGetMetalAppearance() )
1725 MacSetMetalAppearance( false ) ;
1726
1727 MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
1728 set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
1729
1730 // For some reason, Tiger uses white as the background color for this appearance,
1731 // while most apps using it use the typical striped background. Restore that behavior
1732 // for wx.
1733 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
1734 // though)
1735 SetBackgroundColour( wxSYS_COLOUR_WINDOW ) ;
1736 }
1737
1738 bool wxTopLevelWindowMac::MacGetUnifiedAppearance() const
1739 {
1740 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
1741 }
1742
1743 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1744 {
1745 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1746 }
1747
1748 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1749 {
1750 UInt32 attr = 0 ;
1751 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1752
1753 return attr ;
1754 }
1755
1756 void wxTopLevelWindowMac::MacPerformUpdates()
1757 {
1758 // for composited windows this also triggers a redraw of all
1759 // invalid views in the window
1760 HIWindowFlush((WindowRef) m_macWindow) ;
1761 }
1762
1763 // Attracts the users attention to this window if the application is
1764 // inactive (should be called when a background event occurs)
1765
1766 static pascal void wxMacNMResponse( NMRecPtr ptr )
1767 {
1768 NMRemove( ptr ) ;
1769 DisposePtr( (Ptr)ptr ) ;
1770 }
1771
1772 void wxTopLevelWindowMac::RequestUserAttention(int WXUNUSED(flags))
1773 {
1774 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1775 static wxMacNMUPP nmupp( wxMacNMResponse );
1776
1777 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1778 notificationRequest->qType = nmType ;
1779 notificationRequest->nmMark = 1 ;
1780 notificationRequest->nmIcon = 0 ;
1781 notificationRequest->nmSound = 0 ;
1782 notificationRequest->nmStr = NULL ;
1783 notificationRequest->nmResp = nmupp ;
1784
1785 verify_noerr( NMInstall( notificationRequest ) ) ;
1786 }
1787
1788 // ---------------------------------------------------------------------------
1789 // Shape implementation
1790 // ---------------------------------------------------------------------------
1791
1792
1793 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1794 {
1795 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1796 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1797
1798 // The empty region signifies that the shape
1799 // should be removed from the window.
1800 if ( region.IsEmpty() )
1801 {
1802 wxSize sz = GetClientSize();
1803 wxRegion rgn(0, 0, sz.x, sz.y);
1804 if ( rgn.IsEmpty() )
1805 return false ;
1806 else
1807 return SetShape(rgn);
1808 }
1809
1810 // Make a copy of the region
1811 RgnHandle shapeRegion = NewRgn();
1812 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1813
1814 // Dispose of any shape region we may already have
1815 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1816 if ( oldRgn )
1817 DisposeRgn(oldRgn);
1818
1819 // Save the region so we can use it later
1820 SetWRefCon((WindowRef)MacGetWindowRef(), (URefCon)shapeRegion);
1821
1822 // inform the window manager that the window has changed shape
1823 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1824
1825 return true;
1826 }
1827
1828 // ---------------------------------------------------------------------------
1829 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1830 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1831 // ---------------------------------------------------------------------------
1832
1833 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1834 {
1835 GetWindowPortBounds(window, inRect);
1836 Point pt = { inRect->top ,inRect->left };
1837 wxMacLocalToGlobal( window, &pt ) ;
1838 inRect->bottom += pt.v - inRect->top;
1839 inRect->right += pt.h - inRect->left;
1840 inRect->top = pt.v;
1841 inRect->left = pt.h;
1842 }
1843
1844 static SInt32 wxShapedMacWindowGetFeatures(WindowRef WXUNUSED(window), SInt32 param)
1845 {
1846 /*------------------------------------------------------
1847 Define which options your custom window supports.
1848 --------------------------------------------------------*/
1849 //just enable everything for our demo
1850 *(OptionBits*)param =
1851 //kWindowCanGrow |
1852 //kWindowCanZoom |
1853 //kWindowCanCollapse |
1854 //kWindowCanGetWindowRegion |
1855 //kWindowHasTitleBar |
1856 //kWindowSupportsDragHilite |
1857 kWindowCanDrawInCurrentPort |
1858 //kWindowCanMeasureTitle |
1859 kWindowWantsDisposeAtProcessDeath |
1860 kWindowSupportsGetGrowImageRegion |
1861 kWindowDefSupportsColorGrafPort;
1862
1863 return 1;
1864 }
1865
1866 // The content region is left as a rectangle matching the window size, this is
1867 // so the origin in the paint event, and etc. still matches what the
1868 // programmer expects.
1869 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1870 {
1871 SetEmptyRgn(rgn);
1872 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1873 if (win)
1874 {
1875 Rect r ;
1876 wxShapedMacWindowGetPos( window, &r ) ;
1877 RectRgn( rgn , &r ) ;
1878 }
1879 }
1880
1881 // The structure region is set to the shape given to the SetShape method.
1882 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1883 {
1884 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1885
1886 SetEmptyRgn(rgn);
1887 if (cachedRegion)
1888 {
1889 Rect windowRect;
1890 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1891 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1892 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1893 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1894 }
1895 }
1896
1897 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1898 {
1899 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1900
1901 if (rgnRec == NULL)
1902 return paramErr;
1903
1904 switch (rgnRec->regionCode)
1905 {
1906 case kWindowStructureRgn:
1907 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1908 break;
1909
1910 case kWindowContentRgn:
1911 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1912 break;
1913
1914 default:
1915 SetEmptyRgn(rgnRec->winRgn);
1916 break;
1917 }
1918
1919 return noErr;
1920 }
1921
1922 // Determine the region of the window which was hit
1923 //
1924 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1925 {
1926 Point hitPoint;
1927 static RgnHandle tempRgn = NULL;
1928
1929 if (tempRgn == NULL)
1930 tempRgn = NewRgn();
1931
1932 // get the point clicked
1933 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1934
1935 // Mac OS 8.5 or later
1936 wxShapedMacWindowStructureRegion(window, tempRgn);
1937 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1938 return wInContent;
1939
1940 // no significant area was hit
1941 return wNoHit;
1942 }
1943
1944 static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode), WindowRef window, SInt16 message, SInt32 param)
1945 {
1946 switch (message)
1947 {
1948 case kWindowMsgHitTest:
1949 return wxShapedMacWindowHitTest(window, param);
1950
1951 case kWindowMsgGetFeatures:
1952 return wxShapedMacWindowGetFeatures(window, param);
1953
1954 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1955 case kWindowMsgGetRegion:
1956 return wxShapedMacWindowGetRegion(window, param);
1957
1958 default:
1959 break;
1960 }
1961
1962 return 0;
1963 }