]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
'[1219035] cleanup: miscellaneous' and minor source cleaning.
[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
3d1a4878 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a15eb0a5
SC
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"
2d17efa9 39 #include "wx/strconv.h"
179e085f 40 #include "wx/control.h"
a15eb0a5
SC
41#endif //WX_PRECOMP
42
5f0b2f22 43#include "wx/mac/uma.h"
422644a3 44#include "wx/mac/aga.h"
7c091673 45#include "wx/app.h"
5f0b2f22 46#include "wx/tooltip.h"
a07c1212 47#include "wx/dnd.h"
1f90939c
SC
48#if wxUSE_SYSTEM_OPTIONS
49 #include "wx/sysopt.h"
50#endif
5f0b2f22 51
7f0c3a63 52#include <ToolUtils.h>
dd49c691 53
eab19a7c
RN
54//For targeting OSX
55#include "wx/mac/private.h"
56
a15eb0a5
SC
57// ----------------------------------------------------------------------------
58// globals
59// ----------------------------------------------------------------------------
60
61// list of all frames and modeless dialogs
32b5be3d
RR
62wxWindowList wxModelessWindows;
63
6a7e6411
RD
64static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
65
a15eb0a5
SC
66// ============================================================================
67// wxTopLevelWindowMac implementation
68// ============================================================================
69
facd6764
SC
70BEGIN_EVENT_TABLE(wxTopLevelWindowMac, wxTopLevelWindowBase)
71END_EVENT_TABLE()
72
73
851b3a88
SC
74// ---------------------------------------------------------------------------
75// Carbon Events
76// ---------------------------------------------------------------------------
77
851b3a88
SC
78extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
79
1542ea39 80static const EventTypeSpec eventList[] =
851b3a88 81{
facd6764 82 // TODO remove control related event like key and mouse (except for WindowLeave events)
949cb163 83#if 1
851b3a88 84 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
c5c9378c 85
e40298d5 86 { kEventClassKeyboard, kEventRawKeyDown } ,
c5c9378c
SC
87 { kEventClassKeyboard, kEventRawKeyRepeat } ,
88 { kEventClassKeyboard, kEventRawKeyUp } ,
89 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
facd6764 90#endif
c5c9378c 91
16a76184 92 { kEventClassWindow , kEventWindowShown } ,
851b3a88
SC
93 { kEventClassWindow , kEventWindowActivated } ,
94 { kEventClassWindow , kEventWindowDeactivated } ,
95 { kEventClassWindow , kEventWindowBoundsChanging } ,
96 { kEventClassWindow , kEventWindowBoundsChanged } ,
97 { kEventClassWindow , kEventWindowClose } ,
98
facd6764
SC
99 // we have to catch these events on the toplevel window level, as controls don't get the
100 // raw mouse events anymore
902725ee 101
851b3a88
SC
102 { kEventClassMouse , kEventMouseDown } ,
103 { kEventClassMouse , kEventMouseUp } ,
de127c69 104 { kEventClassMouse , kEventMouseWheelMoved } ,
851b3a88
SC
105 { kEventClassMouse , kEventMouseMoved } ,
106 { kEventClassMouse , kEventMouseDragged } ,
851b3a88
SC
107} ;
108
109static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
110{
111 OSStatus result = eventNotHandledErr ;
851b3a88 112
c5c9378c 113 wxWindow* focus = wxWindow::FindFocus() ;
19fc48c6 114 unsigned char charCode ;
1542ea39 115 UInt32 keyCode ;
c5c9378c 116 UInt32 modifiers ;
e40298d5 117 Point point ;
c5c9378c
SC
118
119 EventRef rawEvent ;
1542ea39 120
c5c9378c 121 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1542ea39 122
e40298d5
JS
123 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
124 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
125 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
126 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL,
127 sizeof( Point ), NULL, &point );
128
e40298d5
JS
129 switch ( GetEventKind( event ) )
130 {
131 case kEventTextInputUnicodeForKeyEvent :
ace07c80 132 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
5d2e69e8 133 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
ace07c80
SC
134 wxControl* control = wxDynamicCast( focus , wxControl ) ;
135 if ( control )
136 {
facd6764 137 ControlRef macControl = (ControlRef) control->GetHandle() ;
ace07c80
SC
138 if ( macControl )
139 {
140 ::HandleControlKey( macControl , keyCode , charCode , modifiers ) ;
141 result = noErr ;
142 }
143 }
144 /*
145 // this may lead to double events sent to a window in case all handlers have skipped the key down event
77a4354e
VZ
146 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
147 UInt32 message = (keyCode << 8) + charCode;
148
1542ea39 149 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
e40298d5
JS
150 focus , message , modifiers , when , point.h , point.v ) )
151 {
152 result = noErr ;
153 }
ace07c80 154 */
e40298d5
JS
155 break ;
156 }
c5c9378c
SC
157
158 return result ;
159}
160
161static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
162{
163 OSStatus result = eventNotHandledErr ;
b3bd613a
RR
164 // call DoFindFocus instead of FindFocus, because for Composite Windows(like WxGenericListCtrl)
165 // FindFocus does not return the actual focus window,but the enclosing window
166 wxWindow* focus = wxWindow::DoFindFocus();
92a7272f
SC
167 if ( focus == NULL )
168 return result ;
902725ee 169
19fc48c6 170 unsigned char charCode ;
902725ee 171 wxChar uniChar = 0 ;
1542ea39 172 UInt32 keyCode ;
c5c9378c 173 UInt32 modifiers ;
e40298d5
JS
174 Point point ;
175 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
176
2d17efa9
SC
177#if wxUSE_UNICODE
178 UInt32 dataSize = 0 ;
179 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize , NULL ) == noErr )
180 {
181 UniChar buf[2] ;
902725ee 182
2d17efa9 183 UniChar* charBuf = buf ;
902725ee 184
2d17efa9
SC
185 if ( dataSize > 4 )
186 charBuf = new UniChar[ dataSize / sizeof( UniChar) ] ;
187 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
188#if SIZEOF_WCHAR_T == 2
189 uniChar = charBuf[0] ;
190#else
d9d488cf 191 wxMBConvUTF16 converter ;
2d17efa9 192 converter.MB2WC( &uniChar , (const char*)charBuf , 1 ) ;
902725ee 193#endif
2d17efa9
SC
194 if ( dataSize > 4 )
195 delete[] charBuf ;
196 }
197#endif
198
e40298d5
JS
199 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
200 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
201 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
202 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
203 sizeof( Point ), NULL, &point );
902725ee 204
e40298d5
JS
205 UInt32 message = (keyCode << 8) + charCode;
206 switch( GetEventKind( event ) )
207 {
208 case kEventRawKeyRepeat :
209 case kEventRawKeyDown :
e40298d5 210 {
404f1d80
SC
211 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
212 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
213 wxTheApp->MacSetCurrentEvent( event , handler ) ;
902725ee 214 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
2d17efa9 215 focus , message , modifiers , when , point.h , point.v , uniChar ) )
404f1d80
SC
216 {
217 result = noErr ;
218 }
219 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
e40298d5
JS
220 }
221 break ;
222 case kEventRawKeyUp :
902725ee 223 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
2d17efa9 224 focus , message , modifiers , when , point.h , point.v , uniChar ) )
e40298d5
JS
225 {
226 result = noErr ;
227 }
228 break ;
229 case kEventRawKeyModifiersChanged :
230 {
231 wxKeyEvent event(wxEVT_KEY_DOWN);
232
233 event.m_shiftDown = modifiers & shiftKey;
234 event.m_controlDown = modifiers & controlKey;
235 event.m_altDown = modifiers & optionKey;
236 event.m_metaDown = modifiers & cmdKey;
2d17efa9
SC
237#if wxUSE_UNICODE
238 event.m_uniChar = uniChar ;
239#endif
e40298d5
JS
240 event.m_x = point.h;
241 event.m_y = point.v;
687706f5 242 event.SetTimestamp(when);
e40298d5
JS
243 event.SetEventObject(focus);
244
902725ee 245 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
e40298d5
JS
246 {
247 event.m_keyCode = WXK_CONTROL ;
248 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
249 focus->GetEventHandler()->ProcessEvent( event ) ;
250 }
902725ee 251 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
e40298d5
JS
252 {
253 event.m_keyCode = WXK_SHIFT ;
254 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
255 focus->GetEventHandler()->ProcessEvent( event ) ;
256 }
902725ee 257 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
e40298d5
JS
258 {
259 event.m_keyCode = WXK_ALT ;
260 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
261 focus->GetEventHandler()->ProcessEvent( event ) ;
262 }
902725ee 263 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
b7aec135
SC
264 {
265 event.m_keyCode = WXK_COMMAND ;
266 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
267 focus->GetEventHandler()->ProcessEvent( event ) ;
268 }
2a1f999f 269 wxApp::s_lastModifiers = modifiers ;
e40298d5 270 }
902725ee 271 break ;
e40298d5 272 }
851b3a88
SC
273
274 return result ;
275}
276
facd6764
SC
277// we don't interfere with foreign controls on our toplevel windows, therefore we always give back eventNotHandledErr
278// for windows that we didn't create (like eg Scrollbars in a databrowser) , or for controls where we did not handle the
279// mouse down at all
851b3a88 280
facd6764 281// This handler can also be called from app level where data (ie target window) may be null or a non wx window
1542ea39 282
facd6764 283wxWindow* g_MacLastWindow = NULL ;
a3195b73 284
86a9144f
SC
285static EventMouseButton lastButton = 0 ;
286
facd6764
SC
287static void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent )
288{
289 UInt32 modifiers = cEvent.GetParameter<UInt32>(kEventParamKeyModifiers, typeUInt32) ;
290 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
67245665 291
902725ee 292 // this parameter are not given for all events
facd6764 293 EventMouseButton button = 0 ;
902725ee 294 UInt32 clickCount = 0 ;
facd6764
SC
295 cEvent.GetParameter<EventMouseButton>(kEventParamMouseButton, typeMouseButton , &button) ;
296 cEvent.GetParameter<UInt32>(kEventParamClickCount, typeUInt32 , &clickCount ) ;
297
298 wxevent.m_x = screenMouseLocation.h;
299 wxevent.m_y = screenMouseLocation.v;
300 wxevent.m_shiftDown = modifiers & shiftKey;
301 wxevent.m_controlDown = modifiers & controlKey;
302 wxevent.m_altDown = modifiers & optionKey;
303 wxevent.m_metaDown = modifiers & cmdKey;
304 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
902725ee 305 // a control click is interpreted as a right click
facd6764 306 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
851b3a88 307 {
facd6764
SC
308 button = kEventMouseButtonSecondary ;
309 }
902725ee 310
b343fb9f
SC
311 // otherwise we report double clicks by connecting a left click with a ctrl-left click
312 if ( clickCount > 1 && button != lastButton )
313 clickCount = 1 ;
902725ee 314
86a9144f
SC
315 // we must make sure that our synthetic 'right' button corresponds in
316 // mouse down, moved and mouse up, and does not deliver a right down and left up
902725ee 317
86a9144f
SC
318 if ( cEvent.GetKind() == kEventMouseDown )
319 lastButton = button ;
902725ee 320
4e4e6dce 321 if ( button == 0 )
902725ee 322 lastButton = 0 ;
86a9144f
SC
323 else if ( lastButton )
324 button = lastButton ;
facd6764
SC
325
326 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
327 // this button
328 if ( button != 0 && cEvent.GetKind() != kEventMouseUp )
329 {
330 switch( button )
e40298d5 331 {
facd6764
SC
332 case kEventMouseButtonPrimary :
333 wxevent.m_leftDown = true ;
e40298d5 334 break ;
facd6764
SC
335 case kEventMouseButtonSecondary :
336 wxevent.m_rightDown = true ;
e40298d5 337 break ;
facd6764
SC
338 case kEventMouseButtonTertiary :
339 wxevent.m_middleDown = true ;
e40298d5 340 break ;
facd6764
SC
341 }
342 }
90ff87d7
SC
343 // translate into wx types
344 switch ( cEvent.GetKind() )
facd6764 345 {
90ff87d7
SC
346 case kEventMouseDown :
347 switch( button )
348 {
349 case kEventMouseButtonPrimary :
350 wxevent.SetEventType(clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
351 break ;
352 case kEventMouseButtonSecondary :
353 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
354 break ;
355 case kEventMouseButtonTertiary :
356 wxevent.SetEventType(clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
357 break ;
358 }
359 break ;
360 case kEventMouseUp :
361 switch( button )
362 {
363 case kEventMouseButtonPrimary :
364 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
365 break ;
366 case kEventMouseButtonSecondary :
367 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
368 break ;
369 case kEventMouseButtonTertiary :
370 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
371 break ;
372 }
373 break ;
2cce9b11
JS
374 case kEventMouseWheelMoved :
375 {
facd6764
SC
376 wxevent.SetEventType(wxEVT_MOUSEWHEEL ) ;
377
378 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
379 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeLongInteger) ;
380
381 wxevent.m_wheelRotation = delta;
382 wxevent.m_wheelDelta = 1;
383 wxevent.m_linesPerAction = 1;
90ff87d7 384 break ;
2cce9b11 385 }
90ff87d7 386 default :
facd6764 387 wxevent.SetEventType(wxEVT_MOTION ) ;
90ff87d7 388 break ;
902725ee 389 }
facd6764
SC
390}
391
789ae0cf 392ControlRef wxMacFindSubControl( wxTopLevelWindowMac* toplevelWindow, Point location , ControlRef superControl , ControlPartCode *outPart )
facd6764
SC
393{
394 if ( superControl )
395 {
396 UInt16 childrenCount = 0 ;
902725ee 397 OSStatus err = CountSubControls( superControl , &childrenCount ) ;
facd6764
SC
398 if ( err == errControlIsNotEmbedder )
399 return NULL ;
400 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
902725ee 401
facd6764
SC
402 for ( UInt16 i = childrenCount ; i >=1 ; --i )
403 {
404 ControlHandle sibling ;
405 err = GetIndexedSubControl( superControl , i , & sibling ) ;
406 if ( err == errControlIsNotEmbedder )
407 return NULL ;
902725ee 408
facd6764
SC
409 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
410 if ( IsControlVisible( sibling ) )
411 {
412 Rect r ;
d6fd667b 413 UMAGetControlBoundsInWindowCoords( sibling , &r ) ;
facd6764 414 if ( MacPtInRect( location , &r ) )
de127c69 415 {
789ae0cf 416 ControlHandle child = wxMacFindSubControl( toplevelWindow , location , sibling , outPart ) ;
facd6764
SC
417 if ( child )
418 return child ;
419 else
de127c69 420 {
d6fd667b 421 Point testLocation = location ;
789ae0cf
SC
422
423 if ( toplevelWindow && toplevelWindow->MacUsesCompositing() )
424 {
425 testLocation.h -= r.left ;
426 testLocation.v -= r.top ;
427 }
902725ee 428
d6fd667b 429 *outPart = TestControl( sibling , testLocation ) ;
facd6764 430 return sibling ;
de127c69
GD
431 }
432 }
facd6764 433 }
e40298d5
JS
434 }
435 }
facd6764
SC
436 return NULL ;
437}
1542ea39 438
789ae0cf 439ControlRef wxMacFindControlUnderMouse( wxTopLevelWindowMac* toplevelWindow , Point location , WindowRef window , ControlPartCode *outPart )
facd6764
SC
440{
441#if TARGET_API_MAC_OSX
789ae0cf 442 if ( UMAGetSystemVersion() >= 0x1030 && ( toplevelWindow == 0 || toplevelWindow->MacUsesCompositing() ) )
30e0b1c9
SC
443 return FindControlUnderMouse( location , window , outPart ) ;
444#endif
facd6764
SC
445 ControlRef rootControl = NULL ;
446 verify_noerr( GetRootControl( window , &rootControl ) ) ;
789ae0cf 447 return wxMacFindSubControl( toplevelWindow , location , rootControl , outPart ) ;
30e0b1c9 448
facd6764
SC
449}
450pascal OSStatus wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
451{
789ae0cf 452 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
902725ee 453
facd6764
SC
454 OSStatus result = eventNotHandledErr ;
455
456 wxMacCarbonEvent cEvent( event ) ;
902725ee 457
facd6764
SC
458 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
459 Point windowMouseLocation = screenMouseLocation ;
460
461 WindowRef window ;
462 short windowPart = ::FindWindow(screenMouseLocation, &window);
463
464 wxWindow* currentMouseWindow = NULL ;
d88ffaaa 465 ControlRef control = NULL ;
902725ee 466
facd6764
SC
467 if ( window )
468 {
d6fd667b 469 QDGlobalToLocalPoint( UMAGetWindowPort(window ) , &windowMouseLocation ) ;
facd6764 470
2a1f999f 471 if ( wxApp::s_captureWindow && wxApp::s_captureWindow->MacGetTopLevelWindowRef() == (WXWindow) window && windowPart == inContent )
facd6764 472 {
2a1f999f 473 currentMouseWindow = wxApp::s_captureWindow ;
facd6764
SC
474 }
475 else if ( (IsWindowActive(window) && windowPart == inContent) )
476 {
477 ControlPartCode part ;
789ae0cf 478 control = wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &part ) ;
d88ffaaa 479 // if there is no control below the mouse position, send the event to the toplevel window itself
b19bf058
SC
480 if ( control == 0 )
481 currentMouseWindow = (wxWindow*) data ;
482 else
b16f4bfa 483 {
b19bf058 484 currentMouseWindow = wxFindControlFromMacControl( control ) ;
8198ae50 485 if ( currentMouseWindow == NULL && cEvent.GetKind() == kEventMouseMoved )
b16f4bfa 486 {
179e085f 487#if wxUSE_TOOLBAR
902725ee
WS
488 // for wxToolBar to function we have to send certaint events to it
489 // instead of its children (wxToolBarTools)
b16f4bfa
SC
490 ControlRef parent ;
491 GetSuperControl(control, &parent );
492 wxWindow *wxParent = wxFindControlFromMacControl( parent ) ;
493 if ( wxParent && wxParent->IsKindOf( CLASSINFO( wxToolBar ) ) )
494 currentMouseWindow = wxParent ;
179e085f 495#endif
b16f4bfa
SC
496 }
497 }
902725ee 498 }
facd6764 499 }
902725ee 500
facd6764
SC
501 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
502 SetupMouseEvent( wxevent , cEvent ) ;
503
504 // handle all enter / leave events
902725ee 505
facd6764
SC
506 if ( currentMouseWindow != g_MacLastWindow )
507 {
508 if ( g_MacLastWindow )
509 {
510 wxMouseEvent eventleave(wxevent);
511 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
512 g_MacLastWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
513 eventleave.SetEventObject( g_MacLastWindow ) ;
514
515#if wxUSE_TOOLTIPS
516 wxToolTip::RelayEvent( g_MacLastWindow , eventleave);
517#endif // wxUSE_TOOLTIPS
518 g_MacLastWindow->GetEventHandler()->ProcessEvent(eventleave);
519 }
520 if ( currentMouseWindow )
521 {
522 wxMouseEvent evententer(wxevent);
523 evententer.SetEventType( wxEVT_ENTER_WINDOW );
524 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
525 evententer.SetEventObject( currentMouseWindow ) ;
526#if wxUSE_TOOLTIPS
527 wxToolTip::RelayEvent( currentMouseWindow , evententer);
528#endif // wxUSE_TOOLTIPS
529 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
530 }
531 g_MacLastWindow = currentMouseWindow ;
532 }
902725ee 533
facd6764
SC
534 if ( windowPart == inMenuBar )
535 {
536 // special case menu bar, as we are having a low-level runloop we must do it ourselves
537 if ( cEvent.GetKind() == kEventMouseDown )
538 {
539 ::MenuSelect( screenMouseLocation ) ;
540 result = noErr ;
541 }
542 } // if ( windowPart == inMenuBar )
543 else if ( currentMouseWindow )
544 {
e10e9469 545 wxWindow *currentMouseWindowParent = currentMouseWindow->GetParent();
62ade180
RR
546
547 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
902725ee 548
facd6764
SC
549 wxevent.SetEventObject( currentMouseWindow ) ;
550
facd6764 551 // make tooltips current
902725ee 552
facd6764
SC
553 #if wxUSE_TOOLTIPS
554 if ( wxevent.GetEventType() == wxEVT_MOTION
555 || wxevent.GetEventType() == wxEVT_ENTER_WINDOW
556 || wxevent.GetEventType() == wxEVT_LEAVE_WINDOW )
557 wxToolTip::RelayEvent( currentMouseWindow , wxevent);
902725ee 558 #endif // wxUSE_TOOLTIPS
facd6764 559 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
62ade180 560 {
902725ee 561 if ((currentMouseWindowParent != NULL) &&
62ade180
RR
562 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
563 currentMouseWindow = NULL;
902725ee 564
facd6764 565 result = noErr;
62ade180 566 }
30e0b1c9
SC
567 else
568 {
f90387b4
VZ
569 // if the user code did _not_ handle the event, then perform the
570 // default processing
571 if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
572 {
573 // ... that is set focus to this window
574 if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
575 currentMouseWindow->SetFocus();
576 }
577
30e0b1c9
SC
578 ControlPartCode dummyPart ;
579 // if built-in find control is finding the wrong control (ie static box instead of overlaid
580 // button, we cannot let the standard handler do its job, but must handle manually
581
902725ee 582 if ( ( cEvent.GetKind() == kEventMouseDown )
23ad132c 583#ifdef __WXMAC_OSX__
902725ee
WS
584 &&
585 (FindControlUnderMouse(windowMouseLocation , window , &dummyPart) !=
586 wxMacFindControlUnderMouse( toplevelWindow , windowMouseLocation , window , &dummyPart ) )
23ad132c
SC
587#endif
588 )
30e0b1c9 589 {
902725ee 590 if ( currentMouseWindow->MacIsReallyEnabled() )
7ce1a4e1
SC
591 {
592 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
593 Point clickLocation = windowMouseLocation ;
789ae0cf
SC
594
595 if ( toplevelWindow->MacUsesCompositing() )
596 currentMouseWindow->MacRootWindowToWindow( &clickLocation.h , &clickLocation.v ) ;
597
7ce1a4e1
SC
598 HandleControlClick( (ControlRef) currentMouseWindow->GetHandle() , clickLocation ,
599 modifiers , (ControlActionUPP ) -1 ) ;
902725ee
WS
600
601 if ((currentMouseWindowParent != NULL) &&
62ade180 602 (currentMouseWindowParent->GetChildren().Find(currentMouseWindow) == NULL))
e10e9469 603 currentMouseWindow = NULL;
7ce1a4e1 604 }
30e0b1c9
SC
605 result = noErr ;
606 }
607 }
2a1f999f 608 if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
facd6764 609 {
2a1f999f 610 wxApp::s_captureWindow = NULL ;
facd6764
SC
611 // update cursor ?
612 }
6d63e2fc
SC
613
614 // update cursor
902725ee 615
6d63e2fc
SC
616 wxWindow* cursorTarget = currentMouseWindow ;
617 wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
618
619 while( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
620 {
621 cursorTarget = cursorTarget->GetParent() ;
622 if ( cursorTarget )
e10e9469 623 cursorPoint += cursorTarget->GetPosition();
6d63e2fc
SC
624 }
625
facd6764 626 } // else if ( currentMouseWindow )
d88ffaaa
SC
627 else
628 {
629 // don't mess with controls we don't know about
630 // for some reason returning eventNotHandledErr does not lead to the correct behaviour
631 // so we try sending them the correct control directly
e4b7e0b4 632 if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
d88ffaaa
SC
633 {
634 EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
635 Point clickLocation = windowMouseLocation ;
789ae0cf
SC
636 if ( toplevelWindow->MacUsesCompositing() )
637 {
638#ifdef __WXMAC_OSX__
639 HIPoint hiPoint ;
640 hiPoint.x = clickLocation.h ;
641 hiPoint.y = clickLocation.v ;
642 HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
643 clickLocation.h = (int)hiPoint.x ;
644 clickLocation.v = (int)hiPoint.y ;
d88ffaaa 645#endif
789ae0cf 646 }
d88ffaaa
SC
647 HandleControlClick( control , clickLocation ,
648 modifiers , (ControlActionUPP ) -1 ) ;
649 result = noErr ;
650 }
651 }
facd6764 652 return result ;
851b3a88 653}
facd6764
SC
654
655static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
656{
657 OSStatus result = eventNotHandledErr ;
851b3a88 658
facd6764 659 wxMacCarbonEvent cEvent( event ) ;
902725ee 660
facd6764 661 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
e40298d5 662 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 663
e40298d5
JS
664 switch( GetEventKind( event ) )
665 {
e40298d5 666 case kEventWindowActivated :
facd6764
SC
667 {
668 toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
facd6764
SC
669 wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
670 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
671 wxevent.SetEventObject(toplevelWindow);
672 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 673 // we still sending an eventNotHandledErr in order to allow for default processing
e40298d5 674 break ;
facd6764 675 }
e40298d5 676 case kEventWindowDeactivated :
facd6764
SC
677 {
678 toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
679 wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
680 wxevent.SetTimestamp( cEvent.GetTicks() ) ;
681 wxevent.SetEventObject(toplevelWindow);
682 toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
73fe67bd 683 // we still sending an eventNotHandledErr in order to allow for default processing
e40298d5 684 break ;
facd6764 685 }
902725ee
WS
686 case kEventWindowShown :
687 {
688 toplevelWindow->Refresh() ;
689 result = noErr ;
690 break ;
691 }
e40298d5
JS
692 case kEventWindowClose :
693 toplevelWindow->Close() ;
694 result = noErr ;
695 break ;
696 case kEventWindowBoundsChanged :
facd6764
SC
697 {
698 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
699 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
700 wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
701 if ( attributes & kWindowBoundsChangeSizeChanged )
e40298d5 702 {
facd6764
SC
703 // according to the other ports we handle this within the OS level
704 // resize event, not within a wxSizeEvent
705 wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
706 if ( frame )
707 {
708 #if wxUSE_STATUSBAR
709 frame->PositionStatusBar();
902725ee 710 #endif
facd6764
SC
711 #if wxUSE_TOOLBAR
712 frame->PositionToolBar();
902725ee 713 #endif
facd6764 714 }
1542ea39 715
facd6764
SC
716 wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
717 event.SetEventObject( toplevelWindow ) ;
851b3a88 718
facd6764 719 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
5e533062 720 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
e40298d5 721 }
facd6764
SC
722 if ( attributes & kWindowBoundsChangeOriginChanged )
723 {
724 wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
725 event.SetEventObject( toplevelWindow ) ;
726 toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
727 }
728 result = noErr ;
e40298d5 729 break ;
facd6764 730 }
f81bfef9 731 case kEventWindowBoundsChanging :
facd6764
SC
732 {
733 UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
734 Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
83901ec2 735
facd6764
SC
736 if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
737 {
29281095
SC
738 // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
739 int left , top , right , bottom ;
740 toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
902725ee 741 wxRect r( newRect.left - left , newRect.top - top ,
29281095 742 newRect.right - newRect.left + left + right , newRect.bottom - newRect.top + top + bottom ) ;
facd6764
SC
743 // this is a EVT_SIZING not a EVT_SIZE type !
744 wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
745 wxevent.SetEventObject( toplevelWindow ) ;
746 wxRect adjustR = r ;
747 if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
facd6764 748 adjustR = wxevent.GetRect() ;
29281095 749
facd6764
SC
750 if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
751 adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
2c25bd55 752 if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
facd6764
SC
753 adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
754 if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
755 adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
2c25bd55 756 if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
facd6764 757 adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
4a5f2351 758 const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
facd6764 759 if ( !EqualRect( &newRect , &adjustedRect ) )
1f919f38 760 cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
4817190d 761 toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
f81bfef9 762 }
facd6764 763
902725ee 764 result = noErr ;
f81bfef9 765 break ;
facd6764 766 }
e40298d5
JS
767 default :
768 break ;
769 }
770 return result ;
851b3a88
SC
771}
772
facd6764 773pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
774{
775 OSStatus result = eventNotHandledErr ;
776
777 switch ( GetEventClass( event ) )
778 {
c5c9378c 779 case kEventClassKeyboard :
e40298d5 780 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 781 break ;
851b3a88 782 case kEventClassTextInput :
e40298d5 783 result = TextInputEventHandler( handler, event , data ) ;
851b3a88
SC
784 break ;
785 case kEventClassWindow :
facd6764 786 result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
e40298d5 787 break ;
851b3a88 788 case kEventClassMouse :
facd6764 789 result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
e40298d5 790 break ;
851b3a88
SC
791 default :
792 break ;
793 }
794 return result ;
795}
796
facd6764 797DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
851b3a88 798
5f0b2f22
SC
799// ---------------------------------------------------------------------------
800// wxWindowMac utility functions
801// ---------------------------------------------------------------------------
802
803// Find an item given the Macintosh Window Reference
804
71f2fb52 805#if KEY_wxList_DEPRECATED
facd6764
SC
806wxList wxWinMacWindowList(wxKEY_INTEGER);
807wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
5f0b2f22 808{
facd6764 809 wxNode *node = wxWinMacWindowList.Find((long)inWindowRef);
5f0b2f22
SC
810 if (!node)
811 return NULL;
eb22f2a6 812 return (wxTopLevelWindowMac *)node->GetData();
5f0b2f22
SC
813}
814
facd6764
SC
815void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
816void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
5f0b2f22
SC
817{
818 // adding NULL WindowRef is (first) surely a result of an error and
819 // (secondly) breaks menu command processing
427ff662 820 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
5f0b2f22 821
facd6764
SC
822 if ( !wxWinMacWindowList.Find((long)inWindowRef) )
823 wxWinMacWindowList.Append((long)inWindowRef, win);
5f0b2f22
SC
824}
825
facd6764 826void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
5f0b2f22
SC
827void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
828{
facd6764 829 wxWinMacWindowList.DeleteObject(win);
5f0b2f22 830}
71f2fb52 831#else
5f0b2f22 832
71f2fb52
RN
833WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
834
835static MacWindowMap wxWinMacWindowList;
836
837wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
838{
839 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
840
841 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
842}
843
844void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
845void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
846{
847 // adding NULL WindowRef is (first) surely a result of an error and
848 // nothing else :-)
849 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
850
851 wxWinMacWindowList[inWindowRef] = win;
852}
853
854void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
855void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
856{
857 MacWindowMap::iterator it;
858 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
859 {
860 if ( it->second == win )
861 {
862 wxWinMacWindowList.erase(it);
863 break;
864 }
865 }
866}
867#endif // deprecated wxList
5f0b2f22 868
a15eb0a5
SC
869// ----------------------------------------------------------------------------
870// wxTopLevelWindowMac creation
871// ----------------------------------------------------------------------------
872
245f3581 873wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 874
902725ee 875typedef struct
c5985061
SC
876{
877 wxPoint m_position ;
902725ee 878 wxSize m_size ;
c5985061
SC
879} FullScreenData ;
880
a15eb0a5
SC
881void wxTopLevelWindowMac::Init()
882{
883 m_iconized =
902725ee 884 m_maximizeOnShow = false;
6a17ca35 885 m_macWindow = NULL ;
902725ee 886#if TARGET_API_MAC_OSX
789ae0cf 887 if ( UMAGetSystemVersion() >= 0x1030 )
902725ee
WS
888 {
889 m_macUsesCompositing = true;
789ae0cf
SC
890 }
891 else
facd6764 892#endif
789ae0cf 893 {
902725ee 894 m_macUsesCompositing = false;
789ae0cf 895 }
7c091673 896 m_macEventHandler = NULL ;
c5985061 897 m_macFullScreenData = NULL ;
a15eb0a5
SC
898}
899
118f012e
SC
900class wxMacDeferredWindowDeleter : public wxObject
901{
902public :
1542ea39
RD
903 wxMacDeferredWindowDeleter( WindowRef windowRef )
904 {
905 m_macWindow = windowRef ;
118f012e 906 }
1542ea39
RD
907 virtual ~wxMacDeferredWindowDeleter()
908 {
909 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e
SC
910 }
911 protected :
912 WindowRef m_macWindow ;
913} ;
914
a15eb0a5
SC
915bool wxTopLevelWindowMac::Create(wxWindow *parent,
916 wxWindowID id,
917 const wxString& title,
918 const wxPoint& pos,
919 const wxSize& size,
920 long style,
921 const wxString& name)
922{
923 // init our fields
924 Init();
925
926 m_windowStyle = style;
927
928 SetName(name);
929
930 m_windowId = id == -1 ? NewControlId() : id;
c2bc1e2a 931 wxWindow::SetTitle( title ) ;
a15eb0a5 932
facd6764
SC
933 MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
934
94d1d0f4 935 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
902725ee 936
5bd78bbc
JS
937 if (GetExtraStyle() & wxFRAME_EX_METAL)
938 MacSetMetalAppearance(true);
94d1d0f4 939
a15eb0a5
SC
940 wxTopLevelWindows.Append(this);
941
942 if ( parent )
943 parent->AddChild(this);
944
902725ee 945 return true;
a15eb0a5
SC
946}
947
948wxTopLevelWindowMac::~wxTopLevelWindowMac()
949{
6a17ca35
SC
950 if ( m_macWindow )
951 {
c81c6d54 952#if wxUSE_TOOLTIPS
6a17ca35 953 wxToolTip::NotifyWindowDelete(m_macWindow) ;
c81c6d54 954#endif
118f012e 955 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 956 }
1542ea39 957
7c091673
SC
958 if ( m_macEventHandler )
959 {
960 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
961 m_macEventHandler = NULL ;
962 }
851b3a88 963
5f0b2f22
SC
964 wxRemoveMacWindowAssociation( this ) ;
965
a15eb0a5
SC
966 if ( wxModelessWindows.Find(this) )
967 wxModelessWindows.DeleteObject(this);
902725ee 968
c5985061
SC
969 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
970 delete data ;
971 m_macFullScreenData = NULL ;
a15eb0a5
SC
972}
973
974
975// ----------------------------------------------------------------------------
976// wxTopLevelWindowMac maximize/minimize
977// ----------------------------------------------------------------------------
978
979void wxTopLevelWindowMac::Maximize(bool maximize)
980{
699b6af4
SC
981 // TODO Check, is this still necessary
982#if 0
1f90939c
SC
983 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
984 wxMacWindowClipper clip (this);
699b6af4 985#endif
902725ee 986 if ( !IsWindowInStandardState( (WindowRef)m_macWindow, NULL, NULL) )
699b6af4
SC
987 {
988 Rect rect;
989 GetWindowBounds((WindowRef)m_macWindow, kWindowGlobalPortRgn, &rect);
990 SetWindowIdealUserState((WindowRef)m_macWindow, &rect);
991 SetWindowUserState((WindowRef)m_macWindow, &rect);
992 }
b7aec135 993 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
a15eb0a5
SC
994}
995
996bool wxTopLevelWindowMac::IsMaximized() const
997{
b7aec135 998 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
999}
1000
1001void wxTopLevelWindowMac::Iconize(bool iconize)
1002{
b7aec135
SC
1003 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
1004 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
1005}
1006
1007bool wxTopLevelWindowMac::IsIconized() const
1008{
b7aec135 1009 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
1010}
1011
1012void wxTopLevelWindowMac::Restore()
1013{
902725ee 1014 if ( IsMaximized() )
699b6af4 1015 Maximize(false);
902725ee 1016 else if ( IsIconized() )
699b6af4 1017 Iconize(false);
a15eb0a5
SC
1018}
1019
1020// ----------------------------------------------------------------------------
1021// wxTopLevelWindowMac misc
1022// ----------------------------------------------------------------------------
1023
facd6764
SC
1024wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
1025{
1026 return wxPoint(0,0) ;
1027}
1028
a15eb0a5
SC
1029void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
1030{
1031 // this sets m_icon
1032 wxTopLevelWindowBase::SetIcon(icon);
1033}
5f0b2f22 1034
facd6764 1035void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
902725ee 1036{
facd6764
SC
1037 wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
1038
1039 if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
1040 {
1041 SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
1042 }
1043}
1044
902725ee 1045void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
949cb163
SC
1046{
1047 if ( m_macEventHandler != NULL )
1048 {
1049 verify_noerr( ::RemoveEventHandler( (EventHandlerRef) m_macEventHandler ) ) ;
1050 }
1051 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacTopLevelEventHandlerUPP(),
1052 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
1053}
1054
5f0b2f22
SC
1055void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
1056 const wxPoint& pos,
1057 const wxSize& size,
1058 long style,
1542ea39 1059 const wxString& name )
5f0b2f22 1060{
47ac4f9b 1061 OSStatus err = noErr ;
e40298d5
JS
1062 SetName(name);
1063 m_windowStyle = style;
902725ee 1064 m_isShown = false;
1542ea39 1065
e40298d5 1066 // create frame.
1542ea39 1067
5f0b2f22 1068 Rect theBoundsRect;
1542ea39 1069
facd6764
SC
1070 int x = (int)pos.x;
1071 int y = (int)pos.y;
902725ee 1072
5a486641
SC
1073 wxRect display = wxGetClientDisplayRect() ;
1074
1075 if ( x == wxDefaultPosition.x )
1076 x = display.x ;
902725ee 1077
5a486641
SC
1078 if ( y == wxDefaultPosition.y )
1079 y = display.y ;
1542ea39 1080
facd6764
SC
1081 int w = WidthDefault(size.x);
1082 int h = HeightDefault(size.y);
1542ea39 1083
facd6764 1084 ::SetRect(&theBoundsRect, x, y , x + w, y + h);
1542ea39 1085
5f0b2f22 1086 // translate the window attributes in the appropriate window class and attributes
1542ea39 1087
5f0b2f22
SC
1088 WindowClass wclass = 0;
1089 WindowAttributes attr = kWindowNoAttributes ;
e353795e 1090 WindowGroupRef group = NULL ;
1542ea39 1091
f5744893 1092 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 1093 {
1542ea39 1094 if (
f5744893
SC
1095 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
1096 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
1097 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 1098 )
5f0b2f22 1099 {
f5744893
SC
1100 wclass = kFloatingWindowClass ;
1101 if ( HasFlag(wxTINY_CAPTION_VERT) )
1102 {
1103 attr |= kWindowSideTitlebarAttribute ;
1104 }
1105 }
1106 else
1107 {
1108 wclass = kPlainWindowClass ;
5f0b2f22
SC
1109 }
1110 }
1111 else if ( HasFlag( wxCAPTION ) )
1112 {
1542ea39 1113 wclass = kDocumentWindowClass ;
22e3c5bd 1114 attr |= kWindowInWindowMenuAttribute ;
5f0b2f22 1115 }
eab19a7c 1116#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
3e17bc3f
RN
1117 else if ( HasFlag( wxFRAME_DRAWER ) )
1118 {
1119 wclass = kDrawerWindowClass;
789ae0cf 1120 // we must force compositing on a drawer
902725ee 1121 m_macUsesCompositing = true ;
3e17bc3f
RN
1122 }
1123#endif //10.2 and up
5f0b2f22
SC
1124 else
1125 {
f5744893 1126 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 1127 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
1128 {
1129 wclass = kDocumentWindowClass ;
1130 }
1131 else
1132 {
1133 wclass = kPlainWindowClass ;
1134 }
5f0b2f22 1135 }
1542ea39 1136
38e605eb 1137 if ( HasFlag( wxMINIMIZE_BOX ) && wclass != kPlainWindowClass )
5f0b2f22 1138 {
5f0b2f22
SC
1139 attr |= kWindowCollapseBoxAttribute ;
1140 }
38e605eb 1141 if ( HasFlag( wxMAXIMIZE_BOX ) && wclass != kPlainWindowClass )
47ac4f9b
SC
1142 {
1143 attr |= kWindowFullZoomAttribute ;
1144 }
38e605eb 1145 if ( HasFlag( wxRESIZE_BORDER ) && wclass != kPlainWindowClass )
5f0b2f22
SC
1146 {
1147 attr |= kWindowResizableAttribute ;
1148 }
38e605eb 1149 if ( HasFlag( wxCLOSE_BOX) && wclass != kPlainWindowClass )
5f0b2f22
SC
1150 {
1151 attr |= kWindowCloseBoxAttribute ;
1152 }
1542ea39 1153
eb630bf1
DS
1154 if (UMAGetSystemVersion() >= 0x1000)
1155 {
facd6764 1156 // turn on live resizing (OS X only)
eb630bf1
DS
1157 attr |= kWindowLiveResizeAttribute;
1158 }
1159
e353795e
SC
1160 if ( HasFlag(wxSTAY_ON_TOP) )
1161 {
1162 group = GetWindowGroupOfClass(kUtilityWindowClass) ;
1163 }
6a7e6411 1164
902725ee 1165#if TARGET_API_MAC_OSX
789ae0cf
SC
1166 if ( m_macUsesCompositing )
1167 attr |= kWindowCompositingAttribute;
d66c3960 1168#endif
902725ee 1169
6a7e6411
RD
1170 if ( HasFlag(wxFRAME_SHAPED) )
1171 {
1172 WindowDefSpec customWindowDefSpec;
1173 customWindowDefSpec.defType = kWindowDefProcPtr;
1174 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
1175
47ac4f9b 1176 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
1177 attr, &theBoundsRect,
1178 (WindowRef*) &m_macWindow);
1179 }
1180 else
1181 {
47ac4f9b 1182 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
1183 }
1184
e353795e
SC
1185 if ( err == noErr && m_macWindow != NULL && group != NULL )
1186 SetWindowGroup( (WindowRef) m_macWindow , group ) ;
1187
47ac4f9b 1188 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
f83b6761
SC
1189
1190 // the create commands are only for content rect, so we have to set the size again as
902725ee 1191 // structure bounds
f83b6761
SC
1192 SetWindowBounds( (WindowRef) m_macWindow , kWindowStructureRgn , &theBoundsRect ) ;
1193
facd6764
SC
1194 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
1195 UMASetWTitle( (WindowRef) m_macWindow , title , m_font.GetEncoding() ) ;
1f1c8bd4 1196 m_peer = new wxMacControl(this , true /*isRootControl*/) ;
3a9dc061 1197#if TARGET_API_MAC_OSX
902725ee 1198
789ae0cf 1199 if ( m_macUsesCompositing )
f57d9215 1200 {
902725ee 1201 // There is a bug in 10.2.X for ::GetRootControl returning the window view instead of
3103e8a9 1202 // the content view, so we have to retrieve it explicitly
902725ee 1203 HIViewFindByID( HIViewGetRoot( (WindowRef) m_macWindow ) , kHIViewWindowContentID ,
789ae0cf
SC
1204 m_peer->GetControlRefAddr() ) ;
1205 if ( !m_peer->Ok() )
1206 {
1207 // compatibility mode fallback
1208 GetRootControl( (WindowRef) m_macWindow , m_peer->GetControlRefAddr() ) ;
1209 }
f57d9215 1210 }
3a9dc061 1211#endif
789ae0cf
SC
1212 {
1213 ::CreateRootControl( (WindowRef)m_macWindow , m_peer->GetControlRefAddr() ) ;
1214 }
73fe67bd 1215 // the root control level handleer
5ca0d812 1216 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() ) ;
facd6764 1217
73fe67bd 1218 // the frame window event handler
e40298d5 1219 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
949cb163 1220 MacInstallTopLevelWindowEventHandler() ;
b19bf058 1221
14c96cf2
SC
1222 DoSetWindowVariant( m_windowVariant ) ;
1223
facd6764 1224 m_macFocus = NULL ;
6a7e6411
RD
1225
1226 if ( HasFlag(wxFRAME_SHAPED) )
1227 {
1228 // default shape matches the window size
facd6764 1229 wxRegion rgn(0, 0, w, h);
6a7e6411
RD
1230 SetShape(rgn);
1231 }
3dfafdb9
RD
1232
1233 wxWindowCreateEvent event(this);
1234 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1235}
1236
5d2e69e8 1237void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 1238{
5d2e69e8 1239 wxWindow::ClearBackground() ;
5f0b2f22
SC
1240}
1241
5f0b2f22
SC
1242// Raise the window to the top of the Z order
1243void wxTopLevelWindowMac::Raise()
1244{
7f3c339c 1245 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1246}
1247
1248// Lower the window to the bottom of the Z order
1249void wxTopLevelWindowMac::Lower()
1250{
76a5e5d2 1251 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
1252}
1253
851b3a88 1254
245f3581
DE
1255void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1256{
1257 if(s_macDeactivateWindow)
1258 {
365796b1 1259 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
245f3581
DE
1260 s_macDeactivateWindow->MacActivate(timestamp, false);
1261 }
1262}
1263
851b3a88 1264void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 1265{
17f8d2a7
VZ
1266 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1267
245f3581
DE
1268 if(s_macDeactivateWindow==this)
1269 s_macDeactivateWindow=NULL;
1270 MacDelayedDeactivation(timestamp);
8ab50549 1271 MacPropagateHiliteChanged() ;
5f0b2f22
SC
1272}
1273
1274void wxTopLevelWindowMac::SetTitle(const wxString& title)
1275{
1276 wxWindow::SetTitle( title ) ;
16a76184 1277 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
5f0b2f22
SC
1278}
1279
1280bool wxTopLevelWindowMac::Show(bool show)
1281{
facd6764 1282 if ( !wxTopLevelWindowBase::Show(show) )
902725ee 1283 return false;
5f0b2f22
SC
1284
1285 if (show)
1542ea39 1286 {
902725ee 1287 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1f90939c
SC
1288 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1289 {
1290 ::ShowWindow( (WindowRef)m_macWindow );
1291 }
1292 else
1293 #endif
1294 {
1295 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
1296 }
1297 ::SelectWindow( (WindowRef)m_macWindow ) ;
facd6764
SC
1298 // as apps expect a size event to occur at this moment
1299 wxSizeEvent event( GetSize() , m_windowId);
1f90939c
SC
1300 event.SetEventObject(this);
1301 GetEventHandler()->ProcessEvent(event);
5f0b2f22
SC
1302 }
1303 else
1304 {
1f90939c
SC
1305 #if wxUSE_SYSTEM_OPTIONS
1306 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1307 {
1308 ::HideWindow((WindowRef) m_macWindow );
1309 }
1310 else
1311 #endif
1312 {
1313 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
1314 }
5f0b2f22
SC
1315 }
1316
facd6764 1317 MacPropagateVisibilityChanged() ;
5f0b2f22 1318
902725ee 1319 return true ;
5f0b2f22
SC
1320}
1321
c5985061 1322bool wxTopLevelWindowMac::ShowFullScreen(bool show, long style)
902725ee 1323{
c5985061
SC
1324 if ( show )
1325 {
1326 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
1327 delete data ;
1328 data = new FullScreenData() ;
902725ee 1329
c5985061
SC
1330 m_macFullScreenData = data ;
1331 data->m_position = GetPosition() ;
1332 data->m_size = GetSize() ;
902725ee 1333
c5985061
SC
1334 if ( style & wxFULLSCREEN_NOMENUBAR )
1335 {
1336 HideMenuBar() ;
1337 }
1338 int left , top , right , bottom ;
1339 wxRect client = wxGetClientDisplayRect() ;
1340
1341 int x, y, w, h ;
902725ee 1342
c5985061
SC
1343 x = client.x ;
1344 y = client.y ;
1345 w = client.width ;
1346 h = client.height ;
902725ee 1347
c5985061
SC
1348 MacGetContentAreaInset( left , top , right , bottom ) ;
1349
1350 if ( style & wxFULLSCREEN_NOCAPTION )
1351 {
1352 y -= top ;
1353 h += top ;
1354 }
1355 if ( style & wxFULLSCREEN_NOBORDER )
1356 {
1357 x -= left ;
1358 w += left + right ;
1359 h += bottom ;
1360 }
1361 if ( style & wxFULLSCREEN_NOTOOLBAR )
1362 {
3c86150d
SC
1363 // TODO
1364 }
1365 if ( style & wxFULLSCREEN_NOSTATUSBAR )
1366 {
1367 // TODO
c5985061
SC
1368 }
1369 SetSize( x , y , w, h ) ;
1370 }
1371 else
1372 {
1373 ShowMenuBar() ;
1374 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
1375 SetPosition( data->m_position ) ;
1376 SetSize( data->m_size ) ;
1377 delete data ;
1378 m_macFullScreenData = NULL ;
1379 }
902725ee 1380 return false;
c5985061
SC
1381}
1382
902725ee
WS
1383bool wxTopLevelWindowMac::IsFullScreen() const
1384{
1385 return m_macFullScreenData != NULL ;
c5985061
SC
1386}
1387
facd6764
SC
1388// we are still using coordinates of the content view, todo switch to structure bounds
1389
29281095
SC
1390void wxTopLevelWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1391{
1392 Rect content ;
1393 Rect structure ;
1394 GetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &structure ) ;
1395 GetWindowBounds( (WindowRef) m_macWindow, kWindowContentRgn , &content ) ;
1396
1397 left = content.left - structure.left ;
1398 top = content.top - structure.top ;
1399 right = structure.right - content.right ;
1400 bottom = structure.bottom - content.bottom ;
1401}
1402
5f0b2f22
SC
1403void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1404{
c9698388 1405 m_cachedClippedRectValid = false ;
facd6764
SC
1406 Rect bounds = { y , x , y + height , x + width } ;
1407 verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
4817190d 1408 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
5f0b2f22
SC
1409}
1410
facd6764 1411void wxTopLevelWindowMac::DoGetPosition( int *x, int *y ) const
5f0b2f22 1412{
facd6764
SC
1413 Rect bounds ;
1414 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1415 if(x) *x = bounds.left ;
1416 if(y) *y = bounds.top ;
1417}
1418void wxTopLevelWindowMac::DoGetSize( int *width, int *height ) const
1419{
1420 Rect bounds ;
1421 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
1422 if(width) *width = bounds.right - bounds.left ;
1423 if(height) *height = bounds.bottom - bounds.top ;
1424}
1542ea39 1425
facd6764
SC
1426void wxTopLevelWindowMac::DoGetClientSize( int *width, int *height ) const
1427{
1428 Rect bounds ;
1429 verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowContentRgn , &bounds )) ;
1430 if(width) *width = bounds.right - bounds.left ;
1431 if(height) *height = bounds.bottom - bounds.top ;
1432}
1542ea39 1433
902725ee 1434void wxTopLevelWindowMac::MacSetMetalAppearance( bool set )
facd6764
SC
1435{
1436#if TARGET_API_MAC_OSX
789ae0cf 1437 wxASSERT_MSG( m_macUsesCompositing ,
facd6764 1438 wxT("Cannot set metal appearance on a non-compositing window") ) ;
902725ee
WS
1439
1440 MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
facd6764
SC
1441 set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
1442#endif
1443}
1542ea39 1444
902725ee 1445bool wxTopLevelWindowMac::MacGetMetalAppearance() const
6aab345b
SC
1446{
1447#if TARGET_API_MAC_OSX
1448 return MacGetWindowAttributes() & kWindowMetalAttribute ;
1449#else
1450 return false ;
1451#endif
1452}
1453
902725ee 1454void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
facd6764
SC
1455{
1456 ChangeWindowAttributes ( (WindowRef) m_macWindow , attributesToSet, attributesToClear ) ;
1457}
1542ea39 1458
902725ee 1459wxUint32 wxTopLevelWindowMac::MacGetWindowAttributes() const
facd6764
SC
1460{
1461 UInt32 attr = 0 ;
1462 GetWindowAttributes((WindowRef) m_macWindow , &attr ) ;
1463 return attr ;
5f0b2f22 1464}
a07c1212 1465
902725ee 1466void wxTopLevelWindowMac::MacPerformUpdates()
92346151
SC
1467{
1468#if TARGET_API_MAC_OSX
902725ee
WS
1469 if ( m_macUsesCompositing )
1470 {
92346151 1471#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
902725ee
WS
1472 // for composited windows this also triggers a redraw of all
1473 // invalid views in the window
1474 if( UMAGetSystemVersion() >= 0x1030 )
1475 HIWindowFlush((WindowRef) m_macWindow) ;
1476 else
92346151 1477#endif
902725ee
WS
1478 {
1479 // the only way to trigger the redrawing on earlier systems is to call
1480 // ReceiveNextEvent
1481
1482 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
1483 UInt32 currentEventClass = 0 ;
1484 UInt32 currentEventKind = 0 ;
1485 if ( currentEvent != NULL )
1486 {
1487 currentEventClass = ::GetEventClass( currentEvent ) ;
1488 currentEventKind = ::GetEventKind( currentEvent ) ;
1489 }
1490 if ( currentEventClass != kEventClassMenu )
1491 {
1492 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
1493 EventRef theEvent;
1494 OSStatus status = noErr ;
1495 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
1496 }
1497 }
92346151
SC
1498 }
1499 else
1500#endif
1501 {
902725ee
WS
1502 BeginUpdate( (WindowRef) m_macWindow ) ;
1503
1504 RgnHandle updateRgn = NewRgn();
1505 if ( updateRgn )
1506 {
1507 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
1508 UpdateControls( (WindowRef)m_macWindow , updateRgn ) ;
1509 // if ( !EmptyRgn( updateRgn ) )
1510 // MacDoRedraw( updateRgn , 0 , true) ;
1511 DisposeRgn( updateRgn );
1512 }
1513 EndUpdate( (WindowRef)m_macWindow ) ;
1514 QDFlushPortBuffer( GetWindowPort( (WindowRef)m_macWindow ) , NULL ) ;
92346151
SC
1515 }
1516}
1517
3feeb1e1
SC
1518// Attracts the users attention to this window if the application is
1519// inactive (should be called when a background event occurs)
1520
1521static pascal void wxMacNMResponse( NMRecPtr ptr )
1522{
1523 NMRemove( ptr ) ;
1524 DisposePtr( (Ptr) ptr ) ;
1525}
1526
1527
1528void wxTopLevelWindowMac::RequestUserAttention(int flags )
1529{
1530 NMRecPtr notificationRequest = (NMRecPtr) NewPtr( sizeof( NMRec) ) ;
1531 static wxMacNMUPP nmupp( wxMacNMResponse )
1532 ;
1533 memset( notificationRequest , 0 , sizeof(*notificationRequest) ) ;
1534 notificationRequest->qType = nmType ;
1535 notificationRequest->nmMark = 1 ;
1536 notificationRequest->nmIcon = 0 ;
1537 notificationRequest->nmSound = 0 ;
1538 notificationRequest->nmStr = NULL ;
1539 notificationRequest->nmResp = nmupp ;
1540 verify_noerr( NMInstall( notificationRequest ) ) ;
1541}
1542
facd6764
SC
1543// ---------------------------------------------------------------------------
1544// Shape implementation
1545// ---------------------------------------------------------------------------
1546
6a7e6411 1547
1542ea39
RD
1548bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1549{
902725ee 1550 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
6a7e6411
RD
1551 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1552
1553 // The empty region signifies that the shape should be removed from the
1554 // window.
1555 if ( region.IsEmpty() )
1556 {
1557 wxSize sz = GetClientSize();
1558 wxRegion rgn(0, 0, sz.x, sz.y);
1559 return SetShape(rgn);
1560 }
1561
1562 // Make a copy of the region
1563 RgnHandle shapeRegion = NewRgn();
1564 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1565
1566 // Dispose of any shape region we may already have
1567 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1568 if ( oldRgn )
1569 DisposeRgn(oldRgn);
1570
1571 // Save the region so we can use it later
1572 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1573
1574 // Tell the window manager that the window has changed shape
1575 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
902725ee 1576 return true;
6a7e6411
RD
1577}
1578
6a7e6411
RD
1579// ---------------------------------------------------------------------------
1580// Support functions for shaped windows, based on Apple's CustomWindow sample at
1581// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1582// ---------------------------------------------------------------------------
1583
6a7e6411
RD
1584static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1585{
1586 GetWindowPortBounds(window, inRect);
1587 Point pt = {inRect->left, inRect->top};
d6fd667b 1588 QDLocalToGlobalPoint( GetWindowPort(window) , &pt ) ;
6a7e6411
RD
1589 inRect->top = pt.v;
1590 inRect->left = pt.h;
1591 inRect->bottom += pt.v;
1592 inRect->right += pt.h;
1593}
1594
1595
1596static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1597{
1598 /*------------------------------------------------------
1599 Define which options your custom window supports.
1600 --------------------------------------------------------*/
1601 //just enable everything for our demo
1602 *(OptionBits*)param=//kWindowCanGrow|
1603 //kWindowCanZoom|
1604 //kWindowCanCollapse|
1605 //kWindowCanGetWindowRegion|
1606 //kWindowHasTitleBar|
1607 //kWindowSupportsDragHilite|
1608 kWindowCanDrawInCurrentPort|
1609 //kWindowCanMeasureTitle|
1610 kWindowWantsDisposeAtProcessDeath|
ae53adb4 1611 kWindowSupportsGetGrowImageRegion|
6a7e6411
RD
1612 kWindowDefSupportsColorGrafPort;
1613 return 1;
1542ea39 1614}
6a7e6411
RD
1615
1616// The content region is left as a rectangle matching the window size, this is
1617// so the origin in the paint event, and etc. still matches what the
1618// programmer expects.
1619static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1620{
1621 SetEmptyRgn(rgn);
1622 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1623 if (win)
1624 {
b19bf058
SC
1625 Rect r ;
1626 wxShapedMacWindowGetPos(window, &r ) ;
1627 RectRgn( rgn , &r ) ;
6a7e6411
RD
1628 }
1629}
1630
1631// The structure region is set to the shape given to the SetShape method.
1632static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1633{
1634 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1635
1636 SetEmptyRgn(rgn);
1637 if (cachedRegion)
1638 {
1639 Rect windowRect;
902725ee
WS
1640 wxShapedMacWindowGetPos(window, &windowRect); // how big is the window
1641 CopyRgn(cachedRegion, rgn); // make a copy of our cached region
6a7e6411 1642 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
902725ee 1643 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
6a7e6411
RD
1644 }
1645}
1646
1647
1648
1649static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1650{
1651 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1652
1653 switch(rgnRec->regionCode)
1654 {
1655 case kWindowStructureRgn:
1656 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1657 break;
1658 case kWindowContentRgn:
1659 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1660 break;
1661 default:
1662 SetEmptyRgn(rgnRec->winRgn);
1663 } //switch
1664
1665 return noErr;
1666}
1667
1668
1669static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1670{
1671 /*------------------------------------------------------
1672 Determine the region of the window which was hit
1673 --------------------------------------------------------*/
1674 Point hitPoint;
1675 static RgnHandle tempRgn=nil;
1676
1677 if(!tempRgn)
902725ee 1678 tempRgn=NewRgn();
6a7e6411
RD
1679
1680 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1681
1682 //Mac OS 8.5 or later
1683 wxShapedMacWindowStructureRegion(window, tempRgn);
1684 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1685 return wInContent;
1686
1687 return wNoHit;//no significant area was hit.
1688}
1689
1690
1691static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1692{
1693 switch(message)
1694 {
1695 case kWindowMsgHitTest:
1696 return wxShapedMacWindowHitTest(window,param);
1697
1698 case kWindowMsgGetFeatures:
1699 return wxShapedMacWindowGetFeatures(window,param);
1700
1701 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1702 case kWindowMsgGetRegion:
1703 return wxShapedMacWindowGetRegion(window,param);
1704 }
1705
1706 return 0;
1707}
1925be65 1708
6a7e6411 1709// ---------------------------------------------------------------------------
1925be65 1710