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