]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
all databrowser implementation
[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 wxTopLevelWindow* tlw = (wxTopLevelWindow*) data ;
161
162 wxWindow* focus = wxWindow::FindFocus() ;
163 if ( focus == NULL )
164 return result ;
165
166 char charCode ;
167 UInt32 keyCode ;
168 UInt32 modifiers ;
169 Point point ;
170 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
171
172 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
173 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
174 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
175 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
176 sizeof( Point ), NULL, &point );
177
178 UInt32 message = (keyCode << 8) + charCode;
179 switch( GetEventKind( event ) )
180 {
181 case kEventRawKeyRepeat :
182 case kEventRawKeyDown :
183 {
184 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
185 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
186 wxTheApp->MacSetCurrentEvent( event , handler ) ;
187 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
188 focus , message , modifiers , when , point.h , point.v ) )
189 {
190 result = noErr ;
191 }
192 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
193 }
194 break ;
195 case kEventRawKeyUp :
196 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
197 focus , message , modifiers , when , point.h , point.v ) )
198 {
199 result = noErr ;
200 }
201 break ;
202 case kEventRawKeyModifiersChanged :
203 {
204 wxKeyEvent event(wxEVT_KEY_DOWN);
205
206 event.m_shiftDown = modifiers & shiftKey;
207 event.m_controlDown = modifiers & controlKey;
208 event.m_altDown = modifiers & optionKey;
209 event.m_metaDown = modifiers & cmdKey;
210
211 event.m_x = point.h;
212 event.m_y = point.v;
213 event.m_timeStamp = when;
214 wxWindow* focus = wxWindow::FindFocus() ;
215 event.SetEventObject(focus);
216
217 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
218 {
219 event.m_keyCode = WXK_CONTROL ;
220 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
221 focus->GetEventHandler()->ProcessEvent( event ) ;
222 }
223 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
224 {
225 event.m_keyCode = WXK_SHIFT ;
226 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
227 focus->GetEventHandler()->ProcessEvent( event ) ;
228 }
229 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
230 {
231 event.m_keyCode = WXK_ALT ;
232 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
233 focus->GetEventHandler()->ProcessEvent( event ) ;
234 }
235 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & cmdKey )
236 {
237 event.m_keyCode = WXK_COMMAND ;
238 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
239 focus->GetEventHandler()->ProcessEvent( event ) ;
240 }
241 wxTheApp->s_lastModifiers = modifiers ;
242 }
243 break ;
244 }
245
246 return result ;
247 }
248
249 // we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
250 // for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
251 // mouse down at all
252
253 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
254
255 wxWindow* g_MacLastWindow = NULL ;
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 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
282 // this button
283 if ( button != 0 && cEvent.GetKind() != kEventMouseUp )
284 {
285 switch( button )
286 {
287 case kEventMouseButtonPrimary :
288 wxevent.m_leftDown = true ;
289 break ;
290 case kEventMouseButtonSecondary :
291 wxevent.m_rightDown = true ;
292 break ;
293 case kEventMouseButtonTertiary :
294 wxevent.m_middleDown = true ;
295 break ;
296 }
297 }
298 // determinate the correct click button
299 if ( button == kEventMouseButtonSecondary )
300 {
301 if (cEvent.GetKind() == kEventMouseDown )
302 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
303 else if ( cEvent.GetKind() == kEventMouseUp )
304 wxevent.SetEventType(wxEVT_RIGHT_UP ) ;
305 }
306 else if ( button == kEventMouseButtonTertiary )
307 {
308 if (cEvent.GetKind() == kEventMouseDown )
309 wxevent.SetEventType(clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
310 else if ( cEvent.GetKind() == kEventMouseUp )
311 wxevent.SetEventType(wxEVT_MIDDLE_UP ) ;
312 }
313 else
314 {
315 if (cEvent.GetKind() == kEventMouseDown )
316 wxevent.SetEventType(clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
317 else if ( cEvent.GetKind() == kEventMouseUp )
318 wxevent.SetEventType(wxEVT_LEFT_UP ) ;
319 else if ( cEvent.GetKind() == kEventMouseWheelMoved )
320 {
321 wxevent.SetEventType(wxEVT_MOUSEWHEEL ) ;
322
323 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
324 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeLongInteger) ;
325
326 wxevent.m_wheelRotation = delta;
327 wxevent.m_wheelDelta = 1;
328 wxevent.m_linesPerAction = 1;
329 }
330 else
331 wxevent.SetEventType(wxEVT_MOTION ) ;
332 }
333 }
334
335 ControlRef wxMacFindSubControl( Point location , ControlRef superControl , ControlPartCode *outPart )
336 {
337 if ( superControl )
338 {
339 UInt16 childrenCount = 0 ;
340 OSStatus err = CountSubControls( superControl , &childrenCount ) ;
341 if ( err == errControlIsNotEmbedder )
342 return NULL ;
343 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
344
345 for ( UInt16 i = childrenCount ; i >=1 ; --i )
346 {
347 ControlHandle sibling ;
348 err = GetIndexedSubControl( superControl , i , & sibling ) ;
349 if ( err == errControlIsNotEmbedder )
350 return NULL ;
351
352 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
353 if ( IsControlVisible( sibling ) )
354 {
355 Rect r ;
356 GetControlBounds( sibling , &r ) ;
357 if ( MacPtInRect( location , &r ) )
358 {
359 ControlHandle child = wxMacFindSubControl( location , sibling , outPart ) ;
360 if ( child )
361 return child ;
362 else
363 {
364 *outPart = TestControl( sibling , location ) ;
365 return sibling ;
366 }
367 }
368 }
369 }
370 }
371 return NULL ;
372 }
373
374 ControlRef wxMacFindControlUnderMouse( Point location , WindowRef window , ControlPartCode *outPart )
375 {
376 #if TARGET_API_MAC_OSX
377 return FindControlUnderMouse( location , window , outPart ) ;
378 #else
379 ControlRef rootControl = NULL ;
380 verify_noerr( GetRootControl( window , &rootControl ) ) ;
381 return wxMacFindSubControl( location , rootControl , outPart ) ;
382 #endif
383 }
384 pascal OSStatus wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
385 {
386
387 OSStatus result = eventNotHandledErr ;
388
389 wxMacCarbonEvent cEvent( event ) ;
390
391 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
392 Point windowMouseLocation = screenMouseLocation ;
393
394 WindowRef window ;
395 short windowPart = ::FindWindow(screenMouseLocation, &window);
396
397 wxWindow* currentMouseWindow = NULL ;
398 if ( window )
399 {
400 // calculate window relative coordinates
401 GrafPtr port;
402 ::GetPort( &port ) ;
403 ::SetPort( UMAGetWindowPort(window ) ) ;
404 ::GlobalToLocal( &windowMouseLocation ) ;
405 ::SetPort( port ) ;
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 if ( cEvent.GetKind() == kEventMouseUp && wxTheApp->s_captureWindow )
502 {
503 wxTheApp->s_captureWindow = NULL ;
504 // update cursor ?
505 }
506 } // else if ( currentMouseWindow )
507 return result ;
508 }
509
510 static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
511 {
512 OSStatus result = eventNotHandledErr ;
513
514 wxMacCarbonEvent cEvent( event ) ;
515
516 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
517 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
518
519 switch( GetEventKind( event ) )
520 {
521 case kEventWindowActivated :
522 {
523 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
524 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
525 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
526 wxevent.SetEventObject(toplevelWindow);
527 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
528 // we still sending an eventNotHandledErr in order to allow for default processing
529 break ;
530 }
531 case kEventWindowDeactivated :
532 {
533 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
534 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
535 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
536 wxevent.SetEventObject(toplevelWindow);
537 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
538 // we still sending an eventNotHandledErr in order to allow for default processing
539 break ;
540 }
541 case kEventWindowShown :
542 toplevelWindow->Refresh() ;
543 result = noErr ;
544 break ;
545 case kEventWindowClose :
546 toplevelWindow->Close() ;
547 result = noErr ;
548 break ;
549 case kEventWindowBoundsChanged :
550 {
551 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
552 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
553 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
554 if ( attributes & kWindowBoundsChangeSizeChanged )
555 {
556 // according to the other ports we handle this within the OS level
557 // resize event, not within a wxSizeEvent
558 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
559 if ( frame )
560 {
561 #if wxUSE_STATUSBAR
562 frame->PositionStatusBar();
563 #endif
564 #if wxUSE_TOOLBAR
565 frame->PositionToolBar();
566 #endif
567 }
568
569 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
570 event.SetEventObject( toplevelWindow ) ;
571
572 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
573 }
574 if ( attributes & kWindowBoundsChangeOriginChanged )
575 {
576 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
577 event.SetEventObject( toplevelWindow ) ;
578 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
579 }
580 result = noErr ;
581 break ;
582 }
583 case kEventWindowBoundsChanging :
584 {
585 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
586 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
587
588 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
589 {
590 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
591 int left , top , right , bottom ;
592 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
593 wxRect r( newRect.left - left , newRect.top - top ,
594 newRect.right - newRect.left + left + right , newRect.bottom - newRect.top + top + bottom ) ;
595 // this is a EVT_SIZING not a EVT_SIZE type !
596 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
597 wxevent.SetEventObject( toplevelWindow ) ;
598 wxRect adjustR = r ;
599 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
600 adjustR = wxevent.GetRect() ;
601
602 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
603 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
604 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
605 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
606 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
607 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
608 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
609 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
610 Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
611 if ( !EqualRect( &newRect , &adjustedRect ) )
612 cEvent.SetParameter( kEventParamCurrentBounds , &adjustedRect ) ;
613 }
614
615 result = noErr ;
616 break ;
617 }
618 default :
619 break ;
620 }
621 return result ;
622 }
623
624 pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
625 {
626 OSStatus result = eventNotHandledErr ;
627
628 switch ( GetEventClass( event ) )
629 {
630 case kEventClassKeyboard :
631 result = KeyboardEventHandler( handler, event , data ) ;
632 break ;
633 case kEventClassTextInput :
634 result = TextInputEventHandler( handler, event , data ) ;
635 break ;
636 case kEventClassWindow :
637 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
638 break ;
639 case kEventClassMouse :
640 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
641 break ;
642 default :
643 break ;
644 }
645 return result ;
646 }
647
648 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
649
650 // ---------------------------------------------------------------------------
651 // wxWindowMac utility functions
652 // ---------------------------------------------------------------------------
653
654 // Find an item given the Macintosh Window Reference
655
656 wxList wxWinMacWindowList(wxKEY_INTEGER);
657 wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
658 {
659 wxNode *node = wxWinMacWindowList.Find((long)inWindowRef);
660 if (!node)
661 return NULL;
662 return (wxTopLevelWindowMac *)node->GetData();
663 }
664
665 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
666 void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
667 {
668 // adding NULL WindowRef is (first) surely a result of an error and
669 // (secondly) breaks menu command processing
670 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
671
672 if ( !wxWinMacWindowList.Find((long)inWindowRef) )
673 wxWinMacWindowList.Append((long)inWindowRef, win);
674 }
675
676 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
677 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
678 {
679 wxWinMacWindowList.DeleteObject(win);
680 }
681
682
683 // ----------------------------------------------------------------------------
684 // wxTopLevelWindowMac creation
685 // ----------------------------------------------------------------------------
686
687 wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
688
689 void wxTopLevelWindowMac::Init()
690 {
691 m_iconized =
692 m_maximizeOnShow = FALSE;
693 m_macWindow = NULL ;
694 #if TARGET_API_MAC_OSX
695 m_macUsesCompositing = TRUE;
696 #else
697 m_macUsesCompositing = FALSE;
698 #endif
699 m_macEventHandler = NULL ;
700 }
701
702 class wxMacDeferredWindowDeleter : public wxObject
703 {
704 public :
705 wxMacDeferredWindowDeleter( WindowRef windowRef )
706 {
707 m_macWindow = windowRef ;
708 }
709 virtual ~wxMacDeferredWindowDeleter()
710 {
711 UMADisposeWindow( (WindowRef) m_macWindow ) ;
712 }
713 protected :
714 WindowRef m_macWindow ;
715 } ;
716
717 bool wxTopLevelWindowMac::Create(wxWindow *parent,
718 wxWindowID id,
719 const wxString& title,
720 const wxPoint& pos,
721 const wxSize& size,
722 long style,
723 const wxString& name)
724 {
725 // init our fields
726 Init();
727
728 style = style & ~wxFRAME_SHAPED ;
729
730 m_windowStyle = style;
731
732 SetName(name);
733
734 m_windowId = id == -1 ? NewControlId() : id;
735
736 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
737
738 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
739
740 wxTopLevelWindows.Append(this);
741
742 if ( parent )
743 parent->AddChild(this);
744
745 return TRUE;
746 }
747
748 wxTopLevelWindowMac::~wxTopLevelWindowMac()
749 {
750 if ( m_macWindow )
751 {
752 wxToolTip::NotifyWindowDelete(m_macWindow) ;
753 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
754 }
755
756 if ( m_macEventHandler )
757 {
758 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
759 m_macEventHandler = NULL ;
760 }
761
762 wxRemoveMacWindowAssociation( this ) ;
763
764 if ( wxModelessWindows.Find(this) )
765 wxModelessWindows.DeleteObject(this);
766 }
767
768
769 // ----------------------------------------------------------------------------
770 // wxTopLevelWindowMac maximize/minimize
771 // ----------------------------------------------------------------------------
772
773 void wxTopLevelWindowMac::Maximize(bool maximize)
774 {
775 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
776 wxMacWindowClipper clip (this);
777 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
778 /*
779 Rect r ;
780 GDHandle device = NULL ;
781 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
782 &device , NULL ) ;
783 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
784
785 Rect tempRect ;
786 GrafPtr port ;
787 GetPort( &port ) ;
788 Point pt = { 0, 0 } ;
789 SetPortWindowPort((WindowRef)m_macWindow) ;
790 LocalToGlobal( &pt ) ;
791 SetPort( port ) ;
792
793 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
794 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
795 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
796 */
797 }
798
799 bool wxTopLevelWindowMac::IsMaximized() const
800 {
801 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
802 }
803
804 void wxTopLevelWindowMac::Iconize(bool iconize)
805 {
806 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
807 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
808 }
809
810 bool wxTopLevelWindowMac::IsIconized() const
811 {
812 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
813 }
814
815 void wxTopLevelWindowMac::Restore()
816 {
817 // not available on mac
818 }
819
820 // ----------------------------------------------------------------------------
821 // wxTopLevelWindowMac misc
822 // ----------------------------------------------------------------------------
823
824 wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
825 {
826 return wxPoint(0,0) ;
827 }
828
829 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
830 {
831 // this sets m_icon
832 wxTopLevelWindowBase::SetIcon(icon);
833 }
834
835 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
836 {
837 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
838
839 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
840 {
841 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
842 }
843 }
844
845 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
846 {
847 if ( m_macEventHandler != NULL )
848 {
849 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
850 }
851 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
852 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
853 }
854
855 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
856 const wxPoint& pos,
857 const wxSize& size,
858 long style,
859 const wxString& name )
860 {
861 OSStatus err = noErr ;
862 SetName(name);
863 m_windowStyle = style;
864 m_isShown = FALSE;
865
866 // create frame.
867
868 Rect theBoundsRect;
869
870 int x = (int)pos.x;
871 int y = (int)pos.y;
872 if ( y < 50 )
873 y = 50 ;
874 if ( x < 20 )
875 x = 20 ;
876
877 int w = WidthDefault(size.x);
878 int h = HeightDefault(size.y);
879
880 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
881
882 // translate the window attributes in the appropriate window class and attributes
883
884 WindowClass wclass = 0;
885 WindowAttributes attr = kWindowNoAttributes ;
886
887 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
888 {
889 if (
890 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
891 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
892 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
893 )
894 {
895 wclass = kFloatingWindowClass ;
896 if ( HasFlag(wxTINY_CAPTION_VERT) )
897 {
898 attr |= kWindowSideTitlebarAttribute ;
899 }
900 }
901 else
902 {
903 wclass = kPlainWindowClass ;
904 }
905 }
906 else if ( HasFlag( wxCAPTION ) )
907 {
908 wclass = kDocumentWindowClass ;
909 }
910 else
911 {
912 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
913 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
914 {
915 wclass = kDocumentWindowClass ;
916 }
917 else
918 {
919 wclass = kPlainWindowClass ;
920 }
921 }
922
923 if ( HasFlag( wxMINIMIZE_BOX ) )
924 {
925 attr |= kWindowCollapseBoxAttribute ;
926 }
927 if ( HasFlag( wxMAXIMIZE_BOX ) )
928 {
929 attr |= kWindowFullZoomAttribute ;
930 }
931 if ( HasFlag( wxRESIZE_BORDER ) )
932 {
933 attr |= kWindowResizableAttribute ;
934 }
935 if ( HasFlag( wxCLOSE_BOX) )
936 {
937 attr |= kWindowCloseBoxAttribute ;
938 }
939
940 if (UMAGetSystemVersion() >= 0x1000)
941 {
942 // turn on live resizing (OS X only)
943 attr |= kWindowLiveResizeAttribute;
944 }
945
946 if (HasFlag(wxSTAY_ON_TOP))
947 wclass = kUtilityWindowClass;
948
949 #if TARGET_API_MAC_OSX
950 attr |= kWindowCompositingAttribute;
951 #endif
952
953 if ( HasFlag(wxFRAME_SHAPED) )
954 {
955 WindowDefSpec customWindowDefSpec;
956 customWindowDefSpec.defType = kWindowDefProcPtr;
957 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
958
959 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
960 attr, &theBoundsRect,
961 (WindowRef*) &m_macWindow);
962 }
963 else
964 {
965 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
966 }
967
968 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
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 SetPort((GrafPtr) GetWindowPort(window));
1208 LocalToGlobal(&pt);
1209 inRect->top = pt.v;
1210 inRect->left = pt.h;
1211 inRect->bottom += pt.v;
1212 inRect->right += pt.h;
1213 }
1214
1215
1216 static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1217 {
1218 /*------------------------------------------------------
1219 Define which options your custom window supports.
1220 --------------------------------------------------------*/
1221 //just enable everything for our demo
1222 *(OptionBits*)param=//kWindowCanGrow|
1223 //kWindowCanZoom|
1224 //kWindowCanCollapse|
1225 //kWindowCanGetWindowRegion|
1226 //kWindowHasTitleBar|
1227 //kWindowSupportsDragHilite|
1228 kWindowCanDrawInCurrentPort|
1229 //kWindowCanMeasureTitle|
1230 kWindowWantsDisposeAtProcessDeath|
1231 kWindowSupportsSetGrowImageRegion|
1232 kWindowDefSupportsColorGrafPort;
1233 return 1;
1234 }
1235
1236 // The content region is left as a rectangle matching the window size, this is
1237 // so the origin in the paint event, and etc. still matches what the
1238 // programmer expects.
1239 static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1240 {
1241 SetEmptyRgn(rgn);
1242 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1243 if (win)
1244 {
1245 Rect r ;
1246 wxShapedMacWindowGetPos(window, &r ) ;
1247 RectRgn( rgn , &r ) ;
1248 }
1249 }
1250
1251 // The structure region is set to the shape given to the SetShape method.
1252 static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1253 {
1254 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1255
1256 SetEmptyRgn(rgn);
1257 if (cachedRegion)
1258 {
1259 Rect windowRect;
1260 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1261 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1262 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1263 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1264 }
1265 }
1266
1267
1268
1269 static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1270 {
1271 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1272
1273 switch(rgnRec->regionCode)
1274 {
1275 case kWindowStructureRgn:
1276 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1277 break;
1278 case kWindowContentRgn:
1279 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1280 break;
1281 default:
1282 SetEmptyRgn(rgnRec->winRgn);
1283 } //switch
1284
1285 return noErr;
1286 }
1287
1288
1289 static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1290 {
1291 /*------------------------------------------------------
1292 Determine the region of the window which was hit
1293 --------------------------------------------------------*/
1294 Point hitPoint;
1295 static RgnHandle tempRgn=nil;
1296
1297 if(!tempRgn)
1298 tempRgn=NewRgn();
1299
1300 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1301
1302 //Mac OS 8.5 or later
1303 wxShapedMacWindowStructureRegion(window, tempRgn);
1304 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1305 return wInContent;
1306
1307 return wNoHit;//no significant area was hit.
1308 }
1309
1310
1311 static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1312 {
1313 switch(message)
1314 {
1315 case kWindowMsgHitTest:
1316 return wxShapedMacWindowHitTest(window,param);
1317
1318 case kWindowMsgGetFeatures:
1319 return wxShapedMacWindowGetFeatures(window,param);
1320
1321 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1322 case kWindowMsgGetRegion:
1323 return wxShapedMacWindowGetRegion(window,param);
1324 }
1325
1326 return 0;
1327 }
1328
1329 // ---------------------------------------------------------------------------
1330