]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
the pariterator 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 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         updateLocal(cur);
298 }
299
300
301 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
302 {
303         lyxerr << "InsetText::edit xy" << endl;
304         old_par = -1;
305         return text_.editXY(cur, x, y);
306         //sanitizeEmptyText(cur.bv());
307         //updateLocal(cur);
308 }
309
310
311 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
312 {
313         //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
314         setViewCache(&cur.bv());
315
316         bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
317         text_.dispatch(cur, cmd);
318
319         // If the action has deleted all text in the inset, we need
320         // to change the language to the language of the surronding
321         // text.
322         // Why this cleverness? (Andre')
323         if (!was_empty && paragraphs().begin()->empty() &&
324             paragraphs().size() == 1) {
325                 LyXFont font(LyXFont::ALL_IGNORE);
326                 font.setLanguage(cur.bv().getParentLanguage(this));
327                 text_.setFont(cur, font, false);
328         }
329 }
330
331
332 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
333         FuncStatus & status) const
334 {
335         return text_.getStatus(cur, cmd, status);
336 }
337
338
339 int InsetText::latex(Buffer const & buf, ostream & os,
340                      OutputParams const & runparams) const
341 {
342         TexRow texrow;
343         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
344         return texrow.rows();
345 }
346
347
348 int InsetText::plaintext(Buffer const & buf, ostream & os,
349                      OutputParams const & runparams) const
350 {
351         ParagraphList::const_iterator beg = paragraphs().begin();
352         ParagraphList::const_iterator end = paragraphs().end();
353         ParagraphList::const_iterator it = beg;
354         for (; it != end; ++it)
355                 asciiParagraph(buf, *it, os, runparams, it == beg);
356
357         //FIXME: Give the total numbers of lines
358         return 0;
359 }
360
361
362 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
363                         OutputParams const & runparams) const
364 {
365         linuxdocParagraphs(buf, paragraphs(), os, runparams);
366         return 0;
367 }
368
369
370 int InsetText::docbook(Buffer const & buf, ostream & os,
371                        OutputParams const & runparams) const
372 {
373         docbookParagraphs(buf, paragraphs(), os, runparams);
374         return 0;
375 }
376
377
378 void InsetText::validate(LaTeXFeatures & features) const
379 {
380         for_each(paragraphs().begin(), paragraphs().end(),
381                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
382 }
383
384
385 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
386 {
387         x = text_.cursorX(cur);
388         y = text_.cursorY(cur);
389 }
390
391
392 bool InsetText::showInsetDialog(BufferView *) const
393 {
394         return false;
395 }
396
397
398 void InsetText::getLabelList(Buffer const & buffer,
399                              std::vector<string> & list) const
400 {
401         ParagraphList::const_iterator pit = paragraphs().begin();
402         ParagraphList::const_iterator pend = paragraphs().end();
403         for (; pit != pend; ++pit) {
404                 InsetList::const_iterator beg = pit->insetlist.begin();
405                 InsetList::const_iterator end = pit->insetlist.end();
406                 for (; beg != end; ++beg)
407                         beg->inset->getLabelList(buffer, list);
408         }
409 }
410
411
412 void InsetText::markNew(bool track_changes)
413 {
414         ParagraphList::iterator pit = paragraphs().begin();
415         ParagraphList::iterator end = paragraphs().end();
416         for (; pit != end; ++pit) {
417                 if (track_changes) {
418                         pit->trackChanges();
419                 } else {
420                         // no-op when not tracking
421                         pit->cleanChanges();
422                 }
423         }
424 }
425
426
427 void InsetText::setText(string const & data, LyXFont const & font)
428 {
429         clear(false);
430         for (unsigned int i = 0; i < data.length(); ++i)
431                 paragraphs().begin()->insertChar(i, data[i], font);
432 }
433
434
435 void InsetText::setAutoBreakRows(bool flag)
436 {
437         if (flag != autoBreakRows_) {
438                 autoBreakRows_ = flag;
439                 if (!flag)
440                         removeNewlines();
441         }
442 }
443
444
445 void InsetText::setDrawFrame(DrawFrame how)
446 {
447         drawFrame_ = how;
448 }
449
450
451 LColor_color InsetText::frameColor() const
452 {
453         return LColor::color(frame_color_);
454 }
455
456
457 void InsetText::setFrameColor(LColor_color col)
458 {
459         frame_color_ = col;
460 }
461
462
463 void InsetText::setViewCache(BufferView const * bv) const
464 {
465         if (bv && bv != text_.bv_owner) {
466                 //lyxerr << "setting view cache from "
467                 //      << text_.bv_owner << " to " << bv << "\n";
468                 text_.bv_owner = const_cast<BufferView *>(bv);
469         }
470 }
471
472
473 void InsetText::removeNewlines()
474 {
475         ParagraphList::iterator it = paragraphs().begin();
476         ParagraphList::iterator end = paragraphs().end();
477         for (; it != end; ++it)
478                 for (int i = 0; i < it->size(); ++i)
479                         if (it->isNewline(i))
480                                 it->erase(i);
481 }
482
483
484 LyXText * InsetText::getText(int i) const
485 {
486         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
487 }
488
489
490 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
491 {
492 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
493 // And it probably does. You have to take a look at this John. (Lgb)
494 #warning John, have a look here. (Lgb)
495         ParagraphList::iterator pit = plist.begin();
496         ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
497         ++pit;
498         mergeParagraph(buffer->params(), paragraphs(),
499                 ins - paragraphs().begin() - 1);
500
501         ParagraphList::iterator pend = plist.end();
502         for (; pit != pend; ++pit)
503                 paragraphs().push_back(*pit);
504 }
505
506
507 void InsetText::addPreview(PreviewLoader & loader) const
508 {
509         ParagraphList::const_iterator pit = paragraphs().begin();
510         ParagraphList::const_iterator pend = paragraphs().end();
511
512         for (; pit != pend; ++pit) {
513                 InsetList::const_iterator it  = pit->insetlist.begin();
514                 InsetList::const_iterator end = pit->insetlist.end();
515                 for (; it != end; ++it)
516                         it->inset->addPreview(loader);
517         }
518 }
519
520
521 ParagraphList const & InsetText::paragraphs() const
522 {
523         return text_.paragraphs();
524 }
525
526
527 ParagraphList & InsetText::paragraphs()
528 {
529         return text_.paragraphs();
530 }