| 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_UNCHECKED |
| 43 | #define BST_UNCHECKED 0x0000 |
| 44 | #endif |
| 45 | |
| 46 | #ifndef BST_CHECKED |
| 47 | #define BST_CHECKED 0x0001 |
| 48 | #endif |
| 49 | |
| 50 | #ifndef BST_INDETERMINATE |
| 51 | #define BST_INDETERMINATE 0x0002 |
| 52 | #endif |
| 53 | |
| 54 | // ============================================================================ |
| 55 | // implementation |
| 56 | // ============================================================================ |
| 57 | |
| 58 | #if wxUSE_EXTENDED_RTTI |
| 59 | WX_DEFINE_FLAGS( wxCheckBoxStyle ) |
| 60 | |
| 61 | wxBEGIN_FLAGS( wxCheckBoxStyle ) |
| 62 | // new style border flags, we put them first to |
| 63 | // use them for streaming out |
| 64 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
| 65 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) |
| 66 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) |
| 67 | wxFLAGS_MEMBER(wxBORDER_RAISED) |
| 68 | wxFLAGS_MEMBER(wxBORDER_STATIC) |
| 69 | wxFLAGS_MEMBER(wxBORDER_NONE) |
| 70 | |
| 71 | // old style border flags |
| 72 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
| 73 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) |
| 74 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) |
| 75 | wxFLAGS_MEMBER(wxRAISED_BORDER) |
| 76 | wxFLAGS_MEMBER(wxSTATIC_BORDER) |
| 77 | wxFLAGS_MEMBER(wxNO_BORDER) |
| 78 | |
| 79 | // standard window styles |
| 80 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
| 81 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) |
| 82 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) |
| 83 | wxFLAGS_MEMBER(wxWANTS_CHARS) |
| 84 | wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) |
| 85 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
| 86 | wxFLAGS_MEMBER(wxVSCROLL) |
| 87 | wxFLAGS_MEMBER(wxHSCROLL) |
| 88 | |
| 89 | wxEND_FLAGS( wxCheckBoxStyle ) |
| 90 | |
| 91 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox, wxControl,"wx/checkbox.h") |
| 92 | |
| 93 | wxBEGIN_PROPERTIES_TABLE(wxCheckBox) |
| 94 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_CHECKBOX_CLICKED , wxCommandEvent ) |
| 95 | |
| 96 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 97 | wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 98 | wxPROPERTY( Value ,bool, SetValue, GetValue, EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 99 | wxPROPERTY_FLAGS( WindowStyle , wxCheckBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
| 100 | wxEND_PROPERTIES_TABLE() |
| 101 | |
| 102 | wxBEGIN_HANDLERS_TABLE(wxCheckBox) |
| 103 | wxEND_HANDLERS_TABLE() |
| 104 | |
| 105 | wxCONSTRUCTOR_6( wxCheckBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
| 106 | #else |
| 107 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) |
| 108 | #endif |
| 109 | |
| 110 | |
| 111 | // ---------------------------------------------------------------------------- |
| 112 | // wxCheckBox |
| 113 | // ---------------------------------------------------------------------------- |
| 114 | |
| 115 | bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id)) |
| 116 | { |
| 117 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId); |
| 118 | wxCheckBoxState state = Get3StateValue(); |
| 119 | |
| 120 | // If the style flag to allow the user setting the undetermined state |
| 121 | // is not set, then skip the undetermined state and set it to unchecked. |
| 122 | if ( state == wxCHK_UNDETERMINED && !Is3rdStateAllowedForUser() ) |
| 123 | { |
| 124 | state = wxCHK_UNCHECKED; |
| 125 | Set3StateValue(state); |
| 126 | } |
| 127 | |
| 128 | event.SetInt(state); |
| 129 | event.SetEventObject(this); |
| 130 | ProcessCommand(event); |
| 131 | |
| 132 | return TRUE; |
| 133 | } |
| 134 | |
| 135 | bool wxCheckBox::Create(wxWindow *parent, |
| 136 | wxWindowID id, |
| 137 | const wxString& label, |
| 138 | const wxPoint& pos, |
| 139 | const wxSize& size, long style, |
| 140 | const wxValidator& validator, |
| 141 | const wxString& name) |
| 142 | { |
| 143 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
| 144 | return FALSE; |
| 145 | |
| 146 | long msStyle = WS_TABSTOP; |
| 147 | |
| 148 | if ( style & wxCHK_3STATE ) |
| 149 | { |
| 150 | msStyle |= BS_AUTO3STATE; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | wxASSERT_MSG( !Is3rdStateAllowedForUser(), |
| 155 | wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER") |
| 156 | wxT(" style flag for a 2-state checkbox is useless") ); |
| 157 | msStyle |= BS_AUTOCHECKBOX; |
| 158 | } |
| 159 | |
| 160 | if ( style & wxALIGN_RIGHT ) |
| 161 | { |
| 162 | msStyle |= BS_LEFTTEXT; |
| 163 | } |
| 164 | |
| 165 | return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | wxSize wxCheckBox::DoGetBestSize() const |
| 170 | { |
| 171 | static int s_checkSize = 0; |
| 172 | |
| 173 | if ( !s_checkSize ) |
| 174 | { |
| 175 | wxScreenDC dc; |
| 176 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
| 177 | |
| 178 | s_checkSize = dc.GetCharHeight(); |
| 179 | } |
| 180 | |
| 181 | wxString str = wxGetWindowText(GetHWND()); |
| 182 | |
| 183 | int wCheckbox, hCheckbox; |
| 184 | if ( !str.IsEmpty() ) |
| 185 | { |
| 186 | GetTextExtent(str, &wCheckbox, &hCheckbox); |
| 187 | wCheckbox += s_checkSize + GetCharWidth(); |
| 188 | |
| 189 | if ( hCheckbox < s_checkSize ) |
| 190 | hCheckbox = s_checkSize; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | wCheckbox = s_checkSize; |
| 195 | hCheckbox = s_checkSize; |
| 196 | } |
| 197 | |
| 198 | return wxSize(wCheckbox, hCheckbox); |
| 199 | } |
| 200 | |
| 201 | void wxCheckBox::SetValue(bool val) |
| 202 | { |
| 203 | if (val) |
| 204 | { |
| 205 | Set3StateValue(wxCHK_CHECKED); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | Set3StateValue(wxCHK_UNCHECKED); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | bool wxCheckBox::GetValue() const |
| 214 | { |
| 215 | return (Get3StateValue() != 0); |
| 216 | } |
| 217 | |
| 218 | void wxCheckBox::Command(wxCommandEvent& event) |
| 219 | { |
| 220 | int state = event.GetInt(); |
| 221 | wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED) |
| 222 | || (state == wxCHK_UNDETERMINED), |
| 223 | wxT("event.GetInt() returned an invalid checkbox state") ); |
| 224 | |
| 225 | Set3StateValue((wxCheckBoxState) state); |
| 226 | ProcessCommand(event); |
| 227 | } |
| 228 | |
| 229 | wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED |
| 230 | && wxCHK_CHECKED == BST_CHECKED |
| 231 | && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect); |
| 232 | |
| 233 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) |
| 234 | { |
| 235 | ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0); |
| 236 | } |
| 237 | |
| 238 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const |
| 239 | { |
| 240 | #ifdef __WIN32__ |
| 241 | return (wxCheckBoxState) ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0); |
| 242 | #else |
| 243 | return (wxCheckBoxState) ((::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) |
| 244 | & 0x001) == 0x001); |
| 245 | #endif |
| 246 | |
| 247 | } |
| 248 | |
| 249 | #endif // wxUSE_CHECKBOX |