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