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)
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)
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 baseline) 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, baseline);
269 // no draw is necessary !!!
270 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
271 top_baseline = baseline;
280 top_baseline = baseline;
281 top_y = baseline - 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 RowList::iterator rit = text_.firstRow();
291 RowList::iterator end = text_.endRow();
293 int y_offset = baseline - rit->ascent_of_text();
294 int y2 = pain.paperHeight();
297 while (rit != end && y + rit->height() <= 0) {
299 first += rit->height();
300 rit = text_.nextRow(rit);
303 text_.top_y(-y_offset);
311 int yo = y_offset + first;
313 paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
315 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
316 drawFrame(pain, int(start_x));
320 void InsetText::drawFrame(Painter & pain, int x) const
322 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
323 int const frame_x = x + ttoD2;
324 int const frame_y = top_baseline - dim_.asc + ttoD2;
325 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
326 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
327 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frame_color);
331 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
336 if (!autoBreakRows && paragraphs.size() > 1)
337 collapseParagraphs(bv);
339 if (!text_.selection.set())
340 text_.selection.cursor = text_.cursor;
344 bv->owner()->view_state_changed();
345 bv->owner()->updateMenubar();
346 bv->owner()->updateToolbar();
347 if (old_par != cpar()) {
348 bv->owner()->setLayout(cpar()->layout()->name());
354 string const InsetText::editMessage() const
356 return _("Opened Text Inset");
360 void InsetText::insetUnlock(BufferView * bv)
362 if (the_locking_inset) {
363 the_locking_inset->insetUnlock(bv);
364 the_locking_inset = 0;
365 updateLocal(bv, false);
370 if (text_.selection.set()) {
371 text_.clearSelection();
372 } else if (owner()) {
373 bv->owner()->setLayout(owner()->getLyXText(bv)
374 ->cursor.par()->layout()->name());
376 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
377 // hack for deleteEmptyParMech
378 ParagraphList::iterator first_par = paragraphs.begin();
379 if (!first_par->empty()) {
380 text_.setCursor(first_par, 0);
381 } else if (paragraphs.size() > 1) {
382 text_.setCursor(boost::next(first_par), 0);
387 void InsetText::lockInset(BufferView * bv)
390 the_locking_inset = 0;
391 inset_pos = inset_x = inset_y = 0;
392 inset_boundary = false;
393 inset_par = paragraphs.end();
394 old_par = paragraphs.end();
395 text_.setCursorIntern(paragraphs.begin(), 0);
396 text_.clearSelection();
398 // If the inset is empty set the language of the current font to the
399 // language to the surronding text (if different).
400 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
401 bv->getParentLanguage(this) != text_.current_font.language()) {
402 LyXFont font(LyXFont::ALL_IGNORE);
403 font.setLanguage(bv->getParentLanguage(this));
404 setFont(bv, font, false);
409 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
411 the_locking_inset = inset;
412 inset_x = cix() - top_x + drawTextXOffset;
413 inset_y = ciy() + drawTextYOffset;
416 inset_boundary = cboundary();
420 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
422 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
423 << inset << "): " << endl;
426 if (!the_locking_inset) {
427 ParagraphList::iterator pit = paragraphs.begin();
428 ParagraphList::iterator pend = paragraphs.end();
430 int const id = inset->id();
431 for (; pit != pend; ++pit) {
432 InsetList::iterator it = pit->insetlist.begin();
433 InsetList::iterator const end = pit->insetlist.end();
434 for (; it != end; ++it) {
435 if (it->inset == inset) {
436 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
437 text_.setCursorIntern(pit, it->pos);
438 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
439 lyxerr << "bv: " << bv << " inset: " << inset << endl;
440 lockInset(bv, inset);
441 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
444 if (it->inset->getInsetFromID(id)) {
445 lyxerr << "InsetText::lockInsetInInset: 2" << endl;
446 text_.setCursorIntern(pit, it->pos);
447 it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
448 return the_locking_inset->lockInsetInInset(bv, inset);
452 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
455 if (inset == cpar()->getInset(cpos())) {
456 lyxerr[Debug::INSETS] << "OK" << endl;
457 lockInset(bv, inset);
461 if (the_locking_inset && the_locking_inset == inset) {
462 if (cpar() == inset_par && cpos() == inset_pos) {
463 lyxerr[Debug::INSETS] << "OK" << endl;
464 inset_x = cix() - top_x + drawTextXOffset;
465 inset_y = ciy() + drawTextYOffset;
467 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
469 } else if (the_locking_inset) {
470 lyxerr[Debug::INSETS] << "MAYBE" << endl;
471 return the_locking_inset->lockInsetInInset(bv, inset);
473 lyxerr[Debug::INSETS] << "NOT OK" << endl;
478 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
481 if (!the_locking_inset)
483 if (the_locking_inset == inset) {
484 the_locking_inset->insetUnlock(bv);
485 the_locking_inset = 0;
487 moveRightIntern(bv, true, false);
488 old_par = paragraphs.end(); // force layout setting
492 updateLocal(bv, false);
495 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
499 void InsetText::lfunMousePress(FuncRequest const & cmd)
503 // use this to check mouse motion for selection!
507 BufferView * bv = cmd.view();
508 FuncRequest cmd1 = cmd;
514 int tmp_x = cmd.x - drawTextXOffset;
515 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
516 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
518 if (the_locking_inset) {
519 if (the_locking_inset == inset) {
520 the_locking_inset->localDispatch(cmd1);
523 // otherwise only unlock the_locking_inset
524 the_locking_inset->insetUnlock(bv);
525 the_locking_inset = 0;
528 no_selection = false;
530 if (bv->theLockingInset()) {
531 if (isHighlyEditableInset(inset)) {
532 // We just have to lock the inset before calling a
534 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
535 if (!bv->lockInset(uinset)) {
536 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
538 inset->localDispatch(cmd1);
539 if (the_locking_inset)
540 updateLocal(bv, false);
545 bool paste_internally = false;
546 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
547 localDispatch(FuncRequest(bv, LFUN_COPY));
548 paste_internally = true;
550 int old_top_y = text_.top_y();
552 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
554 // set the selection cursor!
555 text_.selection.cursor = text_.cursor;
556 text_.cursor.x_fix(text_.cursor.x());
558 text_.clearSelection();
559 updateLocal(bv, false);
561 bv->owner()->setLayout(cpar()->layout()->name());
563 // we moved the view we cannot do mouse selection in this case!
564 if (getLyXText(bv)->top_y() != old_top_y)
567 // Insert primary selection with middle mouse
568 // if there is a local selection in the current buffer,
570 if (cmd.button() == mouse_button::button2) {
571 if (paste_internally)
572 localDispatch(FuncRequest(bv, LFUN_PASTE));
574 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
577 getLyXText(bv)->clearSelection();
582 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
584 BufferView * bv = cmd.view();
585 FuncRequest cmd1 = cmd;
590 if (the_locking_inset)
591 return the_locking_inset->localDispatch(cmd1);
593 int tmp_x = cmd.x - drawTextXOffset;
594 int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
595 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
598 // This code should probably be removed now. Simple insets
599 // (!highlyEditable) can actually take the localDispatch,
600 // and turn it into edit() if necessary. But we still
601 // need to deal properly with the whole relative vs.
602 // absolute mouse co-ords thing in a realiable, sensible way
604 if (isHighlyEditableInset(inset))
605 ret = inset->localDispatch(cmd1);
607 inset_x = cix(bv) - top_x + drawTextXOffset;
608 inset_y = ciy() + drawTextYOffset;
609 cmd1.x = cmd.x - inset_x;
610 cmd1.y = cmd.x - inset_y;
611 inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
615 ret = inset->localDispatch(cmd1);
616 updateLocal(bv, false);
623 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
625 FuncRequest cmd1 = cmd;
629 if (the_locking_inset) {
630 the_locking_inset->localDispatch(cmd1);
634 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
637 BufferView * bv = cmd.view();
638 LyXCursor cur = text_.cursor;
639 text_.setCursorFromCoordinates
640 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
641 text_.cursor.x_fix(text_.cursor.x());
642 if (cur == text_.cursor)
644 text_.setSelection();
645 updateLocal(bv, false);
649 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
651 BufferView * bv = cmd.view();
654 switch (cmd.action) {
655 case LFUN_INSET_EDIT: {
656 UpdatableInset::localDispatch(cmd);
658 if (!bv->lockInset(this)) {
659 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
664 the_locking_inset = 0;
668 inset_boundary = false;
669 inset_par = paragraphs.end();
670 old_par = paragraphs.end();
673 if (cmd.argument.size()) {
674 if (cmd.argument == "left")
675 text_.setCursorIntern(paragraphs.begin(), 0);
677 ParagraphList::iterator it = boost::prior(paragraphs.end());
678 text_.setCursor(it, it->size());
681 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
682 // we put here -1 and not button as now the button in the
683 // edit call should not be needed we will fix this in 1.3.x
684 // cycle hopefully (Jug 20020509)
685 // FIXME: GUII I've changed this to none: probably WRONG
686 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
687 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
689 text_.cursor.x_fix(text_.cursor.x());
693 text_.clearSelection();
696 // If the inset is empty set the language of the current font to the
697 // language to the surronding text (if different).
698 if (paragraphs.begin()->empty() &&
699 paragraphs.size() == 1 &&
700 bv->getParentLanguage(this) != text_.current_font.language())
702 LyXFont font(LyXFont::ALL_IGNORE);
703 font.setLanguage(bv->getParentLanguage(this));
704 setFont(bv, font, false);
707 updateLocal(bv, false);
708 // Tell the paragraph dialog that we've entered an insettext.
709 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
713 case LFUN_MOUSE_PRESS:
717 case LFUN_MOUSE_MOTION:
718 lfunMouseMotion(cmd);
721 case LFUN_MOUSE_RELEASE:
722 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
728 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
729 no_selection = false;
731 RESULT result = UpdatableInset::localDispatch(cmd);
732 if (result != UNDISPATCHED)
736 if (cmd.action < 0 && cmd.argument.empty())
739 if (the_locking_inset) {
740 result = the_locking_inset->localDispatch(cmd);
741 if (result == DISPATCHED_NOUPDATE)
743 if (result == DISPATCHED) {
744 updateLocal(bv, false);
747 if (result >= FINISHED) {
750 moveRightIntern(bv, false, false);
755 if (result >= FINISHED) {
756 updateLocal(bv, false);
757 bv->unlockInset(this);
761 result = moveDown(bv);
762 if (result >= FINISHED) {
763 updateLocal(bv, false);
764 bv->unlockInset(this);
771 the_locking_inset = 0;
772 updateLocal(bv, false);
773 // make sure status gets reset immediately
774 bv->owner()->clearMessage();
778 bool updflag = false;
780 switch (cmd.action) {
783 case LFUN_SELFINSERT:
784 if (bv->buffer()->isReadonly()) {
785 // setErrorMessage(N_("Document is read only"));
788 if (!cmd.argument.empty()) {
789 /* Automatically delete the currently selected
790 * text and replace it with what is being
791 * typed in now. Depends on lyxrc settings
792 * "auto_region_delete", which defaults to
795 // This should not be needed here and is also WRONG!
796 recordUndo(bv, Undo::INSERT, text_.cursor.par());
800 if (lyxrc.auto_region_delete && text_.selection.set())
801 text_.cutSelection(false, false);
802 text_.clearSelection();
804 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
805 bv->owner()->getIntl().getTransManager().
806 TranslateAndInsert(cmd.argument[i], &text_);
808 text_.selection.cursor = text_.cursor;
810 result = DISPATCHED_NOUPDATE;
813 // cursor movements that need special handling
816 result = moveRight(bv);
821 result = moveLeft(bv);
825 result = moveDown(bv);
833 if (crow() == text_.firstRow())
834 result = FINISHED_UP;
836 text_.cursorPrevious();
837 text_.clearSelection();
838 result = DISPATCHED_NOUPDATE;
843 if (crow() == text_.lastRow())
844 result = FINISHED_DOWN;
847 text_.clearSelection();
848 result = DISPATCHED_NOUPDATE;
853 if (text_.selection.set())
854 text_.cutSelection(true, false);
861 if (text_.selection.set())
862 text_.cutSelection(true, false);
869 if (!autoBreakRows) {
870 if (CutAndPaste::nrOfParagraphs() > 1) {
872 #warning FIXME horrendously bad UI
874 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
879 replaceSelection(bv->getLyXText());
880 size_t sel_index = 0;
881 string const & arg = cmd.argument;
882 if (isStrUnsignedInt(arg)) {
883 size_t const paste_arg = strToUnsignedInt(arg);
884 #warning FIXME Check if the arg is in the domain of available selections.
885 sel_index = paste_arg;
887 text_.pasteSelection(sel_index);
889 text_.clearSelection();
894 case LFUN_BREAKPARAGRAPH:
895 if (!autoBreakRows) {
899 replaceSelection(bv->getLyXText());
900 text_.breakParagraph(paragraphs, 0);
904 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
905 if (!autoBreakRows) {
909 replaceSelection(bv->getLyXText());
910 text_.breakParagraph(paragraphs, 1);
914 case LFUN_BREAKLINE: {
915 if (!autoBreakRows) {
920 replaceSelection(bv->getLyXText());
921 text_.insertInset(new InsetNewline);
927 // do not set layouts on non breakable textinsets
929 string cur_layout = cpar()->layout()->name();
931 // Derive layout number from given argument (string)
932 // and current buffer's textclass (number).
933 LyXTextClass const & tclass =
934 bv->buffer()->params.getLyXTextClass();
935 string layout = cmd.argument;
936 bool hasLayout = tclass.hasLayout(layout);
938 // If the entry is obsolete, use the new one instead.
940 string const & obs = tclass[layout]->obsoleted_by();
945 // see if we found the layout number:
947 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
948 bv->owner()->dispatch(lf);
952 if (cur_layout != layout) {
954 text_.setLayout(layout);
955 bv->owner()->setLayout(cpar()->layout()->name());
959 // reset the layout box
960 bv->owner()->setLayout(cpar()->layout()->name());
965 if (!bv->dispatch(cmd))
966 result = UNDISPATCHED;
970 updateLocal(bv, updflag);
972 /// If the action has deleted all text in the inset, we need to change the
973 // language to the language of the surronding text.
974 if (!was_empty && paragraphs.begin()->empty() &&
975 paragraphs.size() == 1) {
976 LyXFont font(LyXFont::ALL_IGNORE);
977 font.setLanguage(bv->getParentLanguage(this));
978 setFont(bv, font, false);
981 if (result >= FINISHED)
982 bv->unlockInset(this);
984 if (result == DISPATCHED_NOUPDATE)
990 int InsetText::latex(Buffer const * buf, ostream & os,
991 LatexRunParams const & runparams) const
994 latexParagraphs(buf, paragraphs, os, texrow, runparams);
995 return texrow.rows();
999 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1001 unsigned int lines = 0;
1003 ParagraphList::const_iterator beg = paragraphs.begin();
1004 ParagraphList::const_iterator end = paragraphs.end();
1005 ParagraphList::const_iterator it = beg;
1006 for (; it != end; ++it) {
1007 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1008 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1015 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1017 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1018 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1020 // There is a confusion between the empty paragraph and the default paragraph
1021 // The default paragraph is <p></p>, the empty paragraph is *empty*
1022 // Since none of the floats of linuxdoc accepts standard paragraphs
1023 // I disable them. I don't expect problems. (jamatos 2003/07/27)
1024 for (; pit != pend; ++pit) {
1025 const string name = pit->layout()->latexname();
1027 sgml::openTag(os, 1, 0, name);
1028 buf->simpleLinuxDocOnePar(os, pit, 0);
1030 sgml::closeTag(os, 1, 0, name);
1036 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1038 unsigned int lines = 0;
1040 vector<string> environment_stack(10);
1041 vector<string> environment_inner(10);
1043 int const command_depth = 0;
1046 Paragraph::depth_type depth = 0; // paragraph depth
1048 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1049 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1051 for (; pit != pend; ++pit) {
1052 int desc_on = 0; // description mode
1054 LyXLayout_ptr const & style = pit->layout();
1056 // environment tag closing
1057 for (; depth > pit->params().depth(); --depth) {
1058 if (environment_inner[depth] != "!-- --") {
1059 item_name = "listitem";
1060 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1061 if (environment_inner[depth] == "varlistentry")
1062 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1064 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1065 environment_stack[depth].erase();
1066 environment_inner[depth].erase();
1069 if (depth == pit->params().depth()
1070 && environment_stack[depth] != style->latexname()
1071 && !environment_stack[depth].empty()) {
1072 if (environment_inner[depth] != "!-- --") {
1073 item_name= "listitem";
1074 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1075 if (environment_inner[depth] == "varlistentry")
1076 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1079 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1081 environment_stack[depth].erase();
1082 environment_inner[depth].erase();
1085 // Write opening SGML tags.
1086 switch (style->latextype) {
1087 case LATEX_PARAGRAPH:
1088 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1092 buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1096 case LATEX_ENVIRONMENT:
1097 case LATEX_ITEM_ENVIRONMENT:
1098 if (depth < pit->params().depth()) {
1099 depth = pit->params().depth();
1100 environment_stack[depth].erase();
1103 if (environment_stack[depth] != style->latexname()) {
1104 if (environment_stack.size() == depth + 1) {
1105 environment_stack.push_back("!-- --");
1106 environment_inner.push_back("!-- --");
1108 environment_stack[depth] = style->latexname();
1109 environment_inner[depth] = "!-- --";
1110 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1112 if (environment_inner[depth] != "!-- --") {
1113 item_name= "listitem";
1114 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1115 if (environment_inner[depth] == "varlistentry")
1116 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1120 if (style->latextype == LATEX_ENVIRONMENT) {
1121 if (!style->latexparam().empty()) {
1122 if (style->latexparam() == "CDATA")
1125 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1130 desc_on = (style->labeltype == LABEL_MANUAL);
1132 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1133 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1135 item_name = desc_on ? "term" : "para";
1136 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1140 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1144 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1147 // write closing SGML tags
1148 switch (style->latextype) {
1149 case LATEX_ENVIRONMENT:
1150 if (!style->latexparam().empty()) {
1151 if (style->latexparam() == "CDATA")
1154 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1157 case LATEX_ITEM_ENVIRONMENT:
1161 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1163 case LATEX_PARAGRAPH:
1164 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1167 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1173 for (int d = depth; d >= 0; --d) {
1174 if (!environment_stack[depth].empty()) {
1175 if (environment_inner[depth] != "!-- --") {
1176 item_name = "listitem";
1177 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1178 if (environment_inner[depth] == "varlistentry")
1179 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1182 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1190 void InsetText::validate(LaTeXFeatures & features) const
1192 for_each(paragraphs.begin(), paragraphs.end(),
1193 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1197 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1199 if (the_locking_inset) {
1200 the_locking_inset->getCursor(bv, x, y);
1204 y = cy() + InsetText::y();
1208 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1210 if (the_locking_inset) {
1211 the_locking_inset->getCursorPos(bv, x, y);
1214 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1215 y = cy() - TEXT_TO_INSET_OFFSET;
1219 int InsetText::insetInInsetY() const
1221 if (!the_locking_inset)
1224 return inset_y + the_locking_inset->insetInInsetY();
1228 void InsetText::fitInsetCursor(BufferView * bv) const
1230 if (the_locking_inset) {
1231 the_locking_inset->fitInsetCursor(bv);
1235 LyXFont const font = text_.getFont(cpar(), cpos());
1237 int const asc = font_metrics::maxAscent(font);
1238 int const desc = font_metrics::maxDescent(font);
1240 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1244 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1246 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1247 return moveLeftIntern(bv, false, true, false);
1249 return moveRightIntern(bv, true, true, false);
1253 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1255 if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1256 return moveRightIntern(bv, true, true, false);
1258 return moveLeftIntern(bv, false, true, false);
1263 InsetText::moveRightIntern(BufferView * bv, bool front,
1264 bool activate_inset, bool selecting)
1266 ParagraphList::iterator c_par = cpar();
1268 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1269 return FINISHED_RIGHT;
1270 if (activate_inset && checkAndActivateInset(bv, front))
1272 text_.cursorRight(bv);
1274 text_.clearSelection();
1275 return DISPATCHED_NOUPDATE;
1280 InsetText::moveLeftIntern(BufferView * bv, bool front,
1281 bool activate_inset, bool selecting)
1283 if (cpar() == paragraphs.begin() && cpos() <= 0)
1285 text_.cursorLeft(bv);
1287 text_.clearSelection();
1288 if (activate_inset && checkAndActivateInset(bv, front))
1290 return DISPATCHED_NOUPDATE;
1294 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1296 if (crow() == text_.firstRow())
1299 text_.clearSelection();
1300 return DISPATCHED_NOUPDATE;
1304 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1306 if (crow() == text_.lastRow())
1307 return FINISHED_DOWN;
1308 text_.cursorDown(bv);
1309 text_.clearSelection();
1310 return DISPATCHED_NOUPDATE;
1314 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1316 if (the_locking_inset) {
1317 if (the_locking_inset->insetAllowed(inset))
1318 return the_locking_inset->insertInset(bv, inset);
1321 inset->setOwner(this);
1322 text_.insertInset(inset);
1324 updateLocal(bv, true);
1329 bool InsetText::insetAllowed(InsetOld::Code code) const
1331 // in_insetAllowed is a really gross hack,
1332 // to allow us to call the owner's insetAllowed
1333 // without stack overflow, which can happen
1334 // when the owner uses InsetCollapsable::insetAllowed()
1336 if (in_insetAllowed)
1338 in_insetAllowed = true;
1339 if (the_locking_inset)
1340 ret = the_locking_inset->insetAllowed(code);
1342 ret = owner()->insetAllowed(code);
1343 in_insetAllowed = false;
1348 UpdatableInset * InsetText::getLockingInset() const
1350 return the_locking_inset ? the_locking_inset->getLockingInset() :
1351 const_cast<InsetText *>(this);
1355 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1359 if (the_locking_inset)
1360 return the_locking_inset->getFirstLockingInsetOfType(c);
1365 bool InsetText::showInsetDialog(BufferView * bv) const
1367 if (the_locking_inset)
1368 return the_locking_inset->showInsetDialog(bv);
1373 void InsetText::getLabelList(std::vector<string> & list) const
1375 ParagraphList::const_iterator pit = paragraphs.begin();
1376 ParagraphList::const_iterator pend = paragraphs.end();
1377 for (; pit != pend; ++pit) {
1378 InsetList::const_iterator beg = pit->insetlist.begin();
1379 InsetList::const_iterator end = pit->insetlist.end();
1380 for (; beg != end; ++beg)
1381 beg->inset->getLabelList(list);
1386 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1389 if (the_locking_inset) {
1390 the_locking_inset->setFont(bv, font, toggleall, selectall);
1394 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1395 || cpar()->empty()) {
1396 text_.setFont(font, toggleall);
1401 if (text_.selection.set())
1402 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1406 text_.selection.cursor = text_.cursor;
1407 text_.cursorBottom();
1408 text_.setSelection();
1411 text_.toggleFree(font, toggleall);
1414 text_.clearSelection();
1417 updateLocal(bv, true);
1421 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1423 if (cpos() == cpar()->size())
1425 InsetOld * inset = cpar()->getInset(cpos());
1426 if (!isHighlyEditableInset(inset))
1428 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1429 inset->localDispatch(cmd);
1430 if (!the_locking_inset)
1432 updateLocal(bv, false);
1437 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1438 mouse_button::state button)
1440 x -= drawTextXOffset;
1442 int dummyy = y + dim_.asc;
1443 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1444 // we only do the edit() call if the inset was hit by the mouse
1445 // or if it is a highly editable inset. So we should call this
1446 // function from our own edit with button < 0.
1447 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1449 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1458 inset_x = cix() - top_x + drawTextXOffset;
1459 inset_y = ciy() + drawTextYOffset;
1460 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1461 inset->localDispatch(cmd);
1462 if (!the_locking_inset)
1464 updateLocal(bv, false);
1469 void InsetText::markNew(bool track_changes)
1471 ParagraphList::iterator pit = paragraphs.begin();
1472 ParagraphList::iterator end = paragraphs.end();
1473 for (; pit != end; ++pit) {
1474 if (track_changes) {
1475 pit->trackChanges();
1477 // no-op when not tracking
1478 pit->cleanChanges();
1484 void InsetText::setText(string const & data, LyXFont const & font)
1487 for (unsigned int i = 0; i < data.length(); ++i)
1488 paragraphs.begin()->insertChar(i, data[i], font);
1492 void InsetText::setAutoBreakRows(bool flag)
1494 if (flag != autoBreakRows) {
1495 autoBreakRows = flag;
1502 void InsetText::setDrawFrame(DrawFrame how)
1508 void InsetText::setFrameColor(LColor::color col)
1514 int InsetText::cx() const
1516 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1517 if (the_locking_inset) {
1518 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1519 if (font.isVisibleRightToLeft())
1520 x -= the_locking_inset->width();
1526 int InsetText::cix() const
1528 int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1529 if (the_locking_inset) {
1530 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1531 if (font.isVisibleRightToLeft())
1532 x -= the_locking_inset->width();
1538 int InsetText::cy() const
1540 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1544 int InsetText::ciy() const
1546 return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1550 pos_type InsetText::cpos() const
1552 return text_.cursor.pos();
1556 ParagraphList::iterator InsetText::cpar() const
1558 return text_.cursor.par();
1562 bool InsetText::cboundary() const
1564 return text_.cursor.boundary();
1568 RowList::iterator InsetText::crow() const
1570 return text_.cursorRow();
1574 LyXText * InsetText::getLyXText(BufferView const * bv,
1575 bool const recursive) const
1578 if (recursive && the_locking_inset)
1579 return the_locking_inset->getLyXText(bv, true);
1584 void InsetText::setViewCache(BufferView const * bv) const
1587 if (bv != text_.bv_owner) {
1588 lyxerr << "setting view cache from "
1589 << text_.bv_owner << " to " << bv << "\n";
1590 text_.init(const_cast<BufferView *>(bv));
1592 text_.bv_owner = const_cast<BufferView *>(bv);
1597 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1600 /// then remove all LyXText in text-insets
1601 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1602 const_cast<ParagraphList&>(paragraphs).end(),
1603 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1608 void InsetText::removeNewlines()
1610 ParagraphList::iterator it = paragraphs.begin();
1611 ParagraphList::iterator end = paragraphs.end();
1612 for (; it != end; ++it)
1613 for (int i = 0; i < it->size(); ++i)
1614 if (it->isNewline(i))
1619 int InsetText::scroll(bool recursive) const
1621 int sx = UpdatableInset::scroll(false);
1623 if (recursive && the_locking_inset)
1624 sx += the_locking_inset->scroll(recursive);
1630 void InsetText::clearSelection(BufferView * bv)
1632 getLyXText(bv)->clearSelection();
1636 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1638 Painter & pain = bv->painter();
1640 int h = dim_.asc + dim_.des;
1641 int ty = baseline - dim_.asc;
1647 if (ty + h > pain.paperHeight())
1648 h = pain.paperHeight();
1649 if (top_x + drawTextXOffset + w > pain.paperWidth())
1650 w = pain.paperWidth();
1651 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1655 ParagraphList * InsetText::getParagraphs(int i) const
1657 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1661 LyXCursor const & InsetText::cursor(BufferView * bv) const
1663 if (the_locking_inset)
1664 return the_locking_inset->cursor(bv);
1665 return getLyXText(bv)->cursor;
1669 InsetOld * InsetText::getInsetFromID(int id_arg) const
1672 return const_cast<InsetText *>(this);
1674 ParagraphList::const_iterator pit = paragraphs.begin();
1675 ParagraphList::const_iterator pend = paragraphs.end();
1676 for (; pit != pend; ++pit) {
1677 InsetList::const_iterator it = pit->insetlist.begin();
1678 InsetList::const_iterator end = pit->insetlist.end();
1679 for (; it != end; ++it) {
1680 if (it->inset->id() == id_arg)
1682 InsetOld * in = it->inset->getInsetFromID(id_arg);
1692 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1695 if (the_locking_inset) {
1696 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1697 if (!word.word().empty()) {
1701 // we have to go on checking so move cursor to the next char
1702 text_.cursor.pos(text_.cursor.pos() + 1);
1704 word = text_.selectNextWordToSpellcheck(value);
1705 if (word.word().empty())
1706 bv->unlockInset(const_cast<InsetText *>(this));
1713 void InsetText::selectSelectedWord(BufferView * bv)
1715 if (the_locking_inset) {
1716 the_locking_inset->selectSelectedWord(bv);
1719 getLyXText(bv)->selectSelectedWord();
1720 updateLocal(bv, false);
1724 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1726 if (the_locking_inset) {
1727 if (the_locking_inset->nextChange(bv, length))
1729 text_.cursorRight(true);
1731 lyx::find::SearchResult result =
1732 lyx::find::findNextChange(bv, &text_, length);
1734 if (result == lyx::find::SR_FOUND) {
1735 LyXCursor cur = text_.cursor;
1736 bv->unlockInset(bv->theLockingInset());
1737 if (bv->lockInset(this))
1740 text_.setSelectionRange(length);
1741 updateLocal(bv, false);
1743 return result != lyx::find::SR_NOT_FOUND;
1747 bool InsetText::searchForward(BufferView * bv, string const & str,
1750 if (the_locking_inset) {
1751 if (the_locking_inset->searchForward(bv, str, cs, mw))
1753 text_.cursorRight(true);
1755 lyx::find::SearchResult result =
1756 lyx::find::find(bv, &text_, str, true, cs, mw);
1758 if (result == lyx::find::SR_FOUND) {
1759 LyXCursor cur = text_.cursor;
1760 bv->unlockInset(bv->theLockingInset());
1761 if (bv->lockInset(this))
1764 text_.setSelectionRange(str.length());
1765 updateLocal(bv, false);
1767 return result != lyx::find::SR_NOT_FOUND;
1771 bool InsetText::searchBackward(BufferView * bv, string const & str,
1774 if (the_locking_inset) {
1775 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1779 ParagraphList::iterator pit = paragraphs.begin();
1780 ParagraphList::iterator pend = paragraphs.end();
1782 while (boost::next(pit) != pend)
1785 text_.setCursor(pit, pit->size());
1787 lyx::find::SearchResult result =
1788 lyx::find::find(bv, &text_, str, false, cs, mw);
1790 if (result == lyx::find::SR_FOUND) {
1791 LyXCursor cur = text_.cursor;
1792 bv->unlockInset(bv->theLockingInset());
1793 if (bv->lockInset(this))
1796 text_.setSelectionRange(str.length());
1797 updateLocal(bv, false);
1799 return result != lyx::find::SR_NOT_FOUND;
1803 bool InsetText::checkInsertChar(LyXFont & font)
1805 return owner() ? owner()->checkInsertChar(font) : true;
1809 void InsetText::collapseParagraphs(BufferView * bv)
1811 while (paragraphs.size() > 1) {
1812 ParagraphList::iterator first_par = paragraphs.begin();
1813 ParagraphList::iterator next_par = boost::next(first_par);
1814 size_t const first_par_size = first_par->size();
1816 if (!first_par->empty() &&
1817 !next_par->empty() &&
1818 !first_par->isSeparator(first_par_size - 1)) {
1819 first_par->insertChar(first_par_size, ' ');
1822 if (text_.selection.set()) {
1823 if (text_.selection.start.par() == next_par) {
1824 text_.selection.start.par(first_par);
1825 text_.selection.start.pos(
1826 text_.selection.start.pos() + first_par_size);
1828 if (text_.selection.end.par() == next_par) {
1829 text_.selection.end.par(first_par);
1830 text_.selection.end.pos(
1831 text_.selection.end.pos() + first_par_size);
1835 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1840 void InsetText::getDrawFont(LyXFont & font) const
1844 owner()->getDrawFont(font);
1848 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1850 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1851 // And it probably does. You have to take a look at this John. (Lgb)
1852 #warning John, have a look here. (Lgb)
1853 ParagraphList::iterator pit = plist.begin();
1854 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1856 mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1858 ParagraphList::iterator pend = plist.end();
1859 for (; pit != pend; ++pit)
1860 paragraphs.push_back(*pit);
1864 void InsetText::addPreview(PreviewLoader & loader) const
1866 ParagraphList::const_iterator pit = paragraphs.begin();
1867 ParagraphList::const_iterator pend = paragraphs.end();
1869 for (; pit != pend; ++pit) {
1870 InsetList::const_iterator it = pit->insetlist.begin();
1871 InsetList::const_iterator end = pit->insetlist.end();
1872 for (; it != end; ++it)
1873 it->inset->addPreview(loader);