home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / autobld / workspcs.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  7KB  |  212 lines

  1. // Workspcs.cpp : implementation file
  2. //
  3.  
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6.  
  7. #include "stdafx.h"
  8. #include "AutoBld.h"
  9. #include "Workspcs.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CWorkspaces dialog
  19.  
  20.  
  21. CWorkspaces::CWorkspaces(CWnd* pParent /*=NULL*/)
  22.     : CDialog(CWorkspaces::IDD, pParent)
  23. {
  24.     //{{AFX_DATA_INIT(CWorkspaces)
  25.         // NOTE: the ClassWizard will add member initialization here
  26.     //}}AFX_DATA_INIT
  27. }
  28.  
  29.  
  30. void CWorkspaces::DoDataExchange(CDataExchange* pDX)
  31. {
  32.     CDialog::DoDataExchange(pDX);
  33.     //{{AFX_DATA_MAP(CWorkspaces)
  34.     DDX_Control(pDX, IDC_WORKSPACELIST, m_WorkspaceList);
  35.     DDX_Control(pDX, IDC_REMOVE, m_Remove);
  36.     DDX_Control(pDX, IDC_ADDWORKSPACE, m_Add);
  37.     //}}AFX_DATA_MAP
  38. }
  39.  
  40.  
  41. BEGIN_MESSAGE_MAP(CWorkspaces, CDialog)
  42.     //{{AFX_MSG_MAP(CWorkspaces)
  43.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  44.     ON_BN_CLICKED(IDC_ADDWORKSPACE, OnAddWorkspace)
  45.     ON_BN_CLICKED(IDC_MOVEDOWN, OnMoveDown)
  46.     ON_BN_CLICKED(IDC_MOVEUP, OnMoveUp)
  47.     //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CWorkspaces message handlers
  52.  
  53. void CWorkspaces::OnRemove()
  54. {
  55.     //The user wants to remove a workspace from the list of workspaces to
  56.     //build. Start at the top of the list and walk through it until an item
  57.     //that is selected is found, Do not worry about fiddling with the registry
  58.     //now, because it is updated when OK is pressed
  59.     int nItem = m_WorkspaceList.GetNextItem(-1, LVNI_ALL);
  60.     while (nItem != -1)
  61.     {
  62.         UINT nState = m_WorkspaceList.GetItemState(nItem, LVIS_SELECTED);
  63.         if ((nState & LVIS_SELECTED) == LVIS_SELECTED)
  64.         {
  65.             m_WorkspaceList.DeleteItem(nItem);
  66.             nItem = m_WorkspaceList.GetNextItem(-1, LVNI_ALL);
  67.             break;
  68.         }
  69.         else
  70.             nItem = m_WorkspaceList.GetNextItem(nItem, LVNI_ALL);
  71.     }
  72. }
  73.  
  74. void CWorkspaces::OnAddWorkspace()
  75. {
  76.     //The user wants to add a workspace to the list of workspaces to build
  77.     CString strFileSpec;
  78.     strFileSpec.LoadString(IDS_PROJECTFILES);
  79.     CFileDialog Browser(TRUE, "", "", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
  80.             strFileSpec);
  81.     if (Browser.DoModal() == IDOK)
  82.     {
  83.         //put the item into the list
  84.         m_WorkspaceList.InsertItem(0, "", 0);
  85.         m_WorkspaceList.SetItem(0, 1, LVIF_TEXT, Browser.GetPathName(),
  86.                 0, 0, 0, 0);
  87.         m_WorkspaceList.SetItem(0, 0, LVIF_TEXT, Browser.GetFileTitle(),
  88.                 0, 0, 0, 0);
  89.     }
  90. }
  91.  
  92. void CWorkspaces::OnMoveDown()
  93. {
  94.     int nItem = m_WorkspaceList.GetNextItem(-1, LVNI_ALL);
  95.     while (nItem != -1)
  96.     {
  97.         UINT nState = m_WorkspaceList.GetItemState(nItem, LVIS_SELECTED);
  98.         if ((nState & LVIS_SELECTED) == LVIS_SELECTED)
  99.         {
  100.             int nBelowItem = m_WorkspaceList.GetNextItem(nItem, LVNI_BELOW);
  101.             if (nBelowItem != -1)
  102.             {
  103.                 CString strThisName = m_WorkspaceList.GetItemText(nItem, 0);
  104.                 CString strThisPath = m_WorkspaceList.GetItemText(nItem, 1);
  105.                 CString strBelowName = m_WorkspaceList.GetItemText(nBelowItem, 0);
  106.                 CString strBelowPath = m_WorkspaceList.GetItemText(nBelowItem, 1);
  107.                 m_WorkspaceList.SetItemText(nItem, 0, strBelowName);
  108.                 m_WorkspaceList.SetItemText(nItem, 1, strBelowPath);
  109.                 m_WorkspaceList.SetItemText(nBelowItem, 0, strThisName);
  110.                 m_WorkspaceList.SetItemText(nBelowItem, 1, strThisPath);
  111.                 m_WorkspaceList.SetItemState(nBelowItem, LVIS_SELECTED,
  112.                         LVIS_SELECTED);
  113.                 m_WorkspaceList.SetFocus();
  114.             }
  115.             break;
  116.         }
  117.         else
  118.             nItem = m_WorkspaceList.GetNextItem(nItem, LVNI_ALL);
  119.     }
  120. }
  121.  
  122. void CWorkspaces::OnMoveUp()
  123. {
  124.     int nItem = m_WorkspaceList.GetNextItem(-1, LVNI_ALL);
  125.     while (nItem != -1)
  126.     {
  127.         UINT nState = m_WorkspaceList.GetItemState(nItem, LVIS_SELECTED);
  128.         if ((nState & LVIS_SELECTED) == LVIS_SELECTED)
  129.         {
  130.             int nAboveItem = m_WorkspaceList.GetNextItem(nItem, LVNI_ABOVE);
  131.             if (nAboveItem != -1)
  132.             {
  133.                 CString strThisName = m_WorkspaceList.GetItemText(nItem, 0);
  134.                 CString strThisPath = m_WorkspaceList.GetItemText(nItem, 1);
  135.                 CString strAboveName = m_WorkspaceList.GetItemText(nAboveItem, 0);
  136.                 CString strAbovePath = m_WorkspaceList.GetItemText(nAboveItem, 1);
  137.                 m_WorkspaceList.SetItemText(nItem, 0, strAboveName);
  138.                 m_WorkspaceList.SetItemText(nItem, 1, strAbovePath);
  139.                 m_WorkspaceList.SetItemText(nAboveItem, 0, strThisName);
  140.                 m_WorkspaceList.SetItemText(nAboveItem, 1, strThisPath);
  141.                 m_WorkspaceList.SetItemState(nAboveItem, LVIS_SELECTED,
  142.                         LVIS_SELECTED);
  143.                 m_WorkspaceList.SetFocus();
  144.             }
  145.             break;
  146.         }
  147.         else
  148.             nItem = m_WorkspaceList.GetNextItem(nItem, LVNI_ALL);
  149.     }
  150. }
  151.  
  152. void CWorkspaces::OnOK()
  153. {
  154.     AfxGetApp()->WriteProfileInt(_T("Configure"), _T("NumWorkspaces"),
  155.             m_WorkspaceList.GetItemCount());
  156.     for (int i = 0 ; i < m_WorkspaceList.GetItemCount() ; i++)
  157.     {
  158.         char szRegKey[50];
  159.         wsprintf(szRegKey, "Workspace%d", i);
  160.         AfxGetApp()->WriteProfileString(_T("Configure"), szRegKey,
  161.                 m_WorkspaceList.GetItemText(i, 1));
  162.         wsprintf(szRegKey, "WorkspaceName%d", i);
  163.         AfxGetApp()->WriteProfileString(_T("Configure"), szRegKey,
  164.                 m_WorkspaceList.GetItemText(i, 0));
  165.     }
  166.  
  167.     CDialog::OnOK();
  168. }
  169.  
  170. BOOL CWorkspaces::OnInitDialog()
  171. {
  172.     CDialog::OnInitDialog();
  173.  
  174.     //Need to set up to display the little icons next to each workspace name.
  175.     HICON hIcon = AfxGetApp()->LoadIcon(IDI_WORKSPACE);
  176.     m_ImageList.Create(16, 16, ILC_COLOR, 1, 1);
  177.     m_ImageList.SetBkColor(GetSysColor(COLOR_WINDOW));
  178.     m_ImageList.Add(hIcon);
  179.     m_WorkspaceList.SetImageList(&m_ImageList, LVSIL_SMALL);
  180.  
  181.     //Set up columns inside the list view
  182.     CString strWrkspcName;
  183.     CString strWrkspcPath;
  184.     strWrkspcName.LoadString(IDS_WKSPCNAME);
  185.     strWrkspcPath.LoadString(IDS_WKSPCPATH);
  186.     m_WorkspaceList.InsertColumn(0, strWrkspcName, LVCFMT_LEFT, 100);
  187.     m_WorkspaceList.InsertColumn(1, strWrkspcPath, LVCFMT_LEFT, 230);
  188.  
  189.     //Start getting information from the registry. First get the total number
  190.     //  of workspaces that we want to work with.
  191.     int nNumWorkspaces = AfxGetApp()->GetProfileInt(_T("Configure"),
  192.             _T("NumWorkspaces"), 0);
  193.     for (int i = 0 ; i < nNumWorkspaces ; i++)
  194.     {
  195.         //Now load each workspace name and path
  196.         char szRegKey[50];
  197.         wsprintf(szRegKey, "Workspace%d", i);
  198.         CString strWorkspacePath = AfxGetApp()->GetProfileString(_T("Configure"),
  199.                 szRegKey, "");
  200.         wsprintf(szRegKey, "WorkspaceName%d", i);
  201.         CString strWorkspaceName = AfxGetApp()->GetProfileString(_T("Configure"),
  202.                 szRegKey, "");
  203.         m_WorkspaceList.InsertItem(i, "", 0);
  204.  
  205.         //Store this information in the list view
  206.         m_WorkspaceList.SetItem(i, 1, LVIF_TEXT, strWorkspacePath, 0, 0, 0, 0);
  207.         m_WorkspaceList.SetItem(i, 0, LVIF_TEXT, strWorkspaceName, 0, 0, 0, 0);
  208.     }
  209.     return TRUE;  // return TRUE unless you set the focus to a control
  210.                   // EXCEPTION: OCX Property Pages should return FALSE
  211. }
  212.