]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checkbox.cpp
extracted duplicated code from src/*/data.cpp in a common file
[wxWidgets.git] / src / msw / checkbox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
2b5f62a0 2// Name: msw/checkbox.cpp
2bda0e17
KB
3// Purpose: wxCheckBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4438caf4
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
4438caf4 21 #pragma implementation "checkbox.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
4438caf4 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_CHECKBOX
32
2bda0e17 33#ifndef WX_PRECOMP
4438caf4
VZ
34 #include "wx/checkbox.h"
35 #include "wx/brush.h"
f6bcfd97
BP
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
41
2b5f62a0
VZ
42#ifndef BST_CHECKED
43 #define BST_CHECKED 0x0001
44#endif
2bda0e17 45
4438caf4
VZ
46// ============================================================================
47// implementation
48// ============================================================================
49
2b5f62a0
VZ
50IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
51
4438caf4
VZ
52// ----------------------------------------------------------------------------
53// wxCheckBox
54// ----------------------------------------------------------------------------
55
debe6624 56bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
2bda0e17 57{
4438caf4
VZ
58 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
59 event.SetInt(GetValue());
60 event.SetEventObject(this);
61 ProcessCommand(event);
62 return TRUE;
2bda0e17
KB
63}
64
11b6a93b
VZ
65bool wxCheckBox::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxString& label,
68 const wxPoint& pos,
69 const wxSize& size, long style,
70 const wxValidator& validator,
71 const wxString& name)
2bda0e17 72{
787a85c2 73 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
8912d7eb 74 return FALSE;
4438caf4 75
8912d7eb 76 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP;
4438caf4
VZ
77 if ( style & wxALIGN_RIGHT )
78 msStyle |= BS_LEFTTEXT;
79
8912d7eb 80 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
2bda0e17
KB
81}
82
83void wxCheckBox::SetLabel(const wxString& label)
84{
f6bcfd97 85 SetWindowText(GetHwnd(), label);
2bda0e17
KB
86}
87
f68586e5 88wxSize wxCheckBox::DoGetBestSize() const
2bda0e17 89{
f6bcfd97 90 static int s_checkSize = 0;
81d66cf3 91
f6bcfd97
BP
92 if ( !s_checkSize )
93 {
94 wxScreenDC dc;
a756f210 95 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 96
fb1a41cd 97 s_checkSize = dc.GetCharHeight();
f6bcfd97
BP
98 }
99
100 wxString str = wxGetWindowText(GetHWND());
101
102 int wCheckbox, hCheckbox;
4438caf4
VZ
103 if ( !str.IsEmpty() )
104 {
105 GetTextExtent(str, &wCheckbox, &hCheckbox);
f6bcfd97 106 wCheckbox += s_checkSize + GetCharWidth();
27fda0b6 107
f6bcfd97
BP
108 if ( hCheckbox < s_checkSize )
109 hCheckbox = s_checkSize;
4438caf4
VZ
110 }
111 else
2bda0e17 112 {
f6bcfd97
BP
113 wCheckbox = s_checkSize;
114 hCheckbox = s_checkSize;
2bda0e17
KB
115 }
116
4438caf4 117 return wxSize(wCheckbox, hCheckbox);
2bda0e17
KB
118}
119
debe6624 120void wxCheckBox::SetValue(bool val)
2bda0e17 121{
4438caf4 122 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
2bda0e17
KB
123}
124
bfc6fde4 125bool wxCheckBox::GetValue() const
2bda0e17 126{
2b5f62a0 127 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) & BST_CHECKED) != 0;
2bda0e17
KB
128}
129
2b5f62a0 130void wxCheckBox::Command(wxCommandEvent& event)
2bda0e17 131{
2b5f62a0
VZ
132 SetValue(event.GetInt() != 0);
133 ProcessCommand(event);
2bda0e17 134}
1e6feb95
VZ
135
136#endif // wxUSE_CHECKBOX