]> git.saurik.com Git - wxWidgets.git/blame - src/os2/listbox.cpp
Needed to add #include "wx/statusbr.h" to know that wxStatusBar is derived
[wxWidgets.git] / src / os2 / listbox.cpp
CommitLineData
0e320a79 1///////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/os2/listbox.cpp
0e320a79 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
65571936 9// Licence: wxWindows licence
0e320a79
DW
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:
0fba44b4 52 wxListBoxItem(const wxString& rsStr = wxEmptyString);
fb9010ed
DW
53};
54
49dc8caa
DW
55wxListBoxItem::wxListBoxItem(
56 const wxString& rsStr
57)
58: wxOwnerDrawn( rsStr
ec157c8f 59 ,false
49dc8caa 60 )
fb9010ed 61{
49dc8caa
DW
62 //
63 // No bitmaps/checkmarks
64 //
fb9010ed 65 SetMarginWidth(0);
49dc8caa 66} // end of wxListBoxItem::wxListBoxItem
fb9010ed 67
6670f564 68wxOwnerDrawn* wxListBox::CreateItem( size_t WXUNUSED(n) )
fb9010ed
DW
69{
70 return new wxListBoxItem();
49dc8caa 71} // end of wxListBox::CreateItem
fb9010ed
DW
72
73#endif //USE_OWNER_DRAWN
74
0e320a79
DW
75// ============================================================================
76// list box control implementation
77// ============================================================================
78
79// Listbox item
80wxListBox::wxListBox()
81{
49dc8caa
DW
82 m_nNumItems = 0;
83 m_nSelected = 0;
84} // end of wxListBox::wxListBox
85
584ad2a3
MB
86bool wxListBox::Create(
87 wxWindow* pParent
88, wxWindowID vId
89, const wxPoint& rPos
90, const wxSize& rSize
91, const wxArrayString& asChoices
92, long lStyle
93, const wxValidator& rValidator
94, const wxString& rsName
95)
96{
97 wxCArrayString chs(asChoices);
98
99 return Create(pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(),
100 lStyle, rValidator, rsName);
101}
102
49dc8caa
DW
103bool wxListBox::Create(
104 wxWindow* pParent
105, wxWindowID vId
106, const wxPoint& rPos
107, const wxSize& rSize
108, int n
109, const wxString asChoices[]
110, long lStyle
49dc8caa 111, const wxValidator& rValidator
49dc8caa
DW
112, const wxString& rsName
113)
0e320a79 114{
49dc8caa
DW
115 m_nNumItems = 0;
116 m_hWnd = 0;
117 m_nSelected = 0;
0e320a79 118
49dc8caa 119 SetName(rsName);
5d4b632b 120#if wxUSE_VALIDATORS
49dc8caa 121 SetValidator(rValidator);
5d4b632b 122#endif
0e320a79 123
49dc8caa
DW
124 if (pParent)
125 pParent->AddChild(this);
126
127 wxSystemSettings vSettings;
128
ec157c8f 129 SetBackgroundColour(vSettings.GetColour(wxSYS_COLOUR_WINDOW));
49dc8caa 130 SetForegroundColour(pParent->GetForegroundColour());
0e320a79 131
49dc8caa 132 m_windowId = (vId == -1) ? (int)NewControlId() : vId;
0e320a79 133
49dc8caa
DW
134 int nX = rPos.x;
135 int nY = rPos.y;
136 int nWidth = rSize.x;
137 int nHeight = rSize.y;
0e320a79 138
49dc8caa 139 m_windowStyle = lStyle;
fb9010ed 140
49dc8caa
DW
141 lStyle = WS_VISIBLE;
142
143 if (m_windowStyle & wxCLIP_SIBLINGS )
144 lStyle |= WS_CLIPSIBLINGS;
fb9010ed 145 if (m_windowStyle & wxLB_MULTIPLE)
49dc8caa 146 lStyle |= LS_MULTIPLESEL;
fb9010ed 147 else if (m_windowStyle & wxLB_EXTENDED)
49dc8caa 148 lStyle |= LS_EXTENDEDSEL;
fb9010ed 149 if (m_windowStyle & wxLB_HSCROLL)
49dc8caa
DW
150 lStyle |= LS_HORZSCROLL;
151 if (m_windowStyle & wxLB_OWNERDRAW)
152 lStyle |= LS_OWNERDRAW;
fb9010ed 153
49dc8caa 154 //
fb9010ed
DW
155 // Without this style, you get unexpected heights, so e.g. constraint layout
156 // doesn't work properly
49dc8caa
DW
157 //
158 lStyle |= LS_NOADJUSTPOS;
159
160 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) // Parent
161 ,WC_LISTBOX // Default Listbox class
162 ,"LISTBOX" // Control's name
163 ,lStyle // Initial Style
164 ,0, 0, 0, 0 // Position and size
165 ,GetWinHwnd(pParent) // Owner
166 ,HWND_TOP // Z-Order
167 ,(HMENU)m_windowId // Id
168 ,NULL // Control Data
169 ,NULL // Presentation Parameters
170 );
171 if (m_hWnd == 0)
fb9010ed 172 {
ec157c8f 173 return false;
fb9010ed
DW
174 }
175
49dc8caa
DW
176 //
177 // Subclass again for purposes of dialog editing mode
178 //
fb9010ed
DW
179 SubclassWin(m_hWnd);
180
49dc8caa 181 LONG lUi;
0e320a79 182
49dc8caa
DW
183 for (lUi = 0; lUi < (LONG)n; lUi++)
184 {
185 Append(asChoices[lUi]);
186 }
b3260bce
DW
187 wxFont* pTextFont = new wxFont( 10
188 ,wxMODERN
189 ,wxNORMAL
190 ,wxNORMAL
191 );
192 SetFont(*pTextFont);
e58dab20 193
5d44b24e 194 //
77ffb593 195 // Set standard wxWidgets colors for Listbox items and highlighting
5d44b24e
DW
196 //
197 wxColour vColour;
198
0fba44b4 199 vColour.Set(wxString(wxT("WHITE")));
5d44b24e
DW
200
201 LONG lColor = (LONG)vColour.GetPixel();
202
203 ::WinSetPresParam( m_hWnd
204 ,PP_HILITEFOREGROUNDCOLOR
205 ,sizeof(LONG)
206 ,(PVOID)&lColor
207 );
0fba44b4 208 vColour.Set(wxString(wxT("NAVY")));
5d44b24e
DW
209 lColor = (LONG)vColour.GetPixel();
210 ::WinSetPresParam( m_hWnd
211 ,PP_HILITEBACKGROUNDCOLOR
212 ,sizeof(LONG)
213 ,(PVOID)&lColor
214 );
215
49dc8caa
DW
216 SetSize( nX
217 ,nY
218 ,nWidth
219 ,nHeight
220 );
b3260bce 221 delete pTextFont;
ec157c8f 222 return true;
49dc8caa 223} // end of wxListBox::Create
0e320a79
DW
224
225wxListBox::~wxListBox()
226{
fb9010ed 227#if wxUSE_OWNER_DRAWN
49dc8caa
DW
228 size_t lUiCount = m_aItems.Count();
229
230 while (lUiCount-- != 0)
231 {
232 delete m_aItems[lUiCount];
fb9010ed
DW
233 }
234#endif // wxUSE_OWNER_DRAWN
49dc8caa 235} // end of wxListBox::~wxListBox
fb9010ed
DW
236
237void wxListBox::SetupColours()
238{
a756f210 239 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
fb9010ed 240 SetForegroundColour(GetParent()->GetForegroundColour());
49dc8caa 241} // end of wxListBox::SetupColours
0e320a79 242
dcd307ee
DW
243// ----------------------------------------------------------------------------
244// implementation of wxListBoxBase methods
245// ----------------------------------------------------------------------------
246
49dc8caa
DW
247void wxListBox::DoSetFirstItem(
248 int N
249)
0e320a79 250{
49dc8caa 251 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
252 wxT("invalid index in wxListBox::SetFirstItem") );
253
49dc8caa
DW
254 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX, MPFROMLONG(N), (MPARAM)0);
255} // end of wxListBox::DoSetFirstItem
0e320a79 256
49dc8caa
DW
257void wxListBox::Delete(
258 int N
259)
0e320a79 260{
49dc8caa 261 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed
DW
262 wxT("invalid index in wxListBox::Delete") );
263
dcd307ee
DW
264#if wxUSE_OWNER_DRAWN
265 delete m_aItems[N];
893758d5 266 m_aItems.RemoveAt(N);
dcd307ee 267#else // !wxUSE_OWNER_DRAWN
49dc8caa 268 if (HasClientObjectData())
dcd307ee
DW
269 {
270 delete GetClientObject(N);
271 }
272#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
273
49dc8caa
DW
274 ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)N, (MPARAM)0);
275 m_nNumItems--;
276} // end of wxListBox::DoSetFirstItem
0e320a79 277
49dc8caa
DW
278int wxListBox::DoAppend(
279 const wxString& rsItem
280)
0e320a79 281{
9923c37d
DW
282 long lIndex = 0;
283 LONG lIndexType = 0;
49dc8caa
DW
284
285 if (m_windowStyle & wxLB_SORT)
9923c37d 286 lIndexType = LIT_SORTASCENDING;
49dc8caa 287 else
9923c37d
DW
288 lIndexType = LIT_END;
289 lIndex = (long)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)lIndexType, (MPARAM)rsItem.c_str());
49dc8caa 290 m_nNumItems++;
fb9010ed
DW
291
292#if wxUSE_OWNER_DRAWN
49dc8caa
DW
293 if (m_windowStyle & wxLB_OWNERDRAW)
294 {
9923c37d 295 wxOwnerDrawn* pNewItem = CreateItem(lIndex); // dummy argument
f5ea767e 296 wxScreenDC vDc;
f5ea767e 297
49dc8caa
DW
298
299 pNewItem->SetName(rsItem);
9923c37d
DW
300 m_aItems.Insert(pNewItem, lIndex);
301 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)lIndex, MPFROMP(pNewItem));
49dc8caa 302 pNewItem->SetFont(GetFont());
fb9010ed
DW
303 }
304#endif
9923c37d 305 return (int)lIndex;
49dc8caa 306} // end of wxListBox::DoAppend
0e320a79 307
49dc8caa
DW
308void wxListBox::DoSetItems(
309 const wxArrayString& raChoices
310, void** ppClientData
311)
0e320a79 312{
49dc8caa 313 BOOL bHideAndShow = IsShown();
49dc8caa 314 int i;
9923c37d 315 LONG lIndexType = 0;
0e320a79 316
49dc8caa 317 if (bHideAndShow)
fb9010ed 318 {
49dc8caa
DW
319 ::WinShowWindow(GetHwnd(), FALSE);
320 }
321 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
322 m_nNumItems = raChoices.GetCount();
323 for (i = 0; i < m_nNumItems; i++)
324 {
325
326 if (m_windowStyle & wxLB_SORT)
9923c37d 327 lIndexType = LIT_SORTASCENDING;
49dc8caa 328 else
9923c37d
DW
329 lIndexType = LIT_END;
330 ::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)lIndexType, (MPARAM)raChoices[i].c_str());
49dc8caa
DW
331
332 if (ppClientData)
dcd307ee
DW
333 {
334#if wxUSE_OWNER_DRAWN
49dc8caa 335 wxASSERT_MSG(ppClientData[i] == NULL,
dcd307ee
DW
336 wxT("Can't use client data with owner-drawn listboxes"));
337#else // !wxUSE_OWNER_DRAWN
49dc8caa 338 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(lCount), MPFROMP(ppClientData[i]));
dcd307ee
DW
339#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
340 }
fb9010ed 341 }
fb9010ed
DW
342
343#if wxUSE_OWNER_DRAWN
49dc8caa
DW
344 if ( m_windowStyle & wxLB_OWNERDRAW )
345 {
346 //
347 // First delete old items
348 //
3437f881 349 WX_CLEAR_ARRAY(m_aItems);
fb9010ed 350
49dc8caa
DW
351 //
352 // Then create new ones
353 //
3437f881 354 for (size_t ui = 0; ui < (size_t)m_nNumItems; ui++)
49dc8caa 355 {
3437f881 356 wxOwnerDrawn* pNewItem = CreateItem(ui);
49dc8caa 357
3437f881 358 pNewItem->SetName(raChoices[ui]);
fb9010ed 359 m_aItems.Add(pNewItem);
3437f881 360 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(ui), MPFROMP(pNewItem));
fb9010ed
DW
361 }
362 }
dcd307ee 363#endif // wxUSE_OWNER_DRAWN
49dc8caa
DW
364 ::WinShowWindow(GetHwnd(), TRUE);
365} // end of wxListBox::DoSetItems
0e320a79 366
0e320a79
DW
367void wxListBox::Clear()
368{
fb9010ed 369#if wxUSE_OWNER_DRAWN
49dc8caa
DW
370 size_t lUiCount = m_aItems.Count();
371
372 while (lUiCount-- != 0)
373 {
374 delete m_aItems[lUiCount];
fb9010ed
DW
375 }
376
377 m_aItems.Clear();
dcd307ee 378#else // !wxUSE_OWNER_DRAWN
49dc8caa 379 if (HasClientObjectData())
dcd307ee 380 {
49dc8caa 381 for (size_t n = 0; n < (size_t)m_lNumItems; n++)
dcd307ee
DW
382 {
383 delete GetClientObject(n);
384 }
385 }
386#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
49dc8caa 387 ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
dcd307ee 388
49dc8caa
DW
389 m_nNumItems = 0;
390} // end of wxListBox::Clear
fb9010ed 391
c6179a84 392void wxListBox::DoSetSelection(
49dc8caa
DW
393 int N
394, bool bSelect
395)
0e320a79 396{
49dc8caa 397 wxCHECK_RET( N >= 0 && N < m_nNumItems,
fb9010ed 398 wxT("invalid index in wxListBox::SetSelection") );
49dc8caa
DW
399 ::WinSendMsg( GetHwnd()
400 ,LM_SELECTITEM
401 ,MPFROMLONG(N)
402 ,(MPARAM)bSelect
403 );
1de4baa3
DW
404 if(m_windowStyle & wxLB_OWNERDRAW)
405 Refresh();
49dc8caa
DW
406} // end of wxListBox::SetSelection
407
408bool wxListBox::IsSelected(
409 int N
410) const
411{
ec157c8f 412 wxCHECK_MSG( N >= 0 && N < m_nNumItems, false,
fb9010ed
DW
413 wxT("invalid index in wxListBox::Selected") );
414
49dc8caa 415 LONG lItem;
0e320a79 416
70a2c656
DW
417 if (GetWindowStyleFlag() & wxLB_EXTENDED)
418 {
419 if (N == 0)
420 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0));
421 else
422 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)(N - 1), (MPARAM)0));
423 }
424 else
425 {
426 lItem = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0));
427 }
428 return (lItem == (LONG)N && lItem != LIT_NONE);
49dc8caa
DW
429} // end of wxListBox::IsSelected
430
431wxClientData* wxListBox::DoGetItemClientObject(
432 int n
433) const
0e320a79 434{
dcd307ee 435 return (wxClientData *)DoGetItemClientData(n);
0e320a79
DW
436}
437
49dc8caa
DW
438void* wxListBox::DoGetItemClientData(
439 int n
440) const
0e320a79 441{
49dc8caa 442 wxCHECK_MSG( n >= 0 && n < m_nNumItems, NULL,
fb9010ed
DW
443 wxT("invalid index in wxListBox::GetClientData") );
444
49dc8caa
DW
445 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE, MPFROMLONG(n), (MPARAM)0));
446} // end of wxListBox::DoGetItemClientData
0e320a79 447
49dc8caa
DW
448void wxListBox::DoSetItemClientObject(
449 int n
450, wxClientData* pClientData
451)
0e320a79 452{
49dc8caa
DW
453 DoSetItemClientData( n
454 ,pClientData
455 );
456} // end of wxListBox::DoSetItemClientObject
dcd307ee 457
49dc8caa
DW
458void wxListBox::DoSetItemClientData(
459 int n
460, void* pClientData
461)
dcd307ee 462{
49dc8caa 463 wxCHECK_RET( n >= 0 && n < m_nNumItems,
fb9010ed
DW
464 wxT("invalid index in wxListBox::SetClientData") );
465
dcd307ee
DW
466#if wxUSE_OWNER_DRAWN
467 if ( m_windowStyle & wxLB_OWNERDRAW )
468 {
49dc8caa
DW
469 //
470 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
dcd307ee 471 // in OnMeasure/OnDraw.
49dc8caa 472 //
dcd307ee
DW
473 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
474 }
475#endif // wxUSE_OWNER_DRAWN
476
49dc8caa
DW
477 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
478} // end of wxListBox::DoSetItemClientData
dcd307ee
DW
479
480bool wxListBox::HasMultipleSelection() const
481{
482 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
49dc8caa 483} // end of wxListBox::HasMultipleSelection
0e320a79 484
6670f564 485int wxListBox::GetSelections( wxArrayInt& raSelections ) const
0e320a79 486{
49dc8caa
DW
487 int nCount = 0;
488 LONG lItem;
0e320a79 489
fb9010ed 490
49dc8caa
DW
491 raSelections.Empty();
492 if (HasMultipleSelection())
493 {
494 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
495 ,LM_QUERYSELECTION
496 ,(MPARAM)LIT_FIRST
497 ,(MPARAM)0
498 )
499 );
500 if (lItem != LIT_NONE)
501 {
502 nCount++;
503 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
504 ,LM_QUERYSELECTION
505 ,(MPARAM)lItem
506 ,(MPARAM)0
507 )
508 )) != LIT_NONE)
509 {
510 nCount++;
511 }
512 raSelections.Alloc(nCount);
513 lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
514 ,LM_QUERYSELECTION
515 ,(MPARAM)LIT_FIRST
516 ,(MPARAM)0
517 )
518 );
519
520 raSelections.Add((int)lItem);
521 while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd()
522 ,LM_QUERYSELECTION
523 ,(MPARAM)lItem
524 ,(MPARAM)0
525 )
526 )) != LIT_NONE)
527 {
528 raSelections.Add((int)lItem);
529 }
530 return nCount;
fb9010ed 531 }
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 566 LONG lLen = 0;
0fba44b4 567 wxChar* zBuf;
49dc8caa 568 wxString sResult;
fb9010ed 569
0fba44b4 570 wxCHECK_MSG( N >= 0 && N < m_nNumItems, wxEmptyString,
49dc8caa 571 wxT("invalid index in wxListBox::GetClientData") );
0e320a79 572
49dc8caa 573 lLen = LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH, (MPARAM)N, (MPARAM)0));
0fba44b4 574 zBuf = new wxChar[lLen + 1];
49dc8caa
DW
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
9923c37d 607 ,(MPARAM)((LONG)nIndex)
3437f881
DW
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;
0d598bae 699 wxFont vFont = (wxFont)GetFont();
49dc8caa
DW
700
701 for (int i = 0; i < m_nNumItems; i++)
fb9010ed 702 {
49dc8caa
DW
703 wxString vStr(GetString(i));
704
705 GetTextExtent( vStr
706 ,&nLine
707 ,NULL
708 );
709 if (nLine > nListbox)
710 nListbox = nLine;
fb9010ed
DW
711 }
712
49dc8caa
DW
713 //
714 // Give it some reasonable default value if there are no strings in the
715 // list.
716 //
717 if (nListbox == 0)
718 nListbox = 100;
719
720 //
721 // The listbox should be slightly larger than the widest string
722 //
723 wxGetCharSize( GetHWND()
724 ,&nCx
725 ,&nCy
0d598bae 726 ,&vFont
49dc8caa
DW
727 );
728 nListbox += 3 * nCx;
729
730 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy) * (wxMax(m_nNumItems, 7));
731
732 return wxSize( nListbox
733 ,hListbox
734 );
735} // end of wxListBox::DoGetBestSize
fb9010ed 736
dcd307ee
DW
737// ----------------------------------------------------------------------------
738// callbacks
739// ----------------------------------------------------------------------------
740
49dc8caa
DW
741bool wxListBox::OS2Command(
742 WXUINT uParam
743, WXWORD WXUNUSED(wId))
0e320a79 744{
49dc8caa 745 wxEventType eEvtType;
dcd307ee 746
49dc8caa
DW
747 if (uParam == LN_SELECT)
748 {
749 eEvtType = wxEVT_COMMAND_LISTBOX_SELECTED;
dcd307ee 750 }
49dc8caa 751 if (uParam == LN_ENTER)
0e320a79 752 {
49dc8caa 753 eEvtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
0e320a79 754 }
fb9010ed 755 else
49dc8caa
DW
756 {
757 //
758 // Some event we're not interested in
759 //
ec157c8f 760 return false;
49dc8caa
DW
761 }
762 wxCommandEvent vEvent( eEvtType
763 ,m_windowId
764 );
fb9010ed 765
49dc8caa 766 vEvent.SetEventObject(this);
fb9010ed 767
49dc8caa
DW
768 wxArrayInt aSelections;
769 int n;
770 int nCount = GetSelections(aSelections);
fb9010ed 771
49dc8caa
DW
772 if (nCount > 0)
773 {
774 n = aSelections[0];
775 if (HasClientObjectData())
776 vEvent.SetClientObject(GetClientObject(n));
777 else if ( HasClientUntypedData() )
778 vEvent.SetClientData(GetClientData(n));
779 vEvent.SetString(GetString(n));
780 }
781 else
782 {
783 n = -1;
784 }
687706f5 785 vEvent.SetInt(n);
49dc8caa
DW
786 return GetEventHandler()->ProcessEvent(vEvent);
787} // end of wxListBox::OS2Command
fb9010ed 788
dcd307ee
DW
789// ----------------------------------------------------------------------------
790// wxCheckListBox support
791// ----------------------------------------------------------------------------
fb9010ed
DW
792
793#if wxUSE_OWNER_DRAWN
794
49dc8caa
DW
795//
796// Drawing
fb9010ed 797// -------
49dc8caa 798//
fb9010ed
DW
799#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
800
f5ea767e 801long wxListBox::OS2OnMeasure(
1de4baa3
DW
802 WXMEASUREITEMSTRUCT* pItem
803)
fb9010ed 804{
1de4baa3
DW
805 if (!pItem)
806 pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM;
807
808 POWNERITEM pMeasureStruct = (POWNERITEM)pItem;
809 wxScreenDC vDc;
810
49dc8caa 811 //
1de4baa3 812 // Only owner-drawn control should receive this message
49dc8caa 813 //
1de4baa3
DW
814 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
815
816 vDc.SetFont(GetFont());
817
818 wxCoord vHeight;
f5ea767e
DW
819 wxCoord vWidth;
820
821 GetSize( &vWidth
822 ,NULL
823 );
1de4baa3 824
f5ea767e 825 pMeasureStruct->rclItem.xRight = (USHORT)vWidth;
1de4baa3
DW
826 pMeasureStruct->rclItem.xLeft = 0;
827 pMeasureStruct->rclItem.yTop = 0;
828 pMeasureStruct->rclItem.yBottom = 0;
829
9923c37d 830 vHeight = (wxCoord)(vDc.GetCharHeight() * 2.5);
f5ea767e 831 pMeasureStruct->rclItem.yTop = (USHORT)vHeight;
1de4baa3 832
f5ea767e 833 return long(MRFROM2SHORT((USHORT)vHeight, (USHORT)vWidth));
1de4baa3 834} // end of wxListBox::OS2OnMeasure
fb9010ed 835
1de4baa3
DW
836bool wxListBox::OS2OnDraw (
837 WXDRAWITEMSTRUCT* pItem
838)
fb9010ed 839{
1de4baa3
DW
840 POWNERITEM pDrawStruct = (POWNERITEM)pItem;
841 LONG lItemID = pDrawStruct->idItem;
842 int eAction = 0;
843 int eStatus = 0;
844
49dc8caa 845 //
1de4baa3 846 // Only owner-drawn control should receive this message
49dc8caa 847 //
ec157c8f 848 wxCHECK(((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false);
1de4baa3
DW
849
850
851 //
852 // The item may be -1 for an empty listbox
853 //
854 if (lItemID == -1L)
ec157c8f 855 return false;
1de4baa3
DW
856
857 wxListBoxItem* pData = (wxListBoxItem*)PVOIDFROMMR( ::WinSendMsg( GetHwnd()
858 ,LM_QUERYITEMHANDLE
859 ,MPFROMLONG(pDrawStruct->idItem)
860 ,(MPARAM)0
861 )
862 );
863
ec157c8f 864 wxCHECK(pData, false );
1de4baa3 865
6670f564
WS
866 wxDC vDc;
867 wxPoint pt1( pDrawStruct->rclItem.xLeft, pDrawStruct->rclItem.yTop );
868 wxPoint pt2( pDrawStruct->rclItem.xRight, pDrawStruct->rclItem.yBottom );
869 wxRect vRect( pt1, pt2 );
1de4baa3
DW
870
871 vDc.SetHPS(pDrawStruct->hps);
872
873 if (pDrawStruct->fsAttribute == pDrawStruct->fsAttributeOld)
874 {
875 //
876 // Entire Item needs to be redrawn (either it has reappeared from
877 // behind another window or is being displayed for the first time
878 //
879 eAction = wxOwnerDrawn::wxODDrawAll;
880
881 if (pDrawStruct->fsAttribute & MIA_HILITED)
882 {
883 //
884 // If it is currently selected we let the system handle it
885 //
886 eStatus |= wxOwnerDrawn::wxODSelected;
887 }
888 if (pDrawStruct->fsAttribute & MIA_CHECKED)
889 {
890 //
891 // If it is currently checked we draw our own
892 //
893 eStatus |= wxOwnerDrawn::wxODChecked;
894 pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_CHECKED;
895 }
896 if (pDrawStruct->fsAttribute & MIA_DISABLED)
897 {
898 //
899 // If it is currently disabled we let the system handle it
900 //
901 eStatus |= wxOwnerDrawn::wxODDisabled;
902 }
903 //
904 // Don't really care about framed (indicationg focus) or NoDismiss
905 //
906 }
907 else
908 {
909 if (pDrawStruct->fsAttribute & MIA_HILITED)
910 {
911 eAction = wxOwnerDrawn::wxODDrawAll;
912 eStatus |= wxOwnerDrawn::wxODSelected;
913 //
914 // Keep the system from trying to highlight with its bogus colors
915 //
916 pDrawStruct->fsAttributeOld = pDrawStruct->fsAttribute &= ~MIA_HILITED;
917 }
918 else if (!(pDrawStruct->fsAttribute & MIA_HILITED))
919 {
920 eAction = wxOwnerDrawn::wxODDrawAll;
921 eStatus = 0;
922 //
923 // Keep the system from trying to highlight with its bogus colors
924 //
925 pDrawStruct->fsAttribute = pDrawStruct->fsAttributeOld &= ~MIA_HILITED;
926 }
927 else
928 {
929 //
930 // For now we don't care about anything else
931 // just ignore the entire message!
932 //
ec157c8f 933 return true;
1de4baa3
DW
934 }
935 }
936 return pData->OnDrawItem( vDc
937 ,vRect
938 ,(wxOwnerDrawn::wxODAction)eAction
939 ,(wxOwnerDrawn::wxODStatus)eStatus
940 );
941} // end of wxListBox::OS2OnDraw
942
49dc8caa
DW
943#endif // ndef for wxUSE_OWNER_DRAWN
944
945#endif // ndef for wxUSE_LISTBOX