]> git.lyx.org Git - lyx.git/blob - src/changes.h
Change the color of the background widget to red.
[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/types.h"
18 #include "support/lyxtime.h"
19
20 #include <vector>
21 #include <iosfwd>
22
23
24 struct Change {
25         /// the type of change
26         enum Type {
27                 UNCHANGED, // no change
28                 INSERTED, // new text
29                 DELETED // deleted text
30         };
31
32         Change(Type t = UNCHANGED, 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 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, lyx::pos_type start, lyx::pos_type end);
70
71         /// mark the given change and adjust
72         void record(Change, lyx::pos_type pos);
73
74         /// return the change type at the given position
75         Change::Type lookup(lyx::pos_type pos) const;
76
77         /// return the change at the given position
78         Change const lookupFull(lyx::pos_type pos) const;
79
80         /// return true if there is a change in the given range
81         bool isChange(lyx::pos_type start, lyx::pos_type end) const;
82
83         /// return true if there is a deleted or unchanged range contained
84         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
85
86         /// remove the given entry
87         void erase(lyx::pos_type pos);
88
89         /// output latex to mark a transition between two changetypes
90         /// returns length of text outputted
91         static int latexMarkChange(std::ostream & os, Change::Type old, Change::Type change);
92
93         /// output .lyx file format for transitions between changes
94         static void lyxMarkChange(std::ostream & os, int & column,
95                 lyx::time_type curtime, Change const & old, Change const & change);
96
97 private:
98         struct Range {
99                 Range(lyx::pos_type s, lyx::pos_type e)
100                         : start(s), end(e) {}
101
102                 // does this range contain r ?
103                 bool contains(Range const & r) const;
104
105                 // does this range contain pos ?
106                 bool contains(lyx::pos_type pos) const;
107
108                 // does this range contain pos, or can it be appended ?
109                 bool loose_contains(lyx::pos_type pos) const;
110
111                 // is this range contained within r ?
112                 bool contained(Range const & r) const;
113
114                 // do the ranges intersect ?
115                 bool intersects(Range const & r) const;
116
117                 lyx::pos_type start;
118                 lyx::pos_type end;
119         };
120
121         friend bool operator==(Range const & r1, Range const & r2);
122         friend bool operator!=(Range const & r1, Range const & r2);
123
124         struct ChangeRange {
125                 ChangeRange(lyx::pos_type s, lyx::pos_type e, Change c)
126                         : range(Range(s, e)), change(c) {}
127                 Range range;
128                 Change change;
129         };
130
131         typedef std::vector<ChangeRange> ChangeTable;
132
133         /// our table of changes
134         ChangeTable table_;
135
136         /// change type for an empty paragraph
137         Change::Type empty_type_;
138
139         /// handle a delete
140         void del(Change change, ChangeTable::size_type pos);
141
142         /// handle an add
143         void add(Change change, ChangeTable::size_type pos);
144
145         /// merge neighbouring ranges
146         void merge();
147
148         /// consistency check
149         void check() const;
150
151 };
152
153 #endif // CHANGES_H