]> git.lyx.org Git - lyx.git/blob - src/TexRow.cpp
ac3c7c7a723e412b64898da388f93f0618c41215
[lyx.git] / src / TexRow.cpp
1 /**
2  * \file TexRow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Matthias Ettrich
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "TexRow.h"
16
17 #include "support/debug.h"
18
19 #include <algorithm>
20
21
22 namespace lyx {
23
24
25 void TexRow::reset()
26 {
27         rowlist.clear();
28         lastid = -1;
29         lastpos = -1;
30 }
31
32
33 void TexRow::start(int id, int pos)
34 {
35         lastid = id;
36         lastpos = pos;
37 }
38
39
40 void TexRow::newline()
41 {
42         int const id = lastid;
43         RowList::value_type tmp(id, lastpos);
44         rowlist.push_back(tmp);
45 }
46
47 void TexRow::newlines(int num_lines)
48 {
49         for (int i = 0; i < num_lines; ++i) {
50                 newline();
51         }
52 }
53
54 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
55 {
56         if (row <= 0 || row > int(rowlist.size())) {
57                 id = -1;
58                 pos = 0;
59                 return false;
60         }
61
62         id = rowlist[row - 1].id();
63         pos = rowlist[row - 1].pos();
64         return true;
65 }
66
67
68 int TexRow::getRowFromIdPos(int id, int pos) const
69 {
70         bool foundid = false;
71         //lyxerr<<"Table:";
72         //for (unsigned int i=0; i<rowlist.size(); i++)
73         //lyxerr<<i<<" (id,pos):\t"<<rowlist[i].id()<<" "<<rowlist[i].pos()<<"\n";
74
75         // this loop finds the last *nonempty* row with the same id
76         // and position <= pos
77         RowList::const_iterator bestrow = rowlist.begin();
78         RowList::const_iterator it = rowlist.begin();
79         RowList::const_iterator const end = rowlist.end();
80         for (; it != end; ++it) {
81                 if (it->id() == id && it->pos() <= pos) {
82                         foundid = true;
83                         if (bestrow->id() != id || it->pos() > bestrow->pos())
84                                 bestrow = it;
85                 } else if (foundid)
86                         break;
87         }
88         if (!foundid)
89                 return rowlist.size();
90         return distance(rowlist.begin(), bestrow);
91 }
92
93
94 } // namespace lyx