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