]>
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 | |
5615ab37 DW |
310 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
311 | { | |
312 | wxASSERT_MSG(clientData[i] == NULL, | |
313 | wxT("Can't use client data with owner-drawn listboxes")); | |
314 | } | |
315 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); | |
2ee3ee1b | 316 | #else // !wxUSE_OWNER_DRAWN |
a23fd0e1 | 317 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); |
2ee3ee1b VZ |
318 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN |
319 | } | |
dd3c394a | 320 | } |
2bda0e17 | 321 | |
47d67540 | 322 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 323 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a VZ |
324 | // first delete old items |
325 | size_t ui = m_aItems.Count(); | |
326 | while ( ui-- != 0 ) { | |
327 | delete m_aItems[ui]; | |
328 | } | |
329 | m_aItems.Empty(); | |
330 | ||
331 | // then create new ones | |
2ee3ee1b | 332 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { |
dd3c394a VZ |
333 | wxOwnerDrawn *pNewItem = CreateItem(ui); |
334 | pNewItem->SetName(choices[ui]); | |
335 | m_aItems.Add(pNewItem); | |
a23fd0e1 | 336 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); |
dd3c394a | 337 | } |
2bda0e17 | 338 | } |
2ee3ee1b VZ |
339 | #endif // wxUSE_OWNER_DRAWN |
340 | ||
341 | SetHorizontalExtent(); | |
2bda0e17 | 342 | |
f6bcfd97 BP |
343 | if ( hideAndShow ) |
344 | { | |
345 | // show the listbox back if we hid it | |
346 | ShowWindow(GetHwnd(), SW_SHOW); | |
347 | } | |
2bda0e17 KB |
348 | } |
349 | ||
350 | int wxListBox::FindString(const wxString& s) const | |
351 | { | |
a23fd0e1 | 352 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); |
dd3c394a | 353 | if (pos == LB_ERR) |
2ee3ee1b | 354 | return wxNOT_FOUND; |
dd3c394a VZ |
355 | else |
356 | return pos; | |
2bda0e17 KB |
357 | } |
358 | ||
bfc6fde4 | 359 | void wxListBox::Clear() |
2bda0e17 | 360 | { |
baccb514 | 361 | Free(); |
8ee9d618 VZ |
362 | |
363 | ListBox_ResetContent(GetHwnd()); | |
364 | ||
365 | m_noItems = 0; | |
366 | SetHorizontalExtent(); | |
367 | } | |
368 | ||
369 | void wxListBox::Free() | |
2b273975 | 370 | { |
dd3c394a | 371 | #if wxUSE_OWNER_DRAWN |
185fa6bf VZ |
372 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
373 | { | |
374 | size_t uiCount = m_aItems.Count(); | |
375 | while ( uiCount-- != 0 ) { | |
376 | delete m_aItems[uiCount]; | |
377 | } | |
dd3c394a | 378 | |
185fa6bf VZ |
379 | m_aItems.Clear(); |
380 | } | |
381 | else | |
382 | #endif // wxUSE_OWNER_DRAWN | |
6c8a980f VZ |
383 | if ( HasClientObjectData() ) |
384 | { | |
385 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
386 | { | |
387 | delete GetClientObject(n); | |
388 | } | |
389 | } | |
2bda0e17 | 390 | } |
baccb514 | 391 | |
debe6624 | 392 | void wxListBox::SetSelection(int N, bool select) |
2bda0e17 | 393 | { |
dd3c394a | 394 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 395 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 396 | |
2ee3ee1b VZ |
397 | if ( HasMultipleSelection() ) |
398 | { | |
a23fd0e1 | 399 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
2ee3ee1b | 400 | } |
dd3c394a VZ |
401 | else |
402 | { | |
2ee3ee1b | 403 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 404 | } |
2bda0e17 KB |
405 | } |
406 | ||
2ee3ee1b | 407 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 408 | { |
dd3c394a | 409 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
223d09f6 | 410 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 411 | |
a23fd0e1 | 412 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; |
2bda0e17 KB |
413 | } |
414 | ||
6c8a980f | 415 | wxClientData* wxListBox::DoGetItemClientObject(int n) const |
2bda0e17 | 416 | { |
6c8a980f | 417 | return (wxClientData *)DoGetItemClientData(n); |
2bda0e17 KB |
418 | } |
419 | ||
6c8a980f | 420 | void *wxListBox::DoGetItemClientData(int n) const |
2bda0e17 | 421 | { |
2ee3ee1b | 422 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, |
223d09f6 | 423 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 424 | |
2ee3ee1b | 425 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
426 | } |
427 | ||
6c8a980f | 428 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) |
2bda0e17 | 429 | { |
6c8a980f | 430 | DoSetItemClientData(n, clientData); |
2ee3ee1b VZ |
431 | } |
432 | ||
6c8a980f | 433 | void wxListBox::DoSetItemClientData(int n, void *clientData) |
2ee3ee1b VZ |
434 | { |
435 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
223d09f6 | 436 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 437 | |
2ee3ee1b VZ |
438 | #if wxUSE_OWNER_DRAWN |
439 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
440 | { | |
441 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
442 | // in OnMeasure/OnDraw. | |
443 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
444 | } | |
445 | #endif // wxUSE_OWNER_DRAWN | |
446 | ||
447 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
223d09f6 | 448 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
2bda0e17 KB |
449 | } |
450 | ||
2ee3ee1b VZ |
451 | bool wxListBox::HasMultipleSelection() const |
452 | { | |
453 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
454 | } | |
455 | ||
2bda0e17 | 456 | // Return number of selections and an array of selected integers |
14483330 | 457 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 458 | { |
dd3c394a | 459 | aSelections.Empty(); |
14483330 | 460 | |
2ee3ee1b | 461 | if ( HasMultipleSelection() ) |
dd3c394a | 462 | { |
a23fd0e1 | 463 | int no_sel = ListBox_GetSelCount(GetHwnd()); |
dd3c394a VZ |
464 | if (no_sel != 0) { |
465 | int *selections = new int[no_sel]; | |
2ee3ee1b VZ |
466 | int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections); |
467 | ||
468 | wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed")); | |
14483330 | 469 | |
dd3c394a VZ |
470 | aSelections.Alloc(no_sel); |
471 | for ( int n = 0; n < no_sel; n++ ) | |
472 | aSelections.Add(selections[n]); | |
14483330 | 473 | |
dd3c394a VZ |
474 | delete [] selections; |
475 | } | |
14483330 | 476 | |
dd3c394a VZ |
477 | return no_sel; |
478 | } | |
479 | else // single-selection listbox | |
480 | { | |
f6bcfd97 BP |
481 | if (ListBox_GetCurSel(GetHwnd()) > -1) |
482 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
14483330 | 483 | |
f6bcfd97 | 484 | return aSelections.Count(); |
dd3c394a | 485 | } |
2bda0e17 KB |
486 | } |
487 | ||
488 | // Get single selection, for single choice list items | |
14483330 | 489 | int wxListBox::GetSelection() const |
2bda0e17 | 490 | { |
2ee3ee1b | 491 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 492 | -1, |
f6bcfd97 | 493 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); |
14483330 | 494 | |
a23fd0e1 | 495 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
496 | } |
497 | ||
498 | // Find string for position | |
debe6624 | 499 | wxString wxListBox::GetString(int N) const |
2bda0e17 | 500 | { |
dd3c394a | 501 | wxCHECK_MSG( N >= 0 && N < m_noItems, "", |
223d09f6 | 502 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 503 | |
a23fd0e1 | 504 | int len = ListBox_GetTextLen(GetHwnd(), N); |
dd3c394a VZ |
505 | |
506 | // +1 for terminating NUL | |
507 | wxString result; | |
a23fd0e1 | 508 | ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1)); |
dd3c394a VZ |
509 | result.UngetWriteBuf(); |
510 | ||
511 | return result; | |
2bda0e17 KB |
512 | } |
513 | ||
2ee3ee1b VZ |
514 | void |
515 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
516 | { | |
517 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
518 | wxT("invalid index in wxListBox::InsertItems") ); | |
519 | ||
520 | int nItems = items.GetCount(); | |
521 | for ( int i = 0; i < nItems; i++ ) | |
522 | ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
523 | m_noItems += nItems; | |
524 | ||
525 | SetHorizontalExtent(); | |
526 | } | |
527 | ||
528 | void wxListBox::SetString(int N, const wxString& s) | |
529 | { | |
530 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
531 | wxT("invalid index in wxListBox::SetString") ); | |
532 | ||
533 | // remember the state of the item | |
534 | bool wasSelected = IsSelected(N); | |
535 | ||
536 | void *oldData = NULL; | |
537 | wxClientData *oldObjData = NULL; | |
538 | if ( m_clientDataItemsType == ClientData_Void ) | |
539 | oldData = GetClientData(N); | |
540 | else if ( m_clientDataItemsType == ClientData_Object ) | |
541 | oldObjData = GetClientObject(N); | |
542 | ||
543 | // delete and recreate it | |
544 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
545 | ||
546 | int newN = N; | |
547 | if ( N == m_noItems - 1 ) | |
548 | newN = -1; | |
549 | ||
550 | ListBox_InsertString(GetHwnd(), newN, s); | |
551 | ||
552 | // restore the client data | |
553 | if ( oldData ) | |
554 | SetClientData(N, oldData); | |
555 | else if ( oldObjData ) | |
556 | SetClientObject(N, oldObjData); | |
557 | ||
558 | // we may have lost the selection | |
559 | if ( wasSelected ) | |
560 | Select(N); | |
561 | ||
562 | #if wxUSE_OWNER_DRAWN | |
563 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
564 | // update item's text | |
565 | m_aItems[N]->SetName(s); | |
566 | #endif //USE_OWNER_DRAWN | |
567 | } | |
568 | ||
569 | int wxListBox::GetCount() const | |
570 | { | |
571 | return m_noItems; | |
572 | } | |
573 | ||
574 | // ---------------------------------------------------------------------------- | |
575 | // helpers | |
576 | // ---------------------------------------------------------------------------- | |
577 | ||
4438caf4 VZ |
578 | // Windows-specific code to set the horizontal extent of the listbox, if |
579 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. | |
2bda0e17 KB |
580 | // Otherwise, all strings are used. |
581 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
582 | { | |
dd3c394a VZ |
583 | // Only necessary if we want a horizontal scrollbar |
584 | if (!(m_windowStyle & wxHSCROLL)) | |
585 | return; | |
586 | TEXTMETRIC lpTextMetric; | |
587 | ||
2ee3ee1b | 588 | if ( !s.IsEmpty() ) |
2bda0e17 | 589 | { |
a23fd0e1 VZ |
590 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); |
591 | HDC dc = GetWindowDC(GetHwnd()); | |
dd3c394a VZ |
592 | HFONT oldFont = 0; |
593 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
594 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
595 | ||
596 | GetTextMetrics(dc, &lpTextMetric); | |
597 | SIZE extentXY; | |
837e5743 | 598 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); |
dd3c394a VZ |
599 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
600 | ||
601 | if (oldFont) | |
602 | ::SelectObject(dc, oldFont); | |
603 | ||
a23fd0e1 | 604 | ReleaseDC(GetHwnd(), dc); |
dd3c394a | 605 | if (extentX > existingExtent) |
a23fd0e1 | 606 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); |
dd3c394a VZ |
607 | } |
608 | else | |
609 | { | |
610 | int largestExtent = 0; | |
a23fd0e1 | 611 | HDC dc = GetWindowDC(GetHwnd()); |
dd3c394a VZ |
612 | HFONT oldFont = 0; |
613 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
614 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
615 | ||
616 | GetTextMetrics(dc, &lpTextMetric); | |
617 | int i; | |
618 | for (i = 0; i < m_noItems; i++) | |
619 | { | |
a23fd0e1 | 620 | int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer); |
dd3c394a VZ |
621 | wxBuffer[len] = 0; |
622 | SIZE extentXY; | |
837e5743 | 623 | ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY); |
dd3c394a VZ |
624 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
625 | if (extentX > largestExtent) | |
626 | largestExtent = extentX; | |
627 | } | |
628 | if (oldFont) | |
629 | ::SelectObject(dc, oldFont); | |
630 | ||
a23fd0e1 VZ |
631 | ReleaseDC(GetHwnd(), dc); |
632 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
2bda0e17 | 633 | } |
2bda0e17 KB |
634 | } |
635 | ||
f68586e5 | 636 | wxSize wxListBox::DoGetBestSize() const |
b908d224 VZ |
637 | { |
638 | // find the widest string | |
639 | int wLine; | |
640 | int wListbox = 0; | |
641 | for ( int i = 0; i < m_noItems; i++ ) | |
642 | { | |
643 | wxString str(GetString(i)); | |
644 | GetTextExtent(str, &wLine, NULL); | |
645 | if ( wLine > wListbox ) | |
646 | wListbox = wLine; | |
647 | } | |
648 | ||
649 | // give it some reasonable default value if there are no strings in the | |
650 | // list | |
651 | if ( wListbox == 0 ) | |
652 | wListbox = 100; | |
653 | ||
654 | // the listbox should be slightly larger than the widest string | |
655 | int cx, cy; | |
656 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
657 | ||
658 | wListbox += 3*cx; | |
659 | ||
660 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7)); | |
661 | ||
662 | return wxSize(wListbox, hListbox); | |
663 | } | |
664 | ||
2ee3ee1b VZ |
665 | // ---------------------------------------------------------------------------- |
666 | // callbacks | |
667 | // ---------------------------------------------------------------------------- | |
668 | ||
669 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 670 | { |
bada28f0 | 671 | wxEventType evtType; |
8ee9d618 | 672 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 673 | { |
bada28f0 | 674 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; |
2bda0e17 | 675 | } |
8ee9d618 | 676 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b | 677 | { |
bada28f0 VZ |
678 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
679 | } | |
680 | else | |
681 | { | |
682 | // some event we're not interested in | |
683 | return FALSE; | |
684 | } | |
685 | ||
686 | wxCommandEvent event(evtType, m_windowId); | |
687 | event.SetEventObject( this ); | |
8ee9d618 | 688 | |
bada28f0 VZ |
689 | wxArrayInt aSelections; |
690 | int n, count = GetSelections(aSelections); | |
691 | if ( count > 0 ) | |
692 | { | |
693 | n = aSelections[0]; | |
694 | if ( HasClientObjectData() ) | |
695 | event.SetClientObject( GetClientObject(n) ); | |
696 | else if ( HasClientUntypedData() ) | |
697 | event.SetClientData( GetClientData(n) ); | |
698 | event.SetString( GetString(n) ); | |
2ee3ee1b | 699 | } |
bada28f0 VZ |
700 | else |
701 | { | |
702 | n = -1; | |
703 | } | |
704 | ||
705 | event.m_commandInt = n; | |
2ee3ee1b | 706 | |
bada28f0 | 707 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
708 | } |
709 | ||
2ee3ee1b VZ |
710 | // ---------------------------------------------------------------------------- |
711 | // wxCheckListBox support | |
712 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 713 | |
47d67540 | 714 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
715 | |
716 | // drawing | |
717 | // ------- | |
718 | ||
719 | // space beneath/above each row in pixels | |
720 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
721 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
722 | ||
723 | // the height is the same for all items | |
dd3c394a VZ |
724 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
725 | ||
726 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
727 | // message is not yet sent when we get here! | |
2bda0e17 KB |
728 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
729 | { | |
dd3c394a VZ |
730 | // only owner-drawn control should receive this message |
731 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
2bda0e17 | 732 | |
dd3c394a | 733 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 734 | |
07cf98cb VZ |
735 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
736 | ||
dd3c394a | 737 | wxDC dc; |
07cf98cb | 738 | dc.SetHDC((WXHDC)hdc); |
dd3c394a | 739 | dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); |
2bda0e17 | 740 | |
dd3c394a VZ |
741 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
742 | pStruct->itemWidth = dc.GetCharWidth(); | |
2bda0e17 | 743 | |
07cf98cb VZ |
744 | dc.SetHDC(0); |
745 | ||
746 | DeleteDC(hdc); | |
747 | ||
dd3c394a | 748 | return TRUE; |
2bda0e17 KB |
749 | } |
750 | ||
751 | // forward the message to the appropriate item | |
752 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
753 | { | |
dd3c394a VZ |
754 | // only owner-drawn control should receive this message |
755 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
756 | ||
757 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
f6bcfd97 BP |
758 | UINT itemID = pStruct->itemID; |
759 | ||
760 | // the item may be -1 for an empty listbox | |
761 | if ( itemID == (UINT)-1 ) | |
762 | return FALSE; | |
dd3c394a | 763 | |
a23fd0e1 | 764 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); |
2bda0e17 | 765 | |
dd3c394a | 766 | wxCHECK( data && (data != LB_ERR), FALSE ); |
2bda0e17 | 767 | |
dd3c394a | 768 | wxListBoxItem *pItem = (wxListBoxItem *)data; |
2bda0e17 | 769 | |
dd3c394a VZ |
770 | wxDC dc; |
771 | dc.SetHDC((WXHDC)pStruct->hDC, FALSE); | |
772 | wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), | |
f6bcfd97 | 773 | wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); |
2bda0e17 | 774 | |
dd3c394a VZ |
775 | return pItem->OnDrawItem(dc, rect, |
776 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
777 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
2bda0e17 KB |
778 | } |
779 | ||
780 | #endif | |
dd3c394a | 781 | // wxUSE_OWNER_DRAWN |