]> git.lyx.org Git - lyx.git/blob - src/Changes.h
Avoid full metrics computation with Update:FitCursor
[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/strfwd.h"
19 #include "support/types.h"
20 #include "support/lyxtime.h"
21
22 #include <vector>
23
24
25 namespace lyx {
26
27 class AuthorList;
28 class Buffer;
29 class Color;
30 class DocIterator;
31 class FontInfo;
32 class OutputParams;
33 class otexstream;
34 class PainterInfo;
35 class TocBackend;
36
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         /// set the pos to the given change
98         void set(Change const & change, pos_type pos);
99         /// set the range (excluding end) to the given change
100         void set(Change const & change, pos_type start, pos_type end);
101
102         /// erase the entry at pos and adjust all range bounds past it
103         /// (assumes that a character was deleted at pos)
104         void erase(pos_type pos);
105
106         /// insert a new entry at pos and adjust all range bounds past it
107         /// (assumes that a character was inserted at pos)
108         void insert(Change const & change, pos_type pos);
109
110         ///
111
112         /// return the change at the given pos
113         Change const & lookup(pos_type pos) const;
114
115         /// return true if there is a change in the given range (excluding end)
116         bool isChanged(pos_type start, pos_type end) const;
117         ///
118         bool isChanged() const;
119
120         /// return true if the whole range is deleted
121         bool isDeleted(pos_type start, pos_type end) const;
122
123         /// output latex to mark a transition between two change types
124         /// returns length of text outputted
125         static int latexMarkChange(otexstream & os, BufferParams const & bparams,
126                                    Change const & oldChange, Change const & change,
127                                    OutputParams const & runparams);
128
129         /// output .lyx file format for transitions between changes
130         static void lyxMarkChange(std::ostream & os, BufferParams const & bparams,
131                 int & column, Change const & old, Change const & change);
132
133         ///
134         void checkAuthors(AuthorList const & authorList) const;
135
136         ///
137         void addToToc(DocIterator const & cdit, Buffer const & buffer,
138                       bool output_active, TocBackend & backend) const;
139
140 private:
141         class Range {
142         public:
143                 Range(pos_type s, pos_type e)
144                         : start(s), end(e) {}
145
146                 // does this range contain r ? (inlined as the result of profiling)
147                 bool contains(Range const & r) const {
148                         return r.start >= start && r.end <= end;
149                 }
150
151                 // does this range contain pos ? (inlined as the result of profiling)
152                 bool contains(pos_type pos) const {
153                         return pos >= start && pos < end;
154                 }
155
156                 // do the ranges intersect ?
157                 bool intersects(Range const & r) const;
158
159                 pos_type start;
160                 pos_type end; // Caution: end is not in the range!
161         };
162
163         friend bool operator==(Range const & r1, Range const & r2);
164         friend bool operator!=(Range const & r1, Range const & r2);
165
166         class ChangeRange {
167         public:
168                 ChangeRange(Change const & c, Range const & r)
169                         : change(c), range(r) {}
170
171                 Change change;
172                 Range range;
173         };
174
175         /// merge equal changes with adjoining ranges
176         void merge();
177
178         typedef std::vector<ChangeRange> ChangeTable;
179
180         /// table of changes, every row a change and range descriptor
181         ChangeTable table_;
182 };
183
184
185 } // namespace lyx
186
187 #endif // CHANGES_H