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 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider95
, wxControl
,"wx/scrolbar.h")
43 WX_BEGIN_PROPERTIES_TABLE(wxSlider95
)
44 WX_PROPERTY( Value
, int , SetValue
, GetValue
, 0)
45 WX_PROPERTY( Minimum
, int , SetMin
, GetMin
, 0 )
46 WX_PROPERTY( Maximum
, int , SetMax
, GetMax
, 0 )
47 WX_PROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 )
48 WX_PROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 )
49 WX_PROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 )
50 WX_END_PROPERTIES_TABLE()
52 WX_BEGIN_HANDLERS_TABLE(wxSlider95
)
53 WX_END_HANDLERS_TABLE()
55 WX_CONSTRUCTOR_8( wxSlider95
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
57 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
61 wxSlider95::wxSlider95()
63 m_staticValue
= (WXHWND
) NULL
;
64 m_staticMin
= (WXHWND
) NULL
;
65 m_staticMax
= (WXHWND
) NULL
;
73 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
74 int value
, int minValue
, int maxValue
,
76 const wxSize
& size
, long style
,
77 const wxValidator
& validator
,
80 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
81 style
|= wxBORDER_NONE
;
85 SetValidator(validator
);
86 #endif // wxUSE_VALIDATORS
88 if (parent
) parent
->AddChild(this);
90 SetBackgroundColour(parent
->GetBackgroundColour()) ;
91 SetForegroundColour(parent
->GetForegroundColour()) ;
93 m_staticValue
= (WXHWND
) NULL
;;
94 m_staticMin
= (WXHWND
) NULL
;;
95 m_staticMax
= (WXHWND
) NULL
;;
98 m_windowStyle
= style
;
102 m_windowId
= (int)NewControlId();
114 if ( m_windowStyle
& wxSL_LABELS
)
116 msStyle
|= SS_CENTER
;
119 long valueStyle
= m_windowStyle
& ~wxBORDER_MASK
;
120 valueStyle
|= wxBORDER_SUNKEN
;
121 msStyle
|= MSWGetStyle(valueStyle
, & exStyle
) ;
123 m_staticValue
= (WXHWND
) CreateWindowEx
125 exStyle
, wxT("STATIC"), NULL
,
127 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
128 wxGetInstance(), NULL
131 // Now create min static control
133 minLabel
.Printf(wxT("%d"), minValue
);
134 wstyle
= STATIC_FLAGS
;
135 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
136 msStyle
|= WS_CLIPSIBLINGS
;
137 m_staticMin
= (WXHWND
) CreateWindowEx
139 0, wxT("STATIC"), minLabel
,
141 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
142 wxGetInstance(), NULL
148 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
150 if (m_windowStyle
& wxSL_VERTICAL
)
151 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
153 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
155 if ( m_windowStyle
& wxSL_AUTOTICKS
)
156 msStyle
|= TBS_AUTOTICKS
;
158 if ( m_windowStyle
& wxSL_LEFT
)
160 else if ( m_windowStyle
& wxSL_RIGHT
)
161 msStyle
|= TBS_RIGHT
;
162 else if ( m_windowStyle
& wxSL_TOP
)
164 else if ( m_windowStyle
& wxSL_BOTTOM
)
165 msStyle
|= TBS_BOTTOM
;
166 else if ( m_windowStyle
& wxSL_BOTH
)
168 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
169 msStyle
|= TBS_NOTICKS
;
171 if ( m_windowStyle
& wxSL_SELRANGE
)
172 msStyle
|= TBS_ENABLESELRANGE
;
174 HWND scroll_bar
= CreateWindowEx
176 exStyle
, TRACKBAR_CLASS
, wxEmptyString
,
178 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
179 wxGetInstance(), NULL
182 m_rangeMax
= maxValue
;
183 m_rangeMin
= minValue
;
185 m_pageSize
= (int)((maxValue
-minValue
)/10);
187 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
188 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
189 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
191 m_hWnd
= (WXHWND
)scroll_bar
;
193 SubclassWin(GetHWND());
195 ::SetWindowText((HWND
) m_hWnd
, wxEmptyString
);
197 SetFont(parent
->GetFont());
199 if ( m_windowStyle
& wxSL_LABELS
)
201 // Finally, create max value static item
203 maxLabel
.Printf(wxT("%d"), maxValue
);
204 wstyle
= STATIC_FLAGS
;
206 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
207 msStyle
|= WS_CLIPSIBLINGS
;
209 m_staticMax
= (WXHWND
) CreateWindowEx
211 0, wxT("STATIC"), maxLabel
,
213 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
214 wxGetInstance(), NULL
220 if (GetFont().GetResourceHandle())
223 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
224 (WPARAM
) GetFont().GetResourceHandle(), 0L);
227 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
228 (WPARAM
) GetFont().GetResourceHandle(), 0L);
231 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
232 (WPARAM
) GetFont().GetResourceHandle(), 0L);
237 SetSize(x
, y
, width
, height
);
243 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
244 WXWORD
WXUNUSED(pos
), WXHWND control
)
246 wxEventType scrollEvent
;
250 scrollEvent
= wxEVT_SCROLL_TOP
;
254 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
258 scrollEvent
= wxEVT_SCROLL_LINEUP
;
262 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
266 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
270 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
274 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
277 case SB_THUMBPOSITION
:
278 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
282 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
286 // unknown scroll event?
290 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
291 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
293 // out of range - but we did process it
299 wxScrollEvent
event(scrollEvent
, m_windowId
);
300 event
.SetPosition(newPos
);
301 event
.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(event
);
304 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
305 cevent
.SetInt( newPos
);
306 cevent
.SetEventObject( this );
308 return GetEventHandler()->ProcessEvent( cevent
);
311 wxSlider95::~wxSlider95()
315 ::DestroyWindow((HWND
) m_staticMin
);
316 m_staticMin
= (WXHWND
) NULL
;
321 ::DestroyWindow((HWND
) m_staticMax
);
322 m_staticMax
= (WXHWND
) NULL
;
327 ::DestroyWindow((HWND
) m_staticValue
);
328 m_staticValue
= (WXHWND
) NULL
;
332 int wxSlider95::GetValue() const
334 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
337 void wxSlider95::SetValue(int value
)
339 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
344 str
.Printf(wxT("%d"), value
);
345 ::SetWindowText((HWND
) m_staticValue
, str
);
349 void wxSlider95::DoGetSize(int *width
, int *height
) const
351 GetSize(width
, height
);
354 void wxSlider95::GetSize(int *width
, int *height
) const
357 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
359 wxFindMaxSize(GetHWND(), &rect
);
362 wxFindMaxSize(m_staticMin
, &rect
);
365 wxFindMaxSize(m_staticMax
, &rect
);
368 wxFindMaxSize(m_staticValue
, &rect
);
370 *width
= rect
.right
- rect
.left
;
371 *height
= rect
.bottom
- rect
.top
;
374 void wxSlider95::GetPosition(int *x
, int *y
) const
376 wxWindow
*parent
= GetParent();
378 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
380 wxFindMaxSize(GetHWND(), &rect
);
383 wxFindMaxSize(m_staticMin
, &rect
);
385 wxFindMaxSize(m_staticMax
, &rect
);
387 wxFindMaxSize(m_staticValue
, &rect
);
389 // Since we now have the absolute screen coords,
390 // if there's a parent we must subtract its top left corner
395 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
397 // We may be faking the client origin.
398 // So a window that's really at (0, 30) may appear
399 // (to wxWin apps) to be at (0, 0).
402 wxPoint
pt(GetParent()->GetClientAreaOrigin());
411 // TODO one day, make sense of all this horros and replace it with a readable
413 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
420 int currentX
, currentY
;
421 GetPosition(¤tX
, ¤tY
);
422 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
424 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
427 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
434 int cx
; // slider,min,max sizes
438 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
440 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
442 if ( m_windowStyle
& wxSL_LABELS
)
446 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
447 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
451 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
452 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
455 int new_width
= (int)(wxMax(min_len
, max_len
));
456 int valueHeight
= (int)cyf
;
458 // For some reason, under Win95, the text edit control has
459 // a lot of space before the first character
462 // The height needs to be a bit bigger under Win95 if
463 // using native 3D effects.
464 valueHeight
= (int) (valueHeight
* 1.5) ;
465 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
466 new_width
, valueHeight
, TRUE
);
467 x_offset
+= new_width
+ cx
;
470 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
471 (int) min_len
, cy
, TRUE
);
472 x_offset
+= (int)(min_len
+ cx
);
474 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
476 int slider_height
= h1
;
477 if (slider_height
< 0 )
480 // Slider must have a minimum/default length/height
481 if (slider_length
< 100)
484 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
485 slider_length
, slider_height
, TRUE
);
486 x_offset
+= slider_length
+ cx
;
488 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
489 (int) max_len
, cy
, TRUE
);
494 // If we're prepared to use the existing size, then...
499 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
510 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
515 if ( m_windowStyle
& wxSL_LABELS
)
518 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
519 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
522 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
523 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
527 int new_width
= (int)(wxMax(min_len
, max_len
));
528 int valueHeight
= (int)cyf
;
529 /*** Suggested change by George Tasker - remove this block...
531 // For some reason, under Win95, the text edit control has
532 // a lot of space before the first character
535 ... and replace with following line: */
538 // The height needs to be a bit bigger under Win95 if
539 // using native 3D effects.
540 valueHeight
= (int) (valueHeight
* 1.5) ;
542 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
543 new_width
, valueHeight
, TRUE
);
544 y_offset
+= valueHeight
;
547 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
548 (int) min_len
, cy
, TRUE
);
551 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
553 int slider_width
= w1
;
554 if (slider_width
< 0 )
557 // Slider must have a minimum/default length
558 if (slider_length
< 100)
561 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
562 slider_width
, slider_length
, TRUE
);
563 y_offset
+= slider_length
;
565 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
566 (int)max_len
, cy
, TRUE
);
571 // If we're prepared to use the existing size, then...
574 width
== -1 && height
== -1
575 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
586 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
591 void wxSlider95::SetRange(int minValue
, int maxValue
)
593 m_rangeMin
= minValue
;
594 m_rangeMax
= maxValue
;
596 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
601 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
602 ::SetWindowText((HWND
) m_staticMin
, buf
);
607 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
608 ::SetWindowText((HWND
) m_staticMax
, buf
);
612 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
613 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
616 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
619 if ( nCtlColor
!= CTLCOLOR_STATIC
)
622 // Otherwise, it's a static
623 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
626 // For trackbars only
627 void wxSlider95::SetTickFreq(int n
, int pos
)
630 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
633 void wxSlider95::SetPageSize(int pageSize
)
635 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
636 m_pageSize
= pageSize
;
639 int wxSlider95::GetPageSize() const
644 void wxSlider95::ClearSel()
646 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
649 void wxSlider95::ClearTicks()
651 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
654 void wxSlider95::SetLineSize(int lineSize
)
656 m_lineSize
= lineSize
;
657 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
660 int wxSlider95::GetLineSize() const
662 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
663 (WPARAM
) 0, (LPARAM
) 0 );
666 int wxSlider95::GetSelEnd() const
668 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
669 (WPARAM
) 0, (LPARAM
) 0 );
672 int wxSlider95::GetSelStart() const
674 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
675 (WPARAM
) 0, (LPARAM
) 0 );
678 void wxSlider95::SetSelection(int minPos
, int maxPos
)
680 ::SendMessage(GetHwnd(), TBM_SETSEL
,
681 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
684 void wxSlider95::SetThumbLength(int len
)
686 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
689 int wxSlider95::GetThumbLength() const
691 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
692 (WPARAM
) 0, (LPARAM
) 0 );
695 void wxSlider95::SetTick(int tickPos
)
697 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
700 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
704 hWnd
== GetStaticMin()
705 || hWnd
== GetStaticMax()
706 || hWnd
== GetEditValue()
710 void wxSlider95::Command (wxCommandEvent
& event
)
712 SetValue (event
.GetInt());
713 ProcessCommand (event
);
716 bool wxSlider95::Show(bool show
)
718 wxWindow::Show(show
);
727 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
730 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
733 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
741 #endif // wxUSE_SLIDER