]>
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 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
149 | SetForegroundColour(parent->GetForegroundColour()); | |
150 | ||
151 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; | |
152 | ||
153 | int x = pos.x; | |
154 | int y = pos.y; | |
155 | int width = size.x; | |
156 | int height = size.y; | |
157 | m_windowStyle = style; | |
158 | ||
159 | DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | | |
160 | LBS_NOTIFY | LBS_HASSTRINGS /* | WS_CLIPSIBLINGS */; | |
161 | ||
162 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
163 | _T("only one of listbox selection modes can be specified") ); | |
164 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
165 | wstyle |= WS_CLIPSIBLINGS; | |
166 | ||
167 | if (m_windowStyle & wxLB_MULTIPLE) | |
168 | wstyle |= LBS_MULTIPLESEL; | |
169 | else if (m_windowStyle & wxLB_EXTENDED) | |
170 | wstyle |= LBS_EXTENDEDSEL; | |
171 | ||
172 | if (m_windowStyle & wxLB_ALWAYS_SB) | |
173 | wstyle |= LBS_DISABLENOSCROLL; | |
174 | if (m_windowStyle & wxLB_HSCROLL) | |
175 | wstyle |= WS_HSCROLL; | |
176 | if (m_windowStyle & wxLB_SORT) | |
177 | wstyle |= LBS_SORT; | |
178 | ||
179 | #if wxUSE_OWNER_DRAWN | |
180 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
181 | // we don't support LBS_OWNERDRAWVARIABLE yet | |
182 | wstyle |= LBS_OWNERDRAWFIXED; | |
183 | } | |
184 | #endif | |
185 | ||
186 | // Without this style, you get unexpected heights, so e.g. constraint layout | |
187 | // doesn't work properly | |
188 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
189 | ||
190 | bool want3D; | |
191 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); | |
192 | ||
193 | // Even with extended styles, need to combine with WS_BORDER for them to | |
194 | // look right. | |
195 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
196 | { | |
197 | wstyle |= WS_BORDER; | |
198 | } | |
199 | ||
200 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL, | |
201 | wstyle | WS_CHILD, | |
202 | 0, 0, 0, 0, | |
203 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
204 | wxGetInstance(), NULL); | |
205 | ||
206 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") ); | |
207 | ||
208 | #if wxUSE_CTL3D | |
209 | if (want3D) | |
210 | { | |
211 | Ctl3dSubclassCtl(GetHwnd()); | |
212 | m_useCtl3D = TRUE; | |
213 | } | |
214 | #endif | |
215 | ||
216 | // Subclass again to catch messages | |
217 | SubclassWin(m_hWnd); | |
218 | ||
219 | size_t ui; | |
220 | for (ui = 0; ui < (size_t)n; ui++) { | |
221 | Append(choices[ui]); | |
222 | } | |
223 | ||
224 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) | |
225 | SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0); | |
226 | ||
227 | SetFont(parent->GetFont()); | |
228 | ||
229 | SetSize(x, y, width, height); | |
230 | ||
231 | return TRUE; | |
232 | } | |
233 | ||
234 | wxListBox::~wxListBox() | |
235 | { | |
236 | Free(); | |
237 | } | |
238 | ||
239 | void wxListBox::SetupColours() | |
240 | { | |
241 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
242 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
243 | } | |
244 | ||
245 | // ---------------------------------------------------------------------------- | |
246 | // implementation of wxListBoxBase methods | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | void wxListBox::DoSetFirstItem(int N) | |
250 | { | |
251 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
252 | wxT("invalid index in wxListBox::SetFirstItem") ); | |
253 | ||
254 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); | |
255 | } | |
256 | ||
257 | void wxListBox::Delete(int N) | |
258 | { | |
259 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
260 | wxT("invalid index in wxListBox::Delete") ); | |
261 | ||
262 | // for owner drawn objects, the data is used for storing wxOwnerDrawn | |
263 | // pointers and we shouldn't touch it | |
264 | #if !wxUSE_OWNER_DRAWN | |
265 | if ( !(m_windowStyle & wxLB_OWNERDRAW) ) | |
266 | #endif // !wxUSE_OWNER_DRAWN | |
267 | if ( HasClientObjectData() ) | |
268 | { | |
269 | delete GetClientObject(N); | |
270 | } | |
271 | ||
272 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
273 | m_noItems--; | |
274 | ||
275 | SetHorizontalExtent(""); | |
276 | } | |
277 | ||
278 | int wxListBox::DoAppend(const wxString& item) | |
279 | { | |
280 | int index = ListBox_AddString(GetHwnd(), item); | |
281 | m_noItems++; | |
282 | ||
283 | #if wxUSE_OWNER_DRAWN | |
284 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
285 | wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument | |
286 | pNewItem->SetName(item); | |
287 | m_aItems.Add(pNewItem); | |
288 | ListBox_SetItemData(GetHwnd(), index, pNewItem); | |
289 | pNewItem->SetFont(GetFont()); | |
290 | } | |
291 | #endif | |
292 | ||
293 | SetHorizontalExtent(item); | |
294 | ||
295 | return index; | |
296 | } | |
297 | ||
298 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) | |
299 | { | |
300 | // avoid flicker - but don't need to do this for a hidden listbox | |
301 | bool hideAndShow = IsShown(); | |
302 | if ( hideAndShow ) | |
303 | { | |
304 | ShowWindow(GetHwnd(), SW_HIDE); | |
305 | } | |
306 | ||
307 | ListBox_ResetContent(GetHwnd()); | |
308 | ||
309 | m_noItems = choices.GetCount(); | |
310 | int i; | |
311 | for (i = 0; i < m_noItems; i++) | |
312 | { | |
313 | ListBox_AddString(GetHwnd(), choices[i]); | |
314 | if ( clientData ) | |
315 | { | |
316 | #if wxUSE_OWNER_DRAWN | |
317 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
318 | { | |
319 | wxASSERT_MSG(clientData[i] == NULL, | |
320 | wxT("Can't use client data with owner-drawn listboxes")); | |
321 | } | |
322 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); | |
323 | #else // !wxUSE_OWNER_DRAWN | |
324 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); | |
325 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
326 | } | |
327 | } | |
328 | ||
329 | #if wxUSE_OWNER_DRAWN | |
330 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
331 | // first delete old items | |
332 | size_t ui = m_aItems.Count(); | |
333 | while ( ui-- != 0 ) { | |
334 | delete m_aItems[ui]; | |
335 | } | |
336 | m_aItems.Empty(); | |
337 | ||
338 | // then create new ones | |
339 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
340 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
341 | pNewItem->SetName(choices[ui]); | |
342 | m_aItems.Add(pNewItem); | |
343 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); | |
344 | } | |
345 | } | |
346 | #endif // wxUSE_OWNER_DRAWN | |
347 | ||
348 | SetHorizontalExtent(); | |
349 | ||
350 | if ( hideAndShow ) | |
351 | { | |
352 | // show the listbox back if we hid it | |
353 | ShowWindow(GetHwnd(), SW_SHOW); | |
354 | } | |
355 | } | |
356 | ||
357 | int wxListBox::FindString(const wxString& s) const | |
358 | { | |
359 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); | |
360 | if (pos == LB_ERR) | |
361 | return wxNOT_FOUND; | |
362 | else | |
363 | return pos; | |
364 | } | |
365 | ||
366 | void wxListBox::Clear() | |
367 | { | |
368 | Free(); | |
369 | ||
370 | ListBox_ResetContent(GetHwnd()); | |
371 | ||
372 | m_noItems = 0; | |
373 | SetHorizontalExtent(); | |
374 | } | |
375 | ||
376 | void wxListBox::Free() | |
377 | { | |
378 | #if wxUSE_OWNER_DRAWN | |
379 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
380 | { | |
381 | size_t uiCount = m_aItems.Count(); | |
382 | while ( uiCount-- != 0 ) { | |
383 | delete m_aItems[uiCount]; | |
384 | } | |
385 | ||
386 | m_aItems.Clear(); | |
387 | } | |
388 | else | |
389 | #endif // wxUSE_OWNER_DRAWN | |
390 | if ( HasClientObjectData() ) | |
391 | { | |
392 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
393 | { | |
394 | delete GetClientObject(n); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | void wxListBox::SetSelection(int N, bool select) | |
400 | { | |
401 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
402 | wxT("invalid index in wxListBox::SetSelection") ); | |
403 | ||
404 | if ( HasMultipleSelection() ) | |
405 | { | |
406 | SendMessage(GetHwnd(), LB_SETSEL, select, N); | |
407 | } | |
408 | else | |
409 | { | |
410 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); | |
411 | } | |
412 | } | |
413 | ||
414 | bool wxListBox::IsSelected(int N) const | |
415 | { | |
416 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
417 | wxT("invalid index in wxListBox::Selected") ); | |
418 | ||
419 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; | |
420 | } | |
421 | ||
422 | wxClientData* wxListBox::DoGetItemClientObject(int n) const | |
423 | { | |
424 | return (wxClientData *)DoGetItemClientData(n); | |
425 | } | |
426 | ||
427 | void *wxListBox::DoGetItemClientData(int n) const | |
428 | { | |
429 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, | |
430 | wxT("invalid index in wxListBox::GetClientData") ); | |
431 | ||
432 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); | |
433 | } | |
434 | ||
435 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
436 | { | |
437 | DoSetItemClientData(n, clientData); | |
438 | } | |
439 | ||
440 | void wxListBox::DoSetItemClientData(int n, void *clientData) | |
441 | { | |
442 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
443 | wxT("invalid index in wxListBox::SetClientData") ); | |
444 | ||
445 | #if wxUSE_OWNER_DRAWN | |
446 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
447 | { | |
448 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
449 | // in OnMeasure/OnDraw. | |
450 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
451 | } | |
452 | #endif // wxUSE_OWNER_DRAWN | |
453 | ||
454 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
455 | wxLogDebug(wxT("LB_SETITEMDATA failed")); | |
456 | } | |
457 | ||
458 | // Return number of selections and an array of selected integers | |
459 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
460 | { | |
461 | aSelections.Empty(); | |
462 | ||
463 | if ( HasMultipleSelection() ) | |
464 | { | |
465 | int no_sel = ListBox_GetSelCount(GetHwnd()); | |
466 | if (no_sel != 0) { | |
467 | int *selections = new int[no_sel]; | |
468 | int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections); | |
469 | ||
470 | wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed")); | |
471 | ||
472 | aSelections.Alloc(no_sel); | |
473 | for ( int n = 0; n < no_sel; n++ ) | |
474 | aSelections.Add(selections[n]); | |
475 | ||
476 | delete [] selections; | |
477 | } | |
478 | ||
479 | return no_sel; | |
480 | } | |
481 | else // single-selection listbox | |
482 | { | |
483 | if (ListBox_GetCurSel(GetHwnd()) > -1) | |
484 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
485 | ||
486 | return aSelections.Count(); | |
487 | } | |
488 | } | |
489 | ||
490 | // Get single selection, for single choice list items | |
491 | int wxListBox::GetSelection() const | |
492 | { | |
493 | wxCHECK_MSG( !HasMultipleSelection(), | |
494 | -1, | |
495 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); | |
496 | ||
497 | return ListBox_GetCurSel(GetHwnd()); | |
498 | } | |
499 | ||
500 | // Find string for position | |
501 | wxString wxListBox::GetString(int N) const | |
502 | { | |
503 | wxCHECK_MSG( N >= 0 && N < m_noItems, "", | |
504 | wxT("invalid index in wxListBox::GetClientData") ); | |
505 | ||
506 | int len = ListBox_GetTextLen(GetHwnd(), N); | |
507 | ||
508 | // +1 for terminating NUL | |
509 | wxString result; | |
510 | ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1)); | |
511 | result.UngetWriteBuf(); | |
512 | ||
513 | return result; | |
514 | } | |
515 | ||
516 | void | |
517 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
518 | { | |
519 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
520 | wxT("invalid index in wxListBox::InsertItems") ); | |
521 | ||
522 | int nItems = items.GetCount(); | |
523 | for ( int i = 0; i < nItems; i++ ) | |
524 | ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
525 | m_noItems += nItems; | |
526 | ||
527 | SetHorizontalExtent(); | |
528 | } | |
529 | ||
530 | void wxListBox::SetString(int N, const wxString& s) | |
531 | { | |
532 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
533 | wxT("invalid index in wxListBox::SetString") ); | |
534 | ||
535 | // remember the state of the item | |
536 | bool wasSelected = IsSelected(N); | |
537 | ||
538 | void *oldData = NULL; | |
539 | wxClientData *oldObjData = NULL; | |
540 | if ( m_clientDataItemsType == wxClientData_Void ) | |
541 | oldData = GetClientData(N); | |
542 | else if ( m_clientDataItemsType == wxClientData_Object ) | |
543 | oldObjData = GetClientObject(N); | |
544 | ||
545 | // delete and recreate it | |
546 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
547 | ||
548 | int newN = N; | |
549 | if ( N == m_noItems - 1 ) | |
550 | newN = -1; | |
551 | ||
552 | ListBox_InsertString(GetHwnd(), newN, s); | |
553 | ||
554 | // restore the client data | |
555 | if ( oldData ) | |
556 | SetClientData(N, oldData); | |
557 | else if ( oldObjData ) | |
558 | SetClientObject(N, oldObjData); | |
559 | ||
560 | // we may have lost the selection | |
561 | if ( wasSelected ) | |
562 | Select(N); | |
563 | ||
564 | #if wxUSE_OWNER_DRAWN | |
565 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
566 | { | |
567 | // update item's text | |
568 | m_aItems[N]->SetName(s); | |
569 | // reassign the item's data | |
570 | ListBox_SetItemData(GetHwnd(), N, m_aItems[N]); | |
571 | } | |
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::GetFont(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 // wxUSE_OWNER_DRAWN | |
789 | ||
790 | #endif // wxUSE_LISTBOX |