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