1 /////////////////////////////////////////////////////////////////////////////
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
21 #include "wx/checkbox.h"
22 #include "wx/tglbtn.h"
26 #pragma message disable nosimpint
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
33 #pragma message enable nosimpint
36 #include "wx/motif/private.h"
38 // define symbols that are missing in old versions of Motif.
39 #if wxCHECK_MOTIF_VERSION( 2, 0 )
40 #define wxHAS_3STATE 1
42 #define wxHAS_3STATE 0
46 #include "wx/motif/private.h"
48 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
51 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
53 // Single check box item
54 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
60 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
64 wxString
label1(wxStripMenuCodes(label
));
65 wxXmString
text( label1
);
67 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
69 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
70 xmToggleButtonWidgetClass
, parentWidget
,
71 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
72 XmNlabelString
, text(),
73 XmNrecomputeSize
, False
,
74 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
75 // XmNfillOnSelect, False,
77 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
81 XtAddCallback( (Widget
)m_mainWidget
,
82 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
85 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
87 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
88 pos
.x
, pos
.y
, size
.x
, size
.y
);
90 ChangeBackgroundColour();
94 void wxCheckBox::SetValue(bool val
)
98 Set3StateValue(wxCHK_CHECKED
);
102 Set3StateValue(wxCHK_UNCHECKED
);
106 bool wxCheckBox::GetValue() const
108 return (Get3StateValue() != 0);
111 void wxCheckBox::Command (wxCommandEvent
& event
)
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") );
118 Set3StateValue((wxCheckBoxState
) state
);
119 ProcessCommand(event
);
122 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
123 XtPointer
WXUNUSED(ptr
))
125 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
127 if (item
->InSetValue())
130 wxCheckBoxState state
= item
->Get3StateValue();
132 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
134 state
= wxCHK_UNCHECKED
;
135 item
->Set3StateValue( state
);
138 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
139 event
.SetInt( (int)state
);
140 event
.SetEventObject( item
);
141 item
->ProcessCommand( event
);
144 void wxCheckBox::ChangeBackgroundColour()
146 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
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
,
156 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
158 // Better to have the checkbox selection in black, or it's
159 // hard to determine what state it is in.
160 XtVaSetValues ((Widget
) m_mainWidget
,
161 XmNselectColor
, selectPixel
,
165 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
174 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
175 case wxCHK_CHECKED
: value
= XmSET
; break;
176 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
177 default: wxASSERT(0); return;
180 XtVaSetValues( (Widget
) m_mainWidget
,
184 XmToggleButtonSetState ((Widget
) m_mainWidget
,
185 (Boolean
) state
== wxCHK_CHECKED
, True
);
188 m_inSetValue
= false;
191 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
194 unsigned char value
= 0;
196 XtVaGetValues( (Widget
) m_mainWidget
,
202 case XmUNSET
: return wxCHK_UNCHECKED
;
203 case XmSET
: return wxCHK_CHECKED
;
204 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
208 return wxCHK_UNDETERMINED
;
210 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
214 ///////////////////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////////////////
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
221 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
223 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
224 const wxString
& label
,
228 const wxValidator
& val
,
229 const wxString
&name
)
231 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
234 XtVaSetValues( (Widget
)m_mainWidget
,
236 #if XmVersion >= 2000
237 XmNindicatorOn
, XmINDICATOR_NONE
,
239 XmNindicatorOn
, False
,
241 XmNfillOnSelect
, False
,
242 XmNshadowThickness
, 2,
243 XmNalignment
, XmALIGNMENT_CENTER
,
248 // set it again, because the XtVaSetValue above resets it
249 if( size
.x
!= -1 || size
.y
!= -1 )
255 #endif // wxUSE_TOGGLEBUTTON