]>
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 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_LISTBOX | |
20 | ||
21 | #include "wx/listbox.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/dynarray.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/brush.h" | |
27 | #include "wx/font.h" | |
28 | #include "wx/dc.h" | |
29 | #include "wx/utils.h" | |
30 | #include "wx/log.h" | |
31 | #include "wx/window.h" | |
32 | #endif | |
33 | ||
34 | #include "wx/msw/private.h" | |
35 | #include "wx/msw/dc.h" | |
36 | ||
37 | #include <windowsx.h> | |
38 | ||
39 | #if wxUSE_OWNER_DRAWN | |
40 | #include "wx/ownerdrw.h" | |
41 | #endif | |
42 | ||
43 | #if wxUSE_EXTENDED_RTTI | |
44 | WX_DEFINE_FLAGS( wxListBoxStyle ) | |
45 | ||
46 | wxBEGIN_FLAGS( wxListBoxStyle ) | |
47 | // new style border flags, we put them first to | |
48 | // use them for streaming out | |
49 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
50 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
51 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
52 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
53 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
54 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
55 | ||
56 | // old style border flags | |
57 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
58 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
59 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
60 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
61 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
62 | wxFLAGS_MEMBER(wxBORDER) | |
63 | ||
64 | // standard window styles | |
65 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
66 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
67 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
68 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
69 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
70 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
71 | wxFLAGS_MEMBER(wxVSCROLL) | |
72 | wxFLAGS_MEMBER(wxHSCROLL) | |
73 | ||
74 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
75 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
76 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
77 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
78 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
79 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
80 | wxFLAGS_MEMBER(wxLB_SORT) | |
81 | ||
82 | wxEND_FLAGS( wxListBoxStyle ) | |
83 | ||
84 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems,"wx/listbox.h") | |
85 | ||
86 | wxBEGIN_PROPERTIES_TABLE(wxListBox) | |
87 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent ) | |
88 | wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent ) | |
89 | ||
90 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
91 | wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
92 | wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
93 | wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
94 | wxEND_PROPERTIES_TABLE() | |
95 | ||
96 | wxBEGIN_HANDLERS_TABLE(wxListBox) | |
97 | wxEND_HANDLERS_TABLE() | |
98 | ||
99 | wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) | |
100 | #else | |
101 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) | |
102 | #endif | |
103 | ||
104 | /* | |
105 | TODO PROPERTIES | |
106 | selection | |
107 | content | |
108 | item | |
109 | */ | |
110 | ||
111 | // ============================================================================ | |
112 | // list box item declaration and implementation | |
113 | // ============================================================================ | |
114 | ||
115 | #if wxUSE_OWNER_DRAWN | |
116 | ||
117 | class wxListBoxItem : public wxOwnerDrawn | |
118 | { | |
119 | public: | |
120 | wxListBoxItem(const wxString& str = wxEmptyString); | |
121 | }; | |
122 | ||
123 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false) | |
124 | { | |
125 | // no bitmaps/checkmarks | |
126 | SetMarginWidth(0); | |
127 | } | |
128 | ||
129 | wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) | |
130 | { | |
131 | return new wxListBoxItem(); | |
132 | } | |
133 | ||
134 | #endif //USE_OWNER_DRAWN | |
135 | ||
136 | // ============================================================================ | |
137 | // list box control implementation | |
138 | // ============================================================================ | |
139 | ||
140 | // ---------------------------------------------------------------------------- | |
141 | // creation | |
142 | // ---------------------------------------------------------------------------- | |
143 | ||
144 | // Listbox item | |
145 | wxListBox::wxListBox() | |
146 | { | |
147 | m_noItems = 0; | |
148 | m_updateHorizontalExtent = false; | |
149 | } | |
150 | ||
151 | bool wxListBox::Create(wxWindow *parent, | |
152 | wxWindowID id, | |
153 | const wxPoint& pos, | |
154 | const wxSize& size, | |
155 | int n, const wxString choices[], | |
156 | long style, | |
157 | const wxValidator& validator, | |
158 | const wxString& name) | |
159 | { | |
160 | m_noItems = 0; | |
161 | m_updateHorizontalExtent = false; | |
162 | ||
163 | // initialize base class fields | |
164 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
165 | return false; | |
166 | ||
167 | // create the native control | |
168 | if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) ) | |
169 | { | |
170 | // control creation failed | |
171 | return false; | |
172 | } | |
173 | ||
174 | // initialize the contents | |
175 | for ( int i = 0; i < n; i++ ) | |
176 | { | |
177 | Append(choices[i]); | |
178 | } | |
179 | ||
180 | // now we can compute our best size correctly, so do it again | |
181 | InvalidateBestSize(); | |
182 | SetInitialSize(size); | |
183 | ||
184 | return true; | |
185 | } | |
186 | ||
187 | bool wxListBox::Create(wxWindow *parent, | |
188 | wxWindowID id, | |
189 | const wxPoint& pos, | |
190 | const wxSize& size, | |
191 | const wxArrayString& choices, | |
192 | long style, | |
193 | const wxValidator& validator, | |
194 | const wxString& name) | |
195 | { | |
196 | wxCArrayString chs(choices); | |
197 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
198 | style, validator, name); | |
199 | } | |
200 | ||
201 | wxListBox::~wxListBox() | |
202 | { | |
203 | Clear(); | |
204 | } | |
205 | ||
206 | WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const | |
207 | { | |
208 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
209 | ||
210 | // always show the vertical scrollbar if necessary -- otherwise it is | |
211 | // impossible to use the control with the mouse | |
212 | msStyle |= WS_VSCROLL; | |
213 | ||
214 | // we always want to get the notifications | |
215 | msStyle |= LBS_NOTIFY; | |
216 | ||
217 | // without this style, you get unexpected heights, so e.g. constraint | |
218 | // layout doesn't work properly | |
219 | msStyle |= LBS_NOINTEGRALHEIGHT; | |
220 | ||
221 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
222 | _T("only one of listbox selection modes can be specified") ); | |
223 | ||
224 | if ( style & wxLB_MULTIPLE ) | |
225 | msStyle |= LBS_MULTIPLESEL; | |
226 | else if ( style & wxLB_EXTENDED ) | |
227 | msStyle |= LBS_EXTENDEDSEL; | |
228 | ||
229 | if ( m_windowStyle & wxLB_ALWAYS_SB ) | |
230 | msStyle |= LBS_DISABLENOSCROLL; | |
231 | if ( m_windowStyle & wxLB_HSCROLL ) | |
232 | msStyle |= WS_HSCROLL; | |
233 | if ( m_windowStyle & wxLB_SORT ) | |
234 | msStyle |= LBS_SORT; | |
235 | ||
236 | #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__) | |
237 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
238 | { | |
239 | // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put | |
240 | // the strings in the listbox for simplicity even though we could have | |
241 | // avoided it in this case | |
242 | msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS; | |
243 | } | |
244 | #endif // wxUSE_OWNER_DRAWN | |
245 | ||
246 | return msStyle; | |
247 | } | |
248 | ||
249 | void wxListBox::OnInternalIdle() | |
250 | { | |
251 | wxWindow::OnInternalIdle(); | |
252 | ||
253 | if (m_updateHorizontalExtent) | |
254 | { | |
255 | SetHorizontalExtent(wxEmptyString); | |
256 | m_updateHorizontalExtent = false; | |
257 | } | |
258 | } | |
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // implementation of wxListBoxBase methods | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | void wxListBox::DoSetFirstItem(int N) | |
265 | { | |
266 | wxCHECK_RET( IsValid(N), | |
267 | wxT("invalid index in wxListBox::SetFirstItem") ); | |
268 | ||
269 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); | |
270 | } | |
271 | ||
272 | void wxListBox::DoDeleteOneItem(unsigned int n) | |
273 | { | |
274 | wxCHECK_RET( IsValid(n), | |
275 | wxT("invalid index in wxListBox::Delete") ); | |
276 | ||
277 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); | |
278 | m_noItems--; | |
279 | ||
280 | // SetHorizontalExtent(wxEmptyString); can be slow | |
281 | m_updateHorizontalExtent = true; | |
282 | ||
283 | UpdateOldSelections(); | |
284 | } | |
285 | ||
286 | int wxListBox::FindString(const wxString& s, bool bCase) const | |
287 | { | |
288 | // back to base class search for not native search type | |
289 | if (bCase) | |
290 | return wxItemContainerImmutable::FindString( s, bCase ); | |
291 | ||
292 | int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str()); | |
293 | if (pos == LB_ERR) | |
294 | return wxNOT_FOUND; | |
295 | else | |
296 | return pos; | |
297 | } | |
298 | ||
299 | void wxListBox::DoClear() | |
300 | { | |
301 | Free(); | |
302 | ||
303 | ListBox_ResetContent(GetHwnd()); | |
304 | ||
305 | m_noItems = 0; | |
306 | m_updateHorizontalExtent = true; | |
307 | ||
308 | UpdateOldSelections(); | |
309 | } | |
310 | ||
311 | void wxListBox::Free() | |
312 | { | |
313 | #if wxUSE_OWNER_DRAWN | |
314 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
315 | { | |
316 | WX_CLEAR_ARRAY(m_aItems); | |
317 | } | |
318 | #endif // wxUSE_OWNER_DRAWN | |
319 | } | |
320 | ||
321 | void wxListBox::DoSetSelection(int N, bool select) | |
322 | { | |
323 | wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), | |
324 | wxT("invalid index in wxListBox::SetSelection") ); | |
325 | ||
326 | if ( HasMultipleSelection() ) | |
327 | { | |
328 | SendMessage(GetHwnd(), LB_SETSEL, select, N); | |
329 | } | |
330 | else | |
331 | { | |
332 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); | |
333 | } | |
334 | ||
335 | UpdateOldSelections(); | |
336 | } | |
337 | ||
338 | bool wxListBox::IsSelected(int N) const | |
339 | { | |
340 | wxCHECK_MSG( IsValid(N), false, | |
341 | wxT("invalid index in wxListBox::Selected") ); | |
342 | ||
343 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true; | |
344 | } | |
345 | ||
346 | void *wxListBox::DoGetItemClientData(unsigned int n) const | |
347 | { | |
348 | wxCHECK_MSG( IsValid(n), NULL, | |
349 | wxT("invalid index in wxListBox::GetClientData") ); | |
350 | ||
351 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); | |
352 | } | |
353 | ||
354 | void wxListBox::DoSetItemClientData(unsigned int n, void *clientData) | |
355 | { | |
356 | wxCHECK_RET( IsValid(n), | |
357 | wxT("invalid index in wxListBox::SetClientData") ); | |
358 | ||
359 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
360 | wxLogDebug(wxT("LB_SETITEMDATA failed")); | |
361 | } | |
362 | ||
363 | // Return number of selections and an array of selected integers | |
364 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
365 | { | |
366 | aSelections.Empty(); | |
367 | ||
368 | if ( HasMultipleSelection() ) | |
369 | { | |
370 | int countSel = ListBox_GetSelCount(GetHwnd()); | |
371 | if ( countSel == LB_ERR ) | |
372 | { | |
373 | wxLogDebug(_T("ListBox_GetSelCount failed")); | |
374 | } | |
375 | else if ( countSel != 0 ) | |
376 | { | |
377 | int *selections = new int[countSel]; | |
378 | ||
379 | if ( ListBox_GetSelItems(GetHwnd(), | |
380 | countSel, selections) == LB_ERR ) | |
381 | { | |
382 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
383 | countSel = -1; | |
384 | } | |
385 | else | |
386 | { | |
387 | aSelections.Alloc(countSel); | |
388 | for ( int n = 0; n < countSel; n++ ) | |
389 | aSelections.Add(selections[n]); | |
390 | } | |
391 | ||
392 | delete [] selections; | |
393 | } | |
394 | ||
395 | return countSel; | |
396 | } | |
397 | else // single-selection listbox | |
398 | { | |
399 | if (ListBox_GetCurSel(GetHwnd()) > -1) | |
400 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
401 | ||
402 | return aSelections.Count(); | |
403 | } | |
404 | } | |
405 | ||
406 | // Get single selection, for single choice list items | |
407 | int wxListBox::GetSelection() const | |
408 | { | |
409 | wxCHECK_MSG( !HasMultipleSelection(), | |
410 | -1, | |
411 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); | |
412 | ||
413 | return ListBox_GetCurSel(GetHwnd()); | |
414 | } | |
415 | ||
416 | // Find string for position | |
417 | wxString wxListBox::GetString(unsigned int n) const | |
418 | { | |
419 | wxCHECK_MSG( IsValid(n), wxEmptyString, | |
420 | wxT("invalid index in wxListBox::GetString") ); | |
421 | ||
422 | int len = ListBox_GetTextLen(GetHwnd(), n); | |
423 | ||
424 | // +1 for terminating NUL | |
425 | wxString result; | |
426 | ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1)); | |
427 | ||
428 | return result; | |
429 | } | |
430 | ||
431 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
432 | unsigned int pos, | |
433 | void **clientData, | |
434 | wxClientDataType type) | |
435 | { | |
436 | MSWAllocStorage(items, LB_INITSTORAGE); | |
437 | ||
438 | const bool append = pos == GetCount(); | |
439 | ||
440 | // we must use CB_ADDSTRING when appending as only it works correctly for | |
441 | // the sorted controls | |
442 | const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING; | |
443 | ||
444 | if ( append ) | |
445 | pos = 0; | |
446 | ||
447 | int n = wxNOT_FOUND; | |
448 | ||
449 | const unsigned int numItems = items.GetCount(); | |
450 | for ( unsigned int i = 0; i < numItems; i++ ) | |
451 | { | |
452 | n = MSWInsertOrAppendItem(pos, items[i], msg); | |
453 | if ( n == wxNOT_FOUND ) | |
454 | return n; | |
455 | ||
456 | if ( !append ) | |
457 | pos++; | |
458 | ||
459 | ++m_noItems; | |
460 | ||
461 | #if wxUSE_OWNER_DRAWN | |
462 | if ( HasFlag(wxLB_OWNERDRAW) ) | |
463 | { | |
464 | wxOwnerDrawn *pNewItem = CreateLboxItem(n); | |
465 | pNewItem->SetName(items[i]); | |
466 | pNewItem->SetFont(GetFont()); | |
467 | m_aItems.Insert(pNewItem, n); | |
468 | } | |
469 | #endif // wxUSE_OWNER_DRAWN | |
470 | AssignNewItemClientData(n, clientData, i, type); | |
471 | } | |
472 | ||
473 | m_updateHorizontalExtent = true; | |
474 | ||
475 | UpdateOldSelections(); | |
476 | ||
477 | return n; | |
478 | } | |
479 | ||
480 | int wxListBox::DoListHitTest(const wxPoint& point) const | |
481 | { | |
482 | LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT, | |
483 | 0L, MAKELONG(point.x, point.y)); | |
484 | ||
485 | // non zero high-order word means that this item is outside of the client | |
486 | // area, IOW the point is outside of the listbox | |
487 | return HIWORD(lRes) ? wxNOT_FOUND : lRes; | |
488 | } | |
489 | ||
490 | void wxListBox::SetString(unsigned int n, const wxString& s) | |
491 | { | |
492 | wxCHECK_RET( IsValid(n), | |
493 | wxT("invalid index in wxListBox::SetString") ); | |
494 | ||
495 | // remember the state of the item | |
496 | bool wasSelected = IsSelected(n); | |
497 | ||
498 | void *oldData = NULL; | |
499 | wxClientData *oldObjData = NULL; | |
500 | if ( HasClientUntypedData() ) | |
501 | oldData = GetClientData(n); | |
502 | else if ( HasClientObjectData() ) | |
503 | oldObjData = GetClientObject(n); | |
504 | ||
505 | // delete and recreate it | |
506 | SendMessage(GetHwnd(), LB_DELETESTRING, n, 0); | |
507 | ||
508 | int newN = n; | |
509 | if ( n == (m_noItems - 1) ) | |
510 | newN = -1; | |
511 | ||
512 | ListBox_InsertString(GetHwnd(), newN, s.wx_str()); | |
513 | ||
514 | // restore the client data | |
515 | if ( oldData ) | |
516 | SetClientData(n, oldData); | |
517 | else if ( oldObjData ) | |
518 | SetClientObject(n, oldObjData); | |
519 | ||
520 | #if wxUSE_OWNER_DRAWN | |
521 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
522 | { | |
523 | // update item's text | |
524 | m_aItems[n]->SetName(s); | |
525 | } | |
526 | #endif //USE_OWNER_DRAWN | |
527 | ||
528 | // we may have lost the selection | |
529 | if ( wasSelected ) | |
530 | Select(n); | |
531 | ||
532 | m_updateHorizontalExtent = true; | |
533 | } | |
534 | ||
535 | unsigned int wxListBox::GetCount() const | |
536 | { | |
537 | return m_noItems; | |
538 | } | |
539 | ||
540 | // ---------------------------------------------------------------------------- | |
541 | // size-related stuff | |
542 | // ---------------------------------------------------------------------------- | |
543 | ||
544 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
545 | { | |
546 | // in any case, our best size could have changed | |
547 | InvalidateBestSize(); | |
548 | ||
549 | // the rest is only necessary if we want a horizontal scrollbar | |
550 | if ( !HasFlag(wxHSCROLL) ) | |
551 | return; | |
552 | ||
553 | ||
554 | WindowHDC dc(GetHwnd()); | |
555 | SelectInHDC selFont(dc, GetHfontOf(GetFont())); | |
556 | ||
557 | TEXTMETRIC lpTextMetric; | |
558 | ::GetTextMetrics(dc, &lpTextMetric); | |
559 | ||
560 | int largestExtent = 0; | |
561 | SIZE extentXY; | |
562 | ||
563 | if ( s.empty() ) | |
564 | { | |
565 | // set extent to the max length of all strings | |
566 | for ( unsigned int i = 0; i < m_noItems; i++ ) | |
567 | { | |
568 | const wxString str = GetString(i); | |
569 | ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY); | |
570 | ||
571 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
572 | if ( extentX > largestExtent ) | |
573 | largestExtent = extentX; | |
574 | } | |
575 | } | |
576 | else // just increase the extent to the length of this string | |
577 | { | |
578 | int existingExtent = (int)SendMessage(GetHwnd(), | |
579 | LB_GETHORIZONTALEXTENT, 0, 0L); | |
580 | ||
581 | ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY); | |
582 | ||
583 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
584 | if ( extentX > existingExtent ) | |
585 | largestExtent = extentX; | |
586 | } | |
587 | ||
588 | if ( largestExtent ) | |
589 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
590 | //else: it shouldn't change | |
591 | } | |
592 | ||
593 | wxSize wxListBox::DoGetBestSize() const | |
594 | { | |
595 | // find the widest string | |
596 | int wLine; | |
597 | int wListbox = 0; | |
598 | for (unsigned int i = 0; i < m_noItems; i++) | |
599 | { | |
600 | wxString str(GetString(i)); | |
601 | GetTextExtent(str, &wLine, NULL); | |
602 | if ( wLine > wListbox ) | |
603 | wListbox = wLine; | |
604 | } | |
605 | ||
606 | // give it some reasonable default value if there are no strings in the | |
607 | // list | |
608 | if ( wListbox == 0 ) | |
609 | wListbox = 100; | |
610 | ||
611 | // the listbox should be slightly larger than the widest string | |
612 | int cx, cy; | |
613 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); | |
614 | ||
615 | wListbox += 3*cx; | |
616 | ||
617 | // Add room for the scrollbar | |
618 | wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
619 | ||
620 | // don't make the listbox too tall (limit height to 10 items) but don't | |
621 | // make it too small neither | |
622 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* | |
623 | wxMin(wxMax(m_noItems, 3), 10); | |
624 | ||
625 | wxSize best(wListbox, hListbox); | |
626 | CacheBestSize(best); | |
627 | return best; | |
628 | } | |
629 | ||
630 | // ---------------------------------------------------------------------------- | |
631 | // callbacks | |
632 | // ---------------------------------------------------------------------------- | |
633 | ||
634 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
635 | { | |
636 | if ((param == LBN_SELCHANGE) && HasMultipleSelection()) | |
637 | { | |
638 | CalcAndSendEvent(); | |
639 | return true; | |
640 | } | |
641 | ||
642 | wxEventType evtType; | |
643 | if ( param == LBN_SELCHANGE ) | |
644 | { | |
645 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
646 | } | |
647 | else if ( param == LBN_DBLCLK ) | |
648 | { | |
649 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; | |
650 | } | |
651 | else | |
652 | { | |
653 | // some event we're not interested in | |
654 | return false; | |
655 | } | |
656 | ||
657 | wxCommandEvent event(evtType, m_windowId); | |
658 | event.SetEventObject( this ); | |
659 | ||
660 | // retrieve the affected item | |
661 | int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
662 | if ( n != LB_ERR ) | |
663 | { | |
664 | if ( HasClientObjectData() ) | |
665 | event.SetClientObject( GetClientObject(n) ); | |
666 | else if ( HasClientUntypedData() ) | |
667 | event.SetClientData( GetClientData(n) ); | |
668 | ||
669 | event.SetString(GetString(n)); | |
670 | } | |
671 | else | |
672 | { | |
673 | return false; | |
674 | } | |
675 | ||
676 | event.SetInt(n); | |
677 | ||
678 | return HandleWindowEvent(event); | |
679 | } | |
680 | ||
681 | // ---------------------------------------------------------------------------- | |
682 | // wxCheckListBox support | |
683 | // ---------------------------------------------------------------------------- | |
684 | ||
685 | #if wxUSE_OWNER_DRAWN | |
686 | ||
687 | // drawing | |
688 | // ------- | |
689 | ||
690 | // space beneath/above each row in pixels | |
691 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
692 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
693 | ||
694 | // the height is the same for all items | |
695 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes | |
696 | ||
697 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
698 | // message is not yet sent when we get here! | |
699 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
700 | { | |
701 | // only owner-drawn control should receive this message | |
702 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); | |
703 | ||
704 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
705 | ||
706 | #ifdef __WXWINCE__ | |
707 | HDC hdc = GetDC(NULL); | |
708 | #else | |
709 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); | |
710 | #endif | |
711 | ||
712 | { | |
713 | wxDCTemp dc((WXHDC)hdc); | |
714 | dc.SetFont(GetFont()); | |
715 | ||
716 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; | |
717 | pStruct->itemWidth = dc.GetCharWidth(); | |
718 | } | |
719 | ||
720 | #ifdef __WXWINCE__ | |
721 | ReleaseDC(NULL, hdc); | |
722 | #else | |
723 | DeleteDC(hdc); | |
724 | #endif | |
725 | ||
726 | return true; | |
727 | } | |
728 | ||
729 | // forward the message to the appropriate item | |
730 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
731 | { | |
732 | // only owner-drawn control should receive this message | |
733 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false ); | |
734 | ||
735 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
736 | ||
737 | // the item may be -1 for an empty listbox | |
738 | if ( pStruct->itemID == (UINT)-1 ) | |
739 | return false; | |
740 | ||
741 | wxListBoxItem *pItem = (wxListBoxItem *)m_aItems[pStruct->itemID]; | |
742 | ||
743 | wxDCTemp dc((WXHDC)pStruct->hDC); | |
744 | wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top); | |
745 | wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom); | |
746 | wxRect rect(pt1, pt2); | |
747 | ||
748 | return pItem->OnDrawItem(dc, rect, | |
749 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
750 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
751 | } | |
752 | ||
753 | #endif // wxUSE_OWNER_DRAWN | |
754 | ||
755 | #endif // wxUSE_LISTBOX |