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__)) || defined(__TWIN32__)
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxSlider95
, wxControl
)
43 wxSlider95::wxSlider95()
55 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
56 int value
, int minValue
, int maxValue
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
63 SetValidator(validator
);
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
, "STATIC", NULL
,
98 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
99 wxGetInstance(), NULL
);
101 // Now create min static control
102 sprintf(wxBuffer
, "%d", minValue
);
103 m_staticMin
= (WXHWND
) CreateWindowEx(0, "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
, "");
154 SetFont(parent
->GetFont());
156 if ( m_windowStyle
& wxSL_LABELS
)
158 // Finally, create max value static item
159 sprintf(wxBuffer
, "%d", maxValue
);
160 m_staticMax
= (WXHWND
) CreateWindowEx(0, "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 ( nScrollInc
== 0 )
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
.SetEventObject( this );
265 return GetEventHandler()->ProcessEvent( cevent
);
268 wxSlider95::~wxSlider95()
271 DestroyWindow((HWND
) m_staticMin
);
273 DestroyWindow((HWND
) m_staticMax
);
275 DestroyWindow((HWND
) m_staticValue
);
278 int wxSlider95::GetValue() const
280 return ::SendMessage((HWND
) GetHWND(), TBM_GETPOS
, 0, 0);
283 void wxSlider95::SetValue(int value
)
285 ::SendMessage((HWND
) GetHWND(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
288 sprintf(wxBuffer
, "%d", value
);
289 SetWindowText((HWND
) m_staticValue
, wxBuffer
);
293 void wxSlider95::GetSize(int *width
, int *height
) const
296 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
298 wxFindMaxSize(GetHWND(), &rect
);
301 wxFindMaxSize(m_staticMin
, &rect
);
303 wxFindMaxSize(m_staticMax
, &rect
);
305 wxFindMaxSize(m_staticValue
, &rect
);
307 *width
= rect
.right
- rect
.left
;
308 *height
= rect
.bottom
- rect
.top
;
311 void wxSlider95::GetPosition(int *x
, int *y
) const
313 wxWindow
*parent
= GetParent();
315 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
317 wxFindMaxSize(GetHWND(), &rect
);
320 wxFindMaxSize(m_staticMin
, &rect
);
322 wxFindMaxSize(m_staticMax
, &rect
);
324 wxFindMaxSize(m_staticValue
, &rect
);
326 // Since we now have the absolute screen coords,
327 // if there's a parent we must subtract its top left corner
332 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
334 // We may be faking the client origin.
335 // So a window that's really at (0, 30) may appear
336 // (to wxWin apps) to be at (0, 0).
339 wxPoint
pt(GetParent()->GetClientAreaOrigin());
347 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
354 int currentX
, currentY
;
355 GetPosition(¤tX
, ¤tY
);
356 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
358 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
361 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
368 int cx
; // slider,min,max sizes
372 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
374 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
376 if ( m_windowStyle
& wxSL_LABELS
)
380 GetWindowText((HWND
) m_staticMin
, buf
, 300);
381 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
385 GetWindowText((HWND
) m_staticMax
, buf
, 300);
386 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
389 int new_width
= (int)(wxMax(min_len
, max_len
));
390 int valueHeight
= (int)cyf
;
392 // For some reason, under Win95, the text edit control has
393 // a lot of space before the first character
396 // The height needs to be a bit bigger under Win95 if using native
398 valueHeight
= (int) (valueHeight
* 1.5) ;
399 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
400 x_offset
+= new_width
+ cx
;
403 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
404 x_offset
+= (int)(min_len
+ cx
);
406 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
408 int slider_height
= h1
;
409 if (slider_height
< 0 )
412 // Slider must have a minimum/default length/height
413 if (slider_length
< 100)
416 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_length
, slider_height
, TRUE
);
417 x_offset
+= slider_length
+ cx
;
419 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
424 // If we're prepared to use the existing size, then...
425 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
433 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
438 if ( m_windowStyle
& wxSL_LABELS
)
441 GetWindowText((HWND
) m_staticMin
, buf
, 300);
442 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());
450 int new_width
= (int)(wxMax(min_len
, max_len
));
451 int valueHeight
= (int)cyf
;
452 /*** Suggested change by George Tasker - remove this block...
454 // For some reason, under Win95, the text edit control has
455 // a lot of space before the first character
458 ... and replace with following line: */
461 // The height needs to be a bit bigger under Win95 if using native
463 valueHeight
= (int) (valueHeight
* 1.5) ;
465 MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
, new_width
, valueHeight
, TRUE
);
466 y_offset
+= valueHeight
;
469 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
, (int)min_len
, cy
, TRUE
);
472 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
474 int slider_width
= w1
;
475 if (slider_width
< 0 )
478 // Slider must have a minimum/default length
479 if (slider_length
< 100)
482 MoveWindow((HWND
) GetHWND(), x_offset
, y_offset
, slider_width
, slider_length
, TRUE
);
483 y_offset
+= slider_length
;
485 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
, (int)max_len
, cy
, TRUE
);
490 // If we're prepared to use the existing size, then...
491 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
499 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
504 void wxSlider95::SetRange(int minValue
, int maxValue
)
506 m_rangeMin
= minValue
;
507 m_rangeMax
= maxValue
;
509 ::SendMessage((HWND
) GetHWND(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
514 sprintf(buf
, "%d", m_rangeMin
);
515 SetWindowText((HWND
) m_staticMin
, buf
);
520 sprintf(buf
, "%d", m_rangeMax
);
521 SetWindowText((HWND
) m_staticMax
, buf
);
525 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
526 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
528 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
531 // Otherwise, it's a static
532 if (GetParent()->GetTransparentBackground())
533 SetBkMode((HDC
) pDC
, TRANSPARENT
);
535 SetBkMode((HDC
) pDC
, OPAQUE
);
537 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
538 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
540 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
541 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
544 // For trackbars only
545 void wxSlider95::SetTickFreq(int n
, int pos
)
548 ::SendMessage( (HWND
) GetHWND(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
551 void wxSlider95::SetPageSize(int pageSize
)
553 ::SendMessage( (HWND
) GetHWND(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
554 m_pageSize
= pageSize
;
557 int wxSlider95::GetPageSize() const
562 void wxSlider95::ClearSel()
564 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
567 void wxSlider95::ClearTicks()
569 ::SendMessage( (HWND
) GetHWND(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
572 void wxSlider95::SetLineSize(int lineSize
)
574 m_lineSize
= lineSize
;
575 ::SendMessage( (HWND
) GetHWND(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
578 int wxSlider95::GetLineSize() const
580 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETLINESIZE
, (WPARAM
) 0, (LPARAM
) 0 );
583 int wxSlider95::GetSelEnd() const
585 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_SETSELEND
, (WPARAM
) 0, (LPARAM
) 0 );
588 int wxSlider95::GetSelStart() const
590 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETSELSTART
, (WPARAM
) 0, (LPARAM
) 0 );
593 void wxSlider95::SetSelection(int minPos
, int maxPos
)
595 ::SendMessage( (HWND
) GetHWND(), TBM_SETSEL
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
598 void wxSlider95::SetThumbLength(int len
)
600 ::SendMessage( (HWND
) GetHWND(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
603 int wxSlider95::GetThumbLength() const
605 return (int) ::SendMessage( (HWND
) GetHWND(), TBM_GETTHUMBLENGTH
, (WPARAM
) 0, (LPARAM
) 0 );
608 void wxSlider95::SetTick(int tickPos
)
610 ::SendMessage( (HWND
) GetHWND(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
613 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
615 return ( hWnd
== GetStaticMin() || hWnd
== GetStaticMax() || hWnd
== GetEditValue() );
618 void wxSlider95::Command (wxCommandEvent
& event
)
620 SetValue (event
.GetInt());
621 ProcessCommand (event
);
624 bool wxSlider95::Show(bool show
)
626 wxWindow::Show(show
);
635 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
637 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
639 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);