]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetBox.cpp
Restore XHTML output for InsetListings.
[lyx.git] / src / insets / InsetBox.cpp
index c40c3c54a01eea45ece9cea95321d25b2686622c..ad2084f5bb7497c54d54f6e4db9390c075aa3a7f 100644 (file)
@@ -5,7 +5,7 @@
  *
  * \author Angus Leeming
  * \author Martin Vermeer
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "DispatchResult.h"
 #include "FuncStatus.h"
 #include "FuncRequest.h"
-#include "support/gettext.h"
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
+#include "output_xhtml.h"
 #include "TextClass.h"
 
 #include "support/debug.h"
+#include "support/docstream.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 #include "support/Translator.h"
 
+#include "frontends/Application.h"
+
 #include <sstream>
 
 using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -41,7 +47,7 @@ namespace {
 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
 
-BoxTranslator const init_boxtranslator()
+BoxTranslator initBoxtranslator()
 {
        BoxTranslator translator("Boxed", InsetBox::Boxed);
        translator.addPair("Frameless", InsetBox::Frameless);
@@ -55,7 +61,7 @@ BoxTranslator const init_boxtranslator()
 }
 
 
-BoxTranslatorLoc const init_boxtranslator_loc()
+BoxTranslatorLoc initBoxtranslatorLoc()
 {
        BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
        translator.addPair(_("frameless"), InsetBox::Frameless);
@@ -71,57 +77,43 @@ BoxTranslatorLoc const init_boxtranslator_loc()
 
 BoxTranslator const & boxtranslator()
 {
-       static BoxTranslator translator = init_boxtranslator();
+       static BoxTranslator translator = initBoxtranslator();
        return translator;
 }
 
 
 BoxTranslatorLoc const & boxtranslator_loc()
 {
-       static BoxTranslatorLoc translator = init_boxtranslator_loc();
+       static BoxTranslatorLoc translator = initBoxtranslatorLoc();
        return translator;
 }
 
-} // anon
+} // namespace anon
 
 
-InsetBox::InsetBox(BufferParams const & bp, string const & label)
-       : InsetCollapsable(bp), params_(label)
-{
-       if (forceEmptyLayout())
-               paragraphs().back().setLayout(bp.textClass().emptyLayout());
-}
-
+/////////////////////////////////////////////////////////////////////////
+//
+// InsetBox
+//
+/////////////////////////////////////////////////////////////////////////
 
-InsetBox::InsetBox(InsetBox const & in)
-       : InsetCollapsable(in), params_(in.params_)
+InsetBox::InsetBox(Buffer * buffer, string const & label)
+       : InsetCollapsable(buffer), params_(label)
 {}
 
 
 InsetBox::~InsetBox()
 {
-       InsetBoxMailer(*this).hideDialog();
-}
-
-
-Inset * InsetBox::clone() const
-{
-       return new InsetBox(*this);
-}
-
-
-docstring InsetBox::editMessage() const
-{
-       return _("Opened Box Inset");
+       hideDialogs("box", this);
 }
 
 
 docstring InsetBox::name() const 
 {
        // FIXME: UNICODE
-       string name = string("Box");
+       string name = "Box";
        if (boxtranslator().find(params_.type) == Shaded)
-               name += string(":Shaded");
+               name += ":Shaded";
        return from_ascii(name);
 }
 
@@ -142,27 +134,41 @@ void InsetBox::read(Lexer & lex)
 
 void InsetBox::setButtonLabel()
 {
-       BoxType btype = boxtranslator().find(params_.type);
+       BoxType const btype = boxtranslator().find(params_.type);
 
-       docstring label;
-       label += _("Box");
-       label += " (";
-       if (btype == Frameless) {
+       docstring const type = _("Box");
+
+       docstring inner;
+       if (params_.inner_box) {
                if (params_.use_parbox)
-                       label += _("Parbox");
+                       inner = _("Parbox");
                else
-                       label += _("Minipage");
-       } else
-               label += boxtranslator_loc().find(btype);
-       label += ")";
+                       inner = _("Minipage");
+       }
 
+       docstring frame;
+       if (btype != Frameless)
+               frame = boxtranslator_loc().find(btype);
+
+       docstring label;
+       if (inner.empty() && frame.empty())
+               label = type;
+       else if (inner.empty())
+               label = bformat(_("%1$s (%2$s)"),
+                       type, frame);
+       else if (frame.empty())
+               label = bformat(_("%1$s (%2$s)"),
+                       type, inner);
+       else
+               label = bformat(_("%1$s (%2$s, %3$s)"),
+                       type, inner, frame);
        setLabel(label);
 }
 
 
 bool InsetBox::hasFixedWidth() const
 {
-      return params_.inner_box || params_.special != "width";
+       return params_.inner_box || params_.special != "width";
 }
 
 
