home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / ctllst.cpp < prev    next >
C/C++ Source or Header  |  1994-05-26  |  7KB  |  278 lines

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <owl\applicat.h>
  4. #include <owl\button.h>
  5. #include <owl\edit.h>
  6. #include <owl\framewin.h>
  7. #include <owl\listbox.h>
  8. #include <owl\static.h>
  9. #include <owl\window.h>
  10. #include <owl\window.rh>
  11.  
  12. #include "ctllst1.h"
  13.  
  14. class TMyWindow : public TWindow
  15. {
  16. public:
  17.    TMyWindow(TWindow *parent = 0);
  18.    virtual ~TMyWindow();
  19.  
  20. protected:
  21.    virtual void SetupWindow();
  22.  
  23.    void CbAdd();
  24.    void CbDel();
  25.    void CbGetSelStr();
  26.    void CbSetSelStr();
  27.    void CbGetSelIdx();
  28.    void CbSetSelIdx();
  29.    void CbGetStr();
  30.    void CmExit();
  31.  
  32. private:
  33.    TListBox *list;
  34.    TEdit *strbox, *idxbox;
  35.  
  36.    DECLARE_RESPONSE_TABLE(TMyWindow);
  37. };
  38. DEFINE_RESPONSE_TABLE1(TMyWindow, TWindow)
  39.    EV_COMMAND(CM_EXIT, CmExit),
  40.    EV_BN_CLICKED(IDB_ADD, CbAdd),
  41.    EV_BN_CLICKED(IDB_DEL, CbDel),
  42.    EV_BN_CLICKED(IDB_GETSELSTR, CbGetSelStr),
  43.    EV_BN_CLICKED(IDB_SETSELSTR, CbSetSelStr),
  44.    EV_BN_CLICKED(IDB_GETSELIDX, CbGetSelIdx),
  45.    EV_BN_CLICKED(IDB_SETSELIDX, CbSetSelIdx),
  46.    EV_BN_CLICKED(IDB_GETSTR, CbGetStr),
  47.    EV_BN_CLICKED(IDB_EXIT, CmExit),
  48. END_RESPONSE_TABLE;
  49.  
  50. TMyWindow::TMyWindow(TWindow *parent)
  51. {
  52.    Init(parent, 0, 0);
  53.  
  54.    int   lowvspacing = 5,
  55.          hivspacing = 25,
  56.          hspacing = 50,
  57.          wctl = 150,
  58.          hctl = 30,
  59.          x0 = 30,
  60.          y0 = 50,
  61.          y1;
  62.    int   wbox = 2 * wctl + hspacing;
  63.    int   hlist = hctl + lowvspacing + 4 * (hctl + hivspacing);
  64.  
  65.    int   x = x0, y = y0;
  66.  
  67.    // Create the listbox and its label
  68.    //
  69.    new TStatic(this, -1, "List Box", x, y, wctl, hctl);
  70.    y += hctl + lowvspacing;
  71.    list = new TListBox(this, IDL_STRINGS, x, y, wctl, hlist);
  72.  
  73.    // Create the edit boxes and their labels
  74.    //
  75.    x += wctl + hspacing;
  76.    y = y0;
  77.    new TStatic(this, -1, "String Box", x, y, wctl, hctl);
  78.    y += hctl + lowvspacing;
  79.    strbox = new TEdit(this, IDE_STRING, "", x, y, wbox, hctl);
  80.    y += hctl + hivspacing;
  81.    new TStatic(this, -1, "Index Box", x, y, wctl, hctl);
  82.    y += hctl + lowvspacing;
  83.    idxbox = new TEdit(this, IDE_INDEX, "", x, y, wbox, hctl);
  84.  
  85.    // Create first column of buttons
  86.    //
  87.    y1 = y += hctl + hivspacing;
  88.    new TButton(this, IDB_ADD, "Add String", x, y, wctl, hctl);
  89.    y += hctl + hivspacing;
  90.    new TButton(this, IDB_DEL, "Delete String", x, y, wctl, hctl);
  91.    y += hctl + hivspacing;
  92.    new TButton(this, IDB_GETSELSTR, "Get Selected String", x, y, wctl, hctl);
  93.    y += hctl + hivspacing;
  94.    new TButton(this, IDB_SETSELSTR, "Set Selected String", x, y, wctl, hctl);
  95.  
  96.    // Create the second column of buttons
  97.    y = y1;
  98.    x += wctl + hspacing;
  99.    new TButton(this, IDB_GETSELIDX, "Get Selected Index", x, y, wctl, hctl);
  100.    y += hctl + hivspacing;
  101.    new TButton(this, IDB_SETSELIDX, "Set Selected Index", x, y, wctl, hctl);
  102.    y += hctl + hivspacing;
  103.    new TButton(this, IDB_GETSTR, "Get String by Index", x, y, wctl, hctl);
  104.    y += hctl + hivspacing;
  105.    new TButton(this, IDB_EXIT, "Exit", x, y, wctl, hctl);
  106. }
  107.  
  108. TMyWindow::~TMyWindow()
  109. {
  110. }
  111.  
  112. void TMyWindow::SetupWindow()
  113. {
  114.    TWindow::SetupWindow();    // Initialize the visual element
  115.  
  116.    // Initialize the list box with some data and
  117.    // select the second item
  118.    //
  119.    if (list)
  120.       {
  121.       list->AddString("Keith");
  122.       list->AddString("Kevin");
  123.       list->AddString("Ingrid");
  124.       list->AddString("Roger");
  125.       list->AddString("Rick");
  126.       list->AddString("Beth");
  127.       list->AddString("Kate");
  128.       list->AddString("James");
  129.       list->SetSelIndex(1);
  130.       }
  131. }
  132.  
  133. void TMyWindow::CbAdd()
  134. {
  135.    if (strbox && list)
  136.       {
  137.       char *str;
  138.       int size = strbox->GetWindowTextLength() + 1;
  139.       if ((size > 1) && (NULL != (str = new char[size])))
  140.          {
  141.          strbox->GetWindowText(str, size);
  142.          if (list->FindExactString(str, -1) >= 0)
  143.             MessageBox("Cannot add duplicate names", "Bad Data");
  144.          else
  145.             {
  146.             int ix = list->AddString(str);
  147.             list->SetSelIndex(ix);
  148.             }
  149.          delete str;
  150.          }
  151.       }
  152. }
  153.  
  154. void TMyWindow::CbDel()
  155. {
  156.    if (list)
  157.       {
  158.       int ix = list->GetSelIndex();
  159.       list->DeleteString(ix);
  160.       list->SetSelIndex((ix > 0) ? (ix - 1) : 0);
  161.       }
  162. }
  163.  
  164. void TMyWindow::CbGetSelStr()
  165. {
  166.    if (list && strbox)
  167.       {
  168.       char *str;
  169.       int ix = list->GetSelIndex();
  170.       if (ix >= 0)
  171.          {
  172.          if (NULL != (str = new char[list->GetStringLen(ix) + 1]))
  173.             {
  174.             list->GetString(str, ix);
  175.             strbox->SetWindowText(str);
  176.             delete str;
  177.             }
  178.          }
  179.       }
  180. }
  181.  
  182. void TMyWindow::CbSetSelStr()
  183. {
  184.    if (list && strbox)
  185.       {
  186.       int ix = list->GetSelIndex();
  187.  
  188.       char *str;
  189.       int size = strbox->GetWindowTextLength() + 1;
  190.       if ((size > 1) && (NULL != (str = new char[size])))
  191.          {
  192.          strbox->GetWindowText(str, size);
  193.          if (list->FindExactString(str, -1) >= 0)
  194.             MessageBox("Cannot add duplicate names", "Bad Data");
  195.          else
  196.             {
  197.             list->DeleteString(ix);
  198.             ix = list->AddString(str);
  199.             list->SetSelIndex(ix);
  200.             }
  201.          delete str;
  202.          }
  203.       }
  204. }
  205.  
  206. void TMyWindow::CbGetSelIdx()
  207. {
  208.    if (list && idxbox)
  209.       {
  210.       char str[15];
  211.       sprintf(str, "%d", list->GetSelIndex());
  212.       idxbox->SetWindowText(str);
  213.       }
  214. }
  215.  
  216. void TMyWindow::CbSetSelIdx()
  217. {
  218.    if (list && idxbox)
  219.       {
  220.       char *str;
  221.       int size = idxbox->GetWindowTextLength() + 1;
  222.       if ((size > 1) && (NULL != (str = new char[size])))
  223.          {
  224.          idxbox->GetWindowText(str, size);
  225.          list->SetSelIndex(atoi(str));
  226.          delete str;
  227.          }
  228.       }
  229. }
  230.  
  231. void TMyWindow::CbGetStr()
  232. {
  233.    if (list && idxbox && strbox)
  234.       {
  235.       char *str;
  236.       int ix = -1;
  237.       int size = idxbox->GetWindowTextLength() + 1;
  238.       if ((size > 1) && (NULL != (str = new char[size])))
  239.          {
  240.          idxbox->GetWindowText(str, size);
  241.          ix = atoi(str);
  242.          delete str;
  243.          }
  244.       if ((ix >= 0) && (NULL != (str = new char[list->GetStringLen(ix) + 1])))
  245.          {
  246.          list->GetString(str, ix);
  247.          strbox->SetWindowText(str);
  248.          delete str;
  249.          }
  250.       }
  251. }
  252.  
  253.  
  254. void TMyWindow::CmExit()
  255. {
  256.    SendMessage(WM_CLOSE);
  257. }
  258.  
  259. class TListApp : public TApplication
  260. {
  261. public:
  262.    TListApp() : TApplication()
  263.       { nCmdShow = SW_SHOWMAXIMIZED; }
  264.  
  265.    void InitMainWindow()
  266.       {
  267.       SetMainWindow(new TFrameWindow(  0,
  268.                            "Simple List Box Tester Application",
  269.                            new TMyWindow ));
  270.       GetMainWindow()->AssignMenu("EXITMENU");
  271.       }
  272. };
  273.  
  274. int OwlMain(int, char *[])
  275. {
  276.    return TListApp().Run();
  277. }
  278.