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)
67 : UpdatableInset(), text_(0, this, true, paragraphs)
69 paragraphs.push_back(Paragraph());
70 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
71 if (bp.tracking_changes)
72 paragraphs.begin()->trackChanges();
77 InsetText::InsetText(InsetText const & in)
78 : UpdatableInset(in), text_(0, this, true, paragraphs)
84 InsetText & InsetText::operator=(InsetText const & it)
91 void InsetText::init(InsetText const * ins)
94 textwidth_ = ins->textwidth_;
95 text_.bv_owner = ins->text_.bv_owner;
97 paragraphs = ins->paragraphs;
99 ParagraphList::iterator pit = paragraphs.begin();
100 ParagraphList::iterator end = paragraphs.end();
101 for (; pit != end; ++pit)
102 pit->setInsetOwner(this);
104 autoBreakRows_ = ins->autoBreakRows_;
105 drawFrame_ = ins->drawFrame_;
106 setFrameColor(ins->frameColor());
108 textwidth_ = 0; // broken
110 setFrameColor(LColor::insetframe);
111 autoBreakRows_ = false;
113 the_locking_inset = 0;
114 for_each(paragraphs.begin(), paragraphs.end(),
115 boost::bind(&Paragraph::setInsetOwner, _1, this));
119 old_par = paragraphs.end();
120 in_insetAllowed = false;
124 void InsetText::clear(bool just_mark_erased)
126 if (just_mark_erased) {
127 ParagraphList::iterator it = paragraphs.begin();
128 ParagraphList::iterator end = paragraphs.end();
129 for (; it != end; ++it)
134 // This is a gross hack...
135 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
138 paragraphs.push_back(Paragraph());
139 paragraphs.begin()->setInsetOwner(this);
140 paragraphs.begin()->layout(old_layout);
144 auto_ptr<InsetBase> InsetText::clone() const
146 return auto_ptr<InsetBase>(new InsetText(*this));
150 void InsetText::write(Buffer const & buf, ostream & os) const
153 writeParagraphData(buf, os);
157 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
159 ParagraphList::const_iterator it = paragraphs.begin();
160 ParagraphList::const_iterator end = paragraphs.end();
161 Paragraph::depth_type dth = 0;
162 for (; it != end; ++it) {
163 it->write(buf, os, buf.params(), dth);
168 void InsetText::read(Buffer const & buf, LyXLex & lex)
171 Paragraph::depth_type depth = 0;
175 if (buf.params().tracking_changes)
176 paragraphs.begin()->trackChanges();
178 // delete the initial paragraph
180 ParagraphList::iterator pit = paragraphs.begin();
184 token = lex.getString();
187 if (token == "\\end_inset") {
191 if (token == "\\end_document") {
192 lex.printError("\\end_document read in inset! Error in document!");
197 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
200 pit = paragraphs.begin();
201 ParagraphList::iterator const end = paragraphs.end();
202 for (; pit != end; ++pit)
203 pit->setInsetOwner(this);
205 if (token != "\\end_inset") {
206 lex.printError("Missing \\end_inset at this point. "
212 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
214 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
216 textwidth_ = mi.base.textwidth;
217 BufferView * bv = mi.base.bv;
219 text_.metrics(mi, dim);
220 dim.asc += TEXT_TO_INSET_OFFSET;
221 dim.des += TEXT_TO_INSET_OFFSET;
222 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
223 dim.wid = max(dim.wid, 10);
228 int InsetText::textWidth() const
234 void InsetText::draw(PainterInfo & pi, int x, int y) const
236 // update our idea of where we are. Clearly, we should
237 // not have to know this information.
240 int const start_x = x;
242 BufferView * bv = pi.base.bv;
243 Painter & pain = pi.pain;
245 // repaint the background if needed
246 if (backgroundColor() != LColor::background)
247 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
249 // no draw is necessary !!!
250 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
261 top_y = y - dim_.asc;
263 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
268 x += TEXT_TO_INSET_OFFSET;
270 paintTextInset(*bv, text_, x, y);
272 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
273 drawFrame(pain, start_x);
277 void InsetText::drawFrame(Painter & pain, int x) const
279 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
280 int const frame_x = x + ttoD2;
281 int const frame_y = top_baseline - dim_.asc + ttoD2;
282 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
283 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
284 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
288 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
293 if (!autoBreakRows_ && paragraphs.size() > 1)
294 collapseParagraphs(bv);
296 if (!text_.selection.set())
297 text_.selection.cursor = text_.cursor;
300 bv->updateInset(this);
301 bv->owner()->view_state_changed();
302 bv->owner()->updateMenubar();
303 bv->owner()->updateToolbar();
304 if (old_par != cpar()) {
305 bv->owner()->setLayout(cpar()->layout()->name());
311 string const InsetText::editMessage() const
313 return _("Opened Text Inset");
317 void InsetText::insetUnlock(BufferView * bv)
319 if (the_locking_inset) {
320 the_locking_inset->insetUnlock(bv);
321 the_locking_inset = 0;
322 updateLocal(bv, false);
327 if (text_.selection.set()) {
328 text_.clearSelection();
329 } else if (owner()) {
330 bv->owner()->setLayout(owner()->getLyXText(bv)
331 ->cursor.par()->layout()->name());
333 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
334 // hack for deleteEmptyParMech
335 ParagraphList::iterator first_par = paragraphs.begin();
336 if (!first_par->empty()) {
337 text_.setCursor(first_par, 0);
338 } else if (paragraphs.size() > 1) {
339 text_.setCursor(boost::next(first_par), 0);
344 void InsetText::lockInset(BufferView * bv)
347 the_locking_inset = 0;
348 inset_pos = inset_x = inset_y = 0;
349 inset_boundary = false;
350 inset_par = paragraphs.end();
351 old_par = paragraphs.end();
352 text_.setCursorIntern(paragraphs.begin(), 0);
353 text_.clearSelection();
355 // If the inset is empty set the language of the current font to the
356 // language to the surronding text (if different).
357 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
358 bv->getParentLanguage(this) != text_.current_font.language()) {
359 LyXFont font(LyXFont::ALL_IGNORE);
360 font.setLanguage(bv->getParentLanguage(this));
361 setFont(bv, font, false);
366 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
368 the_locking_inset = inset;
369 inset_x = cx() - top_x;
373 inset_boundary = cboundary();
377 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
379 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
380 << inset << "): " << endl;
383 if (!the_locking_inset) {
384 ParagraphList::iterator pit = paragraphs.begin();
385 ParagraphList::iterator pend = paragraphs.end();
387 for (; pit != pend; ++pit) {
388 InsetList::iterator it = pit->insetlist.begin();
389 InsetList::iterator const end = pit->insetlist.end();
390 for (; it != end; ++it) {
391 if (it->inset == inset) {
392 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
393 text_.setCursorIntern(pit, it->pos);
394 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
395 lyxerr << "bv: " << bv << " inset: " << inset << endl;
396 lockInset(bv, inset);
397 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
402 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
405 if (inset == cpar()->getInset(cpos())) {
406 lyxerr[Debug::INSETS] << "OK" << endl;
407 lockInset(bv, inset);
411 if (the_locking_inset && the_locking_inset == inset) {
412 if (cpar() == inset_par && cpos() == inset_pos) {
413 lyxerr[Debug::INSETS] << "OK" << endl;
414 inset_x = cx() - top_x;
417 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
419 } else if (the_locking_inset) {
420 lyxerr[Debug::INSETS] << "MAYBE" << endl;
421 return the_locking_inset->lockInsetInInset(bv, inset);
423 lyxerr[Debug::INSETS] << "NOT OK" << endl;
428 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
431 if (!the_locking_inset)
433 if (the_locking_inset == inset) {
434 the_locking_inset->insetUnlock(bv);
435 the_locking_inset = 0;
437 moveRightIntern(bv, true, false);
438 old_par = paragraphs.end(); // force layout setting
442 updateLocal(bv, false);
445 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
449 void InsetText::lfunMousePress(FuncRequest const & cmd)
453 // use this to check mouse motion for selection!
457 BufferView * bv = cmd.view();
458 FuncRequest cmd1 = cmd;
465 int tmp_y = cmd.y + dim_.asc - bv->top_y();
466 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
468 if (the_locking_inset) {
469 if (the_locking_inset == inset) {
470 the_locking_inset->localDispatch(cmd1);
473 // otherwise only unlock the_locking_inset
474 the_locking_inset->insetUnlock(bv);
475 the_locking_inset = 0;
478 no_selection = false;
480 if (bv->theLockingInset()) {
481 if (isHighlyEditableInset(inset)) {
482 // We just have to lock the inset before calling a
484 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
485 if (!bv->lockInset(uinset)) {
486 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
488 inset->localDispatch(cmd1);
489 if (the_locking_inset)
490 updateLocal(bv, false);
495 bool paste_internally = false;
496 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
497 localDispatch(FuncRequest(bv, LFUN_COPY));
498 paste_internally = true;
500 int old_top_y = bv->top_y();
502 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
503 // set the selection cursor!
504 text_.selection.cursor = text_.cursor;
505 text_.cursor.x_fix(text_.cursor.x());
507 text_.clearSelection();
508 updateLocal(bv, false);
510 bv->owner()->setLayout(cpar()->layout()->name());
512 // we moved the view we cannot do mouse selection in this case!
513 if (bv->top_y() != old_top_y)
516 // Insert primary selection with middle mouse
517 // if there is a local selection in the current buffer,
519 if (cmd.button() == mouse_button::button2) {
520 if (paste_internally)
521 localDispatch(FuncRequest(bv, LFUN_PASTE));
523 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
526 getLyXText(bv)->clearSelection();
531 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
533 BufferView * bv = cmd.view();
534 FuncRequest cmd1 = cmd;
539 if (the_locking_inset)
540 return the_locking_inset->localDispatch(cmd1);
543 int tmp_y = cmd.y + dim_.asc - bv->top_y();
544 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
547 // This code should probably be removed now. Simple insets
548 // (!highlyEditable) can actually take the localDispatch,
549 // and turn it into edit() if necessary. But we still
550 // need to deal properly with the whole relative vs.
551 // absolute mouse co-ords thing in a realiable, sensible way
553 if (isHighlyEditableInset(inset))
554 ret = inset->localDispatch(cmd1);
556 inset_x = cx(bv) - top_x;
558 cmd1.x = cmd.x - inset_x;
559 cmd1.y = cmd.x - inset_y;
560 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
564 ret = inset->localDispatch(cmd1);
565 updateLocal(bv, false);
572 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
574 FuncRequest cmd1 = cmd;
578 if (the_locking_inset) {
579 the_locking_inset->localDispatch(cmd1);
583 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
586 BufferView * bv = cmd.view();
587 LyXCursor cur = text_.cursor;
588 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
589 text_.cursor.x_fix(text_.cursor.x());
590 if (cur == text_.cursor)
592 text_.setSelection();
593 updateLocal(bv, false);
597 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
599 BufferView * bv = cmd.view();
602 switch (cmd.action) {
603 case LFUN_INSET_EDIT: {
604 UpdatableInset::localDispatch(cmd);
606 if (!bv->lockInset(this)) {
607 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
612 the_locking_inset = 0;
616 inset_boundary = false;
617 inset_par = paragraphs.end();
618 old_par = paragraphs.end();
621 if (cmd.argument.size()) {
622 if (cmd.argument == "left")
623 text_.setCursorIntern(paragraphs.begin(), 0);
625 ParagraphList::iterator it = boost::prior(paragraphs.end());
626 text_.setCursor(it, it->size());
629 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
630 // we put here -1 and not button as now the button in the
631 // edit call should not be needed we will fix this in 1.3.x
632 // cycle hopefully (Jug 20020509)
633 // FIXME: GUII I've changed this to none: probably WRONG
634 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
635 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
636 text_.cursor.x(text_.cursor.x());
637 text_.cursor.x_fix(text_.cursor.x());
641 text_.clearSelection();
644 // If the inset is empty set the language of the current font to the
645 // language to the surronding text (if different).
646 if (paragraphs.begin()->empty() &&
647 paragraphs.size() == 1 &&
648 bv->getParentLanguage(this) != text_.current_font.language())
650 LyXFont font(LyXFont::ALL_IGNORE);
651 font.setLanguage(bv->getParentLanguage(this));
652 setFont(bv, font, false);
655 updateLocal(bv, false);
656 // Tell the paragraph dialog that we've entered an insettext.
657 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
661 case LFUN_MOUSE_PRESS:
665 case LFUN_MOUSE_MOTION:
666 lfunMouseMotion(cmd);
669 case LFUN_MOUSE_RELEASE:
670 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
676 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
677 no_selection = false;
679 RESULT result = UpdatableInset::localDispatch(cmd);
680 if (result != UNDISPATCHED)
684 if (cmd.action < 0 && cmd.argument.empty())
687 if (the_locking_inset) {
688 result = the_locking_inset->localDispatch(cmd);
689 if (result == DISPATCHED_NOUPDATE)
691 if (result == DISPATCHED) {
692 updateLocal(bv, false);
695 if (result >= FINISHED) {
698 moveRightIntern(bv, false, false);
703 if (result >= FINISHED) {
704 updateLocal(bv, false);
705 bv->unlockInset(this);
709 result = moveDown(bv);
710 if (result >= FINISHED) {
711 updateLocal(bv, false);
712 bv->unlockInset(this);
719 the_locking_inset = 0;
720 updateLocal(bv, false);
721 // make sure status gets reset immediately
722 bv->owner()->clearMessage();
726 bool updflag = false;
728 switch (cmd.action) {
731 case LFUN_SELFINSERT:
732 if (bv->buffer()->isReadonly()) {
733 // setErrorMessage(N_("Document is read only"));
736 if (!cmd.argument.empty()) {
737 /* Automatically delete the currently selected
738 * text and replace it with what is being
739 * typed in now. Depends on lyxrc settings
740 * "auto_region_delete", which defaults to
743 // This should not be needed here and is also WRONG!
744 recordUndo(bv, Undo::INSERT, text_.cursor.par());
748 if (lyxrc.auto_region_delete && text_.selection.set())
749 text_.cutSelection(false, false);
750 text_.clearSelection();
752 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
753 bv->owner()->getIntl().getTransManager().
754 TranslateAndInsert(cmd.argument[i], &text_);
756 text_.selection.cursor = text_.cursor;
758 result = DISPATCHED_NOUPDATE;
761 // cursor movements that need special handling
764 result = moveRight(bv);
769 result = moveLeft(bv);
773 result = moveDown(bv);
781 if (crow() == text_.firstRow())
782 result = FINISHED_UP;
784 text_.cursorPrevious();
785 text_.clearSelection();
786 result = DISPATCHED_NOUPDATE;
791 if (crow() == text_.lastRow())
792 result = FINISHED_DOWN;
795 text_.clearSelection();
796 result = DISPATCHED_NOUPDATE;
801 if (text_.selection.set())
802 text_.cutSelection(true, false);
809 if (text_.selection.set())
810 text_.cutSelection(true, false);
817 if (!autoBreakRows_) {
818 if (CutAndPaste::nrOfParagraphs() > 1) {
820 #warning FIXME horrendously bad UI
822 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
827 replaceSelection(bv->getLyXText());
828 size_t sel_index = 0;
829 string const & arg = cmd.argument;
830 if (isStrUnsignedInt(arg)) {
831 size_t const paste_arg = strToUnsignedInt(arg);
832 #warning FIXME Check if the arg is in the domain of available selections.
833 sel_index = paste_arg;
835 text_.pasteSelection(sel_index);
837 text_.clearSelection();
842 case LFUN_BREAKPARAGRAPH:
843 if (!autoBreakRows_) {
847 replaceSelection(bv->getLyXText());
848 text_.breakParagraph(paragraphs, 0);
852 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
853 if (!autoBreakRows_) {
857 replaceSelection(bv->getLyXText());
858 text_.breakParagraph(paragraphs, 1);
862 case LFUN_BREAKLINE: {
863 if (!autoBreakRows_) {
868 replaceSelection(bv->getLyXText());
869 text_.insertInset(new InsetNewline);
875 // do not set layouts on non breakable textinsets
876 if (autoBreakRows_) {
877 string cur_layout = cpar()->layout()->name();
879 // Derive layout number from given argument (string)
880 // and current buffer's textclass (number).
881 LyXTextClass const & tclass =
882 bv->buffer()->params().getLyXTextClass();
883 string layout = cmd.argument;
884 bool hasLayout = tclass.hasLayout(layout);
886 // If the entry is obsolete, use the new one instead.
888 string const & obs = tclass[layout]->obsoleted_by();
893 // see if we found the layout number:
895 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
896 bv->owner()->dispatch(lf);
900 if (cur_layout != layout) {
902 text_.setLayout(layout);
903 bv->owner()->setLayout(cpar()->layout()->name());
907 // reset the layout box
908 bv->owner()->setLayout(cpar()->layout()->name());
913 if (!bv->dispatch(cmd))
914 result = UNDISPATCHED;
918 updateLocal(bv, updflag);
920 /// If the action has deleted all text in the inset, we need to change the
921 // language to the language of the surronding text.
922 if (!was_empty && paragraphs.begin()->empty() &&
923 paragraphs.size() == 1) {
924 LyXFont font(LyXFont::ALL_IGNORE);
925 font.setLanguage(bv->getParentLanguage(this));
926 setFont(bv, font, false);
929 if (result >= FINISHED)
930 bv->unlockInset(this);
932 if (result == DISPATCHED_NOUPDATE)
938 int InsetText::latex(Buffer const & buf, ostream & os,
939 LatexRunParams const & runparams) const
942 latexParagraphs(buf, paragraphs, os, texrow, runparams);
943 return texrow.rows();
947 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
949 unsigned int lines = 0;
951 ParagraphList::const_iterator beg = paragraphs.begin();
952 ParagraphList::const_iterator end = paragraphs.end();
953 ParagraphList::const_iterator it = beg;
954 for (; it != end; ++it) {
955 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
956 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
963 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
965 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
966 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
968 // There is a confusion between the empty paragraph and the default paragraph
969 // The default paragraph is <p></p>, the empty paragraph is *empty*
970 // Since none of the floats of linuxdoc accepts standard paragraphs
971 // I disable them. I don't expect problems. (jamatos 2003/07/27)
972 for (; pit != pend; ++pit) {
973 const string name = pit->layout()->latexname();
975 sgml::openTag(os, 1, 0, name);
976 buf.simpleLinuxDocOnePar(os, pit, 0);
978 sgml::closeTag(os, 1, 0, name);
984 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
986 unsigned int lines = 0;
988 vector<string> environment_stack(10);
989 vector<string> environment_inner(10);
991 int const command_depth = 0;
994 Paragraph::depth_type depth = 0; // paragraph depth
996 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
997 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
999 for (; pit != pend; ++pit) {
1000 int desc_on = 0; // description mode
1002 LyXLayout_ptr const & style = pit->layout();
1004 // environment tag closing
1005 for (; depth > pit->params().depth(); --depth) {
1006 if (environment_inner[depth] != "!-- --") {
1007 item_name = "listitem";
1008 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1009 if (environment_inner[depth] == "varlistentry")
1010 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1012 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1013 environment_stack[depth].erase();
1014 environment_inner[depth].erase();
1017 if (depth == pit->params().depth()
1018 && environment_stack[depth] != style->latexname()
1019 && !environment_stack[depth].empty()) {
1020 if (environment_inner[depth] != "!-- --") {
1021 item_name= "listitem";
1022 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1023 if (environment_inner[depth] == "varlistentry")
1024 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1027 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1029 environment_stack[depth].erase();
1030 environment_inner[depth].erase();
1033 // Write opening SGML tags.
1034 switch (style->latextype) {
1035 case LATEX_PARAGRAPH:
1036 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1040 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1044 case LATEX_ENVIRONMENT:
1045 case LATEX_ITEM_ENVIRONMENT:
1046 if (depth < pit->params().depth()) {
1047 depth = pit->params().depth();
1048 environment_stack[depth].erase();
1051 if (environment_stack[depth] != style->latexname()) {
1052 if (environment_stack.size() == depth + 1) {
1053 environment_stack.push_back("!-- --");
1054 environment_inner.push_back("!-- --");
1056 environment_stack[depth] = style->latexname();
1057 environment_inner[depth] = "!-- --";
1058 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1060 if (environment_inner[depth] != "!-- --") {
1061 item_name= "listitem";
1062 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1063 if (environment_inner[depth] == "varlistentry")
1064 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1068 if (style->latextype == LATEX_ENVIRONMENT) {
1069 if (!style->latexparam().empty()) {
1070 if (style->latexparam() == "CDATA")
1073 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1078 desc_on = (style->labeltype == LABEL_MANUAL);
1080 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1081 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1083 item_name = desc_on ? "term" : "para";
1084 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1088 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1092 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1095 // write closing SGML tags
1096 switch (style->latextype) {
1097 case LATEX_ENVIRONMENT:
1098 if (!style->latexparam().empty()) {
1099 if (style->latexparam() == "CDATA")
1102 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1105 case LATEX_ITEM_ENVIRONMENT:
1109 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1111 case LATEX_PARAGRAPH:
1112 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1115 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1121 for (int d = depth; d >= 0; --d) {
1122 if (!environment_stack[depth].empty()) {
1123 if (environment_inner[depth] != "!-- --") {
1124 item_name = "listitem";
1125 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1126 if (environment_inner[depth] == "varlistentry")
1127 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1130 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1138 void InsetText::validate(LaTeXFeatures & features) const
1140 for_each(paragraphs.begin(), paragraphs.end(),
1141 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1145 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1147 if (the_locking_inset) {
1148 the_locking_inset->getCursor(bv, x, y);
1152 y = cy() + InsetText::y();
1156 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1158 if (the_locking_inset) {
1159 the_locking_inset->getCursorPos(bv, x, y);
1162 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1163 y = cy() - TEXT_TO_INSET_OFFSET;
1167 int InsetText::insetInInsetY() const
1169 if (!the_locking_inset)
1172 return inset_y + the_locking_inset->insetInInsetY();
1176 void InsetText::fitInsetCursor(BufferView * bv) const
1178 if (the_locking_inset) {
1179 the_locking_inset->fitInsetCursor(bv);
1183 LyXFont const font = text_.getFont(cpar(), cpos());
1185 int const asc = font_metrics::maxAscent(font);
1186 int const desc = font_metrics::maxDescent(font);
1188 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1192 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1194 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1195 return moveLeftIntern(bv, false, true, false);
1197 return moveRightIntern(bv, true, true, false);
1201 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1203 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1204 return moveRightIntern(bv, true, true, false);
1206 return moveLeftIntern(bv, false, true, false);
1211 InsetText::moveRightIntern(BufferView * bv, bool front,
1212 bool activate_inset, bool selecting)
1214 ParagraphList::iterator c_par = cpar();
1216 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1217 return FINISHED_RIGHT;
1218 if (activate_inset && checkAndActivateInset(bv, front))
1220 text_.cursorRight(bv);
1222 text_.clearSelection();
1223 return DISPATCHED_NOUPDATE;
1228 InsetText::moveLeftIntern(BufferView * bv, bool front,
1229 bool activate_inset, bool selecting)
1231 if (cpar() == paragraphs.begin() && cpos() <= 0)
1233 text_.cursorLeft(bv);
1235 text_.clearSelection();
1236 if (activate_inset && checkAndActivateInset(bv, front))
1238 return DISPATCHED_NOUPDATE;
1242 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1244 if (crow() == text_.firstRow())
1247 text_.clearSelection();
1248 return DISPATCHED_NOUPDATE;
1252 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1254 if (crow() == text_.lastRow())
1255 return FINISHED_DOWN;
1256 text_.cursorDown(bv);
1257 text_.clearSelection();
1258 return DISPATCHED_NOUPDATE;
1262 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1264 if (the_locking_inset) {
1265 if (the_locking_inset->insetAllowed(inset))
1266 return the_locking_inset->insertInset(bv, inset);
1269 inset->setOwner(this);
1270 text_.insertInset(inset);
1272 updateLocal(bv, true);
1277 bool InsetText::insetAllowed(InsetOld::Code code) const
1279 // in_insetAllowed is a really gross hack,
1280 // to allow us to call the owner's insetAllowed
1281 // without stack overflow, which can happen
1282 // when the owner uses InsetCollapsable::insetAllowed()
1284 if (in_insetAllowed)
1286 in_insetAllowed = true;
1287 if (the_locking_inset)
1288 ret = the_locking_inset->insetAllowed(code);
1290 ret = owner()->insetAllowed(code);
1291 in_insetAllowed = false;
1296 UpdatableInset * InsetText::getLockingInset() const
1298 return the_locking_inset ? the_locking_inset->getLockingInset() :
1299 const_cast<InsetText *>(this);
1303 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1307 if (the_locking_inset)
1308 return the_locking_inset->getFirstLockingInsetOfType(c);
1313 bool InsetText::showInsetDialog(BufferView * bv) const
1315 if (the_locking_inset)
1316 return the_locking_inset->showInsetDialog(bv);
1321 void InsetText::getLabelList(Buffer const & buffer,
1322 std::vector<string> & list) const
1324 ParagraphList::const_iterator pit = paragraphs.begin();
1325 ParagraphList::const_iterator pend = paragraphs.end();
1326 for (; pit != pend; ++pit) {
1327 InsetList::const_iterator beg = pit->insetlist.begin();
1328 InsetList::const_iterator end = pit->insetlist.end();
1329 for (; beg != end; ++beg)
1330 beg->inset->getLabelList(buffer, list);
1335 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1338 if (the_locking_inset) {
1339 the_locking_inset->setFont(bv, font, toggleall, selectall);
1343 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1344 || cpar()->empty()) {
1345 text_.setFont(font, toggleall);
1350 if (text_.selection.set())
1351 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1355 text_.selection.cursor = text_.cursor;
1356 text_.cursorBottom();
1357 text_.setSelection();
1360 text_.toggleFree(font, toggleall);
1363 text_.clearSelection();
1366 updateLocal(bv, true);
1370 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1372 if (cpos() == cpar()->size())
1374 InsetOld * inset = cpar()->getInset(cpos());
1375 if (!isHighlyEditableInset(inset))
1377 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1378 inset->localDispatch(cmd);
1379 if (!the_locking_inset)
1381 updateLocal(bv, false);
1386 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1387 mouse_button::state button)
1390 int dummyy = y + dim_.asc;
1391 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1392 // we only do the edit() call if the inset was hit by the mouse
1393 // or if it is a highly editable inset. So we should call this
1394 // function from our own edit with button < 0.
1395 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1397 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1406 inset_x = cx() - top_x;
1408 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1409 inset->localDispatch(cmd);
1410 if (!the_locking_inset)
1412 updateLocal(bv, false);
1417 void InsetText::markNew(bool track_changes)
1419 ParagraphList::iterator pit = paragraphs.begin();
1420 ParagraphList::iterator end = paragraphs.end();
1421 for (; pit != end; ++pit) {
1422 if (track_changes) {
1423 pit->trackChanges();
1425 // no-op when not tracking
1426 pit->cleanChanges();
1432 void InsetText::setText(string const & data, LyXFont const & font)
1435 for (unsigned int i = 0; i < data.length(); ++i)
1436 paragraphs.begin()->insertChar(i, data[i], font);
1440 void InsetText::setAutoBreakRows(bool flag)
1442 if (flag != autoBreakRows_) {
1443 autoBreakRows_ = flag;
1450 void InsetText::setDrawFrame(DrawFrame how)
1456 LColor_color InsetText::frameColor() const
1458 return LColor::color(frame_color_);
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 + 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);