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