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