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