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);
56 NoteTranslator const init_notetranslator_loc() {
57 NoteTranslator translator(_("Note"), InsetNoteParams::Note);
58 translator.addPair(_("Comment"), InsetNoteParams::Comment);
59 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
64 NoteTranslator const & notetranslator() {
65 static NoteTranslator translator = init_notetranslator();
70 NoteTranslator const & notetranslator_loc() {
71 static NoteTranslator translator = init_notetranslator_loc();
80 InsetNoteParams::InsetNoteParams()
85 void InsetNoteParams::write(ostream & os) const
87 string const label = notetranslator().find(type);
88 os << "Note " << label << "\n";
92 void InsetNoteParams::read(LyXLex & lex)
97 type = notetranslator().find(label);
101 void InsetNote::init()
103 setInsetName("Note");
108 InsetNote::InsetNote(BufferParams const & bp, string const & label)
109 : InsetCollapsable(bp)
111 params_.type = notetranslator().find(label);
116 InsetNote::InsetNote(InsetNote const & in)
117 : InsetCollapsable(in), params_(in.params_)
123 InsetNote::~InsetNote()
125 InsetNoteMailer(*this).hideDialog();
129 auto_ptr<InsetBase> InsetNote::doClone() const
131 return auto_ptr<InsetBase>(new InsetNote(*this));
135 string const InsetNote::editMessage() const
137 return _("Opened Note Inset");
141 void InsetNote::write(Buffer const & buf, ostream & os) const
144 InsetCollapsable::write(buf, os);
148 void InsetNote::read(Buffer const & buf, LyXLex & lex)
151 InsetCollapsable::read(buf, lex);
156 void InsetNote::setButtonLabel()
158 string const label = notetranslator_loc().find(params_.type);
161 LyXFont font(LyXFont::ALL_SANE);
165 switch (params_.type) {
166 case InsetNoteParams::Note:
167 font.setColor(LColor::note);
168 setBackgroundColor(LColor::notebg);
170 case InsetNoteParams::Comment:
171 font.setColor(LColor::comment);
172 setBackgroundColor(LColor::commentbg);
174 case InsetNoteParams::Greyedout:
175 font.setColor(LColor::greyedout);
176 setBackgroundColor(LColor::greyedoutbg);
183 bool InsetNote::showInsetDialog(BufferView * bv) const
185 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
190 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
192 switch (cmd.action) {
194 case LFUN_INSET_MODIFY:
195 InsetNoteMailer::string2params(cmd.argument, params_);
199 case LFUN_INSET_DIALOG_UPDATE:
200 InsetNoteMailer(*this).updateDialog(&cur.bv());
203 case LFUN_MOUSE_RELEASE:
204 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
205 InsetNoteMailer(*this).showDialog(&cur.bv());
207 InsetCollapsable::doDispatch(cur, cmd);
211 InsetCollapsable::doDispatch(cur, cmd);
217 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
218 FuncStatus & flag) const
220 switch (cmd.action) {
222 case LFUN_INSET_MODIFY:
223 case LFUN_INSET_DIALOG_UPDATE:
228 return InsetCollapsable::getStatus(cur, cmd, flag);
233 int InsetNote::latex(Buffer const & buf, ostream & os,
234 OutputParams const & runparams_in) const
236 if (params_.type == InsetNoteParams::Note)
239 OutputParams runparams(runparams_in);
241 if (params_.type == InsetNoteParams::Comment) {
243 runparams.inComment = true;
244 // Ignore files that are exported inside a comment
245 runparams.exportdata.reset(new ExportData);
246 } else if (params_.type == InsetNoteParams::Greyedout)
247 type = "lyxgreyedout";
250 ss << "%\n\\begin{" << type << "}\n";
251 InsetText::latex(buf, ss, runparams);
252 ss << "%\n\\end{" << type << "}\n";
254 string const str = ss.str();
256 // Return how many newlines we issued.
257 return int(lyx::count(str.begin(), str.end(),'\n'));
261 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
262 OutputParams const & runparams_in) const
264 if (params_.type == InsetNoteParams::Note)
267 OutputParams runparams(runparams_in);
269 if (params_.type == InsetNoteParams::Comment) {
271 runparams.inComment = true;
272 // Ignore files that are exported inside a comment
273 runparams.exportdata.reset(new ExportData);
276 InsetText::linuxdoc(buf, ss, runparams);
278 if (params_.type == InsetNoteParams::Comment)
279 ss << "\n</comment>\n";
281 string const str = ss.str();
283 // Return how many newlines we issued.
284 return int(lyx::count(str.begin(), str.end(),'\n'));
288 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
289 OutputParams const & runparams_in) const
291 if (params_.type == InsetNoteParams::Note)
294 OutputParams runparams(runparams_in);
296 if (params_.type == InsetNoteParams::Comment) {
298 runparams.inComment = true;
299 // Ignore files that are exported inside a comment
300 runparams.exportdata.reset(new ExportData);
303 InsetText::docbook(buf, ss, runparams);
305 if (params_.type == InsetNoteParams::Comment)
306 ss << "\n</remark>\n";
308 string const str = ss.str();
310 // Return how many newlines we issued.
311 return int(lyx::count(str.begin(), str.end(),'\n'));
315 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
316 OutputParams const & runparams_in) const
318 if (params_.type == InsetNoteParams::Note)
321 OutputParams runparams(runparams_in);
322 if (params_.type == InsetNoteParams::Comment) {
323 runparams.inComment = true;
324 // Ignore files that are exported inside a comment
325 runparams.exportdata.reset(new ExportData);
329 InsetText::plaintext(buf, ss, runparams);
332 string const str = ss.str();
334 // Return how many newlines we issued.
335 return int(lyx::count(str.begin(), str.end(),'\n'));
339 void InsetNote::validate(LaTeXFeatures & features) const
341 if (params_.type == InsetNoteParams::Comment)
342 features.require("verbatim");
343 if (params_.type == InsetNoteParams::Greyedout) {
344 features.require("color");
345 features.require("lyxgreyedout");
347 InsetText::validate(features);
352 string const InsetNoteMailer::name_("note");
354 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
359 string const InsetNoteMailer::inset2string(Buffer const &) const
361 return params2string(inset_.params());
365 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
368 data << name_ << ' ';
374 void InsetNoteMailer::string2params(string const & in,
375 InsetNoteParams & params)
377 params = InsetNoteParams();
382 istringstream data(in);
388 if (!lex || name != name_)
389 return print_mailer_error("InsetNoteMailer", in, 1, name_);
391 // This is part of the inset proper that is usually swallowed
392 // by LyXText::readInset
395 if (!lex || id != "Note")
396 return print_mailer_error("InsetBoxMailer", in, 2, "Note");