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"
22 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "output_docbook.h"
30 #include "TextClass.h"
31 #include "TocBackend.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::Note)
213 OutputParams runparams(runparams_in);
214 if (params_.type == InsetNoteParams::Comment) {
215 runparams.inComment = true;
216 // Ignore files that are exported inside a comment
217 runparams.exportdata.reset(new ExportData);
220 // the space after the comment in 'a[comment] b' will be eaten by the
221 // comment environment since the space before b is ignored with the
222 // following latex output:
230 // Adding {} before ' b' fixes this.
231 // The {} will be automatically added, but only if needed, for all
232 // insets whose InsetLayout Display tag is false. This is achieved
233 // by telling otexstream to protect an immediately following space
234 // and is done for both comment and greyedout insets.
235 InsetCollapsible::latex(os, runparams);
237 runparams_in.encoding = runparams.encoding;
241 int InsetNote::plaintext(odocstringstream & os,
242 OutputParams const & runparams_in, size_t max_length) const
244 if (params_.type == InsetNoteParams::Note)
247 OutputParams runparams(runparams_in);
248 if (params_.type == InsetNoteParams::Comment) {
249 runparams.inComment = true;
250 // Ignore files that are exported inside a comment
251 runparams.exportdata.reset(new ExportData);
253 os << '[' << buffer().B_("note") << ":\n";
254 InsetText::plaintext(os, runparams, max_length);
257 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
261 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
263 if (params_.type == InsetNoteParams::Note)
266 OutputParams runparams(runparams_in);
267 if (params_.type == InsetNoteParams::Comment) {
268 xs << xml::StartTag("remark");
270 runparams.inComment = true;
271 // Ignore files that are exported inside a comment
272 runparams.exportdata.reset(new ExportData);
274 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
276 InsetText::docbook(xs, runparams);
278 if (params_.type == InsetNoteParams::Comment) {
280 xs << xml::EndTag("remark");
286 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
288 if (params_.type == InsetNoteParams::Note)
291 return InsetCollapsible::xhtml(xs, rp);
295 void InsetNote::validate(LaTeXFeatures & features) const
297 switch (params_.type) {
298 case InsetNoteParams::Comment:
299 if (features.runparams().flavor == Flavor::Html)
300 // we do output this but set display to "none" by default,
301 // but people might want to use it.
302 InsetCollapsible::validate(features);
304 // Only do the requires
305 features.useInsetLayout(getLayout());
307 case InsetNoteParams::Greyedout:
308 if (features.hasRTLLanguage())
309 features.require("environ");
310 InsetCollapsible::validate(features);
312 case InsetNoteParams::Note:
318 string InsetNote::contextMenuName() const
320 return "context-note";
323 bool InsetNote::allowSpellCheck() const
325 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
328 FontInfo InsetNote::getFont() const
330 FontInfo font = getLayout().font();
331 // FIXME: This hardcoded color is a hack!
332 if (params_.type == InsetNoteParams::Greyedout
333 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
334 ColorCode c = lcolor.getFromLyXName("notefontcolor");
342 string InsetNote::params2string(InsetNoteParams const & params)
345 data << "note" << ' ';
351 void InsetNote::string2params(string const & in, InsetNoteParams & params)
353 params = InsetNoteParams();
358 istringstream data(in);
361 lex.setContext("InsetNote::string2params");
363 // There are cases, such as when we are called via getStatus() from
364 // Dialog::canApply(), where we are just called with "note" rather
365 // than a full "note Note TYPE".