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