]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slidrmsw.cpp
0aad8787d239f383c90c98cd15aa198a6a104c82
[wxWidgets.git] / src / msw / slidrmsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slidermsw.cpp
3 // Purpose: wxSliderMSW
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "slidrmsw.h"
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>
25 #endif
26
27 #include "wx/utils.h"
28 #include "wx/brush.h"
29 #include "wx/msw/slidrmsw.h"
30 #include "wx/msw/private.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl)
33
34 // Slider
35 wxSliderMSW::wxSliderMSW()
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 }
45
46 bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
47 int value, int minValue, int maxValue,
48 const wxPoint& pos,
49 const wxSize& size, long style,
50 const wxValidator& validator,
51 const wxString& name)
52 {
53 SetName(name);
54 #if wxUSE_VALIDATORS
55 SetValidator(validator);
56 #endif // wxUSE_VALIDATORS
57
58 if (parent) parent->AddChild(this);
59 SetBackgroundColour(parent->GetBackgroundColour()) ;
60 SetForegroundColour(parent->GetForegroundColour()) ;
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;
68
69 if ( id == -1 )
70 m_windowId = (int)NewControlId();
71 else
72 m_windowId = id;
73
74 int x = pos.x;
75 int y = pos.y;
76 int width = size.x;
77 int height = size.y;
78
79 // non-Win95 implementation
80
81 long msStyle = SS_CENTER;
82
83 // WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
84 WXDWORD exStyle = 0;
85 msStyle |= MSWGetStyle(GetWindowStyle(), & exStyle) ;
86
87 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
88 msStyle,
89 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
90 wxGetInstance(), NULL);
91
92 // Now create min static control
93 wxString buf;
94 buf.Printf(wxT("%d"), minValue);
95 DWORD wstyle = STATIC_FLAGS;
96 if ( m_windowStyle & wxCLIP_SIBLINGS )
97 wstyle |= WS_CLIPSIBLINGS;
98 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), buf,
99 wstyle,
100 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
101 wxGetInstance(), NULL);
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(exStyle, wxT("SCROLLBAR"), wxT(""),
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 buf.Printf(wxT("%d"), maxValue);
129 wstyle = STATIC_FLAGS;
130 if ( m_windowStyle & wxCLIP_SIBLINGS )
131 wstyle |= WS_CLIPSIBLINGS;
132 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), buf,
133 wstyle,
134 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
135 wxGetInstance(), NULL);
136
137 SetFont(parent->GetFont());
138
139 if (GetFont().Ok())
140 {
141 // GetFont()->RealizeResource();
142 if (GetFont().GetResourceHandle())
143 {
144 if ( m_staticMin )
145 SendMessage((HWND)m_staticMin,WM_SETFONT,
146 (WPARAM)GetFont().GetResourceHandle(),0L);
147 if ( m_staticMax )
148 SendMessage((HWND)m_staticMax,WM_SETFONT,
149 (WPARAM)GetFont().GetResourceHandle(),0L);
150 if (m_staticValue)
151 SendMessage((HWND)m_staticValue,WM_SETFONT,
152 (WPARAM)GetFont().GetResourceHandle(),0L);
153 }
154 }
155
156 SetSize(x, y, width, height);
157 SetValue(value);
158
159 return TRUE;
160 }
161
162 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
163 WXWORD pos, WXHWND control)
164 {
165 int position = ::GetScrollPos((HWND)control, SB_CTL);
166
167 int nScrollInc;
168 wxEventType scrollEvent = wxEVT_NULL;
169 switch ( wParam )
170 {
171 case SB_TOP:
172 nScrollInc = m_rangeMax - position;
173 scrollEvent = wxEVT_SCROLL_TOP;
174 break;
175
176 case SB_BOTTOM:
177 nScrollInc = - position;
178 scrollEvent = wxEVT_SCROLL_BOTTOM;
179 break;
180
181 case SB_LINEUP:
182 nScrollInc = - GetLineSize();
183 scrollEvent = wxEVT_SCROLL_LINEUP;
184 break;
185
186 case SB_LINEDOWN:
187 nScrollInc = GetLineSize();
188 scrollEvent = wxEVT_SCROLL_LINEDOWN;
189 break;
190
191 case SB_PAGEUP:
192 nScrollInc = -GetPageSize();
193 scrollEvent = wxEVT_SCROLL_PAGEUP;
194 break;
195
196 case SB_PAGEDOWN:
197 nScrollInc = GetPageSize();
198 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
199 break;
200
201 case SB_THUMBTRACK:
202 case SB_THUMBPOSITION:
203 #ifdef __WIN32__
204 nScrollInc = (signed short)pos - position;
205 #else
206 nScrollInc = pos - position;
207 #endif
208 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
209 break;
210
211 default:
212 nScrollInc = 0;
213 }
214
215 if (nScrollInc == 0)
216 {
217 // no event...
218 return FALSE;
219 }
220
221 int newPos = position + nScrollInc;
222
223 if ( (newPos < GetMin()) || (newPos > GetMax()) )
224 {
225 // out of range - but we did process it
226 return TRUE;
227 }
228
229 SetValue(newPos);
230
231 wxScrollEvent event(scrollEvent, m_windowId);
232 event.SetPosition(newPos);
233 event.SetEventObject( this );
234 GetEventHandler()->ProcessEvent(event);
235
236 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
237 cevent.SetInt( newPos );
238 cevent.SetEventObject( this );
239
240 return GetEventHandler()->ProcessEvent( cevent );
241 }
242
243 wxSliderMSW::~wxSliderMSW()
244 {
245 if (m_staticMin)
246 DestroyWindow((HWND) m_staticMin);
247 if (m_staticMax)
248 DestroyWindow((HWND) m_staticMax);
249 if (m_staticValue)
250 DestroyWindow((HWND) m_staticValue);
251 }
252
253 int wxSliderMSW::GetValue() const
254 {
255 return ::GetScrollPos(GetHwnd(), SB_CTL);
256 }
257
258 void wxSliderMSW::SetValue(int value)
259 {
260 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
261 if (m_staticValue)
262 {
263 wxString buf;
264 buf.Printf(wxT("%d"), value);
265 SetWindowText((HWND) m_staticValue, buf);
266 }
267 }
268
269 void wxSliderMSW::GetSize(int *width, int *height) const
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
287 void wxSliderMSW::GetPosition(int *x, int *y) const
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
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 }
319 *x = point.x;
320 *y = point.y;
321 }
322
323 // TODO one day, make sense of all this horros and replace it with a readable
324 // DoGetBestSize()
325 void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
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);
334 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
335 x1 = currentX;
336 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
337 y1 = currentY;
338
339 AdjustForParentClientOrigin(x1, y1, sizeFlags);
340
341 wxChar buf[300];
342
343 int x_offset = x;
344 int y_offset = y;
345
346 int cx; // slider,min,max sizes
347 int cy;
348 int cyf;
349
350 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
351
352 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
353 {
354 if ( m_windowStyle & wxSL_LABELS )
355 {
356 int min_len = 0;
357
358 GetWindowText((HWND) m_staticMin, buf, 300);
359 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
360
361 int max_len = 0;
362
363 GetWindowText((HWND) m_staticMax, buf, 300);
364 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
365 if (m_staticValue)
366 {
367 int new_width = (int)(wxMax(min_len, max_len));
368 int valueHeight = (int)cyf;
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;
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
383 int slider_height = cy;
384
385 // Slider must have a minimum/default length/height
386 if (slider_length < 100)
387 slider_length = 100;
388
389 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
390 x_offset += slider_length + cx;
391
392 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
393 }
394 else
395 {
396 // No labels
397 if ( w1 < 0 )
398 w1 = 200;
399 if ( h1 < 0 )
400 h1 = 20;
401 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
402 }
403 }
404 else
405 {
406 if ( m_windowStyle & wxSL_LABELS )
407 {
408 int min_len;
409 GetWindowText((HWND) m_staticMin, buf, 300);
410 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
411
412 int max_len;
413 GetWindowText((HWND) m_staticMax, buf, 300);
414 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
415
416 if (m_staticValue)
417 {
418 int new_width = (int)(wxMax(min_len, max_len));
419 int valueHeight = (int)cyf;
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
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);
437
438 // Use character height as an estimate of slider width (yes, width)
439 int slider_width = cy;
440
441 // Slider must have a minimum/default length
442 if (slider_length < 100)
443 slider_length = 100;
444
445 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
446 y_offset += slider_length;
447
448 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
449 }
450 else
451 {
452 // No labels
453 if ( w1 < 0 )
454 w1 = 20;
455 if ( h1 < 0 )
456 h1 = 200;
457 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
458 }
459 }
460 }
461
462 void wxSliderMSW::SetRange(int minValue, int maxValue)
463 {
464 m_rangeMin = minValue;
465 m_rangeMax = maxValue;
466
467 ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
468 wxChar buf[40];
469 if ( m_staticMin )
470 {
471 wxSprintf(buf, wxT("%d"), m_rangeMin);
472 SetWindowText((HWND) m_staticMin, buf);
473 }
474
475 if ( m_staticMax )
476 {
477 wxSprintf(buf, wxT("%d"), m_rangeMax);
478 SetWindowText((HWND) m_staticMax, buf);
479 }
480 }
481
482 WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
483 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
484 {
485 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
486 return 0;
487
488 // Otherwise, it's a static
489 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
490 }
491
492 void wxSliderMSW::SetPageSize(int pageSize)
493 {
494 m_pageSize = pageSize;
495 }
496
497 int wxSliderMSW::GetPageSize() const
498 {
499 return m_pageSize;
500 }
501
502 void wxSliderMSW::SetLineSize(int lineSize)
503 {
504 m_lineSize = lineSize;
505 }
506
507 int wxSliderMSW::GetLineSize() const
508 {
509 return m_lineSize;
510 }
511
512 // Not yet implemented
513 void wxSliderMSW::SetThumbLength(int WXUNUSED(lenPixels))
514 {
515 }
516
517 // Not yet implemented
518 int wxSliderMSW::GetThumbLength() const
519 {
520 return 0;
521 }
522
523 bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
524 {
525 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
526 }
527
528 void wxSliderMSW::Command (wxCommandEvent & event)
529 {
530 SetValue (event.GetInt());
531 ProcessCommand (event);
532 }
533
534 bool wxSliderMSW::Show(bool show)
535 {
536 wxWindow::Show(show);
537
538 int cshow;
539 if (show)
540 cshow = SW_SHOW;
541 else
542 cshow = SW_HIDE;
543
544 if(m_staticValue)
545 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
546 if(m_staticMin)
547 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
548 if(m_staticMax)
549 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
550 return TRUE;
551 }
552
553