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