Document domain parameter of wxTranslations::GetTranslatedString().
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // headers & declarations
13 // ============================================================================
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #if wxUSE_CHECKLISTBOX
19
20 #include "wx/checklst.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/arrstr.h"
24 #endif
25
26 // ============================================================================
27 // implementation
28 // ============================================================================
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
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 // def ctor: use Create() to really create the control
52 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
53 {
54 }
55
56 // ctor which creates the associated control
57 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
58 const wxPoint& pos, const wxSize& size,
59 int nStrings, const wxString choices[],
60 long style, const wxValidator& val,
61 const wxString& name)
62 : wxCheckListBoxBase()
63 {
64 Create(parent, id, pos, size, nStrings, choices,
65 style, val, name);
66 }
67
68 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
69 const wxPoint& pos, const wxSize& size,
70 const wxArrayString& choices,
71 long style, const wxValidator& val,
72 const wxString& name)
73 : wxCheckListBoxBase()
74 {
75 Create(parent, id, pos, size, choices,
76 style, val, name);
77 }
78
79 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
80 const wxPoint& pos,
81 const wxSize& size,
82 int n, const wxString choices[],
83 long style,
84 const wxValidator& validator,
85 const wxString& name)
86 {
87 // wxListBox::Create calls set, which adds the prefixes
88 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
89 style, validator, name);
90 return retVal;
91 }
92
93 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
94 const wxPoint& pos,
95 const wxSize& size,
96 const wxArrayString& choices,
97 long style,
98 const wxValidator& validator,
99 const wxString& name)
100 {
101 // wxListBox::Create calls set, which adds the prefixes
102 bool retVal = wxListBox::Create(parent, id, pos, size, choices,
103 style, validator, name);
104 return retVal;
105 }
106
107 // check items
108 // -----------
109
110 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
111 {
112 return ::IsChecked(wxListBox::GetString(uiIndex));
113 }
114
115 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
116 {
117 wxString label = wxListBox::GetString(uiIndex);
118 if(::IsChecked(label) == bCheck) return;
119 label[1u] = bCheck ? checkChar : uncheckChar;
120 wxListBox::SetString(uiIndex, label);
121 }
122
123 void wxCheckListBox::DoToggleItem( int n, int x )
124 {
125 if( x > 0 && x < 23 )
126 {
127 wxString label = wxListBox::GetString(n);
128 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
129 wxListBox::SetString(n, label);
130
131 wxCommandEvent event(wxEVT_CHECKLISTBOX, GetId());
132 if( HasClientObjectData() )
133 event.SetClientObject( GetClientObject(n) );
134 else if( HasClientUntypedData() )
135 event.SetClientData( GetClientData(n) );
136 event.SetInt(n);
137 event.SetExtraLong(true);
138 event.SetEventObject(this);
139 event.SetString(GetString(n));
140
141 HandleWindowEvent(event);
142 }
143 }
144
145 int wxCheckListBox::FindString(const wxString& s, bool bCase) const
146 {
147 int n1 = wxListBox::FindString(Prefix(false) + s, bCase);
148 int n2 = wxListBox::FindString(Prefix(true) + s, bCase);
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(unsigned int n, const wxString& s)
160 {
161 wxListBox::SetString(n, Prefix(IsChecked(n)) + s);
162 }
163
164 wxString wxCheckListBox::GetString(unsigned int n) const
165 {
166 return wxListBox::GetString(n).substr(4);
167 }
168
169 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter& items,
170 unsigned int pos,
171 void **clientData, wxClientDataType type)
172 {
173 wxArrayString copy;
174 copy.reserve(pos);
175 for ( size_t i = 0; i < items.GetCount(); ++i )
176 copy.push_back( Prefix(false) + items[i] );
177
178 return wxListBox::DoInsertItems(copy, pos, clientData, type);
179 }
180
181 #endif // wxUSE_CHECKLISTBOX