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