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
;
85 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
86 msStyle
|= WS_CLIPSIBLINGS
;
89 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
91 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
93 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
94 wxGetInstance(), NULL
);
96 // Now create min static control
97 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
98 DWORD wstyle
= STATIC_FLAGS
;
99 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
100 wstyle
|= WS_CLIPSIBLINGS
;
101 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
103 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
104 wxGetInstance(), NULL
);
107 if (m_windowStyle
& wxSL_VERTICAL
)
108 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
110 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
112 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("SCROLLBAR"), wxBuffer
,
114 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
115 wxGetInstance(), NULL
);
117 m_pageSize
= (int)((maxValue
-minValue
)/10);
118 m_rangeMax
= maxValue
;
119 m_rangeMin
= minValue
;
121 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
122 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
123 ShowWindow(scroll_bar
, SW_SHOW
);
125 m_hWnd
= (WXHWND
)scroll_bar
;
127 // Subclass again for purposes of dialog editing mode
128 SubclassWin(GetHWND());
130 // Finally, create max value static item
131 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
132 wstyle
= STATIC_FLAGS
;
133 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
134 wstyle
|= WS_CLIPSIBLINGS
;
135 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
137 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
138 wxGetInstance(), NULL
);
140 SetFont(parent
->GetFont());
144 // GetFont()->RealizeResource();
145 if (GetFont().GetResourceHandle())
148 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
149 (WPARAM
)GetFont().GetResourceHandle(),0L);
151 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
152 (WPARAM
)GetFont().GetResourceHandle(),0L);
154 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
155 (WPARAM
)GetFont().GetResourceHandle(),0L);
159 SetSize(x
, y
, width
, height
);
165 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
166 WXWORD pos
, WXHWND control
)
168 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
171 wxEventType scrollEvent
= wxEVT_NULL
;
175 nScrollInc
= m_rangeMax
- position
;
176 scrollEvent
= wxEVT_SCROLL_TOP
;
180 nScrollInc
= - position
;
181 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
185 nScrollInc
= - GetLineSize();
186 scrollEvent
= wxEVT_SCROLL_LINEUP
;
190 nScrollInc
= GetLineSize();
191 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
195 nScrollInc
= -GetPageSize();
196 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
200 nScrollInc
= GetPageSize();
201 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
205 case SB_THUMBPOSITION
:
207 nScrollInc
= (signed short)pos
- position
;
209 nScrollInc
= pos
- position
;
211 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
224 int newPos
= position
+ nScrollInc
;
226 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
228 // out of range - but we did process it
234 wxScrollEvent
event(scrollEvent
, m_windowId
);
235 event
.SetPosition(newPos
);
236 event
.SetEventObject( this );
237 GetEventHandler()->ProcessEvent(event
);
239 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
240 cevent
.SetInt( newPos
);
241 cevent
.SetEventObject( this );
243 return GetEventHandler()->ProcessEvent( cevent
);
246 wxSliderMSW::~wxSliderMSW()
249 DestroyWindow((HWND
) m_staticMin
);
251 DestroyWindow((HWND
) m_staticMax
);
253 DestroyWindow((HWND
) m_staticValue
);
256 int wxSliderMSW::GetValue() const
258 return ::GetScrollPos(GetHwnd(), SB_CTL
);
261 void wxSliderMSW::SetValue(int value
)
263 ::SetScrollPos(GetHwnd(), SB_CTL
, value
, TRUE
);
266 wxSprintf(wxBuffer
, wxT("%d"), value
);
267 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
271 void wxSliderMSW::GetSize(int *width
, int *height
) const
274 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
276 wxFindMaxSize(GetHWND(), &rect
);
279 wxFindMaxSize(m_staticMin
, &rect
);
281 wxFindMaxSize(m_staticMax
, &rect
);
283 wxFindMaxSize(m_staticValue
, &rect
);
285 *width
= rect
.right
- rect
.left
;
286 *height
= rect
.bottom
- rect
.top
;
289 void wxSliderMSW::GetPosition(int *x
, int *y
) const
291 wxWindow
*parent
= GetParent();
293 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
295 wxFindMaxSize(GetHWND(), &rect
);
298 wxFindMaxSize(m_staticMin
, &rect
);
300 wxFindMaxSize(m_staticMax
, &rect
);
302 wxFindMaxSize(m_staticValue
, &rect
);
304 // Since we now have the absolute screen coords,
305 // if there's a parent we must subtract its top left corner
310 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
312 // We may be faking the client origin.
313 // So a window that's really at (0, 30) may appear
314 // (to wxWin apps) to be at (0, 0).
317 wxPoint
pt(GetParent()->GetClientAreaOrigin());
325 // TODO one day, make sense of all this horros and replace it with a readable
327 void wxSliderMSW::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
334 int currentX
, currentY
;
335 GetPosition(¤tX
, ¤tY
);
336 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
338 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
341 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
348 int cx
; // slider,min,max sizes
352 wxGetCharSize(GetHWND(), &cx
, &cy
,& this->GetFont());
354 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
356 if ( m_windowStyle
& wxSL_LABELS
)
360 GetWindowText((HWND
) m_staticMin
, buf
, 300);
361 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
365 GetWindowText((HWND
) m_staticMax
, buf
, 300);
366 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
369 int new_width
= (int)(wxMax(min_len
, max_len
));
370 int valueHeight
= (int)cyf
;
372 // For some reason, under Win95, the text edit control has
373 // a lot of space before the first character
376 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
377 x_offset
+= new_width
+ cx
;
380 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
381 x_offset
+= (int)(min_len
+ cx
);
383 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
385 int slider_height
= cy
;
387 // Slider must have a minimum/default length/height
388 if (slider_length
< 100)
391 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
392 x_offset
+= slider_length
+ cx
;
394 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
403 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
408 if ( m_windowStyle
& wxSL_LABELS
)
411 GetWindowText((HWND
) m_staticMin
, buf
, 300);
412 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,& this->GetFont());
415 GetWindowText((HWND
) m_staticMax
, buf
, 300);
416 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
420 int new_width
= (int)(wxMax(min_len
, max_len
));
421 int valueHeight
= (int)cyf
;
422 /*** Suggested change by George Tasker - remove this block...
424 // For some reason, under Win95, the text edit control has
425 // a lot of space before the first character
428 ... and replace with following line: */
431 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
432 y_offset
+= valueHeight
;
435 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
438 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
440 // Use character height as an estimate of slider width (yes, width)
441 int slider_width
= cy
;
443 // Slider must have a minimum/default length
444 if (slider_length
< 100)
447 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
448 y_offset
+= slider_length
;
450 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
459 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
464 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
466 m_rangeMin
= minValue
;
467 m_rangeMax
= maxValue
;
469 ::SetScrollRange(GetHwnd(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
473 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
474 SetWindowText((HWND
) m_staticMin
, buf
);
479 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
480 SetWindowText((HWND
) m_staticMax
, buf
);
484 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
485 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
487 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
490 // Otherwise, it's a static
491 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
494 // For trackbars only
495 void wxSliderMSW::SetTickFreq(int n
, int pos
)
499 void wxSliderMSW::SetPageSize(int pageSize
)
501 m_pageSize
= pageSize
;
504 int wxSliderMSW::GetPageSize() const
509 void wxSliderMSW::ClearSel()
513 void wxSliderMSW::ClearTicks()
517 void wxSliderMSW::SetLineSize(int lineSize
)
519 m_lineSize
= lineSize
;
522 int wxSliderMSW::GetLineSize() const
527 int wxSliderMSW::GetSelEnd() const
532 int wxSliderMSW::GetSelStart() const
537 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
541 void wxSliderMSW::SetThumbLength(int len
)
545 int wxSliderMSW::GetThumbLength() const
550 void wxSliderMSW::SetTick(int tickPos
)
554 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
556 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
559 void wxSliderMSW::Command (wxCommandEvent
& event
)
561 SetValue (event
.GetInt());
562 ProcessCommand (event
);
565 bool wxSliderMSW::Show(bool show
)
567 wxWindow::Show(show
);
576 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
578 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
580 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);