MyCommand.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:2k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyDefine.h"
  3. #include "MyCommand.h"
  4. //////////////////////////////////////////////////////////////////////////////////////////////
  5. CCommandStruct::CCommandStruct(int command, int num, int* object, int* index)
  6. {
  7. int i;
  8. m_command = command;
  9. m_num = num;
  10. m_object = new int[m_num];
  11. m_index = new int[m_num];
  12. ASSERT(num > 0);
  13. for(i=0; i<m_num; i++)
  14. {
  15. m_object[i] = object[i];
  16. m_index[i] = index[i];
  17. }
  18. }
  19. CCommandStruct::~CCommandStruct()
  20. {
  21. delete m_object;
  22. delete m_index;
  23. }
  24. //////////////////////////////////////////////////////////////////////////////////////////////
  25. IMPLEMENT_DYNCREATE(CCommandStack, CObject)
  26. CCommandStack::CCommandStack()
  27. {
  28. CommandPointer = -1;   //命令指针
  29. }
  30. CCommandStack::~CCommandStack()
  31. {
  32. int index = m_CommandArray.GetSize();
  33. while(index--)
  34. delete (CCommandStruct*)m_CommandArray.GetAt(index);
  35. m_CommandArray.RemoveAll();
  36. }
  37. void CCommandStack::PushCommand(int command, int num, int* object, int* index)
  38. {
  39.    ASSERT(num >= 0);
  40.    CCommandStruct* pCommand;
  41.    
  42.    DelCommandFrom(CommandPointer + 1);
  43.    pCommand = new CCommandStruct(command, num, object, index);
  44.    m_CommandArray.Add(pCommand);
  45.    CommandPointer++;
  46. }
  47. CCommandStruct* CCommandStack::Undo()
  48. {
  49. ASSERT(CommandPointer > -1 && CommandPointer < m_CommandArray.GetSize());
  50. if(CommandPointer == -1)
  51. return NULL;
  52. CCommandStruct* pCommand;
  53. pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
  54. CommandPointer--;
  55. return pCommand;
  56. }
  57. CCommandStruct* CCommandStack::Redo()
  58. {
  59. if(CommandPointer >= m_CommandArray.GetSize())
  60. return NULL;
  61. CCommandStruct* pCommand;
  62. CommandPointer++;
  63. pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
  64. return pCommand;
  65. }
  66. void CCommandStack::DelCommandFrom(int index)
  67. {
  68. ASSERT(index >= 0);
  69. while(index < m_CommandArray.GetSize())
  70. {
  71. delete (CCommandStruct*)m_CommandArray.GetAt(index);
  72. m_CommandArray.RemoveAt(index);
  73. }
  74. }
  75. BOOL CCommandStack::CanUndo()
  76. {
  77. if(m_CommandArray.GetSize() > 0 && CommandPointer > -1)
  78. return TRUE;
  79. else
  80. return FALSE;
  81. }
  82. BOOL CCommandStack::CanRedo()
  83. {
  84. if(m_CommandArray.GetSize() > 0 && CommandPointer < m_CommandArray.GetUpperBound())
  85. return TRUE;
  86. else
  87. return FALSE;
  88. }