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
)
35 #if WXWIN_COMPATIBILITY
36 BEGIN_EVENT_TABLE(wxSliderMSW
, wxControl
)
37 EVT_SCROLL(wxSliderMSW::OnScroll
)
44 wxSliderMSW::wxSliderMSW(void)
56 bool wxSliderMSW::Create(wxWindow
*parent
, wxWindowID id
,
57 int value
, int minValue
, int maxValue
,
59 const wxSize
& size
, long style
,
60 const wxValidator
& validator
,
64 SetValidator(validator
);
66 if (parent
) parent
->AddChild(this);
67 SetBackgroundColour(parent
->GetBackgroundColour()) ;
68 SetForegroundColour(parent
->GetForegroundColour()) ;
75 m_windowStyle
= style
;
79 m_windowId
= (int)NewControlId();
88 // non-Win95 implementation
90 long msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
93 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
95 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, "STATIC", NULL
,
97 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
98 wxGetInstance(), NULL
);
100 // Now create min static control
101 sprintf(wxBuffer
, "%d", minValue
);
102 m_staticMin
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
104 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
105 wxGetInstance(), NULL
);
108 m_windowId
= (int)NewControlId();
111 if (m_windowStyle
& wxSL_VERTICAL
)
112 msStyle
= SBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
114 msStyle
= SBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
116 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "SCROLLBAR", wxBuffer
,
118 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
119 wxGetInstance(), NULL
);
121 m_pageSize
= (int)((maxValue
-minValue
)/10);
122 m_rangeMax
= maxValue
;
123 m_rangeMin
= minValue
;
125 ::SetScrollRange(scroll_bar
, SB_CTL
, minValue
, maxValue
, FALSE
);
126 ::SetScrollPos(scroll_bar
, SB_CTL
, value
, FALSE
);
127 ShowWindow(scroll_bar
, SW_SHOW
);
129 m_hWnd
= (WXHWND
)scroll_bar
;
131 // Subclass again for purposes of dialog editing mode
132 SubclassWin(GetHWND());
134 // Finally, create max value static item
135 sprintf(wxBuffer
, "%d", maxValue
);
136 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
138 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
139 wxGetInstance(), NULL
);
141 SetFont(parent
->GetFont());
145 // GetFont()->RealizeResource();
146 if (GetFont().GetResourceHandle())
149 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
150 (WPARAM
)GetFont().GetResourceHandle(),0L);
152 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
153 (WPARAM
)GetFont().GetResourceHandle(),0L);
155 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
156 (WPARAM
)GetFont().GetResourceHandle(),0L);
160 SetSize(x
, y
, width
, height
);
166 void wxSliderMSW::MSWOnVScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
168 int position
= ::GetScrollPos((HWND
)control
, SB_CTL
);
171 wxEventType scrollEvent
= wxEVT_NULL
;
175 nScrollInc
= m_rangeMax
- position
;
176 scrollEvent
= wxEVT_SCROLL_TOP
;
180 nScrollInc
= - position
;
181 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
185 nScrollInc
= - GetLineSize();
186 scrollEvent
= wxEVT_SCROLL_LINEUP
;
190 nScrollInc
= GetLineSize();
191 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
195 nScrollInc
= -GetPageSize();
196 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
200 nScrollInc
= GetPageSize();
201 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
205 case SB_THUMBPOSITION
:
207 nScrollInc
= (signed short)pos
- position
;
209 nScrollInc
= pos
- position
;
211 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
222 int newPos
= position
+ nScrollInc
;
224 if (!(newPos
< GetMin() || newPos
> GetMax()))
228 wxScrollEvent
event(scrollEvent
, m_windowId
);
229 event
.SetPosition(newPos
);
230 event
.SetEventObject( this );
231 GetEventHandler()->ProcessEvent(event
);
236 void wxSliderMSW::MSWOnHScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
238 MSWOnVScroll(wParam
, pos
, control
);
241 wxSliderMSW::~wxSliderMSW(void)
244 DestroyWindow((HWND
) m_staticMin
);
246 DestroyWindow((HWND
) m_staticMax
);
248 DestroyWindow((HWND
) m_staticValue
);
251 int wxSliderMSW::GetValue(void) const
253 return ::GetScrollPos((HWND
) GetHWND(), SB_CTL
);
256 void wxSliderMSW::SetValue(int value
)
258 ::SetScrollPos((HWND
) GetHWND(), SB_CTL
, value
, TRUE
);
261 sprintf(wxBuffer
, "%d", value
);
262 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
266 void wxSliderMSW::GetSize(int *width
, int *height
) const
269 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
271 wxFindMaxSize(GetHWND(), &rect
);
274 wxFindMaxSize(m_staticMin
, &rect
);
276 wxFindMaxSize(m_staticMax
, &rect
);
278 wxFindMaxSize(m_staticValue
, &rect
);
280 *width
= rect
.right
- rect
.left
;
281 *height
= rect
.bottom
- rect
.top
;
284 void wxSliderMSW::GetPosition(int *x
, int *y
) const
286 wxWindow
*parent
= GetParent();
288 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
290 wxFindMaxSize(GetHWND(), &rect
);
293 wxFindMaxSize(m_staticMin
, &rect
);
295 wxFindMaxSize(m_staticMax
, &rect
);
297 wxFindMaxSize(m_staticValue
, &rect
);
299 // Since we now have the absolute screen coords,
300 // if there's a parent we must subtract its top left corner
305 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
307 // We may be faking the client origin.
308 // So a window that's really at (0, 30) may appear
309 // (to wxWin apps) to be at (0, 0).
312 wxPoint
pt(GetParent()->GetClientAreaOrigin());
320 void wxSliderMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
327 int currentX
, currentY
;
328 GetPosition(¤tX
, ¤tY
);
329 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
331 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
334 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
341 int cx
; // slider,min,max sizes
345 wxGetCharSize(GetHWND(), &cx
, &cy
,& GetFont());
347 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
349 if ( m_windowStyle
& wxSL_LABELS
)
353 GetWindowText((HWND
) m_staticMin
, buf
, 300);
354 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & GetFont());
358 GetWindowText((HWND
) m_staticMax
, buf
, 300);
359 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & GetFont());
362 int new_width
= (int)(wxMax(min_len
, max_len
));
363 int valueHeight
= (int)cyf
;
365 // For some reason, under Win95, the text edit control has
366 // a lot of space before the first character
369 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
370 x_offset
+= new_width
+ cx
;
373 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
374 x_offset
+= (int)(min_len
+ cx
);
376 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
378 int slider_height
= cy
;
380 // Slider must have a minimum/default length/height
381 if (slider_length
< 100)
384 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
385 x_offset
+= slider_length
+ cx
;
387 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
396 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
401 if ( m_windowStyle
& wxSL_LABELS
)
404 GetWindowText((HWND
) m_staticMin
, buf
, 300);
405 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,& GetFont());
408 GetWindowText((HWND
) m_staticMax
, buf
, 300);
409 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & GetFont());
413 int new_width
= (int)(wxMax(min_len
, max_len
));
414 int valueHeight
= (int)cyf
;
415 /*** Suggested change by George Tasker - remove this block...
417 // For some reason, under Win95, the text edit control has
418 // a lot of space before the first character
421 ... and replace with following line: */
424 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
425 y_offset
+= valueHeight
;
428 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
431 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
433 // Use character height as an estimate of slider width (yes, width)
434 int slider_width
= cy
;
436 // Slider must have a minimum/default length
437 if (slider_length
< 100)
440 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
441 y_offset
+= slider_length
;
443 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
452 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
457 void wxSliderMSW::SetRange(int minValue
, int maxValue
)
459 m_rangeMin
= minValue
;
460 m_rangeMax
= maxValue
;
462 ::SetScrollRange((HWND
) GetHWND(), SB_CTL
, m_rangeMin
, m_rangeMax
, TRUE
);
466 sprintf(buf
, "%d", m_rangeMin
);
467 SetWindowText((HWND
) m_staticMin
, buf
);
472 sprintf(buf
, "%d", m_rangeMax
);
473 SetWindowText((HWND
) m_staticMax
, buf
);
477 WXHBRUSH
wxSliderMSW::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
478 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
480 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
483 // Otherwise, it's a static
484 if (GetParent()->GetTransparentBackground())
485 SetBkMode((HDC
) pDC
, TRANSPARENT
);
487 SetBkMode((HDC
) pDC
, OPAQUE
);
489 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
490 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
492 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
494 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
497 // For trackbars only
498 void wxSliderMSW::SetTickFreq(int n
, int pos
)
502 void wxSliderMSW::SetPageSize(int pageSize
)
504 m_pageSize
= pageSize
;
507 int wxSliderMSW::GetPageSize(void) const
512 void wxSliderMSW::ClearSel(void)
516 void wxSliderMSW::ClearTicks(void)
520 void wxSliderMSW::SetLineSize(int lineSize
)
522 m_lineSize
= lineSize
;
525 int wxSliderMSW::GetLineSize(void) const
530 int wxSliderMSW::GetSelEnd(void) const
535 int wxSliderMSW::GetSelStart(void) const
540 void wxSliderMSW::SetSelection(int minPos
, int maxPos
)
544 void wxSliderMSW::SetThumbLength(int len
)
548 int wxSliderMSW::GetThumbLength(void) const
553 void wxSliderMSW::SetTick(int tickPos
)
557 bool wxSliderMSW::ContainsHWND(WXHWND hWnd
) const
559 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
562 #if WXWIN_COMPATIBILITY
563 // Backward compatibility
564 void wxSliderMSW::OnScroll(wxScrollEvent
& event
)
566 wxEventType oldEvent
= event
.GetEventType();
567 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
568 if ( !GetEventHandler()->ProcessEvent(event
) )
570 event
.SetEventType( oldEvent
);
571 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
577 void wxSliderMSW::Command (wxCommandEvent
& event
)
579 SetValue (event
.GetInt());
580 ProcessCommand (event
);
583 bool wxSliderMSW::Show(bool show
)
585 wxWindow::Show(show
);
594 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
596 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
598 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);