home *** CD-ROM | disk | FTP | other *** search
- /*
- TVINPUT.CPP
- (C) Copyright 1992 Michael Joseph Newton
- All rights reserved.
-
-
- This file is only used to demonstrate the use of the
- TStringInputLine class.
-
- The TStringInputLine class is derived from Turbo Vision's
- TInputLine class. TStringInputLine is declared in TSTRINP.HPP,
- and defined in TSTRINP.CPP.
-
- This file is hereby released by the author to the public
- domain. Use it as you will.
-
- Author: Michael Joseph Newton
- Date: 01/17/92
- Address: 17874 Marygold Ave. #32
- Bloomington, CA 92316
- Phone: (714) 877-4655
- CompuServe: 70402,531 (as Michael J. Newton)
- RelayNet: ->ECTECH (as Mick Newton)
-
- All questions or comments will be greatly appreciated!
- */
-
- #define Uses_TApplication
- #define Uses_TButton
- #define Uses_TDeskTop
- #define Uses_TDialog
- #define Uses_TEvent
- #define Uses_THistory
- #define Uses_TKeys
- #define Uses_TMenuBar
- #define Uses_TMenuItem
- #define Uses_TRect
- #define Uses_TStaticText
- #define Uses_TSubMenu
- #define Uses_TStatusLine
- #define Uses_TStatusItem
- #define Uses_TStatusDef
- #define Uses_MsgBox
-
- #include <tv.h>
-
- __link(RButton)
- __link(RFrame)
- __link(RHistory)
- __link(RInputLine)
- __link(RLabel)
- __link(RMenuBar)
- __link(RResourceCollection)
-
- #if !defined __STRING_H
- #include <string.h>
- #endif
-
- #if !defined __STDLIB_H
- #include <stdlib.h>
- #endif
-
- #if !defined __STRSTREA_H
- #include <strstrea.h>
- #endif
-
- #if !defined __TSTRINP_HPP
- #include "tstrinp.hpp"
- #endif
-
- // Define cmXXX commands
- const int cmRunTest = 1000,
- cmCase = 1001,
- cmMand = 1002,
- cmAbout = 1003;
-
- // Define THistory ID for TStringInputLine
- const ushort hlStrInpLine = 100;
-
-
- // Application class
- class TVInput : public TApplication
- {
- public:
- TVInput();
- private:
- // Usual stuff
- void handleEvent(TEvent& Event);
- static TMenuBar *initMenuBar(TRect r);
- static TStatusLine *initStatusLine(TRect r);
- void testDialog(); // Tests TStringInputLine
- void toggleCase(); // Toggles input case
- void toggleMand(); // Toggles mandatory input
- char string[81]; // String buffer for test
- Boolean caseToggle; // True = uppercase input
- Boolean mandToggle; // True = input mandatory
- };
-
-
- // Constructor
- TVInput::TVInput() :
- TApplication(),
- TProgInit(initStatusLine, initMenuBar, initDeskTop)
- {
-
- memset(string, '\0', 81); // Clear the string
- caseToggle = True; // Start with uppercase input...
- mandToggle = True; // and input mandatory
- }
-
-
- void TVInput::handleEvent(TEvent& event)
- {
- TApplication::handleEvent(event);
-
- if(event.what == evCommand)
- {
- switch(event.message.command)
- {
- case cmRunTest:
- testDialog();
- break;
- case cmCase:
- toggleCase();
- break;
- case cmMand:
- toggleMand();
- break;
- case cmAbout:
- char msg[] = "\003TStringInputLine\n\n"
- "\003by Michael Joseph Newton\n"
- "\003CompuServe 70402,531";
- messageBox(msg, mfInformation | mfOKButton);
- break;
- default:
- return;
- }
- clearEvent(event);
- }
- }
-
-
- TMenuBar *TVInput::initMenuBar(TRect r)
- {
- r.b.y = r.a.y + 1;
-
- return new TMenuBar(r,
- *new TSubMenu("~\360~", kbAltSpace, hcNoContext) +
- *new TMenuItem("~A~bout", cmAbout, hcNoContext) +
- *new TSubMenu("~T~est", hcNoContext) +
- *new TMenuItem("T~S~tringInputLine", cmRunTest, hcNoContext) +
- *new TMenuItem("E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X") +
- *new TSubMenu("~T~oggles", hcNoContext) +
- *new TMenuItem("Toggle ~i~nput case", cmCase, hcNoContext) +
- *new TMenuItem("Toggle ~m~andatory input", cmMand, hcNoContext));
- }
-
-
- TStatusLine *TVInput::initStatusLine(TRect r)
- {
- r.a.y = r.b.y - 1;
-
- return new TStatusLine( r,
- *new TStatusDef(0, 0xFFFF) +
- *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem("", kbF10, cmMenu));
- }
-
-
- void TVInput::testDialog()
- // This is the function that actually tests TStringInputLine
- {
- TDialog *d = new TDialog(TRect(0, 0, 50, 8), "Test TStringInputLine");
-
- // Add a prompt to the dialog
- char msg[] = "Enter a string";
- TStaticText *t = new TStaticText(TRect(0, 2, 15, 3), msg);
- t->options |= ofCenterX;
- d->insert(t);
-
- // Create a new TStringInputLine. Max input length = 50
- TStringInputLine *i = new TStringInputLine(TRect(3, 3, 44, 4), 51);
-
- // NOTE: This is a TStringInputLine function, not ios::setf
- // Set 'input mandatory' bit if mandToggle is True
- if(mandToggle)
- i->setf(ofMandInput);
-
- // Set input case bit according to caseToggle
- if(caseToggle)
- i->setf(ofUpperCase, caseField);
- else
- i->setf(ofLowerCase, caseField);
-
- // Insert it in the dialog
- d->insert(i);
-
- // Add a history list
- // NOTE: When you access the history list in the program, there
- // will only be one copy of the string in either upper or
- // lower case.
- d->insert(new THistory(TRect(45, 3, 48, 4), i, hlStrInpLine));
-
- // Add some buttons
- TButton *b;
- b = new TButton(TRect(2, 5, 22, 7), "O~K~", cmOK, bfDefault);
- d->insert(b);
- b = new TButton(TRect(28, 5, 48, 7), "~C~ancel", cmCancel, bfNormal);
- d->insert(b);
-
- d->options |= ofCentered;
- d->selectNext(False);
-
- TView *p = TProgram::application->validView(d);
-
- if(p != 0)
- {
- p->setData(&string);
- ushort result = TProgram::deskTop->execView(p);
- if(result != cmCancel)
- {
- // Display the user's string
- p->getData(&string);
- char buff[256];
- ostrstream os(buff, sizeof buff);
- os << "\003The string you entered :\n"
- << string
- << ends;
- messageBox(os.str(), mfInformation | mfOKButton);
- destroy(p);
- }
- }
- }
-
-
- void TVInput::toggleCase()
- // Toggles input case flag
- {
- caseToggle = (Boolean) !caseToggle;
- }
-
-
- void TVInput::toggleMand()
- // Toggles input mandatory flag
- {
- mandToggle = (Boolean) !mandToggle;
- }
-
-
- int main()
- {
- TVInput program;
- program.run();
- return 0;
- }
-