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()
50 NoteTranslator translator("Note", InsetNoteParams::Note);
51 translator.addPair("Comment", InsetNoteParams::Comment);
52 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
53 translator.addPair("Framed", InsetNoteParams::Framed);
54 translator.addPair("Shaded", InsetNoteParams::Shaded);
59 NoteTranslator const init_notetranslator_loc()
62 NoteTranslator translator(lyx::to_utf8(_("Note")), InsetNoteParams::Note);
63 translator.addPair(lyx::to_utf8(_("Comment")), InsetNoteParams::Comment);
64 translator.addPair(lyx::to_utf8(_("Greyed out")), InsetNoteParams::Greyedout);
65 translator.addPair(lyx::to_utf8(_("Framed")), InsetNoteParams::Framed);
66 translator.addPair(lyx::to_utf8(_("Shaded")), InsetNoteParams::Shaded);
71 NoteTranslator const & notetranslator()
73 static NoteTranslator translator = init_notetranslator();
78 NoteTranslator const & notetranslator_loc()
80 static NoteTranslator translator = init_notetranslator_loc();
89 InsetNoteParams::InsetNoteParams()
94 void InsetNoteParams::write(ostream & os) const
96 string const label = notetranslator().find(type);
97 os << "Note " << label << "\n";
101 void InsetNoteParams::read(LyXLex & lex)
106 type = notetranslator().find(label);
110 void InsetNote::init()
112 setInsetName("Note");
117 InsetNote::InsetNote(BufferParams const & bp, string const & label)
118 : InsetCollapsable(bp)
120 params_.type = notetranslator().find(label);
125 InsetNote::InsetNote(InsetNote const & in)
126 : InsetCollapsable(in), params_(in.params_)
132 InsetNote::~InsetNote()
134 InsetNoteMailer(*this).hideDialog();
138 auto_ptr<InsetBase> InsetNote::doClone() const
140 return auto_ptr<InsetBase>(new InsetNote(*this));
144 string const InsetNote::editMessage() const
147 return lyx::to_utf8(_("Opened Note Inset"));
151 void InsetNote::write(Buffer const & buf, ostream & os) const
154 InsetCollapsable::write(buf, os);
158 void InsetNote::read(Buffer const & buf, LyXLex & lex)
161 InsetCollapsable::read(buf, lex);
166 void InsetNote::setButtonLabel()
168 string const label = notetranslator_loc().find(params_.type);
171 LyXFont font(LyXFont::ALL_SANE);
175 switch (params_.type) {
176 case InsetNoteParams::Note:
177 font.setColor(LColor::note);
178 setBackgroundColor(LColor::notebg);
180 case InsetNoteParams::Comment:
181 font.setColor(LColor::comment);
182 setBackgroundColor(LColor::commentbg);
184 case InsetNoteParams::Greyedout:
185 font.setColor(LColor::greyedout);
186 setBackgroundColor(LColor::greyedoutbg);
188 case InsetNoteParams::Framed:
189 font.setColor(LColor::greyedout);
190 setBackgroundColor(LColor::greyedoutbg);
192 case InsetNoteParams::Shaded:
193 font.setColor(LColor::greyedout);
194 setBackgroundColor(LColor::shadedbg);
201 bool InsetNote::showInsetDialog(BufferView * bv) const
203 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
208 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
210 switch (cmd.action) {
212 case LFUN_INSET_MODIFY:
213 InsetNoteMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
217 case LFUN_INSET_DIALOG_UPDATE:
218 InsetNoteMailer(*this).updateDialog(&cur.bv());
221 case LFUN_MOUSE_RELEASE:
222 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
223 InsetNoteMailer(*this).showDialog(&cur.bv());
225 InsetCollapsable::doDispatch(cur, cmd);
229 InsetCollapsable::doDispatch(cur, cmd);
235 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
236 FuncStatus & flag) const
238 switch (cmd.action) {
240 case LFUN_INSET_MODIFY:
241 case LFUN_INSET_DIALOG_UPDATE:
246 return InsetCollapsable::getStatus(cur, cmd, flag);
251 int InsetNote::latex(Buffer const & buf, ostream & os,
252 OutputParams const & runparams_in) const
254 if (params_.type == InsetNoteParams::Note)
257 OutputParams runparams(runparams_in);
259 if (params_.type == InsetNoteParams::Comment) {
261 runparams.inComment = true;
262 // Ignore files that are exported inside a comment
263 runparams.exportdata.reset(new ExportData);
264 } else if (params_.type == InsetNoteParams::Greyedout)
265 type = "lyxgreyedout";
266 else if (params_.type == InsetNoteParams::Framed)
268 else if (params_.type == InsetNoteParams::Shaded)
272 ss << "%\n\\begin{" << type << "}\n";
273 InsetText::latex(buf, ss, runparams);
274 ss << "\n\\end{" << type << "}\n";
275 // the space after the comment in 'a[comment] b' will be eaten by the
276 // comment environment since the space before b is ignored with the
277 // following latex output:
285 // Adding {} before ' b' fixes this.
286 if (params_.type == InsetNoteParams::Comment)
289 string const str = ss.str();
291 // Return how many newlines we issued.
292 return int(lyx::count(str.begin(), str.end(),'\n'));
296 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
297 OutputParams const & runparams_in) const
299 if (params_.type == InsetNoteParams::Note)
302 OutputParams runparams(runparams_in);
304 if (params_.type == InsetNoteParams::Comment) {
306 runparams.inComment = true;
307 // Ignore files that are exported inside a comment
308 runparams.exportdata.reset(new ExportData);
311 InsetText::docbook(buf, ss, runparams);
313 if (params_.type == InsetNoteParams::Comment)
314 ss << "\n</remark>\n";
316 string const str = ss.str();
318 // Return how many newlines we issued.
319 return int(lyx::count(str.begin(), str.end(),'\n'));
323 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
324 OutputParams const & runparams_in) const
326 if (params_.type == InsetNoteParams::Note)
329 OutputParams runparams(runparams_in);
330 if (params_.type == InsetNoteParams::Comment) {
331 runparams.inComment = true;
332 // Ignore files that are exported inside a comment
333 runparams.exportdata.reset(new ExportData);
337 InsetText::plaintext(buf, ss, runparams);
340 string const str = ss.str();
342 // Return how many newlines we issued.
343 return int(lyx::count(str.begin(), str.end(),'\n'));
347 void InsetNote::validate(LaTeXFeatures & features) const
349 if (params_.type == InsetNoteParams::Comment)
350 features.require("verbatim");
351 if (params_.type == InsetNoteParams::Greyedout) {
352 features.require("color");
353 features.require("lyxgreyedout");
355 if (params_.type == InsetNoteParams::Shaded) {
356 features.require("color");
357 features.require("framed");
359 if (params_.type == InsetNoteParams::Framed)
360 features.require("framed");
361 InsetText::validate(features);
366 string const InsetNoteMailer::name_("note");
368 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
373 string const InsetNoteMailer::inset2string(Buffer const &) const
375 return params2string(inset_.params());
379 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
382 data << name_ << ' ';
388 void InsetNoteMailer::string2params(string const & in,
389 InsetNoteParams & params)
391 params = InsetNoteParams();
396 istringstream data(in);
402 if (!lex || name != name_)
403 return print_mailer_error("InsetNoteMailer", in, 1, name_);
405 // This is part of the inset proper that is usually swallowed
406 // by LyXText::readInset
409 if (!lex || id != "Note")
410 return print_mailer_error("InsetBoxMailer", in, 2, "Note");