]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slidrmsw.cpp
fingers crossed..
[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 and Markus Holzem
9 // Licence: wxWindows license
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 m_tickFreq = 0;
45 }
46
47 bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
48 int value, int minValue, int maxValue,
49 const wxPoint& pos,
50 const wxSize& size, long style,
51 const wxValidator& validator,
52 const wxString& name)
53 {
54 SetName(name);
55 #if wxUSE_VALIDATORS
56 SetValidator(validator);
57 #endif // wxUSE_VALIDATORS
58
59 if (parent) parent->AddChild(this);
60 SetBackgroundColour(parent->GetBackgroundColour()) ;
61 SetForegroundColour(parent->GetForegroundColour()) ;
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
81 // non-Win95 implementation
82
83 long msStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
84
85 if ( m_windowStyle & wxCLIP_SIBLINGS )
86 msStyle |= WS_CLIPSIBLINGS;
87
88 bool want3D;
89 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
90
91 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
92 msStyle,
93 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
94 wxGetInstance(), NULL);
95
96 // Now create min static control
97 wxSprintf(wxBuffer, wxT("%d"), minValue);
98 DWORD wstyle = STATIC_FLAGS;
99 if ( m_windowStyle & wxCLIP_SIBLINGS )
100 wstyle |= WS_CLIPSIBLINGS;
101 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
102 wstyle,
103 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
104 wxGetInstance(), NULL);
105
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
112 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
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
131 wxSprintf(wxBuffer, wxT("%d"), maxValue);
132 wstyle = STATIC_FLAGS;
133 if ( m_windowStyle & wxCLIP_SIBLINGS )
134 wstyle |= WS_CLIPSIBLINGS;
135 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
136 wstyle,
137 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
138 wxGetInstance(), NULL);
139
140 SetFont(parent->GetFont());
141
142 if (GetFont().Ok())
143 {
144 // GetFont()->RealizeResource();
145 if (GetFont().GetResourceHandle())
146 {
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,
152 (WPARAM)GetFont().GetResourceHandle(),0L);
153 if (m_staticValue)
154 SendMessage((HWND)m_staticValue,WM_SETFONT,
155 (WPARAM)GetFont().GetResourceHandle(),0L);
156 }
157 }
158
159 SetSize(x, y, width, height);
160 SetValue(value);
161
162 return TRUE;
163 }
164
165 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
166 WXWORD pos, WXHWND control)
167 {
168 int position = ::GetScrollPos((HWND)control, SB_CTL);
169
170 int nScrollInc;
171 wxEventType scrollEvent = wxEVT_NULL;
172 switch ( wParam )
173 {
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:
206 #ifdef __WIN32__
207 nScrollInc = (signed short)pos - position;
208 #else
209 nScrollInc = pos - position;
210 #endif
211 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
212 break;
213
214 default:
215 nScrollInc = 0;
216 }
217
218 if (nScrollInc == 0)
219 {
220 // no event...
221 return FALSE;
222 }
223
224 int newPos = position + nScrollInc;
225
226 if ( (newPos < GetMin()) || (newPos > GetMax()) )
227 {
228 // out of range - but we did process it
229 return TRUE;
230 }
231
232 SetValue(newPos);
233
234 wxScrollEvent event(scrollEvent, m_windowId);
235 event.SetPosition(newPos);
236 event.SetEventObject( this );
237 GetEventHandler()->ProcessEvent(event);
238
239 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
240 cevent.SetInt( newPos );
241 cevent.SetEventObject( this );
242
243 return GetEventHandler()->ProcessEvent( cevent );
244 }
245
246 wxSliderMSW::~wxSliderMSW()
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
256 int wxSliderMSW::GetValue() const
257 {
258 return ::GetScrollPos(GetHwnd(), SB_CTL);
259 }
260
261 void wxSliderMSW::SetValue(int value)
262 {
263 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
264 if (m_staticValue)
265 {
266 wxSprintf(wxBuffer, wxT("%d"), value);
267 SetWindowText((HWND) m_staticValue, wxBuffer);
268 }
269 }
270
271 void wxSliderMSW::GetSize(int *width, int *height) const
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
289 void wxSliderMSW::GetPosition(int *x, int *y) const
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
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 }
321 *x = point.x;
322 *y = point.y;
323 }
324
325 // TODO one day, make sense of all this horros and replace it with a readable
326 // DoGetBestSize()
327 void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
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);
336 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
337 x1 = currentX;
338 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
339 y1 = currentY;
340
341 AdjustForParentClientOrigin(x1, y1, sizeFlags);
342
343 wxChar buf[300];
344
345 int x_offset = x;
346 int y_offset = y;
347
348 int cx; // slider,min,max sizes
349 int cy;
350 int cyf;
351
352 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
353
354 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
355 {
356 if ( m_windowStyle & wxSL_LABELS )
357 {
358 int min_len = 0;
359
360 GetWindowText((HWND) m_staticMin, buf, 300);
361 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
362
363 int max_len = 0;
364
365 GetWindowText((HWND) m_staticMax, buf, 300);
366 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
367 if (m_staticValue)
368 {
369 int new_width = (int)(wxMax(min_len, max_len));
370 int valueHeight = (int)cyf;
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;
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
385 int slider_height = cy;
386
387 // Slider must have a minimum/default length/height
388 if (slider_length < 100)
389 slider_length = 100;
390
391 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
392 x_offset += slider_length + cx;
393
394 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
395 }
396 else
397 {
398 // No labels
399 if ( w1 < 0 )
400 w1 = 200;
401 if ( h1 < 0 )
402 h1 = 20;
403 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
404 }
405 }
406 else
407 {
408 if ( m_windowStyle & wxSL_LABELS )
409 {
410 int min_len;
411 GetWindowText((HWND) m_staticMin, buf, 300);
412 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
413
414 int max_len;
415 GetWindowText((HWND) m_staticMax, buf, 300);
416 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
417
418 if (m_staticValue)
419 {
420 int new_width = (int)(wxMax(min_len, max_len));
421 int valueHeight = (int)cyf;
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
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);
439
440 // Use character height as an estimate of slider width (yes, width)
441 int slider_width = cy;
442
443 // Slider must have a minimum/default length
444 if (slider_length < 100)
445 slider_length = 100;
446
447 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
448 y_offset += slider_length;
449
450 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
451 }
452 else
453 {
454 // No labels
455 if ( w1 < 0 )
456 w1 = 20;
457 if ( h1 < 0 )
458 h1 = 200;
459 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
460 }
461 }
462 }
463
464 void wxSliderMSW::SetRange(int minValue, int maxValue)
465 {
466 m_rangeMin = minValue;
467 m_rangeMax = maxValue;
468
469 ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
470 wxChar buf[40];
471 if ( m_staticMin )
472 {
473 wxSprintf(buf, wxT("%d"), m_rangeMin);
474 SetWindowText((HWND) m_staticMin, buf);
475 }
476
477 if ( m_staticMax )
478 {
479 wxSprintf(buf, wxT("%d"), m_rangeMax);
480 SetWindowText((HWND) m_staticMax, buf);
481 }
482 }
483
484 WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
485 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
486 {
487 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
488 return 0;
489
490 // Otherwise, it's a static
491 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
492 }
493
494 // For trackbars only
495 void wxSliderMSW::SetTickFreq(int n, int pos)
496 {
497 }
498
499 void wxSliderMSW::SetPageSize(int pageSize)
500 {
501 m_pageSize = pageSize;
502 }
503
504 int wxSliderMSW::GetPageSize() const
505 {
506 return m_pageSize;
507 }
508
509 void wxSliderMSW::ClearSel()
510 {
511 }
512
513 void wxSliderMSW::ClearTicks()
514 {
515 }
516
517 void wxSliderMSW::SetLineSize(int lineSize)
518 {
519 m_lineSize = lineSize;
520 }
521
522 int wxSliderMSW::GetLineSize() const
523 {
524 return m_lineSize;
525 }
526
527 int wxSliderMSW::GetSelEnd() const
528 {
529 return 0;
530 }
531
532 int wxSliderMSW::GetSelStart() const
533 {
534 return 0;
535 }
536
537 void wxSliderMSW::SetSelection(int minPos, int maxPos)
538 {
539 }
540
541 void wxSliderMSW::SetThumbLength(int len)
542 {
543 }
544
545 int wxSliderMSW::GetThumbLength() const
546 {
547 return 0;
548 }
549
550 void wxSliderMSW::SetTick(int tickPos)
551 {
552 }
553
554 bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
555 {
556 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
557 }
558
559 void wxSliderMSW::Command (wxCommandEvent & event)
560 {
561 SetValue (event.GetInt());
562 ProcessCommand (event);
563 }
564
565 bool wxSliderMSW::Show(bool show)
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