home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / tvsamp / tvsample.cpp next >
Encoding:
C/C++ Source or Header  |  1992-04-09  |  14.1 KB  |  436 lines

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