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