/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/listbox.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/listbox.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (9 years, 11 months ago) by william
File size: 23010 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listbox.cpp
3 // Purpose: wxListBox
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (owner drawn stuff)
6 // Created:
7 // RCS-ID: $Id: listbox.cpp 58754 2009-02-08 10:29:09Z VZ $
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_LISTBOX
20
21 #include "wx/listbox.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #include "wx/settings.h"
26 #include "wx/brush.h"
27 #include "wx/font.h"
28 #include "wx/dc.h"
29 #include "wx/utils.h"
30 #include "wx/log.h"
31 #include "wx/window.h"
32 #endif
33
34 #include "wx/msw/private.h"
35
36 #include <windowsx.h>
37
38 #if wxUSE_OWNER_DRAWN
39 #include "wx/ownerdrw.h"
40 #endif
41
42 #if wxUSE_EXTENDED_RTTI
43 WX_DEFINE_FLAGS( wxListBoxStyle )
44
45 wxBEGIN_FLAGS( wxListBoxStyle )
46 // new style border flags, we put them first to
47 // use them for streaming out
48 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
49 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
50 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
51 wxFLAGS_MEMBER(wxBORDER_RAISED)
52 wxFLAGS_MEMBER(wxBORDER_STATIC)
53 wxFLAGS_MEMBER(wxBORDER_NONE)
54
55 // old style border flags
56 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
57 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
58 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
59 wxFLAGS_MEMBER(wxRAISED_BORDER)
60 wxFLAGS_MEMBER(wxSTATIC_BORDER)
61 wxFLAGS_MEMBER(wxBORDER)
62
63 // standard window styles
64 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
65 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
66 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
67 wxFLAGS_MEMBER(wxWANTS_CHARS)
68 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
69 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
70 wxFLAGS_MEMBER(wxVSCROLL)
71 wxFLAGS_MEMBER(wxHSCROLL)
72
73 wxFLAGS_MEMBER(wxLB_SINGLE)
74 wxFLAGS_MEMBER(wxLB_MULTIPLE)
75 wxFLAGS_MEMBER(wxLB_EXTENDED)
76 wxFLAGS_MEMBER(wxLB_HSCROLL)
77 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
78 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
79 wxFLAGS_MEMBER(wxLB_SORT)
80
81 wxEND_FLAGS( wxListBoxStyle )
82
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
84
85 wxBEGIN_PROPERTIES_TABLE(wxListBox)
86 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
87 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
88
89 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
90 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
91 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
92 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
93 wxEND_PROPERTIES_TABLE()
94
95 wxBEGIN_HANDLERS_TABLE(wxListBox)
96 wxEND_HANDLERS_TABLE()
97
98 wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
99 #else
100 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
101 #endif
102
103 /*
104 TODO PROPERTIES
105 selection
106 content
107 item
108 */
109
110 // ============================================================================
111 // list box item declaration and implementation
112 // ============================================================================
113
114 #if wxUSE_OWNER_DRAWN
115
116 class wxListBoxItem : public wxOwnerDrawn
117 {
118 public:
119 wxListBoxItem(const wxString& str = wxEmptyString);
120 };
121
122 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false)
123 {
124 // no bitmaps/checkmarks
125 SetMarginWidth(0);
126 }
127
128 wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
129 {
130 return new wxListBoxItem();
131 }
132
133 #endif //USE_OWNER_DRAWN
134
135 // ============================================================================
136 // list box control implementation
137 // ============================================================================
138
139 // ----------------------------------------------------------------------------
140 // creation
141 // ----------------------------------------------------------------------------
142
143 // Listbox item
144 wxListBox::wxListBox()
145 {
146 m_noItems = 0;
147 m_selected = 0;
148 }
149
150 bool wxListBox::Create(wxWindow *parent,
151 wxWindowID id,
152 const wxPoint& pos,
153 const wxSize& size,
154 int n, const wxString choices[],
155 long style,
156 const wxValidator& validator,
157 const wxString& name)
158 {
159 m_noItems = 0;
160 m_selected = 0;
161
162 // initialize base class fields
163 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
164 return false;
165
166 // create the native control
167 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
168 {
169 // control creation failed
170 return false;
171 }
172
173 // initialize the contents
174 for ( int i = 0; i < n; i++ )
175 {
176 Append(choices[i]);
177 }
178
179 // now we can compute our best size correctly, so do it if necessary
180 SetInitialSize(size);
181
182 return true;
183 }
184
185 bool wxListBox::Create(wxWindow *parent,
186 wxWindowID id,
187 const wxPoint& pos,
188 const wxSize& size,
189 const wxArrayString& choices,
190 long style,
191 const wxValidator& validator,
192 const wxString& name)
193 {
194 wxCArrayString chs(choices);
195 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
196 style, validator, name);
197 }
198
199 wxListBox::~wxListBox()
200 {
201 Free();
202 }
203
204 WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
205 {
206 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
207
208 // always show the vertical scrollbar if necessary -- otherwise it is
209 // impossible to use the control with the mouse
210 msStyle |= WS_VSCROLL;
211
212 // we always want to get the notifications
213 msStyle |= LBS_NOTIFY;
214
215 // without this style, you get unexpected heights, so e.g. constraint
216 // layout doesn't work properly
217 msStyle |= LBS_NOINTEGRALHEIGHT;
218
219 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
220 _T("only one of listbox selection modes can be specified") );
221
222 if ( style & wxLB_MULTIPLE )
223 msStyle |= LBS_MULTIPLESEL;
224 else if ( style & wxLB_EXTENDED )
225 msStyle |= LBS_EXTENDEDSEL;
226
227 if ( m_windowStyle & wxLB_ALWAYS_SB )
228 msStyle |= LBS_DISABLENOSCROLL;
229 if ( m_windowStyle & wxLB_HSCROLL )
230 msStyle |= WS_HSCROLL;
231 if ( m_windowStyle & wxLB_SORT )
232 msStyle |= LBS_SORT;
233
234 #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
235 if ( m_windowStyle & wxLB_OWNERDRAW )
236 {
237 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
238 // the strings in the listbox for simplicity even though we could have
239 // avoided it in this case
240 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
241 }
242 #endif // wxUSE_OWNER_DRAWN
243
244 return msStyle;
245 }
246
247 // ----------------------------------------------------------------------------
248 // implementation of wxListBoxBase methods
249 // ----------------------------------------------------------------------------
250
251 void wxListBox::DoSetFirstItem(int N)
252 {
253 wxCHECK_RET( IsValid(N),
254 wxT("invalid index in wxListBox::SetFirstItem") );
255
256 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
257 }
258
259 void wxListBox::Delete(unsigned int n)
260 {
261 wxCHECK_RET( IsValid(n),
262 wxT("invalid index in wxListBox::Delete") );
263
264 // for owner drawn objects, the data is used for storing wxOwnerDrawn
265 // pointers and we shouldn't touch it
266 #if !wxUSE_OWNER_DRAWN
267 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
268 #endif // !wxUSE_OWNER_DRAWN
269 if ( HasClientObjectData() )
270 {
271 delete GetClientObject(n);
272 }
273
274 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
275 m_noItems--;
276
277 SetHorizontalExtent(wxEmptyString);
278 }
279
280 int wxListBox::DoAppend(const wxString& item)
281 {
282 int index = ListBox_AddString(GetHwnd(), item);
283 m_noItems++;
284
285 #if wxUSE_OWNER_DRAWN
286 if ( m_windowStyle & wxLB_OWNERDRAW ) {
287 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
288 pNewItem->SetName(item);
289 m_aItems.Insert(pNewItem, index);
290 ListBox_SetItemData(GetHwnd(), index, pNewItem);
291 pNewItem->SetFont(GetFont());
292 }
293 #endif // wxUSE_OWNER_DRAWN
294
295 SetHorizontalExtent(item);
296
297 return index;
298 }
299
300 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
301 {
302 // avoid flicker - but don't need to do this for a hidden listbox
303 bool hideAndShow = IsShown();
304 if ( hideAndShow )
305 {
306 ShowWindow(GetHwnd(), SW_HIDE);
307 }
308
309 ListBox_ResetContent(GetHwnd());
310
311 m_noItems = choices.GetCount();
312 unsigned int i;
313 for (i = 0; i < m_noItems; i++)
314 {
315 ListBox_AddString(GetHwnd(), choices[i]);
316 if ( clientData )
317 {
318 SetClientData(i, clientData[i]);
319 }
320 }
321
322 #if wxUSE_OWNER_DRAWN
323 if ( m_windowStyle & wxLB_OWNERDRAW ) {
324 // first delete old items
325 WX_CLEAR_ARRAY(m_aItems);
326
327 // then create new ones
328 for ( unsigned int ui = 0; ui < m_noItems; ui++ ) {
329 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
330 pNewItem->SetName(choices[ui]);
331 m_aItems.Add(pNewItem);
332 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
333 }
334 }
335 #endif // wxUSE_OWNER_DRAWN
336
337 SetHorizontalExtent();
338
339 if ( hideAndShow )
340 {
341 // show the listbox back if we hid it
342 ShowWindow(GetHwnd(), SW_SHOW);
343 }
344 }
345
346 int wxListBox::FindString(const wxString& s, bool bCase) const
347 {
348 // back to base class search for not native search type
349 if (bCase)
350 return wxItemContainerImmutable::FindString( s, bCase );
351
352 int pos = ListBox_FindStringExact(GetHwnd(), -1, s);
353 if (pos == LB_ERR)
354 return wxNOT_FOUND;
355 else
356 return pos;
357 }
358
359 void wxListBox::Clear()
360 {
361 Free();
362
363 ListBox_ResetContent(GetHwnd());
364
365 m_noItems = 0;
366 SetHorizontalExtent();
367 }
368
369 void wxListBox::Free()
370 {
371 #if wxUSE_OWNER_DRAWN
372 if ( m_windowStyle & wxLB_OWNERDRAW )
373 {
374 WX_CLEAR_ARRAY(m_aItems);
375 }
376 else
377 #endif // wxUSE_OWNER_DRAWN
378 if ( HasClientObjectData() )
379 {
380 for ( unsigned int n = 0; n < m_noItems; n++ )
381 {
382 delete GetClientObject(n);
383 }
384 }
385 }
386
387 void wxListBox::DoSetSelection(int N, bool select)
388 {
389 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
390 wxT("invalid index in wxListBox::SetSelection") );
391
392 if ( HasMultipleSelection() )
393 {
394 SendMessage(GetHwnd(), LB_SETSEL, select, N);
395 }
396 else
397 {
398 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
399 }
400 }
401
402 bool wxListBox::IsSelected(int N) const
403 {
404 wxCHECK_MSG( IsValid(N), false,
405 wxT("invalid index in wxListBox::Selected") );
406
407 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
408 }
409
410 wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
411 {
412 return (wxClientData *)DoGetItemClientData(n);
413 }
414
415 void *wxListBox::DoGetItemClientData(unsigned int n) const
416 {
417 wxCHECK_MSG( IsValid(n), NULL,
418 wxT("invalid index in wxListBox::GetClientData") );
419
420 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
421 }
422
423 void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
424 {
425 DoSetItemClientData(n, clientData);
426 }
427
428 void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
429 {
430 wxCHECK_RET( IsValid(n),
431 wxT("invalid index in wxListBox::SetClientData") );
432
433 #if wxUSE_OWNER_DRAWN
434 if ( m_windowStyle & wxLB_OWNERDRAW )
435 {
436 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
437 // in OnMeasure/OnDraw.
438 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
439 }
440 #endif // wxUSE_OWNER_DRAWN
441
442 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
443 wxLogDebug(wxT("LB_SETITEMDATA failed"));
444 }
445
446 // Return number of selections and an array of selected integers
447 int wxListBox::GetSelections(wxArrayInt& aSelections) const
448 {
449 aSelections.Empty();
450
451 if ( HasMultipleSelection() )
452 {
453 int countSel = ListBox_GetSelCount(GetHwnd());
454 if ( countSel == LB_ERR )
455 {
456 wxLogDebug(_T("ListBox_GetSelCount failed"));
457 }
458 else if ( countSel != 0 )
459 {
460 int *selections = new int[countSel];
461
462 if ( ListBox_GetSelItems(GetHwnd(),
463 countSel, selections) == LB_ERR )
464 {
465 wxLogDebug(wxT("ListBox_GetSelItems failed"));
466 countSel = -1;
467 }
468 else
469 {
470 aSelections.Alloc(countSel);
471 for ( int n = 0; n < countSel; n++ )
472 aSelections.Add(selections[n]);
473 }
474
475 delete [] selections;
476 }
477
478 return countSel;
479 }
480 else // single-selection listbox
481 {
482 if (ListBox_GetCurSel(GetHwnd()) > -1)
483 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
484
485 return aSelections.Count();
486 }
487 }
488
489 // Get single selection, for single choice list items
490 int wxListBox::GetSelection() const
491 {
492 wxCHECK_MSG( !HasMultipleSelection(),
493 -1,
494 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
495
496 return ListBox_GetCurSel(GetHwnd());
497 }
498
499 // Find string for position
500 wxString wxListBox::GetString(unsigned int n) const
501 {
502 wxCHECK_MSG( IsValid(n), wxEmptyString,
503 wxT("invalid index in wxListBox::GetString") );
504
505 int len = ListBox_GetTextLen(GetHwnd(), n);
506
507 // +1 for terminating NUL
508 wxString result;
509 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
510
511 return result;
512 }
513
514 void
515 wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
516 {
517 wxCHECK_RET( IsValidInsert(pos),
518 wxT("invalid index in wxListBox::InsertItems") );
519
520 unsigned int nItems = items.GetCount();
521 for ( unsigned int i = 0; i < nItems; i++ )
522 {
523 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
524
525 #if wxUSE_OWNER_DRAWN
526 if ( m_windowStyle & wxLB_OWNERDRAW )
527 {
528 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
529 pNewItem->SetName(items[i]);
530 pNewItem->SetFont(GetFont());
531 m_aItems.Insert(pNewItem, idx);
532
533 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
534 }
535 #else
536 wxUnusedVar(idx);
537 #endif // wxUSE_OWNER_DRAWN
538 }
539
540 m_noItems += nItems;
541
542 SetHorizontalExtent();
543 }
544
545 int wxListBox::DoListHitTest(const wxPoint& point) const
546 {
547 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
548 0, MAKELPARAM(point.x, point.y));
549
550 // non zero high-order word means that this item is outside of the client
551 // area, IOW the point is outside of the listbox
552 return HIWORD(lRes) ? wxNOT_FOUND : LOWORD(lRes);
553 }
554
555 void wxListBox::SetString(unsigned int n, const wxString& s)
556 {
557 wxCHECK_RET( IsValid(n),
558 wxT("invalid index in wxListBox::SetString") );
559
560 // remember the state of the item
561 bool wasSelected = IsSelected(n);
562
563 void *oldData = NULL;
564 wxClientData *oldObjData = NULL;
565 if ( m_clientDataItemsType == wxClientData_Void )
566 oldData = GetClientData(n);
567 else if ( m_clientDataItemsType == wxClientData_Object )
568 oldObjData = GetClientObject(n);
569
570 // delete and recreate it
571 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
572
573 int newN = n;
574 if ( n == (m_noItems - 1) )
575 newN = -1;
576
577 ListBox_InsertString(GetHwnd(), newN, s);
578
579 // restore the client data
580 if ( oldData )
581 SetClientData(n, oldData);
582 else if ( oldObjData )
583 SetClientObject(n, oldObjData);
584
585 #if wxUSE_OWNER_DRAWN
586 if ( m_windowStyle & wxLB_OWNERDRAW )
587 {
588 // update item's text
589 m_aItems[n]->SetName(s);
590
591 // reassign the item's data
592 ListBox_SetItemData(GetHwnd(), n, m_aItems[n]);
593 }
594 #endif //USE_OWNER_DRAWN
595
596 // we may have lost the selection
597 if ( wasSelected )
598 Select(n);
599
600 SetHorizontalExtent();
601 }
602
603 unsigned int wxListBox::GetCount() const
604 {
605 return m_noItems;
606 }
607
608 // ----------------------------------------------------------------------------
609 // size-related stuff
610 // ----------------------------------------------------------------------------
611
612 void wxListBox::SetHorizontalExtent(const wxString& s)
613 {
614 // in any case, our best size could have changed
615 InvalidateBestSize();
616
617 // the rest is only necessary if we want a horizontal scrollbar
618 if ( !HasFlag(wxHSCROLL) )
619 return;
620
621
622 WindowHDC dc(GetHwnd());
623 SelectInHDC selFont(dc, GetHfontOf(GetFont()));
624
625 TEXTMETRIC lpTextMetric;
626 ::GetTextMetrics(dc, &lpTextMetric);
627
628 int largestExtent = 0;
629 SIZE extentXY;
630
631 if ( s.empty() )
632 {
633 // set extent to the max length of all strings
634 for ( unsigned int i = 0; i < m_noItems; i++ )
635 {
636 const wxString str = GetString(i);
637 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
638
639 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
640 if ( extentX > largestExtent )
641 largestExtent = extentX;
642 }
643 }
644 else // just increase the extent to the length of this string
645 {
646 int existingExtent = (int)SendMessage(GetHwnd(),
647 LB_GETHORIZONTALEXTENT, 0, 0L);
648
649 ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY);
650
651 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
652 if ( extentX > existingExtent )
653 largestExtent = extentX;
654 }
655
656 if ( largestExtent )
657 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
658 //else: it shouldn't change
659 }
660
661 wxSize wxListBox::DoGetBestSize() const
662 {
663 // find the widest string
664 int wLine;
665 int wListbox = 0;
666 for (unsigned int i = 0; i < m_noItems; i++)
667 {
668 wxString str(GetString(i));
669 GetTextExtent(str, &wLine, NULL);
670 if ( wLine > wListbox )
671 wListbox = wLine;
672 }
673
674 // give it some reasonable default value if there are no strings in the
675 // list
676 if ( wListbox == 0 )
677 wListbox = 100;
678
679 // the listbox should be slightly larger than the widest string
680 int cx, cy;
681 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
682
683 wListbox += 3*cx;
684
685 // Add room for the scrollbar
686 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
687
688 // don't make the listbox too tall (limit height to 10 items) but don't
689 // make it too small neither
690 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
691 wxMin(wxMax(m_noItems, 3), 10);
692
693 wxSize best(wListbox, hListbox);
694 CacheBestSize(best);
695 return best;
696 }
697
698 // ----------------------------------------------------------------------------
699 // callbacks
700 // ----------------------------------------------------------------------------
701
702 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
703 {
704 wxEventType evtType;
705 int n;
706 if ( param == LBN_SELCHANGE )
707 {
708 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
709 n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
710
711 // NB: conveniently enough, LB_ERR is the same as wxNOT_FOUND
712 }
713 else if ( param == LBN_DBLCLK )
714 {
715 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
716 n = HitTest(ScreenToClient(wxGetMousePosition()));
717 }
718 else
719 {
720 // some event we're not interested in
721 return false;
722 }
723
724 // retrieve the affected item
725 if ( n == wxNOT_FOUND )
726 return false;
727
728 wxCommandEvent event(evtType, m_windowId);
729 event.SetEventObject(this);
730
731 if ( HasClientObjectData() )
732 event.SetClientObject( GetClientObject(n) );
733 else if ( HasClientUntypedData() )
734 event.SetClientData( GetClientData(n) );
735
736 event.SetString(GetString(n));
737 event.SetInt(n);
738 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true );
739
740 return GetEventHandler()->ProcessEvent(event);
741 }
742
743 // ----------------------------------------------------------------------------
744 // wxCheckListBox support
745 // ----------------------------------------------------------------------------
746
747 #if wxUSE_OWNER_DRAWN
748
749 // drawing
750 // -------
751
752 // space beneath/above each row in pixels
753 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
754 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
755
756 // the height is the same for all items
757 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
758
759 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
760 // message is not yet sent when we get here!
761 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
762 {
763 // only owner-drawn control should receive this message
764 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
765
766 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
767
768 #ifdef __WXWINCE__
769 HDC hdc = GetDC(NULL);
770 #else
771 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
772 #endif
773
774 {
775 wxDCTemp dc((WXHDC)hdc);
776 dc.SetFont(GetFont());
777
778 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
779 pStruct->itemWidth = dc.GetCharWidth();
780 }
781
782 #ifdef __WXWINCE__
783 ReleaseDC(NULL, hdc);
784 #else
785 DeleteDC(hdc);
786 #endif
787
788 return true;
789 }
790
791 // forward the message to the appropriate item
792 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
793 {
794 // only owner-drawn control should receive this message
795 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
796
797 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
798 UINT itemID = pStruct->itemID;
799
800 // the item may be -1 for an empty listbox
801 if ( itemID == (UINT)-1 )
802 return false;
803
804 LRESULT data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
805
806 wxCHECK( data && (data != LB_ERR), false );
807
808 wxListBoxItem *pItem = (wxListBoxItem *)data;
809
810 wxDCTemp dc((WXHDC)pStruct->hDC);
811 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
812 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
813 wxRect rect(pt1, pt2);
814
815 return pItem->OnDrawItem(dc, rect,
816 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
817 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
818 }
819
820 #endif // wxUSE_OWNER_DRAWN
821
822 #endif // wxUSE_LISTBOX

  ViewVC Help
Powered by ViewVC 1.1.22