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
);
309 void wxSliderMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
316 int currentX
, currentY
;
317 GetPosition(¤tX
, ¤tY
);
318 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
320 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
328 int cx
; // slider,min,max sizes
332 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
334 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
336 if ( m_windowStyle
& wxSL_LABELS
)
340 GetWindowText((HWND
) m_staticMin
, buf
, 300);
341 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, GetFont());
345 GetWindowText((HWND
) m_staticMax
, buf
, 300);
346 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, GetFont());
349 int new_width
= (int)(wxMax(min_len
, max_len
));
350 int valueHeight
= (int)cyf
;
352 // For some reason, under Win95, the text edit control has
353 // a lot of space before the first character
356 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
357 x_offset
+= new_width
+ cx
;
360 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
361 x_offset
+= (int)(min_len
+ cx
);
363 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
365 int slider_height
= cy
;
367 // Slider must have a minimum/default length/height
368 if (slider_length
< 100)
371 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
372 x_offset
+= slider_length
+ cx
;
374 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
383 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
388 if ( m_windowStyle
& wxSL_LABELS
)
391 GetWindowText((HWND
) m_staticMin
, buf
, 300);
392 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,GetFont());
395 GetWindowText((HWND
) m_staticMax
, buf
, 300);
396 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, GetFont());
400 int new_width
= (int)(wxMax(min_len
, max_len
));
401 int valueHeight
= (int)cyf
;
402 /*** Suggested change by George Tasker - remove this block...
404 // For some reason, under Win95, the text edit control has
405 // a lot of space before the first character
408 ... and replace with following line: */
411 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
412 y_offset
+= valueHeight
;
415 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
418 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
420 // Use character height as an estimate of slider width (yes, width)
421 int slider_width
= cy
;
423 // Slider must have a minimum/default length
424 if (slider_length
< 100)
427 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
428 y_offset
+= slider_length
;
430 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
439 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
444 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
446 m_rangeMin
= minValue
;
447 m_rangeMax
= maxValue
;
449 ::SetScrollRange((HWND
) GetHWND(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
453 sprintf(buf
, "%d", m_rangeMin
);
454 SetWindowText((HWND
) m_staticMin
, buf
);
459 sprintf(buf
, "%d", m_rangeMax
);
460 SetWindowText((HWND
) m_staticMax
, buf
);
464 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
465 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
467 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
470 // Otherwise, it's a static
471 if (GetParent()->GetTransparentBackground())
472 SetBkMode((HDC
) pDC
, TRANSPARENT
);
474 SetBkMode((HDC
) pDC
, OPAQUE
);
476 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
477 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
479 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
481 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
484 // For trackbars only
485 void wxSliderMSW::SetTickFreq(int n
, int pos
)
489 void wxSliderMSW::SetPageSize(int pageSize
)
491 m_pageSize
= pageSize
;
494 int wxSliderMSW::GetPageSize(void) const
499 void wxSliderMSW::ClearSel(void)
503 void wxSliderMSW::ClearTicks(void)
507 void wxSliderMSW::SetLineSize(int lineSize
)
509 m_lineSize
= lineSize
;
512 int wxSliderMSW::GetLineSize(void) const
517 int wxSliderMSW::GetSelEnd(void) const
522 int wxSliderMSW::GetSelStart(void) const
527 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
531 void wxSliderMSW::SetThumbLength(int len
)
535 int wxSliderMSW::GetThumbLength(void) const
540 void wxSliderMSW::SetTick(int tickPos
)
544 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
546 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
549 #if WXWIN_COMPATIBILITY
550 // Backward compatibility
551 void wxSliderMSW::OnScroll(wxScrollEvent
& event
)
553 wxEventType oldEvent
= event
.GetEventType();
554 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
555 if ( !GetEventHandler()->ProcessEvent(event
) )
557 event
.SetEventType( oldEvent
);
558 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
564 void wxSliderMSW::Command (wxCommandEvent
& event
)
566 SetValue (event
.GetInt());
567 ProcessCommand (event
);
570 bool wxSliderMSW::Show(bool show
)
572 wxWindow::Show(show
);
581 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
583 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
585 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);