]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
get rid of MSVC warning (signed/unsigned comparison)
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettext.h"
14 #include "insetnewline.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "coordcache.h"
20 #include "CutAndPaste.h"
21 #include "cursor.h"
22 #include "debug.h"
23 #include "dispatchresult.h"
24 #include "errorlist.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "intl.h"
28 #include "LColor.h"
29 #include "lyxfind.h"
30 #include "lyxlex.h"
31 #include "lyxrc.h"
32 #include "lyxtext.h"
33 #include "metricsinfo.h"
34 #include "output_docbook.h"
35 #include "output_latex.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
41 #include "lyxrow.h"
42 #include "sgml.h"
43 #include "texrow.h"
44 #include "undo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/Painter.h"
48
49 #include "support/lyxalgo.h" // count
50
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
53
54 #include <sstream>
55
56
57 namespace lyx {
58
59 using graphics::PreviewLoader;
60
61 using support::isStrUnsignedInt;
62
63 using boost::bind;
64 using boost::ref;
65
66 using std::endl;
67 using std::for_each;
68 using std::max;
69 using std::string;
70 using std::auto_ptr;
71 using std::ostream;
72 using std::vector;
73
74
75 int InsetText::border_ = 2;
76
77
78 InsetText::InsetText(BufferParams const & bp)
79         : drawFrame_(false), frame_color_(LColor::insetframe)
80 {
81         paragraphs().push_back(Paragraph());
82         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
83         // Dispose of the infamous L-shaped cursor.
84         text_.current_font.setLanguage(bp.language);
85         text_.real_current_font.setLanguage(bp.language);
86         init();
87 }
88
89
90 InsetText::InsetText(InsetText const & in)
91         : InsetOld(in), text_()
92 {
93         text_.autoBreakRows_ = in.text_.autoBreakRows_;
94         drawFrame_ = in.drawFrame_;
95         frame_color_ = in.frame_color_;
96         text_.paragraphs() = in.text_.paragraphs();
97         // Hand current buffer language down to "cloned" textinsets
98         // e.g. tabular cells
99         text_.current_font = in.text_.current_font;
100         text_.real_current_font = in.text_.real_current_font;
101         init();
102 }
103
104
105 InsetText::InsetText()
106 {}
107
108
109 void InsetText::init()
110 {
111         for_each(paragraphs().begin(), paragraphs().end(),
112                  bind(&Paragraph::setInsetOwner, _1, this));
113 }
114
115
116 void InsetText::clear()
117 {
118         ParagraphList & pars = paragraphs();
119
120         // This is a gross hack...
121         LyXLayout_ptr old_layout = pars.begin()->layout();
122
123         pars.clear();
124         pars.push_back(Paragraph());
125         pars.begin()->setInsetOwner(this);
126         pars.begin()->layout(old_layout);
127 }
128
129
130 auto_ptr<InsetBase> InsetText::doClone() const
131 {
132         return auto_ptr<InsetBase>(new InsetText(*this));
133 }
134
135
136 void InsetText::write(Buffer const & buf, ostream & os) const
137 {
138         os << "Text\n";
139         text_.write(buf, os);
140 }
141
142
143 void InsetText::read(Buffer const & buf, LyXLex & lex)
144 {
145         clear();
146
147         // delete the initial paragraph
148         Paragraph oldpar = *paragraphs().begin();
149         paragraphs().clear();
150         ErrorList errorList;
151         bool res = text_.read(buf, lex, errorList);
152         init();
153
154         if (!res) {
155                 lex.printError("Missing \\end_inset at this point. "
156                                            "Read: `$$Token'");
157         }
158
159         // sanity check
160         // ensure we have at least one paragraph.
161         if (paragraphs().empty())
162                 paragraphs().push_back(oldpar);
163 }
164
165
166 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
167 {
168         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
169
170         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
171         mi.base.textwidth -= 2 * border_;
172         font_ = mi.base.font;
173         // Hand font through to contained lyxtext:
174         text_.font_ = mi.base.font;
175         tm.metrics(mi, dim);
176         dim.asc += border_;
177         dim.des += border_;
178         dim.wid += 2 * border_;
179         mi.base.textwidth += 2 * border_;
180         bool const changed = dim_ != dim;
181         dim_ = dim;
182         return changed;
183 }
184
185
186 void InsetText::draw(PainterInfo & pi, int x, int y) const
187 {
188         // update our idea of where we are
189         setPosCache(pi, x, y);
190
191         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
192
193         text_.background_color_ = backgroundColor();
194         text_.draw(pi, x + border_, y);
195
196         if (drawFrame_) {
197                 int const w = tm.width() + 2 * border_;
198                 int const a = tm.ascent() + border_;
199                 int const h = a + tm.descent() + border_;
200                 pi.pain.rectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
201                         frameColor());
202         }
203 }
204
205
206 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
207 {
208         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
209
210         int const w = tm.width() + 2 * border_;
211         int const a = tm.ascent() + border_;
212         int const h = a + tm.descent() + border_;
213         pi.pain.fillRectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
214                 backgroundColor());
215         text_.drawSelection(pi, x, y);
216 }
217
218
219 bool InsetText::covers(BufferView const & bv, int x, int y) const
220 {
221         TextMetrics const & tm = bv.textMetrics(&text_);
222
223         return bv.coordCache().getInsets().has(this)
224                         && x >= xo(bv)
225                         && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
226                         && y >= yo(bv) - ascent()
227                         && y <= yo(bv) + descent();
228 }
229
230
231 docstring const InsetText::editMessage() const
232 {
233         return _("Opened Text Inset");
234 }
235
236
237 void InsetText::edit(LCursor & cur, bool left)
238 {
239         //lyxerr << "InsetText: edit left/right" << endl;
240         int const pit = left ? 0 : paragraphs().size() - 1;
241         int const pos = left ? 0 : paragraphs().back().size();
242         text_.setCursor(cur.top(), pit, pos);
243         cur.clearSelection();
244         finishUndo();
245 }
246
247
248 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
249 {
250         return text_.editXY(cur, x, y);
251 }
252
253
254 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
255 {
256         lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
257                              << " [ cmd.action = "
258                              << cmd.action << ']' << endl;
259         text_.dispatch(cur, cmd);
260 }
261
262
263 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
264         FuncStatus & status) const
265 {
266         return text_.getStatus(cur, cmd, status);
267 }
268
269
270 void InsetText::setChange(Change const & change)
271 {
272         ParagraphList::iterator pit = paragraphs().begin();
273         ParagraphList::iterator end = paragraphs().end();
274         for (; pit != end; ++pit) {
275                 pit->setChange(change);
276         }
277 }
278
279
280 void InsetText::acceptChanges(BufferParams const & bparams)
281 {
282         ParagraphList & pars = paragraphs();
283         pit_type const parsize = (pit_type) pars.size();
284
285         // first, accept changes within each individual paragraph (do not consider end-of-par)
286         for (pit_type pit = 0; pit < parsize; ++pit) {
287                 if (pars[pit].size() != 0)   // prevent assertion failure 
288                         pars[pit].acceptChanges(bparams, 0, pars[pit].size());
289         }
290
291         // next, accept imaginary end-of-par characters
292  
293         for (pit_type pit = 0; pit < parsize; ++pit) {
294                 pos_type pos = pars[pit].size();
295
296                 if (pars[pit].isInserted(pos)) {
297                         pars[pit].setChange(pos, Change(Change::UNCHANGED));
298                 } else if (pars[pit].isDeleted(pos)) {
299                         if (pit == pars.size() - 1) {
300                                 // we cannot remove a par break at the end of the last paragraph;
301                                 // instead, we mark it unchanged
302                                 pars[pit].setChange(pos, Change(Change::UNCHANGED));
303                         } else {
304                                 mergeParagraph(bparams, pars, pit);
305                                 --pit;
306                         }
307                 }
308         }
309
310         // FIXME: finally, invoke the DEPM
311 }
312
313
314 void InsetText::rejectChanges(BufferParams const & bparams)
315 {
316         ParagraphList & pars = paragraphs();
317         pit_type const parsize = (pit_type) pars.size();
318
319         // first, reject changes within each individual paragraph (do not consider end-of-par)
320         
321         for (pit_type pit = 0; pit < parsize; ++pit) {
322                 if (pars[pit].size() != 0)   // prevent assertion failure
323                         pars[pit].rejectChanges(bparams, 0, pars[pit].size());
324         }
325
326         // next, reject imaginary end-of-par characters
327  
328         for (pit_type pit = 0; pit < parsize; ++pit) {
329                 pos_type pos = pars[pit].size();
330
331                 if (pars[pit].isDeleted(pos)) {
332                         pars[pit].setChange(pos, Change(Change::UNCHANGED));
333                 } else if (pars[pit].isInserted(pos)) {
334                         if (pit == pars.size() - 1) {
335                                 // we mark the par break at the end of the last paragraph unchanged
336                                 pars[pit].setChange(pos, Change(Change::UNCHANGED));
337                         } else {
338                                 mergeParagraph(bparams, pars, pit);
339                                 --pit;
340                         }
341                 }
342         }
343
344         // FIXME: finally, invoke the DEPM
345 }
346
347
348 int InsetText::latex(Buffer const & buf, odocstream & os,
349                      OutputParams const & runparams) const
350 {
351         TexRow texrow;
352         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
353         return texrow.rows();
354 }
355
356
357 int InsetText::plaintext(Buffer const & buf, odocstream & os,
358                      OutputParams const & runparams) const
359 {
360         ParagraphList::const_iterator beg = paragraphs().begin();
361         ParagraphList::const_iterator end = paragraphs().end();
362         ParagraphList::const_iterator it = beg;
363         bool ref_printed = false;
364         odocstringstream oss;
365         for (; it != end; ++it)
366                 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
367
368         docstring const str = oss.str();
369         os << str;
370         // Return how many newlines we issued.
371         return int(lyx::count(str.begin(), str.end(), '\n'));
372 }
373
374
375 int InsetText::docbook(Buffer const & buf, odocstream & os,
376                        OutputParams const & runparams) const
377 {
378         docbookParagraphs(paragraphs(), buf, os, runparams);
379         return 0;
380 }
381
382
383 void InsetText::validate(LaTeXFeatures & features) const
384 {
385         for_each(paragraphs().begin(), paragraphs().end(),
386                  bind(&Paragraph::validate, _1, ref(features)));
387 }
388
389
390 void InsetText::cursorPos(BufferView const & bv,
391                 CursorSlice const & sl, bool boundary, int & x, int & y) const
392 {
393         x = text_.cursorX(bv, sl, boundary) + border_;
394         y = text_.cursorY(bv, sl, boundary);
395 }
396
397
398 bool InsetText::showInsetDialog(BufferView *) const
399 {
400         return false;
401 }
402
403
404 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
405 {
406         clear();
407         Paragraph & first = paragraphs().front();
408         for (unsigned int i = 0; i < data.length(); ++i)
409                 first.insertChar(i, data[i], font, trackChanges);
410 }
411
412
413 void InsetText::setAutoBreakRows(bool flag)
414 {
415         if (flag == text_.autoBreakRows_)
416                 return;
417
418         text_.autoBreakRows_ = flag;
419         if (flag)
420                 return;
421
422         // remove previously existing newlines
423         ParagraphList::iterator it = paragraphs().begin();
424         ParagraphList::iterator end = paragraphs().end();
425         for (; it != end; ++it)
426                 for (int i = 0; i < it->size(); ++i)
427                         if (it->isNewline(i))
428                                 // do not track the change, because the user
429                                 // is not allowed to revert/reject it
430                                 it->eraseChar(i, false);
431 }
432
433
434 void InsetText::setDrawFrame(bool flag)
435 {
436         drawFrame_ = flag;
437 }
438
439
440 LColor_color InsetText::frameColor() const
441 {
442         return LColor::color(frame_color_);
443 }
444
445
446 void InsetText::setFrameColor(LColor_color col)
447 {
448         frame_color_ = col;
449 }
450
451
452 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
453 {
454         // There is little we can do here to keep track of changes.
455         // As of 2006/10/20, appendParagraphs is used exclusively by
456         // LyXTabular::setMultiColumn. In this context, the paragraph break
457         // is lost irreversibly and the appended text doesn't really change
458
459         ParagraphList & pl = paragraphs();
460
461         ParagraphList::iterator pit = plist.begin();
462         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
463         ++pit;
464         mergeParagraph(buffer->params(), pl,
465                        std::distance(pl.begin(), ins) - 1);
466
467         for_each(pit, plist.end(),
468                  bind(&ParagraphList::push_back, ref(pl), _1));
469 }
470
471
472 void InsetText::addPreview(PreviewLoader & loader) const
473 {
474         ParagraphList::const_iterator pit = paragraphs().begin();
475         ParagraphList::const_iterator pend = paragraphs().end();
476
477         for (; pit != pend; ++pit) {
478                 InsetList::const_iterator it  = pit->insetlist.begin();
479                 InsetList::const_iterator end = pit->insetlist.end();
480                 for (; it != end; ++it)
481                         it->inset->addPreview(loader);
482         }
483 }
484
485
486 //FIXME: instead of this hack, which only works by chance,
487 // cells should have their own insetcell type, which returns CELL_CODE!
488 bool InsetText::neverIndent(Buffer const & buffer) const
489 {
490         // this is only true for tabular cells
491         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
492 }
493
494
495 ParagraphList const & InsetText::paragraphs() const
496 {
497         return text_.paragraphs();
498 }
499
500
501 ParagraphList & InsetText::paragraphs()
502 {
503         return text_.paragraphs();
504 }
505
506
507 } // namespace lyx