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(__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
,
62 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
63 style
|= wxBORDER_NONE
;
67 SetValidator(validator
);
68 #endif // wxUSE_VALIDATORS
70 if (parent
) parent
->AddChild(this);
72 SetBackgroundColour(parent
->GetBackgroundColour()) ;
73 SetForegroundColour(parent
->GetForegroundColour()) ;
75 m_staticValue
= (WXHWND
) NULL
;;
76 m_staticMin
= (WXHWND
) NULL
;;
77 m_staticMax
= (WXHWND
) NULL
;;
80 m_windowStyle
= style
;
84 m_windowId
= (int)NewControlId();
96 if ( m_windowStyle
& wxSL_LABELS
)
101 long valueStyle
= m_windowStyle
& ~wxBORDER_MASK
;
102 valueStyle
|= wxBORDER_SUNKEN
;
103 msStyle
|= MSWGetStyle(valueStyle
, & exStyle
) ;
105 m_staticValue
= (WXHWND
) CreateWindowEx
107 exStyle
, wxT("STATIC"), NULL
,
109 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
110 wxGetInstance(), NULL
113 // Now create min static control
115 minLabel
.Printf(wxT("%d"), minValue
);
116 wstyle
= STATIC_FLAGS
;
117 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
118 msStyle
|= WS_CLIPSIBLINGS
;
119 m_staticMin
= (WXHWND
) CreateWindowEx
121 0, wxT("STATIC"), minLabel
,
123 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
124 wxGetInstance(), NULL
130 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
132 if (m_windowStyle
& wxSL_VERTICAL
)
133 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
135 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
137 if ( m_windowStyle
& wxSL_AUTOTICKS
)
138 msStyle
|= TBS_AUTOTICKS
;
140 if ( m_windowStyle
& wxSL_LEFT
)
142 else if ( m_windowStyle
& wxSL_RIGHT
)
143 msStyle
|= TBS_RIGHT
;
144 else if ( m_windowStyle
& wxSL_TOP
)
146 else if ( m_windowStyle
& wxSL_BOTTOM
)
147 msStyle
|= TBS_BOTTOM
;
148 else if ( m_windowStyle
& wxSL_BOTH
)
150 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
151 msStyle
|= TBS_NOTICKS
;
153 if ( m_windowStyle
& wxSL_SELRANGE
)
154 msStyle
|= TBS_ENABLESELRANGE
;
156 HWND scroll_bar
= CreateWindowEx
158 exStyle
, TRACKBAR_CLASS
, wxEmptyString
,
160 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
161 wxGetInstance(), NULL
164 m_rangeMax
= maxValue
;
165 m_rangeMin
= minValue
;
167 m_pageSize
= (int)((maxValue
-minValue
)/10);
169 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
170 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
171 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
173 m_hWnd
= (WXHWND
)scroll_bar
;
175 SubclassWin(GetHWND());
177 ::SetWindowText((HWND
) m_hWnd
, wxEmptyString
);
179 SetFont(parent
->GetFont());
181 if ( m_windowStyle
& wxSL_LABELS
)
183 // Finally, create max value static item
185 maxLabel
.Printf(wxT("%d"), maxValue
);
186 wstyle
= STATIC_FLAGS
;
188 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
189 msStyle
|= WS_CLIPSIBLINGS
;
191 m_staticMax
= (WXHWND
) CreateWindowEx
193 0, wxT("STATIC"), maxLabel
,
195 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
196 wxGetInstance(), NULL
202 if (GetFont().GetResourceHandle())
205 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
206 (WPARAM
) GetFont().GetResourceHandle(), 0L);
209 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
210 (WPARAM
) GetFont().GetResourceHandle(), 0L);
213 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
214 (WPARAM
) GetFont().GetResourceHandle(), 0L);
219 SetSize(x
, y
, width
, height
);
225 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
226 WXWORD
WXUNUSED(pos
), WXHWND control
)
228 wxEventType scrollEvent
;
232 scrollEvent
= wxEVT_SCROLL_TOP
;
236 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
240 scrollEvent
= wxEVT_SCROLL_LINEUP
;
244 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
248 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
252 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
256 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
259 case SB_THUMBPOSITION
:
260 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
264 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
268 // unknown scroll event?
272 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
273 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
275 // out of range - but we did process it
281 wxScrollEvent
event(scrollEvent
, m_windowId
);
282 event
.SetPosition(newPos
);
283 event
.SetEventObject( this );
284 GetEventHandler()->ProcessEvent(event
);
286 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
287 cevent
.SetInt( newPos
);
288 cevent
.SetEventObject( this );
290 return GetEventHandler()->ProcessEvent( cevent
);
293 wxSlider95::~wxSlider95()
297 ::DestroyWindow((HWND
) m_staticMin
);
298 m_staticMin
= (WXHWND
) NULL
;
303 ::DestroyWindow((HWND
) m_staticMax
);
304 m_staticMax
= (WXHWND
) NULL
;
309 ::DestroyWindow((HWND
) m_staticValue
);
310 m_staticValue
= (WXHWND
) NULL
;
314 int wxSlider95::GetValue() const
316 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
319 void wxSlider95::SetValue(int value
)
321 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
326 str
.Printf(wxT("%d"), value
);
327 ::SetWindowText((HWND
) m_staticValue
, str
);
331 void wxSlider95::DoGetSize(int *width
, int *height
) const
333 GetSize(width
, height
);
336 void wxSlider95::GetSize(int *width
, int *height
) const
339 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
341 wxFindMaxSize(GetHWND(), &rect
);
344 wxFindMaxSize(m_staticMin
, &rect
);
347 wxFindMaxSize(m_staticMax
, &rect
);
350 wxFindMaxSize(m_staticValue
, &rect
);
352 *width
= rect
.right
- rect
.left
;
353 *height
= rect
.bottom
- rect
.top
;
356 void wxSlider95::GetPosition(int *x
, int *y
) const
358 wxWindow
*parent
= GetParent();
360 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
362 wxFindMaxSize(GetHWND(), &rect
);
365 wxFindMaxSize(m_staticMin
, &rect
);
367 wxFindMaxSize(m_staticMax
, &rect
);
369 wxFindMaxSize(m_staticValue
, &rect
);
371 // Since we now have the absolute screen coords,
372 // if there's a parent we must subtract its top left corner
377 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
379 // We may be faking the client origin.
380 // So a window that's really at (0, 30) may appear
381 // (to wxWin apps) to be at (0, 0).
384 wxPoint
pt(GetParent()->GetClientAreaOrigin());
393 // TODO one day, make sense of all this horros and replace it with a readable
395 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
402 int currentX
, currentY
;
403 GetPosition(¤tX
, ¤tY
);
404 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
406 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
409 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
416 int cx
; // slider,min,max sizes
420 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
422 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
424 if ( m_windowStyle
& wxSL_LABELS
)
428 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
429 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
433 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
434 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
437 int new_width
= (int)(wxMax(min_len
, max_len
));
438 int valueHeight
= (int)cyf
;
440 // For some reason, under Win95, the text edit control has
441 // a lot of space before the first character
444 // The height needs to be a bit bigger under Win95 if
445 // using native 3D effects.
446 valueHeight
= (int) (valueHeight
* 1.5) ;
447 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
448 new_width
, valueHeight
, TRUE
);
449 x_offset
+= new_width
+ cx
;
452 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
453 (int) min_len
, cy
, TRUE
);
454 x_offset
+= (int)(min_len
+ cx
);
456 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
458 int slider_height
= h1
;
459 if (slider_height
< 0 )
462 // Slider must have a minimum/default length/height
463 if (slider_length
< 100)
466 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
467 slider_length
, slider_height
, TRUE
);
468 x_offset
+= slider_length
+ cx
;
470 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
471 (int) max_len
, cy
, TRUE
);
476 // If we're prepared to use the existing size, then...
481 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
492 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
497 if ( m_windowStyle
& wxSL_LABELS
)
500 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
501 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
504 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
505 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
509 int new_width
= (int)(wxMax(min_len
, max_len
));
510 int valueHeight
= (int)cyf
;
511 /*** Suggested change by George Tasker - remove this block...
513 // For some reason, under Win95, the text edit control has
514 // a lot of space before the first character
517 ... and replace with following line: */
520 // The height needs to be a bit bigger under Win95 if
521 // using native 3D effects.
522 valueHeight
= (int) (valueHeight
* 1.5) ;
524 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
525 new_width
, valueHeight
, TRUE
);
526 y_offset
+= valueHeight
;
529 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
530 (int) min_len
, cy
, TRUE
);
533 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
535 int slider_width
= w1
;
536 if (slider_width
< 0 )
539 // Slider must have a minimum/default length
540 if (slider_length
< 100)
543 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
544 slider_width
, slider_length
, TRUE
);
545 y_offset
+= slider_length
;
547 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
548 (int)max_len
, cy
, TRUE
);
553 // If we're prepared to use the existing size, then...
556 width
== -1 && height
== -1
557 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
568 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
573 void wxSlider95::SetRange(int minValue
, int maxValue
)
575 m_rangeMin
= minValue
;
576 m_rangeMax
= maxValue
;
578 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
583 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
584 ::SetWindowText((HWND
) m_staticMin
, buf
);
589 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
590 ::SetWindowText((HWND
) m_staticMax
, buf
);
594 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
595 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
598 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
601 if ( nCtlColor
!= CTLCOLOR_STATIC
)
604 // Otherwise, it's a static
605 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
608 // For trackbars only
609 void wxSlider95::SetTickFreq(int n
, int pos
)
612 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
615 void wxSlider95::SetPageSize(int pageSize
)
617 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
618 m_pageSize
= pageSize
;
621 int wxSlider95::GetPageSize() const
626 void wxSlider95::ClearSel()
628 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
631 void wxSlider95::ClearTicks()
633 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
636 void wxSlider95::SetLineSize(int lineSize
)
638 m_lineSize
= lineSize
;
639 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
642 int wxSlider95::GetLineSize() const
644 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
645 (WPARAM
) 0, (LPARAM
) 0 );
648 int wxSlider95::GetSelEnd() const
650 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
651 (WPARAM
) 0, (LPARAM
) 0 );
654 int wxSlider95::GetSelStart() const
656 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
657 (WPARAM
) 0, (LPARAM
) 0 );
660 void wxSlider95::SetSelection(int minPos
, int maxPos
)
662 ::SendMessage(GetHwnd(), TBM_SETSEL
,
663 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
666 void wxSlider95::SetThumbLength(int len
)
668 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
671 int wxSlider95::GetThumbLength() const
673 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
674 (WPARAM
) 0, (LPARAM
) 0 );
677 void wxSlider95::SetTick(int tickPos
)
679 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
682 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
686 hWnd
== GetStaticMin()
687 || hWnd
== GetStaticMax()
688 || hWnd
== GetEditValue()
692 void wxSlider95::Command (wxCommandEvent
& event
)
694 SetValue (event
.GetInt());
695 ProcessCommand (event
);
698 bool wxSlider95::Show(bool show
)
700 wxWindow::Show(show
);
709 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
712 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
715 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
723 #endif // wxUSE_SLIDER