home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / lstbx2 / lstbx2.cpp
Encoding:
C/C++ Source or Header  |  1992-01-22  |  8.2 KB  |  285 lines

  1. //========================================================================
  2. //  The following example routines have been provided by the Technical
  3. //  Support staff at Borland International.  They are provided as a
  4. //  courtesy and not as part of a Borland product, and as such, are
  5. //  provided without the assurance of technical support or any specific
  6. //  guarantees.
  7. //========================================================================
  8. //  Turbo Vision - Creating a TListBox
  9. //
  10. //  - This sample code demonstrates one method of creating a simple list
  11. //  box which is inserted into a dialog box.
  12. //
  13. //  - Creating a simple list box is requires an associated scroll bar
  14. //  and a list to display.  This example uses a list of the Turbo Vision
  15. //  classes to display.
  16. //  - Also demostrating how to use the double click message.
  17. //------------------------------------------------------------------------
  18. #define Uses_TApplication
  19. #define Uses_TBackground
  20. #define Uses_TButton
  21. #define Uses_TKeys
  22. #define Uses_TDeskTop
  23. #define Uses_TDialog
  24. #define Uses_TListBox
  25. #define Uses_TMenu
  26. #define Uses_TMenuBar
  27. #define Uses_TMenuItem
  28. #define Uses_TRect
  29. #define Uses_TScrollBar
  30. #define Uses_TStaticText
  31. #define Uses_TStatusDef
  32. #define Uses_TStatusItem
  33. #define Uses_TStatusLine
  34. #define Uses_TStringCollection
  35. #define Uses_MsgBox
  36. #define Uses_TEventQueue
  37. #include <tv.h>
  38. #include <stdlib.h>
  39. #include <strstrea.h>
  40.  
  41. const cmAboutCmd = 100;  // User selected menu item 'About'
  42. const cmListCmd  = 101;  // User selected menu item 'List'
  43.  
  44. char *theList[] =
  45. {
  46.     "fpbase",             "fpstream",           "ifpstream",
  47.     "Int11trap",          "iopstream",          "ipstream",
  48.     "MsgBoxText",         "ofpstream",          "opstream",
  49.     "otstream",           "pstream",            "TApplication",
  50.     "TBackground",        "TBufListEntry",      "TButton",
  51.     "TChDirDialog",       "TCheckBoxes",        "TCluster",
  52.     "TCollection",        "TColorDialog",       "TColorDisplay",
  53.     "TColorGroup",        "TColorGroupList",    "TColorItem",
  54.     "TColorItemList",     "TColorSelector",     "TCommandSet",
  55.     "TCrossRef",          "TDeskInit",          "TDeskTop",
  56.     "TDialog",            "TDirCollection",     "TDirEntry",
  57.     "TDirListBox",        "TDisplay",           "TDrawBuffer",
  58.     "TEditor",            "TEditWindow",        "TEventQueue",
  59.     "TFileCollection",    "TFileDialog",        "TFileEditor",
  60.     "TFileInfoPane",      "TFileInputLine",     "TFileList",
  61.     "TFrame",             "TGroup",             "THelpFile",
  62.     "THelpIndex",         "THelpTopic",         "THelpViewer",
  63.     "THelpWindow",        "THistInit",          "THistory",
  64.     "THistoryViewer",     "THistoryWindow",     "THWMouse",
  65.     "TIndicator",         "TInputLine",         "TLabel",
  66.     "TListBox",           "TListViewer",        "TMemo",
  67.     "TMenu",              "TMenuBar",           "TMenuBox",
  68.     "TMenuItem",          "TMenuView",          "TMonoSelector",
  69.     "TMouse",             "TNSCollection",      "TNSSortedCollection",
  70.     "TObject",            "TPalette",           "TParagraph",
  71.     "TParamText",         "TPoint",             "TPReadObjects",
  72.     "TProgInit",          "TProgram",           "TPWObj",
  73.     "TPWrittenObjects",   "TRadioButtons",      "TRect",
  74.     "TResourceCollection","TResourceFile",      "TScreen",
  75.     "TScrollBar",         "TScroller",          "TSItem",
  76.     "TSortedCollection",  "TSortedListBox",     "TStaticText",
  77.     "TStatusDef",         "TStatusItem",        "TStatusLine",
  78.     "TStreamable",        "TStreamableClass",   "TStreamableTypes",
  79.     "TStrIndexRec",       "TStringCollection",  "TStringList",
  80.     "TStrListMaker",      "TSubMenu",           "TSystemError",
  81.     "TTerminal",          "TTextDevice",        "TView",
  82.     "TVMemMgr",           "TWindow",            "TWindowInit"
  83. };
  84.  
  85. int numStrings = sizeof(theList) / sizeof(theList[0]);
  86.  
  87. class TMyApplication : public TApplication
  88. {
  89. public:
  90.     TMyApplication();
  91.     static TMenuBar *initMenuBar(TRect);
  92.     void handleEvent(TEvent &);
  93. private:
  94.     void aboutDlg();
  95.     void listDlg();
  96. };
  97.  
  98.  
  99. //
  100. // TMyApplication - Constructor.
  101. //
  102.  
  103. TMyApplication::TMyApplication() :
  104.     TProgInit(&TApplication::initStatusLine,&TMyApplication::initMenuBar,
  105.               &TApplication::initDeskTop)
  106. {
  107. }
  108.  
  109.  
  110. //
  111. // initMenuBar - Initialize the menu bar.
  112. //
  113.  
  114. TMenuBar *TMyApplication::initMenuBar(TRect bounds)
  115. {
  116.     bounds.b.y = bounds.a.y + 1;
  117.     return(new TMenuBar(bounds,
  118.         new TMenu(
  119.             *new TMenuItem("~A~bout",cmAboutCmd,kbAltA,hcNoContext,0,
  120.              new TMenuItem("~L~ist",cmListCmd,kbAltL,hcNoContext,0)))));
  121. }
  122.  
  123.  
  124. //
  125. // handleEvent - Handle various events for TApp (menu commands).
  126. //
  127.  
  128. void TMyApplication::handleEvent(TEvent &event)
  129. {
  130.     TApplication::handleEvent(event);
  131.  
  132.     if (event.what == evCommand)
  133.     {
  134.         switch (event.message.command)
  135.         {
  136.             case cmAboutCmd:
  137.             {
  138.                 aboutDlg();
  139.                 clearEvent(event);
  140.                 break;
  141.             }
  142.             case cmListCmd:
  143.             {
  144.                 listDlg();
  145.                 clearEvent(event);
  146.                 break;
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152.  
  153. //
  154. // aboutDlg - The about dialog box for TApplication. (Who, When, and What).
  155. //
  156.  
  157. void TMyApplication::aboutDlg()
  158. {
  159.     TDialog *pd = new TDialog(TRect(0,0,35,12),"About");
  160.     if (pd)
  161.     {
  162.         pd->options |= ofCentered;
  163.         pd->insert(new TStaticText(TRect(1,2,34,7),
  164.                    "\003Turbo Vision Example\n\003\n"
  165.                    "\003Creating a TListBox\n\003\n"
  166.                    "\003Borland Technical Support"));
  167.         pd->insert(new TButton(TRect(3,9,32,11),"~O~k",cmOK,bfDefault));
  168.  
  169.         if (validView(pd) != 0)
  170.         {
  171.             deskTop->execView(pd);
  172.  
  173.             destroy(pd);
  174.         }
  175.     }
  176. }
  177.  
  178. class TMyListBox : public TListBox
  179. {
  180. public:
  181.     TMyListBox(TRect& bounds,ushort num,TScrollBar *bar) :
  182.         TListBox(bounds,num,bar) {};
  183.     void handleEvent(TEvent&);
  184. };
  185.  
  186. void TMyListBox::handleEvent(TEvent& event)
  187. {
  188.     if ((event.what == evMouseDown) && (event.mouse.doubleClick))
  189.     {
  190.         message(owner,evBroadcast,cmListItemSelected,this);
  191.         clearEvent(event);
  192.     }
  193.     TListBox::handleEvent(event);
  194. }
  195.  
  196. class TListDlg : public TDialog
  197. {
  198. public:
  199.     TListDlg(TRect&,char *);
  200.     void handleEvent(TEvent&);
  201.     TMyListBox *listBox;
  202. };
  203.  
  204. TListDlg::TListDlg(TRect& bounds,char *aTitle) :
  205.         TDialog(bounds,aTitle),
  206.         TWindowInit(&TListDlg::initFrame)
  207. {
  208.     TStringCollection *theCollection = new TStringCollection(100,10);
  209.  
  210.     for(int i=0;i < numStrings;i++)
  211.     {
  212.         theCollection->insert(newStr(theList[i]));
  213.     }
  214.  
  215.  
  216.     options |= ofCentered;
  217.     eventMask |= evBroadcast;
  218.  
  219.     TScrollBar *listScroller = new TScrollBar(TRect(47,2,48,17));
  220.  
  221.     listBox = new TMyListBox(TRect(3,2,47,17),2,listScroller);
  222.  
  223.     listBox->newList(theCollection);
  224.  
  225.     insert(listBox);
  226.     insert(listScroller);
  227.  
  228.     insert(new TButton(TRect(22,19,30,21),"~O~K",cmOK,bfDefault));
  229.  
  230.     selectNext(False);
  231. }
  232.  
  233.  
  234. void TListDlg::handleEvent(TEvent& event)
  235. {
  236.     if ((event.what == evBroadcast) && (event.message.command == cmListItemSelected))
  237.     {
  238.         char selItem[81];
  239.         char message[201];
  240.  
  241.         listBox->getText(selItem,listBox->focused,80);
  242.  
  243.         ostrstream(message,200) << "\003The item selected was " << selItem
  244.                                 << ends;
  245.  
  246.         messageBox(message,mfOKButton | mfInformation);
  247.     }
  248.     TDialog::handleEvent(event);
  249. }
  250.  
  251.  
  252. //
  253. // listDlg - A dialog box to demostrate a list box.
  254. //
  255.  
  256. void TMyApplication::listDlg()
  257. {
  258.     TListDlg *pd = new TListDlg(TRect(0,0,50,23),"TV Classes");
  259.  
  260.     if (validView(pd) != 0)
  261.     {
  262.         deskTop->execView(pd);
  263.  
  264.         destroy(pd);
  265.     }
  266. }
  267.  
  268.  
  269. //
  270. // main:
  271. //      Here we declare an instance of the application object and call the
  272. //      run() member function to start of the program.  Usually, this will be
  273. //      all that is done here.  (Certain other initializations, like getting
  274. //      EMS memory for the overlay manager would also go here.)
  275. //
  276.  
  277. int main(void)
  278. {
  279.     TMyApplication myApplication;
  280.  
  281.     myApplication.run();
  282.  
  283.     return 0;
  284. }
  285.