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"
16 #define XtDisplay XTDISPLAY
19 #include "wx/checkbox.h"
25 #include "wx/tglbtn.h"
28 #pragma message disable nosimpint
31 #include <Xm/LabelG.h>
32 #include <Xm/ToggleB.h>
33 #include <Xm/ToggleBG.h>
35 #pragma message enable nosimpint
38 #include "wx/motif/private.h"
40 // define symbols that are missing in old versions of Motif.
41 #if wxCHECK_MOTIF_VERSION( 2, 0 )
42 #define wxHAS_3STATE 1
44 #define wxHAS_3STATE 0
48 #include "wx/motif/private.h"
50 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
53 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
55 // Single check box item
56 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
62 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
66 wxString
label1(wxStripMenuCodes(label
));
67 wxXmString
text( label1
);
69 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
71 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
72 xmToggleButtonWidgetClass
, parentWidget
,
73 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
74 XmNlabelString
, text(),
75 XmNrecomputeSize
, False
,
76 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
77 // XmNfillOnSelect, False,
79 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
83 XtAddCallback( (Widget
)m_mainWidget
,
84 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
87 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
89 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
90 pos
.x
, pos
.y
, size
.x
, size
.y
);
92 ChangeBackgroundColour();
96 void wxCheckBox::SetValue(bool val
)
100 Set3StateValue(wxCHK_CHECKED
);
104 Set3StateValue(wxCHK_UNCHECKED
);
108 bool wxCheckBox::GetValue() const
110 return (Get3StateValue() != 0);
113 void wxCheckBox::Command (wxCommandEvent
& event
)
115 int state
= event
.GetInt();
116 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
117 || (state
== wxCHK_UNDETERMINED
),
118 wxT("event.GetInt() returned an invalid checkbox state") );
120 Set3StateValue((wxCheckBoxState
) state
);
121 ProcessCommand(event
);
124 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
125 XtPointer
WXUNUSED(ptr
))
127 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
129 if (item
->InSetValue())
132 wxCheckBoxState state
= item
->Get3StateValue();
134 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
136 state
= wxCHK_UNCHECKED
;
137 item
->Set3StateValue( state
);
140 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
141 event
.SetInt( (int)state
);
142 event
.SetEventObject( item
);
143 item
->ProcessCommand( event
);
146 void wxCheckBox::ChangeBackgroundColour()
148 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
151 XtVaSetValues ((Widget
) m_mainWidget
,
152 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
153 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
154 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
155 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
158 wxColour colour
= *wxBLACK
;
159 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
161 // Better to have the checkbox selection in black, or it's
162 // hard to determine what state it is in.
163 XtVaSetValues ((Widget
) m_mainWidget
,
164 XmNselectColor
, selectPixel
,
168 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
177 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
178 case wxCHK_CHECKED
: value
= XmSET
; break;
179 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
180 default: wxASSERT(0); return;
183 XtVaSetValues( (Widget
) m_mainWidget
,
187 XmToggleButtonSetState ((Widget
) m_mainWidget
,
188 state
== wxCHK_CHECKED
, True
);
191 m_inSetValue
= false;
194 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
197 unsigned char value
= 0;
199 XtVaGetValues( (Widget
) m_mainWidget
,
205 case XmUNSET
: return wxCHK_UNCHECKED
;
206 case XmSET
: return wxCHK_CHECKED
;
207 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
211 return wxCHK_UNDETERMINED
;
213 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
217 ///////////////////////////////////////////////////////////////////////////////
219 ///////////////////////////////////////////////////////////////////////////////
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
224 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
226 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
227 const wxString
& label
,
231 const wxValidator
& val
,
232 const wxString
&name
)
234 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
237 XtVaSetValues( (Widget
)m_mainWidget
,
239 #if XmVersion >= 2000
240 XmNindicatorOn
, XmINDICATOR_NONE
,
242 XmNindicatorOn
, False
,
244 XmNfillOnSelect
, False
,
245 XmNshadowThickness
, 2,
246 XmNalignment
, XmALIGNMENT_CENTER
,
251 // set it again, because the XtVaSetValue above resets it
252 if( size
.x
!= -1 || size
.y
!= -1 )
258 #endif // wxUSE_TOGGLEBUTTON