]> git.lyx.org Git - lyx.git/blob - src/texrow.C
copy some code over to allow work to start on prefs
[lyx.git] / src / texrow.C
1 /**
2  * \file texrow.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Matthias Ettrich
7  */
8
9 #include <config.h>
10
11 #include <algorithm>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "texrow.h"
18 #include "paragraph.h"
19 #include "debug.h"
20
21 using std::find_if;
22 using std::for_each;
23 using std::endl;
24
25 namespace {
26  
27 /// function object returning true when row number is found
28 class same_rownumber {
29 public:
30         same_rownumber(int row) : row_(row) {}
31         bool operator()(TexRow::RowList::value_type const & vt) const {
32                 return vt.rownumber() == row_;
33         }
34  
35 private:
36         int row_;
37 };
38
39
40 /// increment the pos value of the argument if the par id
41 /// is the same, and the pos parameter is larger
42 class increase_pos {
43 public:
44         increase_pos(int id, int pos)
45                 : id_(id), pos_(pos) {}
46
47         void operator()(TexRow::RowList::value_type & vt) const {
48                 if (vt.id() != id_ || vt.pos() >= pos_)
49                         return;
50                 vt.pos(vt.pos() + 1);
51  
52                 lyxerr[Debug::INFO]
53                         << "TeXRow::increasePos: ideally this "
54                         "should never happen..." << endl;
55
56                 // FIXME: When verified to work this clause should be deleted.
57                 if (id_ == vt.id() && pos_ == vt.pos()) {
58                         lyxerr[Debug::INFO]
59                                 << "TexRow::increasePos: this should happen "
60                                 "maximum one time for each run of "
61                                 "increasePos!" << endl;
62                 }
63         }
64
65 private:
66         int id_;
67         int pos_;
68 };
69  
70 } // namespace anon 
71
72  
73 void TexRow::reset()
74 {
75         rowlist.clear();
76         count = 0;
77         lastpar = 0;
78         lastpos = -1;
79 }
80
81
82 void TexRow::start(Paragraph * par, int pos)
83 {
84         lastpar = par;
85         lastpos = pos;
86 }
87
88
89 void TexRow::newline()
90 {
91         int const id = lastpar ? lastpar->id() : -1;
92         RowList::value_type tmp(id, lastpos, ++count);
93         rowlist.push_back(tmp);
94 }
95
96
97 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
98 {
99         RowList::const_iterator cit =
100                 find_if(rowlist.begin(), rowlist.end(), 
101                         same_rownumber(row));
102  
103         if (cit != rowlist.end()) {
104                 id = cit->id();
105                 pos = cit->pos();
106                 return true;
107         }
108         id = -1;
109         pos = 0;
110         return false;
111 }
112
113
114 void TexRow::increasePos(int id, int pos)
115 {
116         for_each(rowlist.begin(), rowlist.end(), increase_pos(id, pos));
117 }
118
119
120 TexRow & TexRow::operator+=(TexRow const & tr)
121 {
122         rowlist.insert(rowlist.end(), tr.rowlist.begin(), tr.rowlist.end());
123         return *this;
124 }