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