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