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"
20 #include "BufferParams.h"
24 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.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 InsetNote::InsetNote(BufferParams const & bp, string const & label)
115 : InsetCollapsable(bp)
117 params_.type = notetranslator().find(label);
123 InsetNote::InsetNote(InsetNote const & in)
124 : InsetCollapsable(in), params_(in.params_)
130 InsetNote::~InsetNote()
132 InsetNoteMailer(*this).hideDialog();
136 Inset * InsetNote::clone() const
138 return new InsetNote(*this);
142 docstring const InsetNote::editMessage() const
144 return _("Opened Note Inset");
148 docstring InsetNote::name() const
150 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
154 Inset::DisplayType InsetNote::display() const
156 switch (params_.type) {
157 case InsetNoteParams::Framed:
158 case InsetNoteParams::Shaded:
166 void InsetNote::write(Buffer const & buf, ostream & os) const
169 InsetCollapsable::write(buf, os);
173 void InsetNote::read(Buffer const & buf, Lexer & lex)
176 InsetCollapsable::read(buf, lex);
177 setLayout(buf.params());
182 void InsetNote::setButtonLabel()
184 docstring const label = notetranslator_loc().find(params_.type);
189 bool InsetNote::showInsetDialog(BufferView * bv) const
191 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
196 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
198 switch (cmd.action) {
200 case LFUN_INSET_MODIFY:
201 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
202 // get a bp from cur:
203 setLayout(cur.buffer().params());
207 case LFUN_INSET_DIALOG_UPDATE:
208 InsetNoteMailer(*this).updateDialog(&cur.bv());
211 InsetCollapsable::doDispatch(cur, cmd);
217 bool InsetNote::getStatus(Cursor & 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);
232 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
234 TextClass const & tclass = buf.params().getTextClass();
235 Counters savecnt = tclass.counters();
236 InsetCollapsable::updateLabels(buf, it);
237 tclass.counters() = savecnt;
241 int InsetNote::latex(Buffer const & buf, odocstream & os,
242 OutputParams const & runparams_in) const
244 if (params_.type == InsetNoteParams::Note)
247 OutputParams runparams(runparams_in);
248 if (params_.type == InsetNoteParams::Comment) {
249 runparams.inComment = true;
250 // Ignore files that are exported inside a comment
251 runparams.exportdata.reset(new ExportData);
255 InsetCollapsable::latex(buf, ss, runparams);
256 // the space after the comment in 'a[comment] b' will be eaten by the
257 // comment environment since the space before b is ignored with the
258 // following latex output:
266 // Adding {} before ' b' fixes this.
267 if (params_.type == InsetNoteParams::Comment)
270 docstring const str = ss.str();
272 runparams_in.encoding = runparams.encoding;
273 // Return how many newlines we issued.
274 return int(std::count(str.begin(), str.end(), '\n'));
278 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
279 OutputParams const & runparams_in) const
281 if (params_.type == InsetNoteParams::Note)
284 OutputParams runparams(runparams_in);
285 if (params_.type == InsetNoteParams::Comment) {
286 runparams.inComment = true;
287 // Ignore files that are exported inside a comment
288 runparams.exportdata.reset(new ExportData);
290 os << '[' << buf.B_("note") << ":\n";
291 InsetText::plaintext(buf, os, runparams);
294 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
298 int InsetNote::docbook(Buffer const & buf, odocstream & os,
299 OutputParams const & runparams_in) const
301 if (params_.type == InsetNoteParams::Note)
304 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 int const n = InsetText::docbook(buf, os, runparams);
314 if (params_.type == InsetNoteParams::Comment)
315 os << "\n</remark>\n";
317 // Return how many newlines we issued.
318 //return int(count(str.begin(), str.end(), '\n'));
323 void InsetNote::validate(LaTeXFeatures & features) const
325 if (params_.type == InsetNoteParams::Comment)
326 features.require("verbatim");
327 if (params_.type == InsetNoteParams::Greyedout) {
328 features.require("color");
329 features.require("lyxgreyedout");
331 if (params_.type == InsetNoteParams::Shaded) {
332 features.require("color");
333 features.require("framed");
335 if (params_.type == InsetNoteParams::Framed)
336 features.require("framed");
337 InsetText::validate(features);
342 string const InsetNoteMailer::name_("note");
344 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
349 string const InsetNoteMailer::inset2string(Buffer const &) const
351 return params2string(inset_.params());
355 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
358 data << name_ << ' ';
364 void InsetNoteMailer::string2params(string const & in,
365 InsetNoteParams & params)
367 params = InsetNoteParams();
372 istringstream data(in);
378 if (!lex || name != name_)
379 return print_mailer_error("InsetNoteMailer", in, 1, name_);
381 // This is part of the inset proper that is usually swallowed
382 // by Text::readInset
385 if (!lex || id != "Note")
386 return print_mailer_error("InsetBoxMailer", in, 2, "Note");