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