3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettext.h"
14 #include "insetnewline.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
20 #include "errorlist.h"
21 #include "funcrequest.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
30 #include "ParagraphParameters.h"
31 #include "rowpainter.h"
33 #include "undo_funcs.h"
34 #include "WordLangTuple.h"
36 #include "frontends/Alert.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/Painter.h"
41 #include "support/lyxalgo.h" // lyx::count
43 #include <boost/bind.hpp>
53 using namespace lyx::support;
54 using namespace lyx::graphics;
55 using namespace bv_funcs;
60 InsetText::InsetText(BufferParams const & bp)
61 : UpdatableInset(), text_(0, this, true, paragraphs)
63 paragraphs.push_back(Paragraph());
64 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
65 if (bp.tracking_changes)
66 paragraphs.begin()->trackChanges();
71 InsetText::InsetText(InsetText const & in)
72 : UpdatableInset(in), text_(0, this, true, paragraphs)
78 InsetText & InsetText::operator=(InsetText const & it)
85 void InsetText::init(InsetText const * ins)
88 textwidth_ = ins->textwidth_;
89 text_.bv_owner = ins->text_.bv_owner;
91 paragraphs = ins->paragraphs;
93 ParagraphList::iterator pit = paragraphs.begin();
94 ParagraphList::iterator end = paragraphs.end();
95 for (; pit != end; ++pit)
96 pit->setInsetOwner(this);
98 autoBreakRows = ins->autoBreakRows;
99 drawFrame_ = ins->drawFrame_;
100 frame_color = ins->frame_color;
102 textwidth_ = 0; // broken
104 frame_color = LColor::insetframe;
105 autoBreakRows = false;
107 the_locking_inset = 0;
108 for_each(paragraphs.begin(), paragraphs.end(),
109 boost::bind(&Paragraph::setInsetOwner, _1, this));
113 old_par = paragraphs.end();
114 in_insetAllowed = false;
118 void InsetText::clear(bool just_mark_erased)
120 if (just_mark_erased) {
121 ParagraphList::iterator it = paragraphs.begin();
122 ParagraphList::iterator end = paragraphs.end();
123 for (; it != end; ++it)
128 // This is a gross hack...
129 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
132 paragraphs.push_back(Paragraph());
133 paragraphs.begin()->setInsetOwner(this);
134 paragraphs.begin()->layout(old_layout);
138 auto_ptr<InsetBase> InsetText::clone() const
140 return auto_ptr<InsetBase>(new InsetText(*this));
144 void InsetText::write(Buffer const & buf, ostream & os) const
147 writeParagraphData(buf, os);
151 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
153 ParagraphList::const_iterator it = paragraphs.begin();
154 ParagraphList::const_iterator end = paragraphs.end();
155 Paragraph::depth_type dth = 0;
156 for (; it != end; ++it) {
157 it->write(buf, os, buf.params(), dth);
162 void InsetText::read(Buffer const & buf, LyXLex & lex)
165 Paragraph::depth_type depth = 0;
169 if (buf.params().tracking_changes)
170 paragraphs.begin()->trackChanges();
172 // delete the initial paragraph
174 ParagraphList::iterator pit = paragraphs.begin();
178 token = lex.getString();
181 if (token == "\\end_inset") {
185 if (token == "\\end_document") {
186 lex.printError("\\end_document read in inset! Error in document!");
191 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
194 pit = paragraphs.begin();
195 ParagraphList::iterator const end = paragraphs.end();
196 for (; pit != end; ++pit)
197 pit->setInsetOwner(this);
199 if (token != "\\end_inset") {
200 lex.printError("Missing \\end_inset at this point. "
206 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
208 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
210 textwidth_ = mi.base.textwidth;
211 BufferView * bv = mi.base.bv;
213 text_.metrics(mi, dim);
214 dim.asc += TEXT_TO_INSET_OFFSET;
215 dim.des += TEXT_TO_INSET_OFFSET;
216 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
217 dim.wid = max(dim.wid, 10);
222 int InsetText::textWidth() const
228 void InsetText::draw(PainterInfo & pi, int x, int y) const
230 // update our idea of where we are. Clearly, we should
231 // not have to know this information.
234 int const start_x = x;
236 BufferView * bv = pi.base.bv;
237 Painter & pain = pi.pain;
239 // repaint the background if needed
240 if (backgroundColor() != LColor::background)
241 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
243 // no draw is necessary !!!
244 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
255 top_y = y - dim_.asc;
257 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
262 x += TEXT_TO_INSET_OFFSET;
264 paintTextInset(*bv, text_, x, y);
266 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
267 drawFrame(pain, start_x);
271 void InsetText::drawFrame(Painter & pain, int x) const
273 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
274 int const frame_x = x + ttoD2;
275 int const frame_y = top_baseline - dim_.asc + ttoD2;
276 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
277 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
278 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
282 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
287 if (!autoBreakRows && paragraphs.size() > 1)
288 collapseParagraphs(bv);
290 if (!text_.selection.set())
291 text_.selection.cursor = text_.cursor;
294 bv->updateInset(this);
295 bv->owner()->view_state_changed();
296 bv->owner()->updateMenubar();
297 bv->owner()->updateToolbar();
298 if (old_par != cpar()) {
299 bv->owner()->setLayout(cpar()->layout()->name());
305 string const InsetText::editMessage() const
307 return _("Opened Text Inset");
311 void InsetText::insetUnlock(BufferView * bv)
313 if (the_locking_inset) {
314 the_locking_inset->insetUnlock(bv);
315 the_locking_inset = 0;
316 updateLocal(bv, false);
321 if (text_.selection.set()) {
322 text_.clearSelection();
323 } else if (owner()) {
324 bv->owner()->setLayout(owner()->getLyXText(bv)
325 ->cursor.par()->layout()->name());
327 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
328 // hack for deleteEmptyParMech
329 ParagraphList::iterator first_par = paragraphs.begin();
330 if (!first_par->empty()) {
331 text_.setCursor(first_par, 0);
332 } else if (paragraphs.size() > 1) {
333 text_.setCursor(boost::next(first_par), 0);
338 void InsetText::lockInset(BufferView * bv)
341 the_locking_inset = 0;
342 inset_pos = inset_x = inset_y = 0;
343 inset_boundary = false;
344 inset_par = paragraphs.end();
345 old_par = paragraphs.end();
346 text_.setCursorIntern(paragraphs.begin(), 0);
347 text_.clearSelection();
349 // If the inset is empty set the language of the current font to the
350 // language to the surronding text (if different).
351 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
352 bv->getParentLanguage(this) != text_.current_font.language()) {
353 LyXFont font(LyXFont::ALL_IGNORE);
354 font.setLanguage(bv->getParentLanguage(this));
355 setFont(bv, font, false);
360 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
362 the_locking_inset = inset;
363 inset_x = cx() - top_x;
367 inset_boundary = cboundary();
371 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
373 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
374 << inset << "): " << endl;
377 if (!the_locking_inset) {
378 ParagraphList::iterator pit = paragraphs.begin();
379 ParagraphList::iterator pend = paragraphs.end();
381 int const id = inset->id();
382 lyxerr << "inset:" << inset << " " << id << endl;
383 for (; pit != pend; ++pit) {
384 InsetList::iterator it = pit->insetlist.begin();
385 InsetList::iterator const end = pit->insetlist.end();
386 for (; it != end; ++it) {
387 lyxerr << "it->inset:" << it->inset << endl;
388 if (it->inset == inset) {
389 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
390 text_.setCursorIntern(pit, it->pos);
391 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
392 lyxerr << "bv: " << bv << " inset: " << inset << endl;
393 lockInset(bv, inset);
394 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
397 if (it->inset->getInsetFromID(id)) {
398 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
399 text_.setCursorIntern(pit, it->pos);
400 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
401 lyxerr << "recurse" << endl;
402 return the_locking_inset->lockInsetInInset(bv, inset);
406 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
409 if (inset == cpar()->getInset(cpos())) {
410 lyxerr[Debug::INSETS] << "OK" << endl;
411 lockInset(bv, inset);
415 if (the_locking_inset && the_locking_inset == inset) {
416 if (cpar() == inset_par && cpos() == inset_pos) {
417 lyxerr[Debug::INSETS] << "OK" << endl;
418 inset_x = cx() - top_x;
421 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
423 } else if (the_locking_inset) {
424 lyxerr[Debug::INSETS] << "MAYBE" << endl;
425 return the_locking_inset->lockInsetInInset(bv, inset);
427 lyxerr[Debug::INSETS] << "NOT OK" << endl;
432 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
435 if (!the_locking_inset)
437 if (the_locking_inset == inset) {
438 the_locking_inset->insetUnlock(bv);
439 the_locking_inset = 0;
441 moveRightIntern(bv, true, false);
442 old_par = paragraphs.end(); // force layout setting
446 updateLocal(bv, false);
449 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
453 void InsetText::lfunMousePress(FuncRequest const & cmd)
457 // use this to check mouse motion for selection!
461 BufferView * bv = cmd.view();
462 FuncRequest cmd1 = cmd;
469 int tmp_y = cmd.y + dim_.asc - bv->top_y();
470 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
472 if (the_locking_inset) {
473 if (the_locking_inset == inset) {
474 the_locking_inset->localDispatch(cmd1);
477 // otherwise only unlock the_locking_inset
478 the_locking_inset->insetUnlock(bv);
479 the_locking_inset = 0;
482 no_selection = false;
484 if (bv->theLockingInset()) {
485 if (isHighlyEditableInset(inset)) {
486 // We just have to lock the inset before calling a
488 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
489 if (!bv->lockInset(uinset)) {
490 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
492 inset->localDispatch(cmd1);
493 if (the_locking_inset)
494 updateLocal(bv, false);
499 bool paste_internally = false;
500 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
501 localDispatch(FuncRequest(bv, LFUN_COPY));
502 paste_internally = true;
504 int old_top_y = bv->top_y();
506 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
507 // set the selection cursor!
508 text_.selection.cursor = text_.cursor;
509 text_.cursor.x_fix(text_.cursor.x());
511 text_.clearSelection();
512 updateLocal(bv, false);
514 bv->owner()->setLayout(cpar()->layout()->name());
516 // we moved the view we cannot do mouse selection in this case!
517 if (bv->top_y() != old_top_y)
520 // Insert primary selection with middle mouse
521 // if there is a local selection in the current buffer,
523 if (cmd.button() == mouse_button::button2) {
524 if (paste_internally)
525 localDispatch(FuncRequest(bv, LFUN_PASTE));
527 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
530 getLyXText(bv)->clearSelection();
535 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
537 BufferView * bv = cmd.view();
538 FuncRequest cmd1 = cmd;
543 if (the_locking_inset)
544 return the_locking_inset->localDispatch(cmd1);
547 int tmp_y = cmd.y + dim_.asc - bv->top_y();
548 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
551 // This code should probably be removed now. Simple insets
552 // (!highlyEditable) can actually take the localDispatch,
553 // and turn it into edit() if necessary. But we still
554 // need to deal properly with the whole relative vs.
555 // absolute mouse co-ords thing in a realiable, sensible way
557 if (isHighlyEditableInset(inset))
558 ret = inset->localDispatch(cmd1);
560 inset_x = cx(bv) - top_x;
562 cmd1.x = cmd.x - inset_x;
563 cmd1.y = cmd.x - inset_y;
564 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
568 ret = inset->localDispatch(cmd1);
569 updateLocal(bv, false);
576 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
578 FuncRequest cmd1 = cmd;
582 if (the_locking_inset) {
583 the_locking_inset->localDispatch(cmd1);
587 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
590 BufferView * bv = cmd.view();
591 LyXCursor cur = text_.cursor;
592 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
593 text_.cursor.x_fix(text_.cursor.x());
594 if (cur == text_.cursor)
596 text_.setSelection();
597 updateLocal(bv, false);
601 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
603 BufferView * bv = cmd.view();
606 switch (cmd.action) {
607 case LFUN_INSET_EDIT: {
608 UpdatableInset::localDispatch(cmd);
610 if (!bv->lockInset(this)) {
611 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
616 the_locking_inset = 0;
620 inset_boundary = false;
621 inset_par = paragraphs.end();
622 old_par = paragraphs.end();
625 if (cmd.argument.size()) {
626 if (cmd.argument == "left")
627 text_.setCursorIntern(paragraphs.begin(), 0);
629 ParagraphList::iterator it = boost::prior(paragraphs.end());
630 text_.setCursor(it, it->size());
633 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
634 // we put here -1 and not button as now the button in the
635 // edit call should not be needed we will fix this in 1.3.x
636 // cycle hopefully (Jug 20020509)
637 // FIXME: GUII I've changed this to none: probably WRONG
638 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
639 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
640 text_.cursor.x(text_.cursor.x());
641 text_.cursor.x_fix(text_.cursor.x());
645 text_.clearSelection();
648 // If the inset is empty set the language of the current font to the
649 // language to the surronding text (if different).
650 if (paragraphs.begin()->empty() &&
651 paragraphs.size() == 1 &&
652 bv->getParentLanguage(this) != text_.current_font.language())
654 LyXFont font(LyXFont::ALL_IGNORE);
655 font.setLanguage(bv->getParentLanguage(this));
656 setFont(bv, font, false);
659 updateLocal(bv, false);
660 // Tell the paragraph dialog that we've entered an insettext.
661 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
665 case LFUN_MOUSE_PRESS:
669 case LFUN_MOUSE_MOTION:
670 lfunMouseMotion(cmd);
673 case LFUN_MOUSE_RELEASE:
674 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
680 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
681 no_selection = false;
683 RESULT result = UpdatableInset::localDispatch(cmd);
684 if (result != UNDISPATCHED)
688 if (cmd.action < 0 && cmd.argument.empty())
691 if (the_locking_inset) {
692 result = the_locking_inset->localDispatch(cmd);
693 if (result == DISPATCHED_NOUPDATE)
695 if (result == DISPATCHED) {
696 updateLocal(bv, false);
699 if (result >= FINISHED) {
702 moveRightIntern(bv, false, false);
707 if (result >= FINISHED) {
708 updateLocal(bv, false);
709 bv->unlockInset(this);
713 result = moveDown(bv);
714 if (result >= FINISHED) {
715 updateLocal(bv, false);
716 bv->unlockInset(this);
723 the_locking_inset = 0;
724 updateLocal(bv, false);
725 // make sure status gets reset immediately
726 bv->owner()->clearMessage();
730 bool updflag = false;
732 switch (cmd.action) {
735 case LFUN_SELFINSERT:
736 if (bv->buffer()->isReadonly()) {
737 // setErrorMessage(N_("Document is read only"));
740 if (!cmd.argument.empty()) {
741 /* Automatically delete the currently selected
742 * text and replace it with what is being
743 * typed in now. Depends on lyxrc settings
744 * "auto_region_delete", which defaults to
747 // This should not be needed here and is also WRONG!
748 recordUndo(bv, Undo::INSERT, text_.cursor.par());
752 if (lyxrc.auto_region_delete && text_.selection.set())
753 text_.cutSelection(false, false);
754 text_.clearSelection();
756 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
757 bv->owner()->getIntl().getTransManager().
758 TranslateAndInsert(cmd.argument[i], &text_);
760 text_.selection.cursor = text_.cursor;
762 result = DISPATCHED_NOUPDATE;
765 // cursor movements that need special handling
768 result = moveRight(bv);
773 result = moveLeft(bv);
777 result = moveDown(bv);
785 if (crow() == text_.firstRow())
786 result = FINISHED_UP;
788 text_.cursorPrevious();
789 text_.clearSelection();
790 result = DISPATCHED_NOUPDATE;
795 if (crow() == text_.lastRow())
796 result = FINISHED_DOWN;
799 text_.clearSelection();
800 result = DISPATCHED_NOUPDATE;
805 if (text_.selection.set())
806 text_.cutSelection(true, false);
813 if (text_.selection.set())
814 text_.cutSelection(true, false);
821 if (!autoBreakRows) {
822 if (CutAndPaste::nrOfParagraphs() > 1) {
824 #warning FIXME horrendously bad UI
826 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
831 replaceSelection(bv->getLyXText());
832 size_t sel_index = 0;
833 string const & arg = cmd.argument;
834 if (isStrUnsignedInt(arg)) {
835 size_t const paste_arg = strToUnsignedInt(arg);
836 #warning FIXME Check if the arg is in the domain of available selections.
837 sel_index = paste_arg;
839 text_.pasteSelection(sel_index);
841 text_.clearSelection();
846 case LFUN_BREAKPARAGRAPH:
847 if (!autoBreakRows) {
851 replaceSelection(bv->getLyXText());
852 text_.breakParagraph(paragraphs, 0);
856 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
857 if (!autoBreakRows) {
861 replaceSelection(bv->getLyXText());
862 text_.breakParagraph(paragraphs, 1);
866 case LFUN_BREAKLINE: {
867 if (!autoBreakRows) {
872 replaceSelection(bv->getLyXText());
873 text_.insertInset(new InsetNewline);
879 // do not set layouts on non breakable textinsets
881 string cur_layout = cpar()->layout()->name();
883 // Derive layout number from given argument (string)
884 // and current buffer's textclass (number).
885 LyXTextClass const & tclass =
886 bv->buffer()->params().getLyXTextClass();
887 string layout = cmd.argument;
888 bool hasLayout = tclass.hasLayout(layout);
890 // If the entry is obsolete, use the new one instead.
892 string const & obs = tclass[layout]->obsoleted_by();
897 // see if we found the layout number:
899 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
900 bv->owner()->dispatch(lf);
904 if (cur_layout != layout) {
906 text_.setLayout(layout);
907 bv->owner()->setLayout(cpar()->layout()->name());
911 // reset the layout box
912 bv->owner()->setLayout(cpar()->layout()->name());
917 if (!bv->dispatch(cmd))
918 result = UNDISPATCHED;
922 updateLocal(bv, updflag);
924 /// If the action has deleted all text in the inset, we need to change the
925 // language to the language of the surronding text.
926 if (!was_empty && paragraphs.begin()->empty() &&
927 paragraphs.size() == 1) {
928 LyXFont font(LyXFont::ALL_IGNORE);
929 font.setLanguage(bv->getParentLanguage(this));
930 setFont(bv, font, false);
933 if (result >= FINISHED)
934 bv->unlockInset(this);
936 if (result == DISPATCHED_NOUPDATE)
942 int InsetText::latex(Buffer const & buf, ostream & os,
943 LatexRunParams const & runparams) const
946 latexParagraphs(buf, paragraphs, os, texrow, runparams);
947 return texrow.rows();
951 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
953 unsigned int lines = 0;
955 ParagraphList::const_iterator beg = paragraphs.begin();
956 ParagraphList::const_iterator end = paragraphs.end();
957 ParagraphList::const_iterator it = beg;
958 for (; it != end; ++it) {
959 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
960 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
967 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
969 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
970 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
972 // There is a confusion between the empty paragraph and the default paragraph
973 // The default paragraph is <p></p>, the empty paragraph is *empty*
974 // Since none of the floats of linuxdoc accepts standard paragraphs
975 // I disable them. I don't expect problems. (jamatos 2003/07/27)
976 for (; pit != pend; ++pit) {
977 const string name = pit->layout()->latexname();
979 sgml::openTag(os, 1, 0, name);
980 buf.simpleLinuxDocOnePar(os, pit, 0);
982 sgml::closeTag(os, 1, 0, name);
988 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
990 unsigned int lines = 0;
992 vector<string> environment_stack(10);
993 vector<string> environment_inner(10);
995 int const command_depth = 0;
998 Paragraph::depth_type depth = 0; // paragraph depth
1000 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1001 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1003 for (; pit != pend; ++pit) {
1004 int desc_on = 0; // description mode
1006 LyXLayout_ptr const & style = pit->layout();
1008 // environment tag closing
1009 for (; depth > pit->params().depth(); --depth) {
1010 if (environment_inner[depth] != "!-- --") {
1011 item_name = "listitem";
1012 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1013 if (environment_inner[depth] == "varlistentry")
1014 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1016 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1017 environment_stack[depth].erase();
1018 environment_inner[depth].erase();
1021 if (depth == pit->params().depth()
1022 && environment_stack[depth] != style->latexname()
1023 && !environment_stack[depth].empty()) {
1024 if (environment_inner[depth] != "!-- --") {
1025 item_name= "listitem";
1026 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1027 if (environment_inner[depth] == "varlistentry")
1028 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1031 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1033 environment_stack[depth].erase();
1034 environment_inner[depth].erase();
1037 // Write opening SGML tags.
1038 switch (style->latextype) {
1039 case LATEX_PARAGRAPH:
1040 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1044 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1048 case LATEX_ENVIRONMENT:
1049 case LATEX_ITEM_ENVIRONMENT:
1050 if (depth < pit->params().depth()) {
1051 depth = pit->params().depth();
1052 environment_stack[depth].erase();
1055 if (environment_stack[depth] != style->latexname()) {
1056 if (environment_stack.size() == depth + 1) {
1057 environment_stack.push_back("!-- --");
1058 environment_inner.push_back("!-- --");
1060 environment_stack[depth] = style->latexname();
1061 environment_inner[depth] = "!-- --";
1062 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1064 if (environment_inner[depth] != "!-- --") {
1065 item_name= "listitem";
1066 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1067 if (environment_inner[depth] == "varlistentry")
1068 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1072 if (style->latextype == LATEX_ENVIRONMENT) {
1073 if (!style->latexparam().empty()) {
1074 if (style->latexparam() == "CDATA")
1077 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1082 desc_on = (style->labeltype == LABEL_MANUAL);
1084 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1085 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1087 item_name = desc_on ? "term" : "para";
1088 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1092 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1096 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1099 // write closing SGML tags
1100 switch (style->latextype) {
1101 case LATEX_ENVIRONMENT:
1102 if (!style->latexparam().empty()) {
1103 if (style->latexparam() == "CDATA")
1106 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1109 case LATEX_ITEM_ENVIRONMENT:
1113 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1115 case LATEX_PARAGRAPH:
1116 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1119 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1125 for (int d = depth; d >= 0; --d) {
1126 if (!environment_stack[depth].empty()) {
1127 if (environment_inner[depth] != "!-- --") {
1128 item_name = "listitem";
1129 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1130 if (environment_inner[depth] == "varlistentry")
1131 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1134 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1142 void InsetText::validate(LaTeXFeatures & features) const
1144 for_each(paragraphs.begin(), paragraphs.end(),
1145 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1149 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1151 if (the_locking_inset) {
1152 the_locking_inset->getCursor(bv, x, y);
1156 y = cy() + InsetText::y();
1160 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1162 if (the_locking_inset) {
1163 the_locking_inset->getCursorPos(bv, x, y);
1166 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1167 y = cy() - TEXT_TO_INSET_OFFSET;
1171 int InsetText::insetInInsetY() const
1173 if (!the_locking_inset)
1176 return inset_y + the_locking_inset->insetInInsetY();
1180 void InsetText::fitInsetCursor(BufferView * bv) const
1182 if (the_locking_inset) {
1183 the_locking_inset->fitInsetCursor(bv);
1187 LyXFont const font = text_.getFont(cpar(), cpos());
1189 int const asc = font_metrics::maxAscent(font);
1190 int const desc = font_metrics::maxDescent(font);
1192 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1196 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1198 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1199 return moveLeftIntern(bv, false, true, false);
1201 return moveRightIntern(bv, true, true, false);
1205 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1207 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1208 return moveRightIntern(bv, true, true, false);
1210 return moveLeftIntern(bv, false, true, false);
1215 InsetText::moveRightIntern(BufferView * bv, bool front,
1216 bool activate_inset, bool selecting)
1218 ParagraphList::iterator c_par = cpar();
1220 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1221 return FINISHED_RIGHT;
1222 if (activate_inset && checkAndActivateInset(bv, front))
1224 text_.cursorRight(bv);
1226 text_.clearSelection();
1227 return DISPATCHED_NOUPDATE;
1232 InsetText::moveLeftIntern(BufferView * bv, bool front,
1233 bool activate_inset, bool selecting)
1235 if (cpar() == paragraphs.begin() && cpos() <= 0)
1237 text_.cursorLeft(bv);
1239 text_.clearSelection();
1240 if (activate_inset && checkAndActivateInset(bv, front))
1242 return DISPATCHED_NOUPDATE;
1246 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1248 if (crow() == text_.firstRow())
1251 text_.clearSelection();
1252 return DISPATCHED_NOUPDATE;
1256 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1258 if (crow() == text_.lastRow())
1259 return FINISHED_DOWN;
1260 text_.cursorDown(bv);
1261 text_.clearSelection();
1262 return DISPATCHED_NOUPDATE;
1266 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1268 if (the_locking_inset) {
1269 if (the_locking_inset->insetAllowed(inset))
1270 return the_locking_inset->insertInset(bv, inset);
1273 inset->setOwner(this);
1274 text_.insertInset(inset);
1276 updateLocal(bv, true);
1281 bool InsetText::insetAllowed(InsetOld::Code code) const
1283 // in_insetAllowed is a really gross hack,
1284 // to allow us to call the owner's insetAllowed
1285 // without stack overflow, which can happen
1286 // when the owner uses InsetCollapsable::insetAllowed()
1288 if (in_insetAllowed)
1290 in_insetAllowed = true;
1291 if (the_locking_inset)
1292 ret = the_locking_inset->insetAllowed(code);
1294 ret = owner()->insetAllowed(code);
1295 in_insetAllowed = false;
1300 UpdatableInset * InsetText::getLockingInset() const
1302 return the_locking_inset ? the_locking_inset->getLockingInset() :
1303 const_cast<InsetText *>(this);
1307 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1311 if (the_locking_inset)
1312 return the_locking_inset->getFirstLockingInsetOfType(c);
1317 bool InsetText::showInsetDialog(BufferView * bv) const
1319 if (the_locking_inset)
1320 return the_locking_inset->showInsetDialog(bv);
1325 void InsetText::getLabelList(std::vector<string> & list) const
1327 ParagraphList::const_iterator pit = paragraphs.begin();
1328 ParagraphList::const_iterator pend = paragraphs.end();
1329 for (; pit != pend; ++pit) {
1330 InsetList::const_iterator beg = pit->insetlist.begin();
1331 InsetList::const_iterator end = pit->insetlist.end();
1332 for (; beg != end; ++beg)
1333 beg->inset->getLabelList(list);
1338 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1341 if (the_locking_inset) {
1342 the_locking_inset->setFont(bv, font, toggleall, selectall);
1346 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1347 || cpar()->empty()) {
1348 text_.setFont(font, toggleall);
1353 if (text_.selection.set())
1354 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1358 text_.selection.cursor = text_.cursor;
1359 text_.cursorBottom();
1360 text_.setSelection();
1363 text_.toggleFree(font, toggleall);
1366 text_.clearSelection();
1369 updateLocal(bv, true);
1373 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1375 if (cpos() == cpar()->size())
1377 InsetOld * inset = cpar()->getInset(cpos());
1378 if (!isHighlyEditableInset(inset))
1380 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1381 inset->localDispatch(cmd);
1382 if (!the_locking_inset)
1384 updateLocal(bv, false);
1389 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1390 mouse_button::state button)
1393 int dummyy = y + dim_.asc;
1394 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1395 // we only do the edit() call if the inset was hit by the mouse
1396 // or if it is a highly editable inset. So we should call this
1397 // function from our own edit with button < 0.
1398 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1400 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1409 inset_x = cx() - top_x;
1411 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1412 inset->localDispatch(cmd);
1413 if (!the_locking_inset)
1415 updateLocal(bv, false);
1420 void InsetText::markNew(bool track_changes)
1422 ParagraphList::iterator pit = paragraphs.begin();
1423 ParagraphList::iterator end = paragraphs.end();
1424 for (; pit != end; ++pit) {
1425 if (track_changes) {
1426 pit->trackChanges();
1428 // no-op when not tracking
1429 pit->cleanChanges();
1435 void InsetText::setText(string const & data, LyXFont const & font)
1438 for (unsigned int i = 0; i < data.length(); ++i)
1439 paragraphs.begin()->insertChar(i, data[i], font);
1443 void InsetText::setAutoBreakRows(bool flag)
1445 if (flag != autoBreakRows) {
1446 autoBreakRows = flag;
1453 void InsetText::setDrawFrame(DrawFrame how)
1459 void InsetText::setFrameColor(LColor::color col)
1465 int InsetText::cx() const
1467 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1468 if (the_locking_inset) {
1469 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1470 if (font.isVisibleRightToLeft())
1471 x -= the_locking_inset->width();
1477 int InsetText::cy() const
1479 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1483 pos_type InsetText::cpos() const
1485 return text_.cursor.pos();
1489 ParagraphList::iterator InsetText::cpar() const
1491 return text_.cursor.par();
1495 bool InsetText::cboundary() const
1497 return text_.cursor.boundary();
1501 RowList::iterator InsetText::crow() const
1503 return text_.cursorRow();
1507 LyXText * InsetText::getLyXText(BufferView const * bv,
1508 bool const recursive) const
1511 if (recursive && the_locking_inset)
1512 return the_locking_inset->getLyXText(bv, true);
1517 void InsetText::setViewCache(BufferView const * bv) const
1520 if (bv != text_.bv_owner) {
1521 //lyxerr << "setting view cache from "
1522 // << text_.bv_owner << " to " << bv << "\n";
1523 text_.init(const_cast<BufferView *>(bv));
1525 text_.bv_owner = const_cast<BufferView *>(bv);
1530 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1533 /// then remove all LyXText in text-insets
1534 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1535 const_cast<ParagraphList&>(paragraphs).end(),
1536 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1541 void InsetText::removeNewlines()
1543 ParagraphList::iterator it = paragraphs.begin();
1544 ParagraphList::iterator end = paragraphs.end();
1545 for (; it != end; ++it)
1546 for (int i = 0; i < it->size(); ++i)
1547 if (it->isNewline(i))
1552 int InsetText::scroll(bool recursive) const
1554 int sx = UpdatableInset::scroll(false);
1556 if (recursive && the_locking_inset)
1557 sx += the_locking_inset->scroll(recursive);
1563 void InsetText::clearSelection(BufferView *)
1565 text_.clearSelection();
1569 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1571 Painter & pain = bv->painter();
1573 int h = dim_.asc + dim_.des;
1574 int ty = baseline - dim_.asc;
1580 if (ty + h > pain.paperHeight())
1581 h = pain.paperHeight();
1582 if (top_x + w > pain.paperWidth())
1583 w = pain.paperWidth();
1584 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1588 ParagraphList * InsetText::getParagraphs(int i) const
1590 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1594 LyXCursor const & InsetText::cursor(BufferView * bv) const
1596 if (the_locking_inset)
1597 return the_locking_inset->cursor(bv);
1598 return getLyXText(bv)->cursor;
1602 InsetOld * InsetText::getInsetFromID(int id_arg) const
1605 return const_cast<InsetText *>(this);
1607 ParagraphList::const_iterator pit = paragraphs.begin();
1608 ParagraphList::const_iterator pend = paragraphs.end();
1609 for (; pit != pend; ++pit) {
1610 InsetList::const_iterator it = pit->insetlist.begin();
1611 InsetList::const_iterator end = pit->insetlist.end();
1612 for (; it != end; ++it) {
1613 if (it->inset->id() == id_arg)
1615 InsetOld * in = it->inset->getInsetFromID(id_arg);
1625 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1628 if (the_locking_inset) {
1629 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1630 if (!word.word().empty()) {
1634 // we have to go on checking so move cursor to the next char
1635 text_.cursor.pos(text_.cursor.pos() + 1);
1637 word = text_.selectNextWordToSpellcheck(value);
1638 if (word.word().empty())
1639 bv->unlockInset(const_cast<InsetText *>(this));
1646 void InsetText::selectSelectedWord(BufferView * bv)
1648 if (the_locking_inset) {
1649 the_locking_inset->selectSelectedWord(bv);
1652 getLyXText(bv)->selectSelectedWord();
1653 updateLocal(bv, false);
1657 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1659 if (the_locking_inset) {
1660 if (the_locking_inset->nextChange(bv, length))
1662 text_.cursorRight(true);
1664 lyx::find::SearchResult result =
1665 lyx::find::findNextChange(bv, &text_, length);
1667 if (result == lyx::find::SR_FOUND) {
1668 LyXCursor cur = text_.cursor;
1669 bv->unlockInset(bv->theLockingInset());
1670 if (bv->lockInset(this))
1673 text_.setSelectionRange(length);
1674 updateLocal(bv, false);
1676 return result != lyx::find::SR_NOT_FOUND;
1680 bool InsetText::searchForward(BufferView * bv, string const & str,
1683 if (the_locking_inset) {
1684 if (the_locking_inset->searchForward(bv, str, cs, mw))
1686 text_.cursorRight(true);
1688 lyx::find::SearchResult result =
1689 lyx::find::find(bv, &text_, str, true, cs, mw);
1691 if (result == lyx::find::SR_FOUND) {
1692 LyXCursor cur = text_.cursor;
1693 bv->unlockInset(bv->theLockingInset());
1694 if (bv->lockInset(this))
1697 text_.setSelectionRange(str.length());
1698 updateLocal(bv, false);
1700 return result != lyx::find::SR_NOT_FOUND;
1704 bool InsetText::searchBackward(BufferView * bv, string const & str,
1707 if (the_locking_inset) {
1708 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1712 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1713 text_.setCursor(pit, pit->size());
1715 lyx::find::SearchResult result =
1716 lyx::find::find(bv, &text_, str, false, cs, mw);
1718 if (result == lyx::find::SR_FOUND) {
1719 LyXCursor cur = text_.cursor;
1720 bv->unlockInset(bv->theLockingInset());
1721 if (bv->lockInset(this))
1724 text_.setSelectionRange(str.length());
1725 updateLocal(bv, false);
1727 return result != lyx::find::SR_NOT_FOUND;
1731 bool InsetText::checkInsertChar(LyXFont & font)
1733 return owner() ? owner()->checkInsertChar(font) : true;
1737 void InsetText::collapseParagraphs(BufferView * bv)
1739 while (paragraphs.size() > 1) {
1740 ParagraphList::iterator first_par = paragraphs.begin();
1741 ParagraphList::iterator next_par = boost::next(first_par);
1742 size_t const first_par_size = first_par->size();
1744 if (!first_par->empty() &&
1745 !next_par->empty() &&
1746 !first_par->isSeparator(first_par_size - 1)) {
1747 first_par->insertChar(first_par_size, ' ');
1750 if (text_.selection.set()) {
1751 if (text_.selection.start.par() == next_par) {
1752 text_.selection.start.par(first_par);
1753 text_.selection.start.pos(
1754 text_.selection.start.pos() + first_par_size);
1756 if (text_.selection.end.par() == next_par) {
1757 text_.selection.end.par(first_par);
1758 text_.selection.end.pos(
1759 text_.selection.end.pos() + first_par_size);
1763 mergeParagraph(bv->buffer()->params(), paragraphs, first_par);
1768 void InsetText::getDrawFont(LyXFont & font) const
1772 owner()->getDrawFont(font);
1776 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1778 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1779 // And it probably does. You have to take a look at this John. (Lgb)
1780 #warning John, have a look here. (Lgb)
1781 ParagraphList::iterator pit = plist.begin();
1782 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1784 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1786 ParagraphList::iterator pend = plist.end();
1787 for (; pit != pend; ++pit)
1788 paragraphs.push_back(*pit);
1792 void InsetText::addPreview(PreviewLoader & loader) const
1794 ParagraphList::const_iterator pit = paragraphs.begin();
1795 ParagraphList::const_iterator pend = paragraphs.end();
1797 for (; pit != pend; ++pit) {
1798 InsetList::const_iterator it = pit->insetlist.begin();
1799 InsetList::const_iterator end = pit->insetlist.end();
1800 for (; it != end; ++it)
1801 it->inset->addPreview(loader);