]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slidrmsw.cpp
no message
[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/msw/slidrmsw.h"
28 #include "wx/msw/private.h"
29
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl)
32
33 #if WXWIN_COMPATIBILITY
34 BEGIN_EVENT_TABLE(wxSliderMSW, wxControl)
35 EVT_SCROLL(wxSliderMSW::OnScroll)
36 END_EVENT_TABLE()
37 #endif
38
39 #endif
40
41 // Slider
42 wxSliderMSW::wxSliderMSW(void)
43 {
44 m_staticValue = 0;
45 m_staticMin = 0;
46 m_staticMax = 0;
47 m_pageSize = 1;
48 m_lineSize = 1;
49 m_rangeMax = 0;
50 m_rangeMin = 0;
51 m_tickFreq = 0;
52 }
53
54 bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
55 int value, int minValue, int maxValue,
56 const wxPoint& pos,
57 const wxSize& size, long style,
58 const wxValidator& validator,
59 const wxString& name)
60 {
61 SetName(name);
62 SetValidator(validator);
63
64 if (parent) parent->AddChild(this);
65 SetBackgroundColour(parent->GetBackgroundColour()) ;
66 SetForegroundColour(parent->GetForegroundColour()) ;
67
68 m_staticValue = 0;
69 m_staticMin = 0;
70 m_staticMax = 0;
71 m_pageSize = 1;
72 m_lineSize = 1;
73 m_windowStyle = style;
74 m_tickFreq = 0;
75
76 if ( id == -1 )
77 m_windowId = (int)NewControlId();
78 else
79 m_windowId = id;
80
81 int x = pos.x;
82 int y = pos.y;
83 int width = size.x;
84 int height = size.y;
85
86 // non-Win95 implementation
87
88 long msStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER;
89
90 bool want3D;
91 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
92
93 m_staticValue = (WXHWND) CreateWindowEx(exStyle, "STATIC", NULL,
94 msStyle,
95 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
96 wxGetInstance(), NULL);
97
98 // Now create min static control
99 sprintf(wxBuffer, "%d", minValue);
100 m_staticMin = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
101 STATIC_FLAGS,
102 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
103 wxGetInstance(), NULL);
104
105 // Now create slider
106 m_windowId = (int)NewControlId();
107
108 msStyle = 0;
109 if (m_windowStyle & wxSL_VERTICAL)
110 msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
111 else
112 msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
113
114 HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(m_windowStyle), "SCROLLBAR", wxBuffer,
115 msStyle,
116 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
117 wxGetInstance(), NULL);
118
119 m_pageSize = (int)((maxValue-minValue)/10);
120 m_rangeMax = maxValue;
121 m_rangeMin = minValue;
122
123 ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE);
124 ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE);
125 ShowWindow(scroll_bar, SW_SHOW);
126
127 m_hWnd = (WXHWND)scroll_bar;
128
129 // Subclass again for purposes of dialog editing mode
130 SubclassWin(GetHWND());
131
132 // Finally, create max value static item
133 sprintf(wxBuffer, "%d", maxValue);
134 m_staticMax = (WXHWND) CreateWindowEx(0, "STATIC", wxBuffer,
135 STATIC_FLAGS,
136 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
137 wxGetInstance(), NULL);
138
139 SetFont(* parent->GetFont());
140
141 if (GetFont())
142 {
143 // GetFont()->RealizeResource();
144 if (GetFont()->GetResourceHandle())
145 {
146 if ( m_staticMin )
147 SendMessage((HWND)m_staticMin,WM_SETFONT,
148 (WPARAM)GetFont()->GetResourceHandle(),0L);
149 if ( m_staticMax )
150 SendMessage((HWND)m_staticMax,WM_SETFONT,
151 (WPARAM)GetFont()->GetResourceHandle(),0L);
152 if (m_staticValue)
153 SendMessage((HWND)m_staticValue,WM_SETFONT,
154 (WPARAM)GetFont()->GetResourceHandle(),0L);
155 }
156 }
157
158 SetSize(x, y, width, height);
159 SetValue(value);
160
161 return TRUE;
162 }
163
164 void wxSliderMSW::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control)
165 {
166 int position = ::GetScrollPos((HWND)control, SB_CTL);
167
168 int nScrollInc;
169 wxEventType scrollEvent = wxEVT_NULL;
170 switch ( wParam )
171 {
172 case SB_TOP:
173 nScrollInc = m_rangeMax - position;
174 scrollEvent = wxEVT_SCROLL_TOP;
175 break;
176
177 case SB_BOTTOM:
178 nScrollInc = - position;
179 scrollEvent = wxEVT_SCROLL_BOTTOM;
180 break;
181
182 case SB_LINEUP:
183 nScrollInc = - GetLineSize();
184 scrollEvent = wxEVT_SCROLL_LINEUP;
185 break;
186
187 case SB_LINEDOWN:
188 nScrollInc = GetLineSize();
189 scrollEvent = wxEVT_SCROLL_LINEDOWN;
190 break;
191
192 case SB_PAGEUP:
193 nScrollInc = -GetPageSize();
194 scrollEvent = wxEVT_SCROLL_PAGEUP;
195 break;
196
197 case SB_PAGEDOWN:
198 nScrollInc = GetPageSize();
199 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
200 break;
201
202 case SB_THUMBTRACK:
203 case SB_THUMBPOSITION:
204 #ifdef __WIN32__
205 nScrollInc = (signed short)pos - position;
206 #else
207 nScrollInc = pos - position;
208 #endif
209 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
210 break;
211
212 default:
213 nScrollInc = 0;
214 return;
215 }
216
217 if (nScrollInc != 0)
218 {
219
220 int newPos = position + nScrollInc;
221
222 if (!(newPos < GetMin() || newPos > GetMax()))
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 }
232 }
233
234 void wxSliderMSW::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control)
235 {
236 MSWOnVScroll(wParam, pos, control);
237 }
238
239 wxSliderMSW::~wxSliderMSW(void)
240 {
241 if (m_staticMin)
242 DestroyWindow((HWND) m_staticMin);
243 if (m_staticMax)
244 DestroyWindow((HWND) m_staticMax);
245 if (m_staticValue)
246 DestroyWindow((HWND) m_staticValue);
247 }
248
249 int wxSliderMSW::GetValue(void) const
250 {
251 return ::GetScrollPos((HWND) GetHWND(), SB_CTL);
252 }
253
254 void wxSliderMSW::SetValue(int value)
255 {
256 ::SetScrollPos((HWND) GetHWND(), SB_CTL, value, TRUE);
257 if (m_staticValue)
258 {
259 sprintf(wxBuffer, "%d", value);
260 SetWindowText((HWND) m_staticValue, wxBuffer);
261 }
262 }
263
264 void wxSliderMSW::GetSize(int *width, int *height) const
265 {
266 RECT rect;
267 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
268
269 wxFindMaxSize(GetHWND(), &rect);
270
271 if (m_staticMin)
272 wxFindMaxSize(m_staticMin, &rect);
273 if (m_staticMax)
274 wxFindMaxSize(m_staticMax, &rect);
275 if (m_staticValue)
276 wxFindMaxSize(m_staticValue, &rect);
277
278 *width = rect.right - rect.left;
279 *height = rect.bottom - rect.top;
280 }
281
282 void wxSliderMSW::GetPosition(int *x, int *y) const
283 {
284 wxWindow *parent = GetParent();
285 RECT rect;
286 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
287
288 wxFindMaxSize(GetHWND(), &rect);
289
290 if (m_staticMin)
291 wxFindMaxSize(m_staticMin, &rect);
292 if (m_staticMax)
293 wxFindMaxSize(m_staticMax, &rect);
294 if (m_staticValue)
295 wxFindMaxSize(m_staticValue, &rect);
296
297 // Since we now have the absolute screen coords,
298 // if there's a parent we must subtract its top left corner
299 POINT point;
300 point.x = rect.left;
301 point.y = rect.top;
302 if (parent)
303 ::ScreenToClient((HWND) parent->GetHWND(), &point);
304
305 // We may be faking the client origin.
306 // So a window that's really at (0, 30) may appear
307 // (to wxWin apps) to be at (0, 0).
308 if (GetParent())
309 {
310 wxPoint pt(GetParent()->GetClientAreaOrigin());
311 point.x -= pt.x;
312 point.y -= pt.y;
313 }
314 *x = point.x;
315 *y = point.y;
316 }
317
318 void wxSliderMSW::SetSize(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 char 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,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, GetFont());
353
354 int max_len = 0;
355
356 GetWindowText((HWND) m_staticMax, buf, 300);
357 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, 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((HWND) 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((HWND) 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,GetFont());
404
405 int max_len;
406 GetWindowText((HWND) m_staticMax, buf, 300);
407 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, 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((HWND) 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((HWND) 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((HWND) GetHWND(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
461 char buf[40];
462 if ( m_staticMin )
463 {
464 sprintf(buf, "%d", m_rangeMin);
465 SetWindowText((HWND) m_staticMin, buf);
466 }
467
468 if ( m_staticMax )
469 {
470 sprintf(buf, "%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 if (GetParent()->GetTransparentBackground())
483 SetBkMode((HDC) pDC, TRANSPARENT);
484 else
485 SetBkMode((HDC) pDC, OPAQUE);
486
487 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
488 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
489
490 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
491
492 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
493 }
494
495 // For trackbars only
496 void wxSliderMSW::SetTickFreq(int n, int pos)
497 {
498 }
499
500 void wxSliderMSW::SetPageSize(int pageSize)
501 {
502 m_pageSize = pageSize;
503 }
504
505 int wxSliderMSW::GetPageSize(void) const
506 {
507 return m_pageSize;
508 }
509
510 void wxSliderMSW::ClearSel(void)
511 {
512 }
513
514 void wxSliderMSW::ClearTicks(void)
515 {
516 }
517
518 void wxSliderMSW::SetLineSize(int lineSize)
519 {
520 m_lineSize = lineSize;
521 }
522
523 int wxSliderMSW::GetLineSize(void) const
524 {
525 return m_lineSize;
526 }
527
528 int wxSliderMSW::GetSelEnd(void) const
529 {
530 return 0;
531 }
532
533 int wxSliderMSW::GetSelStart(void) const
534 {
535 return 0;
536 }
537
538 void wxSliderMSW::SetSelection(int minPos, int maxPos)
539 {
540 }
541
542 void wxSliderMSW::SetThumbLength(int len)
543 {
544 }
545
546 int wxSliderMSW::GetThumbLength(void) const
547 {
548 return 0;
549 }
550
551 void wxSliderMSW::SetTick(int tickPos)
552 {
553 }
554
555 bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
556 {
557 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
558 }
559
560 #if WXWIN_COMPATIBILITY
561 // Backward compatibility
562 void wxSliderMSW::OnScroll(wxScrollEvent& event)
563 {
564 wxEventType oldEvent = event.GetEventType();
565 event.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED );
566 if ( !GetEventHandler()->ProcessEvent(event) )
567 {
568 event.SetEventType( oldEvent );
569 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
570 event.Skip();
571 }
572 }
573 #endif
574
575 void wxSliderMSW::Command (wxCommandEvent & event)
576 {
577 SetValue (event.GetInt());
578 ProcessCommand (event);
579 }
580
581 bool wxSliderMSW::Show(bool show)
582 {
583 wxWindow::Show(show);
584
585 int cshow;
586 if (show)
587 cshow = SW_SHOW;
588 else
589 cshow = SW_HIDE;
590
591 if(m_staticValue)
592 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
593 if(m_staticMin)
594 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
595 if(m_staticMax)
596 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
597 return TRUE;
598 }
599
600