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