home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / tvsample.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  11.6 KB  |  407 lines

  1. /*----------------------------------------------------------*/
  2. /*                                                          */
  3. /*   Turbo C++ Tips & Techniques                            */
  4. /*                                                          */
  5. /*   Copyright (c) 1991 by Borland International            */
  6. /*                                                          */
  7. /*   Turbo Vision Sample program source file                */
  8. /*                                                          */
  9. /*----------------------------------------------------------*/
  10. /*                                                          */
  11. /*   This program illustrates what can be accomplished      */
  12. /*   with approximately 160-170 lines of code using         */
  13. /*   Turbo Vision for C++.                                  */
  14. /*                                                          */
  15. /*----------------------------------------------------------*/
  16.  
  17. // These #defines let the TV.H header file know which Turbo
  18. // Vision classes we intend to use
  19.  
  20. #define Uses_MsgBox
  21. #define Uses_TApplication
  22. #define Uses_TCommandSet
  23. #define Uses_TDeskTop
  24. #define Uses_TDialog
  25. #define Uses_TEditor
  26. #define Uses_TEditWindow
  27. #define Uses_TEvent
  28. #define Uses_TFileDialog
  29. #define Uses_TFileEditor
  30. #define Uses_TKeys
  31. #define Uses_TMenuBar
  32. #define Uses_TMenuItem
  33. #define Uses_TProgram
  34. #define Uses_TStatusDef
  35. #define Uses_TStatusItem
  36. #define Uses_TStatusLine
  37. #define Uses_TSubMenu
  38.  
  39. #include <tv.h>            // Turbo Vision header file
  40. #include "gadgets.h"       // Header file for the clock view
  41. #include <stdarg.h>        
  42. #include <dir.h>           
  43.  
  44. // Command constants used by TVSAMPLE
  45.  
  46. const unsigned cmAboutBox        = 100;
  47. const unsigned cmNewFile         = 101;
  48.  
  49. const unsigned hcEditorOpen      = 0x1000;
  50.  
  51.  
  52. // This is the main application class.  By deriving from TApplication,
  53. // our application inherits most of the built in functionality of
  54. // Turbo Vision.
  55.  
  56. class TSampleApp : public TApplication
  57. {
  58. protected:
  59.   TEditWindow  *theEditor;
  60.   TEditWindow  *theClipboard;
  61.   Boolean      editorOpen;
  62.   TClockView   *clock;
  63.  
  64. public:
  65.   TSampleApp();
  66.   static TMenuBar    *initMenuBar(TRect r);
  67.   static TStatusLine *initStatusLine(TRect r);
  68.   void               handleEvent(TEvent&);
  69.   void               idle(void);
  70.   void               updateCommands(void);
  71.   void               newFile(char *aName);
  72.   Boolean            closeEditor(Boolean destroyIt);
  73.   void               openFile(void);
  74.   static void        outOfMemory(void);
  75. };
  76.  
  77. // Function prototype for the standard editor dialog function
  78.  
  79. ushort edDialog(int what, ...);
  80.  
  81.  
  82. // This is the constructor for TSampleApp.  Here, initialization of
  83. // application-specific variables is performed.
  84.  
  85. TSampleApp::TSampleApp(void) :
  86.   TProgInit(&TSampleApp::initStatusLine,
  87.             &TSampleApp::initMenuBar,
  88.             &TSampleApp::initDeskTop)
  89. {
  90.   editorOpen = False;
  91.  
  92.   TRect r = getExtent();                      // Create the clock view
  93.   r.a.x = r.b.x - 9;      r.b.y = r.a.y + 1;
  94.   clock = new TClockView( r );
  95.   insert(clock);                              // Insert clock into the desktop
  96.  
  97.   // Next, the clipboard is created.  The clipboard is necessary to support
  98.   // Cut, Paste, Copy, etc. in this application
  99.   
  100.   r = deskTop->getExtent();
  101.   theClipboard = new TEditWindow( r, 0, wnNoNumber );
  102.   if (validView( theClipboard ))
  103.   {
  104.     TEditor::clipboard = (TEditor *) theClipboard->editor;
  105.     TEditor::clipboard->canUndo = False;
  106.     theClipboard->hide();
  107.     deskTop->insert( theClipboard );
  108.   }
  109. }
  110.  
  111. // This function constructs the application's menu
  112.  
  113. TMenuBar *TSampleApp::initMenuBar(TRect r)
  114. {
  115.   TSubMenu& sub1 =
  116.     *new TSubMenu("~\360~", kbAltSpace, hcNoContext) +
  117.       *new TMenuItem("~A~bout...", cmAboutBox, kbNoKey, hcNoContext, 0);
  118.  
  119.   TSubMenu& sub2 =
  120.     *new TSubMenu("~F~ile", kbAltF, hcNoContext) +
  121.       *new TMenuItem("~N~ew", cmNewFile, kbNoKey, hcNoContext, 0) +
  122.       newLine() +
  123.       *new TMenuItem("~O~pen...", cmFileOpen, kbF3, hcNoContext, "F3") +
  124.       *new TMenuItem("~S~ave", cmSave, kbF2, hcNoContext, "F2") +
  125.       *new TMenuItem("Save ~a~s...", cmSaveAs, kbNoKey, hcNoContext, 0) +
  126.       newLine() +
  127.       *new TMenuItem("E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X");
  128.  
  129.   TSubMenu& sub3 =
  130.     *new TSubMenu("~E~dit", kbAltE, hcNoContext)+
  131.       *new TMenuItem("~U~ndo", cmUndo, kbNoKey, hcNoContext, 0) +
  132.       newLine() +
  133.       *new TMenuItem("Cu~t~", cmCut, kbShiftDel, hcNoContext, "Shift-Del") +
  134.       *new TMenuItem("~C~opy", cmCopy, kbCtrlIns, hcNoContext, "Ctrl-Ins") +
  135.       *new TMenuItem("~P~aste", cmPaste, kbShiftIns, hcNoContext, "Shift-Ins") +
  136.       newLine() +
  137.       *new TMenuItem("C~l~ear", cmClear, kbDel, hcNoContext, "Del");
  138.  
  139.   TSubMenu& sub4 =
  140.     *new TSubMenu("~W~indow", kbAltW, hcNoContext)+
  141.       *new TMenuItem("~R~esize", cmResize, kbCtrlF5, hcNoContext, "Ctrl-F5") +
  142.       *new TMenuItem("~Z~oom", cmZoom, kbF5, hcNoContext, "F5") +
  143.       newLine() +
  144.       *new TMenuItem("~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3");
  145.  
  146.   r.b.y = r.a.y + 1;
  147.   return new TMenuBar( r, sub1 + sub2 + sub3 + sub4 );
  148. }
  149.  
  150.  
  151.  
  152. // This function constructs the application's status line(s)
  153. // Note that the TStatusDef class specifies a help-context
  154. // range for which a particular status line should be displayed.
  155. // Thus, this application will display two different status lines
  156. // depending on which help-context is active.
  157.  
  158. TStatusLine *TSampleApp::initStatusLine(TRect r)
  159. {
  160.   r.a.y = r.b.y - 1;
  161.  
  162.   TStatusDef& statLine1 =
  163.     *new TStatusDef(hcEditorOpen, hcEditorOpen) +
  164.     *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  165.     *new TStatusItem("~Alt-F3~ Close", kbAltF3, cmClose) +
  166.     *new TStatusItem("~F2~ Save", kbF2, cmSave) +
  167.     *new TStatusItem("~F3~ Open", kbF3, cmFileOpen) +
  168.     *new TStatusItem(0, kbF10, cmMenu);
  169.  
  170.   TStatusDef& statLine2 =
  171.     *new TStatusDef(0, 0xFFFF) +
  172.     *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  173.     *new TStatusItem(0, kbAltF3, cmClose) +
  174.     *new TStatusItem(0, kbF10, cmMenu);
  175.  
  176.   return new TStatusLine( r, statLine1 + statLine2 );
  177. }
  178.  
  179.  
  180. // This function is called by Turbo Vision during idle periods.  It
  181. // is used to update the clock view.
  182.  
  183. void TSampleApp::idle(void)
  184. {
  185.   TApplication::idle();
  186.   clock->update();
  187. }
  188.  
  189.  
  190. // updateCommands is called to determine if the File|Save and the
  191. // File|Save as menu options should be available.
  192.  
  193. void TSampleApp::updateCommands(void)
  194. {
  195.   TCommandSet cs;
  196.  
  197.   cs += cmSave;
  198.   cs += cmSaveAs;
  199.   if (editorOpen)
  200.     deskTop->enableCommands(cs);
  201.   else
  202.     deskTop->disableCommands(cs);
  203. }
  204.  
  205.  
  206. // This function creates a new file editor (after closing any other open
  207. // file editor) and inserts it into the desktop (makes it visible).
  208.  
  209. void TSampleApp::newFile(char *aName)
  210. {
  211.   if (!closeEditor(True))
  212.     return;
  213.  
  214.   TRect r = deskTop->getExtent();
  215.  
  216.   r.a.x++;
  217.   r.b.x -= 3;
  218.   r.b.y -= 3;
  219.   theEditor = new TEditWindow( r, aName, wnNoNumber );
  220.   if (validView( theEditor ))
  221.   {
  222.     theEditor->helpCtx = hcEditorOpen;
  223.     theEditor->editor->editorFlags &= ~(efBackupFiles);
  224.     theEditor->editor->editorDialog = edDialog;
  225.     deskTop->insert( theEditor );
  226.     editorOpen = True;
  227.   }
  228.   updateCommands();
  229. }
  230.  
  231.  
  232. // This function is called to close any open file editor.  This function
  233. // prompts the user to verify this action if there are un-saved changes
  234. // present in the editor.
  235.  
  236. Boolean TSampleApp::closeEditor(Boolean destroyIt)
  237. {
  238.   Boolean retVal = True;
  239.  
  240.   if (editorOpen)
  241.     if (theEditor->editor->modified)
  242.       retVal = 
  243.         (messageBox("\003You will lose your work if you proceed.",
  244.                     mfConfirmation | mfOKCancel) == cmOK) ? True : False;
  245.   if (retVal)
  246.   {
  247.     if (destroyIt)
  248.       if (editorOpen)
  249.         destroy( theEditor );
  250.     editorOpen = False;
  251.     updateCommands();
  252.   }
  253.   return retVal;
  254. }
  255.  
  256.  
  257. // openFile displays the standard file open dialog box.  If a file
  258. // is selected from the dialog, it is loaded.
  259.  
  260. void TSampleApp::openFile(void)
  261. {
  262.   ushort control = cmCancel;
  263.   char   buf[MAXPATH];
  264.  
  265.   TFileDialog *fd =
  266.     new TFileDialog("*.*", "Load a file", "~N~ame", fdOpenButton, 1);
  267.   if (validView( fd ))
  268.     control = execView( fd );
  269.   if (control == cmFileOpen || control == cmOK) 
  270.   {
  271.     if (closeEditor(True))
  272.     {
  273.       fd->getFileName(buf);
  274.       newFile( buf );
  275.     }
  276.   }
  277.   destroy( fd );
  278. }
  279.  
  280.  
  281. // This function is used to automatically handle:
  282. //   1) the upload of program data to a dialog box
  283. //   2) the modal execution of the dialog box
  284. //   3) the download of data from the dialog box to the program
  285. //   4) the destruction of the dialog box
  286.  
  287. ushort execDialog( TDialog *d, void *data )
  288. {
  289.   TView *p = TProgram::application->validView( d );
  290.   if( p == 0 )
  291.     return cmCancel;
  292.   else
  293.   {
  294.     if( data != 0 )
  295.       p->setData( data );
  296.     ushort result = TProgram::deskTop->execView( p );
  297.     if( result != cmCancel && data != 0 )
  298.       p->getData( data );
  299.     TObject::destroy( p );
  300.     return result;
  301.   }
  302. }
  303.  
  304. typedef char *_charPtr;
  305.  
  306.  
  307. // The edDialog function handles messages sent by the editor window.
  308. // The editor window will call this function to handle certain special
  309. // case situations (such as requests for file save and error conditions).
  310.  
  311. ushort edDialog(int what, ...)
  312. {
  313.   va_list arg;
  314.  
  315.   switch (what)
  316.   {
  317.     case edOutOfMemory:
  318.       TSampleApp::outOfMemory();
  319.       break;
  320.  
  321.     case edReadError:
  322.     case edWriteError:
  323.     case edCreateError:
  324.       messageBox(mfOKButton | mfError, "\003Error during file I/O.");
  325.       break;
  326.  
  327.     case edSaveAs:
  328.       va_start( arg, what );
  329.       return execDialog( new TFileDialog( "*.*",
  330.                                "Save file as",
  331.                                "~N~ame",
  332.                                fdOKButton,
  333.                                2 ), va_arg( arg, _charPtr) );
  334.   }
  335.   return cmCancel;
  336. }
  337.  
  338.  
  339. // Global out-of-memory handler
  340.  
  341. void TSampleApp::outOfMemory(void)
  342. {
  343.   messageBox("\003Not enough memory to complete operation.", mfOKButton | mfError);
  344. }
  345.  
  346.  
  347. // This is the sample application's handleEvent function.  This function
  348. // is used to implement the commands unique to this program.  Turbo Vision
  349. // calls this function whenever there is a message to be processed.
  350.  
  351. void TSampleApp::handleEvent(TEvent& event)
  352. {
  353.   if (event.what & evCommand)
  354.   {
  355.     if ((event.message.command == cmClose) && (editorOpen))
  356.       if (!closeEditor(False))
  357.         clearEvent( event );
  358.   }
  359.  
  360.   TApplication::handleEvent( event );     // Call the original handleEvent
  361.  
  362.   if (event.what == evCommand)
  363.   {
  364.     switch (event.message.command)        // This might be a user command
  365.     {                                     // (menu/status line selection)
  366.       case cmNewFile:
  367.         newFile(0);
  368.         break;
  369.  
  370.       case cmFileOpen:
  371.         openFile();
  372.         break;
  373.  
  374.       case cmAboutBox:
  375.         messageBox( mfInformation | mfOKButton, 
  376.                     "\003Simple Turbo Vision Program\n\003\n" \
  377.                     "\003(C) 1991 Borland International");
  378.         break;
  379.  
  380.       default:
  381.         return;
  382.     }
  383.     clearEvent( event );
  384.   }
  385. }
  386.  
  387.  
  388. // This is the entire main program block.  The application is constructed.
  389. // An editor is inserted into the desktop.  Finally, the application is run.
  390.  
  391. main(int argc, char *argv[])
  392. {
  393.   TSampleApp myIDE;
  394.   char   fName[MAXPATH];
  395.   char   *theFile = NULL;
  396.  
  397.   if (argc > 1)
  398.   {
  399.     strcpy( fName, argv[1] );
  400.     fexpand( fName );
  401.     if (validFileName( fName ))
  402.       theFile = fName;
  403.   }
  404.   myIDE.newFile(theFile);
  405.   myIDE.run();
  406. }
  407.