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

  1.  
  2. //lRegen_FileHeading
  3. //lRegen_FileHeading
  4.  
  5.      /********************************************************************
  6.       *
  7.       *   Source File:  distance.cpp
  8.       *   Author:       RC
  9.       *   Module:       Dialog implementation for travel
  10.       *   Program Desc:
  11.       *   Date:         Mon Feb 12 15:33:35 1996
  12.       *   Language:     MFC C++ 3.0
  13.       *
  14.       ********************************************************************/
  15.  
  16. #include ".\travel.h"
  17. //Regen_Cdistance_Top
  18.  
  19. #include <stdlib.h> // for atoi support
  20.  
  21. //Regen_Cdistance_Top
  22.  
  23. IMPLEMENT_DYNCREATE(CdistanceDlg, CDialog)
  24.  
  25. BEGIN_MESSAGE_MAP(CdistanceDlg, CDialog)
  26.  
  27.    //{{AFX_MSG_MAP(CdistanceDlg)
  28.    ON_BN_CLICKED(IDOK, OnOK)
  29.    ON_WM_CLOSE()
  30.    ON_WM_DESTROY()
  31.    ON_WM_SHOWWINDOW()
  32.    ON_CBN_SELCHANGE(IDC_SOURCE, Oncbn_selchangeidc_source)
  33.    ON_CBN_SELCHANGE(IDC_DEST, Oncbn_selchangeidc_dest)
  34.    //}}AFX_MSG_MAP
  35.  
  36.    //Regen_Map
  37.    //Regen_Map
  38.  
  39. END_MESSAGE_MAP()
  40.  
  41. // ------------ Define CdistanceDlg constructor -----------
  42. CdistanceDlg::CdistanceDlg(UINT nIDTemplate, CWnd* pParentWnd)
  43.    : CDialog((nIDTemplate) ? nIDTemplate : distance, pParentWnd)
  44. {
  45.  
  46.    //Regen_distance_VarConstructor
  47.    //Regen_distance_VarConstructor
  48.    //lRegen_distance_Constructor
  49.    //lRegen_distance_Constructor
  50.  
  51.    //{{AFX_DATA_INIT(CdistanceDlg)
  52.    //}}AFX_DATA_INIT
  53. }
  54.  
  55. // Define CdistanceDlg destructor
  56. CdistanceDlg::~CdistanceDlg()
  57. {
  58.    //Regen_distance_Destructor
  59.    //Regen_distance_Destructor
  60. }
  61.  
  62.    //lRegen_Class_Member_Functions
  63.    //lRegen_Class_Member_Functions
  64.  
  65. BOOL CdistanceDlg::OnInitDialog()
  66. {
  67.    //Regen_Cdistance_BeginInitDlg
  68.    //Regen_Cdistance_BeginInitDlg
  69.    CDialog::OnInitDialog();
  70.    //Regen_Cdistance_InitDialog
  71.    CEntity entity;
  72.  
  73.    if(m_pIDO)
  74.    {
  75.       if(m_pIDO->IterateEntityFirst())
  76.          while(m_pIDO->IterateEntityNext(&entity))  // extract the user names of all the entities
  77.          {
  78.             SendDlgItemMessage(IDC_SOURCE, CB_ADDSTRING, 0, (LPARAM)(LPSTR)entity.m_entity.name);
  79.             SendDlgItemMessage(IDC_DEST, CB_ADDSTRING, 0, (LPARAM)(LPSTR)entity.m_entity.name);
  80.          }
  81.  
  82.       SendDlgItemMessage(IDC_SOURCE, CB_SETCURSEL, 0, 0L);
  83.       SendDlgItemMessage(IDC_DEST, CB_SETCURSEL, 0, 0L);
  84.    }
  85.  
  86.    ClearAllItems();
  87.  
  88.    Stack.Init(this);
  89.  
  90.    //Regen_Cdistance_InitDialog
  91.    return TRUE;
  92. }
  93.  
  94. CdistanceDlg::CdistanceDlg()   //Default Constructor for DYNCREATE
  95. {
  96. //Regen_DefConstructor
  97. //Regen_DefConstructor
  98. }
  99.  
  100. void CdistanceDlg::DoDataExchange(CDataExchange* pDX)
  101. {
  102.    CDialog::DoDataExchange(pDX);
  103.    //lRegen_distance_DDX
  104.    //lRegen_distance_DDX
  105.    //{{AFX_DATA_MAP(CdistanceDlg)
  106.    //}}AFX_DATA_MAP
  107. }
  108.  
  109. // ----------- OnClose for Termination of the Dialog --------
  110. void CdistanceDlg::OnClose()
  111. {
  112.    //Regen_distance_PreClose
  113.    //Regen_distance_PreClose
  114.  
  115.    EndDialog(IDCANCEL);
  116.  
  117.    //Regen_distance_PostClose
  118.    //Regen_distance_PostClose
  119. }
  120.  
  121. void CdistanceDlg::OnDestroy()
  122. {
  123.    //Regen_CdistanceDlg_wm_destroy
  124.    RemoveUserData();
  125.    //Regen_CdistanceDlg_wm_destroy
  126.    CDialog::OnDestroy();
  127. }
  128.  
  129. void CdistanceDlg::OnShowWindow(BOOL bShow, UINT nStatus)
  130. {
  131.    //Regen_CdistanceDlg_wm_showwindow
  132.    //Regen_CdistanceDlg_wm_showwindow
  133.    CDialog::OnShowWindow(bShow, nStatus);
  134. }
  135.  
  136. void CdistanceDlg::Oncbn_selchangeidc_source()
  137. {
  138.    //Regen_CdistanceDlg_idc_source_cbn_selchange
  139.  
  140.    CalculateDistance();
  141.  
  142.    //Regen_CdistanceDlg_idc_source_cbn_selchange
  143. }
  144.  
  145. void CdistanceDlg::Oncbn_selchangeidc_dest()
  146. {
  147.    //Regen_CdistanceDlg_idc_dest_cbn_selchange
  148.  
  149.    CalculateDistance();
  150.  
  151.    //Regen_CdistanceDlg_idc_dest_cbn_selchange
  152. }
  153.  
  154. void CdistanceDlg::OnOK()
  155. {
  156.    //lRegen_CdistanceDlg_OK_Var
  157.    //lRegen_CdistanceDlg_OK_Var
  158.    EndDialog(IDOK);
  159.    //Regen_distance_OK_End
  160.    //Regen_distance_OK_End
  161. }
  162.  
  163. void CdistanceDlg::Ondistanceidc_short()
  164. {
  165.    //lRegen_CdistanceDlg_distanceidc_short_Var
  166.    //lRegen_CdistanceDlg_distanceidc_short_Var
  167.    //Regen_distance_distanceidc_short_End
  168.    //Regen_distance_distanceidc_short_End
  169. }
  170.  
  171. void CdistanceDlg::Ondistanceidc_list()
  172. {
  173.    //lRegen_CdistanceDlg_distanceidc_list_Var
  174.    //lRegen_CdistanceDlg_distanceidc_list_Var
  175.    //Regen_distance_distanceidc_list_End
  176.    //Regen_distance_distanceidc_list_End
  177. }
  178.  
  179.  
  180. //Regen_Code
  181.  
  182. // ------------ Define CdistanceDlg constructor -----------
  183. CdistanceDlg::CdistanceDlg(UINT nIDTemplate, CWnd* pParentWnd, CIDO *pIDO)
  184.    : CDialog((nIDTemplate) ? nIDTemplate : distance, pParentWnd)
  185. {
  186.    m_pIDO = pIDO;
  187. }
  188.  
  189. void CdistanceDlg::ClearAllItems()
  190. {
  191.    LPUSERDATA  lpData;
  192.    CRelation * relation;
  193.    CEntity   * entity;
  194.    long        count;
  195.  
  196.  
  197.    if(m_pIDO)
  198.    {
  199.       if(m_pIDO->IterateEntityFirst())
  200.          while(entity = m_pIDO->IterateEntityNext())
  201.          {
  202.             // if the object does not have a user data object, create one and store it
  203.             lpData = (LPUSERDATA)entity->GetUserData();
  204.             if(lpData == NULL)
  205.                lpData = (LPUSERDATA)calloc(1, sizeof(USERDATA));
  206.  
  207.             lpData->lDistance = -1;
  208.             lstrcpy(lpData->szNamePrevious, "");
  209.  
  210.             entity->SetUserData(lpData);
  211.             entity->SetTextColor(RGB(0,0,0));
  212.  
  213.             // if the object has arrows pointing away, iterate through them
  214.             // to reset their colors
  215.             count = entity->GetRelationOutCount();
  216.             if(count)
  217.             {
  218.                // iterate all the away arrows
  219.                entity->IterateRelationOutFirst();
  220.                while(relation = entity->IterateRelationOutNext())
  221.                   relation->SetTextColor(RGB(0,0,0));
  222.             }
  223.          }
  224.  
  225.       m_pIDO->InvalidateRect(NULL, TRUE);
  226.       m_pIDO->UpdateWindow();
  227.    }
  228. }
  229.  
  230.  
  231. void CdistanceDlg::CalculateDistance()
  232. {
  233.    char       szEntityName[100+1];
  234.    LPUSERDATA lpData;
  235.    CEntity    entity;
  236.    UINT       index;
  237.  
  238.  
  239.    ClearAllItems();
  240.  
  241.    index = (UINT)SendDlgItemMessage(IDC_SOURCE, CB_GETCURSEL, 0, 0L);
  242.    SendDlgItemMessage(IDC_SOURCE, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szEntityName);
  243.    SendDlgItemMessage(IDC_SHORT, LB_RESETCONTENT, 0, 0L);
  244.  
  245.    m_pIDO->GetEntity(0, szEntityName, &entity);
  246.    lpData = (LPUSERDATA)entity.GetUserData();
  247.    if(lpData != NULL)
  248.       lpData->lDistance = 0;
  249.  
  250.    Stack.EmptyStack();
  251.    Stack.Push(szEntityName);  // push entity name from the SOURCE combobox
  252.  
  253.    while(SendDlgItemMessage(IDC_LIST, LB_GETCOUNT, 0, 0L))  // while the stack is not empty
  254.    {
  255.       Stack.Pop(szEntityName);
  256.       CalcDist(szEntityName);
  257.    }
  258.  
  259.    WalkList();
  260. }
  261.  
  262.  
  263. // pass the name of the previous item here
  264. void CdistanceDlg::CalcDist(LPSTR lpName)
  265. {
  266.    LPUSERDATA  lpData;
  267.    CRelation   relation;
  268.    CEntity     this_entity;
  269.    CEntity     next_entity;
  270.    long        count, Distance, CurrDist;
  271.    char        szText[50+1];
  272.  
  273.  
  274.    m_pIDO->GetEntity(0, lpName, &this_entity);
  275.    lpData = (LPUSERDATA)this_entity.GetUserData();
  276.    if(lpData != NULL)
  277.       CurrDist = lpData->lDistance;  // save the current distance.
  278.  
  279.  
  280.    // if the object has arrows pointing away, iterate through them
  281.    count = this_entity.GetRelationOutCount();
  282.    if(count)
  283.    {
  284.       // iterate all the away arrows
  285.       this_entity.IterateRelationOutFirst();
  286.       while(this_entity.IterateRelationOutNext(&relation))
  287.       {
  288.          lstrcpy(szText, relation.GetText());   // get the line text
  289.          Distance = (long)atoi(szText);               // do a get prop for this
  290.  
  291.          relation.DestinationEntity(&next_entity);
  292.          lpData = (LPUSERDATA)next_entity.GetUserData();
  293.          if(lpData != NULL)
  294.          {
  295.             // if the distance to the prev item is less, do this
  296.             if( (lpData->lDistance == -1) || (lpData->lDistance > Distance + CurrDist) )
  297.             {
  298.                lstrcpy(lpData->szNamePrevious, lpName);
  299.                lstrcpy(lpData->szNamePreviousLine, (LPSTR)relation.m_relation.name);
  300.                lpData->lDistance = Distance + CurrDist;
  301.                Stack.Push((LPSTR)next_entity.m_entity.name);
  302.             }
  303.          }
  304.       } // end WHILE
  305.    }
  306.  
  307.    count = this_entity.GetRelationInCount();
  308.    if(count)
  309.    {
  310.       //iterate all the arrows pointing to this entity
  311.       this_entity.IterateRelationInFirst();
  312.       while(this_entity.IterateRelationInNext(&relation))
  313.  
  314.       {
  315.          lstrcpy(szText, relation.GetText());  // get the line text
  316.          Distance = (long)atoi(szText);  // do a get prop for this;
  317.  
  318.          relation.SourceEntity(&next_entity);
  319.          lpData = (LPUSERDATA)next_entity.GetUserData();
  320.          if(lpData != NULL)
  321.          {
  322.             // if the distance to the prev item is less, do this
  323.             if( (lpData->lDistance == -1) || (lpData->lDistance > Distance + CurrDist) )
  324.             {
  325.                lstrcpy(lpData->szNamePrevious, lpName);
  326.                lstrcpy(lpData->szNamePreviousLine, relation.m_relation.name);
  327.                lpData->lDistance = Distance + CurrDist;
  328.                Stack.Push((LPSTR)next_entity.m_entity.name);
  329.             }
  330.          }
  331.  
  332.       } // end WHILE
  333.    } // end count
  334.  
  335. }
  336.  
  337.  
  338. void CdistanceDlg::WalkList()
  339. {
  340.    char szEntityName[100+1];
  341.    char szSrc[99+1];
  342.    char szText[199+1];
  343.    long lDist;
  344.    long total;
  345.    UINT index;
  346.    LPUSERDATA  lpData;
  347.    CRelation   relation;
  348.    CEntity     entity;
  349.  
  350.  
  351.    total = 0; lDist = 0;
  352.  
  353.    // what is the destination name?
  354.    index = (UINT)SendDlgItemMessage(IDC_DEST, CB_GETCURSEL, 0, 0L);
  355.    SendDlgItemMessage(IDC_DEST, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szEntityName);
  356.  
  357.    // what is the source name?
  358.    index = (UINT)SendDlgItemMessage(IDC_SOURCE, CB_GETCURSEL, 0, 0L);
  359.    SendDlgItemMessage(IDC_SOURCE, CB_GETLBTEXT, (WPARAM)index, (LPARAM)(LPSTR)szSrc);
  360.  
  361.    while(TRUE)
  362.    {
  363.       m_pIDO->GetEntity(0, szEntityName, &entity);
  364.  
  365.       entity.SetTextColor(RGB(255,0,0));
  366.  
  367.       wsprintf(szText,"%s - %li  %li", (LPSTR)szEntityName, lDist, total);
  368.       SendDlgItemMessage(IDC_SHORT, LB_INSERTSTRING, (WPARAM)0, (LPARAM)(LPSTR)szText);
  369.  
  370.       if(lstrcmp(szEntityName, szSrc)==0)  // we are at the source entity
  371.          break;
  372.  
  373.       lpData = (LPUSERDATA)entity.GetUserData();
  374.       if(lpData == NULL)
  375.          break;
  376.  
  377.       m_pIDO->GetRelation(0, (LPSTR)lpData->szNamePreviousLine, &relation);
  378.       relation.SetTextColor(RGB(255,0,0));
  379.       lDist = lpData->lDistance;
  380.       total += lDist;
  381.  
  382.       lstrcpy(szEntityName, lpData->szNamePrevious);
  383.    }
  384.  
  385.    m_pIDO->InvalidateRect(NULL, TRUE);
  386.    m_pIDO->UpdateWindow();
  387. }
  388.  
  389.  
  390. void CdistanceDlg::RemoveUserData()
  391. {
  392.    LPUSERDATA  lpData;
  393.    CRelation   relation;
  394.    CEntity     entity;
  395.    long        count;
  396.  
  397.  
  398.    if(m_pIDO)
  399.    {
  400.       if(m_pIDO->IterateEntityFirst())
  401.          while(m_pIDO->IterateEntityNext(&entity))
  402.          {
  403.             lpData = NULL;
  404.             lpData = (LPUSERDATA)entity.GetUserData();
  405.             if(lpData != NULL)
  406.                free(lpData);
  407.  
  408.             entity.SetTextColor(RGB(0,0,0));
  409.  
  410.             // if the object has arrows pointing away, iterate through them
  411.             // to reset their colors
  412.             count = entity.GetRelationOutCount();
  413.             if(count)
  414.             {
  415.                // iterate all the away arrows
  416.                entity.IterateRelationOutFirst();
  417.                while(entity.IterateRelationOutNext(&relation))
  418.                   relation.SetTextColor(RGB(0,0,0));
  419.             }
  420.          } // end WHILE
  421.  
  422.       m_pIDO->InvalidateRect(NULL, TRUE);
  423.       m_pIDO->UpdateWindow();
  424.    }  // if pIDO
  425. }
  426.  
  427.  
  428. pvStack::pvStack()
  429. {
  430.    ;
  431. }
  432.  
  433. pvStack::~pvStack()
  434. {
  435.    ;
  436. }
  437.  
  438. void pvStack::Init(CWnd * hWnd)
  439. {
  440.    m_pList = hWnd;
  441. }
  442.  
  443. void pvStack::EmptyStack()
  444. {
  445.    m_pList->SendDlgItemMessage(IDC_LIST, LB_RESETCONTENT, 0, 0L);
  446. }
  447.  
  448. void pvStack::Push(LPCSTR lpName)
  449. {
  450.    m_pList->SendDlgItemMessage(IDC_LIST, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)(LPSTR)lpName);
  451. }
  452.  
  453. void pvStack::Pop(LPCSTR lpName)
  454. {
  455.    m_pList->SendDlgItemMessage(IDC_LIST, LB_GETTEXT, (WPARAM)0, (LPARAM)(LPSTR)lpName);
  456.    m_pList->SendDlgItemMessage(IDC_LIST, LB_DELETESTRING, (WPARAM)0, 0L);
  457. }
  458.  
  459. //Regen_Code
  460.  
  461.  
  462.  
  463.