1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSliderMSW
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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
= SS_CENTER
;
83 // WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
85 msStyle
|= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
87 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
89 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
90 wxGetInstance(), NULL
);
92 // Now create min static control
94 buf
.Printf(wxT("%d"), minValue
);
95 DWORD wstyle
= STATIC_FLAGS
;
96 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
97 wstyle
|= WS_CLIPSIBLINGS
;
98 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), buf
,
100 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
101 wxGetInstance(), NULL
);
104 if (m_windowStyle
& wxSL_VERTICAL
)
105 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
107 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
109 HWND scroll_bar
= CreateWindowEx(exStyle
, wxT("SCROLLBAR"), wxT(""),
111 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
112 wxGetInstance(), NULL
);
114 m_pageSize
= (int)((maxValue
-minValue
)/10);
115 m_rangeMax
= maxValue
;
116 m_rangeMin
= minValue
;
118 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
119 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
120 ShowWindow(scroll_bar
, SW_SHOW
);
122 m_hWnd
= (WXHWND
)scroll_bar
;
124 // Subclass again for purposes of dialog editing mode
125 SubclassWin(GetHWND());
127 // Finally, create max value static item
128 buf
.Printf(wxT("%d"), maxValue
);
129 wstyle
= STATIC_FLAGS
;
130 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
131 wstyle
|= WS_CLIPSIBLINGS
;
132 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), buf
,
134 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
135 wxGetInstance(), NULL
);
137 SetFont(parent
->GetFont());
141 // GetFont()->RealizeResource();
142 if (GetFont().GetResourceHandle())
145 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
146 (WPARAM
)GetFont().GetResourceHandle(),0L);
148 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
149 (WPARAM
)GetFont().GetResourceHandle(),0L);
151 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
152 (WPARAM
)GetFont().GetResourceHandle(),0L);
156 SetSize(x
, y
, width
, height
);
162 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
163 WXWORD pos
, WXHWND control
)
165 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
168 wxEventType scrollEvent
= wxEVT_NULL
;
172 nScrollInc
= m_rangeMax
- position
;
173 scrollEvent
= wxEVT_SCROLL_TOP
;
177 nScrollInc
= - position
;
178 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
182 nScrollInc
= - GetLineSize();
183 scrollEvent
= wxEVT_SCROLL_LINEUP
;
187 nScrollInc
= GetLineSize();
188 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
192 nScrollInc
= -GetPageSize();
193 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
197 nScrollInc
= GetPageSize();
198 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
202 case SB_THUMBPOSITION
:
204 nScrollInc
= (signed short)pos
- position
;
206 nScrollInc
= pos
- position
;
208 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
221 int newPos
= position
+ nScrollInc
;
223 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
225 // out of range - but we did process it
231 wxScrollEvent
event(scrollEvent
, m_windowId
);
232 event
.SetPosition(newPos
);
233 event
.SetEventObject( this );
234 GetEventHandler()->ProcessEvent(event
);
236 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
237 cevent
.SetInt( newPos
);
238 cevent
.SetEventObject( this );
240 return GetEventHandler()->ProcessEvent( cevent
);
243 wxSliderMSW::~wxSliderMSW()
246 DestroyWindow((HWND
) m_staticMin
);
248 DestroyWindow((HWND
) m_staticMax
);
250 DestroyWindow((HWND
) m_staticValue
);
253 int wxSliderMSW::GetValue() const
255 return ::GetScrollPos(GetHwnd(), SB_CTL
);
258 void wxSliderMSW::SetValue(int value
)
260 ::SetScrollPos(GetHwnd(), SB_CTL
, value
, TRUE
);
264 buf
.Printf(wxT("%d"), value
);
265 SetWindowText((HWND
) m_staticValue
, buf
);
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 // Not yet implemented
513 void wxSliderMSW::SetThumbLength(int WXUNUSED(lenPixels
))
517 // Not yet implemented
518 int wxSliderMSW::GetThumbLength() const
523 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
525 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
528 void wxSliderMSW::Command (wxCommandEvent
& event
)
530 SetValue (event
.GetInt());
531 ProcessCommand (event
);
534 bool wxSliderMSW::Show(bool show
)
536 wxWindow::Show(show
);
545 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
547 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
549 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);