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"
22 #include "wx/checklst.h"
23 #include "wx/arrstr.h"
25 // ============================================================================
27 // ============================================================================
29 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
31 // ----------------------------------------------------------------------------
32 // implementation of wxCheckListBox class
33 // ----------------------------------------------------------------------------
37 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
43 static const wxString prefixChecked
= "[x] ";
44 static const wxString prefixUnchecked
= "[ ] ";
45 static const char checkChar
= 'x', uncheckChar
= ' ';
47 static inline const wxString
& Prefix(bool checked
)
48 { return checked
? prefixChecked
: prefixUnchecked
; }
49 static inline bool IsChecked(const wxString
& s
)
50 { wxASSERT(s
.length() >=4); return s
[1] == checkChar
; }
52 static void CopyStringsAddingPrefix(const wxArrayString
& orig
,
57 for(size_t i
= 0; i
< orig
.GetCount(); ++i
)
58 copy
.Add( Prefix(FALSE
) + orig
[i
] );
61 // def ctor: use Create() to really create the control
62 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
66 // ctor which creates the associated control
67 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
68 const wxPoint
& pos
, const wxSize
& size
,
69 int nStrings
, const wxString choices
[],
70 long style
, const wxValidator
& val
,
72 : wxCheckListBoxBase()
74 Create(parent
, id
, pos
, size
, nStrings
, choices
,
78 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
81 int n
, const wxString choices
[],
83 const wxValidator
& validator
,
86 // wxListBox::Create calls set, which adds the prefixes
87 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
88 style
, validator
, name
);
95 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
97 return ::IsChecked(wxListBox::GetString(uiIndex
));
100 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
102 wxString label
= wxListBox::GetString(uiIndex
);
103 if(::IsChecked(label
) == bCheck
) return;
104 label
[1u] = bCheck
? checkChar
: uncheckChar
;
105 wxListBox::SetString(uiIndex
, label
);
108 void wxCheckListBox::DoToggleItem( int n
, int x
)
112 wxString label
= wxListBox::GetString(n
);
113 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
114 wxListBox::SetString(n
, label
);
116 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
117 if( HasClientObjectData() )
118 event
.SetClientObject( GetClientObject(n
) );
119 else if( HasClientUntypedData() )
120 event
.SetClientData( GetClientData(n
) );
121 event
.m_commandInt
= n
;
122 event
.m_extraLong
= TRUE
;
123 event
.SetEventObject(this);
124 event
.SetString( GetString( n
) );
126 GetEventHandler()->ProcessEvent(event
);
130 int wxCheckListBox::DoAppend(const wxString
& item
)
132 return wxListBox::DoAppend( Prefix(FALSE
) + item
);
135 int wxCheckListBox::FindString(const wxString
& s
) const
137 int n1
= wxListBox::FindString(Prefix(FALSE
) + s
);
138 int n2
= wxListBox::FindString(Prefix(TRUE
) + s
);
139 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
142 // n1 == -1, n2 == -1 => return -1 OK
143 // n1 != -1 || n2 != -1 => min == -1 => return the other one
144 // both != -1 => return the first one.
145 if( min
== -1 ) return max
;
149 void wxCheckListBox::SetString(int n
, const wxString
& s
)
151 wxListBox::SetString( n
, Prefix(IsChecked(n
)) + s
);
154 wxString
wxCheckListBox::GetString(int n
) const
156 return wxListBox::GetString(n
).substr(4);
159 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
162 CopyStringsAddingPrefix(items
, copy
);
163 wxListBox::DoInsertItems(copy
, pos
);
166 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
169 CopyStringsAddingPrefix(items
, copy
);
170 wxListBox::DoSetItems(copy
, clientData
);