1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(__CYGWIN10__))
40 #if wxUSE_EXTENDED_RTTI
41 WX_DEFINE_FLAGS( wxSliderStyle
)
43 WX_BEGIN_FLAGS( wxSliderStyle
)
44 // new style border flags, we put them first to
45 // use them for streaming out
46 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
47 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
48 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
49 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
50 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
51 WX_FLAGS_MEMBER(wxBORDER_NONE
)
53 // old style border flags
54 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
55 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
56 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
57 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
58 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
59 WX_FLAGS_MEMBER(wxNO_BORDER
)
61 // standard window styles
62 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
63 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
64 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
65 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
66 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
67 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
68 WX_FLAGS_MEMBER(wxVSCROLL
)
69 WX_FLAGS_MEMBER(wxHSCROLL
)
71 WX_FLAGS_MEMBER(wxSL_HORIZONTAL
)
72 WX_FLAGS_MEMBER(wxSL_VERTICAL
)
73 WX_FLAGS_MEMBER(wxSL_AUTOTICKS
)
74 WX_FLAGS_MEMBER(wxSL_LABELS
)
75 WX_FLAGS_MEMBER(wxSL_LEFT
)
76 WX_FLAGS_MEMBER(wxSL_TOP
)
77 WX_FLAGS_MEMBER(wxSL_RIGHT
)
78 WX_FLAGS_MEMBER(wxSL_BOTTOM
)
79 WX_FLAGS_MEMBER(wxSL_BOTH
)
80 WX_FLAGS_MEMBER(wxSL_SELRANGE
)
82 WX_END_FLAGS( wxSliderStyle
)
84 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider95
, wxControl
,"wx/scrolbar.h")
86 WX_BEGIN_PROPERTIES_TABLE(wxSlider95
)
87 WX_PROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
88 WX_PROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
89 WX_PROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
90 WX_PROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
91 WX_PROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
92 WX_PROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
93 WX_PROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
94 WX_END_PROPERTIES_TABLE()
96 WX_BEGIN_HANDLERS_TABLE(wxSlider95
)
97 WX_END_HANDLERS_TABLE()
99 WX_CONSTRUCTOR_8( wxSlider95
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
101 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
105 wxSlider95::wxSlider95()
107 m_staticValue
= (WXHWND
) NULL
;
108 m_staticMin
= (WXHWND
) NULL
;
109 m_staticMax
= (WXHWND
) NULL
;
117 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
118 int value
, int minValue
, int maxValue
,
120 const wxSize
& size
, long style
,
121 const wxValidator
& validator
,
122 const wxString
& name
)
124 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
125 style
|= wxBORDER_NONE
;
129 SetValidator(validator
);
130 #endif // wxUSE_VALIDATORS
132 if (parent
) parent
->AddChild(this);
134 SetBackgroundColour(parent
->GetBackgroundColour()) ;
135 SetForegroundColour(parent
->GetForegroundColour()) ;
137 m_staticValue
= (WXHWND
) NULL
;;
138 m_staticMin
= (WXHWND
) NULL
;;
139 m_staticMax
= (WXHWND
) NULL
;;
142 m_windowStyle
= style
;
146 m_windowId
= (int)NewControlId();
158 if ( m_windowStyle
& wxSL_LABELS
)
160 msStyle
|= SS_CENTER
;
163 long valueStyle
= m_windowStyle
& ~wxBORDER_MASK
;
164 valueStyle
|= wxBORDER_SUNKEN
;
165 msStyle
|= MSWGetStyle(valueStyle
, & exStyle
) ;
167 m_staticValue
= (WXHWND
) CreateWindowEx
169 exStyle
, wxT("STATIC"), NULL
,
171 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
172 wxGetInstance(), NULL
175 // Now create min static control
177 minLabel
.Printf(wxT("%d"), minValue
);
178 wstyle
= STATIC_FLAGS
;
179 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
180 msStyle
|= WS_CLIPSIBLINGS
;
181 m_staticMin
= (WXHWND
) CreateWindowEx
183 0, wxT("STATIC"), minLabel
,
185 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
186 wxGetInstance(), NULL
192 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
194 if (m_windowStyle
& wxSL_VERTICAL
)
195 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
197 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
199 if ( m_windowStyle
& wxSL_AUTOTICKS
)
200 msStyle
|= TBS_AUTOTICKS
;
202 if ( m_windowStyle
& wxSL_LEFT
)
204 else if ( m_windowStyle
& wxSL_RIGHT
)
205 msStyle
|= TBS_RIGHT
;
206 else if ( m_windowStyle
& wxSL_TOP
)
208 else if ( m_windowStyle
& wxSL_BOTTOM
)
209 msStyle
|= TBS_BOTTOM
;
210 else if ( m_windowStyle
& wxSL_BOTH
)
212 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
213 msStyle
|= TBS_NOTICKS
;
215 if ( m_windowStyle
& wxSL_SELRANGE
)
216 msStyle
|= TBS_ENABLESELRANGE
;
218 HWND scroll_bar
= CreateWindowEx
220 exStyle
, TRACKBAR_CLASS
, wxEmptyString
,
222 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
223 wxGetInstance(), NULL
226 m_rangeMax
= maxValue
;
227 m_rangeMin
= minValue
;
229 m_pageSize
= (int)((maxValue
-minValue
)/10);
231 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
232 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
233 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
235 m_hWnd
= (WXHWND
)scroll_bar
;
237 SubclassWin(GetHWND());
239 ::SetWindowText((HWND
) m_hWnd
, wxEmptyString
);
241 SetFont(parent
->GetFont());
243 if ( m_windowStyle
& wxSL_LABELS
)
245 // Finally, create max value static item
247 maxLabel
.Printf(wxT("%d"), maxValue
);
248 wstyle
= STATIC_FLAGS
;
250 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
251 msStyle
|= WS_CLIPSIBLINGS
;
253 m_staticMax
= (WXHWND
) CreateWindowEx
255 0, wxT("STATIC"), maxLabel
,
257 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
258 wxGetInstance(), NULL
264 if (GetFont().GetResourceHandle())
267 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
268 (WPARAM
) GetFont().GetResourceHandle(), 0L);
271 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
272 (WPARAM
) GetFont().GetResourceHandle(), 0L);
275 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
276 (WPARAM
) GetFont().GetResourceHandle(), 0L);
281 SetSize(x
, y
, width
, height
);
287 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
288 WXWORD
WXUNUSED(pos
), WXHWND control
)
290 wxEventType scrollEvent
;
294 scrollEvent
= wxEVT_SCROLL_TOP
;
298 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
302 scrollEvent
= wxEVT_SCROLL_LINEUP
;
306 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
310 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
314 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
318 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
321 case SB_THUMBPOSITION
:
322 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
326 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
330 // unknown scroll event?
334 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
335 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
337 // out of range - but we did process it
343 wxScrollEvent
event(scrollEvent
, m_windowId
);
344 event
.SetPosition(newPos
);
345 event
.SetEventObject( this );
346 GetEventHandler()->ProcessEvent(event
);
348 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
349 cevent
.SetInt( newPos
);
350 cevent
.SetEventObject( this );
352 return GetEventHandler()->ProcessEvent( cevent
);
355 wxSlider95::~wxSlider95()
359 ::DestroyWindow((HWND
) m_staticMin
);
360 m_staticMin
= (WXHWND
) NULL
;
365 ::DestroyWindow((HWND
) m_staticMax
);
366 m_staticMax
= (WXHWND
) NULL
;
371 ::DestroyWindow((HWND
) m_staticValue
);
372 m_staticValue
= (WXHWND
) NULL
;
376 int wxSlider95::GetValue() const
378 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
381 void wxSlider95::SetValue(int value
)
383 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
388 str
.Printf(wxT("%d"), value
);
389 ::SetWindowText((HWND
) m_staticValue
, str
);
393 void wxSlider95::DoGetSize(int *width
, int *height
) const
395 GetSize(width
, height
);
398 void wxSlider95::GetSize(int *width
, int *height
) const
401 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
403 wxFindMaxSize(GetHWND(), &rect
);
406 wxFindMaxSize(m_staticMin
, &rect
);
409 wxFindMaxSize(m_staticMax
, &rect
);
412 wxFindMaxSize(m_staticValue
, &rect
);
414 *width
= rect
.right
- rect
.left
;
415 *height
= rect
.bottom
- rect
.top
;
418 void wxSlider95::GetPosition(int *x
, int *y
) const
420 wxWindow
*parent
= GetParent();
422 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
424 wxFindMaxSize(GetHWND(), &rect
);
427 wxFindMaxSize(m_staticMin
, &rect
);
429 wxFindMaxSize(m_staticMax
, &rect
);
431 wxFindMaxSize(m_staticValue
, &rect
);
433 // Since we now have the absolute screen coords,
434 // if there's a parent we must subtract its top left corner
439 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
441 // We may be faking the client origin.
442 // So a window that's really at (0, 30) may appear
443 // (to wxWin apps) to be at (0, 0).
446 wxPoint
pt(GetParent()->GetClientAreaOrigin());
455 // TODO one day, make sense of all this horros and replace it with a readable
457 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
464 int currentX
, currentY
;
465 GetPosition(¤tX
, ¤tY
);
466 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
468 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
471 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
478 int cx
; // slider,min,max sizes
482 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
484 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
486 if ( m_windowStyle
& wxSL_LABELS
)
490 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
491 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
495 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
496 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
499 int new_width
= (int)(wxMax(min_len
, max_len
));
500 int valueHeight
= (int)cyf
;
502 // For some reason, under Win95, the text edit control has
503 // a lot of space before the first character
506 // The height needs to be a bit bigger under Win95 if
507 // using native 3D effects.
508 valueHeight
= (int) (valueHeight
* 1.5) ;
509 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
510 new_width
, valueHeight
, TRUE
);
511 x_offset
+= new_width
+ cx
;
514 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
515 (int) min_len
, cy
, TRUE
);
516 x_offset
+= (int)(min_len
+ cx
);
518 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
520 int slider_height
= h1
;
521 if (slider_height
< 0 )
524 // Slider must have a minimum/default length/height
525 if (slider_length
< 100)
528 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
529 slider_length
, slider_height
, TRUE
);
530 x_offset
+= slider_length
+ cx
;
532 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
533 (int) max_len
, cy
, TRUE
);
538 // If we're prepared to use the existing size, then...
543 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
554 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
559 if ( m_windowStyle
& wxSL_LABELS
)
562 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
563 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
566 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
567 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
571 int new_width
= (int)(wxMax(min_len
, max_len
));
572 int valueHeight
= (int)cyf
;
573 /*** Suggested change by George Tasker - remove this block...
575 // For some reason, under Win95, the text edit control has
576 // a lot of space before the first character
579 ... and replace with following line: */
582 // The height needs to be a bit bigger under Win95 if
583 // using native 3D effects.
584 valueHeight
= (int) (valueHeight
* 1.5) ;
586 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
587 new_width
, valueHeight
, TRUE
);
588 y_offset
+= valueHeight
;
591 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
592 (int) min_len
, cy
, TRUE
);
595 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
597 int slider_width
= w1
;
598 if (slider_width
< 0 )
601 // Slider must have a minimum/default length
602 if (slider_length
< 100)
605 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
606 slider_width
, slider_length
, TRUE
);
607 y_offset
+= slider_length
;
609 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
610 (int)max_len
, cy
, TRUE
);
615 // If we're prepared to use the existing size, then...
618 width
== -1 && height
== -1
619 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
630 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
635 void wxSlider95::SetRange(int minValue
, int maxValue
)
637 m_rangeMin
= minValue
;
638 m_rangeMax
= maxValue
;
640 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
645 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
646 ::SetWindowText((HWND
) m_staticMin
, buf
);
651 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
652 ::SetWindowText((HWND
) m_staticMax
, buf
);
656 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
657 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
660 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
663 if ( nCtlColor
!= CTLCOLOR_STATIC
)
666 // Otherwise, it's a static
667 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
670 // For trackbars only
671 void wxSlider95::SetTickFreq(int n
, int pos
)
674 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
677 void wxSlider95::SetPageSize(int pageSize
)
679 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
680 m_pageSize
= pageSize
;
683 int wxSlider95::GetPageSize() const
688 void wxSlider95::ClearSel()
690 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
693 void wxSlider95::ClearTicks()
695 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
698 void wxSlider95::SetLineSize(int lineSize
)
700 m_lineSize
= lineSize
;
701 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
704 int wxSlider95::GetLineSize() const
706 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
707 (WPARAM
) 0, (LPARAM
) 0 );
710 int wxSlider95::GetSelEnd() const
712 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
713 (WPARAM
) 0, (LPARAM
) 0 );
716 int wxSlider95::GetSelStart() const
718 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
719 (WPARAM
) 0, (LPARAM
) 0 );
722 void wxSlider95::SetSelection(int minPos
, int maxPos
)
724 ::SendMessage(GetHwnd(), TBM_SETSEL
,
725 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
728 void wxSlider95::SetThumbLength(int len
)
730 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
733 int wxSlider95::GetThumbLength() const
735 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
736 (WPARAM
) 0, (LPARAM
) 0 );
739 void wxSlider95::SetTick(int tickPos
)
741 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
744 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
748 hWnd
== GetStaticMin()
749 || hWnd
== GetStaticMax()
750 || hWnd
== GetEditValue()
754 void wxSlider95::Command (wxCommandEvent
& event
)
756 SetValue (event
.GetInt());
757 ProcessCommand (event
);
760 bool wxSlider95::Show(bool show
)
762 wxWindow::Show(show
);
771 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
774 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
777 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
785 #endif // wxUSE_SLIDER