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"
29 #include "wx/msw/slider95.h"
30 #include "wx/msw/private.h"
32 #if defined(__WIN95__) && !defined(__GNUWIN32__)
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
39 #if WXWIN_COMPATIBILITY
40 BEGIN_EVENT_TABLE(wxSlider95
, wxControl
)
41 EVT_SCROLL(wxSlider95::OnScroll
)
48 wxSlider95::wxSlider95(void)
60 bool wxSlider95::Create(wxWindow
*parent
, const wxWindowID id
,
61 const int value
, const int minValue
, const int maxValue
,
63 const wxSize
& size
, const long style
,
64 const wxValidator
& validator
,
68 SetValidator(validator
);
70 if (parent
) parent
->AddChild(this);
71 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
72 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
79 m_windowStyle
= style
;
83 m_windowId
= (int)NewControlId();
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
, "STATIC", NULL
,
103 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
104 wxGetInstance(), NULL
);
106 // Now create min static control
107 sprintf(wxBuffer
, "%d", minValue
);
108 m_staticMin
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
110 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
111 wxGetInstance(), NULL
);
115 if (m_windowStyle
& wxSL_VERTICAL
)
116 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
118 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
120 if ( m_windowStyle
& wxSL_AUTOTICKS
)
121 msStyle
|= TBS_AUTOTICKS
;
123 if ( m_windowStyle
& wxSL_LEFT
)
125 else if ( m_windowStyle
& wxSL_RIGHT
)
126 msStyle
|= TBS_RIGHT
;
127 else if ( m_windowStyle
& wxSL_TOP
)
129 else if ( m_windowStyle
& wxSL_BOTTOM
)
130 msStyle
|= TBS_BOTTOM
;
131 else if ( m_windowStyle
& wxSL_BOTH
)
133 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
134 msStyle
|= TBS_NOTICKS
;
136 if ( m_windowStyle
& wxSL_SELRANGE
)
137 msStyle
|= TBS_ENABLESELRANGE
;
139 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
141 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
142 wxGetInstance(), NULL
);
144 m_rangeMax
= maxValue
;
145 m_rangeMin
= minValue
;
147 m_pageSize
= (int)((maxValue
-minValue
)/10);
149 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
150 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
151 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
153 m_hWnd
= (WXHWND
)scroll_bar
;
155 SubclassWin(GetHWND());
157 if ( m_windowStyle
& wxSL_LABELS
)
159 // Finally, create max value static item
160 sprintf(wxBuffer
, "%d", maxValue
);
161 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
163 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
164 wxGetInstance(), NULL
);
166 SetFont(parent
->GetFont());
170 // GetFont()->RealizeResource();
171 if (GetFont()->GetResourceHandle())
174 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
175 (WPARAM
)GetFont()->GetResourceHandle(),0L);
177 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
178 (WPARAM
)GetFont()->GetResourceHandle(),0L);
180 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
181 (WPARAM
)GetFont()->GetResourceHandle(),0L);
186 SetSize(x
, y
, width
, height
);
192 void wxSlider95::MSWOnVScroll(const WXWORD wParam
, const WXWORD pos
, const WXHWND control
)
194 int position
= 0; // Dummy - not used in this mode
197 wxEventType scrollEvent
= wxEVT_NULL
;
201 nScrollInc
= m_rangeMax
- position
;
202 scrollEvent
= wxEVT_SCROLL_TOP
;
206 nScrollInc
= - position
;
207 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
211 nScrollInc
= - GetLineSize();
212 scrollEvent
= wxEVT_SCROLL_LINEUP
;
216 nScrollInc
= GetLineSize();
217 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
221 nScrollInc
= -GetPageSize();
222 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
226 nScrollInc
= GetPageSize();
227 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
231 case SB_THUMBPOSITION
:
233 nScrollInc
= (signed short)pos
- position
;
235 nScrollInc
= pos
- position
;
237 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
247 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
248 if (!(newPos
< GetMin() || newPos
> GetMax()))
252 wxScrollEvent
event(scrollEvent
, m_windowId
);
253 event
.SetPosition(newPos
);
254 event
.SetEventObject( this );
255 GetEventHandler()->ProcessEvent(event
);
260 void wxSlider95::MSWOnHScroll(const WXWORD wParam
, const WXWORD pos
, const WXHWND control
)
262 MSWOnVScroll(wParam
, pos
, control
);
265 wxSlider95::~wxSlider95(void)
268 DestroyWindow((HWND
) m_staticMin
);
270 DestroyWindow((HWND
) m_staticMax
);
272 DestroyWindow((HWND
) m_staticValue
);
275 int wxSlider95::GetValue(void) const
277 return ::SendMessage((HWND
) GetHWND(), TBM_GETPOS
, 0, 0);
280 void wxSlider95::SetValue(const int value
)
282 ::SendMessage((HWND
) GetHWND(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
285 sprintf(wxBuffer
, "%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
);
335 void wxSlider95::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
342 int currentX
, currentY
;
343 GetPosition(¤tX
, ¤tY
);
344 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
346 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
354 int cx
; // slider,min,max sizes
358 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
360 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
362 if ( m_windowStyle
& wxSL_LABELS
)
366 GetWindowText((HWND
) m_staticMin
, buf
, 300);
367 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, GetFont());
371 GetWindowText((HWND
) m_staticMax
, buf
, 300);
372 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, GetFont());
375 int new_width
= (int)(wxMax(min_len
, max_len
));
376 int valueHeight
= (int)cyf
;
378 // For some reason, under Win95, the text edit control has
379 // a lot of space before the first character
382 // The height needs to be a bit bigger under Win95 if using native
384 valueHeight
= (int) (valueHeight
* 1.5) ;
385 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
386 x_offset
+= new_width
+ cx
;
389 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
390 x_offset
+= (int)(min_len
+ cx
);
392 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
394 int slider_height
= h1
;
395 if (slider_height
< 0 )
398 // Slider must have a minimum/default length/height
399 if (slider_length
< 100)
402 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
403 x_offset
+= slider_length
+ cx
;
405 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
414 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
419 if ( m_windowStyle
& wxSL_LABELS
)
422 GetWindowText((HWND
) m_staticMin
, buf
, 300);
423 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
,GetFont());
426 GetWindowText((HWND
) m_staticMax
, buf
, 300);
427 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, GetFont());
431 int new_width
= (int)(wxMax(min_len
, max_len
));
432 int valueHeight
= (int)cyf
;
433 /*** Suggested change by George Tasker - remove this block...
435 // For some reason, under Win95, the text edit control has
436 // a lot of space before the first character
439 ... and replace with following line: */
442 // The height needs to be a bit bigger under Win95 if using native
444 valueHeight
= (int) (valueHeight
* 1.5) ;
446 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
447 y_offset
+= valueHeight
;
450 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
453 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
455 int slider_width
= w1
;
456 if (slider_width
< 0 )
459 // Slider must have a minimum/default length
460 if (slider_length
< 100)
463 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
464 y_offset
+= slider_length
;
466 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
475 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
480 void wxSlider95::SetRange(const int minValue
, const int maxValue
)
482 m_rangeMin
= minValue
;
483 m_rangeMax
= maxValue
;
485 ::SendMessage((HWND
) GetHWND(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
490 sprintf(buf
, "%d", m_rangeMin
);
491 SetWindowText((HWND
) m_staticMin
, buf
);
496 sprintf(buf
, "%d", m_rangeMax
);
497 SetWindowText((HWND
) m_staticMax
, buf
);
501 WXHBRUSH
wxSlider95::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
502 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
504 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
507 // Otherwise, it's a static
508 if (GetParent()->GetTransparentBackground())
509 SetBkMode((HDC
) pDC
, TRANSPARENT
);
511 SetBkMode((HDC
) pDC
, OPAQUE
);
513 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
514 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
516 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
517 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
520 // For trackbars only
521 void wxSlider95::SetTickFreq(const int n
, const int pos
)
524 ::SendMessage( (HWND
) GetHWND(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
527 void wxSlider95::SetPageSize(const int pageSize
)
529 ::SendMessage( (HWND
) GetHWND(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
530 m_pageSize
= pageSize
;
533 int wxSlider95::GetPageSize(void) const
538 void wxSlider95::ClearSel(void)
540 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
543 void wxSlider95::ClearTicks(void)
545 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
548 void wxSlider95::SetLineSize(const int lineSize
)
550 m_lineSize
= lineSize
;
551 ::SendMessage( (HWND
) GetHWND(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
554 int wxSlider95::GetLineSize(void) const
556 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
559 int wxSlider95::GetSelEnd(void) const
561 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
564 int wxSlider95::GetSelStart(void) const
566 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
569 void wxSlider95::SetSelection(const int minPos
, const int maxPos
)
571 ::SendMessage( (HWND
) GetHWND(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
574 void wxSlider95::SetThumbLength(const int len
)
576 ::SendMessage( (HWND
) GetHWND(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
579 int wxSlider95::GetThumbLength(void) const
581 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
584 void wxSlider95::SetTick(const int tickPos
)
586 ::SendMessage( (HWND
) GetHWND(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
589 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
591 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
594 #if WXWIN_COMPATIBILITY
595 // Backward compatibility
596 void wxSlider95::OnScroll(wxScrollEvent
& event
)
598 wxEventType oldEvent
= event
.GetEventType();
599 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
600 if ( !GetEventHandler()->ProcessEvent(event
) )
602 event
.SetEventType( oldEvent
);
603 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
609 void wxSlider95::Command (wxCommandEvent
& event
)
611 SetValue (event
.GetInt());
612 ProcessCommand (event
);
615 bool wxSlider95::Show(const bool show
)
617 wxWindow::Show(show
);
626 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
628 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
630 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);