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 #warning John, look here. Doesnt make much sense.
176 if (buf.params().tracking_changes)
177 paragraphs.begin()->trackChanges();
179 // delete the initial paragraph
180 Paragraph oldpar = *paragraphs.begin();
182 ParagraphList::iterator pit = paragraphs.begin();
186 token = lex.getString();
189 if (token == "\\end_inset") {
193 if (token == "\\end_document") {
194 lex.printError("\\end_document read in inset! Error in document!");
199 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
202 pit = paragraphs.begin();
203 ParagraphList::iterator const end = paragraphs.end();
204 for (; pit != end; ++pit)
205 pit->setInsetOwner(this);
207 if (token != "\\end_inset") {
208 lex.printError("Missing \\end_inset at this point. "
213 // ensure we have at least one par.
214 if (paragraphs.empty())
215 paragraphs.push_back(oldpar);
219 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
221 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
223 textwidth_ = mi.base.textwidth;
224 BufferView * bv = mi.base.bv;
226 text_.metrics(mi, dim);
227 dim.asc += TEXT_TO_INSET_OFFSET;
228 dim.des += TEXT_TO_INSET_OFFSET;
229 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
230 dim.wid = max(dim.wid, 10);
235 int InsetText::textWidth() const
241 void InsetText::draw(PainterInfo & pi, int x, int y) const
243 // update our idea of where we are. Clearly, we should
244 // not have to know this information.
247 int const start_x = x;
249 BufferView * bv = pi.base.bv;
250 Painter & pain = pi.pain;
252 // repaint the background if needed
253 if (backgroundColor() != LColor::background)
254 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
256 // no draw is necessary !!!
257 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
268 top_y = y - dim_.asc;
270 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
275 x += TEXT_TO_INSET_OFFSET;
277 paintTextInset(*bv, text_, x, y);
279 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
280 drawFrame(pain, start_x);
284 void InsetText::drawFrame(Painter & pain, int x) const
286 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
287 int const frame_x = x + ttoD2;
288 int const frame_y = top_baseline - dim_.asc + ttoD2;
289 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
290 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
291 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
295 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
300 if (!autoBreakRows_ && paragraphs.size() > 1)
301 collapseParagraphs(bv);
303 if (!text_.selection.set())
304 text_.selection.cursor = text_.cursor;
307 bv->updateInset(this);
308 bv->owner()->view_state_changed();
309 bv->owner()->updateMenubar();
310 bv->owner()->updateToolbar();
311 if (old_par != cpar()) {
312 bv->owner()->setLayout(cpar()->layout()->name());
318 string const InsetText::editMessage() const
320 return _("Opened Text Inset");
324 void InsetText::insetUnlock(BufferView * bv)
326 if (the_locking_inset) {
327 the_locking_inset->insetUnlock(bv);
328 the_locking_inset = 0;
329 updateLocal(bv, false);
334 if (text_.selection.set()) {
335 text_.clearSelection();
336 } else if (owner()) {
337 bv->owner()->setLayout(owner()->getLyXText(bv)
338 ->cursor.par()->layout()->name());
340 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
341 // hack for deleteEmptyParMech
342 ParagraphList::iterator first_par = paragraphs.begin();
343 if (!first_par->empty()) {
344 text_.setCursor(first_par, 0);
345 } else if (paragraphs.size() > 1) {
346 text_.setCursor(boost::next(first_par), 0);
351 void InsetText::lockInset(BufferView * bv)
354 the_locking_inset = 0;
355 inset_pos = inset_x = inset_y = 0;
356 inset_boundary = false;
357 inset_par = paragraphs.end();
358 old_par = paragraphs.end();
359 text_.setCursorIntern(paragraphs.begin(), 0);
360 text_.clearSelection();
362 // If the inset is empty set the language of the current font to the
363 // language to the surronding text (if different).
364 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
365 bv->getParentLanguage(this) != text_.current_font.language()) {
366 LyXFont font(LyXFont::ALL_IGNORE);
367 font.setLanguage(bv->getParentLanguage(this));
368 setFont(bv, font, false);
373 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
375 the_locking_inset = inset;
376 inset_x = cx() - top_x;
380 inset_boundary = cboundary();
384 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
386 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
387 << inset << "): " << endl;
390 if (!the_locking_inset) {
391 ParagraphList::iterator pit = paragraphs.begin();
392 ParagraphList::iterator pend = paragraphs.end();
394 for (; pit != pend; ++pit) {
395 InsetList::iterator it = pit->insetlist.begin();
396 InsetList::iterator const end = pit->insetlist.end();
397 for (; it != end; ++it) {
398 if (it->inset == inset) {
399 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
400 text_.setCursorIntern(pit, it->pos);
401 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
402 lyxerr << "bv: " << bv << " inset: " << inset << endl;
403 lockInset(bv, inset);
404 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
409 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
412 if (inset == cpar()->getInset(cpos())) {
413 lyxerr[Debug::INSETS] << "OK" << endl;
414 lockInset(bv, inset);
418 if (the_locking_inset && the_locking_inset == inset) {
419 if (cpar() == inset_par && cpos() == inset_pos) {
420 lyxerr[Debug::INSETS] << "OK" << endl;
421 inset_x = cx() - top_x;
424 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
426 } else if (the_locking_inset) {
427 lyxerr[Debug::INSETS] << "MAYBE" << endl;
428 return the_locking_inset->lockInsetInInset(bv, inset);
430 lyxerr[Debug::INSETS] << "NOT OK" << endl;
435 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
438 if (!the_locking_inset)
440 if (the_locking_inset == inset) {
441 the_locking_inset->insetUnlock(bv);
442 the_locking_inset = 0;
444 moveRightIntern(bv, true, false);
445 old_par = paragraphs.end(); // force layout setting
449 updateLocal(bv, false);
452 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
456 void InsetText::lfunMousePress(FuncRequest const & cmd)
460 // use this to check mouse motion for selection!
464 BufferView * bv = cmd.view();
465 FuncRequest cmd1 = cmd;
472 int tmp_y = cmd.y + dim_.asc - bv->top_y();
473 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
475 if (the_locking_inset) {
476 if (the_locking_inset == inset) {
477 the_locking_inset->localDispatch(cmd1);
480 // otherwise only unlock the_locking_inset
481 the_locking_inset->insetUnlock(bv);
482 the_locking_inset = 0;
485 no_selection = false;
487 if (bv->theLockingInset()) {
488 if (isHighlyEditableInset(inset)) {
489 // We just have to lock the inset before calling a
491 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
492 if (!bv->lockInset(uinset)) {
493 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
495 inset->localDispatch(cmd1);
496 if (the_locking_inset)
497 updateLocal(bv, false);
502 bool paste_internally = false;
503 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
504 localDispatch(FuncRequest(bv, LFUN_COPY));
505 paste_internally = true;
507 int old_top_y = bv->top_y();
509 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
510 // set the selection cursor!
511 text_.selection.cursor = text_.cursor;
512 text_.cursor.x_fix(text_.cursor.x());
514 text_.clearSelection();
515 updateLocal(bv, false);
517 bv->owner()->setLayout(cpar()->layout()->name());
519 // we moved the view we cannot do mouse selection in this case!
520 if (bv->top_y() != old_top_y)
523 // Insert primary selection with middle mouse
524 // if there is a local selection in the current buffer,
526 if (cmd.button() == mouse_button::button2) {
527 if (paste_internally)
528 localDispatch(FuncRequest(bv, LFUN_PASTE));
530 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
533 getLyXText(bv)->clearSelection();
538 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
540 BufferView * bv = cmd.view();
541 FuncRequest cmd1 = cmd;
546 if (the_locking_inset)
547 return the_locking_inset->localDispatch(cmd1);
550 int tmp_y = cmd.y + dim_.asc - bv->top_y();
551 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
554 // This code should probably be removed now. Simple insets
555 // (!highlyEditable) can actually take the localDispatch,
556 // and turn it into edit() if necessary. But we still
557 // need to deal properly with the whole relative vs.
558 // absolute mouse co-ords thing in a realiable, sensible way
560 if (isHighlyEditableInset(inset))
561 ret = inset->localDispatch(cmd1);
563 inset_x = cx(bv) - top_x;
565 cmd1.x = cmd.x - inset_x;
566 cmd1.y = cmd.x - inset_y;
567 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
571 ret = inset->localDispatch(cmd1);
572 updateLocal(bv, false);
579 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
581 FuncRequest cmd1 = cmd;
585 if (the_locking_inset) {
586 the_locking_inset->localDispatch(cmd1);
590 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
593 BufferView * bv = cmd.view();
594 LyXCursor cur = text_.cursor;
595 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
596 text_.cursor.x_fix(text_.cursor.x());
597 if (cur == text_.cursor)
599 text_.setSelection();
600 updateLocal(bv, false);
604 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
606 BufferView * bv = cmd.view();
609 switch (cmd.action) {
610 case LFUN_INSET_EDIT: {
611 UpdatableInset::localDispatch(cmd);
613 if (!bv->lockInset(this)) {
614 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
619 the_locking_inset = 0;
623 inset_boundary = false;
624 inset_par = paragraphs.end();
625 old_par = paragraphs.end();
628 if (cmd.argument.size()) {
629 if (cmd.argument == "left")
630 text_.setCursorIntern(paragraphs.begin(), 0);
632 ParagraphList::iterator it = boost::prior(paragraphs.end());
633 text_.setCursor(it, it->size());
636 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
637 // we put here -1 and not button as now the button in the
638 // edit call should not be needed we will fix this in 1.3.x
639 // cycle hopefully (Jug 20020509)
640 // FIXME: GUII I've changed this to none: probably WRONG
641 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
642 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
643 text_.cursor.x(text_.cursor.x());
644 text_.cursor.x_fix(text_.cursor.x());
648 text_.clearSelection();
651 // If the inset is empty set the language of the current font to the
652 // language to the surronding text (if different).
653 if (paragraphs.begin()->empty() &&
654 paragraphs.size() == 1 &&
655 bv->getParentLanguage(this) != text_.current_font.language())
657 LyXFont font(LyXFont::ALL_IGNORE);
658 font.setLanguage(bv->getParentLanguage(this));
659 setFont(bv, font, false);
662 updateLocal(bv, false);
663 // Tell the paragraph dialog that we've entered an insettext.
664 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
668 case LFUN_MOUSE_PRESS:
672 case LFUN_MOUSE_MOTION:
673 lfunMouseMotion(cmd);
676 case LFUN_MOUSE_RELEASE:
677 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
683 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
684 no_selection = false;
686 RESULT result = UpdatableInset::localDispatch(cmd);
687 if (result != UNDISPATCHED)
691 if (cmd.action < 0 && cmd.argument.empty())
694 if (the_locking_inset) {
695 result = the_locking_inset->localDispatch(cmd);
696 if (result == DISPATCHED_NOUPDATE)
698 if (result == DISPATCHED) {
699 updateLocal(bv, false);
702 if (result >= FINISHED) {
705 moveRightIntern(bv, false, false);
710 if (result >= FINISHED) {
711 updateLocal(bv, false);
712 bv->unlockInset(this);
716 result = moveDown(bv);
717 if (result >= FINISHED) {
718 updateLocal(bv, false);
719 bv->unlockInset(this);
726 the_locking_inset = 0;
727 updateLocal(bv, false);
728 // make sure status gets reset immediately
729 bv->owner()->clearMessage();
733 bool updflag = false;
735 switch (cmd.action) {
738 case LFUN_SELFINSERT:
739 if (bv->buffer()->isReadonly()) {
740 // setErrorMessage(N_("Document is read only"));
743 if (!cmd.argument.empty()) {
744 /* Automatically delete the currently selected
745 * text and replace it with what is being
746 * typed in now. Depends on lyxrc settings
747 * "auto_region_delete", which defaults to
750 // This should not be needed here and is also WRONG!
751 recordUndo(bv, Undo::INSERT, text_.cursor.par());
755 if (lyxrc.auto_region_delete && text_.selection.set())
756 text_.cutSelection(false, false);
757 text_.clearSelection();
759 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
760 bv->owner()->getIntl().getTransManager().
761 TranslateAndInsert(cmd.argument[i], &text_);
763 text_.selection.cursor = text_.cursor;
765 result = DISPATCHED_NOUPDATE;
768 // cursor movements that need special handling
771 result = moveRight(bv);
776 result = moveLeft(bv);
780 result = moveDown(bv);
788 if (crow() == text_.firstRow())
789 result = FINISHED_UP;
791 text_.cursorPrevious();
792 text_.clearSelection();
793 result = DISPATCHED_NOUPDATE;
798 if (crow() == text_.lastRow())
799 result = FINISHED_DOWN;
802 text_.clearSelection();
803 result = DISPATCHED_NOUPDATE;
808 if (text_.selection.set())
809 text_.cutSelection(true, false);
816 if (text_.selection.set())
817 text_.cutSelection(true, false);
824 if (!autoBreakRows_) {
825 if (CutAndPaste::nrOfParagraphs() > 1) {
827 #warning FIXME horrendously bad UI
829 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
834 replaceSelection(bv->getLyXText());
835 size_t sel_index = 0;
836 string const & arg = cmd.argument;
837 if (isStrUnsignedInt(arg)) {
838 size_t const paste_arg = strToUnsignedInt(arg);
839 #warning FIXME Check if the arg is in the domain of available selections.
840 sel_index = paste_arg;
842 text_.pasteSelection(sel_index);
844 text_.clearSelection();
849 case LFUN_BREAKPARAGRAPH:
850 if (!autoBreakRows_) {
854 replaceSelection(bv->getLyXText());
855 text_.breakParagraph(paragraphs, 0);
859 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
860 if (!autoBreakRows_) {
864 replaceSelection(bv->getLyXText());
865 text_.breakParagraph(paragraphs, 1);
869 case LFUN_BREAKLINE: {
870 if (!autoBreakRows_) {
875 replaceSelection(bv->getLyXText());
876 text_.insertInset(new InsetNewline);
882 // do not set layouts on non breakable textinsets
883 if (autoBreakRows_) {
884 string cur_layout = cpar()->layout()->name();
886 // Derive layout number from given argument (string)
887 // and current buffer's textclass (number).
888 LyXTextClass const & tclass =
889 bv->buffer()->params().getLyXTextClass();
890 string layout = cmd.argument;
891 bool hasLayout = tclass.hasLayout(layout);
893 // If the entry is obsolete, use the new one instead.
895 string const & obs = tclass[layout]->obsoleted_by();
900 // see if we found the layout number:
902 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
903 bv->owner()->dispatch(lf);
907 if (cur_layout != layout) {
909 text_.setLayout(layout);
910 bv->owner()->setLayout(cpar()->layout()->name());
914 // reset the layout box
915 bv->owner()->setLayout(cpar()->layout()->name());
920 if (!bv->dispatch(cmd))
921 result = UNDISPATCHED;
925 updateLocal(bv, updflag);
927 /// If the action has deleted all text in the inset, we need to change the
928 // language to the language of the surronding text.
929 if (!was_empty && paragraphs.begin()->empty() &&
930 paragraphs.size() == 1) {
931 LyXFont font(LyXFont::ALL_IGNORE);
932 font.setLanguage(bv->getParentLanguage(this));
933 setFont(bv, font, false);
936 if (result >= FINISHED)
937 bv->unlockInset(this);
939 if (result == DISPATCHED_NOUPDATE)
945 int InsetText::latex(Buffer const & buf, ostream & os,
946 LatexRunParams const & runparams) const
949 latexParagraphs(buf, paragraphs, os, texrow, runparams);
950 return texrow.rows();
954 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
956 unsigned int lines = 0;
958 ParagraphList::const_iterator beg = paragraphs.begin();
959 ParagraphList::const_iterator end = paragraphs.end();
960 ParagraphList::const_iterator it = beg;
961 for (; it != end; ++it) {
962 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
963 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
970 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
972 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
973 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
975 // There is a confusion between the empty paragraph and the default paragraph
976 // The default paragraph is <p></p>, the empty paragraph is *empty*
977 // Since none of the floats of linuxdoc accepts standard paragraphs
978 // I disable them. I don't expect problems. (jamatos 2003/07/27)
979 for (; pit != pend; ++pit) {
980 const string name = pit->layout()->latexname();
982 sgml::openTag(os, 1, 0, name);
983 buf.simpleLinuxDocOnePar(os, pit, 0);
985 sgml::closeTag(os, 1, 0, name);
991 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
993 unsigned int lines = 0;
995 vector<string> environment_stack(10);
996 vector<string> environment_inner(10);
998 int const command_depth = 0;
1001 Paragraph::depth_type depth = 0; // paragraph depth
1003 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1004 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1006 for (; pit != pend; ++pit) {
1007 int desc_on = 0; // description mode
1009 LyXLayout_ptr const & style = pit->layout();
1011 // environment tag closing
1012 for (; depth > pit->params().depth(); --depth) {
1013 if (environment_inner[depth] != "!-- --") {
1014 item_name = "listitem";
1015 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1016 if (environment_inner[depth] == "varlistentry")
1017 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1019 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1020 environment_stack[depth].erase();
1021 environment_inner[depth].erase();
1024 if (depth == pit->params().depth()
1025 && environment_stack[depth] != style->latexname()
1026 && !environment_stack[depth].empty()) {
1027 if (environment_inner[depth] != "!-- --") {
1028 item_name= "listitem";
1029 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1030 if (environment_inner[depth] == "varlistentry")
1031 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1034 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1036 environment_stack[depth].erase();
1037 environment_inner[depth].erase();
1040 // Write opening SGML tags.
1041 switch (style->latextype) {
1042 case LATEX_PARAGRAPH:
1043 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1047 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1051 case LATEX_ENVIRONMENT:
1052 case LATEX_ITEM_ENVIRONMENT:
1053 if (depth < pit->params().depth()) {
1054 depth = pit->params().depth();
1055 environment_stack[depth].erase();
1058 if (environment_stack[depth] != style->latexname()) {
1059 if (environment_stack.size() == depth + 1) {
1060 environment_stack.push_back("!-- --");
1061 environment_inner.push_back("!-- --");
1063 environment_stack[depth] = style->latexname();
1064 environment_inner[depth] = "!-- --";
1065 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1067 if (environment_inner[depth] != "!-- --") {
1068 item_name= "listitem";
1069 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1070 if (environment_inner[depth] == "varlistentry")
1071 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1075 if (style->latextype == LATEX_ENVIRONMENT) {
1076 if (!style->latexparam().empty()) {
1077 if (style->latexparam() == "CDATA")
1080 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1085 desc_on = (style->labeltype == LABEL_MANUAL);
1087 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1088 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1090 item_name = desc_on ? "term" : "para";
1091 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1095 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1099 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1102 // write closing SGML tags
1103 switch (style->latextype) {
1104 case LATEX_ENVIRONMENT:
1105 if (!style->latexparam().empty()) {
1106 if (style->latexparam() == "CDATA")
1109 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1112 case LATEX_ITEM_ENVIRONMENT:
1116 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1118 case LATEX_PARAGRAPH:
1119 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1122 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1128 for (int d = depth; d >= 0; --d) {
1129 if (!environment_stack[depth].empty()) {
1130 if (environment_inner[depth] != "!-- --") {
1131 item_name = "listitem";
1132 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1133 if (environment_inner[depth] == "varlistentry")
1134 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1137 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1145 void InsetText::validate(LaTeXFeatures & features) const
1147 for_each(paragraphs.begin(), paragraphs.end(),
1148 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1152 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1154 if (the_locking_inset) {
1155 the_locking_inset->getCursor(bv, x, y);
1159 y = cy() + InsetText::y();
1163 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1165 if (the_locking_inset) {
1166 the_locking_inset->getCursorPos(bv, x, y);
1169 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1170 y = cy() - TEXT_TO_INSET_OFFSET;
1174 int InsetText::insetInInsetY() const
1176 if (!the_locking_inset)
1179 return inset_y + the_locking_inset->insetInInsetY();
1183 void InsetText::fitInsetCursor(BufferView * bv) const
1185 if (the_locking_inset) {
1186 the_locking_inset->fitInsetCursor(bv);
1190 LyXFont const font = text_.getFont(cpar(), cpos());
1192 int const asc = font_metrics::maxAscent(font);
1193 int const desc = font_metrics::maxDescent(font);
1195 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1199 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1201 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1202 return moveLeftIntern(bv, false, true, false);
1204 return moveRightIntern(bv, true, true, false);
1208 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1210 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1211 return moveRightIntern(bv, true, true, false);
1213 return moveLeftIntern(bv, false, true, false);
1218 InsetText::moveRightIntern(BufferView * bv, bool front,
1219 bool activate_inset, bool selecting)
1221 ParagraphList::iterator c_par = cpar();
1223 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1224 return FINISHED_RIGHT;
1225 if (activate_inset && checkAndActivateInset(bv, front))
1227 text_.cursorRight(bv);
1229 text_.clearSelection();
1230 return DISPATCHED_NOUPDATE;
1235 InsetText::moveLeftIntern(BufferView * bv, bool front,
1236 bool activate_inset, bool selecting)
1238 if (cpar() == paragraphs.begin() && cpos() <= 0)
1240 text_.cursorLeft(bv);
1242 text_.clearSelection();
1243 if (activate_inset && checkAndActivateInset(bv, front))
1245 return DISPATCHED_NOUPDATE;
1249 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1251 if (crow() == text_.firstRow())
1254 text_.clearSelection();
1255 return DISPATCHED_NOUPDATE;
1259 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1261 if (crow() == text_.lastRow())
1262 return FINISHED_DOWN;
1263 text_.cursorDown(bv);
1264 text_.clearSelection();
1265 return DISPATCHED_NOUPDATE;
1269 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1271 if (the_locking_inset) {
1272 if (the_locking_inset->insetAllowed(inset))
1273 return the_locking_inset->insertInset(bv, inset);
1276 inset->setOwner(this);
1277 text_.insertInset(inset);
1279 updateLocal(bv, true);
1284 bool InsetText::insetAllowed(InsetOld::Code code) const
1286 // in_insetAllowed is a really gross hack,
1287 // to allow us to call the owner's insetAllowed
1288 // without stack overflow, which can happen
1289 // when the owner uses InsetCollapsable::insetAllowed()
1291 if (in_insetAllowed)
1293 in_insetAllowed = true;
1294 if (the_locking_inset)
1295 ret = the_locking_inset->insetAllowed(code);
1297 ret = owner()->insetAllowed(code);
1298 in_insetAllowed = false;
1303 UpdatableInset * InsetText::getLockingInset() const
1305 return the_locking_inset ? the_locking_inset->getLockingInset() :
1306 const_cast<InsetText *>(this);
1310 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1314 if (the_locking_inset)
1315 return the_locking_inset->getFirstLockingInsetOfType(c);
1320 bool InsetText::showInsetDialog(BufferView * bv) const
1322 if (the_locking_inset)
1323 return the_locking_inset->showInsetDialog(bv);
1328 void InsetText::getLabelList(Buffer const & buffer,
1329 std::vector<string> & list) const
1331 ParagraphList::const_iterator pit = paragraphs.begin();
1332 ParagraphList::const_iterator pend = paragraphs.end();
1333 for (; pit != pend; ++pit) {
1334 InsetList::const_iterator beg = pit->insetlist.begin();
1335 InsetList::const_iterator end = pit->insetlist.end();
1336 for (; beg != end; ++beg)
1337 beg->inset->getLabelList(buffer, list);
1342 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1345 if (the_locking_inset) {
1346 the_locking_inset->setFont(bv, font, toggleall, selectall);
1350 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1351 || cpar()->empty()) {
1352 text_.setFont(font, toggleall);
1357 if (text_.selection.set())
1358 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1362 text_.selection.cursor = text_.cursor;
1363 text_.cursorBottom();
1364 text_.setSelection();
1367 text_.toggleFree(font, toggleall);
1370 text_.clearSelection();
1373 updateLocal(bv, true);
1377 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1379 if (cpos() == cpar()->size())
1381 InsetOld * inset = cpar()->getInset(cpos());
1382 if (!isHighlyEditableInset(inset))
1384 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1385 inset->localDispatch(cmd);
1386 if (!the_locking_inset)
1388 updateLocal(bv, false);
1393 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1394 mouse_button::state button)
1397 int dummyy = y + dim_.asc;
1398 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1399 // we only do the edit() call if the inset was hit by the mouse
1400 // or if it is a highly editable inset. So we should call this
1401 // function from our own edit with button < 0.
1402 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1404 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1413 inset_x = cx() - top_x;
1415 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1416 inset->localDispatch(cmd);
1417 if (!the_locking_inset)
1419 updateLocal(bv, false);
1424 void InsetText::markNew(bool track_changes)
1426 ParagraphList::iterator pit = paragraphs.begin();
1427 ParagraphList::iterator end = paragraphs.end();
1428 for (; pit != end; ++pit) {
1429 if (track_changes) {
1430 pit->trackChanges();
1432 // no-op when not tracking
1433 pit->cleanChanges();
1439 void InsetText::setText(string const & data, LyXFont const & font)
1442 for (unsigned int i = 0; i < data.length(); ++i)
1443 paragraphs.begin()->insertChar(i, data[i], font);
1447 void InsetText::setAutoBreakRows(bool flag)
1449 if (flag != autoBreakRows_) {
1450 autoBreakRows_ = flag;
1457 void InsetText::setDrawFrame(DrawFrame how)
1463 LColor_color InsetText::frameColor() const
1465 return LColor::color(frame_color_);
1469 void InsetText::setFrameColor(LColor_color col)
1475 int InsetText::cx() const
1477 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1478 if (the_locking_inset) {
1479 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1480 if (font.isVisibleRightToLeft())
1481 x -= the_locking_inset->width();
1487 int InsetText::cy() const
1489 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1493 pos_type InsetText::cpos() const
1495 return text_.cursor.pos();
1499 ParagraphList::iterator InsetText::cpar() const
1501 return text_.cursor.par();
1505 bool InsetText::cboundary() const
1507 return text_.cursor.boundary();
1511 RowList::iterator InsetText::crow() const
1513 return text_.cursorRow();
1517 LyXText * InsetText::getLyXText(BufferView const * bv,
1518 bool const recursive) const
1521 if (recursive && the_locking_inset)
1522 return the_locking_inset->getLyXText(bv, true);
1527 void InsetText::setViewCache(BufferView const * bv) const
1530 if (bv != text_.bv_owner) {
1531 //lyxerr << "setting view cache from "
1532 // << text_.bv_owner << " to " << bv << "\n";
1533 text_.init(const_cast<BufferView *>(bv));
1535 text_.bv_owner = const_cast<BufferView *>(bv);
1540 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1543 /// then remove all LyXText in text-insets
1544 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1545 const_cast<ParagraphList&>(paragraphs).end(),
1546 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1551 void InsetText::removeNewlines()
1553 ParagraphList::iterator it = paragraphs.begin();
1554 ParagraphList::iterator end = paragraphs.end();
1555 for (; it != end; ++it)
1556 for (int i = 0; i < it->size(); ++i)
1557 if (it->isNewline(i))
1562 int InsetText::scroll(bool recursive) const
1564 int sx = UpdatableInset::scroll(false);
1566 if (recursive && the_locking_inset)
1567 sx += the_locking_inset->scroll(recursive);
1573 void InsetText::clearSelection(BufferView *)
1575 text_.clearSelection();
1579 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1581 Painter & pain = bv->painter();
1583 int h = dim_.asc + dim_.des;
1584 int ty = baseline - dim_.asc;
1590 if (ty + h > pain.paperHeight())
1591 h = pain.paperHeight();
1592 if (top_x + w > pain.paperWidth())
1593 w = pain.paperWidth();
1594 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1598 ParagraphList * InsetText::getParagraphs(int i) const
1600 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1604 LyXCursor const & InsetText::cursor(BufferView * bv) const
1606 if (the_locking_inset)
1607 return the_locking_inset->cursor(bv);
1608 return getLyXText(bv)->cursor;
1612 InsetOld * InsetText::getInsetFromID(int id_arg) const
1615 return const_cast<InsetText *>(this);
1617 ParagraphList::const_iterator pit = paragraphs.begin();
1618 ParagraphList::const_iterator pend = paragraphs.end();
1619 for (; pit != pend; ++pit) {
1620 InsetList::const_iterator it = pit->insetlist.begin();
1621 InsetList::const_iterator end = pit->insetlist.end();
1622 for (; it != end; ++it) {
1623 if (it->inset->id() == id_arg)
1625 InsetOld * in = it->inset->getInsetFromID(id_arg);
1635 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1638 if (the_locking_inset) {
1639 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1640 if (!word.word().empty()) {
1644 // we have to go on checking so move cursor to the next char
1645 text_.cursor.pos(text_.cursor.pos() + 1);
1647 word = text_.selectNextWordToSpellcheck(value);
1648 if (word.word().empty())
1649 bv->unlockInset(const_cast<InsetText *>(this));
1656 void InsetText::selectSelectedWord(BufferView * bv)
1658 if (the_locking_inset) {
1659 the_locking_inset->selectSelectedWord(bv);
1662 getLyXText(bv)->selectSelectedWord();
1663 updateLocal(bv, false);
1667 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1669 if (the_locking_inset) {
1670 if (the_locking_inset->nextChange(bv, length))
1672 text_.cursorRight(true);
1674 lyx::find::SearchResult result =
1675 lyx::find::findNextChange(bv, &text_, length);
1677 if (result == lyx::find::SR_FOUND) {
1678 LyXCursor cur = text_.cursor;
1679 bv->unlockInset(bv->theLockingInset());
1680 if (bv->lockInset(this))
1683 text_.setSelectionRange(length);
1684 updateLocal(bv, false);
1686 return result != lyx::find::SR_NOT_FOUND;
1690 bool InsetText::searchForward(BufferView * bv, string const & str,
1693 if (the_locking_inset) {
1694 if (the_locking_inset->searchForward(bv, str, cs, mw))
1696 text_.cursorRight(true);
1698 lyx::find::SearchResult result =
1699 lyx::find::find(bv, &text_, str, true, cs, mw);
1701 if (result == lyx::find::SR_FOUND) {
1702 LyXCursor cur = text_.cursor;
1703 bv->unlockInset(bv->theLockingInset());
1704 if (bv->lockInset(this))
1707 text_.setSelectionRange(str.length());
1708 updateLocal(bv, false);
1710 return result != lyx::find::SR_NOT_FOUND;
1714 bool InsetText::searchBackward(BufferView * bv, string const & str,
1717 if (the_locking_inset) {
1718 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1722 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1723 text_.setCursor(pit, pit->size());
1725 lyx::find::SearchResult result =
1726 lyx::find::find(bv, &text_, str, false, cs, mw);
1728 if (result == lyx::find::SR_FOUND) {
1729 LyXCursor cur = text_.cursor;
1730 bv->unlockInset(bv->theLockingInset());
1731 if (bv->lockInset(this))
1734 text_.setSelectionRange(str.length());
1735 updateLocal(bv, false);
1737 return result != lyx::find::SR_NOT_FOUND;
1741 bool InsetText::checkInsertChar(LyXFont & font)
1743 return owner() ? owner()->checkInsertChar(font) : true;
1747 void InsetText::collapseParagraphs(BufferView * bv)
1749 while (paragraphs.size() > 1) {
1750 ParagraphList::iterator first_par = paragraphs.begin();
1751 ParagraphList::iterator next_par = boost::next(first_par);
1752 size_t const first_par_size = first_par->size();
1754 if (!first_par->empty() &&
1755 !next_par->empty() &&
1756 !first_par->isSeparator(first_par_size - 1)) {
1757 first_par->insertChar(first_par_size, ' ');
1760 if (text_.selection.set()) {
1761 if (text_.selection.start.par() == next_par) {
1762 text_.selection.start.par(first_par);
1763 text_.selection.start.pos(
1764 text_.selection.start.pos() + first_par_size);
1766 if (text_.selection.end.par() == next_par) {
1767 text_.selection.end.par(first_par);
1768 text_.selection.end.pos(
1769 text_.selection.end.pos() + first_par_size);
1773 mergeParagraph(bv->buffer()->params(), paragraphs, first_par);
1778 void InsetText::getDrawFont(LyXFont & font) const
1782 owner()->getDrawFont(font);
1786 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1788 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1789 // And it probably does. You have to take a look at this John. (Lgb)
1790 #warning John, have a look here. (Lgb)
1791 ParagraphList::iterator pit = plist.begin();
1792 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1794 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1796 ParagraphList::iterator pend = plist.end();
1797 for (; pit != pend; ++pit)
1798 paragraphs.push_back(*pit);
1802 void InsetText::addPreview(PreviewLoader & loader) const
1804 ParagraphList::const_iterator pit = paragraphs.begin();
1805 ParagraphList::const_iterator pend = paragraphs.end();
1807 for (; pit != pend; ++pit) {
1808 InsetList::const_iterator it = pit->insetlist.begin();
1809 InsetList::const_iterator end = pit->insetlist.end();
1810 for (; it != end; ++it)
1811 it->inset->addPreview(loader);