]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetFloat.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[lyx.git] / src / insets / InsetFloat.cpp
index b710d9ae8c9e5a3e8135a33cb8f7f0cee61b2a64..df970d3de98dea2e3e3437d872e56b20b9592e7f 100644 (file)
@@ -5,6 +5,7 @@
  *
  * \author Jürgen Vigna
  * \author Lars Gullik Bjønnes
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "InsetFloat.h"
+#include "InsetCaption.h"
 
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "Counters.h"
 #include "Cursor.h"
-#include "debug.h"
 #include "DispatchResult.h"
 #include "Floating.h"
 #include "FloatList.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "gettext.h"
+#include "InsetList.h"
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
 #include "OutputParams.h"
+#include "ParIterator.h"
+#include "TextClass.h"
 
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/docstream.h"
 #include "support/convert.h"
 
+using namespace std;
 
 namespace lyx {
 
-using std::endl;
-using std::string;
-using std::istringstream;
-using std::ostream;
-using std::ostringstream;
-
 
 // With this inset it will be possible to support the latex package
 // float.sty, and I am sure that with this and some additional support
@@ -111,10 +111,10 @@ using std::ostringstream;
 // Lgb
 
 
-InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
-       : InsetCollapsable(bp), name_(from_utf8(type))
+InsetFloat::InsetFloat(Buffer const & buf, string const & type)
+       : InsetCollapsable(buf), name_(from_utf8(type))
 {
-       setLabel(_("float: ") + floatName(type, bp));
+       setLabel(_("float: ") + floatName(type, buf.params()));
        params_.type = type;
 }
 
@@ -135,6 +135,7 @@ void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
                params_.placement = params.placement;
                params_.wide      = params.wide;
                params_.sideways  = params.sideways;
+               params_.subfloat  = params.subfloat;
                wide(params_.wide, cur.buffer().params());
                sideways(params_.sideways, cur.buffer().params());
                break;
@@ -147,6 +148,8 @@ void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_RELEASE: {
                if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
+                       if (params_.subfloat)
+                               break;
                        InsetFloatMailer(*this).showDialog(&cur.bv());
                        break;
                }
@@ -177,18 +180,27 @@ bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
+void InsetFloat::updateLabels(ParIterator const & it)
 {
-       Counters & cnts = buf.params().getTextClass().counters();
+       Counters & cnts = buffer().params().documentClass().counters();
        string const saveflt = cnts.current_float();
+       bool const savesubflt = cnts.isSubfloat();
+
+       bool const subflt = it.innerInsetOfType(FLOAT_CODE);
+       // floats can only embed subfloats of their own kind
+       if (subflt)
+               params_.type = saveflt;
+       subfloat(subflt, buffer().params());
 
        // Tell to captions what the current float is
        cnts.current_float(params().type);
+       cnts.isSubfloat(subflt);
 
-       InsetCollapsable::updateLabels(buf, it);
+       InsetCollapsable::updateLabels(it);
 
        //reset afterwards
        cnts.current_float(saveflt);
+       cnts.isSubfloat(savesubflt);
 }
 
 
@@ -242,66 +254,79 @@ void InsetFloatParams::read(Lexer & lex)
 }
 
 
-void InsetFloat::write(Buffer const & buf, ostream & os) const
+void InsetFloat::write(ostream & os) const
 {
        params_.write(os);
-       InsetCollapsable::write(buf, os);
+       InsetCollapsable::write(os);
 }
 
 
-void InsetFloat::read(Buffer const & buf, Lexer & lex)
+void InsetFloat::read(Lexer & lex)
 {
        params_.read(lex);
-       wide(params_.wide, buf.params());
-       sideways(params_.sideways, buf.params());
-       InsetCollapsable::read(buf, lex);
+       wide(params_.wide, buffer().params());
+       sideways(params_.sideways, buffer().params());
+       subfloat(params_.subfloat, buffer().params());
+       InsetCollapsable::read(lex);
 }
 
 
 void InsetFloat::validate(LaTeXFeatures & features) const
 {
-       if (support::contains(params_.placement, 'H')) {
+       if (support::contains(params_.placement, 'H'))
                features.require("float");
-       }
 
        if (params_.sideways)
-               features.require("rotating");
-
-       features.useFloat(params_.type);
-       InsetCollapsable::validate(features);
-}
+               features.require("rotfloat");
 
+       if (params_.subfloat)
+               features.require("subfig");
 
-Inset * InsetFloat::clone() const
-{
-       return new InsetFloat(*this);
+       features.useFloat(params_.type, params_.subfloat);
+       InsetCollapsable::validate(features);
 }
 
 
