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()
46 bool wxSliderMSW::Create(wxWindow
*parent
, wxWindowID id
,
47 int value
, int minValue
, int maxValue
,
49 const wxSize
& size
, long style
,
50 const wxValidator
& validator
,
55 SetValidator(validator
);
56 #endif // wxUSE_VALIDATORS
58 if (parent
) parent
->AddChild(this);
59 SetBackgroundColour(parent
->GetBackgroundColour()) ;
60 SetForegroundColour(parent
->GetForegroundColour()) ;
67 m_windowStyle
= style
;
70 m_windowId
= (int)NewControlId();
79 // non-Win95 implementation
81 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
83 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
84 msStyle
|= WS_CLIPSIBLINGS
;
87 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
89 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
91 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
92 wxGetInstance(), NULL
);
94 // Now create min static control
95 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
96 DWORD wstyle
= STATIC_FLAGS
;
97 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
98 wstyle
|= WS_CLIPSIBLINGS
;
99 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
101 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
102 wxGetInstance(), NULL
);
105 if (m_windowStyle
& wxSL_VERTICAL
)
106 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
108 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
110 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("SCROLLBAR"), wxBuffer
,
112 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
113 wxGetInstance(), NULL
);
115 m_pageSize
= (int)((maxValue
-minValue
)/10);
116 m_rangeMax
= maxValue
;
117 m_rangeMin
= minValue
;
119 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
120 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
121 ShowWindow(scroll_bar
, SW_SHOW
);
123 m_hWnd
= (WXHWND
)scroll_bar
;
125 // Subclass again for purposes of dialog editing mode
126 SubclassWin(GetHWND());
128 // Finally, create max value static item
129 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
130 wstyle
= STATIC_FLAGS
;
131 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
132 wstyle
|= WS_CLIPSIBLINGS
;
133 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
135 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
136 wxGetInstance(), NULL
);
138 SetFont(parent
->GetFont());
142 // GetFont()->RealizeResource();
143 if (GetFont().GetResourceHandle())
146 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
147 (WPARAM
)GetFont().GetResourceHandle(),0L);
149 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
150 (WPARAM
)GetFont().GetResourceHandle(),0L);
152 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
153 (WPARAM
)GetFont().GetResourceHandle(),0L);
157 SetSize(x
, y
, width
, height
);
163 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
164 WXWORD pos
, WXHWND control
)
166 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
169 wxEventType scrollEvent
= wxEVT_NULL
;
173 nScrollInc
= m_rangeMax
- position
;
174 scrollEvent
= wxEVT_SCROLL_TOP
;
178 nScrollInc
= - position
;
179 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
183 nScrollInc
= - GetLineSize();
184 scrollEvent
= wxEVT_SCROLL_LINEUP
;
188 nScrollInc
= GetLineSize();
189 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
193 nScrollInc
= -GetPageSize();
194 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
198 nScrollInc
= GetPageSize();
199 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
203 case SB_THUMBPOSITION
:
205 nScrollInc
= (signed short)pos
- position
;
207 nScrollInc
= pos
- position
;
209 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
222 int newPos
= position
+ nScrollInc
;
224 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
226 // out of range - but we did process it
232 wxScrollEvent
event(scrollEvent
, m_windowId
);
233 event
.SetPosition(newPos
);
234 event
.SetEventObject( this );
235 GetEventHandler()->ProcessEvent(event
);
237 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
238 cevent
.SetInt( newPos
);
239 cevent
.SetEventObject( this );
241 return GetEventHandler()->ProcessEvent( cevent
);
244 wxSliderMSW::~wxSliderMSW()
247 DestroyWindow((HWND
) m_staticMin
);
249 DestroyWindow((HWND
) m_staticMax
);
251 DestroyWindow((HWND
) m_staticValue
);
254 int wxSliderMSW::GetValue() const
256 return ::GetScrollPos(GetHwnd(), SB_CTL
);
259 void wxSliderMSW::SetValue(int value
)
261 ::SetScrollPos(GetHwnd(), SB_CTL
, value
, TRUE
);
264 wxSprintf(wxBuffer
, wxT("%d"), value
);
265 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
269 void wxSliderMSW::GetSize(int *width
, int *height
) const
272 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
274 wxFindMaxSize(GetHWND(), &rect
);
277 wxFindMaxSize(m_staticMin
, &rect
);
279 wxFindMaxSize(m_staticMax
, &rect
);
281 wxFindMaxSize(m_staticValue
, &rect
);
283 *width
= rect
.right
- rect
.left
;
284 *height
= rect
.bottom
- rect
.top
;
287 void wxSliderMSW::GetPosition(int *x
, int *y
) const
289 wxWindow
*parent
= GetParent();
291 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
293 wxFindMaxSize(GetHWND(), &rect
);
296 wxFindMaxSize(m_staticMin
, &rect
);
298 wxFindMaxSize(m_staticMax
, &rect
);
300 wxFindMaxSize(m_staticValue
, &rect
);
302 // Since we now have the absolute screen coords,
303 // if there's a parent we must subtract its top left corner
308 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
310 // We may be faking the client origin.
311 // So a window that's really at (0, 30) may appear
312 // (to wxWin apps) to be at (0, 0).
315 wxPoint
pt(GetParent()->GetClientAreaOrigin());
323 // TODO one day, make sense of all this horros and replace it with a readable
325 void wxSliderMSW::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
332 int currentX
, currentY
;
333 GetPosition(¤tX
, ¤tY
);
334 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
336 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
339 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
346 int cx
; // slider,min,max sizes
350 wxGetCharSize(GetHWND(), &cx
, &cy
,& this->GetFont());
352 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
354 if ( m_windowStyle
& wxSL_LABELS
)
358 GetWindowText((HWND
) m_staticMin
, buf
, 300);
359 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
363 GetWindowText((HWND
) m_staticMax
, buf
, 300);
364 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
367 int new_width
= (int)(wxMax(min_len
, max_len
));
368 int valueHeight
= (int)cyf
;
370 // For some reason, under Win95, the text edit control has
371 // a lot of space before the first character
374 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
375 x_offset
+= new_width
+ cx
;
378 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
379 x_offset
+= (int)(min_len
+ cx
);
381 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
383 int slider_height
= cy
;
385 // Slider must have a minimum/default length/height
386 if (slider_length
< 100)
389 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
390 x_offset
+= slider_length
+ cx
;
392 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
401 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
406 if ( m_windowStyle
& wxSL_LABELS
)
409 GetWindowText((HWND
) m_staticMin
, buf
, 300);
410 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,& this->GetFont());
413 GetWindowText((HWND
) m_staticMax
, buf
, 300);
414 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
418 int new_width
= (int)(wxMax(min_len
, max_len
));
419 int valueHeight
= (int)cyf
;
420 /*** Suggested change by George Tasker - remove this block...
422 // For some reason, under Win95, the text edit control has
423 // a lot of space before the first character
426 ... and replace with following line: */
429 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
430 y_offset
+= valueHeight
;
433 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
436 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
438 // Use character height as an estimate of slider width (yes, width)
439 int slider_width
= cy
;
441 // Slider must have a minimum/default length
442 if (slider_length
< 100)
445 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
446 y_offset
+= slider_length
;
448 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
457 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
462 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
464 m_rangeMin
= minValue
;
465 m_rangeMax
= maxValue
;
467 ::SetScrollRange(GetHwnd(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
471 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
472 SetWindowText((HWND
) m_staticMin
, buf
);
477 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
478 SetWindowText((HWND
) m_staticMax
, buf
);
482 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
483 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
485 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
488 // Otherwise, it's a static
489 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
492 void wxSliderMSW::SetPageSize(int pageSize
)
494 m_pageSize
= pageSize
;
497 int wxSliderMSW::GetPageSize() const
502 void wxSliderMSW::SetLineSize(int lineSize
)
504 m_lineSize
= lineSize
;
507 int wxSliderMSW::GetLineSize() const
512 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
514 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
517 void wxSliderMSW::Command (wxCommandEvent
& event
)
519 SetValue (event
.GetInt());
520 ProcessCommand (event
);
523 bool wxSliderMSW::Show(bool show
)
525 wxWindow::Show(show
);
534 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
536 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
538 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);