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