]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
fb9010ed | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
fb9010ed | 6 | // Created: 10/09/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
fb9010ed | 8 | // Copyright: (c) David Webster |
0e320a79 DW |
9 | // Licence: wxWindows licence |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
fb9010ed DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/window.h" | |
16 | #include "wx/os2/private.h" | |
0e320a79 | 17 | |
fb9010ed | 18 | #ifndef WX_PRECOMP |
0e320a79 DW |
19 | #include "wx/listbox.h" |
20 | #include "wx/settings.h" | |
fb9010ed DW |
21 | #include "wx/brush.h" |
22 | #include "wx/font.h" | |
23 | #include "wx/dc.h" | |
24 | #include "wx/utils.h" | |
a4a16252 | 25 | #include "wx/scrolwin.h" |
fb9010ed DW |
26 | #endif |
27 | ||
28 | #define INCL_M | |
29 | #include <os2.h> | |
30 | ||
0e320a79 DW |
31 | #include "wx/dynarray.h" |
32 | #include "wx/log.h" | |
33 | ||
7e99520b DW |
34 | #if wxUSE_LISTBOX |
35 | ||
fb9010ed DW |
36 | #if wxUSE_OWNER_DRAWN |
37 | #include "wx/ownerdrw.h" | |
38 | #endif | |
39 | ||
0e320a79 | 40 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
0e320a79 | 41 | |
fb9010ed DW |
42 | // ============================================================================ |
43 | // list box item declaration and implementation | |
44 | // ============================================================================ | |
45 | ||
46 | #if wxUSE_OWNER_DRAWN | |
47 | ||
48 | class wxListBoxItem : public wxOwnerDrawn | |
49 | { | |
50 | public: | |
49dc8caa | 51 | wxListBoxItem(const wxString& rsStr = ""); |
fb9010ed DW |
52 | }; |
53 | ||
49dc8caa DW |
54 | wxListBoxItem::wxListBoxItem( |
55 | const wxString& rsStr | |
56 | ) | |
57 | : wxOwnerDrawn( rsStr | |
58 | ,FALSE | |
59 | ) | |
fb9010ed | 60 | { |
49dc8caa DW |
61 | // |
62 | // No bitmaps/checkmarks | |
63 | // | |
fb9010ed | 64 | SetMarginWidth(0); |
49dc8caa | 65 | } // end of wxListBoxItem::wxListBoxItem |
fb9010ed | 66 | |
49dc8caa DW |
67 | wxOwnerDrawn* wxListBox::CreateItem( |
68 | size_t n | |
69 | ) | |
fb9010ed DW |
70 | { |
71 | return new wxListBoxItem(); | |
49dc8caa | 72 | } // end of wxListBox::CreateItem |
fb9010ed DW |
73 | |
74 | #endif //USE_OWNER_DRAWN | |
75 | ||
0e320a79 DW |
76 | // ============================================================================ |
77 | // list box control implementation | |
78 | // ============================================================================ | |
79 | ||
80 | // Listbox item | |
81 | wxListBox::wxListBox() | |
82 | { | |
49dc8caa DW |
83 | m_nNumItems = 0; |
84 | m_nSelected = 0; | |
85 | } // end of wxListBox::wxListBox | |
86 | ||
87 | bool wxListBox::Create( | |
88 | wxWindow* pParent | |
89 | , wxWindowID vId | |
90 | , const wxPoint& rPos | |
91 | , const wxSize& rSize | |
92 | , int n | |
93 | , const wxString asChoices[] | |
94 | , long lStyle | |
5d4b632b | 95 | #if wxUSE_VALIDATORS |
49dc8caa | 96 | , const wxValidator& rValidator |
5d4b632b | 97 | #endif |
49dc8caa DW |
98 | , const wxString& rsName |
99 | ) | |
0e320a79 | 100 | { |
49dc8caa DW |
101 | m_nNumItems = 0; |
102 | m_hWnd = 0; | |
103 | m_nSelected = 0; | |
0e320a79 | 104 | |
49dc8caa | 105 | SetName(rsName); |
5d4b632b | 106 | #if wxUSE_VALIDATORS |
49dc8caa | 107 | SetValidator(rValidator); |
5d4b632b | 108 | #endif |
0e320a79 | 109 | |
49dc8caa DW |
110 | if (pParent) |
111 | pParent->AddChild(this); | |
112 | ||
113 | wxSystemSettings vSettings; | |
114 | ||
115 | SetBackgroundColour(vSettings.GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
116 | SetForegroundColour(pParent->GetForegroundColour()); | |
0e320a79 | 117 | |
49dc8caa | 118 | m_windowId = (vId == -1) ? (int)NewControlId() : vId; |
0e320a79 | 119 | |
49dc8caa DW |
120 | int nX = rPos.x; |
121 | int nY = rPos.y; | |
122 | int nWidth = rSize.x; | |
123 | int nHeight = rSize.y; | |
0e320a79 | 124 | |
49dc8caa | 125 | m_windowStyle = lStyle; |
fb9010ed | 126 | |
49dc8caa DW |
127 | lStyle = WS_VISIBLE; |
128 | ||
129 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
130 | lStyle |= WS_CLIPSIBLINGS; | |
fb9010ed | 131 | if (m_windowStyle & wxLB_MULTIPLE) |
49dc8caa | 132 | lStyle |= LS_MULTIPLESEL; |
fb9010ed | 133 | else if (m_windowStyle & wxLB_EXTENDED) |
49dc8caa | 134 | lStyle |= LS_EXTENDEDSEL; |
fb9010ed | 135 | if (m_windowStyle & wxLB_HSCROLL) |
49dc8caa DW |
136 | lStyle |= LS_HORZSCROLL; |
137 | if (m_windowStyle & wxLB_OWNERDRAW) | |
138 | lStyle |= LS_OWNERDRAW; | |
fb9010ed | 139 | |
49dc8caa | 140 | // |
fb9010ed DW |
141 | // Without this style, you get unexpected heights, so e.g. constraint layout |
142 | // doesn't work properly | |
49dc8caa DW |
143 | // |
144 | lStyle |= LS_NOADJUSTPOS; | |
145 | ||
146 | m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) // Parent | |
147 | ,WC_LISTBOX // Default Listbox class | |
148 | ,"LISTBOX" // Control's name | |
149 | ,lStyle // Initial Style | |
150 | ,0, 0, 0, 0 // Position and size | |
151 | ,GetWinHwnd(pParent) // Owner | |
152 | ,HWND_TOP // Z-Order | |
153 | ,(HMENU)m_windowId // Id | |
154 | ,NULL // Control Data | |
155 | ,NULL // Presentation Parameters | |
156 | ); | |
157 | if (m_hWnd == 0) | |
fb9010ed | 158 | { |
49dc8caa | 159 | return FALSE; |
fb9010ed DW |
160 | } |
161 | ||
49dc8caa DW |
162 | // |
163 | // Subclass again for purposes of dialog editing mode | |
164 | // | |
fb9010ed DW |
165 | SubclassWin(m_hWnd); |
166 | ||
49dc8caa | 167 | LONG lUi; |
0e320a79 | 168 | |
49dc8caa DW |
169 | for (lUi = 0; lUi < (LONG)n; lUi++) |
170 | { | |
171 | Append(asChoices[lUi]); | |
172 | } | |
b3260bce DW |
173 | wxFont* pTextFont = new wxFont( 10 |
174 | ,wxMODERN | |
175 | ,wxNORMAL | |
176 | ,wxNORMAL | |
177 | ); | |
178 | SetFont(*pTextFont); | |
e58dab20 | 179 | |
5d44b24e DW |
180 | // |
181 | // Set standard wxWindows colors for Listbox items and highlighting | |
182 | // | |
183 | wxColour vColour; | |
184 | ||
185 | vColour.Set(wxString("WHITE")); | |
186 | ||
187 | LONG lColor = (LONG)vColour.GetPixel(); | |
188 | ||
189 | ::WinSetPresParam( m_hWnd | |
190 | ,PP_HILITEFOREGROUNDCOLOR | |
191 | ,sizeof(LONG) | |
192 | ,(PVOID)&lColor | |
193 | ); | |
194 | vColour.Set(wxString("NAVY")); | |
195 | lColor = (LONG)vColour.GetPixel(); | |
196 | ::WinSetPresParam( m_hWnd | |
197 | ,PP_HILITEBACKGROUNDCOLOR | |
198 | ,sizeof(LONG) | |
199 | ,(PVOID)&lColor | |
200 | ); | |
201 | ||
49dc8caa DW |
202 | SetSize( nX |
203 | ,nY | |
204 | ,nWidth | |
205 | ,nHeight | |
206 | ); | |
b3260bce | 207 | delete pTextFont; |
dcd307ee | 208 | return TRUE; |
49dc8caa | 209 | } // end of wxListBox::Create |
0e320a79 DW |
210 | |
211 | wxListBox::~wxListBox() | |
212 | { | |
fb9010ed | 213 | #if wxUSE_OWNER_DRAWN |
49dc8caa DW |
214 | size_t lUiCount = m_aItems.Count(); |
215 | ||
216 | while (lUiCount-- != 0) | |
217 | { | |
218 | delete m_aItems[lUiCount]; | |
fb9010ed DW |
219 | } |
220 | #endif // wxUSE_OWNER_DRAWN | |
49dc8caa | 221 | } // end of wxListBox::~wxListBox |
fb9010ed DW |
222 | |
223 | void wxListBox::SetupColours() | |
224 | { | |
a756f210 | 225 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
fb9010ed | 226 | SetForegroundColour(GetParent()->GetForegroundColour()); |
49dc8caa | 227 | } // end of wxListBox::SetupColours |
0e320a79 | 228 | |
dcd307ee DW |
229 | // ---------------------------------------------------------------------------- |
230 | // implementation of wxListBoxBase methods | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
49dc8caa DW |
233 | void wxListBox::DoSetFirstItem( |
234 | int N | |
235 | ) | |
0e320a79 | 236 | { |
49dc8caa | 237 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
fb9010ed DW |
238 | wxT("invalid index in wxListBox::SetFirstItem") ); |
239 | ||
49dc8caa DW |
240 | ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX, MPFROMLONG(N), (MPARAM)0); |
241 | } // end of wxListBox::DoSetFirstItem | |
0e320a79 | 242 | |
49dc8caa DW |
243 | void wxListBox::Delete( |
244 | int N | |
245 | ) | |
0e320a79 | 246 | { |
49dc8caa | 247 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
fb9010ed DW |
248 | wxT("invalid index in wxListBox::Delete") ); |
249 | ||
dcd307ee DW |
250 | #if wxUSE_OWNER_DRAWN |
251 | delete m_aItems[N]; | |
893758d5 | 252 | m_aItems.RemoveAt(N); |
dcd307ee | 253 | #else // !wxUSE_OWNER_DRAWN |
49dc8caa | 254 | if (HasClientObjectData()) |
dcd307ee DW |
255 | { |
256 | delete GetClientObject(N); | |
257 | } | |
258 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
259 | ||
49dc8caa DW |
260 | ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)N, (MPARAM)0); |
261 | m_nNumItems--; | |
262 | } // end of wxListBox::DoSetFirstItem | |
0e320a79 | 263 | |
49dc8caa DW |
264 | int wxListBox::DoAppend( |
265 | const wxString& rsItem | |
266 | ) | |
0e320a79 | 267 | { |
49dc8caa DW |
268 | int nIndex = 0; |
269 | SHORT nIndexType = 0; | |
270 | ||
271 | if (m_windowStyle & wxLB_SORT) | |
272 | nIndexType = LIT_SORTASCENDING; | |
273 | else | |
274 | nIndexType = LIT_END; | |
275 | nIndex = (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)rsItem.c_str()); | |
276 | m_nNumItems++; | |
fb9010ed DW |
277 | |
278 | #if wxUSE_OWNER_DRAWN | |
49dc8caa DW |
279 | if (m_windowStyle & wxLB_OWNERDRAW) |
280 | { | |
281 | wxOwnerDrawn* pNewItem = CreateItem(nIndex); // dummy argument | |
282 | ||
283 | pNewItem->SetName(rsItem); | |
3437f881 | 284 | m_aItems.Insert(pNewItem, nIndex); |
49dc8caa DW |
285 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)((SHORT)nIndex), MPFROMP(pNewItem)); |
286 | pNewItem->SetFont(GetFont()); | |
fb9010ed DW |
287 | } |
288 | #endif | |
49dc8caa DW |
289 | return nIndex; |
290 | } // end of wxListBox::DoAppend | |
0e320a79 | 291 | |
49dc8caa DW |
292 | void wxListBox::DoSetItems( |
293 | const wxArrayString& raChoices | |
294 | , void** ppClientData | |
295 | ) | |
0e320a79 | 296 | { |
49dc8caa DW |
297 | BOOL bHideAndShow = IsShown(); |
298 | int nCount = 0; | |
299 | int i; | |
300 | SHORT nIndexType = 0; | |
0e320a79 | 301 | |
49dc8caa | 302 | if (bHideAndShow) |
fb9010ed | 303 | { |
49dc8caa DW |
304 | ::WinShowWindow(GetHwnd(), FALSE); |
305 | } | |
306 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); | |
307 | m_nNumItems = raChoices.GetCount(); | |
308 | for (i = 0; i < m_nNumItems; i++) | |
309 | { | |
310 | ||
311 | if (m_windowStyle & wxLB_SORT) | |
312 | nIndexType = LIT_SORTASCENDING; | |
313 | else | |
314 | nIndexType = LIT_END; | |
315 | ::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)raChoices[i].c_str()); | |
316 | ||
317 | if (ppClientData) | |
dcd307ee DW |
318 | { |
319 | #if wxUSE_OWNER_DRAWN | |
49dc8caa | 320 | wxASSERT_MSG(ppClientData[i] == NULL, |
dcd307ee DW |
321 | wxT("Can't use client data with owner-drawn listboxes")); |
322 | #else // !wxUSE_OWNER_DRAWN | |
49dc8caa | 323 | ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lCount), MPFROMP(ppClientData[i])); |
dcd307ee DW |
324 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN |
325 | } | |
fb9010ed | 326 | } |
fb9010ed DW |
327 | |
328 | #if wxUSE_OWNER_DRAWN | |
49dc8caa DW |
329 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
330 | { | |
331 | // | |
332 | // First delete old items | |
333 | // | |
3437f881 | 334 | WX_CLEAR_ARRAY(m_aItems); |
fb9010ed | 335 | |
49dc8caa DW |
336 | // |
337 | // Then create new ones | |
338 | // | |
3437f881 | 339 | for (size_t ui = 0; ui < (size_t)m_nNumItems; ui++) |
49dc8caa | 340 | { |
3437f881 | 341 | wxOwnerDrawn* pNewItem = CreateItem(ui); |
49dc8caa | 342 | |
3437f881 | 343 | pNewItem->SetName(raChoices[ui]); |
fb9010ed | 344 | m_aItems.Add(pNewItem); |
3437f881 | 345 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(ui), MPFROMP(pNewItem)); |
fb9010ed DW |
346 | } |
347 | } | |
dcd307ee | 348 | #endif // wxUSE_OWNER_DRAWN |
49dc8caa DW |
349 | ::WinShowWindow(GetHwnd(), TRUE); |
350 | } // end of wxListBox::DoSetItems | |
0e320a79 | 351 | |
49dc8caa DW |
352 | int wxListBox::FindString( |
353 | const wxString& rsString | |
354 | ) const | |
355 | { | |
356 | int nPos; | |
357 | LONG lTextLength; | |
358 | PSZ zStr; | |
dcd307ee | 359 | |
0e320a79 | 360 | |
49dc8caa DW |
361 | for (nPos = 0; nPos < m_nNumItems; nPos++) |
362 | { | |
363 | lTextLength = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0)); | |
364 | zStr = new char[lTextLength + 1]; | |
365 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT(nPos, (SHORT)lTextLength), (MPARAM)zStr); | |
366 | if (rsString == (char*)zStr) | |
367 | { | |
368 | delete [] zStr; | |
369 | break; | |
370 | } | |
371 | delete [] zStr; | |
372 | } | |
373 | return nPos; | |
374 | } // end of wxListBox::FindString | |
0e320a79 DW |
375 | |
376 | void wxListBox::Clear() | |
377 | { | |
fb9010ed | 378 | #if wxUSE_OWNER_DRAWN |
49dc8caa DW |
379 | size_t lUiCount = m_aItems.Count(); |
380 | ||
381 | while (lUiCount-- != 0) | |
382 | { | |
383 | delete m_aItems[lUiCount]; | |
fb9010ed DW |
384 | } |
385 | ||
386 | m_aItems.Clear(); | |
dcd307ee | 387 | #else // !wxUSE_OWNER_DRAWN |
49dc8caa | 388 | if (HasClientObjectData()) |
dcd307ee | 389 | { |
49dc8caa | 390 | for (size_t n = 0; n < (size_t)m_lNumItems; n++) |
dcd307ee DW |
391 | { |
392 | delete GetClientObject(n); | |
393 | } | |
394 | } | |
395 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
49dc8caa | 396 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); |
dcd307ee | 397 | |
49dc8caa DW |
398 | m_nNumItems = 0; |
399 | } // end of wxListBox::Clear | |
fb9010ed | 400 | |
49dc8caa DW |
401 | void wxListBox::SetSelection( |
402 | int N | |
403 | , bool bSelect | |
404 | ) | |
0e320a79 | 405 | { |
49dc8caa | 406 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
fb9010ed | 407 | wxT("invalid index in wxListBox::SetSelection") ); |
49dc8caa DW |
408 | ::WinSendMsg( GetHwnd() |
409 | ,LM_SELECTITEM | |
410 | ,MPFROMLONG(N) | |
411 | ,(MPARAM)bSelect | |
412 | ); | |
413 | } // end of wxListBox::SetSelection | |
414 | ||
415 | bool wxListBox::IsSelected( | |
416 | int N | |
417 | ) const | |
418 | { | |
419 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, FALSE, | |
fb9010ed DW |
420 | wxT("invalid index in wxListBox::Selected") ); |
421 | ||
49dc8caa | 422 | LONG lItem; |
0e320a79 | 423 | |
70a2c656 DW |
424 | if (GetWindowStyleFlag() & wxLB_EXTENDED) |
425 | { | |
426 | if (N == 0) | |
427 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)); | |
428 | else | |
429 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)(N - 1), (MPARAM)0)); | |
430 | } | |
431 | else | |
432 | { | |
433 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)); | |
434 | } | |
435 | return (lItem == (LONG)N && lItem != LIT_NONE); | |
49dc8caa DW |
436 | } // end of wxListBox::IsSelected |
437 | ||
438 | wxClientData* wxListBox::DoGetItemClientObject( | |
439 | int n | |
440 | ) const | |
0e320a79 | 441 | { |
dcd307ee | 442 | return (wxClientData *)DoGetItemClientData(n); |
0e320a79 DW |
443 | } |
444 | ||
49dc8caa DW |
445 | void* wxListBox::DoGetItemClientData( |
446 | int n | |
447 | ) const | |
0e320a79 | 448 | { |
49dc8caa | 449 | wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL, |
fb9010ed DW |
450 | wxT("invalid index in wxListBox::GetClientData") ); |
451 | ||
49dc8caa DW |
452 | return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0)); |
453 | } // end of wxListBox::DoGetItemClientData | |
0e320a79 | 454 | |
49dc8caa DW |
455 | void wxListBox::DoSetItemClientObject( |
456 | int n | |
457 | , wxClientData* pClientData | |
458 | ) | |
0e320a79 | 459 | { |
49dc8caa DW |
460 | DoSetItemClientData( n |
461 | ,pClientData | |
462 | ); | |
463 | } // end of wxListBox::DoSetItemClientObject | |
dcd307ee | 464 | |
49dc8caa DW |
465 | void wxListBox::DoSetItemClientData( |
466 | int n | |
467 | , void* pClientData | |
468 | ) | |
dcd307ee | 469 | { |
49dc8caa | 470 | wxCHECK_RET( n >= 0 && n < m_nNumItems, |
fb9010ed DW |
471 | wxT("invalid index in wxListBox::SetClientData") ); |
472 | ||
dcd307ee DW |
473 | #if wxUSE_OWNER_DRAWN |
474 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
475 | { | |
49dc8caa DW |
476 | // |
477 | // Client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
dcd307ee | 478 | // in OnMeasure/OnDraw. |
49dc8caa | 479 | // |
dcd307ee DW |
480 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); |
481 | } | |
482 | #endif // wxUSE_OWNER_DRAWN | |
483 | ||
49dc8caa DW |
484 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData)); |
485 | } // end of wxListBox::DoSetItemClientData | |
dcd307ee DW |
486 | |
487 | bool wxListBox::HasMultipleSelection() const | |
488 | { | |
489 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
49dc8caa | 490 | } // end of wxListBox::HasMultipleSelection |
0e320a79 | 491 | |
49dc8caa DW |
492 | int wxListBox::GetSelections( |
493 | wxArrayInt& raSelections | |
494 | ) const | |
0e320a79 | 495 | { |
49dc8caa DW |
496 | int nCount = 0; |
497 | LONG lItem; | |
0e320a79 | 498 | |
fb9010ed | 499 | |
49dc8caa DW |
500 | raSelections.Empty(); |
501 | if (HasMultipleSelection()) | |
502 | { | |
503 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
504 | ,LM_QUERYSELECTION | |
505 | ,(MPARAM)LIT_FIRST | |
506 | ,(MPARAM)0 | |
507 | ) | |
508 | ); | |
509 | if (lItem != LIT_NONE) | |
510 | { | |
511 | nCount++; | |
512 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
513 | ,LM_QUERYSELECTION | |
514 | ,(MPARAM)lItem | |
515 | ,(MPARAM)0 | |
516 | ) | |
517 | )) != LIT_NONE) | |
518 | { | |
519 | nCount++; | |
520 | } | |
521 | raSelections.Alloc(nCount); | |
522 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
523 | ,LM_QUERYSELECTION | |
524 | ,(MPARAM)LIT_FIRST | |
525 | ,(MPARAM)0 | |
526 | ) | |
527 | ); | |
528 | ||
529 | raSelections.Add((int)lItem); | |
530 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
531 | ,LM_QUERYSELECTION | |
532 | ,(MPARAM)lItem | |
533 | ,(MPARAM)0 | |
534 | ) | |
535 | )) != LIT_NONE) | |
536 | { | |
537 | raSelections.Add((int)lItem); | |
538 | } | |
539 | return nCount; | |
fb9010ed | 540 | } |
49dc8caa | 541 | return 0; |
0e320a79 DW |
542 | } |
543 | else // single-selection listbox | |
544 | { | |
49dc8caa DW |
545 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() |
546 | ,LM_QUERYSELECTION | |
547 | ,(MPARAM)LIT_FIRST | |
548 | ,(MPARAM)0 | |
549 | ) | |
550 | ); | |
551 | raSelections.Add((int)lItem); | |
0e320a79 DW |
552 | return 1; |
553 | } | |
dcd307ee | 554 | return 0; |
49dc8caa | 555 | } // end of wxListBox::GetSelections |
0e320a79 | 556 | |
0e320a79 DW |
557 | int wxListBox::GetSelection() const |
558 | { | |
dcd307ee | 559 | wxCHECK_MSG( !HasMultipleSelection(), |
fb9010ed DW |
560 | -1, |
561 | wxT("GetSelection() can't be used with multiple-selection " | |
562 | "listboxes, use GetSelections() instead.") ); | |
563 | ||
49dc8caa DW |
564 | return(LONGFROMMR(::WinSendMsg( GetHwnd() |
565 | ,LM_QUERYSELECTION | |
566 | ,(MPARAM)LIT_FIRST | |
567 | ,(MPARAM)0 | |
568 | ) | |
569 | )); | |
570 | } // end of wxListBox::GetSelection | |
0e320a79 | 571 | |
49dc8caa DW |
572 | wxString wxListBox::GetString( |
573 | int N | |
574 | ) const | |
0e320a79 | 575 | { |
49dc8caa DW |
576 | LONG lLen = 0; |
577 | char* zBuf; | |
578 | wxString sResult; | |
fb9010ed | 579 | |
49dc8caa DW |
580 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, "", |
581 | wxT("invalid index in wxListBox::GetClientData") ); | |
0e320a79 | 582 | |
49dc8caa DW |
583 | lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0)); |
584 | zBuf = new char[lLen + 1]; | |
585 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf); | |
586 | zBuf[lLen] = '\0'; | |
587 | sResult = zBuf; | |
588 | delete [] zBuf; | |
589 | return sResult; | |
590 | } // end of wxListBox::GetString | |
591 | ||
592 | void wxListBox::DoInsertItems( | |
593 | const wxArrayString& asItems | |
594 | , int nPos | |
595 | ) | |
596 | { | |
597 | wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems, | |
dcd307ee DW |
598 | wxT("invalid index in wxListBox::InsertItems") ); |
599 | ||
49dc8caa | 600 | int nItems = asItems.GetCount(); |
dcd307ee | 601 | |
49dc8caa | 602 | for (int i = 0; i < nItems; i++) |
3437f881 DW |
603 | { |
604 | int nIndex = (int)::WinSendMsg( GetHwnd() | |
605 | ,LM_INSERTITEM | |
606 | ,MPFROMLONG((LONG)(i + nPos)) | |
607 | ,(MPARAM)asItems[i].c_str() | |
608 | ); | |
609 | ||
610 | wxOwnerDrawn* pNewItem = CreateItem(nIndex); | |
611 | ||
612 | pNewItem->SetName(asItems[i]); | |
613 | pNewItem->SetFont(GetFont()); | |
614 | m_aItems.Insert(pNewItem, nIndex); | |
615 | ::WinSendMsg( GetHwnd() | |
616 | ,LM_SETITEMHANDLE | |
617 | ,(MPARAM)((SHORT)nIndex) | |
618 | ,MPFROMP(pNewItem) | |
619 | ); | |
620 | m_nNumItems += nItems; | |
621 | } | |
49dc8caa | 622 | } // end of wxListBox::DoInsertItems |
dcd307ee | 623 | |
49dc8caa DW |
624 | void wxListBox::SetString( |
625 | int N | |
626 | , const wxString& rsString | |
627 | ) | |
dcd307ee | 628 | { |
49dc8caa | 629 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
dcd307ee DW |
630 | wxT("invalid index in wxListBox::SetString") ); |
631 | ||
49dc8caa DW |
632 | // |
633 | // Remember the state of the item | |
634 | // | |
635 | bool bWasSelected = IsSelected(N); | |
636 | void* pOldData = NULL; | |
637 | wxClientData* pOldObjData = NULL; | |
638 | ||
639 | if (m_clientDataItemsType == wxClientData_Void) | |
640 | pOldData = GetClientData(N); | |
641 | else if (m_clientDataItemsType == wxClientData_Object) | |
642 | pOldObjData = GetClientObject(N); | |
643 | ||
644 | // | |
645 | // Delete and recreate it | |
646 | // | |
647 | ::WinSendMsg( GetHwnd() | |
648 | ,LM_DELETEITEM | |
649 | ,(MPARAM)N | |
650 | ,(MPARAM)0 | |
651 | ); | |
652 | ||
653 | int nNewN = N; | |
654 | ||
655 | if (N == m_nNumItems - 1) | |
656 | nNewN = -1; | |
657 | ||
658 | ::WinSendMsg( GetHwnd() | |
659 | ,LM_INSERTITEM | |
660 | ,(MPARAM)nNewN | |
661 | ,(MPARAM)rsString.c_str() | |
662 | ); | |
663 | ||
664 | // | |
665 | // Restore the client data | |
666 | // | |
667 | if (pOldData) | |
668 | SetClientData( N | |
669 | ,pOldData | |
670 | ); | |
671 | else if (pOldObjData) | |
672 | SetClientObject( N | |
673 | ,pOldObjData | |
674 | ); | |
675 | ||
676 | // | |
677 | // We may have lost the selection | |
678 | // | |
679 | if (bWasSelected) | |
dcd307ee DW |
680 | Select(N); |
681 | ||
682 | #if wxUSE_OWNER_DRAWN | |
49dc8caa DW |
683 | if (m_windowStyle & wxLB_OWNERDRAW) |
684 | // | |
685 | // Update item's text | |
686 | // | |
687 | m_aItems[N]->SetName(rsString); | |
dcd307ee | 688 | #endif //USE_OWNER_DRAWN |
49dc8caa | 689 | } // end of wxListBox::SetString |
dcd307ee DW |
690 | |
691 | int wxListBox::GetCount() const | |
692 | { | |
49dc8caa | 693 | return m_nNumItems; |
dcd307ee DW |
694 | } |
695 | ||
696 | // ---------------------------------------------------------------------------- | |
697 | // helpers | |
698 | // ---------------------------------------------------------------------------- | |
699 | ||
e78c4d50 | 700 | wxSize wxListBox::DoGetBestSize() const |
fb9010ed | 701 | { |
49dc8caa DW |
702 | // |
703 | // Find the widest string | |
704 | // | |
705 | int nLine; | |
706 | int nListbox = 0; | |
707 | int nCx; | |
708 | int nCy; | |
709 | ||
710 | for (int i = 0; i < m_nNumItems; i++) | |
fb9010ed | 711 | { |
49dc8caa DW |
712 | wxString vStr(GetString(i)); |
713 | ||
714 | GetTextExtent( vStr | |
715 | ,&nLine | |
716 | ,NULL | |
717 | ); | |
718 | if (nLine > nListbox) | |
719 | nListbox = nLine; | |
fb9010ed DW |
720 | } |
721 | ||
49dc8caa DW |
722 | // |
723 | // Give it some reasonable default value if there are no strings in the | |
724 | // list. | |
725 | // | |
726 | if (nListbox == 0) | |
727 | nListbox = 100; | |
728 | ||
729 | // | |
730 | // The listbox should be slightly larger than the widest string | |
731 | // | |
732 | wxGetCharSize( GetHWND() | |
733 | ,&nCx | |
734 | ,&nCy | |
735 | ,(wxFont*)&GetFont() | |
736 | ); | |
737 | nListbox += 3 * nCx; | |
738 | ||
739 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7)); | |
740 | ||
741 | return wxSize( nListbox | |
742 | ,hListbox | |
743 | ); | |
744 | } // end of wxListBox::DoGetBestSize | |
fb9010ed | 745 | |
dcd307ee DW |
746 | // ---------------------------------------------------------------------------- |
747 | // callbacks | |
748 | // ---------------------------------------------------------------------------- | |
749 | ||
49dc8caa DW |
750 | bool wxListBox::OS2Command( |
751 | WXUINT uParam | |
752 | , WXWORD WXUNUSED(wId)) | |
0e320a79 | 753 | { |
49dc8caa | 754 | wxEventType eEvtType; |
dcd307ee | 755 | |
49dc8caa DW |
756 | if (uParam == LN_SELECT) |
757 | { | |
758 | eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
dcd307ee | 759 | } |
49dc8caa | 760 | if (uParam == LN_ENTER) |
0e320a79 | 761 | { |
49dc8caa | 762 | eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
0e320a79 | 763 | } |
fb9010ed | 764 | else |
49dc8caa DW |
765 | { |
766 | // | |
767 | // Some event we're not interested in | |
768 | // | |
769 | return FALSE; | |
770 | } | |
771 | wxCommandEvent vEvent( eEvtType | |
772 | ,m_windowId | |
773 | ); | |
fb9010ed | 774 | |
49dc8caa | 775 | vEvent.SetEventObject(this); |
fb9010ed | 776 | |
49dc8caa DW |
777 | wxArrayInt aSelections; |
778 | int n; | |
779 | int nCount = GetSelections(aSelections); | |
fb9010ed | 780 | |
49dc8caa DW |
781 | if (nCount > 0) |
782 | { | |
783 | n = aSelections[0]; | |
784 | if (HasClientObjectData()) | |
785 | vEvent.SetClientObject(GetClientObject(n)); | |
786 | else if ( HasClientUntypedData() ) | |
787 | vEvent.SetClientData(GetClientData(n)); | |
788 | vEvent.SetString(GetString(n)); | |
789 | } | |
790 | else | |
791 | { | |
792 | n = -1; | |
793 | } | |
794 | vEvent.m_commandInt = n; | |
795 | return GetEventHandler()->ProcessEvent(vEvent); | |
796 | } // end of wxListBox::OS2Command | |
fb9010ed | 797 | |
dcd307ee DW |
798 | // ---------------------------------------------------------------------------- |
799 | // wxCheckListBox support | |
800 | // ---------------------------------------------------------------------------- | |
fb9010ed DW |
801 | |
802 | #if wxUSE_OWNER_DRAWN | |
803 | ||
49dc8caa DW |
804 | // |
805 | // Drawing | |
fb9010ed | 806 | // ------- |
49dc8caa | 807 | // |
fb9010ed DW |
808 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) |
809 | ||
fb9010ed DW |
810 | bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT *item) |
811 | { | |
49dc8caa DW |
812 | // |
813 | // TODO: Get to this eventually | |
814 | // | |
fb9010ed DW |
815 | return TRUE; |
816 | } | |
817 | ||
fb9010ed DW |
818 | bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT *item) |
819 | { | |
49dc8caa DW |
820 | // |
821 | // TODO: Get to this eventually | |
822 | // | |
dcd307ee | 823 | return FALSE; |
fb9010ed | 824 | } |
49dc8caa DW |
825 | #endif // ndef for wxUSE_OWNER_DRAWN |
826 | ||
827 | #endif // ndef for wxUSE_LISTBOX | |
7e99520b | 828 |