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