1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/checkbox.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/checkbox.h"
21 #include "wx/tglbtn.h"
24 #pragma message disable nosimpint
27 #include <Xm/LabelG.h>
28 #include <Xm/ToggleB.h>
29 #include <Xm/ToggleBG.h>
31 #pragma message enable nosimpint
34 #include "wx/motif/private.h"
36 // define symbols that are missing in old versions of Motif.
37 #if wxCHECK_MOTIF_VERSION( 2, 0 )
38 #define wxHAS_3STATE 1
40 #define wxHAS_3STATE 0
43 #include "wx/motif/private.h"
45 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
48 // Single check box item
49 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
55 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
60 wxXmString
text( GetLabelText(label
) );
62 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
64 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
65 xmToggleButtonWidgetClass
, parentWidget
,
66 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
67 XmNlabelString
, text(),
68 XmNrecomputeSize
, False
,
69 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
70 // XmNfillOnSelect, False,
72 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
76 XtAddCallback( (Widget
)m_mainWidget
,
77 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
80 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
83 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
84 pos
.x
, pos
.y
, size
.x
, size
.y
);
89 void wxCheckBox::SetValue(bool val
)
93 Set3StateValue(wxCHK_CHECKED
);
97 Set3StateValue(wxCHK_UNCHECKED
);
101 bool wxCheckBox::GetValue() const
103 return (Get3StateValue() != 0);
106 void wxCheckBox::Command (wxCommandEvent
& event
)
108 int state
= event
.GetInt();
109 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
110 || (state
== wxCHK_UNDETERMINED
),
111 wxT("event.GetInt() returned an invalid checkbox state") );
113 Set3StateValue((wxCheckBoxState
) state
);
114 ProcessCommand(event
);
117 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
118 XtPointer
WXUNUSED(ptr
))
120 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
122 if (item
->InSetValue())
125 wxCheckBoxState state
= item
->Get3StateValue();
127 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
129 state
= wxCHK_UNCHECKED
;
130 item
->Set3StateValue( state
);
133 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
134 event
.SetInt( (int)state
);
135 event
.SetEventObject( item
);
136 item
->ProcessCommand( event
);
139 void wxCheckBox::ChangeBackgroundColour()
141 if (!m_backgroundColour
.IsOk())
144 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
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
,
154 wxColour colour
= *wxBLACK
;
155 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
157 // Better to have the checkbox selection in black, or it's
158 // hard to determine what state it is in.
159 XtVaSetValues ((Widget
) m_mainWidget
,
160 XmNselectColor
, selectPixel
,
164 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
173 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
174 case wxCHK_CHECKED
: value
= XmSET
; break;
175 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
176 default: wxASSERT(0); return;
179 XtVaSetValues( (Widget
) m_mainWidget
,
183 XmToggleButtonSetState ((Widget
) m_mainWidget
,
184 state
== wxCHK_CHECKED
, True
);
187 m_inSetValue
= false;
190 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
193 unsigned char value
= 0;
195 XtVaGetValues( (Widget
) m_mainWidget
,
201 case XmUNSET
: return wxCHK_UNCHECKED
;
202 case XmSET
: return wxCHK_CHECKED
;
203 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
207 return wxCHK_UNDETERMINED
;
209 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
213 ///////////////////////////////////////////////////////////////////////////////
215 ///////////////////////////////////////////////////////////////////////////////
219 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
);
220 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
222 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
223 const wxString
& label
,
227 const wxValidator
& val
,
228 const wxString
&name
)
230 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
233 XtVaSetValues( (Widget
)m_mainWidget
,
235 #if XmVersion >= 2000
236 XmNindicatorOn
, XmINDICATOR_NONE
,
238 XmNindicatorOn
, False
,
240 XmNfillOnSelect
, False
,
241 XmNshadowThickness
, 2,
242 XmNalignment
, XmALIGNMENT_CENTER
,
247 // set it again, because the XtVaSetValue above resets it
248 if( size
.x
!= -1 || size
.y
!= -1 )
254 #endif // wxUSE_TOGGLEBTN