home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWLINC.PAK / LISTBOX.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  14KB  |  436 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Definition of class TListBox and TlistBoxData.  This defines the
  6. //   basic behavior of all list box controls.
  7. //----------------------------------------------------------------------------
  8. #if !defined(OWL_LISTBOX_H)
  9. #define OWL_LISTBOX_H
  10.  
  11. #if !defined(OWL_CONTROL_H)
  12. # include <owl/control.h>
  13. #endif
  14. #if !defined(CLASSLIB_ARRAYS_H)
  15. # include <classlib/arrays.h>
  16. #endif
  17.  
  18. //
  19. //  class TListBox
  20. //  ----- --------
  21. //
  22. class _OWLCLASS TListBox : public TControl {
  23.   public:
  24.     TListBox(TWindow*        parent,
  25.              int             id,
  26.              int x, int y, int w, int h,
  27.              TModule*        module = 0);
  28.  
  29.     TListBox(TWindow* parent, int resourceId, TModule*   module = 0);
  30.  
  31.     //
  32.     // list box attributes
  33.     //
  34.     virtual int   GetCount() const;
  35.     virtual int   FindString(const char far* str, int index) const;
  36.     int           FindExactString(const char far* str, int searchIndex) const;
  37.     int           GetTopIndex() const;
  38.     int           SetTopIndex(int index);
  39.     bool          SetTabStops(int numTabs, int far* tabs);
  40.  
  41.     int           GetHorizontalExtent() const;
  42.     void          SetHorizontalExtent(int horzExtent);
  43.     void          SetColumnWidth(int width);
  44.     int           GetCaretIndex() const;
  45.     int           SetCaretIndex(int index, bool partScrollOk);
  46.  
  47.     //
  48.     // query individual list items
  49.     //
  50.     virtual int   GetStringLen(int index) const;
  51.     virtual int   GetString(char far* str, int index) const;
  52.     virtual uint32 GetItemData(int index) const;
  53.     virtual int   SetItemData(int index, uint32 itemData);
  54.  
  55.     int           GetItemRect(int index, TRect& rect) const;
  56.     virtual int   GetItemHeight(int index) const;
  57.     virtual int   SetItemHeight(int index, int height);
  58.  
  59.     //
  60.     // operations on the list box itself
  61.     //
  62.     virtual void  ClearList();
  63.     virtual int   DirectoryList(uint attrs, const char far* fileSpec);
  64.  
  65.     //
  66.     // operations on individual list box items
  67.     //
  68.     virtual int   AddString(const char far* str);
  69.     virtual int   InsertString(const char far* str, int index);
  70.     virtual int   DeleteString(int index);
  71.  
  72.     //
  73.     // single selection list boxes only (combos overload these)
  74.     //
  75.     virtual int   GetSelIndex() const;
  76.     virtual int   SetSelIndex(int index);
  77.             int   GetSelString(char far* str, int maxChars) const;
  78.             int   SetSelString(const char far* str, int searchIndex);
  79.  
  80.     //
  81.     // multiple selection list boxes only
  82.     //
  83.     int           GetSelCount() const;
  84.     int           GetSelStrings(char far** strs, int maxCount,
  85.                                 int maxChars) const;
  86.     int           SetSelStrings(const char far** prefixes, int numSelections,
  87.                                 bool shouldSet);
  88.     int           GetSelIndexes(int* indexes, int maxCount) const;
  89.     int           SetSelIndexes(int* indexes, int numSelections, bool shouldSet);
  90.     bool          GetSel(int index) const;
  91.     int           SetSel(int index, bool select);
  92.     int           SetSelItemRange(bool select, int first, int last);
  93.  
  94.     //
  95.     // Override TWindow virtual member functions
  96.     //
  97.     uint          Transfer(void *buffer, TTransferDirection direction);
  98.  
  99.   protected:
  100.     //
  101.     // Override TWindow virtual member functions
  102.     //
  103.     char far     *GetClassName();
  104.  
  105.   private:
  106.     //
  107.     // hidden to prevent accidental copying or assignment
  108.     //
  109.     TListBox(const TListBox&);
  110.     TListBox& operator =(const TListBox&);
  111.  
  112.   DECLARE_STREAMABLE(_OWLCLASS, TListBox, 1);
  113. };
  114.  
  115. //
  116. // list box notification macros. methods are: void method()
  117. //
  118. // EV_LBN_DBLCLK(id, method)
  119. // EV_LBN_ERRSPACE(id, method)
  120. // EV_LBN_KILLFOCUS(id, method)
  121. // EV_LBN_SELCANCEL(id, method)
  122. // EV_LBN_SELCHANGE(id, method)
  123. // EV_LBN_SETFOCUS(id, method)
  124.  
  125. //
  126. //  Container classes
  127. //  --------- -------
  128. //
  129.  
  130. class _OWLCLASS TStringArray {
  131.   public:
  132.     typedef void (*IterFunc)(string&, void*);
  133.     typedef int  (*CondFunc)(const string&, void*);
  134.  
  135.     TStringArray(int upper, int lower, int delta);
  136.     int LowerBound() const;
  137.     int UpperBound() const;
  138.     unsigned ArraySize() const;
  139.     int IsFull() const;
  140.     int IsEmpty() const;
  141.     unsigned GetItemsInContainer() const;
  142.     int Add(const string& t);
  143.     int Detach(const string& t);
  144.     int Detach(int loc);
  145.     int Destroy(const string& t);
  146.     int Destroy(int loc);
  147.     int HasMember(const string& t) const;
  148.     int Find(const string& t) const;
  149.     string& operator [](int loc);
  150.     string& operator [](int loc) const;
  151.     void ForEach(IterFunc iter, void* args);
  152.     string* FirstThat(CondFunc cond, void* args) const;
  153.     string* LastThat(CondFunc cond, void* args) const;
  154.     void Flush();
  155.  
  156.   private:
  157.     TArrayAsVector<string> Data;
  158.  
  159.   friend class TStringArrayIterator;
  160. };
  161.  
  162. class TStringArrayIterator : public TArrayAsVectorIterator<string> {
  163.   public:
  164.     TStringArrayIterator(const TStringArray& array)
  165.         : TArrayAsVectorIterator<string>(array.Data) {}
  166. };
  167.  
  168. struct TInt {
  169.   int i;
  170.   TInt() {}
  171.   TInt(int _i) {i = _i;}
  172.   operator int() const {return i;}
  173. };
  174.  
  175. class _OWLCLASS TIntArray {
  176.   public:
  177.     typedef void (*IterFunc)(TInt&, void*);
  178.     typedef int  (*CondFunc)(const TInt&, void*);
  179.  
  180.     TIntArray(int upper, int lower, int delta);
  181.     int LowerBound() const;
  182.     int UpperBound() const;
  183.     unsigned ArraySize() const;
  184.     int IsFull() const;
  185.     int IsEmpty() const;
  186.     unsigned GetItemsInContainer() const;
  187.     int Add(const TInt& t);
  188.     int Detach(const TInt& t);
  189.     int Detach(int loc);
  190.     int Destroy(const TInt& t);
  191.     int Destroy(int loc);
  192.     int HasMember(const TInt& t) const;
  193.     int Find(const TInt& t) const;
  194.     TInt& operator [](int loc);
  195.     TInt& operator [](int loc) const;
  196.     void ForEach(IterFunc iter, void* args);
  197.     TInt* FirstThat(CondFunc cond, void* args) const;
  198.     TInt* LastThat(CondFunc cond, void* args) const;
  199.     void Flush();
  200.  
  201.   private:
  202.     TArrayAsVector<TInt> Data;
  203.  
  204.   friend class TIntArrayIterator;
  205. };
  206.  
  207. class TIntArrayIterator : public TArrayAsVectorIterator<TInt> {
  208.   public:
  209.     TIntArrayIterator(const TIntArray& array)
  210.         : TArrayAsVectorIterator<TInt>(array.Data) {}
  211. };
  212.  
  213. class _OWLCLASS TDwordArray {
  214.   public:
  215.     typedef void (*IterFunc)(uint32&, void*);
  216.     typedef int  (*CondFunc)(const uint32&, void*);
  217.  
  218.     TDwordArray(int upper, int lower, int delta);
  219.     int LowerBound() const;
  220.     int UpperBound() const;
  221.     unsigned ArraySize() const;
  222.     int IsFull() const;
  223.     int IsEmpty() const;
  224.     unsigned GetItemsInContainer() const;
  225.     int Add(const uint32& t);
  226.     int Detach(const uint32& t);
  227.     int Detach(int loc);
  228.     int Destroy(const uint32& t);
  229.     int Destroy(int loc);
  230.     int HasMember(const uint32& t) const;
  231.     int Find(const uint32& t) const;
  232.     uint32& operator [](int loc);
  233.     uint32& operator [](int loc) const;
  234.     void ForEach(IterFunc iter, void* args);
  235.     uint32* FirstThat(CondFunc cond, void* args) const;
  236.     uint32* LastThat(CondFunc cond, void* args) const;
  237.     void Flush();
  238.  
  239.   private:
  240.     TArrayAsVector<uint32> Data;
  241.  
  242.   friend class TDwordArrayIterator;
  243. };
  244.  
  245. class TDwordArrayIterator : public TArrayAsVectorIterator<uint32> {
  246.   public:
  247.     TDwordArrayIterator(const TDwordArray& array)
  248.         : TArrayAsVectorIterator<uint32>(array.Data) {}
  249. };
  250.  
  251. //
  252. //  class TListBoxData
  253. //  ----- ------------
  254. //
  255. class _OWLCLASS TListBoxData {
  256.   public:
  257.     TListBoxData();
  258.    ~TListBoxData();
  259.  
  260.     TStringArray&   GetStrings() {return Strings;}
  261.     TDwordArray&    GetItemDatas() {return ItemDatas;}
  262.     TIntArray&      GetSelIndices() {return SelIndices;}
  263.  
  264.     void    AddString(const char* str, bool isSelected = false);
  265.     void    AddStringItem(const char* str, uint32 itemData, bool isSelected = false);
  266.     void    Clear() {Strings.Flush(); ItemDatas.Flush(); ResetSelections();}
  267.  
  268.     void    Select(int index);
  269.     void    SelectString(const char far* str);
  270.     int     GetSelCount() const {return SelIndices.GetItemsInContainer();}
  271.     void    ResetSelections() {SelIndices.Flush();}
  272.     int     GetSelStringLength(int index = 0) const;
  273.     void    GetSelString(char far* buffer, int bufferSize, int index=0) const;
  274.     void    GetSelString(string& str, int index=0) const;
  275.  
  276.   protected:
  277.     TStringArray  Strings;     // Contains all strings in listbox
  278.     TDwordArray   ItemDatas;   // Contains all item data uint32 in listbox
  279.     TIntArray     SelIndices;  // Contains all selection indices
  280. };
  281.  
  282.  
  283. //----------------------------------------------------------------------------
  284. // Inlines
  285. //----------------------------------------------------------------------------
  286.  
  287. inline int TListBox::GetTopIndex() const {
  288.   return (int)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETTOPINDEX);
  289. }
  290.  
  291. inline int TListBox::SetTopIndex(int index) {
  292.   return (int)HandleMessage(LB_SETTOPINDEX, index);
  293. }
  294.  
  295. inline bool TListBox::SetTabStops(int numTabs, int far* tabs) {
  296.   return (bool)HandleMessage(LB_SETTABSTOPS, numTabs, (LPARAM)tabs);
  297. }
  298.  
  299. inline int TListBox::GetHorizontalExtent() const {
  300.   return (int)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETHORIZONTALEXTENT);
  301. }
  302.  
  303. inline void TListBox::SetHorizontalExtent(int horzExtent) {
  304.   HandleMessage(LB_SETHORIZONTALEXTENT, horzExtent);
  305. }
  306.  
  307. inline void TListBox::SetColumnWidth(int width) {
  308.   HandleMessage(LB_SETCOLUMNWIDTH, width);
  309. }
  310.  
  311. inline int TListBox::GetCaretIndex() const {
  312.   return (int)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETCARETINDEX);
  313. }
  314.  
  315. inline int TListBox::SetCaretIndex(int index, bool partScrollOk) {
  316.   return (int)HandleMessage(LB_SETCARETINDEX, index, MAKELPARAM(partScrollOk,0));
  317. }
  318.  
  319. inline uint32 TListBox::GetItemData(int index) const {
  320.   return CONST_CAST(TListBox*,this)->HandleMessage(LB_GETITEMDATA, index);
  321. }
  322.  
  323. inline int TListBox::SetItemData(int index, uint32 itemData) {
  324.   return (int)HandleMessage(LB_SETITEMDATA, index, itemData);
  325. }
  326.  
  327. inline int TListBox::GetItemRect(int index, TRect& rect) const {
  328.   return (int)CONST_CAST(TListBox*,this)->
  329.            HandleMessage(LB_GETITEMRECT, index, (LPARAM)(TRect FAR*)&rect);
  330. }
  331.  
  332. inline int TListBox::GetItemHeight(int index) const {
  333.   return (int)CONST_CAST(TListBox*,this)->
  334.            HandleMessage(LB_GETITEMHEIGHT, index);
  335. }
  336.  
  337. inline int TListBox::SetItemHeight(int index, int height) {
  338.   return (int)HandleMessage(LB_SETITEMHEIGHT, index, MAKELPARAM(height, 0));
  339. }
  340.  
  341. inline int TListBox::DirectoryList(uint attrs, const char far* fileSpec) {
  342.   return (int)HandleMessage(LB_DIR, attrs, (LPARAM)fileSpec);
  343. }
  344.  
  345. // Adds a string to an associated listbox
  346. // Returns index of the string in the list(the first entry is at index 0),
  347. // a negative if an error occurs.
  348. //
  349. inline int TListBox::AddString(const char far* str) {
  350.   return (int)HandleMessage(LB_ADDSTRING, 0, (LPARAM)str);
  351. }
  352.  
  353. // Inserts a string in the associated listbox at the passed index,
  354. // returns the index of the string in the list, a negative if an error occurs
  355. //
  356. inline int TListBox::InsertString(const char far* str, int index) {
  357.   return (int)HandleMessage(LB_INSERTSTRING, index, (LPARAM)str);
  358. }
  359.  
  360. // Deletes the string at the passed index in the associated listbox
  361. // Returns a count of the entries remaining in the list, a negative
  362. // value if an error occurs
  363. //
  364. inline int TListBox::DeleteString(int index) {
  365.   return (int)HandleMessage(LB_DELETESTRING, index);
  366. }
  367.  
  368. // Clears all the entries in the associated listbox
  369. //
  370. inline void TListBox::ClearList() {
  371.   HandleMessage(LB_RESETCONTENT);
  372. }
  373.  
  374. // Returns the number of entries in the associated listbox, a negative
  375. // value if an error occurs
  376. //
  377. inline int TListBox::GetCount() const {
  378.   return (int)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETCOUNT);
  379. }
  380.  
  381. // Returns the index of the first string in the associated listbox which
  382. // begins with the passed string
  383. //
  384. // Searches for a match beginning at the passed SearchIndex. If a match is
  385. // not found after the last string has been compared, the search continues
  386. // from the beginning of the list until a match is found or until the list
  387. // has been completely traversed
  388. //
  389. // Searches from beginning of list when -1 is passed as the index
  390. //
  391. // Returns the index of the selected string, a negative if an error occurs
  392. //
  393. inline int TListBox::FindString(const char far* find, int indexStart) const {
  394.   return (int)CONST_CAST(TListBox*,this)->
  395.            HandleMessage(LB_FINDSTRING, indexStart, (LPARAM)find);
  396. }
  397.  
  398. // Retrieves the contents of the string at the passed index of the
  399. // associated listbox. Returns the length of the string (in bytes
  400. // excluding the terminating 0),  a negative if the passed index is not valid
  401. //
  402. // The buffer must be large enough for the string and the terminating 0
  403. //
  404. inline int TListBox::GetString(char far* str, int index) const {
  405.   return (int)CONST_CAST(TListBox*,this)->
  406.            HandleMessage(LB_GETTEXT, index, (LPARAM)str);
  407. }
  408.  
  409. // Returns the length of the string at the passed index in the associated
  410. // listbox excluding the terminating 0, a negative if an error occurs
  411. //
  412. inline int TListBox::GetStringLen(int index) const {
  413.   return (int)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETTEXTLEN, index);
  414. }
  415.  
  416. //
  417. // Get the selection state (true/false) of a particular item at 'index'
  418. //
  419. inline bool TListBox::GetSel(int index) const {
  420.   return (bool)CONST_CAST(TListBox*,this)->HandleMessage(LB_GETSEL, index);
  421. }
  422.  
  423. //
  424. // Set the selection state (true/false) of a particular item at 'index',
  425. // or all items if index is -1
  426. //
  427. inline int TListBox::SetSel(int index, bool select) {
  428.   return (int)HandleMessage(LB_SETSEL, select, MAKELPARAM(index, 0));
  429. }
  430.  
  431. inline int TListBox::SetSelItemRange(bool select, int first, int last) {
  432.   return (int)HandleMessage(LB_SELITEMRANGE, select, MAKELPARAM(first, last));
  433. }
  434.  
  435. #endif  // OWL_LISTBOX_H
  436.