listbox.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/listbox.h
  3. // Purpose: wxListBox class interface
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 22.10.99
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_LISTBOX_H_BASE_
  11. #define _WX_LISTBOX_H_BASE_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #if wxUSE_LISTBOX
  17. #include "wx/ctrlsub.h" // base class
  18. // forward declarations are enough here
  19. class WXDLLIMPEXP_FWD_BASE wxArrayInt;
  20. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  21. // ----------------------------------------------------------------------------
  22. // global data
  23. // ----------------------------------------------------------------------------
  24. extern WXDLLIMPEXP_DATA_CORE(const char) wxListBoxNameStr[];
  25. // ----------------------------------------------------------------------------
  26. // wxListBox interface is defined by the class wxListBoxBase
  27. // ----------------------------------------------------------------------------
  28. class WXDLLIMPEXP_CORE wxListBoxBase : public wxControlWithItems
  29. {
  30. public:
  31. wxListBoxBase() { }
  32. virtual ~wxListBoxBase();
  33. void InsertItems(unsigned int nItems, const wxString *items, unsigned int pos)
  34. { Insert(nItems, items, pos); }
  35. void InsertItems(const wxArrayString& items, unsigned int pos)
  36. { Insert(items, pos); }
  37. // multiple selection logic
  38. virtual bool IsSelected(int n) const = 0;
  39. virtual void SetSelection(int n);
  40. void SetSelection(int n, bool select) { DoSetSelection(n, select); }
  41. void Deselect(int n) { DoSetSelection(n, false); }
  42. void DeselectAll(int itemToLeaveSelected = -1);
  43. virtual bool SetStringSelection(const wxString& s, bool select);
  44. virtual bool SetStringSelection(const wxString& s)
  45. {
  46. return SetStringSelection(s, true);
  47. }
  48. // works for single as well as multiple selection listboxes (unlike
  49. // GetSelection which only works for listboxes with single selection)
  50. virtual int GetSelections(wxArrayInt& aSelections) const = 0;
  51. // set the specified item at the first visible item or scroll to max
  52. // range.
  53. void SetFirstItem(int n) { DoSetFirstItem(n); }
  54. void SetFirstItem(const wxString& s);
  55. // ensures that the given item is visible scrolling the listbox if
  56. // necessary
  57. virtual void EnsureVisible(int n);
  58. // a combination of Append() and EnsureVisible(): appends the item to the
  59. // listbox and ensures that it is visible i.e. not scrolled out of view
  60. void AppendAndEnsureVisible(const wxString& s);
  61. // return true if the listbox allows multiple selection
  62. bool HasMultipleSelection() const
  63. {
  64. return (m_windowStyle & wxLB_MULTIPLE) ||
  65. (m_windowStyle & wxLB_EXTENDED);
  66. }
  67. // override wxItemContainer::IsSorted
  68. virtual bool IsSorted() const { return HasFlag( wxLB_SORT ); }
  69. // emulate selecting or deselecting the item event.GetInt() (depending on
  70. // event.GetExtraLong())
  71. void Command(wxCommandEvent& event);
  72. // return the index of the item at this position or wxNOT_FOUND
  73. int HitTest(const wxPoint& point) const { return DoListHitTest(point); }
  74. int HitTest(int x, int y) const { return DoListHitTest(wxPoint(x, y)); }
  75. protected:
  76. virtual void DoSetFirstItem(int n) = 0;
  77. virtual void DoSetSelection(int n, bool select) = 0;
  78. // there is already wxWindow::DoHitTest() so call this one differently
  79. virtual int DoListHitTest(const wxPoint& WXUNUSED(point)) const
  80. { return wxNOT_FOUND; }
  81. // Helper for the code generating events in single selection mode: updates
  82. // m_oldSelections and return true if the selection really changed.
  83. // Otherwise just returns false.
  84. bool DoChangeSingleSelection(int item);
  85. // Helper for generating events in multiple and extended mode: compare the
  86. // current selections with the previously recorded ones (in
  87. // m_oldSelections) and send the appropriate event if they differ,
  88. // otherwise just return false.
  89. bool CalcAndSendEvent();
  90. // Send a listbox (de)selection or double click event.
  91. //
  92. // Returns true if the event was processed.
  93. bool SendEvent(wxEventType evtType, int item, bool selected);
  94. // Array storing the indices of all selected items that we already notified
  95. // the user code about for multi selection list boxes.
  96. //
  97. // For single selection list boxes, we reuse this array to store the single
  98. // currently selected item, this is used by DoChangeSingleSelection().
  99. //
  100. // TODO-OPT: wxSelectionStore would be more efficient for big list boxes.
  101. wxArrayInt m_oldSelections;
  102. // Update m_oldSelections with currently selected items (does nothing in
  103. // single selection mode on platforms other than MSW).
  104. void UpdateOldSelections();
  105. private:
  106. wxDECLARE_NO_COPY_CLASS(wxListBoxBase);
  107. };
  108. // ----------------------------------------------------------------------------
  109. // include the platform-specific class declaration
  110. // ----------------------------------------------------------------------------
  111. #if defined(__WXUNIVERSAL__)
  112. #include "wx/univ/listbox.h"
  113. #elif defined(__WXMSW__)
  114. #include "wx/msw/listbox.h"
  115. #elif defined(__WXMOTIF__)
  116. #include "wx/motif/listbox.h"
  117. #elif defined(__WXGTK20__)
  118. #include "wx/gtk/listbox.h"
  119. #elif defined(__WXGTK__)
  120. #include "wx/gtk1/listbox.h"
  121. #elif defined(__WXMAC__)
  122. #include "wx/osx/listbox.h"
  123. #elif defined(__WXPM__)
  124. #include "wx/os2/listbox.h"
  125. #elif defined(__WXCOCOA__)
  126. #include "wx/cocoa/listbox.h"
  127. #endif
  128. #endif // wxUSE_LISTBOX
  129. #endif
  130. // _WX_LISTBOX_H_BASE_