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