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