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