]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
implement and use virtual bool neverIndent() (thereby fixing bug 2003)
[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_linuxdoc.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
41 #include "lyxrow.h"
42 #include "sgml.h"
43 #include "texrow.h"
44 #include "undo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/Painter.h"
49
50 #include "support/lyxalgo.h" // lyx::count
51
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
54
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         init();
84 }
85
86
87 InsetText::InsetText(InsetText const & in)
88         : InsetOld(in), text_(in.text_.bv_owner)
89 {
90         text_.autoBreakRows_ = in.text_.autoBreakRows_;
91         drawFrame_ = in.drawFrame_;
92         frame_color_ = in.frame_color_;
93         text_.paragraphs() = in.text_.paragraphs();
94         init();
95 }
96
97
98 InsetText::InsetText()
99         : text_(0)
100 {}
101
102
103 void InsetText::init()
104 {
105         for_each(paragraphs().begin(), paragraphs().end(),
106                  bind(&Paragraph::setInsetOwner, _1, this));
107         old_pit = -1;
108 }
109
110
111 void InsetText::markErased(bool erased)
112 {
113         ParagraphList & pars = paragraphs();
114         for_each(pars.begin(), pars.end(),
115                  bind(&Paragraph::markErased, _1, erased));
116 }
117
118
119 void InsetText::clear()
120 {
121         ParagraphList & pars = paragraphs();
122
123         // This is a gross hack...
124         LyXLayout_ptr old_layout = pars.begin()->layout();
125
126         pars.clear();
127         pars.push_back(Paragraph());
128         pars.begin()->setInsetOwner(this);
129         pars.begin()->layout(old_layout);
130 }
131
132
133 auto_ptr<InsetBase> InsetText::doClone() const
134 {
135         return auto_ptr<InsetBase>(new InsetText(*this));
136 }
137
138
139 void InsetText::write(Buffer const & buf, ostream & os) const
140 {
141         os << "Text\n";
142         text_.write(buf, os);
143 }
144
145
146 void InsetText::read(Buffer const & buf, LyXLex & lex)
147 {
148         clear();
149
150 #ifdef WITH_WARNINGS
151 #warning John, look here. Doesnt make much sense.
152 #endif
153         if (buf.params().tracking_changes)
154                 paragraphs().begin()->trackChanges();
155
156         // delete the initial paragraph
157         Paragraph oldpar = *paragraphs().begin();
158         paragraphs().clear();
159         bool res = text_.read(buf, lex);
160         init();
161
162         if (!res) {
163                 lex.printError("Missing \\end_inset at this point. "
164                                            "Read: `$$Token'");
165         }
166
167         // sanity check
168         // ensure we have at least one paragraph.
169         if (paragraphs().empty())
170                 paragraphs().push_back(oldpar);
171 }
172
173
174 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
175 {
176         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
177         setViewCache(mi.base.bv);
178         mi.base.textwidth -= 2 * border_;
179         font_ = mi.base.font;
180         text_.font_ = mi.base.font;
181         text_.metrics(mi, dim);
182         dim.asc += border_;
183         dim.des += border_;
184         dim.wid += 2 * border_;
185         mi.base.textwidth += 2 * border_;
186         dim_ = dim;
187 }
188
189
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 {
192         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
193         // update our idea of where we are
194         setPosCache(pi, x, y);
195
196         text_.draw(pi, x + border_, y);
197
198         if (drawFrame_) {
199                 int const w = text_.width() + 2 * border_;
200                 int const a = text_.ascent() + border_;
201                 int const h = a + text_.descent() + border_;
202                 int const ww = pi.base.bv->workWidth();
203                 if (w > ww - 40)  {
204                         pi.pain.line(0, y - a, ww, y - a, frameColor());
205                         pi.pain.line(0, y - a + h, ww, y - a + h, frameColor());
206                 } else {
207                         pi.pain.rectangle(x, y - a, w, h, frameColor());
208                 }
209         }
210 }
211
212
213 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
214 {
215         if (backgroundColor() != LColor::background) {
216                 // repaint the background if needed
217                 int const w = text_.width() + 2 * border_;
218                 int const a = text_.ascent() + border_;
219                 int const h = a + text_.descent() + border_;
220                 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
221         }
222         text_.drawSelection(pi, x, y);
223 }
224
225
226 string const InsetText::editMessage() const
227 {
228         return _("Opened Text Inset");
229 }
230
231
232 void InsetText::edit(LCursor & cur, bool left)
233 {
234         //lyxerr << "InsetText: edit left/right" << endl;
235         old_pit = -1;
236         setViewCache(&cur.bv());
237         int const pit = left ? 0 : paragraphs().size() - 1;
238         int const pos = left ? 0 : paragraphs().back().size();
239         text_.setCursor(cur.top(), pit, pos);
240         cur.clearSelection();
241         finishUndo();
242 }
243
244
245 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
246 {
247         old_pit = -1;
248         return text_.editXY(cur, x, y);
249 }
250
251
252 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
253 {
254         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
255     << " [ cmd.action = " << cmd.action << ']' << endl;
256         setViewCache(&cur.bv());
257         text_.dispatch(cur, cmd);
258 }
259
260
261 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
262         FuncStatus & status) const
263 {
264         return text_.getStatus(cur, cmd, status);
265 }
266
267
268 int InsetText::latex(Buffer const & buf, ostream & os,
269                      OutputParams const & runparams) const
270 {
271         TexRow texrow;
272         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
273         return texrow.rows();
274 }
275
276
277 int InsetText::plaintext(Buffer const & buf, ostream & os,
278                      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         for (; it != end; ++it)
285                 asciiParagraph(buf, *it, os, runparams, ref_printed);
286
287         // FIXME: Give the total numbers of lines
288         return 1;
289 }
290
291
292 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
293                         OutputParams const & runparams) const
294 {
295         linuxdocParagraphs(buf, paragraphs(), os, runparams);
296         return 0;
297 }
298
299
300 int InsetText::docbook(Buffer const & buf, ostream & os,
301                        OutputParams const & runparams) const
302 {
303         docbookParagraphs(paragraphs(), buf, os, runparams);
304         return 0;
305 }
306
307
308 void InsetText::validate(LaTeXFeatures & features) const
309 {
310         for_each(paragraphs().begin(), paragraphs().end(),
311                  bind(&Paragraph::validate, _1, ref(features)));
312 }
313
314
315 void InsetText::cursorPos
316         (CursorSlice const & sl, bool boundary, int & x, int & y) const
317 {
318         x = text_.cursorX(sl, boundary) + border_;
319         y = text_.cursorY(sl, boundary);
320 }
321
322
323 bool InsetText::showInsetDialog(BufferView *) const
324 {
325         return false;
326 }
327
328
329 void InsetText::getLabelList(Buffer const & buffer,
330                              std::vector<string> & list) const
331 {
332         ParagraphList::const_iterator pit = paragraphs().begin();
333         ParagraphList::const_iterator pend = paragraphs().end();
334         for (; pit != pend; ++pit) {
335                 InsetList::const_iterator beg = pit->insetlist.begin();
336                 InsetList::const_iterator end = pit->insetlist.end();
337                 for (; beg != end; ++beg)
338                         beg->inset->getLabelList(buffer, list);
339         }
340 }
341
342
343 void InsetText::markNew(bool track_changes)
344 {
345         ParagraphList::iterator pit = paragraphs().begin();
346         ParagraphList::iterator end = paragraphs().end();
347         for (; pit != end; ++pit) {
348                 if (track_changes)
349                         pit->trackChanges();
350                 else // no-op when not tracking
351                         pit->cleanChanges();
352         }
353 }
354
355
356 void InsetText::setText(string const & data, LyXFont const & font)
357 {
358         clear();
359         Paragraph & first = paragraphs().front();
360         for (unsigned int i = 0; i < data.length(); ++i)
361                 first.insertChar(i, data[i], font);
362 }
363
364
365 void InsetText::setAutoBreakRows(bool flag)
366 {
367         if (flag == text_.autoBreakRows_)
368                 return;
369
370         text_.autoBreakRows_ = flag;
371         if (flag)
372                 return;
373
374         // remove previously existing newlines
375         ParagraphList::iterator it = paragraphs().begin();
376         ParagraphList::iterator end = paragraphs().end();
377         for (; it != end; ++it)
378                 for (int i = 0; i < it->size(); ++i)
379                         if (it->isNewline(i))
380                                 it->erase(i);
381 }
382
383
384 void InsetText::setDrawFrame(bool flag)
385 {
386         drawFrame_ = flag;
387 }
388
389
390 LColor_color InsetText::frameColor() const
391 {
392         return LColor::color(frame_color_);
393 }
394
395
396 void InsetText::setFrameColor(LColor_color col)
397 {
398         frame_color_ = col;
399 }
400
401
402 void InsetText::setViewCache(BufferView const * bv) const
403 {
404         if (bv && bv != text_.bv_owner) {
405                 //lyxerr << "setting view cache from "
406                 //      << text_.bv_owner << " to " << bv << "\n";
407                 text_.bv_owner = const_cast<BufferView *>(bv);
408         }
409 }
410
411
412 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
413 {
414 #ifdef WITH_WARNINGS
415 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
416 // And it probably does. You have to take a look at this John. (Lgb)
417 #warning John, have a look here. (Lgb)
418 #endif
419         ParagraphList & pl = paragraphs();
420
421         ParagraphList::iterator pit = plist.begin();
422         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
423         ++pit;
424         mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
425
426         for_each(pit, plist.end(),
427                  bind(&ParagraphList::push_back, ref(pl), _1));
428 }
429
430
431 void InsetText::addPreview(PreviewLoader & loader) const
432 {
433         ParagraphList::const_iterator pit = paragraphs().begin();
434         ParagraphList::const_iterator pend = paragraphs().end();
435
436         for (; pit != pend; ++pit) {
437                 InsetList::const_iterator it  = pit->insetlist.begin();
438                 InsetList::const_iterator end = pit->insetlist.end();
439                 for (; it != end; ++it)
440                         it->inset->addPreview(loader);
441         }
442 }
443
444
445 //FIXME: instead of this hack, which only works by chance,
446 // cells should have their own insetcell type, which returns CELL_CODE!
447 bool InsetText::neverIndent() const
448 {
449         // this is only true for tabular cells
450         return !text_.isMainText() && lyxCode() == TEXT_CODE;
451 }
452
453
454 ParagraphList const & InsetText::paragraphs() const
455 {
456         return text_.paragraphs();
457 }
458
459
460 ParagraphList & InsetText::paragraphs()
461 {
462         return text_.paragraphs();
463 }