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