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 NoteTranslator const & notetranslator()
66 static NoteTranslator translator = init_notetranslator();
74 InsetNoteParams::InsetNoteParams()
79 void InsetNoteParams::write(ostream & os) const
81 string const label = notetranslator().find(type);
82 os << "Note " << label << "\n";
86 void InsetNoteParams::read(Lexer & lex)
91 type = notetranslator().find(label);
95 /////////////////////////////////////////////////////////////////////
99 /////////////////////////////////////////////////////////////////////
101 InsetNote::InsetNote(Buffer * buf, string const & label)
102 : InsetCollapsable(buf)
104 params_.type = notetranslator().find(label);
108 InsetNote::~InsetNote()
110 hideDialogs("note", this);
114 docstring InsetNote::layoutName() const
116 return from_ascii("Note:" + notetranslator().find(params_.type));
120 Inset::DisplayType InsetNote::display() const
126 void InsetNote::write(ostream & os) const
129 InsetCollapsable::write(os);
133 void InsetNote::read(Lexer & lex)
136 InsetCollapsable::read(lex);
140 bool InsetNote::showInsetDialog(BufferView * bv) const
142 bv->showDialog("note", params2string(params()),
143 const_cast<InsetNote *>(this));
148 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
150 switch (cmd.action()) {
152 case LFUN_INSET_MODIFY:
153 cur.recordUndoInset(ATOMIC_UNDO, this);
154 string2params(to_utf8(cmd.argument()), params_);
156 // what we really want here is a TOC update, but that means
157 // a full buffer update
158 cur.forceBufferUpdate();
161 case LFUN_INSET_DIALOG_UPDATE:
162 cur.bv().updateDialog("note", params2string(params()));
166 InsetCollapsable::doDispatch(cur, cmd);
172 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
173 FuncStatus & flag) const
175 switch (cmd.action()) {
177 case LFUN_INSET_MODIFY:
178 // disallow comment and greyed out in commands
179 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
180 cmd.getArg(2) == "Note");
181 if (cmd.getArg(0) == "note") {
182 InsetNoteParams params;
183 string2params(to_utf8(cmd.argument()), params);
184 flag.setOnOff(params_.type == params.type);
188 case LFUN_INSET_DIALOG_UPDATE:
189 flag.setEnabled(true);
193 return InsetCollapsable::getStatus(cur, cmd, flag);
198 void InsetNote::addToToc(DocIterator const & cpit) const
200 DocIterator pit = cpit;
201 pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
203 Toc & toc = buffer().tocBackend().toc("note");
204 InsetLayout const & il = getLayout();
205 docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
206 text().forToc(str, TOC_ENTRY_LENGTH);
207 toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
208 // Proceed with the rest of the inset.
209 InsetCollapsable::addToToc(cpit);
213 bool InsetNote::isMacroScope() const
215 // LyX note has no latex output
216 if (params_.type == InsetNoteParams::Note)
219 return InsetCollapsable::isMacroScope();
223 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
225 if (params_.type == InsetNoteParams::Note)
228 OutputParams runparams(runparams_in);
229 if (params_.type == InsetNoteParams::Comment) {
230 runparams.inComment = true;
231 // Ignore files that are exported inside a comment
232 runparams.exportdata.reset(new ExportData);
235 // the space after the comment in 'a[comment] b' will be eaten by the
236 // comment environment since the space before b is ignored with the
237 // following latex output:
245 // Adding {} before ' b' fixes this.
246 // The {} will be automatically added, but only if needed, for all
247 // insets whose InsetLayout Display tag is false. This is achieved
248 // by telling otexstream to protect an immediately following space
249 // and is done for both comment and greyedout insets.
250 InsetCollapsable::latex(os, runparams);
252 runparams_in.encoding = runparams.encoding;
256 int InsetNote::plaintext(odocstream & os,
257 OutputParams const & runparams_in) const
259 if (params_.type == InsetNoteParams::Note)
262 OutputParams runparams(runparams_in);
263 if (params_.type == InsetNoteParams::Comment) {
264 runparams.inComment = true;
265 // Ignore files that are exported inside a comment
266 runparams.exportdata.reset(new ExportData);
268 os << '[' << buffer().B_("note") << ":\n";
269 InsetText::plaintext(os, runparams);
272 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
276 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
278 if (params_.type == InsetNoteParams::Note)
281 OutputParams runparams(runparams_in);
282 if (params_.type == InsetNoteParams::Comment) {
284 runparams.inComment = true;
285 // Ignore files that are exported inside a comment
286 runparams.exportdata.reset(new ExportData);
289 int const n = InsetText::docbook(os, runparams);
291 if (params_.type == InsetNoteParams::Comment)
292 os << "\n</remark>\n";
294 // Return how many newlines we issued.
295 //return int(count(str.begin(), str.end(), '\n'));
300 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
302 if (params_.type == InsetNoteParams::Note)
305 return InsetCollapsable::xhtml(xs, rp);
309 void InsetNote::validate(LaTeXFeatures & features) const
311 switch (params_.type) {
312 case InsetNoteParams::Comment:
313 features.require("verbatim");
315 case InsetNoteParams::Greyedout:
316 features.require("color");
317 features.require("lyxgreyedout");
318 InsetCollapsable::validate(features);
320 case InsetNoteParams::Note:
326 docstring InsetNote::contextMenuName() const
328 return from_ascii("context-note");
331 bool InsetNote::allowSpellCheck() const
333 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
337 string InsetNote::params2string(InsetNoteParams const & params)
340 data << "note" << ' ';
346 void InsetNote::string2params(string const & in, InsetNoteParams & params)
348 params = InsetNoteParams();
353 istringstream data(in);
356 lex.setContext("InsetNote::string2params");
358 // There are cases, such as when we are called via getStatus() from
359 // Dialog::canApply(), where we are just called with "note" rather
360 // than a full "note Note TYPE".