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 "funcrequest.h"
26 #include "metricsinfo.h"
27 #include "paragraph_funcs.h"
28 #include "ParagraphParameters.h"
29 #include "rowpainter.h"
31 #include "undo_funcs.h"
32 #include "WordLangTuple.h"
34 #include "frontends/Alert.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Painter.h"
39 #include "support/lyxalgo.h" // lyx::count
41 #include <boost/bind.hpp>
54 using namespace lyx::support;
55 using namespace lyx::graphics;
56 using namespace bv_funcs;
59 using lyx::textclass_type;
62 InsetText::InsetText(BufferParams const & bp)
63 : UpdatableInset(), text_(0, this, true, paragraphs)
65 paragraphs.push_back(Paragraph());
66 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
67 if (bp.tracking_changes)
68 paragraphs.begin()->trackChanges();
73 InsetText::InsetText(InsetText const & in)
74 : UpdatableInset(in), text_(0, this, true, paragraphs)
80 InsetText & InsetText::operator=(InsetText const & it)
87 void InsetText::init(InsetText const * ins)
90 textwidth_ = ins->textwidth_;
91 text_.bv_owner = ins->text_.bv_owner;
93 paragraphs = ins->paragraphs;
95 ParagraphList::iterator pit = paragraphs.begin();
96 ParagraphList::iterator end = paragraphs.end();
97 for (; pit != end; ++pit)
98 pit->setInsetOwner(this);
100 autoBreakRows = ins->autoBreakRows;
101 drawFrame_ = ins->drawFrame_;
102 frame_color = ins->frame_color;
104 textwidth_ = 0; // broken
106 frame_color = LColor::insetframe;
107 autoBreakRows = false;
109 the_locking_inset = 0;
110 for_each(paragraphs.begin(), paragraphs.end(),
111 boost::bind(&Paragraph::setInsetOwner, _1, this));
117 old_par = paragraphs.end();
118 in_insetAllowed = false;
122 void InsetText::clear(bool just_mark_erased)
124 if (just_mark_erased) {
125 ParagraphList::iterator it = paragraphs.begin();
126 ParagraphList::iterator end = paragraphs.end();
127 for (; it != end; ++it)
132 // This is a gross hack...
133 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
136 paragraphs.push_back(Paragraph());
137 paragraphs.begin()->setInsetOwner(this);
138 paragraphs.begin()->layout(old_layout);
142 auto_ptr<InsetBase> InsetText::clone() const
144 return auto_ptr<InsetBase>(new InsetText(*this));
148 void InsetText::write(Buffer const & buf, ostream & os) const
151 writeParagraphData(buf, os);
155 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
157 ParagraphList::const_iterator it = paragraphs.begin();
158 ParagraphList::const_iterator end = paragraphs.end();
159 Paragraph::depth_type dth = 0;
160 for (; it != end; ++it) {
161 it->write(buf, os, buf.params, dth);
166 void InsetText::read(Buffer const & buf, LyXLex & lex)
169 Paragraph::depth_type depth = 0;
173 if (buf.params.tracking_changes)
174 paragraphs.begin()->trackChanges();
176 // delete the initial paragraph
178 ParagraphList::iterator pit = paragraphs.begin();
182 token = lex.getString();
185 if (token == "\\end_inset") {
189 if (token == "\\end_document") {
190 lex.printError("\\end_document read in inset! Error in document!");
195 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
198 pit = paragraphs.begin();
199 ParagraphList::iterator const end = paragraphs.end();
200 for (; pit != end; ++pit)
201 pit->setInsetOwner(this);
203 if (token != "\\end_inset") {
204 lex.printError("Missing \\end_inset at this point. "
210 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
212 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
214 textwidth_ = mi.base.textwidth;
215 BufferView * bv = mi.base.bv;
217 text_.metrics(mi, dim);
218 dim.asc += TEXT_TO_INSET_OFFSET;
219 dim.des += TEXT_TO_INSET_OFFSET;
220 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
221 dim.wid = max(dim.wid, 10);
226 int InsetText::textWidth() const
232 void InsetText::draw(PainterInfo & pi, int x, int y) const
234 // update our idea of where we are. Clearly, we should
235 // not have to know this information.
238 int const start_x = x;
240 BufferView * bv = pi.base.bv;
241 Painter & pain = pi.pain;
243 // repaint the background if needed
244 if (backgroundColor() != LColor::background)
245 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
247 // no draw is necessary !!!
248 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
259 top_y = y - dim_.asc;
261 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
262 inset_x = cx() - x + drawTextXOffset;
263 inset_y = cy() + drawTextYOffset;
266 x += TEXT_TO_INSET_OFFSET;
268 paintTextInset(*bv, text_, x, y);
270 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
271 drawFrame(pain, start_x);
275 void InsetText::drawFrame(Painter & pain, int x) const
277 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
278 int const frame_x = x + ttoD2;
279 int const frame_y = top_baseline - dim_.asc + ttoD2;
280 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
281 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
282 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
286 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
291 if (!autoBreakRows && paragraphs.size() > 1)
292 collapseParagraphs(bv);
294 if (!text_.selection.set())
295 text_.selection.cursor = text_.cursor;
298 bv->updateInset(this);
299 bv->owner()->view_state_changed();
300 bv->owner()->updateMenubar();
301 bv->owner()->updateToolbar();
302 if (old_par != cpar()) {
303 bv->owner()->setLayout(cpar()->layout()->name());
309 string const InsetText::editMessage() const
311 return _("Opened Text Inset");
315 void InsetText::insetUnlock(BufferView * bv)
317 if (the_locking_inset) {
318 the_locking_inset->insetUnlock(bv);
319 the_locking_inset = 0;
320 updateLocal(bv, false);
325 if (text_.selection.set()) {
326 text_.clearSelection();
327 } else if (owner()) {
328 bv->owner()->setLayout(owner()->getLyXText(bv)
329 ->cursor.par()->layout()->name());
331 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
332 // hack for deleteEmptyParMech
333 ParagraphList::iterator first_par = paragraphs.begin();
334 if (!first_par->empty()) {
335 text_.setCursor(first_par, 0);
336 } else if (paragraphs.size() > 1) {
337 text_.setCursor(boost::next(first_par), 0);
342 void InsetText::lockInset(BufferView * bv)
345 the_locking_inset = 0;
346 inset_pos = inset_x = inset_y = 0;
347 inset_boundary = false;
348 inset_par = paragraphs.end();
349 old_par = paragraphs.end();
350 text_.setCursorIntern(paragraphs.begin(), 0);
351 text_.clearSelection();
353 // If the inset is empty set the language of the current font to the
354 // language to the surronding text (if different).
355 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
356 bv->getParentLanguage(this) != text_.current_font.language()) {
357 LyXFont font(LyXFont::ALL_IGNORE);
358 font.setLanguage(bv->getParentLanguage(this));
359 setFont(bv, font, false);
364 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
366 the_locking_inset = inset;
367 inset_x = cx() - top_x + drawTextXOffset;
368 inset_y = cy() + drawTextYOffset;
371 inset_boundary = cboundary();
375 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
377 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
378 << inset << "): " << endl;
381 if (!the_locking_inset) {
382 ParagraphList::iterator pit = paragraphs.begin();
383 ParagraphList::iterator pend = paragraphs.end();
385 int const id = inset->id();
386 for (; pit != pend; ++pit) {
387 InsetList::iterator it = pit->insetlist.begin();
388 InsetList::iterator const end = pit->insetlist.end();
389 for (; it != end; ++it) {
390 if (it->inset == inset) {
391 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
392 text_.setCursorIntern(pit, it->pos);
393 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
394 lyxerr << "bv: " << bv << " inset: " << inset << endl;
395 lockInset(bv, inset);
396 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
399 if (it->inset->getInsetFromID(id)) {
400 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
401 text_.setCursorIntern(pit, it->pos);
402 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
403 return the_locking_inset->lockInsetInInset(bv, inset);
407 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
410 if (inset == cpar()->getInset(cpos())) {
411 lyxerr[Debug::INSETS] << "OK" << endl;
412 lockInset(bv, inset);
416 if (the_locking_inset && the_locking_inset == inset) {
417 if (cpar() == inset_par && cpos() == inset_pos) {
418 lyxerr[Debug::INSETS] << "OK" << endl;
419 inset_x = cx() - top_x + drawTextXOffset;
420 inset_y = cy() + drawTextYOffset;
422 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
424 } else if (the_locking_inset) {
425 lyxerr[Debug::INSETS] << "MAYBE" << endl;
426 return the_locking_inset->lockInsetInInset(bv, inset);
428 lyxerr[Debug::INSETS] << "NOT OK" << endl;
433 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
436 if (!the_locking_inset)
438 if (the_locking_inset == inset) {
439 the_locking_inset->insetUnlock(bv);
440 the_locking_inset = 0;
442 moveRightIntern(bv, true, false);
443 old_par = paragraphs.end(); // force layout setting
447 updateLocal(bv, false);
450 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
454 void InsetText::lfunMousePress(FuncRequest const & cmd)
458 // use this to check mouse motion for selection!
462 BufferView * bv = cmd.view();
463 FuncRequest cmd1 = cmd;
469 int tmp_x = cmd.x - drawTextXOffset;
470 int tmp_y = cmd.y + dim_.asc - bv->top_y();
471 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
473 if (the_locking_inset) {
474 if (the_locking_inset == inset) {
475 the_locking_inset->localDispatch(cmd1);
478 // otherwise only unlock the_locking_inset
479 the_locking_inset->insetUnlock(bv);
480 the_locking_inset = 0;
483 no_selection = false;
485 if (bv->theLockingInset()) {
486 if (isHighlyEditableInset(inset)) {
487 // We just have to lock the inset before calling a
489 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
490 if (!bv->lockInset(uinset)) {
491 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
493 inset->localDispatch(cmd1);
494 if (the_locking_inset)
495 updateLocal(bv, false);
500 bool paste_internally = false;
501 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
502 localDispatch(FuncRequest(bv, LFUN_COPY));
503 paste_internally = true;
505 int old_top_y = bv->top_y();
507 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
509 // set the selection cursor!
510 text_.selection.cursor = text_.cursor;
511 text_.cursor.x_fix(text_.cursor.x());
513 text_.clearSelection();
514 updateLocal(bv, false);
516 bv->owner()->setLayout(cpar()->layout()->name());
518 // we moved the view we cannot do mouse selection in this case!
519 if (bv->top_y() != old_top_y)
522 // Insert primary selection with middle mouse
523 // if there is a local selection in the current buffer,
525 if (cmd.button() == mouse_button::button2) {
526 if (paste_internally)
527 localDispatch(FuncRequest(bv, LFUN_PASTE));
529 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
532 getLyXText(bv)->clearSelection();
537 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
539 BufferView * bv = cmd.view();
540 FuncRequest cmd1 = cmd;
545 if (the_locking_inset)
546 return the_locking_inset->localDispatch(cmd1);
548 int tmp_x = cmd.x - drawTextXOffset;
549 int tmp_y = cmd.y + dim_.asc - bv->top_y();
550 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
553 // This code should probably be removed now. Simple insets
554 // (!highlyEditable) can actually take the localDispatch,
555 // and turn it into edit() if necessary. But we still
556 // need to deal properly with the whole relative vs.
557 // absolute mouse co-ords thing in a realiable, sensible way
559 if (isHighlyEditableInset(inset))
560 ret = inset->localDispatch(cmd1);
562 inset_x = cx(bv) - top_x + drawTextXOffset;
563 inset_y = cy() + drawTextYOffset;
564 cmd1.x = cmd.x - inset_x;
565 cmd1.y = cmd.x - inset_y;
566 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
570 ret = inset->localDispatch(cmd1);
571 updateLocal(bv, false);
578 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
580 FuncRequest cmd1 = cmd;
584 if (the_locking_inset) {
585 the_locking_inset->localDispatch(cmd1);
589 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
592 BufferView * bv = cmd.view();
593 LyXCursor cur = text_.cursor;
594 text_.setCursorFromCoordinates
595 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
596 text_.cursor.x_fix(text_.cursor.x());
597 if (cur == text_.cursor)
599 text_.setSelection();
600 updateLocal(bv, false);
604 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
606 BufferView * bv = cmd.view();
609 switch (cmd.action) {
610 case LFUN_INSET_EDIT: {
611 UpdatableInset::localDispatch(cmd);
613 if (!bv->lockInset(this)) {
614 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
619 the_locking_inset = 0;
623 inset_boundary = false;
624 inset_par = paragraphs.end();
625 old_par = paragraphs.end();
628 if (cmd.argument.size()) {
629 if (cmd.argument == "left")
630 text_.setCursorIntern(paragraphs.begin(), 0);
632 ParagraphList::iterator it = boost::prior(paragraphs.end());
633 text_.setCursor(it, it->size());
636 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
637 // we put here -1 and not button as now the button in the
638 // edit call should not be needed we will fix this in 1.3.x
639 // cycle hopefully (Jug 20020509)
640 // FIXME: GUII I've changed this to none: probably WRONG
641 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
642 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
644 text_.cursor.x(text_.cursor.x());
645 text_.cursor.x_fix(text_.cursor.x());
649 text_.clearSelection();
652 // If the inset is empty set the language of the current font to the
653 // language to the surronding text (if different).
654 if (paragraphs.begin()->empty() &&
655 paragraphs.size() == 1 &&
656 bv->getParentLanguage(this) != text_.current_font.language())
658 LyXFont font(LyXFont::ALL_IGNORE);
659 font.setLanguage(bv->getParentLanguage(this));
660 setFont(bv, font, false);
663 updateLocal(bv, false);
664 // Tell the paragraph dialog that we've entered an insettext.
665 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
669 case LFUN_MOUSE_PRESS:
673 case LFUN_MOUSE_MOTION:
674 lfunMouseMotion(cmd);
677 case LFUN_MOUSE_RELEASE:
678 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
684 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
685 no_selection = false;
687 RESULT result = UpdatableInset::localDispatch(cmd);
688 if (result != UNDISPATCHED)
692 if (cmd.action < 0 && cmd.argument.empty())
695 if (the_locking_inset) {
696 result = the_locking_inset->localDispatch(cmd);
697 if (result == DISPATCHED_NOUPDATE)
699 if (result == DISPATCHED) {
700 updateLocal(bv, false);
703 if (result >= FINISHED) {
706 moveRightIntern(bv, false, false);
711 if (result >= FINISHED) {
712 updateLocal(bv, false);
713 bv->unlockInset(this);
717 result = moveDown(bv);
718 if (result >= FINISHED) {
719 updateLocal(bv, false);
720 bv->unlockInset(this);
727 the_locking_inset = 0;
728 updateLocal(bv, false);
729 // make sure status gets reset immediately
730 bv->owner()->clearMessage();
734 bool updflag = false;
736 switch (cmd.action) {
739 case LFUN_SELFINSERT:
740 if (bv->buffer()->isReadonly()) {
741 // setErrorMessage(N_("Document is read only"));
744 if (!cmd.argument.empty()) {
745 /* Automatically delete the currently selected
746 * text and replace it with what is being
747 * typed in now. Depends on lyxrc settings
748 * "auto_region_delete", which defaults to
751 // This should not be needed here and is also WRONG!
752 recordUndo(bv, Undo::INSERT, text_.cursor.par());
756 if (lyxrc.auto_region_delete && text_.selection.set())
757 text_.cutSelection(false, false);
758 text_.clearSelection();
760 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
761 bv->owner()->getIntl().getTransManager().
762 TranslateAndInsert(cmd.argument[i], &text_);
764 text_.selection.cursor = text_.cursor;
766 result = DISPATCHED_NOUPDATE;
769 // cursor movements that need special handling
772 result = moveRight(bv);
777 result = moveLeft(bv);
781 result = moveDown(bv);
789 if (crow() == text_.firstRow())
790 result = FINISHED_UP;
792 text_.cursorPrevious();
793 text_.clearSelection();
794 result = DISPATCHED_NOUPDATE;
799 if (crow() == text_.lastRow())
800 result = FINISHED_DOWN;
803 text_.clearSelection();
804 result = DISPATCHED_NOUPDATE;
809 if (text_.selection.set())
810 text_.cutSelection(true, false);
817 if (text_.selection.set())
818 text_.cutSelection(true, false);
825 if (!autoBreakRows) {
826 if (CutAndPaste::nrOfParagraphs() > 1) {
828 #warning FIXME horrendously bad UI
830 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
835 replaceSelection(bv->getLyXText());
836 size_t sel_index = 0;
837 string const & arg = cmd.argument;
838 if (isStrUnsignedInt(arg)) {
839 size_t const paste_arg = strToUnsignedInt(arg);
840 #warning FIXME Check if the arg is in the domain of available selections.
841 sel_index = paste_arg;
843 text_.pasteSelection(sel_index);
845 text_.clearSelection();
850 case LFUN_BREAKPARAGRAPH:
851 if (!autoBreakRows) {
855 replaceSelection(bv->getLyXText());
856 text_.breakParagraph(paragraphs, 0);
860 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
861 if (!autoBreakRows) {
865 replaceSelection(bv->getLyXText());
866 text_.breakParagraph(paragraphs, 1);
870 case LFUN_BREAKLINE: {
871 if (!autoBreakRows) {
876 replaceSelection(bv->getLyXText());
877 text_.insertInset(new InsetNewline);
883 // do not set layouts on non breakable textinsets
885 string cur_layout = cpar()->layout()->name();
887 // Derive layout number from given argument (string)
888 // and current buffer's textclass (number).
889 LyXTextClass const & tclass =
890 bv->buffer()->params.getLyXTextClass();
891 string layout = cmd.argument;
892 bool hasLayout = tclass.hasLayout(layout);
894 // If the entry is obsolete, use the new one instead.
896 string const & obs = tclass[layout]->obsoleted_by();
901 // see if we found the layout number:
903 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
904 bv->owner()->dispatch(lf);
908 if (cur_layout != layout) {
910 text_.setLayout(layout);
911 bv->owner()->setLayout(cpar()->layout()->name());
915 // reset the layout box
916 bv->owner()->setLayout(cpar()->layout()->name());
921 if (!bv->dispatch(cmd))
922 result = UNDISPATCHED;
926 updateLocal(bv, updflag);
928 /// If the action has deleted all text in the inset, we need to change the
929 // language to the language of the surronding text.
930 if (!was_empty && paragraphs.begin()->empty() &&
931 paragraphs.size() == 1) {
932 LyXFont font(LyXFont::ALL_IGNORE);
933 font.setLanguage(bv->getParentLanguage(this));
934 setFont(bv, font, false);
937 if (result >= FINISHED)
938 bv->unlockInset(this);
940 if (result == DISPATCHED_NOUPDATE)
946 int InsetText::latex(Buffer const & buf, ostream & os,
947 LatexRunParams const & runparams) const
950 latexParagraphs(buf, paragraphs, os, texrow, runparams);
951 return texrow.rows();
955 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
957 unsigned int lines = 0;
959 ParagraphList::const_iterator beg = paragraphs.begin();
960 ParagraphList::const_iterator end = paragraphs.end();
961 ParagraphList::const_iterator it = beg;
962 for (; it != end; ++it) {
963 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
964 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
971 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
973 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
974 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
976 // There is a confusion between the empty paragraph and the default paragraph
977 // The default paragraph is <p></p>, the empty paragraph is *empty*
978 // Since none of the floats of linuxdoc accepts standard paragraphs
979 // I disable them. I don't expect problems. (jamatos 2003/07/27)
980 for (; pit != pend; ++pit) {
981 const string name = pit->layout()->latexname();
983 sgml::openTag(os, 1, 0, name);
984 buf.simpleLinuxDocOnePar(os, pit, 0);
986 sgml::closeTag(os, 1, 0, name);
992 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
994 unsigned int lines = 0;
996 vector<string> environment_stack(10);
997 vector<string> environment_inner(10);
999 int const command_depth = 0;
1002 Paragraph::depth_type depth = 0; // paragraph depth
1004 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1005 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1007 for (; pit != pend; ++pit) {
1008 int desc_on = 0; // description mode
1010 LyXLayout_ptr const & style = pit->layout();
1012 // environment tag closing
1013 for (; depth > pit->params().depth(); --depth) {
1014 if (environment_inner[depth] != "!-- --") {
1015 item_name = "listitem";
1016 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1017 if (environment_inner[depth] == "varlistentry")
1018 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1020 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1021 environment_stack[depth].erase();
1022 environment_inner[depth].erase();
1025 if (depth == pit->params().depth()
1026 && environment_stack[depth] != style->latexname()
1027 && !environment_stack[depth].empty()) {
1028 if (environment_inner[depth] != "!-- --") {
1029 item_name= "listitem";
1030 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1031 if (environment_inner[depth] == "varlistentry")
1032 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1035 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1037 environment_stack[depth].erase();
1038 environment_inner[depth].erase();
1041 // Write opening SGML tags.
1042 switch (style->latextype) {
1043 case LATEX_PARAGRAPH:
1044 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1048 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1052 case LATEX_ENVIRONMENT:
1053 case LATEX_ITEM_ENVIRONMENT:
1054 if (depth < pit->params().depth()) {
1055 depth = pit->params().depth();
1056 environment_stack[depth].erase();
1059 if (environment_stack[depth] != style->latexname()) {
1060 if (environment_stack.size() == depth + 1) {
1061 environment_stack.push_back("!-- --");
1062 environment_inner.push_back("!-- --");
1064 environment_stack[depth] = style->latexname();
1065 environment_inner[depth] = "!-- --";
1066 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1068 if (environment_inner[depth] != "!-- --") {
1069 item_name= "listitem";
1070 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1071 if (environment_inner[depth] == "varlistentry")
1072 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1076 if (style->latextype == LATEX_ENVIRONMENT) {
1077 if (!style->latexparam().empty()) {
1078 if (style->latexparam() == "CDATA")
1081 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1086 desc_on = (style->labeltype == LABEL_MANUAL);
1088 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1089 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1091 item_name = desc_on ? "term" : "para";
1092 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1096 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1100 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1103 // write closing SGML tags
1104 switch (style->latextype) {
1105 case LATEX_ENVIRONMENT:
1106 if (!style->latexparam().empty()) {
1107 if (style->latexparam() == "CDATA")
1110 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1113 case LATEX_ITEM_ENVIRONMENT:
1117 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1119 case LATEX_PARAGRAPH:
1120 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1123 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1129 for (int d = depth; d >= 0; --d) {
1130 if (!environment_stack[depth].empty()) {
1131 if (environment_inner[depth] != "!-- --") {
1132 item_name = "listitem";
1133 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1134 if (environment_inner[depth] == "varlistentry")
1135 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1138 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1146 void InsetText::validate(LaTeXFeatures & features) const
1148 for_each(paragraphs.begin(), paragraphs.end(),
1149 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1153 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1155 if (the_locking_inset) {
1156 the_locking_inset->getCursor(bv, x, y);
1160 y = cy() + InsetText::y();
1164 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1166 if (the_locking_inset) {
1167 the_locking_inset->getCursorPos(bv, x, y);
1170 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1171 y = cy() - TEXT_TO_INSET_OFFSET;
1175 int InsetText::insetInInsetY() const
1177 if (!the_locking_inset)
1180 return inset_y + the_locking_inset->insetInInsetY();
1184 void InsetText::fitInsetCursor(BufferView * bv) const
1186 if (the_locking_inset) {
1187 the_locking_inset->fitInsetCursor(bv);
1191 LyXFont const font = text_.getFont(cpar(), cpos());
1193 int const asc = font_metrics::maxAscent(font);
1194 int const desc = font_metrics::maxDescent(font);
1196 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1200 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1202 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1203 return moveLeftIntern(bv, false, true, false);
1205 return moveRightIntern(bv, true, true, false);
1209 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1211 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1212 return moveRightIntern(bv, true, true, false);
1214 return moveLeftIntern(bv, false, true, false);
1219 InsetText::moveRightIntern(BufferView * bv, bool front,
1220 bool activate_inset, bool selecting)
1222 ParagraphList::iterator c_par = cpar();
1224 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1225 return FINISHED_RIGHT;
1226 if (activate_inset && checkAndActivateInset(bv, front))
1228 text_.cursorRight(bv);
1230 text_.clearSelection();
1231 return DISPATCHED_NOUPDATE;
1236 InsetText::moveLeftIntern(BufferView * bv, bool front,
1237 bool activate_inset, bool selecting)
1239 if (cpar() == paragraphs.begin() && cpos() <= 0)
1241 text_.cursorLeft(bv);
1243 text_.clearSelection();
1244 if (activate_inset && checkAndActivateInset(bv, front))
1246 return DISPATCHED_NOUPDATE;
1250 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1252 if (crow() == text_.firstRow())
1255 text_.clearSelection();
1256 return DISPATCHED_NOUPDATE;
1260 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1262 if (crow() == text_.lastRow())
1263 return FINISHED_DOWN;
1264 text_.cursorDown(bv);
1265 text_.clearSelection();
1266 return DISPATCHED_NOUPDATE;
1270 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1272 if (the_locking_inset) {
1273 if (the_locking_inset->insetAllowed(inset))
1274 return the_locking_inset->insertInset(bv, inset);
1277 inset->setOwner(this);
1278 text_.insertInset(inset);
1280 updateLocal(bv, true);
1285 bool InsetText::insetAllowed(InsetOld::Code code) const
1287 // in_insetAllowed is a really gross hack,
1288 // to allow us to call the owner's insetAllowed
1289 // without stack overflow, which can happen
1290 // when the owner uses InsetCollapsable::insetAllowed()
1292 if (in_insetAllowed)
1294 in_insetAllowed = true;
1295 if (the_locking_inset)
1296 ret = the_locking_inset->insetAllowed(code);
1298 ret = owner()->insetAllowed(code);
1299 in_insetAllowed = false;
1304 UpdatableInset * InsetText::getLockingInset() const
1306 return the_locking_inset ? the_locking_inset->getLockingInset() :
1307 const_cast<InsetText *>(this);
1311 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1315 if (the_locking_inset)
1316 return the_locking_inset->getFirstLockingInsetOfType(c);
1321 bool InsetText::showInsetDialog(BufferView * bv) const
1323 if (the_locking_inset)
1324 return the_locking_inset->showInsetDialog(bv);
1329 void InsetText::getLabelList(std::vector<string> & list) const
1331 ParagraphList::const_iterator pit = paragraphs.begin();
1332 ParagraphList::const_iterator pend = paragraphs.end();
1333 for (; pit != pend; ++pit) {
1334 InsetList::const_iterator beg = pit->insetlist.begin();
1335 InsetList::const_iterator end = pit->insetlist.end();
1336 for (; beg != end; ++beg)
1337 beg->inset->getLabelList(list);
1342 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1345 if (the_locking_inset) {
1346 the_locking_inset->setFont(bv, font, toggleall, selectall);
1350 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1351 || cpar()->empty()) {
1352 text_.setFont(font, toggleall);
1357 if (text_.selection.set())
1358 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1362 text_.selection.cursor = text_.cursor;
1363 text_.cursorBottom();
1364 text_.setSelection();
1367 text_.toggleFree(font, toggleall);
1370 text_.clearSelection();
1373 updateLocal(bv, true);
1377 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1379 if (cpos() == cpar()->size())
1381 InsetOld * inset = cpar()->getInset(cpos());
1382 if (!isHighlyEditableInset(inset))
1384 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1385 inset->localDispatch(cmd);
1386 if (!the_locking_inset)
1388 updateLocal(bv, false);
1393 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1394 mouse_button::state button)
1396 x -= drawTextXOffset;
1398 int dummyy = y + dim_.asc;
1399 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1400 // we only do the edit() call if the inset was hit by the mouse
1401 // or if it is a highly editable inset. So we should call this
1402 // function from our own edit with button < 0.
1403 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1405 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1414 inset_x = cx() - top_x + drawTextXOffset;
1415 inset_y = cy() + drawTextYOffset;
1416 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1417 inset->localDispatch(cmd);
1418 if (!the_locking_inset)
1420 updateLocal(bv, false);
1425 void InsetText::markNew(bool track_changes)
1427 ParagraphList::iterator pit = paragraphs.begin();
1428 ParagraphList::iterator end = paragraphs.end();
1429 for (; pit != end; ++pit) {
1430 if (track_changes) {
1431 pit->trackChanges();
1433 // no-op when not tracking
1434 pit->cleanChanges();
1440 void InsetText::setText(string const & data, LyXFont const & font)
1443 for (unsigned int i = 0; i < data.length(); ++i)
1444 paragraphs.begin()->insertChar(i, data[i], font);
1448 void InsetText::setAutoBreakRows(bool flag)
1450 if (flag != autoBreakRows) {
1451 autoBreakRows = flag;
1458 void InsetText::setDrawFrame(DrawFrame how)
1464 void InsetText::setFrameColor(LColor::color col)
1470 int InsetText::cx() const
1472 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1473 if (the_locking_inset) {
1474 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1475 if (font.isVisibleRightToLeft())
1476 x -= the_locking_inset->width();
1482 int InsetText::cy() const
1484 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1488 pos_type InsetText::cpos() const
1490 return text_.cursor.pos();
1494 ParagraphList::iterator InsetText::cpar() const
1496 return text_.cursor.par();
1500 bool InsetText::cboundary() const
1502 return text_.cursor.boundary();
1506 RowList::iterator InsetText::crow() const
1508 return text_.cursorRow();
1512 LyXText * InsetText::getLyXText(BufferView const * bv,
1513 bool const recursive) const
1516 if (recursive && the_locking_inset)
1517 return the_locking_inset->getLyXText(bv, true);
1522 void InsetText::setViewCache(BufferView const * bv) const
1525 if (bv != text_.bv_owner) {
1526 //lyxerr << "setting view cache from "
1527 // << text_.bv_owner << " to " << bv << "\n";
1528 text_.init(const_cast<BufferView *>(bv));
1530 text_.bv_owner = const_cast<BufferView *>(bv);
1535 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1538 /// then remove all LyXText in text-insets
1539 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1540 const_cast<ParagraphList&>(paragraphs).end(),
1541 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1546 void InsetText::removeNewlines()
1548 ParagraphList::iterator it = paragraphs.begin();
1549 ParagraphList::iterator end = paragraphs.end();
1550 for (; it != end; ++it)
1551 for (int i = 0; i < it->size(); ++i)
1552 if (it->isNewline(i))
1557 int InsetText::scroll(bool recursive) const
1559 int sx = UpdatableInset::scroll(false);
1561 if (recursive && the_locking_inset)
1562 sx += the_locking_inset->scroll(recursive);
1568 void InsetText::clearSelection(BufferView *)
1570 text_.clearSelection();
1574 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1576 Painter & pain = bv->painter();
1578 int h = dim_.asc + dim_.des;
1579 int ty = baseline - dim_.asc;
1585 if (ty + h > pain.paperHeight())
1586 h = pain.paperHeight();
1587 if (top_x + drawTextXOffset + w > pain.paperWidth())
1588 w = pain.paperWidth();
1589 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1593 ParagraphList * InsetText::getParagraphs(int i) const
1595 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1599 LyXCursor const & InsetText::cursor(BufferView * bv) const
1601 if (the_locking_inset)
1602 return the_locking_inset->cursor(bv);
1603 return getLyXText(bv)->cursor;
1607 InsetOld * InsetText::getInsetFromID(int id_arg) const
1610 return const_cast<InsetText *>(this);
1612 ParagraphList::const_iterator pit = paragraphs.begin();
1613 ParagraphList::const_iterator pend = paragraphs.end();
1614 for (; pit != pend; ++pit) {
1615 InsetList::const_iterator it = pit->insetlist.begin();
1616 InsetList::const_iterator end = pit->insetlist.end();
1617 for (; it != end; ++it) {
1618 if (it->inset->id() == id_arg)
1620 InsetOld * in = it->inset->getInsetFromID(id_arg);
1630 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1633 if (the_locking_inset) {
1634 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1635 if (!word.word().empty()) {
1639 // we have to go on checking so move cursor to the next char
1640 text_.cursor.pos(text_.cursor.pos() + 1);
1642 word = text_.selectNextWordToSpellcheck(value);
1643 if (word.word().empty())
1644 bv->unlockInset(const_cast<InsetText *>(this));
1651 void InsetText::selectSelectedWord(BufferView * bv)
1653 if (the_locking_inset) {
1654 the_locking_inset->selectSelectedWord(bv);
1657 getLyXText(bv)->selectSelectedWord();
1658 updateLocal(bv, false);
1662 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1664 if (the_locking_inset) {
1665 if (the_locking_inset->nextChange(bv, length))
1667 text_.cursorRight(true);
1669 lyx::find::SearchResult result =
1670 lyx::find::findNextChange(bv, &text_, length);
1672 if (result == lyx::find::SR_FOUND) {
1673 LyXCursor cur = text_.cursor;
1674 bv->unlockInset(bv->theLockingInset());
1675 if (bv->lockInset(this))
1678 text_.setSelectionRange(length);
1679 updateLocal(bv, false);
1681 return result != lyx::find::SR_NOT_FOUND;
1685 bool InsetText::searchForward(BufferView * bv, string const & str,
1688 if (the_locking_inset) {
1689 if (the_locking_inset->searchForward(bv, str, cs, mw))
1691 text_.cursorRight(true);
1693 lyx::find::SearchResult result =
1694 lyx::find::find(bv, &text_, str, true, cs, mw);
1696 if (result == lyx::find::SR_FOUND) {
1697 LyXCursor cur = text_.cursor;
1698 bv->unlockInset(bv->theLockingInset());
1699 if (bv->lockInset(this))
1702 text_.setSelectionRange(str.length());
1703 updateLocal(bv, false);
1705 return result != lyx::find::SR_NOT_FOUND;
1709 bool InsetText::searchBackward(BufferView * bv, string const & str,
1712 if (the_locking_inset) {
1713 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1717 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1718 text_.setCursor(pit, pit->size());
1720 lyx::find::SearchResult result =
1721 lyx::find::find(bv, &text_, str, false, cs, mw);
1723 if (result == lyx::find::SR_FOUND) {
1724 LyXCursor cur = text_.cursor;
1725 bv->unlockInset(bv->theLockingInset());
1726 if (bv->lockInset(this))
1729 text_.setSelectionRange(str.length());
1730 updateLocal(bv, false);
1732 return result != lyx::find::SR_NOT_FOUND;
1736 bool InsetText::checkInsertChar(LyXFont & font)
1738 return owner() ? owner()->checkInsertChar(font) : true;
1742 void InsetText::collapseParagraphs(BufferView * bv)
1744 while (paragraphs.size() > 1) {
1745 ParagraphList::iterator first_par = paragraphs.begin();
1746 ParagraphList::iterator next_par = boost::next(first_par);
1747 size_t const first_par_size = first_par->size();
1749 if (!first_par->empty() &&
1750 !next_par->empty() &&
1751 !first_par->isSeparator(first_par_size - 1)) {
1752 first_par->insertChar(first_par_size, ' ');
1755 if (text_.selection.set()) {
1756 if (text_.selection.start.par() == next_par) {
1757 text_.selection.start.par(first_par);
1758 text_.selection.start.pos(
1759 text_.selection.start.pos() + first_par_size);
1761 if (text_.selection.end.par() == next_par) {
1762 text_.selection.end.par(first_par);
1763 text_.selection.end.pos(
1764 text_.selection.end.pos() + first_par_size);
1768 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1773 void InsetText::getDrawFont(LyXFont & font) const
1777 owner()->getDrawFont(font);
1781 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1783 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1784 // And it probably does. You have to take a look at this John. (Lgb)
1785 #warning John, have a look here. (Lgb)
1786 ParagraphList::iterator pit = plist.begin();
1787 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1789 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1791 ParagraphList::iterator pend = plist.end();
1792 for (; pit != pend; ++pit)
1793 paragraphs.push_back(*pit);
1797 void InsetText::addPreview(PreviewLoader & loader) const
1799 ParagraphList::const_iterator pit = paragraphs.begin();
1800 ParagraphList::const_iterator pend = paragraphs.end();
1802 for (; pit != pend; ++pit) {
1803 InsetList::const_iterator it = pit->insetlist.begin();
1804 InsetList::const_iterator end = pit->insetlist.end();
1805 for (; it != end; ++it)
1806 it->inset->addPreview(loader);