]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
wxSpinCtrl
[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 #include <commctrl.h>
42
43 // ----------------------------------------------------------------------------
44 // macros
45 // ----------------------------------------------------------------------------
46
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 // the margin between the up-down control and its buddy
56 static const int MARGIN_BETWEEN = 5;
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // construction
64 // ----------------------------------------------------------------------------
65
66 bool wxSpinCtrl::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 int min, int max, int initial,
72 const wxString& name)
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 SetWindowStyle(style | wxSP_VERTICAL);
78
79 // calculate the sizes: the size given is the toal size for both controls
80 // and we need to fit them both in the given width (height is the same)
81 wxSize sizeText(size), sizeBtn(size);
82 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
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 if ( sizeText.y <= 0 )
103 {
104 // make it the same height as the button then
105 int x, y;
106 wxSpinButton::DoGetSize(&x, &y);
107
108 sizeText.y = y;
109 }
110
111 m_hwndBuddy = (WXHWND)::CreateWindowEx
112 (
113 WS_EX_CLIENTEDGE, // sunken border
114 _T("EDIT"), // window class
115 NULL, // no window title
116 WS_CHILD | WS_VISIBLE | WS_BORDER, // style
117 pos.x, pos.y, // position
118 sizeText.x, sizeText.y, // size
119 GetHwndOf(parent), // parent
120 (HMENU)-1, // control id
121 wxGetInstance(), // app instance
122 NULL // unused client data
123 );
124
125 if ( !m_hwndBuddy )
126 {
127 wxLogLastError("CreateWindow(buddy text window)");
128
129 return FALSE;
130 }
131
132 // should have the same font as the other controls
133 WXHANDLE hFont = GetParent()->GetFont().GetResourceHandle();
134 ::SendMessage((HWND)m_hwndBuddy, WM_SETFONT, (WPARAM)hFont, TRUE);
135
136 // associate the text window with the spin button
137 (void)SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
138
139 return TRUE;
140 }
141
142 // ----------------------------------------------------------------------------
143 // size calculations
144 // ----------------------------------------------------------------------------
145
146 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
147 {
148 int widthBtn = DoGetBestSize().x;
149 int widthText = width - widthBtn - MARGIN_BETWEEN;
150 if ( widthText <= 0 )
151 {
152 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
153 }
154
155 if ( !::MoveWindow((HWND)m_hwndBuddy, x, y, widthText, height, TRUE) )
156 {
157 wxLogLastError("MoveWindow(buddy)");
158 }
159
160 x += widthText + MARGIN_BETWEEN;
161 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
162 {
163 wxLogLastError("MoveWindow");
164 }
165 }
166
167 #endif // __WIN95__