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 "BufferView.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
33 #include "support/lyxalgo.h"
34 #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);
286 int InsetNote::latex(Buffer const & buf, odocstream & os,
287 OutputParams const & runparams_in) const
289 if (params_.type == InsetNoteParams::Note)
292 OutputParams runparams(runparams_in);
294 if (params_.type == InsetNoteParams::Comment) {
296 runparams.inComment = true;
297 // Ignore files that are exported inside a comment
298 runparams.exportdata.reset(new ExportData);
299 } else if (params_.type == InsetNoteParams::Greyedout)
300 type = "lyxgreyedout";
301 else if (params_.type == InsetNoteParams::Framed)
303 else if (params_.type == InsetNoteParams::Shaded)
307 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
308 InsetText::latex(buf, ss, runparams);
309 ss << "\n\\end{" << from_ascii(type) << "}\n";
310 // the space after the comment in 'a[comment] b' will be eaten by the
311 // comment environment since the space before b is ignored with the
312 // following latex output:
320 // Adding {} before ' b' fixes this.
321 if (params_.type == InsetNoteParams::Comment)
324 docstring const str = ss.str();
326 runparams_in.encoding = runparams.encoding;
327 // Return how many newlines we issued.
328 return int(lyx::count(str.begin(), str.end(), '\n'));
332 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
333 OutputParams const & runparams_in) const
335 if (params_.type == InsetNoteParams::Note)
338 OutputParams runparams(runparams_in);
339 if (params_.type == InsetNoteParams::Comment) {
340 runparams.inComment = true;
341 // Ignore files that are exported inside a comment
342 runparams.exportdata.reset(new ExportData);
344 os << '[' << buf.B_("note") << ":\n";
345 InsetText::plaintext(buf, os, runparams);
348 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
352 int InsetNote::docbook(Buffer const & buf, odocstream & os,
353 OutputParams const & runparams_in) const
355 if (params_.type == InsetNoteParams::Note)
358 OutputParams runparams(runparams_in);
359 if (params_.type == InsetNoteParams::Comment) {
361 runparams.inComment = true;
362 // Ignore files that are exported inside a comment
363 runparams.exportdata.reset(new ExportData);
366 int const n = InsetText::docbook(buf, os, runparams);
368 if (params_.type == InsetNoteParams::Comment)
369 os << "\n</remark>\n";
371 // Return how many newlines we issued.
372 //return int(count(str.begin(), str.end(), '\n'));
377 void InsetNote::validate(LaTeXFeatures & features) const
379 if (params_.type == InsetNoteParams::Comment)
380 features.require("verbatim");
381 if (params_.type == InsetNoteParams::Greyedout) {
382 features.require("color");
383 features.require("lyxgreyedout");
385 if (params_.type == InsetNoteParams::Shaded) {
386 features.require("color");
387 features.require("framed");
389 if (params_.type == InsetNoteParams::Framed)
390 features.require("framed");
391 InsetText::validate(features);
396 string const InsetNoteMailer::name_("note");
398 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
403 string const InsetNoteMailer::inset2string(Buffer const &) const
405 return params2string(inset_.params());
409 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
412 data << name_ << ' ';
418 void InsetNoteMailer::string2params(string const & in,
419 InsetNoteParams & params)
421 params = InsetNoteParams();
426 istringstream data(in);
432 if (!lex || name != name_)
433 return print_mailer_error("InsetNoteMailer", in, 1, name_);
435 // This is part of the inset proper that is usually swallowed
436 // by Text::readInset
439 if (!lex || id != "Note")
440 return print_mailer_error("InsetBoxMailer", in, 2, "Note");