X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetcommand.C;h=6d5248844b923d5650941ffb563ea74feb0313ae;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=d48bbbdcb4dbcc7a8531608c294020a53c2e65a9;hpb=b33a63e177780199ed93c0417db41ae2fa7c9a36;p=lyx.git diff --git a/src/insets/insetcommand.C b/src/insets/insetcommand.C index d48bbbdcb4..6d5248844b 100644 --- a/src/insets/insetcommand.C +++ b/src/insets/insetcommand.C @@ -1,218 +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; - } - - 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 (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; } -bool InsetCommandParams::operator==(InsetCommandParams const & o) const +bool InsetCommand::setMouseHover(bool mouse_hover) { - return cmdname == o.cmdname && contents == o.contents - && options == o.options; + mouse_hover_ = mouse_hover; + return true; } -bool InsetCommandParams::operator!=(InsetCommandParams const & o) const +void InsetCommand::draw(PainterInfo & pi, int x, int y) const { - return !(*this == o); + setPosCache(pi, x, y); + button_.setRenderState(mouse_hover_); + button_.draw(pi, x, y); } -void InsetCommandParams::scanCommand(string const & cmd) +void InsetCommand::setParams(InsetCommandParams const & p) { - 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; + p_ = p; + updateButtonLabel_ = true; } -// 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::latex(Buffer const &, odocstream & os, + OutputParams const &) const +{ + os << getCommand(); + return 0; } -void InsetCommandParams::write(ostream & os) const +int InsetCommand::plaintext(Buffer const &, odocstream &, + OutputParams const &) 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 &, odocstream &, + OutputParams const &) const +{ + return 0; } -InsetCommand::InsetCommand(InsetCommandParams const & p, bool) - : p_( p.getCmdName(), p.getContents(), p.getOptions() ) +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; + } + + case LFUN_INSET_DIALOG_UPDATE: { + string const name = to_utf8(cmd.argument()); + InsetCommandMailer(name, *this).updateDialog(&cur.bv()); + break; + } + + case LFUN_MOUSE_RELEASE: { + if (!mailer_name_.empty()) + InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv()); + break; + } + + default: + InsetBase::doDispatch(cur, cmd); + break; + } + } -void InsetCommand::setParams(InsetCommandParams const & p ) +bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd, + FuncStatus & status) const { - p_.setCmdName( p.getCmdName() ); - p_.setContents( p.getContents() ); - p_.setOptions( p.getOptions() ); + 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); + } } -int InsetCommand::latex(Buffer const *, ostream & os, - bool /*fragile*/, bool/*fs*/) const +void InsetCommand::replaceContents(std::string const & from, string const & to) { - os << getCommand(); - return 0; + if (getContents() == from) + setContents(to); } -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 & name, + string const & in, + InsetCommandParams & params) { - return 0; + params.clear(); + if (in.empty()) + return; + + istringstream data(in); + LyXLex lex(0,0); + lex.setStream(data); + + 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"); + + 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