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