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