]> git.saurik.com Git - wxWidgets.git/blame - src/msw/slidrmsw.cpp
fixed my previous fix in wxListCtrl::SetItem
[wxWidgets.git] / src / msw / slidrmsw.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
da87a1ca
JS
2// Name: slidermsw.cpp
3// Purpose: wxSliderMSW
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
a23fd0e1 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
da87a1ca 13#pragma implementation "slidrmsw.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include <stdio.h>
2bda0e17
KB
25#endif
26
2432b92d
JS
27#include "wx/utils.h"
28#include "wx/brush.h"
da87a1ca 29#include "wx/msw/slidrmsw.h"
2bda0e17
KB
30#include "wx/msw/private.h"
31
da87a1ca 32IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl)
2bda0e17
KB
33
34// Slider
bfc6fde4 35wxSliderMSW::wxSliderMSW()
2bda0e17
KB
36{
37 m_staticValue = 0;
38 m_staticMin = 0;
39 m_staticMax = 0;
40 m_pageSize = 1;
41 m_lineSize = 1;
42 m_rangeMax = 0;
43 m_rangeMin = 0;
44 m_tickFreq = 0;
45}
46
debe6624
JS
47bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
48 int value, int minValue, int maxValue,
2bda0e17 49 const wxPoint& pos,
debe6624 50 const wxSize& size, long style,
2bda0e17
KB
51 const wxValidator& validator,
52 const wxString& name)
53{
54 SetName(name);
11b6a93b 55#if wxUSE_VALIDATORS
2bda0e17 56 SetValidator(validator);
11b6a93b 57#endif // wxUSE_VALIDATORS
2bda0e17
KB
58
59 if (parent) parent->AddChild(this);
fd71308f
JS
60 SetBackgroundColour(parent->GetBackgroundColour()) ;
61 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
62
63 m_staticValue = 0;
64 m_staticMin = 0;
65 m_staticMax = 0;
66 m_pageSize = 1;
67 m_lineSize = 1;
68 m_windowStyle = style;
69 m_tickFreq = 0;
70
71 if ( id == -1 )
a23fd0e1 72 m_windowId = (int)NewControlId();
2bda0e17 73 else
a23fd0e1 74 m_windowId = id;
2bda0e17
KB
75
76 int x = pos.x;
77 int y = pos.y;
78 int width = size.x;
79 int height = size.y;
80
2bda0e17
KB
81 // non-Win95 implementation
82
83 long msStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
84
85 bool want3D;
86 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
87
223d09f6 88 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
2bda0e17
KB
89 msStyle,
90 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
91 wxGetInstance(), NULL);
92
93 // Now create min static control
223d09f6
KB
94 wxSprintf(wxBuffer, wxT("%d"), minValue);
95 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
2bda0e17
KB
96 STATIC_FLAGS,
97 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
98 wxGetInstance(), NULL);
99
2bda0e17
KB
100 msStyle = 0;
101 if (m_windowStyle & wxSL_VERTICAL)
102 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
103 else
104 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
105
223d09f6 106 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
2bda0e17
KB
107 msStyle,
108 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
109 wxGetInstance(), NULL);
110
111 m_pageSize = (int)((maxValue-minValue)/10);
112 m_rangeMax = maxValue;
113 m_rangeMin = minValue;
114
115 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
116 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
117 ShowWindow(scroll_bar, SW_SHOW);
118
119 m_hWnd = (WXHWND)scroll_bar;
120
121 // Subclass again for purposes of dialog editing mode
122 SubclassWin(GetHWND());
123
124 // Finally, create max value static item
223d09f6
KB
125 wxSprintf(wxBuffer, wxT("%d"), maxValue);
126 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
2bda0e17
KB
127 STATIC_FLAGS,
128 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
129 wxGetInstance(), NULL);
130
c0ed460c 131 SetFont(parent->GetFont());
2bda0e17 132
c0ed460c 133 if (GetFont().Ok())
2bda0e17
KB
134 {
135// GetFont()->RealizeResource();
c0ed460c 136 if (GetFont().GetResourceHandle())
2bda0e17 137 {
a23fd0e1
VZ
138 if ( m_staticMin )
139 SendMessage((HWND)m_staticMin,WM_SETFONT,
140 (WPARAM)GetFont().GetResourceHandle(),0L);
141 if ( m_staticMax )
142 SendMessage((HWND)m_staticMax,WM_SETFONT,
c0ed460c 143 (WPARAM)GetFont().GetResourceHandle(),0L);
a23fd0e1
VZ
144 if (m_staticValue)
145 SendMessage((HWND)m_staticValue,WM_SETFONT,
c0ed460c 146 (WPARAM)GetFont().GetResourceHandle(),0L);
2bda0e17
KB
147 }
148 }
2bda0e17
KB
149
150 SetSize(x, y, width, height);
151 SetValue(value);
152
153 return TRUE;
154}
155
a23fd0e1
VZ
156bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
157 WXWORD pos, WXHWND control)
2bda0e17 158{
2bda0e17 159 int position = ::GetScrollPos((HWND)control, SB_CTL);
2bda0e17
KB
160
161 int nScrollInc;
7798a18e 162 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
163 switch ( wParam )
164 {
a23fd0e1
VZ
165 case SB_TOP:
166 nScrollInc = m_rangeMax - position;
167 scrollEvent = wxEVT_SCROLL_TOP;
168 break;
169
170 case SB_BOTTOM:
171 nScrollInc = - position;
172 scrollEvent = wxEVT_SCROLL_BOTTOM;
173 break;
174
175 case SB_LINEUP:
176 nScrollInc = - GetLineSize();
177 scrollEvent = wxEVT_SCROLL_LINEUP;
178 break;
179
180 case SB_LINEDOWN:
181 nScrollInc = GetLineSize();
182 scrollEvent = wxEVT_SCROLL_LINEDOWN;
183 break;
184
185 case SB_PAGEUP:
186 nScrollInc = -GetPageSize();
187 scrollEvent = wxEVT_SCROLL_PAGEUP;
188 break;
189
190 case SB_PAGEDOWN:
191 nScrollInc = GetPageSize();
192 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
193 break;
194
195 case SB_THUMBTRACK:
196 case SB_THUMBPOSITION:
2bda0e17 197#ifdef __WIN32__
a23fd0e1 198 nScrollInc = (signed short)pos - position;
2bda0e17 199#else
a23fd0e1 200 nScrollInc = pos - position;
2bda0e17 201#endif
a23fd0e1
VZ
202 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
203 break;
2bda0e17 204
a23fd0e1
VZ
205 default:
206 nScrollInc = 0;
2bda0e17
KB
207 }
208
a23fd0e1 209 if (nScrollInc == 0)
2bda0e17 210 {
a23fd0e1
VZ
211 // no event...
212 return FALSE;
213 }
2bda0e17 214
a23fd0e1 215 int newPos = position + nScrollInc;
da87a1ca 216
a23fd0e1
VZ
217 if ( (newPos < GetMin()) || (newPos > GetMax()) )
218 {
219 // out of range - but we did process it
220 return TRUE;
221 }
2bda0e17 222
a23fd0e1 223 SetValue(newPos);
f3a65071 224
a23fd0e1
VZ
225 wxScrollEvent event(scrollEvent, m_windowId);
226 event.SetPosition(newPos);
227 event.SetEventObject( this );
228 GetEventHandler()->ProcessEvent(event);
2bda0e17 229
a23fd0e1 230 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
f6bcfd97 231 cevent.SetInt( newPos );
a23fd0e1
VZ
232 cevent.SetEventObject( this );
233
234 return GetEventHandler()->ProcessEvent( cevent );
2bda0e17
KB
235}
236
bfc6fde4 237wxSliderMSW::~wxSliderMSW()
2bda0e17
KB
238{
239 if (m_staticMin)
240 DestroyWindow((HWND) m_staticMin);
241 if (m_staticMax)
242 DestroyWindow((HWND) m_staticMax);
243 if (m_staticValue)
244 DestroyWindow((HWND) m_staticValue);
245}
246
bfc6fde4 247int wxSliderMSW::GetValue() const
2bda0e17 248{
4438caf4 249 return ::GetScrollPos(GetHwnd(), SB_CTL);
2bda0e17
KB
250}
251
debe6624 252void wxSliderMSW::SetValue(int value)
2bda0e17 253{
4438caf4 254 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
2bda0e17
KB
255 if (m_staticValue)
256 {
223d09f6 257 wxSprintf(wxBuffer, wxT("%d"), value);
2bda0e17
KB
258 SetWindowText((HWND) m_staticValue, wxBuffer);
259 }
260}
261
da87a1ca 262void wxSliderMSW::GetSize(int *width, int *height) const
2bda0e17
KB
263{
264 RECT rect;
265 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
266
267 wxFindMaxSize(GetHWND(), &rect);
268
269 if (m_staticMin)
270 wxFindMaxSize(m_staticMin, &rect);
271 if (m_staticMax)
272 wxFindMaxSize(m_staticMax, &rect);
273 if (m_staticValue)
274 wxFindMaxSize(m_staticValue, &rect);
275
276 *width = rect.right - rect.left;
277 *height = rect.bottom - rect.top;
278}
279
da87a1ca 280void wxSliderMSW::GetPosition(int *x, int *y) const
2bda0e17
KB
281{
282 wxWindow *parent = GetParent();
283 RECT rect;
284 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
285
286 wxFindMaxSize(GetHWND(), &rect);
287
288 if (m_staticMin)
289 wxFindMaxSize(m_staticMin, &rect);
290 if (m_staticMax)
291 wxFindMaxSize(m_staticMax, &rect);
292 if (m_staticValue)
293 wxFindMaxSize(m_staticValue, &rect);
294
295 // Since we now have the absolute screen coords,
296 // if there's a parent we must subtract its top left corner
297 POINT point;
298 point.x = rect.left;
299 point.y = rect.top;
300 if (parent)
301 ::ScreenToClient((HWND) parent->GetHWND(), &point);
302
81d66cf3
JS
303 // We may be faking the client origin.
304 // So a window that's really at (0, 30) may appear
305 // (to wxWin apps) to be at (0, 0).
306 if (GetParent())
307 {
308 wxPoint pt(GetParent()->GetClientAreaOrigin());
309 point.x -= pt.x;
310 point.y -= pt.y;
311 }
2bda0e17
KB
312 *x = point.x;
313 *y = point.y;
314}
315
4438caf4
VZ
316// TODO one day, make sense of all this horros and replace it with a readable
317// DoGetBestSize()
bfc6fde4 318void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17
KB
319{
320 int x1 = x;
321 int y1 = y;
322 int w1 = width;
323 int h1 = height;
324
325 int currentX, currentY;
326 GetPosition(&currentX, &currentY);
ce96b7a0 327 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2bda0e17 328 x1 = currentX;
ce96b7a0 329 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2bda0e17
KB
330 y1 = currentY;
331
81d66cf3
JS
332 AdjustForParentClientOrigin(x1, y1, sizeFlags);
333
837e5743 334 wxChar buf[300];
2bda0e17
KB
335
336 int x_offset = x;
337 int y_offset = y;
338
339 int cx; // slider,min,max sizes
340 int cy;
debe6624 341 int cyf;
2bda0e17 342
ce3ed50d 343 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
2bda0e17
KB
344
345 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
346 {
a23fd0e1
VZ
347 if ( m_windowStyle & wxSL_LABELS )
348 {
debe6624 349 int min_len = 0;
2bda0e17
KB
350
351 GetWindowText((HWND) m_staticMin, buf, 300);
ce3ed50d 352 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17 353
debe6624 354 int max_len = 0;
2bda0e17
KB
355
356 GetWindowText((HWND) m_staticMax, buf, 300);
ce3ed50d 357 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17
KB
358 if (m_staticValue)
359 {
360 int new_width = (int)(wxMax(min_len, max_len));
a23fd0e1 361 int valueHeight = (int)cyf;
2bda0e17
KB
362#ifdef __WIN32__
363 // For some reason, under Win95, the text edit control has
364 // a lot of space before the first character
365 new_width += 3*cx;
2bda0e17
KB
366#endif
367 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
368 x_offset += new_width + cx;
369 }
370
371 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
372 x_offset += (int)(min_len + cx);
373
374 int slider_length = (int)(w1 - x_offset - max_len - cx);
375
2bda0e17 376 int slider_height = cy;
2bda0e17
KB
377
378 // Slider must have a minimum/default length/height
379 if (slider_length < 100)
380 slider_length = 100;
381
4438caf4 382 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
2bda0e17
KB
383 x_offset += slider_length + cx;
384
385 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
386 }
a23fd0e1
VZ
387 else
388 {
389 // No labels
390 if ( w1 < 0 )
391 w1 = 200;
392 if ( h1 < 0 )
393 h1 = 20;
4438caf4 394 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
a23fd0e1 395 }
2bda0e17
KB
396 }
397 else
398 {
a23fd0e1
VZ
399 if ( m_windowStyle & wxSL_LABELS )
400 {
debe6624 401 int min_len;
2bda0e17 402 GetWindowText((HWND) m_staticMin, buf, 300);
ce3ed50d 403 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
2bda0e17 404
debe6624 405 int max_len;
2bda0e17 406 GetWindowText((HWND) m_staticMax, buf, 300);
ce3ed50d 407 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17
KB
408
409 if (m_staticValue)
410 {
411 int new_width = (int)(wxMax(min_len, max_len));
a23fd0e1 412 int valueHeight = (int)cyf;
2bda0e17
KB
413/*** Suggested change by George Tasker - remove this block...
414#ifdef __WIN32__
415 // For some reason, under Win95, the text edit control has
416 // a lot of space before the first character
417 new_width += 3*cx;
418#endif
419 ... and replace with following line: */
420 new_width += cx;
421
2bda0e17
KB
422 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
423 y_offset += valueHeight;
424 }
425
426 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
427 y_offset += cy;
428
429 int slider_length = (int)(h1 - y_offset - cy - cy);
da87a1ca 430
2bda0e17
KB
431 // Use character height as an estimate of slider width (yes, width)
432 int slider_width = cy;
2bda0e17
KB
433
434 // Slider must have a minimum/default length
435 if (slider_length < 100)
436 slider_length = 100;
437
4438caf4 438 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
2bda0e17
KB
439 y_offset += slider_length;
440
441 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
442 }
a23fd0e1
VZ
443 else
444 {
445 // No labels
446 if ( w1 < 0 )
447 w1 = 20;
448 if ( h1 < 0 )
449 h1 = 200;
4438caf4 450 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
a23fd0e1 451 }
2bda0e17 452 }
2bda0e17
KB
453}
454
debe6624 455void wxSliderMSW::SetRange(int minValue, int maxValue)
2bda0e17
KB
456{
457 m_rangeMin = minValue;
458 m_rangeMax = maxValue;
459
4438caf4 460 ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
837e5743 461 wxChar buf[40];
2bda0e17
KB
462 if ( m_staticMin )
463 {
223d09f6 464 wxSprintf(buf, wxT("%d"), m_rangeMin);
a23fd0e1 465 SetWindowText((HWND) m_staticMin, buf);
2bda0e17
KB
466 }
467
468 if ( m_staticMax )
469 {
223d09f6 470 wxSprintf(buf, wxT("%d"), m_rangeMax);
2bda0e17
KB
471 SetWindowText((HWND) m_staticMax, buf);
472 }
473}
474
debe6624 475WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
a23fd0e1 476 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17
KB
477{
478 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
a23fd0e1 479 return 0;
2bda0e17
KB
480
481 // Otherwise, it's a static
f048e32f 482 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
2bda0e17
KB
483}
484
485// For trackbars only
debe6624 486void wxSliderMSW::SetTickFreq(int n, int pos)
2bda0e17 487{
2bda0e17
KB
488}
489
debe6624 490void wxSliderMSW::SetPageSize(int pageSize)
2bda0e17 491{
2bda0e17
KB
492 m_pageSize = pageSize;
493}
494
bfc6fde4 495int wxSliderMSW::GetPageSize() const
2bda0e17
KB
496{
497 return m_pageSize;
498}
499
bfc6fde4 500void wxSliderMSW::ClearSel()
2bda0e17 501{
2bda0e17
KB
502}
503
bfc6fde4 504void wxSliderMSW::ClearTicks()
2bda0e17 505{
2bda0e17
KB
506}
507
debe6624 508void wxSliderMSW::SetLineSize(int lineSize)
2bda0e17
KB
509{
510 m_lineSize = lineSize;
2bda0e17
KB
511}
512
bfc6fde4 513int wxSliderMSW::GetLineSize() const
2bda0e17 514{
2bda0e17 515 return m_lineSize;
2bda0e17
KB
516}
517
bfc6fde4 518int wxSliderMSW::GetSelEnd() const
2bda0e17 519{
2bda0e17 520 return 0;
2bda0e17
KB
521}
522
bfc6fde4 523int wxSliderMSW::GetSelStart() const
2bda0e17 524{
2bda0e17 525 return 0;
2bda0e17
KB
526}
527
debe6624 528void wxSliderMSW::SetSelection(int minPos, int maxPos)
2bda0e17 529{
2bda0e17
KB
530}
531
debe6624 532void wxSliderMSW::SetThumbLength(int len)
2bda0e17 533{
2bda0e17
KB
534}
535
bfc6fde4 536int wxSliderMSW::GetThumbLength() const
2bda0e17 537{
2bda0e17 538 return 0;
2bda0e17
KB
539}
540
debe6624 541void wxSliderMSW::SetTick(int tickPos)
2bda0e17 542{
2bda0e17
KB
543}
544
da87a1ca 545bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
2bda0e17 546{
a23fd0e1 547 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
2bda0e17
KB
548}
549
da87a1ca 550void wxSliderMSW::Command (wxCommandEvent & event)
2bda0e17
KB
551{
552 SetValue (event.GetInt());
553 ProcessCommand (event);
554}
555
debe6624 556bool wxSliderMSW::Show(bool show)
2bda0e17 557{
a23fd0e1 558 wxWindow::Show(show);
2bda0e17
KB
559
560 int cshow;
561 if (show)
562 cshow = SW_SHOW;
563 else
564 cshow = SW_HIDE;
565
566 if(m_staticValue)
a23fd0e1 567 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
2bda0e17 568 if(m_staticMin)
a23fd0e1 569 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
2bda0e17 570 if(m_staticMax)
a23fd0e1 571 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
2bda0e17
KB
572 return TRUE;
573}
574
575