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