]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checkbox.cpp
Committing in .
[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
47
02e8b2f9
JS
48#include "wx/motif/private.h"
49
50void wxCheckBoxCallback (Widget w, XtPointer clientData,
2d120f83 51 XtPointer ptr);
02e8b2f9 52
4bb6408c 53IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
4bb6408c
JS
54
55// Single check box item
56bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
57 const wxPoint& pos,
58 const wxSize& size, long style,
59 const wxValidator& validator,
60 const wxString& name)
4bb6408c 61{
a969055e
MB
62 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
63 name ) )
96be256b 64 return false;
7a4b8f27 65
a969055e 66 wxString label1(wxStripMenuCodes(label));
dc1efb1d 67 wxXmString text( label1 );
7520f3da 68
02e8b2f9 69 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3 70
02e8b2f9 71 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
2d120f83 72 xmToggleButtonWidgetClass, parentWidget,
73608949 73 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
7a4b8f27 74 XmNlabelString, text(),
d3a80c92 75 XmNrecomputeSize, False,
f8ab089a
MB
76 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
77 // XmNfillOnSelect, False,
c228fcb4 78#if wxHAS_3STATE
f8ab089a 79 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
c228fcb4 80#endif
2d120f83 81 NULL);
7520f3da 82
a969055e
MB
83 XtAddCallback( (Widget)m_mainWidget,
84 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
85 (XtPointer)this );
31528cd3 86
96be256b 87 XmToggleButtonSetState ((Widget) m_mainWidget, False, True);
31528cd3 88
a969055e
MB
89 AttachWidget( parent, m_mainWidget, (WXWidget)NULL,
90 pos.x, pos.y, size.x, size.y );
31528cd3 91
0d57be45 92 ChangeBackgroundColour();
96be256b 93 return true;
4bb6408c
JS
94}
95
96void wxCheckBox::SetValue(bool val)
97{
f8ab089a
MB
98 if (val)
99 {
100 Set3StateValue(wxCHK_CHECKED);
101 }
102 else
103 {
104 Set3StateValue(wxCHK_UNCHECKED);
105 }
4bb6408c
JS
106}
107
108bool wxCheckBox::GetValue() const
109{
f8ab089a 110 return (Get3StateValue() != 0);
4bb6408c
JS
111}
112
113void wxCheckBox::Command (wxCommandEvent & event)
114{
f8ab089a
MB
115 int state = event.GetInt();
116 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
117 || (state == wxCHK_UNDETERMINED),
118 wxT("event.GetInt() returned an invalid checkbox state") );
119
120 Set3StateValue((wxCheckBoxState) state);
121 ProcessCommand(event);
4bb6408c
JS
122}
123
af111fc3
JS
124void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
125 XtPointer WXUNUSED(ptr))
02e8b2f9 126{
2d120f83 127 wxCheckBox *item = (wxCheckBox *) clientData;
31528cd3 128
2d120f83
JS
129 if (item->InSetValue())
130 return;
31528cd3 131
f8ab089a
MB
132 wxCheckBoxState state = item->Get3StateValue();
133
134 if( !item->Is3rdStateAllowedForUser() && state == wxCHK_UNDETERMINED )
135 {
136 state = wxCHK_UNCHECKED;
137 item->Set3StateValue( state );
138 }
139
140 wxCommandEvent event( item->m_evtType, item->GetId() );
141 event.SetInt( (int)state );
142 event.SetEventObject( item );
143 item->ProcessCommand( event );
02e8b2f9 144}
0d57be45 145
0d57be45
JS
146void wxCheckBox::ChangeBackgroundColour()
147{
2d120f83
JS
148 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
149 (wxColour*) NULL);
31528cd3 150
2d120f83
JS
151 XtVaSetValues ((Widget) m_mainWidget,
152 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
153 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
154 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
155 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
156 NULL);
31528cd3 157
f516d986
VZ
158 wxColour colour = *wxBLACK;
159 int selectPixel = colour.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
c228fcb4 172#if wxHAS_3STATE
f8ab089a
MB
173 unsigned char value;
174
175 switch (state)
176 {
177 case wxCHK_UNCHECKED: value = XmUNSET; break;
178 case wxCHK_CHECKED: value = XmSET; break;
179 case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break;
55e3f1c4 180 default: wxASSERT(0); return;
f8ab089a
MB
181 }
182
183 XtVaSetValues( (Widget) m_mainWidget,
184 XmNset, value,
185 NULL );
c228fcb4
MB
186#else
187 XmToggleButtonSetState ((Widget) m_mainWidget,
4aa1c270 188 state == wxCHK_CHECKED, True);
c228fcb4 189#endif
f8ab089a
MB
190
191 m_inSetValue = false;
192}
193
194wxCheckBoxState wxCheckBox::DoGet3StateValue() const
195{
c228fcb4 196#if wxHAS_3STATE
f8ab089a
MB
197 unsigned char value = 0;
198
199 XtVaGetValues( (Widget) m_mainWidget,
200 XmNset, &value,
201 NULL );
202
203 switch (value)
204 {
205 case XmUNSET: return wxCHK_UNCHECKED;
206 case XmSET: return wxCHK_CHECKED;
207 case XmINDETERMINATE: return wxCHK_UNDETERMINED;
208 }
209
210 // impossible...
211 return wxCHK_UNDETERMINED;
c228fcb4
MB
212#else
213 return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget));
214#endif
f8ab089a
MB
215}
216
08e5319b
MB
217///////////////////////////////////////////////////////////////////////////////
218// wxToggleButton
219///////////////////////////////////////////////////////////////////////////////
220
221#if wxUSE_TOGGLEBTN
222
223DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
224IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
225
226bool wxToggleButton::Create( wxWindow* parent, wxWindowID id,
227 const wxString& label,
228 const wxPoint& pos,
229 const wxSize& size,
230 long style,
231 const wxValidator& val,
232 const wxString &name )
233{
234 if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) )
235 return false;
236
237 XtVaSetValues( (Widget)m_mainWidget,
238 XmNindicatorSize, 0,
239#if XmVersion >= 2000
240 XmNindicatorOn, XmINDICATOR_NONE,
241#else
242 XmNindicatorOn, False,
243#endif
244 XmNfillOnSelect, False,
245 XmNshadowThickness, 2,
246 XmNalignment, XmALIGNMENT_CENTER,
247 XmNmarginLeft, 0,
248 XmNmarginRight, 0,
249 NULL );
250
251 // set it again, because the XtVaSetValue above resets it
252 if( size.x != -1 || size.y != -1 )
253 SetSize( size );
254
255 return true;
256}
257
258#endif // wxUSE_TOGGLEBUTTON