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