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