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