]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetfloat.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insetfloat.C
index 1d4c3614cb81a9fbc0dc6b67415468138b08ec09..75a6746df9a33bd9a80e8be472d59b0e669e8044 100644 (file)
@@ -1,12 +1,13 @@
-/* This file is part of
- * ======================================================
- * 
- *           LyX, The Document Processor
- *      
- *          Copyright 1998-2001 The LyX Team.
+/**
+ * \file insetfloat.C
+ * 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
+ *
+ * Full author contact details are available in file CREDITS
+ */
 #include <config.h>
 
 #ifdef __GNUG__
 #include "debug.h"
 #include "Floating.h"
 #include "buffer.h"
-#include "LyXView.h"
+#include "frontends/LyXView.h"
 #include "frontends/Dialogs.h"
+#include "lyxlex.h"
+#include "iterators.h"
 
 using std::ostream;
 using std::endl;
@@ -61,7 +64,7 @@ using std::endl;
 //     within    - This (optional) argument determines whether floats of this
 //                 class will be numbered within some sectional unit of the
 //                 document. For example, if within is equal to chapter, the
-//                 floats will be numbered within chapters. 
+//                 floats will be numbered within chapters.
 //   \floatstyle{style}
 //     style -  plain, boxed, ruled
 //   \floatname{float}{floatname}
@@ -99,11 +102,30 @@ using std::endl;
 //
 // Lgb
 
-InsetFloat::InsetFloat(string const & type)
-       : InsetCollapsable(), wide_(false)
+namespace {
+
+// this should not be hardcoded, but be part of the definition
+// of the float (JMarc)
+string const caplayout("Caption");
+
+string floatname(string const & type, BufferParams const & bp)
 {
-       string lab(_("float:"));
-       lab += type;
+       FloatList const & floats = bp.getLyXTextClass().floats();
+       FloatList::const_iterator it = floats[type];
+       if (it == floats.end())
+               return type;
+
+       return _(it->second.name());
+}
+
+} // namespace anon
+
+
+InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
+       : InsetCollapsable(bp), wide_(false)
+{
+       string lab(_("float: "));
+       lab += floatname(type, bp);
        setLabel(lab);
        LyXFont font(LyXFont::ALL_SANE);
        font.decSize();
@@ -112,6 +134,9 @@ InsetFloat::InsetFloat(string const & type)
        setLabelFont(font);
        floatType_ = type;
        setInsetName(type);
+       LyXTextClass const & tclass = bp.getLyXTextClass();
+       if (tclass.hasLayout(caplayout))
+               inset.paragraph()->layout(tclass[caplayout]);
 }
 
 
@@ -132,10 +157,7 @@ void InsetFloat::write(Buffer const * buf, ostream & os) const
        os << "Float " // getInsetName()
           << floatType_ << '\n';
 
-       if (floatPlacement_.empty()) {
-               os << "placement "
-                  << floatList.getType(floatType_).placement() << "\n";
-       } else {
+       if (!floatPlacement_.empty()) {
                os << "placement " << floatPlacement_ << "\n";
        }
        if (wide_) {
@@ -143,7 +165,7 @@ void InsetFloat::write(Buffer const * buf, ostream & os) const
        } else {
                os << "wide false\n";
        }
-       
+
        InsetCollapsable::write(buf, os);
 }
 
@@ -157,8 +179,6 @@ void InsetFloat::read(Buffer const * buf, LyXLex & lex)
                        lex.next();
                        floatPlacement_ = lex.getString();
                } else {
-                       lyxerr << "InsetFloat::Read: Missing placement!"
-                              << endl;
                        // take countermeasures
                        lex.pushToken(token);
                }
@@ -168,9 +188,9 @@ void InsetFloat::read(Buffer const * buf, LyXLex & lex)
                        lex.next();
                        string const tmptoken = lex.getString();
                        if (tmptoken == "true")
-                               wide(true);
+                               wide(true, buf->params);
                        else
