home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dokpr1.zip / sampapp / sampApp.cpp next >
Text File  |  1995-11-09  |  9KB  |  387 lines

  1. /*
  2. sampApp.cpp
  3. */
  4.  
  5. #include "sampApp.h"
  6. #include "sampcont.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <memory.h>
  10. #include <string.h>
  11. #include "feed.h"
  12. #include "mesamod.h"
  13. #include "mesaview.h"
  14. #include "rect.h"
  15. #include "except.h"
  16. #include "filestr.h"
  17. #include "sheetv.h"
  18.  
  19. static    char *stockNames[ 5 ];
  20. static     float prices[ 5 ];
  21.  
  22. static    char address[ 128 ];
  23. static    char contents[ 512 ];
  24.  
  25. MSampleController *MyMesa;
  26. MMesaModel *theModel,*theModel2;
  27. MMesaView *theView,*theView2;
  28.  
  29. //
  30. // sample dialog procedure
  31. //
  32.  
  33. static MRESULT EXPENTRY sampProc(HWND hw,ULONG msg,MPARAM mp1,MPARAM mp2)
  34. {
  35.     MRect tre,tre2;
  36.     SWP swp;
  37.     HWND hwnd;
  38.     MAddress ad,a2;
  39.     MException me;
  40.     MSheetView *sv;
  41.     static char *titles[] = {"Stock","Current Bid Price"};
  42.     MRange ra;
  43.     MFormat fo;
  44.     
  45.     switch( msg ) {
  46.     case WM_INITDLG:
  47.         WinSendMsg( hw, MEM_UPDATESCREEN, NULL, NULL );
  48.  
  49.         // clear the address/contents fields...
  50.         address[ 0 ] = 0;
  51.         contents[ 0 ] = 0;
  52.  
  53.         WinSetWindowText( WinWindowFromID( hw, SAMPAPP_ADDRESS ), address );
  54.         WinSetWindowText( WinWindowFromID( hw, SAMPAPP_CONTENTS ), contents );
  55.         
  56.         // initialize the buttons...        
  57.         WinEnableWindow( WinWindowFromID( hw, DID_RANDOM ), 1 );
  58.         WinEnableWindow( WinWindowFromID( hw, DID_ENDWALK ), 0 );
  59.         
  60.         hwnd = WinWindowFromID(hw,SAMPAPP_VIEW);
  61.         WinQueryWindowPos(hwnd,&swp);
  62.         
  63.         tre.setX(swp.x);
  64.         tre.setY(swp.y);
  65.         tre.setWid(swp.cx);
  66.         tre.setHi(swp.cy);
  67.         
  68.         tre2 = tre;
  69.         tre.setHi(tre.getHi() / 2);
  70.         // tre.round();
  71.         tre.offsetY(tre.getHi());
  72.         tre2.setHi(tre.getHi());
  73.  
  74.         theModel = new MMesaModel;
  75.         
  76.         theModel -> setUp(MyMesa);
  77.         
  78.         ad.set(0,0,0);
  79.         theModel -> setCellToString(&ad,"HOPII",0,&me,NULL);
  80.         ad.set(1,0,0);
  81.         theModel -> setCellToString(&ad,"STONE",0,&me,NULL);
  82.         ad.set(2,0,0);
  83.         theModel -> setCellToString(&ad,"MESAC",0,&me,NULL);
  84.         ad.set(3,0,0);
  85.         theModel -> setCellToString(&ad,"CARGZ",0,&me,NULL);
  86.         ad.set(4,0,0);
  87.         theModel -> setCellToString(&ad,"ARCSD",0,&me,NULL);
  88.  
  89.         ad.set(0,1,0);
  90.         theModel -> setCellToString(&ad,"=FEED(\"Bid\",A1)",0,&me,NULL);
  91.         ad.set(1,1,0);
  92.         theModel -> setCellToString(&ad,"=SAME($B$1)",0,&me,NULL);
  93.         ad.set(2,1,0);
  94.         theModel -> setCellToString(&ad,"=SAME($B$1)",0,&me,NULL);
  95.         ad.set(3,1,0);
  96.         theModel -> setCellToString(&ad,"=SAME($B$1)",0,&me,NULL);
  97.         ad.set(4,1,0);
  98.         theModel -> setCellToString(&ad,"=SAME($B$1)",0,&me,NULL);
  99.         
  100.         ra.init();
  101.         ad.set(0,1,0);
  102.         a2.set(4,1,0);
  103.         ra.set(&ad,&a2);
  104.         fo.init();
  105.         fo.setMergePrecision(1);
  106.         fo.setMergeFormat(1);
  107.         fo.setFormat(currencyTypeFormat |
  108.             currencySubtypeFormat | 2);
  109.         
  110.         theModel -> setRangeToFormat(&ra,&fo,defaultAction,NULL,&me);
  111.         
  112.         fo.free();
  113.         ra.free();
  114.         
  115.         theView = new MMesaView(0,theModel);
  116.         theView -> setHWND( hw );
  117.         theView -> setUp(&tre);
  118.         
  119.         sv = theView -> getSheetView();
  120.         
  121.         sv -> setColumnTitleHeight(36);
  122.         sv -> setColumnTitles((const char **) titles,2);
  123.  
  124.  
  125.         theModel2 = new MMesaModel;
  126.         
  127.         theModel2 -> setUp(MyMesa);
  128.         
  129.         theView2 = new MMesaView(0,theModel2);
  130.         theView2 -> setHWND( hw );
  131.         theView2 -> setUp(&tre2);
  132.         theModel2 -> setPathName("examples\\portflio.m2",0);
  133.         theModel2 -> loadFromPath(0,&me);
  134.  
  135.         
  136.         break;
  137.         
  138.     case WM_COMMAND:
  139.         {
  140.             int control = SHORT1FROMMP( mp1 );
  141.             int notify = SHORT2FROMMP( mp1 );
  142.             
  143.             switch( control ) {
  144.             case DID_RANDOM:
  145.                 WinStartTimer( WinQueryAnchorBlock( hw ), hw, ID_TIMER, 1000 );
  146.                 // turn off the button
  147.                 WinEnableWindow( WinWindowFromID( hw, DID_RANDOM ), 0 );
  148.                 WinEnableWindow( WinWindowFromID( hw, DID_ENDWALK ), 1 );
  149.                 break;
  150.                 
  151.             case DID_ENDWALK:
  152.                 WinStopTimer( WinQueryAnchorBlock( hw ), hw, ID_TIMER );
  153.                 // turn off the button
  154.                 WinEnableWindow( WinWindowFromID( hw, DID_RANDOM ), 1 );
  155.                 WinEnableWindow( WinWindowFromID( hw, DID_ENDWALK ), 0 );
  156.                 break;
  157.                 
  158.             case DID_EXIT:
  159.                 WinDismissDlg( hw, 0 );
  160.                 return( ( MRESULT )1 );
  161.                 
  162.             case DID_LOADTEXT:
  163.                 {
  164.                     MFileStream *mfs;
  165.                     
  166.                     mfs = new MFileStream(MSTREAM_READ,"sample.txt");
  167.                     if (!mfs -> getError())
  168.                     {
  169.                         MAddress ad;
  170.                         MException me;
  171.                         
  172.                         ad.set(3,3,0);
  173.                         
  174.                         theModel -> importText(mfs,&ad,0,NULL,&me);
  175.                         
  176.                         MyMesa -> displayException(&me);
  177.                         MyMesa -> displayException(mfs -> getException());
  178.                     }
  179.                     
  180.                     delete mfs;
  181.                 }
  182.                 return ((MRESULT) 1);
  183.             
  184.             default:
  185.                 return WinDefDlgProc( hw, msg, mp1, mp2 );
  186.             }
  187.         
  188.         }    // the WM_COMMAND message
  189.         break;
  190.     
  191.     case WM_CONTROL:
  192.         {
  193.             int control = SHORT1FROMMP( mp1 );
  194.             int notify = SHORT2FROMMP( mp1 );
  195.             
  196.             switch( control ) {
  197.                 case SAMPAPP_CONTENTS:
  198.                     if( notify == EN_KILLFOCUS )
  199.                     {
  200.                         int len;
  201.                         
  202.                         hwnd = WinWindowFromID(hw, SAMPAPP_ADDRESS);
  203.                         len = WinQueryWindowTextLength(hwnd);
  204.                         WinQueryWindowText(hwnd,len + 1,(PSZ) address);
  205.                         address[len + 1] = 0;
  206.                         
  207.                         hwnd = WinWindowFromID(hw, SAMPAPP_CONTENTS);
  208.                         len = WinQueryWindowTextLength(hwnd);
  209.                         WinQueryWindowText(hwnd,len + 1,(PSZ) contents);
  210.                         contents[len + 1] = 0;
  211.                         WinSetWindowText( WinWindowFromID( hw, SAMPAPP_ADDRESS ), (PSZ) "" );
  212.                         WinSetWindowText( WinWindowFromID( hw, SAMPAPP_CONTENTS ), (PSZ) "" );
  213.                         pasteIntoMesa( address, contents );
  214.                     }
  215.                         
  216.                     break;
  217.                 
  218.                 default:
  219.                     return WinDefDlgProc( hw, msg, mp1, mp2 );        
  220.             }    // end of control...
  221.         
  222.         }    // end of WM_CONTROL section;
  223.         break;
  224.             
  225.     case WM_TIMER:
  226.         walkRandom();
  227.         stubToMesa();
  228.         // now, refresh my display...
  229.         WinSendMsg( hw, MEM_UPDATESCREEN, NULL, NULL );
  230.         break;
  231.         
  232.     case WM_HITTEST:
  233.         MyMesa -> setHitPoint(SHORT1FROMMP(mp1),SHORT2FROMMP(mp1));
  234.         return WinDefDlgProc( hw, msg, mp1, mp2 );
  235.         
  236.         
  237.     // sent to update the screen from the variables...
  238.     case MEM_UPDATESCREEN:
  239.         updateScreenVals( hw );
  240.         break;
  241.         
  242.     default:
  243.         return WinDefDlgProc( hw, msg, mp1, mp2 );
  244.     }    // the outer-switch statement
  245.     
  246.     return 0;
  247. }
  248.  
  249. // actual main loop
  250. //
  251.  
  252. int main(int argc,char *argv[])
  253. {
  254.     HMQ    hmq;
  255.     HAB    theHAB;
  256.     int    ret = 0;
  257.     int enable;
  258.     
  259.     theHAB = WinInitialize(0);
  260.     hmq = WinCreateMsgQueue(theHAB,256);
  261.     
  262.     // initialize the function parser
  263.     MGController :: setupFuncs();
  264.     
  265.     // create a Mesa Controller
  266.     MyMesa = new MSampleController("None",enable,theHAB,1,0);
  267.     
  268.     // set it up
  269.     MyMesa -> setUp();
  270.     
  271.     // load AddIn's
  272.     MyMesa -> setupAddIns();
  273.  
  274.    
  275.     loadStocks();
  276.     WinDlgBox(HWND_DESKTOP,HWND_DESKTOP, sampProc, 0, IDD_MYAPP, 0 );
  277.     
  278.     theView -> cleanUp();
  279.     delete theView;
  280.     
  281.     WinDestroyMsgQueue(hmq);
  282.     releaseStocks();
  283.     
  284.     // clean up the controller
  285.     MyMesa -> cleanUp();
  286.     
  287.     // and make it go away
  288.     delete MyMesa;
  289.     
  290.     // wait for threads to end
  291.     DosSleep(750);
  292.     
  293.     WinTerminate(theHAB);
  294.     return ret;
  295. }
  296.  
  297. void loadStocks()
  298. {
  299.     // build an array of 5 stock names and prices...
  300.     int i;
  301.     
  302.     for( i = 0; i <5; i++ )
  303.         stockNames[ i ] = (char *)malloc( 32 * sizeof( char ));    
  304.  
  305.     strcpy( stockNames[ 0 ], "HOPII" );
  306.     strcpy( stockNames[ 1 ], "STONE" );
  307.     strcpy( stockNames[ 2 ], "MESAC" );
  308.     strcpy( stockNames[ 3 ], "CARGZ" );
  309.     strcpy( stockNames[ 4 ], "ARCSD" );
  310.     
  311.     prices[ 0 ] = 20.0;
  312.     prices[ 1 ] = 32.2;
  313.     prices[ 2 ] = 4.5;
  314.     prices[ 3 ] = 102.71;
  315.     prices[ 4 ] = 43.02;
  316. }    
  317.  
  318. void releaseStocks()
  319. {
  320.     int i;
  321.     
  322.     for( i =0; i <5; i++ )
  323.         free( stockNames[ i ] );
  324.     
  325. }
  326.  
  327. void updateScreenVals( HWND hw )
  328. {
  329.     int    i;
  330.     char cBuf[ 128 ];
  331.     
  332.     // only count 4 because I haven't re-written the UI piece...
  333.     for( i=0; i <4; i++ )
  334.     {
  335.         WinSetWindowText(WinWindowFromID(hw,SAMPAPP_STOCK1 +i),stockNames[i] );
  336.         sprintf( cBuf, "%.2f", prices[ i ] );
  337.         WinSetWindowText(WinWindowFromID(hw,SAMPAPP_PRICE1 +i),cBuf);
  338.     }
  339. }
  340.  
  341. void walkRandom()
  342. {
  343.     int i;
  344.     float rn;
  345.     
  346.     
  347.     for( i = 0; i<5; i++ )
  348.     {
  349.         rn = rand();
  350.         rn /= RAND_MAX;
  351.         rn -= 0.5;
  352.         prices[ i ] = prices[ i ] + ( prices[ i ]/100 * rn );
  353.     }
  354. }
  355.  
  356. void stubToMesa()
  357. {
  358.     // this is the code that would take the four new values, and send them
  359.     // to the Mesa model.
  360.     int i;
  361.     
  362.     for (i = 0; i < 5; i++)
  363.     {
  364.         setRTFValue("BID",stockNames[i],prices[i]);
  365.     }
  366.     
  367.     return;
  368. }
  369.  
  370. void pasteIntoMesa( char *ad, char *str )
  371. {
  372.     // this is the code that will take an address string and value string,
  373.     // and pass them to the model for evaluation/storage.
  374.     MAddress adr,base;
  375.     MException me;
  376.     int len;
  377.     
  378.     base.set(0,0,0);
  379.     
  380.     if (theModel -> isAddress(ad,&base,&adr,len))
  381.     {
  382.         theModel -> setCellToString(&adr,str,0,&me,NULL);
  383.     }
  384.     
  385.     return;
  386. }
  387.