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