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
)
44 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
45 int value
, int minValue
, int maxValue
,
47 const wxSize
& size
, long style
,
48 const wxValidator
& validator
,
52 SetValidator(validator
);
54 if (parent
) parent
->AddChild(this);
57 m_windowStyle
= style
;
61 m_windowId
= (int)NewControlId();
65 m_rangeMax
= maxValue
;
66 m_rangeMin
= minValue
;
68 m_pageSize
= (int)((maxValue
-minValue
)/10);
75 bool wxSlider::OS2OnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
76 WXWORD pos
, WXHWND control
)
78 int position
= 0; // Dummy - not used in this mode
81 wxEventType scrollEvent
= wxEVT_NULL
;
87 nScrollInc = m_rangeMax - position;
88 scrollEvent = wxEVT_SCROLL_TOP;
92 nScrollInc = - position;
93 scrollEvent = wxEVT_SCROLL_BOTTOM;
97 nScrollInc = - GetLineSize();
98 scrollEvent = wxEVT_SCROLL_LINEUP;
102 nScrollInc = GetLineSize();
103 scrollEvent = wxEVT_SCROLL_LINEDOWN;
107 nScrollInc = -GetPageSize();
108 scrollEvent = wxEVT_SCROLL_PAGEUP;
112 nScrollInc = GetPageSize();
113 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
117 case SB_THUMBPOSITION:
119 nScrollInc = (signed short)pos - position;
121 nScrollInc = pos - position;
123 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
130 if ( nScrollInc == 0 )
136 int newPos = (int)::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
137 if ( (newPos < GetMin()) || (newPos > GetMax()) )
139 // out of range - but we did process it
145 wxScrollEvent
event(scrollEvent
, m_windowId
);
146 event
.SetPosition(newPos
);
147 event
.SetEventObject( this );
148 GetEventHandler()->ProcessEvent(event
);
150 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
151 cevent
.SetEventObject( this );
153 return GetEventHandler()->ProcessEvent( cevent
);
156 wxSlider::~wxSlider()
161 int wxSlider::GetValue() const
167 void wxSlider::SetValue(int value
)
172 void wxSlider::GetSize(int *width
, int *height
) const
177 void wxSlider::GetPosition(int *x
, int *y
) const
182 // TODO one day, make sense of all this horros and replace it with a readable
184 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
191 int currentX
, currentY
;
192 GetPosition(¤tX
, ¤tY
);
193 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
195 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
201 AdjustForParentClientOrigin(x1, y1, sizeFlags);
208 int cx; // slider,min,max sizes
212 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
214 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
216 if ( m_windowStyle & wxSL_LABELS )
220 GetWindowText((HWND) m_staticMin, buf, 300);
221 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
225 GetWindowText((HWND) m_staticMax, buf, 300);
226 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
229 int new_width = (int)(wxMax(min_len, max_len));
230 int valueHeight = (int)cyf;
232 // For some reason, under Win95, the text edit control has
233 // a lot of space before the first character
236 // The height needs to be a bit bigger under Win95 if using native
238 valueHeight = (int) (valueHeight * 1.5) ;
239 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
240 x_offset += new_width + cx;
243 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
244 x_offset += (int)(min_len + cx);
246 int slider_length = (int)(w1 - x_offset - max_len - cx);
248 int slider_height = h1;
249 if (slider_height < 0 )
252 // Slider must have a minimum/default length/height
253 if (slider_length < 100)
256 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
257 x_offset += slider_length + cx;
259 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
264 // If we're prepared to use the existing size, then...
265 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
273 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
278 if ( m_windowStyle & wxSL_LABELS )
281 GetWindowText((HWND) m_staticMin, buf, 300);
282 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
285 GetWindowText((HWND) m_staticMax, buf, 300);
286 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
290 int new_width = (int)(wxMax(min_len, max_len));
291 int valueHeight = (int)cyf;
292 //// Suggested change by George Tasker - remove this block...
294 // For some reason, under Win95, the text edit control has
295 // a lot of space before the first character
298 ... and replace with following line:
301 // The height needs to be a bit bigger under Win95 if using native
303 valueHeight = (int) (valueHeight * 1.5) ;
305 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
306 y_offset += valueHeight;
309 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
312 int slider_length = (int)(h1 - y_offset - cy - cy);
314 int slider_width = w1;
315 if (slider_width < 0 )
318 // Slider must have a minimum/default length
319 if (slider_length < 100)
322 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
323 y_offset += slider_length;
325 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
330 // If we're prepared to use the existing size, then...
331 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
339 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
345 void wxSlider::SetRange(int minValue
, int maxValue
)
347 m_rangeMin
= minValue
;
348 m_rangeMax
= maxValue
;
353 WXHBRUSH
wxSlider::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
354 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
358 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
361 // Otherwise, it's a static
362 if (GetParent()->GetTransparentBackground())
363 SetBkMode((HDC) pDC, TRANSPARENT);
365 SetBkMode((HDC) pDC, OPAQUE);
367 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
368 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
370 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
371 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
376 // For trackbars only
377 void wxSlider::SetTickFreq(int n
, int pos
)
383 void wxSlider::SetPageSize(int pageSize
)
386 m_pageSize
= pageSize
;
389 int wxSlider::GetPageSize() const
394 void wxSlider::ClearSel()
399 void wxSlider::ClearTicks()
404 void wxSlider::SetLineSize(int lineSize
)
406 m_lineSize
= lineSize
;
410 int wxSlider::GetLineSize() const
416 int wxSlider::GetSelEnd() const
422 int wxSlider::GetSelStart() const
428 void wxSlider::SetSelection(int minPos
, int maxPos
)
433 void wxSlider::SetThumbLength(int len
)
438 int wxSlider::GetThumbLength() const
444 void wxSlider::SetTick(int tickPos
)
449 bool wxSlider::ContainsHWND(WXHWND hWnd
) const
451 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
454 void wxSlider::Command (wxCommandEvent
& event
)
456 SetValue (event
.GetInt());
457 ProcessCommand (event
);
460 bool wxSlider::Show(bool show
)