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