/** * \file insetnote.C * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Angus Leeming * \author Martin Vermeer * \author Jürgen Spitzmüller * * Full author contact details are available in file CREDITS. */ #include #include "insetnote.h" #include "BufferView.h" #include "dispatchresult.h" #include "funcrequest.h" #include "gettext.h" #include "LaTeXFeatures.h" #include "LColor.h" #include "lyxlex.h" #include "metricsinfo.h" #include "paragraph.h" #include "support/std_sstream.h" using std::string; using std::auto_ptr; using std::istringstream; using std::ostream; using std::ostringstream; void InsetNote::init() { setInsetName("Note"); setButtonLabel(); } InsetNote::InsetNote(BufferParams const & bp, string const & label) : InsetCollapsable(bp) { params_.type = label; init(); } InsetNote::InsetNote(InsetNote const & in) : InsetCollapsable(in), params_(in.params_) { init(); } InsetNote::~InsetNote() { InsetNoteMailer(*this).hideDialog(); } auto_ptr InsetNote::clone() const { return auto_ptr(new InsetNote(*this)); } string const InsetNote::editMessage() const { return _("Opened Note Inset"); } void InsetNote::write(Buffer const & buf, ostream & os) const { params_.write(os); InsetCollapsable::write(buf, os); } void InsetNote::read(Buffer const & buf, LyXLex & lex) { InsetCollapsable::read(buf, lex); setButtonLabel(); } void InsetNote::setButtonLabel() { LyXFont font(LyXFont::ALL_SANE); font.decSize(); font.decSize(); if (params_.type == "Note") { setLabel(_("LyX Note")); font.setColor(LColor::note); setBackgroundColor(LColor::notebg); } else if (params_.type == "Comment") { setLabel(_("Comment")); font.setColor(LColor::comment); setBackgroundColor(LColor::commentbg); } else { setLabel(_("Greyed Out")); font.setColor(LColor::greyedout); setBackgroundColor(LColor::greyedoutbg); } setLabelFont(font); } bool InsetNote::showInsetDialog(BufferView * bv) const { InsetNoteMailer(const_cast(*this)).showDialog(bv); return true; } DispatchResult InsetNote::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos) { BufferView * bv = cmd.view(); switch (cmd.action) { case LFUN_INSET_MODIFY: { InsetNoteMailer::string2params(cmd.argument, params_); setButtonLabel(); bv->update(); return DispatchResult(true, true); } case LFUN_INSET_DIALOG_UPDATE: InsetNoteMailer(*this).updateDialog(bv); return DispatchResult(true, true); case LFUN_MOUSE_RELEASE: if (cmd.button() == mouse_button::button3 && hitButton(cmd)) { InsetNoteMailer(*this).showDialog(bv); return DispatchResult(true, true); } // fallthrough: default: return InsetCollapsable::priv_dispatch(cmd, idx, pos); } } int InsetNote::latex(Buffer const & buf, ostream & os, OutputParams const & runparams) const { string const pt = params_.type; int i = 0; if (pt == "Comment") // verbatim os << "%\n\\begin{comment}\n"; else if (pt == "Greyedout") // we roll our own macro os << "%\n\\begin{lyxgreyedout}\n"; if (pt != "Note") i = inset.latex(buf, os, runparams); if (pt == "Comment") { os << "%\n\\end{comment}\n"; i += 4; } else if (pt == "Greyedout") { os << "%\n\\end{lyxgreyedout}\n"; i += 4; } return i; } int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os, OutputParams const & runparams) const { string const pt = params_.type; int i = 0; if (pt == "Comment") os << "\n"; if (pt != "Note") i = inset.linuxdoc(buf, os, runparams); if (pt == "Comment") { os << "\n\n"; i += 3; } return i; } int InsetNote::docbook(Buffer const & buf, std::ostream & os, OutputParams const & runparams) const { string const pt = params_.type; int i = 0; if (pt == "Comment") os << "\n"; if (pt != "Note") i = inset.docbook(buf, os, runparams); if (pt == "Comment") { os << "\n\n"; i += 3; } return i; } int InsetNote::plaintext(Buffer const & buf, std::ostream & os, OutputParams const & runparams) const { int i = 0; string const pt = params_.type; if (pt != "Note") { os << "["; i = inset.plaintext(buf, os, runparams); os << "]"; } return i; } void InsetNote::validate(LaTeXFeatures & features) const { if (params_.type == "Comment") features.require("verbatim"); if (params_.type == "Greyedout") { features.require("color"); features.require("lyxgreyedout"); } inset.validate(features); } string const InsetNoteMailer:: name_("note"); InsetNoteMailer::InsetNoteMailer(InsetNote & inset) : inset_(inset) {} string const InsetNoteMailer::inset2string(Buffer const &) const { return params2string(inset_.params()); } string const InsetNoteMailer::params2string(InsetNoteParams const & params) { ostringstream data; data << name_ << ' '; params.write(data); return data.str(); } void InsetNoteMailer::string2params(string const & in, InsetNoteParams & params) { params = InsetNoteParams(); if (in.empty()) return; istringstream data(in); LyXLex lex(0,0); lex.setStream(data); params.read(lex); } void InsetNoteParams::write(ostream & os) const { os << type << "\n"; } void InsetNoteParams::read(LyXLex & lex) { if (lex.isOK()) { lex.next(); string token = lex.getString(); } if (lex.isOK()) { lex.next(); type = lex.getString(); } }