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