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