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