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