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 m_macControlIsShown
= true ;
270 MacAdjustControlRect() ;
271 wxAssociateControlWithMacControl( m_macControl
, this ) ;
274 void wxControl::MacAdjustControlRect()
276 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
277 if ( m_width
== -1 || m_height
== -1 )
279 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
280 short baselineoffset
;
282 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
284 if ( EmptyRect( &bestsize
) )
287 bestsize
.left
= bestsize
.top
= 0 ;
288 bestsize
.right
= 16 ;
289 bestsize
.bottom
= 16 ;
290 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
292 bestsize
.bottom
= 16 ;
294 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
296 bestsize
.bottom
= 24 ;
302 if ( IsKindOf( CLASSINFO( wxButton
) ) )
304 m_width
= m_label
.Length() * 8 + 12 ;
306 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
308 m_width
= m_label
.Length() * 8 ;
311 m_width
= bestsize
.right
- bestsize
.left
;
313 m_width
+= 2 * m_macHorizontalBorder
;
315 if ( m_height
== -1 )
317 m_height
= bestsize
.bottom
- bestsize
.top
;
321 m_height
+= 2 * m_macVerticalBorder
;
324 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
327 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
331 ControlHandle
wxControl::MacGetContainerForEmbedding()
334 return m_macControl
;
336 return wxWindow::MacGetContainerForEmbedding() ;
339 void wxControl::MacSuperChangedPosition()
344 GetControlBounds( m_macControl
, &contrlRect
) ;
345 int former_mac_x
= contrlRect
.left
;
346 int former_mac_y
= contrlRect
.top
;
349 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
351 WindowRef rootwindow
= GetMacRootWindow() ;
352 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
353 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
354 wxMacDrawingHelper
focus( wxrootwindow
) ;
356 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
359 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
360 InvalWindowRect( rootwindow
, &inval
) ;
362 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
364 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
365 InvalWindowRect( rootwindow
, &inval
) ;
368 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
373 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
377 wxWindow::MacSuperChangedPosition() ;
380 void wxControl::MacSuperEnabled( bool enabled
)
385 if ( UMAHasAppearance() )
389 ::DeactivateControl( m_macControl ) ;
394 ::ActivateControl( m_macControl ) ;
401 ::HiliteControl( m_macControl , 255 ) ;
406 ::HiliteControl( m_macControl , 0 ) ;
410 wxWindow::MacSuperEnabled( enabled ) ;
414 void wxControl::MacSuperShown( bool show
)
420 if ( m_macControlIsShown
)
422 ::UMAHideControl( m_macControl
) ;
423 m_macControlIsShown
= false ;
428 if ( MacIsReallyShown() && !m_macControlIsShown
)
430 ::UMAShowControl( m_macControl
) ;
431 m_macControlIsShown
= true ;
436 wxWindow::MacSuperShown( show
) ;
439 void wxControl::DoSetSize(int x
, int y
,
440 int width
, int height
,
443 if ( m_macControl
== NULL
)
445 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
449 WindowRef rootwindow
= GetMacRootWindow() ;
450 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
451 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
455 int former_w
= m_width
;
456 int former_h
= m_height
;
459 GetControlBounds( m_macControl
, &contrlRect
) ;
460 int former_mac_x
= contrlRect
.left
;
461 int former_mac_y
= contrlRect
.top
;
463 int currentX
, currentY
;
464 GetPosition(¤tX
, ¤tY
);
465 int currentW
,currentH
;
466 GetSize(¤tW
, ¤tH
);
468 int actualWidth
= width
;
469 int actualHeight
= height
;
472 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
474 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
477 actualWidth
= currentW
;
479 actualHeight
= currentH
;
481 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
484 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
485 WindowRef macrootwindow
= GetMacRootWindow() ;
486 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( macrootwindow
) ) ;
488 int mac_x
= actualX
;
489 int mac_y
= actualY
;
490 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
492 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
495 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
496 InvalWindowRect( macrootwindow
, &inval
) ;
498 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
500 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
501 InvalWindowRect(macrootwindow
, &inval
) ;
505 if ( actualX
!= former_x
|| actualY
!= former_y
)
510 MacRepositionScrollBars() ;
511 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
512 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
513 event
.SetEventObject(this);
514 GetEventHandler()->ProcessEvent(event
);
516 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
519 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
520 InvalWindowRect( macrootwindow
, &inval
) ;
522 m_width
= actualWidth
;
523 m_height
= actualHeight
;
525 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
527 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
528 InvalWindowRect( macrootwindow
, &inval
) ;
531 MacRepositionScrollBars() ;
532 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
533 event
.SetEventObject(this);
534 GetEventHandler()->ProcessEvent(event
);
536 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
541 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
545 bool wxControl::Show(bool show
)
547 if ( !wxWindow::Show( show
) )
554 if ( m_macControlIsShown
)
556 ::UMAHideControl( m_macControl
) ;
557 m_macControlIsShown
= false ;
562 if ( MacIsReallyShown() && !m_macControlIsShown
)
564 ::UMAShowControl( m_macControl
) ;
565 m_macControlIsShown
= true ;
572 bool wxControl::Enable(bool enable
)
574 if ( !wxWindow::Enable(enable
) )
580 if ( UMAHasAppearance() )
583 ::ActivateControl( m_macControl
) ;
585 ::DeactivateControl( m_macControl
) ;
590 ::HiliteControl( m_macControl
, 0 ) ;
592 ::HiliteControl( m_macControl
, 255 ) ;
598 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
602 wxWindow::Refresh( eraseBack
, rect
) ;
606 wxWindow::Refresh( eraseBack
, rect
) ;
610 void wxControl::MacRedrawControl()
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 ) ;
646 wxDC::MacInvalidateSetup() ;
652 void wxControl::OnPaint(wxPaintEvent
& event
)
656 WindowRef window
= GetMacRootWindow() ;
659 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
662 wxMacDrawingHelper
help( win
) ;
663 // the mac control manager always assumes to have the origin at 0,0
666 bool hasTabBehind
= false ;
667 wxWindow
* parent
= GetParent() ;
670 if( parent
->MacGetWindowData() )
672 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
676 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
678 if ( ((wxControl
*)parent
)->m_macControl
)
679 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
683 parent
= parent
->GetParent() ;
686 UMADrawControl( m_macControl
) ;
687 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
688 wxDC::MacInvalidateSetup() ;
694 // wxWindow::OnPaint( event ) ;
697 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
699 // In general, you don't want to erase the background of a control,
700 // or you'll get a flicker.
701 // TODO: move this 'null' function into each control that
706 void wxControl::OnKeyDown( wxKeyEvent
&event
)
708 if ( m_macControl
== NULL
)
711 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
714 keychar
= short(ev
->message
& charCodeMask
);
715 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
717 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
720 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
722 if ( m_macControl
== NULL
)
728 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
734 MacClientToRootWindow( &x
, &y
) ;
736 ControlHandle control
;
740 WindowRef window
= GetMacRootWindow() ;
747 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
748 modifiers
|= btnState
;
750 if ( event
.m_shiftDown
)
751 modifiers
|= shiftKey
;
753 if ( event
.m_controlDown
)
754 modifiers
|= controlKey
;
756 if ( event
.m_altDown
)
757 modifiers
|= optionKey
;
759 if ( event
.m_metaDown
)
760 modifiers
|= cmdKey
;
762 controlpart
= FindControl( localwhere
, window
, &control
) ;
765 if ( AcceptsFocus() && FindFocus() != this )
770 if ( control
&& UMAIsControlActive( control
) )
773 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
774 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
776 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
777 wxTheApp
->s_lastMouseDown
= 0 ;
778 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
779 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
781 MacHandleControlClick( control
, controlpart
) ;
789 bool wxControl::MacCanFocus() const
791 { if ( m_macControl
== NULL
)
798 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
800 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;