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>
53 using namespace lyx::support;
54 using namespace lyx::graphics;
55 using namespace bv_funcs;
60 InsetText::InsetText(BufferParams const & bp)
61 : UpdatableInset(), text_(0, this, true, paragraphs)
63 paragraphs.push_back(Paragraph());
64 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
65 if (bp.tracking_changes)
66 paragraphs.begin()->trackChanges();
71 InsetText::InsetText(InsetText const & in)
72 : UpdatableInset(in), text_(0, this, true, paragraphs)
78 InsetText & InsetText::operator=(InsetText const & it)
85 void InsetText::init(InsetText const * ins)
88 textwidth_ = ins->textwidth_;
89 text_.bv_owner = ins->text_.bv_owner;
91 paragraphs = ins->paragraphs;
93 ParagraphList::iterator pit = paragraphs.begin();
94 ParagraphList::iterator end = paragraphs.end();
95 for (; pit != end; ++pit)
96 pit->setInsetOwner(this);
98 autoBreakRows = ins->autoBreakRows;
99 drawFrame_ = ins->drawFrame_;
100 frame_color = ins->frame_color;
102 textwidth_ = 0; // broken
104 frame_color = LColor::insetframe;
105 autoBreakRows = false;
107 the_locking_inset = 0;
108 for_each(paragraphs.begin(), paragraphs.end(),
109 boost::bind(&Paragraph::setInsetOwner, _1, this));
115 old_par = paragraphs.end();
116 in_insetAllowed = false;
120 void InsetText::clear(bool just_mark_erased)
122 if (just_mark_erased) {
123 ParagraphList::iterator it = paragraphs.begin();
124 ParagraphList::iterator end = paragraphs.end();
125 for (; it != end; ++it)
130 // This is a gross hack...
131 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
134 paragraphs.push_back(Paragraph());
135 paragraphs.begin()->setInsetOwner(this);
136 paragraphs.begin()->layout(old_layout);
140 auto_ptr<InsetBase> InsetText::clone() const
142 return auto_ptr<InsetBase>(new InsetText(*this));
146 void InsetText::write(Buffer const & buf, ostream & os) const
149 writeParagraphData(buf, os);
153 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
155 ParagraphList::const_iterator it = paragraphs.begin();
156 ParagraphList::const_iterator end = paragraphs.end();
157 Paragraph::depth_type dth = 0;
158 for (; it != end; ++it) {
159 it->write(buf, os, buf.params, dth);
164 void InsetText::read(Buffer const & buf, LyXLex & lex)
167 Paragraph::depth_type depth = 0;
171 if (buf.params.tracking_changes)
172 paragraphs.begin()->trackChanges();
174 // delete the initial paragraph
176 ParagraphList::iterator pit = paragraphs.begin();
180 token = lex.getString();
183 if (token == "\\end_inset") {
187 if (token == "\\end_document") {
188 lex.printError("\\end_document read in inset! Error in document!");
193 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
196 pit = paragraphs.begin();
197 ParagraphList::iterator const end = paragraphs.end();
198 for (; pit != end; ++pit)
199 pit->setInsetOwner(this);
201 if (token != "\\end_inset") {
202 lex.printError("Missing \\end_inset at this point. "
208 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
210 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
212 textwidth_ = mi.base.textwidth;
213 BufferView * bv = mi.base.bv;
215 text_.metrics(mi, dim);
216 dim.asc += TEXT_TO_INSET_OFFSET;
217 dim.des += TEXT_TO_INSET_OFFSET;
218 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
219 dim.wid = max(dim.wid, 10);
224 int InsetText::textWidth() const
230 void InsetText::draw(PainterInfo & pi, int x, int y) const
232 // update our idea of where we are. Clearly, we should
233 // not have to know this information.
236 int const start_x = x;
238 BufferView * bv = pi.base.bv;
239 Painter & pain = pi.pain;
241 // repaint the background if needed
242 if (backgroundColor() != LColor::background)
243 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
245 // no draw is necessary !!!
246 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
257 top_y = y - dim_.asc;
259 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
260 inset_x = cx() - x + drawTextXOffset;
261 inset_y = cy() + drawTextYOffset;
264 x += TEXT_TO_INSET_OFFSET;
266 paintTextInset(*bv, text_, x, y);
268 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
269 drawFrame(pain, start_x);
273 void InsetText::drawFrame(Painter & pain, int x) const
275 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
276 int const frame_x = x + ttoD2;
277 int const frame_y = top_baseline - dim_.asc + ttoD2;
278 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
279 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
280 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
284 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
289 if (!autoBreakRows && paragraphs.size() > 1)
290 collapseParagraphs(bv);
292 if (!text_.selection.set())
293 text_.selection.cursor = text_.cursor;
296 bv->updateInset(this);
297 bv->owner()->view_state_changed();
298 bv->owner()->updateMenubar();
299 bv->owner()->updateToolbar();
300 if (old_par != cpar()) {
301 bv->owner()->setLayout(cpar()->layout()->name());
307 string const InsetText::editMessage() const
309 return _("Opened Text Inset");
313 void InsetText::insetUnlock(BufferView * bv)
315 if (the_locking_inset) {
316 the_locking_inset->insetUnlock(bv);
317 the_locking_inset = 0;
318 updateLocal(bv, false);
323 if (text_.selection.set()) {
324 text_.clearSelection();
325 } else if (owner()) {
326 bv->owner()->setLayout(owner()->getLyXText(bv)
327 ->cursor.par()->layout()->name());
329 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
330 // hack for deleteEmptyParMech
331 ParagraphList::iterator first_par = paragraphs.begin();
332 if (!first_par->empty()) {
333 text_.setCursor(first_par, 0);
334 } else if (paragraphs.size() > 1) {
335 text_.setCursor(boost::next(first_par), 0);
340 void InsetText::lockInset(BufferView * bv)
343 the_locking_inset = 0;
344 inset_pos = inset_x = inset_y = 0;
345 inset_boundary = false;
346 inset_par = paragraphs.end();
347 old_par = paragraphs.end();
348 text_.setCursorIntern(paragraphs.begin(), 0);
349 text_.clearSelection();
351 // If the inset is empty set the language of the current font to the
352 // language to the surronding text (if different).
353 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
354 bv->getParentLanguage(this) != text_.current_font.language()) {
355 LyXFont font(LyXFont::ALL_IGNORE);
356 font.setLanguage(bv->getParentLanguage(this));
357 setFont(bv, font, false);
362 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
364 the_locking_inset = inset;
365 inset_x = cx() - top_x + drawTextXOffset;
366 inset_y = cy() + drawTextYOffset;
369 inset_boundary = cboundary();
373 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
375 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
376 << inset << "): " << endl;
379 if (!the_locking_inset) {
380 ParagraphList::iterator pit = paragraphs.begin();
381 ParagraphList::iterator pend = paragraphs.end();
383 int const id = inset->id();
384 for (; pit != pend; ++pit) {
385 InsetList::iterator it = pit->insetlist.begin();
386 InsetList::iterator const end = pit->insetlist.end();
387 for (; it != end; ++it) {
388 if (it->inset == inset) {
389 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
390 text_.setCursorIntern(pit, it->pos);
391 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
392 lyxerr << "bv: " << bv << " inset: " << inset << endl;
393 lockInset(bv, inset);
394 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
397 if (it->inset->getInsetFromID(id)) {
398 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
399 text_.setCursorIntern(pit, it->pos);
400 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
401 return the_locking_inset->lockInsetInInset(bv, inset);
405 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
408 if (inset == cpar()->getInset(cpos())) {
409 lyxerr[Debug::INSETS] << "OK" << endl;
410 lockInset(bv, inset);
414 if (the_locking_inset && the_locking_inset == inset) {
415 if (cpar() == inset_par && cpos() == inset_pos) {
416 lyxerr[Debug::INSETS] << "OK" << endl;
417 inset_x = cx() - top_x + drawTextXOffset;
418 inset_y = cy() + drawTextYOffset;
420 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
422 } else if (the_locking_inset) {
423 lyxerr[Debug::INSETS] << "MAYBE" << endl;
424 return the_locking_inset->lockInsetInInset(bv, inset);
426 lyxerr[Debug::INSETS] << "NOT OK" << endl;
431 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
434 if (!the_locking_inset)
436 if (the_locking_inset == inset) {
437 the_locking_inset->insetUnlock(bv);
438 the_locking_inset = 0;
440 moveRightIntern(bv, true, false);
441 old_par = paragraphs.end(); // force layout setting
445 updateLocal(bv, false);
448 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
452 void InsetText::lfunMousePress(FuncRequest const & cmd)
456 // use this to check mouse motion for selection!
460 BufferView * bv = cmd.view();
461 FuncRequest cmd1 = cmd;
467 int tmp_x = cmd.x - drawTextXOffset;
468 int tmp_y = cmd.y + dim_.asc - bv->top_y();
469 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
471 if (the_locking_inset) {
472 if (the_locking_inset == inset) {
473 the_locking_inset->localDispatch(cmd1);
476 // otherwise only unlock the_locking_inset
477 the_locking_inset->insetUnlock(bv);
478 the_locking_inset = 0;
481 no_selection = false;
483 if (bv->theLockingInset()) {
484 if (isHighlyEditableInset(inset)) {
485 // We just have to lock the inset before calling a
487 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
488 if (!bv->lockInset(uinset)) {
489 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
491 inset->localDispatch(cmd1);
492 if (the_locking_inset)
493 updateLocal(bv, false);
498 bool paste_internally = false;
499 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
500 localDispatch(FuncRequest(bv, LFUN_COPY));
501 paste_internally = true;
503 int old_top_y = bv->top_y();
505 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
507 // set the selection cursor!
508 text_.selection.cursor = text_.cursor;
509 text_.cursor.x_fix(text_.cursor.x());
511 text_.clearSelection();
512 updateLocal(bv, false);
514 bv->owner()->setLayout(cpar()->layout()->name());
516 // we moved the view we cannot do mouse selection in this case!
517 if (bv->top_y() != old_top_y)
520 // Insert primary selection with middle mouse
521 // if there is a local selection in the current buffer,
523 if (cmd.button() == mouse_button::button2) {
524 if (paste_internally)
525 localDispatch(FuncRequest(bv, LFUN_PASTE));
527 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
530 getLyXText(bv)->clearSelection();
535 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
537 BufferView * bv = cmd.view();
538 FuncRequest cmd1 = cmd;
543 if (the_locking_inset)
544 return the_locking_inset->localDispatch(cmd1);
546 int tmp_x = cmd.x - drawTextXOffset;
547 int tmp_y = cmd.y + dim_.asc - bv->top_y();
548 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
551 // This code should probably be removed now. Simple insets
552 // (!highlyEditable) can actually take the localDispatch,
553 // and turn it into edit() if necessary. But we still
554 // need to deal properly with the whole relative vs.
555 // absolute mouse co-ords thing in a realiable, sensible way
557 if (isHighlyEditableInset(inset))
558 ret = inset->localDispatch(cmd1);
560 inset_x = cx(bv) - top_x + drawTextXOffset;
561 inset_y = cy() + drawTextYOffset;
562 cmd1.x = cmd.x - inset_x;
563 cmd1.y = cmd.x - inset_y;
564 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
568 ret = inset->localDispatch(cmd1);
569 updateLocal(bv, false);
576 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
578 FuncRequest cmd1 = cmd;
582 if (the_locking_inset) {
583 the_locking_inset->localDispatch(cmd1);
587 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
590 BufferView * bv = cmd.view();
591 LyXCursor cur = text_.cursor;
592 text_.setCursorFromCoordinates
593 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
594 text_.cursor.x_fix(text_.cursor.x());
595 if (cur == text_.cursor)
597 text_.setSelection();
598 updateLocal(bv, false);
602 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
604 BufferView * bv = cmd.view();
607 switch (cmd.action) {
608 case LFUN_INSET_EDIT: {
609 UpdatableInset::localDispatch(cmd);
611 if (!bv->lockInset(this)) {
612 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
617 the_locking_inset = 0;
621 inset_boundary = false;
622 inset_par = paragraphs.end();
623 old_par = paragraphs.end();
626 if (cmd.argument.size()) {
627 if (cmd.argument == "left")
628 text_.setCursorIntern(paragraphs.begin(), 0);
630 ParagraphList::iterator it = boost::prior(paragraphs.end());
631 text_.setCursor(it, it->size());
634 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
635 // we put here -1 and not button as now the button in the
636 // edit call should not be needed we will fix this in 1.3.x
637 // cycle hopefully (Jug 20020509)
638 // FIXME: GUII I've changed this to none: probably WRONG
639 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
640 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
642 text_.cursor.x(text_.cursor.x());
643 text_.cursor.x_fix(text_.cursor.x());
647 text_.clearSelection();
650 // If the inset is empty set the language of the current font to the
651 // language to the surronding text (if different).
652 if (paragraphs.begin()->empty() &&
653 paragraphs.size() == 1 &&
654 bv->getParentLanguage(this) != text_.current_font.language())
656 LyXFont font(LyXFont::ALL_IGNORE);
657 font.setLanguage(bv->getParentLanguage(this));
658 setFont(bv, font, false);
661 updateLocal(bv, false);
662 // Tell the paragraph dialog that we've entered an insettext.
663 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
667 case LFUN_MOUSE_PRESS:
671 case LFUN_MOUSE_MOTION:
672 lfunMouseMotion(cmd);
675 case LFUN_MOUSE_RELEASE:
676 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
682 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
683 no_selection = false;
685 RESULT result = UpdatableInset::localDispatch(cmd);
686 if (result != UNDISPATCHED)
690 if (cmd.action < 0 && cmd.argument.empty())
693 if (the_locking_inset) {
694 result = the_locking_inset->localDispatch(cmd);
695 if (result == DISPATCHED_NOUPDATE)
697 if (result == DISPATCHED) {
698 updateLocal(bv, false);
701 if (result >= FINISHED) {
704 moveRightIntern(bv, false, false);
709 if (result >= FINISHED) {
710 updateLocal(bv, false);
711 bv->unlockInset(this);
715 result = moveDown(bv);
716 if (result >= FINISHED) {
717 updateLocal(bv, false);
718 bv->unlockInset(this);
725 the_locking_inset = 0;
726 updateLocal(bv, false);
727 // make sure status gets reset immediately
728 bv->owner()->clearMessage();
732 bool updflag = false;
734 switch (cmd.action) {
737 case LFUN_SELFINSERT:
738 if (bv->buffer()->isReadonly()) {
739 // setErrorMessage(N_("Document is read only"));
742 if (!cmd.argument.empty()) {
743 /* Automatically delete the currently selected
744 * text and replace it with what is being
745 * typed in now. Depends on lyxrc settings
746 * "auto_region_delete", which defaults to
749 // This should not be needed here and is also WRONG!
750 recordUndo(bv, Undo::INSERT, text_.cursor.par());
754 if (lyxrc.auto_region_delete && text_.selection.set())
755 text_.cutSelection(false, false);
756 text_.clearSelection();
758 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
759 bv->owner()->getIntl().getTransManager().
760 TranslateAndInsert(cmd.argument[i], &text_);
762 text_.selection.cursor = text_.cursor;
764 result = DISPATCHED_NOUPDATE;
767 // cursor movements that need special handling
770 result = moveRight(bv);
775 result = moveLeft(bv);
779 result = moveDown(bv);
787 if (crow() == text_.firstRow())
788 result = FINISHED_UP;
790 text_.cursorPrevious();
791 text_.clearSelection();
792 result = DISPATCHED_NOUPDATE;
797 if (crow() == text_.lastRow())
798 result = FINISHED_DOWN;
801 text_.clearSelection();
802 result = DISPATCHED_NOUPDATE;
807 if (text_.selection.set())
808 text_.cutSelection(true, false);
815 if (text_.selection.set())
816 text_.cutSelection(true, false);
823 if (!autoBreakRows) {
824 if (CutAndPaste::nrOfParagraphs() > 1) {
826 #warning FIXME horrendously bad UI
828 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
833 replaceSelection(bv->getLyXText());
834 size_t sel_index = 0;
835 string const & arg = cmd.argument;
836 if (isStrUnsignedInt(arg)) {
837 size_t const paste_arg = strToUnsignedInt(arg);
838 #warning FIXME Check if the arg is in the domain of available selections.
839 sel_index = paste_arg;
841 text_.pasteSelection(sel_index);
843 text_.clearSelection();
848 case LFUN_BREAKPARAGRAPH:
849 if (!autoBreakRows) {
853 replaceSelection(bv->getLyXText());
854 text_.breakParagraph(paragraphs, 0);
858 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
859 if (!autoBreakRows) {
863 replaceSelection(bv->getLyXText());
864 text_.breakParagraph(paragraphs, 1);
868 case LFUN_BREAKLINE: {
869 if (!autoBreakRows) {
874 replaceSelection(bv->getLyXText());
875 text_.insertInset(new InsetNewline);
881 // do not set layouts on non breakable textinsets
883 string cur_layout = cpar()->layout()->name();
885 // Derive layout number from given argument (string)
886 // and current buffer's textclass (number).
887 LyXTextClass const & tclass =
888 bv->buffer()->params.getLyXTextClass();
889 string layout = cmd.argument;
890 bool hasLayout = tclass.hasLayout(layout);
892 // If the entry is obsolete, use the new one instead.
894 string const & obs = tclass[layout]->obsoleted_by();
899 // see if we found the layout number:
901 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
902 bv->owner()->dispatch(lf);
906 if (cur_layout != layout) {
908 text_.setLayout(layout);
909 bv->owner()->setLayout(cpar()->layout()->name());
913 // reset the layout box
914 bv->owner()->setLayout(cpar()->layout()->name());
919 if (!bv->dispatch(cmd))
920 result = UNDISPATCHED;
924 updateLocal(bv, updflag);
926 /// If the action has deleted all text in the inset, we need to change the
927 // language to the language of the surronding text.
928 if (!was_empty && paragraphs.begin()->empty() &&
929 paragraphs.size() == 1) {
930 LyXFont font(LyXFont::ALL_IGNORE);
931 font.setLanguage(bv->getParentLanguage(this));
932 setFont(bv, font, false);
935 if (result >= FINISHED)
936 bv->unlockInset(this);
938 if (result == DISPATCHED_NOUPDATE)
944 int InsetText::latex(Buffer const & buf, ostream & os,
945 LatexRunParams const & runparams) const
948 latexParagraphs(buf, paragraphs, os, texrow, runparams);
949 return texrow.rows();
953 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
955 unsigned int lines = 0;
957 ParagraphList::const_iterator beg = paragraphs.begin();
958 ParagraphList::const_iterator end = paragraphs.end();
959 ParagraphList::const_iterator it = beg;
960 for (; it != end; ++it) {
961 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
962 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
969 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
971 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
972 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
974 // There is a confusion between the empty paragraph and the default paragraph
975 // The default paragraph is <p></p>, the empty paragraph is *empty*
976 // Since none of the floats of linuxdoc accepts standard paragraphs
977 // I disable them. I don't expect problems. (jamatos 2003/07/27)
978 for (; pit != pend; ++pit) {
979 const string name = pit->layout()->latexname();
981 sgml::openTag(os, 1, 0, name);
982 buf.simpleLinuxDocOnePar(os, pit, 0);
984 sgml::closeTag(os, 1, 0, name);
990 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
992 unsigned int lines = 0;
994 vector<string> environment_stack(10);
995 vector<string> environment_inner(10);
997 int const command_depth = 0;
1000 Paragraph::depth_type depth = 0; // paragraph depth
1002 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1003 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1005 for (; pit != pend; ++pit) {
1006 int desc_on = 0; // description mode
1008 LyXLayout_ptr const & style = pit->layout();
1010 // environment tag closing
1011 for (; depth > pit->params().depth(); --depth) {
1012 if (environment_inner[depth] != "!-- --") {
1013 item_name = "listitem";
1014 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1015 if (environment_inner[depth] == "varlistentry")
1016 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1018 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1019 environment_stack[depth].erase();
1020 environment_inner[depth].erase();
1023 if (depth == pit->params().depth()
1024 && environment_stack[depth] != style->latexname()
1025 && !environment_stack[depth].empty()) {
1026 if (environment_inner[depth] != "!-- --") {
1027 item_name= "listitem";
1028 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1029 if (environment_inner[depth] == "varlistentry")
1030 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1033 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1035 environment_stack[depth].erase();
1036 environment_inner[depth].erase();
1039 // Write opening SGML tags.
1040 switch (style->latextype) {
1041 case LATEX_PARAGRAPH:
1042 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1046 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1050 case LATEX_ENVIRONMENT:
1051 case LATEX_ITEM_ENVIRONMENT:
1052 if (depth < pit->params().depth()) {
1053 depth = pit->params().depth();
1054 environment_stack[depth].erase();
1057 if (environment_stack[depth] != style->latexname()) {
1058 if (environment_stack.size() == depth + 1) {
1059 environment_stack.push_back("!-- --");
1060 environment_inner.push_back("!-- --");
1062 environment_stack[depth] = style->latexname();
1063 environment_inner[depth] = "!-- --";
1064 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1066 if (environment_inner[depth] != "!-- --") {
1067 item_name= "listitem";
1068 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1069 if (environment_inner[depth] == "varlistentry")
1070 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1074 if (style->latextype == LATEX_ENVIRONMENT) {
1075 if (!style->latexparam().empty()) {
1076 if (style->latexparam() == "CDATA")
1079 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1084 desc_on = (style->labeltype == LABEL_MANUAL);
1086 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1087 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1089 item_name = desc_on ? "term" : "para";
1090 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1094 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1098 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1101 // write closing SGML tags
1102 switch (style->latextype) {
1103 case LATEX_ENVIRONMENT:
1104 if (!style->latexparam().empty()) {
1105 if (style->latexparam() == "CDATA")
1108 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1111 case LATEX_ITEM_ENVIRONMENT:
1115 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1117 case LATEX_PARAGRAPH:
1118 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1121 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1127 for (int d = depth; d >= 0; --d) {
1128 if (!environment_stack[depth].empty()) {
1129 if (environment_inner[depth] != "!-- --") {
1130 item_name = "listitem";
1131 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1132 if (environment_inner[depth] == "varlistentry")
1133 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1136 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1144 void InsetText::validate(LaTeXFeatures & features) const
1146 for_each(paragraphs.begin(), paragraphs.end(),
1147 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1151 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1153 if (the_locking_inset) {
1154 the_locking_inset->getCursor(bv, x, y);
1158 y = cy() + InsetText::y();
1162 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1164 if (the_locking_inset) {
1165 the_locking_inset->getCursorPos(bv, x, y);
1168 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1169 y = cy() - TEXT_TO_INSET_OFFSET;
1173 int InsetText::insetInInsetY() const
1175 if (!the_locking_inset)
1178 return inset_y + the_locking_inset->insetInInsetY();
1182 void InsetText::fitInsetCursor(BufferView * bv) const
1184 if (the_locking_inset) {
1185 the_locking_inset->fitInsetCursor(bv);
1189 LyXFont const font = text_.getFont(cpar(), cpos());
1191 int const asc = font_metrics::maxAscent(font);
1192 int const desc = font_metrics::maxDescent(font);
1194 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1198 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1200 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1201 return moveLeftIntern(bv, false, true, false);
1203 return moveRightIntern(bv, true, true, false);
1207 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1209 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1210 return moveRightIntern(bv, true, true, false);
1212 return moveLeftIntern(bv, false, true, false);
1217 InsetText::moveRightIntern(BufferView * bv, bool front,
1218 bool activate_inset, bool selecting)
1220 ParagraphList::iterator c_par = cpar();
1222 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1223 return FINISHED_RIGHT;
1224 if (activate_inset && checkAndActivateInset(bv, front))
1226 text_.cursorRight(bv);
1228 text_.clearSelection();
1229 return DISPATCHED_NOUPDATE;
1234 InsetText::moveLeftIntern(BufferView * bv, bool front,
1235 bool activate_inset, bool selecting)
1237 if (cpar() == paragraphs.begin() && cpos() <= 0)
1239 text_.cursorLeft(bv);
1241 text_.clearSelection();
1242 if (activate_inset && checkAndActivateInset(bv, front))
1244 return DISPATCHED_NOUPDATE;
1248 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1250 if (crow() == text_.firstRow())
1253 text_.clearSelection();
1254 return DISPATCHED_NOUPDATE;
1258 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1260 if (crow() == text_.lastRow())
1261 return FINISHED_DOWN;
1262 text_.cursorDown(bv);
1263 text_.clearSelection();
1264 return DISPATCHED_NOUPDATE;
1268 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1270 if (the_locking_inset) {
1271 if (the_locking_inset->insetAllowed(inset))
1272 return the_locking_inset->insertInset(bv, inset);
1275 inset->setOwner(this);
1276 text_.insertInset(inset);
1278 updateLocal(bv, true);
1283 bool InsetText::insetAllowed(InsetOld::Code code) const
1285 // in_insetAllowed is a really gross hack,
1286 // to allow us to call the owner's insetAllowed
1287 // without stack overflow, which can happen
1288 // when the owner uses InsetCollapsable::insetAllowed()
1290 if (in_insetAllowed)
1292 in_insetAllowed = true;
1293 if (the_locking_inset)
1294 ret = the_locking_inset->insetAllowed(code);
1296 ret = owner()->insetAllowed(code);
1297 in_insetAllowed = false;
1302 UpdatableInset * InsetText::getLockingInset() const
1304 return the_locking_inset ? the_locking_inset->getLockingInset() :
1305 const_cast<InsetText *>(this);
1309 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1313 if (the_locking_inset)
1314 return the_locking_inset->getFirstLockingInsetOfType(c);
1319 bool InsetText::showInsetDialog(BufferView * bv) const
1321 if (the_locking_inset)
1322 return the_locking_inset->showInsetDialog(bv);
1327 void InsetText::getLabelList(std::vector<string> & list) const
1329 ParagraphList::const_iterator pit = paragraphs.begin();
1330 ParagraphList::const_iterator pend = paragraphs.end();
1331 for (; pit != pend; ++pit) {
1332 InsetList::const_iterator beg = pit->insetlist.begin();
1333 InsetList::const_iterator end = pit->insetlist.end();
1334 for (; beg != end; ++beg)
1335 beg->inset->getLabelList(list);
1340 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1343 if (the_locking_inset) {
1344 the_locking_inset->setFont(bv, font, toggleall, selectall);
1348 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1349 || cpar()->empty()) {
1350 text_.setFont(font, toggleall);
1355 if (text_.selection.set())
1356 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1360 text_.selection.cursor = text_.cursor;
1361 text_.cursorBottom();
1362 text_.setSelection();
1365 text_.toggleFree(font, toggleall);
1368 text_.clearSelection();
1371 updateLocal(bv, true);
1375 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1377 if (cpos() == cpar()->size())
1379 InsetOld * inset = cpar()->getInset(cpos());
1380 if (!isHighlyEditableInset(inset))
1382 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1383 inset->localDispatch(cmd);
1384 if (!the_locking_inset)
1386 updateLocal(bv, false);
1391 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1392 mouse_button::state button)
1394 x -= drawTextXOffset;
1396 int dummyy = y + dim_.asc;
1397 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1398 // we only do the edit() call if the inset was hit by the mouse
1399 // or if it is a highly editable inset. So we should call this
1400 // function from our own edit with button < 0.
1401 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1403 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1412 inset_x = cx() - top_x + drawTextXOffset;
1413 inset_y = cy() + drawTextYOffset;
1414 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1415 inset->localDispatch(cmd);
1416 if (!the_locking_inset)
1418 updateLocal(bv, false);
1423 void InsetText::markNew(bool track_changes)
1425 ParagraphList::iterator pit = paragraphs.begin();
1426 ParagraphList::iterator end = paragraphs.end();
1427 for (; pit != end; ++pit) {
1428 if (track_changes) {
1429 pit->trackChanges();
1431 // no-op when not tracking
1432 pit->cleanChanges();
1438 void InsetText::setText(string const & data, LyXFont const & font)
1441 for (unsigned int i = 0; i < data.length(); ++i)
1442 paragraphs.begin()->insertChar(i, data[i], font);
1446 void InsetText::setAutoBreakRows(bool flag)
1448 if (flag != autoBreakRows) {
1449 autoBreakRows = flag;
1456 void InsetText::setDrawFrame(DrawFrame how)
1462 void InsetText::setFrameColor(LColor::color col)
1468 int InsetText::cx() const
1470 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1471 if (the_locking_inset) {
1472 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1473 if (font.isVisibleRightToLeft())
1474 x -= the_locking_inset->width();
1480 int InsetText::cy() const
1482 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1486 pos_type InsetText::cpos() const
1488 return text_.cursor.pos();
1492 ParagraphList::iterator InsetText::cpar() const
1494 return text_.cursor.par();
1498 bool InsetText::cboundary() const
1500 return text_.cursor.boundary();
1504 RowList::iterator InsetText::crow() const
1506 return text_.cursorRow();
1510 LyXText * InsetText::getLyXText(BufferView const * bv,
1511 bool const recursive) const
1514 if (recursive && the_locking_inset)
1515 return the_locking_inset->getLyXText(bv, true);
1520 void InsetText::setViewCache(BufferView const * bv) const
1523 if (bv != text_.bv_owner) {
1524 //lyxerr << "setting view cache from "
1525 // << text_.bv_owner << " to " << bv << "\n";
1526 text_.init(const_cast<BufferView *>(bv));
1528 text_.bv_owner = const_cast<BufferView *>(bv);
1533 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1536 /// then remove all LyXText in text-insets
1537 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1538 const_cast<ParagraphList&>(paragraphs).end(),
1539 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1544 void InsetText::removeNewlines()
1546 ParagraphList::iterator it = paragraphs.begin();
1547 ParagraphList::iterator end = paragraphs.end();
1548 for (; it != end; ++it)
1549 for (int i = 0; i < it->size(); ++i)
1550 if (it->isNewline(i))
1555 int InsetText::scroll(bool recursive) const
1557 int sx = UpdatableInset::scroll(false);
1559 if (recursive && the_locking_inset)
1560 sx += the_locking_inset->scroll(recursive);
1566 void InsetText::clearSelection(BufferView *)
1568 text_.clearSelection();
1572 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1574 Painter & pain = bv->painter();
1576 int h = dim_.asc + dim_.des;
1577 int ty = baseline - dim_.asc;
1583 if (ty + h > pain.paperHeight())
1584 h = pain.paperHeight();
1585 if (top_x + drawTextXOffset + w > pain.paperWidth())
1586 w = pain.paperWidth();
1587 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1591 ParagraphList * InsetText::getParagraphs(int i) const
1593 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1597 LyXCursor const & InsetText::cursor(BufferView * bv) const
1599 if (the_locking_inset)
1600 return the_locking_inset->cursor(bv);
1601 return getLyXText(bv)->cursor;
1605 InsetOld * InsetText::getInsetFromID(int id_arg) const
1608 return const_cast<InsetText *>(this);
1610 ParagraphList::const_iterator pit = paragraphs.begin();
1611 ParagraphList::const_iterator pend = paragraphs.end();
1612 for (; pit != pend; ++pit) {
1613 InsetList::const_iterator it = pit->insetlist.begin();
1614 InsetList::const_iterator end = pit->insetlist.end();
1615 for (; it != end; ++it) {
1616 if (it->inset->id() == id_arg)
1618 InsetOld * in = it->inset->getInsetFromID(id_arg);
1628 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1631 if (the_locking_inset) {
1632 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1633 if (!word.word().empty()) {
1637 // we have to go on checking so move cursor to the next char
1638 text_.cursor.pos(text_.cursor.pos() + 1);
1640 word = text_.selectNextWordToSpellcheck(value);
1641 if (word.word().empty())
1642 bv->unlockInset(const_cast<InsetText *>(this));
1649 void InsetText::selectSelectedWord(BufferView * bv)
1651 if (the_locking_inset) {
1652 the_locking_inset->selectSelectedWord(bv);
1655 getLyXText(bv)->selectSelectedWord();
1656 updateLocal(bv, false);
1660 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1662 if (the_locking_inset) {
1663 if (the_locking_inset->nextChange(bv, length))
1665 text_.cursorRight(true);
1667 lyx::find::SearchResult result =
1668 lyx::find::findNextChange(bv, &text_, length);
1670 if (result == lyx::find::SR_FOUND) {
1671 LyXCursor cur = text_.cursor;
1672 bv->unlockInset(bv->theLockingInset());
1673 if (bv->lockInset(this))
1676 text_.setSelectionRange(length);
1677 updateLocal(bv, false);
1679 return result != lyx::find::SR_NOT_FOUND;
1683 bool InsetText::searchForward(BufferView * bv, string const & str,
1686 if (the_locking_inset) {
1687 if (the_locking_inset->searchForward(bv, str, cs, mw))
1689 text_.cursorRight(true);
1691 lyx::find::SearchResult result =
1692 lyx::find::find(bv, &text_, str, true, cs, mw);
1694 if (result == lyx::find::SR_FOUND) {
1695 LyXCursor cur = text_.cursor;
1696 bv->unlockInset(bv->theLockingInset());
1697 if (bv->lockInset(this))
1700 text_.setSelectionRange(str.length());
1701 updateLocal(bv, false);
1703 return result != lyx::find::SR_NOT_FOUND;
1707 bool InsetText::searchBackward(BufferView * bv, string const & str,
1710 if (the_locking_inset) {
1711 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1715 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1716 text_.setCursor(pit, pit->size());
1718 lyx::find::SearchResult result =
1719 lyx::find::find(bv, &text_, str, false, cs, mw);
1721 if (result == lyx::find::SR_FOUND) {
1722 LyXCursor cur = text_.cursor;
1723 bv->unlockInset(bv->theLockingInset());
1724 if (bv->lockInset(this))
1727 text_.setSelectionRange(str.length());
1728 updateLocal(bv, false);
1730 return result != lyx::find::SR_NOT_FOUND;
1734 bool InsetText::checkInsertChar(LyXFont & font)
1736 return owner() ? owner()->checkInsertChar(font) : true;
1740 void InsetText::collapseParagraphs(BufferView * bv)
1742 while (paragraphs.size() > 1) {
1743 ParagraphList::iterator first_par = paragraphs.begin();
1744 ParagraphList::iterator next_par = boost::next(first_par);
1745 size_t const first_par_size = first_par->size();
1747 if (!first_par->empty() &&
1748 !next_par->empty() &&
1749 !first_par->isSeparator(first_par_size - 1)) {
1750 first_par->insertChar(first_par_size, ' ');
1753 if (text_.selection.set()) {
1754 if (text_.selection.start.par() == next_par) {
1755 text_.selection.start.par(first_par);
1756 text_.selection.start.pos(
1757 text_.selection.start.pos() + first_par_size);
1759 if (text_.selection.end.par() == next_par) {
1760 text_.selection.end.par(first_par);
1761 text_.selection.end.pos(
1762 text_.selection.end.pos() + first_par_size);
1766 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1771 void InsetText::getDrawFont(LyXFont & font) const
1775 owner()->getDrawFont(font);
1779 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1781 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1782 // And it probably does. You have to take a look at this John. (Lgb)
1783 #warning John, have a look here. (Lgb)
1784 ParagraphList::iterator pit = plist.begin();
1785 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1787 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1789 ParagraphList::iterator pend = plist.end();
1790 for (; pit != pend; ++pit)
1791 paragraphs.push_back(*pit);
1795 void InsetText::addPreview(PreviewLoader & loader) const
1797 ParagraphList::const_iterator pit = paragraphs.begin();
1798 ParagraphList::const_iterator pend = paragraphs.end();
1800 for (; pit != pend; ++pit) {
1801 InsetList::const_iterator it = pit->insetlist.begin();
1802 InsetList::const_iterator end = pit->insetlist.end();
1803 for (; it != end; ++it)
1804 it->inset->addPreview(loader);