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