]> git.saurik.com Git - wxWidgets.git/blame - src/motif/checkbox.cpp
Add code to remove the selection (if any) in wxTextCtrl::WriteText for multi-line...
[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"
08e5319b 20#include "wx/tglbtn.h"
9838df2c 21#include "wx/utils.h"
4bb6408c 22
338dd992
JJ
23#ifdef __VMS__
24#pragma message disable nosimpint
25#endif
02e8b2f9
JS
26#include <Xm/Label.h>
27#include <Xm/LabelG.h>
28#include <Xm/ToggleB.h>
29#include <Xm/ToggleBG.h>
338dd992
JJ
30#ifdef __VMS__
31#pragma message enable nosimpint
32#endif
02e8b2f9 33
c228fcb4
MB
34#include "wx/motif/private.h"
35
aab52ea5 36// define symbols that are missing in old versions of Motif.
c228fcb4
MB
37#if wxCHECK_MOTIF_VERSION( 2, 0 )
38 #define wxHAS_3STATE 1
39#else
40 #define wxHAS_3STATE 0
aab52ea5
SN
41#endif
42
43
02e8b2f9
JS
44#include "wx/motif/private.h"
45
46void wxCheckBoxCallback (Widget w, XtPointer clientData,
2d120f83 47 XtPointer ptr);
02e8b2f9 48
4bb6408c 49IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
4bb6408c
JS
50
51// Single check box item
52bool wxCheckBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
53 const wxPoint& pos,
54 const wxSize& size, long style,
55 const wxValidator& validator,
56 const wxString& name)
4bb6408c 57{
a969055e
MB
58 if( !wxControl::CreateControl( parent, id, pos, size, style, validator,
59 name ) )
96be256b 60 return false;
7a4b8f27 61
a969055e 62 wxString label1(wxStripMenuCodes(label));
dc1efb1d 63 wxXmString text( label1 );
7520f3da 64
02e8b2f9 65 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3 66
02e8b2f9 67 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("toggle",
2d120f83 68 xmToggleButtonWidgetClass, parentWidget,
73608949 69 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
7a4b8f27 70 XmNlabelString, text(),
d3a80c92 71 XmNrecomputeSize, False,
f8ab089a
MB
72 // XmNindicatorOn, XmINDICATOR_CHECK_BOX,
73 // XmNfillOnSelect, False,
c228fcb4 74#if wxHAS_3STATE
f8ab089a 75 XmNtoggleMode, Is3State() ? XmTOGGLE_INDETERMINATE : XmTOGGLE_BOOLEAN,
c228fcb4 76#endif
2d120f83 77 NULL);
7520f3da 78
a969055e
MB
79 XtAddCallback( (Widget)m_mainWidget,
80 XmNvalueChangedCallback, (XtCallbackProc)wxCheckBoxCallback,
81 (XtPointer)this );
31528cd3 82
96be256b 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();
96be256b 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
f516d986
VZ
154 wxColour colour = *wxBLACK;
155 int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
156
157 // Better to have the checkbox selection in black, or it's
158 // hard to determine what state it is in.
2d120f83 159 XtVaSetValues ((Widget) m_mainWidget,
9838df2c 160 XmNselectColor, selectPixel,
2d120f83 161 NULL);
0d57be45 162}
08e5319b 163
f8ab089a
MB
164void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
165{
166 m_inSetValue = true;
167
c228fcb4 168#if wxHAS_3STATE
f8ab089a
MB
169 unsigned char value;
170
171 switch (state)
172 {
173 case wxCHK_UNCHECKED: value = XmUNSET; break;
174 case wxCHK_CHECKED: value = XmSET; break;
175 case wxCHK_UNDETERMINED: value = XmINDETERMINATE; break;
55e3f1c4 176 default: wxASSERT(0); return;
f8ab089a
MB
177 }
178
179 XtVaSetValues( (Widget) m_mainWidget,
180 XmNset, value,
181 NULL );
c228fcb4
MB
182#else
183 XmToggleButtonSetState ((Widget) m_mainWidget,
4aa1c270 184 state == wxCHK_CHECKED, True);
c228fcb4 185#endif
f8ab089a
MB
186
187 m_inSetValue = false;
188}
189
190wxCheckBoxState wxCheckBox::DoGet3StateValue() const
191{
c228fcb4 192#if wxHAS_3STATE
f8ab089a
MB
193 unsigned char value = 0;
194
195 XtVaGetValues( (Widget) m_mainWidget,
196 XmNset, &value,
197 NULL );
198
199 switch (value)
200 {
201 case XmUNSET: return wxCHK_UNCHECKED;
202 case XmSET: return wxCHK_CHECKED;
203 case XmINDETERMINATE: return wxCHK_UNDETERMINED;
204 }
205
206 // impossible...
207 return wxCHK_UNDETERMINED;
c228fcb4
MB
208#else
209 return wxCheckBoxState(XmToggleButtonGetState ((Widget) m_mainWidget));
210#endif
f8ab089a
MB
211}
212
08e5319b
MB
213///////////////////////////////////////////////////////////////////////////////
214// wxToggleButton
215///////////////////////////////////////////////////////////////////////////////
216
217#if wxUSE_TOGGLEBTN
218
219DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
220IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
221
222bool wxToggleButton::Create( wxWindow* parent, wxWindowID id,
223 const wxString& label,
224 const wxPoint& pos,
225 const wxSize& size,
226 long style,
227 const wxValidator& val,
228 const wxString &name )
229{
230 if( !wxCheckBox::Create( parent, id, label, pos, size, style, val, name ) )
231 return false;
232
233 XtVaSetValues( (Widget)m_mainWidget,
234 XmNindicatorSize, 0,
235#if XmVersion >= 2000
236 XmNindicatorOn, XmINDICATOR_NONE,
237#else
238 XmNindicatorOn, False,
239#endif
240 XmNfillOnSelect, False,
241 XmNshadowThickness, 2,
242 XmNalignment, XmALIGNMENT_CENTER,
243 XmNmarginLeft, 0,
244 XmNmarginRight, 0,
245 NULL );
246
247 // set it again, because the XtVaSetValue above resets it
248 if( size.x != -1 || size.y != -1 )
249 SetSize( size );
250
251 return true;
252}
253
254#endif // wxUSE_TOGGLEBUTTON