home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK8 / MFC / SAMPLES / CHART / CHARTDLG.CP$ / chartdlg
Encoding:
Text File  |  1992-03-18  |  7.8 KB  |  369 lines

  1. // chartdlg.cpp : Defines the behaviors for the Entry and print abort
  2. //                dialogs.  The Entry dialog layout is defined in 
  3. //                entry.dlg; print abort is defined in chart.rc 
  4. //
  5. // This is a part of the Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and Microsoft
  11. // QuickHelp documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14. //
  15.  
  16. #include "chart.h"
  17.  
  18. #include <ctype.h>
  19.  
  20.  
  21. static char szFormat[] = "%5d %s";
  22. static int nFormatSplit = 5;
  23. static BOOL bChange;
  24.  
  25. #define MAXSTRINGLEN    200
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CEntryDialog
  29.  
  30. BEGIN_MESSAGE_MAP(CEntryDialog, CModalDialog)
  31.     ON_BN_CLICKED(BTN_ADD, OnBtnAdd)
  32.     ON_BN_CLICKED(BTN_DEL, OnBtnDel)
  33.     ON_LBN_SELCHANGE(LIST_BOX, OnListSelChange)
  34. END_MESSAGE_MAP()
  35.  
  36. // OnInitDialog:
  37. // Initialize the data entry dialog.  If there's already chart data,
  38. // stuff it into the dialog.
  39. // Note: This member function should not be put into the dialog
  40. // class message map -- it is called automatically by the CModalDialog
  41. // class code.  This is a special case.
  42. //
  43. BOOL CEntryDialog::OnInitDialog()
  44. {
  45.     // Call base class' for proper initialization.
  46.     //
  47.     if (!CModalDialog::OnInitDialog())
  48.     {
  49.         return FALSE;
  50.     }
  51.       
  52.     bChange = FALSE;
  53.  
  54.     // Initial conditions -- add items but don't delete them
  55.     //
  56.     GetDlgItem(BTN_ADD)->SetWindowText("Add");
  57.     GetDlgItem(BTN_DEL)->EnableWindow(FALSE);
  58.  
  59.     // If there's pre-existing data, stuff it into the dialog
  60.     //
  61.     if (m_pData != NULL)
  62.     {
  63.         ASSERT(m_pData->m_pChartData != NULL);
  64.         CObList* pData = m_pData->m_pChartData;
  65.  
  66.         // Insert the title here.
  67.         //
  68.         CEdit* pTitle  = (CEdit*) GetDlgItem(EDIT_TITLE);
  69.         pTitle->SetSel(0, -1);
  70.         pTitle->ReplaceSel(m_pData->m_Title);
  71.  
  72.         int nItems = pData->GetCount();
  73.         if (nItems > 0)
  74.         {        
  75.             // Now fill in the list box control.
  76.             //
  77.             CListBox* pListBox = (CListBox*) GetDlgItem(LIST_BOX);
  78.             pListBox->ResetContent();
  79.  
  80.             POSITION pos = pData->GetHeadPosition();
  81.  
  82.             for (int i = 0; i < nItems; i++)
  83.             {
  84.                 CChartData* ptr;
  85.                 char szValue[MAXSTRINGLEN];
  86.  
  87.                 ptr = (CChartData*)pData->GetNext(pos);
  88.                 sprintf(szValue, szFormat, ptr->height, ptr->szName);
  89.  
  90.                 pListBox->AddString(szValue);
  91.             }
  92.         }
  93.     }
  94.  
  95.     return TRUE;
  96. }
  97.  
  98. // DoModal:
  99. // While we're running this dialog, the member variable m_pData should
  100. // point to the user's data.
  101. //
  102. void CEntryDialog::DoModal(CChartObject* pData)
  103. {
  104.     m_pData = pData;
  105.     CModalDialog::DoModal();  
  106.     m_pData = NULL;
  107. }
  108.  
  109. // OnListSelChange:
  110. // The selection has changed; update edit fields with the new selection's
  111. // height and label.
  112. // 
  113. void CEntryDialog::OnListSelChange()
  114. {
  115.     int nLength;
  116.     char szLabel[MAXSTRINGLEN];
  117.  
  118.     CListBox* pListBox = (CListBox*) GetDlgItem(LIST_BOX);
  119.  
  120.     m_nIndex = pListBox->GetCurSel();
  121.  
  122.     if (m_nIndex == LB_ERR)
  123.     {
  124.         m_nIndex = -1;
  125.         return;
  126.     }
  127.  
  128.     nLength = pListBox->GetText(m_nIndex, szLabel);
  129.     szLabel[nLength] = '\0';
  130.  
  131.     szLabel[nFormatSplit] = '\0';
  132.     GetDlgItem(EDIT_VALUE)->SetWindowText(szLabel);
  133.     GetDlgItem(EDIT_LABEL)->SetWindowText(&szLabel[nFormatSplit+1]);
  134.  
  135.     bChange = TRUE;
  136.     GetDlgItem(BTN_ADD)->SetWindowText("Change");
  137.     GetDlgItem(BTN_DEL)->EnableWindow();
  138. }
  139.  
  140. // OnBtnAdd:
  141. // Transfer a new (height, label) from the edit fields to the listbox.
  142. // Validate height before transfer
  143. //
  144. void CEntryDialog::OnBtnAdd()
  145. {
  146.     int nLabLength, nLength;
  147.     char szValue[MAXSTRINGLEN];
  148.     char szLabel[MAXSTRINGLEN];
  149.     char szTitle[MAXSTRINGLEN];
  150.  
  151.     // valid #'s are ints >= 0
  152.  
  153.     BOOL bValid = FALSE;
  154.  
  155.     int iValue = GetDlgItemInt(EDIT_VALUE, &bValid, TRUE);
  156.  
  157.     if (bValid && (iValue >= 0))
  158.     {
  159.         SetDlgItemInt(EDIT_VALUE, iValue, FALSE);
  160.     }
  161.     else
  162.     {
  163.         MessageBox("An invalid value has been entered.", "Chart",
  164.             MB_ICONEXCLAMATION | MB_OK);
  165.         return;
  166.     }
  167.  
  168.     CWnd* pItem;
  169.     pItem = GetDlgItem(EDIT_LABEL);
  170.  
  171.     nLabLength = pItem->GetWindowTextLength();
  172.  
  173.     if (nLabLength >= MAXSTRINGLEN)
  174.     {
  175.         nLabLength = MAXSTRINGLEN-1;
  176.     }
  177.  
  178.     pItem->GetWindowText(szLabel, nLabLength+1);
  179.  
  180.     szLabel[nLabLength] = '\0';
  181.  
  182.     pItem = GetDlgItem(EDIT_TITLE);
  183.     nLength = pItem->GetWindowTextLength();
  184.  
  185.     if (nLength >= MAXSTRINGLEN)
  186.     {
  187.         nLength = MAXSTRINGLEN-1;
  188.     }
  189.  
  190.     pItem->GetWindowText(szTitle, nLength+1);
  191.  
  192.     szTitle[nLength] = '\0';
  193.  
  194.     m_pData->m_Title = szTitle;
  195.     sprintf(szValue, szFormat, iValue, szLabel);
  196.  
  197.     CListBox* pListBox = (CListBox*) GetDlgItem(LIST_BOX);
  198.  
  199.     if (!bChange)
  200.     {
  201.         pListBox->AddString(szValue);
  202.     }
  203.     else
  204.     {
  205.         pListBox->DeleteString(m_nIndex);
  206.         pListBox->InsertString(m_nIndex, szValue);
  207.  
  208.         bChange = FALSE;
  209.         GetDlgItem(BTN_ADD)->SetWindowText("Add");
  210.         GetDlgItem(BTN_DEL)->EnableWindow(FALSE);
  211.         
  212.         // Now no item is selected
  213.         m_nIndex = -1;
  214.         pListBox->SetCurSel(m_nIndex);
  215.     }
  216.  
  217.     m_pData->m_bDirty = TRUE;
  218.  
  219.     ClearEditBoxes();
  220. }
  221.  
  222. // OnBtnDel:
  223. // Delete the currently selected entry
  224. //
  225. void CEntryDialog::OnBtnDel()
  226. {
  227.     if (bChange)
  228.     {
  229.         CListBox* pListBox = (CListBox*) GetDlgItem(LIST_BOX);
  230.         pListBox->DeleteString(m_nIndex);
  231.  
  232.         // Now no item is selected
  233.         m_nIndex = -1;
  234.         pListBox->SetCurSel(m_nIndex);
  235.  
  236.         bChange = FALSE;
  237.         GetDlgItem(BTN_ADD)->SetWindowText("Add");
  238.         GetDlgItem(BTN_DEL)->EnableWindow(FALSE);
  239.     }
  240.  
  241.     m_pData->m_bDirty = TRUE;
  242.  
  243.     ClearEditBoxes();
  244. }
  245.  
  246. // OnOK:
  247. // Transfer array data from the dialog to the app chart object
  248. //
  249. void CEntryDialog::OnOK()
  250. {
  251.     if (!SetupArrayStructure())
  252.          return;
  253.  
  254.     CModalDialog::OnOK();
  255. }
  256.  
  257. // SetupArrayStructure:
  258. //
  259. BOOL CEntryDialog::SetupArrayStructure()
  260. {
  261.     short nCount, nLength, i;
  262.     char szBuffer[MAXSTRINGLEN];
  263.     CWnd* pItem;
  264.  
  265.     m_pData->RemoveAll();
  266.  
  267.     pItem = GetDlgItem(EDIT_TITLE);
  268.     nLength = pItem->GetWindowTextLength();
  269.  
  270.     if (nLength >= MAXSTRINGLEN)
  271.     {
  272.         nLength = MAXSTRINGLEN-1;
  273.     }
  274.  
  275.     pItem->GetWindowText(szBuffer, nLength+1);
  276.     szBuffer[nLength] = '\0';
  277.  
  278.     if (m_pData->m_Title != szBuffer)
  279.     {
  280.         m_pData->m_Title = szBuffer;
  281.         m_pData->m_bDirty = TRUE;
  282.     }
  283.  
  284.  
  285.     CListBox* pListBox = (CListBox*) GetDlgItem(LIST_BOX);
  286.     nCount = pListBox->GetCount();
  287.  
  288.     if (nCount == 0)
  289.     {
  290.         MessageBox("Warning: no values in chart.","Chart");
  291.     }
  292.     else
  293.     {
  294.         for (i = 0; i < nCount; i++)
  295.         {
  296.             CChartData* ptr;
  297.  
  298.             ptr = new CChartData;
  299.             pListBox->GetText(i, szBuffer);
  300.  
  301.             szBuffer[nFormatSplit] = '\0';
  302.  
  303.             ptr->height = atoi(szBuffer);
  304.             strcpy(ptr->szName, &szBuffer[nFormatSplit+1]);
  305.  
  306.             m_pData->m_pChartData->AddTail(ptr);
  307.         }
  308.     }
  309.  
  310.     return TRUE;
  311. }
  312.  
  313. // ClearEditBoxes:
  314. //
  315. void CEntryDialog::ClearEditBoxes()
  316. {
  317.     CEdit* pEditValue = (CEdit*) GetDlgItem(EDIT_VALUE);
  318.  
  319.     // Select entire field contents
  320.     pEditValue->SetSel(0, -1);
  321.     pEditValue->Clear();
  322.  
  323.     CEdit* pEditLabel = (CEdit*) GetDlgItem(EDIT_LABEL);
  324.     pEditLabel->SetSel(0, -1);
  325.     pEditLabel->Clear();
  326.  
  327.     pEditValue->SetFocus();
  328. }
  329.  
  330. ////////////////////////////////////////////////////////////////
  331. // CPrintDlgBox
  332. // Modeless print abort dialog box
  333. //
  334.  
  335. BEGIN_MESSAGE_MAP(CPrintDlgBox, CDialog)
  336.     ON_COMMAND(IDCANCEL, OnCancel)
  337. END_MESSAGE_MAP()
  338.  
  339. CPrintDlgBox::CPrintDlgBox()
  340. {
  341.     // Dialog defined in chart.rc
  342.     //
  343.     Create("PrintDlgBox");
  344. }
  345.  
  346. // OnInitDialog:
  347. // Disable this dialog box's system menu 'Close' item so user can't 
  348. // dismiss this dialog
  349. //
  350. BOOL CPrintDlgBox::OnInitDialog()
  351. {
  352.     GetSystemMenu(FALSE)->EnableMenuItem(SC_CLOSE, MF_GRAYED);
  353.     return TRUE;
  354. }
  355.  
  356. // OnCancel:
  357. // User hit the cancel button; re-enable the main frame window
  358. // (disabled elsewhere)
  359. //
  360. void CPrintDlgBox::OnCancel()
  361. {
  362.     extern BOOL bUserAbort;
  363.  
  364.     bUserAbort = TRUE;
  365.     GetParent()->EnableWindow(TRUE);
  366.     EndDialog(0);
  367. }
  368.  
  369.