1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for Mac
4 // Author: Stefan Csomor
8 // Copyright: (c) 2001-2004 Stefan Csomor
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
35 #include "wx/string.h"
38 #include "wx/settings.h"
41 #include "wx/mac/uma.h"
42 #include "wx/mac/aga.h"
44 #include "wx/tooltip.h"
46 #if wxUSE_SYSTEM_OPTIONS
47 #include "wx/sysopt.h"
50 #include <ToolUtils.h>
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // list of all frames and modeless dialogs
57 wxWindowList wxModelessWindows
;
59 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
);
61 // ============================================================================
62 // wxTopLevelWindowMac implementation
63 // ============================================================================
65 BEGIN_EVENT_TABLE(wxTopLevelWindowMac
, wxTopLevelWindowBase
)
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 extern long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
75 static const EventTypeSpec eventList
[] =
77 // TODO remove control related event like key and mouse (except for WindowLeave events)
79 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
81 { kEventClassKeyboard
, kEventRawKeyDown
} ,
82 { kEventClassKeyboard
, kEventRawKeyRepeat
} ,
83 { kEventClassKeyboard
, kEventRawKeyUp
} ,
84 { kEventClassKeyboard
, kEventRawKeyModifiersChanged
} ,
87 { kEventClassWindow
, kEventWindowShown
} ,
88 { kEventClassWindow
, kEventWindowActivated
} ,
89 { kEventClassWindow
, kEventWindowDeactivated
} ,
90 { kEventClassWindow
, kEventWindowBoundsChanging
} ,
91 { kEventClassWindow
, kEventWindowBoundsChanged
} ,
92 { kEventClassWindow
, kEventWindowClose
} ,
94 // we have to catch these events on the toplevel window level, as controls don't get the
95 // raw mouse events anymore
97 { kEventClassMouse
, kEventMouseDown
} ,
98 { kEventClassMouse
, kEventMouseUp
} ,
99 { kEventClassMouse
, kEventMouseWheelMoved
} ,
100 { kEventClassMouse
, kEventMouseMoved
} ,
101 { kEventClassMouse
, kEventMouseDragged
} ,
104 static pascal OSStatus
TextInputEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
106 OSStatus result
= eventNotHandledErr
;
108 wxWindow
* focus
= wxWindow::FindFocus() ;
116 GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
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
);
124 switch ( GetEventKind( event
) )
126 case kEventTextInputUnicodeForKeyEvent
:
127 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
128 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
129 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
132 ControlRef macControl
= (ControlRef
) control
->GetHandle() ;
135 ::HandleControlKey( macControl
, keyCode
, charCode
, modifiers
) ;
140 // this may lead to double events sent to a window in case all handlers have skipped the key down event
141 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
142 UInt32 message = (keyCode << 8) + charCode;
144 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
145 focus , message , modifiers , when , point.h , point.v ) )
156 static pascal OSStatus
KeyboardEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
158 OSStatus result
= eventNotHandledErr
;
160 wxWindow
* focus
= wxWindow::FindFocus() ;
168 UInt32 when
= EventTimeToTicks( GetEventTime( event
) ) ;
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
);
176 UInt32 message
= (keyCode
<< 8) + charCode
;
177 switch( GetEventKind( event
) )
179 case kEventRawKeyRepeat
:
180 case kEventRawKeyDown
:
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
) )
190 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerHandler
) ;
193 case kEventRawKeyUp
:
194 if ( (focus
!= NULL
) && wxTheApp
->MacSendKeyUpEvent(
195 focus
, message
, modifiers
, when
, point
.h
, point
.v
) )
200 case kEventRawKeyModifiersChanged
:
202 wxKeyEvent
event(wxEVT_KEY_DOWN
);
204 event
.m_shiftDown
= modifiers
& shiftKey
;
205 event
.m_controlDown
= modifiers
& controlKey
;
206 event
.m_altDown
= modifiers
& optionKey
;
207 event
.m_metaDown
= modifiers
& cmdKey
;
211 event
.m_timeStamp
= when
;
212 wxWindow
* focus
= wxWindow::FindFocus() ;
213 event
.SetEventObject(focus
);
215 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & controlKey
)
217 event
.m_keyCode
= WXK_CONTROL
;
218 event
.SetEventType( ( modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
219 focus
->GetEventHandler()->ProcessEvent( event
) ;
221 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & shiftKey
)
223 event
.m_keyCode
= WXK_SHIFT
;
224 event
.SetEventType( ( modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
225 focus
->GetEventHandler()->ProcessEvent( event
) ;
227 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & optionKey
)
229 event
.m_keyCode
= WXK_ALT
;
230 event
.SetEventType( ( modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
231 focus
->GetEventHandler()->ProcessEvent( event
) ;
233 if ( focus
&& (modifiers
^ wxTheApp
->s_lastModifiers
) & cmdKey
)
235 event
.m_keyCode
= WXK_COMMAND
;
236 event
.SetEventType( ( modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
237 focus
->GetEventHandler()->ProcessEvent( event
) ;
239 wxTheApp
->s_lastModifiers
= modifiers
;
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
251 // This handler can also be called from app level where data (ie target window) may be null or a non wx window
253 wxWindow
* g_MacLastWindow
= NULL
;
255 static void SetupMouseEvent( wxMouseEvent
&wxevent
, wxMacCarbonEvent
&cEvent
)
257 UInt32 modifiers
= cEvent
.GetParameter
<UInt32
>(kEventParamKeyModifiers
, typeUInt32
) ;
258 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
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
) ;
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
) )
276 button
= kEventMouseButtonSecondary
;
279 // determinate the correct down state, wx does not want a 'down' for a mouseUp event, while mac delivers
281 if ( button
!= 0 && cEvent
.GetKind() != kEventMouseUp
)
285 case kEventMouseButtonPrimary
:
286 wxevent
.m_leftDown
= true ;
288 case kEventMouseButtonSecondary
:
289 wxevent
.m_rightDown
= true ;
291 case kEventMouseButtonTertiary
:
292 wxevent
.m_middleDown
= true ;
296 // determinate the correct click button
297 if ( button
== kEventMouseButtonSecondary
)
299 if (cEvent
.GetKind() == kEventMouseDown
)
300 wxevent
.SetEventType( clickCount
> 1 ? wxEVT_RIGHT_DCLICK
: wxEVT_RIGHT_DOWN
) ;
301 else if ( cEvent
.GetKind() == kEventMouseUp
)
302 wxevent
.SetEventType(wxEVT_RIGHT_UP
) ;
304 else if ( button
== kEventMouseButtonTertiary
)
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
) ;
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
)
319 wxevent
.SetEventType(wxEVT_MOUSEWHEEL
) ;
321 // EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
322 SInt32 delta
= cEvent
.GetParameter
<SInt32
>(kEventParamMouseWheelDelta
, typeLongInteger
) ;
324 wxevent
.m_wheelRotation
= delta
;
325 wxevent
.m_wheelDelta
= 1;
326 wxevent
.m_linesPerAction
= 1;
329 wxevent
.SetEventType(wxEVT_MOTION
) ;
333 ControlRef
wxMacFindSubControl( Point location
, ControlRef superControl
, ControlPartCode
*outPart
)
337 UInt16 childrenCount
= 0 ;
338 OSStatus err
= CountSubControls( superControl
, &childrenCount
) ;
339 if ( err
== errControlIsNotEmbedder
)
341 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
343 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
345 ControlHandle sibling
;
346 err
= GetIndexedSubControl( superControl
, i
, & sibling
) ;
347 if ( err
== errControlIsNotEmbedder
)
350 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
351 if ( IsControlVisible( sibling
) )
354 GetControlBounds( sibling
, &r
) ;
355 if ( MacPtInRect( location
, &r
) )
357 ControlHandle child
= wxMacFindSubControl( location
, sibling
, outPart
) ;
362 *outPart
= TestControl( sibling
, location
) ;
372 ControlRef
wxMacFindControlUnderMouse( Point location
, WindowRef window
, ControlPartCode
*outPart
)
374 #if TARGET_API_MAC_OSX
375 if ( UMAGetSystemVersion() >= 1030 )
376 return FindControlUnderMouse( location
, window
, outPart
) ;
378 ControlRef rootControl
= NULL
;
379 verify_noerr( GetRootControl( window
, &rootControl
) ) ;
380 return wxMacFindSubControl( location
, rootControl
, outPart
) ;
383 pascal OSStatus
wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
386 OSStatus result
= eventNotHandledErr
;
388 wxMacCarbonEvent
cEvent( event
) ;
390 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
391 Point windowMouseLocation
= screenMouseLocation
;
394 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
396 wxWindow
* currentMouseWindow
= NULL
;
399 // calculate window relative coordinates
402 ::SetPort( UMAGetWindowPort(window
) ) ;
403 ::GlobalToLocal( &windowMouseLocation
) ;
406 if ( wxTheApp
->s_captureWindow
&& wxTheApp
->s_captureWindow
->MacGetTopLevelWindowRef() == (WXWindow
) window
&& windowPart
== inContent
)
408 currentMouseWindow
= wxTheApp
->s_captureWindow
;
410 else if ( (IsWindowActive(window
) && windowPart
== inContent
) )
412 ControlPartCode part
;
413 ControlRef control
= wxMacFindControlUnderMouse( windowMouseLocation
, window
, &part
) ;
415 currentMouseWindow
= (wxWindow
*) data
;
417 currentMouseWindow
= wxFindControlFromMacControl( control
) ;
421 wxMouseEvent
wxevent(wxEVT_LEFT_DOWN
);
422 SetupMouseEvent( wxevent
, cEvent
) ;
424 // handle all enter / leave events
426 if ( currentMouseWindow
!= g_MacLastWindow
)
428 if ( g_MacLastWindow
)
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
) ;
436 wxToolTip::RelayEvent( g_MacLastWindow
, eventleave
);
437 #endif // wxUSE_TOOLTIPS
438 g_MacLastWindow
->GetEventHandler()->ProcessEvent(eventleave
);
440 if ( currentMouseWindow
)
442 wxMouseEvent
evententer(wxevent
);
443 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
444 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
445 evententer
.SetEventObject( currentMouseWindow
) ;
447 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
448 #endif // wxUSE_TOOLTIPS
449 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
451 g_MacLastWindow
= currentMouseWindow
;
454 if ( windowPart
== inMenuBar
)
456 // special case menu bar, as we are having a low-level runloop we must do it ourselves
457 if ( cEvent
.GetKind() == kEventMouseDown
)
459 ::MenuSelect( screenMouseLocation
) ;
462 } // if ( windowPart == inMenuBar )
463 else if ( currentMouseWindow
)
465 currentMouseWindow
->ScreenToClient( &wxevent
.m_x
, &wxevent
.m_y
) ;
467 wxevent
.SetEventObject( currentMouseWindow
) ;
471 wxWindow
* cursorTarget
= currentMouseWindow
;
472 wxPoint
cursorPoint( wxevent
.m_x
, wxevent
.m_y
) ;
474 while( cursorTarget
&& !cursorTarget
->MacSetupCursor( cursorPoint
) )
476 cursorTarget
= cursorTarget
->GetParent() ;
478 cursorPoint
+= cursorTarget
->GetPosition() ;
483 if ( wxevent
.GetEventType() == wxEVT_LEFT_DOWN
)
485 // set focus to this window
486 if (currentMouseWindow
->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow
)
487 currentMouseWindow
->SetFocus();
490 // make tooltips current
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
) )
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
506 if ( ( cEvent
.GetKind() == kEventMouseDown
) &&
507 (FindControlUnderMouse(windowMouseLocation
, window
, &dummyPart
) !=
508 wxMacFindControlUnderMouse( windowMouseLocation
, window
, &dummyPart
) ) )
510 EventModifiers modifiers
= cEvent
.GetParameter
<EventModifiers
>(kEventParamKeyModifiers
, typeUInt32
) ;
511 HandleControlClick( (ControlRef
) currentMouseWindow
->GetHandle() , windowMouseLocation
,
512 modifiers
, (ControlActionUPP
) -1 ) ;
516 if ( cEvent
.GetKind() == kEventMouseUp
&& wxTheApp
->s_captureWindow
)
518 wxTheApp
->s_captureWindow
= NULL
;
521 } // else if ( currentMouseWindow )
525 static pascal OSStatus
wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
527 OSStatus result
= eventNotHandledErr
;
529 wxMacCarbonEvent
cEvent( event
) ;
531 // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
532 wxTopLevelWindowMac
* toplevelWindow
= (wxTopLevelWindowMac
*) data
;
534 switch( GetEventKind( event
) )
536 case kEventWindowActivated
:
538 toplevelWindow
->MacActivate( cEvent
.GetTicks() , true) ;
539 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, true , toplevelWindow
->GetId());
540 wxevent
.SetTimestamp( cEvent
.GetTicks() ) ;
541 wxevent
.SetEventObject(toplevelWindow
);
542 toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
);
543 // we still sending an eventNotHandledErr in order to allow for default processing
546 case kEventWindowDeactivated
:
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
);
553 // we still sending an eventNotHandledErr in order to allow for default processing
556 case kEventWindowShown
:
557 toplevelWindow
->Refresh() ;
560 case kEventWindowClose
:
561 toplevelWindow
->Close() ;
564 case kEventWindowBoundsChanged
:
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
)
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
) ;
577 frame
->PositionStatusBar();
580 frame
->PositionToolBar();
584 wxSizeEvent
event( r
.GetSize() , toplevelWindow
->GetId() ) ;
585 event
.SetEventObject( toplevelWindow
) ;
587 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
589 if ( attributes
& kWindowBoundsChangeOriginChanged
)
591 wxMoveEvent
event( r
.GetLeftTop() , toplevelWindow
->GetId() ) ;
592 event
.SetEventObject( toplevelWindow
) ;
593 toplevelWindow
->GetEventHandler()->ProcessEvent(event
) ;
598 case kEventWindowBoundsChanging
:
600 UInt32 attributes
= cEvent
.GetParameter
<UInt32
>(kEventParamAttributes
,typeUInt32
) ;
601 Rect newRect
= cEvent
.GetParameter
<Rect
>(kEventParamCurrentBounds
) ;
603 if ( (attributes
& kWindowBoundsChangeSizeChanged
) || (attributes
& kWindowBoundsChangeOriginChanged
) )
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
) ;
610 // this is a EVT_SIZING not a EVT_SIZE type !
611 wxSizeEvent
wxevent( r
, toplevelWindow
->GetId() ) ;
612 wxevent
.SetEventObject( toplevelWindow
) ;
614 if ( toplevelWindow
->GetEventHandler()->ProcessEvent(wxevent
) )
615 adjustR
= wxevent
.GetRect() ;
617 if ( toplevelWindow
->GetMaxWidth() != -1 && adjustR
.GetWidth() > toplevelWindow
->GetMaxWidth() )
618 adjustR
.SetWidth( toplevelWindow
->GetMaxWidth() ) ;
619 if ( toplevelWindow
->GetMaxHeight() != -1 && adjustR
.GetHeight() > toplevelWindow
->GetMaxHeight() )
620 adjustR
.SetHeight( toplevelWindow
->GetMaxHeight() ) ;
621 if ( toplevelWindow
->GetMinWidth() != -1 && adjustR
.GetWidth() < toplevelWindow
->GetMinWidth() )
622 adjustR
.SetWidth( toplevelWindow
->GetMinWidth() ) ;
623 if ( toplevelWindow
->GetMinHeight() != -1 && adjustR
.GetHeight() < toplevelWindow
->GetMinHeight() )
624 adjustR
.SetHeight( toplevelWindow
->GetMinHeight() ) ;
625 Rect adjustedRect
= { adjustR
.y
+ top
, adjustR
.x
+ left
, adjustR
.y
+ adjustR
.height
- bottom
, adjustR
.x
+ adjustR
.width
- right
} ;
626 if ( !EqualRect( &newRect
, &adjustedRect
) )
627 cEvent
.SetParameter( kEventParamCurrentBounds
, &adjustedRect
) ;
639 pascal OSStatus
wxMacTopLevelEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
641 OSStatus result
= eventNotHandledErr
;
643 switch ( GetEventClass( event
) )
645 case kEventClassKeyboard
:
646 result
= KeyboardEventHandler( handler
, event
, data
) ;
648 case kEventClassTextInput
:
649 result
= TextInputEventHandler( handler
, event
, data
) ;
651 case kEventClassWindow
:
652 result
= wxMacTopLevelWindowEventHandler( handler
, event
, data
) ;
654 case kEventClassMouse
:
655 result
= wxMacTopLevelMouseEventHandler( handler
, event
, data
) ;
663 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler
)
665 // ---------------------------------------------------------------------------
666 // wxWindowMac utility functions
667 // ---------------------------------------------------------------------------
669 // Find an item given the Macintosh Window Reference
671 wxList
wxWinMacWindowList(wxKEY_INTEGER
);
672 wxTopLevelWindowMac
*wxFindWinFromMacWindow(WindowRef inWindowRef
)
674 wxNode
*node
= wxWinMacWindowList
.Find((long)inWindowRef
);
677 return (wxTopLevelWindowMac
*)node
->GetData();
680 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
) ;
681 void wxAssociateWinWithMacWindow(WindowRef inWindowRef
, wxTopLevelWindowMac
*win
)
683 // adding NULL WindowRef is (first) surely a result of an error and
684 // (secondly) breaks menu command processing
685 wxCHECK_RET( inWindowRef
!= (WindowRef
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
687 if ( !wxWinMacWindowList
.Find((long)inWindowRef
) )
688 wxWinMacWindowList
.Append((long)inWindowRef
, win
);
691 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
) ;
692 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac
*win
)
694 wxWinMacWindowList
.DeleteObject(win
);
698 // ----------------------------------------------------------------------------
699 // wxTopLevelWindowMac creation
700 // ----------------------------------------------------------------------------
702 wxTopLevelWindowMac
*wxTopLevelWindowMac::s_macDeactivateWindow
= NULL
;
704 void wxTopLevelWindowMac::Init()
707 m_maximizeOnShow
= FALSE
;
709 #if TARGET_API_MAC_OSX
710 m_macUsesCompositing
= TRUE
;
712 m_macUsesCompositing
= FALSE
;
714 m_macEventHandler
= NULL
;
717 class wxMacDeferredWindowDeleter
: public wxObject
720 wxMacDeferredWindowDeleter( WindowRef windowRef
)
722 m_macWindow
= windowRef
;
724 virtual ~wxMacDeferredWindowDeleter()
726 UMADisposeWindow( (WindowRef
) m_macWindow
) ;
729 WindowRef m_macWindow
;
732 bool wxTopLevelWindowMac::Create(wxWindow
*parent
,
734 const wxString
& title
,
738 const wxString
& name
)
743 m_windowStyle
= style
;
747 m_windowId
= id
== -1 ? NewControlId() : id
;
749 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
751 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
753 wxTopLevelWindows
.Append(this);
756 parent
->AddChild(this);
761 wxTopLevelWindowMac::~wxTopLevelWindowMac()
765 wxToolTip::NotifyWindowDelete(m_macWindow
) ;
766 wxPendingDelete
.Append( new wxMacDeferredWindowDeleter( (WindowRef
) m_macWindow
) ) ;
769 if ( m_macEventHandler
)
771 ::RemoveEventHandler((EventHandlerRef
) m_macEventHandler
);
772 m_macEventHandler
= NULL
;
775 wxRemoveMacWindowAssociation( this ) ;
777 if ( wxModelessWindows
.Find(this) )
778 wxModelessWindows
.DeleteObject(this);
782 // ----------------------------------------------------------------------------
783 // wxTopLevelWindowMac maximize/minimize
784 // ----------------------------------------------------------------------------
786 void wxTopLevelWindowMac::Maximize(bool maximize
)
788 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( (WindowRef
) m_macWindow
) ) ;
789 wxMacWindowClipper
clip (this);
790 ZoomWindow( (WindowRef
)m_macWindow
, maximize
? inZoomOut
: inZoomIn
, false ) ;
793 GDHandle device = NULL ;
794 verify_noerr( GetWindowGreatestAreaDevice( (WindowRef) m_macWindow , kWindowContentRgn ,
796 verify_noerr( GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ) ;
801 Point pt = { 0, 0 } ;
802 SetPortWindowPort((WindowRef)m_macWindow) ;
803 LocalToGlobal( &pt ) ;
806 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
807 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
808 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
812 bool wxTopLevelWindowMac::IsMaximized() const
814 return IsWindowInStandardState( (WindowRef
)m_macWindow
, NULL
, NULL
) ;
817 void wxTopLevelWindowMac::Iconize(bool iconize
)
819 if ( IsWindowCollapsable((WindowRef
)m_macWindow
) )
820 CollapseWindow((WindowRef
)m_macWindow
, iconize
) ;
823 bool wxTopLevelWindowMac::IsIconized() const
825 return IsWindowCollapsed((WindowRef
)m_macWindow
) ;
828 void wxTopLevelWindowMac::Restore()
830 // not available on mac
833 // ----------------------------------------------------------------------------
834 // wxTopLevelWindowMac misc
835 // ----------------------------------------------------------------------------
837 wxPoint
wxTopLevelWindowMac::GetClientAreaOrigin() const
839 return wxPoint(0,0) ;
842 void wxTopLevelWindowMac::SetIcon(const wxIcon
& icon
)
845 wxTopLevelWindowBase::SetIcon(icon
);
848 void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush
&brush
)
850 wxTopLevelWindowBase::MacSetBackgroundBrush( brush
) ;
852 if ( m_macBackgroundBrush
.Ok() && m_macBackgroundBrush
.GetStyle() != wxTRANSPARENT
&& m_macBackgroundBrush
.MacGetBrushKind() == kwxMacBrushTheme
)
854 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macBackgroundBrush
.MacGetTheme() , false ) ;
858 void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()
860 if ( m_macEventHandler
!= NULL
)
862 verify_noerr( ::RemoveEventHandler( (EventHandlerRef
) m_macEventHandler
) ) ;
864 InstallWindowEventHandler(MAC_WXHWND(m_macWindow
), GetwxMacTopLevelEventHandlerUPP(),
865 GetEventTypeCount(eventList
), eventList
, this, (EventHandlerRef
*)&m_macEventHandler
);
868 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString
& title
,
872 const wxString
& name
)
874 OSStatus err
= noErr
;
876 m_windowStyle
= style
;
890 int w
= WidthDefault(size
.x
);
891 int h
= HeightDefault(size
.y
);
893 ::SetRect(&theBoundsRect
, x
, y
, x
+ w
, y
+ h
);
895 // translate the window attributes in the appropriate window class and attributes
897 WindowClass wclass
= 0;
898 WindowAttributes attr
= kWindowNoAttributes
;
900 if ( HasFlag( wxFRAME_TOOL_WINDOW
) )
903 HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
904 HasFlag( wxSYSTEM_MENU
) || HasFlag( wxCAPTION
) ||
905 HasFlag(wxTINY_CAPTION_HORIZ
) || HasFlag(wxTINY_CAPTION_VERT
)
908 wclass
= kFloatingWindowClass
;
909 if ( HasFlag(wxTINY_CAPTION_VERT
) )
911 attr
|= kWindowSideTitlebarAttribute
;
916 wclass
= kPlainWindowClass
;
919 else if ( HasFlag( wxCAPTION
) )
921 wclass
= kDocumentWindowClass
;
925 if ( HasFlag( wxMINIMIZE_BOX
) || HasFlag( wxMAXIMIZE_BOX
) ||
926 HasFlag( wxCLOSE_BOX
) || HasFlag( wxSYSTEM_MENU
) )
928 wclass
= kDocumentWindowClass
;
932 wclass
= kPlainWindowClass
;
936 if ( HasFlag( wxMINIMIZE_BOX
) )
938 attr
|= kWindowCollapseBoxAttribute
;
940 if ( HasFlag( wxMAXIMIZE_BOX
) )
942 attr
|= kWindowFullZoomAttribute
;
944 if ( HasFlag( wxRESIZE_BORDER
) )
946 attr
|= kWindowResizableAttribute
;
948 if ( HasFlag( wxCLOSE_BOX
) )
950 attr
|= kWindowCloseBoxAttribute
;
953 if (UMAGetSystemVersion() >= 0x1000)
955 // turn on live resizing (OS X only)
956 attr
|= kWindowLiveResizeAttribute
;
959 if (HasFlag(wxSTAY_ON_TOP
))
960 wclass
= kUtilityWindowClass
;
962 #if TARGET_API_MAC_OSX
963 attr
|= kWindowCompositingAttribute
;
966 if ( HasFlag(wxFRAME_SHAPED
) )
968 WindowDefSpec customWindowDefSpec
;
969 customWindowDefSpec
.defType
= kWindowDefProcPtr
;
970 customWindowDefSpec
.u
.defProc
= NewWindowDefUPP(wxShapedMacWindowDef
);
972 err
= ::CreateCustomWindow( &customWindowDefSpec
, wclass
,
973 attr
, &theBoundsRect
,
974 (WindowRef
*) &m_macWindow
);
978 err
= ::CreateNewWindow( wclass
, attr
, &theBoundsRect
, (WindowRef
*)&m_macWindow
) ;
981 wxCHECK_RET( err
== noErr
, wxT("Mac OS error when trying to create new window") );
983 // the create commands are only for content rect, so we have to set the size again as
985 SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &theBoundsRect
) ;
987 wxAssociateWinWithMacWindow( (WindowRef
) m_macWindow
, this ) ;
988 UMASetWTitle( (WindowRef
) m_macWindow
, title
, m_font
.GetEncoding() ) ;
989 if ( m_macUsesCompositing
)
991 ::GetRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
995 ::CreateRootControl( (WindowRef
)m_macWindow
, (ControlRef
*)&m_macControl
) ;
997 // the root control level handleer
998 MacInstallEventHandler() ;
1000 // the frame window event handler
1001 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow
)) ) ;
1002 MacInstallTopLevelWindowEventHandler() ;
1006 if ( HasFlag(wxFRAME_SHAPED
) )
1008 // default shape matches the window size
1009 wxRegion
rgn(0, 0, w
, h
);
1013 wxWindowCreateEvent
event(this);
1014 GetEventHandler()->ProcessEvent(event
);
1017 void wxTopLevelWindowMac::ClearBackground()
1019 wxWindow::ClearBackground() ;
1022 // Raise the window to the top of the Z order
1023 void wxTopLevelWindowMac::Raise()
1025 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1028 // Lower the window to the bottom of the Z order
1029 void wxTopLevelWindowMac::Lower()
1031 ::SendBehind( (WindowRef
)m_macWindow
, NULL
) ;
1035 void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp
)
1037 if(s_macDeactivateWindow
)
1039 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow
);
1040 s_macDeactivateWindow
->MacActivate(timestamp
, false);
1044 void wxTopLevelWindowMac::MacActivate( long timestamp
, bool inIsActivating
)
1046 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1048 if(s_macDeactivateWindow
==this)
1049 s_macDeactivateWindow
=NULL
;
1050 MacDelayedDeactivation(timestamp
);
1051 MacPropagateHiliteChanged() ;
1054 void wxTopLevelWindowMac::SetTitle(const wxString
& title
)
1056 wxWindow::SetTitle( title
) ;
1057 UMASetWTitle( (WindowRef
)m_macWindow
, title
, m_font
.GetEncoding() ) ;
1060 bool wxTopLevelWindowMac::Show(bool show
)
1062 if ( !wxTopLevelWindowBase::Show(show
) )
1067 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
1068 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1070 ::ShowWindow( (WindowRef
)m_macWindow
);
1075 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowShowTransitionAction
,nil
);
1077 ::SelectWindow( (WindowRef
)m_macWindow
) ;
1078 // as apps expect a size event to occur at this moment
1079 wxSizeEvent
event( GetSize() , m_windowId
);
1080 event
.SetEventObject(this);
1081 GetEventHandler()->ProcessEvent(event
);
1085 #if wxUSE_SYSTEM_OPTIONS
1086 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION
) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION
) == 1) )
1088 ::HideWindow((WindowRef
) m_macWindow
);
1093 ::TransitionWindow((WindowRef
)m_macWindow
,kWindowZoomTransitionEffect
,kWindowHideTransitionAction
,nil
);
1097 MacPropagateVisibilityChanged() ;
1102 // we are still using coordinates of the content view, todo switch to structure bounds
1104 void wxTopLevelWindowMac::MacGetContentAreaInset( int &left
, int &top
, int &right
, int &bottom
)
1108 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &structure
) ;
1109 GetWindowBounds( (WindowRef
) m_macWindow
, kWindowContentRgn
, &content
) ;
1111 left
= content
.left
- structure
.left
;
1112 top
= content
.top
- structure
.top
;
1113 right
= structure
.right
- content
.right
;
1114 bottom
= structure
.bottom
- content
.bottom
;
1117 void wxTopLevelWindowMac::DoMoveWindow(int x
, int y
, int width
, int height
)
1119 Rect bounds
= { y
, x
, y
+ height
, x
+ width
} ;
1120 verify_noerr(SetWindowBounds( (WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1123 void wxTopLevelWindowMac::DoGetPosition( int *x
, int *y
) const
1126 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1127 if(x
) *x
= bounds
.left
;
1128 if(y
) *y
= bounds
.top
;
1130 void wxTopLevelWindowMac::DoGetSize( int *width
, int *height
) const
1133 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowStructureRgn
, &bounds
)) ;
1134 if(width
) *width
= bounds
.right
- bounds
.left
;
1135 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1138 void wxTopLevelWindowMac::DoGetClientSize( int *width
, int *height
) const
1141 verify_noerr(GetWindowBounds((WindowRef
) m_macWindow
, kWindowContentRgn
, &bounds
)) ;
1142 if(width
) *width
= bounds
.right
- bounds
.left
;
1143 if(height
) *height
= bounds
.bottom
- bounds
.top
;
1146 void wxTopLevelWindowMac::MacSetMetalAppearance( bool set
)
1148 #if TARGET_API_MAC_OSX
1150 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1151 wxASSERT_MSG( attr
& kWindowCompositingAttribute
,
1152 wxT("Cannot set metal appearance on a non-compositing window") ) ;
1154 MacChangeWindowAttributes( set
? kWindowMetalAttribute
: kWindowNoAttributes
,
1155 set
? kWindowNoAttributes
: kWindowMetalAttribute
) ;
1159 bool wxTopLevelWindowMac::MacGetMetalAppearance() const
1161 #if TARGET_API_MAC_OSX
1162 return MacGetWindowAttributes() & kWindowMetalAttribute
;
1168 void wxTopLevelWindowMac::MacChangeWindowAttributes( wxUint32 attributesToSet
, wxUint32 attributesToClear
)
1170 ChangeWindowAttributes ( (WindowRef
) m_macWindow
, attributesToSet
, attributesToClear
) ;
1173 wxUint32
wxTopLevelWindowMac::MacGetWindowAttributes() const
1176 GetWindowAttributes((WindowRef
) m_macWindow
, &attr
) ;
1180 // ---------------------------------------------------------------------------
1181 // Shape implementation
1182 // ---------------------------------------------------------------------------
1185 bool wxTopLevelWindowMac::SetShape(const wxRegion
& region
)
1187 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), FALSE
,
1188 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1190 // The empty region signifies that the shape should be removed from the
1192 if ( region
.IsEmpty() )
1194 wxSize sz
= GetClientSize();
1195 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
1196 return SetShape(rgn
);
1199 // Make a copy of the region
1200 RgnHandle shapeRegion
= NewRgn();
1201 CopyRgn( (RgnHandle
)region
.GetWXHRGN(), shapeRegion
);
1203 // Dispose of any shape region we may already have
1204 RgnHandle oldRgn
= (RgnHandle
)GetWRefCon( (WindowRef
)MacGetWindowRef() );
1208 // Save the region so we can use it later
1209 SetWRefCon((WindowRef
)MacGetWindowRef(), (SInt32
)shapeRegion
);
1211 // Tell the window manager that the window has changed shape
1212 ReshapeCustomWindow((WindowRef
)MacGetWindowRef());
1216 // ---------------------------------------------------------------------------
1217 // Support functions for shaped windows, based on Apple's CustomWindow sample at
1218 // http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1219 // ---------------------------------------------------------------------------
1221 static void wxShapedMacWindowGetPos(WindowRef window
, Rect
* inRect
)
1223 GetWindowPortBounds(window
, inRect
);
1224 Point pt
= {inRect
->left
, inRect
->top
};
1225 SetPort((GrafPtr
) GetWindowPort(window
));
1228 inRect
->left
= pt
.h
;
1229 inRect
->bottom
+= pt
.v
;
1230 inRect
->right
+= pt
.h
;
1234 static SInt32
wxShapedMacWindowGetFeatures(WindowRef window
, SInt32 param
)
1236 /*------------------------------------------------------
1237 Define which options your custom window supports.
1238 --------------------------------------------------------*/
1239 //just enable everything for our demo
1240 *(OptionBits
*)param
=//kWindowCanGrow|
1242 //kWindowCanCollapse|
1243 //kWindowCanGetWindowRegion|
1244 //kWindowHasTitleBar|
1245 //kWindowSupportsDragHilite|
1246 kWindowCanDrawInCurrentPort
|
1247 //kWindowCanMeasureTitle|
1248 kWindowWantsDisposeAtProcessDeath
|
1249 kWindowSupportsSetGrowImageRegion
|
1250 kWindowDefSupportsColorGrafPort
;
1254 // The content region is left as a rectangle matching the window size, this is
1255 // so the origin in the paint event, and etc. still matches what the
1256 // programmer expects.
1257 static void wxShapedMacWindowContentRegion(WindowRef window
, RgnHandle rgn
)
1260 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow(window
);
1264 wxShapedMacWindowGetPos(window
, &r
) ;
1265 RectRgn( rgn
, &r
) ;
1269 // The structure region is set to the shape given to the SetShape method.
1270 static void wxShapedMacWindowStructureRegion(WindowRef window
, RgnHandle rgn
)
1272 RgnHandle cachedRegion
= (RgnHandle
) GetWRefCon(window
);
1278 wxShapedMacWindowGetPos(window
, &windowRect
); //how big is the window
1279 CopyRgn(cachedRegion
, rgn
); //make a copy of our cached region
1280 OffsetRgn(rgn
, windowRect
.left
, windowRect
.top
); // position it over window
1281 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1287 static SInt32
wxShapedMacWindowGetRegion(WindowRef window
, SInt32 param
)
1289 GetWindowRegionPtr rgnRec
=(GetWindowRegionPtr
)param
;
1291 switch(rgnRec
->regionCode
)
1293 case kWindowStructureRgn
:
1294 wxShapedMacWindowStructureRegion(window
, rgnRec
->winRgn
);
1296 case kWindowContentRgn
:
1297 wxShapedMacWindowContentRegion(window
, rgnRec
->winRgn
);
1300 SetEmptyRgn(rgnRec
->winRgn
);
1307 static SInt32
wxShapedMacWindowHitTest(WindowRef window
,SInt32 param
)
1309 /*------------------------------------------------------
1310 Determine the region of the window which was hit
1311 --------------------------------------------------------*/
1313 static RgnHandle tempRgn
=nil
;
1318 SetPt(&hitPoint
,LoWord(param
),HiWord(param
));//get the point clicked
1320 //Mac OS 8.5 or later
1321 wxShapedMacWindowStructureRegion(window
, tempRgn
);
1322 if (PtInRgn(hitPoint
, tempRgn
)) //in window content region?
1325 return wNoHit
;//no significant area was hit.
1329 static pascal long wxShapedMacWindowDef(short varCode
, WindowRef window
, SInt16 message
, SInt32 param
)
1333 case kWindowMsgHitTest
:
1334 return wxShapedMacWindowHitTest(window
,param
);
1336 case kWindowMsgGetFeatures
:
1337 return wxShapedMacWindowGetFeatures(window
,param
);
1339 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1340 case kWindowMsgGetRegion
:
1341 return wxShapedMacWindowGetRegion(window
,param
);
1347 // ---------------------------------------------------------------------------