undo.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_UNDO_H
  17. #define BDE_UNDO_H
  18. #include <vector>
  19. #include "types.h"
  20. #include "point.h"
  21. // Class UndoStack records the operations (ops) made to EditBox's buffer.
  22. //
  23. // Strictly speaking, the UndoStack is not really a stack: you move the
  24. // "top" pointer up and down within this stack when you undo and redo
  25. // operations.
  26. //
  27. // UndoStack stores the operations in a vector of UndoOp's. UndoOp contains
  28. // all the information needed to restore one operation.
  29. enum OpType { opInsert, opDelete, opReplace };
  30. struct UndoOp {
  31. OpType type;
  32. Point point; // the point in the buffer where the operation was made
  33. unistring inserted_text;
  34. unistring deleted_text;
  35. size_t calc_size() const {
  36. return
  37. sizeof(UndoOp)
  38. + inserted_text.len() * sizeof(unichar)
  39. + deleted_text.len() * sizeof(unichar);
  40. }
  41. bool merge(const UndoOp &op);
  42. };
  43. class UndoStack {
  44. private:
  45. std::vector<UndoOp> stack;
  46. unsigned top;
  47. size_t bytes_size; // size of current stack
  48. size_t bytes_size_limit; // max size
  49. bool merge_small_ops; // merge small ops?
  50. bool truncated; // was the stack already truncated
  51. // to fit bytes_size_limit?
  52. private:
  53. void erase_redo_ops();
  54. void truncate_undo();
  55. bool undo_size_too_big() const { return bytes_size >= bytes_size_limit; }
  56. void update_size_up(int chars_count);
  57. void update_size_up(const UndoOp &op);
  58. void update_size_down(const UndoOp &op);
  59. public:
  60. UndoStack();
  61. void clear();
  62. bool was_truncated() { return truncated; }
  63. bool disabled() const { return bytes_size_limit == 0; }
  64. bool is_undo_available() const { return (top != 0); }
  65. bool is_redo_available() const { return (top != stack.size()); }
  66. void record_op(const UndoOp &op);
  67. UndoOp *get_prev_op();
  68. UndoOp *get_next_op();
  69. void set_size_limit(size_t limit);
  70. void set_merge(bool value) { merge_small_ops = value; }
  71. bool is_merge() const { return merge_small_ops; }
  72. };
  73. #endif