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