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