1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSliderMSW
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slidrmsw.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/msw/slidrmsw.h"
30 #include "wx/msw/private.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW
, wxControl
)
35 wxSliderMSW::wxSliderMSW()
47 bool wxSliderMSW::Create(wxWindow
*parent
, wxWindowID id
,
48 int value
, int minValue
, int maxValue
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
56 SetValidator(validator
);
57 #endif // wxUSE_VALIDATORS
59 if (parent
) parent
->AddChild(this);
60 SetBackgroundColour(parent
->GetBackgroundColour()) ;
61 SetForegroundColour(parent
->GetForegroundColour()) ;
68 m_windowStyle
= style
;
72 m_windowId
= (int)NewControlId();
81 // non-Win95 implementation
83 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
86 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
88 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
90 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
91 wxGetInstance(), NULL
);
93 // Now create min static control
94 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
95 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
97 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
98 wxGetInstance(), NULL
);
101 if (m_windowStyle
& wxSL_VERTICAL
)
102 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
104 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
106 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("SCROLLBAR"), wxBuffer
,
108 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
109 wxGetInstance(), NULL
);
111 m_pageSize
= (int)((maxValue
-minValue
)/10);
112 m_rangeMax
= maxValue
;
113 m_rangeMin
= minValue
;
115 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
116 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
117 ShowWindow(scroll_bar
, SW_SHOW
);
119 m_hWnd
= (WXHWND
)scroll_bar
;
121 // Subclass again for purposes of dialog editing mode
122 SubclassWin(GetHWND());
124 // Finally, create max value static item
125 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
126 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
128 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
129 wxGetInstance(), NULL
);
131 SetFont(parent
->GetFont());
135 // GetFont()->RealizeResource();
136 if (GetFont().GetResourceHandle())
139 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
140 (WPARAM
)GetFont().GetResourceHandle(),0L);
142 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
143 (WPARAM
)GetFont().GetResourceHandle(),0L);
145 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
146 (WPARAM
)GetFont().GetResourceHandle(),0L);
150 SetSize(x
, y
, width
, height
);
156 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
157 WXWORD pos
, WXHWND control
)
159 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
162 wxEventType scrollEvent
= wxEVT_NULL
;
166 nScrollInc
= m_rangeMax
- position
;
167 scrollEvent
= wxEVT_SCROLL_TOP
;
171 nScrollInc
= - position
;
172 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
176 nScrollInc
= - GetLineSize();
177 scrollEvent
= wxEVT_SCROLL_LINEUP
;
181 nScrollInc
= GetLineSize();
182 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
186 nScrollInc
= -GetPageSize();
187 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
191 nScrollInc
= GetPageSize();
192 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
196 case SB_THUMBPOSITION
:
198 nScrollInc
= (signed short)pos
- position
;
200 nScrollInc
= pos
- position
;
202 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
215 int newPos
= position
+ nScrollInc
;
217 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
219 // out of range - but we did process it
225 wxScrollEvent
event(scrollEvent
, m_windowId
);
226 event
.SetPosition(newPos
);
227 event
.SetEventObject( this );
228 GetEventHandler()->ProcessEvent(event
);
230 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
231 cevent
.SetInt( newPos
);
232 cevent
.SetEventObject( this );
234 return GetEventHandler()->ProcessEvent( cevent
);
237 wxSliderMSW::~wxSliderMSW()
240 DestroyWindow((HWND
) m_staticMin
);
242 DestroyWindow((HWND
) m_staticMax
);
244 DestroyWindow((HWND
) m_staticValue
);
247 int wxSliderMSW::GetValue() const
249 return ::GetScrollPos(GetHwnd(), SB_CTL
);
252 void wxSliderMSW::SetValue(int value
)
254 ::SetScrollPos(GetHwnd(), SB_CTL
, value
, TRUE
);
257 wxSprintf(wxBuffer
, wxT("%d"), value
);
258 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
262 void wxSliderMSW::GetSize(int *width
, int *height
) const
265 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
267 wxFindMaxSize(GetHWND(), &rect
);
270 wxFindMaxSize(m_staticMin
, &rect
);
272 wxFindMaxSize(m_staticMax
, &rect
);
274 wxFindMaxSize(m_staticValue
, &rect
);
276 *width
= rect
.right
- rect
.left
;
277 *height
= rect
.bottom
- rect
.top
;
280 void wxSliderMSW::GetPosition(int *x
, int *y
) const
282 wxWindow
*parent
= GetParent();
284 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
286 wxFindMaxSize(GetHWND(), &rect
);
289 wxFindMaxSize(m_staticMin
, &rect
);
291 wxFindMaxSize(m_staticMax
, &rect
);
293 wxFindMaxSize(m_staticValue
, &rect
);
295 // Since we now have the absolute screen coords,
296 // if there's a parent we must subtract its top left corner
301 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
303 // We may be faking the client origin.
304 // So a window that's really at (0, 30) may appear
305 // (to wxWin apps) to be at (0, 0).
308 wxPoint
pt(GetParent()->GetClientAreaOrigin());
316 // TODO one day, make sense of all this horros and replace it with a readable
318 void wxSliderMSW::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
325 int currentX
, currentY
;
326 GetPosition(¤tX
, ¤tY
);
327 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
329 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
332 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
339 int cx
; // slider,min,max sizes
343 wxGetCharSize(GetHWND(), &cx
, &cy
,& this->GetFont());
345 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
347 if ( m_windowStyle
& wxSL_LABELS
)
351 GetWindowText((HWND
) m_staticMin
, buf
, 300);
352 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
356 GetWindowText((HWND
) m_staticMax
, buf
, 300);
357 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
360 int new_width
= (int)(wxMax(min_len
, max_len
));
361 int valueHeight
= (int)cyf
;
363 // For some reason, under Win95, the text edit control has
364 // a lot of space before the first character
367 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
368 x_offset
+= new_width
+ cx
;
371 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
372 x_offset
+= (int)(min_len
+ cx
);
374 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
376 int slider_height
= cy
;
378 // Slider must have a minimum/default length/height
379 if (slider_length
< 100)
382 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
383 x_offset
+= slider_length
+ cx
;
385 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
394 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
399 if ( m_windowStyle
& wxSL_LABELS
)
402 GetWindowText((HWND
) m_staticMin
, buf
, 300);
403 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,& this->GetFont());
406 GetWindowText((HWND
) m_staticMax
, buf
, 300);
407 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
411 int new_width
= (int)(wxMax(min_len
, max_len
));
412 int valueHeight
= (int)cyf
;
413 /*** Suggested change by George Tasker - remove this block...
415 // For some reason, under Win95, the text edit control has
416 // a lot of space before the first character
419 ... and replace with following line: */
422 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
423 y_offset
+= valueHeight
;
426 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
429 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
431 // Use character height as an estimate of slider width (yes, width)
432 int slider_width
= cy
;
434 // Slider must have a minimum/default length
435 if (slider_length
< 100)
438 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
439 y_offset
+= slider_length
;
441 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
450 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
455 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
457 m_rangeMin
= minValue
;
458 m_rangeMax
= maxValue
;
460 ::SetScrollRange(GetHwnd(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
464 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
465 SetWindowText((HWND
) m_staticMin
, buf
);
470 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
471 SetWindowText((HWND
) m_staticMax
, buf
);
475 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
476 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
478 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
481 // Otherwise, it's a static
482 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
485 // For trackbars only
486 void wxSliderMSW::SetTickFreq(int n
, int pos
)
490 void wxSliderMSW::SetPageSize(int pageSize
)
492 m_pageSize
= pageSize
;
495 int wxSliderMSW::GetPageSize() const
500 void wxSliderMSW::ClearSel()
504 void wxSliderMSW::ClearTicks()
508 void wxSliderMSW::SetLineSize(int lineSize
)
510 m_lineSize
= lineSize
;
513 int wxSliderMSW::GetLineSize() const
518 int wxSliderMSW::GetSelEnd() const
523 int wxSliderMSW::GetSelStart() const
528 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
532 void wxSliderMSW::SetThumbLength(int len
)
536 int wxSliderMSW::GetThumbLength() const
541 void wxSliderMSW::SetTick(int tickPos
)
545 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
547 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
550 void wxSliderMSW::Command (wxCommandEvent
& event
)
552 SetValue (event
.GetInt());
553 ProcessCommand (event
);
556 bool wxSliderMSW::Show(bool show
)
558 wxWindow::Show(show
);
567 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
569 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
571 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);