]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checkbox.cpp
fix building with WXWIN_COMPATIBILITY_2_8 == 0
[wxWidgets.git] / src / motif / checkbox.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/motif/checkbox.cpp
4bb6408c
JS
3// Purpose: wxCheckBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
4bb6408c 7// Copyright: (c) Julian Smart
7520f3da 8// Licence: wxWindows licence
4bb6408c
JS
9/////////////////////////////////////////////////////////////////////////////
10
1248b41f
MB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
4bb6408c 14#include "wx/checkbox.h"
de6185e2
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/utils.h"
18#endif
19
08e5319b 20#include "wx/tglbtn.h"
4bb6408c 21
338dd992
JJ
22#ifdef __VMS__
23#pragma message disable nosimpint
24#endif
02e8b2f9
JS
25#include <Xm/Label.h>
26#include <Xm/LabelG.h>
27#include <Xm/ToggleB.h>
28#include <Xm/ToggleBG.h>
338dd992
JJ
29#ifdef __VMS__
30#pragma message enable nosimpint
31#endif
02e8b2f9 32
c228fcb4
MB
33#include "wx/motif/private.h"
34
aab52ea5 35// define symbols that are missing in old versions of Motif.
c228fcb4
MB
36#if wxCHECK_MOTIF_VERSION( 2, 0 )
37 #define wxHAS_3STATE 1
38#else
39 #define wxHAS_3STATE 0
aab52ea5
SN
40#endif
41
02e8b2f9
JS
42#include "wx/motif/private.h"
43
44void wxCheckBoxCallback (Widget w, XtPointer clientData,
2d120f83 45 XtPointer ptr);
02e8b2f9 46
4bb6408c
JS
47// Single check box item
48bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
49 const wxPoint& pos,
50 const wxSize& size, long style,
51 const wxValidator& validator,
52 const wxString& name)
4bb6408c 53{
a969055e
MB
54 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
55 name ) )
96be256b 56 return false;
105fbe1f 57 PreCreation();
7a4b8f27 58
32cd189d 59 wxXmString text( GetLabelText(label) );
7520f3da 60
02e8b2f9 61 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3 62
02e8b2f9 63 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
2d120f83 64 xmToggleButtonWidgetClass, parentWidget,
73608949 65 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
7a4b8f27 66 XmNlabelString, text(),
d3a80c92 67 XmNrecomputeSize, False,
f8ab089a
MB
68 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
69 // XmNfillOnSelect, False,
c228fcb4 70#if wxHAS_3STATE
f8ab089a 71 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
c228fcb4 72#endif
2d120f83 73 NULL);
7520f3da 74
a969055e
MB
75 XtAddCallback( (Widget)m_mainWidget,
76 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
77 (XtPointer)this );
31528cd3 78
96be256b 79 XmToggleButtonSetState ((Widget) m_mainWidget, False, True);
31528cd3 80
105fbe1f 81 PostCreation();
a969055e
MB
82 AttachWidget( parent, m_mainWidget, (WXWidget)NULL,
83 pos.x, pos.y, size.x, size.y );
31528cd3 84
96be256b 85 return true;
4bb6408c
JS
86}
87
88void wxCheckBox::SetValue(bool val)
89{
f8ab089a
MB
90 if (val)
91 {
92 Set3StateValue(wxCHK_CHECKED);
93 }
94 else
95 {
96 Set3StateValue(wxCHK_UNCHECKED);
97 }
4bb6408c
JS
98}
99
100bool wxCheckBox::GetValue() const
101{
f8ab089a 102 return (Get3StateValue() != 0);
4bb6408c
JS
103}
104
105void wxCheckBox::Command (wxCommandEvent & event)
106{
f8ab089a
MB
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);
4bb6408c
JS
114}
115
af111fc3
JS
116void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
117 XtPointer WXUNUSED(ptr))
02e8b2f9 118{
2d120f83 119 wxCheckBox *item = (wxCheckBox *) clientData;
31528cd3 120
2d120f83
JS
121 if (item->InSetValue())
122 return;
31528cd3 123
f8ab089a
MB
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 );
02e8b2f9 136}
0d57be45 137
0d57be45
JS
138void wxCheckBox::ChangeBackgroundColour()
139{
a1b806b9 140 if (!m_backgroundColour.IsOk())
105fbe1f
MB
141 return;
142
2d120f83 143 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
d3b9f782 144 NULL);
31528cd3 145
2d120f83
JS
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);
31528cd3 152
f516d986 153 wxColour colour = *wxBLACK;
3e0071d9 154 WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
155
156 // Better to have the checkbox selection in black, or it's
157 // hard to determine what state it is in.
2d120f83 158 XtVaSetValues ((Widget) m_mainWidget,
9838df2c 159 XmNselectColor, selectPixel,
3e0071d9 160 NULL);
0d57be45 161}
08e5319b 162
f8ab089a
MB
163void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
164{
165 m_inSetValue = true;
166
c228fcb4 167#if wxHAS_3STATE
f8ab089a
MB
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;
55e3f1c4 175 default: wxASSERT(0); return;
f8ab089a
MB
176 }
177
178 XtVaSetValues( (Widget) m_mainWidget,
179 XmNset, value,
180 NULL );
c228fcb4
MB
181#else
182 XmToggleButtonSetState ((Widget) m_mainWidget,
4aa1c270 183 state == wxCHK_CHECKED, True);
c228fcb4 184#endif
f8ab089a
MB
185
186 m_inSetValue = false;
187}
188
189wxCheckBoxState wxCheckBox::DoGet3StateValue() const
190{
c228fcb4 191#if wxHAS_3STATE
f8ab089a
MB
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;
c228fcb4
MB
207#else
208 return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget));
209#endif
f8ab089a
MB
210}
211
08e5319b
MB
212///////////////////////////////////////////////////////////////////////////////
213// wxToggleButton
214///////////////////////////////////////////////////////////////////////////////
215
216#if wxUSE_TOGGLEBTN
217
ce7fe42e 218wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
08e5319b
MB
219IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
220
221bool 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
b0f76951 253#endif // wxUSE_TOGGLEBTN