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