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