1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/slider.h"
26 #include "wx/os2/private.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
45 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
46 int value
, int minValue
, int maxValue
,
48 const wxSize
& size
, long style
,
49 const wxValidator
& validator
,
53 SetValidator(validator
);
55 if (parent
) parent
->AddChild(this);
58 m_windowStyle
= style
;
62 m_windowId
= (int)NewControlId();
66 m_rangeMax
= maxValue
;
67 m_rangeMin
= minValue
;
69 m_pageSize
= (int)((maxValue
-minValue
)/10);
76 bool wxSlider::OS2OnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
77 WXWORD pos
, WXHWND control
)
79 int position
= 0; // Dummy - not used in this mode
82 wxEventType scrollEvent
= wxEVT_NULL
;
88 nScrollInc = m_rangeMax - position;
89 scrollEvent = wxEVT_SCROLL_TOP;
93 nScrollInc = - position;
94 scrollEvent = wxEVT_SCROLL_BOTTOM;
98 nScrollInc = - GetLineSize();
99 scrollEvent = wxEVT_SCROLL_LINEUP;
103 nScrollInc = GetLineSize();
104 scrollEvent = wxEVT_SCROLL_LINEDOWN;
108 nScrollInc = -GetPageSize();
109 scrollEvent = wxEVT_SCROLL_PAGEUP;
113 nScrollInc = GetPageSize();
114 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
118 case SB_THUMBPOSITION:
120 nScrollInc = (signed short)pos - position;
122 nScrollInc = pos - position;
124 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
131 if ( nScrollInc == 0 )
137 int newPos = (int)::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
138 if ( (newPos < GetMin()) || (newPos > GetMax()) )
140 // out of range - but we did process it
144 int newPos
= 0; //temporary
147 wxScrollEvent
event(scrollEvent
, m_windowId
);
148 event
.SetPosition(newPos
);
149 event
.SetEventObject( this );
150 GetEventHandler()->ProcessEvent(event
);
152 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
153 cevent
.SetEventObject( this );
155 return GetEventHandler()->ProcessEvent( cevent
);
158 wxSlider::~wxSlider()
163 int wxSlider::GetValue() const
169 void wxSlider::SetValue(int value
)
174 void wxSlider::GetSize(int *width
, int *height
) const
179 void wxSlider::GetPosition(int *x
, int *y
) const
184 // TODO one day, make sense of all this horros and replace it with a readable
186 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
193 int currentX
, currentY
;
194 GetPosition(¤tX
, ¤tY
);
195 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
197 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
203 AdjustForParentClientOrigin(x1, y1, sizeFlags);
210 int cx; // slider,min,max sizes
214 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
216 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
218 if ( m_windowStyle & wxSL_LABELS )
222 GetWindowText((HWND) m_staticMin, buf, 300);
223 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
227 GetWindowText((HWND) m_staticMax, buf, 300);
228 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
231 int new_width = (int)(wxMax(min_len, max_len));
232 int valueHeight = (int)cyf;
234 // For some reason, under Win95, the text edit control has
235 // a lot of space before the first character
238 // The height needs to be a bit bigger under Win95 if using native
240 valueHeight = (int) (valueHeight * 1.5) ;
241 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
242 x_offset += new_width + cx;
245 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
246 x_offset += (int)(min_len + cx);
248 int slider_length = (int)(w1 - x_offset - max_len - cx);
250 int slider_height = h1;
251 if (slider_height < 0 )
254 // Slider must have a minimum/default length/height
255 if (slider_length < 100)
258 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
259 x_offset += slider_length + cx;
261 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
266 // If we're prepared to use the existing size, then...
267 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
275 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
280 if ( m_windowStyle & wxSL_LABELS )
283 GetWindowText((HWND) m_staticMin, buf, 300);
284 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
287 GetWindowText((HWND) m_staticMax, buf, 300);
288 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
292 int new_width = (int)(wxMax(min_len, max_len));
293 int valueHeight = (int)cyf;
294 //// Suggested change by George Tasker - remove this block...
296 // For some reason, under Win95, the text edit control has
297 // a lot of space before the first character
300 ... and replace with following line:
303 // The height needs to be a bit bigger under Win95 if using native
305 valueHeight = (int) (valueHeight * 1.5) ;
307 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
308 y_offset += valueHeight;
311 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
314 int slider_length = (int)(h1 - y_offset - cy - cy);
316 int slider_width = w1;
317 if (slider_width < 0 )
320 // Slider must have a minimum/default length
321 if (slider_length < 100)
324 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
325 y_offset += slider_length;
327 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
332 // If we're prepared to use the existing size, then...
333 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
341 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
347 void wxSlider::SetRange(int minValue
, int maxValue
)
349 m_rangeMin
= minValue
;
350 m_rangeMax
= maxValue
;
355 WXHBRUSH
wxSlider::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
356 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
360 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
363 // Otherwise, it's a static
364 if (GetParent()->GetTransparentBackground())
365 SetBkMode((HDC) pDC, TRANSPARENT);
367 SetBkMode((HDC) pDC, OPAQUE);
369 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
370 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
372 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
373 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
378 // For trackbars only
379 void wxSlider::SetTickFreq(int n
, int pos
)
385 void wxSlider::SetPageSize(int pageSize
)
388 m_pageSize
= pageSize
;
391 int wxSlider::GetPageSize() const
396 void wxSlider::ClearSel()
401 void wxSlider::ClearTicks()
406 void wxSlider::SetLineSize(int lineSize
)
408 m_lineSize
= lineSize
;
412 int wxSlider::GetLineSize() const
418 int wxSlider::GetSelEnd() const
424 int wxSlider::GetSelStart() const
430 void wxSlider::SetSelection(int minPos
, int maxPos
)
435 void wxSlider::SetThumbLength(int len
)
440 int wxSlider::GetThumbLength() const
446 void wxSlider::SetTick(int tickPos
)
451 bool wxSlider::ContainsHWND(WXHWND hWnd
) const
453 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
456 void wxSlider::Command (wxCommandEvent
& event
)
458 SetValue (event
.GetInt());
459 ProcessCommand (event
);
462 bool wxSlider::Show(bool show
)