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) const
212 DocIterator pit = cpit;
213 pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
215 Toc & toc = buffer().tocBackend().toc("note");
216 InsetLayout const & il = getLayout();
217 docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
218 text().forOutliner(str, TOC_ENTRY_LENGTH);
219 toc.push_back(TocItem(pit, 0, str, output_active, toolTipText(docstring(), 3, 60)));
221 // Proceed with the rest of the inset.
222 bool doing_output = output_active && producesOutput();
223 InsetCollapsable::addToToc(cpit, doing_output);
227 bool InsetNote::isMacroScope() const
229 // LyX note has no latex output
230 if (params_.type == InsetNoteParams::Note)
233 return InsetCollapsable::isMacroScope();
237 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
239 if (params_.type == InsetNoteParams::Note)
242 OutputParams runparams(runparams_in);
243 if (params_.type == InsetNoteParams::Comment) {
244 runparams.inComment = true;
245 // Ignore files that are exported inside a comment
246 runparams.exportdata.reset(new ExportData);
249 // the space after the comment in 'a[comment] b' will be eaten by the
250 // comment environment since the space before b is ignored with the
251 // following latex output:
259 // Adding {} before ' b' fixes this.
260 // The {} will be automatically added, but only if needed, for all
261 // insets whose InsetLayout Display tag is false. This is achieved
262 // by telling otexstream to protect an immediately following space
263 // and is done for both comment and greyedout insets.
264 InsetCollapsable::latex(os, runparams);
266 runparams_in.encoding = runparams.encoding;
270 int InsetNote::plaintext(odocstringstream & os,
271 OutputParams const & runparams_in, size_t max_length) const
273 if (params_.type == InsetNoteParams::Note)
276 OutputParams runparams(runparams_in);
277 if (params_.type == InsetNoteParams::Comment) {
278 runparams.inComment = true;
279 // Ignore files that are exported inside a comment
280 runparams.exportdata.reset(new ExportData);
282 os << '[' << buffer().B_("note") << ":\n";
283 InsetText::plaintext(os, runparams, max_length);
286 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
290 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
292 if (params_.type == InsetNoteParams::Note)
295 OutputParams runparams(runparams_in);
296 if (params_.type == InsetNoteParams::Comment) {
298 runparams.inComment = true;
299 // Ignore files that are exported inside a comment
300 runparams.exportdata.reset(new ExportData);
303 int const n = InsetText::docbook(os, runparams);
305 if (params_.type == InsetNoteParams::Comment)
306 os << "\n</remark>\n";
308 // Return how many newlines we issued.
309 //return int(count(str.begin(), str.end(), '\n'));
314 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
316 if (params_.type == InsetNoteParams::Note)
319 return InsetCollapsable::xhtml(xs, rp);
323 void InsetNote::validate(LaTeXFeatures & features) const
325 switch (params_.type) {
326 case InsetNoteParams::Comment:
327 features.require("verbatim");
328 if (features.runparams().flavor == OutputParams::HTML)
329 // we do output this but set display to "none" by default,
330 // but people might want to use it.
331 InsetCollapsable::validate(features);
333 case InsetNoteParams::Greyedout:
334 features.require("color");
335 features.require("lyxgreyedout");
336 InsetCollapsable::validate(features);
338 case InsetNoteParams::Note:
344 string InsetNote::contextMenuName() const
346 return "context-note";
349 bool InsetNote::allowSpellCheck() const
351 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
354 FontInfo InsetNote::getFont() const
356 FontInfo font = getLayout().font();
357 // FIXME: This hardcoded color is a hack!
358 if (params_.type == InsetNoteParams::Greyedout
359 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
360 ColorCode c = lcolor.getFromLyXName("notefontcolor");
368 string InsetNote::params2string(InsetNoteParams const & params)
371 data << "note" << ' ';
377 void InsetNote::string2params(string const & in, InsetNoteParams & params)
379 params = InsetNoteParams();
384 istringstream data(in);
387 lex.setContext("InsetNote::string2params");
389 // There are cases, such as when we are called via getStatus() from
390 // Dialog::canApply(), where we are just called with "note" rather
391 // than a full "note Note TYPE".