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 wxWinMacControlList
->DeleteObject(control
);
296 void wxControl::MacPreControlCreate( wxWindow
*parent
, wxWindowID id
, wxString label
,
298 const wxSize
& size
, long style
,
299 const wxValidator
& validator
,
300 const wxString
& name
, WXRECTPTR outBounds
, unsigned char* maclabel
)
304 // These sizes will be adjusted in MacPostControlCreate
310 ((Rect
*)outBounds
)->top
= -10;
311 ((Rect
*)outBounds
)->left
= -10;
312 ((Rect
*)outBounds
)->bottom
= 0;
313 ((Rect
*)outBounds
)->right
= 0;
315 wxMacStringToPascal( wxStripMenuCodes(label
) , maclabel
) ;
318 void wxControl::MacPostControlCreate()
320 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
322 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
326 else if ( !UMAHasAquaLayout() && (IsKindOf( CLASSINFO( wxStaticBox
) ) || IsKindOf( CLASSINFO( wxRadioBox
) ) || IsKindOf( CLASSINFO( wxButton
) ) ) )
328 ControlFontStyleRec controlstyle
;
329 controlstyle
.flags
= kControlUseFontMask
;
330 controlstyle
.font
= kControlFontSmallBoldSystemFont
;
332 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
336 ControlFontStyleRec controlstyle
;
337 controlstyle
.flags
= kControlUseFontMask
;
339 if (IsKindOf( CLASSINFO( wxButton
) ) )
340 controlstyle
.font
= kControlFontSmallSystemFont
; // eventually kControlFontBigSystemFont ;
342 controlstyle
.font
= kControlFontSmallSystemFont
;
344 ::SetControlFontStyle( (ControlHandle
) m_macControl
, &controlstyle
) ;
346 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
347 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
348 ::EmbedControl( (ControlHandle
) m_macControl
, container
) ;
349 m_macControlIsShown
= true ;
351 wxAssociateControlWithMacControl( (ControlHandle
) m_macControl
, this ) ;
352 if ( wxMacSetupControlBackgroundUPP
== NULL
)
354 wxMacSetupControlBackgroundUPP
= NewControlColorUPP( wxMacSetupControlBackground
) ;
356 if ( wxMacControlActionUPP
== NULL
)
358 wxMacControlActionUPP
= NewControlDefUPP( wxMacControlDefinition
) ;
360 // The following block of code is responsible for crashes when switching
361 // back to windows, which can be seen in the dialogs sample.
362 // It is disabled until a proper solution can be found.
366 only working under classic carbon
367 m_macControlAction = *(**(ControlHandle)m_macControl).contrlDefProc ;
368 (**(ControlHandle)m_macControl).contrlDefProc = (Handle) &wxMacControlActionUPP ;
371 m_macControlAction
= *(**(ControlHandle
)m_macControl
).contrlDefProc
;
374 cdef
= (cdefHandle
) NewHandle( sizeof(cdefRec
) ) ;
375 if ( (**(ControlHandle
)m_macControl
).contrlDefProc
!= NULL
)
377 (**cdef
).instruction
= 0x4EF9; /* JMP instruction */
378 (**cdef
).function
= (void(*)()) wxMacControlActionUPP
;
379 (**(ControlHandle
)m_macControl
).contrlDefProc
= (Handle
) cdef
;
383 SetControlColorProc( (ControlHandle
) m_macControl
, wxMacSetupControlBackgroundUPP
) ;
385 // Adjust the controls size and position
386 wxPoint
pos(m_x
, m_y
);
387 wxSize
best_size( DoGetBestSize() );
388 wxSize
new_size( m_width
, m_height
);
390 m_x
= m_y
= m_width
= m_height
= -1; // Forces SetSize to move/size the control
392 if (new_size
.x
== -1) {
393 new_size
.x
= best_size
.x
;
395 if (new_size
.y
== -1) {
396 new_size
.y
= best_size
.y
;
399 SetSize(pos
.x
, pos
.y
, new_size
.x
, new_size
.y
);
402 UMASetControlTitle( (ControlHandle
) m_macControl
, wxStripMenuCodes(m_label
) ) ;
405 UMAShowControl( (ControlHandle
) m_macControl
) ;
407 SetCursor( *wxSTANDARD_CURSOR
) ;
412 void wxControl::MacAdjustControlRect()
414 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
415 if ( m_width
== -1 || m_height
== -1 )
417 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
418 short baselineoffset
;
420 ::GetBestControlRect( (ControlHandle
) m_macControl
, &bestsize
, &baselineoffset
) ;
422 if ( EmptyRect( &bestsize
) )
425 bestsize
.left
= bestsize
.top
= 0 ;
426 bestsize
.right
= 16 ;
427 bestsize
.bottom
= 16 ;
428 if ( IsKindOf( CLASSINFO( wxScrollBar
) ) )
430 bestsize
.bottom
= 16 ;
432 else if ( IsKindOf( CLASSINFO( wxSpinButton
) ) )
434 bestsize
.bottom
= 24 ;
440 if ( IsKindOf( CLASSINFO( wxButton
) ) )
442 m_width
= m_label
.Length() * 8 + 12 ;
446 else if ( IsKindOf( CLASSINFO( wxStaticText
) ) )
448 m_width
= m_label
.Length() * 8 ;
451 m_width
= bestsize
.right
- bestsize
.left
;
453 m_width
+= 2 * m_macHorizontalBorder
+ MacGetLeftBorderSize() + MacGetRightBorderSize() ;
455 if ( m_height
== -1 )
457 m_height
= bestsize
.bottom
- bestsize
.top
;
461 m_height
+= 2 * m_macVerticalBorder
+ MacGetTopBorderSize() + MacGetBottomBorderSize() ;
463 MacUpdateDimensions() ;
464 // UMASizeControl( (ControlHandle) m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
468 WXWidget
wxControl::MacGetContainerForEmbedding()
471 return m_macControl
;
473 return wxWindow::MacGetContainerForEmbedding() ;
476 void wxControl::MacUpdateDimensions()
478 // actually in the current systems this should never be possible, but later reparenting
479 // may become a reality
481 if ( (ControlHandle
) m_macControl
== NULL
)
484 if ( GetParent() == NULL
)
487 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
488 if ( rootwindow
== NULL
)
492 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
494 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
495 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
496 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
;
497 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
;
499 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
500 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
501 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
502 if ( doMove
|| doResize
)
504 InvalWindowRect( rootwindow
, &oldBounds
) ;
507 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
511 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
516 void wxControl::MacSuperChangedPosition()
518 MacUpdateDimensions() ;
519 wxWindow::MacSuperChangedPosition() ;
522 void wxControl::MacSuperEnabled( bool enabled
)
525 wxWindow::MacSuperEnabled( enabled
) ;
528 void wxControl::MacSuperShown( bool show
)
530 if ( (ControlHandle
) m_macControl
)
534 if ( m_macControlIsShown
)
536 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
537 m_macControlIsShown
= false ;
542 if ( MacIsReallyShown() && !m_macControlIsShown
)
544 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
545 m_macControlIsShown
= true ;
550 wxWindow::MacSuperShown( show
) ;
553 void wxControl::DoSetSize(int x
, int y
,
554 int width
, int height
,
557 wxWindow::DoSetSize( x
, y
,width
, height
,sizeFlags
) ;
560 Rect meta
, control
;
561 GetControlBounds( (ControlHandle
) m_macControl
, &control
) ;
562 RgnHandle rgn
= NewRgn() ;
563 GetControlRegion( (ControlHandle
) m_macControl
, kControlStructureMetaPart
, rgn
) ;
564 GetRegionBounds( rgn
, &meta
) ;
565 if ( !EmptyRect( &meta
) )
567 wxASSERT( meta
.left
>= control
.left
- m_macHorizontalBorder
) ;
568 wxASSERT( meta
.right
<= control
.right
+ m_macHorizontalBorder
) ;
569 wxASSERT( meta
.top
>= control
.top
- m_macVerticalBorder
) ;
570 wxASSERT( meta
.bottom
<= control
.bottom
+ m_macVerticalBorder
) ;
578 bool wxControl::Show(bool show
)
580 if ( !wxWindow::Show( show
) )
583 if ( (ControlHandle
) m_macControl
)
587 if ( m_macControlIsShown
)
589 ::UMAHideControl( (ControlHandle
) m_macControl
) ;
590 m_macControlIsShown
= false ;
595 if ( MacIsReallyShown() && !m_macControlIsShown
)
597 ::UMAShowControl( (ControlHandle
) m_macControl
) ;
598 m_macControlIsShown
= true ;
605 bool wxControl::Enable(bool enable
)
607 if ( !wxWindow::Enable(enable
) )
610 if ( (ControlHandle
) m_macControl
)
613 UMAActivateControl( (ControlHandle
) m_macControl
) ;
615 UMADeactivateControl( (ControlHandle
) m_macControl
) ;
620 void wxControl::Refresh(bool eraseBack
, const wxRect
*rect
)
622 wxWindow::Refresh( eraseBack
, rect
) ;
625 void wxControl::MacRedrawControl()
627 if ( (ControlHandle
) m_macControl
&& MacGetRootWindow() && m_macControlIsShown
)
629 wxClientDC
dc(this) ;
630 wxMacPortSetter
helper(&dc
) ;
631 wxMacWindowClipper
clipper(this) ;
632 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
633 UMADrawControl( (ControlHandle
) m_macControl
) ;
637 void wxControl::OnPaint(wxPaintEvent
& event
)
639 if ( (ControlHandle
) m_macControl
)
642 wxMacPortSetter
helper(&dc
) ;
643 wxMacWindowClipper
clipper(this) ;
644 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
645 UMADrawControl( (ControlHandle
) m_macControl
) ;
652 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
654 wxWindow::OnEraseBackground( event
) ;
657 void wxControl::OnKeyDown( wxKeyEvent
&event
)
659 if ( (ControlHandle
) m_macControl
== NULL
)
668 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyMacCharCodes
, typeChar
, NULL
,sizeof(char), NULL
,&charCode
);
669 GetEventParameter( (EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyCode
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &keyCode
);
670 GetEventParameter((EventRef
) wxTheApp
->MacGetCurrentEvent(), kEventParamKeyModifiers
, typeUInt32
, NULL
, sizeof(UInt32
), NULL
, &modifiers
);
672 ::HandleControlKey( (ControlHandle
) m_macControl
, keyCode
, charCode
, modifiers
) ;
675 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
678 keychar
= short(ev
->message
& charCodeMask
);
679 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
681 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
685 void wxControl::OnMouseEvent( wxMouseEvent
&event
)
687 if ( (ControlHandle
) m_macControl
== NULL
)
693 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
699 MacClientToRootWindow( &x
, &y
) ;
701 ControlHandle control
;
710 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
711 modifiers
|= btnState
;
713 if ( event
.m_shiftDown
)
714 modifiers
|= shiftKey
;
716 if ( event
.m_controlDown
)
717 modifiers
|= controlKey
;
719 if ( event
.m_altDown
)
720 modifiers
|= optionKey
;
722 if ( event
.m_metaDown
)
723 modifiers
|= cmdKey
;
725 control
= (ControlHandle
) m_macControl
;
726 if ( control
&& ::IsControlActive( control
) )
729 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
730 wxTheApp
->s_lastMouseDown
= 0 ;
731 if ( control
&& controlpart
!= kControlNoPart
)
733 MacHandleControlClick( control
, controlpart
, false /* mouse not down anymore */ ) ;
745 bool wxControl::MacCanFocus() const
747 if ( (ControlHandle
) m_macControl
== NULL
)
753 void wxControl::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
755 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;