@@ -178,15 +184,16 @@ void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
 }
 
 
-bool InsetBox::forceEmptyLayout() const
+bool InsetBox::forcePlainLayout(idx_type) const
 {
-       return !params_.inner_box;
+       return !params_.inner_box && params_.type != "Framed";
 }
 
 
 bool InsetBox::showInsetDialog(BufferView * bv) const
 {
-       InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
+       bv->showDialog("box", params2string(params_),
+               const_cast<InsetBox *>(this));
        return true;
 }
 
@@ -197,21 +204,15 @@ void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_INSET_MODIFY: {
                //lyxerr << "InsetBox::dispatch MODIFY" << endl;
-               InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
-               setLayout(cur.buffer().params());
+               if (cmd.getArg(0) == "changetype")
+                       params_.type = cmd.getArg(1);
+               else
+                       string2params(to_utf8(cmd.argument()), params_);
                break;
        }
 
        case LFUN_INSET_DIALOG_UPDATE:
-               InsetBoxMailer(*this).updateDialog(&cur.bv());
-               break;
-
-       case LFUN_MOUSE_RELEASE:
-               if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
-                       InsetBoxMailer(*this).showDialog(&cur.bv());
-                       break;
-               }
-               InsetCollapsable::doDispatch(cur, cmd);
+               cur.bv().updateDialog("box", params2string(params_));
                break;
 
        default:
@@ -227,16 +228,20 @@ bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY:
+               if (cmd.getArg(0) == "changetype")
+                       flag.setOnOff(cmd.getArg(1) == params_.type);
+               flag.setEnabled(true);
+               return true;
+
        case LFUN_INSET_DIALOG_UPDATE:
-               flag.enabled(true);
+               flag.setEnabled(true);
                return true;
+
        case LFUN_BREAK_PARAGRAPH:
-               if (params_.inner_box) {
+               if (params_.inner_box || params_.type == "Framed")
                        return InsetCollapsable::getStatus(cur, cmd, flag);
-               } else {
-                       flag.enabled(false);
-                       return true;
-               }
+               flag.setEnabled(false);
+               return true;
 
        default:
                return InsetCollapsable::getStatus(cur, cmd, flag);
