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