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
,
55 SetValidator(validator
);
57 if (parent
) parent
->AddChild(this);
58 SetBackgroundColour(parent
->GetBackgroundColour()) ;
59 SetForegroundColour(parent
->GetForegroundColour()) ;
66 m_windowStyle
= style
;
70 m_windowId
= (int)NewControlId();
79 // non-Win95 implementation
81 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
84 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
86 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
88 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
89 wxGetInstance(), NULL
);
91 // Now create min static control
92 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
93 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
95 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
96 wxGetInstance(), NULL
);
99 m_windowId
= (int)NewControlId();
102 if (m_windowStyle
& wxSL_VERTICAL
)
103 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
105 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
107 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("SCROLLBAR"), wxBuffer
,
109 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
110 wxGetInstance(), NULL
);
112 m_pageSize
= (int)((maxValue
-minValue
)/10);
113 m_rangeMax
= maxValue
;
114 m_rangeMin
= minValue
;
116 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
117 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
118 ShowWindow(scroll_bar
, SW_SHOW
);
120 m_hWnd
= (WXHWND
)scroll_bar
;
122 // Subclass again for purposes of dialog editing mode
123 SubclassWin(GetHWND());
125 // Finally, create max value static item
126 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
127 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
129 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
130 wxGetInstance(), NULL
);
132 SetFont(parent
->GetFont());
136 // GetFont()->RealizeResource();
137 if (GetFont().GetResourceHandle())
140 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
141 (WPARAM
)GetFont().GetResourceHandle(),0L);
143 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
144 (WPARAM
)GetFont().GetResourceHandle(),0L);
146 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
147 (WPARAM
)GetFont().GetResourceHandle(),0L);
151 SetSize(x
, y
, width
, height
);
157 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
158 WXWORD pos
, WXHWND control
)
160 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
163 wxEventType scrollEvent
= wxEVT_NULL
;
167 nScrollInc
= m_rangeMax
- position
;
168 scrollEvent
= wxEVT_SCROLL_TOP
;
172 nScrollInc
= - position
;
173 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
177 nScrollInc
= - GetLineSize();
178 scrollEvent
= wxEVT_SCROLL_LINEUP
;
182 nScrollInc
= GetLineSize();
183 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
187 nScrollInc
= -GetPageSize();
188 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
192 nScrollInc
= GetPageSize();
193 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
197 case SB_THUMBPOSITION
:
199 nScrollInc
= (signed short)pos
- position
;
201 nScrollInc
= pos
- position
;
203 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
216 int newPos
= position
+ nScrollInc
;
218 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
220 // out of range - but we did process it
226 wxScrollEvent
event(scrollEvent
, m_windowId
);
227 event
.SetPosition(newPos
);
228 event
.SetEventObject( this );
229 GetEventHandler()->ProcessEvent(event
);
231 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
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
);