]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetbox.C
* In the process of fixing the math background color bug, this commit transfer backgr...
[lyx.git] / src / insets / insetbox.C
index 27d41f401456529a5d9e74c749ba90fd855bee91..6e07db2cac7ef45c712d93b0eb10b0d2a07a0ffe 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "insetbox.h"
 
+#include "BufferView.h"
 #include "cursor.h"
 #include "dispatchresult.h"
 #include "debug.h"
 #include "lyxlex.h"
 #include "metricsinfo.h"
 #include "paragraph.h"
+#include "TextMetrics.h"
 
 #include "support/translator.h"
 
 #include <sstream>
 
+
+namespace lyx {
+
 using std::auto_ptr;
 using std::string;
 using std::istringstream;
@@ -41,8 +46,10 @@ using std::endl;
 namespace {
 
 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
+typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
 
-BoxTranslator const init_boxtranslator() {
+BoxTranslator const init_boxtranslator()
+{
        BoxTranslator translator("Boxed", InsetBox::Boxed);
        translator.addPair("Frameless", InsetBox::Frameless);
        translator.addPair("ovalbox", InsetBox::ovalbox);
@@ -53,8 +60,9 @@ BoxTranslator const init_boxtranslator() {
 }
 
 
-BoxTranslator const init_boxtranslator_loc() {
-       BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
+BoxTranslatorLoc const init_boxtranslator_loc()
+{
+       BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
        translator.addPair(_("Frameless"), InsetBox::Frameless);
        translator.addPair(_("ovalbox"), InsetBox::ovalbox);
        translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
@@ -64,14 +72,16 @@ BoxTranslator const init_boxtranslator_loc() {
 }
 
 
-BoxTranslator const & boxtranslator() {
+BoxTranslator const & boxtranslator()
+{
        static BoxTranslator translator = init_boxtranslator();
        return translator;
 }
 
 
-BoxTranslator const & boxtranslator_loc() {
-       static BoxTranslator translator = init_boxtranslator_loc();
+BoxTranslatorLoc const & boxtranslator_loc()
+{
+       static BoxTranslatorLoc translator = init_boxtranslator_loc();
        return translator;
 }
 
@@ -80,7 +90,7 @@ BoxTranslator const & boxtranslator_loc() {
 
 void InsetBox::init()
 {
-       setInsetName("Box");
+       setInsetName(from_ascii("Box"));
        setButtonLabel();
 }
 
@@ -111,7 +121,7 @@ auto_ptr<InsetBase> InsetBox::doClone() const
 }
 
 
-string const InsetBox::editMessage() const
+docstring const InsetBox::editMessage() const
 {
        return _("Opened Box Inset");
 }
@@ -139,13 +149,20 @@ void InsetBox::setButtonLabel()
        font.decSize();
 
        BoxType btype = boxtranslator().find(params_.type);
+
+       docstring label;
+       label += _("Box");
+       label += " (";
        if (btype == Frameless) {
                if (params_.use_parbox)
-                       setLabel(_("Box") + " (" + _("Parbox") + ")");
+                       label += _("Parbox");
                else
-                       setLabel(_("Box") + " (" + _("Minipage") + ")");
+                       label += _("Minipage");
        } else
-               setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
+               label += boxtranslator_loc().find(btype);
+       label += ")";
+
+       setLabel(label);
 
        font.setColor(LColor::foreground);
        setBackgroundColor(LColor::background);
@@ -153,14 +170,32 @@ void InsetBox::setButtonLabel()
 }
 
 
-void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
+bool InsetBox::hasFixedWidth() const
+{
+      return params_.inner_box || params_.special != "width";
+}
+
+
+bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
 {
        MetricsInfo mi = m;
-       mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
+       // first round in order to know the minimum size.
+       InsetCollapsable::metrics(mi, dim);
+       TextMetrics & tm = mi.base.bv->textMetrics(&text_);
+       if (hasFixedWidth())
+               mi.base.textwidth =
+                       std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
+                                params_.width.inPixels(m.base.textwidth));
        InsetCollapsable::metrics(mi, dim);
-       //if (params_.inner_box && isOpen())
-       //      dim.wid = mi.base.textwidth;
+       bool const changed = dim_ != dim;
        dim_ = dim;
+       return changed;
+}
+
+
+bool InsetBox::forceDefaultParagraphs(idx_type) const
+{
+       return !params_.inner_box;
 }
 
 
@@ -176,8 +211,8 @@ void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               lyxerr << "InsetBox::dispatch MODIFY" << endl;
-               InsetBoxMailer::string2params(cmd.argument, params_);
+               //lyxerr << "InsetBox::dispatch MODIFY" << endl;
+               InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
                setButtonLabel();
                break;
        }
@@ -210,6 +245,13 @@ bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_INSET_DIALOG_UPDATE:
                flag.enabled(true);
                return true;
+       case LFUN_BREAK_PARAGRAPH:
+               if (params_.inner_box) {
+                       return InsetCollapsable::getStatus(cur, cmd, flag);
+               } else {
+                       flag.enabled(false);
+                       return true;
+               }
 
        default:
                return InsetCollapsable::getStatus(cur, cmd, flag);
@@ -217,8 +259,8 @@ bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
 }
 
 
-int InsetBox::latex(Buffer const & buf, ostream & os,
-                               OutputParams const & runparams) const
+int InsetBox::latex(Buffer const & buf, odocstream & os,
+                    OutputParams const & runparams) const
 {
        BoxType btype = boxtranslator().find(params_.type);
 
@@ -264,11 +306,14 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
                if (!params_.inner_box) {
                        os << "{\\makebox";
                        // Special widths, see usrguide ยง3.5
+                       // FIXME UNICODE
                        if (params_.special != "none") {
                                os << "[" << params_.width.value()
-                                  << "\\" << params_.special << "]";
+                                  << '\\' << from_utf8(params_.special)
+                                  << ']';
                        } else
-                               os << "[" << width_string << "]";
+                               os << '[' << from_ascii(width_string)
+                                  << ']';
                        if (params_.hor_pos != 'c')
                                os << "[" << params_.hor_pos << "]";
                }
@@ -297,16 +342,21 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
 
                os << "[" << params_.pos << "]";
                if (params_.height_special == "none") {
-                       os << "[" << params_.height.asLatexString() << "]";
+                       // FIXME UNICODE
+                       os << '[' << from_ascii(params_.height.asLatexString())
+                          << ']';
                } else {
                        // Special heights
+                       // FIXME UNICODE
                        os << "[" << params_.height.value()
-                          << "\\" << params_.height_special << "]";
+                          << '\\' << from_utf8(params_.height_special)
+                          << ']';
                }
                if (params_.inner_pos != params_.pos)
                        os << "[" << params_.inner_pos << "]";
 
-               os << "{" << width_string << "}";
+               // FIXME UNICODE
+               os << '{' << from_ascii(width_string) << '}';
 
                if (params_.use_parbox)
                        os << "{";
@@ -346,61 +396,57 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
 }
 
 
-int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
-                      OutputParams const & runparams) const
-{
-       return InsetText::linuxdoc(buf, os, runparams);
-}
-
-
-int InsetBox::docbook(Buffer const & buf, std::ostream & os,
-                     OutputParams const & runparams) const
-{
-       return InsetText::docbook(buf, os, runparams);
-}
-
-
-int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
-                   OutputParams const & runparams) const
+int InsetBox::plaintext(Buffer const & buf, odocstream & os,
+                        OutputParams const & runparams) const
 {
        BoxType const btype = boxtranslator().find(params_.type);
 
        switch (btype) {
                case Frameless: break;
-               case Boxed:     os << "[";  break;
-               case ovalbox:   os << "(";  break;
-               case Ovalbox:   os << "(("; break;
-               case Shadowbox: os << "[";  break;
-               case Doublebox: os << "[["; break;
+               case Boxed:     os << "[\n";  break;
+               case ovalbox:   os << "(\n";  break;
+               case Ovalbox:   os << "((\n"; break;
+               case Shadowbox: os << "[/\n"; break;
+               case Doublebox: os << "[[\n"; break;
        }
 
-       int i = InsetText::plaintext(buf, os, runparams);
+       InsetText::plaintext(buf, os, runparams);
 
+       int len = 0;
        switch (btype) {
-               case Frameless: break;
-               case Boxed:     os << "]";  break;
-               case ovalbox:   os << ")";  break;
-               case Ovalbox:   os << "))"; break;
-               case Shadowbox: os << "]/"; break;
-               case Doublebox: os << "]]"; break;
+               case Frameless: os << "\n";            break;
+               case Boxed:     os << "\n]";  len = 1; break;
+               case ovalbox:   os << "\n)";  len = 1; break;
+               case Ovalbox:   os << "\n))"; len = 2; break;
+               case Shadowbox: os << "\n/]"; len = 2; break;
+               case Doublebox: os << "\n]]"; len = 2; break;
        }
 
-       return i;
+       return PLAINTEXT_NEWLINE + len; // len chars on a separate line
+}
+
+
+int InsetBox::docbook(Buffer const & buf, odocstream & os,
+                      OutputParams const & runparams) const
+{
+       return InsetText::docbook(buf, os, runparams);
 }
 
 
 void InsetBox::validate(LaTeXFeatures & features) const
 {
-       features.require("calc");
        BoxType btype = boxtranslator().find(params_.type);
        switch (btype) {
        case Frameless:
+               break;
        case Boxed:
+               features.require("calc");
                break;
        case ovalbox:
        case Ovalbox:
        case Shadowbox:
        case Doublebox:
+               features.require("calc");
                features.require("fancybox");
                break;
        }
@@ -492,15 +538,17 @@ void InsetBoxParams::read(LyXLex & lex)
        if (!lex.isOK())
                return;
 
-       if (lex.isOK()) {
-               lex.next();
-               type = lex.getString();
-       }
-       if (!lex.isOK())
+       lex.next();
+       type = lex.getString();
+
+       if (!lex)
                return;
+
        lex.next();
        string token;
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "position") {
                lex.next();
                // The [0] is needed. We need the first and only char in
@@ -510,10 +558,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "hor_pos") {
                lex.next();
                hor_pos = lex.getString()[0];
@@ -521,10 +570,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "has_inner_box") {
                lex.next();
                inner_box = lex.getInteger();
@@ -533,10 +583,10 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
 
-       if (!lex.isOK())
-               return;
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "inner_pos") {
                lex.next();
                inner_pos = lex.getString()[0];
@@ -545,10 +595,11 @@ void InsetBoxParams::read(LyXLex & lex)
                        << token << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "use_parbox") {
                lex.next();
                use_parbox = lex.getInteger();
@@ -556,10 +607,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "width") {
                lex.next();
                width = LyXLength(lex.getString());
@@ -567,10 +619,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "special") {
                lex.next();
                special = lex.getString();
@@ -578,10 +631,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "height") {
                lex.next();
                height = LyXLength(lex.getString());
@@ -589,10 +643,11 @@ void InsetBoxParams::read(LyXLex & lex)
                lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
                lex.pushToken(token);
        }
-       if (!lex.isOK())
-               return;
+
        lex.next();
        token = lex.getString();
+       if (!lex)
+               return;
        if (token == "height_special") {
                lex.next();
                height_special = lex.getString();
@@ -601,3 +656,6 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
 }
+
+
+} // namespace lyx