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