]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetText.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[lyx.git] / src / insets / InsetText.cpp
index 56e9d8f0318f7995c74bbbb687742e682d185fb0..a396041a30bbb453b7725cf64aa325835495df3e 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.
  */
@@ -25,6 +25,8 @@
 #include "DispatchResult.h"
 #include "ErrorList.h"
 #include "FuncRequest.h"
+#include "FuncStatus.h"
+#include "InsetCaption.h"
 #include "InsetList.h"
 #include "Intl.h"
 #include "Lexer.h"
@@ -33,6 +35,7 @@
 #include "MetricsInfo.h"
 #include "output_docbook.h"
 #include "output_latex.h"
+#include "output_xhtml.h"
 #include "OutputParams.h"
 #include "output_plaintext.h"
 #include "paragraph_funcs.h"
@@ -70,11 +73,11 @@ using graphics::PreviewLoader;
 
 /////////////////////////////////////////////////////////////////////
 
-InsetText::InsetText(Buffer const & buf)
+InsetText::InsetText(Buffer const & buf, UsePlain type)
        : drawFrame_(false), frame_color_(Color_insetframe)
 {
-       initParagraphs(buf.params());
        setBuffer(const_cast<Buffer &>(buf));
+       initParagraphs(type);
 }
 
 
@@ -98,13 +101,17 @@ void InsetText::setBuffer(Buffer & buf)
 }
 
 
-void InsetText::initParagraphs(BufferParams const & bparams)
+void InsetText::initParagraphs(UsePlain type)
 {
        LASSERT(paragraphs().empty(), /**/);
        paragraphs().push_back(Paragraph());
        Paragraph & ourpar = paragraphs().back();
        ourpar.setInsetOwner(this);
-       ourpar.setPlainOrDefaultLayout(bparams.documentClass());
+       DocumentClass const & dc = buffer_->params().documentClass();
+       if (type == DefaultLayout)
+               ourpar.setDefaultLayout(dc);
+       else
+               ourpar.setPlainLayout(dc);
 }
 
 
@@ -178,6 +185,13 @@ void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
        // Hand font through to contained lyxtext:
        tm.font_.fontInfo() = mi.base.font;
        mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+
+       // This can happen when a layout has a left and right margin,
+       // and the view is made very narrow. We can't do better than 
+       // to draw it partly out of view (bug 5890).
+       if (mi.base.textwidth < 1)
+               mi.base.textwidth = 1;
+
        if (hasFixedWidth())
                tm.metrics(mi, dim, mi.base.textwidth);
        else
@@ -199,11 +213,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;
 }
 
 
@@ -243,14 +264,45 @@ void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        LYXERR(Debug::ACTION, "InsetText::doDispatch()"
                << " [ cmd.action = " << cmd.action << ']');
-       text_.dispatch(cur, cmd);
+
+       // Dispatch only to text_ if the cursor is inside
+       // the text_. It is not for context menus (bug 5797).
+       if (cur.text() == &text_)
+               text_.dispatch(cur, cmd);
+       else
+               cur.undispatched();
+       
+       if (!cur.result().dispatched())
+               Inset::doDispatch(cur, 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:
+               // Dispatch only to text_ if the cursor is inside
+               // the text_. It is not for context menus (bug 5797).
+               bool ret = false;
+               if (cur.text() == &text_)
+                       ret = text_.getStatus(cur, cmd, status);
+               
+               if (!ret)
+                       ret = Inset::getStatus(cur, cmd, status);
+               return ret;
+       }
 }
 
 
@@ -264,15 +316,15 @@ void InsetText::setChange(Change const & change)
 }
 
 
-void InsetText::acceptChanges(BufferParams const & bparams)
+void InsetText::acceptChanges()
 {
-       text_.acceptChanges(bparams);
+       text_.acceptChanges(buffer().params());
 }
 
 
-void InsetText::rejectChanges(BufferParams const & bparams)
+void InsetText::rejectChanges()
 {
-       text_.rejectChanges(bparams);
+       text_.rejectChanges(buffer().params());
 }
 
 
@@ -317,6 +369,13 @@ int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetText::xhtml(odocstream & os, OutputParams const & runparams) const
+{
+       xhtmlParagraphs(paragraphs(), buffer(), os, runparams);
+       return docstring();
+}
+
+
 void InsetText::validate(LaTeXFeatures & features) const
 {
        for_each(paragraphs().begin(), paragraphs().end(),
@@ -436,13 +495,13 @@ void InsetText::updateLabels(ParIterator const & it)
 {
        ParIterator it2 = it;
        it2.forwardPos();
-       LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
+       LASSERT(&it2.inset() == this && it2.pit() == 0, return);
        if (producesOutput())
-               lyx::updateLabels(buffer(), it2);
+               buffer().updateLabels(it2);
        else {
-               DocumentClass const & tclass = buffer().params().documentClass();
+               DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
                Counters const savecnt = tclass.counters();
-               lyx::updateLabels(buffer(), it2);
+               buffer().updateLabels(it2);
                tclass.counters() = savecnt;
        }
 }
@@ -482,29 +541,32 @@ void InsetText::addToToc(DocIterator const & cdit)
                                        *static_cast<InsetOptArg&>(inset).paragraphs().begin();
                                if (!par.labelString().empty())
                                        tocstring = par.labelString() + ' ';
-                               tocstring += insetpar.asString();
+                               tocstring += insetpar.asString(AS_STR_INSETS);
                                break;
                        }
                        default:
                                break;
                        }
                }
-               /// now the toc entry for the paragraph
+               // 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);
+                               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());
        }
 }
 
 
 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 {
-       if (cur.buffer().isClean())
+       if (buffer().isClean())
                return Inset::notifyCursorLeaves(old, cur);
        
        // find text inset in old cursor
@@ -515,7 +577,7 @@ bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
        LASSERT(&insetCur.inset() == this, /**/);
        
        // update the old paragraph's words
-       insetCur.paragraph().updateWords(insetCur.top());
+       insetCur.paragraph().updateWords();
        
        return Inset::notifyCursorLeaves(old, cur);
 }
@@ -523,9 +585,7 @@ bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 
 bool InsetText::completionSupported(Cursor const & cur) const
 {
-       Cursor const & bvCur = cur.bv().cursor();
-       if (&bvCur.inset() != this)
-               return false;
+       //LASSERT(&cur.bv().cursor().inset() != this, return false);
        return text_.completionSupported(cur);
 }
 
@@ -592,4 +652,48 @@ docstring InsetText::contextMenu(BufferView const &, int, int) const
 }
 
 
+InsetCaption const * InsetText::getCaptionInset() const
+{
+       ParagraphList::const_iterator pit = paragraphs().begin();
+       for (; pit != paragraphs().end(); ++pit) {
+               InsetList::const_iterator it = pit->insetList().begin();
+               for (; it != pit->insetList().end(); ++it) {
+                       Inset & inset = *it->inset;
+                       if (inset.lyxCode() == CAPTION_CODE) {
+                               InsetCaption const * ins =
+                                       static_cast<InsetCaption const *>(it->inset);
+                               return ins;
+                       }
+               }
+       }
+       return 0;
+}
+
+
+docstring InsetText::getCaptionText(OutputParams const & runparams) const
+{
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       ins->getCaptionAsPlaintext(ods, runparams);
+       return ods.str();
+}
+
+
+docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
+{
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       docstring def = ins->getCaptionAsHTML(ods, runparams);
+       if (!def.empty())
+               ods << def << '\n';
+       return ods.str();
+}
+
+
 } // namespace lyx