]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetSpace.cpp
Some master/child biblio fixes.
[lyx.git] / src / insets / InsetSpace.cpp
index 90d171e2d40570c6330fd308565d115735d243f0..6593d045deee24cb593fcd2a01ac346fb9517844 100644 (file)
@@ -27,6 +27,7 @@
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "output_xhtml.h"
+#include "texstream.h"
 
 #include "support/debug.h"
 #include "support/docstream.h"
@@ -146,10 +147,16 @@ void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action()) {
 
-       case LFUN_INSET_MODIFY:
+       case LFUN_INSET_MODIFY: {
                cur.recordUndo();
-               string2params(to_utf8(cmd.argument()), params_);
+               string arg = to_utf8(cmd.argument());
+               if (arg == "space \\hspace{}")
+                       arg += params_.length.len().empty()
+                               ? " \\length 1" + string(stringFromUnit(Length::defaultUnit()))
+                               : " \\length " + params_.length.asString();
+               string2params(arg, params_);
                break;
+       }
 
        case LFUN_INSET_DIALOG_UPDATE:
                cur.bv().updateDialog("space", params2string(params()));
@@ -172,7 +179,7 @@ bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
                        InsetSpaceParams params;
                        string2params(to_utf8(cmd.argument()), params);
                        status.setOnOff(params_.kind == params.kind);
-                       status.setEnabled(true);        
+                       status.setEnabled(true);
                } else
                        status.setEnabled(false);
                return true;
