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"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
23 #include "errorlist.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxcursor.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
45 #include "textcursor.h"
47 #include "frontends/Alert.h"
48 #include "frontends/Dialogs.h"
49 #include "frontends/font_metrics.h"
50 #include "frontends/LyXView.h"
51 #include "frontends/Painter.h"
52 #include "frontends/screen.h"
54 #include "support/textutils.h"
55 #include "support/LAssert.h"
56 #include "support/lstrings.h"
57 #include "support/lyxalgo.h" // lyx::count
59 #include <boost/bind.hpp>
76 using namespace lyx::support;
77 using namespace lyx::graphics;
78 using namespace bv_funcs;
81 using lyx::textclass_type;
84 InsetText::InsetText(BufferParams const & bp)
85 : UpdatableInset(), text_(0, this, true, paragraphs)
87 paragraphs.push_back(Paragraph());
88 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
89 if (bp.tracking_changes)
90 paragraphs.begin()->trackChanges();
95 InsetText::InsetText(InsetText const & in)
96 : UpdatableInset(in), text_(0, this, true, paragraphs)
102 InsetText & InsetText::operator=(InsetText const & it)
109 void InsetText::init(InsetText const * ins)
112 textwidth_ = ins->textwidth_;
113 text_.bv_owner = ins->text_.bv_owner;
115 paragraphs = ins->paragraphs;
117 ParagraphList::iterator pit = paragraphs.begin();
118 ParagraphList::iterator end = paragraphs.end();
119 for (; pit != end; ++pit)
120 pit->setInsetOwner(this);
122 autoBreakRows = ins->autoBreakRows;
123 drawFrame_ = ins->drawFrame_;
124 frame_color = ins->frame_color;
126 textwidth_ = 0; // broken
128 frame_color = LColor::insetframe;
129 autoBreakRows = false;
131 the_locking_inset = 0;
132 for_each(paragraphs.begin(), paragraphs.end(),
133 boost::bind(&Paragraph::setInsetOwner, _1, this));
139 old_par = paragraphs.end();
140 in_insetAllowed = false;
144 void InsetText::clear(bool just_mark_erased)
146 if (just_mark_erased) {
147 ParagraphList::iterator it = paragraphs.begin();
148 ParagraphList::iterator end = paragraphs.end();
149 for (; it != end; ++it)
154 // This is a gross hack...
155 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
158 paragraphs.push_back(Paragraph());
159 paragraphs.begin()->setInsetOwner(this);
160 paragraphs.begin()->layout(old_layout);
164 auto_ptr<InsetBase> InsetText::clone() const
166 return auto_ptr<InsetBase>(new InsetText(*this));
170 void InsetText::write(Buffer const * buf, ostream & os) const
173 writeParagraphData(buf, os);
177 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
179 ParagraphList::const_iterator it = paragraphs.begin();
180 ParagraphList::const_iterator end = paragraphs.end();
181 Paragraph::depth_type dth = 0;
182 for (; it != end; ++it) {
183 it->write(buf, os, buf->params, dth);
188 void InsetText::read(Buffer const * buf, LyXLex & lex)
191 Paragraph::depth_type depth = 0;
195 if (buf->params.tracking_changes)
196 paragraphs.begin()->trackChanges();
198 // delete the initial paragraph
200 ParagraphList::iterator pit = paragraphs.begin();
204 token = lex.getString();
207 if (token == "\\end_inset") {
211 if (token == "\\end_document") {
212 lex.printError("\\end_document read in inset! Error in document!");
217 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
220 pit = paragraphs.begin();
221 ParagraphList::iterator const end = paragraphs.end();
222 for (; pit != end; ++pit)
223 pit->setInsetOwner(this);
225 if (token != "\\end_inset") {
226 lex.printError("Missing \\end_inset at this point. "
232 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
234 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
236 textwidth_ = mi.base.textwidth;
237 BufferView * bv = mi.base.bv;
239 text_.metrics(mi, dim);
240 dim.asc += TEXT_TO_INSET_OFFSET;
241 dim.des += TEXT_TO_INSET_OFFSET;
242 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
243 dim.wid = max(dim.wid, 10);
248 int InsetText::textWidth() const
254 void InsetText::draw(PainterInfo & pi, int x, int y) const
256 // update our idea of where we are. Clearly, we should
257 // not have to know this information.
260 int const start_x = x;
262 BufferView * bv = pi.base.bv;
263 Painter & pain = pi.pain;
265 // repaint the background if needed
266 if (backgroundColor() != LColor::background)
267 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
269 // no draw is necessary !!!
270 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
281 top_y = y - dim_.asc;
283 if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
284 inset_x = cix() - x + drawTextXOffset;
285 inset_y = ciy() + drawTextYOffset;
288 x += TEXT_TO_INSET_OFFSET;
290 paintTextInset(*bv, text_, x, y);
292 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
293 drawFrame(pain, start_x);
297 void InsetText::drawFrame(Painter & pain, int x) const
299 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
300 int const frame_x = x + ttoD2;
301 int const frame_y = top_baseline - dim_.asc + ttoD2;
302 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
303 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
304 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
308 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
313 if (!autoBreakRows && paragraphs.size() > 1)
314 collapseParagraphs(bv);
316 if (!text_.selection.set())
317 text_.selection.cursor = text_.cursor;
321 bv->owner()->view_state_changed();
322 bv->owner()->updateMenubar();
323 bv->owner()->updateToolbar();
324 if (old_par != cpar()) {
325 bv->owner()->setLayout(cpar()->layout()->name());
331 string const InsetText::editMessage() const
333 return _("Opened Text Inset");
337 void InsetText::insetUnlock(BufferView * bv)
339 if (the_locking_inset) {
340 the_locking_inset->insetUnlock(bv);
341 the_locking_inset = 0;
342 updateLocal(bv, false);
347 if (text_.selection.set()) {
348 text_.clearSelection();
349 } else if (owner()) {
350 bv->owner()->setLayout(owner()->getLyXText(bv)
351 ->cursor.par()->layout()->name());
353 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
354 // hack for deleteEmptyParMech
355 ParagraphList::iterator first_par = paragraphs.begin();
356 if (!first_par->empty()) {
357 text_.setCursor(first_par, 0);
358 } else if (paragraphs.size() > 1) {
359 text_.setCursor(boost::next(first_par), 0);
364 void InsetText::lockInset(BufferView * bv)
367 the_locking_inset = 0;
368 inset_pos = inset_x = inset_y = 0;
369 inset_boundary = false;
370 inset_par = paragraphs.end();
371 old_par = paragraphs.end();
372 text_.setCursorIntern(paragraphs.begin(), 0);
373 text_.clearSelection();
375 // If the inset is empty set the language of the current font to the
376 // language to the surronding text (if different).
377 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
378 bv->getParentLanguage(this) != text_.current_font.language()) {
379 LyXFont font(LyXFont::ALL_IGNORE);
380 font.setLanguage(bv->getParentLanguage(this));
381 setFont(bv, font, false);
386 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
388 the_locking_inset = inset;
389 inset_x = cix() - top_x + drawTextXOffset;
390 inset_y = ciy() + drawTextYOffset;
393 inset_boundary = cboundary();
397 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
399 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
400 << inset << "): " << endl;
403 if (!the_locking_inset) {
404 ParagraphList::iterator pit = paragraphs.begin();
405 ParagraphList::iterator pend = paragraphs.end();
407 int const id = inset->id();
408 for (; pit != pend; ++pit) {
409 InsetList::iterator it = pit->insetlist.begin();
410 InsetList::iterator const end = pit->insetlist.end();
411 for (; it != end; ++it) {
412 if (it->inset == inset) {
413 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
414 text_.setCursorIntern(pit, it->pos);
415 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
416 lyxerr << "bv: " << bv << " inset: " << inset << endl;
417 lockInset(bv, inset);
418 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
421 if (it->inset->getInsetFromID(id)) {
422 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
423 text_.setCursorIntern(pit, it->pos);
424 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
425 return the_locking_inset->lockInsetInInset(bv, inset);
429 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
432 if (inset == cpar()->getInset(cpos())) {
433 lyxerr[Debug::INSETS] << "OK" << endl;
434 lockInset(bv, inset);
438 if (the_locking_inset && the_locking_inset == inset) {
439 if (cpar() == inset_par && cpos() == inset_pos) {
440 lyxerr[Debug::INSETS] << "OK" << endl;
441 inset_x = cix() - top_x + drawTextXOffset;
442 inset_y = ciy() + drawTextYOffset;
444 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
446 } else if (the_locking_inset) {
447 lyxerr[Debug::INSETS] << "MAYBE" << endl;
448 return the_locking_inset->lockInsetInInset(bv, inset);
450 lyxerr[Debug::INSETS] << "NOT OK" << endl;
455 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
458 if (!the_locking_inset)
460 if (the_locking_inset == inset) {
461 the_locking_inset->insetUnlock(bv);
462 the_locking_inset = 0;
464 moveRightIntern(bv, true, false);
465 old_par = paragraphs.end(); // force layout setting
469 updateLocal(bv, false);
472 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
476 void InsetText::lfunMousePress(FuncRequest const & cmd)
480 // use this to check mouse motion for selection!
484 BufferView * bv = cmd.view();
485 FuncRequest cmd1 = cmd;
491 int tmp_x = cmd.x - drawTextXOffset;
492 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
493 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
495 if (the_locking_inset) {
496 if (the_locking_inset == inset) {
497 the_locking_inset->localDispatch(cmd1);
500 // otherwise only unlock the_locking_inset
501 the_locking_inset->insetUnlock(bv);
502 the_locking_inset = 0;
505 no_selection = false;
507 if (bv->theLockingInset()) {
508 if (isHighlyEditableInset(inset)) {
509 // We just have to lock the inset before calling a
511 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
512 if (!bv->lockInset(uinset)) {
513 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
515 inset->localDispatch(cmd1);
516 if (the_locking_inset)
517 updateLocal(bv, false);
522 bool paste_internally = false;
523 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
524 localDispatch(FuncRequest(bv, LFUN_COPY));
525 paste_internally = true;
527 int old_top_y = text_.top_y();
529 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
531 // set the selection cursor!
532 text_.selection.cursor = text_.cursor;
533 text_.cursor.x_fix(text_.cursor.x());
535 text_.clearSelection();
536 updateLocal(bv, false);
538 bv->owner()->setLayout(cpar()->layout()->name());
540 // we moved the view we cannot do mouse selection in this case!
541 if (getLyXText(bv)->top_y() != old_top_y)
544 // Insert primary selection with middle mouse
545 // if there is a local selection in the current buffer,
547 if (cmd.button() == mouse_button::button2) {
548 if (paste_internally)
549 localDispatch(FuncRequest(bv, LFUN_PASTE));
551 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
554 getLyXText(bv)->clearSelection();
559 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
561 BufferView * bv = cmd.view();
562 FuncRequest cmd1 = cmd;
567 if (the_locking_inset)
568 return the_locking_inset->localDispatch(cmd1);
570 int tmp_x = cmd.x - drawTextXOffset;
571 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
572 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
575 // This code should probably be removed now. Simple insets
576 // (!highlyEditable) can actually take the localDispatch,
577 // and turn it into edit() if necessary. But we still
578 // need to deal properly with the whole relative vs.
579 // absolute mouse co-ords thing in a realiable, sensible way
581 if (isHighlyEditableInset(inset))
582 ret = inset->localDispatch(cmd1);
584 inset_x = cix(bv) - top_x + drawTextXOffset;
585 inset_y = ciy() + drawTextYOffset;
586 cmd1.x = cmd.x - inset_x;
587 cmd1.y = cmd.x - inset_y;
588 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
592 ret = inset->localDispatch(cmd1);
593 updateLocal(bv, false);
600 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
602 FuncRequest cmd1 = cmd;
606 if (the_locking_inset) {
607 the_locking_inset->localDispatch(cmd1);
611 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
614 BufferView * bv = cmd.view();
615 LyXCursor cur = text_.cursor;
616 text_.setCursorFromCoordinates
617 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
618 text_.cursor.x_fix(text_.cursor.x());
619 if (cur == text_.cursor)
621 text_.setSelection();
622 updateLocal(bv, false);
626 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
628 BufferView * bv = cmd.view();
631 switch (cmd.action) {
632 case LFUN_INSET_EDIT: {
633 UpdatableInset::localDispatch(cmd);
635 if (!bv->lockInset(this)) {
636 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
641 the_locking_inset = 0;
645 inset_boundary = false;
646 inset_par = paragraphs.end();
647 old_par = paragraphs.end();
650 if (cmd.argument.size()) {
651 if (cmd.argument == "left")
652 text_.setCursorIntern(paragraphs.begin(), 0);
654 ParagraphList::iterator it = boost::prior(paragraphs.end());
655 text_.setCursor(it, it->size());
658 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
659 // we put here -1 and not button as now the button in the
660 // edit call should not be needed we will fix this in 1.3.x
661 // cycle hopefully (Jug 20020509)
662 // FIXME: GUII I've changed this to none: probably WRONG
663 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
664 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
666 text_.cursor.x_fix(text_.cursor.x());
670 text_.clearSelection();
673 // If the inset is empty set the language of the current font to the
674 // language to the surronding text (if different).
675 if (paragraphs.begin()->empty() &&
676 paragraphs.size() == 1 &&
677 bv->getParentLanguage(this) != text_.current_font.language())
679 LyXFont font(LyXFont::ALL_IGNORE);
680 font.setLanguage(bv->getParentLanguage(this));
681 setFont(bv, font, false);
684 updateLocal(bv, false);
685 // Tell the paragraph dialog that we've entered an insettext.
686 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
690 case LFUN_MOUSE_PRESS:
694 case LFUN_MOUSE_MOTION:
695 lfunMouseMotion(cmd);
698 case LFUN_MOUSE_RELEASE:
699 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
705 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
706 no_selection = false;
708 RESULT result = UpdatableInset::localDispatch(cmd);
709 if (result != UNDISPATCHED)
713 if (cmd.action < 0 && cmd.argument.empty())
716 if (the_locking_inset) {
717 result = the_locking_inset->localDispatch(cmd);
718 if (result == DISPATCHED_NOUPDATE)
720 if (result == DISPATCHED) {
721 updateLocal(bv, false);
724 if (result >= FINISHED) {
727 moveRightIntern(bv, false, false);
732 if (result >= FINISHED) {
733 updateLocal(bv, false);
734 bv->unlockInset(this);
738 result = moveDown(bv);
739 if (result >= FINISHED) {
740 updateLocal(bv, false);
741 bv->unlockInset(this);
748 the_locking_inset = 0;
749 updateLocal(bv, false);
750 // make sure status gets reset immediately
751 bv->owner()->clearMessage();
755 bool updflag = false;
757 switch (cmd.action) {
760 case LFUN_SELFINSERT:
761 if (bv->buffer()->isReadonly()) {
762 // setErrorMessage(N_("Document is read only"));
765 if (!cmd.argument.empty()) {
766 /* Automatically delete the currently selected
767 * text and replace it with what is being
768 * typed in now. Depends on lyxrc settings
769 * "auto_region_delete", which defaults to
772 // This should not be needed here and is also WRONG!
773 recordUndo(bv, Undo::INSERT, text_.cursor.par());
777 if (lyxrc.auto_region_delete && text_.selection.set())
778 text_.cutSelection(false, false);
779 text_.clearSelection();
781 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
782 bv->owner()->getIntl().getTransManager().
783 TranslateAndInsert(cmd.argument[i], &text_);
785 text_.selection.cursor = text_.cursor;
787 result = DISPATCHED_NOUPDATE;
790 // cursor movements that need special handling
793 result = moveRight(bv);
798 result = moveLeft(bv);
802 result = moveDown(bv);
810 if (crow() == text_.firstRow())
811 result = FINISHED_UP;
813 text_.cursorPrevious();
814 text_.clearSelection();
815 result = DISPATCHED_NOUPDATE;
820 if (crow() == text_.lastRow())
821 result = FINISHED_DOWN;
824 text_.clearSelection();
825 result = DISPATCHED_NOUPDATE;
830 if (text_.selection.set())
831 text_.cutSelection(true, false);
838 if (text_.selection.set())
839 text_.cutSelection(true, false);
846 if (!autoBreakRows) {
847 if (CutAndPaste::nrOfParagraphs() > 1) {
849 #warning FIXME horrendously bad UI
851 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
856 replaceSelection(bv->getLyXText());
857 size_t sel_index = 0;
858 string const & arg = cmd.argument;
859 if (isStrUnsignedInt(arg)) {
860 size_t const paste_arg = strToUnsignedInt(arg);
861 #warning FIXME Check if the arg is in the domain of available selections.
862 sel_index = paste_arg;
864 text_.pasteSelection(sel_index);
866 text_.clearSelection();
871 case LFUN_BREAKPARAGRAPH:
872 if (!autoBreakRows) {
876 replaceSelection(bv->getLyXText());
877 text_.breakParagraph(paragraphs, 0);
881 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
882 if (!autoBreakRows) {
886 replaceSelection(bv->getLyXText());
887 text_.breakParagraph(paragraphs, 1);
891 case LFUN_BREAKLINE: {
892 if (!autoBreakRows) {
897 replaceSelection(bv->getLyXText());
898 text_.insertInset(new InsetNewline);
904 // do not set layouts on non breakable textinsets
906 string cur_layout = cpar()->layout()->name();
908 // Derive layout number from given argument (string)
909 // and current buffer's textclass (number).
910 LyXTextClass const & tclass =
911 bv->buffer()->params.getLyXTextClass();
912 string layout = cmd.argument;
913 bool hasLayout = tclass.hasLayout(layout);
915 // If the entry is obsolete, use the new one instead.
917 string const & obs = tclass[layout]->obsoleted_by();
922 // see if we found the layout number:
924 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
925 bv->owner()->dispatch(lf);
929 if (cur_layout != layout) {
931 text_.setLayout(layout);
932 bv->owner()->setLayout(cpar()->layout()->name());
936 // reset the layout box
937 bv->owner()->setLayout(cpar()->layout()->name());
942 if (!bv->dispatch(cmd))
943 result = UNDISPATCHED;
947 updateLocal(bv, updflag);
949 /// If the action has deleted all text in the inset, we need to change the
950 // language to the language of the surronding text.
951 if (!was_empty && paragraphs.begin()->empty() &&
952 paragraphs.size() == 1) {
953 LyXFont font(LyXFont::ALL_IGNORE);
954 font.setLanguage(bv->getParentLanguage(this));
955 setFont(bv, font, false);
958 if (result >= FINISHED)
959 bv->unlockInset(this);
961 if (result == DISPATCHED_NOUPDATE)
967 int InsetText::latex(Buffer const * buf, ostream & os,
968 LatexRunParams const & runparams) const
971 latexParagraphs(buf, paragraphs, os, texrow, runparams);
972 return texrow.rows();
976 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
978 unsigned int lines = 0;
980 ParagraphList::const_iterator beg = paragraphs.begin();
981 ParagraphList::const_iterator end = paragraphs.end();
982 ParagraphList::const_iterator it = beg;
983 for (; it != end; ++it) {
984 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
985 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
992 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
994 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
995 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
997 // There is a confusion between the empty paragraph and the default paragraph
998 // The default paragraph is <p></p>, the empty paragraph is *empty*
999 // Since none of the floats of linuxdoc accepts standard paragraphs
1000 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1001 for (; pit != pend; ++pit) {
1002 const string name = pit->layout()->latexname();
1004 sgml::openTag(os, 1, 0, name);
1005 buf->simpleLinuxDocOnePar(os, pit, 0);
1007 sgml::closeTag(os, 1, 0, name);
1013 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1015 unsigned int lines = 0;
1017 vector<string> environment_stack(10);
1018 vector<string> environment_inner(10);
1020 int const command_depth = 0;
1023 Paragraph::depth_type depth = 0; // paragraph depth
1025 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1026 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1028 for (; pit != pend; ++pit) {
1029 int desc_on = 0; // description mode
1031 LyXLayout_ptr const & style = pit->layout();
1033 // environment tag closing
1034 for (; depth > pit->params().depth(); --depth) {
1035 if (environment_inner[depth] != "!-- --") {
1036 item_name = "listitem";
1037 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1038 if (environment_inner[depth] == "varlistentry")
1039 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1041 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1042 environment_stack[depth].erase();
1043 environment_inner[depth].erase();
1046 if (depth == pit->params().depth()
1047 && environment_stack[depth] != style->latexname()
1048 && !environment_stack[depth].empty()) {
1049 if (environment_inner[depth] != "!-- --") {
1050 item_name= "listitem";
1051 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1052 if (environment_inner[depth] == "varlistentry")
1053 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1056 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1058 environment_stack[depth].erase();
1059 environment_inner[depth].erase();
1062 // Write opening SGML tags.
1063 switch (style->latextype) {
1064 case LATEX_PARAGRAPH:
1065 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1069 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1073 case LATEX_ENVIRONMENT:
1074 case LATEX_ITEM_ENVIRONMENT:
1075 if (depth < pit->params().depth()) {
1076 depth = pit->params().depth();
1077 environment_stack[depth].erase();
1080 if (environment_stack[depth] != style->latexname()) {
1081 if (environment_stack.size() == depth + 1) {
1082 environment_stack.push_back("!-- --");
1083 environment_inner.push_back("!-- --");
1085 environment_stack[depth] = style->latexname();
1086 environment_inner[depth] = "!-- --";
1087 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1089 if (environment_inner[depth] != "!-- --") {
1090 item_name= "listitem";
1091 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1092 if (environment_inner[depth] == "varlistentry")
1093 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1097 if (style->latextype == LATEX_ENVIRONMENT) {
1098 if (!style->latexparam().empty()) {
1099 if (style->latexparam() == "CDATA")
1102 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1107 desc_on = (style->labeltype == LABEL_MANUAL);
1109 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1110 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1112 item_name = desc_on ? "term" : "para";
1113 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1117 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1121 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1124 // write closing SGML tags
1125 switch (style->latextype) {
1126 case LATEX_ENVIRONMENT:
1127 if (!style->latexparam().empty()) {
1128 if (style->latexparam() == "CDATA")
1131 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1134 case LATEX_ITEM_ENVIRONMENT:
1138 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1140 case LATEX_PARAGRAPH:
1141 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1144 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1150 for (int d = depth; d >= 0; --d) {
1151 if (!environment_stack[depth].empty()) {
1152 if (environment_inner[depth] != "!-- --") {
1153 item_name = "listitem";
1154 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1155 if (environment_inner[depth] == "varlistentry")
1156 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1159 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1167 void InsetText::validate(LaTeXFeatures & features) const
1169 for_each(paragraphs.begin(), paragraphs.end(),
1170 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1174 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1176 if (the_locking_inset) {
1177 the_locking_inset->getCursor(bv, x, y);
1181 y = cy() + InsetText::y();
1185 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1187 if (the_locking_inset) {
1188 the_locking_inset->getCursorPos(bv, x, y);
1191 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1192 y = cy() - TEXT_TO_INSET_OFFSET;
1196 int InsetText::insetInInsetY() const
1198 if (!the_locking_inset)
1201 return inset_y + the_locking_inset->insetInInsetY();
1205 void InsetText::fitInsetCursor(BufferView * bv) const
1207 if (the_locking_inset) {
1208 the_locking_inset->fitInsetCursor(bv);
1212 LyXFont const font = text_.getFont(cpar(), cpos());
1214 int const asc = font_metrics::maxAscent(font);
1215 int const desc = font_metrics::maxDescent(font);
1217 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1221 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1223 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1224 return moveLeftIntern(bv, false, true, false);
1226 return moveRightIntern(bv, true, true, false);
1230 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1232 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1233 return moveRightIntern(bv, true, true, false);
1235 return moveLeftIntern(bv, false, true, false);
1240 InsetText::moveRightIntern(BufferView * bv, bool front,
1241 bool activate_inset, bool selecting)
1243 ParagraphList::iterator c_par = cpar();
1245 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1246 return FINISHED_RIGHT;
1247 if (activate_inset && checkAndActivateInset(bv, front))
1249 text_.cursorRight(bv);
1251 text_.clearSelection();
1252 return DISPATCHED_NOUPDATE;
1257 InsetText::moveLeftIntern(BufferView * bv, bool front,
1258 bool activate_inset, bool selecting)
1260 if (cpar() == paragraphs.begin() && cpos() <= 0)
1262 text_.cursorLeft(bv);
1264 text_.clearSelection();
1265 if (activate_inset && checkAndActivateInset(bv, front))
1267 return DISPATCHED_NOUPDATE;
1271 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1273 if (crow() == text_.firstRow())
1276 text_.clearSelection();
1277 return DISPATCHED_NOUPDATE;
1281 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1283 if (crow() == text_.lastRow())
1284 return FINISHED_DOWN;
1285 text_.cursorDown(bv);
1286 text_.clearSelection();
1287 return DISPATCHED_NOUPDATE;
1291 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1293 if (the_locking_inset) {
1294 if (the_locking_inset->insetAllowed(inset))
1295 return the_locking_inset->insertInset(bv, inset);
1298 inset->setOwner(this);
1299 text_.insertInset(inset);
1301 updateLocal(bv, true);
1306 bool InsetText::insetAllowed(InsetOld::Code code) const
1308 // in_insetAllowed is a really gross hack,
1309 // to allow us to call the owner's insetAllowed
1310 // without stack overflow, which can happen
1311 // when the owner uses InsetCollapsable::insetAllowed()
1313 if (in_insetAllowed)
1315 in_insetAllowed = true;
1316 if (the_locking_inset)
1317 ret = the_locking_inset->insetAllowed(code);
1319 ret = owner()->insetAllowed(code);
1320 in_insetAllowed = false;
1325 UpdatableInset * InsetText::getLockingInset() const
1327 return the_locking_inset ? the_locking_inset->getLockingInset() :
1328 const_cast<InsetText *>(this);
1332 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1336 if (the_locking_inset)
1337 return the_locking_inset->getFirstLockingInsetOfType(c);
1342 bool InsetText::showInsetDialog(BufferView * bv) const
1344 if (the_locking_inset)
1345 return the_locking_inset->showInsetDialog(bv);
1350 void InsetText::getLabelList(std::vector<string> & list) const
1352 ParagraphList::const_iterator pit = paragraphs.begin();
1353 ParagraphList::const_iterator pend = paragraphs.end();
1354 for (; pit != pend; ++pit) {
1355 InsetList::const_iterator beg = pit->insetlist.begin();
1356 InsetList::const_iterator end = pit->insetlist.end();
1357 for (; beg != end; ++beg)
1358 beg->inset->getLabelList(list);
1363 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1366 if (the_locking_inset) {
1367 the_locking_inset->setFont(bv, font, toggleall, selectall);
1371 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1372 || cpar()->empty()) {
1373 text_.setFont(font, toggleall);
1378 if (text_.selection.set())
1379 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1383 text_.selection.cursor = text_.cursor;
1384 text_.cursorBottom();
1385 text_.setSelection();
1388 text_.toggleFree(font, toggleall);
1391 text_.clearSelection();
1394 updateLocal(bv, true);
1398 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1400 if (cpos() == cpar()->size())
1402 InsetOld * inset = cpar()->getInset(cpos());
1403 if (!isHighlyEditableInset(inset))
1405 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1406 inset->localDispatch(cmd);
1407 if (!the_locking_inset)
1409 updateLocal(bv, false);
1414 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1415 mouse_button::state button)
1417 x -= drawTextXOffset;
1419 int dummyy = y + dim_.asc;
1420 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1421 // we only do the edit() call if the inset was hit by the mouse
1422 // or if it is a highly editable inset. So we should call this
1423 // function from our own edit with button < 0.
1424 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1426 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1435 inset_x = cix() - top_x + drawTextXOffset;
1436 inset_y = ciy() + drawTextYOffset;
1437 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1438 inset->localDispatch(cmd);
1439 if (!the_locking_inset)
1441 updateLocal(bv, false);
1446 void InsetText::markNew(bool track_changes)
1448 ParagraphList::iterator pit = paragraphs.begin();
1449 ParagraphList::iterator end = paragraphs.end();
1450 for (; pit != end; ++pit) {
1451 if (track_changes) {
1452 pit->trackChanges();
1454 // no-op when not tracking
1455 pit->cleanChanges();
1461 void InsetText::setText(string const & data, LyXFont const & font)
1464 for (unsigned int i = 0; i < data.length(); ++i)
1465 paragraphs.begin()->insertChar(i, data[i], font);
1469 void InsetText::setAutoBreakRows(bool flag)
1471 if (flag != autoBreakRows) {
1472 autoBreakRows = flag;
1479 void InsetText::setDrawFrame(DrawFrame how)
1485 void InsetText::setFrameColor(LColor::color col)
1491 int InsetText::cx() const
1493 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1494 if (the_locking_inset) {
1495 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1496 if (font.isVisibleRightToLeft())
1497 x -= the_locking_inset->width();
1503 int InsetText::cix() const
1505 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1506 if (the_locking_inset) {
1507 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1508 if (font.isVisibleRightToLeft())
1509 x -= the_locking_inset->width();
1515 int InsetText::cy() const
1517 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1521 int InsetText::ciy() const
1523 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1527 pos_type InsetText::cpos() const
1529 return text_.cursor.pos();
1533 ParagraphList::iterator InsetText::cpar() const
1535 return text_.cursor.par();
1539 bool InsetText::cboundary() const
1541 return text_.cursor.boundary();
1545 RowList::iterator InsetText::crow() const
1547 return text_.cursorRow();
1551 LyXText * InsetText::getLyXText(BufferView const * bv,
1552 bool const recursive) const
1555 if (recursive && the_locking_inset)
1556 return the_locking_inset->getLyXText(bv, true);
1561 void InsetText::setViewCache(BufferView const * bv) const
1564 if (bv != text_.bv_owner) {
1565 //lyxerr << "setting view cache from "
1566 // << text_.bv_owner << " to " << bv << "\n";
1567 text_.init(const_cast<BufferView *>(bv));
1569 text_.bv_owner = const_cast<BufferView *>(bv);
1574 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1577 /// then remove all LyXText in text-insets
1578 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1579 const_cast<ParagraphList&>(paragraphs).end(),
1580 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1585 void InsetText::removeNewlines()
1587 ParagraphList::iterator it = paragraphs.begin();
1588 ParagraphList::iterator end = paragraphs.end();
1589 for (; it != end; ++it)
1590 for (int i = 0; i < it->size(); ++i)
1591 if (it->isNewline(i))
1596 int InsetText::scroll(bool recursive) const
1598 int sx = UpdatableInset::scroll(false);
1600 if (recursive && the_locking_inset)
1601 sx += the_locking_inset->scroll(recursive);
1607 void InsetText::clearSelection(BufferView * bv)
1609 text_.clearSelection();
1613 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1615 Painter & pain = bv->painter();
1617 int h = dim_.asc + dim_.des;
1618 int ty = baseline - dim_.asc;
1624 if (ty + h > pain.paperHeight())
1625 h = pain.paperHeight();
1626 if (top_x + drawTextXOffset + w > pain.paperWidth())
1627 w = pain.paperWidth();
1628 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1632 ParagraphList * InsetText::getParagraphs(int i) const
1634 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1638 LyXCursor const & InsetText::cursor(BufferView * bv) const
1640 if (the_locking_inset)
1641 return the_locking_inset->cursor(bv);
1642 return getLyXText(bv)->cursor;
1646 InsetOld * InsetText::getInsetFromID(int id_arg) const
1649 return const_cast<InsetText *>(this);
1651 ParagraphList::const_iterator pit = paragraphs.begin();
1652 ParagraphList::const_iterator pend = paragraphs.end();
1653 for (; pit != pend; ++pit) {
1654 InsetList::const_iterator it = pit->insetlist.begin();
1655 InsetList::const_iterator end = pit->insetlist.end();
1656 for (; it != end; ++it) {
1657 if (it->inset->id() == id_arg)
1659 InsetOld * in = it->inset->getInsetFromID(id_arg);
1669 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1672 if (the_locking_inset) {
1673 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1674 if (!word.word().empty()) {
1678 // we have to go on checking so move cursor to the next char
1679 text_.cursor.pos(text_.cursor.pos() + 1);
1681 word = text_.selectNextWordToSpellcheck(value);
1682 if (word.word().empty())
1683 bv->unlockInset(const_cast<InsetText *>(this));
1690 void InsetText::selectSelectedWord(BufferView * bv)
1692 if (the_locking_inset) {
1693 the_locking_inset->selectSelectedWord(bv);
1696 getLyXText(bv)->selectSelectedWord();
1697 updateLocal(bv, false);
1701 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1703 if (the_locking_inset) {
1704 if (the_locking_inset->nextChange(bv, length))
1706 text_.cursorRight(true);
1708 lyx::find::SearchResult result =
1709 lyx::find::findNextChange(bv, &text_, length);
1711 if (result == lyx::find::SR_FOUND) {
1712 LyXCursor cur = text_.cursor;
1713 bv->unlockInset(bv->theLockingInset());
1714 if (bv->lockInset(this))
1717 text_.setSelectionRange(length);
1718 updateLocal(bv, false);
1720 return result != lyx::find::SR_NOT_FOUND;
1724 bool InsetText::searchForward(BufferView * bv, string const & str,
1727 if (the_locking_inset) {
1728 if (the_locking_inset->searchForward(bv, str, cs, mw))
1730 text_.cursorRight(true);
1732 lyx::find::SearchResult result =
1733 lyx::find::find(bv, &text_, str, true, cs, mw);
1735 if (result == lyx::find::SR_FOUND) {
1736 LyXCursor cur = text_.cursor;
1737 bv->unlockInset(bv->theLockingInset());
1738 if (bv->lockInset(this))
1741 text_.setSelectionRange(str.length());
1742 updateLocal(bv, false);
1744 return result != lyx::find::SR_NOT_FOUND;
1748 bool InsetText::searchBackward(BufferView * bv, string const & str,
1751 if (the_locking_inset) {
1752 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1756 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1757 text_.setCursor(pit, pit->size());
1759 lyx::find::SearchResult result =
1760 lyx::find::find(bv, &text_, str, false, cs, mw);
1762 if (result == lyx::find::SR_FOUND) {
1763 LyXCursor cur = text_.cursor;
1764 bv->unlockInset(bv->theLockingInset());
1765 if (bv->lockInset(this))
1768 text_.setSelectionRange(str.length());
1769 updateLocal(bv, false);
1771 return result != lyx::find::SR_NOT_FOUND;
1775 bool InsetText::checkInsertChar(LyXFont & font)
1777 return owner() ? owner()->checkInsertChar(font) : true;
1781 void InsetText::collapseParagraphs(BufferView * bv)
1783 while (paragraphs.size() > 1) {
1784 ParagraphList::iterator first_par = paragraphs.begin();
1785 ParagraphList::iterator next_par = boost::next(first_par);
1786 size_t const first_par_size = first_par->size();
1788 if (!first_par->empty() &&
1789 !next_par->empty() &&
1790 !first_par->isSeparator(first_par_size - 1)) {
1791 first_par->insertChar(first_par_size, ' ');
1794 if (text_.selection.set()) {
1795 if (text_.selection.start.par() == next_par) {
1796 text_.selection.start.par(first_par);
1797 text_.selection.start.pos(
1798 text_.selection.start.pos() + first_par_size);
1800 if (text_.selection.end.par() == next_par) {
1801 text_.selection.end.par(first_par);
1802 text_.selection.end.pos(
1803 text_.selection.end.pos() + first_par_size);
1807 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1812 void InsetText::getDrawFont(LyXFont & font) const
1816 owner()->getDrawFont(font);
1820 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1822 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1823 // And it probably does. You have to take a look at this John. (Lgb)
1824 #warning John, have a look here. (Lgb)
1825 ParagraphList::iterator pit = plist.begin();
1826 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1828 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1830 ParagraphList::iterator pend = plist.end();
1831 for (; pit != pend; ++pit)
1832 paragraphs.push_back(*pit);
1836 void InsetText::addPreview(PreviewLoader & loader) const
1838 ParagraphList::const_iterator pit = paragraphs.begin();
1839 ParagraphList::const_iterator pend = paragraphs.end();
1841 for (; pit != pend; ++pit) {
1842 InsetList::const_iterator it = pit->insetlist.begin();
1843 InsetList::const_iterator end = pit->insetlist.end();
1844 for (; it != end; ++it)
1845 it->inset->addPreview(loader);