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