]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
* BufferParams:
[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 "outputparams.h"
35 #include "output_docbook.h"
36 #include "output_latex.h"
37 #include "output_plaintext.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
41 #include "rowpainter.h"
42 #include "lyxrow.h"
43 #include "sgml.h"
44 #include "texrow.h"
45 #include "undo.h"
46
47 #include "frontends/Alert.h"
48 #include "frontends/Painter.h"
49
50 #include "support/lyxalgo.h" // count
51
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
54
55 #include <sstream>
56
57
58 namespace lyx {
59
60 using graphics::PreviewLoader;
61
62 using support::isStrUnsignedInt;
63
64 using boost::bind;
65 using boost::ref;
66
67 using std::endl;
68 using std::for_each;
69 using std::max;
70 using std::string;
71 using std::auto_ptr;
72 using std::ostream;
73 using std::vector;
74
75
76 int InsetText::border_ = 2;
77
78
79 InsetText::InsetText(BufferParams const & bp)
80         : drawFrame_(false), frame_color_(LColor::insetframe)
81 {
82         paragraphs().push_back(Paragraph());
83         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
84         // Dispose of the infamous L-shaped cursor.
85         text_.current_font.setLanguage(bp.language);
86         text_.real_current_font.setLanguage(bp.language);
87         init();
88 }
89
90
91 InsetText::InsetText(InsetText const & in)
92         : InsetOld(in), text_()
93 {
94         text_.autoBreakRows_ = in.text_.autoBreakRows_;
95         drawFrame_ = in.drawFrame_;
96         frame_color_ = in.frame_color_;
97         text_.paragraphs() = in.text_.paragraphs();
98         // Hand current buffer language down to "cloned" textinsets
99         // e.g. tabular cells
100         text_.current_font = in.text_.current_font;
101         text_.real_current_font = in.text_.real_current_font;
102         init();
103 }
104
105
106 InsetText::InsetText()
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 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
168 {
169         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
170
171         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
172         mi.base.textwidth -= 2 * border_;
173         font_ = mi.base.font;
174         // Hand font through to contained lyxtext:
175         text_.font_ = mi.base.font;
176         tm.metrics(mi, dim);
177         dim.asc += border_;
178         dim.des += border_;
179         dim.wid += 2 * border_;
180         mi.base.textwidth += 2 * border_;
181         bool const changed = dim_ != dim;
182         dim_ = dim;
183         return changed;
184 }
185
186
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
188 {
189         // update our idea of where we are
190         setPosCache(pi, x, y);
191
192         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
193
194         text_.background_color_ = backgroundColor();
195         text_.draw(pi, x + border_, y);
196
197         if (drawFrame_) {
198                 int const w = tm.width() + 2 * border_;
199                 int const a = tm.ascent() + border_;
200                 int const h = a + tm.descent() + border_;
201                 pi.pain.rectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
202                         frameColor());
203         }
204 }
205
206
207 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
208 {
209         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
210
211         int const w = tm.width() + 2 * border_;
212         int const a = tm.ascent() + border_;
213         int const h = a + tm.descent() + border_;
214         pi.pain.fillRectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
215                 backgroundColor());
216         text_.drawSelection(pi, x + border_, y);
217 }
218
219
220 bool InsetText::covers(BufferView const & bv, int x, int y) const
221 {
222         TextMetrics const & tm = bv.textMetrics(&text_);
223
224         return bv.coordCache().getInsets().has(this)
225                         && x >= xo(bv)
226                         && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
227                         && y >= yo(bv) - ascent()
228                         && y <= yo(bv) + descent();
229 }
230
231
232 docstring const InsetText::editMessage() const
233 {
234         return _("Opened Text Inset");
235 }
236
237
238 void InsetText::edit(LCursor & cur, bool left)
239 {
240         //lyxerr << "InsetText: edit left/right" << endl;
241         int const pit = left ? 0 : paragraphs().size() - 1;
242         int const pos = left ? 0 : paragraphs().back().size();
243         text_.setCursor(cur.top(), pit, pos);
244         cur.clearSelection();
245         finishUndo();
246 }
247
248
249 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
250 {
251         return text_.editXY(cur, x, y);
252 }
253
254
255 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
256 {
257         lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
258                              << " [ cmd.action = "
259                              << cmd.action << ']' << endl;
260         text_.dispatch(cur, cmd);
261 }
262
263
264 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
265         FuncStatus & status) const
266 {
267         return text_.getStatus(cur, cmd, status);
268 }
269
270
271 void InsetText::setChange(Change const & change)
272 {
273         ParagraphList::iterator pit = paragraphs().begin();
274         ParagraphList::iterator end = paragraphs().end();
275         for (; pit != end; ++pit) {
276                 pit->setChange(change);
277         }
278 }
279
280
281 void InsetText::acceptChanges(BufferParams const & bparams)
282 {
283         text_.acceptChanges(bparams);
284 }
285
286
287 void InsetText::rejectChanges(BufferParams const & bparams)
288 {
289         text_.rejectChanges(bparams);
290 }
291
292
293 int InsetText::latex(Buffer const & buf, odocstream & os,
294                      OutputParams const & runparams) const
295 {
296         TexRow texrow;
297         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
298         return texrow.rows();
299 }
300
301
302 int InsetText::plaintext(Buffer const & buf, odocstream & os,
303                          OutputParams const & runparams) const
304 {
305         ParagraphList::const_iterator beg = paragraphs().begin();
306         ParagraphList::const_iterator end = paragraphs().end();
307         ParagraphList::const_iterator it = beg;
308         bool ref_printed = false;
309         int len = 0;
310         for (; it != end; ++it) {
311                 if (it != beg) {
312                         os << '\n';
313                         if (runparams.linelen > 0)
314                                 os << '\n';
315                 }
316                 odocstringstream oss;
317                 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
318                 docstring const str = oss.str();
319                 os << str;
320                 // FIXME: len is not computed fully correctly; in principle,
321                 // we have to count the characters after the last '\n'
322                 len = str.size();
323         }
324
325         return len;
326 }
327
328
329 int InsetText::docbook(Buffer const & buf, odocstream & os,
330                        OutputParams const & runparams) const
331 {
332         docbookParagraphs(paragraphs(), buf, os, runparams);
333         return 0;
334 }
335
336
337 void InsetText::validate(LaTeXFeatures & features) const
338 {
339         for_each(paragraphs().begin(), paragraphs().end(),
340                  bind(&Paragraph::validate, _1, ref(features)));
341 }
342
343
344 void InsetText::cursorPos(BufferView const & bv,
345                 CursorSlice const & sl, bool boundary, int & x, int & y) const
346 {
347         x = text_.cursorX(bv, sl, boundary) + border_;
348         y = text_.cursorY(bv, sl, boundary);
349 }
350
351
352 bool InsetText::showInsetDialog(BufferView *) const
353 {
354         return false;
355 }
356
357
358 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
359 {
360         clear();
361         Paragraph & first = paragraphs().front();
362         for (unsigned int i = 0; i < data.length(); ++i)
363                 first.insertChar(i, data[i], font, trackChanges);
364 }
365
366
367 void InsetText::setAutoBreakRows(bool flag)
368 {
369         if (flag == text_.autoBreakRows_)
370                 return;
371
372         text_.autoBreakRows_ = flag;
373         if (flag)
374                 return;
375
376         // remove previously existing newlines
377         ParagraphList::iterator it = paragraphs().begin();
378         ParagraphList::iterator end = paragraphs().end();
379         for (; it != end; ++it)
380                 for (int i = 0; i < it->size(); ++i)
381                         if (it->isNewline(i))
382                                 // do not track the change, because the user
383                                 // is not allowed to revert/reject it
384                                 it->eraseChar(i, false);
385 }
386
387
388 void InsetText::setDrawFrame(bool flag)
389 {
390         drawFrame_ = flag;
391 }
392
393
394 LColor_color InsetText::frameColor() const
395 {
396         return LColor::color(frame_color_);
397 }
398
399
400 void InsetText::setFrameColor(LColor_color col)
401 {
402         frame_color_ = col;
403 }
404
405
406 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
407 {
408         // There is little we can do here to keep track of changes.
409         // As of 2006/10/20, appendParagraphs is used exclusively by
410         // LyXTabular::setMultiColumn. In this context, the paragraph break
411         // is lost irreversibly and the appended text doesn't really change
412
413         ParagraphList & pl = paragraphs();
414
415         ParagraphList::iterator pit = plist.begin();
416         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
417         ++pit;
418         mergeParagraph(buffer->params(), pl,
419                        std::distance(pl.begin(), ins) - 1);
420
421         for_each(pit, plist.end(),
422                  bind(&ParagraphList::push_back, ref(pl), _1));
423 }
424
425
426 void InsetText::addPreview(PreviewLoader & loader) const
427 {
428         ParagraphList::const_iterator pit = paragraphs().begin();
429         ParagraphList::const_iterator pend = paragraphs().end();
430
431         for (; pit != pend; ++pit) {
432                 InsetList::const_iterator it  = pit->insetlist.begin();
433                 InsetList::const_iterator end = pit->insetlist.end();
434                 for (; it != end; ++it)
435                         it->inset->addPreview(loader);
436         }
437 }
438
439
440 //FIXME: instead of this hack, which only works by chance,
441 // cells should have their own insetcell type, which returns CELL_CODE!
442 bool InsetText::neverIndent(Buffer const & buffer) const
443 {
444         // this is only true for tabular cells
445         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
446 }
447
448
449 ParagraphList const & InsetText::paragraphs() const
450 {
451         return text_.paragraphs();
452 }
453
454
455 ParagraphList & InsetText::paragraphs()
456 {
457         return text_.paragraphs();
458 }
459
460
461 } // namespace lyx