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

  1. /*
  2.    TSTRINP.CPP
  3.    (C) Copyright 1992 Michael Joseph Newton
  4.    All rights reserved.
  5.  
  6.    Definitions for TStringInputLine class.
  7.  
  8.       The TStringInputLine class is derived from Turbo Vision's
  9.    TInputLine class. TStringInputLine is declared in TSTRINP.HPP,
  10.    and defined in TSTRINP.CPP.
  11.  
  12.       This file is hereby released by the author to the public
  13.    domain. Use it as you will.
  14.  
  15.    Author:      Michael Joseph Newton
  16.    Date:        01/17/92
  17.    Address:     17874 Marygold Ave. #32
  18.                 Bloomington, CA 92316
  19.    Phone:       (714) 877-4655
  20.    CompuServe:  70402,531 (as Michael J. Newton)
  21.    RelayNet:    ->ECTECH (as Mick Newton)
  22.  
  23.    All questions or comments will be greatly appreciated!
  24. */
  25.  
  26. #define Uses_TEvent
  27. #define Uses_THistory
  28. #define Uses_THistoryWindow
  29. #define Uses_TInputLine
  30. #define Uses_TKeys
  31. #define Uses_TStreamableClass
  32. #define Uses_TStreamable
  33. #define Uses_MsgBox
  34. #define Uses_ipstream
  35. #define Uses_opstream
  36.  
  37. #include <tv.h>
  38.  
  39. __link(RInputLine)
  40.  
  41. #if !defined __STRING_H
  42. #include <string.h>
  43. #endif
  44.  
  45. #if !defined __STDLIB_H
  46. #include <stdlib.h>
  47. #endif
  48.  
  49. #if !defined __STRSTREAM_H
  50. #include <strstream.h>
  51. #endif
  52.  
  53. #if !defined __CTYPE_H
  54. #include <ctype.h>
  55. #endif
  56.  
  57. #if !defined __TSTRINP_HPP
  58. #include "tstrinp.hpp"
  59. #endif
  60.  
  61. // TStringInputLine
  62. /*
  63.    The following is a graphical representation of the defined and
  64. undefined bits in the 'TStringInputLine::options' data member which
  65. is inherited from TView via TInputLine.
  66.  
  67.    If bit 10, 'ofMandInput', is set, then TStringInputLine will not
  68. accept an empty string as input, i.e. the user must enter a minimal
  69. string.
  70.  
  71.    If bit 11, 'ofUpperCase', is set, then TStringInputLine will convert
  72. input to uppercase.
  73.  
  74.    If bit 12, 'ofLowerCase', is set, then TStringInputLine will convert
  75. input to lowercase.
  76.  
  77.           ┌──┬───────────────────────────────────*──caseField     = 0xC00
  78.           │  │     ┌───┬────────────────────────────ofCentered    = 0x300
  79.  15 14 13 12 11 10 9   8   7  6  5  4  3  2  1  0
  80. ╔══╤══╤══╤╧═╤╧═╤══╤╧═╤═╧═╦═══╤══╤══╤══╤══╤══╤══╤══╗
  81. ║  │  │  │  │  │  │  │lsb║msb│  │  │  │  │  │  │  ║
  82. ╚╤═╧╤═╧╤═╧╤═╧╤═╧╤═╧╤═╧═╤═╩═╤═╧╤═╧╤═╧╤═╧╤═╧╤═╧╤═╧╤═╝
  83.  │  │  │  │  │  │  │   │   │  │  │  │  │  │  │  └───ofSelectable  = 0x001
  84.  │  │  │  │  │  │  │   │   │  │  │  │  │  │  └──────ofTopSelect   = 0x002
  85.  │  │  │  │  │  │  │   │   │  │  │  │  │  └─────────ofFirstClick  = 0x004
  86.  │  │  │  │  │  │  │   │   │  │  │  │  └────────────ofFramed      = 0x008
  87.  │  │  │  │  │  │  │   │   │  │  │  └───────────────ofPreProcess  = 0x010
  88.  │  │  │  │  │  │  │   │   │  │  └──────────────────ofPostProcess = 0x020
  89.  │  │  │  │  │  │  │   │   │  └─────────────────────ofBuffered    = 0x040
  90.  │  │  │  │  │  │  │   │   └────────────────────────ofTileable    = 0x080
  91.  │  │  │  │  │  │  │   └────────────────────────────ofCenterX     = 0x100
  92.  │  │  │  │  │  │  └────────────────────────────────ofCenterY     = 0x200
  93.  │  │  │  │  │  └────────────────────────────────*──ofMandInput   = 0x400
  94.  │  │  │  │  └───────────────────────────────────*──ofUpperCase   = 0x800
  95.  │  │  │  └──────────────────────────────────────*──ofLowerCase   = 0x1000
  96.  │  │  └────────────────────────────────────────────Undefined     = 0x2000
  97.  │  └───────────────────────────────────────────────Undefined     = 0x4000
  98.  └──────────────────────────────────────────────────Undefined     = 0x8000
  99. */
  100.  
  101. const char * const TStringInputLine::name = "TStringInputLine";
  102.  
  103.  
  104. TStringInputLine::TStringInputLine(const TRect& bounds, int aMaxLen) :
  105.                   TInputLine(bounds, aMaxLen)
  106. {
  107. }
  108.  
  109.  
  110. TStringInputLine::~TStringInputLine()
  111. // For debugging
  112. {
  113. }
  114.  
  115.  
  116. void TStringInputLine::write(opstream& os)
  117. {
  118.    TInputLine::write(os);
  119. }
  120.  
  121.  
  122. void *TStringInputLine::read(ipstream& is)
  123. {
  124.    TInputLine::read(is);
  125.    return this;
  126. }
  127.  
  128.  
  129. TStreamable *TStringInputLine::build()
  130. {
  131.    return new TStringInputLine(streamableInit);
  132. }
  133.  
  134.  
  135. TStreamableClass RStringInputLine(TStringInputLine::name,
  136.                                   TStringInputLine::build,
  137.                                   __DELTA(TStringInputLine));
  138.  
  139.  
  140. void TStringInputLine::handleEvent(TEvent& event)
  141. {
  142.    TView::handleEvent(event);
  143.  
  144.    // If neccessary, convert keystroke before THistory grabs it
  145.    if((state & sfSelected) && (event.what == evKeyDown))
  146.       {
  147.       if(options & ofUpperCase)        // If uppercase bit set...
  148.          event.keyDown.charScan.charCode =
  149.          toupper(event.keyDown.charScan.charCode);
  150.       else if(options & ofLowerCase)   // If lowercase bit set...
  151.          event.keyDown.charScan.charCode =
  152.          tolower(event.keyDown.charScan.charCode);
  153.       }
  154.  
  155.    TInputLine::handleEvent(event);
  156. }
  157.  
  158.  
  159. ushort TStringInputLine::dataSize()
  160. {
  161.    return(strlen(data));
  162. }
  163.  
  164.  
  165. void TStringInputLine::getData(void *rec)
  166. {
  167.    // Convert string if neccessary
  168.    if(options & ofUpperCase)
  169.       strupr(data);
  170.    else if(options & ofLowerCase)
  171.       strlwr(data);
  172.  
  173.    strcpy((char *)rec, data);
  174. }
  175.  
  176.  
  177. void TStringInputLine::setData(void *rec)
  178. {
  179.    strcpy(data, (char *)rec);
  180.  
  181.    // Convert string if neccessary
  182.    if(options & ofUpperCase)
  183.       strupr(data);
  184.    else if(options & ofLowerCase)
  185.       strlwr(data);
  186.  
  187.    selectAll(True);
  188. }
  189.  
  190.  
  191. Boolean TStringInputLine::valid(ushort command)
  192. // Checks TStringInputLine for valid input
  193. {
  194.    Boolean ok = True;
  195.    char msg[80];
  196.  
  197.    if((command != cmCancel) && (command != cmValid))
  198.       {
  199.        // Invalid if string is empty, and ofMandInput bit is set
  200.       if((strlen(data) == 0) && (options & ofMandInput))
  201.          {
  202.          select();
  203.          strcpy(msg, "\003This field cannot be empty.");
  204.          messageBox(msg, mfError + mfOKButton);
  205.          selectAll(True);
  206.          ok = False;
  207.          }
  208.       }
  209.  
  210.    if(ok)
  211.       return TInputLine::valid(command);
  212.    else
  213.       return False;
  214. }
  215.  
  216.  
  217. ushort TStringInputLine::setf(ushort setBits, ushort aField)
  218. // Clear 'aField' bits and set 'setBits' bits in TView::options word (huh?)
  219. {
  220.    options &= ~aField;                 // Unset 'aField' bits
  221.    options |= setBits;                 // Set 'setBits' bits
  222.    return options;                     // Return 'options' value
  223. }
  224.  
  225.  
  226. ushort TStringInputLine::setf(ushort setBits)
  227. // Set 'setBits' bits in TView::options word
  228. {
  229.    options |= setBits;                 // Set 'setBits' bits
  230.    return options;                     // Return 'options' value
  231. }
  232.  
  233.  
  234. // End of TSTRINP.CPP
  235.