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