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