]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slidrmsw.cpp
Fix for mingw32 compilation (replaced #ifdef NM_CUSTOMDRAW with a test for _WIN32_IE
[wxWidgets.git] / src / msw / slidrmsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slidermsw.cpp
3 // Purpose: wxSliderMSW
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 "slidrmsw.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 #include "wx/utils.h"
28 #include "wx/brush.h"
29 #include "wx/msw/slidrmsw.h"
30 #include "wx/msw/private.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl)
33
34 // Slider
35 wxSliderMSW::wxSliderMSW()
36 {
37 m_staticValue = 0;
38 m_staticMin = 0;
39 m_staticMax = 0;
40 m_pageSize = 1;
41 m_lineSize = 1;
42 m_rangeMax = 0;
43 m_rangeMin = 0;
44 m_tickFreq = 0;
45 }
46
47 bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
48 int value, int minValue, int maxValue,
49 const wxPoint& pos,
50 const wxSize& size, long style,
51 const wxValidator& validator,
52 const wxString& name)
53 {
54 SetName(name);
55 SetValidator(validator);
56
57 if (parent) parent->AddChild(this);
58 SetBackgroundColour(parent->GetBackgroundColour()) ;
59 SetForegroundColour(parent->GetForegroundColour()) ;
60
61 m_staticValue = 0;
62 m_staticMin = 0;
63 m_staticMax = 0;
64 m_pageSize = 1;
65 m_lineSize = 1;
66 m_windowStyle = style;
67 m_tickFreq = 0;
68
69 if ( id == -1 )
70 m_windowId = (int)NewControlId();
71 else
72 m_windowId = id;
73
74 int x = pos.x;
75 int y = pos.y;
76 int width = size.x;
77 int height = size.y;
78
79 // non-Win95 implementation
80
81 long msStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
82
83 bool want3D;
84 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
85
86 m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL,
87 msStyle,
88 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
89 wxGetInstance(), NULL);
90
91 // Now create min static control
92 wxSprintf(wxBuffer, wxT("%d"), minValue);
93 m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
94 STATIC_FLAGS,
95 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
96 wxGetInstance(), NULL);
97
98 // Now create slider
99 m_windowId = (int)NewControlId();
100
101 msStyle = 0;
102 if (m_windowStyle & wxSL_VERTICAL)
103 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
104 else
105 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
106
107 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("SCROLLBAR"), wxBuffer,
108 msStyle,
109 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
110 wxGetInstance(), NULL);
111
112 m_pageSize = (int)((maxValue-minValue)/10);
113 m_rangeMax = maxValue;
114 m_rangeMin = minValue;
115
116 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
117 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
118 ShowWindow(scroll_bar, SW_SHOW);
119
120 m_hWnd = (WXHWND)scroll_bar;
121
122 // Subclass again for purposes of dialog editing mode
123 SubclassWin(GetHWND());
124
125 // Finally, create max value static item
126 wxSprintf(wxBuffer, wxT("%d"), maxValue);
127 m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), wxBuffer,
128 STATIC_FLAGS,
129 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
130 wxGetInstance(), NULL);
131
132 SetFont(parent->GetFont());
133
134 if (GetFont().Ok())
135 {
136 // GetFont()->RealizeResource();
137 if (GetFont().GetResourceHandle())
138 {
139 if ( m_staticMin )
140 SendMessage((HWND)m_staticMin,WM_SETFONT,
141 (WPARAM)GetFont().GetResourceHandle(),0L);
142 if ( m_staticMax )
143 SendMessage((HWND)m_staticMax,WM_SETFONT,
144 (WPARAM)GetFont().GetResourceHandle(),0L);
145 if (m_staticValue)
146 SendMessage((HWND)m_staticValue,WM_SETFONT,
147 (WPARAM)GetFont().GetResourceHandle(),0L);
148 }
149 }
150
151 SetSize(x, y, width, height);
152 SetValue(value);
153
154 return TRUE;
155 }
156
157 bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
158 WXWORD pos, WXHWND control)
159 {
160 int position = ::GetScrollPos((HWND)control, SB_CTL);
161
162 int nScrollInc;
163 wxEventType scrollEvent = wxEVT_NULL;
164 switch ( wParam )
165 {
166 case SB_TOP:
167 nScrollInc = m_rangeMax - position;
168 scrollEvent = wxEVT_SCROLL_TOP;
169 break;
170
171 case SB_BOTTOM:
172 nScrollInc = - position;
173 scrollEvent = wxEVT_SCROLL_BOTTOM;
174 break;
175
176 case SB_LINEUP:
177 nScrollInc = - GetLineSize();
178 scrollEvent = wxEVT_SCROLL_LINEUP;
179 break;
180
181 case SB_LINEDOWN:
182 nScrollInc = GetLineSize();
183 scrollEvent = wxEVT_SCROLL_LINEDOWN;
184 break;
185
186 case SB_PAGEUP:
187 nScrollInc = -GetPageSize();
188 scrollEvent = wxEVT_SCROLL_PAGEUP;
189 break;
190
191 case SB_PAGEDOWN:
192 nScrollInc = GetPageSize();
193 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
194 break;
195
196 case SB_THUMBTRACK:
197 case SB_THUMBPOSITION:
198 #ifdef __WIN32__
199 nScrollInc = (signed short)pos - position;
200 #else
201 nScrollInc = pos - position;
202 #endif
203 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
204 break;
205
206 default:
207 nScrollInc = 0;
208 }
209
210 if (nScrollInc == 0)
211 {
212 // no event...
213 return FALSE;
214 }
215
216 int newPos = position + nScrollInc;
217
218 if ( (newPos < GetMin()) || (newPos > GetMax()) )
219 {
220 // out of range - but we did process it
221 return TRUE;
222 }
223
224 SetValue(newPos);
225
226 wxScrollEvent event(scrollEvent, m_windowId);
227 event.SetPosition(newPos);
228 event.SetEventObject( this );
229 GetEventHandler()->ProcessEvent(event);
230
231 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
232 cevent.SetEventObject( this );
233
234 return GetEventHandler()->ProcessEvent( cevent );
235 }
236
237 wxSliderMSW::~wxSliderMSW()
238 {
239 if (m_staticMin)
240 DestroyWindow((HWND) m_staticMin);
241 if (m_staticMax)
242 DestroyWindow((HWND) m_staticMax);
243 if (m_staticValue)
244 DestroyWindow((HWND) m_staticValue);
245 }
246
247 int wxSliderMSW::GetValue() const
248 {
249 return ::GetScrollPos(GetHwnd(), SB_CTL);
250 }
251
252 void wxSliderMSW::SetValue(int value)
253 {
254 ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE);
255 if (m_staticValue)
256 {
257 wxSprintf(wxBuffer, wxT("%d"), value);
258 SetWindowText((HWND) m_staticValue, wxBuffer);
259 }
260 }
261
262 void wxSliderMSW::GetSize(int *width, int *height) const
263 {
264 RECT rect;
265 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
266
267 wxFindMaxSize(GetHWND(), &rect);
268
269 if (m_staticMin)
270 wxFindMaxSize(m_staticMin, &rect);
271 if (m_staticMax)
272 wxFindMaxSize(m_staticMax, &rect);
273 if (m_staticValue)
274 wxFindMaxSize(m_staticValue, &rect);
275
276 *width = rect.right - rect.left;
277 *height = rect.bottom - rect.top;
278 }
279
280 void wxSliderMSW::GetPosition(int *x, int *y) const
281 {
282 wxWindow *parent = GetParent();
283 RECT rect;
284 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
285
286 wxFindMaxSize(GetHWND(), &rect);
287
288 if (m_staticMin)
289 wxFindMaxSize(m_staticMin, &rect);
290 if (m_staticMax)
291 wxFindMaxSize(m_staticMax, &rect);
292 if (m_staticValue)
293 wxFindMaxSize(m_staticValue, &rect);
294
295 // Since we now have the absolute screen coords,
296 // if there's a parent we must subtract its top left corner
297 POINT point;
298 point.x = rect.left;
299 point.y = rect.top;
300 if (parent)
301 ::ScreenToClient((HWND) parent->GetHWND(), &point);
302
303 // We may be faking the client origin.
304 // So a window that's really at (0, 30) may appear
305 // (to wxWin apps) to be at (0, 0).
306 if (GetParent())
307 {
308 wxPoint pt(GetParent()->GetClientAreaOrigin());
309 point.x -= pt.x;
310 point.y -= pt.y;
311 }
312 *x = point.x;
313 *y = point.y;
314 }
315
316 // TODO one day, make sense of all this horros and replace it with a readable
317 // DoGetBestSize()
318 void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
319 {
320 int x1 = x;
321 int y1 = y;
322 int w1 = width;
323 int h1 = height;
324
325 int currentX, currentY;
326 GetPosition(&currentX, &currentY);
327 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
328 x1 = currentX;
329 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
330 y1 = currentY;
331
332 AdjustForParentClientOrigin(x1, y1, sizeFlags);
333
334 wxChar buf[300];
335
336 int x_offset = x;
337 int y_offset = y;
338
339 int cx; // slider,min,max sizes
340 int cy;
341 int cyf;
342
343 wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont());
344
345 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
346 {
347 if ( m_windowStyle & wxSL_LABELS )
348 {
349 int min_len = 0;
350
351 GetWindowText((HWND) m_staticMin, buf, 300);
352 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
353
354 int max_len = 0;
355
356 GetWindowText((HWND) m_staticMax, buf, 300);
357 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
358 if (m_staticValue)
359 {
360 int new_width = (int)(wxMax(min_len, max_len));
361 int valueHeight = (int)cyf;
362 #ifdef __WIN32__
363 // For some reason, under Win95, the text edit control has
364 // a lot of space before the first character
365 new_width += 3*cx;
366 #endif
367 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
368 x_offset += new_width + cx;
369 }
370
371 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
372 x_offset += (int)(min_len + cx);
373
374 int slider_length = (int)(w1 - x_offset - max_len - cx);
375
376 int slider_height = cy;
377
378 // Slider must have a minimum/default length/height
379 if (slider_length < 100)
380 slider_length = 100;
381
382 MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE);
383 x_offset += slider_length + cx;
384
385 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
386 }
387 else
388 {
389 // No labels
390 if ( w1 < 0 )
391 w1 = 200;
392 if ( h1 < 0 )
393 h1 = 20;
394 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
395 }
396 }
397 else
398 {
399 if ( m_windowStyle & wxSL_LABELS )
400 {
401 int min_len;
402 GetWindowText((HWND) m_staticMin, buf, 300);
403 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont());
404
405 int max_len;
406 GetWindowText((HWND) m_staticMax, buf, 300);
407 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
408
409 if (m_staticValue)
410 {
411 int new_width = (int)(wxMax(min_len, max_len));
412 int valueHeight = (int)cyf;
413 /*** Suggested change by George Tasker - remove this block...
414 #ifdef __WIN32__
415 // For some reason, under Win95, the text edit control has
416 // a lot of space before the first character
417 new_width += 3*cx;
418 #endif
419 ... and replace with following line: */
420 new_width += cx;
421
422 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
423 y_offset += valueHeight;
424 }
425
426 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
427 y_offset += cy;
428
429 int slider_length = (int)(h1 - y_offset - cy - cy);
430
431 // Use character height as an estimate of slider width (yes, width)
432 int slider_width = cy;
433
434 // Slider must have a minimum/default length
435 if (slider_length < 100)
436 slider_length = 100;
437
438 MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE);
439 y_offset += slider_length;
440
441 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
442 }
443 else
444 {
445 // No labels
446 if ( w1 < 0 )
447 w1 = 20;
448 if ( h1 < 0 )
449 h1 = 200;
450 MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
451 }
452 }
453 }
454
455 void wxSliderMSW::SetRange(int minValue, int maxValue)
456 {
457 m_rangeMin = minValue;
458 m_rangeMax = maxValue;
459
460 ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
461 wxChar buf[40];
462 if ( m_staticMin )
463 {
464 wxSprintf(buf, wxT("%d"), m_rangeMin);
465 SetWindowText((HWND) m_staticMin, buf);
466 }
467
468 if ( m_staticMax )
469 {
470 wxSprintf(buf, wxT("%d"), m_rangeMax);
471 SetWindowText((HWND) m_staticMax, buf);
472 }
473 }
474
475 WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
476 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
477 {
478 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
479 return 0;
480
481 // Otherwise, it's a static
482 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
483 }
484
485 // For trackbars only
486 void wxSliderMSW::SetTickFreq(int n, int pos)
487 {
488 }
489
490 void wxSliderMSW::SetPageSize(int pageSize)
491 {
492 m_pageSize = pageSize;
493 }
494
495 int wxSliderMSW::GetPageSize() const
496 {
497 return m_pageSize;
498 }
499
500 void wxSliderMSW::ClearSel()
501 {
502 }
503
504 void wxSliderMSW::ClearTicks()
505 {
506 }
507
508 void wxSliderMSW::SetLineSize(int lineSize)
509 {
510 m_lineSize = lineSize;
511 }
512
513 int wxSliderMSW::GetLineSize() const
514 {
515 return m_lineSize;
516 }
517
518 int wxSliderMSW::GetSelEnd() const
519 {
520 return 0;
521 }
522
523 int wxSliderMSW::GetSelStart() const
524 {
525 return 0;
526 }
527
528 void wxSliderMSW::SetSelection(int minPos, int maxPos)
529 {
530 }
531
532 void wxSliderMSW::SetThumbLength(int len)
533 {
534 }
535
536 int wxSliderMSW::GetThumbLength() const
537 {
538 return 0;
539 }
540
541 void wxSliderMSW::SetTick(int tickPos)
542 {
543 }
544
545 bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
546 {
547 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
548 }
549
550 void wxSliderMSW::Command (wxCommandEvent & event)
551 {
552 SetValue (event.GetInt());
553 ProcessCommand (event);
554 }
555
556 bool wxSliderMSW::Show(bool show)
557 {
558 wxWindow::Show(show);
559
560 int cshow;
561 if (show)
562 cshow = SW_SHOW;
563 else
564 cshow = SW_HIDE;
565
566 if(m_staticValue)
567 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
568 if(m_staticMin)
569 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
570 if(m_staticMax)
571 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
572 return TRUE;
573 }
574
575