]> git.lyx.org Git - lyx.git/blob - src/changes.h
namespace grfx -> lyx::graphics
[lyx.git] / src / changes.h
1 /**
2  * \file changes.h
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * Record changes in a paragraph.
7  *
8  * \author John Levon <levon@movementarian.org>
9  */
10
11 #ifndef CHANGES_H
12 #define CHANGES_H
13
14 #include "support/types.h"
15 #include "support/lyxtime.h"
16
17 #include <vector>
18 #include <iosfwd>
19 #include <ctime>
20
21 struct Change {
22         /// the type of change
23         enum Type {
24                 UNCHANGED, // no change
25                 INSERTED, // new text
26                 DELETED // deleted text
27         };
28
29         Change(Type t = UNCHANGED, int a = 0, lyx::time_type ct = 0)
30                 : type(t), author(a), changetime(ct) {}
31
32         Type type;
33
34         int author;
35
36         lyx::time_type changetime;
37 };
38
39 bool operator==(Change const & l, Change const & r);
40 bool operator!=(Change const & l, Change const & r);
41
42 class Changes {
43 public:
44
45         Changes(Change::Type type);
46
47         ~Changes();
48
49         Changes(Changes const &);
50
51         /// reset "default" change type (for empty pars)
52         void reset(Change::Type type) {
53                 empty_type_ = type;
54         }
55
56         /// set the position to the given change
57         void set(Change change, lyx::pos_type pos);
58
59         /// set the position to the given change
60         void set(Change::Type, lyx::pos_type pos);
61
62         /// set the range to the given change
63         void set(Change::Type, lyx::pos_type start, lyx::pos_type end);
64
65         /// set the range to the given change
66         void set(Change, lyx::pos_type start, lyx::pos_type end);
67
68         /// mark the given change and adjust
69         void record(Change, lyx::pos_type pos);
70
71         /// return the change type at the given position
72         Change::Type lookup(lyx::pos_type pos) const;
73
74         /// return the change at the given position
75         Change const lookupFull(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         /// return true if there is a deleted or unchanged range contained
81         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
82
83         /// remove the given entry
84         void erase(lyx::pos_type pos);
85
86         /// output latex to mark a transition between two changetypes
87         /// returns length of text outputted
88         static int latexMarkChange(std::ostream & os, Change::Type old, Change::Type change);
89
90         /// output .lyx file format for transitions between changes
91         static void lyxMarkChange(std::ostream & os, int & column,
92                 lyx::time_type curtime, Change const & old, Change const & change);
93
94 private:
95         struct Range {
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 loose_contains(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         struct ChangeRange {
122                 ChangeRange(lyx::pos_type s, lyx::pos_type e, Change c)
123                         : range(Range(s, e)), change(c) {}
124                 Range range;
125                 Change change;
126         };
127
128         typedef std::vector<ChangeRange> ChangeTable;
129
130         /// our table of changes
131         ChangeTable table_;
132
133         /// change type for an empty paragraph
134         Change::Type empty_type_;
135
136         /// handle a delete
137         void del(Change change, ChangeTable::size_type pos);
138
139         /// handle an add
140         void add(Change change, ChangeTable::size_type pos);
141
142         /// merge neighbouring ranges
143         void merge();
144
145         /// consistency check
146         void check() const;
147
148 };
149
150 #endif // CHANGES_H