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"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
38 #include "frontends/Application.h"
49 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
51 NoteTranslator const init_notetranslator()
53 NoteTranslator translator("Note", InsetNoteParams::Note);
54 translator.addPair("Comment", InsetNoteParams::Comment);
55 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
60 NoteTranslator const & notetranslator()
62 static NoteTranslator const translator = init_notetranslator();
70 InsetNoteParams::InsetNoteParams()
75 void InsetNoteParams::write(ostream & os) const
77 string const label = notetranslator().find(type);
78 os << "Note " << label << "\n";
82 void InsetNoteParams::read(Lexer & lex)
87 type = notetranslator().find(label);
91 /////////////////////////////////////////////////////////////////////
95 /////////////////////////////////////////////////////////////////////
97 InsetNote::InsetNote(Buffer * buf, string const & label)
98 : InsetCollapsible(buf)
100 params_.type = notetranslator().find(label);
104 InsetNote::~InsetNote()
106 hideDialogs("note", this);
110 docstring InsetNote::layoutName() const
112 return from_ascii("Note:" + notetranslator().find(params_.type));
116 void InsetNote::write(ostream & os) const
119 InsetCollapsible::write(os);
123 void InsetNote::read(Lexer & lex)
126 InsetCollapsible::read(lex);
130 bool InsetNote::showInsetDialog(BufferView * bv) const
132 bv->showDialog("note", params2string(params()),
133 const_cast<InsetNote *>(this));
138 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
140 switch (cmd.action()) {
142 case LFUN_INSET_MODIFY: {
143 // Do not do anything if converting to the same type of Note.
144 // A quick break here is done instead of disabling the LFUN
145 // because disabling the LFUN would lead to a greyed out
146 // entry, which might confuse users.
147 // In the future, we might want to have a radio button for
148 // switching between notes.
149 InsetNoteParams params;
150 string2params(to_utf8(cmd.argument()), params);
151 if (params_.type == params.type)
154 cur.recordUndoInset(this);
155 string2params(to_utf8(cmd.argument()), params_);
157 // what we really want here is a TOC update, but that means
158 // a full buffer update
159 cur.forceBufferUpdate();
163 case LFUN_INSET_DIALOG_UPDATE:
164 cur.bv().updateDialog("note", params2string(params()));
168 InsetCollapsible::doDispatch(cur, cmd);
174 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
175 FuncStatus & flag) const
177 switch (cmd.action()) {
179 case LFUN_INSET_MODIFY:
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 InsetCollapsible::getStatus(cur, cmd, flag);
197 bool InsetNote::isMacroScope() const
199 // LyX note has no latex output
200 if (params_.type == InsetNoteParams::Note)
203 return InsetCollapsible::isMacroScope();
207 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
209 if (params_.type != InsetNoteParams::Greyedout
210 && runparams_in.find_effective()
211 && !runparams_in.find_with_non_output())
214 if (params_.type == InsetNoteParams::Note) {
215 if (runparams_in.find_with_non_output()) {
216 OutputParams runparams(runparams_in);
217 InsetCollapsible::latex(os, runparams);
218 runparams_in.encoding = runparams.encoding;
223 OutputParams runparams(runparams_in);
224 if (params_.type == InsetNoteParams::Comment) {
225 runparams.inComment = true;
226 // Ignore files that are exported inside a comment
227 runparams.exportdata.reset(new ExportData);
230 // the space after the comment in 'a[comment] b' will be eaten by the
231 // comment environment since the space before b is ignored with the
232 // following latex output:
240 // Adding {} before ' b' fixes this.
241 // The {} will be automatically added, but only if needed, for all
242 // insets whose InsetLayout Display tag is false. This is achieved
243 // by telling otexstream to protect an immediately following space
244 // and is done for both comment and greyedout insets.
245 InsetCollapsible::latex(os, runparams);
247 runparams_in.encoding = runparams.encoding;
251 int InsetNote::plaintext(odocstringstream & os,
252 OutputParams const & runparams_in, size_t max_length) const
254 if (!runparams_in.find_with_non_output()) {
255 if (params_.type == InsetNoteParams::Note)
257 else if (params_.type == InsetNoteParams::Comment
258 && runparams_in.find_effective())
262 OutputParams runparams(runparams_in);
263 if (params_.type != InsetNoteParams::Greyedout) {
264 runparams.inComment = true;
265 // Ignore files that are exported inside a comment
266 runparams.exportdata.reset(new ExportData);
268 if (!runparams_in.find_with_non_output())
269 os << '[' << buffer().B_("note") << ":\n";
270 InsetText::plaintext(os, runparams, max_length);
271 if (!runparams_in.find_with_non_output())
274 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
278 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
280 if (params_.type == InsetNoteParams::Note)
283 OutputParams runparams(runparams_in);
284 if (params_.type == InsetNoteParams::Comment) {
285 xs << xml::StartTag("remark");
287 runparams.inComment = true;
288 // Ignore files that are exported inside a comment
289 runparams.exportdata.reset(new ExportData);
291 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
293 InsetText::docbook(xs, runparams);
295 if (params_.type == InsetNoteParams::Comment) {
297 xs << xml::EndTag("remark");
303 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
305 if (params_.type == InsetNoteParams::Note)
308 return InsetCollapsible::xhtml(xs, rp);
312 void InsetNote::validate(LaTeXFeatures & features) const
314 switch (params_.type) {
315 case InsetNoteParams::Comment:
316 if (features.runparams().flavor == Flavor::Html)
317 // we do output this but set display to "none" by default,
318 // but people might want to use it.
319 InsetCollapsible::validate(features);
321 // Only do the requires
322 features.useInsetLayout(getLayout());
324 case InsetNoteParams::Greyedout:
325 if (features.hasRTLLanguage())
326 features.require("environ");
327 InsetCollapsible::validate(features);
329 case InsetNoteParams::Note:
335 string InsetNote::contextMenuName() const
337 return "context-note";
340 bool InsetNote::allowSpellCheck() const
342 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
345 FontInfo InsetNote::getFont() const
347 FontInfo font = getLayout().font();
348 if (params_.type == InsetNoteParams::Greyedout
349 && buffer().params().isnotefontcolor) {
350 ColorCode c = lcolor.getFromLyXName("notefontcolor");
353 // This is the local color (not overridden by other documents)
354 ColorCode lc = lcolor.getFromLyXName("notefontcolor@" + buffer().fileName().absFileName());
355 if (lc != Color_none)
356 font.setPaintColor(lc);
362 string InsetNote::params2string(InsetNoteParams const & params)
365 data << "note" << ' ';
371 void InsetNote::string2params(string const & in, InsetNoteParams & params)
373 params = InsetNoteParams();
378 istringstream data(in);
381 lex.setContext("InsetNote::string2params");
383 // There are cases, such as when we are called via getStatus() from
384 // Dialog::canApply(), where we are just called with "note" rather
385 // than a full "note Note TYPE".