]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
change tracking:
[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), text_(0)
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_(in.text_.bv_owner)
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         : text_(0)
107 {}
108
109
110 void InsetText::init()
111 {
112         for_each(paragraphs().begin(), paragraphs().end(),
113                  bind(&Paragraph::setInsetOwner, _1, this));
114 }
115
116
117 void InsetText::clear()
118 {
119         ParagraphList & pars = paragraphs();
120
121         // This is a gross hack...
122         LyXLayout_ptr old_layout = pars.begin()->layout();
123
124         pars.clear();
125         pars.push_back(Paragraph());
126         pars.begin()->setInsetOwner(this);
127         pars.begin()->layout(old_layout);
128 }
129
130
131 auto_ptr<InsetBase> InsetText::doClone() const
132 {
133         return auto_ptr<InsetBase>(new InsetText(*this));
134 }
135
136
137 void InsetText::write(Buffer const & buf, ostream & os) const
138 {
139         os << "Text\n";
140         text_.write(buf, os);
141 }
142
143
144 void InsetText::read(Buffer const & buf, LyXLex & lex)
145 {
146         clear();
147
148         // delete the initial paragraph
149         Paragraph oldpar = *paragraphs().begin();
150         paragraphs().clear();
151         ErrorList errorList;
152         bool res = text_.read(buf, lex, errorList);
153         init();
154
155         if (!res) {
156                 lex.printError("Missing \\end_inset at this point. "
157                                            "Read: `$$Token'");
158         }
159
160         // sanity check
161         // ensure we have at least one paragraph.
162         if (paragraphs().empty())
163                 paragraphs().push_back(oldpar);
164 }
165
166
167 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
168 {
169         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
170         setViewCache(mi.base.bv);
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         text_.metrics(mi, dim);
176         dim.asc += border_;
177         dim.des += border_;
178         dim.wid += 2 * border_;
179         mi.base.textwidth += 2 * border_;
180         dim_ = dim;
181 }
182
183
184 void InsetText::draw(PainterInfo & pi, int x, int y) const
185 {
186         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
187         // update our idea of where we are
188         setPosCache(pi, x, y);
189
190         text_.background_color_ = backgroundColor();
191         text_.draw(pi, x + border_, y);
192
193         if (drawFrame_) {
194                 int const w = text_.width() + 2 * border_;
195                 int const a = text_.ascent() + border_;
196                 int const h = a + text_.descent() + border_;
197                 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
198                         frameColor());
199         }
200 }
201
202
203 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
204 {
205         int const w = text_.width() + 2 * border_;
206         int const a = text_.ascent() + border_;
207         int const h = a + text_.descent() + border_;
208         pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
209                 backgroundColor());
210         text_.drawSelection(pi, x, y);
211 }
212
213
214 bool InsetText::covers(BufferView & bv, int x, int y) const
215 {
216         return bv.coordCache().getInsets().has(this)
217                         && x >= xo(bv)
218                         && x <= xo(bv) + width() + (Wide() ? text_.maxwidth_ : 0)
219                         && y >= yo(bv) - ascent()
220                         && y <= yo(bv) + descent();
221 }
222
223
224 docstring const InsetText::editMessage() const
225 {
226         return _("Opened Text Inset");
227 }
228
229
230 void InsetText::edit(LCursor & cur, bool left)
231 {
232         //lyxerr << "InsetText: edit left/right" << endl;
233         setViewCache(&cur.bv());
234         int const pit = left ? 0 : paragraphs().size() - 1;
235         int const pos = left ? 0 : paragraphs().back().size();
236         text_.setCursor(cur.top(), pit, pos);
237         cur.clearSelection();
238         finishUndo();
239 }
240
241
242 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
243 {
244         return text_.editXY(cur, x, y);
245 }
246
247
248 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
249 {
250         lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
251                              << " [ cmd.action = "
252                              << cmd.action << ']' << endl;
253         setViewCache(&cur.bv());
254         text_.dispatch(cur, cmd);
255 }
256
257
258 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
259         FuncStatus & status) const
260 {
261         return text_.getStatus(cur, cmd, status);
262 }
263
264
265 void InsetText::setChange(Change const & change)
266 {
267         ParagraphList::iterator pit = paragraphs().begin();
268         ParagraphList::iterator end = paragraphs().end();
269         for (; pit != end; ++pit) {
270                 pit->setChange(change);
271         }
272 }
273
274
275 void InsetText::acceptChanges()
276 {
277         ParagraphList::iterator pit = paragraphs().begin();
278         ParagraphList::iterator end = paragraphs().end();
279         for (; pit != end; ++pit) {
280                 // FIXME: change tracking (MG)
281                 // we must handle end-of-par chars!
282                 pit->acceptChanges(0, pit->size() + 1);
283         }
284 }
285
286
287 void InsetText::rejectChanges()
288 {
289         ParagraphList::iterator pit = paragraphs().begin();
290         ParagraphList::iterator end = paragraphs().end();
291         for (; pit != end; ++pit) {
292                 // FIXME: change tracking (MG)
293                 // we must handle end-of-par chars!
294                 pit->rejectChanges(0, pit->size() + 1);
295         }
296 }
297
298
299 int InsetText::latex(Buffer const & buf, odocstream & os,
300                      OutputParams const & runparams) const
301 {
302         TexRow texrow;
303         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
304         return texrow.rows();
305 }
306
307
308 int InsetText::plaintext(Buffer const & buf, odocstream & os,
309                      OutputParams const & runparams) const
310 {
311         ParagraphList::const_iterator beg = paragraphs().begin();
312         ParagraphList::const_iterator end = paragraphs().end();
313         ParagraphList::const_iterator it = beg;
314         bool ref_printed = false;
315         odocstringstream oss;
316         for (; it != end; ++it)
317                 asciiParagraph(buf, *it, oss, runparams, ref_printed);
318
319         docstring const str = oss.str();
320         os << str;
321         // Return how many newlines we issued.
322         return int(lyx::count(str.begin(), str.end(), '\n'));
323 }
324
325
326 int InsetText::docbook(Buffer const & buf, odocstream & os,
327                        OutputParams const & runparams) const
328 {
329         docbookParagraphs(paragraphs(), buf, os, runparams);
330         return 0;
331 }
332
333
334 void InsetText::validate(LaTeXFeatures & features) const
335 {
336         for_each(paragraphs().begin(), paragraphs().end(),
337                  bind(&Paragraph::validate, _1, ref(features)));
338 }
339
340
341 void InsetText::cursorPos(BufferView const & /*bv*/,
342                 CursorSlice const & sl, bool boundary, int & x, int & y) const
343 {
344         x = text_.cursorX(sl, boundary) + border_;
345         y = text_.cursorY(sl, boundary);
346 }
347
348
349 bool InsetText::showInsetDialog(BufferView *) const
350 {
351         return false;
352 }
353
354
355 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
356 {
357         clear();
358         Paragraph & first = paragraphs().front();
359         for (unsigned int i = 0; i < data.length(); ++i)
360                 first.insertChar(i, data[i], font, trackChanges);
361 }
362
363
364 void InsetText::setAutoBreakRows(bool flag)
365 {
366         if (flag == text_.autoBreakRows_)
367                 return;
368
369         text_.autoBreakRows_ = flag;
370         if (flag)
371                 return;
372
373         // remove previously existing newlines
374         ParagraphList::iterator it = paragraphs().begin();
375         ParagraphList::iterator end = paragraphs().end();
376         for (; it != end; ++it)
377                 for (int i = 0; i < it->size(); ++i)
378                         if (it->isNewline(i))
379                                 // do not track the change, because the user
380                                 // is not allowed to revert/reject it
381                                 it->eraseChar(i, false);
382 }
383
384
385 void InsetText::setDrawFrame(bool flag)
386 {
387         drawFrame_ = flag;
388 }
389
390
391 LColor_color InsetText::frameColor() const
392 {
393         return LColor::color(frame_color_);
394 }
395
396
397 void InsetText::setFrameColor(LColor_color col)
398 {
399         frame_color_ = col;
400 }
401
402
403 void InsetText::setViewCache(BufferView const * bv) const
404 {
405         if (bv && bv != text_.bv_owner) {
406                 //lyxerr << "setting view cache from "
407                 //      << text_.bv_owner << " to " << bv << "\n";
408                 text_.bv_owner = const_cast<BufferView *>(bv);
409         }
410 }
411
412
413 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
414 {
415         // There is little we can do here to keep track of changes.
416         // As of 2006/10/20, appendParagraphs is used exclusively by
417         // LyXTabular::setMultiColumn. In this context, the paragraph break
418         // is lost irreversibly and the appended text doesn't really change
419
420         ParagraphList & pl = paragraphs();
421
422         ParagraphList::iterator pit = plist.begin();
423         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
424         ++pit;
425         mergeParagraph(buffer->params(), pl,
426                        std::distance(pl.begin(), ins) - 1);
427
428         for_each(pit, plist.end(),
429                  bind(&ParagraphList::push_back, ref(pl), _1));
430 }
431
432
433 void InsetText::addPreview(PreviewLoader & loader) const
434 {
435         ParagraphList::const_iterator pit = paragraphs().begin();
436         ParagraphList::const_iterator pend = paragraphs().end();
437
438         for (; pit != pend; ++pit) {
439                 InsetList::const_iterator it  = pit->insetlist.begin();
440                 InsetList::const_iterator end = pit->insetlist.end();
441                 for (; it != end; ++it)
442                         it->inset->addPreview(loader);
443         }
444 }
445
446
447 //FIXME: instead of this hack, which only works by chance,
448 // cells should have their own insetcell type, which returns CELL_CODE!
449 bool InsetText::neverIndent() const
450 {
451         // this is only true for tabular cells
452         return !text_.isMainText() && lyxCode() == TEXT_CODE;
453 }
454
455
456 ParagraphList const & InsetText::paragraphs() const
457 {
458         return text_.paragraphs();
459 }
460
461
462 ParagraphList & InsetText::paragraphs()
463 {
464         return text_.paragraphs();
465 }
466
467
468 } // namespace lyx