]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetFloat.cpp
Improved character count statistics for letter based insets (e.g. the LyX logo).
[features.git] / src / insets / InsetFloat.cpp
index c9f90faf902201837b2e4a234e5e9a9d93582dce..b0a48f2b31007d367de11e70714e06252d3380eb 100644 (file)
  * Full author contact details are available in file CREDITS.
  */
 
-#include <typeinfo>
-
 #include <config.h>
-#include <xml.h>
 
 #include "InsetBox.h"
 #include "InsetCaption.h"
@@ -33,6 +30,7 @@
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
+#include "xml.h"
 #include "output_docbook.h"
 #include "output_xhtml.h"
 #include "ParIterator.h"
@@ -164,6 +162,11 @@ void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
        switch (cmd.action()) {
 
        case LFUN_INSET_MODIFY: {
+               if (!buffer().params().documentClass().floats().typeExist(cmd.getArg(0))) {
+                       // not for us: pass further.
+                       cur.undispatched();
+                       break;
+               }
                InsetFloatParams params;
                string2params(to_utf8(cmd.argument()), params);
                cur.recordUndoInset(this);
@@ -202,6 +205,9 @@ bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
        switch (cmd.action()) {
 
        case LFUN_INSET_MODIFY:
+               if (!buffer().params().documentClass().floats().typeExist(cmd.getArg(0)))
+                       return Inset::getStatus(cur, cmd, flag);
+       // fall through
        case LFUN_INSET_DIALOG_UPDATE:
                flag.setEnabled(true);
                return true;
@@ -557,32 +563,71 @@ const InsetCaption* findCaptionInParagraph(const Paragraph &par)
 }
 
 
-void InsetFloat::docbook(XMLStream & xs, OutputParams const & runparams) const
+void docbookSubfigures(XMLStream & xs, OutputParams const & runparams, const InsetCaption * caption,
+                                          const InsetLabel * label, std::vector<const InsetBox *> & subfigures)
 {
-       // Determine whether the float has a title or not. For this, iterate through the paragraphs and look
-       // for an InsetCaption. Do the same for labels and subfigures.
-       // The caption and the label for each subfigure is handled by recursive calls.
-       const InsetCaption* caption = nullptr;
-       const InsetLabel* label = nullptr;
-       std::vector<const InsetBox *> subfigures;
+       // Ensure there is no label output, it is supposed to be handled as xml:id.
+       OutputParams rpNoLabel = runparams;
+       if (label)
+               rpNoLabel.docbook_anchors_to_ignore.emplace(label->screenLabel());
 
-       auto end = paragraphs().end();
-       for (auto it = paragraphs().begin(); it != end; ++it) {
-               std::vector<const InsetBox *> foundSubfigures = findSubfiguresInParagraph(*it);
-               if (!foundSubfigures.empty()) {
-                       subfigures.reserve(subfigures.size() + foundSubfigures.size());
-                       subfigures.insert(subfigures.end(), foundSubfigures.begin(), foundSubfigures.end());
+       // First, open the formal group.
+       docstring attr = docstring();
+       if (label)
+               attr += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
+
+       xs.startDivision(false);
+       xs << xml::StartTag("formalgroup", attr);
+       xs << xml::CR();
+
+       xs << xml::StartTag("title", attr);
+       if (caption) {
+               caption->getCaptionAsDocBook(xs, rpNoLabel);
+       } else {
+               xs << "No caption";
+               // No caption has been detected, but this tag is required for the document to be valid DocBook.
+       }
+       xs << xml::EndTag("title");
+       xs << xml::CR();
+
+       // Deal with each subfigure individually. This should also deal with their caption and their label.
+       // This should be a recursive call to InsetFloat.
+       for (const InsetBox *subfigure: subfigures) {
+               // If there is no InsetFloat in the paragraphs, output a warning.
+               bool foundInsetFloat = false;
+               for (const auto & it : subfigure->paragraphs()) {
+                       for (pos_type posIn = 0; posIn < it.size(); ++posIn) {
+                               const Inset *inset = it.getInset(posIn);
+                               if (inset && dynamic_cast<const InsetFloat*>(inset)) {
+                                       foundInsetFloat = true;
+                                       break;
+                               }
+                       }
+
+                       if (foundInsetFloat)
+                               break;
                }
 
-               if (!caption)
-                       caption = findCaptionInParagraph(*it);
-               if (!label)
-                       label = findLabelInParagraph(*it);
+               if (!foundInsetFloat)
+                       xs << XMLStream::ESCAPE_NONE << "Error: no float found in the box. "
+                                                               "To use subfigures in DocBook, elements must be wrapped in a float "
+                                           "inset and have a title/caption.";
+               // TODO: could also output a table, that would ensure that the document is correct and *displays* correctly (but without the right semantics), instead of just an error.
+
+               // Finally, recurse.
+               subfigure->docbook(xs, runparams);
        }
 
-       // Gather a few things from global environment that are shared between all following cases.
-       FloatList const &floats = buffer().params().documentClass().floats();
-       Floating const &ftype = floats.getType(params_.type);
+       // Every subfigure is done: close the formal group.
+       xs << xml::EndTag("formalgroup");
+       xs << xml::CR();
+       xs.endDivision();
+}
+
+
+void docbookNoSubfigures(XMLStream & xs, OutputParams const & runparams, const InsetCaption * caption,
+                         const InsetLabel * label, Floating const & ftype, const InsetFloat * thisFloat)
+{
        string const &titleTag = ftype.docbookCaption();
 
        // Ensure there is no label output, it is supposed to be handled as xml:id.
@@ -594,65 +639,10 @@ void InsetFloat::docbook(XMLStream & xs, OutputParams const & runparams) const
        // captions, they cannot appear at the end of the float, albeit LyX is happy with that).
        OutputParams rpNoTitle = runparams;
        rpNoTitle.docbook_in_float = true;
+       if (ftype.floattype() == "table")
+               rpNoTitle.docbook_in_table = true;
 
-       // Deal with subfigures.
-       if (!subfigures.empty()) {
-               // First, open the formal group.
-               docstring attr = docstring();
-               if (label)
-                       attr += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
-
-               xs.startDivision(false);
-               xs << xml::StartTag("formalgroup", attr);
-               xs << xml::CR();
-
-               xs << xml::StartTag("title", attr);
-               if (caption) {
-                       caption->getCaptionAsDocBook(xs, rpNoLabel);
-               } else {
-                       xs << "No caption";
-                       // No caption has been detected, but this tag is required for the document to be valid DocBook.
-               }
-               xs << xml::EndTag("title");
-               xs << xml::CR();
-
-               // Deal with each subfigure individually. This should also deal with their caption and their label.
-               // This should be a recursive call to InsetFloat.
-               for (const InsetBox *subfigure: subfigures) {
-                       // If there is no InsetFloat in the paragraphs, output a warning.
-                       bool foundInsetFloat = false;
-                       for (auto it = subfigure->paragraphs().begin(); it != subfigure->paragraphs().end(); ++it) {
-                               for (pos_type posIn = 0; posIn < it->size(); ++posIn) {
-                                       const Inset *inset = it->getInset(posIn);
-                                       if (inset && dynamic_cast<const InsetFloat*>(inset)) {
-                                               foundInsetFloat = true;
-                                               break;
-                                       }
-                               }
-
-                               if (foundInsetFloat)
-                                       break;
-                       }
-
-                       if (!foundInsetFloat)
-                               xs << XMLStream::ESCAPE_NONE << "Error: no float found in the box. "
-                                                                       "To use subfigures in DocBook, elements must be wrapped in a float "
-                                                   "inset and have a title/caption.";
-                       // TODO: could also output a table, that would ensure that the document is correct and *displays* correctly (but without the right semantics), instead of just an error.
-
-                       // Finally, recurse.
-                       subfigure->docbook(xs, runparams);
-               }
-
-               // Every subfigure is done: close the formal group.
-               xs << xml::EndTag("formalgroup");
-               xs << xml::CR();
-               xs.endDivision();
-       }
-
-       // Here, ensured not to have subfigures.
-
-       // Organisation: <float> <title if any/> <contents without title/> </float>
+       // Organisation: <float> <title if any/> <contents without title/> </float>.
        docstring attr = docstring();
        if (label)
                attr += "xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
@@ -670,12 +660,47 @@ void InsetFloat::docbook(XMLStream & xs, OutputParams const & runparams) const
                xs << xml::EndTag(titleTag);
                xs << xml::CR();
        }
-       InsetText::docbook(xs, rpNoTitle);
+       thisFloat->InsetText::docbook(xs, rpNoTitle);
        xs << xml::EndTag(ftype.docbookTag(caption != nullptr));
        xs << xml::CR();
 }
 
 
+void InsetFloat::docbook(XMLStream & xs, OutputParams const & runparams) const
+{
+       // Determine whether the float has a title or not. For this, iterate through the paragraphs and look
+       // for an InsetCaption. Do the same for labels and subfigures.
+       // The caption and the label for each subfigure is handled by recursive calls.
+       const InsetCaption* caption = nullptr;
+       const InsetLabel* label = nullptr;
+       std::vector<const InsetBox *> subfigures;
+
+       auto end = paragraphs().end();
+       for (auto it = paragraphs().begin(); it != end; ++it) {
+               std::vector<const InsetBox *> foundSubfigures = findSubfiguresInParagraph(*it);
+               if (!foundSubfigures.empty()) {
+                       subfigures.reserve(subfigures.size() + foundSubfigures.size());
+                       subfigures.insert(subfigures.end(), foundSubfigures.begin(), foundSubfigures.end());
+               }
+
+               if (!caption)
+                       caption = findCaptionInParagraph(*it);
+               if (!label)
+                       label = findLabelInParagraph(*it);
+       }
+
+       // Gather a few things from global environment that are shared between all following cases.
+       FloatList const &floats = buffer().params().documentClass().floats();
+       Floating const &ftype = floats.getType(params_.type);
+
+       // Switch on subfigures.
+       if (!subfigures.empty())
+               docbookSubfigures(xs, runparams, caption, label, subfigures);
+       else
+               docbookNoSubfigures(xs, runparams, caption, label, ftype, this);
+}
+
+
 bool InsetFloat::insetAllowed(InsetCode code) const
 {
        // The case that code == FLOAT_CODE is handled in Text3.cpp,