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 // ============================================================================
17 #pragma implementation "checklst.h"
22 #include "wx/checklst.h"
24 // ============================================================================
26 // ============================================================================
28 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
30 // ----------------------------------------------------------------------------
31 // implementation of wxCheckListBox class
32 // ----------------------------------------------------------------------------
36 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
42 static const wxString prefixChecked
= "[x] ";
43 static const wxString prefixUnchecked
= "[ ] ";
44 static const char checkChar
= 'x', uncheckChar
= ' ';
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
; }
51 static void CopyStringsAddingPrefix(const wxArrayString
& orig
,
56 for(size_t i
= 0; i
< orig
.GetCount(); ++i
)
57 copy
.Add( Prefix(FALSE
) + orig
[i
] );
60 // def ctor: use Create() to really create the control
61 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
65 // ctor which creates the associated control
66 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
67 const wxPoint
& pos
, const wxSize
& size
,
68 int nStrings
, const wxString choices
[],
69 long style
, const wxValidator
& val
,
71 : wxCheckListBoxBase()
73 Create(parent
, id
, pos
, size
, nStrings
, choices
,
77 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
80 int n
, const wxString choices
[],
82 const wxValidator
& validator
,
85 // wxListBox::Create calls set, which adds the prefixes
86 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
87 style
, validator
, name
);
94 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
96 return ::IsChecked(wxListBox::GetString(uiIndex
));
99 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
101 wxString label
= wxListBox::GetString(uiIndex
);
102 if(::IsChecked(label
) == bCheck
) return;
103 label
[1u] = bCheck
? checkChar
: uncheckChar
;
104 wxListBox::SetString(uiIndex
, label
);
107 void wxCheckListBox::DoToggleItem( int n
, int x
)
111 wxString label
= wxListBox::GetString(n
);
112 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
113 wxListBox::SetString(n
, label
);
115 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
116 if( HasClientObjectData() )
117 event
.SetClientObject( GetClientObject(n
) );
118 else if( HasClientUntypedData() )
119 event
.SetClientData( GetClientData(n
) );
120 event
.m_commandInt
= n
;
121 event
.m_extraLong
= TRUE
;
122 event
.SetEventObject(this);
123 event
.SetString( GetString( n
) );
125 GetEventHandler()->ProcessEvent(event
);
129 int wxCheckListBox::DoAppend(const wxString
& item
)
131 return wxListBox::DoAppend( Prefix(FALSE
) + item
);
134 int wxCheckListBox::FindString(const wxString
& s
) const
136 int n1
= wxListBox::FindString(Prefix(FALSE
) + s
);
137 int n2
= wxListBox::FindString(Prefix(TRUE
) + s
);
138 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
141 // n1 == -1, n2 == -1 => return -1 OK
142 // n1 != -1 || n2 != -1 => min == -1 => return the other one
143 // both != -1 => return the first one.
144 if( min
== -1 ) return max
;
148 void wxCheckListBox::SetString(int n
, const wxString
& s
)
150 wxListBox::SetString( n
, Prefix(IsChecked(n
)) + s
);
153 wxString
wxCheckListBox::GetString(int n
) const
155 return wxListBox::GetString(n
).substr(4);
158 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
161 CopyStringsAddingPrefix(items
, copy
);
162 wxListBox::DoInsertItems(copy
, pos
);
165 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
168 CopyStringsAddingPrefix(items
, copy
);
169 wxListBox::DoSetItems(copy
, clientData
);