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