Fix crash when getting or setting wxComboBox value in wxUniv.
[wxWidgets.git] / src / univ / checklst.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/checklst.cpp
3 // Purpose: wxCheckListBox implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_CHECKLISTBOX
27
28 #include "wx/checklst.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/validate.h"
34 #endif
35
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
39
40 // ----------------------------------------------------------------------------
41 // wxStdCheckListBoxInputHandler
42 // ----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxStdCheckListboxInputHandler : public wxStdInputHandler
45 {
46 public:
47 wxStdCheckListboxInputHandler(wxInputHandler *inphand);
48
49 virtual bool HandleKey(wxInputConsumer *consumer,
50 const wxKeyEvent& event,
51 bool pressed);
52 virtual bool HandleMouse(wxInputConsumer *consumer,
53 const wxMouseEvent& event);
54 };
55
56 // ============================================================================
57 // implementation of wxCheckListBox
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // creation
62 // ----------------------------------------------------------------------------
63
64 void wxCheckListBox::Init()
65 {
66 }
67
68 wxCheckListBox::wxCheckListBox(wxWindow *parent,
69 wxWindowID id,
70 const wxPoint &pos,
71 const wxSize &size,
72 const wxArrayString& choices,
73 long style,
74 const wxValidator& validator,
75 const wxString &name)
76 {
77 Init();
78
79 Create(parent, id, pos, size, choices, style, validator, name);
80 }
81
82 bool wxCheckListBox::Create(wxWindow *parent,
83 wxWindowID id,
84 const wxPoint &pos,
85 const wxSize &size,
86 const wxArrayString& choices,
87 long style,
88 const wxValidator& validator,
89 const wxString &name)
90 {
91 wxCArrayString chs(choices);
92
93 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
94 style, validator, name);
95 }
96
97 bool wxCheckListBox::Create(wxWindow *parent,
98 wxWindowID id,
99 const wxPoint &pos,
100 const wxSize &size,
101 int n,
102 const wxString choices[],
103 long style,
104 const wxValidator& validator,
105 const wxString &name)
106 {
107 if ( !wxListBox::Create(parent, id, pos, size,
108 n, choices, style, validator, name) )
109 return false;
110
111 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX);
112
113 return true;
114 }
115
116 // ----------------------------------------------------------------------------
117 // wxCheckListBox functions
118 // ----------------------------------------------------------------------------
119
120 bool wxCheckListBox::IsChecked(unsigned int item) const
121 {
122 wxCHECK_MSG( IsValid(item), false,
123 wxT("invalid index in wxCheckListBox::IsChecked") );
124
125 return m_checks[item] != 0;
126 }
127
128 void wxCheckListBox::Check(unsigned int item, bool check)
129 {
130 wxCHECK_RET( IsValid(item),
131 wxT("invalid index in wxCheckListBox::Check") );
132
133 // intermediate var is needed to avoid compiler warning with VC++
134 bool isChecked = m_checks[item] != 0;
135 if ( check != isChecked )
136 {
137 m_checks[item] = check;
138
139 RefreshItem(item);
140 }
141 }
142
143 // ----------------------------------------------------------------------------
144 // methods forwarded to wxListBox
145 // ----------------------------------------------------------------------------
146
147 void wxCheckListBox::DoDeleteOneItem(unsigned int n)
148 {
149 wxListBox::DoDeleteOneItem(n);
150
151 m_checks.RemoveAt(n);
152 }
153
154 void wxCheckListBox::OnItemInserted(unsigned int pos)
155 {
156 m_checks.Insert(false, pos);
157 }
158
159 void wxCheckListBox::DoClear()
160 {
161 m_checks.Empty();
162 }
163
164 // ----------------------------------------------------------------------------
165 // drawing
166 // ----------------------------------------------------------------------------
167
168 wxSize wxCheckListBox::DoGetBestClientSize() const
169 {
170 wxSize size = wxListBox::DoGetBestClientSize();
171 size.x += GetRenderer()->GetCheckBitmapSize().x;
172
173 return size;
174 }
175
176 void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer,
177 int itemFirst, int itemLast)
178 {
179 renderer->DrawCheckItems(this, itemFirst, itemLast);
180 }
181
182 // ----------------------------------------------------------------------------
183 // actions
184 // ----------------------------------------------------------------------------
185
186 bool wxCheckListBox::PerformAction(const wxControlAction& action,
187 long numArg,
188 const wxString& strArg)
189 {
190 if ( action == wxACTION_CHECKLISTBOX_TOGGLE )
191 {
192 int sel = (int)numArg;
193 if ( sel == -1 )
194 {
195 sel = GetSelection();
196 }
197
198 if ( sel != -1 )
199 {
200 Check(sel, !IsChecked(sel));
201
202 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, sel);
203 }
204 }
205 else
206 {
207 return wxListBox::PerformAction(action, numArg, strArg);
208 }
209
210 return true;
211 }
212
213 /* static */
214 wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef)
215 {
216 static wxStdCheckListboxInputHandler s_handler(handlerDef);
217
218 return &s_handler;
219 }
220
221 // ----------------------------------------------------------------------------
222 // wxStdCheckListboxInputHandler
223 // ----------------------------------------------------------------------------
224
225 wxStdCheckListboxInputHandler::
226 wxStdCheckListboxInputHandler(wxInputHandler *inphand)
227 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand))
228 {
229 }
230
231 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer,
232 const wxKeyEvent& event,
233 bool pressed)
234 {
235 if ( pressed && (event.GetKeyCode() == WXK_SPACE) )
236 consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE);
237
238 return wxStdInputHandler::HandleKey(consumer, event, pressed);
239 }
240
241 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer,
242 const wxMouseEvent& event)
243 {
244 if ( event.LeftDown() || event.LeftDClick() )
245 {
246 wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox);
247 int x, y;
248
249 wxPoint pt = event.GetPosition();
250 pt -= consumer->GetInputWindow()->GetClientAreaOrigin();
251 lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y);
252
253 wxRenderer *renderer = lbox->GetRenderer();
254 x -= renderer->GetCheckItemMargin();
255
256 int item = y / lbox->GetLineHeight();
257 if ( x >= 0 &&
258 x < renderer->GetCheckBitmapSize().x &&
259 item >= 0 &&
260 (unsigned int)item < lbox->GetCount() )
261 {
262 lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item);
263
264 return true;
265 }
266 }
267
268 return wxStdInputHandler::HandleMouse(consumer, event);
269 }
270
271 #endif // wxUSE_CHECKLISTBOX