]>
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 | #ifndef WX_PRECOMP | |
24 | #include "wx/listbox.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/brush.h" | |
27 | #include "wx/font.h" | |
28 | #include "wx/dc.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/msw/private.h" | |
32 | ||
33 | #include <windows.h> | |
34 | #include <windowsx.h> | |
35 | ||
36 | #ifndef __TWIN32__ | |
37 | #ifdef __GNUWIN32__ | |
38 | #include <wx/msw/gnuwin32/extra.h> | |
39 | #endif | |
40 | #endif | |
41 | ||
42 | #ifdef GetCharWidth | |
43 | #undef GetCharWidth | |
44 | #endif | |
45 | ||
46 | #if wxUSE_OWNER_DRAWN | |
47 | #include "wx/ownerdrw.h" | |
48 | #endif | |
49 | ||
50 | #include "wx/dynarray.h" | |
51 | #include "wx/log.h" | |
52 | ||
53 | #if !USE_SHARED_LIBRARY | |
54 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) | |
55 | #endif | |
56 | ||
57 | // ============================================================================ | |
58 | // list box item declaration and implementation | |
59 | // ============================================================================ | |
60 | ||
61 | #if wxUSE_OWNER_DRAWN | |
62 | ||
63 | class wxListBoxItem : public wxOwnerDrawn | |
64 | { | |
65 | public: | |
66 | wxListBoxItem(const wxString& str = ""); | |
67 | }; | |
68 | ||
69 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
70 | { | |
71 | // no bitmaps/checkmarks | |
72 | SetMarginWidth(0); | |
73 | } | |
74 | ||
75 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
76 | { | |
77 | return new wxListBoxItem(); | |
78 | } | |
79 | ||
80 | #endif //USE_OWNER_DRAWN | |
81 | ||
82 | // ============================================================================ | |
83 | // list box control implementation | |
84 | // ============================================================================ | |
85 | ||
86 | // this macro is dangerous but still better than tons of (HWND)GetHWND() | |
87 | #define hwnd (HWND)GetHWND() | |
88 | ||
89 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
90 | { | |
91 | /* | |
92 | if (param == LBN_SELCANCEL) | |
93 | { | |
94 | event.extraLong = FALSE; | |
95 | } | |
96 | */ | |
97 | if (param == LBN_SELCHANGE) | |
98 | { | |
99 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
100 | wxArrayInt aSelections; | |
101 | int count = GetSelections(aSelections); | |
102 | if ( count > 0 ) | |
103 | { | |
104 | event.m_commandInt = aSelections[0] ; | |
105 | event.m_clientData = GetClientData(event.m_commandInt); | |
106 | wxString str(GetString(event.m_commandInt)); | |
107 | if (str != "") | |
108 | { | |
109 | event.m_commandString = str; | |
110 | } | |
111 | } | |
112 | else | |
113 | { | |
114 | event.m_commandInt = -1 ; | |
115 | event.m_commandString.Empty(); | |
116 | } | |
117 | ||
118 | event.SetEventObject( this ); | |
119 | ProcessCommand(event); | |
120 | return TRUE; | |
121 | } | |
122 | else if (param == LBN_DBLCLK) | |
123 | { | |
124 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
125 | event.SetEventObject( this ); | |
126 | GetEventHandler()->ProcessEvent(event) ; | |
127 | return TRUE; | |
128 | } | |
129 | ||
130 | return FALSE; | |
131 | } | |
132 | ||
133 | // Listbox item | |
134 | wxListBox::wxListBox() | |
135 | { | |
136 | m_noItems = 0; | |
137 | m_selected = 0; | |
138 | } | |
139 | ||
140 | bool wxListBox::Create(wxWindow *parent, | |
141 | wxWindowID id, | |
142 | const wxPoint& pos, | |
143 | const wxSize& size, | |
144 | int n, const wxString choices[], | |
145 | long style, | |
146 | const wxValidator& validator, | |
147 | const wxString& name) | |
148 | { | |
149 | m_noItems = 0; | |
150 | m_hWnd = 0; | |
151 | m_selected = 0; | |
152 | ||
153 | SetName(name); | |
154 | SetValidator(validator); | |
155 | ||
156 | if (parent) | |
157 | parent->AddChild(this); | |
158 | ||
159 | wxSystemSettings settings; | |
160 | SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
161 | SetForegroundColour(parent->GetForegroundColour()); | |
162 | ||
163 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; | |
164 | ||
165 | int x = pos.x; | |
166 | int y = pos.y; | |
167 | int width = size.x; | |
168 | int height = size.y; | |
169 | m_windowStyle = style; | |
170 | ||
171 | DWORD wstyle = WS_VSCROLL | WS_TABSTOP | LBS_NOTIFY | LBS_HASSTRINGS; | |
172 | if (m_windowStyle & wxLB_MULTIPLE) | |
173 | wstyle |= LBS_MULTIPLESEL; | |
174 | else if (m_windowStyle & wxLB_EXTENDED) | |
175 | wstyle |= LBS_EXTENDEDSEL; | |
176 | ||
177 | if (m_windowStyle & wxLB_ALWAYS_SB) | |
178 | wstyle |= LBS_DISABLENOSCROLL ; | |
179 | if (m_windowStyle & wxLB_HSCROLL) | |
180 | wstyle |= WS_HSCROLL; | |
181 | if (m_windowStyle & wxLB_SORT) | |
182 | wstyle |= LBS_SORT; | |
183 | ||
184 | #if wxUSE_OWNER_DRAWN | |
185 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
186 | // we don't support LBS_OWNERDRAWVARIABLE yet | |
187 | wstyle |= LBS_OWNERDRAWFIXED; | |
188 | } | |
189 | #endif | |
190 | ||
191 | // Without this style, you get unexpected heights, so e.g. constraint layout | |
192 | // doesn't work properly | |
193 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
194 | ||
195 | bool want3D; | |
196 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
197 | ||
198 | // Even with extended styles, need to combine with WS_BORDER | |
199 | // for them to look right. | |
200 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
201 | { | |
202 | wstyle |= WS_BORDER; | |
203 | } | |
204 | ||
205 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "LISTBOX", NULL, | |
206 | wstyle | WS_CHILD, | |
207 | 0, 0, 0, 0, | |
208 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
209 | wxGetInstance(), NULL); | |
210 | ||
211 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create listbox" ); | |
212 | ||
213 | #if wxUSE_CTL3D | |
214 | if (want3D) | |
215 | { | |
216 | Ctl3dSubclassCtl(hwnd); | |
217 | m_useCtl3D = TRUE; | |
218 | } | |
219 | #endif | |
220 | ||
221 | // Subclass again to catch messages | |
222 | SubclassWin(m_hWnd); | |
223 | ||
224 | size_t ui; | |
225 | for (ui = 0; ui < (size_t)n; ui++) { | |
226 | Append(choices[ui]); | |
227 | } | |
228 | ||
229 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) | |
230 | SendMessage(hwnd, LB_SETCURSEL, 0, 0); | |
231 | ||
232 | SetFont(parent->GetFont()); | |
233 | ||
234 | SetSize(x, y, width, height); | |
235 | ||
236 | Show(TRUE); | |
237 | ||
238 | return TRUE; | |
239 | } | |
240 | ||
241 | wxListBox::~wxListBox() | |
242 | { | |
243 | #if wxUSE_OWNER_DRAWN | |
244 | size_t uiCount = m_aItems.Count(); | |
245 | while ( uiCount-- != 0 ) { | |
246 | delete m_aItems[uiCount]; | |
247 | } | |
248 | #endif // wxUSE_OWNER_DRAWN | |
249 | } | |
250 | ||
251 | void wxListBox::SetupColours() | |
252 | { | |
253 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
254 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
255 | } | |
256 | ||
257 | void wxListBox::SetFirstItem(int N) | |
258 | { | |
259 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
260 | "invalid index in wxListBox::SetFirstItem" ); | |
261 | ||
262 | SendMessage(hwnd,LB_SETTOPINDEX,(WPARAM)N,(LPARAM)0) ; | |
263 | } | |
264 | ||
265 | void wxListBox::SetFirstItem(const wxString& s) | |
266 | { | |
267 | int N = FindString(s) ; | |
268 | ||
269 | if ( N >= 0 ) | |
270 | SetFirstItem(N) ; | |
271 | } | |
272 | ||
273 | void wxListBox::Delete(int N) | |
274 | { | |
275 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
276 | "invalid index in wxListBox::Delete" ); | |
277 | ||
278 | SendMessage(hwnd, LB_DELETESTRING, N, 0); | |
279 | m_noItems--; | |
280 | ||
281 | SetHorizontalExtent(""); | |
282 | } | |
283 | ||
284 | void wxListBox::Append(const wxString& item) | |
285 | { | |
286 | int index = ListBox_AddString(hwnd, item); | |
287 | m_noItems ++; | |
288 | ||
289 | #if wxUSE_OWNER_DRAWN | |
290 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
291 | wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument | |
292 | pNewItem->SetName(item); | |
293 | m_aItems.Add(pNewItem); | |
294 | ListBox_SetItemData(hwnd, index, pNewItem); | |
295 | } | |
296 | #endif | |
297 | ||
298 | SetHorizontalExtent(item); | |
299 | } | |
300 | ||
301 | void wxListBox::Append(const wxString& item, char *Client_data) | |
302 | { | |
303 | int index = ListBox_AddString(hwnd, item); | |
304 | m_noItems ++; | |
305 | ||
306 | #if wxUSE_OWNER_DRAWN | |
307 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
308 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
309 | // in OnMeasure/OnDraw. | |
310 | wxFAIL_MSG("Can't use client data with owner-drawn listboxes"); | |
311 | } | |
312 | else | |
313 | #endif | |
314 | ||
315 | ListBox_SetItemData(hwnd, index, Client_data); | |
316 | ||
317 | SetHorizontalExtent(item); | |
318 | } | |
319 | ||
320 | void wxListBox::Set(int n, const wxString *choices, char** clientData) | |
321 | { | |
322 | ShowWindow(hwnd, SW_HIDE); | |
323 | ListBox_ResetContent(hwnd); | |
324 | int i; | |
325 | for (i = 0; i < n; i++) | |
326 | { | |
327 | ListBox_AddString(hwnd, choices[i]); | |
328 | if ( clientData ) | |
329 | ListBox_SetItemData(hwnd, i, clientData[i]); | |
330 | } | |
331 | m_noItems = n; | |
332 | ||
333 | #if wxUSE_OWNER_DRAWN | |
334 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
335 | // first delete old items | |
336 | size_t ui = m_aItems.Count(); | |
337 | while ( ui-- != 0 ) { | |
338 | delete m_aItems[ui]; | |
339 | } | |
340 | m_aItems.Empty(); | |
341 | ||
342 | // then create new ones | |
343 | for (ui = 0; ui < (size_t)n; ui++) { | |
344 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
345 | pNewItem->SetName(choices[ui]); | |
346 | m_aItems.Add(pNewItem); | |
347 | ListBox_SetItemData(hwnd, ui, pNewItem); | |
348 | ||
349 | wxASSERT_MSG(clientData[ui] == NULL, | |
350 | "Can't use client data with owner-drawn listboxes"); | |
351 | } | |
352 | } | |
353 | #endif | |
354 | ||
355 | SetHorizontalExtent(""); | |
356 | ShowWindow(hwnd, SW_SHOW); | |
357 | } | |
358 | ||
359 | int wxListBox::FindString(const wxString& s) const | |
360 | { | |
361 | int pos = ListBox_FindStringExact(hwnd, (WPARAM)-1, s); | |
362 | if (pos == LB_ERR) | |
363 | return -1; | |
364 | else | |
365 | return pos; | |
366 | } | |
367 | ||
368 | void wxListBox::Clear() | |
369 | { | |
370 | ListBox_ResetContent(hwnd); | |
371 | ||
372 | #if wxUSE_OWNER_DRAWN | |
373 | size_t uiCount = m_aItems.Count(); | |
374 | while ( uiCount-- != 0 ) { | |
375 | delete m_aItems[uiCount]; | |
376 | } | |
377 | ||
378 | m_aItems.Clear(); | |
379 | #endif // wxUSE_OWNER_DRAWN | |
380 | ||
381 | m_noItems = 0; | |
382 | ListBox_GetHorizontalExtent(hwnd); | |
383 | } | |
384 | ||
385 | void wxListBox::SetSelection(int N, bool select) | |
386 | { | |
387 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
388 | "invalid index in wxListBox::SetSelection" ); | |
389 | ||
390 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) | |
391 | SendMessage(hwnd, LB_SETSEL, select, N); | |
392 | else | |
393 | { | |
394 | int N1 = N; | |
395 | if (!select) | |
396 | N1 = -1; | |
397 | SendMessage(hwnd, LB_SETCURSEL, N1, 0); | |
398 | } | |
399 | } | |
400 | ||
401 | bool wxListBox::Selected(int N) const | |
402 | { | |
403 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
404 | "invalid index in wxListBox::Selected" ); | |
405 | ||
406 | return SendMessage(hwnd, LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; | |
407 | } | |
408 | ||
409 | void wxListBox::Deselect(int N) | |
410 | { | |
411 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
412 | "invalid index in wxListBox::Deselect" ); | |
413 | ||
414 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) | |
415 | SendMessage(hwnd, LB_SETSEL, FALSE, N); | |
416 | } | |
417 | ||
418 | char *wxListBox::GetClientData(int N) const | |
419 | { | |
420 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, | |
421 | "invalid index in wxListBox::GetClientData" ); | |
422 | ||
423 | return (char *)SendMessage(hwnd, LB_GETITEMDATA, N, 0); | |
424 | } | |
425 | ||
426 | void wxListBox::SetClientData(int N, char *Client_data) | |
427 | { | |
428 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
429 | "invalid index in wxListBox::SetClientData" ); | |
430 | ||
431 | if ( ListBox_SetItemData(hwnd, N, Client_data) == LB_ERR ) | |
432 | wxLogDebug("LB_SETITEMDATA failed"); | |
433 | } | |
434 | ||
435 | // Return number of selections and an array of selected integers | |
436 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
437 | { | |
438 | aSelections.Empty(); | |
439 | ||
440 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) | |
441 | { | |
442 | int no_sel = ListBox_GetSelCount(hwnd); | |
443 | if (no_sel != 0) { | |
444 | int *selections = new int[no_sel]; | |
445 | if ( ListBox_GetSelItems(hwnd, no_sel, selections) == LB_ERR ) { | |
446 | wxFAIL_MSG("This listbox can't have single-selection style!"); | |
447 | } | |
448 | ||
449 | aSelections.Alloc(no_sel); | |
450 | for ( int n = 0; n < no_sel; n++ ) | |
451 | aSelections.Add(selections[n]); | |
452 | ||
453 | delete [] selections; | |
454 | } | |
455 | ||
456 | return no_sel; | |
457 | } | |
458 | else // single-selection listbox | |
459 | { | |
460 | aSelections.Add(ListBox_GetCurSel(hwnd)); | |
461 | ||
462 | return 1; | |
463 | } | |
464 | } | |
465 | ||
466 | // Get single selection, for single choice list items | |
467 | int wxListBox::GetSelection() const | |
468 | { | |
469 | wxCHECK_MSG( !(m_windowStyle & wxLB_MULTIPLE) && | |
470 | !(m_windowStyle & wxLB_EXTENDED), | |
471 | -1, | |
472 | "GetSelection() can't be used with multiple-selection " | |
473 | "listboxes, use GetSelections() instead." ); | |
474 | ||
475 | return ListBox_GetCurSel(hwnd); | |
476 | } | |
477 | ||
478 | // Find string for position | |
479 | wxString wxListBox::GetString(int N) const | |
480 | { | |
481 | wxCHECK_MSG( N >= 0 && N < m_noItems, "", | |
482 | "invalid index in wxListBox::GetClientData" ); | |
483 | ||
484 | int len = ListBox_GetTextLen(hwnd, N); | |
485 | ||
486 | // +1 for terminating NUL | |
487 | wxString result; | |
488 | ListBox_GetText(hwnd, N, result.GetWriteBuf(len + 1)); | |
489 | result.UngetWriteBuf(); | |
490 | ||
491 | return result; | |
492 | } | |
493 | ||
494 | void wxListBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
495 | { | |
496 | int currentX, currentY; | |
497 | GetPosition(¤tX, ¤tY); | |
498 | ||
499 | int x1 = x; | |
500 | int y1 = y; | |
501 | int w1 = width; | |
502 | int h1 = height; | |
503 | ||
504 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
505 | x1 = currentX; | |
506 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
507 | y1 = currentY; | |
508 | ||
509 | AdjustForParentClientOrigin(x1, y1, sizeFlags); | |
510 | ||
511 | // If we're prepared to use the existing size, then... | |
512 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
513 | { | |
514 | GetSize(&w1, &h1); | |
515 | } | |
516 | ||
517 | int cx; // button font dimensions | |
518 | int cy; | |
519 | ||
520 | wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont()); | |
521 | ||
522 | float control_width, control_height, control_x, control_y; | |
523 | ||
524 | // Deal with default size (using -1 values) | |
525 | if (w1<=0) | |
526 | w1 = DEFAULT_ITEM_WIDTH; | |
527 | ||
528 | if (h1<=0) | |
529 | h1 = DEFAULT_ITEM_HEIGHT; | |
530 | ||
531 | control_x = (float)x1; | |
532 | control_y = (float)y1; | |
533 | control_width = (float)w1; | |
534 | control_height = (float)h1; | |
535 | ||
536 | // Calculations may have made size too small | |
537 | if (control_height <= 0) | |
538 | control_height = (float)DEFAULT_ITEM_HEIGHT; | |
539 | ||
540 | if (control_width <= 0) | |
541 | control_width = (float)DEFAULT_ITEM_WIDTH; | |
542 | ||
543 | MoveWindow(hwnd, | |
544 | (int)control_x, (int)control_y, | |
545 | (int)control_width, (int)control_height, | |
546 | TRUE); | |
547 | ||
548 | } | |
549 | ||
550 | // Windows-specific code to set the horizontal extent of | |
551 | // the listbox, if necessary. If s is non-NULL, it's | |
552 | // used to calculate the horizontal extent. | |
553 | // Otherwise, all strings are used. | |
554 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
555 | { | |
556 | // Only necessary if we want a horizontal scrollbar | |
557 | if (!(m_windowStyle & wxHSCROLL)) | |
558 | return; | |
559 | TEXTMETRIC lpTextMetric; | |
560 | ||
561 | if (s != "") | |
562 | { | |
563 | int existingExtent = (int)SendMessage(hwnd, LB_GETHORIZONTALEXTENT, 0, 0L); | |
564 | HDC dc = GetWindowDC(hwnd); | |
565 | HFONT oldFont = 0; | |
566 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
567 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
568 | ||
569 | GetTextMetrics(dc, &lpTextMetric); | |
570 | SIZE extentXY; | |
571 | ::GetTextExtentPoint(dc, (LPSTR) (const char *)s, s.Length(), &extentXY); | |
572 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
573 | ||
574 | if (oldFont) | |
575 | ::SelectObject(dc, oldFont); | |
576 | ||
577 | ReleaseDC(hwnd, dc); | |
578 | if (extentX > existingExtent) | |
579 | SendMessage(hwnd, LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); | |
580 | return; | |
581 | } | |
582 | else | |
583 | { | |
584 | int largestExtent = 0; | |
585 | HDC dc = GetWindowDC(hwnd); | |
586 | HFONT oldFont = 0; | |
587 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
588 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
589 | ||
590 | GetTextMetrics(dc, &lpTextMetric); | |
591 | int i; | |
592 | for (i = 0; i < m_noItems; i++) | |
593 | { | |
594 | int len = (int)SendMessage(hwnd, LB_GETTEXT, i, (LONG)wxBuffer); | |
595 | wxBuffer[len] = 0; | |
596 | SIZE extentXY; | |
597 | ::GetTextExtentPoint(dc, (LPSTR)wxBuffer, len, &extentXY); | |
598 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
599 | if (extentX > largestExtent) | |
600 | largestExtent = extentX; | |
601 | } | |
602 | if (oldFont) | |
603 | ::SelectObject(dc, oldFont); | |
604 | ||
605 | ReleaseDC(hwnd, dc); | |
606 | SendMessage(hwnd, LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
607 | } | |
608 | } | |
609 | ||
610 | void | |
611 | wxListBox::InsertItems(int nItems, const wxString items[], int pos) | |
612 | { | |
613 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
614 | "invalid index in wxListBox::InsertItems" ); | |
615 | ||
616 | int i; | |
617 | for (i = 0; i < nItems; i++) | |
618 | ListBox_InsertString(hwnd, i + pos, items[i]); | |
619 | m_noItems += nItems; | |
620 | ||
621 | SetHorizontalExtent(""); | |
622 | } | |
623 | ||
624 | void wxListBox::SetString(int N, const wxString& s) | |
625 | { | |
626 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
627 | "invalid index in wxListBox::SetString" ); | |
628 | ||
629 | int sel = -1; | |
630 | if (!(m_windowStyle & wxLB_MULTIPLE) && !(m_windowStyle & wxLB_EXTENDED)) | |
631 | sel = GetSelection(); | |
632 | ||
633 | char *oldData = (char *)wxListBox::GetClientData(N); | |
634 | ||
635 | SendMessage(hwnd, LB_DELETESTRING, N, 0); | |
636 | ||
637 | int newN = N; | |
638 | if (N == (m_noItems - 1)) | |
639 | newN = -1; | |
640 | ||
641 | SendMessage(hwnd, LB_INSERTSTRING, newN, (LPARAM) (const char *)s); | |
642 | if (oldData) | |
643 | wxListBox::SetClientData(N, oldData); | |
644 | ||
645 | // Selection may have changed | |
646 | if (sel >= 0) | |
647 | SetSelection(sel); | |
648 | ||
649 | #if wxUSE_OWNER_DRAWN | |
650 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
651 | // update item's text | |
652 | m_aItems[N]->SetName(s); | |
653 | #endif //USE_OWNER_DRAWN | |
654 | } | |
655 | ||
656 | int wxListBox::Number () const | |
657 | { | |
658 | return m_noItems; | |
659 | } | |
660 | ||
661 | // For single selection items only | |
662 | wxString wxListBox::GetStringSelection () const | |
663 | { | |
664 | int sel = GetSelection (); | |
665 | if (sel > -1) | |
666 | return this->GetString (sel); | |
667 | else | |
668 | return wxString(""); | |
669 | } | |
670 | ||
671 | bool wxListBox::SetStringSelection (const wxString& s, bool flag) | |
672 | { | |
673 | int sel = FindString (s); | |
674 | if (sel > -1) | |
675 | { | |
676 | SetSelection (sel, flag); | |
677 | return TRUE; | |
678 | } | |
679 | else | |
680 | return FALSE; | |
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("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(hwnd, 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 |