]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/checkbox.cpp
applied part of patch 1169925: fix errors with multibyte <-> wx conversions
[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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13#pragma implementation "checkbox.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#endif
22
23#include "wx/defs.h"
24
25#include "wx/checkbox.h"
26#include "wx/tglbtn.h"
27#include "wx/utils.h"
28
29#ifdef __VMS__
30#pragma message disable nosimpint
31#endif
32#include <Xm/Label.h>
33#include <Xm/LabelG.h>
34#include <Xm/ToggleB.h>
35#include <Xm/ToggleBG.h>
36#ifdef __VMS__
37#pragma message enable nosimpint
38#endif
39
40// define symbols that are missing in old versions of Motif.
41#if (XmVersion < 2000)
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
51#include "wx/motif/private.h"
52
53void wxCheckBoxCallback (Widget w, XtPointer clientData,
54 XtPointer ptr);
55
56IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
57
58// Single check box item
59bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
60 const wxPoint& pos,
61 const wxSize& size, long style,
62 const wxValidator& validator,
63 const wxString& name)
64{
65 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
66 name ) )
67 return false;
68
69 wxString label1(wxStripMenuCodes(label));
70 wxXmString text( label1 );
71
72 Widget parentWidget = (Widget) parent->GetClientWidget();
73
74 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
75 xmToggleButtonWidgetClass, parentWidget,
76 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
77 XmNlabelString, text(),
78 XmNrecomputeSize, False,
79 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
80 // XmNfillOnSelect, False,
81 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
82 NULL);
83
84 XtAddCallback( (Widget)m_mainWidget,
85 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
86 (XtPointer)this );
87
88 XmToggleButtonSetState ((Widget) m_mainWidget, False, True);
89
90 AttachWidget( parent, m_mainWidget, (WXWidget)NULL,
91 pos.x, pos.y, size.x, size.y );
92
93 ChangeBackgroundColour();
94 return true;
95}
96
97void wxCheckBox::SetValue(bool val)
98{
99 if (val)
100 {
101 Set3StateValue(wxCHK_CHECKED);
102 }
103 else
104 {
105 Set3StateValue(wxCHK_UNCHECKED);
106 }
107}
108
109bool wxCheckBox::GetValue() const
110{
111 return (Get3StateValue() != 0);
112}
113
114void wxCheckBox::Command (wxCommandEvent & event)
115{
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);
123}
124
125void wxCheckBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
126 XtPointer WXUNUSED(ptr))
127{
128 wxCheckBox *item = (wxCheckBox *) clientData;
129
130 if (item->InSetValue())
131 return;
132
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 );
145}
146
147void wxCheckBox::ChangeBackgroundColour()
148{
149 wxComputeColours (XtDisplay((Widget) m_mainWidget), & m_backgroundColour,
150 (wxColour*) NULL);
151
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);
158
159 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
160
161 // Better to have the checkbox selection in black, or it's
162 // hard to determine what state it is in.
163 XtVaSetValues ((Widget) m_mainWidget,
164 XmNselectColor, selectPixel,
165 NULL);
166}
167
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;
179 default: wxASSERT(0); return;
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
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