]>
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); | |
fb9010ed | 284 | m_aItems.Add(pNewItem); |
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 | // | |
334 | size_t lUi = m_aItems.Count(); | |
335 | ||
336 | while (lUi-- != 0) | |
337 | { | |
338 | delete m_aItems[lUi]; | |
fb9010ed DW |
339 | } |
340 | m_aItems.Empty(); | |
341 | ||
49dc8caa DW |
342 | // |
343 | // Then create new ones | |
344 | // | |
345 | for (lUi = 0; lUi < (size_t)m_nNumItems; lUi++) | |
346 | { | |
347 | wxOwnerDrawn* pNewItem = CreateItem(lUi); | |
348 | ||
349 | pNewItem->SetName(raChoices[lUi]); | |
fb9010ed | 350 | m_aItems.Add(pNewItem); |
49dc8caa | 351 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lUi), MPFROMP(pNewItem)); |
fb9010ed DW |
352 | } |
353 | } | |
dcd307ee | 354 | #endif // wxUSE_OWNER_DRAWN |
49dc8caa DW |
355 | ::WinShowWindow(GetHwnd(), TRUE); |
356 | } // end of wxListBox::DoSetItems | |
0e320a79 | 357 | |
49dc8caa DW |
358 | int wxListBox::FindString( |
359 | const wxString& rsString | |
360 | ) const | |
361 | { | |
362 | int nPos; | |
363 | LONG lTextLength; | |
364 | PSZ zStr; | |
dcd307ee | 365 | |
0e320a79 | 366 | |
49dc8caa DW |
367 | for (nPos = 0; nPos < m_nNumItems; nPos++) |
368 | { | |
369 | lTextLength = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0)); | |
370 | zStr = new char[lTextLength + 1]; | |
371 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT(nPos, (SHORT)lTextLength), (MPARAM)zStr); | |
372 | if (rsString == (char*)zStr) | |
373 | { | |
374 | delete [] zStr; | |
375 | break; | |
376 | } | |
377 | delete [] zStr; | |
378 | } | |
379 | return nPos; | |
380 | } // end of wxListBox::FindString | |
0e320a79 DW |
381 | |
382 | void wxListBox::Clear() | |
383 | { | |
fb9010ed | 384 | #if wxUSE_OWNER_DRAWN |
49dc8caa DW |
385 | size_t lUiCount = m_aItems.Count(); |
386 | ||
387 | while (lUiCount-- != 0) | |
388 | { | |
389 | delete m_aItems[lUiCount]; | |
fb9010ed DW |
390 | } |
391 | ||
392 | m_aItems.Clear(); | |
dcd307ee | 393 | #else // !wxUSE_OWNER_DRAWN |
49dc8caa | 394 | if (HasClientObjectData()) |
dcd307ee | 395 | { |
49dc8caa | 396 | for (size_t n = 0; n < (size_t)m_lNumItems; n++) |
dcd307ee DW |
397 | { |
398 | delete GetClientObject(n); | |
399 | } | |
400 | } | |
401 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
49dc8caa | 402 | ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0); |
dcd307ee | 403 | |
49dc8caa DW |
404 | m_nNumItems = 0; |
405 | } // end of wxListBox::Clear | |
fb9010ed | 406 | |
49dc8caa DW |
407 | void wxListBox::SetSelection( |
408 | int N | |
409 | , bool bSelect | |
410 | ) | |
0e320a79 | 411 | { |
49dc8caa | 412 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
fb9010ed | 413 | wxT("invalid index in wxListBox::SetSelection") ); |
49dc8caa DW |
414 | ::WinSendMsg( GetHwnd() |
415 | ,LM_SELECTITEM | |
416 | ,MPFROMLONG(N) | |
417 | ,(MPARAM)bSelect | |
418 | ); | |
419 | } // end of wxListBox::SetSelection | |
420 | ||
421 | bool wxListBox::IsSelected( | |
422 | int N | |
423 | ) const | |
424 | { | |
425 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, FALSE, | |
fb9010ed DW |
426 | wxT("invalid index in wxListBox::Selected") ); |
427 | ||
49dc8caa | 428 | LONG lItem; |
0e320a79 | 429 | |
49dc8caa DW |
430 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)N, (MPARAM)0)); |
431 | return (lItem != LIT_NONE); | |
432 | } // end of wxListBox::IsSelected | |
433 | ||
434 | wxClientData* wxListBox::DoGetItemClientObject( | |
435 | int n | |
436 | ) const | |
0e320a79 | 437 | { |
dcd307ee | 438 | return (wxClientData *)DoGetItemClientData(n); |
0e320a79 DW |
439 | } |
440 | ||
49dc8caa DW |
441 | void* wxListBox::DoGetItemClientData( |
442 | int n | |
443 | ) const | |
0e320a79 | 444 | { |
49dc8caa | 445 | wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL, |
fb9010ed DW |
446 | wxT("invalid index in wxListBox::GetClientData") ); |
447 | ||
49dc8caa DW |
448 | return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0)); |
449 | } // end of wxListBox::DoGetItemClientData | |
0e320a79 | 450 | |
49dc8caa DW |
451 | void wxListBox::DoSetItemClientObject( |
452 | int n | |
453 | , wxClientData* pClientData | |
454 | ) | |
0e320a79 | 455 | { |
49dc8caa DW |
456 | DoSetItemClientData( n |
457 | ,pClientData | |
458 | ); | |
459 | } // end of wxListBox::DoSetItemClientObject | |
dcd307ee | 460 | |
49dc8caa DW |
461 | void wxListBox::DoSetItemClientData( |
462 | int n | |
463 | , void* pClientData | |
464 | ) | |
dcd307ee | 465 | { |
49dc8caa | 466 | wxCHECK_RET( n >= 0 && n < m_nNumItems, |
fb9010ed DW |
467 | wxT("invalid index in wxListBox::SetClientData") ); |
468 | ||
dcd307ee DW |
469 | #if wxUSE_OWNER_DRAWN |
470 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
471 | { | |
49dc8caa DW |
472 | // |
473 | // Client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
dcd307ee | 474 | // in OnMeasure/OnDraw. |
49dc8caa | 475 | // |
dcd307ee DW |
476 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); |
477 | } | |
478 | #endif // wxUSE_OWNER_DRAWN | |
479 | ||
49dc8caa DW |
480 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData)); |
481 | } // end of wxListBox::DoSetItemClientData | |
dcd307ee DW |
482 | |
483 | bool wxListBox::HasMultipleSelection() const | |
484 | { | |
485 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
49dc8caa | 486 | } // end of wxListBox::HasMultipleSelection |
0e320a79 | 487 | |
49dc8caa DW |
488 | int wxListBox::GetSelections( |
489 | wxArrayInt& raSelections | |
490 | ) const | |
0e320a79 | 491 | { |
49dc8caa DW |
492 | int nCount = 0; |
493 | LONG lItem; | |
0e320a79 | 494 | |
fb9010ed | 495 | |
49dc8caa DW |
496 | raSelections.Empty(); |
497 | if (HasMultipleSelection()) | |
498 | { | |
499 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
500 | ,LM_QUERYSELECTION | |
501 | ,(MPARAM)LIT_FIRST | |
502 | ,(MPARAM)0 | |
503 | ) | |
504 | ); | |
505 | if (lItem != LIT_NONE) | |
506 | { | |
507 | nCount++; | |
508 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
509 | ,LM_QUERYSELECTION | |
510 | ,(MPARAM)lItem | |
511 | ,(MPARAM)0 | |
512 | ) | |
513 | )) != LIT_NONE) | |
514 | { | |
515 | nCount++; | |
516 | } | |
517 | raSelections.Alloc(nCount); | |
518 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
519 | ,LM_QUERYSELECTION | |
520 | ,(MPARAM)LIT_FIRST | |
521 | ,(MPARAM)0 | |
522 | ) | |
523 | ); | |
524 | ||
525 | raSelections.Add((int)lItem); | |
526 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
527 | ,LM_QUERYSELECTION | |
528 | ,(MPARAM)lItem | |
529 | ,(MPARAM)0 | |
530 | ) | |
531 | )) != LIT_NONE) | |
532 | { | |
533 | raSelections.Add((int)lItem); | |
534 | } | |
535 | return nCount; | |
fb9010ed | 536 | } |
49dc8caa | 537 | return 0; |
0e320a79 DW |
538 | } |
539 | else // single-selection listbox | |
540 | { | |
49dc8caa DW |
541 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() |
542 | ,LM_QUERYSELECTION | |
543 | ,(MPARAM)LIT_FIRST | |
544 | ,(MPARAM)0 | |
545 | ) | |
546 | ); | |
547 | raSelections.Add((int)lItem); | |
0e320a79 DW |
548 | return 1; |
549 | } | |
dcd307ee | 550 | return 0; |
49dc8caa | 551 | } // end of wxListBox::GetSelections |
0e320a79 | 552 | |
0e320a79 DW |
553 | int wxListBox::GetSelection() const |
554 | { | |
dcd307ee | 555 | wxCHECK_MSG( !HasMultipleSelection(), |
fb9010ed DW |
556 | -1, |
557 | wxT("GetSelection() can't be used with multiple-selection " | |
558 | "listboxes, use GetSelections() instead.") ); | |
559 | ||
49dc8caa DW |
560 | return(LONGFROMMR(::WinSendMsg( GetHwnd() |
561 | ,LM_QUERYSELECTION | |
562 | ,(MPARAM)LIT_FIRST | |
563 | ,(MPARAM)0 | |
564 | ) | |
565 | )); | |
566 | } // end of wxListBox::GetSelection | |
0e320a79 | 567 | |
49dc8caa DW |
568 | wxString wxListBox::GetString( |
569 | int N | |
570 | ) const | |
0e320a79 | 571 | { |
49dc8caa DW |
572 | LONG lLen = 0; |
573 | char* zBuf; | |
574 | wxString sResult; | |
fb9010ed | 575 | |
49dc8caa DW |
576 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, "", |
577 | wxT("invalid index in wxListBox::GetClientData") ); | |
0e320a79 | 578 | |
49dc8caa DW |
579 | lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0)); |
580 | zBuf = new char[lLen + 1]; | |
581 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf); | |
582 | zBuf[lLen] = '\0'; | |
583 | sResult = zBuf; | |
584 | delete [] zBuf; | |
585 | return sResult; | |
586 | } // end of wxListBox::GetString | |
587 | ||
588 | void wxListBox::DoInsertItems( | |
589 | const wxArrayString& asItems | |
590 | , int nPos | |
591 | ) | |
592 | { | |
593 | wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems, | |
dcd307ee DW |
594 | wxT("invalid index in wxListBox::InsertItems") ); |
595 | ||
49dc8caa | 596 | int nItems = asItems.GetCount(); |
dcd307ee | 597 | |
49dc8caa DW |
598 | for (int i = 0; i < nItems; i++) |
599 | ::WinSendMsg(GetHwnd(), LM_INSERTITEM, MPFROMLONG((LONG)(i + nPos)), (MPARAM)asItems[i].c_str()); | |
600 | m_nNumItems += nItems; | |
601 | } // end of wxListBox::DoInsertItems | |
dcd307ee | 602 | |
49dc8caa DW |
603 | void wxListBox::SetString( |
604 | int N | |
605 | , const wxString& rsString | |
606 | ) | |
dcd307ee | 607 | { |
49dc8caa | 608 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
dcd307ee DW |
609 | wxT("invalid index in wxListBox::SetString") ); |
610 | ||
49dc8caa DW |
611 | // |
612 | // Remember the state of the item | |
613 | // | |
614 | bool bWasSelected = IsSelected(N); | |
615 | void* pOldData = NULL; | |
616 | wxClientData* pOldObjData = NULL; | |
617 | ||
618 | if (m_clientDataItemsType == wxClientData_Void) | |
619 | pOldData = GetClientData(N); | |
620 | else if (m_clientDataItemsType == wxClientData_Object) | |
621 | pOldObjData = GetClientObject(N); | |
622 | ||
623 | // | |
624 | // Delete and recreate it | |
625 | // | |
626 | ::WinSendMsg( GetHwnd() | |
627 | ,LM_DELETEITEM | |
628 | ,(MPARAM)N | |
629 | ,(MPARAM)0 | |
630 | ); | |
631 | ||
632 | int nNewN = N; | |
633 | ||
634 | if (N == m_nNumItems - 1) | |
635 | nNewN = -1; | |
636 | ||
637 | ::WinSendMsg( GetHwnd() | |
638 | ,LM_INSERTITEM | |
639 | ,(MPARAM)nNewN | |
640 | ,(MPARAM)rsString.c_str() | |
641 | ); | |
642 | ||
643 | // | |
644 | // Restore the client data | |
645 | // | |
646 | if (pOldData) | |
647 | SetClientData( N | |
648 | ,pOldData | |
649 | ); | |
650 | else if (pOldObjData) | |
651 | SetClientObject( N | |
652 | ,pOldObjData | |
653 | ); | |
654 | ||
655 | // | |
656 | // We may have lost the selection | |
657 | // | |
658 | if (bWasSelected) | |
dcd307ee DW |
659 | Select(N); |
660 | ||
661 | #if wxUSE_OWNER_DRAWN | |
49dc8caa DW |
662 | if (m_windowStyle & wxLB_OWNERDRAW) |
663 | // | |
664 | // Update item's text | |
665 | // | |
666 | m_aItems[N]->SetName(rsString); | |
dcd307ee | 667 | #endif //USE_OWNER_DRAWN |
49dc8caa | 668 | } // end of wxListBox::SetString |
dcd307ee DW |
669 | |
670 | int wxListBox::GetCount() const | |
671 | { | |
49dc8caa | 672 | return m_nNumItems; |
dcd307ee DW |
673 | } |
674 | ||
675 | // ---------------------------------------------------------------------------- | |
676 | // helpers | |
677 | // ---------------------------------------------------------------------------- | |
678 | ||
e78c4d50 | 679 | wxSize wxListBox::DoGetBestSize() const |
fb9010ed | 680 | { |
49dc8caa DW |
681 | // |
682 | // Find the widest string | |
683 | // | |
684 | int nLine; | |
685 | int nListbox = 0; | |
686 | int nCx; | |
687 | int nCy; | |
688 | ||
689 | for (int i = 0; i < m_nNumItems; i++) | |
fb9010ed | 690 | { |
49dc8caa DW |
691 | wxString vStr(GetString(i)); |
692 | ||
693 | GetTextExtent( vStr | |
694 | ,&nLine | |
695 | ,NULL | |
696 | ); | |
697 | if (nLine > nListbox) | |
698 | nListbox = nLine; | |
fb9010ed DW |
699 | } |
700 | ||
49dc8caa DW |
701 | // |
702 | // Give it some reasonable default value if there are no strings in the | |
703 | // list. | |
704 | // | |
705 | if (nListbox == 0) | |
706 | nListbox = 100; | |
707 | ||
708 | // | |
709 | // The listbox should be slightly larger than the widest string | |
710 | // | |
711 | wxGetCharSize( GetHWND() | |
712 | ,&nCx | |
713 | ,&nCy | |
714 | ,(wxFont*)&GetFont() | |
715 | ); | |
716 | nListbox += 3 * nCx; | |
717 | ||
718 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7)); | |
719 | ||
720 | return wxSize( nListbox | |
721 | ,hListbox | |
722 | ); | |
723 | } // end of wxListBox::DoGetBestSize | |
fb9010ed | 724 | |
dcd307ee DW |
725 | // ---------------------------------------------------------------------------- |
726 | // callbacks | |
727 | // ---------------------------------------------------------------------------- | |
728 | ||
49dc8caa DW |
729 | bool wxListBox::OS2Command( |
730 | WXUINT uParam | |
731 | , WXWORD WXUNUSED(wId)) | |
0e320a79 | 732 | { |
49dc8caa | 733 | wxEventType eEvtType; |
dcd307ee | 734 | |
49dc8caa DW |
735 | if (uParam == LN_SELECT) |
736 | { | |
737 | eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
dcd307ee | 738 | } |
49dc8caa | 739 | if (uParam == LN_ENTER) |
0e320a79 | 740 | { |
49dc8caa | 741 | eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
0e320a79 | 742 | } |
fb9010ed | 743 | else |
49dc8caa DW |
744 | { |
745 | // | |
746 | // Some event we're not interested in | |
747 | // | |
748 | return FALSE; | |
749 | } | |
750 | wxCommandEvent vEvent( eEvtType | |
751 | ,m_windowId | |
752 | ); | |
fb9010ed | 753 | |
49dc8caa | 754 | vEvent.SetEventObject(this); |
fb9010ed | 755 | |
49dc8caa DW |
756 | wxArrayInt aSelections; |
757 | int n; | |
758 | int nCount = GetSelections(aSelections); | |
fb9010ed | 759 | |
49dc8caa DW |
760 | if (nCount > 0) |
761 | { | |
762 | n = aSelections[0]; | |
763 | if (HasClientObjectData()) | |
764 | vEvent.SetClientObject(GetClientObject(n)); | |
765 | else if ( HasClientUntypedData() ) | |
766 | vEvent.SetClientData(GetClientData(n)); | |
767 | vEvent.SetString(GetString(n)); | |
768 | } | |
769 | else | |
770 | { | |
771 | n = -1; | |
772 | } | |
773 | vEvent.m_commandInt = n; | |
774 | return GetEventHandler()->ProcessEvent(vEvent); | |
775 | } // end of wxListBox::OS2Command | |
fb9010ed | 776 | |
dcd307ee DW |
777 | // ---------------------------------------------------------------------------- |
778 | // wxCheckListBox support | |
779 | // ---------------------------------------------------------------------------- | |
fb9010ed DW |
780 | |
781 | #if wxUSE_OWNER_DRAWN | |
782 | ||
49dc8caa DW |
783 | // |
784 | // Drawing | |
fb9010ed | 785 | // ------- |
49dc8caa | 786 | // |
fb9010ed DW |
787 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) |
788 | ||
fb9010ed DW |
789 | bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT *item) |
790 | { | |
49dc8caa DW |
791 | // |
792 | // TODO: Get to this eventually | |
793 | // | |
fb9010ed DW |
794 | return TRUE; |
795 | } | |
796 | ||
fb9010ed DW |
797 | bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT *item) |
798 | { | |
49dc8caa DW |
799 | // |
800 | // TODO: Get to this eventually | |
801 | // | |
dcd307ee | 802 | return FALSE; |
fb9010ed | 803 | } |
49dc8caa DW |
804 | #endif // ndef for wxUSE_OWNER_DRAWN |
805 | ||
806 | #endif // ndef for wxUSE_LISTBOX | |
7e99520b | 807 |