]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/slider95.cpp
Remove unused headers.
[wxWidgets.git] / src / msw / slider95.cpp
... / ...
CommitLineData
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
50enum
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
59static const int HGAP = 5;
60
61// ----------------------------------------------------------------------------
62// XTI
63// ----------------------------------------------------------------------------
64
65#if wxUSE_EXTENDED_RTTI
66WX_DEFINE_FLAGS( wxSliderStyle )
67
68wxBEGIN_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
108wxEND_FLAGS( wxSliderStyle )
109
110IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider, wxControl,"wx/scrolbar.h")
111
112wxBEGIN_PROPERTIES_TABLE(wxSlider)
113 wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , 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
123wxEND_PROPERTIES_TABLE()
124
125wxBEGIN_HANDLERS_TABLE(wxSlider)
126wxEND_HANDLERS_TABLE()
127
128wxCONSTRUCTOR_8( wxSlider , wxWindow* , Parent , wxWindowID , Id , int , Value , int , Minimum , int , Maximum , wxPoint , Position , wxSize , Size , long , WindowStyle )
129#else
130IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
131#endif
132
133// ============================================================================
134// wxSlider implementation
135// ============================================================================
136
137// ----------------------------------------------------------------------------
138// construction
139// ----------------------------------------------------------------------------
140
141void 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 m_isDragging = false;
152}
153
154bool
155wxSlider::Create(wxWindow *parent,
156 wxWindowID id,
157 int value,
158 int minValue,
159 int maxValue,
160 const wxPoint& pos,
161 const wxSize& size,
162 long style,
163 const wxValidator& validator,
164 const wxString& name)
165{
166 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
167 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
168 // reasons we can't really change it, instead try to infer the orientation
169 // from the flags given to us here
170 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
171 {
172 case wxSL_LEFT:
173 case wxSL_RIGHT:
174 style |= wxSL_VERTICAL;
175 break;
176
177 case wxSL_TOP:
178 case wxSL_BOTTOM:
179 style |= wxSL_HORIZONTAL;
180 break;
181
182 case 0:
183 // no specific direction, do we have at least the orientation?
184 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
185 {
186 // no, choose default
187 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
188 }
189 };
190
191 wxASSERT_MSG( !(style & wxSL_VERTICAL) | !(style & wxSL_HORIZONTAL),
192 _T("incompatible slider direction and orientation") );
193
194
195 // initialize everything
196 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
197 return false;
198
199 // ensure that we have correct values for GetLabelsSize()
200 m_rangeMin = minValue;
201 m_rangeMax = maxValue;
202
203 // create the labels first, so that our DoGetBestSize() could take them
204 // into account
205 //
206 // note that we could simply create 3 wxStaticTexts here but it could
207 // result in some observable side effects at wx level (e.g. the parent of
208 // wxSlider would have 3 more children than expected) and so we prefer not
209 // to do it like this
210 if ( m_windowStyle & wxSL_LABELS )
211 {
212 m_labels = new wxSubwindows(SliderLabel_Last);
213
214 HWND hwndParent = GetHwndOf(parent);
215 for ( size_t n = 0; n < SliderLabel_Last; n++ )
216 {
217 (*m_labels)[n] = ::CreateWindow
218 (
219 wxT("STATIC"),
220 NULL,
221 WS_CHILD | WS_VISIBLE | SS_CENTER,
222 0, 0, 0, 0,
223 hwndParent,
224 (HMENU)NewControlId(),
225 wxGetInstance(),
226 NULL
227 );
228 }
229
230 m_labels->SetFont(GetFont());
231 }
232
233 // now create the main control too
234 if ( !MSWCreateControl(TRACKBAR_CLASS, wxEmptyString, pos, size) )
235 return false;
236
237 // and initialize everything
238 SetRange(minValue, maxValue);
239 SetValue(value);
240 SetPageSize((maxValue - minValue)/10);
241
242 // we need to position the labels correctly if we have them and if
243 // SetSize() hadn't been called before (when best size was determined by
244 // MSWCreateControl()) as in this case they haven't been put in place yet
245 if ( m_labels && size.x != wxDefaultCoord && size.y != wxDefaultCoord )
246 {
247 SetSize(size);
248 }
249
250 return true;
251}
252
253WXDWORD wxSlider::MSWGetStyle(long style, WXDWORD *exstyle) const
254{
255 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
256
257 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
258 msStyle |= style & wxSL_VERTICAL ? TBS_VERT : TBS_HORZ;
259
260 if ( style & wxSL_BOTH )
261 {
262 // this fully specifies the style combined with TBS_VERT/HORZ above
263 msStyle |= TBS_BOTH;
264 }
265 else // choose one direction
266 {
267 if ( style & wxSL_LEFT )
268 msStyle |= TBS_LEFT;
269 else if ( style & wxSL_RIGHT )
270 msStyle |= TBS_RIGHT;
271 else if ( style & wxSL_TOP )
272 msStyle |= TBS_TOP;
273 else if ( style & wxSL_BOTTOM )
274 msStyle |= TBS_BOTTOM;
275 }
276
277 if ( style & wxSL_AUTOTICKS )
278 msStyle |= TBS_AUTOTICKS;
279 else
280 msStyle |= TBS_NOTICKS;
281
282 if ( style & wxSL_SELRANGE )
283 msStyle |= TBS_ENABLESELRANGE;
284
285 return msStyle;
286}
287
288wxSlider::~wxSlider()
289{
290 delete m_labels;
291}
292
293// ----------------------------------------------------------------------------
294// event handling
295// ----------------------------------------------------------------------------
296
297bool wxSlider::MSWOnScroll(int WXUNUSED(orientation),
298 WXWORD wParam,
299 WXWORD WXUNUSED(pos),
300 WXHWND control)
301{
302 wxEventType scrollEvent;
303 switch ( wParam )
304 {
305 case SB_TOP:
306 scrollEvent = wxEVT_SCROLL_TOP;
307 break;
308
309 case SB_BOTTOM:
310 scrollEvent = wxEVT_SCROLL_BOTTOM;
311 break;
312
313 case SB_LINEUP:
314 scrollEvent = wxEVT_SCROLL_LINEUP;
315 break;
316
317 case SB_LINEDOWN:
318 scrollEvent = wxEVT_SCROLL_LINEDOWN;
319 break;
320
321 case SB_PAGEUP:
322 scrollEvent = wxEVT_SCROLL_PAGEUP;
323 break;
324
325 case SB_PAGEDOWN:
326 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
327 break;
328
329 case SB_THUMBTRACK:
330 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
331 m_isDragging = true;
332 break;
333
334 case SB_THUMBPOSITION:
335 if ( m_isDragging )
336 {
337 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
338 m_isDragging = false;
339 }
340 else
341 {
342 // this seems to only happen when the mouse wheel is used: in
343 // this case, as it might be unexpected to get THUMBRELEASE
344 // without preceding THUMBTRACKs, we don't generate it at all
345 // but generate CHANGED event because the control itself does
346 // not send us SB_ENDSCROLL for whatever reason when mouse
347 // wheel is used
348 scrollEvent = wxEVT_SCROLL_CHANGED;
349 }
350 break;
351
352 case SB_ENDSCROLL:
353 scrollEvent = wxEVT_SCROLL_CHANGED;
354 break;
355
356 default:
357 // unknown scroll event?
358 return false;
359 }
360
361 int newPos = ValueInvertOrNot((int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0));
362 if ( (newPos < GetMin()) || (newPos > GetMax()) )
363 {
364 // out of range - but we did process it
365 return true;
366 }
367
368 SetValue(newPos);
369
370 wxScrollEvent event(scrollEvent, m_windowId);
371 event.SetPosition(newPos);
372 event.SetEventObject( this );
373 GetEventHandler()->ProcessEvent(event);
374
375 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
376 cevent.SetInt( newPos );
377 cevent.SetEventObject( this );
378
379 return GetEventHandler()->ProcessEvent( cevent );
380}
381
382void wxSlider::Command (wxCommandEvent & event)
383{
384 SetValue (event.GetInt());
385 ProcessCommand (event);
386}
387
388// ----------------------------------------------------------------------------
389// geometry stuff
390// ----------------------------------------------------------------------------
391
392wxRect wxSlider::GetBoundingBox() const
393{
394 // take care not to call our own functions which would call us recursively
395 int x, y, w, h;
396 wxSliderBase::DoGetPosition(&x, &y);
397 wxSliderBase::DoGetSize(&w, &h);
398
399 wxRect rect(x, y, w, h);
400 if ( m_labels )
401 {
402 wxRect lrect = m_labels->GetBoundingBox();
403 GetParent()->ScreenToClient(&lrect.x, &lrect.y);
404 rect.Union(lrect);
405 }
406
407 return rect;
408}
409
410void wxSlider::DoGetSize(int *width, int *height) const
411{
412 wxRect rect = GetBoundingBox();
413
414 if ( width )
415 *width = rect.width;
416 if ( height )
417 *height = rect.height;
418}
419
420void wxSlider::DoGetPosition(int *x, int *y) const
421{
422 wxRect rect = GetBoundingBox();
423
424 if ( x )
425 *x = rect.x;
426 if ( y )
427 *y = rect.y;
428}
429
430int wxSlider::GetLabelsSize(int *width) const
431{
432 int cy;
433
434 if ( width )
435 {
436 // find the max label width
437 int wLabelMin, wLabelMax;
438 GetTextExtent(Format(m_rangeMin), &wLabelMin, &cy);
439 GetTextExtent(Format(m_rangeMax), &wLabelMax, &cy);
440
441 *width = wxMax(wLabelMin, wLabelMax);
442 }
443 else
444 {
445 cy = GetCharHeight();
446 }
447
448 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
449}
450
451void wxSlider::DoMoveWindow(int x, int y, int width, int height)
452{
453 // all complications below are because we need to position the labels,
454 // without them everything is easy
455 if ( !m_labels )
456 {
457 wxSliderBase::DoMoveWindow(x, y, width, height);
458 return;
459 }
460
461 // be careful to position the slider itself after moving the labels as
462 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
463 // would return a wrong result and wrong size would be cached internally
464 if ( HasFlag(wxSL_VERTICAL) )
465 {
466 int wLabel;
467 int hLabel = GetLabelsSize(&wLabel);
468
469 int xLabel = HasFlag(wxSL_LEFT) ? x + width - wLabel : x;
470
471 // position all labels: min at the top, value in the middle and max at
472 // the bottom
473 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
474 xLabel, y, wLabel, hLabel);
475
476 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
477 xLabel, y + (height - hLabel)/2, wLabel, hLabel);
478
479 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
480 xLabel, y + height - hLabel, wLabel, hLabel);
481
482 // position the slider itself along the left/right edge
483 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT) ? x : x + wLabel + HGAP,
484 y + hLabel/2,
485 width - wLabel - HGAP,
486 height - hLabel);
487 }
488 else // horizontal
489 {
490 int wLabel;
491 int hLabel = GetLabelsSize(&wLabel);
492
493 int yLabel = HasFlag(wxSL_TOP) ? y + height - hLabel : y;
494
495 // position all labels: min on the left, value in the middle and max to
496 // the right
497 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
498 x, yLabel, wLabel, hLabel);
499
500 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
501 x + (width - wLabel)/2, yLabel, wLabel, hLabel);
502
503 DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
504 x + width - wLabel, yLabel, wLabel, hLabel);
505
506 // position the slider itself along the top/bottom edge
507 wxSliderBase::DoMoveWindow(x,
508 HasFlag(wxSL_TOP) ? y : y + hLabel,
509 width,
510 height - hLabel);
511 }
512}
513
514wxSize wxSlider::DoGetBestSize() const
515{
516 // these values are arbitrary
517 static const int length = 100;
518 static const int thumb = 24;
519 static const int ticks = 8;
520
521 int *width;
522 wxSize size;
523 if ( HasFlag(wxSL_VERTICAL) )
524 {
525 size.x = thumb;
526 size.y = length;
527 width = &size.x;
528
529 if ( m_labels )
530 {
531 int wLabel;
532 int hLabel = GetLabelsSize(&wLabel);
533
534 // account for the labels
535 size.x += HGAP + wLabel;
536
537 // labels are indented relative to the slider itself
538 size.y += hLabel;
539 }
540 }
541 else // horizontal
542 {
543 size.x = length;
544 size.y = thumb;
545 width = &size.y;
546
547 if ( m_labels )
548 {
549 // labels add extra height
550 size.y += GetLabelsSize();
551 }
552 }
553
554 // need extra space to show ticks
555 if ( HasFlag(wxSL_TICKS) )
556 {
557 *width += ticks;
558
559 // and maybe twice as much if we show them on both sides
560 if ( HasFlag(wxSL_BOTH) )
561 *width += ticks;
562 }
563
564 return size;
565}
566
567// ----------------------------------------------------------------------------
568// slider-specific methods
569// ----------------------------------------------------------------------------
570
571int wxSlider::GetValue() const
572{
573 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0));
574}
575
576void wxSlider::SetValue(int value)
577{
578 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)ValueInvertOrNot(value));
579
580 if ( m_labels )
581 {
582 ::SetWindowText((*m_labels)[SliderLabel_Value], Format(value));
583 }
584}
585
586void wxSlider::SetRange(int minValue, int maxValue)
587{
588 m_rangeMin = minValue;
589 m_rangeMax = maxValue;
590
591 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN, TRUE, m_rangeMin);
592 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX, TRUE, m_rangeMax);
593
594 if ( m_labels )
595 {
596 ::SetWindowText((*m_labels)[SliderLabel_Min], Format(ValueInvertOrNot(m_rangeMin)));
597 ::SetWindowText((*m_labels)[SliderLabel_Max], Format(ValueInvertOrNot(m_rangeMax)));
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