]> git.saurik.com Git - wxWidgets.git/blame - src/os2/listbox.cpp
corrected path splitting for mac relative paths
[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
5d44b24e
DW
146 //
147 // If the parent is a scrolled window the controls must
148 // have this style or they will overlap the scrollbars
149 //
150 if (pParent)
151 if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) ||
152 pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow)))
153 lStyle |= WS_CLIPSIBLINGS;
154
49dc8caa
DW
155 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) // Parent
156 ,WC_LISTBOX // Default Listbox class
157 ,"LISTBOX" // Control's name
158 ,lStyle // Initial Style
159 ,0, 0, 0, 0 // Position and size
160 ,GetWinHwnd(pParent) // Owner
161 ,HWND_TOP // Z-Order
162 ,(HMENU)m_windowId // Id
163 ,NULL // Control Data
164 ,NULL // Presentation Parameters
165 );
166 if (m_hWnd == 0)
fb9010ed 167 {
49dc8caa 168 return FALSE;
fb9010ed
DW
169 }
170
49dc8caa
DW
171 //
172 // Subclass again for purposes of dialog editing mode
173 //
fb9010ed
DW
174 SubclassWin(m_hWnd);
175
49dc8caa 176 LONG lUi;
0e320a79 177
49dc8caa
DW
178 for (lUi = 0; lUi < (LONG)n; lUi++)
179 {
180 Append(asChoices[lUi]);
181 }
e58dab20
DW
182 SetFont(*wxSMALL_FONT);
183
5d44b24e
DW
184 //
185 // Set standard wxWindows colors for Listbox items and highlighting
186 //
187 wxColour vColour;
188
189 vColour.Set(wxString("WHITE"));
190
191 LONG lColor = (LONG)vColour.GetPixel();
192
193 ::WinSetPresParam( m_hWnd
194 ,PP_HILITEFOREGROUNDCOLOR
195 ,sizeof(LONG)
196 ,(PVOID)&lColor
197 );
198 vColour.Set(wxString("NAVY"));
199 lColor = (LONG)vColour.GetPixel();
200 ::WinSetPresParam( m_hWnd
201 ,PP_HILITEBACKGROUNDCOLOR
202 ,sizeof(LONG)
203 ,(PVOID)&lColor
204 );
205
49dc8caa
DW
206 SetSize( nX
207 ,nY
208 ,nWidth
209 ,nHeight
210 );
dcd307ee 211 return TRUE;
49dc8caa 212} // end of wxListBox::Create
0e320a79
DW
213
214wxListBox::~wxListBox()
215{
fb9010ed 216#if wxUSE_OWNER_DRAWN
49dc8caa
DW
217 size_t lUiCount = m_aItems.Count();
218
219 while (lUiCount-- != 0)
220 {
221 delete m_aItems[lUiCount];
fb9010ed
DW
222 }
223#endif // wxUSE_OWNER_DRAWN
49dc8caa 224} // end of wxListBox::~wxListBox
fb9010ed
DW
225
226void wxListBox::SetupColours()
227{
a756f210 228 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
fb9010ed 229 SetForegroundColour(GetParent()->GetForegroundColour());
49dc8caa 230} // end of wxListBox::SetupColours
0e320a79 231
dcd307ee
DW
232// ----------------------------------------------------------------------------
233// implementation of wxListBoxBase methods
234// ----------------------------------------------------------------------------
235
49dc8caa
DW
236void wxListBox::DoSetFirstItem(
237 int N
238)
0e320a79 239{
49dc8caa 240 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
241 wxT("invalid index in wxListBox::SetFirstItem") );
242
49dc8caa
DW
243 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX, MPFROMLONG(N), (MPARAM)0);
244} // end of wxListBox::DoSetFirstItem
0e320a79 245
49dc8caa
DW
246void wxListBox::Delete(
247 int N
248)
0e320a79 249{
49dc8caa 250 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
251 wxT("invalid index in wxListBox::Delete") );
252
dcd307ee
DW
253#if wxUSE_OWNER_DRAWN
254 delete m_aItems[N];
893758d5 255 m_aItems.RemoveAt(N);
dcd307ee 256#else // !wxUSE_OWNER_DRAWN
49dc8caa 257 if (HasClientObjectData())
dcd307ee
DW
258 {
259 delete GetClientObject(N);
260 }
261#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
262
49dc8caa
DW
263 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)N, (MPARAM)0);
264 m_nNumItems--;
265} // end of wxListBox::DoSetFirstItem
0e320a79 266
49dc8caa
DW
267int wxListBox::DoAppend(
268 const wxString& rsItem
269)
0e320a79 270{
49dc8caa
DW
271 int nIndex = 0;
272 SHORT nIndexType = 0;
273
274 if (m_windowStyle & wxLB_SORT)
275 nIndexType = LIT_SORTASCENDING;
276 else
277 nIndexType = LIT_END;
278 nIndex = (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)rsItem.c_str());
279 m_nNumItems++;
fb9010ed
DW
280
281#if wxUSE_OWNER_DRAWN
49dc8caa
DW
282 if (m_windowStyle & wxLB_OWNERDRAW)
283 {
284 wxOwnerDrawn* pNewItem = CreateItem(nIndex); // dummy argument
285
286 pNewItem->SetName(rsItem);
fb9010ed 287 m_aItems.Add(pNewItem);
49dc8caa
DW
288 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)((SHORT)nIndex), MPFROMP(pNewItem));
289 pNewItem->SetFont(GetFont());
fb9010ed
DW
290 }
291#endif
49dc8caa
DW
292 return nIndex;
293} // end of wxListBox::DoAppend
0e320a79 294
49dc8caa
DW
295void wxListBox::DoSetItems(
296 const wxArrayString& raChoices
297, void** ppClientData
298)
0e320a79 299{
49dc8caa
DW
300 BOOL bHideAndShow = IsShown();
301 int nCount = 0;
302 int i;
303 SHORT nIndexType = 0;
0e320a79 304
49dc8caa 305 if (bHideAndShow)
fb9010ed 306 {
49dc8caa
DW
307 ::WinShowWindow(GetHwnd(), FALSE);
308 }
309 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
310 m_nNumItems = raChoices.GetCount();
311 for (i = 0; i < m_nNumItems; i++)
312 {
313
314 if (m_windowStyle & wxLB_SORT)
315 nIndexType = LIT_SORTASCENDING;
316 else
317 nIndexType = LIT_END;
318 ::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)raChoices[i].c_str());
319
320 if (ppClientData)
dcd307ee
DW
321 {
322#if wxUSE_OWNER_DRAWN
49dc8caa 323 wxASSERT_MSG(ppClientData[i] == NULL,
dcd307ee
DW
324 wxT("Can't use client data with owner-drawn listboxes"));
325#else // !wxUSE_OWNER_DRAWN
49dc8caa 326 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lCount), MPFROMP(ppClientData[i]));
dcd307ee
DW
327#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
328 }
fb9010ed 329 }
fb9010ed
DW
330
331#if wxUSE_OWNER_DRAWN
49dc8caa
DW
332 if ( m_windowStyle & wxLB_OWNERDRAW )
333 {
334 //
335 // First delete old items
336 //
337 size_t lUi = m_aItems.Count();
338
339 while (lUi-- != 0)
340 {
341 delete m_aItems[lUi];
fb9010ed
DW
342 }
343 m_aItems.Empty();
344
49dc8caa
DW
345 //
346 // Then create new ones
347 //
348 for (lUi = 0; lUi < (size_t)m_nNumItems; lUi++)
349 {
350 wxOwnerDrawn* pNewItem = CreateItem(lUi);
351
352 pNewItem->SetName(raChoices[lUi]);
fb9010ed 353 m_aItems.Add(pNewItem);
49dc8caa 354 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lUi), MPFROMP(pNewItem));
fb9010ed
DW
355 }
356 }
dcd307ee 357#endif // wxUSE_OWNER_DRAWN
49dc8caa
DW
358 ::WinShowWindow(GetHwnd(), TRUE);
359} // end of wxListBox::DoSetItems
0e320a79 360
49dc8caa
DW
361int wxListBox::FindString(
362 const wxString& rsString
363) const
364{
365 int nPos;
366 LONG lTextLength;
367 PSZ zStr;
dcd307ee 368
0e320a79 369
49dc8caa
DW
370 for (nPos = 0; nPos < m_nNumItems; nPos++)
371 {
372 lTextLength = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0));
373 zStr = new char[lTextLength + 1];
374 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT(nPos, (SHORT)lTextLength), (MPARAM)zStr);
375 if (rsString == (char*)zStr)
376 {
377 delete [] zStr;
378 break;
379 }
380 delete [] zStr;
381 }
382 return nPos;
383} // end of wxListBox::FindString
0e320a79
DW
384
385void wxListBox::Clear()
386{
fb9010ed 387#if wxUSE_OWNER_DRAWN
49dc8caa
DW
388 size_t lUiCount = m_aItems.Count();
389
390 while (lUiCount-- != 0)
391 {
392 delete m_aItems[lUiCount];
fb9010ed
DW
393 }
394
395 m_aItems.Clear();
dcd307ee 396#else // !wxUSE_OWNER_DRAWN
49dc8caa 397 if (HasClientObjectData())
dcd307ee 398 {
49dc8caa 399 for (size_t n = 0; n < (size_t)m_lNumItems; n++)
dcd307ee
DW
400 {
401 delete GetClientObject(n);
402 }
403 }
404#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
49dc8caa 405 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
dcd307ee 406
49dc8caa
DW
407 m_nNumItems = 0;
408} // end of wxListBox::Clear
fb9010ed 409
49dc8caa
DW
410void wxListBox::SetSelection(
411 int N
412, bool bSelect
413)
0e320a79 414{
49dc8caa 415 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed 416 wxT("invalid index in wxListBox::SetSelection") );
49dc8caa
DW
417 ::WinSendMsg( GetHwnd()
418 ,LM_SELECTITEM
419 ,MPFROMLONG(N)
420 ,(MPARAM)bSelect
421 );
422} // end of wxListBox::SetSelection
423
424bool wxListBox::IsSelected(
425 int N
426) const
427{
428 wxCHECK_MSG( N >= 0 && N < m_nNumItems, FALSE,
fb9010ed
DW
429 wxT("invalid index in wxListBox::Selected") );
430
49dc8caa 431 LONG lItem;
0e320a79 432
49dc8caa
DW
433 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)N, (MPARAM)0));
434 return (lItem != LIT_NONE);
435} // end of wxListBox::IsSelected
436
437wxClientData* wxListBox::DoGetItemClientObject(
438 int n
439) const
0e320a79 440{
dcd307ee 441 return (wxClientData *)DoGetItemClientData(n);
0e320a79
DW
442}
443
49dc8caa
DW
444void* wxListBox::DoGetItemClientData(
445 int n
446) const
0e320a79 447{
49dc8caa 448 wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL,
fb9010ed
DW
449 wxT("invalid index in wxListBox::GetClientData") );
450
49dc8caa
DW
451 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0));
452} // end of wxListBox::DoGetItemClientData
0e320a79 453
49dc8caa
DW
454void wxListBox::DoSetItemClientObject(
455 int n
456, wxClientData* pClientData
457)
0e320a79 458{
49dc8caa
DW
459 DoSetItemClientData( n
460 ,pClientData
461 );
462} // end of wxListBox::DoSetItemClientObject
dcd307ee 463
49dc8caa
DW
464void wxListBox::DoSetItemClientData(
465 int n
466, void* pClientData
467)
dcd307ee 468{
49dc8caa 469 wxCHECK_RET( n >= 0 && n < m_nNumItems,
fb9010ed
DW
470 wxT("invalid index in wxListBox::SetClientData") );
471
dcd307ee
DW
472#if wxUSE_OWNER_DRAWN
473 if ( m_windowStyle & wxLB_OWNERDRAW )
474 {
49dc8caa
DW
475 //
476 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
dcd307ee 477 // in OnMeasure/OnDraw.
49dc8caa 478 //
dcd307ee
DW
479 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
480 }
481#endif // wxUSE_OWNER_DRAWN
482
49dc8caa
DW
483 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
484} // end of wxListBox::DoSetItemClientData
dcd307ee
DW
485
486bool wxListBox::HasMultipleSelection() const
487{
488 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
49dc8caa 489} // end of wxListBox::HasMultipleSelection
0e320a79 490
49dc8caa
DW
491int wxListBox::GetSelections(
492 wxArrayInt& raSelections
493) const
0e320a79 494{
49dc8caa
DW
495 int nCount = 0;
496 LONG lItem;
0e320a79 497
fb9010ed 498
49dc8caa
DW
499 raSelections.Empty();
500 if (HasMultipleSelection())
501 {
502 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
503 ,LM_QUERYSELECTION
504 ,(MPARAM)LIT_FIRST
505 ,(MPARAM)0
506 )
507 );
508 if (lItem != LIT_NONE)
509 {
510 nCount++;
511 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
512 ,LM_QUERYSELECTION
513 ,(MPARAM)lItem
514 ,(MPARAM)0
515 )
516 )) != LIT_NONE)
517 {
518 nCount++;
519 }
520 raSelections.Alloc(nCount);
521 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
522 ,LM_QUERYSELECTION
523 ,(MPARAM)LIT_FIRST
524 ,(MPARAM)0
525 )
526 );
527
528 raSelections.Add((int)lItem);
529 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
530 ,LM_QUERYSELECTION
531 ,(MPARAM)lItem
532 ,(MPARAM)0
533 )
534 )) != LIT_NONE)
535 {
536 raSelections.Add((int)lItem);
537 }
538 return nCount;
fb9010ed 539 }
49dc8caa 540 return 0;
0e320a79
DW
541 }
542 else // single-selection listbox
543 {
49dc8caa
DW
544 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
545 ,LM_QUERYSELECTION
546 ,(MPARAM)LIT_FIRST
547 ,(MPARAM)0
548 )
549 );
550 raSelections.Add((int)lItem);
0e320a79
DW
551 return 1;
552 }
dcd307ee 553 return 0;
49dc8caa 554} // end of wxListBox::GetSelections
0e320a79 555
0e320a79
DW
556int wxListBox::GetSelection() const
557{
dcd307ee 558 wxCHECK_MSG( !HasMultipleSelection(),
fb9010ed
DW
559 -1,
560 wxT("GetSelection() can't be used with multiple-selection "
561 "listboxes, use GetSelections() instead.") );
562
49dc8caa
DW
563 return(LONGFROMMR(::WinSendMsg( GetHwnd()
564 ,LM_QUERYSELECTION
565 ,(MPARAM)LIT_FIRST
566 ,(MPARAM)0
567 )
568 ));
569} // end of wxListBox::GetSelection
0e320a79 570
49dc8caa
DW
571wxString wxListBox::GetString(
572 int N
573) const
0e320a79 574{
49dc8caa
DW
575 LONG lLen = 0;
576 char* zBuf;
577 wxString sResult;
fb9010ed 578
49dc8caa
DW
579 wxCHECK_MSG( N >= 0 && N < m_nNumItems, "",
580 wxT("invalid index in wxListBox::GetClientData") );
0e320a79 581
49dc8caa
DW
582 lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0));
583 zBuf = new char[lLen + 1];
584 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf);
585 zBuf[lLen] = '\0';
586 sResult = zBuf;
587 delete [] zBuf;
588 return sResult;
589} // end of wxListBox::GetString
590
591void wxListBox::DoInsertItems(
592 const wxArrayString& asItems
593, int nPos
594)
595{
596 wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems,
dcd307ee
DW
597 wxT("invalid index in wxListBox::InsertItems") );
598
49dc8caa 599 int nItems = asItems.GetCount();
dcd307ee 600
49dc8caa
DW
601 for (int i = 0; i < nItems; i++)
602 ::WinSendMsg(GetHwnd(), LM_INSERTITEM, MPFROMLONG((LONG)(i + nPos)), (MPARAM)asItems[i].c_str());
603 m_nNumItems += nItems;
604} // end of wxListBox::DoInsertItems
dcd307ee 605
49dc8caa
DW
606void wxListBox::SetString(
607 int N
608, const wxString& rsString
609)
dcd307ee 610{
49dc8caa 611 wxCHECK_RET( N >= 0 && N < m_nNumItems,
dcd307ee
DW
612 wxT("invalid index in wxListBox::SetString") );
613
49dc8caa
DW
614 //
615 // Remember the state of the item
616 //
617 bool bWasSelected = IsSelected(N);
618 void* pOldData = NULL;
619 wxClientData* pOldObjData = NULL;
620
621 if (m_clientDataItemsType == wxClientData_Void)
622 pOldData = GetClientData(N);
623 else if (m_clientDataItemsType == wxClientData_Object)
624 pOldObjData = GetClientObject(N);
625
626 //
627 // Delete and recreate it
628 //
629 ::WinSendMsg( GetHwnd()
630 ,LM_DELETEITEM
631 ,(MPARAM)N
632 ,(MPARAM)0
633 );
634
635 int nNewN = N;
636
637 if (N == m_nNumItems - 1)
638 nNewN = -1;
639
640 ::WinSendMsg( GetHwnd()
641 ,LM_INSERTITEM
642 ,(MPARAM)nNewN
643 ,(MPARAM)rsString.c_str()
644 );
645
646 //
647 // Restore the client data
648 //
649 if (pOldData)
650 SetClientData( N
651 ,pOldData
652 );
653 else if (pOldObjData)
654 SetClientObject( N
655 ,pOldObjData
656 );
657
658 //
659 // We may have lost the selection
660 //
661 if (bWasSelected)
dcd307ee
DW
662 Select(N);
663
664#if wxUSE_OWNER_DRAWN
49dc8caa
DW
665 if (m_windowStyle & wxLB_OWNERDRAW)
666 //
667 // Update item's text
668 //
669 m_aItems[N]->SetName(rsString);
dcd307ee 670#endif //USE_OWNER_DRAWN
49dc8caa 671} // end of wxListBox::SetString
dcd307ee
DW
672
673int wxListBox::GetCount() const
674{
49dc8caa 675 return m_nNumItems;
dcd307ee
DW
676}
677
678// ----------------------------------------------------------------------------
679// helpers
680// ----------------------------------------------------------------------------
681
e78c4d50 682wxSize wxListBox::DoGetBestSize() const
fb9010ed 683{
49dc8caa
DW
684 //
685 // Find the widest string
686 //
687 int nLine;
688 int nListbox = 0;
689 int nCx;
690 int nCy;
691
692 for (int i = 0; i < m_nNumItems; i++)
fb9010ed 693 {
49dc8caa
DW
694 wxString vStr(GetString(i));
695
696 GetTextExtent( vStr
697 ,&nLine
698 ,NULL
699 );
700 if (nLine > nListbox)
701 nListbox = nLine;
fb9010ed
DW
702 }
703
49dc8caa
DW
704 //
705 // Give it some reasonable default value if there are no strings in the
706 // list.
707 //
708 if (nListbox == 0)
709 nListbox = 100;
710
711 //
712 // The listbox should be slightly larger than the widest string
713 //
714 wxGetCharSize( GetHWND()
715 ,&nCx
716 ,&nCy
717 ,(wxFont*)&GetFont()
718 );
719 nListbox += 3 * nCx;
720
721 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7));
722
723 return wxSize( nListbox
724 ,hListbox
725 );
726} // end of wxListBox::DoGetBestSize
fb9010ed 727
dcd307ee
DW
728// ----------------------------------------------------------------------------
729// callbacks
730// ----------------------------------------------------------------------------
731
49dc8caa
DW
732bool wxListBox::OS2Command(
733 WXUINT uParam
734, WXWORD WXUNUSED(wId))
0e320a79 735{
49dc8caa 736 wxEventType eEvtType;
dcd307ee 737
49dc8caa
DW
738 if (uParam == LN_SELECT)
739 {
740 eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED;
dcd307ee 741 }
49dc8caa 742 if (uParam == LN_ENTER)
0e320a79 743 {
49dc8caa 744 eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
0e320a79 745 }
fb9010ed 746 else
49dc8caa
DW
747 {
748 //
749 // Some event we're not interested in
750 //
751 return FALSE;
752 }
753 wxCommandEvent vEvent( eEvtType
754 ,m_windowId
755 );
fb9010ed 756
49dc8caa 757 vEvent.SetEventObject(this);
fb9010ed 758
49dc8caa
DW
759 wxArrayInt aSelections;
760 int n;
761 int nCount = GetSelections(aSelections);
fb9010ed 762
49dc8caa
DW
763 if (nCount > 0)
764 {
765 n = aSelections[0];
766 if (HasClientObjectData())
767 vEvent.SetClientObject(GetClientObject(n));
768 else if ( HasClientUntypedData() )
769 vEvent.SetClientData(GetClientData(n));
770 vEvent.SetString(GetString(n));
771 }
772 else
773 {
774 n = -1;
775 }
776 vEvent.m_commandInt = n;
777 return GetEventHandler()->ProcessEvent(vEvent);
778} // end of wxListBox::OS2Command
fb9010ed 779
dcd307ee
DW
780// ----------------------------------------------------------------------------
781// wxCheckListBox support
782// ----------------------------------------------------------------------------
fb9010ed
DW
783
784#if wxUSE_OWNER_DRAWN
785
49dc8caa
DW
786//
787// Drawing
fb9010ed 788// -------
49dc8caa 789//
fb9010ed
DW
790#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
791
fb9010ed
DW
792bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT *item)
793{
49dc8caa
DW
794 //
795 // TODO: Get to this eventually
796 //
fb9010ed
DW
797 return TRUE;
798}
799
fb9010ed
DW
800bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT *item)
801{
49dc8caa
DW
802 //
803 // TODO: Get to this eventually
804 //
dcd307ee 805 return FALSE;
fb9010ed 806}
49dc8caa
DW
807#endif // ndef for wxUSE_OWNER_DRAWN
808
809#endif // ndef for wxUSE_LISTBOX
7e99520b 810