]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Fix return value of InsetText::plaintext
[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/font_metrics.h"
47 #include "frontends/Painter.h"
48
49 #include "support/lyxalgo.h" // lyx::count
50
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
53
54 using lyx::docstring;
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60
61 using boost::bind;
62 using boost::ref;
63
64 using std::endl;
65 using std::for_each;
66 using std::max;
67 using std::string;
68 using std::auto_ptr;
69 using std::ostream;
70 using std::vector;
71
72
73 int InsetText::border_ = 2;
74
75
76 InsetText::InsetText(BufferParams const & bp)
77         : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
78 {
79         paragraphs().push_back(Paragraph());
80         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
81         if (bp.tracking_changes)
82                 paragraphs().back().trackChanges();
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         ParagraphList & pars = paragraphs();
120         for_each(pars.begin(), pars.end(),
121                  bind(&Paragraph::markErased, _1, erased));
122 }
123
124
125 void InsetText::clear()
126 {
127         ParagraphList & pars = paragraphs();
128
129         // This is a gross hack...
130         LyXLayout_ptr old_layout = pars.begin()->layout();
131
132         pars.clear();
133         pars.push_back(Paragraph());
134         pars.begin()->setInsetOwner(this);
135         pars.begin()->layout(old_layout);
136 }
137
138
139 auto_ptr<InsetBase> InsetText::doClone() const
140 {
141         return auto_ptr<InsetBase>(new InsetText(*this));
142 }
143
144
145 void InsetText::write(Buffer const & buf, ostream & os) const
146 {
147         os << "Text\n";
148         text_.write(buf, os);
149 }
150
151
152 void InsetText::read(Buffer const & buf, LyXLex & lex)
153 {
154         clear();
155
156 #ifdef WITH_WARNINGS
157 #warning John, look here. Doesnt make much sense.
158 #endif
159         if (buf.params().tracking_changes)
160                 paragraphs().begin()->trackChanges();
161
162         // delete the initial paragraph
163         Paragraph oldpar = *paragraphs().begin();
164         paragraphs().clear();
165         ErrorList errorList;
166         bool res = text_.read(buf, lex, errorList);
167         init();
168
169         if (!res) {
170                 lex.printError("Missing \\end_inset at this point. "
171                                            "Read: `$$Token'");
172         }
173
174         // sanity check
175         // ensure we have at least one paragraph.
176         if (paragraphs().empty())
177                 paragraphs().push_back(oldpar);
178 }
179
180
181 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
182 {
183         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
184         setViewCache(mi.base.bv);
185         mi.base.textwidth -= 2 * border_;
186         font_ = mi.base.font;
187         // Hand font through to contained lyxtext:
188         text_.font_ = mi.base.font;
189         text_.metrics(mi, dim);
190         dim.asc += border_;
191         dim.des += border_;
192         dim.wid += 2 * border_;
193         mi.base.textwidth += 2 * border_;
194         dim_ = dim;
195 }
196
197
198 void InsetText::draw(PainterInfo & pi, int x, int y) const
199 {
200         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
201         // update our idea of where we are
202         setPosCache(pi, x, y);
203
204         text_.background_color_ = backgroundColor();
205         text_.draw(pi, x + border_, y);
206
207         if (drawFrame_) {
208                 int const w = text_.width() + 2 * border_;
209                 int const a = text_.ascent() + border_;
210                 int const h = a + text_.descent() + border_;
211                 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
212                         frameColor());
213         }
214 }
215
216
217 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
218 {
219         int const w = text_.width() + 2 * border_;
220         int const a = text_.ascent() + border_;
221         int const h = a + text_.descent() + border_;
222         pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
223                 backgroundColor());
224         text_.drawSelection(pi, x, y);
225 }
226
227
228 docstring const InsetText::editMessage() const
229 {
230         return _("Opened Text Inset");
231 }
232
233
234 void InsetText::edit(LCursor & cur, bool left)
235 {
236         //lyxerr << "InsetText: edit left/right" << endl;
237         setViewCache(&cur.bv());
238         int const pit = left ? 0 : paragraphs().size() - 1;
239         int const pos = left ? 0 : paragraphs().back().size();
240         text_.setCursor(cur.top(), pit, pos);
241         cur.clearSelection();
242         finishUndo();
243 }
244
245
246 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
247 {
248         return text_.editXY(cur, x, y);
249 }
250
251
252 void InsetText::forceParagraphsToDefault(LCursor & cur)
253 {
254         BufferParams const & bp = cur.buffer().params();
255         LyXLayout_ptr const layout =
256                 bp.getLyXTextClass().defaultLayout();
257         ParagraphList::iterator const end = paragraphs().end();
258         for (ParagraphList::iterator par = paragraphs().begin();
259                         par != end; ++par)
260                 par->layout(layout);
261 }
262
263
264 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
265 {
266         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
267                              << " [ cmd.action = "
268                              << cmd.action << ']' << endl;
269         setViewCache(&cur.bv());
270         text_.dispatch(cur, cmd);
271 }
272
273
274 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
275         FuncStatus & status) const
276 {
277         return text_.getStatus(cur, cmd, status);
278 }
279
280
281 int InsetText::latex(Buffer const & buf, ostream & os,
282                      OutputParams const & runparams) const
283 {
284         TexRow texrow;
285         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
286         return texrow.rows();
287 }
288
289
290 int InsetText::plaintext(Buffer const & buf, ostream & os,
291                      OutputParams const & runparams) const
292 {
293         ParagraphList::const_iterator beg = paragraphs().begin();
294         ParagraphList::const_iterator end = paragraphs().end();
295         ParagraphList::const_iterator it = beg;
296         bool ref_printed = false;
297         std::ostringstream oss;
298         for (; it != end; ++it)
299                 asciiParagraph(buf, *it, oss, runparams, ref_printed);
300
301         string const str = oss.str();
302         os << str;
303         // Return how many newlines we issued.
304         return int(lyx::count(str.begin(), str.end(), '\n'));
305 }
306
307
308 int InsetText::docbook(Buffer const & buf, ostream & os,
309                        OutputParams const & runparams) const
310 {
311         docbookParagraphs(paragraphs(), buf, os, runparams);
312         return 0;
313 }
314
315
316 void InsetText::validate(LaTeXFeatures & features) const
317 {
318         for_each(paragraphs().begin(), paragraphs().end(),
319                  bind(&Paragraph::validate, _1, ref(features)));
320 }
321
322
323 void InsetText::cursorPos
324         (CursorSlice const & sl, bool boundary, int & x, int & y) const
325 {
326         x = text_.cursorX(sl, boundary) + border_;
327         y = text_.cursorY(sl, boundary);
328 }
329
330
331 bool InsetText::showInsetDialog(BufferView *) const
332 {
333         return false;
334 }
335
336
337 void InsetText::markNew(bool track_changes)
338 {
339         ParagraphList::iterator pit = paragraphs().begin();
340         ParagraphList::iterator end = paragraphs().end();
341         for (; pit != end; ++pit) {
342                 if (track_changes)
343                         pit->trackChanges();
344                 else // no-op when not tracking
345                         pit->cleanChanges();
346         }
347 }
348
349
350 void InsetText::setText(docstring const & data, LyXFont const & font)
351 {
352         clear();
353         Paragraph & first = paragraphs().front();
354         for (unsigned int i = 0; i < data.length(); ++i)
355                 first.insertChar(i, data[i], font);
356 }
357
358
359 void InsetText::setAutoBreakRows(bool flag)
360 {
361         if (flag == text_.autoBreakRows_)
362                 return;
363
364         text_.autoBreakRows_ = flag;
365         if (flag)
366                 return;
367
368         // remove previously existing newlines
369         ParagraphList::iterator it = paragraphs().begin();
370         ParagraphList::iterator end = paragraphs().end();
371         for (; it != end; ++it)
372                 for (int i = 0; i < it->size(); ++i)
373                         if (it->isNewline(i))
374                                 it->erase(i);
375 }
376
377
378 void InsetText::setDrawFrame(bool flag)
379 {
380         drawFrame_ = flag;
381 }
382
383
384 LColor_color InsetText::frameColor() const
385 {
386         return LColor::color(frame_color_);
387 }
388
389
390 void InsetText::setFrameColor(LColor_color col)
391 {
392         frame_color_ = col;
393 }
394
395
396 void InsetText::setViewCache(BufferView const * bv) const
397 {
398         if (bv && bv != text_.bv_owner) {
399                 //lyxerr << "setting view cache from "
400                 //      << text_.bv_owner << " to " << bv << "\n";
401                 text_.bv_owner = const_cast<BufferView *>(bv);
402         }
403 }
404
405
406 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
407 {
408 #ifdef WITH_WARNINGS
409 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
410 // And it probably does. You have to take a look at this John. (Lgb)
411 #warning John, have a look here. (Lgb)
412 #endif
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() const
443 {
444         // this is only true for tabular cells
445         return !text_.isMainText() && 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 }