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 "dispatchresult.h"
22 #include "errorlist.h"
23 #include "funcrequest.h"
30 #include "metricsinfo.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "rowpainter.h"
38 #include "WordLangTuple.h"
40 #include "frontends/Alert.h"
41 #include "frontends/font_metrics.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/Painter.h"
45 #include "support/lyxalgo.h" // lyx::count
47 #include <boost/bind.hpp>
49 using bv_funcs::replaceSelection;
53 using lyx::graphics::PreviewLoader;
55 using lyx::support::isStrUnsignedInt;
56 using lyx::support::strToUnsignedInt;
67 InsetText::InsetText(BufferParams const & bp)
70 autoBreakRows_(false),
72 frame_color_(LColor::insetframe),
73 text_(0, this, true, paragraphs)
75 textwidth_ = 0; // broken
76 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
77 if (bp.tracking_changes)
78 paragraphs.begin()->trackChanges();
83 InsetText::InsetText(InsetText const & in)
85 text_(in.text_.bv_owner, this, true, paragraphs)
92 void InsetText::operator=(InsetText const & in)
94 UpdatableInset::operator=(in);
95 paragraphs = in.paragraphs;
96 autoBreakRows_ = in.autoBreakRows_;
97 drawFrame_ = in.drawFrame_;
98 frame_color_ = in.frame_color_;
99 textwidth_ = in.textwidth_;
100 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
105 void InsetText::init()
107 ParagraphList::iterator pit = paragraphs.begin();
108 ParagraphList::iterator end = paragraphs.end();
109 for (; pit != end; ++pit)
110 pit->setInsetOwner(this);
111 text_.paragraphs_ = ¶graphs;
120 the_locking_inset = 0;
122 in_insetAllowed = false;
128 void InsetText::clear(bool just_mark_erased)
130 if (just_mark_erased) {
131 ParagraphList::iterator it = paragraphs.begin();
132 ParagraphList::iterator end = paragraphs.end();
133 for (; it != end; ++it)
138 // This is a gross hack...
139 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
142 paragraphs.push_back(Paragraph());
143 paragraphs.begin()->setInsetOwner(this);
144 paragraphs.begin()->layout(old_layout);
148 auto_ptr<InsetBase> InsetText::clone() const
150 return auto_ptr<InsetBase>(new InsetText(*this));
154 void InsetText::write(Buffer const & buf, ostream & os) const
157 writeParagraphData(buf, os);
161 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
163 ParagraphList::const_iterator it = paragraphs.begin();
164 ParagraphList::const_iterator end = paragraphs.end();
165 Paragraph::depth_type dth = 0;
166 for (; it != end; ++it) {
167 it->write(buf, os, buf.params(), dth);
172 void InsetText::read(Buffer const & buf, LyXLex & lex)
175 Paragraph::depth_type depth = 0;
179 #warning John, look here. Doesnt make much sense.
180 if (buf.params().tracking_changes)
181 paragraphs.begin()->trackChanges();
183 // delete the initial paragraph
184 Paragraph oldpar = *paragraphs.begin();
186 ParagraphList::iterator pit = paragraphs.begin();
190 token = lex.getString();
193 if (token == "\\end_inset") {
197 if (token == "\\end_document") {
198 lex.printError("\\end_document read in inset! Error in document!");
203 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
206 pit = paragraphs.begin();
207 ParagraphList::iterator const end = paragraphs.end();
208 for (; pit != end; ++pit)
209 pit->setInsetOwner(this);
211 if (token != "\\end_inset") {
212 lex.printError("Missing \\end_inset at this point. "
217 // ensure we have at least one par.
218 if (paragraphs.empty())
219 paragraphs.push_back(oldpar);
223 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
225 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226 textwidth_ = mi.base.textwidth - 30;
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->dispatch(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->dispatch(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 dispatch(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 dispatch(FuncRequest(bv, LFUN_PASTE));
533 dispatch(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->dispatch(cmd1) >= DISPATCHED;
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->dispatch(cmd1) >= DISPATCHED;
561 updateLocal(bv, false);
566 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
568 FuncRequest cmd1 = cmd;
572 if (the_locking_inset) {
573 the_locking_inset->dispatch(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);
592 InsetText::priv_dispatch(FuncRequest const & cmd,
593 idx_type & idx, pos_type & pos)
595 BufferView * bv = cmd.view();
598 switch (cmd.action) {
599 case LFUN_INSET_EDIT: {
600 UpdatableInset::priv_dispatch(cmd, idx, pos);
602 if (!bv->lockInset(this)) {
603 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
608 the_locking_inset = 0;
612 inset_boundary = false;
617 if (cmd.argument.size()) {
618 if (cmd.argument == "left")
619 text_.setCursorIntern(0, 0);
621 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
623 int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
624 // we put here -1 and not button as now the button in the
625 // edit call should not be needed we will fix this in 1.3.x
626 // cycle hopefully (Jug 20020509)
627 // FIXME: GUII I've changed this to none: probably WRONG
628 if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
629 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
630 text_.cursor.x(text_.cursor.x());
631 text_.cursor.x_fix(text_.cursor.x());
635 text_.clearSelection();
638 // If the inset is empty set the language of the current font to the
639 // language to the surronding text (if different).
640 if (paragraphs.begin()->empty() &&
641 paragraphs.size() == 1 &&
642 bv->getParentLanguage(this) != text_.current_font.language())
644 LyXFont font(LyXFont::ALL_IGNORE);
645 font.setLanguage(bv->getParentLanguage(this));
646 setFont(bv, font, false);
649 updateLocal(bv, false);
650 // Tell the paragraph dialog that we've entered an insettext.
651 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
655 case LFUN_MOUSE_PRESS:
659 case LFUN_MOUSE_MOTION:
660 lfunMouseMotion(cmd);
663 case LFUN_MOUSE_RELEASE:
664 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
670 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
671 no_selection = false;
673 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
674 if (result != UNDISPATCHED)
678 if (cmd.action < 0 && cmd.argument.empty())
681 if (the_locking_inset) {
682 result = the_locking_inset->dispatch(cmd);
683 if (result == DISPATCHED_NOUPDATE)
685 if (result == DISPATCHED) {
686 updateLocal(bv, false);
689 if (result >= FINISHED) {
690 switch (result.val()) {
692 moveRightIntern(bv, false, false);
697 if (result >= FINISHED) {
698 updateLocal(bv, false);
699 bv->unlockInset(this);
703 result = moveDown(bv);
704 if (result >= FINISHED) {
705 updateLocal(bv, false);
706 bv->unlockInset(this);
713 the_locking_inset = 0;
714 updateLocal(bv, false);
715 // make sure status gets reset immediately
716 bv->owner()->clearMessage();
720 bool updflag = false;
722 switch (cmd.action) {
725 case LFUN_SELFINSERT:
726 if (bv->buffer()->isReadonly()) {
727 // setErrorMessage(N_("Document is read only"));
730 if (!cmd.argument.empty()) {
731 /* Automatically delete the currently selected
732 * text and replace it with what is being
733 * typed in now. Depends on lyxrc settings
734 * "auto_region_delete", which defaults to
737 // This should not be needed here and is also WRONG!
738 recordUndo(bv, Undo::INSERT, text_.cursorPar());
742 if (lyxrc.auto_region_delete && text_.selection.set())
743 text_.cutSelection(false, false);
744 text_.clearSelection();
746 for (string::size_type i = 0; i < cmd.argument.length(); ++i)
747 bv->owner()->getIntl().getTransManager().
748 TranslateAndInsert(cmd.argument[i], &text_);
750 text_.selection.cursor = text_.cursor;
752 result = DISPATCHED_NOUPDATE;
755 // cursor movements that need special handling
758 result = moveRight(bv);
763 result = moveLeft(bv);
767 result = moveDown(bv);
775 if (crow() == text_.firstRow())
776 result = FINISHED_UP;
778 text_.cursorPrevious();
779 text_.clearSelection();
780 result = DISPATCHED_NOUPDATE;
785 if (crow() == text_.lastRow())
786 result = FINISHED_DOWN;
789 text_.clearSelection();
790 result = DISPATCHED_NOUPDATE;
795 if (text_.selection.set())
796 text_.cutSelection(true, false);
803 if (text_.selection.set())
804 text_.cutSelection(true, false);
811 if (!autoBreakRows_) {
812 if (CutAndPaste::nrOfParagraphs() > 1) {
814 #warning FIXME horrendously bad UI
816 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
819 replaceSelection(bv->getLyXText());
820 size_t sel_index = 0;
821 string const & arg = cmd.argument;
822 if (isStrUnsignedInt(arg)) {
823 #warning FIXME Check if the arg is in the domain of available selections.
824 sel_index = strToUnsignedInt(arg);
826 text_.pasteSelection(sel_index);
828 text_.clearSelection();
833 case LFUN_BREAKPARAGRAPH:
834 if (!autoBreakRows_) {
837 replaceSelection(bv->getLyXText());
838 text_.breakParagraph(paragraphs, 0);
843 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
844 if (!autoBreakRows_) {
847 replaceSelection(bv->getLyXText());
848 text_.breakParagraph(paragraphs, 1);
853 case LFUN_BREAKLINE: {
854 if (!autoBreakRows_) {
857 replaceSelection(bv->getLyXText());
858 text_.insertInset(new InsetNewline);
865 // do not set layouts on non breakable textinsets
866 if (autoBreakRows_) {
867 string cur_layout = cpar()->layout()->name();
869 // Derive layout number from given argument (string)
870 // and current buffer's textclass (number).
871 LyXTextClass const & tclass =
872 bv->buffer()->params().getLyXTextClass();
873 string layout = cmd.argument;
874 bool hasLayout = tclass.hasLayout(layout);
876 // If the entry is obsolete, use the new one instead.
878 string const & obs = tclass[layout]->obsoleted_by();
883 // see if we found the layout number:
885 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
886 bv->owner()->dispatch(lf);
890 if (cur_layout != layout) {
892 text_.setLayout(layout);
893 bv->owner()->setLayout(cpar()->layout()->name());
897 // reset the layout box
898 bv->owner()->setLayout(cpar()->layout()->name());
903 if (!bv->dispatch(cmd))
904 result = UNDISPATCHED;
908 updateLocal(bv, updflag);
910 /// If the action has deleted all text in the inset, we need to change the
911 // language to the language of the surronding text.
912 if (!was_empty && paragraphs.begin()->empty() &&
913 paragraphs.size() == 1) {
914 LyXFont font(LyXFont::ALL_IGNORE);
915 font.setLanguage(bv->getParentLanguage(this));
916 setFont(bv, font, false);
919 if (result >= FINISHED)
920 bv->unlockInset(this);
922 if (result == DISPATCHED_NOUPDATE)
928 int InsetText::latex(Buffer const & buf, ostream & os,
929 LatexRunParams const & runparams) const
932 latexParagraphs(buf, paragraphs, os, texrow, runparams);
933 return texrow.rows();
937 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
939 unsigned int lines = 0;
941 ParagraphList::const_iterator beg = paragraphs.begin();
942 ParagraphList::const_iterator end = paragraphs.end();
943 ParagraphList::const_iterator it = beg;
944 for (; it != end; ++it) {
945 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
946 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
953 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
955 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
956 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
958 // There is a confusion between the empty paragraph and the default paragraph
959 // The default paragraph is <p></p>, the empty paragraph is *empty*
960 // Since none of the floats of linuxdoc accepts standard paragraphs
961 // I disable them. I don't expect problems. (jamatos 2003/07/27)
962 for (; pit != pend; ++pit) {
963 const string name = pit->layout()->latexname();
965 sgml::openTag(os, 1, 0, name);
966 buf.simpleLinuxDocOnePar(os, pit, 0);
968 sgml::closeTag(os, 1, 0, name);
974 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
976 unsigned int lines = 0;
978 vector<string> environment_stack(10);
979 vector<string> environment_inner(10);
981 int const command_depth = 0;
984 Paragraph::depth_type depth = 0; // paragraph depth
986 ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
987 ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
989 for (; pit != pend; ++pit) {
990 int desc_on = 0; // description mode
992 LyXLayout_ptr const & style = pit->layout();
994 // environment tag closing
995 for (; depth > pit->params().depth(); --depth) {
996 lines += sgml::closeEnvTags(os, mixcont, environment_inner[depth],
997 command_depth + depth);
998 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
999 environment_stack[depth].erase();
1000 environment_inner[depth].erase();
1003 if (depth == pit->params().depth()
1004 && environment_stack[depth] != style->latexname()
1005 && !environment_stack[depth].empty()) {
1006 lines += sgml::closeEnvTags(os, mixcont, environment_inner[depth],
1007 command_depth + depth);
1008 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1010 environment_stack[depth].erase();
1011 environment_inner[depth].erase();
1014 // Write opening SGML tags.
1015 switch (style->latextype) {
1016 case LATEX_PARAGRAPH:
1017 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1021 buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1025 case LATEX_ENVIRONMENT:
1026 case LATEX_ITEM_ENVIRONMENT:
1027 if (depth < pit->params().depth()) {
1028 depth = pit->params().depth();
1029 environment_stack[depth].erase();
1032 if (environment_stack[depth] != style->latexname()) {
1033 if (environment_stack.size() == depth + 1) {
1034 environment_stack.push_back("!-- --");
1035 environment_inner.push_back("!-- --");
1037 environment_stack[depth] = style->latexname();
1038 environment_inner[depth] = "!-- --";
1039 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1041 lines += sgml::closeEnvTags(os, mixcont, environment_inner[depth],
1042 command_depth + depth);
1045 if (style->latextype == LATEX_ENVIRONMENT) {
1046 if (!style->latexparam().empty()) {
1047 if (style->latexparam() == "CDATA")
1050 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1055 desc_on = (style->labeltype == LABEL_MANUAL);
1057 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1058 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1060 item_name = desc_on ? "term" : "para";
1061 lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1065 lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1069 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1072 // write closing SGML tags
1073 switch (style->latextype) {
1074 case LATEX_ENVIRONMENT:
1075 if (!style->latexparam().empty()) {
1076 if (style->latexparam() == "CDATA")
1079 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1082 case LATEX_ITEM_ENVIRONMENT:
1086 lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1088 case LATEX_PARAGRAPH:
1089 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1092 lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1098 for (int d = depth; d >= 0; --d) {
1099 if (!environment_stack[depth].empty()) {
1100 lines += sgml::closeEnvTags(os, mixcont, environment_inner[depth],
1101 command_depth + depth);
1102 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1110 void InsetText::validate(LaTeXFeatures & features) const
1112 for_each(paragraphs.begin(), paragraphs.end(),
1113 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1117 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1119 if (the_locking_inset) {
1120 the_locking_inset->getCursor(bv, x, y);
1124 y = cy() + InsetText::y();
1128 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1130 if (the_locking_inset) {
1131 the_locking_inset->getCursorPos(bv, x, y);
1134 x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1135 y = cy() - TEXT_TO_INSET_OFFSET;
1139 int InsetText::insetInInsetY() const
1141 if (!the_locking_inset)
1144 return inset_y + the_locking_inset->insetInInsetY();
1148 void InsetText::fitInsetCursor(BufferView * bv) const
1150 if (the_locking_inset) {
1151 the_locking_inset->fitInsetCursor(bv);
1155 LyXFont const font = text_.getFont(cpar(), cpos());
1157 int const asc = font_metrics::maxAscent(font);
1158 int const desc = font_metrics::maxDescent(font);
1160 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1164 DispatchResult InsetText::moveRight(BufferView * bv)
1166 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1167 return moveLeftIntern(bv, false, true, false);
1169 return moveRightIntern(bv, true, true, false);
1173 DispatchResult InsetText::moveLeft(BufferView * bv)
1175 if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1176 return moveRightIntern(bv, true, true, false);
1178 return moveLeftIntern(bv, false, true, false);
1183 InsetText::moveRightIntern(BufferView * bv, bool front,
1184 bool activate_inset, bool selecting)
1186 ParagraphList::iterator c_par = cpar();
1188 if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1189 return FINISHED_RIGHT;
1190 if (activate_inset && checkAndActivateInset(bv, front))
1192 text_.cursorRight(bv);
1194 text_.clearSelection();
1195 return DISPATCHED_NOUPDATE;
1200 InsetText::moveLeftIntern(BufferView * bv, bool front,
1201 bool activate_inset, bool selecting)
1203 if (cpar() == paragraphs.begin() && cpos() <= 0)
1205 text_.cursorLeft(bv);
1207 text_.clearSelection();
1208 if (activate_inset && checkAndActivateInset(bv, front))
1210 return DISPATCHED_NOUPDATE;
1214 DispatchResult InsetText::moveUp(BufferView * bv)
1216 if (crow() == text_.firstRow())
1219 text_.clearSelection();
1220 return DISPATCHED_NOUPDATE;
1224 DispatchResult InsetText::moveDown(BufferView * bv)
1226 if (crow() == text_.lastRow())
1227 return FINISHED_DOWN;
1228 text_.cursorDown(bv);
1229 text_.clearSelection();
1230 return DISPATCHED_NOUPDATE;
1234 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1236 if (the_locking_inset) {
1237 if (the_locking_inset->insetAllowed(inset))
1238 return the_locking_inset->insertInset(bv, inset);
1241 inset->setOwner(this);
1242 text_.insertInset(inset);
1244 updateLocal(bv, true);
1249 bool InsetText::insetAllowed(InsetOld::Code code) const
1251 // in_insetAllowed is a really gross hack,
1252 // to allow us to call the owner's insetAllowed
1253 // without stack overflow, which can happen
1254 // when the owner uses InsetCollapsable::insetAllowed()
1256 if (in_insetAllowed)
1258 in_insetAllowed = true;
1259 if (the_locking_inset)
1260 ret = the_locking_inset->insetAllowed(code);
1262 ret = owner()->insetAllowed(code);
1263 in_insetAllowed = false;
1268 UpdatableInset * InsetText::getLockingInset() const
1270 return the_locking_inset ? the_locking_inset->getLockingInset() :
1271 const_cast<InsetText *>(this);
1275 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1279 if (the_locking_inset)
1280 return the_locking_inset->getFirstLockingInsetOfType(c);
1285 bool InsetText::showInsetDialog(BufferView * bv) const
1287 if (the_locking_inset)
1288 return the_locking_inset->showInsetDialog(bv);
1293 void InsetText::getLabelList(Buffer const & buffer,
1294 std::vector<string> & list) const
1296 ParagraphList::const_iterator pit = paragraphs.begin();
1297 ParagraphList::const_iterator pend = paragraphs.end();
1298 for (; pit != pend; ++pit) {
1299 InsetList::const_iterator beg = pit->insetlist.begin();
1300 InsetList::const_iterator end = pit->insetlist.end();
1301 for (; beg != end; ++beg)
1302 beg->inset->getLabelList(buffer, list);
1307 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1310 if (the_locking_inset) {
1311 the_locking_inset->setFont(bv, font, toggleall, selectall);
1315 if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1316 || cpar()->empty()) {
1317 text_.setFont(font, toggleall);
1322 if (text_.selection.set())
1323 text_.recUndo(text_.cursor.par());
1327 text_.selection.cursor = text_.cursor;
1328 text_.cursorBottom();
1329 text_.setSelection();
1332 text_.toggleFree(font, toggleall);
1335 text_.clearSelection();
1338 updateLocal(bv, true);
1342 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1344 if (cpos() == cpar()->size())
1346 InsetOld * inset = cpar()->getInset(cpos());
1347 if (!isHighlyEditableInset(inset))
1349 FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1350 inset->dispatch(cmd);
1351 if (!the_locking_inset)
1353 updateLocal(bv, false);
1358 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1359 mouse_button::state button)
1362 int dummyy = y + dim_.asc;
1363 InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1364 // we only do the edit() call if the inset was hit by the mouse
1365 // or if it is a highly editable inset. So we should call this
1366 // function from our own edit with button < 0.
1367 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1369 if (button == mouse_button::none && !isHighlyEditableInset(inset))
1378 inset_x = cx() - top_x;
1380 FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1381 inset->dispatch(cmd);
1382 if (!the_locking_inset)
1384 updateLocal(bv, false);
1389 void InsetText::markNew(bool track_changes)
1391 ParagraphList::iterator pit = paragraphs.begin();
1392 ParagraphList::iterator end = paragraphs.end();
1393 for (; pit != end; ++pit) {
1394 if (track_changes) {
1395 pit->trackChanges();
1397 // no-op when not tracking
1398 pit->cleanChanges();
1404 void InsetText::setText(string const & data, LyXFont const & font)
1407 for (unsigned int i = 0; i < data.length(); ++i)
1408 paragraphs.begin()->insertChar(i, data[i], font);
1412 void InsetText::setAutoBreakRows(bool flag)
1414 if (flag != autoBreakRows_) {
1415 autoBreakRows_ = flag;
1422 void InsetText::setDrawFrame(DrawFrame how)
1428 LColor_color InsetText::frameColor() const
1430 return LColor::color(frame_color_);
1434 void InsetText::setFrameColor(LColor_color col)
1440 int InsetText::cx() const
1442 int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1443 if (the_locking_inset) {
1444 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1445 if (font.isVisibleRightToLeft())
1446 x -= the_locking_inset->width();
1452 int InsetText::cy() const
1454 return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1458 pos_type InsetText::cpos() const
1460 return text_.cursor.pos();
1464 ParagraphList::iterator InsetText::cpar() const
1466 return text_.cursorPar();
1470 bool InsetText::cboundary() const
1472 return text_.cursor.boundary();
1476 RowList::iterator InsetText::crow() const
1478 return cpar()->getRow(cpos());
1482 LyXText * InsetText::getLyXText(BufferView const * bv,
1483 bool const recursive) const
1486 if (recursive && the_locking_inset)
1487 return the_locking_inset->getLyXText(bv, true);
1492 void InsetText::setViewCache(BufferView const * bv) const
1495 if (bv != text_.bv_owner) {
1496 //lyxerr << "setting view cache from "
1497 // << text_.bv_owner << " to " << bv << "\n";
1498 text_.init(const_cast<BufferView *>(bv));
1500 text_.bv_owner = const_cast<BufferView *>(bv);
1505 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1508 /// then remove all LyXText in text-insets
1509 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1510 const_cast<ParagraphList&>(paragraphs).end(),
1511 boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1516 void InsetText::removeNewlines()
1518 ParagraphList::iterator it = paragraphs.begin();
1519 ParagraphList::iterator end = paragraphs.end();
1520 for (; it != end; ++it)
1521 for (int i = 0; i < it->size(); ++i)
1522 if (it->isNewline(i))
1527 int InsetText::scroll(bool recursive) const
1529 int sx = UpdatableInset::scroll(false);
1531 if (recursive && the_locking_inset)
1532 sx += the_locking_inset->scroll(recursive);
1538 void InsetText::clearSelection(BufferView *)
1540 text_.clearSelection();
1544 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1546 Painter & pain = bv->painter();
1548 int h = dim_.asc + dim_.des;
1549 int ty = baseline - dim_.asc;
1555 if (ty + h > pain.paperHeight())
1556 h = pain.paperHeight();
1557 if (top_x + w > pain.paperWidth())
1558 w = pain.paperWidth();
1559 pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1563 ParagraphList * InsetText::getParagraphs(int i) const
1565 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
1569 LyXText * InsetText::getText(int i) const
1571 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1575 LyXCursor const & InsetText::cursor(BufferView * bv) const
1577 if (the_locking_inset)
1578 return the_locking_inset->cursor(bv);
1579 return getLyXText(bv)->cursor;
1583 InsetOld * InsetText::getInsetFromID(int id_arg) const
1586 return const_cast<InsetText *>(this);
1588 ParagraphList::const_iterator pit = paragraphs.begin();
1589 ParagraphList::const_iterator pend = paragraphs.end();
1590 for (; pit != pend; ++pit) {
1591 InsetList::const_iterator it = pit->insetlist.begin();
1592 InsetList::const_iterator end = pit->insetlist.end();
1593 for (; it != end; ++it) {
1594 if (it->inset->id() == id_arg)
1596 InsetOld * in = it->inset->getInsetFromID(id_arg);
1606 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1609 if (the_locking_inset) {
1610 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1611 if (!word.word().empty()) {
1615 // we have to go on checking so move cursor to the next char
1616 text_.cursor.pos(text_.cursor.pos() + 1);
1618 word = text_.selectNextWordToSpellcheck(value);
1619 if (word.word().empty())
1620 bv->unlockInset(const_cast<InsetText *>(this));
1627 void InsetText::selectSelectedWord(BufferView * bv)
1629 if (the_locking_inset) {
1630 the_locking_inset->selectSelectedWord(bv);
1633 getLyXText(bv)->selectSelectedWord();
1634 updateLocal(bv, false);
1638 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1640 if (the_locking_inset) {
1641 if (the_locking_inset->nextChange(bv, length))
1643 text_.cursorRight(true);
1645 lyx::find::SearchResult result =
1646 lyx::find::findNextChange(bv, &text_, length);
1648 if (result == lyx::find::SR_FOUND) {
1649 LyXCursor cur = text_.cursor;
1650 bv->unlockInset(bv->theLockingInset());
1651 if (bv->lockInset(this))
1654 text_.setSelectionRange(length);
1655 updateLocal(bv, false);
1657 return result != lyx::find::SR_NOT_FOUND;
1661 bool InsetText::searchForward(BufferView * bv, string const & str,
1664 if (the_locking_inset) {
1665 if (the_locking_inset->searchForward(bv, str, cs, mw))
1667 text_.cursorRight(true);
1669 lyx::find::SearchResult result =
1670 lyx::find::find(bv, &text_, str, true, cs, mw);
1672 if (result == lyx::find::SR_FOUND) {
1673 LyXCursor cur = text_.cursor;
1674 bv->unlockInset(bv->theLockingInset());
1675 if (bv->lockInset(this))
1678 text_.setSelectionRange(str.length());
1679 updateLocal(bv, false);
1681 return result != lyx::find::SR_NOT_FOUND;
1685 bool InsetText::searchBackward(BufferView * bv, string const & str,
1688 if (the_locking_inset) {
1689 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1693 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1695 lyx::find::SearchResult result =
1696 lyx::find::find(bv, &text_, str, false, cs, mw);
1698 if (result == lyx::find::SR_FOUND) {
1699 LyXCursor cur = text_.cursor;
1700 bv->unlockInset(bv->theLockingInset());
1701 if (bv->lockInset(this))
1704 text_.setSelectionRange(str.length());
1705 updateLocal(bv, false);
1707 return result != lyx::find::SR_NOT_FOUND;
1711 bool InsetText::checkInsertChar(LyXFont & font)
1713 return owner() ? owner()->checkInsertChar(font) : true;
1717 void InsetText::collapseParagraphs(BufferView * bv)
1719 while (paragraphs.size() > 1) {
1720 ParagraphList::iterator const first = paragraphs.begin();
1721 ParagraphList::iterator second = first;
1723 size_t const first_par_size = first->size();
1725 if (!first->empty() &&
1727 !first->isSeparator(first_par_size - 1)) {
1728 first->insertChar(first_par_size, ' ');
1731 #warning probably broken
1732 if (text_.selection.set()) {
1733 if (text_.selection.start.par() == 1) {
1734 text_.selection.start.par(1);
1735 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1737 if (text_.selection.end.par() == 2) {
1738 text_.selection.end.par(1);
1739 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1743 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1748 void InsetText::getDrawFont(LyXFont & font) const
1752 owner()->getDrawFont(font);
1756 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1758 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1759 // And it probably does. You have to take a look at this John. (Lgb)
1760 #warning John, have a look here. (Lgb)
1761 ParagraphList::iterator pit = plist.begin();
1762 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1764 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1766 ParagraphList::iterator pend = plist.end();
1767 for (; pit != pend; ++pit)
1768 paragraphs.push_back(*pit);
1772 void InsetText::addPreview(PreviewLoader & loader) const
1774 ParagraphList::const_iterator pit = paragraphs.begin();
1775 ParagraphList::const_iterator pend = paragraphs.end();
1777 for (; pit != pend; ++pit) {
1778 InsetList::const_iterator it = pit->insetlist.begin();
1779 InsetList::const_iterator end = pit->insetlist.end();
1780 for (; it != end; ++it)
1781 it->inset->addPreview(loader);