home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 19.ddi / OWLINC.PAK / LISTBOX.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  13.7 KB  |  427 lines

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