]> git.saurik.com Git - wxWidgets.git/blob - src/os2/spinctrl.cpp
Fixed wxToolbar95::FindToolForPosition
[wxWidgets.git] / src / os2 / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/15/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16
17 #ifdef __GNUG__
18 #pragma implementation "spinctrlbase.h"
19 #pragma implementation "spinctrl.h"
20 #endif
21
22 // ----------------------------------------------------------------------------
23 // headers
24 // ----------------------------------------------------------------------------
25
26 // for compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
28
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #if wxUSE_SPINBTN
35
36 #include "wx/spinctrl.h"
37 #include "wx/os2/private.h"
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
44
45 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
46 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
47 END_EVENT_TABLE()
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // the margin between the up-down control and its buddy
53 static const int MARGIN_BETWEEN = 5;
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // construction
61 // ----------------------------------------------------------------------------
62
63 bool wxSpinCtrl::Create(wxWindow *parent,
64 wxWindowID id,
65 const wxString& value,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 int min, int max, int initial,
70 const wxString& name)
71 {
72 // TODO:
73 /*
74 // before using DoGetBestSize(), have to set style to let the base class
75 // know whether this is a horizontal or vertical control (we're always
76 // vertical)
77 style |= wxSP_VERTICAL;
78 SetWindowStyle(style);
79
80 // calculate the sizes: the size given is the toal size for both controls
81 // and we need to fit them both in the given width (height is the same)
82 wxSize sizeText(size), sizeBtn(size);
83 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
84 if ( sizeText.x <= 0 )
85 {
86 // DEFAULT_ITEM_WIDTH is the default width for the text control
87 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
88 }
89
90 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
91 if ( sizeText.x <= 0 )
92 {
93 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
94 }
95
96 wxPoint posBtn(pos);
97 posBtn.x += sizeText.x + MARGIN_BETWEEN;
98
99 // create the spin button
100 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
101 {
102 return FALSE;
103 }
104
105 SetRange(min, max);
106 SetValue(initial);
107
108 // create the text window
109 m_hwndBuddy = (WXHWND)::CreateWindowEx
110 (
111 WS_EX_CLIENTEDGE, // sunken border
112 _T("EDIT"), // window class
113 NULL, // no window title
114 WS_CHILD | WS_BORDER, // style (will be shown later)
115 pos.x, pos.y, // position
116 0, 0, // size (will be set later)
117 GetHwndOf(parent), // parent
118 (HMENU)-1, // control id
119 wxGetInstance(), // app instance
120 NULL // unused client data
121 );
122
123 if ( !m_hwndBuddy )
124 {
125 wxLogLastError("CreateWindow(buddy text window)");
126
127 return FALSE;
128 }
129
130 // should have the same font as the other controls
131 SetFont(GetParent()->GetFont());
132
133 // set the size of the text window - can do it only now, because we
134 // couldn't call DoGetBestSize() before as font wasn't set
135 if ( sizeText.y <= 0 )
136 {
137 int cx, cy;
138 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
139
140 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
141 }
142
143 DoMoveWindow(pos.x, pos.y,
144 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
145
146 (void)::ShowWindow((HWND)m_hwndBuddy, SW_SHOW);
147
148 // associate the text window with the spin button
149 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
150
151 if ( !value.IsEmpty() )
152 {
153 SetValue(value);
154 }
155 */
156 return FALSE;
157 }
158
159 // ----------------------------------------------------------------------------
160 // wxTextCtrl-like methods
161 // ----------------------------------------------------------------------------
162
163 void wxSpinCtrl::SetValue(const wxString& text)
164 {
165 // TODO:
166 /*
167 if ( !::SetWindowText((HWND)m_hwndBuddy, text.c_str()) )
168 {
169 wxLogLastError("SetWindowText(buddy)");
170 }
171 */
172 }
173
174 int wxSpinCtrl::GetValue() const
175 {
176 wxString val = wxGetWindowText(m_hwndBuddy);
177
178 long n;
179 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
180 n = INT_MIN;
181
182 return n;
183 }
184
185 // ----------------------------------------------------------------------------
186 // forward some methods to subcontrols
187 // ----------------------------------------------------------------------------
188
189 bool wxSpinCtrl::SetFont(const wxFont& font)
190 {
191 if ( !wxWindowBase::SetFont(font) )
192 {
193 // nothing to do
194 return FALSE;
195 }
196
197 WXHANDLE hFont = GetFont().GetResourceHandle();
198 // TODO:
199 /*
200 (void)::SendMessage((HWND)m_hwndBuddy, WM_SETFONT, (WPARAM)hFont, TRUE);
201 */
202 return TRUE;
203 }
204
205 bool wxSpinCtrl::Show(bool show)
206 {
207 if ( !wxControl::Show(show) )
208 {
209 return FALSE;
210 }
211
212 // TODO:
213 /*
214 ::ShowWindow((HWND)m_hwndBuddy, show ? SW_SHOW : SW_HIDE);
215 */
216 return TRUE;
217 }
218
219 bool wxSpinCtrl::Enable(bool enable)
220 {
221 if ( !wxControl::Enable(enable) )
222 {
223 return FALSE;
224 }
225
226 // TODO:
227 /*
228 ::EnableWindow((HWND)m_hwndBuddy, enable);
229 */
230 return TRUE;
231 }
232
233 // ----------------------------------------------------------------------------
234 // event processing
235 // ----------------------------------------------------------------------------
236
237 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
238 {
239 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
240 event.SetEventObject(this);
241 event.SetInt(eventSpin.GetPosition());
242
243 (void)GetEventHandler()->ProcessEvent(event);
244
245 if ( eventSpin.GetSkipped() )
246 {
247 event.Skip();
248 }
249 }
250
251 // ----------------------------------------------------------------------------
252 // size calculations
253 // ----------------------------------------------------------------------------
254
255 wxSize wxSpinCtrl::DoGetBestSize() const
256 {
257 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
258 // TODO:
259 /*
260 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
261
262 int y;
263 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
264 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
265
266 if ( sizeBtn.y < y )
267 {
268 // make the text tall enough
269 sizeBtn.y = y;
270 }
271 */
272 return sizeBtn;
273 }
274
275 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
276 {
277 int widthBtn = DoGetBestSize().x;
278 int widthText = width - widthBtn - MARGIN_BETWEEN;
279 if ( widthText <= 0 )
280 {
281 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
282 }
283 // TODO:
284 /*
285 if ( !::MoveWindow((HWND)m_hwndBuddy, x, y, widthText, height, TRUE) )
286 {
287 wxLogLastError("MoveWindow(buddy)");
288 }
289
290 x += widthText + MARGIN_BETWEEN;
291 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
292 {
293 wxLogLastError("MoveWindow");
294 }
295 */
296 }
297
298 #endif //wxUSE_SPINBTN