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 // ----------------------------------------------------------------------------
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 // def ctor: use Create() to really create the control
53 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
57 // ctor which creates the associated control
58 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int nStrings
, const wxString choices
[],
61 long style
, const wxValidator
& val
,
63 : wxCheckListBoxBase()
65 Create(parent
, id
, pos
, size
, nStrings
, choices
,
69 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
70 const wxPoint
& pos
, const wxSize
& size
,
71 const wxArrayString
& choices
,
72 long style
, const wxValidator
& val
,
74 : wxCheckListBoxBase()
76 Create(parent
, id
, pos
, size
, choices
,
80 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
83 int n
, const wxString choices
[],
85 const wxValidator
& validator
,
88 // wxListBox::Create calls set, which adds the prefixes
89 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
90 style
, validator
, name
);
94 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
97 const wxArrayString
& choices
,
99 const wxValidator
& validator
,
100 const wxString
& name
)
102 // wxListBox::Create calls set, which adds the prefixes
103 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, choices
,
104 style
, validator
, name
);
111 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
113 return ::IsChecked(wxListBox::GetString(uiIndex
));
116 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
118 wxString label
= wxListBox::GetString(uiIndex
);
119 if(::IsChecked(label
) == bCheck
) return;
120 label
[1u] = bCheck
? checkChar
: uncheckChar
;
121 wxListBox::SetString(uiIndex
, label
);
124 void wxCheckListBox::DoToggleItem( int n
, int x
)
126 if( x
> 0 && x
< 23 )
128 wxString label
= wxListBox::GetString(n
);
129 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
130 wxListBox::SetString(n
, label
);
132 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
133 if( HasClientObjectData() )
134 event
.SetClientObject( GetClientObject(n
) );
135 else if( HasClientUntypedData() )
136 event
.SetClientData( GetClientData(n
) );
138 event
.SetExtraLong(true);
139 event
.SetEventObject(this);
140 event
.SetString(GetString(n
));
142 HandleWindowEvent(event
);
146 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const
148 int n1
= wxListBox::FindString(Prefix(false) + s
, bCase
);
149 int n2
= wxListBox::FindString(Prefix(true) + s
, bCase
);
150 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
153 // n1 == -1, n2 == -1 => return -1 OK
154 // n1 != -1 || n2 != -1 => min == -1 => return the other one
155 // both != -1 => return the first one.
156 if( min
== -1 ) return max
;
160 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
)
162 wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
);
165 wxString
wxCheckListBox::GetString(unsigned int n
) const
167 return wxListBox::GetString(n
).substr(4);
170 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
172 void **clientData
, wxClientDataType type
)
176 for ( size_t i
= 0; i
< items
.GetCount(); ++i
)
177 copy
.push_back( Prefix(false) + items
[i
] );
179 return wxListBox::DoInsertItems(copy
, pos
, clientData
, type
);
182 #endif // wxUSE_CHECKLISTBOX