1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if wxUSE_CHECKLISTBOX
21 #include "wx/checklst.h"
24 #include "wx/arrstr.h"
27 // ============================================================================
29 // ============================================================================
31 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
33 // ----------------------------------------------------------------------------
34 // implementation of wxCheckListBox class
35 // ----------------------------------------------------------------------------
39 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
45 static const wxString prefixChecked
= "[x] ";
46 static const wxString prefixUnchecked
= "[ ] ";
47 static const char checkChar
= 'x', uncheckChar
= ' ';
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
; }
54 static void CopyStringsAddingPrefix(const wxArrayString
& orig
,
59 for(size_t i
= 0; i
< orig
.GetCount(); ++i
)
60 copy
.Add( Prefix(false) + orig
[i
] );
63 // def ctor: use Create() to really create the control
64 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
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
,
74 : wxCheckListBoxBase()
76 Create(parent
, id
, pos
, size
, nStrings
, choices
,
80 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
81 const wxPoint
& pos
, const wxSize
& size
,
82 const wxArrayString
& choices
,
83 long style
, const wxValidator
& val
,
85 : wxCheckListBoxBase()
87 Create(parent
, id
, pos
, size
, choices
,
91 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
94 int n
, const wxString choices
[],
96 const wxValidator
& validator
,
99 // wxListBox::Create calls set, which adds the prefixes
100 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
101 style
, validator
, name
);
105 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
108 const wxArrayString
& choices
,
110 const wxValidator
& validator
,
111 const wxString
& name
)
113 // wxListBox::Create calls set, which adds the prefixes
114 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, choices
,
115 style
, validator
, name
);
122 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
124 return ::IsChecked(wxListBox::GetString(uiIndex
));
127 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
129 wxString label
= wxListBox::GetString(uiIndex
);
130 if(::IsChecked(label
) == bCheck
) return;
131 label
[1u] = bCheck
? checkChar
: uncheckChar
;
132 wxListBox::SetString(uiIndex
, label
);
135 void wxCheckListBox::DoToggleItem( int n
, int x
)
139 wxString label
= wxListBox::GetString(n
);
140 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
141 wxListBox::SetString(n
, label
);
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
) );
149 event
.SetExtraLong(true);
150 event
.SetEventObject(this);
151 event
.SetString(GetString(n
));
153 GetEventHandler()->ProcessEvent(event
);
157 int wxCheckListBox::DoAppend(const wxString
& item
)
159 return wxListBox::DoAppend( Prefix(false) + item
);
162 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const
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
);
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
;
176 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
)
178 wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
);
181 wxString
wxCheckListBox::GetString(unsigned int n
) const
183 return wxListBox::GetString(n
).substr(4);
186 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
189 CopyStringsAddingPrefix(items
, copy
);
190 wxListBox::DoInsertItems(copy
, pos
);
193 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
196 CopyStringsAddingPrefix(items
, copy
);
197 wxListBox::DoSetItems(copy
, clientData
);
200 #endif // wxUSE_CHECKLISTBOX