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