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
, wxWindow
)
23 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
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 void wxControl::SetLabel(const wxString
& title
)
91 if( wxApp::s_macDefaultEncodingIsPC
)
92 label
= wxMacMakeMacStringFromPC( title
) ;
96 strcpy( (char*) maclabel
, label
) ;
97 c2pstr( (char*) maclabel
) ;
99 ::SetControlTitle( m_macControl
, maclabel
) ;
103 wxSize
wxControl::DoGetBestSize()
105 return wxSize(20, 20);
108 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
111 // 1) A callback function (to become obsolete)
112 // 2) OnCommand, starting at this window and working up parent hierarchy
113 // 3) OnCommand then calls ProcessEvent to search the event tables.
114 #if WXWIN_COMPATIBILITY
117 (void)(*m_callback
)(this, event
);
122 #endif // WXWIN_COMPATIBILITY
124 return GetEventHandler()->ProcessEvent(event
);
128 // ------------------------
129 wxList
*wxWinMacControlList
= NULL
;
130 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
132 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
135 return (wxControl
*)node
->Data();
138 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
140 // adding NULL WindowRef is (first) surely a result of an error and
141 // (secondly) breaks menu command processing
142 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, "attempt to add a NULL WindowRef to window list" );
144 if ( !wxWinMacControlList
->Find((long)inControl
) )
145 wxWinMacControlList
->Append((long)inControl
, control
);
148 void wxRemoveMacControlAssociation(wxControl
*control
)
150 wxWinMacControlList
->DeleteObject(control
);
153 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
155 const wxSize
& size
, long style
,
156 const wxValidator
& validator
,
157 const wxString
& name
, Rect
*outBounds
, StringPtr maclabel
)
162 SetValidator(validator
);
164 m_windowStyle
= style
;
165 parent
->AddChild((wxButton
*)this);
167 m_backgroundColour
= parent
->GetBackgroundColour() ;
168 m_foregroundColour
= parent
->GetForegroundColour() ;
171 m_windowId
= NewControlId();
179 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
187 parent
->MacClientToRootWindow( &x
, &y
) ;
188 outBounds
->top
= y
+ m_macVerticalBorder
;
189 outBounds
->left
= x
+ m_macHorizontalBorder
;
190 outBounds
->bottom
= outBounds
->top
+ m_height
- 2 * m_macVerticalBorder
;
191 outBounds
->right
= outBounds
->left
+ m_width
- 2 * m_macHorizontalBorder
;
193 strcpy( (char*) maclabel
, label
) ;
194 if( wxApp::s_macDefaultEncodingIsPC
)
196 wxMacConvertFromPCForControls( (char*) maclabel
) ;
199 c2pstr( (char*) maclabel
) ;
202 void wxControl::MacPostControlCreate()
204 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
206 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
210 else if ( IsKindOf( CLASSINFO( wxStaticBox
) ) )
212 ControlFontStyleRec controlstyle
;
213 controlstyle
.flags
= kControlUseFontMask
;
214 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
216 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
220 ControlFontStyleRec controlstyle
;
221 controlstyle
.flags
= kControlUseFontMask
;
222 controlstyle
.font
= kControlFontSmallSystemFont
;
224 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
226 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
227 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
228 ::UMAEmbedControl( m_macControl
, container
) ;
229 MacAdjustControlRect() ;
230 wxAssociateControlWithMacControl( m_macControl
, this ) ;
233 void wxControl::MacAdjustControlRect()
235 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
236 if ( m_width
== -1 || m_height
== -1 )
238 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
239 short baselineoffset
;
241 UMAGetBestControlRect( m_macControl
, &bestsize
, &baselineoffset
) ;
243 if ( EmptyRect( &bestsize
) )
246 bestsize
.left
= bestsize
.top
= 0 ;
247 bestsize
.right
= 16 ;
248 bestsize
.bottom
= 16 ;
249 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
251 bestsize
.bottom
= 16 ;
253 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
255 bestsize
.bottom
= 24 ;
261 if ( IsKindOf( CLASSINFO( wxButton
) ) )
263 m_width
= m_label
.Length() * 8 + 12 + 2 * m_macHorizontalBorder
;
265 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
267 m_width
= m_label
.Length() * 8 ;
270 m_width
= bestsize
.right
- bestsize
.left
+ 2 * m_macHorizontalBorder
;
272 if ( m_height
== -1 )
274 m_height
= bestsize
.bottom
- bestsize
.top
;
278 m_height
+= 2 * m_macVerticalBorder
;
281 wxMacDrawingHelper
helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
284 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
288 ControlHandle
wxControl::MacGetContainerForEmbedding()
291 return m_macControl
;
293 return wxWindow::MacGetContainerForEmbedding() ;
296 void wxControl::MacSuperChangedPosition()
300 int former_mac_x
= (**m_macControl
).contrlRect
.left
;
301 int former_mac_y
= (**m_macControl
).contrlRect
.top
;
304 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
306 WindowRef rootwindow
= GetMacRootWindow() ;
307 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
308 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
309 wxMacDrawingHelper
focus( wxrootwindow
) ;
311 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
314 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
315 InvalRect( &inval
) ;
317 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
319 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
320 InvalRect( &inval
) ;
323 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
328 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
332 wxWindow::MacSuperChangedPosition() ;
335 void wxControl::MacSuperEnabled( bool enabled
)
340 if ( UMAHasAppearance() )
344 ::DeactivateControl( m_macControl ) ;
349 ::ActivateControl( m_macControl ) ;
356 ::HiliteControl( m_macControl , 255 ) ;
361 ::HiliteControl( m_macControl , 0 ) ;
365 wxWindow::MacSuperEnabled( enabled ) ;
369 void wxControl::MacSuperShown( bool show
)
376 ::UMAHideControl( m_macControl ) ;
381 ::UMAShowControl( m_macControl ) ;
385 wxWindow::MacSuperShown( show ) ;
389 void wxControl::DoSetSize(int x
, int y
,
390 int width
, int height
,
393 if ( m_macControl
== NULL
)
395 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
399 WindowRef rootwindow
= GetMacRootWindow() ;
400 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
401 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
405 int former_w
= m_width
;
406 int former_h
= m_height
;
408 int former_mac_x
= (**m_macControl
).contrlRect
.left
;
409 int former_mac_y
= (**m_macControl
).contrlRect
.top
;
411 int currentX
, currentY
;
412 GetPosition(¤tX
, ¤tY
);
413 int currentW
,currentH
;
414 GetSize(¤tW
, ¤tH
);
416 int actualWidth
= width
;
417 int actualHeight
= height
;
420 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
422 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
425 actualWidth
= currentW
;
427 actualHeight
= currentH
;
429 if ( actualX
== currentX
&& actualY
== currentY
&& actualWidth
== currentW
&& actualHeight
== currentH
)
432 AdjustForParentClientOrigin(actualX
, actualY
, sizeFlags
);
433 wxMacDrawingHelper
focus( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
435 int mac_x
= actualX
;
436 int mac_y
= actualY
;
437 GetParent()->MacClientToRootWindow( & mac_x
, & mac_y
) ;
439 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
442 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ m_height
, former_mac_x
+ m_width
} ;
443 InvalRect( &inval
) ;
445 UMAMoveControl( m_macControl
, mac_x
+ m_macHorizontalBorder
, mac_y
+ m_macVerticalBorder
) ;
447 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
448 InvalRect( &inval
) ;
452 if ( actualX
!= former_x
|| actualY
!= former_y
)
457 MacRepositionScrollBars() ;
458 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
459 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
460 event
.SetEventObject(this);
461 GetEventHandler()->ProcessEvent(event
);
463 if ( actualWidth
!= former_w
|| actualHeight
!= former_h
)
466 Rect inval
= { mac_y
, mac_x
, mac_y
+ former_h
, mac_x
+ former_w
} ;
467 InvalRect( &inval
) ;
469 m_width
= actualWidth
;
470 m_height
= actualHeight
;
472 UMASizeControl( m_macControl
, m_width
- 2 * m_macHorizontalBorder
, m_height
- 2 * m_macVerticalBorder
) ;
474 Rect inval
= { mac_y
, mac_x
, mac_y
+ m_height
, mac_x
+ m_width
} ;
475 InvalRect( &inval
) ;
478 MacRepositionScrollBars() ;
479 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
480 event
.SetEventObject(this);
481 GetEventHandler()->ProcessEvent(event
);
483 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
488 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
492 bool wxControl::Show(bool show
)
494 if ( !wxWindow::Show( show
) )
500 ::UMAShowControl( m_macControl
) ;
502 ::UMAHideControl( m_macControl
) ;
507 bool wxControl::Enable(bool enable
)
509 if ( !wxWindow::Enable(enable
) )
515 if ( UMAHasAppearance() )
518 ::ActivateControl( m_macControl
) ;
520 ::DeactivateControl( m_macControl
) ;
525 ::HiliteControl( m_macControl
, 0 ) ;
527 ::HiliteControl( m_macControl
, 255 ) ;
533 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
537 wxWindow::Refresh( eraseBack
, rect
) ;
541 wxWindow::Refresh( eraseBack
, rect
) ;
545 void wxControl::OnPaint(wxPaintEvent
& event
)
549 WindowRef window
= GetMacRootWindow() ;
552 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
555 wxMacDrawingHelper
help( win
) ;
558 bool hasTabBehind
= false ;
559 wxWindow
* parent
= GetParent() ;
562 if( parent
->MacGetWindowData() )
564 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
568 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
570 if ( ((wxControl
*)parent
)->m_macControl
)
571 SetUpControlBackground( ((wxControl
*)parent
)->m_macControl
, -1 , true ) ;
575 parent
= parent
->GetParent() ;
578 UMADrawControl( m_macControl
) ;
579 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
585 // wxWindow::OnPaint( event ) ;
588 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
590 // In general, you don't want to erase the background of a control,
591 // or you'll get a flicker.
592 // TODO: move this 'null' function into each control that
597 void wxControl::OnKeyDown( wxKeyEvent
&event
)
599 if ( m_macControl
== NULL
)
602 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
605 keychar
= short(ev
->message
& charCodeMask
);
606 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
608 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
611 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
613 if ( m_macControl
== NULL
)
619 if (event
.GetEventType() == wxEVT_LEFT_DOWN
)
625 MacClientToRootWindow( &x
, &y
) ;
627 ControlHandle control
;
631 WindowRef window
= GetMacRootWindow() ;
638 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
639 modifiers
|= btnState
;
641 if ( event
.m_shiftDown
)
642 modifiers
|= shiftKey
;
644 if ( event
.m_controlDown
)
645 modifiers
|= controlKey
;
647 if ( event
.m_altDown
)
648 modifiers
|= optionKey
;
650 if ( event
.m_metaDown
)
651 modifiers
|= cmdKey
;
653 controlpart
= FindControl( localwhere
, window
, &control
) ;
655 if ( AcceptsFocus() && FindFocus() != this )
659 if ( control
&& UMAIsControlActive( control
) )
662 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
663 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
665 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
666 wxTheApp
->s_lastMouseDown
= 0 ;
667 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
668 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
670 MacHandleControlClick( control
, controlpart
) ;
678 bool wxControl::MacCanFocus() const
680 { if ( m_macControl
== NULL
)
687 void wxControl::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
689 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;