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 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW
, wxControl
)
37 wxSliderMSW::wxSliderMSW(void)
49 bool wxSliderMSW::Create(wxWindow
*parent
, wxWindowID id
,
50 int value
, int minValue
, int maxValue
,
52 const wxSize
& size
, long style
,
53 const wxValidator
& validator
,
57 SetValidator(validator
);
59 if (parent
) parent
->AddChild(this);
60 SetBackgroundColour(parent
->GetBackgroundColour()) ;
61 SetForegroundColour(parent
->GetForegroundColour()) ;
68 m_windowStyle
= style
;
72 m_windowId
= (int)NewControlId();
81 // non-Win95 implementation
83 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
86 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
88 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, "STATIC", NULL
,
90 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
91 wxGetInstance(), NULL
);
93 // Now create min static control
94 sprintf(wxBuffer
, "%d", minValue
);
95 m_staticMin
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
97 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
98 wxGetInstance(), NULL
);
101 m_windowId
= (int)NewControlId();
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(MakeExtendedStyle(m_windowStyle
), "SCROLLBAR", wxBuffer
,
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 sprintf(wxBuffer
, "%d", maxValue
);
129 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
131 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
132 wxGetInstance(), NULL
);
134 SetFont(parent
->GetFont());
138 // GetFont()->RealizeResource();
139 if (GetFont().GetResourceHandle())
142 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
143 (WPARAM
)GetFont().GetResourceHandle(),0L);
145 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
146 (WPARAM
)GetFont().GetResourceHandle(),0L);
148 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
149 (WPARAM
)GetFont().GetResourceHandle(),0L);
153 SetSize(x
, y
, width
, height
);
159 void wxSliderMSW::MSWOnVScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
161 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
164 wxEventType scrollEvent
= wxEVT_NULL
;
168 nScrollInc
= m_rangeMax
- position
;
169 scrollEvent
= wxEVT_SCROLL_TOP
;
173 nScrollInc
= - position
;
174 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
178 nScrollInc
= - GetLineSize();
179 scrollEvent
= wxEVT_SCROLL_LINEUP
;
183 nScrollInc
= GetLineSize();
184 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
188 nScrollInc
= -GetPageSize();
189 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
193 nScrollInc
= GetPageSize();
194 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
198 case SB_THUMBPOSITION
:
200 nScrollInc
= (signed short)pos
- position
;
202 nScrollInc
= pos
- position
;
204 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
215 int newPos
= position
+ nScrollInc
;
217 if (!(newPos
< GetMin() || newPos
> GetMax()))
221 wxScrollEvent
event(scrollEvent
, m_windowId
);
222 event
.SetPosition(newPos
);
223 event
.SetEventObject( this );
224 GetEventHandler()->ProcessEvent(event
);
226 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
227 cevent
.SetEventObject( this );
228 GetEventHandler()->ProcessEvent( cevent
);
233 void wxSliderMSW::MSWOnHScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
235 MSWOnVScroll(wParam
, pos
, control
);
238 wxSliderMSW::~wxSliderMSW(void)
241 DestroyWindow((HWND
) m_staticMin
);
243 DestroyWindow((HWND
) m_staticMax
);
245 DestroyWindow((HWND
) m_staticValue
);
248 int wxSliderMSW::GetValue(void) const
250 return ::GetScrollPos((HWND
) GetHWND(), SB_CTL
);
253 void wxSliderMSW::SetValue(int value
)
255 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, value
, TRUE
);
258 sprintf(wxBuffer
, "%d", value
);
259 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
263 void wxSliderMSW::GetSize(int *width
, int *height
) const
266 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
268 wxFindMaxSize(GetHWND(), &rect
);
271 wxFindMaxSize(m_staticMin
, &rect
);
273 wxFindMaxSize(m_staticMax
, &rect
);
275 wxFindMaxSize(m_staticValue
, &rect
);
277 *width
= rect
.right
- rect
.left
;
278 *height
= rect
.bottom
- rect
.top
;
281 void wxSliderMSW::GetPosition(int *x
, int *y
) const
283 wxWindow
*parent
= GetParent();
285 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
287 wxFindMaxSize(GetHWND(), &rect
);
290 wxFindMaxSize(m_staticMin
, &rect
);
292 wxFindMaxSize(m_staticMax
, &rect
);
294 wxFindMaxSize(m_staticValue
, &rect
);
296 // Since we now have the absolute screen coords,
297 // if there's a parent we must subtract its top left corner
302 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
304 // We may be faking the client origin.
305 // So a window that's really at (0, 30) may appear
306 // (to wxWin apps) to be at (0, 0).
309 wxPoint
pt(GetParent()->GetClientAreaOrigin());
317 void wxSliderMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
324 int currentX
, currentY
;
325 GetPosition(¤tX
, ¤tY
);
326 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
328 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
331 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
338 int cx
; // slider,min,max sizes
342 wxGetCharSize(GetHWND(), &cx
, &cy
,& this->GetFont());
344 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
346 if ( m_windowStyle
& wxSL_LABELS
)
350 GetWindowText((HWND
) m_staticMin
, buf
, 300);
351 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
355 GetWindowText((HWND
) m_staticMax
, buf
, 300);
356 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
359 int new_width
= (int)(wxMax(min_len
, max_len
));
360 int valueHeight
= (int)cyf
;
362 // For some reason, under Win95, the text edit control has
363 // a lot of space before the first character
366 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
367 x_offset
+= new_width
+ cx
;
370 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
371 x_offset
+= (int)(min_len
+ cx
);
373 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
375 int slider_height
= cy
;
377 // Slider must have a minimum/default length/height
378 if (slider_length
< 100)
381 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
382 x_offset
+= slider_length
+ cx
;
384 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
393 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
398 if ( m_windowStyle
& wxSL_LABELS
)
401 GetWindowText((HWND
) m_staticMin
, buf
, 300);
402 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,& this->GetFont());
405 GetWindowText((HWND
) m_staticMax
, buf
, 300);
406 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
410 int new_width
= (int)(wxMax(min_len
, max_len
));
411 int valueHeight
= (int)cyf
;
412 /*** Suggested change by George Tasker - remove this block...
414 // For some reason, under Win95, the text edit control has
415 // a lot of space before the first character
418 ... and replace with following line: */
421 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
422 y_offset
+= valueHeight
;
425 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
428 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
430 // Use character height as an estimate of slider width (yes, width)
431 int slider_width
= cy
;
433 // Slider must have a minimum/default length
434 if (slider_length
< 100)
437 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
438 y_offset
+= slider_length
;
440 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
449 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
454 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
456 m_rangeMin
= minValue
;
457 m_rangeMax
= maxValue
;
459 ::SetScrollRange((HWND
) GetHWND(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
463 sprintf(buf
, "%d", m_rangeMin
);
464 SetWindowText((HWND
) m_staticMin
, buf
);
469 sprintf(buf
, "%d", m_rangeMax
);
470 SetWindowText((HWND
) m_staticMax
, buf
);
474 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
475 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
477 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
480 // Otherwise, it's a static
481 if (GetParent()->GetTransparentBackground())
482 SetBkMode((HDC
) pDC
, TRANSPARENT
);
484 SetBkMode((HDC
) pDC
, OPAQUE
);
486 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
487 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
489 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
491 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
494 // For trackbars only
495 void wxSliderMSW::SetTickFreq(int n
, int pos
)
499 void wxSliderMSW::SetPageSize(int pageSize
)
501 m_pageSize
= pageSize
;
504 int wxSliderMSW::GetPageSize(void) const
509 void wxSliderMSW::ClearSel(void)
513 void wxSliderMSW::ClearTicks(void)
517 void wxSliderMSW::SetLineSize(int lineSize
)
519 m_lineSize
= lineSize
;
522 int wxSliderMSW::GetLineSize(void) const
527 int wxSliderMSW::GetSelEnd(void) const
532 int wxSliderMSW::GetSelStart(void) const
537 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
541 void wxSliderMSW::SetThumbLength(int len
)
545 int wxSliderMSW::GetThumbLength(void) const
550 void wxSliderMSW::SetTick(int tickPos
)
554 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
556 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
559 void wxSliderMSW::Command (wxCommandEvent
& event
)
561 SetValue (event
.GetInt());
562 ProcessCommand (event
);
565 bool wxSliderMSW::Show(bool show
)
567 wxWindow::Show(show
);
576 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
578 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
580 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);