]> git.saurik.com Git - wxWidgets.git/blame - src/os2/listbox.cpp
cleaned up the checkbox creation code (~70 lines of code became 3)
[wxWidgets.git] / src / os2 / listbox.cpp
CommitLineData
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
48class wxListBoxItem : public wxOwnerDrawn
49{
50public:
49dc8caa 51 wxListBoxItem(const wxString& rsStr = "");
fb9010ed
DW
52};
53
49dc8caa
DW
54wxListBoxItem::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
67wxOwnerDrawn* 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
81wxListBox::wxListBox()
82{
49dc8caa
DW
83 m_nNumItems = 0;
84 m_nSelected = 0;
85} // end of wxListBox::wxListBox
86
87bool 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
211wxListBox::~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
223void 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
233void 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
243void 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
264int 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
292void 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
352int 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
376void 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
401void 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
415bool 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
49dc8caa
DW
424 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)N, (MPARAM)0));
425 return (lItem != LIT_NONE);
426} // end of wxListBox::IsSelected
427
428wxClientData* wxListBox::DoGetItemClientObject(
429 int n
430) const
0e320a79 431{
dcd307ee 432 return (wxClientData *)DoGetItemClientData(n);
0e320a79
DW
433}
434
49dc8caa
DW
435void* wxListBox::DoGetItemClientData(
436 int n
437) const
0e320a79 438{
49dc8caa 439 wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL,
fb9010ed
DW
440 wxT("invalid index in wxListBox::GetClientData") );
441
49dc8caa
DW
442 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0));
443} // end of wxListBox::DoGetItemClientData
0e320a79 444
49dc8caa
DW
445void wxListBox::DoSetItemClientObject(
446 int n
447, wxClientData* pClientData
448)
0e320a79 449{
49dc8caa
DW
450 DoSetItemClientData( n
451 ,pClientData
452 );
453} // end of wxListBox::DoSetItemClientObject
dcd307ee 454
49dc8caa
DW
455void wxListBox::DoSetItemClientData(
456 int n
457, void* pClientData
458)
dcd307ee 459{
49dc8caa 460 wxCHECK_RET( n >= 0 && n < m_nNumItems,
fb9010ed
DW
461 wxT("invalid index in wxListBox::SetClientData") );
462
dcd307ee
DW
463#if wxUSE_OWNER_DRAWN
464 if ( m_windowStyle & wxLB_OWNERDRAW )
465 {
49dc8caa
DW
466 //
467 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
dcd307ee 468 // in OnMeasure/OnDraw.
49dc8caa 469 //
dcd307ee
DW
470 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
471 }
472#endif // wxUSE_OWNER_DRAWN
473
49dc8caa
DW
474 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
475} // end of wxListBox::DoSetItemClientData
dcd307ee
DW
476
477bool wxListBox::HasMultipleSelection() const
478{
479 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
49dc8caa 480} // end of wxListBox::HasMultipleSelection
0e320a79 481
49dc8caa
DW
482int wxListBox::GetSelections(
483 wxArrayInt& raSelections
484) const
0e320a79 485{
49dc8caa
DW
486 int nCount = 0;
487 LONG lItem;
0e320a79 488
fb9010ed 489
49dc8caa
DW
490 raSelections.Empty();
491 if (HasMultipleSelection())
492 {
493 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
494 ,LM_QUERYSELECTION
495 ,(MPARAM)LIT_FIRST
496 ,(MPARAM)0
497 )
498 );
499 if (lItem != LIT_NONE)
500 {
501 nCount++;
502 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
503 ,LM_QUERYSELECTION
504 ,(MPARAM)lItem
505 ,(MPARAM)0
506 )
507 )) != LIT_NONE)
508 {
509 nCount++;
510 }
511 raSelections.Alloc(nCount);
512 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
513 ,LM_QUERYSELECTION
514 ,(MPARAM)LIT_FIRST
515 ,(MPARAM)0
516 )
517 );
518
519 raSelections.Add((int)lItem);
520 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
521 ,LM_QUERYSELECTION
522 ,(MPARAM)lItem
523 ,(MPARAM)0
524 )
525 )) != LIT_NONE)
526 {
527 raSelections.Add((int)lItem);
528 }
529 return nCount;
fb9010ed 530 }
49dc8caa 531 return 0;
0e320a79
DW
532 }
533 else // single-selection listbox
534 {
49dc8caa
DW
535 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
536 ,LM_QUERYSELECTION
537 ,(MPARAM)LIT_FIRST
538 ,(MPARAM)0
539 )
540 );
541 raSelections.Add((int)lItem);
0e320a79
DW
542 return 1;
543 }
dcd307ee 544 return 0;
49dc8caa 545} // end of wxListBox::GetSelections
0e320a79 546
0e320a79
DW
547int wxListBox::GetSelection() const
548{
dcd307ee 549 wxCHECK_MSG( !HasMultipleSelection(),
fb9010ed
DW
550 -1,
551 wxT("GetSelection() can't be used with multiple-selection "
552 "listboxes, use GetSelections() instead.") );
553
49dc8caa
DW
554 return(LONGFROMMR(::WinSendMsg( GetHwnd()
555 ,LM_QUERYSELECTION
556 ,(MPARAM)LIT_FIRST
557 ,(MPARAM)0
558 )
559 ));
560} // end of wxListBox::GetSelection
0e320a79 561
49dc8caa
DW
562wxString wxListBox::GetString(
563 int N
564) const
0e320a79 565{
49dc8caa
DW
566 LONG lLen = 0;
567 char* zBuf;
568 wxString sResult;
fb9010ed 569
49dc8caa
DW
570 wxCHECK_MSG( N >= 0 && N < m_nNumItems, "",
571 wxT("invalid index in wxListBox::GetClientData") );
0e320a79 572
49dc8caa
DW
573 lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0));
574 zBuf = new char[lLen + 1];
575 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf);
576 zBuf[lLen] = '\0';
577 sResult = zBuf;
578 delete [] zBuf;
579 return sResult;
580} // end of wxListBox::GetString
581
582void wxListBox::DoInsertItems(
583 const wxArrayString& asItems
584, int nPos
585)
586{
587 wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems,
dcd307ee
DW
588 wxT("invalid index in wxListBox::InsertItems") );
589
49dc8caa 590 int nItems = asItems.GetCount();
dcd307ee 591
49dc8caa 592 for (int i = 0; i < nItems; i++)
3437f881
DW
593 {
594 int nIndex = (int)::WinSendMsg( GetHwnd()
595 ,LM_INSERTITEM
596 ,MPFROMLONG((LONG)(i + nPos))
597 ,(MPARAM)asItems[i].c_str()
598 );
599
600 wxOwnerDrawn* pNewItem = CreateItem(nIndex);
601
602 pNewItem->SetName(asItems[i]);
603 pNewItem->SetFont(GetFont());
604 m_aItems.Insert(pNewItem, nIndex);
605 ::WinSendMsg( GetHwnd()
606 ,LM_SETITEMHANDLE
607 ,(MPARAM)((SHORT)nIndex)
608 ,MPFROMP(pNewItem)
609 );
610 m_nNumItems += nItems;
611 }
49dc8caa 612} // end of wxListBox::DoInsertItems
dcd307ee 613
49dc8caa
DW
614void wxListBox::SetString(
615 int N
616, const wxString& rsString
617)
dcd307ee 618{
49dc8caa 619 wxCHECK_RET( N >= 0 && N < m_nNumItems,
dcd307ee
DW
620 wxT("invalid index in wxListBox::SetString") );
621
49dc8caa
DW
622 //
623 // Remember the state of the item
624 //
625 bool bWasSelected = IsSelected(N);
626 void* pOldData = NULL;
627 wxClientData* pOldObjData = NULL;
628
629 if (m_clientDataItemsType == wxClientData_Void)
630 pOldData = GetClientData(N);
631 else if (m_clientDataItemsType == wxClientData_Object)
632 pOldObjData = GetClientObject(N);
633
634 //
635 // Delete and recreate it
636 //
637 ::WinSendMsg( GetHwnd()
638 ,LM_DELETEITEM
639 ,(MPARAM)N
640 ,(MPARAM)0
641 );
642
643 int nNewN = N;
644
645 if (N == m_nNumItems - 1)
646 nNewN = -1;
647
648 ::WinSendMsg( GetHwnd()
649 ,LM_INSERTITEM
650 ,(MPARAM)nNewN
651 ,(MPARAM)rsString.c_str()
652 );
653
654 //
655 // Restore the client data
656 //
657 if (pOldData)
658 SetClientData( N
659 ,pOldData
660 );
661 else if (pOldObjData)
662 SetClientObject( N
663 ,pOldObjData
664 );
665
666 //
667 // We may have lost the selection
668 //
669 if (bWasSelected)
dcd307ee
DW
670 Select(N);
671
672#if wxUSE_OWNER_DRAWN
49dc8caa
DW
673 if (m_windowStyle & wxLB_OWNERDRAW)
674 //
675 // Update item's text
676 //
677 m_aItems[N]->SetName(rsString);
dcd307ee 678#endif //USE_OWNER_DRAWN
49dc8caa 679} // end of wxListBox::SetString
dcd307ee
DW
680
681int wxListBox::GetCount() const
682{
49dc8caa 683 return m_nNumItems;
dcd307ee
DW
684}
685
686// ----------------------------------------------------------------------------
687// helpers
688// ----------------------------------------------------------------------------
689
e78c4d50 690wxSize wxListBox::DoGetBestSize() const
fb9010ed 691{
49dc8caa
DW
692 //
693 // Find the widest string
694 //
695 int nLine;
696 int nListbox = 0;
697 int nCx;
698 int nCy;
699
700 for (int i = 0; i < m_nNumItems; i++)
fb9010ed 701 {
49dc8caa
DW
702 wxString vStr(GetString(i));
703
704 GetTextExtent( vStr
705 ,&nLine
706 ,NULL
707 );
708 if (nLine > nListbox)
709 nListbox = nLine;
fb9010ed
DW
710 }
711
49dc8caa
DW
712 //
713 // Give it some reasonable default value if there are no strings in the
714 // list.
715 //
716 if (nListbox == 0)
717 nListbox = 100;
718
719 //
720 // The listbox should be slightly larger than the widest string
721 //
722 wxGetCharSize( GetHWND()
723 ,&nCx
724 ,&nCy
725 ,(wxFont*)&GetFont()
726 );
727 nListbox += 3 * nCx;
728
729 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7));
730
731 return wxSize( nListbox
732 ,hListbox
733 );
734} // end of wxListBox::DoGetBestSize
fb9010ed 735
dcd307ee
DW
736// ----------------------------------------------------------------------------
737// callbacks
738// ----------------------------------------------------------------------------
739
49dc8caa
DW
740bool wxListBox::OS2Command(
741 WXUINT uParam
742, WXWORD WXUNUSED(wId))
0e320a79 743{
49dc8caa 744 wxEventType eEvtType;
dcd307ee 745
49dc8caa
DW
746 if (uParam == LN_SELECT)
747 {
748 eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED;
dcd307ee 749 }
49dc8caa 750 if (uParam == LN_ENTER)
0e320a79 751 {
49dc8caa 752 eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
0e320a79 753 }
fb9010ed 754 else
49dc8caa
DW
755 {
756 //
757 // Some event we're not interested in
758 //
759 return FALSE;
760 }
761 wxCommandEvent vEvent( eEvtType
762 ,m_windowId
763 );
fb9010ed 764
49dc8caa 765 vEvent.SetEventObject(this);
fb9010ed 766
49dc8caa
DW
767 wxArrayInt aSelections;
768 int n;
769 int nCount = GetSelections(aSelections);
fb9010ed 770
49dc8caa
DW
771 if (nCount > 0)
772 {
773 n = aSelections[0];
774 if (HasClientObjectData())
775 vEvent.SetClientObject(GetClientObject(n));
776 else if ( HasClientUntypedData() )
777 vEvent.SetClientData(GetClientData(n));
778 vEvent.SetString(GetString(n));
779 }
780 else
781 {
782 n = -1;
783 }
784 vEvent.m_commandInt = n;
785 return GetEventHandler()->ProcessEvent(vEvent);
786} // end of wxListBox::OS2Command
fb9010ed 787
dcd307ee
DW
788// ----------------------------------------------------------------------------
789// wxCheckListBox support
790// ----------------------------------------------------------------------------
fb9010ed
DW
791
792#if wxUSE_OWNER_DRAWN
793
49dc8caa
DW
794//
795// Drawing
fb9010ed 796// -------
49dc8caa 797//
fb9010ed
DW
798#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
799
fb9010ed
DW
800bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT *item)
801{
49dc8caa
DW
802 //
803 // TODO: Get to this eventually
804 //
fb9010ed
DW
805 return TRUE;
806}
807
fb9010ed
DW
808bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT *item)
809{
49dc8caa
DW
810 //
811 // TODO: Get to this eventually
812 //
dcd307ee 813 return FALSE;
fb9010ed 814}
49dc8caa
DW
815#endif // ndef for wxUSE_OWNER_DRAWN
816
817#endif // ndef for wxUSE_LISTBOX
7e99520b 818