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