]> git.saurik.com Git - wxWidgets.git/blob - src/motif/checkbox.cpp
Use event modifiers and accessors rather than m_ variables directly, which are now...
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "checkbox.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtDisplay XTDISPLAY
21 #define XmNtoggleMode 0
22 #define XmTOGGLE_INDETERMINATE 1
23 #define XmTOGGLE_BOOLEAN 2
24 #define XmUNSET 3
25 #define XmSET 4
26 #define XmINDETERMINATE 5
27 #endif
28
29 #include "wx/defs.h"
30
31 #include "wx/checkbox.h"
32 #include "wx/tglbtn.h"
33 #include "wx/utils.h"
34
35 #ifdef __VMS__
36 #pragma message disable nosimpint
37 #endif
38 #include <Xm/Label.h>
39 #include <Xm/LabelG.h>
40 #include <Xm/ToggleB.h>
41 #include <Xm/ToggleBG.h>
42 #ifdef __VMS__
43 #pragma message enable nosimpint
44 #endif
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.GetFontType(XtDisplay(parentWidget)),
72 XmNlabelString, text(),
73 XmNrecomputeSize, False,
74 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
75 // XmNfillOnSelect, False,
76 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
77 NULL);
78
79 XtAddCallback( (Widget)m_mainWidget,
80 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
81 (XtPointer)this );
82
83 XmToggleButtonSetState ((Widget) m_mainWidget, FALSE, TRUE);
84
85 AttachWidget( parent, m_mainWidget, (WXWidget)NULL,
86 pos.x, pos.y, size.x, size.y );
87
88 ChangeBackgroundColour();
89 return TRUE;
90 }
91
92 void wxCheckBox::SetValue(bool val)
93 {
94 if (val)
95 {
96 Set3StateValue(wxCHK_CHECKED);
97 }
98 else
99 {
100 Set3StateValue(wxCHK_UNCHECKED);
101 }
102 }
103
104 bool wxCheckBox::GetValue() const
105 {
106 return (Get3StateValue() != 0);
107 }
108
109 void wxCheckBox::Command (wxCommandEvent & event)
110 {
111 int state = event.GetInt();
112 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
113 || (state == wxCHK_UNDETERMINED),
114 wxT("event.GetInt() returned an invalid checkbox state") );
115
116 Set3StateValue((wxCheckBoxState) state);
117 ProcessCommand(event);
118 }
119
120 void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
121 XtPointer WXUNUSED(ptr))
122 {
123 wxCheckBox *item = (wxCheckBox *) clientData;
124
125 if (item->InSetValue())
126 return;
127
128 wxCheckBoxState state = item->Get3StateValue();
129
130 if( !item->Is3rdStateAllowedForUser() && state == wxCHK_UNDETERMINED )
131 {
132 state = wxCHK_UNCHECKED;
133 item->Set3StateValue( state );
134 }
135
136 wxCommandEvent event( item->m_evtType, item->GetId() );
137 event.SetInt( (int)state );
138 event.SetEventObject( item );
139 item->ProcessCommand( event );
140 }
141
142 void wxCheckBox::ChangeBackgroundColour()
143 {
144 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
145 (wxColour*) NULL);
146
147 XtVaSetValues ((Widget) m_mainWidget,
148 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
149 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
150 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
151 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
152 NULL);
153
154 int selectPixel = wxBLACK->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 unsigned char value;
168
169 switch (state)
170 {
171 case wxCHK_UNCHECKED: value = XmUNSET; break;
172 case wxCHK_CHECKED: value = XmSET; break;
173 case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break;
174 }
175
176 XtVaSetValues( (Widget) m_mainWidget,
177 XmNset, value,
178 NULL );
179
180 m_inSetValue = false;
181 }
182
183 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
184 {
185 unsigned char value = 0;
186
187 XtVaGetValues( (Widget) m_mainWidget,
188 XmNset, &value,
189 NULL );
190
191 switch (value)
192 {
193 case XmUNSET: return wxCHK_UNCHECKED;
194 case XmSET: return wxCHK_CHECKED;
195 case XmINDETERMINATE: return wxCHK_UNDETERMINED;
196 }
197
198 // impossible...
199 return wxCHK_UNDETERMINED;
200 }
201
202 ///////////////////////////////////////////////////////////////////////////////
203 // wxToggleButton
204 ///////////////////////////////////////////////////////////////////////////////
205
206 #if wxUSE_TOGGLEBTN
207
208 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
209 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
210
211 bool wxToggleButton::Create( wxWindow* parent, wxWindowID id,
212 const wxString& label,
213 const wxPoint& pos,
214 const wxSize& size,
215 long style,
216 const wxValidator& val,
217 const wxString &name )
218 {
219 if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) )
220 return false;
221
222 XtVaSetValues( (Widget)m_mainWidget,
223 XmNindicatorSize, 0,
224 #if XmVersion >= 2000
225 XmNindicatorOn, XmINDICATOR_NONE,
226 #else
227 XmNindicatorOn, False,
228 #endif
229 XmNfillOnSelect, False,
230 XmNshadowThickness, 2,
231 XmNalignment, XmALIGNMENT_CENTER,
232 XmNmarginLeft, 0,
233 XmNmarginRight, 0,
234 NULL );
235
236 // set it again, because the XtVaSetValue above resets it
237 if( size.x != -1 || size.y != -1 )
238 SetSize( size );
239
240 return true;
241 }
242
243 #endif // wxUSE_TOGGLEBUTTON