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
47 #include "wx/motif/private.h"
49 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
52 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
54 // Single check box item
55 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
61 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
66 wxXmString
text( GetLabelText(label
) );
68 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
70 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
71 xmToggleButtonWidgetClass
, parentWidget
,
72 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
73 XmNlabelString
, text(),
74 XmNrecomputeSize
, False
,
75 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
76 // XmNfillOnSelect, False,
78 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
82 XtAddCallback( (Widget
)m_mainWidget
,
83 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
86 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
89 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
90 pos
.x
, pos
.y
, size
.x
, size
.y
);
95 void wxCheckBox::SetValue(bool val
)
99 Set3StateValue(wxCHK_CHECKED
);
103 Set3StateValue(wxCHK_UNCHECKED
);
107 bool wxCheckBox::GetValue() const
109 return (Get3StateValue() != 0);
112 void wxCheckBox::Command (wxCommandEvent
& event
)
114 int state
= event
.GetInt();
115 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
116 || (state
== wxCHK_UNDETERMINED
),
117 wxT("event.GetInt() returned an invalid checkbox state") );
119 Set3StateValue((wxCheckBoxState
) state
);
120 ProcessCommand(event
);
123 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
124 XtPointer
WXUNUSED(ptr
))
126 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
128 if (item
->InSetValue())
131 wxCheckBoxState state
= item
->Get3StateValue();
133 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
135 state
= wxCHK_UNCHECKED
;
136 item
->Set3StateValue( state
);
139 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
140 event
.SetInt( (int)state
);
141 event
.SetEventObject( item
);
142 item
->ProcessCommand( event
);
145 void wxCheckBox::ChangeBackgroundColour()
147 if (!m_backgroundColour
.Ok())
150 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
153 XtVaSetValues ((Widget
) m_mainWidget
,
154 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
155 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
156 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
157 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
160 wxColour colour
= *wxBLACK
;
161 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
163 // Better to have the checkbox selection in black, or it's
164 // hard to determine what state it is in.
165 XtVaSetValues ((Widget
) m_mainWidget
,
166 XmNselectColor
, selectPixel
,
170 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
179 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
180 case wxCHK_CHECKED
: value
= XmSET
; break;
181 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
182 default: wxASSERT(0); return;
185 XtVaSetValues( (Widget
) m_mainWidget
,
189 XmToggleButtonSetState ((Widget
) m_mainWidget
,
190 state
== wxCHK_CHECKED
, True
);
193 m_inSetValue
= false;
196 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
199 unsigned char value
= 0;
201 XtVaGetValues( (Widget
) m_mainWidget
,
207 case XmUNSET
: return wxCHK_UNCHECKED
;
208 case XmSET
: return wxCHK_CHECKED
;
209 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
213 return wxCHK_UNDETERMINED
;
215 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
219 ///////////////////////////////////////////////////////////////////////////////
221 ///////////////////////////////////////////////////////////////////////////////
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
226 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
228 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
229 const wxString
& label
,
233 const wxValidator
& val
,
234 const wxString
&name
)
236 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
239 XtVaSetValues( (Widget
)m_mainWidget
,
241 #if XmVersion >= 2000
242 XmNindicatorOn
, XmINDICATOR_NONE
,
244 XmNindicatorOn
, False
,
246 XmNfillOnSelect
, False
,
247 XmNshadowThickness
, 2,
248 XmNalignment
, XmALIGNMENT_CENTER
,
253 // set it again, because the XtVaSetValue above resets it
254 if( size
.x
!= -1 || size
.y
!= -1 )
260 #endif // wxUSE_TOGGLEBUTTON