]> git.lyx.org Git - lyx.git/blob - src/texrow.C
7399843df0f5532b36697d4eb823a600b89a01a8
[lyx.git] / src / texrow.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright (C) 1995 Matthias Ettrich
7  *          Copyright (C) 1995-1999 The LyX Team.
8  *
9  * ======================================================*/
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "texrow.h"
18 #include "lyxparagraph.h"
19 #include "debug.h"
20
21 // Delete linked list
22 void TexRow::reset()
23 {
24         TexRow_Item *current, *iter = next;
25         while (iter) {
26                 // Iterate through the list deleting as you go.
27                 // A bit easier to debug than recursive deletion.
28                 current = iter;
29                 iter = iter->next;
30                 delete current;
31         }
32         count = 0;
33         next = 0;
34         lastpar = 0;
35         lastpos = -1;
36 }
37
38 // Defines paragraph and position for the beginning of this row
39 void TexRow::start(LyXParagraph *par, int pos) {
40         lastpar = par;
41         lastpos = pos;
42 }
43
44 // Insert node when line is completed
45 void TexRow::newline()
46 {
47         TexRow_Item *tmp = new TexRow_Item;
48         tmp->pos = lastpos;
49         
50         if (lastpar)
51                 tmp->id = lastpar->GetID();
52         else
53                 tmp->id = -1;
54
55         // Inserts at the beginning of the list
56         tmp->next = next;
57         next = tmp;
58         count++;
59         tmp->rownumber = count;
60 }
61
62
63 void TexRow::getIdFromRow(int row, int &id, int &pos)
64 {
65         TexRow_Item *tmp = next;
66         while (tmp && tmp->rownumber != row) {
67                 tmp = tmp->next;
68         }
69         if (tmp) {
70                 TexRow_Item *tmp2 = next;
71                 // Increase the pos of all rows with the
72                 // same id (and where the pos is larger)
73                 // to avoid putting errorinsets at the
74                 // same pos.
75                 while (tmp2) {
76                         if (tmp2 != tmp &&
77                             tmp2->id == tmp->id &&
78                             tmp2->pos >= tmp->pos)
79                                 tmp2->pos++;
80                         tmp2 = tmp2->next;
81                 }
82                 id = tmp->id;
83                 pos = tmp->pos;
84         } else {
85                 id = -1;
86                 pos = 0;
87         }
88 }
89
90
91 TexRow & TexRow::operator+=(const TexRow &tr)
92 {
93         // remember that the lists are stored in reverse 
94         // so you've got to turn the second one around 
95         // as you insert it in the first
96         for (int counter = tr.count; counter > 0; --counter) {
97                 int i = 1;
98                 TexRow_Item *iter = tr.next;
99                 while (i < counter) {
100                         iter = iter->next;
101                         ++i;
102                 }
103
104                 ++count;
105                 TexRow_Item *tmp;
106                 tmp = new TexRow_Item;
107                 tmp->id = iter->id;
108                 tmp->pos = iter->pos;
109                 tmp->next = next;
110                 tmp->rownumber = count;
111                 next = tmp;
112         }
113         // should I be doing this or not?
114         //lastpar = tr.lastpar;
115         //lastpos = tr.lastpos;
116         return *this;
117 }