libt3widget
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
undo.h
1 /* Copyright (C) 2011 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_WIDGET_UNDO_H
15 #define T3_WIDGET_UNDO_H
16 
17 #include <string>
18 #include <t3widget/textline.h>
19 #include <t3widget/util.h>
20 
21 namespace t3_widget {
22 
23 #define TEXT_START_SIZE 32
24 
25 enum undo_type_t {
26  UNDO_NONE,
27  UNDO_DELETE,
28  UNDO_DELETE_BLOCK,
29  UNDO_BACKSPACE,
30  UNDO_ADD,
31  UNDO_ADD_BLOCK,
32  UNDO_REPLACE_BLOCK,
33  UNDO_OVERWRITE,
34  UNDO_DELETE_NEWLINE,
35  UNDO_BACKSPACE_NEWLINE,
36  UNDO_ADD_NEWLINE,
37  UNDO_INDENT,
38  UNDO_UNINDENT,
39  UNDO_ADD_NEWLINE_INDENT,
40  UNDO_BLOCK_START,
41  UNDO_BLOCK_END,
42 
43  // Types only for mapping redo to undo types
44  UNDO_ADD_REDO,
45  UNDO_BACKSPACE_REDO,
46  UNDO_REPLACE_BLOCK_REDO,
47  UNDO_OVERWRITE_REDO,
48  UNDO_ADD_NEWLINE_INDENT_REDO,
49  UNDO_BLOCK_START_REDO,
50  UNDO_BLOCK_END_REDO,
51 };
52 
53 class T3_WIDGET_API undo_list_t {
54  private:
55  undo_t *head, *tail, *current, *mark;
56  bool mark_is_valid;
57  bool mark_beyond_current;
58 
59  public:
60  undo_list_t(void) : head(NULL), tail(NULL), current(NULL), mark(NULL), mark_is_valid(true), mark_beyond_current(false) {}
61  ~undo_list_t(void);
62  void add(undo_t *undo);
63  undo_t *back(void);
64  undo_t *forward(void);
65  void set_mark(void);
66  bool is_at_mark(void) const;
67 
68  #ifdef DEBUG
69  void dump(void);
70  #endif
71 };
72 
73 class T3_WIDGET_API undo_t {
74  private:
75  static undo_type_t redo_map[];
76 
77  undo_type_t type;
78  text_coordinate_t start;
79 
80  undo_t *previous, *next;
81  friend class undo_list_t;
82  public:
83 
84  undo_t(undo_type_t _type, text_coordinate_t _start) : type(_type), start(_start), previous(NULL), next(NULL) {}
85  virtual ~undo_t(void);
86  undo_type_t get_type(void) const;
87  undo_type_t get_redo_type(void) const;
88  virtual text_coordinate_t get_start(void);
89  virtual void add_newline(void) {}
90  virtual std::string *get_text(void);
91  virtual std::string *get_replacement(void);
92  virtual text_coordinate_t get_end(void) const;
93  virtual void minimize(void) {}
94  virtual text_coordinate_t get_new_end(void) const;
95 };
96 
97 class T3_WIDGET_API undo_single_text_t : public undo_t {
98  private:
99  std::string text;
100 
101  public:
102  undo_single_text_t(undo_type_t _type, text_coordinate_t _start) : undo_t(_type, _start) {};
103  virtual void add_newline(void);
104  virtual std::string *get_text(void);
105  virtual void minimize(void);
106 };
107 
109  private:
110  text_coordinate_t end;
111 
112  public:
113  undo_single_text_double_coord_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
114  undo_single_text_t(_type, _start), end(_end) {}
115  virtual text_coordinate_t get_end(void) const;
116 };
117 
119  private:
120  std::string replacement;
121 
122  public:
123  undo_double_text_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
124  undo_single_text_double_coord_t(_type, _start, _end) {}
125 
126  virtual std::string *get_replacement(void);
127  virtual void minimize(void);
128 };
129 
131  private:
132  text_coordinate_t new_end;
133 
134  public:
135  undo_double_text_triple_coord_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
136  undo_double_text_t(_type, _start, _end) {}
137 
138  void set_new_end(text_coordinate_t _new_end);
139  virtual text_coordinate_t get_new_end(void) const;
140 };
141 
142 }; // namespace
143 #endif