1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/slider95.cpp
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart 1998
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
18 #pragma implementation "slider95.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
38 #include "wx/slider.h"
39 #include "wx/msw/subwin.h"
41 #if !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // indices of labels in wxSlider95::m_labels
58 // the gap between the slider and the labels, in pixels
59 static const int HGAP
= 5;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #if wxUSE_EXTENDED_RTTI
66 WX_DEFINE_FLAGS( wxSliderStyle
)
68 wxBEGIN_FLAGS( wxSliderStyle
)
69 // new style border flags, we put them first to
70 // use them for streaming out
71 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
72 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
73 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
74 wxFLAGS_MEMBER(wxBORDER_RAISED
)
75 wxFLAGS_MEMBER(wxBORDER_STATIC
)
76 wxFLAGS_MEMBER(wxBORDER_NONE
)
78 // old style border flags
79 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
80 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
81 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
82 wxFLAGS_MEMBER(wxRAISED_BORDER
)
83 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
84 wxFLAGS_MEMBER(wxBORDER
)
86 // standard window styles
87 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
88 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
89 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
90 wxFLAGS_MEMBER(wxWANTS_CHARS
)
91 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
92 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
93 wxFLAGS_MEMBER(wxVSCROLL
)
94 wxFLAGS_MEMBER(wxHSCROLL
)
96 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
97 wxFLAGS_MEMBER(wxSL_VERTICAL
)
98 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
99 wxFLAGS_MEMBER(wxSL_LABELS
)
100 wxFLAGS_MEMBER(wxSL_LEFT
)
101 wxFLAGS_MEMBER(wxSL_TOP
)
102 wxFLAGS_MEMBER(wxSL_RIGHT
)
103 wxFLAGS_MEMBER(wxSL_BOTTOM
)
104 wxFLAGS_MEMBER(wxSL_BOTH
)
105 wxFLAGS_MEMBER(wxSL_SELRANGE
)
107 wxEND_FLAGS( wxSliderStyle
)
109 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider95
, wxControl
,"wx/scrolbar.h")
111 wxBEGIN_PROPERTIES_TABLE(wxSlider95
)
112 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxScrollEvent
)
113 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
115 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
117 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
118 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
119 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
120 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
122 wxEND_PROPERTIES_TABLE()
124 wxBEGIN_HANDLERS_TABLE(wxSlider95
)
125 wxEND_HANDLERS_TABLE()
127 wxCONSTRUCTOR_8( wxSlider95
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
129 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
132 // ============================================================================
133 // wxSlider95 implementation
134 // ============================================================================
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 void wxSlider95::Init()
152 wxSlider95::Create(wxWindow
*parent
,
160 const wxValidator
& validator
,
161 const wxString
& name
)
163 // initialize everything
164 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
167 // ensure that we have correct values for GetLabelsSize()
168 m_rangeMin
= minValue
;
169 m_rangeMax
= maxValue
;
171 // create the labels first, so that our DoGetBestSize() could take them
174 // note that we could simply create 3 wxStaticTexts here but it could
175 // result in some observable side effects at wx level (e.g. the parent of
176 // wxSlider would have 3 more children than expected) and so we prefer not
177 // to do it like this
178 if ( m_windowStyle
& wxSL_LABELS
)
180 m_labels
= new wxSubwindows(SliderLabel_Last
);
182 HWND hwndParent
= GetHwndOf(parent
);
183 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
185 (*m_labels
)[n
] = ::CreateWindow
189 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
192 (HMENU
)NewControlId(),
198 m_labels
->SetFont(GetFont());
201 // now create the main control too
202 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
205 // and initialize everything
206 SetRange(minValue
, maxValue
);
208 SetPageSize((maxValue
- minValue
)/10);
210 // we need to position the labels correctly if we have them and if
211 // SetSize() hadn't been called before (when best size was determined by
212 // MSWCreateControl()) as in this case they haven't been put in place yet
213 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
221 WXDWORD
wxSlider95::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
223 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
225 // TBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
226 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
228 if ( style
& wxSL_AUTOTICKS
)
229 msStyle
|= TBS_AUTOTICKS
;
231 // again, TBS_RIGHT is 0 but do include it for clarity
232 if ( style
& wxSL_LEFT
)
234 else if ( style
& wxSL_RIGHT
)
235 msStyle
|= TBS_RIGHT
;
236 else if ( style
& wxSL_TOP
)
238 else if ( style
& wxSL_BOTTOM
)
239 msStyle
|= TBS_BOTTOM
;
240 else if ( style
& wxSL_BOTH
)
242 else if ( !(style
& wxSL_AUTOTICKS
) )
243 msStyle
|= TBS_NOTICKS
;
245 if ( style
& wxSL_SELRANGE
)
246 msStyle
|= TBS_ENABLESELRANGE
;
251 wxSlider95::~wxSlider95()
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
),
262 WXWORD
WXUNUSED(pos
),
265 wxEventType scrollEvent
;
269 scrollEvent
= wxEVT_SCROLL_TOP
;
273 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
277 scrollEvent
= wxEVT_SCROLL_LINEUP
;
281 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
285 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
289 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
293 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
296 case SB_THUMBPOSITION
:
297 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
301 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
305 // unknown scroll event?
309 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
310 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
312 // out of range - but we did process it
318 wxScrollEvent
event(scrollEvent
, m_windowId
);
319 event
.SetPosition(newPos
);
320 event
.SetEventObject( this );
321 GetEventHandler()->ProcessEvent(event
);
323 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
324 cevent
.SetInt( newPos
);
325 cevent
.SetEventObject( this );
327 return GetEventHandler()->ProcessEvent( cevent
);
330 void wxSlider95::Command (wxCommandEvent
& event
)
332 SetValue (event
.GetInt());
333 ProcessCommand (event
);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 wxRect
wxSlider95::GetBoundingBox() const
342 // take care not to call our own functions which would call us recursively
344 wxSliderBase::DoGetPosition(&x
, &y
);
345 wxSliderBase::DoGetSize(&w
, &h
);
347 wxRect
rect(x
, y
, w
, h
);
349 rect
.Union(m_labels
->GetBoundingBox());
354 void wxSlider95::DoGetSize(int *width
, int *height
) const
356 wxRect rect
= GetBoundingBox();
361 *height
= rect
.height
;
364 void wxSlider95::DoGetPosition(int *x
, int *y
) const
366 wxRect rect
= GetBoundingBox();
374 int wxSlider95::GetLabelsSize(int *width
) const
380 // find the max label width
381 int wLabelMin
, wLabelMax
;
382 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
383 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
385 *width
= wxMax(wLabelMin
, wLabelMax
);
389 cy
= GetCharHeight();
392 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
395 void wxSlider95::DoMoveWindow(int x
, int y
, int width
, int height
)
397 // all complications below are because we need to position the labels,
398 // without them everything is easy
401 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
405 // be careful to position the slider itself after moving the labels as
406 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
407 // would return a wrong result and wrong size would be cached internally
408 if ( HasFlag(wxSL_VERTICAL
) )
411 int hLabel
= GetLabelsSize(&wLabel
);
413 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
415 // position all labels: min at the top, value in the middle and max at
417 ::MoveWindow((*m_labels
)[SliderLabel_Min
],
418 xLabel
, y
, wLabel
, hLabel
, TRUE
);
420 ::MoveWindow((*m_labels
)[SliderLabel_Value
],
421 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
, TRUE
);
423 ::MoveWindow((*m_labels
)[SliderLabel_Max
],
424 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
, TRUE
);
426 // position the slider itself along the left/right edge
427 ::MoveWindow(GetHwnd(),
428 HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
430 width
- wLabel
- HGAP
,
437 int hLabel
= GetLabelsSize(&wLabel
);
439 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
441 // position all labels: min on the left, value in the middle and max to
443 ::MoveWindow((*m_labels
)[SliderLabel_Min
],
444 x
, yLabel
, wLabel
, hLabel
, TRUE
);
446 ::MoveWindow((*m_labels
)[SliderLabel_Value
],
447 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
, TRUE
);
449 ::MoveWindow((*m_labels
)[SliderLabel_Max
],
450 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
, TRUE
);
452 // position the slider itself along the top/bottom edge
453 ::MoveWindow(GetHwnd(),
455 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
462 wxSize
wxSlider95::DoGetBestSize() const
464 // these values are arbitrary
465 static const int length
= 100;
466 static const int thickness
= 26;
469 if ( HasFlag(wxSL_VERTICAL
) )
477 int hLabel
= GetLabelsSize(&wLabel
);
479 // account for the labels
480 size
.x
+= HGAP
+ wLabel
;
482 // labels are indented relative to the slider itself
493 // labels add extra height
494 size
.y
+= GetLabelsSize();
501 // ----------------------------------------------------------------------------
502 // slider-specific methods
503 // ----------------------------------------------------------------------------
505 int wxSlider95::GetValue() const
507 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
510 void wxSlider95::SetValue(int value
)
512 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
516 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
520 void wxSlider95::SetRange(int minValue
, int maxValue
)
522 m_rangeMin
= minValue
;
523 m_rangeMax
= maxValue
;
525 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
529 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(m_rangeMin
));
530 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(m_rangeMax
));
534 void wxSlider95::SetTickFreq(int n
, int pos
)
537 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
540 void wxSlider95::SetPageSize(int pageSize
)
542 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
543 m_pageSize
= pageSize
;
546 int wxSlider95::GetPageSize() const
551 void wxSlider95::ClearSel()
553 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
556 void wxSlider95::ClearTicks()
558 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
561 void wxSlider95::SetLineSize(int lineSize
)
563 m_lineSize
= lineSize
;
564 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
567 int wxSlider95::GetLineSize() const
569 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
572 int wxSlider95::GetSelEnd() const
574 return (int)::SendMessage(GetHwnd(), TBM_SETSELEND
, 0, 0);
577 int wxSlider95::GetSelStart() const
579 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
582 void wxSlider95::SetSelection(int minPos
, int maxPos
)
584 ::SendMessage(GetHwnd(), TBM_SETSEL
,
585 (WPARAM
) TRUE
/* redraw */,
586 (LPARAM
) MAKELONG( minPos
, maxPos
) );
589 void wxSlider95::SetThumbLength(int len
)
591 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
594 int wxSlider95::GetThumbLength() const
596 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
599 void wxSlider95::SetTick(int tickPos
)
601 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
604 // ----------------------------------------------------------------------------
605 // composite control methods
606 // ----------------------------------------------------------------------------
608 WXHWND
wxSlider95::GetStaticMin() const
610 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
613 WXHWND
wxSlider95::GetStaticMax() const
615 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
618 WXHWND
wxSlider95::GetEditValue() const
620 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
623 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider95
, wxSliderBase
, m_labels
)
625 #endif // wxUSE_SLIDER