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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Defines class TComboBox and TComboBoxData.  This defines the basic
  6. //   behavior of all combo box controls.
  7. //----------------------------------------------------------------------------
  8. #if !defined(OWL_COMBOBOX_H)
  9. #define OWL_COMBOBOX_H
  10.  
  11. #if !defined(OWL_LISTBOX_H)
  12. # include <owl/listbox.h>
  13. #endif
  14.  
  15. //
  16. //  class TComboBox
  17. //  ----- ---------
  18. //
  19. class _OWLCLASS TComboBox : public TListBox {
  20.   public:
  21.     uint  TextLen;
  22.  
  23.     TComboBox(TWindow*        parent,
  24.               int             id,
  25.               int x, int y, int w, int h,
  26.               uint32          style,
  27.               uint            textLen,
  28.               TModule*        module = 0);
  29.  
  30.     TComboBox(TWindow*   parent,
  31.               int        resourceId,
  32.               uint       textLen = 0,
  33.               TModule*   module = 0);
  34.  
  35.     //
  36.     // for combo box's edit control
  37.     //
  38.     int           GetTextLen() const {return GetWindowTextLength();}
  39.     int           GetText(char far* str, int maxChars) const;  // num of chars copied
  40.     void          SetText(const char far* str);
  41.  
  42.     int           GetEditSel(int& startPos, int& endPos);
  43.     int           SetEditSel(int startPos, int endPos);  //CB_ERR if no edit control
  44.  
  45.     void          Clear();  // clear the text
  46.  
  47.     //
  48.     // for drop down combo boxes
  49.     //
  50.     void          ShowList(bool show);
  51.     void          ShowList() {ShowList(true);}
  52.     void          HideList() {ShowList(false);}
  53.  
  54.     void          GetDroppedControlRect(TRect& Rect) const;
  55.     bool          GetDroppedState() const;
  56.     bool          GetExtendedUI() const;
  57.     int           SetExtendedUI(bool Extended);
  58.  
  59.     //
  60.     // Combo's List box virtual functions
  61.     //
  62.     virtual int   AddString(const char far* str);
  63.     virtual int   InsertString(const char far* str, int index);
  64.     virtual int   DeleteString(int index);
  65.  
  66.     virtual void  ClearList();
  67.     virtual int   DirectoryList(uint attrs, const char far* fileSpec);
  68.  
  69.     virtual int   GetCount() const;
  70.     virtual int   FindString(const char far* find, int indexStart) const;
  71.  
  72.     virtual int   GetStringLen(int index) const;
  73.     virtual int   GetString(char far* str, int index) const;
  74.  
  75.     virtual int   GetSelIndex() const;
  76.     virtual int   SetSelIndex(int index);
  77.     virtual int   SetSelString(const char far* findStr, int indexStart);
  78.     virtual uint32 GetItemData(int index) const;
  79.     virtual int   SetItemData(int index, uint32 itemData);
  80.  
  81.     int           GetItemHeight(int index) const;
  82.     int           SetItemHeight(int index, int height);
  83.  
  84.     //
  85.     // Override TWindow virtual member functions
  86.     //
  87.     uint          Transfer(void* buffer, TTransferDirection direction);
  88.  
  89.   protected:
  90.     //
  91.     // Override TWindow virtual member functions
  92.     //
  93.     char far*     GetClassName();
  94.     void          SetupWindow();
  95.  
  96.     //
  97.     // message response functions
  98.     //
  99.  
  100.   private:
  101.     //
  102.     // hidden to prevent accidental copying or assignment
  103.     //
  104.     TComboBox(const TComboBox&);
  105.     TComboBox& operator =(const TComboBox&);
  106.  
  107.   DECLARE_STREAMABLE(_OWLCLASS, TComboBox, 1);
  108. };
  109.  
  110. //
  111. // combo box notification macros. methods are: void method()
  112. //
  113. // EV_CBN_CLOSEUP(id, method)
  114. // EV_CBN_DBLCLK(id, method)
  115. // EV_CBN_DROPDOWN(id, method)
  116. // EV_CBN_EDITCHANGE(id, method)
  117. // EV_CBN_EDITUPDATE(id, method)
  118. // EV_CBN_ERRSPACE(id, method)
  119. // EV_CBN_KILLFOCUS(id, method)
  120. // EV_CBN_SELCHANGE(id, method)
  121. // EV_CBN_SELENDCANCEL(id, method)
  122. // EV_CBN_SELENDOK(id, method)
  123. // EV_CBN_SETFOCUS(id, method)
  124.  
  125. //
  126. //  class TComboBoxData
  127. //  ----- -------------
  128. //
  129. class _OWLCLASS TComboBoxData {
  130.   public:
  131.     TComboBoxData();
  132.    ~TComboBoxData();
  133.  
  134.     TStringArray&   GetStrings() {return Strings;}
  135.     TDwordArray&    GetItemDatas() {return ItemDatas;}
  136.     int             GetSelIndex() {return SelIndex;}
  137.     string&         GetSelection() {return Selection;}
  138.  
  139.     void    AddString(const char* str, bool isSelected = false);
  140.     void    AddStringItem(const char* str, uint32 itemData, bool isSelected = false);
  141.     void    Clear() {Strings.Flush(); ItemDatas.Flush(); ResetSelections();}
  142.  
  143.     void    Select(int index);
  144.     void    SelectString(const char far* str);
  145.     int     GetSelCount() const {return SelIndex == CB_ERR ? 0 : 1;}
  146.     void    ResetSelections() {SelIndex = CB_ERR;  Selection = "";}
  147.     int     GetSelStringLength() const;
  148.     void    GetSelString(char far* buffer, int bufferSize) const;
  149.  
  150.   protected:
  151.     TStringArray   Strings;
  152.     TDwordArray    ItemDatas;
  153.     string         Selection;
  154.     int            SelIndex;
  155. };
  156.  
  157. //----------------------------------------------------------------------------
  158. // Inlines for class TComboBox
  159. //----------------------------------------------------------------------------
  160.  
  161. inline void TComboBox::Clear() {SetText(0);}
  162.  
  163. inline int TComboBox::GetText(char far* str, int maxChars) const {
  164.   return GetWindowText(str, maxChars);
  165. }
  166.  
  167. inline int TComboBox::SetEditSel(int startPos, int endPos) {
  168.   return (int)HandleMessage(CB_SETEDITSEL, 0, MAKELPARAM(startPos, endPos));
  169. }
  170.  
  171. inline void TComboBox::GetDroppedControlRect(TRect& rect) const {
  172.   CONST_CAST(TComboBox*,this)->
  173.     HandleMessage(CB_GETDROPPEDCONTROLRECT, 0, (LPARAM)(TRect FAR*)&rect);
  174. }
  175.  
  176. inline bool TComboBox::GetDroppedState() const {
  177.   return (bool)CONST_CAST(TComboBox*,this)->HandleMessage(CB_GETDROPPEDSTATE);
  178. }
  179.  
  180. inline bool TComboBox::GetExtendedUI() const {
  181.   return (bool)CONST_CAST(TComboBox*,this)->HandleMessage(CB_GETEXTENDEDUI);
  182. }
  183.  
  184. inline int TComboBox::SetExtendedUI(bool extended) {
  185.   return (bool)HandleMessage(CB_SETEXTENDEDUI, extended);
  186. }
  187.  
  188. //
  189. // Adds a string to the list part of the associated combobox
  190. // Returns index of the string in the list(the first entry is at index 0)
  191. // A negative value is returned if an error occurs
  192. //
  193. inline int TComboBox::AddString(const char far* str) {
  194.   return (int)HandleMessage(CB_ADDSTRING, 0, (LPARAM)str);
  195. }
  196.  
  197. //
  198. // Inserts a string in list part of the associated combobox at the passed
  199. // index, returning the index of the string in the list
  200. // A negative value is returned if an error occurs
  201. //
  202. inline int TComboBox::InsertString(const char far* str, int index) {
  203.   return (int)HandleMessage(CB_INSERTSTRING, index, (LPARAM)str);
  204. }
  205.  
  206. //
  207. // Deletes the string at the passed index in the list part of the associated combobox
  208. // Returns a count of the entries remaining in the list; A negative
  209. // value is returned if an error occurs
  210. //
  211. inline int TComboBox::DeleteString(int index) {
  212.   return (int)HandleMessage(CB_DELETESTRING, index);
  213. }
  214.  
  215. inline int TComboBox::DirectoryList(uint attrs, const char far* fileSpec) {
  216.   return (int)HandleMessage(CB_DIR, attrs,(LPARAM)fileSpec);
  217. }
  218.  
  219. //
  220. // Clears all the entries in list part of the associated combobox
  221. //
  222. inline void TComboBox::ClearList() {
  223.   HandleMessage(CB_RESETCONTENT);
  224. }
  225.  
  226. //
  227. // Returns the number of entries in list part of the associated combobox. a negative
  228. // value is returned if an error occurs
  229. //
  230. inline int TComboBox::GetCount() const {
  231.   return (int)CONST_CAST(TComboBox*,this)->HandleMessage(CB_GETCOUNT);
  232. }
  233.  
  234. //
  235. // Returns the index of the first string in list part of the associated combobox which
  236. // begins with the passed string
  237. //
  238. // Searches for a match beginning at the passed SearchIndex. If a match is
  239. // not found after the last string has been compared, the search continues
  240. // from the beginning of the list until a match is found or until the list
  241. // has been completely traversed
  242. //
  243. // Searches from beginning of list when -1 is passed as the index
  244. //
  245. // Returns the index of the selected string.  A negative value is returned
  246. // if an error occurs
  247. //
  248. inline int TComboBox::FindString(const char far* find, int indexStart) const {
  249.   return (int)CONST_CAST(TComboBox*,this)->
  250.            HandleMessage(CB_FINDSTRING, indexStart,(LPARAM)find);
  251. }
  252.  
  253. //
  254. // Retrieves the contents of the string at the passed index of list part of
  255. // the associated combobox, returning the length of the string (in bytes
  256. // excluding the terminating 0) as the value of the call
  257. //
  258. // A negative value is returned if the passed index is not valid
  259. //
  260. // The buffer must be large enough for the string and the terminating
  261. // 0
  262. //
  263. inline int TComboBox::GetString(char far* str, int index) const {
  264.   return (int)CONST_CAST(TComboBox*,this)->
  265.            HandleMessage(CB_GETLBTEXT, index, (LPARAM)str);
  266. }
  267.  
  268. //
  269. // Returns the length of the string at the passed index in the
  270. // associated combo list excluding the terminating 0
  271. //
  272. // A negative value is returned if an error occurs
  273. //
  274. inline int TComboBox::GetStringLen(int index) const {
  275.   return (int)CONST_CAST(TComboBox*,this)->
  276.            HandleMessage(CB_GETLBTEXTLEN, index);
  277. }
  278.  
  279. inline int TComboBox::GetSelIndex() const {
  280.   return (int)CONST_CAST(TComboBox*,this)->HandleMessage(CB_GETCURSEL);
  281. }
  282.  
  283. inline int TComboBox::SetSelIndex(int index) {
  284.   return (int)HandleMessage(CB_SETCURSEL, index);
  285. }
  286.  
  287. inline int TComboBox::SetSelString(const char far* findStr, int indexStart) {
  288.   return (int)HandleMessage(CB_SELECTSTRING, indexStart, (LPARAM)findStr);
  289. }
  290.  
  291. inline uint32 TComboBox::GetItemData(int index) const {
  292.   return CONST_CAST(TComboBox*,this)->HandleMessage(CB_GETITEMDATA, index);
  293. }
  294.  
  295. inline int TComboBox::SetItemData(int index, uint32 itemData) {
  296.   return (int)HandleMessage(CB_SETITEMDATA, index, itemData);
  297. }
  298.  
  299. inline int TComboBox::GetItemHeight(int index) const {
  300.   return (int)CONST_CAST(TComboBox*,this)->
  301.            HandleMessage(CB_GETITEMHEIGHT, index);
  302. }
  303.  
  304. inline int TComboBox::SetItemHeight(int index, int height) {
  305.   return (int)HandleMessage(CB_GETITEMHEIGHT, index, MAKELPARAM(height,0));
  306. }
  307.  
  308. #endif  // OWL_COMBOBOX_H
  309.