1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "checkbox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
25 #include "wx/checkbox.h"
26 #include "wx/tglbtn.h"
30 #pragma message disable nosimpint
33 #include <Xm/LabelG.h>
34 #include <Xm/ToggleB.h>
35 #include <Xm/ToggleBG.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
45 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
47 // Single check box item
48 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
54 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
58 wxString
label1(wxStripMenuCodes(label
));
59 wxXmString
text( label1
);
61 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
63 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
64 xmToggleButtonWidgetClass
, parentWidget
,
65 wxFont::GetFontTag(), m_font
.GetFontType(XtDisplay(parentWidget
)),
66 XmNlabelString
, text(),
67 XmNrecomputeSize
, False
,
68 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
69 // XmNfillOnSelect, False,
70 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
73 XtAddCallback( (Widget
)m_mainWidget
,
74 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
77 XmToggleButtonSetState ((Widget
) m_mainWidget
, FALSE
, TRUE
);
79 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
80 pos
.x
, pos
.y
, size
.x
, size
.y
);
82 ChangeBackgroundColour();
86 void wxCheckBox::SetValue(bool val
)
90 Set3StateValue(wxCHK_CHECKED
);
94 Set3StateValue(wxCHK_UNCHECKED
);
98 bool wxCheckBox::GetValue() const
100 return (Get3StateValue() != 0);
103 void wxCheckBox::Command (wxCommandEvent
& event
)
105 int state
= event
.GetInt();
106 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
107 || (state
== wxCHK_UNDETERMINED
),
108 wxT("event.GetInt() returned an invalid checkbox state") );
110 Set3StateValue((wxCheckBoxState
) state
);
111 ProcessCommand(event
);
114 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
115 XtPointer
WXUNUSED(ptr
))
117 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
119 if (item
->InSetValue())
122 wxCheckBoxState state
= item
->Get3StateValue();
124 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
126 state
= wxCHK_UNCHECKED
;
127 item
->Set3StateValue( state
);
130 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
131 event
.SetInt( (int)state
);
132 event
.SetEventObject( item
);
133 item
->ProcessCommand( event
);
136 void wxCheckBox::ChangeBackgroundColour()
138 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
141 XtVaSetValues ((Widget
) m_mainWidget
,
142 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
143 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
144 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
145 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
148 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
150 // Better to have the checkbox selection in black, or it's
151 // hard to determine what state it is in.
152 XtVaSetValues ((Widget
) m_mainWidget
,
153 XmNselectColor
, selectPixel
,
157 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
165 case wxCHK_UNCHECKED
: value
= XmUNSET
; break;
166 case wxCHK_CHECKED
: value
= XmSET
; break;
167 case wxCHK_UNDETERMINED
: value
= XmINDETERMINATE
; break;
170 XtVaSetValues( (Widget
) m_mainWidget
,
174 m_inSetValue
= false;
177 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
179 unsigned char value
= 0;
181 XtVaGetValues( (Widget
) m_mainWidget
,
187 case XmUNSET
: return wxCHK_UNCHECKED
;
188 case XmSET
: return wxCHK_CHECKED
;
189 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
193 return wxCHK_UNDETERMINED
;
196 ///////////////////////////////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////////////////////////////
202 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
203 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
205 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
206 const wxString
& label
,
210 const wxValidator
& val
,
211 const wxString
&name
)
213 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
216 XtVaSetValues( (Widget
)m_mainWidget
,
218 #if XmVersion >= 2000
219 XmNindicatorOn
, XmINDICATOR_NONE
,
221 XmNindicatorOn
, False
,
223 XmNfillOnSelect
, False
,
224 XmNshadowThickness
, 2,
225 XmNalignment
, XmALIGNMENT_CENTER
,
230 // set it again, because the XtVaSetValue above resets it
231 if( size
.x
!= -1 || size
.y
!= -1 )
237 #endif // wxUSE_TOGGLEBUTTON