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 wxBEGIN_FLAGS( wxSliderStyle
)
44 // new style border flags, we put them first to
45 // use them for streaming out
46 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
47 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
48 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
49 wxFLAGS_MEMBER(wxBORDER_RAISED
)
50 wxFLAGS_MEMBER(wxBORDER_STATIC
)
51 wxFLAGS_MEMBER(wxBORDER_NONE
)
53 // old style border flags
54 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
55 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
56 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
57 wxFLAGS_MEMBER(wxRAISED_BORDER
)
58 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
59 wxFLAGS_MEMBER(wxBORDER
)
61 // standard window styles
62 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
63 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
64 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
65 wxFLAGS_MEMBER(wxWANTS_CHARS
)
66 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
67 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
68 wxFLAGS_MEMBER(wxVSCROLL
)
69 wxFLAGS_MEMBER(wxHSCROLL
)
71 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
72 wxFLAGS_MEMBER(wxSL_VERTICAL
)
73 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
74 wxFLAGS_MEMBER(wxSL_LABELS
)
75 wxFLAGS_MEMBER(wxSL_LEFT
)
76 wxFLAGS_MEMBER(wxSL_TOP
)
77 wxFLAGS_MEMBER(wxSL_RIGHT
)
78 wxFLAGS_MEMBER(wxSL_BOTTOM
)
79 wxFLAGS_MEMBER(wxSL_BOTH
)
80 wxFLAGS_MEMBER(wxSL_SELRANGE
)
82 wxEND_FLAGS( wxSliderStyle
)
84 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider95
, wxControl
,"wx/scrolbar.h")
86 wxBEGIN_PROPERTIES_TABLE(wxSlider95
)
87 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxScrollEvent
)
88 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
90 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
91 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
92 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
93 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
94 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
95 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
97 wxEND_PROPERTIES_TABLE()
99 wxBEGIN_HANDLERS_TABLE(wxSlider95
)
100 wxEND_HANDLERS_TABLE()
102 wxCONSTRUCTOR_8( wxSlider95
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
104 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
108 wxSlider95::wxSlider95()
110 m_staticValue
= (WXHWND
) NULL
;
111 m_staticMin
= (WXHWND
) NULL
;
112 m_staticMax
= (WXHWND
) NULL
;
120 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
121 int value
, int minValue
, int maxValue
,
123 const wxSize
& size
, long style
,
124 const wxValidator
& wxVALIDATOR_PARAM(validator
),
125 const wxString
& name
)
127 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
128 style
|= wxBORDER_NONE
;
132 SetValidator(validator
);
133 #endif // wxUSE_VALIDATORS
135 if (parent
) parent
->AddChild(this);
137 SetBackgroundColour(parent
->GetBackgroundColour()) ;
138 SetForegroundColour(parent
->GetForegroundColour()) ;
140 m_staticValue
= (WXHWND
) NULL
;;
141 m_staticMin
= (WXHWND
) NULL
;;
142 m_staticMax
= (WXHWND
) NULL
;;
145 m_windowStyle
= style
;
149 m_windowId
= (int)NewControlId();
161 if ( m_windowStyle
& wxSL_LABELS
)
163 msStyle
|= SS_CENTER
;
166 long valueStyle
= m_windowStyle
& ~wxBORDER_MASK
;
167 valueStyle
|= wxBORDER_SUNKEN
;
168 msStyle
|= MSWGetStyle(valueStyle
, & exStyle
) ;
170 m_staticValue
= (WXHWND
) CreateWindowEx
172 exStyle
, wxT("STATIC"), NULL
,
174 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
175 wxGetInstance(), NULL
178 // Now create min static control
180 minLabel
.Printf(wxT("%d"), minValue
);
181 wstyle
= STATIC_FLAGS
;
182 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
183 msStyle
|= WS_CLIPSIBLINGS
;
184 m_staticMin
= (WXHWND
) CreateWindowEx
186 0, wxT("STATIC"), minLabel
,
188 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
189 wxGetInstance(), NULL
195 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
197 if (m_windowStyle
& wxSL_VERTICAL
)
198 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
200 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
202 if ( m_windowStyle
& wxSL_AUTOTICKS
)
203 msStyle
|= TBS_AUTOTICKS
;
205 if ( m_windowStyle
& wxSL_LEFT
)
207 else if ( m_windowStyle
& wxSL_RIGHT
)
208 msStyle
|= TBS_RIGHT
;
209 else if ( m_windowStyle
& wxSL_TOP
)
211 else if ( m_windowStyle
& wxSL_BOTTOM
)
212 msStyle
|= TBS_BOTTOM
;
213 else if ( m_windowStyle
& wxSL_BOTH
)
215 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
216 msStyle
|= TBS_NOTICKS
;
218 if ( m_windowStyle
& wxSL_SELRANGE
)
219 msStyle
|= TBS_ENABLESELRANGE
;
221 HWND scroll_bar
= CreateWindowEx
223 exStyle
, TRACKBAR_CLASS
, wxEmptyString
,
225 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
226 wxGetInstance(), NULL
229 m_rangeMax
= maxValue
;
230 m_rangeMin
= minValue
;
232 m_pageSize
= (int)((maxValue
-minValue
)/10);
234 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
235 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
236 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
238 m_hWnd
= (WXHWND
)scroll_bar
;
240 SubclassWin(GetHWND());
242 ::SetWindowText((HWND
) m_hWnd
, wxEmptyString
);
244 SetFont(parent
->GetFont());
246 if ( m_windowStyle
& wxSL_LABELS
)
248 // Finally, create max value static item
250 maxLabel
.Printf(wxT("%d"), maxValue
);
251 wstyle
= STATIC_FLAGS
;
253 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
254 msStyle
|= WS_CLIPSIBLINGS
;
256 m_staticMax
= (WXHWND
) CreateWindowEx
258 0, wxT("STATIC"), maxLabel
,
260 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
261 wxGetInstance(), NULL
267 if (GetFont().GetResourceHandle())
270 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
271 (WPARAM
) GetFont().GetResourceHandle(), 0L);
274 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
275 (WPARAM
) GetFont().GetResourceHandle(), 0L);
278 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
279 (WPARAM
) GetFont().GetResourceHandle(), 0L);
284 SetSize(x
, y
, width
, height
);
290 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
291 WXWORD
WXUNUSED(pos
), WXHWND control
)
293 wxEventType scrollEvent
;
297 scrollEvent
= wxEVT_SCROLL_TOP
;
301 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
305 scrollEvent
= wxEVT_SCROLL_LINEUP
;
309 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
313 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
317 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
321 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
324 case SB_THUMBPOSITION
:
325 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
329 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
333 // unknown scroll event?
337 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
338 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
340 // out of range - but we did process it
346 wxScrollEvent
event(scrollEvent
, m_windowId
);
347 event
.SetPosition(newPos
);
348 event
.SetEventObject( this );
349 GetEventHandler()->ProcessEvent(event
);
351 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
352 cevent
.SetInt( newPos
);
353 cevent
.SetEventObject( this );
355 return GetEventHandler()->ProcessEvent( cevent
);
358 wxSlider95::~wxSlider95()
362 ::DestroyWindow((HWND
) m_staticMin
);
363 m_staticMin
= (WXHWND
) NULL
;
368 ::DestroyWindow((HWND
) m_staticMax
);
369 m_staticMax
= (WXHWND
) NULL
;
374 ::DestroyWindow((HWND
) m_staticValue
);
375 m_staticValue
= (WXHWND
) NULL
;
379 int wxSlider95::GetValue() const
381 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
384 void wxSlider95::SetValue(int value
)
386 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
391 str
.Printf(wxT("%d"), value
);
392 ::SetWindowText((HWND
) m_staticValue
, str
);
396 void wxSlider95::DoGetSize(int *width
, int *height
) const
398 GetSize(width
, height
);
401 void wxSlider95::GetSize(int *width
, int *height
) const
404 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
406 wxFindMaxSize(GetHWND(), &rect
);
409 wxFindMaxSize(m_staticMin
, &rect
);
412 wxFindMaxSize(m_staticMax
, &rect
);
415 wxFindMaxSize(m_staticValue
, &rect
);
417 *width
= rect
.right
- rect
.left
;
418 *height
= rect
.bottom
- rect
.top
;
421 void wxSlider95::GetPosition(int *x
, int *y
) const
423 wxWindow
*parent
= GetParent();
425 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
427 wxFindMaxSize(GetHWND(), &rect
);
430 wxFindMaxSize(m_staticMin
, &rect
);
432 wxFindMaxSize(m_staticMax
, &rect
);
434 wxFindMaxSize(m_staticValue
, &rect
);
436 // Since we now have the absolute screen coords,
437 // if there's a parent we must subtract its top left corner
442 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
444 // We may be faking the client origin.
445 // So a window that's really at (0, 30) may appear
446 // (to wxWin apps) to be at (0, 0).
449 wxPoint
pt(GetParent()->GetClientAreaOrigin());
458 // TODO one day, make sense of all this horros and replace it with a readable
460 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
467 int currentX
, currentY
;
468 GetPosition(¤tX
, ¤tY
);
469 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
471 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
474 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
481 int cx
; // slider,min,max sizes
485 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
487 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
489 if ( m_windowStyle
& wxSL_LABELS
)
493 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
494 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
498 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
499 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
502 int new_width
= (int)(wxMax(min_len
, max_len
));
503 int valueHeight
= (int)cyf
;
505 // For some reason, under Win95, the text edit control has
506 // a lot of space before the first character
509 // The height needs to be a bit bigger under Win95 if
510 // using native 3D effects.
511 valueHeight
= (int) (valueHeight
* 1.5) ;
512 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
513 new_width
, valueHeight
, TRUE
);
514 x_offset
+= new_width
+ cx
;
517 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
518 (int) min_len
, cy
, TRUE
);
519 x_offset
+= (int)(min_len
+ cx
);
521 // slider_length = (total width available) - (width used so far)
522 // - (width of max label) - (border)
523 int slider_length
= (int)(w1
- (x_offset
-x
) - max_len
- cx
);
525 int slider_height
= h1
;
526 if (slider_height
< 0 )
529 // Slider must have a minimum/default length/height
530 if (slider_length
< 100)
533 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
534 slider_length
, slider_height
, TRUE
);
535 x_offset
+= slider_length
+ cx
;
537 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
538 (int) max_len
, cy
, TRUE
);
543 // If we're prepared to use the existing size, then...
548 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
559 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
564 if ( m_windowStyle
& wxSL_LABELS
)
567 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
568 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
571 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
572 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
576 int new_width
= (int)(wxMax(min_len
, max_len
));
577 int valueHeight
= (int)cyf
;
580 // The height needs to be a bit bigger under Win95 if
581 // using native 3D effects.
582 valueHeight
= (int) (valueHeight
* 1.5) ;
584 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
585 new_width
, valueHeight
, TRUE
);
586 y_offset
+= valueHeight
;
589 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
590 (int) min_len
, cy
, TRUE
);
593 // slider_length = (total height available) - (height used so far)
594 // - (height of max label) - (border)
595 int slider_length
= (int)(h1
- (y_offset
-y
) - 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