wxWindow::GetBestSize() added
[wxWidgets.git] / src / msw / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinButton
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "spinbutt.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 // Can't resolve reference to CreateUpDownControl in
36 // TWIN32, but could probably use normal CreateWindow instead.
37
38 #if wxUSE_SPINBTN
39
40 #if defined(__WIN95__) && !defined(__TWIN32__)
41
42 #include "wx/spinbutt.h"
43 #include "wx/msw/private.h"
44
45 #if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
46 #include <commctrl.h>
47 #endif
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxWin macros
55 // ----------------------------------------------------------------------------
56
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
59 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // wxSpinButton
64 // ----------------------------------------------------------------------------
65
66 bool wxSpinButton::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 // basic initialization
74 InitBase();
75
76 m_windowId = (id == -1) ? NewControlId() : id;
77
78 m_backgroundColour = parent->GetBackgroundColour() ;
79 m_foregroundColour = parent->GetForegroundColour() ;
80
81 SetName(name);
82
83 int x = pos.x;
84 int y = pos.y;
85 int width = size.x;
86 int height = size.y;
87
88 m_windowStyle = style;
89
90 SetParent(parent);
91
92 // get the right size for the control
93 if ( width <= 0 || height <= 0 )
94 {
95 wxSize size = DoGetBestSize();
96 if ( width <= 0 )
97 width = size.x;
98 if ( height <= 0 )
99 height = size.y;
100 }
101
102 if ( x < 0 )
103 x = 0;
104 if ( y < 0 )
105 y = 0;
106
107 // translate the styles
108 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
109 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
110
111 if ( m_windowStyle & wxSP_HORIZONTAL )
112 wstyle |= UDS_HORZ;
113 if ( m_windowStyle & wxSP_ARROW_KEYS )
114 wstyle |= UDS_ARROWKEYS;
115 if ( m_windowStyle & wxSP_WRAP )
116 wstyle |= UDS_WRAP;
117
118 // create the UpDown control.
119 m_hWnd = (WXHWND)CreateUpDownControl
120 (
121 wstyle,
122 x, y, width, height,
123 GetHwndOf(parent),
124 m_windowId,
125 wxGetInstance(),
126 NULL, // no buddy
127 m_max, m_min,
128 m_min // initial position
129 );
130
131 if ( !m_hWnd )
132 {
133 wxLogLastError("CreateUpDownControl");
134
135 return FALSE;
136 }
137
138 if ( parent )
139 {
140 parent->AddChild(this);
141 }
142
143 SubclassWin(m_hWnd);
144
145 return TRUE;
146 }
147
148 wxSpinButton::~wxSpinButton()
149 {
150 }
151
152 // ----------------------------------------------------------------------------
153 // size calculation
154 // ----------------------------------------------------------------------------
155
156 wxSize wxSpinButton::DoGetBestSize() const
157 {
158 if ( (GetWindowStyle() & wxSP_VERTICAL) != 0 )
159 {
160 // vertical control
161 return wxSize(GetSystemMetrics(SM_CXVSCROLL),
162 2*GetSystemMetrics(SM_CYVSCROLL));
163 }
164 else
165 {
166 // horizontal control
167 return wxSize(2*GetSystemMetrics(SM_CXHSCROLL),
168 GetSystemMetrics(SM_CYHSCROLL));
169 }
170 }
171
172 // ----------------------------------------------------------------------------
173 // Attributes
174 // ----------------------------------------------------------------------------
175
176 int wxSpinButton::GetValue() const
177 {
178 return (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
179 }
180
181 void wxSpinButton::SetValue(int val)
182 {
183 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, (LPARAM) MAKELONG((short) val, 0));
184 }
185
186 void wxSpinButton::SetRange(int minVal, int maxVal)
187 {
188 wxSpinButtonBase::SetRange(minVal, maxVal);
189 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
190 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
191 }
192
193 bool wxSpinButton::MSWOnScroll(int orientation, WXWORD wParam,
194 WXWORD pos, WXHWND control)
195 {
196 wxCHECK_MSG( control, FALSE, wxT("scrolling what?") )
197
198 if ( wParam != SB_THUMBPOSITION )
199 {
200 // probable SB_ENDSCROLL - we don't react to it
201 return FALSE;
202 }
203
204 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
205 event.SetPosition((short)pos); // cast is important for negative values!
206 event.SetEventObject(this);
207
208 return GetEventHandler()->ProcessEvent(event);
209 }
210
211 bool wxSpinButton::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
212 {
213 #ifndef __GNUWIN32__
214 #if defined(__BORLANDC__) || defined(__WATCOMC__)
215 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
216 #elif defined(__VISUALC__) && (__VISUALC__ >= 1000) && (__VISUALC__ < 1020)
217 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
218 #else
219 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
220 #endif
221
222 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
223 : wxEVT_SCROLL_LINEDOWN,
224 m_windowId);
225 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
226 event.SetEventObject(this);
227
228 bool processed = GetEventHandler()->ProcessEvent(event);
229
230 *result = event.IsAllowed() ? 0 : 1;
231
232 return processed;
233 #else // GnuWin32
234 return FALSE;
235 #endif
236 }
237
238 bool wxSpinButton::MSWCommand(WXUINT cmd, WXWORD id)
239 {
240 // No command messages
241 return FALSE;
242 }
243
244 #endif // __WIN95__
245
246 #endif
247 // wxUSE_SPINCTN
248