]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Reorganized docbook export code and fixed several bugs in the process.
[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_linuxdoc.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/font_metrics.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
50
51 #include "support/lyxalgo.h" // lyx::count
52
53 #include <boost/bind.hpp>
54
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
61
62 using std::endl;
63 using std::for_each;
64 using std::max;
65 using std::string;
66 using std::auto_ptr;
67 using std::ostream;
68 using std::vector;
69
70
71 InsetText::InsetText(BufferParams const & bp)
72         : autoBreakRows_(false), drawFrame_(false),
73           frame_color_(LColor::insetframe), text_(0)
74 {
75         paragraphs().push_back(Paragraph());
76         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
77         if (bp.tracking_changes)
78                 paragraphs().back().trackChanges();
79         init();
80 }
81
82
83 InsetText::InsetText(InsetText const & in)
84         : UpdatableInset(in), text_(in.text_.bv_owner)
85 {
86         // this is ugly...
87         operator=(in);
88 }
89
90
91 InsetText::InsetText() : text_(0)
92 {}
93
94
95 void InsetText::operator=(InsetText const & in)
96 {
97         UpdatableInset::operator=(in);
98         autoBreakRows_ = in.autoBreakRows_;
99         drawFrame_ = in.drawFrame_;
100         frame_color_ = in.frame_color_;
101         text_ = LyXText(in.text_.bv_owner);
102         text_.paragraphs() = in.text_.paragraphs();
103         init();
104 }
105
106
107 void InsetText::init()
108 {
109         ParagraphList::iterator pit = paragraphs().begin();
110         ParagraphList::iterator end = paragraphs().end();
111         for (; pit != end; ++pit)
112                 pit->setInsetOwner(this);
113         old_par = -1;
114 }
115
116
117 void InsetText::clear(bool just_mark_erased)
118 {
119         ParagraphList & pars = paragraphs();
120         if (just_mark_erased) {
121                 ParagraphList::iterator it = pars.begin();
122                 ParagraphList::iterator end = pars.end();
123                 for (; it != end; ++it)
124                         it->markErased();
125                 return;
126         }
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::clone() 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(false);
154
155 #ifdef WITH_WARNINGS
156 #warning John, look here. Doesnt make much sense.
157 #endif
158         if (buf.params().tracking_changes)
159                 paragraphs().begin()->trackChanges();
160
161         // delete the initial paragraph
162         Paragraph oldpar = *paragraphs().begin();
163         paragraphs().clear();
164         bool res = text_.read(buf, lex);
165         init();
166
167         if (!res) {
168                 lex.printError("Missing \\end_inset at this point. "
169                                            "Read: `$$Token'");
170         }
171
172         // sanity check
173         // ensure we have at least one par.
174         if (paragraphs().empty())
175                 paragraphs().push_back(oldpar);
176 }
177
178
179 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
180 {
181         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
182         setViewCache(mi.base.bv);
183         text_.metrics(mi, dim);
184         dim_ = dim;
185         font_ = mi.base.font;
186         text_.font_ = mi.base.font;
187 }
188
189
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 {
192         BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
193         // update our idea of where we are
194         setPosCache(pi, x, y);
195
196         BufferView * bv = pi.base.bv;
197         bv->hideCursor();
198
199         x += scroll();
200         y -= text_.ascent();
201
202         // repaint the background if needed
203         if (backgroundColor() != LColor::background)
204                 clearInset(pi.pain, x, y);
205
206         text_.draw(pi, x, y + bv->top_y());
207
208         if (drawFrame_)
209                 drawFrame(pi.pain, x, y);
210 }
211
212
213 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
214 {
215         text_.drawSelection(pi, x, y);
216 }
217
218
219 void InsetText::drawFrame(Painter & pain, int x, int y) const
220 {
221         int const w = text_.width();
222         int const h = text_.height();
223         pain.rectangle(x, y, w, h, frameColor());
224 }
225
226
227 void InsetText::clearInset(Painter & pain, int x, int y) const
228 {
229         int const w = text_.width();
230         int const h = text_.height();
231         pain.fillRectangle(x, y, w, h, backgroundColor());
232 }
233
234
235 void InsetText::updateLocal(LCursor & cur)
236 {
237         if (!autoBreakRows_ && paragraphs().size() > 1) {
238                 // collapseParagraphs
239                 while (paragraphs().size() > 1) {
240                         ParagraphList::iterator const first = paragraphs().begin();
241                         ParagraphList::iterator second = first;
242                         ++second;
243                         size_t const first_par_size = first->size();
244
245                         if (!first->empty() &&
246                                         !second->empty() &&
247                                         !first->isSeparator(first_par_size - 1)) {
248                                 first->insertChar(first_par_size, ' ');
249                         }
250
251                         cur.clearSelection();
252                         mergeParagraph(cur.buffer().params(), paragraphs(), 0);
253                 }
254         }
255
256         if (!cur.selection())
257                 cur.resetAnchor();
258
259         LyXView * lv = cur.bv().owner();
260         lv->view_state_changed();
261         lv->updateMenubar();
262         lv->updateToolbars();
263         if (old_par != cur.par()) {
264                 lv->setLayout(text_.getPar(cur.par()).layout()->name());
265                 old_par = cur.par();
266         }
267 }
268
269
270 string const InsetText::editMessage() const
271 {
272         return _("Opened Text Inset");
273 }
274
275
276 void InsetText::edit(LCursor & cur, bool left)
277 {
278         //lyxerr << "InsetText: edit left/right" << endl;
279         old_par = -1;
280         setViewCache(&cur.bv());
281         int const par = left ? 0 : paragraphs().size() - 1;
282         int const pos = left ? 0 : paragraphs().back().size();
283         text_.setCursor(cur.top(), par, pos);
284         cur.clearSelection();
285         finishUndo();
286 #ifdef WITH_WARNINGS
287 #warning can someone check if/when this is needed?
288 #endif
289 //Andre?
290 //      updateLocal(cur);
291 }
292
293
294 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
295 {
296         old_par = -1;
297         return text_.editXY(cur, x, y);
298         //sanitizeEmptyText(cur.bv());
299         //updateLocal(cur);
300 }
301
302
303 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
304 {
305         //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
306         setViewCache(&cur.bv());
307
308         bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
309         text_.dispatch(cur, cmd);
310
311 }
312
313
314 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
315         FuncStatus & status) const
316 {
317         return text_.getStatus(cur, cmd, status);
318 }
319
320
321 int InsetText::latex(Buffer const & buf, ostream & os,
322                      OutputParams const & runparams) const
323 {
324         TexRow texrow;
325         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
326         return texrow.rows();
327 }
328
329
330 int InsetText::plaintext(Buffer const & buf, ostream & os,
331                      OutputParams const & runparams) const
332 {
333         ParagraphList::const_iterator beg = paragraphs().begin();
334         ParagraphList::const_iterator end = paragraphs().end();
335         ParagraphList::const_iterator it = beg;
336         for (; it != end; ++it)
337                 asciiParagraph(buf, *it, os, runparams, it == beg);
338
339         //FIXME: Give the total numbers of lines
340         return 0;
341 }
342
343
344 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
345                         OutputParams const & runparams) const
346 {
347         linuxdocParagraphs(buf, paragraphs(), os, runparams);
348         return 0;
349 }
350
351
352 int InsetText::docbook(Buffer const & buf, ostream & os,
353                        OutputParams const & runparams) const
354 {
355         docbookParagraphs(paragraphs(), buf, os, runparams);
356         return 0;
357 }
358
359
360 void InsetText::validate(LaTeXFeatures & features) const
361 {
362         for_each(paragraphs().begin(), paragraphs().end(),
363                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
364 }
365
366
367 void InsetText::getCursorPos(LCursor const & cur, int & x, int & y) const
368 {
369         x = text_.cursorX(cur.top());
370         y = text_.cursorY(cur.top());
371 }
372
373
374 bool InsetText::showInsetDialog(BufferView *) const
375 {
376         return false;
377 }
378
379
380 void InsetText::getLabelList(Buffer const & buffer,
381                              std::vector<string> & list) const
382 {
383         ParagraphList::const_iterator pit = paragraphs().begin();
384         ParagraphList::const_iterator pend = paragraphs().end();
385         for (; pit != pend; ++pit) {
386                 InsetList::const_iterator beg = pit->insetlist.begin();
387                 InsetList::const_iterator end = pit->insetlist.end();
388                 for (; beg != end; ++beg)
389                         beg->inset->getLabelList(buffer, list);
390         }
391 }
392
393
394 void InsetText::markNew(bool track_changes)
395 {
396         ParagraphList::iterator pit = paragraphs().begin();
397         ParagraphList::iterator end = paragraphs().end();
398         for (; pit != end; ++pit) {
399                 if (track_changes) {
400                         pit->trackChanges();
401                 } else {
402                         // no-op when not tracking
403                         pit->cleanChanges();
404                 }
405         }
406 }
407
408
409 void InsetText::setText(string const & data, LyXFont const & font)
410 {
411         clear(false);
412         for (unsigned int i = 0; i < data.length(); ++i)
413                 paragraphs().begin()->insertChar(i, data[i], font);
414 }
415
416
417 void InsetText::setAutoBreakRows(bool flag)
418 {
419         if (flag != autoBreakRows_) {
420                 autoBreakRows_ = flag;
421                 if (!flag)
422                         removeNewlines();
423         }
424 }
425
426
427 void InsetText::setDrawFrame(bool flag)
428 {
429         drawFrame_ = flag;
430 }
431
432
433 LColor_color InsetText::frameColor() const
434 {
435         return LColor::color(frame_color_);
436 }
437
438
439 void InsetText::setFrameColor(LColor_color col)
440 {
441         frame_color_ = col;
442 }
443
444
445 void InsetText::setViewCache(BufferView const * bv) const
446 {
447         if (bv && bv != text_.bv_owner) {
448                 //lyxerr << "setting view cache from "
449                 //      << text_.bv_owner << " to " << bv << "\n";
450                 text_.bv_owner = const_cast<BufferView *>(bv);
451         }
452 }
453
454
455 void InsetText::removeNewlines()
456 {
457         ParagraphList::iterator it = paragraphs().begin();
458         ParagraphList::iterator end = paragraphs().end();
459         for (; it != end; ++it)
460                 for (int i = 0; i < it->size(); ++i)
461                         if (it->isNewline(i))
462                                 it->erase(i);
463 }
464
465
466 LyXText * InsetText::getText(int i) const
467 {
468         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
469 }
470
471
472 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
473 {
474 #ifdef WITH_WARNINGS
475 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
476 // And it probably does. You have to take a look at this John. (Lgb)
477 #warning John, have a look here. (Lgb)
478 #endif
479         ParagraphList::iterator pit = plist.begin();
480         ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
481         ++pit;
482         mergeParagraph(buffer->params(), paragraphs(),
483                 ins - paragraphs().begin() - 1);
484
485         ParagraphList::iterator pend = plist.end();
486         for (; pit != pend; ++pit)
487                 paragraphs().push_back(*pit);
488 }
489
490
491 void InsetText::addPreview(PreviewLoader & loader) const
492 {
493         ParagraphList::const_iterator pit = paragraphs().begin();
494         ParagraphList::const_iterator pend = paragraphs().end();
495
496         for (; pit != pend; ++pit) {
497                 InsetList::const_iterator it  = pit->insetlist.begin();
498                 InsetList::const_iterator end = pit->insetlist.end();
499                 for (; it != end; ++it)
500                         it->inset->addPreview(loader);
501         }
502 }
503
504
505 ParagraphList const & InsetText::paragraphs() const
506 {
507         return text_.paragraphs();
508 }
509
510
511 ParagraphList & InsetText::paragraphs()
512 {
513         return text_.paragraphs();
514 }