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