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