home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / COMBOBOX.CPP < prev    next >
Text File  |  1995-08-29  |  7KB  |  216 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* -------------------------------------------------------
  4.   COMBOBOX.CPP
  5.   Defines type TComboBox.  This defines the basic behavior
  6.   of all combo box controls.
  7.   -------------------------------------------------------- */
  8.  
  9. #include "combobox.h"
  10.  
  11. /* Constructor for a TComboBoxData object. Initializes Strings and
  12.    Selection. */
  13. TComboBoxData::TComboBoxData()
  14. {
  15.   Strings = new Array(10, 0, 10);
  16.   Selection = NULL;
  17. }
  18.  
  19. /* Destructor for TComboBoxData. Deletes Strings and Selection. */
  20. TComboBoxData::~TComboBoxData()
  21. {
  22.   if ( Strings )
  23.     delete Strings;
  24.   if ( Selection )
  25.     delete Selection;
  26. }
  27.  
  28. /* Adds the supplied string to the Strings Array and copies it into
  29.    Selection if IsSelected is TRUE. */
  30. void TComboBoxData::AddString(Pchar AString, BOOL IsSelected)
  31. {
  32.   Strings->add(*new String(AString));
  33.   if ( IsSelected )
  34.   {
  35.       if (AString != Selection)
  36.       {
  37.           if (Selection)
  38.             delete Selection;
  39.       Selection = strdup(AString);
  40.       }
  41.   }
  42. }
  43.  
  44. /* Constructor for a TComboBox object.  Initializes its data fields
  45.    using supplied parameters and default values.  By default, an
  46.    MS-Windows combobox associated with the TComboBox will have a
  47.    vertical scrollbar and will maintain its entries in alphabetical
  48.    order. */
  49. TComboBox::TComboBox(PTWindowsObject AParent, int AnId, int X, int Y,
  50.                      int W, int H, DWORD AStyle, WORD ATextLen,
  51.                      PTModule AModule)
  52.            : TListBox(AParent, AnId, X, Y, W, H, AModule)
  53. {
  54.   TextLen = ATextLen;
  55.   Attr.Style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | CBS_SORT
  56.                | CBS_AUTOHSCROLL | WS_VSCROLL | AStyle;
  57. }
  58.  
  59. TComboBox::TComboBox(PTWindowsObject AParent, int ResourceId,
  60.                      WORD ATextLen, PTModule AModule)
  61.            : TListBox(AParent, ResourceId, AModule)
  62. {
  63.   TextLen = ATextLen;
  64. }
  65.  
  66. /* Sets and selects the contents of the associated edit
  67.    control to the supplied string. */
  68. void TComboBox::SetText(LPSTR AString)
  69. {
  70.   if (SetSelString(AString, -1) < 0 )  // AString not in list box
  71.   {
  72.     SetWindowText(HWindow, AString);
  73.     SetEditSel(0, _fstrlen(AString));
  74.   }
  75. }
  76.  
  77. /* Returns, in the supplied reference parameters, the starting and
  78.    ending positions of the text selected in the associated
  79.    edit control. Returns CB_ERR if the combo box has no edit
  80.    control */
  81. int TComboBox::GetEditSel(int& StartPos, int& EndPos)
  82. {
  83.     long RetValue;
  84.  
  85.     RetValue = SendMessage(HWindow, CB_GETEDITSEL, 0, 0);
  86.     StartPos = LOWORD(RetValue);
  87.     EndPos   = HIWORD(RetValue);
  88.     return (int)RetValue;
  89. }
  90.  
  91. /* Shows the list of an associated drop-down combobox. */
  92. void TComboBox::ShowList()
  93. {
  94.     if ( (GetWindowLong(HWindow,GWL_STYLE) & CBS_DROPDOWN) == CBS_DROPDOWN )
  95.       SendMessage(HWindow, CB_SHOWDROPDOWN, 1, 0);
  96. }
  97.  
  98. /* Hides the list of an associated drop-down combobox. */
  99. void TComboBox::HideList()
  100. {
  101.     if ( (GetWindowLong(HWindow,GWL_STYLE) & CBS_DROPDOWN) == CBS_DROPDOWN )
  102.         SendMessage(HWindow, CB_SHOWDROPDOWN, 0, 0);
  103. }
  104.  
  105.  
  106. static void DoAddForCB(RObject AString, Pvoid AComboBox)
  107. {
  108.   if (AString!=NOOBJECT)
  109.      ((PTListBox)AComboBox)->AddString((Pchar)(PCchar)(RString)AString);
  110. }
  111.  
  112. /* Transfers the items and selection of the combo box to or from a
  113.    transfer buffer if TF_SETDATA or TF_GETDATA, repectively, is passed
  114.    as the TransferFlag. DataPtr should point to a PTComboBoxData
  115.    (i.e. it should be a pointer to a pointer to a TComboBoxData)
  116.    which points to the data to be transferred.
  117.    Transfer returns the size of PTComboBox (the pointer not the
  118.    object). To retrieve the size without transferring data, pass
  119.    TF_SIZEDATA as the TransferFlag. */
  120. WORD TComboBox::Transfer(Pvoid DataPtr, WORD TransferFlag)
  121. {
  122.    Pchar TmpString;
  123.  
  124.   PTComboBoxData ComboBoxData = *(PTComboBoxData _FAR *)DataPtr;
  125.  
  126.   if ( TransferFlag == TF_GETDATA )
  127.   {
  128. #if 0    
  129.     int StringSize = GetWindowTextLength(HWindow) + 1;
  130.     if ( ComboBoxData->Selection )
  131.       delete ComboBoxData->Selection;
  132.     ComboBoxData->Selection= new char[StringSize];
  133.     GetWindowText(HWindow, ComboBoxData->Selection, StringSize);
  134. #endif
  135.  
  136.     // first, clear out Strings array and fill with contents of list box
  137.     ComboBoxData->Strings->flush();
  138.  
  139.     for (int i=0; i<GetCount(); i++) {
  140.        TmpString = new char[GetStringLen(i)+1];
  141.        GetString(TmpString, i);
  142.        ComboBoxData->AddString(TmpString, FALSE);
  143.        delete TmpString;
  144.     }
  145.  
  146.    LONG nCBIndex = SendMessage(HWindow, CB_GETCURSEL, (WORD) NULL, (DWORD) NULL);
  147.    if (ComboBoxData->Selection)
  148.        delete ComboBoxData->Selection;
  149.  
  150.    if (nCBIndex != CB_ERR)   //item is selected
  151.       {  int StringSize = SendMessage (HWindow, CB_GETLBTEXTLEN, nCBIndex, (DWORD) NULL);
  152.           ComboBoxData->Selection = new char[StringSize];
  153.           if (SendMessage (HWindow, CB_GETLBTEXT, nCBIndex, (DWORD) ComboBoxData->Selection) == CB_ERR)
  154.              delete ComboBoxData->Selection;    //delete if not successfully filled
  155.       }
  156.   }
  157.   else
  158.     if ( TransferFlag == TF_SETDATA )
  159.     {
  160.       ClearList();
  161.       ComboBoxData->Strings->forEach(DoAddForCB, this);
  162.  
  163.       int SelIndex = FindExactString(ComboBoxData->Selection, -1);
  164.       if ( SelIndex > -1 )
  165.         SetSelIndex(SelIndex);
  166.  
  167.       SetWindowText(HWindow, ComboBoxData->Selection);
  168.     }
  169.   return sizeof(PTComboBoxData);
  170. }
  171.  
  172. /* Returns the appropriate Windows message int identifier for the
  173.    function identified by the supplied AnId. Allows instances
  174.    of TComboBox to inherit many TListBox methods */
  175. WORD TComboBox::GetMsgID(WORD AnId)
  176. {
  177.   WORD MsgXlat[] = {CB_ADDSTRING,    CB_INSERTSTRING, CB_DELETESTRING,
  178.                     CB_RESETCONTENT, CB_GETCOUNT,     CB_GETLBTEXT,
  179.                     CB_GETLBTEXTLEN, CB_SELECTSTRING, CB_SETCURSEL,
  180.                     CB_GETCURSEL,    CB_FINDSTRING };
  181.  
  182.   return MsgXlat[AnId];
  183. }
  184.  
  185. /* Limits the amount of text that the user can enter in the combo box's
  186.    edit control to the value of TextLen minus 1. */
  187. void TComboBox::SetupWindow()
  188. {
  189.   TListBox::SetupWindow();
  190.   if ( TextLen != 0 )
  191.     SendMessage(HWindow, CB_LIMITTEXT, TextLen - 1, 0);
  192. }
  193.  
  194. /* Reads an instance of TComboBox from the supplied ipstream. */
  195. void *TComboBox::read(Ripstream is)
  196. {
  197.   TListBox::read(is);
  198.   is >> TextLen;
  199.   return this;
  200. }
  201.  
  202. /* Writes the TComboBox to the supplied opstream. */
  203. void TComboBox::write(Ropstream os)
  204. {
  205.   TListBox::write(os);
  206.   os << TextLen;
  207. }
  208.  
  209. PTStreamable TComboBox::build()
  210. {
  211.   return new TComboBox(streamableInit);
  212. }
  213.  
  214. TStreamableClass RegComboBox("TComboBox", TComboBox::build,
  215.                  __DELTA(TComboBox));
  216.