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"
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;
225 textwidth_ = mi.base.textwidth - 30;
226 BufferView * bv = mi.base.bv;
228 text_.metrics(mi, dim);
229 dim.asc += TEXT_TO_INSET_OFFSET;
230 dim.des += TEXT_TO_INSET_OFFSET;
231 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
232 dim.wid = max(dim.wid, 10);
237 int InsetText::textWidth() const
243 void InsetText::draw(PainterInfo & pi, int x, int y) const
245 // update our idea of where we are. Clearly, we should
246 // not have to know this information.
249 int const start_x = x;
251 BufferView * bv = pi.base.bv;
252 Painter & pain = pi.pain;
254 // repaint the background if needed
255 if (backgroundColor() != LColor::background)
256 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
258 // no draw is necessary !!!
259 if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
270 top_y = y - dim_.asc;
272 if (the_locking_inset
273 && text_.cursor.par() == inset_par && cpos() == inset_pos) {
278 x += TEXT_TO_INSET_OFFSET;
280 paintTextInset(*bv, text_, x, y);
282 if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283 drawFrame(pain, start_x);
287 void InsetText::drawFrame(Painter & pain, int x) const
289 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290 int const frame_x = x + ttoD2;
291 int const frame_y = top_baseline - dim_.asc + ttoD2;
292 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
303 if (!autoBreakRows_ && paragraphs.size() > 1)
304 collapseParagraphs(bv);
306 if (!text_.selection.set())
307 text_.selection.cursor = text_.cursor;
310 bv->updateInset(this);
311 bv->owner()->view_state_changed();
312 bv->owner()->updateMenubar();
313 bv->owner()->updateToolbar();
314 if (old_par != text_.cursor.par()) {
315 bv->owner()->setLayout(cpar()->layout()->name());
316 old_par = text_.cursor.par();
321 string const InsetText::editMessage() const
323 return _("Opened Text Inset");
327 void InsetText::insetUnlock(BufferView * bv)
329 if (the_locking_inset) {
330 the_locking_inset->insetUnlock(bv);
331 the_locking_inset = 0;
332 updateLocal(bv, false);
337 if (text_.selection.set())
338 text_.clearSelection();
340 bv->owner()->setLayout(owner()->getLyXText(bv)
341 ->cursorPar()->layout()->name());
343 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
344 // hack for deleteEmptyParMech
345 if (!paragraphs.begin()->empty())
346 text_.setCursor(0, 0);
347 else if (paragraphs.size() > 1)
348 text_.setCursor(1, 0);
352 void InsetText::lockInset(BufferView * bv)
355 the_locking_inset = 0;
356 inset_pos = inset_x = inset_y = 0;
357 inset_boundary = false;
360 text_.setCursorIntern(0, 0);
361 text_.clearSelection();
363 // If the inset is empty set the language of the current font to the
364 // language to the surronding text (if different).
365 if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
366 bv->getParentLanguage(this) != text_.current_font.language()) {
367 LyXFont font(LyXFont::ALL_IGNORE);
368 font.setLanguage(bv->getParentLanguage(this));
369 setFont(bv, font, false);
374 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
376 the_locking_inset = inset;
377 inset_x = cx() - top_x;
380 inset_par = text_.cursor.par();
381 inset_boundary = cboundary();
385 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
387 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
388 << inset << "): " << endl;
391 if (!the_locking_inset) {
392 ParagraphList::iterator pit = paragraphs.begin();
393 ParagraphList::iterator pend = paragraphs.end();
395 for (; pit != pend; ++pit) {
396 InsetList::iterator it = pit->insetlist.begin();
397 InsetList::iterator const end = pit->insetlist.end();
398 for (; it != end; ++it) {
399 if (it->inset == inset) {
400 lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
401 text_.setCursorIntern(
402 std::distance(paragraphs.begin(), pit), it->pos);
403 lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
404 lyxerr << "bv: " << bv << " inset: " << inset << endl;
405 lockInset(bv, inset);
406 lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
411 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
414 if (inset == cpar()->getInset(cpos())) {
415 lyxerr[Debug::INSETS] << "OK" << endl;
416 lockInset(bv, inset);
420 if (the_locking_inset && the_locking_inset == inset) {
421 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
422 lyxerr[Debug::INSETS] << "OK" << endl;
423 inset_x = cx() - top_x;
426 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
428 } else if (the_locking_inset) {
429 lyxerr[Debug::INSETS] << "MAYBE" << endl;
430 return the_locking_inset->lockInsetInInset(bv, inset);
432 lyxerr[Debug::INSETS] << "NOT OK" << endl;
437 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
440 if (!the_locking_inset)
442 if (the_locking_inset == inset) {
443 the_locking_inset->insetUnlock(bv);
444 the_locking_inset = 0;
446 moveRightIntern(bv, true, false);
447 old_par = -1; // force layout setting
451 updateLocal(bv, false);
454 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
458 void InsetText::lfunMousePress(FuncRequest const & cmd)
462 // use this to check mouse motion for selection!
466 BufferView * bv = cmd.view();
467 FuncRequest cmd1 = cmd;
474 int tmp_y = cmd.y + dim_.asc - bv->top_y();
475 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
477 if (the_locking_inset) {
478 if (the_locking_inset == inset) {
479 the_locking_inset->dispatch(cmd1);
482 // otherwise only unlock the_locking_inset
483 the_locking_inset->insetUnlock(bv);
484 the_locking_inset = 0;
487 no_selection = false;
489 if (bv->theLockingInset()) {
490 if (isHighlyEditableInset(inset)) {
491 // We just have to lock the inset before calling a
493 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
494 if (!bv->lockInset(uinset)) {
495 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497 inset->dispatch(cmd1);
498 if (the_locking_inset)
499 updateLocal(bv, false);
504 bool paste_internally = false;
505 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
506 dispatch(FuncRequest(bv, LFUN_COPY));
507 paste_internally = true;
509 int old_top_y = bv->top_y();
511 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
512 // set the selection cursor!
513 text_.selection.cursor = text_.cursor;
514 text_.cursor.x_fix(text_.cursor.x());
516 text_.clearSelection();
517 updateLocal(bv, false);
519 bv->owner()->setLayout(cpar()->layout()->name());
521 // we moved the view we cannot do mouse selection in this case!
522 if (bv->top_y() != old_top_y)
524 old_par = text_.cursor.par();
525 // Insert primary selection with middle mouse
526 // if there is a local selection in the current buffer,
528 if (cmd.button() == mouse_button::button2) {
529 if (paste_internally)
530 dispatch(FuncRequest(bv, LFUN_PASTE));
532 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
535 getLyXText(bv)->clearSelection();
540 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
542 BufferView * bv = cmd.view();
543 FuncRequest cmd1 = cmd;
548 if (the_locking_inset)
549 return the_locking_inset->dispatch(cmd1);
552 int tmp_y = cmd.y + dim_.asc - bv->top_y();
553 InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
557 // We still need to deal properly with the whole relative vs.
558 // absolute mouse co-ords thing in a realiable, sensible way
559 bool ret = inset->dispatch(cmd1);
560 updateLocal(bv, false);
565 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
567 FuncRequest cmd1 = cmd;
571 if (the_locking_inset) {
572 the_locking_inset->dispatch(cmd1);
576 if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
579 BufferView * bv = cmd.view();
580 LyXCursor cur = text_.cursor;
581 text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
582 text_.cursor.x_fix(text_.cursor.x());
583 if (cur == text_.cursor)
585 text_.setSelection();
586 updateLocal(bv, false);
591 InsetText::priv_dispatch(FuncRequest const & cmd,
592 idx_type & idx, pos_type & pos)
594 BufferView * bv = cmd.view();
597 switch (cmd.action) {
598 case LFUN_INSET_EDIT: {
599 UpdatableInset::priv_dispatch(cmd, idx, pos);
601 if (!bv->lockInset(this)) {
602 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
607 the_locking_inset = 0;
611 inset_boundary = false;
616 if (cmd.argument.size()) {
617 if (cmd.argument == "left")
618 text_.setCursorIntern(0, 0);
620 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
622 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
623 // we put here -1 and not button as now the button in the
624 // edit call should not be needed we will fix this in 1.3.x
625 // cycle hopefully (Jug 20020509)
626 // FIXME: GUII I've changed this to none: probably WRONG
627 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
628 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
629 text_.cursor.x(text_.cursor.x());
630 text_.cursor.x_fix(text_.cursor.x());
634 text_.clearSelection();
637 // If the inset is empty set the language of the current font to the
638 // language to the surronding text (if different).
639 if (paragraphs.begin()->empty() &&
640 paragraphs.size() == 1 &&
641 bv->getParentLanguage(this) != text_.current_font.language())
643 LyXFont font(LyXFont::ALL_IGNORE);
644 font.setLanguage(bv->getParentLanguage(this));
645 setFont(bv, font, false);
648 updateLocal(bv, false);
649 // Tell the paragraph dialog that we've entered an insettext.
650 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
654 case LFUN_MOUSE_PRESS:
658 case LFUN_MOUSE_MOTION:
659 lfunMouseMotion(cmd);
662 case LFUN_MOUSE_RELEASE:
663 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
669 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
670 no_selection = false;
672 dispatch_result result = UpdatableInset::priv_dispatch(cmd, idx, pos);
673 if (result != UNDISPATCHED)
677 if (cmd.action < 0 && cmd.argument.empty())
680 if (the_locking_inset) {
681 result = the_locking_inset->dispatch(cmd);
682 if (result == DISPATCHED_NOUPDATE)
684 if (result == DISPATCHED) {
685 updateLocal(bv, false);
688 if (result >= FINISHED) {
691 moveRightIntern(bv, false, false);
696 if (result >= FINISHED) {
697 updateLocal(bv, false);
698 bv->unlockInset(this);
702 result = moveDown(bv);
703 if (result >= FINISHED) {
704 updateLocal(bv, false);
705 bv->unlockInset(this);
712 the_locking_inset = 0;
713 updateLocal(bv, false);
714 // make sure status gets reset immediately
715 bv->owner()->clearMessage();
719 bool updflag = false;
721 switch (cmd.action) {
724 case LFUN_SELFINSERT:
725 if (bv->buffer()->isReadonly()) {
726 // setErrorMessage(N_("Document is read only"));
729 if (!cmd.argument.empty()) {
730 /* Automatically delete the currently selected
731 * text and replace it with what is being
732 * typed in now. Depends on lyxrc settings
733 * "auto_region_delete", which defaults to
736 // This should not be needed here and is also WRONG!
737 recordUndo(bv, Undo::INSERT, text_.cursorPar());
741 if (lyxrc.auto_region_delete && text_.selection.set())
742 text_.cutSelection(false, false);
743 text_.clearSelection();
745 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
746 bv->owner()->getIntl().getTransManager().
747 TranslateAndInsert(cmd.argument[i], &text_);
749 text_.selection.cursor = text_.cursor;
751 result = DISPATCHED_NOUPDATE;
754 // cursor movements that need special handling
757 result = moveRight(bv);
762 result = moveLeft(bv);
766 result = moveDown(bv);
774 if (crow() == text_.firstRow())
775 result = FINISHED_UP;
777 text_.cursorPrevious();
778 text_.clearSelection();
779 result = DISPATCHED_NOUPDATE;
784 if (crow() == text_.lastRow())
785 result = FINISHED_DOWN;
788 text_.clearSelection();
789 result = DISPATCHED_NOUPDATE;
794 if (text_.selection.set())
795 text_.cutSelection(true, false);
802 if (text_.selection.set())
803 text_.cutSelection(true, false);
810 if (!autoBreakRows_) {
811 if (CutAndPaste::nrOfParagraphs() > 1) {
813 #warning FIXME horrendously bad UI
815 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
818 replaceSelection(bv->getLyXText());
819 size_t sel_index = 0;
820 string const & arg = cmd.argument;
821 if (isStrUnsignedInt(arg)) {
822 #warning FIXME Check if the arg is in the domain of available selections.
823 sel_index = strToUnsignedInt(arg);
825 text_.pasteSelection(sel_index);
827 text_.clearSelection();
832 case LFUN_BREAKPARAGRAPH:
833 if (!autoBreakRows_) {
836 replaceSelection(bv->getLyXText());
837 text_.breakParagraph(paragraphs, 0);
842 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
843 if (!autoBreakRows_) {
846 replaceSelection(bv->getLyXText());
847 text_.breakParagraph(paragraphs, 1);
852 case LFUN_BREAKLINE: {
853 if (!autoBreakRows_) {
856 replaceSelection(bv->getLyXText());
857 text_.insertInset(new InsetNewline);
864 // do not set layouts on non breakable textinsets
865 if (autoBreakRows_) {
866 string cur_layout = cpar()->layout()->name();
868 // Derive layout number from given argument (string)
869 // and current buffer's textclass (number).
870 LyXTextClass const & tclass =
871 bv->buffer()->params().getLyXTextClass();
872 string layout = cmd.argument;
873 bool hasLayout = tclass.hasLayout(layout);
875 // If the entry is obsolete, use the new one instead.
877 string const & obs = tclass[layout]->obsoleted_by();
882 // see if we found the layout number:
884 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
885 bv->owner()->dispatch(lf);
889 if (cur_layout != layout) {
891 text_.setLayout(layout);
892 bv->owner()->setLayout(cpar()->layout()->name());
896 // reset the layout box
897 bv->owner()->setLayout(cpar()->layout()->name());
902 if (!bv->dispatch(cmd))
903 result = UNDISPATCHED;
907 updateLocal(bv, updflag);
909 /// If the action has deleted all text in the inset, we need to change the
910 // language to the language of the surronding text.
911 if (!was_empty && paragraphs.begin()->empty() &&
912 paragraphs.size() == 1) {
913 LyXFont font(LyXFont::ALL_IGNORE);
914 font.setLanguage(bv->getParentLanguage(this));
915 setFont(bv, font, false);
918 if (result >= FINISHED)
919 bv->unlockInset(this);
921 if (result == DISPATCHED_NOUPDATE)
927 int InsetText::latex(Buffer const & buf, ostream & os,
928 LatexRunParams const & runparams) const
931 latexParagraphs(buf, paragraphs, os, texrow, runparams);
932 return texrow.rows();
936 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
938 unsigned int lines = 0;
940 ParagraphList::const_iterator beg = paragraphs.begin();
941 ParagraphList::const_iterator end = paragraphs.end();
942 ParagraphList::const_iterator it = beg;
943 for (; it != end; ++it) {
944 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
945 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
952 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
954 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
955 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
957 // There is a confusion between the empty paragraph and the default paragraph
958 // The default paragraph is <p></p>, the empty paragraph is *empty*
959 // Since none of the floats of linuxdoc accepts standard paragraphs
960 // I disable them. I don't expect problems. (jamatos 2003/07/27)
961 for (; pit != pend; ++pit) {
962 const string name = pit->layout()->latexname();
964 sgml::openTag(os, 1, 0, name);
965 buf.simpleLinuxDocOnePar(os, pit, 0);
967 sgml::closeTag(os, 1, 0, name);
973 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
975 unsigned int lines = 0;
977 vector<string> environment_stack(10);
978 vector<string> environment_inner(10);
980 int const command_depth = 0;
983 Paragraph::depth_type depth = 0; // paragraph depth
985 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
986 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
988 for (; pit != pend; ++pit) {
989 int desc_on = 0; // description mode
991 LyXLayout_ptr const & style = pit->layout();
993 // environment tag closing
994 for (; depth > pit->params().depth(); --depth) {
995 if (environment_inner[depth] != "!-- --") {
996 item_name = "listitem";
997 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
998 if (environment_inner[depth] == "varlistentry")
999 lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1001 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1002 environment_stack[depth].erase();
1003 environment_inner[depth].erase();
1006 if (depth == pit->params().depth()
1007 && environment_stack[depth] != style->latexname()
1008 && !environment_stack[depth].empty()) {
1009 if (environment_inner[depth] != "!-- --") {
1010 item_name= "listitem";
1011 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1012 if (environment_inner[depth] == "varlistentry")
1013 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1016 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1018 environment_stack[depth].erase();
1019 environment_inner[depth].erase();
1022 // Write opening SGML tags.
1023 switch (style->latextype) {
1024 case LATEX_PARAGRAPH:
1025 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1029 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1033 case LATEX_ENVIRONMENT:
1034 case LATEX_ITEM_ENVIRONMENT:
1035 if (depth < pit->params().depth()) {
1036 depth = pit->params().depth();
1037 environment_stack[depth].erase();
1040 if (environment_stack[depth] != style->latexname()) {
1041 if (environment_stack.size() == depth + 1) {
1042 environment_stack.push_back("!-- --");
1043 environment_inner.push_back("!-- --");
1045 environment_stack[depth] = style->latexname();
1046 environment_inner[depth] = "!-- --";
1047 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1049 if (environment_inner[depth] != "!-- --") {
1050 item_name= "listitem";
1051 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1052 if (environment_inner[depth] == "varlistentry")
1053 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1057 if (style->latextype == LATEX_ENVIRONMENT) {
1058 if (!style->latexparam().empty()) {
1059 if (style->latexparam() == "CDATA")
1062 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1067 desc_on = (style->labeltype == LABEL_MANUAL);
1069 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1070 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1072 item_name = desc_on ? "term" : "para";
1073 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1077 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1081 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1084 // write closing SGML tags
1085 switch (style->latextype) {
1086 case LATEX_ENVIRONMENT:
1087 if (!style->latexparam().empty()) {
1088 if (style->latexparam() == "CDATA")
1091 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1094 case LATEX_ITEM_ENVIRONMENT:
1098 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1100 case LATEX_PARAGRAPH:
1101 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1104 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1110 for (int d = depth; d >= 0; --d) {
1111 if (!environment_stack[depth].empty()) {
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]);
1119 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1127 void InsetText::validate(LaTeXFeatures & features) const
1129 for_each(paragraphs.begin(), paragraphs.end(),
1130 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1134 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1136 if (the_locking_inset) {
1137 the_locking_inset->getCursor(bv, x, y);
1141 y = cy() + InsetText::y();
1145 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1147 if (the_locking_inset) {
1148 the_locking_inset->getCursorPos(bv, x, y);
1151 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1152 y = cy() - TEXT_TO_INSET_OFFSET;
1156 int InsetText::insetInInsetY() const
1158 if (!the_locking_inset)
1161 return inset_y + the_locking_inset->insetInInsetY();
1165 void InsetText::fitInsetCursor(BufferView * bv) const
1167 if (the_locking_inset) {
1168 the_locking_inset->fitInsetCursor(bv);
1172 LyXFont const font = text_.getFont(cpar(), cpos());
1174 int const asc = font_metrics::maxAscent(font);
1175 int const desc = font_metrics::maxDescent(font);
1177 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1181 dispatch_result InsetText::moveRight(BufferView * bv)
1183 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1184 return moveLeftIntern(bv, false, true, false);
1186 return moveRightIntern(bv, true, true, false);
1190 dispatch_result InsetText::moveLeft(BufferView * bv)
1192 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1193 return moveRightIntern(bv, true, true, false);
1195 return moveLeftIntern(bv, false, true, false);
1200 InsetText::moveRightIntern(BufferView * bv, bool front,
1201 bool activate_inset, bool selecting)
1203 ParagraphList::iterator c_par = cpar();
1205 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1206 return FINISHED_RIGHT;
1207 if (activate_inset && checkAndActivateInset(bv, front))
1209 text_.cursorRight(bv);
1211 text_.clearSelection();
1212 return DISPATCHED_NOUPDATE;
1217 InsetText::moveLeftIntern(BufferView * bv, bool front,
1218 bool activate_inset, bool selecting)
1220 if (cpar() == paragraphs.begin() && cpos() <= 0)
1222 text_.cursorLeft(bv);
1224 text_.clearSelection();
1225 if (activate_inset && checkAndActivateInset(bv, front))
1227 return DISPATCHED_NOUPDATE;
1231 dispatch_result InsetText::moveUp(BufferView * bv)
1233 if (crow() == text_.firstRow())
1236 text_.clearSelection();
1237 return DISPATCHED_NOUPDATE;
1241 dispatch_result InsetText::moveDown(BufferView * bv)
1243 if (crow() == text_.lastRow())
1244 return FINISHED_DOWN;
1245 text_.cursorDown(bv);
1246 text_.clearSelection();
1247 return DISPATCHED_NOUPDATE;
1251 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1253 if (the_locking_inset) {
1254 if (the_locking_inset->insetAllowed(inset))
1255 return the_locking_inset->insertInset(bv, inset);
1258 inset->setOwner(this);
1259 text_.insertInset(inset);
1261 updateLocal(bv, true);
1266 bool InsetText::insetAllowed(InsetOld::Code code) const
1268 // in_insetAllowed is a really gross hack,
1269 // to allow us to call the owner's insetAllowed
1270 // without stack overflow, which can happen
1271 // when the owner uses InsetCollapsable::insetAllowed()
1273 if (in_insetAllowed)
1275 in_insetAllowed = true;
1276 if (the_locking_inset)
1277 ret = the_locking_inset->insetAllowed(code);
1279 ret = owner()->insetAllowed(code);
1280 in_insetAllowed = false;
1285 UpdatableInset * InsetText::getLockingInset() const
1287 return the_locking_inset ? the_locking_inset->getLockingInset() :
1288 const_cast<InsetText *>(this);
1292 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1296 if (the_locking_inset)
1297 return the_locking_inset->getFirstLockingInsetOfType(c);
1302 bool InsetText::showInsetDialog(BufferView * bv) const
1304 if (the_locking_inset)
1305 return the_locking_inset->showInsetDialog(bv);
1310 void InsetText::getLabelList(Buffer const & buffer,
1311 std::vector<string> & list) const
1313 ParagraphList::const_iterator pit = paragraphs.begin();
1314 ParagraphList::const_iterator pend = paragraphs.end();
1315 for (; pit != pend; ++pit) {
1316 InsetList::const_iterator beg = pit->insetlist.begin();
1317 InsetList::const_iterator end = pit->insetlist.end();
1318 for (; beg != end; ++beg)
1319 beg->inset->getLabelList(buffer, list);
1324 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1327 if (the_locking_inset) {
1328 the_locking_inset->setFont(bv, font, toggleall, selectall);
1332 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1333 || cpar()->empty()) {
1334 text_.setFont(font, toggleall);
1339 if (text_.selection.set())
1340 text_.recUndo(text_.cursor.par());
1344 text_.selection.cursor = text_.cursor;
1345 text_.cursorBottom();
1346 text_.setSelection();
1349 text_.toggleFree(font, toggleall);
1352 text_.clearSelection();
1355 updateLocal(bv, true);
1359 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1361 if (cpos() == cpar()->size())
1363 InsetOld * inset = cpar()->getInset(cpos());
1364 if (!isHighlyEditableInset(inset))
1366 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1367 inset->dispatch(cmd);
1368 if (!the_locking_inset)
1370 updateLocal(bv, false);
1375 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1376 mouse_button::state button)
1379 int dummyy = y + dim_.asc;
1380 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1381 // we only do the edit() call if the inset was hit by the mouse
1382 // or if it is a highly editable inset. So we should call this
1383 // function from our own edit with button < 0.
1384 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1386 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1395 inset_x = cx() - top_x;
1397 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1398 inset->dispatch(cmd);
1399 if (!the_locking_inset)
1401 updateLocal(bv, false);
1406 void InsetText::markNew(bool track_changes)
1408 ParagraphList::iterator pit = paragraphs.begin();
1409 ParagraphList::iterator end = paragraphs.end();
1410 for (; pit != end; ++pit) {
1411 if (track_changes) {
1412 pit->trackChanges();
1414 // no-op when not tracking
1415 pit->cleanChanges();
1421 void InsetText::setText(string const & data, LyXFont const & font)
1424 for (unsigned int i = 0; i < data.length(); ++i)
1425 paragraphs.begin()->insertChar(i, data[i], font);
1429 void InsetText::setAutoBreakRows(bool flag)
1431 if (flag != autoBreakRows_) {
1432 autoBreakRows_ = flag;
1439 void InsetText::setDrawFrame(DrawFrame how)
1445 LColor_color InsetText::frameColor() const
1447 return LColor::color(frame_color_);
1451 void InsetText::setFrameColor(LColor_color col)
1457 int InsetText::cx() const
1459 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1460 if (the_locking_inset) {
1461 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1462 if (font.isVisibleRightToLeft())
1463 x -= the_locking_inset->width();
1469 int InsetText::cy() const
1471 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1475 pos_type InsetText::cpos() const
1477 return text_.cursor.pos();
1481 ParagraphList::iterator InsetText::cpar() const
1483 return text_.cursorPar();
1487 bool InsetText::cboundary() const
1489 return text_.cursor.boundary();
1493 RowList::iterator InsetText::crow() const
1495 return cpar()->getRow(cpos());
1499 LyXText * InsetText::getLyXText(BufferView const * bv,
1500 bool const recursive) const
1503 if (recursive && the_locking_inset)
1504 return the_locking_inset->getLyXText(bv, true);
1509 void InsetText::setViewCache(BufferView const * bv) const
1512 if (bv != text_.bv_owner) {
1513 //lyxerr << "setting view cache from "
1514 // << text_.bv_owner << " to " << bv << "\n";
1515 text_.init(const_cast<BufferView *>(bv));
1517 text_.bv_owner = const_cast<BufferView *>(bv);
1522 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1525 /// then remove all LyXText in text-insets
1526 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1527 const_cast<ParagraphList&>(paragraphs).end(),
1528 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1533 void InsetText::removeNewlines()
1535 ParagraphList::iterator it = paragraphs.begin();
1536 ParagraphList::iterator end = paragraphs.end();
1537 for (; it != end; ++it)
1538 for (int i = 0; i < it->size(); ++i)
1539 if (it->isNewline(i))
1544 int InsetText::scroll(bool recursive) const
1546 int sx = UpdatableInset::scroll(false);
1548 if (recursive && the_locking_inset)
1549 sx += the_locking_inset->scroll(recursive);
1555 void InsetText::clearSelection(BufferView *)
1557 text_.clearSelection();
1561 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1563 Painter & pain = bv->painter();
1565 int h = dim_.asc + dim_.des;
1566 int ty = baseline - dim_.asc;
1572 if (ty + h > pain.paperHeight())
1573 h = pain.paperHeight();
1574 if (top_x + w > pain.paperWidth())
1575 w = pain.paperWidth();
1576 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1580 ParagraphList * InsetText::getParagraphs(int i) const
1582 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1586 LyXText * InsetText::getText(int i) const
1588 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1592 LyXCursor const & InsetText::cursor(BufferView * bv) const
1594 if (the_locking_inset)
1595 return the_locking_inset->cursor(bv);
1596 return getLyXText(bv)->cursor;
1600 InsetOld * InsetText::getInsetFromID(int id_arg) const
1603 return const_cast<InsetText *>(this);
1605 ParagraphList::const_iterator pit = paragraphs.begin();
1606 ParagraphList::const_iterator pend = paragraphs.end();
1607 for (; pit != pend; ++pit) {
1608 InsetList::const_iterator it = pit->insetlist.begin();
1609 InsetList::const_iterator end = pit->insetlist.end();
1610 for (; it != end; ++it) {
1611 if (it->inset->id() == id_arg)
1613 InsetOld * in = it->inset->getInsetFromID(id_arg);
1623 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1626 if (the_locking_inset) {
1627 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1628 if (!word.word().empty()) {
1632 // we have to go on checking so move cursor to the next char
1633 text_.cursor.pos(text_.cursor.pos() + 1);
1635 word = text_.selectNextWordToSpellcheck(value);
1636 if (word.word().empty())
1637 bv->unlockInset(const_cast<InsetText *>(this));
1644 void InsetText::selectSelectedWord(BufferView * bv)
1646 if (the_locking_inset) {
1647 the_locking_inset->selectSelectedWord(bv);
1650 getLyXText(bv)->selectSelectedWord();
1651 updateLocal(bv, false);
1655 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1657 if (the_locking_inset) {
1658 if (the_locking_inset->nextChange(bv, length))
1660 text_.cursorRight(true);
1662 lyx::find::SearchResult result =
1663 lyx::find::findNextChange(bv, &text_, length);
1665 if (result == lyx::find::SR_FOUND) {
1666 LyXCursor cur = text_.cursor;
1667 bv->unlockInset(bv->theLockingInset());
1668 if (bv->lockInset(this))
1671 text_.setSelectionRange(length);
1672 updateLocal(bv, false);
1674 return result != lyx::find::SR_NOT_FOUND;
1678 bool InsetText::searchForward(BufferView * bv, string const & str,
1681 if (the_locking_inset) {
1682 if (the_locking_inset->searchForward(bv, str, cs, mw))
1684 text_.cursorRight(true);
1686 lyx::find::SearchResult result =
1687 lyx::find::find(bv, &text_, str, true, cs, mw);
1689 if (result == lyx::find::SR_FOUND) {
1690 LyXCursor cur = text_.cursor;
1691 bv->unlockInset(bv->theLockingInset());
1692 if (bv->lockInset(this))
1695 text_.setSelectionRange(str.length());
1696 updateLocal(bv, false);
1698 return result != lyx::find::SR_NOT_FOUND;
1702 bool InsetText::searchBackward(BufferView * bv, string const & str,
1705 if (the_locking_inset) {
1706 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1710 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1712 lyx::find::SearchResult result =
1713 lyx::find::find(bv, &text_, str, false, cs, mw);
1715 if (result == lyx::find::SR_FOUND) {
1716 LyXCursor cur = text_.cursor;
1717 bv->unlockInset(bv->theLockingInset());
1718 if (bv->lockInset(this))
1721 text_.setSelectionRange(str.length());
1722 updateLocal(bv, false);
1724 return result != lyx::find::SR_NOT_FOUND;
1728 bool InsetText::checkInsertChar(LyXFont & font)
1730 return owner() ? owner()->checkInsertChar(font) : true;
1734 void InsetText::collapseParagraphs(BufferView * bv)
1736 while (paragraphs.size() > 1) {
1737 ParagraphList::iterator const first = paragraphs.begin();
1738 ParagraphList::iterator second = first;
1740 size_t const first_par_size = first->size();
1742 if (!first->empty() &&
1744 !first->isSeparator(first_par_size - 1)) {
1745 first->insertChar(first_par_size, ' ');
1748 #warning probably broken
1749 if (text_.selection.set()) {
1750 if (text_.selection.start.par() == 1) {
1751 text_.selection.start.par(1);
1752 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1754 if (text_.selection.end.par() == 2) {
1755 text_.selection.end.par(1);
1756 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1760 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1765 void InsetText::getDrawFont(LyXFont & font) const
1769 owner()->getDrawFont(font);
1773 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1775 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1776 // And it probably does. You have to take a look at this John. (Lgb)
1777 #warning John, have a look here. (Lgb)
1778 ParagraphList::iterator pit = plist.begin();
1779 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1781 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1783 ParagraphList::iterator pend = plist.end();
1784 for (; pit != pend; ++pit)
1785 paragraphs.push_back(*pit);
1789 void InsetText::addPreview(PreviewLoader & loader) const
1791 ParagraphList::const_iterator pit = paragraphs.begin();
1792 ParagraphList::const_iterator pend = paragraphs.end();
1794 for (; pit != pend; ++pit) {
1795 InsetList::const_iterator it = pit->insetlist.begin();
1796 InsetList::const_iterator end = pit->insetlist.end();
1797 for (; it != end; ++it)
1798 it->inset->addPreview(loader);