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 "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
21 #include "errorlist.h"
22 #include "funcrequest.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "rowpainter.h"
36 #include "undo_funcs.h"
37 #include "WordLangTuple.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
44 #include "support/lyxalgo.h" // lyx::count
46 #include <boost/bind.hpp>
48 using bv_funcs::replaceSelection;
52 using lyx::graphics::PreviewLoader;
54 using lyx::support::isStrUnsignedInt;
55 using lyx::support::strToUnsignedInt;
66 InsetText::InsetText(BufferParams const & bp)
69 autoBreakRows_(false),
71 frame_color_(LColor::insetframe),
72 text_(0, this, true, paragraphs)
74 textwidth_ = 0; // broken
75 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
76 if (bp.tracking_changes)
77 paragraphs.begin()->trackChanges();
82 InsetText::InsetText(InsetText const & in)
84 text_(in.text_.bv_owner, this, true, paragraphs)
91 void InsetText::operator=(InsetText const & in)
93 UpdatableInset::operator=(in);
94 paragraphs = in.paragraphs;
95 autoBreakRows_ = in.autoBreakRows_;
96 drawFrame_ = in.drawFrame_;
97 frame_color_ = in.frame_color_;
98 textwidth_ = in.textwidth_;
99 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
104 void InsetText::init()
106 ParagraphList::iterator pit = paragraphs.begin();
107 ParagraphList::iterator end = paragraphs.end();
108 for (; pit != end; ++pit)
109 pit->setInsetOwner(this);
110 text_.paragraphs_ = ¶graphs;
114 inset_par = paragraphs.end();
119 the_locking_inset = 0;
120 old_par = paragraphs.end();
121 in_insetAllowed = false;
127 void InsetText::clear(bool just_mark_erased)
129 if (just_mark_erased) {
130 ParagraphList::iterator it = paragraphs.begin();
131 ParagraphList::iterator end = paragraphs.end();
132 for (; it != end; ++it)
137 // This is a gross hack...
138 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
141 paragraphs.push_back(Paragraph());
142 paragraphs.begin()->setInsetOwner(this);
143 paragraphs.begin()->layout(old_layout);
147 auto_ptr<InsetBase> InsetText::clone() const
149 return auto_ptr<InsetBase>(new InsetText(*this));
153 void InsetText::write(Buffer const & buf, ostream & os) const
156 writeParagraphData(buf, os);
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
162 ParagraphList::const_iterator it = paragraphs.begin();
163 ParagraphList::const_iterator end = paragraphs.end();
164 Paragraph::depth_type dth = 0;
165 for (; it != end; ++it) {
166 it->write(buf, os, buf.params(), dth);
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
174 Paragraph::depth_type depth = 0;
178 #warning John, look here. Doesnt make much sense.
179 if (buf.params().tracking_changes)
180 paragraphs.begin()->trackChanges();
182 // delete the initial paragraph
183 Paragraph oldpar = *paragraphs.begin();
185 ParagraphList::iterator pit = paragraphs.begin();
189 token = lex.getString();
192 if (token == "\\end_inset") {
196 if (token == "\\end_document") {
197 lex.printError("\\end_document read in inset! Error in document!");
202 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
205 pit = paragraphs.begin();
206 ParagraphList::iterator const end = paragraphs.end();
207 for (; pit != end; ++pit)
208 pit->setInsetOwner(this);
210 if (token != "\\end_inset") {
211 lex.printError("Missing \\end_inset at this point. "
216 // ensure we have at least one par.
217 if (paragraphs.empty())
218 paragraphs.push_back(oldpar);
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
224 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226 textwidth_ = mi.base.textwidth;
227 BufferView * bv = mi.base.bv;
229 text_.metrics(mi, dim);
230 dim.asc += TEXT_TO_INSET_OFFSET;
231 dim.des += TEXT_TO_INSET_OFFSET;
232 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233 dim.wid = max(dim.wid, 10);
238 int InsetText::textWidth() const
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
246 // update our idea of where we are. Clearly, we should
247 // not have to know this information.
250 int const start_x = x;
252 BufferView * bv = pi.base.bv;
253 Painter & pain = pi.pain;
255 // repaint the background if needed
256 if (backgroundColor() != LColor::background)
257 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
259 // no draw is necessary !!!
260 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
271 top_y = y - dim_.asc;
273 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
278 x += TEXT_TO_INSET_OFFSET;
280 paintTextInset(*bv, text_, x, y);
282 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283 drawFrame(pain, start_x);
287 void InsetText::drawFrame(Painter & pain, int x) const
289 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290 int const frame_x = x + ttoD2;
291 int const frame_y = top_baseline - dim_.asc + ttoD2;
292 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
303 if (!autoBreakRows_ && paragraphs.size() > 1)
304 collapseParagraphs(bv);
306 if (!text_.selection.set())
307 text_.selection.cursor = text_.cursor;
310 bv->updateInset(this);
311 bv->owner()->view_state_changed();
312 bv->owner()->updateMenubar();
313 bv->owner()->updateToolbar();
314 if (old_par != cpar()) {
315 bv->owner()->setLayout(cpar()->layout()->name());
321 string const InsetText::editMessage() const
323 return _("Opened Text Inset");
327 void InsetText::insetUnlock(BufferView * bv)
329 if (the_locking_inset) {
330 the_locking_inset->insetUnlock(bv);
331 the_locking_inset = 0;
332 updateLocal(bv, false);
337 if (text_.selection.set()) {
338 text_.clearSelection();
339 } else if (owner()) {
340 bv->owner()->setLayout(owner()->getLyXText(bv)
341 ->cursor.par()->layout()->name());
343 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
344 // hack for deleteEmptyParMech
345 ParagraphList::iterator first_par = paragraphs.begin();
346 if (!first_par->empty()) {
347 text_.setCursor(first_par, 0);
348 } else if (paragraphs.size() > 1) {
349 text_.setCursor(boost::next(first_par), 0);
354 void InsetText::lockInset(BufferView * bv)
357 the_locking_inset = 0;
358 inset_pos = inset_x = inset_y = 0;
359 inset_boundary = false;
360 inset_par = paragraphs.end();
361 old_par = paragraphs.end();
362 text_.setCursorIntern(paragraphs.begin(), 0);
363 text_.clearSelection();
365 // If the inset is empty set the language of the current font to the
366 // language to the surronding text (if different).
367 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
368 bv->getParentLanguage(this) != text_.current_font.language()) {
369 LyXFont font(LyXFont::ALL_IGNORE);
370 font.setLanguage(bv->getParentLanguage(this));
371 setFont(bv, font, false);
376 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
378 the_locking_inset = inset;
379 inset_x = cx() - top_x;
383 inset_boundary = cboundary();
387 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
389 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
390 << inset << "): " << endl;
393 if (!the_locking_inset) {
394 ParagraphList::iterator pit = paragraphs.begin();
395 ParagraphList::iterator pend = paragraphs.end();
397 for (; pit != pend; ++pit) {
398 InsetList::iterator it = pit->insetlist.begin();
399 InsetList::iterator const end = pit->insetlist.end();
400 for (; it != end; ++it) {
401 if (it->inset == inset) {
402 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
403 text_.setCursorIntern(pit, it->pos);
404 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
405 lyxerr << "bv: " << bv << " inset: " << inset << endl;
406 lockInset(bv, inset);
407 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
412 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
415 if (inset == cpar()->getInset(cpos())) {
416 lyxerr[Debug::INSETS] << "OK" << endl;
417 lockInset(bv, inset);
421 if (the_locking_inset && the_locking_inset == inset) {
422 if (cpar() == inset_par && cpos() == inset_pos) {
423 lyxerr[Debug::INSETS] << "OK" << endl;
424 inset_x = cx() - top_x;
427 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
429 } else if (the_locking_inset) {
430 lyxerr[Debug::INSETS] << "MAYBE" << endl;
431 return the_locking_inset->lockInsetInInset(bv, inset);
433 lyxerr[Debug::INSETS] << "NOT OK" << endl;
438 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
441 if (!the_locking_inset)
443 if (the_locking_inset == inset) {
444 the_locking_inset->insetUnlock(bv);
445 the_locking_inset = 0;
447 moveRightIntern(bv, true, false);
448 old_par = paragraphs.end(); // force layout setting
452 updateLocal(bv, false);
455 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
459 void InsetText::lfunMousePress(FuncRequest const & cmd)
463 // use this to check mouse motion for selection!
467 BufferView * bv = cmd.view();
468 FuncRequest cmd1 = cmd;
475 int tmp_y = cmd.y + dim_.asc - bv->top_y();
476 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
478 if (the_locking_inset) {
479 if (the_locking_inset == inset) {
480 the_locking_inset->localDispatch(cmd1);
483 // otherwise only unlock the_locking_inset
484 the_locking_inset->insetUnlock(bv);
485 the_locking_inset = 0;
488 no_selection = false;
490 if (bv->theLockingInset()) {
491 if (isHighlyEditableInset(inset)) {
492 // We just have to lock the inset before calling a
494 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
495 if (!bv->lockInset(uinset)) {
496 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
498 inset->localDispatch(cmd1);
499 if (the_locking_inset)
500 updateLocal(bv, false);
505 bool paste_internally = false;
506 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
507 localDispatch(FuncRequest(bv, LFUN_COPY));
508 paste_internally = true;
510 int old_top_y = bv->top_y();
512 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
513 // set the selection cursor!
514 text_.selection.cursor = text_.cursor;
515 text_.cursor.x_fix(text_.cursor.x());
517 text_.clearSelection();
518 updateLocal(bv, false);
520 bv->owner()->setLayout(cpar()->layout()->name());
522 // we moved the view we cannot do mouse selection in this case!
523 if (bv->top_y() != old_top_y)
526 // Insert primary selection with middle mouse
527 // if there is a local selection in the current buffer,
529 if (cmd.button() == mouse_button::button2) {
530 if (paste_internally)
531 localDispatch(FuncRequest(bv, LFUN_PASTE));
533 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
536 getLyXText(bv)->clearSelection();
541 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
543 BufferView * bv = cmd.view();
544 FuncRequest cmd1 = cmd;
549 if (the_locking_inset)
550 return the_locking_inset->localDispatch(cmd1);
553 int tmp_y = cmd.y + dim_.asc - bv->top_y();
554 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
557 // This code should probably be removed now. Simple insets
558 // (!highlyEditable) can actually take the localDispatch,
559 // and turn it into edit() if necessary. But we still
560 // need to deal properly with the whole relative vs.
561 // absolute mouse co-ords thing in a realiable, sensible way
563 if (isHighlyEditableInset(inset))
564 ret = inset->localDispatch(cmd1);
566 inset_x = cx(bv) - top_x;
568 cmd1.x = cmd.x - inset_x;
569 cmd1.y = cmd.x - inset_y;
570 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
574 ret = inset->localDispatch(cmd1);
575 updateLocal(bv, false);
582 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
584 FuncRequest cmd1 = cmd;
588 if (the_locking_inset) {
589 the_locking_inset->localDispatch(cmd1);
593 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
596 BufferView * bv = cmd.view();
597 LyXCursor cur = text_.cursor;
598 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
599 text_.cursor.x_fix(text_.cursor.x());
600 if (cur == text_.cursor)
602 text_.setSelection();
603 updateLocal(bv, false);
607 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
609 BufferView * bv = cmd.view();
612 switch (cmd.action) {
613 case LFUN_INSET_EDIT: {
614 UpdatableInset::localDispatch(cmd);
616 if (!bv->lockInset(this)) {
617 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
622 the_locking_inset = 0;
626 inset_boundary = false;
627 inset_par = paragraphs.end();
628 old_par = paragraphs.end();
631 if (cmd.argument.size()) {
632 if (cmd.argument == "left")
633 text_.setCursorIntern(paragraphs.begin(), 0);
635 ParagraphList::iterator it = boost::prior(paragraphs.end());
636 text_.setCursor(it, it->size());
639 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
640 // we put here -1 and not button as now the button in the
641 // edit call should not be needed we will fix this in 1.3.x
642 // cycle hopefully (Jug 20020509)
643 // FIXME: GUII I've changed this to none: probably WRONG
644 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
645 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
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
886 if (autoBreakRows_) {
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(Buffer const & buffer,
1332 std::vector<string> & list) const
1334 ParagraphList::const_iterator pit = paragraphs.begin();
1335 ParagraphList::const_iterator pend = paragraphs.end();
1336 for (; pit != pend; ++pit) {
1337 InsetList::const_iterator beg = pit->insetlist.begin();
1338 InsetList::const_iterator end = pit->insetlist.end();
1339 for (; beg != end; ++beg)
1340 beg->inset->getLabelList(buffer, list);
1345 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1348 if (the_locking_inset) {
1349 the_locking_inset->setFont(bv, font, toggleall, selectall);
1353 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1354 || cpar()->empty()) {
1355 text_.setFont(font, toggleall);
1360 if (text_.selection.set())
1361 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1365 text_.selection.cursor = text_.cursor;
1366 text_.cursorBottom();
1367 text_.setSelection();
1370 text_.toggleFree(font, toggleall);
1373 text_.clearSelection();
1376 updateLocal(bv, true);
1380 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1382 if (cpos() == cpar()->size())
1384 InsetOld * inset = cpar()->getInset(cpos());
1385 if (!isHighlyEditableInset(inset))
1387 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1388 inset->localDispatch(cmd);
1389 if (!the_locking_inset)
1391 updateLocal(bv, false);
1396 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1397 mouse_button::state button)
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;
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 LColor_color InsetText::frameColor() const
1468 return LColor::color(frame_color_);
1472 void InsetText::setFrameColor(LColor_color col)
1478 int InsetText::cx() const
1480 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1481 if (the_locking_inset) {
1482 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1483 if (font.isVisibleRightToLeft())
1484 x -= the_locking_inset->width();
1490 int InsetText::cy() const
1492 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1496 pos_type InsetText::cpos() const
1498 return text_.cursor.pos();
1502 ParagraphList::iterator InsetText::cpar() const
1504 return text_.cursor.par();
1508 bool InsetText::cboundary() const
1510 return text_.cursor.boundary();
1514 RowList::iterator InsetText::crow() const
1516 return text_.cursorRow();
1520 LyXText * InsetText::getLyXText(BufferView const * bv,
1521 bool const recursive) const
1524 if (recursive && the_locking_inset)
1525 return the_locking_inset->getLyXText(bv, true);
1530 void InsetText::setViewCache(BufferView const * bv) const
1533 if (bv != text_.bv_owner) {
1534 //lyxerr << "setting view cache from "
1535 // << text_.bv_owner << " to " << bv << "\n";
1536 text_.init(const_cast<BufferView *>(bv));
1538 text_.bv_owner = const_cast<BufferView *>(bv);
1543 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1546 /// then remove all LyXText in text-insets
1547 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1548 const_cast<ParagraphList&>(paragraphs).end(),
1549 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1554 void InsetText::removeNewlines()
1556 ParagraphList::iterator it = paragraphs.begin();
1557 ParagraphList::iterator end = paragraphs.end();
1558 for (; it != end; ++it)
1559 for (int i = 0; i < it->size(); ++i)
1560 if (it->isNewline(i))
1565 int InsetText::scroll(bool recursive) const
1567 int sx = UpdatableInset::scroll(false);
1569 if (recursive && the_locking_inset)
1570 sx += the_locking_inset->scroll(recursive);
1576 void InsetText::clearSelection(BufferView *)
1578 text_.clearSelection();
1582 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1584 Painter & pain = bv->painter();
1586 int h = dim_.asc + dim_.des;
1587 int ty = baseline - dim_.asc;
1593 if (ty + h > pain.paperHeight())
1594 h = pain.paperHeight();
1595 if (top_x + w > pain.paperWidth())
1596 w = pain.paperWidth();
1597 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1601 ParagraphList * InsetText::getParagraphs(int i) const
1603 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1607 LyXCursor const & InsetText::cursor(BufferView * bv) const
1609 if (the_locking_inset)
1610 return the_locking_inset->cursor(bv);
1611 return getLyXText(bv)->cursor;
1615 InsetOld * InsetText::getInsetFromID(int id_arg) const
1618 return const_cast<InsetText *>(this);
1620 ParagraphList::const_iterator pit = paragraphs.begin();
1621 ParagraphList::const_iterator pend = paragraphs.end();
1622 for (; pit != pend; ++pit) {
1623 InsetList::const_iterator it = pit->insetlist.begin();
1624 InsetList::const_iterator end = pit->insetlist.end();
1625 for (; it != end; ++it) {
1626 if (it->inset->id() == id_arg)
1628 InsetOld * in = it->inset->getInsetFromID(id_arg);
1638 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1641 if (the_locking_inset) {
1642 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1643 if (!word.word().empty()) {
1647 // we have to go on checking so move cursor to the next char
1648 text_.cursor.pos(text_.cursor.pos() + 1);
1650 word = text_.selectNextWordToSpellcheck(value);
1651 if (word.word().empty())
1652 bv->unlockInset(const_cast<InsetText *>(this));
1659 void InsetText::selectSelectedWord(BufferView * bv)
1661 if (the_locking_inset) {
1662 the_locking_inset->selectSelectedWord(bv);
1665 getLyXText(bv)->selectSelectedWord();
1666 updateLocal(bv, false);
1670 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1672 if (the_locking_inset) {
1673 if (the_locking_inset->nextChange(bv, length))
1675 text_.cursorRight(true);
1677 lyx::find::SearchResult result =
1678 lyx::find::findNextChange(bv, &text_, length);
1680 if (result == lyx::find::SR_FOUND) {
1681 LyXCursor cur = text_.cursor;
1682 bv->unlockInset(bv->theLockingInset());
1683 if (bv->lockInset(this))
1686 text_.setSelectionRange(length);
1687 updateLocal(bv, false);
1689 return result != lyx::find::SR_NOT_FOUND;
1693 bool InsetText::searchForward(BufferView * bv, string const & str,
1696 if (the_locking_inset) {
1697 if (the_locking_inset->searchForward(bv, str, cs, mw))
1699 text_.cursorRight(true);
1701 lyx::find::SearchResult result =
1702 lyx::find::find(bv, &text_, str, true, cs, mw);
1704 if (result == lyx::find::SR_FOUND) {
1705 LyXCursor cur = text_.cursor;
1706 bv->unlockInset(bv->theLockingInset());
1707 if (bv->lockInset(this))
1710 text_.setSelectionRange(str.length());
1711 updateLocal(bv, false);
1713 return result != lyx::find::SR_NOT_FOUND;
1717 bool InsetText::searchBackward(BufferView * bv, string const & str,
1720 if (the_locking_inset) {
1721 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1725 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1726 text_.setCursor(pit, pit->size());
1728 lyx::find::SearchResult result =
1729 lyx::find::find(bv, &text_, str, false, cs, mw);
1731 if (result == lyx::find::SR_FOUND) {
1732 LyXCursor cur = text_.cursor;
1733 bv->unlockInset(bv->theLockingInset());
1734 if (bv->lockInset(this))
1737 text_.setSelectionRange(str.length());
1738 updateLocal(bv, false);
1740 return result != lyx::find::SR_NOT_FOUND;
1744 bool InsetText::checkInsertChar(LyXFont & font)
1746 return owner() ? owner()->checkInsertChar(font) : true;
1750 void InsetText::collapseParagraphs(BufferView * bv)
1752 while (paragraphs.size() > 1) {
1753 ParagraphList::iterator first_par = paragraphs.begin();
1754 ParagraphList::iterator next_par = boost::next(first_par);
1755 size_t const first_par_size = first_par->size();
1757 if (!first_par->empty() &&
1758 !next_par->empty() &&
1759 !first_par->isSeparator(first_par_size - 1)) {
1760 first_par->insertChar(first_par_size, ' ');
1763 if (text_.selection.set()) {
1764 if (text_.selection.start.par() == next_par) {
1765 text_.selection.start.par(first_par);
1766 text_.selection.start.pos(
1767 text_.selection.start.pos() + first_par_size);
1769 if (text_.selection.end.par() == next_par) {
1770 text_.selection.end.par(first_par);
1771 text_.selection.end.pos(
1772 text_.selection.end.pos() + first_par_size);
1776 mergeParagraph(bv->buffer()->params(), paragraphs, first_par);
1781 void InsetText::getDrawFont(LyXFont & font) const
1785 owner()->getDrawFont(font);
1789 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1791 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1792 // And it probably does. You have to take a look at this John. (Lgb)
1793 #warning John, have a look here. (Lgb)
1794 ParagraphList::iterator pit = plist.begin();
1795 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1797 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1799 ParagraphList::iterator pend = plist.end();
1800 for (; pit != pend; ++pit)
1801 paragraphs.push_back(*pit);
1805 void InsetText::addPreview(PreviewLoader & loader) const
1807 ParagraphList::const_iterator pit = paragraphs.begin();
1808 ParagraphList::const_iterator pend = paragraphs.end();
1810 for (; pit != pend; ++pit) {
1811 InsetList::const_iterator it = pit->insetlist.begin();
1812 InsetList::const_iterator end = pit->insetlist.end();
1813 for (; it != end; ++it)
1814 it->inset->addPreview(loader);