]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
lexer cosmetics
[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
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CompletionList.h"
20 #include "CoordCache.h"
21 #include "Cursor.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "InsetList.h"
27 #include "Intl.h"
28 #include "Lexer.h"
29 #include "lyxfind.h"
30 #include "LyXRC.h"
31 #include "MetricsInfo.h"
32 #include "output_docbook.h"
33 #include "output_latex.h"
34 #include "OutputParams.h"
35 #include "output_plaintext.h"
36 #include "paragraph_funcs.h"
37 #include "Paragraph.h"
38 #include "ParagraphParameters.h"
39 #include "ParIterator.h"
40 #include "Row.h"
41 #include "sgml.h"
42 #include "TexRow.h"
43 #include "TextClass.h"
44 #include "Text.h"
45 #include "TextMetrics.h"
46
47 #include "frontends/alert.h"
48 #include "frontends/Painter.h"
49
50 #include "support/debug.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
53
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
56
57 using namespace std;
58 using namespace lyx::support;
59
60 using boost::bind;
61 using boost::ref;
62
63 namespace lyx {
64
65 using graphics::PreviewLoader;
66
67
68 /////////////////////////////////////////////////////////////////////
69
70 InsetText::InsetText(Buffer const & buf)
71         : drawFrame_(false), frame_color_(Color_insetframe)
72 {
73         initParagraphs(buf);
74 }
75
76
77 InsetText::InsetText(InsetText const & in)
78         : Inset(in), text_()
79 {
80         text_.autoBreakRows_ = in.text_.autoBreakRows_;
81         drawFrame_ = in.drawFrame_;
82         frame_color_ = in.frame_color_;
83         text_.paragraphs() = in.text_.paragraphs();
84         setParagraphOwner();
85 }
86
87
88 InsetText::InsetText()
89 {}
90
91
92 void InsetText::initParagraphs(Buffer const & buf)
93 {
94         BOOST_ASSERT(paragraphs().empty());
95         buffer_ = const_cast<Buffer *>(&buf);
96         paragraphs().push_back(Paragraph());
97         Paragraph & ourpar = paragraphs().back();
98         ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
99         ourpar.setInsetOwner(this);
100 }
101
102 void InsetText::setParagraphOwner()
103 {
104         for_each(paragraphs().begin(), paragraphs().end(),
105                  bind(&Paragraph::setInsetOwner, _1, this));
106 }
107
108
109 void InsetText::clear()
110 {
111         ParagraphList & pars = paragraphs();
112         BOOST_ASSERT(!pars.empty());
113
114         // This is a gross hack...
115         Layout const & old_layout = pars.begin()->layout();
116
117         pars.clear();
118         pars.push_back(Paragraph());
119         pars.begin()->setInsetOwner(this);
120         pars.begin()->setLayout(old_layout);
121 }
122
123
124 Dimension const InsetText::dimension(BufferView const & bv) const
125 {
126         TextMetrics const & tm = bv.textMetrics(&text_);
127         Dimension dim = tm.dimension();
128         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
129         dim.des += TEXT_TO_INSET_OFFSET;
130         dim.asc += TEXT_TO_INSET_OFFSET;
131         return dim;
132 }
133
134
135 void InsetText::write(ostream & os) const
136 {
137         os << "Text\n";
138         text_.write(buffer(), os);
139 }
140
141
142 void InsetText::read(Lexer & lex)
143 {
144         clear();
145
146         // delete the initial paragraph
147         Paragraph oldpar = *paragraphs().begin();
148         paragraphs().clear();
149         ErrorList errorList;
150         lex.setContext("InsetText::read");
151         bool res = text_.read(buffer(), lex, errorList, this);
152
153         if (!res)
154                 lex.printError("Missing \\end_inset at this point. ");
155
156         // sanity check
157         // ensure we have at least one paragraph.
158         if (paragraphs().empty())
159                 paragraphs().push_back(oldpar);
160 }
161
162
163 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
164 {
165         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
166
167         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
168
169         // Hand font through to contained lyxtext:
170         tm.font_.fontInfo() = mi.base.font;
171         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
172         if (hasFixedWidth())
173                 tm.metrics(mi, dim, mi.base.textwidth);
174         else
175                 tm.metrics(mi, dim);
176         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
177         dim.asc += TEXT_TO_INSET_OFFSET;
178         dim.des += TEXT_TO_INSET_OFFSET;
179         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
180 }
181
182
183 void InsetText::draw(PainterInfo & pi, int x, int y) const
184 {
185         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
186
187         if (drawFrame_ || pi.full_repaint) {
188                 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
189                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
190                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
191                 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
192                 if (pi.full_repaint)
193                         pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
194                 if (drawFrame_)
195                         pi.pain.rectangle(xframe, yframe, w, h, frameColor());
196         }
197         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
198 }
199
200
201 docstring InsetText::editMessage() const
202 {
203         return _("Opened Text Inset");
204 }
205
206
207 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
208 {
209         pit_type const pit = front ? 0 : paragraphs().size() - 1;
210         pos_type pos = front ? 0 : paragraphs().back().size();
211
212         // if visual information is not to be ignored, move to extreme right/left
213         if (entry_from != ENTRY_DIRECTION_IGNORE) {
214                 Cursor temp_cur = cur;
215                 temp_cur.pit() = pit;
216                 temp_cur.pos() = pos;
217                 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
218                 pos = temp_cur.pos();
219         }
220
221         text_.setCursor(cur.top(), pit, pos);
222         cur.clearSelection();
223         cur.finishUndo();
224 }
225
226
227 Inset * InsetText::editXY(Cursor & cur, int x, int y)
228 {
229         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
230 }
231
232
233 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
234 {
235         LYXERR(Debug::ACTION, "InsetText::doDispatch()"
236                 << " [ cmd.action = " << cmd.action << ']');
237         text_.dispatch(cur, cmd);
238 }
239
240
241 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
242         FuncStatus & status) const
243 {
244         return text_.getStatus(cur, cmd, status);
245 }
246
247
248 void InsetText::setChange(Change const & change)
249 {
250         ParagraphList::iterator pit = paragraphs().begin();
251         ParagraphList::iterator end = paragraphs().end();
252         for (; pit != end; ++pit) {
253                 pit->setChange(change);
254         }
255 }
256
257
258 void InsetText::acceptChanges(BufferParams const & bparams)
259 {
260         text_.acceptChanges(bparams);
261 }
262
263
264 void InsetText::rejectChanges(BufferParams const & bparams)
265 {
266         text_.rejectChanges(bparams);
267 }
268
269
270 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
271 {
272         TexRow texrow;
273         latexParagraphs(buffer(), text_, os, texrow, runparams);
274         return texrow.rows();
275 }
276
277
278 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
279 {
280         ParagraphList::const_iterator beg = paragraphs().begin();
281         ParagraphList::const_iterator end = paragraphs().end();
282         ParagraphList::const_iterator it = beg;
283         bool ref_printed = false;
284         int len = 0;
285         for (; it != end; ++it) {
286                 if (it != beg) {
287                         os << '\n';
288                         if (runparams.linelen > 0)
289                                 os << '\n';
290                 }
291                 odocstringstream oss;
292                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
293                 docstring const str = oss.str();
294                 os << str;
295                 // FIXME: len is not computed fully correctly; in principle,
296                 // we have to count the characters after the last '\n'
297                 len = str.size();
298         }
299
300         return len;
301 }
302
303
304 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
305 {
306         docbookParagraphs(paragraphs(), buffer(), os, runparams);
307         return 0;
308 }
309
310
311 void InsetText::validate(LaTeXFeatures & features) const
312 {
313         for_each(paragraphs().begin(), paragraphs().end(),
314                  bind(&Paragraph::validate, _1, ref(features)));
315 }
316
317
318 void InsetText::cursorPos(BufferView const & bv,
319                 CursorSlice const & sl, bool boundary, int & x, int & y) const
320 {
321         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
322         y = bv.textMetrics(&text_).cursorY(sl, boundary);
323 }
324
325
326 bool InsetText::showInsetDialog(BufferView *) const
327 {
328         return false;
329 }
330
331
332 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
333 {
334         clear();
335         Paragraph & first = paragraphs().front();
336         for (unsigned int i = 0; i < data.length(); ++i)
337                 first.insertChar(i, data[i], font, trackChanges);
338 }
339
340
341 void InsetText::setAutoBreakRows(bool flag)
342 {
343         if (flag == text_.autoBreakRows_)
344                 return;
345
346         text_.autoBreakRows_ = flag;
347         if (flag)
348                 return;
349
350         // remove previously existing newlines
351         ParagraphList::iterator it = paragraphs().begin();
352         ParagraphList::iterator end = paragraphs().end();
353         for (; it != end; ++it)
354                 for (int i = 0; i < it->size(); ++i)
355                         if (it->isNewline(i))
356                                 // do not track the change, because the user
357                                 // is not allowed to revert/reject it
358                                 it->eraseChar(i, false);
359 }
360
361
362 void InsetText::setDrawFrame(bool flag)
363 {
364         drawFrame_ = flag;
365 }
366
367
368 ColorCode InsetText::frameColor() const
369 {
370         return frame_color_;
371 }
372
373
374 void InsetText::setFrameColor(ColorCode col)
375 {
376         frame_color_ = col;
377 }
378
379
380 void InsetText::appendParagraphs(ParagraphList & plist)
381 {
382         // There is little we can do here to keep track of changes.
383         // As of 2006/10/20, appendParagraphs is used exclusively by
384         // LyXTabular::setMultiColumn. In this context, the paragraph break
385         // is lost irreversibly and the appended text doesn't really change
386
387         ParagraphList & pl = paragraphs();
388
389         ParagraphList::iterator pit = plist.begin();
390         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
391         ++pit;
392         mergeParagraph(buffer().params(), pl,
393                        distance(pl.begin(), ins) - 1);
394
395         for_each(pit, plist.end(),
396                  bind(&ParagraphList::push_back, ref(pl), _1));
397 }
398
399
400 void InsetText::addPreview(PreviewLoader & loader) const
401 {
402         ParagraphList::const_iterator pit = paragraphs().begin();
403         ParagraphList::const_iterator pend = paragraphs().end();
404
405         for (; pit != pend; ++pit) {
406                 InsetList::const_iterator it  = pit->insetList().begin();
407                 InsetList::const_iterator end = pit->insetList().end();
408                 for (; it != end; ++it)
409                         it->inset->addPreview(loader);
410         }
411 }
412
413
414 ParagraphList const & InsetText::paragraphs() const
415 {
416         return text_.paragraphs();
417 }
418
419
420 ParagraphList & InsetText::paragraphs()
421 {
422         return text_.paragraphs();
423 }
424
425
426 //void InsetInclude::addToToc(ParConstIterator const & cpit) const
427 //{
428 //}
429
430
431 void InsetText::updateLabels(ParIterator const & it)
432 {
433         ParIterator it2 = it;
434         it2.forwardPos();
435         BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
436         lyx::updateLabels(buffer(), it2);
437 }
438
439
440 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
441 {
442         if (cur.buffer().isClean())
443                 return Inset::notifyCursorLeaves(old, cur);
444         
445         // find text inset in old cursor
446         Cursor insetCur = old;
447         int scriptSlice = insetCur.find(this);
448         BOOST_ASSERT(scriptSlice != -1);
449         insetCur.cutOff(scriptSlice);
450         BOOST_ASSERT(&insetCur.inset() == this);
451         
452         // update the old paragraph's words
453         insetCur.paragraph().updateWords(insetCur.top());
454         
455         return Inset::notifyCursorLeaves(old, cur);
456 }
457
458
459 bool InsetText::completionSupported(Cursor const & cur) const
460 {
461         Cursor const & bvCur = cur.bv().cursor();
462         if (&bvCur.inset() != this)
463                 return false;
464         return text_.completionSupported(cur);
465 }
466
467
468 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
469 {
470         return completionSupported(cur);
471 }
472
473
474 bool InsetText::automaticInlineCompletion() const
475 {
476         return lyxrc.completion_inline_text;
477 }
478
479
480 bool InsetText::automaticPopupCompletion() const
481 {
482         return lyxrc.completion_popup_text;
483 }
484
485
486 bool InsetText::showCompletionCursor() const
487 {
488         return lyxrc.completion_cursor_text;
489 }
490
491
492 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
493 {
494         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
495 }
496
497
498 docstring InsetText::completionPrefix(Cursor const & cur) const
499 {
500         if (!completionSupported(cur))
501                 return docstring();
502         return text_.completionPrefix(cur);
503 }
504
505
506 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
507         bool finished)
508 {
509         if (!completionSupported(cur))
510                 return false;
511
512         return text_.insertCompletion(cur, s, finished);
513 }
514
515
516 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
517         Dimension & dim) const
518 {
519         TextMetrics const & tm = cur.bv().textMetrics(&text_);
520         tm.completionPosAndDim(cur, x, y, dim);
521 }
522
523
524 docstring InsetText::contextMenu(BufferView const &, int, int) const
525 {
526         return from_ascii("context-edit");
527 }
528
529
530 } // namespace lyx