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"
29 #include "output_docbook.h"
30 #include "output_latex.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/Lexer.h"
36 #include "support/lstrings.h"
37 #include "support/Translator.h"
39 #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 const 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 : InsetCollapsible(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 void InsetNote::write(ostream & os) const
122 InsetCollapsible::write(os);
126 void InsetNote::read(Lexer & lex)
129 InsetCollapsible::read(lex);
133 bool InsetNote::showInsetDialog(BufferView * bv) const
135 bv->showDialog("note", params2string(params()),
136 const_cast<InsetNote *>(this));
141 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
143 switch (cmd.action()) {
145 case LFUN_INSET_MODIFY: {
146 if (cmd.getArg(0) != "note") {
147 // not for us; might be handled higher up
151 // Do not do anything if converting to the same type of Note.
152 // A quick break here is done instead of disabling the LFUN
153 // because disabling the LFUN would lead to a greyed out
154 // entry, which might confuse users.
155 // In the future, we might want to have a radio button for
156 // switching between notes.
157 InsetNoteParams params;
158 string2params(to_utf8(cmd.argument()), params);
159 if (params_.type == params.type)
162 cur.recordUndoInset(this);
163 string2params(to_utf8(cmd.argument()), params_);
165 // what we really want here is a TOC update, but that means
166 // a full buffer update
167 cur.forceBufferUpdate();
171 case LFUN_INSET_DIALOG_UPDATE:
172 cur.bv().updateDialog("note", params2string(params()));
176 InsetCollapsible::doDispatch(cur, cmd);
182 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
183 FuncStatus & flag) const
185 switch (cmd.action()) {
187 case LFUN_INSET_MODIFY:
188 if (cmd.getArg(0) == "note") {
189 InsetNoteParams params;
190 string2params(to_utf8(cmd.argument()), params);
191 flag.setOnOff(params_.type == params.type);
195 case LFUN_INSET_DIALOG_UPDATE:
196 flag.setEnabled(true);
200 return InsetCollapsible::getStatus(cur, cmd, flag);
205 bool InsetNote::isMacroScope() const
207 // LyX note has no latex output
208 if (params_.type == InsetNoteParams::Note)
211 return InsetCollapsible::isMacroScope();
215 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
217 if (params_.type != InsetNoteParams::Greyedout
218 && runparams_in.find_effective()
219 && !runparams_in.find_with_non_output())
222 if (params_.type == InsetNoteParams::Note) {
223 if (runparams_in.find_with_non_output()) {
224 OutputParams runparams(runparams_in);
225 InsetCollapsible::latex(os, runparams);
226 runparams_in.encoding = runparams.encoding;
231 OutputParams runparams(runparams_in);
232 if (params_.type == InsetNoteParams::Comment) {
233 if (runparams_in.inComment) {
234 // Nested comments should just output the contents.
235 latexParagraphs(buffer(), text(), os, runparams);
239 runparams.inComment = true;
240 // Ignore files that are exported inside a comment
241 runparams.exportdata.reset(new ExportData);
244 // the space after the comment in 'a[comment] b' will be eaten by the
245 // comment environment since the space before b is ignored with the
246 // following latex output:
254 // Adding {} before ' b' fixes this.
255 // The {} will be automatically added, but only if needed, for all
256 // insets whose InsetLayout Display tag is false. This is achieved
257 // by telling otexstream to protect an immediately following space
258 // and is done for both comment and greyedout insets.
259 InsetCollapsible::latex(os, runparams);
261 runparams_in.encoding = runparams.encoding;
265 int InsetNote::plaintext(odocstringstream & os,
266 OutputParams const & runparams_in, size_t max_length) const
268 if (!runparams_in.find_with_non_output()) {
269 if (params_.type == InsetNoteParams::Note)
271 else if (params_.type == InsetNoteParams::Comment
272 && runparams_in.find_effective())
276 OutputParams runparams(runparams_in);
277 if (params_.type != InsetNoteParams::Greyedout) {
278 runparams.inComment = true;
279 // Ignore files that are exported inside a comment
280 runparams.exportdata.reset(new ExportData);
282 if (!runparams_in.find_with_non_output())
283 os << '[' << buffer().B_("note") << ":\n";
284 InsetText::plaintext(os, runparams, max_length);
285 if (!runparams_in.find_with_non_output())
288 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
292 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
294 if (params_.type == InsetNoteParams::Note)
297 OutputParams runparams(runparams_in);
298 if (params_.type == InsetNoteParams::Comment) {
299 xs << xml::StartTag("remark");
301 runparams.inComment = true;
302 // Ignore files that are exported inside a comment
303 runparams.exportdata.reset(new ExportData);
305 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
307 InsetText::docbook(xs, runparams);
309 if (params_.type == InsetNoteParams::Comment) {
311 xs << xml::EndTag("remark");
317 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
319 if (params_.type == InsetNoteParams::Note)
322 return InsetCollapsible::xhtml(xs, rp);
326 void InsetNote::validate(LaTeXFeatures & features) const
328 switch (params_.type) {
329 case InsetNoteParams::Comment:
330 if (features.runparams().flavor == Flavor::Html)
331 // we do output this but set display to "none" by default,
332 // but people might want to use it.
333 InsetCollapsible::validate(features);
335 // Only do the requires
336 features.useInsetLayout(getLayout());
338 case InsetNoteParams::Greyedout:
339 InsetCollapsible::validate(features);
341 case InsetNoteParams::Note:
342 // Showing previews in this inset may require stuff
343 if (features.runparams().for_preview)
344 InsetCollapsible::validate(features);
350 string InsetNote::contextMenuName() const
352 return "context-note";
355 bool InsetNote::allowSpellCheck() const
357 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
360 FontInfo InsetNote::getFont() const
362 FontInfo font = getLayout().font();
363 if (params_.type == InsetNoteParams::Greyedout
364 && buffer().params().isnotefontcolor) {
365 ColorCode c = lcolor.getFromLyXName("notefontcolor");
368 // This is the local color (not overridden by other documents)
369 ColorCode lc = lcolor.getFromLyXName("notefontcolor@" + buffer().fileName().absFileName());
370 if (lc != Color_none)
371 font.setPaintColor(lc);
377 string InsetNote::params2string(InsetNoteParams const & params)
380 data << "note" << ' ';
386 void InsetNote::string2params(string const & in, InsetNoteParams & params)
388 params = InsetNoteParams();
393 istringstream data(in);
396 lex.setContext("InsetNote::string2params");
398 // There are cases, such as when we are called via getStatus() from
399 // Dialog::canApply(), where we are just called with "note" rather
400 // than a full "note Note TYPE".