]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
Let wxToolBar assume that no orientation flag means horizontal.
[wxWidgets.git] / src / msw / slider95.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slider95.cpp
3 // Purpose: wxSlider95, using the Win95 trackbar control
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 "slider95.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 #if wxUSE_SLIDER
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #include "wx/brush.h"
28 #include "wx/slider.h"
29 #endif
30
31 #ifdef __WIN95__
32
33 #include "wx/msw/slider95.h"
34 #include "wx/msw/private.h"
35
36 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
37 #include <commctrl.h>
38 #endif
39
40 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
41
42 // Slider
43 wxSlider95::wxSlider95()
44 {
45 m_staticValue = (WXHWND) NULL;
46 m_staticMin = (WXHWND) NULL;
47 m_staticMax = (WXHWND) NULL;
48 m_pageSize = 1;
49 m_lineSize = 1;
50 m_rangeMax = 0;
51 m_rangeMin = 0;
52 m_tickFreq = 0;
53 }
54
55 bool wxSlider95::Create(wxWindow *parent, wxWindowID id,
56 int value, int minValue, int maxValue,
57 const wxPoint& pos,
58 const wxSize& size, long style,
59 const wxValidator& validator,
60 const wxString& name)
61 {
62 SetName(name);
63 #if wxUSE_VALIDATORS
64 SetValidator(validator);
65 #endif // wxUSE_VALIDATORS
66
67 if (parent) parent->AddChild(this);
68
69 SetBackgroundColour(parent->GetBackgroundColour()) ;
70 SetForegroundColour(parent->GetForegroundColour()) ;
71
72 m_staticValue = (WXHWND) NULL;;
73 m_staticMin = (WXHWND) NULL;;
74 m_staticMax = (WXHWND) NULL;;
75 m_pageSize = 1;
76 m_lineSize = 1;
77 m_windowStyle = style;
78 m_tickFreq = 0;
79
80 if ( id == -1 )
81 m_windowId = (int)NewControlId();
82 else
83 m_windowId = id;
84
85 int x = pos.x;
86 int y = pos.y;
87 int width = size.x;
88 int height = size.y;
89
90 long msStyle = 0;
91 long wstyle = 0;
92
93 if ( m_windowStyle & wxCLIP_SIBLINGS )
94 msStyle |= WS_CLIPSIBLINGS;
95
96 if ( m_windowStyle & wxSL_LABELS )
97 {
98 msStyle |= WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
99
100 bool want3D;
101 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
102
103 m_staticValue = (WXHWND) CreateWindowEx
104 (
105 exStyle, wxT("STATIC"), NULL,
106 msStyle,
107 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
108 wxGetInstance(), NULL
109 );
110
111 // Now create min static control
112 wxSprintf(wxBuffer, wxT("%d"), minValue);
113 wstyle = STATIC_FLAGS;
114 if ( m_windowStyle & wxCLIP_SIBLINGS )
115 msStyle |= WS_CLIPSIBLINGS;
116 m_staticMin = (WXHWND) CreateWindowEx
117 (
118 0, wxT("STATIC"), wxBuffer,
119 wstyle,
120 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
121 wxGetInstance(), NULL
122 );
123 }
124
125 msStyle = 0;
126
127 if ( m_windowStyle & wxCLIP_SIBLINGS )
128 msStyle |= WS_CLIPSIBLINGS;
129
130 if (m_windowStyle & wxSL_VERTICAL)
131 msStyle = TBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
132 else
133 msStyle = TBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
134
135 if ( m_windowStyle & wxSL_AUTOTICKS )
136 msStyle |= TBS_AUTOTICKS ;
137
138 if ( m_windowStyle & wxSL_LEFT )
139 msStyle |= TBS_LEFT;
140 else if ( m_windowStyle & wxSL_RIGHT )
141 msStyle |= TBS_RIGHT;
142 else if ( m_windowStyle & wxSL_TOP )
143 msStyle |= TBS_TOP;
144 else if ( m_windowStyle & wxSL_BOTTOM )
145 msStyle |= TBS_BOTTOM;
146 else if ( m_windowStyle & wxSL_BOTH )
147 msStyle |= TBS_BOTH;
148 else if ( ! (m_windowStyle & wxSL_AUTOTICKS) )
149 msStyle |= TBS_NOTICKS;
150
151 if ( m_windowStyle & wxSL_SELRANGE )
152 msStyle |= TBS_ENABLESELRANGE;
153
154 HWND scroll_bar = CreateWindowEx
155 (
156 MakeExtendedStyle(m_windowStyle), TRACKBAR_CLASS, wxBuffer,
157 msStyle,
158 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
159 wxGetInstance(), NULL
160 );
161
162 m_rangeMax = maxValue;
163 m_rangeMin = minValue;
164
165 m_pageSize = (int)((maxValue-minValue)/10);
166
167 ::SendMessage(scroll_bar, TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
168 ::SendMessage(scroll_bar, TBM_SETPOS, TRUE, (LPARAM)value);
169 ::SendMessage(scroll_bar, TBM_SETPAGESIZE, 0, (LPARAM)m_pageSize);
170
171 m_hWnd = (WXHWND)scroll_bar;
172
173 SubclassWin(GetHWND());
174
175 ::SetWindowText((HWND) m_hWnd, wxT(""));
176
177 SetFont(parent->GetFont());
178
179 if ( m_windowStyle & wxSL_LABELS )
180 {
181 // Finally, create max value static item
182 wxSprintf(wxBuffer, wxT("%d"), maxValue);
183 wstyle = STATIC_FLAGS;
184
185 if ( m_windowStyle & wxCLIP_SIBLINGS )
186 msStyle |= WS_CLIPSIBLINGS;
187
188 m_staticMax = (WXHWND) CreateWindowEx
189 (
190 0, wxT("STATIC"), wxBuffer,
191 wstyle,
192 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
193 wxGetInstance(), NULL
194 );
195
196
197 if (GetFont().Ok())
198 {
199 if (GetFont().GetResourceHandle())
200 {
201 if ( m_staticMin )
202 ::SendMessage((HWND) m_staticMin, WM_SETFONT,
203 (WPARAM) GetFont().GetResourceHandle(), 0L);
204
205 if ( m_staticMax )
206 ::SendMessage((HWND) m_staticMax, WM_SETFONT,
207 (WPARAM) GetFont().GetResourceHandle(), 0L);
208
209 if (m_staticValue)
210 ::SendMessage((HWND) m_staticValue, WM_SETFONT,
211 (WPARAM) GetFont().GetResourceHandle(), 0L);
212 }
213 }
214 }
215
216 SetSize(x, y, width, height);
217 SetValue(value);
218
219 return TRUE;
220 }
221
222 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
223 WXWORD WXUNUSED(pos), WXHWND control)
224 {
225 wxEventType scrollEvent;
226 switch ( wParam )
227 {
228 case SB_TOP:
229 scrollEvent = wxEVT_SCROLL_TOP;
230 break;
231
232 case SB_BOTTOM:
233 scrollEvent = wxEVT_SCROLL_BOTTOM;
234 break;
235
236 case SB_LINEUP:
237 scrollEvent = wxEVT_SCROLL_LINEUP;
238 break;
239
240 case SB_LINEDOWN:
241 scrollEvent = wxEVT_SCROLL_LINEDOWN;
242 break;
243
244 case SB_PAGEUP:
245 scrollEvent = wxEVT_SCROLL_PAGEUP;
246 break;
247
248 case SB_PAGEDOWN:
249 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
250 break;
251
252 case SB_THUMBTRACK:
253 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
254 break;
255
256 case SB_THUMBPOSITION:
257 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
258 break;
259
260 case SB_ENDSCROLL:
261 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
262 break;
263
264 default:
265 // unknown scroll event?
266 return FALSE;
267 }
268
269 int newPos = (int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
270 if ( (newPos < GetMin()) || (newPos > GetMax()) )
271 {
272 // out of range - but we did process it
273 return TRUE;
274 }
275
276 SetValue(newPos);
277
278 wxScrollEvent event(scrollEvent, m_windowId);
279 event.SetPosition(newPos);
280 event.SetEventObject( this );
281 GetEventHandler()->ProcessEvent(event);
282
283 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
284 cevent.SetInt( newPos );
285 cevent.SetEventObject( this );
286
287 return GetEventHandler()->ProcessEvent( cevent );
288 }
289
290 wxSlider95::~wxSlider95()
291 {
292 if (m_staticMin)
293 {
294 ::DestroyWindow((HWND) m_staticMin);
295 m_staticMin = (WXHWND) NULL;
296 }
297
298 if (m_staticMax)
299 {
300 ::DestroyWindow((HWND) m_staticMax);
301 m_staticMax = (WXHWND) NULL;
302 }
303
304 if (m_staticValue)
305 {
306 ::DestroyWindow((HWND) m_staticValue);
307 m_staticValue = (WXHWND) NULL;
308 }
309 }
310
311 int wxSlider95::GetValue() const
312 {
313 return ::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0);
314 }
315
316 void wxSlider95::SetValue(int value)
317 {
318 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)value);
319
320 if (m_staticValue)
321 {
322 wxSprintf(wxBuffer, wxT("%d"), value);
323 ::SetWindowText((HWND) m_staticValue, wxBuffer);
324 }
325 }
326
327 void wxSlider95::DoGetSize(int *width, int *height) const
328 {
329 GetSize(width, height);
330 }
331
332 void wxSlider95::GetSize(int *width, int *height) const
333 {
334 RECT rect;
335 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
336
337 wxFindMaxSize(GetHWND(), &rect);
338
339 if (m_staticMin)
340 wxFindMaxSize(m_staticMin, &rect);
341
342 if (m_staticMax)
343 wxFindMaxSize(m_staticMax, &rect);
344
345 if (m_staticValue)
346 wxFindMaxSize(m_staticValue, &rect);
347
348 *width = rect.right - rect.left;
349 *height = rect.bottom - rect.top;
350 }
351
352 void wxSlider95::GetPosition(int *x, int *y) const
353 {
354 wxWindow *parent = GetParent();
355 RECT rect;
356 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
357
358 wxFindMaxSize(GetHWND(), &rect);
359
360 if (m_staticMin)
361 wxFindMaxSize(m_staticMin, &rect);
362 if (m_staticMax)
363 wxFindMaxSize(m_staticMax, &rect);
364 if (m_staticValue)
365 wxFindMaxSize(m_staticValue, &rect);
366
367 // Since we now have the absolute screen coords,
368 // if there's a parent we must subtract its top left corner
369 POINT point;
370 point.x = rect.left;
371 point.y = rect.top;
372 if (parent)
373 ::ScreenToClient((HWND) parent->GetHWND(), &point);
374
375 // We may be faking the client origin.
376 // So a window that's really at (0, 30) may appear
377 // (to wxWin apps) to be at (0, 0).
378 if (GetParent())
379 {
380 wxPoint pt(GetParent()->GetClientAreaOrigin());
381 point.x -= pt.x;
382 point.y -= pt.y;
383 }
384
385 *x = point.x;
386 *y = point.y;
387 }
388
389 // TODO one day, make sense of all this horros and replace it with a readable
390 // DoGetBestSize()
391 void wxSlider95::DoSetSize(int x, int y, int width, int height, int sizeFlags)
392 {
393 int x1 = x;
394 int y1 = y;
395 int w1 = width;
396 int h1 = height;
397
398 int currentX, currentY;
399 GetPosition(&currentX, &currentY);
400 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
401 x1 = currentX;
402 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
403 y1 = currentY;
404
405 AdjustForParentClientOrigin(x1, y1, sizeFlags);
406
407 wxChar buf[300];
408
409 int x_offset = x;
410 int y_offset = y;
411
412 int cx; // slider,min,max sizes
413 int cy;
414 int cyf;
415
416 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
417
418 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
419 {
420 if ( m_windowStyle & wxSL_LABELS )
421 {
422 int min_len = 0;
423
424 ::GetWindowText((HWND) m_staticMin, buf, 300);
425 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
426
427 int max_len = 0;
428
429 ::GetWindowText((HWND) m_staticMax, buf, 300);
430 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
431 if (m_staticValue)
432 {
433 int new_width = (int)(wxMax(min_len, max_len));
434 int valueHeight = (int)cyf;
435 #ifdef __WIN32__
436 // For some reason, under Win95, the text edit control has
437 // a lot of space before the first character
438 new_width += 3*cx;
439 #endif
440 // The height needs to be a bit bigger under Win95 if
441 // using native 3D effects.
442 valueHeight = (int) (valueHeight * 1.5) ;
443 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
444 new_width, valueHeight, TRUE);
445 x_offset += new_width + cx;
446 }
447
448 MoveWindow((HWND) m_staticMin, x_offset, y_offset,
449 (int) min_len, cy, TRUE);
450 x_offset += (int)(min_len + cx);
451
452 int slider_length = (int)(w1 - x_offset - max_len - cx);
453
454 int slider_height = h1;
455 if (slider_height < 0 )
456 slider_height = 20;
457
458 // Slider must have a minimum/default length/height
459 if (slider_length < 100)
460 slider_length = 100;
461
462 ::MoveWindow(GetHwnd(), x_offset, y_offset,
463 slider_length, slider_height, TRUE);
464 x_offset += slider_length + cx;
465
466 MoveWindow((HWND) m_staticMax, x_offset, y_offset,
467 (int) max_len, cy, TRUE);
468 }
469 else
470 {
471 // No labels
472 // If we're prepared to use the existing size, then...
473 if
474 (
475 width == -1
476 && height == -1
477 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
478 )
479 {
480 GetSize(&w1, &h1);
481 }
482
483 if ( w1 < 0 )
484 w1 = 200;
485 if ( h1 < 0 )
486 h1 = 20;
487
488 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
489 }
490 }
491 else
492 {
493 if ( m_windowStyle & wxSL_LABELS )
494 {
495 int min_len;
496 ::GetWindowText((HWND) m_staticMin, buf, 300);
497 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
498
499 int max_len;
500 ::GetWindowText((HWND) m_staticMax, buf, 300);
501 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
502
503 if (m_staticValue)
504 {
505 int new_width = (int)(wxMax(min_len, max_len));
506 int valueHeight = (int)cyf;
507 /*** Suggested change by George Tasker - remove this block...
508 #ifdef __WIN32__
509 // For some reason, under Win95, the text edit control has
510 // a lot of space before the first character
511 new_width += 3*cx;
512 #endif
513 ... and replace with following line: */
514 new_width += cx;
515
516 // The height needs to be a bit bigger under Win95 if
517 // using native 3D effects.
518 valueHeight = (int) (valueHeight * 1.5) ;
519
520 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
521 new_width, valueHeight, TRUE);
522 y_offset += valueHeight;
523 }
524
525 ::MoveWindow((HWND) m_staticMin, x_offset, y_offset,
526 (int) min_len, cy, TRUE);
527 y_offset += cy;
528
529 int slider_length = (int)(h1 - y_offset - cy - cy);
530
531 int slider_width = w1;
532 if (slider_width < 0 )
533 slider_width = 20;
534
535 // Slider must have a minimum/default length
536 if (slider_length < 100)
537 slider_length = 100;
538
539 ::MoveWindow(GetHwnd(), x_offset, y_offset,
540 slider_width, slider_length, TRUE);
541 y_offset += slider_length;
542
543 ::MoveWindow((HWND) m_staticMax, x_offset, y_offset,
544 (int)max_len, cy, TRUE);
545 }
546 else
547 {
548 // No labels
549 // If we're prepared to use the existing size, then...
550 if
551 (
552 width == -1 && height == -1
553 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
554 )
555 {
556 GetSize(&w1, &h1);
557 }
558
559 if ( w1 < 0 )
560 w1 = 20;
561 if ( h1 < 0 )
562 h1 = 200;
563
564 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
565 }
566 }
567 }
568
569 void wxSlider95::SetRange(int minValue, int maxValue)
570 {
571 m_rangeMin = minValue;
572 m_rangeMax = maxValue;
573
574 ::SendMessage(GetHwnd(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
575
576 wxChar buf[40];
577 if ( m_staticMin )
578 {
579 wxSprintf(buf, wxT("%d"), m_rangeMin);
580 ::SetWindowText((HWND) m_staticMin, buf);
581 }
582
583 if ( m_staticMax )
584 {
585 wxSprintf(buf, wxT("%d"), m_rangeMax);
586 ::SetWindowText((HWND) m_staticMax, buf);
587 }
588 }
589
590 WXHBRUSH wxSlider95::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
591 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
592 {
593 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
594 return 0;
595
596 // Otherwise, it's a static
597 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
598 }
599
600 // For trackbars only
601 void wxSlider95::SetTickFreq(int n, int pos)
602 {
603 m_tickFreq = n;
604 ::SendMessage( GetHwnd(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
605 }
606
607 void wxSlider95::SetPageSize(int pageSize)
608 {
609 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
610 m_pageSize = pageSize;
611 }
612
613 int wxSlider95::GetPageSize() const
614 {
615 return m_pageSize;
616 }
617
618 void wxSlider95::ClearSel()
619 {
620 ::SendMessage( GetHwnd(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0 );
621 }
622
623 void wxSlider95::ClearTicks()
624 {
625 ::SendMessage( GetHwnd(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0 );
626 }
627
628 void wxSlider95::SetLineSize(int lineSize)
629 {
630 m_lineSize = lineSize;
631 ::SendMessage( GetHwnd(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize );
632 }
633
634 int wxSlider95::GetLineSize() const
635 {
636 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE,
637 (WPARAM) 0, (LPARAM) 0 );
638 }
639
640 int wxSlider95::GetSelEnd() const
641 {
642 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND,
643 (WPARAM) 0, (LPARAM) 0 );
644 }
645
646 int wxSlider95::GetSelStart() const
647 {
648 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART,
649 (WPARAM) 0, (LPARAM) 0 );
650 }
651
652 void wxSlider95::SetSelection(int minPos, int maxPos)
653 {
654 ::SendMessage(GetHwnd(), TBM_SETSEL,
655 (WPARAM) TRUE, (LPARAM) MAKELONG( minPos, maxPos) );
656 }
657
658 void wxSlider95::SetThumbLength(int len)
659 {
660 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0 );
661 }
662
663 int wxSlider95::GetThumbLength() const
664 {
665 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH,
666 (WPARAM) 0, (LPARAM) 0 );
667 }
668
669 void wxSlider95::SetTick(int tickPos)
670 {
671 ::SendMessage( GetHwnd(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
672 }
673
674 bool wxSlider95::ContainsHWND(WXHWND hWnd) const
675 {
676 return
677 (
678 hWnd == GetStaticMin()
679 || hWnd == GetStaticMax()
680 || hWnd == GetEditValue()
681 );
682 }
683
684 void wxSlider95::Command (wxCommandEvent & event)
685 {
686 SetValue (event.GetInt());
687 ProcessCommand (event);
688 }
689
690 bool wxSlider95::Show(bool show)
691 {
692 wxWindow::Show(show);
693
694 int cshow;
695 if (show)
696 cshow = SW_SHOW;
697 else
698 cshow = SW_HIDE;
699
700 if(m_staticValue)
701 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
702
703 if(m_staticMin)
704 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
705
706 if(m_staticMax)
707 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
708
709 return TRUE;
710 }
711
712 #endif
713 // __WIN95__
714
715 #endif // wxUSE_SLIDER