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