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