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