]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinctrl.cpp
minor bug fixes
[wxWidgets.git] / src / msw / spinctrl.cpp
CommitLineData
b782f2e0
VZ
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
4c0a2c5c
JS
41#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
42 #include <commctrl.h>
43#endif
b782f2e0
VZ
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
58static const int MARGIN_BETWEEN = 5;
59
60// ============================================================================
61// implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
65// construction
66// ----------------------------------------------------------------------------
67
68bool wxSpinCtrl::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxPoint& pos,
71 const wxSize& size,
72 long style,
73 int min, int max, int initial,
74 const wxString& name)
75{
76 // before using DoGetBestSize(), have to set style to let the base class
77 // know whether this is a horizontal or vertical control (we're always
78 // vertical)
79 SetWindowStyle(style | wxSP_VERTICAL);
80
81 // calculate the sizes: the size given is the toal size for both controls
82 // and we need to fit them both in the given width (height is the same)
83 wxSize sizeText(size), sizeBtn(size);
84 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
85 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
86 if ( sizeText.x <= 0 )
87 {
88 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
89 }
90
91 wxPoint posBtn(pos);
92 posBtn.x += sizeText.x + MARGIN_BETWEEN;
93
94 // create the spin button
95 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
96 {
97 return FALSE;
98 }
99
100 SetRange(min, max);
101 SetValue(initial);
102
103 // create the text window
104 if ( sizeText.y <= 0 )
105 {
106 // make it the same height as the button then
107 int x, y;
108 wxSpinButton::DoGetSize(&x, &y);
109
110 sizeText.y = y;
111 }
112
113 m_hwndBuddy = (WXHWND)::CreateWindowEx
114 (
115 WS_EX_CLIENTEDGE, // sunken border
116 _T("EDIT"), // window class
117 NULL, // no window title
118 WS_CHILD | WS_VISIBLE | WS_BORDER, // style
119 pos.x, pos.y, // position
120 sizeText.x, sizeText.y, // size
121 GetHwndOf(parent), // parent
122 (HMENU)-1, // control id
123 wxGetInstance(), // app instance
124 NULL // unused client data
125 );
126
127 if ( !m_hwndBuddy )
128 {
129 wxLogLastError("CreateWindow(buddy text window)");
130
131 return FALSE;
132 }
133
134 // should have the same font as the other controls
135 WXHANDLE hFont = GetParent()->GetFont().GetResourceHandle();
136 ::SendMessage((HWND)m_hwndBuddy, WM_SETFONT, (WPARAM)hFont, TRUE);
137
138 // associate the text window with the spin button
139 (void)SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
140
141 return TRUE;
142}
143
144// ----------------------------------------------------------------------------
145// size calculations
146// ----------------------------------------------------------------------------
147
148void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
149{
150 int widthBtn = DoGetBestSize().x;
151 int widthText = width - widthBtn - MARGIN_BETWEEN;
152 if ( widthText <= 0 )
153 {
154 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
155 }
156
157 if ( !::MoveWindow((HWND)m_hwndBuddy, x, y, widthText, height, TRUE) )
158 {
159 wxLogLastError("MoveWindow(buddy)");
160 }
161
162 x += widthText + MARGIN_BETWEEN;
163 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
164 {
165 wxLogLastError("MoveWindow");
166 }
167}
168
169#endif // __WIN95__