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