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