]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checkbox.cpp
Implement wxRadioButton Get/SetLabel methods.
[wxWidgets.git] / src / motif / checkbox.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/motif/checkbox.cpp
4bb6408c
JS
3// Purpose: wxCheckBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
7520f3da 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
2349d7c7
SN
15#ifdef __VMS
16#define XtDisplay XTDISPLAY
17#endif
18
4bb6408c 19#include "wx/checkbox.h"
de6185e2
WS
20
21#ifndef WX_PRECOMP
22 #include "wx/utils.h"
23#endif
24
08e5319b 25#include "wx/tglbtn.h"
4bb6408c 26
338dd992
JJ
27#ifdef __VMS__
28#pragma message disable nosimpint
29#endif
02e8b2f9
JS
30#include <Xm/Label.h>
31#include <Xm/LabelG.h>
32#include <Xm/ToggleB.h>
33#include <Xm/ToggleBG.h>
338dd992
JJ
34#ifdef __VMS__
35#pragma message enable nosimpint
36#endif
02e8b2f9 37
c228fcb4
MB
38#include "wx/motif/private.h"
39
aab52ea5 40// define symbols that are missing in old versions of Motif.
c228fcb4
MB
41#if wxCHECK_MOTIF_VERSION( 2, 0 )
42 #define wxHAS_3STATE 1
43#else
44 #define wxHAS_3STATE 0
aab52ea5
SN
45#endif
46
02e8b2f9
JS
47#include "wx/motif/private.h"
48
49void wxCheckBoxCallback (Widget w, XtPointer clientData,
2d120f83 50 XtPointer ptr);
02e8b2f9 51
4bb6408c 52IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
4bb6408c
JS
53
54// Single check box item
55bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
56 const wxPoint& pos,
57 const wxSize& size, long style,
58 const wxValidator& validator,
59 const wxString& name)
4bb6408c 60{
a969055e
MB
61 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
62 name ) )
96be256b 63 return false;
105fbe1f 64 PreCreation();
7a4b8f27 65
32cd189d 66 wxXmString text( GetLabelText(label) );
7520f3da 67
02e8b2f9 68 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3 69
02e8b2f9 70 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
2d120f83 71 xmToggleButtonWidgetClass, parentWidget,
73608949 72 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
7a4b8f27 73 XmNlabelString, text(),
d3a80c92 74 XmNrecomputeSize, False,
f8ab089a
MB
75 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
76 // XmNfillOnSelect, False,
c228fcb4 77#if wxHAS_3STATE
f8ab089a 78 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
c228fcb4 79#endif
2d120f83 80 NULL);
7520f3da 81
a969055e
MB
82 XtAddCallback( (Widget)m_mainWidget,
83 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
84 (XtPointer)this );
31528cd3 85
96be256b 86 XmToggleButtonSetState ((Widget) m_mainWidget, False, True);
31528cd3 87
105fbe1f 88 PostCreation();
a969055e
MB
89 AttachWidget( parent, m_mainWidget, (WXWidget)NULL,
90 pos.x, pos.y, size.x, size.y );
31528cd3 91
96be256b 92 return true;
4bb6408c
JS
93}
94
95void wxCheckBox::SetValue(bool val)
96{
f8ab089a
MB
97 if (val)
98 {
99 Set3StateValue(wxCHK_CHECKED);
100 }
101 else
102 {
103 Set3StateValue(wxCHK_UNCHECKED);
104 }
4bb6408c
JS
105}
106
107bool wxCheckBox::GetValue() const
108{
f8ab089a 109 return (Get3StateValue() != 0);
4bb6408c
JS
110}
111
112void wxCheckBox::Command (wxCommandEvent & event)
113{
f8ab089a
MB
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") );
118
119 Set3StateValue((wxCheckBoxState) state);
120 ProcessCommand(event);
4bb6408c
JS
121}
122
af111fc3
JS
123void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
124 XtPointer WXUNUSED(ptr))
02e8b2f9 125{
2d120f83 126 wxCheckBox *item = (wxCheckBox *) clientData;
31528cd3 127
2d120f83
JS
128 if (item->InSetValue())
129 return;
31528cd3 130
f8ab089a
MB
131 wxCheckBoxState state = item->Get3StateValue();
132
133 if( !item->Is3rdStateAllowedForUser() && state == wxCHK_UNDETERMINED )
134 {
135 state = wxCHK_UNCHECKED;
136 item->Set3StateValue( state );
137 }
138
139 wxCommandEvent event( item->m_evtType, item->GetId() );
140 event.SetInt( (int)state );
141 event.SetEventObject( item );
142 item->ProcessCommand( event );
02e8b2f9 143}
0d57be45 144
0d57be45
JS
145void wxCheckBox::ChangeBackgroundColour()
146{
105fbe1f
MB
147 if (!m_backgroundColour.Ok())
148 return;
149
2d120f83
JS
150 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
151 (wxColour*) NULL);
31528cd3 152
2d120f83
JS
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,
158 NULL);
31528cd3 159
f516d986 160 wxColour colour = *wxBLACK;
3e0071d9 161 WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
162
163 // Better to have the checkbox selection in black, or it's
164 // hard to determine what state it is in.
2d120f83 165 XtVaSetValues ((Widget) m_mainWidget,
9838df2c 166 XmNselectColor, selectPixel,
3e0071d9 167 NULL);
0d57be45 168}
08e5319b 169
f8ab089a
MB
170void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
171{
172 m_inSetValue = true;
173
c228fcb4 174#if wxHAS_3STATE
f8ab089a
MB
175 unsigned char value;
176
177 switch (state)
178 {
179 case wxCHK_UNCHECKED: value = XmUNSET; break;
180 case wxCHK_CHECKED: value = XmSET; break;
181 case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break;
55e3f1c4 182 default: wxASSERT(0); return;
f8ab089a
MB
183 }
184
185 XtVaSetValues( (Widget) m_mainWidget,
186 XmNset, value,
187 NULL );
c228fcb4
MB
188#else
189 XmToggleButtonSetState ((Widget) m_mainWidget,
4aa1c270 190 state == wxCHK_CHECKED, True);
c228fcb4 191#endif
f8ab089a
MB
192
193 m_inSetValue = false;
194}
195
196wxCheckBoxState wxCheckBox::DoGet3StateValue() const
197{
c228fcb4 198#if wxHAS_3STATE
f8ab089a
MB
199 unsigned char value = 0;
200
201 XtVaGetValues( (Widget) m_mainWidget,
202 XmNset, &value,
203 NULL );
204
205 switch (value)
206 {
207 case XmUNSET: return wxCHK_UNCHECKED;
208 case XmSET: return wxCHK_CHECKED;
209 case XmINDETERMINATE: return wxCHK_UNDETERMINED;
210 }
211
212 // impossible...
213 return wxCHK_UNDETERMINED;
c228fcb4
MB
214#else
215 return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget));
216#endif
f8ab089a
MB
217}
218
08e5319b
MB
219///////////////////////////////////////////////////////////////////////////////
220// wxToggleButton
221///////////////////////////////////////////////////////////////////////////////
222
223#if wxUSE_TOGGLEBTN
224
225DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
226IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
227
228bool wxToggleButton::Create( wxWindow* parent, wxWindowID id,
229 const wxString& label,
230 const wxPoint& pos,
231 const wxSize& size,
232 long style,
233 const wxValidator& val,
234 const wxString &name )
235{
236 if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) )
237 return false;
238
239 XtVaSetValues( (Widget)m_mainWidget,
240 XmNindicatorSize, 0,
241#if XmVersion >= 2000
242 XmNindicatorOn, XmINDICATOR_NONE,
243#else
244 XmNindicatorOn, False,
245#endif
246 XmNfillOnSelect, False,
247 XmNshadowThickness, 2,
248 XmNalignment, XmALIGNMENT_CENTER,
249 XmNmarginLeft, 0,
250 XmNmarginRight, 0,
251 NULL );
252
253 // set it again, because the XtVaSetValue above resets it
254 if( size.x != -1 || size.y != -1 )
255 SetSize( size );
256
257 return true;
258}
259
b0f76951 260#endif // wxUSE_TOGGLEBTN