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