]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
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 | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
dd3c394a | 13 | #pragma implementation "listbox.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
dd3c394a | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
0c589ad0 BM |
23 | #include "wx/window.h" |
24 | #include "wx/msw/private.h" | |
25 | ||
2bda0e17 | 26 | #ifndef WX_PRECOMP |
0c589ad0 BM |
27 | #include "wx/listbox.h" |
28 | #include "wx/settings.h" | |
29 | #include "wx/brush.h" | |
30 | #include "wx/font.h" | |
31 | #include "wx/dc.h" | |
2662e49e | 32 | #include "wx/utils.h" |
2bda0e17 KB |
33 | #endif |
34 | ||
5ea105e0 RR |
35 | #include <windowsx.h> |
36 | ||
37 | #ifdef __WXWINE__ | |
38 | #if defined(GetWindowStyle) | |
39 | #undef GetWindowStyle | |
40 | #endif | |
41 | #endif | |
42 | ||
0c589ad0 BM |
43 | #include "wx/dynarray.h" |
44 | #include "wx/log.h" | |
2bda0e17 | 45 | |
0c589ad0 BM |
46 | #if wxUSE_OWNER_DRAWN |
47 | #include "wx/ownerdrw.h" | |
48 | #endif | |
2bda0e17 | 49 | |
57c208c5 | 50 | #ifndef __TWIN32__ |
5ea105e0 | 51 | #if defined(__GNUWIN32__) |
65fd5cb0 | 52 | #ifndef wxUSE_NORLANDER_HEADERS |
3096bd2f | 53 | #include "wx/msw/gnuwin32/extra.h" |
65fd5cb0 | 54 | #endif |
dd3c394a | 55 | #endif |
57c208c5 | 56 | #endif |
2bda0e17 | 57 | |
5ea105e0 RR |
58 | #ifdef __WXWINE__ |
59 | #ifndef ListBox_SetItemData | |
60 | #define ListBox_SetItemData(hwndCtl, index, data) \ | |
61 | ((int)(DWORD)SendMessage((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data))) | |
62 | #endif | |
63 | #ifndef ListBox_GetHorizontalExtent | |
64 | #define ListBox_GetHorizontalExtent(hwndCtl) \ | |
65 | ((int)(DWORD)SendMessage((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L)) | |
66 | #endif | |
67 | #ifndef ListBox_GetSelCount | |
68 | #define ListBox_GetSelCount(hwndCtl) \ | |
69 | ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELCOUNT, 0L, 0L)) | |
70 | #endif | |
71 | #ifndef ListBox_GetSelItems | |
72 | #define ListBox_GetSelItems(hwndCtl, cItems, lpItems) \ | |
73 | ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems))) | |
74 | #endif | |
75 | #ifndef ListBox_GetTextLen | |
76 | #define ListBox_GetTextLen(hwndCtl, index) \ | |
77 | ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L)) | |
78 | #endif | |
79 | #ifndef ListBox_GetText | |
80 | #define ListBox_GetText(hwndCtl, index, lpszBuffer) \ | |
81 | ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer))) | |
82 | #endif | |
83 | #endif | |
14483330 | 84 | |
2bda0e17 | 85 | #if !USE_SHARED_LIBRARY |
dd3c394a | 86 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
2bda0e17 KB |
87 | #endif |
88 | ||
89 | // ============================================================================ | |
90 | // list box item declaration and implementation | |
91 | // ============================================================================ | |
92 | ||
47d67540 | 93 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
94 | |
95 | class wxListBoxItem : public wxOwnerDrawn | |
96 | { | |
97 | public: | |
dd3c394a | 98 | wxListBoxItem(const wxString& str = ""); |
2bda0e17 KB |
99 | }; |
100 | ||
dd3c394a | 101 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) |
2bda0e17 | 102 | { |
dd3c394a VZ |
103 | // no bitmaps/checkmarks |
104 | SetMarginWidth(0); | |
2bda0e17 KB |
105 | } |
106 | ||
c86f1403 | 107 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) |
2bda0e17 | 108 | { |
dd3c394a | 109 | return new wxListBoxItem(); |
2bda0e17 KB |
110 | } |
111 | ||
112 | #endif //USE_OWNER_DRAWN | |
113 | ||
114 | // ============================================================================ | |
115 | // list box control implementation | |
116 | // ============================================================================ | |
117 | ||
2ee3ee1b VZ |
118 | // ---------------------------------------------------------------------------- |
119 | // creation | |
120 | // ---------------------------------------------------------------------------- | |
dd3c394a | 121 | |
2ee3ee1b | 122 | // Listbox item |
bfc6fde4 | 123 | wxListBox::wxListBox() |
2bda0e17 | 124 | { |
dd3c394a VZ |
125 | m_noItems = 0; |
126 | m_selected = 0; | |
2bda0e17 KB |
127 | } |
128 | ||
dd3c394a VZ |
129 | bool wxListBox::Create(wxWindow *parent, |
130 | wxWindowID id, | |
2bda0e17 KB |
131 | const wxPoint& pos, |
132 | const wxSize& size, | |
debe6624 JS |
133 | int n, const wxString choices[], |
134 | long style, | |
2bda0e17 KB |
135 | const wxValidator& validator, |
136 | const wxString& name) | |
137 | { | |
dd3c394a VZ |
138 | m_noItems = 0; |
139 | m_hWnd = 0; | |
140 | m_selected = 0; | |
2bda0e17 | 141 | |
dd3c394a VZ |
142 | SetName(name); |
143 | SetValidator(validator); | |
2bda0e17 | 144 | |
dd3c394a VZ |
145 | if (parent) |
146 | parent->AddChild(this); | |
2bda0e17 | 147 | |
dd3c394a VZ |
148 | wxSystemSettings settings; |
149 | SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
150 | SetForegroundColour(parent->GetForegroundColour()); | |
2bda0e17 | 151 | |
dd3c394a | 152 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; |
2bda0e17 | 153 | |
dd3c394a VZ |
154 | int x = pos.x; |
155 | int y = pos.y; | |
156 | int width = size.x; | |
157 | int height = size.y; | |
158 | m_windowStyle = style; | |
2bda0e17 | 159 | |
42e69d6b VZ |
160 | DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | |
161 | LBS_NOTIFY | LBS_HASSTRINGS; | |
dd3c394a VZ |
162 | if (m_windowStyle & wxLB_MULTIPLE) |
163 | wstyle |= LBS_MULTIPLESEL; | |
164 | else if (m_windowStyle & wxLB_EXTENDED) | |
165 | wstyle |= LBS_EXTENDEDSEL; | |
2bda0e17 | 166 | |
dd3c394a | 167 | if (m_windowStyle & wxLB_ALWAYS_SB) |
2ee3ee1b | 168 | wstyle |= LBS_DISABLENOSCROLL; |
dd3c394a VZ |
169 | if (m_windowStyle & wxLB_HSCROLL) |
170 | wstyle |= WS_HSCROLL; | |
171 | if (m_windowStyle & wxLB_SORT) | |
172 | wstyle |= LBS_SORT; | |
2bda0e17 | 173 | |
47d67540 | 174 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 175 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a VZ |
176 | // we don't support LBS_OWNERDRAWVARIABLE yet |
177 | wstyle |= LBS_OWNERDRAWFIXED; | |
2bda0e17 | 178 | } |
2bda0e17 KB |
179 | #endif |
180 | ||
dd3c394a VZ |
181 | // Without this style, you get unexpected heights, so e.g. constraint layout |
182 | // doesn't work properly | |
183 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
184 | ||
185 | bool want3D; | |
2ee3ee1b | 186 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); |
2bda0e17 | 187 | |
2ee3ee1b VZ |
188 | // Even with extended styles, need to combine with WS_BORDER for them to |
189 | // look right. | |
dd3c394a VZ |
190 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
191 | { | |
192 | wstyle |= WS_BORDER; | |
193 | } | |
2bda0e17 | 194 | |
223d09f6 | 195 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL, |
dd3c394a VZ |
196 | wstyle | WS_CHILD, |
197 | 0, 0, 0, 0, | |
198 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
199 | wxGetInstance(), NULL); | |
1c089c47 | 200 | |
223d09f6 | 201 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") ); |
1c089c47 | 202 | |
1f112209 | 203 | #if wxUSE_CTL3D |
dd3c394a VZ |
204 | if (want3D) |
205 | { | |
a23fd0e1 | 206 | Ctl3dSubclassCtl(GetHwnd()); |
dd3c394a VZ |
207 | m_useCtl3D = TRUE; |
208 | } | |
2bda0e17 KB |
209 | #endif |
210 | ||
dd3c394a VZ |
211 | // Subclass again to catch messages |
212 | SubclassWin(m_hWnd); | |
2bda0e17 | 213 | |
dd3c394a VZ |
214 | size_t ui; |
215 | for (ui = 0; ui < (size_t)n; ui++) { | |
216 | Append(choices[ui]); | |
2bda0e17 | 217 | } |
2bda0e17 | 218 | |
dd3c394a | 219 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) |
a23fd0e1 | 220 | SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0); |
2bda0e17 | 221 | |
dd3c394a | 222 | SetFont(parent->GetFont()); |
2bda0e17 | 223 | |
dd3c394a | 224 | SetSize(x, y, width, height); |
2bda0e17 | 225 | |
dd3c394a | 226 | Show(TRUE); |
1c089c47 | 227 | |
dd3c394a | 228 | return TRUE; |
2bda0e17 KB |
229 | } |
230 | ||
bfc6fde4 | 231 | wxListBox::~wxListBox() |
2bda0e17 | 232 | { |
baccb514 | 233 | Free(); |
2bda0e17 KB |
234 | } |
235 | ||
bfc6fde4 | 236 | void wxListBox::SetupColours() |
2bda0e17 | 237 | { |
dd3c394a VZ |
238 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); |
239 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
2bda0e17 KB |
240 | } |
241 | ||
2ee3ee1b VZ |
242 | // ---------------------------------------------------------------------------- |
243 | // implementation of wxListBoxBase methods | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
246 | void wxListBox::DoSetFirstItem(int N) | |
2bda0e17 | 247 | { |
dd3c394a | 248 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 249 | wxT("invalid index in wxListBox::SetFirstItem") ); |
dd3c394a | 250 | |
2ee3ee1b | 251 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); |
2bda0e17 KB |
252 | } |
253 | ||
debe6624 | 254 | void wxListBox::Delete(int N) |
2bda0e17 | 255 | { |
dd3c394a | 256 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 257 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a | 258 | |
07cf98cb VZ |
259 | // for owner drawn objects, the data is used for storing wxOwnerDrawn |
260 | // pointers and we shouldn't touch it | |
261 | #if !wxUSE_OWNER_DRAWN | |
262 | if ( !(m_windowStyle & wxLB_OWNERDRAW) ) | |
263 | #endif // !wxUSE_OWNER_DRAWN | |
264 | if ( HasClientObjectData() ) | |
265 | { | |
266 | delete GetClientObject(N); | |
267 | } | |
6c8a980f | 268 | |
a23fd0e1 | 269 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); |
dd3c394a VZ |
270 | m_noItems--; |
271 | ||
272 | SetHorizontalExtent(""); | |
2bda0e17 KB |
273 | } |
274 | ||
2ee3ee1b | 275 | int wxListBox::DoAppend(const wxString& item) |
2bda0e17 | 276 | { |
a23fd0e1 | 277 | int index = ListBox_AddString(GetHwnd(), item); |
2ee3ee1b | 278 | m_noItems++; |
2bda0e17 | 279 | |
47d67540 | 280 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 281 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a VZ |
282 | wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument |
283 | pNewItem->SetName(item); | |
284 | m_aItems.Add(pNewItem); | |
a23fd0e1 | 285 | ListBox_SetItemData(GetHwnd(), index, pNewItem); |
60c65519 | 286 | pNewItem->SetFont(GetFont()); |
2bda0e17 | 287 | } |
1c089c47 | 288 | #endif |
2bda0e17 | 289 | |
dd3c394a | 290 | SetHorizontalExtent(item); |
dd3c394a | 291 | |
2ee3ee1b | 292 | return index; |
2bda0e17 KB |
293 | } |
294 | ||
2ee3ee1b | 295 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
2bda0e17 | 296 | { |
a23fd0e1 | 297 | ShowWindow(GetHwnd(), SW_HIDE); |
2ee3ee1b | 298 | |
a23fd0e1 | 299 | ListBox_ResetContent(GetHwnd()); |
2ee3ee1b VZ |
300 | |
301 | m_noItems = choices.GetCount(); | |
dd3c394a | 302 | int i; |
2ee3ee1b | 303 | for (i = 0; i < m_noItems; i++) |
dd3c394a | 304 | { |
a23fd0e1 | 305 | ListBox_AddString(GetHwnd(), choices[i]); |
dd3c394a | 306 | if ( clientData ) |
2ee3ee1b VZ |
307 | { |
308 | #if wxUSE_OWNER_DRAWN | |
d9a012ad | 309 | wxASSERT_MSG(clientData[i] == NULL, |
2ee3ee1b VZ |
310 | wxT("Can't use client data with owner-drawn listboxes")); |
311 | #else // !wxUSE_OWNER_DRAWN | |
a23fd0e1 | 312 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); |
2ee3ee1b VZ |
313 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN |
314 | } | |
dd3c394a | 315 | } |
2bda0e17 | 316 | |
47d67540 | 317 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 318 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a VZ |
319 | // first delete old items |
320 | size_t ui = m_aItems.Count(); | |
321 | while ( ui-- != 0 ) { | |
322 | delete m_aItems[ui]; | |
323 | } | |
324 | m_aItems.Empty(); | |
325 | ||
326 | // then create new ones | |
2ee3ee1b | 327 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { |
dd3c394a VZ |
328 | wxOwnerDrawn *pNewItem = CreateItem(ui); |
329 | pNewItem->SetName(choices[ui]); | |
330 | m_aItems.Add(pNewItem); | |
a23fd0e1 | 331 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); |
dd3c394a | 332 | } |
2bda0e17 | 333 | } |
2ee3ee1b VZ |
334 | #endif // wxUSE_OWNER_DRAWN |
335 | ||
336 | SetHorizontalExtent(); | |
2bda0e17 | 337 | |
a23fd0e1 | 338 | ShowWindow(GetHwnd(), SW_SHOW); |
2bda0e17 KB |
339 | } |
340 | ||
341 | int wxListBox::FindString(const wxString& s) const | |
342 | { | |
a23fd0e1 | 343 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); |
dd3c394a | 344 | if (pos == LB_ERR) |
2ee3ee1b | 345 | return wxNOT_FOUND; |
dd3c394a VZ |
346 | else |
347 | return pos; | |
2bda0e17 KB |
348 | } |
349 | ||
bfc6fde4 | 350 | void wxListBox::Clear() |
2bda0e17 | 351 | { |
baccb514 | 352 | Free(); |
8ee9d618 VZ |
353 | |
354 | ListBox_ResetContent(GetHwnd()); | |
355 | ||
356 | m_noItems = 0; | |
357 | SetHorizontalExtent(); | |
358 | } | |
359 | ||
360 | void wxListBox::Free() | |
2b273975 | 361 | { |
dd3c394a | 362 | #if wxUSE_OWNER_DRAWN |
185fa6bf VZ |
363 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
364 | { | |
365 | size_t uiCount = m_aItems.Count(); | |
366 | while ( uiCount-- != 0 ) { | |
367 | delete m_aItems[uiCount]; | |
368 | } | |
dd3c394a | 369 | |
185fa6bf VZ |
370 | m_aItems.Clear(); |
371 | } | |
372 | else | |
373 | #endif // wxUSE_OWNER_DRAWN | |
6c8a980f VZ |
374 | if ( HasClientObjectData() ) |
375 | { | |
376 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
377 | { | |
378 | delete GetClientObject(n); | |
379 | } | |
380 | } | |
2bda0e17 | 381 | } |
baccb514 | 382 | |
debe6624 | 383 | void wxListBox::SetSelection(int N, bool select) |
2bda0e17 | 384 | { |
dd3c394a | 385 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 386 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 387 | |
2ee3ee1b VZ |
388 | if ( HasMultipleSelection() ) |
389 | { | |
a23fd0e1 | 390 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
2ee3ee1b | 391 | } |
dd3c394a VZ |
392 | else |
393 | { | |
2ee3ee1b | 394 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 395 | } |
2bda0e17 KB |
396 | } |
397 | ||
2ee3ee1b | 398 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 399 | { |
dd3c394a | 400 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
223d09f6 | 401 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 402 | |
a23fd0e1 | 403 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; |
2bda0e17 KB |
404 | } |
405 | ||
6c8a980f | 406 | wxClientData* wxListBox::DoGetItemClientObject(int n) const |
2bda0e17 | 407 | { |
6c8a980f | 408 | return (wxClientData *)DoGetItemClientData(n); |
2bda0e17 KB |
409 | } |
410 | ||
6c8a980f | 411 | void *wxListBox::DoGetItemClientData(int n) const |
2bda0e17 | 412 | { |
2ee3ee1b | 413 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, |
223d09f6 | 414 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 415 | |
2ee3ee1b | 416 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
417 | } |
418 | ||
6c8a980f | 419 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) |
2bda0e17 | 420 | { |
6c8a980f | 421 | DoSetItemClientData(n, clientData); |
2ee3ee1b VZ |
422 | } |
423 | ||
6c8a980f | 424 | void wxListBox::DoSetItemClientData(int n, void *clientData) |
2ee3ee1b VZ |
425 | { |
426 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
223d09f6 | 427 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 428 | |
2ee3ee1b VZ |
429 | #if wxUSE_OWNER_DRAWN |
430 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
431 | { | |
432 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
433 | // in OnMeasure/OnDraw. | |
434 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
435 | } | |
436 | #endif // wxUSE_OWNER_DRAWN | |
437 | ||
438 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
223d09f6 | 439 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
2bda0e17 KB |
440 | } |
441 | ||
2ee3ee1b VZ |
442 | bool wxListBox::HasMultipleSelection() const |
443 | { | |
444 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
445 | } | |
446 | ||
2bda0e17 | 447 | // Return number of selections and an array of selected integers |
14483330 | 448 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 449 | { |
dd3c394a | 450 | aSelections.Empty(); |
14483330 | 451 | |
2ee3ee1b | 452 | if ( HasMultipleSelection() ) |
dd3c394a | 453 | { |
a23fd0e1 | 454 | int no_sel = ListBox_GetSelCount(GetHwnd()); |
dd3c394a VZ |
455 | if (no_sel != 0) { |
456 | int *selections = new int[no_sel]; | |
2ee3ee1b VZ |
457 | int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections); |
458 | ||
459 | wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed")); | |
14483330 | 460 | |
dd3c394a VZ |
461 | aSelections.Alloc(no_sel); |
462 | for ( int n = 0; n < no_sel; n++ ) | |
463 | aSelections.Add(selections[n]); | |
14483330 | 464 | |
dd3c394a VZ |
465 | delete [] selections; |
466 | } | |
14483330 | 467 | |
dd3c394a VZ |
468 | return no_sel; |
469 | } | |
470 | else // single-selection listbox | |
471 | { | |
a23fd0e1 | 472 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); |
14483330 | 473 | |
dd3c394a VZ |
474 | return 1; |
475 | } | |
2bda0e17 KB |
476 | } |
477 | ||
478 | // Get single selection, for single choice list items | |
14483330 | 479 | int wxListBox::GetSelection() const |
2bda0e17 | 480 | { |
2ee3ee1b | 481 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 482 | -1, |
223d09f6 | 483 | wxT("GetSelection() can't be used with multiple-selection " |
837e5743 | 484 | "listboxes, use GetSelections() instead.") ); |
14483330 | 485 | |
a23fd0e1 | 486 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
487 | } |
488 | ||
489 | // Find string for position | |
debe6624 | 490 | wxString wxListBox::GetString(int N) const |
2bda0e17 | 491 | { |
dd3c394a | 492 | wxCHECK_MSG( N >= 0 && N < m_noItems, "", |
223d09f6 | 493 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 494 | |
a23fd0e1 | 495 | int len = ListBox_GetTextLen(GetHwnd(), N); |
dd3c394a VZ |
496 | |
497 | // +1 for terminating NUL | |
498 | wxString result; | |
a23fd0e1 | 499 | ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1)); |
dd3c394a VZ |
500 | result.UngetWriteBuf(); |
501 | ||
502 | return result; | |
2bda0e17 KB |
503 | } |
504 | ||
2ee3ee1b VZ |
505 | void |
506 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
507 | { | |
508 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
509 | wxT("invalid index in wxListBox::InsertItems") ); | |
510 | ||
511 | int nItems = items.GetCount(); | |
512 | for ( int i = 0; i < nItems; i++ ) | |
513 | ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
514 | m_noItems += nItems; | |
515 | ||
516 | SetHorizontalExtent(); | |
517 | } | |
518 | ||
519 | void wxListBox::SetString(int N, const wxString& s) | |
520 | { | |
521 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
522 | wxT("invalid index in wxListBox::SetString") ); | |
523 | ||
524 | // remember the state of the item | |
525 | bool wasSelected = IsSelected(N); | |
526 | ||
527 | void *oldData = NULL; | |
528 | wxClientData *oldObjData = NULL; | |
529 | if ( m_clientDataItemsType == ClientData_Void ) | |
530 | oldData = GetClientData(N); | |
531 | else if ( m_clientDataItemsType == ClientData_Object ) | |
532 | oldObjData = GetClientObject(N); | |
533 | ||
534 | // delete and recreate it | |
535 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
536 | ||
537 | int newN = N; | |
538 | if ( N == m_noItems - 1 ) | |
539 | newN = -1; | |
540 | ||
541 | ListBox_InsertString(GetHwnd(), newN, s); | |
542 | ||
543 | // restore the client data | |
544 | if ( oldData ) | |
545 | SetClientData(N, oldData); | |
546 | else if ( oldObjData ) | |
547 | SetClientObject(N, oldObjData); | |
548 | ||
549 | // we may have lost the selection | |
550 | if ( wasSelected ) | |
551 | Select(N); | |
552 | ||
553 | #if wxUSE_OWNER_DRAWN | |
554 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
555 | // update item's text | |
556 | m_aItems[N]->SetName(s); | |
557 | #endif //USE_OWNER_DRAWN | |
558 | } | |
559 | ||
560 | int wxListBox::GetCount() const | |
561 | { | |
562 | return m_noItems; | |
563 | } | |
564 | ||
565 | // ---------------------------------------------------------------------------- | |
566 | // helpers | |
567 | // ---------------------------------------------------------------------------- | |
568 | ||
4438caf4 VZ |
569 | // Windows-specific code to set the horizontal extent of the listbox, if |
570 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. | |
2bda0e17 KB |
571 | // Otherwise, all strings are used. |
572 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
573 | { | |
dd3c394a VZ |
574 | // Only necessary if we want a horizontal scrollbar |
575 | if (!(m_windowStyle & wxHSCROLL)) | |
576 | return; | |
577 | TEXTMETRIC lpTextMetric; | |
578 | ||
2ee3ee1b | 579 | if ( !s.IsEmpty() ) |
2bda0e17 | 580 | { |
a23fd0e1 VZ |
581 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); |
582 | HDC dc = GetWindowDC(GetHwnd()); | |
dd3c394a VZ |
583 | HFONT oldFont = 0; |
584 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
585 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
586 | ||
587 | GetTextMetrics(dc, &lpTextMetric); | |
588 | SIZE extentXY; | |
837e5743 | 589 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); |
dd3c394a VZ |
590 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
591 | ||
592 | if (oldFont) | |
593 | ::SelectObject(dc, oldFont); | |
594 | ||
a23fd0e1 | 595 | ReleaseDC(GetHwnd(), dc); |
dd3c394a | 596 | if (extentX > existingExtent) |
a23fd0e1 | 597 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); |
dd3c394a VZ |
598 | } |
599 | else | |
600 | { | |
601 | int largestExtent = 0; | |
a23fd0e1 | 602 | HDC dc = GetWindowDC(GetHwnd()); |
dd3c394a VZ |
603 | HFONT oldFont = 0; |
604 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
605 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
606 | ||
607 | GetTextMetrics(dc, &lpTextMetric); | |
608 | int i; | |
609 | for (i = 0; i < m_noItems; i++) | |
610 | { | |
a23fd0e1 | 611 | int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer); |
dd3c394a VZ |
612 | wxBuffer[len] = 0; |
613 | SIZE extentXY; | |
837e5743 | 614 | ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY); |
dd3c394a VZ |
615 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
616 | if (extentX > largestExtent) | |
617 | largestExtent = extentX; | |
618 | } | |
619 | if (oldFont) | |
620 | ::SelectObject(dc, oldFont); | |
621 | ||
a23fd0e1 VZ |
622 | ReleaseDC(GetHwnd(), dc); |
623 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
2bda0e17 | 624 | } |
2bda0e17 KB |
625 | } |
626 | ||
f68586e5 | 627 | wxSize wxListBox::DoGetBestSize() const |
b908d224 VZ |
628 | { |
629 | // find the widest string | |
630 | int wLine; | |
631 | int wListbox = 0; | |
632 | for ( int i = 0; i < m_noItems; i++ ) | |
633 | { | |
634 | wxString str(GetString(i)); | |
635 | GetTextExtent(str, &wLine, NULL); | |
636 | if ( wLine > wListbox ) | |
637 | wListbox = wLine; | |
638 | } | |
639 | ||
640 | // give it some reasonable default value if there are no strings in the | |
641 | // list | |
642 | if ( wListbox == 0 ) | |
643 | wListbox = 100; | |
644 | ||
645 | // the listbox should be slightly larger than the widest string | |
646 | int cx, cy; | |
647 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
648 | ||
649 | wListbox += 3*cx; | |
650 | ||
651 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7)); | |
652 | ||
653 | return wxSize(wListbox, hListbox); | |
654 | } | |
655 | ||
2ee3ee1b VZ |
656 | // ---------------------------------------------------------------------------- |
657 | // callbacks | |
658 | // ---------------------------------------------------------------------------- | |
659 | ||
660 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 661 | { |
8ee9d618 | 662 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 663 | { |
2ee3ee1b | 664 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); |
8ee9d618 VZ |
665 | event.SetEventObject( this ); |
666 | ||
2ee3ee1b | 667 | wxArrayInt aSelections; |
8ee9d618 | 668 | int n, count = GetSelections(aSelections); |
2ee3ee1b VZ |
669 | if ( count > 0 ) |
670 | { | |
8ee9d618 VZ |
671 | n = aSelections[0]; |
672 | if ( HasClientObjectData() ) | |
673 | event.SetClientObject( GetClientObject(n) ); | |
674 | else if ( HasClientUntypedData() ) | |
675 | event.SetClientData( GetClientData(n) ); | |
676 | event.SetString( GetString(n) ); | |
2ee3ee1b VZ |
677 | } |
678 | else | |
679 | { | |
8ee9d618 | 680 | n = -1; |
2ee3ee1b VZ |
681 | } |
682 | ||
8ee9d618 VZ |
683 | event.m_commandInt = n; |
684 | ||
685 | return GetEventHandler()->ProcessEvent(event); | |
2bda0e17 | 686 | } |
8ee9d618 | 687 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b VZ |
688 | { |
689 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
690 | event.SetEventObject( this ); | |
8ee9d618 VZ |
691 | |
692 | return GetEventHandler()->ProcessEvent(event); | |
2ee3ee1b | 693 | } |
8ee9d618 | 694 | //else: |
2ee3ee1b VZ |
695 | |
696 | return FALSE; | |
2bda0e17 KB |
697 | } |
698 | ||
2ee3ee1b VZ |
699 | // ---------------------------------------------------------------------------- |
700 | // wxCheckListBox support | |
701 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 702 | |
47d67540 | 703 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
704 | |
705 | // drawing | |
706 | // ------- | |
707 | ||
708 | // space beneath/above each row in pixels | |
709 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
710 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
711 | ||
712 | // the height is the same for all items | |
dd3c394a VZ |
713 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
714 | ||
715 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
716 | // message is not yet sent when we get here! | |
2bda0e17 KB |
717 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
718 | { | |
dd3c394a VZ |
719 | // only owner-drawn control should receive this message |
720 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
2bda0e17 | 721 | |
dd3c394a | 722 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 723 | |
07cf98cb VZ |
724 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
725 | ||
dd3c394a | 726 | wxDC dc; |
07cf98cb | 727 | dc.SetHDC((WXHDC)hdc); |
dd3c394a | 728 | dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); |
2bda0e17 | 729 | |
dd3c394a VZ |
730 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
731 | pStruct->itemWidth = dc.GetCharWidth(); | |
2bda0e17 | 732 | |
07cf98cb VZ |
733 | dc.SetHDC(0); |
734 | ||
735 | DeleteDC(hdc); | |
736 | ||
dd3c394a | 737 | return TRUE; |
2bda0e17 KB |
738 | } |
739 | ||
740 | // forward the message to the appropriate item | |
741 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
742 | { | |
dd3c394a VZ |
743 | // only owner-drawn control should receive this message |
744 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
745 | ||
746 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
747 | ||
a23fd0e1 | 748 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); |
2bda0e17 | 749 | |
dd3c394a | 750 | wxCHECK( data && (data != LB_ERR), FALSE ); |
2bda0e17 | 751 | |
dd3c394a | 752 | wxListBoxItem *pItem = (wxListBoxItem *)data; |
2bda0e17 | 753 | |
dd3c394a VZ |
754 | wxDC dc; |
755 | dc.SetHDC((WXHDC)pStruct->hDC, FALSE); | |
756 | wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), | |
757 | wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); | |
2bda0e17 | 758 | |
dd3c394a VZ |
759 | return pItem->OnDrawItem(dc, rect, |
760 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
761 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
2bda0e17 KB |
762 | } |
763 | ||
764 | #endif | |
dd3c394a | 765 | // wxUSE_OWNER_DRAWN |