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