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 "InsetIterator.h"
28 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "ParIterator.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/Translator.h"
42 #include "frontends/Application.h"
53 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
54 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
56 NoteTranslator const init_notetranslator()
58 NoteTranslator translator("Note", InsetNoteParams::Note);
59 translator.addPair("Comment", InsetNoteParams::Comment);
60 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
65 NoteTranslatorLoc const init_notetranslator_loc()
67 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
68 translator.addPair(_("Comment"), InsetNoteParams::Comment);
69 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
74 NoteTranslator const & notetranslator()
76 static NoteTranslator translator = init_notetranslator();
81 NoteTranslatorLoc const & notetranslator_loc()
83 static NoteTranslatorLoc translator = init_notetranslator_loc();
92 InsetNoteParams::InsetNoteParams()
97 void InsetNoteParams::write(ostream & os) const
99 string const label = notetranslator().find(type);
100 os << "Note " << label << "\n";
104 void InsetNoteParams::read(Lexer & lex)
109 type = notetranslator().find(label);
113 /////////////////////////////////////////////////////////////////////
117 /////////////////////////////////////////////////////////////////////
119 InsetNote::InsetNote(Buffer * buf, string const & label)
120 : InsetCollapsable(buf)
122 params_.type = notetranslator().find(label);
126 InsetNote::~InsetNote()
128 hideDialogs("note", this);
132 docstring InsetNote::name() const
134 return from_ascii("Note:" + notetranslator().find(params_.type));
138 Inset::DisplayType InsetNote::display() const
144 void InsetNote::write(ostream & os) const
147 InsetCollapsable::write(os);
151 void InsetNote::read(Lexer & lex)
154 InsetCollapsable::read(lex);
158 void InsetNote::setButtonLabel()
160 docstring const label = notetranslator_loc().find(params_.type);
165 bool InsetNote::showInsetDialog(BufferView * bv) const
167 bv->showDialog("note", params2string(params()),
168 const_cast<InsetNote *>(this));
173 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY:
178 string2params(to_utf8(cmd.argument()), params_);
182 case LFUN_INSET_DIALOG_UPDATE:
183 cur.bv().updateDialog("note", params2string(params()));
187 InsetCollapsable::doDispatch(cur, cmd);
193 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
194 FuncStatus & flag) const
196 switch (cmd.action) {
198 case LFUN_INSET_MODIFY:
199 // disallow comment and greyed out in commands
200 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
201 cmd.getArg(2) == "Note");
202 if (cmd.getArg(0) == "note") {
203 InsetNoteParams params;
204 string2params(to_utf8(cmd.argument()), params);
205 flag.setOnOff(params_.type == params.type);
209 case LFUN_INSET_DIALOG_UPDATE:
210 flag.setEnabled(true);
214 return InsetCollapsable::getStatus(cur, cmd, flag);
219 void InsetNote::addToToc(DocIterator const & cpit)
221 DocIterator pit = cpit;
222 pit.push_back(CursorSlice(*this));
224 Toc & toc = buffer().tocBackend().toc("note");
226 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
228 toc.push_back(TocItem(pit, 0, str));
229 // Proceed with the rest of the inset.
230 InsetCollapsable::addToToc(cpit);
234 bool InsetNote::isMacroScope() const
236 // LyX note has no latex output
237 if (params_.type == InsetNoteParams::Note)
240 return InsetCollapsable::isMacroScope();
244 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
246 if (params_.type == InsetNoteParams::Note)
249 OutputParams runparams(runparams_in);
250 if (params_.type == InsetNoteParams::Comment) {
251 runparams.inComment = true;
252 // Ignore files that are exported inside a comment
253 runparams.exportdata.reset(new ExportData);
257 InsetCollapsable::latex(ss, runparams);
258 // the space after the comment in 'a[comment] b' will be eaten by the
259 // comment environment since the space before b is ignored with the
260 // following latex output:
268 // Adding {} before ' b' fixes this.
269 if (params_.type == InsetNoteParams::Comment)
272 docstring const str = ss.str();
274 runparams_in.encoding = runparams.encoding;
275 // Return how many newlines we issued.
276 return int(count(str.begin(), str.end(), '\n'));
280 int InsetNote::plaintext(odocstream & os,
281 OutputParams const & runparams_in) const
283 if (params_.type == InsetNoteParams::Note)
286 OutputParams runparams(runparams_in);
287 if (params_.type == InsetNoteParams::Comment) {
288 runparams.inComment = true;
289 // Ignore files that are exported inside a comment
290 runparams.exportdata.reset(new ExportData);
292 os << '[' << buffer().B_("note") << ":\n";
293 InsetText::plaintext(os, runparams);
296 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
300 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
302 if (params_.type == InsetNoteParams::Note)
305 OutputParams runparams(runparams_in);
306 if (params_.type == InsetNoteParams::Comment) {
308 runparams.inComment = true;
309 // Ignore files that are exported inside a comment
310 runparams.exportdata.reset(new ExportData);
313 int const n = InsetText::docbook(os, runparams);
315 if (params_.type == InsetNoteParams::Comment)
316 os << "\n</remark>\n";
318 // Return how many newlines we issued.
319 //return int(count(str.begin(), str.end(), '\n'));
324 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
326 if (params_.type == InsetNoteParams::Note)
329 return InsetCollapsable::xhtml(xs, rp);
333 void InsetNote::validate(LaTeXFeatures & features) const
335 switch (params_.type) {
336 case InsetNoteParams::Comment:
337 features.require("verbatim");
339 case InsetNoteParams::Greyedout:
340 features.require("color");
341 features.require("lyxgreyedout");
342 InsetCollapsable::validate(features);
344 case InsetNoteParams::Note:
350 docstring InsetNote::contextMenu(BufferView const &, int, int) const
352 return from_ascii("context-note");
355 bool InsetNote::allowSpellCheck() const
357 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
361 string InsetNote::params2string(InsetNoteParams const & params)
364 data << "note" << ' ';
370 void InsetNote::string2params(string const & in, InsetNoteParams & params)
372 params = InsetNoteParams();
377 istringstream data(in);
380 lex.setContext("InsetNote::string2params");
382 // There are cases, such as when we are called via getStatus() from
383 // Dialog::canApply(), where we are just called with "note" rather
384 // than a full "note Note TYPE".