]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
GDI double-deletion fix, wxBitmap depth bug
[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 #endif
26
27 #ifdef __WIN95__
28
29 #include "wx/msw/slider95.h"
30 #include "wx/msw/private.h"
31
32 #if defined(__WIN95__) && !defined(__GNUWIN32__)
33 #include <commctrl.h>
34 #endif
35
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxSlider95, wxControl)
38
39 #if WXWIN_COMPATIBILITY
40 BEGIN_EVENT_TABLE(wxSlider95, wxControl)
41 EVT_SCROLL(wxSlider95::OnScroll)
42 END_EVENT_TABLE()
43 #endif
44
45 #endif
46
47 // Slider
48 wxSlider95::wxSlider95(void)
49 {
50 m_staticValue = 0;
51 m_staticMin = 0;
52 m_staticMax = 0;
53 m_pageSize = 1;
54 m_lineSize = 1;
55 m_rangeMax = 0;
56 m_rangeMin = 0;
57 m_tickFreq = 0;
58 }
59
60 bool wxSlider95::Create(wxWindow *parent, wxWindowID id,
61 int value, int minValue, int maxValue,
62 const wxPoint& pos,
63 const wxSize& size, long style,
64 const wxValidator& validator,
65 const wxString& name)
66 {
67 SetName(name);
68 SetValidator(validator);
69
70 if (parent) parent->AddChild(this);
71 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
72 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
73
74 m_staticValue = 0;
75 m_staticMin = 0;
76 m_staticMax = 0;
77 m_pageSize = 1;
78 m_lineSize = 1;
79 m_windowStyle = style;
80 m_tickFreq = 0;
81
82 if ( id == -1 )
83 m_windowId = (int)NewControlId();
84 else
85 m_windowId = id;
86
87 int x = pos.x;
88 int y = pos.y;
89 int width = size.x;
90 int height = size.y;
91
92 long msStyle ;
93
94 if ( m_windowStyle & wxSL_LABELS )
95 {
96 msStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
97
98 bool want3D;
99 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
100
101 m_staticValue = (WXHWND) CreateWindowEx(exStyle, "STATIC", NULL,
102 msStyle,
103 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
104 wxGetInstance(), NULL);
105
106 // Now create min static control
107 sprintf(wxBuffer, "%d", minValue);
108 m_staticMin = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
109 STATIC_FLAGS,
110 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
111 wxGetInstance(), NULL);
112 }
113
114 msStyle = 0;
115 if (m_windowStyle & wxSL_VERTICAL)
116 msStyle = TBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
117 else
118 msStyle = TBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
119
120 if ( m_windowStyle & wxSL_AUTOTICKS )
121 msStyle |= TBS_AUTOTICKS ;
122
123 if ( m_windowStyle & wxSL_LEFT )
124 msStyle |= TBS_LEFT;
125 else if ( m_windowStyle & wxSL_RIGHT )
126 msStyle |= TBS_RIGHT;
127 else if ( m_windowStyle & wxSL_TOP )
128 msStyle |= TBS_TOP;
129 else if ( m_windowStyle & wxSL_BOTTOM )
130 msStyle |= TBS_BOTTOM;
131 else if ( m_windowStyle & wxSL_BOTH )
132 msStyle |= TBS_BOTH;
133 else if ( ! (m_windowStyle & wxSL_AUTOTICKS) )
134 msStyle |= TBS_NOTICKS;
135
136 if ( m_windowStyle & wxSL_SELRANGE )
137 msStyle |= TBS_ENABLESELRANGE ;
138
139 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), TRACKBAR_CLASS, wxBuffer,
140 msStyle,
141 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
142 wxGetInstance(), NULL);
143
144 m_rangeMax = maxValue;
145 m_rangeMin = minValue;
146
147 m_pageSize = (int)((maxValue-minValue)/10);
148
149 ::SendMessage(scroll_bar, TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
150 ::SendMessage(scroll_bar, TBM_SETPOS, TRUE, (LPARAM)value);
151 ::SendMessage(scroll_bar, TBM_SETPAGESIZE, 0, (LPARAM)m_pageSize);
152
153 m_hWnd = (WXHWND)scroll_bar;
154
155 SubclassWin(GetHWND());
156
157 SetWindowText((HWND) m_hWnd, "");
158
159 SetFont(* parent->GetFont());
160
161 if ( m_windowStyle & wxSL_LABELS )
162 {
163 // Finally, create max value static item
164 sprintf(wxBuffer, "%d", maxValue);
165 m_staticMax = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
166 STATIC_FLAGS,
167 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
168 wxGetInstance(), NULL);
169
170
171 if (GetFont())
172 {
173 // GetFont()->RealizeResource();
174 if (GetFont()->GetResourceHandle())
175 {
176 if ( m_staticMin )
177 SendMessage((HWND)m_staticMin,WM_SETFONT,
178 (WPARAM)GetFont()->GetResourceHandle(),0L);
179 if ( m_staticMax )
180 SendMessage((HWND)m_staticMax,WM_SETFONT,
181 (WPARAM)GetFont()->GetResourceHandle(),0L);
182 if (m_staticValue)
183 SendMessage((HWND)m_staticValue,WM_SETFONT,
184 (WPARAM)GetFont()->GetResourceHandle(),0L);
185 }
186 }
187 }
188
189 SetSize(x, y, width, height);
190 SetValue(value);
191
192 return TRUE;
193 }
194
195 void wxSlider95::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control)
196 {
197 int position = 0; // Dummy - not used in this mode
198
199 int nScrollInc;
200 wxEventType scrollEvent = wxEVT_NULL;
201 switch ( wParam )
202 {
203 case SB_TOP:
204 nScrollInc = m_rangeMax - position;
205 scrollEvent = wxEVT_SCROLL_TOP;
206 break;
207
208 case SB_BOTTOM:
209 nScrollInc = - position;
210 scrollEvent = wxEVT_SCROLL_BOTTOM;
211 break;
212
213 case SB_LINEUP:
214 nScrollInc = - GetLineSize();
215 scrollEvent = wxEVT_SCROLL_LINEUP;
216 break;
217
218 case SB_LINEDOWN:
219 nScrollInc = GetLineSize();
220 scrollEvent = wxEVT_SCROLL_LINEDOWN;
221 break;
222
223 case SB_PAGEUP:
224 nScrollInc = -GetPageSize();
225 scrollEvent = wxEVT_SCROLL_PAGEUP;
226 break;
227
228 case SB_PAGEDOWN:
229 nScrollInc = GetPageSize();
230 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
231 break;
232
233 case SB_THUMBTRACK:
234 case SB_THUMBPOSITION:
235 #ifdef __WIN32__
236 nScrollInc = (signed short)pos - position;
237 #else
238 nScrollInc = pos - position;
239 #endif
240 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
241 break;
242
243 default:
244 nScrollInc = 0;
245 return;
246 }
247
248 {
249
250 int newPos = (int)::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
251 if (!(newPos < GetMin() || newPos > GetMax()))
252 {
253 SetValue(newPos);
254
255 wxScrollEvent event(scrollEvent, m_windowId);
256 event.SetPosition(newPos);
257 event.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(event);
259 }
260 }
261 }
262
263 void wxSlider95::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control)
264 {
265 MSWOnVScroll(wParam, pos, control);
266 }
267
268 wxSlider95::~wxSlider95(void)
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(void) const
279 {
280 return ::SendMessage((HWND) GetHWND(), TBM_GETPOS, 0, 0);
281 }
282
283 void wxSlider95::SetValue(int value)
284 {
285 ::SendMessage((HWND) GetHWND(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)value);
286 if (m_staticValue)
287 {
288 sprintf(wxBuffer, "%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 void wxSlider95::SetSize(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 char 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,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, GetFont());
382
383 int max_len = 0;
384
385 GetWindowText((HWND) m_staticMax, buf, 300);
386 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, 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((HWND) 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((HWND) 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,GetFont());
443
444 int max_len;
445 GetWindowText((HWND) m_staticMax, buf, 300);
446 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, 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((HWND) 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((HWND) 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((HWND) GetHWND(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
510
511 char buf[40];
512 if ( m_staticMin )
513 {
514 sprintf(buf, "%d", m_rangeMin);
515 SetWindowText((HWND) m_staticMin, buf);
516 }
517
518 if ( m_staticMax )
519 {
520 sprintf(buf, "%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 if (GetParent()->GetTransparentBackground())
533 SetBkMode((HDC) pDC, TRANSPARENT);
534 else
535 SetBkMode((HDC) pDC, OPAQUE);
536
537 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
538 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
539
540 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
541 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
542 }
543
544 // For trackbars only
545 void wxSlider95::SetTickFreq(int n, int pos)
546 {
547 m_tickFreq = n;
548 ::SendMessage( (HWND) GetHWND(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
549 }
550
551 void wxSlider95::SetPageSize(int pageSize)
552 {
553 ::SendMessage( (HWND) GetHWND(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
554 m_pageSize = pageSize;
555 }
556
557 int wxSlider95::GetPageSize(void) const
558 {
559 return m_pageSize;
560 }
561
562 void wxSlider95::ClearSel(void)
563 {
564 ::SendMessage( (HWND) GetHWND(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0 );
565 }
566
567 void wxSlider95::ClearTicks(void)
568 {
569 ::SendMessage( (HWND) GetHWND(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0 );
570 }
571
572 void wxSlider95::SetLineSize(int lineSize)
573 {
574 m_lineSize = lineSize;
575 ::SendMessage( (HWND) GetHWND(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize );
576 }
577
578 int wxSlider95::GetLineSize(void) const
579 {
580 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETLINESIZE, (WPARAM) 0, (LPARAM) 0 );
581 }
582
583 int wxSlider95::GetSelEnd(void) const
584 {
585 return (int) ::SendMessage( (HWND) GetHWND(), TBM_SETSELEND, (WPARAM) 0, (LPARAM) 0 );
586 }
587
588 int wxSlider95::GetSelStart(void) const
589 {
590 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETSELSTART, (WPARAM) 0, (LPARAM) 0 );
591 }
592
593 void wxSlider95::SetSelection(int minPos, int maxPos)
594 {
595 ::SendMessage( (HWND) GetHWND(), TBM_SETSEL, (WPARAM) TRUE, (LPARAM) MAKELONG( minPos, maxPos) );
596 }
597
598 void wxSlider95::SetThumbLength(int len)
599 {
600 ::SendMessage( (HWND) GetHWND(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0 );
601 }
602
603 int wxSlider95::GetThumbLength(void) const
604 {
605 return (int) ::SendMessage( (HWND) GetHWND(), TBM_GETTHUMBLENGTH, (WPARAM) 0, (LPARAM) 0 );
606 }
607
608 void wxSlider95::SetTick(int tickPos)
609 {
610 ::SendMessage( (HWND) GetHWND(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
611 }
612
613 bool wxSlider95::ContainsHWND(WXHWND hWnd) const
614 {
615 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
616 }
617
618 #if WXWIN_COMPATIBILITY
619 // Backward compatibility
620 void wxSlider95::OnScroll(wxScrollEvent& event)
621 {
622 wxEventType oldEvent = event.GetEventType();
623 event.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED );
624 if ( !GetEventHandler()->ProcessEvent(event) )
625 {
626 event.SetEventType( oldEvent );
627 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
628 event.Skip();
629 }
630 }
631 #endif
632
633 void wxSlider95::Command (wxCommandEvent & event)
634 {
635 SetValue (event.GetInt());
636 ProcessCommand (event);
637 }
638
639 bool wxSlider95::Show(bool show)
640 {
641 wxWindow::Show(show);
642
643 int cshow;
644 if (show)
645 cshow = SW_SHOW;
646 else
647 cshow = SW_HIDE;
648
649 if(m_staticValue)
650 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
651 if(m_staticMin)
652 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
653 if(m_staticMax)
654 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
655 return TRUE;
656 }
657
658 #endif
659 // __WIN95__
660