]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
31e507961bcb2a4c4fb22dad58e84925247805ce
[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(wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)));
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 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDocumentWindowBackground, false ) ;
1020 else if ( col == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) || col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
1021 SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDialogBackgroundActive, false ) ;
1022 // TODO BETTER THEME SUPPORT
1023 return true;
1024 }
1025
1026 void wxTopLevelWindowMacInstallTopLevelWindowEventHandler(WindowRef window, EventHandlerRef* handler, void *ref)
1027 {
1028 InstallWindowEventHandler(window, GetwxMacTopLevelEventHandlerUPP(),
1029 GetEventTypeCount(eventList), eventList, ref, handler );
1030 }
1031
1032 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
1033 {
1034 if ( m_macEventHandler != NULL )
1035 {
1036 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1037 }
1038 wxTopLevelWindowMacInstallTopLevelWindowEventHandler(MAC_WXHWND(m_macWindow),(EventHandlerRef *)&m_macEventHandler,this);
1039 }
1040
1041 void wxTopLevelWindowMac::MacCreateRealWindow(
1042 const wxString& title,
1043 const wxPoint& pos,
1044 const wxSize& size,
1045 long style,
1046 const wxString& name )
1047 {
1048 DoMacCreateRealWindow( NULL, title, pos, size, style, name );
1049 }
1050
1051 void wxTopLevelWindowMac::DoMacCreateRealWindow(
1052 wxWindow* parent,
1053 const wxString& title,
1054 const wxPoint& pos,
1055 const wxSize& size,
1056 long style,
1057 const wxString& name )
1058 {
1059 OSStatus err = noErr ;
1060 SetName(name);
1061 m_windowStyle = style;
1062 m_isShown = false;
1063
1064 // create frame.
1065 int x = (int)pos.x;
1066 int y = (int)pos.y;
1067
1068 Rect theBoundsRect;
1069 wxRect display = wxGetClientDisplayRect() ;
1070
1071 if ( x == wxDefaultPosition.x )
1072 x = display.x ;
1073
1074 if ( y == wxDefaultPosition.y )
1075 y = display.y ;
1076
1077 int w = WidthDefault(size.x);
1078 int h = HeightDefault(size.y);
1079
1080 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1081
1082 // translate the window attributes in the appropriate window class and attributes
1083 WindowClass wclass = 0;
1084 WindowAttributes attr = kWindowNoAttributes ;
1085 WindowGroupRef group = NULL ;
1086 bool activationScopeSet = false;
1087 WindowActivationScope activationScope = kWindowActivationScopeNone;
1088
1089 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
1090 {
1091 if (
1092 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1093 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1094 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
1095 )
1096 {
1097 if ( HasFlag( wxSTAY_ON_TOP ) )
1098 wclass = kUtilityWindowClass;
1099 else
1100 wclass = kFloatingWindowClass ;
1101
1102 if ( HasFlag(wxTINY_CAPTION_VERT) )
1103 attr |= kWindowSideTitlebarAttribute ;
1104 }
1105 else
1106 {
1107 wclass = kPlainWindowClass ;
1108 activationScopeSet = true;
1109 activationScope = kWindowActivationScopeNone;
1110 }
1111 }
1112 else if ( HasFlag( wxPOPUP_WINDOW ) )
1113 {
1114 // TEMPORARY HACK!
1115 // Until we've got a real wxPopupWindow class on wxMac make it a
1116 // little easier for wxFrame to be used to emulate it and workaround
1117 // the lack of wxPopupWindow.
1118 if ( HasFlag( wxBORDER_NONE ) )
1119 wclass = kHelpWindowClass ; // has no border
1120 else
1121 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1122 //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
1123 group = GetWindowGroupOfClass( // float above other windows
1124 kFloatingWindowClass) ;
1125 }
1126 else if ( HasFlag( wxCAPTION ) )
1127 {
1128 wclass = kDocumentWindowClass ;
1129 attr |= kWindowInWindowMenuAttribute ;
1130 }
1131 else if ( HasFlag( wxFRAME_DRAWER ) )
1132 {
1133 wclass = kDrawerWindowClass;
1134 }
1135 else
1136 {
1137 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1138 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1139 {
1140 wclass = kDocumentWindowClass ;
1141 }
1142 else if ( HasFlag( wxNO_BORDER ) )
1143 {
1144 wclass = kSimpleWindowClass ;
1145 }
1146 else
1147 {
1148 wclass = kPlainWindowClass ;
1149 }
1150 }
1151
1152 if ( wclass != kPlainWindowClass )
1153 {
1154 if ( HasFlag( wxMINIMIZE_BOX ) )
1155 attr |= kWindowCollapseBoxAttribute ;
1156
1157 if ( HasFlag( wxMAXIMIZE_BOX ) )
1158 attr |= kWindowFullZoomAttribute ;
1159
1160 if ( HasFlag( wxRESIZE_BORDER ) )
1161 attr |= kWindowResizableAttribute ;
1162
1163 if ( HasFlag( wxCLOSE_BOX) )
1164 attr |= kWindowCloseBoxAttribute ;
1165 }
1166 attr |= kWindowLiveResizeAttribute;
1167
1168 if ( HasFlag(wxSTAY_ON_TOP) )
1169 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1170
1171 if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
1172 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1173
1174 if ( group == NULL && parent != NULL )
1175 {
1176 WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
1177 if( parenttlw )
1178 group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
1179 }
1180
1181 attr |= kWindowCompositingAttribute;
1182 #if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1183 attr |= kWindowFrameworkScaledAttribute;
1184 #endif
1185
1186 if ( HasFlag(wxFRAME_SHAPED) )
1187 {
1188 WindowDefSpec customWindowDefSpec;
1189 customWindowDefSpec.defType = kWindowDefProcPtr;
1190 customWindowDefSpec.u.defProc =
1191 #ifdef __LP64__
1192 (WindowDefUPP) wxShapedMacWindowDef;
1193 #else
1194 NewWindowDefUPP(wxShapedMacWindowDef);
1195 #endif
1196 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1197 attr, &theBoundsRect,
1198 (WindowRef*) &m_macWindow);
1199 }
1200 else
1201 {
1202 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1203 }
1204
1205 if ( err == noErr && m_macWindow != NULL && group != NULL )
1206 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1207
1208 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1209
1210 // setup a separate group for each window, so that overlays can be handled easily
1211
1212 WindowGroupRef overlaygroup = NULL;
1213 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
1214 verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
1215 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
1216
1217 if ( activationScopeSet )
1218 {
1219 verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
1220 }
1221
1222 // the create commands are only for content rect,
1223 // so we have to set the size again as structure bounds
1224 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1225
1226 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1227 SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxMacCFStringHolder( title , m_font.GetEncoding() ) );
1228 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1229
1230 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1231 // the content view, so we have to retrieve it explicitly
1232 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1233 m_peer->GetControlRefAddr() ) ;
1234 if ( !m_peer->Ok() )
1235 {
1236 // compatibility mode fallback
1237 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1238 }
1239
1240 // the root control level handler
1241 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1242
1243 // Causes the inner part of the window not to be metal
1244 // if the style is used before window creation.
1245 #if 0 // TARGET_API_MAC_OSX
1246 if ( m_macUsesCompositing && m_macWindow != NULL )
1247 {
1248 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1249 MacSetMetalAppearance( true ) ;
1250 }
1251 #endif
1252
1253 if ( m_macWindow != NULL )
1254 {
1255 MacSetUnifiedAppearance( true ) ;
1256 }
1257
1258 HIViewRef growBoxRef = 0 ;
1259 err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
1260 if ( err == noErr && growBoxRef != 0 )
1261 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
1262
1263 // the frame window event handler
1264 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1265 MacInstallTopLevelWindowEventHandler() ;
1266
1267 DoSetWindowVariant( m_windowVariant ) ;
1268
1269 m_macFocus = NULL ;
1270
1271 if ( HasFlag(wxFRAME_SHAPED) )
1272 {
1273 // default shape matches the window size
1274 wxRegion rgn( 0, 0, w, h );
1275 SetShape( rgn );
1276 }
1277
1278 wxWindowCreateEvent event(this);
1279 GetEventHandler()->ProcessEvent(event);
1280 }
1281
1282 void wxTopLevelWindowMac::ClearBackground()
1283 {
1284 wxWindow::ClearBackground() ;
1285 }
1286
1287 // Raise the window to the top of the Z order
1288 void wxTopLevelWindowMac::Raise()
1289 {
1290 ::SelectWindow( (WindowRef)m_macWindow ) ;
1291 }
1292
1293 // Lower the window to the bottom of the Z order
1294 void wxTopLevelWindowMac::Lower()
1295 {
1296 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1297 }
1298
1299 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1300 {
1301 if (s_macDeactivateWindow)
1302 {
1303 wxLogTrace(TRACE_ACTIVATE,
1304 wxT("Doing delayed deactivation of %p"),
1305 s_macDeactivateWindow);
1306
1307 s_macDeactivateWindow->MacActivate(timestamp, false);
1308 }
1309 }
1310
1311 void wxTopLevelWindowMac::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
1312 {
1313 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1314
1315 if (s_macDeactivateWindow == this)
1316 s_macDeactivateWindow = NULL;
1317
1318 MacDelayedDeactivation(timestamp);
1319 }
1320
1321 void wxTopLevelWindowMac::SetTitle(const wxString& title)
1322 {
1323 wxWindow::SetLabel( title ) ;
1324 SetWindowTitleWithCFString( (WindowRef) m_macWindow , wxMacCFStringHolder( title , m_font.GetEncoding() ) ) ;
1325 }
1326
1327 wxString wxTopLevelWindowMac::GetTitle() const
1328 {
1329 return wxWindow::GetLabel();
1330 }
1331
1332 bool wxTopLevelWindowMac::Show(bool show)
1333 {
1334 if ( !wxTopLevelWindowBase::Show(show) )
1335 return false;
1336
1337 bool plainTransition = true;
1338
1339 #if wxUSE_SYSTEM_OPTIONS
1340 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1341 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1342 #endif
1343
1344 if (show)
1345 {
1346 if ( plainTransition )
1347 ::ShowWindow( (WindowRef)m_macWindow );
1348 else
1349 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1350
1351 ::SelectWindow( (WindowRef)m_macWindow ) ;
1352
1353 // because apps expect a size event to occur at this moment
1354 wxSizeEvent event(GetSize() , m_windowId);
1355 event.SetEventObject(this);
1356 GetEventHandler()->ProcessEvent(event);
1357 }
1358 else
1359 {
1360 if ( plainTransition )
1361 ::HideWindow( (WindowRef)m_macWindow );
1362 else
1363 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1364 }
1365
1366 return true ;
1367 }
1368
1369 bool wxTopLevelWindowMac::ShowWithEffect(wxShowEffect effect,
1370 unsigned timeout,
1371 wxDirection dir)
1372 {
1373 // TODO factor common code
1374 if ( !wxTopLevelWindowBase::Show(true) )
1375 return false;
1376
1377 WindowTransitionEffect transition = 0 ;
1378 switch( effect )
1379 {
1380 case wxSHOW_EFFECT_ROLL :
1381 case wxSHOW_EFFECT_SLIDE :
1382 transition = kWindowGenieTransitionEffect;
1383 break;
1384 case wxSHOW_EFFECT_BLEND :
1385 transition = kWindowFadeTransitionEffect;
1386 break;
1387 case wxSHOW_EFFECT_EXPAND :
1388 default :
1389 // having sheets would be fine, but this might lead to a repositioning
1390 #if 0
1391 if ( GetParent() )
1392 transition = kWindowSheetTransitionEffect;
1393 else
1394 #endif
1395 transition = kWindowZoomTransitionEffect;
1396 break;
1397 }
1398
1399 TransitionWindowOptions options;
1400 options.version = 0;
1401 options.duration = timeout / 1000.0;
1402 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1403 options.userData = 0;
1404
1405 wxSize size = wxGetDisplaySize();
1406 Rect bounds;
1407 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1408 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1409
1410 if ( dir & wxRIGHT )
1411 {
1412 hiBounds.origin.x = size.x;
1413 hiBounds.size.width = 0;
1414 }
1415 if ( dir & wxUP )
1416 {
1417 hiBounds.origin.y = 0;
1418 hiBounds.size.height = 0;
1419 }
1420 if ( dir & wxDOWN )
1421 {
1422 hiBounds.origin.y = size.y;
1423 hiBounds.size.height = 0;
1424 }
1425 if ( dir & wxLEFT )
1426 {
1427 hiBounds.origin.x = 0;
1428 hiBounds.size.width = 0;
1429 }
1430
1431 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowShowTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1432 false, &options );
1433
1434 ::SelectWindow( (WindowRef)m_macWindow ) ;
1435
1436 // because apps expect a size event to occur at this moment
1437 wxSizeEvent event(GetSize() , m_windowId);
1438 event.SetEventObject(this);
1439 GetEventHandler()->ProcessEvent(event);
1440
1441 return true;
1442 }
1443
1444 bool wxTopLevelWindowMac::HideWithEffect(wxShowEffect effect,
1445 unsigned timeout ,
1446 wxDirection dir )
1447 {
1448 if ( !wxTopLevelWindowBase::Show(false) )
1449 return false;
1450
1451 WindowTransitionEffect transition = 0 ;
1452 switch( effect )
1453 {
1454 case wxSHOW_EFFECT_ROLL :
1455 case wxSHOW_EFFECT_SLIDE :
1456 transition = kWindowGenieTransitionEffect;
1457 break;
1458 case wxSHOW_EFFECT_BLEND :
1459 transition = kWindowFadeTransitionEffect;
1460 break;
1461 case wxSHOW_EFFECT_EXPAND :
1462 default:
1463 #if 0
1464 if ( GetParent() )
1465 transition = kWindowSheetTransitionEffect;
1466 else
1467 #endif
1468 transition = kWindowZoomTransitionEffect;
1469 break;
1470 }
1471 TransitionWindowOptions options;
1472 options.version = 0;
1473 options.duration = timeout / 1000.0;
1474 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1475 options.userData = 0;
1476
1477 wxSize size = wxGetDisplaySize();
1478 Rect bounds;
1479 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1480 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1481
1482 if ( dir & wxRIGHT )
1483 {
1484 hiBounds.origin.x = size.x;
1485 hiBounds.size.width = 0;
1486 }
1487 if ( dir & wxUP )
1488 {
1489 hiBounds.origin.y = 0;
1490 hiBounds.size.height = 0;
1491 }
1492 if ( dir & wxDOWN )
1493 {
1494 hiBounds.origin.y = size.y;
1495 hiBounds.size.height = 0;
1496 }
1497 if ( dir & wxLEFT )
1498 {
1499 hiBounds.origin.x = 0;
1500 hiBounds.size.width = 0;
1501 }
1502 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowHideTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1503 false, &options );
1504
1505 return true;
1506 }
1507
1508 bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
1509 {
1510 if ( show )
1511 {
1512 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1513 delete data ;
1514 data = new FullScreenData() ;
1515
1516 m_macFullScreenData = data ;
1517 data->m_position = GetPosition() ;
1518 data->m_size = GetSize() ;
1519 data->m_wasResizable = MacGetWindowAttributes() & kWindowResizableAttribute ;
1520
1521 if ( style & wxFULLSCREEN_NOMENUBAR )
1522 HideMenuBar() ;
1523
1524 wxRect client = wxGetClientDisplayRect() ;
1525
1526 int left , top , right , bottom ;
1527 int x, y, w, h ;
1528
1529 x = client.x ;
1530 y = client.y ;
1531 w = client.width ;
1532 h = client.height ;
1533
1534 MacGetContentAreaInset( left , top , right , bottom ) ;
1535
1536 if ( style & wxFULLSCREEN_NOCAPTION )
1537 {
1538 y -= top ;
1539 h += top ;
1540 }
1541
1542 if ( style & wxFULLSCREEN_NOBORDER )
1543 {
1544 x -= left ;
1545 w += left + right ;
1546 h += bottom ;
1547 }
1548
1549 if ( style & wxFULLSCREEN_NOTOOLBAR )
1550 {
1551 // TODO
1552 }
1553
1554 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1555 {
1556 // TODO
1557 }
1558
1559 SetSize( x , y , w, h ) ;
1560 if ( data->m_wasResizable )
1561 MacChangeWindowAttributes( kWindowNoAttributes , kWindowResizableAttribute ) ;
1562 }
1563 else
1564 {
1565 ShowMenuBar() ;
1566 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1567 if ( data->m_wasResizable )
1568 MacChangeWindowAttributes( kWindowResizableAttribute , kWindowNoAttributes ) ;
1569 SetPosition( data->m_position ) ;
1570 SetSize( data->m_size ) ;
1571
1572 delete data ;
1573 m_macFullScreenData = NULL ;
1574 }
1575
1576 return false;
1577 }
1578
1579 bool wxTopLevelWindowMac::IsFullScreen() const
1580 {
1581 return m_macFullScreenData != NULL ;
1582 }
1583
1584
1585 bool wxTopLevelWindowMac::SetTransparent(wxByte alpha)
1586 {
1587 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, float(alpha)/255.0);
1588 return result == noErr;
1589 }
1590
1591
1592 bool wxTopLevelWindowMac::CanSetTransparent()
1593 {
1594 return true;
1595 }
1596
1597
1598 void wxTopLevelWindowMac::SetExtraStyle(long exStyle)
1599 {
1600 if ( GetExtraStyle() == exStyle )
1601 return ;
1602
1603 wxTopLevelWindowBase::SetExtraStyle( exStyle ) ;
1604
1605 if ( m_macWindow != NULL )
1606 {
1607 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1608
1609 if ( MacGetMetalAppearance() != metal )
1610 {
1611 if ( MacGetUnifiedAppearance() )
1612 MacSetUnifiedAppearance( !metal ) ;
1613
1614 MacSetMetalAppearance( metal ) ;
1615 }
1616 }
1617 }
1618
1619 bool wxTopLevelWindowMac::SetBackgroundStyle(wxBackgroundStyle style)
1620 {
1621 if ( !wxTopLevelWindowBase::SetBackgroundStyle(style) )
1622 return false ;
1623
1624 WindowRef windowRef = HIViewGetWindow( (HIViewRef)GetHandle() );
1625
1626 if ( GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
1627 {
1628 OSStatus err = HIWindowChangeFeatures( windowRef, 0, kWindowIsOpaque );
1629 verify_noerr( err );
1630 err = ReshapeCustomWindow( windowRef );
1631 verify_noerr( err );
1632 }
1633
1634 return true ;
1635 }
1636
1637 // TODO: switch to structure bounds -
1638 // we are still using coordinates of the content view
1639 //
1640 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1641 {
1642 Rect content, structure ;
1643
1644 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1645 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1646
1647 left = content.left - structure.left ;
1648 top = content.top - structure.top ;
1649 right = structure.right - content.right ;
1650 bottom = structure.bottom - content.bottom ;
1651 }
1652
1653 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1654 {
1655 m_cachedClippedRectValid = false ;
1656 Rect bounds = { y , x , y + height , x + width } ;
1657 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1658 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1659 }
1660
1661 void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
1662 {
1663 Rect bounds ;
1664
1665 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1666
1667 if (x)
1668 *x = bounds.left ;
1669 if (y)
1670 *y = bounds.top ;
1671 }
1672
1673 void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1674 {
1675 Rect bounds ;
1676
1677 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1678
1679 if (width)
1680 *width = bounds.right - bounds.left ;
1681 if (height)
1682 *height = bounds.bottom - bounds.top ;
1683 }
1684
1685 void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1686 {
1687 Rect bounds ;
1688
1689 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1690
1691 if (width)
1692 *width = bounds.right - bounds.left ;
1693 if (height)
1694 *height = bounds.bottom - bounds.top ;
1695 }
1696
1697 void wxTopLevelWindowMac::DoCentre(int dir)
1698 {
1699 if ( m_macWindow != 0 )
1700 wxTopLevelWindowBase::DoCentre(dir);
1701 }
1702
1703 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
1704 {
1705 if ( MacGetUnifiedAppearance() )
1706 MacSetUnifiedAppearance( false ) ;
1707
1708 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1709 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1710 }
1711
1712 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1713 {
1714 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1715 }
1716
1717 void wxTopLevelWindowMac::MacSetUnifiedAppearance( bool set )
1718 {
1719 if ( MacGetMetalAppearance() )
1720 MacSetMetalAppearance( false ) ;
1721
1722 MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
1723 set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
1724
1725 // For some reason, Tiger uses white as the background color for this appearance,
1726 // while most apps using it use the typical striped background. Restore that behavior
1727 // for wx.
1728 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
1729 // though)
1730 SetBackgroundColour( wxSYS_COLOUR_WINDOW ) ;
1731 }
1732
1733 bool wxTopLevelWindowMac::MacGetUnifiedAppearance() const
1734 {
1735 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
1736 }
1737
1738 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1739 {
1740 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1741 }
1742
1743 wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
1744 {
1745 UInt32 attr = 0 ;
1746 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1747
1748 return attr ;
1749 }
1750
1751 void wxTopLevelWindowMac::MacPerformUpdates()
1752 {
1753 // for composited windows this also triggers a redraw of all
1754 // invalid views in the window
1755 HIWindowFlush((WindowRef) m_macWindow) ;
1756 }
1757
1758 // Attracts the users attention to this window if the application is
1759 // inactive (should be called when a background event occurs)
1760
1761 static pascal void wxMacNMResponse( NMRecPtr ptr )
1762 {
1763 NMRemove( ptr ) ;
1764 DisposePtr( (Ptr)ptr ) ;
1765 }
1766
1767 void wxTopLevelWindowMac::RequestUserAttention(int WXUNUSED(flags))
1768 {
1769 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1770 static wxMacNMUPP nmupp( wxMacNMResponse );
1771
1772 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1773 notificationRequest->qType = nmType ;
1774 notificationRequest->nmMark = 1 ;
1775 notificationRequest->nmIcon = 0 ;
1776 notificationRequest->nmSound = 0 ;
1777 notificationRequest->nmStr = NULL ;
1778 notificationRequest->nmResp = nmupp ;
1779
1780 verify_noerr( NMInstall( notificationRequest ) ) ;
1781 }
1782
1783 // ---------------------------------------------------------------------------
1784 // Shape implementation
1785 // ---------------------------------------------------------------------------
1786
1787
1788 bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1789 {
1790 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1791 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1792
1793 // The empty region signifies that the shape
1794 // should be removed from the window.
1795 if ( region.IsEmpty() )
1796 {
1797 wxSize sz = GetClientSize();
1798 wxRegion rgn(0, 0, sz.x, sz.y);
1799 if ( rgn.IsEmpty() )
1800 return false ;
1801 else
1802 return SetShape(rgn);
1803 }
1804
1805 // Make a copy of the region
1806 RgnHandle shapeRegion = NewRgn();
1807 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1808
1809 // Dispose of any shape region we may already have
1810 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1811 if ( oldRgn )
1812 DisposeRgn(oldRgn);
1813
1814 // Save the region so we can use it later
1815 SetWRefCon((WindowRef)MacGetWindowRef(), (URefCon)shapeRegion);
1816
1817 // inform the window manager that the window has changed shape
1818 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1819
1820 return true;
1821 }
1822
1823 // ---------------------------------------------------------------------------
1824 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1825 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1826 // ---------------------------------------------------------------------------
1827
1828 static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1829 {
1830 GetWindowPortBounds(window, inRect);
1831 Point pt = { inRect->top ,inRect->left };
1832 wxMacLocalToGlobal( window, &pt ) ;
1833 inRect->bottom += pt.v - inRect->top;
1834 inRect->right += pt.h - inRect->left;
1835 inRect->top = pt.v;
1836 inRect->left = pt.h;
1837 }
1838
1839 static SInt32 wxShapedMacWindowGetFeatures(WindowRef WXUNUSED(window), SInt32 param)
1840 {
1841 /*------------------------------------------------------
1842 Define which options your custom window supports.
1843 --------------------------------------------------------*/
1844 //just enable everything for our demo
1845 *(OptionBits*)param =
1846 //kWindowCanGrow |
1847 //kWindowCanZoom |
1848 //kWindowCanCollapse |
1849 //kWindowCanGetWindowRegion |
1850 //kWindowHasTitleBar |
1851 //kWindowSupportsDragHilite |
1852 kWindowCanDrawInCurrentPort |
1853 //kWindowCanMeasureTitle |
1854 kWindowWantsDisposeAtProcessDeath |
1855 kWindowSupportsGetGrowImageRegion |
1856 kWindowDefSupportsColorGrafPort;
1857
1858 return 1;
1859 }
1860
1861 // The content region is left as a rectangle matching the window size, this is
1862 // so the origin in the paint event, and etc. still matches what the
1863 // programmer expects.
1864 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1865 {
1866 SetEmptyRgn(rgn);
1867 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1868 if (win)
1869 {
1870 Rect r ;
1871 wxShapedMacWindowGetPos( window, &r ) ;
1872 RectRgn( rgn , &r ) ;
1873 }
1874 }
1875
1876 // The structure region is set to the shape given to the SetShape method.
1877 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1878 {
1879 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1880
1881 SetEmptyRgn(rgn);
1882 if (cachedRegion)
1883 {
1884 Rect windowRect;
1885 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1886 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1887 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1888 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1889 }
1890 }
1891
1892 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1893 {
1894 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1895
1896 if (rgnRec == NULL)
1897 return paramErr;
1898
1899 switch (rgnRec->regionCode)
1900 {
1901 case kWindowStructureRgn:
1902 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1903 break;
1904
1905 case kWindowContentRgn:
1906 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1907 break;
1908
1909 default:
1910 SetEmptyRgn(rgnRec->winRgn);
1911 break;
1912 }
1913
1914 return noErr;
1915 }
1916
1917 // Determine the region of the window which was hit
1918 //
1919 static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1920 {
1921 Point hitPoint;
1922 static RgnHandle tempRgn = NULL;
1923
1924 if (tempRgn == NULL)
1925 tempRgn = NewRgn();
1926
1927 // get the point clicked
1928 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1929
1930 // Mac OS 8.5 or later
1931 wxShapedMacWindowStructureRegion(window, tempRgn);
1932 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1933 return wInContent;
1934
1935 // no significant area was hit
1936 return wNoHit;
1937 }
1938
1939 static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode), WindowRef window, SInt16 message, SInt32 param)
1940 {
1941 switch (message)
1942 {
1943 case kWindowMsgHitTest:
1944 return wxShapedMacWindowHitTest(window, param);
1945
1946 case kWindowMsgGetFeatures:
1947 return wxShapedMacWindowGetFeatures(window, param);
1948
1949 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1950 case kWindowMsgGetRegion:
1951 return wxShapedMacWindowGetRegion(window, param);
1952
1953 default:
1954 break;
1955 }
1956
1957 return 0;
1958 }