]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetText.cpp
move more stuff from buffer_func to Buffer
[features.git] / src / insets / InsetText.cpp
index 1b0435b0a0b3fcf73ba2082043d30152dbe1630d..078d8154dbca4a0b4c76c6f32d357aaadcbfb0a4 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Jürgen Vigna
+ * \author Jürgen Vigna
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -12,6 +12,8 @@
 
 #include "InsetText.h"
 
+#include "insets/InsetOptArg.h"
+
 #include "buffer_funcs.h"
 #include "Buffer.h"
 #include "BufferParams.h"
@@ -23,6 +25,7 @@
 #include "DispatchResult.h"
 #include "ErrorList.h"
 #include "FuncRequest.h"
+#include "FuncStatus.h"
 #include "InsetList.h"
 #include "Intl.h"
 #include "Lexer.h"
@@ -43,6 +46,7 @@
 #include "TextClass.h"
 #include "Text.h"
 #include "TextMetrics.h"
+#include "TocBackend.h"
 
 #include "frontends/alert.h"
 #include "frontends/Painter.h"
@@ -52,7 +56,7 @@
 #include "support/lstrings.h"
 
 #include <boost/bind.hpp>
-#include <boost/assert.hpp>
+#include "support/lassert.h"
 
 using namespace std;
 using namespace lyx::support;
@@ -70,7 +74,8 @@ using graphics::PreviewLoader;
 InsetText::InsetText(Buffer const & buf)
        : drawFrame_(false), frame_color_(Color_insetframe)
 {
-       initParagraphs(buf);
+       setBuffer(const_cast<Buffer &>(buf));
+       initParagraphs();
 }
 
 
@@ -85,20 +90,25 @@ InsetText::InsetText(InsetText const & in)
 }
 
 
-InsetText::InsetText()
-{}
+void InsetText::setBuffer(Buffer & buf)
+{
+       ParagraphList::iterator end = paragraphs().end();
+       for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
+               it->setBuffer(buf);
+       Inset::setBuffer(buf);
+}
 
 
-void InsetText::initParagraphs(Buffer const & buf)
+void InsetText::initParagraphs()
 {
-       BOOST_ASSERT(paragraphs().empty());
-       buffer_ = const_cast<Buffer *>(&buf);
+       LASSERT(paragraphs().empty(), /**/);
        paragraphs().push_back(Paragraph());
        Paragraph & ourpar = paragraphs().back();
-       ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
        ourpar.setInsetOwner(this);
+       ourpar.setDefaultLayout(buffer_->params().documentClass());
 }
 
+
 void InsetText::setParagraphOwner()
 {
        for_each(paragraphs().begin(), paragraphs().end(),
@@ -109,7 +119,7 @@ void InsetText::setParagraphOwner()
 void InsetText::clear()
 {
        ParagraphList & pars = paragraphs();
-       BOOST_ASSERT(!pars.empty());
+       LASSERT(!pars.empty(), /**/);
 
        // This is a gross hack...
        Layout const & old_layout = pars.begin()->layout();
@@ -147,12 +157,11 @@ void InsetText::read(Lexer & lex)
        Paragraph oldpar = *paragraphs().begin();
        paragraphs().clear();
        ErrorList errorList;
+       lex.setContext("InsetText::read");
        bool res = text_.read(buffer(), lex, errorList, this);
 
-       if (!res) {
-               lex.printError("Missing \\end_inset at this point. "
-                                          "Read: `$$Token'");
-       }
+       if (!res)
+               lex.printError("Missing \\end_inset at this point. ");
 
        // sanity check
        // ensure we have at least one paragraph.
@@ -191,11 +200,18 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
                int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
                int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
                if (pi.full_repaint)
-                       pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
+                       pi.pain.fillRectangle(xframe, yframe, w, h,
+                               pi.backgroundColor(this));
+
                if (drawFrame_)
                        pi.pain.rectangle(xframe, yframe, w, h, frameColor());
        }
+       ColorCode const old_color = pi.background_color;
+       pi.background_color = pi.backgroundColor(this, false);
+
        tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
+
+       pi.background_color = old_color;
 }
 
 
@@ -242,7 +258,21 @@ void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
        FuncStatus & status) const
 {
-       return text_.getStatus(cur, cmd, status);
+       switch (cmd.action) {
+       case LFUN_LAYOUT:
+               status.setEnabled(!forcePlainLayout());
+               return true;
+
+       case LFUN_LAYOUT_PARAGRAPH:
+       case LFUN_PARAGRAPH_PARAMS:
+       case LFUN_PARAGRAPH_PARAMS_APPLY:
+       case LFUN_PARAGRAPH_SPACING:
+       case LFUN_PARAGRAPH_UPDATE:
+               status.setEnabled(allowParagraphCustomization());
+               return true;
+       default:
+               return text_.getStatus(cur, cmd, status);
+       }
 }
 
 
