]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
Implement GetValue() and SetValue() and send an event when clicked
[wxWidgets.git] / src / msw / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/checkbox.cpp
3 // Purpose: wxCheckBox
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "checkbox.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CHECKBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/checkbox.h"
35 #include "wx/brush.h"
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/msw/private.h"
41
42 #ifndef BST_CHECKED
43 #define BST_CHECKED 0x0001
44 #endif
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 #if wxUSE_EXTENDED_RTTI
51 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h")
52
53 WX_BEGIN_PROPERTIES_TABLE(wxCheckBox)
54 WX_DELEGATE( OnClick , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent )
55
56 WX_PROPERTY( Font , wxFont , SetFont , GetWindowFont , )
57 WX_PROPERTY( Label,wxString, SetLabel, GetLabel, wxT("") )
58 WX_PROPERTY( Value ,bool, SetValue, GetValue, )
59 WX_END_PROPERTIES_TABLE()
60
61 WX_BEGIN_HANDLERS_TABLE(wxCheckBox)
62 WX_END_HANDLERS_TABLE()
63
64 WX_CONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
65 #else
66 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
67 #endif
68
69
70 // ----------------------------------------------------------------------------
71 // wxCheckBox
72 // ----------------------------------------------------------------------------
73
74 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
75 {
76 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
77 event.SetInt(GetValue());
78 event.SetEventObject(this);
79 ProcessCommand(event);
80 return TRUE;
81 }
82
83 bool wxCheckBox::Create(wxWindow *parent,
84 wxWindowID id,
85 const wxString& label,
86 const wxPoint& pos,
87 const wxSize& size, long style,
88 const wxValidator& validator,
89 const wxString& name)
90 {
91 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
92 return FALSE;
93
94 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP;
95 if ( style & wxALIGN_RIGHT )
96 msStyle |= BS_LEFTTEXT;
97
98 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
99 }
100
101 void wxCheckBox::SetLabel(const wxString& label)
102 {
103 SetWindowText(GetHwnd(), label);
104 }
105
106 wxSize wxCheckBox::DoGetBestSize() const
107 {
108 static int s_checkSize = 0;
109
110 if ( !s_checkSize )
111 {
112 wxScreenDC dc;
113 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
114
115 s_checkSize = dc.GetCharHeight();
116 }
117
118 wxString str = wxGetWindowText(GetHWND());
119
120 int wCheckbox, hCheckbox;
121 if ( !str.IsEmpty() )
122 {
123 GetTextExtent(str, &wCheckbox, &hCheckbox);
124 wCheckbox += s_checkSize + GetCharWidth();
125
126 if ( hCheckbox < s_checkSize )
127 hCheckbox = s_checkSize;
128 }
129 else
130 {
131 wCheckbox = s_checkSize;
132 hCheckbox = s_checkSize;
133 }
134
135 return wxSize(wCheckbox, hCheckbox);
136 }
137
138 void wxCheckBox::SetValue(bool val)
139 {
140 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
141 }
142
143 bool wxCheckBox::GetValue() const
144 {
145 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) & BST_CHECKED) != 0;
146 }
147
148 void wxCheckBox::Command(wxCommandEvent& event)
149 {
150 SetValue(event.GetInt() != 0);
151 ProcessCommand(event);
152 }
153
154 #endif // wxUSE_CHECKBOX