]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetvspace.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / insetvspace.C
index 469d65589ddd720c898f0b2bdb1602cd88cebed6..afb221aff28cc25295def2fab9f87d1cf48b3e06 100644 (file)
@@ -14,7 +14,7 @@
 #include "insetvspace.h"
 
 #include "buffer.h"
-#include "BufferView.h"
+#include "cursor.h"
 #include "dispatchresult.h"
 #include "funcrequest.h"
 #include "gettext.h"
 #include "lyxtext.h"
 #include "metricsinfo.h"
 
-#include "frontends/font_metrics.h"
+#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
-#include "support/std_sstream.h"
+#include <sstream>
+
+
+namespace lyx {
 
 using std::istringstream;
 using std::ostream;
@@ -53,39 +56,45 @@ InsetVSpace::~InsetVSpace()
 }
 
 
-std::auto_ptr<InsetBase> InsetVSpace::clone() const
+std::auto_ptr<InsetBase> InsetVSpace::doClone() const
 {
        return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
 }
 
 
-DispatchResult
-InsetVSpace::priv_dispatch(FuncRequest const & cmd,
-                          idx_type & idx, pos_type & pos)
+void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               InsetVSpaceMailer::string2params(cmd.argument, space_);
-               return DispatchResult(true, true);
+               InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
+               break;
        }
 
-       case LFUN_MOUSE_PRESS:
-               InsetVSpaceMailer(*this).showDialog(cmd.view());
-               return DispatchResult(true, true);
+       case LFUN_MOUSE_RELEASE:
+               InsetVSpaceMailer(*this).showDialog(&cur.bv());
+               break;
 
        default:
-               return InsetOld::priv_dispatch(cmd, idx, pos);
+               InsetBase::doDispatch(cur, cmd);
+               break;
        }
 }
-               
+
 
 void InsetVSpace::read(Buffer const &, LyXLex & lex)
 {
-       if (lex.isOK()) {
-               lex.next();
-               space_ = VSpace(lex.getString());
-       }
+       BOOST_ASSERT(lex.isOK());
+       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'");
 }
 
 
@@ -95,58 +104,65 @@ void InsetVSpace::write(Buffer const &, ostream & os) const
 }
 
 
-void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
+docstring const InsetVSpace::label() const
 {
-       int size = 10;
-       int const arrow_size = 4;
-       int const space_size = space_.inPixels(*mi.base.bv);
+       static docstring const label = _("Vertical Space");
+       return label + " (" + space_.asGUIName() + ')';
+}
+
+
+namespace {
+int const arrow_size = 4;
+}
+
+
+bool InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       int height = 3 * arrow_size;
+       if (space_.length().len().value() >= 0.0)
+               height = max(height, space_.inPixels(*mi.base.bv));
 
        LyXFont font;
        font.decSize();
-       int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
+       font.decSize();
 
-       if (space_.length().len().value() < 0.0)
-               size = min_size;
-       else
-               size = max(min_size, space_size);
+       int w = 0;
+       int a = 0;
+       int d = 0;
+       theFontMetrics(font).rectText(label(), w, a, d);
 
-       dim.asc = size / 2;
-       dim.des = size / 2;
-       dim.wid = 10 + 2 * ADD_TO_VSPACE_WIDTH;
+       height = max(height, a + d);
 
+       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;
+       bool const changed = dim_ != dim;
        dim_ = dim;
+       return changed;
 }
 
 
 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
 {
-       static std::string const label = _("Vertical Space");
-
-       xo_ = x;
-       yo_ = y;
+       setPosCache(pi, x, y);
 
        x += ADD_TO_VSPACE_WIDTH;
 
-       int const arrow_size = 4;
        int const start = y - dim_.asc;
        int const end   = y + dim_.des;
 
-       // the label to display (if any)
-       string str;
        // y-values for top arrow
        int ty1, ty2;
        // y-values for bottom arrow
        int by1, by2;
 
-       str = label + " (" + space_.asLyXCommand() + ")";
-
        if (space_.kind() == VSpace::VFILL) {
                ty1 = ty2 = start;
                by1 = by2 = end;
        } else {
                // adding or removing space
                bool const added = space_.kind() != VSpace::LENGTH ||
-                                  space_.length().len().value() > 0.0;
+                                  space_.length().len().value() >= 0.0;
                ty1 = added ? (start + arrow_size) : start;
                ty2 = added ? start : (start + arrow_size);
                by1 = added ? (end - arrow_size) : end;
@@ -165,10 +181,12 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
        font.setColor(LColor::added_space);
        font.decSize();
        font.decSize();
-       font_metrics::rectText(str, font, w, a, d);
+       docstring const lab = label();
+       theFontMetrics(font).rectText(lab, w, a, d);
 
-       pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
-                      str, font, LColor::none, LColor::none);
+       pi.pain.rectText(x + 2 * arrow_size + 5,
+                        start + (end - start) / 2 + (a - d) / 2,
+                        lab, font, LColor::none, LColor::none);
 
        // top arrow
        pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
@@ -183,15 +201,15 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-int InsetVSpace::latex(Buffer const & buf, ostream & os,
+int InsetVSpace::latex(Buffer const & buf, odocstream & os,
                          OutputParams const &) const
 {
-       os << space_.asLatexCommand(buf.params()) << '\n';
+       os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
        return 1;
 }
 
 
-int InsetVSpace::plaintext(Buffer const &, ostream & os,
+int InsetVSpace::plaintext(Buffer const &, odocstream & os,
                           OutputParams const &) const
 {
        os << "\n\n";
@@ -199,15 +217,7 @@ int InsetVSpace::plaintext(Buffer const &, ostream & os,
 }
 
 
-int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
-                         OutputParams const &) const
-{
-       os << '\n';
-       return 1;
-}
-
-
-int InsetVSpace::docbook(Buffer const &, std::ostream & os,
+int InsetVSpace::docbook(Buffer const &, odocstream & os,
                         OutputParams const &) const
 {
        os << '\n';
@@ -225,14 +235,13 @@ InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
 
 string const InsetVSpaceMailer::inset2string(Buffer const &) const
 {
-       return params2string(inset_.space_);
+       return params2string(inset_.space());
 }
 
 
 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
 {
        vspace = VSpace();
-
        if (in.empty())
                return;
 
@@ -240,17 +249,15 @@ void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
        LyXLex lex(0,0);
        lex.setStream(data);
 
-       if (lex.isOK()) {
-               lex.next();
-               string const name = lex.getString();
-       }
+       string name;
+       lex >> name;
+       if (!lex || name != name_)
+               return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
 
-       // This is part of the inset proper that is usually swallowed
-       // by Buffer::readInset
-       if (lex.isOK()) {
-               lex.next();
-               vspace = VSpace(lex.getString());
-       }
+       string vsp;
+       lex >> vsp;
+       if (lex)
+               vspace = VSpace(vsp);
 }
 
 
@@ -260,3 +267,6 @@ string const InsetVSpaceMailer::params2string(VSpace const & vspace)
        data << name_ << ' ' << vspace.asLyXCommand();
        return data.str();
 }
+
+
+} // namespace lyx