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"
28 #include "MetricsInfo.h"
29 #include "OutputParams.h"
31 #include "support/lyxalgo.h"
32 #include "support/Translator.h"
41 using std::istringstream;
43 using std::ostringstream;
48 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
49 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
51 NoteTranslator const init_notetranslator()
53 NoteTranslator translator("Note", InsetNoteParams::Note);
54 translator.addPair("Comment", InsetNoteParams::Comment);
55 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
56 translator.addPair("Framed", InsetNoteParams::Framed);
57 translator.addPair("Shaded", InsetNoteParams::Shaded);
62 NoteTranslatorLoc const init_notetranslator_loc()
64 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
65 translator.addPair(_("Comment"), InsetNoteParams::Comment);
66 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
67 translator.addPair(_("Framed"), InsetNoteParams::Framed);
68 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
73 NoteTranslator const & notetranslator()
75 static NoteTranslator translator = init_notetranslator();
80 NoteTranslatorLoc const & notetranslator_loc()
82 static NoteTranslatorLoc 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(Lexer & lex)
108 type = notetranslator().find(label);
112 void InsetNote::init()
118 InsetNote::InsetNote(BufferParams const & bp, string const & label)
119 : InsetCollapsable(bp)
121 params_.type = notetranslator().find(label);
126 InsetNote::InsetNote(InsetNote const & in)
127 : InsetCollapsable(in), params_(in.params_)
133 InsetNote::~InsetNote()
135 InsetNoteMailer(*this).hideDialog();
139 auto_ptr<Inset> InsetNote::doClone() const
141 return auto_ptr<Inset>(new InsetNote(*this));
145 docstring const InsetNote::editMessage() const
147 return _("Opened Note Inset");
151 Inset::DisplayType InsetNote::display() const
153 switch (params_.type) {
154 case InsetNoteParams::Framed:
155 case InsetNoteParams::Shaded:
163 void InsetNote::write(Buffer const & buf, ostream & os) const
166 InsetCollapsable::write(buf, os);
170 void InsetNote::read(Buffer const & buf, Lexer & lex)
173 InsetCollapsable::read(buf, lex);
178 void InsetNote::setButtonLabel()
180 docstring const label = notetranslator_loc().find(params_.type);
183 Font font(Font::ALL_SANE);
188 switch (params_.type) {
189 case InsetNoteParams::Note:
192 case InsetNoteParams::Comment:
195 case InsetNoteParams::Greyedout:
196 c = Color::greyedout;
198 case InsetNoteParams::Framed:
199 c = Color::greyedout;
201 case InsetNoteParams::Shaded:
202 c = Color::greyedout;
210 Color_color InsetNote::backgroundColor() const
213 switch (params_.type) {
214 case InsetNoteParams::Note:
217 case InsetNoteParams::Comment:
218 c = Color::commentbg;
220 case InsetNoteParams::Greyedout:
221 c = Color::greyedoutbg;
223 case InsetNoteParams::Framed:
224 c = Color::greyedoutbg;
226 case InsetNoteParams::Shaded:
234 bool InsetNote::showInsetDialog(BufferView * bv) const
236 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
241 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
243 switch (cmd.action) {
245 case LFUN_INSET_MODIFY:
246 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
250 case LFUN_INSET_DIALOG_UPDATE:
251 InsetNoteMailer(*this).updateDialog(&cur.bv());
254 case LFUN_MOUSE_RELEASE:
255 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
256 InsetNoteMailer(*this).showDialog(&cur.bv());
258 InsetCollapsable::doDispatch(cur, cmd);
262 InsetCollapsable::doDispatch(cur, cmd);
268 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
269 FuncStatus & flag) const
271 switch (cmd.action) {
273 case LFUN_INSET_MODIFY:
274 case LFUN_INSET_DIALOG_UPDATE:
279 return InsetCollapsable::getStatus(cur, cmd, flag);
284 int InsetNote::latex(Buffer const & buf, odocstream & os,
285 OutputParams const & runparams_in) const
287 if (params_.type == InsetNoteParams::Note)
290 OutputParams runparams(runparams_in);
292 if (params_.type == InsetNoteParams::Comment) {
294 runparams.inComment = true;
295 // Ignore files that are exported inside a comment
296 runparams.exportdata.reset(new ExportData);
297 } else if (params_.type == InsetNoteParams::Greyedout)
298 type = "lyxgreyedout";
299 else if (params_.type == InsetNoteParams::Framed)
301 else if (params_.type == InsetNoteParams::Shaded)
305 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
306 InsetText::latex(buf, ss, runparams);
307 ss << "\n\\end{" << from_ascii(type) << "}\n";
308 // the space after the comment in 'a[comment] b' will be eaten by the
309 // comment environment since the space before b is ignored with the
310 // following latex output:
318 // Adding {} before ' b' fixes this.
319 if (params_.type == InsetNoteParams::Comment)
322 docstring const str = ss.str();
324 runparams_in.encoding = runparams.encoding;
325 // Return how many newlines we issued.
326 return int(lyx::count(str.begin(), str.end(), '\n'));
330 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
331 OutputParams const & runparams_in) const
333 if (params_.type == InsetNoteParams::Note)
336 OutputParams runparams(runparams_in);
337 if (params_.type == InsetNoteParams::Comment) {
338 runparams.inComment = true;
339 // Ignore files that are exported inside a comment
340 runparams.exportdata.reset(new ExportData);
342 os << '[' << buf.B_("note") << ":\n";
343 InsetText::plaintext(buf, os, runparams);
346 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
350 int InsetNote::docbook(Buffer const & buf, odocstream & os,
351 OutputParams const & runparams_in) const
353 if (params_.type == InsetNoteParams::Note)
356 OutputParams runparams(runparams_in);
357 if (params_.type == InsetNoteParams::Comment) {
359 runparams.inComment = true;
360 // Ignore files that are exported inside a comment
361 runparams.exportdata.reset(new ExportData);
364 int const n = InsetText::docbook(buf, os, runparams);
366 if (params_.type == InsetNoteParams::Comment)
367 os << "\n</remark>\n";
369 // Return how many newlines we issued.
370 //return int(count(str.begin(), str.end(), '\n'));
375 void InsetNote::validate(LaTeXFeatures & features) const
377 if (params_.type == InsetNoteParams::Comment)
378 features.require("verbatim");
379 if (params_.type == InsetNoteParams::Greyedout) {
380 features.require("color");
381 features.require("lyxgreyedout");
383 if (params_.type == InsetNoteParams::Shaded) {
384 features.require("color");
385 features.require("framed");
387 if (params_.type == InsetNoteParams::Framed)
388 features.require("framed");
389 InsetText::validate(features);
394 string const InsetNoteMailer::name_("note");
396 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
401 string const InsetNoteMailer::inset2string(Buffer const &) const
403 return params2string(inset_.params());
407 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
410 data << name_ << ' ';
416 void InsetNoteMailer::string2params(string const & in,
417 InsetNoteParams & params)
419 params = InsetNoteParams();
424 istringstream data(in);
430 if (!lex || name != name_)
431 return print_mailer_error("InsetNoteMailer", in, 1, name_);
433 // This is part of the inset proper that is usually swallowed
434 // by Text::readInset
437 if (!lex || id != "Note")
438 return print_mailer_error("InsetBoxMailer", in, 2, "Note");