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 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
43 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
44 int value
, int minValue
, int maxValue
,
46 const wxSize
& size
, long style
,
48 const wxValidator
& validator
,
54 SetValidator(validator
);
57 if (parent
) parent
->AddChild(this);
60 m_windowStyle
= style
;
64 m_windowId
= (int)NewControlId();
68 m_rangeMax
= maxValue
;
69 m_rangeMin
= minValue
;
71 m_pageSize
= (int)((maxValue
-minValue
)/10);
78 bool wxSlider::OS2OnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
79 WXWORD pos
, WXHWND control
)
81 int position
= 0; // Dummy - not used in this mode
84 wxEventType scrollEvent
= wxEVT_NULL
;
90 nScrollInc = m_rangeMax - position;
91 scrollEvent = wxEVT_SCROLL_TOP;
95 nScrollInc = - position;
96 scrollEvent = wxEVT_SCROLL_BOTTOM;
100 nScrollInc = - GetLineSize();
101 scrollEvent = wxEVT_SCROLL_LINEUP;
105 nScrollInc = GetLineSize();
106 scrollEvent = wxEVT_SCROLL_LINEDOWN;
110 nScrollInc = -GetPageSize();
111 scrollEvent = wxEVT_SCROLL_PAGEUP;
115 nScrollInc = GetPageSize();
116 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
120 case SB_THUMBPOSITION:
122 nScrollInc = (signed short)pos - position;
124 nScrollInc = pos - position;
126 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
133 if ( nScrollInc == 0 )
139 int newPos = (int)::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
140 if ( (newPos < GetMin()) || (newPos > GetMax()) )
142 // out of range - but we did process it
146 int newPos
= 0; //temporary
149 wxScrollEvent
event(scrollEvent
, m_windowId
);
150 event
.SetPosition(newPos
);
151 event
.SetEventObject( this );
152 GetEventHandler()->ProcessEvent(event
);
154 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
155 cevent
.SetEventObject( this );
157 return GetEventHandler()->ProcessEvent( cevent
);
160 wxSlider::~wxSlider()
165 int wxSlider::GetValue() const
171 void wxSlider::SetValue(int value
)
176 void wxSlider::GetSize(int *width
, int *height
) const
181 void wxSlider::GetPosition(int *x
, int *y
) const
186 // TODO one day, make sense of all this horros and replace it with a readable
188 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
195 int currentX
, currentY
;
196 GetPosition(¤tX
, ¤tY
);
197 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
199 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
205 AdjustForParentClientOrigin(x1, y1, sizeFlags);
212 int cx; // slider,min,max sizes
216 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
218 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
220 if ( m_windowStyle & wxSL_LABELS )
224 GetWindowText((HWND) m_staticMin, buf, 300);
225 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
229 GetWindowText((HWND) m_staticMax, buf, 300);
230 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
233 int new_width = (int)(wxMax(min_len, max_len));
234 int valueHeight = (int)cyf;
236 // For some reason, under Win95, the text edit control has
237 // a lot of space before the first character
240 // The height needs to be a bit bigger under Win95 if using native
242 valueHeight = (int) (valueHeight * 1.5) ;
243 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
244 x_offset += new_width + cx;
247 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
248 x_offset += (int)(min_len + cx);
250 int slider_length = (int)(w1 - x_offset - max_len - cx);
252 int slider_height = h1;
253 if (slider_height < 0 )
256 // Slider must have a minimum/default length/height
257 if (slider_length < 100)
260 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
261 x_offset += slider_length + cx;
263 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
268 // If we're prepared to use the existing size, then...
269 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
277 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
282 if ( m_windowStyle & wxSL_LABELS )
285 GetWindowText((HWND) m_staticMin, buf, 300);
286 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
289 GetWindowText((HWND) m_staticMax, buf, 300);
290 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
294 int new_width = (int)(wxMax(min_len, max_len));
295 int valueHeight = (int)cyf;
296 //// Suggested change by George Tasker - remove this block...
298 // For some reason, under Win95, the text edit control has
299 // a lot of space before the first character
302 ... and replace with following line:
305 // The height needs to be a bit bigger under Win95 if using native
307 valueHeight = (int) (valueHeight * 1.5) ;
309 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
310 y_offset += valueHeight;
313 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
316 int slider_length = (int)(h1 - y_offset - cy - cy);
318 int slider_width = w1;
319 if (slider_width < 0 )
322 // Slider must have a minimum/default length
323 if (slider_length < 100)
326 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
327 y_offset += slider_length;
329 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
334 // If we're prepared to use the existing size, then...
335 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
343 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
349 void wxSlider::SetRange(int minValue
, int maxValue
)
351 m_rangeMin
= minValue
;
352 m_rangeMax
= maxValue
;
357 WXHBRUSH
wxSlider::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
358 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
362 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
365 // Otherwise, it's a static
366 if (GetParent()->GetTransparentBackground())
367 SetBkMode((HDC) pDC, TRANSPARENT);
369 SetBkMode((HDC) pDC, OPAQUE);
371 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
372 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
374 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
375 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
380 // For trackbars only
381 void wxSlider::SetTickFreq(int n
, int pos
)
387 void wxSlider::SetPageSize(int pageSize
)
390 m_pageSize
= pageSize
;
393 int wxSlider::GetPageSize() const
398 void wxSlider::ClearSel()
403 void wxSlider::ClearTicks()
408 void wxSlider::SetLineSize(int lineSize
)
410 m_lineSize
= lineSize
;
414 int wxSlider::GetLineSize() const
420 int wxSlider::GetSelEnd() const
426 int wxSlider::GetSelStart() const
432 void wxSlider::SetSelection(int minPos
, int maxPos
)
437 void wxSlider::SetThumbLength(int len
)
442 int wxSlider::GetThumbLength() const
448 void wxSlider::SetTick(int tickPos
)
453 bool wxSlider::ContainsHWND(WXHWND hWnd
) const
455 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
458 void wxSlider::Command (wxCommandEvent
& event
)
460 SetValue (event
.GetInt());
461 ProcessCommand (event
);
464 bool wxSlider::Show(bool show
)