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