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