| 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 wxUSE_SPINCTRL |
| 37 | |
| 38 | #if defined(__WIN95__) |
| 39 | |
| 40 | #include "wx/spinctrl.h" |
| 41 | #include "wx/msw/private.h" |
| 42 | |
| 43 | #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__)) |
| 44 | #include <commctrl.h> |
| 45 | #endif |
| 46 | |
| 47 | #include <limits.h> // for INT_MIN |
| 48 | |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | // macros |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
| 53 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) |
| 54 | |
| 55 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) |
| 56 | EVT_CHAR(wxSpinCtrl::OnChar) |
| 57 | EVT_SPIN(-1, wxSpinCtrl::OnSpinChange) |
| 58 | END_EVENT_TABLE() |
| 59 | |
| 60 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // constants |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | // the margin between the up-down control and its buddy (can be arbitrary, |
| 67 | // choose what you like - or may be decide during run-time depending on the |
| 68 | // font size?) |
| 69 | static const int MARGIN_BETWEEN = 1; |
| 70 | |
| 71 | // ============================================================================ |
| 72 | // implementation |
| 73 | // ============================================================================ |
| 74 | |
| 75 | wxArraySpins wxSpinCtrl::ms_allSpins; |
| 76 | |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | // wnd proc for the buddy text ctrl |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | |
| 81 | LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd, |
| 82 | UINT message, |
| 83 | WPARAM wParam, |
| 84 | LPARAM lParam) |
| 85 | { |
| 86 | wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA); |
| 87 | |
| 88 | // forward some messages (the key ones only so far) to the spin ctrl |
| 89 | switch ( message ) |
| 90 | { |
| 91 | case WM_CHAR: |
| 92 | case WM_DEADCHAR: |
| 93 | case WM_KEYUP: |
| 94 | case WM_KEYDOWN: |
| 95 | spin->MSWWindowProc(message, wParam, lParam); |
| 96 | |
| 97 | // The control may have been deleted at this point, so check. |
| 98 | if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin)) |
| 99 | return 0; |
| 100 | break; |
| 101 | |
| 102 | case WM_GETDLGCODE: |
| 103 | // we want to get WXK_RETURN in order to generate the event for it |
| 104 | return DLGC_WANTCHARS; |
| 105 | } |
| 106 | |
| 107 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
| 108 | hwnd, message, wParam, lParam); |
| 109 | } |
| 110 | |
| 111 | /* static */ |
| 112 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) |
| 113 | { |
| 114 | wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy, |
| 115 | GWL_USERDATA); |
| 116 | |
| 117 | int i = ms_allSpins.Index(spin); |
| 118 | |
| 119 | if ( i == wxNOT_FOUND ) |
| 120 | return NULL; |
| 121 | |
| 122 | // sanity check |
| 123 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, |
| 124 | _T("wxSpinCtrl has incorrect buddy HWND!") ); |
| 125 | |
| 126 | return spin; |
| 127 | } |
| 128 | |
| 129 | // process a WM_COMMAND generated by the buddy text control |
| 130 | bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) |
| 131 | { |
| 132 | switch (cmd) |
| 133 | { |
| 134 | case EN_CHANGE: |
| 135 | { |
| 136 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
| 137 | event.SetEventObject(this); |
| 138 | wxString val = wxGetWindowText(m_hwndBuddy); |
| 139 | event.SetString(val); |
| 140 | event.SetInt(GetValue()); |
| 141 | return GetEventHandler()->ProcessEvent(event); |
| 142 | } |
| 143 | case EN_SETFOCUS: |
| 144 | case EN_KILLFOCUS: |
| 145 | { |
| 146 | wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS |
| 147 | : wxEVT_SET_FOCUS, |
| 148 | m_windowId); |
| 149 | event.SetEventObject( this ); |
| 150 | return GetEventHandler()->ProcessEvent(event); |
| 151 | } |
| 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | // not processed |
| 157 | return FALSE; |
| 158 | } |
| 159 | |
| 160 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
| 161 | { |
| 162 | switch ( event.KeyCode() ) |
| 163 | { |
| 164 | case WXK_RETURN: |
| 165 | { |
| 166 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); |
| 167 | InitCommandEvent(event); |
| 168 | wxString val = wxGetWindowText(m_hwndBuddy); |
| 169 | event.SetString(val); |
| 170 | event.SetInt(GetValue()); |
| 171 | if ( GetEventHandler()->ProcessEvent(event) ) |
| 172 | return; |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | case WXK_TAB: |
| 177 | // always produce navigation event - even if we process TAB |
| 178 | // ourselves the fact that we got here means that the user code |
| 179 | // decided to skip processing of this TAB - probably to let it |
| 180 | // do its default job. |
| 181 | { |
| 182 | wxNavigationKeyEvent eventNav; |
| 183 | eventNav.SetDirection(!event.ShiftDown()); |
| 184 | eventNav.SetWindowChange(event.ControlDown()); |
| 185 | eventNav.SetEventObject(this); |
| 186 | |
| 187 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
| 188 | return; |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | // no, we didn't process it |
| 194 | event.Skip(); |
| 195 | } |
| 196 | |
| 197 | // ---------------------------------------------------------------------------- |
| 198 | // construction |
| 199 | // ---------------------------------------------------------------------------- |
| 200 | |
| 201 | bool wxSpinCtrl::Create(wxWindow *parent, |
| 202 | wxWindowID id, |
| 203 | const wxString& value, |
| 204 | const wxPoint& pos, |
| 205 | const wxSize& size, |
| 206 | long style, |
| 207 | int min, int max, int initial, |
| 208 | const wxString& name) |
| 209 | { |
| 210 | // before using DoGetBestSize(), have to set style to let the base class |
| 211 | // know whether this is a horizontal or vertical control (we're always |
| 212 | // vertical) |
| 213 | style |= wxSP_VERTICAL; |
| 214 | SetWindowStyle(style); |
| 215 | |
| 216 | // calculate the sizes: the size given is the toal size for both controls |
| 217 | // and we need to fit them both in the given width (height is the same) |
| 218 | wxSize sizeText(size), sizeBtn(size); |
| 219 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; |
| 220 | if ( sizeText.x <= 0 ) |
| 221 | { |
| 222 | // DEFAULT_ITEM_WIDTH is the default width for the text control |
| 223 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; |
| 224 | } |
| 225 | |
| 226 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; |
| 227 | if ( sizeText.x <= 0 ) |
| 228 | { |
| 229 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); |
| 230 | } |
| 231 | |
| 232 | wxPoint posBtn(pos); |
| 233 | posBtn.x += sizeText.x + MARGIN_BETWEEN; |
| 234 | |
| 235 | // create the spin button |
| 236 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) |
| 237 | { |
| 238 | return FALSE; |
| 239 | } |
| 240 | |
| 241 | SetRange(min, max); |
| 242 | SetValue(initial); |
| 243 | |
| 244 | bool want3D; |
| 245 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); |
| 246 | int msStyle = WS_CHILD; |
| 247 | |
| 248 | // Even with extended styles, need to combine with WS_BORDER for them to |
| 249 | // look right. |
| 250 | if ( want3D || wxStyleHasBorder(style) ) |
| 251 | msStyle |= WS_BORDER; |
| 252 | |
| 253 | if ( style & wxCLIP_SIBLINGS ) |
| 254 | msStyle |= WS_CLIPSIBLINGS; |
| 255 | |
| 256 | // create the text window |
| 257 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
| 258 | ( |
| 259 | exStyle, // sunken border |
| 260 | _T("EDIT"), // window class |
| 261 | NULL, // no window title |
| 262 | msStyle /* | WS_CLIPSIBLINGS */, // style (will be shown later) |
| 263 | pos.x, pos.y, // position |
| 264 | 0, 0, // size (will be set later) |
| 265 | GetHwndOf(parent), // parent |
| 266 | (HMENU)-1, // control id |
| 267 | wxGetInstance(), // app instance |
| 268 | NULL // unused client data |
| 269 | ); |
| 270 | |
| 271 | if ( !m_hwndBuddy ) |
| 272 | { |
| 273 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
| 274 | |
| 275 | return FALSE; |
| 276 | } |
| 277 | |
| 278 | // subclass the text ctrl to be able to intercept some events |
| 279 | m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC); |
| 280 | ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this); |
| 281 | ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc); |
| 282 | |
| 283 | // should have the same font as the other controls |
| 284 | SetFont(GetParent()->GetFont()); |
| 285 | |
| 286 | // set the size of the text window - can do it only now, because we |
| 287 | // couldn't call DoGetBestSize() before as font wasn't set |
| 288 | if ( sizeText.y <= 0 ) |
| 289 | { |
| 290 | int cx, cy; |
| 291 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); |
| 292 | |
| 293 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); |
| 294 | } |
| 295 | |
| 296 | DoMoveWindow(pos.x, pos.y, |
| 297 | sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y); |
| 298 | |
| 299 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
| 300 | |
| 301 | // associate the text window with the spin button |
| 302 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
| 303 | |
| 304 | if ( !value.IsEmpty() ) |
| 305 | { |
| 306 | SetValue(value); |
| 307 | } |
| 308 | |
| 309 | // do it after finishing with m_hwndBuddy creation to avoid generating |
| 310 | // initial wxEVT_COMMAND_TEXT_UPDATED message |
| 311 | ms_allSpins.Add(this); |
| 312 | |
| 313 | return TRUE; |
| 314 | } |
| 315 | |
| 316 | wxSpinCtrl::~wxSpinCtrl() |
| 317 | { |
| 318 | ms_allSpins.Remove(this); |
| 319 | |
| 320 | // This removes spurious memory leak reporting |
| 321 | if (ms_allSpins.GetCount() == 0) |
| 322 | ms_allSpins.Clear(); |
| 323 | |
| 324 | // destroy the buddy window because this pointer which wxBuddyTextWndProc |
| 325 | // uses will not soon be valid any more |
| 326 | ::DestroyWindow(GetBuddyHwnd()); |
| 327 | } |
| 328 | |
| 329 | // ---------------------------------------------------------------------------- |
| 330 | // wxTextCtrl-like methods |
| 331 | // ---------------------------------------------------------------------------- |
| 332 | |
| 333 | void wxSpinCtrl::SetValue(const wxString& text) |
| 334 | { |
| 335 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
| 336 | { |
| 337 | wxLogLastError(wxT("SetWindowText(buddy)")); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | int wxSpinCtrl::GetValue() const |
| 342 | { |
| 343 | wxString val = wxGetWindowText(m_hwndBuddy); |
| 344 | |
| 345 | long n; |
| 346 | if ( (wxSscanf(val, wxT("%lu"), &n) != 1) ) |
| 347 | n = INT_MIN; |
| 348 | |
| 349 | return n; |
| 350 | } |
| 351 | |
| 352 | // ---------------------------------------------------------------------------- |
| 353 | // forward some methods to subcontrols |
| 354 | // ---------------------------------------------------------------------------- |
| 355 | |
| 356 | bool wxSpinCtrl::SetFont(const wxFont& font) |
| 357 | { |
| 358 | if ( !wxWindowBase::SetFont(font) ) |
| 359 | { |
| 360 | // nothing to do |
| 361 | return FALSE; |
| 362 | } |
| 363 | |
| 364 | WXHANDLE hFont = GetFont().GetResourceHandle(); |
| 365 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
| 366 | |
| 367 | return TRUE; |
| 368 | } |
| 369 | |
| 370 | bool wxSpinCtrl::Show(bool show) |
| 371 | { |
| 372 | if ( !wxControl::Show(show) ) |
| 373 | { |
| 374 | return FALSE; |
| 375 | } |
| 376 | |
| 377 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
| 378 | |
| 379 | return TRUE; |
| 380 | } |
| 381 | |
| 382 | bool wxSpinCtrl::Enable(bool enable) |
| 383 | { |
| 384 | if ( !wxControl::Enable(enable) ) |
| 385 | { |
| 386 | return FALSE; |
| 387 | } |
| 388 | |
| 389 | ::EnableWindow(GetBuddyHwnd(), enable); |
| 390 | |
| 391 | return TRUE; |
| 392 | } |
| 393 | |
| 394 | void wxSpinCtrl::SetFocus() |
| 395 | { |
| 396 | ::SetFocus(GetBuddyHwnd()); |
| 397 | } |
| 398 | |
| 399 | // ---------------------------------------------------------------------------- |
| 400 | // event processing |
| 401 | // ---------------------------------------------------------------------------- |
| 402 | |
| 403 | void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin) |
| 404 | { |
| 405 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); |
| 406 | event.SetEventObject(this); |
| 407 | event.SetInt(eventSpin.GetPosition()); |
| 408 | |
| 409 | (void)GetEventHandler()->ProcessEvent(event); |
| 410 | |
| 411 | if ( eventSpin.GetSkipped() ) |
| 412 | { |
| 413 | event.Skip(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // ---------------------------------------------------------------------------- |
| 418 | // size calculations |
| 419 | // ---------------------------------------------------------------------------- |
| 420 | |
| 421 | wxSize wxSpinCtrl::DoGetBestSize() const |
| 422 | { |
| 423 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); |
| 424 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; |
| 425 | |
| 426 | int y; |
| 427 | wxGetCharSize(GetHWND(), NULL, &y, &GetFont()); |
| 428 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); |
| 429 | |
| 430 | if ( sizeBtn.y < y ) |
| 431 | { |
| 432 | // make the text tall enough |
| 433 | sizeBtn.y = y; |
| 434 | } |
| 435 | |
| 436 | return sizeBtn; |
| 437 | } |
| 438 | |
| 439 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
| 440 | { |
| 441 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
| 442 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
| 443 | if ( widthText <= 0 ) |
| 444 | { |
| 445 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); |
| 446 | } |
| 447 | |
| 448 | if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) ) |
| 449 | { |
| 450 | wxLogLastError(wxT("MoveWindow(buddy)")); |
| 451 | } |
| 452 | |
| 453 | x += widthText + MARGIN_BETWEEN; |
| 454 | if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) ) |
| 455 | { |
| 456 | wxLogLastError(wxT("MoveWindow")); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // get total size of the control |
| 461 | void wxSpinCtrl::DoGetSize(int *x, int *y) const |
| 462 | { |
| 463 | RECT spinrect, textrect, ctrlrect; |
| 464 | GetWindowRect(GetHwnd(), &spinrect); |
| 465 | GetWindowRect(GetBuddyHwnd(), &textrect); |
| 466 | UnionRect(&ctrlrect,&textrect, &spinrect); |
| 467 | |
| 468 | if ( x ) |
| 469 | *x = ctrlrect.right - ctrlrect.left; |
| 470 | if ( y ) |
| 471 | *y = ctrlrect.bottom - ctrlrect.top; |
| 472 | } |
| 473 | |
| 474 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const |
| 475 | { |
| 476 | // hack: pretend that our HWND is the text control just for a moment |
| 477 | WXHWND hWnd = GetHWND(); |
| 478 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; |
| 479 | |
| 480 | wxSpinButton::DoGetPosition(x, y); |
| 481 | |
| 482 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; |
| 483 | } |
| 484 | |
| 485 | #endif // __WIN95__ |
| 486 | |
| 487 | #endif |
| 488 | // wxUSE_SPINCTRL |
| 489 | |