1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 #include "wx/control.h"
20 #include "wx/notebook.h"
21 #include "wx/tabctrl.h"
22 #include "wx/radiobox.h"
23 #include "wx/spinbutt.h"
24 #include "wx/scrolbar.h"
25 #include "wx/button.h"
26 #include "wx/dialog.h"
27 #include "wx/statbox.h"
29 #include "wx/stattext.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
34 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
35 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
36 EVT_CHAR( wxControl::OnKeyDown
)
37 EVT_PAINT( wxControl::OnPaint
)
41 #include <wx/mac/uma.h>
45 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
47 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
48 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
52 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
55 wx
->MacHandleControlClick( control
, partCode
) ;
60 wxControl::wxControl()
63 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
64 m_macVerticalBorder
= 0 ;
65 m_backgroundColour
= *wxWHITE
;
66 m_foregroundColour
= *wxBLACK
;
67 #if WXWIN_COMPATIBILITY
69 #endif // WXWIN_COMPATIBILITY
71 if ( wxMacLiveScrollbarActionUPP
== NULL
)
73 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
74 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
76 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
81 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
83 const wxSize
& size
, long style
,
84 const wxValidator
& validator
,
88 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
89 m_macVerticalBorder
= 0 ;
90 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
93 SetValidator(validator
);
99 wxControl::~wxControl()
101 m_isBeingDeleted
= TRUE
;
102 // If we delete an item, we should initialize the parent panel,
103 // because it could now be invalid.
104 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
107 if (panel
->GetDefaultItem() == (wxButton
*) this)
108 panel
->SetDefaultItem(NULL
);
112 UMADisposeControl( m_macControl
) ;
113 m_macControl
= NULL
;
117 void wxControl::SetLabel(const wxString
& title
)
126 if( wxApp::s_macDefaultEncodingIsPC
)
127 label
= wxMacMakeMacStringFromPC( title
) ;
132 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
134 strcpy( (char *) maclabel
, label
) ;
135 c2pstr( (char *) maclabel
) ;
137 ::SetControlTitle( m_macControl
, maclabel
) ;
142 wxSize
wxControl::DoGetBestSize() const
144 return wxSize(20, 20);
147 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
150 // 1) A callback function (to become obsolete)
151 // 2) OnCommand, starting at this window and working up parent hierarchy
152 // 3) OnCommand then calls ProcessEvent to search the event tables.
153 #if WXWIN_COMPATIBILITY
156 (void)(*m_callback
)(this, event
);
161 #endif // WXWIN_COMPATIBILITY
163 return GetEventHandler()->ProcessEvent(event
);
167 // ------------------------
168 wxList
*wxWinMacControlList
= NULL
;
169 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
171 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
174 return (wxControl
*)node
->Data();
177 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
179 // adding NULL WindowRef is (first) surely a result of an error and
180 // (secondly) breaks menu command processing
181 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
183 if ( !wxWinMacControlList
->Find((long)inControl
) )
184 wxWinMacControlList
->Append((long)inControl
, control
);
187 void wxRemoveMacControlAssociation(wxControl
*control
)
189 wxWinMacControlList
->DeleteObject(control
);
192 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
194 const wxSize
& size
, long style
,
195 const wxValidator
& validator
,
196 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
201 SetValidator(validator
);
203 m_windowStyle
= style
;
204 parent
->AddChild((wxButton
*)this);
206 m_backgroundColour
= parent
->GetBackgroundColour() ;
207 m_foregroundColour
= parent
->GetForegroundColour() ;
210 m_windowId
= NewControlId();
218 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
223 parent
->MacClientToRootWindow( &x
, &y
) ;
224 outBounds
->top
= y
+ m_macVerticalBorder
;
225 outBounds
->left
= x
+ m_macHorizontalBorder
;
226 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
227 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
230 strcpy( c_text
, label
) ;
231 if( wxApp::s_macDefaultEncodingIsPC
)
233 wxMacConvertFromPCForControls( c_text
) ;
237 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
239 strcpy( (char *) maclabel
, c_text
) ;
240 c2pstr( (char *) maclabel
) ;
244 void wxControl::MacPostControlCreate()
246 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
248 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
252 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
254 ControlFontStyleRec controlstyle
;
255 controlstyle
.flags
= kControlUseFontMask
;
256 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
258 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
262 ControlFontStyleRec controlstyle
;
263 controlstyle
.flags
= kControlUseFontMask
;
264 controlstyle
.font
= kControlFontSmallSystemFont
;
266 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
268 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
269 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
270 ::UMAEmbedControl( m_macControl
, container
) ;
271 m_macControlIsShown
= true ;
272 MacAdjustControlRect() ;
273 wxAssociateControlWithMacControl( m_macControl
, this ) ;
274 UMAShowControl( m_macControl
) ;
277 void wxControl::MacAdjustControlRect()
279 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
280 if ( m_width
== -1 || m_height
== -1 )
282 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
283 short baselineoffset
;
285 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
287 if ( EmptyRect( &bestsize
) )
290 bestsize
.left
= bestsize
.top
= 0 ;
291 bestsize
.right
= 16 ;
292 bestsize
.bottom
= 16 ;
293 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
295 bestsize
.bottom
= 16 ;
297 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
299 bestsize
.bottom
= 24 ;
305 if ( IsKindOf( CLASSINFO( wxButton
) ) )
307 m_width
= m_label
.Length() * 8 + 12 ;
311 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
313 m_width
= m_label
.Length() * 8 ;
316 m_width
= bestsize
.right
- bestsize
.left
;
318 m_width
+= 2 * m_macHorizontalBorder
;
320 if ( m_height
== -1 )
322 m_height
= bestsize
.bottom
- bestsize
.top
;
326 m_height
+= 2 * m_macVerticalBorder
;
329 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
332 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
336 ControlHandle
wxControl::MacGetContainerForEmbedding()
339 return m_macControl
;
341 return wxWindow::MacGetContainerForEmbedding() ;
344 void wxControl::MacSuperChangedPosition()
349 GetControlBounds( m_macControl
, &contrlRect
) ;
350 int former_mac_x
= contrlRect
.left
;
351 int former_mac_y
= contrlRect
.top
;
354 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
356 WindowRef rootwindow
= GetMacRootWindow() ;
357 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
358 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
359 wxMacDrawingHelper
focus( wxrootwindow
) ;
361 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
363 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
365 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
370 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
374 wxWindow::MacSuperChangedPosition() ;
377 void wxControl::MacSuperEnabled( bool enabled
)
382 if ( UMAHasAppearance() )
386 ::DeactivateControl( m_macControl ) ;
391 ::ActivateControl( m_macControl ) ;
398 ::HiliteControl( m_macControl , 255 ) ;
403 ::HiliteControl( m_macControl , 0 ) ;
407 wxWindow::MacSuperEnabled( enabled ) ;
411 void wxControl::MacSuperShown( bool show
)
417 if ( m_macControlIsShown
)
419 ::UMAHideControl( m_macControl
) ;
420 m_macControlIsShown
= false ;
425 if ( MacIsReallyShown() && !m_macControlIsShown
)
427 ::UMAShowControl( m_macControl
) ;
428 m_macControlIsShown
= true ;
433 wxWindow::MacSuperShown( show
) ;
436 void wxControl::DoSetSize(int x
, int y
,
437 int width
, int height
,
440 if ( m_macControl
== NULL
)
442 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
446 WindowRef rootwindow
= GetMacRootWindow() ;
447 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
448 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
452 int former_w
= m_width
;
453 int former_h
= m_height
;
456 GetControlBounds( m_macControl
, &contrlRect
) ;
457 int former_mac_x
= contrlRect
.left
;
458 int former_mac_y
= contrlRect
.top
;
460 int currentX
, currentY
;
461 GetPosition(¤tX
, ¤tY
);
462 int currentW
,currentH
;
463 GetSize(¤tW
, ¤tH
);
465 int actualWidth
= width
;
466 int actualHeight
= height
;
469 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
471 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
474 actualWidth
= currentW
;
476 actualHeight
= currentH
;
478 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
481 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
482 WindowRef macrootwindow
= GetMacRootWindow() ;
483 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
485 int mac_x
= actualX
;
486 int mac_y
= actualY
;
487 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
489 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
491 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
494 if ( actualX
!= former_x
|| actualY
!= former_y
)
499 MacRepositionScrollBars() ;
500 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
501 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
502 event
.SetEventObject(this);
503 GetEventHandler()->ProcessEvent(event
);
505 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
508 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
509 InvalWindowRect( macrootwindow
, &inval
) ;
511 m_width
= actualWidth
;
512 m_height
= actualHeight
;
514 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
516 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
517 InvalWindowRect( macrootwindow
, &inval
) ;
520 MacRepositionScrollBars() ;
521 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
522 event
.SetEventObject(this);
523 GetEventHandler()->ProcessEvent(event
);
525 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
530 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
534 bool wxControl::Show(bool show
)
536 if ( !wxWindow::Show( show
) )
543 if ( m_macControlIsShown
)
545 ::UMAHideControl( m_macControl
) ;
546 m_macControlIsShown
= false ;
551 if ( MacIsReallyShown() && !m_macControlIsShown
)
553 ::UMAShowControl( m_macControl
) ;
554 m_macControlIsShown
= true ;
561 bool wxControl::Enable(bool enable
)
563 if ( !wxWindow::Enable(enable
) )
569 UMAActivateControl( m_macControl
) ;
571 UMADeactivateControl( m_macControl
) ;
576 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
580 wxWindow::Refresh( eraseBack
, rect
) ;
584 wxWindow::Refresh( eraseBack
, rect
) ;
588 void wxControl::MacRedrawControl()
592 WindowRef window
= GetMacRootWindow() ;
595 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
598 wxMacDrawingHelper
help( win
) ;
599 // the mac control manager always assumes to have the origin at 0,0
602 bool hasTabBehind
= false ;
603 wxWindow
* parent
= GetParent() ;
606 if( parent
->MacGetWindowData() )
608 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
612 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
614 if ( ((wxControl
*)parent
)->m_macControl
)
615 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
619 parent
= parent
->GetParent() ;
622 UMADrawControl( m_macControl
) ;
623 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
629 void wxControl::OnPaint(wxPaintEvent
& event
)
633 WindowRef window
= GetMacRootWindow() ;
636 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
639 wxMacDrawingHelper
help( win
) ;
640 // the mac control manager always assumes to have the origin at 0,0
644 bool hasTabBehind
= false ;
645 wxWindow
* parent
= GetParent() ;
648 if( parent
->MacGetWindowData() )
650 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
654 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
656 if ( ((wxControl
*)parent
)->m_macControl
)
657 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
661 parent
= parent
->GetParent() ;
664 UMADrawControl( m_macControl
) ;
665 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
671 // wxWindow::OnPaint( event ) ;
674 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
676 // In general, you don't want to erase the background of a control,
677 // or you'll get a flicker.
678 // TODO: move this 'null' function into each control that
683 void wxControl::OnKeyDown( wxKeyEvent
&event
)
685 if ( m_macControl
== NULL
)
688 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
691 keychar
= short(ev
->message
& charCodeMask
);
692 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
694 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
697 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
699 if ( m_macControl
== NULL
)
705 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
711 MacClientToRootWindow( &x
, &y
) ;
713 ControlHandle control
;
717 WindowRef window
= GetMacRootWindow() ;
724 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
725 modifiers
|= btnState
;
727 if ( event
.m_shiftDown
)
728 modifiers
|= shiftKey
;
730 if ( event
.m_controlDown
)
731 modifiers
|= controlKey
;
733 if ( event
.m_altDown
)
734 modifiers
|= optionKey
;
736 if ( event
.m_metaDown
)
737 modifiers
|= cmdKey
;
739 controlpart
= FindControl( localwhere
, window
, &control
) ;
742 if ( AcceptsFocus() && FindFocus() != this )
747 if ( control
&& UMAIsControlActive( control
) )
750 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
751 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
753 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
754 wxTheApp
->s_lastMouseDown
= 0 ;
755 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
756 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
758 MacHandleControlClick( control
, controlpart
) ;
766 bool wxControl::MacCanFocus() const
768 { if ( m_macControl
== NULL
)
775 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
777 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;