@@ -244,13 +249,6 @@ bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-bool InsetBox::isMacroScope() const
-{
-       BoxType btype = boxtranslator().find(params_.type);
-       return btype != Frameless || params_.inner_box;
-}
-
-
 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
 {
        BoxType btype = boxtranslator().find(params_.type);
@@ -302,7 +300,7 @@ int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
                os << "\\framebox";
                if (!params_.inner_box) {
                        os << "{\\makebox";
-                       // Special widths, see usrguide §3.5
+                       // Special widths, see usrguide Â§3.5
                        // FIXME UNICODE
                        if (params_.special != "none") {
                                os << "[" << params_.width.value()
@@ -367,11 +365,12 @@ int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
                if (params_.use_parbox)
                        os << "{";
                os << "%\n";
-               i += 1;
+               ++i;
        }
-       if (btype == Shaded)
+       if (btype == Shaded) {
                os << "\\begin{shaded}%\n";
-               i += 1;
+               ++i;
+       }
 
        i += InsetText::latex(os, runparams);
 
@@ -480,6 +479,27 @@ int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
+{
+       // construct attributes
+       string attrs = "class='" + params_.type + "'";
+       string style;
+       if (!params_.width.empty())
+               style += ("width: " + params_.width.asHTMLString() + ";");
+       if (!params_.height.empty())
+               style += ("height: " + params_.height.asHTMLString() + ";");
+       if (!style.empty())
+               attrs += " style='" + style + "'";
+
+       xs << StartTag("div", attrs);
+       XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
+       docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
+       xs << EndTag("div");
+       xs << defer;
+       return docstring();
+}
+
+
 void InsetBox::validate(LaTeXFeatures & features) const
 {
        BoxType btype = boxtranslator().find(params_.type);
@@ -504,25 +524,17 @@ void InsetBox::validate(LaTeXFeatures & features) const
                features.require("framed");
                break;
        }
-       InsetText::validate(features);
+       InsetCollapsable::validate(features);
 }
 
 
-InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
-       : inset_(inset)
-{}
-
-
-string const InsetBoxMailer::name_ = "box";
-
-
-string const InsetBoxMailer::inset2string(Buffer const &) const
+docstring InsetBox::contextMenu(BufferView const &, int, int) const
 {
-       return params2string(inset_.params());
+       return from_ascii("context-box");
 }
 
 
-string const InsetBoxMailer::params2string(InsetBoxParams const & params)
+string InsetBox::params2string(InsetBoxParams const & params)
 {
        ostringstream data;
        data << "box" << ' ';
@@ -531,33 +543,43 @@ string const InsetBoxMailer::params2string(InsetBoxParams const & params)
 }
 
 
-void InsetBoxMailer::string2params(string const & in,
-                                  InsetBoxParams & params)
+void InsetBox::string2params(string const & in, InsetBoxParams & params)
 {
        params = InsetBoxParams(string());
        if (in.empty())
                return;
 
        istringstream data(in);
-       Lexer lex(0,0);
+       Lexer lex;
        lex.setStream(data);
 
        string name;
        lex >> name;
-       if (!lex || name != name_)
-               return print_mailer_error("InsetBoxMailer", in, 1, name_);
+       if (!lex || name != "box") {
+               LYXERR0("InsetBox::string2params(" << in << ")\n"
+                                         "Expected arg 1 to be \"box\"\n");
+               return;
+       }
 
        // This is part of the inset proper that is usually swallowed
        // by Text::readInset
        string id;
        lex >> id;
-       if (!lex || id != "Box")
-               return print_mailer_error("InsetBoxMailer", in, 2, "Box");
+       if (!lex || id != "Box") {
+               LYXERR0("InsetBox::string2params(" << in << ")\n"
+                                         "Expected arg 2 to be \"Box\"\n");
+       }
 
        params.read(lex);
 }
 
 
+/////////////////////////////////////////////////////////////////////////
+//
+// InsetBoxParams
+//
+/////////////////////////////////////////////////////////////////////////
+
 InsetBoxParams::InsetBoxParams(string const & label)
        : type(label),
          use_parbox(false),
@@ -589,126 +611,19 @@ void InsetBoxParams::write(ostream & os) const
 
 void InsetBoxParams::read(Lexer & lex)
 {
-       if (!lex.isOK())
-               return;
-
-       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
-               // this string -- MV
-               pos = lex.getString()[0];
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "hor_pos") {
-               lex.next();
-               hor_pos = lex.getString()[0];
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "has_inner_box") {
-               lex.next();
-               inner_box = lex.getInteger();
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "inner_pos") {
-               lex.next();
-               inner_pos = lex.getString()[0];
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
-                       << token << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "use_parbox") {
-               lex.next();
-               use_parbox = lex.getInteger();
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "width") {
-               lex.next();
-               width = Length(lex.getString());
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "special") {
-               lex.next();
-               special = lex.getString();
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "height") {
-               lex.next();
-               height = Length(lex.getString());
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
-               lex.pushToken(token);
-       }
-
-       lex.next();
-       token = lex.getString();
-       if (!lex)
-               return;
-       if (token == "height_special") {
-               lex.next();
-               height_special = lex.getString();
-       } else {
-               lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
-               lex.pushToken(token);
-       }
+       lex.setContext("InsetBoxParams::read");
+       lex >> type;
+       lex >> "position" >> pos;
+       lex >> "hor_pos" >> hor_pos;
+       lex >> "has_inner_box" >> inner_box;
+       if (type == "Framed")
+               inner_box = false;
+       lex >> "inner_pos" >> inner_pos;
+       lex >> "use_parbox" >> use_parbox;
+       lex >> "width" >> width;
+       lex >> "special" >> special;
+       lex >> "height" >> height;
+       lex >> "height_special" >> height_special;
 }