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"
20 #include "BufferParams.h"
24 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "InsetIterator.h"
29 #include "LaTeXFeatures.h"
32 #include "OutputParams.h"
33 #include "ParIterator.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.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 : InsetCollapsable(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 Inset::DisplayType InsetNote::display() const
126 void InsetNote::write(ostream & os) const
129 InsetCollapsable::write(os);
133 void InsetNote::read(Lexer & lex)
136 InsetCollapsable::read(lex);
140 bool InsetNote::showInsetDialog(BufferView * bv) const
142 bv->showDialog("note", params2string(params()),
143 const_cast<InsetNote *>(this));
148 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
150 switch (cmd.action()) {
152 case LFUN_INSET_MODIFY: {
153 // Do not do anything if converting to the same type of Note.
154 // A quick break here is done instead of disabling the LFUN
155 // because disabling the LFUN would lead to a greyed out
156 // entry, which might confuse users.
157 // In the future, we might want to have a radio button for
158 // switching between notes.
159 InsetNoteParams params;
160 string2params(to_utf8(cmd.argument()), params);
161 if (params_.type == params.type)
164 cur.recordUndoInset(this);
165 string2params(to_utf8(cmd.argument()), params_);
167 // what we really want here is a TOC update, but that means
168 // a full buffer update
169 cur.forceBufferUpdate();
173 case LFUN_INSET_DIALOG_UPDATE:
174 cur.bv().updateDialog("note", params2string(params()));
178 InsetCollapsable::doDispatch(cur, cmd);
184 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
185 FuncStatus & flag) const
187 switch (cmd.action()) {
189 case LFUN_INSET_MODIFY:
190 // disallow comment and greyed out in commands
191 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
192 cmd.getArg(2) == "Note");
193 if (cmd.getArg(0) == "note") {
194 InsetNoteParams params;
195 string2params(to_utf8(cmd.argument()), params);
196 flag.setOnOff(params_.type == params.type);
200 case LFUN_INSET_DIALOG_UPDATE:
201 flag.setEnabled(true);
205 return InsetCollapsable::getStatus(cur, cmd, flag);
210 void InsetNote::addToToc(DocIterator const & cpit, bool output_active,
211 UpdateType utype) const
213 DocIterator pit = cpit;
214 pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
216 shared_ptr<Toc> toc = buffer().tocBackend().toc("note");
217 InsetLayout const & il = getLayout();
218 docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
219 text().forOutliner(str, TOC_ENTRY_LENGTH);
220 toc->push_back(TocItem(pit, 0, str, output_active, toolTipText(docstring(), 3, 60)));
222 // Proceed with the rest of the inset.
223 bool doing_output = output_active && producesOutput();
224 InsetCollapsable::addToToc(cpit, doing_output, utype);
228 bool InsetNote::isMacroScope() const
230 // LyX note has no latex output
231 if (params_.type == InsetNoteParams::Note)
234 return InsetCollapsable::isMacroScope();
238 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
240 if (params_.type == InsetNoteParams::Note)
243 OutputParams runparams(runparams_in);
244 if (params_.type == InsetNoteParams::Comment) {
245 runparams.inComment = true;
246 // Ignore files that are exported inside a comment
247 runparams.exportdata.reset(new ExportData);
250 // the space after the comment in 'a[comment] b' will be eaten by the
251 // comment environment since the space before b is ignored with the
252 // following latex output:
260 // Adding {} before ' b' fixes this.
261 // The {} will be automatically added, but only if needed, for all
262 // insets whose InsetLayout Display tag is false. This is achieved
263 // by telling otexstream to protect an immediately following space
264 // and is done for both comment and greyedout insets.
265 InsetCollapsable::latex(os, runparams);
267 runparams_in.encoding = runparams.encoding;
271 int InsetNote::plaintext(odocstringstream & os,
272 OutputParams const & runparams_in, size_t max_length) const
274 if (params_.type == InsetNoteParams::Note)
277 OutputParams runparams(runparams_in);
278 if (params_.type == InsetNoteParams::Comment) {
279 runparams.inComment = true;
280 // Ignore files that are exported inside a comment
281 runparams.exportdata.reset(new ExportData);
283 os << '[' << buffer().B_("note") << ":\n";
284 InsetText::plaintext(os, runparams, max_length);
287 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
291 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
293 if (params_.type == InsetNoteParams::Note)
296 OutputParams runparams(runparams_in);
297 if (params_.type == InsetNoteParams::Comment) {
299 runparams.inComment = true;
300 // Ignore files that are exported inside a comment
301 runparams.exportdata.reset(new ExportData);
304 int const n = InsetText::docbook(os, runparams);
306 if (params_.type == InsetNoteParams::Comment)
307 os << "\n</remark>\n";
309 // Return how many newlines we issued.
310 //return int(count(str.begin(), str.end(), '\n'));
315 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
317 if (params_.type == InsetNoteParams::Note)
320 return InsetCollapsable::xhtml(xs, rp);
324 void InsetNote::validate(LaTeXFeatures & features) const
326 switch (params_.type) {
327 case InsetNoteParams::Comment:
328 features.require("verbatim");
329 if (features.runparams().flavor == OutputParams::HTML)
330 // we do output this but set display to "none" by default,
331 // but people might want to use it.
332 InsetCollapsable::validate(features);
334 case InsetNoteParams::Greyedout:
335 features.require("color");
336 features.require("lyxgreyedout");
337 InsetCollapsable::validate(features);
339 case InsetNoteParams::Note:
345 string InsetNote::contextMenuName() const
347 return "context-note";
350 bool InsetNote::allowSpellCheck() const
352 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
355 FontInfo InsetNote::getFont() const
357 FontInfo font = getLayout().font();
358 // FIXME: This hardcoded color is a hack!
359 if (params_.type == InsetNoteParams::Greyedout
360 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
361 ColorCode c = lcolor.getFromLyXName("notefontcolor");
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".