]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
Improve default size. If the control is auto-fit its size won't be less
[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 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
51
52 /*
53 TODO PROPERTIES :
54
55 bool "checked" , 0
56 */
57
58 // ----------------------------------------------------------------------------
59 // wxCheckBox
60 // ----------------------------------------------------------------------------
61
62 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
63 {
64 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
65 event.SetInt(GetValue());
66 event.SetEventObject(this);
67 ProcessCommand(event);
68 return TRUE;
69 }
70
71 bool wxCheckBox::Create(wxWindow *parent,
72 wxWindowID id,
73 const wxString& label,
74 const wxPoint& pos,
75 const wxSize& size, long style,
76 const wxValidator& validator,
77 const wxString& name)
78 {
79 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
80 return FALSE;
81
82 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP;
83 if ( style & wxALIGN_RIGHT )
84 msStyle |= BS_LEFTTEXT;
85
86 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
87 }
88
89 void wxCheckBox::SetLabel(const wxString& label)
90 {
91 SetWindowText(GetHwnd(), label);
92 }
93
94 wxSize wxCheckBox::DoGetBestSize() const
95 {
96 static int s_checkSize = 0;
97
98 if ( !s_checkSize )
99 {
100 wxScreenDC dc;
101 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
102
103 s_checkSize = dc.GetCharHeight();
104 }
105
106 wxString str = wxGetWindowText(GetHWND());
107
108 int wCheckbox, hCheckbox;
109 if ( !str.IsEmpty() )
110 {
111 GetTextExtent(str, &wCheckbox, &hCheckbox);
112 wCheckbox += s_checkSize + GetCharWidth();
113
114 if ( hCheckbox < s_checkSize )
115 hCheckbox = s_checkSize;
116 }
117 else
118 {
119 wCheckbox = s_checkSize;
120 hCheckbox = s_checkSize;
121 }
122
123 return wxSize(wCheckbox, hCheckbox);
124 }
125
126 void wxCheckBox::SetValue(bool val)
127 {
128 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
129 }
130
131 bool wxCheckBox::GetValue() const
132 {
133 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) & BST_CHECKED) != 0;
134 }
135
136 void wxCheckBox::Command(wxCommandEvent& event)
137 {
138 SetValue(event.GetInt() != 0);
139 ProcessCommand(event);
140 }
141
142 #endif // wxUSE_CHECKBOX