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

  1. /*----------------------------------------------------------*/
  2. /*                                                          */
  3. /*   Turbo Vision 1.0                                       */
  4. /*   Copyright (c) 1991 by Borland International            */
  5. /*                                                          */
  6. /*   Turbo Vision TVDEMO source file                        */
  7. /*----------------------------------------------------------*/
  8.  
  9. #define Uses_TView
  10. #define Uses_TRect
  11. #define Uses_TStatusLine
  12. #define Uses_TStatusDef
  13. #define Uses_TStatusItem
  14. #define Uses_TKeys
  15. #define Uses_MsgBox
  16. #define Uses_fpstream
  17. #define Uses_TEvent
  18. #define Uses_TDeskTop
  19. #define Uses_TApplication
  20. #define Uses_TWindow
  21. #define Uses_TDeskTop
  22.  
  23. #include <tv.h>
  24.  
  25. #include "tvdemo.h"
  26. #include "gadgets.h"
  27. #include "fileview.h"
  28. #include "puzzle.h"
  29. #include "demohelp.h"
  30. #include <help.h>
  31.  
  32. #include <dir.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. //
  37. // main: create an application object.  Constructor takes care of all
  38. //   initialization.  Calling run() from TProgram makes it tick and
  39. //   the destructor will destroy the world.
  40. //
  41. //   File names can be specified on the command line for automatic
  42. //   opening.
  43. //
  44.  
  45. int main(int argc, char **argv)
  46. {
  47.     TVDemo demoProgram(argc, argv);        //  Application Instance.
  48.  
  49.     demoProgram.run();
  50.     return 0;
  51. }
  52.  
  53.  
  54. //
  55. // Constructor for the application.  Command line parameters are interpreted
  56. //   as file names and opened.  Wildcards are accepted and put up a dialog
  57. //   box with the appropriate search path.
  58. //
  59.  
  60. TVDemo::TVDemo( int argc, char **argv ) :
  61.     TProgInit( &TVDemo::initStatusLine,
  62.                &TVDemo::initMenuBar,
  63.                &TVDemo::initDeskTop )
  64. {
  65.     TView *w;
  66.     char fileSpec[128];
  67.     int len;
  68.  
  69.     TRect r = getExtent();                      // Create the clock view.
  70.     r.a.x = r.b.x - 9;      r.b.y = r.a.y + 1;
  71.     clock = new TClockView( r );
  72.     insert(clock);
  73.  
  74.     r = getExtent();                            // Create the heap view.
  75.     r.a.x = r.b.x - 13;     r.a.y = r.b.y - 1;
  76.     heap = new THeapView( r );
  77.     insert(heap);
  78.  
  79.     while (--argc > 0)                              // Display files specified
  80.         {                                           //  on command line.
  81.         strcpy( fileSpec, *++argv );
  82.         len = strlen( fileSpec );
  83.         if( fileSpec[len-1] == '\\' )
  84.             strcat( fileSpec, "*.*" );
  85.         if( strchr( fileSpec, '*' ) || strchr( fileSpec, '?' ) )
  86.             openFile( fileSpec );
  87.         else
  88.             {
  89.             w = validView( new TFileWindow( fileSpec ) );
  90.             if( w != 0 )
  91.                 deskTop->insert(w);
  92.             }
  93.         }
  94.  
  95. }
  96.  
  97.  
  98. //
  99. // DemoApp::getEvent()
  100. //  Event loop to check for context help request
  101. //
  102.  
  103. void TVDemo::getEvent(TEvent &event)
  104. {
  105.     TWindow *w;
  106.     THelpFile *hFile;
  107.     fpstream *helpStrm;
  108.     static Boolean helpInUse = False;
  109.  
  110.     TApplication::getEvent(event);
  111.     switch (event.what)
  112.         {
  113.         case evCommand:
  114.             if ((event.message.command == cmHelp) && ( helpInUse == False)) 
  115.                 {
  116.                 helpInUse = True;
  117.                 helpStrm = new fpstream("DEMOHELP.HLP", ios::in|ios::binary);
  118.                 hFile = new THelpFile(*helpStrm);
  119.                 if (!helpStrm)
  120.                     {
  121.                     messageBox("Could not open help file", mfError | mfOKButton);
  122.                     delete hFile;
  123.                     }
  124.                 else
  125.                     {
  126.                     w = new THelpWindow(hFile, getHelpCtx());
  127.                     if (validView(w) != 0)
  128.                         {
  129.                         execView(w);
  130.                         destroy( w );
  131.                         }
  132.                     clearEvent(event);
  133.                     }
  134.                 helpInUse = False;
  135.                 }
  136.             break;
  137.         case evMouseDown:
  138.             if (event.mouse.buttons != 1)
  139.                 event.what = evNothing;
  140.             break;
  141.         }
  142.  
  143. }  
  144.  
  145. //
  146. // Create statusline.
  147. //
  148.  
  149. TStatusLine *TVDemo::initStatusLine( TRect r )
  150. {
  151.     r.a.y = r.b.y - 1;
  152.  
  153.     return (new TStatusLine( r,
  154.       *new TStatusDef( 0, 0xFFFF ) +
  155.             *new TStatusItem( "~F1~ Help", kbF1, cmHelp ) +
  156.         *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  157.         *new TStatusItem( 0, kbF10, cmMenu )
  158.         )
  159.     );
  160. }
  161.  
  162.  
  163. //
  164. // Puzzle function
  165. //
  166.  
  167. void TVDemo::puzzle()
  168. {
  169.     TPuzzleWindow *puzz = (TPuzzleWindow *) validView(new TPuzzleWindow);
  170.  
  171.     if(puzz != 0)
  172.         {
  173.         puzz->helpCtx = hcPuzzle;
  174.         deskTop->insert(puzz);
  175.     }
  176. }
  177.  
  178.  
  179. //
  180. // retrieveDesktop() function ( restores the previously stored Desktop )
  181. //
  182.  
  183. void TVDemo::retrieveDesktop()
  184. {
  185.     struct ffblk ffblk;
  186.  
  187.     if (findfirst("TVDEMO.DST", &ffblk, 0))
  188.         messageBox("Could not find desktop file", mfOKButton | mfError);
  189.     else 
  190.         {
  191.         fpstream *f = new fpstream("TVDEMO.DST", ios::in|ios::binary);
  192.         if( !f )
  193.             messageBox("Could not open desktop file", mfOKButton | mfError);
  194.         else
  195.            {
  196.            TVDemo::loadDesktop(*f);
  197.            if( !f )
  198.                messageBox("Error reading desktop file", mfOKButton | mfError);
  199.            }
  200.         delete f;
  201.         }
  202. }
  203.  
  204. //
  205. // saveDesktop() function ( saves the DeskTop by calling storeDesktop function )
  206. //
  207.  
  208. void TVDemo::saveDesktop()
  209. {
  210.     fpstream *f = new fpstream("TVDEMO.DST", ios::out|ios::binary);
  211.  
  212.     if( f )
  213.         {
  214.         TVDemo::storeDesktop(*f);
  215.         if( !f )
  216.             {
  217.             messageBox("Could not create TVDEMO.DST.", mfOKButton | mfError);
  218.             delete f;
  219.             ::remove("TVDEMO.DST");
  220.             return;
  221.             }
  222.         }
  223.     delete f;
  224. }
  225.  
  226. //
  227. // writeView() function ( writes a view object to a resource file )
  228. //
  229.  
  230. static void writeView(TView *p, void *strm)
  231. {
  232.    fpstream *s = (fpstream *) strm;
  233.    if (p != TProgram::deskTop->last)
  234.       *s << p;
  235. }
  236.  
  237. //
  238. // storeDesktop() function ( stores the Desktop in a resource file )
  239. //
  240.  
  241. void TVDemo::storeDesktop(fpstream& s)
  242. {
  243.   deskTop->forEach(::writeView, &s);
  244.   s << 0;
  245. }
  246.  
  247. //
  248. // Tile function
  249. //
  250.  
  251. void TVDemo::tile()
  252. {
  253.     deskTop->tile( deskTop->getExtent() );
  254. }
  255.  
  256.  
  257.