1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider95.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 #include "wx/msw/slider95.h"
32 #include "wx/msw/private.h"
34 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
38 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
41 wxSlider95::wxSlider95()
53 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
54 int value
, int minValue
, int maxValue
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
62 SetValidator(validator
);
63 #endif // wxUSE_VALIDATORS
65 if (parent
) parent
->AddChild(this);
66 SetBackgroundColour(parent
->GetBackgroundColour()) ;
67 SetForegroundColour(parent
->GetForegroundColour()) ;
74 m_windowStyle
= style
;
78 m_windowId
= (int)NewControlId();
90 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
91 msStyle
|= WS_CLIPSIBLINGS
;
93 if ( m_windowStyle
& wxSL_LABELS
)
95 msStyle
|= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
98 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
100 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
102 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
103 wxGetInstance(), NULL
);
105 // Now create min static control
106 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
107 wstyle
= STATIC_FLAGS
;
108 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
109 msStyle
|= WS_CLIPSIBLINGS
;
110 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
112 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
113 wxGetInstance(), NULL
);
118 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
119 msStyle
|= WS_CLIPSIBLINGS
;
121 if (m_windowStyle
& wxSL_VERTICAL
)
122 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
124 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
126 if ( m_windowStyle
& wxSL_AUTOTICKS
)
127 msStyle
|= TBS_AUTOTICKS
;
129 if ( m_windowStyle
& wxSL_LEFT
)
131 else if ( m_windowStyle
& wxSL_RIGHT
)
132 msStyle
|= TBS_RIGHT
;
133 else if ( m_windowStyle
& wxSL_TOP
)
135 else if ( m_windowStyle
& wxSL_BOTTOM
)
136 msStyle
|= TBS_BOTTOM
;
137 else if ( m_windowStyle
& wxSL_BOTH
)
139 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
140 msStyle
|= TBS_NOTICKS
;
142 if ( m_windowStyle
& wxSL_SELRANGE
)
143 msStyle
|= TBS_ENABLESELRANGE
;
145 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
147 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
148 wxGetInstance(), NULL
);
150 m_rangeMax
= maxValue
;
151 m_rangeMin
= minValue
;
153 m_pageSize
= (int)((maxValue
-minValue
)/10);
155 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
156 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
157 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
159 m_hWnd
= (WXHWND
)scroll_bar
;
161 SubclassWin(GetHWND());
163 SetWindowText((HWND
) m_hWnd
, wxT(""));
165 SetFont(parent
->GetFont());
167 if ( m_windowStyle
& wxSL_LABELS
)
169 // Finally, create max value static item
170 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
171 wstyle
= STATIC_FLAGS
;
172 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
173 msStyle
|= WS_CLIPSIBLINGS
;
174 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
176 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
177 wxGetInstance(), NULL
);
182 if (GetFont().GetResourceHandle())
185 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
186 (WPARAM
)GetFont().GetResourceHandle(),0L);
188 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
189 (WPARAM
)GetFont().GetResourceHandle(),0L);
191 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
192 (WPARAM
)GetFont().GetResourceHandle(),0L);
197 SetSize(x
, y
, width
, height
);
203 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
204 WXWORD pos
, WXHWND control
)
206 int position
= 0; // Dummy - not used in this mode
209 wxEventType scrollEvent
= wxEVT_NULL
;
213 nScrollInc
= m_rangeMax
- position
;
214 scrollEvent
= wxEVT_SCROLL_TOP
;
218 nScrollInc
= - position
;
219 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
223 nScrollInc
= - GetLineSize();
224 scrollEvent
= wxEVT_SCROLL_LINEUP
;
228 nScrollInc
= GetLineSize();
229 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
233 nScrollInc
= -GetPageSize();
234 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
238 nScrollInc
= GetPageSize();
239 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
243 case SB_THUMBPOSITION
:
245 nScrollInc
= (signed short)pos
- position
;
247 nScrollInc
= pos
- position
;
249 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
256 if (scrollEvent
== wxEVT_NULL
)
262 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
263 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
265 // out of range - but we did process it
271 wxScrollEvent
event(scrollEvent
, m_windowId
);
272 event
.SetPosition(newPos
);
273 event
.SetEventObject( this );
274 GetEventHandler()->ProcessEvent(event
);
276 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
277 cevent
.SetInt( newPos
);
278 cevent
.SetEventObject( this );
280 return GetEventHandler()->ProcessEvent( cevent
);
283 wxSlider95::~wxSlider95()
286 DestroyWindow((HWND
) m_staticMin
);
288 DestroyWindow((HWND
) m_staticMax
);
290 DestroyWindow((HWND
) m_staticValue
);
293 int wxSlider95::GetValue() const
295 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
298 void wxSlider95::SetValue(int value
)
300 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
303 wxSprintf(wxBuffer
, wxT("%d"), value
);
304 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
308 void wxSlider95::GetSize(int *width
, int *height
) const
311 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
313 wxFindMaxSize(GetHWND(), &rect
);
316 wxFindMaxSize(m_staticMin
, &rect
);
318 wxFindMaxSize(m_staticMax
, &rect
);
320 wxFindMaxSize(m_staticValue
, &rect
);
322 *width
= rect
.right
- rect
.left
;
323 *height
= rect
.bottom
- rect
.top
;
326 void wxSlider95::GetPosition(int *x
, int *y
) const
328 wxWindow
*parent
= GetParent();
330 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
332 wxFindMaxSize(GetHWND(), &rect
);
335 wxFindMaxSize(m_staticMin
, &rect
);
337 wxFindMaxSize(m_staticMax
, &rect
);
339 wxFindMaxSize(m_staticValue
, &rect
);
341 // Since we now have the absolute screen coords,
342 // if there's a parent we must subtract its top left corner
347 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
349 // We may be faking the client origin.
350 // So a window that's really at (0, 30) may appear
351 // (to wxWin apps) to be at (0, 0).
354 wxPoint
pt(GetParent()->GetClientAreaOrigin());
362 // TODO one day, make sense of all this horros and replace it with a readable
364 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
371 int currentX
, currentY
;
372 GetPosition(¤tX
, ¤tY
);
373 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
375 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
378 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
385 int cx
; // slider,min,max sizes
389 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
391 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
393 if ( m_windowStyle
& wxSL_LABELS
)
397 GetWindowText((HWND
) m_staticMin
, buf
, 300);
398 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
402 GetWindowText((HWND
) m_staticMax
, buf
, 300);
403 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
406 int new_width
= (int)(wxMax(min_len
, max_len
));
407 int valueHeight
= (int)cyf
;
409 // For some reason, under Win95, the text edit control has
410 // a lot of space before the first character
413 // The height needs to be a bit bigger under Win95 if using native
415 valueHeight
= (int) (valueHeight
* 1.5) ;
416 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
417 x_offset
+= new_width
+ cx
;
420 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
421 x_offset
+= (int)(min_len
+ cx
);
423 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
425 int slider_height
= h1
;
426 if (slider_height
< 0 )
429 // Slider must have a minimum/default length/height
430 if (slider_length
< 100)
433 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
434 x_offset
+= slider_length
+ cx
;
436 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
441 // If we're prepared to use the existing size, then...
442 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
450 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
455 if ( m_windowStyle
& wxSL_LABELS
)
458 GetWindowText((HWND
) m_staticMin
, buf
, 300);
459 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
462 GetWindowText((HWND
) m_staticMax
, buf
, 300);
463 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
467 int new_width
= (int)(wxMax(min_len
, max_len
));
468 int valueHeight
= (int)cyf
;
469 /*** Suggested change by George Tasker - remove this block...
471 // For some reason, under Win95, the text edit control has
472 // a lot of space before the first character
475 ... and replace with following line: */
478 // The height needs to be a bit bigger under Win95 if using native
480 valueHeight
= (int) (valueHeight
* 1.5) ;
482 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
483 y_offset
+= valueHeight
;
486 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
489 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
491 int slider_width
= w1
;
492 if (slider_width
< 0 )
495 // Slider must have a minimum/default length
496 if (slider_length
< 100)
499 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
500 y_offset
+= slider_length
;
502 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
507 // If we're prepared to use the existing size, then...
508 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
516 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
521 void wxSlider95::SetRange(int minValue
, int maxValue
)
523 m_rangeMin
= minValue
;
524 m_rangeMax
= maxValue
;
526 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
531 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
532 SetWindowText((HWND
) m_staticMin
, buf
);
537 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
538 SetWindowText((HWND
) m_staticMax
, buf
);
542 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
543 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
545 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
548 // Otherwise, it's a static
549 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
552 // For trackbars only
553 void wxSlider95::SetTickFreq(int n
, int pos
)
556 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
559 void wxSlider95::SetPageSize(int pageSize
)
561 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
562 m_pageSize
= pageSize
;
565 int wxSlider95::GetPageSize() const
570 void wxSlider95::ClearSel()
572 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
575 void wxSlider95::ClearTicks()
577 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
580 void wxSlider95::SetLineSize(int lineSize
)
582 m_lineSize
= lineSize
;
583 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
586 int wxSlider95::GetLineSize() const
588 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
591 int wxSlider95::GetSelEnd() const
593 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
596 int wxSlider95::GetSelStart() const
598 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
601 void wxSlider95::SetSelection(int minPos
, int maxPos
)
603 ::SendMessage( GetHwnd(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
606 void wxSlider95::SetThumbLength(int len
)
608 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
611 int wxSlider95::GetThumbLength() const
613 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
616 void wxSlider95::SetTick(int tickPos
)
618 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
621 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
623 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
626 void wxSlider95::Command (wxCommandEvent
& event
)
628 SetValue (event
.GetInt());
629 ProcessCommand (event
);
632 bool wxSlider95::Show(bool show
)
634 wxWindow::Show(show
);
643 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
645 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
647 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);