3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "insetnote.h"
17 #include "BufferView.h"
20 #include "dispatchresult.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
25 #include "LaTeXFeatures.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
32 #include "support/lyxalgo.h"
33 #include "support/translator.h"
39 using std::istringstream;
41 using std::ostringstream;
46 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
48 NoteTranslator const init_notetranslator() {
49 NoteTranslator translator("Note", InsetNoteParams::Note);
50 translator.addPair("Comment", InsetNoteParams::Comment);
51 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
52 translator.addPair("Framed", InsetNoteParams::Framed);
53 translator.addPair("Shaded", InsetNoteParams::Shaded);
58 NoteTranslator const init_notetranslator_loc() {
59 NoteTranslator translator(_("Note"), InsetNoteParams::Note);
60 translator.addPair(_("Comment"), InsetNoteParams::Comment);
61 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
62 translator.addPair(_("Framed"), InsetNoteParams::Framed);
63 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
68 NoteTranslator const & notetranslator() {
69 static NoteTranslator translator = init_notetranslator();
74 NoteTranslator const & notetranslator_loc() {
75 static NoteTranslator translator = init_notetranslator_loc();
84 InsetNoteParams::InsetNoteParams()
89 void InsetNoteParams::write(ostream & os) const
91 string const label = notetranslator().find(type);
92 os << "Note " << label << "\n";
96 void InsetNoteParams::read(LyXLex & lex)
101 type = notetranslator().find(label);
105 void InsetNote::init()
107 setInsetName("Note");
112 InsetNote::InsetNote(BufferParams const & bp, string const & label)
113 : InsetCollapsable(bp)
115 params_.type = notetranslator().find(label);
120 InsetNote::InsetNote(InsetNote const & in)
121 : InsetCollapsable(in), params_(in.params_)
127 InsetNote::~InsetNote()
129 InsetNoteMailer(*this).hideDialog();
133 auto_ptr<InsetBase> InsetNote::doClone() const
135 return auto_ptr<InsetBase>(new InsetNote(*this));
139 string const InsetNote::editMessage() const
141 return _("Opened Note Inset");
145 void InsetNote::write(Buffer const & buf, ostream & os) const
148 InsetCollapsable::write(buf, os);
152 void InsetNote::read(Buffer const & buf, LyXLex & lex)
155 InsetCollapsable::read(buf, lex);
160 void InsetNote::setButtonLabel()
162 string const label = notetranslator_loc().find(params_.type);
165 LyXFont font(LyXFont::ALL_SANE);
169 switch (params_.type) {
170 case InsetNoteParams::Note:
171 font.setColor(LColor::note);
172 setBackgroundColor(LColor::notebg);
174 case InsetNoteParams::Comment:
175 font.setColor(LColor::comment);
176 setBackgroundColor(LColor::commentbg);
178 case InsetNoteParams::Greyedout:
179 font.setColor(LColor::greyedout);
180 setBackgroundColor(LColor::greyedoutbg);
182 case InsetNoteParams::Framed:
183 font.setColor(LColor::greyedout);
184 setBackgroundColor(LColor::greyedoutbg);
186 case InsetNoteParams::Shaded:
187 font.setColor(LColor::greyedout);
188 setBackgroundColor(LColor::shadedbg);
195 bool InsetNote::showInsetDialog(BufferView * bv) const
197 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
202 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
204 switch (cmd.action) {
206 case LFUN_INSET_MODIFY:
207 InsetNoteMailer::string2params(cmd.argument, params_);
211 case LFUN_INSET_DIALOG_UPDATE:
212 InsetNoteMailer(*this).updateDialog(&cur.bv());
215 case LFUN_MOUSE_RELEASE:
216 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
217 InsetNoteMailer(*this).showDialog(&cur.bv());
219 InsetCollapsable::doDispatch(cur, cmd);
223 InsetCollapsable::doDispatch(cur, cmd);
229 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
230 FuncStatus & flag) const
232 switch (cmd.action) {
234 case LFUN_INSET_MODIFY:
235 case LFUN_INSET_DIALOG_UPDATE:
240 return InsetCollapsable::getStatus(cur, cmd, flag);
245 int InsetNote::latex(Buffer const & buf, ostream & os,
246 OutputParams const & runparams_in) const
248 if (params_.type == InsetNoteParams::Note)
251 OutputParams runparams(runparams_in);
253 if (params_.type == InsetNoteParams::Comment) {
255 runparams.inComment = true;
256 // Ignore files that are exported inside a comment
257 runparams.exportdata.reset(new ExportData);
258 } else if (params_.type == InsetNoteParams::Greyedout)
259 type = "lyxgreyedout";
260 else if (params_.type == InsetNoteParams::Framed)
262 else if (params_.type == InsetNoteParams::Shaded)
266 ss << "%\n\\begin{" << type << "}\n";
267 InsetText::latex(buf, ss, runparams);
268 ss << "\n\\end{" << type << "}\n";
269 // the space after the comment in 'a[comment] b' will be eaten by the
270 // comment environment since the space before b is ignored with the
271 // following latex output:
279 // Adding {} before ' b' fixes this.
280 if (params_.type == InsetNoteParams::Comment)
283 string const str = ss.str();
285 // Return how many newlines we issued.
286 return int(lyx::count(str.begin(), str.end(),'\n'));
290 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
291 OutputParams const & runparams_in) const
293 if (params_.type == InsetNoteParams::Note)
296 OutputParams runparams(runparams_in);
298 if (params_.type == InsetNoteParams::Comment) {
300 runparams.inComment = true;
301 // Ignore files that are exported inside a comment
302 runparams.exportdata.reset(new ExportData);
305 InsetText::docbook(buf, ss, runparams);
307 if (params_.type == InsetNoteParams::Comment)
308 ss << "\n</remark>\n";
310 string const str = ss.str();
312 // Return how many newlines we issued.
313 return int(lyx::count(str.begin(), str.end(),'\n'));
317 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
318 OutputParams const & runparams_in) const
320 if (params_.type == InsetNoteParams::Note)
323 OutputParams runparams(runparams_in);
324 if (params_.type == InsetNoteParams::Comment) {
325 runparams.inComment = true;
326 // Ignore files that are exported inside a comment
327 runparams.exportdata.reset(new ExportData);
331 InsetText::plaintext(buf, ss, runparams);
334 string const str = ss.str();
336 // Return how many newlines we issued.
337 return int(lyx::count(str.begin(), str.end(),'\n'));
341 void InsetNote::validate(LaTeXFeatures & features) const
343 if (params_.type == InsetNoteParams::Comment)
344 features.require("verbatim");
345 if (params_.type == InsetNoteParams::Greyedout) {
346 features.require("color");
347 features.require("lyxgreyedout");
349 if (params_.type == InsetNoteParams::Shaded) {
350 features.require("color");
351 features.require("framed");
353 if (params_.type == InsetNoteParams::Framed)
354 features.require("framed");
355 InsetText::validate(features);
360 string const InsetNoteMailer::name_("note");
362 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
367 string const InsetNoteMailer::inset2string(Buffer const &) const
369 return params2string(inset_.params());
373 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
376 data << name_ << ' ';
382 void InsetNoteMailer::string2params(string const & in,
383 InsetNoteParams & params)
385 params = InsetNoteParams();
390 istringstream data(in);
396 if (!lex || name != name_)
397 return print_mailer_error("InsetNoteMailer", in, 1, name_);
399 // This is part of the inset proper that is usually swallowed
400 // by LyXText::readInset
403 if (!lex || id != "Note")
404 return print_mailer_error("InsetBoxMailer", in, 2, "Note");