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