]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/slider.cpp
Output the extracted number from wxString::ToXXX() even if it returns false.
[wxWidgets.git] / src / msw / slider.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/slider.cpp
3// Purpose: wxSlider, using the Win95 (and later) trackbar control
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart 1998
9// Vadim Zeitlin 2004
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
28#if wxUSE_SLIDER
29
30#include "wx/slider.h"
31
32#ifndef WX_PRECOMP
33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/brush.h"
35#endif
36
37#include "wx/msw/subwin.h"
38
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// indices of labels in wxSlider::m_labels
44enum
45{
46 SliderLabel_Min,
47 SliderLabel_Max,
48 SliderLabel_Value,
49 SliderLabel_Last
50};
51
52// the gap between the slider and the labels, in pixels
53static const int HGAP = 5;
54
55// ----------------------------------------------------------------------------
56// XTI
57// ----------------------------------------------------------------------------
58
59#if wxUSE_EXTENDED_RTTI
60WX_DEFINE_FLAGS( wxSliderStyle )
61
62wxBEGIN_FLAGS( wxSliderStyle )
63 // new style border flags, we put them first to
64 // use them for streaming out
65 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
66 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
67 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
68 wxFLAGS_MEMBER(wxBORDER_RAISED)
69 wxFLAGS_MEMBER(wxBORDER_STATIC)
70 wxFLAGS_MEMBER(wxBORDER_NONE)
71
72 // old style border flags
73 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
74 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
75 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
76 wxFLAGS_MEMBER(wxRAISED_BORDER)
77 wxFLAGS_MEMBER(wxSTATIC_BORDER)
78 wxFLAGS_MEMBER(wxBORDER)
79
80 // standard window styles
81 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
82 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
83 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
84 wxFLAGS_MEMBER(wxWANTS_CHARS)
85 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
86 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
87 wxFLAGS_MEMBER(wxVSCROLL)
88 wxFLAGS_MEMBER(wxHSCROLL)
89
90 wxFLAGS_MEMBER(wxSL_HORIZONTAL)
91 wxFLAGS_MEMBER(wxSL_VERTICAL)
92 wxFLAGS_MEMBER(wxSL_AUTOTICKS)
93 wxFLAGS_MEMBER(wxSL_LABELS)
94 wxFLAGS_MEMBER(wxSL_LEFT)
95 wxFLAGS_MEMBER(wxSL_TOP)
96 wxFLAGS_MEMBER(wxSL_RIGHT)
97 wxFLAGS_MEMBER(wxSL_BOTTOM)
98 wxFLAGS_MEMBER(wxSL_BOTH)
99 wxFLAGS_MEMBER(wxSL_SELRANGE)
100 wxFLAGS_MEMBER(wxSL_INVERSE)
101
102wxEND_FLAGS( wxSliderStyle )
103
104IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider, wxControl,"wx/slider.h")
105
106wxBEGIN_PROPERTIES_TABLE(wxSlider)
107 wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxScrollEvent )
108 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SLIDER_UPDATED , wxCommandEvent )
109
110 wxPROPERTY( Value , int , SetValue, GetValue , 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
111 wxPROPERTY( Minimum , int , SetMin, GetMin, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
112 wxPROPERTY( Maximum , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY( PageSize , int , SetPageSize, GetLineSize, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
114 wxPROPERTY( LineSize , int , SetLineSize, GetLineSize, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
115 wxPROPERTY( ThumbLength , int , SetThumbLength, GetThumbLength, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 wxPROPERTY_FLAGS( WindowStyle , wxSliderStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
117wxEND_PROPERTIES_TABLE()
118
119wxBEGIN_HANDLERS_TABLE(wxSlider)
120wxEND_HANDLERS_TABLE()
121
122wxCONSTRUCTOR_8( wxSlider , wxWindow* , Parent , wxWindowID , Id , int , Value , int , Minimum , int , Maximum , wxPoint , Position , wxSize , Size , long , WindowStyle )
123#else
124IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
125#endif
126
127// ============================================================================
128// wxSlider implementation
129// ============================================================================
130
131// ----------------------------------------------------------------------------
132// construction
133// ----------------------------------------------------------------------------
134
135void wxSlider::Init()
136{
137 m_labels = NULL;
138
139 m_pageSize = 1;
140 m_lineSize = 1;
141 m_rangeMax = 0;
142 m_rangeMin = 0;
143 m_tickFreq = 0;
144
145 m_isDragging = false;
146}
147
148bool
149wxSlider::Create(wxWindow *parent,
150 wxWindowID id,
151 int value,
152 int minValue,
153 int maxValue,
154 const wxPoint& pos,
155 const wxSize& size,
156 long style,
157 const wxValidator& validator,
158 const wxString& name)
159{
160 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
161 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
162 // reasons we can't really change it, instead try to infer the orientation
163 // from the flags given to us here
164 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
165 {
166 case wxSL_LEFT:
167 case wxSL_RIGHT:
168 style |= wxSL_VERTICAL;
169 break;
170
171 case wxSL_TOP:
172 case wxSL_BOTTOM:
173 style |= wxSL_HORIZONTAL;
174 break;
175
176 case 0:
177 // no specific direction, do we have at least the orientation?
178 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
179 {
180 // no, choose default
181 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
182 }
183 };
184
185 wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
186 wxT("incompatible slider direction and orientation") );
187
188
189 // initialize everything
190 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
191 return false;
192
193 // ensure that we have correct values for GetLabelsSize()
194 m_rangeMin = minValue;
195 m_rangeMax = maxValue;
196
197 // create the labels first, so that our DoGetBestSize() could take them
198 // into account
199 //
200 // note that we could simply create 3 wxStaticTexts here but it could
201 // result in some observable side effects at wx level (e.g. the parent of
202 // wxSlider would have 3 more children than expected) and so we prefer not
203 // to do it like this
204 if ( m_windowStyle & wxSL_LABELS )
205 {
206 m_labels = new wxSubwindows(SliderLabel_Last);
207
208 HWND hwndParent = GetHwndOf(parent);
209 for ( size_t n = 0; n < SliderLabel_Last; n++ )
210 {
211 wxWindowIDRef lblid = NewControlId();
212
213 HWND wnd = ::CreateWindow
214 (
215 wxT("STATIC"),
216 NULL,
217 WS_CHILD | WS_VISIBLE | SS_CENTER,
218 0, 0, 0, 0,
219 hwndParent,
220 (HMENU)wxUIntToPtr(lblid.GetValue()),
221 wxGetInstance(),
222 NULL
223 );
224
225 m_labels->Set(n, wnd, lblid);
226 }
227
228 m_labels->SetFont(GetFont());
229 }
230
231 // now create the main control too
232 if ( !MSWCreateControl(TRACKBAR_CLASS, wxEmptyString, pos, size) )
233 return false;
234
235 // and initialize everything
236 SetRange(minValue, maxValue);
237 SetValue(value);
238 SetPageSize((maxValue - minValue)/10);
239
240 // we need to position the labels correctly if we have them and if
241 // SetSize() hadn't been called before (when best size was determined by
242 // MSWCreateControl()) as in this case they haven't been put in place yet
243 if ( m_labels && size.x != wxDefaultCoord && size.y != wxDefaultCoord )
244 {
245 SetSize(size);
246 }
247
248 return true;
249}
250
251WXDWORD wxSlider::MSWGetStyle(long style, WXDWORD *exstyle) const
252{
253 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
254
255 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
256 msStyle |= style & wxSL_VERTICAL ? TBS_VERT : TBS_HORZ;
257
258 if ( style & wxSL_BOTH )
259 {
260 // this fully specifies the style combined with TBS_VERT/HORZ above
261 msStyle |= TBS_BOTH;
262 }
263 else // choose one direction
264 {
265 if ( style & wxSL_LEFT )
266 msStyle |= TBS_LEFT;
267 else if ( style & wxSL_RIGHT )
268 msStyle |= TBS_RIGHT;
269 else if ( style & wxSL_TOP )
270 msStyle |= TBS_TOP;
271 else if ( style & wxSL_BOTTOM )
272 msStyle |= TBS_BOTTOM;
273 }
274
275 if ( style & wxSL_AUTOTICKS )
276 msStyle |= TBS_AUTOTICKS;
277 else
278 msStyle |= TBS_NOTICKS;
279
280 if ( style & wxSL_SELRANGE )
281 msStyle |= TBS_ENABLESELRANGE;
282
283 return msStyle;
284}
285
286wxSlider::~wxSlider()
287{
288 delete m_labels;
289}
290
291// ----------------------------------------------------------------------------
292// event handling
293// ----------------------------------------------------------------------------
294
295bool wxSlider::MSWOnScroll(int WXUNUSED(orientation),
296 WXWORD wParam,
297 WXWORD WXUNUSED(pos),
298 WXHWND control)
299{
300 wxEventType scrollEvent;
301 switch ( wParam )
302 {
303 case SB_TOP:
304 scrollEvent = wxEVT_SCROLL_TOP;
305 break;
306
307 case SB_BOTTOM:
308 scrollEvent = wxEVT_SCROLL_BOTTOM;
309 break;
310
311 case SB_LINEUP:
312 scrollEvent = wxEVT_SCROLL_LINEUP;
313 break;
314
315 case SB_LINEDOWN:
316 scrollEvent = wxEVT_SCROLL_LINEDOWN;
317 break;
318
319 case SB_PAGEUP:
320 scrollEvent = wxEVT_SCROLL_PAGEUP;
321 break;
322
323 case SB_PAGEDOWN:
324 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
325 break;
326
327 case SB_THUMBTRACK:
328 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
329 m_isDragging = true;
330 break;
331
332 case SB_THUMBPOSITION:
333 if ( m_isDragging )
334 {
335 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
336 m_isDragging = false;
337 }
338 else
339 {
340 // this seems to only happen when the mouse wheel is used: in
341 // this case, as it might be unexpected to get THUMBRELEASE
342 // without preceding THUMBTRACKs, we don't generate it at all
343 // but generate CHANGED event because the control itself does
344 // not send us SB_ENDSCROLL for whatever reason when mouse
345 // wheel is used
346 scrollEvent = wxEVT_SCROLL_CHANGED;
347 }
348 break;
349
350 case SB_ENDSCROLL:
351 scrollEvent = wxEVT_SCROLL_CHANGED;
352 break;
353
354 default:
355 // unknown scroll event?
356 return false;
357 }
358
359 int newPos = ValueInvertOrNot((int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0));
360 if ( (newPos < GetMin()) || (newPos > GetMax()) )
361 {
362 // out of range - but we did process it
363 return true;
364 }
365
366 SetValue(newPos);
367
368 wxScrollEvent event(scrollEvent, m_windowId);
369 event.SetPosition(newPos);
370 event.SetEventObject( this );
371 HandleWindowEvent(event);
372
373 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
374 cevent.SetInt( newPos );
375 cevent.SetEventObject( this );
376
377 return HandleWindowEvent( cevent );
378}
379
380void wxSlider::Command (wxCommandEvent & event)
381{
382 SetValue (event.GetInt());
383 ProcessCommand (event);
384}
385
386// ----------------------------------------------------------------------------
387// geometry stuff
388// ----------------------------------------------------------------------------
389
390wxRect wxSlider::GetBoundingBox() const
391{
392 // take care not to call our own functions which would call us recursively
393 int x, y, w, h;
394 wxSliderBase::DoGetPosition(&x, &y);
395 wxSliderBase::DoGetSize(&w, &h);
396
397 wxRect rect(x, y, w, h);
398 if ( m_labels )
399 {
400 wxRect lrect = m_labels->GetBoundingBox();
401 GetParent()->ScreenToClient(&lrect.x, &lrect.y);
402 rect.Union(lrect);
403 }
404
405 return rect;
406}
407
408void wxSlider::DoGetSize(int *width, int *height) const
409{
410 wxRect rect = GetBoundingBox();
411
412 if ( width )
413 *width = rect.width;
414 if ( height )
415 *height = rect.height;
416}
417
418void wxSlider::DoGetPosition(int *x, int *y) const
419{
420 wxRect rect = GetBoundingBox();
421
422 if ( x )
423 *x = rect.x;
424 if ( y )
425 *y = rect.y;
426}
427
428int wxSlider::GetLabelsSize(int *width) const
429{
430 int cy;
431
432 if ( width )
433 {
434 // find the max label width
435 int wLabelMin, wLabelMax;
436 GetTextExtent(Format(m_rangeMin), &wLabelMin, &cy);
437 GetTextExtent(Format(m_rangeMax), &wLabelMax, &cy);
438
439 *width = wxMax(wLabelMin, wLabelMax);
440 }
441 else
442 {
443 cy = GetCharHeight();
444 }
445
446 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
447}
448
449void wxSlider::DoMoveWindow(int x, int y, int width, int height)
450{
451 // all complications below are because we need to position the labels,
452 // without them everything is easy
453 if ( !m_labels )
454 {
455 wxSliderBase::DoMoveWindow(x, y, width, height);
456 return;
457 }
458
459 // be careful to position the slider itself after moving the labels as
460 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
461 // would return a wrong result and wrong size would be cached internally
462 if ( HasFlag(wxSL_VERTICAL) )
463 {
464 int wLabel;
465 int hLabel = GetLabelsSize(&wLabel);
466
467 int xLabel = HasFlag(wxSL_LEFT) ? x + width - wLabel : x;
468
469 // position all labels: min at the top, value in the middle and max at
470 // the bottom
471 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
472 xLabel, y, wLabel, hLabel);
473
474 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
475 xLabel, y + (height - hLabel)/2, wLabel, hLabel);
476
477 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
478 xLabel, y + height - hLabel, wLabel, hLabel);
479
480 // position the slider itself along the left/right edge
481 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT) ? x : x + wLabel + HGAP,
482 y + hLabel/2,
483 width - wLabel - HGAP,
484 height - hLabel);
485 }
486 else // horizontal
487 {
488 int wLabel;
489 int hLabel = GetLabelsSize(&wLabel);
490
491 int yLabel = HasFlag(wxSL_TOP) ? y + height - hLabel : y;
492
493 // position all labels: min on the left, value in the middle and max to
494 // the right
495 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
496 x, yLabel, wLabel, hLabel);
497
498 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
499 x + (width - wLabel)/2, yLabel, wLabel, hLabel);
500
501 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
502 x + width - wLabel, yLabel, wLabel, hLabel);
503
504 // position the slider itself along the top/bottom edge
505 wxSliderBase::DoMoveWindow(x,
506 HasFlag(wxSL_TOP) ? y : y + hLabel,
507 width,
508 height - hLabel);
509 }
510}
511
512wxSize wxSlider::DoGetBestSize() const
513{
514 // these values are arbitrary
515 static const int length = 100;
516 static const int thumb = 24;
517 static const int ticks = 8;
518
519 int *width;
520 wxSize size;
521 if ( HasFlag(wxSL_VERTICAL) )
522 {
523 size.x = thumb;
524 size.y = length;
525 width = &size.x;
526
527 if ( m_labels )
528 {
529 int wLabel;
530 int hLabel = GetLabelsSize(&wLabel);
531
532 // account for the labels
533 size.x += HGAP + wLabel;
534
535 // labels are indented relative to the slider itself
536 size.y += hLabel;
537 }
538 }
539 else // horizontal
540 {
541 size.x = length;
542 size.y = thumb;
543 width = &size.y;
544
545 if ( m_labels )
546 {
547 // labels add extra height
548 size.y += GetLabelsSize();
549 }
550 }
551
552 // need extra space to show ticks
553 if ( HasFlag(wxSL_TICKS) )
554 {
555 *width += ticks;
556
557 // and maybe twice as much if we show them on both sides
558 if ( HasFlag(wxSL_BOTH) )
559 *width += ticks;
560 }
561
562 return size;
563}
564
565// ----------------------------------------------------------------------------
566// slider-specific methods
567// ----------------------------------------------------------------------------
568
569int wxSlider::GetValue() const
570{
571 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0));
572}
573
574void wxSlider::SetValue(int value)
575{
576 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)ValueInvertOrNot(value));
577
578 if ( m_labels )
579 {
580 ::SetWindowText((*m_labels)[SliderLabel_Value], Format(value).wx_str());
581 }
582}
583
584void wxSlider::SetRange(int minValue, int maxValue)
585{
586 m_rangeMin = minValue;
587 m_rangeMax = maxValue;
588
589 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN, TRUE, m_rangeMin);
590 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX, TRUE, m_rangeMax);
591
592 if ( m_labels )
593 {
594 ::SetWindowText((*m_labels)[SliderLabel_Min],
595 Format(ValueInvertOrNot(m_rangeMin)).wx_str());
596 ::SetWindowText((*m_labels)[SliderLabel_Max],
597 Format(ValueInvertOrNot(m_rangeMax)).wx_str());
598 }
599}
600
601void wxSlider::SetTickFreq(int n, int pos)
602{
603 m_tickFreq = n;
604 ::SendMessage( GetHwnd(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
605}
606
607void wxSlider::SetPageSize(int pageSize)
608{
609 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
610 m_pageSize = pageSize;
611}
612
613int wxSlider::GetPageSize() const
614{
615 return m_pageSize;
616}
617
618void wxSlider::ClearSel()
619{
620 ::SendMessage(GetHwnd(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0);
621}
622
623void wxSlider::ClearTicks()
624{
625 ::SendMessage(GetHwnd(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0);
626}
627
628void wxSlider::SetLineSize(int lineSize)
629{
630 m_lineSize = lineSize;
631 ::SendMessage(GetHwnd(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize);
632}
633
634int wxSlider::GetLineSize() const
635{
636 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE, 0, 0);
637}
638
639int wxSlider::GetSelEnd() const
640{
641 return (int)::SendMessage(GetHwnd(), TBM_GETSELEND, 0, 0);
642}
643
644int wxSlider::GetSelStart() const
645{
646 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART, 0, 0);
647}
648
649void wxSlider::SetSelection(int minPos, int maxPos)
650{
651 ::SendMessage(GetHwnd(), TBM_SETSEL,
652 (WPARAM) TRUE /* redraw */,
653 (LPARAM) MAKELONG( minPos, maxPos) );
654}
655
656void wxSlider::SetThumbLength(int len)
657{
658 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0);
659}
660
661int wxSlider::GetThumbLength() const
662{
663 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH, 0, 0);
664}
665
666void wxSlider::SetTick(int tickPos)
667{
668 ::SendMessage( GetHwnd(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
669}
670
671// ----------------------------------------------------------------------------
672// composite control methods
673// ----------------------------------------------------------------------------
674
675WXHWND wxSlider::GetStaticMin() const
676{
677 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Min] : NULL;
678}
679
680WXHWND wxSlider::GetStaticMax() const
681{
682 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Max] : NULL;
683}
684
685WXHWND wxSlider::GetEditValue() const
686{
687 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Value] : NULL;
688}
689
690WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider, wxSliderBase, m_labels)
691
692#endif // wxUSE_SLIDER