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 = cx() - x + drawTextXOffset;
285 inset_y = cy() + 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;
320 bv->updateInset(this);
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 = cx() - top_x + drawTextXOffset;
390 inset_y = cy() + 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 = cx() - top_x + drawTextXOffset;
442 inset_y = cy() + 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 - 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 = bv->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 (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 - 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 = cx(bv) - top_x + drawTextXOffset;
585 inset_y = cy() + 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(text_.cursor.x());
667 text_.cursor.x_fix(text_.cursor.x());
671 text_.clearSelection();
674 // If the inset is empty set the language of the current font to the
675 // language to the surronding text (if different).
676 if (paragraphs.begin()->empty() &&
677 paragraphs.size() == 1 &&
678 bv->getParentLanguage(this) != text_.current_font.language())
680 LyXFont font(LyXFont::ALL_IGNORE);
681 font.setLanguage(bv->getParentLanguage(this));
682 setFont(bv, font, false);
685 updateLocal(bv, false);
686 // Tell the paragraph dialog that we've entered an insettext.
687 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
691 case LFUN_MOUSE_PRESS:
695 case LFUN_MOUSE_MOTION:
696 lfunMouseMotion(cmd);
699 case LFUN_MOUSE_RELEASE:
700 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
706 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
707 no_selection = false;
709 RESULT result = UpdatableInset::localDispatch(cmd);
710 if (result != UNDISPATCHED)
714 if (cmd.action < 0 && cmd.argument.empty())
717 if (the_locking_inset) {
718 result = the_locking_inset->localDispatch(cmd);
719 if (result == DISPATCHED_NOUPDATE)
721 if (result == DISPATCHED) {
722 updateLocal(bv, false);
725 if (result >= FINISHED) {
728 moveRightIntern(bv, false, false);
733 if (result >= FINISHED) {
734 updateLocal(bv, false);
735 bv->unlockInset(this);
739 result = moveDown(bv);
740 if (result >= FINISHED) {
741 updateLocal(bv, false);
742 bv->unlockInset(this);
749 the_locking_inset = 0;
750 updateLocal(bv, false);
751 // make sure status gets reset immediately
752 bv->owner()->clearMessage();
756 bool updflag = false;
758 switch (cmd.action) {
761 case LFUN_SELFINSERT:
762 if (bv->buffer()->isReadonly()) {
763 // setErrorMessage(N_("Document is read only"));
766 if (!cmd.argument.empty()) {
767 /* Automatically delete the currently selected
768 * text and replace it with what is being
769 * typed in now. Depends on lyxrc settings
770 * "auto_region_delete", which defaults to
773 // This should not be needed here and is also WRONG!
774 recordUndo(bv, Undo::INSERT, text_.cursor.par());
778 if (lyxrc.auto_region_delete && text_.selection.set())
779 text_.cutSelection(false, false);
780 text_.clearSelection();
782 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
783 bv->owner()->getIntl().getTransManager().
784 TranslateAndInsert(cmd.argument[i], &text_);
786 text_.selection.cursor = text_.cursor;
788 result = DISPATCHED_NOUPDATE;
791 // cursor movements that need special handling
794 result = moveRight(bv);
799 result = moveLeft(bv);
803 result = moveDown(bv);
811 if (crow() == text_.firstRow())
812 result = FINISHED_UP;
814 text_.cursorPrevious();
815 text_.clearSelection();
816 result = DISPATCHED_NOUPDATE;
821 if (crow() == text_.lastRow())
822 result = FINISHED_DOWN;
825 text_.clearSelection();
826 result = DISPATCHED_NOUPDATE;
831 if (text_.selection.set())
832 text_.cutSelection(true, false);
839 if (text_.selection.set())
840 text_.cutSelection(true, false);
847 if (!autoBreakRows) {
848 if (CutAndPaste::nrOfParagraphs() > 1) {
850 #warning FIXME horrendously bad UI
852 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
857 replaceSelection(bv->getLyXText());
858 size_t sel_index = 0;
859 string const & arg = cmd.argument;
860 if (isStrUnsignedInt(arg)) {
861 size_t const paste_arg = strToUnsignedInt(arg);
862 #warning FIXME Check if the arg is in the domain of available selections.
863 sel_index = paste_arg;
865 text_.pasteSelection(sel_index);
867 text_.clearSelection();
872 case LFUN_BREAKPARAGRAPH:
873 if (!autoBreakRows) {
877 replaceSelection(bv->getLyXText());
878 text_.breakParagraph(paragraphs, 0);
882 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
883 if (!autoBreakRows) {
887 replaceSelection(bv->getLyXText());
888 text_.breakParagraph(paragraphs, 1);
892 case LFUN_BREAKLINE: {
893 if (!autoBreakRows) {
898 replaceSelection(bv->getLyXText());
899 text_.insertInset(new InsetNewline);
905 // do not set layouts on non breakable textinsets
907 string cur_layout = cpar()->layout()->name();
909 // Derive layout number from given argument (string)
910 // and current buffer's textclass (number).
911 LyXTextClass const & tclass =
912 bv->buffer()->params.getLyXTextClass();
913 string layout = cmd.argument;
914 bool hasLayout = tclass.hasLayout(layout);
916 // If the entry is obsolete, use the new one instead.
918 string const & obs = tclass[layout]->obsoleted_by();
923 // see if we found the layout number:
925 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
926 bv->owner()->dispatch(lf);
930 if (cur_layout != layout) {
932 text_.setLayout(layout);
933 bv->owner()->setLayout(cpar()->layout()->name());
937 // reset the layout box
938 bv->owner()->setLayout(cpar()->layout()->name());
943 if (!bv->dispatch(cmd))
944 result = UNDISPATCHED;
948 updateLocal(bv, updflag);
950 /// If the action has deleted all text in the inset, we need to change the
951 // language to the language of the surronding text.
952 if (!was_empty && paragraphs.begin()->empty() &&
953 paragraphs.size() == 1) {
954 LyXFont font(LyXFont::ALL_IGNORE);
955 font.setLanguage(bv->getParentLanguage(this));
956 setFont(bv, font, false);
959 if (result >= FINISHED)
960 bv->unlockInset(this);
962 if (result == DISPATCHED_NOUPDATE)
968 int InsetText::latex(Buffer const & buf, ostream & os,
969 LatexRunParams const & runparams) const
972 latexParagraphs(buf, paragraphs, os, texrow, runparams);
973 return texrow.rows();
977 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
979 unsigned int lines = 0;
981 ParagraphList::const_iterator beg = paragraphs.begin();
982 ParagraphList::const_iterator end = paragraphs.end();
983 ParagraphList::const_iterator it = beg;
984 for (; it != end; ++it) {
985 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
986 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
993 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
995 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
996 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
998 // There is a confusion between the empty paragraph and the default paragraph
999 // The default paragraph is <p></p>, the empty paragraph is *empty*
1000 // Since none of the floats of linuxdoc accepts standard paragraphs
1001 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1002 for (; pit != pend; ++pit) {
1003 const string name = pit->layout()->latexname();
1005 sgml::openTag(os, 1, 0, name);
1006 buf.simpleLinuxDocOnePar(os, pit, 0);
1008 sgml::closeTag(os, 1, 0, name);
1014 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1016 unsigned int lines = 0;
1018 vector<string> environment_stack(10);
1019 vector<string> environment_inner(10);
1021 int const command_depth = 0;
1024 Paragraph::depth_type depth = 0; // paragraph depth
1026 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1027 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1029 for (; pit != pend; ++pit) {
1030 int desc_on = 0; // description mode
1032 LyXLayout_ptr const & style = pit->layout();
1034 // environment tag closing
1035 for (; depth > pit->params().depth(); --depth) {
1036 if (environment_inner[depth] != "!-- --") {
1037 item_name = "listitem";
1038 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1039 if (environment_inner[depth] == "varlistentry")
1040 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1042 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1043 environment_stack[depth].erase();
1044 environment_inner[depth].erase();
1047 if (depth == pit->params().depth()
1048 && environment_stack[depth] != style->latexname()
1049 && !environment_stack[depth].empty()) {
1050 if (environment_inner[depth] != "!-- --") {
1051 item_name= "listitem";
1052 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1053 if (environment_inner[depth] == "varlistentry")
1054 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1057 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1059 environment_stack[depth].erase();
1060 environment_inner[depth].erase();
1063 // Write opening SGML tags.
1064 switch (style->latextype) {
1065 case LATEX_PARAGRAPH:
1066 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1070 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1074 case LATEX_ENVIRONMENT:
1075 case LATEX_ITEM_ENVIRONMENT:
1076 if (depth < pit->params().depth()) {
1077 depth = pit->params().depth();
1078 environment_stack[depth].erase();
1081 if (environment_stack[depth] != style->latexname()) {
1082 if (environment_stack.size() == depth + 1) {
1083 environment_stack.push_back("!-- --");
1084 environment_inner.push_back("!-- --");
1086 environment_stack[depth] = style->latexname();
1087 environment_inner[depth] = "!-- --";
1088 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1090 if (environment_inner[depth] != "!-- --") {
1091 item_name= "listitem";
1092 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1093 if (environment_inner[depth] == "varlistentry")
1094 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1098 if (style->latextype == LATEX_ENVIRONMENT) {
1099 if (!style->latexparam().empty()) {
1100 if (style->latexparam() == "CDATA")
1103 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1108 desc_on = (style->labeltype == LABEL_MANUAL);
1110 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1111 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1113 item_name = desc_on ? "term" : "para";
1114 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1118 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1122 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1125 // write closing SGML tags
1126 switch (style->latextype) {
1127 case LATEX_ENVIRONMENT:
1128 if (!style->latexparam().empty()) {
1129 if (style->latexparam() == "CDATA")
1132 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1135 case LATEX_ITEM_ENVIRONMENT:
1139 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1141 case LATEX_PARAGRAPH:
1142 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1145 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1151 for (int d = depth; d >= 0; --d) {
1152 if (!environment_stack[depth].empty()) {
1153 if (environment_inner[depth] != "!-- --") {
1154 item_name = "listitem";
1155 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1156 if (environment_inner[depth] == "varlistentry")
1157 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1160 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1168 void InsetText::validate(LaTeXFeatures & features) const
1170 for_each(paragraphs.begin(), paragraphs.end(),
1171 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1175 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1177 if (the_locking_inset) {
1178 the_locking_inset->getCursor(bv, x, y);
1182 y = cy() + InsetText::y();
1186 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1188 if (the_locking_inset) {
1189 the_locking_inset->getCursorPos(bv, x, y);
1192 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1193 y = cy() - TEXT_TO_INSET_OFFSET;
1197 int InsetText::insetInInsetY() const
1199 if (!the_locking_inset)
1202 return inset_y + the_locking_inset->insetInInsetY();
1206 void InsetText::fitInsetCursor(BufferView * bv) const
1208 if (the_locking_inset) {
1209 the_locking_inset->fitInsetCursor(bv);
1213 LyXFont const font = text_.getFont(cpar(), cpos());
1215 int const asc = font_metrics::maxAscent(font);
1216 int const desc = font_metrics::maxDescent(font);
1218 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1222 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1224 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1225 return moveLeftIntern(bv, false, true, false);
1227 return moveRightIntern(bv, true, true, false);
1231 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1233 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1234 return moveRightIntern(bv, true, true, false);
1236 return moveLeftIntern(bv, false, true, false);
1241 InsetText::moveRightIntern(BufferView * bv, bool front,
1242 bool activate_inset, bool selecting)
1244 ParagraphList::iterator c_par = cpar();
1246 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1247 return FINISHED_RIGHT;
1248 if (activate_inset && checkAndActivateInset(bv, front))
1250 text_.cursorRight(bv);
1252 text_.clearSelection();
1253 return DISPATCHED_NOUPDATE;
1258 InsetText::moveLeftIntern(BufferView * bv, bool front,
1259 bool activate_inset, bool selecting)
1261 if (cpar() == paragraphs.begin() && cpos() <= 0)
1263 text_.cursorLeft(bv);
1265 text_.clearSelection();
1266 if (activate_inset && checkAndActivateInset(bv, front))
1268 return DISPATCHED_NOUPDATE;
1272 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1274 if (crow() == text_.firstRow())
1277 text_.clearSelection();
1278 return DISPATCHED_NOUPDATE;
1282 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1284 if (crow() == text_.lastRow())
1285 return FINISHED_DOWN;
1286 text_.cursorDown(bv);
1287 text_.clearSelection();
1288 return DISPATCHED_NOUPDATE;
1292 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1294 if (the_locking_inset) {
1295 if (the_locking_inset->insetAllowed(inset))
1296 return the_locking_inset->insertInset(bv, inset);
1299 inset->setOwner(this);
1300 text_.insertInset(inset);
1302 updateLocal(bv, true);
1307 bool InsetText::insetAllowed(InsetOld::Code code) const
1309 // in_insetAllowed is a really gross hack,
1310 // to allow us to call the owner's insetAllowed
1311 // without stack overflow, which can happen
1312 // when the owner uses InsetCollapsable::insetAllowed()
1314 if (in_insetAllowed)
1316 in_insetAllowed = true;
1317 if (the_locking_inset)
1318 ret = the_locking_inset->insetAllowed(code);
1320 ret = owner()->insetAllowed(code);
1321 in_insetAllowed = false;
1326 UpdatableInset * InsetText::getLockingInset() const
1328 return the_locking_inset ? the_locking_inset->getLockingInset() :
1329 const_cast<InsetText *>(this);
1333 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1337 if (the_locking_inset)
1338 return the_locking_inset->getFirstLockingInsetOfType(c);
1343 bool InsetText::showInsetDialog(BufferView * bv) const
1345 if (the_locking_inset)
1346 return the_locking_inset->showInsetDialog(bv);
1351 void InsetText::getLabelList(std::vector<string> & list) const
1353 ParagraphList::const_iterator pit = paragraphs.begin();
1354 ParagraphList::const_iterator pend = paragraphs.end();
1355 for (; pit != pend; ++pit) {
1356 InsetList::const_iterator beg = pit->insetlist.begin();
1357 InsetList::const_iterator end = pit->insetlist.end();
1358 for (; beg != end; ++beg)
1359 beg->inset->getLabelList(list);
1364 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1367 if (the_locking_inset) {
1368 the_locking_inset->setFont(bv, font, toggleall, selectall);
1372 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1373 || cpar()->empty()) {
1374 text_.setFont(font, toggleall);
1379 if (text_.selection.set())
1380 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1384 text_.selection.cursor = text_.cursor;
1385 text_.cursorBottom();
1386 text_.setSelection();
1389 text_.toggleFree(font, toggleall);
1392 text_.clearSelection();
1395 updateLocal(bv, true);
1399 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1401 if (cpos() == cpar()->size())
1403 InsetOld * inset = cpar()->getInset(cpos());
1404 if (!isHighlyEditableInset(inset))
1406 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1407 inset->localDispatch(cmd);
1408 if (!the_locking_inset)
1410 updateLocal(bv, false);
1415 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1416 mouse_button::state button)
1418 x -= drawTextXOffset;
1420 int dummyy = y + dim_.asc;
1421 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1422 // we only do the edit() call if the inset was hit by the mouse
1423 // or if it is a highly editable inset. So we should call this
1424 // function from our own edit with button < 0.
1425 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1427 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1436 inset_x = cx() - top_x + drawTextXOffset;
1437 inset_y = cy() + drawTextYOffset;
1438 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1439 inset->localDispatch(cmd);
1440 if (!the_locking_inset)
1442 updateLocal(bv, false);
1447 void InsetText::markNew(bool track_changes)
1449 ParagraphList::iterator pit = paragraphs.begin();
1450 ParagraphList::iterator end = paragraphs.end();
1451 for (; pit != end; ++pit) {
1452 if (track_changes) {
1453 pit->trackChanges();
1455 // no-op when not tracking
1456 pit->cleanChanges();
1462 void InsetText::setText(string const & data, LyXFont const & font)
1465 for (unsigned int i = 0; i < data.length(); ++i)
1466 paragraphs.begin()->insertChar(i, data[i], font);
1470 void InsetText::setAutoBreakRows(bool flag)
1472 if (flag != autoBreakRows) {
1473 autoBreakRows = flag;
1480 void InsetText::setDrawFrame(DrawFrame how)
1486 void InsetText::setFrameColor(LColor::color col)
1492 int InsetText::cx() const
1494 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1495 if (the_locking_inset) {
1496 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1497 if (font.isVisibleRightToLeft())
1498 x -= the_locking_inset->width();
1504 int InsetText::cy() const
1506 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1510 pos_type InsetText::cpos() const
1512 return text_.cursor.pos();
1516 ParagraphList::iterator InsetText::cpar() const
1518 return text_.cursor.par();
1522 bool InsetText::cboundary() const
1524 return text_.cursor.boundary();
1528 RowList::iterator InsetText::crow() const
1530 return text_.cursorRow();
1534 LyXText * InsetText::getLyXText(BufferView const * bv,
1535 bool const recursive) const
1538 if (recursive && the_locking_inset)
1539 return the_locking_inset->getLyXText(bv, true);
1544 void InsetText::setViewCache(BufferView const * bv) const
1547 if (bv != text_.bv_owner) {
1548 //lyxerr << "setting view cache from "
1549 // << text_.bv_owner << " to " << bv << "\n";
1550 text_.init(const_cast<BufferView *>(bv));
1552 text_.bv_owner = const_cast<BufferView *>(bv);
1557 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1560 /// then remove all LyXText in text-insets
1561 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1562 const_cast<ParagraphList&>(paragraphs).end(),
1563 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1568 void InsetText::removeNewlines()
1570 ParagraphList::iterator it = paragraphs.begin();
1571 ParagraphList::iterator end = paragraphs.end();
1572 for (; it != end; ++it)
1573 for (int i = 0; i < it->size(); ++i)
1574 if (it->isNewline(i))
1579 int InsetText::scroll(bool recursive) const
1581 int sx = UpdatableInset::scroll(false);
1583 if (recursive && the_locking_inset)
1584 sx += the_locking_inset->scroll(recursive);
1590 void InsetText::clearSelection(BufferView *)
1592 text_.clearSelection();
1596 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1598 Painter & pain = bv->painter();
1600 int h = dim_.asc + dim_.des;
1601 int ty = baseline - dim_.asc;
1607 if (ty + h > pain.paperHeight())
1608 h = pain.paperHeight();
1609 if (top_x + drawTextXOffset + w > pain.paperWidth())
1610 w = pain.paperWidth();
1611 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1615 ParagraphList * InsetText::getParagraphs(int i) const
1617 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1621 LyXCursor const & InsetText::cursor(BufferView * bv) const
1623 if (the_locking_inset)
1624 return the_locking_inset->cursor(bv);
1625 return getLyXText(bv)->cursor;
1629 InsetOld * InsetText::getInsetFromID(int id_arg) const
1632 return const_cast<InsetText *>(this);
1634 ParagraphList::const_iterator pit = paragraphs.begin();
1635 ParagraphList::const_iterator pend = paragraphs.end();
1636 for (; pit != pend; ++pit) {
1637 InsetList::const_iterator it = pit->insetlist.begin();
1638 InsetList::const_iterator end = pit->insetlist.end();
1639 for (; it != end; ++it) {
1640 if (it->inset->id() == id_arg)
1642 InsetOld * in = it->inset->getInsetFromID(id_arg);
1652 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1655 if (the_locking_inset) {
1656 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1657 if (!word.word().empty()) {
1661 // we have to go on checking so move cursor to the next char
1662 text_.cursor.pos(text_.cursor.pos() + 1);
1664 word = text_.selectNextWordToSpellcheck(value);
1665 if (word.word().empty())
1666 bv->unlockInset(const_cast<InsetText *>(this));
1673 void InsetText::selectSelectedWord(BufferView * bv)
1675 if (the_locking_inset) {
1676 the_locking_inset->selectSelectedWord(bv);
1679 getLyXText(bv)->selectSelectedWord();
1680 updateLocal(bv, false);
1684 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1686 if (the_locking_inset) {
1687 if (the_locking_inset->nextChange(bv, length))
1689 text_.cursorRight(true);
1691 lyx::find::SearchResult result =
1692 lyx::find::findNextChange(bv, &text_, length);
1694 if (result == lyx::find::SR_FOUND) {
1695 LyXCursor cur = text_.cursor;
1696 bv->unlockInset(bv->theLockingInset());
1697 if (bv->lockInset(this))
1700 text_.setSelectionRange(length);
1701 updateLocal(bv, false);
1703 return result != lyx::find::SR_NOT_FOUND;
1707 bool InsetText::searchForward(BufferView * bv, string const & str,
1710 if (the_locking_inset) {
1711 if (the_locking_inset->searchForward(bv, str, cs, mw))
1713 text_.cursorRight(true);
1715 lyx::find::SearchResult result =
1716 lyx::find::find(bv, &text_, str, true, 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::searchBackward(BufferView * bv, string const & str,
1734 if (the_locking_inset) {
1735 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1739 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1740 text_.setCursor(pit, pit->size());
1742 lyx::find::SearchResult result =
1743 lyx::find::find(bv, &text_, str, false, cs, mw);
1745 if (result == lyx::find::SR_FOUND) {
1746 LyXCursor cur = text_.cursor;
1747 bv->unlockInset(bv->theLockingInset());
1748 if (bv->lockInset(this))
1751 text_.setSelectionRange(str.length());
1752 updateLocal(bv, false);
1754 return result != lyx::find::SR_NOT_FOUND;
1758 bool InsetText::checkInsertChar(LyXFont & font)
1760 return owner() ? owner()->checkInsertChar(font) : true;
1764 void InsetText::collapseParagraphs(BufferView * bv)
1766 while (paragraphs.size() > 1) {
1767 ParagraphList::iterator first_par = paragraphs.begin();
1768 ParagraphList::iterator next_par = boost::next(first_par);
1769 size_t const first_par_size = first_par->size();
1771 if (!first_par->empty() &&
1772 !next_par->empty() &&
1773 !first_par->isSeparator(first_par_size - 1)) {
1774 first_par->insertChar(first_par_size, ' ');
1777 if (text_.selection.set()) {
1778 if (text_.selection.start.par() == next_par) {
1779 text_.selection.start.par(first_par);
1780 text_.selection.start.pos(
1781 text_.selection.start.pos() + first_par_size);
1783 if (text_.selection.end.par() == next_par) {
1784 text_.selection.end.par(first_par);
1785 text_.selection.end.pos(
1786 text_.selection.end.pos() + first_par_size);
1790 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1795 void InsetText::getDrawFont(LyXFont & font) const
1799 owner()->getDrawFont(font);
1803 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1805 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1806 // And it probably does. You have to take a look at this John. (Lgb)
1807 #warning John, have a look here. (Lgb)
1808 ParagraphList::iterator pit = plist.begin();
1809 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1811 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1813 ParagraphList::iterator pend = plist.end();
1814 for (; pit != pend; ++pit)
1815 paragraphs.push_back(*pit);
1819 void InsetText::addPreview(PreviewLoader & loader) const
1821 ParagraphList::const_iterator pit = paragraphs.begin();
1822 ParagraphList::const_iterator pend = paragraphs.end();
1824 for (; pit != pend; ++pit) {
1825 InsetList::const_iterator it = pit->insetlist.begin();
1826 InsetList::const_iterator end = pit->insetlist.end();
1827 for (; it != end; ++it)
1828 it->inset->addPreview(loader);