]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
Consistent naming of e.g. wxUSE_CTRL
[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__)
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(void)
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, "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 sprintf(wxBuffer, "%d", minValue);
103 m_staticMin = (WXHWND) CreateWindowEx(0, "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, "");
153
154 SetFont(parent->GetFont());
155
156 if ( m_windowStyle & wxSL_LABELS )
157 {
158 // Finally, create max value static item
159 sprintf(wxBuffer, "%d", maxValue);
160 m_staticMax = (WXHWND) CreateWindowEx(0, "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 void wxSlider95::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control)
190 {
191 int position = 0; // Dummy - not used in this mode
192
193 int nScrollInc;
194 wxEventType scrollEvent = wxEVT_NULL;
195 switch ( wParam )
196 {
197 case SB_TOP:
198 nScrollInc = m_rangeMax - position;
199 scrollEvent = wxEVT_SCROLL_TOP;
200 break;
201
202 case SB_BOTTOM:
203 nScrollInc = - position;
204 scrollEvent = wxEVT_SCROLL_BOTTOM;
205 break;
206
207 case SB_LINEUP:
208 nScrollInc = - GetLineSize();
209 scrollEvent = wxEVT_SCROLL_LINEUP;
210 break;
211
212 case SB_LINEDOWN:
213 nScrollInc = GetLineSize();
214 scrollEvent = wxEVT_SCROLL_LINEDOWN;
215 break;
216
217 case SB_PAGEUP:
218 nScrollInc = -GetPageSize();
219 scrollEvent = wxEVT_SCROLL_PAGEUP;
220 break;
221
222 case SB_PAGEDOWN:
223 nScrollInc = GetPageSize();
224 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
225 break;
226
227 case SB_THUMBTRACK:
228 case SB_THUMBPOSITION:
229 #ifdef __WIN32__
230 nScrollInc = (signed short)pos - position;
231 #else
232 nScrollInc = pos - position;
233 #endif
234 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
235 break;
236
237 default:
238 nScrollInc = 0;
239 return;
240 }
241
242 {
243
244 int newPos = (int)::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
245 if (!(newPos < GetMin() || newPos > GetMax()))
246 {
247 SetValue(newPos);
248
249 wxScrollEvent event(scrollEvent, m_windowId);
250 event.SetPosition(newPos);
251 event.SetEventObject( this );
252 GetEventHandler()->ProcessEvent(event);
253
254 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
255 cevent.SetEventObject( this );
256 GetEventHandler()->ProcessEvent( cevent );
257
258 }
259 }
260 }
261
262 void wxSlider95::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control)
263 {
264 MSWOnVScroll(wParam, pos, control);
265 }
266
267 wxSlider95::~wxSlider95(void)
268 {
269 if (m_staticMin)
270 DestroyWindow((HWND) m_staticMin);
271 if (m_staticMax)
272 DestroyWindow((HWND) m_staticMax);
273 if (m_staticValue)
274 DestroyWindow((HWND) m_staticValue);
275 }
276
277 int wxSlider95::GetValue(void) const
278 {
279 return ::SendMessage((HWND) GetHWND(), TBM_GETPOS, 0, 0);
280 }
281
282 void wxSlider95::SetValue(int value)
283 {
284 ::SendMessage((HWND) GetHWND(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)value);
285 if (m_staticValue)
286 {
287 sprintf(wxBuffer, "%d", value);
288 SetWindowText((HWND) m_staticValue, wxBuffer);
289 }
290 }
291
292 void wxSlider95::GetSize(int *width, int *height) const
293 {
294 RECT rect;
295 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
296
297 wxFindMaxSize(GetHWND(), &rect);
298
299 if (m_staticMin)
300 wxFindMaxSize(m_staticMin, &rect);
301 if (m_staticMax)
302 wxFindMaxSize(m_staticMax, &rect);
303 if (m_staticValue)
304 wxFindMaxSize(m_staticValue, &rect);
305
306 *width = rect.right - rect.left;
307 *height = rect.bottom - rect.top;
308 }
309
310 void wxSlider95::GetPosition(int *x, int *y) const
311 {
312 wxWindow *parent = GetParent();
313 RECT rect;
314 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
315
316 wxFindMaxSize(GetHWND(), &rect);
317
318 if (m_staticMin)
319 wxFindMaxSize(m_staticMin, &rect);
320 if (m_staticMax)
321 wxFindMaxSize(m_staticMax, &rect);
322 if (m_staticValue)
323 wxFindMaxSize(m_staticValue, &rect);
324
325 // Since we now have the absolute screen coords,
326 // if there's a parent we must subtract its top left corner
327 POINT point;
328 point.x = rect.left;
329 point.y = rect.top;
330 if (parent)
331 ::ScreenToClient((HWND) parent->GetHWND(), &point);
332
333 // We may be faking the client origin.
334 // So a window that's really at (0, 30) may appear
335 // (to wxWin apps) to be at (0, 0).
336 if (GetParent())
337 {
338 wxPoint pt(GetParent()->GetClientAreaOrigin());
339 point.x -= pt.x;
340 point.y -= pt.y;
341 }
342 *x = point.x;
343 *y = point.y;
344 }
345
346 void wxSlider95::SetSize(int x, int y, int width, int height, int sizeFlags)
347 {
348 int x1 = x;
349 int y1 = y;
350 int w1 = width;
351 int h1 = height;
352
353 int currentX, currentY;
354 GetPosition(&currentX, &currentY);
355 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
356 x1 = currentX;
357 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
358 y1 = currentY;
359
360 AdjustForParentClientOrigin(x1, y1, sizeFlags);
361
362 char buf[300];
363
364 int x_offset = x;
365 int y_offset = y;
366
367 int cx; // slider,min,max sizes
368 int cy;
369 int cyf;
370
371 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
372
373 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
374 {
375 if ( m_windowStyle & wxSL_LABELS )
376 {
377 int min_len = 0;
378
379 GetWindowText((HWND) m_staticMin, buf, 300);
380 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
381
382 int max_len = 0;
383
384 GetWindowText((HWND) m_staticMax, buf, 300);
385 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
386 if (m_staticValue)
387 {
388 int new_width = (int)(wxMax(min_len, max_len));
389 int valueHeight = (int)cyf;
390 #ifdef __WIN32__
391 // For some reason, under Win95, the text edit control has
392 // a lot of space before the first character
393 new_width += 3*cx;
394 #endif
395 // The height needs to be a bit bigger under Win95 if using native
396 // 3D effects.
397 valueHeight = (int) (valueHeight * 1.5) ;
398 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
399 x_offset += new_width + cx;
400 }
401
402 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
403 x_offset += (int)(min_len + cx);
404
405 int slider_length = (int)(w1 - x_offset - max_len - cx);
406
407 int slider_height = h1;
408 if (slider_height < 0 )
409 slider_height = 20;
410
411 // Slider must have a minimum/default length/height
412 if (slider_length < 100)
413 slider_length = 100;
414
415 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_length, slider_height, TRUE);
416 x_offset += slider_length + cx;
417
418 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
419 }
420 else
421 {
422 // No labels
423 // If we're prepared to use the existing size, then...
424 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
425 {
426 GetSize(&w1, &h1);
427 }
428 if ( w1 < 0 )
429 w1 = 200;
430 if ( h1 < 0 )
431 h1 = 20;
432 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
433 }
434 }
435 else
436 {
437 if ( m_windowStyle & wxSL_LABELS )
438 {
439 int min_len;
440 GetWindowText((HWND) m_staticMin, buf, 300);
441 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
442
443 int max_len;
444 GetWindowText((HWND) m_staticMax, buf, 300);
445 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
446
447 if (m_staticValue)
448 {
449 int new_width = (int)(wxMax(min_len, max_len));
450 int valueHeight = (int)cyf;
451 /*** Suggested change by George Tasker - remove this block...
452 #ifdef __WIN32__
453 // For some reason, under Win95, the text edit control has
454 // a lot of space before the first character
455 new_width += 3*cx;
456 #endif
457 ... and replace with following line: */
458 new_width += cx;
459
460 // The height needs to be a bit bigger under Win95 if using native
461 // 3D effects.
462 valueHeight = (int) (valueHeight * 1.5) ;
463
464 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
465 y_offset += valueHeight;
466 }
467
468 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
469 y_offset += cy;
470
471 int slider_length = (int)(h1 - y_offset - cy - cy);
472
473 int slider_width = w1;
474 if (slider_width < 0 )
475 slider_width = 20;
476
477 // Slider must have a minimum/default length
478 if (slider_length < 100)
479 slider_length = 100;
480
481 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_width, slider_length, TRUE);
482 y_offset += slider_length;
483
484 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
485 }
486 else
487 {
488 // No labels
489 // If we're prepared to use the existing size, then...
490 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
491 {
492 GetSize(&w1, &h1);
493 }
494 if ( w1 < 0 )
495 w1 = 20;
496 if ( h1 < 0 )
497 h1 = 200;
498 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
499 }
500 }
501 }
502
503 void wxSlider95::SetRange(int minValue, int maxValue)
504 {
505 m_rangeMin = minValue;
506 m_rangeMax = maxValue;
507
508 ::SendMessage((HWND) GetHWND(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
509
510 char buf[40];
511 if ( m_staticMin )
512 {
513 sprintf(buf, "%d", m_rangeMin);
514 SetWindowText((HWND) m_staticMin, buf);
515 }
516
517 if ( m_staticMax )
518 {
519 sprintf(buf, "%d", m_rangeMax);
520 SetWindowText((HWND) m_staticMax, buf);
521 }
522 }
523
524 WXHBRUSH wxSlider95::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
525 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
526 {
527 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
528 return 0;
529
530 // Otherwise, it's a static
531 if (GetParent()->GetTransparentBackground())
532 SetBkMode((HDC) pDC, TRANSPARENT);
533 else
534 SetBkMode((HDC) pDC, OPAQUE);
535
536 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
537 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
538
539 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
540 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
541 }
542
543 // For trackbars only
544 void wxSlider95::SetTickFreq(int n, int pos)
545 {
546 m_tickFreq = n;
547 ::SendMessage( (HWND) GetHWND(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
548 }
549
550 void wxSlider95::SetPageSize(int pageSize)
551 {
552 ::SendMessage( (HWND) GetHWND(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
553 m_pageSize = pageSize;
554 }
555
556 int wxSlider95::GetPageSize(void) const
557 {
558 return m_pageSize;
559 }
560
561 void wxSlider95::ClearSel(void)
562 {
563 ::SendMessage( (HWND) GetHWND(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0 );
564 }
565
566 void wxSlider95::ClearTicks(void)
567 {
568 ::SendMessage( (HWND) GetHWND(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0 );
569 }
570
571 void wxSlider95::SetLineSize(int lineSize)
572 {
573 m_lineSize = lineSize;
574 ::SendMessage( (HWND) GetHWND(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize );
575 }
576
577 int wxSlider95::GetLineSize(void) const
578 {
579 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETLINESIZE, (WPARAM) 0, (LPARAM) 0 );
580 }
581
582 int wxSlider95::GetSelEnd(void) const
583 {
584 return (int) ::SendMessage( (HWND) GetHWND(), TBM_SETSELEND, (WPARAM) 0, (LPARAM) 0 );
585 }
586
587 int wxSlider95::GetSelStart(void) const
588 {
589 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETSELSTART, (WPARAM) 0, (LPARAM) 0 );
590 }
591
592 void wxSlider95::SetSelection(int minPos, int maxPos)
593 {
594 ::SendMessage( (HWND) GetHWND(), TBM_SETSEL, (WPARAM) TRUE, (LPARAM) MAKELONG( minPos, maxPos) );
595 }
596
597 void wxSlider95::SetThumbLength(int len)
598 {
599 ::SendMessage( (HWND) GetHWND(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0 );
600 }
601
602 int wxSlider95::GetThumbLength(void) const
603 {
604 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETTHUMBLENGTH, (WPARAM) 0, (LPARAM) 0 );
605 }
606
607 void wxSlider95::SetTick(int tickPos)
608 {
609 ::SendMessage( (HWND) GetHWND(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
610 }
611
612 bool wxSlider95::ContainsHWND(WXHWND hWnd) const
613 {
614 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
615 }
616
617 void wxSlider95::Command (wxCommandEvent & event)
618 {
619 SetValue (event.GetInt());
620 ProcessCommand (event);
621 }
622
623 bool wxSlider95::Show(bool show)
624 {
625 wxWindow::Show(show);
626
627 int cshow;
628 if (show)
629 cshow = SW_SHOW;
630 else
631 cshow = SW_HIDE;
632
633 if(m_staticValue)
634 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
635 if(m_staticMin)
636 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
637 if(m_staticMax)
638 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
639 return TRUE;
640 }
641
642 #endif
643 // __WIN95__
644