]> 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, bool trackChanges)
333 {
334         clear();
335         Paragraph & first = paragraphs().front();
336         for (unsigned int i = 0; i < data.length(); ++i)
337                 first.insertChar(i, data[i], font, trackChanges);
338 }
339
340
341 void InsetText::setAutoBreakRows(bool flag)
342 {
343         if (flag == text_.autoBreakRows_)
344                 return;
345
346         text_.autoBreakRows_ = flag;
347         if (flag)
348                 return;
349
350         // remove previously existing newlines
351         ParagraphList::iterator it = paragraphs().begin();
352         ParagraphList::iterator end = paragraphs().end();
353         for (; it != end; ++it)
354                 for (int i = 0; i < it->size(); ++i)
355                         if (it->isNewline(i))
356                                 // do not track the change, because the user
357                                 // is not allowed to revert/reject it
358                                 it->erase(i, false);
359 }
360
361
362 void InsetText::setDrawFrame(bool flag)
363 {
364         drawFrame_ = flag;
365 }
366
367
368 LColor_color InsetText::frameColor() const
369 {
370         return LColor::color(frame_color_);
371 }
372
373
374 void InsetText::setFrameColor(LColor_color col)
375 {
376         frame_color_ = col;
377 }
378
379
380 void InsetText::setViewCache(BufferView const * bv) const
381 {
382         if (bv && bv != text_.bv_owner) {
383                 //lyxerr << "setting view cache from "
384                 //      << text_.bv_owner << " to " << bv << "\n";
385                 text_.bv_owner = const_cast<BufferView *>(bv);
386         }
387 }
388
389
390 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
391 {
392 #ifdef WITH_WARNINGS
393 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
394 // And it probably does. You have to take a look at this John. (Lgb)
395 #warning John, have a look here. (Lgb)
396 #endif
397         ParagraphList & pl = paragraphs();
398
399         ParagraphList::iterator pit = plist.begin();
400         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
401         ++pit;
402         mergeParagraph(buffer->params(), pl,
403                        std::distance(pl.begin(), ins) - 1);
404
405         for_each(pit, plist.end(),
406                  bind(&ParagraphList::push_back, ref(pl), _1));
407 }
408
409
410 void InsetText::addPreview(PreviewLoader & loader) const
411 {
412         ParagraphList::const_iterator pit = paragraphs().begin();
413         ParagraphList::const_iterator pend = paragraphs().end();
414
415         for (; pit != pend; ++pit) {
416                 InsetList::const_iterator it  = pit->insetlist.begin();
417                 InsetList::const_iterator end = pit->insetlist.end();
418                 for (; it != end; ++it)
419                         it->inset->addPreview(loader);
420         }
421 }
422
423
424 //FIXME: instead of this hack, which only works by chance,
425 // cells should have their own insetcell type, which returns CELL_CODE!
426 bool InsetText::neverIndent() const
427 {
428         // this is only true for tabular cells
429         return !text_.isMainText() && lyxCode() == TEXT_CODE;
430 }
431
432
433 ParagraphList const & InsetText::paragraphs() const
434 {
435         return text_.paragraphs();
436 }
437
438
439 ParagraphList & InsetText::paragraphs()
440 {
441         return text_.paragraphs();
442 }