home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Samples / VfbEx / DynamicVF.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-10  |  11.1 KB  |  440 lines

  1. // -------------------------------------------------------------------------
  2. // DynamicVF.cpp
  3. // -------------------------------------------------------------------------
  4.  
  5. #include "stdafx.h"
  6. #include "DynamicVf.h"
  7. #include "resource.h"
  8. #include "VfbEx.h"
  9. #include "VfbLoad.h"
  10. #include "VfbExView.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18.  
  19. // -------------------------------------------------------------------------
  20. // TestData Constructor
  21. // -------------------------------------------------------------------------
  22. TestData::TestData()
  23. {
  24. }
  25.  
  26. TestData& TestData::operator =(TestData &that)
  27. {
  28.     // RemoveAll();
  29.     m_fld.Copy(that.m_fld);
  30.     return *this;
  31. }
  32.  
  33. // -------------------------------------------------------------------------
  34. // Class DynamicVfDtl
  35. // -------------------------------------------------------------------------
  36. DynamicVfDtl::DynamicVfDtl()
  37. {
  38. }
  39.  
  40. BEGIN_MESSAGE_MAP(DynamicVf, VForm)
  41.     //{{AFX_MSG_MAP(DynamicVf)
  42.     ON_WM_SIZE()
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. // -------------------------------------------------------------------------
  47. // Class DynamicVf
  48. // -------------------------------------------------------------------------
  49. DynamicVf::DynamicVf()
  50. {
  51.     m_nFldCnt = 0;
  52.     m_bDropSource = FALSE;
  53.  
  54.     RowHeight(33);
  55.     RowWidth(511);
  56.     GridRowHeight(20);
  57.     GridMode(FALSE);
  58.  
  59.     // Create header and add it to the VForm
  60.     DynamicVfHdr *pHdr = new DynamicVfHdr();
  61.     SetHeader(pHdr, FALSE);
  62.  
  63.     // Create detail rows NOW and add them to the VForm
  64.     VRow *pDtlRow = new DynamicVfDtl();
  65.     VRow *pTmpRow = new DynamicVfDtl();
  66.     SetDetailRows(pDtlRow, pTmpRow, FALSE);
  67.  
  68.     // Create footer and add it to the VForm
  69.     DynamicVfFtr *pFtr = new DynamicVfFtr();
  70.     SetFooter(pFtr, FALSE);
  71.  
  72.     Font(VForm::GetFonts()->GetOrAdd(_T("Arial"),
  73.             100, FALSE, FALSE, FALSE));
  74. }
  75.  
  76. // -------------------------------------------------------------------------
  77. // Function to create a DynamicVfDtl
  78. // -------------------------------------------------------------------------
  79. VRow* DynamicVf::InitRow()
  80. {
  81.     // ----------- note - called from OnCreate ---------
  82.     DynamicVfDtl *pRow = new DynamicVfDtl();
  83.     return(pRow);
  84. }
  85.  
  86.  
  87. // -------------------------------------------------------------------------
  88. // 
  89. // -------------------------------------------------------------------------
  90. BOOL DynamicVf::LoadVfbFile(LPCSTR szVfbFile)
  91. {
  92.     BOOL rc;
  93.     CVfBldrDoc myDoc;
  94.  
  95.     // Call base class implementation
  96.     rc = VForm::LoadVfbFile(szVfbFile, &myDoc);
  97.     m_bDropSource = myDoc.DropSource()    ;    // Set our DropSource flag
  98.     return rc;
  99. }
  100.  
  101. void DynamicVf::OnSize(UINT nType, int cx, int cy) 
  102. {
  103.     VForm::OnSize(nType, cx, cy);
  104.  
  105.     // This handles allowing for horz scroll size when dynamically
  106.     // added by VForm (only on initial display if smaller than min size)
  107.     CVfbExView* pView = (CVfbExView*) GetParent();
  108.     pView->SetScrollByVForm();
  109. }
  110.  
  111. // -------------------------------------------------------------------------
  112. // 
  113. // -------------------------------------------------------------------------
  114. CPalette* DynamicVf::GetBackgroundPalette()
  115. {
  116.     CPalette *pPal;
  117.     VRow *pSection;
  118.  
  119.     pSection = DtlRow();
  120.     if(pSection)
  121.     {
  122.         pPal = pSection->GetBackPalette();
  123.         if(pPal)    return pPal;
  124.     }
  125.  
  126.     pSection = Header();
  127.     if(pSection)
  128.     {
  129.         pPal = pSection->GetBackPalette();
  130.         if(pPal)    return pPal;
  131.     }
  132.  
  133.     pSection = Footer();
  134.     if(pSection)
  135.     {
  136.         pPal = pSection->GetBackPalette();
  137.         if(pPal)    return pPal;
  138.     }
  139.  
  140.     return 0;
  141. }
  142.  
  143.  
  144. // -------------------------------------------------------------------------
  145. // Called when a row needs to be put on the screen
  146. // -------------------------------------------------------------------------
  147. BOOL DynamicVf::OnGetRow(VRow *pRow)
  148. {
  149.     DynamicVfDtl *r = (DynamicVfDtl*)pRow;
  150.     long idx = r->RowId();
  151.     TestData *pData = &m_data[idx];
  152.     int i, j;
  153.     VCtl *pCtl;
  154.  
  155.     // Loop through fields - Get data for appropriate index
  156.     j=0;
  157.     for(i=0; i < r->CtlCount(); i++)
  158.     {
  159.         pCtl = r->GetCtlPtr(i);
  160.         switch(pCtl->IsA())
  161.         {
  162.         case VCTL_EDIT:        pCtl->Text(pData->m_fld[j++]);        break;
  163.         case VCTL_DROPDOWN:    pCtl->Text(pData->m_fld[j++]);        break;
  164.         case VCTL_CHECKBOX:
  165.             {
  166.                 VCheckBox *p = (VCheckBox *) pCtl;
  167.                 p->Checked(pData->m_fld[j++] == "Y");
  168.                 break;
  169.             }
  170.         case VCTL_RADIOLIST:
  171.             {
  172.                 VRadioList *p = (VRadioList *) pCtl;
  173.                 int nItem = atoi(pData->m_fld[j++]); 
  174.                 p->SelItem(nItem);
  175.                 break;
  176.             }
  177.         case VCTL_PICTCYCLE:
  178.             {
  179.                 VPictCycle *p = (VPictCycle *) pCtl;
  180.                 int nIdx = atoi(pData->m_fld[j++]); 
  181.                 p->Index(nIdx);
  182.                 break;
  183.             }
  184.         }
  185.     }
  186.  
  187.     return TRUE;            // Succesfully Gotten
  188. }
  189.  
  190. // -------------------------------------------------------------------------
  191. // Called when a row needs to be saved (in the data)
  192. // -------------------------------------------------------------------------
  193. BOOL DynamicVf::OnSaveRow(VRow *pRow)
  194. {
  195.     DynamicVfDtl *r = (DynamicVfDtl*)pRow;
  196.     long idx = r->RowId();
  197.     TestData *pData = &m_data[idx];
  198.     int i, j;
  199.     VCtl *pCtl;
  200.     CString s1;
  201.  
  202.     // Loop through fields - Get data for appropriate index
  203.     j=0;
  204.     for(i=0; i < r->CtlCount(); i++)
  205.     {
  206.         pCtl = r->GetCtlPtr(i);
  207.         switch(pCtl->IsA())
  208.         {
  209.         case VCTL_EDIT:        pData->m_fld[j++] = pCtl->Text();    break;
  210.         case VCTL_DROPDOWN:    pData->m_fld[j++] = pCtl->Text();    break;
  211.         case VCTL_CHECKBOX:    
  212.             {
  213.                 VCheckBox *p = (VCheckBox *) pCtl;
  214.                 pData->m_fld[j++] = p->Checked() ? "Y" : "N";
  215.                 break;
  216.             }
  217.         case VCTL_RADIOLIST:
  218.             {
  219.                 VRadioList *p = (VRadioList *) pCtl;
  220.                 s1.Format("%d", p->SelItem());
  221.                 pData->m_fld[j++] = s1;
  222.                 break;
  223.             }
  224.         case VCTL_PICTCYCLE:
  225.             {
  226.                 VPictCycle *p = (VPictCycle *) pCtl;
  227.                 s1.Format("%d", p->Index());
  228.                 pData->m_fld[j++] = s1;
  229.                 break;
  230.             }
  231.         }
  232.     }
  233.  
  234.     return TRUE;            // Succesfully Saved
  235. }
  236.  
  237. // -------------------------------------------------------------------------
  238. // 
  239. // -------------------------------------------------------------------------
  240. void DynamicVf::ResetFieldCount()
  241. {
  242.     m_nFldCnt = CalcFieldCount(m_pDtlRow);
  243. }
  244.  
  245. // -------------------------------------------------------------------------
  246. // 
  247. // -------------------------------------------------------------------------
  248. int DynamicVf::CalcFieldCount(VRow *pRow)
  249. {
  250.     int i, nCnt;
  251.     VCtl *pCtl;
  252.  
  253.     // Loop through fields - Get data for appropriate index
  254.     nCnt=0;
  255.     for(i=0; i < pRow->CtlCount(); i++)
  256.     {
  257.         pCtl = pRow->GetCtlPtr(i);
  258.         switch(pCtl->IsA())
  259.         {
  260.         case VCTL_EDIT:
  261.         case VCTL_DROPDOWN:
  262.         case VCTL_CHECKBOX:
  263.         case VCTL_RADIOLIST:
  264.         case VCTL_PICTCYCLE:
  265.             nCnt++;
  266.             break;
  267.         }
  268.     }
  269.     return nCnt;            // Succesfully Added
  270. }
  271.  
  272.  
  273. // -------------------------------------------------------------------------
  274. // Called when a row needs to added (to the data)
  275. // -------------------------------------------------------------------------
  276. BOOL DynamicVf::OnAddRow(VRow *pRow)
  277. {
  278.     DynamicVfDtl *r = (DynamicVfDtl*)pRow;
  279.     long idx = r->RowId();
  280.     CString s1;
  281.     TestData data;
  282.     int i, j;
  283.     VCtl *pCtl;
  284.  
  285.     m_data.SetAtGrow(idx, data);
  286.     TestData *pData = &m_data[idx];
  287.     pData->m_fld.SetSize(m_nFldCnt);    // Grow the array to correct size
  288.  
  289.     // Loop through fields - Get data for appropriate index
  290.     j=0;
  291.     for(i=0; i < r->CtlCount(); i++)
  292.     {
  293.         pCtl = r->GetCtlPtr(i);
  294.         switch(pCtl->IsA())
  295.         {
  296.         case VCTL_EDIT:        pData->m_fld[j++] = pCtl->Text();    break;
  297.         case VCTL_DROPDOWN:    pData->m_fld[j++] = pCtl->Text();    break;
  298.         case VCTL_CHECKBOX:    
  299.             {
  300.                 VCheckBox *p = (VCheckBox *) pCtl;
  301.                 pData->m_fld[j++] = p->Checked() ? "Y" : "N";
  302.                 break;
  303.             }
  304.         case VCTL_RADIOLIST:
  305.             {
  306.                 VRadioList *p = (VRadioList *) pCtl;
  307.                 s1.Format("%d", p->SelItem());
  308.                 pData->m_fld[j++] = s1;
  309.                 break;
  310.             }
  311.         case VCTL_PICTCYCLE:
  312.             {
  313.                 VPictCycle *p = (VPictCycle *) pCtl;
  314.                 s1.Format("%d", p->Index());
  315.                 pData->m_fld[j++] = s1;
  316.                 break;
  317.             }
  318.         }
  319.     }
  320.  
  321.     return TRUE;            // Succesfully Added
  322. }
  323.  
  324. // -------------------------------------------------------------------------
  325. // OnStartAdd   - Set default values for a new record/row
  326. // -------------------------------------------------------------------------
  327. void DynamicVf::OnStartAdd(VRow *pRow)
  328. {
  329.     DynamicVfDtl *r = (DynamicVfDtl*)pRow;
  330.     long idx = r->RowId();
  331.     int i, j;
  332.     VCtl *pCtl;
  333.  
  334.     // Loop through fields - Get data for appropriate index
  335.     j=0;
  336.     for(i=0; i < r->CtlCount(); i++)
  337.     {
  338.         pCtl = r->GetCtlPtr(i);
  339.         switch(pCtl->IsA())
  340.         {
  341.         case VCTL_EDIT:        pCtl->Text("");    break;
  342.         case VCTL_DROPDOWN:    pCtl->Text("");    break;
  343.         case VCTL_CHECKBOX:    
  344.             {
  345.                 VCheckBox *p = (VCheckBox *) pCtl;
  346.                 p->Checked(FALSE);
  347.                 break;
  348.             }
  349.         case VCTL_RADIOLIST:    
  350.             {
  351.                 VRadioList *p = (VRadioList *) pCtl;
  352.                 p->SelItem(0);
  353.                 break;
  354.             }
  355.         case VCTL_PICTCYCLE:
  356.             {
  357.                 VPictCycle *p = (VPictCycle *) pCtl;
  358.                 p->Index(0);
  359.                 break;
  360.             }
  361.         }
  362.     }
  363. }
  364.  
  365. // -------------------------------------------------------------------------
  366. // Called when row(s) needs to be deleted from the data
  367. // -------------------------------------------------------------------------
  368. BOOL DynamicVf::OnDelete()
  369. {
  370.     CString s1;
  371.     int rc, i;
  372.     long nPriorCnt=0;
  373.  
  374.     long nNumSelected = m_selectedList.Count();
  375.     long nRowId = m_selectedList.Last();
  376.  
  377.     s1.Format("You have selected to delete %ld rows", nNumSelected);
  378.     s1 += "\nDo you wish to continue?";
  379.     rc = AfxMessageBox(s1, MB_OKCANCEL | MB_ICONQUESTION);
  380.     if(rc != IDOK)
  381.         return TRUE;                            // We handled delete key
  382.  
  383.     for(i=0; i<nNumSelected; i++)
  384.     {
  385.         if(nRowId < CurRowId())
  386.             nPriorCnt++;                        // Count prior rows deleted
  387.         m_data.RemoveAt(nRowId);                // Delete this row
  388.         nRowId = m_selectedList.Prev();            // Get next row
  389.     }
  390.  
  391.     // Reset our Row Count and move to appropriate new row
  392.     // - by subtracting the number of rows deleted above us,
  393.     //   we stay on the same row.
  394.     ResetNumRows(m_data.GetSize(), CurRowId() - nPriorCnt);
  395.     return TRUE;                                // We handled delete key
  396. }
  397.  
  398.  
  399. // -------------------------------------------------------------------------
  400. // Called from OnLButtonDown
  401. // -------------------------------------------------------------------------
  402. BOOL DynamicVf::OnBeginDrag(long nRowId)
  403. {
  404.     DROPEFFECT dEffect, retDEffect;
  405.     CString s1;
  406.  
  407.     // Only begin drag if we are a drop source
  408.     if(!m_bDropSource) return FALSE;
  409.  
  410.     s1.Format(_T("** VFB Example TEST DROP MSG **\r\n-- Dropped Row %ld --"), nRowId);
  411.     dEffect = (::GetKeyState(VK_CONTROL) < 0) ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
  412.  
  413.     retDEffect = VfuDoDragDrop(s1, dEffect);
  414.  
  415.     if(retDEffect == DROPEFFECT_NONE) return FALSE;
  416.  
  417.     return TRUE;
  418. }
  419.  
  420. // -------------------------------------------------------------------------
  421. // Called when something is dropped on us
  422. // -------------------------------------------------------------------------
  423. BOOL DynamicVf::OnDrop(long nRowId, COleDataObject* pDataObject, 
  424.                        DROPEFFECT dropEffect)
  425. {
  426.     CString sText, s1; 
  427.  
  428.     if(!VfuGetDropText(pDataObject, sText))
  429.         s1.Format(_T("You dropped on row %ld!"), nRowId);
  430.     else
  431.         s1.Format(_T("You dropped the following on row %ld:\n\n%s"), 
  432.                 nRowId, (LPCTSTR) sText);
  433.  
  434.     AfxMessageBox(s1, MB_OK | MB_ICONINFORMATION);
  435.  
  436.     return FALSE;        // We do not accept the drop
  437. }
  438.  
  439.  
  440.