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