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