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"
28 #include "wx/slider.h"
33 #include "wx/msw/slider95.h"
34 #include "wx/msw/private.h"
36 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
40 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
43 wxSlider95::wxSlider95()
45 m_staticValue
= (WXHWND
) NULL
;
46 m_staticMin
= (WXHWND
) NULL
;
47 m_staticMax
= (WXHWND
) NULL
;
55 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
56 int value
, int minValue
, int maxValue
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
64 SetValidator(validator
);
65 #endif // wxUSE_VALIDATORS
67 if (parent
) parent
->AddChild(this);
69 SetBackgroundColour(parent
->GetBackgroundColour()) ;
70 SetForegroundColour(parent
->GetForegroundColour()) ;
72 m_staticValue
= (WXHWND
) NULL
;;
73 m_staticMin
= (WXHWND
) NULL
;;
74 m_staticMax
= (WXHWND
) NULL
;;
77 m_windowStyle
= style
;
81 m_windowId
= (int)NewControlId();
93 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
94 msStyle
|= WS_CLIPSIBLINGS
;
96 if ( m_windowStyle
& wxSL_LABELS
)
98 msStyle
|= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
101 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
103 m_staticValue
= (WXHWND
) CreateWindowEx
105 exStyle
, wxT("STATIC"), NULL
,
107 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
108 wxGetInstance(), NULL
111 // Now create min static control
112 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
113 wstyle
= STATIC_FLAGS
;
114 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
115 msStyle
|= WS_CLIPSIBLINGS
;
116 m_staticMin
= (WXHWND
) CreateWindowEx
118 0, wxT("STATIC"), wxBuffer
,
120 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
121 wxGetInstance(), NULL
127 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
128 msStyle
|= WS_CLIPSIBLINGS
;
130 if (m_windowStyle
& wxSL_VERTICAL
)
131 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
133 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
135 if ( m_windowStyle
& wxSL_AUTOTICKS
)
136 msStyle
|= TBS_AUTOTICKS
;
138 if ( m_windowStyle
& wxSL_LEFT
)
140 else if ( m_windowStyle
& wxSL_RIGHT
)
141 msStyle
|= TBS_RIGHT
;
142 else if ( m_windowStyle
& wxSL_TOP
)
144 else if ( m_windowStyle
& wxSL_BOTTOM
)
145 msStyle
|= TBS_BOTTOM
;
146 else if ( m_windowStyle
& wxSL_BOTH
)
148 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
149 msStyle
|= TBS_NOTICKS
;
151 if ( m_windowStyle
& wxSL_SELRANGE
)
152 msStyle
|= TBS_ENABLESELRANGE
;
154 HWND scroll_bar
= CreateWindowEx
156 MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
158 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
159 wxGetInstance(), NULL
162 m_rangeMax
= maxValue
;
163 m_rangeMin
= minValue
;
165 m_pageSize
= (int)((maxValue
-minValue
)/10);
167 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
168 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
169 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
171 m_hWnd
= (WXHWND
)scroll_bar
;
173 SubclassWin(GetHWND());
175 ::SetWindowText((HWND
) m_hWnd
, wxT(""));
177 SetFont(parent
->GetFont());
179 if ( m_windowStyle
& wxSL_LABELS
)
181 // Finally, create max value static item
182 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
183 wstyle
= STATIC_FLAGS
;
185 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
186 msStyle
|= WS_CLIPSIBLINGS
;
188 m_staticMax
= (WXHWND
) CreateWindowEx
190 0, wxT("STATIC"), wxBuffer
,
192 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
193 wxGetInstance(), NULL
199 if (GetFont().GetResourceHandle())
202 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
203 (WPARAM
) GetFont().GetResourceHandle(), 0L);
206 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
207 (WPARAM
) GetFont().GetResourceHandle(), 0L);
210 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
211 (WPARAM
) GetFont().GetResourceHandle(), 0L);
216 SetSize(x
, y
, width
, height
);
222 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
223 WXWORD
WXUNUSED(pos
), WXHWND control
)
225 wxEventType scrollEvent
;
229 scrollEvent
= wxEVT_SCROLL_TOP
;
233 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
237 scrollEvent
= wxEVT_SCROLL_LINEUP
;
241 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
245 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
249 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
253 case SB_THUMBPOSITION
:
254 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
258 // unknown scroll event?
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()
287 ::DestroyWindow((HWND
) m_staticMin
);
288 m_staticMin
= (WXHWND
) NULL
;
293 ::DestroyWindow((HWND
) m_staticMax
);
294 m_staticMax
= (WXHWND
) NULL
;
299 ::DestroyWindow((HWND
) m_staticValue
);
300 m_staticValue
= (WXHWND
) NULL
;
304 int wxSlider95::GetValue() const
306 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
309 void wxSlider95::SetValue(int value
)
311 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
315 wxSprintf(wxBuffer
, wxT("%d"), value
);
316 ::SetWindowText((HWND
) m_staticValue
, wxBuffer
);
320 void wxSlider95::DoGetSize(int *width
, int *height
) const
322 GetSize(width
, height
);
325 void wxSlider95::GetSize(int *width
, int *height
) const
328 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
330 wxFindMaxSize(GetHWND(), &rect
);
333 wxFindMaxSize(m_staticMin
, &rect
);
336 wxFindMaxSize(m_staticMax
, &rect
);
339 wxFindMaxSize(m_staticValue
, &rect
);
341 *width
= rect
.right
- rect
.left
;
342 *height
= rect
.bottom
- rect
.top
;
345 void wxSlider95::GetPosition(int *x
, int *y
) const
347 wxWindow
*parent
= GetParent();
349 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
351 wxFindMaxSize(GetHWND(), &rect
);
354 wxFindMaxSize(m_staticMin
, &rect
);
356 wxFindMaxSize(m_staticMax
, &rect
);
358 wxFindMaxSize(m_staticValue
, &rect
);
360 // Since we now have the absolute screen coords,
361 // if there's a parent we must subtract its top left corner
366 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
368 // We may be faking the client origin.
369 // So a window that's really at (0, 30) may appear
370 // (to wxWin apps) to be at (0, 0).
373 wxPoint
pt(GetParent()->GetClientAreaOrigin());
382 // TODO one day, make sense of all this horros and replace it with a readable
384 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
391 int currentX
, currentY
;
392 GetPosition(¤tX
, ¤tY
);
393 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
395 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
398 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
405 int cx
; // slider,min,max sizes
409 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
411 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
413 if ( m_windowStyle
& wxSL_LABELS
)
417 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
418 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
422 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
423 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
426 int new_width
= (int)(wxMax(min_len
, max_len
));
427 int valueHeight
= (int)cyf
;
429 // For some reason, under Win95, the text edit control has
430 // a lot of space before the first character
433 // The height needs to be a bit bigger under Win95 if
434 // using native 3D effects.
435 valueHeight
= (int) (valueHeight
* 1.5) ;
436 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
437 new_width
, valueHeight
, TRUE
);
438 x_offset
+= new_width
+ cx
;
441 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
442 (int) min_len
, cy
, TRUE
);
443 x_offset
+= (int)(min_len
+ cx
);
445 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
447 int slider_height
= h1
;
448 if (slider_height
< 0 )
451 // Slider must have a minimum/default length/height
452 if (slider_length
< 100)
455 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
456 slider_length
, slider_height
, TRUE
);
457 x_offset
+= slider_length
+ cx
;
459 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
460 (int) max_len
, cy
, TRUE
);
465 // If we're prepared to use the existing size, then...
470 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
481 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
486 if ( m_windowStyle
& wxSL_LABELS
)
489 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
490 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
493 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
494 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
498 int new_width
= (int)(wxMax(min_len
, max_len
));
499 int valueHeight
= (int)cyf
;
500 /*** Suggested change by George Tasker - remove this block...
502 // For some reason, under Win95, the text edit control has
503 // a lot of space before the first character
506 ... and replace with following line: */
509 // The height needs to be a bit bigger under Win95 if
510 // using native 3D effects.
511 valueHeight
= (int) (valueHeight
* 1.5) ;
513 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
514 new_width
, valueHeight
, TRUE
);
515 y_offset
+= valueHeight
;
518 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
519 (int) min_len
, cy
, TRUE
);
522 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
524 int slider_width
= w1
;
525 if (slider_width
< 0 )
528 // Slider must have a minimum/default length
529 if (slider_length
< 100)
532 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
533 slider_width
, slider_length
, TRUE
);
534 y_offset
+= slider_length
;
536 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
537 (int)max_len
, cy
, TRUE
);
542 // If we're prepared to use the existing size, then...
545 width
== -1 && height
== -1
546 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
557 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
562 void wxSlider95::SetRange(int minValue
, int maxValue
)
564 m_rangeMin
= minValue
;
565 m_rangeMax
= maxValue
;
567 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
572 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
573 ::SetWindowText((HWND
) m_staticMin
, buf
);
578 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
579 ::SetWindowText((HWND
) m_staticMax
, buf
);
583 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
584 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
586 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
589 // Otherwise, it's a static
590 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
593 // For trackbars only
594 void wxSlider95::SetTickFreq(int n
, int pos
)
597 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
600 void wxSlider95::SetPageSize(int pageSize
)
602 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
603 m_pageSize
= pageSize
;
606 int wxSlider95::GetPageSize() const
611 void wxSlider95::ClearSel()
613 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
616 void wxSlider95::ClearTicks()
618 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
621 void wxSlider95::SetLineSize(int lineSize
)
623 m_lineSize
= lineSize
;
624 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
627 int wxSlider95::GetLineSize() const
629 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
630 (WPARAM
) 0, (LPARAM
) 0 );
633 int wxSlider95::GetSelEnd() const
635 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
636 (WPARAM
) 0, (LPARAM
) 0 );
639 int wxSlider95::GetSelStart() const
641 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
642 (WPARAM
) 0, (LPARAM
) 0 );
645 void wxSlider95::SetSelection(int minPos
, int maxPos
)
647 ::SendMessage(GetHwnd(), TBM_SETSEL
,
648 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
651 void wxSlider95::SetThumbLength(int len
)
653 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
656 int wxSlider95::GetThumbLength() const
658 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
659 (WPARAM
) 0, (LPARAM
) 0 );
662 void wxSlider95::SetTick(int tickPos
)
664 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
667 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
671 hWnd
== GetStaticMin()
672 || hWnd
== GetStaticMax()
673 || hWnd
== GetEditValue()
677 void wxSlider95::Command (wxCommandEvent
& event
)
679 SetValue (event
.GetInt());
680 ProcessCommand (event
);
683 bool wxSlider95::Show(bool show
)
685 wxWindow::Show(show
);
694 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
697 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
700 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
708 #endif // wxUSE_SLIDER