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