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";
270 string const str = ss.str();
272 // Return how many newlines we issued.
273 return int(lyx::count(str.begin(), str.end(),'\n'));
277 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
278 OutputParams const & runparams_in) const
280 if (params_.type == InsetNoteParams::Note)
283 OutputParams runparams(runparams_in);
285 if (params_.type == InsetNoteParams::Comment) {
287 runparams.inComment = true;
288 // Ignore files that are exported inside a comment
289 runparams.exportdata.reset(new ExportData);
292 InsetText::linuxdoc(buf, ss, runparams);
294 if (params_.type == InsetNoteParams::Comment)
295 ss << "\n</comment>\n";
297 string const str = ss.str();
299 // Return how many newlines we issued.
300 return int(lyx::count(str.begin(), str.end(),'\n'));
304 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
305 OutputParams const & runparams_in) const
307 if (params_.type == InsetNoteParams::Note)
310 OutputParams runparams(runparams_in);
312 if (params_.type == InsetNoteParams::Comment) {
314 runparams.inComment = true;
315 // Ignore files that are exported inside a comment
316 runparams.exportdata.reset(new ExportData);
319 InsetText::docbook(buf, ss, runparams);
321 if (params_.type == InsetNoteParams::Comment)
322 ss << "\n</remark>\n";
324 string const str = ss.str();
326 // Return how many newlines we issued.
327 return int(lyx::count(str.begin(), str.end(),'\n'));
331 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
332 OutputParams const & runparams_in) const
334 if (params_.type == InsetNoteParams::Note)
337 OutputParams runparams(runparams_in);
338 if (params_.type == InsetNoteParams::Comment) {
339 runparams.inComment = true;
340 // Ignore files that are exported inside a comment
341 runparams.exportdata.reset(new ExportData);
345 InsetText::plaintext(buf, ss, runparams);
348 string const str = ss.str();
350 // Return how many newlines we issued.
351 return int(lyx::count(str.begin(), str.end(),'\n'));
355 void InsetNote::validate(LaTeXFeatures & features) const
357 if (params_.type == InsetNoteParams::Comment)
358 features.require("verbatim");
359 if (params_.type == InsetNoteParams::Greyedout) {
360 features.require("color");
361 features.require("lyxgreyedout");
363 if (params_.type == InsetNoteParams::Shaded) {
364 features.require("color");
365 features.require("framed");
367 if (params_.type == InsetNoteParams::Framed)
368 features.require("framed");
369 InsetText::validate(features);
374 string const InsetNoteMailer::name_("note");
376 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
381 string const InsetNoteMailer::inset2string(Buffer const &) const
383 return params2string(inset_.params());
387 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
390 data << name_ << ' ';
396 void InsetNoteMailer::string2params(string const & in,
397 InsetNoteParams & params)
399 params = InsetNoteParams();
404 istringstream data(in);
410 if (!lex || name != name_)
411 return print_mailer_error("InsetNoteMailer", in, 1, name_);
413 // This is part of the inset proper that is usually swallowed
414 // by LyXText::readInset
417 if (!lex || id != "Note")
418 return print_mailer_error("InsetBoxMailer", in, 2, "Note");