1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 #include "wx/control.h"
20 #include "wx/notebook.h"
21 #include "wx/tabctrl.h"
22 #include "wx/radiobox.h"
23 #include "wx/spinbutt.h"
24 #include "wx/scrolbar.h"
25 #include "wx/button.h"
26 #include "wx/dialog.h"
27 #include "wx/statbox.h"
29 #include "wx/stattext.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
34 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
35 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
36 EVT_CHAR( wxControl::OnKeyDown
)
37 EVT_PAINT( wxControl::OnPaint
)
41 #include <wx/mac/uma.h>
45 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
47 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
48 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
52 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
55 wx
->MacHandleControlClick( control
, partCode
) ;
60 wxControl::wxControl()
63 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
64 m_macVerticalBorder
= 0 ;
65 m_backgroundColour
= *wxWHITE
;
66 m_foregroundColour
= *wxBLACK
;
67 #if WXWIN_COMPATIBILITY
69 #endif // WXWIN_COMPATIBILITY
71 if ( wxMacLiveScrollbarActionUPP
== NULL
)
74 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
76 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
81 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
83 const wxSize
& size
, long style
,
84 const wxValidator
& validator
,
88 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
89 m_macVerticalBorder
= 0 ;
90 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
93 SetValidator(validator
);
99 wxControl::~wxControl()
101 m_isBeingDeleted
= TRUE
;
102 // If we delete an item, we should initialize the parent panel,
103 // because it could now be invalid.
104 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
107 if (panel
->GetDefaultItem() == (wxButton
*) this)
108 panel
->SetDefaultItem(NULL
);
112 UMADisposeControl( m_macControl
) ;
113 m_macControl
= NULL
;
117 void wxControl::SetLabel(const wxString
& title
)
126 if( wxApp::s_macDefaultEncodingIsPC
)
127 label
= wxMacMakeMacStringFromPC( title
) ;
132 c2pstrcpy( (StringPtr
) maclabel
, label
) ;
134 strcpy( (char *) maclabel
, label
) ;
135 c2pstr( (char *) maclabel
) ;
137 ::SetControlTitle( m_macControl
, maclabel
) ;
141 wxSize
wxControl::DoGetBestSize() const
143 return wxSize(20, 20);
146 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
149 // 1) A callback function (to become obsolete)
150 // 2) OnCommand, starting at this window and working up parent hierarchy
151 // 3) OnCommand then calls ProcessEvent to search the event tables.
152 #if WXWIN_COMPATIBILITY
155 (void)(*m_callback
)(this, event
);
160 #endif // WXWIN_COMPATIBILITY
162 return GetEventHandler()->ProcessEvent(event
);
166 // ------------------------
167 wxList
*wxWinMacControlList
= NULL
;
168 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
170 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
173 return (wxControl
*)node
->Data();
176 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
178 // adding NULL WindowRef is (first) surely a result of an error and
179 // (secondly) breaks menu command processing
180 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
182 if ( !wxWinMacControlList
->Find((long)inControl
) )
183 wxWinMacControlList
->Append((long)inControl
, control
);
186 void wxRemoveMacControlAssociation(wxControl
*control
)
188 wxWinMacControlList
->DeleteObject(control
);
191 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
193 const wxSize
& size
, long style
,
194 const wxValidator
& validator
,
195 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
200 SetValidator(validator
);
202 m_windowStyle
= style
;
203 parent
->AddChild((wxButton
*)this);
205 m_backgroundColour
= parent
->GetBackgroundColour() ;
206 m_foregroundColour
= parent
->GetForegroundColour() ;
209 m_windowId
= NewControlId();
217 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
222 parent
->MacClientToRootWindow( &x
, &y
) ;
223 outBounds
->top
= y
+ m_macVerticalBorder
;
224 outBounds
->left
= x
+ m_macHorizontalBorder
;
225 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
226 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
229 strcpy( c_text
, label
) ;
230 if( wxApp::s_macDefaultEncodingIsPC
)
232 wxMacConvertFromPCForControls( c_text
) ;
236 c2pstrcpy( (StringPtr
) maclabel
, c_text
) ;
238 strcpy( (char *) maclabel
, c_text
) ;
239 c2pstr( (char *) maclabel
) ;
243 void wxControl::MacPostControlCreate()
245 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
247 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
251 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) )
253 ControlFontStyleRec controlstyle
;
254 controlstyle
.flags
= kControlUseFontMask
;
255 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
257 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
261 ControlFontStyleRec controlstyle
;
262 controlstyle
.flags
= kControlUseFontMask
;
263 controlstyle
.font
= kControlFontSmallSystemFont
;
265 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
267 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
268 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
269 ::UMAEmbedControl( m_macControl
, container
) ;
270 m_macControlIsShown
= true ;
271 MacAdjustControlRect() ;
272 wxAssociateControlWithMacControl( m_macControl
, this ) ;
275 void wxControl::MacAdjustControlRect()
277 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
278 if ( m_width
== -1 || m_height
== -1 )
280 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
281 short baselineoffset
;
283 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
285 if ( EmptyRect( &bestsize
) )
288 bestsize
.left
= bestsize
.top
= 0 ;
289 bestsize
.right
= 16 ;
290 bestsize
.bottom
= 16 ;
291 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
293 bestsize
.bottom
= 16 ;
295 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
297 bestsize
.bottom
= 24 ;
303 if ( IsKindOf( CLASSINFO( wxButton
) ) )
305 m_width
= m_label
.Length() * 8 + 12 ;
309 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
311 m_width
= m_label
.Length() * 8 ;
314 m_width
= bestsize
.right
- bestsize
.left
;
316 m_width
+= 2 * m_macHorizontalBorder
;
318 if ( m_height
== -1 )
320 m_height
= bestsize
.bottom
- bestsize
.top
;
324 m_height
+= 2 * m_macVerticalBorder
;
327 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
330 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
334 ControlHandle
wxControl::MacGetContainerForEmbedding()
337 return m_macControl
;
339 return wxWindow::MacGetContainerForEmbedding() ;
342 void wxControl::MacSuperChangedPosition()
347 GetControlBounds( m_macControl
, &contrlRect
) ;
348 int former_mac_x
= contrlRect
.left
;
349 int former_mac_y
= contrlRect
.top
;
352 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
354 WindowRef rootwindow
= GetMacRootWindow() ;
355 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
356 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
357 wxMacDrawingHelper
focus( wxrootwindow
) ;
359 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
362 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
363 InvalWindowRect( rootwindow
, &inval
) ;
365 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
367 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
368 InvalWindowRect( rootwindow
, &inval
) ;
371 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
376 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
380 wxWindow::MacSuperChangedPosition() ;
383 void wxControl::MacSuperEnabled( bool enabled
)
388 if ( UMAHasAppearance() )
392 ::DeactivateControl( m_macControl ) ;
397 ::ActivateControl( m_macControl ) ;
404 ::HiliteControl( m_macControl , 255 ) ;
409 ::HiliteControl( m_macControl , 0 ) ;
413 wxWindow::MacSuperEnabled( enabled ) ;
417 void wxControl::MacSuperShown( bool show
)
423 if ( m_macControlIsShown
)
425 ::UMAHideControl( m_macControl
) ;
426 m_macControlIsShown
= false ;
431 if ( MacIsReallyShown() && !m_macControlIsShown
)
433 ::UMAShowControl( m_macControl
) ;
434 m_macControlIsShown
= true ;
439 wxWindow::MacSuperShown( show
) ;
442 void wxControl::DoSetSize(int x
, int y
,
443 int width
, int height
,
446 if ( m_macControl
== NULL
)
448 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
452 WindowRef rootwindow
= GetMacRootWindow() ;
453 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
454 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
458 int former_w
= m_width
;
459 int former_h
= m_height
;
462 GetControlBounds( m_macControl
, &contrlRect
) ;
463 int former_mac_x
= contrlRect
.left
;
464 int former_mac_y
= contrlRect
.top
;
466 int currentX
, currentY
;
467 GetPosition(¤tX
, ¤tY
);
468 int currentW
,currentH
;
469 GetSize(¤tW
, ¤tH
);
471 int actualWidth
= width
;
472 int actualHeight
= height
;
475 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
477 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
480 actualWidth
= currentW
;
482 actualHeight
= currentH
;
484 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
487 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
488 WindowRef macrootwindow
= GetMacRootWindow() ;
489 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
491 int mac_x
= actualX
;
492 int mac_y
= actualY
;
493 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
495 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
498 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
499 InvalWindowRect( macrootwindow
, &inval
) ;
501 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
503 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
504 InvalWindowRect(macrootwindow
, &inval
) ;
508 if ( actualX
!= former_x
|| actualY
!= former_y
)
513 MacRepositionScrollBars() ;
514 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
515 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
516 event
.SetEventObject(this);
517 GetEventHandler()->ProcessEvent(event
);
519 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
522 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
523 InvalWindowRect( macrootwindow
, &inval
) ;
525 m_width
= actualWidth
;
526 m_height
= actualHeight
;
528 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
530 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
531 InvalWindowRect( macrootwindow
, &inval
) ;
534 MacRepositionScrollBars() ;
535 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
536 event
.SetEventObject(this);
537 GetEventHandler()->ProcessEvent(event
);
539 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
544 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
548 bool wxControl::Show(bool show
)
550 if ( !wxWindow::Show( show
) )
557 if ( m_macControlIsShown
)
559 ::UMAHideControl( m_macControl
) ;
560 m_macControlIsShown
= false ;
565 if ( MacIsReallyShown() && !m_macControlIsShown
)
567 ::UMAShowControl( m_macControl
) ;
568 m_macControlIsShown
= true ;
575 bool wxControl::Enable(bool enable
)
577 if ( !wxWindow::Enable(enable
) )
583 if ( UMAHasAppearance() )
586 ::ActivateControl( m_macControl
) ;
588 ::DeactivateControl( m_macControl
) ;
593 ::HiliteControl( m_macControl
, 0 ) ;
595 ::HiliteControl( m_macControl
, 255 ) ;
601 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
605 wxWindow::Refresh( eraseBack
, rect
) ;
609 wxWindow::Refresh( eraseBack
, rect
) ;
613 void wxControl::MacRedrawControl()
617 WindowRef window
= GetMacRootWindow() ;
620 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
623 wxMacDrawingHelper
help( win
) ;
624 // the mac control manager always assumes to have the origin at 0,0
627 bool hasTabBehind
= false ;
628 wxWindow
* parent
= GetParent() ;
631 if( parent
->MacGetWindowData() )
633 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
637 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
639 if ( ((wxControl
*)parent
)->m_macControl
)
640 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
644 parent
= parent
->GetParent() ;
647 UMADrawControl( m_macControl
) ;
648 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
649 wxDC::MacInvalidateSetup() ;
655 void wxControl::OnPaint(wxPaintEvent
& event
)
659 WindowRef window
= GetMacRootWindow() ;
662 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
665 wxMacDrawingHelper
help( win
) ;
666 // the mac control manager always assumes to have the origin at 0,0
669 bool hasTabBehind
= false ;
670 wxWindow
* parent
= GetParent() ;
673 if( parent
->MacGetWindowData() )
675 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
679 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
681 if ( ((wxControl
*)parent
)->m_macControl
)
682 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
686 parent
= parent
->GetParent() ;
689 UMADrawControl( m_macControl
) ;
690 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
691 wxDC::MacInvalidateSetup() ;
697 // wxWindow::OnPaint( event ) ;
700 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
702 // In general, you don't want to erase the background of a control,
703 // or you'll get a flicker.
704 // TODO: move this 'null' function into each control that
709 void wxControl::OnKeyDown( wxKeyEvent
&event
)
711 if ( m_macControl
== NULL
)
714 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
717 keychar
= short(ev
->message
& charCodeMask
);
718 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
720 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
723 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
725 if ( m_macControl
== NULL
)
731 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
737 MacClientToRootWindow( &x
, &y
) ;
739 ControlHandle control
;
743 WindowRef window
= GetMacRootWindow() ;
750 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
751 modifiers
|= btnState
;
753 if ( event
.m_shiftDown
)
754 modifiers
|= shiftKey
;
756 if ( event
.m_controlDown
)
757 modifiers
|= controlKey
;
759 if ( event
.m_altDown
)
760 modifiers
|= optionKey
;
762 if ( event
.m_metaDown
)
763 modifiers
|= cmdKey
;
765 controlpart
= FindControl( localwhere
, window
, &control
) ;
768 if ( AcceptsFocus() && FindFocus() != this )
773 if ( control
&& UMAIsControlActive( control
) )
776 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
777 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
779 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
780 wxTheApp
->s_lastMouseDown
= 0 ;
781 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
782 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
784 MacHandleControlClick( control
, controlpart
) ;
792 bool wxControl::MacCanFocus() const
794 { if ( m_macControl
== NULL
)
801 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
803 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;