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