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"
28 #include "metricsinfo.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
32 #include "rowpainter.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
38 #include "frontends/Alert.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
43 #include "support/lyxalgo.h" // lyx::count
45 #include <boost/bind.hpp>
47 using bv_funcs::replaceSelection;
51 using lyx::graphics::PreviewLoader;
53 using lyx::support::isStrUnsignedInt;
54 using lyx::support::strToUnsignedInt;
65 InsetText::InsetText(BufferParams const & bp)
66 : UpdatableInset(), text_(0, this, true, paragraphs)
68 paragraphs.push_back(Paragraph());
69 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
70 if (bp.tracking_changes)
71 paragraphs.begin()->trackChanges();
76 InsetText::InsetText(InsetText const & in)
77 : UpdatableInset(in), text_(0, this, true, paragraphs)
83 InsetText & InsetText::operator=(InsetText const & it)
90 void InsetText::init(InsetText const * ins)
93 textwidth_ = ins->textwidth_;
94 text_.bv_owner = ins->text_.bv_owner;
96 paragraphs = ins->paragraphs;
98 ParagraphList::iterator pit = paragraphs.begin();
99 ParagraphList::iterator end = paragraphs.end();
100 for (; pit != end; ++pit)
101 pit->setInsetOwner(this);
103 autoBreakRows_ = ins->autoBreakRows_;
104 drawFrame_ = ins->drawFrame_;
105 frame_color_ = ins->frame_color_;
107 textwidth_ = 0; // broken
109 frame_color_ = LColor::insetframe;
110 autoBreakRows_ = false;
112 the_locking_inset = 0;
113 for_each(paragraphs.begin(), paragraphs.end(),
114 boost::bind(&Paragraph::setInsetOwner, _1, this));
118 old_par = paragraphs.end();
119 in_insetAllowed = false;
123 void InsetText::clear(bool just_mark_erased)
125 if (just_mark_erased) {
126 ParagraphList::iterator it = paragraphs.begin();
127 ParagraphList::iterator end = paragraphs.end();
128 for (; it != end; ++it)
133 // This is a gross hack...
134 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
137 paragraphs.push_back(Paragraph());
138 paragraphs.begin()->setInsetOwner(this);
139 paragraphs.begin()->layout(old_layout);
143 auto_ptr<InsetBase> InsetText::clone() const
145 return auto_ptr<InsetBase>(new InsetText(*this));
149 void InsetText::write(Buffer const & buf, ostream & os) const
152 writeParagraphData(buf, os);
156 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
158 ParagraphList::const_iterator it = paragraphs.begin();
159 ParagraphList::const_iterator end = paragraphs.end();
160 Paragraph::depth_type dth = 0;
161 for (; it != end; ++it) {
162 it->write(buf, os, buf.params(), dth);
167 void InsetText::read(Buffer const & buf, LyXLex & lex)
170 Paragraph::depth_type depth = 0;
174 if (buf.params().tracking_changes)
175 paragraphs.begin()->trackChanges();
177 // delete the initial paragraph
179 ParagraphList::iterator pit = paragraphs.begin();
183 token = lex.getString();
186 if (token == "\\end_inset") {
190 if (token == "\\end_document") {
191 lex.printError("\\end_document read in inset! Error in document!");
196 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
199 pit = paragraphs.begin();
200 ParagraphList::iterator const end = paragraphs.end();
201 for (; pit != end; ++pit)
202 pit->setInsetOwner(this);
204 if (token != "\\end_inset") {
205 lex.printError("Missing \\end_inset at this point. "
211 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
213 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
215 textwidth_ = mi.base.textwidth;
216 BufferView * bv = mi.base.bv;
218 text_.metrics(mi, dim);
219 dim.asc += TEXT_TO_INSET_OFFSET;
220 dim.des += TEXT_TO_INSET_OFFSET;
221 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
222 dim.wid = max(dim.wid, 10);
227 int InsetText::textWidth() const
233 void InsetText::draw(PainterInfo & pi, int x, int y) const
235 // update our idea of where we are. Clearly, we should
236 // not have to know this information.
239 int const start_x = x;
241 BufferView * bv = pi.base.bv;
242 Painter & pain = pi.pain;
244 // repaint the background if needed
245 if (backgroundColor() != LColor::background)
246 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
248 // no draw is necessary !!!
249 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
260 top_y = y - dim_.asc;
262 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
267 x += TEXT_TO_INSET_OFFSET;
269 paintTextInset(*bv, text_, x, y);
271 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
272 drawFrame(pain, start_x);
276 void InsetText::drawFrame(Painter & pain, int x) const
278 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
279 int const frame_x = x + ttoD2;
280 int const frame_y = top_baseline - dim_.asc + ttoD2;
281 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
282 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
283 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color_);
287 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
292 if (!autoBreakRows_ && paragraphs.size() > 1)
293 collapseParagraphs(bv);
295 if (!text_.selection.set())
296 text_.selection.cursor = text_.cursor;
299 bv->updateInset(this);
300 bv->owner()->view_state_changed();
301 bv->owner()->updateMenubar();
302 bv->owner()->updateToolbar();
303 if (old_par != cpar()) {
304 bv->owner()->setLayout(cpar()->layout()->name());
310 string const InsetText::editMessage() const
312 return _("Opened Text Inset");
316 void InsetText::insetUnlock(BufferView * bv)
318 if (the_locking_inset) {
319 the_locking_inset->insetUnlock(bv);
320 the_locking_inset = 0;
321 updateLocal(bv, false);
326 if (text_.selection.set()) {
327 text_.clearSelection();
328 } else if (owner()) {
329 bv->owner()->setLayout(owner()->getLyXText(bv)
330 ->cursor.par()->layout()->name());
332 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
333 // hack for deleteEmptyParMech
334 ParagraphList::iterator first_par = paragraphs.begin();
335 if (!first_par->empty()) {
336 text_.setCursor(first_par, 0);
337 } else if (paragraphs.size() > 1) {
338 text_.setCursor(boost::next(first_par), 0);
343 void InsetText::lockInset(BufferView * bv)
346 the_locking_inset = 0;
347 inset_pos = inset_x = inset_y = 0;
348 inset_boundary = false;
349 inset_par = paragraphs.end();
350 old_par = paragraphs.end();
351 text_.setCursorIntern(paragraphs.begin(), 0);
352 text_.clearSelection();
354 // If the inset is empty set the language of the current font to the
355 // language to the surronding text (if different).
356 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
357 bv->getParentLanguage(this) != text_.current_font.language()) {
358 LyXFont font(LyXFont::ALL_IGNORE);
359 font.setLanguage(bv->getParentLanguage(this));
360 setFont(bv, font, false);
365 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
367 the_locking_inset = inset;
368 inset_x = cx() - top_x;
372 inset_boundary = cboundary();
376 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
378 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
379 << inset << "): " << endl;
382 if (!the_locking_inset) {
383 ParagraphList::iterator pit = paragraphs.begin();
384 ParagraphList::iterator pend = paragraphs.end();
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;
401 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
404 if (inset == cpar()->getInset(cpos())) {
405 lyxerr[Debug::INSETS] << "OK" << endl;
406 lockInset(bv, inset);
410 if (the_locking_inset && the_locking_inset == inset) {
411 if (cpar() == inset_par && cpos() == inset_pos) {
412 lyxerr[Debug::INSETS] << "OK" << endl;
413 inset_x = cx() - top_x;
416 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
418 } else if (the_locking_inset) {
419 lyxerr[Debug::INSETS] << "MAYBE" << endl;
420 return the_locking_inset->lockInsetInInset(bv, inset);
422 lyxerr[Debug::INSETS] << "NOT OK" << endl;
427 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
430 if (!the_locking_inset)
432 if (the_locking_inset == inset) {
433 the_locking_inset->insetUnlock(bv);
434 the_locking_inset = 0;
436 moveRightIntern(bv, true, false);
437 old_par = paragraphs.end(); // force layout setting
441 updateLocal(bv, false);
444 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
448 void InsetText::lfunMousePress(FuncRequest const & cmd)
452 // use this to check mouse motion for selection!
456 BufferView * bv = cmd.view();
457 FuncRequest cmd1 = cmd;
464 int tmp_y = cmd.y + dim_.asc - bv->top_y();
465 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
467 if (the_locking_inset) {
468 if (the_locking_inset == inset) {
469 the_locking_inset->localDispatch(cmd1);
472 // otherwise only unlock the_locking_inset
473 the_locking_inset->insetUnlock(bv);
474 the_locking_inset = 0;
477 no_selection = false;
479 if (bv->theLockingInset()) {
480 if (isHighlyEditableInset(inset)) {
481 // We just have to lock the inset before calling a
483 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
484 if (!bv->lockInset(uinset)) {
485 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
487 inset->localDispatch(cmd1);
488 if (the_locking_inset)
489 updateLocal(bv, false);
494 bool paste_internally = false;
495 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
496 localDispatch(FuncRequest(bv, LFUN_COPY));
497 paste_internally = true;
499 int old_top_y = bv->top_y();
501 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
502 // set the selection cursor!
503 text_.selection.cursor = text_.cursor;
504 text_.cursor.x_fix(text_.cursor.x());
506 text_.clearSelection();
507 updateLocal(bv, false);
509 bv->owner()->setLayout(cpar()->layout()->name());
511 // we moved the view we cannot do mouse selection in this case!
512 if (bv->top_y() != old_top_y)
515 // Insert primary selection with middle mouse
516 // if there is a local selection in the current buffer,
518 if (cmd.button() == mouse_button::button2) {
519 if (paste_internally)
520 localDispatch(FuncRequest(bv, LFUN_PASTE));
522 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
525 getLyXText(bv)->clearSelection();
530 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
532 BufferView * bv = cmd.view();
533 FuncRequest cmd1 = cmd;
538 if (the_locking_inset)
539 return the_locking_inset->localDispatch(cmd1);
542 int tmp_y = cmd.y + dim_.asc - bv->top_y();
543 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
546 // This code should probably be removed now. Simple insets
547 // (!highlyEditable) can actually take the localDispatch,
548 // and turn it into edit() if necessary. But we still
549 // need to deal properly with the whole relative vs.
550 // absolute mouse co-ords thing in a realiable, sensible way
552 if (isHighlyEditableInset(inset))
553 ret = inset->localDispatch(cmd1);
555 inset_x = cx(bv) - top_x;
557 cmd1.x = cmd.x - inset_x;
558 cmd1.y = cmd.x - inset_y;
559 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
563 ret = inset->localDispatch(cmd1);
564 updateLocal(bv, false);
571 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
573 FuncRequest cmd1 = cmd;
577 if (the_locking_inset) {
578 the_locking_inset->localDispatch(cmd1);
582 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
585 BufferView * bv = cmd.view();
586 LyXCursor cur = text_.cursor;
587 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
588 text_.cursor.x_fix(text_.cursor.x());
589 if (cur == text_.cursor)
591 text_.setSelection();
592 updateLocal(bv, false);
596 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
598 BufferView * bv = cmd.view();
601 switch (cmd.action) {
602 case LFUN_INSET_EDIT: {
603 UpdatableInset::localDispatch(cmd);
605 if (!bv->lockInset(this)) {
606 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
611 the_locking_inset = 0;
615 inset_boundary = false;
616 inset_par = paragraphs.end();
617 old_par = paragraphs.end();
620 if (cmd.argument.size()) {
621 if (cmd.argument == "left")
622 text_.setCursorIntern(paragraphs.begin(), 0);
624 ParagraphList::iterator it = boost::prior(paragraphs.end());
625 text_.setCursor(it, it->size());
628 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
629 // we put here -1 and not button as now the button in the
630 // edit call should not be needed we will fix this in 1.3.x
631 // cycle hopefully (Jug 20020509)
632 // FIXME: GUII I've changed this to none: probably WRONG
633 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
634 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
635 text_.cursor.x(text_.cursor.x());
636 text_.cursor.x_fix(text_.cursor.x());
640 text_.clearSelection();
643 // If the inset is empty set the language of the current font to the
644 // language to the surronding text (if different).
645 if (paragraphs.begin()->empty() &&
646 paragraphs.size() == 1 &&
647 bv->getParentLanguage(this) != text_.current_font.language())
649 LyXFont font(LyXFont::ALL_IGNORE);
650 font.setLanguage(bv->getParentLanguage(this));
651 setFont(bv, font, false);
654 updateLocal(bv, false);
655 // Tell the paragraph dialog that we've entered an insettext.
656 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
660 case LFUN_MOUSE_PRESS:
664 case LFUN_MOUSE_MOTION:
665 lfunMouseMotion(cmd);
668 case LFUN_MOUSE_RELEASE:
669 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
675 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
676 no_selection = false;
678 RESULT result = UpdatableInset::localDispatch(cmd);
679 if (result != UNDISPATCHED)
683 if (cmd.action < 0 && cmd.argument.empty())
686 if (the_locking_inset) {
687 result = the_locking_inset->localDispatch(cmd);
688 if (result == DISPATCHED_NOUPDATE)
690 if (result == DISPATCHED) {
691 updateLocal(bv, false);
694 if (result >= FINISHED) {
697 moveRightIntern(bv, false, false);
702 if (result >= FINISHED) {
703 updateLocal(bv, false);
704 bv->unlockInset(this);
708 result = moveDown(bv);
709 if (result >= FINISHED) {
710 updateLocal(bv, false);
711 bv->unlockInset(this);
718 the_locking_inset = 0;
719 updateLocal(bv, false);
720 // make sure status gets reset immediately
721 bv->owner()->clearMessage();
725 bool updflag = false;
727 switch (cmd.action) {
730 case LFUN_SELFINSERT:
731 if (bv->buffer()->isReadonly()) {
732 // setErrorMessage(N_("Document is read only"));
735 if (!cmd.argument.empty()) {
736 /* Automatically delete the currently selected
737 * text and replace it with what is being
738 * typed in now. Depends on lyxrc settings
739 * "auto_region_delete", which defaults to
742 // This should not be needed here and is also WRONG!
743 recordUndo(bv, Undo::INSERT, text_.cursor.par());
747 if (lyxrc.auto_region_delete && text_.selection.set())
748 text_.cutSelection(false, false);
749 text_.clearSelection();
751 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
752 bv->owner()->getIntl().getTransManager().
753 TranslateAndInsert(cmd.argument[i], &text_);
755 text_.selection.cursor = text_.cursor;
757 result = DISPATCHED_NOUPDATE;
760 // cursor movements that need special handling
763 result = moveRight(bv);
768 result = moveLeft(bv);
772 result = moveDown(bv);
780 if (crow() == text_.firstRow())
781 result = FINISHED_UP;
783 text_.cursorPrevious();
784 text_.clearSelection();
785 result = DISPATCHED_NOUPDATE;
790 if (crow() == text_.lastRow())
791 result = FINISHED_DOWN;
794 text_.clearSelection();
795 result = DISPATCHED_NOUPDATE;
800 if (text_.selection.set())
801 text_.cutSelection(true, false);
808 if (text_.selection.set())
809 text_.cutSelection(true, false);
816 if (!autoBreakRows_) {
817 if (CutAndPaste::nrOfParagraphs() > 1) {
819 #warning FIXME horrendously bad UI
821 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
826 replaceSelection(bv->getLyXText());
827 size_t sel_index = 0;
828 string const & arg = cmd.argument;
829 if (isStrUnsignedInt(arg)) {
830 size_t const paste_arg = strToUnsignedInt(arg);
831 #warning FIXME Check if the arg is in the domain of available selections.
832 sel_index = paste_arg;
834 text_.pasteSelection(sel_index);
836 text_.clearSelection();
841 case LFUN_BREAKPARAGRAPH:
842 if (!autoBreakRows_) {
846 replaceSelection(bv->getLyXText());
847 text_.breakParagraph(paragraphs, 0);
851 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
852 if (!autoBreakRows_) {
856 replaceSelection(bv->getLyXText());
857 text_.breakParagraph(paragraphs, 1);
861 case LFUN_BREAKLINE: {
862 if (!autoBreakRows_) {
867 replaceSelection(bv->getLyXText());
868 text_.insertInset(new InsetNewline);
874 // do not set layouts on non breakable textinsets
875 if (autoBreakRows_) {
876 string cur_layout = cpar()->layout()->name();
878 // Derive layout number from given argument (string)
879 // and current buffer's textclass (number).
880 LyXTextClass const & tclass =
881 bv->buffer()->params().getLyXTextClass();
882 string layout = cmd.argument;
883 bool hasLayout = tclass.hasLayout(layout);
885 // If the entry is obsolete, use the new one instead.
887 string const & obs = tclass[layout]->obsoleted_by();
892 // see if we found the layout number:
894 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
895 bv->owner()->dispatch(lf);
899 if (cur_layout != layout) {
901 text_.setLayout(layout);
902 bv->owner()->setLayout(cpar()->layout()->name());
906 // reset the layout box
907 bv->owner()->setLayout(cpar()->layout()->name());
912 if (!bv->dispatch(cmd))
913 result = UNDISPATCHED;
917 updateLocal(bv, updflag);
919 /// If the action has deleted all text in the inset, we need to change the
920 // language to the language of the surronding text.
921 if (!was_empty && paragraphs.begin()->empty() &&
922 paragraphs.size() == 1) {
923 LyXFont font(LyXFont::ALL_IGNORE);
924 font.setLanguage(bv->getParentLanguage(this));
925 setFont(bv, font, false);
928 if (result >= FINISHED)
929 bv->unlockInset(this);
931 if (result == DISPATCHED_NOUPDATE)
937 int InsetText::latex(Buffer const & buf, ostream & os,
938 LatexRunParams const & runparams) const
941 latexParagraphs(buf, paragraphs, os, texrow, runparams);
942 return texrow.rows();
946 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
948 unsigned int lines = 0;
950 ParagraphList::const_iterator beg = paragraphs.begin();
951 ParagraphList::const_iterator end = paragraphs.end();
952 ParagraphList::const_iterator it = beg;
953 for (; it != end; ++it) {
954 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
955 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
962 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
964 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
965 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
967 // There is a confusion between the empty paragraph and the default paragraph
968 // The default paragraph is <p></p>, the empty paragraph is *empty*
969 // Since none of the floats of linuxdoc accepts standard paragraphs
970 // I disable them. I don't expect problems. (jamatos 2003/07/27)
971 for (; pit != pend; ++pit) {
972 const string name = pit->layout()->latexname();
974 sgml::openTag(os, 1, 0, name);
975 buf.simpleLinuxDocOnePar(os, pit, 0);
977 sgml::closeTag(os, 1, 0, name);
983 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
985 unsigned int lines = 0;
987 vector<string> environment_stack(10);
988 vector<string> environment_inner(10);
990 int const command_depth = 0;
993 Paragraph::depth_type depth = 0; // paragraph depth
995 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
996 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
998 for (; pit != pend; ++pit) {
999 int desc_on = 0; // description mode
1001 LyXLayout_ptr const & style = pit->layout();
1003 // environment tag closing
1004 for (; depth > pit->params().depth(); --depth) {
1005 if (environment_inner[depth] != "!-- --") {
1006 item_name = "listitem";
1007 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1008 if (environment_inner[depth] == "varlistentry")
1009 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1011 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1012 environment_stack[depth].erase();
1013 environment_inner[depth].erase();
1016 if (depth == pit->params().depth()
1017 && environment_stack[depth] != style->latexname()
1018 && !environment_stack[depth].empty()) {
1019 if (environment_inner[depth] != "!-- --") {
1020 item_name= "listitem";
1021 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1022 if (environment_inner[depth] == "varlistentry")
1023 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1026 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1028 environment_stack[depth].erase();
1029 environment_inner[depth].erase();
1032 // Write opening SGML tags.
1033 switch (style->latextype) {
1034 case LATEX_PARAGRAPH:
1035 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1039 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1043 case LATEX_ENVIRONMENT:
1044 case LATEX_ITEM_ENVIRONMENT:
1045 if (depth < pit->params().depth()) {
1046 depth = pit->params().depth();
1047 environment_stack[depth].erase();
1050 if (environment_stack[depth] != style->latexname()) {
1051 if (environment_stack.size() == depth + 1) {
1052 environment_stack.push_back("!-- --");
1053 environment_inner.push_back("!-- --");
1055 environment_stack[depth] = style->latexname();
1056 environment_inner[depth] = "!-- --";
1057 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1059 if (environment_inner[depth] != "!-- --") {
1060 item_name= "listitem";
1061 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1062 if (environment_inner[depth] == "varlistentry")
1063 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1067 if (style->latextype == LATEX_ENVIRONMENT) {
1068 if (!style->latexparam().empty()) {
1069 if (style->latexparam() == "CDATA")
1072 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1077 desc_on = (style->labeltype == LABEL_MANUAL);
1079 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1080 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1082 item_name = desc_on ? "term" : "para";
1083 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1087 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1091 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1094 // write closing SGML tags
1095 switch (style->latextype) {
1096 case LATEX_ENVIRONMENT:
1097 if (!style->latexparam().empty()) {
1098 if (style->latexparam() == "CDATA")
1101 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1104 case LATEX_ITEM_ENVIRONMENT:
1108 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1110 case LATEX_PARAGRAPH:
1111 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1114 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1120 for (int d = depth; d >= 0; --d) {
1121 if (!environment_stack[depth].empty()) {
1122 if (environment_inner[depth] != "!-- --") {
1123 item_name = "listitem";
1124 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1125 if (environment_inner[depth] == "varlistentry")
1126 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1129 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1137 void InsetText::validate(LaTeXFeatures & features) const
1139 for_each(paragraphs.begin(), paragraphs.end(),
1140 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1144 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1146 if (the_locking_inset) {
1147 the_locking_inset->getCursor(bv, x, y);
1151 y = cy() + InsetText::y();
1155 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1157 if (the_locking_inset) {
1158 the_locking_inset->getCursorPos(bv, x, y);
1161 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1162 y = cy() - TEXT_TO_INSET_OFFSET;
1166 int InsetText::insetInInsetY() const
1168 if (!the_locking_inset)
1171 return inset_y + the_locking_inset->insetInInsetY();
1175 void InsetText::fitInsetCursor(BufferView * bv) const
1177 if (the_locking_inset) {
1178 the_locking_inset->fitInsetCursor(bv);
1182 LyXFont const font = text_.getFont(cpar(), cpos());
1184 int const asc = font_metrics::maxAscent(font);
1185 int const desc = font_metrics::maxDescent(font);
1187 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1191 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1193 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1194 return moveLeftIntern(bv, false, true, false);
1196 return moveRightIntern(bv, true, true, false);
1200 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1202 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1203 return moveRightIntern(bv, true, true, false);
1205 return moveLeftIntern(bv, false, true, false);
1210 InsetText::moveRightIntern(BufferView * bv, bool front,
1211 bool activate_inset, bool selecting)
1213 ParagraphList::iterator c_par = cpar();
1215 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1216 return FINISHED_RIGHT;
1217 if (activate_inset && checkAndActivateInset(bv, front))
1219 text_.cursorRight(bv);
1221 text_.clearSelection();
1222 return DISPATCHED_NOUPDATE;
1227 InsetText::moveLeftIntern(BufferView * bv, bool front,
1228 bool activate_inset, bool selecting)
1230 if (cpar() == paragraphs.begin() && cpos() <= 0)
1232 text_.cursorLeft(bv);
1234 text_.clearSelection();
1235 if (activate_inset && checkAndActivateInset(bv, front))
1237 return DISPATCHED_NOUPDATE;
1241 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1243 if (crow() == text_.firstRow())
1246 text_.clearSelection();
1247 return DISPATCHED_NOUPDATE;
1251 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1253 if (crow() == text_.lastRow())
1254 return FINISHED_DOWN;
1255 text_.cursorDown(bv);
1256 text_.clearSelection();
1257 return DISPATCHED_NOUPDATE;
1261 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1263 if (the_locking_inset) {
1264 if (the_locking_inset->insetAllowed(inset))
1265 return the_locking_inset->insertInset(bv, inset);
1268 inset->setOwner(this);
1269 text_.insertInset(inset);
1271 updateLocal(bv, true);
1276 bool InsetText::insetAllowed(InsetOld::Code code) const
1278 // in_insetAllowed is a really gross hack,
1279 // to allow us to call the owner's insetAllowed
1280 // without stack overflow, which can happen
1281 // when the owner uses InsetCollapsable::insetAllowed()
1283 if (in_insetAllowed)
1285 in_insetAllowed = true;
1286 if (the_locking_inset)
1287 ret = the_locking_inset->insetAllowed(code);
1289 ret = owner()->insetAllowed(code);
1290 in_insetAllowed = false;
1295 UpdatableInset * InsetText::getLockingInset() const
1297 return the_locking_inset ? the_locking_inset->getLockingInset() :
1298 const_cast<InsetText *>(this);
1302 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1306 if (the_locking_inset)
1307 return the_locking_inset->getFirstLockingInsetOfType(c);
1312 bool InsetText::showInsetDialog(BufferView * bv) const
1314 if (the_locking_inset)
1315 return the_locking_inset->showInsetDialog(bv);
1320 void InsetText::getLabelList(std::vector<string> & list) const
1322 ParagraphList::const_iterator pit = paragraphs.begin();
1323 ParagraphList::const_iterator pend = paragraphs.end();
1324 for (; pit != pend; ++pit) {
1325 InsetList::const_iterator beg = pit->insetlist.begin();
1326 InsetList::const_iterator end = pit->insetlist.end();
1327 for (; beg != end; ++beg)
1328 beg->inset->getLabelList(list);
1333 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1336 if (the_locking_inset) {
1337 the_locking_inset->setFont(bv, font, toggleall, selectall);
1341 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1342 || cpar()->empty()) {
1343 text_.setFont(font, toggleall);
1348 if (text_.selection.set())
1349 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1353 text_.selection.cursor = text_.cursor;
1354 text_.cursorBottom();
1355 text_.setSelection();
1358 text_.toggleFree(font, toggleall);
1361 text_.clearSelection();
1364 updateLocal(bv, true);
1368 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1370 if (cpos() == cpar()->size())
1372 InsetOld * inset = cpar()->getInset(cpos());
1373 if (!isHighlyEditableInset(inset))
1375 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1376 inset->localDispatch(cmd);
1377 if (!the_locking_inset)
1379 updateLocal(bv, false);
1384 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1385 mouse_button::state button)
1388 int dummyy = y + dim_.asc;
1389 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1390 // we only do the edit() call if the inset was hit by the mouse
1391 // or if it is a highly editable inset. So we should call this
1392 // function from our own edit with button < 0.
1393 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1395 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1404 inset_x = cx() - top_x;
1406 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1407 inset->localDispatch(cmd);
1408 if (!the_locking_inset)
1410 updateLocal(bv, false);
1415 void InsetText::markNew(bool track_changes)
1417 ParagraphList::iterator pit = paragraphs.begin();
1418 ParagraphList::iterator end = paragraphs.end();
1419 for (; pit != end; ++pit) {
1420 if (track_changes) {
1421 pit->trackChanges();
1423 // no-op when not tracking
1424 pit->cleanChanges();
1430 void InsetText::setText(string const & data, LyXFont const & font)
1433 for (unsigned int i = 0; i < data.length(); ++i)
1434 paragraphs.begin()->insertChar(i, data[i], font);
1438 void InsetText::setAutoBreakRows(bool flag)
1440 if (flag != autoBreakRows_) {
1441 autoBreakRows_ = flag;
1448 void InsetText::setDrawFrame(DrawFrame how)
1454 void InsetText::setFrameColor(EnumLColor col)
1460 int InsetText::cx() const
1462 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1463 if (the_locking_inset) {
1464 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1465 if (font.isVisibleRightToLeft())
1466 x -= the_locking_inset->width();
1472 int InsetText::cy() const
1474 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1478 pos_type InsetText::cpos() const
1480 return text_.cursor.pos();
1484 ParagraphList::iterator InsetText::cpar() const
1486 return text_.cursor.par();
1490 bool InsetText::cboundary() const
1492 return text_.cursor.boundary();
1496 RowList::iterator InsetText::crow() const
1498 return text_.cursorRow();
1502 LyXText * InsetText::getLyXText(BufferView const * bv,
1503 bool const recursive) const
1506 if (recursive && the_locking_inset)
1507 return the_locking_inset->getLyXText(bv, true);
1512 void InsetText::setViewCache(BufferView const * bv) const
1515 if (bv != text_.bv_owner) {
1516 //lyxerr << "setting view cache from "
1517 // << text_.bv_owner << " to " << bv << "\n";
1518 text_.init(const_cast<BufferView *>(bv));
1520 text_.bv_owner = const_cast<BufferView *>(bv);
1525 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1528 /// then remove all LyXText in text-insets
1529 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1530 const_cast<ParagraphList&>(paragraphs).end(),
1531 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1536 void InsetText::removeNewlines()
1538 ParagraphList::iterator it = paragraphs.begin();
1539 ParagraphList::iterator end = paragraphs.end();
1540 for (; it != end; ++it)
1541 for (int i = 0; i < it->size(); ++i)
1542 if (it->isNewline(i))
1547 int InsetText::scroll(bool recursive) const
1549 int sx = UpdatableInset::scroll(false);
1551 if (recursive && the_locking_inset)
1552 sx += the_locking_inset->scroll(recursive);
1558 void InsetText::clearSelection(BufferView *)
1560 text_.clearSelection();
1564 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1566 Painter & pain = bv->painter();
1568 int h = dim_.asc + dim_.des;
1569 int ty = baseline - dim_.asc;
1575 if (ty + h > pain.paperHeight())
1576 h = pain.paperHeight();
1577 if (top_x + w > pain.paperWidth())
1578 w = pain.paperWidth();
1579 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1583 ParagraphList * InsetText::getParagraphs(int i) const
1585 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1589 LyXCursor const & InsetText::cursor(BufferView * bv) const
1591 if (the_locking_inset)
1592 return the_locking_inset->cursor(bv);
1593 return getLyXText(bv)->cursor;
1597 InsetOld * InsetText::getInsetFromID(int id_arg) const
1600 return const_cast<InsetText *>(this);
1602 ParagraphList::const_iterator pit = paragraphs.begin();
1603 ParagraphList::const_iterator pend = paragraphs.end();
1604 for (; pit != pend; ++pit) {
1605 InsetList::const_iterator it = pit->insetlist.begin();
1606 InsetList::const_iterator end = pit->insetlist.end();
1607 for (; it != end; ++it) {
1608 if (it->inset->id() == id_arg)
1610 InsetOld * in = it->inset->getInsetFromID(id_arg);
1620 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1623 if (the_locking_inset) {
1624 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1625 if (!word.word().empty()) {
1629 // we have to go on checking so move cursor to the next char
1630 text_.cursor.pos(text_.cursor.pos() + 1);
1632 word = text_.selectNextWordToSpellcheck(value);
1633 if (word.word().empty())
1634 bv->unlockInset(const_cast<InsetText *>(this));
1641 void InsetText::selectSelectedWord(BufferView * bv)
1643 if (the_locking_inset) {
1644 the_locking_inset->selectSelectedWord(bv);
1647 getLyXText(bv)->selectSelectedWord();
1648 updateLocal(bv, false);
1652 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1654 if (the_locking_inset) {
1655 if (the_locking_inset->nextChange(bv, length))
1657 text_.cursorRight(true);
1659 lyx::find::SearchResult result =
1660 lyx::find::findNextChange(bv, &text_, length);
1662 if (result == lyx::find::SR_FOUND) {
1663 LyXCursor cur = text_.cursor;
1664 bv->unlockInset(bv->theLockingInset());
1665 if (bv->lockInset(this))
1668 text_.setSelectionRange(length);
1669 updateLocal(bv, false);
1671 return result != lyx::find::SR_NOT_FOUND;
1675 bool InsetText::searchForward(BufferView * bv, string const & str,
1678 if (the_locking_inset) {
1679 if (the_locking_inset->searchForward(bv, str, cs, mw))
1681 text_.cursorRight(true);
1683 lyx::find::SearchResult result =
1684 lyx::find::find(bv, &text_, str, true, cs, mw);
1686 if (result == lyx::find::SR_FOUND) {
1687 LyXCursor cur = text_.cursor;
1688 bv->unlockInset(bv->theLockingInset());
1689 if (bv->lockInset(this))
1692 text_.setSelectionRange(str.length());
1693 updateLocal(bv, false);
1695 return result != lyx::find::SR_NOT_FOUND;
1699 bool InsetText::searchBackward(BufferView * bv, string const & str,
1702 if (the_locking_inset) {
1703 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1707 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1708 text_.setCursor(pit, pit->size());
1710 lyx::find::SearchResult result =
1711 lyx::find::find(bv, &text_, str, false, cs, mw);
1713 if (result == lyx::find::SR_FOUND) {
1714 LyXCursor cur = text_.cursor;
1715 bv->unlockInset(bv->theLockingInset());
1716 if (bv->lockInset(this))
1719 text_.setSelectionRange(str.length());
1720 updateLocal(bv, false);
1722 return result != lyx::find::SR_NOT_FOUND;
1726 bool InsetText::checkInsertChar(LyXFont & font)
1728 return owner() ? owner()->checkInsertChar(font) : true;
1732 void InsetText::collapseParagraphs(BufferView * bv)
1734 while (paragraphs.size() > 1) {
1735 ParagraphList::iterator first_par = paragraphs.begin();
1736 ParagraphList::iterator next_par = boost::next(first_par);
1737 size_t const first_par_size = first_par->size();
1739 if (!first_par->empty() &&
1740 !next_par->empty() &&
1741 !first_par->isSeparator(first_par_size - 1)) {
1742 first_par->insertChar(first_par_size, ' ');
1745 if (text_.selection.set()) {
1746 if (text_.selection.start.par() == next_par) {
1747 text_.selection.start.par(first_par);
1748 text_.selection.start.pos(
1749 text_.selection.start.pos() + first_par_size);
1751 if (text_.selection.end.par() == next_par) {
1752 text_.selection.end.par(first_par);
1753 text_.selection.end.pos(
1754 text_.selection.end.pos() + first_par_size);
1758 mergeParagraph(bv->buffer()->params(), paragraphs, first_par);
1763 void InsetText::getDrawFont(LyXFont & font) const
1767 owner()->getDrawFont(font);
1771 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1773 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1774 // And it probably does. You have to take a look at this John. (Lgb)
1775 #warning John, have a look here. (Lgb)
1776 ParagraphList::iterator pit = plist.begin();
1777 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1779 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1781 ParagraphList::iterator pend = plist.end();
1782 for (; pit != pend; ++pit)
1783 paragraphs.push_back(*pit);
1787 void InsetText::addPreview(PreviewLoader & loader) const
1789 ParagraphList::const_iterator pit = paragraphs.begin();
1790 ParagraphList::const_iterator pend = paragraphs.end();
1792 for (; pit != pend; ++pit) {
1793 InsetList::const_iterator it = pit->insetlist.begin();
1794 InsetList::const_iterator end = pit->insetlist.end();
1795 for (; it != end; ++it)
1796 it->inset->addPreview(loader);