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