home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dokpr1.zip / posys / posys.cpp < prev    next >
Text File  |  1995-11-09  |  17KB  |  519 lines

  1. #define INCL_WIN
  2. #define INCL_DOS
  3. #include <os2.h>
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <memory.h>
  8. #include <string.h>
  9. #include "porder.hpp"
  10. #include "posys.h"
  11. #include "sampcont.h"
  12.  
  13. #define WM_NEW WM_USER
  14. #define WM_OPENFILE WM_USER+1
  15.  
  16.  
  17. MSampleController *MyMesa;
  18. POrder * currentPo;
  19.  
  20. // the icon stuck in the dlgs upper corner and displayed when minimized
  21. HPOINTER prtIcon;
  22.  
  23. // this procedure handles the add dialog.  When the OK button is pushed
  24. // it queries the fields and calls the addItem method of the purchase order.
  25. MRESULT EXPENTRY AddItemDlg(HWND hw,ULONG msg,MPARAM mp1, MPARAM mp2)
  26. {
  27.    char desc[200],cost[100],quant[100];
  28.  
  29.    switch (msg) {
  30.    case WM_COMMAND:
  31.      switch (SHORT1FROMMP(mp1)) {
  32.      case DID_OK:
  33.         WinQueryDlgItemText(hw,DID_DESCRIPTIONS,sizeof(desc),desc);
  34.         WinQueryDlgItemText(hw,DID_COST,sizeof(cost),cost);
  35.         WinQueryDlgItemText(hw,DID_QUANTITY,sizeof(quant),quant);
  36.         currentPo->addItem(desc,cost,quant);
  37.         WinDismissDlg(hw,DID_OK);
  38.         break;
  39.      case DID_CANCEL:
  40.         WinDismissDlg(hw,DID_CANCEL);
  41.         break;
  42.       } /* endswitch */
  43.       break;
  44.    } /* endswitch */
  45.    return WinDefDlgProc(hw,msg,mp1,mp2);
  46. }
  47.  
  48.  
  49.  
  50. // this procedure queries the description of the currently selected item
  51. // in the list box and then updates the cost, quanitity, and item total
  52. // fields in the dialog by getting the information from the purchase order
  53. void UpdateCosts(HWND hw, POrder *thePo, int id = 0)
  54. {
  55.    char buffer[300];
  56.    char *str;
  57.    int select;
  58.    static int flag;
  59.  
  60.    if (flag) {
  61.       return;
  62.    }
  63.    flag = 1;
  64.  
  65.    // get the id of the selected item
  66.    select=(int)WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_QUERYSELECTION,(MPARAM)LIT_FIRST,0L);
  67.  
  68.    // query the text for the selected item
  69.    WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_QUERYITEMTEXT,MPFROM2SHORT(select,sizeof(buffer)),buffer);
  70.  
  71.    // if there are no items in the listbox, it will return an empty string so
  72.    // we'll disable all the appropriate entry fields and buttons and the leave
  73.    if (strcmp(buffer,"")==0) {
  74.       WinEnableWindow(WinWindowFromID(hw,DID_DELETEITEM),FALSE);
  75.       WinEnableWindow(WinWindowFromID(hw,DID_COST),FALSE);
  76.       WinEnableWindow(WinWindowFromID(hw,DID_QUANTITY),FALSE);
  77.       return;
  78.    } /* endif */
  79.  
  80.    // otherwise, we'll enable the windows, query the values, and then set the
  81.    // text of each of the fields
  82.    WinEnableWindow(WinWindowFromID(hw,DID_DELETEITEM),TRUE);
  83.    WinEnableWindow(WinWindowFromID(hw,DID_COST),TRUE);
  84.    WinEnableWindow(WinWindowFromID(hw,DID_QUANTITY),TRUE);
  85.  
  86.    if(id != DID_COST)
  87.    {
  88.      str=thePo->queryItemCost(buffer);
  89.      WinSetWindowText(WinWindowFromID(hw,DID_COST),str);
  90.      delete str;
  91.    }
  92.  
  93.    if(id != DID_QUANTITY)
  94.    {
  95.      str=thePo->queryItemQuantity(buffer);
  96.      WinSetWindowText(WinWindowFromID(hw,DID_QUANTITY),str);
  97.      delete str;
  98.    }
  99.  
  100.    str=thePo->queryItemTotal(buffer);
  101.    WinSetWindowText(WinWindowFromID(hw,DID_ITEMTOTAL),str);
  102.    delete str;
  103.  
  104.    str=thePo->SubTotal();
  105.    WinSetWindowText(WinWindowFromID(hw,DID_SUBTOTAL),str);
  106.    delete str;
  107.  
  108.    str=thePo->Total();
  109.    WinSetWindowText(WinWindowFromID(hw,DID_TOTAL),str);
  110.    delete str;
  111.  
  112.    flag = 0;
  113. }
  114.  
  115. // this updates all of the fields in the window.  Ideally, when something
  116. // changed, we'd just update what changed, but this is easier to deal with
  117. void updateWindow(HWND hw,POrder * thePo)
  118. {
  119.    char *str;
  120.  
  121.    str=thePo->PONum();
  122.    WinSetWindowText(WinWindowFromID(hw,DID_PONUM),str);
  123.    delete str;
  124.  
  125.    str=thePo->Date();
  126.    WinSetWindowText(WinWindowFromID(hw,DID_DATE),str);
  127.    delete str;
  128.  
  129.    str=thePo->Initials();
  130.    WinSetWindowText(WinWindowFromID(hw,DID_INITIALS),str);
  131.    delete str;
  132.  
  133.    str=thePo->SubTotal();
  134.    WinSetWindowText(WinWindowFromID(hw,DID_SUBTOTAL),str);
  135.    delete str;
  136.  
  137.    str=thePo->Tax();
  138.    WinSetWindowText(WinWindowFromID(hw,DID_TAX),str);
  139.    delete str;
  140.  
  141.    str=thePo->Shipping();
  142.    WinSetWindowText(WinWindowFromID(hw,DID_SHIPPING),str);
  143.    delete str;
  144.  
  145.    str=thePo->Total();
  146.    WinSetWindowText(WinWindowFromID(hw,DID_TOTAL),str);
  147.    delete str;
  148.  
  149.    str=thePo->Comments();
  150.    WinSetWindowText(WinWindowFromID(hw,DID_COMMENTS),str);
  151.    delete str;
  152.  
  153.    str=thePo->Flags();
  154.    WinSetWindowText(WinWindowFromID(hw,DID_FLAGS),str);
  155.    delete str;
  156.  
  157.    WinSendMsg(WinWindowFromID(hw,DID_DESCRIPTIONS),LM_DELETEALL,0L,0L);
  158.    WinEnableWindow(WinWindowFromID(hw,DID_DELETEITEM),FALSE);
  159.    WinEnableWindow(WinWindowFromID(hw,DID_COST),FALSE);
  160.    WinEnableWindow(WinWindowFromID(hw,DID_QUANTITY),FALSE);
  161.    str=thePo->startEnumDescriptions();
  162.    while (strcmp(str,"") != 0) {
  163.          WinSendMsg(WinWindowFromID(hw,DID_DESCRIPTIONS),LM_INSERTITEM,(MPARAM)LIT_END,(MPARAM)str);
  164.          WinEnableWindow(WinWindowFromID(hw,DID_DELETEITEM),TRUE);
  165.          WinEnableWindow(WinWindowFromID(hw,DID_COST),TRUE);
  166.          WinEnableWindow(WinWindowFromID(hw,DID_QUANTITY),TRUE);
  167.          delete str;
  168.          str=thePo->nextDescription();
  169.    } /* end while */
  170.    delete str;
  171.  
  172.    WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_SELECTITEM,(MPARAM)0,(MPARAM)TRUE);
  173. }
  174.  
  175.  
  176. // pops up the new item dialog box to allow the user to create new items
  177. // when the item is added, it updates the entire window so that the right
  178. // information is displayed
  179. void NewItemProcedure(HWND hw, POrder  *thePo)
  180. {
  181.    currentPo=thePo;
  182.    WinDlgBox(HWND_DESKTOP,hw,AddItemDlg,NULLHANDLE,IDD_NEWITEM,0L);
  183.    updateWindow(hw,thePo);
  184. }
  185.  
  186.  
  187. //  This procedure pops up a message box verifing that the user really wants to
  188. //  delete the selected item and then deletes the item if they really do
  189. void DeleteItemProcedure(HWND hw, POrder *thePo)
  190. {
  191.  
  192.    //query the item id
  193.    int select=(int)WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,
  194.                            LM_QUERYSELECTION,(MPARAM)LIT_FIRST,0L);
  195.    char desc[200];
  196.    char buffer[400];
  197.  
  198.    // query the text of the selected item
  199.    WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_QUERYITEMTEXT,
  200.                            MPFROM2SHORT(select,sizeof(desc)),desc);
  201.  
  202.    // format the text for the message box
  203.    sprintf(buffer,"Are you sure you want to delete item: %s",desc);
  204.  
  205.    // display the message box
  206.    if (WinMessageBox(HWND_DESKTOP,hw,buffer,"Attention!",24,MB_YESNO | MB_ICONEXCLAMATION)==MBID_YES) {
  207.       // if the use hit the YES button, have the PO delete the item
  208.       thePo->deleteItem(desc);
  209.    } /* endif */
  210.    // nomatter what, update the window.  It really only needs to be done if something
  211.    // was actually deleted, but it doesn't hurt
  212.    UpdateCosts(hw,thePo);
  213.    updateWindow(hw,thePo);
  214. }
  215.  
  216. // this is the window procedure that handles the purchase order
  217. MRESULT EXPENTRY POProc(HWND hw,ULONG msg,MPARAM mp1,MPARAM mp2)
  218. {
  219.    // the pointer to the purchase order is stored in the first window ULONG
  220.    // so we'll query that first thing
  221.    POrder *thePo=(POrder *) WinQueryWindowULong(hw,QWL_USER);
  222.    char buffer[10000];
  223.    char buffer2[1000];
  224.    char *ptr;
  225.    int select;
  226.  
  227.    switch (msg) {
  228.    case WM_NEW:
  229.       // we're creating a new PO so we'll just new it without a file name
  230.           thePo=new POrder(hw);
  231.       // set the first user ULONG to the po
  232.           WinSetWindowULong(hw,QWL_USER,(ULONG)thePo);
  233.       // update the window so it reflects the data in the sheet
  234.           updateWindow(hw,thePo);
  235.       break;
  236.    case WM_OPENFILE:
  237.           {
  238.          // for this, we'll pop up a standard dialog box asking for the filename
  239.             FILEDLG fdg;
  240.             char *types[2]={"Mesa Workbook File",NULL};
  241.             char *type="Mesa Workbook File";
  242.  
  243.             memset(&fdg,0,sizeof(FILEDLG));
  244.             fdg.cbSize=sizeof(FILEDLG);
  245.             fdg.fl=FDS_CENTER | FDS_OPEN_DIALOG;
  246.             fdg.pszIType = ( PSZ )type;
  247.             fdg.papszITypeList = ( PAPSZ )types;
  248.             WinFileDlg(HWND_DESKTOP,hw,&fdg);
  249.             if(fdg.lReturn == DID_OK) {
  250.                // create a new PO and have it load it's info from the selected file
  251.                thePo=new POrder(fdg.szFullFile,hw);
  252.                // set the window ULONG to the PO
  253.                WinSetWindowULong(hw,QWL_USER,(ULONG)thePo);
  254.                // update the window to reflect the new values
  255.                updateWindow(hw,thePo);
  256.             } else WinDismissDlg(hw,DID_CANCEL);
  257.           }
  258.       break;
  259.    case WM_CONTROL:
  260.      switch (SHORT1FROMMP(mp1)) {
  261.      case DID_DESCRIPTIONS:
  262.           if (SHORT2FROMMP(mp1) == LN_SELECT) {
  263.                UpdateCosts(hw,thePo);
  264.           } /* endif */
  265.         break;
  266.      case DID_PONUM:
  267.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  268.              WinQueryDlgItemText(hw,DID_PONUM,sizeof(buffer),buffer);
  269.              thePo->setPONum(buffer);
  270.           } /* endif */
  271.         break;
  272.      case DID_INITIALS:
  273.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  274.              WinQueryDlgItemText(hw,DID_INITIALS,sizeof(buffer),buffer);
  275.              thePo->setInitials(buffer);
  276.           } /* endif */
  277.         break;
  278.      case DID_DATE:
  279.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  280.              WinQueryDlgItemText(hw,DID_DATE,sizeof(buffer),buffer);
  281.              thePo->setDate(buffer);
  282.           } /* endif */
  283.         break;
  284.      case DID_COMMENTS:
  285.           if (SHORT2FROMMP(mp1) == MLN_CHANGE) {
  286.              WinQueryDlgItemText(hw,DID_COMMENTS,sizeof(buffer),buffer);
  287.              thePo->setComments(buffer);
  288.           } /* endif */
  289.         break;
  290.      case DID_SHIPPING:
  291.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  292.              WinQueryDlgItemText(hw,DID_SHIPPING,sizeof(buffer),buffer);
  293.              thePo->setShipping(buffer);
  294.              DosSleep(100);
  295.              WinSetWindowText(WinWindowFromID(hw,DID_TOTAL),thePo->Total());
  296.            } /* endif */
  297.         break;
  298.      case DID_TAXRATE:
  299.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  300.              WinQueryDlgItemText(hw,DID_TAXRATE,sizeof(buffer),buffer);
  301.              thePo->setTaxRate(buffer);
  302.              DosSleep(100);
  303.              WinSetWindowText(WinWindowFromID(hw,DID_TOTAL),thePo->Total());
  304.            } /* endif */
  305.         break; 
  306.      case DID_COST:
  307.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  308.              WinQueryDlgItemText(hw,DID_COST,sizeof(buffer),buffer);
  309.              select=(int)WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,
  310.                            LM_QUERYSELECTION,(MPARAM)LIT_FIRST,0L);
  311.              WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_QUERYITEMTEXT,
  312.                            MPFROM2SHORT(select,sizeof(buffer2)),buffer2);
  313.              thePo->setItemCost(buffer2,buffer);
  314.              UpdateCosts(hw,thePo, DID_COST);
  315.           } /* endif */
  316.         break;
  317.      case DID_QUANTITY:
  318.           if (SHORT2FROMMP(mp1) == EN_CHANGE) {
  319.              select=(int)WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,
  320.                            LM_QUERYSELECTION,(MPARAM)LIT_FIRST,0L);
  321.              WinSendDlgItemMsg(hw,DID_DESCRIPTIONS,LM_QUERYITEMTEXT,
  322.                            MPFROM2SHORT(select,sizeof(buffer2)),buffer2);
  323.              WinQueryDlgItemText(hw,DID_QUANTITY,sizeof(buffer),buffer);
  324.              thePo->setItemQuantity(buffer2,buffer);
  325.              UpdateCosts(hw,thePo,DID_QUANTITY);
  326.             } /* endif */
  327.         break;
  328.      }
  329.      break;
  330.    case WM_COMMAND:
  331.       switch (LOUSHORT(mp1)) {
  332.       case DID_OK:
  333.          ptr=thePo->fileName();
  334.          if (strcmp(ptr,"")==0) {
  335.             FILEDLG fdg;
  336.             memset(&fdg,0,sizeof(FILEDLG));
  337.             fdg.cbSize=sizeof(FILEDLG);
  338.             fdg.fl=FDS_CENTER | FDS_SAVEAS_DIALOG;
  339.             WinFileDlg(HWND_DESKTOP,hw,&fdg);
  340.             if(fdg.lReturn == DID_OK) {
  341.                thePo->setFileName(fdg.szFullFile);
  342.             }
  343.          } /* endif */
  344.          delete ptr;
  345.          thePo->save();
  346.          delete thePo;
  347.          WinDismissDlg(hw,DID_OK);
  348.          break;
  349.       case DID_CANCEL:
  350.          delete thePo;
  351.          WinDismissDlg(hw,DID_CANCEL);
  352.          break;
  353.       case DID_TEST:
  354.           updateWindow(hw,thePo);
  355.          break;
  356.       case DID_NEWITEM:
  357.            NewItemProcedure(hw,thePo);
  358.          break;
  359.       case DID_DELETEITEM:
  360.            DeleteItemProcedure(hw,thePo);
  361.          break;
  362.       case DID_SHEETVIEW:
  363.          thePo->displaySheetView(hw);
  364.          updateWindow(hw,thePo);
  365.          break;
  366.       default:
  367.         return WinDefDlgProc(hw,msg,mp1,mp2);
  368.       } /* endswitch */
  369.       break;
  370.    case PO_CONTENTSCHANGED:
  371.    case PO_RESETVIEW:
  372.    case PO_REDISPLAY:
  373.       if (thePo->isInSheetView()) {
  374.          updateWindow(hw,thePo);
  375.       } /* endif */
  376.       break;
  377.    default:
  378.      return WinDefDlgProc(hw,msg,mp1,mp2);
  379.    } /* endswitch */
  380.    return (MRESULT)FALSE;
  381. }
  382.  
  383.  
  384. // this procedure processes the messages sent to the main application window
  385. // this includes menu items (which are usually just forwarded to the appropriate
  386. // purchase order) and painting.
  387. MRESULT EXPENTRY PurchaseOrderSysProc(HWND hw,ULONG msg,MPARAM mp1,MPARAM mp2)
  388. {
  389.    HWND hwnddlg;
  390.     
  391.    switch( msg ) {
  392.    case WM_COMMAND:
  393.      {
  394.      switch( SHORT1FROMMP( mp1 ) ) {
  395.      // the File->New PO  menu item.  It loads the dialog from the resources
  396.      // and sends it the NEW po message to tell it to create a new PO
  397.      case MID_FILE_NEW:
  398.           hwnddlg=WinLoadDlg(hw,hw,&POProc,NULL,IDD_PURCHASE_ORDER,0L);
  399.           // set the icon
  400.           WinSendMsg(hwnddlg,WM_SETICON,(MPARAM)prtIcon,0L);
  401.           // send it the new message
  402.           WinSendMsg(hwnddlg,WM_NEW,0L,0L);
  403.           // show the window
  404.           WinShowWindow(hwnddlg,TRUE);
  405.         break;
  406.  
  407.      // the File->Open menu item.  It loads the dialog from the resources and
  408.      // sends it the Open message to have it open an existing PO.
  409.      case MID_FILE_OPEN:
  410.           hwnddlg=WinLoadDlg(hw,hw,&POProc,NULL,IDD_PURCHASE_ORDER,0L);
  411.           // set the icon
  412.           WinSendMsg(hwnddlg,WM_SETICON,(MPARAM)prtIcon,0L);
  413.           // tell it to open a file
  414.           WinSendMsg(hwnddlg,WM_OPENFILE,0L,0L);
  415.           // show it
  416.           WinShowWindow(hwnddlg,TRUE);
  417.         break;
  418.  
  419.  
  420.      case MID_FILE_SAVE:
  421.         break;
  422.      case MID_FILE_SAVEAS:
  423.         break;
  424.      case MID_FILE_PRINT:
  425.         break;
  426.  
  427.       // close the window if the exit menu option is selected
  428.      case MID_FILE_EXIT:
  429.         WinPostMsg(hw,WM_CLOSE,0L,0L);
  430.         break;
  431.      default:
  432.         return WinDefWindowProc( hw, msg, mp1, mp2 );
  433.      }
  434.         
  435.    }  // the WM_COMMAND message
  436.       break;
  437.  
  438.  
  439.    // general window stuff
  440.    case WM_ERASEBACKGROUND:
  441.        return (MRESULT)TRUE;
  442.  
  443.     case WM_PAINT:
  444.        {
  445.           HPS hps=WinBeginPaint(hw,0,0);
  446.           RECTL re;
  447.           WinQueryWindowRect(hw,&re);
  448.           WinFillRect(hps,&re,CLR_WHITE);
  449.           WinEndPaint(hps);
  450.           return 0L;
  451.        }
  452.  
  453.     default:
  454.         return WinDefWindowProc( hw, msg, mp1, mp2 );
  455.     }    // the outer-switch statement
  456.     
  457.     return 0;
  458. }
  459.  
  460.  
  461.  
  462. int main(int argc,char *argv[])
  463. {
  464.     HMQ    hmq;
  465.    QMSG  qmsg;
  466.     HAB    theHAB;
  467.     int enable;
  468.    HWND frame,client=NULLHANDLE;
  469.    int rc=0;
  470.  
  471.  
  472.    ULONG frameFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER | FCF_ICON |
  473.                       FCF_MINMAX | FCF_MENU | FCF_SHELLPOSITION | FCF_TASKLIST;
  474.     
  475.     theHAB = WinInitialize(0);
  476.     hmq = WinCreateMsgQueue(theHAB,256);
  477.    rc=WinRegisterClass(theHAB,"CLIENT",PurchaseOrderSysProc,0,0);
  478.     
  479.     // initialize the function parser
  480.     MGController :: setupFuncs();
  481.     
  482.     // create a Mesa Controller
  483.     MyMesa = new MSampleController("None",enable,theHAB,1);
  484.     
  485.     // set it up
  486.     MyMesa -> setUp();
  487.     
  488.     // load AddIn's
  489.     MyMesa -> setupAddIns();
  490.  
  491.    // create the frame window
  492.    frame=WinCreateStdWindow(HWND_DESKTOP,WS_VISIBLE,&frameFlags,"CLIENT",
  493.                      "Purchase Ordering System",0,0,ID_APPNAME,
  494.                      &client);
  495.  
  496.    // load the pointer used by the client windows
  497.    prtIcon=WinLoadPointer(HWND_DESKTOP,NULL,1);
  498.  
  499.    // run the application
  500.    while (WinGetMsg(theHAB,&qmsg,0,0,0)) {
  501.          WinDispatchMsg(theHAB,&qmsg);
  502.    } /* endwhile */
  503.  
  504.    //destroy the message queue when done
  505.     WinDestroyMsgQueue(hmq);
  506.  
  507.     // clean up the controller
  508.     MyMesa -> cleanUp();
  509.     
  510.     // and make it go away
  511.     delete MyMesa;
  512.     
  513.     // wait for threads to end
  514.     DosSleep(750);
  515.     
  516.    // outa here!
  517.     WinTerminate(theHAB);
  518. }
  519.