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 int const id = inset->id();
387 lyxerr << "inset:" << inset << " " << id << endl;
388 for (; pit != pend; ++pit) {
389 InsetList::iterator it = pit->insetlist.begin();
390 InsetList::iterator const end = pit->insetlist.end();
391 for (; it != end; ++it) {
392 lyxerr << "it->inset:" << it->inset << endl;
393 if (it->inset == inset) {
394 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
395 text_.setCursorIntern(pit, it->pos);
396 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
397 lyxerr << "bv: " << bv << " inset: " << inset << endl;
398 lockInset(bv, inset);
399 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
402 if (it->inset->getInsetFromID(id)) {
403 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
404 text_.setCursorIntern(pit, it->pos);
405 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
406 lyxerr << "recurse" << endl;
407 return the_locking_inset->lockInsetInInset(bv, inset);
411 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
414 if (inset == cpar()->getInset(cpos())) {
415 lyxerr[Debug::INSETS] << "OK" << endl;
416 lockInset(bv, inset);
420 if (the_locking_inset && the_locking_inset == inset) {
421 if (cpar() == inset_par && cpos() == inset_pos) {
422 lyxerr[Debug::INSETS] << "OK" << endl;
423 inset_x = cx() - top_x;
426 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
428 } else if (the_locking_inset) {
429 lyxerr[Debug::INSETS] << "MAYBE" << endl;
430 return the_locking_inset->lockInsetInInset(bv, inset);
432 lyxerr[Debug::INSETS] << "NOT OK" << endl;
437 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
440 if (!the_locking_inset)
442 if (the_locking_inset == inset) {
443 the_locking_inset->insetUnlock(bv);
444 the_locking_inset = 0;
446 moveRightIntern(bv, true, false);
447 old_par = paragraphs.end(); // force layout setting
451 updateLocal(bv, false);
454 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
458 void InsetText::lfunMousePress(FuncRequest const & cmd)
462 // use this to check mouse motion for selection!
466 BufferView * bv = cmd.view();
467 FuncRequest cmd1 = cmd;
474 int tmp_y = cmd.y + dim_.asc - bv->top_y();
475 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
477 if (the_locking_inset) {
478 if (the_locking_inset == inset) {
479 the_locking_inset->localDispatch(cmd1);
482 // otherwise only unlock the_locking_inset
483 the_locking_inset->insetUnlock(bv);
484 the_locking_inset = 0;
487 no_selection = false;
489 if (bv->theLockingInset()) {
490 if (isHighlyEditableInset(inset)) {
491 // We just have to lock the inset before calling a
493 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
494 if (!bv->lockInset(uinset)) {
495 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497 inset->localDispatch(cmd1);
498 if (the_locking_inset)
499 updateLocal(bv, false);
504 bool paste_internally = false;
505 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
506 localDispatch(FuncRequest(bv, LFUN_COPY));
507 paste_internally = true;
509 int old_top_y = bv->top_y();
511 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
512 // set the selection cursor!
513 text_.selection.cursor = text_.cursor;
514 text_.cursor.x_fix(text_.cursor.x());
516 text_.clearSelection();
517 updateLocal(bv, false);
519 bv->owner()->setLayout(cpar()->layout()->name());
521 // we moved the view we cannot do mouse selection in this case!
522 if (bv->top_y() != old_top_y)
525 // Insert primary selection with middle mouse
526 // if there is a local selection in the current buffer,
528 if (cmd.button() == mouse_button::button2) {
529 if (paste_internally)
530 localDispatch(FuncRequest(bv, LFUN_PASTE));
532 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
535 getLyXText(bv)->clearSelection();
540 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
542 BufferView * bv = cmd.view();
543 FuncRequest cmd1 = cmd;
548 if (the_locking_inset)
549 return the_locking_inset->localDispatch(cmd1);
552 int tmp_y = cmd.y + dim_.asc - bv->top_y();
553 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
556 // This code should probably be removed now. Simple insets
557 // (!highlyEditable) can actually take the localDispatch,
558 // and turn it into edit() if necessary. But we still
559 // need to deal properly with the whole relative vs.
560 // absolute mouse co-ords thing in a realiable, sensible way
562 if (isHighlyEditableInset(inset))
563 ret = inset->localDispatch(cmd1);
565 inset_x = cx(bv) - top_x;
567 cmd1.x = cmd.x - inset_x;
568 cmd1.y = cmd.x - inset_y;
569 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
573 ret = inset->localDispatch(cmd1);
574 updateLocal(bv, false);
581 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
583 FuncRequest cmd1 = cmd;
587 if (the_locking_inset) {
588 the_locking_inset->localDispatch(cmd1);
592 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
595 BufferView * bv = cmd.view();
596 LyXCursor cur = text_.cursor;
597 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
598 text_.cursor.x_fix(text_.cursor.x());
599 if (cur == text_.cursor)
601 text_.setSelection();
602 updateLocal(bv, false);
606 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
608 BufferView * bv = cmd.view();
611 switch (cmd.action) {
612 case LFUN_INSET_EDIT: {
613 UpdatableInset::localDispatch(cmd);
615 if (!bv->lockInset(this)) {
616 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
621 the_locking_inset = 0;
625 inset_boundary = false;
626 inset_par = paragraphs.end();
627 old_par = paragraphs.end();
630 if (cmd.argument.size()) {
631 if (cmd.argument == "left")
632 text_.setCursorIntern(paragraphs.begin(), 0);
634 ParagraphList::iterator it = boost::prior(paragraphs.end());
635 text_.setCursor(it, it->size());
638 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
639 // we put here -1 and not button as now the button in the
640 // edit call should not be needed we will fix this in 1.3.x
641 // cycle hopefully (Jug 20020509)
642 // FIXME: GUII I've changed this to none: probably WRONG
643 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
644 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
645 text_.cursor.x(text_.cursor.x());
646 text_.cursor.x_fix(text_.cursor.x());
650 text_.clearSelection();
653 // If the inset is empty set the language of the current font to the
654 // language to the surronding text (if different).
655 if (paragraphs.begin()->empty() &&
656 paragraphs.size() == 1 &&
657 bv->getParentLanguage(this) != text_.current_font.language())
659 LyXFont font(LyXFont::ALL_IGNORE);
660 font.setLanguage(bv->getParentLanguage(this));
661 setFont(bv, font, false);
664 updateLocal(bv, false);
665 // Tell the paragraph dialog that we've entered an insettext.
666 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
670 case LFUN_MOUSE_PRESS:
674 case LFUN_MOUSE_MOTION:
675 lfunMouseMotion(cmd);
678 case LFUN_MOUSE_RELEASE:
679 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
685 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
686 no_selection = false;
688 RESULT result = UpdatableInset::localDispatch(cmd);
689 if (result != UNDISPATCHED)
693 if (cmd.action < 0 && cmd.argument.empty())
696 if (the_locking_inset) {
697 result = the_locking_inset->localDispatch(cmd);
698 if (result == DISPATCHED_NOUPDATE)
700 if (result == DISPATCHED) {
701 updateLocal(bv, false);
704 if (result >= FINISHED) {
707 moveRightIntern(bv, false, false);
712 if (result >= FINISHED) {
713 updateLocal(bv, false);
714 bv->unlockInset(this);
718 result = moveDown(bv);
719 if (result >= FINISHED) {
720 updateLocal(bv, false);
721 bv->unlockInset(this);
728 the_locking_inset = 0;
729 updateLocal(bv, false);
730 // make sure status gets reset immediately
731 bv->owner()->clearMessage();
735 bool updflag = false;
737 switch (cmd.action) {
740 case LFUN_SELFINSERT:
741 if (bv->buffer()->isReadonly()) {
742 // setErrorMessage(N_("Document is read only"));
745 if (!cmd.argument.empty()) {
746 /* Automatically delete the currently selected
747 * text and replace it with what is being
748 * typed in now. Depends on lyxrc settings
749 * "auto_region_delete", which defaults to
752 // This should not be needed here and is also WRONG!
753 recordUndo(bv, Undo::INSERT, text_.cursor.par());
757 if (lyxrc.auto_region_delete && text_.selection.set())
758 text_.cutSelection(false, false);
759 text_.clearSelection();
761 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
762 bv->owner()->getIntl().getTransManager().
763 TranslateAndInsert(cmd.argument[i], &text_);
765 text_.selection.cursor = text_.cursor;
767 result = DISPATCHED_NOUPDATE;
770 // cursor movements that need special handling
773 result = moveRight(bv);
778 result = moveLeft(bv);
782 result = moveDown(bv);
790 if (crow() == text_.firstRow())
791 result = FINISHED_UP;
793 text_.cursorPrevious();
794 text_.clearSelection();
795 result = DISPATCHED_NOUPDATE;
800 if (crow() == text_.lastRow())
801 result = FINISHED_DOWN;
804 text_.clearSelection();
805 result = DISPATCHED_NOUPDATE;
810 if (text_.selection.set())
811 text_.cutSelection(true, false);
818 if (text_.selection.set())
819 text_.cutSelection(true, false);
826 if (!autoBreakRows) {
827 if (CutAndPaste::nrOfParagraphs() > 1) {
829 #warning FIXME horrendously bad UI
831 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
836 replaceSelection(bv->getLyXText());
837 size_t sel_index = 0;
838 string const & arg = cmd.argument;
839 if (isStrUnsignedInt(arg)) {
840 size_t const paste_arg = strToUnsignedInt(arg);
841 #warning FIXME Check if the arg is in the domain of available selections.
842 sel_index = paste_arg;
844 text_.pasteSelection(sel_index);
846 text_.clearSelection();
851 case LFUN_BREAKPARAGRAPH:
852 if (!autoBreakRows) {
856 replaceSelection(bv->getLyXText());
857 text_.breakParagraph(paragraphs, 0);
861 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
862 if (!autoBreakRows) {
866 replaceSelection(bv->getLyXText());
867 text_.breakParagraph(paragraphs, 1);
871 case LFUN_BREAKLINE: {
872 if (!autoBreakRows) {
877 replaceSelection(bv->getLyXText());
878 text_.insertInset(new InsetNewline);
884 // do not set layouts on non breakable textinsets
886 string cur_layout = cpar()->layout()->name();
888 // Derive layout number from given argument (string)
889 // and current buffer's textclass (number).
890 LyXTextClass const & tclass =
891 bv->buffer()->params().getLyXTextClass();
892 string layout = cmd.argument;
893 bool hasLayout = tclass.hasLayout(layout);
895 // If the entry is obsolete, use the new one instead.
897 string const & obs = tclass[layout]->obsoleted_by();
902 // see if we found the layout number:
904 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
905 bv->owner()->dispatch(lf);
909 if (cur_layout != layout) {
911 text_.setLayout(layout);
912 bv->owner()->setLayout(cpar()->layout()->name());
916 // reset the layout box
917 bv->owner()->setLayout(cpar()->layout()->name());
922 if (!bv->dispatch(cmd))
923 result = UNDISPATCHED;
927 updateLocal(bv, updflag);
929 /// If the action has deleted all text in the inset, we need to change the
930 // language to the language of the surronding text.
931 if (!was_empty && paragraphs.begin()->empty() &&
932 paragraphs.size() == 1) {
933 LyXFont font(LyXFont::ALL_IGNORE);
934 font.setLanguage(bv->getParentLanguage(this));
935 setFont(bv, font, false);
938 if (result >= FINISHED)
939 bv->unlockInset(this);
941 if (result == DISPATCHED_NOUPDATE)
947 int InsetText::latex(Buffer const & buf, ostream & os,
948 LatexRunParams const & runparams) const
951 latexParagraphs(buf, paragraphs, os, texrow, runparams);
952 return texrow.rows();
956 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
958 unsigned int lines = 0;
960 ParagraphList::const_iterator beg = paragraphs.begin();
961 ParagraphList::const_iterator end = paragraphs.end();
962 ParagraphList::const_iterator it = beg;
963 for (; it != end; ++it) {
964 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
965 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
972 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
974 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
975 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
977 // There is a confusion between the empty paragraph and the default paragraph
978 // The default paragraph is <p></p>, the empty paragraph is *empty*
979 // Since none of the floats of linuxdoc accepts standard paragraphs
980 // I disable them. I don't expect problems. (jamatos 2003/07/27)
981 for (; pit != pend; ++pit) {
982 const string name = pit->layout()->latexname();
984 sgml::openTag(os, 1, 0, name);
985 buf.simpleLinuxDocOnePar(os, pit, 0);
987 sgml::closeTag(os, 1, 0, name);
993 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
995 unsigned int lines = 0;
997 vector<string> environment_stack(10);
998 vector<string> environment_inner(10);
1000 int const command_depth = 0;
1003 Paragraph::depth_type depth = 0; // paragraph depth
1005 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1006 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1008 for (; pit != pend; ++pit) {
1009 int desc_on = 0; // description mode
1011 LyXLayout_ptr const & style = pit->layout();
1013 // environment tag closing
1014 for (; depth > pit->params().depth(); --depth) {
1015 if (environment_inner[depth] != "!-- --") {
1016 item_name = "listitem";
1017 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1018 if (environment_inner[depth] == "varlistentry")
1019 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1021 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1022 environment_stack[depth].erase();
1023 environment_inner[depth].erase();
1026 if (depth == pit->params().depth()
1027 && environment_stack[depth] != style->latexname()
1028 && !environment_stack[depth].empty()) {
1029 if (environment_inner[depth] != "!-- --") {
1030 item_name= "listitem";
1031 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1032 if (environment_inner[depth] == "varlistentry")
1033 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1036 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1038 environment_stack[depth].erase();
1039 environment_inner[depth].erase();
1042 // Write opening SGML tags.
1043 switch (style->latextype) {
1044 case LATEX_PARAGRAPH:
1045 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1049 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1053 case LATEX_ENVIRONMENT:
1054 case LATEX_ITEM_ENVIRONMENT:
1055 if (depth < pit->params().depth()) {
1056 depth = pit->params().depth();
1057 environment_stack[depth].erase();
1060 if (environment_stack[depth] != style->latexname()) {
1061 if (environment_stack.size() == depth + 1) {
1062 environment_stack.push_back("!-- --");
1063 environment_inner.push_back("!-- --");
1065 environment_stack[depth] = style->latexname();
1066 environment_inner[depth] = "!-- --";
1067 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1069 if (environment_inner[depth] != "!-- --") {
1070 item_name= "listitem";
1071 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1072 if (environment_inner[depth] == "varlistentry")
1073 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1077 if (style->latextype == LATEX_ENVIRONMENT) {
1078 if (!style->latexparam().empty()) {
1079 if (style->latexparam() == "CDATA")
1082 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1087 desc_on = (style->labeltype == LABEL_MANUAL);
1089 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1090 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1092 item_name = desc_on ? "term" : "para";
1093 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1097 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1101 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1104 // write closing SGML tags
1105 switch (style->latextype) {
1106 case LATEX_ENVIRONMENT:
1107 if (!style->latexparam().empty()) {
1108 if (style->latexparam() == "CDATA")
1111 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1114 case LATEX_ITEM_ENVIRONMENT:
1118 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1120 case LATEX_PARAGRAPH:
1121 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1124 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1130 for (int d = depth; d >= 0; --d) {
1131 if (!environment_stack[depth].empty()) {
1132 if (environment_inner[depth] != "!-- --") {
1133 item_name = "listitem";
1134 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1135 if (environment_inner[depth] == "varlistentry")
1136 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1139 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1147 void InsetText::validate(LaTeXFeatures & features) const
1149 for_each(paragraphs.begin(), paragraphs.end(),
1150 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1154 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1156 if (the_locking_inset) {
1157 the_locking_inset->getCursor(bv, x, y);
1161 y = cy() + InsetText::y();
1165 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1167 if (the_locking_inset) {
1168 the_locking_inset->getCursorPos(bv, x, y);
1171 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1172 y = cy() - TEXT_TO_INSET_OFFSET;
1176 int InsetText::insetInInsetY() const
1178 if (!the_locking_inset)
1181 return inset_y + the_locking_inset->insetInInsetY();
1185 void InsetText::fitInsetCursor(BufferView * bv) const
1187 if (the_locking_inset) {
1188 the_locking_inset->fitInsetCursor(bv);
1192 LyXFont const font = text_.getFont(cpar(), cpos());
1194 int const asc = font_metrics::maxAscent(font);
1195 int const desc = font_metrics::maxDescent(font);
1197 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1201 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1203 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1204 return moveLeftIntern(bv, false, true, false);
1206 return moveRightIntern(bv, true, true, false);
1210 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1212 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1213 return moveRightIntern(bv, true, true, false);
1215 return moveLeftIntern(bv, false, true, false);
1220 InsetText::moveRightIntern(BufferView * bv, bool front,
1221 bool activate_inset, bool selecting)
1223 ParagraphList::iterator c_par = cpar();
1225 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1226 return FINISHED_RIGHT;
1227 if (activate_inset && checkAndActivateInset(bv, front))
1229 text_.cursorRight(bv);
1231 text_.clearSelection();
1232 return DISPATCHED_NOUPDATE;
1237 InsetText::moveLeftIntern(BufferView * bv, bool front,
1238 bool activate_inset, bool selecting)
1240 if (cpar() == paragraphs.begin() && cpos() <= 0)
1242 text_.cursorLeft(bv);
1244 text_.clearSelection();
1245 if (activate_inset && checkAndActivateInset(bv, front))
1247 return DISPATCHED_NOUPDATE;
1251 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1253 if (crow() == text_.firstRow())
1256 text_.clearSelection();
1257 return DISPATCHED_NOUPDATE;
1261 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1263 if (crow() == text_.lastRow())
1264 return FINISHED_DOWN;
1265 text_.cursorDown(bv);
1266 text_.clearSelection();
1267 return DISPATCHED_NOUPDATE;
1271 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1273 if (the_locking_inset) {
1274 if (the_locking_inset->insetAllowed(inset))
1275 return the_locking_inset->insertInset(bv, inset);
1278 inset->setOwner(this);
1279 text_.insertInset(inset);
1281 updateLocal(bv, true);
1286 bool InsetText::insetAllowed(InsetOld::Code code) const
1288 // in_insetAllowed is a really gross hack,
1289 // to allow us to call the owner's insetAllowed
1290 // without stack overflow, which can happen
1291 // when the owner uses InsetCollapsable::insetAllowed()
1293 if (in_insetAllowed)
1295 in_insetAllowed = true;
1296 if (the_locking_inset)
1297 ret = the_locking_inset->insetAllowed(code);
1299 ret = owner()->insetAllowed(code);
1300 in_insetAllowed = false;
1305 UpdatableInset * InsetText::getLockingInset() const
1307 return the_locking_inset ? the_locking_inset->getLockingInset() :
1308 const_cast<InsetText *>(this);
1312 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1316 if (the_locking_inset)
1317 return the_locking_inset->getFirstLockingInsetOfType(c);
1322 bool InsetText::showInsetDialog(BufferView * bv) const
1324 if (the_locking_inset)
1325 return the_locking_inset->showInsetDialog(bv);
1330 void InsetText::getLabelList(std::vector<string> & list) const
1332 ParagraphList::const_iterator pit = paragraphs.begin();
1333 ParagraphList::const_iterator pend = paragraphs.end();
1334 for (; pit != pend; ++pit) {
1335 InsetList::const_iterator beg = pit->insetlist.begin();
1336 InsetList::const_iterator end = pit->insetlist.end();
1337 for (; beg != end; ++beg)
1338 beg->inset->getLabelList(list);
1343 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1346 if (the_locking_inset) {
1347 the_locking_inset->setFont(bv, font, toggleall, selectall);
1351 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1352 || cpar()->empty()) {
1353 text_.setFont(font, toggleall);
1358 if (text_.selection.set())
1359 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1363 text_.selection.cursor = text_.cursor;
1364 text_.cursorBottom();
1365 text_.setSelection();
1368 text_.toggleFree(font, toggleall);
1371 text_.clearSelection();
1374 updateLocal(bv, true);
1378 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1380 if (cpos() == cpar()->size())
1382 InsetOld * inset = cpar()->getInset(cpos());
1383 if (!isHighlyEditableInset(inset))
1385 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1386 inset->localDispatch(cmd);
1387 if (!the_locking_inset)
1389 updateLocal(bv, false);
1394 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1395 mouse_button::state button)
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;
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 + 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);