@@ -412,22 +442,6 @@ void InsetText::addPreview(PreviewLoader & loader) const
 }
 
 
-// FIXME: instead of this hack, which only works by chance,
-// cells should have their own insetcell type, which returns CELL_CODE!
-bool InsetText::isTableCell() const
-{
-       // this is only true for tabular cells
-       return !text_.isMainText(buffer()) && lyxCode() == TEXT_CODE;
-}
-
-
-
-bool InsetText::neverIndent() const
-{
-       return isTableCell();
-}
-
-
 ParagraphList const & InsetText::paragraphs() const
 {
        return text_.paragraphs();
@@ -444,8 +458,72 @@ void InsetText::updateLabels(ParIterator const & it)
 {
        ParIterator it2 = it;
        it2.forwardPos();
-       BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
-       lyx::updateLabels(buffer(), it2);
+       LASSERT(&it2.inset() == this && it2.pit() == 0, return);
+       if (producesOutput()) {
+               buffer().updateLabels(it2);
+       } else {
+               DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
+               Counters const savecnt = tclass.counters();
+               buffer().updateLabels(it2);
+               tclass.counters() = savecnt;
+       }
+}
+
+
+void InsetText::addToToc(DocIterator const & cdit)
+{
+       DocIterator dit = cdit;
+       dit.push_back(CursorSlice(*this));
+       Toc & toc = buffer().tocBackend().toc("tableofcontents");
+
+       BufferParams const & bufparams = buffer_->params();
+       const int min_toclevel = bufparams.documentClass().min_toclevel();
+
+       // For each paragraph, traverse its insets and let them add
+       // their toc items
+       ParagraphList & pars = paragraphs();
+       pit_type pend = paragraphs().size();
+       for (pit_type pit = 0; pit != pend; ++pit) {
+               Paragraph const & par = pars[pit];
+               dit.pit() = pit;
+               // the string that goes to the toc (could be the optarg)
+               docstring tocstring;
+               InsetList::const_iterator it  = par.insetList().begin();
+               InsetList::const_iterator end = par.insetList().end();
+               for (; it != end; ++it) {
+                       Inset & inset = *it->inset;
+                       dit.pos() = it->pos;
+                       //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
+                       inset.addToToc(dit);
+                       switch (inset.lyxCode()) {
+                       case OPTARG_CODE: {
+                               if (!tocstring.empty())
+                                       break;
+                               dit.pos() = 0;
+                               Paragraph const & insetpar =
+                                       *static_cast<InsetOptArg&>(inset).paragraphs().begin();
+                               if (!par.labelString().empty())
+                                       tocstring = par.labelString() + ' ';
+                               tocstring += insetpar.asString(AS_STR_INSETS);
+                               break;
+                       }
+                       default:
+                               break;
+                       }
+               }
+               /// now the toc entry for the paragraph
+               int const toclevel = par.layout().toclevel;
+               if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
+                       dit.pos() = 0;
+                       // insert this into the table of contents
+                       if (tocstring.empty())
+                               tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
+                       toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
+               }
+               
+               // And now the list of changes.
+               par.addChangesToToc(dit, buffer());
+       }
 }
 
 
@@ -457,9 +535,9 @@ bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
        // find text inset in old cursor
        Cursor insetCur = old;
        int scriptSlice = insetCur.find(this);
-       BOOST_ASSERT(scriptSlice != -1);
+       LASSERT(scriptSlice != -1, /**/);
        insetCur.cutOff(scriptSlice);
-       BOOST_ASSERT(&insetCur.inset() == this);
+       LASSERT(&insetCur.inset() == this, /**/);
        
        // update the old paragraph's words
        insetCur.paragraph().updateWords(insetCur.top());
@@ -495,6 +573,12 @@ bool InsetText::automaticPopupCompletion() const
 }
 
 
+bool InsetText::showCompletionCursor() const
+{
+       return lyxrc.completion_cursor_text;
+}
+
+
 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
 {
        return completionSupported(cur) ? text_.createCompletionList(cur) : 0;