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