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
23 #include "wx/checklst.h"
24 #include "wx/arrstr.h"
26 // ============================================================================
28 // ============================================================================
30 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
32 // ----------------------------------------------------------------------------
33 // implementation of wxCheckListBox class
34 // ----------------------------------------------------------------------------
38 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
44 static const wxString prefixChecked
= "[x] ";
45 static const wxString prefixUnchecked
= "[ ] ";
46 static const char checkChar
= 'x', uncheckChar
= ' ';
48 static inline const wxString
& Prefix(bool checked
)
49 { return checked
? prefixChecked
: prefixUnchecked
; }
50 static inline bool IsChecked(const wxString
& s
)
51 { wxASSERT(s
.length() >=4); return s
[1] == checkChar
; }
53 static void CopyStringsAddingPrefix(const wxArrayString
& orig
,
58 for(size_t i
= 0; i
< orig
.GetCount(); ++i
)
59 copy
.Add( Prefix(false) + orig
[i
] );
62 // def ctor: use Create() to really create the control
63 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
67 // ctor which creates the associated control
68 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
69 const wxPoint
& pos
, const wxSize
& size
,
70 int nStrings
, const wxString choices
[],
71 long style
, const wxValidator
& val
,
73 : wxCheckListBoxBase()
75 Create(parent
, id
, pos
, size
, nStrings
, choices
,
79 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
80 const wxPoint
& pos
, const wxSize
& size
,
81 const wxArrayString
& choices
,
82 long style
, const wxValidator
& val
,
84 : wxCheckListBoxBase()
86 Create(parent
, id
, pos
, size
, choices
,
90 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
93 int n
, const wxString choices
[],
95 const wxValidator
& validator
,
98 // wxListBox::Create calls set, which adds the prefixes
99 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
100 style
, validator
, name
);
104 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
107 const wxArrayString
& choices
,
109 const wxValidator
& validator
,
110 const wxString
& name
)
112 // wxListBox::Create calls set, which adds the prefixes
113 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, choices
,
114 style
, validator
, name
);
121 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
123 return ::IsChecked(wxListBox::GetString(uiIndex
));
126 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
128 wxString label
= wxListBox::GetString(uiIndex
);
129 if(::IsChecked(label
) == bCheck
) return;
130 label
[1u] = bCheck
? checkChar
: uncheckChar
;
131 wxListBox::SetString(uiIndex
, label
);
134 void wxCheckListBox::DoToggleItem( int n
, int x
)
138 wxString label
= wxListBox::GetString(n
);
139 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
140 wxListBox::SetString(n
, label
);
142 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
143 if( HasClientObjectData() )
144 event
.SetClientObject( GetClientObject(n
) );
145 else if( HasClientUntypedData() )
146 event
.SetClientData( GetClientData(n
) );
148 event
.SetExtraLong(true);
149 event
.SetEventObject(this);
150 event
.SetString(GetString(n
));
152 GetEventHandler()->ProcessEvent(event
);
156 int wxCheckListBox::DoAppend(const wxString
& item
)
158 return wxListBox::DoAppend( Prefix(false) + item
);
161 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const
163 int n1
= wxListBox::FindString(Prefix(false) + s
, bCase
);
164 int n2
= wxListBox::FindString(Prefix(true) + s
, bCase
);
165 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
168 // n1 == -1, n2 == -1 => return -1 OK
169 // n1 != -1 || n2 != -1 => min == -1 => return the other one
170 // both != -1 => return the first one.
171 if( min
== -1 ) return max
;
175 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
)
177 wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
);
180 wxString
wxCheckListBox::GetString(unsigned int n
) const
182 return wxListBox::GetString(n
).substr(4);
185 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
188 CopyStringsAddingPrefix(items
, copy
);
189 wxListBox::DoInsertItems(copy
, pos
);
192 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
195 CopyStringsAddingPrefix(items
, copy
);
196 wxListBox::DoSetItems(copy
, clientData
);
199 #endif // wxUSE_CHECKLISTBOX