home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / componen / interact / demo / data.2 / samples / mfc / DBMODEL / DBMODVW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-20  |  8.0 KB  |  323 lines

  1. // dbmodvw.cpp : implementation of the CDbmodelView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "dbmodel.h"
  6.  
  7. #include "dbmoddoc.h"
  8. #include "dbmodvw.h"
  9. #include "idomfc.h"
  10. #include "textdlg.h"
  11.  
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CDbmodelView
  19.  
  20. IMPLEMENT_DYNCREATE(CDbmodelView, CView)
  21.  
  22. BEGIN_MESSAGE_MAP(CDbmodelView, CView)
  23.      //{{AFX_MSG_MAP(CDbmodelView)
  24.      ON_WM_CREATE()
  25.      ON_WM_SIZE()
  26.      ON_COMMAND(ID_EDIT_ADDE1, OnEditAdde1)
  27.      ON_COMMAND(ID_EDIT_ADDE2, OnEditAdde2)
  28.      ON_COMMAND(ID_EDIT_ADDR1, OnEditAddr1)
  29.      ON_COMMAND(ID_EDIT_ADDR2, OnEditAddr2)
  30.      ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  31.      ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  32.      ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  33.      //}}AFX_MSG_MAP
  34.      ON_IDOM_N_CLICK_ENTITY(OnClickEntity)
  35.      ON_IDOM_N_ENTITYADDED(OnEntityAdded)
  36.      ON_IDOM_N_RELATIONADDREQUEST(OnRelationAddRequest)
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDbmodelView construction/destruction
  41.  
  42. CDbmodelView::CDbmodelView()
  43. {
  44.      // TODO: add construction code here
  45.      m_pIDO = NULL;
  46. }
  47.  
  48. CDbmodelView::~CDbmodelView()
  49. {
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CDbmodelView drawing
  54.  
  55. void CDbmodelView::OnDraw(CDC* pDC)
  56. {
  57.      CDbmodelDoc* pDoc = GetDocument();
  58.      ASSERT_VALID(pDoc);
  59.  
  60.      // TODO: add draw code for native data here
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CDbmodelView diagnostics
  65.  
  66. #ifdef _DEBUG
  67. void CDbmodelView::AssertValid() const
  68. {
  69.      CView::AssertValid();
  70. }
  71.  
  72. void CDbmodelView::Dump(CDumpContext& dc) const
  73. {
  74.      CView::Dump(dc);
  75. }
  76.  
  77. CDbmodelDoc* CDbmodelView::GetDocument() // non-debug version is inline
  78. {
  79.      ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDbmodelDoc)));
  80.      return (CDbmodelDoc*)m_pDocument;
  81. }
  82. #endif //_DEBUG
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CDbmodelView message handlers
  86.  
  87. int CDbmodelView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  88. {
  89.      if (CView::OnCreate(lpCreateStruct) == -1)
  90.           return -1;
  91.      
  92.    long lStyle = WS_VISIBLE | WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_TABSTOP;
  93.    RECT rect;
  94.  
  95.    rect.top = 10;
  96.    rect.bottom = 20;
  97.    rect.left = 0;
  98.    rect.right = 10;
  99.  
  100.    m_pIDO = new CIDO;
  101.    m_pIDO->Create(lStyle, rect, this, 101);
  102.  
  103.    if(m_pIDO)
  104.    {
  105.       // allow the IDO to be editable
  106.       m_pIDO->SetEditMode(TRUE);
  107.       
  108.       // hide the tools palette
  109.       m_pIDO->SetToolsPalette(FALSE);
  110.       
  111.       // rules are not enforced in this diagram
  112.       m_pIDO->SetRulesEnforced(FALSE);
  113.       
  114.       // turn off default menus of IDO
  115. //      m_pIDO->SetPopupMenu(IDOMENU_IDO, FALSE);
  116.       m_pIDO->SetPopupMenu(IDOMENU_ENTITY, FALSE);
  117.       m_pIDO->SetPopupMenu(IDOMENU_LINE, FALSE);
  118.       
  119.       // configure the classes for the diagram
  120.       m_pIDO->ResetPalette();
  121.       m_pIDO->ReadPalette("dbmodel.plt");
  122.    }
  123.  
  124.      
  125.      return 0;
  126. }
  127.  
  128. void CDbmodelView::OnSize(UINT nType, int cx, int cy) 
  129. {
  130.      CView::OnSize(nType, cx, cy);
  131.      
  132.    if(m_pIDO)
  133.    {
  134.       m_pIDO->SetWindowPos(NULL, 0, 20, cx, cy - 20, SWP_NOZORDER | SWP_NOACTIVATE);
  135.    }
  136. }
  137.  
  138.  
  139. void CDbmodelView::OnFilePrint() 
  140. {
  141.    if(m_pIDO)
  142.    {
  143.       m_pIDO->PrintDiagram();
  144.    }
  145. }
  146. void CDbmodelView::OnEditAdde1() 
  147. {
  148.    if(m_pIDO)
  149.    {
  150.       m_pIDO->DragAddEntity("entity1");
  151.    }
  152. }
  153. void CDbmodelView::OnEditAdde2() 
  154. {
  155.    if(m_pIDO)
  156.    {
  157.       m_pIDO->DragAddEntity("entity2");
  158.    }
  159. }
  160. void CDbmodelView::OnEditAddr1() 
  161. {
  162.    if(m_pIDO)
  163.    {
  164.       m_pIDO->DragAddRelation("link1");
  165.    }
  166. }
  167. void CDbmodelView::OnEditAddr2() 
  168. {
  169.    if(m_pIDO)
  170.    {
  171.       m_pIDO->DragAddRelation("link2");
  172.    }
  173. }
  174.  
  175. LRESULT CDbmodelView::OnEntityAdded(WPARAM wParam, LPARAM lParam)
  176. {
  177.    CEntity * pEntity;
  178.    CString text;
  179.  
  180.  
  181.    // get some text to place in this node
  182.    CTextDlg textDlg(this, &text);
  183.    textDlg.DoModal();
  184.  
  185.    // info is passed in the LPARAM of the message - 
  186.    // create a CEntity from this info
  187.    pEntity = new CEntity((LPENTITY)lParam);
  188.    pEntity->SetText(text.GetBuffer(0));
  189.    // you must always call Repaint when changing attributes of an object
  190.    pEntity->Repaint();
  191.    delete pEntity;
  192.    
  193.    return TRUE;
  194. }
  195. LRESULT CDbmodelView::OnClickEntity(WPARAM wParam, LPARAM lParam)
  196. {
  197.    CEntity * pEntity;
  198.    CString text;
  199.  
  200.  
  201.    if(lParam == NOTIFY_CLICK_LEFT_DBL)
  202.    {
  203.       // info is passed in the LPARAM of the message - 
  204.       // create a CEntity from this info
  205.       pEntity = m_pIDO->GetNotifyEntity();
  206.       if(!pEntity)
  207.          return FALSE;
  208.       
  209.       text = pEntity->GetText();
  210.  
  211.       // get some text to place in this node
  212.       CTextDlg textDlg(this, &text);
  213.       textDlg.DoModal();
  214.  
  215.       pEntity->SetText(text.GetBuffer(0));
  216.       // you must always call Repaint when changing attributes of an object
  217.       pEntity->Repaint();
  218.  
  219.       delete pEntity;
  220.    }
  221.    
  222.    return TRUE;
  223. }
  224.  
  225.  
  226.  
  227. LRESULT CDbmodelView::OnRelationAddRequest(WPARAM wParam, LPARAM lParam)
  228. {
  229.    CRelation * pRelation;
  230.    CString classname;
  231.    
  232.    
  233.    // info is passed in the LPARAM of the message - 
  234.    // create a CEntity from this info
  235.    pRelation = new CRelation((LPRELATION)lParam);
  236.    classname = pRelation->GetClassName();
  237.    
  238.    if(IDNO == MessageBox("Are you sure you want to add this link?", "Question", MB_YESNO | MB_ICONQUESTION))
  239.       m_pIDO->CancelAction();
  240.       
  241.    delete pRelation;
  242.    
  243.    return TRUE;
  244. }
  245.  
  246.  
  247. void CDbmodelView::OnFileOpen() 
  248. {
  249.    int  bOFRetCode;
  250.    char szOFFilePath[80+1];
  251.    char szOFFileTitle[40+1];
  252.    char szOFFileDialogExt[] = "IDO";
  253.    char szOFFileDialogFilter[] = "Diagram File|*.IDO||";
  254.  
  255.  
  256.  
  257.    CFileDialog fileDialogOF(TRUE, szOFFileDialogExt, NULL,
  258.            OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,szOFFileDialogFilter);
  259.  
  260.    szOFFilePath[0] = '\0';
  261.    fileDialogOF.m_ofn.lStructSize = sizeof(OPENFILENAME);
  262.    fileDialogOF.m_ofn.hwndOwner = m_hWnd;
  263.    fileDialogOF.m_ofn.lpstrCustomFilter = (LPSTR)NULL;
  264.    fileDialogOF.m_ofn.nMaxCustFilter = 0L;
  265.    fileDialogOF.m_ofn.nFilterIndex = 0;
  266.    fileDialogOF.m_ofn.lpstrFile = szOFFilePath;
  267.    fileDialogOF.m_ofn.nMaxFile = 256;
  268.    fileDialogOF.m_ofn.lpstrFileTitle = szOFFileTitle;
  269.    fileDialogOF.m_ofn.nMaxFileTitle = sizeof(szOFFileTitle);
  270.    fileDialogOF.m_ofn.lpstrInitialDir = NULL;
  271.    fileDialogOF.m_ofn.lpstrTitle = (LPSTR)"Open Diagram File";
  272.    fileDialogOF.m_ofn.nFileOffset = 0;
  273.    fileDialogOF.m_ofn.nFileExtension = 0;
  274.  
  275.    bOFRetCode = fileDialogOF.DoModal();
  276.  
  277.  
  278.    if(bOFRetCode)
  279.    {
  280.       m_pIDO->ReadDiagram(szOFFilePath);
  281.       SetWindowText(szOFFilePath);
  282.    }
  283.  
  284. }
  285.  
  286. void CDbmodelView::OnFileSave() 
  287. {
  288.    int  bSFRetCode;
  289.    char szSFFilePath[80+1];
  290.    char szSFFileTitle[40+1];
  291.    char szSFFileDialogExt[] = "IDO";
  292.    char szSFFileDialogFilter[] = "Diagram File|*.IDO||";
  293.  
  294.  
  295.  
  296.    CFileDialog fileDialogSF(FALSE, szSFFileDialogExt, NULL,
  297.            OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST,szSFFileDialogFilter);
  298.  
  299.    szSFFilePath[0] = '\0';
  300.    fileDialogSF.m_ofn.lStructSize = sizeof(OPENFILENAME);
  301.    fileDialogSF.m_ofn.hwndOwner = m_hWnd;
  302.    fileDialogSF.m_ofn.lpstrCustomFilter = (LPSTR)NULL;
  303.    fileDialogSF.m_ofn.nMaxCustFilter = 0L;
  304.    fileDialogSF.m_ofn.nFilterIndex = 0;
  305.    fileDialogSF.m_ofn.lpstrFile = szSFFilePath;
  306.    fileDialogSF.m_ofn.nMaxFile = 256;
  307.    fileDialogSF.m_ofn.lpstrFileTitle = szSFFileTitle;
  308.    fileDialogSF.m_ofn.nMaxFileTitle = sizeof(szSFFileTitle);
  309.    fileDialogSF.m_ofn.lpstrInitialDir = NULL;
  310.    fileDialogSF.m_ofn.lpstrTitle = (LPSTR)"Save Diagram File";
  311.    fileDialogSF.m_ofn.nFileOffset = 0;
  312.    fileDialogSF.m_ofn.nFileExtension = 0;
  313.  
  314.    bSFRetCode = fileDialogSF.DoModal();
  315.  
  316.  
  317.    if(bSFRetCode)
  318.    {
  319.       m_pIDO->SaveDiagram(szSFFilePath);
  320.       SetWindowText(szSFFilePath);
  321.    }
  322. }
  323.