home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / TVFORMS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.3 KB  |  244 lines

  1. /*---------------------------------------------------------------------*/
  2. /*                                                                     */
  3. /*   Turbo Vision 1.0                                                  */
  4. /*   Turbo Vision Forms Demo main source file.                         */
  5. /*   Copyright (c) 1991 by Borland International                       */
  6. /*                                                                     */
  7. /*---------------------------------------------------------------------*/
  8. /*                                                                     */
  9. /* This Turbo Vision application uses forms to enter and edit data     */
  10. /* in a collection. Two data files, PHONENUM.TVF and PARTS.TVF, are    */
  11. /* provided and can be loaded using this application's File|Open menu. */
  12. /* The .TVF files were created by GENFORMS.MAK, which compiles and     */
  13. /* and runs GENFORM.CPP. You can create additional data files by       */
  14. /* copying and modifying GENPARTS.H or GENPHONE.H and then             */
  15. /* incorporating your new header into GENFORM.CPP.                     */
  16. /*                                                                     */
  17. /*---------------------------------------------------------------------*/
  18.  
  19. #define Uses_TKeys
  20. #define Uses_TApplication
  21. #define Uses_TEvent
  22. #define Uses_TRect
  23. #define Uses_TDialog
  24. #define Uses_TStaticText
  25. #define Uses_TButton
  26. #define Uses_TMenuBar
  27. #define Uses_TSubMenu
  28. #define Uses_TMenuItem
  29. #define Uses_TStatusLine
  30. #define Uses_TStatusItem
  31. #define Uses_TStatusDef
  32. #define Uses_TDeskTop
  33. #define Uses_TChDirDialog
  34. #define Uses_TFileDialog
  35. #define Uses_MsgBox
  36. #define Uses_TDisplay
  37. #define Uses_TScreen
  38. #define Uses_TEditor
  39. #define Uses_TMemo
  40. #include <tv.h>
  41. __link( RResourceCollection )
  42. __link( RButton )
  43. __link( RCheckBoxes )
  44. __link( RInputLine )
  45. __link( RLabel )
  46. __link( RMenuBar )
  47. __link( RRadioButtons )
  48. __link( RFrame )
  49. __link( REditor )
  50. __link( RMemo )
  51.  
  52. #if !defined( __STRING_H )
  53. #include  <string.h>
  54. #endif // __STRING_H
  55.  
  56. #if !defined( __STDLIB_H )
  57. #include <stdlib.h>
  58. #endif // __STDLIB_H
  59.  
  60. #if !defined( __FORMCMDS_H )
  61. #include "formcmds.h"
  62. #endif // __FORMCMDS_H
  63.  
  64. #if !defined( __LISTDLG_H )
  65. #include "listdlg.h"
  66. #endif // __LISTDLG_H
  67.  
  68. extern TPoint shadowSize;
  69. const MAXSIZE = 150;
  70.  
  71.  
  72. class TFormApp : public TApplication
  73. {
  74. public:
  75.  
  76.     TFormApp();
  77.  
  78.     void handleEvent( TEvent& Event);
  79.     static TMenuBar *initMenuBar( TRect r);
  80.     static TStatusLine *initStatusLine( TRect r);
  81.     void changeDir();
  82.     void dosShell();
  83.     void openListDialog();
  84. };
  85.  
  86. // TFormApp
  87. TFormApp::TFormApp() :
  88.     TApplication(),
  89.     TProgInit(&TFormApp::initStatusLine,
  90.               &TFormApp::initMenuBar,
  91.               &TFormApp::initDeskTop)
  92. {
  93.     TEvent event;
  94.  
  95.     // Display about box
  96.     event.what = evCommand;
  97.     event.message.command = cmAboutBox;
  98.     putEvent(event);
  99. }
  100.  
  101. void TFormApp::changeDir()
  102. {
  103.     TView *d = validView( new TChDirDialog( 0, hlChangeDir ) );
  104.  
  105.     if( d != 0 ) {
  106.         deskTop->execView( d );
  107.         destroy(d);
  108.     }
  109. }
  110.  
  111. void TFormApp::dosShell()
  112. {
  113.     suspend();
  114.     system("cls");
  115.     cout << "Type EXIT to return...";
  116.     system( getenv( "COMSPEC"));
  117.     resume();
  118.     redraw();
  119. }
  120.  
  121. void TFormApp::openListDialog()
  122. {
  123.     TFileDialog *d;
  124.     char *fileName;
  125.     TDialog *listEditor;
  126.     char errorMsg[MAXSIZE];
  127.     extern Boolean fileExists( char *);
  128.     char name[MAXFILE];
  129.     char drive[MAXDRIVE];
  130.     char dir[MAXDIR];
  131.     char ext[MAXEXT];
  132.  
  133.     d = new TFileDialog("*.TVF", "Open File",
  134.            "~N~ame", fdOpenButton, hlOpenListDlg);
  135.     if (validView(d) != NULL)
  136.         {
  137.         if (deskTop->execView(d) != cmCancel)
  138.             {
  139.             fileName = new char[MAXPATH];
  140.             d->getFileName(fileName);
  141.             if (!fileExists(fileName))
  142.                 {
  143.                 strcpy(errorMsg, "Cannot find file ");
  144.                 strcat(errorMsg, fileName);
  145.                 messageBox(errorMsg, mfError | mfOKButton);
  146.                 }
  147.             else
  148.                 {
  149.                 // If listEditor exists, select it; otherwise, open new one
  150.                 fnsplit(fileName, drive, dir, name, ext);
  151.                 listEditor = (TDialog *)message(deskTop, evBroadcast, cmEditingFile, fileName);
  152.                 if (listEditor == NULL)
  153.                     deskTop->insert(validView(new TListDialog(fileName, name)));
  154.                 else listEditor->select();
  155.                 }
  156.             delete fileName;
  157.             }
  158.         destroy(d);
  159.         }
  160. }
  161.  
  162. void TFormApp::handleEvent(TEvent& event)
  163. {
  164.     ushort newMode;
  165.     char aboutMsg[80]; 
  166.  
  167.     TApplication::handleEvent(event);
  168.     if (event.what == evCommand)
  169.         {
  170.         switch (event.message.command)
  171.             {
  172.             case cmListOpen:
  173.                 openListDialog();
  174.                 break;
  175.             case cmChgDir:
  176.                 changeDir();
  177.                 break;
  178.             case cmDosShell:
  179.                 dosShell();
  180.                 break;
  181.             case cmAboutBox:
  182.                 strcpy(aboutMsg, "\x3Turbo Vision C++ 1.0\n\n\x3Turbo Vision Forms Demo");
  183.                 messageBox(aboutMsg, mfInformation | mfOKButton);
  184.                 break;
  185.             case cmVideoMode:
  186.                 newMode = TScreen::screenMode ^ TDisplay::smFont8x8;
  187.                 if ((newMode & TDisplay::smFont8x8) != 0)
  188.                     shadowSize.x = 1;
  189.                 else
  190.                     shadowSize.x = 2;
  191.                 setScreenMode(newMode);
  192.                 break;
  193.  
  194.             default:
  195.                 return; 
  196.             }
  197.         clearEvent(event);
  198.         }
  199. }
  200.  
  201. TMenuBar *TFormApp::initMenuBar( TRect r)
  202. {
  203.  
  204.     r.b.y = r.a.y + 1;
  205.     return new TMenuBar(r,
  206.       *new TSubMenu( "~\xF0~", hcNoContext ) +
  207.         *new TMenuItem( "~V~ideo mode", cmVideoMode, kbNoKey, hcNoContext, "" ) +
  208.              newLine() +
  209.         *new TMenuItem( "~A~bout...", cmAboutBox, kbNoKey, hcNoContext ) +
  210.       *new TSubMenu( "~F~ile", hcNoContext) +
  211.         *new TMenuItem( "~O~pen...", cmListOpen, kbF3, hcNoContext, "F3" ) +
  212.         *new TMenuItem( "~S~ave", cmListSave, kbF2, hcNoContext, "F2" ) +
  213.              newLine() +
  214.         *new TMenuItem( "~C~hange directory...", cmChgDir, kbNoKey, hcNoContext ) +
  215.         *new TMenuItem( "~D~OS shell", cmDosShell, kbNoKey, hcNoContext ) +
  216.         *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X" ) +
  217.       *new TSubMenu( "~W~indow", hcNoContext ) +
  218.         *new TMenuItem( "~M~ove", cmResize, kbCtrlF5, hcNoContext, "Cntl-F5") +
  219.         *new TMenuItem( "~N~ext", cmNext, kbF6, hcNoContext, "F6") +
  220.         *new TMenuItem( "~P~rev", cmPrev, kbShiftF6, hcNoContext, "Shift-F6") +
  221.         *new TMenuItem( "~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3")
  222.       );
  223. }
  224.  
  225. TStatusLine *TFormApp::initStatusLine( TRect r )
  226. {
  227.     r.a.y = r.b.y - 1;
  228.     return new TStatusLine( r,
  229.     *new TStatusDef( 0, 0xFFFF ) +
  230.       *new TStatusItem( "~F2~ Save", kbF2, cmListSave ) +
  231.       *new TStatusItem( "~F3~ Open", kbF3, cmListOpen ) +
  232.       *new TStatusItem( "~F10~ Menu", kbF10, cmMenu) +
  233.       *new TStatusItem( "", kbCtrlF5, cmResize )
  234.       );
  235. }
  236.  
  237. int main()
  238. {
  239.     TFormApp formApp;
  240.  
  241.     formApp.run();
  242.     return 0;
  243. }
  244.