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