]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetSpace.cpp
Move isMultiCell() to Cursor, and use it.
[lyx.git] / src / insets / InsetSpace.cpp
index 10ce34582b7073db2de1521e2996e2c8657a0158..c84e3d6f21fd911d1bae0ee0a93ccddb41b97779 100644 (file)
@@ -158,9 +158,8 @@ bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
                        InsetSpaceParams params;
                        string2params(to_utf8(cmd.argument()), params);
                        status.setOnOff(params_.kind == params.kind);
-               } else {
-                       status.enabled(true);
                }
+               status.setEnabled(true);
                return true;
        default:
                return Inset::getStatus(cur, cmd, status);
@@ -174,6 +173,11 @@ void InsetSpace::edit(Cursor & cur, bool, EntryDirection)
 }
 
 
+namespace {
+int const arrow_size = 8;
+}
+
+
 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        if (isStretchableSpace()) {
@@ -207,9 +211,12 @@ void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
                        dim.wid = int(0.5 * fm.width(char_type('M')));
                        break;
                case InsetSpaceParams::CUSTOM:
-               case InsetSpaceParams::CUSTOM_PROTECTED:
-                       dim.wid = params_.length.inBP();
+               case InsetSpaceParams::CUSTOM_PROTECTED: {
+                       int const minwidth = (params_.length.inBP() < 0)
+                               ? 3 * arrow_size : 4;
+                       dim.wid = max(minwidth, abs(params_.length.inBP()));
                        break;
+               }
                case InsetSpaceParams::HFILL:
                case InsetSpaceParams::HFILL_PROTECTED:
                case InsetSpaceParams::DOTFILL:
@@ -230,17 +237,33 @@ void InsetSpace::draw(PainterInfo & pi, int x, int y) const
 {
        Dimension const dim = dimension(*pi.base.bv);
 
-       if (isStretchableSpace()) {
+       if (isStretchableSpace() || params_.length.inBP() < 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:
+               int const oddheight = (asc ^ desc) & 1;
                int const x0 = x + 1;
                int const x1 = x + dim.wid - 2;
-               int const y0 = y + desc;
-               int const y1 = y - asc;
-               int const y2 = y - asc / 2;
-               int const xoffset = (y0 - y1) / 2;
+               int const y0 = y + desc - 1;
+               int const y1 = y - asc + oddheight - 1;
+               int const y2 = (y0 + y1) / 2;
+               int xoffset = (y0 - y1) / 2;
+
+               // Two tests for very narrow insets
+               if (xoffset > x1 - x0
+                    && (params_.kind == InsetSpaceParams::LEFTARROWFILL
+                        || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
+                               xoffset = x1 - x0;
+               if (xoffset * 6 > (x1 - x0)
+                    && (params_.kind == InsetSpaceParams::UPBRACEFILL
+                        || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
+                               xoffset = (x1 - x0) / 6;
+
                int const x2 = x0 + xoffset;
                int const x3 = x1 - xoffset;
+               int const xm = (x0 + x1) / 2;
+               int const xml = xm - xoffset;
+               int const xmr = xm + xoffset;
 
                if (params_.kind == InsetSpaceParams::HFILL) {
                        pi.pain.line(x0, y1, x0, y0, Color_added_space);
@@ -254,29 +277,47 @@ void InsetSpace::draw(PainterInfo & pi, int x, int y) const
                        pi.pain.line(x1, y1, x1, y0, Color_latex);
                } else if (params_.kind == InsetSpaceParams::DOTFILL) {
                        pi.pain.line(x0, y1, x0, y0, Color_special);
-                       pi.pain.line(x0, y, x1, y, Color_special,
+                       pi.pain.line(x0, y0, x1, y0, Color_special,
                                frontend::Painter::line_onoffdash);
                        pi.pain.line(x1, y1, x1, y0, Color_special);
                } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
                        pi.pain.line(x0, y1, x0, y0, Color_special);
-                       pi.pain.line(x0, y, x1, y, Color_special);
+                       pi.pain.line(x0, y0, x1, y0, Color_special);
                        pi.pain.line(x1, y1, x1, y0, Color_special);
                } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
-                       pi.pain.line(x2, y1 , x0, y2, Color_special);
-                       pi.pain.line(x0, y2 , x2, y0, Color_special);
+                       pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
+                       pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
                        pi.pain.line(x0, y2 , x1, y2, Color_special);
                } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
-                       pi.pain.line(x3, y1 , x1, y2, Color_special);
-                       pi.pain.line(x1, y2 , x3, y0, Color_special);
+                       pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
+                       pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
                        pi.pain.line(x0, y2 , x1, y2, Color_special);
                } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
-                       pi.pain.line(x0, y1 , x2, y2, Color_special);
-                       pi.pain.line(x3, y2 , x1, y1, Color_special);
-                       pi.pain.line(x2, y2 , x3, y2, Color_special);
+                       pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
+                       pi.pain.line(x2, y2 , xml, y2, Color_special);
+                       pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
+                       pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
+                       pi.pain.line(xmr, y2 , x3, y2, Color_special);
+                       pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
                } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
-                       pi.pain.line(x0, y0 , x2, y2, Color_special);
-                       pi.pain.line(x3, y2 , x1, y0, Color_special);
+                       pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
+                       pi.pain.line(x2, y2 , xml, y2, Color_special);
+                       pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
+                       pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
+                       pi.pain.line(xmr, y2 , x3, y2, Color_special);
+                       pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
+               } else if (params_.kind == InsetSpaceParams::CUSTOM) {
+                       pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_special);
+                       pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_special);
+                       pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_special);
+                       pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_special);
                        pi.pain.line(x2, y2 , x3, y2, Color_special);
+               } else if (params_.kind == InsetSpaceParams::CUSTOM_PROTECTED) {
+                       pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_latex);
+                       pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_latex);
+                       pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_latex);
+                       pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_latex);
+                       pi.pain.line(x2, y2 , x3, y2, Color_latex);
                }
                return;
        }
@@ -420,13 +461,12 @@ void InsetSpaceParams::read(Lexer & lex)
 
        if (lex.checkFor("\\length"))
                lex >> length;
-       lex >> "\\end_inset";
 }
 
 
 void InsetSpace::write(ostream & os) const
 {
-       os << "Space ";
+       os << "space ";
        params_.write(os);
 }
 
@@ -434,6 +474,7 @@ void InsetSpace::write(ostream & os) const
 void InsetSpace::read(Lexer & lex)
 {
        params_.read(lex);
+       lex >> "\\end_inset";
 }
 
 
@@ -611,7 +652,11 @@ void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
        lex.setContext("InsetSpace::string2params");
        lex >> "space";
 
-       params.read(lex);
+       // There are cases, such as when we are called via getStatus() from
+       // Dialog::canApply(), where we are just called with "space" rather
+       // than a full "space \type{}\n\\end_inset".
+       if (lex.isOK())
+               params.read(lex);
 }