libt3widget
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 /* FIXME: the undo/redo functionality should be reimplemented, composing operations from:
26  DELETE, ADD, ADD_NEWLINE, DELETE_NEWLINE, BLOCK_START, BLOCK_END
27  The last two should contain the cursor positions before and after the action, the others
28  should only encode the minimal location information to execute the operation at the
29  correct point. If the location information does not leave the cursor at the correct
30  point, it should be enclosed by the BLOCK_START/BLOCK_END combination.
31 */
32 
33 enum undo_type_t {
34  UNDO_NONE,
35  UNDO_DELETE,
36  UNDO_DELETE_BLOCK,
37  UNDO_BACKSPACE,
38  UNDO_ADD,
39  UNDO_ADD_BLOCK,
40  UNDO_REPLACE_BLOCK,
41  UNDO_OVERWRITE,
42  UNDO_DELETE_NEWLINE,
43  UNDO_BACKSPACE_NEWLINE,
44  UNDO_ADD_NEWLINE,
45  UNDO_INDENT,
46  UNDO_UNINDENT,
47  UNDO_ADD_NEWLINE_INDENT,
48  /* Markers for blocks of undo operations. All operations between a UNDO_BLOCK_START and UNDO_BLOCK_END
49  are to be applied as a single operation. */
50  UNDO_BLOCK_START,
51  UNDO_BLOCK_END,
52 
53  // Types only for mapping redo to undo types
54  UNDO_ADD_REDO,
55  UNDO_BACKSPACE_REDO,
56  UNDO_REPLACE_BLOCK_REDO,
57  UNDO_OVERWRITE_REDO,
58  UNDO_ADD_NEWLINE_INDENT_REDO,
59  UNDO_BLOCK_START_REDO,
60  UNDO_BLOCK_END_REDO,
61 };
62 
63 // FIXME: reimplement using std::list
64 
65 class T3_WIDGET_API undo_list_t {
66  private:
67  undo_t *head, *tail, *current, *mark;
68  bool mark_is_valid;
69  bool mark_beyond_current;
70 
71  public:
72  undo_list_t(void) : head(NULL), tail(NULL), current(NULL), mark(NULL), mark_is_valid(true), mark_beyond_current(false) {}
73  ~undo_list_t(void);
74  void add(undo_t *undo);
75  undo_t *back(void);
76  undo_t *forward(void);
77  void set_mark(void);
78  bool is_at_mark(void) const;
79 
80  #ifdef DEBUG
81  void dump(void);
82  #endif
83 };
84 
85 class T3_WIDGET_API undo_t {
86  private:
87  static undo_type_t redo_map[];
88 
89  undo_type_t type;
90  text_coordinate_t start;
91 
92  undo_t *previous, *next;
93  friend class undo_list_t;
94  public:
95 
96  undo_t(undo_type_t _type, text_coordinate_t _start) : type(_type), start(_start), previous(NULL), next(NULL) {}
97  virtual ~undo_t(void);
98  undo_type_t get_type(void) const;
99  undo_type_t get_redo_type(void) const;
100  virtual text_coordinate_t get_start(void);
101  virtual void add_newline(void) {}
102  virtual std::string *get_text(void);
103  virtual std::string *get_replacement(void);
104  virtual text_coordinate_t get_end(void) const;
105  virtual void minimize(void) {}
106  virtual text_coordinate_t get_new_end(void) const;
107 };
108 
109 class T3_WIDGET_API undo_single_text_t : public undo_t {
110  private:
111  std::string text;
112 
113  public:
114  undo_single_text_t(undo_type_t _type, text_coordinate_t _start) : undo_t(_type, _start) {};
115  virtual void add_newline(void);
116  virtual std::string *get_text(void);
117  virtual void minimize(void);
118 };
119 
121  private:
122  text_coordinate_t end;
123 
124  public:
125  undo_single_text_double_coord_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
126  undo_single_text_t(_type, _start), end(_end) {}
127  virtual text_coordinate_t get_end(void) const;
128 };
129 
131  private:
132  std::string replacement;
133 
134  public:
135  undo_double_text_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
136  undo_single_text_double_coord_t(_type, _start, _end) {}
137 
138  virtual std::string *get_replacement(void);
139  virtual void minimize(void);
140 };
141 
143  private:
144  text_coordinate_t new_end;
145 
146  public:
147  undo_double_text_triple_coord_t(undo_type_t _type, text_coordinate_t _start, text_coordinate_t _end) :
148  undo_double_text_t(_type, _start, _end) {}
149 
150  void set_new_end(text_coordinate_t _new_end);
151  virtual text_coordinate_t get_new_end(void) const;
152 };
153 
154 }; // namespace
155 #endif
Definition: undo.h:130
The t3_widget namespace is contains all classes, functions and global variables in the libt3widget li...
Definition: autocompleter.cc:18
Definition: undo.h:109
Definition: util.h:49
Definition: undo.h:65
Definition: undo.h:85