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"
42 using std::istringstream;
44 using std::ostringstream;
49 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
50 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
52 NoteTranslator const init_notetranslator()
54 NoteTranslator translator("Note", InsetNoteParams::Note);
55 translator.addPair("Comment", InsetNoteParams::Comment);
56 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
57 translator.addPair("Framed", InsetNoteParams::Framed);
58 translator.addPair("Shaded", InsetNoteParams::Shaded);
63 NoteTranslatorLoc const init_notetranslator_loc()
65 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
66 translator.addPair(_("Comment"), InsetNoteParams::Comment);
67 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
68 translator.addPair(_("Framed"), InsetNoteParams::Framed);
69 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
74 NoteTranslator const & notetranslator()
76 static NoteTranslator translator = init_notetranslator();
81 NoteTranslatorLoc const & notetranslator_loc()
83 static NoteTranslatorLoc 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(from_ascii("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()
170 docstring const label = notetranslator_loc().find(params_.type);
173 LyXFont font(LyXFont::ALL_SANE);
177 switch (params_.type) {
178 case InsetNoteParams::Note:
179 font.setColor(LColor::note);
180 setBackgroundColor(LColor::notebg);
182 case InsetNoteParams::Comment:
183 font.setColor(LColor::comment);
184 setBackgroundColor(LColor::commentbg);
186 case InsetNoteParams::Greyedout:
187 font.setColor(LColor::greyedout);
188 setBackgroundColor(LColor::greyedoutbg);
190 case InsetNoteParams::Framed:
191 font.setColor(LColor::greyedout);
192 setBackgroundColor(LColor::greyedoutbg);
194 case InsetNoteParams::Shaded:
195 font.setColor(LColor::greyedout);
196 setBackgroundColor(LColor::shadedbg);
203 bool InsetNote::showInsetDialog(BufferView * bv) const
205 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
210 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
212 switch (cmd.action) {
214 case LFUN_INSET_MODIFY:
215 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
219 case LFUN_INSET_DIALOG_UPDATE:
220 InsetNoteMailer(*this).updateDialog(&cur.bv());
223 case LFUN_MOUSE_RELEASE:
224 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
225 InsetNoteMailer(*this).showDialog(&cur.bv());
227 InsetCollapsable::doDispatch(cur, cmd);
231 InsetCollapsable::doDispatch(cur, cmd);
237 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
238 FuncStatus & flag) const
240 switch (cmd.action) {
242 case LFUN_INSET_MODIFY:
243 case LFUN_INSET_DIALOG_UPDATE:
248 return InsetCollapsable::getStatus(cur, cmd, flag);
253 int InsetNote::latex(Buffer const & buf, odocstream & os,
254 OutputParams const & runparams_in) const
256 if (params_.type == InsetNoteParams::Note)
259 OutputParams runparams(runparams_in);
261 if (params_.type == InsetNoteParams::Comment) {
263 runparams.inComment = true;
264 // Ignore files that are exported inside a comment
265 runparams.exportdata.reset(new ExportData);
266 } else if (params_.type == InsetNoteParams::Greyedout)
267 type = "lyxgreyedout";
268 else if (params_.type == InsetNoteParams::Framed)
270 else if (params_.type == InsetNoteParams::Shaded)
274 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
275 InsetText::latex(buf, ss, runparams);
276 ss << "\n\\end{" << from_ascii(type) << "}\n";
277 // the space after the comment in 'a[comment] b' will be eaten by the
278 // comment environment since the space before b is ignored with the
279 // following latex output:
287 // Adding {} before ' b' fixes this.
288 if (params_.type == InsetNoteParams::Comment)
291 docstring const str = ss.str();
293 // Return how many newlines we issued.
294 return int(lyx::count(str.begin(), str.end(), '\n'));
298 int InsetNote::docbook(Buffer const & buf, odocstream & os,
299 OutputParams const & runparams_in) const
301 if (params_.type == InsetNoteParams::Note)
304 OutputParams runparams(runparams_in);
305 if (params_.type == InsetNoteParams::Comment) {
307 runparams.inComment = true;
308 // Ignore files that are exported inside a comment
309 runparams.exportdata.reset(new ExportData);
312 int const n = InsetText::docbook(buf, os, runparams);
314 if (params_.type == InsetNoteParams::Comment)
315 os << "\n</remark>\n";
317 // Return how many newlines we issued.
318 //return int(count(str.begin(), str.end(), '\n'));
323 int InsetNote::plaintext(Buffer const & buf, odocstream & 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);
336 int const nlines = InsetText::plaintext(buf, os, runparams);
339 // Return how many newlines we issued.
344 void InsetNote::validate(LaTeXFeatures & features) const
346 if (params_.type == InsetNoteParams::Comment)
347 features.require("verbatim");
348 if (params_.type == InsetNoteParams::Greyedout) {
349 features.require("color");
350 features.require("lyxgreyedout");
352 if (params_.type == InsetNoteParams::Shaded) {
353 features.require("color");
354 features.require("framed");
356 if (params_.type == InsetNoteParams::Framed)
357 features.require("framed");
358 InsetText::validate(features);
363 string const InsetNoteMailer::name_("note");
365 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
370 string const InsetNoteMailer::inset2string(Buffer const &) const
372 return params2string(inset_.params());
376 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
379 data << name_ << ' ';
385 void InsetNoteMailer::string2params(string const & in,
386 InsetNoteParams & params)
388 params = InsetNoteParams();
393 istringstream data(in);
399 if (!lex || name != name_)
400 return print_mailer_error("InsetNoteMailer", in, 1, name_);
402 // This is part of the inset proper that is usually swallowed
403 // by LyXText::readInset
406 if (!lex || id != "Note")
407 return print_mailer_error("InsetBoxMailer", in, 2, "Note");