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