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