]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/scrolbar.cpp
switching to new renderer API
[wxWidgets.git] / src / mac / carbon / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/scrolbar.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/mac/uma.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
25
26 BEGIN_EVENT_TABLE(wxScrollBar, wxControl)
27 END_EVENT_TABLE()
28
29
30 bool wxScrollBar::Create( wxWindow *parent,
31 wxWindowID id,
32 const wxPoint& pos,
33 const wxSize& size,
34 long style,
35 const wxValidator& validator,
36 const wxString& name )
37 {
38 m_macIsUserPane = false;
39
40 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
41 return false;
42
43 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
44
45 m_peer = new wxMacControl( this );
46 OSStatus err = CreateScrollBarControl(
47 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
48 0, 0, 100, 1, true /* liveTracking */,
49 GetwxMacLiveScrollbarActionProc(),
50 m_peer->GetControlRefAddr() );
51 verify_noerr( err );
52
53 MacPostControlCreate( pos, size );
54
55 return true;
56 }
57
58 wxScrollBar::~wxScrollBar()
59 {
60 }
61
62 void wxScrollBar::SetThumbPosition( int viewStart )
63 {
64 m_peer->SetValue( viewStart );
65 }
66
67 int wxScrollBar::GetThumbPosition() const
68 {
69 return m_peer->GetValue();
70 }
71
72 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, bool refresh )
73 {
74 m_pageSize = pageSize;
75 m_viewSize = thumbSize;
76 m_objectSize = range;
77
78 int range1 = wxMax( (m_objectSize - m_viewSize), 0 );
79
80 m_peer->SetMinimum( 0 );
81 m_peer->SetMaximum( range1 );
82 m_peer->SetValue( position );
83 m_peer->SetViewSize( m_viewSize );
84 }
85
86 void wxScrollBar::Command( wxCommandEvent& event )
87 {
88 SetThumbPosition( event.GetInt() );
89 ProcessCommand( event );
90 }
91
92 void wxScrollBar::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown )
93 {
94 int position = m_peer->GetValue();
95 int minPos = m_peer->GetMinimum();
96 int maxPos = m_peer->GetMaximum();
97
98 wxEventType scrollEvent = wxEVT_NULL;
99 int nScrollInc = 0;
100
101 // all events have already been reported during mouse down, except for THUMBRELEASE
102 if ( !mouseStillDown && controlpart != kControlIndicatorPart )
103 return;
104
105 switch ( controlpart )
106 {
107 case kControlUpButtonPart:
108 nScrollInc = -1;
109 scrollEvent = wxEVT_SCROLL_LINEUP;
110 break;
111
112 case kControlDownButtonPart:
113 nScrollInc = 1;
114 scrollEvent = wxEVT_SCROLL_LINEDOWN;
115 break;
116
117 case kControlPageUpPart:
118 nScrollInc = -m_pageSize;
119 scrollEvent = wxEVT_SCROLL_PAGEUP;
120 break;
121
122 case kControlPageDownPart:
123 nScrollInc = m_pageSize;
124 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
125 break;
126
127 case kControlIndicatorPart:
128 nScrollInc = 0;
129 if ( mouseStillDown )
130 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
131 else
132 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
133 break;
134
135 default:
136 wxFAIL_MSG(wxT("unknown scrollbar selector"));
137 break;
138 }
139
140 int new_pos = position + nScrollInc;
141
142 if (new_pos < minPos)
143 new_pos = minPos;
144 else if (new_pos > maxPos)
145 new_pos = maxPos;
146
147 if ( nScrollInc )
148 SetThumbPosition( new_pos );
149
150 wxScrollEvent event( scrollEvent, m_windowId );
151 if ( m_windowStyle & wxHORIZONTAL )
152 event.SetOrientation( wxHORIZONTAL );
153 else
154 event.SetOrientation( wxVERTICAL );
155
156 event.SetPosition( new_pos );
157 event.SetEventObject( this );
158
159 wxWindow* window = GetParent();
160 if (window && window->MacIsWindowScrollbar( this ))
161 // this is hardcoded
162 window->MacOnScroll( event );
163 else
164 GetEventHandler()->ProcessEvent( event );
165 }
166
167 wxInt32 wxScrollBar::MacControlHit( WXEVENTHANDLERREF handler, WXEVENTREF mevent )
168 {
169 int position = m_peer->GetValue();
170 int minPos = m_peer->GetMinimum();
171 int maxPos = m_peer->GetMaximum();
172
173 wxEventType scrollEvent = wxEVT_NULL;
174 int nScrollInc = 0;
175
176 wxMacCarbonEvent cEvent( (EventRef)mevent );
177 ControlPartCode controlpart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode);
178
179 // all events have already been reported during mouse down, except for THUMBRELEASE
180 // NB: this may need to be reviewed in light of the fact that scroll wheel events
181 // aren't being handled properly
182 if ( controlpart != kControlIndicatorPart )
183 return eventNotHandledErr;
184
185 switch ( controlpart )
186 {
187 case kControlIndicatorPart:
188 nScrollInc = 0;
189 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
190 break;
191
192 default:
193 wxFAIL_MSG(wxT("unknown scrollbar selector"));
194 break;
195 }
196
197 int new_pos = position + nScrollInc;
198
199 if (new_pos < minPos)
200 new_pos = minPos;
201 else if (new_pos > maxPos)
202 new_pos = maxPos;
203
204 if ( nScrollInc )
205 SetThumbPosition( new_pos );
206
207 wxScrollEvent event( scrollEvent, m_windowId );
208 if ( m_windowStyle & wxHORIZONTAL )
209 event.SetOrientation( wxHORIZONTAL );
210 else
211 event.SetOrientation( wxVERTICAL );
212
213 event.SetPosition( new_pos );
214 event.SetEventObject( this );
215 wxWindow* window = GetParent();
216 if (window && window->MacIsWindowScrollbar( this ))
217 // this is hardcoded
218 window->MacOnScroll( event );
219 else
220 GetEventHandler()->ProcessEvent( event );
221
222 return noErr;
223 }
224
225
226 wxSize wxScrollBar::DoGetBestSize() const
227 {
228 int w = 100;
229 int h = 100;
230
231 if ( IsVertical() )
232 {
233 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
234 }
235 else
236 {
237 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
238 }
239
240 wxSize best(w, h);
241 CacheBestSize(best);
242 return best;
243 }