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