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"
18 #include "BufferParams.h"
19 #include "BufferView.h"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
33 #include "support/Translator.h"
43 using std::istringstream;
45 using std::ostringstream;
50 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
51 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
53 NoteTranslator const init_notetranslator()
55 NoteTranslator translator("Note", InsetNoteParams::Note);
56 translator.addPair("Comment", InsetNoteParams::Comment);
57 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
58 translator.addPair("Framed", InsetNoteParams::Framed);
59 translator.addPair("Shaded", InsetNoteParams::Shaded);
64 NoteTranslatorLoc const init_notetranslator_loc()
66 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
67 translator.addPair(_("Comment"), InsetNoteParams::Comment);
68 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
69 translator.addPair(_("Framed"), InsetNoteParams::Framed);
70 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
75 NoteTranslator const & notetranslator()
77 static NoteTranslator translator = init_notetranslator();
82 NoteTranslatorLoc const & notetranslator_loc()
84 static NoteTranslatorLoc translator = init_notetranslator_loc();
93 InsetNoteParams::InsetNoteParams()
98 void InsetNoteParams::write(ostream & os) const
100 string const label = notetranslator().find(type);
101 os << "Note " << label << "\n";
105 void InsetNoteParams::read(Lexer & lex)
110 type = notetranslator().find(label);
114 void InsetNote::init()
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<Inset> InsetNote::doClone() const
143 return auto_ptr<Inset>(new InsetNote(*this));
147 docstring const InsetNote::editMessage() const
149 return _("Opened Note Inset");
153 Inset::DisplayType InsetNote::display() const
155 switch (params_.type) {
156 case InsetNoteParams::Framed:
157 case InsetNoteParams::Shaded:
165 void InsetNote::write(Buffer const & buf, ostream & os) const
168 InsetCollapsable::write(buf, os);
172 void InsetNote::read(Buffer const & buf, Lexer & lex)
175 InsetCollapsable::read(buf, lex);
180 void InsetNote::setButtonLabel()
182 docstring const label = notetranslator_loc().find(params_.type);
185 Font font(Font::ALL_SANE);
190 switch (params_.type) {
191 case InsetNoteParams::Note:
194 case InsetNoteParams::Comment:
197 case InsetNoteParams::Greyedout:
198 c = Color::greyedout;
200 case InsetNoteParams::Framed:
201 c = Color::greyedout;
203 case InsetNoteParams::Shaded:
204 c = Color::greyedout;
212 Color_color InsetNote::backgroundColor() const
215 switch (params_.type) {
216 case InsetNoteParams::Note:
219 case InsetNoteParams::Comment:
220 c = Color::commentbg;
222 case InsetNoteParams::Greyedout:
223 c = Color::greyedoutbg;
225 case InsetNoteParams::Framed:
226 c = Color::greyedoutbg;
228 case InsetNoteParams::Shaded:
236 bool InsetNote::showInsetDialog(BufferView * bv) const
238 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
243 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
245 switch (cmd.action) {
247 case LFUN_INSET_MODIFY:
248 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
252 case LFUN_INSET_DIALOG_UPDATE:
253 InsetNoteMailer(*this).updateDialog(&cur.bv());
256 case LFUN_MOUSE_RELEASE:
257 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
258 InsetNoteMailer(*this).showDialog(&cur.bv());
260 InsetCollapsable::doDispatch(cur, cmd);
264 InsetCollapsable::doDispatch(cur, cmd);
270 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
271 FuncStatus & flag) const
273 switch (cmd.action) {
275 case LFUN_INSET_MODIFY:
276 case LFUN_INSET_DIALOG_UPDATE:
281 return InsetCollapsable::getStatus(cur, cmd, flag);
285 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
287 TextClass const & tclass = buf.params().getTextClass();
288 Counters savecnt = tclass.counters();
289 InsetCollapsable::updateLabels(buf, it);
290 tclass.counters() = savecnt;
294 int InsetNote::latex(Buffer const & buf, odocstream & os,
295 OutputParams const & runparams_in) const
297 if (params_.type == InsetNoteParams::Note)
300 OutputParams runparams(runparams_in);
302 if (params_.type == InsetNoteParams::Comment) {
304 runparams.inComment = true;
305 // Ignore files that are exported inside a comment
306 runparams.exportdata.reset(new ExportData);
307 } else if (params_.type == InsetNoteParams::Greyedout)
308 type = "lyxgreyedout";
309 else if (params_.type == InsetNoteParams::Framed)
311 else if (params_.type == InsetNoteParams::Shaded)
315 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
316 InsetText::latex(buf, ss, runparams);
317 ss << "\n\\end{" << from_ascii(type) << "}\n";
318 // the space after the comment in 'a[comment] b' will be eaten by the
319 // comment environment since the space before b is ignored with the
320 // following latex output:
328 // Adding {} before ' b' fixes this.
329 if (params_.type == InsetNoteParams::Comment)
332 docstring const str = ss.str();
334 runparams_in.encoding = runparams.encoding;
335 // Return how many newlines we issued.
336 return int(std::count(str.begin(), str.end(), '\n'));
340 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
341 OutputParams const & runparams_in) const
343 if (params_.type == InsetNoteParams::Note)
346 OutputParams runparams(runparams_in);
347 if (params_.type == InsetNoteParams::Comment) {
348 runparams.inComment = true;
349 // Ignore files that are exported inside a comment
350 runparams.exportdata.reset(new ExportData);
352 os << '[' << buf.B_("note") << ":\n";
353 InsetText::plaintext(buf, os, runparams);
356 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
360 int InsetNote::docbook(Buffer const & buf, odocstream & os,
361 OutputParams const & runparams_in) const
363 if (params_.type == InsetNoteParams::Note)
366 OutputParams runparams(runparams_in);
367 if (params_.type == InsetNoteParams::Comment) {
369 runparams.inComment = true;
370 // Ignore files that are exported inside a comment
371 runparams.exportdata.reset(new ExportData);
374 int const n = InsetText::docbook(buf, os, runparams);
376 if (params_.type == InsetNoteParams::Comment)
377 os << "\n</remark>\n";
379 // Return how many newlines we issued.
380 //return int(count(str.begin(), str.end(), '\n'));
385 void InsetNote::validate(LaTeXFeatures & features) const
387 if (params_.type == InsetNoteParams::Comment)
388 features.require("verbatim");
389 if (params_.type == InsetNoteParams::Greyedout) {
390 features.require("color");
391 features.require("lyxgreyedout");
393 if (params_.type == InsetNoteParams::Shaded) {
394 features.require("color");
395 features.require("framed");
397 if (params_.type == InsetNoteParams::Framed)
398 features.require("framed");
399 InsetText::validate(features);
404 string const InsetNoteMailer::name_("note");
406 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
411 string const InsetNoteMailer::inset2string(Buffer const &) const
413 return params2string(inset_.params());
417 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
420 data << name_ << ' ';
426 void InsetNoteMailer::string2params(string const & in,
427 InsetNoteParams & params)
429 params = InsetNoteParams();
434 istringstream data(in);
440 if (!lex || name != name_)
441 return print_mailer_error("InsetNoteMailer", in, 1, name_);
443 // This is part of the inset proper that is usually swallowed
444 // by Text::readInset
447 if (!lex || id != "Note")
448 return print_mailer_error("InsetBoxMailer", in, 2, "Note");