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 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
44 value wxSL_DEFAULT_VALUE
52 selmin / selmax (long)
55 wxSlider95::wxSlider95()
57 m_staticValue
= (WXHWND
) NULL
;
58 m_staticMin
= (WXHWND
) NULL
;
59 m_staticMax
= (WXHWND
) NULL
;
67 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
68 int value
, int minValue
, int maxValue
,
70 const wxSize
& size
, long style
,
71 const wxValidator
& validator
,
74 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
75 style
|= wxBORDER_NONE
;
79 SetValidator(validator
);
80 #endif // wxUSE_VALIDATORS
82 if (parent
) parent
->AddChild(this);
84 SetBackgroundColour(parent
->GetBackgroundColour()) ;
85 SetForegroundColour(parent
->GetForegroundColour()) ;
87 m_staticValue
= (WXHWND
) NULL
;;
88 m_staticMin
= (WXHWND
) NULL
;;
89 m_staticMax
= (WXHWND
) NULL
;;
92 m_windowStyle
= style
;
96 m_windowId
= (int)NewControlId();
108 if ( m_windowStyle
& wxSL_LABELS
)
110 msStyle
|= SS_CENTER
;
113 long valueStyle
= m_windowStyle
& ~wxBORDER_MASK
;
114 valueStyle
|= wxBORDER_SUNKEN
;
115 msStyle
|= MSWGetStyle(valueStyle
, & exStyle
) ;
117 m_staticValue
= (WXHWND
) CreateWindowEx
119 exStyle
, wxT("STATIC"), NULL
,
121 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
122 wxGetInstance(), NULL
125 // Now create min static control
127 minLabel
.Printf(wxT("%d"), minValue
);
128 wstyle
= STATIC_FLAGS
;
129 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
130 msStyle
|= WS_CLIPSIBLINGS
;
131 m_staticMin
= (WXHWND
) CreateWindowEx
133 0, wxT("STATIC"), minLabel
,
135 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
136 wxGetInstance(), NULL
142 msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
144 if (m_windowStyle
& wxSL_VERTICAL
)
145 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
147 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
149 if ( m_windowStyle
& wxSL_AUTOTICKS
)
150 msStyle
|= TBS_AUTOTICKS
;
152 if ( m_windowStyle
& wxSL_LEFT
)
154 else if ( m_windowStyle
& wxSL_RIGHT
)
155 msStyle
|= TBS_RIGHT
;
156 else if ( m_windowStyle
& wxSL_TOP
)
158 else if ( m_windowStyle
& wxSL_BOTTOM
)
159 msStyle
|= TBS_BOTTOM
;
160 else if ( m_windowStyle
& wxSL_BOTH
)
162 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
163 msStyle
|= TBS_NOTICKS
;
165 if ( m_windowStyle
& wxSL_SELRANGE
)
166 msStyle
|= TBS_ENABLESELRANGE
;
168 HWND scroll_bar
= CreateWindowEx
170 exStyle
, TRACKBAR_CLASS
, wxEmptyString
,
172 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
173 wxGetInstance(), NULL
176 m_rangeMax
= maxValue
;
177 m_rangeMin
= minValue
;
179 m_pageSize
= (int)((maxValue
-minValue
)/10);
181 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
182 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
183 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
185 m_hWnd
= (WXHWND
)scroll_bar
;
187 SubclassWin(GetHWND());
189 ::SetWindowText((HWND
) m_hWnd
, wxEmptyString
);
191 SetFont(parent
->GetFont());
193 if ( m_windowStyle
& wxSL_LABELS
)
195 // Finally, create max value static item
197 maxLabel
.Printf(wxT("%d"), maxValue
);
198 wstyle
= STATIC_FLAGS
;
200 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
201 msStyle
|= WS_CLIPSIBLINGS
;
203 m_staticMax
= (WXHWND
) CreateWindowEx
205 0, wxT("STATIC"), maxLabel
,
207 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
208 wxGetInstance(), NULL
214 if (GetFont().GetResourceHandle())
217 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
218 (WPARAM
) GetFont().GetResourceHandle(), 0L);
221 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
222 (WPARAM
) GetFont().GetResourceHandle(), 0L);
225 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
226 (WPARAM
) GetFont().GetResourceHandle(), 0L);
231 SetSize(x
, y
, width
, height
);
237 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
238 WXWORD
WXUNUSED(pos
), WXHWND control
)
240 wxEventType scrollEvent
;
244 scrollEvent
= wxEVT_SCROLL_TOP
;
248 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
252 scrollEvent
= wxEVT_SCROLL_LINEUP
;
256 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
260 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
264 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
268 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
271 case SB_THUMBPOSITION
:
272 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
276 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
280 // unknown scroll event?
284 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
285 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
287 // out of range - but we did process it
293 wxScrollEvent
event(scrollEvent
, m_windowId
);
294 event
.SetPosition(newPos
);
295 event
.SetEventObject( this );
296 GetEventHandler()->ProcessEvent(event
);
298 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
299 cevent
.SetInt( newPos
);
300 cevent
.SetEventObject( this );
302 return GetEventHandler()->ProcessEvent( cevent
);
305 wxSlider95::~wxSlider95()
309 ::DestroyWindow((HWND
) m_staticMin
);
310 m_staticMin
= (WXHWND
) NULL
;
315 ::DestroyWindow((HWND
) m_staticMax
);
316 m_staticMax
= (WXHWND
) NULL
;
321 ::DestroyWindow((HWND
) m_staticValue
);
322 m_staticValue
= (WXHWND
) NULL
;
326 int wxSlider95::GetValue() const
328 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
331 void wxSlider95::SetValue(int value
)
333 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
338 str
.Printf(wxT("%d"), value
);
339 ::SetWindowText((HWND
) m_staticValue
, str
);
343 void wxSlider95::DoGetSize(int *width
, int *height
) const
345 GetSize(width
, height
);
348 void wxSlider95::GetSize(int *width
, int *height
) const
351 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
353 wxFindMaxSize(GetHWND(), &rect
);
356 wxFindMaxSize(m_staticMin
, &rect
);
359 wxFindMaxSize(m_staticMax
, &rect
);
362 wxFindMaxSize(m_staticValue
, &rect
);
364 *width
= rect
.right
- rect
.left
;
365 *height
= rect
.bottom
- rect
.top
;
368 void wxSlider95::GetPosition(int *x
, int *y
) const
370 wxWindow
*parent
= GetParent();
372 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
374 wxFindMaxSize(GetHWND(), &rect
);
377 wxFindMaxSize(m_staticMin
, &rect
);
379 wxFindMaxSize(m_staticMax
, &rect
);
381 wxFindMaxSize(m_staticValue
, &rect
);
383 // Since we now have the absolute screen coords,
384 // if there's a parent we must subtract its top left corner
389 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
391 // We may be faking the client origin.
392 // So a window that's really at (0, 30) may appear
393 // (to wxWin apps) to be at (0, 0).
396 wxPoint
pt(GetParent()->GetClientAreaOrigin());
405 // TODO one day, make sense of all this horros and replace it with a readable
407 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
414 int currentX
, currentY
;
415 GetPosition(¤tX
, ¤tY
);
416 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
418 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
421 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
428 int cx
; // slider,min,max sizes
432 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
434 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
436 if ( m_windowStyle
& wxSL_LABELS
)
440 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
441 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
445 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
446 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
449 int new_width
= (int)(wxMax(min_len
, max_len
));
450 int valueHeight
= (int)cyf
;
452 // For some reason, under Win95, the text edit control has
453 // a lot of space before the first character
456 // The height needs to be a bit bigger under Win95 if
457 // using native 3D effects.
458 valueHeight
= (int) (valueHeight
* 1.5) ;
459 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
460 new_width
, valueHeight
, TRUE
);
461 x_offset
+= new_width
+ cx
;
464 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
465 (int) min_len
, cy
, TRUE
);
466 x_offset
+= (int)(min_len
+ cx
);
468 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
470 int slider_height
= h1
;
471 if (slider_height
< 0 )
474 // Slider must have a minimum/default length/height
475 if (slider_length
< 100)
478 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
479 slider_length
, slider_height
, TRUE
);
480 x_offset
+= slider_length
+ cx
;
482 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
483 (int) max_len
, cy
, TRUE
);
488 // If we're prepared to use the existing size, then...
493 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
504 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
509 if ( m_windowStyle
& wxSL_LABELS
)
512 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
513 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
516 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
517 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
521 int new_width
= (int)(wxMax(min_len
, max_len
));
522 int valueHeight
= (int)cyf
;
523 /*** Suggested change by George Tasker - remove this block...
525 // For some reason, under Win95, the text edit control has
526 // a lot of space before the first character
529 ... and replace with following line: */
532 // The height needs to be a bit bigger under Win95 if
533 // using native 3D effects.
534 valueHeight
= (int) (valueHeight
* 1.5) ;
536 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
537 new_width
, valueHeight
, TRUE
);
538 y_offset
+= valueHeight
;
541 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
542 (int) min_len
, cy
, TRUE
);
545 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
547 int slider_width
= w1
;
548 if (slider_width
< 0 )
551 // Slider must have a minimum/default length
552 if (slider_length
< 100)
555 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
556 slider_width
, slider_length
, TRUE
);
557 y_offset
+= slider_length
;
559 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
560 (int)max_len
, cy
, TRUE
);
565 // If we're prepared to use the existing size, then...
568 width
== -1 && height
== -1
569 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
580 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
585 void wxSlider95::SetRange(int minValue
, int maxValue
)
587 m_rangeMin
= minValue
;
588 m_rangeMax
= maxValue
;
590 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
595 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
596 ::SetWindowText((HWND
) m_staticMin
, buf
);
601 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
602 ::SetWindowText((HWND
) m_staticMax
, buf
);
606 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
607 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
610 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
613 if ( nCtlColor
!= CTLCOLOR_STATIC
)
616 // Otherwise, it's a static
617 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
620 // For trackbars only
621 void wxSlider95::SetTickFreq(int n
, int pos
)
624 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
627 void wxSlider95::SetPageSize(int pageSize
)
629 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
630 m_pageSize
= pageSize
;
633 int wxSlider95::GetPageSize() const
638 void wxSlider95::ClearSel()
640 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
643 void wxSlider95::ClearTicks()
645 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
648 void wxSlider95::SetLineSize(int lineSize
)
650 m_lineSize
= lineSize
;
651 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
654 int wxSlider95::GetLineSize() const
656 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
657 (WPARAM
) 0, (LPARAM
) 0 );
660 int wxSlider95::GetSelEnd() const
662 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
663 (WPARAM
) 0, (LPARAM
) 0 );
666 int wxSlider95::GetSelStart() const
668 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
669 (WPARAM
) 0, (LPARAM
) 0 );
672 void wxSlider95::SetSelection(int minPos
, int maxPos
)
674 ::SendMessage(GetHwnd(), TBM_SETSEL
,
675 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
678 void wxSlider95::SetThumbLength(int len
)
680 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
683 int wxSlider95::GetThumbLength() const
685 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
686 (WPARAM
) 0, (LPARAM
) 0 );
689 void wxSlider95::SetTick(int tickPos
)
691 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
694 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
698 hWnd
== GetStaticMin()
699 || hWnd
== GetStaticMax()
700 || hWnd
== GetEditValue()
704 void wxSlider95::Command (wxCommandEvent
& event
)
706 SetValue (event
.GetInt());
707 ProcessCommand (event
);
710 bool wxSlider95::Show(bool show
)
712 wxWindow::Show(show
);
721 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
724 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
727 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
735 #endif // wxUSE_SLIDER