1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider95.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 #include "wx/msw/slider95.h"
33 #include "wx/msw/private.h"
35 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
39 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
42 wxSlider95::wxSlider95()
54 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
55 int value
, int minValue
, int maxValue
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
63 SetValidator(validator
);
64 #endif // wxUSE_VALIDATORS
66 if (parent
) parent
->AddChild(this);
67 SetBackgroundColour(parent
->GetBackgroundColour()) ;
68 SetForegroundColour(parent
->GetForegroundColour()) ;
75 m_windowStyle
= style
;
79 m_windowId
= (int)NewControlId();
91 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
92 msStyle
|= WS_CLIPSIBLINGS
;
94 if ( m_windowStyle
& wxSL_LABELS
)
96 msStyle
|= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
99 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
101 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
103 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
104 wxGetInstance(), NULL
);
106 // Now create min static control
107 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
108 wstyle
= STATIC_FLAGS
;
109 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
110 msStyle
|= WS_CLIPSIBLINGS
;
111 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
113 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
114 wxGetInstance(), NULL
);
119 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
120 msStyle
|= WS_CLIPSIBLINGS
;
122 if (m_windowStyle
& wxSL_VERTICAL
)
123 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
125 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
127 if ( m_windowStyle
& wxSL_AUTOTICKS
)
128 msStyle
|= TBS_AUTOTICKS
;
130 if ( m_windowStyle
& wxSL_LEFT
)
132 else if ( m_windowStyle
& wxSL_RIGHT
)
133 msStyle
|= TBS_RIGHT
;
134 else if ( m_windowStyle
& wxSL_TOP
)
136 else if ( m_windowStyle
& wxSL_BOTTOM
)
137 msStyle
|= TBS_BOTTOM
;
138 else if ( m_windowStyle
& wxSL_BOTH
)
140 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
141 msStyle
|= TBS_NOTICKS
;
143 if ( m_windowStyle
& wxSL_SELRANGE
)
144 msStyle
|= TBS_ENABLESELRANGE
;
146 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
148 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
149 wxGetInstance(), NULL
);
151 m_rangeMax
= maxValue
;
152 m_rangeMin
= minValue
;
154 m_pageSize
= (int)((maxValue
-minValue
)/10);
156 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
157 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
158 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
160 m_hWnd
= (WXHWND
)scroll_bar
;
162 SubclassWin(GetHWND());
164 SetWindowText((HWND
) m_hWnd
, wxT(""));
166 SetFont(parent
->GetFont());
168 if ( m_windowStyle
& wxSL_LABELS
)
170 // Finally, create max value static item
171 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
172 wstyle
= STATIC_FLAGS
;
173 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
174 msStyle
|= WS_CLIPSIBLINGS
;
175 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
177 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
178 wxGetInstance(), NULL
);
183 if (GetFont().GetResourceHandle())
186 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
187 (WPARAM
)GetFont().GetResourceHandle(),0L);
189 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
190 (WPARAM
)GetFont().GetResourceHandle(),0L);
192 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
193 (WPARAM
)GetFont().GetResourceHandle(),0L);
198 SetSize(x
, y
, width
, height
);
204 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
205 WXWORD pos
, WXHWND control
)
207 wxEventType scrollEvent
;
211 scrollEvent
= wxEVT_SCROLL_TOP
;
215 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
219 scrollEvent
= wxEVT_SCROLL_LINEUP
;
223 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
227 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
231 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
235 case SB_THUMBPOSITION
:
236 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
240 // unknown scroll event?
244 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
245 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
247 // out of range - but we did process it
253 wxScrollEvent
event(scrollEvent
, m_windowId
);
254 event
.SetPosition(newPos
);
255 event
.SetEventObject( this );
256 GetEventHandler()->ProcessEvent(event
);
258 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
259 cevent
.SetInt( newPos
);
260 cevent
.SetEventObject( this );
262 return GetEventHandler()->ProcessEvent( cevent
);
265 wxSlider95::~wxSlider95()
268 DestroyWindow((HWND
) m_staticMin
);
270 DestroyWindow((HWND
) m_staticMax
);
272 DestroyWindow((HWND
) m_staticValue
);
275 int wxSlider95::GetValue() const
277 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
280 void wxSlider95::SetValue(int value
)
282 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
285 wxSprintf(wxBuffer
, wxT("%d"), value
);
286 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
290 void wxSlider95::GetSize(int *width
, int *height
) const
293 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
295 wxFindMaxSize(GetHWND(), &rect
);
298 wxFindMaxSize(m_staticMin
, &rect
);
300 wxFindMaxSize(m_staticMax
, &rect
);
302 wxFindMaxSize(m_staticValue
, &rect
);
304 *width
= rect
.right
- rect
.left
;
305 *height
= rect
.bottom
- rect
.top
;
308 void wxSlider95::GetPosition(int *x
, int *y
) const
310 wxWindow
*parent
= GetParent();
312 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
314 wxFindMaxSize(GetHWND(), &rect
);
317 wxFindMaxSize(m_staticMin
, &rect
);
319 wxFindMaxSize(m_staticMax
, &rect
);
321 wxFindMaxSize(m_staticValue
, &rect
);
323 // Since we now have the absolute screen coords,
324 // if there's a parent we must subtract its top left corner
329 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
331 // We may be faking the client origin.
332 // So a window that's really at (0, 30) may appear
333 // (to wxWin apps) to be at (0, 0).
336 wxPoint
pt(GetParent()->GetClientAreaOrigin());
344 // TODO one day, make sense of all this horros and replace it with a readable
346 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
353 int currentX
, currentY
;
354 GetPosition(¤tX
, ¤tY
);
355 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
357 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
360 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
367 int cx
; // slider,min,max sizes
371 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
373 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
375 if ( m_windowStyle
& wxSL_LABELS
)
379 GetWindowText((HWND
) m_staticMin
, buf
, 300);
380 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
384 GetWindowText((HWND
) m_staticMax
, buf
, 300);
385 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
388 int new_width
= (int)(wxMax(min_len
, max_len
));
389 int valueHeight
= (int)cyf
;
391 // For some reason, under Win95, the text edit control has
392 // a lot of space before the first character
395 // The height needs to be a bit bigger under Win95 if using native
397 valueHeight
= (int) (valueHeight
* 1.5) ;
398 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
399 x_offset
+= new_width
+ cx
;
402 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
403 x_offset
+= (int)(min_len
+ cx
);
405 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
407 int slider_height
= h1
;
408 if (slider_height
< 0 )
411 // Slider must have a minimum/default length/height
412 if (slider_length
< 100)
415 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
416 x_offset
+= slider_length
+ cx
;
418 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
423 // If we're prepared to use the existing size, then...
424 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
432 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
437 if ( m_windowStyle
& wxSL_LABELS
)
440 GetWindowText((HWND
) m_staticMin
, buf
, 300);
441 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
444 GetWindowText((HWND
) m_staticMax
, buf
, 300);
445 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
449 int new_width
= (int)(wxMax(min_len
, max_len
));
450 int valueHeight
= (int)cyf
;
451 /*** Suggested change by George Tasker - remove this block...
453 // For some reason, under Win95, the text edit control has
454 // a lot of space before the first character
457 ... and replace with following line: */
460 // The height needs to be a bit bigger under Win95 if using native
462 valueHeight
= (int) (valueHeight
* 1.5) ;
464 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
465 y_offset
+= valueHeight
;
468 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
471 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
473 int slider_width
= w1
;
474 if (slider_width
< 0 )
477 // Slider must have a minimum/default length
478 if (slider_length
< 100)
481 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
482 y_offset
+= slider_length
;
484 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
489 // If we're prepared to use the existing size, then...
490 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
498 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
503 void wxSlider95::SetRange(int minValue
, int maxValue
)
505 m_rangeMin
= minValue
;
506 m_rangeMax
= maxValue
;
508 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
513 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
514 SetWindowText((HWND
) m_staticMin
, buf
);
519 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
520 SetWindowText((HWND
) m_staticMax
, buf
);
524 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
525 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
527 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
530 // Otherwise, it's a static
531 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
534 // For trackbars only
535 void wxSlider95::SetTickFreq(int n
, int pos
)
538 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
541 void wxSlider95::SetPageSize(int pageSize
)
543 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
544 m_pageSize
= pageSize
;
547 int wxSlider95::GetPageSize() const
552 void wxSlider95::ClearSel()
554 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
557 void wxSlider95::ClearTicks()
559 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
562 void wxSlider95::SetLineSize(int lineSize
)
564 m_lineSize
= lineSize
;
565 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
568 int wxSlider95::GetLineSize() const
570 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
573 int wxSlider95::GetSelEnd() const
575 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
578 int wxSlider95::GetSelStart() const
580 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
583 void wxSlider95::SetSelection(int minPos
, int maxPos
)
585 ::SendMessage( GetHwnd(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
588 void wxSlider95::SetThumbLength(int len
)
590 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
593 int wxSlider95::GetThumbLength() const
595 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
598 void wxSlider95::SetTick(int tickPos
)
600 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
603 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
605 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
608 void wxSlider95::Command (wxCommandEvent
& event
)
610 SetValue (event
.GetInt());
611 ProcessCommand (event
);
614 bool wxSlider95::Show(bool show
)
616 wxWindow::Show(show
);
625 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
627 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
629 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
636 #endif // wxUSE_SLIDER