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"
41 using std::istringstream;
43 using std::ostringstream;
48 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
50 NoteTranslator const init_notetranslator()
52 NoteTranslator translator("Note", InsetNoteParams::Note);
53 translator.addPair("Comment", InsetNoteParams::Comment);
54 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
55 translator.addPair("Framed", InsetNoteParams::Framed);
56 translator.addPair("Shaded", InsetNoteParams::Shaded);
61 NoteTranslator const init_notetranslator_loc()
64 NoteTranslator translator(lyx::to_utf8(_("Note")), InsetNoteParams::Note);
65 translator.addPair(lyx::to_utf8(_("Comment")), InsetNoteParams::Comment);
66 translator.addPair(lyx::to_utf8(_("Greyed out")), InsetNoteParams::Greyedout);
67 translator.addPair(lyx::to_utf8(_("Framed")), InsetNoteParams::Framed);
68 translator.addPair(lyx::to_utf8(_("Shaded")), InsetNoteParams::Shaded);
73 NoteTranslator const & notetranslator()
75 static NoteTranslator translator = init_notetranslator();
80 NoteTranslator const & notetranslator_loc()
82 static NoteTranslator translator = init_notetranslator_loc();
91 InsetNoteParams::InsetNoteParams()
96 void InsetNoteParams::write(ostream & os) const
98 string const label = notetranslator().find(type);
99 os << "Note " << label << "\n";
103 void InsetNoteParams::read(LyXLex & lex)
108 type = notetranslator().find(label);
112 void InsetNote::init()
114 setInsetName("Note");
119 InsetNote::InsetNote(BufferParams const & bp, string const & label)
120 : InsetCollapsable(bp)
122 params_.type = notetranslator().find(label);
127 InsetNote::InsetNote(InsetNote const & in)
128 : InsetCollapsable(in), params_(in.params_)
134 InsetNote::~InsetNote()
136 InsetNoteMailer(*this).hideDialog();
140 auto_ptr<InsetBase> InsetNote::doClone() const
142 return auto_ptr<InsetBase>(new InsetNote(*this));
146 docstring const InsetNote::editMessage() const
148 return _("Opened Note Inset");
152 void InsetNote::write(Buffer const & buf, ostream & os) const
155 InsetCollapsable::write(buf, os);
159 void InsetNote::read(Buffer const & buf, LyXLex & lex)
162 InsetCollapsable::read(buf, lex);
167 void InsetNote::setButtonLabel()
169 string const label = notetranslator_loc().find(params_.type);
172 LyXFont font(LyXFont::ALL_SANE);
176 switch (params_.type) {
177 case InsetNoteParams::Note:
178 font.setColor(LColor::note);
179 setBackgroundColor(LColor::notebg);
181 case InsetNoteParams::Comment:
182 font.setColor(LColor::comment);
183 setBackgroundColor(LColor::commentbg);
185 case InsetNoteParams::Greyedout:
186 font.setColor(LColor::greyedout);
187 setBackgroundColor(LColor::greyedoutbg);
189 case InsetNoteParams::Framed:
190 font.setColor(LColor::greyedout);
191 setBackgroundColor(LColor::greyedoutbg);
193 case InsetNoteParams::Shaded:
194 font.setColor(LColor::greyedout);
195 setBackgroundColor(LColor::shadedbg);
202 bool InsetNote::showInsetDialog(BufferView * bv) const
204 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
209 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
211 switch (cmd.action) {
213 case LFUN_INSET_MODIFY:
214 InsetNoteMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
218 case LFUN_INSET_DIALOG_UPDATE:
219 InsetNoteMailer(*this).updateDialog(&cur.bv());
222 case LFUN_MOUSE_RELEASE:
223 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
224 InsetNoteMailer(*this).showDialog(&cur.bv());
226 InsetCollapsable::doDispatch(cur, cmd);
230 InsetCollapsable::doDispatch(cur, cmd);
236 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
237 FuncStatus & flag) const
239 switch (cmd.action) {
241 case LFUN_INSET_MODIFY:
242 case LFUN_INSET_DIALOG_UPDATE:
247 return InsetCollapsable::getStatus(cur, cmd, flag);
252 int InsetNote::latex(Buffer const & buf, ostream & os,
253 OutputParams const & runparams_in) const
255 if (params_.type == InsetNoteParams::Note)
258 OutputParams runparams(runparams_in);
260 if (params_.type == InsetNoteParams::Comment) {
262 runparams.inComment = true;
263 // Ignore files that are exported inside a comment
264 runparams.exportdata.reset(new ExportData);
265 } else if (params_.type == InsetNoteParams::Greyedout)
266 type = "lyxgreyedout";
267 else if (params_.type == InsetNoteParams::Framed)
269 else if (params_.type == InsetNoteParams::Shaded)
273 ss << "%\n\\begin{" << type << "}\n";
274 InsetText::latex(buf, ss, runparams);
275 ss << "\n\\end{" << type << "}\n";
276 // the space after the comment in 'a[comment] b' will be eaten by the
277 // comment environment since the space before b is ignored with the
278 // following latex output:
286 // Adding {} before ' b' fixes this.
287 if (params_.type == InsetNoteParams::Comment)
290 string const str = ss.str();
292 // Return how many newlines we issued.
293 return int(lyx::count(str.begin(), str.end(),'\n'));
297 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
298 OutputParams const & runparams_in) const
300 if (params_.type == InsetNoteParams::Note)
303 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 InsetText::docbook(buf, ss, runparams);
314 if (params_.type == InsetNoteParams::Comment)
315 ss << "\n</remark>\n";
317 string const str = ss.str();
319 // Return how many newlines we issued.
320 return int(lyx::count(str.begin(), str.end(),'\n'));
324 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
325 OutputParams const & runparams_in) const
327 if (params_.type == InsetNoteParams::Note)
330 OutputParams runparams(runparams_in);
331 if (params_.type == InsetNoteParams::Comment) {
332 runparams.inComment = true;
333 // Ignore files that are exported inside a comment
334 runparams.exportdata.reset(new ExportData);
338 InsetText::plaintext(buf, ss, runparams);
341 string const str = ss.str();
343 // Return how many newlines we issued.
344 return int(lyx::count(str.begin(), str.end(),'\n'));
348 void InsetNote::validate(LaTeXFeatures & features) const
350 if (params_.type == InsetNoteParams::Comment)
351 features.require("verbatim");
352 if (params_.type == InsetNoteParams::Greyedout) {
353 features.require("color");
354 features.require("lyxgreyedout");
356 if (params_.type == InsetNoteParams::Shaded) {
357 features.require("color");
358 features.require("framed");
360 if (params_.type == InsetNoteParams::Framed)
361 features.require("framed");
362 InsetText::validate(features);
367 string const InsetNoteMailer::name_("note");
369 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
374 string const InsetNoteMailer::inset2string(Buffer const &) const
376 return params2string(inset_.params());
380 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
383 data << name_ << ' ';
389 void InsetNoteMailer::string2params(string const & in,
390 InsetNoteParams & params)
392 params = InsetNoteParams();
397 istringstream data(in);
403 if (!lex || name != name_)
404 return print_mailer_error("InsetNoteMailer", in, 1, name_);
406 // This is part of the inset proper that is usually swallowed
407 // by LyXText::readInset
410 if (!lex || id != "Note")
411 return print_mailer_error("InsetBoxMailer", in, 2, "Note");