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