3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettext.h"
14 #include "insetnewline.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
20 #include "errorlist.h"
21 #include "funcrequest.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
30 #include "ParagraphParameters.h"
31 #include "rowpainter.h"
33 #include "undo_funcs.h"
34 #include "WordLangTuple.h"
36 #include "frontends/Alert.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/Painter.h"
41 #include "support/lyxalgo.h" // lyx::count
43 #include <boost/bind.hpp>
56 using namespace lyx::support;
57 using namespace lyx::graphics;
58 using namespace bv_funcs;
61 using lyx::textclass_type;
64 InsetText::InsetText(BufferParams const & bp)
65 : UpdatableInset(), text_(0, this, true, paragraphs)
67 paragraphs.push_back(Paragraph());
68 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
69 if (bp.tracking_changes)
70 paragraphs.begin()->trackChanges();
75 InsetText::InsetText(InsetText const & in)
76 : UpdatableInset(in), text_(0, this, true, paragraphs)
82 InsetText & InsetText::operator=(InsetText const & it)
89 void InsetText::init(InsetText const * ins)
92 textwidth_ = ins->textwidth_;
93 text_.bv_owner = ins->text_.bv_owner;
95 paragraphs = ins->paragraphs;
97 ParagraphList::iterator pit = paragraphs.begin();
98 ParagraphList::iterator end = paragraphs.end();
99 for (; pit != end; ++pit)
100 pit->setInsetOwner(this);
102 autoBreakRows = ins->autoBreakRows;
103 drawFrame_ = ins->drawFrame_;
104 frame_color = ins->frame_color;
106 textwidth_ = 0; // broken
108 frame_color = LColor::insetframe;
109 autoBreakRows = false;
111 the_locking_inset = 0;
112 for_each(paragraphs.begin(), paragraphs.end(),
113 boost::bind(&Paragraph::setInsetOwner, _1, this));
119 old_par = paragraphs.end();
120 in_insetAllowed = false;
124 void InsetText::clear(bool just_mark_erased)
126 if (just_mark_erased) {
127 ParagraphList::iterator it = paragraphs.begin();
128 ParagraphList::iterator end = paragraphs.end();
129 for (; it != end; ++it)
134 // This is a gross hack...
135 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
138 paragraphs.push_back(Paragraph());
139 paragraphs.begin()->setInsetOwner(this);
140 paragraphs.begin()->layout(old_layout);
144 auto_ptr<InsetBase> InsetText::clone() const
146 return auto_ptr<InsetBase>(new InsetText(*this));
150 void InsetText::write(Buffer const & buf, ostream & os) const
153 writeParagraphData(buf, os);
157 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
159 ParagraphList::const_iterator it = paragraphs.begin();
160 ParagraphList::const_iterator end = paragraphs.end();
161 Paragraph::depth_type dth = 0;
162 for (; it != end; ++it) {
163 it->write(buf, os, buf.params, dth);
168 void InsetText::read(Buffer const & buf, LyXLex & lex)
171 Paragraph::depth_type depth = 0;
175 if (buf.params.tracking_changes)
176 paragraphs.begin()->trackChanges();
178 // delete the initial paragraph
180 ParagraphList::iterator pit = paragraphs.begin();
184 token = lex.getString();
187 if (token == "\\end_inset") {
191 if (token == "\\end_document") {
192 lex.printError("\\end_document read in inset! Error in document!");
197 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
200 pit = paragraphs.begin();
201 ParagraphList::iterator const end = paragraphs.end();
202 for (; pit != end; ++pit)
203 pit->setInsetOwner(this);
205 if (token != "\\end_inset") {
206 lex.printError("Missing \\end_inset at this point. "
212 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
214 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
216 textwidth_ = mi.base.textwidth;
217 BufferView * bv = mi.base.bv;
219 text_.metrics(mi, dim);
220 dim.asc += TEXT_TO_INSET_OFFSET;
221 dim.des += TEXT_TO_INSET_OFFSET;
222 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
223 dim.wid = max(dim.wid, 10);
228 int InsetText::textWidth() const
234 void InsetText::draw(PainterInfo & pi, int x, int y) const
236 // update our idea of where we are. Clearly, we should
237 // not have to know this information.
240 int const start_x = x;
242 BufferView * bv = pi.base.bv;
243 Painter & pain = pi.pain;
245 // repaint the background if needed
246 if (backgroundColor() != LColor::background)
247 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
249 // no draw is necessary !!!
250 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
261 top_y = y - dim_.asc;
263 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
264 inset_x = cx() - x + drawTextXOffset;
265 inset_y = cy() + drawTextYOffset;
268 x += TEXT_TO_INSET_OFFSET;
270 paintTextInset(*bv, text_, x, y);
272 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
273 drawFrame(pain, start_x);
277 void InsetText::drawFrame(Painter & pain, int x) const
279 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
280 int const frame_x = x + ttoD2;
281 int const frame_y = top_baseline - dim_.asc + ttoD2;
282 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
283 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
284 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
288 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
293 if (!autoBreakRows && paragraphs.size() > 1)
294 collapseParagraphs(bv);
296 if (!text_.selection.set())
297 text_.selection.cursor = text_.cursor;
300 bv->updateInset(this);
301 bv->owner()->view_state_changed();
302 bv->owner()->updateMenubar();
303 bv->owner()->updateToolbar();
304 if (old_par != cpar()) {
305 bv->owner()->setLayout(cpar()->layout()->name());
311 string const InsetText::editMessage() const
313 return _("Opened Text Inset");
317 void InsetText::insetUnlock(BufferView * bv)
319 if (the_locking_inset) {
320 the_locking_inset->insetUnlock(bv);
321 the_locking_inset = 0;
322 updateLocal(bv, false);
327 if (text_.selection.set()) {
328 text_.clearSelection();
329 } else if (owner()) {
330 bv->owner()->setLayout(owner()->getLyXText(bv)
331 ->cursor.par()->layout()->name());
333 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
334 // hack for deleteEmptyParMech
335 ParagraphList::iterator first_par = paragraphs.begin();
336 if (!first_par->empty()) {
337 text_.setCursor(first_par, 0);
338 } else if (paragraphs.size() > 1) {
339 text_.setCursor(boost::next(first_par), 0);
344 void InsetText::lockInset(BufferView * bv)
347 the_locking_inset = 0;
348 inset_pos = inset_x = inset_y = 0;
349 inset_boundary = false;
350 inset_par = paragraphs.end();
351 old_par = paragraphs.end();
352 text_.setCursorIntern(paragraphs.begin(), 0);
353 text_.clearSelection();
355 // If the inset is empty set the language of the current font to the
356 // language to the surronding text (if different).
357 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
358 bv->getParentLanguage(this) != text_.current_font.language()) {
359 LyXFont font(LyXFont::ALL_IGNORE);
360 font.setLanguage(bv->getParentLanguage(this));
361 setFont(bv, font, false);
366 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
368 the_locking_inset = inset;
369 inset_x = cx() - top_x + drawTextXOffset;
370 inset_y = cy() + drawTextYOffset;
373 inset_boundary = cboundary();
377 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
379 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
380 << inset << "): " << endl;
383 if (!the_locking_inset) {
384 ParagraphList::iterator pit = paragraphs.begin();
385 ParagraphList::iterator pend = paragraphs.end();
387 int const id = inset->id();
388 for (; pit != pend; ++pit) {
389 InsetList::iterator it = pit->insetlist.begin();
390 InsetList::iterator const end = pit->insetlist.end();
391 for (; it != end; ++it) {
392 if (it->inset == inset) {
393 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
394 text_.setCursorIntern(pit, it->pos);
395 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
396 lyxerr << "bv: " << bv << " inset: " << inset << endl;
397 lockInset(bv, inset);
398 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
401 if (it->inset->getInsetFromID(id)) {
402 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
403 text_.setCursorIntern(pit, it->pos);
404 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
405 return the_locking_inset->lockInsetInInset(bv, inset);
409 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
412 if (inset == cpar()->getInset(cpos())) {
413 lyxerr[Debug::INSETS] << "OK" << endl;
414 lockInset(bv, inset);
418 if (the_locking_inset && the_locking_inset == inset) {
419 if (cpar() == inset_par && cpos() == inset_pos) {
420 lyxerr[Debug::INSETS] << "OK" << endl;
421 inset_x = cx() - top_x + drawTextXOffset;
422 inset_y = cy() + drawTextYOffset;
424 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
426 } else if (the_locking_inset) {
427 lyxerr[Debug::INSETS] << "MAYBE" << endl;
428 return the_locking_inset->lockInsetInInset(bv, inset);
430 lyxerr[Debug::INSETS] << "NOT OK" << endl;
435 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
438 if (!the_locking_inset)
440 if (the_locking_inset == inset) {
441 the_locking_inset->insetUnlock(bv);
442 the_locking_inset = 0;
444 moveRightIntern(bv, true, false);
445 old_par = paragraphs.end(); // force layout setting
449 updateLocal(bv, false);
452 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
456 void InsetText::lfunMousePress(FuncRequest const & cmd)
460 // use this to check mouse motion for selection!
464 BufferView * bv = cmd.view();
465 FuncRequest cmd1 = cmd;
471 int tmp_x = cmd.x - drawTextXOffset;
472 int tmp_y = cmd.y + dim_.asc - bv->top_y();
473 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
475 if (the_locking_inset) {
476 if (the_locking_inset == inset) {
477 the_locking_inset->localDispatch(cmd1);
480 // otherwise only unlock the_locking_inset
481 the_locking_inset->insetUnlock(bv);
482 the_locking_inset = 0;
485 no_selection = false;
487 if (bv->theLockingInset()) {
488 if (isHighlyEditableInset(inset)) {
489 // We just have to lock the inset before calling a
491 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
492 if (!bv->lockInset(uinset)) {
493 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
495 inset->localDispatch(cmd1);
496 if (the_locking_inset)
497 updateLocal(bv, false);
502 bool paste_internally = false;
503 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
504 localDispatch(FuncRequest(bv, LFUN_COPY));
505 paste_internally = true;
507 int old_top_y = bv->top_y();
509 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
511 // set the selection cursor!
512 text_.selection.cursor = text_.cursor;
513 text_.cursor.x_fix(text_.cursor.x());
515 text_.clearSelection();
516 updateLocal(bv, false);
518 bv->owner()->setLayout(cpar()->layout()->name());
520 // we moved the view we cannot do mouse selection in this case!
521 if (bv->top_y() != old_top_y)
524 // Insert primary selection with middle mouse
525 // if there is a local selection in the current buffer,
527 if (cmd.button() == mouse_button::button2) {
528 if (paste_internally)
529 localDispatch(FuncRequest(bv, LFUN_PASTE));
531 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
534 getLyXText(bv)->clearSelection();
539 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
541 BufferView * bv = cmd.view();
542 FuncRequest cmd1 = cmd;
547 if (the_locking_inset)
548 return the_locking_inset->localDispatch(cmd1);
550 int tmp_x = cmd.x - drawTextXOffset;
551 int tmp_y = cmd.y + dim_.asc - bv->top_y();
552 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
555 // This code should probably be removed now. Simple insets
556 // (!highlyEditable) can actually take the localDispatch,
557 // and turn it into edit() if necessary. But we still
558 // need to deal properly with the whole relative vs.
559 // absolute mouse co-ords thing in a realiable, sensible way
561 if (isHighlyEditableInset(inset))
562 ret = inset->localDispatch(cmd1);
564 inset_x = cx(bv) - top_x + drawTextXOffset;
565 inset_y = cy() + drawTextYOffset;
566 cmd1.x = cmd.x - inset_x;
567 cmd1.y = cmd.x - inset_y;
568 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
572 ret = inset->localDispatch(cmd1);
573 updateLocal(bv, false);
580 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
582 FuncRequest cmd1 = cmd;
586 if (the_locking_inset) {
587 the_locking_inset->localDispatch(cmd1);
591 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
594 BufferView * bv = cmd.view();
595 LyXCursor cur = text_.cursor;
596 text_.setCursorFromCoordinates
597 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
598 text_.cursor.x_fix(text_.cursor.x());
599 if (cur == text_.cursor)
601 text_.setSelection();
602 updateLocal(bv, false);
606 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
608 BufferView * bv = cmd.view();
611 switch (cmd.action) {
612 case LFUN_INSET_EDIT: {
613 UpdatableInset::localDispatch(cmd);
615 if (!bv->lockInset(this)) {
616 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
621 the_locking_inset = 0;
625 inset_boundary = false;
626 inset_par = paragraphs.end();
627 old_par = paragraphs.end();
630 if (cmd.argument.size()) {
631 if (cmd.argument == "left")
632 text_.setCursorIntern(paragraphs.begin(), 0);
634 ParagraphList::iterator it = boost::prior(paragraphs.end());
635 text_.setCursor(it, it->size());
638 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
639 // we put here -1 and not button as now the button in the
640 // edit call should not be needed we will fix this in 1.3.x
641 // cycle hopefully (Jug 20020509)
642 // FIXME: GUII I've changed this to none: probably WRONG
643 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
644 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
646 text_.cursor.x(text_.cursor.x());
647 text_.cursor.x_fix(text_.cursor.x());
651 text_.clearSelection();
654 // If the inset is empty set the language of the current font to the
655 // language to the surronding text (if different).
656 if (paragraphs.begin()->empty() &&
657 paragraphs.size() == 1 &&
658 bv->getParentLanguage(this) != text_.current_font.language())
660 LyXFont font(LyXFont::ALL_IGNORE);
661 font.setLanguage(bv->getParentLanguage(this));
662 setFont(bv, font, false);
665 updateLocal(bv, false);
666 // Tell the paragraph dialog that we've entered an insettext.
667 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
671 case LFUN_MOUSE_PRESS:
675 case LFUN_MOUSE_MOTION:
676 lfunMouseMotion(cmd);
679 case LFUN_MOUSE_RELEASE:
680 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
686 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
687 no_selection = false;
689 RESULT result = UpdatableInset::localDispatch(cmd);
690 if (result != UNDISPATCHED)
694 if (cmd.action < 0 && cmd.argument.empty())
697 if (the_locking_inset) {
698 result = the_locking_inset->localDispatch(cmd);
699 if (result == DISPATCHED_NOUPDATE)
701 if (result == DISPATCHED) {
702 updateLocal(bv, false);
705 if (result >= FINISHED) {
708 moveRightIntern(bv, false, false);
713 if (result >= FINISHED) {
714 updateLocal(bv, false);
715 bv->unlockInset(this);
719 result = moveDown(bv);
720 if (result >= FINISHED) {
721 updateLocal(bv, false);
722 bv->unlockInset(this);
729 the_locking_inset = 0;
730 updateLocal(bv, false);
731 // make sure status gets reset immediately
732 bv->owner()->clearMessage();
736 bool updflag = false;
738 switch (cmd.action) {
741 case LFUN_SELFINSERT:
742 if (bv->buffer()->isReadonly()) {
743 // setErrorMessage(N_("Document is read only"));
746 if (!cmd.argument.empty()) {
747 /* Automatically delete the currently selected
748 * text and replace it with what is being
749 * typed in now. Depends on lyxrc settings
750 * "auto_region_delete", which defaults to
753 // This should not be needed here and is also WRONG!
754 recordUndo(bv, Undo::INSERT, text_.cursor.par());
758 if (lyxrc.auto_region_delete && text_.selection.set())
759 text_.cutSelection(false, false);
760 text_.clearSelection();
762 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
763 bv->owner()->getIntl().getTransManager().
764 TranslateAndInsert(cmd.argument[i], &text_);
766 text_.selection.cursor = text_.cursor;
768 result = DISPATCHED_NOUPDATE;
771 // cursor movements that need special handling
774 result = moveRight(bv);
779 result = moveLeft(bv);
783 result = moveDown(bv);
791 if (crow() == text_.firstRow())
792 result = FINISHED_UP;
794 text_.cursorPrevious();
795 text_.clearSelection();
796 result = DISPATCHED_NOUPDATE;
801 if (crow() == text_.lastRow())
802 result = FINISHED_DOWN;
805 text_.clearSelection();
806 result = DISPATCHED_NOUPDATE;
811 if (text_.selection.set())
812 text_.cutSelection(true, false);
819 if (text_.selection.set())
820 text_.cutSelection(true, false);
827 if (!autoBreakRows) {
828 if (CutAndPaste::nrOfParagraphs() > 1) {
830 #warning FIXME horrendously bad UI
832 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
837 replaceSelection(bv->getLyXText());
838 size_t sel_index = 0;
839 string const & arg = cmd.argument;
840 if (isStrUnsignedInt(arg)) {
841 size_t const paste_arg = strToUnsignedInt(arg);
842 #warning FIXME Check if the arg is in the domain of available selections.
843 sel_index = paste_arg;
845 text_.pasteSelection(sel_index);
847 text_.clearSelection();
852 case LFUN_BREAKPARAGRAPH:
853 if (!autoBreakRows) {
857 replaceSelection(bv->getLyXText());
858 text_.breakParagraph(paragraphs, 0);
862 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
863 if (!autoBreakRows) {
867 replaceSelection(bv->getLyXText());
868 text_.breakParagraph(paragraphs, 1);
872 case LFUN_BREAKLINE: {
873 if (!autoBreakRows) {
878 replaceSelection(bv->getLyXText());
879 text_.insertInset(new InsetNewline);
885 // do not set layouts on non breakable textinsets
887 string cur_layout = cpar()->layout()->name();
889 // Derive layout number from given argument (string)
890 // and current buffer's textclass (number).
891 LyXTextClass const & tclass =
892 bv->buffer()->params.getLyXTextClass();
893 string layout = cmd.argument;
894 bool hasLayout = tclass.hasLayout(layout);
896 // If the entry is obsolete, use the new one instead.
898 string const & obs = tclass[layout]->obsoleted_by();
903 // see if we found the layout number:
905 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
906 bv->owner()->dispatch(lf);
910 if (cur_layout != layout) {
912 text_.setLayout(layout);
913 bv->owner()->setLayout(cpar()->layout()->name());
917 // reset the layout box
918 bv->owner()->setLayout(cpar()->layout()->name());
923 if (!bv->dispatch(cmd))
924 result = UNDISPATCHED;
928 updateLocal(bv, updflag);
930 /// If the action has deleted all text in the inset, we need to change the
931 // language to the language of the surronding text.
932 if (!was_empty && paragraphs.begin()->empty() &&
933 paragraphs.size() == 1) {
934 LyXFont font(LyXFont::ALL_IGNORE);
935 font.setLanguage(bv->getParentLanguage(this));
936 setFont(bv, font, false);
939 if (result >= FINISHED)
940 bv->unlockInset(this);
942 if (result == DISPATCHED_NOUPDATE)
948 int InsetText::latex(Buffer const & buf, ostream & os,
949 LatexRunParams const & runparams) const
952 latexParagraphs(buf, paragraphs, os, texrow, runparams);
953 return texrow.rows();
957 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
959 unsigned int lines = 0;
961 ParagraphList::const_iterator beg = paragraphs.begin();
962 ParagraphList::const_iterator end = paragraphs.end();
963 ParagraphList::const_iterator it = beg;
964 for (; it != end; ++it) {
965 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
966 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
973 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
975 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
976 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
978 // There is a confusion between the empty paragraph and the default paragraph
979 // The default paragraph is <p></p>, the empty paragraph is *empty*
980 // Since none of the floats of linuxdoc accepts standard paragraphs
981 // I disable them. I don't expect problems. (jamatos 2003/07/27)
982 for (; pit != pend; ++pit) {
983 const string name = pit->layout()->latexname();
985 sgml::openTag(os, 1, 0, name);
986 buf.simpleLinuxDocOnePar(os, pit, 0);
988 sgml::closeTag(os, 1, 0, name);
994 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
996 unsigned int lines = 0;
998 vector<string> environment_stack(10);
999 vector<string> environment_inner(10);
1001 int const command_depth = 0;
1004 Paragraph::depth_type depth = 0; // paragraph depth
1006 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1007 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1009 for (; pit != pend; ++pit) {
1010 int desc_on = 0; // description mode
1012 LyXLayout_ptr const & style = pit->layout();
1014 // environment tag closing
1015 for (; depth > pit->params().depth(); --depth) {
1016 if (environment_inner[depth] != "!-- --") {
1017 item_name = "listitem";
1018 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1019 if (environment_inner[depth] == "varlistentry")
1020 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1022 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1023 environment_stack[depth].erase();
1024 environment_inner[depth].erase();
1027 if (depth == pit->params().depth()
1028 && environment_stack[depth] != style->latexname()
1029 && !environment_stack[depth].empty()) {
1030 if (environment_inner[depth] != "!-- --") {
1031 item_name= "listitem";
1032 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1033 if (environment_inner[depth] == "varlistentry")
1034 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1037 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1039 environment_stack[depth].erase();
1040 environment_inner[depth].erase();
1043 // Write opening SGML tags.
1044 switch (style->latextype) {
1045 case LATEX_PARAGRAPH:
1046 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1050 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1054 case LATEX_ENVIRONMENT:
1055 case LATEX_ITEM_ENVIRONMENT:
1056 if (depth < pit->params().depth()) {
1057 depth = pit->params().depth();
1058 environment_stack[depth].erase();
1061 if (environment_stack[depth] != style->latexname()) {
1062 if (environment_stack.size() == depth + 1) {
1063 environment_stack.push_back("!-- --");
1064 environment_inner.push_back("!-- --");
1066 environment_stack[depth] = style->latexname();
1067 environment_inner[depth] = "!-- --";
1068 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1070 if (environment_inner[depth] != "!-- --") {
1071 item_name= "listitem";
1072 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1073 if (environment_inner[depth] == "varlistentry")
1074 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1078 if (style->latextype == LATEX_ENVIRONMENT) {
1079 if (!style->latexparam().empty()) {
1080 if (style->latexparam() == "CDATA")
1083 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1088 desc_on = (style->labeltype == LABEL_MANUAL);
1090 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1091 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1093 item_name = desc_on ? "term" : "para";
1094 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1098 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1102 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1105 // write closing SGML tags
1106 switch (style->latextype) {
1107 case LATEX_ENVIRONMENT:
1108 if (!style->latexparam().empty()) {
1109 if (style->latexparam() == "CDATA")
1112 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1115 case LATEX_ITEM_ENVIRONMENT:
1119 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1121 case LATEX_PARAGRAPH:
1122 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1125 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1131 for (int d = depth; d >= 0; --d) {
1132 if (!environment_stack[depth].empty()) {
1133 if (environment_inner[depth] != "!-- --") {
1134 item_name = "listitem";
1135 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1136 if (environment_inner[depth] == "varlistentry")
1137 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1140 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1148 void InsetText::validate(LaTeXFeatures & features) const
1150 for_each(paragraphs.begin(), paragraphs.end(),
1151 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1155 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1157 if (the_locking_inset) {
1158 the_locking_inset->getCursor(bv, x, y);
1162 y = cy() + InsetText::y();
1166 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1168 if (the_locking_inset) {
1169 the_locking_inset->getCursorPos(bv, x, y);
1172 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1173 y = cy() - TEXT_TO_INSET_OFFSET;
1177 int InsetText::insetInInsetY() const
1179 if (!the_locking_inset)
1182 return inset_y + the_locking_inset->insetInInsetY();
1186 void InsetText::fitInsetCursor(BufferView * bv) const
1188 if (the_locking_inset) {
1189 the_locking_inset->fitInsetCursor(bv);
1193 LyXFont const font = text_.getFont(cpar(), cpos());
1195 int const asc = font_metrics::maxAscent(font);
1196 int const desc = font_metrics::maxDescent(font);
1198 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1202 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1204 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1205 return moveLeftIntern(bv, false, true, false);
1207 return moveRightIntern(bv, true, true, false);
1211 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1213 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1214 return moveRightIntern(bv, true, true, false);
1216 return moveLeftIntern(bv, false, true, false);
1221 InsetText::moveRightIntern(BufferView * bv, bool front,
1222 bool activate_inset, bool selecting)
1224 ParagraphList::iterator c_par = cpar();
1226 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1227 return FINISHED_RIGHT;
1228 if (activate_inset && checkAndActivateInset(bv, front))
1230 text_.cursorRight(bv);
1232 text_.clearSelection();
1233 return DISPATCHED_NOUPDATE;
1238 InsetText::moveLeftIntern(BufferView * bv, bool front,
1239 bool activate_inset, bool selecting)
1241 if (cpar() == paragraphs.begin() && cpos() <= 0)
1243 text_.cursorLeft(bv);
1245 text_.clearSelection();
1246 if (activate_inset && checkAndActivateInset(bv, front))
1248 return DISPATCHED_NOUPDATE;
1252 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1254 if (crow() == text_.firstRow())
1257 text_.clearSelection();
1258 return DISPATCHED_NOUPDATE;
1262 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1264 if (crow() == text_.lastRow())
1265 return FINISHED_DOWN;
1266 text_.cursorDown(bv);
1267 text_.clearSelection();
1268 return DISPATCHED_NOUPDATE;
1272 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1274 if (the_locking_inset) {
1275 if (the_locking_inset->insetAllowed(inset))
1276 return the_locking_inset->insertInset(bv, inset);
1279 inset->setOwner(this);
1280 text_.insertInset(inset);
1282 updateLocal(bv, true);
1287 bool InsetText::insetAllowed(InsetOld::Code code) const
1289 // in_insetAllowed is a really gross hack,
1290 // to allow us to call the owner's insetAllowed
1291 // without stack overflow, which can happen
1292 // when the owner uses InsetCollapsable::insetAllowed()
1294 if (in_insetAllowed)
1296 in_insetAllowed = true;
1297 if (the_locking_inset)
1298 ret = the_locking_inset->insetAllowed(code);
1300 ret = owner()->insetAllowed(code);
1301 in_insetAllowed = false;
1306 UpdatableInset * InsetText::getLockingInset() const
1308 return the_locking_inset ? the_locking_inset->getLockingInset() :
1309 const_cast<InsetText *>(this);
1313 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1317 if (the_locking_inset)
1318 return the_locking_inset->getFirstLockingInsetOfType(c);
1323 bool InsetText::showInsetDialog(BufferView * bv) const
1325 if (the_locking_inset)
1326 return the_locking_inset->showInsetDialog(bv);
1331 void InsetText::getLabelList(std::vector<string> & list) const
1333 ParagraphList::const_iterator pit = paragraphs.begin();
1334 ParagraphList::const_iterator pend = paragraphs.end();
1335 for (; pit != pend; ++pit) {
1336 InsetList::const_iterator beg = pit->insetlist.begin();
1337 InsetList::const_iterator end = pit->insetlist.end();
1338 for (; beg != end; ++beg)
1339 beg->inset->getLabelList(list);
1344 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1347 if (the_locking_inset) {
1348 the_locking_inset->setFont(bv, font, toggleall, selectall);
1352 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1353 || cpar()->empty()) {
1354 text_.setFont(font, toggleall);
1359 if (text_.selection.set())
1360 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1364 text_.selection.cursor = text_.cursor;
1365 text_.cursorBottom();
1366 text_.setSelection();
1369 text_.toggleFree(font, toggleall);
1372 text_.clearSelection();
1375 updateLocal(bv, true);
1379 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1381 if (cpos() == cpar()->size())
1383 InsetOld * inset = cpar()->getInset(cpos());
1384 if (!isHighlyEditableInset(inset))
1386 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1387 inset->localDispatch(cmd);
1388 if (!the_locking_inset)
1390 updateLocal(bv, false);
1395 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1396 mouse_button::state button)
1398 x -= drawTextXOffset;
1400 int dummyy = y + dim_.asc;
1401 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1402 // we only do the edit() call if the inset was hit by the mouse
1403 // or if it is a highly editable inset. So we should call this
1404 // function from our own edit with button < 0.
1405 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1407 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1416 inset_x = cx() - top_x + drawTextXOffset;
1417 inset_y = cy() + drawTextYOffset;
1418 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1419 inset->localDispatch(cmd);
1420 if (!the_locking_inset)
1422 updateLocal(bv, false);
1427 void InsetText::markNew(bool track_changes)
1429 ParagraphList::iterator pit = paragraphs.begin();
1430 ParagraphList::iterator end = paragraphs.end();
1431 for (; pit != end; ++pit) {
1432 if (track_changes) {
1433 pit->trackChanges();
1435 // no-op when not tracking
1436 pit->cleanChanges();
1442 void InsetText::setText(string const & data, LyXFont const & font)
1445 for (unsigned int i = 0; i < data.length(); ++i)
1446 paragraphs.begin()->insertChar(i, data[i], font);
1450 void InsetText::setAutoBreakRows(bool flag)
1452 if (flag != autoBreakRows) {
1453 autoBreakRows = flag;
1460 void InsetText::setDrawFrame(DrawFrame how)
1466 void InsetText::setFrameColor(LColor::color col)
1472 int InsetText::cx() const
1474 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1475 if (the_locking_inset) {
1476 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1477 if (font.isVisibleRightToLeft())
1478 x -= the_locking_inset->width();
1484 int InsetText::cy() const
1486 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1490 pos_type InsetText::cpos() const
1492 return text_.cursor.pos();
1496 ParagraphList::iterator InsetText::cpar() const
1498 return text_.cursor.par();
1502 bool InsetText::cboundary() const
1504 return text_.cursor.boundary();
1508 RowList::iterator InsetText::crow() const
1510 return text_.cursorRow();
1514 LyXText * InsetText::getLyXText(BufferView const * bv,
1515 bool const recursive) const
1518 if (recursive && the_locking_inset)
1519 return the_locking_inset->getLyXText(bv, true);
1524 void InsetText::setViewCache(BufferView const * bv) const
1527 if (bv != text_.bv_owner) {
1528 //lyxerr << "setting view cache from "
1529 // << text_.bv_owner << " to " << bv << "\n";
1530 text_.init(const_cast<BufferView *>(bv));
1532 text_.bv_owner = const_cast<BufferView *>(bv);
1537 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1540 /// then remove all LyXText in text-insets
1541 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1542 const_cast<ParagraphList&>(paragraphs).end(),
1543 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1548 void InsetText::removeNewlines()
1550 ParagraphList::iterator it = paragraphs.begin();
1551 ParagraphList::iterator end = paragraphs.end();
1552 for (; it != end; ++it)
1553 for (int i = 0; i < it->size(); ++i)
1554 if (it->isNewline(i))
1559 int InsetText::scroll(bool recursive) const
1561 int sx = UpdatableInset::scroll(false);
1563 if (recursive && the_locking_inset)
1564 sx += the_locking_inset->scroll(recursive);
1570 void InsetText::clearSelection(BufferView *)
1572 text_.clearSelection();
1576 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1578 Painter & pain = bv->painter();
1580 int h = dim_.asc + dim_.des;
1581 int ty = baseline - dim_.asc;
1587 if (ty + h > pain.paperHeight())
1588 h = pain.paperHeight();
1589 if (top_x + drawTextXOffset + w > pain.paperWidth())
1590 w = pain.paperWidth();
1591 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1595 ParagraphList * InsetText::getParagraphs(int i) const
1597 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1601 LyXCursor const & InsetText::cursor(BufferView * bv) const
1603 if (the_locking_inset)
1604 return the_locking_inset->cursor(bv);
1605 return getLyXText(bv)->cursor;
1609 InsetOld * InsetText::getInsetFromID(int id_arg) const
1612 return const_cast<InsetText *>(this);
1614 ParagraphList::const_iterator pit = paragraphs.begin();
1615 ParagraphList::const_iterator pend = paragraphs.end();
1616 for (; pit != pend; ++pit) {
1617 InsetList::const_iterator it = pit->insetlist.begin();
1618 InsetList::const_iterator end = pit->insetlist.end();
1619 for (; it != end; ++it) {
1620 if (it->inset->id() == id_arg)
1622 InsetOld * in = it->inset->getInsetFromID(id_arg);
1632 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1635 if (the_locking_inset) {
1636 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1637 if (!word.word().empty()) {
1641 // we have to go on checking so move cursor to the next char
1642 text_.cursor.pos(text_.cursor.pos() + 1);
1644 word = text_.selectNextWordToSpellcheck(value);
1645 if (word.word().empty())
1646 bv->unlockInset(const_cast<InsetText *>(this));
1653 void InsetText::selectSelectedWord(BufferView * bv)
1655 if (the_locking_inset) {
1656 the_locking_inset->selectSelectedWord(bv);
1659 getLyXText(bv)->selectSelectedWord();
1660 updateLocal(bv, false);
1664 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1666 if (the_locking_inset) {
1667 if (the_locking_inset->nextChange(bv, length))
1669 text_.cursorRight(true);
1671 lyx::find::SearchResult result =
1672 lyx::find::findNextChange(bv, &text_, length);
1674 if (result == lyx::find::SR_FOUND) {
1675 LyXCursor cur = text_.cursor;
1676 bv->unlockInset(bv->theLockingInset());
1677 if (bv->lockInset(this))
1680 text_.setSelectionRange(length);
1681 updateLocal(bv, false);
1683 return result != lyx::find::SR_NOT_FOUND;
1687 bool InsetText::searchForward(BufferView * bv, string const & str,
1690 if (the_locking_inset) {
1691 if (the_locking_inset->searchForward(bv, str, cs, mw))
1693 text_.cursorRight(true);
1695 lyx::find::SearchResult result =
1696 lyx::find::find(bv, &text_, str, true, cs, mw);
1698 if (result == lyx::find::SR_FOUND) {
1699 LyXCursor cur = text_.cursor;
1700 bv->unlockInset(bv->theLockingInset());
1701 if (bv->lockInset(this))
1704 text_.setSelectionRange(str.length());
1705 updateLocal(bv, false);
1707 return result != lyx::find::SR_NOT_FOUND;
1711 bool InsetText::searchBackward(BufferView * bv, string const & str,
1714 if (the_locking_inset) {
1715 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1719 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1720 text_.setCursor(pit, pit->size());
1722 lyx::find::SearchResult result =
1723 lyx::find::find(bv, &text_, str, false, cs, mw);
1725 if (result == lyx::find::SR_FOUND) {
1726 LyXCursor cur = text_.cursor;
1727 bv->unlockInset(bv->theLockingInset());
1728 if (bv->lockInset(this))
1731 text_.setSelectionRange(str.length());
1732 updateLocal(bv, false);
1734 return result != lyx::find::SR_NOT_FOUND;
1738 bool InsetText::checkInsertChar(LyXFont & font)
1740 return owner() ? owner()->checkInsertChar(font) : true;
1744 void InsetText::collapseParagraphs(BufferView * bv)
1746 while (paragraphs.size() > 1) {
1747 ParagraphList::iterator first_par = paragraphs.begin();
1748 ParagraphList::iterator next_par = boost::next(first_par);
1749 size_t const first_par_size = first_par->size();
1751 if (!first_par->empty() &&
1752 !next_par->empty() &&
1753 !first_par->isSeparator(first_par_size - 1)) {
1754 first_par->insertChar(first_par_size, ' ');
1757 if (text_.selection.set()) {
1758 if (text_.selection.start.par() == next_par) {
1759 text_.selection.start.par(first_par);
1760 text_.selection.start.pos(
1761 text_.selection.start.pos() + first_par_size);
1763 if (text_.selection.end.par() == next_par) {
1764 text_.selection.end.par(first_par);
1765 text_.selection.end.pos(
1766 text_.selection.end.pos() + first_par_size);
1770 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1775 void InsetText::getDrawFont(LyXFont & font) const
1779 owner()->getDrawFont(font);
1783 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1785 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1786 // And it probably does. You have to take a look at this John. (Lgb)
1787 #warning John, have a look here. (Lgb)
1788 ParagraphList::iterator pit = plist.begin();
1789 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1791 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1793 ParagraphList::iterator pend = plist.end();
1794 for (; pit != pend; ++pit)
1795 paragraphs.push_back(*pit);
1799 void InsetText::addPreview(PreviewLoader & loader) const
1801 ParagraphList::const_iterator pit = paragraphs.begin();
1802 ParagraphList::const_iterator pend = paragraphs.end();
1804 for (; pit != pend; ++pit) {
1805 InsetList::const_iterator it = pit->insetlist.begin();
1806 InsetList::const_iterator end = pit->insetlist.end();
1807 for (; it != end; ++it)
1808 it->inset->addPreview(loader);