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