home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / componen / interact / demo / data.2 / samples / ansic / TRAVEL / DISTANCE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  12.2 KB  |  450 lines

  1. //lRegen_FileHeading
  2. //lRegen_FileHeading
  3.  
  4.  
  5.      /********************************************************************
  6.       *
  7.       *   Source File:  distance.c
  8.       *   Author:       RC
  9.       *   Module:       Module source file for dialog distance
  10.       *   Program Desc:
  11.       *   Date:         Wed Jan 24 15:30:38 1996
  12.       *   Language:     ANSI C
  13.       *
  14.       ********************************************************************/
  15.  
  16. #include <windows.h>
  17. #include ".\travel.h"
  18.  
  19.  
  20. //lRegen_Variables
  21.  
  22. #include <stdlib.h> // for atoi support
  23.  
  24. void CalculateDistance(HWND);
  25. void CalcDist(LPSTR);
  26. void WalkList(HWND);
  27.  
  28. void ClearAllItems();
  29. void RemoveUserData();
  30.  
  31. void Push(LPSTR);
  32. void Pop(LPSTR);
  33. void EmptyStack();
  34.  
  35. //lRegen_Variables
  36. //Lock
  37.  
  38. static HWND     hDlgdistance;
  39.  
  40. BOOL CALLBACK EXPORT fndistanceDlgProc(HWND, UINT, WPARAM, LPARAM);
  41. int fndistance(HWND hParentWnd, UINT iData, void FAR *lpData)
  42. {
  43.    int   RetCode;
  44.  
  45.    //lRegen_InitDlg
  46.    //lRegen_InitDlg
  47.  
  48.    if((RetCode = DialogBox(hInstance, MAKEINTRESOURCE(distance), hParentWnd, fndistanceDlgProc)) == -1)
  49.    {
  50.         MessageBox(NULL, "Unable to display dialog", "System Error",
  51.                    MB_SYSTEMMODAL | MB_ICONHAND | MB_OK);
  52.         return FALSE;
  53.    }
  54.  
  55.  
  56.    //lRegen_TermDlg
  57.    //lRegen_TermDlg
  58.  
  59.    return(RetCode);
  60. }
  61.  
  62. BOOL CALLBACK EXPORT fndistanceDlgProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  63. {
  64.    WORD CtlId;
  65.    WORD Notify;
  66.    HWND CtlWnd;
  67.    //lRegen_WindowProcVariables
  68.    //lRegen_WindowProcVariables
  69.  
  70.    switch(uMessage)
  71.    {
  72.       //lRegen_WndProc
  73.  
  74.       case WM_DESTROY :
  75.          RemoveUserData();
  76.          break;
  77.  
  78.       //lRegen_WndProc
  79.  
  80.       case WM_INITDIALOG :
  81.  
  82.          //Regen_WM_InitDialog
  83.          hDlgdistance = hWnd;
  84.  
  85.          {
  86.             ENTITY entity;
  87.  
  88.             if(IsWindow(hIDO))
  89.             {
  90.                if(idoIterateEntityFirst(hIDO))
  91.                   while(idoIterateEntityNext(hIDO, &entity))  // extract the user names of all the entities
  92.                   {
  93.                      SendDlgItemMessage(hWnd, IDC_SOURCE, CB_ADDSTRING, 0, (LPARAM)(LPSTR)entity.name);
  94.                      SendDlgItemMessage(hWnd, IDC_DEST, CB_ADDSTRING, 0, (LPARAM)(LPSTR)entity.name);
  95.                   }
  96.  
  97.                SendDlgItemMessage(hWnd, IDC_SOURCE, CB_SETCURSEL, 0, 0L);
  98.                SendDlgItemMessage(hWnd, IDC_DEST, CB_SETCURSEL, 0, 0L);
  99.             }
  100.  
  101.             ClearAllItems();
  102.          }
  103.          //Regen_WM_InitDialog
  104.  
  105.          return TRUE;
  106.  
  107.       case WM_SHOWWINDOW :
  108.          //Regen_WM_SHOWWINDOW
  109.          //Regen_WM_SHOWWINDOW
  110.          break;
  111.  
  112.       case WM_COMMAND :
  113.       #ifdef WIN32
  114.          CtlId  = LOWORD(wParam);
  115.          Notify = HIWORD(wParam);
  116.          CtlWnd = (HWND)lParam;
  117.       #else
  118.          CtlId  = wParam;
  119.          Notify = HIWORD(lParam);
  120.          CtlWnd = (HWND)LOWORD(lParam);
  121.       #endif
  122.          switch(CtlId)
  123.          {
  124.             case IDC_SOURCE :
  125.                   switch(Notify)
  126.                   {
  127.                      case CBN_SELCHANGE :
  128.                         //Regen_IDC_SOURCE_CBN_SELCHANGE
  129.  
  130.                         CalculateDistance(hWnd);
  131.  
  132.                         //Regen_IDC_SOURCE_CBN_SELCHANGE
  133.                         break;
  134.  
  135.                   }
  136.                   break;
  137.             case IDC_DEST :
  138.                   switch(Notify)
  139.                   {
  140.                      case CBN_SELCHANGE :
  141.                         //Regen_IDC_DEST_CBN_SELCHANGE
  142.  
  143.                         CalculateDistance(hWnd);
  144.  
  145.                         //Regen_IDC_DEST_CBN_SELCHANGE
  146.                         break;
  147.  
  148.                   }
  149.                   break;
  150.             case IDOK :
  151.                //Regen_IDOK
  152.                //Regen_IDOK
  153.  
  154.                EndDialog(hWnd, TRUE);
  155.                return TRUE;
  156.  
  157.             case IDC_SHORT :
  158.               //Regen_IDC_SHORT
  159.               //Regen_IDC_SHORT
  160.               break;
  161.  
  162.             case IDC_LIST :
  163.               //Regen_IDC_LIST
  164.               //Regen_IDC_LIST
  165.               break;
  166.  
  167.             //lRegen_CustomCommand
  168.             //lRegen_CustomCommand
  169.  
  170.                default :
  171.                   return FALSE;
  172.  
  173.              }
  174.  
  175.        case WM_SYSCOMMAND :
  176.  
  177.             //lRegen_SysCommand
  178.             //lRegen_SysCommand
  179.             switch(wParam & 0xFFF0)
  180.             {
  181.                 case SC_CLOSE :
  182.                   EndDialog(hWnd, FALSE);
  183.                   return TRUE;
  184.             }
  185.    }
  186.    return FALSE;
  187. }
  188.  
  189. //Regen_CustomCode
  190.  
  191. void ClearAllItems()
  192. {
  193.    LPUSERDATA lpData;
  194.    RELATION   relation;
  195.    ENTITY     entity;
  196.    long       count;
  197.  
  198.  
  199.    if(IsWindow(hIDO))
  200.    {
  201.       if(idoIterateEntityFirst(hIDO))
  202.          while(idoIterateEntityNext(hIDO, &entity))
  203.          {
  204.             // if the object does not have a user data object, create one and store it
  205.             lpData = idoEntityGetUserData(&entity);
  206.             if(lpData == NULL)
  207.                lpData = (LPUSERDATA)calloc(1, sizeof(USERDATA));
  208.  
  209.             lpData->lDistance = -1;
  210.             lstrcpy(lpData->szNamePrevious, "");
  211.  
  212.             idoEntitySetUserData(&entity, lpData);
  213.             idoEntitySetTextColor(&entity, RGB(0,0,0));
  214.  
  215.             // if the object has arrows pointing away, iterate through them
  216.             // to reset their colors
  217.             count = idoEntityGetRelationOutCount(&entity);
  218.             if(count)
  219.             {
  220.                // iterate all the away arrows
  221.                idoEntityIterateRelationOutFirst(&entity);
  222.                while(idoEntityIterateRelationOutNext(&entity, &relation))
  223.                   idoRelationSetTextColor(&relation, RGB(0,0,0));
  224.             }
  225.          }
  226.  
  227.       InvalidateRect(hIDO, NULL, TRUE);
  228.       UpdateWindow(hIDO);
  229.    }
  230. }
  231.  
  232. void CalculateDistance(HWND hDlg)
  233. {
  234.    char       szEntityName[100+1];
  235.    LPUSERDATA lpData;
  236.    ENTITY     entity;
  237.    UINT       index;
  238.  
  239.  
  240.    ClearAllItems();
  241.  
  242.    index = (UINT)SendDlgItemMessage(hDlg, IDC_SOURCE, CB_GETCURSEL, 0, 0L);
  243.    SendDlgItemMessage(hDlg, IDC_SOURCE, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szEntityName);
  244.    SendDlgItemMessage(hDlg, IDC_SHORT, LB_RESETCONTENT, 0, 0L);
  245.  
  246.    idoGetEntity(hIDO, 0, szEntityName, &entity);
  247.    lpData = idoEntityGetUserData(&entity);
  248.    if(lpData != NULL)
  249.       lpData->lDistance = 0;
  250.  
  251.    EmptyStack();
  252.    Push(szEntityName);  // push entity name from the SOURCE combobox
  253.  
  254.    while(SendDlgItemMessage(hDlg, IDC_LIST, LB_GETCOUNT, 0, 0L))  // while the stack is not empty
  255.    {
  256.       Pop(szEntityName);
  257.       CalcDist(szEntityName);
  258.    }
  259.  
  260.    WalkList(hDlg);
  261. }
  262.  
  263.  
  264. // pass the name of the previous item here
  265. void CalcDist(LPSTR lpName)
  266. {
  267.    LPUSERDATA lpData;
  268.    RELATION   relation;
  269.    ENTITY     this_entity;
  270.    ENTITY     next_entity;
  271.    long       count, Distance, CurrDist;
  272.    char       szText[50+1];
  273.  
  274.  
  275.    idoGetEntity(hIDO, 0, lpName, &this_entity);
  276.    lpData = idoEntityGetUserData(&this_entity);
  277.    if(lpData != NULL)
  278.       CurrDist = lpData->lDistance;  // save the current distance.
  279.  
  280.  
  281.    // if the object has arrows pointing away, iterate through them
  282.    count = idoEntityGetRelationOutCount(&this_entity);
  283.    if(count)
  284.    {
  285.       // iterate all the away arrows
  286.       idoEntityIterateRelationOutFirst(&this_entity);
  287.       while(idoEntityIterateRelationOutNext(&this_entity, &relation))
  288.       {
  289.          lstrcpy(szText, idoRelationGetText(&relation));   // get the line text
  290.          Distance = (long)atoi(szText);               // do a get prop for this
  291.  
  292.          idoRelationDestinationEntity(&relation, &next_entity);
  293.          lpData = idoEntityGetUserData(&next_entity);
  294.          if(lpData != NULL)
  295.          {
  296.             // if the distance to the prev item is less, do this
  297.             if( (lpData->lDistance == -1) || (lpData->lDistance > Distance + CurrDist) )
  298.             {
  299.                lstrcpy(lpData->szNamePrevious, lpName);
  300.                lstrcpy(lpData->szNamePreviousLine, (LPSTR)relation.name);
  301.                lpData->lDistance = Distance + CurrDist;
  302.                Push((LPSTR)next_entity.name);
  303.             }
  304.          }
  305.       } // end WHILE
  306.    }
  307.  
  308.    count = idoEntityGetRelationInCount(&this_entity);
  309.    if(count)
  310.    {
  311.       //iterate all the arrows pointing to this entity
  312.       idoEntityIterateRelationInFirst(&this_entity);
  313.       while(idoEntityIterateRelationInNext(&this_entity, &relation))
  314.  
  315.       {
  316.          lstrcpy(szText, idoRelationGetText(&relation));  // get the line text
  317.          Distance = (long)atoi(szText);  // do a get prop for this;
  318.  
  319.          idoRelationSourceEntity(&relation, &next_entity);
  320.          lpData = idoEntityGetUserData(&next_entity);
  321.          if(lpData != NULL)
  322.          {
  323.             // if the distance to the prev item is less, do this
  324.             if( (lpData->lDistance == -1) || (lpData->lDistance > Distance + CurrDist) )
  325.             {
  326.                lstrcpy(lpData->szNamePrevious, lpName);
  327.                lstrcpy(lpData->szNamePreviousLine, relation.name);
  328.                lpData->lDistance = Distance + CurrDist;
  329.                Push((LPSTR)next_entity.name);
  330.             }
  331.          }
  332.  
  333.       } // end WHILE
  334.    } // end count
  335.  
  336. }
  337.  
  338.  
  339. void WalkList(HWND hDlg)
  340. {
  341.    char szEntityName[100+1];
  342.    char szEntityText[100+1];
  343.    char szSrc[99+1];
  344.    char szText[199+1];
  345.    long lDist;
  346.    long total;
  347.    UINT index;
  348.    LPUSERDATA lpData;
  349.    RELATION   relation;
  350.    ENTITY     entity;
  351.  
  352.  
  353.    total = 0; lDist = 0;
  354.  
  355.    // what is the destination name?
  356.    index = (UINT)SendDlgItemMessage(hDlg, IDC_DEST, CB_GETCURSEL, 0, 0L);
  357.    SendDlgItemMessage(hDlg, IDC_DEST, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szEntityName);
  358.  
  359.    // what is the source name?
  360.    index = (UINT)SendDlgItemMessage(hDlg, IDC_SOURCE, CB_GETCURSEL, 0, 0L);
  361.    SendDlgItemMessage(hDlg, IDC_SOURCE, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szSrc);
  362.  
  363.    while(TRUE)
  364.    {
  365.       idoGetEntity(hIDO, 0, szEntityName, &entity);
  366.  
  367.       idoEntitySetTextColor(&entity, RGB(255,0,0));
  368.       lstrcpy(szEntityText, idoEntityGetText(&entity));
  369.  
  370.       wsprintf(szText,"%s - %li  %li", (LPSTR)szEntityText, lDist, total);
  371.       SendDlgItemMessage(hDlgdistance, IDC_SHORT, LB_INSERTSTRING, (WPARAM)0, (LPARAM)(LPSTR)szText);
  372.  
  373.       if(lstrcmp(szEntityName, szSrc)==0)  // we are at the source entity
  374.          break;
  375.  
  376.       lpData = idoEntityGetUserData(&entity);
  377.       if(lpData == NULL)
  378.          break;
  379.  
  380.       idoGetRelation(hIDO, 0, (LPSTR)lpData->szNamePreviousLine, &relation);
  381.       idoRelationSetTextColor(&relation, RGB(255,0,0));
  382.       lDist = lpData->lDistance;
  383.       total += lDist;
  384.  
  385.       lstrcpy(szEntityName, lpData->szNamePrevious);
  386.    }
  387.  
  388.    InvalidateRect(hIDO, NULL, TRUE);
  389.    UpdateWindow(hIDO);
  390. }
  391.  
  392.  
  393. void RemoveUserData()
  394. {
  395.    LPUSERDATA lpData;
  396.    RELATION   relation;
  397.    ENTITY     entity;
  398.    long       count;
  399.  
  400.  
  401.    if(IsWindow(hIDO))
  402.    {
  403.       if(idoIterateEntityFirst(hIDO))
  404.          while(idoIterateEntityNext(hIDO, &entity))
  405.          {
  406.             lpData = NULL;
  407.             lpData = idoEntityGetUserData(&entity);
  408.             if(lpData != NULL)
  409.                free(lpData);
  410.  
  411.             idoEntitySetTextColor(&entity, RGB(0,0,0));
  412.  
  413.             // if the object has arrows pointing away, iterate through them
  414.             // to reset their colors
  415.             count = idoEntityGetRelationOutCount(&entity);
  416.             if(count)
  417.             {
  418.                // iterate all the away arrows
  419.                idoEntityIterateRelationOutFirst(&entity);
  420.                while(idoEntityIterateRelationOutNext(&entity, &relation))
  421.                   idoRelationSetTextColor(&relation, RGB(0,0,0));
  422.             }
  423.          } // end WHILE
  424.  
  425.       InvalidateRect(hIDO, NULL, TRUE);
  426.       UpdateWindow(hIDO);
  427.    }  // if IsWindow
  428. }
  429.  
  430.  
  431.  
  432.  
  433. void EmptyStack()
  434. {
  435.    SendDlgItemMessage(hDlgdistance, IDC_LIST, LB_RESETCONTENT, 0, 0L);
  436. }
  437.  
  438. void Push(LPSTR lpName)
  439. {
  440.    SendDlgItemMessage(hDlgdistance, IDC_LIST, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)lpName);
  441. }
  442.  
  443. void Pop(LPSTR lpName)
  444. {
  445.    SendDlgItemMessage(hDlgdistance, IDC_LIST, LB_GETTEXT, (WPARAM)0, (LPARAM)lpName);
  446.    SendDlgItemMessage(hDlgdistance, IDC_LIST, LB_DELETESTRING, (WPARAM)0, 0L);
  447. }
  448. //Regen_CustomCode
  449.  
  450.