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 // define symbols that are missing in old versions of Motif.
41 #if (XmVersion < 2000)
42 #define XmNtoggleMode 0
43 #define XmTOGGLE_INDETERMINATE 1
44 #define XmTOGGLE_BOOLEAN 2
47 #define XmINDETERMINATE 5
51 #include "wx/motif/private.h"
53 void wxCheckBoxCallback (Widget w
, XtPointer clientData
,
56 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
58 // Single check box item
59 bool wxCheckBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
61 const wxSize
& size
, long style
,
62 const wxValidator
& validator
,
65 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
, validator
,
69 wxString
label1(wxStripMenuCodes(label
));
70 wxXmString
text( label1
);
72 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
74 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("toggle",
75 xmToggleButtonWidgetClass
, parentWidget
,
76 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
77 XmNlabelString
, text(),
78 XmNrecomputeSize
, False
,
79 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
80 // XmNfillOnSelect, False,
81 XmNtoggleMode
, Is3State() ? XmTOGGLE_INDETERMINATE
: XmTOGGLE_BOOLEAN
,
84 XtAddCallback( (Widget
)m_mainWidget
,
85 XmNvalueChangedCallback
, (XtCallbackProc
)wxCheckBoxCallback
,
88 XmToggleButtonSetState ((Widget
) m_mainWidget
, False
, True
);
90 AttachWidget( parent
, m_mainWidget
, (WXWidget
)NULL
,
91 pos
.x
, pos
.y
, size
.x
, size
.y
);
93 ChangeBackgroundColour();
97 void wxCheckBox::SetValue(bool val
)
101 Set3StateValue(wxCHK_CHECKED
);
105 Set3StateValue(wxCHK_UNCHECKED
);
109 bool wxCheckBox::GetValue() const
111 return (Get3StateValue() != 0);
114 void wxCheckBox::Command (wxCommandEvent
& event
)
116 int state
= event
.GetInt();
117 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
118 || (state
== wxCHK_UNDETERMINED
),
119 wxT("event.GetInt() returned an invalid checkbox state") );
121 Set3StateValue((wxCheckBoxState
) state
);
122 ProcessCommand(event
);
125 void wxCheckBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
126 XtPointer
WXUNUSED(ptr
))
128 wxCheckBox
*item
= (wxCheckBox
*) clientData
;
130 if (item
->InSetValue())
133 wxCheckBoxState state
= item
->Get3StateValue();
135 if( !item
->Is3rdStateAllowedForUser() && state
== wxCHK_UNDETERMINED
)
137 state
= wxCHK_UNCHECKED
;
138 item
->Set3StateValue( state
);
141 wxCommandEvent
event( item
->m_evtType
, item
->GetId() );
142 event
.SetInt( (int)state
);
143 event
.SetEventObject( item
);
144 item
->ProcessCommand( event
);
147 void wxCheckBox::ChangeBackgroundColour()
149 wxComputeColours (XtDisplay((Widget
) m_mainWidget
), & m_backgroundColour
,
152 XtVaSetValues ((Widget
) m_mainWidget
,
153 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
154 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
155 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
156 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
159 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
161 // Better to have the checkbox selection in black, or it's
162 // hard to determine what state it is in.
163 XtVaSetValues ((Widget
) m_mainWidget
,
164 XmNselectColor
, selectPixel
,
168 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 m_inSetValue
= false;
189 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
191 unsigned char value
= 0;
193 XtVaGetValues( (Widget
) m_mainWidget
,
199 case XmUNSET
: return wxCHK_UNCHECKED
;
200 case XmSET
: return wxCHK_CHECKED
;
201 case XmINDETERMINATE
: return wxCHK_UNDETERMINED
;
205 return wxCHK_UNDETERMINED
;
208 ///////////////////////////////////////////////////////////////////////////////
210 ///////////////////////////////////////////////////////////////////////////////
214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
215 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
217 bool wxToggleButton::Create( wxWindow
* parent
, wxWindowID id
,
218 const wxString
& label
,
222 const wxValidator
& val
,
223 const wxString
&name
)
225 if( !wxCheckBox::Create( parent
, id
, label
, pos
, size
, style
, val
, name
) )
228 XtVaSetValues( (Widget
)m_mainWidget
,
230 #if XmVersion >= 2000
231 XmNindicatorOn
, XmINDICATOR_NONE
,
233 XmNindicatorOn
, False
,
235 XmNfillOnSelect
, False
,
236 XmNshadowThickness
, 2,
237 XmNalignment
, XmALIGNMENT_CENTER
,
242 // set it again, because the XtVaSetValue above resets it
243 if( size
.x
!= -1 || size
.y
!= -1 )
249 #endif // wxUSE_TOGGLEBUTTON