1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/checkbox.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/checkbox.h"
20 #include "wx/tglbtn.h"
23 #pragma message disable nosimpint
26 #include <Xm/LabelG.h>
27 #include <Xm/ToggleB.h>
28 #include <Xm/ToggleBG.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 // define symbols that are missing in old versions of Motif.
36 #if wxCHECK_MOTIF_VERSION( 2, 0 )
37 #define wxHAS_3STATE 1
39 #define wxHAS_3STATE 0
42 #include "wx/motif/private.h"
44 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
47 // Single check box item
48 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
54 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
59 wxXmString
text( GetLabelText(label
) );
61 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
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,
71 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
75 XtAddCallback( (Widget
)m_mainWidget
,
76 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
79 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
82 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
83 pos
.x
, pos
.y
, size
.x
, size
.y
);
88 void wxCheckBox::SetValue(bool val
)
92 Set3StateValue(wxCHK_CHECKED
);
96 Set3StateValue(wxCHK_UNCHECKED
);
100 bool wxCheckBox::GetValue() const
102 return (Get3StateValue() != 0);
105 void wxCheckBox::Command (wxCommandEvent
& event
)
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") );
112 Set3StateValue((wxCheckBoxState
) state
);
113 ProcessCommand(event
);
116 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
117 XtPointer
WXUNUSED(ptr
))
119 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
121 if (item
->InSetValue())
124 wxCheckBoxState state
= item
->Get3StateValue();
126 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
128 state
= wxCHK_UNCHECKED
;
129 item
->Set3StateValue( state
);
132 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
133 event
.SetInt( (int)state
);
134 event
.SetEventObject( item
);
135 item
->ProcessCommand( event
);
138 void wxCheckBox::ChangeBackgroundColour()
140 if (!m_backgroundColour
.IsOk())
143 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
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
,
153 wxColour colour
= *wxBLACK
;
154 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
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
,
163 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
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;
178 XtVaSetValues( (Widget
) m_mainWidget
,
182 XmToggleButtonSetState ((Widget
) m_mainWidget
,
183 state
== wxCHK_CHECKED
, True
);
186 m_inSetValue
= false;
189 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
192 unsigned char value
= 0;
194 XtVaGetValues( (Widget
) m_mainWidget
,
200 case XmUNSET
: return wxCHK_UNCHECKED
;
201 case XmSET
: return wxCHK_CHECKED
;
202 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
206 return wxCHK_UNDETERMINED
;
208 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
212 ///////////////////////////////////////////////////////////////////////////////
214 ///////////////////////////////////////////////////////////////////////////////
218 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON
, wxCommandEvent
);
219 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
221 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
222 const wxString
& label
,
226 const wxValidator
& val
,
227 const wxString
&name
)
229 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
232 XtVaSetValues( (Widget
)m_mainWidget
,
234 #if XmVersion >= 2000
235 XmNindicatorOn
, XmINDICATOR_NONE
,
237 XmNindicatorOn
, False
,
239 XmNfillOnSelect
, False
,
240 XmNshadowThickness
, 2,
241 XmNalignment
, XmALIGNMENT_CENTER
,
246 // set it again, because the XtVaSetValue above resets it
247 if( size
.x
!= -1 || size
.y
!= -1 )
253 #endif // wxUSE_TOGGLEBTN