-/* This file is part of
- * ======================================================
- *
- * LyX, The Document Processor
- *
- * Copyright 1995 Matthias Ettrich
- * Copyright 1995-2001 The LyX Team.
+/**
+ * \file insetcommand.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * ====================================================== */
+ * \author Angus Leeming
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS
+ */
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "insetcommand.h"
+#include "BufferView.h"
#include "debug.h"
-#include "Painter.h"
+#include "funcrequest.h"
#include "lyxlex.h"
+#include "metricsinfo.h"
-using std::ostream;
-using std::endl;
+#include "frontends/Painter.h"
+#include "support/lstrings.h"
-InsetCommandParams::InsetCommandParams()
-{}
+#include "Lsstream.h"
+
+using std::ostream;
-InsetCommandParams::InsetCommandParams( string const & n,
- string const & c,
- string const & o )
- : cmdname(n), contents(c), options(o)
+InsetCommand::InsetCommand(InsetCommandParams const & p)
+ : p_(p.getCmdName(), p.getContents(), p.getOptions()),
+ set_label_(false)
{}
-string const InsetCommandParams::getAsString() const
+BufferView * InsetCommand::view() const
{
- return cmdname + "|++|" + contents + "|++|" + options;
+ return button_.view();
}
-void InsetCommandParams::setFromString( string const & b )
+void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
{
- string::size_type idx = b.find("|++|");
- if (idx == string::npos) {
- cmdname = b;
- contents = "";
- options = "";
- return;
- }
-
- cmdname = b.substr(0, idx);
- string tmp = b.substr(idx+4);
-
- idx = tmp.find("|++|");
- if (idx == string::npos) {
- contents = tmp;
- options = "";
- } else {
- contents = tmp.substr(0, idx);
- options = tmp.substr(idx+4);
+ if (!set_label_) {
+ set_label_ = true;
+ button_.update(getScreenLabel(mi.base.bv->buffer()),
+ editable() != NOT_EDITABLE);
}
+ button_.metrics(mi, dim);
+ dim_ = dim;
}
-bool InsetCommandParams::operator==(InsetCommandParams const & o) const
+void InsetCommand::draw(PainterInfo & pi, int x, int y) const
{
- return cmdname == o.cmdname && contents == o.contents
- && options == o.options;
+ button_.draw(pi, x, y);
}
-bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
+void InsetCommand::setParams(InsetCommandParams const & p)
{
- return !(*this == o);
+ p_.setCmdName(p.getCmdName());
+ p_.setContents(p.getContents());
+ p_.setOptions(p.getOptions());
+ set_label_ = false;
}
-void InsetCommandParams::scanCommand(string const & cmd)
+int InsetCommand::latex(Buffer const *, ostream & os,
+ LatexRunParams const &) const
{
- string tcmdname, toptions, tcontents;
-
- if (cmd.empty()) return;
-
- enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
-
- // Used to handle things like \command[foo[bar]]{foo{bar}}
- int nestdepth = 0;
-
- for (string::size_type i = 0; i < cmd.length(); ++i) {
- char c = cmd[i];
- if ((state == CMDNAME && c == ' ') ||
- (state == CMDNAME && c == '[') ||
- (state == CMDNAME && c == '{')) {
- state = WS;
- }
- if ((state == OPTION && c == ']') ||
- (state == CONTENT && c == '}')) {
- if (nestdepth == 0) {
- state = WS;
- } else {
- --nestdepth;
- }
- }
- if ((state == OPTION && c == '[') ||
- (state == CONTENT && c == '{')) {
- ++nestdepth;
- }
- switch (state) {
- case CMDNAME: tcmdname += c; break;
- case OPTION: toptions += c; break;
- case CONTENT: tcontents += c; break;
- case WS:
- if (c == '\\') {
- state = CMDNAME;
- } else if (c == '[') {
- state = OPTION;
- nestdepth = 0; // Just to be sure
- } else if (c == '{') {
- state = CONTENT;
- nestdepth = 0; // Just to be sure
- }
- break;
- }
- }
-
- // Don't mess with this.
- if (!tcmdname.empty()) setCmdName( tcmdname );
- if (!toptions.empty()) setOptions( toptions );
- if (!tcontents.empty()) setContents( tcontents );
-
- if (lyxerr.debugging(Debug::PARSER))
- lyxerr << "Command <" << cmd
- << "> == <" << getCommand()
- << "> == <" << getCmdName()
- << '|' << getContents()
- << '|' << getOptions() << '>' << endl;
+ os << getCommand();
+ return 0;
}
-// This function will not be necessary when lyx3
-void InsetCommandParams::read(LyXLex & lex)
-{
- string token;
-
- if (lex.EatLine()) {
- token = lex.GetString();
- scanCommand(token);
- } else
- lex.printError("InsetCommand: Parse error: `$$Token'");
- while (lex.IsOK()) {
- lex.nextToken();
- token = lex.GetString();
- if (token == "\\end_inset")
- break;
- }
- if (token != "\\end_inset") {
- lex.printError("Missing \\end_inset at this point. "
- "Read: `$$Token'");
- }
+int InsetCommand::ascii(Buffer const *, ostream &, int) const
+{
+ return 0;
}
-void InsetCommandParams::write(ostream & os) const
+int InsetCommand::linuxdoc(Buffer const *, ostream &) const
{
- os << "LatexCommand " << getCommand() << "\n";
+ return 0;
}
-string const InsetCommandParams::getCommand() const
-{
- string s;
- if (!getCmdName().empty()) s += "\\"+getCmdName();
- if (!getOptions().empty()) s += "["+getOptions()+']';
- s += "{"+getContents()+'}';
- return s;
+int InsetCommand::docbook(Buffer const *, ostream &, bool) const
+{
+ return 0;
}
-InsetCommand::InsetCommand(InsetCommandParams const & p, bool)
- : p_( p.getCmdName(), p.getContents(), p.getOptions() )
+dispatch_result InsetCommand::localDispatch(FuncRequest const & cmd)
{
-}
+ switch (cmd.action) {
+ case LFUN_INSET_MODIFY: {
+ InsetCommandParams p;
+ InsetCommandMailer::string2params(cmd.argument, p);
+ if (p.getCmdName().empty())
+ return UNDISPATCHED;
+
+ setParams(p);
+ cmd.view()->updateInset(this);
+ return DISPATCHED;
+ }
+ case LFUN_INSET_DIALOG_UPDATE:
+ InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
+ return DISPATCHED;
-void InsetCommand::setParams(InsetCommandParams const & p )
-{
- p_.setCmdName( p.getCmdName() );
- p_.setContents( p.getContents() );
- p_.setOptions( p.getOptions() );
-}
+ case LFUN_MOUSE_RELEASE:
+ return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
+ default:
+ return InsetOld::localDispatch(cmd);
+ }
-int InsetCommand::latex(Buffer const *, ostream & os,
- bool /*fragile*/, bool/*fs*/) const
-{
- os << getCommand();
- return 0;
}
-int InsetCommand::ascii(Buffer const *, ostream &, int) const
+InsetCommandMailer::InsetCommandMailer(string const & name,
+ InsetCommand & inset)
+ : name_(name), inset_(inset)
+{}
+
+
+string const InsetCommandMailer::inset2string(Buffer const &) const
{
- return 0;
+ return params2string(name(), inset_.params());
}
-int InsetCommand::linuxdoc(Buffer const *, ostream &) const
+void InsetCommandMailer::string2params(string const & in,
+ InsetCommandParams & params)
{
- return 0;
+ params.setCmdName(string());
+ params.setContents(string());
+ params.setOptions(string());
+
+ if (in.empty())
+ return;
+
+ istringstream data(STRCONV(in));
+ LyXLex lex(0,0);
+ lex.setStream(data);
+
+ if (lex.isOK()) {
+ lex.next();
+ string const name = lex.getString();
+ }
+
+ // This is part of the inset proper that is usually swallowed
+ // by Buffer::readInset
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token != "LatexCommand")
+ return;
+ }
+ if (lex.isOK()) {
+ params.read(lex);
+ }
}
-int InsetCommand::docBook(Buffer const *, ostream &) const
+string const InsetCommandMailer::params2string(string const & name,
+ InsetCommandParams const & params)
{
- return 0;
+ ostringstream data;
+ data << name << ' ';
+ params.write(data);
+ data << "\\end_inset\n";
+ return STRCONV(data.str());
}