]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetText.cpp
Wrap rotated cells with linebreaks in varwidth
[lyx.git] / src / insets / InsetText.cpp
index a733588ef25f4097bcd5a9268b56967977e9db74..0f4619a99470fa24b6ab3b3aa068420f5e08fa65 100644 (file)
@@ -220,11 +220,19 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
        int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
        int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
        bool change_drawn = false;
-       if (drawFrame_ || pi.full_repaint) {
-               if (pi.full_repaint)
+       if (pi.full_repaint)
                        pi.pain.fillRectangle(xframe, yframe, w, h,
                                pi.backgroundColor(this));
 
+       {
+               Changer dummy = make_change(pi.background_color,
+                                           pi.backgroundColor(this, false));
+               // The change tracking cue must not be inherited
+               Changer dummy2 = make_change(pi.change_, Change());
+               tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
+       }
+
+       if (drawFrame_) {
                // Change color of the frame in tracked changes, like for tabulars.
                // Only do so if the color is not custom. But do so even if RowPainter
                // handles the strike-through already.
@@ -242,16 +250,9 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
                        change_drawn = true;
                } else
                        c = frameColor();
-               if (drawFrame_)
-                       pi.pain.rectangle(xframe, yframe, w, h, c);
-       }
-       {
-               Changer dummy = make_change(pi.background_color,
-                                           pi.backgroundColor(this, false));
-               // The change tracking cue must not be inherited
-               Changer dummy2 = make_change(pi.change_, Change());
-               tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
+               pi.pain.rectangle(xframe, yframe, w, h, c);
        }
+
        if (canPaintChange(*pi.base.bv) && (!change_drawn || pi.change_.deleted()))
                // Do not draw the change tracking cue if already done by RowPainter and
                // do not draw the cue for INSERTED if the information is already in the
@@ -360,7 +361,6 @@ bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
 
        case LFUN_ARGUMENT_INSERT: {
                string const arg = cmd.getArg(0);
-               bool const force = cmd.getArg(1) == "force";
                if (arg.empty()) {
                        status.setEnabled(false);
                        return true;
@@ -375,7 +375,7 @@ bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
                        for (Paragraph const & par : paragraphs())
                                for (auto const & table : par.insetList())
                                        if (InsetArgument const * ins = table.inset->asInsetArgument())
-                                               if (ins->name() == arg && !force) {
+                                               if (ins->name() == arg) {
                                                        // we have this already
                                                        status.setEnabled(false);
                                                        return true;
@@ -459,7 +459,9 @@ void InsetText::latex(otexstream & os, OutputParams const & runparams) const
                        // FIXME UNICODE
                        // FIXME \protect should only be used for fragile
                        //    commands, but we do not provide this information yet.
-                       if (runparams.moving_arg)
+                       if (hasCProtectContent(runparams.moving_arg))
+                               os << "\\cprotect";
+                       else if (runparams.moving_arg)
                                os << "\\protect";
                        os << '\\' << from_utf8(il.latexname());
                        if (!il.latexargs().empty())
@@ -761,6 +763,19 @@ ParagraphList & InsetText::paragraphs()
 }
 
 
+bool InsetText::hasCProtectContent(bool const fragile) const
+{
+       ParagraphList const & pars = paragraphs();
+       pit_type pend = paragraphs().size();
+       for (pit_type pit = 0; pit != pend; ++pit) {
+               Paragraph const & par = pars[pit];
+               if (par.needsCProtection(fragile))
+                       return true;
+       }
+       return false;
+}
+
+
 bool InsetText::insetAllowed(InsetCode code) const
 {
        switch (code) {
@@ -1071,4 +1086,50 @@ InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOp
        return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);
 }
 
+
+bool InsetText::needsCProtection(bool const maintext, bool const fragile) const
+{
+       // Nested cprotect content needs \cprotect
+       // on each level
+       if (producesOutput() && hasCProtectContent(fragile))
+               return true;
+
+       // Environments generally need cprotection in fragile context
+       if (fragile && getLayout().latextype() == InsetLayout::ENVIRONMENT)
+               return true;
+
+       if (!getLayout().needsCProtect())
+               return false;
+
+       // Environments and "no latex" types (e.g., knitr chunks)
+       // need cprotection regardless the content
+       if (!maintext && getLayout().latextype() != InsetLayout::COMMAND)
+               return true;
+
+       // If the inset does not produce output (e.g. Note or Branch),
+       // we can ignore the contained paragraphs
+       if (!producesOutput())
+               return false;
+
+       // Commands need cprotection if they contain specific chars
+       int const nchars_escape = 9;
+       static char_type const chars_escape[nchars_escape] = {
+               '&', '_', '$', '%', '#', '^', '{', '}', '\\'};
+
+       ParagraphList const & pars = paragraphs();
+       pit_type pend = paragraphs().size();
+
+       for (pit_type pit = 0; pit != pend; ++pit) {
+               Paragraph const & par = pars[pit];
+               if (par.needsCProtection(fragile))
+                       return true;
+               docstring const pars = par.asString();
+               for (int k = 0; k < nchars_escape; k++) {
+                       if (contains(pars, chars_escape[k]))
+                               return true;
+               }
+       }
+       return false;
+}
+
 } // namespace lyx