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"
27 #include "wx/msw/slidrmsw.h"
28 #include "wx/msw/private.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW
, wxControl
)
33 #if WXWIN_COMPATIBILITY
34 BEGIN_EVENT_TABLE(wxSliderMSW
, wxControl
)
35 EVT_SCROLL(wxSliderMSW::OnScroll
)
42 wxSliderMSW::wxSliderMSW(void)
54 bool wxSliderMSW::Create(wxWindow
*parent
, wxWindowID id
,
55 int value
, int minValue
, int maxValue
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
62 SetValidator(validator
);
64 if (parent
) parent
->AddChild(this);
65 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
66 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
73 m_windowStyle
= style
;
77 m_windowId
= (int)NewControlId();
86 // non-Win95 implementation
88 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
91 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
93 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, "STATIC", NULL
,
95 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
96 wxGetInstance(), NULL
);
98 // Now create min static control
99 sprintf(wxBuffer
, "%d", minValue
);
100 m_staticMin
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
102 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
103 wxGetInstance(), NULL
);
106 m_windowId
= (int)NewControlId();
109 if (m_windowStyle
& wxSL_VERTICAL
)
110 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
112 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
114 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "SCROLLBAR", wxBuffer
,
116 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
117 wxGetInstance(), NULL
);
119 m_pageSize
= (int)((maxValue
-minValue
)/10);
120 m_rangeMax
= maxValue
;
121 m_rangeMin
= minValue
;
123 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
124 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
125 ShowWindow(scroll_bar
, SW_SHOW
);
127 m_hWnd
= (WXHWND
)scroll_bar
;
129 // Subclass again for purposes of dialog editing mode
130 SubclassWin(GetHWND());
132 // Finally, create max value static item
133 sprintf(wxBuffer
, "%d", maxValue
);
134 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
136 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
137 wxGetInstance(), NULL
);
139 SetFont(* parent
->GetFont());
143 // GetFont()->RealizeResource();
144 if (GetFont()->GetResourceHandle())
147 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
148 (WPARAM
)GetFont()->GetResourceHandle(),0L);
150 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
151 (WPARAM
)GetFont()->GetResourceHandle(),0L);
153 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
154 (WPARAM
)GetFont()->GetResourceHandle(),0L);
158 SetSize(x
, y
, width
, height
);
164 void wxSliderMSW::MSWOnVScroll(WXWORD wParam
, 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
;
220 int newPos
= position
+ nScrollInc
;
222 if (!(newPos
< GetMin() || newPos
> GetMax()))
226 wxScrollEvent
event(scrollEvent
, m_windowId
);
227 event
.SetPosition(newPos
);
228 event
.SetEventObject( this );
229 GetEventHandler()->ProcessEvent(event
);
234 void wxSliderMSW::MSWOnHScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
236 MSWOnVScroll(wParam
, pos
, control
);
239 wxSliderMSW::~wxSliderMSW(void)
242 DestroyWindow((HWND
) m_staticMin
);
244 DestroyWindow((HWND
) m_staticMax
);
246 DestroyWindow((HWND
) m_staticValue
);
249 int wxSliderMSW::GetValue(void) const
251 return ::GetScrollPos((HWND
) GetHWND(), SB_CTL
);
254 void wxSliderMSW::SetValue(int value
)
256 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, value
, TRUE
);
259 sprintf(wxBuffer
, "%d", value
);
260 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
264 void wxSliderMSW::GetSize(int *width
, int *height
) const
267 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
269 wxFindMaxSize(GetHWND(), &rect
);
272 wxFindMaxSize(m_staticMin
, &rect
);
274 wxFindMaxSize(m_staticMax
, &rect
);
276 wxFindMaxSize(m_staticValue
, &rect
);
278 *width
= rect
.right
- rect
.left
;
279 *height
= rect
.bottom
- rect
.top
;
282 void wxSliderMSW::GetPosition(int *x
, int *y
) const
284 wxWindow
*parent
= GetParent();
286 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
288 wxFindMaxSize(GetHWND(), &rect
);
291 wxFindMaxSize(m_staticMin
, &rect
);
293 wxFindMaxSize(m_staticMax
, &rect
);
295 wxFindMaxSize(m_staticValue
, &rect
);
297 // Since we now have the absolute screen coords,
298 // if there's a parent we must subtract its top left corner
303 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
305 // We may be faking the client origin.
306 // So a window that's really at (0, 30) may appear
307 // (to wxWin apps) to be at (0, 0).
310 wxPoint
pt(GetParent()->GetClientAreaOrigin());
318 void wxSliderMSW::SetSize(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
,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
, GetFont());
356 GetWindowText((HWND
) m_staticMax
, buf
, 300);
357 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, 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((HWND
) 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((HWND
) 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
,GetFont());
406 GetWindowText((HWND
) m_staticMax
, buf
, 300);
407 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, 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((HWND
) 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((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
455 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
457 m_rangeMin
= minValue
;
458 m_rangeMax
= maxValue
;
460 ::SetScrollRange((HWND
) GetHWND(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
464 sprintf(buf
, "%d", m_rangeMin
);
465 SetWindowText((HWND
) m_staticMin
, buf
);
470 sprintf(buf
, "%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 if (GetParent()->GetTransparentBackground())
483 SetBkMode((HDC
) pDC
, TRANSPARENT
);
485 SetBkMode((HDC
) pDC
, OPAQUE
);
487 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
488 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
490 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
492 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
495 // For trackbars only
496 void wxSliderMSW::SetTickFreq(int n
, int pos
)
500 void wxSliderMSW::SetPageSize(int pageSize
)
502 m_pageSize
= pageSize
;
505 int wxSliderMSW::GetPageSize(void) const
510 void wxSliderMSW::ClearSel(void)
514 void wxSliderMSW::ClearTicks(void)
518 void wxSliderMSW::SetLineSize(int lineSize
)
520 m_lineSize
= lineSize
;
523 int wxSliderMSW::GetLineSize(void) const
528 int wxSliderMSW::GetSelEnd(void) const
533 int wxSliderMSW::GetSelStart(void) const
538 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
542 void wxSliderMSW::SetThumbLength(int len
)
546 int wxSliderMSW::GetThumbLength(void) const
551 void wxSliderMSW::SetTick(int tickPos
)
555 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
557 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
560 #if WXWIN_COMPATIBILITY
561 // Backward compatibility
562 void wxSliderMSW::OnScroll(wxScrollEvent
& event
)
564 wxEventType oldEvent
= event
.GetEventType();
565 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
566 if ( !GetEventHandler()->ProcessEvent(event
) )
568 event
.SetEventType( oldEvent
);
569 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
575 void wxSliderMSW::Command (wxCommandEvent
& event
)
577 SetValue (event
.GetInt());
578 ProcessCommand (event
);
581 bool wxSliderMSW::Show(bool show
)
583 wxWindow::Show(show
);
592 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
594 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
596 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);