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