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 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
50 // Single check box item
51 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
53 const wxSize
& size
, long style
,
54 const wxValidator
& validator
,
57 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
62 wxXmString
text( GetLabelText(label
) );
64 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
66 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
67 xmToggleButtonWidgetClass
, parentWidget
,
68 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
69 XmNlabelString
, text(),
70 XmNrecomputeSize
, False
,
71 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
72 // XmNfillOnSelect, False,
74 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
78 XtAddCallback( (Widget
)m_mainWidget
,
79 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
82 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
85 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
86 pos
.x
, pos
.y
, size
.x
, size
.y
);
91 void wxCheckBox::SetValue(bool val
)
95 Set3StateValue(wxCHK_CHECKED
);
99 Set3StateValue(wxCHK_UNCHECKED
);
103 bool wxCheckBox::GetValue() const
105 return (Get3StateValue() != 0);
108 void wxCheckBox::Command (wxCommandEvent
& event
)
110 int state
= event
.GetInt();
111 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
112 || (state
== wxCHK_UNDETERMINED
),
113 wxT("event.GetInt() returned an invalid checkbox state") );
115 Set3StateValue((wxCheckBoxState
) state
);
116 ProcessCommand(event
);
119 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
120 XtPointer
WXUNUSED(ptr
))
122 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
124 if (item
->InSetValue())
127 wxCheckBoxState state
= item
->Get3StateValue();
129 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
131 state
= wxCHK_UNCHECKED
;
132 item
->Set3StateValue( state
);
135 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
136 event
.SetInt( (int)state
);
137 event
.SetEventObject( item
);
138 item
->ProcessCommand( event
);
141 void wxCheckBox::ChangeBackgroundColour()
143 if (!m_backgroundColour
.Ok())
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 wxColour colour
= *wxBLACK
;
157 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
159 // Better to have the checkbox selection in black, or it's
160 // hard to determine what state it is in.
161 XtVaSetValues ((Widget
) m_mainWidget
,
162 XmNselectColor
, selectPixel
,
166 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
175 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
176 case wxCHK_CHECKED
: value
= XmSET
; break;
177 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
178 default: wxASSERT(0); return;
181 XtVaSetValues( (Widget
) m_mainWidget
,
185 XmToggleButtonSetState ((Widget
) m_mainWidget
,
186 state
== wxCHK_CHECKED
, True
);
189 m_inSetValue
= false;
192 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
195 unsigned char value
= 0;
197 XtVaGetValues( (Widget
) m_mainWidget
,
203 case XmUNSET
: return wxCHK_UNCHECKED
;
204 case XmSET
: return wxCHK_CHECKED
;
205 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
209 return wxCHK_UNDETERMINED
;
211 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
215 ///////////////////////////////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////////////////////////////
221 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
);
222 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
224 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
225 const wxString
& label
,
229 const wxValidator
& val
,
230 const wxString
&name
)
232 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
235 XtVaSetValues( (Widget
)m_mainWidget
,
237 #if XmVersion >= 2000
238 XmNindicatorOn
, XmINDICATOR_NONE
,
240 XmNindicatorOn
, False
,
242 XmNfillOnSelect
, False
,
243 XmNshadowThickness
, 2,
244 XmNalignment
, XmALIGNMENT_CENTER
,
249 // set it again, because the XtVaSetValue above resets it
250 if( size
.x
!= -1 || size
.y
!= -1 )
256 #endif // wxUSE_TOGGLEBTN