]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
Fix wxFileConfig for WinCE
[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 // initialize everything
165 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
166 return false;
167
168 // ensure that we have correct values for GetLabelsSize()
169 m_rangeMin = minValue;
170 m_rangeMax = maxValue;
171
172 // create the labels first, so that our DoGetBestSize() could take them
173 // into account
174 //
175 // note that we could simply create 3 wxStaticTexts here but it could
176 // result in some observable side effects at wx level (e.g. the parent of
177 // wxSlider would have 3 more children than expected) and so we prefer not
178 // to do it like this
179 if ( m_windowStyle & wxSL_LABELS )
180 {
181 m_labels = new wxSubwindows(SliderLabel_Last);
182
183 HWND hwndParent = GetHwndOf(parent);
184 for ( size_t n = 0; n < SliderLabel_Last; n++ )
185 {
186 (*m_labels)[n] = ::CreateWindow
187 (
188 wxT("STATIC"),
189 NULL,
190 WS_CHILD | WS_VISIBLE | SS_CENTER,
191 0, 0, 0, 0,
192 hwndParent,
193 (HMENU)NewControlId(),
194 wxGetInstance(),
195 NULL
196 );
197 }
198
199 m_labels->SetFont(GetFont());
200 }
201
202 // now create the main control too
203 if ( !MSWCreateControl(TRACKBAR_CLASS, wxEmptyString, pos, size) )
204 return false;
205
206 // and initialize everything
207 SetRange(minValue, maxValue);
208 SetValue(value);
209 SetPageSize((maxValue - minValue)/10);
210
211 // we need to position the labels correctly if we have them and if
212 // SetSize() hadn't been called before (when best size was determined by
213 // MSWCreateControl()) as in this case they haven't been put in place yet
214 if ( m_labels && size.x != wxDefaultCoord && size.y != wxDefaultCoord )
215 {
216 SetSize(size);
217 }
218
219 return true;
220 }
221
222 WXDWORD wxSlider::MSWGetStyle(long style, WXDWORD *exstyle) const
223 {
224 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
225
226 // TBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
227 msStyle |= style & wxSL_VERTICAL ? TBS_VERT : TBS_HORZ;
228
229 if ( style & wxSL_AUTOTICKS )
230 msStyle |= TBS_AUTOTICKS ;
231
232 // again, TBS_RIGHT is 0 but do include it for clarity
233 if ( style & wxSL_LEFT )
234 msStyle |= TBS_LEFT;
235 else if ( style & wxSL_RIGHT )
236 msStyle |= TBS_RIGHT;
237 else if ( style & wxSL_TOP )
238 msStyle |= TBS_TOP;
239 else if ( style & wxSL_BOTTOM )
240 msStyle |= TBS_BOTTOM;
241 else if ( style & wxSL_BOTH )
242 msStyle |= TBS_BOTH;
243 else if ( !(style & wxSL_AUTOTICKS) )
244 msStyle |= TBS_NOTICKS;
245
246 if ( style & wxSL_SELRANGE )
247 msStyle |= TBS_ENABLESELRANGE;
248
249 return msStyle;
250 }
251
252 wxSlider::~wxSlider()
253 {
254 delete m_labels;
255 }
256
257 // ----------------------------------------------------------------------------
258 // event handling
259 // ----------------------------------------------------------------------------
260
261 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation),
262 WXWORD wParam,
263 WXWORD WXUNUSED(pos),
264 WXHWND control)
265 {
266 wxEventType scrollEvent;
267 switch ( wParam )
268 {
269 case SB_TOP:
270 scrollEvent = wxEVT_SCROLL_TOP;
271 break;
272
273 case SB_BOTTOM:
274 scrollEvent = wxEVT_SCROLL_BOTTOM;
275 break;
276
277 case SB_LINEUP:
278 scrollEvent = wxEVT_SCROLL_LINEUP;
279 break;
280
281 case SB_LINEDOWN:
282 scrollEvent = wxEVT_SCROLL_LINEDOWN;
283 break;
284
285 case SB_PAGEUP:
286 scrollEvent = wxEVT_SCROLL_PAGEUP;
287 break;
288
289 case SB_PAGEDOWN:
290 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
291 break;
292
293 case SB_THUMBTRACK:
294 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
295 break;
296
297 case SB_THUMBPOSITION:
298 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
299 break;
300
301 case SB_ENDSCROLL:
302 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
303 break;
304
305 default:
306 // unknown scroll event?
307 return false;
308 }
309
310 int newPos = ValueInvertOrNot((int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0));
311 if ( (newPos < GetMin()) || (newPos > GetMax()) )
312 {
313 // out of range - but we did process it
314 return true;
315 }
316
317 SetValue(newPos);
318
319 wxScrollEvent event(scrollEvent, m_windowId);
320 event.SetPosition(newPos);
321 event.SetEventObject( this );
322 GetEventHandler()->ProcessEvent(event);
323
324 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
325 cevent.SetInt( newPos );
326 cevent.SetEventObject( this );
327
328 return GetEventHandler()->ProcessEvent( cevent );
329 }
330
331 void wxSlider::Command (wxCommandEvent & event)
332 {
333 SetValue (event.GetInt());
334 ProcessCommand (event);
335 }
336
337 // ----------------------------------------------------------------------------
338 // geometry stuff
339 // ----------------------------------------------------------------------------
340
341 wxRect wxSlider::GetBoundingBox() const
342 {
343 // take care not to call our own functions which would call us recursively
344 int x, y, w, h;
345 wxSliderBase::DoGetPosition(&x, &y);
346 wxSliderBase::DoGetSize(&w, &h);
347
348 wxRect rect(x, y, w, h);
349 if ( m_labels )
350 {
351 wxRect lrect = m_labels->GetBoundingBox();
352 GetParent()->ScreenToClient(&lrect.x, &lrect.y);
353 rect.Union(lrect);
354 }
355
356 return rect;
357 }
358
359 void wxSlider::DoGetSize(int *width, int *height) const
360 {
361 wxRect rect = GetBoundingBox();
362
363 if ( width )
364 *width = rect.width;
365 if ( height )
366 *height = rect.height;
367 }
368
369 void wxSlider::DoGetPosition(int *x, int *y) const
370 {
371 wxRect rect = GetBoundingBox();
372
373 if ( x )
374 *x = rect.x;
375 if ( y )
376 *y = rect.y;
377 }
378
379 int wxSlider::GetLabelsSize(int *width) const
380 {
381 int cy;
382
383 if ( width )
384 {
385 // find the max label width
386 int wLabelMin, wLabelMax;
387 GetTextExtent(Format(m_rangeMin), &wLabelMin, &cy);
388 GetTextExtent(Format(m_rangeMax), &wLabelMax, &cy);
389
390 *width = wxMax(wLabelMin, wLabelMax);
391 }
392 else
393 {
394 cy = GetCharHeight();
395 }
396
397 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
398 }
399
400 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
401 {
402 // all complications below are because we need to position the labels,
403 // without them everything is easy
404 if ( !m_labels )
405 {
406 wxSliderBase::DoMoveWindow(x, y, width, height);
407 return;
408 }
409
410 // be careful to position the slider itself after moving the labels as
411 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
412 // would return a wrong result and wrong size would be cached internally
413 if ( HasFlag(wxSL_VERTICAL) )
414 {
415 int wLabel;
416 int hLabel = GetLabelsSize(&wLabel);
417
418 int xLabel = HasFlag(wxSL_LEFT) ? x + width - wLabel : x;
419
420 // position all labels: min at the top, value in the middle and max at
421 // the bottom
422 ::MoveWindow((*m_labels)[SliderLabel_Min],
423 xLabel, y, wLabel, hLabel, TRUE);
424
425 ::MoveWindow((*m_labels)[SliderLabel_Value],
426 xLabel, y + (height - hLabel)/2, wLabel, hLabel, TRUE);
427
428 ::MoveWindow((*m_labels)[SliderLabel_Max],
429 xLabel, y + height - hLabel, wLabel, hLabel, TRUE);
430
431 // position the slider itself along the left/right edge
432 ::MoveWindow(GetHwnd(),
433 HasFlag(wxSL_LEFT) ? x : x + wLabel + HGAP,
434 y + hLabel/2,
435 width - wLabel - HGAP,
436 height - hLabel,
437 TRUE);
438 }
439 else // horizontal
440 {
441 int wLabel;
442 int hLabel = GetLabelsSize(&wLabel);
443
444 int yLabel = HasFlag(wxSL_TOP) ? y + height - hLabel : y;
445
446 // position all labels: min on the left, value in the middle and max to
447 // the right
448 ::MoveWindow((*m_labels)[SliderLabel_Min],
449 x, yLabel, wLabel, hLabel, TRUE);
450
451 ::MoveWindow((*m_labels)[SliderLabel_Value],
452 x + (width - wLabel)/2, yLabel, wLabel, hLabel, TRUE);
453
454 ::MoveWindow((*m_labels)[SliderLabel_Max],
455 x + width - wLabel, yLabel, wLabel, hLabel, TRUE);
456
457 // position the slider itself along the top/bottom edge
458 ::MoveWindow(GetHwnd(),
459 x,
460 HasFlag(wxSL_TOP) ? y : y + hLabel,
461 width,
462 height - hLabel,
463 TRUE);
464 }
465 }
466
467 wxSize wxSlider::DoGetBestSize() const
468 {
469 // these values are arbitrary
470 static const int length = 100;
471 static const int thickness = 26;
472
473 wxSize size;
474 if ( HasFlag(wxSL_VERTICAL) )
475 {
476 size.x = thickness;
477 size.y = length;
478
479 if ( m_labels )
480 {
481 int wLabel;
482 int hLabel = GetLabelsSize(&wLabel);
483
484 // account for the labels
485 size.x += HGAP + wLabel;
486
487 // labels are indented relative to the slider itself
488 size.y += hLabel;
489 }
490 }
491 else // horizontal
492 {
493 size.x = length;
494 size.y = thickness;
495
496 if ( m_labels )
497 {
498 // labels add extra height
499 size.y += GetLabelsSize();
500 }
501 }
502
503 return size;
504 }
505
506 // ----------------------------------------------------------------------------
507 // slider-specific methods
508 // ----------------------------------------------------------------------------
509
510 int wxSlider::GetValue() const
511 {
512 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0));
513 }
514
515 void wxSlider::SetValue(int value)
516 {
517 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)ValueInvertOrNot(value));
518
519 if ( m_labels )
520 {
521 ::SetWindowText((*m_labels)[SliderLabel_Value], Format(value));
522 }
523 }
524
525 void wxSlider::SetRange(int minValue, int maxValue)
526 {
527 m_rangeMin = minValue;
528 m_rangeMax = maxValue;
529
530 ::SendMessage(GetHwnd(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
531
532 if ( m_labels )
533 {
534 ::SetWindowText((*m_labels)[SliderLabel_Min], Format(ValueInvertOrNot(m_rangeMin)));
535 ::SetWindowText((*m_labels)[SliderLabel_Max], Format(ValueInvertOrNot(m_rangeMax)));
536 }
537 }
538
539 void wxSlider::SetTickFreq(int n, int pos)
540 {
541 m_tickFreq = n;
542 ::SendMessage( GetHwnd(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
543 }
544
545 void wxSlider::SetPageSize(int pageSize)
546 {
547 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
548 m_pageSize = pageSize;
549 }
550
551 int wxSlider::GetPageSize() const
552 {
553 return m_pageSize;
554 }
555
556 void wxSlider::ClearSel()
557 {
558 ::SendMessage(GetHwnd(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0);
559 }
560
561 void wxSlider::ClearTicks()
562 {
563 ::SendMessage(GetHwnd(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0);
564 }
565
566 void wxSlider::SetLineSize(int lineSize)
567 {
568 m_lineSize = lineSize;
569 ::SendMessage(GetHwnd(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize);
570 }
571
572 int wxSlider::GetLineSize() const
573 {
574 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE, 0, 0);
575 }
576
577 int wxSlider::GetSelEnd() const
578 {
579 return (int)::SendMessage(GetHwnd(), TBM_SETSELEND, 0, 0);
580 }
581
582 int wxSlider::GetSelStart() const
583 {
584 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART, 0, 0);
585 }
586
587 void wxSlider::SetSelection(int minPos, int maxPos)
588 {
589 ::SendMessage(GetHwnd(), TBM_SETSEL,
590 (WPARAM) TRUE /* redraw */,
591 (LPARAM) MAKELONG( minPos, maxPos) );
592 }
593
594 void wxSlider::SetThumbLength(int len)
595 {
596 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0);
597 }
598
599 int wxSlider::GetThumbLength() const
600 {
601 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH, 0, 0);
602 }
603
604 void wxSlider::SetTick(int tickPos)
605 {
606 ::SendMessage( GetHwnd(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
607 }
608
609 // ----------------------------------------------------------------------------
610 // composite control methods
611 // ----------------------------------------------------------------------------
612
613 WXHWND wxSlider::GetStaticMin() const
614 {
615 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Min] : NULL;
616 }
617
618 WXHWND wxSlider::GetStaticMax() const
619 {
620 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Max] : NULL;
621 }
622
623 WXHWND wxSlider::GetEditValue() const
624 {
625 return m_labels ? (WXHWND)(*m_labels)[SliderLabel_Value] : NULL;
626 }
627
628 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider, wxSliderBase, m_labels)
629
630 #endif // wxUSE_SLIDER