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