1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
18 #include "wx/control.h"
22 #include "wx/dcclient.h"
23 #include "wx/notebook.h"
24 #include "wx/tabctrl.h"
25 #include "wx/radiobox.h"
26 #include "wx/spinbutt.h"
27 #include "wx/scrolbar.h"
28 #include "wx/button.h"
29 #include "wx/dialog.h"
30 #include "wx/statbox.h"
32 #include "wx/stattext.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
37 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
38 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent
)
39 EVT_PAINT( wxControl::OnPaint
)
43 #include "wx/mac/uma.h"
44 #include "wx/mac/private.h"
49 #if PRAGMA_STRUCT_ALIGN
50 #pragma options align=mac68k
51 #elif PRAGMA_STRUCT_PACKPUSH
53 #elif PRAGMA_STRUCT_PACK
58 unsigned short instruction
;
60 } cdefRec
, *cdefPtr
, **cdefHandle
;
62 #if PRAGMA_STRUCT_ALIGN
63 #pragma options align=reset
64 #elif PRAGMA_STRUCT_PACKPUSH
66 #elif PRAGMA_STRUCT_PACK
70 ControlActionUPP wxMacLiveScrollbarActionUPP
= NULL
;
71 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
) ;
73 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
) ;
74 pascal void wxMacLiveScrollbarActionProc( ControlHandle control
, ControlPartCode partCode
)
78 wxControl
* wx
= (wxControl
*) GetControlReference( control
) ;
81 wx
->MacHandleControlClick( control
, partCode
, true /* stillDown */ ) ;
86 ControlColorUPP wxMacSetupControlBackgroundUPP
= NULL
;
87 ControlDefUPP wxMacControlActionUPP
= NULL
;
89 pascal SInt32
wxMacControlDefinition(SInt16 varCode
, ControlRef theControl
, ControlDefProcMessage message
, SInt32 param
)
92 wxControl
* wx
= (wxControl
*) wxFindControlFromMacControl( theControl
) ;
93 if ( wx
!= NULL
&& wx
->IsKindOf( CLASSINFO( wxControl
) ) )
95 if( message
== drawCntl
)
97 wxMacWindowClipper
clip( wx
) ;
98 return InvokeControlDefUPP( varCode
, theControl
, message
, param
, (ControlDefUPP
) wx
->MacGetControlAction() ) ;
101 return InvokeControlDefUPP( varCode
, theControl
, message
, param
, (ControlDefUPP
) wx
->MacGetControlAction() ) ;
106 pascal OSStatus
wxMacSetupControlBackground( ControlRef iControl
, SInt16 iMessage
, SInt16 iDepth
, Boolean iIsColor
)
108 OSStatus status
= noErr
;
111 case kControlMsgSetUpBackground
:
113 wxControl
* wx
= (wxControl
*) GetControlReference( iControl
) ;
114 if ( wx
!= NULL
&& wx
->IsKindOf( CLASSINFO( wxControl
) ) )
116 wxDC::MacSetupBackgroundForCurrentPort( wx
->MacGetBackgroundBrush() ) ;
118 // under classic this would lead to partial redraws
119 RgnHandle clip
= NewRgn() ;
122 wx
->MacWindowToRootWindow( &x
,&y
) ;
123 CopyRgn( (RgnHandle
) wx
->MacGetVisibleRegion(false).GetWXHRGN() , clip
) ;
124 OffsetRgn( clip
, x
, y
) ;
142 wxControl::wxControl()
144 m_macControl
= NULL
;
145 m_macControlAction
= NULL
;
146 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
147 m_macVerticalBorder
= 0 ;
148 m_backgroundColour
= *wxWHITE
;
149 m_foregroundColour
= *wxBLACK
;
151 if ( wxMacLiveScrollbarActionUPP
== NULL
)
153 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
154 wxMacLiveScrollbarActionUPP
= NewControlActionUPP( wxMacLiveScrollbarActionProc
);
156 wxMacLiveScrollbarActionUPP
= NewControlActionProc( wxMacLiveScrollbarActionProc
) ;
161 bool wxControl::Create(wxWindow
*parent
, wxWindowID id
,
163 const wxSize
& size
, long style
,
164 const wxValidator
& validator
,
165 const wxString
& name
)
167 m_macControl
= NULL
;
168 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
169 m_macVerticalBorder
= 0 ;
171 bool rval
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
174 m_backgroundColour
= parent
->GetBackgroundColour() ;
175 m_foregroundColour
= parent
->GetForegroundColour() ;
179 SetValidator(validator
);
185 wxControl::~wxControl()
187 m_isBeingDeleted
= TRUE
;
188 wxRemoveMacControlAssociation( this ) ;
189 // If we delete an item, we should initialize the parent panel,
190 // because it could now be invalid.
191 wxWindow
*parent
= GetParent() ;
194 if (parent
->GetDefaultItem() == (wxButton
*) this)
195 parent
->SetDefaultItem(NULL
);
197 if ( (ControlHandle
) m_macControl
)
199 ::DisposeControl( (ControlHandle
) m_macControl
) ;
200 m_macControl
= NULL
;
204 void wxControl::SetLabel(const wxString
& title
)
206 m_label
= wxStripMenuCodes(title
) ;
210 UMASetControlTitle( (ControlHandle
) m_macControl
, m_label
, m_font
.GetEncoding() ) ;
215 wxSize
wxControl::DoGetBestSize() const
217 if ( (ControlHandle
) m_macControl
== NULL
)
218 return wxWindow::DoGetBestSize() ;
220 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
221 short baselineoffset
;
222 int bestWidth
, bestHeight
;
223 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
225 if ( EmptyRect( &bestsize
) )
228 bestsize
.left
= bestsize
.top
= 0 ;
229 bestsize
.right
= 16 ;
230 bestsize
.bottom
= 16 ;
231 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
233 bestsize
.bottom
= 16 ;
235 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
237 bestsize
.bottom
= 24 ;
241 bestWidth
= bestsize
.right
- bestsize
.left
;
243 bestWidth
+= 2 * m_macHorizontalBorder
;
245 bestHeight
= bestsize
.bottom
- bestsize
.top
;
246 if ( bestHeight
< 10 )
249 bestHeight
+= 2 * m_macVerticalBorder
;
252 return wxSize(bestWidth
, bestHeight
);
255 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
258 // 1) OnCommand, starting at this window and working up parent hierarchy
259 // 2) OnCommand then calls ProcessEvent to search the event tables.
260 return GetEventHandler()->ProcessEvent(event
);
263 // ------------------------
264 wxList
*wxWinMacControlList
= NULL
;
265 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
267 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
270 return (wxControl
*)node
->GetData();
273 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
275 // adding NULL WindowRef is (first) surely a result of an error and
276 // (secondly) breaks menu command processing
277 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
279 if ( !wxWinMacControlList
->Find((long)inControl
) )
280 wxWinMacControlList
->Append((long)inControl
, control
);
283 void wxRemoveMacControlAssociation(wxControl
*control
)
285 if ( wxWinMacControlList
)
286 wxWinMacControlList
->DeleteObject(control
);
289 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
291 const wxSize
& size
, long style
,
292 const wxValidator
& validator
,
293 const wxString
& name
, WXRECTPTR outBounds
, unsigned char* maclabel
)
297 // These sizes will be adjusted in MacPostControlCreate
303 ((Rect
*)outBounds
)->top
= -10;
304 ((Rect
*)outBounds
)->left
= -10;
305 ((Rect
*)outBounds
)->bottom
= 0;
306 ((Rect
*)outBounds
)->right
= 0;
308 wxMacStringToPascal( wxStripMenuCodes(label
) , maclabel
) ;
311 void wxControl::MacPostControlCreate()
313 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
315 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
319 else if ( !UMAHasAquaLayout() && (IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) ) )
321 ControlFontStyleRec controlstyle
;
322 controlstyle
.flags
= kControlUseFontMask
;
323 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
325 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
329 ControlFontStyleRec controlstyle
;
330 controlstyle
.flags
= kControlUseFontMask
;
332 if (IsKindOf( CLASSINFO( wxButton
) ) )
333 controlstyle
.font
= kControlFontBigSystemFont
; // eventually kControlFontBigSystemFont ;
335 controlstyle
.font
= kControlFontSmallSystemFont
;
337 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
339 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
340 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
341 ::EmbedControl( (ControlHandle
) m_macControl
, container
) ;
342 m_macControlIsShown
= MacIsReallyShown() ;
344 wxAssociateControlWithMacControl( (ControlHandle
) m_macControl
, this ) ;
345 if ( wxMacSetupControlBackgroundUPP
== NULL
)
347 wxMacSetupControlBackgroundUPP
= NewControlColorUPP( wxMacSetupControlBackground
) ;
349 if ( wxMacControlActionUPP
== NULL
)
351 wxMacControlActionUPP
= NewControlDefUPP( wxMacControlDefinition
) ;
353 // The following block of code is responsible for crashes when switching
354 // back to windows, which can be seen in the dialogs sample.
355 // It is disabled until a proper solution can be found.
359 only working under classic carbon
360 m_macControlAction = *(**(ControlHandle)m_macControl).contrlDefProc ;
361 (**(ControlHandle)m_macControl).contrlDefProc = (Handle) &wxMacControlActionUPP ;
364 m_macControlAction
= *(**(ControlHandle
)m_macControl
).contrlDefProc
;
367 cdef
= (cdefHandle
) NewHandle( sizeof(cdefRec
) ) ;
368 if ( (**(ControlHandle
)m_macControl
).contrlDefProc
!= NULL
)
370 (**cdef
).instruction
= 0x4EF9; /* JMP instruction */
371 (**cdef
).function
= (void(*)()) wxMacControlActionUPP
;
372 (**(ControlHandle
)m_macControl
).contrlDefProc
= (Handle
) cdef
;
376 SetControlColorProc( (ControlHandle
) m_macControl
, wxMacSetupControlBackgroundUPP
) ;
378 // Adjust the controls size and position
379 wxPoint
pos(m_x
, m_y
);
380 wxSize
best_size( DoGetBestSize() );
381 wxSize
new_size( m_width
, m_height
);
383 m_x
= m_y
= m_width
= m_height
= -1; // Forces SetSize to move/size the control
385 if (new_size
.x
== -1) {
386 new_size
.x
= best_size
.x
;
388 if (new_size
.y
== -1) {
389 new_size
.y
= best_size
.y
;
392 SetSize(pos
.x
, pos
.y
, new_size
.x
, new_size
.y
);
395 UMASetControlTitle( (ControlHandle
) m_macControl
, wxStripMenuCodes(m_label
) , m_font
.GetEncoding() ) ;
398 if ( m_macControlIsShown
)
399 UMAShowControl( (ControlHandle
) m_macControl
) ;
401 SetCursor( *wxSTANDARD_CURSOR
) ;
406 void wxControl::MacAdjustControlRect()
408 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
409 if ( m_width
== -1 || m_height
== -1 )
411 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
412 short baselineoffset
;
414 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
416 if ( EmptyRect( &bestsize
) )
419 bestsize
.left
= bestsize
.top
= 0 ;
420 bestsize
.right
= 16 ;
421 bestsize
.bottom
= 16 ;
422 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
424 bestsize
.bottom
= 16 ;
426 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
428 bestsize
.bottom
= 24 ;
434 if ( IsKindOf( CLASSINFO( wxButton
) ) )
436 m_width
= m_label
.Length() * 10 + 12 ;
440 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
442 m_width
= m_label
.Length() * 8 ;
445 m_width
= bestsize
.right
- bestsize
.left
;
447 m_width
+= 2 * m_macHorizontalBorder
+ MacGetLeftBorderSize() + MacGetRightBorderSize() ;
449 if ( m_height
== -1 )
451 m_height
= bestsize
.bottom
- bestsize
.top
;
455 m_height
+= 2 * m_macVerticalBorder
+ MacGetTopBorderSize() + MacGetBottomBorderSize() ;
457 MacUpdateDimensions() ;
461 WXWidget
wxControl::MacGetContainerForEmbedding()
464 return m_macControl
;
466 return wxWindow::MacGetContainerForEmbedding() ;
469 void wxControl::MacUpdateDimensions()
471 // actually in the current systems this should never be possible, but later reparenting
472 // may become a reality
474 if ( (ControlHandle
) m_macControl
== NULL
)
477 if ( GetParent() == NULL
)
480 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
481 if ( rootwindow
== NULL
)
485 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
487 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
488 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
489 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
;
490 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
;
492 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
493 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
494 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
495 if ( doMove
|| doResize
)
497 InvalWindowRect( rootwindow
, &oldBounds
) ;
500 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
504 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
509 void wxControl::MacSuperChangedPosition()
511 MacUpdateDimensions() ;
512 wxWindow::MacSuperChangedPosition() ;
515 void wxControl::MacSuperEnabled( bool enabled
)
518 wxWindow::MacSuperEnabled( enabled
) ;
521 void wxControl::MacSuperShown( bool show
)
523 if ( (ControlHandle
) m_macControl
)
527 if ( m_macControlIsShown
)
529 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
530 m_macControlIsShown
= false ;
535 if ( MacIsReallyShown() && !m_macControlIsShown
)
537 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
538 m_macControlIsShown
= true ;
543 wxWindow::MacSuperShown( show
) ;
546 void wxControl::DoSetSize(int x
, int y
,
547 int width
, int height
,
550 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
553 Rect meta
, control
;
554 GetControlBounds( (ControlHandle
) m_macControl
, &control
) ;
555 RgnHandle rgn
= NewRgn() ;
556 GetControlRegion( (ControlHandle
) m_macControl
, kControlStructureMetaPart
, rgn
) ;
557 GetRegionBounds( rgn
, &meta
) ;
558 if ( !EmptyRect( &meta
) )
560 wxASSERT( meta
.left
>= control
.left
- m_macHorizontalBorder
) ;
561 wxASSERT( meta
.right
<= control
.right
+ m_macHorizontalBorder
) ;
562 wxASSERT( meta
.top
>= control
.top
- m_macVerticalBorder
) ;
563 wxASSERT( meta
.bottom
<= control
.bottom
+ m_macVerticalBorder
) ;
571 bool wxControl::Show(bool show
)
573 if ( !wxWindow::Show( show
) )
576 if ( (ControlHandle
) m_macControl
)
580 if ( m_macControlIsShown
)
582 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
583 m_macControlIsShown
= false ;
588 if ( MacIsReallyShown() && !m_macControlIsShown
)
590 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
591 m_macControlIsShown
= true ;
598 bool wxControl::Enable(bool enable
)
600 if ( !wxWindow::Enable(enable
) )
603 if ( (ControlHandle
) m_macControl
)
606 UMAActivateControl( (ControlHandle
) m_macControl
) ;
608 UMADeactivateControl( (ControlHandle
) m_macControl
) ;
613 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
615 wxWindow::Refresh( eraseBack
, rect
) ;
618 void wxControl::MacRedrawControl()
620 if ( (ControlHandle
) m_macControl
&& MacGetRootWindow() && m_macControlIsShown
)
622 wxClientDC
dc(this) ;
623 wxMacPortSetter
helper(&dc
) ;
624 wxMacWindowClipper
clipper(this) ;
625 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
626 UMADrawControl( (ControlHandle
) m_macControl
) ;
630 void wxControl::OnPaint(wxPaintEvent
& event
)
632 if ( (ControlHandle
) m_macControl
)
635 wxMacPortSetter
helper(&dc
) ;
636 wxMacWindowClipper
clipper(this) ;
637 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
638 UMADrawControl( (ControlHandle
) m_macControl
) ;
645 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
647 wxWindow::OnEraseBackground( event
) ;
650 void wxControl::OnKeyDown( wxKeyEvent
&event
)
652 if ( (ControlHandle
) m_macControl
== NULL
)
661 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
662 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
663 GetEventParameter((EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
665 ::HandleControlKey( (ControlHandle
) m_macControl
, keyCode
, charCode
, modifiers
) ;
668 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
671 keychar
= short(ev
->message
& charCodeMask
);
672 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
674 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
678 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
680 if ( (ControlHandle
) m_macControl
== NULL
)
686 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
692 MacClientToRootWindow( &x
, &y
) ;
694 ControlHandle control
;
703 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
704 modifiers
|= btnState
;
706 if ( event
.m_shiftDown
)
707 modifiers
|= shiftKey
;
709 if ( event
.m_controlDown
)
710 modifiers
|= controlKey
;
712 if ( event
.m_altDown
)
713 modifiers
|= optionKey
;
715 if ( event
.m_metaDown
)
716 modifiers
|= cmdKey
;
718 control
= (ControlHandle
) m_macControl
;
719 if ( control
&& ::IsControlActive( control
) )
722 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
723 wxTheApp
->s_lastMouseDown
= 0 ;
724 if ( control
&& controlpart
!= kControlNoPart
)
726 MacHandleControlClick( control
, controlpart
, false /* mouse not down anymore */ ) ;
738 bool wxControl::MacCanFocus() const
740 if ( (ControlHandle
) m_macControl
== NULL
)
746 void wxControl::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
748 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;