Replaced TRUE and FALSE with true and false
[wxWidgets.git] / src / motif / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
15
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "checklst.h"
18 #endif
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/defs.h"
24
25 #include "wx/checklst.h"
26 #include "wx/arrstr.h"
27
28 // ============================================================================
29 // implementation
30 // ============================================================================
31
32 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
33
34 // ----------------------------------------------------------------------------
35 // implementation of wxCheckListBox class
36 // ----------------------------------------------------------------------------
37
38 // define event table
39 // ------------------
40 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
41 END_EVENT_TABLE()
42
43 // control creation
44 // ----------------
45
46 static const wxString prefixChecked = "[x] ";
47 static const wxString prefixUnchecked = "[ ] ";
48 static const char checkChar = 'x', uncheckChar = ' ';
49
50 static inline const wxString& Prefix(bool checked)
51 { return checked ? prefixChecked : prefixUnchecked; }
52 static inline bool IsChecked(const wxString& s)
53 { wxASSERT(s.length() >=4); return s[1] == checkChar; }
54
55 static void CopyStringsAddingPrefix(const wxArrayString& orig,
56 wxArrayString& copy)
57 {
58 copy.Clear();
59
60 for(size_t i = 0; i < orig.GetCount(); ++i )
61 copy.Add( Prefix(false) + orig[i] );
62 }
63
64 // def ctor: use Create() to really create the control
65 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
66 {
67 }
68
69 // ctor which creates the associated control
70 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
71 const wxPoint& pos, const wxSize& size,
72 int nStrings, const wxString choices[],
73 long style, const wxValidator& val,
74 const wxString& name)
75 : wxCheckListBoxBase()
76 {
77 Create(parent, id, pos, size, nStrings, choices,
78 style, val, name);
79 }
80
81 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
82 const wxPoint& pos, const wxSize& size,
83 const wxArrayString& choices,
84 long style, const wxValidator& val,
85 const wxString& name)
86 : wxCheckListBoxBase()
87 {
88 Create(parent, id, pos, size, choices,
89 style, val, name);
90 }
91
92 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
93 const wxPoint& pos,
94 const wxSize& size,
95 int n, const wxString choices[],
96 long style,
97 const wxValidator& validator,
98 const wxString& name)
99 {
100 // wxListBox::Create calls set, which adds the prefixes
101 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
102 style, validator, name);
103 return retVal;
104 }
105
106 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
107 const wxPoint& pos,
108 const wxSize& size,
109 const wxArrayString& choices,
110 long style,
111 const wxValidator& validator,
112 const wxString& name)
113 {
114 // wxListBox::Create calls set, which adds the prefixes
115 bool retVal = wxListBox::Create(parent, id, pos, size, choices,
116 style, validator, name);
117 return retVal;
118 }
119
120 // check items
121 // -----------
122
123 bool wxCheckListBox::IsChecked(size_t uiIndex) const
124 {
125 return ::IsChecked(wxListBox::GetString(uiIndex));
126 }
127
128 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
129 {
130 wxString label = wxListBox::GetString(uiIndex);
131 if(::IsChecked(label) == bCheck) return;
132 label[1u] = bCheck ? checkChar : uncheckChar;
133 wxListBox::SetString(uiIndex, label);
134 }
135
136 void wxCheckListBox::DoToggleItem( int n, int x )
137 {
138 if( x < 23 )
139 {
140 wxString label = wxListBox::GetString(n);
141 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
142 wxListBox::SetString(n, label);
143
144 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
145 if( HasClientObjectData() )
146 event.SetClientObject( GetClientObject(n) );
147 else if( HasClientUntypedData() )
148 event.SetClientData( GetClientData(n) );
149 event.SetInt(n);
150 event.SetExtraLong(true);
151 event.SetEventObject(this);
152 event.SetString( GetString( n ) );
153
154 GetEventHandler()->ProcessEvent(event);
155 }
156 }
157
158 int wxCheckListBox::DoAppend(const wxString& item)
159 {
160 return wxListBox::DoAppend( Prefix(false) + item );
161 }
162
163 int wxCheckListBox::FindString(const wxString& s) const
164 {
165 int n1 = wxListBox::FindString(Prefix(false) + s);
166 int n2 = wxListBox::FindString(Prefix(true) + s);
167 int min = wxMin(n1, n2), max = wxMax(n1, n2);
168
169 // why this works:
170 // n1 == -1, n2 == -1 => return -1 OK
171 // n1 != -1 || n2 != -1 => min == -1 => return the other one
172 // both != -1 => return the first one.
173 if( min == -1 ) return max;
174 return min;
175 }
176
177 void wxCheckListBox::SetString(int n, const wxString& s)
178 {
179 wxListBox::SetString( n, Prefix(IsChecked(n)) + s );
180 }
181
182 wxString wxCheckListBox::GetString(int n) const
183 {
184 return wxListBox::GetString(n).substr(4);
185 }
186
187 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
188 {
189 wxArrayString copy;
190 CopyStringsAddingPrefix(items, copy);
191 wxListBox::DoInsertItems(copy, pos);
192 }
193
194 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
195 {
196 wxArrayString copy;
197 CopyStringsAddingPrefix(items, copy);
198 wxListBox::DoSetItems(copy, clientData);
199 }