]> git.lyx.org Git - lyx.git/blob - src/Changes.h
9219edc6539dcd26c6588f916ffa1618671d8267
[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 "Color.h"
19
20 #include "support/docstream.h"
21 #include "support/strfwd.h"
22 #include "support/types.h"
23 #include "support/lyxtime.h"
24
25 #include <vector>
26
27
28 namespace lyx {
29
30 class AuthorList;
31 class Buffer;
32 class DocIterator;
33 class OutputParams;
34
35 class Change {
36 public:
37         /// the type of change
38         enum Type {
39                 UNCHANGED, // no change
40                 INSERTED, // new text
41                 DELETED // deleted text
42         };
43
44         explicit Change(Type t = UNCHANGED, int a = 0, time_t ct = support::current_time())
45                 : type(t), author(a), changetime(ct) {}
46
47         /// is the change similar to the given change such that both can be merged?
48         bool isSimilarTo(Change const & change) const;
49         /// The color of this change on screen
50         Color color() const;
51         ///
52         bool changed() const { return type != UNCHANGED; }
53         ///
54         void setUnchanged() { type = UNCHANGED; }
55         ///
56         bool inserted() const { return type == INSERTED; }
57         ///
58         void setInserted() { type = INSERTED; }
59         ///
60         bool deleted() const { return type == DELETED; }
61         ///
62         void setDeleted() { type = DELETED; }
63         /// Is this change made by the current author ?
64         bool currentAuthor() const { return author == 0; }
65
66         Type type;
67
68         int author;
69
70         time_t changetime;
71 };
72
73 bool operator==(Change const & l, Change const & r);
74 bool operator!=(Change const & l, Change const & r);
75
76 class BufferParams;
77
78 class Changes {
79 public:
80         /// set the pos to the given change
81         void set(Change const & change, pos_type pos);
82         /// set the range (excluding end) to the given change
83         void set(Change const & change, pos_type start, pos_type end);
84
85         /// erase the entry at pos and adjust all range bounds past it
86         /// (assumes that a character was deleted at pos)
87         void erase(lyx::pos_type pos);
88
89         /// insert a new entry at pos and adjust all range bounds past it
90         /// (assumes that a character was inserted at pos)
91         void insert(Change const & change, lyx::pos_type pos);
92
93         ///
94
95         /// return the change at the given pos
96         Change const & lookup(pos_type pos) const;
97
98         /// return true if there is a change in the given range (excluding end)
99         bool isChanged(pos_type start, pos_type end) const;
100
101         /// return true if the whole range is deleted
102         bool isDeleted(pos_type start, pos_type end) const;
103
104         /// output latex to mark a transition between two change types
105         /// returns length of text outputted
106         static int latexMarkChange(otexstream & os, BufferParams const & bparams,
107                                    Change const & oldChange, Change const & change,
108                                    OutputParams const & runparams);
109
110         /// output .lyx file format for transitions between changes
111         static void lyxMarkChange(std::ostream & os, BufferParams const & bparams,
112                 int & column, Change const & old, Change const & change);
113
114         ///
115         void checkAuthors(AuthorList const & authorList);
116
117         ///
118         void addToToc(DocIterator const & cdit, Buffer const & buffer,
119                 bool output_active) const;
120
121 private:
122         class Range {
123         public:
124                 Range(pos_type s, pos_type e)
125                         : start(s), end(e) {}
126
127                 // does this range contain r ? (inlined as the result of profiling)
128                 bool contains(Range const & r) const {
129                         return r.start >= start && r.end <= end;
130                 }
131
132                 // does this range contain pos ? (inlined as the result of profiling)
133                 bool contains(pos_type pos) const {
134                         return pos >= start && pos < end;
135                 }
136
137                 // do the ranges intersect ?
138                 bool intersects(Range const & r) const;
139
140                 pos_type start;
141                 pos_type end; // Caution: end is not in the range!
142         };
143
144         friend bool operator==(Range const & r1, Range const & r2);
145         friend bool operator!=(Range const & r1, Range const & r2);
146
147         class ChangeRange {
148         public:
149                 ChangeRange(Change const & c, Range const & r)
150                         : change(c), range(r) {}
151
152                 Change change;
153                 Range range;
154         };
155
156         /// merge equal changes with adjoining ranges
157         void merge();
158
159         typedef std::vector<ChangeRange> ChangeTable;
160
161         /// table of changes, every row a change and range descriptor
162         ChangeTable table_;
163 };
164
165
166 } // namespace lyx
167
168 #endif // CHANGES_H