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