]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | /////////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/msw/listbox.cpp |
2bda0e17 KB |
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 | |
6c9a19aa | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
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 | ||
1e6feb95 | 23 | #if wxUSE_LISTBOX |
0c589ad0 | 24 | |
2bda0e17 | 25 | #ifndef WX_PRECOMP |
0c589ad0 BM |
26 | #include "wx/listbox.h" |
27 | #include "wx/settings.h" | |
28 | #include "wx/brush.h" | |
29 | #include "wx/font.h" | |
30 | #include "wx/dc.h" | |
2662e49e | 31 | #include "wx/utils.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
1e6feb95 VZ |
34 | #include "wx/window.h" |
35 | #include "wx/msw/private.h" | |
36 | ||
5ea105e0 RR |
37 | #include <windowsx.h> |
38 | ||
0c589ad0 BM |
39 | #include "wx/dynarray.h" |
40 | #include "wx/log.h" | |
2bda0e17 | 41 | |
0c589ad0 BM |
42 | #if wxUSE_OWNER_DRAWN |
43 | #include "wx/ownerdrw.h" | |
44 | #endif | |
2bda0e17 | 45 | |
b39dbf34 JS |
46 | #ifdef __GNUWIN32_OLD__ |
47 | #include "wx/msw/gnuwin32/extra.h" | |
57c208c5 | 48 | #endif |
2bda0e17 | 49 | |
6a89f9ee | 50 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
51 | WX_DEFINE_FLAGS( wxListBoxStyle ) |
52 | ||
3ff066a4 | 53 | wxBEGIN_FLAGS( wxListBoxStyle ) |
bc9fb572 JS |
54 | // new style border flags, we put them first to |
55 | // use them for streaming out | |
3ff066a4 SC |
56 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
57 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
58 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
59 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
60 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
61 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
bc9fb572 JS |
62 | |
63 | // old style border flags | |
3ff066a4 SC |
64 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
65 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
66 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
67 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
68 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 69 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
70 | |
71 | // standard window styles | |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
73 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
74 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
75 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 76 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
77 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
78 | wxFLAGS_MEMBER(wxVSCROLL) | |
79 | wxFLAGS_MEMBER(wxHSCROLL) | |
80 | ||
81 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
82 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
83 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
84 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
85 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
86 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
87 | wxFLAGS_MEMBER(wxLB_SORT) | |
88 | ||
89 | wxEND_FLAGS( wxListBoxStyle ) | |
bc9fb572 | 90 | |
6a89f9ee SC |
91 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h") |
92 | ||
3ff066a4 SC |
93 | wxBEGIN_PROPERTIES_TABLE(wxListBox) |
94 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent ) | |
95 | wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent ) | |
c5ca409b | 96 | |
3ff066a4 SC |
97 | wxPROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
98 | wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
99 | wxPROPERTY( Selection ,int, SetSelection, GetSelection,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
100 | wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
101 | wxEND_PROPERTIES_TABLE() | |
6a89f9ee | 102 | |
3ff066a4 SC |
103 | wxBEGIN_HANDLERS_TABLE(wxListBox) |
104 | wxEND_HANDLERS_TABLE() | |
6a89f9ee | 105 | |
3ff066a4 | 106 | wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) |
6a89f9ee | 107 | #else |
9c9c3d7a | 108 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
6a89f9ee | 109 | #endif |
2bda0e17 | 110 | |
066f1b7a SC |
111 | /* |
112 | TODO PROPERTIES | |
113 | selection | |
114 | content | |
115 | item | |
116 | */ | |
117 | ||
2bda0e17 KB |
118 | // ============================================================================ |
119 | // list box item declaration and implementation | |
120 | // ============================================================================ | |
121 | ||
47d67540 | 122 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
123 | |
124 | class wxListBoxItem : public wxOwnerDrawn | |
125 | { | |
126 | public: | |
2b5f62a0 | 127 | wxListBoxItem(const wxString& str = wxEmptyString); |
2bda0e17 KB |
128 | }; |
129 | ||
dd3c394a | 130 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) |
2bda0e17 | 131 | { |
dd3c394a VZ |
132 | // no bitmaps/checkmarks |
133 | SetMarginWidth(0); | |
2bda0e17 KB |
134 | } |
135 | ||
2b5f62a0 | 136 | wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n)) |
2bda0e17 | 137 | { |
dd3c394a | 138 | return new wxListBoxItem(); |
2bda0e17 KB |
139 | } |
140 | ||
141 | #endif //USE_OWNER_DRAWN | |
142 | ||
143 | // ============================================================================ | |
144 | // list box control implementation | |
145 | // ============================================================================ | |
146 | ||
2ee3ee1b VZ |
147 | // ---------------------------------------------------------------------------- |
148 | // creation | |
149 | // ---------------------------------------------------------------------------- | |
dd3c394a | 150 | |
2ee3ee1b | 151 | // Listbox item |
bfc6fde4 | 152 | wxListBox::wxListBox() |
2bda0e17 | 153 | { |
dd3c394a VZ |
154 | m_noItems = 0; |
155 | m_selected = 0; | |
2bda0e17 KB |
156 | } |
157 | ||
dd3c394a VZ |
158 | bool wxListBox::Create(wxWindow *parent, |
159 | wxWindowID id, | |
2bda0e17 KB |
160 | const wxPoint& pos, |
161 | const wxSize& size, | |
debe6624 JS |
162 | int n, const wxString choices[], |
163 | long style, | |
ac8d0c11 | 164 | const wxValidator& wxVALIDATOR_PARAM(validator), |
2bda0e17 KB |
165 | const wxString& name) |
166 | { | |
dd3c394a VZ |
167 | m_noItems = 0; |
168 | m_hWnd = 0; | |
169 | m_selected = 0; | |
2bda0e17 | 170 | |
dd3c394a | 171 | SetName(name); |
11b6a93b | 172 | #if wxUSE_VALIDATORS |
dd3c394a | 173 | SetValidator(validator); |
11b6a93b | 174 | #endif // wxUSE_VALIDATORS |
2bda0e17 | 175 | |
dd3c394a VZ |
176 | if (parent) |
177 | parent->AddChild(this); | |
2bda0e17 | 178 | |
7516ed26 | 179 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
dd3c394a | 180 | SetForegroundColour(parent->GetForegroundColour()); |
2bda0e17 | 181 | |
dd3c394a | 182 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; |
2bda0e17 | 183 | |
dd3c394a VZ |
184 | int x = pos.x; |
185 | int y = pos.y; | |
186 | int width = size.x; | |
187 | int height = size.y; | |
188 | m_windowStyle = style; | |
2bda0e17 | 189 | |
fe3d9123 JS |
190 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_TABSTOP | |
191 | LBS_NOTIFY | LBS_HASSTRINGS ; | |
54081dc5 VZ |
192 | |
193 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
194 | _T("only one of listbox selection modes can be specified") ); | |
c76b1a30 JS |
195 | |
196 | if ( (m_windowStyle & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
197 | m_windowStyle |= wxBORDER_SUNKEN; | |
198 | ||
b0766406 JS |
199 | if ( m_windowStyle & wxCLIP_SIBLINGS ) |
200 | wstyle |= WS_CLIPSIBLINGS; | |
54081dc5 | 201 | |
dd3c394a VZ |
202 | if (m_windowStyle & wxLB_MULTIPLE) |
203 | wstyle |= LBS_MULTIPLESEL; | |
204 | else if (m_windowStyle & wxLB_EXTENDED) | |
205 | wstyle |= LBS_EXTENDEDSEL; | |
2bda0e17 | 206 | |
dd3c394a | 207 | if (m_windowStyle & wxLB_ALWAYS_SB) |
2ee3ee1b | 208 | wstyle |= LBS_DISABLENOSCROLL; |
dd3c394a VZ |
209 | if (m_windowStyle & wxLB_HSCROLL) |
210 | wstyle |= WS_HSCROLL; | |
211 | if (m_windowStyle & wxLB_SORT) | |
212 | wstyle |= LBS_SORT; | |
2bda0e17 | 213 | |
4676948b | 214 | #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__) |
2bda0e17 | 215 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a VZ |
216 | // we don't support LBS_OWNERDRAWVARIABLE yet |
217 | wstyle |= LBS_OWNERDRAWFIXED; | |
2bda0e17 | 218 | } |
2bda0e17 KB |
219 | #endif |
220 | ||
dd3c394a VZ |
221 | // Without this style, you get unexpected heights, so e.g. constraint layout |
222 | // doesn't work properly | |
223 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
224 | ||
fe3d9123 | 225 | WXDWORD exStyle = 0; |
22a746d7 | 226 | (void) MSWGetStyle(m_windowStyle, & exStyle) ; |
2bda0e17 | 227 | |
223d09f6 | 228 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL, |
fe3d9123 | 229 | wstyle , |
dd3c394a VZ |
230 | 0, 0, 0, 0, |
231 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
232 | wxGetInstance(), NULL); | |
1c089c47 | 233 | |
223d09f6 | 234 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") ); |
1c089c47 | 235 | |
dd3c394a VZ |
236 | // Subclass again to catch messages |
237 | SubclassWin(m_hWnd); | |
2bda0e17 | 238 | |
dd3c394a VZ |
239 | size_t ui; |
240 | for (ui = 0; ui < (size_t)n; ui++) { | |
241 | Append(choices[ui]); | |
2bda0e17 | 242 | } |
2bda0e17 | 243 | |
dd3c394a | 244 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) |
a23fd0e1 | 245 | SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0); |
2bda0e17 | 246 | |
dd3c394a | 247 | SetFont(parent->GetFont()); |
2bda0e17 | 248 | |
dd3c394a | 249 | SetSize(x, y, width, height); |
2bda0e17 | 250 | |
dd3c394a | 251 | return TRUE; |
2bda0e17 KB |
252 | } |
253 | ||
584ad2a3 MB |
254 | bool wxListBox::Create(wxWindow *parent, |
255 | wxWindowID id, | |
256 | const wxPoint& pos, | |
257 | const wxSize& size, | |
258 | const wxArrayString& choices, | |
259 | long style, | |
76efbc2a | 260 | const wxValidator& validator, |
584ad2a3 MB |
261 | const wxString& name) |
262 | { | |
263 | wxCArrayString chs(choices); | |
264 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
265 | style, validator, name); | |
266 | } | |
267 | ||
bfc6fde4 | 268 | wxListBox::~wxListBox() |
2bda0e17 | 269 | { |
baccb514 | 270 | Free(); |
2bda0e17 KB |
271 | } |
272 | ||
bfc6fde4 | 273 | void wxListBox::SetupColours() |
2bda0e17 | 274 | { |
a756f210 | 275 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
dd3c394a | 276 | SetForegroundColour(GetParent()->GetForegroundColour()); |
2bda0e17 KB |
277 | } |
278 | ||
2ee3ee1b VZ |
279 | // ---------------------------------------------------------------------------- |
280 | // implementation of wxListBoxBase methods | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | void wxListBox::DoSetFirstItem(int N) | |
2bda0e17 | 284 | { |
dd3c394a | 285 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 286 | wxT("invalid index in wxListBox::SetFirstItem") ); |
dd3c394a | 287 | |
2ee3ee1b | 288 | SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); |
2bda0e17 KB |
289 | } |
290 | ||
debe6624 | 291 | void wxListBox::Delete(int N) |
2bda0e17 | 292 | { |
dd3c394a | 293 | wxCHECK_RET( N >= 0 && N < m_noItems, |
223d09f6 | 294 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a | 295 | |
07cf98cb VZ |
296 | // for owner drawn objects, the data is used for storing wxOwnerDrawn |
297 | // pointers and we shouldn't touch it | |
298 | #if !wxUSE_OWNER_DRAWN | |
299 | if ( !(m_windowStyle & wxLB_OWNERDRAW) ) | |
300 | #endif // !wxUSE_OWNER_DRAWN | |
301 | if ( HasClientObjectData() ) | |
302 | { | |
303 | delete GetClientObject(N); | |
304 | } | |
6c8a980f | 305 | |
a23fd0e1 | 306 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); |
dd3c394a VZ |
307 | m_noItems--; |
308 | ||
2b5f62a0 | 309 | SetHorizontalExtent(wxEmptyString); |
2bda0e17 KB |
310 | } |
311 | ||
2ee3ee1b | 312 | int wxListBox::DoAppend(const wxString& item) |
2bda0e17 | 313 | { |
a23fd0e1 | 314 | int index = ListBox_AddString(GetHwnd(), item); |
2ee3ee1b | 315 | m_noItems++; |
2bda0e17 | 316 | |
47d67540 | 317 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 318 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
2b5f62a0 | 319 | wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument |
dd3c394a | 320 | pNewItem->SetName(item); |
fd7ab28c | 321 | m_aItems.Insert(pNewItem, index); |
a23fd0e1 | 322 | ListBox_SetItemData(GetHwnd(), index, pNewItem); |
60c65519 | 323 | pNewItem->SetFont(GetFont()); |
2bda0e17 | 324 | } |
fd7ab28c | 325 | #endif // wxUSE_OWNER_DRAWN |
2bda0e17 | 326 | |
dd3c394a | 327 | SetHorizontalExtent(item); |
dd3c394a | 328 | |
2ee3ee1b | 329 | return index; |
2bda0e17 KB |
330 | } |
331 | ||
2ee3ee1b | 332 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
2bda0e17 | 333 | { |
f6bcfd97 BP |
334 | // avoid flicker - but don't need to do this for a hidden listbox |
335 | bool hideAndShow = IsShown(); | |
336 | if ( hideAndShow ) | |
337 | { | |
338 | ShowWindow(GetHwnd(), SW_HIDE); | |
339 | } | |
2ee3ee1b | 340 | |
a23fd0e1 | 341 | ListBox_ResetContent(GetHwnd()); |
2ee3ee1b VZ |
342 | |
343 | m_noItems = choices.GetCount(); | |
dd3c394a | 344 | int i; |
2ee3ee1b | 345 | for (i = 0; i < m_noItems; i++) |
dd3c394a | 346 | { |
a23fd0e1 | 347 | ListBox_AddString(GetHwnd(), choices[i]); |
dd3c394a | 348 | if ( clientData ) |
2ee3ee1b | 349 | { |
2b5f62a0 | 350 | SetClientData(i, clientData[i]); |
2ee3ee1b | 351 | } |
dd3c394a | 352 | } |
2bda0e17 | 353 | |
47d67540 | 354 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 355 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
dd3c394a | 356 | // first delete old items |
fd7ab28c | 357 | WX_CLEAR_ARRAY(m_aItems); |
dd3c394a VZ |
358 | |
359 | // then create new ones | |
fd7ab28c | 360 | for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) { |
2b5f62a0 | 361 | wxOwnerDrawn *pNewItem = CreateLboxItem(ui); |
dd3c394a VZ |
362 | pNewItem->SetName(choices[ui]); |
363 | m_aItems.Add(pNewItem); | |
a23fd0e1 | 364 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); |
dd3c394a | 365 | } |
2bda0e17 | 366 | } |
2ee3ee1b VZ |
367 | #endif // wxUSE_OWNER_DRAWN |
368 | ||
369 | SetHorizontalExtent(); | |
2bda0e17 | 370 | |
f6bcfd97 BP |
371 | if ( hideAndShow ) |
372 | { | |
373 | // show the listbox back if we hid it | |
374 | ShowWindow(GetHwnd(), SW_SHOW); | |
375 | } | |
2bda0e17 KB |
376 | } |
377 | ||
378 | int wxListBox::FindString(const wxString& s) const | |
379 | { | |
a23fd0e1 | 380 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); |
dd3c394a | 381 | if (pos == LB_ERR) |
2ee3ee1b | 382 | return wxNOT_FOUND; |
dd3c394a VZ |
383 | else |
384 | return pos; | |
2bda0e17 KB |
385 | } |
386 | ||
bfc6fde4 | 387 | void wxListBox::Clear() |
2bda0e17 | 388 | { |
baccb514 | 389 | Free(); |
8ee9d618 VZ |
390 | |
391 | ListBox_ResetContent(GetHwnd()); | |
392 | ||
393 | m_noItems = 0; | |
394 | SetHorizontalExtent(); | |
395 | } | |
396 | ||
397 | void wxListBox::Free() | |
2b273975 | 398 | { |
dd3c394a | 399 | #if wxUSE_OWNER_DRAWN |
185fa6bf VZ |
400 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
401 | { | |
fd7ab28c | 402 | WX_CLEAR_ARRAY(m_aItems); |
185fa6bf VZ |
403 | } |
404 | else | |
405 | #endif // wxUSE_OWNER_DRAWN | |
6c8a980f VZ |
406 | if ( HasClientObjectData() ) |
407 | { | |
408 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
409 | { | |
410 | delete GetClientObject(n); | |
411 | } | |
412 | } | |
2bda0e17 | 413 | } |
baccb514 | 414 | |
debe6624 | 415 | void wxListBox::SetSelection(int N, bool select) |
2bda0e17 | 416 | { |
5955710c JS |
417 | wxCHECK_RET( N == wxNOT_FOUND || |
418 | (N >= 0 && N < m_noItems), | |
223d09f6 | 419 | wxT("invalid index in wxListBox::SetSelection") ); |
dd3c394a | 420 | |
2ee3ee1b VZ |
421 | if ( HasMultipleSelection() ) |
422 | { | |
a23fd0e1 | 423 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
2ee3ee1b | 424 | } |
dd3c394a VZ |
425 | else |
426 | { | |
2ee3ee1b | 427 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
dd3c394a | 428 | } |
2bda0e17 KB |
429 | } |
430 | ||
2ee3ee1b | 431 | bool wxListBox::IsSelected(int N) const |
2bda0e17 | 432 | { |
dd3c394a | 433 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
223d09f6 | 434 | wxT("invalid index in wxListBox::Selected") ); |
dd3c394a | 435 | |
a23fd0e1 | 436 | return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; |
2bda0e17 KB |
437 | } |
438 | ||
6c8a980f | 439 | wxClientData* wxListBox::DoGetItemClientObject(int n) const |
2bda0e17 | 440 | { |
6c8a980f | 441 | return (wxClientData *)DoGetItemClientData(n); |
2bda0e17 KB |
442 | } |
443 | ||
6c8a980f | 444 | void *wxListBox::DoGetItemClientData(int n) const |
2bda0e17 | 445 | { |
2ee3ee1b | 446 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, |
223d09f6 | 447 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 448 | |
2ee3ee1b | 449 | return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
2bda0e17 KB |
450 | } |
451 | ||
6c8a980f | 452 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) |
2bda0e17 | 453 | { |
6c8a980f | 454 | DoSetItemClientData(n, clientData); |
2ee3ee1b VZ |
455 | } |
456 | ||
6c8a980f | 457 | void wxListBox::DoSetItemClientData(int n, void *clientData) |
2ee3ee1b VZ |
458 | { |
459 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
223d09f6 | 460 | wxT("invalid index in wxListBox::SetClientData") ); |
dd3c394a | 461 | |
2ee3ee1b VZ |
462 | #if wxUSE_OWNER_DRAWN |
463 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
464 | { | |
465 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
466 | // in OnMeasure/OnDraw. | |
467 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
468 | } | |
469 | #endif // wxUSE_OWNER_DRAWN | |
470 | ||
471 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
223d09f6 | 472 | wxLogDebug(wxT("LB_SETITEMDATA failed")); |
2bda0e17 KB |
473 | } |
474 | ||
475 | // Return number of selections and an array of selected integers | |
14483330 | 476 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 477 | { |
dd3c394a | 478 | aSelections.Empty(); |
14483330 | 479 | |
2ee3ee1b | 480 | if ( HasMultipleSelection() ) |
dd3c394a | 481 | { |
d90879fa VZ |
482 | int countSel = ListBox_GetSelCount(GetHwnd()); |
483 | if ( countSel == LB_ERR ) | |
484 | { | |
485 | wxLogDebug(_T("ListBox_GetSelCount failed")); | |
486 | } | |
487 | else if ( countSel != 0 ) | |
488 | { | |
489 | int *selections = new int[countSel]; | |
14483330 | 490 | |
d90879fa VZ |
491 | if ( ListBox_GetSelItems(GetHwnd(), |
492 | countSel, selections) == LB_ERR ) | |
493 | { | |
494 | wxLogDebug(wxT("ListBox_GetSelItems failed")); | |
495 | countSel = -1; | |
496 | } | |
497 | else | |
498 | { | |
499 | aSelections.Alloc(countSel); | |
500 | for ( int n = 0; n < countSel; n++ ) | |
501 | aSelections.Add(selections[n]); | |
502 | } | |
14483330 | 503 | |
dd3c394a VZ |
504 | delete [] selections; |
505 | } | |
14483330 | 506 | |
d90879fa | 507 | return countSel; |
dd3c394a VZ |
508 | } |
509 | else // single-selection listbox | |
510 | { | |
f6bcfd97 BP |
511 | if (ListBox_GetCurSel(GetHwnd()) > -1) |
512 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); | |
14483330 | 513 | |
f6bcfd97 | 514 | return aSelections.Count(); |
dd3c394a | 515 | } |
2bda0e17 KB |
516 | } |
517 | ||
518 | // Get single selection, for single choice list items | |
14483330 | 519 | int wxListBox::GetSelection() const |
2bda0e17 | 520 | { |
2ee3ee1b | 521 | wxCHECK_MSG( !HasMultipleSelection(), |
dd3c394a | 522 | -1, |
f6bcfd97 | 523 | wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); |
14483330 | 524 | |
a23fd0e1 | 525 | return ListBox_GetCurSel(GetHwnd()); |
2bda0e17 KB |
526 | } |
527 | ||
528 | // Find string for position | |
debe6624 | 529 | wxString wxListBox::GetString(int N) const |
2bda0e17 | 530 | { |
2b5f62a0 | 531 | wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString, |
223d09f6 | 532 | wxT("invalid index in wxListBox::GetClientData") ); |
dd3c394a | 533 | |
a23fd0e1 | 534 | int len = ListBox_GetTextLen(GetHwnd(), N); |
dd3c394a VZ |
535 | |
536 | // +1 for terminating NUL | |
537 | wxString result; | |
de564874 | 538 | ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1)); |
dd3c394a VZ |
539 | |
540 | return result; | |
2bda0e17 KB |
541 | } |
542 | ||
2ee3ee1b VZ |
543 | void |
544 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
545 | { | |
546 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
547 | wxT("invalid index in wxListBox::InsertItems") ); | |
548 | ||
549 | int nItems = items.GetCount(); | |
550 | for ( int i = 0; i < nItems; i++ ) | |
fd7ab28c VZ |
551 | { |
552 | int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
553 | ||
554 | #if wxUSE_OWNER_DRAWN | |
9085d634 RD |
555 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
556 | { | |
2b5f62a0 | 557 | wxOwnerDrawn *pNewItem = CreateLboxItem(idx); |
9085d634 RD |
558 | pNewItem->SetName(items[i]); |
559 | pNewItem->SetFont(GetFont()); | |
560 | m_aItems.Insert(pNewItem, idx); | |
fd7ab28c | 561 | |
9085d634 RD |
562 | ListBox_SetItemData(GetHwnd(), idx, pNewItem); |
563 | } | |
fd7ab28c VZ |
564 | #endif // wxUSE_OWNER_DRAWN |
565 | } | |
566 | ||
2ee3ee1b VZ |
567 | m_noItems += nItems; |
568 | ||
569 | SetHorizontalExtent(); | |
570 | } | |
571 | ||
572 | void wxListBox::SetString(int N, const wxString& s) | |
573 | { | |
574 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
575 | wxT("invalid index in wxListBox::SetString") ); | |
576 | ||
577 | // remember the state of the item | |
578 | bool wasSelected = IsSelected(N); | |
579 | ||
580 | void *oldData = NULL; | |
581 | wxClientData *oldObjData = NULL; | |
1e6feb95 | 582 | if ( m_clientDataItemsType == wxClientData_Void ) |
2ee3ee1b | 583 | oldData = GetClientData(N); |
1e6feb95 | 584 | else if ( m_clientDataItemsType == wxClientData_Object ) |
2ee3ee1b VZ |
585 | oldObjData = GetClientObject(N); |
586 | ||
587 | // delete and recreate it | |
588 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
589 | ||
590 | int newN = N; | |
591 | if ( N == m_noItems - 1 ) | |
592 | newN = -1; | |
593 | ||
594 | ListBox_InsertString(GetHwnd(), newN, s); | |
595 | ||
596 | // restore the client data | |
597 | if ( oldData ) | |
598 | SetClientData(N, oldData); | |
599 | else if ( oldObjData ) | |
600 | SetClientObject(N, oldObjData); | |
601 | ||
602 | // we may have lost the selection | |
603 | if ( wasSelected ) | |
604 | Select(N); | |
605 | ||
606 | #if wxUSE_OWNER_DRAWN | |
607 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
c294f450 | 608 | { |
2ee3ee1b VZ |
609 | // update item's text |
610 | m_aItems[N]->SetName(s); | |
fd7ab28c | 611 | |
c294f450 RD |
612 | // reassign the item's data |
613 | ListBox_SetItemData(GetHwnd(), N, m_aItems[N]); | |
614 | } | |
2ee3ee1b VZ |
615 | #endif //USE_OWNER_DRAWN |
616 | } | |
617 | ||
618 | int wxListBox::GetCount() const | |
619 | { | |
620 | return m_noItems; | |
621 | } | |
622 | ||
623 | // ---------------------------------------------------------------------------- | |
624 | // helpers | |
625 | // ---------------------------------------------------------------------------- | |
626 | ||
4438caf4 VZ |
627 | // Windows-specific code to set the horizontal extent of the listbox, if |
628 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. | |
2bda0e17 KB |
629 | // Otherwise, all strings are used. |
630 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
631 | { | |
dd3c394a VZ |
632 | // Only necessary if we want a horizontal scrollbar |
633 | if (!(m_windowStyle & wxHSCROLL)) | |
634 | return; | |
635 | TEXTMETRIC lpTextMetric; | |
636 | ||
2ee3ee1b | 637 | if ( !s.IsEmpty() ) |
2bda0e17 | 638 | { |
a23fd0e1 VZ |
639 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); |
640 | HDC dc = GetWindowDC(GetHwnd()); | |
dd3c394a VZ |
641 | HFONT oldFont = 0; |
642 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
643 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
644 | ||
645 | GetTextMetrics(dc, &lpTextMetric); | |
646 | SIZE extentXY; | |
837e5743 | 647 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); |
dd3c394a VZ |
648 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
649 | ||
650 | if (oldFont) | |
651 | ::SelectObject(dc, oldFont); | |
652 | ||
a23fd0e1 | 653 | ReleaseDC(GetHwnd(), dc); |
dd3c394a | 654 | if (extentX > existingExtent) |
a23fd0e1 | 655 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); |
dd3c394a VZ |
656 | } |
657 | else | |
658 | { | |
659 | int largestExtent = 0; | |
a23fd0e1 | 660 | HDC dc = GetWindowDC(GetHwnd()); |
dd3c394a VZ |
661 | HFONT oldFont = 0; |
662 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
663 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
664 | ||
665 | GetTextMetrics(dc, &lpTextMetric); | |
3e3be693 VZ |
666 | |
667 | // FIXME: buffer overflow!! | |
668 | wxChar buf[1024]; | |
669 | for (int i = 0; i < m_noItems; i++) | |
dd3c394a | 670 | { |
3e3be693 VZ |
671 | int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf); |
672 | buf[len] = 0; | |
dd3c394a | 673 | SIZE extentXY; |
3e3be693 | 674 | ::GetTextExtentPoint(dc, buf, len, &extentXY); |
dd3c394a VZ |
675 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); |
676 | if (extentX > largestExtent) | |
677 | largestExtent = extentX; | |
678 | } | |
679 | if (oldFont) | |
680 | ::SelectObject(dc, oldFont); | |
681 | ||
a23fd0e1 VZ |
682 | ReleaseDC(GetHwnd(), dc); |
683 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
2bda0e17 | 684 | } |
2bda0e17 KB |
685 | } |
686 | ||
f68586e5 | 687 | wxSize wxListBox::DoGetBestSize() const |
b908d224 VZ |
688 | { |
689 | // find the widest string | |
690 | int wLine; | |
691 | int wListbox = 0; | |
692 | for ( int i = 0; i < m_noItems; i++ ) | |
693 | { | |
694 | wxString str(GetString(i)); | |
695 | GetTextExtent(str, &wLine, NULL); | |
696 | if ( wLine > wListbox ) | |
697 | wListbox = wLine; | |
698 | } | |
699 | ||
700 | // give it some reasonable default value if there are no strings in the | |
701 | // list | |
702 | if ( wListbox == 0 ) | |
703 | wListbox = 100; | |
704 | ||
705 | // the listbox should be slightly larger than the widest string | |
706 | int cx, cy; | |
707 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
708 | ||
709 | wListbox += 3*cx; | |
710 | ||
5b6d6b70 VZ |
711 | // don't make the listbox too tall (limit height to 10 items) but don't |
712 | // make it too small neither | |
713 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)* | |
714 | wxMin(wxMax(m_noItems, 3), 10); | |
b908d224 VZ |
715 | |
716 | return wxSize(wListbox, hListbox); | |
717 | } | |
718 | ||
2ee3ee1b VZ |
719 | // ---------------------------------------------------------------------------- |
720 | // callbacks | |
721 | // ---------------------------------------------------------------------------- | |
722 | ||
723 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
2bda0e17 | 724 | { |
bada28f0 | 725 | wxEventType evtType; |
8ee9d618 | 726 | if ( param == LBN_SELCHANGE ) |
2bda0e17 | 727 | { |
bada28f0 | 728 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; |
2bda0e17 | 729 | } |
8ee9d618 | 730 | else if ( param == LBN_DBLCLK ) |
2ee3ee1b | 731 | { |
bada28f0 VZ |
732 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
733 | } | |
734 | else | |
735 | { | |
736 | // some event we're not interested in | |
737 | return FALSE; | |
738 | } | |
739 | ||
740 | wxCommandEvent event(evtType, m_windowId); | |
741 | event.SetEventObject( this ); | |
8ee9d618 | 742 | |
9c9c3d7a VZ |
743 | // retrieve the affected item |
744 | int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); | |
745 | if ( n != LB_ERR ) | |
bada28f0 | 746 | { |
bada28f0 VZ |
747 | if ( HasClientObjectData() ) |
748 | event.SetClientObject( GetClientObject(n) ); | |
749 | else if ( HasClientUntypedData() ) | |
750 | event.SetClientData( GetClientData(n) ); | |
9c9c3d7a | 751 | |
bada28f0 | 752 | event.SetString( GetString(n) ); |
9c9c3d7a | 753 | event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE ); |
bada28f0 VZ |
754 | } |
755 | ||
756 | event.m_commandInt = n; | |
2ee3ee1b | 757 | |
bada28f0 | 758 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
759 | } |
760 | ||
2ee3ee1b VZ |
761 | // ---------------------------------------------------------------------------- |
762 | // wxCheckListBox support | |
763 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 764 | |
47d67540 | 765 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
766 | |
767 | // drawing | |
768 | // ------- | |
769 | ||
770 | // space beneath/above each row in pixels | |
771 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
772 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
773 | ||
774 | // the height is the same for all items | |
dd3c394a VZ |
775 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes |
776 | ||
777 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
778 | // message is not yet sent when we get here! | |
2bda0e17 KB |
779 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
780 | { | |
dd3c394a VZ |
781 | // only owner-drawn control should receive this message |
782 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
2bda0e17 | 783 | |
dd3c394a | 784 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
2bda0e17 | 785 | |
4676948b JS |
786 | #ifdef __WXWINCE__ |
787 | HDC hdc = GetDC(NULL); | |
788 | #else | |
07cf98cb | 789 | HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); |
4676948b | 790 | #endif |
07cf98cb | 791 | |
dd3c394a | 792 | wxDC dc; |
07cf98cb | 793 | dc.SetHDC((WXHDC)hdc); |
a756f210 | 794 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT)); |
2bda0e17 | 795 | |
dd3c394a VZ |
796 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
797 | pStruct->itemWidth = dc.GetCharWidth(); | |
2bda0e17 | 798 | |
07cf98cb VZ |
799 | dc.SetHDC(0); |
800 | ||
801 | DeleteDC(hdc); | |
802 | ||
dd3c394a | 803 | return TRUE; |
2bda0e17 KB |
804 | } |
805 | ||
806 | // forward the message to the appropriate item | |
807 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
808 | { | |
dd3c394a VZ |
809 | // only owner-drawn control should receive this message |
810 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
811 | ||
812 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
f6bcfd97 BP |
813 | UINT itemID = pStruct->itemID; |
814 | ||
815 | // the item may be -1 for an empty listbox | |
816 | if ( itemID == (UINT)-1 ) | |
817 | return FALSE; | |
dd3c394a | 818 | |
a23fd0e1 | 819 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); |
2bda0e17 | 820 | |
dd3c394a | 821 | wxCHECK( data && (data != LB_ERR), FALSE ); |
2bda0e17 | 822 | |
dd3c394a | 823 | wxListBoxItem *pItem = (wxListBoxItem *)data; |
2bda0e17 | 824 | |
7561aacd | 825 | wxDCTemp dc((WXHDC)pStruct->hDC); |
dd3c394a | 826 | wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), |
f6bcfd97 | 827 | wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); |
2bda0e17 | 828 | |
dd3c394a | 829 | return pItem->OnDrawItem(dc, rect, |
7561aacd VZ |
830 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, |
831 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
2bda0e17 KB |
832 | } |
833 | ||
1e6feb95 VZ |
834 | #endif // wxUSE_OWNER_DRAWN |
835 | ||
836 | #endif // wxUSE_LISTBOX |