]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
prevent the parent window from losing activation when a popup is shown
[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 case SB_THUMBPOSITION:
254 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
255 break;
256
257 default:
258 // unknown scroll event?
259 return FALSE;
260 }
261
262 int newPos = (int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
263 if ( (newPos < GetMin()) || (newPos > GetMax()) )
264 {
265 // out of range - but we did process it
266 return TRUE;
267 }
268
269 SetValue(newPos);
270
271 wxScrollEvent event(scrollEvent, m_windowId);
272 event.SetPosition(newPos);
273 event.SetEventObject( this );
274 GetEventHandler()->ProcessEvent(event);
275
276 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
277 cevent.SetInt( newPos );
278 cevent.SetEventObject( this );
279
280 return GetEventHandler()->ProcessEvent( cevent );
281 }
282
283 wxSlider95::~wxSlider95()
284 {
285 if (m_staticMin)
286 {
287 ::DestroyWindow((HWND) m_staticMin);
288 m_staticMin = (WXHWND) NULL;
289 }
290
291 if (m_staticMax)
292 {
293 ::DestroyWindow((HWND) m_staticMax);
294 m_staticMax = (WXHWND) NULL;
295 }
296
297 if (m_staticValue)
298 {
299 ::DestroyWindow((HWND) m_staticValue);
300 m_staticValue = (WXHWND) NULL;
301 }
302 }
303
304 int wxSlider95::GetValue() const
305 {
306 return ::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0);
307 }
308
309 void wxSlider95::SetValue(int value)
310 {
311 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)value);
312
313 if (m_staticValue)
314 {
315 wxSprintf(wxBuffer, wxT("%d"), value);
316 ::SetWindowText((HWND) m_staticValue, wxBuffer);
317 }
318 }
319
320 void wxSlider95::DoGetSize(int *width, int *height) const
321 {
322 GetSize(width, height);
323 }
324
325 void wxSlider95::GetSize(int *width, int *height) const
326 {
327 RECT rect;
328 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
329
330 wxFindMaxSize(GetHWND(), &rect);
331
332 if (m_staticMin)
333 wxFindMaxSize(m_staticMin, &rect);
334
335 if (m_staticMax)
336 wxFindMaxSize(m_staticMax, &rect);
337
338 if (m_staticValue)
339 wxFindMaxSize(m_staticValue, &rect);
340
341 *width = rect.right - rect.left;
342 *height = rect.bottom - rect.top;
343 }
344
345 void wxSlider95::GetPosition(int *x, int *y) const
346 {
347 wxWindow *parent = GetParent();
348 RECT rect;
349 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
350
351 wxFindMaxSize(GetHWND(), &rect);
352
353 if (m_staticMin)
354 wxFindMaxSize(m_staticMin, &rect);
355 if (m_staticMax)
356 wxFindMaxSize(m_staticMax, &rect);
357 if (m_staticValue)
358 wxFindMaxSize(m_staticValue, &rect);
359
360 // Since we now have the absolute screen coords,
361 // if there's a parent we must subtract its top left corner
362 POINT point;
363 point.x = rect.left;
364 point.y = rect.top;
365 if (parent)
366 ::ScreenToClient((HWND) parent->GetHWND(), &point);
367
368 // We may be faking the client origin.
369 // So a window that's really at (0, 30) may appear
370 // (to wxWin apps) to be at (0, 0).
371 if (GetParent())
372 {
373 wxPoint pt(GetParent()->GetClientAreaOrigin());
374 point.x -= pt.x;
375 point.y -= pt.y;
376 }
377
378 *x = point.x;
379 *y = point.y;
380 }
381
382 // TODO one day, make sense of all this horros and replace it with a readable
383 // DoGetBestSize()
384 void wxSlider95::DoSetSize(int x, int y, int width, int height, int sizeFlags)
385 {
386 int x1 = x;
387 int y1 = y;
388 int w1 = width;
389 int h1 = height;
390
391 int currentX, currentY;
392 GetPosition(&currentX, &currentY);
393 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
394 x1 = currentX;
395 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
396 y1 = currentY;
397
398 AdjustForParentClientOrigin(x1, y1, sizeFlags);
399
400 wxChar buf[300];
401
402 int x_offset = x;
403 int y_offset = y;
404
405 int cx; // slider,min,max sizes
406 int cy;
407 int cyf;
408
409 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
410
411 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
412 {
413 if ( m_windowStyle & wxSL_LABELS )
414 {
415 int min_len = 0;
416
417 ::GetWindowText((HWND) m_staticMin, buf, 300);
418 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
419
420 int max_len = 0;
421
422 ::GetWindowText((HWND) m_staticMax, buf, 300);
423 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
424 if (m_staticValue)
425 {
426 int new_width = (int)(wxMax(min_len, max_len));
427 int valueHeight = (int)cyf;
428 #ifdef __WIN32__
429 // For some reason, under Win95, the text edit control has
430 // a lot of space before the first character
431 new_width += 3*cx;
432 #endif
433 // The height needs to be a bit bigger under Win95 if
434 // using native 3D effects.
435 valueHeight = (int) (valueHeight * 1.5) ;
436 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
437 new_width, valueHeight, TRUE);
438 x_offset += new_width + cx;
439 }
440
441 MoveWindow((HWND) m_staticMin, x_offset, y_offset,
442 (int) min_len, cy, TRUE);
443 x_offset += (int)(min_len + cx);
444
445 int slider_length = (int)(w1 - x_offset - max_len - cx);
446
447 int slider_height = h1;
448 if (slider_height < 0 )
449 slider_height = 20;
450
451 // Slider must have a minimum/default length/height
452 if (slider_length < 100)
453 slider_length = 100;
454
455 ::MoveWindow(GetHwnd(), x_offset, y_offset,
456 slider_length, slider_height, TRUE);
457 x_offset += slider_length + cx;
458
459 MoveWindow((HWND) m_staticMax, x_offset, y_offset,
460 (int) max_len, cy, TRUE);
461 }
462 else
463 {
464 // No labels
465 // If we're prepared to use the existing size, then...
466 if
467 (
468 width == -1
469 && height == -1
470 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
471 )
472 {
473 GetSize(&w1, &h1);
474 }
475
476 if ( w1 < 0 )
477 w1 = 200;
478 if ( h1 < 0 )
479 h1 = 20;
480
481 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
482 }
483 }
484 else
485 {
486 if ( m_windowStyle & wxSL_LABELS )
487 {
488 int min_len;
489 ::GetWindowText((HWND) m_staticMin, buf, 300);
490 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
491
492 int max_len;
493 ::GetWindowText((HWND) m_staticMax, buf, 300);
494 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
495
496 if (m_staticValue)
497 {
498 int new_width = (int)(wxMax(min_len, max_len));
499 int valueHeight = (int)cyf;
500 /*** Suggested change by George Tasker - remove this block...
501 #ifdef __WIN32__
502 // For some reason, under Win95, the text edit control has
503 // a lot of space before the first character
504 new_width += 3*cx;
505 #endif
506 ... and replace with following line: */
507 new_width += cx;
508
509 // The height needs to be a bit bigger under Win95 if
510 // using native 3D effects.
511 valueHeight = (int) (valueHeight * 1.5) ;
512
513 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
514 new_width, valueHeight, TRUE);
515 y_offset += valueHeight;
516 }
517
518 ::MoveWindow((HWND) m_staticMin, x_offset, y_offset,
519 (int) min_len, cy, TRUE);
520 y_offset += cy;
521
522 int slider_length = (int)(h1 - y_offset - cy - cy);
523
524 int slider_width = w1;
525 if (slider_width < 0 )
526 slider_width = 20;
527
528 // Slider must have a minimum/default length
529 if (slider_length < 100)
530 slider_length = 100;
531
532 ::MoveWindow(GetHwnd(), x_offset, y_offset,
533 slider_width, slider_length, TRUE);
534 y_offset += slider_length;
535
536 ::MoveWindow((HWND) m_staticMax, x_offset, y_offset,
537 (int)max_len, cy, TRUE);
538 }
539 else
540 {
541 // No labels
542 // If we're prepared to use the existing size, then...
543 if
544 (
545 width == -1 && height == -1
546 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
547 )
548 {
549 GetSize(&w1, &h1);
550 }
551
552 if ( w1 < 0 )
553 w1 = 20;
554 if ( h1 < 0 )
555 h1 = 200;
556
557 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
558 }
559 }
560 }
561
562 void wxSlider95::SetRange(int minValue, int maxValue)
563 {
564 m_rangeMin = minValue;
565 m_rangeMax = maxValue;
566
567 ::SendMessage(GetHwnd(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
568
569 wxChar buf[40];
570 if ( m_staticMin )
571 {
572 wxSprintf(buf, wxT("%d"), m_rangeMin);
573 ::SetWindowText((HWND) m_staticMin, buf);
574 }
575
576 if ( m_staticMax )
577 {
578 wxSprintf(buf, wxT("%d"), m_rangeMax);
579 ::SetWindowText((HWND) m_staticMax, buf);
580 }
581 }
582
583 WXHBRUSH wxSlider95::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
584 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
585 {
586 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
587 return 0;
588
589 // Otherwise, it's a static
590 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
591 }
592
593 // For trackbars only
594 void wxSlider95::SetTickFreq(int n, int pos)
595 {
596 m_tickFreq = n;
597 ::SendMessage( GetHwnd(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
598 }
599
600 void wxSlider95::SetPageSize(int pageSize)
601 {
602 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
603 m_pageSize = pageSize;
604 }
605
606 int wxSlider95::GetPageSize() const
607 {
608 return m_pageSize;
609 }
610
611 void wxSlider95::ClearSel()
612 {
613 ::SendMessage( GetHwnd(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0 );
614 }
615
616 void wxSlider95::ClearTicks()
617 {
618 ::SendMessage( GetHwnd(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0 );
619 }
620
621 void wxSlider95::SetLineSize(int lineSize)
622 {
623 m_lineSize = lineSize;
624 ::SendMessage( GetHwnd(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize );
625 }
626
627 int wxSlider95::GetLineSize() const
628 {
629 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE,
630 (WPARAM) 0, (LPARAM) 0 );
631 }
632
633 int wxSlider95::GetSelEnd() const
634 {
635 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND,
636 (WPARAM) 0, (LPARAM) 0 );
637 }
638
639 int wxSlider95::GetSelStart() const
640 {
641 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART,
642 (WPARAM) 0, (LPARAM) 0 );
643 }
644
645 void wxSlider95::SetSelection(int minPos, int maxPos)
646 {
647 ::SendMessage(GetHwnd(), TBM_SETSEL,
648 (WPARAM) TRUE, (LPARAM) MAKELONG( minPos, maxPos) );
649 }
650
651 void wxSlider95::SetThumbLength(int len)
652 {
653 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0 );
654 }
655
656 int wxSlider95::GetThumbLength() const
657 {
658 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH,
659 (WPARAM) 0, (LPARAM) 0 );
660 }
661
662 void wxSlider95::SetTick(int tickPos)
663 {
664 ::SendMessage( GetHwnd(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
665 }
666
667 bool wxSlider95::ContainsHWND(WXHWND hWnd) const
668 {
669 return
670 (
671 hWnd == GetStaticMin()
672 || hWnd == GetStaticMax()
673 || hWnd == GetEditValue()
674 );
675 }
676
677 void wxSlider95::Command (wxCommandEvent & event)
678 {
679 SetValue (event.GetInt());
680 ProcessCommand (event);
681 }
682
683 bool wxSlider95::Show(bool show)
684 {
685 wxWindow::Show(show);
686
687 int cshow;
688 if (show)
689 cshow = SW_SHOW;
690 else
691 cshow = SW_HIDE;
692
693 if(m_staticValue)
694 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
695
696 if(m_staticMin)
697 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
698
699 if(m_staticMax)
700 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
701
702 return TRUE;
703 }
704
705 #endif
706 // __WIN95__
707
708 #endif // wxUSE_SLIDER