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