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