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__)
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
41 BEGIN_EVENT_TABLE(wxSlider95
, wxControl
)
42 #if WXWIN_COMPATIBILITY
43 EVT_SCROLL(wxSlider95::OnScroll
)
50 wxSlider95::wxSlider95(void)
62 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
63 int value
, int minValue
, int maxValue
,
65 const wxSize
& size
, long style
,
66 const wxValidator
& validator
,
70 SetValidator(validator
);
72 if (parent
) parent
->AddChild(this);
73 SetBackgroundColour(parent
->GetBackgroundColour()) ;
74 SetForegroundColour(parent
->GetForegroundColour()) ;
81 m_windowStyle
= style
;
85 m_windowId
= (int)NewControlId();
96 if ( m_windowStyle
& wxSL_LABELS
)
98 msStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
101 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
103 m_staticValue
= (WXHWND
) CreateWindowEx(exStyle
, "STATIC", NULL
,
105 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
106 wxGetInstance(), NULL
);
108 // Now create min static control
109 sprintf(wxBuffer
, "%d", minValue
);
110 m_staticMin
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
112 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
113 wxGetInstance(), NULL
);
117 if (m_windowStyle
& wxSL_VERTICAL
)
118 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
120 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
122 if ( m_windowStyle
& wxSL_AUTOTICKS
)
123 msStyle
|= TBS_AUTOTICKS
;
125 if ( m_windowStyle
& wxSL_LEFT
)
127 else if ( m_windowStyle
& wxSL_RIGHT
)
128 msStyle
|= TBS_RIGHT
;
129 else if ( m_windowStyle
& wxSL_TOP
)
131 else if ( m_windowStyle
& wxSL_BOTTOM
)
132 msStyle
|= TBS_BOTTOM
;
133 else if ( m_windowStyle
& wxSL_BOTH
)
135 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
136 msStyle
|= TBS_NOTICKS
;
138 if ( m_windowStyle
& wxSL_SELRANGE
)
139 msStyle
|= TBS_ENABLESELRANGE
;
141 HWND scroll_bar
= CreateWindowEx(MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
143 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
144 wxGetInstance(), NULL
);
146 m_rangeMax
= maxValue
;
147 m_rangeMin
= minValue
;
149 m_pageSize
= (int)((maxValue
-minValue
)/10);
151 ::SendMessage(scroll_bar
, TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
152 ::SendMessage(scroll_bar
, TBM_SETPOS
, TRUE
, (LPARAM
)value
);
153 ::SendMessage(scroll_bar
, TBM_SETPAGESIZE
, 0, (LPARAM
)m_pageSize
);
155 m_hWnd
= (WXHWND
)scroll_bar
;
157 SubclassWin(GetHWND());
159 SetWindowText((HWND
) m_hWnd
, "");
161 SetFont(parent
->GetFont());
163 if ( m_windowStyle
& wxSL_LABELS
)
165 // Finally, create max value static item
166 sprintf(wxBuffer
, "%d", maxValue
);
167 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
169 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
170 wxGetInstance(), NULL
);
175 if (GetFont().GetResourceHandle())
178 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
179 (WPARAM
)GetFont().GetResourceHandle(),0L);
181 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
182 (WPARAM
)GetFont().GetResourceHandle(),0L);
184 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
185 (WPARAM
)GetFont().GetResourceHandle(),0L);
190 SetSize(x
, y
, width
, height
);
196 void wxSlider95::MSWOnVScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
198 int position
= 0; // Dummy - not used in this mode
201 wxEventType scrollEvent
= wxEVT_NULL
;
205 nScrollInc
= m_rangeMax
- position
;
206 scrollEvent
= wxEVT_SCROLL_TOP
;
210 nScrollInc
= - position
;
211 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
215 nScrollInc
= - GetLineSize();
216 scrollEvent
= wxEVT_SCROLL_LINEUP
;
220 nScrollInc
= GetLineSize();
221 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
225 nScrollInc
= -GetPageSize();
226 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
230 nScrollInc
= GetPageSize();
231 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
235 case SB_THUMBPOSITION
:
237 nScrollInc
= (signed short)pos
- position
;
239 nScrollInc
= pos
- position
;
241 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
251 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
252 if (!(newPos
< GetMin() || newPos
> GetMax()))
256 wxScrollEvent
event(scrollEvent
, m_windowId
);
257 event
.SetPosition(newPos
);
258 event
.SetEventObject( this );
259 GetEventHandler()->ProcessEvent(event
);
264 void wxSlider95::MSWOnHScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
266 MSWOnVScroll(wParam
, pos
, control
);
269 wxSlider95::~wxSlider95(void)
272 DestroyWindow((HWND
) m_staticMin
);
274 DestroyWindow((HWND
) m_staticMax
);
276 DestroyWindow((HWND
) m_staticValue
);
279 int wxSlider95::GetValue(void) const
281 return ::SendMessage((HWND
) GetHWND(), TBM_GETPOS
, 0, 0);
284 void wxSlider95::SetValue(int value
)
286 ::SendMessage((HWND
) GetHWND(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
289 sprintf(wxBuffer
, "%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 void wxSlider95::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
355 int currentX
, currentY
;
356 GetPosition(¤tX
, ¤tY
);
357 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
359 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
362 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
369 int cx
; // slider,min,max sizes
373 wxGetCharSize(GetHWND(), &cx
, &cy
, & GetFont());
375 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
377 if ( m_windowStyle
& wxSL_LABELS
)
381 GetWindowText((HWND
) m_staticMin
, buf
, 300);
382 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & GetFont());
386 GetWindowText((HWND
) m_staticMax
, buf
, 300);
387 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & GetFont());
390 int new_width
= (int)(wxMax(min_len
, max_len
));
391 int valueHeight
= (int)cyf
;
393 // For some reason, under Win95, the text edit control has
394 // a lot of space before the first character
397 // The height needs to be a bit bigger under Win95 if using native
399 valueHeight
= (int) (valueHeight
* 1.5) ;
400 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
401 x_offset
+= new_width
+ cx
;
404 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
405 x_offset
+= (int)(min_len
+ cx
);
407 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
409 int slider_height
= h1
;
410 if (slider_height
< 0 )
413 // Slider must have a minimum/default length/height
414 if (slider_length
< 100)
417 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
418 x_offset
+= slider_length
+ cx
;
420 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
425 // If we're prepared to use the existing size, then...
426 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
434 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
439 if ( m_windowStyle
& wxSL_LABELS
)
442 GetWindowText((HWND
) m_staticMin
, buf
, 300);
443 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & GetFont());
446 GetWindowText((HWND
) m_staticMax
, buf
, 300);
447 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & GetFont());
451 int new_width
= (int)(wxMax(min_len
, max_len
));
452 int valueHeight
= (int)cyf
;
453 /*** Suggested change by George Tasker - remove this block...
455 // For some reason, under Win95, the text edit control has
456 // a lot of space before the first character
459 ... and replace with following line: */
462 // The height needs to be a bit bigger under Win95 if using native
464 valueHeight
= (int) (valueHeight
* 1.5) ;
466 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
467 y_offset
+= valueHeight
;
470 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
473 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
475 int slider_width
= w1
;
476 if (slider_width
< 0 )
479 // Slider must have a minimum/default length
480 if (slider_length
< 100)
483 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
484 y_offset
+= slider_length
;
486 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
491 // If we're prepared to use the existing size, then...
492 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
500 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
505 void wxSlider95::SetRange(int minValue
, int maxValue
)
507 m_rangeMin
= minValue
;
508 m_rangeMax
= maxValue
;
510 ::SendMessage((HWND
) GetHWND(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
515 sprintf(buf
, "%d", m_rangeMin
);
516 SetWindowText((HWND
) m_staticMin
, buf
);
521 sprintf(buf
, "%d", m_rangeMax
);
522 SetWindowText((HWND
) m_staticMax
, buf
);
526 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
527 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
529 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
532 // Otherwise, it's a static
533 if (GetParent()->GetTransparentBackground())
534 SetBkMode((HDC
) pDC
, TRANSPARENT
);
536 SetBkMode((HDC
) pDC
, OPAQUE
);
538 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
539 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
541 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
542 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
545 // For trackbars only
546 void wxSlider95::SetTickFreq(int n
, int pos
)
549 ::SendMessage( (HWND
) GetHWND(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
552 void wxSlider95::SetPageSize(int pageSize
)
554 ::SendMessage( (HWND
) GetHWND(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
555 m_pageSize
= pageSize
;
558 int wxSlider95::GetPageSize(void) const
563 void wxSlider95::ClearSel(void)
565 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
568 void wxSlider95::ClearTicks(void)
570 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
573 void wxSlider95::SetLineSize(int lineSize
)
575 m_lineSize
= lineSize
;
576 ::SendMessage( (HWND
) GetHWND(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
579 int wxSlider95::GetLineSize(void) const
581 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
584 int wxSlider95::GetSelEnd(void) const
586 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
589 int wxSlider95::GetSelStart(void) const
591 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
594 void wxSlider95::SetSelection(int minPos
, int maxPos
)
596 ::SendMessage( (HWND
) GetHWND(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
599 void wxSlider95::SetThumbLength(int len
)
601 ::SendMessage( (HWND
) GetHWND(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
604 int wxSlider95::GetThumbLength(void) const
606 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
609 void wxSlider95::SetTick(int tickPos
)
611 ::SendMessage( (HWND
) GetHWND(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
614 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
616 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
619 #if WXWIN_COMPATIBILITY
620 // Backward compatibility
621 void wxSlider95::OnScroll(wxScrollEvent
& event
)
623 wxEventType oldEvent
= event
.GetEventType();
624 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
625 if ( !GetEventHandler()->ProcessEvent(event
) )
627 event
.SetEventType( oldEvent
);
628 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
634 void wxSlider95::Command (wxCommandEvent
& event
)
636 SetValue (event
.GetInt());
637 ProcessCommand (event
);
640 bool wxSlider95::Show(bool show
)
642 wxWindow::Show(show
);
651 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
653 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
655 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);