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