@@ -193,29 +200,31 @@ int const arrow_size = 8;
 
 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       if (isStretchableSpace()) {
-               // The metrics for this kinds are calculated externally in
-               // \c TextMetrics::computeRowMetrics. Those are dummy value:
-               dim = Dimension(10, 10, 10);
+       if (isHfill()) {
+               // The width for hfills is calculated externally in
+               // TextMetrics::computeRowMetrics. The value of 5 is the
+               // minimal value when the hfill is not active.
+               dim = Dimension(5, 10, 10);
                return;
        }
 
        frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
        dim.asc = fm.maxAscent();
        dim.des = fm.maxDescent();
+       int const em = fm.em();
 
        switch (params_.kind) {
                case InsetSpaceParams::THIN:
                case InsetSpaceParams::NEGTHIN:
-                       dim.wid = fm.width(char_type('M')) / 6;
+                       dim.wid = em / 6;
                        break;
                case InsetSpaceParams::MEDIUM:
                case InsetSpaceParams::NEGMEDIUM:
-                       dim.wid = fm.width(char_type('M')) / 4;
+                       dim.wid = em / 4;
                        break;
                case InsetSpaceParams::THICK:
                case InsetSpaceParams::NEGTHICK:
-                       dim.wid = fm.width(char_type('M')) / 2;
+                       dim.wid = em / 2;
                        break;
                case InsetSpaceParams::PROTECTED:
                case InsetSpaceParams::VISIBLE:
@@ -223,20 +232,19 @@ void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
                        dim.wid = fm.width(char_type(' '));
                        break;
                case InsetSpaceParams::QUAD:
-                       dim.wid = fm.width(char_type('M'));
+                       dim.wid = em;
                        break;
                case InsetSpaceParams::QQUAD:
-                       dim.wid = 2 * fm.width(char_type('M'));
+                       dim.wid = 2 * em;
                        break;
                case InsetSpaceParams::ENSPACE:
                case InsetSpaceParams::ENSKIP:
-                       dim.wid = int(0.5 * fm.width(char_type('M')));
+                       dim.wid = int(0.5 * em);
                        break;
                case InsetSpaceParams::CUSTOM:
                case InsetSpaceParams::CUSTOM_PROTECTED: {
-                       int const w = 
-                               params_.length.len().inPixels(mi.base.textwidth,
-                                                       fm.width(char_type('M')));
+                       int const w =
+                               params_.length.len().inPixels(mi.base);
                        int const minw = (w < 0) ? 3 * arrow_size : 4;
                        dim.wid = max(minw, abs(w));
                        break;
@@ -252,8 +260,6 @@ void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
                        // shut up compiler
                        break;
        }
-       // Cache the inset dimension.
-       setDimCache(mi, dim);
 }
 
 
@@ -261,7 +267,7 @@ void InsetSpace::draw(PainterInfo & pi, int x, int y) const
 {
        Dimension const dim = dimension(*pi.base.bv);
 
-       if (isStretchableSpace() || params_.length.len().value() < 0) {
+       if (isHfill() || params_.length.len().value() < 0) {
                int const asc = theFontMetrics(pi.base.font).ascent('M');
                int const desc = theFontMetrics(pi.base.font).descent('M');
                // Pixel height divisible by 2 for prettier fill graphics:
@@ -381,7 +387,6 @@ void InsetSpace::draw(PainterInfo & pi, int x, int y) const
 
 void InsetSpaceParams::write(ostream & os) const
 {
-       string command;
        switch (kind) {
        case InsetSpaceParams::NORMAL:
                os << "\\space{}";
@@ -453,7 +458,7 @@ void InsetSpaceParams::write(ostream & os) const
                os <<  "\\hspace*{}";
                break;
        }
-       
+
        if (!length.len().empty())
                os << "\n\\length " << length.asString();
 }
@@ -621,7 +626,8 @@ void InsetSpace::latex(otexstream & os, OutputParams const & runparams) const
 }
 
 
-int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
+int InsetSpace::plaintext(odocstringstream & os,
+        OutputParams const &, size_t) const
 {
        switch (params_.kind) {
        case InsetSpaceParams::HFILL:
@@ -665,13 +671,17 @@ int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
                os.put(0x2003);
                return 2;
        case InsetSpaceParams::THIN:
-               os.put(0x2009);
+               os.put(0x202f);
                return 1;
        case InsetSpaceParams::MEDIUM:
+               os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable medium space breakable
                os.put(0x2005);
+               os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable medium space breakable
                return 1;
        case InsetSpaceParams::THICK:
+               os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable thick space breakable
                os.put(0x2004);
+               os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable thick space breakable
                return 1;
        case InsetSpaceParams::PROTECTED:
        case InsetSpaceParams::CUSTOM_PROTECTED:
@@ -692,24 +702,39 @@ int InsetSpace::docbook(odocstream & os, OutputParams const &) const
 {
        switch (params_.kind) {
        case InsetSpaceParams::NORMAL:
+               os << " ";
+               break;
        case InsetSpaceParams::QUAD:
+               os << "&emsp;";
+               break;
        case InsetSpaceParams::QQUAD:
+               os << "&emsp;&emsp;";
+               break;
        case InsetSpaceParams::ENSKIP:
-               os << " ";
+               os << "&ensp;";
                break;
-       // FIXME For spaces and dashes look here:
-       // http://oreilly.com/catalog/docbook/book2/iso-pub.html
        case InsetSpaceParams::PROTECTED:
-       // FIXME &blank; ?
+               os << "&nbsp;";
+               break;
        case InsetSpaceParams::VISIBLE:
+               os << "&#x2423;";
+               break;
        case InsetSpaceParams::ENSPACE:
-       // FIXME &thinsp; ?
+               os << "&#x2060;&ensp;&#x2060;";
+               break;
        case InsetSpaceParams::THIN:
+               os << "&thinsp;";
+               break;
        case InsetSpaceParams::MEDIUM:
+               os << "&emsp14;";
+               break;
        case InsetSpaceParams::THICK:
+               os << "&emsp13;";
+               break;
        case InsetSpaceParams::NEGTHIN:
        case InsetSpaceParams::NEGMEDIUM:
        case InsetSpaceParams::NEGTHICK:
+               // FIXME
                os << "&nbsp;";
                break;
        case InsetSpaceParams::HFILL:
@@ -813,18 +838,20 @@ void InsetSpace::validate(LaTeXFeatures & features) const
 
 void InsetSpace::toString(odocstream & os) const
 {
-       plaintext(os, OutputParams(0));
+       odocstringstream ods;
+       plaintext(ods, OutputParams(0));
+       os << ods.str();
 }
 
 
-void InsetSpace::forToc(docstring & os, size_t) const
+void InsetSpace::forOutliner(docstring & os, size_t const, bool const) const
 {
        // There's no need to be cute here.
        os += " ";
 }
 
 
-bool InsetSpace::isStretchableSpace() const
+bool InsetSpace::isHfill() const
 {
        return params_.kind == InsetSpaceParams::HFILL
                || params_.kind == InsetSpaceParams::HFILL_PROTECTED
@@ -859,7 +886,8 @@ void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
                params.math = true;
        else {
                params.math = false;
-               LASSERT(name == "space", /**/);
+               // we can try to read this even if the name is wrong
+               LATTEST(name == "space");
        }
 
        // There are cases, such as when we are called via getStatus() from