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