]> git.saurik.com Git - wxWidgets.git/blame - src/os2/listbox.cpp
Send all menu item actions to a dedicated target. This is to ensure
[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"
1a75e76f 24#include "wx/dcscreen.h"
fb9010ed 25#include "wx/utils.h"
a4a16252 26#include "wx/scrolwin.h"
fb9010ed
DW
27#endif
28
29#define INCL_M
30#include <os2.h>
31
0e320a79
DW
32#include "wx/dynarray.h"
33#include "wx/log.h"
34
7e99520b
DW
35#if wxUSE_LISTBOX
36
fb9010ed
DW
37#if wxUSE_OWNER_DRAWN
38 #include "wx/ownerdrw.h"
39#endif
40
0e320a79 41 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
0e320a79 42
fb9010ed
DW
43// ============================================================================
44// list box item declaration and implementation
45// ============================================================================
46
47#if wxUSE_OWNER_DRAWN
48
49class wxListBoxItem : public wxOwnerDrawn
50{
51public:
49dc8caa 52 wxListBoxItem(const wxString& rsStr = "");
fb9010ed
DW
53};
54
49dc8caa
DW
55wxListBoxItem::wxListBoxItem(
56 const wxString& rsStr
57)
58: wxOwnerDrawn( rsStr
59 ,FALSE
60 )
fb9010ed 61{
49dc8caa
DW
62 //
63 // No bitmaps/checkmarks
64 //
fb9010ed 65 SetMarginWidth(0);
49dc8caa 66} // end of wxListBoxItem::wxListBoxItem
fb9010ed 67
49dc8caa
DW
68wxOwnerDrawn* wxListBox::CreateItem(
69 size_t n
70)
fb9010ed
DW
71{
72 return new wxListBoxItem();
49dc8caa 73} // end of wxListBox::CreateItem
fb9010ed
DW
74
75#endif //USE_OWNER_DRAWN
76
0e320a79
DW
77// ============================================================================
78// list box control implementation
79// ============================================================================
80
81// Listbox item
82wxListBox::wxListBox()
83{
49dc8caa
DW
84 m_nNumItems = 0;
85 m_nSelected = 0;
86} // end of wxListBox::wxListBox
87
88bool wxListBox::Create(
89 wxWindow* pParent
90, wxWindowID vId
91, const wxPoint& rPos
92, const wxSize& rSize
93, int n
94, const wxString asChoices[]
95, long lStyle
49dc8caa 96, const wxValidator& rValidator
49dc8caa
DW
97, const wxString& rsName
98)
0e320a79 99{
49dc8caa
DW
100 m_nNumItems = 0;
101 m_hWnd = 0;
102 m_nSelected = 0;
0e320a79 103
49dc8caa 104 SetName(rsName);
5d4b632b 105#if wxUSE_VALIDATORS
49dc8caa 106 SetValidator(rValidator);
5d4b632b 107#endif
0e320a79 108
49dc8caa
DW
109 if (pParent)
110 pParent->AddChild(this);
111
112 wxSystemSettings vSettings;
113
114 SetBackgroundColour(vSettings.GetSystemColour(wxSYS_COLOUR_WINDOW));
115 SetForegroundColour(pParent->GetForegroundColour());
0e320a79 116
49dc8caa 117 m_windowId = (vId == -1) ? (int)NewControlId() : vId;
0e320a79 118
49dc8caa
DW
119 int nX = rPos.x;
120 int nY = rPos.y;
121 int nWidth = rSize.x;
122 int nHeight = rSize.y;
0e320a79 123
49dc8caa 124 m_windowStyle = lStyle;
fb9010ed 125
49dc8caa
DW
126 lStyle = WS_VISIBLE;
127
128 if (m_windowStyle & wxCLIP_SIBLINGS )
129 lStyle |= WS_CLIPSIBLINGS;
fb9010ed 130 if (m_windowStyle & wxLB_MULTIPLE)
49dc8caa 131 lStyle |= LS_MULTIPLESEL;
fb9010ed 132 else if (m_windowStyle & wxLB_EXTENDED)
49dc8caa 133 lStyle |= LS_EXTENDEDSEL;
fb9010ed 134 if (m_windowStyle & wxLB_HSCROLL)
49dc8caa
DW
135 lStyle |= LS_HORZSCROLL;
136 if (m_windowStyle & wxLB_OWNERDRAW)
137 lStyle |= LS_OWNERDRAW;
fb9010ed 138
49dc8caa 139 //
fb9010ed
DW
140 // Without this style, you get unexpected heights, so e.g. constraint layout
141 // doesn't work properly
49dc8caa
DW
142 //
143 lStyle |= LS_NOADJUSTPOS;
144
145 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) // Parent
146 ,WC_LISTBOX // Default Listbox class
147 ,"LISTBOX" // Control's name
148 ,lStyle // Initial Style
149 ,0, 0, 0, 0 // Position and size
150 ,GetWinHwnd(pParent) // Owner
151 ,HWND_TOP // Z-Order
152 ,(HMENU)m_windowId // Id
153 ,NULL // Control Data
154 ,NULL // Presentation Parameters
155 );
156 if (m_hWnd == 0)
fb9010ed 157 {
49dc8caa 158 return FALSE;
fb9010ed
DW
159 }
160
49dc8caa
DW
161 //
162 // Subclass again for purposes of dialog editing mode
163 //
fb9010ed
DW
164 SubclassWin(m_hWnd);
165
49dc8caa 166 LONG lUi;
0e320a79 167
49dc8caa
DW
168 for (lUi = 0; lUi < (LONG)n; lUi++)
169 {
170 Append(asChoices[lUi]);
171 }
b3260bce
DW
172 wxFont* pTextFont = new wxFont( 10
173 ,wxMODERN
174 ,wxNORMAL
175 ,wxNORMAL
176 );
177 SetFont(*pTextFont);
e58dab20 178
5d44b24e
DW
179 //
180 // Set standard wxWindows colors for Listbox items and highlighting
181 //
182 wxColour vColour;
183
184 vColour.Set(wxString("WHITE"));
185
186 LONG lColor = (LONG)vColour.GetPixel();
187
188 ::WinSetPresParam( m_hWnd
189 ,PP_HILITEFOREGROUNDCOLOR
190 ,sizeof(LONG)
191 ,(PVOID)&lColor
192 );
193 vColour.Set(wxString("NAVY"));
194 lColor = (LONG)vColour.GetPixel();
195 ::WinSetPresParam( m_hWnd
196 ,PP_HILITEBACKGROUNDCOLOR
197 ,sizeof(LONG)
198 ,(PVOID)&lColor
199 );
200
49dc8caa
DW
201 SetSize( nX
202 ,nY
203 ,nWidth
204 ,nHeight
205 );
b3260bce 206 delete pTextFont;
dcd307ee 207 return TRUE;
49dc8caa 208} // end of wxListBox::Create
0e320a79
DW
209
210wxListBox::~wxListBox()
211{
fb9010ed 212#if wxUSE_OWNER_DRAWN
49dc8caa
DW
213 size_t lUiCount = m_aItems.Count();
214
215 while (lUiCount-- != 0)
216 {
217 delete m_aItems[lUiCount];
fb9010ed
DW
218 }
219#endif // wxUSE_OWNER_DRAWN
49dc8caa 220} // end of wxListBox::~wxListBox
fb9010ed
DW
221
222void wxListBox::SetupColours()
223{
a756f210 224 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
fb9010ed 225 SetForegroundColour(GetParent()->GetForegroundColour());
49dc8caa 226} // end of wxListBox::SetupColours
0e320a79 227
dcd307ee
DW
228// ----------------------------------------------------------------------------
229// implementation of wxListBoxBase methods
230// ----------------------------------------------------------------------------
231
49dc8caa
DW
232void wxListBox::DoSetFirstItem(
233 int N
234)
0e320a79 235{
49dc8caa 236 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
237 wxT("invalid index in wxListBox::SetFirstItem") );
238
49dc8caa
DW
239 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX, MPFROMLONG(N), (MPARAM)0);
240} // end of wxListBox::DoSetFirstItem
0e320a79 241
49dc8caa
DW
242void wxListBox::Delete(
243 int N
244)
0e320a79 245{
49dc8caa 246 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
247 wxT("invalid index in wxListBox::Delete") );
248
dcd307ee
DW
249#if wxUSE_OWNER_DRAWN
250 delete m_aItems[N];
893758d5 251 m_aItems.RemoveAt(N);
dcd307ee 252#else // !wxUSE_OWNER_DRAWN
49dc8caa 253 if (HasClientObjectData())
dcd307ee
DW
254 {
255 delete GetClientObject(N);
256 }
257#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
258
49dc8caa
DW
259 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)N, (MPARAM)0);
260 m_nNumItems--;
261} // end of wxListBox::DoSetFirstItem
0e320a79 262
49dc8caa
DW
263int wxListBox::DoAppend(
264 const wxString& rsItem
265)
0e320a79 266{
49dc8caa
DW
267 int nIndex = 0;
268 SHORT nIndexType = 0;
269
270 if (m_windowStyle & wxLB_SORT)
271 nIndexType = LIT_SORTASCENDING;
272 else
273 nIndexType = LIT_END;
274 nIndex = (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)rsItem.c_str());
275 m_nNumItems++;
fb9010ed
DW
276
277#if wxUSE_OWNER_DRAWN
49dc8caa
DW
278 if (m_windowStyle & wxLB_OWNERDRAW)
279 {
280 wxOwnerDrawn* pNewItem = CreateItem(nIndex); // dummy argument
f5ea767e
DW
281 wxScreenDC vDc;
282 wxCoord vHeight;
283
49dc8caa
DW
284
285 pNewItem->SetName(rsItem);
3437f881 286 m_aItems.Insert(pNewItem, nIndex);
49dc8caa
DW
287 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)((SHORT)nIndex), MPFROMP(pNewItem));
288 pNewItem->SetFont(GetFont());
fb9010ed
DW
289 }
290#endif
49dc8caa
DW
291 return nIndex;
292} // end of wxListBox::DoAppend
0e320a79 293
49dc8caa
DW
294void wxListBox::DoSetItems(
295 const wxArrayString& raChoices
296, void** ppClientData
297)
0e320a79 298{
49dc8caa
DW
299 BOOL bHideAndShow = IsShown();
300 int nCount = 0;
301 int i;
302 SHORT nIndexType = 0;
0e320a79 303
49dc8caa 304 if (bHideAndShow)
fb9010ed 305 {
49dc8caa
DW
306 ::WinShowWindow(GetHwnd(), FALSE);
307 }
308 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
309 m_nNumItems = raChoices.GetCount();
310 for (i = 0; i < m_nNumItems; i++)
311 {
312
313 if (m_windowStyle & wxLB_SORT)
314 nIndexType = LIT_SORTASCENDING;
315 else
316 nIndexType = LIT_END;
317 ::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)nIndexType, (MPARAM)raChoices[i].c_str());
318
319 if (ppClientData)
dcd307ee
DW
320 {
321#if wxUSE_OWNER_DRAWN
49dc8caa 322 wxASSERT_MSG(ppClientData[i] == NULL,
dcd307ee
DW
323 wxT("Can't use client data with owner-drawn listboxes"));
324#else // !wxUSE_OWNER_DRAWN
49dc8caa 325 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lCount), MPFROMP(ppClientData[i]));
dcd307ee
DW
326#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
327 }
fb9010ed 328 }
fb9010ed
DW
329
330#if wxUSE_OWNER_DRAWN
49dc8caa
DW
331 if ( m_windowStyle & wxLB_OWNERDRAW )
332 {
333 //
334 // First delete old items
335 //
3437f881 336 WX_CLEAR_ARRAY(m_aItems);
fb9010ed 337
49dc8caa
DW
338 //
339 // Then create new ones
340 //
3437f881 341 for (size_t ui = 0; ui < (size_t)m_nNumItems; ui++)
49dc8caa 342 {
3437f881 343 wxOwnerDrawn* pNewItem = CreateItem(ui);
49dc8caa 344
3437f881 345 pNewItem->SetName(raChoices[ui]);
fb9010ed 346 m_aItems.Add(pNewItem);
3437f881 347 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(ui), MPFROMP(pNewItem));
fb9010ed
DW
348 }
349 }
dcd307ee 350#endif // wxUSE_OWNER_DRAWN
49dc8caa
DW
351 ::WinShowWindow(GetHwnd(), TRUE);
352} // end of wxListBox::DoSetItems
0e320a79 353
49dc8caa
DW
354int wxListBox::FindString(
355 const wxString& rsString
356) const
357{
358 int nPos;
359 LONG lTextLength;
360 PSZ zStr;
dcd307ee 361
0e320a79 362
49dc8caa
DW
363 for (nPos = 0; nPos < m_nNumItems; nPos++)
364 {
365 lTextLength = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)nPos, (MPARAM)0));
366 zStr = new char[lTextLength + 1];
367 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT(nPos, (SHORT)lTextLength), (MPARAM)zStr);
368 if (rsString == (char*)zStr)
369 {
370 delete [] zStr;
371 break;
372 }
373 delete [] zStr;
374 }
375 return nPos;
376} // end of wxListBox::FindString
0e320a79
DW
377
378void wxListBox::Clear()
379{
fb9010ed 380#if wxUSE_OWNER_DRAWN
49dc8caa
DW
381 size_t lUiCount = m_aItems.Count();
382
383 while (lUiCount-- != 0)
384 {
385 delete m_aItems[lUiCount];
fb9010ed
DW
386 }
387
388 m_aItems.Clear();
dcd307ee 389#else // !wxUSE_OWNER_DRAWN
49dc8caa 390 if (HasClientObjectData())
dcd307ee 391 {
49dc8caa 392 for (size_t n = 0; n < (size_t)m_lNumItems; n++)
dcd307ee
DW
393 {
394 delete GetClientObject(n);
395 }
396 }
397#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
49dc8caa 398 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
dcd307ee 399
49dc8caa
DW
400 m_nNumItems = 0;
401} // end of wxListBox::Clear
fb9010ed 402
49dc8caa
DW
403void wxListBox::SetSelection(
404 int N
405, bool bSelect
406)
0e320a79 407{
49dc8caa 408 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed 409 wxT("invalid index in wxListBox::SetSelection") );
49dc8caa
DW
410 ::WinSendMsg( GetHwnd()
411 ,LM_SELECTITEM
412 ,MPFROMLONG(N)
413 ,(MPARAM)bSelect
414 );
1de4baa3
DW
415 if(m_windowStyle & wxLB_OWNERDRAW)
416 Refresh();
49dc8caa
DW
417} // end of wxListBox::SetSelection
418
419bool wxListBox::IsSelected(
420 int N
421) const
422{
423 wxCHECK_MSG( N >= 0 && N < m_nNumItems, FALSE,
fb9010ed
DW
424 wxT("invalid index in wxListBox::Selected") );
425
49dc8caa 426 LONG lItem;
0e320a79 427
70a2c656
DW
428 if (GetWindowStyleFlag() & wxLB_EXTENDED)
429 {
430 if (N == 0)
431 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0));
432 else
433 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)(N - 1), (MPARAM)0));
434 }
435 else
436 {
437 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0));
438 }
439 return (lItem == (LONG)N && lItem != LIT_NONE);
49dc8caa
DW
440} // end of wxListBox::IsSelected
441
442wxClientData* wxListBox::DoGetItemClientObject(
443 int n
444) const
0e320a79 445{
dcd307ee 446 return (wxClientData *)DoGetItemClientData(n);
0e320a79
DW
447}
448
49dc8caa
DW
449void* wxListBox::DoGetItemClientData(
450 int n
451) const
0e320a79 452{
49dc8caa 453 wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL,
fb9010ed
DW
454 wxT("invalid index in wxListBox::GetClientData") );
455
49dc8caa
DW
456 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0));
457} // end of wxListBox::DoGetItemClientData
0e320a79 458
49dc8caa
DW
459void wxListBox::DoSetItemClientObject(
460 int n
461, wxClientData* pClientData
462)
0e320a79 463{
49dc8caa
DW
464 DoSetItemClientData( n
465 ,pClientData
466 );
467} // end of wxListBox::DoSetItemClientObject
dcd307ee 468
49dc8caa
DW
469void wxListBox::DoSetItemClientData(
470 int n
471, void* pClientData
472)
dcd307ee 473{
49dc8caa 474 wxCHECK_RET( n >= 0 && n < m_nNumItems,
fb9010ed
DW
475 wxT("invalid index in wxListBox::SetClientData") );
476
dcd307ee
DW
477#if wxUSE_OWNER_DRAWN
478 if ( m_windowStyle & wxLB_OWNERDRAW )
479 {
49dc8caa
DW
480 //
481 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
dcd307ee 482 // in OnMeasure/OnDraw.
49dc8caa 483 //
dcd307ee
DW
484 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
485 }
486#endif // wxUSE_OWNER_DRAWN
487
49dc8caa
DW
488 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
489} // end of wxListBox::DoSetItemClientData
dcd307ee
DW
490
491bool wxListBox::HasMultipleSelection() const
492{
493 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
49dc8caa 494} // end of wxListBox::HasMultipleSelection
0e320a79 495
49dc8caa
DW
496int wxListBox::GetSelections(
497 wxArrayInt& raSelections
498) const
0e320a79 499{
49dc8caa
DW
500 int nCount = 0;
501 LONG lItem;
0e320a79 502
fb9010ed 503
49dc8caa
DW
504 raSelections.Empty();
505 if (HasMultipleSelection())
506 {
507 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
508 ,LM_QUERYSELECTION
509 ,(MPARAM)LIT_FIRST
510 ,(MPARAM)0
511 )
512 );
513 if (lItem != LIT_NONE)
514 {
515 nCount++;
516 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
517 ,LM_QUERYSELECTION
518 ,(MPARAM)lItem
519 ,(MPARAM)0
520 )
521 )) != LIT_NONE)
522 {
523 nCount++;
524 }
525 raSelections.Alloc(nCount);
526 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
527 ,LM_QUERYSELECTION
528 ,(MPARAM)LIT_FIRST
529 ,(MPARAM)0
530 )
531 );
532
533 raSelections.Add((int)lItem);
534 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
535 ,LM_QUERYSELECTION
536 ,(MPARAM)lItem
537 ,(MPARAM)0
538 )
539 )) != LIT_NONE)
540 {
541 raSelections.Add((int)lItem);
542 }
543 return nCount;
fb9010ed 544 }
49dc8caa 545 return 0;
0e320a79
DW
546 }
547 else // single-selection listbox
548 {
49dc8caa
DW
549 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
550 ,LM_QUERYSELECTION
551 ,(MPARAM)LIT_FIRST
552 ,(MPARAM)0
553 )
554 );
555 raSelections.Add((int)lItem);
0e320a79
DW
556 return 1;
557 }
dcd307ee 558 return 0;
49dc8caa 559} // end of wxListBox::GetSelections
0e320a79 560
0e320a79
DW
561int wxListBox::GetSelection() const
562{
dcd307ee 563 wxCHECK_MSG( !HasMultipleSelection(),
fb9010ed
DW
564 -1,
565 wxT("GetSelection() can't be used with multiple-selection "
566 "listboxes, use GetSelections() instead.") );
567
49dc8caa
DW
568 return(LONGFROMMR(::WinSendMsg( GetHwnd()
569 ,LM_QUERYSELECTION
570 ,(MPARAM)LIT_FIRST
571 ,(MPARAM)0
572 )
573 ));
574} // end of wxListBox::GetSelection
0e320a79 575
49dc8caa
DW
576wxString wxListBox::GetString(
577 int N
578) const
0e320a79 579{
49dc8caa
DW
580 LONG lLen = 0;
581 char* zBuf;
582 wxString sResult;
fb9010ed 583
49dc8caa
DW
584 wxCHECK_MSG( N >= 0 && N < m_nNumItems, "",
585 wxT("invalid index in wxListBox::GetClientData") );
0e320a79 586
49dc8caa
DW
587 lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0));
588 zBuf = new char[lLen + 1];
589 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT, MPFROM2SHORT((SHORT)N, (SHORT)lLen), (MPARAM)zBuf);
590 zBuf[lLen] = '\0';
591 sResult = zBuf;
592 delete [] zBuf;
593 return sResult;
594} // end of wxListBox::GetString
595
596void wxListBox::DoInsertItems(
597 const wxArrayString& asItems
598, int nPos
599)
600{
601 wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems,
dcd307ee
DW
602 wxT("invalid index in wxListBox::InsertItems") );
603
49dc8caa 604 int nItems = asItems.GetCount();
dcd307ee 605
49dc8caa 606 for (int i = 0; i < nItems; i++)
3437f881
DW
607 {
608 int nIndex = (int)::WinSendMsg( GetHwnd()
609 ,LM_INSERTITEM
610 ,MPFROMLONG((LONG)(i + nPos))
611 ,(MPARAM)asItems[i].c_str()
612 );
613
614 wxOwnerDrawn* pNewItem = CreateItem(nIndex);
615
616 pNewItem->SetName(asItems[i]);
617 pNewItem->SetFont(GetFont());
618 m_aItems.Insert(pNewItem, nIndex);
619 ::WinSendMsg( GetHwnd()
620 ,LM_SETITEMHANDLE
621 ,(MPARAM)((SHORT)nIndex)
622 ,MPFROMP(pNewItem)
623 );
624 m_nNumItems += nItems;
625 }
49dc8caa 626} // end of wxListBox::DoInsertItems
dcd307ee 627
49dc8caa
DW
628void wxListBox::SetString(
629 int N
630, const wxString& rsString
631)
dcd307ee 632{
49dc8caa 633 wxCHECK_RET( N >= 0 && N < m_nNumItems,
dcd307ee
DW
634 wxT("invalid index in wxListBox::SetString") );
635
49dc8caa
DW
636 //
637 // Remember the state of the item
638 //
639 bool bWasSelected = IsSelected(N);
640 void* pOldData = NULL;
641 wxClientData* pOldObjData = NULL;
642
643 if (m_clientDataItemsType == wxClientData_Void)
644 pOldData = GetClientData(N);
645 else if (m_clientDataItemsType == wxClientData_Object)
646 pOldObjData = GetClientObject(N);
647
648 //
649 // Delete and recreate it
650 //
651 ::WinSendMsg( GetHwnd()
652 ,LM_DELETEITEM
653 ,(MPARAM)N
654 ,(MPARAM)0
655 );
656
657 int nNewN = N;
658
659 if (N == m_nNumItems - 1)
660 nNewN = -1;
661
662 ::WinSendMsg( GetHwnd()
663 ,LM_INSERTITEM
664 ,(MPARAM)nNewN
665 ,(MPARAM)rsString.c_str()
666 );
667
668 //
669 // Restore the client data
670 //
671 if (pOldData)
672 SetClientData( N
673 ,pOldData
674 );
675 else if (pOldObjData)
676 SetClientObject( N
677 ,pOldObjData
678 );
679
680 //
681 // We may have lost the selection
682 //
683 if (bWasSelected)
dcd307ee
DW
684 Select(N);
685
686#if wxUSE_OWNER_DRAWN
49dc8caa
DW
687 if (m_windowStyle & wxLB_OWNERDRAW)
688 //
689 // Update item's text
690 //
691 m_aItems[N]->SetName(rsString);
dcd307ee 692#endif //USE_OWNER_DRAWN
49dc8caa 693} // end of wxListBox::SetString
dcd307ee
DW
694
695int wxListBox::GetCount() const
696{
49dc8caa 697 return m_nNumItems;
dcd307ee
DW
698}
699
700// ----------------------------------------------------------------------------
701// helpers
702// ----------------------------------------------------------------------------
703
e78c4d50 704wxSize wxListBox::DoGetBestSize() const
fb9010ed 705{
49dc8caa
DW
706 //
707 // Find the widest string
708 //
709 int nLine;
710 int nListbox = 0;
711 int nCx;
712 int nCy;
713
714 for (int i = 0; i < m_nNumItems; i++)
fb9010ed 715 {
49dc8caa
DW
716 wxString vStr(GetString(i));
717
718 GetTextExtent( vStr
719 ,&nLine
720 ,NULL
721 );
722 if (nLine > nListbox)
723 nListbox = nLine;
fb9010ed
DW
724 }
725
49dc8caa
DW
726 //
727 // Give it some reasonable default value if there are no strings in the
728 // list.
729 //
730 if (nListbox == 0)
731 nListbox = 100;
732
733 //
734 // The listbox should be slightly larger than the widest string
735 //
736 wxGetCharSize( GetHWND()
737 ,&nCx
738 ,&nCy
739 ,(wxFont*)&GetFont()
740 );
741 nListbox += 3 * nCx;
742
743 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7));
744
745 return wxSize( nListbox
746 ,hListbox
747 );
748} // end of wxListBox::DoGetBestSize
fb9010ed 749
dcd307ee
DW
750// ----------------------------------------------------------------------------
751// callbacks
752// ----------------------------------------------------------------------------
753
49dc8caa
DW
754bool wxListBox::OS2Command(
755 WXUINT uParam
756, WXWORD WXUNUSED(wId))
0e320a79 757{
49dc8caa 758 wxEventType eEvtType;
dcd307ee 759
49dc8caa
DW
760 if (uParam == LN_SELECT)
761 {
762 eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED;
dcd307ee 763 }
49dc8caa 764 if (uParam == LN_ENTER)
0e320a79 765 {
49dc8caa 766 eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
0e320a79 767 }
fb9010ed 768 else
49dc8caa
DW
769 {
770 //
771 // Some event we're not interested in
772 //
773 return FALSE;
774 }
775 wxCommandEvent vEvent( eEvtType
776 ,m_windowId
777 );
fb9010ed 778
49dc8caa 779 vEvent.SetEventObject(this);
fb9010ed 780
49dc8caa
DW
781 wxArrayInt aSelections;
782 int n;
783 int nCount = GetSelections(aSelections);
fb9010ed 784
49dc8caa
DW
785 if (nCount > 0)
786 {
787 n = aSelections[0];
788 if (HasClientObjectData())
789 vEvent.SetClientObject(GetClientObject(n));
790 else if ( HasClientUntypedData() )
791 vEvent.SetClientData(GetClientData(n));
792 vEvent.SetString(GetString(n));
793 }
794 else
795 {
796 n = -1;
797 }
798 vEvent.m_commandInt = n;
799 return GetEventHandler()->ProcessEvent(vEvent);
800} // end of wxListBox::OS2Command
fb9010ed 801
dcd307ee
DW
802// ----------------------------------------------------------------------------
803// wxCheckListBox support
804// ----------------------------------------------------------------------------
fb9010ed
DW
805
806#if wxUSE_OWNER_DRAWN
807
49dc8caa
DW
808//
809// Drawing
fb9010ed 810// -------
49dc8caa 811//
fb9010ed
DW
812#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
813
f5ea767e 814long wxListBox::OS2OnMeasure(
1de4baa3
DW
815 WXMEASUREITEMSTRUCT* pItem
816)
fb9010ed 817{
1de4baa3
DW
818 if (!pItem)
819 pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM;
820
821 POWNERITEM pMeasureStruct = (POWNERITEM)pItem;
822 wxScreenDC vDc;
823
49dc8caa 824 //
1de4baa3 825 // Only owner-drawn control should receive this message
49dc8caa 826 //
1de4baa3
DW
827 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
828
829 vDc.SetFont(GetFont());
830
831 wxCoord vHeight;
f5ea767e
DW
832 wxCoord vWidth;
833
834 GetSize( &vWidth
835 ,NULL
836 );
1de4baa3 837
f5ea767e 838 pMeasureStruct->rclItem.xRight = (USHORT)vWidth;
1de4baa3
DW
839 pMeasureStruct->rclItem.xLeft = 0;
840 pMeasureStruct->rclItem.yTop = 0;
841 pMeasureStruct->rclItem.yBottom = 0;
842
843 vHeight = vDc.GetCharHeight() * 2.5;
f5ea767e 844 pMeasureStruct->rclItem.yTop = (USHORT)vHeight;
1de4baa3 845
f5ea767e 846 return long(MRFROM2SHORT((USHORT)vHeight, (USHORT)vWidth));
1de4baa3 847} // end of wxListBox::OS2OnMeasure
fb9010ed 848
1de4baa3
DW
849bool wxListBox::OS2OnDraw (
850 WXDRAWITEMSTRUCT* pItem
851)
fb9010ed 852{
1de4baa3
DW
853 POWNERITEM pDrawStruct = (POWNERITEM)pItem;
854 LONG lItemID = pDrawStruct->idItem;
855 int eAction = 0;
856 int eStatus = 0;
857
49dc8caa 858 //
1de4baa3 859 // Only owner-drawn control should receive this message
49dc8caa 860 //
1de4baa3
DW
861 wxCHECK(((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE);
862
863
864 //
865 // The item may be -1 for an empty listbox
866 //
867 if (lItemID == -1L)
868 return FALSE;
869
870 wxListBoxItem* pData = (wxListBoxItem*)PVOIDFROMMR( ::WinSendMsg( GetHwnd()
871 ,LM_QUERYITEMHANDLE
872 ,MPFROMLONG(pDrawStruct->idItem)
873 ,(MPARAM)0
874 )
875 );
876
877 wxCHECK(pData, FALSE );
878
879 wxDC vDc;
880 wxRect vRect( wxPoint( pDrawStruct->rclItem.xLeft
881 ,pDrawStruct->rclItem.yTop
882 )
883 ,wxPoint( pDrawStruct->rclItem.xRight
884 ,pDrawStruct->rclItem.yBottom
885 )
886 );
887
888 vDc.SetHPS(pDrawStruct->hps);
889
890 if (pDrawStruct->fsAttribute == pDrawStruct->fsAttributeOld)
891 {
892 //
893 // Entire Item needs to be redrawn (either it has reappeared from
894 // behind another window or is being displayed for the first time
895 //
896 eAction = wxOwnerDrawn::wxODDrawAll;
897
898 if (pDrawStruct->fsAttribute & MIA_HILITED)
899 {
900 //
901 // If it is currently selected we let the system handle it
902 //
903 eStatus |= wxOwnerDrawn::wxODSelected;
904 }
905 if (pDrawStruct->fsAttribute & MIA_CHECKED)
906 {
907 //
908 // If it is currently checked we draw our own
909 //
910 eStatus |= wxOwnerDrawn::wxODChecked;
911 pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_CHECKED;
912 }
913 if (pDrawStruct->fsAttribute & MIA_DISABLED)
914 {
915 //
916 // If it is currently disabled we let the system handle it
917 //
918 eStatus |= wxOwnerDrawn::wxODDisabled;
919 }
920 //
921 // Don't really care about framed (indicationg focus) or NoDismiss
922 //
923 }
924 else
925 {
926 if (pDrawStruct->fsAttribute & MIA_HILITED)
927 {
928 eAction = wxOwnerDrawn::wxODDrawAll;
929 eStatus |= wxOwnerDrawn::wxODSelected;
930 //
931 // Keep the system from trying to highlight with its bogus colors
932 //
933 pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_HILITED;
934 }
935 else if (!(pDrawStruct->fsAttribute & MIA_HILITED))
936 {
937 eAction = wxOwnerDrawn::wxODDrawAll;
938 eStatus = 0;
939 //
940 // Keep the system from trying to highlight with its bogus colors
941 //
942 pDrawStruct->fsAttribute = pDrawStruct->fsAttributeOld &= ~MIA_HILITED;
943 }
944 else
945 {
946 //
947 // For now we don't care about anything else
948 // just ignore the entire message!
949 //
950 return TRUE;
951 }
952 }
953 return pData->OnDrawItem( vDc
954 ,vRect
955 ,(wxOwnerDrawn::wxODAction)eAction
956 ,(wxOwnerDrawn::wxODStatus)eStatus
957 );
958} // end of wxListBox::OS2OnDraw
959
49dc8caa
DW
960#endif // ndef for wxUSE_OWNER_DRAWN
961
962#endif // ndef for wxUSE_LISTBOX
7e99520b 963