]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
CoordBranch merge
[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_pit = -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 paragraph.
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         font_ = mi.base.font;
174         text_.font_ = mi.base.font;
175         text_.metrics(mi, dim);
176         dim_ = dim;
177 }
178
179
180 void InsetText::draw(PainterInfo & pi, int x, int y) const
181 {
182         BOOST_ASSERT(!text_.paragraphs().front().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
193         text_.draw(pi, x, y);
194
195         if (drawFrame_)
196                 drawFrame(pi.pain, x, y);
197 }
198
199
200 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
201 {
202         // repaint the background if needed
203         if (backgroundColor() != LColor::background)
204                 clearInset(pi.pain, x, y);
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 = max(1, text_.width());
212         int const h = text_.height();
213         int const a = text_.ascent();
214         pain.rectangle(x, y - a, w, h, frameColor());
215 }
216
217
218 void InsetText::clearInset(Painter & pain, int x, int y) const
219 {
220         int const w = text_.width();
221         int const h = text_.height();
222         int const a = text_.ascent();
223         pain.fillRectangle(x, y - a, w, h, backgroundColor());
224 }
225
226
227 void InsetText::updateLocal(LCursor & cur)
228 {
229         if (!autoBreakRows_ && paragraphs().size() > 1) {
230                 // collapse paragraphs
231                 while (paragraphs().size() > 1) {
232                         ParagraphList::iterator const first = paragraphs().begin();
233                         ParagraphList::iterator second = first;
234                         ++second;
235                         size_t const first_par_size = first->size();
236
237                         if (!first->empty() &&
238                                         !second->empty() &&
239                                         !first->isSeparator(first_par_size - 1)) {
240                                 first->insertChar(first_par_size, ' ');
241                         }
242
243                         cur.clearSelection();
244                         mergeParagraph(cur.buffer().params(), paragraphs(), 0);
245                 }
246         }
247
248         if (!cur.selection())
249                 cur.resetAnchor();
250
251         LyXView * lv = cur.bv().owner();
252         lv->view_state_changed();
253         lv->updateMenubar();
254         lv->updateToolbars();
255         if (old_pit != cur.pit()) {
256                 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
257                 old_pit = cur.pit();
258         }
259 }
260
261
262 string const InsetText::editMessage() const
263 {
264         return _("Opened Text Inset");
265 }
266
267
268 void InsetText::edit(LCursor & cur, bool left)
269 {
270         //lyxerr << "InsetText: edit left/right" << endl;
271         old_pit = -1;
272         setViewCache(&cur.bv());
273         int const pit = left ? 0 : paragraphs().size() - 1;
274         int const pos = left ? 0 : paragraphs().back().size();
275         text_.setCursor(cur.top(), pit, pos);
276         cur.clearSelection();
277         finishUndo();
278 #ifdef WITH_WARNINGS
279 #warning can someone check if/when this is needed?
280 #endif
281 //Andre?
282 //      updateLocal(cur);
283 }
284
285
286 InsetBase * InsetText::editXY(LCursor & cur, int x, int y) const
287 {
288         old_pit = -1;
289         return text_.editXY(cur, x, y);
290         //sanitizeEmptyText(cur.bv());
291         //updateLocal(cur);
292 }
293
294
295 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
296 {
297         //lyxerr << "InsetText::doDispatch: " << cmd.action << " " << endl;
298         setViewCache(&cur.bv());
299
300         bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
301         text_.dispatch(cur, cmd);
302
303 }
304
305
306 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
307         FuncStatus & status) const
308 {
309         return text_.getStatus(cur, cmd, status);
310 }
311
312
313 int InsetText::latex(Buffer const & buf, ostream & os,
314                      OutputParams const & runparams) const
315 {
316         TexRow texrow;
317         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
318         return texrow.rows();
319 }
320
321
322 int InsetText::plaintext(Buffer const & buf, ostream & os,
323                      OutputParams const & runparams) const
324 {
325         ParagraphList::const_iterator beg = paragraphs().begin();
326         ParagraphList::const_iterator end = paragraphs().end();
327         ParagraphList::const_iterator it = beg;
328         for (; it != end; ++it)
329                 asciiParagraph(buf, *it, os, runparams, it == beg);
330
331         // FIXME: Give the total numbers of lines
332         return 0;
333 }
334
335
336 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
337                         OutputParams const & runparams) const
338 {
339         linuxdocParagraphs(buf, paragraphs(), os, runparams);
340         return 0;
341 }
342
343
344 int InsetText::docbook(Buffer const & buf, ostream & os,
345                        OutputParams const & runparams) const
346 {
347         docbookParagraphs(paragraphs(), buf, os, runparams);
348         return 0;
349 }
350
351
352 void InsetText::validate(LaTeXFeatures & features) const
353 {
354         for_each(paragraphs().begin(), paragraphs().end(),
355                  bind(&Paragraph::validate, _1, ref(features)));
356 }
357
358
359 void InsetText::getCursorPos(CursorSlice const & sl, int & x, int & y) const
360 {
361         x = text_.cursorX(sl);
362         y = text_.cursorY(sl);
363 }
364
365
366 bool InsetText::showInsetDialog(BufferView *) const
367 {
368         return false;
369 }
370
371
372 void InsetText::getLabelList(Buffer const & buffer,
373                              std::vector<string> & list) const
374 {
375         ParagraphList::const_iterator pit = paragraphs().begin();
376         ParagraphList::const_iterator pend = paragraphs().end();
377         for (; pit != pend; ++pit) {
378                 InsetList::const_iterator beg = pit->insetlist.begin();
379                 InsetList::const_iterator end = pit->insetlist.end();
380                 for (; beg != end; ++beg)
381                         beg->inset->getLabelList(buffer, list);
382         }
383 }
384
385
386 void InsetText::markNew(bool track_changes)
387 {
388         ParagraphList::iterator pit = paragraphs().begin();
389         ParagraphList::iterator end = paragraphs().end();
390         for (; pit != end; ++pit) {
391                 if (track_changes) {
392                         pit->trackChanges();
393                 } else {
394                         // no-op when not tracking
395                         pit->cleanChanges();
396                 }
397         }
398 }
399
400
401 void InsetText::setText(string const & data, LyXFont const & font)
402 {
403         clear(false);
404         Paragraph & first = paragraphs().front();
405         for (unsigned int i = 0; i < data.length(); ++i)
406                 first.insertChar(i, data[i], font);
407 }
408
409
410 void InsetText::setAutoBreakRows(bool flag)
411 {
412         if (flag != autoBreakRows_) {
413                 autoBreakRows_ = flag;
414                 if (!flag)
415                         removeNewlines();
416         }
417 }
418
419
420 void InsetText::setDrawFrame(bool flag)
421 {
422         drawFrame_ = flag;
423 }
424
425
426 LColor_color InsetText::frameColor() const
427 {
428         return LColor::color(frame_color_);
429 }
430
431
432 void InsetText::setFrameColor(LColor_color col)
433 {
434         frame_color_ = col;
435 }
436
437
438 void InsetText::setViewCache(BufferView const * bv) const
439 {
440         if (bv && bv != text_.bv_owner) {
441                 //lyxerr << "setting view cache from "
442                 //      << text_.bv_owner << " to " << bv << "\n";
443                 text_.bv_owner = const_cast<BufferView *>(bv);
444         }
445 }
446
447
448 void InsetText::removeNewlines()
449 {
450         ParagraphList::iterator it = paragraphs().begin();
451         ParagraphList::iterator end = paragraphs().end();
452         for (; it != end; ++it)
453                 for (int i = 0; i < it->size(); ++i)
454                         if (it->isNewline(i))
455                                 it->erase(i);
456 }
457
458
459 LyXText * InsetText::getText(int i) const
460 {
461         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
462 }
463
464
465 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
466 {
467 #ifdef WITH_WARNINGS
468 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
469 // And it probably does. You have to take a look at this John. (Lgb)
470 #warning John, have a look here. (Lgb)
471 #endif
472         ParagraphList & pl = paragraphs();
473
474         ParagraphList::iterator pit = plist.begin();
475         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
476         ++pit;
477         mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
478
479         for_each(pit, plist.end(),
480                  bind(&ParagraphList::push_back, ref(pl), _1));
481 }
482
483
484 void InsetText::addPreview(PreviewLoader & loader) const
485 {
486         ParagraphList::const_iterator pit = paragraphs().begin();
487         ParagraphList::const_iterator pend = paragraphs().end();
488
489         for (; pit != pend; ++pit) {
490                 InsetList::const_iterator it  = pit->insetlist.begin();
491                 InsetList::const_iterator end = pit->insetlist.end();
492                 for (; it != end; ++it)
493                         it->inset->addPreview(loader);
494         }
495 }
496
497
498 ParagraphList const & InsetText::paragraphs() const
499 {
500         return text_.paragraphs();
501 }
502
503
504 ParagraphList & InsetText::paragraphs()
505 {
506         return text_.paragraphs();
507 }