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