]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
* Painter.h:
[lyx.git] / src / paragraph_funcs.C
1 /**
2  * \file paragraph_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14
15 #include "bufferparams.h"
16 #include "lyxtext.h"
17 #include "paragraph_pimpl.h"
18
19
20 namespace lyx {
21
22 using std::string;
23
24
25 static bool moveItem(Paragraph & from, Paragraph & to,
26         BufferParams const & params, pos_type i, pos_type j,
27         Change change = Change(Change::INSERTED))
28 {
29         Paragraph::value_type const tmpchar = from.getChar(i);
30         LyXFont tmpfont = from.getFontSettings(params, i);
31
32         if (tmpchar == Paragraph::META_INSET) {
33                 InsetBase * tmpinset = 0;
34                 if (from.getInset(i)) {
35                         // the inset is not in a paragraph anymore
36                         tmpinset = from.insetlist.release(i);
37                         from.insetlist.erase(i);
38                 }
39
40                 if (!to.insetAllowed(tmpinset->lyxCode())) {
41                         delete tmpinset;
42                         return false;
43                 }
44                 if (tmpinset)
45                         to.insertInset(j, tmpinset, tmpfont, change);
46         } else {
47                 to.insertChar(j, tmpchar, tmpfont, change);
48         }
49         return true;
50 }
51
52
53 void breakParagraph(BufferParams const & bparams,
54         ParagraphList & pars, pit_type par_offset, pos_type pos, int flag)
55 {
56         // create a new paragraph, and insert into the list
57         ParagraphList::iterator tmp =
58                 pars.insert(boost::next(pars.begin(), par_offset + 1),
59                             Paragraph());
60
61         Paragraph & par = pars[par_offset];
62
63         // we will invalidate the row cache
64         par.rows().clear();
65
66         // without doing that we get a crash when typing <Return> at the
67         // end of a paragraph
68         tmp->layout(bparams.getLyXTextClass().defaultLayout());
69         // remember to set the inset_owner
70         tmp->setInsetOwner(par.inInset());
71
72         // this is an idea for a more userfriendly layout handling, I will
73         // see what the users say
74
75         // layout stays the same with latex-environments
76         if (flag) {
77                 tmp->layout(par.layout());
78                 tmp->setLabelWidthString(par.params().labelWidthString());
79                 tmp->params().depth(par.params().depth());
80         } else if (par.params().depth() > 0) {
81                 Paragraph const & hook = pars[outerHook(par_offset, pars)];
82                 tmp->layout(hook.layout());
83                 // not sure the line below is useful
84                 tmp->setLabelWidthString(par.params().labelWidthString());
85                 tmp->params().depth(hook.params().depth());
86         }
87
88         bool const isempty = (par.allowEmpty() && par.empty());
89
90         if (!isempty && (par.size() > pos || par.empty() || flag == 2)) {
91                 tmp->layout(par.layout());
92                 tmp->params().align(par.params().align());
93                 tmp->setLabelWidthString(par.params().labelWidthString());
94
95                 tmp->params().depth(par.params().depth());
96                 tmp->params().noindent(par.params().noindent());
97
98                 // copy everything behind the break-position
99                 // to the new paragraph
100
101                 /* Note: if !keepempty, empty() == true, then we reach
102                  * here with size() == 0. So pos_end becomes - 1. This
103                  * doesn't cause problems because both loops below
104                  * enforce pos <= pos_end and 0 <= pos
105                  */
106                 pos_type pos_end = par.size() - 1;
107
108                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
109                         Change::Type change = par.lookupChange(i).type;
110                         if (moveItem(par, *tmp, bparams, i, j - pos)) {
111                                 // FIXME: change tracking (MG)
112                                 tmp->setChange(j - pos, Change(change));
113                                 ++j;
114                         }
115                 }
116
117                 for (pos_type i = pos_end; i >= pos; --i)
118                         // FIXME: change tracking (MG)
119                         par.eraseChar(i, false); // erase without change tracking
120         }
121
122         if (pos) {
123                 // Make sure that we keep the language when
124                 // breaking paragrpah.
125                 if (tmp->empty()) {
126                         LyXFont changed = tmp->getFirstFontSettings(bparams);
127                         LyXFont old = par.getFontSettings(bparams, par.size());
128                         changed.setLanguage(old.language());
129                         tmp->setFont(0, changed);
130                 }
131
132                 return;
133         }
134
135         if (!isempty) {
136                 par.params().clear();
137                 par.layout(bparams.getLyXTextClass().defaultLayout());
138         }
139
140         // layout stays the same with latex-environments
141         if (flag) {
142                 par.layout(tmp->layout());
143                 par.setLabelWidthString(tmp->params().labelWidthString());
144                 par.params().depth(tmp->params().depth());
145         }
146
147         // subtle, but needed to get empty pars working right
148         if (bparams.trackChanges) {
149                 // FIXME: Change tracking (MG)
150                 // if (!par.size())
151                 //      set 'par' text to INSERTED in CT mode; clear CT info otherwise
152                 // else if (!tmp->size())
153                 //      set 'tmp' text to INSERTED in CT mode; clear CT info otherwise
154         }
155 }
156
157
158 void breakParagraphConservative(BufferParams const & bparams,
159         ParagraphList & pars, pit_type par_offset, pos_type pos)
160 {
161         // create a new paragraph
162         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
163                                        Paragraph());
164         Paragraph & par = pars[par_offset];
165
166         tmp.makeSameLayout(par);
167
168         // When can pos > size()?
169         // I guess pos == size() is possible.
170         if (par.size() > pos) {
171                 // copy everything behind the break-position to the new
172                 // paragraph
173                 pos_type pos_end = par.size() - 1;
174
175                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
176                         Change::Type change = par.lookupChange(i).type;
177                         // FIXME: change tracking (MG)
178                         if (moveItem(par, tmp, bparams, i, j - pos, Change(change)))
179                                 ++j;
180                 }
181                 // Move over end-of-par change attr
182                 // FIXME: change tracking (MG)
183                 tmp.setChange(tmp.size(), Change(par.lookupChange(par.size()).type));
184
185                 // If tracking changes, set all the text that is to be
186                 // erased to Type::INSERTED.
187                 for (pos_type k = pos_end; k >= pos; --k) {
188                         if (bparams.trackChanges)
189                                 // FIXME: Change tracking (MG)
190                                 par.setChange(k, Change(Change::INSERTED));
191                         // FIXME: change tracking (MG)
192                         par.eraseChar(k, false);
193                 }
194         }
195 }
196
197
198 void mergeParagraph(BufferParams const & bparams,
199         ParagraphList & pars, pit_type par_offset)
200 {
201         Paragraph & next = pars[par_offset + 1];
202         Paragraph & par = pars[par_offset];
203
204         pos_type pos_end = next.size() - 1;
205         pos_type pos_insert = par.size();
206
207         // What happens is the following. Later on, moveItem() will copy
208         // over characters from the next paragraph to be inserted into this
209         // position. Now, if the first char to be so copied is "red" (i.e.,
210         // marked deleted) and the paragraph break is marked "blue",
211         // insertChar will trigger (eventually, through record(), and see
212         // del() and erase() in changes.C) a "hard" character deletion.
213         // Which doesn't make sense of course at this pos, but the effect is
214         // to shorten the change range to which this para break belongs, by
215         // one. It will (should) remain "orphaned", having no CT info to it,
216         // and check() in changes.C will assert. Setting the para break
217         // forcibly to "black" prevents this scenario. -- MV 13.3.2006
218         // FIXME: change tracking (MG)
219         par.setChange(par.size(), Change(Change::UNCHANGED));
220
221         Change::Type cr = next.lookupChange(next.size()).type;
222         // ok, now copy the paragraph
223         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
224                 Change::Type change = next.lookupChange(i).type;
225                 // FIXME: change tracking (MG)
226                 if (moveItem(next, par, bparams, i, pos_insert + j, Change(change)))
227                         ++j;
228         }
229         // Move the change status of "carriage return" over
230         // FIXME: change tracking (MG)
231         par.setChange(par.size(), Change(cr));
232
233         pars.erase(boost::next(pars.begin(), par_offset + 1));
234 }
235
236
237 pit_type depthHook(pit_type pit, ParagraphList const & pars, depth_type depth)
238 {
239         pit_type newpit = pit;
240
241         if (newpit != 0)
242                 --newpit;
243
244         while (newpit != 0 && pars[newpit].getDepth() > depth)
245                 --newpit;
246
247         if (pars[newpit].getDepth() > depth)
248                 return pit;
249
250         return newpit;
251 }
252
253
254 pit_type outerHook(pit_type par_offset, ParagraphList const & pars)
255 {
256         Paragraph const & par = pars[par_offset];
257
258         if (par.getDepth() == 0)
259                 return pars.size();
260         return depthHook(par_offset, pars, depth_type(par.getDepth() - 1));
261 }
262
263
264 bool isFirstInSequence(pit_type par_offset, ParagraphList const & pars)
265 {
266         Paragraph const & par = pars[par_offset];
267
268         pit_type dhook_offset = depthHook(par_offset, pars, par.getDepth());
269
270         Paragraph const & dhook = pars[dhook_offset];
271
272         return dhook_offset == par_offset
273                 || dhook.layout() != par.layout()
274                 || dhook.getDepth() != par.getDepth();
275 }
276
277
278 int getEndLabel(pit_type p, ParagraphList const & pars)
279 {
280         pit_type pit = p;
281         depth_type par_depth = pars[p].getDepth();
282         while (pit != pit_type(pars.size())) {
283                 LyXLayout_ptr const & layout = pars[pit].layout();
284                 int const endlabeltype = layout->endlabeltype;
285
286                 if (endlabeltype != END_LABEL_NO_LABEL) {
287                         if (p + 1 == pit_type(pars.size()))
288                                 return endlabeltype;
289
290                         depth_type const next_depth =
291                                 pars[p + 1].getDepth();
292                         if (par_depth > next_depth ||
293                             (par_depth == next_depth && layout != pars[p + 1].layout()))
294                                 return endlabeltype;
295                         break;
296                 }
297                 if (par_depth == 0)
298                         break;
299                 pit = outerHook(pit, pars);
300                 if (pit != pit_type(pars.size()))
301                         par_depth = pars[pit].getDepth();
302         }
303         return END_LABEL_NO_LABEL;
304 }
305
306
307 LyXFont const outerFont(pit_type par_offset, ParagraphList const & pars)
308 {
309         depth_type par_depth = pars[par_offset].getDepth();
310         LyXFont tmpfont(LyXFont::ALL_INHERIT);
311
312         // Resolve against environment font information
313         while (par_offset != pit_type(pars.size())
314                && par_depth
315                && !tmpfont.resolved()) {
316                 par_offset = outerHook(par_offset, pars);
317                 if (par_offset != pit_type(pars.size())) {
318                         tmpfont.realize(pars[par_offset].layout()->font);
319                         par_depth = pars[par_offset].getDepth();
320                 }
321         }
322
323         return tmpfont;
324 }
325
326
327 /// return the number of InsetOptArg in a paragraph
328 int numberOfOptArgs(Paragraph const & par)
329 {
330         int num = 0;
331
332         InsetList::const_iterator it = par.insetlist.begin();
333         InsetList::const_iterator end = par.insetlist.end();
334         for (; it != end ; ++it) {
335                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE)
336                         ++num;
337         }
338         return num;
339 }
340
341
342 } // namespace lyx