]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
7ade80ae4c8493620d3b6153be687dd15af5d349
[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 "CutAndPaste.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "LColor.h"
28 #include "lyxfind.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_plaintext.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "rowpainter.h"
40 #include "lyxrow.h"
41 #include "sgml.h"
42 #include "texrow.h"
43 #include "undo.h"
44
45 #include "frontends/Alert.h"
46 #include "frontends/Painter.h"
47
48 #include "support/lyxalgo.h" // lyx::count
49
50 #include <boost/bind.hpp>
51 #include <boost/current_function.hpp>
52
53 #include <sstream>
54
55 using lyx::docstring;
56 using lyx::odocstream;
57 using lyx::pos_type;
58
59 using lyx::graphics::PreviewLoader;
60
61 using lyx::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::markErased(bool erased)
118 {
119         // FIXME: change tracking (MG)
120         ParagraphList & pars = paragraphs();
121         for_each(pars.begin(), pars.end(),
122                  bind(&Paragraph::setChange, _1, Change(erased ? Change::DELETED : Change::UNCHANGED)));
123 }
124
125
126 void InsetText::clear()
127 {
128         ParagraphList & pars = paragraphs();
129
130         // This is a gross hack...
131         LyXLayout_ptr old_layout = pars.begin()->layout();
132
133         pars.clear();
134         pars.push_back(Paragraph());
135         pars.begin()->setInsetOwner(this);
136         pars.begin()->layout(old_layout);
137 }
138
139
140 auto_ptr<InsetBase> InsetText::doClone() const
141 {
142         return auto_ptr<InsetBase>(new InsetText(*this));
143 }
144
145
146 void InsetText::write(Buffer const & buf, ostream & os) const
147 {
148         os << "Text\n";
149         text_.write(buf, os);
150 }
151
152
153 void InsetText::read(Buffer const & buf, LyXLex & lex)
154 {
155         clear();
156
157         // delete the initial paragraph
158         Paragraph oldpar = *paragraphs().begin();
159         paragraphs().clear();
160         ErrorList errorList;
161         bool res = text_.read(buf, lex, errorList);
162         init();
163
164         if (!res) {
165                 lex.printError("Missing \\end_inset at this point. "
166                                            "Read: `$$Token'");
167         }
168
169         // sanity check
170         // ensure we have at least one paragraph.
171         if (paragraphs().empty())
172                 paragraphs().push_back(oldpar);
173 }
174
175
176 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
177 {
178         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
179         setViewCache(mi.base.bv);
180         mi.base.textwidth -= 2 * border_;
181         font_ = mi.base.font;
182         // Hand font through to contained lyxtext:
183         text_.font_ = mi.base.font;
184         text_.metrics(mi, dim);
185         dim.asc += border_;
186         dim.des += border_;
187         dim.wid += 2 * border_;
188         mi.base.textwidth += 2 * border_;
189         dim_ = dim;
190 }
191
192
193 void InsetText::draw(PainterInfo & pi, int x, int y) const
194 {
195         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
196         // update our idea of where we are
197         setPosCache(pi, x, y);
198
199         text_.background_color_ = backgroundColor();
200         text_.draw(pi, x + border_, y);
201
202         if (drawFrame_) {
203                 int const w = text_.width() + 2 * border_;
204                 int const a = text_.ascent() + border_;
205                 int const h = a + text_.descent() + border_;
206                 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
207                         frameColor());
208         }
209 }
210
211
212 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
213 {
214         int const w = text_.width() + 2 * border_;
215         int const a = text_.ascent() + border_;
216         int const h = a + text_.descent() + border_;
217         pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
218                 backgroundColor());
219         text_.drawSelection(pi, x, y);
220 }
221
222
223 docstring const InsetText::editMessage() const
224 {
225         return _("Opened Text Inset");
226 }
227
228
229 void InsetText::edit(LCursor & cur, bool left)
230 {
231         //lyxerr << "InsetText: edit left/right" << endl;
232         setViewCache(&cur.bv());
233         int const pit = left ? 0 : paragraphs().size() - 1;
234         int const pos = left ? 0 : paragraphs().back().size();
235         text_.setCursor(cur.top(), pit, pos);
236         cur.clearSelection();
237         finishUndo();
238 }
239
240
241 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
242 {
243         return text_.editXY(cur, x, y);
244 }
245
246
247 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
248 {
249         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
250                              << " [ cmd.action = "
251                              << cmd.action << ']' << endl;
252         setViewCache(&cur.bv());
253         text_.dispatch(cur, cmd);
254 }
255
256
257 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
258         FuncStatus & status) const
259 {
260         return text_.getStatus(cur, cmd, status);
261 }
262
263
264 int InsetText::latex(Buffer const & buf, odocstream & os,
265                      OutputParams const & runparams) const
266 {
267         TexRow texrow;
268         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
269         return texrow.rows();
270 }
271
272
273 int InsetText::plaintext(Buffer const & buf, odocstream & os,
274                      OutputParams const & runparams) const
275 {
276         ParagraphList::const_iterator beg = paragraphs().begin();
277         ParagraphList::const_iterator end = paragraphs().end();
278         ParagraphList::const_iterator it = beg;
279         bool ref_printed = false;
280         lyx::odocstringstream oss;
281         for (; it != end; ++it)
282                 asciiParagraph(buf, *it, oss, runparams, ref_printed);
283
284         docstring const str = oss.str();
285         os << str;
286         // Return how many newlines we issued.
287         return int(lyx::count(str.begin(), str.end(), '\n'));
288 }
289
290
291 int InsetText::docbook(Buffer const & buf, ostream & os,
292                        OutputParams const & runparams) const
293 {
294         docbookParagraphs(paragraphs(), buf, os, runparams);
295         return 0;
296 }
297
298
299 void InsetText::validate(LaTeXFeatures & features) const
300 {
301         for_each(paragraphs().begin(), paragraphs().end(),
302                  bind(&Paragraph::validate, _1, ref(features)));
303 }
304
305
306 void InsetText::cursorPos(BufferView const & bv,
307                 CursorSlice const & sl, bool boundary, int & x, int & y) const
308 {
309         x = text_.cursorX(sl, boundary) + border_;
310         y = text_.cursorY(sl, boundary);
311 }
312
313
314 bool InsetText::showInsetDialog(BufferView *) const
315 {
316         return false;
317 }
318
319
320 void InsetText::markNew(bool track_changes)
321 {
322         ParagraphList::iterator pit = paragraphs().begin();
323         ParagraphList::iterator end = paragraphs().end();
324         for (; pit != end; ++pit) {
325                 // FIXME: change tracking (MG)
326                 // if (track_changes)
327                 //   set pit's text to UNCHANGED
328                 // else
329                 //   set pit's text to INSERTED in CT mode; reset CT info otherwise
330         }
331 }
332
333
334 void InsetText::setText(docstring const & data, LyXFont const & font)
335 {
336         clear();
337         Paragraph & first = paragraphs().front();
338         for (unsigned int i = 0; i < data.length(); ++i)
339                 // FIXME: change tracking (MG)
340                 first.insertChar(i, data[i], font, Change(Change::INSERTED));
341 }
342
343
344 void InsetText::setAutoBreakRows(bool flag)
345 {
346         if (flag == text_.autoBreakRows_)
347                 return;
348
349         text_.autoBreakRows_ = flag;
350         if (flag)
351                 return;
352
353         // remove previously existing newlines
354         ParagraphList::iterator it = paragraphs().begin();
355         ParagraphList::iterator end = paragraphs().end();
356         for (; it != end; ++it)
357                 for (int i = 0; i < it->size(); ++i)
358                         if (it->isNewline(i))
359                                 // do not track the change, because the user
360                                 // is not allowed to revert/reject it
361                                 it->erase(i, false);
362 }
363
364
365 void InsetText::setDrawFrame(bool flag)
366 {
367         drawFrame_ = flag;
368 }
369
370
371 LColor_color InsetText::frameColor() const
372 {
373         return LColor::color(frame_color_);
374 }
375
376
377 void InsetText::setFrameColor(LColor_color col)
378 {
379         frame_color_ = col;
380 }
381
382
383 void InsetText::setViewCache(BufferView const * bv) const
384 {
385         if (bv && bv != text_.bv_owner) {
386                 //lyxerr << "setting view cache from "
387                 //      << text_.bv_owner << " to " << bv << "\n";
388                 text_.bv_owner = const_cast<BufferView *>(bv);
389         }
390 }
391
392
393 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
394 {
395 #ifdef WITH_WARNINGS
396 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
397 // And it probably does. You have to take a look at this John. (Lgb)
398 #warning John, have a look here. (Lgb)
399 #endif
400         ParagraphList & pl = paragraphs();
401
402         ParagraphList::iterator pit = plist.begin();
403         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
404         ++pit;
405         mergeParagraph(buffer->params(), pl,
406                        std::distance(pl.begin(), ins) - 1);
407
408         for_each(pit, plist.end(),
409                  bind(&ParagraphList::push_back, ref(pl), _1));
410 }
411
412
413 void InsetText::addPreview(PreviewLoader & loader) const
414 {
415         ParagraphList::const_iterator pit = paragraphs().begin();
416         ParagraphList::const_iterator pend = paragraphs().end();
417
418         for (; pit != pend; ++pit) {
419                 InsetList::const_iterator it  = pit->insetlist.begin();
420                 InsetList::const_iterator end = pit->insetlist.end();
421                 for (; it != end; ++it)
422                         it->inset->addPreview(loader);
423         }
424 }
425
426
427 //FIXME: instead of this hack, which only works by chance,
428 // cells should have their own insetcell type, which returns CELL_CODE!
429 bool InsetText::neverIndent() const
430 {
431         // this is only true for tabular cells
432         return !text_.isMainText() && lyxCode() == TEXT_CODE;
433 }
434
435
436 ParagraphList const & InsetText::paragraphs() const
437 {
438         return text_.paragraphs();
439 }
440
441
442 ParagraphList & InsetText::paragraphs()
443 {
444         return text_.paragraphs();
445 }