]> git.lyx.org Git - lyx.git/blob - src/changes.h
75c83e3c442a443c1e0a8cbc0f4032600914a2c2
[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  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Record changes in a paragraph.
12  */
13
14 #ifndef CHANGES_H
15 #define CHANGES_H
16
17 #include "support/docstream.h"
18 #include "support/lyxtime.h"
19
20 #include <vector>
21
22
23 class Change {
24 public:
25         /// the type of change
26         enum Type {
27                 UNCHANGED, // no change
28                 INSERTED, // new text
29                 DELETED // deleted text
30         };
31
32         explicit Change(Type t, int a = 0, lyx::time_type ct = 0)
33                 : type(t), author(a), changetime(ct) {}
34
35         Type type;
36
37         int author;
38
39         lyx::time_type changetime;
40 };
41
42 bool operator==(Change const & l, Change const & r);
43 bool operator!=(Change const & l, Change const & r);
44
45 class Changes {
46 public:
47
48         Changes(Change::Type type);
49
50         ~Changes();
51
52         Changes(Changes const &);
53
54         /// reset "default" change type (for empty pars)
55         void reset(Change::Type type) {
56                 empty_type_ = type;
57         }
58
59         /// set the position to the given change
60         void set(Change const & change, lyx::pos_type pos);
61
62         /// set the position to the given change
63         void set(Change::Type, lyx::pos_type pos);
64
65         /// set the range to the given change
66         void set(Change::Type, lyx::pos_type start, lyx::pos_type end);
67
68         /// set the range to the given change
69         void set(Change const & change, lyx::pos_type start, lyx::pos_type end);
70
71         /// mark the given change and adjust
72         void record(Change const & change, lyx::pos_type pos);
73
74         /// return the change at the given position
75         Change const lookup(lyx::pos_type pos) const;
76
77         /// return true if there is a change in the given range
78         bool isChange(lyx::pos_type start, lyx::pos_type end) const;
79
80         /// remove the given entry. This implies that a character was
81         /// deleted at pos, and will adjust all range bounds past it
82         void erase(lyx::pos_type pos);
83
84         /// output latex to mark a transition between two changetypes
85         /// returns length of text outputted
86         static int latexMarkChange(lyx::odocstream & os, Change::Type old,
87                 Change::Type change, bool const & output);
88
89         /// output .lyx file format for transitions between changes
90         static void lyxMarkChange(std::ostream & os, int & column,
91                 lyx::time_type curtime, Change const & old, Change const & change);
92
93 private:
94         class Range {
95         public:
96                 Range(lyx::pos_type s, lyx::pos_type e)
97                         : start(s), end(e) {}
98
99                 // does this range contain r ?
100                 bool contains(Range const & r) const;
101
102                 // does this range contain pos ?
103                 bool contains(lyx::pos_type pos) const;
104
105                 // does this range contain pos, or can it be appended ?
106                 bool containsOrPrecedes(lyx::pos_type pos) const;
107
108                 // is this range contained within r ?
109                 bool contained(Range const & r) const;
110
111                 // do the ranges intersect ?
112                 bool intersects(Range const & r) const;
113
114                 lyx::pos_type start;
115                 lyx::pos_type end;
116         };
117
118         friend bool operator==(Range const & r1, Range const & r2);
119         friend bool operator!=(Range const & r1, Range const & r2);
120
121         class ChangeRange {
122         public:
123                 ChangeRange(lyx::pos_type s, lyx::pos_type e, Change const & c)
124                         : range(Range(s, e)), change(c) {}
125                 Range range;
126                 Change change;
127         };
128
129         typedef std::vector<ChangeRange> ChangeTable;
130
131         /// our table of changes, every row a range and change descriptor
132         ChangeTable table_;
133
134         /// change type for an empty paragraph
135         Change::Type empty_type_;
136
137         /// handle a delete, either logical or physical (see erase)
138         void del(Change const & change, ChangeTable::size_type pos);
139
140         /// handle an add, adjusting range bounds past it
141         void add(Change const & change, ChangeTable::size_type pos);
142
143         /// merge neighbouring ranges, assuming that they are abutting
144         /// (as done by set())
145         void merge();
146
147         /// consistency check, needed before merge()
148         void check() const;
149
150 };
151
152 #endif // CHANGES_H