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 msStyle
|= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
103 m_staticValue
= (WXHWND
) CreateWindowEx
105 exStyle
, wxT("STATIC"), NULL
,
107 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
108 wxGetInstance(), NULL
111 // Now create min static control
113 minLabel
.Printf(wxT("%d"), minValue
);
114 wstyle
= STATIC_FLAGS
;
115 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
116 msStyle
|= WS_CLIPSIBLINGS
;
117 m_staticMin
= (WXHWND
) CreateWindowEx
119 0, wxT("STATIC"), minLabel
,
121 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
122 wxGetInstance(), NULL
128 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
130 if (m_windowStyle
& wxSL_VERTICAL
)
131 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
133 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
135 if ( m_windowStyle
& wxSL_AUTOTICKS
)
136 msStyle
|= TBS_AUTOTICKS
;
138 if ( m_windowStyle
& wxSL_LEFT
)
140 else if ( m_windowStyle
& wxSL_RIGHT
)
141 msStyle
|= TBS_RIGHT
;
142 else if ( m_windowStyle
& wxSL_TOP
)
144 else if ( m_windowStyle
& wxSL_BOTTOM
)
145 msStyle
|= TBS_BOTTOM
;
146 else if ( m_windowStyle
& wxSL_BOTH
)
148 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
149 msStyle
|= TBS_NOTICKS
;
151 if ( m_windowStyle
& wxSL_SELRANGE
)
152 msStyle
|= TBS_ENABLESELRANGE
;
154 HWND scroll_bar
= CreateWindowEx
156 exStyle
, TRACKBAR_CLASS
, wxT(""),
158 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
159 wxGetInstance(), NULL
162 m_rangeMax
= maxValue
;
163 m_rangeMin
= minValue
;
165 m_pageSize
= (int)((maxValue
-minValue
)/10);
167 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
168 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
169 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
171 m_hWnd
= (WXHWND
)scroll_bar
;
173 SubclassWin(GetHWND());
175 ::SetWindowText((HWND
) m_hWnd
, wxT(""));
177 SetFont(parent
->GetFont());
179 if ( m_windowStyle
& wxSL_LABELS
)
181 // Finally, create max value static item
183 maxLabel
.Printf(wxT("%d"), maxValue
);
184 wstyle
= STATIC_FLAGS
;
186 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
187 msStyle
|= WS_CLIPSIBLINGS
;
189 m_staticMax
= (WXHWND
) CreateWindowEx
191 0, wxT("STATIC"), maxLabel
,
193 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
194 wxGetInstance(), NULL
200 if (GetFont().GetResourceHandle())
203 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
204 (WPARAM
) GetFont().GetResourceHandle(), 0L);
207 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
208 (WPARAM
) GetFont().GetResourceHandle(), 0L);
211 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
212 (WPARAM
) GetFont().GetResourceHandle(), 0L);
217 SetSize(x
, y
, width
, height
);
223 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
224 WXWORD
WXUNUSED(pos
), WXHWND control
)
226 wxEventType scrollEvent
;
230 scrollEvent
= wxEVT_SCROLL_TOP
;
234 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
238 scrollEvent
= wxEVT_SCROLL_LINEUP
;
242 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
246 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
250 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
254 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
257 case SB_THUMBPOSITION
:
258 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
262 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
266 // unknown scroll event?
270 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
271 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
273 // out of range - but we did process it
279 wxScrollEvent
event(scrollEvent
, m_windowId
);
280 event
.SetPosition(newPos
);
281 event
.SetEventObject( this );
282 GetEventHandler()->ProcessEvent(event
);
284 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
285 cevent
.SetInt( newPos
);
286 cevent
.SetEventObject( this );
288 return GetEventHandler()->ProcessEvent( cevent
);
291 wxSlider95::~wxSlider95()
295 ::DestroyWindow((HWND
) m_staticMin
);
296 m_staticMin
= (WXHWND
) NULL
;
301 ::DestroyWindow((HWND
) m_staticMax
);
302 m_staticMax
= (WXHWND
) NULL
;
307 ::DestroyWindow((HWND
) m_staticValue
);
308 m_staticValue
= (WXHWND
) NULL
;
312 int wxSlider95::GetValue() const
314 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
317 void wxSlider95::SetValue(int value
)
319 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
324 str
.Printf(wxT("%d"), value
);
325 ::SetWindowText((HWND
) m_staticValue
, str
);
329 void wxSlider95::DoGetSize(int *width
, int *height
) const
331 GetSize(width
, height
);
334 void wxSlider95::GetSize(int *width
, int *height
) const
337 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
339 wxFindMaxSize(GetHWND(), &rect
);
342 wxFindMaxSize(m_staticMin
, &rect
);
345 wxFindMaxSize(m_staticMax
, &rect
);
348 wxFindMaxSize(m_staticValue
, &rect
);
350 *width
= rect
.right
- rect
.left
;
351 *height
= rect
.bottom
- rect
.top
;
354 void wxSlider95::GetPosition(int *x
, int *y
) const
356 wxWindow
*parent
= GetParent();
358 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
360 wxFindMaxSize(GetHWND(), &rect
);
363 wxFindMaxSize(m_staticMin
, &rect
);
365 wxFindMaxSize(m_staticMax
, &rect
);
367 wxFindMaxSize(m_staticValue
, &rect
);
369 // Since we now have the absolute screen coords,
370 // if there's a parent we must subtract its top left corner
375 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
377 // We may be faking the client origin.
378 // So a window that's really at (0, 30) may appear
379 // (to wxWin apps) to be at (0, 0).
382 wxPoint
pt(GetParent()->GetClientAreaOrigin());
391 // TODO one day, make sense of all this horros and replace it with a readable
393 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
400 int currentX
, currentY
;
401 GetPosition(¤tX
, ¤tY
);
402 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
404 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
407 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
414 int cx
; // slider,min,max sizes
418 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
420 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
422 if ( m_windowStyle
& wxSL_LABELS
)
426 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
427 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
431 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
432 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
435 int new_width
= (int)(wxMax(min_len
, max_len
));
436 int valueHeight
= (int)cyf
;
438 // For some reason, under Win95, the text edit control has
439 // a lot of space before the first character
442 // The height needs to be a bit bigger under Win95 if
443 // using native 3D effects.
444 valueHeight
= (int) (valueHeight
* 1.5) ;
445 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
446 new_width
, valueHeight
, TRUE
);
447 x_offset
+= new_width
+ cx
;
450 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
451 (int) min_len
, cy
, TRUE
);
452 x_offset
+= (int)(min_len
+ cx
);
454 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
456 int slider_height
= h1
;
457 if (slider_height
< 0 )
460 // Slider must have a minimum/default length/height
461 if (slider_length
< 100)
464 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
465 slider_length
, slider_height
, TRUE
);
466 x_offset
+= slider_length
+ cx
;
468 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
469 (int) max_len
, cy
, TRUE
);
474 // If we're prepared to use the existing size, then...
479 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
490 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
495 if ( m_windowStyle
& wxSL_LABELS
)
498 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
499 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
502 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
503 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
507 int new_width
= (int)(wxMax(min_len
, max_len
));
508 int valueHeight
= (int)cyf
;
509 /*** Suggested change by George Tasker - remove this block...
511 // For some reason, under Win95, the text edit control has
512 // a lot of space before the first character
515 ... and replace with following line: */
518 // The height needs to be a bit bigger under Win95 if
519 // using native 3D effects.
520 valueHeight
= (int) (valueHeight
* 1.5) ;
522 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
523 new_width
, valueHeight
, TRUE
);
524 y_offset
+= valueHeight
;
527 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
528 (int) min_len
, cy
, TRUE
);
531 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
533 int slider_width
= w1
;
534 if (slider_width
< 0 )
537 // Slider must have a minimum/default length
538 if (slider_length
< 100)
541 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
542 slider_width
, slider_length
, TRUE
);
543 y_offset
+= slider_length
;
545 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
546 (int)max_len
, cy
, TRUE
);
551 // If we're prepared to use the existing size, then...
554 width
== -1 && height
== -1
555 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
566 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
571 void wxSlider95::SetRange(int minValue
, int maxValue
)
573 m_rangeMin
= minValue
;
574 m_rangeMax
= maxValue
;
576 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
581 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
582 ::SetWindowText((HWND
) m_staticMin
, buf
);
587 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
588 ::SetWindowText((HWND
) m_staticMax
, buf
);
592 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
593 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
595 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
598 // Otherwise, it's a static
599 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
602 // For trackbars only
603 void wxSlider95::SetTickFreq(int n
, int pos
)
606 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
609 void wxSlider95::SetPageSize(int pageSize
)
611 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
612 m_pageSize
= pageSize
;
615 int wxSlider95::GetPageSize() const
620 void wxSlider95::ClearSel()
622 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
625 void wxSlider95::ClearTicks()
627 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
630 void wxSlider95::SetLineSize(int lineSize
)
632 m_lineSize
= lineSize
;
633 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
636 int wxSlider95::GetLineSize() const
638 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
639 (WPARAM
) 0, (LPARAM
) 0 );
642 int wxSlider95::GetSelEnd() const
644 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
645 (WPARAM
) 0, (LPARAM
) 0 );
648 int wxSlider95::GetSelStart() const
650 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
651 (WPARAM
) 0, (LPARAM
) 0 );
654 void wxSlider95::SetSelection(int minPos
, int maxPos
)
656 ::SendMessage(GetHwnd(), TBM_SETSEL
,
657 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
660 void wxSlider95::SetThumbLength(int len
)
662 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
665 int wxSlider95::GetThumbLength() const
667 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
668 (WPARAM
) 0, (LPARAM
) 0 );
671 void wxSlider95::SetTick(int tickPos
)
673 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
676 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
680 hWnd
== GetStaticMin()
681 || hWnd
== GetStaticMax()
682 || hWnd
== GetEditValue()
686 void wxSlider95::Command (wxCommandEvent
& event
)
688 SetValue (event
.GetInt());
689 ProcessCommand (event
);
692 bool wxSlider95::Show(bool show
)
694 wxWindow::Show(show
);
703 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
706 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
709 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
717 #endif // wxUSE_SLIDER