1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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
113 minLabel
.Printf(wxT("%d"), minValue
);
114 wstyle
= STATIC_FLAGS
;
115 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
116 msStyle
|= WS_CLIPSIBLINGS
;
117 m_staticMin
= (WXHWND
) CreateWindowEx
119 0, wxT("STATIC"), minLabel
,
121 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
122 wxGetInstance(), NULL
128 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
129 msStyle
|= WS_CLIPSIBLINGS
;
131 if (m_windowStyle
& wxSL_VERTICAL
)
132 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
134 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
136 if ( m_windowStyle
& wxSL_AUTOTICKS
)
137 msStyle
|= TBS_AUTOTICKS
;
139 if ( m_windowStyle
& wxSL_LEFT
)
141 else if ( m_windowStyle
& wxSL_RIGHT
)
142 msStyle
|= TBS_RIGHT
;
143 else if ( m_windowStyle
& wxSL_TOP
)
145 else if ( m_windowStyle
& wxSL_BOTTOM
)
146 msStyle
|= TBS_BOTTOM
;
147 else if ( m_windowStyle
& wxSL_BOTH
)
149 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
150 msStyle
|= TBS_NOTICKS
;
152 if ( m_windowStyle
& wxSL_SELRANGE
)
153 msStyle
|= TBS_ENABLESELRANGE
;
155 HWND scroll_bar
= CreateWindowEx
157 MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxT(""),
159 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
160 wxGetInstance(), NULL
163 m_rangeMax
= maxValue
;
164 m_rangeMin
= minValue
;
166 m_pageSize
= (int)((maxValue
-minValue
)/10);
168 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
169 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
170 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
172 m_hWnd
= (WXHWND
)scroll_bar
;
174 SubclassWin(GetHWND());
176 ::SetWindowText((HWND
) m_hWnd
, wxT(""));
178 SetFont(parent
->GetFont());
180 if ( m_windowStyle
& wxSL_LABELS
)
182 // Finally, create max value static item
184 maxLabel
.Printf(wxT("%d"), maxValue
);
185 wstyle
= STATIC_FLAGS
;
187 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
188 msStyle
|= WS_CLIPSIBLINGS
;
190 m_staticMax
= (WXHWND
) CreateWindowEx
192 0, wxT("STATIC"), maxLabel
,
194 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
195 wxGetInstance(), NULL
201 if (GetFont().GetResourceHandle())
204 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
205 (WPARAM
) GetFont().GetResourceHandle(), 0L);
208 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
209 (WPARAM
) GetFont().GetResourceHandle(), 0L);
212 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
213 (WPARAM
) GetFont().GetResourceHandle(), 0L);
218 SetSize(x
, y
, width
, height
);
224 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
225 WXWORD
WXUNUSED(pos
), WXHWND control
)
227 wxEventType scrollEvent
;
231 scrollEvent
= wxEVT_SCROLL_TOP
;
235 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
239 scrollEvent
= wxEVT_SCROLL_LINEUP
;
243 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
247 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
251 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
255 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
258 case SB_THUMBPOSITION
:
259 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
263 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
267 // unknown scroll event?
271 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
272 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
274 // out of range - but we did process it
280 wxScrollEvent
event(scrollEvent
, m_windowId
);
281 event
.SetPosition(newPos
);
282 event
.SetEventObject( this );
283 GetEventHandler()->ProcessEvent(event
);
285 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
286 cevent
.SetInt( newPos
);
287 cevent
.SetEventObject( this );
289 return GetEventHandler()->ProcessEvent( cevent
);
292 wxSlider95::~wxSlider95()
296 ::DestroyWindow((HWND
) m_staticMin
);
297 m_staticMin
= (WXHWND
) NULL
;
302 ::DestroyWindow((HWND
) m_staticMax
);
303 m_staticMax
= (WXHWND
) NULL
;
308 ::DestroyWindow((HWND
) m_staticValue
);
309 m_staticValue
= (WXHWND
) NULL
;
313 int wxSlider95::GetValue() const
315 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
318 void wxSlider95::SetValue(int value
)
320 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
325 str
.Printf(wxT("%d"), value
);
326 ::SetWindowText((HWND
) m_staticValue
, str
);
330 void wxSlider95::DoGetSize(int *width
, int *height
) const
332 GetSize(width
, height
);
335 void wxSlider95::GetSize(int *width
, int *height
) const
338 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
340 wxFindMaxSize(GetHWND(), &rect
);
343 wxFindMaxSize(m_staticMin
, &rect
);
346 wxFindMaxSize(m_staticMax
, &rect
);
349 wxFindMaxSize(m_staticValue
, &rect
);
351 *width
= rect
.right
- rect
.left
;
352 *height
= rect
.bottom
- rect
.top
;
355 void wxSlider95::GetPosition(int *x
, int *y
) const
357 wxWindow
*parent
= GetParent();
359 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
361 wxFindMaxSize(GetHWND(), &rect
);
364 wxFindMaxSize(m_staticMin
, &rect
);
366 wxFindMaxSize(m_staticMax
, &rect
);
368 wxFindMaxSize(m_staticValue
, &rect
);
370 // Since we now have the absolute screen coords,
371 // if there's a parent we must subtract its top left corner
376 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
378 // We may be faking the client origin.
379 // So a window that's really at (0, 30) may appear
380 // (to wxWin apps) to be at (0, 0).
383 wxPoint
pt(GetParent()->GetClientAreaOrigin());
392 // TODO one day, make sense of all this horros and replace it with a readable
394 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
401 int currentX
, currentY
;
402 GetPosition(¤tX
, ¤tY
);
403 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
405 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
408 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
415 int cx
; // slider,min,max sizes
419 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
421 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
423 if ( m_windowStyle
& wxSL_LABELS
)
427 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
428 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
432 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
433 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
436 int new_width
= (int)(wxMax(min_len
, max_len
));
437 int valueHeight
= (int)cyf
;
439 // For some reason, under Win95, the text edit control has
440 // a lot of space before the first character
443 // The height needs to be a bit bigger under Win95 if
444 // using native 3D effects.
445 valueHeight
= (int) (valueHeight
* 1.5) ;
446 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
447 new_width
, valueHeight
, TRUE
);
448 x_offset
+= new_width
+ cx
;
451 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
452 (int) min_len
, cy
, TRUE
);
453 x_offset
+= (int)(min_len
+ cx
);
455 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
457 int slider_height
= h1
;
458 if (slider_height
< 0 )
461 // Slider must have a minimum/default length/height
462 if (slider_length
< 100)
465 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
466 slider_length
, slider_height
, TRUE
);
467 x_offset
+= slider_length
+ cx
;
469 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
470 (int) max_len
, cy
, TRUE
);
475 // If we're prepared to use the existing size, then...
480 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
491 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
496 if ( m_windowStyle
& wxSL_LABELS
)
499 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
500 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
503 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
504 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
508 int new_width
= (int)(wxMax(min_len
, max_len
));
509 int valueHeight
= (int)cyf
;
510 /*** Suggested change by George Tasker - remove this block...
512 // For some reason, under Win95, the text edit control has
513 // a lot of space before the first character
516 ... and replace with following line: */
519 // The height needs to be a bit bigger under Win95 if
520 // using native 3D effects.
521 valueHeight
= (int) (valueHeight
* 1.5) ;
523 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
524 new_width
, valueHeight
, TRUE
);
525 y_offset
+= valueHeight
;
528 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
529 (int) min_len
, cy
, TRUE
);
532 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
534 int slider_width
= w1
;
535 if (slider_width
< 0 )
538 // Slider must have a minimum/default length
539 if (slider_length
< 100)
542 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
543 slider_width
, slider_length
, TRUE
);
544 y_offset
+= slider_length
;
546 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
547 (int)max_len
, cy
, TRUE
);
552 // If we're prepared to use the existing size, then...
555 width
== -1 && height
== -1
556 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
567 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
572 void wxSlider95::SetRange(int minValue
, int maxValue
)
574 m_rangeMin
= minValue
;
575 m_rangeMax
= maxValue
;
577 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
582 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
583 ::SetWindowText((HWND
) m_staticMin
, buf
);
588 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
589 ::SetWindowText((HWND
) m_staticMax
, buf
);
593 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
594 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
596 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
599 // Otherwise, it's a static
600 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
603 // For trackbars only
604 void wxSlider95::SetTickFreq(int n
, int pos
)
607 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
610 void wxSlider95::SetPageSize(int pageSize
)
612 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
613 m_pageSize
= pageSize
;
616 int wxSlider95::GetPageSize() const
621 void wxSlider95::ClearSel()
623 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
626 void wxSlider95::ClearTicks()
628 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
631 void wxSlider95::SetLineSize(int lineSize
)
633 m_lineSize
= lineSize
;
634 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
637 int wxSlider95::GetLineSize() const
639 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
640 (WPARAM
) 0, (LPARAM
) 0 );
643 int wxSlider95::GetSelEnd() const
645 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
646 (WPARAM
) 0, (LPARAM
) 0 );
649 int wxSlider95::GetSelStart() const
651 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
652 (WPARAM
) 0, (LPARAM
) 0 );
655 void wxSlider95::SetSelection(int minPos
, int maxPos
)
657 ::SendMessage(GetHwnd(), TBM_SETSEL
,
658 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
661 void wxSlider95::SetThumbLength(int len
)
663 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
666 int wxSlider95::GetThumbLength() const
668 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
669 (WPARAM
) 0, (LPARAM
) 0 );
672 void wxSlider95::SetTick(int tickPos
)
674 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
677 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
681 hWnd
== GetStaticMin()
682 || hWnd
== GetStaticMax()
683 || hWnd
== GetEditValue()
687 void wxSlider95::Command (wxCommandEvent
& event
)
689 SetValue (event
.GetInt());
690 ProcessCommand (event
);
693 bool wxSlider95::Show(bool show
)
695 wxWindow::Show(show
);
704 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
707 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
710 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
718 #endif // wxUSE_SLIDER