]>
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 | ); | |
1de4baa3 DW |
413 | if(m_windowStyle & wxLB_OWNERDRAW) |
414 | Refresh(); | |
49dc8caa DW |
415 | } // end of wxListBox::SetSelection |
416 | ||
417 | bool wxListBox::IsSelected( | |
418 | int N | |
419 | ) const | |
420 | { | |
421 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, FALSE, | |
fb9010ed DW |
422 | wxT("invalid index in wxListBox::Selected") ); |
423 | ||
49dc8caa | 424 | LONG lItem; |
0e320a79 | 425 | |
70a2c656 DW |
426 | if (GetWindowStyleFlag() & wxLB_EXTENDED) |
427 | { | |
428 | if (N == 0) | |
429 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)); | |
430 | else | |
431 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)(N - 1), (MPARAM)0)); | |
432 | } | |
433 | else | |
434 | { | |
435 | lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0)); | |
436 | } | |
437 | return (lItem == (LONG)N && lItem != LIT_NONE); | |
49dc8caa DW |
438 | } // end of wxListBox::IsSelected |
439 | ||
440 | wxClientData* wxListBox::DoGetItemClientObject( | |
441 | int n | |
442 | ) const | |
0e320a79 | 443 | { |
dcd307ee | 444 | return (wxClientData *)DoGetItemClientData(n); |
0e320a79 DW |
445 | } |
446 | ||
49dc8caa DW |
447 | void* wxListBox::DoGetItemClientData( |
448 | int n | |
449 | ) const | |
0e320a79 | 450 | { |
49dc8caa | 451 | wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL, |
fb9010ed DW |
452 | wxT("invalid index in wxListBox::GetClientData") ); |
453 | ||
49dc8caa DW |
454 | return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0)); |
455 | } // end of wxListBox::DoGetItemClientData | |
0e320a79 | 456 | |
49dc8caa DW |
457 | void wxListBox::DoSetItemClientObject( |
458 | int n | |
459 | , wxClientData* pClientData | |
460 | ) | |
0e320a79 | 461 | { |
49dc8caa DW |
462 | DoSetItemClientData( n |
463 | ,pClientData | |
464 | ); | |
465 | } // end of wxListBox::DoSetItemClientObject | |
dcd307ee | 466 | |
49dc8caa DW |
467 | void wxListBox::DoSetItemClientData( |
468 | int n | |
469 | , void* pClientData | |
470 | ) | |
dcd307ee | 471 | { |
49dc8caa | 472 | wxCHECK_RET( n >= 0 && n < m_nNumItems, |
fb9010ed DW |
473 | wxT("invalid index in wxListBox::SetClientData") ); |
474 | ||
dcd307ee DW |
475 | #if wxUSE_OWNER_DRAWN |
476 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
477 | { | |
49dc8caa DW |
478 | // |
479 | // Client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
dcd307ee | 480 | // in OnMeasure/OnDraw. |
49dc8caa | 481 | // |
dcd307ee DW |
482 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); |
483 | } | |
484 | #endif // wxUSE_OWNER_DRAWN | |
485 | ||
49dc8caa DW |
486 | ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData)); |
487 | } // end of wxListBox::DoSetItemClientData | |
dcd307ee DW |
488 | |
489 | bool wxListBox::HasMultipleSelection() const | |
490 | { | |
491 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
49dc8caa | 492 | } // end of wxListBox::HasMultipleSelection |
0e320a79 | 493 | |
49dc8caa DW |
494 | int wxListBox::GetSelections( |
495 | wxArrayInt& raSelections | |
496 | ) const | |
0e320a79 | 497 | { |
49dc8caa DW |
498 | int nCount = 0; |
499 | LONG lItem; | |
0e320a79 | 500 | |
fb9010ed | 501 | |
49dc8caa DW |
502 | raSelections.Empty(); |
503 | if (HasMultipleSelection()) | |
504 | { | |
505 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
506 | ,LM_QUERYSELECTION | |
507 | ,(MPARAM)LIT_FIRST | |
508 | ,(MPARAM)0 | |
509 | ) | |
510 | ); | |
511 | if (lItem != LIT_NONE) | |
512 | { | |
513 | nCount++; | |
514 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
515 | ,LM_QUERYSELECTION | |
516 | ,(MPARAM)lItem | |
517 | ,(MPARAM)0 | |
518 | ) | |
519 | )) != LIT_NONE) | |
520 | { | |
521 | nCount++; | |
522 | } | |
523 | raSelections.Alloc(nCount); | |
524 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
525 | ,LM_QUERYSELECTION | |
526 | ,(MPARAM)LIT_FIRST | |
527 | ,(MPARAM)0 | |
528 | ) | |
529 | ); | |
530 | ||
531 | raSelections.Add((int)lItem); | |
532 | while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() | |
533 | ,LM_QUERYSELECTION | |
534 | ,(MPARAM)lItem | |
535 | ,(MPARAM)0 | |
536 | ) | |
537 | )) != LIT_NONE) | |
538 | { | |
539 | raSelections.Add((int)lItem); | |
540 | } | |
541 | return nCount; | |
fb9010ed | 542 | } |
49dc8caa | 543 | return 0; |
0e320a79 DW |
544 | } |
545 | else // single-selection listbox | |
546 | { | |
49dc8caa DW |
547 | lItem = LONGFROMMR(::WinSendMsg( GetHwnd() |
548 | ,LM_QUERYSELECTION | |
549 | ,(MPARAM)LIT_FIRST | |
550 | ,(MPARAM)0 | |
551 | ) | |
552 | ); | |
553 | raSelections.Add((int)lItem); | |
0e320a79 DW |
554 | return 1; |
555 | } | |
dcd307ee | 556 | return 0; |
49dc8caa | 557 | } // end of wxListBox::GetSelections |
0e320a79 | 558 | |
0e320a79 DW |
559 | int wxListBox::GetSelection() const |
560 | { | |
dcd307ee | 561 | wxCHECK_MSG( !HasMultipleSelection(), |
fb9010ed DW |
562 | -1, |
563 | wxT("GetSelection() can't be used with multiple-selection " | |
564 | "listboxes, use GetSelections() instead.") ); | |
565 | ||
49dc8caa DW |
566 | return(LONGFROMMR(::WinSendMsg( GetHwnd() |
567 | ,LM_QUERYSELECTION | |
568 | ,(MPARAM)LIT_FIRST | |
569 | ,(MPARAM)0 | |
570 | ) | |
571 | )); | |
572 | } // end of wxListBox::GetSelection | |
0e320a79 | 573 | |
49dc8caa DW |
574 | wxString wxListBox::GetString( |
575 | int N | |
576 | ) const | |
0e320a79 | 577 | { |
49dc8caa DW |
578 | LONG lLen = 0; |
579 | char* zBuf; | |
580 | wxString sResult; | |
fb9010ed | 581 | |
49dc8caa DW |
582 | wxCHECK_MSG( N >= 0 && N < m_nNumItems, "", |
583 | wxT("invalid index in wxListBox::GetClientData") ); | |
0e320a79 | 584 | |
49dc8caa DW |
585 | lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0)); |
586 | zBuf = new char[lLen + 1]; | |
587 | ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf); | |
588 | zBuf[lLen] = '\0'; | |
589 | sResult = zBuf; | |
590 | delete [] zBuf; | |
591 | return sResult; | |
592 | } // end of wxListBox::GetString | |
593 | ||
594 | void wxListBox::DoInsertItems( | |
595 | const wxArrayString& asItems | |
596 | , int nPos | |
597 | ) | |
598 | { | |
599 | wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems, | |
dcd307ee DW |
600 | wxT("invalid index in wxListBox::InsertItems") ); |
601 | ||
49dc8caa | 602 | int nItems = asItems.GetCount(); |
dcd307ee | 603 | |
49dc8caa | 604 | for (int i = 0; i < nItems; i++) |
3437f881 DW |
605 | { |
606 | int nIndex = (int)::WinSendMsg( GetHwnd() | |
607 | ,LM_INSERTITEM | |
608 | ,MPFROMLONG((LONG)(i + nPos)) | |
609 | ,(MPARAM)asItems[i].c_str() | |
610 | ); | |
611 | ||
612 | wxOwnerDrawn* pNewItem = CreateItem(nIndex); | |
613 | ||
614 | pNewItem->SetName(asItems[i]); | |
615 | pNewItem->SetFont(GetFont()); | |
616 | m_aItems.Insert(pNewItem, nIndex); | |
617 | ::WinSendMsg( GetHwnd() | |
618 | ,LM_SETITEMHANDLE | |
619 | ,(MPARAM)((SHORT)nIndex) | |
620 | ,MPFROMP(pNewItem) | |
621 | ); | |
622 | m_nNumItems += nItems; | |
623 | } | |
49dc8caa | 624 | } // end of wxListBox::DoInsertItems |
dcd307ee | 625 | |
49dc8caa DW |
626 | void wxListBox::SetString( |
627 | int N | |
628 | , const wxString& rsString | |
629 | ) | |
dcd307ee | 630 | { |
49dc8caa | 631 | wxCHECK_RET( N >= 0 && N < m_nNumItems, |
dcd307ee DW |
632 | wxT("invalid index in wxListBox::SetString") ); |
633 | ||
49dc8caa DW |
634 | // |
635 | // Remember the state of the item | |
636 | // | |
637 | bool bWasSelected = IsSelected(N); | |
638 | void* pOldData = NULL; | |
639 | wxClientData* pOldObjData = NULL; | |
640 | ||
641 | if (m_clientDataItemsType == wxClientData_Void) | |
642 | pOldData = GetClientData(N); | |
643 | else if (m_clientDataItemsType == wxClientData_Object) | |
644 | pOldObjData = GetClientObject(N); | |
645 | ||
646 | // | |
647 | // Delete and recreate it | |
648 | // | |
649 | ::WinSendMsg( GetHwnd() | |
650 | ,LM_DELETEITEM | |
651 | ,(MPARAM)N | |
652 | ,(MPARAM)0 | |
653 | ); | |
654 | ||
655 | int nNewN = N; | |
656 | ||
657 | if (N == m_nNumItems - 1) | |
658 | nNewN = -1; | |
659 | ||
660 | ::WinSendMsg( GetHwnd() | |
661 | ,LM_INSERTITEM | |
662 | ,(MPARAM)nNewN | |
663 | ,(MPARAM)rsString.c_str() | |
664 | ); | |
665 | ||
666 | // | |
667 | // Restore the client data | |
668 | // | |
669 | if (pOldData) | |
670 | SetClientData( N | |
671 | ,pOldData | |
672 | ); | |
673 | else if (pOldObjData) | |
674 | SetClientObject( N | |
675 | ,pOldObjData | |
676 | ); | |
677 | ||
678 | // | |
679 | // We may have lost the selection | |
680 | // | |
681 | if (bWasSelected) | |
dcd307ee DW |
682 | Select(N); |
683 | ||
684 | #if wxUSE_OWNER_DRAWN | |
49dc8caa DW |
685 | if (m_windowStyle & wxLB_OWNERDRAW) |
686 | // | |
687 | // Update item's text | |
688 | // | |
689 | m_aItems[N]->SetName(rsString); | |
dcd307ee | 690 | #endif //USE_OWNER_DRAWN |
49dc8caa | 691 | } // end of wxListBox::SetString |
dcd307ee DW |
692 | |
693 | int wxListBox::GetCount() const | |
694 | { | |
49dc8caa | 695 | return m_nNumItems; |
dcd307ee DW |
696 | } |
697 | ||
698 | // ---------------------------------------------------------------------------- | |
699 | // helpers | |
700 | // ---------------------------------------------------------------------------- | |
701 | ||
e78c4d50 | 702 | wxSize wxListBox::DoGetBestSize() const |
fb9010ed | 703 | { |
49dc8caa DW |
704 | // |
705 | // Find the widest string | |
706 | // | |
707 | int nLine; | |
708 | int nListbox = 0; | |
709 | int nCx; | |
710 | int nCy; | |
711 | ||
712 | for (int i = 0; i < m_nNumItems; i++) | |
fb9010ed | 713 | { |
49dc8caa DW |
714 | wxString vStr(GetString(i)); |
715 | ||
716 | GetTextExtent( vStr | |
717 | ,&nLine | |
718 | ,NULL | |
719 | ); | |
720 | if (nLine > nListbox) | |
721 | nListbox = nLine; | |
fb9010ed DW |
722 | } |
723 | ||
49dc8caa DW |
724 | // |
725 | // Give it some reasonable default value if there are no strings in the | |
726 | // list. | |
727 | // | |
728 | if (nListbox == 0) | |
729 | nListbox = 100; | |
730 | ||
731 | // | |
732 | // The listbox should be slightly larger than the widest string | |
733 | // | |
734 | wxGetCharSize( GetHWND() | |
735 | ,&nCx | |
736 | ,&nCy | |
737 | ,(wxFont*)&GetFont() | |
738 | ); | |
739 | nListbox += 3 * nCx; | |
740 | ||
741 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7)); | |
742 | ||
743 | return wxSize( nListbox | |
744 | ,hListbox | |
745 | ); | |
746 | } // end of wxListBox::DoGetBestSize | |
fb9010ed | 747 | |
dcd307ee DW |
748 | // ---------------------------------------------------------------------------- |
749 | // callbacks | |
750 | // ---------------------------------------------------------------------------- | |
751 | ||
49dc8caa DW |
752 | bool wxListBox::OS2Command( |
753 | WXUINT uParam | |
754 | , WXWORD WXUNUSED(wId)) | |
0e320a79 | 755 | { |
49dc8caa | 756 | wxEventType eEvtType; |
dcd307ee | 757 | |
49dc8caa DW |
758 | if (uParam == LN_SELECT) |
759 | { | |
760 | eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
dcd307ee | 761 | } |
49dc8caa | 762 | if (uParam == LN_ENTER) |
0e320a79 | 763 | { |
49dc8caa | 764 | eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; |
0e320a79 | 765 | } |
fb9010ed | 766 | else |
49dc8caa DW |
767 | { |
768 | // | |
769 | // Some event we're not interested in | |
770 | // | |
771 | return FALSE; | |
772 | } | |
773 | wxCommandEvent vEvent( eEvtType | |
774 | ,m_windowId | |
775 | ); | |
fb9010ed | 776 | |
49dc8caa | 777 | vEvent.SetEventObject(this); |
fb9010ed | 778 | |
49dc8caa DW |
779 | wxArrayInt aSelections; |
780 | int n; | |
781 | int nCount = GetSelections(aSelections); | |
fb9010ed | 782 | |
49dc8caa DW |
783 | if (nCount > 0) |
784 | { | |
785 | n = aSelections[0]; | |
786 | if (HasClientObjectData()) | |
787 | vEvent.SetClientObject(GetClientObject(n)); | |
788 | else if ( HasClientUntypedData() ) | |
789 | vEvent.SetClientData(GetClientData(n)); | |
790 | vEvent.SetString(GetString(n)); | |
791 | } | |
792 | else | |
793 | { | |
794 | n = -1; | |
795 | } | |
796 | vEvent.m_commandInt = n; | |
797 | return GetEventHandler()->ProcessEvent(vEvent); | |
798 | } // end of wxListBox::OS2Command | |
fb9010ed | 799 | |
dcd307ee DW |
800 | // ---------------------------------------------------------------------------- |
801 | // wxCheckListBox support | |
802 | // ---------------------------------------------------------------------------- | |
fb9010ed DW |
803 | |
804 | #if wxUSE_OWNER_DRAWN | |
805 | ||
49dc8caa DW |
806 | // |
807 | // Drawing | |
fb9010ed | 808 | // ------- |
49dc8caa | 809 | // |
fb9010ed DW |
810 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) |
811 | ||
1de4baa3 DW |
812 | bool wxListBox::OS2OnMeasure( |
813 | WXMEASUREITEMSTRUCT* pItem | |
814 | ) | |
fb9010ed | 815 | { |
1de4baa3 DW |
816 | if (!pItem) |
817 | pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM; | |
818 | ||
819 | POWNERITEM pMeasureStruct = (POWNERITEM)pItem; | |
820 | wxScreenDC vDc; | |
821 | ||
49dc8caa | 822 | // |
1de4baa3 | 823 | // Only owner-drawn control should receive this message |
49dc8caa | 824 | // |
1de4baa3 DW |
825 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); |
826 | ||
827 | vDc.SetFont(GetFont()); | |
828 | ||
829 | wxCoord vHeight; | |
830 | ||
831 | pMeasureStruct->rclItem.xRight = 0; | |
832 | pMeasureStruct->rclItem.xLeft = 0; | |
833 | pMeasureStruct->rclItem.yTop = 0; | |
834 | pMeasureStruct->rclItem.yBottom = 0; | |
835 | ||
836 | vHeight = vDc.GetCharHeight() * 2.5; | |
837 | pMeasureStruct->rclItem.yTop = vHeight; | |
838 | ||
839 | ::WinSendMsg( GetHWND() | |
840 | ,LM_SETITEMHEIGHT | |
841 | ,MPFROMLONG(vHeight) | |
842 | ,MPFROMLONG(pMeasureStruct->idItem) | |
843 | ); | |
fb9010ed | 844 | return TRUE; |
1de4baa3 | 845 | } // end of wxListBox::OS2OnMeasure |
fb9010ed | 846 | |
1de4baa3 DW |
847 | bool wxListBox::OS2OnDraw ( |
848 | WXDRAWITEMSTRUCT* pItem | |
849 | ) | |
fb9010ed | 850 | { |
1de4baa3 DW |
851 | POWNERITEM pDrawStruct = (POWNERITEM)pItem; |
852 | LONG lItemID = pDrawStruct->idItem; | |
853 | int eAction = 0; | |
854 | int eStatus = 0; | |
855 | ||
49dc8caa | 856 | // |
1de4baa3 | 857 | // Only owner-drawn control should receive this message |
49dc8caa | 858 | // |
1de4baa3 DW |
859 | wxCHECK(((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE); |
860 | ||
861 | ||
862 | // | |
863 | // The item may be -1 for an empty listbox | |
864 | // | |
865 | if (lItemID == -1L) | |
866 | return FALSE; | |
867 | ||
868 | wxListBoxItem* pData = (wxListBoxItem*)PVOIDFROMMR( ::WinSendMsg( GetHwnd() | |
869 | ,LM_QUERYITEMHANDLE | |
870 | ,MPFROMLONG(pDrawStruct->idItem) | |
871 | ,(MPARAM)0 | |
872 | ) | |
873 | ); | |
874 | ||
875 | wxCHECK(pData, FALSE ); | |
876 | ||
877 | wxDC vDc; | |
878 | wxRect vRect( wxPoint( pDrawStruct->rclItem.xLeft | |
879 | ,pDrawStruct->rclItem.yTop | |
880 | ) | |
881 | ,wxPoint( pDrawStruct->rclItem.xRight | |
882 | ,pDrawStruct->rclItem.yBottom | |
883 | ) | |
884 | ); | |
885 | ||
886 | vDc.SetHPS(pDrawStruct->hps); | |
887 | ||
888 | if (pDrawStruct->fsAttribute == pDrawStruct->fsAttributeOld) | |
889 | { | |
890 | // | |
891 | // Entire Item needs to be redrawn (either it has reappeared from | |
892 | // behind another window or is being displayed for the first time | |
893 | // | |
894 | eAction = wxOwnerDrawn::wxODDrawAll; | |
895 | ||
896 | if (pDrawStruct->fsAttribute & MIA_HILITED) | |
897 | { | |
898 | // | |
899 | // If it is currently selected we let the system handle it | |
900 | // | |
901 | eStatus |= wxOwnerDrawn::wxODSelected; | |
902 | } | |
903 | if (pDrawStruct->fsAttribute & MIA_CHECKED) | |
904 | { | |
905 | // | |
906 | // If it is currently checked we draw our own | |
907 | // | |
908 | eStatus |= wxOwnerDrawn::wxODChecked; | |
909 | pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_CHECKED; | |
910 | } | |
911 | if (pDrawStruct->fsAttribute & MIA_DISABLED) | |
912 | { | |
913 | // | |
914 | // If it is currently disabled we let the system handle it | |
915 | // | |
916 | eStatus |= wxOwnerDrawn::wxODDisabled; | |
917 | } | |
918 | // | |
919 | // Don't really care about framed (indicationg focus) or NoDismiss | |
920 | // | |
921 | } | |
922 | else | |
923 | { | |
924 | if (pDrawStruct->fsAttribute & MIA_HILITED) | |
925 | { | |
926 | eAction = wxOwnerDrawn::wxODDrawAll; | |
927 | eStatus |= wxOwnerDrawn::wxODSelected; | |
928 | // | |
929 | // Keep the system from trying to highlight with its bogus colors | |
930 | // | |
931 | pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_HILITED; | |
932 | } | |
933 | else if (!(pDrawStruct->fsAttribute & MIA_HILITED)) | |
934 | { | |
935 | eAction = wxOwnerDrawn::wxODDrawAll; | |
936 | eStatus = 0; | |
937 | // | |
938 | // Keep the system from trying to highlight with its bogus colors | |
939 | // | |
940 | pDrawStruct->fsAttribute = pDrawStruct->fsAttributeOld &= ~MIA_HILITED; | |
941 | } | |
942 | else | |
943 | { | |
944 | // | |
945 | // For now we don't care about anything else | |
946 | // just ignore the entire message! | |
947 | // | |
948 | return TRUE; | |
949 | } | |
950 | } | |
951 | return pData->OnDrawItem( vDc | |
952 | ,vRect | |
953 | ,(wxOwnerDrawn::wxODAction)eAction | |
954 | ,(wxOwnerDrawn::wxODStatus)eStatus | |
955 | ); | |
956 | } // end of wxListBox::OS2OnDraw | |
957 | ||
49dc8caa DW |
958 | #endif // ndef for wxUSE_OWNER_DRAWN |
959 | ||
960 | #endif // ndef for wxUSE_LISTBOX | |
7e99520b | 961 |