]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetFloat.cpp
This optional argument to the InsetCollapsable constructor
[lyx.git] / src / insets / InsetFloat.cpp
index 370a29d16d502ffed22d0f08d92c5ef2d8ed2bff..0a537598697556e6d393cdc038aed8aaafefb1e0 100644 (file)
 #include "ParIterator.h"
 #include "TextClass.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
+#include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/docstream.h"
-#include "support/convert.h"
+
+#include "frontends/Application.h"
 
 using namespace std;
 
-namespace lyx {
 
+namespace lyx {
 
 // 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,17 +113,27 @@ namespace lyx {
 // 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;
 }
 
 
 InsetFloat::~InsetFloat()
 {
-       InsetFloatMailer(*this).hideDialog();
+       hideDialogs("float", this);
+}
+
+
+docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
+{
+       if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen())
+               return docstring();
+
+       OutputParams rp(&buffer().params().encoding());
+       return getCaptionText(rp);
 }
 
 
@@ -131,29 +143,23 @@ void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_INSET_MODIFY: {
                InsetFloatParams params;
-               InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
-               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;
-       }
+               string2params(to_utf8(cmd.argument()), 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;
+                       setWide(params_.wide, cur.buffer().params(), false);
+                       setSideways(params_.sideways, cur.buffer().params(), false);
+               }
 
-       case LFUN_INSET_DIALOG_UPDATE: {
-               InsetFloatMailer(*this).updateDialog(&cur.bv());
+               setNewLabel(cur.buffer().params());
                break;
        }
 
-       case LFUN_MOUSE_RELEASE: {
-               if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
-                       if (params_.subfloat)
-                               break;
-                       InsetFloatMailer(*this).showDialog(&cur.bv());
-                       break;
-               }
-               InsetCollapsable::doDispatch(cur, cmd);
+       case LFUN_INSET_DIALOG_UPDATE: {
+               cur.bv().updateDialog("float", params2string(params()));
                break;
        }
 
@@ -171,7 +177,7 @@ bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
 
        case LFUN_INSET_MODIFY:
        case LFUN_INSET_DIALOG_UPDATE:
-               flag.enabled(true);
+               flag.setEnabled(true);
                return true;
 
        default:
@@ -182,15 +188,16 @@ bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetFloat::updateLabels(ParIterator const & it)
 {
-       Counters & cnts = buffer().params().documentClass().counters();
+       Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
        string const saveflt = cnts.current_float();
        bool const savesubflt = cnts.isSubfloat();
 
-       bool const subflt = it.innerInsetOfType(FLOAT_CODE);
+       bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
+                            || it.innerInsetOfType(WRAP_CODE));
        // floats can only embed subfloats of their own kind
        if (subflt)
                params_.type = saveflt;
-       subfloat(subflt, buffer().params());
+       setSubfloat(subflt, buffer().params());
 
        // Tell to captions what the current float is
        cnts.current_float(params().type);
@@ -225,32 +232,11 @@ void InsetFloatParams::write(ostream & os) const
 
 void InsetFloatParams::read(Lexer & lex)
 {
-       string token;
-       lex >> token;
-       if (token == "placement") {
+       lex.setContext("InsetFloatParams::read");
+       if (lex.checkFor("placement"))
                lex >> placement;
-       } else {
-               // take countermeasures
-               lex.pushToken(token);
-       }
-       lex >> token;
-       if (token == "wide") {
-               lex >> wide;
-       } else {
-               lyxerr << "InsetFloat::Read:: Missing wide!"
-               << endl;
-               // take countermeasures
-               lex.pushToken(token);
-       }
-       lex >> token;
-       if (token == "sideways") {
-               lex >> sideways;
-       } else {
-               lyxerr << "InsetFloat::Read:: Missing sideways!"
-               << endl;
-               // take countermeasures
-               lex.pushToken(token);
-       }
+       lex >> "wide" >> wide;
+       lex >> "sideways" >> sideways;
 }
 
 
