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 ControlColorUPP wxMacSetupControlBackgroundUPP
= NULL
;
65 pascal OSStatus
wxMacSetupControlBackground( ControlRef iControl
, SInt16 iMessage
, SInt16 iDepth
, Boolean iIsColor
)
67 OSStatus status
= noErr
;
70 case kControlMsgSetUpBackground
:
72 wxControl
* wx
= (wxControl
*) GetControlReference( iControl
) ;
73 if ( wx
!= NULL
&& wx
->IsKindOf( CLASSINFO( wxControl
) ) )
75 wxDC::MacSetupBackgroundForCurrentPort( wx
->MacGetBackgroundBrush() ) ;
76 // SetThemeBackground( iDepth , iIsColor ) ;
91 wxControl::wxControl()
94 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
95 m_macVerticalBorder
= 0 ;
96 m_backgroundColour
= *wxWHITE
;
97 m_foregroundColour
= *wxBLACK
;
98 #if WXWIN_COMPATIBILITY
100 #endif // WXWIN_COMPATIBILITY
102 if ( wxMacLiveScrollbarActionUPP
== NULL
)
104 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
105 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
107 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
112 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
114 const wxSize
& size
, long style
,
115 const wxValidator
& validator
,
116 const wxString
& name
)
118 m_macControl
= NULL
;
119 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
120 m_macVerticalBorder
= 0 ;
122 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
125 m_backgroundColour
= parent
->GetBackgroundColour() ;
126 m_foregroundColour
= parent
->GetForegroundColour() ;
130 SetValidator(validator
);
136 wxControl::~wxControl()
138 m_isBeingDeleted
= TRUE
;
139 // If we delete an item, we should initialize the parent panel,
140 // because it could now be invalid.
141 wxWindow
*parent
= GetParent() ;
144 if (parent
->GetDefaultItem() == (wxButton
*) this)
145 parent
->SetDefaultItem(NULL
);
147 if ( (ControlHandle
) m_macControl
)
149 ::DisposeControl( (ControlHandle
) m_macControl
) ;
150 m_macControl
= NULL
;
154 void wxControl::SetLabel(const wxString
& title
)
156 m_label
= wxStripMenuCodes(title
) ;
158 if ( (ControlHandle
) m_macControl
)
163 if( wxApp::s_macDefaultEncodingIsPC
)
164 label
= wxMacMakeMacStringFromPC( m_label
) ;
169 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
171 strcpy( (char *) maclabel
, label
) ;
172 c2pstr( (char *) maclabel
) ;
174 ::SetControlTitle( (ControlHandle
) m_macControl
, maclabel
) ;
179 wxSize
wxControl::DoGetBestSize() const
181 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
182 short baselineoffset
;
183 int bestWidth
, bestHeight
;
184 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
186 if ( EmptyRect( &bestsize
) )
189 bestsize
.left
= bestsize
.top
= 0 ;
190 bestsize
.right
= 16 ;
191 bestsize
.bottom
= 16 ;
192 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
194 bestsize
.bottom
= 16 ;
196 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
198 bestsize
.bottom
= 24 ;
202 if ( IsKindOf( CLASSINFO( wxButton
) ) )
204 bestWidth
= m_label
.Length() * 8 + 12 ;
205 if ( bestWidth
< 70 )
208 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
210 bestWidth
= m_label
.Length() * 8 ;
213 bestWidth
= bestsize
.right
- bestsize
.left
;
215 bestWidth
+= 2 * m_macHorizontalBorder
;
217 bestHeight
= bestsize
.bottom
- bestsize
.top
;
218 if ( bestHeight
< 10 )
221 bestHeight
+= 2 * m_macVerticalBorder
;
224 return wxSize(bestWidth
, bestHeight
);
227 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
230 // 1) A callback function (to become obsolete)
231 // 2) OnCommand, starting at this window and working up parent hierarchy
232 // 3) OnCommand then calls ProcessEvent to search the event tables.
233 #if WXWIN_COMPATIBILITY
236 (void)(*m_callback
)(this, event
);
241 #endif // WXWIN_COMPATIBILITY
243 return GetEventHandler()->ProcessEvent(event
);
247 // ------------------------
248 wxList
*wxWinMacControlList
= NULL
;
249 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
251 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
254 return (wxControl
*)node
->Data();
257 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
259 // adding NULL WindowRef is (first) surely a result of an error and
260 // (secondly) breaks menu command processing
261 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
263 if ( !wxWinMacControlList
->Find((long)inControl
) )
264 wxWinMacControlList
->Append((long)inControl
, control
);
267 void wxRemoveMacControlAssociation(wxControl
*control
)
269 wxWinMacControlList
->DeleteObject(control
);
272 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
274 const wxSize
& size
, long style
,
275 const wxValidator
& validator
,
276 const wxString
& name
, WXRECTPTR outBounds
, unsigned char* maclabel
)
281 SetValidator(validator
);
283 m_windowStyle
= style
;
284 parent
->AddChild(this);
286 m_backgroundColour
= parent
->GetBackgroundColour() ;
287 m_foregroundColour
= parent
->GetForegroundColour() ;
290 m_windowId
= NewControlId();
294 // These sizes will be adjusted in MacPostControlCreate
301 ((Rect
*)outBounds
)->top
= -10;
302 ((Rect
*)outBounds
)->left
= -10;
303 ((Rect
*)outBounds
)->bottom
= 0;
304 ((Rect
*)outBounds
)->right
= 0;
307 strcpy( c_text
, label
) ;
308 if( wxApp::s_macDefaultEncodingIsPC
)
310 wxMacConvertFromPCForControls( c_text
) ;
314 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
316 strcpy( (char *) maclabel
, c_text
) ;
317 c2pstr( (char *) maclabel
) ;
321 void wxControl::MacPostControlCreate()
323 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, "No valid mac control" ) ;
325 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
329 else if ( !UMAHasAquaLayout() && (IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) ) )
331 ControlFontStyleRec controlstyle
;
332 controlstyle
.flags
= kControlUseFontMask
;
333 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
335 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
339 ControlFontStyleRec controlstyle
;
340 controlstyle
.flags
= kControlUseFontMask
;
341 controlstyle
.font
= kControlFontSmallSystemFont
;
343 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
345 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
346 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
347 ::EmbedControl( (ControlHandle
) m_macControl
, container
) ;
348 m_macControlIsShown
= true ;
350 wxAssociateControlWithMacControl( (ControlHandle
) m_macControl
, this ) ;
352 if ( wxMacSetupControlBackgroundUPP
== NULL
)
354 wxMacSetupControlBackgroundUPP
= NewControlColorUPP( wxMacSetupControlBackground
) ;
356 SetControlColorProc( (ControlHandle
) m_macControl
, wxMacSetupControlBackgroundUPP
) ;
358 // Adjust the controls size and position
359 wxPoint
pos(m_x
, m_y
);
360 wxSize
best_size( DoGetBestSize() );
361 wxSize
new_size( m_width
, m_height
);
363 m_x
= m_y
= m_width
= m_height
= -1; // Forces SetSize to move/size the control
365 if (new_size
.x
== -1) {
366 new_size
.x
= best_size
.x
;
368 if (new_size
.y
== -1) {
369 new_size
.y
= best_size
.y
;
372 SetSize(pos
.x
, pos
.y
, new_size
.x
, new_size
.y
);
374 UMAShowControl( (ControlHandle
) m_macControl
) ;
378 void wxControl::MacAdjustControlRect()
380 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
381 if ( m_width
== -1 || m_height
== -1 )
383 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
384 short baselineoffset
;
386 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
388 if ( EmptyRect( &bestsize
) )
391 bestsize
.left
= bestsize
.top
= 0 ;
392 bestsize
.right
= 16 ;
393 bestsize
.bottom
= 16 ;
394 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
396 bestsize
.bottom
= 16 ;
398 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
400 bestsize
.bottom
= 24 ;
406 if ( IsKindOf( CLASSINFO( wxButton
) ) )
408 m_width
= m_label
.Length() * 8 + 12 ;
412 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
414 m_width
= m_label
.Length() * 8 ;
417 m_width
= bestsize
.right
- bestsize
.left
;
419 m_width
+= 2 * m_macHorizontalBorder
+ MacGetLeftBorderSize() + MacGetRightBorderSize() ;
421 if ( m_height
== -1 )
423 m_height
= bestsize
.bottom
- bestsize
.top
;
427 m_height
+= 2 * m_macVerticalBorder
+ MacGetTopBorderSize() + MacGetBottomBorderSize() ;
429 MacUpdateDimensions() ;
430 // UMASizeControl( (ControlHandle) m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
434 WXWidget
wxControl::MacGetContainerForEmbedding()
437 return m_macControl
;
439 return wxWindow::MacGetContainerForEmbedding() ;
442 void wxControl::MacUpdateDimensions()
444 // actually in the current systems this should never be possible, but later reparenting
445 // may become a reality
447 if ( (ControlHandle
) m_macControl
== NULL
)
450 if ( GetParent() == NULL
)
453 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
454 if ( rootwindow
== NULL
)
458 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
460 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
461 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
462 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
;
463 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
;
465 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
466 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
467 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
468 if ( doMove
|| doResize
)
470 InvalWindowRect( rootwindow
, &oldBounds
) ;
473 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
477 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
482 void wxControl::MacSuperChangedPosition()
484 MacUpdateDimensions() ;
485 wxWindow::MacSuperChangedPosition() ;
488 void wxControl::MacSuperEnabled( bool enabled
)
491 wxWindow::MacSuperEnabled( enabled
) ;
494 void wxControl::MacSuperShown( bool show
)
496 if ( (ControlHandle
) m_macControl
)
500 if ( m_macControlIsShown
)
502 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
503 m_macControlIsShown
= false ;
508 if ( MacIsReallyShown() && !m_macControlIsShown
)
510 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
511 m_macControlIsShown
= true ;
516 wxWindow::MacSuperShown( show
) ;
519 void wxControl::DoSetSize(int x
, int y
,
520 int width
, int height
,
523 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
527 if ( (ControlHandle) m_macControl == NULL )
529 wxWindow::DoSetSize( x , y ,width , height ,sizeFlags ) ;
534 int new_x, new_y, new_width, new_height;
540 new_height = m_height;
542 if (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)
551 if (x != -1) new_x = x;
552 if (y != -1) new_y = y;
553 if (width != -1) new_width = width;
554 if (height != -1) new_height = height;
557 if(sizeFlags & wxSIZE_AUTO)
559 wxSize size = GetBestSize();
560 if (sizeFlags & wxSIZE_AUTO_WIDTH)
562 if (width == -1) new_width = size.x;
564 if (sizeFlags & wxSIZE_AUTO_HEIGHT)
566 if (height == -1) new_height = size.y;
569 AdjustForParentClientOrigin(new_x, new_y, sizeFlags);
574 GetParent()->MacWindowToRootWindow(&mac_x, &mac_y);
576 GetControlBounds( (ControlHandle) m_macControl, &oldbounds);
577 oldbounds.right = oldbounds.left + m_width;
578 oldbounds.bottom = oldbounds.top + m_height;
581 bool doResize = false;
583 if ( mac_x != (oldbounds.left - m_macHorizontalBorder) ||
584 mac_y != (oldbounds.top - m_macVerticalBorder) )
588 if ( new_width != oldbounds.right - oldbounds.left - 2 * m_macHorizontalBorder ||
589 new_height != oldbounds.bottom - oldbounds.top - 2 * m_macVerticalBorder)
594 if ( doMove || doResize )
598 // Ensure resize is within constraints
599 if ((m_minWidth != -1) && (new_width < m_minWidth)) {
600 new_width = m_minWidth;
602 if ((m_minHeight != -1) && (new_height < m_minHeight)) {
603 new_height = m_minHeight;
605 if ((m_maxWidth != -1) && (new_width > m_maxWidth)) {
606 new_width = m_maxWidth;
608 if ((m_maxHeight != -1) && (new_height > m_maxHeight)) {
609 new_height = m_maxHeight;
617 UMAMoveControl( (ControlHandle) m_macControl,
618 mac_x + m_macHorizontalBorder, mac_y + m_macVerticalBorder);
620 wxMoveEvent event(wxPoint(m_x, m_y), m_windowId);
621 event.SetEventObject(this);
622 GetEventHandler()->ProcessEvent(event) ;
627 m_height = new_height;
629 UMASizeControl( (ControlHandle) m_macControl,
630 m_width - 2 * m_macHorizontalBorder,
631 m_height - 2 * m_macVerticalBorder ) ;
634 wxSizeEvent event(wxSize(m_width, m_height), m_windowId);
635 event.SetEventObject(this);
636 GetEventHandler()->ProcessEvent(event);
644 bool wxControl::Show(bool show
)
646 if ( !wxWindow::Show( show
) )
649 if ( (ControlHandle
) m_macControl
)
653 if ( m_macControlIsShown
)
655 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
656 m_macControlIsShown
= false ;
661 if ( MacIsReallyShown() && !m_macControlIsShown
)
663 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
664 m_macControlIsShown
= true ;
671 bool wxControl::Enable(bool enable
)
673 if ( !wxWindow::Enable(enable
) )
676 if ( (ControlHandle
) m_macControl
)
679 UMAActivateControl( (ControlHandle
) m_macControl
) ;
681 UMADeactivateControl( (ControlHandle
) m_macControl
) ;
686 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
688 wxWindow::Refresh( eraseBack
, rect
) ;
691 void wxControl::MacRedrawControl()
693 if ( (ControlHandle
) m_macControl
&& MacGetRootWindow() )
695 wxClientDC
dc(this) ;
696 wxMacPortSetter
helper(&dc
) ;
698 // the controls sometimes draw outside their boundaries, this
699 // should be resolved differently but is not trivial (e.g. drop shadows)
700 // since adding them to the border would yield in enormous gaps between
702 Rect r
= { 0 , 0 , 32000 , 32000 } ;
704 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
705 UMADrawControl( (ControlHandle
) m_macControl
) ;
709 void wxControl::OnPaint(wxPaintEvent
& event
)
711 if ( (ControlHandle
) m_macControl
)
714 wxMacPortSetter
helper(&dc
) ;
715 // the controls sometimes draw outside their boundaries, this
716 // should be resolved differently but is not trivial (e.g. drop shadows)
717 // since adding them to the border would yield in enormous gaps between
719 Rect r
= { 0 , 0 , 32000 , 32000 } ;
722 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
723 UMADrawControl( (ControlHandle
) m_macControl
) ;
730 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
732 wxWindow::OnEraseBackground( event
) ;
736 void wxControl::OnKeyDown( wxKeyEvent
&event
)
738 if ( (ControlHandle
) m_macControl
== NULL
)
741 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
744 keychar
= short(ev
->message
& charCodeMask
);
745 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
747 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
750 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
752 if ( (ControlHandle
) m_macControl
== NULL
)
758 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
764 MacClientToRootWindow( &x
, &y
) ;
766 ControlHandle control
;
769 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
776 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
777 modifiers
|= btnState
;
779 if ( event
.m_shiftDown
)
780 modifiers
|= shiftKey
;
782 if ( event
.m_controlDown
)
783 modifiers
|= controlKey
;
785 if ( event
.m_altDown
)
786 modifiers
|= optionKey
;
788 if ( event
.m_metaDown
)
789 modifiers
|= cmdKey
;
792 control = FindControlUnderMouse( localwhere , window , &controlpart ) ;
794 controlpart = FindControl( localwhere , window , &control ) ;
799 if ( AcceptsFocus() && FindFocus() != this )
804 control
= (ControlHandle
) m_macControl
;
805 if ( control
&& ::IsControlActive( control
) )
808 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
809 wxTheApp
->s_lastMouseDown
= 0 ;
810 if ( control
&& controlpart
!= kControlNoPart
&&
811 ! IsKindOf( CLASSINFO( wxScrollBar
) )
812 ) // otherwise we will get the event twice for scrollbar
814 MacHandleControlClick( control
, controlpart
) ;
822 bool wxControl::MacCanFocus() const
824 { if ( (ControlHandle
) m_macControl
== NULL
)
831 void wxControl::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
833 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, "No valid mac control" ) ;