]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slidrmsw.cpp
corrected eventual endless loop
[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 bool want3D;
86 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
87
88 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("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 wxSprintf(wxBuffer, wxT("%d"), minValue);
95 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
96 STATIC_FLAGS,
97 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
98 wxGetInstance(), NULL);
99
100 msStyle = 0;
101 if (m_windowStyle & wxSL_VERTICAL)
102 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
103 else
104 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
105
106 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
107 msStyle,
108 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
109 wxGetInstance(), NULL);
110
111 m_pageSize = (int)((maxValue-minValue)/10);
112 m_rangeMax = maxValue;
113 m_rangeMin = minValue;
114
115 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
116 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
117 ShowWindow(scroll_bar, SW_SHOW);
118
119 m_hWnd = (WXHWND)scroll_bar;
120
121 // Subclass again for purposes of dialog editing mode
122 SubclassWin(GetHWND());
123
124 // Finally, create max value static item
125 wxSprintf(wxBuffer, wxT("%d"), maxValue);
126 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
127 STATIC_FLAGS,
128 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
129 wxGetInstance(), NULL);
130
131 SetFont(parent->GetFont());
132
133 if (GetFont().Ok())
134 {
135 // GetFont()->RealizeResource();
136 if (GetFont().GetResourceHandle())
137 {
138 if ( m_staticMin )
139 SendMessage((HWND)m_staticMin,WM_SETFONT,
140 (WPARAM)GetFont().GetResourceHandle(),0L);
141 if ( m_staticMax )
142 SendMessage((HWND)m_staticMax,WM_SETFONT,
143 (WPARAM)GetFont().GetResourceHandle(),0L);
144 if (m_staticValue)
145 SendMessage((HWND)m_staticValue,WM_SETFONT,
146 (WPARAM)GetFont().GetResourceHandle(),0L);
147 }
148 }
149
150 SetSize(x, y, width, height);
151 SetValue(value);
152
153 return TRUE;
154 }
155
156 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
157 WXWORD pos, WXHWND control)
158 {
159 int position = ::GetScrollPos((HWND)control, SB_CTL);
160
161 int nScrollInc;
162 wxEventType scrollEvent = wxEVT_NULL;
163 switch ( wParam )
164 {
165 case SB_TOP:
166 nScrollInc = m_rangeMax - position;
167 scrollEvent = wxEVT_SCROLL_TOP;
168 break;
169
170 case SB_BOTTOM:
171 nScrollInc = - position;
172 scrollEvent = wxEVT_SCROLL_BOTTOM;
173 break;
174
175 case SB_LINEUP:
176 nScrollInc = - GetLineSize();
177 scrollEvent = wxEVT_SCROLL_LINEUP;
178 break;
179
180 case SB_LINEDOWN:
181 nScrollInc = GetLineSize();
182 scrollEvent = wxEVT_SCROLL_LINEDOWN;
183 break;
184
185 case SB_PAGEUP:
186 nScrollInc = -GetPageSize();
187 scrollEvent = wxEVT_SCROLL_PAGEUP;
188 break;
189
190 case SB_PAGEDOWN:
191 nScrollInc = GetPageSize();
192 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
193 break;
194
195 case SB_THUMBTRACK:
196 case SB_THUMBPOSITION:
197 #ifdef __WIN32__
198 nScrollInc = (signed short)pos - position;
199 #else
200 nScrollInc = pos - position;
201 #endif
202 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
203 break;
204
205 default:
206 nScrollInc = 0;
207 }
208
209 if (nScrollInc == 0)
210 {
211 // no event...
212 return FALSE;
213 }
214
215 int newPos = position + nScrollInc;
216
217 if ( (newPos < GetMin()) || (newPos > GetMax()) )
218 {
219 // out of range - but we did process it
220 return TRUE;
221 }
222
223 SetValue(newPos);
224
225 wxScrollEvent event(scrollEvent, m_windowId);
226 event.SetPosition(newPos);
227 event.SetEventObject( this );
228 GetEventHandler()->ProcessEvent(event);
229
230 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
231 cevent.SetInt( newPos );
232 cevent.SetEventObject( this );
233
234 return GetEventHandler()->ProcessEvent( cevent );
235 }
236
237 wxSliderMSW::~wxSliderMSW()
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
247 int wxSliderMSW::GetValue() const
248 {
249 return ::GetScrollPos(GetHwnd(), SB_CTL);
250 }
251
252 void wxSliderMSW::SetValue(int value)
253 {
254 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
255 if (m_staticValue)
256 {
257 wxSprintf(wxBuffer, wxT("%d"), value);
258 SetWindowText((HWND) m_staticValue, wxBuffer);
259 }
260 }
261
262 void wxSliderMSW::GetSize(int *width, int *height) const
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
280 void wxSliderMSW::GetPosition(int *x, int *y) const
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
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 }
312 *x = point.x;
313 *y = point.y;
314 }
315
316 // TODO one day, make sense of all this horros and replace it with a readable
317 // DoGetBestSize()
318 void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
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);
327 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
328 x1 = currentX;
329 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
330 y1 = currentY;
331
332 AdjustForParentClientOrigin(x1, y1, sizeFlags);
333
334 wxChar buf[300];
335
336 int x_offset = x;
337 int y_offset = y;
338
339 int cx; // slider,min,max sizes
340 int cy;
341 int cyf;
342
343 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
344
345 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
346 {
347 if ( m_windowStyle & wxSL_LABELS )
348 {
349 int min_len = 0;
350
351 GetWindowText((HWND) m_staticMin, buf, 300);
352 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
353
354 int max_len = 0;
355
356 GetWindowText((HWND) m_staticMax, buf, 300);
357 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
358 if (m_staticValue)
359 {
360 int new_width = (int)(wxMax(min_len, max_len));
361 int valueHeight = (int)cyf;
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;
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
376 int slider_height = cy;
377
378 // Slider must have a minimum/default length/height
379 if (slider_length < 100)
380 slider_length = 100;
381
382 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
383 x_offset += slider_length + cx;
384
385 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
386 }
387 else
388 {
389 // No labels
390 if ( w1 < 0 )
391 w1 = 200;
392 if ( h1 < 0 )
393 h1 = 20;
394 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
395 }
396 }
397 else
398 {
399 if ( m_windowStyle & wxSL_LABELS )
400 {
401 int min_len;
402 GetWindowText((HWND) m_staticMin, buf, 300);
403 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
404
405 int max_len;
406 GetWindowText((HWND) m_staticMax, buf, 300);
407 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
408
409 if (m_staticValue)
410 {
411 int new_width = (int)(wxMax(min_len, max_len));
412 int valueHeight = (int)cyf;
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
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);
430
431 // Use character height as an estimate of slider width (yes, width)
432 int slider_width = cy;
433
434 // Slider must have a minimum/default length
435 if (slider_length < 100)
436 slider_length = 100;
437
438 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
439 y_offset += slider_length;
440
441 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
442 }
443 else
444 {
445 // No labels
446 if ( w1 < 0 )
447 w1 = 20;
448 if ( h1 < 0 )
449 h1 = 200;
450 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
451 }
452 }
453 }
454
455 void wxSliderMSW::SetRange(int minValue, int maxValue)
456 {
457 m_rangeMin = minValue;
458 m_rangeMax = maxValue;
459
460 ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
461 wxChar buf[40];
462 if ( m_staticMin )
463 {
464 wxSprintf(buf, wxT("%d"), m_rangeMin);
465 SetWindowText((HWND) m_staticMin, buf);
466 }
467
468 if ( m_staticMax )
469 {
470 wxSprintf(buf, wxT("%d"), m_rangeMax);
471 SetWindowText((HWND) m_staticMax, buf);
472 }
473 }
474
475 WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
476 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
477 {
478 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
479 return 0;
480
481 // Otherwise, it's a static
482 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
483 }
484
485 // For trackbars only
486 void wxSliderMSW::SetTickFreq(int n, int pos)
487 {
488 }
489
490 void wxSliderMSW::SetPageSize(int pageSize)
491 {
492 m_pageSize = pageSize;
493 }
494
495 int wxSliderMSW::GetPageSize() const
496 {
497 return m_pageSize;
498 }
499
500 void wxSliderMSW::ClearSel()
501 {
502 }
503
504 void wxSliderMSW::ClearTicks()
505 {
506 }
507
508 void wxSliderMSW::SetLineSize(int lineSize)
509 {
510 m_lineSize = lineSize;
511 }
512
513 int wxSliderMSW::GetLineSize() const
514 {
515 return m_lineSize;
516 }
517
518 int wxSliderMSW::GetSelEnd() const
519 {
520 return 0;
521 }
522
523 int wxSliderMSW::GetSelStart() const
524 {
525 return 0;
526 }
527
528 void wxSliderMSW::SetSelection(int minPos, int maxPos)
529 {
530 }
531
532 void wxSliderMSW::SetThumbLength(int len)
533 {
534 }
535
536 int wxSliderMSW::GetThumbLength() const
537 {
538 return 0;
539 }
540
541 void wxSliderMSW::SetTick(int tickPos)
542 {
543 }
544
545 bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
546 {
547 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
548 }
549
550 void wxSliderMSW::Command (wxCommandEvent & event)
551 {
552 SetValue (event.GetInt());
553 ProcessCommand (event);
554 }
555
556 bool wxSliderMSW::Show(bool show)
557 {
558 wxWindow::Show(show);
559
560 int cshow;
561 if (show)
562 cshow = SW_SHOW;
563 else
564 cshow = SW_HIDE;
565
566 if(m_staticValue)
567 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
568 if(m_staticMin)
569 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
570 if(m_staticMax)
571 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
572 return TRUE;
573 }
574
575