1
0

undo.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. #include "undo.h"
  26. #include "render.h"
  27. int C_UndoStack::list_pos;
  28. C_UndoItem *C_UndoStack::list[256];
  29. C_UndoItem::C_UndoItem() : data(NULL), length(0), isdirty(true)
  30. {
  31. }
  32. C_UndoItem::C_UndoItem(const C_UndoItem& T) : data(NULL), length(0), isdirty(true)
  33. {
  34. *this = T;
  35. }
  36. C_UndoItem::C_UndoItem(void *_data, int _length, bool _isdirty) : data(NULL), length(length), isdirty(_isdirty)
  37. {
  38. data = GlobalAlloc(GPTR, length);
  39. memcpy(data, _data, length);
  40. }
  41. C_UndoItem::~C_UndoItem()
  42. {
  43. if (data)
  44. {
  45. GlobalFree(data);
  46. }
  47. }
  48. C_UndoItem & C_UndoItem::operator = (const C_UndoItem& T)
  49. {
  50. length = T.length;
  51. isdirty = T.isdirty;
  52. if (data)
  53. {
  54. GlobalFree(data);
  55. }
  56. data = GlobalAlloc(GPTR, length);
  57. memcpy(data, T.data, length);
  58. return *this;
  59. }
  60. bool C_UndoItem::operator == (const C_UndoItem& T) const
  61. {
  62. bool retval = false;
  63. if (length == T.length)
  64. {
  65. retval = (memcmp(data, T.data, length) == 0);
  66. }
  67. return retval;
  68. }
  69. void C_UndoItem::set(void *_data, int _length, bool _isdirty)
  70. {
  71. length = _length;
  72. isdirty = _isdirty;
  73. if (data)
  74. {
  75. GlobalFree(data);
  76. }
  77. data = GlobalAlloc(GPTR, length);
  78. memcpy(data, _data, length);
  79. }
  80. void C_UndoStack::saveundo(int is2)
  81. {
  82. // Save to the undo buffer (sets the dirty bit for this item)
  83. C_UndoItem *item = new C_UndoItem;
  84. C_UndoItem *old = list[list_pos];
  85. if (is2)
  86. {
  87. g_render_effects2->__SavePresetToUndo(*item);
  88. }
  89. else
  90. {
  91. g_render_effects->__SavePresetToUndo(*item);
  92. }
  93. // Only add it to the stack if it has changed.
  94. if (!old || !(*old == *item))
  95. {
  96. if (list_pos == sizeof(list)/sizeof(list[0])-1)
  97. {
  98. delete list[0];
  99. memcpy(list,list+1,sizeof(list)/sizeof(list[0])-1);
  100. list_pos--;
  101. }
  102. list[++list_pos]=item;
  103. }
  104. else delete item;
  105. }
  106. void C_UndoStack::cleardirty()
  107. {
  108. // If we're clearing the dirty bit, we only clear it on the current item.
  109. if (list_pos >= 0 && list_pos < sizeof(list)/sizeof(list[0]) && list[list_pos])
  110. {
  111. list[list_pos]->isdirty = 0;
  112. }
  113. }
  114. bool C_UndoStack::isdirty()
  115. {
  116. if (list_pos >= 0 && list_pos < sizeof(list)/sizeof(list[0]) && list[list_pos])
  117. return list[list_pos]->isdirty;
  118. return false;
  119. }
  120. int C_UndoStack::can_undo()
  121. {
  122. return (list_pos>0 && list[list_pos-1]);
  123. }
  124. int C_UndoStack::can_redo()
  125. {
  126. return list_pos < sizeof(list)/sizeof(list[0])-1 && list[list_pos+1];
  127. }
  128. void C_UndoStack::undo()
  129. {
  130. if (list_pos>0 && list[list_pos-1])
  131. {
  132. g_render_transition->LoadPreset(NULL,0,list[--list_pos]);
  133. }
  134. }
  135. void C_UndoStack::redo()
  136. {
  137. if (list_pos < sizeof(list)/sizeof(list[0])-1 && list[list_pos+1])
  138. {
  139. g_render_transition->LoadPreset(NULL,0,list[++list_pos]);
  140. }
  141. }
  142. void C_UndoStack::clear()
  143. {
  144. list_pos=0;
  145. int x;
  146. for (x = 0; x < sizeof(list)/sizeof(list[0]); x ++)
  147. {
  148. delete list[x];
  149. list[x]=0;
  150. }
  151. }