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/spinbutt.h"
21 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxControlBase
)
23 BEGIN_EVENT_TABLE(wxControl
, wxControlBase
)
24 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
25 EVT_CHAR( wxControl::OnKeyDown
)
26 EVT_PAINT( wxControl::OnPaint
)
29 #include <wx/mac/uma.h>
33 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
35 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
39 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
42 wx
->MacHandleControlClick( control
, partCode
) ;
47 wxControl::wxControl()
50 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
51 m_macVerticalBorder
= 0 ;
52 m_backgroundColour
= *wxWHITE
;
53 m_foregroundColour
= *wxBLACK
;
54 #if WXWIN_COMPATIBILITY
56 #endif // WXWIN_COMPATIBILITY
58 if ( wxMacLiveScrollbarActionUPP
== NULL
)
60 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
64 wxControl::~wxControl()
66 m_isBeingDeleted
= TRUE
;
67 // If we delete an item, we should initialize the parent panel,
68 // because it could now be invalid.
69 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
72 if (panel
->GetDefaultItem() == (wxButton
*) this)
73 panel
->SetDefaultItem(NULL
);
77 UMADisposeControl( m_macControl
) ;
82 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
84 const wxSize
& size
, long style
,
85 const wxValidator
& validator
,
88 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
91 SetValidator(validator
);
97 void wxControl::SetLabel(const wxString
& title
)
106 if( wxApp::s_macDefaultEncodingIsPC
)
107 label
= wxMacMakeMacStringFromPC( title
) ;
111 strcpy( (char*) maclabel
, label
) ;
112 c2pstr( (char*) maclabel
) ;
114 ::SetControlTitle( m_macControl
, maclabel
) ;
118 wxSize
wxControl::DoGetBestSize() const
120 return wxSize(20, 20);
123 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
126 // 1) A callback function (to become obsolete)
127 // 2) OnCommand, starting at this window and working up parent hierarchy
128 // 3) OnCommand then calls ProcessEvent to search the event tables.
129 #if WXWIN_COMPATIBILITY
132 (void)(*m_callback
)(this, event
);
137 #endif // WXWIN_COMPATIBILITY
139 return GetEventHandler()->ProcessEvent(event
);
143 // ------------------------
144 wxList
*wxWinMacControlList
= NULL
;
145 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
147 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
150 return (wxControl
*)node
->Data();
153 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
155 // adding NULL WindowRef is (first) surely a result of an error and
156 // (secondly) breaks menu command processing
157 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
159 if ( !wxWinMacControlList
->Find((long)inControl
) )
160 wxWinMacControlList
->Append((long)inControl
, control
);
163 void wxRemoveMacControlAssociation(wxControl
*control
)
165 wxWinMacControlList
->DeleteObject(control
);
168 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
170 const wxSize
& size
, long style
,
171 const wxValidator
& validator
,
172 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
177 SetValidator(validator
);
179 m_windowStyle
= style
;
180 parent
->AddChild((wxButton
*)this);
182 m_backgroundColour
= parent
->GetBackgroundColour() ;
183 m_foregroundColour
= parent
->GetForegroundColour() ;
186 m_windowId
= NewControlId();
194 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
202 parent
->MacClientToRootWindow( &x
, &y
) ;
203 outBounds
->top
= y
+ m_macVerticalBorder
;
204 outBounds
->left
= x
+ m_macHorizontalBorder
;
205 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
206 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
208 strcpy( (char*) maclabel
, label
) ;
209 if( wxApp::s_macDefaultEncodingIsPC
)
211 wxMacConvertFromPCForControls( (char*) maclabel
) ;
214 c2pstr( (char*) maclabel
) ;
217 void wxControl::MacPostControlCreate()
219 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
221 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
225 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) )
227 ControlFontStyleRec controlstyle
;
228 controlstyle
.flags
= kControlUseFontMask
;
229 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
231 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
235 ControlFontStyleRec controlstyle
;
236 controlstyle
.flags
= kControlUseFontMask
;
237 controlstyle
.font
= kControlFontSmallSystemFont
;
239 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
241 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
242 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
243 ::UMAEmbedControl( m_macControl
, container
) ;
244 MacAdjustControlRect() ;
245 wxAssociateControlWithMacControl( m_macControl
, this ) ;
248 void wxControl::MacAdjustControlRect()
250 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
251 if ( m_width
== -1 || m_height
== -1 )
253 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
254 short baselineoffset
;
256 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
258 if ( EmptyRect( &bestsize
) )
261 bestsize
.left
= bestsize
.top
= 0 ;
262 bestsize
.right
= 16 ;
263 bestsize
.bottom
= 16 ;
264 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
266 bestsize
.bottom
= 16 ;
268 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
270 bestsize
.bottom
= 24 ;
276 if ( IsKindOf( CLASSINFO( wxButton
) ) )
278 m_width
= m_label
.Length() * 8 + 12 + 2 * m_macHorizontalBorder
;
280 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
282 m_width
= m_label
.Length() * 8 ;
285 m_width
= bestsize
.right
- bestsize
.left
+ 2 * m_macHorizontalBorder
;
287 if ( m_height
== -1 )
289 m_height
= bestsize
.bottom
- bestsize
.top
;
293 m_height
+= 2 * m_macVerticalBorder
;
296 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
299 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
303 ControlHandle
wxControl::MacGetContainerForEmbedding()
306 return m_macControl
;
308 return wxWindow::MacGetContainerForEmbedding() ;
311 void wxControl::MacSuperChangedPosition()
315 int former_mac_x
= (**m_macControl
).contrlRect
.left
;
316 int former_mac_y
= (**m_macControl
).contrlRect
.top
;
319 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
321 WindowRef rootwindow
= GetMacRootWindow() ;
322 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
323 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
324 wxMacDrawingHelper
focus( wxrootwindow
) ;
326 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
329 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
330 InvalRect( &inval
) ;
332 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
334 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
335 InvalRect( &inval
) ;
338 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
343 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
347 wxWindow::MacSuperChangedPosition() ;
350 void wxControl::MacSuperEnabled( bool enabled
)
355 if ( UMAHasAppearance() )
359 ::DeactivateControl( m_macControl ) ;
364 ::ActivateControl( m_macControl ) ;
371 ::HiliteControl( m_macControl , 255 ) ;
376 ::HiliteControl( m_macControl , 0 ) ;
380 wxWindow::MacSuperEnabled( enabled ) ;
384 void wxControl::MacSuperShown( bool show
)
391 ::UMAHideControl( m_macControl ) ;
396 ::UMAShowControl( m_macControl ) ;
400 wxWindow::MacSuperShown( show ) ;
404 void wxControl::DoSetSize(int x
, int y
,
405 int width
, int height
,
408 if ( m_macControl
== NULL
)
410 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
414 WindowRef rootwindow
= GetMacRootWindow() ;
415 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
416 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
420 int former_w
= m_width
;
421 int former_h
= m_height
;
423 int former_mac_x
= (**m_macControl
).contrlRect
.left
;
424 int former_mac_y
= (**m_macControl
).contrlRect
.top
;
426 int currentX
, currentY
;
427 GetPosition(¤tX
, ¤tY
);
428 int currentW
,currentH
;
429 GetSize(¤tW
, ¤tH
);
431 int actualWidth
= width
;
432 int actualHeight
= height
;
435 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
437 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
440 actualWidth
= currentW
;
442 actualHeight
= currentH
;
444 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
447 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
448 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
450 int mac_x
= actualX
;
451 int mac_y
= actualY
;
452 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
454 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
457 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
458 InvalRect( &inval
) ;
460 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
462 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
463 InvalRect( &inval
) ;
467 if ( actualX
!= former_x
|| actualY
!= former_y
)
472 MacRepositionScrollBars() ;
473 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
474 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
475 event
.SetEventObject(this);
476 GetEventHandler()->ProcessEvent(event
);
478 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
481 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
482 InvalRect( &inval
) ;
484 m_width
= actualWidth
;
485 m_height
= actualHeight
;
487 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
489 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
490 InvalRect( &inval
) ;
493 MacRepositionScrollBars() ;
494 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
495 event
.SetEventObject(this);
496 GetEventHandler()->ProcessEvent(event
);
498 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
503 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
507 bool wxControl::Show(bool show
)
509 if ( !wxWindow::Show( show
) )
515 ::UMAShowControl( m_macControl
) ;
517 ::UMAHideControl( m_macControl
) ;
522 bool wxControl::Enable(bool enable
)
524 if ( !wxWindow::Enable(enable
) )
530 if ( UMAHasAppearance() )
533 ::ActivateControl( m_macControl
) ;
535 ::DeactivateControl( m_macControl
) ;
540 ::HiliteControl( m_macControl
, 0 ) ;
542 ::HiliteControl( m_macControl
, 255 ) ;
548 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
552 wxWindow::Refresh( eraseBack
, rect
) ;
556 wxWindow::Refresh( eraseBack
, rect
) ;
560 void wxControl::OnPaint(wxPaintEvent
& event
)
564 WindowRef window
= GetMacRootWindow() ;
567 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
570 wxMacDrawingHelper
help( win
) ;
573 bool hasTabBehind
= false ;
574 wxWindow
* parent
= GetParent() ;
577 if( parent
->MacGetWindowData() )
579 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
583 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
585 if ( ((wxControl
*)parent
)->m_macControl
)
586 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
590 parent
= parent
->GetParent() ;
593 UMADrawControl( m_macControl
) ;
594 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
600 // wxWindow::OnPaint( event ) ;
603 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
605 // In general, you don't want to erase the background of a control,
606 // or you'll get a flicker.
607 // TODO: move this 'null' function into each control that
612 void wxControl::OnKeyDown( wxKeyEvent
&event
)
614 if ( m_macControl
== NULL
)
617 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
620 keychar
= short(ev
->message
& charCodeMask
);
621 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
623 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
626 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
628 if ( m_macControl
== NULL
)
634 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
640 MacClientToRootWindow( &x
, &y
) ;
642 ControlHandle control
;
646 WindowRef window
= GetMacRootWindow() ;
653 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
654 modifiers
|= btnState
;
656 if ( event
.m_shiftDown
)
657 modifiers
|= shiftKey
;
659 if ( event
.m_controlDown
)
660 modifiers
|= controlKey
;
662 if ( event
.m_altDown
)
663 modifiers
|= optionKey
;
665 if ( event
.m_metaDown
)
666 modifiers
|= cmdKey
;
668 controlpart
= FindControl( localwhere
, window
, &control
) ;
670 if ( AcceptsFocus() && FindFocus() != this )
674 if ( control
&& UMAIsControlActive( control
) )
677 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
678 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
680 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
681 wxTheApp
->s_lastMouseDown
= 0 ;
682 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
683 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
685 MacHandleControlClick( control
, controlpart
) ;
693 bool wxControl::MacCanFocus() const
695 { if ( m_macControl
== NULL
)
702 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
704 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;