]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetVSpace.cpp
Do not reset custom space values if accessed via context menu (#8847)
[lyx.git] / src / insets / InsetVSpace.cpp
index 2444f675310d91c1e0742f92fcb8bbc17b59839c..f621172fe2b5ba28475212002d26c7250fbdf6c7 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author various
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "InsetVSpace.h"
 
 #include "Buffer.h"
+#include "BufferView.h"
 #include "Cursor.h"
 #include "Dimension.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
-#include "support/gettext.h"
+#include "FuncStatus.h"
 #include "Lexer.h"
-#include "Text.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
+#include "output_xhtml.h"
+#include "Text.h"
+
+#include "support/debug.h"
+#include "support/docstream.h"
+#include "support/gettext.h"
+#include "support/lassert.h"
 
+#include "frontends/Application.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include <sstream>
 
+using namespace std;
 
 namespace lyx {
 
-using std::istringstream;
-using std::ostream;
-using std::ostringstream;
-using std::string;
-using std::max;
-
-
 namespace {
 
 int const ADD_TO_VSPACE_WIDTH = 5;
@@ -47,36 +49,25 @@ int const ADD_TO_VSPACE_WIDTH = 5;
 
 
 InsetVSpace::InsetVSpace(VSpace const & space)
-       : space_(space)
+       : Inset(0), space_(space)
 {}
 
 
-InsetVSpace::~InsetVSpace()
-{
-       InsetVSpaceMailer(*this).hideDialog();
-}
-
-
-Inset * InsetVSpace::clone() const
-{
-       return new InsetVSpace(*this);
-}
-
-
 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       switch (cmd.action) {
+       switch (cmd.action()) {
 
        case LFUN_INSET_MODIFY: {
-               InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
+               cur.recordUndo();
+               string arg = to_utf8(cmd.argument());
+               if (arg == "vspace custom")
+                       arg = (space_.kind() == VSpace::LENGTH)
+                               ? "vspace " + space_.length().asString()
+                               : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
+               InsetVSpace::string2params(arg, space_);
                break;
        }
 
-       case LFUN_MOUSE_RELEASE:
-               if (!cur.selection())
-                       InsetVSpaceMailer(*this).showDialog(&cur.bv());
-               break;
-
        default:
                Inset::doDispatch(cur, cmd);
                break;
@@ -84,23 +75,43 @@ void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
-void InsetVSpace::read(Buffer const &, Lexer & lex)
+bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
+       FuncStatus & status) const
 {
-       BOOST_ASSERT(lex.isOK());
+       switch (cmd.action()) {
+       // we handle these
+       case LFUN_INSET_MODIFY:
+               if (cmd.getArg(0) == "vspace") {
+                       VSpace vspace;
+                       string arg = to_utf8(cmd.argument());
+                       if (arg == "vspace custom")
+                               arg = (space_.kind() == VSpace::LENGTH)
+                               ? "vspace " + space_.length().asString()
+                               : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
+                       InsetVSpace::string2params(arg, vspace);
+                       status.setOnOff(vspace == space_);
+               } 
+               status.setEnabled(true);
+               return true;
+       
+       default:
+               return Inset::getStatus(cur, cmd, status);
+       }
+}
+
+
+void InsetVSpace::read(Lexer & lex)
+{
+       LASSERT(lex.isOK(), return);
        string vsp;
        lex >> vsp;
        if (lex)
                space_ = VSpace(vsp);
-
-       string end_token;
-       lex >> end_token;
-       if (end_token != "\\end_inset")
-               lex.printError("Missing \\end_inset at this point. "
-                              "Read: `$$Token'");
+       lex >> "\\end_inset";
 }
 
 
-void InsetVSpace::write(Buffer const &, ostream & os) const
+void InsetVSpace::write(ostream & os) const
 {
        os << "VSpace " << space_.asLyXCommand();
 }
@@ -114,13 +125,13 @@ docstring const InsetVSpace::label() const
 
 
 namespace {
-int const arrow_size = 4;
+int const vspace_arrow_size = 4;
 }
 
 
 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       int height = 3 * arrow_size;
+       int height = 3 * vspace_arrow_size;
        if (space_.length().len().value() >= 0.0)
                height = max(height, space_.inPixels(*mi.base.bv));
 
@@ -137,7 +148,7 @@ void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
 
        dim.asc = height / 2 + (a - d) / 2; // align cursor with the
        dim.des = height - dim.asc;         // label text
-       dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
+       dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
        // Cache the inset dimension. 
        setDimCache(mi, dim);
 }
@@ -162,14 +173,14 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
                // adding or removing space
                bool const added = space_.kind() != VSpace::LENGTH ||
                                   space_.length().len().value() >= 0.0;
-               ty1 = added ? (start + arrow_size) : start;
-               ty2 = added ? start : (start + arrow_size);
-               by1 = added ? (end - arrow_size) : end;
-               by2 = added ? end : (end - arrow_size);
+               ty1 = added ? (start + vspace_arrow_size) : start;
+               ty2 = added ? start : (start + vspace_arrow_size);
+               by1 = added ? (end - vspace_arrow_size) : end;
+               by2 = added ? end : (end - vspace_arrow_size);
        }
 
-       int const midx = x + arrow_size;
-       int const rightx = midx + arrow_size;
+       int const midx = x + vspace_arrow_size;
+       int const rightx = midx + vspace_arrow_size;
 
        // first the string
        int w = 0;
@@ -183,7 +194,7 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
        docstring const lab = label();
        theFontMetrics(font).rectText(lab, w, a, d);
 
-       pi.pain.rectText(x + 2 * arrow_size + 5,
+       pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
                         start + (end - start) / 2 + (a - d) / 2,
                         lab, font, Color_none, Color_none);
 
@@ -200,70 +211,62 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-int InsetVSpace::latex(Buffer const & buf, odocstream & os,
-                      OutputParams const &) const
+void InsetVSpace::latex(otexstream & os, OutputParams const &) const
 {
-       os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
-       return 1;
+       os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
 }
 
 
-int InsetVSpace::plaintext(Buffer const &, odocstream & os,
-                          OutputParams const &) const
+int InsetVSpace::plaintext(odocstringstream & os,
+        OutputParams const &, size_t) const
 {
        os << "\n\n";
        return PLAINTEXT_NEWLINE;
 }
 
 
-int InsetVSpace::docbook(Buffer const &, odocstream & os,
-                        OutputParams const &) const
+int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
 {
        os << '\n';
        return 1;
 }
 
 
-string const InsetVSpaceMailer::name_ = "vspace";
-
-
-InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
-       : inset_(inset)
-{}
+docstring InsetVSpace::xhtml(XHTMLStream &, OutputParams const &) const
+{
+       odocstringstream ods;
+       XHTMLStream xds(ods);
+       string const len = space_.asHTMLLength();
+       string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
+       xds << html::StartTag("div", attr, true) << html::EndTag("div");
+       return ods.str();
+}
 
 
-string const InsetVSpaceMailer::inset2string(Buffer const &) const
+string InsetVSpace::contextMenuName() const
 {
-       return params2string(inset_.space());
+       return "context-vspace";
 }
 
 
-void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
+void InsetVSpace::string2params(string const & in, VSpace & vspace)
 {
        vspace = VSpace();
        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("InsetVSpaceMailer", in, 1, name_);
-
-       string vsp;
-       lex >> vsp;
-       if (lex)
-               vspace = VSpace(vsp);
+       lex.setContext("InsetVSpace::string2params");
+       lex >> "vspace" >> vspace;
 }
 
 
-string const InsetVSpaceMailer::params2string(VSpace const & vspace)
+string InsetVSpace::params2string(VSpace const & vspace)
 {
        ostringstream data;
-       data << name_ << ' ' << vspace.asLyXCommand();
+       data << "vspace" << ' ' << vspace.asLyXCommand();
        return data.str();
 }