]>
Commit | Line | Data |
---|---|---|
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/osx/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, | |
73 | int thumbSize, | |
74 | int range, | |
75 | int pageSize, | |
76 | bool WXUNUSED(refresh) ) | |
77 | { | |
78 | m_pageSize = pageSize; | |
79 | m_viewSize = thumbSize; | |
80 | m_objectSize = range; | |
81 | ||
82 | int range1 = wxMax( (m_objectSize - m_viewSize), 0 ); | |
83 | ||
84 | m_peer->SetMinimum( 0 ); | |
85 | m_peer->SetMaximum( range1 ); | |
86 | m_peer->SetValue( position ); | |
87 | m_peer->SetViewSize( m_viewSize ); | |
88 | } | |
89 | ||
90 | void wxScrollBar::Command( wxCommandEvent& event ) | |
91 | { | |
92 | SetThumbPosition( event.GetInt() ); | |
93 | ProcessCommand( event ); | |
94 | } | |
95 | ||
96 | void wxScrollBar::MacHandleControlClick( WXWidget WXUNUSED(control), wxInt16 controlpart, bool mouseStillDown ) | |
97 | { | |
98 | int position = m_peer->GetValue(); | |
99 | int minPos = m_peer->GetMinimum(); | |
100 | int maxPos = m_peer->GetMaximum(); | |
101 | ||
102 | wxEventType scrollEvent = wxEVT_NULL; | |
103 | int nScrollInc = 0; | |
104 | ||
105 | // all events have already been reported during mouse down, except for THUMBRELEASE | |
106 | if ( !mouseStillDown && controlpart != kControlIndicatorPart ) | |
107 | return; | |
108 | ||
109 | switch ( controlpart ) | |
110 | { | |
111 | case kControlUpButtonPart: | |
112 | nScrollInc = -1; | |
113 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
114 | break; | |
115 | ||
116 | case kControlDownButtonPart: | |
117 | nScrollInc = 1; | |
118 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
119 | break; | |
120 | ||
121 | case kControlPageUpPart: | |
122 | nScrollInc = -m_pageSize; | |
123 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
124 | break; | |
125 | ||
126 | case kControlPageDownPart: | |
127 | nScrollInc = m_pageSize; | |
128 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
129 | break; | |
130 | ||
131 | case kControlIndicatorPart: | |
132 | nScrollInc = 0; | |
133 | if ( mouseStillDown ) | |
134 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
135 | else | |
136 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; | |
137 | break; | |
138 | ||
139 | default: | |
140 | wxFAIL_MSG(wxT("unknown scrollbar selector")); | |
141 | break; | |
142 | } | |
143 | ||
144 | int new_pos = position + nScrollInc; | |
145 | ||
146 | if (new_pos < minPos) | |
147 | new_pos = minPos; | |
148 | else if (new_pos > maxPos) | |
149 | new_pos = maxPos; | |
150 | ||
151 | if ( nScrollInc ) | |
152 | SetThumbPosition( new_pos ); | |
153 | ||
154 | wxScrollEvent event( scrollEvent, m_windowId ); | |
155 | if ( m_windowStyle & wxHORIZONTAL ) | |
156 | event.SetOrientation( wxHORIZONTAL ); | |
157 | else | |
158 | event.SetOrientation( wxVERTICAL ); | |
159 | ||
160 | event.SetPosition( new_pos ); | |
161 | event.SetEventObject( this ); | |
162 | ||
163 | wxWindow* window = GetParent(); | |
164 | if (window && window->MacIsWindowScrollbar( this )) | |
165 | // this is hardcoded | |
166 | window->MacOnScroll( event ); | |
167 | else | |
168 | HandleWindowEvent( event ); | |
169 | } | |
170 | ||
171 | wxInt32 wxScrollBar::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler), WXEVENTREF mevent ) | |
172 | { | |
173 | int position = m_peer->GetValue(); | |
174 | int minPos = m_peer->GetMinimum(); | |
175 | int maxPos = m_peer->GetMaximum(); | |
176 | ||
177 | wxEventType scrollEvent = wxEVT_NULL; | |
178 | int nScrollInc = 0; | |
179 | ||
180 | wxMacCarbonEvent cEvent( (EventRef)mevent ); | |
181 | ControlPartCode controlpart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode); | |
182 | ||
183 | // all events have already been reported during mouse down, except for THUMBRELEASE | |
184 | // NB: this may need to be reviewed in light of the fact that scroll wheel events | |
185 | // aren't being handled properly | |
186 | if ( controlpart != kControlIndicatorPart ) | |
187 | return eventNotHandledErr; | |
188 | ||
189 | switch ( controlpart ) | |
190 | { | |
191 | case kControlIndicatorPart: | |
192 | nScrollInc = 0; | |
193 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; | |
194 | break; | |
195 | ||
196 | default: | |
197 | wxFAIL_MSG(wxT("unknown scrollbar selector")); | |
198 | break; | |
199 | } | |
200 | ||
201 | int new_pos = position + nScrollInc; | |
202 | ||
203 | if (new_pos < minPos) | |
204 | new_pos = minPos; | |
205 | else if (new_pos > maxPos) | |
206 | new_pos = maxPos; | |
207 | ||
208 | if ( nScrollInc ) | |
209 | SetThumbPosition( new_pos ); | |
210 | ||
211 | wxScrollEvent event( scrollEvent, m_windowId ); | |
212 | if ( m_windowStyle & wxHORIZONTAL ) | |
213 | event.SetOrientation( wxHORIZONTAL ); | |
214 | else | |
215 | event.SetOrientation( wxVERTICAL ); | |
216 | ||
217 | event.SetPosition( new_pos ); | |
218 | event.SetEventObject( this ); | |
219 | wxWindow* window = GetParent(); | |
220 | if (window && window->MacIsWindowScrollbar( this )) | |
221 | // this is hardcoded | |
222 | window->MacOnScroll( event ); | |
223 | else | |
224 | HandleWindowEvent( event ); | |
225 | ||
226 | return noErr; | |
227 | } | |
228 | ||
229 | ||
230 | wxSize wxScrollBar::DoGetBestSize() const | |
231 | { | |
232 | int w = 100; | |
233 | int h = 100; | |
234 | ||
235 | if ( IsVertical() ) | |
236 | { | |
237 | w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
238 | } | |
239 | else | |
240 | { | |
241 | h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y); | |
242 | } | |
243 | ||
244 | wxSize best(w, h); | |
245 | CacheBestSize(best); | |
246 | return best; | |
247 | } |