1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 #include "wx/control.h"
17 #include "wx/notebook.h"
18 #include "wx/tabctrl.h"
19 #include "wx/radiobox.h"
20 #include "wx/spinbutt.h"
22 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
25 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
26 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
27 EVT_CHAR( wxControl::OnKeyDown
)
28 EVT_PAINT( wxControl::OnPaint
)
32 #include <wx/mac/uma.h>
36 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
38 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
39 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
43 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
46 wx
->MacHandleControlClick( control
, partCode
) ;
51 wxControl::wxControl()
54 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
55 m_macVerticalBorder
= 0 ;
56 m_backgroundColour
= *wxWHITE
;
57 m_foregroundColour
= *wxBLACK
;
58 #if WXWIN_COMPATIBILITY
60 #endif // WXWIN_COMPATIBILITY
62 if ( wxMacLiveScrollbarActionUPP
== NULL
)
64 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
68 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
70 const wxSize
& size
, long style
,
71 const wxValidator
& validator
,
75 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
76 m_macVerticalBorder
= 0 ;
77 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
80 SetValidator(validator
);
86 wxControl::~wxControl()
88 m_isBeingDeleted
= TRUE
;
89 // If we delete an item, we should initialize the parent panel,
90 // because it could now be invalid.
91 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
94 if (panel
->GetDefaultItem() == (wxButton
*) this)
95 panel
->SetDefaultItem(NULL
);
99 UMADisposeControl( m_macControl
) ;
100 m_macControl
= NULL
;
104 void wxControl::SetLabel(const wxString
& title
)
113 if( wxApp::s_macDefaultEncodingIsPC
)
114 label
= wxMacMakeMacStringFromPC( title
) ;
118 strcpy( (char*) maclabel
, label
) ;
119 c2pstr( (char*) maclabel
) ;
121 ::SetControlTitle( m_macControl
, maclabel
) ;
125 wxSize
wxControl::DoGetBestSize() const
127 return wxSize(20, 20);
130 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
133 // 1) A callback function (to become obsolete)
134 // 2) OnCommand, starting at this window and working up parent hierarchy
135 // 3) OnCommand then calls ProcessEvent to search the event tables.
136 #if WXWIN_COMPATIBILITY
139 (void)(*m_callback
)(this, event
);
144 #endif // WXWIN_COMPATIBILITY
146 return GetEventHandler()->ProcessEvent(event
);
150 // ------------------------
151 wxList
*wxWinMacControlList
= NULL
;
152 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
154 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
157 return (wxControl
*)node
->Data();
160 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
162 // adding NULL WindowRef is (first) surely a result of an error and
163 // (secondly) breaks menu command processing
164 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
166 if ( !wxWinMacControlList
->Find((long)inControl
) )
167 wxWinMacControlList
->Append((long)inControl
, control
);
170 void wxRemoveMacControlAssociation(wxControl
*control
)
172 wxWinMacControlList
->DeleteObject(control
);
175 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
177 const wxSize
& size
, long style
,
178 const wxValidator
& validator
,
179 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
184 SetValidator(validator
);
186 m_windowStyle
= style
;
187 parent
->AddChild((wxButton
*)this);
189 m_backgroundColour
= parent
->GetBackgroundColour() ;
190 m_foregroundColour
= parent
->GetForegroundColour() ;
193 m_windowId
= NewControlId();
201 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
206 parent
->MacClientToRootWindow( &x
, &y
) ;
207 outBounds
->top
= y
+ m_macVerticalBorder
;
208 outBounds
->left
= x
+ m_macHorizontalBorder
;
209 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
210 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
212 strcpy( (char*) maclabel
, label
) ;
213 if( wxApp::s_macDefaultEncodingIsPC
)
215 wxMacConvertFromPCForControls( (char*) maclabel
) ;
218 c2pstr( (char*) maclabel
) ;
221 void wxControl::MacPostControlCreate()
223 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
225 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
229 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
231 ControlFontStyleRec controlstyle
;
232 controlstyle
.flags
= kControlUseFontMask
;
233 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
235 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
239 ControlFontStyleRec controlstyle
;
240 controlstyle
.flags
= kControlUseFontMask
;
241 controlstyle
.font
= kControlFontSmallSystemFont
;
243 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
245 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
246 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
247 ::UMAEmbedControl( m_macControl
, container
) ;
248 MacAdjustControlRect() ;
249 wxAssociateControlWithMacControl( m_macControl
, this ) ;
252 void wxControl::MacAdjustControlRect()
254 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
255 if ( m_width
== -1 || m_height
== -1 )
257 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
258 short baselineoffset
;
260 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
262 if ( EmptyRect( &bestsize
) )
265 bestsize
.left
= bestsize
.top
= 0 ;
266 bestsize
.right
= 16 ;
267 bestsize
.bottom
= 16 ;
268 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
270 bestsize
.bottom
= 16 ;
272 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
274 bestsize
.bottom
= 24 ;
280 if ( IsKindOf( CLASSINFO( wxButton
) ) )
282 m_width
= m_label
.Length() * 8 + 12 ;
284 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
286 m_width
= m_label
.Length() * 8 ;
289 m_width
= bestsize
.right
- bestsize
.left
;
291 m_width
+= 2 * m_macHorizontalBorder
;
293 if ( m_height
== -1 )
295 m_height
= bestsize
.bottom
- bestsize
.top
;
299 m_height
+= 2 * m_macVerticalBorder
;
302 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
305 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
309 ControlHandle
wxControl::MacGetContainerForEmbedding()
312 return m_macControl
;
314 return wxWindow::MacGetContainerForEmbedding() ;
317 void wxControl::MacSuperChangedPosition()
322 GetControlBounds( m_macControl
, &contrlRect
) ;
323 int former_mac_x
= contrlRect
.left
;
324 int former_mac_y
= contrlRect
.top
;
327 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
329 WindowRef rootwindow
= GetMacRootWindow() ;
330 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
331 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
332 wxMacDrawingHelper
focus( wxrootwindow
) ;
334 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
337 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
338 InvalWindowRect( rootwindow
, &inval
) ;
340 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
342 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
343 InvalWindowRect( rootwindow
, &inval
) ;
346 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
351 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
355 wxWindow::MacSuperChangedPosition() ;
358 void wxControl::MacSuperEnabled( bool enabled
)
363 if ( UMAHasAppearance() )
367 ::DeactivateControl( m_macControl ) ;
372 ::ActivateControl( m_macControl ) ;
379 ::HiliteControl( m_macControl , 255 ) ;
384 ::HiliteControl( m_macControl , 0 ) ;
388 wxWindow::MacSuperEnabled( enabled ) ;
392 void wxControl::MacSuperShown( bool show
)
398 ::UMAHideControl( m_macControl
) ;
403 ::UMAShowControl( m_macControl
) ;
407 wxWindow::MacSuperShown( show
) ;
410 void wxControl::DoSetSize(int x
, int y
,
411 int width
, int height
,
414 if ( m_macControl
== NULL
)
416 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
420 WindowRef rootwindow
= GetMacRootWindow() ;
421 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
422 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
426 int former_w
= m_width
;
427 int former_h
= m_height
;
430 GetControlBounds( m_macControl
, &contrlRect
) ;
431 int former_mac_x
= contrlRect
.left
;
432 int former_mac_y
= contrlRect
.top
;
434 int currentX
, currentY
;
435 GetPosition(¤tX
, ¤tY
);
436 int currentW
,currentH
;
437 GetSize(¤tW
, ¤tH
);
439 int actualWidth
= width
;
440 int actualHeight
= height
;
443 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
445 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
448 actualWidth
= currentW
;
450 actualHeight
= currentH
;
452 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
455 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
456 WindowRef macrootwindow
= GetMacRootWindow() ;
457 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
459 int mac_x
= actualX
;
460 int mac_y
= actualY
;
461 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
463 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
466 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
467 InvalWindowRect( macrootwindow
, &inval
) ;
469 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
471 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
472 InvalWindowRect(macrootwindow
, &inval
) ;
476 if ( actualX
!= former_x
|| actualY
!= former_y
)
481 MacRepositionScrollBars() ;
482 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
483 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
484 event
.SetEventObject(this);
485 GetEventHandler()->ProcessEvent(event
);
487 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
490 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
491 InvalWindowRect( macrootwindow
, &inval
) ;
493 m_width
= actualWidth
;
494 m_height
= actualHeight
;
496 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
498 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
499 InvalWindowRect( macrootwindow
, &inval
) ;
502 MacRepositionScrollBars() ;
503 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
504 event
.SetEventObject(this);
505 GetEventHandler()->ProcessEvent(event
);
507 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
512 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
516 bool wxControl::Show(bool show
)
518 if ( !wxWindow::Show( show
) )
524 ::UMAShowControl( m_macControl
) ;
526 ::UMAHideControl( m_macControl
) ;
531 bool wxControl::Enable(bool enable
)
533 if ( !wxWindow::Enable(enable
) )
539 if ( UMAHasAppearance() )
542 ::ActivateControl( m_macControl
) ;
544 ::DeactivateControl( m_macControl
) ;
549 ::HiliteControl( m_macControl
, 0 ) ;
551 ::HiliteControl( m_macControl
, 255 ) ;
557 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
561 wxWindow::Refresh( eraseBack
, rect
) ;
565 wxWindow::Refresh( eraseBack
, rect
) ;
569 void wxControl::MacRedrawControl()
573 WindowRef window
= GetMacRootWindow() ;
576 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
579 wxMacDrawingHelper
help( win
) ;
580 // the mac control manager always assumes to have the origin at 0,0
583 bool hasTabBehind
= false ;
584 wxWindow
* parent
= GetParent() ;
587 if( parent
->MacGetWindowData() )
589 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
593 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
595 if ( ((wxControl
*)parent
)->m_macControl
)
596 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
600 parent
= parent
->GetParent() ;
603 UMADrawControl( m_macControl
) ;
604 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
610 void wxControl::OnPaint(wxPaintEvent
& event
)
614 WindowRef window
= GetMacRootWindow() ;
617 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
620 wxMacDrawingHelper
help( win
) ;
621 // the mac control manager always assumes to have the origin at 0,0
624 bool hasTabBehind
= false ;
625 wxWindow
* parent
= GetParent() ;
628 if( parent
->MacGetWindowData() )
630 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
634 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
636 if ( ((wxControl
*)parent
)->m_macControl
)
637 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
641 parent
= parent
->GetParent() ;
644 UMADrawControl( m_macControl
) ;
645 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
651 // wxWindow::OnPaint( event ) ;
654 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
656 // In general, you don't want to erase the background of a control,
657 // or you'll get a flicker.
658 // TODO: move this 'null' function into each control that
663 void wxControl::OnKeyDown( wxKeyEvent
&event
)
665 if ( m_macControl
== NULL
)
668 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
671 keychar
= short(ev
->message
& charCodeMask
);
672 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
674 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
677 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
679 if ( m_macControl
== NULL
)
685 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
691 MacClientToRootWindow( &x
, &y
) ;
693 ControlHandle control
;
697 WindowRef window
= GetMacRootWindow() ;
704 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
705 modifiers
|= btnState
;
707 if ( event
.m_shiftDown
)
708 modifiers
|= shiftKey
;
710 if ( event
.m_controlDown
)
711 modifiers
|= controlKey
;
713 if ( event
.m_altDown
)
714 modifiers
|= optionKey
;
716 if ( event
.m_metaDown
)
717 modifiers
|= cmdKey
;
719 controlpart
= FindControl( localwhere
, window
, &control
) ;
721 if ( AcceptsFocus() && FindFocus() != this )
725 if ( control
&& UMAIsControlActive( control
) )
728 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
729 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
731 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
732 wxTheApp
->s_lastMouseDown
= 0 ;
733 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
734 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
736 MacHandleControlClick( control
, controlpart
) ;
744 bool wxControl::MacCanFocus() const
746 { if ( m_macControl
== NULL
)
753 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
755 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;