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