home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 220 / MMPI.ZIP / USER.CPP < prev    next >
Text File  |  1992-03-22  |  15KB  |  599 lines

  1. // Turbo-vision definitions
  2. #define Uses_TApplication
  3. #define Uses_TDeskTop
  4. #define Uses_TWindow
  5. #define Uses_TView
  6. #define Uses_TKeys
  7. #define Uses_TRect
  8. #define Uses_TMenuBar
  9. #define Uses_TSubMenu
  10. #define Uses_TMenuItem
  11. #define Uses_TStatusLine
  12. #define Uses_TStatusItem
  13. #define Uses_TStatusDef
  14. #define Uses_TDialog
  15. #define Uses_TStaticText
  16. #define Uses_TButton
  17. #define Uses_MsgBox
  18. #define Uses_TFileDialog
  19. #define Uses_TChDirDialog
  20. #define Uses_TStreamable
  21. #define Uses_fpstream
  22. #define Uses_ipstream
  23. #include <tv.h>
  24. #include <help.h>
  25.  
  26. #include <String.h>
  27. #include <Fstream.h>
  28. #include <Dir.h>
  29. #include "FileView.h"
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "MMPI.HPP"
  35. #include "USER.HPP"
  36. #include "MARBLE.H"
  37.  
  38. TVTest::TVTest() :
  39.     TProgInit(
  40.         &TVTest::initStatusLine,
  41.         &TVTest::initMenuBar,
  42.         &TVTest::initDeskTop
  43.     )
  44. {
  45.    TRect r = getExtent();
  46.    r.a.x = r.b.x - 9;      r.b.y = r.a.y + 1;
  47.    clock = new TClockView( r );
  48.    insert(clock);
  49.     titleWin();
  50. }
  51.  
  52. TVTest::~TVTest()
  53. {
  54.    delete clock;
  55. }
  56.  
  57. // Is a window tileable??
  58. static Boolean isTileable(TView *p, void * )
  59. {
  60.    if( (p->options & ofTileable) != 0)
  61.        return True;
  62.    else
  63.        return False;
  64. }
  65.  
  66. void TVTest::idle()
  67. {
  68.    TProgram::idle();
  69.    clock->update();
  70.    if (deskTop->firstThat(isTileable, 0) != 0 ) {
  71.       enableCommand(cmTile);
  72.       enableCommand(cmCascade); 
  73.       }
  74.    else {
  75.       disableCommand(cmTile);
  76.       disableCommand(cmCascade);
  77.       }
  78.    if (theTest.isEmpty()) {
  79.       disableCommand(cmSaveTest);
  80.       disableCommand(cmReportTest);
  81.       }
  82.    else {
  83.       enableCommand(cmSaveTest);
  84.       enableCommand(cmReportTest);
  85.       }
  86. }
  87.  
  88. TStatusLine *TVTest::initStatusLine(TRect r)
  89. {
  90.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  91.     return new TStatusLine( r,
  92.         *new TStatusDef( 0, 0xFFFF ) +
  93.         // set range of help contexts
  94.             *new TStatusItem( 0, kbF10, cmMenu ) +
  95.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  96.             *new TStatusItem( "~F1~ Help", kbF1, cmHelp ) +
  97.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose ) +
  98.             *new TStatusItem( "~F9~ Take Test", kbF9, cmTakeTest )
  99.         );
  100. }
  101.  
  102. TMenuBar *TVTest::initMenuBar( TRect r )
  103. {
  104.  
  105.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  106.     return new TMenuBar( r,
  107.         *new TSubMenu( "~H~elp", kbAltH )+
  108.             *new TMenuItem( "The Marble Counter",    cmCopyrights, kbNoKey, hcNoContext, "" )+
  109.             *new TMenuItem( "The MMPI",                  cmTheTest,    kbNoKey, hcNoContext, "" )+
  110.             *new TMenuItem( "The Psychotic Menace",cmTheMenace,  kbNoKey, hcNoContext, "" )+
  111.             *new TMenuItem( "To The Paranoid",        cmForTheParanoid,kbNoKey, hcNoContext, "" )+
  112.            newLine()+
  113.             *new TMenuItem( "Q  - Question",            cmWhatIs_Q,        kbNoKey,    hcNoContext,"")+
  114.             *new TMenuItem( "F  - Validity",            cmWhatIs_F,        kbNoKey,    hcNoContext,"")+
  115.             *new TMenuItem( "K  - Correction",        cmWhatIs_K,        kbNoKey,    hcNoContext,"")+
  116.             *new TMenuItem( "Hs - Hypochondriasis",cmWhatIs_Hs,    kbNoKey,    hcNoContext,"")+
  117.             *new TMenuItem( "D  - Depression",        cmWhatIs_D,        kbNoKey,    hcNoContext,"")+
  118.             *new TMenuItem( "Hy - Hysteria",            cmWhatIs_Hy,    kbNoKey,    hcNoContext,"")+
  119.             *new TMenuItem( "Pd - Psychopathic deviant",    cmWhatIs_Pd,    kbNoKey,    hcNoContext,"")+
  120.             *new TMenuItem( "Mf - Masculinity/Femininity",  cmWhatIs_MfM,    kbNoKey,    hcNoContext,"")+
  121.             *new TMenuItem( "Pa - Paranoia",            cmWhatIs_Pa,    kbNoKey,    hcNoContext,"")+
  122.             *new TMenuItem( "Pt - Psychasthenia",    cmWhatIs_Pt,    kbNoKey,    hcNoContext,"")+
  123.             *new TMenuItem( "Sc - Schizophrenia",    cmWhatIs_Sc,    kbNoKey,    hcNoContext,"")+
  124.             *new TMenuItem( "Ma - Hypomania",        cmWhatIs_Ma,    kbNoKey,    hcNoContext,"")+
  125.             *new TMenuItem( "Si - Social Introversion",    cmWhatIs_Si,    kbNoKey,    hcNoContext,"")+
  126.             *new TMenuItem( "L  - Lie",                cmWhatIs_L,        kbNoKey,    hcNoContext,"")+
  127.  
  128.         *new TSubMenu( "~F~ile", kbAltF )+
  129.            *new TMenuItem( "~C~hange Directory", cmChangeDir, kbNoKey, hcNoContext, "" )+
  130.            *new TMenuItem( "~L~oad Test",    cmLoadTest,        kbF3, hcNoContext, "F3" )+
  131.            *new TMenuItem( "~S~ave Test",    cmSaveTest,        kbF4, hcNoContext, "F4" )+
  132.            *new TMenuItem( "~R~eport Test",    cmReportTest,    kbF6,hcNoContext,  "F2" )+
  133.            newLine()+
  134.            *new TMenuItem( "E~x~it",            cmQuit,    kbAltX, hcNoContext, "Alt-X" )+
  135.  
  136.         *new TSubMenu( "~T~est", kbAltT )+
  137.            *new TMenuItem( "~T~ake Test",     cmTakeTest, kbF9,     hcNoContext, "F9" )+
  138.            *new TMenuItem( "~P~ractice Test",cmCheatTest,kbAltF9, hcNoContext, "Alt-F9" )+
  139.  
  140.        *new TSubMenu( "~W~indows", 0, hcNoContext ) +
  141.            *new TMenuItem( "~R~esize/move", cmResize, kbCtrlF5, hcNoContext, "Ctrl-F5" ) +
  142.            *new TMenuItem( "~Z~oom", cmZoom, kbF5, hcNoContext, "F5" ) +
  143.            *new TMenuItem( "~N~ext", cmNext, kbF6, hcNoContext, "F6" ) +
  144.            *new TMenuItem( "~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3" ) +
  145.            *new TMenuItem( "~T~ile", cmTile, kbNoKey, hcNoContext ) +
  146.            *new TMenuItem( "C~a~scade", cmCascade, kbNoKey, hcNoContext )
  147.         );
  148. }
  149.  
  150. void TVTest::handleEvent( TEvent& event )
  151. {
  152.     
  153.     TApplication::handleEvent( event );
  154.    if( event.what == evCommand ) {
  155.        switch( event.message.command ) {
  156.               
  157.             case cmChangeDir:
  158.                 changeDir();
  159.             clearEvent(event);
  160.                 break;
  161.             
  162.             case cmLoadTest:
  163.                 loadAnswers();
  164.             clearEvent(event);
  165.             break;
  166.             
  167.             case cmSaveTest:
  168.                 saveAnswers();
  169.             clearEvent(event);
  170.                 break;
  171.         
  172.           case cmReportTest:
  173.             reportTest();
  174.             clearEvent(event);
  175.             break;
  176.          
  177.          case cmTakeTest:
  178.                 TakeTest(0);
  179.             clearEvent(event);
  180.                 break;
  181.               
  182.             case cmCheatTest:
  183.                 TakeTest(1);
  184.             clearEvent(event);
  185.                 break;
  186.  
  187.          case cmTile:
  188.             deskTop->tile( deskTop->getExtent() );
  189.             break;
  190.  
  191.          case cmCascade:
  192.             deskTop->cascade( deskTop->getExtent() );
  193.             break;
  194.             
  195.          case cmHelp:
  196.                 HelpMe(event, getHelpCtx());
  197.                 break;
  198.             
  199.             case cmWhatIs_Q:
  200.                 HelpMe(event, hcWhatIs_Q);
  201.                 break;
  202.             case cmWhatIs_F:
  203.                 HelpMe(event, hcWhatIs_F);
  204.                 break;
  205.             case cmWhatIs_K:
  206.                 HelpMe(event, hcWhatIs_K);
  207.                 break;
  208.             case cmWhatIs_Hs:
  209.                 HelpMe(event, hcWhatIs_Hs);
  210.                 break;
  211.             case cmWhatIs_D:
  212.                 HelpMe(event, hcWhatIs_D);
  213.                 break;
  214.             case cmWhatIs_Hy:
  215.                 HelpMe(event, hcWhatIs_Hy);
  216.                 break;
  217.             case cmWhatIs_Pd:
  218.                 HelpMe(event, hcWhatIs_Pd);
  219.                 break;
  220.             case cmWhatIs_MfM:
  221.                 HelpMe(event, hcWhatIs_Mf);
  222.                 break;
  223.             case cmWhatIs_Pa:
  224.                 HelpMe(event, hcWhatIs_Pa);
  225.                 break;
  226.             case cmWhatIs_Pt:
  227.                 HelpMe(event, hcWhatIs_Pt);
  228.                 break;
  229.             case cmWhatIs_Sc:
  230.                 HelpMe(event, hcWhatIs_Sc);
  231.                 break;
  232.             case cmWhatIs_Ma:
  233.                 HelpMe(event, hcWhatIs_Ma);
  234.                 break;
  235.             case cmWhatIs_Si:
  236.                 HelpMe(event, hcWhatIs_Si);
  237.                 break;
  238.             case cmWhatIs_L:
  239.                 HelpMe(event, hcWhatIs_L);
  240.                 break;
  241.             
  242.             case cmCopyrights:
  243.                 HelpMe(event, hcNocontext);
  244.                 break;
  245.             case cmTheTest:
  246.                 HelpMe(event, hcMMPI);
  247.                 break;
  248.             case cmTheMenace:
  249.                 HelpMe(event, hcTheMenace);
  250.                 break;
  251.             case cmForTheParanoid:
  252.                 HelpMe(event, hcParanoidPeople);
  253.                 break;
  254.  
  255.             default:
  256.                 break;
  257.          }
  258.       }
  259. }
  260.  
  261. void TVTest::changeDir()
  262. {
  263.     TView *d = validView( new TChDirDialog( 0, cmChangeDir ) );
  264.     if( d != 0 )
  265.         {
  266.         deskTop->execView( d );
  267.         destroy( d );
  268.         }
  269. }
  270.  
  271. // Read 
  272. void TVTest::loadAnswers()
  273. {
  274.    
  275.    if( !theTest.isEmpty() &&
  276.       (messageBox("The test already in RAM will be lost. Continue?", mfInformation | mfYesButton | mfNoButton ) != cmYes) )
  277.      return;
  278.  
  279.    TFileDialog *d= (TFileDialog *)validView(
  280.         new TFileDialog( "*.ANS", "Load Test Answers", "~N~ame", fdOpenButton, 100 ));
  281.     
  282.     if( d != 0 && deskTop->execView( d ) != cmCancel ) {
  283.        char fileName[MAXPATH];
  284.    
  285.         d->getFileName( fileName );
  286.    
  287.         if(theTest.loadAnswers(fileName)) {
  288.          if(theTest.isNotComplete())
  289.                 TakeTest(0);
  290.          else
  291.               if(theTest.ResultForm())
  292.                showTest();
  293.          }
  294.       else
  295.          messageBox( "Could not read ANSwer file.", mfError | mfOKButton );
  296.         }
  297.     destroy( d );
  298. }
  299.  
  300. void TVTest::saveAnswers()
  301. {
  302.     TFileDialog *d= (TFileDialog *)validView(
  303.         new TFileDialog( "*.ANS", "Save Test Answers", "~N~ame", fdOKButton, 100 ));
  304.     
  305.     if( d != 0 && deskTop->execView( d ) != cmCancel ) {
  306.        char fileName[MAXPATH];
  307.    
  308.         d->getFileName( fileName );
  309.     
  310.         if(!theTest.saveAnswers(fileName))
  311.          messageBox( "Could not write ANSwer file.", mfError | mfOKButton );
  312.         }
  313.     destroy( d );
  314. }
  315.  
  316. // Run through the test questions
  317. void TVTest::TakeTest(int CHEAT)
  318. {
  319.     MMPIQuestion *current;
  320.     int answer;
  321.  
  322.     if(theTest.isEmpty()) {
  323.       theTest.clearAnswers();
  324.        current = theTest.first();
  325.       }
  326.    else {
  327.         // Find the first unanswered question
  328.         current = theTest.isNotComplete();
  329.         if( current ) {
  330.             if(messageBox("There is a partial test in RAM.\nResume this test?", mfInformation | mfYesButton | mfNoButton ) == cmYes )
  331.                 current = theTest.current();
  332.          else {
  333.             theTest.clearAnswers();
  334.                 current = theTest.first();
  335.                 }
  336.          }
  337.         else {
  338.             if(messageBox("The test already in RAM will be lost. Continue?", mfInformation | mfYesButton | mfNoButton ) == cmYes ) {
  339.             theTest.clearAnswers();
  340.              current = theTest.first();
  341.             }
  342.          else
  343.             return;
  344.          }
  345.       }
  346.  
  347.    if( !theTest.gender )
  348.       switch(GetGender()) {
  349.          case cmYes:
  350.             theTest.gender=MALE;
  351.             break;
  352.          case cmNo:
  353.             theTest.gender=FEMALE;
  354.             break;
  355.          default:
  356.             return;
  357.       };
  358.  
  359.     if(current) {
  360.         do {
  361.             answer = ask(current,CHEAT);
  362.             switch(answer) {
  363.                 case cmCancel:
  364.                     switch(abortTest()) {
  365.                         case cmNo:
  366.                              if( !(current=theTest.prev()) )
  367.                         current=theTest.first();
  368.                      break;
  369.                         case cmYes:
  370.                          if( !theTest.isEmpty() &&
  371.                          (messageBox( "You may resume this test by saving it now, then loading it at another time.\n\3Save it now?", mfInformation | mfYesButton | mfNoButton ) == cmYes)
  372.                        )
  373.                         saveAnswers();
  374.                      return;
  375.                         default:
  376.                             break;
  377.                         }
  378.                      break;
  379.                 case cmYes:
  380.                     current->response = 'T';
  381.                     current=theTest.next();
  382.                     break;
  383.                 case cmNo:
  384.                     current->response = 'F';
  385.                     current=theTest.next();
  386.                     break;
  387.  
  388.                 }
  389.         } while(current);
  390.         theTest.saveAnswers("BACKUP.ANS");
  391.         if(theTest.ResultForm())
  392.             showTest();
  393.     }
  394. }
  395.  
  396. // Put up an ABORT dialog
  397. int TVTest::GetGender()
  398. {
  399.  
  400.     TDialog dial(TRect(0,0,25,9), "Subject Gender" );
  401.     dial.options|= (ofCenterX | ofCenterY);
  402.     TButton *nb;
  403.     
  404.     nb=new TButton( TRect( 0, 5, 15, 7 ), "FEMALE", cmYes, bfNormal );
  405.         nb->options|= ofCenterX;
  406.         dial.insert(nb);
  407.     nb=new TButton( TRect( 0, 2, 15, 4 ), "MALE", cmNo,bfNormal );
  408.         nb->options|= ofCenterX;
  409.         dial.insert(nb);
  410.  
  411.     return(deskTop->execView( &dial ));
  412. }
  413.  
  414.  
  415. // Put up an ABORT dialog
  416. int TVTest::abortTest()
  417. {
  418.  
  419.     TDialog dial(TRect(0,0,25,11), "Test Options" );
  420.     dial.options|= (ofCenterX | ofCenterY);
  421.     TButton *nb;
  422.     
  423.     nb=new TButton( TRect( 0, 2, 15, 4 ), "Back Up", cmNo,bfNormal );    
  424.         nb->options|= ofCenterX;
  425.         dial.insert(nb);
  426.     nb=new TButton( TRect( 0, 5, 15, 7 ), "Stop Test", cmYes, bfNormal );
  427.         nb->options|= ofCenterX;
  428.         dial.insert(nb);
  429.     nb=new TButton( TRect( 0, 8, 15, 10 ), "Continue", cmCancel, bfNormal );
  430.         nb->options|= ofCenterX;
  431.         dial.insert(nb);
  432.  
  433.     return(deskTop->execView( &dial ));
  434. }
  435.  
  436.  
  437. // Put up a question dialog
  438.  
  439. int TVTest::ask(MMPIQuestion *q, int CHEAT)
  440. {
  441.     static char scales[128];
  442.     static char scaleFormat[16];
  443.    int c;
  444.      char scale;
  445.     TDialog dial(TRect( 0, 0, 70, 11), "MMPI Form R" );
  446.     dial.options|= (ofCenterX | ofCenterY);
  447.     
  448.     
  449.     if(CHEAT) {
  450.         strcpy(scales, "Scales: ");
  451.         for(c=0;c<NUM_SCALES;++c) {
  452.             scale = q->getScale(c);
  453.             if(scale != ' ') {
  454.                 sprintf(scaleFormat, "%s=%s ", aScale[c], (scale=='T')?"TRUE":"FALSE");
  455.                 strcat(scales,scaleFormat);
  456.                 }
  457.             }
  458.         dial.insert( new TStaticText( TRect( 5, 1, 65, 2 ), scales ) );
  459.         }
  460.     
  461.     TButton *bTrue;
  462.     TButton *bFalse;
  463.     TStaticText *textI;
  464.     
  465.     textI = new TStaticText( TRect( 5, 2, 65, 5 ), q->text );
  466.     bTrue = new TButton( TRect( 5, 6, 30, 10 ), "~T~RUE",  cmYes, bfNormal );
  467.     bFalse = new TButton( TRect( 40, 6, 65, 10 ), "~F~ALSE", cmNo, bfNormal );
  468.     
  469.     dial.insert(textI);
  470.     
  471.     if(q->response == 'F') {
  472.         dial.insert(bTrue);
  473.         dial.insert(bFalse);
  474.         }
  475.     else {
  476.         dial.insert(bFalse);
  477.         dial.insert(bTrue);
  478.         }    
  479.     return(deskTop->execView( &dial ));
  480. }
  481.  
  482. void TVTest::titleWin()
  483. {
  484.    messageBox(
  485.         "\3The Minnesota Marble Counter 1.1\n\n\3The Psychotic Menace\n \n\3April 1, 1992",
  486.         mfInformation | mfOKButton
  487.         );
  488. }
  489.  
  490. void TVTest::HelpMe(TEvent& event, int how)
  491. {
  492.     TWindow *w;
  493.     THelpFile *hFile;
  494.     fpstream *helpStrm;
  495.     static int helpInUse = False;
  496.  
  497.    clearEvent(event);
  498.     
  499.    if(!helpInUse) {
  500.         helpInUse = True;
  501.         helpStrm = new fpstream("MARBLE.HLP", ios::in|ios::binary);
  502.         hFile = new THelpFile(*helpStrm);
  503.         if (!helpStrm) {
  504.             messageBox("Could not open MARBLE.HLP", mfError | mfOKButton);
  505.             delete hFile;
  506.             }
  507.         else {
  508.             w = new THelpWindow(hFile, how);
  509.             if (validView(w) != 0) {
  510.                 execView( w );
  511.                 destroy( w );
  512.               }
  513.             else
  514.                 delete w;
  515.            }
  516.         helpInUse = False;
  517.          }
  518. }
  519.  
  520. void TVTest::reportTest()
  521. {
  522.     TFileDialog *d= (TFileDialog *)validView(
  523.         new TFileDialog( "*.RPT", "Report Test Results", "~N~ame", fdOKButton, 100 ));
  524.     
  525.     if( d != 0 && deskTop->execView( d ) != cmCancel ) {
  526.        char fileName[MAXPATH];
  527.    
  528.         d->getFileName( fileName );
  529.           if(!theTest.ResultForm(fileName))
  530.             messageBox( "Could not write Report.", mfError | mfOKButton );
  531.       }
  532.     destroy( d );
  533. }
  534.  
  535.  
  536. // Display a test result file
  537. void TVTest::showTest(char *fName)
  538. {
  539.    TView *vWin= validView( new TFileWindow( fName ) );
  540.    if( vWin != 0 )
  541.       deskTop->insert(vWin);
  542.  
  543.  
  544.  
  545. //
  546. // -------------- Clock Viewer functions
  547. //
  548.  
  549. TClockView::TClockView( TRect& r ) : TView( r )
  550. {
  551.     strcpy(lastTime, "        ");
  552.     strcpy(curTime, "        ");
  553. }
  554.  
  555.  
  556. void TClockView::draw()
  557. {
  558.     TDrawBuffer buf;
  559.     char c = getColor(2);
  560.  
  561.     buf.moveChar(0, ' ', c, size.x);
  562.     buf.moveStr(0, curTime, c);
  563.     writeLine(0, 0, size.x, 1, buf);
  564. }
  565.  
  566.  
  567. void TClockView::update()
  568. {
  569.     time_t t = time(0);
  570.     char *date = ctime(&t);
  571.  
  572.     date[19] = '\0';
  573.     strcpy(curTime, &date[11]);        /* Extract time. */
  574.  
  575.     if( strcmp(lastTime, curTime) )
  576.         {
  577.         drawView();
  578.         strcpy(lastTime, curTime);
  579.         }
  580. }
  581.  
  582.  
  583.  
  584.  
  585. // Ah, what a fine spot to put main
  586.  
  587. void main()
  588. {
  589.     TVTest test;
  590.     
  591.    if(test.theTest.size < 1)
  592.       messageBox( "FATAL:\n \n   Could not read TEST.DAT!", mfError | mfOKButton );
  593.    else
  594.       test.run();
  595. }
  596.  
  597.  
  598.