1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 #include "wx/control.h"
19 #include "wx/notebook.h"
20 #include "wx/tabctrl.h"
21 #include "wx/radiobox.h"
22 #include "wx/spinbutt.h"
23 #include "wx/scrolbar.h"
24 #include "wx/button.h"
25 #include "wx/dialog.h"
26 #include "wx/statbox.h"
28 #include "wx/stattext.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
33 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
34 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
35 EVT_CHAR( wxControl::OnKeyDown
)
36 EVT_PAINT( wxControl::OnPaint
)
40 #include <wx/mac/uma.h>
44 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
46 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
47 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
51 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
54 wx
->MacHandleControlClick( control
, partCode
) ;
59 wxControl::wxControl()
62 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
63 m_macVerticalBorder
= 0 ;
64 m_backgroundColour
= *wxWHITE
;
65 m_foregroundColour
= *wxBLACK
;
66 #if WXWIN_COMPATIBILITY
68 #endif // WXWIN_COMPATIBILITY
70 if ( wxMacLiveScrollbarActionUPP
== NULL
)
73 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
75 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
80 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
82 const wxSize
& size
, long style
,
83 const wxValidator
& validator
,
87 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
88 m_macVerticalBorder
= 0 ;
89 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
92 SetValidator(validator
);
98 wxControl::~wxControl()
100 m_isBeingDeleted
= TRUE
;
101 // If we delete an item, we should initialize the parent panel,
102 // because it could now be invalid.
103 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
106 if (panel
->GetDefaultItem() == (wxButton
*) this)
107 panel
->SetDefaultItem(NULL
);
111 UMADisposeControl( m_macControl
) ;
112 m_macControl
= NULL
;
116 void wxControl::SetLabel(const wxString
& title
)
125 if( wxApp::s_macDefaultEncodingIsPC
)
126 label
= wxMacMakeMacStringFromPC( title
) ;
131 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
133 strcpy( (char *) maclabel
, label
) ;
134 c2pstr( (char *) maclabel
) ;
136 ::SetControlTitle( m_macControl
, maclabel
) ;
140 wxSize
wxControl::DoGetBestSize() const
142 return wxSize(20, 20);
145 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
148 // 1) A callback function (to become obsolete)
149 // 2) OnCommand, starting at this window and working up parent hierarchy
150 // 3) OnCommand then calls ProcessEvent to search the event tables.
151 #if WXWIN_COMPATIBILITY
154 (void)(*m_callback
)(this, event
);
159 #endif // WXWIN_COMPATIBILITY
161 return GetEventHandler()->ProcessEvent(event
);
165 // ------------------------
166 wxList
*wxWinMacControlList
= NULL
;
167 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
169 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
172 return (wxControl
*)node
->Data();
175 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
177 // adding NULL WindowRef is (first) surely a result of an error and
178 // (secondly) breaks menu command processing
179 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
181 if ( !wxWinMacControlList
->Find((long)inControl
) )
182 wxWinMacControlList
->Append((long)inControl
, control
);
185 void wxRemoveMacControlAssociation(wxControl
*control
)
187 wxWinMacControlList
->DeleteObject(control
);
190 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
192 const wxSize
& size
, long style
,
193 const wxValidator
& validator
,
194 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
199 SetValidator(validator
);
201 m_windowStyle
= style
;
202 parent
->AddChild((wxButton
*)this);
204 m_backgroundColour
= parent
->GetBackgroundColour() ;
205 m_foregroundColour
= parent
->GetForegroundColour() ;
208 m_windowId
= NewControlId();
216 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
221 parent
->MacClientToRootWindow( &x
, &y
) ;
222 outBounds
->top
= y
+ m_macVerticalBorder
;
223 outBounds
->left
= x
+ m_macHorizontalBorder
;
224 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
225 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
228 strcpy( c_text
, label
) ;
229 if( wxApp::s_macDefaultEncodingIsPC
)
231 wxMacConvertFromPCForControls( c_text
) ;
235 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
237 strcpy( (char *) maclabel
, c_text
) ;
238 c2pstr( (char *) maclabel
) ;
242 void wxControl::MacPostControlCreate()
244 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
246 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
250 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
252 ControlFontStyleRec controlstyle
;
253 controlstyle
.flags
= kControlUseFontMask
;
254 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
256 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
260 ControlFontStyleRec controlstyle
;
261 controlstyle
.flags
= kControlUseFontMask
;
262 controlstyle
.font
= kControlFontSmallSystemFont
;
264 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
266 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
267 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
268 ::UMAEmbedControl( m_macControl
, container
) ;
269 MacAdjustControlRect() ;
270 wxAssociateControlWithMacControl( m_macControl
, this ) ;
273 void wxControl::MacAdjustControlRect()
275 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
276 if ( m_width
== -1 || m_height
== -1 )
278 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
279 short baselineoffset
;
281 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
283 if ( EmptyRect( &bestsize
) )
286 bestsize
.left
= bestsize
.top
= 0 ;
287 bestsize
.right
= 16 ;
288 bestsize
.bottom
= 16 ;
289 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
291 bestsize
.bottom
= 16 ;
293 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
295 bestsize
.bottom
= 24 ;
301 if ( IsKindOf( CLASSINFO( wxButton
) ) )
303 m_width
= m_label
.Length() * 8 + 12 ;
305 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
307 m_width
= m_label
.Length() * 8 ;
310 m_width
= bestsize
.right
- bestsize
.left
;
312 m_width
+= 2 * m_macHorizontalBorder
;
314 if ( m_height
== -1 )
316 m_height
= bestsize
.bottom
- bestsize
.top
;
320 m_height
+= 2 * m_macVerticalBorder
;
323 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
326 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
330 ControlHandle
wxControl::MacGetContainerForEmbedding()
333 return m_macControl
;
335 return wxWindow::MacGetContainerForEmbedding() ;
338 void wxControl::MacSuperChangedPosition()
343 GetControlBounds( m_macControl
, &contrlRect
) ;
344 int former_mac_x
= contrlRect
.left
;
345 int former_mac_y
= contrlRect
.top
;
348 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
350 WindowRef rootwindow
= GetMacRootWindow() ;
351 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
352 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
353 wxMacDrawingHelper
focus( wxrootwindow
) ;
355 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
358 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
359 InvalWindowRect( rootwindow
, &inval
) ;
361 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
363 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
364 InvalWindowRect( rootwindow
, &inval
) ;
367 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
372 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
376 wxWindow::MacSuperChangedPosition() ;
379 void wxControl::MacSuperEnabled( bool enabled
)
384 if ( UMAHasAppearance() )
388 ::DeactivateControl( m_macControl ) ;
393 ::ActivateControl( m_macControl ) ;
400 ::HiliteControl( m_macControl , 255 ) ;
405 ::HiliteControl( m_macControl , 0 ) ;
409 wxWindow::MacSuperEnabled( enabled ) ;
413 void wxControl::MacSuperShown( bool show
)
419 ::UMAHideControl( m_macControl
) ;
424 ::UMAShowControl( m_macControl
) ;
428 wxWindow::MacSuperShown( show
) ;
431 void wxControl::DoSetSize(int x
, int y
,
432 int width
, int height
,
435 if ( m_macControl
== NULL
)
437 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
441 WindowRef rootwindow
= GetMacRootWindow() ;
442 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
443 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
447 int former_w
= m_width
;
448 int former_h
= m_height
;
451 GetControlBounds( m_macControl
, &contrlRect
) ;
452 int former_mac_x
= contrlRect
.left
;
453 int former_mac_y
= contrlRect
.top
;
455 int currentX
, currentY
;
456 GetPosition(¤tX
, ¤tY
);
457 int currentW
,currentH
;
458 GetSize(¤tW
, ¤tH
);
460 int actualWidth
= width
;
461 int actualHeight
= height
;
464 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
466 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
469 actualWidth
= currentW
;
471 actualHeight
= currentH
;
473 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
476 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
477 WindowRef macrootwindow
= GetMacRootWindow() ;
478 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
480 int mac_x
= actualX
;
481 int mac_y
= actualY
;
482 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
484 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
487 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
488 InvalWindowRect( macrootwindow
, &inval
) ;
490 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
492 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
493 InvalWindowRect(macrootwindow
, &inval
) ;
497 if ( actualX
!= former_x
|| actualY
!= former_y
)
502 MacRepositionScrollBars() ;
503 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
504 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
505 event
.SetEventObject(this);
506 GetEventHandler()->ProcessEvent(event
);
508 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
511 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
512 InvalWindowRect( macrootwindow
, &inval
) ;
514 m_width
= actualWidth
;
515 m_height
= actualHeight
;
517 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
519 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
520 InvalWindowRect( macrootwindow
, &inval
) ;
523 MacRepositionScrollBars() ;
524 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
525 event
.SetEventObject(this);
526 GetEventHandler()->ProcessEvent(event
);
528 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
533 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
537 bool wxControl::Show(bool show
)
539 if ( !wxWindow::Show( show
) )
545 ::UMAShowControl( m_macControl
) ;
547 ::UMAHideControl( m_macControl
) ;
552 bool wxControl::Enable(bool enable
)
554 if ( !wxWindow::Enable(enable
) )
560 if ( UMAHasAppearance() )
563 ::ActivateControl( m_macControl
) ;
565 ::DeactivateControl( m_macControl
) ;
570 ::HiliteControl( m_macControl
, 0 ) ;
572 ::HiliteControl( m_macControl
, 255 ) ;
578 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
582 wxWindow::Refresh( eraseBack
, rect
) ;
586 wxWindow::Refresh( eraseBack
, rect
) ;
590 void wxControl::MacRedrawControl()
594 WindowRef window
= GetMacRootWindow() ;
597 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
600 wxMacDrawingHelper
help( win
) ;
601 // the mac control manager always assumes to have the origin at 0,0
604 bool hasTabBehind
= false ;
605 wxWindow
* parent
= GetParent() ;
608 if( parent
->MacGetWindowData() )
610 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
614 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
616 if ( ((wxControl
*)parent
)->m_macControl
)
617 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
621 parent
= parent
->GetParent() ;
624 UMADrawControl( m_macControl
) ;
625 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
631 void wxControl::OnPaint(wxPaintEvent
& event
)
635 WindowRef window
= GetMacRootWindow() ;
638 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
641 wxMacDrawingHelper
help( win
) ;
642 // the mac control manager always assumes to have the origin at 0,0
645 bool hasTabBehind
= false ;
646 wxWindow
* parent
= GetParent() ;
649 if( parent
->MacGetWindowData() )
651 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
655 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
657 if ( ((wxControl
*)parent
)->m_macControl
)
658 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
662 parent
= parent
->GetParent() ;
665 UMADrawControl( m_macControl
) ;
666 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
672 // wxWindow::OnPaint( event ) ;
675 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
677 // In general, you don't want to erase the background of a control,
678 // or you'll get a flicker.
679 // TODO: move this 'null' function into each control that
684 void wxControl::OnKeyDown( wxKeyEvent
&event
)
686 if ( m_macControl
== NULL
)
689 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
692 keychar
= short(ev
->message
& charCodeMask
);
693 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
695 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
698 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
700 if ( m_macControl
== NULL
)
706 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
712 MacClientToRootWindow( &x
, &y
) ;
714 ControlHandle control
;
718 WindowRef window
= GetMacRootWindow() ;
725 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
726 modifiers
|= btnState
;
728 if ( event
.m_shiftDown
)
729 modifiers
|= shiftKey
;
731 if ( event
.m_controlDown
)
732 modifiers
|= controlKey
;
734 if ( event
.m_altDown
)
735 modifiers
|= optionKey
;
737 if ( event
.m_metaDown
)
738 modifiers
|= cmdKey
;
740 controlpart
= FindControl( localwhere
, window
, &control
) ;
742 if ( AcceptsFocus() && FindFocus() != this )
746 if ( control
&& UMAIsControlActive( control
) )
749 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
750 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
752 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
753 wxTheApp
->s_lastMouseDown
= 0 ;
754 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
755 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
757 MacHandleControlClick( control
, controlpart
) ;
765 bool wxControl::MacCanFocus() const
767 { if ( m_macControl
== NULL
)
774 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
776 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;