1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
18 #include "wx/control.h"
22 #include "wx/dcclient.h"
23 #include "wx/notebook.h"
24 #include "wx/tabctrl.h"
25 #include "wx/radiobox.h"
26 #include "wx/spinbutt.h"
27 #include "wx/scrolbar.h"
28 #include "wx/button.h"
29 #include "wx/dialog.h"
30 #include "wx/statbox.h"
32 #include "wx/stattext.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
37 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
38 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
39 EVT_CHAR( wxControl::OnKeyDown
)
40 EVT_PAINT( wxControl::OnPaint
)
44 #include "wx/mac/uma.h"
48 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
50 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
51 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
55 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
58 wx
->MacHandleControlClick( control
, partCode
) ;
63 wxControl::wxControl()
66 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
67 m_macVerticalBorder
= 0 ;
68 m_backgroundColour
= *wxWHITE
;
69 m_foregroundColour
= *wxBLACK
;
70 #if WXWIN_COMPATIBILITY
72 #endif // WXWIN_COMPATIBILITY
74 if ( wxMacLiveScrollbarActionUPP
== NULL
)
76 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
77 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
79 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
84 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
86 const wxSize
& size
, long style
,
87 const wxValidator
& validator
,
91 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
92 m_macVerticalBorder
= 0 ;
93 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
96 SetValidator(validator
);
102 wxControl::~wxControl()
104 m_isBeingDeleted
= TRUE
;
105 // If we delete an item, we should initialize the parent panel,
106 // because it could now be invalid.
107 wxWindow
*parent
= GetParent() ;
110 if (parent
->GetDefaultItem() == (wxButton
*) this)
111 parent
->SetDefaultItem(NULL
);
113 if ( (ControlHandle
) m_macControl
)
115 ::DisposeControl( (ControlHandle
) m_macControl
) ;
116 m_macControl
= NULL
;
120 void wxControl::SetLabel(const wxString
& title
)
124 if ( (ControlHandle
) m_macControl
)
129 if( wxApp::s_macDefaultEncodingIsPC
)
130 label
= wxMacMakeMacStringFromPC( title
) ;
135 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
137 strcpy( (char *) maclabel
, label
) ;
138 c2pstr( (char *) maclabel
) ;
140 ::SetControlTitle( (ControlHandle
) m_macControl
, maclabel
) ;
145 wxSize
wxControl::DoGetBestSize() const
147 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
148 short baselineoffset
;
149 int bestWidth
, bestHeight
;
150 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
152 if ( EmptyRect( &bestsize
) )
155 bestsize
.left
= bestsize
.top
= 0 ;
156 bestsize
.right
= 16 ;
157 bestsize
.bottom
= 16 ;
158 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
160 bestsize
.bottom
= 16 ;
162 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
164 bestsize
.bottom
= 24 ;
168 if ( IsKindOf( CLASSINFO( wxButton
) ) )
170 bestWidth
= m_label
.Length() * 8 + 12 ;
171 if ( bestWidth
< 70 )
174 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
176 bestWidth
= m_label
.Length() * 8 ;
179 bestWidth
= bestsize
.right
- bestsize
.left
;
181 bestWidth
+= 2 * m_macHorizontalBorder
;
183 bestHeight
= bestsize
.bottom
- bestsize
.top
;
184 if ( bestHeight
< 10 )
187 bestHeight
+= 2 * m_macVerticalBorder
;
190 return wxSize(bestWidth
, bestHeight
);
193 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
196 // 1) A callback function (to become obsolete)
197 // 2) OnCommand, starting at this window and working up parent hierarchy
198 // 3) OnCommand then calls ProcessEvent to search the event tables.
199 #if WXWIN_COMPATIBILITY
202 (void)(*m_callback
)(this, event
);
207 #endif // WXWIN_COMPATIBILITY
209 return GetEventHandler()->ProcessEvent(event
);
213 // ------------------------
214 wxList
*wxWinMacControlList
= NULL
;
215 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
217 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
220 return (wxControl
*)node
->Data();
223 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
225 // adding NULL WindowRef is (first) surely a result of an error and
226 // (secondly) breaks menu command processing
227 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
229 if ( !wxWinMacControlList
->Find((long)inControl
) )
230 wxWinMacControlList
->Append((long)inControl
, control
);
233 void wxRemoveMacControlAssociation(wxControl
*control
)
235 wxWinMacControlList
->DeleteObject(control
);
238 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
240 const wxSize
& size
, long style
,
241 const wxValidator
& validator
,
242 const wxString
& name
, WXRECTPTR outBounds
, unsigned char* maclabel
)
247 SetValidator(validator
);
249 m_windowStyle
= style
;
250 parent
->AddChild(this);
252 m_backgroundColour
= parent
->GetBackgroundColour() ;
253 m_foregroundColour
= parent
->GetForegroundColour() ;
256 m_windowId
= NewControlId();
260 // These sizes will be adjusted in MacPostControlCreate
267 ((Rect
*)outBounds
)->top
= -10;
268 ((Rect
*)outBounds
)->left
= -10;
269 ((Rect
*)outBounds
)->bottom
= 0;
270 ((Rect
*)outBounds
)->right
= 0;
273 strcpy( c_text
, label
) ;
274 if( wxApp::s_macDefaultEncodingIsPC
)
276 wxMacConvertFromPCForControls( c_text
) ;
280 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
282 strcpy( (char *) maclabel
, c_text
) ;
283 c2pstr( (char *) maclabel
) ;
287 void wxControl::MacPostControlCreate()
289 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, "No valid mac control" ) ;
291 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
295 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
297 ControlFontStyleRec controlstyle
;
298 controlstyle
.flags
= kControlUseFontMask
;
299 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
301 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
305 ControlFontStyleRec controlstyle
;
306 controlstyle
.flags
= kControlUseFontMask
;
307 controlstyle
.font
= kControlFontSmallSystemFont
;
309 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
311 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
312 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
313 ::EmbedControl( (ControlHandle
) m_macControl
, container
) ;
314 m_macControlIsShown
= true ;
316 wxAssociateControlWithMacControl( (ControlHandle
) m_macControl
, this ) ;
319 // Adjust the controls size and position
320 wxPoint
pos(m_x
, m_y
);
321 wxSize
best_size( DoGetBestSize() );
322 wxSize
new_size( m_width
, m_height
);
324 m_x
= m_y
= m_width
= m_height
= -1; // Forces SetSize to move/size the control
326 if (new_size
.x
== -1) {
327 new_size
.x
= best_size
.x
;
329 if (new_size
.y
== -1) {
330 new_size
.y
= best_size
.y
;
333 SetSize(pos
.x
, pos
.y
, new_size
.x
, new_size
.y
);
335 UMAShowControl( (ControlHandle
) m_macControl
) ;
339 void wxControl::MacAdjustControlRect()
341 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
342 if ( m_width
== -1 || m_height
== -1 )
344 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
345 short baselineoffset
;
347 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
349 if ( EmptyRect( &bestsize
) )
352 bestsize
.left
= bestsize
.top
= 0 ;
353 bestsize
.right
= 16 ;
354 bestsize
.bottom
= 16 ;
355 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
357 bestsize
.bottom
= 16 ;
359 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
361 bestsize
.bottom
= 24 ;
367 if ( IsKindOf( CLASSINFO( wxButton
) ) )
369 m_width
= m_label
.Length() * 8 + 12 ;
373 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
375 m_width
= m_label
.Length() * 8 ;
378 m_width
= bestsize
.right
- bestsize
.left
;
380 m_width
+= 2 * m_macHorizontalBorder
;
382 if ( m_height
== -1 )
384 m_height
= bestsize
.bottom
- bestsize
.top
;
388 m_height
+= 2 * m_macVerticalBorder
;
391 UMASizeControl( (ControlHandle
) m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
395 WXWidget
wxControl::MacGetContainerForEmbedding()
398 return m_macControl
;
400 return wxWindow::MacGetContainerForEmbedding() ;
403 void wxControl::MacSuperChangedPosition()
405 if ( (ControlHandle
) m_macControl
)
408 GetControlBounds( (ControlHandle
) m_macControl
, &contrlRect
) ;
409 int former_mac_x
= contrlRect
.left
;
410 int former_mac_y
= contrlRect
.top
;
413 GetParent()->MacWindowToRootWindow( & mac_x
, & mac_y
) ;
415 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
417 if ( mac_x
+ m_macHorizontalBorder
!= former_mac_x
||
418 mac_y
+ m_macVerticalBorder
!= former_mac_y
)
421 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
422 InvalWindowRect( rootwindow
, &inval
) ;
424 UMAMoveControl( (ControlHandle
) m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
426 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
427 InvalWindowRect( rootwindow
, &inval
) ;
432 wxWindow::MacSuperChangedPosition() ;
435 void wxControl::MacSuperEnabled( bool enabled
)
438 wxWindow::MacSuperEnabled( enabled
) ;
441 void wxControl::MacSuperShown( bool show
)
443 if ( (ControlHandle
) m_macControl
)
447 if ( m_macControlIsShown
)
449 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
450 m_macControlIsShown
= false ;
455 if ( MacIsReallyShown() && !m_macControlIsShown
)
457 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
458 m_macControlIsShown
= true ;
463 wxWindow::MacSuperShown( show
) ;
466 void wxControl::DoSetSize(int x
, int y
,
467 int width
, int height
,
470 if ( (ControlHandle
) m_macControl
== NULL
)
472 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
477 int new_x
, new_y
, new_width
, new_height
;
483 new_height
= m_height
;
485 if (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
)
494 if (x
!= -1) new_x
= x
;
495 if (y
!= -1) new_y
= y
;
496 if (width
!= -1) new_width
= width
;
497 if (height
!= -1) new_height
= height
;
500 if(sizeFlags
& wxSIZE_AUTO
)
502 wxSize size
= GetBestSize();
503 if (sizeFlags
& wxSIZE_AUTO_WIDTH
)
505 if (width
== -1) new_width
= size
.x
;
507 if (sizeFlags
& wxSIZE_AUTO_HEIGHT
)
509 if (height
== -1) new_height
= size
.y
;
512 AdjustForParentClientOrigin(new_x
, new_y
, sizeFlags
);
517 GetParent()->MacWindowToRootWindow(&mac_x
, &mac_y
);
519 GetControlBounds( (ControlHandle
) m_macControl
, &oldbounds
);
520 oldbounds
.right
= oldbounds
.left
+ m_width
;
521 oldbounds
.bottom
= oldbounds
.top
+ m_height
;
524 bool doResize
= false;
526 if ( mac_x
!= (oldbounds
.left
- m_macHorizontalBorder
) ||
527 mac_y
!= (oldbounds
.top
- m_macVerticalBorder
) )
531 if ( new_width
!= oldbounds
.right
- oldbounds
.left
- 2 * m_macHorizontalBorder
||
532 new_height
!= oldbounds
.bottom
- oldbounds
.top
- 2 * m_macVerticalBorder
)
537 if ( doMove
|| doResize
)
541 // Ensure resize is within constraints
542 if ((m_minWidth
!= -1) && (new_width
< m_minWidth
)) {
543 new_width
= m_minWidth
;
545 if ((m_minHeight
!= -1) && (new_height
< m_minHeight
)) {
546 new_height
= m_minHeight
;
548 if ((m_maxWidth
!= -1) && (new_width
> m_maxWidth
)) {
549 new_width
= m_maxWidth
;
551 if ((m_maxHeight
!= -1) && (new_height
> m_maxHeight
)) {
552 new_height
= m_maxHeight
;
560 UMAMoveControl( (ControlHandle
) m_macControl
,
561 mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
);
563 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
564 event
.SetEventObject(this);
565 GetEventHandler()->ProcessEvent(event
) ;
570 m_height
= new_height
;
572 UMASizeControl( (ControlHandle
) m_macControl
,
573 m_width
- 2 * m_macHorizontalBorder
,
574 m_height
- 2 * m_macVerticalBorder
) ;
577 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
578 event
.SetEventObject(this);
579 GetEventHandler()->ProcessEvent(event
);
586 bool wxControl::Show(bool show
)
588 if ( !wxWindow::Show( show
) )
591 if ( (ControlHandle
) m_macControl
)
595 if ( m_macControlIsShown
)
597 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
598 m_macControlIsShown
= false ;
603 if ( MacIsReallyShown() && !m_macControlIsShown
)
605 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
606 m_macControlIsShown
= true ;
613 bool wxControl::Enable(bool enable
)
615 if ( !wxWindow::Enable(enable
) )
618 if ( (ControlHandle
) m_macControl
)
621 UMAActivateControl( (ControlHandle
) m_macControl
) ;
623 UMADeactivateControl( (ControlHandle
) m_macControl
) ;
628 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
630 wxWindow::Refresh( eraseBack
, rect
) ;
633 void wxControl::MacRedrawControl()
635 if ( (ControlHandle
) m_macControl
&& MacGetRootWindow() )
637 wxClientDC
dc(this) ;
638 wxMacPortSetter
helper(&dc
) ;
640 // the controls sometimes draw outside their boundaries, this
641 // should be resolved differently but is not trivial (e.g. drop shadows)
642 // since adding them to the border would yield in enormous gaps between
644 Rect r
= { 0 , 0 , 32000 , 32000 } ;
646 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
647 UMADrawControl( (ControlHandle
) m_macControl
) ;
651 void wxControl::OnPaint(wxPaintEvent
& event
)
653 if ( (ControlHandle
) m_macControl
)
656 wxMacPortSetter
helper(&dc
) ;
657 // the controls sometimes draw outside their boundaries, this
658 // should be resolved differently but is not trivial (e.g. drop shadows)
659 // since adding them to the border would yield in enormous gaps between
661 Rect r
= { 0 , 0 , 32000 , 32000 } ;
664 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
665 UMADrawControl( (ControlHandle
) m_macControl
) ;
672 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
674 wxWindow::OnEraseBackground( event
) ;
678 void wxControl::OnKeyDown( wxKeyEvent
&event
)
680 if ( (ControlHandle
) m_macControl
== NULL
)
683 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
686 keychar
= short(ev
->message
& charCodeMask
);
687 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
689 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
692 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
694 if ( (ControlHandle
) m_macControl
== NULL
)
700 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
706 MacClientToRootWindow( &x
, &y
) ;
708 ControlHandle control
;
711 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
718 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
719 modifiers
|= btnState
;
721 if ( event
.m_shiftDown
)
722 modifiers
|= shiftKey
;
724 if ( event
.m_controlDown
)
725 modifiers
|= controlKey
;
727 if ( event
.m_altDown
)
728 modifiers
|= optionKey
;
730 if ( event
.m_metaDown
)
731 modifiers
|= cmdKey
;
734 control = FindControlUnderMouse( localwhere , window , &controlpart ) ;
736 controlpart = FindControl( localwhere , window , &control ) ;
741 if ( AcceptsFocus() && FindFocus() != this )
746 control
= (ControlHandle
) m_macControl
;
747 if ( control
&& ::IsControlActive( control
) )
750 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
751 wxTheApp
->s_lastMouseDown
= 0 ;
752 if ( control
&& controlpart
!= kControlNoPart
&&
753 ! IsKindOf( CLASSINFO( wxScrollBar
) )
754 ) // otherwise we will get the event twice for scrollbar
756 MacHandleControlClick( control
, controlpart
) ;
764 bool wxControl::MacCanFocus() const
766 { if ( (ControlHandle
) m_macControl
== NULL
)
773 void wxControl::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
775 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, "No valid mac control" ) ;