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