]> git.lyx.org Git - lyx.git/blob - src/changes.h
7ee38031f7ea437e44ff212805527acf08efee0c
[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/docstream.h"
19 #include "support/lyxtime.h"
20
21 #include <vector>
22
23
24 namespace lyx {
25
26
27 class Change {
28 public:
29         /// the type of change
30         enum Type {
31                 UNCHANGED, // no change
32                 INSERTED, // new text
33                 DELETED // deleted text
34         };
35
36         explicit Change(Type t, int a = 0, time_type ct = current_time())
37                 : type(t), author(a), changetime(ct) {}
38
39         Type type;
40
41         int author;
42
43         time_type changetime;
44 };
45
46 bool operator==(Change const & l, Change const & r);
47 bool operator!=(Change const & l, Change const & r);
48
49 class Changes {
50 public:
51         /// set the pos to the given change
52         void set(Change const & change, pos_type pos);
53         /// set the range (excluding end) to the given change
54         void set(Change const & change, pos_type start, pos_type end);
55
56         /// erase the entry at pos and adjust all range bounds past it
57         /// (assumes that a character was deleted at pos)
58         void erase(lyx::pos_type pos);
59
60         /// insert a new entry at pos and adjust all range bounds past it
61         /// (assumes that a character was inserted at pos)
62         void insert(Change const & change, lyx::pos_type pos);
63
64         ///
65
66         /// return the change at the given pos
67         Change const lookup(pos_type pos) const;
68
69         /// return true if there is a change in the given range (excluding end)
70         bool isChanged(pos_type start, pos_type end) const;
71
72         ///
73
74         /// output latex to mark a transition between two change types
75         /// returns length of text outputted
76         static int latexMarkChange(odocstream & os, Change::Type oldChangeType,
77                 Change::Type changeType, bool const & output);
78
79         /// output .lyx file format for transitions between changes
80         static void lyxMarkChange(std::ostream & os, int & column,
81                 Change const & old, Change const & change);
82
83 private:
84         class Range {
85         public:
86                 Range(pos_type s, pos_type e)
87                         : start(s), end(e) {}
88
89                 // does this range contain r ?
90                 bool contains(Range const & r) const;
91
92                 // does this range contain pos ?
93                 bool contains(pos_type pos) const;
94
95                 // do the ranges intersect ?
96                 bool intersects(Range const & r) const;
97
98                 pos_type start;
99                 pos_type end; // Caution: end is not in the range!
100         };
101
102         friend bool operator==(Range const & r1, Range const & r2);
103         friend bool operator!=(Range const & r1, Range const & r2);
104
105         class ChangeRange {
106         public:
107                 ChangeRange(Change const & c, Range const & r)
108                         : change(c), range(r) {}
109
110                 Change change;
111                 Range range;
112         };
113
114         /// merge equal changes with adjoining ranges
115         void merge();
116
117         typedef std::vector<ChangeRange> ChangeTable;
118
119         /// table of changes, every row a change and range descriptor
120         ChangeTable table_;
121 };
122
123
124 } // namespace lyx
125
126 #endif // CHANGES_H