]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
1. more code commented out by DW (@#%#%!#%!@) uncommented
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 22.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 #if defined(__WIN95__)
37
38 #include "wx/spinctrl.h"
39 #include "wx/msw/private.h"
40
41 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
42 #include <commctrl.h>
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // macros
47 // ----------------------------------------------------------------------------
48
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // constants
55 // ----------------------------------------------------------------------------
56
57 // the margin between the up-down control and its buddy (can be arbitrary,
58 // choose what you like - or may be decide during run-time depending on the
59 // font size?)
60 static const int MARGIN_BETWEEN = 1;
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // construction
68 // ----------------------------------------------------------------------------
69
70 bool wxSpinCtrl::Create(wxWindow *parent,
71 wxWindowID id,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 int min, int max, int initial,
76 const wxString& name)
77 {
78 // before using DoGetBestSize(), have to set style to let the base class
79 // know whether this is a horizontal or vertical control (we're always
80 // vertical)
81 SetWindowStyle(style | wxSP_VERTICAL);
82
83 // calculate the sizes: the size given is the toal size for both controls
84 // and we need to fit them both in the given width (height is the same)
85 wxSize sizeText(size), sizeBtn(size);
86 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
87 if ( sizeText.x <= 0 )
88 {
89 // DEFAULT_ITEM_WIDTH is the default width for the text control
90 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
91 }
92
93 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
94 if ( sizeText.x <= 0 )
95 {
96 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
97 }
98
99 wxPoint posBtn(pos);
100 posBtn.x += sizeText.x + MARGIN_BETWEEN;
101
102 // create the spin button
103 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
104 {
105 return FALSE;
106 }
107
108 SetRange(min, max);
109 SetValue(initial);
110
111 // create the text window
112 m_hwndBuddy = (WXHWND)::CreateWindowEx
113 (
114 WS_EX_CLIENTEDGE, // sunken border
115 _T("EDIT"), // window class
116 NULL, // no window title
117 WS_CHILD | WS_BORDER, // style (will be shown later)
118 pos.x, pos.y, // position
119 0, 0, // size (will be set later)
120 GetHwndOf(parent), // parent
121 (HMENU)-1, // control id
122 wxGetInstance(), // app instance
123 NULL // unused client data
124 );
125
126 if ( !m_hwndBuddy )
127 {
128 wxLogLastError("CreateWindow(buddy text window)");
129
130 return FALSE;
131 }
132
133 // should have the same font as the other controls
134 SetFont(GetParent()->GetFont());
135
136 // set the size of the text window - can do it only now, because we
137 // couldn't call DoGetBestSize() before as font wasn't set
138 if ( sizeText.y <= 0 )
139 {
140 // make it the same height as the button then
141 sizeText.y = DoGetBestSize().y;
142 }
143
144 DoMoveWindow(pos.x, pos.y,
145 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
146
147 (void)::ShowWindow((HWND)m_hwndBuddy, SW_SHOW);
148
149 // associate the text window with the spin button
150 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
151
152 return TRUE;
153 }
154
155 // ----------------------------------------------------------------------------
156 // when setting font, we need to do it for both controls
157 // ----------------------------------------------------------------------------
158
159 bool wxSpinCtrl::SetFont(const wxFont& font)
160 {
161 if ( !wxWindowBase::SetFont(font) )
162 {
163 // nothing to do
164 return FALSE;
165 }
166
167 WXHANDLE hFont = GetFont().GetResourceHandle();
168 (void)::SendMessage((HWND)m_hwndBuddy, WM_SETFONT, (WPARAM)hFont, TRUE);
169
170 return TRUE;
171 }
172
173 // ----------------------------------------------------------------------------
174 // size calculations
175 // ----------------------------------------------------------------------------
176
177 wxSize wxSpinCtrl::DoGetBestSize()
178 {
179 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
180 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
181
182 int y;
183 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
184 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
185
186 if ( sizeBtn.y < y )
187 {
188 // make the text tall enough
189 sizeBtn.y = y;
190 }
191
192 return sizeBtn;
193 }
194
195 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
196 {
197 int widthBtn = wxSpinButton::DoGetBestSize().x;
198 int widthText = width - widthBtn - MARGIN_BETWEEN;
199 if ( widthText <= 0 )
200 {
201 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
202 }
203
204 if ( !::MoveWindow((HWND)m_hwndBuddy, x, y, widthText, height, TRUE) )
205 {
206 wxLogLastError("MoveWindow(buddy)");
207 }
208
209 x += widthText + MARGIN_BETWEEN;
210 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
211 {
212 wxLogLastError("MoveWindow");
213 }
214 }
215
216 #endif // __WIN95__