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 "InsetIterator.h"
27 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "output_docbook.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/lstrings.h"
40 #include "support/Translator.h"
42 #include "frontends/Application.h"
53 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
55 NoteTranslator const init_notetranslator()
57 NoteTranslator translator("Note", InsetNoteParams::Note);
58 translator.addPair("Comment", InsetNoteParams::Comment);
59 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
64 NoteTranslator const & notetranslator()
66 static NoteTranslator const translator = init_notetranslator();
74 InsetNoteParams::InsetNoteParams()
79 void InsetNoteParams::write(ostream & os) const
81 string const label = notetranslator().find(type);
82 os << "Note " << label << "\n";
86 void InsetNoteParams::read(Lexer & lex)
91 type = notetranslator().find(label);
95 /////////////////////////////////////////////////////////////////////
99 /////////////////////////////////////////////////////////////////////
101 InsetNote::InsetNote(Buffer * buf, string const & label)
102 : InsetCollapsible(buf)
104 params_.type = notetranslator().find(label);
108 InsetNote::~InsetNote()
110 hideDialogs("note", this);
114 docstring InsetNote::layoutName() const
116 return from_ascii("Note:" + notetranslator().find(params_.type));
120 void InsetNote::write(ostream & os) const
123 InsetCollapsible::write(os);
127 void InsetNote::read(Lexer & lex)
130 InsetCollapsible::read(lex);
134 bool InsetNote::showInsetDialog(BufferView * bv) const
136 bv->showDialog("note", params2string(params()),
137 const_cast<InsetNote *>(this));
142 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
144 switch (cmd.action()) {
146 case LFUN_INSET_MODIFY: {
147 // Do not do anything if converting to the same type of Note.
148 // A quick break here is done instead of disabling the LFUN
149 // because disabling the LFUN would lead to a greyed out
150 // entry, which might confuse users.
151 // In the future, we might want to have a radio button for
152 // switching between notes.
153 InsetNoteParams params;
154 string2params(to_utf8(cmd.argument()), params);
155 if (params_.type == params.type)
158 cur.recordUndoInset(this);
159 string2params(to_utf8(cmd.argument()), params_);
161 // what we really want here is a TOC update, but that means
162 // a full buffer update
163 cur.forceBufferUpdate();
167 case LFUN_INSET_DIALOG_UPDATE:
168 cur.bv().updateDialog("note", params2string(params()));
172 InsetCollapsible::doDispatch(cur, cmd);
178 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & flag) const
181 switch (cmd.action()) {
183 case LFUN_INSET_MODIFY:
184 if (cmd.getArg(0) == "note") {
185 InsetNoteParams params;
186 string2params(to_utf8(cmd.argument()), params);
187 flag.setOnOff(params_.type == params.type);
191 case LFUN_INSET_DIALOG_UPDATE:
192 flag.setEnabled(true);
196 return InsetCollapsible::getStatus(cur, cmd, flag);
201 bool InsetNote::isMacroScope() const
203 // LyX note has no latex output
204 if (params_.type == InsetNoteParams::Note)
207 return InsetCollapsible::isMacroScope();
211 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
213 if (params_.type == InsetNoteParams::Note)
216 OutputParams runparams(runparams_in);
217 if (params_.type == InsetNoteParams::Comment) {
218 runparams.inComment = true;
219 // Ignore files that are exported inside a comment
220 runparams.exportdata.reset(new ExportData);
223 // the space after the comment in 'a[comment] b' will be eaten by the
224 // comment environment since the space before b is ignored with the
225 // following latex output:
233 // Adding {} before ' b' fixes this.
234 // The {} will be automatically added, but only if needed, for all
235 // insets whose InsetLayout Display tag is false. This is achieved
236 // by telling otexstream to protect an immediately following space
237 // and is done for both comment and greyedout insets.
238 InsetCollapsible::latex(os, runparams);
240 runparams_in.encoding = runparams.encoding;
244 int InsetNote::plaintext(odocstringstream & os,
245 OutputParams const & runparams_in, size_t max_length) const
247 if (params_.type == InsetNoteParams::Note)
250 OutputParams runparams(runparams_in);
251 if (params_.type == InsetNoteParams::Comment) {
252 runparams.inComment = true;
253 // Ignore files that are exported inside a comment
254 runparams.exportdata.reset(new ExportData);
256 os << '[' << buffer().B_("note") << ":\n";
257 InsetText::plaintext(os, runparams, max_length);
260 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
264 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
266 if (params_.type == InsetNoteParams::Note)
269 OutputParams runparams(runparams_in);
270 if (params_.type == InsetNoteParams::Comment) {
271 xs << xml::StartTag("remark");
273 runparams.inComment = true;
274 // Ignore files that are exported inside a comment
275 runparams.exportdata.reset(new ExportData);
277 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
279 InsetText::docbook(xs, runparams);
281 if (params_.type == InsetNoteParams::Comment) {
283 xs << xml::EndTag("remark");
289 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
291 if (params_.type == InsetNoteParams::Note)
294 return InsetCollapsible::xhtml(xs, rp);
298 void InsetNote::validate(LaTeXFeatures & features) const
300 switch (params_.type) {
301 case InsetNoteParams::Comment:
302 if (features.runparams().flavor == OutputParams::HTML)
303 // we do output this but set display to "none" by default,
304 // but people might want to use it.
305 InsetCollapsible::validate(features);
307 // Only do the requires
308 features.useInsetLayout(getLayout());
310 case InsetNoteParams::Greyedout:
311 if (features.hasRTLLanguage())
312 features.require("environ");
313 InsetCollapsible::validate(features);
315 case InsetNoteParams::Note:
321 string InsetNote::contextMenuName() const
323 return "context-note";
326 bool InsetNote::allowSpellCheck() const
328 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
331 FontInfo InsetNote::getFont() const
333 FontInfo font = getLayout().font();
334 // FIXME: This hardcoded color is a hack!
335 if (params_.type == InsetNoteParams::Greyedout
336 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
337 ColorCode c = lcolor.getFromLyXName("notefontcolor");
345 string InsetNote::params2string(InsetNoteParams const & params)
348 data << "note" << ' ';
354 void InsetNote::string2params(string const & in, InsetNoteParams & params)
356 params = InsetNoteParams();
361 istringstream data(in);
364 lex.setContext("InsetNote::string2params");
366 // There are cases, such as when we are called via getStatus() from
367 // Dialog::canApply(), where we are just called with "note" rather
368 // than a full "note Note TYPE".