Lots of fixes for OS/2
[wxWidgets.git] / src / os2 / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: checkbox.cpp
3 // Purpose: wxCheckBox
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/checkbox.h"
17 #include "wx/brush.h"
18 #include "wx/scrolwin.h"
19 #include "wx/dcscreen.h"
20 #include "wx/settings.h"
21 #endif
22
23 #include "wx/os2/private.h"
24
25 // ----------------------------------------------------------------------------
26 // macros
27 // ----------------------------------------------------------------------------
28
29 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 // ----------------------------------------------------------------------------
37 // wxCheckBox
38 // ----------------------------------------------------------------------------
39
40 bool wxCheckBox::OS2Command(
41 WXUINT WXUNUSED(uParam)
42 , WXWORD WXUNUSED(wId)
43 )
44 {
45 wxCommandEvent rEvent( wxEVT_COMMAND_CHECKBOX_CLICKED
46 ,m_windowId
47 );
48 rEvent.SetInt(GetValue());
49 rEvent.SetEventObject(this);
50 ProcessCommand(rEvent);
51 return TRUE;
52 } // end of wxCheckBox::OS2Command
53
54 bool wxCheckBox::Create(
55 wxWindow* pParent
56 , wxWindowID vId
57 , const wxString& rsLabel
58 , const wxPoint& rPos
59 , const wxSize& rSize
60 , long lStyle
61 #if wxUSE_VALIDATORS
62 , const wxValidator& rValidator
63 #endif
64 , const wxString& rsName
65 )
66 {
67 SetName(rsName);
68 #if wxUSE_VALIDATORS
69 SetValidator(rValidator);
70 #endif
71 if (pParent)
72 pParent->AddChild(this);
73
74 SetBackgroundColour(pParent->GetBackgroundColour());
75 SetForegroundColour(pParent->GetForegroundColour());
76 m_windowStyle = lStyle;
77
78 wxString sLabel = rsLabel;
79
80 if (sLabel == wxT(""))
81 sLabel = wxT(" "); // Apparently needed or checkbox won't show
82
83 if (vId == -1 )
84 m_windowId = NewControlId();
85 else
86 m_windowId = vId;
87
88 int nX = rPos.x;
89 int nY = rPos.y;
90 int nWidth = rSize.x;
91 int nHeight = rSize.y;
92 long lSstyle = 0L;
93
94 lSstyle = BS_AUTOCHECKBOX |
95 WS_TABSTOP |
96 WS_VISIBLE;
97 if (lStyle & wxCLIP_SIBLINGS )
98 lSstyle |= WS_CLIPSIBLINGS;
99
100 //
101 // If the parent is a scrolled window the controls must
102 // have this style or they will overlap the scrollbars
103 //
104 if (pParent)
105 if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) ||
106 pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow)))
107 lSstyle |= WS_CLIPSIBLINGS;
108
109 m_hWnd = (WXHWND)::WinCreateWindow ( GetHwndOf(pParent)
110 ,WC_BUTTON
111 ,rsLabel.c_str()
112 ,lSstyle
113 ,0, 0, 0, 0
114 ,GetWinHwnd(pParent)
115 ,HWND_TOP
116 ,(HMENU)m_windowId
117 ,NULL
118 ,NULL
119 );
120
121 //
122 // Subclass again for purposes of dialog editing mode
123 //
124 SubclassWin(m_hWnd);
125
126 LONG lColor = (LONG)m_backgroundColour.GetPixel();
127
128 ::WinSetPresParam( m_hWnd
129 ,PP_BACKGROUNDCOLOR
130 ,sizeof(LONG)
131 ,(PVOID)&lColor
132 );
133
134 SetFont(*wxSMALL_FONT);
135
136 SetSize( nX
137 ,nY
138 ,nWidth
139 ,nHeight
140 );
141 return TRUE;
142 } // end of wxCheckBox::Create
143
144 void wxCheckBox::SetLabel(
145 const wxString& rsLabel
146 )
147 {
148 ::WinSetWindowText(GetHwnd(), rsLabel.c_str());
149 } // end of wxCheckBox::SetLabel
150
151 wxSize wxCheckBox::DoGetBestSize() const
152 {
153 static int nCheckSize = 0;
154
155 if (!nCheckSize)
156 {
157 wxScreenDC vDc;
158
159 vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
160
161 //
162 // The height of a standard button in the dialog units is 8,
163 // translate this to pixels (as one dialog unit is precisely equal to
164 // 8 character heights, it's just the char height)
165 //
166 nCheckSize = vDc.GetCharHeight();
167 }
168
169 int nWidthCheckbox;
170 int nHeightCheckbox;
171 wxString sStr = wxGetWindowText(GetHWND());
172
173 if (!sStr.IsEmpty())
174 {
175 GetTextExtent( sStr
176 ,&nWidthCheckbox
177 ,&nHeightCheckbox
178 );
179 nWidthCheckbox += nCheckSize + GetCharWidth();
180
181 if (nHeightCheckbox < nCheckSize)
182 nHeightCheckbox = nCheckSize;
183 }
184 else
185 {
186 nWidthCheckbox = nCheckSize;
187 nHeightCheckbox = nCheckSize;
188 }
189
190 return wxSize( nWidthCheckbox
191 ,nHeightCheckbox
192 );
193 } // end of wxCheckBox::DoGetBestSize
194
195 void wxCheckBox::SetValue(
196 bool bValue
197 )
198 {
199 ::WinSendMsg(GetHwnd(), BM_SETCHECK, (MPARAM)bValue, 0);
200 } // end of wxCheckBox::SetValue
201
202 #ifndef BST_CHECKED
203 #define BST_CHECKED 0x0001
204 #endif
205
206 bool wxCheckBox::GetValue() const
207 {
208 return((LONGFROMMR(::WinSendMsg(GetHwnd(), BM_QUERYCHECK, (MPARAM)0, (MPARAM)0)) == 1L));
209 } // end of wxCheckBox::GetValue
210
211 void wxCheckBox::Command (
212 wxCommandEvent& rEvent
213 )
214 {
215 SetValue((rEvent.GetInt() != 0));
216 ProcessCommand(rEvent);
217 } // end of wxCheckBox:: Command
218
219 // ----------------------------------------------------------------------------
220 // wxBitmapCheckBox
221 // ----------------------------------------------------------------------------
222
223 bool wxBitmapCheckBox::Create(
224 wxWindow* pParent
225 , wxWindowID vId
226 , const wxBitmap* pLabel
227 , const wxPoint& rPos
228 , const wxSize& rSize
229 , long lStyle
230 #if wxUSE_VALIDATORS
231 , const wxValidator& rValidator
232 #endif
233 , const wxString& rsName
234 )
235 {
236 SetName(rsName);
237 #if wxUSE_VALIDATORS
238 SetValidator(rValidator);
239 #endif
240 if (pParent)
241 pParent->AddChild(this);
242
243 SetBackgroundColour(pParent->GetBackgroundColour()) ;
244 SetForegroundColour(pParent->GetForegroundColour()) ;
245 m_windowStyle = lStyle;
246
247 if (vId == -1)
248 m_windowId = NewControlId();
249 else
250 m_windowId = vId;
251
252 int nX = rPos.x;
253 int nY = rPos.y;
254 int nWidth = rSize.x;
255 int nHeight = rSize.y;
256
257 m_nCheckWidth = -1 ;
258 m_nCheckHeight = -1 ;
259 // long msStyle = CHECK_FLAGS;
260
261 HWND hButton = 0; // TODO: Create the bitmap checkbox
262
263 m_hWnd = (WXHWND)hButton;
264
265 //
266 // Subclass again for purposes of dialog editing mode
267 //
268 SubclassWin((WXHWND)hButton);
269
270 SetSize( nX
271 ,nY
272 ,nWidth
273 ,nHeight
274 );
275
276 ::WinShowWindow(hButton, TRUE);
277 return TRUE;
278 } // end of wxBitmapCheckBox::Create
279
280 void wxBitmapCheckBox::SetLabel(
281 const wxBitmap& rBitmap
282 )
283 {
284 wxFAIL_MSG(wxT("not implemented"));
285 } // end of wxBitmapCheckBox::SetLabel
286