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