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 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
);
88 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
89 pos
.x
, pos
.y
, size
.x
, size
.y
);
91 ChangeBackgroundColour();
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 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
150 XtVaSetValues ((Widget
) m_mainWidget
,
151 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
152 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
153 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
154 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
157 wxColour colour
= *wxBLACK
;
158 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
160 // Better to have the checkbox selection in black, or it's
161 // hard to determine what state it is in.
162 XtVaSetValues ((Widget
) m_mainWidget
,
163 XmNselectColor
, selectPixel
,
167 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
176 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
177 case wxCHK_CHECKED
: value
= XmSET
; break;
178 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
179 default: wxASSERT(0); return;
182 XtVaSetValues( (Widget
) m_mainWidget
,
186 XmToggleButtonSetState ((Widget
) m_mainWidget
,
187 state
== wxCHK_CHECKED
, True
);
190 m_inSetValue
= false;
193 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
196 unsigned char value
= 0;
198 XtVaGetValues( (Widget
) m_mainWidget
,
204 case XmUNSET
: return wxCHK_UNCHECKED
;
205 case XmSET
: return wxCHK_CHECKED
;
206 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
210 return wxCHK_UNDETERMINED
;
212 return wxCheckBoxState(XmToggleButtonGetState ((Widget
) m_mainWidget
));
216 ///////////////////////////////////////////////////////////////////////////////
218 ///////////////////////////////////////////////////////////////////////////////
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
223 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
225 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
226 const wxString
& label
,
230 const wxValidator
& val
,
231 const wxString
&name
)
233 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
236 XtVaSetValues( (Widget
)m_mainWidget
,
238 #if XmVersion >= 2000
239 XmNindicatorOn
, XmINDICATOR_NONE
,
241 XmNindicatorOn
, False
,
243 XmNfillOnSelect
, False
,
244 XmNshadowThickness
, 2,
245 XmNalignment
, XmALIGNMENT_CENTER
,
250 // set it again, because the XtVaSetValue above resets it
251 if( size
.x
!= -1 || size
.y
!= -1 )
257 #endif // wxUSE_TOGGLEBUTTON