1 ///////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "checklst.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/checklst.h"
26 #include "wx/arrstr.h"
28 // ============================================================================
30 // ============================================================================
32 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
34 // ----------------------------------------------------------------------------
35 // implementation of wxCheckListBox class
36 // ----------------------------------------------------------------------------
40 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
46 static const wxString prefixChecked
= "[x] ";
47 static const wxString prefixUnchecked
= "[ ] ";
48 static const char checkChar
= 'x', uncheckChar
= ' ';
50 static inline const wxString
& Prefix(bool checked
)
51 { return checked
? prefixChecked
: prefixUnchecked
; }
52 static inline bool IsChecked(const wxString
& s
)
53 { wxASSERT(s
.length() >=4); return s
[1] == checkChar
; }
55 static void CopyStringsAddingPrefix(const wxArrayString
& orig
,
60 for(size_t i
= 0; i
< orig
.GetCount(); ++i
)
61 copy
.Add( Prefix(FALSE
) + orig
[i
] );
64 // def ctor: use Create() to really create the control
65 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
69 // ctor which creates the associated control
70 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
71 const wxPoint
& pos
, const wxSize
& size
,
72 int nStrings
, const wxString choices
[],
73 long style
, const wxValidator
& val
,
75 : wxCheckListBoxBase()
77 Create(parent
, id
, pos
, size
, nStrings
, choices
,
81 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
82 const wxPoint
& pos
, const wxSize
& size
,
83 const wxArrayString
& choices
,
84 long style
, const wxValidator
& val
,
86 : wxCheckListBoxBase()
88 Create(parent
, id
, pos
, size
, choices
,
92 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
95 int n
, const wxString choices
[],
97 const wxValidator
& validator
,
100 // wxListBox::Create calls set, which adds the prefixes
101 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
102 style
, validator
, name
);
106 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
109 const wxArrayString
& choices
,
111 const wxValidator
& validator
,
112 const wxString
& name
)
114 // wxListBox::Create calls set, which adds the prefixes
115 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, choices
,
116 style
, validator
, name
);
123 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
125 return ::IsChecked(wxListBox::GetString(uiIndex
));
128 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
130 wxString label
= wxListBox::GetString(uiIndex
);
131 if(::IsChecked(label
) == bCheck
) return;
132 label
[1u] = bCheck
? checkChar
: uncheckChar
;
133 wxListBox::SetString(uiIndex
, label
);
136 void wxCheckListBox::DoToggleItem( int n
, int x
)
140 wxString label
= wxListBox::GetString(n
);
141 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
142 wxListBox::SetString(n
, label
);
144 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
145 if( HasClientObjectData() )
146 event
.SetClientObject( GetClientObject(n
) );
147 else if( HasClientUntypedData() )
148 event
.SetClientData( GetClientData(n
) );
149 event
.m_commandInt
= n
;
150 event
.m_extraLong
= TRUE
;
151 event
.SetEventObject(this);
152 event
.SetString( GetString( n
) );
154 GetEventHandler()->ProcessEvent(event
);
158 int wxCheckListBox::DoAppend(const wxString
& item
)
160 return wxListBox::DoAppend( Prefix(FALSE
) + item
);
163 int wxCheckListBox::FindString(const wxString
& s
) const
165 int n1
= wxListBox::FindString(Prefix(FALSE
) + s
);
166 int n2
= wxListBox::FindString(Prefix(TRUE
) + s
);
167 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
170 // n1 == -1, n2 == -1 => return -1 OK
171 // n1 != -1 || n2 != -1 => min == -1 => return the other one
172 // both != -1 => return the first one.
173 if( min
== -1 ) return max
;
177 void wxCheckListBox::SetString(int n
, const wxString
& s
)
179 wxListBox::SetString( n
, Prefix(IsChecked(n
)) + s
);
182 wxString
wxCheckListBox::GetString(int n
) const
184 return wxListBox::GetString(n
).substr(4);
187 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
190 CopyStringsAddingPrefix(items
, copy
);
191 wxListBox::DoInsertItems(copy
, pos
);
194 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
197 CopyStringsAddingPrefix(items
, copy
);
198 wxListBox::DoSetItems(copy
, clientData
);