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"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/Translator.h"
49 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
50 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
52 NoteTranslator const init_notetranslator()
54 NoteTranslator translator("Note", InsetNoteParams::Note);
55 translator.addPair("Comment", InsetNoteParams::Comment);
56 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
61 NoteTranslatorLoc const init_notetranslator_loc()
63 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
64 translator.addPair(_("Comment"), InsetNoteParams::Comment);
65 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
70 NoteTranslator const & notetranslator()
72 static NoteTranslator translator = init_notetranslator();
77 NoteTranslatorLoc const & notetranslator_loc()
79 static NoteTranslatorLoc translator = init_notetranslator_loc();
88 InsetNoteParams::InsetNoteParams()
93 void InsetNoteParams::write(ostream & os) const
95 string const label = notetranslator().find(type);
96 os << "Note " << label << "\n";
100 void InsetNoteParams::read(Lexer & lex)
105 type = notetranslator().find(label);
109 /////////////////////////////////////////////////////////////////////
113 /////////////////////////////////////////////////////////////////////
115 InsetNote::InsetNote(Buffer const & buf, string const & label)
116 : InsetCollapsable(buf)
118 params_.type = notetranslator().find(label);
122 InsetNote::~InsetNote()
124 InsetNoteMailer(*this).hideDialog();
128 docstring InsetNote::editMessage() const
130 return _("Opened Note Inset");
134 docstring InsetNote::name() const
136 return from_ascii("Note:" + notetranslator().find(params_.type));
140 Inset::DisplayType InsetNote::display() const
146 void InsetNote::write(ostream & os) const
149 InsetCollapsable::write(os);
153 void InsetNote::read(Lexer & lex)
156 InsetCollapsable::read(lex);
160 void InsetNote::setButtonLabel()
162 docstring const label = notetranslator_loc().find(params_.type);
167 bool InsetNote::showInsetDialog(BufferView * bv) const
169 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
174 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
176 switch (cmd.action) {
178 case LFUN_INSET_MODIFY:
179 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
180 // get a bp from cur:
181 setLayout(cur.buffer().params());
184 case LFUN_INSET_DIALOG_UPDATE:
185 InsetNoteMailer(*this).updateDialog(&cur.bv());
188 InsetCollapsable::doDispatch(cur, cmd);
194 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
195 FuncStatus & flag) const
197 switch (cmd.action) {
199 case LFUN_INSET_MODIFY:
200 // disallow comment and greyed out in commands
201 flag.enabled(!cur.paragraph().layout().isCommand() ||
202 cmd.getArg(2) == "Note");
203 if (cmd.getArg(0) == "note") {
204 InsetNoteParams params;
205 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params);
206 flag.setOnOff(params_.type == params.type);
210 case LFUN_INSET_DIALOG_UPDATE:
215 return InsetCollapsable::getStatus(cur, cmd, flag);
220 void InsetNote::updateLabels(ParIterator const & it)
222 DocumentClass const & tclass = buffer().params().documentClass();
223 Counters savecnt = tclass.counters();
224 InsetCollapsable::updateLabels(it);
225 tclass.counters() = savecnt;
229 void InsetNote::addToToc(ParConstIterator const & cpit) const
231 ParConstIterator pit = cpit;
232 pit.push_back(*this);
234 Toc & toc = buffer().tocBackend().toc("note");
236 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
238 toc.push_back(TocItem(pit, 0, str));
242 bool InsetNote::isMacroScope() const
244 // LyX note has no latex output
245 if (params_.type == InsetNoteParams::Note)
248 return InsetCollapsable::isMacroScope();
252 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
254 if (params_.type == InsetNoteParams::Note)
257 OutputParams runparams(runparams_in);
258 if (params_.type == InsetNoteParams::Comment) {
259 runparams.inComment = true;
260 // Ignore files that are exported inside a comment
261 runparams.exportdata.reset(new ExportData);
265 InsetCollapsable::latex(ss, runparams);
266 // the space after the comment in 'a[comment] b' will be eaten by the
267 // comment environment since the space before b is ignored with the
268 // following latex output:
276 // Adding {} before ' b' fixes this.
277 if (params_.type == InsetNoteParams::Comment)
280 docstring const str = ss.str();
282 runparams_in.encoding = runparams.encoding;
283 // Return how many newlines we issued.
284 return int(count(str.begin(), str.end(), '\n'));
288 int InsetNote::plaintext(odocstream & os,
289 OutputParams const & runparams_in) const
291 if (params_.type == InsetNoteParams::Note)
294 OutputParams runparams(runparams_in);
295 if (params_.type == InsetNoteParams::Comment) {
296 runparams.inComment = true;
297 // Ignore files that are exported inside a comment
298 runparams.exportdata.reset(new ExportData);
300 os << '[' << buffer().B_("note") << ":\n";
301 InsetText::plaintext(os, runparams);
304 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
308 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
310 if (params_.type == InsetNoteParams::Note)
313 OutputParams runparams(runparams_in);
314 if (params_.type == InsetNoteParams::Comment) {
316 runparams.inComment = true;
317 // Ignore files that are exported inside a comment
318 runparams.exportdata.reset(new ExportData);
321 int const n = InsetText::docbook(os, runparams);
323 if (params_.type == InsetNoteParams::Comment)
324 os << "\n</remark>\n";
326 // Return how many newlines we issued.
327 //return int(count(str.begin(), str.end(), '\n'));
332 void InsetNote::validate(LaTeXFeatures & features) const
334 if (params_.type == InsetNoteParams::Comment)
335 features.require("verbatim");
336 if (params_.type == InsetNoteParams::Greyedout) {
337 features.require("color");
338 features.require("lyxgreyedout");
340 InsetText::validate(features);
344 docstring InsetNote::contextMenu(BufferView const &, int, int) const
346 return from_ascii("context-note");
350 string const InsetNoteMailer::name_("note");
352 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
357 string const InsetNoteMailer::inset2string(Buffer const &) const
359 return params2string(inset_.params());
363 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
366 data << name_ << ' ';
372 void InsetNoteMailer::string2params(string const & in,
373 InsetNoteParams & params)
375 params = InsetNoteParams();
380 istringstream data(in);
386 if (!lex || name != name_)
387 return print_mailer_error("InsetNoteMailer", in, 1, name_);
389 // This is part of the inset proper that is usually swallowed
390 // by Text::readInset
393 if (!lex || id != "Note")
394 return print_mailer_error("InsetNoteMailer", in, 2, "Note");