home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / combobox.pak / COMBOBXX.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  14KB  |  516 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <owl\framewin.h>
  7. #include <owl\static.h>
  8. #include <owl\combobox.h>
  9. #include <owl\inputdia.h>
  10. #include <owl\validate.h>
  11. #include "combobxx.rh"
  12.  
  13. const WORD ID_CB_BASE                = 200;
  14. const WORD ID_SIMPLE_COMBOBOX        = ID_CB_BASE;
  15. const WORD ID_DROPDOWN_COMBOBOX      = ID_CB_BASE + 1;
  16. const WORD ID_DROPDOWNLIST_COMBOBOX  = ID_CB_BASE + 2;
  17.  
  18. class TComboBoxWindow : public TFrameWindow {
  19.   public:
  20.     TComboBoxWindow(const char* title);
  21.  
  22.     // override method defined by TFrameWindow
  23.     //
  24.     void   Paint(TDC&, BOOL, TRect&);
  25.  
  26.     // message response functions
  27.     //
  28.     void EvCBNSelChange();
  29.  
  30.     void CmSimple();                  // create simple combobox.
  31.     void CmDropDown();                // create drop-down combobox.
  32.     void CmDropDownList();            // create drop-down-list combobox.
  33.     void CmAddString();               // add string to listbox of combobox.
  34.     void CmAddStringAt();             // add string at index position.
  35.     void CmFindString();              // goto/display index of given string.
  36.     void CmFindStringAt();            // goto index, display string.
  37.     void CmDeleteString();            // delete string.
  38.     void CmDeleteStringAt();          // delete string at index.
  39.     void CmClear();                   // clear combobox.
  40.     void CmShowList();                // show listbox part.
  41.     void CmHideList();                // hide listbox part.
  42.  
  43.     void CeSimple(TCommandEnabler& commandHandler);
  44.     void CeDropDown(TCommandEnabler& commandHandler);
  45.     void CeDropDownList(TCommandEnabler& commandHandler);
  46.     void CeNotSimple(TCommandEnabler& commandHandler);
  47.     void CeAny(TCommandEnabler& commandHandler);
  48.  
  49.   private:
  50.     TComboBox*        ComboBox;               // combobox.
  51.     TStatic*          CurSelOfListBox;        // text of selected string.
  52.     TStatic*          CurSelIndexOfListBox;   // index of selected string.
  53.     TStatic*          CurEditString;          // string of edit control.
  54.     TStatic*          SelStringLength;        // length of selected string.
  55.     TStatic*          EditStringLength;       // length of edit string.
  56.     UINT              WhichComboBox;          // current combobox.
  57.  
  58.     static const int     TextLen;
  59.  
  60.     void  ResetTextFields();                  // reset text fields to blanks.
  61.     void  UpdateTextFields();                 // updates from combobox.
  62.     int   InputNumber(char* pmpt, char* s);   // get number from user.
  63.     int   InputString(char* pmpt, char* s);   // get string from user.
  64.                                               // get string and number.
  65.     int   InputStringAndNumber(char* pmpt, char* s, int& n);
  66.  
  67.   DECLARE_RESPONSE_TABLE(TComboBoxWindow);
  68. };
  69.  
  70. DEFINE_RESPONSE_TABLE1(TComboBoxWindow, TFrameWindow)
  71.   EV_CBN_SELCHANGE(ID_SIMPLE_COMBOBOX,        EvCBNSelChange),
  72.   EV_CBN_SELCHANGE(ID_DROPDOWN_COMBOBOX,      EvCBNSelChange),
  73.   EV_CBN_SELCHANGE(ID_DROPDOWNLIST_COMBOBOX,  EvCBNSelChange),
  74.   EV_COMMAND(CM_SIMPLE,                 CmSimple),
  75.   EV_COMMAND(CM_DROPDOWN,               CmDropDown),
  76.   EV_COMMAND(CM_DROPDOWN_LIST,          CmDropDownList),
  77.   EV_COMMAND(CM_ADD_STRING,             CmAddString),
  78.   EV_COMMAND(CM_ADD_STRING_AT,          CmAddStringAt),
  79.   EV_COMMAND(CM_FIND_STRING,            CmFindString),
  80.   EV_COMMAND(CM_FIND_STRING_AT,         CmFindStringAt),
  81.   EV_COMMAND(CM_DELETE_STRING,          CmDeleteString),
  82.   EV_COMMAND(CM_DELETE_STRING_AT,       CmDeleteStringAt),
  83.   EV_COMMAND(CM_CLEAR,                  CmClear),
  84.   EV_COMMAND(CM_SHOW_LIST,              CmShowList),
  85.   EV_COMMAND(CM_HIDE_LIST,              CmHideList),
  86.   EV_COMMAND_ENABLE(CM_SIMPLE,          CeSimple),
  87.   EV_COMMAND_ENABLE(CM_DROPDOWN,        CeDropDown),
  88.   EV_COMMAND_ENABLE(CM_DROPDOWN_LIST,   CeDropDownList),
  89.   EV_COMMAND_ENABLE(CM_ADD_STRING,      CeAny),
  90.   EV_COMMAND_ENABLE(CM_ADD_STRING_AT,   CeAny),
  91.   EV_COMMAND_ENABLE(CM_FIND_STRING,     CeAny),
  92.   EV_COMMAND_ENABLE(CM_FIND_STRING_AT,  CeAny),
  93.   EV_COMMAND_ENABLE(CM_DELETE_STRING,   CeAny),
  94.   EV_COMMAND_ENABLE(CM_DELETE_STRING_AT,CeAny),
  95.   EV_COMMAND_ENABLE(CM_CLEAR,           CeAny),
  96.   EV_COMMAND_ENABLE(CM_SHOW_LIST,       CeNotSimple),
  97.   EV_COMMAND_ENABLE(CM_HIDE_LIST,       CeNotSimple),
  98. END_RESPONSE_TABLE;
  99.  
  100. const int TComboBoxWindow::TextLen = 31;
  101.  
  102. TComboBoxWindow::TComboBoxWindow(const char* title)
  103.    : TFrameWindow(0, title), TWindow(0, title)
  104. {
  105.   ComboBox = 0;
  106.   WhichComboBox = 0;
  107.   AssignMenu("COMBOBOX_MENU");
  108.  
  109.   // setup static text area.
  110.   new TStatic(this, -1, "Current selection:",  200, 30, 122, 18, 18);
  111.   CurSelOfListBox = new TStatic(this, -1, " ",  392, 30, 158, 18, 25);
  112.  
  113.   new TStatic(this, -1, "Index of current selection:",  200, 52, 176, 18, 28);
  114.   CurSelIndexOfListBox = new TStatic(this, -1, " ",  392, 52, 158, 18, 25);
  115.  
  116.   new TStatic(this, -1, "Length of current selection:",  200, 76, 184, 18, 30);
  117.   SelStringLength = new TStatic(this, -1, " ",  392, 76, 158, 18, 25);
  118.  
  119.   new TStatic(this, -1, "Edit control string:",  200, 103, 124, 18, 30);
  120.   CurEditString= new TStatic(this, -1, " ",  392, 103, 158, 18, 25);
  121.  
  122.   new TStatic(this, -1, "Length of edit control string:",  200, 125, 186, 18, 30);
  123.   EditStringLength = new TStatic(this, -1, " ",  392, 125, 158, 18, 25);
  124. }
  125.  
  126. //
  127. // make sure comboboxes are not dropped, if they are, they'll be left floating
  128. //
  129. void
  130. TComboBoxWindow::Paint(TDC&, BOOL, TRect&)
  131. {
  132.   if (ComboBox)
  133.     ComboBox->HideList();
  134. }
  135.  
  136. //
  137. // A selection of the listbox part of the combobox has taken place.
  138. // Update text info.
  139. //
  140. void
  141. TComboBoxWindow::EvCBNSelChange()
  142. {
  143.   UpdateTextFields();
  144. }
  145.  
  146. //
  147. // 'ComboBox|Simple' menu item.  Create simple combobox and fill it with some
  148. // strings.
  149. //
  150. void
  151. TComboBoxWindow::CmSimple()
  152. {
  153.   if (ComboBox) {
  154.     ComboBox->Destroy();
  155.     delete ComboBox;
  156.   }
  157.   ComboBox = new TComboBox(this, ID_SIMPLE_COMBOBOX, 10, 30, 150, 150,
  158.                            CBS_SIMPLE, 25);
  159.   ComboBox->Create();
  160.   ComboBox->SetFocus();
  161.   ComboBox->AddString("abc");
  162.   ComboBox->AddString("DEFG");
  163.   ComboBox->AddString("12345");
  164.   ComboBox->AddString("OWL");
  165.  
  166.   WhichComboBox = ID_SIMPLE_COMBOBOX;
  167.  
  168.   ResetTextFields();
  169.   UpdateTextFields();
  170. }
  171.  
  172. //
  173. // 'ComboBox|Drop Down' menu item.  Create drop down combobox and fill it some
  174. // some strings.
  175. //
  176. void
  177. TComboBoxWindow::CmDropDown()
  178. {
  179.   if (ComboBox) {
  180.     ComboBox->Destroy();
  181.     delete ComboBox;
  182.   }
  183.   ComboBox = new TComboBox(this, ID_DROPDOWN_COMBOBOX, 10, 30, 150, 150,
  184.                            CBS_DROPDOWN, 25);
  185.   ComboBox->Create();
  186.   ComboBox->SetFocus();
  187.   ComboBox->AddString("Jack");
  188.   ComboBox->AddString("Denice");
  189.   ComboBox->AddString("If then else");
  190.   ComboBox->AddString("a");
  191.   ComboBox->AddString("99");
  192.   ComboBox->AddString("42");
  193.   ComboBox->AddString("Help!");
  194.   ComboBox->AddString("windows");
  195.  
  196.   WhichComboBox = ID_DROPDOWN_COMBOBOX;
  197.  
  198.   ResetTextFields();
  199.   UpdateTextFields();
  200. }
  201.  
  202. //
  203. // 'ComboBox|Drop Down List' menu item.  Create drop down list combobox and fill
  204. // it with some strings.
  205. //
  206. void
  207. TComboBoxWindow::CmDropDownList()
  208. {
  209.   if (ComboBox) {
  210.     ComboBox->Destroy();
  211.     delete ComboBox;
  212.   }
  213.   ComboBox = new TComboBox(this, ID_DROPDOWNLIST_COMBOBOX, 10, 30, 150, 150,
  214.                            CBS_DROPDOWNLIST, 25);
  215.   ComboBox->Create();
  216.   ComboBox->SetFocus();
  217.   ComboBox->AddString("A B C D");
  218.   ComboBox->AddString("[^abc]");
  219.   ComboBox->AddString("1234567890");
  220.   ComboBox->AddString("C++");
  221.   ComboBox->AddString("Just a string");
  222.   ComboBox->AddString("and another!");
  223.  
  224.   WhichComboBox = ID_DROPDOWNLIST_COMBOBOX;
  225.  
  226.   ResetTextFields();
  227.   UpdateTextFields();
  228. }
  229.  
  230. //
  231. // Add a string to the listbox part of the combobox.
  232. //
  233. void
  234. TComboBoxWindow::CmAddString()
  235. {
  236.   char  buf[TextLen] = "";
  237.  
  238.   if (InputString("Enter string:", buf)) {
  239.     ComboBox->AddString(buf);
  240.     UpdateTextFields();
  241.   }
  242. }
  243.  
  244. //
  245. // Insert a string at a given index in the listbox part of the combobox.
  246. //
  247. void
  248. TComboBoxWindow::CmAddStringAt()
  249. {
  250.   char  buf[TextLen] = "";
  251.   int   index;
  252.  
  253.   if (InputStringAndNumber("Enter string:", buf, index)) {
  254.     if (ComboBox->InsertString(buf, index) != CB_ERR)
  255.        UpdateTextFields();
  256.     else
  257.        MessageBox("Index out of range", "Error", MB_OK);
  258.   }
  259. }
  260.  
  261. //
  262. // Find string.  Tell combobox to select string at given index.
  263. // Update text fields.
  264. //
  265. void
  266. TComboBoxWindow::CmFindString()
  267. {
  268.   char  buf[TextLen] = "";
  269.   int   index;
  270.  
  271.   if (InputString("Enter string:", buf)) {
  272.     if ((index = ComboBox->FindString(buf, 0)) != CB_ERR) {
  273.        ComboBox->SendMessage(CB_SETCURSEL, index, 0);
  274.        UpdateTextFields();
  275.     }
  276.     else
  277.        MessageBox("String not found", "Error", MB_OK);
  278.   }
  279. }
  280.  
  281. //
  282. // Find string.  Tell combobox to select string at given index.
  283. // Update text fields.  Assumes index input is correct, else atoi()
  284. // will return 0 and will be used to select the first string.
  285. //
  286. void
  287. TComboBoxWindow::CmFindStringAt()
  288. {
  289.   char  buf[TextLen] = "";
  290.   int   index;
  291.  
  292.   if (InputNumber("Enter number:", buf)) {
  293.     index = atoi(buf);
  294.     if (ComboBox->GetString(buf, index) != CB_ERR) {
  295.        ComboBox->SendMessage(CB_SETCURSEL, index, 0);
  296.        UpdateTextFields();
  297.     }
  298.     else
  299.        MessageBox("Index out of range", "Error", MB_OK);
  300.   }
  301. }
  302.  
  303. //
  304. // Delete string.  Delete string input by user.
  305. //
  306. void
  307. TComboBoxWindow::CmDeleteString()
  308. {
  309.   char  buf[TextLen] = "";
  310.   int   index;
  311.  
  312.   if (InputString( "Enter string:", buf)) {
  313.     if ((index = ComboBox->FindString(buf, 0)) != CB_ERR) {
  314.       ComboBox->DeleteString(index);
  315.       UpdateTextFields();
  316.     } else
  317.        MessageBox("String not found", "Error", MB_OK);
  318.   }
  319. }
  320.  
  321. //
  322. // Delete string.  Delete string at given index.
  323. //
  324. void
  325. TComboBoxWindow::CmDeleteStringAt()
  326. {
  327.   char  buf[TextLen] = "";
  328.   int   index;
  329.  
  330.   if (InputNumber("Enter number:", buf)) {
  331.     index = atoi(buf);
  332.     if (ComboBox->GetString(buf, index) != CB_ERR) {
  333.       ComboBox->DeleteString(index);
  334.       UpdateTextFields();
  335.     } else
  336.        MessageBox("Index out of range", "Error", MB_OK);
  337.   }
  338. }
  339.  
  340. //
  341. // Clear.  Clear edit control and listbox part of combobox.
  342. //
  343. void
  344. TComboBoxWindow::CmClear()
  345. {
  346.   ComboBox->Clear();
  347.   ComboBox->ClearList();
  348.   ResetTextFields();
  349. }
  350.  
  351. //
  352. // Show listbox part of combobox.
  353. //
  354. void
  355. TComboBoxWindow::CmShowList()
  356. {
  357.   ComboBox->ShowList();
  358. }
  359.  
  360. //
  361. // Hide listbox part of combobox.
  362. //
  363. void
  364. TComboBoxWindow::CmHideList()
  365. {
  366.   ComboBox->HideList();
  367. }
  368.  
  369. //
  370. // Command enablers...
  371. //
  372. // Handle popup menus.  Grey out certain menu items depending on
  373. // whether a combobox has been created or menu item is inappropriate
  374. // for current type of combobox.
  375. //
  376. //
  377.  
  378. void
  379. TComboBoxWindow::CeSimple(TCommandEnabler& ce)
  380. {
  381.   ce.Enable(WhichComboBox != ID_SIMPLE_COMBOBOX || !ComboBox);
  382. }
  383.  
  384. void
  385. TComboBoxWindow::CeDropDown(TCommandEnabler& ce)
  386. {
  387.   ce.Enable(WhichComboBox != ID_DROPDOWN_COMBOBOX || !ComboBox);
  388. }
  389.  
  390. void
  391. TComboBoxWindow::CeDropDownList(TCommandEnabler& ce)
  392. {
  393.   ce.Enable(WhichComboBox != ID_DROPDOWNLIST_COMBOBOX || !ComboBox);
  394. }
  395.  
  396. void
  397. TComboBoxWindow::CeNotSimple(TCommandEnabler& ce)
  398. {
  399.   ce.Enable(WhichComboBox != ID_SIMPLE_COMBOBOX && ComboBox);
  400. }
  401.  
  402. void
  403. TComboBoxWindow::CeAny(TCommandEnabler& ce)
  404. {
  405.   ce.Enable(ComboBox != 0);
  406. }
  407.  
  408. //
  409. // Updates text fields that reflex the combobox's state.
  410. //
  411. void
  412. TComboBoxWindow::UpdateTextFields()
  413. {
  414.   char  buf[TextLen] = "";
  415.   int   index = ComboBox->GetSelIndex();
  416.   int   strLen;
  417.  
  418.   if (index != -1) {     // is something selected?
  419.     ComboBox->GetString(buf, index);
  420.     strLen = ComboBox->GetStringLen(index);
  421.     CurSelOfListBox->SetText(buf);
  422.  
  423.     itoa(strLen, buf, 10);
  424.     SelStringLength->SetText(buf);
  425.  
  426.     itoa(index, buf, 10);
  427.     CurSelIndexOfListBox->SetText(buf);
  428.  
  429.     ComboBox->GetText(buf, TextLen - 1);
  430.     strLen = ComboBox->GetTextLen();
  431.     CurEditString->SetText(buf);
  432.  
  433.     if (WhichComboBox == ID_DROPDOWNLIST_COMBOBOX)
  434.       strLen = ComboBox->GetStringLen(ComboBox->GetSelIndex());
  435.     
  436.     itoa(strLen, buf, 10);
  437.     EditStringLength->SetText(buf);
  438.   }
  439.   else
  440.     ResetTextFields();
  441. }
  442.  
  443. //
  444. // Reset text fields to blanks.
  445. //
  446. void
  447. TComboBoxWindow::ResetTextFields()
  448. {
  449.   CurSelOfListBox->SetText(" ");
  450.   SelStringLength->SetText(" ");
  451.   CurSelIndexOfListBox->SetText(" ");
  452.   CurEditString->SetText(" ");
  453.   EditStringLength->SetText(" ");
  454. }
  455.  
  456. //
  457. // Get string from user.  Return 1 if successful, 0 otherwise.
  458. // assumes string length of TextLen - 1.
  459. //
  460. int
  461. TComboBoxWindow::InputString(char* pmpt, char* s)
  462. {
  463.   return TInputDialog(this, "String", pmpt, s, TextLen - 1).Execute() == IDOK;
  464. }
  465.  
  466. int
  467. TComboBoxWindow::InputNumber(char* pmpt, char* s)
  468. {
  469.   return TInputDialog(this, "String", pmpt, s, TextLen-1, 0, 
  470.                       new TFilterValidator("0-9")).Execute() == IDOK;
  471. }
  472.  
  473. //
  474. // Get string and number (index) from user.  Return 1 if successful, 0
  475. // otherwise. Assumes string length of TextLen - 1.
  476. //
  477. int
  478. TComboBoxWindow::InputStringAndNumber(char* pmpt, char* s, int& n)
  479. {
  480.   char sbuf[TextLen] = "";
  481.   char nbuf[TextLen] = "";
  482.   TInputDialog getStr(this, "String", pmpt, sbuf, TextLen - 1);
  483.  
  484.   // This input dialog has a validator that only accepts digits
  485.   //
  486.   TInputDialog getNum(this, "String", "Enter number:", nbuf, TextLen - 1,
  487.     0,new TFilterValidator("0-9"));
  488.  
  489.   if (getStr.Execute() == IDOK &&
  490.       getNum.Execute() == IDOK) {
  491.     strcpy(s, sbuf);
  492.     n = atoi(nbuf);
  493.     return 1;
  494.   } else
  495.     return 0;
  496. }
  497.  
  498. //----------------------------------------------------------------------------
  499.  
  500. class TComboBoxApp : public TApplication {
  501.   public:
  502.     void  InitMainWindow();
  503. };
  504.  
  505. void
  506. TComboBoxApp::InitMainWindow()
  507. {
  508.   MainWindow = new TComboBoxWindow("ComboBox Example");
  509. }
  510.  
  511. int
  512. OwlMain(int /*argc*/, char* /*argv*/ [])
  513. {
  514.   return TComboBoxApp().Run();
  515. }
  516.