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