-docstring const InsetFloat::editMessage() const
+docstring InsetFloat::editMessage() const
 {
        return _("Opened Float Inset");
 }
 
 
-int InsetFloat::latex(Buffer const & buf, odocstream & os,
-                     OutputParams const & runparams) const
+int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
 {
-       FloatList const & floats = buf.params().getTextClass().floats();
-       string tmptype = (params_.wide ? params_.type + "*" : params_.type);
-       if (params_.sideways) {
-               if (params_.type == "table")
-                       tmptype = "sidewaystable";
-               else if (params_.type == "figure")
-                       tmptype = "sidewaysfigure";
+       if (params_.subfloat) {
+               if (runparams.moving_arg)
+                       os << "\\protect";
+               os << "\\subfloat";
+       
+               OutputParams rp = runparams;
+               docstring const caption = getCaption(rp);
+               if (!caption.empty()) {
+                       os << caption;
+               }
+               os << '{';
+               int const i = InsetText::latex(os, runparams);
+               os << "}";
+       
+               return i + 1;
        }
+
+       FloatList const & floats = buffer().params().documentClass().floats();
+       string tmptype = params_.type;
+       if (params_.sideways)
+               tmptype = "sideways" + params_.type;
+       if (params_.wide && (!params_.sideways ||
+                            params_.type == "figure" ||
+                            params_.type == "table"))
+               tmptype += "*";
        // Figure out the float placement to use.
        // From lowest to highest:
        // - float default placement
        // - document wide default placement
        // - specific float placement
        string placement;
-       string const buf_placement = buf.params().float_placement;
+       string const buf_placement = buffer().params().float_placement;
        string const def_placement = floats.defaultPlacement(params_.type);
        if (!params_.placement.empty()
            && params_.placement != def_placement) {
@@ -323,7 +348,7 @@ int InsetFloat::latex(Buffer const & buf, odocstream & os,
        }
        os << '\n';
 
-       int const i = InsetText::latex(buf, os, runparams);
+       int const i = InsetText::latex(os, runparams);
 
        // The \n is used to force \end{<floatname>} to appear in a new line.
        // In this case, we do not case if the current output line is empty.
@@ -333,23 +358,23 @@ int InsetFloat::latex(Buffer const & buf, odocstream & os,
 }
 
 
-int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
-                         OutputParams const & runparams) const
+int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
 {
-       os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
-       InsetText::plaintext(buf, os, runparams);
+       os << '[' << buffer().B_("float") << ' '
+               << floatName(params_.type, buffer().params()) << ":\n";
+       InsetText::plaintext(os, runparams);
        os << "\n]";
 
        return PLAINTEXT_NEWLINE + 1; // one char on a separate line
 }
 
 
-int InsetFloat::docbook(Buffer const & buf, odocstream & os,
-                       OutputParams const & runparams) const
+int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
 {
+       // FIXME Implement subfloat!
        // FIXME UNICODE
        os << '<' << from_ascii(params_.type) << '>';
-       int const i = InsetText::docbook(buf, os, runparams);
+       int const i = InsetText::docbook(os, runparams);
        os << "</" << from_ascii(params_.type) << '>';
 
        return i;
@@ -358,9 +383,9 @@ int InsetFloat::docbook(Buffer const & buf, odocstream & os,
 
 bool InsetFloat::insetAllowed(InsetCode code) const
 {
-       return code != FLOAT_CODE
-           && code != FOOT_CODE
-           && code != MARGIN_CODE;
+       return code != FOOT_CODE
+           && code != MARGIN_CODE
+           && (code != FLOAT_CODE || !params_.subfloat);
 }
 
 
@@ -392,6 +417,42 @@ void InsetFloat::sideways(bool s, BufferParams const & bp)
 }
 
 
+void InsetFloat::subfloat(bool s, BufferParams const & bp)
+{
+       params_.subfloat = s;
+       docstring lab = _("float: ") + floatName(params_.type, bp);
+       if (s)
+               lab = _("subfloat: ") + floatName(params_.type, bp);
+       setLabel(lab);
+}
+
+
+docstring InsetFloat::getCaption(OutputParams const & runparams) const
+{
+       if (paragraphs().empty())
+               return docstring();
+
+       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) {
+                               odocstringstream ods;
+                               InsetCaption * ins =
+                                       static_cast<InsetCaption *>(it->inset);
+                               ins->getOptArg(ods, runparams);
+                               ods << '[';
+                               ins->getArgument(ods, runparams);
+                               ods << ']';
+                               return ods.str();
+                       }
+               }
+       }
+       return docstring();
+}
+
+
 string const InsetFloatMailer::name_("float");
 
 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)