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. ASSERT(num > 0);
  8. int i;
  9. m_command = command;
  10. m_num = num;
  11. m_object = new int[m_num];
  12. m_index = new int[m_num];
  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. void CCommandStack::RemoveAll()
  48. {
  49. for(int index = 0; index < m_CommandArray.GetSize(); index++)
  50. delete (CCommandStack*)m_CommandArray.GetAt(index);
  51. m_CommandArray.RemoveAll();
  52. }
  53. CCommandStruct* CCommandStack::Undo()
  54. {
  55. ASSERT(CommandPointer > -1 && CommandPointer < m_CommandArray.GetSize());
  56. if(CommandPointer == -1)
  57. return NULL;
  58. CCommandStruct* pCommand;
  59. pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
  60. CommandPointer--;
  61. return pCommand;
  62. }
  63. CCommandStruct* CCommandStack::Redo()
  64. {
  65. if(CommandPointer >= m_CommandArray.GetSize())
  66. return NULL;
  67. CCommandStruct* pCommand;
  68. CommandPointer++;
  69. pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
  70. return pCommand;
  71. }
  72. void CCommandStack::DelCommandFrom(int index)
  73. {
  74. ASSERT(index >= 0);
  75. while(index < m_CommandArray.GetSize())
  76. {
  77. delete (CCommandStruct*)m_CommandArray.GetAt(index);
  78. m_CommandArray.RemoveAt(index);
  79. }
  80. }
  81. BOOL CCommandStack::CanUndo()
  82. {
  83. if(m_CommandArray.GetSize() > 0 && CommandPointer > -1)
  84. return TRUE;
  85. else
  86. return FALSE;
  87. }
  88. BOOL CCommandStack::CanRedo()
  89. {
  90. if(m_CommandArray.GetSize() > 0 && CommandPointer < m_CommandArray.GetUpperBound())
  91. return TRUE;
  92. else
  93. return FALSE;
  94. }