]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checkbox.cpp
Silence warning about truncation since the comment says it ok
[wxWidgets.git] / src / motif / checkbox.cpp
CommitLineData
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
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
2349d7c7
SN
15#ifdef __VMS
16#define XtDisplay XTDISPLAY
17#endif
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
c228fcb4
MB
36#include "wx/motif/private.h"
37
aab52ea5 38// define symbols that are missing in old versions of Motif.
c228fcb4
MB
39#if wxCHECK_MOTIF_VERSION( 2, 0 )
40 #define wxHAS_3STATE 1
41#else
42 #define wxHAS_3STATE 0
aab52ea5
SN
43#endif
44
45
02e8b2f9
JS
46#include "wx/motif/private.h"
47
48void wxCheckBoxCallback (Widget w, XtPointer clientData,
2d120f83 49 XtPointer ptr);
02e8b2f9 50
4bb6408c 51IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
4bb6408c
JS
52
53// Single check box item
54bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
55 const wxPoint& pos,
56 const wxSize& size, long style,
57 const wxValidator& validator,
58 const wxString& name)
4bb6408c 59{
a969055e
MB
60 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
61 name ) )
96be256b 62 return false;
7a4b8f27 63
a969055e 64 wxString label1(wxStripMenuCodes(label));
dc1efb1d 65 wxXmString text( label1 );
7a4b8f27 66
02e8b2f9 67 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3 68
02e8b2f9 69 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
2d120f83 70 xmToggleButtonWidgetClass, parentWidget,
73608949 71 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
7a4b8f27 72 XmNlabelString, text(),
d3a80c92 73 XmNrecomputeSize, False,
f8ab089a
MB
74 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
75 // XmNfillOnSelect, False,
c228fcb4 76#if wxHAS_3STATE
f8ab089a 77 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
c228fcb4 78#endif
2d120f83 79 NULL);
7a4b8f27 80
a969055e
MB
81 XtAddCallback( (Widget)m_mainWidget,
82 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
83 (XtPointer)this );
31528cd3 84
96be256b 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();
96be256b 91 return true;
4bb6408c
JS
92}
93
94void 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
106bool wxCheckBox::GetValue() const
107{
f8ab089a 108 return (Get3StateValue() != 0);
4bb6408c
JS
109}
110
111void 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
122void 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
144void 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
165void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
166{
167 m_inSetValue = true;
168
c228fcb4 169#if wxHAS_3STATE
f8ab089a
MB
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;
55e3f1c4 177 default: wxASSERT(0); return;
f8ab089a
MB
178 }
179
180 XtVaSetValues( (Widget) m_mainWidget,
181 XmNset, value,
182 NULL );
c228fcb4
MB
183#else
184 XmToggleButtonSetState ((Widget) m_mainWidget,
4aa1c270 185 state == wxCHK_CHECKED, True);
c228fcb4 186#endif
f8ab089a
MB
187
188 m_inSetValue = false;
189}
190
191wxCheckBoxState wxCheckBox::DoGet3StateValue() const
192{
c228fcb4 193#if wxHAS_3STATE
f8ab089a
MB
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;
c228fcb4
MB
209#else
210 return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget));
211#endif
f8ab089a
MB
212}
213
08e5319b
MB
214///////////////////////////////////////////////////////////////////////////////
215// wxToggleButton
216///////////////////////////////////////////////////////////////////////////////
217
218#if wxUSE_TOGGLEBTN
219
220DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
221IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
222
223bool 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