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