3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettext.h"
14 #include "insetnewline.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
21 #include "errorlist.h"
22 #include "funcrequest.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "rowpainter.h"
36 #include "undo_funcs.h"
37 #include "WordLangTuple.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
44 #include "support/lyxalgo.h" // lyx::count
46 #include <boost/bind.hpp>
48 using bv_funcs::replaceSelection;
52 using lyx::graphics::PreviewLoader;
54 using lyx::support::isStrUnsignedInt;
55 using lyx::support::strToUnsignedInt;
66 InsetText::InsetText(BufferParams const & bp)
69 autoBreakRows_(false),
71 frame_color_(LColor::insetframe),
72 text_(0, this, true, paragraphs)
74 textwidth_ = 0; // broken
75 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
76 if (bp.tracking_changes)
77 paragraphs.begin()->trackChanges();
82 InsetText::InsetText(InsetText const & in)
84 text_(in.text_.bv_owner, this, true, paragraphs)
91 void InsetText::operator=(InsetText const & in)
93 UpdatableInset::operator=(in);
94 paragraphs = in.paragraphs;
95 autoBreakRows_ = in.autoBreakRows_;
96 drawFrame_ = in.drawFrame_;
97 frame_color_ = in.frame_color_;
98 textwidth_ = in.textwidth_;
99 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
104 void InsetText::init()
106 ParagraphList::iterator pit = paragraphs.begin();
107 ParagraphList::iterator end = paragraphs.end();
108 for (; pit != end; ++pit)
109 pit->setInsetOwner(this);
110 text_.paragraphs_ = ¶graphs;
119 the_locking_inset = 0;
121 in_insetAllowed = false;
127 void InsetText::clear(bool just_mark_erased)
129 if (just_mark_erased) {
130 ParagraphList::iterator it = paragraphs.begin();
131 ParagraphList::iterator end = paragraphs.end();
132 for (; it != end; ++it)
137 // This is a gross hack...
138 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
141 paragraphs.push_back(Paragraph());
142 paragraphs.begin()->setInsetOwner(this);
143 paragraphs.begin()->layout(old_layout);
147 auto_ptr<InsetBase> InsetText::clone() const
149 return auto_ptr<InsetBase>(new InsetText(*this));
153 void InsetText::write(Buffer const & buf, ostream & os) const
156 writeParagraphData(buf, os);
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
162 ParagraphList::const_iterator it = paragraphs.begin();
163 ParagraphList::const_iterator end = paragraphs.end();
164 Paragraph::depth_type dth = 0;
165 for (; it != end; ++it) {
166 it->write(buf, os, buf.params(), dth);
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
174 Paragraph::depth_type depth = 0;
178 #warning John, look here. Doesnt make much sense.
179 if (buf.params().tracking_changes)
180 paragraphs.begin()->trackChanges();
182 // delete the initial paragraph
183 Paragraph oldpar = *paragraphs.begin();
185 ParagraphList::iterator pit = paragraphs.begin();
189 token = lex.getString();
192 if (token == "\\end_inset") {
196 if (token == "\\end_document") {
197 lex.printError("\\end_document read in inset! Error in document!");
202 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
205 pit = paragraphs.begin();
206 ParagraphList::iterator const end = paragraphs.end();
207 for (; pit != end; ++pit)
208 pit->setInsetOwner(this);
210 if (token != "\\end_inset") {
211 lex.printError("Missing \\end_inset at this point. "
216 // ensure we have at least one par.
217 if (paragraphs.empty())
218 paragraphs.push_back(oldpar);
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
224 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226 textwidth_ = mi.base.textwidth;
227 BufferView * bv = mi.base.bv;
229 text_.metrics(mi, dim);
230 dim.asc += TEXT_TO_INSET_OFFSET;
231 dim.des += TEXT_TO_INSET_OFFSET;
232 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233 dim.wid = max(dim.wid, 10);
238 int InsetText::textWidth() const
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
246 // update our idea of where we are. Clearly, we should
247 // not have to know this information.
250 int const start_x = x;
252 BufferView * bv = pi.base.bv;
253 Painter & pain = pi.pain;
255 // repaint the background if needed
256 if (backgroundColor() != LColor::background)
257 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
259 // no draw is necessary !!!
260 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
271 top_y = y - dim_.asc;
273 if (the_locking_inset
274 && text_.cursor.par() == inset_par && cpos() == inset_pos) {
279 x += TEXT_TO_INSET_OFFSET;
281 paintTextInset(*bv, text_, x, y);
283 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
284 drawFrame(pain, start_x);
288 void InsetText::drawFrame(Painter & pain, int x) const
290 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
291 int const frame_x = x + ttoD2;
292 int const frame_y = top_baseline - dim_.asc + ttoD2;
293 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
294 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
295 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
299 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
304 if (!autoBreakRows_ && paragraphs.size() > 1)
305 collapseParagraphs(bv);
307 if (!text_.selection.set())
308 text_.selection.cursor = text_.cursor;
311 bv->updateInset(this);
312 bv->owner()->view_state_changed();
313 bv->owner()->updateMenubar();
314 bv->owner()->updateToolbar();
315 if (old_par != text_.cursor.par()) {
316 bv->owner()->setLayout(cpar()->layout()->name());
317 old_par = text_.cursor.par();
322 string const InsetText::editMessage() const
324 return _("Opened Text Inset");
328 void InsetText::insetUnlock(BufferView * bv)
330 if (the_locking_inset) {
331 the_locking_inset->insetUnlock(bv);
332 the_locking_inset = 0;
333 updateLocal(bv, false);
338 if (text_.selection.set())
339 text_.clearSelection();
341 bv->owner()->setLayout(owner()->getLyXText(bv)
342 ->cursorPar()->layout()->name());
344 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
345 // hack for deleteEmptyParMech
346 if (!paragraphs.begin()->empty())
347 text_.setCursor(0, 0);
348 else if (paragraphs.size() > 1)
349 text_.setCursor(1, 0);
353 void InsetText::lockInset(BufferView * bv)
356 the_locking_inset = 0;
357 inset_pos = inset_x = inset_y = 0;
358 inset_boundary = false;
361 text_.setCursorIntern(0, 0);
362 text_.clearSelection();
364 // If the inset is empty set the language of the current font to the
365 // language to the surronding text (if different).
366 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
367 bv->getParentLanguage(this) != text_.current_font.language()) {
368 LyXFont font(LyXFont::ALL_IGNORE);
369 font.setLanguage(bv->getParentLanguage(this));
370 setFont(bv, font, false);
375 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
377 the_locking_inset = inset;
378 inset_x = cx() - top_x;
381 inset_par = text_.cursor.par();
382 inset_boundary = cboundary();
386 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
388 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
389 << inset << "): " << endl;
392 if (!the_locking_inset) {
393 ParagraphList::iterator pit = paragraphs.begin();
394 ParagraphList::iterator pend = paragraphs.end();
396 for (; pit != pend; ++pit) {
397 InsetList::iterator it = pit->insetlist.begin();
398 InsetList::iterator const end = pit->insetlist.end();
399 for (; it != end; ++it) {
400 if (it->inset == inset) {
401 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
402 text_.setCursorIntern(
403 std::distance(paragraphs.begin(), pit), it->pos);
404 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
405 lyxerr << "bv: " << bv << " inset: " << inset << endl;
406 lockInset(bv, inset);
407 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
412 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
415 if (inset == cpar()->getInset(cpos())) {
416 lyxerr[Debug::INSETS] << "OK" << endl;
417 lockInset(bv, inset);
421 if (the_locking_inset && the_locking_inset == inset) {
422 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
423 lyxerr[Debug::INSETS] << "OK" << endl;
424 inset_x = cx() - top_x;
427 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
429 } else if (the_locking_inset) {
430 lyxerr[Debug::INSETS] << "MAYBE" << endl;
431 return the_locking_inset->lockInsetInInset(bv, inset);
433 lyxerr[Debug::INSETS] << "NOT OK" << endl;
438 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
441 if (!the_locking_inset)
443 if (the_locking_inset == inset) {
444 the_locking_inset->insetUnlock(bv);
445 the_locking_inset = 0;
447 moveRightIntern(bv, true, false);
448 old_par = -1; // force layout setting
452 updateLocal(bv, false);
455 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
459 void InsetText::lfunMousePress(FuncRequest const & cmd)
463 // use this to check mouse motion for selection!
467 BufferView * bv = cmd.view();
468 FuncRequest cmd1 = cmd;
475 int tmp_y = cmd.y + dim_.asc - bv->top_y();
476 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
478 if (the_locking_inset) {
479 if (the_locking_inset == inset) {
480 the_locking_inset->localDispatch(cmd1);
483 // otherwise only unlock the_locking_inset
484 the_locking_inset->insetUnlock(bv);
485 the_locking_inset = 0;
488 no_selection = false;
490 if (bv->theLockingInset()) {
491 if (isHighlyEditableInset(inset)) {
492 // We just have to lock the inset before calling a
494 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
495 if (!bv->lockInset(uinset)) {
496 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
498 inset->localDispatch(cmd1);
499 if (the_locking_inset)
500 updateLocal(bv, false);
505 bool paste_internally = false;
506 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
507 localDispatch(FuncRequest(bv, LFUN_COPY));
508 paste_internally = true;
510 int old_top_y = bv->top_y();
512 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
513 // set the selection cursor!
514 text_.selection.cursor = text_.cursor;
515 text_.cursor.x_fix(text_.cursor.x());
517 text_.clearSelection();
518 updateLocal(bv, false);
520 bv->owner()->setLayout(cpar()->layout()->name());
522 // we moved the view we cannot do mouse selection in this case!
523 if (bv->top_y() != old_top_y)
525 old_par = text_.cursor.par();
526 // Insert primary selection with middle mouse
527 // if there is a local selection in the current buffer,
529 if (cmd.button() == mouse_button::button2) {
530 if (paste_internally)
531 localDispatch(FuncRequest(bv, LFUN_PASTE));
533 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
536 getLyXText(bv)->clearSelection();
541 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
543 BufferView * bv = cmd.view();
544 FuncRequest cmd1 = cmd;
549 if (the_locking_inset)
550 return the_locking_inset->localDispatch(cmd1);
553 int tmp_y = cmd.y + dim_.asc - bv->top_y();
554 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
558 // We still need to deal properly with the whole relative vs.
559 // absolute mouse co-ords thing in a realiable, sensible way
560 bool ret = inset->localDispatch(cmd1);
561 updateLocal(bv, false);
566 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
568 FuncRequest cmd1 = cmd;
572 if (the_locking_inset) {
573 the_locking_inset->localDispatch(cmd1);
577 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
580 BufferView * bv = cmd.view();
581 LyXCursor cur = text_.cursor;
582 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
583 text_.cursor.x_fix(text_.cursor.x());
584 if (cur == text_.cursor)
586 text_.setSelection();
587 updateLocal(bv, false);
591 dispatch_result InsetText::localDispatch(FuncRequest const & cmd)
593 BufferView * bv = cmd.view();
596 switch (cmd.action) {
597 case LFUN_INSET_EDIT: {
598 UpdatableInset::localDispatch(cmd);
600 if (!bv->lockInset(this)) {
601 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
606 the_locking_inset = 0;
610 inset_boundary = false;
615 if (cmd.argument.size()) {
616 if (cmd.argument == "left")
617 text_.setCursorIntern(0, 0);
619 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
621 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
622 // we put here -1 and not button as now the button in the
623 // edit call should not be needed we will fix this in 1.3.x
624 // cycle hopefully (Jug 20020509)
625 // FIXME: GUII I've changed this to none: probably WRONG
626 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
627 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
628 text_.cursor.x(text_.cursor.x());
629 text_.cursor.x_fix(text_.cursor.x());
633 text_.clearSelection();
636 // If the inset is empty set the language of the current font to the
637 // language to the surronding text (if different).
638 if (paragraphs.begin()->empty() &&
639 paragraphs.size() == 1 &&
640 bv->getParentLanguage(this) != text_.current_font.language())
642 LyXFont font(LyXFont::ALL_IGNORE);
643 font.setLanguage(bv->getParentLanguage(this));
644 setFont(bv, font, false);
647 updateLocal(bv, false);
648 // Tell the paragraph dialog that we've entered an insettext.
649 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
653 case LFUN_MOUSE_PRESS:
657 case LFUN_MOUSE_MOTION:
658 lfunMouseMotion(cmd);
661 case LFUN_MOUSE_RELEASE:
662 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
668 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
669 no_selection = false;
671 dispatch_result result = UpdatableInset::localDispatch(cmd);
672 if (result != UNDISPATCHED)
676 if (cmd.action < 0 && cmd.argument.empty())
679 if (the_locking_inset) {
680 result = the_locking_inset->localDispatch(cmd);
681 if (result == DISPATCHED_NOUPDATE)
683 if (result == DISPATCHED) {
684 updateLocal(bv, false);
687 if (result >= FINISHED) {
690 moveRightIntern(bv, false, false);
695 if (result >= FINISHED) {
696 updateLocal(bv, false);
697 bv->unlockInset(this);
701 result = moveDown(bv);
702 if (result >= FINISHED) {
703 updateLocal(bv, false);
704 bv->unlockInset(this);
711 the_locking_inset = 0;
712 updateLocal(bv, false);
713 // make sure status gets reset immediately
714 bv->owner()->clearMessage();
718 bool updflag = false;
720 switch (cmd.action) {
723 case LFUN_SELFINSERT:
724 if (bv->buffer()->isReadonly()) {
725 // setErrorMessage(N_("Document is read only"));
728 if (!cmd.argument.empty()) {
729 /* Automatically delete the currently selected
730 * text and replace it with what is being
731 * typed in now. Depends on lyxrc settings
732 * "auto_region_delete", which defaults to
735 // This should not be needed here and is also WRONG!
736 recordUndo(bv, Undo::INSERT, text_.cursorPar());
740 if (lyxrc.auto_region_delete && text_.selection.set())
741 text_.cutSelection(false, false);
742 text_.clearSelection();
744 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
745 bv->owner()->getIntl().getTransManager().
746 TranslateAndInsert(cmd.argument[i], &text_);
748 text_.selection.cursor = text_.cursor;
750 result = DISPATCHED_NOUPDATE;
753 // cursor movements that need special handling
756 result = moveRight(bv);
761 result = moveLeft(bv);
765 result = moveDown(bv);
773 if (crow() == text_.firstRow())
774 result = FINISHED_UP;
776 text_.cursorPrevious();
777 text_.clearSelection();
778 result = DISPATCHED_NOUPDATE;
783 if (crow() == text_.lastRow())
784 result = FINISHED_DOWN;
787 text_.clearSelection();
788 result = DISPATCHED_NOUPDATE;
793 if (text_.selection.set())
794 text_.cutSelection(true, false);
801 if (text_.selection.set())
802 text_.cutSelection(true, false);
809 if (!autoBreakRows_) {
810 if (CutAndPaste::nrOfParagraphs() > 1) {
812 #warning FIXME horrendously bad UI
814 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
817 replaceSelection(bv->getLyXText());
818 size_t sel_index = 0;
819 string const & arg = cmd.argument;
820 if (isStrUnsignedInt(arg)) {
821 #warning FIXME Check if the arg is in the domain of available selections.
822 sel_index = strToUnsignedInt(arg);
824 text_.pasteSelection(sel_index);
826 text_.clearSelection();
831 case LFUN_BREAKPARAGRAPH:
832 if (!autoBreakRows_) {
835 replaceSelection(bv->getLyXText());
836 text_.breakParagraph(paragraphs, 0);
841 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
842 if (!autoBreakRows_) {
845 replaceSelection(bv->getLyXText());
846 text_.breakParagraph(paragraphs, 1);
851 case LFUN_BREAKLINE: {
852 if (!autoBreakRows_) {
855 replaceSelection(bv->getLyXText());
856 text_.insertInset(new InsetNewline);
863 // do not set layouts on non breakable textinsets
864 if (autoBreakRows_) {
865 string cur_layout = cpar()->layout()->name();
867 // Derive layout number from given argument (string)
868 // and current buffer's textclass (number).
869 LyXTextClass const & tclass =
870 bv->buffer()->params().getLyXTextClass();
871 string layout = cmd.argument;
872 bool hasLayout = tclass.hasLayout(layout);
874 // If the entry is obsolete, use the new one instead.
876 string const & obs = tclass[layout]->obsoleted_by();
881 // see if we found the layout number:
883 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
884 bv->owner()->dispatch(lf);
888 if (cur_layout != layout) {
890 text_.setLayout(layout);
891 bv->owner()->setLayout(cpar()->layout()->name());
895 // reset the layout box
896 bv->owner()->setLayout(cpar()->layout()->name());
901 if (!bv->dispatch(cmd))
902 result = UNDISPATCHED;
906 updateLocal(bv, updflag);
908 /// If the action has deleted all text in the inset, we need to change the
909 // language to the language of the surronding text.
910 if (!was_empty && paragraphs.begin()->empty() &&
911 paragraphs.size() == 1) {
912 LyXFont font(LyXFont::ALL_IGNORE);
913 font.setLanguage(bv->getParentLanguage(this));
914 setFont(bv, font, false);
917 if (result >= FINISHED)
918 bv->unlockInset(this);
920 if (result == DISPATCHED_NOUPDATE)
926 int InsetText::latex(Buffer const & buf, ostream & os,
927 LatexRunParams const & runparams) const
930 latexParagraphs(buf, paragraphs, os, texrow, runparams);
931 return texrow.rows();
935 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
937 unsigned int lines = 0;
939 ParagraphList::const_iterator beg = paragraphs.begin();
940 ParagraphList::const_iterator end = paragraphs.end();
941 ParagraphList::const_iterator it = beg;
942 for (; it != end; ++it) {
943 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
944 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
951 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
953 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
954 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
956 // There is a confusion between the empty paragraph and the default paragraph
957 // The default paragraph is <p></p>, the empty paragraph is *empty*
958 // Since none of the floats of linuxdoc accepts standard paragraphs
959 // I disable them. I don't expect problems. (jamatos 2003/07/27)
960 for (; pit != pend; ++pit) {
961 const string name = pit->layout()->latexname();
963 sgml::openTag(os, 1, 0, name);
964 buf.simpleLinuxDocOnePar(os, pit, 0);
966 sgml::closeTag(os, 1, 0, name);
972 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
974 unsigned int lines = 0;
976 vector<string> environment_stack(10);
977 vector<string> environment_inner(10);
979 int const command_depth = 0;
982 Paragraph::depth_type depth = 0; // paragraph depth
984 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
985 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
987 for (; pit != pend; ++pit) {
988 int desc_on = 0; // description mode
990 LyXLayout_ptr const & style = pit->layout();
992 // environment tag closing
993 for (; depth > pit->params().depth(); --depth) {
994 if (environment_inner[depth] != "!-- --") {
995 item_name = "listitem";
996 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
997 if (environment_inner[depth] == "varlistentry")
998 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1000 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1001 environment_stack[depth].erase();
1002 environment_inner[depth].erase();
1005 if (depth == pit->params().depth()
1006 && environment_stack[depth] != style->latexname()
1007 && !environment_stack[depth].empty()) {
1008 if (environment_inner[depth] != "!-- --") {
1009 item_name= "listitem";
1010 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1011 if (environment_inner[depth] == "varlistentry")
1012 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1015 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1017 environment_stack[depth].erase();
1018 environment_inner[depth].erase();
1021 // Write opening SGML tags.
1022 switch (style->latextype) {
1023 case LATEX_PARAGRAPH:
1024 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1028 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1032 case LATEX_ENVIRONMENT:
1033 case LATEX_ITEM_ENVIRONMENT:
1034 if (depth < pit->params().depth()) {
1035 depth = pit->params().depth();
1036 environment_stack[depth].erase();
1039 if (environment_stack[depth] != style->latexname()) {
1040 if (environment_stack.size() == depth + 1) {
1041 environment_stack.push_back("!-- --");
1042 environment_inner.push_back("!-- --");
1044 environment_stack[depth] = style->latexname();
1045 environment_inner[depth] = "!-- --";
1046 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1048 if (environment_inner[depth] != "!-- --") {
1049 item_name= "listitem";
1050 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1051 if (environment_inner[depth] == "varlistentry")
1052 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1056 if (style->latextype == LATEX_ENVIRONMENT) {
1057 if (!style->latexparam().empty()) {
1058 if (style->latexparam() == "CDATA")
1061 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1066 desc_on = (style->labeltype == LABEL_MANUAL);
1068 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1069 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1071 item_name = desc_on ? "term" : "para";
1072 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1076 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1080 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1083 // write closing SGML tags
1084 switch (style->latextype) {
1085 case LATEX_ENVIRONMENT:
1086 if (!style->latexparam().empty()) {
1087 if (style->latexparam() == "CDATA")
1090 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1093 case LATEX_ITEM_ENVIRONMENT:
1097 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1099 case LATEX_PARAGRAPH:
1100 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1103 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1109 for (int d = depth; d >= 0; --d) {
1110 if (!environment_stack[depth].empty()) {
1111 if (environment_inner[depth] != "!-- --") {
1112 item_name = "listitem";
1113 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1114 if (environment_inner[depth] == "varlistentry")
1115 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1118 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1126 void InsetText::validate(LaTeXFeatures & features) const
1128 for_each(paragraphs.begin(), paragraphs.end(),
1129 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1133 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1135 if (the_locking_inset) {
1136 the_locking_inset->getCursor(bv, x, y);
1140 y = cy() + InsetText::y();
1144 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1146 if (the_locking_inset) {
1147 the_locking_inset->getCursorPos(bv, x, y);
1150 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1151 y = cy() - TEXT_TO_INSET_OFFSET;
1155 int InsetText::insetInInsetY() const
1157 if (!the_locking_inset)
1160 return inset_y + the_locking_inset->insetInInsetY();
1164 void InsetText::fitInsetCursor(BufferView * bv) const
1166 if (the_locking_inset) {
1167 the_locking_inset->fitInsetCursor(bv);
1171 LyXFont const font = text_.getFont(cpar(), cpos());
1173 int const asc = font_metrics::maxAscent(font);
1174 int const desc = font_metrics::maxDescent(font);
1176 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1180 dispatch_result InsetText::moveRight(BufferView * bv)
1182 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1183 return moveLeftIntern(bv, false, true, false);
1185 return moveRightIntern(bv, true, true, false);
1189 dispatch_result InsetText::moveLeft(BufferView * bv)
1191 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1192 return moveRightIntern(bv, true, true, false);
1194 return moveLeftIntern(bv, false, true, false);
1199 InsetText::moveRightIntern(BufferView * bv, bool front,
1200 bool activate_inset, bool selecting)
1202 ParagraphList::iterator c_par = cpar();
1204 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1205 return FINISHED_RIGHT;
1206 if (activate_inset && checkAndActivateInset(bv, front))
1208 text_.cursorRight(bv);
1210 text_.clearSelection();
1211 return DISPATCHED_NOUPDATE;
1216 InsetText::moveLeftIntern(BufferView * bv, bool front,
1217 bool activate_inset, bool selecting)
1219 if (cpar() == paragraphs.begin() && cpos() <= 0)
1221 text_.cursorLeft(bv);
1223 text_.clearSelection();
1224 if (activate_inset && checkAndActivateInset(bv, front))
1226 return DISPATCHED_NOUPDATE;
1230 dispatch_result InsetText::moveUp(BufferView * bv)
1232 if (crow() == text_.firstRow())
1235 text_.clearSelection();
1236 return DISPATCHED_NOUPDATE;
1240 dispatch_result InsetText::moveDown(BufferView * bv)
1242 if (crow() == text_.lastRow())
1243 return FINISHED_DOWN;
1244 text_.cursorDown(bv);
1245 text_.clearSelection();
1246 return DISPATCHED_NOUPDATE;
1250 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1252 if (the_locking_inset) {
1253 if (the_locking_inset->insetAllowed(inset))
1254 return the_locking_inset->insertInset(bv, inset);
1257 inset->setOwner(this);
1258 text_.insertInset(inset);
1260 updateLocal(bv, true);
1265 bool InsetText::insetAllowed(InsetOld::Code code) const
1267 // in_insetAllowed is a really gross hack,
1268 // to allow us to call the owner's insetAllowed
1269 // without stack overflow, which can happen
1270 // when the owner uses InsetCollapsable::insetAllowed()
1272 if (in_insetAllowed)
1274 in_insetAllowed = true;
1275 if (the_locking_inset)
1276 ret = the_locking_inset->insetAllowed(code);
1278 ret = owner()->insetAllowed(code);
1279 in_insetAllowed = false;
1284 UpdatableInset * InsetText::getLockingInset() const
1286 return the_locking_inset ? the_locking_inset->getLockingInset() :
1287 const_cast<InsetText *>(this);
1291 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1295 if (the_locking_inset)
1296 return the_locking_inset->getFirstLockingInsetOfType(c);
1301 bool InsetText::showInsetDialog(BufferView * bv) const
1303 if (the_locking_inset)
1304 return the_locking_inset->showInsetDialog(bv);
1309 void InsetText::getLabelList(Buffer const & buffer,
1310 std::vector<string> & list) const
1312 ParagraphList::const_iterator pit = paragraphs.begin();
1313 ParagraphList::const_iterator pend = paragraphs.end();
1314 for (; pit != pend; ++pit) {
1315 InsetList::const_iterator beg = pit->insetlist.begin();
1316 InsetList::const_iterator end = pit->insetlist.end();
1317 for (; beg != end; ++beg)
1318 beg->inset->getLabelList(buffer, list);
1323 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1326 if (the_locking_inset) {
1327 the_locking_inset->setFont(bv, font, toggleall, selectall);
1331 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1332 || cpar()->empty()) {
1333 text_.setFont(font, toggleall);
1338 if (text_.selection.set())
1339 text_.recUndo(text_.cursor.par());
1343 text_.selection.cursor = text_.cursor;
1344 text_.cursorBottom();
1345 text_.setSelection();
1348 text_.toggleFree(font, toggleall);
1351 text_.clearSelection();
1354 updateLocal(bv, true);
1358 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1360 if (cpos() == cpar()->size())
1362 InsetOld * inset = cpar()->getInset(cpos());
1363 if (!isHighlyEditableInset(inset))
1365 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1366 inset->localDispatch(cmd);
1367 if (!the_locking_inset)
1369 updateLocal(bv, false);
1374 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1375 mouse_button::state button)
1378 int dummyy = y + dim_.asc;
1379 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1380 // we only do the edit() call if the inset was hit by the mouse
1381 // or if it is a highly editable inset. So we should call this
1382 // function from our own edit with button < 0.
1383 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1385 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1394 inset_x = cx() - top_x;
1396 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1397 inset->localDispatch(cmd);
1398 if (!the_locking_inset)
1400 updateLocal(bv, false);
1405 void InsetText::markNew(bool track_changes)
1407 ParagraphList::iterator pit = paragraphs.begin();
1408 ParagraphList::iterator end = paragraphs.end();
1409 for (; pit != end; ++pit) {
1410 if (track_changes) {
1411 pit->trackChanges();
1413 // no-op when not tracking
1414 pit->cleanChanges();
1420 void InsetText::setText(string const & data, LyXFont const & font)
1423 for (unsigned int i = 0; i < data.length(); ++i)
1424 paragraphs.begin()->insertChar(i, data[i], font);
1428 void InsetText::setAutoBreakRows(bool flag)
1430 if (flag != autoBreakRows_) {
1431 autoBreakRows_ = flag;
1438 void InsetText::setDrawFrame(DrawFrame how)
1444 LColor_color InsetText::frameColor() const
1446 return LColor::color(frame_color_);
1450 void InsetText::setFrameColor(LColor_color col)
1456 int InsetText::cx() const
1458 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1459 if (the_locking_inset) {
1460 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1461 if (font.isVisibleRightToLeft())
1462 x -= the_locking_inset->width();
1468 int InsetText::cy() const
1470 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1474 pos_type InsetText::cpos() const
1476 return text_.cursor.pos();
1480 ParagraphList::iterator InsetText::cpar() const
1482 return text_.cursorPar();
1486 bool InsetText::cboundary() const
1488 return text_.cursor.boundary();
1492 RowList::iterator InsetText::crow() const
1494 return text_.cursorRow();
1498 LyXText * InsetText::getLyXText(BufferView const * bv,
1499 bool const recursive) const
1502 if (recursive && the_locking_inset)
1503 return the_locking_inset->getLyXText(bv, true);
1508 void InsetText::setViewCache(BufferView const * bv) const
1511 if (bv != text_.bv_owner) {
1512 //lyxerr << "setting view cache from "
1513 // << text_.bv_owner << " to " << bv << "\n";
1514 text_.init(const_cast<BufferView *>(bv));
1516 text_.bv_owner = const_cast<BufferView *>(bv);
1521 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1524 /// then remove all LyXText in text-insets
1525 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1526 const_cast<ParagraphList&>(paragraphs).end(),
1527 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1532 void InsetText::removeNewlines()
1534 ParagraphList::iterator it = paragraphs.begin();
1535 ParagraphList::iterator end = paragraphs.end();
1536 for (; it != end; ++it)
1537 for (int i = 0; i < it->size(); ++i)
1538 if (it->isNewline(i))
1543 int InsetText::scroll(bool recursive) const
1545 int sx = UpdatableInset::scroll(false);
1547 if (recursive && the_locking_inset)
1548 sx += the_locking_inset->scroll(recursive);
1554 void InsetText::clearSelection(BufferView *)
1556 text_.clearSelection();
1560 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1562 Painter & pain = bv->painter();
1564 int h = dim_.asc + dim_.des;
1565 int ty = baseline - dim_.asc;
1571 if (ty + h > pain.paperHeight())
1572 h = pain.paperHeight();
1573 if (top_x + w > pain.paperWidth())
1574 w = pain.paperWidth();
1575 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1579 ParagraphList * InsetText::getParagraphs(int i) const
1581 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1585 LyXCursor const & InsetText::cursor(BufferView * bv) const
1587 if (the_locking_inset)
1588 return the_locking_inset->cursor(bv);
1589 return getLyXText(bv)->cursor;
1593 InsetOld * InsetText::getInsetFromID(int id_arg) const
1596 return const_cast<InsetText *>(this);
1598 ParagraphList::const_iterator pit = paragraphs.begin();
1599 ParagraphList::const_iterator pend = paragraphs.end();
1600 for (; pit != pend; ++pit) {
1601 InsetList::const_iterator it = pit->insetlist.begin();
1602 InsetList::const_iterator end = pit->insetlist.end();
1603 for (; it != end; ++it) {
1604 if (it->inset->id() == id_arg)
1606 InsetOld * in = it->inset->getInsetFromID(id_arg);
1616 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1619 if (the_locking_inset) {
1620 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1621 if (!word.word().empty()) {
1625 // we have to go on checking so move cursor to the next char
1626 text_.cursor.pos(text_.cursor.pos() + 1);
1628 word = text_.selectNextWordToSpellcheck(value);
1629 if (word.word().empty())
1630 bv->unlockInset(const_cast<InsetText *>(this));
1637 void InsetText::selectSelectedWord(BufferView * bv)
1639 if (the_locking_inset) {
1640 the_locking_inset->selectSelectedWord(bv);
1643 getLyXText(bv)->selectSelectedWord();
1644 updateLocal(bv, false);
1648 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1650 if (the_locking_inset) {
1651 if (the_locking_inset->nextChange(bv, length))
1653 text_.cursorRight(true);
1655 lyx::find::SearchResult result =
1656 lyx::find::findNextChange(bv, &text_, length);
1658 if (result == lyx::find::SR_FOUND) {
1659 LyXCursor cur = text_.cursor;
1660 bv->unlockInset(bv->theLockingInset());
1661 if (bv->lockInset(this))
1664 text_.setSelectionRange(length);
1665 updateLocal(bv, false);
1667 return result != lyx::find::SR_NOT_FOUND;
1671 bool InsetText::searchForward(BufferView * bv, string const & str,
1674 if (the_locking_inset) {
1675 if (the_locking_inset->searchForward(bv, str, cs, mw))
1677 text_.cursorRight(true);
1679 lyx::find::SearchResult result =
1680 lyx::find::find(bv, &text_, str, true, cs, mw);
1682 if (result == lyx::find::SR_FOUND) {
1683 LyXCursor cur = text_.cursor;
1684 bv->unlockInset(bv->theLockingInset());
1685 if (bv->lockInset(this))
1688 text_.setSelectionRange(str.length());
1689 updateLocal(bv, false);
1691 return result != lyx::find::SR_NOT_FOUND;
1695 bool InsetText::searchBackward(BufferView * bv, string const & str,
1698 if (the_locking_inset) {
1699 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1703 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1705 lyx::find::SearchResult result =
1706 lyx::find::find(bv, &text_, str, false, cs, mw);
1708 if (result == lyx::find::SR_FOUND) {
1709 LyXCursor cur = text_.cursor;
1710 bv->unlockInset(bv->theLockingInset());
1711 if (bv->lockInset(this))
1714 text_.setSelectionRange(str.length());
1715 updateLocal(bv, false);
1717 return result != lyx::find::SR_NOT_FOUND;
1721 bool InsetText::checkInsertChar(LyXFont & font)
1723 return owner() ? owner()->checkInsertChar(font) : true;
1727 void InsetText::collapseParagraphs(BufferView * bv)
1729 while (paragraphs.size() > 1) {
1730 ParagraphList::iterator const first = paragraphs.begin();
1731 ParagraphList::iterator second = first;
1733 size_t const first_par_size = first->size();
1735 if (!first->empty() &&
1737 !first->isSeparator(first_par_size - 1)) {
1738 first->insertChar(first_par_size, ' ');
1741 #warning probably broken
1742 if (text_.selection.set()) {
1743 if (text_.selection.start.par() == 1) {
1744 text_.selection.start.par(1);
1745 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1747 if (text_.selection.end.par() == 2) {
1748 text_.selection.end.par(1);
1749 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1753 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1758 void InsetText::getDrawFont(LyXFont & font) const
1762 owner()->getDrawFont(font);
1766 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1768 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1769 // And it probably does. You have to take a look at this John. (Lgb)
1770 #warning John, have a look here. (Lgb)
1771 ParagraphList::iterator pit = plist.begin();
1772 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1774 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1776 ParagraphList::iterator pend = plist.end();
1777 for (; pit != pend; ++pit)
1778 paragraphs.push_back(*pit);
1782 void InsetText::addPreview(PreviewLoader & loader) const
1784 ParagraphList::const_iterator pit = paragraphs.begin();
1785 ParagraphList::const_iterator pend = paragraphs.end();
1787 for (; pit != pend; ++pit) {
1788 InsetList::const_iterator it = pit->insetlist.begin();
1789 InsetList::const_iterator end = pit->insetlist.end();
1790 for (; it != end; ++it)
1791 it->inset->addPreview(loader);