]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
12 // headers & declarations
13 // ============================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #if wxUSE_CHECKLISTBOX
20 #include "wx/checklst.h"
23 #include "wx/arrstr.h"
26 // ============================================================================
28 // ============================================================================
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 // def ctor: use Create() to really create the control
52 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
56 // ctor which creates the associated control
57 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
58 const wxPoint
& pos
, const wxSize
& size
,
59 int nStrings
, const wxString choices
[],
60 long style
, const wxValidator
& val
,
62 : wxCheckListBoxBase()
64 Create(parent
, id
, pos
, size
, nStrings
, choices
,
68 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
69 const wxPoint
& pos
, const wxSize
& size
,
70 const wxArrayString
& choices
,
71 long style
, const wxValidator
& val
,
73 : wxCheckListBoxBase()
75 Create(parent
, id
, pos
, size
, choices
,
79 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
82 int n
, const wxString choices
[],
84 const wxValidator
& validator
,
87 // wxListBox::Create calls set, which adds the prefixes
88 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
89 style
, validator
, name
);
93 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
96 const wxArrayString
& choices
,
98 const wxValidator
& validator
,
101 // wxListBox::Create calls set, which adds the prefixes
102 bool retVal
= wxListBox::Create(parent
, id
, pos
, size
, choices
,
103 style
, validator
, name
);
110 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
112 return ::IsChecked(wxListBox::GetString(uiIndex
));
115 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
117 wxString label
= wxListBox::GetString(uiIndex
);
118 if(::IsChecked(label
) == bCheck
) return;
119 label
[1u] = bCheck
? checkChar
: uncheckChar
;
120 wxListBox::SetString(uiIndex
, label
);
123 void wxCheckListBox::DoToggleItem( int n
, int x
)
125 if( x
> 0 && x
< 23 )
127 wxString label
= wxListBox::GetString(n
);
128 label
[1u] = (!::IsChecked(label
)) ? checkChar
: uncheckChar
;
129 wxListBox::SetString(n
, label
);
131 wxCommandEvent
event(wxEVT_CHECKLISTBOX
, GetId());
132 if( HasClientObjectData() )
133 event
.SetClientObject( GetClientObject(n
) );
134 else if( HasClientUntypedData() )
135 event
.SetClientData( GetClientData(n
) );
137 event
.SetExtraLong(true);
138 event
.SetEventObject(this);
139 event
.SetString(GetString(n
));
141 HandleWindowEvent(event
);
145 int wxCheckListBox::FindString(const wxString
& s
, bool bCase
) const
147 int n1
= wxListBox::FindString(Prefix(false) + s
, bCase
);
148 int n2
= wxListBox::FindString(Prefix(true) + s
, bCase
);
149 int min
= wxMin(n1
, n2
), max
= wxMax(n1
, n2
);
152 // n1 == -1, n2 == -1 => return -1 OK
153 // n1 != -1 || n2 != -1 => min == -1 => return the other one
154 // both != -1 => return the first one.
155 if( min
== -1 ) return max
;
159 void wxCheckListBox::SetString(unsigned int n
, const wxString
& s
)
161 wxListBox::SetString(n
, Prefix(IsChecked(n
)) + s
);
164 wxString
wxCheckListBox::GetString(unsigned int n
) const
166 return wxListBox::GetString(n
).substr(4);
169 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
171 void **clientData
, wxClientDataType type
)
175 for ( size_t i
= 0; i
< items
.GetCount(); ++i
)
176 copy
.push_back( Prefix(false) + items
[i
] );
178 return wxListBox::DoInsertItems(copy
, pos
, clientData
, type
);
181 #endif // wxUSE_CHECKLISTBOX