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"
31 #include "wx/msw/slider95.h"
32 #include "wx/msw/private.h"
34 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
38 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
41 wxSlider95::wxSlider95()
53 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
54 int value
, int minValue
, int maxValue
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
62 SetValidator(validator
);
63 #endif // wxUSE_VALIDATORS
65 if (parent
) parent
->AddChild(this);
66 SetBackgroundColour(parent
->GetBackgroundColour()) ;
67 SetForegroundColour(parent
->GetForegroundColour()) ;
74 m_windowStyle
= style
;
78 m_windowId
= (int)NewControlId();
89 if ( m_windowStyle
& wxSL_LABELS
)
91 msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
94 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
96 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, wxT("STATIC"), NULL
,
98 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
99 wxGetInstance(), NULL
);
101 // Now create min static control
102 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
103 m_staticMin
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
105 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
106 wxGetInstance(), NULL
);
110 if (m_windowStyle
& wxSL_VERTICAL
)
111 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
113 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
115 if ( m_windowStyle
& wxSL_AUTOTICKS
)
116 msStyle
|= TBS_AUTOTICKS
;
118 if ( m_windowStyle
& wxSL_LEFT
)
120 else if ( m_windowStyle
& wxSL_RIGHT
)
121 msStyle
|= TBS_RIGHT
;
122 else if ( m_windowStyle
& wxSL_TOP
)
124 else if ( m_windowStyle
& wxSL_BOTTOM
)
125 msStyle
|= TBS_BOTTOM
;
126 else if ( m_windowStyle
& wxSL_BOTH
)
128 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
129 msStyle
|= TBS_NOTICKS
;
131 if ( m_windowStyle
& wxSL_SELRANGE
)
132 msStyle
|= TBS_ENABLESELRANGE
;
134 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
136 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
137 wxGetInstance(), NULL
);
139 m_rangeMax
= maxValue
;
140 m_rangeMin
= minValue
;
142 m_pageSize
= (int)((maxValue
-minValue
)/10);
144 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
145 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
146 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
148 m_hWnd
= (WXHWND
)scroll_bar
;
150 SubclassWin(GetHWND());
152 SetWindowText((HWND
) m_hWnd
, wxT(""));
154 SetFont(parent
->GetFont());
156 if ( m_windowStyle
& wxSL_LABELS
)
158 // Finally, create max value static item
159 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
160 m_staticMax
= (WXHWND
) CreateWindowEx(0, wxT("STATIC"), wxBuffer
,
162 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
163 wxGetInstance(), NULL
);
168 if (GetFont().GetResourceHandle())
171 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
172 (WPARAM
)GetFont().GetResourceHandle(),0L);
174 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
175 (WPARAM
)GetFont().GetResourceHandle(),0L);
177 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
178 (WPARAM
)GetFont().GetResourceHandle(),0L);
183 SetSize(x
, y
, width
, height
);
189 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
190 WXWORD pos
, WXHWND control
)
192 int position
= 0; // Dummy - not used in this mode
195 wxEventType scrollEvent
= wxEVT_NULL
;
199 nScrollInc
= m_rangeMax
- position
;
200 scrollEvent
= wxEVT_SCROLL_TOP
;
204 nScrollInc
= - position
;
205 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
209 nScrollInc
= - GetLineSize();
210 scrollEvent
= wxEVT_SCROLL_LINEUP
;
214 nScrollInc
= GetLineSize();
215 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
219 nScrollInc
= -GetPageSize();
220 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
224 nScrollInc
= GetPageSize();
225 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
229 case SB_THUMBPOSITION
:
231 nScrollInc
= (signed short)pos
- position
;
233 nScrollInc
= pos
- position
;
235 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
242 if (scrollEvent
== wxEVT_NULL
)
248 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
249 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
251 // out of range - but we did process it
257 wxScrollEvent
event(scrollEvent
, m_windowId
);
258 event
.SetPosition(newPos
);
259 event
.SetEventObject( this );
260 GetEventHandler()->ProcessEvent(event
);
262 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
263 cevent
.SetInt( newPos
);
264 cevent
.SetEventObject( this );
266 return GetEventHandler()->ProcessEvent( cevent
);
269 wxSlider95::~wxSlider95()
272 DestroyWindow((HWND
) m_staticMin
);
274 DestroyWindow((HWND
) m_staticMax
);
276 DestroyWindow((HWND
) m_staticValue
);
279 int wxSlider95::GetValue() const
281 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
284 void wxSlider95::SetValue(int value
)
286 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
289 wxSprintf(wxBuffer
, wxT("%d"), value
);
290 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
294 void wxSlider95::GetSize(int *width
, int *height
) const
297 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
299 wxFindMaxSize(GetHWND(), &rect
);
302 wxFindMaxSize(m_staticMin
, &rect
);
304 wxFindMaxSize(m_staticMax
, &rect
);
306 wxFindMaxSize(m_staticValue
, &rect
);
308 *width
= rect
.right
- rect
.left
;
309 *height
= rect
.bottom
- rect
.top
;
312 void wxSlider95::GetPosition(int *x
, int *y
) const
314 wxWindow
*parent
= GetParent();
316 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
318 wxFindMaxSize(GetHWND(), &rect
);
321 wxFindMaxSize(m_staticMin
, &rect
);
323 wxFindMaxSize(m_staticMax
, &rect
);
325 wxFindMaxSize(m_staticValue
, &rect
);
327 // Since we now have the absolute screen coords,
328 // if there's a parent we must subtract its top left corner
333 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
335 // We may be faking the client origin.
336 // So a window that's really at (0, 30) may appear
337 // (to wxWin apps) to be at (0, 0).
340 wxPoint
pt(GetParent()->GetClientAreaOrigin());
348 // TODO one day, make sense of all this horros and replace it with a readable
350 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
357 int currentX
, currentY
;
358 GetPosition(¤tX
, ¤tY
);
359 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
361 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
364 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
371 int cx
; // slider,min,max sizes
375 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
377 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
379 if ( m_windowStyle
& wxSL_LABELS
)
383 GetWindowText((HWND
) m_staticMin
, buf
, 300);
384 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
388 GetWindowText((HWND
) m_staticMax
, buf
, 300);
389 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
392 int new_width
= (int)(wxMax(min_len
, max_len
));
393 int valueHeight
= (int)cyf
;
395 // For some reason, under Win95, the text edit control has
396 // a lot of space before the first character
399 // The height needs to be a bit bigger under Win95 if using native
401 valueHeight
= (int) (valueHeight
* 1.5) ;
402 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
403 x_offset
+= new_width
+ cx
;
406 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
407 x_offset
+= (int)(min_len
+ cx
);
409 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
411 int slider_height
= h1
;
412 if (slider_height
< 0 )
415 // Slider must have a minimum/default length/height
416 if (slider_length
< 100)
419 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
420 x_offset
+= slider_length
+ cx
;
422 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
427 // If we're prepared to use the existing size, then...
428 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
436 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
441 if ( m_windowStyle
& wxSL_LABELS
)
444 GetWindowText((HWND
) m_staticMin
, buf
, 300);
445 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
448 GetWindowText((HWND
) m_staticMax
, buf
, 300);
449 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
453 int new_width
= (int)(wxMax(min_len
, max_len
));
454 int valueHeight
= (int)cyf
;
455 /*** Suggested change by George Tasker - remove this block...
457 // For some reason, under Win95, the text edit control has
458 // a lot of space before the first character
461 ... and replace with following line: */
464 // The height needs to be a bit bigger under Win95 if using native
466 valueHeight
= (int) (valueHeight
* 1.5) ;
468 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
469 y_offset
+= valueHeight
;
472 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
475 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
477 int slider_width
= w1
;
478 if (slider_width
< 0 )
481 // Slider must have a minimum/default length
482 if (slider_length
< 100)
485 MoveWindow(GetHwnd(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
486 y_offset
+= slider_length
;
488 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
493 // If we're prepared to use the existing size, then...
494 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
502 MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
507 void wxSlider95::SetRange(int minValue
, int maxValue
)
509 m_rangeMin
= minValue
;
510 m_rangeMax
= maxValue
;
512 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
517 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
518 SetWindowText((HWND
) m_staticMin
, buf
);
523 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
524 SetWindowText((HWND
) m_staticMax
, buf
);
528 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
529 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
531 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
534 // Otherwise, it's a static
535 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
538 // For trackbars only
539 void wxSlider95::SetTickFreq(int n
, int pos
)
542 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
545 void wxSlider95::SetPageSize(int pageSize
)
547 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
548 m_pageSize
= pageSize
;
551 int wxSlider95::GetPageSize() const
556 void wxSlider95::ClearSel()
558 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
561 void wxSlider95::ClearTicks()
563 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
566 void wxSlider95::SetLineSize(int lineSize
)
568 m_lineSize
= lineSize
;
569 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
572 int wxSlider95::GetLineSize() const
574 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
577 int wxSlider95::GetSelEnd() const
579 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
582 int wxSlider95::GetSelStart() const
584 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
587 void wxSlider95::SetSelection(int minPos
, int maxPos
)
589 ::SendMessage( GetHwnd(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
592 void wxSlider95::SetThumbLength(int len
)
594 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
597 int wxSlider95::GetThumbLength() const
599 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
602 void wxSlider95::SetTick(int tickPos
)
604 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
607 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
609 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
612 void wxSlider95::Command (wxCommandEvent
& event
)
614 SetValue (event
.GetInt());
615 ProcessCommand (event
);
618 bool wxSlider95::Show(bool show
)
620 wxWindow::Show(show
);
629 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
631 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
633 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);