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