home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 June / PCWorld_2003-06_cd.bin / KOMUNIK / MIRRORIT / SRC / MIRRORITVIEW.CPP < prev    next >
C/C++ Source or Header  |  1998-10-25  |  14KB  |  544 lines

  1. // MirrorItView.cpp : implementation of the CMirrorItView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MirrorIt.h"
  6.  
  7. #include "MirrorItDoc.h"
  8. #include "MirrorItView.h"
  9. #include "SessionPropertiesFileNamePage.h"
  10. #include "SessionPropertiesMirrorPage.h"
  11. #include "SessionPropertiesMainPage.h"
  12. #include "SessionPropertiesFileTypesPage.h"
  13. #include "SessionPropertiesLimitPage.h"
  14. #include "SessionPropertiesProxyPage.h"
  15. #include "SessionPropertiesPasswordPage.h"
  16. #include "SessionPropertiesAdvancedPage.h"
  17. #include "SessionPropertiesSheet.h"
  18. #include "Session.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMirrorItView
  28.  
  29. IMPLEMENT_DYNCREATE(CMirrorItView, CListView)
  30.  
  31. BEGIN_MESSAGE_MAP(CMirrorItView, CListView)
  32.     ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopyCut)
  33.     ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCopyCut)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  35.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  36.     ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  37.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  38.     //{{AFX_MSG_MAP(CMirrorItView)
  39.     ON_COMMAND(IDM_VIEW_DETAILS, OnViewDetails)
  40.     ON_COMMAND(IDM_VIEW_LARGEICONS, OnViewLargeicons)
  41.     ON_COMMAND(IDM_VIEW_LIST, OnViewList)
  42.     ON_COMMAND(IDM_VIEW_SMALLICONS, OnViewSmallicons)
  43.     ON_UPDATE_COMMAND_UI(IDM_VIEW_SMALLICONS, OnUpdateViewSmallicons)
  44.     ON_UPDATE_COMMAND_UI(IDM_VIEW_LIST, OnUpdateViewList)
  45.     ON_UPDATE_COMMAND_UI(IDM_VIEW_LARGEICONS, OnUpdateViewLargeicons)
  46.     ON_UPDATE_COMMAND_UI(IDM_VIEW_DETAILS, OnUpdateViewDetails)
  47.     ON_COMMAND(ID_SESSION_PROPERTIES, OnSessionProperties)
  48.     ON_UPDATE_COMMAND_UI(ID_SESSION_PROPERTIES, OnUpdateSessionProperties)
  49.     ON_COMMAND(ID_SESSION_REMOVE, OnSessionRemove)
  50.     ON_UPDATE_COMMAND_UI(ID_SESSION_REMOVE, OnUpdateSessionRemove)
  51.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditClear)
  52.     ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  53.     ON_COMMAND(ID_SESSION_ADD, OnSessionAdd)
  54.     ON_WM_LBUTTONDBLCLK()
  55.     ON_UPDATE_COMMAND_UI(ID_SESSION_MOVEDOWN, OnUpdateSessionMovedown)
  56.     ON_COMMAND(ID_SESSION_MOVEDOWN, OnSessionMovedown)
  57.     ON_COMMAND(ID_SESSION_MOVEUP, OnSessionMoveup)
  58.     ON_UPDATE_COMMAND_UI(ID_SESSION_MOVEUP, OnUpdateSessionMoveup)
  59.     ON_UPDATE_COMMAND_UI(ID_SESSION_START, OnUpdateSessionStart)
  60.     ON_COMMAND(ID_SESSION_START, OnSessionStart)
  61.     ON_WM_CREATE()
  62.     //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CMirrorItView construction/destruction
  67.  
  68. CMirrorItView::CMirrorItView()
  69. {
  70.     // TODO: add construction code here
  71.  
  72.     // CG: This line was added by the Clipboard Assistant component
  73.     m_nClipboardFormat = ::RegisterClipboardFormat(_T("MirrorIt Document"));
  74. }
  75.  
  76. CMirrorItView::~CMirrorItView()
  77. {
  78. }
  79.  
  80. BOOL CMirrorItView::PreCreateWindow(CREATESTRUCT& cs)
  81. {
  82.     cs.style = (cs.style & ~LVS_TYPEMASK) | LVS_REPORT;
  83.     cs.style |= LVS_AUTOARRANGE;
  84.     return CListView::PreCreateWindow(cs);
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CMirrorItView drawing
  89.  
  90. void CMirrorItView::OnDraw(CDC* pDC)
  91. {
  92.     CMirrorItDoc* pDoc = GetDocument();
  93.     ASSERT_VALID(pDoc);
  94.  
  95.     // TODO: add draw code for native data here
  96. }
  97.  
  98. void CMirrorItView::OnInitialUpdate()
  99. {
  100.     CListView::OnInitialUpdate();
  101.     GetListCtrl().DeleteAllItems();
  102.  
  103.     CMirrorItDoc *pDoc = GetDocument();
  104.  
  105.     for (int i = 0; i < pDoc -> sessions.GetSize(); i++)
  106.         AddSession((CSession *)pDoc -> sessions.GetAt(i));
  107.  
  108. }
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CMirrorItView diagnostics
  112.  
  113. #ifdef _DEBUG
  114. void CMirrorItView::AssertValid() const
  115. {
  116.     CListView::AssertValid();
  117. }
  118.  
  119. void CMirrorItView::Dump(CDumpContext& dc) const
  120. {
  121.     CListView::Dump(dc);
  122. }
  123.  
  124. CMirrorItDoc* CMirrorItView::GetDocument() // non-debug version is inline
  125. {
  126.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMirrorItDoc)));
  127.     return (CMirrorItDoc*)m_pDocument;
  128. }
  129. #endif //_DEBUG
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMirrorItView message handlers
  133.  
  134. BOOL CMirrorItView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  135. {
  136.     BOOL bCreated = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  137.     if ( bCreated )
  138.     {
  139.         CListCtrl& listView = GetListCtrl();
  140.         int aWidths[] = {100, 220, 148};
  141.         CString strHeading;
  142.  
  143.         for (int iCol = 0; iCol < C_COLUMNS; iCol++) 
  144.         {
  145.             strHeading.LoadString(IDS_COLUMN0 + iCol);
  146.             listView.InsertColumn(iCol, strHeading, LVCFMT_LEFT,
  147.                                   aWidths[iCol], iCol);
  148.         }
  149.  
  150.         // Create the full-sized and small icon image lists.
  151.         if ( m_ilLarge.Create( IDB_GLOBE, 32, 1, RGB(255, 0, 0) ) )
  152.         {
  153.             listView.SetImageList( &m_ilLarge, LVSIL_NORMAL );
  154.         }
  155.         if ( m_ilSmall.Create( IDB_GLOBESMALL, 16, 1, RGB(255,0,0) ) )
  156.         {
  157.             listView.SetImageList( &m_ilSmall, LVSIL_SMALL);
  158.         }
  159.  
  160.  
  161.     }
  162.     return bCreated;
  163. }
  164.  
  165. void CMirrorItView::SetListView(DWORD dwView)
  166. {
  167.     CListCtrl& list = GetListCtrl();
  168.     DWORD dwStyle = GetWindowLong(list.m_hWnd, GWL_STYLE);
  169.     if ((dwStyle & LVS_TYPEMASK) != dwView)
  170.         SetWindowLong(list.m_hWnd, GWL_STYLE,
  171.                       (dwStyle & ~LVS_TYPEMASK) | dwView);
  172. }
  173.  
  174. void CMirrorItView::UpdateListView(CCmdUI * pCmdUI, DWORD dwView)
  175. {
  176.     CListCtrl& list = GetListCtrl();
  177.     DWORD dwStyle = GetWindowLong(list.m_hWnd, GWL_STYLE) & LVS_TYPEMASK;
  178.     pCmdUI -> SetRadio(dwStyle == dwView);
  179. }
  180.  
  181. void CMirrorItView::OnViewDetails() 
  182. {
  183.     SetListView(LVS_REPORT);
  184. }
  185.  
  186. void CMirrorItView::OnViewLargeicons() 
  187. {
  188.     SetListView(LVS_ICON);
  189. }
  190.  
  191. void CMirrorItView::OnViewList() 
  192. {
  193.     SetListView(LVS_LIST);
  194. }
  195.  
  196. void CMirrorItView::OnViewSmallicons() 
  197. {
  198.     SetListView(LVS_SMALLICON);
  199. }
  200.  
  201. void CMirrorItView::OnUpdateViewSmallicons(CCmdUI* pCmdUI) 
  202. {
  203.     UpdateListView(pCmdUI, LVS_SMALLICON);
  204. }
  205.  
  206. void CMirrorItView::OnUpdateViewList(CCmdUI* pCmdUI) 
  207. {
  208.     UpdateListView(pCmdUI, LVS_LIST);
  209. }
  210.  
  211. void CMirrorItView::OnUpdateViewLargeicons(CCmdUI* pCmdUI) 
  212. {
  213.     UpdateListView(pCmdUI, LVS_ICON);
  214. }
  215.  
  216. void CMirrorItView::OnUpdateViewDetails(CCmdUI* pCmdUI) 
  217. {
  218.     UpdateListView(pCmdUI, LVS_REPORT);
  219. }
  220.  
  221. void CMirrorItView::OnSessionProperties() 
  222. {
  223.     CListCtrl& list = GetListCtrl();
  224.  
  225.     if (list.GetSelectedCount() != 1) return;
  226.     
  227.     CMirrorItDoc *pDoc = GetDocument();
  228.     pDoc -> PropertiesSession(list);
  229. }
  230.  
  231. void CMirrorItView::OnUpdateSessionProperties(CCmdUI* pCmdUI) 
  232. {
  233.     pCmdUI -> Enable(GetListCtrl().GetSelectedCount() == 1);
  234. }
  235.  
  236. void CMirrorItView::OnSessionRemove() 
  237. {
  238.     CListCtrl & list = GetListCtrl();
  239.  
  240.     int selcount = list.GetSelectedCount();
  241.     if (selcount < 1) return;
  242.  
  243.     CMirrorItDoc *pDoc = GetDocument();
  244.     pDoc -> RemoveSession(list);
  245. }
  246.  
  247. void CMirrorItView::OnUpdateSessionRemove(CCmdUI* pCmdUI) 
  248. {
  249.     pCmdUI -> Enable(GetListCtrl().GetSelectedCount());
  250. }
  251.  
  252. void CMirrorItView::OnUpdateEditClear(CCmdUI* pCmdUI) 
  253. {
  254.     OnUpdateSessionRemove(pCmdUI);
  255. }
  256.  
  257. void CMirrorItView::OnEditClear() 
  258. {
  259.     OnSessionRemove();
  260. }
  261.  
  262. void CMirrorItView::AddSession(CSession * session)
  263. {
  264.     CListCtrl& list = GetListCtrl();
  265.  
  266.     int n = list.GetItemCount();
  267.  
  268.     list.InsertItem(n, session -> m_SessionName);
  269.     list.SetItemText(n, 1, session -> m_BaseURL);
  270.     list.SetItemText(n, 2, session -> m_Comment);
  271.  
  272.     list.SetItemData(n, (DWORD) session);
  273. }
  274.  
  275. void CMirrorItView::UpdateSession(int n, CSession * session)
  276. {
  277.     CListCtrl& list = GetListCtrl();
  278.  
  279.     list.SetItemText(n, 0, (LPCSTR)session -> m_SessionName);
  280.     list.SetItemText(n, 1, (LPCSTR)session -> m_BaseURL);
  281.     list.SetItemText(n, 2, (LPCSTR)session -> m_Comment);
  282.  
  283.     list.SetItemData(n, (DWORD) session);
  284. }
  285.  
  286. void CMirrorItView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  287. {
  288.     LV_FINDINFO fi;
  289.     int res, i, count;
  290.     CSession *session;
  291.     CListCtrl & list = GetListCtrl();
  292.     CPtrArray *items;
  293.     CDWordArray *ditems;
  294.  
  295.     CMirrorItDoc *pDoc = GetDocument();
  296.  
  297.     switch (lHint)
  298.     {
  299.     case UPDATE_ADD:
  300.         if (!pHint) break;
  301.         AddSession((CSession *)pHint);
  302.         return;
  303.     case UPDATE_MODIFY:
  304.         if (!pHint) break;
  305.  
  306.         session = (CSession *)pHint;
  307.  
  308.         fi.flags = LVFI_PARAM;
  309.         fi.lParam = (DWORD) session;
  310.  
  311.         if ((res = list.FindItem(&fi)) != -1)
  312.             UpdateSession(res, session);
  313.         return;
  314.  
  315.     case UPDATE_REMOVE:
  316.         if (!pHint) break;
  317.  
  318.         items = (CPtrArray *)pHint;
  319.         count = items -> GetSize();
  320.         fi.flags = LVFI_PARAM;
  321.  
  322.         for (i = 0; i < count; i++)
  323.         {
  324.             session = (CSession *) items -> GetAt(i);
  325.             fi.lParam = (DWORD) session;
  326.             if ((res = list.FindItem(&fi)) != -1)
  327.                 list.DeleteItem(res);
  328.             delete session;
  329.         }
  330.         delete pHint;
  331.         return;
  332.  
  333.     case UPDATE_MOVE:
  334.         if (!pHint) break;
  335.  
  336.         ditems = (CDWordArray *)pHint;
  337.         int item1 = ditems -> GetAt(0);
  338.         int item2 = ditems -> GetAt(1);
  339.         CSession *item1session = (CSession *)list.GetItemData(item1);
  340.         CSession *item2session = (CSession *)list.GetItemData(item2);
  341.         UpdateSession(item1, item2session);
  342.         UpdateSession(item2, item1session);
  343.         list.SetItemState(item1, 0, LVIS_SELECTED | LVIS_FOCUSED);
  344.         list.SetItemState(item2, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  345.         delete ditems;
  346.  
  347.         return;
  348.     }
  349.  
  350.     list.DeleteAllItems();
  351.     for (i = 0; i < pDoc -> sessions.GetSize(); i++)
  352.         AddSession((CSession *)pDoc -> sessions.GetAt(i));
  353. }
  354.  
  355. void CMirrorItView::OnSessionAdd() 
  356. {
  357.     CMirrorItDoc *pDoc = GetDocument();
  358.     pDoc -> AddSession();
  359. }
  360.  
  361.  
  362. void CMirrorItView::OnLButtonDblClk(UINT nFlags, CPoint point) 
  363. {
  364.  
  365.     int i = GetListCtrl().HitTest(point);
  366.     if (i != -1)
  367.     {
  368.         CMirrorItDoc *pDoc = GetDocument();
  369.         pDoc -> PropertiesSession(i);
  370.     }
  371.     CListView::OnLButtonDblClk(nFlags, point);
  372. }
  373.  
  374.  
  375.  
  376. void CMirrorItView::OnUpdateSessionMovedown(CCmdUI* pCmdUI) 
  377. {
  378.     pCmdUI -> Enable(GetListCtrl().GetSelectedCount() == 1 &&
  379.                      GetListCtrl().GetItemCount() > 1);
  380. }
  381.  
  382. void CMirrorItView::OnSessionMovedown() 
  383. {
  384.     CListCtrl & list = GetListCtrl();
  385.  
  386.     int selcount = list.GetSelectedCount();
  387.     if (selcount < 1 || list.GetItemCount() < 1) return;
  388.  
  389.     CMirrorItDoc *pDoc = GetDocument();
  390.     pDoc -> MovedownSession(list);
  391. }
  392.  
  393. void CMirrorItView::OnSessionMoveup() 
  394. {
  395.     CListCtrl & list = GetListCtrl();
  396.  
  397.     int selcount = list.GetSelectedCount();
  398.     if (selcount < 1 || list.GetItemCount() < 1) return;
  399.  
  400.     CMirrorItDoc *pDoc = GetDocument();
  401.     pDoc -> MoveupSession(list);
  402. }
  403.  
  404. void CMirrorItView::OnUpdateSessionMoveup(CCmdUI* pCmdUI) 
  405. {
  406.     pCmdUI -> Enable(GetListCtrl().GetSelectedCount() == 1 &&
  407.                      GetListCtrl().GetItemCount() > 1);
  408. }
  409.  
  410. void CMirrorItView::OnUpdateSessionStart(CCmdUI* pCmdUI) 
  411. {
  412.     pCmdUI -> Enable(GetListCtrl().GetItemCount());
  413. }
  414.  
  415. void CMirrorItView::OnSessionStart() 
  416. {
  417.     CMirrorItDoc *pDoc = GetDocument();
  418.  
  419.     pDoc -> StartSession();
  420. }
  421.  
  422. void CMirrorItView::OnUpdateEditCopyCut(CCmdUI* pCmdUI)
  423. {
  424.     // CG: This function was added by the Clipboard Assistant component
  425.     pCmdUI->Enable(IsSelected());
  426. }
  427.  
  428. void CMirrorItView::OnUpdateEditPaste(CCmdUI* pCmdUI)
  429. {
  430.     // CG: This function was added by the Clipboard Assistant component
  431.     pCmdUI->Enable(::IsClipboardFormatAvailable(m_nClipboardFormat));
  432. }
  433.  
  434. void CMirrorItView::OnEditCopy()
  435. {
  436.     // CG: This block was added by the Clipboard Assistant component
  437.     {
  438.         CSharedFile    memFile;
  439.         CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete);
  440.  
  441.         // serialize data to archive object
  442.         DoCutCopyPaste(ar, FALSE);
  443.  
  444.         ar.Flush();
  445.  
  446.         HGLOBAL hData = memFile.Detach();
  447.         if (OpenClipboard())
  448.         {
  449.             ::SetClipboardData(m_nClipboardFormat, hData);
  450.             CloseClipboard();
  451.         }
  452.         else
  453.             AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
  454.     }
  455. }
  456.  
  457. void CMirrorItView::OnEditCut()
  458. {
  459.     // CG: This block was added by the Clipboard Assistant component
  460.     {
  461.         CSharedFile    memFile;
  462.         CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete);
  463.  
  464.         // serialize data to archive object
  465.         DoCutCopyPaste(ar, TRUE);
  466.  
  467.         ar.Flush();
  468.  
  469.         HGLOBAL hData = memFile.Detach();
  470.         if (OpenClipboard())
  471.         {
  472.             ::SetClipboardData(m_nClipboardFormat, hData);
  473.             CloseClipboard();
  474.         }
  475.         else
  476.             AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
  477.     }
  478. }
  479.  
  480. void CMirrorItView::OnEditPaste()
  481. {
  482.     // CG: This block was added by the Clipboard Assistant component
  483.     {
  484.         if (OpenClipboard())
  485.         {
  486.             HANDLE hData = ::GetClipboardData(m_nClipboardFormat);
  487.             if (hData != NULL)
  488.             {
  489.                 CSharedFile memFile;
  490.                 memFile.SetHandle(hData,FALSE);
  491.                 CArchive ar(&memFile, CArchive::load);
  492.  
  493.                 // serialize data to document
  494.                 DoCutCopyPaste(ar, FALSE);
  495.                 ar.Close();
  496.                 memFile.Detach();
  497.             }
  498.             else
  499.                 AfxMessageBox(CG_IDS_CANNOT_GET_CLIPBOARD_DATA);
  500.             CloseClipboard();
  501.         }
  502.         else
  503.             AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD);
  504.     }
  505.  
  506. }
  507.  
  508. BOOL CMirrorItView::IsSelected()
  509. {
  510.  
  511.     // TODO: Add/Replace code to selectively enable and disable the copy 
  512.     //       and cut menu items. Return TRUE when one or more items are
  513.     //       selected, and return FALSE when no items are selected.
  514.     return GetListCtrl().GetSelectedCount() > 0;
  515. }
  516.  
  517. void CMirrorItView::DoCutCopyPaste(CArchive &ar, BOOL bCut)
  518. {
  519.  
  520.     // TODO: Add/Replace code to copy the currently selected data to or
  521.     //       from the clipboard, via the CArchive object. Note: 
  522.     //       ar.IsStoring() set to TRUE indicates a copy or cut operation.
  523.  
  524.     ((CMirrorItDoc *) GetDocument()) -> m_ClipboardOperation = TRUE;
  525.     GetDocument()->Serialize(ar);
  526.  
  527.     if (bCut)
  528.     {
  529.         ASSERT(ar.IsStoring()); // no cutting on paste operation
  530.  
  531.         // TODO: Add/Replace code to delete the currently selected items.
  532.  
  533.         GetDocument()->DeleteContents();
  534.     }
  535.  
  536.     // updated view and flag as modified if cut or paste operation
  537.     if (!(ar.IsStoring() && !bCut))
  538.     {
  539.         GetDocument()->UpdateAllViews(NULL);
  540.         GetDocument()->SetModifiedFlag();
  541.     }
  542.     ((CMirrorItDoc *) GetDocument()) -> m_ClipboardOperation = FALSE;
  543. }
  544.