]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetFloat.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[lyx.git] / src / insets / InsetFloat.cpp
index a595b2e0321f5560530f58b5abef6d6fd352c9b4..d70d356e7f30af7e2892ff7d558a4ad7c9051e18 100644 (file)
@@ -3,9 +3,9 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Jürgen Vigna
- * \author Lars Gullik Bjønnes
- * \author Jürgen Spitzmüller
+ * \author Jürgen Vigna
+ * \author Lars Gullik Bjønnes
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "FloatList.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "InsetList.h"
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
-#include "OutputParams.h"
 #include "ParIterator.h"
 #include "TextClass.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
@@ -51,7 +48,7 @@ namespace lyx {
 // (read DocBook).
 // By using float.sty we will have the same handling for all floats, both
 // for those already in existance (table and figure) and all user created
-// ones¹. So suddenly we give the users the possibility of creating new
+// ones¹. So suddenly we give the users the possibility of creating new
 // kinds of floats on the fly. (and with a uniform look)
 //
 // API to float.sty:
@@ -88,7 +85,7 @@ namespace lyx {
 //   \listof{type}{title}
 //     title -
 
-// ¹ the algorithm float is defined using the float.sty package. Like this
+// Â¹ the algorithm float is defined using the float.sty package. Like this
 //   \floatstyle{ruled}
 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
 //   \floatname{algorithm}{Algorithm}
@@ -116,7 +113,7 @@ namespace lyx {
 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
        : InsetCollapsable(buf), name_(from_utf8(type))
 {
-       setLabel(_("float: ") + floatName(type, buf.params()));
+       setLabel(_("float: ") + floatName(type));
        params_.type = type;
 }
 
@@ -127,21 +124,19 @@ InsetFloat::~InsetFloat()
 }
 
 
+docstring InsetFloat::name() const 
+{ 
+       return "Float:" + name_; 
+}
+
+
 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
 {
-       if (InsetCollapsable::toolTip(bv, x, y).empty())
+       if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
                return docstring();
 
-       docstring default_tip;
-       if (isOpen())
-               default_tip = _("Left-click to collapse the inset");
-       else
-               default_tip = _("Left-click to open the inset");
        OutputParams rp(&buffer().params().encoding());
-       docstring caption_tip = getCaptionText(rp);
-       if (!isOpen() && !caption_tip.empty())
-               return caption_tip + '\n' + default_tip;
-       return default_tip;
+       return getCaptionText(rp);
 }
 
 
@@ -152,12 +147,15 @@ void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_INSET_MODIFY: {
                InsetFloatParams params;
                string2params(to_utf8(cmd.argument()), params);
-               params_.placement = params.placement;
-               params_.wide      = params.wide;
-               params_.sideways  = params.sideways;
-               params_.subfloat  = params.subfloat;
-               setWide(params_.wide, cur.buffer().params());
-               setSideways(params_.sideways, cur.buffer().params());
+
+               // placement, wide and sideways are not used for subfloats
+               if (!params_.subfloat) {
+                       params_.placement = params.placement;
+                       params_.wide      = params.wide;
+                       params_.sideways  = params.sideways;
+               }
+
+               setNewLabel();
                break;
        }
 
@@ -191,7 +189,8 @@ bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetFloat::updateLabels(ParIterator const & it)
 {
-       Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
+       Counters & cnts =
+               buffer().masterBuffer()->params().documentClass().counters();
        string const saveflt = cnts.current_float();
        bool const savesubflt = cnts.isSubfloat();
 
@@ -200,7 +199,7 @@ void InsetFloat::updateLabels(ParIterator const & it)
        // floats can only embed subfloats of their own kind
        if (subflt)
                params_.type = saveflt;
-       setSubfloat(subflt, buffer().params());
+       setSubfloat(subflt);
 
        // Tell to captions what the current float is
        cnts.current_float(params().type);
@@ -253,9 +252,6 @@ void InsetFloat::write(ostream & os) const
 void InsetFloat::read(Lexer & lex)
 {
        params_.read(lex);
-       setWide(params_.wide, buffer().params());
-       setSideways(params_.sideways, buffer().params());
-       setSubfloat(params_.subfloat, buffer().params());
        InsetCollapsable::read(lex);
 }
 
@@ -268,11 +264,13 @@ void InsetFloat::validate(LaTeXFeatures & features) const
        if (params_.sideways)
                features.require("rotfloat");
 
-       if (params_.subfloat)
+       if (features.inFloat())
                features.require("subfig");
 
-       features.useFloat(params_.type, params_.subfloat);
+       features.useFloat(params_.type, features.inFloat());
+       features.inFloat(true);
        InsetCollapsable::validate(features);
+       features.inFloat(false);
 }
 
 
@@ -282,24 +280,57 @@ docstring InsetFloat::editMessage() const
 }
 
 
-int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
+docstring InsetFloat::xhtml(odocstream & os, OutputParams const & rp) const
+{
+       FloatList const & floats = buffer().params().documentClass().floats();
+       Floating const & ftype = floats.getType(params_.type);
+       string const htmltype = ftype.htmlType().empty() ? 
+                       "div" : ftype.htmlType();
+       string const htmlclass = ftype.htmlClass().empty() ?
+                       "float-" + params_.type : ftype.htmlClass();
+       docstring const otag = 
+                       from_ascii("<" + htmltype + " class='float " + htmlclass + "'>\n");
+       docstring const ctag = from_ascii("</" + htmltype + ">\n");
+
+       odocstringstream out;
+
+       out << otag;
+       docstring def = InsetText::xhtml(out, rp);
+       out << ctag;
+
+       if (rp.inFloat == OutputParams::NONFLOAT)
+               // In this case, this float needs to be deferred, but we'll put it
+               // before anything the text itself deferred.
+               def = out.str() + '\n' + def;
+       else 
+               // In this case, the whole thing is already being deferred, so
+               // we can write to the stream.
+               os << out.str();
+       return def;
+}
+
+
+int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
 {
-       if (params_.subfloat) {
-               if (runparams.moving_arg)
+       if (runparams_in.inFloat != OutputParams::NONFLOAT) {
+               if (runparams_in.moving_arg)
                        os << "\\protect";
                os << "\\subfloat";
        
-               OutputParams rp = runparams;
+               OutputParams rp = runparams_in;
                docstring const caption = getCaption(rp);
                if (!caption.empty()) {
                        os << caption;
                }
                os << '{';
-               int const i = InsetText::latex(os, runparams);
+               rp.inFloat = OutputParams::SUBFLOAT;
+               int const i = InsetText::latex(os, rp);
                os << "}";
        
                return i + 1;
        }
+       OutputParams runparams(runparams_in);
+       runparams.inFloat = OutputParams::MAINFLOAT;
 
        FloatList const & floats = buffer().params().documentClass().floats();
        string tmptype = params_.type;
@@ -350,7 +381,7 @@ int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
 {
        os << '[' << buffer().B_("float") << ' '
-               << floatName(params_.type, buffer().params()) << ":\n";
+               << floatName(params_.type) << ":\n";
        InsetText::plaintext(os, runparams);
        os << "\n]";
 
@@ -387,82 +418,64 @@ bool InsetFloat::showInsetDialog(BufferView * bv) const
 }
 
 
-void InsetFloat::setWide(bool w, BufferParams const & bp)
+void InsetFloat::setWide(bool w, bool update_label)
 {
        params_.wide = w;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (params_.wide)
-               lab += '*';
-       setLabel(lab);
+       if (update_label)
+               setNewLabel();
 }
 
 
-void InsetFloat::setSideways(bool s, BufferParams const & bp)
+void InsetFloat::setSideways(bool s, bool update_label)
 {
        params_.sideways = s;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (params_.sideways)
-               lab += _(" (sideways)");
-       setLabel(lab);
+       if (update_label)
+               setNewLabel();
 }
 
 
-void InsetFloat::setSubfloat(bool s, BufferParams const & bp)
+void InsetFloat::setSubfloat(bool s, bool update_label)
 {
        params_.subfloat = s;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (s)
-               lab = _("subfloat: ") + floatName(params_.type, bp);
-       setLabel(lab);
+       if (update_label)
+               setNewLabel();
 }
 
 
-docstring InsetFloat::getCaption(OutputParams const & runparams) const
+void InsetFloat::setNewLabel()
 {
-       if (paragraphs().empty())
-               return docstring();
+       docstring lab = _("float: ");
 
-       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();
+       if (params_.subfloat)
+               lab = _("subfloat: ");
+
+       lab += floatName(params_.type);
+
+       if (params_.wide)
+               lab += '*';
+
+       if (params_.sideways)
+               lab += _(" (sideways)");
+
+       setLabel(lab);
 }
 
 
-docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
+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->getCaptionText(ods, runparams);
-                               return ods.str();
-                       }
-               }
-       }
-       return docstring();
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       ins->getOptArg(ods, runparams);
+       ods << '[';
+       ins->getArgument(ods, runparams);
+       ods << ']';
+       return ods.str();
 }