]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
Updated to current API.
[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 WX_DEFINE_FLAGS( wxCheckBoxStyle )
52
53 WX_BEGIN_FLAGS( wxCheckBoxStyle )
54 // new style border flags, we put them first to
55 // use them for streaming out
56 WX_FLAGS_MEMBER(wxBORDER_SIMPLE)
57 WX_FLAGS_MEMBER(wxBORDER_SUNKEN)
58 WX_FLAGS_MEMBER(wxBORDER_DOUBLE)
59 WX_FLAGS_MEMBER(wxBORDER_RAISED)
60 WX_FLAGS_MEMBER(wxBORDER_STATIC)
61 WX_FLAGS_MEMBER(wxBORDER_NONE)
62
63 // old style border flags
64 WX_FLAGS_MEMBER(wxSIMPLE_BORDER)
65 WX_FLAGS_MEMBER(wxSUNKEN_BORDER)
66 WX_FLAGS_MEMBER(wxDOUBLE_BORDER)
67 WX_FLAGS_MEMBER(wxRAISED_BORDER)
68 WX_FLAGS_MEMBER(wxSTATIC_BORDER)
69 WX_FLAGS_MEMBER(wxNO_BORDER)
70
71 // standard window styles
72 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL)
73 WX_FLAGS_MEMBER(wxCLIP_CHILDREN)
74 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW)
75 WX_FLAGS_MEMBER(wxWANTS_CHARS)
76 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
77 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB )
78 WX_FLAGS_MEMBER(wxVSCROLL)
79 WX_FLAGS_MEMBER(wxHSCROLL)
80
81 WX_END_FLAGS( wxCheckBoxStyle )
82
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h")
84
85 WX_BEGIN_PROPERTIES_TABLE(wxCheckBox)
86 WX_EVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent )
87
88 WX_PROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
89 WX_PROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
90 WX_PROPERTY( Value ,bool, SetValue, GetValue, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
91 WX_PROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
92 WX_END_PROPERTIES_TABLE()
93
94 WX_BEGIN_HANDLERS_TABLE(wxCheckBox)
95 WX_END_HANDLERS_TABLE()
96
97 WX_CONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
98 #else
99 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
100 #endif
101
102
103 // ----------------------------------------------------------------------------
104 // wxCheckBox
105 // ----------------------------------------------------------------------------
106
107 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
108 {
109 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
110 event.SetInt(GetValue());
111 event.SetEventObject(this);
112 ProcessCommand(event);
113 return TRUE;
114 }
115
116 bool wxCheckBox::Create(wxWindow *parent,
117 wxWindowID id,
118 const wxString& label,
119 const wxPoint& pos,
120 const wxSize& size, long style,
121 const wxValidator& validator,
122 const wxString& name)
123 {
124 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
125 return FALSE;
126
127 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP;
128 if ( style & wxALIGN_RIGHT )
129 msStyle |= BS_LEFTTEXT;
130
131 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
132 }
133
134 void wxCheckBox::SetLabel(const wxString& label)
135 {
136 SetWindowText(GetHwnd(), label);
137 }
138
139 wxSize wxCheckBox::DoGetBestSize() const
140 {
141 static int s_checkSize = 0;
142
143 if ( !s_checkSize )
144 {
145 wxScreenDC dc;
146 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
147
148 s_checkSize = dc.GetCharHeight();
149 }
150
151 wxString str = wxGetWindowText(GetHWND());
152
153 int wCheckbox, hCheckbox;
154 if ( !str.IsEmpty() )
155 {
156 GetTextExtent(str, &wCheckbox, &hCheckbox);
157 wCheckbox += s_checkSize + GetCharWidth();
158
159 if ( hCheckbox < s_checkSize )
160 hCheckbox = s_checkSize;
161 }
162 else
163 {
164 wCheckbox = s_checkSize;
165 hCheckbox = s_checkSize;
166 }
167
168 return wxSize(wCheckbox, hCheckbox);
169 }
170
171 void wxCheckBox::SetValue(bool val)
172 {
173 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
174 }
175
176 bool wxCheckBox::GetValue() const
177 {
178 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) & BST_CHECKED) != 0;
179 }
180
181 void wxCheckBox::Command(wxCommandEvent& event)
182 {
183 SetValue(event.GetInt() != 0);
184 ProcessCommand(event);
185 }
186
187 #endif // wxUSE_CHECKBOX