]> git.saurik.com Git - wxWidgets.git/blob - src/motif/checklst.cpp
Fixed centering/right alignment of last line
[wxWidgets.git] / src / motif / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_CHECKLISTBOX
20
21 #include "wx/checklst.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/arrstr.h"
25 #endif
26
27 // ============================================================================
28 // implementation
29 // ============================================================================
30
31 // ----------------------------------------------------------------------------
32 // implementation of wxCheckListBox class
33 // ----------------------------------------------------------------------------
34
35 // define event table
36 // ------------------
37 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
38 END_EVENT_TABLE()
39
40 // control creation
41 // ----------------
42
43 static const wxString prefixChecked = "[x] ";
44 static const wxString prefixUnchecked = "[ ] ";
45 static const char checkChar = 'x', uncheckChar = ' ';
46
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; }
51
52 // def ctor: use Create() to really create the control
53 wxCheckListBox::wxCheckListBox() : wxCheckListBoxBase()
54 {
55 }
56
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,
62 const wxString& name)
63 : wxCheckListBoxBase()
64 {
65 Create(parent, id, pos, size, nStrings, choices,
66 style, val, name);
67 }
68
69 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
70 const wxPoint& pos, const wxSize& size,
71 const wxArrayString& choices,
72 long style, const wxValidator& val,
73 const wxString& name)
74 : wxCheckListBoxBase()
75 {
76 Create(parent, id, pos, size, choices,
77 style, val, name);
78 }
79
80 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
81 const wxPoint& pos,
82 const wxSize& size,
83 int n, const wxString choices[],
84 long style,
85 const wxValidator& validator,
86 const wxString& name)
87 {
88 // wxListBox::Create calls set, which adds the prefixes
89 bool retVal = wxListBox::Create(parent, id, pos, size, n, choices,
90 style, validator, name);
91 return retVal;
92 }
93
94 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
95 const wxPoint& pos,
96 const wxSize& size,
97 const wxArrayString& choices,
98 long style,
99 const wxValidator& validator,
100 const wxString& name)
101 {
102 // wxListBox::Create calls set, which adds the prefixes
103 bool retVal = wxListBox::Create(parent, id, pos, size, choices,
104 style, validator, name);
105 return retVal;
106 }
107
108 // check items
109 // -----------
110
111 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
112 {
113 return ::IsChecked(wxListBox::GetString(uiIndex));
114 }
115
116 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
117 {
118 wxString label = wxListBox::GetString(uiIndex);
119 if(::IsChecked(label) == bCheck) return;
120 label[1u] = bCheck ? checkChar : uncheckChar;
121 wxListBox::SetString(uiIndex, label);
122 }
123
124 void wxCheckListBox::DoToggleItem( int n, int x )
125 {
126 if( x > 0 && x < 23 )
127 {
128 wxString label = wxListBox::GetString(n);
129 label[1u] = (!::IsChecked(label)) ? checkChar : uncheckChar;
130 wxListBox::SetString(n, label);
131
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) );
137 event.SetInt(n);
138 event.SetExtraLong(true);
139 event.SetEventObject(this);
140 event.SetString(GetString(n));
141
142 HandleWindowEvent(event);
143 }
144 }
145
146 int wxCheckListBox::FindString(const wxString& s, bool bCase) const
147 {
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);
151
152 // why this works:
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;
157 return min;
158 }
159
160 void wxCheckListBox::SetString(unsigned int n, const wxString& s)
161 {
162 wxListBox::SetString(n, Prefix(IsChecked(n)) + s);
163 }
164
165 wxString wxCheckListBox::GetString(unsigned int n) const
166 {
167 return wxListBox::GetString(n).substr(4);
168 }
169
170 int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter& items,
171 unsigned int pos,
172 void **clientData, wxClientDataType type)
173 {
174 wxArrayString copy;
175 copy.reserve(pos);
176 for ( size_t i = 0; i < items.GetCount(); ++i )
177 copy.push_back( Prefix(false) + items[i] );
178
179 return wxListBox::DoInsertItems(copy, pos, clientData, type);
180 }
181
182 #endif // wxUSE_CHECKLISTBOX