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