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"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
30 #include "output_docbook.h"
31 #include "output_latex.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/Translator.h"
39 #include "frontends/Application.h"
50 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
52 NoteTranslator const init_notetranslator()
54 NoteTranslator translator("Note", InsetNoteParams::Note);
55 translator.addPair("Comment", InsetNoteParams::Comment);
56 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
61 NoteTranslator const & notetranslator()
63 static NoteTranslator const translator = init_notetranslator();
71 InsetNoteParams::InsetNoteParams()
76 void InsetNoteParams::write(ostream & os) const
78 string const label = notetranslator().find(type);
79 os << "Note " << label << "\n";
83 void InsetNoteParams::read(Lexer & lex)
88 type = notetranslator().find(label);
92 /////////////////////////////////////////////////////////////////////
96 /////////////////////////////////////////////////////////////////////
98 InsetNote::InsetNote(Buffer * buf, string const & label)
99 : InsetCollapsible(buf)
101 params_.type = notetranslator().find(label);
105 InsetNote::~InsetNote()
107 hideDialogs("note", this);
111 docstring InsetNote::layoutName() const
113 return from_ascii("Note:" + notetranslator().find(params_.type));
117 void InsetNote::write(ostream & os) const
120 InsetCollapsible::write(os);
124 void InsetNote::read(Lexer & lex)
127 InsetCollapsible::read(lex);
131 bool InsetNote::showInsetDialog(BufferView * bv) const
133 bv->showDialog("note", params2string(params()),
134 const_cast<InsetNote *>(this));
139 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 // Do not do anything if converting to the same type of Note.
145 // A quick break here is done instead of disabling the LFUN
146 // because disabling the LFUN would lead to a greyed out
147 // entry, which might confuse users.
148 // In the future, we might want to have a radio button for
149 // switching between notes.
150 InsetNoteParams params;
151 string2params(to_utf8(cmd.argument()), params);
152 if (params_.type == params.type)
155 cur.recordUndoInset(this);
156 string2params(to_utf8(cmd.argument()), params_);
158 // what we really want here is a TOC update, but that means
159 // a full buffer update
160 cur.forceBufferUpdate();
164 case LFUN_INSET_DIALOG_UPDATE:
165 cur.bv().updateDialog("note", params2string(params()));
169 InsetCollapsible::doDispatch(cur, cmd);
175 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
176 FuncStatus & flag) const
178 switch (cmd.action()) {
180 case LFUN_INSET_MODIFY:
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 InsetCollapsible::getStatus(cur, cmd, flag);
198 bool InsetNote::isMacroScope() const
200 // LyX note has no latex output
201 if (params_.type == InsetNoteParams::Note)
204 return InsetCollapsible::isMacroScope();
208 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
210 if (params_.type != InsetNoteParams::Greyedout
211 && runparams_in.find_effective()
212 && !runparams_in.find_with_non_output())
215 if (params_.type == InsetNoteParams::Note) {
216 if (runparams_in.find_with_non_output()) {
217 OutputParams runparams(runparams_in);
218 InsetCollapsible::latex(os, runparams);
219 runparams_in.encoding = runparams.encoding;
224 OutputParams runparams(runparams_in);
225 if (params_.type == InsetNoteParams::Comment) {
226 if (runparams_in.inComment) {
227 // Nested comments should just output the contents.
228 latexParagraphs(buffer(), text(), os, runparams);
232 runparams.inComment = true;
233 // Ignore files that are exported inside a comment
234 runparams.exportdata.reset(new ExportData);
237 // the space after the comment in 'a[comment] b' will be eaten by the
238 // comment environment since the space before b is ignored with the
239 // following latex output:
247 // Adding {} before ' b' fixes this.
248 // The {} will be automatically added, but only if needed, for all
249 // insets whose InsetLayout Display tag is false. This is achieved
250 // by telling otexstream to protect an immediately following space
251 // and is done for both comment and greyedout insets.
252 InsetCollapsible::latex(os, runparams);
254 runparams_in.encoding = runparams.encoding;
258 int InsetNote::plaintext(odocstringstream & os,
259 OutputParams const & runparams_in, size_t max_length) const
261 if (!runparams_in.find_with_non_output()) {
262 if (params_.type == InsetNoteParams::Note)
264 else if (params_.type == InsetNoteParams::Comment
265 && runparams_in.find_effective())
269 OutputParams runparams(runparams_in);
270 if (params_.type != InsetNoteParams::Greyedout) {
271 runparams.inComment = true;
272 // Ignore files that are exported inside a comment
273 runparams.exportdata.reset(new ExportData);
275 if (!runparams_in.find_with_non_output())
276 os << '[' << buffer().B_("note") << ":\n";
277 InsetText::plaintext(os, runparams, max_length);
278 if (!runparams_in.find_with_non_output())
281 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
285 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
287 if (params_.type == InsetNoteParams::Note)
290 OutputParams runparams(runparams_in);
291 if (params_.type == InsetNoteParams::Comment) {
292 xs << xml::StartTag("remark");
294 runparams.inComment = true;
295 // Ignore files that are exported inside a comment
296 runparams.exportdata.reset(new ExportData);
298 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
300 InsetText::docbook(xs, runparams);
302 if (params_.type == InsetNoteParams::Comment) {
304 xs << xml::EndTag("remark");
310 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
312 if (params_.type == InsetNoteParams::Note)
315 return InsetCollapsible::xhtml(xs, rp);
319 void InsetNote::validate(LaTeXFeatures & features) const
321 switch (params_.type) {
322 case InsetNoteParams::Comment:
323 if (features.runparams().flavor == Flavor::Html)
324 // we do output this but set display to "none" by default,
325 // but people might want to use it.
326 InsetCollapsible::validate(features);
328 // Only do the requires
329 features.useInsetLayout(getLayout());
331 case InsetNoteParams::Greyedout:
332 if (features.hasRTLLanguage())
333 features.require("environ");
334 InsetCollapsible::validate(features);
336 case InsetNoteParams::Note:
342 string InsetNote::contextMenuName() const
344 return "context-note";
347 bool InsetNote::allowSpellCheck() const
349 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
352 FontInfo InsetNote::getFont() const
354 FontInfo font = getLayout().font();
355 if (params_.type == InsetNoteParams::Greyedout
356 && buffer().params().isnotefontcolor) {
357 ColorCode c = lcolor.getFromLyXName("notefontcolor");
360 // This is the local color (not overridden by other documents)
361 ColorCode lc = lcolor.getFromLyXName("notefontcolor@" + buffer().fileName().absFileName());
362 if (lc != Color_none)
363 font.setPaintColor(lc);
369 string InsetNote::params2string(InsetNoteParams const & params)
372 data << "note" << ' ';
378 void InsetNote::string2params(string const & in, InsetNoteParams & params)
380 params = InsetNoteParams();
385 istringstream data(in);
388 lex.setContext("InsetNote::string2params");
390 // There are cases, such as when we are called via getStatus() from
391 // Dialog::canApply(), where we are just called with "note" rather
392 // than a full "note Note TYPE".