]> git.saurik.com Git - wxWidgets.git/blame - src/msw/slidrmsw.cpp
1. compilation fixes for TB_REPLACEBITMAP and FONTENUMPROC
[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);
55 SetValidator(validator);
56
57 if (parent) parent->AddChild(this);
fd71308f
JS
58 SetBackgroundColour(parent->GetBackgroundColour()) ;
59 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
60
61 m_staticValue = 0;
62 m_staticMin = 0;
63 m_staticMax = 0;
64 m_pageSize = 1;
65 m_lineSize = 1;
66 m_windowStyle = style;
67 m_tickFreq = 0;
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
83 bool want3D;
84 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
85
223d09f6 86 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
2bda0e17
KB
87 msStyle,
88 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
89 wxGetInstance(), NULL);
90
91 // Now create min static control
223d09f6
KB
92 wxSprintf(wxBuffer, wxT("%d"), minValue);
93 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
2bda0e17
KB
94 STATIC_FLAGS,
95 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
96 wxGetInstance(), NULL);
97
98 // Now create slider
99 m_windowId = (int)NewControlId();
100
101 msStyle = 0;
102 if (m_windowStyle & wxSL_VERTICAL)
103 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
104 else
105 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
106
223d09f6 107 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
2bda0e17
KB
108 msStyle,
109 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
110 wxGetInstance(), NULL);
111
112 m_pageSize = (int)((maxValue-minValue)/10);
113 m_rangeMax = maxValue;
114 m_rangeMin = minValue;
115
116 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
117 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
118 ShowWindow(scroll_bar, SW_SHOW);
119
120 m_hWnd = (WXHWND)scroll_bar;
121
122 // Subclass again for purposes of dialog editing mode
123 SubclassWin(GetHWND());
124
125 // Finally, create max value static item
223d09f6
KB
126 wxSprintf(wxBuffer, wxT("%d"), maxValue);
127 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
2bda0e17
KB
128 STATIC_FLAGS,
129 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
130 wxGetInstance(), NULL);
131
c0ed460c 132 SetFont(parent->GetFont());
2bda0e17 133
c0ed460c 134 if (GetFont().Ok())
2bda0e17
KB
135 {
136// GetFont()->RealizeResource();
c0ed460c 137 if (GetFont().GetResourceHandle())
2bda0e17 138 {
a23fd0e1
VZ
139 if ( m_staticMin )
140 SendMessage((HWND)m_staticMin,WM_SETFONT,
141 (WPARAM)GetFont().GetResourceHandle(),0L);
142 if ( m_staticMax )
143 SendMessage((HWND)m_staticMax,WM_SETFONT,
c0ed460c 144 (WPARAM)GetFont().GetResourceHandle(),0L);
a23fd0e1
VZ
145 if (m_staticValue)
146 SendMessage((HWND)m_staticValue,WM_SETFONT,
c0ed460c 147 (WPARAM)GetFont().GetResourceHandle(),0L);
2bda0e17
KB
148 }
149 }
2bda0e17
KB
150
151 SetSize(x, y, width, height);
152 SetValue(value);
153
154 return TRUE;
155}
156
a23fd0e1
VZ
157bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
158 WXWORD pos, WXHWND control)
2bda0e17 159{
2bda0e17 160 int position = ::GetScrollPos((HWND)control, SB_CTL);
2bda0e17
KB
161
162 int nScrollInc;
7798a18e 163 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
164 switch ( wParam )
165 {
a23fd0e1
VZ
166 case SB_TOP:
167 nScrollInc = m_rangeMax - position;
168 scrollEvent = wxEVT_SCROLL_TOP;
169 break;
170
171 case SB_BOTTOM:
172 nScrollInc = - position;
173 scrollEvent = wxEVT_SCROLL_BOTTOM;
174 break;
175
176 case SB_LINEUP:
177 nScrollInc = - GetLineSize();
178 scrollEvent = wxEVT_SCROLL_LINEUP;
179 break;
180
181 case SB_LINEDOWN:
182 nScrollInc = GetLineSize();
183 scrollEvent = wxEVT_SCROLL_LINEDOWN;
184 break;
185
186 case SB_PAGEUP:
187 nScrollInc = -GetPageSize();
188 scrollEvent = wxEVT_SCROLL_PAGEUP;
189 break;
190
191 case SB_PAGEDOWN:
192 nScrollInc = GetPageSize();
193 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
194 break;
195
196 case SB_THUMBTRACK:
197 case SB_THUMBPOSITION:
2bda0e17 198#ifdef __WIN32__
a23fd0e1 199 nScrollInc = (signed short)pos - position;
2bda0e17 200#else
a23fd0e1 201 nScrollInc = pos - position;
2bda0e17 202#endif
a23fd0e1
VZ
203 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
204 break;
2bda0e17 205
a23fd0e1
VZ
206 default:
207 nScrollInc = 0;
2bda0e17
KB
208 }
209
a23fd0e1 210 if (nScrollInc == 0)
2bda0e17 211 {
a23fd0e1
VZ
212 // no event...
213 return FALSE;
214 }
2bda0e17 215
a23fd0e1 216 int newPos = position + nScrollInc;
da87a1ca 217
a23fd0e1
VZ
218 if ( (newPos < GetMin()) || (newPos > GetMax()) )
219 {
220 // out of range - but we did process it
221 return TRUE;
222 }
2bda0e17 223
a23fd0e1 224 SetValue(newPos);
f3a65071 225
a23fd0e1
VZ
226 wxScrollEvent event(scrollEvent, m_windowId);
227 event.SetPosition(newPos);
228 event.SetEventObject( this );
229 GetEventHandler()->ProcessEvent(event);
2bda0e17 230
a23fd0e1
VZ
231 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
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