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

  1. /* LISTBOX2.CPP: Deriving a new Dialog Box containing a TListBox
  2.  
  3.    This sample code demonstrates the use of class derivation to create
  4.    a new dialog box class which contains a Tlistbox. It is identical to
  5.    LISBOX1.CPP in functionality but is quite different in the method
  6.    used to realize that functionality. Note that the reponsibility for
  7.    for creating the listbox bas been moved into the derived dialog
  8.    box's constructor; only the code required to create, run and remove
  9.    the dialog box remains in TApp's ListDialog() function.
  10.  
  11.   ----------------------------------------------------------------------
  12.    The following example routines have been provided by the Technical
  13.    Support staff at Borland International.  They are provided as a
  14.    courtesy and not as part of a Borland product, and as such, are
  15.    provided without the assurance of technical support or any specific
  16.    guarantees.
  17. */
  18.  
  19. #define Uses_TApplication
  20. #define Uses_TBackground
  21. #define Uses_TButton
  22. #define Uses_TKeys
  23. #define Uses_TDeskTop
  24. #define Uses_TDialog
  25. #define Uses_TListBox
  26. #define Uses_TMenu
  27. #define Uses_TMenuBar
  28. #define Uses_TMenuItem
  29. #define Uses_TRect
  30. #define Uses_TScrollBar
  31. #define Uses_TStaticText
  32. #define Uses_TStatusDef
  33. #define Uses_TStatusItem
  34. #define Uses_TStatusLine
  35. #define Uses_TStringCollection
  36. #include <tv.h>
  37. #include <stdlib.h>
  38.  
  39. //  global data
  40. const cmAbout   = 100;  // User selected menu item 'About'
  41. const cmList    = 101;  // User selected menu item 'List'
  42.  
  43. // we'll use a list of the TV classes for something
  44. // to display in the list box
  45. char *theList[] = {
  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. // determine the number of items in the list
  86. int numStrings = sizeof(theList) / sizeof(theList[0]);
  87.  
  88.  
  89. //////////////////////////////////////////// TMyDialog ///////////////////
  90. class TMyDialog : public TDialog {
  91.     public:
  92.         TMyDialog(TRect &trect, char *title, char *sarray[], int ns);
  93.  
  94.     private:
  95.     TListBox *listBox;
  96.     TScrollBar *listScroller;
  97.     TStringCollection *theCollection;
  98. };
  99.  
  100. //==========================================[ MyDialogBox ]===============
  101. TMyDialog::TMyDialog(TRect &trect, char *title, char *sarray[], int ns)
  102.                     :TDialog(trect, title),
  103.                      TWindowInit(&TDialog::initFrame) {
  104.  
  105.   // build a collection to pass to the listbox
  106.   theCollection = new TStringCollection(100, 10);
  107.   for (int i=0; i<ns; i++) theCollection->insert(sarray[i]);
  108.  
  109.   options |= ofCentered;
  110.  
  111.   // Note that we have modified the sizing of the listbox to be
  112.   // relative to the size of the enclosing dialog box... this makes it
  113.   // possible to change the TRect arguments to the TMyDialog constructor
  114.   // without requiring a rewrite of the constructor. Note, however,
  115.   // that we do not do anything to constrain the dialog or list box
  116.   // coordinates to within valid desktop ranges... not very robust but
  117.   // maybe we'll do sonething about it in a later example.
  118.   int lbwidth = (trect.b.x - trect.a.x) - 3;
  119.   int lbhite = (trect.b.y - trect.a.y) - 2;
  120.  
  121.   // create a scroll bar for the list box
  122.   listScroller = new TScrollBar(TRect(trect.a.x+3, trect.a.y+lbhite,
  123.                                       trect.a.x+lbwidth, trect.a.y+lbhite+1));
  124.  
  125.   // create a list box with 2 columns and associated scroller
  126.   listBox = new TListBox(TRect(trect.a.x+3, trect.a.y+2,
  127.                                trect.a.x+lbwidth, trect.a.y+lbhite),
  128.                                2, listScroller);
  129.  
  130.   // associate the collection with the list box
  131.   listBox->newList(theCollection);
  132.  
  133.   // insert the list box with its scroller into the dialog box
  134.   insert(listBox);
  135.   insert(listScroller);
  136. } // end of MyDialogBox::MyDialogBox()
  137.  
  138.  
  139. //////////////////////////////////////////// TApp ////////////////////////
  140. class TApp : public TApplication {
  141.   public:
  142.     TApp();
  143.  
  144.     // virtual functions to be locally redefined
  145.     static TMenuBar *initMenuBar(TRect r);
  146.     void handleEvent(TEvent &event);
  147.  
  148.     // declare new functions
  149.     void AboutDialog();
  150.     void ListDialog();
  151. };
  152.  
  153. //==========================================[ TApp ]======================
  154. TApp::TApp() : TProgInit(&TApplication::initStatusLine,
  155.                          &TApp::initMenuBar,
  156.                          &TApplication::initDeskTop) {
  157. }
  158.  
  159. //=========================================== TApp =======================
  160. TMenuBar *TApp::initMenuBar(TRect r) {
  161.  
  162.   r.b.y = r.a.y + 1;
  163.   return(new TMenuBar(r, new TMenu(
  164.     *new TMenuItem("~A~bout", cmAbout, kbAltA, hcNoContext, 0,
  165.       new TMenuItem("~L~ist", cmList, kbAltL, hcNoContext, 0)))));
  166. }
  167.  
  168. //=========================================== TApp =======================
  169. void TApp::handleEvent(TEvent &event) {
  170.  
  171.   TApplication::handleEvent(event);
  172.   if (event.what == evCommand) {
  173.     switch(event.message.command) {
  174.       case cmAbout:                     // display the about box
  175.         AboutDialog();
  176.         clearEvent(event);
  177.         break;
  178.       case cmList:                      // display our list box
  179.         ListDialog();
  180.         clearEvent(event);
  181.         break;
  182.     }
  183.   }
  184. }
  185.  
  186. /*=========================================== TApp =======================
  187.    AboutDialog - create modal About dialog box
  188. */
  189.  
  190. void TApp::AboutDialog() {
  191.  
  192.   // the About box reminds the user what is being demonstrated
  193.   TDialog *pd = new TDialog(TRect(0, 0, 35, 12), "About");
  194.   if (pd) {
  195.     pd->options |= ofCentered;
  196.     pd->insert(new TStaticText(TRect(1, 2, 34, 7),
  197.                "\003Turbo Vision Example\n\003\n"
  198.                "\003Deriving a Dialog Box class\n\003\n"
  199.                "\003Borland Technical Support"));
  200.     pd->insert(new TButton(TRect(3, 9, 32, 11), "~O~k", cmOK, bfDefault));
  201.         deskTop->execView(pd);
  202.   }
  203.   destroy(pd);
  204. }
  205.  
  206. /*=========================================== TApp =======================
  207.    ListDialog - display a dialog box containing a listbox
  208. */
  209.  
  210. void TApp::ListDialog() {
  211.  
  212.   TMyDialog *pd = new TMyDialog(TRect(0, 0, 60, 15),
  213.                                 "TV Classes", theList, numStrings);
  214.  
  215.   // if the object was safely built, exec it as modal
  216.   if (validView(pd)) deskTop->execView(pd);
  217.  
  218.   // and a little clean up
  219.   destroy(pd);
  220. }
  221.  
  222. //************************************************************************
  223. int main(void) {
  224.  
  225.   // now for the tough stuff
  226.   TApp myApp;
  227.   myApp.run();
  228.   return 0;
  229. }
  230.