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"
38 using lyx::odocstream;
42 using std::istringstream;
44 using std::ostringstream;
49 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
51 NoteTranslator const init_notetranslator()
53 NoteTranslator translator("Note", InsetNoteParams::Note);
54 translator.addPair("Comment", InsetNoteParams::Comment);
55 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
56 translator.addPair("Framed", InsetNoteParams::Framed);
57 translator.addPair("Shaded", InsetNoteParams::Shaded);
62 NoteTranslator const init_notetranslator_loc()
65 NoteTranslator translator(lyx::to_utf8(_("Note")), InsetNoteParams::Note);
66 translator.addPair(lyx::to_utf8(_("Comment")), InsetNoteParams::Comment);
67 translator.addPair(lyx::to_utf8(_("Greyed out")), InsetNoteParams::Greyedout);
68 translator.addPair(lyx::to_utf8(_("Framed")), InsetNoteParams::Framed);
69 translator.addPair(lyx::to_utf8(_("Shaded")), InsetNoteParams::Shaded);
74 NoteTranslator const & notetranslator()
76 static NoteTranslator translator = init_notetranslator();
81 NoteTranslator const & notetranslator_loc()
83 static NoteTranslator translator = init_notetranslator_loc();
92 InsetNoteParams::InsetNoteParams()
97 void InsetNoteParams::write(ostream & os) const
99 string const label = notetranslator().find(type);
100 os << "Note " << label << "\n";
104 void InsetNoteParams::read(LyXLex & lex)
109 type = notetranslator().find(label);
113 void InsetNote::init()
115 setInsetName("Note");
120 InsetNote::InsetNote(BufferParams const & bp, string const & label)
121 : InsetCollapsable(bp)
123 params_.type = notetranslator().find(label);
128 InsetNote::InsetNote(InsetNote const & in)
129 : InsetCollapsable(in), params_(in.params_)
135 InsetNote::~InsetNote()
137 InsetNoteMailer(*this).hideDialog();
141 auto_ptr<InsetBase> InsetNote::doClone() const
143 return auto_ptr<InsetBase>(new InsetNote(*this));
147 docstring const InsetNote::editMessage() const
149 return _("Opened Note Inset");
153 void InsetNote::write(Buffer const & buf, ostream & os) const
156 InsetCollapsable::write(buf, os);
160 void InsetNote::read(Buffer const & buf, LyXLex & lex)
163 InsetCollapsable::read(buf, lex);
168 void InsetNote::setButtonLabel()
171 docstring const label = lyx::from_utf8(notetranslator_loc().find(params_.type));
174 LyXFont font(LyXFont::ALL_SANE);
178 switch (params_.type) {
179 case InsetNoteParams::Note:
180 font.setColor(LColor::note);
181 setBackgroundColor(LColor::notebg);
183 case InsetNoteParams::Comment:
184 font.setColor(LColor::comment);
185 setBackgroundColor(LColor::commentbg);
187 case InsetNoteParams::Greyedout:
188 font.setColor(LColor::greyedout);
189 setBackgroundColor(LColor::greyedoutbg);
191 case InsetNoteParams::Framed:
192 font.setColor(LColor::greyedout);
193 setBackgroundColor(LColor::greyedoutbg);
195 case InsetNoteParams::Shaded:
196 font.setColor(LColor::greyedout);
197 setBackgroundColor(LColor::shadedbg);
204 bool InsetNote::showInsetDialog(BufferView * bv) const
206 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
211 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
213 switch (cmd.action) {
215 case LFUN_INSET_MODIFY:
216 InsetNoteMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
220 case LFUN_INSET_DIALOG_UPDATE:
221 InsetNoteMailer(*this).updateDialog(&cur.bv());
224 case LFUN_MOUSE_RELEASE:
225 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
226 InsetNoteMailer(*this).showDialog(&cur.bv());
228 InsetCollapsable::doDispatch(cur, cmd);
232 InsetCollapsable::doDispatch(cur, cmd);
238 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
239 FuncStatus & flag) const
241 switch (cmd.action) {
243 case LFUN_INSET_MODIFY:
244 case LFUN_INSET_DIALOG_UPDATE:
249 return InsetCollapsable::getStatus(cur, cmd, flag);
254 int InsetNote::latex(Buffer const & buf, odocstream & os,
255 OutputParams const & runparams_in) const
257 if (params_.type == InsetNoteParams::Note)
260 OutputParams runparams(runparams_in);
262 if (params_.type == InsetNoteParams::Comment) {
264 runparams.inComment = true;
265 // Ignore files that are exported inside a comment
266 runparams.exportdata.reset(new ExportData);
267 } else if (params_.type == InsetNoteParams::Greyedout)
268 type = "lyxgreyedout";
269 else if (params_.type == InsetNoteParams::Framed)
271 else if (params_.type == InsetNoteParams::Shaded)
274 lyx::odocstringstream ss;
275 ss << "%\n\\begin{" << lyx::from_ascii(type) << "}\n";
276 InsetText::latex(buf, ss, runparams);
277 ss << "\n\\end{" << lyx::from_ascii(type) << "}\n";
278 // the space after the comment in 'a[comment] b' will be eaten by the
279 // comment environment since the space before b is ignored with the
280 // following latex output:
288 // Adding {} before ' b' fixes this.
289 if (params_.type == InsetNoteParams::Comment)
292 docstring const str = ss.str();
294 // Return how many newlines we issued.
295 return int(lyx::count(str.begin(), str.end(), '\n'));
299 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
300 OutputParams const & runparams_in) const
302 if (params_.type == InsetNoteParams::Note)
305 OutputParams runparams(runparams_in);
307 if (params_.type == InsetNoteParams::Comment) {
309 runparams.inComment = true;
310 // Ignore files that are exported inside a comment
311 runparams.exportdata.reset(new ExportData);
314 InsetText::docbook(buf, ss, runparams);
316 if (params_.type == InsetNoteParams::Comment)
317 ss << "\n</remark>\n";
319 string const str = ss.str();
321 // Return how many newlines we issued.
322 return int(lyx::count(str.begin(), str.end(),'\n'));
326 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
327 OutputParams const & runparams_in) const
329 if (params_.type == InsetNoteParams::Note)
332 OutputParams runparams(runparams_in);
333 if (params_.type == InsetNoteParams::Comment) {
334 runparams.inComment = true;
335 // Ignore files that are exported inside a comment
336 runparams.exportdata.reset(new ExportData);
339 int const nlines = InsetText::plaintext(buf, os, runparams);
342 // Return how many newlines we issued.
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");