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

  1. /*-------------------------------------------------------*/
  2. /*                                                       */
  3. /*   Turbo Vision 1.0                                    */
  4. /*   Turbo Vision Forms Demo                             */
  5. /*   Copyright (c) 1991 by Borland International         */
  6. /*                                                       */
  7. /*   Forms.cpp: Support source file for TVFORMS demo     */
  8. /*-------------------------------------------------------*/
  9.  
  10. #define Uses_TKeys
  11. #define Uses_TEvent
  12. #define Uses_TRect
  13. #define Uses_TDialog
  14. #define Uses_TStreamableClass
  15. #define Uses_MsgBox
  16. #include <tv.h>
  17. __link( RDialog )
  18. __link( RView )
  19.  
  20. #if !defined( __FORMS_H )
  21. #include "Forms.h"
  22. #endif  // __FORMS_H
  23.  
  24. #if !defined( __LISTDLG_H )
  25. #include "Listdlg.h"
  26. #endif  // __LISTDLG_H
  27.  
  28. #if !defined( __FORMCMDS_H )
  29. #include "Formcmds.h"
  30. #endif  // __FORMCMDS_H
  31.  
  32. #if !defined( __STRING_H )
  33. #include <string.h>
  34. #endif  // __STRING_H
  35.  
  36. // Compares two buffers and returns True if contents are equal
  37.  
  38. Boolean compBlocks( void *buf1, void *buf2, ushort bufSize )
  39. {
  40.     return Boolean(memcmp( buf1, buf2, bufSize ) == 0);
  41. }
  42.  
  43. const char * const TForm::name = "TForm";
  44.  
  45. void TForm::write( opstream& os )
  46. {
  47.  
  48.     TDialog::write( os );
  49.     os << keyWidth;
  50.     
  51. }
  52.  
  53. void *TForm::read( ipstream& is )
  54. {
  55.  
  56.     TDialog::read( is );
  57.     is >> keyWidth;
  58.     return this;
  59. }
  60.  
  61. TStreamable *TForm::build()
  62. {
  63.     return new TForm( streamableInit );
  64. }
  65.  
  66.  
  67. TStreamableClass RForm( TForm::name,
  68.                         TForm::build,
  69.                         __DELTA(TForm)
  70.                       );
  71.  
  72. TForm::TForm( const TRect& bounds, const char *aTitle) :
  73.        TDialog( bounds, aTitle),
  74.        TWindowInit(&TForm::initFrame)
  75. {
  76. }
  77.  
  78. Boolean TForm::changed()
  79. {
  80.     void *curData;
  81.     ushort compSize;
  82.     Boolean newForm, result;
  83.  
  84.     compSize = dataSize();
  85.     curData = new char[compSize];
  86.     getData(curData);
  87.     if (prevData == NULL)
  88.     newForm = True;
  89.     else
  90.     newForm = False;
  91.  
  92.     if (newForm)
  93.     {
  94.     // Dummy up empty record for comparison
  95.     prevData = new char[compSize];
  96.     memset(prevData, 0, compSize);
  97.     }
  98.     if (compBlocks(prevData, curData, compSize))
  99.         result = False;
  100.     else
  101.         result = True;
  102.  
  103.     delete (curData);
  104.     if (newForm)
  105.     {
  106.     delete(prevData);
  107.     prevData = NULL;
  108.     }
  109.     if (result)
  110.     return True;
  111.     else
  112.     return False;
  113. }
  114.  
  115. void TForm::handleEvent( TEvent& event)
  116. {
  117.     // Respond to CANCEL button and ESC
  118.     if ( ( (event.what == evKeyDown) && (event.keyDown.keyCode == kbEsc) ) ||
  119.        ( (event.what == evCommand) && (event.message.command == cmCancel) ) )
  120.     {
  121.     clearEvent(event);
  122.     destroy(this);
  123.     return;
  124.     }
  125.  
  126.     // Respond to SAVE button
  127.     if ( (event.what == evCommand) && (event.message.command == cmFormSave) )
  128.     {
  129.     clearEvent(event);
  130.     if (changed() == True)
  131.         {
  132.         if (((TListDialog *)listDialog)->saveForm(this))
  133.         {
  134.         destroy(this);
  135.         return;
  136.         }
  137.             }
  138.     else
  139.         {
  140.         destroy(this);                        // not changed
  141.         return;
  142.         }
  143.     }
  144.  
  145.     TDialog::handleEvent(event);
  146.  
  147.     // Respond to TopForm messages
  148.     if (event.what == evBroadcast)
  149.     if (event.message.command == cmEditingForm)
  150.         {
  151.         // Already editing broadcast form?
  152.         if ( (prevData != NULL) && (event.message.infoPtr == prevData) )
  153.         clearEvent(event);
  154.         }
  155.     else
  156.         // Belong to sending ListDialog?
  157.         if (listDialog == event.message.infoPtr)
  158.         {
  159.         if (event.message.command == cmTopForm)
  160.             clearEvent(event);
  161.         else if (event.message.command == cmCanCloseForm)
  162.             {
  163.             if (!valid(cmClose))
  164.             clearEvent(event);
  165.             }
  166.         else if (event.message.command == cmCloseForm)
  167.             destroy(this);
  168.        }
  169. }
  170.  
  171. Boolean TForm::valid(ushort command)
  172. {
  173.     ushort action;
  174.  
  175.     action = cmYes;                    // assume calling inherited
  176.     if (command == cmClose)
  177.     if (changed())
  178.         {
  179.         select();
  180.         action = messageBox("Form data has been modified. Save? ",
  181.                 mfYesNoCancel);
  182.         switch (action)
  183.         {
  184.         case cmYes:
  185.         // Try to save changes. Cancel if save fails
  186.             if (!((TListDialog *)listDialog)->saveForm(this))
  187.             action = cmCancel;
  188.             break;
  189.         case cmNo:
  190.             break;                // abandon changes
  191.         default :
  192.             action = cmCancel;            // cancel close request
  193.         }
  194.         }
  195.     else
  196.         action = cmNo;                        // no changes
  197.   if (action == cmYes)
  198.       return TDialog::valid(command);
  199.   else
  200.       {
  201.       if (action != cmCancel)
  202.       return True;
  203.       else
  204.       return False;
  205.  
  206.       }
  207. }
  208.