home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / tstrin / tvinput.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-17  |  6.4 KB  |  256 lines

  1. /*
  2.    TVINPUT.CPP
  3.    (C) Copyright 1992 Michael Joseph Newton
  4.    All rights reserved.
  5.  
  6.  
  7.       This file is only used to demonstrate the use of the
  8.    TStringInputLine class.
  9.  
  10.       The TStringInputLine class is derived from Turbo Vision's
  11.    TInputLine class. TStringInputLine is declared in TSTRINP.HPP,
  12.    and defined in TSTRINP.CPP.
  13.  
  14.       This file is hereby released by the author to the public
  15.    domain. Use it as you will.
  16.  
  17.    Author:      Michael Joseph Newton
  18.    Date:        01/17/92
  19.    Address:     17874 Marygold Ave. #32
  20.                 Bloomington, CA 92316
  21.    Phone:       (714) 877-4655
  22.    CompuServe:  70402,531 (as Michael J. Newton)
  23.    RelayNet:    ->ECTECH (as Mick Newton)
  24.  
  25.    All questions or comments will be greatly appreciated!
  26. */
  27.  
  28. #define Uses_TApplication
  29. #define Uses_TButton
  30. #define Uses_TDeskTop
  31. #define Uses_TDialog
  32. #define Uses_TEvent
  33. #define Uses_THistory
  34. #define Uses_TKeys
  35. #define Uses_TMenuBar
  36. #define Uses_TMenuItem
  37. #define Uses_TRect
  38. #define Uses_TStaticText
  39. #define Uses_TSubMenu
  40. #define Uses_TStatusLine
  41. #define Uses_TStatusItem
  42. #define Uses_TStatusDef
  43. #define Uses_MsgBox
  44.  
  45. #include <tv.h>
  46.  
  47. __link(RButton)
  48. __link(RFrame)
  49. __link(RHistory)
  50. __link(RInputLine)
  51. __link(RLabel)
  52. __link(RMenuBar)
  53. __link(RResourceCollection)
  54.  
  55. #if !defined __STRING_H
  56. #include  <string.h>
  57. #endif
  58.  
  59. #if !defined __STDLIB_H
  60. #include <stdlib.h>
  61. #endif
  62.  
  63. #if !defined __STRSTREA_H
  64. #include <strstrea.h>
  65. #endif
  66.  
  67. #if !defined __TSTRINP_HPP
  68. #include "tstrinp.hpp"
  69. #endif
  70.  
  71. // Define cmXXX commands
  72. const int cmRunTest = 1000,
  73.           cmCase    = 1001,
  74.           cmMand    = 1002,
  75.           cmAbout   = 1003;
  76.  
  77. // Define THistory ID for TStringInputLine
  78. const ushort hlStrInpLine = 100;
  79.  
  80.  
  81. // Application class
  82. class TVInput : public TApplication
  83. {
  84.    public:
  85.       TVInput();
  86.    private:
  87.       // Usual stuff
  88.       void handleEvent(TEvent& Event);
  89.       static TMenuBar *initMenuBar(TRect r);
  90.       static TStatusLine *initStatusLine(TRect r);
  91.       void testDialog();          // Tests TStringInputLine
  92.       void toggleCase();          // Toggles input case
  93.       void toggleMand();          // Toggles mandatory input
  94.       char string[81];            // String buffer for test
  95.       Boolean caseToggle;         // True = uppercase input
  96.       Boolean mandToggle;         // True = input mandatory
  97. };
  98.  
  99.  
  100. // Constructor
  101. TVInput::TVInput() :
  102.          TApplication(),
  103.          TProgInit(initStatusLine, initMenuBar, initDeskTop)
  104. {
  105.  
  106.    memset(string, '\0', 81);      // Clear the string
  107.    caseToggle = True;             // Start with uppercase input...
  108.    mandToggle = True;             // and input mandatory
  109. }
  110.  
  111.  
  112. void TVInput::handleEvent(TEvent& event)
  113. {
  114.    TApplication::handleEvent(event);
  115.  
  116.    if(event.what == evCommand)
  117.       {
  118.       switch(event.message.command)
  119.          {
  120.          case cmRunTest:
  121.             testDialog();
  122.             break;
  123.          case cmCase:
  124.             toggleCase();
  125.             break;
  126.          case cmMand:
  127.             toggleMand();
  128.             break;
  129.          case cmAbout:
  130.             char msg[] = "\003TStringInputLine\n\n"
  131.                          "\003by Michael Joseph Newton\n"
  132.                          "\003CompuServe 70402,531";
  133.             messageBox(msg, mfInformation | mfOKButton);
  134.             break;
  135.          default:
  136.             return;
  137.          }
  138.       clearEvent(event);
  139.       }
  140. }
  141.  
  142.  
  143. TMenuBar *TVInput::initMenuBar(TRect r)
  144. {
  145.    r.b.y = r.a.y + 1;
  146.  
  147.    return new TMenuBar(r,
  148.       *new TSubMenu("~\360~", kbAltSpace, hcNoContext) +
  149.       *new TMenuItem("~A~bout", cmAbout, hcNoContext) +
  150.       *new TSubMenu("~T~est", hcNoContext) +
  151.       *new TMenuItem("T~S~tringInputLine", cmRunTest, hcNoContext) +
  152.       *new TMenuItem("E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X") +
  153.       *new TSubMenu("~T~oggles", hcNoContext) +
  154.       *new TMenuItem("Toggle ~i~nput case", cmCase, hcNoContext) +
  155.       *new TMenuItem("Toggle ~m~andatory input", cmMand, hcNoContext));
  156. }
  157.  
  158.  
  159. TStatusLine *TVInput::initStatusLine(TRect r)
  160. {
  161.    r.a.y = r.b.y - 1;
  162.  
  163.    return new TStatusLine( r,
  164.       *new TStatusDef(0, 0xFFFF) +
  165.       *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  166.       *new TStatusItem("", kbF10, cmMenu));
  167. }
  168.  
  169.  
  170. void TVInput::testDialog()
  171. // This is the function that actually tests TStringInputLine
  172. {
  173.    TDialog *d = new TDialog(TRect(0, 0, 50, 8), "Test TStringInputLine");
  174.  
  175.    // Add a prompt to the dialog
  176.    char msg[] = "Enter a string";
  177.    TStaticText *t = new TStaticText(TRect(0, 2, 15, 3), msg);
  178.    t->options |= ofCenterX;
  179.    d->insert(t);
  180.  
  181.    // Create a new TStringInputLine. Max input length = 50
  182.    TStringInputLine *i = new TStringInputLine(TRect(3, 3, 44, 4), 51);
  183.  
  184.    // NOTE: This is a TStringInputLine function, not ios::setf
  185.    // Set 'input mandatory' bit if mandToggle is True
  186.    if(mandToggle)
  187.       i->setf(ofMandInput);
  188.  
  189.    // Set input case bit according to caseToggle
  190.    if(caseToggle)
  191.       i->setf(ofUpperCase, caseField);
  192.    else
  193.       i->setf(ofLowerCase, caseField);
  194.  
  195.    // Insert it in the dialog
  196.    d->insert(i);
  197.  
  198.    // Add a history list
  199.    // NOTE: When you access the history list in the program, there
  200.    //       will only be one copy of the string in either upper or
  201.    //       lower case.
  202.    d->insert(new THistory(TRect(45, 3, 48, 4), i, hlStrInpLine));
  203.  
  204.    // Add some buttons
  205.    TButton *b;
  206.    b = new TButton(TRect(2, 5, 22, 7), "O~K~", cmOK, bfDefault);
  207.    d->insert(b);
  208.    b = new TButton(TRect(28, 5, 48, 7), "~C~ancel", cmCancel, bfNormal);
  209.    d->insert(b);
  210.  
  211.    d->options |= ofCentered;
  212.    d->selectNext(False);
  213.  
  214.    TView *p = TProgram::application->validView(d);
  215.  
  216.    if(p != 0)
  217.       {
  218.       p->setData(&string);
  219.       ushort result = TProgram::deskTop->execView(p);
  220.       if(result != cmCancel)
  221.          {
  222.          // Display the user's string
  223.          p->getData(&string);
  224.          char buff[256];
  225.          ostrstream os(buff, sizeof buff);
  226.          os << "\003The string you entered :\n"
  227.             << string
  228.             << ends;
  229.          messageBox(os.str(), mfInformation | mfOKButton);
  230.          destroy(p);
  231.          }
  232.       }
  233. }
  234.  
  235.  
  236. void TVInput::toggleCase()
  237. // Toggles input case flag
  238. {
  239.    caseToggle = (Boolean) !caseToggle;
  240. }
  241.  
  242.  
  243. void TVInput::toggleMand()
  244. // Toggles input mandatory flag
  245. {
  246.    mandToggle = (Boolean) !mandToggle;
  247. }
  248.  
  249.  
  250. int main()
  251. {
  252.    TVInput program;
  253.    program.run();
  254.    return 0;
  255. }
  256.