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