X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetcommand.C;h=6d5248844b923d5650941ffb563ea74feb0313ae;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=cb40f395ea2c273ce5f86dce0795a15219caf33b;hpb=2470d9297d3f2c303f555c8f235d022b19396679;p=lyx.git diff --git a/src/insets/insetcommand.C b/src/insets/insetcommand.C index cb40f395ea..6d5248844b 100644 --- a/src/insets/insetcommand.C +++ b/src/insets/insetcommand.C @@ -1,219 +1,224 @@ -/* 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 -#ifdef __GNUG__ -#pragma implementation -#endif - #include "insetcommand.h" -#include "debug.h" -#include "Painter.h" + +#include "BufferView.h" +#include "dispatchresult.h" +#include "funcrequest.h" +#include "FuncStatus.h" #include "lyxlex.h" +#include "metricsinfo.h" -using std::ostream; -using std::endl; +#include -InsetCommandParams::InsetCommandParams() -{} +namespace lyx { +using std::string; +using std::istringstream; +using std::ostream; +using std::ostringstream; -InsetCommandParams::InsetCommandParams( string const & n, - string const & c, - string const & o ) - : cmdname(n), contents(c), options(o) + +InsetCommand::InsetCommand(InsetCommandParams const & p, + string const & mailer_name) + : p_(p), + mailer_name_(mailer_name), + mouse_hover_(false), + updateButtonLabel_(true) {} -string const InsetCommandParams::getAsString() const +InsetCommand::~InsetCommand() { - return cmdname + "|++|" + contents + "|++|" + options; + if (!mailer_name_.empty()) + InsetCommandMailer(mailer_name_, *this).hideDialog(); } -void InsetCommandParams::setFromString( string const & b ) +bool InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const { - string::size_type idx = b.find("|++|"); - if (idx == string::npos) { - cmdname = b; - contents = ""; - options = ""; - return; + if (updateButtonLabel_) { + updateButtonLabel_ = false; + button_.update(getScreenLabel(*mi.base.bv->buffer()), + editable() != NOT_EDITABLE); } + button_.metrics(mi, dim); + bool const changed = dim_ != dim; + dim_ = dim; + return changed; +} - 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); - } +bool InsetCommand::setMouseHover(bool mouse_hover) +{ + mouse_hover_ = mouse_hover; + return true; } -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; + setPosCache(pi, x, y); + button_.setRenderState(mouse_hover_); + button_.draw(pi, x, y); } -bool InsetCommandParams::operator!=(InsetCommandParams const & o) const +void InsetCommand::setParams(InsetCommandParams const & p) { - return !(*this == o); + p_ = p; + updateButtonLabel_ = true; } -void InsetCommandParams::scanCommand(string const & cmd) +int InsetCommand::latex(Buffer const &, odocstream & os, + OutputParams 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; - } - } + os << getCommand(); + return 0; +} + + +int InsetCommand::plaintext(Buffer const &, odocstream &, + OutputParams const &) const +{ + return 0; +} + - // 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; +int InsetCommand::docbook(Buffer const &, odocstream &, + OutputParams const &) const +{ + return 0; } -// This function will not be necessary when lyx3 -void InsetCommandParams::read(LyXLex & lex) -{ - string token; +void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd) +{ + switch (cmd.action) { + case LFUN_INSET_REFRESH: + updateButtonLabel_ = true; + break; + + case LFUN_INSET_MODIFY: { + InsetCommandParams p(p_.getCmdName()); + InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p); + if (p.getCmdName().empty()) + cur.noUpdate(); + else + setParams(p); + break; + } - if (lex.eatLine()) { - token = lex.getString(); - scanCommand(token); - } else { - lex.printError("InsetCommand: Parse error: `$$Token'"); + case LFUN_INSET_DIALOG_UPDATE: { + string const name = to_utf8(cmd.argument()); + InsetCommandMailer(name, *this).updateDialog(&cur.bv()); + break; } - - while (lex.isOK()) { - lex.nextToken(); - token = lex.getString(); - if (token == "\\end_inset") - break; + + case LFUN_MOUSE_RELEASE: { + if (!mailer_name_.empty()) + InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv()); + break; } - if (token != "\\end_inset") { - lex.printError("Missing \\end_inset at this point. " - "Read: `$$Token'"); + + default: + InsetBase::doDispatch(cur, cmd); + break; } + } -void InsetCommandParams::write(ostream & os) const +bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd, + FuncStatus & status) const { - os << "LatexCommand " << getCommand() << "\n"; + switch (cmd.action) { + // suppress these + case LFUN_ERT_INSERT: + status.enabled(false); + return true; + // we handle these + case LFUN_INSET_REFRESH: + case LFUN_INSET_MODIFY: + case LFUN_INSET_DIALOG_UPDATE: + status.enabled(true); + return true; + default: + return InsetBase::getStatus(cur, cmd, status); + } } -string const InsetCommandParams::getCommand() const -{ - string s; - if (!getCmdName().empty()) s += "\\"+getCmdName(); - if (!getOptions().empty()) s += "["+getOptions()+']'; - s += "{"+getContents()+'}'; - return s; +void InsetCommand::replaceContents(std::string const & from, string const & to) +{ + if (getContents() == from) + setContents(to); } -InsetCommand::InsetCommand(InsetCommandParams const & p, bool) - : p_( p.getCmdName(), p.getContents(), p.getOptions() ) +InsetCommandMailer::InsetCommandMailer(string const & name, + InsetCommand & inset) + : name_(name), inset_(inset) {} -void InsetCommand::setParams(InsetCommandParams const & p ) +string const InsetCommandMailer::inset2string(Buffer const &) const { - p_.setCmdName( p.getCmdName() ); - p_.setContents( p.getContents() ); - p_.setOptions( p.getOptions() ); + return params2string(name(), inset_.params()); } -int InsetCommand::latex(Buffer const *, ostream & os, - bool /*fragile*/, bool/*fs*/) const +void InsetCommandMailer::string2params(string const & name, + string const & in, + InsetCommandParams & params) { - os << getCommand(); - return 0; -} + params.clear(); + if (in.empty()) + return; + istringstream data(in); + LyXLex lex(0,0); + lex.setStream(data); -int InsetCommand::ascii(Buffer const *, ostream &, int) const -{ - return 0; -} + string n; + lex >> n; + if (!lex || n != name) + return print_mailer_error("InsetCommandMailer", in, 1, name); + // This is part of the inset proper that is usually swallowed + // by LyXText::readInset + string id; + lex >> id; + if (!lex || id != "LatexCommand") + return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand"); -int InsetCommand::linuxdoc(Buffer const *, ostream &) const -{ - return 0; + 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 data.str(); } + + +} // namespace lyx