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
) ;
215 wxSize
wxControl::DoGetBestSize() const
217 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
218 short baselineoffset
;
219 int bestWidth
, bestHeight
;
220 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
222 if ( EmptyRect( &bestsize
) )
225 bestsize
.left
= bestsize
.top
= 0 ;
226 bestsize
.right
= 16 ;
227 bestsize
.bottom
= 16 ;
228 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
230 bestsize
.bottom
= 16 ;
232 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
234 bestsize
.bottom
= 24 ;
238 if ( IsKindOf( CLASSINFO( wxButton
) ) )
240 bestWidth
= m_label
.Length() * 8 + 12 ;
241 if ( bestWidth
< 70 )
244 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
246 bestWidth
= m_label
.Length() * 8 ;
249 bestWidth
= bestsize
.right
- bestsize
.left
;
251 bestWidth
+= 2 * m_macHorizontalBorder
;
253 bestHeight
= bestsize
.bottom
- bestsize
.top
;
254 if ( bestHeight
< 10 )
257 bestHeight
+= 2 * m_macVerticalBorder
;
260 return wxSize(bestWidth
, bestHeight
);
263 bool wxControl::ProcessCommand (wxCommandEvent
& event
)
266 // 1) OnCommand, starting at this window and working up parent hierarchy
267 // 2) OnCommand then calls ProcessEvent to search the event tables.
268 return GetEventHandler()->ProcessEvent(event
);
271 // ------------------------
272 wxList
*wxWinMacControlList
= NULL
;
273 wxControl
*wxFindControlFromMacControl(ControlHandle inControl
)
275 wxNode
*node
= wxWinMacControlList
->Find((long)inControl
);
278 return (wxControl
*)node
->GetData();
281 void wxAssociateControlWithMacControl(ControlHandle inControl
, wxControl
*control
)
283 // adding NULL WindowRef is (first) surely a result of an error and
284 // (secondly) breaks menu command processing
285 wxCHECK_RET( inControl
!= (ControlHandle
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
287 if ( !wxWinMacControlList
->Find((long)inControl
) )
288 wxWinMacControlList
->Append((long)inControl
, control
);
291 void wxRemoveMacControlAssociation(wxControl
*control
)
293 if ( wxWinMacControlList
)
294 wxWinMacControlList
->DeleteObject(control
);
297 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
299 const wxSize
& size
, long style
,
300 const wxValidator
& validator
,
301 const wxString
& name
, WXRECTPTR outBounds
, unsigned char* maclabel
)
305 // These sizes will be adjusted in MacPostControlCreate
311 ((Rect
*)outBounds
)->top
= -10;
312 ((Rect
*)outBounds
)->left
= -10;
313 ((Rect
*)outBounds
)->bottom
= 0;
314 ((Rect
*)outBounds
)->right
= 0;
316 wxMacStringToPascal( wxStripMenuCodes(label
) , maclabel
) ;
319 void wxControl::MacPostControlCreate()
321 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
323 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
327 else if ( !UMAHasAquaLayout() && (IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) ) )
329 ControlFontStyleRec controlstyle
;
330 controlstyle
.flags
= kControlUseFontMask
;
331 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
333 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
337 ControlFontStyleRec controlstyle
;
338 controlstyle
.flags
= kControlUseFontMask
;
340 if (IsKindOf( CLASSINFO( wxButton
) ) )
341 controlstyle
.font
= kControlFontSmallSystemFont
; // eventually kControlFontBigSystemFont ;
343 controlstyle
.font
= kControlFontSmallSystemFont
;
345 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
347 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
348 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
349 ::EmbedControl( (ControlHandle
) m_macControl
, container
) ;
350 m_macControlIsShown
= MacIsReallyShown() ;
352 wxAssociateControlWithMacControl( (ControlHandle
) m_macControl
, this ) ;
353 if ( wxMacSetupControlBackgroundUPP
== NULL
)
355 wxMacSetupControlBackgroundUPP
= NewControlColorUPP( wxMacSetupControlBackground
) ;
357 if ( wxMacControlActionUPP
== NULL
)
359 wxMacControlActionUPP
= NewControlDefUPP( wxMacControlDefinition
) ;
361 // The following block of code is responsible for crashes when switching
362 // back to windows, which can be seen in the dialogs sample.
363 // It is disabled until a proper solution can be found.
367 only working under classic carbon
368 m_macControlAction = *(**(ControlHandle)m_macControl).contrlDefProc ;
369 (**(ControlHandle)m_macControl).contrlDefProc = (Handle) &wxMacControlActionUPP ;
372 m_macControlAction
= *(**(ControlHandle
)m_macControl
).contrlDefProc
;
375 cdef
= (cdefHandle
) NewHandle( sizeof(cdefRec
) ) ;
376 if ( (**(ControlHandle
)m_macControl
).contrlDefProc
!= NULL
)
378 (**cdef
).instruction
= 0x4EF9; /* JMP instruction */
379 (**cdef
).function
= (void(*)()) wxMacControlActionUPP
;
380 (**(ControlHandle
)m_macControl
).contrlDefProc
= (Handle
) cdef
;
384 SetControlColorProc( (ControlHandle
) m_macControl
, wxMacSetupControlBackgroundUPP
) ;
386 // Adjust the controls size and position
387 wxPoint
pos(m_x
, m_y
);
388 wxSize
best_size( DoGetBestSize() );
389 wxSize
new_size( m_width
, m_height
);
391 m_x
= m_y
= m_width
= m_height
= -1; // Forces SetSize to move/size the control
393 if (new_size
.x
== -1) {
394 new_size
.x
= best_size
.x
;
396 if (new_size
.y
== -1) {
397 new_size
.y
= best_size
.y
;
400 SetSize(pos
.x
, pos
.y
, new_size
.x
, new_size
.y
);
403 UMASetControlTitle( (ControlHandle
) m_macControl
, wxStripMenuCodes(m_label
) ) ;
406 if ( m_macControlIsShown
)
407 UMAShowControl( (ControlHandle
) m_macControl
) ;
409 SetCursor( *wxSTANDARD_CURSOR
) ;
414 void wxControl::MacAdjustControlRect()
416 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
417 if ( m_width
== -1 || m_height
== -1 )
419 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
420 short baselineoffset
;
422 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
424 if ( EmptyRect( &bestsize
) )
427 bestsize
.left
= bestsize
.top
= 0 ;
428 bestsize
.right
= 16 ;
429 bestsize
.bottom
= 16 ;
430 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
432 bestsize
.bottom
= 16 ;
434 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
436 bestsize
.bottom
= 24 ;
442 if ( IsKindOf( CLASSINFO( wxButton
) ) )
444 m_width
= m_label
.Length() * 8 + 12 ;
448 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
450 m_width
= m_label
.Length() * 8 ;
453 m_width
= bestsize
.right
- bestsize
.left
;
455 m_width
+= 2 * m_macHorizontalBorder
+ MacGetLeftBorderSize() + MacGetRightBorderSize() ;
457 if ( m_height
== -1 )
459 m_height
= bestsize
.bottom
- bestsize
.top
;
463 m_height
+= 2 * m_macVerticalBorder
+ MacGetTopBorderSize() + MacGetBottomBorderSize() ;
465 MacUpdateDimensions() ;
466 // UMASizeControl( (ControlHandle) m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
470 WXWidget
wxControl::MacGetContainerForEmbedding()
473 return m_macControl
;
475 return wxWindow::MacGetContainerForEmbedding() ;
478 void wxControl::MacUpdateDimensions()
480 // actually in the current systems this should never be possible, but later reparenting
481 // may become a reality
483 if ( (ControlHandle
) m_macControl
== NULL
)
486 if ( GetParent() == NULL
)
489 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
490 if ( rootwindow
== NULL
)
494 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
496 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
497 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
498 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
;
499 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
;
501 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
502 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
503 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
504 if ( doMove
|| doResize
)
506 InvalWindowRect( rootwindow
, &oldBounds
) ;
509 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
513 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
518 void wxControl::MacSuperChangedPosition()
520 MacUpdateDimensions() ;
521 wxWindow::MacSuperChangedPosition() ;
524 void wxControl::MacSuperEnabled( bool enabled
)
527 wxWindow::MacSuperEnabled( enabled
) ;
530 void wxControl::MacSuperShown( bool show
)
532 if ( (ControlHandle
) m_macControl
)
536 if ( m_macControlIsShown
)
538 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
539 m_macControlIsShown
= false ;
544 if ( MacIsReallyShown() && !m_macControlIsShown
)
546 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
547 m_macControlIsShown
= true ;
552 wxWindow::MacSuperShown( show
) ;
555 void wxControl::DoSetSize(int x
, int y
,
556 int width
, int height
,
559 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
562 Rect meta
, control
;
563 GetControlBounds( (ControlHandle
) m_macControl
, &control
) ;
564 RgnHandle rgn
= NewRgn() ;
565 GetControlRegion( (ControlHandle
) m_macControl
, kControlStructureMetaPart
, rgn
) ;
566 GetRegionBounds( rgn
, &meta
) ;
567 if ( !EmptyRect( &meta
) )
569 wxASSERT( meta
.left
>= control
.left
- m_macHorizontalBorder
) ;
570 wxASSERT( meta
.right
<= control
.right
+ m_macHorizontalBorder
) ;
571 wxASSERT( meta
.top
>= control
.top
- m_macVerticalBorder
) ;
572 wxASSERT( meta
.bottom
<= control
.bottom
+ m_macVerticalBorder
) ;
580 bool wxControl::Show(bool show
)
582 if ( !wxWindow::Show( show
) )
585 if ( (ControlHandle
) m_macControl
)
589 if ( m_macControlIsShown
)
591 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
592 m_macControlIsShown
= false ;
597 if ( MacIsReallyShown() && !m_macControlIsShown
)
599 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
600 m_macControlIsShown
= true ;
607 bool wxControl::Enable(bool enable
)
609 if ( !wxWindow::Enable(enable
) )
612 if ( (ControlHandle
) m_macControl
)
615 UMAActivateControl( (ControlHandle
) m_macControl
) ;
617 UMADeactivateControl( (ControlHandle
) m_macControl
) ;
622 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
624 wxWindow::Refresh( eraseBack
, rect
) ;
627 void wxControl::MacRedrawControl()
629 if ( (ControlHandle
) m_macControl
&& MacGetRootWindow() && m_macControlIsShown
)
631 wxClientDC
dc(this) ;
632 wxMacPortSetter
helper(&dc
) ;
633 wxMacWindowClipper
clipper(this) ;
634 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
635 UMADrawControl( (ControlHandle
) m_macControl
) ;
639 void wxControl::OnPaint(wxPaintEvent
& event
)
641 if ( (ControlHandle
) m_macControl
)
644 wxMacPortSetter
helper(&dc
) ;
645 wxMacWindowClipper
clipper(this) ;
646 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
647 UMADrawControl( (ControlHandle
) m_macControl
) ;
654 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
656 wxWindow::OnEraseBackground( event
) ;
659 void wxControl::OnKeyDown( wxKeyEvent
&event
)
661 if ( (ControlHandle
) m_macControl
== NULL
)
670 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
671 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
672 GetEventParameter((EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
674 ::HandleControlKey( (ControlHandle
) m_macControl
, keyCode
, charCode
, modifiers
) ;
677 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
680 keychar
= short(ev
->message
& charCodeMask
);
681 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
683 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
687 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
689 if ( (ControlHandle
) m_macControl
== NULL
)
695 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
701 MacClientToRootWindow( &x
, &y
) ;
703 ControlHandle control
;
712 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
713 modifiers
|= btnState
;
715 if ( event
.m_shiftDown
)
716 modifiers
|= shiftKey
;
718 if ( event
.m_controlDown
)
719 modifiers
|= controlKey
;
721 if ( event
.m_altDown
)
722 modifiers
|= optionKey
;
724 if ( event
.m_metaDown
)
725 modifiers
|= cmdKey
;
727 control
= (ControlHandle
) m_macControl
;
728 if ( control
&& ::IsControlActive( control
) )
731 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
732 wxTheApp
->s_lastMouseDown
= 0 ;
733 if ( control
&& controlpart
!= kControlNoPart
)
735 MacHandleControlClick( control
, controlpart
, false /* mouse not down anymore */ ) ;
747 bool wxControl::MacCanFocus() const
749 if ( (ControlHandle
) m_macControl
== NULL
)
755 void wxControl::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
757 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;