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