]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | /////////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/msw/listbox.cpp |
2bda0e17 KB |
3 | // Purpose: wxListBox |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin (owner drawn stuff) | |
dd3c394a | 6 | // Created: |
2bda0e17 KB |
7 | // RCS-ID: $Id$ |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
dd3c394a | 13 | #pragma implementation "listbox.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
dd3c394a | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
1e6feb95 | 23 | #if wxUSE_LISTBOX |
0c589ad0 | 24 | |
2bda0e17 | 25 | #ifndef WX_PRECOMP |
0c589ad0 BM |
26 | #include "wx/listbox.h" |
27 | #include "wx/settings.h" | |
28 | #include "wx/brush.h" | |
29 | #include "wx/font.h" | |
30 | #include "wx/dc.h" | |
2662e49e | 31 | #include "wx/utils.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
1e6feb95 VZ |
34 | #include "wx/window.h" |
35 | #include "wx/msw/private.h" | |
36 | ||
5ea105e0 RR |
37 | #include <windowsx.h> |
38 | ||
0c589ad0 BM |
39 | #include "wx/dynarray.h" |
40 | #include "wx/log.h" | |
2bda0e17 | 41 | |
0c589ad0 BM |
42 | #if wxUSE_OWNER_DRAWN |
43 | #include "wx/ownerdrw.h" | |
44 | #endif | |
2bda0e17 | 45 | |
6a89f9ee | 46 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
47 | WX_DEFINE_FLAGS( wxListBoxStyle ) |
48 | ||
3ff066a4 | 49 | wxBEGIN_FLAGS( wxListBoxStyle ) |
bc9fb572 JS |
50 | // new style border flags, we put them first to |
51 | // use them for streaming out | |
3ff066a4 SC |
52 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
53 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
54 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
55 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
56 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
57 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
7ec5921d | 58 | |
bc9fb572 | 59 | // old style border flags |
3ff066a4 SC |
60 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
61 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
62 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
63 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
64 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 65 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
66 | |
67 | // standard window styles | |
3ff066a4 SC |
68 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
69 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
70 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
71 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 72 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
73 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
74 | wxFLAGS_MEMBER(wxVSCROLL) | |
75 | wxFLAGS_MEMBER(wxHSCROLL) | |
76 | ||
77 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
78 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
79 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
80 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
81 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
82 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
83 | wxFLAGS_MEMBER(wxLB_SORT) | |
84 | ||
85 | wxEND_FLAGS( wxListBoxStyle ) | |
bc9fb572 | 86 | |
6a89f9ee SC |
87 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h") |
88 | ||
3ff066a4 | 89 | wxBEGIN_PROPERTIES_TABLE(wxListBox) |
665b71b1 WS |
90 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent ) |
91 | wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent ) | |
c5ca409b | 92 | |
af498247 | 93 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
3ff066a4 | 94 | wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
665b71b1 | 95 | wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
af498247 | 96 | wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 97 | wxEND_PROPERTIES_TABLE() |
6a89f9ee | 98 | |
3ff066a4 SC |
99 | wxBEGIN_HANDLERS_TABLE(wxListBox) |
100 | wxEND_HANDLERS_TABLE() | |
6a89f9ee | 101 | |
7ec5921d | 102 | wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) |
6a89f9ee | 103 | #else |
9c9c3d7a | 104 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
6a89f9ee | 105 | #endif |
2bda0e17 | 106 | |
066f1b7a SC |
107 | /* |
108 | TODO PROPERTIES | |
665b71b1 WS |
109 | selection |
110 | content | |
111 | item | |
066f1b7a SC |
112 | */ |
113 | ||
2bda0e17 KB |
114 | // ============================================================================ |
115 | // list box item declaration and implementation | |
116 | // ============================================================================ | |
117 | ||
47d67540 | 118 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
119 | |
120 | class wxListBoxItem : public wxOwnerDrawn | |
121 | { | |
122 | public: | |
2b5f62a0 | 123 | wxListBoxItem(const wxString& str = wxEmptyString); |
2bda0e17 KB |
124 | }; |
125 | ||
598ddd96 | 126 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false) |
2bda0e17 | 127 | { |
dd3c394a VZ |
128 | // no bitmaps/checkmarks |
129 | SetMarginWidth(0); | |
2bda0e17 KB |
130 | } |
131 | ||
2b5f62a0 | 132 | wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) |
2bda0e17 | 133 | { |
dd3c394a | 134 | return new wxListBoxItem(); |
2bda0e17 KB |
135 | } |
136 | ||
137 | #endif //USE_OWNER_DRAWN | |
138 | ||
139 | // ============================================================================ | |
140 | // list box control implementation | |
141 | // ============================================================================ | |
142 | ||
2ee3ee1b VZ |
143 | // ---------------------------------------------------------------------------- |
144 | // creation | |
145 | // ---------------------------------------------------------------------------- | |
dd3c394a | 146 | |
2ee3ee1b | 147 | // Listbox item |
bfc6fde4 | 148 | wxListBox::wxListBox() |
2bda0e17 | 149 | { |
dd3c394a VZ |
150 | m_noItems = 0; |
151 | m_selected = 0; | |
2bda0e17 KB |
152 | } |
153 | ||
dd3c394a VZ |
154 | bool wxListBox::Create(wxWindow *parent, |
155 | wxWindowID id, | |
2bda0e17 KB |
156 | const wxPoint& pos, |
157 | const wxSize& size, | |
debe6624 JS |
158 | int n, const wxString choices[], |
159 | long style, | |
56a44c64 | 160 | const wxValidator& validator, |
2bda0e17 KB |
161 | const wxString& name) |
162 | { | |
dd3c394a | 163 | m_noItems = 0; |
dd3c394a | 164 | m_selected = 0; |
2bda0e17 | 165 | |
7a69cd96 VZ |
166 | // initialize base class fields |
167 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
168 | return false; | |
2bda0e17 | 169 | |
7a69cd96 VZ |
170 | // create the native control |
171 | if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) ) | |
172 | { | |
173 | // control creation failed | |
174 | return false; | |
2bda0e17 | 175 | } |
1c089c47 | 176 | |
7a69cd96 VZ |
177 | // initialize the contents |
178 | for ( int i = 0; i < n; i++ ) | |
179 | { | |
180 | Append(choices[i]); | |
2bda0e17 | 181 | } |
2bda0e17 | 182 | |
7ec5921d VZ |
183 | // now we can compute our best size correctly, so do it if necessary |
184 | SetBestSize(size); | |
185 | ||
7a69cd96 | 186 | return true; |
2bda0e17 KB |
187 | } |
188 | ||
584ad2a3 MB |
189 | bool wxListBox::Create(wxWindow *parent, |
190 | wxWindowID id, | |
191 | const wxPoint& pos, | |
192 | const wxSize& size, | |
193 | const wxArrayString& choices, | |
194 | long style, | |
76efbc2a | 195 | const wxValidator& validator, |
584ad2a3 MB |
196 | const wxString& name) |
197 | { | |
198 | wxCArrayString chs(choices); | |
199 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
200 | style, validator, name); | |
201 | } | |
202 | ||
bfc6fde4 | 203 | wxListBox::~wxListBox() |
2bda0e17 | 204 | { |
baccb514 | 205 | Free(); |
2bda0e17 KB |
206 | } |
207 | ||
7a69cd96 | 208 | WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const |
2bda0e17 | 209 | { |
7a69cd96 VZ |
210 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); |
211 | ||
6079ab87 VZ |
212 | // always show the vertical scrollbar if necessary -- otherwise it is |
213 | // impossible to use the control with the mouse | |
214 | msStyle |= WS_VSCROLL; | |
215 | ||
7a69cd96 VZ |
216 | // we always want to get the notifications |
217 | msStyle |= LBS_NOTIFY; | |
218 | ||
219 | // without this style, you get unexpected heights, so e.g. constraint | |
220 | // layout doesn't work properly | |
221 | msStyle |= LBS_NOINTEGRALHEIGHT; | |
222 | ||
223 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
224 | _T("only one of listbox selection modes can be specified") ); | |
225 | ||
226 | if ( style & wxLB_MULTIPLE ) | |
227 | msStyle |= LBS_MULTIPLESEL; | |
228 | else if ( style & wxLB_EXTENDED ) | |
229 | msStyle |= LBS_EXTENDEDSEL; | |
230 | ||
231 | if ( m_windowStyle & wxLB_ALWAYS_SB ) | |
232 | msStyle |= LBS_DISABLENOSCROLL; | |
233 | if ( m_windowStyle & wxLB_HSCROLL ) | |
234 | msStyle |= WS_HSCROLL; | |
235 | if ( m_windowStyle & wxLB_SORT ) | |
236 | msStyle |= LBS_SORT; | |
237 | ||
238 | #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__) | |
239 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
240 | { | |
241 | // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put | |
242 | // the strings in the listbox for simplicity even though we could have | |
243 | // avoided it in this case | |
244 | msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS; | |
245 | } | |
246 | #endif // wxUSE_OWNER_DRAWN | |
247 | ||
248 | return msStyle; | |
2bda0e17 KB |
249 | } |
250 | ||
2ee3ee1b VZ |
251 | // ---------------------------------------------------------------------------- |
252 | // implementation of wxListBoxBase methods | |
253 | // ---------------------------------------------------------------------------- | |
254 | ||
255 | void wxListBox::DoSetFirstItem(int N) | |
2bda0e17 | 256 | { |
dd3c394a | 257 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 258 | wxT("invalid index in wxListBox::SetFirstItem") ); |
dd3c394a | 259 | |
2ee3ee1b | 260 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); |
2bda0e17 KB |
261 | } |
262 | ||
debe6624 | 263 | void wxListBox::Delete(int N) |
2bda0e17 | 264 | { |
dd3c394a | 265 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 266 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a | 267 | |
07cf98cb VZ |
268 | // for owner drawn objects, the data is used for storing wxOwnerDrawn |
269 | // pointers and we shouldn't touch it | |
270 | #if !wxUSE_OWNER_DRAWN | |
271 | if ( !(m_windowStyle & wxLB_OWNERDRAW) ) | |
272 | #endif // !wxUSE_OWNER_DRAWN | |
273 | if ( HasClientObjectData() ) | |
274 | { | |
275 | delete GetClientObject(N); | |
276 | } | |
6c8a980f | 277 | |
a23fd0e1 | 278 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); |
dd3c394a VZ |
279 | m_noItems--; |
280 | ||
2b5f62a0 | 281 | SetHorizontalExtent(wxEmptyString); |
2bda0e17 KB |
282 | } |
283 | ||
2ee3ee1b | 284 | int wxListBox::DoAppend(const wxString& item) |
2bda0e17 | 285 | { |
9f884528 RD |
286 | InvalidateBestSize(); |
287 | ||
a23fd0e1 | 288 | int index = ListBox_AddString(GetHwnd(), item); |
2ee3ee1b | 289 | m_noItems++; |
2bda0e17 | 290 | |
47d67540 | 291 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 292 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
2b5f62a0 | 293 | wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument |
dd3c394a | 294 | pNewItem->SetName(item); |
fd7ab28c | 295 | m_aItems.Insert(pNewItem, index); |
a23fd0e1 | 296 | ListBox_SetItemData(GetHwnd(), index, pNewItem); |
60c65519 | 297 | pNewItem->SetFont(GetFont()); |
2bda0e17 | 298 | } |
fd7ab28c | 299 | #endif // wxUSE_OWNER_DRAWN |
2bda0e17 | 300 | |
dd3c394a | 301 | SetHorizontalExtent(item); |
dd3c394a | 302 | |
2ee3ee1b | 303 | return index; |
2bda0e17 KB |
304 | } |
305 | ||
2ee3ee1b | 306 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
2bda0e17 | 307 | { |
f6bcfd97 BP |
308 | // avoid flicker - but don't need to do this for a hidden listbox |
309 | bool hideAndShow = IsShown(); | |
310 | if ( hideAndShow ) | |
311 | { | |
312 | ShowWindow(GetHwnd(), SW_HIDE); | |
313 | } | |
2ee3ee1b | 314 | |
a23fd0e1 | 315 | ListBox_ResetContent(GetHwnd()); |
2ee3ee1b VZ |
316 | |
317 | m_noItems = choices.GetCount(); | |
dd3c394a | 318 | int i; |
2ee3ee1b | 319 | for (i = 0; i < m_noItems; i++) |
dd3c394a | 320 | { |
a23fd0e1 | 321 | ListBox_AddString(GetHwnd(), choices[i]); |
dd3c394a | 322 | if ( clientData ) |
2ee3ee1b | 323 | { |
2b5f62a0 | 324 | SetClientData(i, clientData[i]); |
2ee3ee1b | 325 | } |
dd3c394a | 326 | } |
2bda0e17 | 327 | |
47d67540 | 328 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 329 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a | 330 | // first delete old items |
fd7ab28c | 331 | WX_CLEAR_ARRAY(m_aItems); |
dd3c394a VZ |
332 | |
333 | // then create new ones | |
fd7ab28c | 334 | for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) { |
2b5f62a0 | 335 | wxOwnerDrawn *pNewItem = CreateLboxItem(ui); |
dd3c394a VZ |
336 | pNewItem->SetName(choices[ui]); |
337 | m_aItems.Add(pNewItem); | |
a23fd0e1 | 338 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); |
dd3c394a | 339 | } |
2bda0e17 | 340 | } |
2ee3ee1b VZ |
341 | #endif // wxUSE_OWNER_DRAWN |
342 | ||
343 | SetHorizontalExtent(); | |
2bda0e17 | 344 | |
f6bcfd97 BP |
345 | if ( hideAndShow ) |
346 | { | |
347 | // show the listbox back if we hid it | |
348 | ShowWindow(GetHwnd(), SW_SHOW); | |
349 | } | |
2bda0e17 KB |
350 | } |
351 | ||
352 | int wxListBox::FindString(const wxString& s) const | |
353 | { | |
a23fd0e1 | 354 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); |
dd3c394a | 355 | if (pos == LB_ERR) |
2ee3ee1b | 356 | return wxNOT_FOUND; |
dd3c394a VZ |
357 | else |
358 | return pos; | |
2bda0e17 KB |
359 | } |
360 | ||
bfc6fde4 | 361 | void wxListBox::Clear() |
2bda0e17 | 362 | { |
baccb514 | 363 | Free(); |
8ee9d618 VZ |
364 | |
365 | ListBox_ResetContent(GetHwnd()); | |
366 | ||
367 | m_noItems = 0; | |
368 | SetHorizontalExtent(); | |
369 | } | |
370 | ||
371 | void wxListBox::Free() | |
2b273975 | 372 | { |
dd3c394a | 373 | #if wxUSE_OWNER_DRAWN |
185fa6bf VZ |
374 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
375 | { | |
fd7ab28c | 376 | WX_CLEAR_ARRAY(m_aItems); |
185fa6bf VZ |
377 | } |
378 | else | |
379 | #endif // wxUSE_OWNER_DRAWN | |
6c8a980f VZ |
380 | if ( HasClientObjectData() ) |
381 | { | |
382 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
383 | { | |
384 | delete GetClientObject(n); | |
385 | } | |
386 | } | |
2bda0e17 | 387 | } |
baccb514 | 388 | |
c6179a84 | 389 | void wxListBox::DoSetSelection(int N, bool select) |
2bda0e17 | 390 | { |
7ec5921d | 391 | wxCHECK_RET( N == wxNOT_FOUND || |
5955710c | 392 | (N >= 0 && N < m_noItems), |
223d09f6 | 393 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 394 | |
2ee3ee1b VZ |
395 | if ( HasMultipleSelection() ) |
396 | { | |
a23fd0e1 | 397 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
2ee3ee1b | 398 | } |
dd3c394a VZ |
399 | else |
400 | { | |
2ee3ee1b | 401 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 402 | } |
2bda0e17 KB |
403 | } |
404 | ||
2ee3ee1b | 405 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 406 | { |
598ddd96 | 407 | wxCHECK_MSG( N >= 0 && N < m_noItems, false, |
223d09f6 | 408 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 409 | |
598ddd96 | 410 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true; |
2bda0e17 KB |
411 | } |
412 | ||
6c8a980f | 413 | wxClientData* wxListBox::DoGetItemClientObject(int n) const |
2bda0e17 | 414 | { |
6c8a980f | 415 | return (wxClientData *)DoGetItemClientData(n); |
2bda0e17 KB |
416 | } |
417 | ||
6c8a980f | 418 | void *wxListBox::DoGetItemClientData(int n) const |
2bda0e17 | 419 | { |
2ee3ee1b | 420 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, |
223d09f6 | 421 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 422 | |
2ee3ee1b | 423 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
424 | } |
425 | ||
6c8a980f | 426 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) |
2bda0e17 | 427 | { |
6c8a980f | 428 | DoSetItemClientData(n, clientData); |
2ee3ee1b VZ |
429 | } |
430 | ||
6c8a980f | 431 | void wxListBox::DoSetItemClientData(int n, void *clientData) |
2ee3ee1b VZ |
432 | { |
433 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
223d09f6 | 434 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 435 | |
2ee3ee1b VZ |
436 | #if wxUSE_OWNER_DRAWN |
437 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
438 | { | |
439 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
440 | // in OnMeasure/OnDraw. | |
441 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
442 | } | |
443 | #endif // wxUSE_OWNER_DRAWN | |
444 | ||
445 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
223d09f6 | 446 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
2bda0e17 KB |
447 | } |
448 | ||
449 | // Return number of selections and an array of selected integers | |
14483330 | 450 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 451 | { |
dd3c394a | 452 | aSelections.Empty(); |
14483330 | 453 | |
2ee3ee1b | 454 | if ( HasMultipleSelection() ) |
dd3c394a | 455 | { |
d90879fa VZ |
456 | int countSel = ListBox_GetSelCount(GetHwnd()); |
457 | if ( countSel == LB_ERR ) | |
458 | { | |
459 | wxLogDebug(_T("ListBox_GetSelCount failed")); | |
460 | } | |
461 | else if ( countSel != 0 ) | |
462 | { | |
463 | int *selections = new int[countSel]; | |
14483330 | 464 | |
d90879fa VZ |
465 | if ( ListBox_GetSelItems(GetHwnd(), |
466 | countSel, selections) == LB_ERR ) | |
467 | { | |
468 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
469 | countSel = -1; | |
470 | } | |
471 | else | |
472 | { | |
473 | aSelections.Alloc(countSel); | |
474 | for ( int n = 0; n < countSel; n++ ) | |
475 | aSelections.Add(selections[n]); | |
476 | } | |
14483330 | 477 | |
dd3c394a VZ |
478 | delete [] selections; |
479 | } | |
14483330 | 480 | |
d90879fa | 481 | return countSel; |
dd3c394a VZ |
482 | } |
483 | else // single-selection listbox | |
484 | { | |
f6bcfd97 BP |
485 | if (ListBox_GetCurSel(GetHwnd()) > -1) |
486 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
14483330 | 487 | |
f6bcfd97 | 488 | return aSelections.Count(); |
dd3c394a | 489 | } |
2bda0e17 KB |
490 | } |
491 | ||
492 | // Get single selection, for single choice list items | |
14483330 | 493 | int wxListBox::GetSelection() const |
2bda0e17 | 494 | { |
2ee3ee1b | 495 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 496 | -1, |
f6bcfd97 | 497 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); |
14483330 | 498 | |
a23fd0e1 | 499 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
500 | } |
501 | ||
502 | // Find string for position | |
debe6624 | 503 | wxString wxListBox::GetString(int N) const |
2bda0e17 | 504 | { |
2b5f62a0 | 505 | wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString, |
66cf41cb | 506 | wxT("invalid index in wxListBox::GetString") ); |
dd3c394a | 507 | |
a23fd0e1 | 508 | int len = ListBox_GetTextLen(GetHwnd(), N); |
dd3c394a VZ |
509 | |
510 | // +1 for terminating NUL | |
511 | wxString result; | |
c34924f7 | 512 | ListBox_GetText(GetHwnd(), N, (wxChar*)wxStringBuffer(result, len + 1)); |
dd3c394a VZ |
513 | |
514 | return result; | |
2bda0e17 KB |
515 | } |
516 | ||
2ee3ee1b VZ |
517 | void |
518 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
519 | { | |
520 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
521 | wxT("invalid index in wxListBox::InsertItems") ); | |
522 | ||
9f884528 RD |
523 | InvalidateBestSize(); |
524 | ||
2ee3ee1b VZ |
525 | int nItems = items.GetCount(); |
526 | for ( int i = 0; i < nItems; i++ ) | |
fd7ab28c VZ |
527 | { |
528 | int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
529 | ||
530 | #if wxUSE_OWNER_DRAWN | |
9085d634 RD |
531 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
532 | { | |
2b5f62a0 | 533 | wxOwnerDrawn *pNewItem = CreateLboxItem(idx); |
9085d634 RD |
534 | pNewItem->SetName(items[i]); |
535 | pNewItem->SetFont(GetFont()); | |
536 | m_aItems.Insert(pNewItem, idx); | |
fd7ab28c | 537 | |
9085d634 RD |
538 | ListBox_SetItemData(GetHwnd(), idx, pNewItem); |
539 | } | |
665b71b1 WS |
540 | #else |
541 | wxUnusedVar(idx); | |
fd7ab28c VZ |
542 | #endif // wxUSE_OWNER_DRAWN |
543 | } | |
544 | ||
2ee3ee1b VZ |
545 | m_noItems += nItems; |
546 | ||
547 | SetHorizontalExtent(); | |
548 | } | |
549 | ||
550 | void wxListBox::SetString(int N, const wxString& s) | |
551 | { | |
552 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
553 | wxT("invalid index in wxListBox::SetString") ); | |
554 | ||
555 | // remember the state of the item | |
556 | bool wasSelected = IsSelected(N); | |
557 | ||
558 | void *oldData = NULL; | |
559 | wxClientData *oldObjData = NULL; | |
1e6feb95 | 560 | if ( m_clientDataItemsType == wxClientData_Void ) |
2ee3ee1b | 561 | oldData = GetClientData(N); |
1e6feb95 | 562 | else if ( m_clientDataItemsType == wxClientData_Object ) |
2ee3ee1b VZ |
563 | oldObjData = GetClientObject(N); |
564 | ||
565 | // delete and recreate it | |
566 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
567 | ||
568 | int newN = N; | |
569 | if ( N == m_noItems - 1 ) | |
570 | newN = -1; | |
571 | ||
572 | ListBox_InsertString(GetHwnd(), newN, s); | |
573 | ||
574 | // restore the client data | |
575 | if ( oldData ) | |
576 | SetClientData(N, oldData); | |
577 | else if ( oldObjData ) | |
578 | SetClientObject(N, oldObjData); | |
579 | ||
2ee3ee1b VZ |
580 | #if wxUSE_OWNER_DRAWN |
581 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
c294f450 | 582 | { |
2ee3ee1b VZ |
583 | // update item's text |
584 | m_aItems[N]->SetName(s); | |
fd7ab28c | 585 | |
c294f450 RD |
586 | // reassign the item's data |
587 | ListBox_SetItemData(GetHwnd(), N, m_aItems[N]); | |
588 | } | |
2ee3ee1b | 589 | #endif //USE_OWNER_DRAWN |
781bdbb4 JS |
590 | |
591 | // we may have lost the selection | |
592 | if ( wasSelected ) | |
593 | Select(N); | |
2ee3ee1b VZ |
594 | } |
595 | ||
596 | int wxListBox::GetCount() const | |
597 | { | |
598 | return m_noItems; | |
599 | } | |
600 | ||
601 | // ---------------------------------------------------------------------------- | |
602 | // helpers | |
603 | // ---------------------------------------------------------------------------- | |
604 | ||
4438caf4 VZ |
605 | // Windows-specific code to set the horizontal extent of the listbox, if |
606 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. | |
2bda0e17 KB |
607 | // Otherwise, all strings are used. |
608 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
609 | { | |
dd3c394a VZ |
610 | // Only necessary if we want a horizontal scrollbar |
611 | if (!(m_windowStyle & wxHSCROLL)) | |
612 | return; | |
613 | TEXTMETRIC lpTextMetric; | |
614 | ||
c34924f7 | 615 | if ( !s.empty() ) |
2bda0e17 | 616 | { |
a23fd0e1 VZ |
617 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); |
618 | HDC dc = GetWindowDC(GetHwnd()); | |
dd3c394a | 619 | HFONT oldFont = 0; |
6305fabe | 620 | if (GetFont().Ok() && GetFont().GetResourceHandle() != 0) |
dd3c394a VZ |
621 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); |
622 | ||
623 | GetTextMetrics(dc, &lpTextMetric); | |
624 | SIZE extentXY; | |
837e5743 | 625 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); |
dd3c394a VZ |
626 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
627 | ||
628 | if (oldFont) | |
629 | ::SelectObject(dc, oldFont); | |
630 | ||
a23fd0e1 | 631 | ReleaseDC(GetHwnd(), dc); |
dd3c394a | 632 | if (extentX > existingExtent) |
a23fd0e1 | 633 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); |
dd3c394a VZ |
634 | } |
635 | else | |
636 | { | |
637 | int largestExtent = 0; | |
a23fd0e1 | 638 | HDC dc = GetWindowDC(GetHwnd()); |
dd3c394a | 639 | HFONT oldFont = 0; |
6305fabe | 640 | if (GetFont().Ok() && GetFont().GetResourceHandle() != 0) |
dd3c394a VZ |
641 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); |
642 | ||
643 | GetTextMetrics(dc, &lpTextMetric); | |
3e3be693 | 644 | |
3e3be693 | 645 | for (int i = 0; i < m_noItems; i++) |
dd3c394a | 646 | { |
50c6bf91 | 647 | wxString str = GetString(i); |
dd3c394a | 648 | SIZE extentXY; |
50c6bf91 | 649 | ::GetTextExtentPoint(dc, str.c_str(), str.length(), &extentXY); |
dd3c394a VZ |
650 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
651 | if (extentX > largestExtent) | |
652 | largestExtent = extentX; | |
653 | } | |
654 | if (oldFont) | |
655 | ::SelectObject(dc, oldFont); | |
656 | ||
a23fd0e1 VZ |
657 | ReleaseDC(GetHwnd(), dc); |
658 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
2bda0e17 | 659 | } |
2bda0e17 KB |
660 | } |
661 | ||
f68586e5 | 662 | wxSize wxListBox::DoGetBestSize() const |
b908d224 VZ |
663 | { |
664 | // find the widest string | |
665 | int wLine; | |
666 | int wListbox = 0; | |
667 | for ( int i = 0; i < m_noItems; i++ ) | |
668 | { | |
669 | wxString str(GetString(i)); | |
670 | GetTextExtent(str, &wLine, NULL); | |
671 | if ( wLine > wListbox ) | |
672 | wListbox = wLine; | |
673 | } | |
674 | ||
675 | // give it some reasonable default value if there are no strings in the | |
676 | // list | |
677 | if ( wListbox == 0 ) | |
678 | wListbox = 100; | |
679 | ||
680 | // the listbox should be slightly larger than the widest string | |
681 | int cx, cy; | |
7a5e53ab | 682 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
b908d224 VZ |
683 | |
684 | wListbox += 3*cx; | |
685 | ||
908f91bc RD |
686 | // Add room for the scrollbar |
687 | wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
688 | ||
5b6d6b70 VZ |
689 | // don't make the listbox too tall (limit height to 10 items) but don't |
690 | // make it too small neither | |
691 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* | |
692 | wxMin(wxMax(m_noItems, 3), 10); | |
b908d224 VZ |
693 | |
694 | return wxSize(wListbox, hListbox); | |
695 | } | |
696 | ||
2ee3ee1b VZ |
697 | // ---------------------------------------------------------------------------- |
698 | // callbacks | |
699 | // ---------------------------------------------------------------------------- | |
700 | ||
701 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 702 | { |
bada28f0 | 703 | wxEventType evtType; |
8ee9d618 | 704 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 705 | { |
bada28f0 | 706 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; |
2bda0e17 | 707 | } |
8ee9d618 | 708 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b | 709 | { |
bada28f0 VZ |
710 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
711 | } | |
712 | else | |
713 | { | |
714 | // some event we're not interested in | |
598ddd96 | 715 | return false; |
bada28f0 VZ |
716 | } |
717 | ||
718 | wxCommandEvent event(evtType, m_windowId); | |
719 | event.SetEventObject( this ); | |
8ee9d618 | 720 | |
9c9c3d7a VZ |
721 | // retrieve the affected item |
722 | int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
723 | if ( n != LB_ERR ) | |
bada28f0 | 724 | { |
bada28f0 VZ |
725 | if ( HasClientObjectData() ) |
726 | event.SetClientObject( GetClientObject(n) ); | |
727 | else if ( HasClientUntypedData() ) | |
728 | event.SetClientData( GetClientData(n) ); | |
9c9c3d7a | 729 | |
bada28f0 | 730 | event.SetString( GetString(n) ); |
598ddd96 | 731 | event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true ); |
bada28f0 VZ |
732 | } |
733 | ||
687706f5 | 734 | event.SetInt(n); |
2ee3ee1b | 735 | |
bada28f0 | 736 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
737 | } |
738 | ||
2ee3ee1b VZ |
739 | // ---------------------------------------------------------------------------- |
740 | // wxCheckListBox support | |
741 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 742 | |
47d67540 | 743 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
744 | |
745 | // drawing | |
746 | // ------- | |
747 | ||
748 | // space beneath/above each row in pixels | |
749 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
750 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
751 | ||
752 | // the height is the same for all items | |
dd3c394a VZ |
753 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
754 | ||
755 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
756 | // message is not yet sent when we get here! | |
2bda0e17 KB |
757 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
758 | { | |
dd3c394a | 759 | // only owner-drawn control should receive this message |
598ddd96 | 760 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); |
2bda0e17 | 761 | |
dd3c394a | 762 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 763 | |
4676948b JS |
764 | #ifdef __WXWINCE__ |
765 | HDC hdc = GetDC(NULL); | |
766 | #else | |
07cf98cb | 767 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
4676948b | 768 | #endif |
07cf98cb | 769 | |
dd3c394a | 770 | wxDC dc; |
07cf98cb | 771 | dc.SetHDC((WXHDC)hdc); |
e0176dd9 | 772 | dc.SetFont(GetFont()); |
2bda0e17 | 773 | |
dd3c394a VZ |
774 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
775 | pStruct->itemWidth = dc.GetCharWidth(); | |
2bda0e17 | 776 | |
07cf98cb VZ |
777 | dc.SetHDC(0); |
778 | ||
779 | DeleteDC(hdc); | |
780 | ||
598ddd96 | 781 | return true; |
2bda0e17 KB |
782 | } |
783 | ||
784 | // forward the message to the appropriate item | |
785 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
786 | { | |
dd3c394a | 787 | // only owner-drawn control should receive this message |
598ddd96 | 788 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); |
dd3c394a VZ |
789 | |
790 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
f6bcfd97 BP |
791 | UINT itemID = pStruct->itemID; |
792 | ||
793 | // the item may be -1 for an empty listbox | |
794 | if ( itemID == (UINT)-1 ) | |
598ddd96 | 795 | return false; |
dd3c394a | 796 | |
a23fd0e1 | 797 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); |
2bda0e17 | 798 | |
598ddd96 | 799 | wxCHECK( data && (data != LB_ERR), false ); |
2bda0e17 | 800 | |
dd3c394a | 801 | wxListBoxItem *pItem = (wxListBoxItem *)data; |
2bda0e17 | 802 | |
7561aacd | 803 | wxDCTemp dc((WXHDC)pStruct->hDC); |
42841dfc WS |
804 | wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top); |
805 | wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom); | |
806 | wxRect rect(pt1, pt2); | |
2bda0e17 | 807 | |
dd3c394a | 808 | return pItem->OnDrawItem(dc, rect, |
7561aacd VZ |
809 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, |
810 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
2bda0e17 KB |
811 | } |
812 | ||
1e6feb95 VZ |
813 | #endif // wxUSE_OWNER_DRAWN |
814 | ||
815 | #endif // wxUSE_LISTBOX |