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