home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / lbox / listbox4.cpp < prev   
Encoding:
C/C++ Source or Header  |  1992-04-16  |  10.8 KB  |  367 lines

  1. /* LISTBOX4.CPP:  Selecting a list box item
  2.  
  3.    This sample code (modified from LISTBOX3.CPP) adds a case element
  4.    to the TMyDialog class to recognize and act upon a double click on
  5.    a focused list box item.
  6.  
  7.    Contrary to what the Turbo Vision 1.0 manual says, a TListBox object
  8.    does not broadcast a cmListItemSelected message to the owner.  To
  9.    overcome this fact we create a derivitive list box (inheritance saves
  10.    the day once again) and supply the necessary cmListItemSelected
  11.    broadcast ourselves.
  12.  
  13.   ----------------------------------------------------------------------
  14.    The following example routines have been provided by the Technical
  15.    Support staff at Borland International.  They are provided as a
  16.    courtesy and not as part of a Borland product, and as such, are
  17.    provided without the assurance of technical support or any specific
  18.    guarantees.
  19. */
  20.  
  21. #include <string.h>             // strlen()
  22. #include <strstream.h>          // ostrstream()
  23.  
  24. #define Uses_TApplication
  25. #define Uses_TBackground
  26. #define Uses_TButton
  27. #define Uses_TKeys
  28. #define Uses_TDeskTop
  29. #define Uses_TDialog
  30. #define Uses_TListBox
  31. #define Uses_TMenu
  32. #define Uses_TMenuBar
  33. #define Uses_TMenuItem
  34. #define Uses_TRect
  35. #define Uses_TInputLine
  36. #define Uses_TScrollBar
  37. #define Uses_TStaticText
  38. #define Uses_TStatusDef
  39. #define Uses_TStatusItem
  40. #define Uses_TStatusLine
  41. #define Uses_TStringCollection
  42. #define Uses_MsgBox
  43. #include <tv.h>
  44. #include <stdlib.h>
  45.  
  46. // A NULL terminated list of strings
  47. char *theList[] = { "Ecologic Envoy, The",
  48.                     "Squares of the City, The",
  49.                     "Worthing Saga, The",
  50.                     "Arachne", NULL };
  51.  
  52. const cmAbout   = 100;  // User selected menu item 'About'
  53. const cmList    = 101;  // User selected menu item 'List'
  54.  
  55. int ifWidth     = 35;   // input line width
  56.  
  57. //////////////////////////////////////////// TMyListBox //////////////////
  58. class TMyListBox : public TListBox {
  59.     public:
  60.     TMyListBox(TRect& bounds, ushort num, TScrollBar *bar)
  61.                :TListBox(bounds, num, bar) {};
  62.     void handleEvent(TEvent&);
  63.     protected:
  64.     private:
  65. };
  66.  
  67.  
  68. /*========================================== TMyListBox ==================
  69.    handleEvent - because the TListBox class isn't broadcasting item
  70.                  selection, we supply the necessary functionality in our
  71.                  derivitive list box event handler.
  72.  
  73.      REQUIRES:
  74.      RETURNS:
  75. */
  76.  
  77. void TMyListBox::handleEvent(TEvent& event) {
  78.  
  79.   if ((event.what == evMouseDown) && (event.mouse.doubleClick)) {
  80.     message(owner, evBroadcast, cmListItemSelected, this);
  81.     clearEvent(event);
  82.   }
  83.  
  84.   TListBox::handleEvent(event);
  85. } // end of TMyListBox::handleEvent()
  86.  
  87.  
  88. //////////////////////////////////////////// TMyDialog ///////////////////
  89. class TMyDialog : public TDialog {
  90.     public:
  91.         TMyDialog(TRect &trect, int cols, char *title, char *sarray[]);
  92.     void handleEvent(TEvent &);
  93.     void deleteString();
  94.     void addString();
  95.  
  96.   protected:
  97.     private:
  98.     TInputLine *inputLine;
  99.     TMyListBox *listBox;                // Use our list box
  100.     TScrollBar *listScroller;
  101.     TStringCollection *theCollection;
  102. };
  103.  
  104.  
  105. //==========================================[ MyDialogBox ]===============
  106. TMyDialog::TMyDialog(TRect &trect, int cols, char *title, char *sarray[])
  107.                     :TDialog(trect, title),
  108.                      TWindowInit(&TDialog::initFrame) {
  109.  
  110.   // Must have at least one column!
  111.   if (!cols) cols = 1;
  112.  
  113.   // Build a collection to pass to the listbox.
  114.   theCollection = new TStringCollection(100, 10);
  115.   for (int i=0; sarray[i]; i++)
  116.     theCollection->insert(sarray[i]);
  117.  
  118.   options |= ofCentered;
  119.  
  120.   int lbwidth = (trect.b.x - trect.a.x) - 3;
  121.   int lbhite = (trect.b.y - trect.a.y) - 2;
  122.  
  123.   // If more than 1 column requested, create a horizontal scroll bar
  124.   // else create a vertical scroll bar for the list box.
  125.   if (cols > 1)
  126.     listScroller = new TScrollBar(TRect(trect.a.x+3, trect.a.y+lbhite,
  127.                                   trect.a.x+lbwidth, trect.a.y+lbhite+1));
  128.   else
  129.     listScroller = new TScrollBar(TRect(trect.a.x+lbwidth, trect.a.y+3,
  130.                                   trect.a.x+lbwidth+1, trect.a.y+lbhite));
  131.  
  132.   // Create a list box with 'cols' columns and an associated scroller.
  133.   // NOTE:  we are now using our derivitive list box
  134.   listBox = new TMyListBox(TRect(trect.a.x+3, trect.a.y+3,
  135.                                  trect.a.x+lbwidth, trect.a.y+lbhite),
  136.                                  cols, listScroller);
  137.  
  138.   // Associate the collection with the list box.
  139.   listBox->newList(theCollection);
  140.  
  141.   // Create input line.
  142.   inputLine = new TInputLine(TRect(trect.a.x+3, trect.a.y+2,
  143.                                    trect.a.x+lbwidth+1, trect.a.y+3),
  144.                                    ifWidth);
  145.  
  146.   // Insert list box, its scroller and the input line into dialog box.
  147.   insert(listBox);
  148.   insert(listScroller);
  149.   insert(inputLine);
  150. } // end of MyDialogBox::MyDialogBox()
  151.  
  152.  
  153. /*========================================== MyDialogBox =================
  154.      handleEvent -
  155. */
  156.  
  157. void TMyDialog::handleEvent(TEvent &event) {
  158.  
  159.     switch(event.what) {
  160.     case evKeyDown:
  161.           switch(event.keyDown.keyCode) {
  162.  
  163.         // [Enter] with input line focused puts string in list.
  164.               case kbEnter:
  165.           if (inputLine->state&sfSelected) {
  166.                   addString();
  167.                   clearEvent(event);
  168.           }
  169.                 break;
  170.  
  171.         // [Delete] deletes focused data
  172.               case kbDel:
  173.           if (listBox->state&sfSelected && theCollection->getCount()) {
  174.                     deleteString();
  175.                     clearEvent(event);
  176.           }
  177.                   break;
  178.  
  179.         // Down arrow while in line input editor moves into listbox.
  180.               case kbDown:
  181.           if (inputLine->state&sfSelected) {
  182.             listBox->focusItem(0);
  183.             event.keyDown.keyCode = kbTab;
  184.           }
  185.                   break;
  186.  
  187.         // Up arrow when top entry of listbox has focus moves into
  188.         // line input editor.
  189.               case kbUp:
  190.           if (listBox->focused == 0 && listBox->state&sfSelected)
  191.             event.keyDown.keyCode = kbTab;
  192.                   break;
  193.       }
  194.  
  195.       case evBroadcast:
  196.         switch (event.message.command) {
  197.  
  198.           // Handle user selected list box item.
  199.           case cmListItemSelected:
  200.             char selItem[81];
  201.             char message[201];
  202.  
  203.             // Get the item string,...
  204.             listBox->getText(selItem, listBox->focused, 80);
  205.  
  206.             // ...compose a message using it to...
  207.             ostrstream(message, 200) << "\003The item selected was "
  208.                                      << selItem << ends;
  209.  
  210.             // ...display in a message box then clear this event.
  211.             messageBox(message, mfOKButton|mfInformation);
  212.             clearEvent(event);
  213.         }
  214.   }
  215.  
  216.     // Let TDialog handler do it's thing with any remaining events.
  217.     TDialog::handleEvent(event);
  218.     clearEvent(event);                                // Dood it so dump command.
  219. } // end of MyDialogBox::eventHandler()
  220.  
  221.  
  222. /*========================================== TMyDialog ===================
  223.      deleteString - Pressing [Delete] while a list box item is focused
  224.                   deletes that item from the list.
  225. */
  226.  
  227. void TMyDialog::deleteString() {
  228.  
  229.   // If there are any items in collection...
  230.     if (theCollection->getCount()) {
  231.  
  232.     // delete the currently focused item.
  233.         theCollection->atFree(listBox->focused);
  234.         listBox->setRange(theCollection->getCount());
  235.         listBox->drawView();
  236.     }
  237. }
  238.  
  239.  
  240. /*========================================== TMyDialog ===================
  241.      addString - Pressing Enter with the input line focused adds the
  242.                string (if any) on the input line to the list box. The
  243.                input line is then cleared.
  244. */
  245.  
  246. void TMyDialog::addString() {
  247.  
  248.     char *temp = new char[ifWidth+1];
  249.     inputLine->getData(temp);
  250.  
  251.   // If there's anything to transfer to theCollection...
  252.     if (strlen(temp)) {
  253.  
  254.     // ...transfer it.
  255.         theCollection->insert(temp);
  256.         listBox->setRange(theCollection->getCount());
  257.         listBox->drawView();
  258.  
  259.     // Clear input line buffer, display & re-select.
  260.         *inputLine->data='\0';
  261.     inputLine->draw();
  262.         inputLine->selectAll(True);
  263.     }
  264.   else
  265.  
  266.     // If nothing to insert, throw away the allocation.
  267.     delete temp;
  268. } // end of TMyDialog::addString()
  269.  
  270.  
  271. //////////////////////////////////////////// TApp ////////////////////////
  272. class TApp : public TApplication {
  273.   public:
  274.     TApp();
  275.     static TMenuBar *initMenuBar(TRect r);
  276.     void handleEvent(TEvent &event);
  277.  
  278.     void AboutDialog();
  279.     void ListDialog();
  280.  
  281.   protected:
  282.   private:
  283. };
  284.  
  285.  
  286. //==========================================[ TApp ]======================
  287. TApp::TApp() : TProgInit(&TApplication::initStatusLine,
  288.                          &TApp::initMenuBar,
  289.                          &TApplication::initDeskTop) {
  290. }
  291.  
  292.  
  293. //=========================================== TApp =======================
  294. TMenuBar *TApp::initMenuBar(TRect r) {
  295.  
  296.   r.b.y = r.a.y + 1;
  297.   return(new TMenuBar(r, new TMenu(
  298.     *new TMenuItem("~A~bout", cmAbout, kbAltA, hcNoContext, 0,
  299.       new TMenuItem("~L~ist", cmList, kbAltL, hcNoContext, 0)))));
  300. }
  301.  
  302.  
  303. //=========================================== TApp =======================
  304. void TApp::handleEvent(TEvent &event) {
  305.  
  306.   TApplication::handleEvent(event);
  307.   if (event.what == evCommand) {
  308.     switch(event.message.command) {
  309.       case cmAbout:                     // display the about box
  310.         AboutDialog();
  311.         clearEvent(event);
  312.         break;
  313.       case cmList:                      // display our list box
  314.         ListDialog();
  315.         clearEvent(event);
  316.         break;
  317.     }
  318.   }
  319. }
  320.  
  321.  
  322. /*=========================================== TApp =======================
  323.    AboutDialog - create modal About dialog box
  324. */
  325.  
  326. void TApp::AboutDialog() {
  327.  
  328.   // the About box reminds the user what is being demonstrated
  329.   TDialog *pd = new TDialog(TRect(0, 0, 35, 12), "About");
  330.   if (pd) {
  331.     pd->options |= ofCentered;
  332.     pd->insert(new TStaticText(TRect(1, 2, 34, 7),
  333.                "\003Turbo Vision Example\n\003\n"
  334.                "\003Selecting List Box item\n\003\n"
  335.                "\003Borland Technical Support"));
  336.     pd->insert(new TButton(TRect(3, 9, 32, 11), "~O~k", cmOK, bfDefault));
  337.         deskTop->execView(pd);
  338.   }
  339.   destroy(pd);
  340. }
  341.  
  342.  
  343. /*=========================================== TApp =======================
  344.    ListDialog - display a dialog box containing a listbox
  345. */
  346.  
  347. void TApp::ListDialog() {
  348.  
  349.   TMyDialog *pd = new TMyDialog(TRect(0, 0, 35, 15),
  350.                                 1, "SF List", theList);
  351.  
  352.   // if the object was safely built, exec it as modal
  353.   if (validView(pd)) deskTop->execView(pd);
  354.  
  355.   // and a little clean up
  356.   destroy(pd);
  357. }
  358.  
  359. //************************************************************************
  360. int main(void) {
  361.  
  362.   // Your standard Turbo Vision main().
  363.   TApp myApp;
  364.   myApp.run();
  365.   return 0;
  366. }
  367.