home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1701.ZIP / TI1701.ASC
Text File  |  1993-10-07  |  13KB  |  529 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  9.   VERSION  :  3.x
  10.        OS  :  DOS
  11.      DATE  :  October 7, 1993                          PAGE  :  1/8
  12.  
  13.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  14.  
  15.  
  16.  
  17.  
  18.   //=============================================================
  19.   //  Turbo Vision - Streaming a User Defined Dialog
  20.   //
  21.   //  - This sample code demonstrates a method of creating a
  22.   // user defined  Dialog box with unique data members and
  23.   // handleEvent, executing the  Dialog box, streaming it out
  24.   // to disk, retrieving it from disk and  reexecuting it.
  25.   //
  26.   //  - Please refer to Chapter 8 in the Turbo Vision User's Guide
  27.   //  for more information on streamable objects.  TI #1023
  28.   // also provides  detailed information about using Turbo
  29.   // Vision Streams.
  30.   //  - Compile with Bcc -ml strmdlg.cpp gadgets.cpp tv.lib
  31.   //
  32.   //  - Note: GADGETS.CPP can be found in the TVISION\DEMOS
  33.   // directory of  your C++ product.
  34.   //
  35.   //=============================================================
  36.  
  37.   //----------------- TV "Uses" ---------------------------------
  38.   #define Uses_MsgBox
  39.   #define Uses_TProgram
  40.   #define Uses_TApplication
  41.   #define Uses_TKeys
  42.   #define Uses_TRect
  43.   #define Uses_TMenuBar
  44.   #define Uses_TSubMenu
  45.   #define Uses_TMenuItem
  46.   #define Uses_TStatusLine
  47.   #define Uses_TStatusItem
  48.   #define Uses_TStatusDef
  49.   #define Uses_TDeskTop
  50.   #define Uses_TView
  51.   #define Uses_TWindow
  52.   #define Uses_TFrame
  53.   #define Uses_TDialog
  54.   #define Uses_TButton
  55.   #define Uses_TSItem
  56.   #define Uses_TMenu
  57.   #define Uses_TObject
  58.   #define Uses_TStreamable
  59.   #define Uses_TResourceCollection
  60.   #define Uses_TStaticText
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  75.   VERSION  :  3.x
  76.        OS  :  DOS
  77.      DATE  :  October 7, 1993                          PAGE  :  2/8
  78.  
  79.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  80.  
  81.  
  82.  
  83.  
  84.   //----------------------- stream "Uses" -----------------------
  85.   #define Uses_ifpstream
  86.   #define Uses_ofpstream
  87.   #define Uses_TStreamableClass
  88.   #define Uses_ipstream
  89.   #define Uses_opstream
  90.  
  91.   #include "gadgets.h"
  92.   #include <stdio.h>         // for sprintf()
  93.   #include <tv.h>            // Need TV.H before the __LINK Macros
  94.  
  95.  
  96.   //-------------------------------------------------------------
  97.   //  You must register every TV class you intend to use.
  98.   //  Attempting to stream a class which is not registered
  99.   //  will cause the program to crash.
  100.   //-------------------------------------------------------------
  101.   __link( RButton )
  102.   __link( RFrame )
  103.   __link( RResourceCollection )
  104.   __link( RDialog )
  105.   __link( RStaticText )
  106.  
  107.  
  108.   //------------ define our message constants ------------------
  109.   const int cmAbout   = 100;
  110.   const int cmExit    = 101;
  111.   const int cmSpace   = 102;
  112.   const int cmDialog1 = 103;
  113.  
  114.   //---------------- filename for streaming ---------------------
  115.  
  116.   const char *FILE_NAME = "strmdlg.sav";
  117.  
  118.  
  119.   //-------------------------------------------------------------
  120.   //     This is an example of a user defined Dialog box.
  121.   // Although     we inherit the streamable aspects of TDialog,
  122.   // we will need     to override several member functions
  123.   // inorder to provide     streamability for this new class.
  124.   //
  125.   //     NOTICE: The overridden WRITE, READ, BUILD, and
  126.   //       STREAMABLENAME member functions. We also
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  141.   VERSION  :  3.x
  142.        OS  :  DOS
  143.      DATE  :  October 7, 1993                          PAGE  :  3/8
  144.  
  145.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  146.  
  147.  
  148.  
  149.  
  150.   //       need the static NAME data member.
  151.   //
  152.   //-------------------------------------------------------------
  153.   class TMyDialog: public TDialog
  154.   {
  155.       public:
  156.  
  157.       char buffer[60];
  158.       int var1, var2, flag;
  159.  
  160.       virtual void write( opstream& os);
  161.       virtual void *read( ipstream& );
  162.  
  163.       static const char * const name;
  164.       static TStreamable *build();
  165.  
  166.       TMyDialog( const TRect& bounds, const char *aTitle) :
  167.       TDialog( bounds, aTitle),
  168.       TWindowInit( initFrame )
  169.       {
  170.           // my new data members
  171.           var1 = 37;
  172.           // will need to be streamed.
  173.           var2 = 99;
  174.           //turn off the move option.
  175.           flags ^= wfMove;
  176.       }
  177.  
  178.       // override the handle event
  179.       virtual void handleEvent( TEvent& );
  180.  
  181.       protected:
  182.  
  183.       TMyDialog( StreamableInit  ) :
  184.       TDialog( streamableInit ), TWindowInit( streamableInit ) {};
  185.  
  186.       private:
  187.  
  188.       virtual const char *streamableName() const
  189.       { return name; }
  190.   };
  191.  
  192.   /*-----------------------------------------------------------
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  207.   VERSION  :  3.x
  208.        OS  :  DOS
  209.      DATE  :  October 7, 1993                          PAGE  :  4/8
  210.  
  211.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  212.  
  213.  
  214.  
  215.  
  216.   /* Build a skeleton for our new object.
  217.   \*-----------------------------------------------------------*/
  218.   TStreamable *TMyDialog::build()
  219.   {
  220.       return new TMyDialog( streamableInit );
  221.   }
  222.  
  223.   /*----------------------------------------------------------
  224.    Notice how we write out the new data members of our class
  225.     (TMyDialog).
  226.   ----------------------------------------------------------*/
  227.   void TMyDialog::write( opstream& os)
  228.   {
  229.       TDialog::write( os );
  230.       os << var1 << var2;
  231.   }
  232.  
  233.   /*--------------------------------------------------------
  234.    Here we read in the new data members of our class(TMyDialog).
  235.    -----------------------------------------------------------*/
  236.   void *TMyDialog::read( ipstream& is )
  237.   {
  238.       TDialog::read( is );
  239.       is >> var1 >> var2;
  240.       return this;
  241.   }
  242.  
  243.   //------- initialize the static data member ----------------\\
  244.   const char * const TMyDialog::name = "TMyDialog";
  245.  
  246.   /*------------------------------------------------------------
  247.     Overload the insertion and extraction operators for our new
  248.     class( TMyDialog ).
  249.   \*-----------------------------------------------------------*/
  250.   inline ipstream& operator >> ( ipstream& is, TMyDialog& cl )
  251.   {    return is >> ( TStreamable& )cl;    }
  252.  
  253.   inline ipstream& operator >> ( ipstream& is, TMyDialog*& cl )
  254.   {    return is >> (void *&)cl;           }
  255.  
  256.   inline opstream& operator << ( opstream& os, TMyDialog& cl )
  257.   {    return os << ( TStreamable& )cl;    }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  273.   VERSION  :  3.x
  274.        OS  :  DOS
  275.      DATE  :  October 7, 1993                          PAGE  :  5/8
  276.  
  277.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  278.  
  279.  
  280.  
  281.  
  282.   inline opstream& operator << ( opstream& os, TMyDialog* cl )
  283.   {    return os << (TStreamable *)cl;     }
  284.  
  285.   //-------------- Register our new class explictly------------\\
  286.   TStreamableClass RMyDialog(  TMyDialog::name,
  287.   TMyDialog::build,
  288.   __DELTA(TMyDialog) );
  289.  
  290.   /*-----------------------------------------------------------
  291.    Overloaded handle event.
  292.   \*---------------------------------------------------------*/
  293.   void TMyDialog::handleEvent( TEvent& event )
  294.   {
  295.       if( event.what == evCommand )
  296.       {
  297.           switch( event.message.command )
  298.           {
  299.               case cmOK:
  300.               sprintf( buffer, "Var #1 is: %d  Var #2 is: %d",
  301.                      var1, var2);
  302.               messageBox( buffer, mfInformation | mfOKButton);
  303.               clearEvent( event );
  304.               break;
  305.           }
  306.       }
  307.       TDialog::handleEvent( event );
  308.   }
  309.  
  310.   class TMyApp : public TApplication
  311.   {
  312.       public:
  313.       TMyApp();
  314.       static TMenuBar *initMenuBar( TRect );
  315.       void handleEvent(TEvent& event);
  316.       void Dialog1(void);
  317.       void idle(void);
  318.       THeapView *heap;
  319.  
  320.   };
  321.  
  322.   TMyApp::TMyApp() :
  323.      TApplication(),
  324.      TProgInit( initStatusLine, initMenuBar, initDeskTop )
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  339.   VERSION  :  3.x
  340.        OS  :  DOS
  341.      DATE  :  October 7, 1993                          PAGE  :  6/8
  342.  
  343.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  344.  
  345.  
  346.  
  347.  
  348.   {
  349.       // Create the heap view.
  350.       TRect r = getExtent();
  351.       r.a.x = r.b.x - 13;     r.a.y = r.b.y - 1;
  352.       heap = new THeapView( r );
  353.       insert(heap);
  354.  
  355.   }
  356.  
  357.   void TMyApp::idle()
  358.   {
  359.       TProgram::idle();
  360.       if (heap != 0 )
  361.       {
  362.           heap->update();
  363.       }
  364.   }
  365.  
  366.   void TMyApp::Dialog1(void)
  367.   {
  368.       char *text =
  369.         "This Dialog will be streamed out to disk... then"\
  370.         " streamed back and executed again...";
  371.  
  372.       // make initial dialog..........
  373.       TMyDialog *pd = new TMyDialog( TRect( 5,1,75,20)
  374.                                           , "Streaming along...");
  375.  
  376.       // open output stream....check for valid
  377.       ofpstream of( FILE_NAME, ios::out | ios::binary );
  378.       if( !of )
  379.       {
  380.           messageBox("Error opening output file!", mfError );
  381.           return;
  382.       }
  383.  
  384.       // if valid insert buttons and execute
  385.       if( validView( pd ) )
  386.       {
  387.           pd->insert( new TStaticText( TRect(10,3,60, 15),
  388.                                             text) );
  389.           pd->insert( new TButton(TRect(10,14,25,16)
  390.                                        , "Variables", cmOK,
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  405.   VERSION  :  3.x
  406.        OS  :  DOS
  407.      DATE  :  October 7, 1993                          PAGE  :  7/8
  408.  
  409.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  410.  
  411.  
  412.  
  413.  
  414.                                        bfNormal) );
  415.           pd->insert( new TButton(TRect(10,10,25,12)
  416.                                        , "Continue", cmCancel,
  417.                                        bfDefault) );
  418.           deskTop->execView( pd );
  419.       }
  420.  
  421.       messageBox("Streaming Dialog to disk now...",
  422.                 mfInformation|mfOKButton);
  423.       of << pd;        // stream dialog out to disk
  424.  
  425.       of.close();      // close the output file
  426.       destroy(pd);     // destroy the dialog
  427.  
  428.       //--- stream the dialog back in from disk ----
  429.  
  430.       TMyDialog  *dg = NULL;          // pointer to a TMyDialog
  431.  
  432.       ifpstream ifps( FILE_NAME );       // open input stream
  433.       if( !ifps )                        // open correctly?
  434.       {
  435.           messageBox("Error opening input file!", mfError);
  436.           return ;
  437.       }
  438.  
  439.       messageBox("Streaming Dialog back from disk...",
  440.                 mfInformation|mfOKButton);
  441.       ifps >> dg;                     // stream in the dialog
  442.  
  443.       if( validView( dg ) )
  444.       {
  445.           // execute the dialog again
  446.           deskTop->execView( dg );
  447.       }
  448.       destroy(dg);
  449.   }
  450.  
  451.   /*------------------------------------------------------------
  452.   Application handleEvent...
  453.   \*-----------------------------------------------------------*/
  454.   void TMyApp::handleEvent(TEvent& event)
  455.   {
  456.       TApplication::handleEvent( event );
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.   PRODUCT  :  Borland C++                           NUMBER  :  1701
  471.   VERSION  :  3.x
  472.        OS  :  DOS
  473.      DATE  :  October 7, 1993                          PAGE  :  8/8
  474.  
  475.     TITLE  :  Turbo Vision: Streaming a user defined dialog
  476.  
  477.  
  478.  
  479.  
  480.       if( event.what == evCommand )
  481.       {
  482.           switch( event.message.command)
  483.           {
  484.               case cmDialog1:
  485.               Dialog1();
  486.               break;
  487.  
  488.               default:
  489.               break;
  490.           }
  491.           clearEvent( event );
  492.       }
  493.   }
  494.  
  495.   TMenuBar *TMyApp::initMenuBar( TRect r )
  496.   {
  497.       r.b.y = r.a.y + 1;
  498.  
  499.       TMenuItem *one =
  500.       new TMenuItem("~E~xit", cmQuit, kbAltX, hcNoContext, 0);
  501.  
  502.       TMenuItem *two =
  503.       new TMenuItem("~\xF0~", cmDialog1, kbAltSpace,
  504.                     hcNoContext,0, one);
  505.  
  506.       return new TMenuBar( r, new TMenu( *two ));
  507.   }
  508.  
  509.   int main()
  510.   {
  511.       TMyApp myApp;
  512.       myApp.run();
  513.       return 0;
  514.   }
  515.  
  516.  
  517.   DISCLAIMER: You have the right to use this technical information
  518.   subject to the terms of the No-Nonsense License Statement that
  519.   you received with the Borland product to which this information
  520.   pertains.
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.