home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / demos / tvdemo1.cpp < prev    next >
C/C++ Source or Header  |  1999-06-01  |  7KB  |  286 lines

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