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 BEGIN_EVENT_TABLE(wxSlider95
, wxControl
)
40 #if WXWIN_COMPATIBILITY
41 EVT_SCROLL(wxSlider95::OnScroll
)
48 wxSlider95::wxSlider95(void)
60 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
61 int value
, int minValue
, int maxValue
,
63 const wxSize
& size
, long style
,
64 const wxValidator
& validator
,
68 SetValidator(validator
);
70 if (parent
) parent
->AddChild(this);
71 SetBackgroundColour(parent
->GetBackgroundColour()) ;
72 SetForegroundColour(parent
->GetForegroundColour()) ;
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 SetWindowText((HWND
) m_hWnd
, "");
159 SetFont(parent
->GetFont());
161 if ( m_windowStyle
& wxSL_LABELS
)
163 // Finally, create max value static item
164 sprintf(wxBuffer
, "%d", maxValue
);
165 m_staticMax
= (WXHWND
) CreateWindowEx(0, "STATIC", wxBuffer
,
167 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
168 wxGetInstance(), NULL
);
173 if (GetFont().GetResourceHandle())
176 SendMessage((HWND
)m_staticMin
,WM_SETFONT
,
177 (WPARAM
)GetFont().GetResourceHandle(),0L);
179 SendMessage((HWND
)m_staticMax
,WM_SETFONT
,
180 (WPARAM
)GetFont().GetResourceHandle(),0L);
182 SendMessage((HWND
)m_staticValue
,WM_SETFONT
,
183 (WPARAM
)GetFont().GetResourceHandle(),0L);
188 SetSize(x
, y
, width
, height
);
194 void wxSlider95::MSWOnVScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
196 int position
= 0; // Dummy - not used in this mode
199 wxEventType scrollEvent
= wxEVT_NULL
;
203 nScrollInc
= m_rangeMax
- position
;
204 scrollEvent
= wxEVT_SCROLL_TOP
;
208 nScrollInc
= - position
;
209 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
213 nScrollInc
= - GetLineSize();
214 scrollEvent
= wxEVT_SCROLL_LINEUP
;
218 nScrollInc
= GetLineSize();
219 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
223 nScrollInc
= -GetPageSize();
224 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
228 nScrollInc
= GetPageSize();
229 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
233 case SB_THUMBPOSITION
:
235 nScrollInc
= (signed short)pos
- position
;
237 nScrollInc
= pos
- position
;
239 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
249 int newPos
= (int)::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
250 if (!(newPos
< GetMin() || newPos
> GetMax()))
254 wxScrollEvent
event(scrollEvent
, m_windowId
);
255 event
.SetPosition(newPos
);
256 event
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(event
);
262 void wxSlider95::MSWOnHScroll(WXWORD wParam
, WXWORD pos
, WXHWND control
)
264 MSWOnVScroll(wParam
, pos
, control
);
267 wxSlider95::~wxSlider95(void)
270 DestroyWindow((HWND
) m_staticMin
);
272 DestroyWindow((HWND
) m_staticMax
);
274 DestroyWindow((HWND
) m_staticValue
);
277 int wxSlider95::GetValue(void) const
279 return ::SendMessage((HWND
) GetHWND(), TBM_GETPOS
, 0, 0);
282 void wxSlider95::SetValue(int value
)
284 ::SendMessage((HWND
) GetHWND(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
287 sprintf(wxBuffer
, "%d", value
);
288 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
292 void wxSlider95::GetSize(int *width
, int *height
) const
295 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
297 wxFindMaxSize(GetHWND(), &rect
);
300 wxFindMaxSize(m_staticMin
, &rect
);
302 wxFindMaxSize(m_staticMax
, &rect
);
304 wxFindMaxSize(m_staticValue
, &rect
);
306 *width
= rect
.right
- rect
.left
;
307 *height
= rect
.bottom
- rect
.top
;
310 void wxSlider95::GetPosition(int *x
, int *y
) const
312 wxWindow
*parent
= GetParent();
314 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
316 wxFindMaxSize(GetHWND(), &rect
);
319 wxFindMaxSize(m_staticMin
, &rect
);
321 wxFindMaxSize(m_staticMax
, &rect
);
323 wxFindMaxSize(m_staticValue
, &rect
);
325 // Since we now have the absolute screen coords,
326 // if there's a parent we must subtract its top left corner
331 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
333 // We may be faking the client origin.
334 // So a window that's really at (0, 30) may appear
335 // (to wxWin apps) to be at (0, 0).
338 wxPoint
pt(GetParent()->GetClientAreaOrigin());
346 void wxSlider95::SetSize(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
, & 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
, & GetFont());
384 GetWindowText((HWND
) m_staticMax
, buf
, 300);
385 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & 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((HWND
) 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((HWND
) 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
, & GetFont());
444 GetWindowText((HWND
) m_staticMax
, buf
, 300);
445 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & 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((HWND
) 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((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
503 void wxSlider95::SetRange(int minValue
, int maxValue
)
505 m_rangeMin
= minValue
;
506 m_rangeMax
= maxValue
;
508 ::SendMessage((HWND
) GetHWND(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
513 sprintf(buf
, "%d", m_rangeMin
);
514 SetWindowText((HWND
) m_staticMin
, buf
);
519 sprintf(buf
, "%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 if (GetParent()->GetTransparentBackground())
532 SetBkMode((HDC
) pDC
, TRANSPARENT
);
534 SetBkMode((HDC
) pDC
, OPAQUE
);
536 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
537 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
539 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
540 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
543 // For trackbars only
544 void wxSlider95::SetTickFreq(int n
, int pos
)
547 ::SendMessage( (HWND
) GetHWND(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
550 void wxSlider95::SetPageSize(int pageSize
)
552 ::SendMessage( (HWND
) GetHWND(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
553 m_pageSize
= pageSize
;
556 int wxSlider95::GetPageSize(void) const
561 void wxSlider95::ClearSel(void)
563 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
566 void wxSlider95::ClearTicks(void)
568 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
571 void wxSlider95::SetLineSize(int lineSize
)
573 m_lineSize
= lineSize
;
574 ::SendMessage( (HWND
) GetHWND(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
577 int wxSlider95::GetLineSize(void) const
579 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
582 int wxSlider95::GetSelEnd(void) const
584 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
587 int wxSlider95::GetSelStart(void) const
589 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
592 void wxSlider95::SetSelection(int minPos
, int maxPos
)
594 ::SendMessage( (HWND
) GetHWND(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
597 void wxSlider95::SetThumbLength(int len
)
599 ::SendMessage( (HWND
) GetHWND(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
602 int wxSlider95::GetThumbLength(void) const
604 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
607 void wxSlider95::SetTick(int tickPos
)
609 ::SendMessage( (HWND
) GetHWND(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
612 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
614 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
617 #if WXWIN_COMPATIBILITY
618 // Backward compatibility
619 void wxSlider95::OnScroll(wxScrollEvent
& event
)
621 wxEventType oldEvent
= event
.GetEventType();
622 event
.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED
);
623 if ( !GetEventHandler()->ProcessEvent(event
) )
625 event
.SetEventType( oldEvent
);
626 if (!GetParent()->GetEventHandler()->ProcessEvent(event
))
632 void wxSlider95::Command (wxCommandEvent
& event
)
634 SetValue (event
.GetInt());
635 ProcessCommand (event
);
638 bool wxSlider95::Show(bool show
)
640 wxWindow::Show(show
);
649 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
651 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
653 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);