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