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