@@ -264,18 +250,17 @@ void InsetFloat::write(ostream & os) const
 void InsetFloat::read(Lexer & lex)
 {
        params_.read(lex);
-       wide(params_.wide, buffer().params());
-       sideways(params_.sideways, buffer().params());
-       subfloat(params_.subfloat, buffer().params());
+       setWide(params_.wide, buffer().params());
+       setSideways(params_.sideways, buffer().params());
+       setSubfloat(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("rotfloat");
@@ -288,12 +273,6 @@ void InsetFloat::validate(LaTeXFeatures & features) const
 }
 
 
-Inset * InsetFloat::clone() const
-{
-       return new InsetFloat(*this);
-}
-
-
 docstring InsetFloat::editMessage() const
 {
        return _("Opened Float Inset");
@@ -399,37 +378,51 @@ bool InsetFloat::insetAllowed(InsetCode code) const
 bool InsetFloat::showInsetDialog(BufferView * bv) const
 {
        if (!InsetText::showInsetDialog(bv))
-               InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
+               bv->showDialog("float", params2string(params()),
+                       const_cast<InsetFloat *>(this));
        return true;
 }
 
 
-void InsetFloat::wide(bool w, BufferParams const & bp)
+void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
 {
        params_.wide = w;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (params_.wide)
-               lab += '*';
-       setLabel(lab);
+       if (update_label)
+               setNewLabel(bp);
 }
 
 
-void InsetFloat::sideways(bool s, BufferParams const & bp)
+void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
 {
        params_.sideways = s;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (params_.sideways)
-               lab += _(" (sideways)");
-       setLabel(lab);
+       if (update_label)
+               setNewLabel(bp);
 }
 
 
-void InsetFloat::subfloat(bool s, BufferParams const & bp)
+void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
 {
        params_.subfloat = s;
-       docstring lab = _("float: ") + floatName(params_.type, bp);
-       if (s)
-               lab = _("subfloat: ") + floatName(params_.type, bp);
+       if (update_label)
+               setNewLabel(bp);
+}
+
+
+void InsetFloat::setNewLabel(BufferParams const & bp)
+{
+       docstring lab = _("float: ");
+
+       if( params_.subfloat )
+               lab = _("subfloat: ");
+
+       lab += floatName(params_.type, bp);
+
+       if (params_.wide)
+               lab += '*';
+
+       if (params_.sideways)
+               lab += _(" (sideways)");
+
        setLabel(lab);
 }
 
@@ -460,52 +453,49 @@ docstring InsetFloat::getCaption(OutputParams const & runparams) const
 }
 
 
-string const InsetFloatMailer::name_("float");
-
-InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
-       : inset_(inset)
-{}
-
-
-string const InsetFloatMailer::inset2string(Buffer const &) const
+docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
 {
-       return params2string(inset_.params());
+       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();
 }
 
 
-void InsetFloatMailer::string2params(string const & in,
-                                    InsetFloatParams & params)
+void InsetFloat::string2params(string const & in, InsetFloatParams & params)
 {
        params = InsetFloatParams();
        if (in.empty())
                return;
 
        istringstream data(in);
-       Lexer lex(0,0);
+       Lexer lex;
        lex.setStream(data);
-
-       string name;
-       lex >> name;
-       if (!lex || name != name_)
-               return print_mailer_error("InsetFloatMailer", in, 1, name_);
-
-       // This is part of the inset proper that is usually swallowed
-       // by Text::readInset
-       string id;
-       lex >> id;
-       if (!lex || id != "Float")
-               return print_mailer_error("InsetBoxMailer", in, 2, "Float");
-
-       // We have to read the type here!
-       lex >> params.type;
+       lex.setContext("InsetFloat::string2params");
+       lex >> "float" >> "Float";
+       lex >> params.type; // We have to read the type here!
        params.read(lex);
 }
 
 
-string const InsetFloatMailer::params2string(InsetFloatParams const & params)
+string InsetFloat::params2string(InsetFloatParams const & params)
 {
        ostringstream data;
-       data << name_ << ' ';
+       data << "float" << ' ';
        params.write(data);
        return data.str();
 }