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;
54 NoteTranslator const init_notetranslator()
56 NoteTranslator translator("Note", InsetNoteParams::Note);
57 translator.addPair("Comment", InsetNoteParams::Comment);
58 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
63 NoteTranslator const & notetranslator()
65 static NoteTranslator translator = init_notetranslator();
73 InsetNoteParams::InsetNoteParams()
78 void InsetNoteParams::write(ostream & os) const
80 string const label = notetranslator().find(type);
81 os << "Note " << label << "\n";
85 void InsetNoteParams::read(Lexer & lex)
90 type = notetranslator().find(label);
94 /////////////////////////////////////////////////////////////////////
98 /////////////////////////////////////////////////////////////////////
100 InsetNote::InsetNote(Buffer * buf, string const & label)
101 : InsetCollapsable(buf)
103 params_.type = notetranslator().find(label);
107 InsetNote::~InsetNote()
109 hideDialogs("note", this);
113 docstring InsetNote::layoutName() const
115 return from_ascii("Note:" + notetranslator().find(params_.type));
119 Inset::DisplayType InsetNote::display() const
125 void InsetNote::write(ostream & os) const
128 InsetCollapsable::write(os);
132 void InsetNote::read(Lexer & lex)
135 InsetCollapsable::read(lex);
139 bool InsetNote::showInsetDialog(BufferView * bv) const
141 bv->showDialog("note", params2string(params()),
142 const_cast<InsetNote *>(this));
147 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
149 switch (cmd.action()) {
151 case LFUN_INSET_MODIFY:
152 cur.recordUndoInset(ATOMIC_UNDO, this);
153 string2params(to_utf8(cmd.argument()), params_);
155 // what we really want here is a TOC update, but that means
156 // a full buffer update
157 cur.forceBufferUpdate();
160 case LFUN_INSET_DIALOG_UPDATE:
161 cur.bv().updateDialog("note", params2string(params()));
165 InsetCollapsable::doDispatch(cur, cmd);
171 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
172 FuncStatus & flag) const
174 switch (cmd.action()) {
176 case LFUN_INSET_MODIFY:
177 // disallow comment and greyed out in commands
178 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
179 cmd.getArg(2) == "Note");
180 if (cmd.getArg(0) == "note") {
181 InsetNoteParams params;
182 string2params(to_utf8(cmd.argument()), params);
183 flag.setOnOff(params_.type == params.type);
187 case LFUN_INSET_DIALOG_UPDATE:
188 flag.setEnabled(true);
192 return InsetCollapsable::getStatus(cur, cmd, flag);
197 void InsetNote::addToToc(DocIterator const & cpit) const
199 DocIterator pit = cpit;
200 pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
202 Toc & toc = buffer().tocBackend().toc("note");
203 InsetLayout const & il = getLayout();
204 docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
205 text().forToc(str, TOC_ENTRY_LENGTH);
206 toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
207 // Proceed with the rest of the inset.
208 InsetCollapsable::addToToc(cpit);
212 bool InsetNote::isMacroScope() const
214 // LyX note has no latex output
215 if (params_.type == InsetNoteParams::Note)
218 return InsetCollapsable::isMacroScope();
222 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
224 if (params_.type == InsetNoteParams::Note)
227 OutputParams runparams(runparams_in);
228 if (params_.type == InsetNoteParams::Comment) {
229 runparams.inComment = true;
230 // Ignore files that are exported inside a comment
231 runparams.exportdata.reset(new ExportData);
234 // the space after the comment in 'a[comment] b' will be eaten by the
235 // comment environment since the space before b is ignored with the
236 // following latex output:
244 // Adding {} before ' b' fixes this.
245 // The {} will be automatically added, but only if needed, for all
246 // insets whose InsetLayout Display tag is false. This is achieved
247 // by telling otexstream to protect an immediately following space
248 // and is done for both comment and greyedout insets.
249 InsetCollapsable::latex(os, runparams);
251 runparams_in.encoding = runparams.encoding;
255 int InsetNote::plaintext(odocstream & os,
256 OutputParams const & runparams_in) const
258 if (params_.type == InsetNoteParams::Note)
261 OutputParams runparams(runparams_in);
262 if (params_.type == InsetNoteParams::Comment) {
263 runparams.inComment = true;
264 // Ignore files that are exported inside a comment
265 runparams.exportdata.reset(new ExportData);
267 os << '[' << buffer().B_("note") << ":\n";
268 InsetText::plaintext(os, runparams);
271 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
275 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
277 if (params_.type == InsetNoteParams::Note)
280 OutputParams runparams(runparams_in);
281 if (params_.type == InsetNoteParams::Comment) {
283 runparams.inComment = true;
284 // Ignore files that are exported inside a comment
285 runparams.exportdata.reset(new ExportData);
288 int const n = InsetText::docbook(os, runparams);
290 if (params_.type == InsetNoteParams::Comment)
291 os << "\n</remark>\n";
293 // Return how many newlines we issued.
294 //return int(count(str.begin(), str.end(), '\n'));
299 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
301 if (params_.type == InsetNoteParams::Note)
304 return InsetCollapsable::xhtml(xs, rp);
308 void InsetNote::validate(LaTeXFeatures & features) const
310 switch (params_.type) {
311 case InsetNoteParams::Comment:
312 features.require("verbatim");
314 case InsetNoteParams::Greyedout:
315 features.require("color");
316 features.require("lyxgreyedout");
317 InsetCollapsable::validate(features);
319 case InsetNoteParams::Note:
325 docstring InsetNote::contextMenuName() const
327 return from_ascii("context-note");
330 bool InsetNote::allowSpellCheck() const
332 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
336 string InsetNote::params2string(InsetNoteParams const & params)
339 data << "note" << ' ';
345 void InsetNote::string2params(string const & in, InsetNoteParams & params)
347 params = InsetNoteParams();
352 istringstream data(in);
355 lex.setContext("InsetNote::string2params");
357 // There are cases, such as when we are called via getStatus() from
358 // Dialog::canApply(), where we are just called with "note" rather
359 // than a full "note Note TYPE".