]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetbox.C
convert graphics for .tex export, fixes bug 1231
[lyx.git] / src / insets / insetbox.C
index 147edf57630bc460c9d65782e368c296c91acf29..4f8124fff5579fdd78c07bd616787fb684de7ebc 100644 (file)
 #include <config.h>
 
 #include "insetbox.h"
-#include "debug.h"
 
-#include "BufferView.h"
+#include "cursor.h"
+#include "dispatchresult.h"
+#include "debug.h"
 #include "funcrequest.h"
 #include "gettext.h"
 #include "LaTeXFeatures.h"
@@ -25,6 +26,7 @@
 #include "paragraph.h"
 
 #include "support/std_sstream.h"
+#include "support/translator.h"
 
 using std::auto_ptr;
 using std::string;
@@ -36,6 +38,8 @@ using std::endl;
 
 namespace {
 
+typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
+
 BoxTranslator const init_boxtranslator() {
        BoxTranslator translator("Boxed", InsetBox::Boxed);
        translator.addPair("Frameless", InsetBox::Frameless);
@@ -95,8 +99,7 @@ InsetBox::InsetBox(InsetBox const & in)
 
 InsetBox::~InsetBox()
 {
-       InsetBoxMailer mailer(*this);
-       mailer.hideDialog();
+       InsetBoxMailer(*this).hideDialog();
 }
 
 
@@ -136,11 +139,11 @@ void InsetBox::setButtonLabel()
        BoxType btype = boxtranslator().find(params_.type);
        if (btype == Frameless) {
                if (params_.use_parbox)
-                       setLabel(_("Parbox"));
+                       setLabel(_("Box") + " (" + _("Parbox") + ")");
                else
-                       setLabel(_("Minipage"));
+                       setLabel(_("Box") + " (" + _("Minipage") + ")");
        } else
-               setLabel(boxtranslator_loc().find(btype));
+               setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
 
        font.setColor(LColor::foreground);
        setBackgroundColor(LColor::background);
@@ -148,11 +151,13 @@ void InsetBox::setButtonLabel()
 }
 
 
-void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
+void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
 {
+       MetricsInfo mi = m;
+       mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
        InsetCollapsable::metrics(mi, dim);
-       if (params_.inner_box && isOpen())
-               dim.wid = mi.base.textwidth;
+       //if (params_.inner_box && isOpen())
+       //      dim.wid = mi.base.textwidth;
        dim_ = dim;
 }
 
@@ -164,37 +169,38 @@ bool InsetBox::showInsetDialog(BufferView * bv) const
 }
 
 
-dispatch_result InsetBox::localDispatch(FuncRequest const & cmd)
+void InsetBox::priv_dispatch(LCursor & cur, FuncRequest & cmd)
 {
-       BufferView * bv = cmd.view();
-
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
+               lyxerr << "InsetBox::dispatch MODIFY" << endl;
                InsetBoxMailer::string2params(cmd.argument, params_);
                setButtonLabel();
-               bv->updateInset(this);
-               return DISPATCHED;
+               break;
        }
+
        case LFUN_INSET_DIALOG_UPDATE:
-               InsetBoxMailer(*this).updateDialog(bv);
-               return DISPATCHED;
+               InsetBoxMailer(*this).updateDialog(&cur.bv());
+               break;
 
        case LFUN_MOUSE_RELEASE:
                if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
-                       InsetBoxMailer(*this).showDialog(bv);
-                       return DISPATCHED;
+                       InsetBoxMailer(*this).showDialog(&cur.bv());
+                       break;
                }
-               // fallthrough:
+               InsetCollapsable::priv_dispatch(cur, cmd);
+               break;
 
        default:
-               return InsetCollapsable::localDispatch(cmd);
+               InsetCollapsable::priv_dispatch(cur, cmd);
+               break;
        }
 }
 
 
 int InsetBox::latex(Buffer const & buf, ostream & os,
-                               LatexRunParams const & runparams) const
+                               OutputParams const & runparams) const
 {
        BoxType btype = boxtranslator().find(params_.type);
 
@@ -225,13 +231,13 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
                        break;
                }
        }
-       
+
        int i = 0;
        os << "%\n";
        // Adapt to column/text width correctly also if paragraphs indented:
        if (stdwidth)
                os << "\\noindent";
-       
+
        switch (btype) {
        case Frameless:
                break;
@@ -248,7 +254,7 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
                        if (params_.hor_pos != 'c')
                                os << "[" << params_.hor_pos << "]";
                }
-               
+
                os << "{";
                break;
        case ovalbox:
@@ -290,7 +296,7 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
                i += 1;
        }
 
