]>
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 | |
2a673eb1 WS |
21 | #include "wx/listbox.h" |
22 | ||
2bda0e17 | 23 | #ifndef WX_PRECOMP |
ad9835c9 | 24 | #include "wx/dynarray.h" |
ad9835c9 WS |
25 | #include "wx/settings.h" |
26 | #include "wx/brush.h" | |
27 | #include "wx/font.h" | |
28 | #include "wx/dc.h" | |
29 | #include "wx/utils.h" | |
e4db172a | 30 | #include "wx/log.h" |
cdccdfab | 31 | #include "wx/window.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
1e6feb95 VZ |
34 | #include "wx/msw/private.h" |
35 | ||
5ea105e0 RR |
36 | #include <windowsx.h> |
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 | |
0ad2a19e | 83 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems,"wx/listbox.h") |
6a89f9ee | 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 |
0ad2a19e | 100 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) |
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 | 179 | // now we can compute our best size correctly, so do it if necessary |
170acdc9 | 180 | SetInitialSize(size); |
7ec5921d | 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 | ||
a236aa20 | 259 | void wxListBox::DoDeleteOneItem(unsigned int n) |
2bda0e17 | 260 | { |
aa61d352 | 261 | wxCHECK_RET( IsValid(n), |
223d09f6 | 262 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a | 263 | |
aa61d352 | 264 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); |
dd3c394a VZ |
265 | m_noItems--; |
266 | ||
2b5f62a0 | 267 | SetHorizontalExtent(wxEmptyString); |
2bda0e17 KB |
268 | } |
269 | ||
11e62fe6 | 270 | int wxListBox::FindString(const wxString& s, bool bCase) const |
2bda0e17 | 271 | { |
11e62fe6 WS |
272 | // back to base class search for not native search type |
273 | if (bCase) | |
274 | return wxItemContainerImmutable::FindString( s, bCase ); | |
275 | ||
e0a050e3 | 276 | int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str()); |
dd3c394a | 277 | if (pos == LB_ERR) |
2ee3ee1b | 278 | return wxNOT_FOUND; |
dd3c394a VZ |
279 | else |
280 | return pos; | |
2bda0e17 KB |
281 | } |
282 | ||
a236aa20 | 283 | void wxListBox::DoClear() |
2bda0e17 | 284 | { |
baccb514 | 285 | Free(); |
8ee9d618 VZ |
286 | |
287 | ListBox_ResetContent(GetHwnd()); | |
288 | ||
289 | m_noItems = 0; | |
290 | SetHorizontalExtent(); | |
291 | } | |
292 | ||
293 | void wxListBox::Free() | |
2b273975 | 294 | { |
dd3c394a | 295 | #if wxUSE_OWNER_DRAWN |
185fa6bf VZ |
296 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
297 | { | |
fd7ab28c | 298 | WX_CLEAR_ARRAY(m_aItems); |
185fa6bf | 299 | } |
185fa6bf | 300 | #endif // wxUSE_OWNER_DRAWN |
2bda0e17 | 301 | } |
baccb514 | 302 | |
c6179a84 | 303 | void wxListBox::DoSetSelection(int N, bool select) |
2bda0e17 | 304 | { |
8228b893 | 305 | wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), |
223d09f6 | 306 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 307 | |
2ee3ee1b VZ |
308 | if ( HasMultipleSelection() ) |
309 | { | |
a23fd0e1 | 310 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
2ee3ee1b | 311 | } |
dd3c394a VZ |
312 | else |
313 | { | |
2ee3ee1b | 314 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 315 | } |
2bda0e17 KB |
316 | } |
317 | ||
2ee3ee1b | 318 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 319 | { |
8228b893 | 320 | wxCHECK_MSG( IsValid(N), false, |
223d09f6 | 321 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 322 | |
598ddd96 | 323 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true; |
2bda0e17 KB |
324 | } |
325 | ||
aa61d352 | 326 | void *wxListBox::DoGetItemClientData(unsigned int n) const |
2bda0e17 | 327 | { |
8228b893 | 328 | wxCHECK_MSG( IsValid(n), NULL, |
223d09f6 | 329 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 330 | |
2ee3ee1b | 331 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
332 | } |
333 | ||
aa61d352 | 334 | void wxListBox::DoSetItemClientData(unsigned int n, void *clientData) |
2ee3ee1b | 335 | { |
8228b893 | 336 | wxCHECK_RET( IsValid(n), |
223d09f6 | 337 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 338 | |
2ee3ee1b VZ |
339 | #if wxUSE_OWNER_DRAWN |
340 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
341 | { | |
342 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
343 | // in OnMeasure/OnDraw. | |
344 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
345 | } | |
346 | #endif // wxUSE_OWNER_DRAWN | |
347 | ||
348 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
223d09f6 | 349 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
2bda0e17 KB |
350 | } |
351 | ||
352 | // Return number of selections and an array of selected integers | |
14483330 | 353 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 354 | { |
dd3c394a | 355 | aSelections.Empty(); |
14483330 | 356 | |
2ee3ee1b | 357 | if ( HasMultipleSelection() ) |
dd3c394a | 358 | { |
d90879fa VZ |
359 | int countSel = ListBox_GetSelCount(GetHwnd()); |
360 | if ( countSel == LB_ERR ) | |
361 | { | |
362 | wxLogDebug(_T("ListBox_GetSelCount failed")); | |
363 | } | |
364 | else if ( countSel != 0 ) | |
365 | { | |
366 | int *selections = new int[countSel]; | |
14483330 | 367 | |
d90879fa VZ |
368 | if ( ListBox_GetSelItems(GetHwnd(), |
369 | countSel, selections) == LB_ERR ) | |
370 | { | |
371 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
372 | countSel = -1; | |
373 | } | |
374 | else | |
375 | { | |
376 | aSelections.Alloc(countSel); | |
377 | for ( int n = 0; n < countSel; n++ ) | |
378 | aSelections.Add(selections[n]); | |
379 | } | |
14483330 | 380 | |
dd3c394a VZ |
381 | delete [] selections; |
382 | } | |
14483330 | 383 | |
d90879fa | 384 | return countSel; |
dd3c394a VZ |
385 | } |
386 | else // single-selection listbox | |
387 | { | |
f6bcfd97 BP |
388 | if (ListBox_GetCurSel(GetHwnd()) > -1) |
389 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
14483330 | 390 | |
f6bcfd97 | 391 | return aSelections.Count(); |
dd3c394a | 392 | } |
2bda0e17 KB |
393 | } |
394 | ||
395 | // Get single selection, for single choice list items | |
14483330 | 396 | int wxListBox::GetSelection() const |
2bda0e17 | 397 | { |
2ee3ee1b | 398 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 399 | -1, |
f6bcfd97 | 400 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); |
14483330 | 401 | |
a23fd0e1 | 402 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
403 | } |
404 | ||
405 | // Find string for position | |
aa61d352 | 406 | wxString wxListBox::GetString(unsigned int n) const |
2bda0e17 | 407 | { |
aa61d352 | 408 | wxCHECK_MSG( IsValid(n), wxEmptyString, |
66cf41cb | 409 | wxT("invalid index in wxListBox::GetString") ); |
dd3c394a | 410 | |
aa61d352 | 411 | int len = ListBox_GetTextLen(GetHwnd(), n); |
dd3c394a VZ |
412 | |
413 | // +1 for terminating NUL | |
414 | wxString result; | |
aa61d352 | 415 | ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1)); |
dd3c394a VZ |
416 | |
417 | return result; | |
2bda0e17 KB |
418 | } |
419 | ||
a236aa20 VZ |
420 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, |
421 | unsigned int pos, | |
422 | void **clientData, | |
423 | wxClientDataType type) | |
2ee3ee1b | 424 | { |
a236aa20 VZ |
425 | MSWAllocStorage(items, LB_INITSTORAGE); |
426 | ||
427 | const bool append = pos == GetCount(); | |
428 | ||
429 | // we must use CB_ADDSTRING when appending as only it works correctly for | |
430 | // the sorted controls | |
431 | const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING; | |
2ee3ee1b | 432 | |
a236aa20 VZ |
433 | if ( append ) |
434 | pos = 0; | |
435 | ||
436 | int n = wxNOT_FOUND; | |
437 | ||
438 | const unsigned int numItems = items.GetCount(); | |
439 | for ( unsigned int i = 0; i < numItems; i++ ) | |
fd7ab28c | 440 | { |
a236aa20 VZ |
441 | n = MSWInsertOrAppendItem(pos, items[i], msg); |
442 | if ( n == wxNOT_FOUND ) | |
443 | return n; | |
444 | ||
445 | if ( !append ) | |
446 | pos++; | |
447 | ||
448 | ++m_noItems; | |
fd7ab28c VZ |
449 | |
450 | #if wxUSE_OWNER_DRAWN | |
a236aa20 | 451 | if ( HasFlag(wxLB_OWNERDRAW) ) |
9085d634 | 452 | { |
a236aa20 | 453 | wxOwnerDrawn *pNewItem = CreateLboxItem(n); |
9085d634 RD |
454 | pNewItem->SetName(items[i]); |
455 | pNewItem->SetFont(GetFont()); | |
a236aa20 | 456 | m_aItems.Insert(pNewItem, n); |
fd7ab28c | 457 | |
a236aa20 | 458 | ListBox_SetItemData(GetHwnd(), n, pNewItem); |
9085d634 | 459 | } |
fd7ab28c | 460 | #endif // wxUSE_OWNER_DRAWN |
a236aa20 | 461 | AssignNewItemClientData(n, clientData, i, type); |
fd7ab28c VZ |
462 | } |
463 | ||
2ee3ee1b | 464 | SetHorizontalExtent(); |
a236aa20 VZ |
465 | |
466 | return n; | |
2ee3ee1b VZ |
467 | } |
468 | ||
c00fed0e VZ |
469 | int wxListBox::DoListHitTest(const wxPoint& point) const |
470 | { | |
8228b893 | 471 | LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT, |
c00fed0e VZ |
472 | 0L, MAKELONG(point.x, point.y)); |
473 | ||
474 | // non zero high-order word means that this item is outside of the client | |
475 | // area, IOW the point is outside of the listbox | |
476 | return HIWORD(lRes) ? wxNOT_FOUND : lRes; | |
477 | } | |
478 | ||
aa61d352 | 479 | void wxListBox::SetString(unsigned int n, const wxString& s) |
2ee3ee1b | 480 | { |
aa61d352 | 481 | wxCHECK_RET( IsValid(n), |
2ee3ee1b VZ |
482 | wxT("invalid index in wxListBox::SetString") ); |
483 | ||
484 | // remember the state of the item | |
aa61d352 | 485 | bool wasSelected = IsSelected(n); |
2ee3ee1b VZ |
486 | |
487 | void *oldData = NULL; | |
488 | wxClientData *oldObjData = NULL; | |
b4a11fe8 | 489 | if ( HasClientUntypedData() ) |
aa61d352 | 490 | oldData = GetClientData(n); |
b4a11fe8 | 491 | else if ( HasClientObjectData() ) |
aa61d352 | 492 | oldObjData = GetClientObject(n); |
2ee3ee1b VZ |
493 | |
494 | // delete and recreate it | |
aa61d352 | 495 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); |
2ee3ee1b | 496 | |
aa61d352 VZ |
497 | int newN = n; |
498 | if ( n == (m_noItems - 1) ) | |
2ee3ee1b VZ |
499 | newN = -1; |
500 | ||
e0a050e3 | 501 | ListBox_InsertString(GetHwnd(), newN, s.wx_str()); |
2ee3ee1b VZ |
502 | |
503 | // restore the client data | |
504 | if ( oldData ) | |
aa61d352 | 505 | SetClientData(n, oldData); |
2ee3ee1b | 506 | else if ( oldObjData ) |
aa61d352 | 507 | SetClientObject(n, oldObjData); |
2ee3ee1b | 508 | |
2ee3ee1b VZ |
509 | #if wxUSE_OWNER_DRAWN |
510 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
c294f450 | 511 | { |
2ee3ee1b | 512 | // update item's text |
aa61d352 | 513 | m_aItems[n]->SetName(s); |
fd7ab28c | 514 | |
c294f450 | 515 | // reassign the item's data |
aa61d352 | 516 | ListBox_SetItemData(GetHwnd(), n, m_aItems[n]); |
c294f450 | 517 | } |
2ee3ee1b | 518 | #endif //USE_OWNER_DRAWN |
781bdbb4 JS |
519 | |
520 | // we may have lost the selection | |
521 | if ( wasSelected ) | |
aa61d352 | 522 | Select(n); |
31582e4e | 523 | |
fca418ae | 524 | SetHorizontalExtent(); |
2ee3ee1b VZ |
525 | } |
526 | ||
aa61d352 | 527 | unsigned int wxListBox::GetCount() const |
2ee3ee1b VZ |
528 | { |
529 | return m_noItems; | |
530 | } | |
531 | ||
532 | // ---------------------------------------------------------------------------- | |
fca418ae | 533 | // size-related stuff |
2ee3ee1b VZ |
534 | // ---------------------------------------------------------------------------- |
535 | ||
2bda0e17 KB |
536 | void wxListBox::SetHorizontalExtent(const wxString& s) |
537 | { | |
fca418ae VZ |
538 | // in any case, our best size could have changed |
539 | InvalidateBestSize(); | |
540 | ||
541 | // the rest is only necessary if we want a horizontal scrollbar | |
542 | if ( !HasFlag(wxHSCROLL) ) | |
dd3c394a | 543 | return; |
dd3c394a | 544 | |
dd3c394a | 545 | |
fca418ae VZ |
546 | WindowHDC dc(GetHwnd()); |
547 | SelectInHDC selFont(dc, GetHfontOf(GetFont())); | |
dd3c394a | 548 | |
fca418ae VZ |
549 | TEXTMETRIC lpTextMetric; |
550 | ::GetTextMetrics(dc, &lpTextMetric); | |
dd3c394a | 551 | |
fca418ae VZ |
552 | int largestExtent = 0; |
553 | SIZE extentXY; | |
3e3be693 | 554 | |
fca418ae VZ |
555 | if ( s.empty() ) |
556 | { | |
557 | // set extent to the max length of all strings | |
558 | for ( unsigned int i = 0; i < m_noItems; i++ ) | |
dd3c394a | 559 | { |
fca418ae | 560 | const wxString str = GetString(i); |
767b35a5 | 561 | ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY); |
fca418ae | 562 | |
dd3c394a | 563 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
fca418ae | 564 | if ( extentX > largestExtent ) |
dd3c394a VZ |
565 | largestExtent = extentX; |
566 | } | |
fca418ae VZ |
567 | } |
568 | else // just increase the extent to the length of this string | |
569 | { | |
570 | int existingExtent = (int)SendMessage(GetHwnd(), | |
571 | LB_GETHORIZONTALEXTENT, 0, 0L); | |
dd3c394a | 572 | |
fca418ae VZ |
573 | ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY); |
574 | ||
575 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
576 | if ( extentX > existingExtent ) | |
577 | largestExtent = extentX; | |
2bda0e17 | 578 | } |
fca418ae VZ |
579 | |
580 | if ( largestExtent ) | |
581 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
582 | //else: it shouldn't change | |
2bda0e17 KB |
583 | } |
584 | ||
f68586e5 | 585 | wxSize wxListBox::DoGetBestSize() const |
b908d224 VZ |
586 | { |
587 | // find the widest string | |
588 | int wLine; | |
589 | int wListbox = 0; | |
aa61d352 | 590 | for (unsigned int i = 0; i < m_noItems; i++) |
b908d224 VZ |
591 | { |
592 | wxString str(GetString(i)); | |
593 | GetTextExtent(str, &wLine, NULL); | |
594 | if ( wLine > wListbox ) | |
595 | wListbox = wLine; | |
596 | } | |
597 | ||
598 | // give it some reasonable default value if there are no strings in the | |
599 | // list | |
600 | if ( wListbox == 0 ) | |
601 | wListbox = 100; | |
602 | ||
603 | // the listbox should be slightly larger than the widest string | |
604 | int cx, cy; | |
7a5e53ab | 605 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
b908d224 VZ |
606 | |
607 | wListbox += 3*cx; | |
608 | ||
908f91bc RD |
609 | // Add room for the scrollbar |
610 | wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
611 | ||
5b6d6b70 VZ |
612 | // don't make the listbox too tall (limit height to 10 items) but don't |
613 | // make it too small neither | |
614 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* | |
615 | wxMin(wxMax(m_noItems, 3), 10); | |
b908d224 | 616 | |
31582e4e RD |
617 | wxSize best(wListbox, hListbox); |
618 | CacheBestSize(best); | |
619 | return best; | |
b908d224 VZ |
620 | } |
621 | ||
2ee3ee1b VZ |
622 | // ---------------------------------------------------------------------------- |
623 | // callbacks | |
624 | // ---------------------------------------------------------------------------- | |
625 | ||
626 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 627 | { |
bada28f0 | 628 | wxEventType evtType; |
8ee9d618 | 629 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 630 | { |
bada28f0 | 631 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; |
2bda0e17 | 632 | } |
8ee9d618 | 633 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b | 634 | { |
bada28f0 VZ |
635 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
636 | } | |
637 | else | |
638 | { | |
639 | // some event we're not interested in | |
598ddd96 | 640 | return false; |
bada28f0 VZ |
641 | } |
642 | ||
643 | wxCommandEvent event(evtType, m_windowId); | |
644 | event.SetEventObject( this ); | |
8ee9d618 | 645 | |
9c9c3d7a VZ |
646 | // retrieve the affected item |
647 | int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
648 | if ( n != LB_ERR ) | |
bada28f0 | 649 | { |
bada28f0 VZ |
650 | if ( HasClientObjectData() ) |
651 | event.SetClientObject( GetClientObject(n) ); | |
652 | else if ( HasClientUntypedData() ) | |
653 | event.SetClientData( GetClientData(n) ); | |
9c9c3d7a | 654 | |
aa61d352 | 655 | event.SetString(GetString(n)); |
598ddd96 | 656 | event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true ); |
bada28f0 VZ |
657 | } |
658 | ||
687706f5 | 659 | event.SetInt(n); |
2ee3ee1b | 660 | |
bada28f0 | 661 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
662 | } |
663 | ||
2ee3ee1b VZ |
664 | // ---------------------------------------------------------------------------- |
665 | // wxCheckListBox support | |
666 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 667 | |
47d67540 | 668 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
669 | |
670 | // drawing | |
671 | // ------- | |
672 | ||
673 | // space beneath/above each row in pixels | |
674 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
675 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
676 | ||
677 | // the height is the same for all items | |
dd3c394a VZ |
678 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
679 | ||
680 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
681 | // message is not yet sent when we get here! | |
2bda0e17 KB |
682 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
683 | { | |
dd3c394a | 684 | // only owner-drawn control should receive this message |
598ddd96 | 685 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); |
2bda0e17 | 686 | |
dd3c394a | 687 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 688 | |
4676948b JS |
689 | #ifdef __WXWINCE__ |
690 | HDC hdc = GetDC(NULL); | |
691 | #else | |
07cf98cb | 692 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
4676948b | 693 | #endif |
07cf98cb | 694 | |
7d09b97f VZ |
695 | { |
696 | wxDCTemp dc((WXHDC)hdc); | |
697 | dc.SetFont(GetFont()); | |
2bda0e17 | 698 | |
7d09b97f VZ |
699 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
700 | pStruct->itemWidth = dc.GetCharWidth(); | |
701 | } | |
07cf98cb | 702 | |
7d09b97f VZ |
703 | #ifdef __WXWINCE__ |
704 | ReleaseDC(NULL, hdc); | |
705 | #else | |
07cf98cb | 706 | DeleteDC(hdc); |
7d09b97f | 707 | #endif |
07cf98cb | 708 | |
598ddd96 | 709 | return true; |
2bda0e17 KB |
710 | } |
711 | ||
712 | // forward the message to the appropriate item | |
713 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
714 | { | |
dd3c394a | 715 | // only owner-drawn control should receive this message |
598ddd96 | 716 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); |
dd3c394a VZ |
717 | |
718 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
f6bcfd97 BP |
719 | UINT itemID = pStruct->itemID; |
720 | ||
721 | // the item may be -1 for an empty listbox | |
722 | if ( itemID == (UINT)-1 ) | |
598ddd96 | 723 | return false; |
dd3c394a | 724 | |
81b635b1 | 725 | LRESULT data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); |
2bda0e17 | 726 | |
598ddd96 | 727 | wxCHECK( data && (data != LB_ERR), false ); |
2bda0e17 | 728 | |
dd3c394a | 729 | wxListBoxItem *pItem = (wxListBoxItem *)data; |
2bda0e17 | 730 | |
7561aacd | 731 | wxDCTemp dc((WXHDC)pStruct->hDC); |
42841dfc WS |
732 | wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top); |
733 | wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom); | |
734 | wxRect rect(pt1, pt2); | |
2bda0e17 | 735 | |
dd3c394a | 736 | return pItem->OnDrawItem(dc, rect, |
7561aacd VZ |
737 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, |
738 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
2bda0e17 KB |
739 | } |
740 | ||
1e6feb95 VZ |
741 | #endif // wxUSE_OWNER_DRAWN |
742 | ||
743 | #endif // wxUSE_LISTBOX |