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/lstrings.h"
41 #include "support/Translator.h"
43 #include "frontends/Application.h"
54 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
56 NoteTranslator const init_notetranslator()
58 NoteTranslator translator("Note", InsetNoteParams::Note);
59 translator.addPair("Comment", InsetNoteParams::Comment);
60 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
65 NoteTranslator const & notetranslator()
67 static NoteTranslator const translator = init_notetranslator();
75 InsetNoteParams::InsetNoteParams()
80 void InsetNoteParams::write(ostream & os) const
82 string const label = notetranslator().find(type);
83 os << "Note " << label << "\n";
87 void InsetNoteParams::read(Lexer & lex)
92 type = notetranslator().find(label);
96 /////////////////////////////////////////////////////////////////////
100 /////////////////////////////////////////////////////////////////////
102 InsetNote::InsetNote(Buffer * buf, string const & label)
103 : InsetCollapsable(buf)
105 params_.type = notetranslator().find(label);
109 InsetNote::~InsetNote()
111 hideDialogs("note", this);
115 docstring InsetNote::layoutName() const
117 return from_ascii("Note:" + notetranslator().find(params_.type));
121 Inset::DisplayType InsetNote::display() const
127 void InsetNote::write(ostream & os) const
130 InsetCollapsable::write(os);
134 void InsetNote::read(Lexer & lex)
137 InsetCollapsable::read(lex);
141 bool InsetNote::showInsetDialog(BufferView * bv) const
143 bv->showDialog("note", params2string(params()),
144 const_cast<InsetNote *>(this));
149 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
151 switch (cmd.action()) {
153 case LFUN_INSET_MODIFY: {
154 // Do not do anything if converting to the same type of Note.
155 // A quick break here is done instead of disabling the LFUN
156 // because disabling the LFUN would lead to a greyed out
157 // entry, which might confuse users.
158 // In the future, we might want to have a radio button for
159 // switching between notes.
160 InsetNoteParams params;
161 string2params(to_utf8(cmd.argument()), params);
162 if (params_.type == params.type)
165 cur.recordUndoInset(this);
166 string2params(to_utf8(cmd.argument()), params_);
168 // what we really want here is a TOC update, but that means
169 // a full buffer update
170 cur.forceBufferUpdate();
174 case LFUN_INSET_DIALOG_UPDATE:
175 cur.bv().updateDialog("note", params2string(params()));
179 InsetCollapsable::doDispatch(cur, cmd);
185 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
186 FuncStatus & flag) const
188 switch (cmd.action()) {
190 case LFUN_INSET_MODIFY:
191 // disallow comment and greyed out in commands
192 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
193 cmd.getArg(2) == "Note");
194 if (cmd.getArg(0) == "note") {
195 InsetNoteParams params;
196 string2params(to_utf8(cmd.argument()), params);
197 flag.setOnOff(params_.type == params.type);
201 case LFUN_INSET_DIALOG_UPDATE:
202 flag.setEnabled(true);
206 return InsetCollapsable::getStatus(cur, cmd, flag);
211 void InsetNote::addToToc(DocIterator const & cpit, bool output_active,
212 UpdateType utype) const
214 DocIterator pit = cpit;
215 pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
217 InsetLayout const & il = getLayout();
219 text().forOutliner(str, TOC_ENTRY_LENGTH);
220 str = translateIfPossible(il.labelstring()) + ": " + str;
222 shared_ptr<Toc> toc = buffer().tocBackend().toc("note");
223 toc->push_back(TocItem(pit, 0, str, output_active));
225 // Proceed with the rest of the inset.
226 bool doing_output = output_active && producesOutput();
227 InsetCollapsable::addToToc(cpit, doing_output, utype);
231 bool InsetNote::isMacroScope() const
233 // LyX note has no latex output
234 if (params_.type == InsetNoteParams::Note)
237 return InsetCollapsable::isMacroScope();
241 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
243 if (params_.type == InsetNoteParams::Note)
246 OutputParams runparams(runparams_in);
247 if (params_.type == InsetNoteParams::Comment) {
248 runparams.inComment = true;
249 // Ignore files that are exported inside a comment
250 runparams.exportdata.reset(new ExportData);
253 // the space after the comment in 'a[comment] b' will be eaten by the
254 // comment environment since the space before b is ignored with the
255 // following latex output:
263 // Adding {} before ' b' fixes this.
264 // The {} will be automatically added, but only if needed, for all
265 // insets whose InsetLayout Display tag is false. This is achieved
266 // by telling otexstream to protect an immediately following space
267 // and is done for both comment and greyedout insets.
268 InsetCollapsable::latex(os, runparams);
270 runparams_in.encoding = runparams.encoding;
274 int InsetNote::plaintext(odocstringstream & os,
275 OutputParams const & runparams_in, size_t max_length) const
277 if (params_.type == InsetNoteParams::Note)
280 OutputParams runparams(runparams_in);
281 if (params_.type == InsetNoteParams::Comment) {
282 runparams.inComment = true;
283 // Ignore files that are exported inside a comment
284 runparams.exportdata.reset(new ExportData);
286 os << '[' << buffer().B_("note") << ":\n";
287 InsetText::plaintext(os, runparams, max_length);
290 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
294 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
296 if (params_.type == InsetNoteParams::Note)
299 OutputParams runparams(runparams_in);
300 if (params_.type == InsetNoteParams::Comment) {
302 runparams.inComment = true;
303 // Ignore files that are exported inside a comment
304 runparams.exportdata.reset(new ExportData);
307 int const n = InsetText::docbook(os, runparams);
309 if (params_.type == InsetNoteParams::Comment)
310 os << "\n</remark>\n";
312 // Return how many newlines we issued.
313 //return int(count(str.begin(), str.end(), '\n'));
318 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
320 if (params_.type == InsetNoteParams::Note)
323 return InsetCollapsable::xhtml(xs, rp);
327 void InsetNote::validate(LaTeXFeatures & features) const
329 switch (params_.type) {
330 case InsetNoteParams::Comment:
331 if (features.runparams().flavor == OutputParams::HTML)
332 // we do output this but set display to "none" by default,
333 // but people might want to use it.
334 InsetCollapsable::validate(features);
336 // Only do the requires
337 features.useInsetLayout(getLayout());
339 case InsetNoteParams::Greyedout:
340 InsetCollapsable::validate(features);
342 case InsetNoteParams::Note:
348 string InsetNote::contextMenuName() const
350 return "context-note";
353 bool InsetNote::allowSpellCheck() const
355 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
358 FontInfo InsetNote::getFont() const
360 FontInfo font = getLayout().font();
361 // FIXME: This hardcoded color is a hack!
362 if (params_.type == InsetNoteParams::Greyedout
363 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
364 ColorCode c = lcolor.getFromLyXName("notefontcolor");
372 string InsetNote::params2string(InsetNoteParams const & params)
375 data << "note" << ' ';
381 void InsetNote::string2params(string const & in, InsetNoteParams & params)
383 params = InsetNoteParams();
388 istringstream data(in);
391 lex.setContext("InsetNote::string2params");
393 // There are cases, such as when we are called via getStatus() from
394 // Dialog::canApply(), where we are just called with "note" rather
395 // than a full "note Note TYPE".