-       i += inset.latex(buf, os, runparams);
+       i += InsetText::latex(buf, os, runparams);
 
        if (params_.inner_box) {
                if (params_.use_parbox)
@@ -298,7 +304,7 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
                else
                        os << "%\n\\end{minipage}";
        }
-       
+
        switch (btype) {
        case Frameless:
                break;
@@ -313,72 +319,52 @@ int InsetBox::latex(Buffer const & buf, ostream & os,
        case Shadowbox:
                os << "}";
                break;
-       }       
+       }
        os << "%\n";
-       
+
        i += 3;
 
        return i;
 }
 
 
-int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
+int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
+                      OutputParams const & runparams) const
 {
-       return inset.linuxdoc(buf, os);
+       return InsetText::linuxdoc(buf, os, runparams);
 }
 
 
-int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
+int InsetBox::docbook(Buffer const & buf, std::ostream & os,
+                     OutputParams const & runparams) const
 {
-       return inset.docbook(buf, os, mixcont);
+       return InsetText::docbook(buf, os, runparams);
 }
 
 
-int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
+int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
+                   OutputParams const & runparams) const
 {
-       int i = 0;
-       string const pt = params_.type;
-       BoxType btype = boxtranslator().find(params_.type);
+       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 Frameless: break;
+               case Boxed:     os << "[";  break;
+               case ovalbox:   os << "(";  break;
+               case Ovalbox:   os << "(("; break;
+               case Shadowbox: os << "[";  break;
+               case Doublebox: os << "[["; break;
        }
 
-       i = inset.ascii(buf, os, ll);
+       int i = InsetText::plaintext(buf, os, runparams);
 
        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: break;
+               case Boxed:     os << "]";  break;
+               case ovalbox:   os << ")";  break;
+               case Ovalbox:   os << "))"; break;
+               case Shadowbox: os << "]/"; break;
+               case Doublebox: os << "]]"; break;
        }
 
        return i;
@@ -400,14 +386,13 @@ void InsetBox::validate(LaTeXFeatures & features) const
                features.require("fancybox");
                break;
        }
-       inset.validate(features);
+       InsetText::validate(features);
 }
 
 
 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
        : inset_(inset)
-{
-}
+{}
 
 
 string const InsetBoxMailer::name_ = "box";
@@ -429,10 +414,9 @@ string const InsetBoxMailer::params2string(InsetBoxParams const & params)
 
 
 void InsetBoxMailer::string2params(string const & in,
-       InsetBoxParams & params)
+                                  InsetBoxParams & params)
 {
        params = InsetBoxParams(string());
-
        if (in.empty())
                return;
 
@@ -440,10 +424,17 @@ void InsetBoxMailer::string2params(string const & in,
        LyXLex lex(0,0);
        lex.setStream(data);
 
-       string token;
-       lex.next();
-       token = lex.getString();
-       lex.next();
+       string name;
+       lex >> name;
+       if (!lex || name != name_)
+               return print_mailer_error("InsetBoxMailer", in, 1, name_);
+
+       // This is part of the inset proper that is usually swallowed
+       // by LyXText::readInset
+       string id;
+       lex >> id;
+       if (!lex || id != "Box")
+               return print_mailer_error("InsetBoxMailer", in, 2, "Box");
 
        params.read(lex);
 }
@@ -465,7 +456,7 @@ InsetBoxParams::InsetBoxParams(string const & label)
 
 void InsetBoxParams::write(ostream & os) const
 {
-       os << type << "\n";
+       os << "Box " << type << "\n";
        os << "position \"" << pos << "\"\n";
        os << "hor_pos \"" << hor_pos << "\"\n";
        os << "has_inner_box " << inner_box << "\n";
@@ -480,13 +471,17 @@ void InsetBoxParams::write(ostream & os) const
 
 void InsetBoxParams::read(LyXLex & lex)
 {
+       if (!lex.isOK())
+               return;
+
        if (lex.isOK()) {
+               lex.next();
                type = lex.getString();
        }
-       string token;
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
+       string token;
        token = lex.getString();
        if (token == "position") {
                lex.next();
@@ -498,7 +493,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "hor_pos") {
@@ -509,7 +504,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "has_inner_box") {
@@ -521,7 +516,7 @@ void InsetBoxParams::read(LyXLex & lex)
        }
 
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "inner_pos") {
@@ -533,7 +528,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "use_parbox") {
@@ -544,7 +539,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "width") {
@@ -555,7 +550,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "special") {
@@ -566,7 +561,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "height") {
@@ -577,7 +572,7 @@ void InsetBoxParams::read(LyXLex & lex)
                lex.pushToken(token);
        }
        if (!lex.isOK())
-               return;
+               return;
        lex.next();
        token = lex.getString();
        if (token == "height_special") {