]> git.lyx.org Git - lyx.git/blob - src/Changes.h
rename MathArray into MathData
[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  * \author Michael Gerz
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Record changes in a paragraph.
13  */
14
15 #ifndef CHANGES_H
16 #define CHANGES_H
17
18 #include "support/docstream.h"
19 #include "support/lyxtime.h"
20
21 #include <vector>
22
23
24 namespace lyx {
25
26
27 class Change {
28 public:
29         /// the type of change
30         enum Type {
31                 UNCHANGED, // no change
32                 INSERTED, // new text
33                 DELETED // deleted text
34         };
35
36         explicit Change(Type t, int a = 0, time_type ct = current_time())
37                 : type(t), author(a), changetime(ct) {}
38
39         /// is the change similar to the given change such that both can be merged?
40         bool isSimilarTo(Change const & change);
41
42         Type type;
43
44         int author;
45
46         time_type changetime;
47 };
48
49 bool operator==(Change const & l, Change const & r);
50 bool operator!=(Change const & l, Change const & r);
51
52 class Changes {
53 public:
54         /// set the pos to the given change
55         void set(Change const & change, pos_type pos);
56         /// set the range (excluding end) to the given change
57         void set(Change const & change, pos_type start, pos_type end);
58
59         /// erase the entry at pos and adjust all range bounds past it
60         /// (assumes that a character was deleted at pos)
61         void erase(lyx::pos_type pos);
62
63         /// insert a new entry at pos and adjust all range bounds past it
64         /// (assumes that a character was inserted at pos)
65         void insert(Change const & change, lyx::pos_type pos);
66
67         ///
68
69         /// return the change at the given pos
70         Change const & lookup(pos_type pos) const;
71
72         /// return true if there is a change in the given range (excluding end)
73         bool isChanged(pos_type start, pos_type end) const;
74
75         ///
76
77         /// output latex to mark a transition between two change types
78         /// returns length of text outputted
79         static int latexMarkChange(odocstream & os, Change::Type oldChangeType,
80                 Change::Type changeType, bool const & output);
81
82         /// output .lyx file format for transitions between changes
83         static void lyxMarkChange(std::ostream & os, int & column,
84                 Change const & old, Change const & change);
85
86 private:
87         class Range {
88         public:
89                 Range(pos_type s, pos_type e)
90                         : start(s), end(e) {}
91
92                 // does this range contain r ? (inlined as the result of profiling)
93                 bool contains(Range const & r) const {
94                         return r.start >= start && r.end <= end;
95                 }
96
97                 // does this range contain pos ? (inlined as the result of profiling)
98                 bool contains(pos_type pos) const {
99                         return pos >= start && pos < end;
100                 }
101
102                 // do the ranges intersect ?
103                 bool intersects(Range const & r) const;
104
105                 pos_type start;
106                 pos_type end; // Caution: end is not in the range!
107         };
108
109         friend bool operator==(Range const & r1, Range const & r2);
110         friend bool operator!=(Range const & r1, Range const & r2);
111
112         class ChangeRange {
113         public:
114                 ChangeRange(Change const & c, Range const & r)
115                         : change(c), range(r) {}
116
117                 Change change;
118                 Range range;
119         };
120
121         /// merge equal changes with adjoining ranges
122         void merge();
123
124         typedef std::vector<ChangeRange> ChangeTable;
125
126         /// table of changes, every row a change and range descriptor
127         ChangeTable table_;
128 };
129
130
131 } // namespace lyx
132
133 #endif // CHANGES_H