]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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 | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "checkbox.h" |
14 | #endif | |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
bcd055ae JJ |
19 | #ifdef __VMS |
20 | #define XtDisplay XTDISPLAY | |
21 | #endif | |
22 | ||
f6045f99 GD |
23 | #include "wx/defs.h" |
24 | ||
4bb6408c | 25 | #include "wx/checkbox.h" |
08e5319b | 26 | #include "wx/tglbtn.h" |
9838df2c | 27 | #include "wx/utils.h" |
4bb6408c | 28 | |
338dd992 JJ |
29 | #ifdef __VMS__ |
30 | #pragma message disable nosimpint | |
31 | #endif | |
02e8b2f9 JS |
32 | #include <Xm/Label.h> |
33 | #include <Xm/LabelG.h> | |
34 | #include <Xm/ToggleB.h> | |
35 | #include <Xm/ToggleBG.h> | |
338dd992 JJ |
36 | #ifdef __VMS__ |
37 | #pragma message enable nosimpint | |
38 | #endif | |
02e8b2f9 JS |
39 | |
40 | #include "wx/motif/private.h" | |
41 | ||
42 | void wxCheckBoxCallback (Widget w, XtPointer clientData, | |
2d120f83 | 43 | XtPointer ptr); |
02e8b2f9 | 44 | |
4bb6408c | 45 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl) |
4bb6408c JS |
46 | |
47 | // Single check box item | |
48 | bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label, | |
2d120f83 JS |
49 | const wxPoint& pos, |
50 | const wxSize& size, long style, | |
51 | const wxValidator& validator, | |
52 | const wxString& name) | |
4bb6408c | 53 | { |
a969055e MB |
54 | if( !wxControl::CreateControl( parent, id, pos, size, style, validator, |
55 | name ) ) | |
56 | return FALSE; | |
7a4b8f27 | 57 | |
a969055e | 58 | wxString label1(wxStripMenuCodes(label)); |
dc1efb1d | 59 | wxXmString text( label1 ); |
7a4b8f27 | 60 | |
02e8b2f9 | 61 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
31528cd3 | 62 | |
02e8b2f9 | 63 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle", |
2d120f83 | 64 | xmToggleButtonWidgetClass, parentWidget, |
da494b40 | 65 | wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)), |
7a4b8f27 | 66 | XmNlabelString, text(), |
d3a80c92 | 67 | XmNrecomputeSize, False, |
f8ab089a MB |
68 | // XmNindicatorOn, XmINDICATOR_CHECK_BOX, |
69 | // XmNfillOnSelect, False, | |
70 | XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN, | |
2d120f83 | 71 | NULL); |
7a4b8f27 | 72 | |
a969055e MB |
73 | XtAddCallback( (Widget)m_mainWidget, |
74 | XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback, | |
75 | (XtPointer)this ); | |
31528cd3 | 76 | |
02e8b2f9 | 77 | XmToggleButtonSetState ((Widget) m_mainWidget, FALSE, TRUE); |
31528cd3 | 78 | |
a969055e MB |
79 | AttachWidget( parent, m_mainWidget, (WXWidget)NULL, |
80 | pos.x, pos.y, size.x, size.y ); | |
31528cd3 | 81 | |
0d57be45 | 82 | ChangeBackgroundColour(); |
02e8b2f9 | 83 | return TRUE; |
4bb6408c JS |
84 | } |
85 | ||
86 | void wxCheckBox::SetValue(bool val) | |
87 | { | |
f8ab089a MB |
88 | if (val) |
89 | { | |
90 | Set3StateValue(wxCHK_CHECKED); | |
91 | } | |
92 | else | |
93 | { | |
94 | Set3StateValue(wxCHK_UNCHECKED); | |
95 | } | |
4bb6408c JS |
96 | } |
97 | ||
98 | bool wxCheckBox::GetValue() const | |
99 | { | |
f8ab089a | 100 | return (Get3StateValue() != 0); |
4bb6408c JS |
101 | } |
102 | ||
103 | void wxCheckBox::Command (wxCommandEvent & event) | |
104 | { | |
f8ab089a MB |
105 | int state = event.GetInt(); |
106 | wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED) | |
107 | || (state == wxCHK_UNDETERMINED), | |
108 | wxT("event.GetInt() returned an invalid checkbox state") ); | |
109 | ||
110 | Set3StateValue((wxCheckBoxState) state); | |
111 | ProcessCommand(event); | |
4bb6408c JS |
112 | } |
113 | ||
af111fc3 JS |
114 | void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
115 | XtPointer WXUNUSED(ptr)) | |
02e8b2f9 | 116 | { |
2d120f83 | 117 | wxCheckBox *item = (wxCheckBox *) clientData; |
31528cd3 | 118 | |
2d120f83 JS |
119 | if (item->InSetValue()) |
120 | return; | |
31528cd3 | 121 | |
f8ab089a MB |
122 | wxCheckBoxState state = item->Get3StateValue(); |
123 | ||
124 | if( !item->Is3rdStateAllowedForUser() && state == wxCHK_UNDETERMINED ) | |
125 | { | |
126 | state = wxCHK_UNCHECKED; | |
127 | item->Set3StateValue( state ); | |
128 | } | |
129 | ||
130 | wxCommandEvent event( item->m_evtType, item->GetId() ); | |
131 | event.SetInt( (int)state ); | |
132 | event.SetEventObject( item ); | |
133 | item->ProcessCommand( event ); | |
02e8b2f9 | 134 | } |
0d57be45 | 135 | |
0d57be45 JS |
136 | void wxCheckBox::ChangeBackgroundColour() |
137 | { | |
2d120f83 JS |
138 | wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour, |
139 | (wxColour*) NULL); | |
31528cd3 | 140 | |
2d120f83 JS |
141 | XtVaSetValues ((Widget) m_mainWidget, |
142 | XmNbackground, g_itemColors[wxBACK_INDEX].pixel, | |
143 | XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel, | |
144 | XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel, | |
145 | XmNforeground, g_itemColors[wxFORE_INDEX].pixel, | |
146 | NULL); | |
31528cd3 | 147 | |
eb6fa4b4 | 148 | int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c JS |
149 | |
150 | // Better to have the checkbox selection in black, or it's | |
151 | // hard to determine what state it is in. | |
2d120f83 | 152 | XtVaSetValues ((Widget) m_mainWidget, |
9838df2c | 153 | XmNselectColor, selectPixel, |
2d120f83 | 154 | NULL); |
0d57be45 | 155 | } |
08e5319b | 156 | |
f8ab089a MB |
157 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) |
158 | { | |
159 | m_inSetValue = true; | |
160 | ||
161 | unsigned char value; | |
162 | ||
163 | switch (state) | |
164 | { | |
165 | case wxCHK_UNCHECKED: value = XmUNSET; break; | |
166 | case wxCHK_CHECKED: value = XmSET; break; | |
167 | case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break; | |
168 | } | |
169 | ||
170 | XtVaSetValues( (Widget) m_mainWidget, | |
171 | XmNset, value, | |
172 | NULL ); | |
173 | ||
174 | m_inSetValue = false; | |
175 | } | |
176 | ||
177 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const | |
178 | { | |
179 | unsigned char value = 0; | |
180 | ||
181 | XtVaGetValues( (Widget) m_mainWidget, | |
182 | XmNset, &value, | |
183 | NULL ); | |
184 | ||
185 | switch (value) | |
186 | { | |
187 | case XmUNSET: return wxCHK_UNCHECKED; | |
188 | case XmSET: return wxCHK_CHECKED; | |
189 | case XmINDETERMINATE: return wxCHK_UNDETERMINED; | |
190 | } | |
191 | ||
192 | // impossible... | |
193 | return wxCHK_UNDETERMINED; | |
194 | } | |
195 | ||
08e5319b MB |
196 | /////////////////////////////////////////////////////////////////////////////// |
197 | // wxToggleButton | |
198 | /////////////////////////////////////////////////////////////////////////////// | |
199 | ||
200 | #if wxUSE_TOGGLEBTN | |
201 | ||
202 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED) | |
203 | IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl) | |
204 | ||
205 | bool wxToggleButton::Create( wxWindow* parent, wxWindowID id, | |
206 | const wxString& label, | |
207 | const wxPoint& pos, | |
208 | const wxSize& size, | |
209 | long style, | |
210 | const wxValidator& val, | |
211 | const wxString &name ) | |
212 | { | |
213 | if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) ) | |
214 | return false; | |
215 | ||
216 | XtVaSetValues( (Widget)m_mainWidget, | |
217 | XmNindicatorSize, 0, | |
218 | #if XmVersion >= 2000 | |
219 | XmNindicatorOn, XmINDICATOR_NONE, | |
220 | #else | |
221 | XmNindicatorOn, False, | |
222 | #endif | |
223 | XmNfillOnSelect, False, | |
224 | XmNshadowThickness, 2, | |
225 | XmNalignment, XmALIGNMENT_CENTER, | |
226 | XmNmarginLeft, 0, | |
227 | XmNmarginRight, 0, | |
228 | NULL ); | |
229 | ||
230 | // set it again, because the XtVaSetValue above resets it | |
231 | if( size.x != -1 || size.y != -1 ) | |
232 | SetSize( size ); | |
233 | ||
234 | return true; | |
235 | } | |
236 | ||
237 | #endif // wxUSE_TOGGLEBUTTON |