]> git.saurik.com Git - wxWidgets.git/blame - src/msw/slidrmsw.cpp
added FreeBSD
[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
9// Licence: wxWindows license
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
da87a1ca 37wxSliderMSW::wxSliderMSW(void)
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 )
72 m_windowId = (int)NewControlId();
73 else
74 m_windowId = id;
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
88 m_staticValue = (WXHWND) CreateWindowEx(exStyle, "STATIC", NULL,
89 msStyle,
90 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
91 wxGetInstance(), NULL);
92
93 // Now create min static control
94 sprintf(wxBuffer, "%d", minValue);
95 m_staticMin = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
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
109 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), "SCROLLBAR", wxBuffer,
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
128 sprintf(wxBuffer, "%d", maxValue);
129 m_staticMax = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
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
KB
140 {
141 if ( m_staticMin )
142 SendMessage((HWND)m_staticMin,WM_SETFONT,
c0ed460c 143 (WPARAM)GetFont().GetResourceHandle(),0L);
2bda0e17
KB
144 if ( m_staticMax )
145 SendMessage((HWND)m_staticMax,WM_SETFONT,
c0ed460c 146 (WPARAM)GetFont().GetResourceHandle(),0L);
2bda0e17
KB
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
debe6624 159void wxSliderMSW::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control)
2bda0e17 160{
2bda0e17 161 int position = ::GetScrollPos((HWND)control, SB_CTL);
2bda0e17
KB
162
163 int nScrollInc;
7798a18e 164 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
165 switch ( wParam )
166 {
167 case SB_TOP:
168 nScrollInc = m_rangeMax - position;
169 scrollEvent = wxEVT_SCROLL_TOP;
170 break;
171
172 case SB_BOTTOM:
173 nScrollInc = - position;
174 scrollEvent = wxEVT_SCROLL_BOTTOM;
175 break;
176
177 case SB_LINEUP:
178 nScrollInc = - GetLineSize();
179 scrollEvent = wxEVT_SCROLL_LINEUP;
180 break;
181
182 case SB_LINEDOWN:
183 nScrollInc = GetLineSize();
184 scrollEvent = wxEVT_SCROLL_LINEDOWN;
185 break;
186
187 case SB_PAGEUP:
188 nScrollInc = -GetPageSize();
189 scrollEvent = wxEVT_SCROLL_PAGEUP;
190 break;
191
192 case SB_PAGEDOWN:
193 nScrollInc = GetPageSize();
194 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
195 break;
196
197 case SB_THUMBTRACK:
198 case SB_THUMBPOSITION:
199#ifdef __WIN32__
200 nScrollInc = (signed short)pos - position;
201#else
202 nScrollInc = pos - position;
203#endif
204 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
205 break;
206
207 default:
208 nScrollInc = 0;
209 return;
210 }
211
2bda0e17 212 if (nScrollInc != 0)
2bda0e17
KB
213 {
214
2bda0e17 215 int newPos = position + nScrollInc;
da87a1ca 216
2bda0e17
KB
217 if (!(newPos < GetMin() || newPos > GetMax()))
218 {
219 SetValue(newPos);
220
221 wxScrollEvent event(scrollEvent, m_windowId);
222 event.SetPosition(newPos);
223 event.SetEventObject( this );
224 GetEventHandler()->ProcessEvent(event);
f3a65071
JS
225
226 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
227 cevent.SetEventObject( this );
228 GetEventHandler()->ProcessEvent( cevent );
2bda0e17
KB
229 }
230 }
231}
232
debe6624 233void wxSliderMSW::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control)
2bda0e17
KB
234{
235 MSWOnVScroll(wParam, pos, control);
236}
237
da87a1ca 238wxSliderMSW::~wxSliderMSW(void)
2bda0e17
KB
239{
240 if (m_staticMin)
241 DestroyWindow((HWND) m_staticMin);
242 if (m_staticMax)
243 DestroyWindow((HWND) m_staticMax);
244 if (m_staticValue)
245 DestroyWindow((HWND) m_staticValue);
246}
247
da87a1ca 248int wxSliderMSW::GetValue(void) const
2bda0e17 249{
2bda0e17 250 return ::GetScrollPos((HWND) GetHWND(), SB_CTL);
2bda0e17
KB
251}
252
debe6624 253void wxSliderMSW::SetValue(int value)
2bda0e17 254{
2bda0e17 255 ::SetScrollPos((HWND) GetHWND(), SB_CTL, value, TRUE);
2bda0e17
KB
256 if (m_staticValue)
257 {
258 sprintf(wxBuffer, "%d", value);
259 SetWindowText((HWND) m_staticValue, wxBuffer);
260 }
261}
262
da87a1ca 263void wxSliderMSW::GetSize(int *width, int *height) const
2bda0e17
KB
264{
265 RECT rect;
266 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
267
268 wxFindMaxSize(GetHWND(), &rect);
269
270 if (m_staticMin)
271 wxFindMaxSize(m_staticMin, &rect);
272 if (m_staticMax)
273 wxFindMaxSize(m_staticMax, &rect);
274 if (m_staticValue)
275 wxFindMaxSize(m_staticValue, &rect);
276
277 *width = rect.right - rect.left;
278 *height = rect.bottom - rect.top;
279}
280
da87a1ca 281void wxSliderMSW::GetPosition(int *x, int *y) const
2bda0e17
KB
282{
283 wxWindow *parent = GetParent();
284 RECT rect;
285 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
286
287 wxFindMaxSize(GetHWND(), &rect);
288
289 if (m_staticMin)
290 wxFindMaxSize(m_staticMin, &rect);
291 if (m_staticMax)
292 wxFindMaxSize(m_staticMax, &rect);
293 if (m_staticValue)
294 wxFindMaxSize(m_staticValue, &rect);
295
296 // Since we now have the absolute screen coords,
297 // if there's a parent we must subtract its top left corner
298 POINT point;
299 point.x = rect.left;
300 point.y = rect.top;
301 if (parent)
302 ::ScreenToClient((HWND) parent->GetHWND(), &point);
303
81d66cf3
JS
304 // We may be faking the client origin.
305 // So a window that's really at (0, 30) may appear
306 // (to wxWin apps) to be at (0, 0).
307 if (GetParent())
308 {
309 wxPoint pt(GetParent()->GetClientAreaOrigin());
310 point.x -= pt.x;
311 point.y -= pt.y;
312 }
2bda0e17
KB
313 *x = point.x;
314 *y = point.y;
315}
316
debe6624 317void wxSliderMSW::SetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17
KB
318{
319 int x1 = x;
320 int y1 = y;
321 int w1 = width;
322 int h1 = height;
323
324 int currentX, currentY;
325 GetPosition(&currentX, &currentY);
326 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
327 x1 = currentX;
328 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
329 y1 = currentY;
330
81d66cf3
JS
331 AdjustForParentClientOrigin(x1, y1, sizeFlags);
332
2bda0e17
KB
333 char buf[300];
334
335 int x_offset = x;
336 int y_offset = y;
337
338 int cx; // slider,min,max sizes
339 int cy;
debe6624 340 int cyf;
2bda0e17 341
ce3ed50d 342 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
2bda0e17
KB
343
344 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
345 {
346 if ( m_windowStyle & wxSL_LABELS )
347 {
debe6624 348 int min_len = 0;
2bda0e17
KB
349
350 GetWindowText((HWND) m_staticMin, buf, 300);
ce3ed50d 351 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17 352
debe6624 353 int max_len = 0;
2bda0e17
KB
354
355 GetWindowText((HWND) m_staticMax, buf, 300);
ce3ed50d 356 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17
KB
357 if (m_staticValue)
358 {
359 int new_width = (int)(wxMax(min_len, max_len));
360 int valueHeight = (int)cyf;
361#ifdef __WIN32__
362 // For some reason, under Win95, the text edit control has
363 // a lot of space before the first character
364 new_width += 3*cx;
2bda0e17
KB
365#endif
366 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
367 x_offset += new_width + cx;
368 }
369
370 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
371 x_offset += (int)(min_len + cx);
372
373 int slider_length = (int)(w1 - x_offset - max_len - cx);
374
2bda0e17 375 int slider_height = cy;
2bda0e17
KB
376
377 // Slider must have a minimum/default length/height
378 if (slider_length < 100)
379 slider_length = 100;
380
381 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_length, slider_height, TRUE);
382 x_offset += slider_length + cx;
383
384 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
385 }
386 else
387 {
388 // No labels
389 if ( w1 < 0 )
390 w1 = 200;
391 if ( h1 < 0 )
392 h1 = 20;
393 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
394 }
395 }
396 else
397 {
398 if ( m_windowStyle & wxSL_LABELS )
399 {
debe6624 400 int min_len;
2bda0e17 401 GetWindowText((HWND) m_staticMin, buf, 300);
ce3ed50d 402 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
2bda0e17 403
debe6624 404 int max_len;
2bda0e17 405 GetWindowText((HWND) m_staticMax, buf, 300);
ce3ed50d 406 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
2bda0e17
KB
407
408 if (m_staticValue)
409 {
410 int new_width = (int)(wxMax(min_len, max_len));
411 int valueHeight = (int)cyf;
412/*** Suggested change by George Tasker - remove this block...
413#ifdef __WIN32__
414 // For some reason, under Win95, the text edit control has
415 // a lot of space before the first character
416 new_width += 3*cx;
417#endif
418 ... and replace with following line: */
419 new_width += cx;
420
2bda0e17
KB
421 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
422 y_offset += valueHeight;
423 }
424
425 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
426 y_offset += cy;
427
428 int slider_length = (int)(h1 - y_offset - cy - cy);
da87a1ca 429
2bda0e17
KB
430 // Use character height as an estimate of slider width (yes, width)
431 int slider_width = cy;
2bda0e17
KB
432
433 // Slider must have a minimum/default length
434 if (slider_length < 100)
435 slider_length = 100;
436
437 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_width, slider_length, TRUE);
438 y_offset += slider_length;
439
440 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
441 }
442 else
443 {
444 // No labels
445 if ( w1 < 0 )
446 w1 = 20;
447 if ( h1 < 0 )
448 h1 = 200;
449 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
450 }
451 }
2bda0e17
KB
452}
453
debe6624 454void wxSliderMSW::SetRange(int minValue, int maxValue)
2bda0e17
KB
455{
456 m_rangeMin = minValue;
457 m_rangeMax = maxValue;
458
2bda0e17 459 ::SetScrollRange((HWND) GetHWND(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
2bda0e17
KB
460 char buf[40];
461 if ( m_staticMin )
462 {
463 sprintf(buf, "%d", m_rangeMin);
464 SetWindowText((HWND) m_staticMin, buf);
465 }
466
467 if ( m_staticMax )
468 {
469 sprintf(buf, "%d", m_rangeMax);
470 SetWindowText((HWND) m_staticMax, buf);
471 }
472}
473
debe6624 474WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
2bda0e17
KB
475 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
476{
477 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
478 return 0;
479
480 // Otherwise, it's a static
481 if (GetParent()->GetTransparentBackground())
482 SetBkMode((HDC) pDC, TRANSPARENT);
483 else
484 SetBkMode((HDC) pDC, OPAQUE);
485
486 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
487 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
488
489 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
490
2bda0e17
KB
491 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
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
da87a1ca 504int wxSliderMSW::GetPageSize(void) const
2bda0e17
KB
505{
506 return m_pageSize;
507}
508
da87a1ca 509void wxSliderMSW::ClearSel(void)
2bda0e17 510{
2bda0e17
KB
511}
512
da87a1ca 513void wxSliderMSW::ClearTicks(void)
2bda0e17 514{
2bda0e17
KB
515}
516
debe6624 517void wxSliderMSW::SetLineSize(int lineSize)
2bda0e17
KB
518{
519 m_lineSize = lineSize;
2bda0e17
KB
520}
521
da87a1ca 522int wxSliderMSW::GetLineSize(void) const
2bda0e17 523{
2bda0e17 524 return m_lineSize;
2bda0e17
KB
525}
526
da87a1ca 527int wxSliderMSW::GetSelEnd(void) const
2bda0e17 528{
2bda0e17 529 return 0;
2bda0e17
KB
530}
531
da87a1ca 532int wxSliderMSW::GetSelStart(void) 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
da87a1ca 545int wxSliderMSW::GetThumbLength(void) 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
KB
555{
556 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
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
KB
566{
567 wxWindow::Show(show);
568
569 int cshow;
570 if (show)
571 cshow = SW_SHOW;
572 else
573 cshow = SW_HIDE;
574
575 if(m_staticValue)
576 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
577 if(m_staticMin)
578 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
579 if(m_staticMax)
580 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
581 return TRUE;
582}
583
584