-                               wide(false);
+                               wide(false, buf->params);
                } else {
                        lyxerr << "InsetFloat::Read:: Missing wide!"
                               << endl;
@@ -185,9 +205,9 @@ void InsetFloat::read(Buffer const * buf, LyXLex & lex)
 void InsetFloat::validate(LaTeXFeatures & features) const
 {
        if (contains(placement(), "H")) {
-               features.require("floats");
+               features.require("float");
        }
-       
+
        features.useFloat(floatType_);
        InsetCollapsable::validate(features);
 }
@@ -208,6 +228,7 @@ string const InsetFloat::editMessage() const
 int InsetFloat::latex(Buffer const * buf,
                      ostream & os, bool fragile, bool fp) const
 {
+       FloatList const & floats = buf->params.getLyXTextClass().floats();
        string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
        // Figure out the float placement to use.
        // From lowest to highest:
@@ -216,34 +237,40 @@ int InsetFloat::latex(Buffer const * buf,
        // - specific float placement
        string placement;
        string const buf_placement = buf->params.float_placement;
-       string const def_placement = floatList.defaultPlacement(floatType_);
+       string const def_placement = floats.defaultPlacement(floatType_);
        if (!floatPlacement_.empty()
            && floatPlacement_ != def_placement) {
                placement = floatPlacement_;
-       } else if (!buf_placement.empty()
+       } else if (floatPlacement_.empty() 
+                  && !buf_placement.empty()
                   && buf_placement != def_placement) {
                placement = buf_placement;
        }
-       
-       os << "\\begin{" << tmptype << "}";
+
+       // The \n is used to force \begin{<floatname>} to appear in a new line.
+       // The % is needed to prevent two consecutive \n chars in the case
+       // when the current output line is empty.
+       os << "%\n\\begin{" << tmptype << "}";
        // We only output placement if different from the def_placement.
        if (!placement.empty()) {
                os << "[" << placement << "]";
        }
-       
-       os << "%\n";
-    
+       os << "\n";
+
        int const i = inset.latex(buf, os, fragile, fp);
-       os << "\\end{" << tmptype << "}%\n";
-       
-       return i + 2;
+
+       // 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.
+       os << "\n\\end{" << tmptype << "}\n";
+
+       return i + 4;
 }
 
 
-int InsetFloat::docbook(Buffer const * buf, ostream & os) const
+int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
 {
        os << "<" << floatType_ << ">";
-       int const i = inset.docbook(buf, os);
+       int const i = inset.docbook(buf, os, mixcont);
        os << "</" << floatType_ << ">";
 
        return i;
@@ -265,13 +292,13 @@ bool InsetFloat::insetAllowed(Inset::Code code) const
 bool InsetFloat::showInsetDialog(BufferView * bv) const
 {
        if (!inset.showInsetDialog(bv)) {
-               bv->owner()->getDialogs()->showFloat(const_cast<InsetFloat *>(this)); 
+               bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
        }
        return true;
 }
 
 
-string const & InsetFloat::type() const 
+string const & InsetFloat::type() const
 {
        return floatType_;
 }
@@ -291,19 +318,17 @@ string const & InsetFloat::placement() const
 }
 
 
-void InsetFloat::wide(bool w)
+void InsetFloat::wide(bool w, BufferParams const & bp)
 {
        wide_ = w;
-       if (wide_) {
-               string lab(_("float:"));
-               lab += floatType_;
+
+       string lab(_("float:"));
+       lab += floatname(floatType_, bp);
+
+       if (wide_)
                lab += "*";
-               setLabel(lab);
-       } else {
-               string lab(_("float:"));
-               lab += floatType_;
-               setLabel(lab);
-       }
+
+       setLabel(lab);
 }
 
 
@@ -311,3 +336,23 @@ bool InsetFloat::wide() const
 {
        return wide_;
 }
+
+
+void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
+{
+       ParIterator pit(inset.paragraph());
+       ParIterator end;
+
+       // Find a caption layout in one of the (child inset's) pars
+       for (; pit != end; ++pit) {
+               Paragraph * tmp = *pit;
+               if (tmp->layout()->name() == caplayout) {
+                       string const str =
+                               tostr(toclist[type()].size() + 1)
+                               + ". " + tmp->asString(buf, false);
+                       toc::TocItem const item(tmp, 0 , str);
+                       toclist[type()].push_back(item);
+               }
+       }
+}