]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/nonownedwnd.cpp
fixing custom background on non owned windows
[wxWidgets.git] / src / mac / carbon / nonownedwnd.cpp
CommitLineData
8d007ed4
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
23#include "wx/mac/private.h"
c887f67c
JS
24#include "wx/settings.h"
25#include "wx/frame.h"
8d007ed4
SC
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
4b8dab63 39#define TRACE_ACTIVATE "activation"
8d007ed4
SC
40
41// ----------------------------------------------------------------------------
42// globals
43// ----------------------------------------------------------------------------
44
8d007ed4
SC
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
8d007ed4
SC
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
8d007ed4
SC
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
328f0df8 898 if ( !wxWindow::SetBackgroundColour(col) && m_hasBgCol )
8d007ed4
SC
899 return false ;
900
a66cf5a0 901 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
8d007ed4 902 {
a66cf5a0
SC
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 }
328f0df8 913 }
8d007ed4
SC
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 // TEMPORARY HACK!
1004 // Until we've got a real wxPopupWindow class on wxMac make it a
1005 // little easier for wxFrame to be used to emulate it and workaround
1006 // the lack of wxPopupWindow.
1007 if ( HasFlag( wxBORDER_NONE ) )
1008 wclass = kHelpWindowClass ; // has no border
1009 else
1010 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
1011 //attr |= kWindowNoShadowAttribute; // turn off the shadow Should we??
1012 group = GetWindowGroupOfClass( // float above other windows
1013 kFloatingWindowClass) ;
1014 }
1015 else if ( HasFlag( wxCAPTION ) )
1016 {
1017 wclass = kDocumentWindowClass ;
1018 attr |= kWindowInWindowMenuAttribute ;
1019 }
1020 else if ( HasFlag( wxFRAME_DRAWER ) )
1021 {
1022 wclass = kDrawerWindowClass;
1023 }
1024 else
1025 {
1026 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1027 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
1028 {
1029 wclass = kDocumentWindowClass ;
1030 }
1031 else if ( HasFlag( wxNO_BORDER ) )
1032 {
1033 wclass = kSimpleWindowClass ;
1034 }
1035 else
1036 {
1037 wclass = kPlainWindowClass ;
1038 }
1039 }
1040
1041 if ( wclass != kPlainWindowClass )
1042 {
1043 if ( HasFlag( wxMINIMIZE_BOX ) )
1044 attr |= kWindowCollapseBoxAttribute ;
1045
1046 if ( HasFlag( wxMAXIMIZE_BOX ) )
1047 attr |= kWindowFullZoomAttribute ;
1048
1049 if ( HasFlag( wxRESIZE_BORDER ) )
1050 attr |= kWindowResizableAttribute ;
1051
1052 if ( HasFlag( wxCLOSE_BOX) )
1053 attr |= kWindowCloseBoxAttribute ;
1054 }
1055 attr |= kWindowLiveResizeAttribute;
1056
1057 if ( HasFlag(wxSTAY_ON_TOP) )
1058 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1059
1060 if ( HasFlag( wxFRAME_FLOAT_ON_PARENT ) )
1061 group = GetWindowGroupOfClass(kFloatingWindowClass) ;
1062
1063 if ( group == NULL && parent != NULL )
1064 {
1065 WindowRef parenttlw = (WindowRef) parent->MacGetTopLevelWindowRef();
1066 if( parenttlw )
1067 group = GetWindowGroupParent( GetWindowGroup( parenttlw ) );
1068 }
1069
1070 attr |= kWindowCompositingAttribute;
1071#if 0 // wxMAC_USE_CORE_GRAPHICS ; TODO : decide on overall handling of high dpi screens (pixel vs userscale)
1072 attr |= kWindowFrameworkScaledAttribute;
1073#endif
1074
1075 if ( HasFlag(wxFRAME_SHAPED) )
1076 {
1077 WindowDefSpec customWindowDefSpec;
1078 customWindowDefSpec.defType = kWindowDefProcPtr;
1079 customWindowDefSpec.u.defProc =
1080#ifdef __LP64__
1081 (WindowDefUPP) wxShapedMacWindowDef;
1082#else
1083 NewWindowDefUPP(wxShapedMacWindowDef);
1084#endif
1085 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
1086 attr, &theBoundsRect,
1087 (WindowRef*) &m_macWindow);
1088 }
1089 else
1090 {
1091 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
1092 }
1093
1094 if ( err == noErr && m_macWindow != NULL && group != NULL )
1095 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1096
1097 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
1098
1099 // setup a separate group for each window, so that overlays can be handled easily
1100
1101 WindowGroupRef overlaygroup = NULL;
1102 verify_noerr( CreateWindowGroup( kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrHideOnCollapse, &overlaygroup ));
1103 verify_noerr( SetWindowGroupParent( overlaygroup, GetWindowGroup( (WindowRef) m_macWindow )));
1104 verify_noerr( SetWindowGroup( (WindowRef) m_macWindow , overlaygroup ));
1105
1106 if ( activationScopeSet )
1107 {
1108 verify_noerr( SetWindowActivationScope( (WindowRef) m_macWindow , activationScope ));
1109 }
1110
1111 // the create commands are only for content rect,
1112 // so we have to set the size again as structure bounds
1113 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1114
1115 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1116 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
1117
1118 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
1119 // the content view, so we have to retrieve it explicitly
1120 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
1121 m_peer->GetControlRefAddr() ) ;
1122 if ( !m_peer->Ok() )
1123 {
1124 // compatibility mode fallback
1125 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1126 }
1127
1128 // the root control level handler
1129 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
1130
1131 // Causes the inner part of the window not to be metal
1132 // if the style is used before window creation.
1133#if 0 // TARGET_API_MAC_OSX
1134 if ( m_macUsesCompositing && m_macWindow != NULL )
1135 {
1136 if ( GetExtraStyle() & wxFRAME_EX_METAL )
1137 MacSetMetalAppearance( true ) ;
1138 }
1139#endif
1140
1141 if ( m_macWindow != NULL )
1142 {
1143 MacSetUnifiedAppearance( true ) ;
1144 }
1145
1146 HIViewRef growBoxRef = 0 ;
1147 err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
1148 if ( err == noErr && growBoxRef != 0 )
1149 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
1150
1151 // the frame window event handler
1152 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1153 MacInstallTopLevelWindowEventHandler() ;
1154
1155 DoSetWindowVariant( m_windowVariant ) ;
1156
1157 m_macFocus = NULL ;
1158
1159 if ( HasFlag(wxFRAME_SHAPED) )
1160 {
1161 // default shape matches the window size
1162 wxRegion rgn( 0, 0, w, h );
1163 SetShape( rgn );
1164 }
1165
1166 wxWindowCreateEvent event(this);
1167 HandleWindowEvent(event);
1168}
1169
1170// Raise the window to the top of the Z order
1171void wxNonOwnedWindow::Raise()
1172{
1173 ::SelectWindow( (WindowRef)m_macWindow ) ;
1174}
1175
1176// Lower the window to the bottom of the Z order
1177void wxNonOwnedWindow::Lower()
1178{
1179 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
1180}
1181
1182void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
1183{
1184 if (s_macDeactivateWindow)
1185 {
1186 wxLogTrace(TRACE_ACTIVATE,
1187 wxT("Doing delayed deactivation of %p"),
1188 s_macDeactivateWindow);
1189
1190 s_macDeactivateWindow->MacActivate(timestamp, false);
1191 }
1192}
1193
1194void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
1195{
1196 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
1197
1198 if (s_macDeactivateWindow == this)
1199 s_macDeactivateWindow = NULL;
1200
1201 MacDelayedDeactivation(timestamp);
1202}
1203
1204bool wxNonOwnedWindow::Show(bool show)
1205{
1206 if ( !wxWindow::Show(show) )
1207 return false;
1208
1209 bool plainTransition = true;
1210
1211#if wxUSE_SYSTEM_OPTIONS
1212 if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
1213 plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
1214#endif
1215
1216 if (show)
1217 {
1218 if ( plainTransition )
1219 ::ShowWindow( (WindowRef)m_macWindow );
1220 else
1221 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
1222
1223 ::SelectWindow( (WindowRef)m_macWindow ) ;
1224
1225 // because apps expect a size event to occur at this moment
1226 wxSizeEvent event(GetSize() , m_windowId);
1227 event.SetEventObject(this);
1228 HandleWindowEvent(event);
1229 }
1230 else
1231 {
1232 if ( plainTransition )
1233 ::HideWindow( (WindowRef)m_macWindow );
1234 else
1235 ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
1236 }
1237
1238 return true ;
1239}
1240
1241bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect,
1242 unsigned timeout,
1243 wxDirection dir)
1244{
1245 // TODO factor common code
1246 if ( !wxWindow::Show(true) )
1247 return false;
1248
1249 WindowTransitionEffect transition = 0 ;
1250 switch( effect )
1251 {
1252 case wxSHOW_EFFECT_ROLL :
1253 case wxSHOW_EFFECT_SLIDE :
1254 transition = kWindowGenieTransitionEffect;
1255 break;
1256 case wxSHOW_EFFECT_BLEND :
1257 transition = kWindowFadeTransitionEffect;
1258 break;
1259 case wxSHOW_EFFECT_EXPAND :
1260 default :
1261 // having sheets would be fine, but this might lead to a repositioning
1262#if 0
1263 if ( GetParent() )
1264 transition = kWindowSheetTransitionEffect;
1265 else
1266#endif
1267 transition = kWindowZoomTransitionEffect;
1268 break;
1269 }
1270
1271 TransitionWindowOptions options;
1272 options.version = 0;
1273 options.duration = timeout / 1000.0;
1274 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1275 options.userData = 0;
1276
1277 wxSize size = wxGetDisplaySize();
1278 Rect bounds;
1279 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1280 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1281
1282 if ( dir & wxRIGHT )
1283 {
1284 hiBounds.origin.x = size.x;
1285 hiBounds.size.width = 0;
1286 }
1287 if ( dir & wxUP )
1288 {
1289 hiBounds.origin.y = 0;
1290 hiBounds.size.height = 0;
1291 }
1292 if ( dir & wxDOWN )
1293 {
1294 hiBounds.origin.y = size.y;
1295 hiBounds.size.height = 0;
1296 }
1297 if ( dir & wxLEFT )
1298 {
1299 hiBounds.origin.x = 0;
1300 hiBounds.size.width = 0;
1301 }
1302
1303 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowShowTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1304 false, &options );
1305
1306 ::SelectWindow( (WindowRef)m_macWindow ) ;
1307
1308 // because apps expect a size event to occur at this moment
1309 wxSizeEvent event(GetSize() , m_windowId);
1310 event.SetEventObject(this);
1311 HandleWindowEvent(event);
1312
1313 return true;
1314}
1315
1316bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect,
1317 unsigned timeout ,
1318 wxDirection dir )
1319{
1320 if ( !wxWindow::Show(false) )
1321 return false;
1322
1323 WindowTransitionEffect transition = 0 ;
1324 switch( effect )
1325 {
1326 case wxSHOW_EFFECT_ROLL :
1327 case wxSHOW_EFFECT_SLIDE :
1328 transition = kWindowGenieTransitionEffect;
1329 break;
1330 case wxSHOW_EFFECT_BLEND :
1331 transition = kWindowFadeTransitionEffect;
1332 break;
1333 case wxSHOW_EFFECT_EXPAND :
1334 default:
1335#if 0
1336 if ( GetParent() )
1337 transition = kWindowSheetTransitionEffect;
1338 else
1339#endif
1340 transition = kWindowZoomTransitionEffect;
1341 break;
1342 }
1343 TransitionWindowOptions options;
1344 options.version = 0;
1345 options.duration = timeout / 1000.0;
1346 options.window = transition == kWindowSheetTransitionEffect ? (WindowRef) GetParent()->MacGetTopLevelWindowRef() :0;
1347 options.userData = 0;
1348
1349 wxSize size = wxGetDisplaySize();
1350 Rect bounds;
1351 GetWindowBounds( (WindowRef)m_macWindow, kWindowStructureRgn, &bounds );
1352 CGRect hiBounds = CGRectMake( bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top );
1353
1354 if ( dir & wxRIGHT )
1355 {
1356 hiBounds.origin.x = size.x;
1357 hiBounds.size.width = 0;
1358 }
1359 if ( dir & wxUP )
1360 {
1361 hiBounds.origin.y = 0;
1362 hiBounds.size.height = 0;
1363 }
1364 if ( dir & wxDOWN )
1365 {
1366 hiBounds.origin.y = size.y;
1367 hiBounds.size.height = 0;
1368 }
1369 if ( dir & wxLEFT )
1370 {
1371 hiBounds.origin.x = 0;
1372 hiBounds.size.width = 0;
1373 }
1374 ::TransitionWindowWithOptions( (WindowRef)m_macWindow, transition, kWindowHideTransitionAction, transition == kWindowGenieTransitionEffect ? &hiBounds : NULL ,
1375 false, &options );
1376
1377 return true;
1378}
1379
1380bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
1381{
1382 OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, (CGFloat)((alpha)/255.0));
1383 return result == noErr;
1384}
1385
1386
1387bool wxNonOwnedWindow::CanSetTransparent()
1388{
1389 return true;
1390}
1391
1392
1393void wxNonOwnedWindow::SetExtraStyle(long exStyle)
1394{
1395 if ( GetExtraStyle() == exStyle )
1396 return ;
1397
1398 wxWindow::SetExtraStyle( exStyle ) ;
1399
1400 if ( m_macWindow != NULL )
1401 {
1402 bool metal = GetExtraStyle() & wxFRAME_EX_METAL ;
1403
1404 if ( MacGetMetalAppearance() != metal )
1405 {
1406 if ( MacGetUnifiedAppearance() )
1407 MacSetUnifiedAppearance( !metal ) ;
1408
1409 MacSetMetalAppearance( metal ) ;
1410 }
1411 }
1412}
1413
1414bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
1415{
1416 if ( !wxWindow::SetBackgroundStyle(style) )
1417 return false ;
1418
1419 WindowRef windowRef = HIViewGetWindow( (HIViewRef)GetHandle() );
1420
1421 if ( GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT )
1422 {
1423 OSStatus err = HIWindowChangeFeatures( windowRef, 0, kWindowIsOpaque );
1424 verify_noerr( err );
1425 err = ReshapeCustomWindow( windowRef );
1426 verify_noerr( err );
1427 }
1428
1429 return true ;
1430}
1431
1432// TODO: switch to structure bounds -
1433// we are still using coordinates of the content view
1434//
1435void wxNonOwnedWindow::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1436{
1437 Rect content, structure ;
1438
1439 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1440 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1441
1442 left = content.left - structure.left ;
1443 top = content.top - structure.top ;
1444 right = structure.right - content.right ;
1445 bottom = structure.bottom - content.bottom ;
1446}
1447
1448void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
1449{
1450 m_cachedClippedRectValid = false ;
1451 Rect bounds = { y , x , y + height , x + width } ;
1452 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1453 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1454}
1455
1456void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
1457{
1458 Rect bounds ;
1459
1460 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1461
1462 if (x)
1463 *x = bounds.left ;
1464 if (y)
1465 *y = bounds.top ;
1466}
1467
1468void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
1469{
1470 Rect bounds ;
1471
1472 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1473
1474 if (width)
1475 *width = bounds.right - bounds.left ;
1476 if (height)
1477 *height = bounds.bottom - bounds.top ;
1478}
1479
1480void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
1481{
1482 Rect bounds ;
1483
1484 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1485
1486 if (width)
1487 *width = bounds.right - bounds.left ;
1488 if (height)
1489 *height = bounds.bottom - bounds.top ;
1490}
1491
1492void wxNonOwnedWindow::MacSetMetalAppearance( bool set )
1493{
1494 if ( MacGetUnifiedAppearance() )
1495 MacSetUnifiedAppearance( false ) ;
1496
1497 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
1498 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1499}
1500
1501bool wxNonOwnedWindow::MacGetMetalAppearance() const
1502{
1503 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1504}
1505
1506void wxNonOwnedWindow::MacSetUnifiedAppearance( bool set )
1507{
1508 if ( MacGetMetalAppearance() )
1509 MacSetMetalAppearance( false ) ;
1510
1511 MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
1512 set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
1513
1514 // For some reason, Tiger uses white as the background color for this appearance,
1515 // while most apps using it use the typical striped background. Restore that behavior
1516 // for wx.
1517 // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
1518 // though)
1519 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ) ;
1520}
1521
1522bool wxNonOwnedWindow::MacGetUnifiedAppearance() const
1523{
1524 return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
1525}
1526
1527void wxNonOwnedWindow::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
1528{
1529 ChangeWindowAttributes( (WindowRef)m_macWindow, attributesToSet, attributesToClear ) ;
1530}
1531
1532wxUint32 wxNonOwnedWindow::MacGetWindowAttributes() const
1533{
1534 UInt32 attr = 0 ;
1535 GetWindowAttributes( (WindowRef) m_macWindow, &attr ) ;
1536
1537 return attr ;
1538}
1539
1540void wxNonOwnedWindow::MacPerformUpdates()
1541{
1542 // for composited windows this also triggers a redraw of all
1543 // invalid views in the window
1544 HIWindowFlush((WindowRef) m_macWindow) ;
1545}
1546
1547// ---------------------------------------------------------------------------
1548// Shape implementation
1549// ---------------------------------------------------------------------------
1550
1551
1552bool wxNonOwnedWindow::SetShape(const wxRegion& region)
1553{
1554 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1555 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1556
1557 // The empty region signifies that the shape
1558 // should be removed from the window.
1559 if ( region.IsEmpty() )
1560 {
1561 wxSize sz = GetClientSize();
1562 wxRegion rgn(0, 0, sz.x, sz.y);
1563 if ( rgn.IsEmpty() )
1564 return false ;
1565 else
1566 return SetShape(rgn);
1567 }
1568
1569 // Make a copy of the region
1570 RgnHandle shapeRegion = NewRgn();
1571 HIShapeGetAsQDRgn( region.GetWXHRGN(), shapeRegion );
1572
1573 // Dispose of any shape region we may already have
1574 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1575 if ( oldRgn )
1576 DisposeRgn(oldRgn);
1577
1578 // Save the region so we can use it later
1579 SetWRefCon((WindowRef)MacGetWindowRef(), (URefCon)shapeRegion);
1580
1581 // inform the window manager that the window has changed shape
1582 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1583
1584 return true;
1585}
1586
1587// ---------------------------------------------------------------------------
1588// Support functions for shaped windows, based on Apple's CustomWindow sample at
1589// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1590// ---------------------------------------------------------------------------
1591
1592static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1593{
1594 GetWindowPortBounds(window, inRect);
1595 Point pt = { inRect->top ,inRect->left };
1596 wxMacLocalToGlobal( window, &pt ) ;
1597 inRect->bottom += pt.v - inRect->top;
1598 inRect->right += pt.h - inRect->left;
1599 inRect->top = pt.v;
1600 inRect->left = pt.h;
1601}
1602
1603static SInt32 wxShapedMacWindowGetFeatures(WindowRef WXUNUSED(window), SInt32 param)
1604{
1605 /*------------------------------------------------------
1606 Define which options your custom window supports.
1607 --------------------------------------------------------*/
1608 //just enable everything for our demo
1609 *(OptionBits*)param =
1610 //kWindowCanGrow |
1611 //kWindowCanZoom |
1612 kWindowCanCollapse |
1613 //kWindowCanGetWindowRegion |
1614 //kWindowHasTitleBar |
1615 //kWindowSupportsDragHilite |
1616 kWindowCanDrawInCurrentPort |
1617 //kWindowCanMeasureTitle |
1618 kWindowWantsDisposeAtProcessDeath |
1619 kWindowSupportsGetGrowImageRegion |
1620 kWindowDefSupportsColorGrafPort;
1621
1622 return 1;
1623}
1624
1625// The content region is left as a rectangle matching the window size, this is
1626// so the origin in the paint event, and etc. still matches what the
1627// programmer expects.
1628static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1629{
1630 SetEmptyRgn(rgn);
1631 wxNonOwnedWindow* win = wxFindWinFromMacWindow(window);
1632 if (win)
1633 {
1634 Rect r ;
1635 wxShapedMacWindowGetPos( window, &r ) ;
1636 RectRgn( rgn , &r ) ;
1637 }
1638}
1639
1640// The structure region is set to the shape given to the SetShape method.
1641static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1642{
1643 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1644
1645 SetEmptyRgn(rgn);
1646 if (cachedRegion)
1647 {
1648 Rect windowRect;
1649 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1650 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
1651 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1652 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1653 }
1654}
1655
1656static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1657{
1658 GetWindowRegionPtr rgnRec = (GetWindowRegionPtr)param;
1659
1660 if (rgnRec == NULL)
1661 return paramErr;
1662
1663 switch (rgnRec->regionCode)
1664 {
1665 case kWindowStructureRgn:
1666 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1667 break;
1668
1669 case kWindowContentRgn:
1670 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1671 break;
1672
1673 default:
1674 SetEmptyRgn(rgnRec->winRgn);
1675 break;
1676 }
1677
1678 return noErr;
1679}
1680
1681// Determine the region of the window which was hit
1682//
1683static SInt32 wxShapedMacWindowHitTest(WindowRef window, SInt32 param)
1684{
1685 Point hitPoint;
1686 static RgnHandle tempRgn = NULL;
1687
1688 if (tempRgn == NULL)
1689 tempRgn = NewRgn();
1690
1691 // get the point clicked
1692 SetPt( &hitPoint, LoWord(param), HiWord(param) );
1693
1694 // Mac OS 8.5 or later
1695 wxShapedMacWindowStructureRegion(window, tempRgn);
1696 if (PtInRgn( hitPoint, tempRgn )) //in window content region?
1697 return wInContent;
1698
1699 // no significant area was hit
1700 return wNoHit;
1701}
1702
1703static pascal long wxShapedMacWindowDef(short WXUNUSED(varCode), WindowRef window, SInt16 message, SInt32 param)
1704{
1705 switch (message)
1706 {
1707 case kWindowMsgHitTest:
1708 return wxShapedMacWindowHitTest(window, param);
1709
1710 case kWindowMsgGetFeatures:
1711 return wxShapedMacWindowGetFeatures(window, param);
1712
1713 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1714 case kWindowMsgGetRegion:
1715 return wxShapedMacWindowGetRegion(window, param);
1716
1717 default:
1718 break;
1719 }
1720
1721 return 0;
1722}
1723
1724