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