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 "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/gettext.h"
39 #include "support/Translator.h"
41 #include "frontends/Application.h"
52 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
53 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
55 NoteTranslator const init_notetranslator()
57 NoteTranslator translator("Note", InsetNoteParams::Note);
58 translator.addPair("Comment", InsetNoteParams::Comment);
59 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
64 NoteTranslatorLoc const init_notetranslator_loc()
66 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
67 translator.addPair(_("Comment"), InsetNoteParams::Comment);
68 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
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 /////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////
118 InsetNote::InsetNote(Buffer * buf, string const & label)
119 : InsetCollapsable(buf)
121 params_.type = notetranslator().find(label);
125 InsetNote::~InsetNote()
127 hideDialogs("note", this);
131 docstring InsetNote::name() const
133 return from_ascii("Note:" + notetranslator().find(params_.type));
137 Inset::DisplayType InsetNote::display() const
143 void InsetNote::write(ostream & os) const
146 InsetCollapsable::write(os);
150 void InsetNote::read(Lexer & lex)
153 InsetCollapsable::read(lex);
157 bool InsetNote::showInsetDialog(BufferView * bv) const
159 bv->showDialog("note", params2string(params()),
160 const_cast<InsetNote *>(this));
165 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
167 switch (cmd.action()) {
169 case LFUN_INSET_MODIFY:
170 cur.recordUndoInset(ATOMIC_UNDO, this);
171 string2params(to_utf8(cmd.argument()), params_);
173 // what we really want here is a TOC update, but that means
174 // a full buffer update
175 cur.forceBufferUpdate();
178 case LFUN_INSET_DIALOG_UPDATE:
179 cur.bv().updateDialog("note", params2string(params()));
183 InsetCollapsable::doDispatch(cur, cmd);
189 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
190 FuncStatus & flag) const
192 switch (cmd.action()) {
194 case LFUN_INSET_MODIFY:
195 // disallow comment and greyed out in commands
196 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
197 cmd.getArg(2) == "Note");
198 if (cmd.getArg(0) == "note") {
199 InsetNoteParams params;
200 string2params(to_utf8(cmd.argument()), params);
201 flag.setOnOff(params_.type == params.type);
205 case LFUN_INSET_DIALOG_UPDATE:
206 flag.setEnabled(true);
210 return InsetCollapsable::getStatus(cur, cmd, flag);
215 void InsetNote::addToToc(DocIterator const & cpit)
217 DocIterator pit = cpit;
218 pit.push_back(CursorSlice(*this));
220 Toc & toc = buffer().tocBackend().toc("note");
222 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
223 + text().getPar(0).asString();
224 toc.push_back(TocItem(pit, 0, str, toolTipText()));
225 // Proceed with the rest of the inset.
226 InsetCollapsable::addToToc(cpit);
230 bool InsetNote::isMacroScope() const
232 // LyX note has no latex output
233 if (params_.type == InsetNoteParams::Note)
236 return InsetCollapsable::isMacroScope();
240 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
242 if (params_.type == InsetNoteParams::Note)
245 OutputParams runparams(runparams_in);
246 if (params_.type == InsetNoteParams::Comment) {
247 runparams.inComment = true;
248 // Ignore files that are exported inside a comment
249 runparams.exportdata.reset(new ExportData);
253 InsetCollapsable::latex(ss, runparams);
254 // the space after the comment in 'a[comment] b' will be eaten by the
255 // comment environment since the space before b is ignored with the
256 // following latex output:
264 // Adding {} before ' b' fixes this.
265 if (params_.type == InsetNoteParams::Comment)
268 docstring const str = ss.str();
270 runparams_in.encoding = runparams.encoding;
271 // Return how many newlines we issued.
272 return int(count(str.begin(), str.end(), '\n'));
276 int InsetNote::plaintext(odocstream & os,
277 OutputParams const & runparams_in) const
279 if (params_.type == InsetNoteParams::Note)
282 OutputParams runparams(runparams_in);
283 if (params_.type == InsetNoteParams::Comment) {
284 runparams.inComment = true;
285 // Ignore files that are exported inside a comment
286 runparams.exportdata.reset(new ExportData);
288 os << '[' << buffer().B_("note") << ":\n";
289 InsetText::plaintext(os, runparams);
292 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
296 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
298 if (params_.type == InsetNoteParams::Note)
301 OutputParams runparams(runparams_in);
302 if (params_.type == InsetNoteParams::Comment) {
304 runparams.inComment = true;
305 // Ignore files that are exported inside a comment
306 runparams.exportdata.reset(new ExportData);
309 int const n = InsetText::docbook(os, runparams);
311 if (params_.type == InsetNoteParams::Comment)
312 os << "\n</remark>\n";
314 // Return how many newlines we issued.
315 //return int(count(str.begin(), str.end(), '\n'));
320 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
322 if (params_.type == InsetNoteParams::Note)
325 return InsetCollapsable::xhtml(xs, rp);
329 void InsetNote::validate(LaTeXFeatures & features) const
331 switch (params_.type) {
332 case InsetNoteParams::Comment:
333 features.require("verbatim");
335 case InsetNoteParams::Greyedout:
336 features.require("color");
337 features.require("lyxgreyedout");
338 InsetCollapsable::validate(features);
340 case InsetNoteParams::Note:
346 docstring InsetNote::contextMenuName() const
348 return from_ascii("context-note");
351 bool InsetNote::allowSpellCheck() const
353 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
357 string InsetNote::params2string(InsetNoteParams const & params)
360 data << "note" << ' ';
366 void InsetNote::string2params(string const & in, InsetNoteParams & params)
368 params = InsetNoteParams();
373 istringstream data(in);
376 lex.setContext("InsetNote::string2params");
378 // There are cases, such as when we are called via getStatus() from
379 // Dialog::canApply(), where we are just called with "note" rather
380 // than a full "note Note TYPE".