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

  1. /* LISTBOX3.CPP:  Interactive update of a list box
  2.  
  3.    This sample code derives a Dialog box containing a TListBox and a
  4.    TInputLine and has an eventHandler() that demonstrates a method for
  5.    overriding and/or supplementing the standard interface behavior of
  6.    a Turbo Vision application:
  7.  
  8.       - Pressing [Enter] with the input line focused puts the string
  9.         in the input line (if any) into the list
  10.  
  11.       - Pressing [downarrow] with the input line focused moves to the
  12.         first (upper-most) item in the list box and gives it focus.
  13.  
  14.       - Pressing [uparrow] with the first (upper-most) list box item
  15.         focused moves focus to the input line.
  16.  
  17.       - Pressing [Del]ete with a list box item focused deletes the
  18.         focused list box item.
  19.  
  20.    All other control behavior is the behavior defined for a standard
  21.    Turbo Vision application.
  22.  
  23.   ----------------------------------------------------------------------
  24.    The following example routines have been provided by the Technical
  25.    Support staff at Borland International.  They are provided as a
  26.    courtesy and not as part of a Borland product, and as such, are
  27.    provided without the assurance of technical support or any specific
  28.    guarantees.
  29. */
  30.  
  31. #include <string.h>             // strlen()
  32.  
  33. #define Uses_TApplication
  34. #define Uses_TBackground
  35. #define Uses_TButton
  36. #define Uses_TKeys
  37. #define Uses_TDeskTop
  38. #define Uses_TDialog
  39. #define Uses_TListBox
  40. #define Uses_TMenu
  41. #define Uses_TMenuBar
  42. #define Uses_TMenuItem
  43. #define Uses_TRect
  44. #define Uses_TInputLine
  45. #define Uses_TScrollBar
  46. #define Uses_TStaticText
  47. #define Uses_TStatusDef
  48. #define Uses_TStatusItem
  49. #define Uses_TStatusLine
  50. #define Uses_TStringCollection
  51. #include <tv.h>
  52. #include <stdlib.h>
  53.  
  54. // A NULL terminated list of strings is now required by TMyDialog
  55. char *theList[] = { "Ecologic Envoy, The",
  56.                     "Squares of the City, The",
  57.                     "Worthing Saga, The",
  58.                     "Arachne", NULL };
  59.  
  60. const cmAbout   = 100;  // User selected menu item 'About'
  61. const cmList    = 101;  // User selected menu item 'List'
  62.  
  63. int ifWidth     = 35;   // input line width
  64.  
  65. //////////////////////////////////////////// TMyDialog ///////////////////
  66. class TMyDialog : public TDialog {
  67.     public:
  68.         TMyDialog(TRect &trect, int cols, char *title, char *sarray[]);
  69.     void handleEvent(TEvent &);
  70.     void deleteString();
  71.     void addString();
  72.  
  73.     private:
  74.     TInputLine *inputLine;
  75.     TListBox *listBox;
  76.     TScrollBar *listScroller;
  77.     TStringCollection *theCollection;
  78. };
  79.  
  80. //==========================================[ MyDialogBox ]===============
  81. TMyDialog::TMyDialog(TRect &trect, int cols, char *title, char *sarray[])
  82.                     :TDialog(trect, title),
  83.                      TWindowInit(&TDialog::initFrame) {
  84.  
  85.   // Must have at least one column!
  86.   if (!cols) cols = 1;
  87.  
  88.   // Build a collection to pass to the listbox. Note that in this
  89.   // example our constructor expects a NULL terminated array. This
  90.   // change allowed us to eliminate the need to pass the number of
  91.   // elements in the array to the constructor as was required in the
  92.   // previous list box examples.
  93.   theCollection = new TStringCollection(100, 10);
  94.   for (int i=0; sarray[i]; i++)
  95.     theCollection->insert(sarray[i]);
  96.  
  97.   options |= ofCentered;
  98.  
  99.   int lbwidth = (trect.b.x - trect.a.x) - 3;
  100.   int lbhite = (trect.b.y - trect.a.y) - 2;
  101.  
  102.   // If more than 1 column requested, create a horizontal scroll bar
  103.   // else create a vertical scroll bar for the list box. This change
  104.   // permits us a reasonably natural way to use TMyDialog to create
  105.   // dialog boxes with either a horizontal or vertical scroll bar
  106.   // without editing the constructor.
  107.   if (cols > 1)
  108.     listScroller = new TScrollBar(TRect(trect.a.x+3, trect.a.y+lbhite,
  109.                                   trect.a.x+lbwidth, trect.a.y+lbhite+1));
  110.   else
  111.     listScroller = new TScrollBar(TRect(trect.a.x+lbwidth, trect.a.y+3,
  112.                                   trect.a.x+lbwidth+1, trect.a.y+lbhite));
  113.  
  114.   // Create a list box with 'cols' columns and an associated scroller.
  115.   listBox = new TListBox(TRect(trect.a.x+3, trect.a.y+3,
  116.                                trect.a.x+lbwidth, trect.a.y+lbhite),
  117.                                cols, listScroller);
  118.  
  119.   // Associate the collection with the list box.
  120.   listBox->newList(theCollection);
  121.  
  122.   // Create input line.
  123.   inputLine = new TInputLine(TRect(trect.a.x+3, trect.a.y+2,
  124.                                    trect.a.x+lbwidth+1, trect.a.y+3),
  125.                                    ifWidth);
  126.  
  127.   // Insert list box, its scroller and the input line into dialog box.
  128.   insert(listBox);
  129.   insert(listScroller);
  130.   insert(inputLine);
  131. } // end of MyDialogBox::MyDialogBox()
  132.  
  133.  
  134. /*========================================== MyDialogBox =================
  135.      handleEvent -
  136. */
  137.  
  138. void TMyDialog::handleEvent(TEvent &event) {
  139.  
  140.     switch(event.what) {
  141.     case evKeyDown:
  142.           switch(event.keyDown.keyCode) {
  143.  
  144.         // Enter with input line focused, put string in list.
  145.               case kbEnter:
  146.           if (inputLine->state&sfSelected) {
  147.                   addString();
  148.                   clearEvent(event);
  149.           }
  150.                 break;
  151.  
  152.               case kbDel:                         // Delete key deletes focused data
  153.           if (listBox->state&sfSelected && theCollection->getCount()) {
  154.                     deleteString();
  155.                     clearEvent(event);
  156.           }
  157.                   break;
  158.  
  159.         // Down arrow while in line input editor moves into listbox.
  160.               case kbDown:
  161.           if (inputLine->state&sfSelected) {
  162.             listBox->focusItem(0);
  163.             event.keyDown.keyCode = kbTab;
  164.           }
  165.                   break;
  166.  
  167.         // Up arrow when top entry of listbox has focus moves into
  168.         // line input editor.
  169.               case kbUp:
  170.           if (listBox->focused == 0 && listBox->state&sfSelected)
  171.             event.keyDown.keyCode = kbTab;
  172.                   break;
  173.       }
  174.     }
  175.  
  176.     // Let TDialog handler do it's thing with any remaining events.
  177.     TDialog::handleEvent(event);
  178.     clearEvent(event);                                // Dood it so dump command.
  179. } // end of MyDialogBox::eventHandler()
  180.  
  181. /*========================================== TMyDialog ===================
  182.      deleteString - Pressing [Del]ete while a list box item is focused
  183.                   deletes that item from the list.
  184. */
  185.  
  186. void TMyDialog::deleteString() {
  187.  
  188.   // If there are any items in collection...
  189.     if (theCollection->getCount()) {
  190.  
  191.     // delete the currently focused item.
  192.         theCollection->atFree(listBox->focused);
  193.         listBox->setRange(theCollection->getCount());
  194.         listBox->drawView();
  195.     }
  196. }
  197.  
  198. /*========================================== TMyDialog ===================
  199.      addString - Pressing Enter with the input line focused adds the
  200.                string (if any) in the input box to the list box. The
  201.                input line is then cleared.
  202. */
  203.  
  204. void TMyDialog::addString() {
  205.  
  206.     char *temp = new char[ifWidth+1];
  207.     inputLine->getData(temp);
  208.  
  209.   // If there's anything to transfer to theCollection...
  210.     if (strlen(temp)) {
  211.  
  212.     // ...transfer it.
  213.         theCollection->insert(temp);
  214.         listBox->setRange(theCollection->getCount());
  215.         listBox->drawView();
  216.  
  217.     // Clear input line buffer, display & re-select.
  218.         *inputLine->data='\0';
  219.     inputLine->draw();
  220.         inputLine->selectAll(True);
  221.     }
  222.   else
  223.     delete temp;
  224. } // end of TMyDialog::addString()
  225.  
  226.  
  227. //////////////////////////////////////////// TApp ////////////////////////
  228. class TApp : public TApplication {
  229.   public:
  230.     TApp();
  231.  
  232.     // virtual functions to be locally redefined
  233.     static TMenuBar *initMenuBar(TRect r);
  234.     void handleEvent(TEvent &event);
  235.  
  236.     // declare new functions
  237.     void AboutDialog();
  238.     void ListDialog();
  239. };
  240.  
  241. //==========================================[ TApp ]======================
  242. TApp::TApp() : TProgInit(&TApplication::initStatusLine,
  243.                          &TApp::initMenuBar,
  244.                          &TApplication::initDeskTop) {
  245. }
  246.  
  247. //=========================================== TApp =======================
  248. TMenuBar *TApp::initMenuBar(TRect r) {
  249.  
  250.   r.b.y = r.a.y + 1;
  251.   return(new TMenuBar(r, new TMenu(
  252.     *new TMenuItem("~A~bout", cmAbout, kbAltA, hcNoContext, 0,
  253.       new TMenuItem("~L~ist", cmList, kbAltL, hcNoContext, 0)))));
  254. }
  255.  
  256. //=========================================== TApp =======================
  257. void TApp::handleEvent(TEvent &event) {
  258.  
  259.   TApplication::handleEvent(event);
  260.   if (event.what == evCommand) {
  261.     switch(event.message.command) {
  262.       case cmAbout:                     // display the about box
  263.         AboutDialog();
  264.         clearEvent(event);
  265.         break;
  266.       case cmList:                      // display our list box
  267.         ListDialog();
  268.         clearEvent(event);
  269.         break;
  270.     }
  271.   }
  272. }
  273.  
  274. /*=========================================== TApp =======================
  275.    AboutDialog - create modal About dialog box
  276. */
  277.  
  278. void TApp::AboutDialog() {
  279.  
  280.   // the About box reminds the user what is being demonstrated
  281.   TDialog *pd = new TDialog(TRect(0, 0, 35, 12), "About");
  282.   if (pd) {
  283.     pd->options |= ofCentered;
  284.     pd->insert(new TStaticText(TRect(1, 2, 34, 7),
  285.                "\003Turbo Vision Example\n\003\n"
  286.                "\003Interactive TListBox update\n\003\n"
  287.                "\003Borland Technical Support"));
  288.     pd->insert(new TButton(TRect(3, 9, 32, 11), "~O~k", cmOK, bfDefault));
  289.         deskTop->execView(pd);
  290.   }
  291.   destroy(pd);
  292. }
  293.  
  294. /*=========================================== TApp =======================
  295.    ListDialog - display a dialog box containing a listbox
  296. */
  297.  
  298. void TApp::ListDialog() {
  299.  
  300.   TMyDialog *pd = new TMyDialog(TRect(0, 0, 35, 15),
  301.                                 1, "SF List", theList);
  302.  
  303.   // if the object was safely built, exec it as modal
  304.   if (validView(pd)) deskTop->execView(pd);
  305.  
  306.   // and a little clean up
  307.   destroy(pd);
  308. }
  309.  
  310. //************************************************************************
  311. int main(void) {
  312.  
  313.   // Your standard Turbo Vision main().
  314.   TApp myApp;
  315.   myApp.run();
  316.   return 0;
  317. }
  318.