]> git.saurik.com Git - wxWidgets.git/blame - src/msw/slidrmsw.cpp
Applied patch #421554: implementation of wxEVT_CONTEXT_MENU
[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
b0766406
JS
85 if ( m_windowStyle & wxCLIP_SIBLINGS )
86 msStyle |= WS_CLIPSIBLINGS;
87
2bda0e17
KB
88 bool want3D;
89 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
90
223d09f6 91 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
2bda0e17
KB
92 msStyle,
93 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
94 wxGetInstance(), NULL);
95
96 // Now create min static control
223d09f6 97 wxSprintf(wxBuffer, wxT("%d"), minValue);
b0766406
JS
98 DWORD wstyle = STATIC_FLAGS;
99 if ( m_windowStyle & wxCLIP_SIBLINGS )
100 wstyle |= WS_CLIPSIBLINGS;
223d09f6 101 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
b0766406 102 wstyle,
2bda0e17
KB
103 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
104 wxGetInstance(), NULL);
105
2bda0e17
KB
106 msStyle = 0;
107 if (m_windowStyle & wxSL_VERTICAL)
108 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
109 else
110 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
111
223d09f6 112 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
2bda0e17
KB
113 msStyle,
114 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
115 wxGetInstance(), NULL);
116
117 m_pageSize = (int)((maxValue-minValue)/10);
118 m_rangeMax = maxValue;
119 m_rangeMin = minValue;
120
121 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
122 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
123 ShowWindow(scroll_bar, SW_SHOW);
124
125 m_hWnd = (WXHWND)scroll_bar;
126
127 // Subclass again for purposes of dialog editing mode
128 SubclassWin(GetHWND());
129
130 // Finally, create max value static item
223d09f6 131 wxSprintf(wxBuffer, wxT("%d"), maxValue);
b0766406
JS
132 wstyle = STATIC_FLAGS;
133 if ( m_windowStyle & wxCLIP_SIBLINGS )
134 wstyle |= WS_CLIPSIBLINGS;
223d09f6 135 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
b0766406 136 wstyle,
2bda0e17
KB
137 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
138 wxGetInstance(), NULL);
139
c0ed460c 140 SetFont(parent->GetFont());
2bda0e17 141
c0ed460c 142 if (GetFont().Ok())
2bda0e17
KB
143 {
144// GetFont()->RealizeResource();
c0ed460c 145 if (GetFont().GetResourceHandle())
2bda0e17 146 {
a23fd0e1
VZ
147 if ( m_staticMin )
148 SendMessage((HWND)m_staticMin,WM_SETFONT,
149 (WPARAM)GetFont().GetResourceHandle(),0L);
150 if ( m_staticMax )
151 SendMessage((HWND)m_staticMax,WM_SETFONT,
c0ed460c 152 (WPARAM)GetFont().GetResourceHandle(),0L);
a23fd0e1
VZ
153 if (m_staticValue)
154 SendMessage((HWND)m_staticValue,WM_SETFONT,
c0ed460c 155 (WPARAM)GetFont().GetResourceHandle(),0L);
2bda0e17
KB
156 }
157 }
2bda0e17
KB
158
159 SetSize(x, y, width, height);
160 SetValue(value);
161
162 return TRUE;
163}
164
a23fd0e1
VZ
165bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
166 WXWORD pos, WXHWND control)
2bda0e17 167{
2bda0e17 168 int position = ::GetScrollPos((HWND)control, SB_CTL);
2bda0e17
KB
169
170 int nScrollInc;
7798a18e 171 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
172 switch ( wParam )
173 {
a23fd0e1
VZ
174 case SB_TOP:
175 nScrollInc = m_rangeMax - position;
176 scrollEvent = wxEVT_SCROLL_TOP;
177 break;
178
179 case SB_BOTTOM:
180 nScrollInc = - position;
181 scrollEvent = wxEVT_SCROLL_BOTTOM;
182 break;
183
184 case SB_LINEUP:
185 nScrollInc = - GetLineSize();
186 scrollEvent = wxEVT_SCROLL_LINEUP;
187 break;
188
189 case SB_LINEDOWN:
190 nScrollInc = GetLineSize();
191 scrollEvent = wxEVT_SCROLL_LINEDOWN;
192 break;
193
194 case SB_PAGEUP:
195 nScrollInc = -GetPageSize();
196 scrollEvent = wxEVT_SCROLL_PAGEUP;
197 break;
198
199 case SB_PAGEDOWN:
200 nScrollInc = GetPageSize();
201 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
202 break;
203
204 case SB_THUMBTRACK:
205 case SB_THUMBPOSITION:
2bda0e17 206#ifdef __WIN32__
a23fd0e1 207 nScrollInc = (signed short)pos - position;
2bda0e17 208#else
a23fd0e1 209 nScrollInc = pos - position;
2bda0e17 210#endif
a23fd0e1
VZ
211 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
212 break;
2bda0e17 213
a23fd0e1
VZ
214 default:
215 nScrollInc = 0;
2bda0e17
KB
216 }
217
a23fd0e1 218 if (nScrollInc == 0)
2bda0e17 219 {
a23fd0e1
VZ
220 // no event...
221 return FALSE;
222 }
2bda0e17 223
a23fd0e1 224 int newPos = position + nScrollInc;
da87a1ca 225
a23fd0e1
VZ
226 if ( (newPos < GetMin()) || (newPos > GetMax()) )
227 {
228 // out of range - but we did process it
229 return TRUE;
230 }
2bda0e17 231
a23fd0e1 232 SetValue(newPos);
f3a65071 233
a23fd0e1
VZ
234 wxScrollEvent event(scrollEvent, m_windowId);
235 event.SetPosition(newPos);
236 event.SetEventObject( this );
237 GetEventHandler()->ProcessEvent(event);
2bda0e17 238
a23fd0e1 239 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
f6bcfd97 240 cevent.SetInt( newPos );
a23fd0e1
VZ
241 cevent.SetEventObject( this );
242
243 return GetEventHandler()->ProcessEvent( cevent );
2bda0e17
KB
244}
245
bfc6fde4 246wxSliderMSW::~wxSliderMSW()
2bda0e17
KB
247{
248 if (m_staticMin)
249 DestroyWindow((HWND) m_staticMin);
250 if (m_staticMax)
251 DestroyWindow((HWND) m_staticMax);
252 if (m_staticValue)
253 DestroyWindow((HWND) m_staticValue);
254}
255
bfc6fde4 256int wxSliderMSW::GetValue() const
2bda0e17 257{
4438caf4 258 return ::GetScrollPos(GetHwnd(), SB_CTL);
2bda0e17
KB
259}
260
debe6624 261void wxSliderMSW::SetValue(int value)
2bda0e17 262{
4438caf4 263 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
2bda0e17
KB
264 if (m_staticValue)
265 {
223d09f6 266 wxSprintf(wxBuffer, wxT("%d"), value);
2bda0e17
KB
267 SetWindowText((HWND) m_staticValue, wxBuffer);
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
494// For trackbars only
debe6624 495void wxSliderMSW::SetTickFreq(int n, int pos)
2bda0e17 496{
2bda0e17
KB
497}
498
debe6624 499void wxSliderMSW::SetPageSize(int pageSize)
2bda0e17 500{
2bda0e17
KB
501 m_pageSize = pageSize;
502}
503
bfc6fde4 504int wxSliderMSW::GetPageSize() const
2bda0e17
KB
505{
506 return m_pageSize;
507}
508
bfc6fde4 509void wxSliderMSW::ClearSel()
2bda0e17 510{
2bda0e17
KB
511}
512
bfc6fde4 513void wxSliderMSW::ClearTicks()
2bda0e17 514{
2bda0e17
KB
515}
516
debe6624 517void wxSliderMSW::SetLineSize(int lineSize)
2bda0e17
KB
518{
519 m_lineSize = lineSize;
2bda0e17
KB
520}
521
bfc6fde4 522int wxSliderMSW::GetLineSize() const
2bda0e17 523{
2bda0e17 524 return m_lineSize;
2bda0e17
KB
525}
526
bfc6fde4 527int wxSliderMSW::GetSelEnd() const
2bda0e17 528{
2bda0e17 529 return 0;
2bda0e17
KB
530}
531
bfc6fde4 532int wxSliderMSW::GetSelStart() const
2bda0e17 533{
2bda0e17 534 return 0;
2bda0e17
KB
535}
536
debe6624 537void wxSliderMSW::SetSelection(int minPos, int maxPos)
2bda0e17 538{
2bda0e17
KB
539}
540
debe6624 541void wxSliderMSW::SetThumbLength(int len)
2bda0e17 542{
2bda0e17
KB
543}
544
bfc6fde4 545int wxSliderMSW::GetThumbLength() const
2bda0e17 546{
2bda0e17 547 return 0;
2bda0e17
KB
548}
549
debe6624 550void wxSliderMSW::SetTick(int tickPos)
2bda0e17 551{
2bda0e17
KB
552}
553
da87a1ca 554bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
2bda0e17 555{
a23fd0e1 556 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
2bda0e17
KB
557}
558
da87a1ca 559void wxSliderMSW::Command (wxCommandEvent & event)
2bda0e17
KB
560{
561 SetValue (event.GetInt());
562 ProcessCommand (event);
563}
564
debe6624 565bool wxSliderMSW::Show(bool show)
2bda0e17 566{
a23fd0e1 567 wxWindow::Show(show);
2bda0e17
KB
568
569 int cshow;
570 if (show)
571 cshow = SW_SHOW;
572 else
573 cshow = SW_HIDE;
574
575 if(m_staticValue)
a23fd0e1 576 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
2bda0e17 577 if(m_staticMin)
a23fd0e1 578 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
2bda0e17 579 if(m_staticMax)
a23fd0e1 580 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
2bda0e17
KB
581 return TRUE;
582}
583
584