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