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/notebook.h"
23 #include "wx/tabctrl.h"
24 #include "wx/radiobox.h"
25 #include "wx/spinbutt.h"
26 #include "wx/scrolbar.h"
27 #include "wx/button.h"
28 #include "wx/dialog.h"
29 #include "wx/statbox.h"
31 #include "wx/stattext.h"
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
36 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
37 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
38 EVT_CHAR( wxControl::OnKeyDown
)
39 EVT_PAINT( wxControl::OnPaint
)
43 #include <wx/mac/uma.h>
47 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
49 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
50 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
54 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
57 wx
->MacHandleControlClick( control
, partCode
) ;
62 wxControl::wxControl()
65 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
66 m_macVerticalBorder
= 0 ;
67 m_backgroundColour
= *wxWHITE
;
68 m_foregroundColour
= *wxBLACK
;
69 #if WXWIN_COMPATIBILITY
71 #endif // WXWIN_COMPATIBILITY
73 if ( wxMacLiveScrollbarActionUPP
== NULL
)
75 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
76 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
78 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
83 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
85 const wxSize
& size
, long style
,
86 const wxValidator
& validator
,
90 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
91 m_macVerticalBorder
= 0 ;
92 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
95 SetValidator(validator
);
101 wxControl::~wxControl()
103 m_isBeingDeleted
= TRUE
;
104 // If we delete an item, we should initialize the parent panel,
105 // because it could now be invalid.
106 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
109 if (panel
->GetDefaultItem() == (wxButton
*) this)
110 panel
->SetDefaultItem(NULL
);
114 UMADisposeControl( m_macControl
) ;
115 m_macControl
= NULL
;
119 void wxControl::SetLabel(const wxString
& title
)
128 if( wxApp::s_macDefaultEncodingIsPC
)
129 label
= wxMacMakeMacStringFromPC( title
) ;
134 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
136 strcpy( (char *) maclabel
, label
) ;
137 c2pstr( (char *) maclabel
) ;
139 ::SetControlTitle( m_macControl
, maclabel
) ;
144 wxSize
wxControl::DoGetBestSize() const
146 return wxSize(20, 20);
149 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
152 // 1) A callback function (to become obsolete)
153 // 2) OnCommand, starting at this window and working up parent hierarchy
154 // 3) OnCommand then calls ProcessEvent to search the event tables.
155 #if WXWIN_COMPATIBILITY
158 (void)(*m_callback
)(this, event
);
163 #endif // WXWIN_COMPATIBILITY
165 return GetEventHandler()->ProcessEvent(event
);
169 // ------------------------
170 wxList
*wxWinMacControlList
= NULL
;
171 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
173 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
176 return (wxControl
*)node
->Data();
179 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
181 // adding NULL WindowRef is (first) surely a result of an error and
182 // (secondly) breaks menu command processing
183 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
185 if ( !wxWinMacControlList
->Find((long)inControl
) )
186 wxWinMacControlList
->Append((long)inControl
, control
);
189 void wxRemoveMacControlAssociation(wxControl
*control
)
191 wxWinMacControlList
->DeleteObject(control
);
194 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
196 const wxSize
& size
, long style
,
197 const wxValidator
& validator
,
198 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
203 SetValidator(validator
);
205 m_windowStyle
= style
;
206 parent
->AddChild((wxButton
*)this);
208 m_backgroundColour
= parent
->GetBackgroundColour() ;
209 m_foregroundColour
= parent
->GetForegroundColour() ;
212 m_windowId
= NewControlId();
220 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
225 parent
->MacClientToRootWindow( &x
, &y
) ;
226 outBounds
->top
= y
+ m_macVerticalBorder
;
227 outBounds
->left
= x
+ m_macHorizontalBorder
;
228 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
229 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
232 strcpy( c_text
, label
) ;
233 if( wxApp::s_macDefaultEncodingIsPC
)
235 wxMacConvertFromPCForControls( c_text
) ;
239 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
241 strcpy( (char *) maclabel
, c_text
) ;
242 c2pstr( (char *) maclabel
) ;
246 void wxControl::MacPostControlCreate()
248 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
250 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
254 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
256 ControlFontStyleRec controlstyle
;
257 controlstyle
.flags
= kControlUseFontMask
;
258 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
260 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
264 ControlFontStyleRec controlstyle
;
265 controlstyle
.flags
= kControlUseFontMask
;
266 controlstyle
.font
= kControlFontSmallSystemFont
;
268 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
270 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
271 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
272 ::UMAEmbedControl( m_macControl
, container
) ;
273 m_macControlIsShown
= true ;
274 MacAdjustControlRect() ;
275 wxAssociateControlWithMacControl( m_macControl
, this ) ;
276 UMAShowControl( m_macControl
) ;
279 void wxControl::MacAdjustControlRect()
281 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
282 if ( m_width
== -1 || m_height
== -1 )
284 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
285 short baselineoffset
;
287 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
289 if ( EmptyRect( &bestsize
) )
292 bestsize
.left
= bestsize
.top
= 0 ;
293 bestsize
.right
= 16 ;
294 bestsize
.bottom
= 16 ;
295 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
297 bestsize
.bottom
= 16 ;
299 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
301 bestsize
.bottom
= 24 ;
307 if ( IsKindOf( CLASSINFO( wxButton
) ) )
309 m_width
= m_label
.Length() * 8 + 12 ;
313 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
315 m_width
= m_label
.Length() * 8 ;
318 m_width
= bestsize
.right
- bestsize
.left
;
320 m_width
+= 2 * m_macHorizontalBorder
;
322 if ( m_height
== -1 )
324 m_height
= bestsize
.bottom
- bestsize
.top
;
328 m_height
+= 2 * m_macVerticalBorder
;
331 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
334 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
338 ControlHandle
wxControl::MacGetContainerForEmbedding()
341 return m_macControl
;
343 return wxWindow::MacGetContainerForEmbedding() ;
346 void wxControl::MacSuperChangedPosition()
351 GetControlBounds( m_macControl
, &contrlRect
) ;
352 int former_mac_x
= contrlRect
.left
;
353 int former_mac_y
= contrlRect
.top
;
356 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
358 WindowRef rootwindow
= GetMacRootWindow() ;
359 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
360 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
361 wxMacDrawingHelper
focus( wxrootwindow
) ;
363 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
366 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
367 InvalWindowRect( rootwindow
, &inval
) ;
369 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
371 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
372 InvalWindowRect( rootwindow
, &inval
) ;
375 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
380 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
384 wxWindow::MacSuperChangedPosition() ;
387 void wxControl::MacSuperEnabled( bool enabled
)
392 if ( UMAHasAppearance() )
396 ::DeactivateControl( m_macControl ) ;
401 ::ActivateControl( m_macControl ) ;
408 ::HiliteControl( m_macControl , 255 ) ;
413 ::HiliteControl( m_macControl , 0 ) ;
417 wxWindow::MacSuperEnabled( enabled ) ;
421 void wxControl::MacSuperShown( bool show
)
427 if ( m_macControlIsShown
)
429 ::UMAHideControl( m_macControl
) ;
430 m_macControlIsShown
= false ;
435 if ( MacIsReallyShown() && !m_macControlIsShown
)
437 ::UMAShowControl( m_macControl
) ;
438 m_macControlIsShown
= true ;
443 wxWindow::MacSuperShown( show
) ;
446 void wxControl::DoSetSize(int x
, int y
,
447 int width
, int height
,
450 if ( m_macControl
== NULL
)
452 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
456 WindowRef rootwindow
= GetMacRootWindow() ;
457 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
458 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
462 int former_w
= m_width
;
463 int former_h
= m_height
;
466 GetControlBounds( m_macControl
, &contrlRect
) ;
467 int former_mac_x
= contrlRect
.left
;
468 int former_mac_y
= contrlRect
.top
;
470 int currentX
, currentY
;
471 GetPosition(¤tX
, ¤tY
);
472 int currentW
,currentH
;
473 GetSize(¤tW
, ¤tH
);
475 int actualWidth
= width
;
476 int actualHeight
= height
;
479 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
481 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
484 actualWidth
= currentW
;
486 actualHeight
= currentH
;
488 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
491 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
492 WindowRef macrootwindow
= GetMacRootWindow() ;
493 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
495 int mac_x
= actualX
;
496 int mac_y
= actualY
;
497 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
499 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
502 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
503 InvalWindowRect( macrootwindow
, &inval
) ;
505 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
507 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
508 InvalWindowRect(macrootwindow
, &inval
) ;
512 if ( actualX
!= former_x
|| actualY
!= former_y
)
517 MacRepositionScrollBars() ;
518 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
519 wxPoint
point(m_x
, m_y
);
520 wxMoveEvent
event(point
, m_windowId
);
521 event
.SetEventObject(this);
522 GetEventHandler()->ProcessEvent(event
);
524 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
527 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
528 InvalWindowRect( macrootwindow
, &inval
) ;
530 m_width
= actualWidth
;
531 m_height
= actualHeight
;
533 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
535 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
536 InvalWindowRect( macrootwindow
, &inval
) ;
539 MacRepositionScrollBars() ;
540 wxSize
size(m_width
, m_height
);
541 wxSizeEvent
event(size
, m_windowId
);
542 event
.SetEventObject(this);
543 GetEventHandler()->ProcessEvent(event
);
545 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
550 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
554 bool wxControl::Show(bool show
)
556 if ( !wxWindow::Show( show
) )
563 if ( m_macControlIsShown
)
565 ::UMAHideControl( m_macControl
) ;
566 m_macControlIsShown
= false ;
571 if ( MacIsReallyShown() && !m_macControlIsShown
)
573 ::UMAShowControl( m_macControl
) ;
574 m_macControlIsShown
= true ;
581 bool wxControl::Enable(bool enable
)
583 if ( !wxWindow::Enable(enable
) )
589 UMAActivateControl( m_macControl
) ;
591 UMADeactivateControl( m_macControl
) ;
596 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
600 wxWindow::Refresh( eraseBack
, rect
) ;
604 wxWindow::Refresh( eraseBack
, rect
) ;
608 void wxControl::MacRedrawControl()
612 WindowRef window
= GetMacRootWindow() ;
615 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
618 wxMacDrawingHelper
help( win
) ;
619 // the mac control manager always assumes to have the origin at 0,0
622 bool hasTabBehind
= false ;
623 wxWindow
* parent
= GetParent() ;
626 if( parent
->MacGetWindowData() )
628 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
632 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
634 if ( ((wxControl
*)parent
)->m_macControl
)
635 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
639 parent
= parent
->GetParent() ;
642 UMADrawControl( m_macControl
) ;
643 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
649 void wxControl::OnPaint(wxPaintEvent
& event
)
653 WindowRef window
= GetMacRootWindow() ;
656 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
659 wxMacDrawingHelper
help( win
) ;
660 // the mac control manager always assumes to have the origin at 0,0
664 bool hasTabBehind
= false ;
665 wxWindow
* parent
= GetParent() ;
668 if( parent
->MacGetWindowData() )
670 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
674 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
676 if ( ((wxControl
*)parent
)->m_macControl
)
677 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
681 parent
= parent
->GetParent() ;
684 UMADrawControl( m_macControl
) ;
685 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
691 // wxWindow::OnPaint( event ) ;
694 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
696 // In general, you don't want to erase the background of a control,
697 // or you'll get a flicker.
698 // TODO: move this 'null' function into each control that
703 void wxControl::OnKeyDown( wxKeyEvent
&event
)
705 if ( m_macControl
== NULL
)
708 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
711 keychar
= short(ev
->message
& charCodeMask
);
712 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
714 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
717 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
719 if ( m_macControl
== NULL
)
725 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
731 MacClientToRootWindow( &x
, &y
) ;
733 ControlHandle control
;
737 WindowRef window
= GetMacRootWindow() ;
744 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
745 modifiers
|= btnState
;
747 if ( event
.m_shiftDown
)
748 modifiers
|= shiftKey
;
750 if ( event
.m_controlDown
)
751 modifiers
|= controlKey
;
753 if ( event
.m_altDown
)
754 modifiers
|= optionKey
;
756 if ( event
.m_metaDown
)
757 modifiers
|= cmdKey
;
759 controlpart
= FindControl( localwhere
, window
, &control
) ;
762 if ( AcceptsFocus() && FindFocus() != this )
767 if ( control
&& UMAIsControlActive( control
) )
770 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
771 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
773 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
774 wxTheApp
->s_lastMouseDown
= 0 ;
775 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
776 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
778 MacHandleControlClick( control
, controlpart
) ;
786 bool wxControl::MacCanFocus() const
788 { if ( m_macControl
== NULL
)
795 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
797 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;