]> git.lyx.org Git - lyx.git/blob - src/changes.h
bug 2298: cursorTop/Bottom/Home/End does not redraw after dEPM
[lyx.git] / src / changes.h
1 // -*- C++ -*-
2 /**
3  * \file changes.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Record changes in a paragraph.
12  */
13
14 #ifndef CHANGES_H
15 #define CHANGES_H
16
17 #include "support/types.h"
18 #include "support/lyxtime.h"
19
20 #include <vector>
21 #include <iosfwd>
22
23
24 class Change {
25 public:
26         /// the type of change
27         enum Type {
28                 UNCHANGED, // no change
29                 INSERTED, // new text
30                 DELETED // deleted text
31         };
32
33         Change(Type t = UNCHANGED, int a = 0, lyx::time_type ct = 0)
34                 : type(t), author(a), changetime(ct) {}
35
36         Type type;
37
38         int author;
39
40         lyx::time_type changetime;
41 };
42
43 bool operator==(Change const & l, Change const & r);
44 bool operator!=(Change const & l, Change const & r);
45
46 class Changes {
47 public:
48
49         Changes(Change::Type type);
50
51         ~Changes();
52
53         Changes(Changes const &);
54
55         /// reset "default" change type (for empty pars)
56         void reset(Change::Type type) {
57                 empty_type_ = type;
58         }
59
60         /// set the position to the given change
61         void set(Change change, lyx::pos_type pos);
62
63         /// set the position to the given change
64         void set(Change::Type, lyx::pos_type pos);
65
66         /// set the range to the given change
67         void set(Change::Type, lyx::pos_type start, lyx::pos_type end);
68
69         /// set the range to the given change
70         void set(Change, lyx::pos_type start, lyx::pos_type end);
71
72         /// mark the given change and adjust
73         void record(Change, lyx::pos_type pos);
74
75         /// return the change type at the given position
76         Change::Type lookup(lyx::pos_type pos) const;
77
78         /// return the change at the given position
79         Change const lookupFull(lyx::pos_type pos) const;
80
81         /// return true if there is a change in the given range
82         bool isChange(lyx::pos_type start, lyx::pos_type end) const;
83
84         /// return true if there is a deleted or unchanged range contained
85         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
86
87         /// remove the given entry. This implies that a character was
88         /// deleted at pos, and will adjust all range bounds past it
89         void erase(lyx::pos_type pos);
90
91         /// output latex to mark a transition between two changetypes
92         /// returns length of text outputted
93         static int latexMarkChange(std::ostream & os, Change::Type old,
94                 Change::Type change, bool const & output);
95
96         /// output .lyx file format for transitions between changes
97         static void lyxMarkChange(std::ostream & os, int & column,
98                 lyx::time_type curtime, Change const & old, Change const & change);
99
100 private:
101         class Range {
102         public:
103                 Range(lyx::pos_type s, lyx::pos_type e)
104                         : start(s), end(e) {}
105
106                 // does this range contain r ?
107                 bool contains(Range const & r) const;
108
109                 // does this range contain pos ?
110                 bool contains(lyx::pos_type pos) const;
111
112                 // does this range contain pos, or can it be appended ?
113                 bool loose_contains(lyx::pos_type pos) const;
114
115                 // is this range contained within r ?
116                 bool contained(Range const & r) const;
117
118                 // do the ranges intersect ?
119                 bool intersects(Range const & r) const;
120
121                 lyx::pos_type start;
122                 lyx::pos_type end;
123         };
124
125         friend bool operator==(Range const & r1, Range const & r2);
126         friend bool operator!=(Range const & r1, Range const & r2);
127
128         class ChangeRange {
129         public:
130                 ChangeRange(lyx::pos_type s, lyx::pos_type e, Change c)
131                         : range(Range(s, e)), change(c) {}
132                 Range range;
133                 Change change;
134         };
135
136         typedef std::vector<ChangeRange> ChangeTable;
137
138         /// our table of changes, every row a range and change descriptor
139         ChangeTable table_;
140
141         /// change type for an empty paragraph
142         Change::Type empty_type_;
143
144         /// handle a delete, either logical or physical (see erase)
145         void del(Change change, ChangeTable::size_type pos);
146
147         /// handle an add, adjusting range bounds past it
148         void add(Change change, ChangeTable::size_type pos);
149
150         /// merge neighbouring ranges, assuming that they are abutting
151         /// (as done by set())
152         void merge();
153
154         /// consistency check, needed before merge()
155         void check() const;
156
157 };
158
159 #endif // CHANGES_H