home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / lbox / listbox1.cpp next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  7.0 KB  |  198 lines

  1. /* LISTBOX.CPP: Creating a TListBox
  2.  
  3.    This sample code demonstrates one method of creating a simple list
  4.    box which is inserted into a dialog box.
  5.  
  6.    Creating a simple list box requires an associated scroll bar and a
  7.    list to display.  This example displays a list of the Turbo Vision
  8.    classes.
  9.  
  10.   ----------------------------------------------------------------------
  11.    The following example routines have been provided by the Technical
  12.    Support staff at Borland International.  They are provided as a
  13.    courtesy and not as part of a Borland product, and as such, are
  14.    provided without the assurance of technical support or any specific
  15.    guarantees.
  16. */
  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. #include <tv.h>
  36. #include <stdlib.h>
  37.  
  38. //  global data
  39. const cmAbout   = 100;  // User selected menu item 'About'
  40. const cmList    = 101;  // User selected menu item 'List'
  41.  
  42. // we'll use a list of the TV classes for something
  43. // to display in the list box
  44. char *theList[] = {
  45.     "fpbase",             "fpstream",           "ifpstream",
  46.     "Int11trap",          "iopstream",          "ipstream",
  47.     "MsgBoxText",         "ofpstream",          "opstream",
  48.     "otstream",           "pstream",            "TApplication",
  49.     "TBackground",        "TBufListEntry",      "TButton",
  50.     "TChDirDialog",       "TCheckBoxes",        "TCluster",
  51.     "TCollection",        "TColorDialog",       "TColorDisplay",
  52.     "TColorGroup",        "TColorGroupList",    "TColorItem",
  53.     "TColorItemList",     "TColorSelector",     "TCommandSet",
  54.     "TCrossRef",          "TDeskInit",          "TDeskTop",
  55.     "TDialog",            "TDirCollection",     "TDirEntry",
  56.     "TDirListBox",        "TDisplay",           "TDrawBuffer",
  57.     "TEditor",            "TEditWindow",        "TEventQueue",
  58.     "TFileCollection",    "TFileDialog",        "TFileEditor",
  59.     "TFileInfoPane",      "TFileInputLine",     "TFileList",
  60.     "TFrame",             "TGroup",             "THelpFile",
  61.     "THelpIndex",         "THelpTopic",         "THelpViewer",
  62.     "THelpWindow",        "THistInit",          "THistory",
  63.     "THistoryViewer",     "THistoryWindow",     "THWMouse",
  64.     "TIndicator",         "TInputLine",         "TLabel",
  65.     "TListBox",           "TListViewer",        "TMemo",
  66.     "TMenu",              "TMenuBar",           "TMenuBox",
  67.     "TMenuItem",          "TMenuView",          "TMonoSelector",
  68.     "TMouse",             "TNSCollection",      "TNSSortedCollection",
  69.     "TObject",            "TPalette",           "TParagraph",
  70.     "TParamText",         "TPoint",             "TPReadObjects",
  71.     "TProgInit",          "TProgram",           "TPWObj",
  72.     "TPWrittenObjects",   "TRadioButtons",      "TRect",
  73.     "TResourceCollection","TResourceFile",      "TScreen",
  74.     "TScrollBar",         "TScroller",          "TSItem",
  75.     "TSortedCollection",  "TSortedListBox",     "TStaticText",
  76.     "TStatusDef",         "TStatusItem",        "TStatusLine",
  77.     "TStreamable",        "TStreamableClass",   "TStreamableTypes",
  78.     "TStrIndexRec",       "TStringCollection",  "TStringList",
  79.     "TStrListMaker",      "TSubMenu",           "TSystemError",
  80.     "TTerminal",          "TTextDevice",        "TView",
  81.     "TVMemMgr",           "TWindow",            "TWindowInit"
  82. };
  83.  
  84. // determine the number of items in the list
  85. // did you know there are 111 classes in Turbo Vision?
  86. int numStrings = sizeof(theList) / sizeof(theList[0]);
  87.  
  88. //////////////////////////////////////////// TApp ////////////////////////
  89. class TApp : public TApplication {
  90.   public:
  91.     TApp();
  92.  
  93.     // virtual functions to be locally redefined
  94.     static TMenuBar *initMenuBar(TRect r);
  95.     void handleEvent(TEvent &event);
  96.  
  97.     // declare new functions
  98.     void AboutDialog();
  99.     void ListDialog();
  100. };
  101.  
  102. //==========================================[ TApp ]======================
  103. TApp::TApp() : TProgInit(&TApplication::initStatusLine,
  104.                          &TApp::initMenuBar,
  105.                          &TApplication::initDeskTop) {
  106. }
  107.  
  108. //=========================================== TApp =======================
  109. TMenuBar *TApp::initMenuBar(TRect r) {
  110.  
  111.   r.b.y = r.a.y + 1;
  112.   return(new TMenuBar(r, new TMenu(
  113.     *new TMenuItem("~A~bout", cmAbout, kbAltA, hcNoContext, 0,
  114.       new TMenuItem("~L~ist", cmList, kbAltL, hcNoContext, 0)))));
  115. }
  116.  
  117. //=========================================== TApp =======================
  118. void TApp::handleEvent(TEvent &event) {
  119.  
  120.   TApplication::handleEvent(event);
  121.   if (event.what == evCommand) {
  122.     switch(event.message.command) {
  123.       case cmAbout:       // display the about box
  124.         AboutDialog();
  125.         clearEvent(event);
  126.         break;
  127.       case cmList:        // display our list box
  128.         ListDialog();
  129.         clearEvent(event);
  130.         break;
  131.     }
  132.   }
  133. }
  134.  
  135. /*=========================================== TApp =======================
  136.    AboutDialog - create modal About dialog box
  137. */
  138.  
  139. void TApp::AboutDialog() {
  140.  
  141.   // the About box reminds the user what is being demonstrated
  142.   TDialog *pd = new TDialog(TRect(0, 0, 35, 12), "About");
  143.   if (pd) {
  144.     pd->options |= ofCentered;
  145.     pd->insert(new TStaticText(TRect(1, 2, 34, 7),
  146.                "\003Turbo Vision Example\n\003\n"
  147.                "\003Creating a TListBox\n\003\n"
  148.                "\003Borland Technical Support"));
  149.     pd->insert(new TButton(TRect(3, 9, 32, 11), "~O~k", cmOK, bfDefault));
  150.         deskTop->execView(pd);
  151.   }
  152.   destroy(pd);
  153. }
  154.  
  155. /*=========================================== TApp =======================
  156.    ListDialog - display a dialog box containing a listbox
  157. */
  158.  
  159. void TApp::ListDialog() {
  160.  
  161.   // build a collection to pass to the listbox
  162.   TStringCollection *theCollection = new TStringCollection(100, 10);
  163.  
  164.   for (int i=0; i<numStrings; i++)
  165.     theCollection->insert(theList[i]);
  166.  
  167.   TDialog *pd = new TDialog(TRect(0, 0, 50, 20), "TV Classes");
  168.   pd->options |= ofCentered;
  169.  
  170.   // create a scroll bar for the list box
  171.   TScrollBar *listScroller = new TScrollBar(TRect(3, 17, 47, 18));
  172.  
  173.   // create a list box with 2 columns and associated scroller
  174.   TListBox *listBox = new TListBox(TRect(3, 2, 47, 17), 2, listScroller);
  175.  
  176.   // associate the collection with the list box
  177.   listBox->newList(theCollection);
  178.  
  179.   // insert the list box with its scroller into the dialog box
  180.   pd->insert(listBox);
  181.   pd->insert(listScroller);
  182.  
  183.   // if the object was safely built, exec it as modal
  184.   if(validView(pd)) deskTop->execView(pd);
  185.  
  186.   // and a little clean up
  187.   destroy(pd);
  188. }
  189.  
  190. //************************************************************************
  191. int main(void) {
  192.  
  193.   // now for the tough stuff
  194.   TApp myApp;
  195.   myApp.run();
  196.   return 0;
  197. }
  198.