]> git.saurik.com Git - wxWidgets.git/blame - src/msw/slidrmsw.cpp
Moved Get/SetToolBar down into frame.h/cpp
[wxWidgets.git] / src / msw / slidrmsw.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
da87a1ca
JS
2// Name: slidermsw.cpp
3// Purpose: wxSliderMSW
2bda0e17
KB
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__
da87a1ca 13#pragma implementation "slidrmsw.h"
2bda0e17
KB
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>
2bda0e17
KB
25#endif
26
da87a1ca 27#include "wx/msw/slidrmsw.h"
2bda0e17
KB
28#include "wx/msw/private.h"
29
2bda0e17 30#if !USE_SHARED_LIBRARY
da87a1ca 31IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl)
2bda0e17
KB
32
33#if WXWIN_COMPATIBILITY
da87a1ca
JS
34BEGIN_EVENT_TABLE(wxSliderMSW, wxControl)
35 EVT_SCROLL(wxSliderMSW::OnScroll)
2bda0e17
KB
36END_EVENT_TABLE()
37#endif
38
39#endif
40
41// Slider
da87a1ca 42wxSliderMSW::wxSliderMSW(void)
2bda0e17
KB
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
debe6624
JS
54bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id,
55 int value, int minValue, int maxValue,
2bda0e17 56 const wxPoint& pos,
debe6624 57 const wxSize& size, long style,
2bda0e17
KB
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->GetDefaultBackgroundColour()) ;
66 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
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
2bda0e17
KB
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 }
2bda0e17
KB
157
158 SetSize(x, y, width, height);
159 SetValue(value);
160
161 return TRUE;
162}
163
debe6624 164void wxSliderMSW::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control)
2bda0e17 165{
2bda0e17 166 int position = ::GetScrollPos((HWND)control, SB_CTL);
2bda0e17
KB
167
168 int nScrollInc;
7798a18e 169 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
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
2bda0e17 217 if (nScrollInc != 0)
2bda0e17
KB
218 {
219
2bda0e17 220 int newPos = position + nScrollInc;
da87a1ca 221
2bda0e17
KB
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
debe6624 234void wxSliderMSW::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control)
2bda0e17
KB
235{
236 MSWOnVScroll(wParam, pos, control);
237}
238
da87a1ca 239wxSliderMSW::~wxSliderMSW(void)
2bda0e17
KB
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
da87a1ca 249int wxSliderMSW::GetValue(void) const
2bda0e17 250{
2bda0e17 251 return ::GetScrollPos((HWND) GetHWND(), SB_CTL);
2bda0e17
KB
252}
253
debe6624 254void wxSliderMSW::SetValue(int value)
2bda0e17 255{
2bda0e17 256 ::SetScrollPos((HWND) GetHWND(), SB_CTL, value, TRUE);
2bda0e17
KB
257 if (m_staticValue)
258 {
259 sprintf(wxBuffer, "%d", value);
260 SetWindowText((HWND) m_staticValue, wxBuffer);
261 }
262}
263
da87a1ca 264void wxSliderMSW::GetSize(int *width, int *height) const
2bda0e17
KB
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
da87a1ca 282void wxSliderMSW::GetPosition(int *x, int *y) const
2bda0e17
KB
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 *x = point.x;
306 *y = point.y;
307}
308
debe6624 309void wxSliderMSW::SetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17
KB
310{
311 int x1 = x;
312 int y1 = y;
313 int w1 = width;
314 int h1 = height;
315
316 int currentX, currentY;
317 GetPosition(&currentX, &currentY);
318 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
319 x1 = currentX;
320 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
321 y1 = currentY;
322
323 char buf[300];
324
325 int x_offset = x;
326 int y_offset = y;
327
328 int cx; // slider,min,max sizes
329 int cy;
debe6624 330 int cyf;
2bda0e17
KB
331
332 wxGetCharSize(GetHWND(), &cx, &cy,GetFont());
333
334 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
335 {
336 if ( m_windowStyle & wxSL_LABELS )
337 {
debe6624 338 int min_len = 0;
2bda0e17
KB
339
340 GetWindowText((HWND) m_staticMin, buf, 300);
341 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, GetFont());
342
debe6624 343 int max_len = 0;
2bda0e17
KB
344
345 GetWindowText((HWND) m_staticMax, buf, 300);
346 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, GetFont());
347 if (m_staticValue)
348 {
349 int new_width = (int)(wxMax(min_len, max_len));
350 int valueHeight = (int)cyf;
351#ifdef __WIN32__
352 // For some reason, under Win95, the text edit control has
353 // a lot of space before the first character
354 new_width += 3*cx;
2bda0e17
KB
355#endif
356 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
357 x_offset += new_width + cx;
358 }
359
360 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
361 x_offset += (int)(min_len + cx);
362
363 int slider_length = (int)(w1 - x_offset - max_len - cx);
364
2bda0e17 365 int slider_height = cy;
2bda0e17
KB
366
367 // Slider must have a minimum/default length/height
368 if (slider_length < 100)
369 slider_length = 100;
370
371 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_length, slider_height, TRUE);
372 x_offset += slider_length + cx;
373
374 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
375 }
376 else
377 {
378 // No labels
379 if ( w1 < 0 )
380 w1 = 200;
381 if ( h1 < 0 )
382 h1 = 20;
383 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
384 }
385 }
386 else
387 {
388 if ( m_windowStyle & wxSL_LABELS )
389 {
debe6624 390 int min_len;
2bda0e17
KB
391 GetWindowText((HWND) m_staticMin, buf, 300);
392 GetTextExtent(buf, &min_len, &cyf,NULL,NULL,GetFont());
393
debe6624 394 int max_len;
2bda0e17
KB
395 GetWindowText((HWND) m_staticMax, buf, 300);
396 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, GetFont());
397
398 if (m_staticValue)
399 {
400 int new_width = (int)(wxMax(min_len, max_len));
401 int valueHeight = (int)cyf;
402/*** Suggested change by George Tasker - remove this block...
403#ifdef __WIN32__
404 // For some reason, under Win95, the text edit control has
405 // a lot of space before the first character
406 new_width += 3*cx;
407#endif
408 ... and replace with following line: */
409 new_width += cx;
410
2bda0e17
KB
411 MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE);
412 y_offset += valueHeight;
413 }
414
415 MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE);
416 y_offset += cy;
417
418 int slider_length = (int)(h1 - y_offset - cy - cy);
da87a1ca 419
2bda0e17
KB
420 // Use character height as an estimate of slider width (yes, width)
421 int slider_width = cy;
2bda0e17
KB
422
423 // Slider must have a minimum/default length
424 if (slider_length < 100)
425 slider_length = 100;
426
427 MoveWindow((HWND) GetHWND(), x_offset, y_offset, slider_width, slider_length, TRUE);
428 y_offset += slider_length;
429
430 MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE);
431 }
432 else
433 {
434 // No labels
435 if ( w1 < 0 )
436 w1 = 20;
437 if ( h1 < 0 )
438 h1 = 200;
439 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
440 }
441 }
2bda0e17
KB
442}
443
debe6624 444void wxSliderMSW::SetRange(int minValue, int maxValue)
2bda0e17
KB
445{
446 m_rangeMin = minValue;
447 m_rangeMax = maxValue;
448
2bda0e17 449 ::SetScrollRange((HWND) GetHWND(), SB_CTL, m_rangeMin, m_rangeMax, TRUE);
2bda0e17
KB
450 char buf[40];
451 if ( m_staticMin )
452 {
453 sprintf(buf, "%d", m_rangeMin);
454 SetWindowText((HWND) m_staticMin, buf);
455 }
456
457 if ( m_staticMax )
458 {
459 sprintf(buf, "%d", m_rangeMax);
460 SetWindowText((HWND) m_staticMax, buf);
461 }
462}
463
debe6624 464WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
2bda0e17
KB
465 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
466{
467 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
468 return 0;
469
470 // Otherwise, it's a static
471 if (GetParent()->GetTransparentBackground())
472 SetBkMode((HDC) pDC, TRANSPARENT);
473 else
474 SetBkMode((HDC) pDC, OPAQUE);
475
476 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
477 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
478
479 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
480
2bda0e17
KB
481 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
482}
483
484// For trackbars only
debe6624 485void wxSliderMSW::SetTickFreq(int n, int pos)
2bda0e17 486{
2bda0e17
KB
487}
488
debe6624 489void wxSliderMSW::SetPageSize(int pageSize)
2bda0e17 490{
2bda0e17
KB
491 m_pageSize = pageSize;
492}
493
da87a1ca 494int wxSliderMSW::GetPageSize(void) const
2bda0e17
KB
495{
496 return m_pageSize;
497}
498
da87a1ca 499void wxSliderMSW::ClearSel(void)
2bda0e17 500{
2bda0e17
KB
501}
502
da87a1ca 503void wxSliderMSW::ClearTicks(void)
2bda0e17 504{
2bda0e17
KB
505}
506
debe6624 507void wxSliderMSW::SetLineSize(int lineSize)
2bda0e17
KB
508{
509 m_lineSize = lineSize;
2bda0e17
KB
510}
511
da87a1ca 512int wxSliderMSW::GetLineSize(void) const
2bda0e17 513{
2bda0e17 514 return m_lineSize;
2bda0e17
KB
515}
516
da87a1ca 517int wxSliderMSW::GetSelEnd(void) const
2bda0e17 518{
2bda0e17 519 return 0;
2bda0e17
KB
520}
521
da87a1ca 522int wxSliderMSW::GetSelStart(void) const
2bda0e17 523{
2bda0e17 524 return 0;
2bda0e17
KB
525}
526
debe6624 527void wxSliderMSW::SetSelection(int minPos, int maxPos)
2bda0e17 528{
2bda0e17
KB
529}
530
debe6624 531void wxSliderMSW::SetThumbLength(int len)
2bda0e17 532{
2bda0e17
KB
533}
534
da87a1ca 535int wxSliderMSW::GetThumbLength(void) const
2bda0e17 536{
2bda0e17 537 return 0;
2bda0e17
KB
538}
539
debe6624 540void wxSliderMSW::SetTick(int tickPos)
2bda0e17 541{
2bda0e17
KB
542}
543
da87a1ca 544bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const
2bda0e17
KB
545{
546 return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() );
547}
548
549#if WXWIN_COMPATIBILITY
550// Backward compatibility
da87a1ca 551void wxSliderMSW::OnScroll(wxScrollEvent& event)
2bda0e17 552{
7798a18e 553 wxEventType oldEvent = event.GetEventType();
2bda0e17
KB
554 event.SetEventType( wxEVT_COMMAND_SLIDER_UPDATED );
555 if ( !GetEventHandler()->ProcessEvent(event) )
556 {
557 event.SetEventType( oldEvent );
558 if (!GetParent()->GetEventHandler()->ProcessEvent(event))
559 event.Skip();
560 }
561}
562#endif
563
da87a1ca 564void wxSliderMSW::Command (wxCommandEvent & event)
2bda0e17
KB
565{
566 SetValue (event.GetInt());
567 ProcessCommand (event);
568}
569
debe6624 570bool wxSliderMSW::Show(bool show)
2bda0e17
KB
571{
572 wxWindow::Show(show);
573
574 int cshow;
575 if (show)
576 cshow = SW_SHOW;
577 else
578 cshow = SW_HIDE;
579
580 if(m_staticValue)
581 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
582 if(m_staticMin)
583 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
584 if(m_staticMax)
585 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
586 return TRUE;
587}
588
589